Browse code

adx: use 12-bit coefficients instead of 14-bit to avoid integer overflow

Justin Ruggles authored on 2011/11/21 04:03:21
Showing 3 changed files
... ...
@@ -43,8 +43,8 @@ typedef struct {
43 43
     int in_temp;
44 44
 } ADXContext;
45 45
 
46
-#define    BASEVOL   0x4000
47
-#define    SCALE1    0x7298
48
-#define    SCALE2    0x3350
46
+#define COEFF_BITS  12
47
+#define COEFF1      0x1CA6
48
+#define COEFF2      0x0CD4
49 49
 
50 50
 #endif /* AVCODEC_ADX_H */
... ...
@@ -59,7 +59,7 @@ static void adx_decode(ADXContext *c, int16_t *out, const uint8_t *in, int ch)
59 59
     s2 = prev->s2;
60 60
     for (i = 0; i < 32; i++) {
61 61
         d  = get_sbits(&gb, 4);
62
-        s0 = (BASEVOL * d * scale + SCALE1 * s1 - SCALE2 * s2) >> 14;
62
+        s0 = ((d << COEFF_BITS) * scale + COEFF1 * s1 - COEFF2 * s2) >> COEFF_BITS;
63 63
         s2 = s1;
64 64
         s1 = av_clip_int16(s0);
65 65
         *out = s1;
... ...
@@ -48,7 +48,7 @@ static void adx_encode(unsigned char *adx,const short *wav,
48 48
     s2 = prev->s2;
49 49
     for(i=0;i<32;i++) {
50 50
         s0 = wav[i];
51
-        d = ((s0<<14) - SCALE1*s1 + SCALE2*s2)/BASEVOL;
51
+        d = ((s0 << COEFF_BITS) - COEFF1 * s1 + COEFF2 * s2) >> COEFF_BITS;
52 52
         data[i]=d;
53 53
         if (max<d) max=d;
54 54
         if (min>d) min=d;