]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/adpcm.c
iac: generate codec tables as they are supposed to be
[ffmpeg] / libavcodec / adpcm.c
index ebeb2dad30fb8da92abdac32aa26fcd060e2ce06..8170e7f4163171108401168239478b99dd7f709e 100644 (file)
@@ -128,7 +128,7 @@ static av_cold int adpcm_decode_init(AVCodecContext * avctx)
         }
         break;
     case CODEC_ID_ADPCM_IMA_WS:
-        if (avctx->extradata && avctx->extradata_size >= 42)
+        if (avctx->extradata && avctx->extradata_size >= 2)
             c->vqa_version = AV_RL16(avctx->extradata);
         break;
     default:
@@ -343,6 +343,64 @@ static int xa_decode(AVCodecContext *avctx,
     return 0;
 }
 
+static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples)
+{
+    ADPCMDecodeContext *c = avctx->priv_data;
+    GetBitContext gb;
+    const int *table;
+    int k0, signmask, nb_bits, count;
+    int size = buf_size*8;
+    int i;
+
+    init_get_bits(&gb, buf, size);
+
+    //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];
+    k0 = 1 << (nb_bits-2);
+    signmask = 1 << (nb_bits-1);
+
+    while (get_bits_count(&gb) <= size - 22*avctx->channels) {
+        for (i = 0; i < avctx->channels; i++) {
+            *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
+            c->status[i].step_index = get_bits(&gb, 6);
+        }
+
+        for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
+            int i;
+
+            for (i = 0; i < avctx->channels; i++) {
+                // similar to IMA adpcm
+                int delta = get_bits(&gb, nb_bits);
+                int step = ff_adpcm_step_table[c->status[i].step_index];
+                long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
+                int k = k0;
+
+                do {
+                    if (delta & k)
+                        vpdiff += step;
+                    step >>= 1;
+                    k >>= 1;
+                } while(k);
+                vpdiff += step;
+
+                if (delta & signmask)
+                    c->status[i].predictor -= vpdiff;
+                else
+                    c->status[i].predictor += vpdiff;
+
+                c->status[i].step_index += table[delta & (~signmask)];
+
+                c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
+                c->status[i].predictor = av_clip_int16(c->status[i].predictor);
+
+                *samples++ = c->status[i].predictor;
+            }
+        }
+    }
+}
+
 /**
  * Get the number of samples that will be decoded from the packet.
  * In one case, this is actually the maximum number of samples possible to
@@ -352,8 +410,7 @@ static int xa_decode(AVCodecContext *avctx,
  *                           packet, or 0 if the codec does not encode the
  *                           number of samples in each frame.
  */
-static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
-                          GetByteContext *gb,
+static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
                           int buf_size, int *coded_samples)
 {
     ADPCMDecodeContext *s = avctx->priv_data;
@@ -403,9 +460,7 @@ static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
     switch (avctx->codec->id) {
     case CODEC_ID_ADPCM_EA:
         has_coded_samples = 1;
-        if (buf_size < 4)
-            return 0;
-        *coded_samples  = AV_RL32(buf);
+        *coded_samples  = bytestream2_get_le32(gb);
         *coded_samples -= *coded_samples % 28;
         nb_samples      = (buf_size - 12) / 30 * 28;
         break;
@@ -415,7 +470,7 @@ static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
         nb_samples     = (buf_size - (4 + 8 * ch)) * 2 / ch;
         break;
     case CODEC_ID_ADPCM_EA_MAXIS_XA:
-        nb_samples = ((buf_size - ch) / (2 * ch)) * 2 * ch;
+        nb_samples = (buf_size - ch) / ch * 2;
         break;
     case CODEC_ID_ADPCM_EA_R1:
     case CODEC_ID_ADPCM_EA_R2:
@@ -423,20 +478,18 @@ static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
         /* maximum number of samples */
         /* has internal offsets and a per-frame switch to signal raw 16-bit */
         has_coded_samples = 1;
-        if (buf_size < 4)
-            return 0;
         switch (avctx->codec->id) {
         case CODEC_ID_ADPCM_EA_R1:
             header_size    = 4 + 9 * ch;
-            *coded_samples = AV_RL32(buf);
+            *coded_samples = bytestream2_get_le32(gb);
             break;
         case CODEC_ID_ADPCM_EA_R2:
             header_size    = 4 + 5 * ch;
-            *coded_samples = AV_RL32(buf);
+            *coded_samples = bytestream2_get_le32(gb);
             break;
         case CODEC_ID_ADPCM_EA_R3:
             header_size    = 4 + 5 * ch;
-            *coded_samples = AV_RB32(buf);
+            *coded_samples = bytestream2_get_be32(gb);
             break;
         }
         *coded_samples -= *coded_samples % 28;
@@ -483,7 +536,7 @@ static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
     case CODEC_ID_ADPCM_SWF:
     {
         int buf_bits       = buf_size * 8 - 2;
-        int nbits          = (buf[0] >> 6) + 2;
+        int nbits          = (bytestream2_get_byte(gb) >> 6) + 2;
         int block_hdr_size = 22 * ch;
         int block_size     = block_hdr_size + nbits * ch * 4095;
         int nblocks        = buf_bits / block_size;
@@ -495,9 +548,8 @@ static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
     }
     case CODEC_ID_ADPCM_THP:
         has_coded_samples = 1;
-        if (buf_size < 8)
-            return 0;
-        *coded_samples  = AV_RB32(&buf[4]);
+        bytestream2_skip(gb, 4); // channel size
+        *coded_samples  = bytestream2_get_be32(gb);
         *coded_samples -= *coded_samples % 14;
         nb_samples      = (buf_size - 80) / (8 * ch) * 14;
         break;
@@ -522,14 +574,13 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
     ADPCMChannelStatus *cs;
     int n, m, channel, i;
     short *samples;
-    const uint8_t *src;
     int st; /* stereo */
     int count1, count2;
     int nb_samples, coded_samples, ret;
     GetByteContext gb;
 
     bytestream2_init(&gb, buf, buf_size);
-    nb_samples = get_nb_samples(avctx, buf, &gb, buf_size, &coded_samples);
+    nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples);
     if (nb_samples <= 0) {
         av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
         return AVERROR_INVALIDDATA;
@@ -551,8 +602,6 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
         c->frame.nb_samples = nb_samples = coded_samples;
     }
 
-    src = buf;
-
     st = avctx->channels == 2 ? 1 : 0;
 
     switch(avctx->codec->id) {
@@ -868,37 +917,35 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
         break;
     case CODEC_ID_ADPCM_EA:
     {
-        int32_t previous_left_sample, previous_right_sample;
-        int32_t current_left_sample, current_right_sample;
-        int32_t next_left_sample, next_right_sample;
-        int32_t coeff1l, coeff2l, coeff1r, coeff2r;
-        uint8_t shift_left, shift_right;
+        int previous_left_sample, previous_right_sample;
+        int current_left_sample, current_right_sample;
+        int next_left_sample, next_right_sample;
+        int coeff1l, coeff2l, coeff1r, coeff2r;
+        int shift_left, shift_right;
 
         /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
            each coding 28 stereo samples. */
 
-        src += 4; // skip sample count (already read)
-
-        current_left_sample   = (int16_t)bytestream_get_le16(&src);
-        previous_left_sample  = (int16_t)bytestream_get_le16(&src);
-        current_right_sample  = (int16_t)bytestream_get_le16(&src);
-        previous_right_sample = (int16_t)bytestream_get_le16(&src);
+        current_left_sample   = sign_extend(bytestream2_get_le16u(&gb), 16);
+        previous_left_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
+        current_right_sample  = sign_extend(bytestream2_get_le16u(&gb), 16);
+        previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
 
         for (count1 = 0; count1 < nb_samples / 28; count1++) {
-            coeff1l = ea_adpcm_table[ *src >> 4       ];
-            coeff2l = ea_adpcm_table[(*src >> 4  ) + 4];
-            coeff1r = ea_adpcm_table[*src & 0x0F];
-            coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
-            src++;
+            int byte = bytestream2_get_byteu(&gb);
+            coeff1l = ea_adpcm_table[ byte >> 4       ];
+            coeff2l = ea_adpcm_table[(byte >> 4  ) + 4];
+            coeff1r = ea_adpcm_table[ byte & 0x0F];
+            coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
 
-            shift_left  = 20 - (*src >> 4);
-            shift_right = 20 - (*src & 0x0F);
-            src++;
+            byte = bytestream2_get_byteu(&gb);
+            shift_left  = 20 - (byte >> 4);
+            shift_right = 20 - (byte & 0x0F);
 
             for (count2 = 0; count2 < 28; count2++) {
-                next_left_sample  = sign_extend(*src >> 4, 4) << shift_left;
-                next_right_sample = sign_extend(*src,      4) << shift_right;
-                src++;
+                byte = bytestream2_get_byteu(&gb);
+                next_left_sample  = sign_extend(byte >> 4, 4) << shift_left;
+                next_right_sample = sign_extend(byte,      4) << shift_right;
 
                 next_left_sample = (next_left_sample +
                     (current_left_sample * coeff1l) +
@@ -911,13 +958,12 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
                 current_left_sample = av_clip_int16(next_left_sample);
                 previous_right_sample = current_right_sample;
                 current_right_sample = av_clip_int16(next_right_sample);
-                *samples++ = (unsigned short)current_left_sample;
-                *samples++ = (unsigned short)current_right_sample;
+                *samples++ = current_left_sample;
+                *samples++ = current_right_sample;
             }
         }
 
-        if (src - buf == buf_size - 2)
-            src += 2; // Skip terminating 0x0000
+        bytestream2_skip(&gb, 2); // Skip terminating 0x0000
 
         break;
     }
@@ -926,15 +972,19 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
         int coeff[2][2], shift[2];
 
         for(channel = 0; channel < avctx->channels; channel++) {
+            int byte = bytestream2_get_byteu(&gb);
             for (i=0; i<2; i++)
-                coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
-            shift[channel] = 20 - (*src & 0x0F);
-            src++;
+                coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
+            shift[channel] = 20 - (byte & 0x0F);
         }
         for (count1 = 0; count1 < nb_samples / 2; count1++) {
+            int byte[2];
+
+            byte[0] = bytestream2_get_byteu(&gb);
+            if (st) byte[1] = bytestream2_get_byteu(&gb);
             for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
                 for(channel = 0; channel < avctx->channels; channel++) {
-                    int32_t sample = sign_extend(src[channel] >> i, 4) << shift[channel];
+                    int sample = sign_extend(byte[channel] >> i, 4) << shift[channel];
                     sample = (sample +
                              c->status[channel].sample1 * coeff[channel][0] +
                              c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
@@ -943,10 +993,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
                     *samples++ = c->status[channel].sample1;
                 }
             }
-            src+=avctx->channels;
         }
-        /* consume whole packet */
-        src = buf + buf_size;
+        bytestream2_seek(&gb, 0, SEEK_END);
         break;
     }
     case CODEC_ID_ADPCM_EA_R1:
@@ -957,56 +1005,53 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
            4chan: 0=fl, 1=rl, 2=fr, 3=rr
            6chan: 0=fl, 1=c,  2=fr, 3=rl,  4=rr, 5=sub */
         const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
-        int32_t previous_sample, current_sample, next_sample;
-        int32_t coeff1, coeff2;
-        uint8_t shift;
+        int previous_sample, current_sample, next_sample;
+        int coeff1, coeff2;
+        int shift;
         unsigned int channel;
         uint16_t *samplesC;
-        const uint8_t *srcC;
-        const uint8_t *src_end = buf + buf_size;
         int count = 0;
+        int offsets[6];
 
-        src += 4; // skip sample count (already read)
+        for (channel=0; channel<avctx->channels; channel++)
+            offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
+                                             bytestream2_get_le32(&gb)) +
+                               (avctx->channels + 1) * 4;
 
         for (channel=0; channel<avctx->channels; channel++) {
-            int32_t offset = (big_endian ? bytestream_get_be32(&src)
-                                         : bytestream_get_le32(&src))
-                           + (avctx->channels-channel-1) * 4;
-
-            if ((offset < 0) || (offset >= src_end - src - 4)) break;
-            srcC  = src + offset;
+            bytestream2_seek(&gb, offsets[channel], SEEK_SET);
             samplesC = samples + channel;
 
             if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
-                current_sample  = (int16_t)bytestream_get_le16(&srcC);
-                previous_sample = (int16_t)bytestream_get_le16(&srcC);
+                current_sample  = sign_extend(bytestream2_get_le16(&gb), 16);
+                previous_sample = sign_extend(bytestream2_get_le16(&gb), 16);
             } else {
                 current_sample  = c->status[channel].predictor;
                 previous_sample = c->status[channel].prev_sample;
             }
 
             for (count1 = 0; count1 < nb_samples / 28; count1++) {
-                if (*srcC == 0xEE) {  /* only seen in R2 and R3 */
-                    srcC++;
-                    if (srcC > src_end - 30*2) break;
-                    current_sample  = (int16_t)bytestream_get_be16(&srcC);
-                    previous_sample = (int16_t)bytestream_get_be16(&srcC);
+                int byte = bytestream2_get_byte(&gb);
+                if (byte == 0xEE) {  /* only seen in R2 and R3 */
+                    current_sample  = sign_extend(bytestream2_get_be16(&gb), 16);
+                    previous_sample = sign_extend(bytestream2_get_be16(&gb), 16);
 
                     for (count2=0; count2<28; count2++) {
-                        *samplesC = (int16_t)bytestream_get_be16(&srcC);
+                        *samplesC = sign_extend(bytestream2_get_be16(&gb), 16);
                         samplesC += avctx->channels;
                     }
                 } else {
-                    coeff1 = ea_adpcm_table[ *srcC>>4     ];
-                    coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
-                    shift = 20 - (*srcC++ & 0x0F);
+                    coeff1 = ea_adpcm_table[ byte >> 4     ];
+                    coeff2 = ea_adpcm_table[(byte >> 4) + 4];
+                    shift = 20 - (byte & 0x0F);
 
-                    if (srcC > src_end - 14) break;
                     for (count2=0; count2<28; count2++) {
                         if (count2 & 1)
-                            next_sample = sign_extend(*srcC++,    4) << shift;
-                        else
-                            next_sample = sign_extend(*srcC >> 4, 4) << shift;
+                            next_sample = sign_extend(byte,    4) << shift;
+                        else {
+                            byte = bytestream2_get_byte(&gb);
+                            next_sample = sign_extend(byte >> 4, 4) << shift;
+                        }
 
                         next_sample += (current_sample  * coeff1) +
                                        (previous_sample * coeff2);
@@ -1033,7 +1078,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
         }
 
         c->frame.nb_samples = count * 28;
-        src = src_end;
+        bytestream2_seek(&gb, 0, SEEK_END);
         break;
     }
     case CODEC_ID_ADPCM_EA_XAS:
@@ -1041,18 +1086,22 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
             int coeff[2][4], shift[4];
             short *s2, *s = &samples[channel];
             for (n=0; n<4; n++, s+=32*avctx->channels) {
+                int val = sign_extend(bytestream2_get_le16u(&gb), 16);
                 for (i=0; i<2; i++)
-                    coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
-                shift[n] = 20 - (src[2] & 0x0F);
-                for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
-                    s2[0] = (src[0]&0xF0) + (src[1]<<8);
+                    coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i];
+                s[0] = val & ~0x0F;
+
+                val = sign_extend(bytestream2_get_le16u(&gb), 16);
+                shift[n] = 20 - (val & 0x0F);
+                s[avctx->channels] = val & ~0x0F;
             }
 
             for (m=2; m<32; m+=2) {
                 s = &samples[m*avctx->channels + channel];
-                for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
+                for (n=0; n<4; n++, s+=32*avctx->channels) {
+                    int byte = bytestream2_get_byteu(&gb);
                     for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
-                        int level = sign_extend(*src >> (4 - i), 4) << shift[n];
+                        int level = sign_extend(byte >> (4 - i), 4) << shift[n];
                         int pred  = s2[-1*avctx->channels] * coeff[0][n]
                                   + s2[-2*avctx->channels] * coeff[1][n];
                         s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
@@ -1064,32 +1113,38 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
     case CODEC_ID_ADPCM_IMA_AMV:
     case CODEC_ID_ADPCM_IMA_SMJPEG:
         if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV) {
-            c->status[0].predictor = sign_extend(bytestream_get_le16(&src), 16);
-            c->status[0].step_index = av_clip(bytestream_get_le16(&src), 0, 88);
-            src += 4;
+            c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
+            c->status[0].step_index = bytestream2_get_le16u(&gb);
+            bytestream2_skipu(&gb, 4);
         } else {
-            c->status[0].predictor = sign_extend(bytestream_get_be16(&src), 16);
-            c->status[0].step_index = av_clip(bytestream_get_byte(&src), 0, 88);
-            src += 1;
+            c->status[0].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
+            c->status[0].step_index = bytestream2_get_byteu(&gb);
+            bytestream2_skipu(&gb, 1);
+        }
+        if (c->status[0].step_index > 88u) {
+            av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
+                   c->status[0].step_index);
+            return AVERROR_INVALIDDATA;
         }
 
-        for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
-            char hi, lo;
-            lo = *src & 0x0F;
-            hi = *src >> 4;
+        for (n = nb_samples >> (1 - st); n > 0; n--) {
+            int hi, lo, v = bytestream2_get_byteu(&gb);
 
-            if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
-                FFSWAP(char, hi, lo);
+            if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV) {
+                hi = v & 0x0F;
+                lo = v >> 4;
+            } else {
+                lo = v & 0x0F;
+                hi = v >> 4;
+            }
 
-            *samples++ = adpcm_ima_expand_nibble(&c->status[0],
-                lo, 3);
-            *samples++ = adpcm_ima_expand_nibble(&c->status[0],
-                hi, 3);
+            *samples++ = adpcm_ima_expand_nibble(&c->status[0], lo, 3);
+            *samples++ = adpcm_ima_expand_nibble(&c->status[0], hi, 3);
         }
         break;
     case CODEC_ID_ADPCM_CT:
-        for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
-            uint8_t v = *src;
+        for (n = nb_samples >> (1 - st); n > 0; n--) {
+            int v = bytestream2_get_byteu(&gb);
             *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4  );
             *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
         }
@@ -1099,101 +1154,51 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
     case CODEC_ID_ADPCM_SBPRO_2:
         if (!c->status[0].step_index) {
             /* the first byte is a raw sample */
-            *samples++ = 128 * (*src++ - 0x80);
+            *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
             if (st)
-              *samples++ = 128 * (*src++ - 0x80);
+                *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);
             c->status[0].step_index = 1;
             nb_samples--;
         }
         if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
-            for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
+            for (n = nb_samples >> (1 - st); n > 0; n--) {
+                int byte = bytestream2_get_byteu(&gb);
                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
-                    src[0] >> 4, 4, 0);
+                                                       byte >> 4,   4, 0);
                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
-                    src[0] & 0x0F, 4, 0);
+                                                       byte & 0x0F, 4, 0);
             }
         } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
-            for (n = nb_samples / 3; n > 0; n--, src++) {
+            for (n = nb_samples / 3; n > 0; n--) {
+                int byte = bytestream2_get_byteu(&gb);
                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
-                     src[0] >> 5        , 3, 0);
+                                                        byte >> 5        , 3, 0);
                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
-                    (src[0] >> 2) & 0x07, 3, 0);
+                                                       (byte >> 2) & 0x07, 3, 0);
                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
-                    src[0] & 0x03, 2, 0);
+                                                        byte & 0x03,       2, 0);
             }
         } else {
-            for (n = nb_samples >> (2 - st); n > 0; n--, src++) {
+            for (n = nb_samples >> (2 - st); n > 0; n--) {
+                int byte = bytestream2_get_byteu(&gb);
                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
-                     src[0] >> 6        , 2, 2);
+                                                        byte >> 6        , 2, 2);
                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
-                    (src[0] >> 4) & 0x03, 2, 2);
+                                                       (byte >> 4) & 0x03, 2, 2);
                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
