ratecontrol.c 99.2 KB
Newer Older
1
/*****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
2 3
 * ratecontrol.c: h264 encoder library (Rate Control)
 *****************************************************************************
4
 * Copyright (C) 2005-2008 x264 project
Laurent Aimar's avatar
Laurent Aimar committed
5
 *
Loren Merritt's avatar
Loren Merritt committed
6 7
 * Authors: Loren Merritt <lorenm@u.washington.edu>
 *          Michael Niedermayer <michaelni@gmx.at>
8 9
 *          Gabriel Bouvigne <gabriel.bouvigne@joost.com>
 *          Fiona Glaser <fiona@x264.com>
Loren Merritt's avatar
Loren Merritt committed
10
 *          Mns Rullgrd <mru@mru.ath.cx>
Laurent Aimar's avatar
Laurent Aimar committed
11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
Laurent Aimar's avatar
Laurent Aimar committed
25 26
 *****************************************************************************/

27
#define _ISOC99_SOURCE
28
#undef NDEBUG // always check asserts, the speed effect is far too small to disable them
29
#include <math.h>
Laurent Aimar's avatar
Laurent Aimar committed
30

31
#include "common/common.h"
Laurent Aimar's avatar
Laurent Aimar committed
32
#include "ratecontrol.h"
33
#include "me.h"
Laurent Aimar's avatar
Laurent Aimar committed
34

35 36 37
typedef struct
{
    int pict_type;
38
    int frame_type;
39
    int kept_as_ref;
40
    double qscale;
41
    int mv_bits;
42
    int tex_bits;
43
    int misc_bits;
44
    uint64_t expected_bits; /*total expected bits up to the current frame (current one excluded)*/
45
    double expected_vbv;
46
    double new_qscale;
47 48 49 50
    int new_qp;
    int i_count;
    int p_count;
    int s_count;
Loren Merritt's avatar
Loren Merritt committed
51
    float blurred_complexity;
Loren Merritt's avatar
Loren Merritt committed
52
    char direct_mode;
53 54
    int16_t weight[2];
    int16_t i_weight_denom;
55 56
    int refcount[16];
    int refs;
57 58
    int i_duration;
    int i_cpb_duration;
59 60
} ratecontrol_entry_t;

Loren Merritt's avatar
Loren Merritt committed
61 62 63 64 65
typedef struct
{
    double coeff;
    double count;
    double decay;
Fiona Glaser's avatar
Fiona Glaser committed
66
    double offset;
Loren Merritt's avatar
Loren Merritt committed
67 68
} predictor_t;

69 70 71
struct x264_ratecontrol_t
{
    /* constants */
Loren Merritt's avatar
Loren Merritt committed
72 73
    int b_abr;
    int b_2pass;
74
    int b_vbv;
75
    int b_vbv_min_rate;
76
    double fps;
Loren Merritt's avatar
Loren Merritt committed
77 78
    double bitrate;
    double rate_tolerance;
Fiona Glaser's avatar
Fiona Glaser committed
79
    double qcompress;
80
    int nmb;                    /* number of macroblocks in a frame */
81
    int qp_constant[5];
82

Loren Merritt's avatar
Loren Merritt committed
83 84
    /* current frame */
    ratecontrol_entry_t *rce;
85
    int qp;                     /* qp for current frame */
86
    float qpm;                  /* qp for current macroblock: precise float for AQ */
87 88
    float qpa_rc;               /* average of macroblocks' qp before aq */
    float qpa_aq;               /* average of macroblocks' qp after aq */
Fiona Glaser's avatar
Fiona Glaser committed
89
    float qp_novbv;             /* QP for the current frame if 1-pass VBV was disabled. */
Måns Rullgård's avatar
Måns Rullgård committed
90
    int qp_force;
91

Loren Merritt's avatar
Loren Merritt committed
92 93
    /* VBV stuff */
    double buffer_size;
Loren Merritt's avatar
Loren Merritt committed
94 95
    double buffer_fill_final;   /* real buffer as of the last finished frame */
    double buffer_fill;         /* planned buffer, if all in-progress frames hit their bit budget */
Loren Merritt's avatar
Loren Merritt committed
96
    double buffer_rate;         /* # of bits added to buffer_fill after each frame */
97
    double vbv_max_rate;        /* # of bits added to buffer_fill per second */
Loren Merritt's avatar
Loren Merritt committed
98
    predictor_t *pred;          /* predict frame size from satd */
99
    int single_frame_vbv;
Fiona Glaser's avatar
Fiona Glaser committed
100
    double rate_factor_max_increment; /* Don't allow RF above (CRF + this value). */
Loren Merritt's avatar
Loren Merritt committed
101 102 103 104 105

    /* ABR stuff */
    int    last_satd;
    double last_rceq;
    double cplxr_sum;           /* sum of bits*qscale/rceq */
106
    double expected_bits_sum;   /* sum of qscale2bits after rceq, ratefactor, and overflow, only includes finished frames */
Loren Merritt's avatar
Loren Merritt committed
107 108 109 110
    double wanted_bits_window;  /* target bitrate * window */
    double cbr_decay;
    double short_term_cplxsum;
    double short_term_cplxcount;
111
    double rate_factor_constant;
112 113
    double ip_offset;
    double pb_offset;
Loren Merritt's avatar
Loren Merritt committed
114

115 116
    /* 2pass stuff */
    FILE *p_stat_file_out;
117
    char *psz_stat_file_tmpname;
Fiona Glaser's avatar
Fiona Glaser committed
118 119 120 121
    FILE *p_mbtree_stat_file_out;
    char *psz_mbtree_stat_file_tmpname;
    char *psz_mbtree_stat_file_name;
    FILE *p_mbtree_stat_file_in;
122 123

    int num_entries;            /* number of ratecontrol_entry_ts */
Loren Merritt's avatar
Loren Merritt committed
124
    ratecontrol_entry_t *entry; /* FIXME: copy needed data and free this once init is done */
125 126 127
    double last_qscale;
    double last_qscale_for[5];  /* last qscale for a specific pict type, used for max_diff & ipb factor stuff  */
    int last_non_b_pict_type;
128 129
    double accum_p_qp;          /* for determining I-frame quant */
    double accum_p_norm;
130
    double last_accum_p_norm;
131
    double lmin[5];             /* min qscale by frame type */
132
    double lmax[5];
Loren Merritt's avatar
Loren Merritt committed
133
    double lstep;               /* max change (multiply) in qscale per frame */
134 135 136
    uint16_t *qp_buffer[2];     /* Global buffers for converting MB-tree quantizer data. */
    int qpbuf_pos;              /* In order to handle pyramid reordering, QP buffer acts as a stack.
                                 * This value is the current position (0 or 1). */
Loren Merritt's avatar
Loren Merritt committed
137

138
    /* MBRC stuff */
Fiona Glaser's avatar
Fiona Glaser committed
139 140
    float frame_size_estimated; /* Access to this variable must be atomic: double is
                                 * not atomic on all arches we care about */
141
    double frame_size_maximum;  /* Maximum frame size due to MinCR */
142
    double frame_size_planned;
143
    double slice_size_planned;
Fiona Glaser's avatar
Fiona Glaser committed
144
    double max_frame_error;
145 146
    predictor_t (*row_pred)[2];
    predictor_t row_preds[5][2];
Loren Merritt's avatar
Loren Merritt committed
147
    predictor_t *pred_b_from_p; /* predict B-frame size from P-frame satd */
148 149 150
    int bframes;                /* # consecutive B-frames before this P-frame */
    int bframe_bits;            /* total cost of those frames */

Loren Merritt's avatar
Loren Merritt committed
151 152
    int i_zones;
    x264_zone_t *zones;
153
    x264_zone_t *prev_zone;
154 155 156

