diff mbox series

[FFmpeg-devel] avformat/demux: Add more retries to get more stream durations

Message ID 20240202180719.784910-1-nicolas.gaullier@cji.paris
State New
Headers show
Series [FFmpeg-devel] avformat/demux: Add more retries to get more stream durations | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Nicolas Gaullier Feb. 2, 2024, 6:07 p.m. UTC
The number of extra retries to get more than a single duration is
currently limited to 1 (see commit 77a0df4b5).
This patch raises this number of retries to 3 (amongst the overall 6 max retries).

Exemple: this sample requires 3 retries to get all durations:
http://samples.ffmpeg.org/archive/extension/ts/
mpegts+mpeg2video+ac3++mpegts_multiple_ts_packets_pmt_pid_0x50.ts

Moreover, when an mpegts file is cleany cut at a precise pts independantly
for audio and video, it results in a big gap between the last video packet
and the last audio packet.

And above the probing benefits themselves, having all durations is somewhat
recommanded for using concatdec, and even required when using
concatdec with streamcopy.

Signed-off-by: Nicolas Gaullier <nicolas.gaullier@cji.paris>
---
 libavformat/demux.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/libavformat/demux.c b/libavformat/demux.c
index 6f640b92b1..f489868d34 100644
--- a/libavformat/demux.c
+++ b/libavformat/demux.c
@@ -1742,6 +1742,7 @@  static void estimate_timings_from_bit_rate(AVFormatContext *ic)
 
 #define DURATION_MAX_READ_SIZE 250000LL
 #define DURATION_MAX_RETRY 6
+#define MORE_DURATIONS_MAX_RETRY 3
 
 /* only usable for MPEG-PS streams */
 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
@@ -1753,6 +1754,7 @@  static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
     int is_end;
     int64_t filesize, offset, duration;
     int retry = 0;
+    int retry_get_more_durations = 0;
 
     /* flush packet queue */
     ff_flush_packet_queue(ic);
@@ -1783,7 +1785,6 @@  static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
     /* XXX: may need to support wrapping */
     filesize = ic->pb ? avio_size(ic->pb) : 0;
     do {
-        is_end = found_duration;
         offset = filesize - (DURATION_MAX_READ_SIZE << retry);
         if (offset < 0)
             offset = 0;
@@ -1833,7 +1834,7 @@  static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
         }
 
         /* check if all audio/video streams have valid duration */
-        if (!is_end) {
+        if (found_duration) {
             is_end = 1;
             for (unsigned i = 0; i < ic->nb_streams; i++) {
                 const AVStream *const st = ic->streams[i];
@@ -1846,6 +1847,7 @@  static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
             }
         }
     } while (!is_end &&
+             (!found_duration || ++retry_get_more_durations <= MORE_DURATIONS_MAX_RETRY) &&
              offset &&
              ++retry <= DURATION_MAX_RETRY);