diff mbox series

[FFmpeg-devel,19/35] fftools/ffmpeg: use last filter output pts to choose next output stream

Message ID 20220616195534.5278-19-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,01/35] fftools/ffmpeg_mux: add private muxer context | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Anton Khirnov June 16, 2022, 7:55 p.m. UTC
This will be needed in the following commit that will add a new
buffering stages after encoding and bitstream filtering.
---
 fftools/ffmpeg.c     | 23 ++++++++++++++++++-----
 fftools/ffmpeg.h     |  2 ++
 fftools/ffmpeg_opt.c |  1 +
 3 files changed, 21 insertions(+), 5 deletions(-)

Comments

Michael Niedermayer June 17, 2022, 6:45 p.m. UTC | #1
On Thu, Jun 16, 2022 at 09:55:18PM +0200, Anton Khirnov wrote:
> This will be needed in the following commit that will add a new
> buffering stages after encoding and bitstream filtering.
> ---
>  fftools/ffmpeg.c     | 23 ++++++++++++++++++-----
>  fftools/ffmpeg.h     |  2 ++
>  fftools/ffmpeg_opt.c |  1 +
>  3 files changed, 21 insertions(+), 5 deletions(-)

this seems to break this:
ffmpeg -i tickets//3015/test_video -filter:a apad -f flv -vcodec flv -ar 22050 -bitexact -shortest file3015-dont-infloop.flv

after the patch ffmpeg seems not to exit and keeps increasing the output file size

input file seems to be here https://trac.ffmpeg.org/raw-attachment/ticket/3015/test_video

thx

[...]
diff mbox series

Patch

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 53ca8c7f7b..4647555ebf 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1343,6 +1343,12 @@  static int reap_filters(int flush)
                 continue;
             }
 
+            if (filtered_frame->pts != AV_NOPTS_VALUE) {
+                AVRational tb = av_buffersink_get_time_base(filter);
+                ost->last_filter_pts = av_rescale_q(filtered_frame->pts, tb,
+                                                    AV_TIME_BASE_Q);
+            }
+
             switch (av_buffersink_get_type(filter)) {
             case AVMEDIA_TYPE_VIDEO:
                 if (!ost->frame_aspect_ratio.num)
@@ -3440,13 +3446,20 @@  static OutputStream *choose_output(void)
 
     for (i = 0; i < nb_output_streams; i++) {
         OutputStream *ost = output_streams[i];
-        int64_t opts = ost->last_mux_dts == AV_NOPTS_VALUE ? INT64_MIN :
+        int64_t opts;
+
+        if (ost->filter) {
+            opts = ost->last_filter_pts == AV_NOPTS_VALUE ?
+                   INT64_MIN : ost->last_filter_pts;
+        } else {
+            opts = ost->last_mux_dts == AV_NOPTS_VALUE ? INT64_MIN :
                        av_rescale_q(ost->last_mux_dts, ost->st->time_base,
                                     AV_TIME_BASE_Q);
-        if (ost->last_mux_dts == AV_NOPTS_VALUE)
-            av_log(NULL, AV_LOG_DEBUG,
-                "cur_dts is invalid st:%d (%d) [init:%d i_done:%d finish:%d] (this is harmless if it occurs once at the start per stream)\n",
-                ost->st->index, ost->st->id, ost->initialized, ost->inputs_done, ost->finished);
+            if (ost->last_mux_dts == AV_NOPTS_VALUE)
+                av_log(NULL, AV_LOG_DEBUG,
+                    "cur_dts is invalid st:%d (%d) [init:%d i_done:%d finish:%d] (this is harmless if it occurs once at the start per stream)\n",
+                    ost->st->index, ost->st->id, ost->initialized, ost->inputs_done, ost->finished);
+        }
 
         if (!ost->initialized && !ost->inputs_done)
             return ost->unavailable ? NULL : ost;
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 2aa220da29..861f8140cf 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -468,6 +468,8 @@  typedef struct OutputStream {
     int64_t first_pts;
     /* dts of the last packet sent to the muxer */
     int64_t last_mux_dts;
+    /* pts of the last frame received from the filters, in AV_TIME_BASE_Q */
+    int64_t last_filter_pts;
     // the timebase of the packets sent to the muxer
     AVRational mux_timebase;
     AVRational enc_timebase;
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 1bcb7a3006..09a281dba3 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1662,6 +1662,7 @@  static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e
         input_streams[source_index]->st->discard = input_streams[source_index]->user_set_discard;
     }
     ost->last_mux_dts = AV_NOPTS_VALUE;
+    ost->last_filter_pts = AV_NOPTS_VALUE;
 
     return ost;
 }