---
libavcodec/adpcm.c | 366 ++++++++++++++++++++++++++++-----------------------
1 files changed, 201 insertions(+), 165 deletions(-)
diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c
index b319635..1e9078c 100644
--- a/libavcodec/adpcm.c
+++ b/libavcodec/adpcm.c
@@ -86,6 +86,7 @@ static const int swf_index_tables[4][16] = {
typedef struct ADPCMDecodeContext {
AVFrame frame;
ADPCMChannelStatus status[6];
+ GetByteContext g;
} ADPCMDecodeContext;
static av_cold int adpcm_decode_init(AVCodecContext * avctx)
@@ -334,14 +335,15 @@ static void xa_decode(short *out, const unsigned char *in,
* 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,
- int buf_size, int *coded_samples)
+static int get_nb_samples(AVCodecContext *avctx, ADPCMDecodeContext *ctx,
+ int *coded_samples)
{
ADPCMDecodeContext *s = avctx->priv_data;
int nb_samples = 0;
int ch = avctx->channels;
int has_coded_samples = 0;
int header_size;
+ int buf_size = bytestream2_get_bytes_left(&ctx->g);
*coded_samples = 0;
@@ -385,7 +387,7 @@ static int get_nb_samples(AVCodecContext *avctx, const
uint8_t *buf,
has_coded_samples = 1;
if (buf_size < 4)
return 0;
- *coded_samples = AV_RL32(buf);
+ *coded_samples = bytestream2_get_le32(&ctx->g);
*coded_samples -= *coded_samples % 28;
nb_samples = (buf_size - 12) / 30 * 28;
break;
@@ -393,7 +395,7 @@ static int get_nb_samples(AVCodecContext *avctx, const
uint8_t *buf,
has_coded_samples = 1;
if (buf_size < 4)
return 0;
- *coded_samples = AV_RL32(buf);
+ *coded_samples = bytestream2_get_le32(&ctx->g);
nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
break;
case CODEC_ID_ADPCM_EA_MAXIS_XA:
@@ -410,15 +412,15 @@ static int get_nb_samples(AVCodecContext *avctx, const
uint8_t *buf,
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(&ctx->g);
break;
case CODEC_ID_ADPCM_EA_R2:
header_size = 4 + 5 * ch;
- *coded_samples = AV_RL32(buf);
+ *coded_samples = bytestream2_get_le32(&ctx->g);
break;
case CODEC_ID_ADPCM_EA_R3:
header_size = 4 + 5 * ch;
- *coded_samples = AV_RB32(buf);
+ *coded_samples = bytestream2_get_be32(&ctx->g);
break;
}
*coded_samples -= *coded_samples % 28;
@@ -463,7 +465,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(&ctx->g) >> 6) + 2;
int block_hdr_size = 22 * ch;
int block_size = block_hdr_size + nbits * ch * 4095;
int nblocks = buf_bits / block_size;
@@ -477,7 +479,8 @@ static int get_nb_samples(AVCodecContext *avctx, const
uint8_t *buf,
has_coded_samples = 1;
if (buf_size < 8)
return 0;
- *coded_samples = AV_RB32(&buf[4]);
+ bytestream2_skip(&ctx->g, 4);
+ *coded_samples = bytestream2_get_be32(&ctx->g);
*coded_samples -= *coded_samples % 14;
nb_samples = (buf_size - 80) / (8 * ch) * 14;
break;
@@ -504,8 +507,8 @@ static int get_nb_samples(AVCodecContext *avctx, const
uint8_t *buf,
{ \
if (end_of_packet) \
break; \
- last_byte = *src++; \
- if (src >= buf + buf_size) \
+ last_byte = bytestream2_get_byte(&c->g); \
+ if (!bytestream2_get_bytes_left(&c->g)) \
end_of_packet = 1; \
nibble = last_byte & 0x0F; \
decode_top_nibble_next = 1; \
@@ -520,12 +523,12 @@ 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;
- nb_samples = get_nb_samples(avctx, buf, buf_size, &coded_samples);
+ bytestream2_init(&c->g, buf, buf_size);
+ nb_samples = get_nb_samples(avctx, c, &coded_samples);
if (nb_samples <= 0) {
av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
return AVERROR_INVALIDDATA;
@@ -547,8 +550,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) {
@@ -562,12 +563,10 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
/* (pppppp) (piiiiiii) */
/* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor
value */
- predictor = AV_RB16(src);
+ predictor = bytestream2_get_be16(&c->g);
step_index = predictor & 0x7F;
predictor &= 0xFF80;
- src += 2;
-
if (cs->step_index == step_index) {
int diff = (int)predictor - cs->predictor;
if (diff < 0)
@@ -588,11 +587,11 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
samples = (short *)c->frame.data[0] + channel;
for (m = 0; m < 32; m++) {
- *samples = adpcm_ima_qt_expand_nibble(cs, src[0] & 0x0F, 3);
+ unsigned int v = bytestream2_get_byte(&c->g);
+ *samples = adpcm_ima_qt_expand_nibble(cs, v & 0x0F, 3);
samples += avctx->channels;
- *samples = adpcm_ima_qt_expand_nibble(cs, src[0] >> 4 , 3);
+ *samples = adpcm_ima_qt_expand_nibble(cs, v >> 4 , 3);
samples += avctx->channels;
- src ++;
}
}
break;
@@ -601,22 +600,28 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
buf_size = avctx->block_align;
for(i=0; i<avctx->channels; i++){
+ unsigned int v;
cs = &(c->status[i]);
- cs->predictor = *samples++ = (int16_t)bytestream_get_le16(&src);
+ cs->predictor = *samples++ = (int16_t)bytestream2_get_le16(&c->g);
- cs->step_index = *src++;
+ cs->step_index = bytestream2_get_byte(&c->g);
if (cs->step_index > 88){
av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
cs->step_index);
cs->step_index = 88;
}
- if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be
null but is %d!!\n", src[-1]); /* unused */
+
+ v = bytestream2_get_byte(&c->g);
+ if (v)
+ /* unused */
+ av_log(avctx, AV_LOG_ERROR,
+ "unused byte should be null but is %d!!\n", v);
}
for (n = (nb_samples - 1) / 8; n > 0; n--) {
for (i = 0; i < avctx->channels; i++) {
cs = &c->status[i];
for (m = 0; m < 4; m++) {
- uint8_t v = *src++;
+ uint8_t v = bytestream2_get_byte(&c->g);
*samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
samples += avctx->channels;
*samples = adpcm_ima_expand_nibble(cs, v >> 4 , 3);
@@ -629,18 +634,18 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
break;
case CODEC_ID_ADPCM_4XM:
for (i = 0; i < avctx->channels; i++)
- c->status[i].predictor= (int16_t)bytestream_get_le16(&src);
+ c->status[i].predictor = (int16_t)bytestream2_get_le16(&c->g);
for (i = 0; i < avctx->channels; i++) {
- c->status[i].step_index= (int16_t)bytestream_get_le16(&src);
+ c->status[i].step_index= (int16_t)bytestream2_get_le16(&c->g);
c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
}
for (i = 0; i < avctx->channels; i++) {
samples = (short *)c->frame.data[0] + i;
cs = &c->status[i];
- for (n = nb_samples >> 1; n > 0; n--, src++) {
- uint8_t v = *src;
+ for (n = nb_samples >> 1; n > 0; n--) {
+ uint8_t v = bytestream2_get_byte(&c->g);
*samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
samples += avctx->channels;
*samples = adpcm_ima_expand_nibble(cs, v >> 4 , 4);
@@ -655,31 +660,32 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
if (avctx->block_align != 0 && buf_size > avctx->block_align)
buf_size = avctx->block_align;
- block_predictor = av_clip(*src++, 0, 6);
+ block_predictor = av_clip(bytestream2_get_byte(&c->g), 0, 6);
c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
if (st) {
- block_predictor = av_clip(*src++, 0, 6);
+ block_predictor = av_clip(bytestream2_get_byte(&c->g), 0, 6);
c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
}
- c->status[0].idelta = (int16_t)bytestream_get_le16(&src);
+ c->status[0].idelta = (int16_t)bytestream2_get_le16(&c->g);
if (st){
- c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
+ c->status[1].idelta = (int16_t)bytestream2_get_le16(&c->g);
}
- c->status[0].sample1 = bytestream_get_le16(&src);
- if (st) c->status[1].sample1 = bytestream_get_le16(&src);
- c->status[0].sample2 = bytestream_get_le16(&src);
- if (st) c->status[1].sample2 = bytestream_get_le16(&src);
+ c->status[0].sample1 = bytestream2_get_le16(&c->g);
+ if (st) c->status[1].sample1 = bytestream2_get_le16(&c->g);
+ c->status[0].sample2 = bytestream2_get_le16(&c->g);
+ if (st) c->status[1].sample2 = bytestream2_get_le16(&c->g);
*samples++ = c->status[0].sample2;
if (st) *samples++ = c->status[1].sample2;
*samples++ = c->status[0].sample1;
if (st) *samples++ = c->status[1].sample1;
- for(n = (nb_samples - 2) >> (1 - st); n > 0; n--, src++) {
- *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4 );
- *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
+ for(n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
+ unsigned int b = bytestream2_get_byte(&c->g);
+ *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], b >> 4 );
+ *samples++ = adpcm_ms_expand_nibble(&c->status[st], b & 0x0F);
}
break;
}
@@ -689,13 +695,13 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
for (channel = 0; channel < avctx->channels; channel++) {
cs = &c->status[channel];
- cs->predictor = (int16_t)bytestream_get_le16(&src);
- cs->step_index = *src++;
- src++;
+ cs->predictor = (int16_t)bytestream2_get_le16(&c->g);
+ cs->step_index = bytestream2_get_byte(&c->g);
+ bytestream2_skip(&c->g, 1);
*samples++ = cs->predictor;
}
- for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
- uint8_t v = *src;
+ for (n = nb_samples >> (1 - st); n > 0; n--) {
+ uint8_t v = bytestream2_get_byte(&c->g);
*samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3);
*samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
}
@@ -711,12 +717,12 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
if (avctx->block_align != 0 && buf_size > avctx->block_align)
buf_size = avctx->block_align;
- c->status[0].predictor = (int16_t)AV_RL16(src + 10);
- c->status[1].predictor = (int16_t)AV_RL16(src + 12);
- c->status[0].step_index = src[14];
- c->status[1].step_index = src[15];
+ bytestream2_skip(&c->g, 10);
+ c->status[0].predictor = (int16_t)bytestream2_get_le16(&c->g);
+ c->status[1].predictor = (int16_t)bytestream2_get_le16(&c->g);
+ c->status[0].step_index = bytestream2_get_byte(&c->g);
+ c->status[1].step_index = bytestream2_get_byte(&c->g);
/* sign extend the predictors */
- src += 16;
diff_channel = c->status[1].predictor;
/* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
@@ -753,14 +759,14 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
case CODEC_ID_ADPCM_IMA_ISS:
for (channel = 0; channel < avctx->channels; channel++) {
cs = &c->status[channel];
- cs->predictor = (int16_t)bytestream_get_le16(&src);
- cs->step_index = *src++;
- src++;
+ cs->predictor = (int16_t)bytestream2_get_le16(&c->g);
+ cs->step_index = bytestream2_get_byte(&c->g);
+ bytestream2_skip(&c->g, 1);
}
- for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
+ for (n = nb_samples >> (1 - st); n > 0; n--) {
uint8_t v1, v2;
- uint8_t v = *src;
+ uint8_t v = bytestream2_get_byte(&c->g);
/* nibbles are swapped for mono */
if (st) {
v1 = v >> 4;
@@ -774,38 +780,42 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
}
break;
case CODEC_ID_ADPCM_IMA_WS:
- while (src < buf + buf_size) {
- uint8_t v = *src++;
+ while (bytestream2_get_bytes_left(&c->g)) {
+ uint8_t v = bytestream2_get_byteu(&c->g);
*samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3);
*samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
}
break;
- case CODEC_ID_ADPCM_XA:
- while (buf_size >= 128) {
- xa_decode(samples, src, &c->status[0], &c->status[1],
- avctx->channels);
- src += 128;
+ case CODEC_ID_ADPCM_XA: {
+ unsigned char buffer[128];
+
+ while (bytestream2_get_bytes_left(&c->g) >= 128) {
+ bytestream2_get_buffer(&c->g, buffer, 128);
+ xa_decode(samples, buffer, &c->status[0], &c->status[1],
+ avctx->channels);
samples += 28 * 8;
buf_size -= 128;
}
break;
+ }
case CODEC_ID_ADPCM_IMA_EA_EACS:
- src += 4; // skip sample count (already read)
for (i=0; i<=st; i++)
- c->status[i].step_index = bytestream_get_le32(&src);
+ c->status[i].step_index = bytestream2_get_le32(&c->g);
for (i=0; i<=st; i++)
- c->status[i].predictor = bytestream_get_le32(&src);
+ c->status[i].predictor = bytestream2_get_le32(&c->g);
- for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
- *samples++ = adpcm_ima_expand_nibble(&c->status[0], *src>>4, 3);
- *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
+ for (n = nb_samples >> (1 - st); n > 0; n--) {
+ unsigned int b = bytestream2_get_byte(&c->g);
+ *samples++ = adpcm_ima_expand_nibble(&c->status[0], b >> 4, 3);
+ *samples++ = adpcm_ima_expand_nibble(&c->status[st], b & 0x0F, 3);
}
break;
case CODEC_ID_ADPCM_IMA_EA_SEAD:
- for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
- *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4,
6);
- *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F,
6);
+ for (n = nb_samples >> (1 - st); n > 0; n--) {
+ unsigned int b = bytestream2_get_byte(&c->g);
+ *samples++ = adpcm_ima_expand_nibble(&c->status[0], b >> 4, 6);
+ *samples++ = adpcm_ima_expand_nibble(&c->status[st], b & 0x0F, 6);
}
break;
case CODEC_ID_ADPCM_EA:
@@ -819,28 +829,26 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
/* 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 = (int16_t)bytestream2_get_le16(&c->g);
+ previous_left_sample = (int16_t)bytestream2_get_le16(&c->g);
+ current_right_sample = (int16_t)bytestream2_get_le16(&c->g);
+ previous_right_sample = (int16_t)bytestream2_get_le16(&c->g);
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++;
+ unsigned int b = bytestream2_get_be16(&c->g);
+ coeff1l = ea_adpcm_table[ b >> 12 ];
+ coeff2l = ea_adpcm_table[ (b >> 12) + 4];
+ coeff1r = ea_adpcm_table[ (b >> 8 ) & 0x0F];
+ coeff2r = ea_adpcm_table[((b >> 8 ) & 0x0F) + 4];
- shift_left = 20 - (*src >> 4);
- shift_right = 20 - (*src & 0x0F);
- src++;
+ shift_left = 20 - ((b & 0xFF) >> 4);
+ shift_right = 20 - ( b & 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++;
+ unsigned int b = bytestream2_get_byte(&c->g);
+ next_left_sample = sign_extend(b >> 4, 4) << shift_left;
+ next_right_sample = sign_extend(b, 4) << shift_right;
next_left_sample = (next_left_sample +
(current_left_sample * coeff1l) +
@@ -858,8 +866,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
}
}
- if (src - buf == buf_size - 2)
- src += 2; // Skip terminating 0x0000
+ if (bytestream2_get_bytes_left(&c->g) == 2)
+ bytestream2_skip(&c->g, 2); // Skip terminating 0x0000
break;
}
@@ -868,15 +876,18 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
int coeff[2][2], shift[2];
for(channel = 0; channel < avctx->channels; channel++) {
- for (i=0; i<2; i++)
- coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
- shift[channel] = 20 - (*src & 0x0F);
- src++;
+ unsigned int b = bytestream2_get_byte(&c->g);
+ coeff[channel][0] = ea_adpcm_table[(b >> 4)];
+ coeff[channel][1] = ea_adpcm_table[(b >> 4) + 4];
+ shift[channel] = 20 - (b & 0x0F);
}
for (count1 = 0; count1 < nb_samples / 2; count1++) {
+ unsigned int bytes[2];
+ for (channel = 0; channel < avctx->channels; channel++)
+ bytes[channel] = bytestream2_get_byte(&c->g);
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];
+ int32_t sample = sign_extend(bytes[channel] >> i, 4) <<
shift[channel];
sample = (sample +
c->status[channel].sample1 * coeff[channel][0] +
c->status[channel].sample2 * coeff[channel][1] +
0x80) >> 8;
@@ -885,10 +896,9 @@ 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_skip(&c->g, bytestream2_get_bytes_left(&c->g));
break;
}
case CODEC_ID_ADPCM_EA_R1:
@@ -904,51 +914,65 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
uint8_t shift;
unsigned int channel;
uint16_t *samplesC;
- const uint8_t *srcC;
- const uint8_t *src_end = buf + buf_size;
int count = 0;
+ int header_end, offset[6];
+
+ for (channel=0; channel<avctx->channels; channel++) {
+ offset[channel] = (big_endian ? bytestream2_get_be32(&c->g)
+ : bytestream2_get_le32(&c->g));
+
+ if (offset[channel] < 0)
+ return AVERROR_INVALIDDATA;
+ }
- src += 4; // skip sample count (already read)
+ header_end = bytestream2_tell(&c->g);
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;
+ bytestream2_seek(&c->g, header_end + offset[channel], SEEK_SET);
+ if (bytestream2_get_bytes_left(&c->g) < 1) {
+ av_log(avctx, AV_LOG_ERROR, "invalid channel offset\n");
+ return AVERROR_INVALIDDATA;
+ }
- if ((offset < 0) || (offset >= src_end - src - 4)) break;
- srcC = src + offset;
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(&c->g), 16);
+ previous_sample = sign_extend(bytestream2_get_le16(&c->g), 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);
+ if (bytestream2_peek_byte(&c->g) == 0xEE) { /* only seen in
R2 and R3 */
+ bytestream2_skip(&c->g, 1);
+ if (bytestream2_get_bytes_left(&c->g) < 30 * 2)
+ break;
+ current_sample =
sign_extend(bytestream2_get_be16u(&c->g), 16);
+ previous_sample =
sign_extend(bytestream2_get_be16u(&c->g), 16);
for (count2=0; count2<28; count2++) {
- *samplesC = (int16_t)bytestream_get_be16(&srcC);
+ *samplesC = sign_extend(bytestream2_get_be16u(&c->g),
16);
samplesC += avctx->channels;
}
} else {
- coeff1 = ea_adpcm_table[ *srcC>>4 ];
- coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
- shift = 20 - (*srcC++ & 0x0F);
+ unsigned int v;
+ v = bytestream2_get_byte(&c->g);
+ coeff1 = ea_adpcm_table[ v >> 4];
+ coeff2 = ea_adpcm_table[(v >> 4) + 4];
+ shift = 20 - (v & 0x0F);
+
+ if (bytestream2_get_bytes_left(&c->g) < 14)
+ break;
- 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(v, 4) << shift;
+ else {
+ v = bytestream2_get_byte(&c->g);
+ next_sample = sign_extend(v >> 4, 4) << shift;
+ }
next_sample += (current_sample * coeff1) +
(previous_sample * coeff2);
@@ -975,26 +999,31 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
}
c->frame.nb_samples = count * 28;
- src = src_end;
+ bytestream2_skip(&c->g, bytestream2_get_bytes_left(&c->g));
break;
}
case CODEC_ID_ADPCM_EA_XAS:
for (channel=0; channel<avctx->channels; channel++) {
int coeff[2][4], shift[4];
short *s2, *s = &samples[channel];
+ unsigned int bytes1, bytes2;
for (n=0; n<4; n++, s+=32*avctx->channels) {
- 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);
+ bytes1 = bytestream2_get_le16(&c->g);
+ bytes2 = bytestream2_get_le16(&c->g);
+
+ coeff[0][n] = ea_adpcm_table[ bytes1 & 0x0F];
+ coeff[1][n] = ea_adpcm_table[(bytes1 & 0x0F) + 4];
+ shift[n] = 20 - (bytes2 & 0x0F);
+ s[0] = bytes1 & 0xFFF0;
+ s[avctx->channels] = bytes2 & 0xFFF0;
}
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) {
+ unsigned int v = bytestream2_get_byte(&c->g);
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(v >> (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);
@@ -1006,19 +1035,21 @@ 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 = bytestream_get_le16(&src);
- src += 4;
+ c->status[0].predictor = sign_extend(bytestream2_get_le16(&c->g),
16);
+ c->status[0].step_index = bytestream2_get_le16(&c->g);
+ bytestream2_skip(&c->g, 4);
} else {
- c->status[0].predictor = sign_extend(bytestream_get_be16(&src),
16);
- c->status[0].step_index = bytestream_get_byte(&src);
- src += 1;
+ c->status[0].predictor = sign_extend(bytestream2_get_be16(&c->g),
16);
+ c->status[0].step_index = bytestream2_get_byte(&c->g);
+ bytestream2_skip(&c->g, 1);
}
- for (n = nb_samples >> (1 - st); n > 0; n--, src++) {
+ for (n = nb_samples >> (1 - st); n > 0; n--) {
+ unsigned int b;
char hi, lo;
- lo = *src & 0x0F;
- hi = *src >> 4;
+ b = bytestream2_get_byte(&c->g);
+ lo = b & 0x0F;
+ hi = b >> 4;
if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
FFSWAP(char, hi, lo);
@@ -1030,8 +1061,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
}
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--) {
+ uint8_t v = bytestream2_get_byte(&c->g);
*samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 );
*samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
}
@@ -1041,38 +1072,41 @@ 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_byte(&c->g) - 0x80);
if (st)
- *samples++ = 128 * (*src++ - 0x80);
+ *samples++ = 128 * (bytestream2_get_byte(&c->g) - 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--) {
+ unsigned int b = bytestream2_get_byte(&c->g);
*samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
- src[0] >> 4, 4, 0);
+ b >> 4, 4, 0);
*samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
- src[0] & 0x0F, 4, 0);
+ b & 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--) {
+ unsigned int b = bytestream2_get_byte(&c->g);
*samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
- src[0] >> 5 , 3, 0);
+ b >> 5 , 3, 0);
*samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
- (src[0] >> 2) & 0x07, 3, 0);
+ (b >> 2) & 0x07, 3, 0);
*samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
- src[0] & 0x03, 2, 0);
+ b & 0x03, 2, 0);
}
} else {
- for (n = nb_samples >> (2 - st); n > 0; n--, src++) {
+ for (n = nb_samples >> (2 - st); n > 0; n--) {
+ unsigned int b = bytestream2_get_byte(&c->g);
*samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
- src[0] >> 6 , 2, 2);
+ b >> 6 , 2, 2);
*samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
- (src[0] >> 4) & 0x03, 2, 2);
+ (b >> 4) & 0x03, 2, 2);
*samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
- (src[0] >> 2) & 0x03, 2, 2);
+ (b >> 2) & 0x03, 2, 2);
*samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
- src[0] & 0x03, 2, 2);
+ b & 0x03, 2, 2);
}
}
break;
@@ -1130,12 +1164,12 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
void *data,
}
}
}
- src += buf_size;
+ bytestream2_skip(&c->g, bytestream2_get_bytes_left(&c->g));
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--) {
+ uint8_t v = bytestream2_get_byte(&c->g);
*samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F);
*samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 );
}
@@ -1146,31 +1180,33 @@ 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);
+ table[0][i] = (int16_t)bytestream2_get_be16(&c->g);
/* Initialize the previous sample. */
for (i = 0; i < 4; i++)
- prev[0][i] = (int16_t)bytestream_get_be16(&src);
+ prev[0][i] = (int16_t)bytestream2_get_be16(&c->g);
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;
+ unsigned int b = bytestream2_get_byte(&c->g);
+ int index = (b >> 4) & 7;
+ unsigned int exp = b & 15;
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(b, 4);
+ else {
+ b = bytestream2_get_byte(&c->g);
+ sampledat = sign_extend(b >> 4, 4);
+ }
sampledat = ((prev[ch][0]*factor1
+ prev[ch][1]*factor2) >> 11) + (sampledat <<
exp);
@@ -1194,7 +1230,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void
*data,
*got_frame_ptr = 1;
*(AVFrame *)data = c->frame;
- return src - buf;
+ return bytestream2_tell(&c->g);
}
--
1.7.7.3
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel