static int ape_decode_frame(AVCodecContext *avctx,

                            void *data, int *data_size,

                            AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    int buf_size = avpkt->size;

    APEContext *s = avctx->priv_data;

    int16_t *samples = data;

    uint32_t nblocks;

    int i;

    int blockstodecode;

    int bytes_used;



    /* should not happen but who knows */

    if (BLOCKS_PER_LOOP * 2 * avctx->channels > *data_size) {

        av_log (avctx, AV_LOG_ERROR, "Output buffer is too small.\n");

        return AVERROR(EINVAL);

    }



    /* this should never be negative, but bad things will happen if it is, so

       check it just to make sure. */

    av_assert0(s->samples >= 0);



    if(!s->samples){

        uint32_t offset;

        void *tmp_data;



        if (buf_size < 8) {

            av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");

            return AVERROR_INVALIDDATA;

        }



        tmp_data = av_realloc(s->data, FFALIGN(buf_size, 4));

        if (!tmp_data)

            return AVERROR(ENOMEM);

        s->data = tmp_data;

        s->dsp.bswap_buf((uint32_t*)s->data, (const uint32_t*)buf, buf_size >> 2);

        s->ptr = s->last_ptr = s->data;

        s->data_end = s->data + buf_size;



        nblocks = bytestream_get_be32(&s->ptr);

        offset  = bytestream_get_be32(&s->ptr);

        if (offset > 3) {

            av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n");

            s->data = NULL;

            return AVERROR_INVALIDDATA;

        }

        if (s->data_end - s->ptr < offset) {

            av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");

            return AVERROR_INVALIDDATA;

        }

        s->ptr += offset;



        if (!nblocks || nblocks > INT_MAX) {

            av_log(avctx, AV_LOG_ERROR, "Invalid sample count: %u.\n", nblocks);

            return AVERROR_INVALIDDATA;

        }

        s->samples = nblocks;



        memset(s->decoded0,  0, sizeof(s->decoded0));

        memset(s->decoded1,  0, sizeof(s->decoded1));



        /* Initialize the frame decoder */

        if (init_frame_decoder(s) < 0) {

            av_log(avctx, AV_LOG_ERROR, "Error reading frame header\n");

            return AVERROR_INVALIDDATA;

        }

    }



    if (!s->data) {

        *data_size = 0;

        return buf_size;

    }



    nblocks = s->samples;

    blockstodecode = FFMIN(BLOCKS_PER_LOOP, nblocks);



    s->error=0;



    if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))

        ape_unpack_mono(s, blockstodecode);

    else

        ape_unpack_stereo(s, blockstodecode);

    emms_c();



    if (s->error) {

        s->samples=0;

        av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");

        return AVERROR_INVALIDDATA;

    }



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

        *samples++ = s->decoded0[i];

        if(s->channels == 2)

            *samples++ = s->decoded1[i];

    }



    s->samples -= blockstodecode;



    *data_size = blockstodecode * 2 * s->channels;

    bytes_used = s->samples ? s->ptr - s->last_ptr : buf_size;

    s->last_ptr = s->ptr;

    return bytes_used;

}

