[FFmpeg-devel] [RFC PATCH] avformat/utils: always seek back after avformat_find_stream_info()

Aman Gupta ffmpeg at tmm1.net
Wed Apr 10 03:14:48 EEST 2019

From: Aman Gupta <aman at tmm1.net>

Previously, the initial seek position was recorded into
old_offset at the beginning of avformat_find_stream_info(),
and passed into estimate_timings(). In the case of mpegts
with a known filesize, it was further passed into
estimate_timings_from_pts() which called avio_seek(SEEK_SET)
after doing its timing related seeks. (Interestingly, this
seeked all the way back to the initial position before
the probe, rather than only back before the timing
related seeks to the end of the file).

With this commit, we pull the avio_seek() out of the mpegts
specific code-path and unconditionally seek all streams back
to their original position after probing is complete.

This effectively prevents data that is consumed during
avformat_find_stream_info() from being discarded, so packets
contained at the beginning of a file are still passed back
to the user for playback.

Signed-off-by: Aman Gupta <aman at tmm1.net>
 libavformat/utils.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/libavformat/utils.c b/libavformat/utils.c
index 9b3f0d28e6..94d166869a 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -2786,7 +2786,7 @@ static void estimate_timings_from_bit_rate(AVFormatContext *ic)
 /* only usable for MPEG-PS streams */
-static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
+static void estimate_timings_from_pts(AVFormatContext *ic)
     AVPacket pkt1, *pkt = &pkt1;
     AVStream *st;
@@ -2905,7 +2905,6 @@ static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
-    avio_seek(ic->pb, old_offset, SEEK_SET);
     for (i = 0; i < ic->nb_streams; i++) {
         int j;
@@ -2918,7 +2917,7 @@ skip_duration_calc:
-static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
+static void estimate_timings(AVFormatContext *ic)
     int64_t file_size;
@@ -2934,7 +2933,7 @@ static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
          !strcmp(ic->iformat->name, "mpegts")) &&
         file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
         /* get accurate estimate from the PTSes */
-        estimate_timings_from_pts(ic, old_offset);
+        estimate_timings_from_pts(ic);
         ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
     } else if (has_duration(ic)) {
         /* at least one component has timings - we use them for all
     if (probesize)
-        estimate_timings(ic, old_offset);
+        estimate_timings(ic);
+    if ((ic->pb->seekable & AVIO_SEEKABLE_NORMAL))
+        avio_seek(ic->pb, old_offset, SEEK_SET);
     av_opt_set(ic, "skip_clear", "0", AV_OPT_SEARCH_CHILDREN);

More information about the ffmpeg-devel mailing list