On 01/07/2012 06:43 PM, Aneesh Dogra wrote:

> ---
>  libavcodec/adpcm.c |  329 
> ++++++++++++++++++++++++++++------------------------
>  1 files changed, 177 insertions(+), 152 deletions(-)
> 
> diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c
> index b319635..7e8febb 100644
> --- a/libavcodec/adpcm.c
> +++ b/libavcodec/adpcm.c
> @@ -504,8 +504,8 @@ static int get_nb_samples(AVCodecContext *avctx, const 
> uint8_t *buf,


would be good to also use bytestream2 functions in get_nb_samples()
instead of passing buf/buf_size.

>      { \
>          if (end_of_packet) \
>              break; \
> -        last_byte = *src++; \
> -        if (src >= buf + buf_size) \
> +        last_byte = bytestream2_get_byte(&g); \
> +        if (!bytestream2_get_bytes_left(&g)) \
>              end_of_packet = 1; \
>          nibble = last_byte & 0x0F; \
>          decode_top_nibble_next = 1; \
> @@ -520,7 +520,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void 
> *data,
>      ADPCMChannelStatus *cs;
>      int n, m, channel, i;
>      short *samples;
> -    const uint8_t *src;
> +    GetByteContext g;
>      int st; /* stereo */
>      int count1, count2;
>      int nb_samples, coded_samples, ret;
> @@ -547,7 +547,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void 
> *data,
>          c->frame.nb_samples = nb_samples = coded_samples;
>      }
>  
> -    src = buf;
> +    bytestream2_init(&g, buf, buf_size);
>  
>      st = avctx->channels == 2 ? 1 : 0;
>  
> @@ -562,12 +562,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(&g);
>              step_index = predictor & 0x7F;
>              predictor &= 0xFF80;


align the =

>  
> -            src += 2;
> -
>              if (cs->step_index == step_index) {
>                  int diff = (int)predictor - cs->predictor;
>                  if (diff < 0)
> @@ -588,11 +586,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(&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;
> @@ -602,21 +600,22 @@ static int adpcm_decode_frame(AVCodecContext *avctx, 
> void *data,
>  
>          for(i=0; i<avctx->channels; i++){
>              cs = &(c->status[i]);
> -            cs->predictor = *samples++ = (int16_t)bytestream_get_le16(&src);
> +            cs->predictor = *samples++ = (int16_t)bytestream2_get_le16(&g);


ideally we should go ahead and change all these unsigned-to-signed casts
to sign_extend(), but i'm ok with doing that in a separate follow-up patch.

>  
> -            cs->step_index = *src++;
> +            cs->step_index = bytestream2_get_byte(&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 */
> +            if (bytestream2_peek_byte(&g)) av_log(avctx, AV_LOG_ERROR, 
> "unused byte should be null but is %d!!\n", bytestream2_peek_byte(&g)); /* 
> unused */
> +            bytestream2_skip(&g, 1);


put the av_log() on its own line and avoid going over 80 chars

>          }
>  
>          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(&g);
>                      *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
>                      samples += avctx->channels;
>                      *samples = adpcm_ima_expand_nibble(cs, v >> 4  , 3);
> @@ -629,18 +628,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(&g);


add a space before the =

>  
>          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(&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(&g);
>                  *samples = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
>                  samples += avctx->channels;
>                  *samples = adpcm_ima_expand_nibble(cs, v >> 4  , 4);
> @@ -655,31 +654,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(&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(&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(&g);
>          if (st){
> -            c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
> +            c->status[1].idelta = (int16_t)bytestream2_get_le16(&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(&g);
> +        if (st) c->status[1].sample1 = bytestream2_get_le16(&g);
> +        c->status[0].sample2 = bytestream2_get_le16(&g);
> +        if (st) c->status[1].sample2 = bytestream2_get_le16(&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(&g);
> +            *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], b >> 4  );
> +            *samples++ = adpcm_ms_expand_nibble(&c->status[st], b & 0x0F);
>          }
>          break;
>      }
> @@ -689,13 +689,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(&g);
> +            cs->step_index = bytestream2_get_byte(&g);
> +            bytestream2_skip(&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(&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 +711,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(&g, 10);
> +        c->status[0].predictor  = (int16_t)bytestream2_get_le16(&g);
> +        c->status[1].predictor  = (int16_t)bytestream2_get_le16(&g);
> +        c->status[0].step_index = bytestream2_get_byte(&g);
> +        c->status[1].step_index = bytestream2_get_byte(&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 +753,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(&g);
> +            cs->step_index = bytestream2_get_byte(&g);
> +            bytestream2_skip(&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(&g);
>              /* nibbles are swapped for mono */
>              if (st) {
>                  v1 = v >> 4;
> @@ -774,38 +774,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(&g)) {
> +            uint8_t v = bytestream2_get_byteu(&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],
> +    case CODEC_ID_ADPCM_XA: {
> +        unsigned char buffer[128];
> +        while (bytestream2_get_bytes_left(&g) >= 128) {
> +            bytestream2_get_buffer(&g, buffer, 128);
> +            xa_decode(samples, buffer, &c->status[0], &c->status[1],
>                  avctx->channels);
> -            src += 128;
>              samples += 28 * 8;
>              buf_size -= 128;
>          }
>          break;
> +    }
>      case CODEC_ID_ADPCM_IMA_EA_EACS:
> -        src += 4; // skip sample count (already read)
> +        bytestream2_skip(&g, 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(&g);
>          for (i=0; i<=st; i++)
> -            c->status[i].predictor  = bytestream_get_le32(&src);
> +            c->status[i].predictor  = bytestream2_get_le32(&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(&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(&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 +823,27 @@ 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)
> +        bytestream2_skip(&g, 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(&g);
> +        previous_left_sample  = (int16_t)bytestream2_get_le16(&g);
> +        current_right_sample  = (int16_t)bytestream2_get_le16(&g);
> +        previous_right_sample = (int16_t)bytestream2_get_le16(&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(&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(&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 +861,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void 
> *data,
>              }
>          }
>  
> -        if (src - buf == buf_size - 2)
> -            src += 2; // Skip terminating 0x0000
> +        if (bytestream2_tell(&g) == buf_size - 2)
> +            bytestream2_skip(&g, 2); // Skip terminating 0x0000


if (bytestream2_get_bytes_left(&g) == 2)

>  
>          break;
>      }
> @@ -868,15 +871,17 @@ 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++;
> +            coeff[channel][0] = ea_adpcm_table[(bytestream2_peek_byte(&g) >> 
> 4)];
> +            coeff[channel][1] = ea_adpcm_table[(bytestream2_peek_byte(&g) >> 
> 4) + 4];
> +            shift[channel] = 20 - (bytestream2_get_byte(&g) & 0x0F);


read to a local variable to avoid the peeks

>          }
>          for (count1 = 0; count1 < nb_samples / 2; count1++) {
> +            unsigned int bytes[2];
> +            for (channel = 0; channel < avctx->channels; channel++)
> +                bytes[channel] = bytestream2_get_byte(&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 +890,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(&g, bytestream2_get_bytes_left(&g));
>          break;
>      }
>      case CODEC_ID_ADPCM_EA_R1:
> @@ -904,51 +908,63 @@ 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];
> +
> +        bytestream2_skip(&g, 4); // skip sample count (already read)
> +
> +        for (channel=0; channel<avctx->channels; channel++) {
> +            offset[channel] = (big_endian ? bytestream2_get_be32(&g)
> +                                         : bytestream2_get_le32(&g));


fix the vertical alignment

> +
> +            if (offset[channel] < 0)
> +                return AVERROR_INVALIDDATA;
> +            }


wrong alignment on the closing brace

>  
> -        src += 4; // skip sample count (already read)
> +        header_end = bytestream2_tell(&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(&g, header_end + offset[channel], SEEK_SET);
> +            if (bytestream2_get_bytes_left(&g) < 1) {
> +                av_log(avctx, AV_LOG_ERROR, "invalid channel offset\n");
> +                return AVERROR_INVALIDDATA;
> +                }


wrong alignment on the closing brace

also, if you check for 5 bytes instead of 2, the reads below can be
unchecked (e.g. get_le16u)

>  
> -            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(&g), 16);
> +                previous_sample = sign_extend(bytestream2_get_le16(&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(&g) == 0xEE) {  /* only seen in R2 
> and R3 */
> +                    bytestream2_skip(&g, 1);
> +                    if (bytestream2_get_bytes_left(&g) < 30*2) break;
> +                    current_sample  = sign_extend(bytestream2_get_be16(&g), 
> 16);
> +                    previous_sample = sign_extend(bytestream2_get_be16(&g), 
> 16);
>  
>                      for (count2=0; count2<28; count2++) {
> -                        *samplesC = (int16_t)bytestream_get_be16(&srcC);
> +                        *samplesC = sign_extend(bytestream2_get_be16(&g), 
> 16);
>                          samplesC += avctx->channels;
>                      }
>                  } else {
> -                    coeff1 = ea_adpcm_table[ *srcC>>4     ];
> -                    coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
> -                    shift = 20 - (*srcC++ & 0x0F);
> -
> -                    if (srcC > src_end - 14) break;
> +                    unsigned int v;
> +                    v = bytestream2_get_byte(&g);
> +                    coeff1 = ea_adpcm_table[ v >> 4];
> +                    coeff2 = ea_adpcm_table[(v >> 4) + 4];
> +                    shift = 20 - (v & 0x0F);
> +
> +                    if (bytestream2_get_bytes_left(&g) < 14)
> +                        break;
>                      for (count2=0; count2<28; count2++) {
>                          if (count2 & 1)
> -                            next_sample = sign_extend(*srcC++,    4) << 
> shift;
> +                            next_sample = 
> sign_extend(bytestream2_get_byte(&g),    4) << shift;
>                          else
> -                            next_sample = sign_extend(*srcC >> 4, 4) << 
> shift;
> +                            next_sample = 
> sign_extend(bytestream2_peek_byte(&g) >> 4, 4) << shift;


you can avoid a separate peek by reading the byte in the else case
before reading next_sample.

also many of the reads above can be unchecked because you're doing the
check explicitly before the loops.

>  
>                          next_sample += (current_sample  * coeff1) +
>                                         (previous_sample * coeff2);
> @@ -975,26 +991,30 @@ static int adpcm_decode_frame(AVCodecContext *avctx, 
> void *data,
>          }
>  
>          c->frame.nb_samples = count * 28;
> -        src = src_end;
> +        bytestream2_skip(&g, bytestream2_get_bytes_left(&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(&g);
> +                bytes2 = bytestream2_get_le16(&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;


remove the tabs. and align the =.

>              }
>  
>              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, 
> bytestream2_skip(&g, 1)) {
>                      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(bytestream2_peek_byte(&g) >> 
> (4 - i), 4) << shift[n];


bytestream2_read_byte() instead of peek/skip

>                          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 +1026,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(&g), 
> 16);
> +            c->status[0].step_index = bytestream2_get_le16(&g);
> +            bytestream2_skip(&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(&g), 
> 16);
> +            c->status[0].step_index = bytestream2_get_byte(&g);
> +            bytestream2_skip(&g, 1);


align the =

>          }
>  
> -        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(&g);
> +            lo = b & 0x0F;
> +            hi = b >> 4;


align the =

>  
>              if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
>                  FFSWAP(char, hi, lo);
> @@ -1030,8 +1052,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(&g);
>              *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4  );
>              *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
>          }
> @@ -1041,38 +1063,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(&g) - 0x80);
>              if (st)
> -              *samples++ = 128 * (*src++ - 0x80);
> +              *samples++ = 128 * (bytestream2_get_byte(&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(&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);


go ahead and align these correctly since you're changing them.

>              }
>          } 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(&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);


ditto on alignment.

>              }
>          } 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(&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);


and here.

>              }
>          }
>          break;
> @@ -1130,12 +1155,12 @@ static int adpcm_decode_frame(AVCodecContext *avctx, 
> void *data,
>                  }
>              }
>          }
> -        src += buf_size;
> +        bytestream2_skip(&g, bytestream2_get_bytes_left(&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(&g);
>              *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 
> 0x0F);
>              *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4  
> );
>          }
> @@ -1146,31 +1171,31 @@ 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)
> +        bytestream2_skip(&g, 4); // skip channel size
> +        bytestream2_skip(&g, 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(&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(&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;
> +                int index = (bytestream2_peek_byte(&g) >> 4) & 7;
> +                unsigned int exp = bytestream2_get_byte(&g) & 15;


this can use 1 get_byte instead of peek/get

>                  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(bytestream2_get_byte(&g), 4);
> +                    else    sampledat = 
> sign_extend(bytestream2_peek_byte(&g) >> 4, 4);


same here.

>  
>                      sampledat = ((prev[ch][0]*factor1
>                                  + prev[ch][1]*factor2) >> 11) + (sampledat 
> << exp);
> @@ -1194,7 +1219,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, 
> void *data,
>      *got_frame_ptr   = 1;
>      *(AVFrame *)data = c->frame;
>  
> -    return src - buf;
> +    return bytestream2_tell(&g);
>  }
>  
>  


_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to