• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • 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 "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}; // start with map packet
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 = 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(AVIOContext *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 = avio_r8(pb);
00165         int tlen = avio_r8(pb);
00166         *len -= 2;
00167         if (tlen > *len)
00168             return;
00169         *len -= tlen;
00170         if (tlen == 4) {
00171             uint32_t value = avio_rb32(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             avio_skip(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(AVIOContext *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 = avio_r8(pb);
00214         int tlen = avio_r8(pb);
00215         *len -= 2;
00216         if (tlen > *len)
00217             return;
00218         *len -= tlen;
00219         if (tlen == 4) {
00220             uint32_t value = avio_rb32(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             avio_skip(pb, tlen);
00227     }
00228 }
00229 
00233 static void gxf_read_index(AVFormatContext *s, int pkt_len) {
00234     AVIOContext *pb = s->pb;
00235     AVStream *st = s->streams[0];
00236     uint32_t fields_per_map = avio_rl32(pb);
00237     uint32_t map_cnt = avio_rl32(pb);
00238     int i;
00239     pkt_len -= 8;
00240     if (s->flags & AVFMT_FLAG_IGNIDX) {
00241         avio_skip(pb, pkt_len);
00242         return;
00243     }
00244     if (map_cnt > 1000) {
00245         av_log(s, AV_LOG_ERROR, "too many index entries %u (%x)\n", map_cnt, map_cnt);
00246         map_cnt = 1000;
00247     }
00248     if (pkt_len < 4 * map_cnt) {
00249         av_log(s, AV_LOG_ERROR, "invalid index length\n");
00250         avio_skip(pb, pkt_len);
00251         return;
00252     }
00253     pkt_len -= 4 * map_cnt;
00254     av_add_index_entry(st, 0, 0, 0, 0, 0);
00255     for (i = 0; i < map_cnt; i++)
00256         av_add_index_entry(st, (uint64_t)avio_rl32(pb) * 1024,
00257                            i * (uint64_t)fields_per_map + 1, 0, 0, 0);
00258     avio_skip(pb, pkt_len);
00259 }
00260 
00261 static int gxf_header(AVFormatContext *s, AVFormatParameters *ap) {
00262     AVIOContext *pb = s->pb;
00263     GXFPktType pkt_type;
00264     int map_len;
00265     int len;
00266     AVRational main_timebase = {0, 0};
00267     struct gxf_stream_info *si = s->priv_data;
00268     int i;
00269     if (!parse_packet_header(pb, &pkt_type, &map_len) || pkt_type != PKT_MAP) {
00270         av_log(s, AV_LOG_ERROR, "map packet not found\n");
00271         return 0;
00272     }
00273     map_len -= 2;
00274     if (avio_r8(pb) != 0x0e0 || avio_r8(pb) != 0xff) {
00275         av_log(s, AV_LOG_ERROR, "unknown version or invalid map preamble\n");
00276         return 0;
00277     }
00278     map_len -= 2;
00279     len = avio_rb16(pb); // length of material data section
00280     if (len > map_len) {
00281         av_log(s, AV_LOG_ERROR, "material data longer than map data\n");
00282         return 0;
00283     }
00284     map_len -= len;
00285     gxf_material_tags(pb, &len, si);
00286     avio_skip(pb, len);
00287     map_len -= 2;
00288     len = avio_rb16(pb); // length of track description
00289     if (len > map_len) {
00290         av_log(s, AV_LOG_ERROR, "track description longer than map data\n");
00291         return 0;
00292     }
00293     map_len -= len;
00294     while (len > 0) {
00295         int track_type, track_id, track_len;
00296         AVStream *st;
00297         int idx;
00298         len -= 4;
00299         track_type = avio_r8(pb);
00300         track_id = avio_r8(pb);
00301         track_len = avio_rb16(pb);
00302         len -= track_len;
00303         gxf_track_tags(pb, &track_len, si);
00304         avio_skip(pb, track_len);
00305         if (!(track_type & 0x80)) {
00306            av_log(s, AV_LOG_ERROR, "invalid track type %x\n", track_type);
00307            continue;
00308         }
00309         track_type &= 0x7f;
00310         if ((track_id & 0xc0) != 0xc0) {
00311            av_log(s, AV_LOG_ERROR, "invalid track id %x\n", track_id);
00312            continue;
00313         }
00314         track_id &= 0x3f;
00315         idx = get_sindex(s, track_id, track_type);
00316         if (idx < 0) continue;
00317         st = s->streams[idx];
00318         if (!main_timebase.num || !main_timebase.den) {
00319             main_timebase.num = si->frames_per_second.den;
00320             main_timebase.den = si->frames_per_second.num * 2;
00321         }
00322         st->start_time = si->first_field;
00323         if (si->first_field != AV_NOPTS_VALUE && si->last_field != AV_NOPTS_VALUE)
00324             st->duration = si->last_field - si->first_field;
00325     }
00326     if (len < 0)
00327         av_log(s, AV_LOG_ERROR, "invalid track description length specified\n");
00328     if (map_len)
00329         avio_skip(pb, map_len);
00330     if (!parse_packet_header(pb, &pkt_type, &len)) {
00331         av_log(s, AV_LOG_ERROR, "sync lost in header\n");
00332         return -1;
00333     }
00334     if (pkt_type == PKT_FLT) {
00335         gxf_read_index(s, len);
00336         if (!parse_packet_header(pb, &pkt_type, &len)) {
00337             av_log(s, AV_LOG_ERROR, "sync lost in header\n");
00338             return -1;
00339         }
00340     }
00341     if (pkt_type == PKT_UMF) {
00342         if (len >= 0x39) {
00343             AVRational fps;
00344             len -= 0x39;
00345             avio_skip(pb, 5); // preamble
00346             avio_skip(pb, 0x30); // payload description
00347             fps = fps_umf2avr(avio_rl32(pb));
00348             if (!main_timebase.num || !main_timebase.den) {
00349                 // this may not always be correct, but simply the best we can get
00350                 main_timebase.num = fps.den;
00351                 main_timebase.den = fps.num * 2;
00352             }
00353         } else
00354             av_log(s, AV_LOG_INFO, "UMF packet too short\n");
00355     } else
00356         av_log(s, AV_LOG_INFO, "UMF packet missing\n");
00357     avio_skip(pb, len);
00358     // set a fallback value, 60000/1001 is specified for audio-only files
00359     // so use that regardless of why we do not know the video frame rate.
00360     if (!main_timebase.num || !main_timebase.den)
00361         main_timebase = (AVRational){1001, 60000};
00362     for (i = 0; i < s->nb_streams; i++) {
00363         AVStream *st = s->streams[i];
00364         av_set_pts_info(st, 32, main_timebase.num, main_timebase.den);
00365     }
00366     return 0;
00367 }
00368 
00369 #define READ_ONE() \
00370     { \
00371         if (!max_interval-- || url_feof(pb)) \
00372             goto out; \
00373         tmp = tmp << 8 | avio_r8(pb); \
00374     }
00375 
00383 static int64_t gxf_resync_media(AVFormatContext *s, uint64_t max_interval, int track, int timestamp) {
00384     uint32_t tmp;
00385     uint64_t last_pos;
00386     uint64_t last_found_pos = 0;
00387     int cur_track;
00388     int64_t cur_timestamp = AV_NOPTS_VALUE;
00389     int len;
00390     AVIOContext *pb = s->pb;
00391     GXFPktType type;
00392     tmp = avio_rb32(pb);
00393 start:
00394     while (tmp)
00395         READ_ONE();
00396     READ_ONE();
00397     if (tmp != 1)
00398         goto start;
00399     last_pos = avio_tell(pb);
00400     if (avio_seek(pb, -5, SEEK_CUR) < 0)
00401         goto out;
00402     if (!parse_packet_header(pb, &type, &len) || type != PKT_MEDIA) {
00403         if (avio_seek(pb, last_pos, SEEK_SET) < 0)
00404             goto out;
00405         goto start;
00406     }
00407     avio_r8(pb);
00408     cur_track = avio_r8(pb);
00409     cur_timestamp = avio_rb32(pb);
00410     last_found_pos = avio_tell(pb) - 16 - 6;
00411     if ((track >= 0 && track != cur_track) || (timestamp >= 0 && timestamp > cur_timestamp)) {
00412         if (avio_seek(pb, last_pos, SEEK_SET) >= 0)
00413             goto start;
00414     }
00415 out:
00416     if (last_found_pos)
00417         avio_seek(pb, last_found_pos, SEEK_SET);
00418     return cur_timestamp;
00419 }
00420 
00421 static int gxf_packet(AVFormatContext *s, AVPacket *pkt) {
00422     AVIOContext *pb = s->pb;
00423     GXFPktType pkt_type;
00424     int pkt_len;
00425     struct gxf_stream_info *si = s->priv_data;
00426 
00427     while (!url_feof(pb)) {
00428         AVStream *st;
00429         int track_type, track_id, ret;
00430         int field_nr, field_info, skip = 0;
00431         int stream_index;
00432         if (!parse_packet_header(pb, &pkt_type, &pkt_len)) {
00433             if (!url_feof(pb))
00434                 av_log(s, AV_LOG_ERROR, "sync lost\n");
00435             return -1;
00436         }
00437         if (pkt_type == PKT_FLT) {
00438             gxf_read_index(s, pkt_len);
00439             continue;
00440         }
00441         if (pkt_type != PKT_MEDIA) {
00442             avio_skip(pb, pkt_len);
00443             continue;
00444         }
00445         if (pkt_len < 16) {
00446             av_log(s, AV_LOG_ERROR, "invalid media packet length\n");
00447             continue;
00448         }
00449         pkt_len -= 16;
00450         track_type = avio_r8(pb);
00451         track_id = avio_r8(pb);
00452         stream_index = get_sindex(s, track_id, track_type);
00453         if (stream_index < 0)
00454             return stream_index;
00455         st = s->streams[stream_index];
00456         field_nr = avio_rb32(pb);
00457         field_info = avio_rb32(pb);
00458         avio_rb32(pb); // "timeline" field number
00459         avio_r8(pb); // flags
00460         avio_r8(pb); // reserved
00461         if (st->codec->codec_id == CODEC_ID_PCM_S24LE ||
00462             st->codec->codec_id == CODEC_ID_PCM_S16LE) {
00463             int first = field_info >> 16;
00464             int last  = field_info & 0xffff; // last is exclusive
00465             int bps = av_get_bits_per_sample(st->codec->codec_id)>>3;
00466             if (first <= last && last*bps <= pkt_len) {
00467                 avio_skip(pb, first*bps);
00468                 skip = pkt_len - last*bps;
00469                 pkt_len = (last-first)*bps;
00470             } else
00471                 av_log(s, AV_LOG_ERROR, "invalid first and last sample values\n");
00472         }
00473         ret = av_get_packet(pb, pkt, pkt_len);
00474         if (skip)
00475             avio_skip(pb, skip);
00476         pkt->stream_index = stream_index;
00477         pkt->dts = field_nr;
00478 
00479         //set duration manually for DV or else lavf misdetects the frame rate
00480         if (st->codec->codec_id == CODEC_ID_DVVIDEO)
00481             pkt->duration = si->fields_per_frame;
00482 
00483         return ret;
00484     }
00485     return AVERROR(EIO);
00486 }
00487 
00488 static int gxf_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
00489     int res = 0;
00490     uint64_t pos;
00491     uint64_t maxlen = 100 * 1024 * 1024;
00492     AVStream *st = s->streams[0];
00493     int64_t start_time = s->streams[stream_index]->start_time;
00494     int64_t found;
00495     int idx;
00496     if (timestamp < start_time) timestamp = start_time;
00497     idx = av_index_search_timestamp(st, timestamp - start_time,
00498                                     AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
00499     if (idx < 0)
00500         return -1;
00501     pos = st->index_entries[idx].pos;
00502     if (idx < st->nb_index_entries - 2)
00503         maxlen = st->index_entries[idx + 2].pos - pos;
00504     maxlen = FFMAX(maxlen, 200 * 1024);
00505     res = avio_seek(s->pb, pos, SEEK_SET);
00506     if (res < 0)
00507         return res;
00508     found = gxf_resync_media(s, maxlen, -1, timestamp);
00509     if (FFABS(found - timestamp) > 4)
00510         return -1;
00511     return 0;
00512 }
00513 
00514 static int64_t gxf_read_timestamp(AVFormatContext *s, int stream_index,
00515                                   int64_t *pos, int64_t pos_limit) {
00516     AVIOContext *pb = s->pb;
00517     int64_t res;
00518     if (avio_seek(pb, *pos, SEEK_SET) < 0)
00519         return AV_NOPTS_VALUE;
00520     res = gxf_resync_media(s, pos_limit - *pos, -1, -1);
00521     *pos = avio_tell(pb);
00522     return res;
00523 }
00524 
00525 AVInputFormat ff_gxf_demuxer = {
00526     "gxf",
00527     NULL_IF_CONFIG_SMALL("GXF format"),
00528     sizeof(struct gxf_stream_info),
00529     gxf_probe,
00530     gxf_header,
00531     gxf_packet,
00532     NULL,
00533     gxf_seek,
00534     gxf_read_timestamp,
00535 };

Generated on Wed Apr 11 2012 07:31:36 for FFmpeg by  doxygen 1.7.1