FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/seek.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 323 422 76.5%
Functions: 17 22 77.3%
Branches: 183 286 64.0%

Line Branch Exec Source
1 /*
2 * Seeking and index-related functions
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdint.h>
23
24 #include "libavutil/avassert.h"
25 #include "libavutil/mathematics.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/timestamp.h"
28
29 #include "libavcodec/avcodec.h"
30
31 #include "avformat.h"
32 #include "avformat_internal.h"
33 #include "avio_internal.h"
34 #include "demux.h"
35 #include "internal.h"
36
37 1261 void avpriv_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
38 {
39
2/2
✓ Branch 0 taken 1565 times.
✓ Branch 1 taken 1261 times.
2826 for (unsigned i = 0; i < s->nb_streams; i++) {
40 1565 AVStream *const st = s->streams[i];
41 1565 FFStream *const sti = ffstream(st);
42
43 1565 sti->cur_dts =
44 1565 av_rescale(timestamp,
45 1565 st->time_base.den * (int64_t) ref_st->time_base.num,
46 1565 st->time_base.num * (int64_t) ref_st->time_base.den);
47 }
48 1261 }
49
50 245886 void ff_reduce_index(AVFormatContext *s, int stream_index)
51 {
52 245886 AVStream *const st = s->streams[stream_index];
53 245886 FFStream *const sti = ffstream(st);
54 245886 unsigned int max_entries = s->max_index_size / sizeof(AVIndexEntry);
55
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245886 times.
245886 if ((unsigned) sti->nb_index_entries >= max_entries) {
57 int i;
58 for (i = 0; 2 * i < sti->nb_index_entries; i++)
59 sti->index_entries[i] = sti->index_entries[2 * i];
60 sti->nb_index_entries = i;
61 }
62 245886 }
63
64 302066 int ff_add_index_entry(AVIndexEntry **index_entries,
65 int *nb_index_entries,
66 unsigned int *index_entries_allocated_size,
67 int64_t pos, int64_t timestamp,
68 int size, int distance, int flags)
69 {
70 AVIndexEntry *entries, *ie;
71 int index;
72
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302066 times.
302066 if ((unsigned) *nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
74 return -1;
75
76
2/2
✓ Branch 0 taken 3514 times.
✓ Branch 1 taken 298552 times.
302066 if (timestamp == AV_NOPTS_VALUE)
77 3514 return AVERROR(EINVAL);
78
79
2/4
✓ Branch 0 taken 298552 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 298552 times.
298552 if (size < 0 || size > 0x3FFFFFFF)
80 return AVERROR(EINVAL);
81
82
2/2
✓ Branch 1 taken 106306 times.
✓ Branch 2 taken 192246 times.
298552 if (is_relative(timestamp)) //FIXME this maintains previous behavior but we should shift by the correct offset once known
83 106306 timestamp -= RELATIVE_TS_BASE;
84
85 298552 entries = av_fast_realloc(*index_entries,
86 index_entries_allocated_size,
87 298552 (*nb_index_entries + 1) *
88 sizeof(AVIndexEntry));
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 298552 times.
298552 if (!entries)
90 return -1;
91
92 298552 *index_entries = entries;
93
94 298552 index = ff_index_search_timestamp(*index_entries, *nb_index_entries,
95 timestamp, AVSEEK_FLAG_ANY);
96
2/2
✓ Branch 0 taken 210550 times.
✓ Branch 1 taken 88002 times.
298552 if (index < 0) {
97 210550 index = (*nb_index_entries)++;
98 210550 ie = &entries[index];
99
3/4
✓ Branch 0 taken 204704 times.
✓ Branch 1 taken 5846 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 204704 times.
210550 av_assert0(index == 0 || ie[-1].timestamp < timestamp);
100 } else {
101 88002 ie = &entries[index];
102
2/2
✓ Branch 0 taken 655 times.
✓ Branch 1 taken 87347 times.
88002 if (ie->timestamp != timestamp) {
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 655 times.
655 if (ie->timestamp <= timestamp)
104 return -1;
105 655 memmove(entries + index + 1, entries + index,
106 655 sizeof(AVIndexEntry) * (*nb_index_entries - index));
107 655 (*nb_index_entries)++;
108
4/4
✓ Branch 0 taken 86916 times.
✓ Branch 1 taken 431 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 86914 times.
87347 } else if (ie->pos == pos && distance < ie->min_distance)
109 // do not reduce the distance
110 2 distance = ie->min_distance;
111 }
112
113 298552 ie->pos = pos;
114 298552 ie->timestamp = timestamp;
115 298552 ie->min_distance = distance;
116 298552 ie->size = size;
117 298552 ie->flags = flags;
118
119 298552 return index;
120 }
121
122 301749 int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
123 int size, int distance, int flags)
124 {
125 301749 FFStream *const sti = ffstream(st);
126 301749 timestamp = ff_wrap_timestamp(st, timestamp);
127 301749 return ff_add_index_entry(&sti->index_entries, &sti->nb_index_entries,
128 &sti->index_entries_allocated_size, pos,
129 timestamp, size, distance, flags);
130 }
131
132 333602 int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
133 int64_t wanted_timestamp, int flags)
134 {
135 int a, b, m;
136 int64_t timestamp;
137
138 333602 a = -1;
139 333602 b = nb_entries;
140
141 // Optimize appending index entries at the end.
142
4/4
✓ Branch 0 taken 324774 times.
✓ Branch 1 taken 8828 times.
✓ Branch 2 taken 209566 times.
✓ Branch 3 taken 115208 times.
333602 if (b && entries[b - 1].timestamp < wanted_timestamp)
143 209566 a = b - 1;
144
145
2/2
✓ Branch 0 taken 691718 times.
✓ Branch 1 taken 333602 times.
1025320 while (b - a > 1) {
146 691718 m = (a + b) >> 1;
147
148 // Search for the next non-discarded packet.
149
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 691718 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
691718 while ((entries[m].flags & AVINDEX_DISCARD_FRAME) && m < b && m < nb_entries - 1) {
150 m++;
151 if (m == b && entries[m].timestamp >= wanted_timestamp) {
152 m = b - 1;
153 break;
154 }
155 }
156
157 691718 timestamp = entries[m].timestamp;
158
2/2
✓ Branch 0 taken 220146 times.
✓ Branch 1 taken 471572 times.
691718 if (timestamp >= wanted_timestamp)
159 220146 b = m;
160
2/2
✓ Branch 0 taken 583154 times.
✓ Branch 1 taken 108564 times.
691718 if (timestamp <= wanted_timestamp)
161 583154 a = m;
162 }
163
2/2
✓ Branch 0 taken 9338 times.
✓ Branch 1 taken 324264 times.
333602 m = (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
164
165
2/2
✓ Branch 0 taken 11067 times.
✓ Branch 1 taken 322535 times.
333602 if (!(flags & AVSEEK_FLAG_ANY))
166
4/4
✓ Branch 0 taken 10357 times.
✓ Branch 1 taken 3281 times.
✓ Branch 2 taken 9258 times.
✓ Branch 3 taken 1099 times.
13638 while (m >= 0 && m < nb_entries &&
167
2/2
✓ Branch 0 taken 2571 times.
✓ Branch 1 taken 6687 times.
9258 !(entries[m].flags & AVINDEX_KEYFRAME))
168
2/2
✓ Branch 0 taken 1623 times.
✓ Branch 1 taken 948 times.
2571 m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1;
169
170
2/2
✓ Branch 0 taken 211762 times.
✓ Branch 1 taken 121840 times.
333602 if (m == nb_entries)
171 211762 return -1;
172 121840 return m;
173 }
174
175 492 void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
176 {
177 492 int64_t pos_delta = 0;
178 492 int64_t skip = 0;
179 //We could use URLProtocol flags here but as many user applications do not use URLProtocols this would be unreliable
180 492 const char *proto = avio_find_protocol_name(s->url);
181 FFIOContext *ctx;
182
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 492 times.
492 av_assert0(time_tolerance >= 0);
184
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 492 times.
492 if (!proto) {
186 av_log(s, AV_LOG_INFO,
187 "Protocol name not provided, cannot determine if input is local or "
188 "a network protocol, buffers and access patterns cannot be configured "
189 "optimally without knowing the protocol\n");
190 }
191
192
2/8
✓ Branch 0 taken 492 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 492 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
492 if (proto && !(strcmp(proto, "file") && strcmp(proto, "pipe") && strcmp(proto, "cache")))
193 492 return;
194
195 for (unsigned ist1 = 0; ist1 < s->nb_streams; ist1++) {
196 AVStream *const st1 = s->streams[ist1];
197 FFStream *const sti1 = ffstream(st1);
198 for (unsigned ist2 = 0; ist2 < s->nb_streams; ist2++) {
199 AVStream *const st2 = s->streams[ist2];
200 FFStream *const sti2 = ffstream(st2);
201
202 if (ist1 == ist2)
203 continue;
204
205 for (int i1 = 0, i2 = 0; i1 < sti1->nb_index_entries; i1++) {
206 const AVIndexEntry *const e1 = &sti1->index_entries[i1];
207 int64_t e1_pts = av_rescale_q(e1->timestamp, st1->time_base, AV_TIME_BASE_Q);
208
209 if (e1->size < (1 << 23))
210 skip = FFMAX(skip, e1->size);
211
212 for (; i2 < sti2->nb_index_entries; i2++) {
213 const AVIndexEntry *const e2 = &sti2->index_entries[i2];
214 int64_t e2_pts = av_rescale_q(e2->timestamp, st2->time_base, AV_TIME_BASE_Q);
215 int64_t cur_delta;
216 if (e2_pts < e1_pts || e2_pts - (uint64_t)e1_pts < time_tolerance)
217 continue;
218 cur_delta = FFABS(e1->pos - e2->pos);
219 if (cur_delta < (1 << 23))
220 pos_delta = FFMAX(pos_delta, cur_delta);
221 break;
222 }
223 }
224 }
225 }
226
227 pos_delta *= 2;
228 ctx = ffiocontext(s->pb);
229 /* XXX This could be adjusted depending on protocol*/
230 if (s->pb->buffer_size < pos_delta) {
231 av_log(s, AV_LOG_VERBOSE, "Reconfiguring buffers to size %"PRId64"\n", pos_delta);
232
233 /* realloc the buffer and the original data will be retained */
234 if (ffio_realloc_buf(s->pb, pos_delta)) {
235 av_log(s, AV_LOG_ERROR, "Realloc buffer fail.\n");
236 return;
237 }
238
239 ctx->short_seek_threshold = FFMAX(ctx->short_seek_threshold, pos_delta/2);
240 }
241
242 ctx->short_seek_threshold = FFMAX(ctx->short_seek_threshold, skip);
243 }
244
245 34970 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)
246 {
247 34970 const FFStream *const sti = ffstream(st);
248 34970 return ff_index_search_timestamp(sti->index_entries, sti->nb_index_entries,
249 wanted_timestamp, flags);
250 }
251
252 int avformat_index_get_entries_count(const AVStream *st)
253 {
254 return cffstream(st)->nb_index_entries;
255 }
256
257 const AVIndexEntry *avformat_index_get_entry(AVStream *st, int idx)
258 {
259 const FFStream *const sti = ffstream(st);
260 if (idx < 0 || idx >= sti->nb_index_entries)
261 return NULL;
262
263 return &sti->index_entries[idx];
264 }
265
266 const AVIndexEntry *avformat_index_get_entry_from_timestamp(AVStream *st,
267 int64_t wanted_timestamp,
268 int flags)
269 {
270 const FFStream *const sti = ffstream(st);
271 int idx = ff_index_search_timestamp(sti->index_entries,
272 sti->nb_index_entries,
273 wanted_timestamp, flags);
274
275 if (idx < 0)
276 return NULL;
277
278 return &sti->index_entries[idx];
279 }
280
281 818 static int64_t read_timestamp(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit,
282 int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ))
283 {
284 818 int64_t ts = read_timestamp(s, stream_index, ppos, pos_limit);
285
1/2
✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
818 if (stream_index >= 0)
286 818 ts = ff_wrap_timestamp(s->streams[stream_index], ts);
287 818 return ts;
288 }
289
290 206 int ff_seek_frame_binary(AVFormatContext *s, int stream_index,
291 int64_t target_ts, int flags)
292 {
293 206 const FFInputFormat *const avif = ffifmt(s->iformat);
294 206 int64_t pos_min = 0, pos_max = 0, pos, pos_limit;
295 int64_t ts_min, ts_max, ts;
296 int index;
297 int64_t ret;
298 AVStream *st;
299 FFStream *sti;
300
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 206 times.
206 if (stream_index < 0)
302 return -1;
303
304 206 av_log(s, AV_LOG_TRACE, "read_seek: %d %s\n", stream_index, av_ts2str(target_ts));
305
306 206 ts_max =
307 206 ts_min = AV_NOPTS_VALUE;
308 206 pos_limit = -1; // GCC falsely says it may be uninitialized.
309
310 206 st = s->streams[stream_index];
311 206 sti = ffstream(st);
312
2/2
✓ Branch 0 taken 197 times.
✓ Branch 1 taken 9 times.
206 if (sti->index_entries) {
313 const AVIndexEntry *e;
314
315 /* FIXME: Whole function must be checked for non-keyframe entries in
316 * index case, especially read_timestamp(). */
317 197 index = av_index_search_timestamp(st, target_ts,
318 flags | AVSEEK_FLAG_BACKWARD);
319 197 index = FFMAX(index, 0);
320 197 e = &sti->index_entries[index];
321
322
3/4
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 71 times.
197 if (e->timestamp <= target_ts || e->pos == e->min_distance) {
323 126 pos_min = e->pos;
324 126 ts_min = e->timestamp;
325 126 av_log(s, AV_LOG_TRACE, "using cached pos_min=0x%"PRIx64" dts_min=%s\n",
326 126 pos_min, av_ts2str(ts_min));
327 } else {
328 av_assert1(index == 0);
329 }
330
331 197 index = av_index_search_timestamp(st, target_ts,
332 flags & ~AVSEEK_FLAG_BACKWARD);
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
197 av_assert0(index < sti->nb_index_entries);
334
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 73 times.
197 if (index >= 0) {
335 124 e = &sti->index_entries[index];
336 av_assert1(e->timestamp >= target_ts);
337 124 pos_max = e->pos;
338 124 ts_max = e->timestamp;
339 124 pos_limit = pos_max - e->min_distance;
340 124 av_log(s, AV_LOG_TRACE, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64
341 124 " dts_max=%s\n", pos_max, pos_limit, av_ts2str(ts_max));
342 }
343 }
344
345 206 pos = ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit,
346 206 ts_min, ts_max, flags, &ts, avif->read_timestamp);
347
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 204 times.
206 if (pos < 0)
348 2 return -1;
349
350 /* do the seek */
351
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 204 times.
204 if ((ret = avio_seek(s->pb, pos, SEEK_SET)) < 0)
352 return ret;
353
354 204 ff_read_frame_flush(s);
355 204 avpriv_update_cur_dts(s, st, ts);
356
357 204 return 0;
358 }
359
360 81 int ff_find_last_ts(AVFormatContext *s, int stream_index, int64_t *ts, int64_t *pos,
361 int64_t (*read_timestamp_func)(struct AVFormatContext *, int , int64_t *, int64_t ))
362 {
363 81 int64_t step = 1024;
364 int64_t limit, ts_max;
365 81 int64_t filesize = avio_size(s->pb);
366 81 int64_t pos_max = filesize - 1;
367 do {
368 353 limit = pos_max;
369 353 pos_max = FFMAX(0, (pos_max) - step);
370 353 ts_max = read_timestamp(s, stream_index,
371 &pos_max, limit, read_timestamp_func);
372 353 step += step;
373
3/4
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 81 times.
✓ Branch 2 taken 272 times.
✗ Branch 3 not taken.
353 } while (ts_max == AV_NOPTS_VALUE && 2*limit > step);
374
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 if (ts_max == AV_NOPTS_VALUE)
375 return -1;
376
377 61 for (;;) {
378 142 int64_t tmp_pos = pos_max + 1;
379 142 int64_t tmp_ts = read_timestamp(s, stream_index,
380 &tmp_pos, INT64_MAX, read_timestamp_func);
381
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 61 times.
142 if (tmp_ts == AV_NOPTS_VALUE)
382 81 break;
383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 av_assert0(tmp_pos > pos_max);
384 61 ts_max = tmp_ts;
385 61 pos_max = tmp_pos;
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (tmp_pos >= filesize)
387 break;
388 }
389
390
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 if (ts)
391 81 *ts = ts_max;
392
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 if (pos)
393 81 *pos = pos_max;
394
395 81 return 0;
396 }
397
398 206 int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts,
399 int64_t pos_min, int64_t pos_max, int64_t pos_limit,
400 int64_t ts_min, int64_t ts_max,
401 int flags, int64_t *ts_ret,
402 int64_t (*read_timestamp_func)(struct AVFormatContext *,
403 int, int64_t *, int64_t))
404 {
405 206 FFFormatContext *const si = ffformatcontext(s);
406 int64_t pos, ts;
407 int64_t start_pos;
408 int no_change;
409 int ret;
410
411 206 av_log(s, AV_LOG_TRACE, "gen_seek: %d %s\n", stream_index, av_ts2str(target_ts));
412
413
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 126 times.
206 if (ts_min == AV_NOPTS_VALUE) {
414 80 pos_min = si->data_offset;
415 80 ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp_func);
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (ts_min == AV_NOPTS_VALUE)
417 return -1;
418 }
419
420
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 135 times.
206 if (ts_min >= target_ts) {
421 71 *ts_ret = ts_min;
422 71 return pos_min;
423 }
424
425
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 54 times.
135 if (ts_max == AV_NOPTS_VALUE) {
426
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 81 times.
81 if ((ret = ff_find_last_ts(s, stream_index, &ts_max, &pos_max, read_timestamp_func)) < 0)
427 return ret;
428 81 pos_limit = pos_max;
429 }
430
431
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 64 times.
135 if (ts_max <= target_ts) {
432 71 *ts_ret = ts_max;
433 71 return pos_max;
434 }
435
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 av_assert0(ts_min < ts_max);
437
438 64 no_change = 0;
439
2/2
✓ Branch 0 taken 243 times.
✓ Branch 1 taken 62 times.
305 while (pos_min < pos_limit) {
440 243 av_log(s, AV_LOG_TRACE,
441 "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%s dts_max=%s\n",
442 243 pos_min, pos_max, av_ts2str(ts_min), av_ts2str(ts_max));
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 243 times.
243 av_assert0(pos_limit <= pos_max);
444
445
2/2
✓ Branch 0 taken 141 times.
✓ Branch 1 taken 102 times.
243 if (no_change == 0) {
446 141 int64_t approximate_keyframe_distance = pos_max - pos_limit;
447 // interpolate position (better than dichotomy)
448 141 pos = av_rescale(target_ts - ts_min, pos_max - pos_min,
449 141 ts_max - ts_min) +
450 141 pos_min - approximate_keyframe_distance;
451
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 40 times.
102 } else if (no_change == 1) {
452 // bisection if interpolation did not change min / max pos last time
453 62 pos = (pos_min + pos_limit) >> 1;
454 } else {
455 /* linear search if bisection failed, can only happen if there
456 * are very few or no keyframes between min/max */
457 40 pos = pos_min;
458 }
459
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 183 times.
243 if (pos <= pos_min)
460 60 pos = pos_min + 1;
461
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183 times.
183 else if (pos > pos_limit)
462 pos = pos_limit;
463 243 start_pos = pos;
464
465 // May pass pos_limit instead of -1.
466 243 ts = read_timestamp(s, stream_index, &pos, INT64_MAX, read_timestamp_func);
467
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 83 times.
243 if (pos == pos_max)
468 160 no_change++;
469 else
470 83 no_change = 0;
471 243 av_log(s, AV_LOG_TRACE, "%"PRId64" %"PRId64" %"PRId64" / %s %s %s"
472 " target:%s limit:%"PRId64" start:%"PRId64" noc:%d\n",
473 pos_min, pos, pos_max,
474 243 av_ts2str(ts_min), av_ts2str(ts), av_ts2str(ts_max), av_ts2str(target_ts),
475 pos_limit, start_pos, no_change);
476
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 241 times.
243 if (ts == AV_NOPTS_VALUE) {
477 2 av_log(s, AV_LOG_ERROR, "read_timestamp() failed in the middle\n");
478 2 return -1;
479 }
480
2/2
✓ Branch 0 taken 199 times.
✓ Branch 1 taken 42 times.
241 if (target_ts <= ts) {
481 199 pos_limit = start_pos - 1;
482 199 pos_max = pos;
483 199 ts_max = ts;
484 }
485
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 197 times.
241 if (target_ts >= ts) {
486 44 pos_min = pos;
487 44 ts_min = ts;
488 }
489 }
490
491
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 30 times.
62 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
492
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 30 times.
62 ts = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min : ts_max;
493 #if 0
494 pos_min = pos;
495 ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp_func);
496 pos_min++;
497 ts_max = read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp_func);
498 av_log(s, AV_LOG_TRACE, "pos=0x%"PRIx64" %s<=%s<=%s\n",
499 pos, av_ts2str(ts_min), av_ts2str(target_ts), av_ts2str(ts_max));
500 #endif
501 62 *ts_ret = ts;
502 62 return pos;
503 }
504
505 static int seek_frame_byte(AVFormatContext *s, int stream_index,
506 int64_t pos, int flags)
507 {
508 FFFormatContext *const si = ffformatcontext(s);
509 int64_t pos_min, pos_max;
510
511 pos_min = si->data_offset;
512 pos_max = avio_size(s->pb) - 1;
513
514 if (pos < pos_min)
515 pos = pos_min;
516 else if (pos > pos_max)
517 pos = pos_max;
518
519 avio_seek(s->pb, pos, SEEK_SET);
520
521 s->io_repositioned = 1;
522
523 return 0;
524 }
525
526 1140 static int seek_frame_generic(AVFormatContext *s, int stream_index,
527 int64_t timestamp, int flags)
528 {
529 1140 FFFormatContext *const si = ffformatcontext(s);
530 1140 AVStream *const st = s->streams[stream_index];
531 1140 FFStream *const sti = ffstream(st);
532 const AVIndexEntry *ie;
533 int index;
534 int64_t ret;
535
536 1140 index = av_index_search_timestamp(st, timestamp, flags);
537
538
4/4
✓ Branch 0 taken 682 times.
✓ Branch 1 taken 458 times.
✓ Branch 2 taken 434 times.
✓ Branch 3 taken 248 times.
1140 if (index < 0 && sti->nb_index_entries &&
539
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 226 times.
434 timestamp < sti->index_entries[0].timestamp)
540 208 return -1;
541
542
4/4
✓ Branch 0 taken 458 times.
✓ Branch 1 taken 474 times.
✓ Branch 2 taken 161 times.
✓ Branch 3 taken 297 times.
932 if (index < 0 || index == sti->nb_index_entries - 1) {
543 635 AVPacket *const pkt = si->pkt;
544 635 int nonkey = 0;
545
546
2/2
✓ Branch 0 taken 387 times.
✓ Branch 1 taken 248 times.
635 if (sti->nb_index_entries) {
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 387 times.
387 av_assert0(sti->index_entries);
548 387 ie = &sti->index_entries[sti->nb_index_entries - 1];
549
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 354 times.
387 if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
550 33 return ret;
551 354 s->io_repositioned = 1;
552 354 avpriv_update_cur_dts(s, st, ie->timestamp);
553 } else {
554
2/2
✓ Branch 1 taken 184 times.
✓ Branch 2 taken 64 times.
248 if ((ret = avio_seek(s->pb, si->data_offset, SEEK_SET)) < 0)
555 184 return ret;
556 64 s->io_repositioned = 1;
557 }
558 418 av_packet_unref(pkt);
559 3328 for (;;) {
560 int read_status;
561 do {
562 3746 read_status = av_read_frame(s, pkt);
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3746 times.
3746 } while (read_status == AVERROR(EAGAIN));
564
2/2
✓ Branch 0 taken 365 times.
✓ Branch 1 taken 3381 times.
3746 if (read_status < 0)
565 365 break;
566
4/4
✓ Branch 0 taken 3278 times.
✓ Branch 1 taken 103 times.
✓ Branch 2 taken 486 times.
✓ Branch 3 taken 2792 times.
3381 if (stream_index == pkt->stream_index && pkt->dts > timestamp) {
567
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 433 times.
486 if (pkt->flags & AV_PKT_FLAG_KEY) {
568 53 av_packet_unref(pkt);
569 53 break;
570 }
571
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 433 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
433 if (nonkey++ > 1000 && st->codecpar->codec_id != AV_CODEC_ID_CDGRAPHICS) {
572 av_log(s, AV_LOG_ERROR,"seek_frame_generic failed as this stream seems to contain no keyframes after the target timestamp, %d non keyframes found\n", nonkey);
573 av_packet_unref(pkt);
574 break;
575 }
576 }
577 3328 av_packet_unref(pkt);
578 }
579 418 index = av_index_search_timestamp(st, timestamp, flags);
580 }
581
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 445 times.
715 if (index < 0)
582 270 return -1;
583
584 445 ff_read_frame_flush(s);
585
2/2
✓ Branch 1 taken 187 times.
✓ Branch 2 taken 258 times.
445 if (ffifmt(s->iformat)->read_seek)
586
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 187 times.
187 if (ffifmt(s->iformat)->read_seek(s, stream_index, timestamp, flags) >= 0)
587 return 0;
588 445 ie = &sti->index_entries[index];
589
2/2
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 424 times.
445 if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
590 21 return ret;
591 424 s->io_repositioned = 1;
592 424 avpriv_update_cur_dts(s, st, ie->timestamp);
593
594 424 return 0;
595 }
596
597 3314 static int seek_frame_internal(AVFormatContext *s, int stream_index,
598 int64_t timestamp, int flags)
599 {
600 AVStream *st;
601 int ret;
602
603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3314 times.
3314 if (flags & AVSEEK_FLAG_BYTE) {
604 if (s->iformat->flags & AVFMT_NO_BYTE_SEEK)
605 return -1;
606 ff_read_frame_flush(s);
607 return seek_frame_byte(s, stream_index, timestamp, flags);
608 }
609
610
2/2
✓ Branch 0 taken 1748 times.
✓ Branch 1 taken 1566 times.
3314 if (stream_index < 0) {
611 1748 stream_index = av_find_default_stream_index(s);
612
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1748 times.
1748 if (stream_index < 0)
613 return -1;
614
615 1748 st = s->streams[stream_index];
616 /* timestamp for default must be expressed in AV_TIME_BASE units */
617 1748 timestamp = av_rescale(timestamp, st->time_base.den,
618 1748 AV_TIME_BASE * (int64_t) st->time_base.num);
619 }
620
621 /* first, we try the format specific seek */
622
2/2
✓ Branch 1 taken 2788 times.
✓ Branch 2 taken 526 times.
3314 if (ffifmt(s->iformat)->read_seek) {
623 2788 ff_read_frame_flush(s);
624 2788 ret = ffifmt(s->iformat)->read_seek(s, stream_index, timestamp, flags);
625 } else
626 526 ret = -1;
627
2/2
✓ Branch 0 taken 2077 times.
✓ Branch 1 taken 1237 times.
3314 if (ret >= 0)
628 2077 return 0;
629
630
2/2
✓ Branch 1 taken 92 times.
✓ Branch 2 taken 1145 times.
1237 if (ffifmt(s->iformat)->read_timestamp &&
631
1/2
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
92 !(s->iformat->flags & AVFMT_NOBINSEARCH)) {
632 92 ff_read_frame_flush(s);
633 92 return ff_seek_frame_binary(s, stream_index, timestamp, flags);
634
2/2
✓ Branch 0 taken 1140 times.
✓ Branch 1 taken 5 times.
1145 } else if (!(s->iformat->flags & AVFMT_NOGENSEARCH)) {
635 1140 ff_read_frame_flush(s);
636 1140 return seek_frame_generic(s, stream_index, timestamp, flags);
637 } else
638 5 return -1;
639 }
640
641 3314 int av_seek_frame(AVFormatContext *s, int stream_index,
642 int64_t timestamp, int flags)
643 {
644 int ret;
645
646
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 3314 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3314 if (ffifmt(s->iformat)->read_seek2 && !ffifmt(s->iformat)->read_seek) {
647 int64_t min_ts = INT64_MIN, max_ts = INT64_MAX;
648 if ((flags & AVSEEK_FLAG_BACKWARD))
649 max_ts = timestamp;
650 else
651 min_ts = timestamp;
652 return avformat_seek_file(s, stream_index, min_ts, timestamp, max_ts,
653 flags & ~AVSEEK_FLAG_BACKWARD);
654 }
655
656 3314 ret = seek_frame_internal(s, stream_index, timestamp, flags);
657
658
2/2
✓ Branch 0 taken 2592 times.
✓ Branch 1 taken 722 times.
3314 if (ret >= 0)
659 2592 ret = avformat_queue_attached_pictures(s);
660
661 3314 return ret;
662 }
663
664 3288 int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts,
665 int64_t ts, int64_t max_ts, int flags)
666 {
667
2/4
✓ Branch 0 taken 3288 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3288 times.
3288 if (min_ts > ts || max_ts < ts)
668 return -1;
669
2/4
✓ Branch 0 taken 3288 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3288 times.
3288 if (stream_index < -1 || stream_index >= (int)s->nb_streams)
670 return AVERROR(EINVAL);
671
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3288 times.
3288 if (s->seek2any > 0)
673 flags |= AVSEEK_FLAG_ANY;
674 3288 flags &= ~AVSEEK_FLAG_BACKWARD;
675
676
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3286 times.
3288 if (ffifmt(s->iformat)->read_seek2) {
677 int ret;
678 2 ff_read_frame_flush(s);
679
680
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (stream_index == -1 && s->nb_streams == 1) {
681 2 AVRational time_base = s->streams[0]->time_base;
682 2 ts = av_rescale_q(ts, AV_TIME_BASE_Q, time_base);
683 2 min_ts = av_rescale_rnd(min_ts, time_base.den,
684 2 time_base.num * (int64_t)AV_TIME_BASE,
685 AV_ROUND_UP | AV_ROUND_PASS_MINMAX);
686 2 max_ts = av_rescale_rnd(max_ts, time_base.den,
687 2 time_base.num * (int64_t)AV_TIME_BASE,
688 AV_ROUND_DOWN | AV_ROUND_PASS_MINMAX);
689 2 stream_index = 0;
690 }
691
692 2 ret = ffifmt(s->iformat)->read_seek2(s, stream_index, min_ts,
693 ts, max_ts, flags);
694
695
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (ret >= 0)
696 1 ret = avformat_queue_attached_pictures(s);
697 2 return ret;
698 }
699
700 3286 if (ffifmt(s->iformat)->read_timestamp) {
701 // try to seek via read_timestamp()
702 }
703
704 // Fall back on old API if new is not implemented but old is.
705 // Note the old API has somewhat different semantics.
706 3286 if (ffifmt(s->iformat)->read_seek || 1) {
707 3286 int dir = (ts - (uint64_t)min_ts > (uint64_t)max_ts - ts ? AVSEEK_FLAG_BACKWARD : 0);
708 3286 int ret = av_seek_frame(s, stream_index, ts, flags | dir);
709
5/6
✓ Branch 0 taken 722 times.
✓ Branch 1 taken 2564 times.
✓ Branch 2 taken 358 times.
✓ Branch 3 taken 364 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 358 times.
3286 if (ret < 0 && ts != min_ts && max_ts != ts) {
710 ret = av_seek_frame(s, stream_index, dir ? max_ts : min_ts, flags | dir);
711 if (ret >= 0)
712 ret = av_seek_frame(s, stream_index, ts, flags | (dir^AVSEEK_FLAG_BACKWARD));
713 }
714 3286 return ret;
715 }
716
717 // try some generic seek like seek_frame_generic() but with new ts semantics
718 return -1; //unreachable
719 }
720
721 /** Flush the frame reader. */
722 4784 void ff_read_frame_flush(AVFormatContext *s)
723 {
724 4784 FFFormatContext *const si = ffformatcontext(s);
725
726 4784 ff_flush_packet_queue(s);
727
728 /* Reset read state for each stream. */
729
2/2
✓ Branch 0 taken 5530 times.
✓ Branch 1 taken 4784 times.
10314 for (unsigned i = 0; i < s->nb_streams; i++) {
730 5530 AVStream *const st = s->streams[i];
731 5530 FFStream *const sti = ffstream(st);
732
733
2/2
✓ Branch 0 taken 1162 times.
✓ Branch 1 taken 4368 times.
5530 if (sti->parser) {
734 1162 av_parser_close(sti->parser);
735 1162 sti->parser = NULL;
736 }
737 5530 sti->last_IP_pts = AV_NOPTS_VALUE;
738 5530 sti->last_dts_for_order_check = AV_NOPTS_VALUE;
739
2/2
✓ Branch 0 taken 1615 times.
✓ Branch 1 taken 3915 times.
5530 if (sti->first_dts == AV_NOPTS_VALUE)
740 1615 sti->cur_dts = RELATIVE_TS_BASE;
741 else
742 /* We set the current DTS to an unspecified origin. */
743 3915 sti->cur_dts = AV_NOPTS_VALUE;
744
745 5530 sti->probe_packets = s->max_probe_packets;
746
747
2/2
✓ Branch 0 taken 94010 times.
✓ Branch 1 taken 5530 times.
99540 for (int j = 0; j < MAX_REORDER_DELAY + 1; j++)
748 94010 sti->pts_buffer[j] = AV_NOPTS_VALUE;
749
750 #if FF_API_AVSTREAM_SIDE_DATA
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5530 times.
5530 if (si->inject_global_side_data)
752 sti->inject_global_side_data = 1;
753 #endif
754
755 5530 sti->skip_samples = 0;
756 }
757 4784 }
758
759 int avformat_flush(AVFormatContext *s)
760 {
761 ff_read_frame_flush(s);
762 return 0;
763 }
764
765 3 void ff_rescale_interval(AVRational tb_in, AVRational tb_out,
766 int64_t *min_ts, int64_t *ts, int64_t *max_ts)
767 {
768 3 *ts = av_rescale_q (* ts, tb_in, tb_out);
769 3 *min_ts = av_rescale_q_rnd(*min_ts, tb_in, tb_out,
770 AV_ROUND_UP | AV_ROUND_PASS_MINMAX);
771 3 *max_ts = av_rescale_q_rnd(*max_ts, tb_in, tb_out,
772 AV_ROUND_DOWN | AV_ROUND_PASS_MINMAX);
773 3 }
774