encoder.c 144 KB
Newer Older
Laurent Aimar's avatar
Laurent Aimar committed
1
/*****************************************************************************
Fiona Glaser's avatar
Fiona Glaser committed
2
 * encoder.c: top-level encoder functions
Laurent Aimar's avatar
Laurent Aimar committed
3
 *****************************************************************************
Sean McGovern's avatar
Sean McGovern committed
4
 * Copyright (C) 2003-2011 x264 project
Laurent Aimar's avatar
Laurent Aimar committed
5
6
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *          Loren Merritt <lorenm@u.washington.edu>
8
 *          Fiona Glaser <fiona@x264.com>
Laurent Aimar's avatar
Laurent Aimar committed
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * 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
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
Fiona Glaser's avatar
Fiona Glaser committed
23
24
25
 *
 * 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
26
27
 *****************************************************************************/

28
#include "common/common.h"
Laurent Aimar's avatar
Laurent Aimar committed
29
30
31
32
33

#include "set.h"
#include "analyse.h"
#include "ratecontrol.h"
#include "macroblock.h"
34
#include "me.h"
Laurent Aimar's avatar
Laurent Aimar committed
35

Steven Walters's avatar
Steven Walters committed
36
#if HAVE_VISUALIZE
Loren Merritt's avatar
Loren Merritt committed
37
38
39
#include "common/visualize.h"
#endif

Laurent Aimar's avatar
Laurent Aimar committed
40
41
//#define DEBUG_MB_TYPE

Fiona Glaser's avatar
Fiona Glaser committed
42
43
#define bs_write_ue bs_write_ue_big

Fiona Glaser's avatar
Fiona Glaser committed
44
static int x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
45
46
                                   x264_nal_t **pp_nal, int *pi_nal,
                                   x264_picture_t *pic_out );
Loren Merritt's avatar
Loren Merritt committed
47

Laurent Aimar's avatar
Laurent Aimar committed
48
49
50
51
52
/****************************************************************************
 *
 ******************************* x264 libs **********************************
 *
 ****************************************************************************/
Fiona Glaser's avatar
Fiona Glaser committed
53
static double x264_psnr( double sqe, double size )
54
{
Fiona Glaser's avatar
Fiona Glaser committed
55
56
    double mse = sqe / (PIXEL_MAX*PIXEL_MAX * size);
    if( mse <= 0.0000000001 ) /* Max 100dB */
57
58
        return 100;

Fiona Glaser's avatar
Fiona Glaser committed
59
    return -10.0 * log10( mse );
60
61
}

Fiona Glaser's avatar
Fiona Glaser committed
62
static double x264_ssim( double ssim )
63
{
Anton Mitrofanov's avatar
Anton Mitrofanov committed
64
65
66
67
68
    double inv_ssim = 1 - ssim;
    if( inv_ssim <= 0.0000000001 ) /* Max 100dB */
        return 100;

    return -10.0 * log10( inv_ssim );
69
70
}

71
static void x264_frame_dump( x264_t *h )
Laurent Aimar's avatar
Laurent Aimar committed
72
{
73
    FILE *f = fopen( h->param.psz_dump_yuv, "r+b" );
74
75
    if( !f )
        return;
Fiona Glaser's avatar
Fiona Glaser committed
76

77
    /* Write the frame in display order */
Henrik Gramner's avatar
Henrik Gramner committed
78
    int frame_size = FRAME_SIZE( h->param.i_height * h->param.i_width * sizeof(pixel) );
Fiona Glaser's avatar
Fiona Glaser committed
79
80
81
82
83
84
85
    fseek( f, (uint64_t)h->fdec->i_frame * frame_size, SEEK_SET );
    for( int p = 0; p < (CHROMA444 ? 3 : 1); p++ )
        for( int y = 0; y < h->param.i_height; y++ )
            fwrite( &h->fdec->plane[p][y*h->fdec->i_stride[p]], sizeof(pixel), h->param.i_width, f );
    if( !CHROMA444 )
    {
        int cw = h->param.i_width>>1;
86
        int ch = h->param.i_height>>CHROMA_V_SHIFT;
Fiona Glaser's avatar
Fiona Glaser committed
87
88
89
90
91
92
93
        pixel *planeu = x264_malloc( (cw*ch*2+32)*sizeof(pixel) );
        pixel *planev = planeu + cw*ch + 16;
        h->mc.plane_copy_deinterleave( planeu, cw, planev, cw, h->fdec->plane[1], h->fdec->i_stride[1], cw, ch );
        fwrite( planeu, 1, cw*ch*sizeof(pixel), f );
        fwrite( planev, 1, cw*ch*sizeof(pixel), f );
        x264_free( planeu );
    }
Laurent Aimar's avatar
Laurent Aimar committed
94
95
96
97
    fclose( f );
}

/* Fill "default" values */
98
static void x264_slice_header_init( x264_t *h, x264_slice_header_t *sh,
Laurent Aimar's avatar
Laurent Aimar committed
99
                                    x264_sps_t *sps, x264_pps_t *pps,
Loren Merritt's avatar
Loren Merritt committed
100
                                    int i_idr_pic_id, int i_frame, int i_qp )
Laurent Aimar's avatar
Laurent Aimar committed
101
{
102
103
    x264_param_t *param = &h->param;

104
    /* First we fill all fields */
Laurent Aimar's avatar
Laurent Aimar committed
105
106
107
108
    sh->sps = sps;
    sh->pps = pps;

    sh->i_first_mb  = 0;
Fiona Glaser's avatar
Fiona Glaser committed
109
    sh->i_last_mb   = h->mb.i_mb_count - 1;
Laurent Aimar's avatar
Laurent Aimar committed
110
111
112
113
    sh->i_pps_id    = pps->i_id;

    sh->i_frame_num = i_frame;

114
    sh->b_mbaff = PARAM_INTERLACED;
115
116
    sh->b_field_pic = 0;    /* no field support for now */
    sh->b_bottom_field = 0; /* not yet used */
Laurent Aimar's avatar
Laurent Aimar committed
117
118
119
120

    sh->i_idr_pic_id = i_idr_pic_id;

    /* poc stuff, fixed later */
121
    sh->i_poc = 0;
Laurent Aimar's avatar
Laurent Aimar committed
122
123
124
125
126
127
    sh->i_delta_poc_bottom = 0;
    sh->i_delta_poc[0] = 0;
    sh->i_delta_poc[1] = 0;

    sh->i_redundant_pic_cnt = 0;

128
129
130
131
132
    h->mb.b_direct_auto_write = h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_AUTO
                                && h->param.i_bframe
                                && ( h->param.rc.b_stat_write || !h->param.rc.b_stat_read );

    if( !h->mb.b_direct_auto_read && sh->i_type == SLICE_TYPE_B )
Loren Merritt's avatar
Loren Merritt committed
133
    {
134
        if( h->fref[1][0]->i_poc_l0ref0 == h->fref[0][0]->i_poc )
135
136
137
138
139
140
        {
            if( h->mb.b_direct_auto_write )
                sh->b_direct_spatial_mv_pred = ( h->stat.i_direct_score[1] > h->stat.i_direct_score[0] );
            else
                sh->b_direct_spatial_mv_pred = ( param->analyse.i_direct_mv_pred == X264_DIRECT_PRED_SPATIAL );
        }
Loren Merritt's avatar
Loren Merritt committed
141
        else
142
143
144
145
        {
            h->mb.b_direct_auto_write = 0;
            sh->b_direct_spatial_mv_pred = 1;
        }
Loren Merritt's avatar
Loren Merritt committed
146
147
    }
    /* else b_direct_spatial_mv_pred was read from the 2pass statsfile */
Laurent Aimar's avatar
Laurent Aimar committed
148
149
150
151
152

    sh->b_num_ref_idx_override = 0;
    sh->i_num_ref_idx_l0_active = 1;
    sh->i_num_ref_idx_l1_active = 1;

153
154
    sh->b_ref_pic_list_reordering[0] = h->b_ref_reorder[0];
    sh->b_ref_pic_list_reordering[1] = h->b_ref_reorder[1];
155
156

    /* If the ref list isn't in the default order, construct reordering header */
157
    for( int list = 0; list < 2; list++ )
158
    {
159
        if( sh->b_ref_pic_list_reordering[list] )
160
        {
161
162
163
164
165
166
167
168
            int pred_frame_num = i_frame;
            for( int i = 0; i < h->i_ref[list]; i++ )
            {
                int diff = h->fref[list][i]->i_frame_num - pred_frame_num;
                sh->ref_pic_list_order[list][i].idc = ( diff > 0 );
                sh->ref_pic_list_order[list][i].arg = (abs(diff) - 1) & ((1 << sps->i_log2_max_frame_num) - 1);
                pred_frame_num = h->fref[list][i]->i_frame_num;
            }
169
170
171
        }
    }

Laurent Aimar's avatar
Laurent Aimar committed
172
173
    sh->i_cabac_init_idc = param->i_cabac_init_idc;

Fiona Glaser's avatar
Fiona Glaser committed
174
175
    sh->i_qp = SPEC_QP(i_qp);
    sh->i_qp_delta = sh->i_qp - pps->i_pic_init_qp;
Laurent Aimar's avatar
Laurent Aimar committed
176
177
178
    sh->b_sp_for_swidth = 0;
    sh->i_qs_delta = 0;

179
    int deblock_thresh = i_qp + 2 * X264_MIN(param->i_deblocking_filter_alphac0, param->i_deblocking_filter_beta);
180
    /* If effective qp <= 15, deblocking would have no effect anyway */
181
    if( param->b_deblocking_filter && (h->mb.b_variable_qp || 15 < deblock_thresh ) )
182
        sh->i_disable_deblocking_filter_idc = param->b_sliced_threads ? 2 : 0;
Laurent Aimar's avatar
Laurent Aimar committed
183
184
185
186
187
188
189
190
    else
        sh->i_disable_deblocking_filter_idc = 1;
    sh->i_alpha_c0_offset = param->i_deblocking_filter_alphac0 << 1;
    sh->i_beta_offset = param->i_deblocking_filter_beta << 1;
}

