[FFmpeg-devel] [PATCH 2/7] avcodec/ratecontrol: Try to keep fps as a rational

Michael Niedermayer michael at niedermayer.cc
Tue Jun 18 16:48:21 EEST 2024


Signed-off-by: Michael Niedermayer <michael at niedermayer.cc>
---
 libavcodec/ratecontrol.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c
index 609d47faeb4..df27639ca73 100644
--- a/libavcodec/ratecontrol.c
+++ b/libavcodec/ratecontrol.c
@@ -56,20 +56,25 @@ void ff_write_pass1_stats(MpegEncContext *s)
              s->header_bits);
 }
 
-static double get_fps(AVCodecContext *avctx)
+static AVRational get_fpsQ(AVCodecContext *avctx)
 {
     if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
-        return av_q2d(avctx->framerate);
+        return avctx->framerate;
 
 FF_DISABLE_DEPRECATION_WARNINGS
-    return 1.0 / av_q2d(avctx->time_base)
 #if FF_API_TICKS_PER_FRAME
-        / FFMAX(avctx->ticks_per_frame, 1)
+    return av_div_q((AVRational){1, FFMAX(avctx->ticks_per_frame, 1)}, avctx->time_base);
+#else
+    return av_inv_q(avctx->time_base);
 #endif
-        ;
 FF_ENABLE_DEPRECATION_WARNINGS
 }
 
+static double get_fps(AVCodecContext *avctx)
+{
+    return av_q2d(get_fpsQ(avctx));
+}
+
 static inline double qp2bits(const RateControlEntry *rce, double qp)
 {
     if (qp <= 0.0) {
@@ -332,12 +337,13 @@ static int init_pass2(MpegEncContext *s)
     RateControlContext *rcc = &s->rc_context;
     AVCodecContext *a       = s->avctx;
     int i, toobig;
-    double fps             = get_fps(s->avctx);
+    AVRational fps         = get_fpsQ(s->avctx);
     double complexity[5]   = { 0 }; // approximate bits at quant=1
     uint64_t const_bits[5] = { 0 }; // quantizer independent bits
     uint64_t all_const_bits;
-    uint64_t all_available_bits = (uint64_t)(s->bit_rate *
-                                             (double)rcc->num_entries / fps);
+    uint64_t all_available_bits = av_rescale_q(s->bit_rate,
+                                               (AVRational){rcc->num_entries,1},
+                                               fps);
     double rate_factor          = 0;
     double step;
     const int filter_size = (int)(a->qblur * 4) | 1;
-- 
2.45.2



More information about the ffmpeg-devel mailing list