    /* hrd stuff */
    int initial_cpb_removal_delay;
Kieran Kunhya's avatar
Kieran Kunhya committed
157
    int initial_cpb_removal_delay_offset;
158 159
    double nrt_first_access_unit; /* nominal removal time */
    double previous_cpb_final_arrival_time;
160
};
Laurent Aimar's avatar
Laurent Aimar committed
161

162

163
static int parse_zones( x264_t *h );
164
static int init_pass2(x264_t *);
165
static float rate_estimate_qscale( x264_t *h );
166
static int update_vbv( x264_t *h, int bits );
167
static void update_vbv_plan( x264_t *h, int overhead );
168 169
static double predict_size( predictor_t *p, double q, double var );
static void update_predictor( predictor_t *p, double q, double var, double bits );
170

Fiona Glaser's avatar
Fiona Glaser committed
171 172 173 174 175 176 177 178 179
#define CMP_OPT_FIRST_PASS( opt, param_val )\
{\
    if( ( p = strstr( opts, opt "=" ) ) && sscanf( p, opt "=%d" , &i ) && param_val != i )\
    {\
        x264_log( h, X264_LOG_ERROR, "different " opt " setting than first pass (%d vs %d)\n", param_val, i );\
        return -1;\
    }\
}

180 181
/* Terminology:
 * qp = h.264's quantizer
182
 * qscale = linearized quantizer = Lagrange multiplier
183
 */
184
static inline double qp2qscale( double qp )
185
{
186
    return 0.85 * pow( 2.0, ( qp - 12.0 ) / 6.0 );
187
}
188
static inline double qscale2qp( double qscale )
189
{
190
    return 12.0 + 6.0 * log2( qscale/0.85 );
191 192
}

193 194 195 196
/* Texture bitrate is not quite inversely proportional to qscale,
 * probably due the the changing number of SKIP blocks.
 * MV bits level off at about qp<=12, because the lambda used
 * for motion estimation is constant there. */
197
static inline double qscale2bits( ratecontrol_entry_t *rce, double qscale )
198
{
199
    if( qscale<0.1 )
200
        qscale = 0.1;
201
    return (rce->tex_bits + .1) * pow( rce->qscale / qscale, 1.1 )
202
           + rce->mv_bits * pow( X264_MAX(rce->qscale, 1) / X264_MAX(qscale, 1), 0.5 )
203
           + rce->misc_bits;
204 205
}

Fiona Glaser's avatar
Fiona Glaser committed
206 207 208 209 210 211 212 213 214 215 216 217
static ALWAYS_INLINE uint32_t ac_energy_plane( x264_t *h, int mb_x, int mb_y, x264_frame_t *frame, int i )
{
    int w = i ? 8 : 16;
    int shift = i ? 6 : 8;
    int stride = frame->i_stride[i];
    int offset = h->mb.b_interlaced
        ? w * (mb_x + (mb_y&~1) * stride) + (mb_y&1) * stride
        : w * (mb_x + mb_y * stride);
    int pix = i ? PIXEL_8x8 : PIXEL_16x16;
    stride <<= h->mb.b_interlaced;
    uint64_t res = h->pixf.var[pix]( frame->plane[i] + offset, stride );
    uint32_t sum = (uint32_t)res;
218 219 220 221
    uint32_t ssd = res >> 32;
    frame->i_pixel_sum[i] += sum;
    frame->i_pixel_ssd[i] += ssd;
    return ssd - (sum * sum >> shift);
Fiona Glaser's avatar
Fiona Glaser committed
222 223
}

224
// Find the total AC energy of the block in all planes.
225
static NOINLINE uint32_t x264_ac_energy_mb( x264_t *h, int mb_x, int mb_y, x264_frame_t *frame )
226
{
227 228 229 230
    /* This function contains annoying hacks because GCC has a habit of reordering emms
     * and putting it after floating point ops.  As a result, we put the emms at the end of the
     * function and make sure that its always called before the float math.  Noinline makes
     * sure no reordering goes on. */
Fiona Glaser's avatar
Fiona Glaser committed
231 232 233
    uint32_t var = ac_energy_plane( h, mb_x, mb_y, frame, 0 );
    var         += ac_energy_plane( h, mb_x, mb_y, frame, 1 );
    var         += ac_energy_plane( h, mb_x, mb_y, frame, 2 );
234
    x264_emms();
235 236 237
    return var;
}

