diff mbox series

[FFmpeg-devel,2/3] fftools/ffmpeg: move filter/encoder PTS computation to a separate function.

Message ID 20200625183518.1217406-2-george@nsup.org
State New
Headers show
Series [FFmpeg-devel,1/3] lavfi/buffersink: add av_buffersink_get_pts(). | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Nicolas George June 25, 2020, 6:35 p.m. UTC
Signed-off-by: Nicolas George <george@nsup.org>
---
 fftools/ffmpeg.c | 45 +++++++++++++++++++++++++++------------------
 1 file changed, 27 insertions(+), 18 deletions(-)

Comments

Michael Niedermayer June 26, 2020, 7:35 p.m. UTC | #1
On Thu, Jun 25, 2020 at 08:35:17PM +0200, Nicolas George wrote:
> Signed-off-by: Nicolas George <george@nsup.org>
> ---
>  fftools/ffmpeg.c | 45 +++++++++++++++++++++++++++------------------
>  1 file changed, 27 insertions(+), 18 deletions(-)

LGTM

thx

[...]
diff mbox series

Patch

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 2e9448ea2b..3919f2ab62 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1404,6 +1404,32 @@  static void finish_output_stream(OutputStream *ost)
     }
 }
 
+static double compute_encoder_pts_from_filter_pts(AVFilterContext *filter, OutputFile *of,
+                                                  AVCodecContext *enc, int64_t *filtered_frame_pts)
+{
+    double float_pts = AV_NOPTS_VALUE;
+
+    if (*filtered_frame_pts != AV_NOPTS_VALUE) {
+        int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
+        AVRational filter_tb = av_buffersink_get_time_base(filter);
+        AVRational tb = enc->time_base;
+        int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
+
+        tb.den <<= extra_bits;
+        float_pts =
+            av_rescale_q(*filtered_frame_pts, filter_tb, tb) -
+            av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
+        float_pts /= 1 << extra_bits;
+        // avoid exact midoints to reduce the chance of rounding differences, this can be removed in case the fps code is changed to work with integers
+        float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
+
+        *filtered_frame_pts =
+            av_rescale_q(*filtered_frame_pts, filter_tb, enc->time_base) -
+            av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);
+    }
+    return float_pts;
+}
+
 /**
  * Get and encode new output from any of the filtergraphs, without causing
  * activity.
@@ -1460,24 +1486,7 @@  static int reap_filters(int flush)
                 av_frame_unref(filtered_frame);
                 continue;
             }
-            if (filtered_frame->pts != AV_NOPTS_VALUE) {
-                int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
-                AVRational filter_tb = av_buffersink_get_time_base(filter);
-                AVRational tb = enc->time_base;
-                int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
-
-                tb.den <<= extra_bits;
-                float_pts =
-                    av_rescale_q(filtered_frame->pts, filter_tb, tb) -
-                    av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
-                float_pts /= 1 << extra_bits;
-                // avoid exact midoints to reduce the chance of rounding differences, this can be removed in case the fps code is changed to work with integers
-                float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
-
-                filtered_frame->pts =
-                    av_rescale_q(filtered_frame->pts, filter_tb, enc->time_base) -
-                    av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);
-            }
+            float_pts = compute_encoder_pts_from_filter_pts(filter, of, enc, &filtered_frame->pts);
 
             switch (av_buffersink_get_type(filter)) {
             case AVMEDIA_TYPE_VIDEO: