static int latm_decode_frame(AVCodecContext *avctx, void *out,

                             int *got_frame_ptr, AVPacket *avpkt)

{

    struct LATMContext *latmctx = avctx->priv_data;

    int                 muxlength, err;

    GetBitContext       gb;



    init_get_bits(&gb, avpkt->data, avpkt->size * 8);



    // check for LOAS sync word

    if (get_bits(&gb, 11) != LOAS_SYNC_WORD)

        return AVERROR_INVALIDDATA;



    muxlength = get_bits(&gb, 13) + 3;

    // not enough data, the parser should have sorted this

    if (muxlength > avpkt->size)

        return AVERROR_INVALIDDATA;



    if ((err = read_audio_mux_element(latmctx, &gb)) < 0)

        return err;



    if (!latmctx->initialized) {

        if (!avctx->extradata) {

            *got_frame_ptr = 0;

            return avpkt->size;

        } else {

            push_output_configuration(&latmctx->aac_ctx);

            if ((err = decode_audio_specific_config(

                    &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,

                    avctx->extradata, avctx->extradata_size*8, 1)) < 0) {

                pop_output_configuration(&latmctx->aac_ctx);

                return err;

            }

            latmctx->initialized = 1;

        }

    }



    if (show_bits(&gb, 12) == 0xfff) {

        av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,

               "ADTS header detected, probably as result of configuration "

               "misparsing\n");

        return AVERROR_INVALIDDATA;

    }



    if ((err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb)) < 0)

        return err;



    return muxlength;

}
