Commit dde2f1b077 for ffmpeg

commit dde2f1b0778b3dabc91b31843cc95a03f54ad2b2
Author: Michael Niedermayer <michael@niedermayer.cc>
Date:   Sat Oct 10 17:57:35 2026 +0200

    avcodec/ratecontrol: keep the wanted bits inside the int64 range in both directions

    Signed integer overflow Replicated through Modified FFmpeg (fuzzer harness built with --disable-asm) with UBSAN

    Fixes: signed integer overflow: 167640 - -9223372036854775808 cannot be represented in type 'int64_t'
    Fixes: 9.22337e+18 is outside the range of representable values of type 'long'
    Fixes: 565995849/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_SNOW_fuzzer-5651323392163840
    Fixes: tYZSxpyV9Sjx
    Regression since: 7147c3c911f4ebf5bd1c21444423d8f78c6541ea

    Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
    Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>

diff --git a/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c
index a3ada799b5..397a235821 100644
--- a/libavcodec/ratecontrol.c
+++ b/libavcodec/ratecontrol.c
@@ -971,14 +971,14 @@ float ff_rate_estimate_qscale(MPVMainEncContext *const m, int dry_run)
             wanted_bits_double = m->bit_rate * (double)picture_number / fps;
         else
             wanted_bits_double = m->bit_rate * (double)dts_pic->f->pts / fps;
-        if (wanted_bits_double > INT64_MAX) {
+        if (wanted_bits_double >= 0x1p63) {
             av_log(s->c.avctx, AV_LOG_WARNING, "Bits exceed 64bit range\n");
             wanted_bits = INT64_MAX;
         } else
-            wanted_bits = (int64_t)wanted_bits_double;
+            wanted_bits = FFMAX(wanted_bits_double, INT64_MIN);
     }

-    diff = m->total_bits - wanted_bits;
+    diff = (double)m->total_bits - wanted_bits;
     br_compensation = (a->bit_rate_tolerance - diff) / a->bit_rate_tolerance;
     if (br_compensation <= 0.0)
         br_compensation = 0.001;