1 /*
2 * seek utility functions for use within format handlers
3 *
4 * Copyright (c) 2009 Ivan Schreter
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg 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.
12 *
13 * FFmpeg 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.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
27
28 // NOTE: implementation should be moved here in another patch, to keep patches
29 // separated.
30
31 /**
32 * helper structure describing keyframe search state of one stream
33 */
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
37
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
40
41 int64_t
last_pos;
///< last known position of a frame, for multi-frame packets
42
43 int64_t
term_ts;
///< termination timestamp (which TS we already read)
45 int64_t
first_ts;
///< first packet timestamp in this iteration (to fill term_ts later)
47
48 int terminated;
///< termination flag for the current iteration
50
51 /**
52 * Compute a distance between timestamps.
53 *
54 * Distances are only comparable, if same time bases are used for computing
55 * distances.
56 *
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
62 */
65 int64_t ts_lo,
67 {
68 int64_t hi, lo;
69
70 hi = ts_hi * tb_hi.
num * tb_lo.
den;
71 lo = ts_lo * tb_lo.
num * tb_hi.
den;
72
73 return hi - lo;
74 }
75
76 /**
77 * Partial search for keyframes in multiple streams.
78 *
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).
83 *
84 * This routine is called iteratively with an exponential backoff to find the lower
85 * timestamp.
86 *
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
96 */
98 int64_t timestamp,
102 int keyframes_to_find,
103 int *found_lo,
104 int *found_hi,
105 int first_iter)
106 {
110 int idx;
111 int flg;
112 int terminated_count = 0;
113 int64_t pos;
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
117
118 for (;;) {
120 // EOF or error, make sure high flags are set
123 sp = &sync[idx];
124 if (sp->
pos_hi == INT64_MAX) {
125 // no high frame exists for this stream
126 (*found_hi)++;
127 sp->
ts_hi = INT64_MAX;
128 sp->
pos_hi = INT64_MAX - 1;
129 }
130 }
131 }
132 break;
133 }
134
138 // this stream is not active, skip packet
139 continue;
140
141 sp = &sync[idx];
142
148 // some formats don't provide PTS, only DTS
149 pts = dts;
150
152
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).
159 if (pos < 0)
161 else
163
164 // Evaluate key frames with known TS (or any frames, if AVSEEK_FLAG_ANY set).
168 // for byte seeking, use position as timestamp
169 ts = pos;
172 } else {
173 // otherwise, get stream time_base
174 ts = pts;
176 }
177
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).
185 }
186
189 // past the end position from last iteration, ignore packet
192 ++terminated_count;
193 if (sp->
pos_hi == INT64_MAX) {
194 // no high frame exists for this stream
195 (*found_hi)++;
196 sp->
ts_hi = INT64_MAX;
197 sp->
pos_hi = INT64_MAX - 1;
198 }
199 if (terminated_count == keyframes_to_find)
200 break; // all terminated, iteration done
201 }
202 continue;
203 }
204
206 // keyframe found before target timestamp
207 if (sp->
pos_lo == INT64_MAX) {
208 // found first keyframe lower than target timestamp
209 (*found_lo)++;
212 }
else if (sp->
ts_lo < ts) {
213 // found a better match (closer to target timestamp)
216 }
217 }
219 // keyframe found after target timestamp
220 if (sp->
pos_hi == INT64_MAX) {
221 // found first keyframe higher than target timestamp
222 (*found_hi)++;
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).
229 break;
230 }
231 }
else if (sp->
ts_hi > ts) {
232 // found a better match (actually, shouldn't happen)
235 }
236 }
237 }
238 }
239
240 // Clean up the parser.
242 }
243
245 int stream_index,
246 int64_t pos,
247 int64_t ts_min,
248 int64_t ts,
249 int64_t ts_max,
251 {
254 int i;
255 int keyframes_to_find = 0;
256 int64_t curpos;
257 int64_t step;
258 int found_lo = 0, found_hi = 0;
260 int64_t min_pos = 0;
261 int first_iter = 1;
263
265 // for byte seeking, we have exact 1:1 "timestamps" - positions
268 } else {
269 if (stream_index >= 0) {
270 // we have a reference stream, which time base we use
273 } else {
274 // no reference stream, use AV_TIME_BASE as reference time base
277 }
278 }
279
280 // Initialize syncpoint structures for each stream.
282 if (!sync)
283 // cannot allocate helper structure
284 return -1;
285
288 sp = &sync[i];
289
291 sp->
ts_lo = INT64_MAX;
293 sp->
ts_hi = INT64_MAX;
299
301
303 ++keyframes_to_find;
304 }
305
306 if (!keyframes_to_find) {
307 // no stream active, error
309 return -1;
310 }
311
312 // Find keyframes in all active streams with timestamp/position just before
313 // and just after requested timestamp/position.
315 curpos =
FFMAX(pos - step / 2, 0);
316 for (;;) {
319 ts, time_base,
320 flags,
321 sync,
322 keyframes_to_find,
323 &found_lo, &found_hi,
324 first_iter);
325 if (found_lo == keyframes_to_find && found_hi == keyframes_to_find)
326 break; // have all keyframes we wanted
327 if (!curpos)
328 break; // cannot go back anymore
329
330 curpos = pos - step;
331 if (curpos < 0)
332 curpos = 0;
333 step *= 2;
334
335 // switch termination positions
339
340 sp = &sync[i];
345 }
348 }
349 first_iter = 0;
350 }
351
352 // Find actual position to start decoding so that decoder synchronizes
353 // closest to ts and between ts_min and ts_max.
354 pos = INT64_MAX;
355
359 sp = &sync[i];
360 min_distance = INT64_MAX;
361 // Find timestamp closest to requested timestamp within min/max limits.
362 if (sp->
pos_lo != INT64_MAX
365 // low timestamp is in range
368 }
369 if (sp->
pos_hi != INT64_MAX
372 // high timestamp is in range, check distance
374 if (distance < min_distance) {
377 }
378 }
379 if (min_distance == INT64_MAX) {
380 // no timestamp is in range, cannot seek
382 return -1;
383 }
384 if (min_pos < pos)
385 pos = min_pos;
386 }
387 }
388
391 return pos;
392 }
393
395 {
396 int i;
400 if (!state)
401 return NULL;
402
406 return NULL;
407 }
408
410
411 // copy context structures
416
421
422 // copy stream structures
427
433
439 }
440
442 }
443
445 {
446 int i;
450
451 if (!state)
452 return;
453
455
456 // copy context structures
461
462 // copy stream structures
466
472 }
473
476 }
477
479 {
481 while (pktl) {
482 cur = pktl;
486 }
487 }
488
490 {
491 int i;
493
494 if (!state)
495 return;
496
501 }
502
506
509 }