-                    (src[0] >> 2) & 0x03, 2, 2);
+                                                       (byte >> 2) & 0x03, 2, 2);
                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
-                    src[0] & 0x03, 2, 2);
+                                                        byte & 0x03,       2, 2);
             }
         }
         break;
     case CODEC_ID_ADPCM_SWF:
-    {
-        GetBitContext gb;
-        const int *table;
-        int k0, signmask, nb_bits, count;
-        int size = buf_size*8;
-
-        init_get_bits(&gb, buf, size);
-
-        //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];
-        k0 = 1 << (nb_bits-2);
-        signmask = 1 << (nb_bits-1);
-
-        while (get_bits_count(&gb) <= size - 22*avctx->channels) {
-            for (i = 0; i < avctx->channels; i++) {
-                *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
-                c->status[i].step_index = get_bits(&gb, 6);
-            }
-
-            for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
-                int i;
-
-                for (i = 0; i < avctx->channels; i++) {
-                    // similar to IMA adpcm
-                    int delta = get_bits(&gb, nb_bits);
-                    int step = ff_adpcm_step_table[c->status[i].step_index];
-                    long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
-                    int k = k0;
-
-                    do {
-                        if (delta & k)
-                            vpdiff += step;
-                        step >>= 1;
-                        k >>= 1;
-                    } while(k);
-                    vpdiff += step;
-
-                    if (delta & signmask)
-                        c->status[i].predictor -= vpdiff;
-                    else
-                        c->status[i].predictor += vpdiff;
-
-                    c->status[i].step_index += table[delta & (~signmask)];
-
-                    c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
-                    c->status[i].predictor = av_clip_int16(c->status[i].predictor);
-
-                    *samples++ = c->status[i].predictor;
-                }
-            }
-        }
-        src += buf_size;
+        adpcm_swf_decode(avctx, buf, buf_size, samples);
+        bytestream2_seek(&gb, 0, SEEK_END);
         break;
