00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/common.h"
00023 #include "avformat.h"
00024 #include "internal.h"
00025 #include "gxf.h"
00026
00027 struct gxf_stream_info {
00028 int64_t first_field;
00029 int64_t last_field;
00030 AVRational frames_per_second;
00031 int32_t fields_per_frame;
00032 };
00033
00041 static int parse_packet_header(AVIOContext *pb, GXFPktType *type, int *length) {
00042 if (avio_rb32(pb))
00043 return 0;
00044 if (avio_r8(pb) != 1)
00045 return 0;
00046 *type = avio_r8(pb);
00047 *length = avio_rb32(pb);
00048 if ((*length >> 24) || *length < 16)
00049 return 0;
00050 *length -= 16;
00051 if (avio_rb32(pb))
00052 return 0;
00053 if (avio_r8(pb) != 0xe1)
00054 return 0;
00055 if (avio_r8(pb) != 0xe2)
00056 return 0;
00057 return 1;
00058 }
00059
00063 static int gxf_probe(AVProbeData *p) {
00064 static const uint8_t startcode[] = {0, 0, 0, 0, 1, 0xbc};
00065 static const uint8_t endcode[] = {0, 0, 0, 0, 0xe1, 0xe2};
00066 if (!memcmp(p->buf, startcode, sizeof(startcode)) &&
00067 !memcmp(&p->buf[16 - sizeof(endcode)], endcode, sizeof(endcode)))
00068 return AVPROBE_SCORE_MAX;
00069 return 0;
00070 }
00071
00078 static int get_sindex(AVFormatContext *s, int id, int format) {
00079 int i;
00080 AVStream *st = NULL;
00081 i = ff_find_stream_index(s, id);
00082 if (i >= 0)
00083 return i;
00084 st = avformat_new_stream(s, NULL);
00085 if (!st)
00086 return AVERROR(ENOMEM);
00087 st->id = id;
00088 switch (format) {
00089 case 3:
00090 case 4:
00091 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00092 st->codec->codec_id = CODEC_ID_MJPEG;
00093 break;
00094 case 13:
00095 case 15:
00096 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00097 st->codec->codec_id = CODEC_ID_DVVIDEO;
00098 break;
00099 case 14:
00100 case 16:
00101 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00102 st->codec->codec_id = CODEC_ID_DVVIDEO;
00103 break;
00104 case 11:
00105 case 12:
00106 case 20:
00107 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00108 st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
00109 st->need_parsing = AVSTREAM_PARSE_HEADERS;
00110 break;
00111 case 22:
00112 case 23:
00113 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00114 st->codec->codec_id = CODEC_ID_MPEG1VIDEO;
00115 st->need_parsing = AVSTREAM_PARSE_HEADERS;
00116 break;
00117 case 9:
00118 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00119 st->codec->codec_id = CODEC_ID_PCM_S24LE;
00120 st->codec->channels = 1;
00121 st->codec->sample_rate = 48000;
00122 st->codec->bit_rate = 3 * 1 * 48000 * 8;
00123 st->codec->block_align = 3 * 1;
00124 st->codec->bits_per_coded_sample = 24;
00125 break;
00126 case 10:
00127 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00128 st->codec->codec_id = CODEC_ID_PCM_S16LE;
00129 st->codec->channels = 1;
00130 st->codec->sample_rate = 48000;
00131 st->codec->bit_rate = 2 * 1 * 48000 * 8;
00132 st->codec->block_align = 2 * 1;
00133 st->codec->bits_per_coded_sample = 16;
00134 break;
00135 case 17:
00136 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00137 st->codec->codec_id = CODEC_ID_AC3;
00138 st->codec->channels = 2;
00139 st->codec->sample_rate = 48000;
00140 break;
00141
00142 case 7:
00143 case 8:
00144 case 24:
00145 st->codec->codec_type = AVMEDIA_TYPE_DATA;
00146 st->codec->codec_id = CODEC_ID_NONE;
00147 break;
00148 default:
00149 st->codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
00150 st->codec->codec_id = CODEC_ID_NONE;
00151 break;
00152 }
00153 return s->nb_streams - 1;
00154 }
00155
00161 static void gxf_material_tags(AVIOContext *pb, int *len, struct gxf_stream_info *si) {
00162 si->first_field = AV_NOPTS_VALUE;
00163 si->last_field = AV_NOPTS_VALUE;
00164 while (*len >= 2) {
00165 GXFMatTag tag = avio_r8(pb);
00166 int tlen = avio_r8(pb);
00167 *len -= 2;
00168 if (tlen > *len)
00169 return;
00170 *len -= tlen;
00171 if (tlen == 4) {
00172 uint32_t value = avio_rb32(pb);
00173 if (tag == MAT_FIRST_FIELD)
00174 si->first_field = value;
00175 else if (tag == MAT_LAST_FIELD)
00176 si->last_field = value;
00177 } else
00178 avio_skip(pb, tlen);
00179 }
00180 }
00181
00187 static AVRational fps_tag2avr(int32_t fps) {
00188 extern const AVRational avpriv_frame_rate_tab[];
00189 if (fps < 1 || fps > 9) fps = 9;
00190 return avpriv_frame_rate_tab[9 - fps];
00191 }
00192
00198 static AVRational fps_umf2avr(uint32_t flags) {
00199 static const AVRational map[] = {{50, 1}, {60000, 1001}, {24, 1},
00200 {25, 1}, {30000, 1001}};
00201 int idx = av_log2((flags & 0x7c0) >> 6);
00202 return map[idx];
00203 }
00204
00210 static void gxf_track_tags(AVIOContext *pb, int *len, struct gxf_stream_info *si) {
00211 si->frames_per_second = (AVRational){0, 0};
00212 si->fields_per_frame = 0;
00213 while (*len >= 2) {
00214 GXFTrackTag tag = avio_r8(pb);
00215 int tlen = avio_r8(pb);
00216 *len -= 2;
00217 if (tlen > *len)
00218 return;
00219 *len -= tlen;
00220 if (tlen == 4) {
00221 uint32_t value = avio_rb32(pb);
00222 if (tag == TRACK_FPS)
00223 si->frames_per_second = fps_tag2avr(value);
00224 else if (tag == TRACK_FPF && (value == 1 || value == 2))
00225 si->fields_per_frame = value;
00226 } else
00227 avio_skip(pb, tlen);
00228 }
00229 }
00230
00234 static void gxf_read_index(AVFormatContext *s, int pkt_len) {
00235 AVIOContext *pb = s->pb;
00236 AVStream *st = s->streams[0];
00237 uint32_t fields_per_map = avio_rl32(pb);
00238 uint32_t map_cnt = avio_rl32(pb);
00239 int i;
00240 pkt_len -= 8;
00241 if (s->flags & AVFMT_FLAG_IGNIDX) {
00242 avio_skip(pb, pkt_len);
00243 return;
00244 }
00245 if (map_cnt > 1000) {
00246 av_log(s, AV_LOG_ERROR, "too many index entries %u (%x)\n", map_cnt, map_cnt);
00247 map_cnt = 1000;
00248 }
00249 if (pkt_len < 4 * map_cnt) {
00250 av_log(s, AV_LOG_ERROR, "invalid index length\n");
00251 avio_skip(pb, pkt_len);
00252 return;
00253 }
00254 pkt_len -= 4 * map_cnt;
00255 av_add_index_entry(st, 0, 0, 0, 0, 0);
00256 for (i = 0; i < map_cnt; i++)
00257 av_add_index_entry(st, (uint64_t)avio_rl32(pb) * 1024,
00258 i * (uint64_t)fields_per_map + 1, 0, 0, 0);
00259 avio_skip(pb, pkt_len);
00260 }
00261
00262 static int gxf_header(AVFormatContext *s, AVFormatParameters *ap) {
00263 AVIOContext *pb = s->pb;
00264 GXFPktType pkt_type;
00265 int map_len;
00266 int len;
00267 AVRational main_timebase = {0, 0};
00268 struct gxf_stream_info *si = s->priv_data;
00269 int i;
00270 if (!parse_packet_header(pb, &pkt_type, &map_len) || pkt_type != PKT_MAP) {
00271 av_log(s, AV_LOG_ERROR, "map packet not found\n");
00272 return 0;
00273 }
00274 map_len -= 2;
00275 if (avio_r8(pb) != 0x0e0 || avio_r8(pb) != 0xff) {
00276 av_log(s, AV_LOG_ERROR, "unknown version or invalid map preamble\n");
00277 return 0;
00278 }
00279 map_len -= 2;
00280 len = avio_rb16(pb);
00281 if (len > map_len) {
00282 av_log(s, AV_LOG_ERROR, "material data longer than map data\n");
00283 return 0;
00284 }
00285 map_len -= len;
00286 gxf_material_tags(pb, &len, si);
00287 avio_skip(pb, len);
00288 map_len -= 2;
00289 len = avio_rb16(pb);
00290 if (len > map_len) {
00291 av_log(s, AV_LOG_ERROR, "track description longer than map data\n");
00292 return 0;
00293 }
00294 map_len -= len;
00295 while (len > 0) {
00296 int track_type, track_id, track_len;
00297 AVStream *st;
00298 int idx;
00299 len -= 4;
00300 track_type = avio_r8(pb);
00301 track_id = avio_r8(pb);
00302 track_len = avio_rb16(pb);
00303 len -= track_len;
00304 gxf_track_tags(pb, &track_len, si);
00305 avio_skip(pb, track_len);
00306 if (!(track_type & 0x80)) {
00307 av_log(s, AV_LOG_ERROR, "invalid track type %x\n", track_type);
00308 continue;
00309 }
00310 track_type &= 0x7f;
00311 if ((track_id & 0xc0) != 0xc0) {
00312 av_log(s, AV_LOG_ERROR, "invalid track id %x\n", track_id);
00313 continue;
00314 }
00315 track_id &= 0x3f;
00316 idx = get_sindex(s, track_id, track_type);
00317 if (idx < 0) continue;
00318 st = s->streams[idx];
00319 if (!main_timebase.num || !main_timebase.den) {
00320 main_timebase.num = si->frames_per_second.den;
00321 main_timebase.den = si->frames_per_second.num * 2;
00322 }
00323 st->start_time = si->first_field;
00324 if (si->first_field != AV_NOPTS_VALUE && si->last_field != AV_NOPTS_VALUE)
00325 st->duration = si->last_field - si->first_field;
00326 }
00327 if (len < 0)
00328 av_log(s, AV_LOG_ERROR, "invalid track description length specified\n");
00329 if (map_len)
00330 avio_skip(pb, map_len);
00331 if (!parse_packet_header(pb, &pkt_type, &len)) {
00332 av_log(s, AV_LOG_ERROR, "sync lost in header\n");
00333 return -1;
00334 }
00335 if (pkt_type == PKT_FLT) {
00336 gxf_read_index(s, len);
00337 if (!parse_packet_header(pb, &pkt_type, &len)) {
00338 av_log(s, AV_LOG_ERROR, "sync lost in header\n");
00339 return -1;
00340 }
00341 }
00342 if (pkt_type == PKT_UMF) {
00343 if (len >= 0x39) {
00344 AVRational fps;
00345 len -= 0x39;
00346 avio_skip(pb, 5);
00347 avio_skip(pb, 0x30);
00348 fps = fps_umf2avr(avio_rl32(pb));
00349 if (!main_timebase.num || !main_timebase.den) {
00350
00351 main_timebase.num = fps.den;
00352 main_timebase.den = fps.num * 2;
00353 }
00354 } else
00355 av_log(s, AV_LOG_INFO, "UMF packet too short\n");
00356 } else
00357 av_log(s, AV_LOG_INFO, "UMF packet missing\n");
00358 avio_skip(pb, len);
00359
00360
00361 if (!main_timebase.num || !main_timebase.den)
00362 main_timebase = (AVRational){1001, 60000};
00363 for (i = 0; i < s->nb_streams; i++) {
00364 AVStream *st = s->streams[i];
00365 avpriv_set_pts_info(st, 32, main_timebase.num, main_timebase.den);
00366 }
00367 return 0;
00368 }
00369
00370 #define READ_ONE() \
00371 { \
00372 if (!max_interval-- || pb->eof_reached) \
00373 goto out; \
00374 tmp = tmp << 8 | avio_r8(pb); \
00375 }
00376
00384 static int64_t gxf_resync_media(AVFormatContext *s, uint64_t max_interval, int track, int timestamp) {
00385 uint32_t tmp;
00386 uint64_t last_pos;
00387 uint64_t last_found_pos = 0;
00388 int cur_track;
00389 int64_t cur_timestamp = AV_NOPTS_VALUE;
00390 int len;
00391 AVIOContext *pb = s->pb;
00392 GXFPktType type;
00393 tmp = avio_rb32(pb);
00394 start:
00395 while (tmp)
00396 READ_ONE();
00397 READ_ONE();
00398 if (tmp != 1)
00399 goto start;
00400 last_pos = avio_tell(pb);
00401 if (avio_seek(pb, -5, SEEK_CUR) < 0)
00402 goto out;
00403 if (!parse_packet_header(pb, &type, &len) || type != PKT_MEDIA) {
00404 if (avio_seek(pb, last_pos, SEEK_SET) < 0)
00405 goto out;
00406 goto start;
00407 }
00408 avio_r8(pb);
00409 cur_track = avio_r8(pb);
00410 cur_timestamp = avio_rb32(pb);
00411 last_found_pos = avio_tell(pb) - 16 - 6;
00412 if ((track >= 0 && track != cur_track) || (timestamp >= 0 && timestamp > cur_timestamp)) {
00413 if (avio_seek(pb, last_pos, SEEK_SET) >= 0)
00414 goto start;
00415 }
00416 out:
00417 if (last_found_pos)
00418 avio_seek(pb, last_found_pos, SEEK_SET);
00419 return cur_timestamp;
00420 }
00421
00422 static int gxf_packet(AVFormatContext *s, AVPacket *pkt) {
00423 AVIOContext *pb = s->pb;
00424 GXFPktType pkt_type;
00425 int pkt_len;
00426 struct gxf_stream_info *si = s->priv_data;
00427
00428 while (!pb->eof_reached) {
00429 AVStream *st;
00430 int track_type, track_id, ret;
00431 int field_nr, field_info, skip = 0;
00432 int stream_index;
00433 if (!parse_packet_header(pb, &pkt_type, &pkt_len)) {
00434 if (!pb->eof_reached)
00435 av_log(s, AV_LOG_ERROR, "sync lost\n");
00436 return -1;
00437 }
00438 if (pkt_type == PKT_FLT) {
00439 gxf_read_index(s, pkt_len);
00440 continue;
00441 }
00442 if (pkt_type != PKT_MEDIA) {
00443 avio_skip(pb, pkt_len);
00444 continue;
00445 }
00446 if (pkt_len < 16) {
00447 av_log(s, AV_LOG_ERROR, "invalid media packet length\n");
00448 continue;
00449 }
00450 pkt_len -= 16;
00451 track_type = avio_r8(pb);
00452 track_id = avio_r8(pb);
00453 stream_index = get_sindex(s, track_id, track_type);
00454 if (stream_index < 0)
00455 return stream_index;
00456 st = s->streams[stream_index];
00457 field_nr = avio_rb32(pb);
00458 field_info = avio_rb32(pb);
00459 avio_rb32(pb);
00460 avio_r8(pb);
00461 avio_r8(pb);
00462 if (st->codec->codec_id == CODEC_ID_PCM_S24LE ||
00463 st->codec->codec_id == CODEC_ID_PCM_S16LE) {
00464 int first = field_info >> 16;
00465 int last = field_info & 0xffff;
00466 int bps = av_get_bits_per_sample(st->codec->codec_id)>>3;
00467 if (first <= last && last*bps <= pkt_len) {
00468 avio_skip(pb, first*bps);
00469 skip = pkt_len - last*bps;
00470 pkt_len = (last-first)*bps;
00471 } else
00472 av_log(s, AV_LOG_ERROR, "invalid first and last sample values\n");
00473 }
00474 ret = av_get_packet(pb, pkt, pkt_len);
00475 if (skip)
00476 avio_skip(pb, skip);
00477 pkt->stream_index = stream_index;
00478 pkt->dts = field_nr;
00479
00480
00481 if (st->codec->codec_id == CODEC_ID_DVVIDEO)
00482 pkt->duration = si->fields_per_frame;
00483
00484 return ret;
00485 }
00486 return AVERROR(EIO);
00487 }
00488
00489 static int gxf_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
00490 int res = 0;
00491 uint64_t pos;
00492 uint64_t maxlen = 100 * 1024 * 1024;
00493 AVStream *st = s->streams[0];
00494 int64_t start_time = s->streams[stream_index]->start_time;
00495 int64_t found;
00496 int idx;
00497 if (timestamp < start_time) timestamp = start_time;
00498 idx = av_index_search_timestamp(st, timestamp - start_time,
00499 AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
00500 if (idx < 0)
00501 return -1;
00502 pos = st->index_entries[idx].pos;
00503 if (idx < st->nb_index_entries - 2)
00504 maxlen = st->index_entries[idx + 2].pos - pos;
00505 maxlen = FFMAX(maxlen, 200 * 1024);
00506 res = avio_seek(s->pb, pos, SEEK_SET);
00507 if (res < 0)
00508 return res;
00509 found = gxf_resync_media(s, maxlen, -1, timestamp);
00510 if (FFABS(found - timestamp) > 4)
00511 return -1;
00512 return 0;
00513 }
00514
00515 static int64_t gxf_read_timestamp(AVFormatContext *s, int stream_index,
00516 int64_t *pos, int64_t pos_limit) {
00517 AVIOContext *pb = s->pb;
00518 int64_t res;
00519 if (avio_seek(pb, *pos, SEEK_SET) < 0)
00520 return AV_NOPTS_VALUE;
00521 res = gxf_resync_media(s, pos_limit - *pos, -1, -1);
00522 *pos = avio_tell(pb);
00523 return res;
00524 }
00525
00526 AVInputFormat ff_gxf_demuxer = {
00527 .name = "gxf",
00528 .long_name = NULL_IF_CONFIG_SMALL("GXF format"),
00529 .priv_data_size = sizeof(struct gxf_stream_info),
00530 .read_probe = gxf_probe,
00531 .read_header = gxf_header,
00532 .read_packet = gxf_packet,
00533 .read_seek = gxf_seek,
00534 .read_timestamp = gxf_read_timestamp,
00535 };