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

                                int *got_frame_ptr, AVPacket *avpkt)

{

    FDKAACDecContext *s = avctx->priv_data;

    AVFrame *frame = data;

    int ret;

    AAC_DECODER_ERROR err;

    UINT valid = avpkt->size;



    err = aacDecoder_Fill(s->handle, &avpkt->data, &avpkt->size, &valid);

    if (err != AAC_DEC_OK) {

        av_log(avctx, AV_LOG_ERROR, "aacDecoder_Fill() failed: %x\n", err);

        return AVERROR_INVALIDDATA;

    }



    err = aacDecoder_DecodeFrame(s->handle, (INT_PCM *) s->decoder_buffer, s->decoder_buffer_size, 0);

    if (err == AAC_DEC_NOT_ENOUGH_BITS) {

        ret = avpkt->size - valid;

        goto end;

    }

    if (err != AAC_DEC_OK) {

        av_log(avctx, AV_LOG_ERROR,

               "aacDecoder_DecodeFrame() failed: %x\n", err);

        ret = AVERROR_UNKNOWN;

        goto end;

    }



    if ((ret = get_stream_info(avctx)) < 0)

        goto end;

    frame->nb_samples = avctx->frame_size;



    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)

        goto end;



    memcpy(frame->extended_data[0], s->decoder_buffer,

           avctx->channels * avctx->frame_size *

           av_get_bytes_per_sample(avctx->sample_fmt));



    *got_frame_ptr = 1;

    ret = avpkt->size - valid;



end:

    return ret;

}
