static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
                          int buf_size, int *coded_samples)
{
    ADPCMDecodeContext *s = avctx->priv_data;
    int nb_samples        = 0;
    int ch                = avctx->channels;
    int has_coded_samples = 0;
    int header_size;
    *coded_samples = 0;
    switch (avctx->codec->id) {
    /* constant, only check buf_size */
    case CODEC_ID_ADPCM_EA_XAS:
        if (buf_size < 76 * ch)
        nb_samples = 128;
        break;
    case CODEC_ID_ADPCM_IMA_QT:
        if (buf_size < 34 * ch)
        nb_samples = 64;
        break;
    /* simple 4-bit adpcm */
    case CODEC_ID_ADPCM_CT:
    case CODEC_ID_ADPCM_IMA_EA_SEAD:
    case CODEC_ID_ADPCM_IMA_WS:
    case CODEC_ID_ADPCM_YAMAHA:
        nb_samples = buf_size * 2 / ch;
        break;
    }
    if (nb_samples)
        return nb_samples;
    /* simple 4-bit adpcm, with header */
    header_size = 0;
    switch (avctx->codec->id) {
        case CODEC_ID_ADPCM_4XM:
        case CODEC_ID_ADPCM_IMA_ISS:     header_size = 4 * ch;      break;
        case CODEC_ID_ADPCM_IMA_AMV:     header_size = 8;           break;
        case CODEC_ID_ADPCM_IMA_SMJPEG:  header_size = 4;           break;
    }
    if (header_size > 0)
        return (buf_size - header_size) * 2 / ch;
    /* more complex formats */
    switch (avctx->codec->id) {
    case CODEC_ID_ADPCM_EA:
        has_coded_samples = 1;
        if (buf_size < 4)
        *coded_samples  = AV_RL32(buf);
        *coded_samples -= *coded_samples % 28;
        nb_samples      = (buf_size - 12) / 30 * 28;
        break;
    case CODEC_ID_ADPCM_IMA_EA_EACS:
        has_coded_samples = 1;
        if (buf_size < 4)
        *coded_samples = AV_RL32(buf);
        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;
        break;
    case CODEC_ID_ADPCM_EA_R1:
    case CODEC_ID_ADPCM_EA_R2:
    case CODEC_ID_ADPCM_EA_R3:
        /* 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)
        switch (avctx->codec->id) {
        case CODEC_ID_ADPCM_EA_R1:
            header_size    = 4 + 9 * ch;
            *coded_samples = AV_RL32(buf);
            break;
        case CODEC_ID_ADPCM_EA_R2:
            header_size    = 4 + 5 * ch;
            *coded_samples = AV_RL32(buf);
            break;
        case CODEC_ID_ADPCM_EA_R3:
            header_size    = 4 + 5 * ch;
            *coded_samples = AV_RB32(buf);
            break;
        }
        *coded_samples -= *coded_samples % 28;
        nb_samples      = (buf_size - header_size) * 2 / ch;
        nb_samples     -= nb_samples % 28;
        break;
    case CODEC_ID_ADPCM_IMA_DK3:
        if (avctx->block_align > 0)
            buf_size = FFMIN(buf_size, avctx->block_align);
        nb_samples = ((buf_size - 16) * 8 / 3) / ch;
        break;
    case CODEC_ID_ADPCM_IMA_DK4:
        nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
        break;
    case CODEC_ID_ADPCM_IMA_WAV:
        if (avctx->block_align > 0)
            buf_size = FFMIN(buf_size, avctx->block_align);
        nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
        break;
    case CODEC_ID_ADPCM_MS:
        if (avctx->block_align > 0)
            buf_size = FFMIN(buf_size, avctx->block_align);
        nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
        break;
    case CODEC_ID_ADPCM_SBPRO_2:
    case CODEC_ID_ADPCM_SBPRO_3:
    case CODEC_ID_ADPCM_SBPRO_4:
    {
        int samples_per_byte;
        switch (avctx->codec->id) {
        case CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
        case CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
        case CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
        }
        if (!s->status[0].step_index) {
            nb_samples++;
            buf_size -= ch;
        }
        nb_samples += buf_size * samples_per_byte / ch;
        break;
    }
    case CODEC_ID_ADPCM_SWF:
    {
        int buf_bits       = buf_size * 8 - 2;
        int nbits          = (buf[0] >> 6) + 2;
        int block_hdr_size = 22 * ch;
        int block_size     = block_hdr_size + nbits * ch * 4095;
        int nblocks        = buf_bits / block_size;
        int bits_left      = buf_bits - nblocks * block_size;
        nb_samples         = nblocks * 4096;
        if (bits_left >= block_hdr_size)
            nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
        break;
    }
    case CODEC_ID_ADPCM_THP:
        has_coded_samples = 1;
        if (buf_size < 8)
        *coded_samples  = AV_RB32(&buf[4]);
        *coded_samples -= *coded_samples % 14;
        nb_samples      = (buf_size - 80) / (8 * ch) * 14;
        break;
    case CODEC_ID_ADPCM_XA:
        nb_samples = (buf_size / 128) * 224 / ch;
        break;
    }
    /* validate coded sample count */
    if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
        return AVERROR_INVALIDDATA;
    return nb_samples;
}
static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf,
                          int buf_size, int *coded_samples)
{
    ADPCMDecodeContext *s = avctx->priv_data;
    int nb_samples        = 0;
    int ch                = avctx->channels;
    int has_coded_samples = 0;
    int header_size;
    *coded_samples = 0;
    switch (avctx->codec->id) {
    
    case CODEC_ID_ADPCM_EA_XAS:
        if (buf_size < 76 * ch)
        nb_samples = 128;
        break;
    case CODEC_ID_ADPCM_IMA_QT:
        if (buf_size < 34 * ch)
        nb_samples = 64;
        break;
    
    case CODEC_ID_ADPCM_CT:
    case CODEC_ID_ADPCM_IMA_EA_SEAD:
    case CODEC_ID_ADPCM_IMA_WS:
    case CODEC_ID_ADPCM_YAMAHA:
        nb_samples = buf_size * 2 / ch;
        break;
    }
    if (nb_samples)
        return nb_samples;
    
    header_size = 0;
    switch (avctx->codec->id) {
        case CODEC_ID_ADPCM_4XM:
        case CODEC_ID_ADPCM_IMA_ISS:     header_size = 4 * ch;      break;
        case CODEC_ID_ADPCM_IMA_AMV:     header_size = 8;           break;
        case CODEC_ID_ADPCM_IMA_SMJPEG:  header_size = 4;           break;
    }
    if (header_size > 0)
        return (buf_size - header_size) * 2 / ch;
    
    switch (avctx->codec->id) {
    case CODEC_ID_ADPCM_EA:
        has_coded_samples = 1;
        if (buf_size < 4)
        *coded_samples  = AV_RL32(buf);
        *coded_samples -= *coded_samples % 28;
        nb_samples      = (buf_size - 12) / 30 * 28;
        break;
    case CODEC_ID_ADPCM_IMA_EA_EACS:
        has_coded_samples = 1;
        if (buf_size < 4)
        *coded_samples = AV_RL32(buf);
        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;
        break;
    case CODEC_ID_ADPCM_EA_R1:
    case CODEC_ID_ADPCM_EA_R2:
    case CODEC_ID_ADPCM_EA_R3:
        
        
        has_coded_samples = 1;
        if (buf_size < 4)
        switch (avctx->codec->id) {
        case CODEC_ID_ADPCM_EA_R1:
            header_size    = 4 + 9 * ch;
            *coded_samples = AV_RL32(buf);
            break;
        case CODEC_ID_ADPCM_EA_R2:
            header_size    = 4 + 5 * ch;
            *coded_samples = AV_RL32(buf);
            break;
        case CODEC_ID_ADPCM_EA_R3:
            header_size    = 4 + 5 * ch;
            *coded_samples = AV_RB32(buf);
            break;
        }
        *coded_samples -= *coded_samples % 28;
        nb_samples      = (buf_size - header_size) * 2 / ch;
        nb_samples     -= nb_samples % 28;
        break;
    case CODEC_ID_ADPCM_IMA_DK3:
        if (avctx->block_align > 0)
            buf_size = FFMIN(buf_size, avctx->block_align);
        nb_samples = ((buf_size - 16) * 8 / 3) / ch;
        break;
    case CODEC_ID_ADPCM_IMA_DK4:
        nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
        break;
    case CODEC_ID_ADPCM_IMA_WAV:
        if (avctx->block_align > 0)
            buf_size = FFMIN(buf_size, avctx->block_align);
        nb_samples = 1 + (buf_size - 4 * ch) / (4 * ch) * 8;
        break;
    case CODEC_ID_ADPCM_MS:
        if (avctx->block_align > 0)
            buf_size = FFMIN(buf_size, avctx->block_align);
        nb_samples = 2 + (buf_size - 7 * ch) * 2 / ch;
        break;
    case CODEC_ID_ADPCM_SBPRO_2:
    case CODEC_ID_ADPCM_SBPRO_3:
    case CODEC_ID_ADPCM_SBPRO_4:
    {
        int samples_per_byte;
        switch (avctx->codec->id) {
        case CODEC_ID_ADPCM_SBPRO_2: samples_per_byte = 4; break;
        case CODEC_ID_ADPCM_SBPRO_3: samples_per_byte = 3; break;
        case CODEC_ID_ADPCM_SBPRO_4: samples_per_byte = 2; break;
        }
        if (!s->status[0].step_index) {
            nb_samples++;
            buf_size -= ch;
        }
        nb_samples += buf_size * samples_per_byte / ch;
        break;
    }
    case CODEC_ID_ADPCM_SWF:
    {
        int buf_bits       = buf_size * 8 - 2;
        int nbits          = (buf[0] >> 6) + 2;
        int block_hdr_size = 22 * ch;
        int block_size     = block_hdr_size + nbits * ch * 4095;
        int nblocks        = buf_bits / block_size;
        int bits_left      = buf_bits - nblocks * block_size;
        nb_samples         = nblocks * 4096;
        if (bits_left >= block_hdr_size)
            nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
        break;
    }
    case CODEC_ID_ADPCM_THP:
        has_coded_samples = 1;
        if (buf_size < 8)
        *coded_samples  = AV_RB32(&buf[4]);
        *coded_samples -= *coded_samples % 14;
        nb_samples      = (buf_size - 80) / (8 * ch) * 14;
        break;
    case CODEC_ID_ADPCM_XA:
        nb_samples = (buf_size / 128) * 224 / ch;
        break;
    }
    
    if (has_coded_samples && (*coded_samples <= 0 || *coded_samples > nb_samples))
        return AVERROR_INVALIDDATA;
    return nb_samples;
}
static int FUNC_0(AVCodecContext *VAR_0, const uint8_t *VAR_1,
                          int VAR_2, int *VAR_3)
{
    ADPCMDecodeContext *s = VAR_0->priv_data;
    int VAR_4        = 0;
    int VAR_5                = VAR_0->channels;
    int VAR_6 = 0;
    int VAR_7;
    *VAR_3 = 0;
    switch (VAR_0->codec->id) {
    
    case CODEC_ID_ADPCM_EA_XAS:
        if (VAR_2 < 76 * VAR_5)
        VAR_4 = 128;
        break;
    case CODEC_ID_ADPCM_IMA_QT:
        if (VAR_2 < 34 * VAR_5)
        VAR_4 = 64;
        break;
    
    case CODEC_ID_ADPCM_CT:
    case CODEC_ID_ADPCM_IMA_EA_SEAD:
    case CODEC_ID_ADPCM_IMA_WS:
    case CODEC_ID_ADPCM_YAMAHA:
        VAR_4 = VAR_2 * 2 / VAR_5;
        break;
    }
    if (VAR_4)
        return VAR_4;
    
    VAR_7 = 0;
    switch (VAR_0->codec->id) {
        case CODEC_ID_ADPCM_4XM:
        case CODEC_ID_ADPCM_IMA_ISS:     VAR_7 = 4 * VAR_5;      break;
        case CODEC_ID_ADPCM_IMA_AMV:     VAR_7 = 8;           break;
        case CODEC_ID_ADPCM_IMA_SMJPEG:  VAR_7 = 4;           break;
    }
    if (VAR_7 > 0)
        return (VAR_2 - VAR_7) * 2 / VAR_5;
    
    switch (VAR_0->codec->id) {
    case CODEC_ID_ADPCM_EA:
        VAR_6 = 1;
        if (VAR_2 < 4)
        *VAR_3  = AV_RL32(VAR_1);
        *VAR_3 -= *VAR_3 % 28;
        VAR_4      = (VAR_2 - 12) / 30 * 28;
        break;
    case CODEC_ID_ADPCM_IMA_EA_EACS:
        VAR_6 = 1;
        if (VAR_2 < 4)
        *VAR_3 = AV_RL32(VAR_1);
        VAR_4     = (VAR_2 - (4 + 8 * VAR_5)) * 2 / VAR_5;
        break;
    case CODEC_ID_ADPCM_EA_MAXIS_XA:
        VAR_4 = ((VAR_2 - VAR_5) / (2 * VAR_5)) * 2 * VAR_5;
        break;
    case CODEC_ID_ADPCM_EA_R1:
    case CODEC_ID_ADPCM_EA_R2:
    case CODEC_ID_ADPCM_EA_R3:
        
        
        VAR_6 = 1;
        if (VAR_2 < 4)
        switch (VAR_0->codec->id) {
        case CODEC_ID_ADPCM_EA_R1:
            VAR_7    = 4 + 9 * VAR_5;
            *VAR_3 = AV_RL32(VAR_1);
            break;
        case CODEC_ID_ADPCM_EA_R2:
            VAR_7    = 4 + 5 * VAR_5;
            *VAR_3 = AV_RL32(VAR_1);
            break;
        case CODEC_ID_ADPCM_EA_R3:
            VAR_7    = 4 + 5 * VAR_5;
            *VAR_3 = AV_RB32(VAR_1);
            break;
        }
        *VAR_3 -= *VAR_3 % 28;
        VAR_4      = (VAR_2 - VAR_7) * 2 / VAR_5;
        VAR_4     -= VAR_4 % 28;
        break;
    case CODEC_ID_ADPCM_IMA_DK3:
        if (VAR_0->block_align > 0)
            VAR_2 = FFMIN(VAR_2, VAR_0->block_align);
        VAR_4 = ((VAR_2 - 16) * 8 / 3) / VAR_5;
        break;
    case CODEC_ID_ADPCM_IMA_DK4:
        VAR_4 = 1 + (VAR_2 - 4 * VAR_5) * 2 / VAR_5;
        break;
    case CODEC_ID_ADPCM_IMA_WAV:
        if (VAR_0->block_align > 0)
            VAR_2 = FFMIN(VAR_2, VAR_0->block_align);
        VAR_4 = 1 + (VAR_2 - 4 * VAR_5) / (4 * VAR_5) * 8;
        break;
    case CODEC_ID_ADPCM_MS:
        if (VAR_0->block_align > 0)
            VAR_2 = FFMIN(VAR_2, VAR_0->block_align);
        VAR_4 = 2 + (VAR_2 - 7 * VAR_5) * 2 / VAR_5;
        break;
    case CODEC_ID_ADPCM_SBPRO_2:
    case CODEC_ID_ADPCM_SBPRO_3:
    case CODEC_ID_ADPCM_SBPRO_4:
    {
        int VAR_8;
        switch (VAR_0->codec->id) {
        case CODEC_ID_ADPCM_SBPRO_2: VAR_8 = 4; break;
        case CODEC_ID_ADPCM_SBPRO_3: VAR_8 = 3; break;
        case CODEC_ID_ADPCM_SBPRO_4: VAR_8 = 2; break;
        }
        if (!s->status[0].step_index) {
            VAR_4++;
            VAR_2 -= VAR_5;
        }
        VAR_4 += VAR_2 * VAR_8 / VAR_5;
        break;
    }
    case CODEC_ID_ADPCM_SWF:
    {
        int VAR_9       = VAR_2 * 8 - 2;
        int VAR_10          = (VAR_1[0] >> 6) + 2;
        int VAR_11 = 22 * VAR_5;
        int VAR_12     = VAR_11 + VAR_10 * VAR_5 * 4095;
        int VAR_13        = VAR_9 / VAR_12;
        int VAR_14      = VAR_9 - VAR_13 * VAR_12;
        VAR_4         = VAR_13 * 4096;
        if (VAR_14 >= VAR_11)
            VAR_4 += 1 + (VAR_14 - VAR_11) / (VAR_10 * VAR_5);
        break;
    }
    case CODEC_ID_ADPCM_THP:
        VAR_6 = 1;
        if (VAR_2 < 8)
        *VAR_3  = AV_RB32(&VAR_1[4]);
        *VAR_3 -= *VAR_3 % 14;
        VAR_4      = (VAR_2 - 80) / (8 * VAR_5) * 14;
        break;
    case CODEC_ID_ADPCM_XA:
        VAR_4 = (VAR_2 / 128) * 224 / VAR_5;
        break;
    }
    
    if (VAR_6 && (*VAR_3 <= 0 || *VAR_3 > VAR_4))
        return AVERROR_INVALIDDATA;
    return VAR_4;
}
static int FUNC_0(AVCodecContext *VAR_0, const uint8_t *VAR_1,
int VAR_2, int *VAR_3)
{ ADPCMDecodeContext *s = VAR_0->priv_data; int VAR_4        = 0; int VAR_5                = VAR_0->channels; int VAR_6 = 0; int VAR_7; *VAR_3 = 0; switch (VAR_0->codec->id) { case CODEC_ID_ADPCM_EA_XAS:
if (VAR_2 < 76 * VAR_5)
VAR_4 = 128; break; case CODEC_ID_ADPCM_IMA_QT:
if (VAR_2 < 34 * VAR_5)
VAR_4 = 64; break; case CODEC_ID_ADPCM_CT:
case CODEC_ID_ADPCM_IMA_EA_SEAD:
case CODEC_ID_ADPCM_IMA_WS:
case CODEC_ID_ADPCM_YAMAHA:
VAR_4 = VAR_2 * 2 / VAR_5; break; } if (VAR_4)
return VAR_4; VAR_7 = 0; switch (VAR_0->codec->id) { case CODEC_ID_ADPCM_4XM:
case CODEC_ID_ADPCM_IMA_ISS:     VAR_7 = 4 * VAR_5;      break; case CODEC_ID_ADPCM_IMA_AMV:     VAR_7 = 8;           break; case CODEC_ID_ADPCM_IMA_SMJPEG:  VAR_7 = 4;           break; } if (VAR_7 > 0)
return (VAR_2 - VAR_7) * 2 / VAR_5; switch (VAR_0->codec->id) { case CODEC_ID_ADPCM_EA:
VAR_6 = 1; if (VAR_2 < 4)
*VAR_3  = AV_RL32(VAR_1); *VAR_3 -= *VAR_3 % 28; VAR_4      = (VAR_2 - 12) / 30 * 28; break; case CODEC_ID_ADPCM_IMA_EA_EACS:
VAR_6 = 1; if (VAR_2 < 4)
*VAR_3 = AV_RL32(VAR_1); VAR_4     = (VAR_2 - (4 + 8 * VAR_5)) * 2 / VAR_5; break; case CODEC_ID_ADPCM_EA_MAXIS_XA:
VAR_4 = ((VAR_2 - VAR_5) / (2 * VAR_5)) * 2 * VAR_5; break; case CODEC_ID_ADPCM_EA_R1:
case CODEC_ID_ADPCM_EA_R2:
case CODEC_ID_ADPCM_EA_R3:
VAR_6 = 1; if (VAR_2 < 4)
switch (VAR_0->codec->id) { case CODEC_ID_ADPCM_EA_R1:
VAR_7    = 4 + 9 * VAR_5; *VAR_3 = AV_RL32(VAR_1); break; case CODEC_ID_ADPCM_EA_R2:
VAR_7    = 4 + 5 * VAR_5; *VAR_3 = AV_RL32(VAR_1); break; case CODEC_ID_ADPCM_EA_R3:
VAR_7    = 4 + 5 * VAR_5; *VAR_3 = AV_RB32(VAR_1); break; } *VAR_3 -= *VAR_3 % 28; VAR_4      = (VAR_2 - VAR_7) * 2 / VAR_5; VAR_4     -= VAR_4 % 28; break; case CODEC_ID_ADPCM_IMA_DK3:
if (VAR_0->block_align > 0)
VAR_2 = FFMIN(VAR_2, VAR_0->block_align); VAR_4 = ((VAR_2 - 16) * 8 / 3) / VAR_5; break; case CODEC_ID_ADPCM_IMA_DK4:
VAR_4 = 1 + (VAR_2 - 4 * VAR_5) * 2 / VAR_5; break; case CODEC_ID_ADPCM_IMA_WAV:
if (VAR_0->block_align > 0)
VAR_2 = FFMIN(VAR_2, VAR_0->block_align); VAR_4 = 1 + (VAR_2 - 4 * VAR_5) / (4 * VAR_5) * 8; break; case CODEC_ID_ADPCM_MS:
if (VAR_0->block_align > 0)
VAR_2 = FFMIN(VAR_2, VAR_0->block_align); VAR_4 = 2 + (VAR_2 - 7 * VAR_5) * 2 / VAR_5; break; case CODEC_ID_ADPCM_SBPRO_2:
case CODEC_ID_ADPCM_SBPRO_3:
case CODEC_ID_ADPCM_SBPRO_4:
{ int VAR_8; switch (VAR_0->codec->id) { case CODEC_ID_ADPCM_SBPRO_2: VAR_8 = 4; break; case CODEC_ID_ADPCM_SBPRO_3: VAR_8 = 3; break; case CODEC_ID_ADPCM_SBPRO_4: VAR_8 = 2; break; } if (!s->status[0].step_index) { VAR_4++; VAR_2 -= VAR_5; } VAR_4 += VAR_2 * VAR_8 / VAR_5; break; } case CODEC_ID_ADPCM_SWF:
{ int VAR_9       = VAR_2 * 8 - 2; int VAR_10          = (VAR_1[0] >> 6) + 2; int VAR_11 = 22 * VAR_5; int VAR_12     = VAR_11 + VAR_10 * VAR_5 * 4095; int VAR_13        = VAR_9 / VAR_12; int VAR_14      = VAR_9 - VAR_13 * VAR_12; VAR_4         = VAR_13 * 4096; if (VAR_14 >= VAR_11)
VAR_4 += 1 + (VAR_14 - VAR_11) / (VAR_10 * VAR_5); break; } case CODEC_ID_ADPCM_THP:
VAR_6 = 1; if (VAR_2 < 8)
*VAR_3  = AV_RB32(&VAR_1[4]); *VAR_3 -= *VAR_3 % 14; VAR_4      = (VAR_2 - 80) / (8 * VAR_5) * 14; break; case CODEC_ID_ADPCM_XA:
VAR_4 = (VAR_2 / 128) * 224 / VAR_5; break; } if (VAR_6 && (*VAR_3 <= 0 || *VAR_3 > VAR_4))
return AVERROR_INVALIDDATA; return VAR_4; }