238
void x264_adaptive_quant_frame( x264_t *h, x264_frame_t *frame )
239
{
240 241
    /* constants chosen to result in approximately the same overall bitrate as without AQ.
     * FIXME: while they're written in 5 significant digits, they're only tuned to 2. */
Anton Mitrofanov's avatar
Anton Mitrofanov committed
242 243
    float strength;
    float avg_adj = 0.f;
244 245 246 247
    int width = h->sps->i_mb_width;
    int height = h->sps->i_mb_height;
    /* Initialize frame stats */
    for( int i = 0; i < 3; i++ )
Fiona Glaser's avatar
Fiona Glaser committed
248
    {
249 250
        frame->i_pixel_sum[i] = 0;
        frame->i_pixel_ssd[i] = 0;
Fiona Glaser's avatar
Fiona Glaser committed
251 252
    }

253 254
    /* Degenerate cases */
    if( h->param.rc.i_aq_mode == X264_AQ_NONE || h->param.rc.f_aq_strength == 0 )
Anton Mitrofanov's avatar
Anton Mitrofanov committed
255
    {
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
        /* Need to init it anyways for MB tree */
        if( h->param.rc.f_aq_strength == 0 )
        {
            memset( frame->f_qp_offset, 0, h->mb.i_mb_count * sizeof(float) );
            memset( frame->f_qp_offset_aq, 0, h->mb.i_mb_count * sizeof(float) );
            if( h->frames.b_have_lowres )
                for( int mb_xy = 0; mb_xy < h->mb.i_mb_count; mb_xy++ )
                    frame->i_inv_qscale_factor[mb_xy] = 256;
        }
        /* Need variance data for weighted prediction */
        if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_FAKE || h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
        {
            for( int mb_y = 0; mb_y < height; mb_y++ )
                for( int mb_x = 0; mb_x < width; mb_x++ )
                    x264_ac_energy_mb( h, mb_x, mb_y, frame );
        }
        else
            return;
Anton Mitrofanov's avatar
Anton Mitrofanov committed
274
    }
275
    /* Actual adaptive quantization */
Anton Mitrofanov's avatar
Anton Mitrofanov committed
276
    else
277 278
    {
        if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE )
279
        {
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
            float avg_adj_pow2 = 0.f;
            for( int mb_y = 0; mb_y < height; mb_y++ )
                for( int mb_x = 0; mb_x < width; mb_x++ )
                {
                    uint32_t energy = x264_ac_energy_mb( h, mb_x, mb_y, frame );
                    float qp_adj = powf( energy + 1, 0.125f );
                    frame->f_qp_offset[mb_x + mb_y*h->mb.i_mb_stride] = qp_adj;
                    avg_adj += qp_adj;
                    avg_adj_pow2 += qp_adj * qp_adj;
                }
            avg_adj /= h->mb.i_mb_count;
            avg_adj_pow2 /= h->mb.i_mb_count;
            strength = h->param.rc.f_aq_strength * avg_adj;
            avg_adj = avg_adj - 0.5f * (avg_adj_pow2 - 14.f) / avg_adj;
        }
        else
            strength = h->param.rc.f_aq_strength * 1.0397f;

        for( int mb_y = 0; mb_y < height; mb_y++ )
            for( int mb_x = 0; mb_x < width; mb_x++ )
Anton Mitrofanov's avatar
Anton Mitrofanov committed
300
            {
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
                float qp_adj;
                if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE )
                {
                    qp_adj = frame->f_qp_offset[mb_x + mb_y*h->mb.i_mb_stride];
                    qp_adj = strength * (qp_adj - avg_adj);
                }
                else
                {
                    uint32_t energy = x264_ac_energy_mb( h, mb_x, mb_y, frame );
                    qp_adj = strength * (x264_log2( X264_MAX(energy, 1) ) - 14.427f);
                }
                frame->f_qp_offset[mb_x + mb_y*h->mb.i_mb_stride] =
                frame->f_qp_offset_aq[mb_x + mb_y*h->mb.i_mb_stride] = qp_adj;
                if( h->frames.b_have_lowres )
                    frame->i_inv_qscale_factor[mb_x + mb_y*h->mb.i_mb_stride] = x264_exp2fix8(qp_adj);
Anton Mitrofanov's avatar
Anton Mitrofanov committed
316
            }
317 318 319 320 321 322 323 324 325 326 327
    }

    /* Remove mean from SSD calculation */
    for( int i = 0; i < 3; i++ )
    {
        uint64_t ssd = frame->i_pixel_ssd[i];
        uint64_t sum = frame->i_pixel_sum[i];
        int w = width*16>>!!i;
        int h = height*16>>!!i;
        frame->i_pixel_ssd[i] = ssd - (sum * sum + w * h / 2) / (w * h);
    }
328 329
}

Fiona Glaser's avatar
Fiona Glaser committed
330 331 332 333 334
int x264_macroblock_tree_read( x264_t *h, x264_frame_t *frame )
{
    x264_ratecontrol_t *rc = h->rc;
    uint8_t i_type_actual = rc->entry[frame->i_frame].pict_type;

335
    if( rc->entry[frame->i_frame].kept_as_ref )
Fiona Glaser's avatar
Fiona Glaser committed
336 337
    {
        uint8_t i_type;
338 339 340 341 342
        if( rc->qpbuf_pos < 0 )
        {
            do
            {
                rc->qpbuf_pos++;
Fiona Glaser's avatar
Fiona Glaser committed
343

344 345 346 347
                if( !fread( &i_type, 1, 1, rc->p_mbtree_stat_file_in ) )
                    goto fail;
                if( fread( rc->qp_buffer[rc->qpbuf_pos], sizeof(uint16_t), h->mb.i_mb_count, rc->p_mbtree_stat_file_in ) != h->mb.i_mb_count )
                    goto fail;
Fiona Glaser's avatar
Fiona Glaser committed
348

349 350 351 352 353 354
                if( i_type != i_type_actual && rc->qpbuf_pos == 1 )
                {
                    x264_log(h, X264_LOG_ERROR, "MB-tree frametype %d doesn't match actual frametype %d.\n", i_type, i_type_actual);
                    return -1;
                }
            } while( i_type != i_type_actual );
Fiona Glaser's avatar
Fiona Glaser committed
355 356
        }

357
        for( int i = 0; i < h->mb.i_mb_count; i++ )
358
        {
359
            frame->f_qp_offset[i] = ((float)(int16_t)endian_fix16( rc->qp_buffer[rc->qpbuf_pos][i] )) * (1/256.0);
360 361 362
            if( h->frames.b_have_lowres )
                frame->i_inv_qscale_factor[i] = x264_exp2fix8(frame->f_qp_offset[i]);
        }
363
        rc->qpbuf_pos--;
Fiona Glaser's avatar
Fiona Glaser committed
364 365 366 367 368 369 370 371 372
    }
    else
        x264_adaptive_quant_frame( h, frame );
    return 0;
fail:
    x264_log(h, X264_LOG_ERROR, "Incomplete MB-tree stats file.\n");
    return -1;
}

373 374 375 376
int x264_reference_build_list_optimal( x264_t *h )
{
    ratecontrol_entry_t *rce = h->rc->rce;
    x264_frame_t *frames[16];
Dylan Yudaken's avatar
Dylan Yudaken committed
377 378
    x264_weight_t weights[16][3];
    int refcount[16];
379 380 381 382 383

    if( rce->refs != h->i_ref0 )
        return -1;

    memcpy( frames, h->fref0, sizeof(frames) );
Dylan Yudaken's avatar
Dylan Yudaken committed
384 385
    memcpy( refcount, rce->refcount, sizeof(refcount) );
    memcpy( weights, h->fenc->weight, sizeof(weights) );
Dylan Yudaken's avatar
Dylan Yudaken committed
386
    memset( &h->fenc->weight[1][0], 0, sizeof(x264_weight_t[15][3]) );
387 388 389

    /* For now don't reorder ref 0; it seems to lower quality
       in most cases due to skips. */
390
    for( int ref = 1; ref < h->i_ref0; ref++ )
391 392 393
    {
        int max = -1;
        int bestref = 1;
Dylan Yudaken's avatar
Dylan Yudaken committed
394

395
        for( int i = 1; i < h->i_ref0; i++ )
396 397
            /* Favor lower POC as a tiebreaker. */
            COPY2_IF_GT( max, refcount[i], bestref, i );
Dylan Yudaken's avatar
Dylan Yudaken committed
398 399 400 401 402

        /* FIXME: If there are duplicates from frames other than ref0 then it is possible
         * that the optimal ordering doesnt place every duplicate. */

        refcount[bestref] = -1;
403
        h->fref0[ref] = frames[bestref];
Dylan Yudaken's avatar
Dylan Yudaken committed
404
        memcpy( h->fenc->weight[ref], weights[bestref], sizeof(weights[bestref]) );
405 406 407 408 409
    }

    return 0;
}

