/
encoder.c
3957 lines (3545 loc) · 153 KB
/
encoder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* encoder.c: top-level encoder functions
*****************************************************************************
* Copyright (C) 2003-2013 x264 project
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
* Loren Merritt <lorenm@u.washington.edu>
* Jason Garrett-Glaser <darkshikari@gmail.com>
*
* 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
*
* This program is also available under a commercial proprietary license.
* For more information, contact us at licensing@x264.com.
*****************************************************************************/
#include "common/common.h"
#include "set.h"
#include "analyse.h"
#include "ratecontrol.h"
#include "macroblock.h"
#include "me.h"
#if HAVE_VISUALIZE
#include "common/visualize.h"
#endif
#if HAVE_OPENCL
#include "opencl.c"
#endif
//#define DEBUG_MB_TYPE
#define bs_write_ue bs_write_ue_big
static int x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
x264_nal_t **pp_nal, int *pi_nal,
x264_picture_t *pic_out );
/****************************************************************************
*
******************************* x264 libs **********************************
*
****************************************************************************/
static double x264_psnr( double sqe, double size )
{
double mse = sqe / (PIXEL_MAX*PIXEL_MAX * size);
if( mse <= 0.0000000001 ) /* Max 100dB */
return 100;
return -10.0 * log10( mse );
}
static double x264_ssim( double ssim )
{
double inv_ssim = 1 - ssim;
if( inv_ssim <= 0.0000000001 ) /* Max 100dB */
return 100;
return -10.0 * log10( inv_ssim );
}
static int x264_threadpool_wait_all( x264_t *h )
{
for( int i = 0; i < h->param.i_threads; i++ )
if( h->thread[i]->b_thread_active )
{
h->thread[i]->b_thread_active = 0;
if( (intptr_t)x264_threadpool_wait( h->threadpool, h->thread[i] ) < 0 )
return -1;
}
return 0;
}
static void x264_frame_dump( x264_t *h )
{
FILE *f = fopen( h->param.psz_dump_yuv, "r+b" );
if( !f )
return;
/* Wait for the threads to finish deblocking */
if( h->param.b_sliced_threads )
x264_threadpool_wait_all( h );
/* Write the frame in display order */
int frame_size = FRAME_SIZE( h->param.i_height * h->param.i_width * sizeof(pixel) );
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;
int ch = h->param.i_height>>CHROMA_V_SHIFT;
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 );
}
fclose( f );
}
/* Fill "default" values */
static void x264_slice_header_init( x264_t *h, x264_slice_header_t *sh,
x264_sps_t *sps, x264_pps_t *pps,
int i_idr_pic_id, int i_frame, int i_qp )
{
x264_param_t *param = &h->param;
/* First we fill all fields */
sh->sps = sps;
sh->pps = pps;
sh->i_first_mb = 0;
sh->i_last_mb = h->mb.i_mb_count - 1;
sh->i_pps_id = pps->i_id;
sh->i_frame_num = i_frame;
sh->b_mbaff = PARAM_INTERLACED;
sh->b_field_pic = 0; /* no field support for now */
sh->b_bottom_field = 0; /* not yet used */
sh->i_idr_pic_id = i_idr_pic_id;
/* poc stuff, fixed later */
sh->i_poc = 0;
sh->i_delta_poc_bottom = 0;
sh->i_delta_poc[0] = 0;
sh->i_delta_poc[1] = 0;
sh->i_redundant_pic_cnt = 0;
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 )
{
if( h->fref[1][0]->i_poc_l0ref0 == h->fref[0][0]->i_poc )
{
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 );
}
else
{
h->mb.b_direct_auto_write = 0;
sh->b_direct_spatial_mv_pred = 1;
}
}
/* else b_direct_spatial_mv_pred was read from the 2pass statsfile */
sh->b_num_ref_idx_override = 0;
sh->i_num_ref_idx_l0_active = 1;
sh->i_num_ref_idx_l1_active = 1;
sh->b_ref_pic_list_reordering[0] = h->b_ref_reorder[0];
sh->b_ref_pic_list_reordering[1] = h->b_ref_reorder[1];
/* If the ref list isn't in the default order, construct reordering header */
for( int list = 0; list < 2; list++ )
{
if( sh->b_ref_pic_list_reordering[list] )
{
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;
}
}
}
sh->i_cabac_init_idc = param->i_cabac_init_idc;
sh->i_qp = SPEC_QP(i_qp);
sh->i_qp_delta = sh->i_qp - pps->i_pic_init_qp;
sh->b_sp_for_swidth = 0;
sh->i_qs_delta = 0;
int deblock_thresh = i_qp + 2 * X264_MIN(param->i_deblocking_filter_alphac0, param->i_deblocking_filter_beta);
/* If effective qp <= 15, deblocking would have no effect anyway */
if( param->b_deblocking_filter && (h->mb.b_variable_qp || 15 < deblock_thresh ) )
sh->i_disable_deblocking_filter_idc = param->b_sliced_threads ? 2 : 0;
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 )
{
if( sh->b_mbaff )
{
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 );
}
else
bs_write_ue( s, sh->i_first_mb );
bs_write_ue( s, sh->i_type + 5 ); /* same type things */
bs_write_ue( s, sh->i_pps_id );
bs_write( s, sh->sps->i_log2_max_frame_num, sh->i_frame_num & ((1<<sh->sps->i_log2_max_frame_num)-1) );
if( !sh->sps->b_frame_mbs_only )
{
bs_write1( s, sh->b_field_pic );
if( sh->b_field_pic )
bs_write1( s, sh->b_bottom_field );
}
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 )
{
bs_write( s, sh->sps->i_log2_max_poc_lsb, sh->i_poc & ((1<<sh->sps->i_log2_max_poc_lsb)-1) );
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 );
if( sh->i_type == SLICE_TYPE_P || sh->i_type == SLICE_TYPE_B )
{
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 )
{
bs_write1( s, sh->b_ref_pic_list_reordering[0] );
if( sh->b_ref_pic_list_reordering[0] )
{
for( int i = 0; i < sh->i_num_ref_idx_l0_active; i++ )
{
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 );
}
}
if( sh->i_type == SLICE_TYPE_B )
{
bs_write1( s, sh->b_ref_pic_list_reordering[1] );
if( sh->b_ref_pic_list_reordering[1] )
{
for( int i = 0; i < sh->i_num_ref_idx_l1_active; i++ )
{
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 );
}
}
sh->b_weighted_pred = 0;
if( sh->pps->b_weighted_pred && sh->i_type == SLICE_TYPE_P )
{
sh->b_weighted_pred = sh->weight[0][0].weightfn || sh->weight[0][1].weightfn || sh->weight[0][2].weightfn;
/* pred_weight_table() */
bs_write_ue( s, sh->weight[0][0].i_denom );
bs_write_ue( s, sh->weight[0][1].i_denom );
for( int i = 0; i < sh->i_num_ref_idx_l0_active; i++ )
{
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 )
{
for( int j = 1; j < 3; j++ )
{
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 */
}
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
{
bs_write1( s, sh->i_mmco_command_count > 0 ); /* adaptive_ref_pic_marking_mode_flag */
if( sh->i_mmco_command_count > 0 )
{
for( int i = 0; i < sh->i_mmco_command_count; i++ )
{
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 */
}
}
}
if( sh->pps->b_cabac && sh->i_type != SLICE_TYPE_I )
bs_write_ue( s, sh->i_cabac_init_idc );
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 );
}
}
}
/* If we are within a reasonable distance of the end of the memory allocated for the bitstream, */
/* reallocate, adding an arbitrary amount of space. */
static int x264_bitstream_check_buffer( x264_t *h )
{
uint8_t *bs_bak = h->out.p_bitstream;
int max_row_size = (2500 << SLICE_MBAFF) * h->mb.i_mb_width;
if( (h->param.b_cabac && (h->cabac.p_end - h->cabac.p < max_row_size)) ||
(h->out.bs.p_end - h->out.bs.p < max_row_size) )
{
h->out.i_bitstream += max_row_size;
CHECKED_MALLOC( h->out.p_bitstream, h->out.i_bitstream );
h->mc.memcpy_aligned( h->out.p_bitstream, bs_bak, (h->out.i_bitstream - max_row_size) & ~15 );
intptr_t delta = h->out.p_bitstream - bs_bak;
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;
for( int i = 0; i <= h->out.i_nal; i++ )
h->out.nal[i].p_payload += delta;
x264_free( bs_bak );
}
return 0;
fail:
x264_free( bs_bak );
return -1;
}
#if HAVE_THREAD
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
}
static void x264_lookahead_thread_init( x264_t *h )
{
#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
/****************************************************************************
*
****************************************************************************
****************************** External API*********************************
****************************************************************************
*
****************************************************************************/
static int x264_validate_parameters( x264_t *h, int b_open )
{
#if HAVE_MMX
if( b_open )
{
int cpuflags = x264_cpu_detect();
int fail = 0;
#ifdef __SSE__
if( !(cpuflags & X264_CPU_SSE) )
{
x264_log( h, X264_LOG_ERROR, "your cpu does not support SSE1, but x264 was compiled with asm\n");
fail = 1;
}
#else
if( !(cpuflags & X264_CPU_MMX2) )
{
x264_log( h, X264_LOG_ERROR, "your cpu does not support MMXEXT, but x264 was compiled with asm\n");
fail = 1;
}
#endif
if( !fail && !(cpuflags & X264_CPU_CMOV) )
{
x264_log( h, X264_LOG_ERROR, "your cpu does not support CMOV, but x264 was compiled with asm\n");
fail = 1;
}
if( fail )
{
x264_log( h, X264_LOG_ERROR, "to run x264, recompile without asm (configure --disable-asm)\n");
return -1;
}
}
#endif
#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
if( h->param.i_width <= 0 || h->param.i_height <= 0 )
{
x264_log( h, X264_LOG_ERROR, "invalid width x height (%dx%d)\n",
h->param.i_width, h->param.i_height );
return -1;
}
int i_csp = h->param.i_csp & X264_CSP_MASK;
#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
if( i_csp <= X264_CSP_NONE || i_csp >= X264_CSP_MAX )
{
x264_log( h, X264_LOG_ERROR, "invalid CSP (only I420/YV12/NV12/I422/YV16/NV16/I444/YV24/BGR/BGRA/RGB supported)\n" );
return -1;
}
if( i_csp < X264_CSP_I444 && h->param.i_width % 2 )
{
x264_log( h, X264_LOG_ERROR, "width not divisible by 2 (%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 % 4 )
{
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 );
return -1;
}
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;
}
if( h->param.i_threads == X264_THREADS_AUTO )
h->param.i_threads = x264_cpu_num_processors() * (h->param.b_sliced_threads?2:3)/2;
int max_sliced_threads = X264_MAX( 1, (h->param.i_height+15)/16 / 4 );
if( h->param.i_threads > 1 )
{
#if !HAVE_THREAD
x264_log( h, X264_LOG_WARNING, "not compiled with thread support!\n");
h->param.i_threads = 1;
#endif
/* 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 )
h->param.i_threads = X264_MIN( h->param.i_threads, max_sliced_threads );
}
h->param.i_threads = x264_clip3( h->param.i_threads, 1, X264_THREAD_MAX );
if( h->param.i_threads == 1 )
{
h->param.b_sliced_threads = 0;
h->param.i_lookahead_threads = 1;
}
h->i_thread_frames = h->param.b_sliced_threads ? 1 : h->param.i_threads;
if( h->i_thread_frames > 1 )
h->param.nalu_process = NULL;
#ifndef HAVE_OPENCL
if( h->param.b_opencl )
{
x264_log( h, X264_LOG_WARNING, "not compiled with OpenCL support!\n" );
h->param.b_opencl = 0;
}
#endif
#if BIT_DEPTH > 8
if( h->param.b_opencl )
{
x264_log( h, X264_LOG_WARNING, "OpenCL lookahead does not support high bit depth!\n" );
h->param.b_opencl = 0;
}
#endif
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;
}
h->param.i_frame_packing = x264_clip3( h->param.i_frame_packing, -1, 5 );
/* Detect default ffmpeg settings and terminate with an error. */
if( b_open )
{
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" );
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" );
return -1;
}
}
if( h->param.rc.i_rc_method < 0 || h->param.rc.i_rc_method > 2 )
{
x264_log( h, X264_LOG_ERROR, "no ratecontrol method specified\n" );
return -1;
}
h->param.rc.f_rf_constant = x264_clip3f( h->param.rc.f_rf_constant, -QP_BD_OFFSET, 51 );
h->param.rc.f_rf_constant_max = x264_clip3f( h->param.rc.f_rf_constant_max, -QP_BD_OFFSET, 51 );
h->param.rc.i_qp_constant = x264_clip3( h->param.rc.i_qp_constant, 0, QP_MAX );
h->param.analyse.i_subpel_refine = x264_clip3( h->param.analyse.i_subpel_refine, 0, 11 );
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 );
if( h->param.rc.i_rc_method == X264_RC_CRF )
{
h->param.rc.i_qp_constant = h->param.rc.f_rf_constant + QP_BD_OFFSET;
h->param.rc.i_bitrate = 0;
}
if( b_open && (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 )
{
h->mb.b_lossless = 1;
h->param.i_cqm_preset = X264_CQM_FLAT;
h->param.psz_cqm_file = NULL;
h->param.rc.i_rc_method = X264_RC_CQP;
h->param.rc.f_ip_factor = 1;
h->param.rc.f_pb_factor = 1;
h->param.analyse.b_psnr = 0;
h->param.analyse.b_ssim = 0;
h->param.analyse.i_chroma_qp_offset = 0;
h->param.analyse.i_trellis = 0;
h->param.analyse.b_fast_pskip = 0;
h->param.analyse.i_noise_reduction = 0;
h->param.analyse.b_psy = 0;
h->param.i_bframe = 0;
/* 8x8dct is not useful without RD in CAVLC lossless */
if( !h->param.b_cabac && h->param.analyse.i_subpel_refine < 6 )
h->param.analyse.b_transform_8x8 = 0;
}
if( h->param.rc.i_rc_method == X264_RC_CQP )
{
float qp_p = h->param.rc.i_qp_constant;
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 );
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 );
h->param.rc.i_aq_mode = 0;
h->param.rc.b_mb_tree = 0;
h->param.rc.i_bitrate = 0;
}
h->param.rc.i_qp_max = x264_clip3( h->param.rc.i_qp_max, 0, QP_MAX );
h->param.rc.i_qp_min = x264_clip3( h->param.rc.i_qp_min, 0, h->param.rc.i_qp_max );
h->param.rc.i_qp_step = x264_clip3( h->param.rc.i_qp_step, 2, QP_MAX );
h->param.rc.i_bitrate = x264_clip3( h->param.rc.i_bitrate, 0, 2000000 );
if( h->param.rc.i_rc_method == X264_RC_ABR && !h->param.rc.i_bitrate )
{
x264_log( h, X264_LOG_ERROR, "bitrate not specified\n" );
return -1;
}
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 );
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;
}
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 );
int max_slices = (h->param.i_height+((16<<PARAM_INTERLACED)-1))/(16<<PARAM_INTERLACED);
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;
}
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 );
/* Don't use I-frames, because Blu-ray treats them the same as IDR. */
h->param.i_keyint_min = 1;
/* 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 );
if( h->param.b_fake_interlaced )
h->param.b_pic_struct = 1;
}
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 );
if( h->param.i_scenecut_threshold < 0 )
h->param.i_scenecut_threshold = 0;
h->param.analyse.i_direct_mv_pred = x264_clip3( h->param.analyse.i_direct_mv_pred, X264_DIRECT_PRED_NONE, X264_DIRECT_PRED_AUTO );
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;
}
h->param.i_bframe = x264_clip3( h->param.i_bframe, 0, X264_MIN( X264_BFRAME_MAX, h->param.i_keyint_max-1 ) );
h->param.i_bframe_bias = x264_clip3( h->param.i_bframe_bias, -90, 100 );
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 );
h->param.i_bframe_adaptive = x264_clip3( h->param.i_bframe_adaptive, X264_B_ADAPT_NONE, X264_B_ADAPT_TRELLIS );
if( !h->param.i_bframe )
{
h->param.i_bframe_adaptive = X264_B_ADAPT_NONE;
h->param.analyse.i_direct_mv_pred = 0;
h->param.analyse.b_weighted_bipred = 0;
h->param.b_open_gop = 0;
}
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;
}
if( h->param.b_intra_refresh && (h->param.i_frame_reference > 1 || h->param.i_dpb_size > 1) )
{
x264_log( h, X264_LOG_WARNING, "ref > 1 + intra-refresh is not supported\n" );
h->param.i_frame_reference = 1;
h->param.i_dpb_size = 1;
}
if( h->param.b_intra_refresh && h->param.b_open_gop )
{
x264_log( h, X264_LOG_WARNING, "intra-refresh is not compatible with open-gop\n" );
h->param.b_open_gop = 0;
}
if( !h->param.i_fps_num || !h->param.i_fps_den )
{
h->param.i_fps_num = 25;
h->param.i_fps_den = 1;
}
float fps = (float) h->param.i_fps_num / h->param.i_fps_den;
if( h->param.i_keyint_min == X264_KEYINT_MIN_AUTO )
h->param.i_keyint_min = X264_MIN( h->param.i_keyint_max / 10, fps );
h->param.i_keyint_min = x264_clip3( h->param.i_keyint_min, 1, h->param.i_keyint_max/2+1 );
h->param.rc.i_lookahead = x264_clip3( h->param.rc.i_lookahead, 0, X264_LOOKAHEAD_MAX );
{
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 ) );
}
if( !h->param.i_timebase_num || !h->param.i_timebase_den || !(h->param.b_vfr_input || h->param.b_pulldown) )
{
h->param.i_timebase_num = h->param.i_fps_den;
h->param.i_timebase_den = h->param.i_fps_num;
}
h->param.rc.f_qcompress = x264_clip3f( h->param.rc.f_qcompress, 0.0, 1.0 );
if( h->param.i_keyint_max == 1 || h->param.rc.f_qcompress == 1 )
h->param.rc.b_mb_tree = 0;
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 )
{
x264_log( h, X264_LOG_WARNING, "lookaheadless mb-tree requires intra refresh or infinite keyint\n" );
h->param.rc.b_mb_tree = 0;
}
if( b_open && h->param.rc.b_stat_read )
h->param.rc.i_lookahead = 0;
#if HAVE_THREAD
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 );
if( h->param.rc.b_stat_read || h->i_thread_frames == 1 )
h->param.i_sync_lookahead = 0;
#else
h->param.i_sync_lookahead = 0;
#endif
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 );
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 );
h->param.i_cabac_init_idc = x264_clip3( h->param.i_cabac_init_idc, 0, 2 );
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;
if( h->param.analyse.i_me_method < X264_ME_DIA ||
h->param.analyse.i_me_method > X264_ME_TESA )
h->param.analyse.i_me_method = X264_ME_HEX;
h->param.analyse.i_me_range = x264_clip3( h->param.analyse.i_me_range, 4, 1024 );
if( h->param.analyse.i_me_range > 16 && h->param.analyse.i_me_method <= X264_ME_HEX )
h->param.analyse.i_me_range = 16;
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;
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;
if( !(h->param.analyse.inter & X264_ANALYSE_PSUB16x16) )
h->param.analyse.inter &= ~X264_ANALYSE_PSUB8x8;
if( !h->param.analyse.b_transform_8x8 )
{
h->param.analyse.inter &= ~X264_ANALYSE_I8x8;
h->param.analyse.intra &= ~X264_ANALYSE_I8x8;
}
h->param.analyse.i_trellis = x264_clip3( h->param.analyse.i_trellis, 0, 2 );
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;
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. */
if( b_open && (h->param.analyse.b_psnr || h->param.analyse.b_ssim) )
{
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 );
}
if( !h->param.analyse.b_psy )
{
h->param.analyse.f_psy_rd = 0;
h->param.analyse.f_psy_trellis = 0;
}
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 );
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;
h->param.analyse.i_chroma_qp_offset = x264_clip3(h->param.analyse.i_chroma_qp_offset, -32, 32);
/* In 4:4:4 mode, chroma gets twice as much resolution, so we can halve its quality. */
if( b_open && i_csp >= X264_CSP_I444 && i_csp < X264_CSP_BGR && h->param.analyse.b_psy )
h->param.analyse.i_chroma_qp_offset += 6;
/* 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 */
if( b_open && h->mb.i_psy_rd )
h->param.analyse.i_chroma_qp_offset -= h->param.analyse.f_psy_rd < 0.25 ? 1 : 2;
/* Psy trellis has a similar effect. */
if( b_open && h->mb.i_psy_trellis )
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);
/* 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;
}
h->param.analyse.i_noise_reduction = x264_clip3( h->param.analyse.i_noise_reduction, 0, 1<<16 );
if( h->param.analyse.i_subpel_refine >= 10 && (h->param.analyse.i_trellis != 2 || !h->param.rc.i_aq_mode) )
h->param.analyse.i_subpel_refine = 9;
{
const x264_level_t *l = x264_levels;
if( h->param.i_level_idc < 0 )
{
int maxrate_bak = h->param.rc.i_vbv_max_bitrate;
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++ );
h->param.rc.i_vbv_max_bitrate = maxrate_bak;
}
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;
}
}
if( h->param.analyse.i_mv_range <= 0 )
h->param.analyse.i_mv_range = l->mv_range >> PARAM_INTERLACED;
else
h->param.analyse.i_mv_range = x264_clip3(h->param.analyse.i_mv_range, 32, 512 >> PARAM_INTERLACED);
}
h->param.analyse.i_weighted_pred = x264_clip3( h->param.analyse.i_weighted_pred, X264_WEIGHTP_NONE, X264_WEIGHTP_SMART );
if( h->param.i_lookahead_threads == X264_THREADS_AUTO )
{
if( h->param.b_sliced_threads )
h->param.i_lookahead_threads = h->param.i_threads;
else
{
/* If we're using much slower lookahead settings than encoding settings, it helps a lot to use
* more lookahead threads. This typically happens in the first pass of a two-pass encode, so
* try to guess at this sort of case.
*
* Tuned by a little bit of real encoding with the various presets. */
int badapt = h->param.i_bframe_adaptive == X264_B_ADAPT_TRELLIS;
int subme = X264_MIN( h->param.analyse.i_subpel_refine / 3, 3 ) + (h->param.analyse.i_subpel_refine > 1);
int bframes = X264_MIN( (h->param.i_bframe - 1) / 3, 3 );
/* [b-adapt 0/1 vs 2][quantized subme][quantized bframes] */
static const uint8_t lookahead_thread_div[2][5][4] =
{{{6,6,6,6}, {3,3,3,3}, {4,4,4,4}, {6,6,6,6}, {12,12,12,12}},
{{3,2,1,1}, {2,1,1,1}, {4,3,2,1}, {6,4,3,2}, {12, 9, 6, 4}}};
h->param.i_lookahead_threads = h->param.i_threads / lookahead_thread_div[badapt][subme][bframes];
/* Since too many lookahead threads significantly degrades lookahead accuracy, limit auto
* lookahead threads to about 8 macroblock rows high each at worst. This number is chosen
* pretty much arbitrarily. */
h->param.i_lookahead_threads = X264_MIN( h->param.i_lookahead_threads, h->param.i_height / 128 );
}
}
h->param.i_lookahead_threads = x264_clip3( h->param.i_lookahead_threads, 1, X264_MIN( max_sliced_threads, X264_LOOKAHEAD_THREAD_MAX ) );
if( PARAM_INTERLACED )
{
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;
}
}
if( !h->param.analyse.i_weighted_pred && h->param.rc.b_mb_tree && h->param.analyse.b_psy )
h->param.analyse.i_weighted_pred = X264_WEIGHTP_FAKE;
if( h->i_thread_frames > 1 )
{
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.
int max_range = (h->param.i_height + X264_THREAD_HEIGHT) / h->i_thread_frames - X264_THREAD_HEIGHT;
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;
}
if( h->param.rc.f_rate_tolerance < 0 )
h->param.rc.f_rate_tolerance = 0;