static int adpcm_decode_frame(AVCodecContext *avctx, void *data,

                              int *got_frame_ptr, AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    int buf_size = avpkt->size;

    ADPCMDecodeContext *c = avctx->priv_data;

    ADPCMChannelStatus *cs;

    int n, m, channel, i;

    short *samples;

    int16_t **samples_p;

    int st; 

    int count1, count2;

    int nb_samples, coded_samples, ret;

    GetByteContext gb;



    bytestream2_init(&gb, buf, buf_size);

    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;

    if ((ret = ff_get_buffer(avctx, &c->frame)) < 0) {

        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");

        return ret;

    }

    samples = (short *)c->frame.data[0];

    samples_p = (int16_t **)c->frame.extended_data;



    

    

    if (coded_samples) {

        if (coded_samples != nb_samples)

            av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");

        c->frame.nb_samples = nb_samples = coded_samples;

    }



    st = avctx->channels == 2 ? 1 : 0;



    switch(avctx->codec->id) {

    case AV_CODEC_ID_ADPCM_IMA_QT:

        



        for (channel = 0; channel < avctx->channels; channel++) {

            int predictor;

            int step_index;

            cs = &(c->status[channel]);

            



            

            predictor = sign_extend(bytestream2_get_be16u(&gb), 16);

            step_index = predictor & 0x7F;

            predictor &= ~0x7F;



            if (cs->step_index == step_index) {

                int diff = predictor - cs->predictor;

                if (diff < 0)

                    diff = - diff;

                if (diff > 0x7f)

                    goto update;

            } else {

            update:

                cs->step_index = step_index;

                cs->predictor = predictor;

            }



            if (cs->step_index > 88u){

                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",

                       channel, cs->step_index);

                return AVERROR_INVALIDDATA;

            }



            samples = samples_p[channel];



            for (m = 0; m < 64; m += 2) {

                int byte = bytestream2_get_byteu(&gb);

                samples[m    ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F, 3);

                samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4  , 3);

            }

        }

        break;

    case AV_CODEC_ID_ADPCM_IMA_WAV:

        for(i=0; i<avctx->channels; i++){

            cs = &(c->status[i]);

            cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);



            cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);

            if (cs->step_index > 88u){

                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",

                       i, cs->step_index);

                return AVERROR_INVALIDDATA;

            }

        }



        for (n = 0; n < (nb_samples - 1) / 8; n++) {

            for (i = 0; i < avctx->channels; i++) {

                cs = &c->status[i];

                samples = &samples_p[i][1 + n * 8];

                for (m = 0; m < 8; m += 2) {

                    int v = bytestream2_get_byteu(&gb);

                    samples[m    ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);

                    samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4  , 3);

                }

            }

        }

        break;

    case AV_CODEC_ID_ADPCM_4XM:

        for (i = 0; i < avctx->channels; i++)

            c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);



        for (i = 0; i < avctx->channels; i++) {

            c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);

            if (c->status[i].step_index > 88u) {

                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",

                       i, c->status[i].step_index);

                return AVERROR_INVALIDDATA;

            }

        }



        for (i = 0; i < avctx->channels; i++) {

            samples = (int16_t *)c->frame.data[i];

            cs = &c->status[i];

            for (n = nb_samples >> 1; n > 0; n--) {

                int v = bytestream2_get_byteu(&gb);

                *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);

                *samples++ = adpcm_ima_expand_nibble(cs, v >> 4  , 4);

            }

        }

        break;

    case AV_CODEC_ID_ADPCM_MS:

    {

        int block_predictor;



        block_predictor = bytestream2_get_byteu(&gb);

        if (block_predictor > 6) {

            av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",

                   block_predictor);

            return AVERROR_INVALIDDATA;

        }

        c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];

        c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];

        if (st) {

            block_predictor = bytestream2_get_byteu(&gb);

            if (block_predictor > 6) {

                av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",

                       block_predictor);

                return AVERROR_INVALIDDATA;

            }

            c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];

            c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];

        }

        c->status[0].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);

        if (st){

            c->status[1].idelta = sign_extend(bytestream2_get_le16u(&gb), 16);

        }



        c->status[0].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);

        if (st) c->status[1].sample1 = sign_extend(bytestream2_get_le16u(&gb), 16);

        c->status[0].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);

        if (st) c->status[1].sample2 = sign_extend(bytestream2_get_le16u(&gb), 16);



        *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--) {

            int byte = bytestream2_get_byteu(&gb);

            *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], byte >> 4  );

            *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);

        }

        break;

    }

    case AV_CODEC_ID_ADPCM_IMA_DK4:

        for (channel = 0; channel < avctx->channels; channel++) {

            cs = &c->status[channel];

            cs->predictor  = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);

            cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);

            if (cs->step_index > 88u){

                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",

                       channel, cs->step_index);

                return AVERROR_INVALIDDATA;

            }

        }

        for (n = nb_samples >> (1 - st); n > 0; n--) {

            int v = bytestream2_get_byteu(&gb);

            *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4  , 3);

            *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);

        }

        break;

    case AV_CODEC_ID_ADPCM_IMA_DK3:

    {

        int last_byte = 0;

        int nibble;

        int decode_top_nibble_next = 0;

        int diff_channel;

        const int16_t *samples_end = samples + avctx->channels * nb_samples;



        bytestream2_skipu(&gb, 10);

        c->status[0].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);

        c->status[1].predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);

        c->status[0].step_index = bytestream2_get_byteu(&gb);

        c->status[1].step_index = bytestream2_get_byteu(&gb);

        if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){

            av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",

                   c->status[0].step_index, c->status[1].step_index);

            return AVERROR_INVALIDDATA;

        }

        

        diff_channel = c->status[1].predictor;



        

