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

                               int *got_frame_ptr, AVPacket *avpkt)

{

    DCAContext *s = avctx->priv_data;

    AVFrame *frame = data;

    uint8_t *input = avpkt->data;

    int input_size = avpkt->size;

    int i, ret, prev_packet = s->packet;



    if (input_size < MIN_PACKET_SIZE || input_size > MAX_PACKET_SIZE) {

        av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");

        return AVERROR_INVALIDDATA;

    }



    av_fast_malloc(&s->buffer, &s->buffer_size,

                   FFALIGN(input_size, 4096) + DCA_BUFFER_PADDING_SIZE);

    if (!s->buffer)

        return AVERROR(ENOMEM);



    for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - MIN_PACKET_SIZE + 1 && ret < 0; i++)

        ret = convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);



    if (ret < 0) {

        av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");

        return ret;

    }



    input      = s->buffer;

    input_size = ret;



    s->packet = 0;



    // Parse backward compatible core sub-stream

    if (AV_RB32(input) == DCA_SYNCWORD_CORE_BE) {

        int frame_size;



        if ((ret = ff_dca_core_parse(&s->core, input, input_size)) < 0)

            return ret;



        s->packet |= DCA_PACKET_CORE;



        // EXXS data must be aligned on 4-byte boundary

        frame_size = FFALIGN(s->core.frame_size, 4);

        if (input_size - 4 > frame_size) {

            input      += frame_size;

            input_size -= frame_size;

        }

    }



    if (!s->core_only) {

        DCAExssAsset *asset = NULL;



        // Parse extension sub-stream (EXSS)

        if (AV_RB32(input) == DCA_SYNCWORD_SUBSTREAM) {

            if ((ret = ff_dca_exss_parse(&s->exss, input, input_size)) < 0) {

                if (avctx->err_recognition & AV_EF_EXPLODE)

                    return ret;

            } else {

                s->packet |= DCA_PACKET_EXSS;

                asset = &s->exss.assets[0];

            }

        }



        // Parse XLL component in EXSS

        if (asset && (asset->extension_mask & DCA_EXSS_XLL)) {

            if ((ret = ff_dca_xll_parse(&s->xll, input, asset)) < 0) {

                // Conceal XLL synchronization error

                if (ret == AVERROR(EAGAIN)

                    && (prev_packet & DCA_PACKET_XLL)

                    && (s->packet & DCA_PACKET_CORE))

                    s->packet |= DCA_PACKET_XLL | DCA_PACKET_RECOVERY;

                else if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))

                    return ret;

            } else {

                s->packet |= DCA_PACKET_XLL;

            }

        }



        // Parse LBR component in EXSS

        if (asset && (asset->extension_mask & DCA_EXSS_LBR)) {

            if ((ret = ff_dca_lbr_parse(&s->lbr, input, asset)) < 0) {

                if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))

                    return ret;

            } else {

                s->packet |= DCA_PACKET_LBR;

            }

        }



        // Parse core extensions in EXSS or backward compatible core sub-stream

        if ((s->packet & DCA_PACKET_CORE)

            && (ret = ff_dca_core_parse_exss(&s->core, input, asset)) < 0)

            return ret;

    }



    // Filter the frame

    if (s->packet & DCA_PACKET_LBR) {

        if ((ret = ff_dca_lbr_filter_frame(&s->lbr, frame)) < 0)

            return ret;

    } else if (s->packet & DCA_PACKET_XLL) {

        if (s->packet & DCA_PACKET_CORE) {

            int x96_synth = -1;



            // Enable X96 synthesis if needed

            if (s->xll.chset[0].freq == 96000 && s->core.sample_rate == 48000)

                x96_synth = 1;



            if ((ret = ff_dca_core_filter_fixed(&s->core, x96_synth)) < 0)

                return ret;



            // Force lossy downmixed output on the first core frame filtered.

            // This prevents audible clicks when seeking and is consistent with

            // what reference decoder does when there are multiple channel sets.

            if (!(prev_packet & DCA_PACKET_RESIDUAL) && s->xll.nreschsets > 0

                && s->xll.nchsets > 1) {

                av_log(avctx, AV_LOG_VERBOSE, "Forcing XLL recovery mode\n");

                s->packet |= DCA_PACKET_RECOVERY;

            }



            // Set 'residual ok' flag for the next frame

            s->packet |= DCA_PACKET_RESIDUAL;

        }



        if ((ret = ff_dca_xll_filter_frame(&s->xll, frame)) < 0) {

            // Fall back to core unless hard error

            if (!(s->packet & DCA_PACKET_CORE))

                return ret;

            if (ret != AVERROR_INVALIDDATA || (avctx->err_recognition & AV_EF_EXPLODE))

                return ret;

            if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0)

                return ret;

        }

    } else if (s->packet & DCA_PACKET_CORE) {

        if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0)

            return ret;

        if (s->core.filter_mode & DCA_FILTER_MODE_FIXED)

            s->packet |= DCA_PACKET_RESIDUAL;

    } else {

        av_log(avctx, AV_LOG_ERROR, "No valid DCA sub-stream found\n");

        if (s->core_only)

            av_log(avctx, AV_LOG_WARNING, "Consider disabling 'core_only' option\n");

        return AVERROR_INVALIDDATA;

    }



    *got_frame_ptr = 1;



    return avpkt->size;

}

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

                               int *got_frame_ptr, AVPacket *avpkt)

