• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/gxf.c

Go to the documentation of this file.
00001 /*
00002  * GXF demuxer.
00003  * Copyright (c) 2006 Reimar Doeffinger
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "libavutil/common.h"
00023 #include "avformat.h"
00024 #include "gxf.h"
00025 
00026 struct gxf_stream_info {
00027     int64_t first_field;
00028     int64_t last_field;
00029     AVRational frames_per_second;
00030     int32_t fields_per_frame;
00031 };
00032 
00040 static int parse_packet_header(ByteIOContext *pb, GXFPktType *type, int *length) {
00041     if (get_be32(pb))
00042         return 0;
00043     if (get_byte(pb) != 1)
00044         return 0;
00045     *type = get_byte(pb);
00046     *length = get_be32(pb);
00047     if ((*length >> 24) || *length < 16)
00048         return 0;
00049     *length -= 16;
00050     if (get_be32(pb))
00051         return 0;
00052     if (get_byte(pb) != 0xe1)
00053         return 0;
00054     if (get_byte(pb) != 0xe2)
00055         return 0;
00056     return 1;
00057 }
00058 
00062 static int gxf_probe(AVProbeData *p) {
00063     static const uint8_t startcode[] = {0, 0, 0, 0, 1, 0xbc}; // start with map packet
00064     static const uint8_t endcode[] = {0, 0, 0, 0, 0xe1, 0xe2};
00065     if (!memcmp(p->buf, startcode, sizeof(startcode)) &&
00066         !memcmp(&p->buf[16 - sizeof(endcode)], endcode, sizeof(endcode)))
00067         return AVPROBE_SCORE_MAX;
00068     return 0;
00069 }
00070 
00077 static int get_sindex(AVFormatContext *s, int id, int format) {
00078     int i;
00079     AVStream *st = NULL;
00080     for (i = 0; i < s->nb_streams; i++) {
00081         if (s->streams[i]->id == id)
00082             return i;
00083     }
00084     st = av_new_stream(s, id);
00085     if (!st)
00086         return AVERROR(ENOMEM);
00087     switch (format) {
00088         case 3:
00089         case 4:
00090             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00091             st->codec->codec_id = CODEC_ID_MJPEG;
00092             break;
00093         case 13:
00094         case 15:
00095             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00096             st->codec->codec_id = CODEC_ID_DVVIDEO;
00097             break;
00098         case 14:
00099         case 16:
00100             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00101             st->codec->codec_id = CODEC_ID_DVVIDEO;
00102             break;
00103         case 11:
00104         case 12:
00105         case 20:
00106             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00107             st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
00108             st->need_parsing = AVSTREAM_PARSE_HEADERS; //get keyframe flag etc.
00109             break;
00110         case 22:
00111         case 23:
00112             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00113             st->codec->codec_id = CODEC_ID_MPEG1VIDEO;
00114             st->need_parsing = AVSTREAM_PARSE_HEADERS; //get keyframe flag etc.
00115             break;
00116         case 9:
00117             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00118             st->codec->codec_id = CODEC_ID_PCM_S24LE;
00119             st->codec->channels = 1;
00120             st->codec->sample_rate = 48000;
00121             st->codec->bit_rate = 3 * 1 * 48000 * 8;
00122             st->codec->block_align = 3 * 1;
00123             st->codec->bits_per_coded_sample = 24;
00124             break;
00125         case 10:
00126             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00127             st->codec->codec_id = CODEC_ID_PCM_S16LE;
00128             st->codec->channels = 1;
00129             st->codec->sample_rate = 48000;
00130             st->codec->bit_rate = 2 * 1 * 48000 * 8;
00131             st->codec->block_align = 2 * 1;
00132             st->codec->bits_per_coded_sample = 16;
00133             break;
00134         case 17:
00135             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00136             st->codec->codec_id = CODEC_ID_AC3;
00137             st->codec->channels = 2;
00138             st->codec->sample_rate = 48000;
00139             break;
00140         // timecode tracks:
00141         case 7:
00142         case 8:
00143         case 24:
00144             st->codec->codec_type = AVMEDIA_TYPE_DATA;
00145             st->codec->codec_id = CODEC_ID_NONE;
00146             break;
00147         default:
00148             st->codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
00149             st->codec->codec_id = CODEC_ID_NONE;
00150             break;
00151     }
00152     return s->nb_streams - 1;
00153 }
00154 
00160 static void gxf_material_tags(ByteIOContext *pb, int *len, struct gxf_stream_info *si) {
00161     si->first_field = AV_NOPTS_VALUE;
00162     si->last_field = AV_NOPTS_VALUE;
00163     while (*len >= 2) {
00164         GXFMatTag tag = get_byte(pb);
00165         int tlen = get_byte(pb);
00166         *len -= 2;
00167         if (tlen > *len)
00168             return;
00169         *len -= tlen;
00170         if (tlen == 4) {
00171             uint32_t value = get_be32(pb);
00172             if (tag == MAT_FIRST_FIELD)
00173                 si->first_field = value;
00174             else if (tag == MAT_LAST_FIELD)
00175                 si->last_field = value;
00176         } else
00177             url_fskip(pb, tlen);
00178     }
00179 }
00180 
00186 static AVRational fps_tag2avr(int32_t fps) {
00187     extern const AVRational ff_frame_rate_tab[];
00188     if (fps < 1 || fps > 9) fps = 9;
00189     return ff_frame_rate_tab[9 - fps]; // values have opposite order
00190 }
00191 
00197 static AVRational fps_umf2avr(uint32_t flags) {
00198     static const AVRational map[] = {{50, 1}, {60000, 1001}, {24, 1},
00199         {25, 1}, {30000, 1001}};
00200     int idx =  av_log2((flags & 0x7c0) >> 6);
00201     return map[idx];
00202 }
00203 
00209 static void gxf_track_tags(ByteIOContext *pb, int *len, struct gxf_stream_info *si) {
00210     si->frames_per_second = (AVRational){0, 0};
00211     si->fields_per_frame = 0;
00212     while (*len >= 2) {
00213         GXFTrackTag tag = get_byte(pb);
00214         int tlen = get_byte(pb);
00215         *len -= 2;
00216         if (tlen > *len)
00217             return;
00218         *len -= tlen;
00219         if (tlen == 4) {
00220             uint32_t value = get_be32(pb);
00221             if (tag == TRACK_FPS)
00222                 si->frames_per_second = fps_tag2avr(value);
00223             else if (tag == TRACK_FPF && (value == 1 || value == 2))
00224                 si->fields_per_frame = value;
00225         } else
00226             url_fskip(pb, tlen);
00227     }
00228 }
00229 
00233 static void gxf_read_index(AVFormatContext *s, int pkt_len) {
00234     ByteIOContext *pb = s->pb;
00235     AVStream *st = s->streams[0];
00236     uint32_t fields_per_map = get_le32(pb);
00237     uint32_t map_cnt = get_le32(pb);
00238     int i;
00239     pkt_len -= 8;
00240     if (map_cnt > 1000) {
00241         av_log(s, AV_LOG_ERROR, "too many index entries %u (%x)\n", map_cnt, map_cnt);
00242         map_cnt = 1000;
00243     }
00244     if (pkt_len < 4 * map_cnt) {
00245         av_log(s, AV_LOG_ERROR, "invalid index length\n");
00246         url_fskip(pb, pkt_len);
00247         return;
00248     }
00249     pkt_len -= 4 * map_cnt;
00250     av_add_index_entry(st, 0, 0, 0, 0, 0);
00251     for (i = 0; i < map_cnt; i++)
00252         av_add_index_entry(st, (uint64_t)get_le32(pb) * 1024,
00253                            i * (uint64_t)fields_per_map + 1, 0, 0, 0);
00254     url_fskip(pb, pkt_len);
00255 }
00256 
00257 static int gxf_header(AVFormatContext *s, AVFormatParameters *ap) {
00258     ByteIOContext *pb = s->pb;
00259     GXFPktType pkt_type;
00260     int map_len;
00261     int len;
00262     AVRational main_timebase = {0, 0};
00263     struct gxf_stream_info si;
00264     int i;
00265     if (!parse_packet_header(pb, &pkt_type, &map_len) || pkt_type != PKT_MAP) {
00266         av_log(s, AV_LOG_ERROR, "map packet not found\n");
00267         return 0;
00268     }
00269     map_len -= 2;
00270     if (get_byte(pb) != 0x0e0 || get_byte(pb) != 0xff) {
00271         av_log(s, AV_LOG_ERROR, "unknown version or invalid map preamble\n");
00272         return 0;
00273     }
00274     map_len -= 2;
00275     len = get_be16(pb); // length of material data section
00276     if (len > map_len) {
00277         av_log(s, AV_LOG_ERROR, "material data longer than map data\n");
00278         return 0;
00279     }
00280     map_len -= len;
00281     gxf_material_tags(pb, &len, &si);
00282     url_fskip(pb, len);
00283     map_len -= 2;
00284     len = get_be16(pb); // length of track description
00285     if (len > map_len) {
00286         av_log(s, AV_LOG_ERROR, "track description longer than map data\n");
00287         return 0;
00288     }
00289     map_len -= len;
00290     while (len > 0) {
00291         int track_type, track_id, track_len;
00292         AVStream *st;
00293         int idx;
00294         len -= 4;
00295         track_type = get_byte(pb);
00296         track_id = get_byte(pb);
00297         track_len = get_be16(pb);
00298         len -= track_len;
00299         gxf_track_tags(pb, &track_len, &si);
00300         url_fskip(pb, track_len);
00301         if (!(track_type & 0x80)) {
00302            av_log(s, AV_LOG_ERROR, "invalid track type %x\n", track_type);
00303            continue;
00304         }
00305         track_type &= 0x7f;
00306         if ((track_id & 0xc0) != 0xc0) {
00307            av_log(s, AV_LOG_ERROR, "invalid track id %x\n", track_id);
00308            continue;
00309         }
00310         track_id &= 0x3f;
00311         idx = get_sindex(s, track_id, track_type);
00312         if (idx < 0) continue;
00313         st = s->streams[idx];
00314         if (!main_timebase.num || !main_timebase.den) {
00315             main_timebase.num = si.frames_per_second.den;
00316             main_timebase.den = si.frames_per_second.num * 2;
00317         }
00318         st->start_time = si.first_field;
00319         if (si.first_field != AV_NOPTS_VALUE && si.last_field != AV_NOPTS_VALUE)
00320             st->duration = si.last_field - si.first_field;
00321     }
00322     if (len < 0)
00323         av_log(s, AV_LOG_ERROR, "invalid track description length specified\n");
00324     if (map_len)
00325         url_fskip(pb, map_len);
00326     if (!parse_packet_header(pb, &pkt_type, &len)) {
00327         av_log(s, AV_LOG_ERROR, "sync lost in header\n");
00328         return -1;
00329     }
00330     if (pkt_type == PKT_FLT) {
00331         gxf_read_index(s, len);
00332         if (!parse_packet_header(pb, &pkt_type, &len)) {
00333             av_log(s, AV_LOG_ERROR, "sync lost in header\n");
00334             return -1;
00335         }
00336     }
00337     if (pkt_type == PKT_UMF) {
00338         if (len >= 0x39) {
00339             AVRational fps;
00340             len -= 0x39;
00341             url_fskip(pb, 5); // preamble
00342             url_fskip(pb, 0x30); // payload description
00343             fps = fps_umf2avr(get_le32(pb));
00344             if (!main_timebase.num || !main_timebase.den) {
00345                 // this may not always be correct, but simply the best we can get
00346                 main_timebase.num = fps.den;
00347                 main_timebase.den = fps.num * 2;
00348             }
00349         } else
00350             av_log(s, AV_LOG_INFO, "UMF packet too short\n");
00351     } else
00352         av_log(s, AV_LOG_INFO, "UMF packet missing\n");
00353     url_fskip(pb, len);
00354     // set a fallback value, 60000/1001 is specified for audio-only files
00355     // so use that regardless of why we do not know the video frame rate.
00356     if (!main_timebase.num || !main_timebase.den)
00357         main_timebase = (AVRational){1001, 60000};
00358     for (i = 0; i < s->nb_streams; i++) {
00359         AVStream *st = s->streams[i];
00360         av_set_pts_info(st, 32, main_timebase.num, main_timebase.den);
00361     }
00362     return 0;
00363 }
00364 
00365 #define READ_ONE() \
00366     { \
00367         if (!max_interval-- || url_feof(pb)) \
00368             goto out; \
00369         tmp = tmp << 8 | get_byte(pb); \
00370     }
00371 
00379 static int64_t gxf_resync_media(AVFormatContext *s, uint64_t max_interval, int track, int timestamp) {
00380     uint32_t tmp;
00381     uint64_t last_pos;
00382     uint64_t last_found_pos = 0;
00383     int cur_track;
00384     int64_t cur_timestamp = AV_NOPTS_VALUE;
00385     int len;
00386     ByteIOContext *pb = s->pb;
00387     GXFPktType type;
00388     tmp = get_be32(pb);
00389 start:
00390     while (tmp)
00391         READ_ONE();
00392     READ_ONE();
00393     if (tmp != 1)
00394         goto start;
00395     last_pos = url_ftell(pb);
00396     url_fseek(pb, -5, SEEK_CUR);
00397     if (!parse_packet_header(pb, &type, &len) || type != PKT_MEDIA) {
00398         url_fseek(pb, last_pos, SEEK_SET);
00399         goto start;
00400     }
00401     get_byte(pb);
00402     cur_track = get_byte(pb);
00403     cur_timestamp = get_be32(pb);
00404     last_found_pos = url_ftell(pb) - 16 - 6;
00405     if ((track >= 0 && track != cur_track) || (timestamp >= 0 && timestamp > cur_timestamp)) {
00406         url_fseek(pb, last_pos, SEEK_SET);
00407         goto start;
00408     }
00409 out:
00410     if (last_found_pos)
00411         url_fseek(pb, last_found_pos, SEEK_SET);
00412     return cur_timestamp;
00413 }
00414 
00415 static int gxf_packet(AVFormatContext *s, AVPacket *pkt) {
00416     ByteIOContext *pb = s->pb;
00417     GXFPktType pkt_type;
00418     int pkt_len;
00419     while (!url_feof(pb)) {
00420         AVStream *st;
00421         int track_type, track_id, ret;
00422         int field_nr, field_info, skip = 0;
00423         int stream_index;
00424         if (!parse_packet_header(pb, &pkt_type, &pkt_len)) {
00425             if (!url_feof(pb))
00426                 av_log(s, AV_LOG_ERROR, "sync lost\n");
00427             return -1;
00428         }
00429         if (pkt_type == PKT_FLT) {
00430             gxf_read_index(s, pkt_len);
00431             continue;
00432         }
00433         if (pkt_type != PKT_MEDIA) {
00434             url_fskip(pb, pkt_len);
00435             continue;
00436         }
00437         if (pkt_len < 16) {
00438             av_log(s, AV_LOG_ERROR, "invalid media packet length\n");
00439             continue;
00440         }
00441         pkt_len -= 16;
00442         track_type = get_byte(pb);
00443         track_id = get_byte(pb);
00444         stream_index = get_sindex(s, track_id, track_type);
00445         if (stream_index < 0)
00446             return stream_index;
00447         st = s->streams[stream_index];
00448         field_nr = get_be32(pb);
00449         field_info = get_be32(pb);
00450         get_be32(pb); // "timeline" field number
00451         get_byte(pb); // flags
00452         get_byte(pb); // reserved
00453         if (st->codec->codec_id == CODEC_ID_PCM_S24LE ||
00454             st->codec->codec_id == CODEC_ID_PCM_S16LE) {
00455             int first = field_info >> 16;
00456             int last  = field_info & 0xffff; // last is exclusive
00457             int bps = av_get_bits_per_sample(st->codec->codec_id)>>3;
00458             if (first <= last && last*bps <= pkt_len) {
00459                 url_fskip(pb, first*bps);
00460                 skip = pkt_len - last*bps;
00461                 pkt_len = (last-first)*bps;
00462             } else
00463                 av_log(s, AV_LOG_ERROR, "invalid first and last sample values\n");
00464         }
00465         ret = av_get_packet(pb, pkt, pkt_len);
00466         if (skip)
00467             url_fskip(pb, skip);
00468         pkt->stream_index = stream_index;
00469         pkt->dts = field_nr;
00470         return ret;
00471     }
00472     return AVERROR(EIO);
00473 }
00474 
00475 static int gxf_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
00476     uint64_t pos;
00477     uint64_t maxlen = 100 * 1024 * 1024;
00478     AVStream *st = s->streams[0];
00479     int64_t start_time = s->streams[stream_index]->start_time;
00480     int64_t found;
00481     int idx;
00482     if (timestamp < start_time) timestamp = start_time;
00483     idx = av_index_search_timestamp(st, timestamp - start_time,
00484                                     AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
00485     if (idx < 0)
00486         return -1;
00487     pos = st->index_entries[idx].pos;
00488     if (idx < st->nb_index_entries - 2)
00489         maxlen = st->index_entries[idx + 2].pos - pos;
00490     maxlen = FFMAX(maxlen, 200 * 1024);
00491     url_fseek(s->pb, pos, SEEK_SET);
00492     found = gxf_resync_media(s, maxlen, -1, timestamp);
00493     if (FFABS(found - timestamp) > 4)
00494         return -1;
00495     return 0;
00496 }
00497 
00498 static int64_t gxf_read_timestamp(AVFormatContext *s, int stream_index,
00499                                   int64_t *pos, int64_t pos_limit) {
00500     ByteIOContext *pb = s->pb;
00501     int64_t res;
00502     url_fseek(pb, *pos, SEEK_SET);
00503     res = gxf_resync_media(s, pos_limit - *pos, -1, -1);
00504     *pos = url_ftell(pb);
00505     return res;
00506 }
00507 
00508 AVInputFormat gxf_demuxer = {
00509     "gxf",
00510     NULL_IF_CONFIG_SMALL("GXF format"),
00511     0,
00512     gxf_probe,
00513     gxf_header,
00514     gxf_packet,
00515     NULL,
00516     gxf_seek,
00517     gxf_read_timestamp,
00518 };

Generated on Fri Sep 16 2011 17:17:48 for FFmpeg by  doxygen 1.7.1