yading@11: /* yading@11: * GXF demuxer. yading@11: * Copyright (c) 2006 Reimar Doeffinger yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include "libavutil/channel_layout.h" yading@11: #include "libavutil/common.h" yading@11: #include "avformat.h" yading@11: #include "internal.h" yading@11: #include "gxf.h" yading@11: #include "libavcodec/mpeg12data.h" yading@11: yading@11: struct gxf_stream_info { yading@11: int64_t first_field; yading@11: int64_t last_field; yading@11: AVRational frames_per_second; yading@11: int32_t fields_per_frame; yading@11: int64_t track_aux_data; yading@11: }; yading@11: yading@11: /** yading@11: * @brief parse gxf timecode and add it to metadata yading@11: */ yading@11: static int add_timecode_metadata(AVDictionary **pm, const char *key, uint32_t timecode, int fields_per_frame) yading@11: { yading@11: char tmp[128]; yading@11: int field = timecode & 0xff; yading@11: int frame = fields_per_frame ? field / fields_per_frame : field; yading@11: int second = (timecode >> 8) & 0xff; yading@11: int minute = (timecode >> 16) & 0xff; yading@11: int hour = (timecode >> 24) & 0x1f; yading@11: int drop = (timecode >> 29) & 1; yading@11: // bit 30: color_frame, unused yading@11: // ignore invalid time code yading@11: if (timecode >> 31) yading@11: return 0; yading@11: snprintf(tmp, sizeof(tmp), "%02d:%02d:%02d%c%02d", yading@11: hour, minute, second, drop ? ';' : ':', frame); yading@11: return av_dict_set(pm, key, tmp, 0); yading@11: } yading@11: yading@11: /** yading@11: * @brief parses a packet header, extracting type and length yading@11: * @param pb AVIOContext to read header from yading@11: * @param type detected packet type is stored here yading@11: * @param length detected packet length, excluding header is stored here yading@11: * @return 0 if header not found or contains invalid data, 1 otherwise yading@11: */ yading@11: static int parse_packet_header(AVIOContext *pb, GXFPktType *type, int *length) { yading@11: if (avio_rb32(pb)) yading@11: return 0; yading@11: if (avio_r8(pb) != 1) yading@11: return 0; yading@11: *type = avio_r8(pb); yading@11: *length = avio_rb32(pb); yading@11: if ((*length >> 24) || *length < 16) yading@11: return 0; yading@11: *length -= 16; yading@11: if (avio_rb32(pb)) yading@11: return 0; yading@11: if (avio_r8(pb) != 0xe1) yading@11: return 0; yading@11: if (avio_r8(pb) != 0xe2) yading@11: return 0; yading@11: return 1; yading@11: } yading@11: yading@11: /** yading@11: * @brief check if file starts with a PKT_MAP header yading@11: */ yading@11: static int gxf_probe(AVProbeData *p) { yading@11: static const uint8_t startcode[] = {0, 0, 0, 0, 1, 0xbc}; // start with map packet yading@11: static const uint8_t endcode[] = {0, 0, 0, 0, 0xe1, 0xe2}; yading@11: if (!memcmp(p->buf, startcode, sizeof(startcode)) && yading@11: !memcmp(&p->buf[16 - sizeof(endcode)], endcode, sizeof(endcode))) yading@11: return AVPROBE_SCORE_MAX; yading@11: return 0; yading@11: } yading@11: yading@11: /** yading@11: * @brief gets the stream index for the track with the specified id, creates new yading@11: * stream if not found yading@11: * @param id id of stream to find / add yading@11: * @param format stream format identifier yading@11: */ yading@11: static int get_sindex(AVFormatContext *s, int id, int format) { yading@11: int i; yading@11: AVStream *st = NULL; yading@11: i = ff_find_stream_index(s, id); yading@11: if (i >= 0) yading@11: return i; yading@11: st = avformat_new_stream(s, NULL); yading@11: if (!st) yading@11: return AVERROR(ENOMEM); yading@11: st->id = id; yading@11: switch (format) { yading@11: case 3: yading@11: case 4: yading@11: st->codec->codec_type = AVMEDIA_TYPE_VIDEO; yading@11: st->codec->codec_id = AV_CODEC_ID_MJPEG; yading@11: break; yading@11: case 13: yading@11: case 15: yading@11: st->codec->codec_type = AVMEDIA_TYPE_VIDEO; yading@11: st->codec->codec_id = AV_CODEC_ID_DVVIDEO; yading@11: break; yading@11: case 14: yading@11: case 16: yading@11: st->codec->codec_type = AVMEDIA_TYPE_VIDEO; yading@11: st->codec->codec_id = AV_CODEC_ID_DVVIDEO; yading@11: break; yading@11: case 11: yading@11: case 12: yading@11: case 20: yading@11: st->codec->codec_type = AVMEDIA_TYPE_VIDEO; yading@11: st->codec->codec_id = AV_CODEC_ID_MPEG2VIDEO; yading@11: st->need_parsing = AVSTREAM_PARSE_HEADERS; //get keyframe flag etc. yading@11: break; yading@11: case 22: yading@11: case 23: yading@11: st->codec->codec_type = AVMEDIA_TYPE_VIDEO; yading@11: st->codec->codec_id = AV_CODEC_ID_MPEG1VIDEO; yading@11: st->need_parsing = AVSTREAM_PARSE_HEADERS; //get keyframe flag etc. yading@11: break; yading@11: case 9: yading@11: st->codec->codec_type = AVMEDIA_TYPE_AUDIO; yading@11: st->codec->codec_id = AV_CODEC_ID_PCM_S24LE; yading@11: st->codec->channels = 1; yading@11: st->codec->channel_layout = AV_CH_LAYOUT_MONO; yading@11: st->codec->sample_rate = 48000; yading@11: st->codec->bit_rate = 3 * 1 * 48000 * 8; yading@11: st->codec->block_align = 3 * 1; yading@11: st->codec->bits_per_coded_sample = 24; yading@11: break; yading@11: case 10: yading@11: st->codec->codec_type = AVMEDIA_TYPE_AUDIO; yading@11: st->codec->codec_id = AV_CODEC_ID_PCM_S16LE; yading@11: st->codec->channels = 1; yading@11: st->codec->channel_layout = AV_CH_LAYOUT_MONO; yading@11: st->codec->sample_rate = 48000; yading@11: st->codec->bit_rate = 2 * 1 * 48000 * 8; yading@11: st->codec->block_align = 2 * 1; yading@11: st->codec->bits_per_coded_sample = 16; yading@11: break; yading@11: case 17: yading@11: st->codec->codec_type = AVMEDIA_TYPE_AUDIO; yading@11: st->codec->codec_id = AV_CODEC_ID_AC3; yading@11: st->codec->channels = 2; yading@11: st->codec->channel_layout = AV_CH_LAYOUT_STEREO; yading@11: st->codec->sample_rate = 48000; yading@11: break; yading@11: // timecode tracks: yading@11: case 7: yading@11: case 8: yading@11: case 24: yading@11: st->codec->codec_type = AVMEDIA_TYPE_DATA; yading@11: st->codec->codec_id = AV_CODEC_ID_NONE; yading@11: break; yading@11: default: yading@11: st->codec->codec_type = AVMEDIA_TYPE_UNKNOWN; yading@11: st->codec->codec_id = AV_CODEC_ID_NONE; yading@11: break; yading@11: } yading@11: return s->nb_streams - 1; yading@11: } yading@11: yading@11: /** yading@11: * @brief filters out interesting tags from material information. yading@11: * @param len length of tag section, will be adjusted to contain remaining bytes yading@11: * @param si struct to store collected information into yading@11: */ yading@11: static void gxf_material_tags(AVIOContext *pb, int *len, struct gxf_stream_info *si) { yading@11: si->first_field = AV_NOPTS_VALUE; yading@11: si->last_field = AV_NOPTS_VALUE; yading@11: while (*len >= 2) { yading@11: GXFMatTag tag = avio_r8(pb); yading@11: int tlen = avio_r8(pb); yading@11: *len -= 2; yading@11: if (tlen > *len) yading@11: return; yading@11: *len -= tlen; yading@11: if (tlen == 4) { yading@11: uint32_t value = avio_rb32(pb); yading@11: if (tag == MAT_FIRST_FIELD) yading@11: si->first_field = value; yading@11: else if (tag == MAT_LAST_FIELD) yading@11: si->last_field = value; yading@11: } else yading@11: avio_skip(pb, tlen); yading@11: } yading@11: } yading@11: yading@11: static const AVRational frame_rate_tab[] = { yading@11: { 60, 1}, yading@11: {60000, 1001}, yading@11: { 50, 1}, yading@11: { 30, 1}, yading@11: {30000, 1001}, yading@11: { 25, 1}, yading@11: { 24, 1}, yading@11: {24000, 1001}, yading@11: { 0, 0}, yading@11: }; yading@11: yading@11: /** yading@11: * @brief convert fps tag value to AVRational fps yading@11: * @param fps fps value from tag yading@11: * @return fps as AVRational, or 0 / 0 if unknown yading@11: */ yading@11: static AVRational fps_tag2avr(int32_t fps) { yading@11: if (fps < 1 || fps > 9) fps = 9; yading@11: return frame_rate_tab[fps - 1]; yading@11: } yading@11: yading@11: /** yading@11: * @brief convert UMF attributes flags to AVRational fps yading@11: * @param flags UMF flags to convert yading@11: * @return fps as AVRational, or 0 / 0 if unknown yading@11: */ yading@11: static AVRational fps_umf2avr(uint32_t flags) { yading@11: static const AVRational map[] = {{50, 1}, {60000, 1001}, {24, 1}, yading@11: {25, 1}, {30000, 1001}}; yading@11: int idx = av_log2((flags & 0x7c0) >> 6); yading@11: return map[idx]; yading@11: } yading@11: yading@11: /** yading@11: * @brief filters out interesting tags from track information. yading@11: * @param len length of tag section, will be adjusted to contain remaining bytes yading@11: * @param si struct to store collected information into yading@11: */ yading@11: static void gxf_track_tags(AVIOContext *pb, int *len, struct gxf_stream_info *si) { yading@11: si->frames_per_second = (AVRational){0, 0}; yading@11: si->fields_per_frame = 0; yading@11: si->track_aux_data = 0x80000000; yading@11: while (*len >= 2) { yading@11: GXFTrackTag tag = avio_r8(pb); yading@11: int tlen = avio_r8(pb); yading@11: *len -= 2; yading@11: if (tlen > *len) yading@11: return; yading@11: *len -= tlen; yading@11: if (tlen == 4) { yading@11: uint32_t value = avio_rb32(pb); yading@11: if (tag == TRACK_FPS) yading@11: si->frames_per_second = fps_tag2avr(value); yading@11: else if (tag == TRACK_FPF && (value == 1 || value == 2)) yading@11: si->fields_per_frame = value; yading@11: } else if (tlen == 8 && tag == TRACK_AUX) yading@11: si->track_aux_data = avio_rl64(pb); yading@11: else yading@11: avio_skip(pb, tlen); yading@11: } yading@11: } yading@11: yading@11: /** yading@11: * @brief read index from FLT packet into stream 0 av_index yading@11: */ yading@11: static void gxf_read_index(AVFormatContext *s, int pkt_len) { yading@11: AVIOContext *pb = s->pb; yading@11: AVStream *st; yading@11: uint32_t fields_per_map = avio_rl32(pb); yading@11: uint32_t map_cnt = avio_rl32(pb); yading@11: int i; yading@11: pkt_len -= 8; yading@11: if ((s->flags & AVFMT_FLAG_IGNIDX) || !s->streams) { yading@11: avio_skip(pb, pkt_len); yading@11: return; yading@11: } yading@11: st = s->streams[0]; yading@11: if (map_cnt > 1000) { yading@11: av_log(s, AV_LOG_ERROR, "too many index entries %u (%x)\n", map_cnt, map_cnt); yading@11: map_cnt = 1000; yading@11: } yading@11: if (pkt_len < 4 * map_cnt) { yading@11: av_log(s, AV_LOG_ERROR, "invalid index length\n"); yading@11: avio_skip(pb, pkt_len); yading@11: return; yading@11: } yading@11: pkt_len -= 4 * map_cnt; yading@11: av_add_index_entry(st, 0, 0, 0, 0, 0); yading@11: for (i = 0; i < map_cnt; i++) yading@11: av_add_index_entry(st, (uint64_t)avio_rl32(pb) * 1024, yading@11: i * (uint64_t)fields_per_map + 1, 0, 0, 0); yading@11: avio_skip(pb, pkt_len); yading@11: } yading@11: yading@11: static int gxf_header(AVFormatContext *s) { yading@11: AVIOContext *pb = s->pb; yading@11: GXFPktType pkt_type; yading@11: int map_len; yading@11: int len; yading@11: AVRational main_timebase = {0, 0}; yading@11: struct gxf_stream_info *si = s->priv_data; yading@11: int i; yading@11: if (!parse_packet_header(pb, &pkt_type, &map_len) || pkt_type != PKT_MAP) { yading@11: av_log(s, AV_LOG_ERROR, "map packet not found\n"); yading@11: return 0; yading@11: } yading@11: map_len -= 2; yading@11: if (avio_r8(pb) != 0x0e0 || avio_r8(pb) != 0xff) { yading@11: av_log(s, AV_LOG_ERROR, "unknown version or invalid map preamble\n"); yading@11: return 0; yading@11: } yading@11: map_len -= 2; yading@11: len = avio_rb16(pb); // length of material data section yading@11: if (len > map_len) { yading@11: av_log(s, AV_LOG_ERROR, "material data longer than map data\n"); yading@11: return 0; yading@11: } yading@11: map_len -= len; yading@11: gxf_material_tags(pb, &len, si); yading@11: avio_skip(pb, len); yading@11: map_len -= 2; yading@11: len = avio_rb16(pb); // length of track description yading@11: if (len > map_len) { yading@11: av_log(s, AV_LOG_ERROR, "track description longer than map data\n"); yading@11: return 0; yading@11: } yading@11: map_len -= len; yading@11: while (len > 0) { yading@11: int track_type, track_id, track_len; yading@11: AVStream *st; yading@11: int idx; yading@11: len -= 4; yading@11: track_type = avio_r8(pb); yading@11: track_id = avio_r8(pb); yading@11: track_len = avio_rb16(pb); yading@11: len -= track_len; yading@11: if (!(track_type & 0x80)) { yading@11: av_log(s, AV_LOG_ERROR, "invalid track type %x\n", track_type); yading@11: continue; yading@11: } yading@11: track_type &= 0x7f; yading@11: if ((track_id & 0xc0) != 0xc0) { yading@11: av_log(s, AV_LOG_ERROR, "invalid track id %x\n", track_id); yading@11: continue; yading@11: } yading@11: track_id &= 0x3f; yading@11: gxf_track_tags(pb, &track_len, si); yading@11: // check for timecode tracks yading@11: if (track_type == 7 || track_type == 8 || track_type == 24) { yading@11: add_timecode_metadata(&s->metadata, "timecode", yading@11: si->track_aux_data & 0xffffffff, yading@11: si->fields_per_frame); yading@11: yading@11: } yading@11: avio_skip(pb, track_len); yading@11: yading@11: idx = get_sindex(s, track_id, track_type); yading@11: if (idx < 0) continue; yading@11: st = s->streams[idx]; yading@11: if (!main_timebase.num || !main_timebase.den) { yading@11: main_timebase.num = si->frames_per_second.den; yading@11: main_timebase.den = si->frames_per_second.num * 2; yading@11: } yading@11: st->start_time = si->first_field; yading@11: if (si->first_field != AV_NOPTS_VALUE && si->last_field != AV_NOPTS_VALUE) yading@11: st->duration = si->last_field - si->first_field; yading@11: } yading@11: if (len < 0) yading@11: av_log(s, AV_LOG_ERROR, "invalid track description length specified\n"); yading@11: if (map_len) yading@11: avio_skip(pb, map_len); yading@11: if (!parse_packet_header(pb, &pkt_type, &len)) { yading@11: av_log(s, AV_LOG_ERROR, "sync lost in header\n"); yading@11: return -1; yading@11: } yading@11: if (pkt_type == PKT_FLT) { yading@11: gxf_read_index(s, len); yading@11: if (!parse_packet_header(pb, &pkt_type, &len)) { yading@11: av_log(s, AV_LOG_ERROR, "sync lost in header\n"); yading@11: return -1; yading@11: } yading@11: } yading@11: if (pkt_type == PKT_UMF) { yading@11: if (len >= 0x39) { yading@11: AVRational fps; yading@11: len -= 0x39; yading@11: avio_skip(pb, 5); // preamble yading@11: avio_skip(pb, 0x30); // payload description yading@11: fps = fps_umf2avr(avio_rl32(pb)); yading@11: if (!main_timebase.num || !main_timebase.den) { yading@11: av_log(s, AV_LOG_WARNING, "No FPS track tag, using UMF fps tag." yading@11: " This might give wrong results.\n"); yading@11: // this may not always be correct, but simply the best we can get yading@11: main_timebase.num = fps.den; yading@11: main_timebase.den = fps.num * 2; yading@11: } yading@11: yading@11: if (len >= 0x18) { yading@11: len -= 0x18; yading@11: avio_skip(pb, 0x10); yading@11: add_timecode_metadata(&s->metadata, "timecode_at_mark_in", yading@11: avio_rl32(pb), si->fields_per_frame); yading@11: add_timecode_metadata(&s->metadata, "timecode_at_mark_out", yading@11: avio_rl32(pb), si->fields_per_frame); yading@11: } yading@11: } else yading@11: av_log(s, AV_LOG_INFO, "UMF packet too short\n"); yading@11: } else yading@11: av_log(s, AV_LOG_INFO, "UMF packet missing\n"); yading@11: avio_skip(pb, len); yading@11: // set a fallback value, 60000/1001 is specified for audio-only files yading@11: // so use that regardless of why we do not know the video frame rate. yading@11: if (!main_timebase.num || !main_timebase.den) yading@11: main_timebase = (AVRational){1001, 60000}; yading@11: for (i = 0; i < s->nb_streams; i++) { yading@11: AVStream *st = s->streams[i]; yading@11: avpriv_set_pts_info(st, 32, main_timebase.num, main_timebase.den); yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: #define READ_ONE() \ yading@11: { \ yading@11: if (!max_interval-- || url_feof(pb)) \ yading@11: goto out; \ yading@11: tmp = tmp << 8 | avio_r8(pb); \ yading@11: } yading@11: yading@11: /** yading@11: * @brief resync the stream on the next media packet with specified properties yading@11: * @param max_interval how many bytes to search for matching packet at most yading@11: * @param track track id the media packet must belong to, -1 for any yading@11: * @param timestamp minimum timestamp (== field number) the packet must have, -1 for any yading@11: * @return timestamp of packet found yading@11: */ yading@11: static int64_t gxf_resync_media(AVFormatContext *s, uint64_t max_interval, int track, int timestamp) { yading@11: uint32_t tmp; yading@11: uint64_t last_pos; yading@11: uint64_t last_found_pos = 0; yading@11: int cur_track; yading@11: int64_t cur_timestamp = AV_NOPTS_VALUE; yading@11: int len; yading@11: AVIOContext *pb = s->pb; yading@11: GXFPktType type; yading@11: tmp = avio_rb32(pb); yading@11: start: yading@11: while (tmp) yading@11: READ_ONE(); yading@11: READ_ONE(); yading@11: if (tmp != 1) yading@11: goto start; yading@11: last_pos = avio_tell(pb); yading@11: if (avio_seek(pb, -5, SEEK_CUR) < 0) yading@11: goto out; yading@11: if (!parse_packet_header(pb, &type, &len) || type != PKT_MEDIA) { yading@11: if (avio_seek(pb, last_pos, SEEK_SET) < 0) yading@11: goto out; yading@11: goto start; yading@11: } yading@11: avio_r8(pb); yading@11: cur_track = avio_r8(pb); yading@11: cur_timestamp = avio_rb32(pb); yading@11: last_found_pos = avio_tell(pb) - 16 - 6; yading@11: if ((track >= 0 && track != cur_track) || (timestamp >= 0 && timestamp > cur_timestamp)) { yading@11: if (avio_seek(pb, last_pos, SEEK_SET) >= 0) yading@11: goto start; yading@11: } yading@11: out: yading@11: if (last_found_pos) yading@11: avio_seek(pb, last_found_pos, SEEK_SET); yading@11: return cur_timestamp; yading@11: } yading@11: yading@11: static int gxf_packet(AVFormatContext *s, AVPacket *pkt) { yading@11: AVIOContext *pb = s->pb; yading@11: GXFPktType pkt_type; yading@11: int pkt_len; yading@11: struct gxf_stream_info *si = s->priv_data; yading@11: yading@11: while (!pb->eof_reached) { yading@11: AVStream *st; yading@11: int track_type, track_id, ret; yading@11: int field_nr, field_info, skip = 0; yading@11: int stream_index; yading@11: if (!parse_packet_header(pb, &pkt_type, &pkt_len)) { yading@11: if (!url_feof(pb)) yading@11: av_log(s, AV_LOG_ERROR, "sync lost\n"); yading@11: return -1; yading@11: } yading@11: if (pkt_type == PKT_FLT) { yading@11: gxf_read_index(s, pkt_len); yading@11: continue; yading@11: } yading@11: if (pkt_type != PKT_MEDIA) { yading@11: avio_skip(pb, pkt_len); yading@11: continue; yading@11: } yading@11: if (pkt_len < 16) { yading@11: av_log(s, AV_LOG_ERROR, "invalid media packet length\n"); yading@11: continue; yading@11: } yading@11: pkt_len -= 16; yading@11: track_type = avio_r8(pb); yading@11: track_id = avio_r8(pb); yading@11: stream_index = get_sindex(s, track_id, track_type); yading@11: if (stream_index < 0) yading@11: return stream_index; yading@11: st = s->streams[stream_index]; yading@11: field_nr = avio_rb32(pb); yading@11: field_info = avio_rb32(pb); yading@11: avio_rb32(pb); // "timeline" field number yading@11: avio_r8(pb); // flags yading@11: avio_r8(pb); // reserved yading@11: if (st->codec->codec_id == AV_CODEC_ID_PCM_S24LE || yading@11: st->codec->codec_id == AV_CODEC_ID_PCM_S16LE) { yading@11: int first = field_info >> 16; yading@11: int last = field_info & 0xffff; // last is exclusive yading@11: int bps = av_get_bits_per_sample(st->codec->codec_id)>>3; yading@11: if (first <= last && last*bps <= pkt_len) { yading@11: avio_skip(pb, first*bps); yading@11: skip = pkt_len - last*bps; yading@11: pkt_len = (last-first)*bps; yading@11: } else yading@11: av_log(s, AV_LOG_ERROR, "invalid first and last sample values\n"); yading@11: } yading@11: ret = av_get_packet(pb, pkt, pkt_len); yading@11: if (skip) yading@11: avio_skip(pb, skip); yading@11: pkt->stream_index = stream_index; yading@11: pkt->dts = field_nr; yading@11: yading@11: //set duration manually for DV or else lavf misdetects the frame rate yading@11: if (st->codec->codec_id == AV_CODEC_ID_DVVIDEO) yading@11: pkt->duration = si->fields_per_frame; yading@11: yading@11: return ret; yading@11: } yading@11: return AVERROR_EOF; yading@11: } yading@11: yading@11: static int gxf_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) { yading@11: int res = 0; yading@11: uint64_t pos; yading@11: uint64_t maxlen = 100 * 1024 * 1024; yading@11: AVStream *st = s->streams[0]; yading@11: int64_t start_time = s->streams[stream_index]->start_time; yading@11: int64_t found; yading@11: int idx; yading@11: if (timestamp < start_time) timestamp = start_time; yading@11: idx = av_index_search_timestamp(st, timestamp - start_time, yading@11: AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD); yading@11: if (idx < 0) yading@11: return -1; yading@11: pos = st->index_entries[idx].pos; yading@11: if (idx < st->nb_index_entries - 2) yading@11: maxlen = st->index_entries[idx + 2].pos - pos; yading@11: maxlen = FFMAX(maxlen, 200 * 1024); yading@11: res = avio_seek(s->pb, pos, SEEK_SET); yading@11: if (res < 0) yading@11: return res; yading@11: found = gxf_resync_media(s, maxlen, -1, timestamp); yading@11: if (FFABS(found - timestamp) > 4) yading@11: return -1; yading@11: return 0; yading@11: } yading@11: yading@11: static int64_t gxf_read_timestamp(AVFormatContext *s, int stream_index, yading@11: int64_t *pos, int64_t pos_limit) { yading@11: AVIOContext *pb = s->pb; yading@11: int64_t res; yading@11: if (avio_seek(pb, *pos, SEEK_SET) < 0) yading@11: return AV_NOPTS_VALUE; yading@11: res = gxf_resync_media(s, pos_limit - *pos, -1, -1); yading@11: *pos = avio_tell(pb); yading@11: return res; yading@11: } yading@11: yading@11: AVInputFormat ff_gxf_demuxer = { yading@11: .name = "gxf", yading@11: .long_name = NULL_IF_CONFIG_SMALL("GXF (General eXchange Format)"), yading@11: .priv_data_size = sizeof(struct gxf_stream_info), yading@11: .read_probe = gxf_probe, yading@11: .read_header = gxf_header, yading@11: .read_packet = gxf_packet, yading@11: .read_seek = gxf_seek, yading@11: .read_timestamp = gxf_read_timestamp, yading@11: };