{

    DCAContext *s = avctx->priv_data;

    AVFrame *frame = data;

    uint8_t *input = avpkt->data;

    int input_size = avpkt->size;

    int i, ret, prev_packet = s->packet;



    if (input_size < MIN_PACKET_SIZE || input_size > MAX_PACKET_SIZE) {

        av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");

        return AVERROR_INVALIDDATA;

    }



    av_fast_malloc(&s->buffer, &s->buffer_size,

                   FFALIGN(input_size, 4096) + DCA_BUFFER_PADDING_SIZE);

    if (!s->buffer)

        return AVERROR(ENOMEM);



    for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - MIN_PACKET_SIZE + 1 && ret < 0; i++)

        ret = convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);



    if (ret < 0) {

        av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");

        return ret;

    }



    input      = s->buffer;

    input_size = ret;



    s->packet = 0;



    

    if (AV_RB32(input) == DCA_SYNCWORD_CORE_BE) {

        int frame_size;



        if ((ret = ff_dca_core_parse(&s->core, input, input_size)) < 0)

            return ret;



        s->packet |= DCA_PACKET_CORE;



        

        frame_size = FFALIGN(s->core.frame_size, 4);

        if (input_size - 4 > frame_size) {

            input      += frame_size;

            input_size -= frame_size;

        }

    }



    if (!s->core_only) {

        DCAExssAsset *asset = NULL;



        

        if (AV_RB32(input) == DCA_SYNCWORD_SUBSTREAM) {

            if ((ret = ff_dca_exss_parse(&s->exss, input, input_size)) < 0) {

                if (avctx->err_recognition & AV_EF_EXPLODE)

                    return ret;

            } else {

                s->packet |= DCA_PACKET_EXSS;

                asset = &s->exss.assets[0];

            }

        }



        

        if (asset && (asset->extension_mask & DCA_EXSS_XLL)) {

            if ((ret = ff_dca_xll_parse(&s->xll, input, asset)) < 0) {

                

                if (ret == AVERROR(EAGAIN)

                    && (prev_packet & DCA_PACKET_XLL)

                    && (s->packet & DCA_PACKET_CORE))

                    s->packet |= DCA_PACKET_XLL | DCA_PACKET_RECOVERY;

                else if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))

                    return ret;

            } else {

                s->packet |= DCA_PACKET_XLL;

            }

        }



        

        if (asset && (asset->extension_mask & DCA_EXSS_LBR)) {

            if ((ret = ff_dca_lbr_parse(&s->lbr, input, asset)) < 0) {

                if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))

                    return ret;

            } else {

                s->packet |= DCA_PACKET_LBR;

            }

        }



        

        if ((s->packet & DCA_PACKET_CORE)

            && (ret = ff_dca_core_parse_exss(&s->core, input, asset)) < 0)

            return ret;

    }



    

    if (s->packet & DCA_PACKET_LBR) {

        if ((ret = ff_dca_lbr_filter_frame(&s->lbr, frame)) < 0)

            return ret;

    } else if (s->packet & DCA_PACKET_XLL) {

        if (s->packet & DCA_PACKET_CORE) {

            int x96_synth = -1;



            

            if (s->xll.chset[0].freq == 96000 && s->core.sample_rate == 48000)

                x96_synth = 1;



            if ((ret = ff_dca_core_filter_fixed(&s->core, x96_synth)) < 0)

                return ret;



            

            

            

            if (!(prev_packet & DCA_PACKET_RESIDUAL) && s->xll.nreschsets > 0

                && s->xll.nchsets > 1) {

                av_log(avctx, AV_LOG_VERBOSE, "Forcing XLL recovery mode\n");

                s->packet |= DCA_PACKET_RECOVERY;

            }



            

            s->packet |= DCA_PACKET_RESIDUAL;

        }



        if ((ret = ff_dca_xll_filter_frame(&s->xll, frame)) < 0) {

            

            if (!(s->packet & DCA_PACKET_CORE))

                return ret;

            if (ret != AVERROR_INVALIDDATA || (avctx->err_recognition & AV_EF_EXPLODE))

                return ret;

            if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0)

                return ret;

        }

    } else if (s->packet & DCA_PACKET_CORE) {

        if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0)

            return ret;

        if (s->core.filter_mode & DCA_FILTER_MODE_FIXED)

            s->packet |= DCA_PACKET_RESIDUAL;

    } else {

        av_log(avctx, AV_LOG_ERROR, "No valid DCA sub-stream found\n");

        if (s->core_only)

            av_log(avctx, AV_LOG_WARNING, "Consider disabling 'core_only' option\n");

        return AVERROR_INVALIDDATA;

    }



    *got_frame_ptr = 1;



    return avpkt->size;

}

