]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/adpcm.c
Fix an underflow/overflow that was causing some crackles when playing
[ffmpeg] / libavcodec / adpcm.c
index cf282f1071a4b048288c303fe41e3c418d8679f0..57281d137a1619c21845b1d7fced8dd1f69e61a4 100644 (file)
@@ -20,6 +20,7 @@
  */
 #include "avcodec.h"
 #include "bitstream.h"
+#include "bytestream.h"
 
 /**
  * @file adpcm.c
@@ -612,6 +613,12 @@ static int adpcm_decode_init(AVCodecContext * avctx)
     case CODEC_ID_ADPCM_CT:
         c->status[0].step = c->status[1].step = 511;
         break;
+    case CODEC_ID_ADPCM_IMA_WS:
+        if (avctx->extradata && avctx->extradata_size == 2 * 4) {
+            c->status[0].predictor = AV_RL32(avctx->extradata);
+            c->status[1].predictor = AV_RL32(avctx->extradata + 4);
+        }
+        break;
     default:
         break;
     }
@@ -1310,11 +1317,10 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
         }
         break;
     case CODEC_ID_ADPCM_THP:
-      {
-        GetBitContext gb;
-        int table[16][2];
+    {
+        int table[2][16];
         unsigned int samplecnt;
-        int prev1[2], prev2[2];
+        int prev[2][2];
         int ch;
 
         if (buf_size < 80) {
@@ -1322,21 +1328,15 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
             return -1;
         }
 
-        init_get_bits(&gb, src, buf_size * 8);
-        src += buf_size;
-
-                    get_bits_long(&gb, 32); /* Channel size */
-        samplecnt = get_bits_long(&gb, 32);
+        src+=4;
+        samplecnt = bytestream_get_be32(&src);
 
-        for (ch = 0; ch < 2; ch++)
-            for (i = 0; i < 16; i++)
-                table[i][ch] = get_sbits(&gb, 16);
+        for (i = 0; i < 32; i++)
+            table[0][i] = (int16_t)bytestream_get_be16(&src);
 
         /* Initialize the previous sample.  */
-        for (ch = 0; ch < 2; ch++) {
-            prev1[ch] = get_sbits(&gb, 16);
-            prev2[ch] = get_sbits(&gb, 16);
-        }
+        for (i = 0; i < 4; i++)
+            prev[0][i] = (int16_t)bytestream_get_be16(&src);
 
         if (samplecnt >= (samples_end - samples) /  (st + 1)) {
             av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
@@ -1348,19 +1348,23 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
 
             /* Read in every sample for this channel.  */
             for (i = 0; i < samplecnt / 14; i++) {
-                uint8_t index = get_bits (&gb, 4) & 7;
-                unsigned int exp = get_bits (&gb, 4);
-                int factor1 = table[index * 2][ch];
-                int factor2 = table[index * 2 + 1][ch];
+                int index = (*src >> 4) & 7;
+                unsigned int exp = 28 - (*src++ & 15);
+                int factor1 = table[ch][index * 2];
+                int factor2 = table[ch][index * 2 + 1];
 
                 /* Decode 14 samples.  */
                 for (n = 0; n < 14; n++) {
-                    int sampledat = get_sbits (&gb, 4);
+                    int32_t sampledat;
+                    if(n&1) sampledat=  *src++    <<28;
+                    else    sampledat= (*src&0xF0)<<24;
 
-                    *samples = ((prev1[ch]*factor1
-                                + prev2[ch]*factor2) >> 11) + (sampledat << exp);
-                    prev2[ch] = prev1[ch];
-                    prev1[ch] = *samples++;
+                    sampledat = ((prev[ch][0]*factor1
+                                + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
+                    CLAMP_TO_SHORT(sampledat);
+                    *samples = sampledat;
+                    prev[ch][1] = prev[ch][0];
+                    prev[ch][0] = *samples++;
 
                     /* In case of stereo, skip one sample, this sample
                        is for the other channel.  */
@@ -1373,7 +1377,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
            increased exactly one time too often.  */
         samples -= st;
         break;
-      }
+    }
 
     default:
         return -1;