yading@11: /* yading@11: * seek utility functions for use within format handlers yading@11: * yading@11: * Copyright (c) 2009 Ivan Schreter 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 "seek.h" yading@11: #include "libavutil/mathematics.h" yading@11: #include "libavutil/mem.h" yading@11: #include "internal.h" yading@11: yading@11: // NOTE: implementation should be moved here in another patch, to keep patches yading@11: // separated. yading@11: yading@11: /** yading@11: * helper structure describing keyframe search state of one stream yading@11: */ yading@11: typedef struct { yading@11: int64_t pos_lo; ///< position of the frame with low timestamp in file or INT64_MAX if not found (yet) yading@11: int64_t ts_lo; ///< frame presentation timestamp or same as pos_lo for byte seeking yading@11: yading@11: int64_t pos_hi; ///< position of the frame with high timestamp in file or INT64_MAX if not found (yet) yading@11: int64_t ts_hi; ///< frame presentation timestamp or same as pos_hi for byte seeking yading@11: yading@11: int64_t last_pos; ///< last known position of a frame, for multi-frame packets yading@11: yading@11: int64_t term_ts; ///< termination timestamp (which TS we already read) yading@11: AVRational term_ts_tb; ///< timebase for term_ts yading@11: int64_t first_ts; ///< first packet timestamp in this iteration (to fill term_ts later) yading@11: AVRational first_ts_tb; ///< timebase for first_ts yading@11: yading@11: int terminated; ///< termination flag for the current iteration yading@11: } AVSyncPoint; yading@11: yading@11: /** yading@11: * Compute a distance between timestamps. yading@11: * yading@11: * Distances are only comparable, if same time bases are used for computing yading@11: * distances. yading@11: * yading@11: * @param ts_hi high timestamp yading@11: * @param tb_hi high timestamp time base yading@11: * @param ts_lo low timestamp yading@11: * @param tb_lo low timestamp time base yading@11: * @return representation of distance between high and low timestamps yading@11: */ yading@11: static int64_t ts_distance(int64_t ts_hi, yading@11: AVRational tb_hi, yading@11: int64_t ts_lo, yading@11: AVRational tb_lo) yading@11: { yading@11: int64_t hi, lo; yading@11: yading@11: hi = ts_hi * tb_hi.num * tb_lo.den; yading@11: lo = ts_lo * tb_lo.num * tb_hi.den; yading@11: yading@11: return hi - lo; yading@11: } yading@11: yading@11: /** yading@11: * Partial search for keyframes in multiple streams. yading@11: * yading@11: * This routine searches in each stream for the next lower and the next higher yading@11: * timestamp compared to the given target timestamp. The search starts at the current yading@11: * file position and ends at the file position, where all streams have already been yading@11: * examined (or when all higher key frames are found in the first iteration). yading@11: * yading@11: * This routine is called iteratively with an exponential backoff to find the lower yading@11: * timestamp. yading@11: * yading@11: * @param s format context yading@11: * @param timestamp target timestamp (or position, if AVSEEK_FLAG_BYTE) yading@11: * @param timebase time base for timestamps yading@11: * @param flags seeking flags yading@11: * @param sync array with information per stream yading@11: * @param keyframes_to_find count of keyframes to find in total yading@11: * @param found_lo ptr to the count of already found low timestamp keyframes yading@11: * @param found_hi ptr to the count of already found high timestamp keyframes yading@11: * @param first_iter flag for first iteration yading@11: */ yading@11: static void search_hi_lo_keyframes(AVFormatContext *s, yading@11: int64_t timestamp, yading@11: AVRational timebase, yading@11: int flags, yading@11: AVSyncPoint *sync, yading@11: int keyframes_to_find, yading@11: int *found_lo, yading@11: int *found_hi, yading@11: int first_iter) yading@11: { yading@11: AVPacket pkt; yading@11: AVSyncPoint *sp; yading@11: AVStream *st; yading@11: int idx; yading@11: int flg; yading@11: int terminated_count = 0; yading@11: int64_t pos; yading@11: int64_t pts, dts; // PTS/DTS from stream yading@11: int64_t ts; // PTS in stream-local time base or position for byte seeking yading@11: AVRational ts_tb; // Time base of the stream or 1:1 for byte seeking yading@11: yading@11: for (;;) { yading@11: if (av_read_frame(s, &pkt) < 0) { yading@11: // EOF or error, make sure high flags are set yading@11: for (idx = 0; idx < s->nb_streams; ++idx) { yading@11: if (s->streams[idx]->discard < AVDISCARD_ALL) { yading@11: sp = &sync[idx]; yading@11: if (sp->pos_hi == INT64_MAX) { yading@11: // no high frame exists for this stream yading@11: (*found_hi)++; yading@11: sp->ts_hi = INT64_MAX; yading@11: sp->pos_hi = INT64_MAX - 1; yading@11: } yading@11: } yading@11: } yading@11: break; yading@11: } yading@11: yading@11: idx = pkt.stream_index; yading@11: st = s->streams[idx]; yading@11: if (st->discard >= AVDISCARD_ALL) yading@11: // this stream is not active, skip packet yading@11: continue; yading@11: yading@11: sp = &sync[idx]; yading@11: yading@11: flg = pkt.flags; yading@11: pos = pkt.pos; yading@11: pts = pkt.pts; yading@11: dts = pkt.dts; yading@11: if (pts == AV_NOPTS_VALUE) yading@11: // some formats don't provide PTS, only DTS yading@11: pts = dts; yading@11: yading@11: av_free_packet(&pkt); yading@11: yading@11: // Multi-frame packets only return position for the very first frame. yading@11: // Other frames are read with position == -1. Therefore, we note down yading@11: // last known position of a frame and use it if a frame without yading@11: // position arrives. In this way, it's possible to seek to proper yading@11: // position. Additionally, for parsers not providing position at all, yading@11: // an approximation will be used (starting position of this iteration). yading@11: if (pos < 0) yading@11: pos = sp->last_pos; yading@11: else yading@11: sp->last_pos = pos; yading@11: yading@11: // Evaluate key frames with known TS (or any frames, if AVSEEK_FLAG_ANY set). yading@11: if (pts != AV_NOPTS_VALUE && yading@11: ((flg & AV_PKT_FLAG_KEY) || (flags & AVSEEK_FLAG_ANY))) { yading@11: if (flags & AVSEEK_FLAG_BYTE) { yading@11: // for byte seeking, use position as timestamp yading@11: ts = pos; yading@11: ts_tb.num = 1; yading@11: ts_tb.den = 1; yading@11: } else { yading@11: // otherwise, get stream time_base yading@11: ts = pts; yading@11: ts_tb = st->time_base; yading@11: } yading@11: yading@11: if (sp->first_ts == AV_NOPTS_VALUE) { yading@11: // Note down termination timestamp for the next iteration - when yading@11: // we encounter a packet with the same timestamp, we will ignore yading@11: // any further packets for this stream in next iteration (as they yading@11: // are already evaluated). yading@11: sp->first_ts = ts; yading@11: sp->first_ts_tb = ts_tb; yading@11: } yading@11: yading@11: if (sp->term_ts != AV_NOPTS_VALUE && yading@11: av_compare_ts(ts, ts_tb, sp->term_ts, sp->term_ts_tb) > 0) { yading@11: // past the end position from last iteration, ignore packet yading@11: if (!sp->terminated) { yading@11: sp->terminated = 1; yading@11: ++terminated_count; yading@11: if (sp->pos_hi == INT64_MAX) { yading@11: // no high frame exists for this stream yading@11: (*found_hi)++; yading@11: sp->ts_hi = INT64_MAX; yading@11: sp->pos_hi = INT64_MAX - 1; yading@11: } yading@11: if (terminated_count == keyframes_to_find) yading@11: break; // all terminated, iteration done yading@11: } yading@11: continue; yading@11: } yading@11: yading@11: if (av_compare_ts(ts, ts_tb, timestamp, timebase) <= 0) { yading@11: // keyframe found before target timestamp yading@11: if (sp->pos_lo == INT64_MAX) { yading@11: // found first keyframe lower than target timestamp yading@11: (*found_lo)++; yading@11: sp->ts_lo = ts; yading@11: sp->pos_lo = pos; yading@11: } else if (sp->ts_lo < ts) { yading@11: // found a better match (closer to target timestamp) yading@11: sp->ts_lo = ts; yading@11: sp->pos_lo = pos; yading@11: } yading@11: } yading@11: if (av_compare_ts(ts, ts_tb, timestamp, timebase) >= 0) { yading@11: // keyframe found after target timestamp yading@11: if (sp->pos_hi == INT64_MAX) { yading@11: // found first keyframe higher than target timestamp yading@11: (*found_hi)++; yading@11: sp->ts_hi = ts; yading@11: sp->pos_hi = pos; yading@11: if (*found_hi >= keyframes_to_find && first_iter) { yading@11: // We found high frame for all. They may get updated yading@11: // to TS closer to target TS in later iterations (which yading@11: // will stop at start position of previous iteration). yading@11: break; yading@11: } yading@11: } else if (sp->ts_hi > ts) { yading@11: // found a better match (actually, shouldn't happen) yading@11: sp->ts_hi = ts; yading@11: sp->pos_hi = pos; yading@11: } yading@11: } yading@11: } yading@11: } yading@11: yading@11: // Clean up the parser. yading@11: ff_read_frame_flush(s); yading@11: } yading@11: yading@11: int64_t ff_gen_syncpoint_search(AVFormatContext *s, yading@11: int stream_index, yading@11: int64_t pos, yading@11: int64_t ts_min, yading@11: int64_t ts, yading@11: int64_t ts_max, yading@11: int flags) yading@11: { yading@11: AVSyncPoint *sync, *sp; yading@11: AVStream *st; yading@11: int i; yading@11: int keyframes_to_find = 0; yading@11: int64_t curpos; yading@11: int64_t step; yading@11: int found_lo = 0, found_hi = 0; yading@11: int64_t min_distance, distance; yading@11: int64_t min_pos = 0; yading@11: int first_iter = 1; yading@11: AVRational time_base; yading@11: yading@11: if (flags & AVSEEK_FLAG_BYTE) { yading@11: // for byte seeking, we have exact 1:1 "timestamps" - positions yading@11: time_base.num = 1; yading@11: time_base.den = 1; yading@11: } else { yading@11: if (stream_index >= 0) { yading@11: // we have a reference stream, which time base we use yading@11: st = s->streams[stream_index]; yading@11: time_base = st->time_base; yading@11: } else { yading@11: // no reference stream, use AV_TIME_BASE as reference time base yading@11: time_base.num = 1; yading@11: time_base.den = AV_TIME_BASE; yading@11: } yading@11: } yading@11: yading@11: // Initialize syncpoint structures for each stream. yading@11: sync = av_malloc(s->nb_streams * sizeof(AVSyncPoint)); yading@11: if (!sync) yading@11: // cannot allocate helper structure yading@11: return -1; yading@11: yading@11: for (i = 0; i < s->nb_streams; ++i) { yading@11: st = s->streams[i]; yading@11: sp = &sync[i]; yading@11: yading@11: sp->pos_lo = INT64_MAX; yading@11: sp->ts_lo = INT64_MAX; yading@11: sp->pos_hi = INT64_MAX; yading@11: sp->ts_hi = INT64_MAX; yading@11: sp->terminated = 0; yading@11: sp->first_ts = AV_NOPTS_VALUE; yading@11: sp->term_ts = ts_max; yading@11: sp->term_ts_tb = time_base; yading@11: sp->last_pos = pos; yading@11: yading@11: st->cur_dts = AV_NOPTS_VALUE; yading@11: yading@11: if (st->discard < AVDISCARD_ALL) yading@11: ++keyframes_to_find; yading@11: } yading@11: yading@11: if (!keyframes_to_find) { yading@11: // no stream active, error yading@11: av_free(sync); yading@11: return -1; yading@11: } yading@11: yading@11: // Find keyframes in all active streams with timestamp/position just before yading@11: // and just after requested timestamp/position. yading@11: step = s->pb->buffer_size; yading@11: curpos = FFMAX(pos - step / 2, 0); yading@11: for (;;) { yading@11: avio_seek(s->pb, curpos, SEEK_SET); yading@11: search_hi_lo_keyframes(s, yading@11: ts, time_base, yading@11: flags, yading@11: sync, yading@11: keyframes_to_find, yading@11: &found_lo, &found_hi, yading@11: first_iter); yading@11: if (found_lo == keyframes_to_find && found_hi == keyframes_to_find) yading@11: break; // have all keyframes we wanted yading@11: if (!curpos) yading@11: break; // cannot go back anymore yading@11: yading@11: curpos = pos - step; yading@11: if (curpos < 0) yading@11: curpos = 0; yading@11: step *= 2; yading@11: yading@11: // switch termination positions yading@11: for (i = 0; i < s->nb_streams; ++i) { yading@11: st = s->streams[i]; yading@11: st->cur_dts = AV_NOPTS_VALUE; yading@11: yading@11: sp = &sync[i]; yading@11: if (sp->first_ts != AV_NOPTS_VALUE) { yading@11: sp->term_ts = sp->first_ts; yading@11: sp->term_ts_tb = sp->first_ts_tb; yading@11: sp->first_ts = AV_NOPTS_VALUE; yading@11: } yading@11: sp->terminated = 0; yading@11: sp->last_pos = curpos; yading@11: } yading@11: first_iter = 0; yading@11: } yading@11: yading@11: // Find actual position to start decoding so that decoder synchronizes yading@11: // closest to ts and between ts_min and ts_max. yading@11: pos = INT64_MAX; yading@11: yading@11: for (i = 0; i < s->nb_streams; ++i) { yading@11: st = s->streams[i]; yading@11: if (st->discard < AVDISCARD_ALL) { yading@11: sp = &sync[i]; yading@11: min_distance = INT64_MAX; yading@11: // Find timestamp closest to requested timestamp within min/max limits. yading@11: if (sp->pos_lo != INT64_MAX yading@11: && av_compare_ts(ts_min, time_base, sp->ts_lo, st->time_base) <= 0 yading@11: && av_compare_ts(sp->ts_lo, st->time_base, ts_max, time_base) <= 0) { yading@11: // low timestamp is in range yading@11: min_distance = ts_distance(ts, time_base, sp->ts_lo, st->time_base); yading@11: min_pos = sp->pos_lo; yading@11: } yading@11: if (sp->pos_hi != INT64_MAX yading@11: && av_compare_ts(ts_min, time_base, sp->ts_hi, st->time_base) <= 0 yading@11: && av_compare_ts(sp->ts_hi, st->time_base, ts_max, time_base) <= 0) { yading@11: // high timestamp is in range, check distance yading@11: distance = ts_distance(sp->ts_hi, st->time_base, ts, time_base); yading@11: if (distance < min_distance) { yading@11: min_distance = distance; yading@11: min_pos = sp->pos_hi; yading@11: } yading@11: } yading@11: if (min_distance == INT64_MAX) { yading@11: // no timestamp is in range, cannot seek yading@11: av_free(sync); yading@11: return -1; yading@11: } yading@11: if (min_pos < pos) yading@11: pos = min_pos; yading@11: } yading@11: } yading@11: yading@11: avio_seek(s->pb, pos, SEEK_SET); yading@11: av_free(sync); yading@11: return pos; yading@11: } yading@11: yading@11: AVParserState *ff_store_parser_state(AVFormatContext *s) yading@11: { yading@11: int i; yading@11: AVStream *st; yading@11: AVParserStreamState *ss; yading@11: AVParserState *state = av_malloc(sizeof(AVParserState)); yading@11: if (!state) yading@11: return NULL; yading@11: yading@11: state->stream_states = av_malloc(sizeof(AVParserStreamState) * s->nb_streams); yading@11: if (!state->stream_states) { yading@11: av_free(state); yading@11: return NULL; yading@11: } yading@11: yading@11: state->fpos = avio_tell(s->pb); yading@11: yading@11: // copy context structures yading@11: state->packet_buffer = s->packet_buffer; yading@11: state->parse_queue = s->parse_queue; yading@11: state->raw_packet_buffer = s->raw_packet_buffer; yading@11: state->raw_packet_buffer_remaining_size = s->raw_packet_buffer_remaining_size; yading@11: yading@11: s->packet_buffer = NULL; yading@11: s->parse_queue = NULL; yading@11: s->raw_packet_buffer = NULL; yading@11: s->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE; yading@11: yading@11: // copy stream structures yading@11: state->nb_streams = s->nb_streams; yading@11: for (i = 0; i < s->nb_streams; i++) { yading@11: st = s->streams[i]; yading@11: ss = &state->stream_states[i]; yading@11: yading@11: ss->parser = st->parser; yading@11: ss->last_IP_pts = st->last_IP_pts; yading@11: ss->cur_dts = st->cur_dts; yading@11: ss->reference_dts = st->reference_dts; yading@11: ss->probe_packets = st->probe_packets; yading@11: yading@11: st->parser = NULL; yading@11: st->last_IP_pts = AV_NOPTS_VALUE; yading@11: st->cur_dts = AV_NOPTS_VALUE; yading@11: st->reference_dts = AV_NOPTS_VALUE; yading@11: st->probe_packets = MAX_PROBE_PACKETS; yading@11: } yading@11: yading@11: return state; yading@11: } yading@11: yading@11: void ff_restore_parser_state(AVFormatContext *s, AVParserState *state) yading@11: { yading@11: int i; yading@11: AVStream *st; yading@11: AVParserStreamState *ss; yading@11: ff_read_frame_flush(s); yading@11: yading@11: if (!state) yading@11: return; yading@11: yading@11: avio_seek(s->pb, state->fpos, SEEK_SET); yading@11: yading@11: // copy context structures yading@11: s->packet_buffer = state->packet_buffer; yading@11: s->parse_queue = state->parse_queue; yading@11: s->raw_packet_buffer = state->raw_packet_buffer; yading@11: s->raw_packet_buffer_remaining_size = state->raw_packet_buffer_remaining_size; yading@11: yading@11: // copy stream structures yading@11: for (i = 0; i < state->nb_streams; i++) { yading@11: st = s->streams[i]; yading@11: ss = &state->stream_states[i]; yading@11: yading@11: st->parser = ss->parser; yading@11: st->last_IP_pts = ss->last_IP_pts; yading@11: st->cur_dts = ss->cur_dts; yading@11: st->reference_dts = ss->reference_dts; yading@11: st->probe_packets = ss->probe_packets; yading@11: } yading@11: yading@11: av_free(state->stream_states); yading@11: av_free(state); yading@11: } yading@11: yading@11: static void free_packet_list(AVPacketList *pktl) yading@11: { yading@11: AVPacketList *cur; yading@11: while (pktl) { yading@11: cur = pktl; yading@11: pktl = cur->next; yading@11: av_free_packet(&cur->pkt); yading@11: av_free(cur); yading@11: } yading@11: } yading@11: yading@11: void ff_free_parser_state(AVFormatContext *s, AVParserState *state) yading@11: { yading@11: int i; yading@11: AVParserStreamState *ss; yading@11: yading@11: if (!state) yading@11: return; yading@11: yading@11: for (i = 0; i < state->nb_streams; i++) { yading@11: ss = &state->stream_states[i]; yading@11: if (ss->parser) yading@11: av_parser_close(ss->parser); yading@11: } yading@11: yading@11: free_packet_list(state->packet_buffer); yading@11: free_packet_list(state->parse_queue); yading@11: free_packet_list(state->raw_packet_buffer); yading@11: yading@11: av_free(state->stream_states); yading@11: av_free(state); yading@11: }