PR #26227 opened by Pu Cheng (GeekQAQ)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26227
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26227.patch

For noise shaping dither methods, swri_dither_init() scales
ns_scale_1 by 2 / (1 << (8 * bytes_per_sample)). With 32-bit integer
output this shifts an int by 32, which is undefined; on x86 the shift
count wraps and the result is 1, so the scale is computed as about
1 - 2 * 10^(gain/200) instead of a value close to 1.

Use a 64-bit shift.

Signed-off-by: Pu Cheng <[email protected]>



>From 5cebf026d48ccc67ba02175544692588344b478e Mon Sep 17 00:00:00 2001
From: Pu Cheng <[email protected]>
Date: Fri, 20 Nov 2026 14:34:00 +0800
Subject: [PATCH] swresample/dither: avoid shifting 1 by 32 for 32-bit output

For noise shaping dither methods, swri_dither_init() scales
ns_scale_1 by 2 / (1 << (8 * bytes_per_sample)). With 32-bit integer
output this shifts an int by 32, which is undefined; on x86 the shift
count wraps and the result is 1, so the scale is computed as about
1 - 2 * 10^(gain/200) instead of a value close to 1.

Use a 64-bit shift.

Signed-off-by: Pu Cheng <[email protected]>
---
 libswresample/dither.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libswresample/dither.c b/libswresample/dither.c
index 61151a26ea4..eb8af361597 100644
--- a/libswresample/dither.c
+++ b/libswresample/dither.c
@@ -120,7 +120,7 @@ av_cold int swri_dither_init(SwrContext *s, enum 
AVSampleFormat out_fmt, enum AV
             s->dither.ns_taps = f->len;
             for (j=0; j<f->len; j++)
                 s->dither.ns_coeffs[j] = f->coefs[j];
-            s->dither.ns_scale_1 *= 1 - exp(f->gain_cB * M_LN10 * 0.005) * 2 / 
(1<<(8*av_get_bytes_per_sample(out_fmt)));
+            s->dither.ns_scale_1 *= 1 - exp(f->gain_cB * M_LN10 * 0.005) * 2 / 
(1LL<<(8*av_get_bytes_per_sample(out_fmt)));
             break;
         }
     }
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to