[FFmpeg-cvslog] avcodec/mpegvideo_enc: use 64bit multiplication in dct_quantize_trellis_c and dct_quantize_c

Marton Balint git at videolan.org
Sat Jan 25 21:24:52 EET 2025


ffmpeg | branch: master | Marton Balint <cus at passwd.hu> | Mon Jan  6 23:08:32 2025 +0100| [ec4d3dc5b90f0e5b4e931667fd9c9c6944da40fa] | committer: Marton Balint

avcodec/mpegvideo_enc: use 64bit multiplication in dct_quantize_trellis_c and dct_quantize_c

Fixes corruption with:

ffmpeg -t 1 -filter_complex "sine=f=21,showwaves=scale=cbrt:mode=line:colors=white:draw=full" -c:v mpeg2video -non_linear_quant 1 -qmin 1 -qmax 1 -cpuflags 0 out.mpg

or

ffmpeg -t 1 -filter_complex "sine=f=21,showwaves=scale=cbrt:mode=line:colors=white:draw=full" -c:v mpeg2video -non_linear_quant 1 -qmin 1 -qmax 1 -trellis 1 out.mpg

Signed-off-by: Marton Balint <cus at passwd.hu>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=ec4d3dc5b90f0e5b4e931667fd9c9c6944da40fa
---

 libavcodec/mpegvideo_enc.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index d5088fe67f..e6e6648e3c 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -3998,9 +3998,9 @@ static int dct_quantize_trellis_c(MpegEncContext *s,
 
     for(i=63; i>=start_i; i--) {
         const int j = scantable[i];
-        int level = block[j] * qmat[j];
+        int64_t level = (int64_t)block[j] * qmat[j];
 
-        if(((unsigned)(level+threshold1))>threshold2){
+        if(((uint64_t)(level+threshold1))>threshold2){
             last_non_zero = i;
             break;
         }
@@ -4008,11 +4008,11 @@ static int dct_quantize_trellis_c(MpegEncContext *s,
 
     for(i=start_i; i<=last_non_zero; i++) {
         const int j = scantable[i];
-        int level = block[j] * qmat[j];
+        int64_t level = (int64_t)block[j] * qmat[j];
 
 //        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
 //           || bias-level >= (1<<(QMAT_SHIFT - 3))){
-        if(((unsigned)(level+threshold1))>threshold2){
+        if(((uint64_t)(level+threshold1))>threshold2){
             if(level>0){
                 level= (bias + level)>>QMAT_SHIFT;
                 coeff[0][i]= level;
@@ -4601,7 +4601,7 @@ static int dct_quantize_c(MpegEncContext *s,
                           int16_t *block, int n,
                           int qscale, int *overflow)
 {
-    int i, j, level, last_non_zero, q, start_i;
+    int i, last_non_zero, q, start_i;
     const int *qmat;
     const uint8_t *scantable;
     int bias;
@@ -4641,10 +4641,10 @@ static int dct_quantize_c(MpegEncContext *s,
     threshold1= (1<<QMAT_SHIFT) - bias - 1;
     threshold2= (threshold1<<1);
     for(i=63;i>=start_i;i--) {
-        j = scantable[i];
-        level = block[j] * qmat[j];
+        const int j = scantable[i];
+        int64_t level = (int64_t)block[j] * qmat[j];
 
-        if(((unsigned)(level+threshold1))>threshold2){
+        if(((uint64_t)(level+threshold1))>threshold2){
             last_non_zero = i;
             break;
         }else{
@@ -4652,12 +4652,12 @@ static int dct_quantize_c(MpegEncContext *s,
         }
     }
     for(i=start_i; i<=last_non_zero; i++) {
-        j = scantable[i];
-        level = block[j] * qmat[j];
+        const int j = scantable[i];
+        int64_t level = (int64_t)block[j] * qmat[j];
 
 //        if(   bias+level >= (1<<QMAT_SHIFT)
 //           || bias-level >= (1<<QMAT_SHIFT)){
-        if(((unsigned)(level+threshold1))>threshold2){
+        if(((uint64_t)(level+threshold1))>threshold2){
             if(level>0){
                 level= (bias + level)>>QMAT_SHIFT;
                 block[j]= level;



More information about the ffmpeg-cvslog mailing list