ratecontrol.c 120 KB
Newer Older
1
/*****************************************************************************
Fiona Glaser's avatar
Fiona Glaser committed
2
 * ratecontrol.c: ratecontrol
Laurent Aimar's avatar
Laurent Aimar committed
3
 *****************************************************************************
Henrik Gramner's avatar
Henrik Gramner committed
4
 * Copyright (C) 2005-2017 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.
Fiona Glaser's avatar
Fiona Glaser committed
25
26
27
 *
 * This program is also available under a commercial proprietary license.
 * For more information, contact us at licensing@x264.com.
Laurent Aimar's avatar
Laurent Aimar committed
28
29
 *****************************************************************************/

30
#undef NDEBUG // always check asserts, the speed effect is far too small to disable them
Laurent Aimar's avatar
Laurent Aimar committed
31

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

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

Loren Merritt's avatar
Loren Merritt committed
63
64
typedef struct
{
65
    float coeff_min;
Fiona Glaser's avatar
Fiona Glaser committed
66
67
68
69
    float coeff;
    float count;
    float decay;
    float offset;
Loren Merritt's avatar
Loren Merritt committed
70
71
} predictor_t;

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

Loren Merritt's avatar
Loren Merritt committed
86
87
    /* current frame */
    ratecontrol_entry_t *rce;
88
    float qpm;                  /* qp for current macroblock: precise float for AQ */
89
    float qpa_rc;               /* average of macroblocks' qp before aq */
90
    float qpa_rc_prev;
Fiona Glaser's avatar
Fiona Glaser committed
91
    int   qpa_aq;               /* average of macroblocks' qp after aq */
92
    int   qpa_aq_prev;
Fiona Glaser's avatar
Fiona Glaser committed
93
    float qp_novbv;             /* QP for the current frame if 1-pass VBV was disabled. */
94

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

    /* ABR stuff */
    int    last_satd;
    double last_rceq;
    double cplxr_sum;           /* sum of bits*qscale/rceq */
110
    double expected_bits_sum;   /* sum of qscale2bits after rceq, ratefactor, and overflow, only includes finished frames */
111
    int64_t filler_bits_sum;    /* sum in bits of finished frames' filler data */
Loren Merritt's avatar
Loren Merritt committed
112
113
114
115
    double wanted_bits_window;  /* target bitrate * window */
    double cbr_decay;
    double short_term_cplxsum;
    double short_term_cplxcount;
116
    double rate_factor_constant;
117
118
    double ip_offset;
    double pb_offset;
Loren Merritt's avatar
Loren Merritt committed
119

120
121
    /* 2pass stuff */
    FILE *p_stat_file_out;
122
    char *psz_stat_file_tmpname;
Fiona Glaser's avatar
Fiona Glaser committed
123
124
125
126
    FILE *p_mbtree_stat_file_out;
    char *psz_mbtree_stat_file_tmpname;
    char *psz_mbtree_stat_file_name;
    FILE *p_mbtree_stat_file_in;
127
128

    int num_entries;            /* number of ratecontrol_entry_ts */
Loren Merritt's avatar
Loren Merritt committed
129
    ratecontrol_entry_t *entry; /* FIXME: copy needed data and free this once init is done */
130
    ratecontrol_entry_t **entry_out;
131
    double last_qscale;
132
    double last_qscale_for[3];  /* last qscale for a specific pict type, used for max_diff & ipb factor stuff */
133
    int last_non_b_pict_type;
134
135
    double accum_p_qp;          /* for determining I-frame quant */
    double accum_p_norm;
136
    double last_accum_p_norm;
137
138
    double lmin[3];             /* min qscale by frame type */
    double lmax[3];
Loren Merritt's avatar
Loren Merritt committed
139
    double lstep;               /* max change (multiply) in qscale per frame */
140
141
142
143
    struct
    {
        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.
144
                                 * This value is the current position (0 or 1). */
145
146
147
148
149
150
151
152
153
154
        int src_mb_count;

        /* For rescaling */
        int rescale_enabled;
        float *scale_buffer[2]; /* Intermediate buffers */
        int filtersize[2];      /* filter size (H/V) */
        float *coeffs[2];
        int *pos[2];
        int srcdim[2];          /* Source dimensions (W/H) */
    } mbtree;
Loren Merritt's avatar
Loren Merritt committed
155

156
    /* MBRC stuff */
Fiona Glaser's avatar
Fiona Glaser committed
157
158
    float frame_size_estimated; /* Access to this variable must be atomic: double is
                                 * not atomic on all arches we care about */
Fiona Glaser's avatar
Fiona Glaser committed
159
    double frame_size_maximum;  /* Maximum frame size due to MinCR */
160
    double frame_size_planned;
161
    double slice_size_planned;
162
    predictor_t *row_pred;
163
    predictor_t row_preds[3][2];
Loren Merritt's avatar
Loren Merritt committed
164
    predictor_t *pred_b_from_p; /* predict B-frame size from P-frame satd */
165
166
167
    int bframes;                /* # consecutive B-frames before this P-frame */
    int bframe_bits;            /* total cost of those frames */

Loren Merritt's avatar
Loren Merritt committed
168
169
    int i_zones;
    x264_zone_t *zones;
170
    x264_zone_t *prev_zone;