static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1,

                               int *VAR_2, AVPacket *VAR_3)

{

    DCAContext *s = VAR_0->priv_data;

    AVFrame *frame = VAR_1;

    uint8_t *input = VAR_3->VAR_1;

    int VAR_4 = VAR_3->size;

    int VAR_5, VAR_6, VAR_7 = s->packet;



    if (VAR_4 < MIN_PACKET_SIZE || VAR_4 > MAX_PACKET_SIZE) {

        av_log(VAR_0, AV_LOG_ERROR, "Invalid packet size\n");

        return AVERROR_INVALIDDATA;

    }



    av_fast_malloc(&s->buffer, &s->buffer_size,

                   FFALIGN(VAR_4, 4096) + DCA_BUFFER_PADDING_SIZE);

    if (!s->buffer)

        return AVERROR(ENOMEM);



    for (VAR_5 = 0, VAR_6 = AVERROR_INVALIDDATA; VAR_5 < VAR_4 - MIN_PACKET_SIZE + 1 && VAR_6 < 0; VAR_5++)

        VAR_6 = convert_bitstream(input + VAR_5, VAR_4 - VAR_5, s->buffer, s->buffer_size);



    if (VAR_6 < 0) {

        av_log(VAR_0, AV_LOG_ERROR, "Not a valid DCA frame\n");

        return VAR_6;

    }



    input      = s->buffer;

    VAR_4 = VAR_6;



    s->packet = 0;



    

    if (AV_RB32(input) == DCA_SYNCWORD_CORE_BE) {

        int VAR_8;



        if ((VAR_6 = ff_dca_core_parse(&s->core, input, VAR_4)) < 0)

            return VAR_6;



        s->packet |= DCA_PACKET_CORE;



        

        VAR_8 = FFALIGN(s->core.VAR_8, 4);

        if (VAR_4 - 4 > VAR_8) {

            input      += VAR_8;

            VAR_4 -= VAR_8;

        }

    }



    if (!s->core_only) {

        DCAExssAsset *asset = NULL;



        

        if (AV_RB32(input) == DCA_SYNCWORD_SUBSTREAM) {

            if ((VAR_6 = ff_dca_exss_parse(&s->exss, input, VAR_4)) < 0) {

                if (VAR_0->err_recognition & AV_EF_EXPLODE)

                    return VAR_6;

            } else {

                s->packet |= DCA_PACKET_EXSS;

                asset = &s->exss.assets[0];

            }

        }



        

        if (asset && (asset->extension_mask & DCA_EXSS_XLL)) {

            if ((VAR_6 = ff_dca_xll_parse(&s->xll, input, asset)) < 0) {

                

                if (VAR_6 == AVERROR(EAGAIN)

                    && (VAR_7 & DCA_PACKET_XLL)

                    && (s->packet & DCA_PACKET_CORE))

                    s->packet |= DCA_PACKET_XLL | DCA_PACKET_RECOVERY;

                else if (VAR_6 == AVERROR(ENOMEM) || (VAR_0->err_recognition & AV_EF_EXPLODE))

                    return VAR_6;

            } else {

                s->packet |= DCA_PACKET_XLL;

            }

        }



        

        if (asset && (asset->extension_mask & DCA_EXSS_LBR)) {

            if ((VAR_6 = ff_dca_lbr_parse(&s->lbr, input, asset)) < 0) {

                if (VAR_6 == AVERROR(ENOMEM) || (VAR_0->err_recognition & AV_EF_EXPLODE))

                    return VAR_6;

            } else {

                s->packet |= DCA_PACKET_LBR;

            }

        }



        

        if ((s->packet & DCA_PACKET_CORE)

            && (VAR_6 = ff_dca_core_parse_exss(&s->core, input, asset)) < 0)

            return VAR_6;

    }



    

    if (s->packet & DCA_PACKET_LBR) {

        if ((VAR_6 = ff_dca_lbr_filter_frame(&s->lbr, frame)) < 0)

            return VAR_6;

    } else if (s->packet & DCA_PACKET_XLL) {

        if (s->packet & DCA_PACKET_CORE) {

            int VAR_9 = -1;



            

            if (s->xll.chset[0].freq == 96000 && s->core.sample_rate == 48000)

                VAR_9 = 1;



            if ((VAR_6 = ff_dca_core_filter_fixed(&s->core, VAR_9)) < 0)

                return VAR_6;



            

            

            

            if (!(VAR_7 & DCA_PACKET_RESIDUAL) && s->xll.nreschsets > 0

                && s->xll.nchsets > 1) {

                av_log(VAR_0, AV_LOG_VERBOSE, "Forcing XLL recovery mode\n");

                s->packet |= DCA_PACKET_RECOVERY;

            }



            

            s->packet |= DCA_PACKET_RESIDUAL;

        }



        if ((VAR_6 = ff_dca_xll_filter_frame(&s->xll, frame)) < 0) {

            

            if (!(s->packet & DCA_PACKET_CORE))

                return VAR_6;

            if (VAR_6 != AVERROR_INVALIDDATA || (VAR_0->err_recognition & AV_EF_EXPLODE))

                return VAR_6;

            if ((VAR_6 = ff_dca_core_filter_frame(&s->core, frame)) < 0)

                return VAR_6;

        }

    } else if (s->packet & DCA_PACKET_CORE) {

        if ((VAR_6 = ff_dca_core_filter_frame(&s->core, frame)) < 0)

            return VAR_6;

        if (s->core.filter_mode & DCA_FILTER_MODE_FIXED)

            s->packet |= DCA_PACKET_RESIDUAL;

    } else {

        av_log(VAR_0, AV_LOG_ERROR, "No valid DCA sub-stream found\n");

        if (s->core_only)

            av_log(VAR_0, AV_LOG_WARNING, "Consider disabling 'core_only' option\n");

        return AVERROR_INVALIDDATA;

    }



    *VAR_2 = 1;



    return VAR_3->size;

}

