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

Nicolas George george at nsup.org
Thu Jun 25 21:35:17 EEST 2020


Signed-off-by: Nicolas George <george at nsup.org>
---
 fftools/ffmpeg.c | 45 +++++++++++++++++++++++++++------------------
 1 file changed, 27 insertions(+), 18 deletions(-)

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:
-- 
2.27.0



More information about the ffmpeg-devel mailing list