171
172
173

    /* hrd stuff */
    int initial_cpb_removal_delay;
Kieran Kunhya's avatar
Kieran Kunhya committed
174
    int initial_cpb_removal_delay_offset;
175
176
    double nrt_first_access_unit; /* nominal removal time */
    double previous_cpb_final_arrival_time;
Fiona Glaser's avatar
Fiona Glaser committed
177
    uint64_t hrd_multiply_denom;
178
};
Laurent Aimar's avatar
Laurent Aimar committed
179

180

181
static int parse_zones( x264_t *h );
182
static int init_pass2(x264_t *);
183
static float rate_estimate_qscale( x264_t *h );
184
static int update_vbv( x264_t *h, int bits );
185
static void update_vbv_plan( x264_t *h, int overhead );
Fiona Glaser's avatar
Fiona Glaser committed
186
187
static float predict_size( predictor_t *p, float q, float var );
static void update_predictor( predictor_t *p, float q, float var, float bits );
188

Fiona Glaser's avatar
Fiona Glaser committed
189
190
191
192
193
194
195
196
197
#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;\
    }\
}

198
199
/* Terminology:
 * qp = h.264's quantizer
200
 * qscale = linearized quantizer = Lagrange multiplier
201
 */
Fiona Glaser's avatar
Fiona Glaser committed
202
static inline float qp2qscale( float qp )
203
{
204
    return 0.85f * powf( 2.0f, ( qp - (12.0f + QP_BD_OFFSET) ) / 6.0f );
205
}
Fiona Glaser's avatar
Fiona Glaser committed
206
static inline float qscale2qp( float qscale )
207
{
208
    return (12.0f + QP_BD_OFFSET) + 6.0f * log2f( qscale/0.85f );
209
210
}

211
212
213
214
/* 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. */
215
static inline double qscale2bits( ratecontrol_entry_t *rce, double qscale )
216
{
217
    if( qscale<0.1 )
218
        qscale = 0.1;
219
    return (rce->tex_bits + .1) * pow( rce->qscale / qscale, 1.1 )
220
           + rce->mv_bits * pow( X264_MAX(rce->qscale, 1) / X264_MAX(qscale, 1), 0.5 )
221
           + rce->misc_bits;
222
223
}

Simon Horlick's avatar
Simon Horlick committed
224
static ALWAYS_INLINE uint32_t ac_energy_var( uint64_t sum_ssd, int shift, x264_frame_t *frame, int i, int b_store )
225
226
227
{
    uint32_t sum = sum_ssd;
    uint32_t ssd = sum_ssd >> 32;
Simon Horlick's avatar
Simon Horlick committed
228
229
230
231
232
    if( b_store )
    {
        frame->i_pixel_sum[i] += sum;
        frame->i_pixel_ssd[i] += ssd;
    }
233
234
235
    return ssd - ((uint64_t)sum * sum >> shift);
}

Fiona Glaser's avatar
Fiona Glaser committed
236
static ALWAYS_INLINE uint32_t ac_energy_plane( x264_t *h, int mb_x, int mb_y, x264_frame_t *frame, int i, int b_chroma, int b_field, int b_store )
Fiona Glaser's avatar
Fiona Glaser committed
237
{
238
    int height = b_chroma ? 16>>CHROMA_V_SHIFT : 16;
Fiona Glaser's avatar
Fiona Glaser committed
239
    int stride = frame->i_stride[i];
Fiona Glaser's avatar
Fiona Glaser committed
240
    int offset = b_field
Henrik Gramner's avatar
Henrik Gramner committed
241
242
        ? 16 * mb_x + height * (mb_y&~1) * stride + (mb_y&1) * stride
        : 16 * mb_x + height * mb_y * stride;
Fiona Glaser's avatar
Fiona Glaser committed
243
244
    stride <<= b_field;
    if( b_chroma )
245
    {
246
        ALIGNED_ARRAY_64( pixel, pix,[FENC_STRIDE*16] );
Henrik Gramner's avatar
Henrik Gramner committed
247
        int chromapix = h->luma2chroma_pixel[PIXEL_16x16];
248
        int shift = 7 - CHROMA_V_SHIFT;
Henrik Gramner's avatar
Henrik Gramner committed
249
250
251
252

        h->mc.load_deinterleave_chroma_fenc( pix, frame->plane[1] + offset, stride, height );
        return ac_energy_var( h->pixf.var[chromapix]( pix,               FENC_STRIDE ), shift, frame, 1, b_store )
             + ac_energy_var( h->pixf.var[chromapix]( pix+FENC_STRIDE/2, FENC_STRIDE ), shift, frame, 2, b_store );
253
254
    }
    else
Fiona Glaser's avatar
Fiona Glaser committed
255
        return ac_energy_var( h->pixf.var[PIXEL_16x16]( frame->plane[i] + offset, stride ), 8, frame, i, b_store );
Fiona Glaser's avatar
Fiona Glaser committed
256
257
}