static void x264_slice_header_write( bs_t *s, x264_slice_header_t *sh, int i_nal_ref_idc )
{
191
192
    if( sh->b_mbaff )
    {
193
194
195
196
        int first_x = sh->i_first_mb % sh->sps->i_mb_width;
        int first_y = sh->i_first_mb / sh->sps->i_mb_width;
        assert( (first_y&1) == 0 );
        bs_write_ue( s, (2*first_x + sh->sps->i_mb_width*(first_y&~1) + (first_y&1)) >> 1 );
197
198
199
200
    }
    else
        bs_write_ue( s, sh->i_first_mb );

Laurent Aimar's avatar
Laurent Aimar committed
201
202
    bs_write_ue( s, sh->i_type + 5 );   /* same type things */
    bs_write_ue( s, sh->i_pps_id );
Fiona Glaser's avatar
Fiona Glaser committed
203
    bs_write( s, sh->sps->i_log2_max_frame_num, sh->i_frame_num & ((1<<sh->sps->i_log2_max_frame_num)-1) );
Laurent Aimar's avatar
Laurent Aimar committed
204

205
206
207
    if( !sh->sps->b_frame_mbs_only )
    {
        bs_write1( s, sh->b_field_pic );
Fiona Glaser's avatar
Fiona Glaser committed
208
        if( sh->b_field_pic )
209
210
211
            bs_write1( s, sh->b_bottom_field );
    }

Laurent Aimar's avatar
Laurent Aimar committed
212
213
214
215
216
    if( sh->i_idr_pic_id >= 0 ) /* NAL IDR */
        bs_write_ue( s, sh->i_idr_pic_id );

    if( sh->sps->i_poc_type == 0 )
    {
217
        bs_write( s, sh->sps->i_log2_max_poc_lsb, sh->i_poc & ((1<<sh->sps->i_log2_max_poc_lsb)-1) );
Laurent Aimar's avatar
Laurent Aimar committed
218
219
220
221
222
223
224
225
226
        if( sh->pps->b_pic_order && !sh->b_field_pic )
            bs_write_se( s, sh->i_delta_poc_bottom );
    }

    if( sh->pps->b_redundant_pic_cnt )
        bs_write_ue( s, sh->i_redundant_pic_cnt );

    if( sh->i_type == SLICE_TYPE_B )
        bs_write1( s, sh->b_direct_spatial_mv_pred );
227

228
    if( sh->i_type == SLICE_TYPE_P || sh->i_type == SLICE_TYPE_B )
Laurent Aimar's avatar
Laurent Aimar committed
229
230
231
232
233
234
235
236
237
238
239
240
241
    {
        bs_write1( s, sh->b_num_ref_idx_override );
        if( sh->b_num_ref_idx_override )
        {
            bs_write_ue( s, sh->i_num_ref_idx_l0_active - 1 );
            if( sh->i_type == SLICE_TYPE_B )
                bs_write_ue( s, sh->i_num_ref_idx_l1_active - 1 );
        }
    }

    /* ref pic list reordering */
    if( sh->i_type != SLICE_TYPE_I )
    {
242
243
        bs_write1( s, sh->b_ref_pic_list_reordering[0] );
        if( sh->b_ref_pic_list_reordering[0] )
Laurent Aimar's avatar
Laurent Aimar committed
244
        {
245
            for( int i = 0; i < sh->i_num_ref_idx_l0_active; i++ )
246
247
248
249
250
            {
                bs_write_ue( s, sh->ref_pic_list_order[0][i].idc );
                bs_write_ue( s, sh->ref_pic_list_order[0][i].arg );
            }
            bs_write_ue( s, 3 );
Laurent Aimar's avatar
Laurent Aimar committed
251
252
253
254
        }
    }
    if( sh->i_type == SLICE_TYPE_B )
    {
255
256
        bs_write1( s, sh->b_ref_pic_list_reordering[1] );
        if( sh->b_ref_pic_list_reordering[1] )
Laurent Aimar's avatar
Laurent Aimar committed
257
        {
258
            for( int i = 0; i < sh->i_num_ref_idx_l1_active; i++ )
259
260
261
262
263
            {
                bs_write_ue( s, sh->ref_pic_list_order[1][i].idc );
                bs_write_ue( s, sh->ref_pic_list_order[1][i].arg );
            }
            bs_write_ue( s, 3 );
Laurent Aimar's avatar
Laurent Aimar committed
264
265
266
        }
    }

267
    if( sh->pps->b_weighted_pred && sh->i_type == SLICE_TYPE_P )
Laurent Aimar's avatar
Laurent Aimar committed
268
    {
Dylan Yudaken's avatar
Dylan Yudaken committed
269
270
271
        /* pred_weight_table() */
        bs_write_ue( s, sh->weight[0][0].i_denom );
        bs_write_ue( s, sh->weight[0][1].i_denom );
272
        for( int i = 0; i < sh->i_num_ref_idx_l0_active; i++ )
Dylan Yudaken's avatar
Dylan Yudaken committed
273
274
275
276
277
278
279
280
281
282
283
284
        {
            int luma_weight_l0_flag = !!sh->weight[i][0].weightfn;
            int chroma_weight_l0_flag = !!sh->weight[i][1].weightfn || !!sh->weight[i][2].weightfn;
            bs_write1( s, luma_weight_l0_flag );
            if( luma_weight_l0_flag )
            {
                bs_write_se( s, sh->weight[i][0].i_scale );
                bs_write_se( s, sh->weight[i][0].i_offset );
            }
            bs_write1( s, chroma_weight_l0_flag );
            if( chroma_weight_l0_flag )
            {
285
                for( int j = 1; j < 3; j++ )
Dylan Yudaken's avatar
Dylan Yudaken committed
286
287
288
289
290
291
292
293
294
295
                {
                    bs_write_se( s, sh->weight[i][j].i_scale );
                    bs_write_se( s, sh->weight[i][j].i_offset );
                }
            }
        }
    }
    else if( sh->pps->b_weighted_bipred == 1 && sh->i_type == SLICE_TYPE_B )
    {
      /* TODO */
Laurent Aimar's avatar
Laurent Aimar committed
296
297
298
299
300
301
302
303
304
305
306
    }

    if( i_nal_ref_idc != 0 )
    {
        if( sh->i_idr_pic_id >= 0 )
        {
            bs_write1( s, 0 );  /* no output of prior pics flag */
            bs_write1( s, 0 );  /* long term reference flag */
        }
        else
        {
Lamont Alston's avatar
Lamont Alston committed
307
308
309
            bs_write1( s, sh->i_mmco_command_count > 0 ); /* adaptive_ref_pic_marking_mode_flag */
            if( sh->i_mmco_command_count > 0 )
            {
310
                for( int i = 0; i < sh->i_mmco_command_count; i++ )
Lamont Alston's avatar
Lamont Alston committed
311
312
313
314
315
316
                {
                    bs_write_ue( s, 1 ); /* mark short term ref as unused */
                    bs_write_ue( s, sh->mmco[i].i_difference_of_pic_nums - 1 );
                }
                bs_write_ue( s, 0 ); /* end command list */
            }
Laurent Aimar's avatar
Laurent Aimar committed
317
318
319
320
321
        }
    }

    if( sh->pps->b_cabac && sh->i_type != SLICE_TYPE_I )
        bs_write_ue( s, sh->i_cabac_init_idc );
322

Laurent Aimar's avatar
Laurent Aimar committed
323
324
325
326
327
328
329
330
331
332
333
334
335
    bs_write_se( s, sh->i_qp_delta );      /* slice qp delta */

    if( sh->pps->b_deblocking_filter_control )
    {
        bs_write_ue( s, sh->i_disable_deblocking_filter_idc );
        if( sh->i_disable_deblocking_filter_idc != 1 )
        {
            bs_write_se( s, sh->i_alpha_c0_offset >> 1 );
            bs_write_se( s, sh->i_beta_offset >> 1 );
        }
    }
}

336
337
/* If we are within a reasonable distance of the end of the memory allocated for the bitstream, */
/* reallocate, adding an arbitrary amount of space (100 kilobytes). */
338
static int x264_bitstream_check_buffer( x264_t *h )
339
{
340
    uint8_t *bs_bak = h->out.p_bitstream;
341
342
343
    int max_mb_size = 2500 << SLICE_MBAFF;
    if( (h->param.b_cabac && (h->cabac.p_end - h->cabac.p < max_mb_size)) ||
        (h->out.bs.p_end - h->out.bs.p < max_mb_size) )
344
345
    {
        h->out.i_bitstream += 100000;
346
347
        CHECKED_MALLOC( h->out.p_bitstream, h->out.i_bitstream );
        h->mc.memcpy_aligned( h->out.p_bitstream, bs_bak, (h->out.i_bitstream - 100000) & ~15 );
348
        intptr_t delta = h->out.p_bitstream - bs_bak;
349
350
351
352
353
354
355
356
357

        h->out.bs.p_start += delta;
        h->out.bs.p += delta;
        h->out.bs.p_end = h->out.p_bitstream + h->out.i_bitstream;

        h->cabac.p_start += delta;
        h->cabac.p += delta;
        h->cabac.p_end = h->out.p_bitstream + h->out.i_bitstream;

358
        for( int i = 0; i <= h->out.i_nal; i++ )
359
            h->out.nal[i].p_payload += delta;
360
        x264_free( bs_bak );
361
    }
362
363
364
365
    return 0;
fail:
    x264_free( bs_bak );
    return -1;
366
367
}

368
#if HAVE_THREAD
369
370
371
372
373
374
375
376
377
378
379
380
381
static void x264_encoder_thread_init( x264_t *h )
{
    if( h->param.i_sync_lookahead )
        x264_lower_thread_priority( 10 );

#if HAVE_MMX
    /* Misalign mask has to be set separately for each thread. */
    if( h->param.cpu&X264_CPU_SSE_MISALIGN )
        x264_cpu_mask_misalign_sse();
#endif
}
#endif

Laurent Aimar's avatar
Laurent Aimar committed
382
383
384
385
386
387
388
389
/****************************************************************************
 *
 ****************************************************************************
 ****************************** External API*********************************
 ****************************************************************************
 *
 ****************************************************************************/

390
static int x264_validate_parameters( x264_t *h, int b_open )
Laurent Aimar's avatar
Laurent Aimar committed
391
{
Steven Walters's avatar
Steven Walters committed
392
#if HAVE_MMX
393
#ifdef __SSE__
394
    if( b_open && !(x264_cpu_detect() & X264_CPU_SSE) )
395
    {
396
        x264_log( h, X264_LOG_ERROR, "your cpu does not support SSE1, but x264 was compiled with asm support\n");
397
#else
Loren Merritt's avatar
Loren Merritt committed
398
    if( b_open && !(x264_cpu_detect() & X264_CPU_MMX2) )
399
400
401
    {
        x264_log( h, X264_LOG_ERROR, "your cpu does not support MMXEXT, but x264 was compiled with asm support\n");
#endif
402
403
404
405
        x264_log( h, X264_LOG_ERROR, "to run x264, recompile without asm support (configure --disable-asm)\n");
        return -1;
    }
#endif
Henrik Gramner's avatar
Henrik Gramner committed
406
407
408
409
410
411
412
413
414
415
416

#if HAVE_INTERLACED
    h->param.b_interlaced = !!PARAM_INTERLACED;
#else
    if( h->param.b_interlaced )
    {
        x264_log( h, X264_LOG_ERROR, "not compiled with interlaced support\n" );
        return -1;
    }
#endif

417
    if( h->param.i_width <= 0 || h->param.i_height <= 0 )
Laurent Aimar's avatar
Laurent Aimar committed
418
    {
419
        x264_log( h, X264_LOG_ERROR, "invalid width x height (%dx%d)\n",
420
421
                  h->param.i_width, h->param.i_height );
        return -1;
Laurent Aimar's avatar
Laurent Aimar committed
422
423
    }

Fiona Glaser's avatar
Fiona Glaser committed
424
    int i_csp = h->param.i_csp & X264_CSP_MASK;
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#if X264_CHROMA_FORMAT
    if( CHROMA_FORMAT != CHROMA_420 && i_csp >= X264_CSP_I420 && i_csp <= X264_CSP_NV12 )
    {
        x264_log( h, X264_LOG_ERROR, "not compiled with 4:2:0 support\n" );
        return -1;
    }
    else if( CHROMA_FORMAT != CHROMA_422 && i_csp >= X264_CSP_I422 && i_csp <= X264_CSP_NV16 )
    {
        x264_log( h, X264_LOG_ERROR, "not compiled with 4:2:2 support\n" );
        return -1;
    }
    else if( CHROMA_FORMAT != CHROMA_444 && i_csp >= X264_CSP_I444 && i_csp <= X264_CSP_RGB )
    {
        x264_log( h, X264_LOG_ERROR, "not compiled with 4:4:4 support\n" );
        return -1;
    }
#endif
Fiona Glaser's avatar
Fiona Glaser committed
442
    if( i_csp <= X264_CSP_NONE || i_csp >= X264_CSP_MAX )
Laurent Aimar's avatar
Laurent Aimar committed
443
    {
Henrik Gramner's avatar
Henrik Gramner committed
444
        x264_log( h, X264_LOG_ERROR, "invalid CSP (only I420/YV12/NV12/I422/YV16/NV16/I444/YV24/BGR/BGRA/RGB supported)\n" );
445
        return -1;
Laurent Aimar's avatar
Laurent Aimar committed
446
    }
Fiona Glaser's avatar
Fiona Glaser committed
447

Henrik Gramner's avatar
Henrik Gramner committed
448
    if( i_csp < X264_CSP_I444 && h->param.i_width % 2 )
Laurent Aimar's avatar
Laurent Aimar committed
449
    {
Henrik Gramner's avatar
Henrik Gramner committed
450
        x264_log( h, X264_LOG_ERROR, "width not divisible by 2 (%dx%d)\n",
Fiona Glaser's avatar
Fiona Glaser committed
451
                  h->param.i_width, h->param.i_height );
452
        return -1;
Laurent Aimar's avatar
Laurent Aimar committed
453
454
    }

Henrik Gramner's avatar
Henrik Gramner committed
455
    if( i_csp < X264_CSP_I422 && PARAM_INTERLACED && h->param.i_height % 4 )
456
    {
Henrik Gramner's avatar
Henrik Gramner committed
457
458
459
460
461
462
463
464
465
        x264_log( h, X264_LOG_ERROR, "height not divisible by 4 (%dx%d)\n",
                  h->param.i_width, h->param.i_height );
        return -1;
    }

    if( (i_csp < X264_CSP_I422 || PARAM_INTERLACED) && h->param.i_height % 2 )
    {
        x264_log( h, X264_LOG_ERROR, "height not divisible by 2 (%dx%d)\n",
                  h->param.i_width, h->param.i_height );
466
467
468
        return -1;
    }

469
470
471
472
473
474
475
476
    if( (h->param.crop_rect.i_left + h->param.crop_rect.i_right ) >= h->param.i_width ||
        (h->param.crop_rect.i_top  + h->param.crop_rect.i_bottom) >= h->param.i_height )
    {
        x264_log( h, X264_LOG_ERROR, "invalid crop-rect %u,%u,%u,%u\n", h->param.crop_rect.i_left,
                  h->param.crop_rect.i_top, h->param.crop_rect.i_right,  h->param.crop_rect.i_bottom );
        return -1;
    }

Steven Walters's avatar
Steven Walters committed
477
    if( h->param.i_threads == X264_THREADS_AUTO )
Fiona Glaser's avatar
Fiona Glaser committed
478
        h->param.i_threads = x264_cpu_num_processors() * (h->param.b_sliced_threads?2:3)/2;
Loren Merritt's avatar
Loren Merritt committed
479
480
    if( h->param.i_threads > 1 )
    {
481
482
#if !HAVE_THREAD
        x264_log( h, X264_LOG_WARNING, "not compiled with thread support!\n");
Loren Merritt's avatar
Loren Merritt committed
483
        h->param.i_threads = 1;
Loren Merritt's avatar
Loren Merritt committed
484
#endif
485
486
487
488
489
490
491
        /* Avoid absurdly small thread slices as they can reduce performance
         * and VBV compliance.  Capped at an arbitrary 4 rows per thread. */
        if( h->param.b_sliced_threads )
        {
            int max_threads = (h->param.i_height+15)/16 / 4;
            h->param.i_threads = X264_MIN( h->param.i_threads, max_threads );
        }
Loren Merritt's avatar
Loren Merritt committed
492
    }
493
494
    h->param.i_threads = x264_clip3( h->param.i_threads, 1, X264_THREAD_MAX );
    if( h->param.i_threads == 1 )
495
        h->param.b_sliced_threads = 0;
496
    h->i_thread_frames = h->param.b_sliced_threads ? 1 : h->param.i_threads;
497
498
    if( h->i_thread_frames > 1 )
        h->param.nalu_process = NULL;
Loren Merritt's avatar
Loren Merritt committed
499

500
501
502
503
504
505
506
    h->param.i_keyint_max = x264_clip3( h->param.i_keyint_max, 1, X264_KEYINT_MAX_INFINITE );
    if( h->param.i_keyint_max == 1 )
    {
        h->param.b_intra_refresh = 0;
        h->param.analyse.i_weighted_pred = 0;
    }

507
508
    h->param.i_frame_packing = x264_clip3( h->param.i_frame_packing, -1, 5 );

509
    /* Detect default ffmpeg settings and terminate with an error. */
510
    if( b_open )
511
512
513
514
515
516
517
518
519
520
521
522
523
524
    {
        int score = 0;
        score += h->param.analyse.i_me_range == 0;
        score += h->param.rc.i_qp_step == 3;
        score += h->param.i_keyint_max == 12;
        score += h->param.rc.i_qp_min == 2;
        score += h->param.rc.i_qp_max == 31;
        score += h->param.rc.f_qcompress == 0.5;
        score += fabs(h->param.rc.f_ip_factor - 1.25) < 0.01;
        score += fabs(h->param.rc.f_pb_factor - 1.25) < 0.01;
        score += h->param.analyse.inter == 0 && h->param.analyse.i_subpel_refine == 8;
        if( score >= 5 )
        {
            x264_log( h, X264_LOG_ERROR, "broken ffmpeg default settings detected\n" );
525
526
527
528
            x264_log( h, X264_LOG_ERROR, "use an encoding preset (e.g. -vpre medium)\n" );
            x264_log( h, X264_LOG_ERROR, "preset usage: -vpre <speed> -vpre <profile>\n" );
            x264_log( h, X264_LOG_ERROR, "speed presets are listed in x264 --help\n" );
            x264_log( h, X264_LOG_ERROR, "profile is optional; x264 defaults to high\n" );
529
530
531
532
            return -1;
        }
    }

533
534
    if( h->param.rc.i_rc_method < 0 || h->param.rc.i_rc_method > 2 )
    {
535
        x264_log( h, X264_LOG_ERROR, "no ratecontrol method specified\n" );
536
537
        return -1;
    }
Fiona Glaser's avatar
Fiona Glaser committed
538
    h->param.rc.f_rf_constant = x264_clip3f( h->param.rc.f_rf_constant, -QP_BD_OFFSET, 51 );
539
    h->param.rc.f_rf_constant_max = x264_clip3f( h->param.rc.f_rf_constant_max, -QP_BD_OFFSET, 51 );
540
    h->param.rc.i_qp_constant = x264_clip3( h->param.rc.i_qp_constant, 0, QP_MAX );
541
    h->param.analyse.i_subpel_refine = x264_clip3( h->param.analyse.i_subpel_refine, 0, 11 );
542
543
    h->param.rc.f_ip_factor = X264_MAX( h->param.rc.f_ip_factor, 0.01f );
    h->param.rc.f_pb_factor = X264_MAX( h->param.rc.f_pb_factor, 0.01f );
544
    if( h->param.rc.i_rc_method == X264_RC_CRF )
Fiona Glaser's avatar
Fiona Glaser committed
545
    {
Fiona Glaser's avatar
Fiona Glaser committed
546
        h->param.rc.i_qp_constant = h->param.rc.f_rf_constant + QP_BD_OFFSET;
Fiona Glaser's avatar
Fiona Glaser committed
547
548
        h->param.rc.i_bitrate = 0;
    }
549
550
    if( (h->param.rc.i_rc_method == X264_RC_CQP || h->param.rc.i_rc_method == X264_RC_CRF)
        && h->param.rc.i_qp_constant == 0 )
Loren Merritt's avatar
Loren Merritt committed
551
552
    {
        h->mb.b_lossless = 1;
Loren Merritt's avatar
Loren Merritt committed
553
        h->param.i_cqm_preset = X264_CQM_FLAT;
Loren Merritt's avatar
Loren Merritt committed
554
        h->param.psz_cqm_file = NULL;
555
        h->param.rc.i_rc_method = X264_RC_CQP;
Loren Merritt's avatar
Loren Merritt committed
556
557
558
        h->param.rc.f_ip_factor = 1;
        h->param.rc.f_pb_factor = 1;
        h->param.analyse.b_psnr = 0;
559
        h->param.analyse.b_ssim = 0;
560
        h->param.analyse.i_chroma_qp_offset = 0;
561
        h->param.analyse.i_trellis = 0;
Loren Merritt's avatar
Loren Merritt committed
562
        h->param.analyse.b_fast_pskip = 0;
563
        h->param.analyse.i_noise_reduction = 0;
Anton Mitrofanov's avatar
Anton Mitrofanov committed
564
        h->param.analyse.b_psy = 0;
565
        h->param.i_bframe = 0;
566
567
        /* 8x8dct is not useful without RD in CAVLC lossless */
        if( !h->param.b_cabac && h->param.analyse.i_subpel_refine < 6 )
568
            h->param.analyse.b_transform_8x8 = 0;
Loren Merritt's avatar
Loren Merritt committed
569
    }
Loren Merritt's avatar
Loren Merritt committed
570
571
572
    if( h->param.rc.i_rc_method == X264_RC_CQP )
    {
        float qp_p = h->param.rc.i_qp_constant;
573
574
        float qp_i = qp_p - 6*log2f( h->param.rc.f_ip_factor );
        float qp_b = qp_p + 6*log2f( h->param.rc.f_pb_factor );
575
576
        h->param.rc.i_qp_min = x264_clip3( (int)(X264_MIN3( qp_p, qp_i, qp_b )), 0, QP_MAX );
        h->param.rc.i_qp_max = x264_clip3( (int)(X264_MAX3( qp_p, qp_i, qp_b ) + .999), 0, QP_MAX );
Loren Merritt's avatar
Loren Merritt committed
577
        h->param.rc.i_aq_mode = 0;
Fiona Glaser's avatar
Fiona Glaser committed
578
        h->param.rc.b_mb_tree = 0;
579
        h->param.rc.i_bitrate = 0;
Loren Merritt's avatar
Loren Merritt committed
580
    }
581
    h->param.rc.i_qp_max = x264_clip3( h->param.rc.i_qp_max, 0, QP_MAX );
582
    h->param.rc.i_qp_min = x264_clip3( h->param.rc.i_qp_min, 0, h->param.rc.i_qp_max );
583
    h->param.rc.i_qp_step = x264_clip3( h->param.rc.i_qp_step, 0, QP_MAX );
584
585
586
587
    h->param.rc.i_bitrate = x264_clip3( h->param.rc.i_bitrate, 0, 2000000 );
    h->param.rc.i_vbv_buffer_size = x264_clip3( h->param.rc.i_vbv_buffer_size, 0, 2000000 );
    h->param.rc.i_vbv_max_bitrate = x264_clip3( h->param.rc.i_vbv_max_bitrate, 0, 2000000 );
    h->param.rc.f_vbv_buffer_init = x264_clip3f( h->param.rc.f_vbv_buffer_init, 0, 2000000 );
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
    if( h->param.rc.i_vbv_buffer_size )
    {
        if( h->param.rc.i_rc_method == X264_RC_CQP )
        {
            x264_log( h, X264_LOG_WARNING, "VBV is incompatible with constant QP, ignored.\n" );
            h->param.rc.i_vbv_max_bitrate = 0;
            h->param.rc.i_vbv_buffer_size = 0;
        }
        else if( h->param.rc.i_vbv_max_bitrate == 0 )
        {
            if( h->param.rc.i_rc_method == X264_RC_ABR )
            {
                x264_log( h, X264_LOG_WARNING, "VBV maxrate unspecified, assuming CBR\n" );
                h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate;
            }
            else
            {
                x264_log( h, X264_LOG_WARNING, "VBV bufsize set but maxrate unspecified, ignored\n" );
                h->param.rc.i_vbv_buffer_size = 0;
            }
        }
        else if( h->param.rc.i_vbv_max_bitrate < h->param.rc.i_bitrate &&
                 h->param.rc.i_rc_method == X264_RC_ABR )
        {
            x264_log( h, X264_LOG_WARNING, "max bitrate less than average bitrate, assuming CBR\n" );
            h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate;
        }
    }
    else if( h->param.rc.i_vbv_max_bitrate )
    {
        x264_log( h, X264_LOG_WARNING, "VBV maxrate specified, but no bufsize, ignored\n" );
        h->param.rc.i_vbv_max_bitrate = 0;
    }
Loren Merritt's avatar
Loren Merritt committed
621

Fiona Glaser's avatar
Fiona Glaser committed
622
623
624
    h->param.i_slice_max_size = X264_MAX( h->param.i_slice_max_size, 0 );
    h->param.i_slice_max_mbs = X264_MAX( h->param.i_slice_max_mbs, 0 );

625
    int max_slices = (h->param.i_height+((16<<PARAM_INTERLACED)-1))/(16<<PARAM_INTERLACED);
Fiona Glaser's avatar
Fiona Glaser committed
626
627
628
629
630
631
632
633
    if( h->param.b_sliced_threads )
        h->param.i_slice_count = x264_clip3( h->param.i_threads, 0, max_slices );
    else
    {
        h->param.i_slice_count = x264_clip3( h->param.i_slice_count, 0, max_slices );
        if( h->param.i_slice_max_mbs || h->param.i_slice_max_size )
            h->param.i_slice_count = 0;
    }
Fiona Glaser's avatar
Fiona Glaser committed
634

635
636
637
638
639
640
641
642
643
644
645
646
647
    if( h->param.b_bluray_compat )
    {
        h->param.i_bframe_pyramid = X264_MIN( X264_B_PYRAMID_STRICT, h->param.i_bframe_pyramid );
        h->param.i_bframe = X264_MIN( h->param.i_bframe, 3 );
        h->param.b_aud = 1;
        h->param.i_nal_hrd = X264_MAX( h->param.i_nal_hrd, X264_NAL_HRD_VBR );
        h->param.i_slice_max_size = 0;
        h->param.i_slice_max_mbs = 0;
        h->param.b_intra_refresh = 0;
        h->param.i_frame_reference = X264_MIN( h->param.i_frame_reference, 6 );
        h->param.i_dpb_size = X264_MIN( h->param.i_dpb_size, 6 );
        /* Due to the proliferation of broken players that don't handle dupes properly. */
        h->param.analyse.i_weighted_pred = X264_MIN( h->param.analyse.i_weighted_pred, X264_WEIGHTP_SIMPLE );
648
649
        if( h->param.b_fake_interlaced )
            h->param.b_pic_struct = 1;
650
651
    }

652
653
    h->param.i_frame_reference = x264_clip3( h->param.i_frame_reference, 1, X264_REF_MAX );
    h->param.i_dpb_size = x264_clip3( h->param.i_dpb_size, 1, X264_REF_MAX );
Fiona Glaser's avatar
Fiona Glaser committed
654
655
    if( h->param.i_scenecut_threshold < 0 )
        h->param.i_scenecut_threshold = 0;
656
    h->param.analyse.i_direct_mv_pred = x264_clip3( h->param.analyse.i_direct_mv_pred, X264_DIRECT_PRED_NONE, X264_DIRECT_PRED_AUTO );
657
658
659
660
661
    if( !h->param.analyse.i_subpel_refine && h->param.analyse.i_direct_mv_pred > X264_DIRECT_PRED_SPATIAL )
    {
        x264_log( h, X264_LOG_WARNING, "subme=0 + direct=temporal is not supported\n" );
        h->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
    }
Lamont Alston's avatar
Lamont Alston committed
662
    h->param.i_bframe = x264_clip3( h->param.i_bframe, 0, X264_MIN( X264_BFRAME_MAX, h->param.i_keyint_max-1 ) );
663
    h->param.i_bframe_bias = x264_clip3( h->param.i_bframe_bias, -90, 100 );
Lamont Alston's avatar
Lamont Alston committed
664
665
666
    if( h->param.i_bframe <= 1 )
        h->param.i_bframe_pyramid = X264_B_PYRAMID_NONE;
    h->param.i_bframe_pyramid = x264_clip3( h->param.i_bframe_pyramid, X264_B_PYRAMID_NONE, X264_B_PYRAMID_NORMAL );
667
    h->param.i_bframe_adaptive = x264_clip3( h->param.i_bframe_adaptive, X264_B_ADAPT_NONE, X264_B_ADAPT_TRELLIS );
668
    if( !h->param.i_bframe )
669
    {
670
        h->param.i_bframe_adaptive = X264_B_ADAPT_NONE;
671
672
        h->param.analyse.i_direct_mv_pred = 0;
        h->param.analyse.b_weighted_bipred = 0;
673
        h->param.b_open_gop = 0;
674
    }
Fiona Glaser's avatar
Fiona Glaser committed
675
676
677
678
679
    if( h->param.b_intra_refresh && h->param.i_bframe_pyramid == X264_B_PYRAMID_NORMAL )
    {
        x264_log( h, X264_LOG_WARNING, "b-pyramid normal + intra-refresh is not supported\n" );
        h->param.i_bframe_pyramid = X264_B_PYRAMID_STRICT;
    }
680
    if( h->param.b_intra_refresh && (h->param.i_frame_reference > 1 || h->param.i_dpb_size > 1) )
Fiona Glaser's avatar
Fiona Glaser committed
681
682
683
    {
        x264_log( h, X264_LOG_WARNING, "ref > 1 + intra-refresh is not supported\n" );
        h->param.i_frame_reference = 1;
684
        h->param.i_dpb_size = 1;
Fiona Glaser's avatar
Fiona Glaser committed
685
    }
686
    if( h->param.b_intra_refresh && h->param.b_open_gop )
Lamont Alston's avatar
Lamont Alston committed
687
688
    {
        x264_log( h, X264_LOG_WARNING, "intra-refresh is not compatible with open-gop\n" );
689
        h->param.b_open_gop = 0;
Lamont Alston's avatar
Lamont Alston committed
690
    }
691
    float fps = h->param.i_fps_num > 0 && h->param.i_fps_den > 0 ? (float) h->param.i_fps_num / h->param.i_fps_den : 25.0;
Fiona Glaser's avatar
Fiona Glaser committed
692
    if( h->param.i_keyint_min == X264_KEYINT_MIN_AUTO )
693
        h->param.i_keyint_min = X264_MIN( h->param.i_keyint_max / 10, fps );
Fiona Glaser's avatar
Fiona Glaser committed
694
    h->param.i_keyint_min = x264_clip3( h->param.i_keyint_min, 1, h->param.i_keyint_max/2+1 );
Fiona Glaser's avatar
Fiona Glaser committed
695
    h->param.rc.i_lookahead = x264_clip3( h->param.rc.i_lookahead, 0, X264_LOOKAHEAD_MAX );
Fiona Glaser's avatar
Fiona Glaser committed
696
697
698
699
700
701
    {
        int maxrate = X264_MAX( h->param.rc.i_vbv_max_bitrate, h->param.rc.i_bitrate );
        float bufsize = maxrate ? (float)h->param.rc.i_vbv_buffer_size / maxrate : 0;
        h->param.rc.i_lookahead = X264_MIN( h->param.rc.i_lookahead, X264_MAX( h->param.i_keyint_max, bufsize*fps ) );
    }

702
    if( !h->param.i_timebase_num || !h->param.i_timebase_den || !(h->param.b_vfr_input || h->param.b_pulldown) )
703
704
705
706
707
    {
        h->param.i_timebase_num = h->param.i_fps_den;
        h->param.i_timebase_den = h->param.i_fps_num;
    }

708
    h->param.rc.f_qcompress = x264_clip3f( h->param.rc.f_qcompress, 0.0, 1.0 );
Fiona Glaser's avatar
Fiona Glaser committed
709
    if( h->param.i_keyint_max == 1 || h->param.rc.f_qcompress == 1 )
710
        h->param.rc.b_mb_tree = 0;
711
712
    if( (!h->param.b_intra_refresh && h->param.i_keyint_max != X264_KEYINT_MAX_INFINITE) &&
        !h->param.rc.i_lookahead && h->param.rc.b_mb_tree )
Fiona Glaser's avatar
Fiona Glaser committed
713
    {
714
        x264_log( h, X264_LOG_WARNING, "lookaheadless mb-tree requires intra refresh or infinite keyint\n" );
Fiona Glaser's avatar
Fiona Glaser committed
715
716
        h->param.rc.b_mb_tree = 0;
    }
Fiona Glaser's avatar
Fiona Glaser committed
717
718
    if( h->param.rc.b_stat_read )
        h->param.rc.i_lookahead = 0;
719
#if HAVE_THREAD
720
721
722
    if( h->param.i_sync_lookahead < 0 )
        h->param.i_sync_lookahead = h->param.i_bframe + 1;
    h->param.i_sync_lookahead = X264_MIN( h->param.i_sync_lookahead, X264_LOOKAHEAD_MAX );
723
    if( h->param.rc.b_stat_read || h->i_thread_frames == 1 )
Steven Walters's avatar
Steven Walters committed
724
725
726
727
        h->param.i_sync_lookahead = 0;
#else
    h->param.i_sync_lookahead = 0;
#endif
Fiona Glaser's avatar
Fiona Glaser committed
728

Laurent Aimar's avatar
Laurent Aimar committed
729
730
    h->param.i_deblocking_filter_alphac0 = x264_clip3( h->param.i_deblocking_filter_alphac0, -6, 6 );
    h->param.i_deblocking_filter_beta    = x264_clip3( h->param.i_deblocking_filter_beta, -6, 6 );
731
732
    h->param.analyse.i_luma_deadzone[0] = x264_clip3( h->param.analyse.i_luma_deadzone[0], 0, 32 );
    h->param.analyse.i_luma_deadzone[1] = x264_clip3( h->param.analyse.i_luma_deadzone[1], 0, 32 );
Laurent Aimar's avatar
Laurent Aimar committed
733

734
    h->param.i_cabac_init_idc = x264_clip3( h->param.i_cabac_init_idc, 0, 2 );
Laurent Aimar's avatar
Laurent Aimar committed
735

Loren Merritt's avatar
Loren Merritt committed
736
737
738
    if( h->param.i_cqm_preset < X264_CQM_FLAT || h->param.i_cqm_preset > X264_CQM_CUSTOM )
        h->param.i_cqm_preset = X264_CQM_FLAT;

739
    if( h->param.analyse.i_me_method < X264_ME_DIA ||
740
        h->param.analyse.i_me_method > X264_ME_TESA )
741
        h->param.analyse.i_me_method = X264_ME_HEX;
742
    h->param.analyse.i_me_range = x264_clip3( h->param.analyse.i_me_range, 4, 1024 );
743
    if( h->param.analyse.i_me_range > 16 && h->param.analyse.i_me_method <= X264_ME_HEX )
744
        h->param.analyse.i_me_range = 16;
745
746
747
    if( h->param.analyse.i_me_method == X264_ME_TESA &&
        (h->mb.b_lossless || h->param.analyse.i_subpel_refine <= 1) )
        h->param.analyse.i_me_method = X264_ME_ESA;
748
749
750
751
    h->param.analyse.b_mixed_references = h->param.analyse.b_mixed_references && h->param.i_frame_reference > 1;
    h->param.analyse.inter &= X264_ANALYSE_PSUB16x16|X264_ANALYSE_PSUB8x8|X264_ANALYSE_BSUB16x16|
                              X264_ANALYSE_I4x4|X264_ANALYSE_I8x8;
    h->param.analyse.intra &= X264_ANALYSE_I4x4|X264_ANALYSE_I8x8;
752
753
    if( !(h->param.analyse.inter & X264_ANALYSE_PSUB16x16) )
        h->param.analyse.inter &= ~X264_ANALYSE_PSUB8x8;
754
755
756
757
758
    if( !h->param.analyse.b_transform_8x8 )
    {
        h->param.analyse.inter &= ~X264_ANALYSE_I8x8;
        h->param.analyse.intra &= ~X264_ANALYSE_I8x8;
    }
759
    h->param.analyse.i_trellis = x264_clip3( h->param.analyse.i_trellis, 0, 2 );
760
761
762
763
    h->param.rc.i_aq_mode = x264_clip3( h->param.rc.i_aq_mode, 0, 2 );
    h->param.rc.f_aq_strength = x264_clip3f( h->param.rc.f_aq_strength, 0, 3 );
    if( h->param.rc.f_aq_strength == 0 )
        h->param.rc.i_aq_mode = 0;
764
765
766
767
768
769
770

    if( h->param.i_log_level < X264_LOG_INFO )
    {
        h->param.analyse.b_psnr = 0;
        h->param.analyse.b_ssim = 0;
    }
    /* Warn users trying to measure PSNR/SSIM with psy opts on. */
771
    if( b_open && (h->param.analyse.b_psnr || h->param.analyse.b_ssim) )
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
    {
        char *s = NULL;

        if( h->param.analyse.b_psy )
        {
            s = h->param.analyse.b_psnr ? "psnr" : "ssim";
            x264_log( h, X264_LOG_WARNING, "--%s used with psy on: results will be invalid!\n", s );
        }
        else if( !h->param.rc.i_aq_mode && h->param.analyse.b_ssim )
        {
            x264_log( h, X264_LOG_WARNING, "--ssim used with AQ off: results will be invalid!\n" );
            s = "ssim";
        }
        else if(  h->param.rc.i_aq_mode && h->param.analyse.b_psnr )
        {
            x264_log( h, X264_LOG_WARNING, "--psnr used with AQ on: results will be invalid!\n" );
            s = "psnr";
        }
        if( s )
            x264_log( h, X264_LOG_WARNING, "--tune %s should be used if attempting to benchmark %s!\n", s, s );
    }

Fiona Glaser's avatar
Fiona Glaser committed
794
795
796
797
798
    if( !h->param.analyse.b_psy )
    {
        h->param.analyse.f_psy_rd = 0;
        h->param.analyse.f_psy_trellis = 0;
    }
799
800
    h->param.analyse.f_psy_rd = x264_clip3f( h->param.analyse.f_psy_rd, 0, 10 );
    h->param.analyse.f_psy_trellis = x264_clip3f( h->param.analyse.f_psy_trellis, 0, 10 );
801
802
    h->mb.i_psy_rd = h->param.analyse.i_subpel_refine >= 6 ? FIX8( h->param.analyse.f_psy_rd ) : 0;
    h->mb.i_psy_trellis = h->param.analyse.i_trellis ? FIX8( h->param.analyse.f_psy_trellis / 4 ) : 0;
803
    h->param.analyse.i_chroma_qp_offset = x264_clip3(h->param.analyse.i_chroma_qp_offset, -32, 32);
Fiona Glaser's avatar
Fiona Glaser committed
804
    /* In 4:4:4 mode, chroma gets twice as much resolution, so we can halve its quality. */
xvidfan's avatar
xvidfan committed
805
    if( b_open && i_csp >= X264_CSP_I444 && i_csp < X264_CSP_BGR && h->param.analyse.b_psy )
Fiona Glaser's avatar
Fiona Glaser committed
806
        h->param.analyse.i_chroma_qp_offset += 6;
807
808
    /* Psy RDO increases overall quantizers to improve the quality of luma--this indirectly hurts chroma quality */
    /* so we lower the chroma QP offset to compensate */
809
    if( b_open && h->mb.i_psy_rd )
810
811
        h->param.analyse.i_chroma_qp_offset -= h->param.analyse.f_psy_rd < 0.25 ? 1 : 2;
    /* Psy trellis has a similar effect. */
812
    if( b_open && h->mb.i_psy_trellis )
813
814
        h->param.analyse.i_chroma_qp_offset -= h->param.analyse.f_psy_trellis < 0.25 ? 1 : 2;
    h->param.analyse.i_chroma_qp_offset = x264_clip3(h->param.analyse.i_chroma_qp_offset, -12, 12);
Fiona Glaser's avatar
Fiona Glaser committed
815
816
817
818
819
820
    /* MB-tree requires AQ to be on, even if the strength is zero. */
    if( !h->param.rc.i_aq_mode && h->param.rc.b_mb_tree )
    {
        h->param.rc.i_aq_mode = 1;
        h->param.rc.f_aq_strength = 0;
    }
821
    h->param.analyse.i_noise_reduction = x264_clip3( h->param.analyse.i_noise_reduction, 0, 1<<16 );
822
    if( h->param.analyse.i_subpel_refine >= 10 && (h->param.analyse.i_trellis != 2 || !h->param.rc.i_aq_mode) )
Fiona Glaser's avatar
Fiona Glaser committed
823
        h->param.analyse.i_subpel_refine = 9;
824

825
826
    {
        const x264_level_t *l = x264_levels;
827
        if( h->param.i_level_idc < 0 )
828
        {
Loren Merritt's avatar
Loren Merritt committed
829
            int maxrate_bak = h->param.rc.i_vbv_max_bitrate;
830
831
832
833
834
            if( h->param.rc.i_rc_method == X264_RC_ABR && h->param.rc.i_vbv_buffer_size <= 0 )
                h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate * 2;
            x264_sps_init( h->sps, h->param.i_sps_id, &h->param );
            do h->param.i_level_idc = l->level_idc;
                while( l[1].level_idc && x264_validate_levels( h, 0 ) && l++ );
Loren Merritt's avatar
Loren Merritt committed
835
            h->param.rc.i_vbv_max_bitrate = maxrate_bak;
836
837
838
839
840
841
842
843
844
845
        }
        else
        {
            while( l->level_idc && l->level_idc != h->param.i_level_idc )
                l++;
            if( l->level_idc == 0 )
            {
                x264_log( h, X264_LOG_ERROR, "invalid level_idc: %d\n", h->param.i_level_idc );
                return -1;
            }
846
847
        }
        if( h->param.analyse.i_mv_range <= 0 )
848
            h->param.analyse.i_mv_range = l->mv_range >> PARAM_INTERLACED;
849
        else
850
            h->param.analyse.i_mv_range = x264_clip3(h->param.analyse.i_mv_range, 32, 512 >> PARAM_INTERLACED);
851
852
    }

853
    h->param.analyse.i_weighted_pred = x264_clip3( h->param.analyse.i_weighted_pred, X264_WEIGHTP_NONE, X264_WEIGHTP_SMART );
854

855
    if( PARAM_INTERLACED )
856
857
858
859
860
861
862
863
864
865
866
867
868
    {
        if( h->param.analyse.i_me_method >= X264_ME_ESA )
        {
            x264_log( h, X264_LOG_WARNING, "interlace + me=esa is not implemented\n" );
            h->param.analyse.i_me_method = X264_ME_UMH;
        }
        if( h->param.analyse.i_weighted_pred > 0 )
        {
            x264_log( h, X264_LOG_WARNING, "interlace + weightp is not implemented\n" );
            h->param.analyse.i_weighted_pred = X264_WEIGHTP_NONE;
        }
    }

869
    if( !h->param.analyse.i_weighted_pred && h->param.rc.b_mb_tree && h->param.analyse.b_psy )
Dylan Yudaken's avatar
Dylan Yudaken committed
870
871
        h->param.analyse.i_weighted_pred = X264_WEIGHTP_FAKE;

872
    if( h->i_thread_frames > 1 )
Loren Merritt's avatar
Loren Merritt committed
873
874
875
876
877
878
879
880
881
    {
        int r = h->param.analyse.i_mv_range_thread;
        int r2;
        if( r <= 0 )
        {
            // half of the available space is reserved and divided evenly among the threads,
            // the rest is allocated to whichever thread is far enough ahead to use it.
            // reserving more space increases quality for some videos, but costs more time
            // in thread synchronization.
882
            int max_range = (h->param.i_height + X264_THREAD_HEIGHT) / h->i_thread_frames - X264_THREAD_HEIGHT;
Loren Merritt's avatar
Loren Merritt committed
883
884
885
886
887
888
889
890
891
892
893
894
            r = max_range / 2;
        }
        r = X264_MAX( r, h->param.analyse.i_me_range );
        r = X264_MIN( r, h->param.analyse.i_mv_range );
        // round up to use the whole mb row
        r2 = (r & ~15) + ((-X264_THREAD_HEIGHT) & 15);
        if( r2 < r )
            r2 += 16;
        x264_log( h, X264_LOG_DEBUG, "using mv_range_thread = %d\n", r2 );
        h->param.analyse.i_mv_range_thread = r2;
    }

895
896
    if( h->param.rc.f_rate_tolerance < 0 )
        h->param.rc.f_rate_tolerance = 0;
Loren Merritt's avatar
Loren Merritt committed
897
898
899
900
901
    if( h->param.rc.f_qblur < 0 )
        h->param.rc.f_qblur = 0;
    if( h->param.rc.f_complexity_blur < 0 )
        h->param.rc.f_complexity_blur = 0;

902
903
    h->param.i_sps_id &= 31;

904
    if( PARAM_INTERLACED )
905
906
        h->param.b_pic_struct = 1;

907
908
    h->param.i_nal_hrd = x264_clip3( h->param.i_nal_hrd, X264_NAL_HRD_NONE, X264_NAL_HRD_CBR );

909
910
911
912
913
914
915
916
917
918
919
920
921
    if( h->param.i_nal_hrd && !h->param.rc.i_vbv_buffer_size )
    {
        x264_log( h, X264_LOG_WARNING, "NAL HRD parameters require VBV parameters\n" );
        h->param.i_nal_hrd = X264_NAL_HRD_NONE;
    }

    if( h->param.i_nal_hrd == X264_NAL_HRD_CBR &&
       (h->param.rc.i_bitrate != h->param.rc.i_vbv_max_bitrate || !h->param.rc.i_vbv_max_bitrate) )
    {
        x264_log( h, X264_LOG_WARNING, "CBR HRD requires constant bitrate\n" );
        h->param.i_nal_hrd = X264_NAL_HRD_VBR;
    }

922
923
924
    /* ensure the booleans are 0 or 1 so they can be used in math */
#define BOOLIFY(x) h->param.x = !!h->param.x
    BOOLIFY( b_cabac );
925
    BOOLIFY( b_constrained_intra );
926
    BOOLIFY( b_deblocking_filter );
927
    BOOLIFY( b_deterministic );
928
    BOOLIFY( b_sliced_threads );
929
    BOOLIFY( b_interlaced );
Fiona Glaser's avatar
Fiona Glaser committed
930
    BOOLIFY( b_intra_refresh );
931
932
933
934
    BOOLIFY( b_visualize );
    BOOLIFY( b_aud );
    BOOLIFY( b_repeat_headers );
    BOOLIFY( b_annexb );
935
    BOOLIFY( b_vfr_input );
936
937
    BOOLIFY( b_pulldown );
    BOOLIFY( b_tff );
938
    BOOLIFY( b_pic_struct );
Kieran Kunhya's avatar
Kieran Kunhya committed
939
    BOOLIFY( b_fake_interlaced );
940
    BOOLIFY( b_open_gop );
941
    BOOLIFY( b_bluray_compat );
942
    BOOLIFY( analyse.b_transform_8x8 );
943
    BOOLIFY( analyse.b_weighted_bipred );
944
    BOOLIFY( analyse.b_chroma_me );
945
    BOOLIFY( analyse.b_mixed_references );
946
    BOOLIFY( analyse.b_fast_pskip );
947
948
949
950
    BOOLIFY( analyse.b_dct_decimate );
    BOOLIFY( analyse.b_psy );
    BOOLIFY( analyse.b_psnr );
    BOOLIFY( analyse.b_ssim );
951
952
    BOOLIFY( rc.b_stat_write );
    BOOLIFY( rc.b_stat_read );
953
    BOOLIFY( rc.b_mb_tree );
954
955
#undef BOOLIFY

956
957
958
    return 0;
}

959
960
static void mbcmp_init( x264_t *h )
{
961
    int satd = !h->mb.b_lossless && h->param.analyse.i_subpel_refine > 1;
962
963
    memcpy( h->pixf.mbcmp, satd ? h->pixf.satd : h->pixf.sad_aligned, sizeof(h->pixf.mbcmp) );
    memcpy( h->pixf.mbcmp_unaligned, satd ? h->pixf.satd : h->pixf.sad, sizeof(h->pixf.mbcmp_unaligned) );
964
    h->pixf.intra_mbcmp_x3_16x16 = satd ? h->pixf.intra_satd_x3_16x16 : h->pixf.intra_sad_x3_16x16;
Henrik Gramner's avatar
Henrik Gramner committed
965
966
    h->pixf.intra_mbcmp_x3_8x16c = satd ? h->pixf.intra_satd_x3_8x16c : h->pixf.intra_sad_x3_8x16c;
    h->pixf.intra_mbcmp_x3_8x8c  = satd ? h->pixf.intra_satd_x3_8x8c  : h->pixf.intra_sad_x3_8x8c;
Fiona Glaser's avatar
Fiona Glaser committed
967
    h->pixf.intra_mbcmp_x3_8x8 = satd ? h->pixf.intra_sa8d_x3_8x8 : h->pixf.intra_sad_x3_8x8;
Fiona Glaser's avatar
Fiona Glaser committed
968
    h->pixf.intra_mbcmp_x3_4x4 = satd ? h->pixf.intra_satd_x3_4x4 : h->pixf.intra_sad_x3_4x4;
969
970
    h->pixf.intra_mbcmp_x9_4x4 = h->param.b_cpu_independent || h->mb.b_lossless ? NULL
                               : satd ? h->pixf.intra_satd_x9_4x4 : h->pixf.intra_sad_x9_4x4;
971
972
    h->pixf.intra_mbcmp_x9_8x8 = h->param.b_cpu_independent || h->mb.b_lossless ? NULL
                               : satd ? h->pixf.intra_sa8d_x9_8x8 : h->pixf.intra_sad_x9_8x8;
973
974
975
976
    satd &= h->param.analyse.i_me_method == X264_ME_TESA;
    memcpy( h->pixf.fpelcmp, satd ? h->pixf.satd : h->pixf.sad, sizeof(h->pixf.fpelcmp) );
    memcpy( h->pixf.fpelcmp_x3, satd ? h->pixf.satd_x3 : h->pixf.sad_x3, sizeof(h->pixf.fpelcmp_x3) );
    memcpy( h->pixf.fpelcmp_x4, satd ? h->pixf.satd_x4 : h->pixf.sad_x4, sizeof(h->pixf.fpelcmp_x4) );
977
978
}

Henrik Gramner's avatar
Henrik Gramner committed
979
980
981
982
983
984
985
986
static void chroma_dsp_init( x264_t *h )
{
    memcpy( h->luma2chroma_pixel, x264_luma2chroma_pixel[CHROMA_FORMAT], sizeof(h->luma2chroma_pixel) );

    switch( CHROMA_FORMAT )
    {
        case CHROMA_420:
            memcpy( h->predict_chroma, h->predict_8x8c, sizeof(h->predict_chroma) );
987
            h->mc.prefetch_fenc = h->mc.prefetch_fenc_420;
Henrik Gramner's avatar
Henrik Gramner committed
988
989
990
991
992
993
994
995
996
997
            h->loopf.deblock_chroma[0] = h->loopf.deblock_h_chroma_420;
            h->loopf.deblock_chroma_intra[0] = h->loopf.deblock_h_chroma_420_intra;
            h->loopf.deblock_chroma_mbaff = h->loopf.deblock_chroma_420_mbaff;
            h->loopf.deblock_chroma_intra_mbaff = h->loopf.deblock_chroma_420_intra_mbaff;
            h->pixf.intra_mbcmp_x3_chroma = h->pixf.intra_mbcmp_x3_8x8c;
            h->quantf.coeff_last[DCT_CHROMA_DC] = h->quantf.coeff_last4;
            h->quantf.coeff_level_run[DCT_CHROMA_DC] = h->quantf.coeff_level_run4;
            break;
        case CHROMA_422:
            memcpy( h->predict_chroma, h->predict_8x16c, sizeof(h->predict_chroma) );
998
            h->mc.prefetch_fenc = h->mc.prefetch_fenc_422;
Henrik Gramner's avatar
Henrik Gramner committed
999
1000
1001
1002
1003
1004
1005
1006
1007
            h->loopf.deblock_chroma[0] = h->loopf.deblock_h_chroma_422;
            h->loopf.deblock_chroma_intra[0] = h->loopf.deblock_h_chroma_422_intra;
            h->loopf.deblock_chroma_mbaff = h->loopf.deblock_chroma_422_mbaff;
            h->loopf.deblock_chroma_intra_mbaff = h->loopf.deblock_chroma_422_intra_mbaff;
            h->pixf.intra_mbcmp_x3_chroma = h->pixf.intra_mbcmp_x3_8x16c;
            h->quantf.coeff_last[DCT_CHROMA_DC] = h->quantf.coeff_last8;
            h->quantf.coeff_level_run[DCT_CHROMA_DC] = h->quantf.coeff_level_run8;
            break;
        case CHROMA_444:
1008
            h->mc.prefetch_fenc = h->mc.prefetch_fenc_422; /* FIXME: doesn't cover V plane */
Henrik Gramner's avatar
Henrik Gramner committed
1009
1010
1011
1012
1013
1014
            h->loopf.deblock_chroma_mbaff = h->loopf.deblock_luma_mbaff;
            h->loopf.deblock_chroma_intra_mbaff = h->loopf.deblock_luma_intra_mbaff;
            break;
    }
}

1015
1016