}
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;
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:
/* 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;
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) {
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;
*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:
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);
}
c->frame.nb_samples = count * 28;
- src = src_end;
+ bytestream2_seek(&gb, 0, SEEK_END);
break;
}
case CODEC_ID_ADPCM_EA_XAS:
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);
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);
}