258
// Find the total AC energy of the block in all planes.
259
static NOINLINE uint32_t ac_energy_mb( x264_t *h, int mb_x, int mb_y, x264_frame_t *frame )
260
{
261
262
263
264
    /* 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. */
Simon Horlick's avatar
Simon Horlick committed
265
    uint32_t var;
266
    x264_prefetch_fenc( h, frame, mb_x, mb_y );
Simon Horlick's avatar
Simon Horlick committed
267
268
269
270
271
    if( h->mb.b_adaptive_mbaff )
    {
        /* We don't know the super-MB mode we're going to pick yet, so
         * simply try both and pick the lower of the two. */
        uint32_t var_interlaced, var_progressive;
Fiona Glaser's avatar
Fiona Glaser committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
        var_interlaced   = ac_energy_plane( h, mb_x, mb_y, frame, 0, 0, 1, 1 );
        var_progressive  = ac_energy_plane( h, mb_x, mb_y, frame, 0, 0, 0, 0 );
        if( CHROMA444 )
        {
            var_interlaced  += ac_energy_plane( h, mb_x, mb_y, frame, 1, 0, 1, 1 );
            var_progressive += ac_energy_plane( h, mb_x, mb_y, frame, 1, 0, 0, 0 );
            var_interlaced  += ac_energy_plane( h, mb_x, mb_y, frame, 2, 0, 1, 1 );
            var_progressive += ac_energy_plane( h, mb_x, mb_y, frame, 2, 0, 0, 0 );
        }
        else
        {
            var_interlaced  += ac_energy_plane( h, mb_x, mb_y, frame, 1, 1, 1, 1 );
            var_progressive += ac_energy_plane( h, mb_x, mb_y, frame, 1, 1, 0, 0 );
        }
Simon Horlick's avatar
Simon Horlick committed
286
287
288
289
        var = X264_MIN( var_interlaced, var_progressive );
    }
    else
    {
Fiona Glaser's avatar
Fiona Glaser committed
290
291
292
293
294
295
296
297
        var  = ac_energy_plane( h, mb_x, mb_y, frame, 0, 0, PARAM_INTERLACED, 1 );
        if( CHROMA444 )
        {
            var += ac_energy_plane( h, mb_x, mb_y, frame, 1, 0, PARAM_INTERLACED, 1 );
            var += ac_energy_plane( h, mb_x, mb_y, frame, 2, 0, PARAM_INTERLACED, 1 );
        }
        else
            var += ac_energy_plane( h, mb_x, mb_y, frame, 1, 1, PARAM_INTERLACED, 1 );
Simon Horlick's avatar
Simon Horlick committed
298
    }
Loren Merritt's avatar
Loren Merritt committed
299
    x264_emms();
300
301
302
    return var;
}

303
void x264_adaptive_quant_frame( x264_t *h, x264_frame_t *frame, float *quant_offsets )
304
{
305
306
    /* Initialize frame stats */
    for( int i = 0; i < 3; i++ )
Fiona Glaser's avatar
Fiona Glaser committed
307
    {
308
309
        frame->i_pixel_sum[i] = 0;
        frame->i_pixel_ssd[i] = 0;
Fiona Glaser's avatar
Fiona Glaser committed
310
311
    }

312
313
    /* 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
314
    {
315
        /* Need to init it anyways for MB tree */
Yusuke Nakamura's avatar
Yusuke Nakamura committed
316
        if( h->param.rc.i_aq_mode && h->param.rc.f_aq_strength == 0 )
317
        {
318
319
            if( quant_offsets )
            {
320
                for( int mb_xy = 0; mb_xy < h->mb.i_mb_count; mb_xy++ )
321
322
323
324
325
326
327
328
329
330
331
332
333
                    frame->f_qp_offset[mb_xy] = frame->f_qp_offset_aq[mb_xy] = quant_offsets[mb_xy];
                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] = x264_exp2fix8( frame->f_qp_offset[mb_xy] );
            }
            else
            {
                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;
            }
334
335
        }
        /* Need variance data for weighted prediction */
336
        if( h->param.analyse.i_weighted_pred )
337
        {
338
339
            for( int mb_y = 0; mb_y < h->mb.i_mb_height; mb_y++ )
                for( int mb_x = 0; mb_x < h->mb.i_mb_width; mb_x++ )
340
                    ac_energy_mb( h, mb_x, mb_y, frame );
341
342
343
        }
        else
            return;
Anton Mitrofanov's avatar
Anton Mitrofanov committed
344
    }
345
    /* Actual adaptive quantization */
Anton Mitrofanov's avatar
Anton Mitrofanov committed
346
    else
347
    {
348
349
350
351
352
353
354
        /* 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. */
        float strength;
        float avg_adj = 0.f;
        float bias_strength = 0.f;

        if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE || h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE_BIASED )
355
        {
356
            float bit_depth_correction = 1.f / (1 << (2*(BIT_DEPTH-8)));
357
            float avg_adj_pow2 = 0.f;
358
359
            for( int mb_y = 0; mb_y < h->mb.i_mb_height; mb_y++ )
                for( int mb_x = 0; mb_x < h->mb.i_mb_width; mb_x++ )
360
                {
361
                    uint32_t energy = ac_energy_mb( h, mb_x, mb_y, frame );
362
                    float qp_adj = powf( energy * bit_depth_correction + 1, 0.125f );
363
364
365
366
367
368
                    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;
369
370
371
            strength = h->param.rc.f_aq_strength * avg_adj;
            avg_adj = avg_adj - 0.5f * (avg_adj_pow2 - 14.f) / avg_adj;
            bias_strength = h->param.rc.f_aq_strength;
372
373
374
375
        }
        else
            strength = h->param.rc.f_aq_strength * 1.0397f;

376
377
        for( int mb_y = 0; mb_y < h->mb.i_mb_height; mb_y++ )
            for( int mb_x = 0; mb_x < h->mb.i_mb_width; mb_x++ )
Anton Mitrofanov's avatar
Anton Mitrofanov committed
378
            {
379
                float qp_adj;
380
                int mb_xy = mb_x + mb_y*h->mb.i_mb_stride;
381
382
383
384
385
386
                if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE_BIASED )
                {
                    qp_adj = frame->f_qp_offset[mb_xy];
                    qp_adj = strength * (qp_adj - avg_adj) + bias_strength * (1.f - 14.f / (qp_adj * qp_adj));
                }
                else if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE )
387
                {
388
                    qp_adj = frame->f_qp_offset[mb_xy];
389
390
391
392
                    qp_adj = strength * (qp_adj - avg_adj);
                }
                else
                {
393
                    uint32_t energy = ac_energy_mb( h, mb_x, mb_y, frame );
394
                    qp_adj = strength * (x264_log2( X264_MAX(energy, 1) ) - (14.427f + 2*(BIT_DEPTH-8)));
395
                }
396
397
398
399
                if( quant_offsets )
                    qp_adj += quant_offsets[mb_xy];
                frame->f_qp_offset[mb_xy] =
                frame->f_qp_offset_aq[mb_xy] = qp_adj;
400
                if( h->frames.b_have_lowres )
401
                    frame->i_inv_qscale_factor[mb_xy] = x264_exp2fix8(qp_adj);
Anton Mitrofanov's avatar
Anton Mitrofanov committed
402
            }
403
404
405
406
407
408
409
    }

    /* 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];
410
411
        int width  = 16*h->mb.i_mb_width  >> (i && CHROMA_H_SHIFT);
        int height = 16*h->mb.i_mb_height >> (i && CHROMA_V_SHIFT);
412
        frame->i_pixel_ssd[i] = ssd - (sum * sum + width * height / 2) / (width * height);
413
    }
414
415
}

416
static int macroblock_tree_rescale_init( x264_t *h, x264_ratecontrol_t *rc )
417
418
419
420
421
422
{
    /* Use fractional QP array dimensions to compensate for edge padding */
    float srcdim[2] = {rc->mbtree.srcdim[0] / 16.f, rc->mbtree.srcdim[1] / 16.f};
    float dstdim[2] = {    h->param.i_width / 16.f,    h->param.i_height / 16.f};
    int srcdimi[2] = {ceil(srcdim[0]), ceil(srcdim[1])};
    int dstdimi[2] = {ceil(dstdim[0]), ceil(dstdim[1])};
Anton Mitrofanov's avatar
Anton Mitrofanov committed
423
    if( h->param.b_interlaced || h->param.b_fake_interlaced )
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
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
    {
        srcdimi[1] = (srcdimi[1]+1)&~1;
        dstdimi[1] = (dstdimi[1]+1)&~1;
    }

    rc->mbtree.src_mb_count = srcdimi[0] * srcdimi[1];

    CHECKED_MALLOC( rc->mbtree.qp_buffer[0], rc->mbtree.src_mb_count * sizeof(uint16_t) );
    if( h->param.i_bframe_pyramid && h->param.rc.b_stat_read )
        CHECKED_MALLOC( rc->mbtree.qp_buffer[1], rc->mbtree.src_mb_count * sizeof(uint16_t) );
    rc->mbtree.qpbuf_pos = -1;

    /* No rescaling to do */
    if( srcdimi[0] == dstdimi[0] && srcdimi[1] == dstdimi[1] )
        return 0;

    rc->mbtree.rescale_enabled = 1;

    /* Allocate intermediate scaling buffers */
    CHECKED_MALLOC( rc->mbtree.scale_buffer[0], srcdimi[0] * srcdimi[1] * sizeof(float) );
    CHECKED_MALLOC( rc->mbtree.scale_buffer[1], dstdimi[0] * srcdimi[1] * sizeof(float) );

    /* Allocate and calculate resize filter parameters and coefficients */
    for( int i = 0; i < 2; i++ )
    {
        if( srcdim[i] > dstdim[i] ) // downscale
            rc->mbtree.filtersize[i] = 1 + (2 * srcdimi[i] + dstdimi[i] - 1) / dstdimi[i];
        else                        // upscale
            rc->mbtree.filtersize[i] = 3;

        CHECKED_MALLOC( rc->mbtree.coeffs[i], rc->mbtree.filtersize[i] * dstdimi[i] * sizeof(float) );
        CHECKED_MALLOC( rc->mbtree.pos[i], dstdimi[i] * sizeof(int) );

        /* Initialize filter coefficients */
        float inc = srcdim[i] / dstdim[i];
        float dmul = inc > 1.f ? dstdim[i] / srcdim[i] : 1.f;
        float dstinsrc = 0.5f * inc - 0.5f;
        int filtersize = rc->mbtree.filtersize[i];
        for( int j = 0; j < dstdimi[i]; j++ )
        {
            int pos = dstinsrc - (filtersize - 2.f) * 0.5f;
            float sum = 0.0;
            rc->mbtree.pos[i][j] = pos;
            for( int k = 0; k < filtersize; k++ )
            {
                float d = fabs( pos + k - dstinsrc ) * dmul;
                float coeff = X264_MAX( 1.f - d, 0 );
                rc->mbtree.coeffs[i][j * filtersize + k] = coeff;
                sum += coeff;
            }
            sum = 1.0f / sum;
            for( int k = 0; k < filtersize; k++ )
                rc->mbtree.coeffs[i][j * filtersize + k] *= sum;
            dstinsrc += inc;
        }
    }

    /* Write back actual qp array dimensions */
    rc->mbtree.srcdim[0] = srcdimi[0];
    rc->mbtree.srcdim[1] = srcdimi[1];
    return 0;
fail:
    return -1;
}

489
static void macroblock_tree_rescale_destroy( x264_ratecontrol_t *rc )
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
{
    for( int i = 0; i < 2; i++ )
    {
        x264_free( rc->mbtree.qp_buffer[i] );
        x264_free( rc->mbtree.scale_buffer[i] );
        x264_free( rc->mbtree.coeffs[i] );
        x264_free( rc->mbtree.pos[i] );
    }
}

static ALWAYS_INLINE float tapfilter( float *src, int pos, int max, int stride, float *coeff, int filtersize )
{
    float sum = 0.f;
    for( int i = 0; i < filtersize; i++, pos++ )
        sum += src[x264_clip3( pos, 0, max-1 )*stride] * coeff[i];
    return sum;
}

508
static void macroblock_tree_rescale( x264_t *h, x264_ratecontrol_t *rc, float *dst )
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
{
    float *input, *output;
    int filtersize, stride, height;

    /* H scale first */
    input = rc->mbtree.scale_buffer[0];
    output = rc->mbtree.scale_buffer[1];
    filtersize = rc->mbtree.filtersize[0];
    stride = rc->mbtree.srcdim[0];
    height = rc->mbtree.srcdim[1];
    for( int y = 0; y < height; y++, input += stride, output += h->mb.i_mb_width )
    {
        float *coeff = rc->mbtree.coeffs[0];
        for( int x = 0; x < h->mb.i_mb_width; x++, coeff+=filtersize )
            output[x] = tapfilter( input, rc->mbtree.pos[0][x], stride, 1, coeff, filtersize );
    }

    /* V scale next */
    input = rc->mbtree.scale_buffer[1];
    output = dst;
    filtersize = rc->mbtree.filtersize[1];
    stride = h->mb.i_mb_width;
    height = rc->mbtree.srcdim[1];
    for( int x = 0; x < h->mb.i_mb_width; x++, input++, output++ )
    {
        float *coeff = rc->mbtree.coeffs[1];
        for( int y = 0; y < h->mb.i_mb_height; y++, coeff+=filtersize )
            output[y*stride] = tapfilter( input, rc->mbtree.pos[1][y], height, stride, coeff, filtersize );
    }
}

540
int x264_macroblock_tree_read( x264_t *h, x264_frame_t *frame, float *quant_offsets )
Fiona Glaser's avatar
Fiona Glaser committed
541
542
543
544
{
    x264_ratecontrol_t *rc = h->rc;
    uint8_t i_type_actual = rc->entry[frame->i_frame].pict_type;

545
    if( rc->entry[frame->i_frame].kept_as_ref )
Fiona Glaser's avatar
Fiona Glaser committed
546
547
    {
        uint8_t i_type;
548
        if( rc->mbtree.qpbuf_pos < 0 )
549
550
551
        {
            do
            {
552
                rc->mbtree.qpbuf_pos++;
Fiona Glaser's avatar
Fiona Glaser committed
553

554
555
                if( !fread( &i_type, 1, 1, rc->p_mbtree_stat_file_in ) )
                    goto fail;
556
                if( fread( rc->mbtree.qp_buffer[rc->mbtree.qpbuf_pos], sizeof(uint16_t), rc->mbtree.src_mb_count, rc->p_mbtree_stat_file_in ) != rc->mbtree.src_mb_count )
557
                    goto fail;
Fiona Glaser's avatar
Fiona Glaser committed
558

559
                if( i_type != i_type_actual && rc->mbtree.qpbuf_pos == 1 )
560
                {
561
                    x264_log( h, X264_LOG_ERROR, "MB-tree frametype %d doesn't match actual frametype %d.\n", i_type, i_type_actual );
562
563
564
                    return -1;
                }
            } while( i_type != i_type_actual );
Fiona Glaser's avatar
Fiona Glaser committed
565
566
        }

567
        float *dst = rc->mbtree.rescale_enabled ? rc->mbtree.scale_buffer[0] : frame->f_qp_offset;
568
        h->mc.mbtree_fix8_unpack( dst, rc->mbtree.qp_buffer[rc->mbtree.qpbuf_pos], rc->mbtree.src_mb_count );
569
        if( rc->mbtree.rescale_enabled )
570
            macroblock_tree_rescale( h, rc, frame->f_qp_offset );
571
572
573
574
        if( h->frames.b_have_lowres )
            for( int i = 0; i < h->mb.i_mb_count; i++ )
                frame->i_inv_qscale_factor[i] = x264_exp2fix8( frame->f_qp_offset[i] );
        rc->mbtree.qpbuf_pos--;
Fiona Glaser's avatar
Fiona Glaser committed
575
576
    }
    else
577
        x264_stack_align( x264_adaptive_quant_frame, h, frame, quant_offsets );
Fiona Glaser's avatar
Fiona Glaser committed
578
579
    return 0;
fail:
580
    x264_log( h, X264_LOG_ERROR, "Incomplete MB-tree stats file.\n" );
Fiona Glaser's avatar
Fiona Glaser committed
581
582
583
    return -1;
}

