static int encode_residual_ch(FlacEncodeContext *s, int ch)

{

    int i, n;

    int min_order, max_order, opt_order, omethod;

    FlacFrame *frame;

    FlacSubframe *sub;

    int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];

    int shift[MAX_LPC_ORDER];

    int32_t *res, *smp;



    frame = &s->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 subframe_count_exact(s, sub, 0);

    }



    /* VERBATIM */

    if (frame->verbatim_only || n < 5) {

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

        memcpy(res, smp, n * sizeof(int32_t));

        return subframe_count_exact(s, sub, 0);

    }



    min_order  = s->options.min_prediction_order;

    max_order  = s->options.max_prediction_order;

    omethod    = s->options.prediction_order_method;



    /* FIXED */

    sub->type = FLAC_SUBFRAME_FIXED;

    if (s->options.lpc_type == FF_LPC_TYPE_NONE  ||

        s->options.lpc_type == FF_LPC_TYPE_FIXED || 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] = find_subframe_rice_params(s, sub, i);

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

                opt_order = i;

        }

        sub->order     = opt_order;

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

        if (sub->order != max_order) {

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

            find_subframe_rice_params(s, sub, sub->order);

        }

        return subframe_count_exact(s, sub, sub->order);

    }



    /* LPC */

    sub->type = FLAC_SUBFRAME_LPC;

    opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, smp, n, min_order, max_order,

                                  s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type,

                                  s->options.lpc_passes, 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[1 << ORDER_METHOD_8LEVEL];

        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] = find_subframe_rice_params(s, sub, order+1);

            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] = find_subframe_rice_params(s, sub, i+1);

            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] = find_subframe_rice_params(s, sub, i+1);

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

                    opt_order = i;

            }

        }

        opt_order++;

    }



    sub->order     = opt_order;

    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);



    find_subframe_rice_params(s, sub, sub->order);



    return subframe_count_exact(s, sub, sub->order);

}

static int encode_residual_ch(FlacEncodeContext *s, int ch)

{

    int i, n;

    int min_order, max_order, opt_order, omethod;

    FlacFrame *frame;

    FlacSubframe *sub;

    int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];

    int shift[MAX_LPC_ORDER];

    int32_t *res, *smp;



    frame = &s->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 subframe_count_exact(s, sub, 0);

    }



    

    if (frame->verbatim_only || n < 5) {

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

        memcpy(res, smp, n * sizeof(int32_t));

        return subframe_count_exact(s, sub, 0);

    }



    min_order  = s->options.min_prediction_order;

    max_order  = s->options.max_prediction_order;

    omethod    = s->options.prediction_order_method;



    

    sub->type = FLAC_SUBFRAME_FIXED;

    if (s->options.lpc_type == FF_LPC_TYPE_NONE  ||

        s->options.lpc_type == FF_LPC_TYPE_FIXED || 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] = find_subframe_rice_params(s, sub, i);

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

                opt_order = i;

        }

        sub->order     = opt_order;

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

        if (sub->order != max_order) {

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

            find_subframe_rice_params(s, sub, sub->order);

        }

        return subframe_count_exact(s, sub, sub->order);

    }



    

    sub->type = FLAC_SUBFRAME_LPC;

    opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, smp, n, min_order, max_order,

                                  s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type,

                                  s->options.lpc_passes, 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[1 << ORDER_METHOD_8LEVEL];

        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] = find_subframe_rice_params(s, sub, order+1);

            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] = find_subframe_rice_params(s, sub, i+1);

            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] = find_subframe_rice_params(s, sub, i+1);

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

                    opt_order = i;

            }

        }

        opt_order++;

    }



    sub->order     = opt_order;

    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);



    find_subframe_rice_params(s, sub, sub->order);



    return subframe_count_exact(s, sub, sub->order);

}

        uint32_t bits[MAX_FIXED_ORDER+1];         uint32_t bits[1 << ORDER_METHOD_8LEVEL];         uint32_t bits[MAX_LPC_ORDER];         uint32_t bits[MAX_LPC_ORDER];
static int FUNC_0(FlacEncodeContext *VAR_0, int VAR_1)