Fiona Glaser's avatar
Fiona Glaser committed
410 411 412
static char *x264_strcat_filename( char *input, char *suffix )
{
    char *output = x264_malloc( strlen( input ) + strlen( suffix ) + 1 );
413 414
    if( !output )
        return NULL;
Fiona Glaser's avatar
Fiona Glaser committed
415 416 417 418 419
    strcpy( output, input );
    strcat( output, suffix );
    return output;
}

420 421 422 423 424 425
void x264_ratecontrol_init_reconfigurable( x264_t *h, int b_init )
{
    x264_ratecontrol_t *rc = h->rc;
    if( !b_init && rc->b_2pass )
        return;

426 427 428 429 430 431 432 433 434 435
    if( h->param.rc.i_rc_method == X264_RC_CRF )
    {
        /* Arbitrary rescaling to make CRF somewhat similar to QP.
         * Try to compensate for MB-tree's effects as well. */
        double base_cplx = h->mb.i_mb_count * (h->param.i_bframe ? 120 : 80);
        double mbtree_offset = h->param.rc.b_mb_tree ? (1.0-h->param.rc.f_qcompress)*13.5 : 0;
        rc->rate_factor_constant = pow( base_cplx, 1 - rc->qcompress )
                                 / qp2qscale( h->param.rc.f_rf_constant + mbtree_offset );
    }

436 437 438 439 440 441 442 443 444 445 446 447 448
    if( h->param.rc.i_vbv_max_bitrate > 0 && h->param.rc.i_vbv_buffer_size > 0 )
    {
        if( h->param.rc.i_vbv_buffer_size < (int)(h->param.rc.i_vbv_max_bitrate / rc->fps) )
        {
            h->param.rc.i_vbv_buffer_size = h->param.rc.i_vbv_max_bitrate / rc->fps;
            x264_log( h, X264_LOG_WARNING, "VBV buffer size cannot be smaller than one frame, using %d kbit\n",
                      h->param.rc.i_vbv_buffer_size );
        }

        /* We don't support changing the ABR bitrate right now,
           so if the stream starts as CBR, keep it CBR. */
        if( rc->b_vbv_min_rate )
            h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate;
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501

        int vbv_buffer_size = h->param.rc.i_vbv_buffer_size * 1000;
        int vbv_max_bitrate = h->param.rc.i_vbv_max_bitrate * 1000;

        /* Init HRD */
        if( h->param.i_nal_hrd && b_init )
        {
            h->sps->vui.hrd.i_cpb_cnt = 1;
            h->sps->vui.hrd.b_cbr_hrd = h->param.i_nal_hrd == X264_NAL_HRD_CBR;
            h->sps->vui.hrd.i_time_offset_length = 0;

            #define BR_SHIFT  6
            #define CPB_SHIFT 4

            int bitrate = 1000*h->param.rc.i_vbv_max_bitrate;
            int bufsize = 1000*h->param.rc.i_vbv_buffer_size;

            // normalize HRD size and rate to the value / scale notation
            h->sps->vui.hrd.i_bit_rate_scale = x264_clip3( x264_ctz( bitrate ) - BR_SHIFT, 0, 15 );
            h->sps->vui.hrd.i_bit_rate_value = bitrate >> ( h->sps->vui.hrd.i_bit_rate_scale + BR_SHIFT );
            h->sps->vui.hrd.i_bit_rate_unscaled = h->sps->vui.hrd.i_bit_rate_value << ( h->sps->vui.hrd.i_bit_rate_scale + BR_SHIFT );
            h->sps->vui.hrd.i_cpb_size_scale = x264_clip3( x264_ctz( bufsize ) - CPB_SHIFT, 0, 15 );
            h->sps->vui.hrd.i_cpb_size_value = bufsize >> ( h->sps->vui.hrd.i_cpb_size_scale + CPB_SHIFT );
            h->sps->vui.hrd.i_cpb_size_unscaled = h->sps->vui.hrd.i_cpb_size_value << ( h->sps->vui.hrd.i_cpb_size_scale + CPB_SHIFT );

            #undef CPB_SHIFT
            #undef BR_SHIFT

            // arbitrary
            #define MAX_DURATION 0.5

            int max_cpb_output_delay = h->param.i_keyint_max * MAX_DURATION * h->sps->vui.i_time_scale / h->sps->vui.i_num_units_in_tick;
            int max_dpb_output_delay = h->sps->vui.i_max_dec_frame_buffering * MAX_DURATION * h->sps->vui.i_time_scale / h->sps->vui.i_num_units_in_tick;
            int max_delay = (int)(90000.0 * (double)h->sps->vui.hrd.i_cpb_size_unscaled / h->sps->vui.hrd.i_bit_rate_unscaled + 0.5);

            h->sps->vui.hrd.i_initial_cpb_removal_delay_length = 2 + x264_clip3( 32 - x264_clz( max_delay ), 4, 22 );
            h->sps->vui.hrd.i_cpb_removal_delay_length = x264_clip3( 32 - x264_clz( max_cpb_output_delay ), 4, 32 );
            h->sps->vui.hrd.i_dpb_output_delay_length  = x264_clip3( 32 - x264_clz( max_dpb_output_delay ), 4, 32 );

            #undef MAX_DURATION

            vbv_buffer_size = X264_MIN( vbv_buffer_size, h->sps->vui.hrd.i_cpb_size_unscaled );
            vbv_max_bitrate = X264_MIN( vbv_max_bitrate, h->sps->vui.hrd.i_bit_rate_unscaled );
        }
        else if( h->param.i_nal_hrd && !b_init )
        {
            x264_log( h, X264_LOG_WARNING, "VBV parameters cannot be changed when NAL HRD is in use\n" );
            return;
        }

        rc->buffer_rate = vbv_max_bitrate / rc->fps;
        rc->vbv_max_rate = vbv_max_bitrate;
        rc->buffer_size = vbv_buffer_size;
502 503 504
        rc->single_frame_vbv = rc->buffer_rate * 1.1 > rc->buffer_size;
        rc->cbr_decay = 1.0 - rc->buffer_rate / rc->buffer_size
                      * 0.5 * X264_MAX(0, 1.5 - rc->buffer_rate * rc->fps / rc->bitrate);
Fiona Glaser's avatar
Fiona Glaser committed
505 506 507 508 509 510 511 512 513
        if( h->param.rc.i_rc_method == X264_RC_CRF && h->param.rc.f_rf_constant_max )
        {
            rc->rate_factor_max_increment = h->param.rc.f_rf_constant_max - h->param.rc.f_rf_constant;
            if( rc->rate_factor_max_increment <= 0 )
            {
                x264_log( h, X264_LOG_WARNING, "CRF max must be greater than CRF\n" );
                rc->rate_factor_max_increment = 0;
            }
        }
514 515 516 517
        if( b_init )
        {
            if( h->param.rc.f_vbv_buffer_init > 1. )
                h->param.rc.f_vbv_buffer_init = x264_clip3f( h->param.rc.f_vbv_buffer_init / h->param.rc.i_vbv_buffer_size, 0, 1 );
Kieran Kunhya's avatar
Kieran Kunhya committed
518
            h->param.rc.f_vbv_buffer_init = x264_clip3f( X264_MAX( h->param.rc.f_vbv_buffer_init, rc->buffer_rate / rc->buffer_size ), 0, 1);
519 520 521 522 523 524 525 526 527
            rc->buffer_fill_final = rc->buffer_size * h->param.rc.f_vbv_buffer_init;
            rc->b_vbv = 1;
            rc->b_vbv_min_rate = !rc->b_2pass
                          && h->param.rc.i_rc_method == X264_RC_ABR
                          && h->param.rc.i_vbv_max_bitrate <= h->param.rc.i_bitrate;
        }
    }
}

