00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/channel_layout.h"
00023 #include "libavutil/common.h"
00024 #include "avformat.h"
00025 #include "internal.h"
00026 #include "gxf.h"
00027 #include "libavcodec/mpeg12data.h"
00028
00029 struct gxf_stream_info {
00030 int64_t first_field;
00031 int64_t last_field;
00032 AVRational frames_per_second;
00033 int32_t fields_per_frame;
00034 int64_t track_aux_data;
00035 };
00036
00040 static int add_timecode_metadata(AVDictionary **pm, const char *key, uint32_t timecode, int fields_per_frame)
00041 {
00042 char tmp[128];
00043 int field = timecode & 0xff;
00044 int frame = fields_per_frame ? field / fields_per_frame : field;
00045 int second = (timecode >> 8) & 0xff;
00046 int minute = (timecode >> 16) & 0xff;
00047 int hour = (timecode >> 24) & 0x1f;
00048 int drop = (timecode >> 29) & 1;
00049
00050
00051 if (timecode >> 31)
00052 return 0;
00053 snprintf(tmp, sizeof(tmp), "%02d:%02d:%02d%c%02d",
00054 hour, minute, second, drop ? ';' : ':', frame);
00055 return av_dict_set(pm, key, tmp, 0);
00056 }
00057
00065 static int parse_packet_header(AVIOContext *pb, GXFPktType *type, int *length) {
00066 if (avio_rb32(pb))
00067 return 0;
00068 if (avio_r8(pb) != 1)
00069 return 0;
00070 *type = avio_r8(pb);
00071 *length = avio_rb32(pb);
00072 if ((*length >> 24) || *length < 16)
00073 return 0;
00074 *length -= 16;
00075 if (avio_rb32(pb))
00076 return 0;
00077 if (avio_r8(pb) != 0xe1)
00078 return 0;
00079 if (avio_r8(pb) != 0xe2)
00080 return 0;
00081 return 1;
00082 }
00083
00087 static int gxf_probe(AVProbeData *p) {
00088 static const uint8_t startcode[] = {0, 0, 0, 0, 1, 0xbc};
00089 static const uint8_t endcode[] = {0, 0, 0, 0, 0xe1, 0xe2};
00090 if (!memcmp(p->buf, startcode, sizeof(startcode)) &&
00091 !memcmp(&p->buf[16 - sizeof(endcode)], endcode, sizeof(endcode)))
00092 return AVPROBE_SCORE_MAX;
00093 return 0;
00094 }
00095
00102 static int get_sindex(AVFormatContext *s, int id, int format) {
00103 int i;
00104 AVStream *st = NULL;
00105 i = ff_find_stream_index(s, id);
00106 if (i >= 0)
00107 return i;
00108 st = avformat_new_stream(s, NULL);
00109 if (!st)
00110 return AVERROR(ENOMEM);
00111 st->id = id;
00112 switch (format) {
00113 case 3:
00114 case 4:
00115 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00116 st->codec->codec_id = AV_CODEC_ID_MJPEG;
00117 break;
00118 case 13:
00119 case 15:
00120 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00121 st->codec->codec_id = AV_CODEC_ID_DVVIDEO;
00122 break;
00123 case 14:
00124 case 16:
00125 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00126 st->codec->codec_id = AV_CODEC_ID_DVVIDEO;
00127 break;
00128 case 11:
00129 case 12:
00130 case 20:
00131 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00132 st->codec->codec_id = AV_CODEC_ID_MPEG2VIDEO;
00133 st->need_parsing = AVSTREAM_PARSE_HEADERS;
00134 break;
00135 case 22:
00136 case 23:
00137 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00138 st->codec->codec_id = AV_CODEC_ID_MPEG1VIDEO;
00139 st->need_parsing = AVSTREAM_PARSE_HEADERS;
00140 break;
00141 case 9:
00142 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00143 st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
00144 st->codec->channels = 1;
00145 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
00146 st->codec->sample_rate = 48000;
00147 st->codec->bit_rate = 3 * 1 * 48000 * 8;
00148 st->codec->block_align = 3 * 1;
00149 st->codec->bits_per_coded_sample = 24;
00150 break;
00151 case 10:
00152 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00153 st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
00154 st->codec->channels = 1;
00155 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
00156 st->codec->sample_rate = 48000;
00157 st->codec->bit_rate = 2 * 1 * 48000 * 8;
00158 st->codec->block_align = 2 * 1;
00159 st->codec->bits_per_coded_sample = 16;
00160 break;
00161 case 17:
00162 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00163 st->codec->codec_id = AV_CODEC_ID_AC3;
00164 st->codec->channels = 2;
00165 st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
00166 st->codec->sample_rate = 48000;
00167 break;
00168
00169 case 7:
00170 case 8:
00171 case 24:
00172 st->codec->codec_type = AVMEDIA_TYPE_DATA;
00173 st->codec->codec_id = AV_CODEC_ID_NONE;
00174 break;
00175 default:
00176 st->codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
00177 st->codec->codec_id = AV_CODEC_ID_NONE;
00178 break;
00179 }
00180 return s->nb_streams - 1;
00181 }
00182
00188 static void gxf_material_tags(AVIOContext *pb, int *len, struct gxf_stream_info *si) {
00189 si->first_field = AV_NOPTS_VALUE;
00190 si->last_field = AV_NOPTS_VALUE;
00191 while (*len >= 2) {
00192 GXFMatTag tag = avio_r8(pb);
00193 int tlen = avio_r8(pb);
00194 *len -= 2;
00195 if (tlen > *len)
00196 return;
00197 *len -= tlen;
00198 if (tlen == 4) {
00199 uint32_t value = avio_rb32(pb);
00200 if (tag == MAT_FIRST_FIELD)
00201 si->first_field = value;
00202 else if (tag == MAT_LAST_FIELD)
00203 si->last_field = value;
00204 } else
00205 avio_skip(pb, tlen);
00206 }
00207 }
00208
00209 static const AVRational frame_rate_tab[] = {
00210 { 60, 1},
00211 {60000, 1001},
00212 { 50, 1},
00213 { 30, 1},
00214 {30000, 1001},
00215 { 25, 1},
00216 { 24, 1},
00217 {24000, 1001},
00218 { 0, 0},
00219 };
00220
00226 static AVRational fps_tag2avr(int32_t fps) {
00227 if (fps < 1 || fps > 9) fps = 9;
00228 return frame_rate_tab[fps - 1];
00229 }
00230
00236 static AVRational fps_umf2avr(uint32_t flags) {
00237 static const AVRational map[] = {{50, 1}, {60000, 1001}, {24, 1},
00238 {25, 1}, {30000, 1001}};
00239 int idx = av_log2((flags & 0x7c0) >> 6);
00240 return map[idx];
00241 }
00242
00248 static void gxf_track_tags(AVIOContext *pb, int *len, struct gxf_stream_info *si) {
00249 si->frames_per_second = (AVRational){0, 0};
00250 si->fields_per_frame = 0;
00251 si->track_aux_data = 0x80000000;
00252 while (*len >= 2) {
00253 GXFTrackTag tag = avio_r8(pb);
00254 int tlen = avio_r8(pb);
00255 *len -= 2;
00256 if (tlen > *len)
00257 return;
00258 *len -= tlen;
00259 if (tlen == 4) {
00260 uint32_t value = avio_rb32(pb);
00261 if (tag == TRACK_FPS)
00262 si->frames_per_second = fps_tag2avr(value);
00263 else if (tag == TRACK_FPF && (value == 1 || value == 2))
00264 si->fields_per_frame = value;
00265 } else if (tlen == 8 && tag == TRACK_AUX)
00266 si->track_aux_data = avio_rl64(pb);
00267 else
00268 avio_skip(pb, tlen);
00269 }
00270 }
00271
00275 static void gxf_read_index(AVFormatContext *s, int pkt_len) {
00276 AVIOContext *pb = s->pb;
00277 AVStream *st;
00278 uint32_t fields_per_map = avio_rl32(pb);
00279 uint32_t map_cnt = avio_rl32(pb);
00280 int i;
00281 pkt_len -= 8;
00282 if ((s->flags & AVFMT_FLAG_IGNIDX) || !s->streams) {
00283 avio_skip(pb, pkt_len);
00284 return;
00285 }
00286 st = s->streams[0];
00287 if (map_cnt > 1000) {
00288 av_log(s, AV_LOG_ERROR, "too many index entries %u (%x)\n", map_cnt, map_cnt);
00289 map_cnt = 1000;
00290 }
00291 if (pkt_len < 4 * map_cnt) {
00292 av_log(s, AV_LOG_ERROR, "invalid index length\n");
00293 avio_skip(pb, pkt_len);
00294 return;
00295 }
00296 pkt_len -= 4 * map_cnt;
00297 av_add_index_entry(st, 0, 0, 0, 0, 0);
00298 for (i = 0; i < map_cnt; i++)
00299 av_add_index_entry(st, (uint64_t)avio_rl32(pb) * 1024,
00300 i * (uint64_t)fields_per_map + 1, 0, 0, 0);
00301 avio_skip(pb, pkt_len);
00302 }
00303
00304 static int gxf_header(AVFormatContext *s) {
00305 AVIOContext *pb = s->pb;
00306 GXFPktType pkt_type;
00307 int map_len;
00308 int len;
00309 AVRational main_timebase = {0, 0};
00310 struct gxf_stream_info *si = s->priv_data;
00311 int i;
00312 if (!parse_packet_header(pb, &pkt_type, &map_len) || pkt_type != PKT_MAP) {
00313 av_log(s, AV_LOG_ERROR, "map packet not found\n");
00314 return 0;
00315 }
00316 map_len -= 2;
00317 if (avio_r8(pb) != 0x0e0 || avio_r8(pb) != 0xff) {
00318 av_log(s, AV_LOG_ERROR, "unknown version or invalid map preamble\n");
00319 return 0;
00320 }
00321 map_len -= 2;
00322 len = avio_rb16(pb);
00323 if (len > map_len) {
00324 av_log(s, AV_LOG_ERROR, "material data longer than map data\n");
00325 return 0;
00326 }
00327 map_len -= len;
00328 gxf_material_tags(pb, &len, si);
00329 avio_skip(pb, len);
00330 map_len -= 2;
00331 len = avio_rb16(pb);
00332 if (len > map_len) {
00333 av_log(s, AV_LOG_ERROR, "track description longer than map data\n");
00334 return 0;
00335 }
00336 map_len -= len;
00337 while (len > 0) {
00338 int track_type, track_id, track_len;
00339 AVStream *st;
00340 int idx;
00341 len -= 4;
00342 track_type = avio_r8(pb);
00343 track_id = avio_r8(pb);
00344 track_len = avio_rb16(pb);
00345 len -= track_len;
00346 if (!(track_type & 0x80)) {
00347 av_log(s, AV_LOG_ERROR, "invalid track type %x\n", track_type);
00348 continue;
00349 }
00350 track_type &= 0x7f;
00351 if ((track_id & 0xc0) != 0xc0) {
00352 av_log(s, AV_LOG_ERROR, "invalid track id %x\n", track_id);
00353 continue;
00354 }
00355 track_id &= 0x3f;
00356 gxf_track_tags(pb, &track_len, si);
00357
00358 if (track_type == 7 || track_type == 8 || track_type == 24) {
00359 add_timecode_metadata(&s->metadata, "timecode",
00360 si->track_aux_data & 0xffffffff,
00361 si->fields_per_frame);
00362
00363 }
00364 avio_skip(pb, track_len);
00365
00366 idx = get_sindex(s, track_id, track_type);
00367 if (idx < 0) continue;
00368 st = s->streams[idx];
00369 if (!main_timebase.num || !main_timebase.den) {
00370 main_timebase.num = si->frames_per_second.den;
00371 main_timebase.den = si->frames_per_second.num * 2;
00372 }
00373 st->start_time = si->first_field;
00374 if (si->first_field != AV_NOPTS_VALUE && si->last_field != AV_NOPTS_VALUE)
00375 st->duration = si->last_field - si->first_field;
00376 }
00377 if (len < 0)
00378 av_log(s, AV_LOG_ERROR, "invalid track description length specified\n");
00379 if (map_len)
00380 avio_skip(pb, map_len);
00381 if (!parse_packet_header(pb, &pkt_type, &len)) {
00382 av_log(s, AV_LOG_ERROR, "sync lost in header\n");
00383 return -1;
00384 }
00385 if (pkt_type == PKT_FLT) {
00386 gxf_read_index(s, len);
00387 if (!parse_packet_header(pb, &pkt_type, &len)) {
00388 av_log(s, AV_LOG_ERROR, "sync lost in header\n");
00389 return -1;
00390 }
00391 }
00392 if (pkt_type == PKT_UMF) {
00393 if (len >= 0x39) {
00394 AVRational fps;
00395 len -= 0x39;
00396 avio_skip(pb, 5);
00397 avio_skip(pb, 0x30);
00398 fps = fps_umf2avr(avio_rl32(pb));
00399 if (!main_timebase.num || !main_timebase.den) {
00400 av_log(s, AV_LOG_WARNING, "No FPS track tag, using UMF fps tag."
00401 " This might give wrong results.\n");
00402
00403 main_timebase.num = fps.den;
00404 main_timebase.den = fps.num * 2;
00405 }
00406
00407 if (len >= 0x18) {
00408 len -= 0x18;
00409 avio_skip(pb, 0x10);
00410 add_timecode_metadata(&s->metadata, "timecode_at_mark_in",
00411 avio_rl32(pb), si->fields_per_frame);
00412 add_timecode_metadata(&s->metadata, "timecode_at_mark_out",
00413 avio_rl32(pb), si->fields_per_frame);
00414 }
00415 } else
00416 av_log(s, AV_LOG_INFO, "UMF packet too short\n");
00417 } else
00418 av_log(s, AV_LOG_INFO, "UMF packet missing\n");
00419 avio_skip(pb, len);
00420
00421
00422 if (!main_timebase.num || !main_timebase.den)
00423 main_timebase = (AVRational){1001, 60000};
00424 for (i = 0; i < s->nb_streams; i++) {
00425 AVStream *st = s->streams[i];
00426 avpriv_set_pts_info(st, 32, main_timebase.num, main_timebase.den);
00427 }
00428 return 0;
00429 }
00430
00431 #define READ_ONE() \
00432 { \
00433 if (!max_interval-- || url_feof(pb)) \
00434 goto out; \
00435 tmp = tmp << 8 | avio_r8(pb); \
00436 }
00437
00445 static int64_t gxf_resync_media(AVFormatContext *s, uint64_t max_interval, int track, int timestamp) {
00446 uint32_t tmp;
00447 uint64_t last_pos;
00448 uint64_t last_found_pos = 0;
00449 int cur_track;
00450 int64_t cur_timestamp = AV_NOPTS_VALUE;
00451 int len;
00452 AVIOContext *pb = s->pb;
00453 GXFPktType type;
00454 tmp = avio_rb32(pb);
00455 start:
00456 while (tmp)
00457 READ_ONE();
00458 READ_ONE();
00459 if (tmp != 1)
00460 goto start;
00461 last_pos = avio_tell(pb);
00462 if (avio_seek(pb, -5, SEEK_CUR) < 0)
00463 goto out;
00464 if (!parse_packet_header(pb, &type, &len) || type != PKT_MEDIA) {
00465 if (avio_seek(pb, last_pos, SEEK_SET) < 0)
00466 goto out;
00467 goto start;
00468 }
00469 avio_r8(pb);
00470 cur_track = avio_r8(pb);
00471 cur_timestamp = avio_rb32(pb);
00472 last_found_pos = avio_tell(pb) - 16 - 6;
00473 if ((track >= 0 && track != cur_track) || (timestamp >= 0 && timestamp > cur_timestamp)) {
00474 if (avio_seek(pb, last_pos, SEEK_SET) >= 0)
00475 goto start;
00476 }
00477 out:
00478 if (last_found_pos)
00479 avio_seek(pb, last_found_pos, SEEK_SET);
00480 return cur_timestamp;
00481 }
00482
00483 static int gxf_packet(AVFormatContext *s, AVPacket *pkt) {
00484 AVIOContext *pb = s->pb;
00485 GXFPktType pkt_type;
00486 int pkt_len;
00487 struct gxf_stream_info *si = s->priv_data;
00488
00489 while (!pb->eof_reached) {
00490 AVStream *st;
00491 int track_type, track_id, ret;
00492 int field_nr, field_info, skip = 0;
00493 int stream_index;
00494 if (!parse_packet_header(pb, &pkt_type, &pkt_len)) {
00495 if (!url_feof(pb))
00496 av_log(s, AV_LOG_ERROR, "sync lost\n");
00497 return -1;
00498 }
00499 if (pkt_type == PKT_FLT) {
00500 gxf_read_index(s, pkt_len);
00501 continue;
00502 }
00503 if (pkt_type != PKT_MEDIA) {
00504 avio_skip(pb, pkt_len);
00505 continue;
00506 }
00507 if (pkt_len < 16) {
00508 av_log(s, AV_LOG_ERROR, "invalid media packet length\n");
00509 continue;
00510 }
00511 pkt_len -= 16;
00512 track_type = avio_r8(pb);
00513 track_id = avio_r8(pb);
00514 stream_index = get_sindex(s, track_id, track_type);
00515 if (stream_index < 0)
00516 return stream_index;
00517 st = s->streams[stream_index];
00518 field_nr = avio_rb32(pb);
00519 field_info = avio_rb32(pb);
00520 avio_rb32(pb);
00521 avio_r8(pb);
00522 avio_r8(pb);
00523 if (st->codec->codec_id == AV_CODEC_ID_PCM_S24LE ||
00524 st->codec->codec_id == AV_CODEC_ID_PCM_S16LE) {
00525 int first = field_info >> 16;
00526 int last = field_info & 0xffff;
00527 int bps = av_get_bits_per_sample(st->codec->codec_id)>>3;
00528 if (first <= last && last*bps <= pkt_len) {
00529 avio_skip(pb, first*bps);
00530 skip = pkt_len - last*bps;
00531 pkt_len = (last-first)*bps;
00532 } else
00533 av_log(s, AV_LOG_ERROR, "invalid first and last sample values\n");
00534 }
00535 ret = av_get_packet(pb, pkt, pkt_len);
00536 if (skip)
00537 avio_skip(pb, skip);
00538 pkt->stream_index = stream_index;
00539 pkt->dts = field_nr;
00540
00541
00542 if (st->codec->codec_id == AV_CODEC_ID_DVVIDEO)
00543 pkt->duration = si->fields_per_frame;
00544
00545 return ret;
00546 }
00547 return AVERROR_EOF;
00548 }
00549
00550 static int gxf_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
00551 int res = 0;
00552 uint64_t pos;
00553 uint64_t maxlen = 100 * 1024 * 1024;
00554 AVStream *st = s->streams[0];
00555 int64_t start_time = s->streams[stream_index]->start_time;
00556 int64_t found;
00557 int idx;
00558 if (timestamp < start_time) timestamp = start_time;
00559 idx = av_index_search_timestamp(st, timestamp - start_time,
00560 AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
00561 if (idx < 0)
00562 return -1;
00563 pos = st->index_entries[idx].pos;
00564 if (idx < st->nb_index_entries - 2)
00565 maxlen = st->index_entries[idx + 2].pos - pos;
00566 maxlen = FFMAX(maxlen, 200 * 1024);
00567 res = avio_seek(s->pb, pos, SEEK_SET);
00568 if (res < 0)
00569 return res;
00570 found = gxf_resync_media(s, maxlen, -1, timestamp);
00571 if (FFABS(found - timestamp) > 4)
00572 return -1;
00573 return 0;
00574 }
00575
00576 static int64_t gxf_read_timestamp(AVFormatContext *s, int stream_index,
00577 int64_t *pos, int64_t pos_limit) {
00578 AVIOContext *pb = s->pb;
00579 int64_t res;
00580 if (avio_seek(pb, *pos, SEEK_SET) < 0)
00581 return AV_NOPTS_VALUE;
00582 res = gxf_resync_media(s, pos_limit - *pos, -1, -1);
00583 *pos = avio_tell(pb);
00584 return res;
00585 }
00586
00587 AVInputFormat ff_gxf_demuxer = {
00588 .name = "gxf",
00589 .long_name = NULL_IF_CONFIG_SMALL("GXF (General eXchange Format)"),
00590 .priv_data_size = sizeof(struct gxf_stream_info),
00591 .read_probe = gxf_probe,
00592 .read_header = gxf_header,
00593 .read_packet = gxf_packet,
00594 .read_seek = gxf_seek,
00595 .read_timestamp = gxf_read_timestamp,
00596 };