]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/adpcm.c
Fix typo
[ffmpeg] / libavcodec / adpcm.c
index 085d1a149c79b27364a2581c271aab09c2763672..9111a7ac10a327d97acb37ee51d81f183cad2813 100644 (file)
@@ -20,6 +20,7 @@
  */
 #include "avcodec.h"
 #include "bitstream.h"
+#include "bytestream.h"
 
 /**
  * @file adpcm.c
@@ -181,6 +182,15 @@ static int adpcm_encode_init(AVCodecContext *avctx)
         avctx->frame_size = BLKSIZE * avctx->channels;
         avctx->block_align = BLKSIZE;
         break;
+    case CODEC_ID_ADPCM_SWF:
+        if (avctx->sample_rate != 11025 &&
+            avctx->sample_rate != 22050 &&
+            avctx->sample_rate != 44100) {
+            av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n");
+            return -1;
+        }
+        avctx->frame_size = 512 * (avctx->sample_rate / 11025);
+        break;
     default:
         return -1;
         break;
@@ -447,16 +457,14 @@ static int adpcm_encode_frame(AVCodecContext *avctx,
         n = avctx->frame_size / 8;
             c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
 /*            c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
-            *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
-            *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
+            bytestream_put_le16(&dst, c->status[0].prev_sample);
             *dst++ = (unsigned char)c->status[0].step_index;
             *dst++ = 0; /* unknown */
             samples++;
             if (avctx->channels == 2) {
                 c->status[1].prev_sample = (signed short)samples[1];
 /*                c->status[1].step_index = 0; */
-                *dst++ = (c->status[1].prev_sample) & 0xFF;
-                *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
+                bytestream_put_le16(&dst, c->status[1].prev_sample);
                 *dst++ = (unsigned char)c->status[1].step_index;
                 *dst++ = 0;
                 samples++;
@@ -512,6 +520,32 @@ static int adpcm_encode_frame(AVCodecContext *avctx,
                 samples += 8 * avctx->channels;
             }
         break;
+    case CODEC_ID_ADPCM_SWF:
+    {
+        int i;
+        PutBitContext pb;
+        init_put_bits(&pb, dst, buf_size*8);
+
+        //Store AdpcmCodeSize
+        put_bits(&pb, 2, 2);                //Set 4bits flash adpcm format
+
+        //Init the encoder state
+        for(i=0; i<avctx->channels; i++){
+            c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63); // clip step so it fits 6 bits
+            put_bits(&pb, 16, samples[i] & 0xFFFF);
+            put_bits(&pb, 6, c->status[i].step_index);
+            c->status[i].prev_sample = (signed short)samples[i];
+        }
+
+        for (i=0; i<avctx->frame_size; i++) {
+            put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels*i]) & 0xF);
+            if (avctx->channels == 2)
+                put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1], samples[2*i+1]) & 0xF);
+        }
+        flush_put_bits(&pb);
+        dst += put_bits_count(&pb)>>3;
+        break;
+    }
     case CODEC_ID_ADPCM_MS:
         for(i=0; i<avctx->channels; i++){
             int predictor=0;
@@ -524,20 +558,17 @@ static int adpcm_encode_frame(AVCodecContext *avctx,
             if (c->status[i].idelta < 16)
                 c->status[i].idelta = 16;
 
-            *dst++ = c->status[i].idelta & 0xFF;
-            *dst++ = c->status[i].idelta >> 8;
+            bytestream_put_le16(&dst, c->status[i].idelta);
         }
         for(i=0; i<avctx->channels; i++){
             c->status[i].sample1= *samples++;
 
-            *dst++ = c->status[i].sample1 & 0xFF;
-            *dst++ = c->status[i].sample1 >> 8;
+            bytestream_put_le16(&dst, c->status[i].sample1);
         }
         for(i=0; i<avctx->channels; i++){
             c->status[i].sample2= *samples++;
 
-            *dst++ = c->status[i].sample2 & 0xFF;
-            *dst++ = c->status[i].sample2 >> 8;
+            bytestream_put_le16(&dst, c->status[i].sample2);
         }
 
         if(avctx->trellis > 0) {
@@ -612,6 +643,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;
     }
@@ -1242,7 +1279,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
 
         init_get_bits(&gb, buf, size);
 
-        //read bits & inital values
+        //read bits & initial values
         nb_bits = get_bits(&gb, 2)+2;
         //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
         table = swf_index_tables[nb_bits-2];
@@ -1310,8 +1347,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
         }
         break;
     case CODEC_ID_ADPCM_THP:
-      {
-        GetBitContext gb;
+    {
         int table[2][16];
         unsigned int samplecnt;
         int prev[2][2];
@@ -1322,20 +1358,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 (i = 0; i < 32; i++)
-            table[0][i] = get_sbits(&gb, 16);
+            table[0][i] = (int16_t)bytestream_get_be16(&src);
 
         /* Initialize the previous sample.  */
-        for (ch = 0; ch < 2; ch++) {
-            prev[ch][0] = get_sbits(&gb, 16);
-            prev[ch][1] = 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");
@@ -1347,17 +1378,21 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
 
             /* Read in every sample for this channel.  */
             for (i = 0; i < samplecnt / 14; i++) {
-                int index = get_bits (&gb, 4) & 7;
-                unsigned int exp = get_bits (&gb, 4);
+                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);
-
-                    *samples = ((prev[ch][0]*factor1
-                                + prev[ch][1]*factor2) >> 11) + (sampledat << exp);
+                    int32_t sampledat;
+                    if(n&1) sampledat=  *src++    <<28;
+                    else    sampledat= (*src&0xF0)<<24;
+
+                    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++;
 
@@ -1372,7 +1407,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
            increased exactly one time too often.  */
         samples -= st;
         break;
-      }
+    }
 
     default:
         return -1;