#define DK3_GET_NEXT_NIBBLE() \

    if (decode_top_nibble_next) { \

        nibble = last_byte >> 4; \

        decode_top_nibble_next = 0; \

    } else { \

        last_byte = bytestream2_get_byteu(&gb); \

        nibble = last_byte & 0x0F; \

        decode_top_nibble_next = 1; \

    }



        while (samples < samples_end) {



            





            

            DK3_GET_NEXT_NIBBLE();

            adpcm_ima_expand_nibble(&c->status[0], nibble, 3);



            

            DK3_GET_NEXT_NIBBLE();

            adpcm_ima_expand_nibble(&c->status[1], nibble, 3);



            

            diff_channel = (diff_channel + c->status[1].predictor) / 2;

            *samples++ = c->status[0].predictor + c->status[1].predictor;

            *samples++ = c->status[0].predictor - c->status[1].predictor;



            

            DK3_GET_NEXT_NIBBLE();

            adpcm_ima_expand_nibble(&c->status[0], nibble, 3);



            

            diff_channel = (diff_channel + c->status[1].predictor) / 2;

            *samples++ = c->status[0].predictor + c->status[1].predictor;

            *samples++ = c->status[0].predictor - c->status[1].predictor;

        }

        break;

    }

    case AV_CODEC_ID_ADPCM_IMA_ISS:

        for (channel = 0; channel < avctx->channels; channel++) {

            cs = &c->status[channel];

            cs->predictor  = sign_extend(bytestream2_get_le16u(&gb), 16);

            cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);

            if (cs->step_index > 88u){

                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",

                       channel, cs->step_index);

                return AVERROR_INVALIDDATA;

            }

        }



        for (n = nb_samples >> (1 - st); n > 0; n--) {

            int v1, v2;

            int v = bytestream2_get_byteu(&gb);

            

            if (st) {

                v1 = v >> 4;

                v2 = v & 0x0F;

            } else {

                v2 = v >> 4;

                v1 = v & 0x0F;

            }

            *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v1, 3);

            *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);

        }

        break;

    case AV_CODEC_ID_ADPCM_IMA_APC:

        while (bytestream2_get_bytes_left(&gb) > 0) {

            int v = bytestream2_get_byteu(&gb);

            *samples++ = adpcm_ima_expand_nibble(&c->status[0],  v >> 4  , 3);

            *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);

        }

        break;

    case AV_CODEC_ID_ADPCM_IMA_OKI:

        while (bytestream2_get_bytes_left(&gb) > 0) {

            int v = bytestream2_get_byteu(&gb);

            *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0],  v >> 4  );

            *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);

        }

        break;

    case AV_CODEC_ID_ADPCM_IMA_WS:

        if (c->vqa_version == 3) {

            for (channel = 0; channel < avctx->channels; channel++) {

                int16_t *smp = samples_p[channel];



                for (n = nb_samples / 2; n > 0; n--) {

                    int v = bytestream2_get_byteu(&gb);

                    *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);

                    *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);

                }

            }

        } else {

            for (n = nb_samples / 2; n > 0; n--) {

                for (channel = 0; channel < avctx->channels; channel++) {

                    int v = bytestream2_get_byteu(&gb);

                    *samples++  = adpcm_ima_expand_nibble(&c->status[channel], v >> 4  , 3);

                    samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);

                }

                samples += avctx->channels;

            }

        }

        bytestream2_seek(&gb, 0, SEEK_END);

        break;

    case AV_CODEC_ID_ADPCM_XA:

    {

        int16_t *out0 = samples_p[0];

        int16_t *out1 = samples_p[1];

        int samples_per_block = 28 * (3 - avctx->channels) * 4;

        int sample_offset = 0;

        while (bytestream2_get_bytes_left(&gb) >= 128) {

            if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),

                                 &c->status[0], &c->status[1],

                                 avctx->channels, sample_offset)) < 0)

                return ret;

            bytestream2_skipu(&gb, 128);

            sample_offset += samples_per_block;

        }

        break;

    }

    case AV_CODEC_ID_ADPCM_IMA_EA_EACS:

        for (i=0; i<=st; i++) {

            c->status[i].step_index = bytestream2_get_le32u(&gb);

            if (c->status[i].step_index > 88u) {

                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",

                       i, c->status[i].step_index);

                return AVERROR_INVALIDDATA;

            }

        }

        for (i=0; i<=st; i++)

            c->status[i].predictor  = bytestream2_get_le32u(&gb);



        for (n = nb_samples >> (1 - st); n > 0; n--) {

            int byte   = bytestream2_get_byteu(&gb);

            *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   3);

            *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);

        }

        break;

    case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:

        for (n = nb_samples >> (1 - st); n > 0; n--) {

            int byte = bytestream2_get_byteu(&gb);

            *samples++ = adpcm_ima_expand_nibble(&c->status[0],  byte >> 4,   6);

            *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);

        }

        break;

    case AV_CODEC_ID_ADPCM_EA:

    {

        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;



        





        if(avctx->channels != 2)

            return AVERROR_INVALIDDATA;



        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++) {

            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];



            byte = bytestream2_get_byteu(&gb);

            shift_left  = 20 - (byte >> 4);

            shift_right = 20 - (byte & 0x0F);



            for (count2 = 0; count2 < 28; count2++) {

                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) +

                    (previous_left_sample * coeff2l) + 0x80) >> 8;

                next_right_sample = (next_right_sample +

                    (current_right_sample * coeff1r) +

                    (previous_right_sample * coeff2r) + 0x80) >> 8;



                previous_left_sample = current_left_sample;

                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++ = current_left_sample;

                *samples++ = current_right_sample;

            }

        }



        bytestream2_skip(&gb, 2); 



        break;

    }

    case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:

    {

        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[(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) { 

                for(channel = 0; channel < avctx->channels; 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;

                    c->status[channel].sample2 = c->status[channel].sample1;

                    c->status[channel].sample1 = av_clip_int16(sample);

                    *samples++ = c->status[channel].sample1;

                }

            }

        }

        bytestream2_seek(&gb, 0, SEEK_END);

        break;

    }

    case AV_CODEC_ID_ADPCM_EA_R1:

    case AV_CODEC_ID_ADPCM_EA_R2:

    case AV_CODEC_ID_ADPCM_EA_R3: {

        







        const int big_endian = avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R3;

        int previous_sample, current_sample, next_sample;

        int coeff1, coeff2;

        int shift;

        unsigned int channel;

        uint16_t *samplesC;

        int count = 0;

        int offsets[6];



        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++) {

            bytestream2_seek(&gb, offsets[channel], SEEK_SET);

            samplesC = samples_p[channel];



            if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) {

                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++) {

                int byte = bytestream2_get_byte(&gb);

                if (byte == 0xEE) {  

                    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++ = sign_extend(bytestream2_get_be16(&gb), 16);

                } else {

                    coeff1 = ea_adpcm_table[ byte >> 4     ];

                    coeff2 = ea_adpcm_table[(byte >> 4) + 4];

                    shift = 20 - (byte & 0x0F);



                    for (count2=0; count2<28; count2++) {

                        if (count2 & 1)

                            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);

                        next_sample = av_clip_int16(next_sample >> 8);



                        previous_sample = current_sample;

                        current_sample  = next_sample;

                        *samplesC++ = current_sample;

                    }

                }

            }

            if (!count) {

                count = count1;

            } else if (count != count1) {

                av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");

                count = FFMAX(count, count1);

            }



            if (avctx->codec->id != AV_CODEC_ID_ADPCM_EA_R1) {

                c->status[channel].predictor   = current_sample;

                c->status[channel].prev_sample = previous_sample;

            }

        }



        c->frame.nb_samples = count * 28;

        bytestream2_seek(&gb, 0, SEEK_END);

        break;

    }

    case AV_CODEC_ID_ADPCM_EA_XAS:

        for (channel=0; channel<avctx->channels; channel++) {

            int coeff[2][4], shift[4];

            int16_t *s = samples_p[channel];

            for (n = 0; n < 4; n++, s += 32) {

                int val = sign_extend(bytestream2_get_le16u(&gb), 16);

                for (i=0; i<2; i++)

                    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[1] = val & ~0x0F;

            }



            for (m=2; m<32; m+=2) {

                s = &samples_p[channel][m];

                for (n = 0; n < 4; n++, s += 32) {

                    int level, pred;

                    int byte = bytestream2_get_byteu(&gb);



                    level = sign_extend(byte >> 4, 4) << shift[n];

                    pred  = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];

                    s[0]  = av_clip_int16((level + pred + 0x80) >> 8);



                    level = sign_extend(byte, 4) << shift[n];

                    pred  = s[0] * coeff[0][n] + s[-1] * coeff[1][n];

                    s[1]  = av_clip_int16((level + pred + 0x80) >> 8);

                }

            }

        }

        break;

    case AV_CODEC_ID_ADPCM_IMA_AMV:

        c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);

        c->status[0].step_index = bytestream2_get_le16u(&gb);

        bytestream2_skipu(&gb, 4);

        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--) {

            int v = bytestream2_get_byteu(&gb);



            *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);

            *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);

        }

        break;

    case AV_CODEC_ID_ADPCM_IMA_SMJPEG:

        for (i = 0; i < avctx->channels; i++) {

            c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);

            c->status[i].step_index = bytestream2_get_byteu(&gb);

            bytestream2_skipu(&gb, 1);

            if (c->status[i].step_index > 88u) {

                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",

                       c->status[i].step_index);

                return AVERROR_INVALIDDATA;

            }

        }



        for (n = nb_samples >> (1 - st); n > 0; n--) {

            int v = bytestream2_get_byteu(&gb);



            *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4, 3);

            *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf, 3);

        }

        break;

    case AV_CODEC_ID_ADPCM_CT:

        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);

        }

        break;

    case AV_CODEC_ID_ADPCM_SBPRO_4:

    case AV_CODEC_ID_ADPCM_SBPRO_3:

    case AV_CODEC_ID_ADPCM_SBPRO_2:

        if (!c->status[0].step_index) {

            

            *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);

            if (st)

                *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80);

            c->status[0].step_index = 1;

            nb_samples--;

        }

        if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) {

            for (n = nb_samples >> (1 - st); n > 0; n--) {

                int byte = bytestream2_get_byteu(&gb);

                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],

                                                       byte >> 4,   4, 0);

                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],

                                                       byte & 0x0F, 4, 0);

            }

        } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) {

            for (n = nb_samples / 3; n > 0; n--) {

                int byte = bytestream2_get_byteu(&gb);

                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],

                                                        byte >> 5        , 3, 0);

                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],

                                                       (byte >> 2) & 0x07, 3, 0);

                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],

                                                        byte & 0x03,       2, 0);

            }

        } else {

            for (n = nb_samples >> (2 - st); n > 0; n--) {

                int byte = bytestream2_get_byteu(&gb);

                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],

                                                        byte >> 6        , 2, 2);

                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],

                                                       (byte >> 4) & 0x03, 2, 2);

                *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],

                                                       (byte >> 2) & 0x03, 2, 2);

                *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],

                                                        byte & 0x03,       2, 2);

            }

        }

        break;

    case AV_CODEC_ID_ADPCM_SWF:

        adpcm_swf_decode(avctx, buf, buf_size, samples);

        bytestream2_seek(&gb, 0, SEEK_END);

        break;

    case AV_CODEC_ID_ADPCM_YAMAHA:

        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  );

        }

        break;

    case AV_CODEC_ID_ADPCM_AFC:

    {

        int samples_per_block;

        int blocks;



        if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {

            samples_per_block = avctx->extradata[0] / 16;

            blocks = nb_samples / avctx->extradata[0];

        } else {

            samples_per_block = nb_samples / 16;

            blocks = 1;

        }



        for (m = 0; m < blocks; m++) {

        for (channel = 0; channel < avctx->channels; channel++) {

            int prev1 = c->status[channel].sample1;

            int prev2 = c->status[channel].sample2;



            samples = samples_p[channel] + m * 16;

            

            for (i = 0; i < samples_per_block; i++) {

                int byte = bytestream2_get_byteu(&gb);

                int scale = 1 << (byte >> 4);

                int index = byte & 0xf;

                int factor1 = ff_adpcm_afc_coeffs[0][index];

                int factor2 = ff_adpcm_afc_coeffs[1][index];



                

                for (n = 0; n < 16; n++) {

                    int32_t sampledat;



                    if (n & 1) {

                        sampledat = sign_extend(byte, 4);

                    } else {

                        byte = bytestream2_get_byteu(&gb);

                        sampledat = sign_extend(byte >> 4, 4);

                    }



                    sampledat = ((prev1 * factor1 + prev2 * factor2) +

                                 ((sampledat * scale) << 11)) >> 11;

                    *samples = av_clip_int16(sampledat);

                    prev2 = prev1;

                    prev1 = *samples++;

                }

            }



            c->status[channel].sample1 = prev1;

            c->status[channel].sample2 = prev2;

        }

        }

        bytestream2_seek(&gb, 0, SEEK_END);

        break;

    }

    case AV_CODEC_ID_ADPCM_THP:

    {

        int table[6][16];

        int ch;



        for (i = 0; i < avctx->channels; i++)

            for (n = 0; n < 16; n++)

                table[i][n] = sign_extend(bytestream2_get_be16u(&gb), 16);



        

        for (i = 0; i < avctx->channels; i++) {

            c->status[i].sample1 = sign_extend(bytestream2_get_be16u(&gb), 16);

            c->status[i].sample2 = sign_extend(bytestream2_get_be16u(&gb), 16);

        }



        for (ch = 0; ch < avctx->channels; ch++) {

            samples = samples_p[ch];



            

            for (i = 0; i < nb_samples / 14; i++) {

                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];



                

                for (n = 0; n < 14; n++) {

                    int32_t sampledat;



                    if (n & 1) {

                        sampledat = sign_extend(byte, 4);

                    } else {

                        byte = bytestream2_get_byteu(&gb);

                        sampledat = sign_extend(byte >> 4, 4);

                    }



                    sampledat = ((c->status[ch].sample1 * factor1

                                + c->status[ch].sample2 * factor2) >> 11) + (sampledat << exp);

                    *samples = av_clip_int16(sampledat);

                    c->status[ch].sample2 = c->status[ch].sample1;

                    c->status[ch].sample1 = *samples++;

                }

            }

        }

        break;

    }



    default:

        return -1;

    }



    if (avpkt->size && bytestream2_tell(&gb) == 0) {

        av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");

        return AVERROR_INVALIDDATA;

    }



    *got_frame_ptr   = 1;

    *(AVFrame *)data = c->frame;



    return bytestream2_tell(&gb);

}
