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;

    int nblocks;

    int i, n;

    int blockstodecode;

    int bytes_used;



    if (buf_size == 0 && !s->samples) {

        *data_size = 0;

        return 0;

    }



    /* should not happen but who knows */

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

        av_log (avctx, AV_LOG_ERROR, "Packet size is too big to be handled in lavc! (max is %d where you have %d)\n", *data_size, s->samples * 2 * avctx->channels);

        return -1;

    }



    if(!s->samples){

        s->data = av_realloc(s->data, (buf_size + 3) & ~3);

        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 = s->samples = bytestream_get_be32(&s->ptr);

        n =  bytestream_get_be32(&s->ptr);

        if(n < 0 || n > 3){

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

            s->data = NULL;

            return -1;

        }

        s->ptr += n;



        s->currentframeblocks = nblocks;

        buf += 4;

        if (s->samples <= 0) {

            *data_size = 0;

            return buf_size;

        }



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

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



        /* Initialize the frame decoder */

        init_frame_decoder(s);

    }



    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->ptr > s->data_end){

        s->samples=0;

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

        return -1;

    }



    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;

    int nblocks;

    int i, n;

    int blockstodecode;

    int bytes_used;



    if (buf_size == 0 && !s->samples) {

        *data_size = 0;

        return 0;

    }



    

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

        av_log (avctx, AV_LOG_ERROR, "Packet size is too big to be handled in lavc! (max is %d where you have %d)\n", *data_size, s->samples * 2 * avctx->channels);

        return -1;

    }



    if(!s->samples){

        s->data = av_realloc(s->data, (buf_size + 3) & ~3);

        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 = s->samples = bytestream_get_be32(&s->ptr);

        n =  bytestream_get_be32(&s->ptr);

        if(n < 0 || n > 3){

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

            s->data = NULL;

            return -1;

        }

        s->ptr += n;



        s->currentframeblocks = nblocks;

        buf += 4;

        if (s->samples <= 0) {

            *data_size = 0;

            return buf_size;

        }



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

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



        

        init_frame_decoder(s);

    }



    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->ptr > s->data_end){

        s->samples=0;

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

        return -1;

    }



    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;

    int VAR_6;

    int VAR_7, VAR_8;

    int VAR_9;

    int VAR_10;



    if (VAR_5 == 0 && !s->samples) {

        *VAR_2 = 0;

        return 0;

    }



    

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

        av_log (VAR_0, AV_LOG_ERROR, "Packet size is too big to be handled in lavc! (max is %d where you have %d)\VAR_8", *VAR_2, s->samples * 2 * VAR_0->channels);

        return -1;

    }



    if(!s->samples){

        s->VAR_1 = av_realloc(s->VAR_1, (VAR_5 + 3) & ~3);

        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;



        VAR_6 = s->samples = bytestream_get_be32(&s->ptr);

        VAR_8 =  bytestream_get_be32(&s->ptr);

        if(VAR_8 < 0 || VAR_8 > 3){

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

            s->VAR_1 = NULL;

            return -1;

        }

        s->ptr += VAR_8;



        s->currentframeblocks = VAR_6;

        VAR_4 += 4;

        if (s->samples <= 0) {

            *VAR_2 = 0;

            return VAR_5;

        }



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

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



        

        init_frame_decoder(s);

    }



    if (!s->VAR_1) {

        *VAR_2 = 0;

        return VAR_5;

    }



    VAR_6 = s->samples;

    VAR_9 = FFMIN(BLOCKS_PER_LOOP, VAR_6);



    s->error=0;



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

        ape_unpack_mono(s, VAR_9);

    else

        ape_unpack_stereo(s, VAR_9);

    emms_c();



    if(s->error || s->ptr > s->data_end){

        s->samples=0;

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

        return -1;

    }



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

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

        if(s->channels == 2)

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

    }



    s->samples -= VAR_9;



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

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

    s->last_ptr = s->ptr;

    return VAR_10;

}

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; int VAR_6; int VAR_7, VAR_8; int VAR_9; int VAR_10; if (VAR_5 == 0 && !s->samples) { *VAR_2 = 0; return 0; } if (BLOCKS_PER_LOOP * 2 * VAR_0->channels > *VAR_2) { av_log (VAR_0, AV_LOG_ERROR, "Packet size is too big to be handled in lavc! (max is %d where you have %d)\VAR_8", *VAR_2, s->samples * 2 * VAR_0->channels); return -1; } if(!s->samples){ s->VAR_1 = av_realloc(s->VAR_1, (VAR_5 + 3) & ~3); 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; VAR_6 = s->samples = bytestream_get_be32(&s->ptr); VAR_8 =  bytestream_get_be32(&s->ptr); if(VAR_8 < 0 || VAR_8 > 3){ av_log(VAR_0, AV_LOG_ERROR, "Incorrect offset passed\VAR_8"); s->VAR_1 = NULL; return -1; } s->ptr += VAR_8; s->currentframeblocks = VAR_6; VAR_4 += 4; if (s->samples <= 0) { *VAR_2 = 0; return VAR_5; } memset(s->decoded0,  0, sizeof(s->decoded0)); memset(s->decoded1,  0, sizeof(s->decoded1)); init_frame_decoder(s); } if (!s->VAR_1) { *VAR_2 = 0; return VAR_5; } VAR_6 = s->samples; VAR_9 = FFMIN(BLOCKS_PER_LOOP, VAR_6); s->error=0; if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
ape_unpack_mono(s, VAR_9); else
ape_unpack_stereo(s, VAR_9); emms_c(); if(s->error || s->ptr > s->data_end){ s->samples=0; av_log(VAR_0, AV_LOG_ERROR, "Error decoding frame\VAR_8"); return -1; } for (VAR_7 = 0; VAR_7 < VAR_9; VAR_7++) { *samples++ = s->decoded0[VAR_7]; if(s->channels == 2)
*samples++ = s->decoded1[VAR_7]; } s->samples -= VAR_9; *VAR_2 = VAR_9 * 2 * s->channels; VAR_10 = s->samples ? s->ptr - s->last_ptr : VAR_5; s->last_ptr = s->ptr; return VAR_10; }