}
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:
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
* 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;
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;
}
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;
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;
c->frame.nb_samples = nb_samples = coded_samples;
}
- src = buf;
-
st = avctx->channels == 2 ? 1 : 0;
switch(avctx->codec->id) {
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);
}
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 );
}
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);
*got_frame_ptr = 1;
*(AVFrame *)data = c->frame;
- return src == buf ? bytestream2_tell(&gb) : src - buf;
+ return bytestream2_tell(&gb);
}