static int ape_decode_frame(AVCodecContext *avctx,

                            void *data, int *data_size,

                            AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    int buf_size = avpkt->size;

    APEContext *s = avctx->priv_data;

    int16_t *samples = data;

    uint32_t nblocks;

    int i;

    int blockstodecode;

    int bytes_used;



    

    if (BLOCKS_PER_LOOP * 2 * avctx->channels > *data_size) {

        av_log (avctx, AV_LOG_ERROR, "Output buffer is too small.\n");

        return AVERROR(EINVAL);

    }



    



    av_assert0(s->samples >= 0);



    if(!s->samples){

        uint32_t offset;

        void *tmp_data;



        if (buf_size < 8) {

            av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");

            return AVERROR_INVALIDDATA;

        }



        tmp_data = av_realloc(s->data, FFALIGN(buf_size, 4));

        if (!tmp_data)

            return AVERROR(ENOMEM);

        s->data = tmp_data;

        s->dsp.bswap_buf((uint32_t*)s->data, (const uint32_t*)buf, buf_size >> 2);

        s->ptr = s->last_ptr = s->data;

        s->data_end = s->data + buf_size;



        nblocks = bytestream_get_be32(&s->ptr);

        offset  = bytestream_get_be32(&s->ptr);

        if (offset > 3) {

            av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n");

            s->data = NULL;

            return AVERROR_INVALIDDATA;

        }

        if (s->data_end - s->ptr < offset) {

            av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");

            return AVERROR_INVALIDDATA;

        }

        s->ptr += offset;



        if (!nblocks || nblocks > INT_MAX) {

            av_log(avctx, AV_LOG_ERROR, "Invalid sample count: %u.\n", nblocks);

            return AVERROR_INVALIDDATA;

        }

        s->samples = nblocks;



        memset(s->decoded0,  0, sizeof(s->decoded0));

        memset(s->decoded1,  0, sizeof(s->decoded1));



        

        if (init_frame_decoder(s) < 0) {

            av_log(avctx, AV_LOG_ERROR, "Error reading frame header\n");

            return AVERROR_INVALIDDATA;

        }

    }



    if (!s->data) {

        *data_size = 0;

        return buf_size;

    }



    nblocks = s->samples;

    blockstodecode = FFMIN(BLOCKS_PER_LOOP, nblocks);



    s->error=0;



    if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))

        ape_unpack_mono(s, blockstodecode);

    else

        ape_unpack_stereo(s, blockstodecode);

    emms_c();



    if (s->error) {

        s->samples=0;

        av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");

        return AVERROR_INVALIDDATA;

    }



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

        *samples++ = s->decoded0[i];

        if(s->channels == 2)

            *samples++ = s->decoded1[i];

    }



    s->samples -= blockstodecode;



    *data_size = blockstodecode * 2 * s->channels;

    bytes_used = s->samples ? s->ptr - s->last_ptr : buf_size;

    s->last_ptr = s->ptr;

    return bytes_used;

}

static int FUNC_0(AVCodecContext *VAR_0,

                            void *VAR_1, int *VAR_2,

                            AVPacket *VAR_3)

{

    const uint8_t *VAR_4 = VAR_3->VAR_1;

    int VAR_5 = VAR_3->size;

    APEContext *s = VAR_0->priv_data;

    int16_t *samples = VAR_1;

    uint32_t nblocks;

    int VAR_6;

    int VAR_7;

    int VAR_8;



    

    if (BLOCKS_PER_LOOP * 2 * VAR_0->channels > *VAR_2) {

        av_log (VAR_0, AV_LOG_ERROR, "Output buffer is too small.\n");

        return AVERROR(EINVAL);

    }



    



    av_assert0(s->samples >= 0);



    if(!s->samples){

        uint32_t offset;

        void *VAR_9;



        if (VAR_5 < 8) {

            av_log(VAR_0, AV_LOG_ERROR, "Packet is too small\n");

            return AVERROR_INVALIDDATA;

        }



        VAR_9 = av_realloc(s->VAR_1, FFALIGN(VAR_5, 4));

        if (!VAR_9)

            return AVERROR(ENOMEM);

        s->VAR_1 = VAR_9;

        s->dsp.bswap_buf((uint32_t*)s->VAR_1, (const uint32_t*)VAR_4, VAR_5 >> 2);

        s->ptr = s->last_ptr = s->VAR_1;

        s->data_end = s->VAR_1 + VAR_5;



        nblocks = bytestream_get_be32(&s->ptr);

        offset  = bytestream_get_be32(&s->ptr);

        if (offset > 3) {

            av_log(VAR_0, AV_LOG_ERROR, "Incorrect offset passed\n");

            s->VAR_1 = NULL;

            return AVERROR_INVALIDDATA;

        }

        if (s->data_end - s->ptr < offset) {

            av_log(VAR_0, AV_LOG_ERROR, "Packet is too small\n");

            return AVERROR_INVALIDDATA;

        }

        s->ptr += offset;



        if (!nblocks || nblocks > INT_MAX) {

            av_log(VAR_0, AV_LOG_ERROR, "Invalid sample count: %u.\n", nblocks);

            return AVERROR_INVALIDDATA;

        }

        s->samples = nblocks;



        memset(s->decoded0,  0, sizeof(s->decoded0));

        memset(s->decoded1,  0, sizeof(s->decoded1));



        

        if (init_frame_decoder(s) < 0) {

            av_log(VAR_0, AV_LOG_ERROR, "Error reading frame header\n");

            return AVERROR_INVALIDDATA;

        }

    }



    if (!s->VAR_1) {

        *VAR_2 = 0;

        return VAR_5;

    }



    nblocks = s->samples;

    VAR_7 = FFMIN(BLOCKS_PER_LOOP, nblocks);



    s->error=0;



    if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))

        ape_unpack_mono(s, VAR_7);

    else

        ape_unpack_stereo(s, VAR_7);

    emms_c();



    if (s->error) {

        s->samples=0;

        av_log(VAR_0, AV_LOG_ERROR, "Error decoding frame\n");

        return AVERROR_INVALIDDATA;

    }



    for (VAR_6 = 0; VAR_6 < VAR_7; VAR_6++) {

        *samples++ = s->decoded0[VAR_6];

        if(s->channels == 2)

            *samples++ = s->decoded1[VAR_6];

    }



    s->samples -= VAR_7;



    *VAR_2 = VAR_7 * 2 * s->channels;

    VAR_8 = s->samples ? s->ptr - s->last_ptr : VAR_5;

    s->last_ptr = s->ptr;

    return VAR_8;

}

