static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx)

{

    int lambda, up_step, down_step;

    int last_lower = INT_MAX, last_higher = 0;

    int x, y, q;



    for (q = 1; q < avctx->qmax; q++) {

        ctx->qscale = q;

        avctx->execute2(avctx, dnxhd_calc_bits_thread, NULL, NULL, ctx->m.mb_height);

    }

    up_step = down_step = 2<<LAMBDA_FRAC_BITS;

    lambda = ctx->lambda;



    for (;;) {

        int bits = 0;

        int end = 0;

        if (lambda == last_higher) {

            lambda++;

            end = 1; // need to set final qscales/bits

        }

        for (y = 0; y < ctx->m.mb_height; y++) {

            for (x = 0; x < ctx->m.mb_width; x++) {

                unsigned min = UINT_MAX;

                int qscale = 1;

                int mb = y*ctx->m.mb_width+x;

                for (q = 1; q < avctx->qmax; q++) {

                    unsigned score = ctx->mb_rc[q][mb].bits*lambda+(ctx->mb_rc[q][mb].ssd<<LAMBDA_FRAC_BITS);

                    if (score < min) {

                        min = score;

                        qscale = q;

                    }

                }

                bits += ctx->mb_rc[qscale][mb].bits;

                ctx->mb_qscale[mb] = qscale;

                ctx->mb_bits[mb] = ctx->mb_rc[qscale][mb].bits;

            }

            bits = (bits+31)&~31; // padding

            if (bits > ctx->frame_bits)

                break;

        }

        //av_dlog(ctx->m.avctx, "lambda %d, up %u, down %u, bits %d, frame %d\n",

        //        lambda, last_higher, last_lower, bits, ctx->frame_bits);

        if (end) {

            if (bits > ctx->frame_bits)

                return -1;

            break;

        }

        if (bits < ctx->frame_bits) {

            last_lower = FFMIN(lambda, last_lower);

            if (last_higher != 0)

                lambda = (lambda+last_higher)>>1;

            else

                lambda -= down_step;

            down_step *= 5; // XXX tune ?

            up_step = 1<<LAMBDA_FRAC_BITS;

            lambda = FFMAX(1, lambda);

            if (lambda == last_lower)

                break;

        } else {

            last_higher = FFMAX(lambda, last_higher);

            if (last_lower != INT_MAX)

                lambda = (lambda+last_lower)>>1;

            else if ((int64_t)lambda + up_step > INT_MAX)

                return -1;

            else

                lambda += up_step;

            up_step = FFMIN((int64_t)up_step*5, INT_MAX);

            down_step = 1<<LAMBDA_FRAC_BITS;

        }

    }

    //av_dlog(ctx->m.avctx, "out lambda %d\n", lambda);

    ctx->lambda = lambda;

    return 0;

}

static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx)