{

    int VAR_2, VAR_3;

    int VAR_4, VAR_5, VAR_6, VAR_7;

    FlacFrame *frame;

    FlacSubframe *sub;

    int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];

    int VAR_8[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 subframe_count_exact(VAR_0, sub, 0);

    }



    

    if (frame->verbatim_only || VAR_3 < 5) {

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

        memcpy(res, smp, VAR_3 * sizeof(int32_t));

        return subframe_count_exact(VAR_0, sub, 0);

    }



    VAR_4  = VAR_0->options.min_prediction_order;

    VAR_5  = VAR_0->options.max_prediction_order;

    VAR_7    = VAR_0->options.prediction_order_method;



    

    sub->type = FLAC_SUBFRAME_FIXED;

    if (VAR_0->options.lpc_type == FF_LPC_TYPE_NONE  ||

        VAR_0->options.lpc_type == FF_LPC_TYPE_FIXED || 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] = find_subframe_rice_params(VAR_0, sub, VAR_2);

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

                VAR_6 = VAR_2;

        }

        sub->VAR_10     = VAR_6;

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

        if (sub->VAR_10 != VAR_5) {

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

            find_subframe_rice_params(VAR_0, sub, sub->VAR_10);

        }

        return subframe_count_exact(VAR_0, sub, sub->VAR_10);

    }



    

    sub->type = FLAC_SUBFRAME_LPC;

    VAR_6 = ff_lpc_calc_coefs(&VAR_0->lpc_ctx, smp, VAR_3, VAR_4, VAR_5,

                                  VAR_0->options.lpc_coeff_precision, coefs, VAR_8, VAR_0->options.lpc_type,

                                  VAR_0->options.lpc_passes, VAR_7,

                                  MAX_LPC_SHIFT, 0);



    if (VAR_7 == ORDER_METHOD_2LEVEL ||

        VAR_7 == ORDER_METHOD_4LEVEL ||

        VAR_7 == ORDER_METHOD_8LEVEL) {

        int VAR_9 = 1 << VAR_7;

        uint32_t bits[1 << ORDER_METHOD_8LEVEL];

        int VAR_10;

        int VAR_11   = VAR_9-1;

        VAR_6       = VAR_5-1;

        bits[VAR_11] = UINT32_MAX;

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

            VAR_10 = VAR_4 + (((VAR_5-VAR_4+1) * (VAR_2+1)) / VAR_9)-1;

            if (VAR_10 < 0)

                VAR_10 = 0;

            encode_residual_lpc(res, smp, VAR_3, VAR_10+1, coefs[VAR_10], VAR_8[VAR_10]);

            bits[VAR_2] = find_subframe_rice_params(VAR_0, sub, VAR_10+1);

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

                VAR_11 = VAR_2;

                VAR_6 = VAR_10;

            }

        }

        VAR_6++;

    } else if (VAR_7 == 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_8[VAR_2]);

            bits[VAR_2] = find_subframe_rice_params(VAR_0, sub, VAR_2+1);

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

                VAR_6 = VAR_2;

        }

        VAR_6++;

    } else if (VAR_7 == ORDER_METHOD_LOG) {

        uint32_t bits[MAX_LPC_ORDER];

        int VAR_12;



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

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



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

            int VAR_13 = VAR_6;

            for (VAR_2 = VAR_13-VAR_12; VAR_2 <= VAR_13+VAR_12; VAR_2 += VAR_12) {

                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_8[VAR_2]);

                bits[VAR_2] = find_subframe_rice_params(VAR_0, sub, VAR_2+1);

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

                    VAR_6 = VAR_2;

            }

        }

        VAR_6++;

    }



    sub->VAR_10     = VAR_6;

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

    sub->VAR_8     = VAR_8[sub->VAR_10-1];

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

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



    encode_residual_lpc(res, smp, VAR_3, sub->VAR_10, sub->coefs, sub->VAR_8);



    find_subframe_rice_params(VAR_0, sub, sub->VAR_10);



    return subframe_count_exact(VAR_0, sub, sub->VAR_10);

}

