static int encode_residual(FlacEncodeContext *ctx, int ch)

{

    int i, n;

    int min_order, max_order, opt_order, precision, omethod;

    int min_porder, max_porder;

    FlacFrame *frame;

    FlacSubframe *sub;

    int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];

    int shift[MAX_LPC_ORDER];

    int32_t *res, *smp;



    frame = &ctx->frame;

    sub = &frame->subframes[ch];

    res = sub->residual;

    smp = sub->samples;

    n = frame->blocksize;



    /* CONSTANT */

    for(i=1; i<n; i++) {

        if(smp[i] != smp[0]) break;

    }

    if(i == n) {

        sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;

        res[0] = smp[0];

        return sub->obits;

    }



    /* VERBATIM */

    if(n < 5) {

        sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;

        encode_residual_verbatim(res, smp, n);

        return sub->obits * n;

    }



    min_order = ctx->options.min_prediction_order;

    max_order = ctx->options.max_prediction_order;

    min_porder = ctx->options.min_partition_order;

    max_porder = ctx->options.max_partition_order;

    precision = ctx->options.lpc_coeff_precision;

    omethod = ctx->options.prediction_order_method;



    /* FIXED */

    if(!ctx->options.use_lpc || max_order == 0 || (n <= max_order)) {

        uint32_t bits[MAX_FIXED_ORDER+1];

        if(max_order > MAX_FIXED_ORDER) max_order = MAX_FIXED_ORDER;

        opt_order = 0;

        bits[0] = UINT32_MAX;

        for(i=min_order; i<=max_order; i++) {

            encode_residual_fixed(res, smp, n, i);

            bits[i] = calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res,

                                             n, i, sub->obits);

            if(bits[i] < bits[opt_order]) {

                opt_order = i;

            }

        }

        sub->order = opt_order;

        sub->type = FLAC_SUBFRAME_FIXED;

        sub->type_code = sub->type | sub->order;

        if(sub->order != max_order) {

            encode_residual_fixed(res, smp, n, sub->order);

            return calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res, n,

                                          sub->order, sub->obits);

        }

        return bits[sub->order];

    }



    /* LPC */

    opt_order = ff_lpc_calc_coefs(&ctx->dsp, smp, n, max_order, precision, coefs,

                               shift, ctx->options.use_lpc, omethod, MAX_LPC_SHIFT, 0);



    if(omethod == ORDER_METHOD_2LEVEL ||

       omethod == ORDER_METHOD_4LEVEL ||

       omethod == ORDER_METHOD_8LEVEL) {

        int levels = 1 << omethod;

        uint32_t bits[levels];

        int order;

        int opt_index = levels-1;

        opt_order = max_order-1;

        bits[opt_index] = UINT32_MAX;

        for(i=levels-1; i>=0; i--) {

            order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;

            if(order < 0) order = 0;

            encode_residual_lpc(res, smp, n, order+1, coefs[order], shift[order]);

            bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,

                                           res, n, order+1, sub->obits, precision);

            if(bits[i] < bits[opt_index]) {

                opt_index = i;

                opt_order = order;

            }

        }

        opt_order++;

    } else if(omethod == ORDER_METHOD_SEARCH) {

        // brute-force optimal order search

        uint32_t bits[MAX_LPC_ORDER];

        opt_order = 0;

        bits[0] = UINT32_MAX;

        for(i=min_order-1; i<max_order; i++) {

            encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);

            bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,

                                           res, n, i+1, sub->obits, precision);

            if(bits[i] < bits[opt_order]) {

                opt_order = i;

            }

        }

        opt_order++;

    } else if(omethod == ORDER_METHOD_LOG) {

        uint32_t bits[MAX_LPC_ORDER];

        int step;



        opt_order= min_order - 1 + (max_order-min_order)/3;

        memset(bits, -1, sizeof(bits));



        for(step=16 ;step; step>>=1){

            int last= opt_order;

            for(i=last-step; i<=last+step; i+= step){

                if(i<min_order-1 || i>=max_order || bits[i] < UINT32_MAX)

                    continue;

                encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);

                bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,

                                            res, n, i+1, sub->obits, precision);

                if(bits[i] < bits[opt_order])

                    opt_order= i;

            }

        }

        opt_order++;

    }



    sub->order = opt_order;

    sub->type = FLAC_SUBFRAME_LPC;

    sub->type_code = sub->type | (sub->order-1);

    sub->shift = shift[sub->order-1];

    for(i=0; i<sub->order; i++) {

        sub->coefs[i] = coefs[sub->order-1][i];

    }

    encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);

    return calc_rice_params_lpc(&sub->rc, min_porder, max_porder, res, n, sub->order,

                                sub->obits, precision);

}