-    }
     case CODEC_ID_ADPCM_YAMAHA:
-        for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
-            uint8_t v = *src;
+        for (n = nb_samples >> (1 - st); n > 0; n--) {
+            int v = bytestream2_get_byteu(&gb);
             *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
             *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4  );
         }
@@ -1204,31 +1209,36 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
         int prev[2][2];
         int ch;
 
-        src += 4; // skip channel size
-        src += 4; // skip number of samples (already read)
-
-        for (i = 0; i < 32; i++)
-            table[0][i] = (int16_t)bytestream_get_be16(&src);
+        for (i = 0; i < 2; i++)
+            for (n = 0; n < 16; n++)
+                table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
 
         /* Initialize the previous sample.  */
-        for (i = 0; i < 4; i++)
-            prev[0][i] = (int16_t)bytestream_get_be16(&src);
+        for (i = 0; i < 2; i++)
+            for (n = 0; n < 2; n++)
+                prev[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);
 
         for (ch = 0; ch <= st; ch++) {
             samples = (short *)c->frame.data[0] + ch;
 
             /* Read in every sample for this channel.  */
             for (i = 0; i < nb_samples / 14; i++) {
-                int index = (*src >> 4) & 7;
-                unsigned int exp = *src++ & 15;
+                int byte = bytestream2_get_byteu(&gb);
+                int index = (byte >> 4) & 7;
+                unsigned int exp = byte & 0x0F;
                 int factor1 = table[ch][index * 2];
                 int factor2 = table[ch][index * 2 + 1];
 
                 /* Decode 14 samples.  */
                 for (n = 0; n < 14; n++) {
                     int32_t sampledat;
-                    if(n&1) sampledat = sign_extend(*src++, 4);
-                    else    sampledat = sign_extend(*src >> 4, 4);
+
+                    if (n & 1) {
+                        sampledat = sign_extend(byte, 4);
+                    } else {
+                        byte = bytestream2_get_byteu(&gb);
+                        sampledat = sign_extend(byte >> 4, 4);
+                    }
 
                     sampledat = ((prev[ch][0]*factor1
                                 + prev[ch][1]*factor2) >> 11) + (sampledat << exp);
@@ -1252,7 +1262,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
     *got_frame_ptr   = 1;
     *(AVFrame *)data = c->frame;
 
-    return src == buf ? bytestream2_tell(&gb) : src - buf;
+    return bytestream2_tell(&gb);
 }