static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1,
int *VAR_2, AVPacket *VAR_3)
{ DCAContext *s = VAR_0->priv_data; AVFrame *frame = VAR_1; uint8_t *input = VAR_3->VAR_1; int VAR_4 = VAR_3->size; int VAR_5, VAR_6, VAR_7 = s->packet; if (VAR_4 < MIN_PACKET_SIZE || VAR_4 > MAX_PACKET_SIZE) { av_log(VAR_0, AV_LOG_ERROR, "Invalid packet size\n"); return AVERROR_INVALIDDATA; } av_fast_malloc(&s->buffer, &s->buffer_size,
FFALIGN(VAR_4, 4096) + DCA_BUFFER_PADDING_SIZE); if (!s->buffer)
return AVERROR(ENOMEM); for (VAR_5 = 0, VAR_6 = AVERROR_INVALIDDATA; VAR_5 < VAR_4 - MIN_PACKET_SIZE + 1 && VAR_6 < 0; VAR_5++) VAR_6 = convert_bitstream(input + VAR_5, VAR_4 - VAR_5, s->buffer, s->buffer_size); if (VAR_6 < 0) { av_log(VAR_0, AV_LOG_ERROR, "Not a valid DCA frame\n"); return VAR_6; } input      = s->buffer; VAR_4 = VAR_6; s->packet = 0; if (AV_RB32(input) == DCA_SYNCWORD_CORE_BE) { int VAR_8; if ((VAR_6 = ff_dca_core_parse(&s->core, input, VAR_4)) < 0)
return VAR_6; s->packet |= DCA_PACKET_CORE; VAR_8 = FFALIGN(s->core.VAR_8, 4); if (VAR_4 - 4 > VAR_8) { input      += VAR_8; VAR_4 -= VAR_8; } } if (!s->core_only) { DCAExssAsset *asset = NULL; if (AV_RB32(input) == DCA_SYNCWORD_SUBSTREAM) { if ((VAR_6 = ff_dca_exss_parse(&s->exss, input, VAR_4)) < 0) { if (VAR_0->err_recognition & AV_EF_EXPLODE)
return VAR_6; } else { s->packet |= DCA_PACKET_EXSS; asset = &s->exss.assets[0]; } } if (asset && (asset->extension_mask & DCA_EXSS_XLL)) { if ((VAR_6 = ff_dca_xll_parse(&s->xll, input, asset)) < 0) { if (VAR_6 == AVERROR(EAGAIN)
&& (VAR_7 & DCA_PACKET_XLL)
&& (s->packet & DCA_PACKET_CORE))
s->packet |= DCA_PACKET_XLL | DCA_PACKET_RECOVERY; else if (VAR_6 == AVERROR(ENOMEM) || (VAR_0->err_recognition & AV_EF_EXPLODE))
return VAR_6; } else { s->packet |= DCA_PACKET_XLL; } } if (asset && (asset->extension_mask & DCA_EXSS_LBR)) { if ((VAR_6 = ff_dca_lbr_parse(&s->lbr, input, asset)) < 0) { if (VAR_6 == AVERROR(ENOMEM) || (VAR_0->err_recognition & AV_EF_EXPLODE))
return VAR_6; } else { s->packet |= DCA_PACKET_LBR; } } if ((s->packet & DCA_PACKET_CORE)
&& (VAR_6 = ff_dca_core_parse_exss(&s->core, input, asset)) < 0)
return VAR_6; } if (s->packet & DCA_PACKET_LBR) { if ((VAR_6 = ff_dca_lbr_filter_frame(&s->lbr, frame)) < 0)
return VAR_6; } else if (s->packet & DCA_PACKET_XLL) { if (s->packet & DCA_PACKET_CORE) { int VAR_9 = -1; if (s->xll.chset[0].freq == 96000 && s->core.sample_rate == 48000)
VAR_9 = 1; if ((VAR_6 = ff_dca_core_filter_fixed(&s->core, VAR_9)) < 0)
return VAR_6; if (!(VAR_7 & DCA_PACKET_RESIDUAL) && s->xll.nreschsets > 0
&& s->xll.nchsets > 1) { av_log(VAR_0, AV_LOG_VERBOSE, "Forcing XLL recovery mode\n"); s->packet |= DCA_PACKET_RECOVERY; } s->packet |= DCA_PACKET_RESIDUAL; } if ((VAR_6 = ff_dca_xll_filter_frame(&s->xll, frame)) < 0) { if (!(s->packet & DCA_PACKET_CORE))
return VAR_6; if (VAR_6 != AVERROR_INVALIDDATA || (VAR_0->err_recognition & AV_EF_EXPLODE))
return VAR_6; if ((VAR_6 = ff_dca_core_filter_frame(&s->core, frame)) < 0)
return VAR_6; } } else if (s->packet & DCA_PACKET_CORE) { if ((VAR_6 = ff_dca_core_filter_frame(&s->core, frame)) < 0)
return VAR_6; if (s->core.filter_mode & DCA_FILTER_MODE_FIXED)
s->packet |= DCA_PACKET_RESIDUAL; } else { av_log(VAR_0, AV_LOG_ERROR, "No valid DCA sub-stream found\n"); if (s->core_only)
av_log(VAR_0, AV_LOG_WARNING, "Consider disabling 'core_only' option\n"); return AVERROR_INVALIDDATA; } *VAR_2 = 1; return VAR_3->size; }