static int encode_residual(FlacEncodeContext *ctx, int ch)

{

    int i, n;

    int min_order, max_order, opt_order, precision, omethod;

    int min_porder, max_porder;

    FlacFrame *frame;

    FlacSubframe *sub;

    int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];

    int shift[MAX_LPC_ORDER];

    int32_t *res, *smp;



    frame = &ctx->frame;

    sub = &frame->subframes[ch];

    res = sub->residual;

    smp = sub->samples;

    n = frame->blocksize;



    

    for(i=1; i<n; i++) {

        if(smp[i] != smp[0]) break;

    }

    if(i == n) {

        sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;

        res[0] = smp[0];

        return sub->obits;

    }



    

    if(n < 5) {

        sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;

        encode_residual_verbatim(res, smp, n);

        return sub->obits * n;

    }



    min_order = ctx->options.min_prediction_order;

    max_order = ctx->options.max_prediction_order;

    min_porder = ctx->options.min_partition_order;

    max_porder = ctx->options.max_partition_order;

    precision = ctx->options.lpc_coeff_precision;

    omethod = ctx->options.prediction_order_method;



    

    if(!ctx->options.use_lpc || max_order == 0 || (n <= max_order)) {

        uint32_t bits[MAX_FIXED_ORDER+1];

        if(max_order > MAX_FIXED_ORDER) max_order = MAX_FIXED_ORDER;

        opt_order = 0;

        bits[0] = UINT32_MAX;

        for(i=min_order; i<=max_order; i++) {

            encode_residual_fixed(res, smp, n, i);

            bits[i] = calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res,

                                             n, i, sub->obits);

            if(bits[i] < bits[opt_order]) {

                opt_order = i;

            }

        }

        sub->order = opt_order;

        sub->type = FLAC_SUBFRAME_FIXED;

        sub->type_code = sub->type | sub->order;

        if(sub->order != max_order) {

            encode_residual_fixed(res, smp, n, sub->order);

            return calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res, n,

                                          sub->order, sub->obits);

        }

        return bits[sub->order];

    }



    

    opt_order = ff_lpc_calc_coefs(&ctx->dsp, smp, n, max_order, precision, coefs,

                               shift, ctx->options.use_lpc, omethod, MAX_LPC_SHIFT, 0);



    if(omethod == ORDER_METHOD_2LEVEL ||

       omethod == ORDER_METHOD_4LEVEL ||

       omethod == ORDER_METHOD_8LEVEL) {

        int levels = 1 << omethod;

        uint32_t bits[levels];

        int order;

        int opt_index = levels-1;

        opt_order = max_order-1;

        bits[opt_index] = UINT32_MAX;

        for(i=levels-1; i>=0; i--) {

            order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;

            if(order < 0) order = 0;

            encode_residual_lpc(res, smp, n, order+1, coefs[order], shift[order]);

            bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,

                                           res, n, order+1, sub->obits, precision);

            if(bits[i] < bits[opt_index]) {

                opt_index = i;

                opt_order = order;

            }

        }

        opt_order++;

    } else if(omethod == ORDER_METHOD_SEARCH) {

        

        uint32_t bits[MAX_LPC_ORDER];

        opt_order = 0;

        bits[0] = UINT32_MAX;

        for(i=min_order-1; i<max_order; i++) {

            encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);

            bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,

                                           res, n, i+1, sub->obits, precision);

            if(bits[i] < bits[opt_order]) {

                opt_order = i;

            }

        }

        opt_order++;

    } else if(omethod == ORDER_METHOD_LOG) {

        uint32_t bits[MAX_LPC_ORDER];

        int step;



        opt_order= min_order - 1 + (max_order-min_order)/3;

        memset(bits, -1, sizeof(bits));



        for(step=16 ;step; step>>=1){

            int last= opt_order;

            for(i=last-step; i<=last+step; i+= step){

                if(i<min_order-1 || i>=max_order || bits[i] < UINT32_MAX)

                    continue;

                encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);

                bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,

                                            res, n, i+1, sub->obits, precision);

                if(bits[i] < bits[opt_order])

                    opt_order= i;

            }

        }

        opt_order++;

    }



    sub->order = opt_order;

    sub->type = FLAC_SUBFRAME_LPC;

    sub->type_code = sub->type | (sub->order-1);

    sub->shift = shift[sub->order-1];

    for(i=0; i<sub->order; i++) {

        sub->coefs[i] = coefs[sub->order-1][i];

    }

    encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);

    return calc_rice_params_lpc(&sub->rc, min_porder, max_porder, res, n, sub->order,

                                sub->obits, precision);

}

static int FUNC_0(FlacEncodeContext *VAR_0, int VAR_1)

{

    int VAR_2, VAR_3;

    int VAR_4, VAR_5, VAR_6, VAR_7, VAR_8;

    int VAR_9, VAR_10;

    FlacFrame *frame;

    FlacSubframe *sub;

    int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];

    int VAR_11[MAX_LPC_ORDER];

    int32_t *res, *smp;



    frame = &VAR_0->frame;

    sub = &frame->subframes[VAR_1];

    res = sub->residual;

    smp = sub->samples;

    VAR_3 = frame->blocksize;



    

    for(VAR_2=1; VAR_2<VAR_3; VAR_2++) {

        if(smp[VAR_2] != smp[0]) break;

    }

    if(VAR_2 == VAR_3) {

        sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;

        res[0] = smp[0];

        return sub->obits;

    }



    

    if(VAR_3 < 5) {

        sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;

        encode_residual_verbatim(res, smp, VAR_3);

        return sub->obits * VAR_3;

    }



    VAR_4 = VAR_0->options.min_prediction_order;

    VAR_5 = VAR_0->options.max_prediction_order;

    VAR_9 = VAR_0->options.min_partition_order;

    VAR_10 = VAR_0->options.max_partition_order;

    VAR_7 = VAR_0->options.lpc_coeff_precision;

    VAR_8 = VAR_0->options.prediction_order_method;



    

    if(!VAR_0->options.use_lpc || VAR_5 == 0 || (VAR_3 <= VAR_5)) {

        uint32_t bits[MAX_FIXED_ORDER+1];

        if(VAR_5 > MAX_FIXED_ORDER) VAR_5 = MAX_FIXED_ORDER;

        VAR_6 = 0;

        bits[0] = UINT32_MAX;

        for(VAR_2=VAR_4; VAR_2<=VAR_5; VAR_2++) {

            encode_residual_fixed(res, smp, VAR_3, VAR_2);

            bits[VAR_2] = calc_rice_params_fixed(&sub->rc, VAR_9, VAR_10, res,

                                             VAR_3, VAR_2, sub->obits);

            if(bits[VAR_2] < bits[VAR_6]) {

                VAR_6 = VAR_2;

            }

        }

        sub->VAR_13 = VAR_6;

        sub->type = FLAC_SUBFRAME_FIXED;

        sub->type_code = sub->type | sub->VAR_13;

        if(sub->VAR_13 != VAR_5) {

            encode_residual_fixed(res, smp, VAR_3, sub->VAR_13);

            return calc_rice_params_fixed(&sub->rc, VAR_9, VAR_10, res, VAR_3,

                                          sub->VAR_13, sub->obits);

        }

        return bits[sub->VAR_13];

    }



    

    VAR_6 = ff_lpc_calc_coefs(&VAR_0->dsp, smp, VAR_3, VAR_5, VAR_7, coefs,

                               VAR_11, VAR_0->options.use_lpc, VAR_8, MAX_LPC_SHIFT, 0);



    if(VAR_8 == ORDER_METHOD_2LEVEL ||

       VAR_8 == ORDER_METHOD_4LEVEL ||

       VAR_8 == ORDER_METHOD_8LEVEL) {

        int VAR_12 = 1 << VAR_8;

        uint32_t bits[VAR_12];

        int VAR_13;

        int VAR_14 = VAR_12-1;

        VAR_6 = VAR_5-1;

        bits[VAR_14] = UINT32_MAX;

        for(VAR_2=VAR_12-1; VAR_2>=0; VAR_2--) {

            VAR_13 = VAR_4 + (((VAR_5-VAR_4+1) * (VAR_2+1)) / VAR_12)-1;

            if(VAR_13 < 0) VAR_13 = 0;

            encode_residual_lpc(res, smp, VAR_3, VAR_13+1, coefs[VAR_13], VAR_11[VAR_13]);

            bits[VAR_2] = calc_rice_params_lpc(&sub->rc, VAR_9, VAR_10,

                                           res, VAR_3, VAR_13+1, sub->obits, VAR_7);

            if(bits[VAR_2] < bits[VAR_14]) {

                VAR_14 = VAR_2;

                VAR_6 = VAR_13;

            }

        }

        VAR_6++;

    } else if(VAR_8 == ORDER_METHOD_SEARCH) {

        

        uint32_t bits[MAX_LPC_ORDER];

        VAR_6 = 0;

        bits[0] = UINT32_MAX;

        for(VAR_2=VAR_4-1; VAR_2<VAR_5; VAR_2++) {

            encode_residual_lpc(res, smp, VAR_3, VAR_2+1, coefs[VAR_2], VAR_11[VAR_2]);

            bits[VAR_2] = calc_rice_params_lpc(&sub->rc, VAR_9, VAR_10,

                                           res, VAR_3, VAR_2+1, sub->obits, VAR_7);

            if(bits[VAR_2] < bits[VAR_6]) {

                VAR_6 = VAR_2;

            }

        }

        VAR_6++;

    } else if(VAR_8 == ORDER_METHOD_LOG) {

        uint32_t bits[MAX_LPC_ORDER];

        int VAR_15;



        VAR_6= VAR_4 - 1 + (VAR_5-VAR_4)/3;

        memset(bits, -1, sizeof(bits));



        for(VAR_15=16 ;VAR_15; VAR_15>>=1){

            int VAR_16= VAR_6;

            for(VAR_2=VAR_16-VAR_15; VAR_2<=VAR_16+VAR_15; VAR_2+= VAR_15){

                if(VAR_2<VAR_4-1 || VAR_2>=VAR_5 || bits[VAR_2] < UINT32_MAX)

                    continue;

                encode_residual_lpc(res, smp, VAR_3, VAR_2+1, coefs[VAR_2], VAR_11[VAR_2]);

                bits[VAR_2] = calc_rice_params_lpc(&sub->rc, VAR_9, VAR_10,

                                            res, VAR_3, VAR_2+1, sub->obits, VAR_7);

                if(bits[VAR_2] < bits[VAR_6])

                    VAR_6= VAR_2;

            }

        }

        VAR_6++;

    }



    sub->VAR_13 = VAR_6;

    sub->type = FLAC_SUBFRAME_LPC;

    sub->type_code = sub->type | (sub->VAR_13-1);

    sub->VAR_11 = VAR_11[sub->VAR_13-1];

    for(VAR_2=0; VAR_2<sub->VAR_13; VAR_2++) {

        sub->coefs[VAR_2] = coefs[sub->VAR_13-1][VAR_2];

    }

    encode_residual_lpc(res, smp, VAR_3, sub->VAR_13, sub->coefs, sub->VAR_11);

    return calc_rice_params_lpc(&sub->rc, VAR_9, VAR_10, res, VAR_3, sub->VAR_13,

                                sub->obits, VAR_7);

}

static int FUNC_0(FlacEncodeContext *VAR_0, int VAR_1)
{ int VAR_2, VAR_3; int VAR_4, VAR_5, VAR_6, VAR_7, VAR_8; int VAR_9, VAR_10; FlacFrame *frame; FlacSubframe *sub; int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER]; int VAR_11[MAX_LPC_ORDER]; int32_t *res, *smp; frame = &VAR_0->frame; sub = &frame->subframes[VAR_1]; res = sub->residual; smp = sub->samples; VAR_3 = frame->blocksize; for(VAR_2=1; VAR_2<VAR_3; VAR_2++) { if(smp[VAR_2] != smp[0]) break; } if(VAR_2 == VAR_3) { sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT; res[0] = smp[0]; return sub->obits; } if(VAR_3 < 5) { sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM; encode_residual_verbatim(res, smp, VAR_3); return sub->obits * VAR_3; } VAR_4 = VAR_0->options.min_prediction_order; VAR_5 = VAR_0->options.max_prediction_order; VAR_9 = VAR_0->options.min_partition_order; VAR_10 = VAR_0->options.max_partition_order; VAR_7 = VAR_0->options.lpc_coeff_precision; VAR_8 = VAR_0->options.prediction_order_method; if(!VAR_0->options.use_lpc || VAR_5 == 0 || (VAR_3 <= VAR_5)) { uint32_t bits[MAX_FIXED_ORDER+1]; if(VAR_5 > MAX_FIXED_ORDER) VAR_5 = MAX_FIXED_ORDER; VAR_6 = 0; bits[0] = UINT32_MAX; for(VAR_2=VAR_4; VAR_2<=VAR_5; VAR_2++) { encode_residual_fixed(res, smp, VAR_3, VAR_2); bits[VAR_2] = calc_rice_params_fixed(&sub->rc, VAR_9, VAR_10, res,
VAR_3, VAR_2, sub->obits); if(bits[VAR_2] < bits[VAR_6]) { VAR_6 = VAR_2; } } sub->VAR_13 = VAR_6; sub->type = FLAC_SUBFRAME_FIXED; sub->type_code = sub->type | sub->VAR_13; if(sub->VAR_13 != VAR_5) { encode_residual_fixed(res, smp, VAR_3, sub->VAR_13); return calc_rice_params_fixed(&sub->rc, VAR_9, VAR_10, res, VAR_3,
sub->VAR_13, sub->obits); } return bits[sub->VAR_13]; } VAR_6 = ff_lpc_calc_coefs(&VAR_0->dsp, smp, VAR_3, VAR_5, VAR_7, coefs,
VAR_11, VAR_0->options.use_lpc, VAR_8, MAX_LPC_SHIFT, 0); if(VAR_8 == ORDER_METHOD_2LEVEL ||
VAR_8 == ORDER_METHOD_4LEVEL ||
VAR_8 == ORDER_METHOD_8LEVEL) { int VAR_12 = 1 << VAR_8; uint32_t bits[VAR_12]; int VAR_13; int VAR_14 = VAR_12-1; VAR_6 = VAR_5-1; bits[VAR_14] = UINT32_MAX; for(VAR_2=VAR_12-1; VAR_2>=0; VAR_2--) { VAR_13 = VAR_4 + (((VAR_5-VAR_4+1) * (VAR_2+1)) / VAR_12)-1; if(VAR_13 < 0) VAR_13 = 0; encode_residual_lpc(res, smp, VAR_3, VAR_13+1, coefs[VAR_13], VAR_11[VAR_13]); bits[VAR_2] = calc_rice_params_lpc(&sub->rc, VAR_9, VAR_10,
res, VAR_3, VAR_13+1, sub->obits, VAR_7); if(bits[VAR_2] < bits[VAR_14]) { VAR_14 = VAR_2; VAR_6 = VAR_13; } } VAR_6++; } else if(VAR_8 == ORDER_METHOD_SEARCH) { uint32_t bits[MAX_LPC_ORDER]; VAR_6 = 0; bits[0] = UINT32_MAX; for(VAR_2=VAR_4-1; VAR_2<VAR_5; VAR_2++) { encode_residual_lpc(res, smp, VAR_3, VAR_2+1, coefs[VAR_2], VAR_11[VAR_2]); bits[VAR_2] = calc_rice_params_lpc(&sub->rc, VAR_9, VAR_10,
res, VAR_3, VAR_2+1, sub->obits, VAR_7); if(bits[VAR_2] < bits[VAR_6]) { VAR_6 = VAR_2; } } VAR_6++; } else if(VAR_8 == ORDER_METHOD_LOG) { uint32_t bits[MAX_LPC_ORDER]; int VAR_15; VAR_6= VAR_4 - 1 + (VAR_5-VAR_4)/3; memset(bits, -1, sizeof(bits)); for(VAR_15=16 ;VAR_15; VAR_15>>=1){ int VAR_16= VAR_6; for(VAR_2=VAR_16-VAR_15; VAR_2<=VAR_16+VAR_15; VAR_2+= VAR_15){ if(VAR_2<VAR_4-1 || VAR_2>=VAR_5 || bits[VAR_2] < UINT32_MAX)
continue; encode_residual_lpc(res, smp, VAR_3, VAR_2+1, coefs[VAR_2], VAR_11[VAR_2]); bits[VAR_2] = calc_rice_params_lpc(&sub->rc, VAR_9, VAR_10,
res, VAR_3, VAR_2+1, sub->obits, VAR_7); if(bits[VAR_2] < bits[VAR_6])
VAR_6= VAR_2; } } VAR_6++; } sub->VAR_13 = VAR_6; sub->type = FLAC_SUBFRAME_LPC; sub->type_code = sub->type | (sub->VAR_13-1); sub->VAR_11 = VAR_11[sub->VAR_13-1]; for(VAR_2=0; VAR_2<sub->VAR_13; VAR_2++) { sub->coefs[VAR_2] = coefs[sub->VAR_13-1][VAR_2]; } encode_residual_lpc(res, smp, VAR_3, sub->VAR_13, sub->coefs, sub->VAR_11); return calc_rice_params_lpc(&sub->rc, VAR_9, VAR_10, res, VAR_3, sub->VAR_13,
sub->obits, VAR_7); }