{

    int lambda, up_step, down_step;

    int last_lower = INT_MAX, last_higher = 0;

    int x, y, q;



    for (q = 1; q < avctx->qmax; q++) {

        ctx->qscale = q;

        avctx->execute2(avctx, dnxhd_calc_bits_thread, NULL, NULL, ctx->m.mb_height);

    }

    up_step = down_step = 2<<LAMBDA_FRAC_BITS;

    lambda = ctx->lambda;



    for (;;) {

        int bits = 0;

        int end = 0;

        if (lambda == last_higher) {

            lambda++;

            end = 1; 

        }

        for (y = 0; y < ctx->m.mb_height; y++) {

            for (x = 0; x < ctx->m.mb_width; x++) {

                unsigned min = UINT_MAX;

                int qscale = 1;

                int mb = y*ctx->m.mb_width+x;

                for (q = 1; q < avctx->qmax; q++) {

                    unsigned score = ctx->mb_rc[q][mb].bits*lambda+(ctx->mb_rc[q][mb].ssd<<LAMBDA_FRAC_BITS);

                    if (score < min) {

                        min = score;

                        qscale = q;

                    }

                }

                bits += ctx->mb_rc[qscale][mb].bits;

                ctx->mb_qscale[mb] = qscale;

                ctx->mb_bits[mb] = ctx->mb_rc[qscale][mb].bits;

            }

            bits = (bits+31)&~31; 

            if (bits > ctx->frame_bits)

                break;

        }

        

        

        if (end) {

            if (bits > ctx->frame_bits)

                return -1;

            break;

        }

        if (bits < ctx->frame_bits) {

            last_lower = FFMIN(lambda, last_lower);

            if (last_higher != 0)

                lambda = (lambda+last_higher)>>1;

            else

                lambda -= down_step;

            down_step *= 5; 

            up_step = 1<<LAMBDA_FRAC_BITS;

            lambda = FFMAX(1, lambda);

            if (lambda == last_lower)

                break;

        } else {

            last_higher = FFMAX(lambda, last_higher);

            if (last_lower != INT_MAX)

                lambda = (lambda+last_lower)>>1;

            else if ((int64_t)lambda + up_step > INT_MAX)

                return -1;

            else

                lambda += up_step;

            up_step = FFMIN((int64_t)up_step*5, INT_MAX);

            down_step = 1<<LAMBDA_FRAC_BITS;

        }

    }

    

    ctx->lambda = lambda;

    return 0;

}

                    unsigned score = ctx->mb_rc[q][mb].bits*lambda+(ctx->mb_rc[q][mb].ssd<<LAMBDA_FRAC_BITS);
static int FUNC_0(AVCodecContext *VAR_0, DNXHDEncContext *VAR_1)

{

    int VAR_2, VAR_3, VAR_4;

    int VAR_5 = INT_MAX, VAR_6 = 0;

    int VAR_7, VAR_8, VAR_9;



    for (VAR_9 = 1; VAR_9 < VAR_0->qmax; VAR_9++) {

        VAR_1->qscale = VAR_9;

        VAR_0->execute2(VAR_0, dnxhd_calc_bits_thread, NULL, NULL, VAR_1->m.mb_height);

    }

    VAR_3 = VAR_4 = 2<<LAMBDA_FRAC_BITS;

    VAR_2 = VAR_1->VAR_2;



    for (;;) {

        int VAR_10 = 0;

        int VAR_11 = 0;

        if (VAR_2 == VAR_6) {

            VAR_2++;

            VAR_11 = 1; 

        }

        for (VAR_8 = 0; VAR_8 < VAR_1->m.mb_height; VAR_8++) {

            for (VAR_7 = 0; VAR_7 < VAR_1->m.mb_width; VAR_7++) {

                unsigned min = UINT_MAX;

                int qscale = 1;

                int mb = VAR_8*VAR_1->m.mb_width+VAR_7;

                for (VAR_9 = 1; VAR_9 < VAR_0->qmax; VAR_9++) {

                    unsigned score = VAR_1->mb_rc[VAR_9][mb].VAR_10*VAR_2+(VAR_1->mb_rc[VAR_9][mb].ssd<<LAMBDA_FRAC_BITS);

                    if (score < min) {

                        min = score;

                        qscale = VAR_9;

                    }

                }

                VAR_10 += VAR_1->mb_rc[qscale][mb].VAR_10;

                VAR_1->mb_qscale[mb] = qscale;

                VAR_1->mb_bits[mb] = VAR_1->mb_rc[qscale][mb].VAR_10;

            }

            VAR_10 = (VAR_10+31)&~31; 

            if (VAR_10 > VAR_1->frame_bits)

                break;

        }

        

        

        if (VAR_11) {

            if (VAR_10 > VAR_1->frame_bits)

                return -1;

            break;

        }

        if (VAR_10 < VAR_1->frame_bits) {

            VAR_5 = FFMIN(VAR_2, VAR_5);

            if (VAR_6 != 0)

                VAR_2 = (VAR_2+VAR_6)>>1;

            else

                VAR_2 -= VAR_4;

            VAR_4 *= 5; 

            VAR_3 = 1<<LAMBDA_FRAC_BITS;

            VAR_2 = FFMAX(1, VAR_2);

            if (VAR_2 == VAR_5)

                break;

        } else {

            VAR_6 = FFMAX(VAR_2, VAR_6);

            if (VAR_5 != INT_MAX)

                VAR_2 = (VAR_2+VAR_5)>>1;

            else if ((int64_t)VAR_2 + VAR_3 > INT_MAX)

                return -1;

            else

                VAR_2 += VAR_3;

            VAR_3 = FFMIN((int64_t)VAR_3*5, INT_MAX);

            VAR_4 = 1<<LAMBDA_FRAC_BITS;

        }

    }

    

    VAR_1->VAR_2 = VAR_2;

    return 0;

}

