annotate ffmpeg/libavformat/seek.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * seek utility functions for use within format handlers
yading@11 3 *
yading@11 4 * Copyright (c) 2009 Ivan Schreter
yading@11 5 *
yading@11 6 * This file is part of FFmpeg.
yading@11 7 *
yading@11 8 * FFmpeg is free software; you can redistribute it and/or
yading@11 9 * modify it under the terms of the GNU Lesser General Public
yading@11 10 * License as published by the Free Software Foundation; either
yading@11 11 * version 2.1 of the License, or (at your option) any later version.
yading@11 12 *
yading@11 13 * FFmpeg is distributed in the hope that it will be useful,
yading@11 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 16 * Lesser General Public License for more details.
yading@11 17 *
yading@11 18 * You should have received a copy of the GNU Lesser General Public
yading@11 19 * License along with FFmpeg; if not, write to the Free Software
yading@11 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 21 */
yading@11 22
yading@11 23 #include "seek.h"
yading@11 24 #include "libavutil/mathematics.h"
yading@11 25 #include "libavutil/mem.h"
yading@11 26 #include "internal.h"
yading@11 27
yading@11 28 // NOTE: implementation should be moved here in another patch, to keep patches
yading@11 29 // separated.
yading@11 30
yading@11 31 /**
yading@11 32 * helper structure describing keyframe search state of one stream
yading@11 33 */
yading@11 34 typedef struct {
yading@11 35 int64_t pos_lo; ///< position of the frame with low timestamp in file or INT64_MAX if not found (yet)
yading@11 36 int64_t ts_lo; ///< frame presentation timestamp or same as pos_lo for byte seeking
yading@11 37
yading@11 38 int64_t pos_hi; ///< position of the frame with high timestamp in file or INT64_MAX if not found (yet)
yading@11 39 int64_t ts_hi; ///< frame presentation timestamp or same as pos_hi for byte seeking
yading@11 40
yading@11 41 int64_t last_pos; ///< last known position of a frame, for multi-frame packets
yading@11 42
yading@11 43 int64_t term_ts; ///< termination timestamp (which TS we already read)
yading@11 44 AVRational term_ts_tb; ///< timebase for term_ts
yading@11 45 int64_t first_ts; ///< first packet timestamp in this iteration (to fill term_ts later)
yading@11 46 AVRational first_ts_tb; ///< timebase for first_ts
yading@11 47
yading@11 48 int terminated; ///< termination flag for the current iteration
yading@11 49 } AVSyncPoint;
yading@11 50
yading@11 51 /**
yading@11 52 * Compute a distance between timestamps.
yading@11 53 *
yading@11 54 * Distances are only comparable, if same time bases are used for computing
yading@11 55 * distances.
yading@11 56 *
yading@11 57 * @param ts_hi high timestamp
yading@11 58 * @param tb_hi high timestamp time base
yading@11 59 * @param ts_lo low timestamp
yading@11 60 * @param tb_lo low timestamp time base
yading@11 61 * @return representation of distance between high and low timestamps
yading@11 62 */
yading@11 63 static int64_t ts_distance(int64_t ts_hi,
yading@11 64 AVRational tb_hi,
yading@11 65 int64_t ts_lo,
yading@11 66 AVRational tb_lo)
yading@11 67 {
yading@11 68 int64_t hi, lo;
yading@11 69
yading@11 70 hi = ts_hi * tb_hi.num * tb_lo.den;
yading@11 71 lo = ts_lo * tb_lo.num * tb_hi.den;
yading@11 72
yading@11 73 return hi - lo;
yading@11 74 }
yading@11 75
yading@11 76 /**
yading@11 77 * Partial search for keyframes in multiple streams.
yading@11 78 *
yading@11 79 * This routine searches in each stream for the next lower and the next higher
yading@11 80 * timestamp compared to the given target timestamp. The search starts at the current
yading@11 81 * file position and ends at the file position, where all streams have already been
yading@11 82 * examined (or when all higher key frames are found in the first iteration).
yading@11 83 *
yading@11 84 * This routine is called iteratively with an exponential backoff to find the lower
yading@11 85 * timestamp.
yading@11 86 *
yading@11 87 * @param s format context
yading@11 88 * @param timestamp target timestamp (or position, if AVSEEK_FLAG_BYTE)
yading@11 89 * @param timebase time base for timestamps
yading@11 90 * @param flags seeking flags
yading@11 91 * @param sync array with information per stream
yading@11 92 * @param keyframes_to_find count of keyframes to find in total
yading@11 93 * @param found_lo ptr to the count of already found low timestamp keyframes
yading@11 94 * @param found_hi ptr to the count of already found high timestamp keyframes
yading@11 95 * @param first_iter flag for first iteration
yading@11 96 */
yading@11 97 static void search_hi_lo_keyframes(AVFormatContext *s,
yading@11 98 int64_t timestamp,
yading@11 99 AVRational timebase,
yading@11 100 int flags,
yading@11 101 AVSyncPoint *sync,
yading@11 102 int keyframes_to_find,
yading@11 103 int *found_lo,
yading@11 104 int *found_hi,
yading@11 105 int first_iter)
yading@11 106 {
yading@11 107 AVPacket pkt;
yading@11 108 AVSyncPoint *sp;
yading@11 109 AVStream *st;
yading@11 110 int idx;
yading@11 111 int flg;
yading@11 112 int terminated_count = 0;
yading@11 113 int64_t pos;
yading@11 114 int64_t pts, dts; // PTS/DTS from stream
yading@11 115 int64_t ts; // PTS in stream-local time base or position for byte seeking
yading@11 116 AVRational ts_tb; // Time base of the stream or 1:1 for byte seeking
yading@11 117
yading@11 118 for (;;) {
yading@11 119 if (av_read_frame(s, &pkt) < 0) {
yading@11 120 // EOF or error, make sure high flags are set
yading@11 121 for (idx = 0; idx < s->nb_streams; ++idx) {
yading@11 122 if (s->streams[idx]->discard < AVDISCARD_ALL) {
yading@11 123 sp = &sync[idx];
yading@11 124 if (sp->pos_hi == INT64_MAX) {
yading@11 125 // no high frame exists for this stream
yading@11 126 (*found_hi)++;
yading@11 127 sp->ts_hi = INT64_MAX;
yading@11 128 sp->pos_hi = INT64_MAX - 1;
yading@11 129 }
yading@11 130 }
yading@11 131 }
yading@11 132 break;
yading@11 133 }
yading@11 134
yading@11 135 idx = pkt.stream_index;
yading@11 136 st = s->streams[idx];
yading@11 137 if (st->discard >= AVDISCARD_ALL)
yading@11 138 // this stream is not active, skip packet
yading@11 139 continue;
yading@11 140
yading@11 141 sp = &sync[idx];
yading@11 142
yading@11 143 flg = pkt.flags;
yading@11 144 pos = pkt.pos;
yading@11 145 pts = pkt.pts;
yading@11 146 dts = pkt.dts;
yading@11 147 if (pts == AV_NOPTS_VALUE)
yading@11 148 // some formats don't provide PTS, only DTS
yading@11 149 pts = dts;
yading@11 150
yading@11 151 av_free_packet(&pkt);
yading@11 152
yading@11 153 // Multi-frame packets only return position for the very first frame.
yading@11 154 // Other frames are read with position == -1. Therefore, we note down
yading@11 155 // last known position of a frame and use it if a frame without
yading@11 156 // position arrives. In this way, it's possible to seek to proper
yading@11 157 // position. Additionally, for parsers not providing position at all,
yading@11 158 // an approximation will be used (starting position of this iteration).
yading@11 159 if (pos < 0)
yading@11 160 pos = sp->last_pos;
yading@11 161 else
yading@11 162 sp->last_pos = pos;
yading@11 163
yading@11 164 // Evaluate key frames with known TS (or any frames, if AVSEEK_FLAG_ANY set).
yading@11 165 if (pts != AV_NOPTS_VALUE &&
yading@11 166 ((flg & AV_PKT_FLAG_KEY) || (flags & AVSEEK_FLAG_ANY))) {
yading@11 167 if (flags & AVSEEK_FLAG_BYTE) {
yading@11 168 // for byte seeking, use position as timestamp
yading@11 169 ts = pos;
yading@11 170 ts_tb.num = 1;
yading@11 171 ts_tb.den = 1;
yading@11 172 } else {
yading@11 173 // otherwise, get stream time_base
yading@11 174 ts = pts;
yading@11 175 ts_tb = st->time_base;
yading@11 176 }
yading@11 177
yading@11 178 if (sp->first_ts == AV_NOPTS_VALUE) {
yading@11 179 // Note down termination timestamp for the next iteration - when
yading@11 180 // we encounter a packet with the same timestamp, we will ignore
yading@11 181 // any further packets for this stream in next iteration (as they
yading@11 182 // are already evaluated).
yading@11 183 sp->first_ts = ts;
yading@11 184 sp->first_ts_tb = ts_tb;
yading@11 185 }
yading@11 186
yading@11 187 if (sp->term_ts != AV_NOPTS_VALUE &&
yading@11 188 av_compare_ts(ts, ts_tb, sp->term_ts, sp->term_ts_tb) > 0) {
yading@11 189 // past the end position from last iteration, ignore packet
yading@11 190 if (!sp->terminated) {
yading@11 191 sp->terminated = 1;
yading@11 192 ++terminated_count;
yading@11 193 if (sp->pos_hi == INT64_MAX) {
yading@11 194 // no high frame exists for this stream
yading@11 195 (*found_hi)++;
yading@11 196 sp->ts_hi = INT64_MAX;
yading@11 197 sp->pos_hi = INT64_MAX - 1;
yading@11 198 }
yading@11 199 if (terminated_count == keyframes_to_find)
yading@11 200 break; // all terminated, iteration done
yading@11 201 }
yading@11 202 continue;
yading@11 203 }
yading@11 204
yading@11 205 if (av_compare_ts(ts, ts_tb, timestamp, timebase) <= 0) {
yading@11 206 // keyframe found before target timestamp
yading@11 207 if (sp->pos_lo == INT64_MAX) {
yading@11 208 // found first keyframe lower than target timestamp
yading@11 209 (*found_lo)++;
yading@11 210 sp->ts_lo = ts;
yading@11 211 sp->pos_lo = pos;
yading@11 212 } else if (sp->ts_lo < ts) {
yading@11 213 // found a better match (closer to target timestamp)
yading@11 214 sp->ts_lo = ts;
yading@11 215 sp->pos_lo = pos;
yading@11 216 }
yading@11 217 }
yading@11 218 if (av_compare_ts(ts, ts_tb, timestamp, timebase) >= 0) {
yading@11 219 // keyframe found after target timestamp
yading@11 220 if (sp->pos_hi == INT64_MAX) {
yading@11 221 // found first keyframe higher than target timestamp
yading@11 222 (*found_hi)++;
yading@11 223 sp->ts_hi = ts;
yading@11 224 sp->pos_hi = pos;
yading@11 225 if (*found_hi >= keyframes_to_find && first_iter) {
yading@11 226 // We found high frame for all. They may get updated
yading@11 227 // to TS closer to target TS in later iterations (which
yading@11 228 // will stop at start position of previous iteration).
yading@11 229 break;
yading@11 230 }
yading@11 231 } else if (sp->ts_hi > ts) {
yading@11 232 // found a better match (actually, shouldn't happen)
yading@11 233 sp->ts_hi = ts;
yading@11 234 sp->pos_hi = pos;
yading@11 235 }
yading@11 236 }
yading@11 237 }
yading@11 238 }
yading@11 239
yading@11 240 // Clean up the parser.
yading@11 241 ff_read_frame_flush(s);
yading@11 242 }
yading@11 243
yading@11 244 int64_t ff_gen_syncpoint_search(AVFormatContext *s,
yading@11 245 int stream_index,
yading@11 246 int64_t pos,
yading@11 247 int64_t ts_min,
yading@11 248 int64_t ts,
yading@11 249 int64_t ts_max,
yading@11 250 int flags)
yading@11 251 {
yading@11 252 AVSyncPoint *sync, *sp;
yading@11 253 AVStream *st;
yading@11 254 int i;
yading@11 255 int keyframes_to_find = 0;
yading@11 256 int64_t curpos;
yading@11 257 int64_t step;
yading@11 258 int found_lo = 0, found_hi = 0;
yading@11 259 int64_t min_distance, distance;
yading@11 260 int64_t min_pos = 0;
yading@11 261 int first_iter = 1;
yading@11 262 AVRational time_base;
yading@11 263
yading@11 264 if (flags & AVSEEK_FLAG_BYTE) {
yading@11 265 // for byte seeking, we have exact 1:1 "timestamps" - positions
yading@11 266 time_base.num = 1;
yading@11 267 time_base.den = 1;
yading@11 268 } else {
yading@11 269 if (stream_index >= 0) {
yading@11 270 // we have a reference stream, which time base we use
yading@11 271 st = s->streams[stream_index];
yading@11 272 time_base = st->time_base;
yading@11 273 } else {
yading@11 274 // no reference stream, use AV_TIME_BASE as reference time base
yading@11 275 time_base.num = 1;
yading@11 276 time_base.den = AV_TIME_BASE;
yading@11 277 }
yading@11 278 }
yading@11 279
yading@11 280 // Initialize syncpoint structures for each stream.
yading@11 281 sync = av_malloc(s->nb_streams * sizeof(AVSyncPoint));
yading@11 282 if (!sync)
yading@11 283 // cannot allocate helper structure
yading@11 284 return -1;
yading@11 285
yading@11 286 for (i = 0; i < s->nb_streams; ++i) {
yading@11 287 st = s->streams[i];
yading@11 288 sp = &sync[i];
yading@11 289
yading@11 290 sp->pos_lo = INT64_MAX;
yading@11 291 sp->ts_lo = INT64_MAX;
yading@11 292 sp->pos_hi = INT64_MAX;
yading@11 293 sp->ts_hi = INT64_MAX;
yading@11 294 sp->terminated = 0;
yading@11 295 sp->first_ts = AV_NOPTS_VALUE;
yading@11 296 sp->term_ts = ts_max;
yading@11 297 sp->term_ts_tb = time_base;
yading@11 298 sp->last_pos = pos;
yading@11 299
yading@11 300 st->cur_dts = AV_NOPTS_VALUE;
yading@11 301
yading@11 302 if (st->discard < AVDISCARD_ALL)
yading@11 303 ++keyframes_to_find;
yading@11 304 }
yading@11 305
yading@11 306 if (!keyframes_to_find) {
yading@11 307 // no stream active, error
yading@11 308 av_free(sync);
yading@11 309 return -1;
yading@11 310 }
yading@11 311
yading@11 312 // Find keyframes in all active streams with timestamp/position just before
yading@11 313 // and just after requested timestamp/position.
yading@11 314 step = s->pb->buffer_size;
yading@11 315 curpos = FFMAX(pos - step / 2, 0);
yading@11 316 for (;;) {
yading@11 317 avio_seek(s->pb, curpos, SEEK_SET);
yading@11 318 search_hi_lo_keyframes(s,
yading@11 319 ts, time_base,
yading@11 320 flags,
yading@11 321 sync,
yading@11 322 keyframes_to_find,
yading@11 323 &found_lo, &found_hi,
yading@11 324 first_iter);
yading@11 325 if (found_lo == keyframes_to_find && found_hi == keyframes_to_find)
yading@11 326 break; // have all keyframes we wanted
yading@11 327 if (!curpos)
yading@11 328 break; // cannot go back anymore
yading@11 329
yading@11 330 curpos = pos - step;
yading@11 331 if (curpos < 0)
yading@11 332 curpos = 0;
yading@11 333 step *= 2;
yading@11 334
yading@11 335 // switch termination positions
yading@11 336 for (i = 0; i < s->nb_streams; ++i) {
yading@11 337 st = s->streams[i];
yading@11 338 st->cur_dts = AV_NOPTS_VALUE;
yading@11 339
yading@11 340 sp = &sync[i];
yading@11 341 if (sp->first_ts != AV_NOPTS_VALUE) {
yading@11 342 sp->term_ts = sp->first_ts;
yading@11 343 sp->term_ts_tb = sp->first_ts_tb;
yading@11 344 sp->first_ts = AV_NOPTS_VALUE;
yading@11 345 }
yading@11 346 sp->terminated = 0;
yading@11 347 sp->last_pos = curpos;
yading@11 348 }
yading@11 349 first_iter = 0;
yading@11 350 }
yading@11 351
yading@11 352 // Find actual position to start decoding so that decoder synchronizes
yading@11 353 // closest to ts and between ts_min and ts_max.
yading@11 354 pos = INT64_MAX;
yading@11 355
yading@11 356 for (i = 0; i < s->nb_streams; ++i) {
yading@11 357 st = s->streams[i];
yading@11 358 if (st->discard < AVDISCARD_ALL) {
yading@11 359 sp = &sync[i];
yading@11 360 min_distance = INT64_MAX;
yading@11 361 // Find timestamp closest to requested timestamp within min/max limits.
yading@11 362 if (sp->pos_lo != INT64_MAX
yading@11 363 && av_compare_ts(ts_min, time_base, sp->ts_lo, st->time_base) <= 0
yading@11 364 && av_compare_ts(sp->ts_lo, st->time_base, ts_max, time_base) <= 0) {
yading@11 365 // low timestamp is in range
yading@11 366 min_distance = ts_distance(ts, time_base, sp->ts_lo, st->time_base);
yading@11 367 min_pos = sp->pos_lo;
yading@11 368 }
yading@11 369 if (sp->pos_hi != INT64_MAX
yading@11 370 && av_compare_ts(ts_min, time_base, sp->ts_hi, st->time_base) <= 0
yading@11 371 && av_compare_ts(sp->ts_hi, st->time_base, ts_max, time_base) <= 0) {
yading@11 372 // high timestamp is in range, check distance
yading@11 373 distance = ts_distance(sp->ts_hi, st->time_base, ts, time_base);
yading@11 374 if (distance < min_distance) {
yading@11 375 min_distance = distance;
yading@11 376 min_pos = sp->pos_hi;
yading@11 377 }
yading@11 378 }
yading@11 379 if (min_distance == INT64_MAX) {
yading@11 380 // no timestamp is in range, cannot seek
yading@11 381 av_free(sync);
yading@11 382 return -1;
yading@11 383 }
yading@11 384 if (min_pos < pos)
yading@11 385 pos = min_pos;
yading@11 386 }
yading@11 387 }
yading@11 388
yading@11 389 avio_seek(s->pb, pos, SEEK_SET);
yading@11 390 av_free(sync);
yading@11 391 return pos;
yading@11 392 }
yading@11 393
yading@11 394 AVParserState *ff_store_parser_state(AVFormatContext *s)
yading@11 395 {
yading@11 396 int i;
yading@11 397 AVStream *st;
yading@11 398 AVParserStreamState *ss;
yading@11 399 AVParserState *state = av_malloc(sizeof(AVParserState));
yading@11 400 if (!state)
yading@11 401 return NULL;
yading@11 402
yading@11 403 state->stream_states = av_malloc(sizeof(AVParserStreamState) * s->nb_streams);
yading@11 404 if (!state->stream_states) {
yading@11 405 av_free(state);
yading@11 406 return NULL;
yading@11 407 }
yading@11 408
yading@11 409 state->fpos = avio_tell(s->pb);
yading@11 410
yading@11 411 // copy context structures
yading@11 412 state->packet_buffer = s->packet_buffer;
yading@11 413 state->parse_queue = s->parse_queue;
yading@11 414 state->raw_packet_buffer = s->raw_packet_buffer;
yading@11 415 state->raw_packet_buffer_remaining_size = s->raw_packet_buffer_remaining_size;
yading@11 416
yading@11 417 s->packet_buffer = NULL;
yading@11 418 s->parse_queue = NULL;
yading@11 419 s->raw_packet_buffer = NULL;
yading@11 420 s->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
yading@11 421
yading@11 422 // copy stream structures
yading@11 423 state->nb_streams = s->nb_streams;
yading@11 424 for (i = 0; i < s->nb_streams; i++) {
yading@11 425 st = s->streams[i];
yading@11 426 ss = &state->stream_states[i];
yading@11 427
yading@11 428 ss->parser = st->parser;
yading@11 429 ss->last_IP_pts = st->last_IP_pts;
yading@11 430 ss->cur_dts = st->cur_dts;
yading@11 431 ss->reference_dts = st->reference_dts;
yading@11 432 ss->probe_packets = st->probe_packets;
yading@11 433
yading@11 434 st->parser = NULL;
yading@11 435 st->last_IP_pts = AV_NOPTS_VALUE;
yading@11 436 st->cur_dts = AV_NOPTS_VALUE;
yading@11 437 st->reference_dts = AV_NOPTS_VALUE;
yading@11 438 st->probe_packets = MAX_PROBE_PACKETS;
yading@11 439 }
yading@11 440
yading@11 441 return state;
yading@11 442 }
yading@11 443
yading@11 444 void ff_restore_parser_state(AVFormatContext *s, AVParserState *state)
yading@11 445 {
yading@11 446 int i;
yading@11 447 AVStream *st;
yading@11 448 AVParserStreamState *ss;
yading@11 449 ff_read_frame_flush(s);
yading@11 450
yading@11 451 if (!state)
yading@11 452 return;
yading@11 453
yading@11 454 avio_seek(s->pb, state->fpos, SEEK_SET);
yading@11 455
yading@11 456 // copy context structures
yading@11 457 s->packet_buffer = state->packet_buffer;
yading@11 458 s->parse_queue = state->parse_queue;
yading@11 459 s->raw_packet_buffer = state->raw_packet_buffer;
yading@11 460 s->raw_packet_buffer_remaining_size = state->raw_packet_buffer_remaining_size;
yading@11 461
yading@11 462 // copy stream structures
yading@11 463 for (i = 0; i < state->nb_streams; i++) {
yading@11 464 st = s->streams[i];
yading@11 465 ss = &state->stream_states[i];
yading@11 466
yading@11 467 st->parser = ss->parser;
yading@11 468 st->last_IP_pts = ss->last_IP_pts;
yading@11 469 st->cur_dts = ss->cur_dts;
yading@11 470 st->reference_dts = ss->reference_dts;
yading@11 471 st->probe_packets = ss->probe_packets;
yading@11 472 }
yading@11 473
yading@11 474 av_free(state->stream_states);
yading@11 475 av_free(state);
yading@11 476 }
yading@11 477
yading@11 478 static void free_packet_list(AVPacketList *pktl)
yading@11 479 {
yading@11 480 AVPacketList *cur;
yading@11 481 while (pktl) {
yading@11 482 cur = pktl;
yading@11 483 pktl = cur->next;
yading@11 484 av_free_packet(&cur->pkt);
yading@11 485 av_free(cur);
yading@11 486 }
yading@11 487 }
yading@11 488
yading@11 489 void ff_free_parser_state(AVFormatContext *s, AVParserState *state)
yading@11 490 {
yading@11 491 int i;
yading@11 492 AVParserStreamState *ss;
yading@11 493
yading@11 494 if (!state)
yading@11 495 return;
yading@11 496
yading@11 497 for (i = 0; i < state->nb_streams; i++) {
yading@11 498 ss = &state->stream_states[i];
yading@11 499 if (ss->parser)
yading@11 500 av_parser_close(ss->parser);
yading@11 501 }
yading@11 502
yading@11 503 free_packet_list(state->packet_buffer);
yading@11 504 free_packet_list(state->parse_queue);
yading@11 505 free_packet_list(state->raw_packet_buffer);
yading@11 506
yading@11 507 av_free(state->stream_states);
yading@11 508 av_free(state);
yading@11 509 }