528
int x264_ratecontrol_new( x264_t *h )
Laurent Aimar's avatar
Laurent Aimar committed
529
{
530
    x264_ratecontrol_t *rc;
531

532
    x264_emms();
533

534 535
    CHECKED_MALLOCZERO( h->rc, h->param.i_threads * sizeof(x264_ratecontrol_t) );
    rc = h->rc;
536

537 538
    rc->b_abr = h->param.rc.i_rc_method != X264_RC_CQP && !h->param.rc.b_stat_read;
    rc->b_2pass = h->param.rc.i_rc_method == X264_RC_ABR && h->param.rc.b_stat_read;
Loren Merritt's avatar
Loren Merritt committed
539

540
    /* FIXME: use integers */
541
    if( h->param.i_fps_num > 0 && h->param.i_fps_den > 0 )
542 543 544 545
        rc->fps = (float) h->param.i_fps_num / h->param.i_fps_den;
    else
        rc->fps = 25.0;

Fiona Glaser's avatar
Fiona Glaser committed
546 547 548 549 550 551 552 553
    if( h->param.rc.b_mb_tree )
    {
        h->param.rc.f_pb_factor = 1;
        rc->qcompress = 1;
    }
    else
        rc->qcompress = h->param.rc.f_qcompress;

554
    rc->bitrate = h->param.rc.i_bitrate * 1000.;
Loren Merritt's avatar
Loren Merritt committed
555
    rc->rate_tolerance = h->param.rc.f_rate_tolerance;
556
    rc->nmb = h->mb.i_mb_count;
Loren Merritt's avatar
Loren Merritt committed
557 558 559
    rc->last_non_b_pict_type = -1;
    rc->cbr_decay = 1.0;

560
    if( h->param.rc.i_rc_method == X264_RC_CRF && h->param.rc.b_stat_read )
561
    {
562
        x264_log(h, X264_LOG_ERROR, "constant rate-factor is incompatible with 2pass.\n");
563 564
        return -1;
    }
565 566 567 568

    x264_ratecontrol_init_reconfigurable( h, 1 );

    if( rc->rate_tolerance < 0.01 )
Fiona Glaser's avatar
Fiona Glaser committed
569
    {
570
        x264_log(h, X264_LOG_WARNING, "bitrate tolerance too small, using .01\n");
571 572
        rc->rate_tolerance = 0.01;
    }
573

574
    h->mb.b_variable_qp = rc->b_vbv || h->param.rc.i_aq_mode;
575

Loren Merritt's avatar
Loren Merritt committed
576 577
    if( rc->b_abr )
    {
578
        /* FIXME ABR_INIT_QP is actually used only in CRF */
579
#define ABR_INIT_QP ( h->param.rc.i_rc_method == X264_RC_CRF ? h->param.rc.f_rf_constant : 24 )
Loren Merritt's avatar
Loren Merritt committed
580 581
        rc->accum_p_norm = .01;
        rc->accum_p_qp = ABR_INIT_QP * rc->accum_p_norm;
582
        /* estimated ratio that produces a reasonable QP for the first I-frame */
Fiona Glaser's avatar
Fiona Glaser committed
583
        rc->cplxr_sum = .01 * pow( 7.0e5, rc->qcompress ) * pow( h->mb.i_mb_count, 0.5 );
584 585
        rc->wanted_bits_window = 1.0 * rc->bitrate / rc->fps;
        rc->last_non_b_pict_type = SLICE_TYPE_I;
586 587
    }

588 589
    rc->ip_offset = 6.0 * log2f( h->param.rc.f_ip_factor );
    rc->pb_offset = 6.0 * log2f( h->param.rc.f_pb_factor );
Loren Merritt's avatar
Loren Merritt committed
590
    rc->qp_constant[SLICE_TYPE_P] = h->param.rc.i_qp_constant;
591 592
    rc->qp_constant[SLICE_TYPE_I] = x264_clip3( h->param.rc.i_qp_constant - rc->ip_offset + 0.5, 0, 51 );
    rc->qp_constant[SLICE_TYPE_B] = x264_clip3( h->param.rc.i_qp_constant + rc->pb_offset + 0.5, 0, 51 );
Fiona Glaser's avatar
Fiona Glaser committed
593
    h->mb.ip_offset = rc->ip_offset + 0.5;
594

Loren Merritt's avatar
Loren Merritt committed
595
    rc->lstep = pow( 2, h->param.rc.i_qp_step / 6.0 );
596
    rc->last_qscale = qp2qscale( 26 );
Fiona Glaser's avatar
Fiona Glaser committed
597 598
    int num_preds = h->param.b_sliced_threads * h->param.i_threads + 1;
    CHECKED_MALLOC( rc->pred, 5 * sizeof(predictor_t) * num_preds );
599
    CHECKED_MALLOC( rc->pred_b_from_p, sizeof(predictor_t) );
600
    for( int i = 0; i < 5; i++ )
601
    {
602
        rc->last_qscale_for[i] = qp2qscale( ABR_INIT_QP );
603 604
        rc->lmin[i] = qp2qscale( h->param.rc.i_qp_min );
        rc->lmax[i] = qp2qscale( h->param.rc.i_qp_max );
605
        for( int j = 0; j < num_preds; j++ )
Fiona Glaser's avatar
Fiona Glaser committed
606 607 608 609 610 611
        {
            rc->pred[i+j*5].coeff= 2.0;
            rc->pred[i+j*5].count= 1.0;
            rc->pred[i+j*5].decay= 0.5;
            rc->pred[i+j*5].offset= 0.0;
        }
612
        for( int j = 0; j < 2; j++ )
613 614 615 616 617 618
        {
            rc->row_preds[i][j].coeff= .25;
            rc->row_preds[i][j].count= 1.0;
            rc->row_preds[i][j].decay= 0.5;
            rc->row_preds[i][j].offset= 0.0;
        }
619
    }
Loren Merritt's avatar
Loren Merritt committed
620
    *rc->pred_b_from_p = rc->pred[0];
621

622
    if( parse_zones( h ) < 0 )
623 624
    {
        x264_log( h, X264_LOG_ERROR, "failed to parse zones\n" );
625
        return -1;
626
    }
Loren Merritt's avatar
Loren Merritt committed
627

628 629 630
    /* Load stat file and init 2pass algo */
    if( h->param.rc.b_stat_read )
    {
631
        char *p, *stats_in, *stats_buf;
632 633 634

        /* read 1st pass stats */
        assert( h->param.rc.psz_stat_in );
635 636
        stats_buf = stats_in = x264_slurp_file( h->param.rc.psz_stat_in );
        if( !stats_buf )
637 638 639 640
        {
            x264_log(h, X264_LOG_ERROR, "ratecontrol_init: can't open stats file\n");
            return -1;
        }
Fiona Glaser's avatar
Fiona Glaser committed
641 642 643
        if( h->param.rc.b_mb_tree )
        {
            char *mbtree_stats_in = x264_strcat_filename( h->param.rc.psz_stat_in, ".mbtree" );
644 645
            if( !mbtree_stats_in )
                return -1;
Fiona Glaser's avatar
Fiona Glaser committed
646 647 648 649 650 651 652 653
            rc->p_mbtree_stat_file_in = fopen( mbtree_stats_in, "rb" );
            x264_free( mbtree_stats_in );
            if( !rc->p_mbtree_stat_file_in )
            {
                x264_log(h, X264_LOG_ERROR, "ratecontrol_init: can't open mbtree stats file\n");
                return -1;
            }
        }
654

655 656 657
        /* check whether 1st pass options were compatible with current options */
        if( !strncmp( stats_buf, "#options:", 9 ) )
        {
658
            int i, j;
659
            uint32_t k, l;
660 661 662 663 664 665
            char *opts = stats_buf;
            stats_in = strchr( stats_buf, '\n' );
            if( !stats_in )
                return -1;
            *stats_in = '\0';
            stats_in++;
666 667 668 669 670 671 672 673 674 675 676
            if( sscanf( opts, "#options: %dx%d", &i, &j ) != 2 )
            {
                x264_log( h, X264_LOG_ERROR, "resolution specified in stats file not valid\n" );
                return -1;
            }
            else if( h->param.rc.b_mb_tree && (i != h->param.i_width || j != h->param.i_height)  )
            {
                x264_log( h, X264_LOG_ERROR, "MB-tree doesn't support different resolution than 1st pass (%dx%d vs %dx%d)\n",
                          h->param.i_width, h->param.i_height, i, j );
                return -1;
            }
677

678
            if( ( p = strstr( opts, "timebase=" ) ) && sscanf( p, "timebase=%u/%u", &k, &l ) != 2 )
679 680 681 682
            {
                x264_log( h, X264_LOG_ERROR, "timebase specified in stats file not valid\n" );
                return -1;
            }
683
            if( k != h->param.i_timebase_num || l != h->param.i_timebase_den )
684
            {
685 686
                x264_log( h, X264_LOG_ERROR, "timebase mismatch with 1st pass (%u/%u vs %u/%u)\n",
                          h->param.i_timebase_num, h->param.i_timebase_den, k, l );
687 688 689
                return -1;
            }

690
            CMP_OPT_FIRST_PASS( "weightp", X264_MAX( 0, h->param.analyse.i_weighted_pred ) );
Fiona Glaser's avatar
Fiona Glaser committed
691 692 693 694
            CMP_OPT_FIRST_PASS( "bframes", h->param.i_bframe );
            CMP_OPT_FIRST_PASS( "b_pyramid", h->param.i_bframe_pyramid );
            CMP_OPT_FIRST_PASS( "intra_refresh", h->param.b_intra_refresh );
            CMP_OPT_FIRST_PASS( "keyint", h->param.i_keyint_max );
695

696
            if( strstr( opts, "qp=0" ) && h->param.rc.i_rc_method == X264_RC_ABR )
697
                x264_log( h, X264_LOG_WARNING, "1st pass was lossless, bitrate prediction will be inaccurate\n" );
698

699 700 701 702 703 704
            if( !strstr( opts, "direct=3" ) && h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_AUTO )
            {
                x264_log( h, X264_LOG_WARNING, "direct=auto not used on the first pass\n" );
                h->mb.b_direct_auto_write = 1;
            }

705 706
            if( ( p = strstr( opts, "b_adapt=" ) ) && sscanf( p, "b_adapt=%d", &i ) && i >= X264_B_ADAPT_NONE && i <= X264_B_ADAPT_TRELLIS )
                h->param.i_bframe_adaptive = i;
707
            else if( h->param.i_bframe )
708 709 710 711
            {
                x264_log( h, X264_LOG_ERROR, "b_adapt method specified in stats file not valid\n" );
                return -1;
            }
Fiona Glaser's avatar
Fiona Glaser committed
712

713
            if( (h->param.rc.b_mb_tree || h->param.rc.i_vbv_buffer_size) && ( p = strstr( opts, "rc_lookahead=" ) ) && sscanf( p, "rc_lookahead=%d", &i ) )
Fiona Glaser's avatar
Fiona Glaser committed
714
                h->param.rc.i_lookahead = i;
715 716
        }

717 718
        /* find number of pics */
        p = stats_in;
719 720 721 722
        int num_entries;
        for( num_entries = -1; p; num_entries++ )
            p = strchr( p + 1, ';' );
        if( !num_entries )
Loren Merritt's avatar
Loren Merritt committed
723 724 725
        {
            x264_log(h, X264_LOG_ERROR, "empty stats file\n");
            return -1;
726
        }
727
        rc->num_entries = num_entries;
728

729
        if( h->param.i_frame_total < rc->num_entries && h->param.i_frame_total > 0 )
730 731 732 733
        {
            x264_log( h, X264_LOG_WARNING, "2nd pass has fewer frames than 1st pass (%d vs %d)\n",
                      h->param.i_frame_total, rc->num_entries );
        }
734
        if( h->param.i_frame_total > rc->num_entries )
735 736 737 738 739 740
        {
            x264_log( h, X264_LOG_ERROR, "2nd pass has more frames than 1st pass (%d vs %d)\n",
                      h->param.i_frame_total, rc->num_entries );
            return -1;
        }

741
        CHECKED_MALLOCZERO( rc->entry, rc->num_entries * sizeof(ratecontrol_entry_t) );
742 743

        /* init all to skipped p frames */
744
        for( int i = 0; i < rc->num_entries; i++ )
Fiona Glaser's avatar
Fiona Glaser committed
745
        {
746
            ratecontrol_entry_t *rce = &rc->entry[i];
747
            rce->pict_type = SLICE_TYPE_P;
748
            rce->qscale = rce->new_qscale = qp2qscale( 20 );
749 750 751 752 753 754
            rce->misc_bits = rc->nmb + 10;
            rce->new_qp = 0;
        }

        /* read stats */
        p = stats_in;
755
        for( int i = 0; i < rc->num_entries; i++ )
Fiona Glaser's avatar
Fiona Glaser committed
756
        {
757
            ratecontrol_entry_t *rce;
758 759
            int frame_number;
            char pict_type;
760 761 762
            int e;
            char *next;
            float qp;
763
            int ref;
764 765

            next= strchr(p, ';');
766 767 768
            if( next )
                *next++ = 0; //sscanf is unbelievably slow on long strings
            e = sscanf( p, " in:%d ", &frame_number );
769

770
            if( frame_number < 0 || frame_number >= rc->num_entries )
Loren Merritt's avatar
Loren Merritt committed
771
            {
772
                x264_log( h, X264_LOG_ERROR, "bad frame number (%d) at stats line %d\n", frame_number, i );
Loren Merritt's avatar
Loren Merritt committed
773 774
                return -1;
            }
775
            rce = &rc->entry[frame_number];
Loren Merritt's avatar
Loren Merritt committed
776
            rce->direct_mode = 0;
777

778
            e += sscanf( p, " in:%*d out:%*d type:%c dur:%d cpbdur:%d q:%f tex:%d mv:%d misc:%d imb:%d pmb:%d smb:%d d:%c",
779
                   &pict_type, &rce->i_duration, &rce->i_cpb_duration, &qp, &rce->tex_bits,
Loren Merritt's avatar
Loren Merritt committed
780
                   &rce->mv_bits, &rce->misc_bits, &rce->i_count, &rce->p_count,
781
                   &rce->s_count, &rce->direct_mode );
782

783 784 785 786 787 788 789 790 791 792 793 794 795 796
            p = strstr( p, "ref:" );
            if( !p )
                goto parse_error;
            p += 4;
            for( ref = 0; ref < 16; ref++ )
            {
                if( sscanf( p, " %d", &rce->refcount[ref] ) != 1 )
                    break;
                p = strchr( p+1, ' ' );
                if( !p )
                    goto parse_error;
            }
            rce->refs = ref;

Dylan Yudaken's avatar
Dylan Yudaken committed
797 798 799 800
            /* find weights */
            rce->i_weight_denom = -1;
            char *w = strchr( p, 'w' );
            if( w )
801
                if( sscanf( w, "w:%hd,%hd,%hd", &rce->i_weight_denom, &rce->weight[0], &rce->weight[1] ) != 3 )
Dylan Yudaken's avatar
Dylan Yudaken committed
802 803
                    rce->i_weight_denom = -1;

804 805 806
            if( pict_type != 'b' )
                rce->kept_as_ref = 1;
            switch( pict_type )
Fiona Glaser's avatar
Fiona Glaser committed
807
            {
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
                case 'I':
                    rce->frame_type = X264_TYPE_IDR;
                    rce->pict_type  = SLICE_TYPE_I;
                    break;
                case 'i':
                    rce->frame_type = X264_TYPE_I;
                    rce->pict_type  = SLICE_TYPE_I;
                    break;
                case 'P':
                    rce->frame_type = X264_TYPE_P;
                    rce->pict_type  = SLICE_TYPE_P;
                    break;
                case 'B':
                    rce->frame_type = X264_TYPE_BREF;
                    rce->pict_type  = SLICE_TYPE_B;
                    break;
                case 'b':
                    rce->frame_type = X264_TYPE_B;
                    rce->pict_type  = SLICE_TYPE_B;
                    break;
828 829
                default:  e = -1; break;
            }
830
            if( e < 12 )
Fiona Glaser's avatar
Fiona Glaser committed
831
            {
832
parse_error:
833
                x264_log( h, X264_LOG_ERROR, "statistics are damaged at line %d, parser out=%d\n", i, e );
834 835
                return -1;
            }
836
            rce->qscale = qp2qscale( qp );
837 838 839
            p = next;
        }

840
        x264_free( stats_buf );
841

842
        if( h->param.rc.i_rc_method == X264_RC_ABR )
843
        {
844 845
            if( init_pass2( h ) < 0 )
                return -1;
Loren Merritt's avatar
Loren Merritt committed
846
        } /* else we're using constant quant, so no need to run the bitrate allocation */
847 848 849
    }

    /* Open output file */
850 851
    /* If input and output files are the same, output to a temp file
     * and move it to the real name only when it's complete */
852 853
    if( h->param.rc.b_stat_write )
    {
854
        char *p;
Fiona Glaser's avatar
Fiona Glaser committed
855
        rc->psz_stat_file_tmpname = x264_strcat_filename( h->param.rc.psz_stat_out, ".temp" );
856 857
        if( !rc->psz_stat_file_tmpname )
            return -1;
858 859

        rc->p_stat_file_out = fopen( rc->psz_stat_file_tmpname, "wb" );
860 861 862 863 864
        if( rc->p_stat_file_out == NULL )
        {
            x264_log(h, X264_LOG_ERROR, "ratecontrol_init: can't open stats file\n");
            return -1;
        }
865 866

        p = x264_param2string( &h->param, 1 );
867 868
        if( p )
            fprintf( rc->p_stat_file_out, "#options: %s\n", p );
869
        x264_free( p );
Fiona Glaser's avatar
Fiona Glaser committed
870 871 872 873
        if( h->param.rc.b_mb_tree && !h->param.rc.b_stat_read )
        {
            rc->psz_mbtree_stat_file_tmpname = x264_strcat_filename( h->param.rc.psz_stat_out, ".mbtree.temp" );
            rc->psz_mbtree_stat_file_name = x264_strcat_filename( h->param.rc.psz_stat_out, ".mbtree" );
874 875
            if( !rc->psz_mbtree_stat_file_tmpname || !rc->psz_mbtree_stat_file_name )
                return -1;
Fiona Glaser's avatar
Fiona Glaser committed
876 877 878 879 880 881 882 883 884 885 886

            rc->p_mbtree_stat_file_out = fopen( rc->psz_mbtree_stat_file_tmpname, "wb" );
            if( rc->p_mbtree_stat_file_out == NULL )
            {
                x264_log(h, X264_LOG_ERROR, "ratecontrol_init: can't open mbtree stats file\n");
                return -1;
            }
        }
    }

    if( h->param.rc.b_mb_tree && (h->param.rc.b_stat_read || h->param.rc.b_stat_write) )
887 888 889 890 891 892
    {
        CHECKED_MALLOC( rc->qp_buffer[0], h->mb.i_mb_count * sizeof(uint16_t) );
        if( h->param.i_bframe_pyramid && h->param.rc.b_stat_read )
            CHECKED_MALLOC( rc->qp_buffer[1], h->mb.i_mb_count * sizeof(uint16_t) );
        rc->qpbuf_pos = -1;
    }
893

894
    for( int i = 0; i<h->param.i_threads; i++ )
Loren Merritt's avatar
Loren Merritt committed
895 896
    {
        h->thread[i]->rc = rc+i;
897
        if( i )
898
        {
899
            rc[i] = rc[0];
Loren Merritt's avatar
Loren Merritt committed
900
            h->thread[i]->param = h->param;
901
            h->thread[i]->mb.b_variable_qp = h->mb.b_variable_qp;
902
        }
Loren Merritt's avatar
Loren Merritt committed
903 904
    }

905
    return 0;
906 907
fail:
    return -1;
Laurent Aimar's avatar
Laurent Aimar committed
908 909
}

