static int rate_control(AVCodecContext *avctx, void *arg)

{

    SliceArgs *slice_dat = arg;

    VC2EncContext *s = slice_dat->ctx;

    const int sx = slice_dat->x;

    const int sy = slice_dat->y;

    int quant_buf[2], bits_buf[2], quant = s->q_start, range = s->q_start/3;

    const int64_t top = slice_dat->bits_ceil;

    const double percent = s->tolerance;

    const double bottom = top - top*(percent/100.0f);

    int bits = count_hq_slice(s, sx, sy, quant);

    range -= range & 1; /* Make it an even number */

    while ((bits > top) || (bits < bottom)) {

        range *= bits > top ? +1 : -1;

        quant = av_clip(quant + range, 0, s->q_ceil);

        bits = count_hq_slice(s, sx, sy, quant);

        range = av_clip(range/2, 1, s->q_ceil);

        if (quant_buf[1] == quant) {

            quant = bits_buf[0] < bits ? quant_buf[0] : quant;

            bits = bits_buf[0] < bits ? bits_buf[0] : bits;

            break;

        }

        quant_buf[1] = quant_buf[0];

        quant_buf[0] = quant;

        bits_buf[1] = bits_buf[0];

        bits_buf[0] = bits;

    }

    slice_dat->quant_idx = av_clip(quant, 0, s->q_ceil);

    slice_dat->bytes = FFALIGN((bits >> 3), s->size_scaler) + 4 + s->prefix_bytes;



    return 0;

}

static int rate_control(AVCodecContext *avctx, void *arg)

{

    SliceArgs *slice_dat = arg;

    VC2EncContext *s = slice_dat->ctx;

    const int sx = slice_dat->x;

    const int sy = slice_dat->y;

    int quant_buf[2], bits_buf[2], quant = s->q_start, range = s->q_start/3;

    const int64_t top = slice_dat->bits_ceil;

    const double percent = s->tolerance;

    const double bottom = top - top*(percent/100.0f);

    int bits = count_hq_slice(s, sx, sy, quant);

    range -= range & 1; 

    while ((bits > top) || (bits < bottom)) {

        range *= bits > top ? +1 : -1;

        quant = av_clip(quant + range, 0, s->q_ceil);

        bits = count_hq_slice(s, sx, sy, quant);

        range = av_clip(range/2, 1, s->q_ceil);

        if (quant_buf[1] == quant) {

            quant = bits_buf[0] < bits ? quant_buf[0] : quant;

            bits = bits_buf[0] < bits ? bits_buf[0] : bits;

            break;

        }

        quant_buf[1] = quant_buf[0];

        quant_buf[0] = quant;

        bits_buf[1] = bits_buf[0];

        bits_buf[0] = bits;

    }

    slice_dat->quant_idx = av_clip(quant, 0, s->q_ceil);

    slice_dat->bytes = FFALIGN((bits >> 3), s->size_scaler) + 4 + s->prefix_bytes;



    return 0;

}

    int quant_buf[2], bits_buf[2], quant = s->q_start, range = s->q_start/3;             quant = bits_buf[0] < bits ? quant_buf[0] : quant;             bits = bits_buf[0] < bits ? bits_buf[0] : bits;         bits_buf[1] = bits_buf[0];         bits_buf[0] = bits;
static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1)

{

    SliceArgs *slice_dat = VAR_1;

    VC2EncContext *s = slice_dat->ctx;

    const int VAR_2 = slice_dat->x;

    const int VAR_3 = slice_dat->y;

    int VAR_4[2], VAR_5[2], VAR_6 = s->q_start, VAR_7 = s->q_start/3;

    const int64_t VAR_8 = slice_dat->bits_ceil;

    const double VAR_9 = s->tolerance;

    const double VAR_10 = VAR_8 - VAR_8*(VAR_9/100.0f);

    int VAR_11 = count_hq_slice(s, VAR_2, VAR_3, VAR_6);

    VAR_7 -= VAR_7 & 1; 

    while ((VAR_11 > VAR_8) || (VAR_11 < VAR_10)) {

        VAR_7 *= VAR_11 > VAR_8 ? +1 : -1;

        VAR_6 = av_clip(VAR_6 + VAR_7, 0, s->q_ceil);

        VAR_11 = count_hq_slice(s, VAR_2, VAR_3, VAR_6);

        VAR_7 = av_clip(VAR_7/2, 1, s->q_ceil);

        if (VAR_4[1] == VAR_6) {

            VAR_6 = VAR_5[0] < VAR_11 ? VAR_4[0] : VAR_6;

            VAR_11 = VAR_5[0] < VAR_11 ? VAR_5[0] : VAR_11;

            break;

        }

        VAR_4[1] = VAR_4[0];

        VAR_4[0] = VAR_6;

        VAR_5[1] = VAR_5[0];

        VAR_5[0] = VAR_11;

    }

    slice_dat->quant_idx = av_clip(VAR_6, 0, s->q_ceil);

    slice_dat->bytes = FFALIGN((VAR_11 >> 3), s->size_scaler) + 4 + s->prefix_bytes;



    return 0;

}

static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1)
{ SliceArgs *slice_dat = VAR_1; VC2EncContext *s = slice_dat->ctx; const int VAR_2 = slice_dat->x; const int VAR_3 = slice_dat->y; int VAR_4[2], VAR_5[2], VAR_6 = s->q_start, VAR_7 = s->q_start/3; const int64_t VAR_8 = slice_dat->bits_ceil; const double VAR_9 = s->tolerance; const double VAR_10 = VAR_8 - VAR_8*(VAR_9/100.0f); int VAR_11 = count_hq_slice(s, VAR_2, VAR_3, VAR_6); VAR_7 -= VAR_7 & 1; while ((VAR_11 > VAR_8) || (VAR_11 < VAR_10)) { VAR_7 *= VAR_11 > VAR_8 ? +1 : -1; VAR_6 = av_clip(VAR_6 + VAR_7, 0, s->q_ceil); VAR_11 = count_hq_slice(s, VAR_2, VAR_3, VAR_6); VAR_7 = av_clip(VAR_7/2, 1, s->q_ceil); if (VAR_4[1] == VAR_6) { VAR_6 = VAR_5[0] < VAR_11 ? VAR_4[0] : VAR_6; VAR_11 = VAR_5[0] < VAR_11 ? VAR_5[0] : VAR_11; break; } VAR_4[1] = VAR_4[0]; VAR_4[0] = VAR_6; VAR_5[1] = VAR_5[0]; VAR_5[0] = VAR_11; } slice_dat->quant_idx = av_clip(VAR_6, 0, s->q_ceil); slice_dat->bytes = FFALIGN((VAR_11 >> 3), s->size_scaler) + 4 + s->prefix_bytes; return 0; }