584
585
586
587
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
588
589
    x264_weight_t weights[16][3];
    int refcount[16];
590

591
    if( rce->refs != h->i_ref[0] )
592
593
        return -1;

594
    memcpy( frames, h->fref[0], sizeof(frames) );
Dylan Yudaken's avatar
Dylan Yudaken committed
595
596
    memcpy( refcount, rce->refcount, sizeof(refcount) );
    memcpy( weights, h->fenc->weight, sizeof(weights) );
Dylan Yudaken's avatar
Dylan Yudaken committed
597
    memset( &h->fenc->weight[1][0], 0, sizeof(x264_weight_t[15][3]) );
598
599
600

    /* For now don't reorder ref 0; it seems to lower quality
       in most cases due to skips. */
601
    for( int ref = 1; ref < h->i_ref[0]; ref++ )
602
603
604
    {
        int max = -1;
        int bestref = 1;
Dylan Yudaken's avatar
Dylan Yudaken committed
605

606
        for( int i = 1; i < h->i_ref[0]; i++ )
607
608
            /* Favor lower POC as a tiebreaker. */
            COPY2_IF_GT( max, refcount[i], bestref, i );
Dylan Yudaken's avatar
Dylan Yudaken committed
609
610
611
612
613

        /* 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;
614
        h->fref[0][ref] = frames[bestref];
Dylan Yudaken's avatar
Dylan Yudaken committed
615
        memcpy( h->fenc->weight[ref], weights[bestref], sizeof(weights[bestref]) );
616
617
618
619
620
    }

    return 0;
}

621
static char *strcat_filename( char *input, char *suffix )
Fiona Glaser's avatar
Fiona Glaser committed
622
623
{
    char *output = x264_malloc( strlen( input ) + strlen( suffix ) + 1 );
624
625
    if( !output )
        return NULL;
Fiona Glaser's avatar
Fiona Glaser committed
626
627
628
629
630
    strcpy( output, input );
    strcat( output, suffix );
    return output;
}

631
632
633
634
635
636
void x264_ratecontrol_init_reconfigurable( x264_t *h, int b_init )
{
    x264_ratecontrol_t *rc = h->rc;
    if( !b_init && rc->b_2pass )
        return;

637
638
639
640
641
642
643
    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 )
Fiona Glaser's avatar
Fiona Glaser committed
644
                                 / qp2qscale( h->param.rc.f_rf_constant + mbtree_offset + QP_BD_OFFSET );
645
646
    }

647
648
    if( h->param.rc.i_vbv_max_bitrate > 0 && h->param.rc.i_vbv_buffer_size > 0 )
    {
649
650
651
652
653
        /* 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;

654
655
656
657
658
659
660
        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 );
        }

661
        int kilobit_size = h->param.i_avcintra_class ? 1024 : 1000;
Kieran Kunhya's avatar
Kieran Kunhya committed
662
663
        int vbv_buffer_size = h->param.rc.i_vbv_buffer_size * kilobit_size;
        int vbv_max_bitrate = h->param.rc.i_vbv_max_bitrate * kilobit_size;
664
665
666
667
668
669
670
671
672
673
674
675

        /* 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

            // normalize HRD size and rate to the value / scale notation
Kieran Kunhya's avatar
Kieran Kunhya committed
676
677
            h->sps->vui.hrd.i_bit_rate_scale = x264_clip3( x264_ctz( vbv_max_bitrate ) - BR_SHIFT, 0, 15 );
            h->sps->vui.hrd.i_bit_rate_value = vbv_max_bitrate >> ( h->sps->vui.hrd.i_bit_rate_scale + BR_SHIFT );
678
            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 );
Kieran Kunhya's avatar
Kieran Kunhya committed
679
680
            h->sps->vui.hrd.i_cpb_size_scale = x264_clip3( x264_ctz( vbv_buffer_size ) - CPB_SHIFT, 0, 15 );
            h->sps->vui.hrd.i_cpb_size_value = vbv_buffer_size >> ( h->sps->vui.hrd.i_cpb_size_scale + CPB_SHIFT );
681
682
683
684
685
686
687
688
            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

689
            int max_cpb_output_delay = X264_MIN( h->param.i_keyint_max * MAX_DURATION * h->sps->vui.i_time_scale / h->sps->vui.i_num_units_in_tick, INT_MAX );
690
691
692
693
            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 );
694
695
            h->sps->vui.hrd.i_cpb_removal_delay_length = x264_clip3( 32 - x264_clz( max_cpb_output_delay ), 4, 31 );
            h->sps->vui.hrd.i_dpb_output_delay_length  = x264_clip3( 32 - x264_clz( max_dpb_output_delay ), 4, 31 );
696
697
698

            #undef MAX_DURATION

Fiona Glaser's avatar
Fiona Glaser committed
699
700
            vbv_buffer_size = h->sps->vui.hrd.i_cpb_size_unscaled;
            vbv_max_bitrate = h->sps->vui.hrd.i_bit_rate_unscaled;
701
702
703
704
705
706
        }
        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;
        }
707
708
        h->sps->vui.hrd.i_bit_rate_unscaled = vbv_max_bitrate;
        h->sps->vui.hrd.i_cpb_size_unscaled = vbv_buffer_size;
709

710
        if( rc->b_vbv_min_rate )
Kieran Kunhya's avatar
Kieran Kunhya committed
711
            rc->bitrate = (double)h->param.rc.i_bitrate * kilobit_size;
712
713
714
        rc->buffer_rate = vbv_max_bitrate / rc->fps;
        rc->vbv_max_rate = vbv_max_bitrate;
        rc->buffer_size = vbv_buffer_size;
715
716
717
        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
718
719
720
721
722
723
724
725
726
        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;
            }
        }
727
728
729
730
        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
731
            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);
Anton Mitrofanov's avatar
Anton Mitrofanov committed
732
733
            rc->buffer_fill_final =
            rc->buffer_fill_final_min = rc->buffer_size * h->param.rc.f_vbv_buffer_init * h->sps->vui.i_time_scale;
734
735
736
737
738
739
740
741
            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;
        }
    }
}

742
int x264_ratecontrol_new( x264_t *h )
Laurent Aimar's avatar
Laurent Aimar committed
743
{
744
    x264_ratecontrol_t *rc;
745

Loren Merritt's avatar
Loren Merritt committed
746
    x264_emms();
747

748
749
    CHECKED_MALLOCZERO( h->rc, h->param.i_threads * sizeof(x264_ratecontrol_t) );
    rc = h->rc;
750

751
752
    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
753

Måns Rullgård's avatar
Måns Rullgård committed
754
    /* FIXME: use integers */
755
    if( h->param.i_fps_num > 0 && h->param.i_fps_den > 0 )
Måns Rullgård's avatar
Måns Rullgård committed
756
757
758
759
        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
760
761
762
763
764
765
766
767
    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;

768
    rc->bitrate = h->param.rc.i_bitrate * (h->param.i_avcintra_class ? 1024. : 1000.);
Loren Merritt's avatar
Loren Merritt committed
769
    rc->rate_tolerance = h->param.rc.f_rate_tolerance;
770
    rc->nmb = h->mb.i_mb_count;
Loren Merritt's avatar
Loren Merritt committed
771
772
773
    rc->last_non_b_pict_type = -1;
    rc->cbr_decay = 1.0;

774
    if( h->param.rc.i_rc_method == X264_RC_CRF && h->param.rc.b_stat_read )
775
    {
776
        x264_log( h, X264_LOG_ERROR, "constant rate-factor is incompatible with 2pass.\n" );
777
778
        return -1;
    }
779
780
781

    x264_ratecontrol_init_reconfigurable( h, 1 );

Fiona Glaser's avatar
Fiona Glaser committed
782
783
784
    if( h->param.i_nal_hrd )
    {
        uint64_t denom = (uint64_t)h->sps->vui.hrd.i_bit_rate_unscaled * h->sps->vui.i_time_scale;
Anton Mitrofanov's avatar
Anton Mitrofanov committed
785
        uint64_t num = 90000;
Fiona Glaser's avatar
Fiona Glaser committed
786
        x264_reduce_fraction64( &num, &denom );
Anton Mitrofanov's avatar
Anton Mitrofanov committed
787
        rc->hrd_multiply_denom = 90000 / num;
Fiona Glaser's avatar
Fiona Glaser committed
788

789
        double bits_required = log2( num )
Fiona Glaser's avatar
Fiona Glaser committed
790
791
792
793
794
795
796
797
798
                             + log2( h->sps->vui.i_time_scale )
                             + log2( h->sps->vui.hrd.i_cpb_size_unscaled );
        if( bits_required >= 63 )
        {
            x264_log( h, X264_LOG_ERROR, "HRD with very large timescale and bufsize not supported\n" );
            return -1;
        }
    }

799
    if( rc->rate_tolerance < 0.01 )
Fiona Glaser's avatar
Fiona Glaser committed
800
    {
801
        x264_log( h, X264_LOG_WARNING, "bitrate tolerance too small, using .01\n" );
802
803
        rc->rate_tolerance = 0.01;
    }
804

805
    h->mb.b_variable_qp = rc->b_vbv || h->param.rc.i_aq_mode;
806

Loren Merritt's avatar
Loren Merritt committed
807
808
    if( rc->b_abr )
    {
809
        /* FIXME ABR_INIT_QP is actually used only in CRF */
Fiona Glaser's avatar
Fiona Glaser committed
810
#define ABR_INIT_QP (( h->param.rc.i_rc_method == X264_RC_CRF ? h->param.rc.f_rf_constant : 24 ) + QP_BD_OFFSET)
Loren Merritt's avatar
Loren Merritt committed
811
812
        rc->accum_p_norm = .01;
        rc->accum_p_qp = ABR_INIT_QP * rc->accum_p_norm;
813
        /* estimated ratio that produces a reasonable QP for the first I-frame */
Fiona Glaser's avatar
Fiona Glaser committed
814
        rc->cplxr_sum = .01 * pow( 7.0e5, rc->qcompress ) * pow( h->mb.i_mb_count, 0.5 );
815
816
        rc->wanted_bits_window = 1.0 * rc->bitrate / rc->fps;
        rc->last_non_b_pict_type = SLICE_TYPE_I;
817
818
    }

819
820
    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
821
    rc->qp_constant[SLICE_TYPE_P] = h->param.rc.i_qp_constant;
822
823
    rc->qp_constant[SLICE_TYPE_I] = x264_clip3( h->param.rc.i_qp_constant - rc->ip_offset + 0.5, 0, QP_MAX );
    rc->qp_constant[SLICE_TYPE_B] = x264_clip3( h->param.rc.i_qp_constant + rc->pb_offset + 0.5, 0, QP_MAX );
Fiona Glaser's avatar
Fiona Glaser committed
824
    h->mb.ip_offset = rc->ip_offset + 0.5;
825

Loren Merritt's avatar
Loren Merritt committed
826
    rc->lstep = pow( 2, h->param.rc.i_qp_step / 6.0 );
827
    rc->last_qscale = qp2qscale( 26 + QP_BD_OFFSET );
Fiona Glaser's avatar
Fiona Glaser committed
828
829
    int num_preds = h->param.b_sliced_threads * h->param.i_threads + 1;
    CHECKED_MALLOC( rc->pred, 5 * sizeof(predictor_t) * num_preds );
830
    CHECKED_MALLOC( rc->pred_b_from_p, sizeof(predictor_t) );
831
    static const float pred_coeff_table[3] = { 1.0, 1.0, 1.5 };
832
    for( int i = 0; i < 3; i++ )
833
    {
834
        rc->last_qscale_for[i] = qp2qscale( ABR_INIT_QP );
835
836
        rc->lmin[i] = qp2qscale( h->param.rc.i_qp_min );
        rc->lmax[i] = qp2qscale( h->param.rc.i_qp_max );
837
        for( int j = 0; j < num_preds; j++ )
Fiona Glaser's avatar
Fiona Glaser committed
838
        {
839
840
            rc->pred[i+j*5].coeff_min = pred_coeff_table[i] / 2;
            rc->pred[i+j*5].coeff = pred_coeff_table[i];
841
842
843
            rc->pred[i+j*5].count = 1.0;
            rc->pred[i+j*5].decay = 0.5;
            rc->pred[i+j*5].offset = 0.0;
Fiona Glaser's avatar
Fiona Glaser committed
844
        }
845
        for( int j = 0; j < 2; j++ )
846
        {
847
848
849
850
851
            rc->row_preds[i][j].coeff_min = .25 / 4;
            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;
852
        }
853
    }
854
855
856
857
858
    rc->pred_b_from_p->coeff_min = 0.5 / 2;
    rc->pred_b_from_p->coeff = 0.5;
    rc->pred_b_from_p->count = 1.0;
    rc->pred_b_from_p->decay = 0.5;
    rc->pred_b_from_p->offset = 0.0;
859

860
    if( parse_zones( h ) < 0 )
861
862
    {
        x264_log( h, X264_LOG_ERROR, "failed to parse zones\n" );
863
        return -1;
864
    }
Loren Merritt's avatar
Loren Merritt committed
865

866
867
868
    /* Load stat file and init 2pass algo */
    if( h->param.rc.b_stat_read )
    {
869
        char *p, *stats_in, *stats_buf;
870
871
872

        /* read 1st pass stats */
        assert( h->param.rc.psz_stat_in );
873
874
        stats_buf = stats_in = x264_slurp_file( h->param.rc.psz_stat_in );
        if( !stats_buf )
875
        {
876
            x264_log( h, X264_LOG_ERROR, "ratecontrol_init: can't open stats file\n" );
877
878
            return -1;
        }
Fiona Glaser's avatar
Fiona Glaser committed
879
880
        if( h->param.rc.b_mb_tree )
        {
881
            char *mbtree_stats_in = strcat_filename( h->param.rc.psz_stat_in, ".mbtree" );
882
883
            if( !mbtree_stats_in )
                return -1;
Henrik Gramner's avatar
Henrik Gramner committed
884
            rc->p_mbtree_stat_file_in = x264_fopen( mbtree_stats_in, "rb" );
Fiona Glaser's avatar
Fiona Glaser committed
885
886
887
            x264_free( mbtree_stats_in );
            if( !rc->p_mbtree_stat_file_in )
            {
888
                x264_log( h, X264_LOG_ERROR, "ratecontrol_init: can't open mbtree stats file\n" );
Fiona Glaser's avatar
Fiona Glaser committed
889
890
891
                return -1;
            }
        }
892

893
        /* check whether 1st pass options were compatible with current options */
894
895
896
897
898
899
900
        if( strncmp( stats_buf, "#options:", 9 ) )
        {
            x264_log( h, X264_LOG_ERROR, "options list in stats file not valid\n" );
            return -1;
        }

        float res_factor, res_factor_bits;
901
        {
902
            int i, j;
903
            uint32_t k, l;
904
905
906
907
908
909
            char *opts = stats_buf;
            stats_in = strchr( stats_buf, '\n' );
            if( !stats_in )
                return -1;
            *stats_in = '\0';
            stats_in++;
910
911
912
913
914
            if( sscanf( opts, "#options: %dx%d", &i, &j ) != 2 )
            {