2 * seek utility functions for use within format handlers
4 * Copyright (c) 2009 Ivan Schreter
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/mathematics.h"
25 #include "libavutil/mem.h"
28 // NOTE: implementation should be moved here in another patch, to keep patches
32 * helper structure describing keyframe search state of one stream
35 int64_t pos_lo; ///< position of the frame with low timestamp in file or INT64_MAX if not found (yet)
36 int64_t ts_lo; ///< frame presentation timestamp or same as pos_lo for byte seeking
38 int64_t pos_hi; ///< position of the frame with high timestamp in file or INT64_MAX if not found (yet)
39 int64_t ts_hi; ///< frame presentation timestamp or same as pos_hi for byte seeking
41 int64_t last_pos; ///< last known position of a frame, for multi-frame packets
43 int64_t term_ts; ///< termination timestamp (which TS we already read)
44 AVRational term_ts_tb; ///< timebase for term_ts
45 int64_t first_ts; ///< first packet timestamp in this iteration (to fill term_ts later)
46 AVRational first_ts_tb; ///< timebase for first_ts
48 int terminated; ///< termination flag for the current iteration
52 * Compute a distance between timestamps.
54 * Distances are only comparable, if same time bases are used for computing
57 * @param ts_hi high timestamp
58 * @param tb_hi high timestamp time base
59 * @param ts_lo low timestamp
60 * @param tb_lo low timestamp time base
61 * @return representation of distance between high and low timestamps
63 static int64_t ts_distance(int64_t ts_hi,
70 hi = ts_hi * tb_hi.num * tb_lo.den;
71 lo = ts_lo * tb_lo.num * tb_hi.den;
77 * Partial search for keyframes in multiple streams.
79 * This routine searches in each stream for the next lower and the next higher
80 * timestamp compared to the given target timestamp. The search starts at the current
81 * file position and ends at the file position, where all streams have already been
82 * examined (or when all higher key frames are found in the first iteration).
84 * This routine is called iteratively with an exponential backoff to find the lower
87 * @param s format context
88 * @param timestamp target timestamp (or position, if AVSEEK_FLAG_BYTE)
89 * @param timebase time base for timestamps
90 * @param flags seeking flags
91 * @param sync array with information per stream
92 * @param keyframes_to_find count of keyframes to find in total
93 * @param found_lo ptr to the count of already found low timestamp keyframes
94 * @param found_hi ptr to the count of already found high timestamp keyframes
95 * @param first_iter flag for first iteration
97 static void search_hi_lo_keyframes(AVFormatContext *s,
102 int keyframes_to_find,
112 int terminated_count = 0;
114 int64_t pts, dts; // PTS/DTS from stream
115 int64_t ts; // PTS in stream-local time base or position for byte seeking
116 AVRational ts_tb; // Time base of the stream or 1:1 for byte seeking
119 if (av_read_frame(s, &pkt) < 0) {
120 // EOF or error, make sure high flags are set
121 for (idx = 0; idx < s->nb_streams; ++idx) {
122 if (s->streams[idx]->discard < AVDISCARD_ALL) {
124 if (sp->pos_hi == INT64_MAX) {
125 // no high frame exists for this stream
127 sp->ts_hi = INT64_MAX;
128 sp->pos_hi = INT64_MAX - 1;
135 idx = pkt.stream_index;
136 st = s->streams[idx];
137 if (st->discard >= AVDISCARD_ALL)
138 // this stream is not active, skip packet
147 if (pts == AV_NOPTS_VALUE)
148 // some formats don't provide PTS, only DTS
151 av_free_packet(&pkt);
153 // Multi-frame packets only return position for the very first frame.
154 // Other frames are read with position == -1. Therefore, we note down
155 // last known position of a frame and use it if a frame without
156 // position arrives. In this way, it's possible to seek to proper
157 // position. Additionally, for parsers not providing position at all,
158 // an approximation will be used (starting position of this iteration).
164 // Evaluate key frames with known TS (or any frames, if AVSEEK_FLAG_ANY set).
165 if (pts != AV_NOPTS_VALUE &&
166 ((flg & AV_PKT_FLAG_KEY) || (flags & AVSEEK_FLAG_ANY))) {
167 if (flags & AVSEEK_FLAG_BYTE) {
168 // for byte seeking, use position as timestamp
173 // otherwise, get stream time_base
175 ts_tb = st->time_base;
178 if (sp->first_ts == AV_NOPTS_VALUE) {
179 // Note down termination timestamp for the next iteration - when
180 // we encounter a packet with the same timestamp, we will ignore
181 // any further packets for this stream in next iteration (as they
182 // are already evaluated).
184 sp->first_ts_tb = ts_tb;
187 if (sp->term_ts != AV_NOPTS_VALUE &&
188 av_compare_ts(ts, ts_tb, sp->term_ts, sp->term_ts_tb) > 0) {
189 // past the end position from last iteration, ignore packet
190 if (!sp->terminated) {
193 if (sp->pos_hi == INT64_MAX) {
194 // no high frame exists for this stream
196 sp->ts_hi = INT64_MAX;
197 sp->pos_hi = INT64_MAX - 1;
199 if (terminated_count == keyframes_to_find)
200 break; // all terminated, iteration done
205 if (av_compare_ts(ts, ts_tb, timestamp, timebase) <= 0) {
206 // keyframe found before target timestamp
207 if (sp->pos_lo == INT64_MAX) {
208 // found first keyframe lower than target timestamp
212 } else if (sp->ts_lo < ts) {
213 // found a better match (closer to target timestamp)
218 if (av_compare_ts(ts, ts_tb, timestamp, timebase) >= 0) {
219 // keyframe found after target timestamp
220 if (sp->pos_hi == INT64_MAX) {
221 // found first keyframe higher than target timestamp
225 if (*found_hi >= keyframes_to_find && first_iter) {
226 // We found high frame for all. They may get updated
227 // to TS closer to target TS in later iterations (which
228 // will stop at start position of previous iteration).
231 } else if (sp->ts_hi > ts) {
232 // found a better match (actually, shouldn't happen)
240 // Clean up the parser.
241 ff_read_frame_flush(s);
244 int64_t ff_gen_syncpoint_search(AVFormatContext *s,
252 AVSyncPoint *sync, *sp;
255 int keyframes_to_find = 0;
258 int found_lo = 0, found_hi = 0;
259 int64_t min_distance, distance;
262 AVRational time_base;
264 if (flags & AVSEEK_FLAG_BYTE) {
265 // for byte seeking, we have exact 1:1 "timestamps" - positions
269 if (stream_index >= 0) {
270 // we have a reference stream, which time base we use
271 st = s->streams[stream_index];
272 time_base = st->time_base;
274 // no reference stream, use AV_TIME_BASE as reference time base
276 time_base.den = AV_TIME_BASE;
280 // Initialize syncpoint structures for each stream.
281 sync = av_malloc(s->nb_streams * sizeof(AVSyncPoint));
283 // cannot allocate helper structure
286 for (i = 0; i < s->nb_streams; ++i) {
290 sp->pos_lo = INT64_MAX;
291 sp->ts_lo = INT64_MAX;
292 sp->pos_hi = INT64_MAX;
293 sp->ts_hi = INT64_MAX;
295 sp->first_ts = AV_NOPTS_VALUE;
296 sp->term_ts = ts_max;
297 sp->term_ts_tb = time_base;
300 st->cur_dts = AV_NOPTS_VALUE;
302 if (st->discard < AVDISCARD_ALL)
306 if (!keyframes_to_find) {
307 // no stream active, error
312 // Find keyframes in all active streams with timestamp/position just before
313 // and just after requested timestamp/position.
314 step = s->pb->buffer_size;
315 curpos = FFMAX(pos - step / 2, 0);
317 avio_seek(s->pb, curpos, SEEK_SET);
318 search_hi_lo_keyframes(s,
323 &found_lo, &found_hi,
325 if (found_lo == keyframes_to_find && found_hi == keyframes_to_find)
326 break; // have all keyframes we wanted
328 break; // cannot go back anymore
335 // switch termination positions
336 for (i = 0; i < s->nb_streams; ++i) {
338 st->cur_dts = AV_NOPTS_VALUE;
341 if (sp->first_ts != AV_NOPTS_VALUE) {
342 sp->term_ts = sp->first_ts;
343 sp->term_ts_tb = sp->first_ts_tb;
344 sp->first_ts = AV_NOPTS_VALUE;
347 sp->last_pos = curpos;
352 // Find actual position to start decoding so that decoder synchronizes
353 // closest to ts and between ts_min and ts_max.
356 for (i = 0; i < s->nb_streams; ++i) {
358 if (st->discard < AVDISCARD_ALL) {
360 min_distance = INT64_MAX;
361 // Find timestamp closest to requested timestamp within min/max limits.
362 if (sp->pos_lo != INT64_MAX
363 && av_compare_ts(ts_min, time_base, sp->ts_lo, st->time_base) <= 0
364 && av_compare_ts(sp->ts_lo, st->time_base, ts_max, time_base) <= 0) {
365 // low timestamp is in range
366 min_distance = ts_distance(ts, time_base, sp->ts_lo, st->time_base);
367 min_pos = sp->pos_lo;
369 if (sp->pos_hi != INT64_MAX
370 && av_compare_ts(ts_min, time_base, sp->ts_hi, st->time_base) <= 0
371 && av_compare_ts(sp->ts_hi, st->time_base, ts_max, time_base) <= 0) {
372 // high timestamp is in range, check distance
373 distance = ts_distance(sp->ts_hi, st->time_base, ts, time_base);
374 if (distance < min_distance) {
375 min_distance = distance;
376 min_pos = sp->pos_hi;
379 if (min_distance == INT64_MAX) {
380 // no timestamp is in range, cannot seek
389 avio_seek(s->pb, pos, SEEK_SET);
394 AVParserState *ff_store_parser_state(AVFormatContext *s)
398 AVParserStreamState *ss;
399 AVParserState *state = av_malloc(sizeof(AVParserState));
403 state->stream_states = av_malloc(sizeof(AVParserStreamState) * s->nb_streams);
404 if (!state->stream_states) {
409 state->fpos = avio_tell(s->pb);
411 // copy context structures
412 state->packet_buffer = s->packet_buffer;
413 state->parse_queue = s->parse_queue;
414 state->raw_packet_buffer = s->raw_packet_buffer;
415 state->raw_packet_buffer_remaining_size = s->raw_packet_buffer_remaining_size;
417 s->packet_buffer = NULL;
418 s->parse_queue = NULL;
419 s->raw_packet_buffer = NULL;
420 s->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
422 // copy stream structures
423 state->nb_streams = s->nb_streams;
424 for (i = 0; i < s->nb_streams; i++) {
426 ss = &state->stream_states[i];
428 ss->parser = st->parser;
429 ss->last_IP_pts = st->last_IP_pts;
430 ss->cur_dts = st->cur_dts;
431 ss->reference_dts = st->reference_dts;
432 ss->probe_packets = st->probe_packets;
435 st->last_IP_pts = AV_NOPTS_VALUE;
436 st->cur_dts = AV_NOPTS_VALUE;
437 st->reference_dts = AV_NOPTS_VALUE;
438 st->probe_packets = MAX_PROBE_PACKETS;
444 void ff_restore_parser_state(AVFormatContext *s, AVParserState *state)
448 AVParserStreamState *ss;
449 ff_read_frame_flush(s);
454 avio_seek(s->pb, state->fpos, SEEK_SET);
456 // copy context structures
457 s->packet_buffer = state->packet_buffer;
458 s->parse_queue = state->parse_queue;
459 s->raw_packet_buffer = state->raw_packet_buffer;
460 s->raw_packet_buffer_remaining_size = state->raw_packet_buffer_remaining_size;
462 // copy stream structures
463 for (i = 0; i < state->nb_streams; i++) {
465 ss = &state->stream_states[i];
467 st->parser = ss->parser;
468 st->last_IP_pts = ss->last_IP_pts;
469 st->cur_dts = ss->cur_dts;
470 st->reference_dts = ss->reference_dts;
471 st->probe_packets = ss->probe_packets;
474 av_free(state->stream_states);
478 static void free_packet_list(AVPacketList *pktl)
484 av_free_packet(&cur->pkt);
489 void ff_free_parser_state(AVFormatContext *s, AVParserState *state)
492 AVParserStreamState *ss;
497 for (i = 0; i < state->nb_streams; i++) {
498 ss = &state->stream_states[i];
500 av_parser_close(ss->parser);
503 free_packet_list(state->packet_buffer);
504 free_packet_list(state->parse_queue);
505 free_packet_list(state->raw_packet_buffer);
507 av_free(state->stream_states);