910 911 912
static int parse_zone( x264_t *h, x264_zone_t *z, char *p )
{
    int len = 0;
913
    char *tok, UNUSED *saveptr=NULL;
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
    z->param = NULL;
    z->f_bitrate_factor = 1;
    if( 3 <= sscanf(p, "%u,%u,q=%u%n", &z->i_start, &z->i_end, &z->i_qp, &len) )
        z->b_force_qp = 1;
    else if( 3 <= sscanf(p, "%u,%u,b=%f%n", &z->i_start, &z->i_end, &z->f_bitrate_factor, &len) )
        z->b_force_qp = 0;
    else if( 2 <= sscanf(p, "%u,%u%n", &z->i_start, &z->i_end, &len) )
        z->b_force_qp = 0;
    else
    {
        x264_log( h, X264_LOG_ERROR, "invalid zone: \"%s\"\n", p );
        return -1;
    }
    p += len;
    if( !*p )
        return 0;
930
    CHECKED_MALLOC( z->param, sizeof(x264_param_t) );
931
    memcpy( z->param, &h->param, sizeof(x264_param_t) );
932
    z->param->param_free = x264_free;
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
    while( (tok = strtok_r( p, ",", &saveptr )) )
    {
        char *val = strchr( tok, '=' );
        if( val )
        {
            *val = '\0';
            val++;
        }
        if( x264_param_parse( z->param, tok, val ) )
        {
            x264_log( h, X264_LOG_ERROR, "invalid zone param: %s = %s\n", tok, val );
            return -1;
        }
        p = NULL;
    }
    return 0;
949 950
fail:
    return -1;
951 952
}