static int FUNC_0(AVCodecContext *VAR_0,
void *VAR_1, int *VAR_2,
AVPacket *VAR_3)
{ const uint8_t *VAR_4 = VAR_3->VAR_1; int VAR_5 = VAR_3->size; APEContext *s = VAR_0->priv_data; int16_t *samples = VAR_1; uint32_t nblocks; int VAR_6; int VAR_7; int VAR_8; if (BLOCKS_PER_LOOP * 2 * VAR_0->channels > *VAR_2) { av_log (VAR_0, AV_LOG_ERROR, "Output buffer is too small.\n"); return AVERROR(EINVAL); } av_assert0(s->samples >= 0); if(!s->samples){ uint32_t offset; void *VAR_9; if (VAR_5 < 8) { av_log(VAR_0, AV_LOG_ERROR, "Packet is too small\n"); return AVERROR_INVALIDDATA; } VAR_9 = av_realloc(s->VAR_1, FFALIGN(VAR_5, 4)); if (!VAR_9)
return AVERROR(ENOMEM); s->VAR_1 = VAR_9; s->dsp.bswap_buf((uint32_t*)s->VAR_1, (const uint32_t*)VAR_4, VAR_5 >> 2); s->ptr = s->last_ptr = s->VAR_1; s->data_end = s->VAR_1 + VAR_5; nblocks = bytestream_get_be32(&s->ptr); offset  = bytestream_get_be32(&s->ptr); if (offset > 3) { av_log(VAR_0, AV_LOG_ERROR, "Incorrect offset passed\n"); s->VAR_1 = NULL; return AVERROR_INVALIDDATA; } if (s->data_end - s->ptr < offset) { av_log(VAR_0, AV_LOG_ERROR, "Packet is too small\n"); return AVERROR_INVALIDDATA; } s->ptr += offset; if (!nblocks || nblocks > INT_MAX) { av_log(VAR_0, AV_LOG_ERROR, "Invalid sample count: %u.\n", nblocks); return AVERROR_INVALIDDATA; } s->samples = nblocks; memset(s->decoded0,  0, sizeof(s->decoded0)); memset(s->decoded1,  0, sizeof(s->decoded1)); if (init_frame_decoder(s) < 0) { av_log(VAR_0, AV_LOG_ERROR, "Error reading frame header\n"); return AVERROR_INVALIDDATA; } } if (!s->VAR_1) { *VAR_2 = 0; return VAR_5; } nblocks = s->samples; VAR_7 = FFMIN(BLOCKS_PER_LOOP, nblocks); s->error=0; if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
ape_unpack_mono(s, VAR_7); else
ape_unpack_stereo(s, VAR_7); emms_c(); if (s->error) { s->samples=0; av_log(VAR_0, AV_LOG_ERROR, "Error decoding frame\n"); return AVERROR_INVALIDDATA; } for (VAR_6 = 0; VAR_6 < VAR_7; VAR_6++) { *samples++ = s->decoded0[VAR_6]; if(s->channels == 2)
*samples++ = s->decoded1[VAR_6]; } s->samples -= VAR_7; *VAR_2 = VAR_7 * 2 * s->channels; VAR_8 = s->samples ? s->ptr - s->last_ptr : VAR_5; s->last_ptr = s->ptr; return VAR_8; }