static int FUNC_0(FlacEncodeContext *VAR_0, int VAR_1)
{ int VAR_2, VAR_3; int VAR_4, VAR_5, VAR_6, VAR_7; FlacFrame *frame; FlacSubframe *sub; int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER]; int VAR_8[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 subframe_count_exact(VAR_0, sub, 0); } if (frame->verbatim_only || VAR_3 < 5) { sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM; memcpy(res, smp, VAR_3 * sizeof(int32_t)); return subframe_count_exact(VAR_0, sub, 0); } VAR_4  = VAR_0->options.min_prediction_order; VAR_5  = VAR_0->options.max_prediction_order; VAR_7    = VAR_0->options.prediction_order_method; sub->type = FLAC_SUBFRAME_FIXED; if (VAR_0->options.lpc_type == FF_LPC_TYPE_NONE  ||
VAR_0->options.lpc_type == FF_LPC_TYPE_FIXED || 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] = find_subframe_rice_params(VAR_0, sub, VAR_2); if (bits[VAR_2] < bits[VAR_6])
VAR_6 = VAR_2; } sub->VAR_10     = VAR_6; sub->type_code = sub->type | sub->VAR_10; if (sub->VAR_10 != VAR_5) { encode_residual_fixed(res, smp, VAR_3, sub->VAR_10); find_subframe_rice_params(VAR_0, sub, sub->VAR_10); } return subframe_count_exact(VAR_0, sub, sub->VAR_10); } sub->type = FLAC_SUBFRAME_LPC; VAR_6 = ff_lpc_calc_coefs(&VAR_0->lpc_ctx, smp, VAR_3, VAR_4, VAR_5,
VAR_0->options.lpc_coeff_precision, coefs, VAR_8, VAR_0->options.lpc_type,
VAR_0->options.lpc_passes, VAR_7,
MAX_LPC_SHIFT, 0); if (VAR_7 == ORDER_METHOD_2LEVEL ||
VAR_7 == ORDER_METHOD_4LEVEL ||
VAR_7 == ORDER_METHOD_8LEVEL) { int VAR_9 = 1 << VAR_7; uint32_t bits[1 << ORDER_METHOD_8LEVEL]; int VAR_10; int VAR_11   = VAR_9-1; VAR_6       = VAR_5-1; bits[VAR_11] = UINT32_MAX; for (VAR_2 = VAR_9-1; VAR_2 >= 0; VAR_2--) { VAR_10 = VAR_4 + (((VAR_5-VAR_4+1) * (VAR_2+1)) / VAR_9)-1; if (VAR_10 < 0)
VAR_10 = 0; encode_residual_lpc(res, smp, VAR_3, VAR_10+1, coefs[VAR_10], VAR_8[VAR_10]); bits[VAR_2] = find_subframe_rice_params(VAR_0, sub, VAR_10+1); if (bits[VAR_2] < bits[VAR_11]) { VAR_11 = VAR_2; VAR_6 = VAR_10; } } VAR_6++; } else if (VAR_7 == 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_8[VAR_2]); bits[VAR_2] = find_subframe_rice_params(VAR_0, sub, VAR_2+1); if (bits[VAR_2] < bits[VAR_6])
VAR_6 = VAR_2; } VAR_6++; } else if (VAR_7 == ORDER_METHOD_LOG) { uint32_t bits[MAX_LPC_ORDER]; int VAR_12; VAR_6 = VAR_4 - 1 + (VAR_5-VAR_4)/3; memset(bits, -1, sizeof(bits)); for (VAR_12 = 16; VAR_12; VAR_12 >>= 1) { int VAR_13 = VAR_6; for (VAR_2 = VAR_13-VAR_12; VAR_2 <= VAR_13+VAR_12; VAR_2 += VAR_12) { 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_8[VAR_2]); bits[VAR_2] = find_subframe_rice_params(VAR_0, sub, VAR_2+1); if (bits[VAR_2] < bits[VAR_6])
VAR_6 = VAR_2; } } VAR_6++; } sub->VAR_10     = VAR_6; sub->type_code = sub->type | (sub->VAR_10-1); sub->VAR_8     = VAR_8[sub->VAR_10-1]; for (VAR_2 = 0; VAR_2 < sub->VAR_10; VAR_2++) sub->coefs[VAR_2] = coefs[sub->VAR_10-1][VAR_2]; encode_residual_lpc(res, smp, VAR_3, sub->VAR_10, sub->coefs, sub->VAR_8); find_subframe_rice_params(VAR_0, sub, sub->VAR_10); return subframe_count_exact(VAR_0, sub, sub->VAR_10); }