953 954 955 956 957
static int parse_zones( x264_t *h )
{
    x264_ratecontrol_t *rc = h->rc;
    if( h->param.rc.psz_zones && !h->param.rc.i_zones )
    {
Steven Walters's avatar
Steven Walters committed
958
        char *psz_zones, *p;
959
        CHECKED_MALLOC( psz_zones, strlen( h->param.rc.psz_zones )+1 );
960
        strcpy( psz_zones, h->param.rc.psz_zones );
961
        h->param.rc.i_zones = 1;
962
        for( p = psz_zones; *p; p++ )
963
            h->param.rc.i_zones += (*p == '/');
964
        CHECKED_MALLOC( h->param.rc.zones, h->param.rc.i_zones * sizeof(x264_zone_t) );
965
        p = psz_zones;
966
        for( int i = 0; i < h->param.rc.i_zones; i++ )
967
        {
Steven Walters's avatar
Steven Walters committed
968 969 970
            int i_tok = strcspn( p, "/" );
            p[i_tok] = 0;
            if( parse_zone( h, &h->param.rc.zones[i], p ) )
971
                return -1;
Steven Walters's avatar
Steven Walters committed
972
            p += i_tok + 1;
973
        }
974
        x264_free( psz_zones );
975 976 977 978
    }

    if( h->param.rc.i_zones > 0 )
    {
979
        for( int i = 0; i < h->param.rc.i_zones; i++ )
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
        {
            x264_zone_t z = h->param.rc.zones[i];
            if( z.i_start < 0 || z.i_start > z.i_end )
            {
                x264_log( h, X264_LOG_ERROR, "invalid zone: start=%d end=%d\n",
                          z.i_start, z.i_end );
                return -1;
            }
            else if( !z.b_force_qp && z.f_bitrate_factor <= 0 )
            {
                x264_log( h, X264_LOG_ERROR, "invalid zone: bitrate_factor=%f\n",
                          z.f_bitrate_factor );
                return -1;
            }
        }

996
        rc->i_zones = h->param.rc.i_zones + 1;
997
        CHECKED_MALLOC( rc->zones, rc->i_zones * sizeof(x264_zone_t) );
998 999 1000 1001 1002 1003 1004
        memcpy( rc->zones+1, h->param.rc.zones, (rc->i_zones-1) * sizeof(x264_zone_t) );

        // default zone to fall back to if none of the others match
        rc->zones[0].i_start = 0;
        rc->zones[0].i_end = INT_MAX;
        rc->zones[0].b_force_qp = 0;
        rc->zones[0].f_bitrate_factor = 1;
1005
        CHECKED_MALLOC( rc->zones[0].param, sizeof(x264_param_t) );
1006
        memcpy( rc->zones[0].param, &h->param, sizeof(x264_param_t) );
1007
        for( int i = 1; i < rc->i_zones; i++ )
1008 1009 1010 1011
        {
            if( !rc->zones[i].param )
                rc->zones[i].param = rc->zones[0].param;
        }
1012 1013 1014
    }

    return 0;
1015 1016
fail:
    return -1;
1017 1018
}

1019
static x264_zone_t *get_zone( x264_t *h, int frame_num )
Loren Merritt's avatar
Loren Merritt committed
1020
{
1021
    for( int i = h->rc->i_zones - 1; i >= 0; i-- )
Loren Merritt's avatar
Loren Merritt committed
1022 1023 1024 1025 1026 1027 1028 1029
    {
        x264_zone_t *z = &h->rc->zones[i];
        if( frame_num >= z->i_start && frame_num <= z->i_end )
            return z;
    }
    return NULL;
}

1030 1031 1032
void x264_ratecontrol_summary( x264_t *h )
{
    x264_ratecontrol_t *rc = h->rc;
1033
    if( rc->b_abr && h->param.rc.i_rc_method == X264_RC_ABR && rc->cbr_decay > .9999 )
1034 1035
    {
        double <