static int FUNC_0(AVCodecContext *VAR_0, DNXHDEncContext *VAR_1)
{ int VAR_2, VAR_3, VAR_4; int VAR_5 = INT_MAX, VAR_6 = 0; int VAR_7, VAR_8, VAR_9; for (VAR_9 = 1; VAR_9 < VAR_0->qmax; VAR_9++) { VAR_1->qscale = VAR_9; VAR_0->execute2(VAR_0, dnxhd_calc_bits_thread, NULL, NULL, VAR_1->m.mb_height); } VAR_3 = VAR_4 = 2<<LAMBDA_FRAC_BITS; VAR_2 = VAR_1->VAR_2; for (;;) { int VAR_10 = 0; int VAR_11 = 0; if (VAR_2 == VAR_6) { VAR_2++; VAR_11 = 1; } for (VAR_8 = 0; VAR_8 < VAR_1->m.mb_height; VAR_8++) { for (VAR_7 = 0; VAR_7 < VAR_1->m.mb_width; VAR_7++) { unsigned min = UINT_MAX; int qscale = 1; int mb = VAR_8*VAR_1->m.mb_width+VAR_7; for (VAR_9 = 1; VAR_9 < VAR_0->qmax; VAR_9++) { unsigned score = VAR_1->mb_rc[VAR_9][mb].VAR_10*VAR_2+(VAR_1->mb_rc[VAR_9][mb].ssd<<LAMBDA_FRAC_BITS); if (score < min) { min = score; qscale = VAR_9; } } VAR_10 += VAR_1->mb_rc[qscale][mb].VAR_10; VAR_1->mb_qscale[mb] = qscale; VAR_1->mb_bits[mb] = VAR_1->mb_rc[qscale][mb].VAR_10; } VAR_10 = (VAR_10+31)&~31; if (VAR_10 > VAR_1->frame_bits)
break; } if (VAR_11) { if (VAR_10 > VAR_1->frame_bits)
return -1; break; } if (VAR_10 < VAR_1->frame_bits) { VAR_5 = FFMIN(VAR_2, VAR_5); if (VAR_6 != 0)
VAR_2 = (VAR_2+VAR_6)>>1; else
VAR_2 -= VAR_4; VAR_4 *= 5; VAR_3 = 1<<LAMBDA_FRAC_BITS; VAR_2 = FFMAX(1, VAR_2); if (VAR_2 == VAR_5)
break; } else { VAR_6 = FFMAX(VAR_2, VAR_6); if (VAR_5 != INT_MAX)
VAR_2 = (VAR_2+VAR_5)>>1; else if ((int64_t)VAR_2 + VAR_3 > INT_MAX)
return -1; else
VAR_2 += VAR_3; VAR_3 = FFMIN((int64_t)VAR_3*5, INT_MAX); VAR_4 = 1<<LAMBDA_FRAC_BITS; } } VAR_1->VAR_2 = VAR_2; return 0; }