1 /*****************************************************************************
2 * encoder.c: top-level encoder functions
3 *****************************************************************************
4 * Copyright (C) 2003-2017 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
8 * Fiona Glaser <fiona@x264.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
24 * This program is also available under a commercial proprietary license.
25 * For more information, contact us at licensing@x264.com.
26 *****************************************************************************/
28 #include "common/common.h"
32 #include "ratecontrol.h"
33 #include "macroblock.h"
35 #if HAVE_INTEL_DISPATCHER
36 #include "extras/intel_dispatcher.h"
39 //#define DEBUG_MB_TYPE
41 #define bs_write_ue bs_write_ue_big
43 static int x264_encoder_frame_end( x264_t
*h
, x264_t
*thread_current
,
44 x264_nal_t
**pp_nal
, int *pi_nal
,
45 x264_picture_t
*pic_out
);
47 /****************************************************************************
49 ******************************* x264 libs **********************************
51 ****************************************************************************/
52 static double x264_psnr( double sqe
, double size
)
54 double mse
= sqe
/ (PIXEL_MAX
*PIXEL_MAX
* size
);
55 if( mse
<= 0.0000000001 ) /* Max 100dB */
58 return -10.0 * log10( mse
);
61 static double x264_ssim( double ssim
)
63 double inv_ssim
= 1 - ssim
;
64 if( inv_ssim
<= 0.0000000001 ) /* Max 100dB */
67 return -10.0 * log10( inv_ssim
);
70 static int x264_threadpool_wait_all( x264_t
*h
)
72 for( int i
= 0; i
< h
->param
.i_threads
; i
++ )
73 if( h
->thread
[i
]->b_thread_active
)
75 h
->thread
[i
]->b_thread_active
= 0;
76 if( (intptr_t)x264_threadpool_wait( h
->threadpool
, h
->thread
[i
] ) < 0 )
82 static void x264_frame_dump( x264_t
*h
)
84 FILE *f
= x264_fopen( h
->param
.psz_dump_yuv
, "r+b" );
88 /* Wait for the threads to finish deblocking */
89 if( h
->param
.b_sliced_threads
)
90 x264_threadpool_wait_all( h
);
92 /* Write the frame in display order */
93 int frame_size
= FRAME_SIZE( h
->param
.i_height
* h
->param
.i_width
* sizeof(pixel
) );
94 if( !fseek( f
, (int64_t)h
->fdec
->i_frame
* frame_size
, SEEK_SET
) )
96 for( int p
= 0; p
< (CHROMA444
? 3 : 1); p
++ )
97 for( int y
= 0; y
< h
->param
.i_height
; y
++ )
98 fwrite( &h
->fdec
->plane
[p
][y
*h
->fdec
->i_stride
[p
]], sizeof(pixel
), h
->param
.i_width
, f
);
101 int cw
= h
->param
.i_width
>>1;
102 int ch
= h
->param
.i_height
>>CHROMA_V_SHIFT
;
103 pixel
*planeu
= x264_malloc( 2 * (cw
*ch
*sizeof(pixel
) + 32) );
106 pixel
*planev
= planeu
+ cw
*ch
+ 32/sizeof(pixel
);
107 h
->mc
.plane_copy_deinterleave( planeu
, cw
, planev
, cw
, h
->fdec
->plane
[1], h
->fdec
->i_stride
[1], cw
, ch
);
108 fwrite( planeu
, 1, cw
*ch
*sizeof(pixel
), f
);
109 fwrite( planev
, 1, cw
*ch
*sizeof(pixel
), f
);
117 /* Fill "default" values */
118 static void x264_slice_header_init( x264_t
*h
, x264_slice_header_t
*sh
,
119 x264_sps_t
*sps
, x264_pps_t
*pps
,
120 int i_idr_pic_id
, int i_frame
, int i_qp
)
122 x264_param_t
*param
= &h
->param
;
124 /* First we fill all fields */
129 sh
->i_last_mb
= h
->mb
.i_mb_count
- 1;
130 sh
->i_pps_id
= pps
->i_id
;
132 sh
->i_frame_num
= i_frame
;
134 sh
->b_mbaff
= PARAM_INTERLACED
;
135 sh
->b_field_pic
= 0; /* no field support for now */
136 sh
->b_bottom_field
= 0; /* not yet used */
138 sh
->i_idr_pic_id
= i_idr_pic_id
;
140 /* poc stuff, fixed later */
142 sh
->i_delta_poc_bottom
= 0;
143 sh
->i_delta_poc
[0] = 0;
144 sh
->i_delta_poc
[1] = 0;
146 sh
->i_redundant_pic_cnt
= 0;
148 h
->mb
.b_direct_auto_write
= h
->param
.analyse
.i_direct_mv_pred
== X264_DIRECT_PRED_AUTO
150 && ( h
->param
.rc
.b_stat_write
|| !h
->param
.rc
.b_stat_read
);
152 if( !h
->mb
.b_direct_auto_read
&& sh
->i_type
== SLICE_TYPE_B
)
154 if( h
->fref
[1][0]->i_poc_l0ref0
== h
->fref
[0][0]->i_poc
)
156 if( h
->mb
.b_direct_auto_write
)
157 sh
->b_direct_spatial_mv_pred
= ( h
->stat
.i_direct_score
[1] > h
->stat
.i_direct_score
[0] );
159 sh
->b_direct_spatial_mv_pred
= ( param
->analyse
.i_direct_mv_pred
== X264_DIRECT_PRED_SPATIAL
);
163 h
->mb
.b_direct_auto_write
= 0;
164 sh
->b_direct_spatial_mv_pred
= 1;
167 /* else b_direct_spatial_mv_pred was read from the 2pass statsfile */
169 sh
->b_num_ref_idx_override
= 0;
170 sh
->i_num_ref_idx_l0_active
= 1;
171 sh
->i_num_ref_idx_l1_active
= 1;
173 sh
->b_ref_pic_list_reordering
[0] = h
->b_ref_reorder
[0];
174 sh
->b_ref_pic_list_reordering
[1] = h
->b_ref_reorder
[1];
176 /* If the ref list isn't in the default order, construct reordering header */
177 for( int list
= 0; list
< 2; list
++ )
179 if( sh
->b_ref_pic_list_reordering
[list
] )
181 int pred_frame_num
= i_frame
;
182 for( int i
= 0; i
< h
->i_ref
[list
]; i
++ )
184 int diff
= h
->fref
[list
][i
]->i_frame_num
- pred_frame_num
;
185 sh
->ref_pic_list_order
[list
][i
].idc
= ( diff
> 0 );
186 sh
->ref_pic_list_order
[list
][i
].arg
= (abs(diff
) - 1) & ((1 << sps
->i_log2_max_frame_num
) - 1);
187 pred_frame_num
= h
->fref
[list
][i
]->i_frame_num
;
192 sh
->i_cabac_init_idc
= param
->i_cabac_init_idc
;
194 sh
->i_qp
= SPEC_QP(i_qp
);
195 sh
->i_qp_delta
= sh
->i_qp
- pps
->i_pic_init_qp
;
196 sh
->b_sp_for_swidth
= 0;
199 int deblock_thresh
= i_qp
+ 2 * X264_MIN(param
->i_deblocking_filter_alphac0
, param
->i_deblocking_filter_beta
);
200 /* If effective qp <= 15, deblocking would have no effect anyway */
201 if( param
->b_deblocking_filter
&& (h
->mb
.b_variable_qp
|| 15 < deblock_thresh
) )
202 sh
->i_disable_deblocking_filter_idc
= param
->b_sliced_threads
? 2 : 0;
204 sh
->i_disable_deblocking_filter_idc
= 1;
205 sh
->i_alpha_c0_offset
= param
->i_deblocking_filter_alphac0
<< 1;
206 sh
->i_beta_offset
= param
->i_deblocking_filter_beta
<< 1;
209 static void x264_slice_header_write( bs_t
*s
, x264_slice_header_t
*sh
, int i_nal_ref_idc
)
213 int first_x
= sh
->i_first_mb
% sh
->sps
->i_mb_width
;
214 int first_y
= sh
->i_first_mb
/ sh
->sps
->i_mb_width
;
215 assert( (first_y
&1) == 0 );
216 bs_write_ue( s
, (2*first_x
+ sh
->sps
->i_mb_width
*(first_y
&~1) + (first_y
&1)) >> 1 );
219 bs_write_ue( s
, sh
->i_first_mb
);
221 bs_write_ue( s
, sh
->i_type
+ 5 ); /* same type things */
222 bs_write_ue( s
, sh
->i_pps_id
);
223 bs_write( s
, sh
->sps
->i_log2_max_frame_num
, sh
->i_frame_num
& ((1<<sh
->sps
->i_log2_max_frame_num
)-1) );
225 if( !sh
->sps
->b_frame_mbs_only
)
227 bs_write1( s
, sh
->b_field_pic
);
228 if( sh
->b_field_pic
)
229 bs_write1( s
, sh
->b_bottom_field
);
232 if( sh
->i_idr_pic_id
>= 0 ) /* NAL IDR */
233 bs_write_ue( s
, sh
->i_idr_pic_id
);
235 if( sh
->sps
->i_poc_type
== 0 )
237 bs_write( s
, sh
->sps
->i_log2_max_poc_lsb
, sh
->i_poc
& ((1<<sh
->sps
->i_log2_max_poc_lsb
)-1) );
238 if( sh
->pps
->b_pic_order
&& !sh
->b_field_pic
)
239 bs_write_se( s
, sh
->i_delta_poc_bottom
);
242 if( sh
->pps
->b_redundant_pic_cnt
)
243 bs_write_ue( s
, sh
->i_redundant_pic_cnt
);
245 if( sh
->i_type
== SLICE_TYPE_B
)
246 bs_write1( s
, sh
->b_direct_spatial_mv_pred
);
248 if( sh
->i_type
== SLICE_TYPE_P
|| sh
->i_type
== SLICE_TYPE_B
)
250 bs_write1( s
, sh
->b_num_ref_idx_override
);
251 if( sh
->b_num_ref_idx_override
)
253 bs_write_ue( s
, sh
->i_num_ref_idx_l0_active
- 1 );
254 if( sh
->i_type
== SLICE_TYPE_B
)
255 bs_write_ue( s
, sh
->i_num_ref_idx_l1_active
- 1 );
259 /* ref pic list reordering */
260 if( sh
->i_type
!= SLICE_TYPE_I
)
262 bs_write1( s
, sh
->b_ref_pic_list_reordering
[0] );
263 if( sh
->b_ref_pic_list_reordering
[0] )
265 for( int i
= 0; i
< sh
->i_num_ref_idx_l0_active
; i
++ )
267 bs_write_ue( s
, sh
->ref_pic_list_order
[0][i
].idc
);
268 bs_write_ue( s
, sh
->ref_pic_list_order
[0][i
].arg
);
273 if( sh
->i_type
== SLICE_TYPE_B
)
275 bs_write1( s
, sh
->b_ref_pic_list_reordering
[1] );
276 if( sh
->b_ref_pic_list_reordering
[1] )
278 for( int i
= 0; i
< sh
->i_num_ref_idx_l1_active
; i
++ )
280 bs_write_ue( s
, sh
->ref_pic_list_order
[1][i
].idc
);
281 bs_write_ue( s
, sh
->ref_pic_list_order
[1][i
].arg
);
287 sh
->b_weighted_pred
= 0;
288 if( sh
->pps
->b_weighted_pred
&& sh
->i_type
== SLICE_TYPE_P
)
290 /* pred_weight_table() */
291 bs_write_ue( s
, sh
->weight
[0][0].i_denom
);
292 bs_write_ue( s
, sh
->weight
[0][1].i_denom
);
293 for( int i
= 0; i
< sh
->i_num_ref_idx_l0_active
; i
++ )
295 int luma_weight_l0_flag
= !!sh
->weight
[i
][0].weightfn
&&
296 !(sh
->weight
[i
][0].i_scale
== 1<<sh
->weight
[i
][0].i_denom
&& sh
->weight
[i
][0].i_offset
== 0);
297 int chroma_weight_l0_flag
= !!sh
->weight
[i
][1].weightfn
|| !!sh
->weight
[i
][2].weightfn
;
299 sh
->b_weighted_pred
|= luma_weight_l0_flag
|| chroma_weight_l0_flag
;
301 bs_write1( s
, luma_weight_l0_flag
);
302 if( luma_weight_l0_flag
)
304 bs_write_se( s
, sh
->weight
[i
][0].i_scale
);
305 bs_write_se( s
, sh
->weight
[i
][0].i_offset
);
307 bs_write1( s
, chroma_weight_l0_flag
);
308 if( chroma_weight_l0_flag
)
310 for( int j
= 1; j
< 3; j
++ )
312 bs_write_se( s
, sh
->weight
[i
][j
].i_scale
);
313 bs_write_se( s
, sh
->weight
[i
][j
].i_offset
);
318 else if( sh
->pps
->b_weighted_bipred
== 1 && sh
->i_type
== SLICE_TYPE_B
)
323 if( i_nal_ref_idc
!= 0 )
325 if( sh
->i_idr_pic_id
>= 0 )
327 bs_write1( s
, 0 ); /* no output of prior pics flag */
328 bs_write1( s
, 0 ); /* long term reference flag */
332 bs_write1( s
, sh
->i_mmco_command_count
> 0 ); /* adaptive_ref_pic_marking_mode_flag */
333 if( sh
->i_mmco_command_count
> 0 )
335 for( int i
= 0; i
< sh
->i_mmco_command_count
; i
++ )
337 bs_write_ue( s
, 1 ); /* mark short term ref as unused */
338 bs_write_ue( s
, sh
->mmco
[i
].i_difference_of_pic_nums
- 1 );
340 bs_write_ue( s
, 0 ); /* end command list */
345 if( sh
->pps
->b_cabac
&& sh
->i_type
!= SLICE_TYPE_I
)
346 bs_write_ue( s
, sh
->i_cabac_init_idc
);
348 bs_write_se( s
, sh
->i_qp_delta
); /* slice qp delta */
350 if( sh
->pps
->b_deblocking_filter_control
)
352 bs_write_ue( s
, sh
->i_disable_deblocking_filter_idc
);
353 if( sh
->i_disable_deblocking_filter_idc
!= 1 )
355 bs_write_se( s
, sh
->i_alpha_c0_offset
>> 1 );
356 bs_write_se( s
, sh
->i_beta_offset
>> 1 );
361 /* If we are within a reasonable distance of the end of the memory allocated for the bitstream, */
362 /* reallocate, adding an arbitrary amount of space. */
363 static int x264_bitstream_check_buffer_internal( x264_t
*h
, int size
, int b_cabac
, int i_nal
)
365 if( (b_cabac
&& (h
->cabac
.p_end
- h
->cabac
.p
< size
)) ||
366 (h
->out
.bs
.p_end
- h
->out
.bs
.p
< size
) )
368 int buf_size
= h
->out
.i_bitstream
+ size
;
369 uint8_t *buf
= x264_malloc( buf_size
);
372 int aligned_size
= h
->out
.i_bitstream
& ~15;
373 h
->mc
.memcpy_aligned( buf
, h
->out
.p_bitstream
, aligned_size
);
374 memcpy( buf
+ aligned_size
, h
->out
.p_bitstream
+ aligned_size
, h
->out
.i_bitstream
- aligned_size
);
376 intptr_t delta
= buf
- h
->out
.p_bitstream
;
378 h
->out
.bs
.p_start
+= delta
;
379 h
->out
.bs
.p
+= delta
;
380 h
->out
.bs
.p_end
= buf
+ buf_size
;
382 h
->cabac
.p_start
+= delta
;
384 h
->cabac
.p_end
= buf
+ buf_size
;
386 for( int i
= 0; i
<= i_nal
; i
++ )
387 h
->out
.nal
[i
].p_payload
+= delta
;
389 x264_free( h
->out
.p_bitstream
);
390 h
->out
.p_bitstream
= buf
;
391 h
->out
.i_bitstream
= buf_size
;
396 static int x264_bitstream_check_buffer( x264_t
*h
)
398 int max_row_size
= (2500 << SLICE_MBAFF
) * h
->mb
.i_mb_width
;
399 return x264_bitstream_check_buffer_internal( h
, max_row_size
, h
->param
.b_cabac
, h
->out
.i_nal
);
402 static int x264_bitstream_check_buffer_filler( x264_t
*h
, int filler
)
404 filler
+= 32; // add padding for safety
405 return x264_bitstream_check_buffer_internal( h
, filler
, 0, -1 );
409 static void x264_encoder_thread_init( x264_t
*h
)
411 if( h
->param
.i_sync_lookahead
)
412 x264_lower_thread_priority( 10 );
416 /****************************************************************************
418 ****************************************************************************
419 ****************************** External API*********************************
420 ****************************************************************************
422 ****************************************************************************/
424 static int x264_validate_parameters( x264_t
*h
, int b_open
)
426 if( !h
->param
.pf_log
)
428 x264_log( NULL
, X264_LOG_ERROR
, "pf_log not set! did you forget to call x264_param_default?\n" );
435 int cpuflags
= x264_cpu_detect();
438 if( !(cpuflags
& X264_CPU_SSE
) )
440 x264_log( h
, X264_LOG_ERROR
, "your cpu does not support SSE1, but x264 was compiled with asm\n");
444 if( !(cpuflags
& X264_CPU_MMX2
) )
446 x264_log( h
, X264_LOG_ERROR
, "your cpu does not support MMXEXT, but x264 was compiled with asm\n");
452 x264_log( h
, X264_LOG_ERROR
, "to run x264, recompile without asm (configure --disable-asm)\n");
459 h
->param
.b_interlaced
= !!PARAM_INTERLACED
;
461 if( h
->param
.b_interlaced
)
463 x264_log( h
, X264_LOG_ERROR
, "not compiled with interlaced support\n" );
468 if( h
->param
.i_width
<= 0 || h
->param
.i_height
<= 0 )
470 x264_log( h
, X264_LOG_ERROR
, "invalid width x height (%dx%d)\n",
471 h
->param
.i_width
, h
->param
.i_height
);
475 int i_csp
= h
->param
.i_csp
& X264_CSP_MASK
;
476 #if X264_CHROMA_FORMAT
477 if( CHROMA_FORMAT
!= CHROMA_420
&& i_csp
>= X264_CSP_I420
&& i_csp
< X264_CSP_I422
)
479 x264_log( h
, X264_LOG_ERROR
, "not compiled with 4:2:0 support\n" );
482 else if( CHROMA_FORMAT
!= CHROMA_422
&& i_csp
>= X264_CSP_I422
&& i_csp
< X264_CSP_I444
)
484 x264_log( h
, X264_LOG_ERROR
, "not compiled with 4:2:2 support\n" );
487 else if( CHROMA_FORMAT
!= CHROMA_444
&& i_csp
>= X264_CSP_I444
&& i_csp
<= X264_CSP_RGB
)
489 x264_log( h
, X264_LOG_ERROR
, "not compiled with 4:4:4 support\n" );
493 if( i_csp
<= X264_CSP_NONE
|| i_csp
>= X264_CSP_MAX
)
495 x264_log( h
, X264_LOG_ERROR
, "invalid CSP (only I420/YV12/NV12/NV21/I422/YV16/NV16/YUYV/UYVY/"
496 "I444/YV24/BGR/BGRA/RGB supported)\n" );
500 int w_mod
= i_csp
< X264_CSP_I444
? 2 : 1;
501 int h_mod
= (i_csp
< X264_CSP_I422
? 2 : 1) << PARAM_INTERLACED
;
502 if( h
->param
.i_width
% w_mod
)
504 x264_log( h
, X264_LOG_ERROR
, "width not divisible by %d (%dx%d)\n",
505 w_mod
, h
->param
.i_width
, h
->param
.i_height
);
508 if( h
->param
.i_height
% h_mod
)
510 x264_log( h
, X264_LOG_ERROR
, "height not divisible by %d (%dx%d)\n",
511 h_mod
, h
->param
.i_width
, h
->param
.i_height
);
515 if( h
->param
.crop_rect
.i_left
>= h
->param
.i_width
||
516 h
->param
.crop_rect
.i_right
>= h
->param
.i_width
||
517 h
->param
.crop_rect
.i_top
>= h
->param
.i_height
||
518 h
->param
.crop_rect
.i_bottom
>= h
->param
.i_height
||
519 h
->param
.crop_rect
.i_left
+ h
->param
.crop_rect
.i_right
>= h
->param
.i_width
||
520 h
->param
.crop_rect
.i_top
+ h
->param
.crop_rect
.i_bottom
>= h
->param
.i_height
)
522 x264_log( h
, X264_LOG_ERROR
, "invalid crop-rect %u,%u,%u,%u\n", h
->param
.crop_rect
.i_left
,
523 h
->param
.crop_rect
.i_top
, h
->param
.crop_rect
.i_right
, h
->param
.crop_rect
.i_bottom
);
526 if( h
->param
.crop_rect
.i_left
% w_mod
|| h
->param
.crop_rect
.i_right
% w_mod
||
527 h
->param
.crop_rect
.i_top
% h_mod
|| h
->param
.crop_rect
.i_bottom
% h_mod
)
529 x264_log( h
, X264_LOG_ERROR
, "crop-rect %u,%u,%u,%u not divisible by %dx%d\n", h
->param
.crop_rect
.i_left
,
530 h
->param
.crop_rect
.i_top
, h
->param
.crop_rect
.i_right
, h
->param
.crop_rect
.i_bottom
, w_mod
, h_mod
);
534 if( h
->param
.vui
.i_sar_width
<= 0 || h
->param
.vui
.i_sar_height
<= 0 )
536 h
->param
.vui
.i_sar_width
= 0;
537 h
->param
.vui
.i_sar_height
= 0;
540 if( h
->param
.i_threads
== X264_THREADS_AUTO
)
542 h
->param
.i_threads
= x264_cpu_num_processors() * (h
->param
.b_sliced_threads
?2:3)/2;
543 /* Avoid too many threads as they don't improve performance and
544 * complicate VBV. Capped at an arbitrary 2 rows per thread. */
545 int max_threads
= X264_MAX( 1, (h
->param
.i_height
+15)/16 / 2 );
546 h
->param
.i_threads
= X264_MIN( h
->param
.i_threads
, max_threads
);
548 int max_sliced_threads
= X264_MAX( 1, (h
->param
.i_height
+15)/16 / 4 );
549 if( h
->param
.i_threads
> 1 )
552 x264_log( h
, X264_LOG_WARNING
, "not compiled with thread support!\n");
553 h
->param
.i_threads
= 1;
555 /* Avoid absurdly small thread slices as they can reduce performance
556 * and VBV compliance. Capped at an arbitrary 4 rows per thread. */
557 if( h
->param
.b_sliced_threads
)
558 h
->param
.i_threads
= X264_MIN( h
->param
.i_threads
, max_sliced_threads
);
560 h
->param
.i_threads
= x264_clip3( h
->param
.i_threads
, 1, X264_THREAD_MAX
);
561 if( h
->param
.i_threads
== 1 )
563 h
->param
.b_sliced_threads
= 0;
564 h
->param
.i_lookahead_threads
= 1;
566 h
->i_thread_frames
= h
->param
.b_sliced_threads
? 1 : h
->param
.i_threads
;
567 if( h
->i_thread_frames
> 1 )
568 h
->param
.nalu_process
= NULL
;
570 if( h
->param
.b_opencl
)
573 x264_log( h
, X264_LOG_WARNING
, "OpenCL: not compiled with OpenCL support, disabling\n" );
574 h
->param
.b_opencl
= 0;
576 x264_log( h
, X264_LOG_WARNING
, "OpenCL lookahead does not support high bit depth, disabling opencl\n" );
577 h
->param
.b_opencl
= 0;
579 if( h
->param
.i_width
< 32 || h
->param
.i_height
< 32 )
581 x264_log( h
, X264_LOG_WARNING
, "OpenCL: frame size is too small, disabling opencl\n" );
582 h
->param
.b_opencl
= 0;
585 if( h
->param
.opencl_device_id
&& h
->param
.i_opencl_device
)
587 x264_log( h
, X264_LOG_WARNING
, "OpenCL: device id and device skip count configured; dropping skip\n" );
588 h
->param
.i_opencl_device
= 0;
592 if( !h
->param
.i_fps_num
|| !h
->param
.i_fps_den
)
594 h
->param
.i_fps_num
= 25;
595 h
->param
.i_fps_den
= 1;
597 float fps
= (float) h
->param
.i_fps_num
/ h
->param
.i_fps_den
;
598 if( h
->param
.i_keyint_max
== X264_KEYINT_MAX_AUTO
)
600 h
->param
.i_keyint_max
= (int)ceilf( fps
) * 10;
601 x264_log( h
, X264_LOG_INFO
, "keyint is automatically set to %d.\n", h
->param
.i_keyint_max
);
603 h
->param
.i_keyint_max
= x264_clip3( h
->param
.i_keyint_max
, 1, X264_KEYINT_MAX_INFINITE
);
604 if( h
->param
.i_keyint_max
== 1 )
606 h
->param
.b_intra_refresh
= 0;
607 h
->param
.analyse
.i_weighted_pred
= 0;
608 h
->param
.i_frame_reference
= 1;
609 h
->param
.i_dpb_size
= 1;
612 if( h
->param
.i_frame_packing
< -1 || h
->param
.i_frame_packing
> 7 )
614 x264_log( h
, X264_LOG_WARNING
, "ignoring unknown frame packing value\n" );
615 h
->param
.i_frame_packing
= -1;
617 if( h
->param
.i_frame_packing
== 7 &&
618 ((h
->param
.i_width
- h
->param
.crop_rect
.i_left
- h
->param
.crop_rect
.i_right
) % 3 ||
619 (h
->param
.i_height
- h
->param
.crop_rect
.i_top
- h
->param
.crop_rect
.i_bottom
) % 3) )
621 x264_log( h
, X264_LOG_ERROR
, "cropped resolution %dx%d not compatible with tile format frame packing\n",
622 h
->param
.i_width
- h
->param
.crop_rect
.i_left
- h
->param
.crop_rect
.i_right
,
623 h
->param
.i_height
- h
->param
.crop_rect
.i_top
- h
->param
.crop_rect
.i_bottom
);
627 /* Detect default ffmpeg settings and terminate with an error. */
631 score
+= h
->param
.analyse
.i_me_range
== 0;
632 score
+= h
->param
.rc
.i_qp_step
== 3;
633 score
+= h
->param
.i_keyint_max
== 12;
634 score
+= h
->param
.rc
.i_qp_min
[SLICE_TYPE_I
] == 2 ||
635 h
->param
.rc
.i_qp_min
[SLICE_TYPE_P
] == 2 ||
636 h
->param
.rc
.i_qp_min
[SLICE_TYPE_B
] == 2;
637 score
+= h
->param
.rc
.i_qp_max
[SLICE_TYPE_I
] == 31 ||
638 h
->param
.rc
.i_qp_max
[SLICE_TYPE_P
] == 31 ||
639 h
->param
.rc
.i_qp_max
[SLICE_TYPE_B
] == 31;
640 score
+= h
->param
.rc
.f_qcompress
== 0.5;
641 score
+= fabs(h
->param
.rc
.f_ip_factor
- 1.25) < 0.01;
642 score
+= fabs(h
->param
.rc
.f_pb_factor
- 1.25) < 0.01;
643 score
+= h
->param
.analyse
.inter
== 0 && h
->param
.analyse
.i_subpel_refine
== 8;
646 x264_log( h
, X264_LOG_ERROR
, "broken ffmpeg default settings detected\n" );
647 x264_log( h
, X264_LOG_ERROR
, "use an encoding preset (e.g. -vpre medium)\n" );
648 x264_log( h
, X264_LOG_ERROR
, "preset usage: -vpre <speed> -vpre <profile>\n" );
649 x264_log( h
, X264_LOG_ERROR
, "speed presets are listed in x264 --help\n" );
650 x264_log( h
, X264_LOG_ERROR
, "profile is optional; x264 defaults to high\n" );
655 h
->param
.i_frame_reference
= x264_clip3( h
->param
.i_frame_reference
, 1, X264_REF_MAX
);
658 const x264_level_t
*l
= x264_levels
;
659 if( h
->param
.i_level_idc
== X264_LEVEL_IDC_AUTO
)
661 int maxrate_bak
= h
->param
.rc
.i_vbv_max_bitrate
;
662 if( h
->param
.rc
.i_rc_method
== X264_RC_ABR
&& h
->param
.rc
.i_vbv_buffer_size
<= 0 )
663 h
->param
.rc
.i_vbv_max_bitrate
= h
->param
.rc
.i_bitrate
* 2;
664 x264_sps_init( h
->sps
, h
->param
.i_sps_id
, &h
->param
);
665 do h
->param
.i_level_idc
= l
->level_idc
;
666 while( l
[1].level_idc
&& x264_validate_levels( h
, 0 ) && l
++ );
667 h
->param
.rc
.i_vbv_max_bitrate
= maxrate_bak
;
671 while( l
->level_idc
&& l
->level_idc
!= h
->param
.i_level_idc
)
673 if( l
->level_idc
== 0 )
675 x264_log( h
, X264_LOG_ERROR
, "invalid level_idc: %d\n", h
->param
.i_level_idc
);
679 if( h
->param
.rc
.i_vbv_max_bitrate
< 0 )
681 int cbp_factor
= h
->param
.rc
.i_vbv_max_bitrate
== X264_VBV_MAXRATE_HIGH444
? 16 :
682 h
->param
.rc
.i_vbv_max_bitrate
== X264_VBV_MAXRATE_HIGH422
? 16 :
683 h
->param
.rc
.i_vbv_max_bitrate
== X264_VBV_MAXRATE_HIGH10
? 12 :
684 h
->param
.rc
.i_vbv_max_bitrate
== X264_VBV_MAXRATE_HIGH
? 5 : 4;
685 h
->param
.rc
.i_vbv_max_bitrate
= (l
->bitrate
* cbp_factor
) / 4;
686 x264_log( h
, X264_LOG_INFO
, "VBV maxrate is automatically set to %d.\n", h
->param
.rc
.i_vbv_max_bitrate
);
688 if( h
->param
.rc
.i_vbv_buffer_size
< 0 )
690 int cbp_factor
= h
->param
.rc
.i_vbv_buffer_size
== X264_VBV_BUFSIZE_HIGH444
? 16 :
691 h
->param
.rc
.i_vbv_buffer_size
== X264_VBV_BUFSIZE_HIGH422
? 16 :
692 h
->param
.rc
.i_vbv_buffer_size
== X264_VBV_BUFSIZE_HIGH10
? 12 :
693 h
->param
.rc
.i_vbv_buffer_size
== X264_VBV_BUFSIZE_HIGH
? 5 : 4;
694 h
->param
.rc
.i_vbv_buffer_size
= (l
->cpb
* cbp_factor
) / 4;
695 x264_log( h
, X264_LOG_INFO
, "VBV bufsize is automatically set to %d.\n", h
->param
.rc
.i_vbv_buffer_size
);
697 if( h
->param
.analyse
.i_mv_range
<= 0 )
698 h
->param
.analyse
.i_mv_range
= l
->mv_range
>> PARAM_INTERLACED
;
700 h
->param
.analyse
.i_mv_range
= x264_clip3(h
->param
.analyse
.i_mv_range
, 32, 512 >> PARAM_INTERLACED
);
703 if( h
->param
.rc
.i_rc_method
< 0 || h
->param
.rc
.i_rc_method
> 2 )
705 x264_log( h
, X264_LOG_ERROR
, "no ratecontrol method specified\n" );
709 if( PARAM_INTERLACED
)
710 h
->param
.b_pic_struct
= 1;
712 if( h
->param
.i_avcintra_class
)
714 if( BIT_DEPTH
!= 10 )
716 x264_log( h
, X264_LOG_ERROR
, "%2d-bit AVC-Intra is not widely compatible\n", BIT_DEPTH
);
717 x264_log( h
, X264_LOG_ERROR
, "10-bit x264 is required to encode AVC-Intra\n" );
721 int type
= h
->param
.i_avcintra_class
== 200 ? 2 :
722 h
->param
.i_avcintra_class
== 100 ? 1 :
723 h
->param
.i_avcintra_class
== 50 ? 0 : -1;
726 x264_log( h
, X264_LOG_ERROR
, "Invalid AVC-Intra class\n" );
730 /* [50/100/200][res][fps] */
737 const uint8_t *cqm_4ic
;
738 const uint8_t *cqm_8iy
;
739 } avcintra_lut
[3][2][7] =
741 {{{ 60000, 1001, 0, 912, x264_cqm_avci50_4ic
, x264_cqm_avci50_p_8iy
},
742 { 50, 1, 0, 1100, x264_cqm_avci50_4ic
, x264_cqm_avci50_p_8iy
},
743 { 30000, 1001, 0, 912, x264_cqm_avci50_4ic
, x264_cqm_avci50_p_8iy
},
744 { 25, 1, 0, 1100, x264_cqm_avci50_4ic
, x264_cqm_avci50_p_8iy
},
745 { 24000, 1001, 0, 912, x264_cqm_avci50_4ic
, x264_cqm_avci50_p_8iy
}},
746 {{ 30000, 1001, 1, 1820, x264_cqm_avci50_4ic
, x264_cqm_avci50_1080i_8iy
},
747 { 25, 1, 1, 2196, x264_cqm_avci50_4ic
, x264_cqm_avci50_1080i_8iy
},
748 { 60000, 1001, 0, 1820, x264_cqm_avci50_4ic
, x264_cqm_avci50_p_8iy
},
749 { 30000, 1001, 0, 1820, x264_cqm_avci50_4ic
, x264_cqm_avci50_p_8iy
},
750 { 50, 1, 0, 2196, x264_cqm_avci50_4ic
, x264_cqm_avci50_p_8iy
},
751 { 25, 1, 0, 2196, x264_cqm_avci50_4ic
, x264_cqm_avci50_p_8iy
},
752 { 24000, 1001, 0, 1820, x264_cqm_avci50_4ic
, x264_cqm_avci50_p_8iy
}}},
753 {{{ 60000, 1001, 0, 1848, x264_cqm_avci100_720p_4ic
, x264_cqm_avci100_720p_8iy
},
754 { 50, 1, 0, 2224, x264_cqm_avci100_720p_4ic
, x264_cqm_avci100_720p_8iy
},
755 { 30000, 1001, 0, 1848, x264_cqm_avci100_720p_4ic
, x264_cqm_avci100_720p_8iy
},
756 { 25, 1, 0, 2224, x264_cqm_avci100_720p_4ic
, x264_cqm_avci100_720p_8iy
},
757 { 24000, 1001, 0, 1848, x264_cqm_avci100_720p_4ic
, x264_cqm_avci100_720p_8iy
}},
758 {{ 30000, 1001, 1, 3692, x264_cqm_avci100_1080_4ic
, x264_cqm_avci100_1080i_8iy
},
759 { 25, 1, 1, 4444, x264_cqm_avci100_1080_4ic
, x264_cqm_avci100_1080i_8iy
},
760 { 60000, 1001, 0, 3692, x264_cqm_avci100_1080_4ic
, x264_cqm_avci100_1080p_8iy
},
761 { 30000, 1001, 0, 3692, x264_cqm_avci100_1080_4ic
, x264_cqm_avci100_1080p_8iy
},
762 { 50, 1, 0, 4444, x264_cqm_avci100_1080_4ic
, x264_cqm_avci100_1080p_8iy
},
763 { 25, 1, 0, 4444, x264_cqm_avci100_1080_4ic
, x264_cqm_avci100_1080p_8iy
},
764 { 24000, 1001, 0, 3692, x264_cqm_avci100_1080_4ic
, x264_cqm_avci100_1080p_8iy
}}},
765 {{{ 60000, 1001, 0, 3724, x264_cqm_avci100_720p_4ic
, x264_cqm_avci100_720p_8iy
},
766 { 50, 1, 0, 4472, x264_cqm_avci100_720p_4ic
, x264_cqm_avci100_720p_8iy
}},
767 {{ 30000, 1001, 1, 7444, x264_cqm_avci100_1080_4ic
, x264_cqm_avci100_1080i_8iy
},
768 { 25, 1, 1, 8940, x264_cqm_avci100_1080_4ic
, x264_cqm_avci100_1080i_8iy
},
769 { 60000, 1001, 0, 7444, x264_cqm_avci100_1080_4ic
, x264_cqm_avci100_1080p_8iy
},
770 { 30000, 1001, 0, 7444, x264_cqm_avci100_1080_4ic
, x264_cqm_avci100_1080p_8iy
},
771 { 50, 1, 0, 8940, x264_cqm_avci100_1080_4ic
, x264_cqm_avci100_1080p_8iy
},
772 { 25, 1, 0, 8940, x264_cqm_avci100_1080_4ic
, x264_cqm_avci100_1080p_8iy
},
773 { 24000, 1001, 0, 7444, x264_cqm_avci100_1080_4ic
, x264_cqm_avci100_1080p_8iy
}}}
777 if( i_csp
>= X264_CSP_I420
&& i_csp
< X264_CSP_I422
&& !type
)
779 if( h
->param
.i_width
== 1440 && h
->param
.i_height
== 1080 ) res
= 1;
780 else if( h
->param
.i_width
== 960 && h
->param
.i_height
== 720 ) res
= 0;
782 else if( i_csp
>= X264_CSP_I422
&& i_csp
< X264_CSP_I444
&& type
)
784 if( h
->param
.i_width
== 1920 && h
->param
.i_height
== 1080 ) res
= 1;
785 else if( h
->param
.i_width
== 1280 && h
->param
.i_height
== 720 ) res
= 0;
789 x264_log( h
, X264_LOG_ERROR
, "Invalid colorspace for AVC-Intra %d\n", h
->param
.i_avcintra_class
);
795 x264_log( h
, X264_LOG_ERROR
, "Resolution %dx%d invalid for AVC-Intra %d\n",
796 h
->param
.i_width
, h
->param
.i_height
, h
->param
.i_avcintra_class
);
800 if( h
->param
.nalu_process
)
802 x264_log( h
, X264_LOG_ERROR
, "nalu_process is not supported in AVC-Intra mode\n" );
806 if( !h
->param
.b_repeat_headers
)
808 x264_log( h
, X264_LOG_ERROR
, "Separate headers not supported in AVC-Intra mode\n" );
813 uint32_t fps_num
= h
->param
.i_fps_num
, fps_den
= h
->param
.i_fps_den
;
814 x264_reduce_fraction( &fps_num
, &fps_den
);
815 for( i
= 0; i
< 7; i
++ )
817 if( avcintra_lut
[type
][res
][i
].fps_num
== fps_num
&&
818 avcintra_lut
[type
][res
][i
].fps_den
== fps_den
&&
819 avcintra_lut
[type
][res
][i
].interlaced
== PARAM_INTERLACED
)
826 x264_log( h
, X264_LOG_ERROR
, "FPS %d/%d%c not compatible with AVC-Intra\n",
827 h
->param
.i_fps_num
, h
->param
.i_fps_den
, PARAM_INTERLACED
? 'i' : 'p' );
831 h
->param
.i_keyint_max
= 1;
832 h
->param
.b_intra_refresh
= 0;
833 h
->param
.analyse
.i_weighted_pred
= 0;
834 h
->param
.i_frame_reference
= 1;
835 h
->param
.i_dpb_size
= 1;
837 h
->param
.b_bluray_compat
= 0;
838 h
->param
.b_vfr_input
= 0;
840 h
->param
.vui
.i_chroma_loc
= 0;
841 h
->param
.i_nal_hrd
= X264_NAL_HRD_NONE
;
842 h
->param
.b_deblocking_filter
= 0;
843 h
->param
.b_stitchable
= 1;
844 h
->param
.b_pic_struct
= 0;
845 h
->param
.analyse
.b_transform_8x8
= 1;
846 h
->param
.analyse
.intra
= X264_ANALYSE_I8x8
;
847 h
->param
.analyse
.i_chroma_qp_offset
= res
&& type
? 3 : 4;
848 h
->param
.b_cabac
= !type
;
849 h
->param
.rc
.i_vbv_buffer_size
= avcintra_lut
[type
][res
][i
].frame_size
;
850 h
->param
.rc
.i_vbv_max_bitrate
=
851 h
->param
.rc
.i_bitrate
= h
->param
.rc
.i_vbv_buffer_size
* fps_num
/ fps_den
;
852 h
->param
.rc
.i_rc_method
= X264_RC_ABR
;
853 h
->param
.rc
.f_vbv_buffer_init
= 1.0;
854 h
->param
.rc
.b_filler
= 1;
855 h
->param
.i_cqm_preset
= X264_CQM_CUSTOM
;
856 memcpy( h
->param
.cqm_4iy
, x264_cqm_jvt4i
, sizeof(h
->param
.cqm_4iy
) );
857 memcpy( h
->param
.cqm_4ic
, avcintra_lut
[type
][res
][i
].cqm_4ic
, sizeof(h
->param
.cqm_4ic
) );
858 memcpy( h
->param
.cqm_8iy
, avcintra_lut
[type
][res
][i
].cqm_8iy
, sizeof(h
->param
.cqm_8iy
) );
860 /* Need exactly 10 slices of equal MB count... why? $deity knows... */
861 h
->param
.i_slice_max_mbs
= ((h
->param
.i_width
+ 15) / 16) * ((h
->param
.i_height
+ 15) / 16) / 10;
862 h
->param
.i_slice_max_size
= 0;
863 /* The slice structure only allows a maximum of 2 threads for 1080i/p
864 * and 1 or 5 threads for 720p */
865 if( h
->param
.b_sliced_threads
)
868 h
->param
.i_threads
= X264_MIN( 2, h
->param
.i_threads
);
871 h
->param
.i_threads
= X264_MIN( 5, h
->param
.i_threads
);
872 if( h
->param
.i_threads
< 5 )
873 h
->param
.i_threads
= 1;
878 h
->param
.vui
.i_sar_width
= h
->param
.vui
.i_sar_height
= 1;
881 h
->param
.vui
.i_sar_width
= 4;
882 h
->param
.vui
.i_sar_height
= 3;
885 /* Official encoder doesn't appear to go under 13
886 * and Avid cannot handle negative QPs */
887 for( int j
= 0; j
< 3; j
++ )
888 h
->param
.rc
.i_qp_min
[j
] = X264_MAX( h
->param
.rc
.i_qp_min
[j
], QP_BD_OFFSET
+ 1 );
891 h
->param
.rc
.f_rf_constant
= x264_clip3f( h
->param
.rc
.f_rf_constant
, -QP_BD_OFFSET
, 51 );
892 h
->param
.rc
.f_rf_constant_max
= x264_clip3f( h
->param
.rc
.f_rf_constant_max
, -QP_BD_OFFSET
, 51 );
893 h
->param
.rc
.i_qp_constant
= x264_clip3( h
->param
.rc
.i_qp_constant
, 0, QP_MAX
);
894 h
->param
.analyse
.i_subpel_refine
= x264_clip3( h
->param
.analyse
.i_subpel_refine
, 0, 11 );
895 h
->param
.rc
.f_ip_factor
= X264_MAX( h
->param
.rc
.f_ip_factor
, 0.01f
);
896 h
->param
.rc
.f_pb_factor
= X264_MAX( h
->param
.rc
.f_pb_factor
, 0.01f
);
897 if( h
->param
.rc
.i_rc_method
== X264_RC_CRF
)
899 h
->param
.rc
.i_qp_constant
= h
->param
.rc
.f_rf_constant
+ QP_BD_OFFSET
;
900 h
->param
.rc
.i_bitrate
= 0;
902 if( b_open
&& (h
->param
.rc
.i_rc_method
== X264_RC_CQP
|| h
->param
.rc
.i_rc_method
== X264_RC_CRF
)
903 && h
->param
.rc
.i_qp_constant
== 0 )
905 h
->mb
.b_lossless
= 1;
906 h
->param
.i_cqm_preset
= X264_CQM_FLAT
;
907 h
->param
.psz_cqm_file
= NULL
;
908 h
->param
.rc
.i_rc_method
= X264_RC_CQP
;
909 h
->param
.rc
.f_ip_factor
= 1;
910 h
->param
.rc
.f_pb_factor
= 1;
911 h
->param
.analyse
.b_psnr
= 0;
912 h
->param
.analyse
.b_ssim
= 0;
913 h
->param
.analyse
.i_chroma_qp_offset
= 0;
914 h
->param
.analyse
.i_trellis
= 0;
915 h
->param
.analyse
.b_fast_pskip
= 0;
916 h
->param
.analyse
.i_noise_reduction
= 0;
917 h
->param
.analyse
.i_fgo
= 0;
918 h
->param
.analyse
.b_psy
= 0;
919 h
->param
.i_bframe
= 0;
920 /* 8x8dct is not useful without RD in CAVLC lossless */
921 if( !h
->param
.b_cabac
&& h
->param
.analyse
.i_subpel_refine
< 6 )
922 h
->param
.analyse
.b_transform_8x8
= 0;
923 h
->param
.analyse
.inter
&= ~X264_ANALYSE_I8x8
;
924 h
->param
.analyse
.intra
&= ~X264_ANALYSE_I8x8
;
926 if( i_csp
>= X264_CSP_I444
&& h
->param
.b_cabac
)
928 /* Disable 8x8dct during 4:4:4+CABAC encoding for compatibility with libavcodec */
929 h
->param
.analyse
.b_transform_8x8
= 0;
931 if( h
->param
.rc
.i_rc_method
== X264_RC_CQP
)
933 float qp_p
= h
->param
.rc
.i_qp_constant
;
934 float qp_i
= qp_p
- 6*log2f( h
->param
.rc
.f_ip_factor
);
935 float qp_b
= qp_p
+ 6*log2f( h
->param
.rc
.f_pb_factor
);
936 h
->param
.rc
.i_qp_min_min
=
937 h
->param
.rc
.i_qp_min
[SLICE_TYPE_I
] =
938 h
->param
.rc
.i_qp_min
[SLICE_TYPE_P
] =
939 h
->param
.rc
.i_qp_min
[SLICE_TYPE_B
] = x264_clip3( (int)(X264_MIN3( qp_p
, qp_i
, qp_b
)), 0, QP_MAX
);
940 h
->param
.rc
.i_qp_max_max
=
941 h
->param
.rc
.i_qp_max
[SLICE_TYPE_I
] =
942 h
->param
.rc
.i_qp_max
[SLICE_TYPE_P
] =
943 h
->param
.rc
.i_qp_max
[SLICE_TYPE_B
] = x264_clip3( (int)(X264_MAX3( qp_p
, qp_i
, qp_b
) + .999), 0, QP_MAX
);
944 h
->param
.rc
.i_aq_mode
= 0;
945 h
->param
.rc
.b_aq2
= 0;
946 h
->param
.rc
.i_aq3_mode
= 0;
947 h
->param
.rc
.b_mb_tree
= 0;
948 h
->param
.rc
.i_bitrate
= 0;
950 for( int i
= 0; i
< 3; i
++ )
952 h
->param
.rc
.i_qp_max
[i
] = x264_clip3( h
->param
.rc
.i_qp_max
[i
], 0, QP_MAX
);
953 h
->param
.rc
.i_qp_min
[i
] = x264_clip3( h
->param
.rc
.i_qp_min
[i
], 0, h
->param
.rc
.i_qp_max
[i
] );
955 h
->param
.rc
.i_qp_min_min
= X264_MIN3( h
->param
.rc
.i_qp_min
[SLICE_TYPE_I
],
956 h
->param
.rc
.i_qp_min
[SLICE_TYPE_P
],
957 h
->param
.rc
.i_qp_min
[SLICE_TYPE_B
] );
958 h
->param
.rc
.i_qp_max_max
= X264_MAX3( h
->param
.rc
.i_qp_max
[SLICE_TYPE_I
],
959 h
->param
.rc
.i_qp_max
[SLICE_TYPE_P
],
960 h
->param
.rc
.i_qp_max
[SLICE_TYPE_B
] );
961 h
->param
.rc
.i_qp_step
= x264_clip3( h
->param
.rc
.i_qp_step
, 2, QP_MAX
);
962 h
->param
.rc
.i_bitrate
= x264_clip3( h
->param
.rc
.i_bitrate
, 0, 2000000 );
963 if( h
->param
.rc
.i_rc_method
== X264_RC_ABR
&& !h
->param
.rc
.i_bitrate
)
965 x264_log( h
, X264_LOG_ERROR
, "bitrate not specified\n" );
968 h
->param
.rc
.i_vbv_buffer_size
= x264_clip3( h
->param
.rc
.i_vbv_buffer_size
, 0, 2000000 );
969 h
->param
.rc
.i_vbv_max_bitrate
= x264_clip3( h
->param
.rc
.i_vbv_max_bitrate
, 0, 2000000 );
970 h
->param
.rc
.f_vbv_buffer_init
= x264_clip3f( h
->param
.rc
.f_vbv_buffer_init
, 0, 2000000 );
971 if( h
->param
.rc
.i_vbv_buffer_size
)
973 if( h
->param
.rc
.i_rc_method
== X264_RC_CQP
)
975 x264_log( h
, X264_LOG_WARNING
, "VBV is incompatible with constant QP, ignored.\n" );
976 h
->param
.rc
.i_vbv_max_bitrate
= 0;
977 h
->param
.rc
.i_vbv_buffer_size
= 0;
979 else if( h
->param
.rc
.i_vbv_max_bitrate
== 0 )
981 if( h
->param
.rc
.i_rc_method
== X264_RC_ABR
)
983 x264_log( h
, X264_LOG_WARNING
, "VBV maxrate unspecified, assuming CBR\n" );
984 h
->param
.rc
.i_vbv_max_bitrate
= h
->param
.rc
.i_bitrate
;
988 x264_log( h
, X264_LOG_WARNING
, "VBV bufsize set but maxrate unspecified, ignored\n" );
989 h
->param
.rc
.i_vbv_buffer_size
= 0;
992 else if( h
->param
.rc
.i_vbv_max_bitrate
< h
->param
.rc
.i_bitrate
&&
993 h
->param
.rc
.i_rc_method
== X264_RC_ABR
)
995 x264_log( h
, X264_LOG_WARNING
, "max bitrate less than average bitrate, assuming CBR\n" );
996 h
->param
.rc
.i_bitrate
= h
->param
.rc
.i_vbv_max_bitrate
;
999 else if( h
->param
.rc
.i_vbv_max_bitrate
)
1001 x264_log( h
, X264_LOG_WARNING
, "VBV maxrate specified, but no bufsize, ignored\n" );
1002 h
->param
.rc
.i_vbv_max_bitrate
= 0;
1005 h
->param
.i_slice_max_size
= X264_MAX( h
->param
.i_slice_max_size
, 0 );
1006 h
->param
.i_slice_max_mbs
= X264_MAX( h
->param
.i_slice_max_mbs
, 0 );
1007 h
->param
.i_slice_min_mbs
= X264_MAX( h
->param
.i_slice_min_mbs
, 0 );
1008 if( h
->param
.i_slice_max_mbs
)
1009 h
->param
.i_slice_min_mbs
= X264_MIN( h
->param
.i_slice_min_mbs
, h
->param
.i_slice_max_mbs
/2 );
1010 else if( !h
->param
.i_slice_max_size
)
1011 h
->param
.i_slice_min_mbs
= 0;
1012 if( PARAM_INTERLACED
&& h
->param
.i_slice_min_mbs
)
1014 x264_log( h
, X264_LOG_WARNING
, "interlace + slice-min-mbs is not implemented\n" );
1015 h
->param
.i_slice_min_mbs
= 0;
1017 int mb_width
= (h
->param
.i_width
+15)/16;
1018 if( h
->param
.i_slice_min_mbs
> mb_width
)
1020 x264_log( h
, X264_LOG_WARNING
, "slice-min-mbs > row mb size (%d) not implemented\n", mb_width
);
1021 h
->param
.i_slice_min_mbs
= mb_width
;
1024 int max_slices
= (h
->param
.i_height
+((16<<PARAM_INTERLACED
)-1))/(16<<PARAM_INTERLACED
);
1025 if( h
->param
.b_sliced_threads
)
1026 h
->param
.i_slice_count
= x264_clip3( h
->param
.i_threads
, 0, max_slices
);
1029 h
->param
.i_slice_count
= x264_clip3( h
->param
.i_slice_count
, 0, max_slices
);
1030 if( h
->param
.i_slice_max_mbs
|| h
->param
.i_slice_max_size
)
1031 h
->param
.i_slice_count
= 0;
1033 if( h
->param
.i_slice_count_max
> 0 )
1034 h
->param
.i_slice_count_max
= X264_MAX( h
->param
.i_slice_count
, h
->param
.i_slice_count_max
);
1036 if( h
->param
.b_bluray_compat
)
1038 h
->param
.i_bframe_pyramid
= X264_MIN( X264_B_PYRAMID_STRICT
, h
->param
.i_bframe_pyramid
);
1039 h
->param
.i_bframe
= X264_MIN( h
->param
.i_bframe
, 3 );
1041 h
->param
.i_nal_hrd
= X264_MAX( h
->param
.i_nal_hrd
, X264_NAL_HRD_VBR
);
1042 h
->param
.i_slice_max_size
= 0;
1043 h
->param
.i_slice_max_mbs
= 0;
1044 h
->param
.b_intra_refresh
= 0;
1045 h
->param
.i_frame_reference
= X264_MIN( h
->param
.i_frame_reference
, 6 );
1046 h
->param
.i_dpb_size
= X264_MIN( h
->param
.i_dpb_size
, 6 );
1047 /* Don't use I-frames, because Blu-ray treats them the same as IDR. */
1048 h
->param
.i_keyint_min
= 1;
1049 /* Due to the proliferation of broken players that don't handle dupes properly. */
1050 h
->param
.analyse
.i_weighted_pred
= X264_MIN( h
->param
.analyse
.i_weighted_pred
, X264_WEIGHTP_SIMPLE
);
1051 if( h
->param
.b_fake_interlaced
)
1052 h
->param
.b_pic_struct
= 1;
1055 h
->param
.i_dpb_size
= x264_clip3( h
->param
.i_dpb_size
, 1, X264_REF_MAX
);
1056 if( h
->param
.i_scenecut_threshold
< 0 )
1057 h
->param
.i_scenecut_threshold
= 0;
1058 h
->param
.analyse
.i_direct_mv_pred
= x264_clip3( h
->param
.analyse
.i_direct_mv_pred
, X264_DIRECT_PRED_NONE
, X264_DIRECT_PRED_AUTO
);
1059 if( !h
->param
.analyse
.i_subpel_refine
&& h
->param
.analyse
.i_direct_mv_pred
> X264_DIRECT_PRED_SPATIAL
)
1061 x264_log( h
, X264_LOG_WARNING
, "subme=0 + direct=temporal is not supported\n" );
1062 h
->param
.analyse
.i_direct_mv_pred
= X264_DIRECT_PRED_SPATIAL
;
1064 h
->param
.i_bframe
= x264_clip3( h
->param
.i_bframe
, 0, X264_MIN( X264_BFRAME_MAX
, h
->param
.i_keyint_max
-1 ) );
1065 h
->param
.i_bframe_bias
= x264_clip3( h
->param
.i_bframe_bias
, -90, 100 );
1066 if( h
->param
.i_bframe
<= 1 )
1067 h
->param
.i_bframe_pyramid
= X264_B_PYRAMID_NONE
;
1068 h
->param
.i_bframe_pyramid
= x264_clip3( h
->param
.i_bframe_pyramid
, X264_B_PYRAMID_NONE
, X264_B_PYRAMID_NORMAL
);
1069 h
->param
.i_bframe_adaptive
= x264_clip3( h
->param
.i_bframe_adaptive
, X264_B_ADAPT_NONE
, X264_B_ADAPT_TRELLIS
);
1070 if( !h
->param
.i_bframe
)
1072 h
->param
.i_bframe_adaptive
= X264_B_ADAPT_NONE
;
1073 h
->param
.analyse
.i_direct_mv_pred
= 0;
1074 h
->param
.analyse
.b_weighted_bipred
= 0;
1075 h
->param
.b_open_gop
= 0;
1077 if( h
->param
.b_intra_refresh
&& h
->param
.i_bframe_pyramid
== X264_B_PYRAMID_NORMAL
)
1079 x264_log( h
, X264_LOG_WARNING
, "b-pyramid normal + intra-refresh is not supported\n" );
1080 h
->param
.i_bframe_pyramid
= X264_B_PYRAMID_STRICT
;
1082 if( h
->param
.b_intra_refresh
&& (h
->param
.i_frame_reference
> 1 || h
->param
.i_dpb_size
> 1) )
1084 x264_log( h
, X264_LOG_WARNING
, "ref > 1 + intra-refresh is not supported\n" );
1085 h
->param
.i_frame_reference
= 1;
1086 h
->param
.i_dpb_size
= 1;
1088 if( h
->param
.b_intra_refresh
&& h
->param
.b_open_gop
)
1090 x264_log( h
, X264_LOG_WARNING
, "intra-refresh is not compatible with open-gop\n" );
1091 h
->param
.b_open_gop
= 0;
1093 if( h
->param
.i_keyint_min
== X264_KEYINT_MIN_AUTO
)
1094 h
->param
.i_keyint_min
= X264_MIN( h
->param
.i_keyint_max
/ 10, (int)fps
);
1095 h
->param
.i_keyint_min
= x264_clip3( h
->param
.i_keyint_min
, 1, h
->param
.i_keyint_max
/2+1 );
1096 h
->param
.rc
.i_lookahead
= x264_clip3( h
->param
.rc
.i_lookahead
, 0, X264_LOOKAHEAD_MAX
);
1098 int maxrate
= X264_MAX( h
->param
.rc
.i_vbv_max_bitrate
, h
->param
.rc
.i_bitrate
);
1099 float bufsize
= maxrate
? (float)h
->param
.rc
.i_vbv_buffer_size
/ maxrate
: 0;
1100 h
->param
.rc
.i_lookahead
= X264_MIN( h
->param
.rc
.i_lookahead
, X264_MAX( h
->param
.i_keyint_max
, bufsize
*fps
) );
1103 if( !h
->param
.i_timebase_num
|| !h
->param
.i_timebase_den
|| !(h
->param
.b_vfr_input
|| h
->param
.b_pulldown
) )
1105 h
->param
.i_timebase_num
= h
->param
.i_fps_den
;
1106 h
->param
.i_timebase_den
= h
->param
.i_fps_num
;
1109 h
->param
.rc
.f_qcompress
= x264_clip3f( h
->param
.rc
.f_qcompress
, 0.0, 1.0 );
1110 if( h
->param
.i_keyint_max
== 1 || h
->param
.rc
.f_qcompress
== 1 )
1111 h
->param
.rc
.b_mb_tree
= 0;
1112 if( (!h
->param
.b_intra_refresh
&& h
->param
.i_keyint_max
!= X264_KEYINT_MAX_INFINITE
) &&
1113 !h
->param
.rc
.i_lookahead
&& h
->param
.rc
.b_mb_tree
)
1115 x264_log( h
, X264_LOG_WARNING
, "lookaheadless mb-tree requires intra refresh or infinite keyint\n" );
1116 h
->param
.rc
.b_mb_tree
= 0;
1118 if( b_open
&& h
->param
.rc
.b_stat_read
)
1119 h
->param
.rc
.i_lookahead
= 0;
1121 if( h
->param
.i_sync_lookahead
< 0 )
1122 h
->param
.i_sync_lookahead
= h
->param
.i_bframe
+ 1;
1123 h
->param
.i_sync_lookahead
= X264_MIN( h
->param
.i_sync_lookahead
, X264_LOOKAHEAD_MAX
);
1124 if( h
->param
.rc
.b_stat_read
|| h
->i_thread_frames
== 1 )
1125 h
->param
.i_sync_lookahead
= 0;
1127 h
->param
.i_sync_lookahead
= 0;
1130 h
->param
.i_deblocking_filter_alphac0
= x264_clip3( h
->param
.i_deblocking_filter_alphac0
, -6, 6 );
1131 h
->param
.i_deblocking_filter_beta
= x264_clip3( h
->param
.i_deblocking_filter_beta
, -6, 6 );
1132 h
->param
.analyse
.i_luma_deadzone
[0] = x264_clip3( h
->param
.analyse
.i_luma_deadzone
[0], 0, 32 );
1133 h
->param
.analyse
.i_luma_deadzone
[1] = x264_clip3( h
->param
.analyse
.i_luma_deadzone
[1], 0, 32 );
1135 h
->param
.i_cabac_init_idc
= x264_clip3( h
->param
.i_cabac_init_idc
, 0, 2 );
1137 if( h
->param
.i_cqm_preset
< X264_CQM_FLAT
|| h
->param
.i_cqm_preset
> X264_CQM_CUSTOM
)
1138 h
->param
.i_cqm_preset
= X264_CQM_FLAT
;
1140 if( h
->param
.analyse
.i_me_method
< X264_ME_DIA
||
1141 h
->param
.analyse
.i_me_method
> X264_ME_TESA
)
1142 h
->param
.analyse
.i_me_method
= X264_ME_HEX
;
1143 h
->param
.analyse
.i_me_range
= x264_clip3( h
->param
.analyse
.i_me_range
, 4, 1024 );
1144 if( h
->param
.analyse
.i_me_range
> 16 && h
->param
.analyse
.i_me_method
<= X264_ME_HEX
)
1145 h
->param
.analyse
.i_me_range
= 16;
1146 if( h
->param
.analyse
.i_me_method
== X264_ME_TESA
&&
1147 (h
->mb
.b_lossless
|| h
->param
.analyse
.i_subpel_refine
<= 1) )
1148 h
->param
.analyse
.i_me_method
= X264_ME_ESA
;
1149 h
->param
.analyse
.b_mixed_references
= h
->param
.analyse
.b_mixed_references
&& h
->param
.i_frame_reference
> 1;
1150 h
->param
.analyse
.inter
&= X264_ANALYSE_PSUB16x16
|X264_ANALYSE_PSUB8x8
|X264_ANALYSE_BSUB16x16
|
1151 X264_ANALYSE_I4x4
|X264_ANALYSE_I8x8
;
1152 h
->param
.analyse
.intra
&= X264_ANALYSE_I4x4
|X264_ANALYSE_I8x8
;
1153 if( !(h
->param
.analyse
.inter
& X264_ANALYSE_PSUB16x16
) )
1154 h
->param
.analyse
.inter
&= ~X264_ANALYSE_PSUB8x8
;
1155 if( !h
->param
.analyse
.b_transform_8x8
)
1157 h
->param
.analyse
.inter
&= ~X264_ANALYSE_I8x8
;
1158 h
->param
.analyse
.intra
&= ~X264_ANALYSE_I8x8
;
1160 h
->param
.analyse
.i_trellis
= x264_clip3( h
->param
.analyse
.i_trellis
, 0, 2 );
1162 if( h
->param
.analyse
.i_weighted_pred
== X264_WEIGHTP_NONE
)
1163 h
->param
.rc
.f_fade_compensate
+= 0.1;
1164 if( !h
->param
.rc
.b_mb_tree
)
1165 h
->param
.rc
.f_fade_compensate
= 0;
1167 h
->param
.rc
.i_aq_mode
= x264_clip3( h
->param
.rc
.i_aq_mode
, 0, 4 );
1168 h
->param
.rc
.f_aq_strength
= x264_clip3f( h
->param
.rc
.f_aq_strength
, -3, 3 );
1169 h
->param
.rc
.b_aq2
= h
->param
.rc
.b_aq2
&& h
->param
.rc
.f_aq2_strength
> 0;
1170 if( h
->param
.rc
.f_aq_strength
== 0 && (h
->param
.rc
.i_aq_mode
> 0 ? !h
->param
.rc
.b_aq2
: 1) )
1171 h
->param
.rc
.i_aq_mode
= 0;
1172 if( h
->param
.rc
.f_aq_sensitivity
< 0 )
1173 h
->param
.rc
.f_aq_sensitivity
= 0;
1174 h
->param
.rc
.f_aq_ifactor
= x264_clip3f( h
->param
.rc
.f_aq_ifactor
, -10, 10 );
1175 h
->param
.rc
.f_aq_pfactor
= x264_clip3f( h
->param
.rc
.f_aq_pfactor
, -10, 10 );
1176 h
->param
.rc
.f_aq_bfactor
= x264_clip3f( h
->param
.rc
.f_aq_bfactor
, -10, 10 );
1177 h
->param
.rc
.f_aq2_ifactor
= x264_clip3f( h
->param
.rc
.f_aq2_ifactor
, -10, 10 );
1178 h
->param
.rc
.f_aq2_pfactor
= x264_clip3f( h
->param
.rc
.f_aq2_pfactor
, -10, 10 );
1179 h
->param
.rc
.f_aq2_bfactor
= x264_clip3f( h
->param
.rc
.f_aq2_bfactor
, -10, 10 );
1180 h
->param
.rc
.i_aq3_mode
= x264_clip3( h
->param
.rc
.i_aq3_mode
, 0, 2 );
1181 h
->param
.rc
.f_aq3_strength
= x264_clip3f( h
->param
.rc
.f_aq3_strength
, -3, 3 );
1182 for( int i
= 0; i
< 2; i
++ )
1183 for( int j
= 0; j
< 4; j
++ )
1184 h
->param
.rc
.f_aq3_strengths
[i
][j
] = x264_clip3f( h
->param
.rc
.f_aq3_strengths
[i
][j
], -3, 3 );
1185 if( h
->param
.rc
.f_aq3_strengths
[0][0] == 0 && h
->param
.rc
.f_aq3_strengths
[1][0] == 0 &&
1186 h
->param
.rc
.f_aq3_strengths
[0][1] == 0 && h
->param
.rc
.f_aq3_strengths
[1][1] == 0 &&
1187 h
->param
.rc
.f_aq3_strengths
[0][2] == 0 && h
->param
.rc
.f_aq3_strengths
[1][2] == 0 &&
1188 h
->param
.rc
.f_aq3_strengths
[0][3] == 0 && h
->param
.rc
.f_aq3_strengths
[1][3] == 0 )
1190 if( h
->param
.rc
.f_aq3_strength
== 0 )
1191 h
->param
.rc
.i_aq3_mode
= 0;
1193 for( int i
= 0; i
< 2; i
++ )
1194 for( int j
= 0; j
< 4; j
++ )
1195 h
->param
.rc
.f_aq3_strengths
[i
][j
] = h
->param
.rc
.f_aq3_strength
;
1197 if( h
->param
.rc
.f_aq3_sensitivity
< 0 )
1198 h
->param
.rc
.f_aq3_sensitivity
= 0;
1199 for( int i
= 0; i
< 2; i
++ )
1201 h
->param
.rc
.f_aq3_ifactor
[i
] = x264_clip3f( h
->param
.rc
.f_aq3_ifactor
[i
], -10, 10 );
1202 h
->param
.rc
.f_aq3_pfactor
[i
] = x264_clip3f( h
->param
.rc
.f_aq3_pfactor
[i
], -10, 10 );
1203 h
->param
.rc
.f_aq3_bfactor
[i
] = x264_clip3f( h
->param
.rc
.f_aq3_bfactor
[i
], -10, 10 );
1205 h
->param
.rc
.i_aq3_boundary
[0] = x264_clip3( h
->param
.rc
.i_aq3_boundary
[0], 0, (256 << (BIT_DEPTH
- 8)) - 1 );
1206 h
->param
.rc
.i_aq3_boundary
[1] = x264_clip3( h
->param
.rc
.i_aq3_boundary
[1], 0, (256 << (BIT_DEPTH
- 8)) - 1 );
1207 h
->param
.rc
.i_aq3_boundary
[2] = x264_clip3( h
->param
.rc
.i_aq3_boundary
[2], 0, (256 << (BIT_DEPTH
- 8)) - 1 );
1208 if( !h
->param
.rc
.b_aq3_boundary
||
1209 h
->param
.rc
.i_aq3_boundary
[0] <= h
->param
.rc
.i_aq3_boundary
[1] || h
->param
.rc
.i_aq3_boundary
[1] <= h
->param
.rc
.i_aq3_boundary
[2] )
1211 h
->param
.rc
.i_aq3_boundary
[0] = (h
->param
.vui
.b_fullrange
== 1 ? 205 : 192) << (BIT_DEPTH
- 8);
1212 h
->param
.rc
.i_aq3_boundary
[1] = (h
->param
.vui
.b_fullrange
== 1 ? 56 : 64) << (BIT_DEPTH
- 8);
1213 h
->param
.rc
.i_aq3_boundary
[2] = (h
->param
.vui
.b_fullrange
== 1 ? 9 : 24) << (BIT_DEPTH
- 8);
1216 if( h
->param
.i_log_level
< X264_LOG_INFO
&& (!h
->param
.psz_log_file
|| h
->param
.i_log_file_level
< X264_LOG_INFO
) )
1218 h
->param
.analyse
.b_psnr
= 0;
1219 h
->param
.analyse
.b_ssim
= 0;
1221 /* Warn users trying to measure PSNR/SSIM with psy opts on. */
1222 if( b_open
&& (h
->param
.analyse
.b_psnr
|| h
->param
.analyse
.b_ssim
) )
1226 if( h
->param
.analyse
.b_psy
)
1228 s
= h
->param
.analyse
.b_psnr
? "psnr" : "ssim";
1229 x264_log( h
, X264_LOG_WARNING
, "--%s used with psy on: results will be invalid!\n", s
);
1231 else if( !h
->param
.rc
.i_aq_mode
&& h
->param
.analyse
.b_ssim
)
1233 x264_log( h
, X264_LOG_WARNING
, "--ssim used with AQ off: results will be invalid!\n" );
1236 else if( h
->param
.rc
.i_aq_mode
&& h
->param
.analyse
.b_psnr
)
1238 x264_log( h
, X264_LOG_WARNING
, "--psnr used with AQ on: results will be invalid!\n" );
1242 x264_log( h
, X264_LOG_WARNING
, "--tune %s should be used if attempting to benchmark %s!\n", s
, s
);
1245 if( !h
->param
.analyse
.b_psy
)
1247 h
->param
.analyse
.f_psy_rd
= 0;
1248 h
->param
.analyse
.f_psy_trellis
= 0;
1249 h
->param
.rc
.f_fade_compensate
= 0;
1251 h
->param
.analyse
.f_psy_rd
= x264_clip3f( h
->param
.analyse
.f_psy_rd
, 0, 10 );
1252 h
->param
.analyse
.f_psy_trellis
= x264_clip3f( h
->param
.analyse
.f_psy_trellis
, 0, 10 );
1253 h
->mb
.i_psy_rd
= h
->param
.analyse
.i_subpel_refine
>= 6 ? FIX8( h
->param
.analyse
.f_psy_rd
) : 0;
1254 h
->mb
.i_psy_trellis
= h
->param
.analyse
.i_trellis
? FIX8( h
->param
.analyse
.f_psy_trellis
/ 4 ) : 0;
1255 h
->param
.analyse
.i_chroma_qp_offset
= x264_clip3(h
->param
.analyse
.i_chroma_qp_offset
, -32, 32);
1256 /* In 4:4:4 mode, chroma gets twice as much resolution, so we can halve its quality. */
1257 if( b_open
&& i_csp
>= X264_CSP_I444
&& i_csp
< X264_CSP_BGR
&& h
->param
.analyse
.b_psy
)
1258 h
->param
.analyse
.i_chroma_qp_offset
+= 6;
1259 /* Psy RDO increases overall quantizers to improve the quality of luma--this indirectly hurts chroma quality */
1260 /* so we lower the chroma QP offset to compensate */
1261 if( b_open
&& h
->mb
.i_psy_rd
&& !h
->param
.i_avcintra_class
)
1262 h
->param
.analyse
.i_chroma_qp_offset
-= h
->param
.analyse
.f_psy_rd
< 0.25 ? 1 : 2;
1263 /* Psy trellis has a similar effect. */
1264 if( b_open
&& h
->mb
.i_psy_trellis
&& !h
->param
.i_avcintra_class
)
1265 h
->param
.analyse
.i_chroma_qp_offset
-= h
->param
.analyse
.f_psy_trellis
< 0.25 ? 1 : 2;
1266 h
->param
.analyse
.i_chroma_qp_offset
= x264_clip3(h
->param
.analyse
.i_chroma_qp_offset
, -12, 12);
1267 /* MB-tree requires AQ to be on, even if the strength is zero. */
1268 if( !h
->param
.rc
.i_aq_mode
&& h
->param
.rc
.b_mb_tree
)
1270 h
->param
.rc
.i_aq_mode
= 1;
1271 h
->param
.rc
.f_aq_strength
= 0;
1273 h
->param
.analyse
.i_noise_reduction
= x264_clip3( h
->param
.analyse
.i_noise_reduction
, 0, 1<<16 );
1274 if( h
->param
.analyse
.i_subpel_refine
>= 10 && (h
->param
.analyse
.i_trellis
!= 2 || !h
->param
.rc
.i_aq_mode
) )
1275 h
->param
.analyse
.i_subpel_refine
= 9;
1279 const x264_level_t
*l
= x264_levels
;
1280 if( h
->param
.i_level_idc
< 0 )
1282 int maxrate_bak
= h
->param
.rc
.i_vbv_max_bitrate
;
1283 if( h
->param
.rc
.i_rc_method
== X264_RC_ABR
&& h
->param
.rc
.i_vbv_buffer_size
<= 0 )
1284 h
->param
.rc
.i_vbv_max_bitrate
= h
->param
.rc
.i_bitrate
* 2;
1285 x264_sps_init( h
->sps
, h
->param
.i_sps_id
, &h
->param
);
1286 do h
->param
.i_level_idc
= l
->level_idc
;
1287 while( l
[1].level_idc
&& x264_validate_levels( h
, 0 ) && l
++ );
1288 h
->param
.rc
.i_vbv_max_bitrate
= maxrate_bak
;
1292 while( l
->level_idc
&& l
->level_idc
!= h
->param
.i_level_idc
)
1294 if( l
->level_idc
== 0 )
1296 x264_log( h
, X264_LOG_ERROR
, "invalid level_idc: %d\n", h
->param
.i_level_idc
);
1300 if( h
->param
.analyse
.i_mv_range
<= 0 )
1301 h
->param
.analyse
.i_mv_range
= l
->mv_range
>> PARAM_INTERLACED
;
1303 h
->param
.analyse
.i_mv_range
= x264_clip3(h
->param
.analyse
.i_mv_range
, 32, 8192 >> PARAM_INTERLACED
);
1306 h
->param
.analyse
.i_weighted_pred
= x264_clip3( h
->param
.analyse
.i_weighted_pred
, X264_WEIGHTP_NONE
, X264_WEIGHTP_KMEAN
);
1308 if( h
->param
.i_lookahead_threads
== X264_THREADS_AUTO
)
1310 if( h
->param
.b_sliced_threads
)
1311 h
->param
.i_lookahead_threads
= h
->param
.i_threads
;
1314 /* If we're using much slower lookahead settings than encoding settings, it helps a lot to use
1315 * more lookahead threads. This typically happens in the first pass of a two-pass encode, so
1316 * try to guess at this sort of case.
1318 * Tuned by a little bit of real encoding with the various presets. */
1319 int badapt
= h
->param
.i_bframe_adaptive
== X264_B_ADAPT_TRELLIS
;
1320 int subme
= X264_MIN( h
->param
.analyse
.i_subpel_refine
/ 3, 3 ) + (h
->param
.analyse
.i_subpel_refine
> 1);
1321 int bframes
= X264_MIN( (h
->param
.i_bframe
- 1) / 3, 3 );
1323 /* [b-adapt 0/1 vs 2][quantized subme][quantized bframes] */
1324 static const uint8_t lookahead_thread_div
[2][5][4] =
1325 {{{6,6,6,6}, {3,3,3,3}, {4,4,4,4}, {6,6,6,6}, {12,12,12,12}},
1326 {{3,2,1,1}, {2,1,1,1}, {4,3,2,1}, {6,4,3,2}, {12, 9, 6, 4}}};
1328 h
->param
.i_lookahead_threads
= h
->param
.i_threads
/ lookahead_thread_div
[badapt
][subme
][bframes
];
1329 /* Since too many lookahead threads significantly degrades lookahead accuracy, limit auto
1330 * lookahead threads to about 8 macroblock rows high each at worst. This number is chosen
1331 * pretty much arbitrarily. */
1332 h
->param
.i_lookahead_threads
= X264_MIN( h
->param
.i_lookahead_threads
, h
->param
.i_height
/ 128 );
1335 h
->param
.i_lookahead_threads
= x264_clip3( h
->param
.i_lookahead_threads
, 1, X264_MIN( max_sliced_threads
, X264_LOOKAHEAD_THREAD_MAX
) );
1337 if( PARAM_INTERLACED
)
1339 if( h
->param
.analyse
.i_me_method
>= X264_ME_ESA
)
1341 x264_log( h
, X264_LOG_WARNING
, "interlace + me=esa is not implemented\n" );
1342 h
->param
.analyse
.i_me_method
= X264_ME_UMH
;
1344 if( h
->param
.analyse
.i_weighted_pred
> 0 )
1346 x264_log( h
, X264_LOG_WARNING
, "interlace + weightp is not implemented\n" );
1347 h
->param
.analyse
.i_weighted_pred
= X264_WEIGHTP_NONE
;
1351 if( !h
->param
.analyse
.i_weighted_pred
&& h
->param
.rc
.b_mb_tree
&& h
->param
.analyse
.b_psy
)
1352 h
->param
.analyse
.i_weighted_pred
= X264_WEIGHTP_FAKE
;
1354 if( h
->param
.analyse
.i_fgo
)
1356 if( h
->param
.analyse
.i_subpel_refine
< 7 )
1358 x264_log( h
, X264_LOG_WARNING
, "fgo requires subme >= 7\n" );
1359 h
->param
.analyse
.i_fgo
= 0;
1363 /* Arbitrary clipping. */
1364 h
->param
.analyse
.i_fgo
= x264_clip3( h
->param
.analyse
.i_fgo
, 0, 50 );
1365 /* P-skip's threshold isn't necessarily accurate when using NSSD/FGO */
1366 h
->param
.analyse
.b_fast_pskip
= 0;
1367 /* B-frame QPs need to be lower to retain grain */
1368 /* Arbitrary formula to scale pbratio based on fgo strength. */
1369 h
->param
.rc
.f_pb_factor
= 1 + (h
->param
.rc
.f_pb_factor
- 1) / pow(h
->param
.analyse
.i_fgo
, 0.3);
1373 if( h
->i_thread_frames
> 1 )
1375 int r
= h
->param
.analyse
.i_mv_range_thread
;
1379 // half of the available space is reserved and divided evenly among the threads,
1380 // the rest is allocated to whichever thread is far enough ahead to use it.
1381 // reserving more space increases quality for some videos, but costs more time
1382 // in thread synchronization.
1383 int max_range
= (h
->param
.i_height
+ X264_THREAD_HEIGHT
) / h
->i_thread_frames
- X264_THREAD_HEIGHT
;
1386 r
= X264_MAX( r
, h
->param
.analyse
.i_me_range
);
1387 r
= X264_MIN( r
, h
->param
.analyse
.i_mv_range
);
1388 // round up to use the whole mb row
1389 r2
= (r
& ~15) + ((-X264_THREAD_HEIGHT
) & 15);
1392 x264_log( h
, X264_LOG_DEBUG
, "using mv_range_thread = %d\n", r2
);
1393 h
->param
.analyse
.i_mv_range_thread
= r2
;
1396 if( h
->param
.rc
.f_rate_tolerance
< 0 )
1397 h
->param
.rc
.f_rate_tolerance
= 0;
1398 if( h
->param
.rc
.f_qblur
< 0 )
1399 h
->param
.rc
.f_qblur
= 0;
1400 if( h
->param
.rc
.f_complexity_blur
< 0 )
1401 h
->param
.rc
.f_complexity_blur
= 0;
1403 h
->param
.i_sps_id
&= 31;
1405 h
->param
.i_nal_hrd
= x264_clip3( h
->param
.i_nal_hrd
, X264_NAL_HRD_NONE
, X264_NAL_HRD_CBR
);
1407 if( h
->param
.i_nal_hrd
&& !h
->param
.rc
.i_vbv_buffer_size
)
1409 x264_log( h
, X264_LOG_WARNING
, "NAL HRD parameters require VBV parameters\n" );
1410 h
->param
.i_nal_hrd
= X264_NAL_HRD_NONE
;
1413 if( h
->param
.i_nal_hrd
== X264_NAL_HRD_CBR
&&
1414 (h
->param
.rc
.i_bitrate
!= h
->param
.rc
.i_vbv_max_bitrate
|| !h
->param
.rc
.i_vbv_max_bitrate
) )
1416 x264_log( h
, X264_LOG_WARNING
, "CBR HRD requires constant bitrate\n" );
1417 h
->param
.i_nal_hrd
= X264_NAL_HRD_VBR
;
1420 if( h
->param
.i_nal_hrd
== X264_NAL_HRD_CBR
)
1421 h
->param
.rc
.b_filler
= 1;
1423 /* ensure the booleans are 0 or 1 so they can be used in math */
1424 #define BOOLIFY(x) h->param.x = !!h->param.x
1426 BOOLIFY( b_constrained_intra
);
1427 BOOLIFY( b_deblocking_filter
);
1428 BOOLIFY( b_deterministic
);
1429 BOOLIFY( b_sliced_threads
);
1430 BOOLIFY( b_interlaced
);
1431 BOOLIFY( b_intra_refresh
);
1433 BOOLIFY( b_repeat_headers
);
1434 BOOLIFY( b_annexb
);
1435 BOOLIFY( b_vfr_input
);
1436 BOOLIFY( b_pulldown
);
1438 BOOLIFY( b_pic_struct
);
1439 BOOLIFY( b_fake_interlaced
);
1440 BOOLIFY( b_open_gop
);
1441 BOOLIFY( b_bluray_compat
);
1442 BOOLIFY( b_stitchable
);
1443 BOOLIFY( b_full_recon
);
1444 BOOLIFY( b_opencl
);
1445 BOOLIFY( analyse
.b_transform_8x8
);
1446 BOOLIFY( analyse
.b_weighted_bipred
);
1447 BOOLIFY( analyse
.b_chroma_me
);
1448 BOOLIFY( analyse
.b_mixed_references
);
1449 BOOLIFY( analyse
.b_fast_pskip
);
1450 BOOLIFY( analyse
.b_dct_decimate
);
1451 BOOLIFY( analyse
.b_psy
);
1452 BOOLIFY( analyse
.b_psnr
);
1453 BOOLIFY( analyse
.b_ssim
);
1454 BOOLIFY( rc
.b_stat_write
);
1455 BOOLIFY( rc
.b_stat_read
);
1456 BOOLIFY( rc
.b_mb_tree
);
1457 BOOLIFY( rc
.b_filler
);
1463 static void mbcmp_init( x264_t
*h
)
1465 int satd
= !h
->mb
.b_lossless
&& h
->param
.analyse
.i_subpel_refine
> 1;
1466 memcpy( h
->pixf
.mbcmp
, satd
? h
->pixf
.satd
: h
->pixf
.sad_aligned
, sizeof(h
->pixf
.mbcmp
) );
1467 memcpy( h
->pixf
.mbcmp_unaligned
, satd
? h
->pixf
.satd
: h
->pixf
.sad
, sizeof(h
->pixf
.mbcmp_unaligned
) );
1468 h
->pixf
.intra_mbcmp_x3_16x16
= satd
? h
->pixf
.intra_satd_x3_16x16
: h
->pixf
.intra_sad_x3_16x16
;
1469 h
->pixf
.intra_mbcmp_x3_8x16c
= satd
? h
->pixf
.intra_satd_x3_8x16c
: h
->pixf
.intra_sad_x3_8x16c
;
1470 h
->pixf
.intra_mbcmp_x3_8x8c
= satd
? h
->pixf
.intra_satd_x3_8x8c
: h
->pixf
.intra_sad_x3_8x8c
;
1471 h
->pixf
.intra_mbcmp_x3_8x8
= satd
? h
->pixf
.intra_sa8d_x3_8x8
: h
->pixf
.intra_sad_x3_8x8
;
1472 h
->pixf
.intra_mbcmp_x3_4x4
= satd
? h
->pixf
.intra_satd_x3_4x4
: h
->pixf
.intra_sad_x3_4x4
;
1473 h
->pixf
.intra_mbcmp_x9_4x4
= h
->param
.b_cpu_independent
|| h
->mb
.b_lossless
? NULL
1474 : satd
? h
->pixf
.intra_satd_x9_4x4
: h
->pixf
.intra_sad_x9_4x4
;
1475 h
->pixf
.intra_mbcmp_x9_8x8
= h
->param
.b_cpu_independent
|| h
->mb
.b_lossless
? NULL
1476 : satd
? h
->pixf
.intra_sa8d_x9_8x8
: h
->pixf
.intra_sad_x9_8x8
;
1477 satd
&= h
->param
.analyse
.i_me_method
== X264_ME_TESA
;
1478 memcpy( h
->pixf
.fpelcmp
, satd
? h
->pixf
.satd
: h
->pixf
.sad
, sizeof(h
->pixf
.fpelcmp
) );
1479 memcpy( h
->pixf
.fpelcmp_x3
, satd
? h
->pixf
.satd_x3
: h
->pixf
.sad_x3
, sizeof(h
->pixf
.fpelcmp_x3
) );
1480 memcpy( h
->pixf
.fpelcmp_x4
, satd
? h
->pixf
.satd_x4
: h
->pixf
.sad_x4
, sizeof(h
->pixf
.fpelcmp_x4
) );
1481 memcpy( h
->pixf
.rdcmp
, h
->param
.analyse
.i_fgo
? h
->pixf
.nssd
: h
->pixf
.ssd
, sizeof(h
->pixf
.rdcmp
) );
1484 static void chroma_dsp_init( x264_t
*h
)
1486 memcpy( h
->luma2chroma_pixel
, x264_luma2chroma_pixel
[CHROMA_FORMAT
], sizeof(h
->luma2chroma_pixel
) );
1488 switch( CHROMA_FORMAT
)
1491 memcpy( h
->predict_chroma
, h
->predict_8x8c
, sizeof(h
->predict_chroma
) );
1492 h
->mc
.prefetch_fenc
= h
->mc
.prefetch_fenc_420
;
1493 h
->loopf
.deblock_chroma
[0] = h
->loopf
.deblock_h_chroma_420
;
1494 h
->loopf
.deblock_chroma_intra
[0] = h
->loopf
.deblock_h_chroma_420_intra
;
1495 h
->loopf
.deblock_chroma_mbaff
= h
->loopf
.deblock_chroma_420_mbaff
;
1496 h
->loopf
.deblock_chroma_intra_mbaff
= h
->loopf
.deblock_chroma_420_intra_mbaff
;
1497 h
->pixf
.intra_mbcmp_x3_chroma
= h
->pixf
.intra_mbcmp_x3_8x8c
;
1498 h
->quantf
.coeff_last
[DCT_CHROMA_DC
] = h
->quantf
.coeff_last4
;
1499 h
->quantf
.coeff_level_run
[DCT_CHROMA_DC
] = h
->quantf
.coeff_level_run4
;
1502 memcpy( h
->predict_chroma
, h
->predict_8x16c
, sizeof(h
->predict_chroma
) );
1503 h
->mc
.prefetch_fenc
= h
->mc
.prefetch_fenc_422
;
1504 h
->loopf
.deblock_chroma
[0] = h
->loopf
.deblock_h_chroma_422
;
1505 h
->loopf
.deblock_chroma_intra
[0] = h
->loopf
.deblock_h_chroma_422_intra
;
1506 h
->loopf
.deblock_chroma_mbaff
= h
->loopf
.deblock_chroma_422_mbaff
;
1507 h
->loopf
.deblock_chroma_intra_mbaff
= h
->loopf
.deblock_chroma_422_intra_mbaff
;
1508 h
->pixf
.intra_mbcmp_x3_chroma
= h
->pixf
.intra_mbcmp_x3_8x16c
;
1509 h
->quantf
.coeff_last
[DCT_CHROMA_DC
] = h
->quantf
.coeff_last8
;
1510 h
->quantf
.coeff_level_run
[DCT_CHROMA_DC
] = h
->quantf
.coeff_level_run8
;
1513 h
->mc
.prefetch_fenc
= h
->mc
.prefetch_fenc_422
; /* FIXME: doesn't cover V plane */
1514 h
->loopf
.deblock_chroma_mbaff
= h
->loopf
.deblock_luma_mbaff
;
1515 h
->loopf
.deblock_chroma_intra_mbaff
= h
->loopf
.deblock_luma_intra_mbaff
;
1520 static void x264_set_aspect_ratio( x264_t
*h
, x264_param_t
*param
, int initial
)
1523 if( param
->vui
.i_sar_width
> 0 && param
->vui
.i_sar_height
> 0 )
1525 uint32_t i_w
= param
->vui
.i_sar_width
;
1526 uint32_t i_h
= param
->vui
.i_sar_height
;
1527 uint32_t old_w
= h
->param
.vui
.i_sar_width
;
1528 uint32_t old_h
= h
->param
.vui
.i_sar_height
;
1530 x264_reduce_fraction( &i_w
, &i_h
);
1532 while( i_w
> 65535 || i_h
> 65535 )
1538 x264_reduce_fraction( &i_w
, &i_h
);
1540 if( i_w
!= old_w
|| i_h
!= old_h
|| initial
)
1542 h
->param
.vui
.i_sar_width
= 0;
1543 h
->param
.vui
.i_sar_height
= 0;
1544 if( i_w
== 0 || i_h
== 0 )
1545 x264_log( h
, X264_LOG_WARNING
, "cannot create valid sample aspect ratio\n" );
1548 x264_log( h
, initial
?X264_LOG_INFO
:X264_LOG_DEBUG
, "using SAR=%d/%d\n", i_w
, i_h
);
1549 h
->param
.vui
.i_sar_width
= i_w
;
1550 h
->param
.vui
.i_sar_height
= i_h
;
1556 /****************************************************************************
1557 * x264_encoder_open:
1558 ****************************************************************************/
1559 x264_t
*x264_encoder_open( x264_param_t
*param
)
1563 int i_slicetype_length
;
1565 CHECKED_MALLOCZERO( h
, sizeof(x264_t
) );
1567 /* Create a copy of param */
1568 memcpy( &h
->param
, param
, sizeof(x264_param_t
) );
1570 if( param
->param_free
)
1571 param
->param_free( param
);
1573 #if HAVE_INTEL_DISPATCHER
1574 x264_intel_dispatcher_override();
1577 if( x264_threading_init() )
1579 x264_log( h
, X264_LOG_ERROR
, "unable to initialize threading\n" );
1583 if( x264_validate_parameters( h
, 1 ) < 0 )
1586 if( h
->param
.psz_cqm_file
)
1587 if( x264_cqm_parse_file( h
, h
->param
.psz_cqm_file
) < 0 )
1590 if( h
->param
.rc
.psz_stat_out
)
1591 h
->param
.rc
.psz_stat_out
= strdup( h
->param
.rc
.psz_stat_out
);
1592 if( h
->param
.rc
.psz_stat_in
)
1593 h
->param
.rc
.psz_stat_in
= strdup( h
->param
.rc
.psz_stat_in
);
1595 x264_reduce_fraction( &h
->param
.i_fps_num
, &h
->param
.i_fps_den
);
1596 x264_reduce_fraction( &h
->param
.i_timebase_num
, &h
->param
.i_timebase_den
);
1602 if( h
->param
.i_avcintra_class
)
1603 h
->i_idr_pic_id
= 5;
1605 h
->i_idr_pic_id
= 0;
1607 if( (uint64_t)h
->param
.i_timebase_den
* 2 > UINT32_MAX
)
1609 x264_log( h
, X264_LOG_ERROR
, "Effective timebase denominator %u exceeds H.264 maximum\n", h
->param
.i_timebase_den
);
1613 x264_set_aspect_ratio( h
, &h
->param
, 1 );
1615 x264_sps_init( h
->sps
, h
->param
.i_sps_id
, &h
->param
);
1616 x264_pps_init( h
->pps
, h
->param
.i_sps_id
, &h
->param
, h
->sps
);
1618 x264_validate_levels( h
, 1 );
1620 h
->chroma_qp_table
= i_chroma_qp_table
+ 12 + h
->pps
->i_chroma_qp_index_offset
;
1622 if( x264_cqm_init( h
) < 0 )
1625 h
->mb
.i_mb_width
= h
->sps
->i_mb_width
;
1626 h
->mb
.i_mb_height
= h
->sps
->i_mb_height
;
1627 h
->mb
.i_mb_count
= h
->mb
.i_mb_width
* h
->mb
.i_mb_height
;
1629 h
->mb
.chroma_h_shift
= CHROMA_FORMAT
== CHROMA_420
|| CHROMA_FORMAT
== CHROMA_422
;
1630 h
->mb
.chroma_v_shift
= CHROMA_FORMAT
== CHROMA_420
;
1632 /* Adaptive MBAFF and subme 0 are not supported as we require halving motion
1633 * vectors during prediction, resulting in hpel mvs.
1634 * The chosen solution is to make MBAFF non-adaptive in this case. */
1635 h
->mb
.b_adaptive_mbaff
= PARAM_INTERLACED
&& h
->param
.analyse
.i_subpel_refine
;
1638 if( h
->param
.i_bframe_adaptive
== X264_B_ADAPT_TRELLIS
&& !h
->param
.rc
.b_stat_read
)
1639 h
->frames
.i_delay
= X264_MAX(h
->param
.i_bframe
,3)*4;
1641 h
->frames
.i_delay
= h
->param
.i_bframe
;
1642 if( h
->param
.rc
.b_mb_tree
|| h
->param
.rc
.i_vbv_buffer_size
)
1643 h
->frames
.i_delay
= X264_MAX( h
->frames
.i_delay
, h
->param
.rc
.i_lookahead
);
1644 i_slicetype_length
= h
->frames
.i_delay
;
1645 h
->frames
.i_delay
+= h
->i_thread_frames
- 1;
1646 h
->frames
.i_delay
+= h
->param
.i_sync_lookahead
;
1647 h
->frames
.i_delay
+= h
->param
.b_vfr_input
;
1648 h
->frames
.i_bframe_delay
= h
->param
.i_bframe
? (h
->param
.i_bframe_pyramid
? 2 : 1) : 0;
1650 h
->frames
.i_max_ref0
= h
->param
.i_frame_reference
;
1651 h
->frames
.i_max_ref1
= X264_MIN( h
->sps
->vui
.i_num_reorder_frames
, h
->param
.i_frame_reference
);
1652 h
->frames
.i_max_dpb
= h
->sps
->vui
.i_max_dec_frame_buffering
;
1653 h
->frames
.b_have_lowres
= !h
->param
.rc
.b_stat_read
1654 && ( h
->param
.rc
.i_rc_method
== X264_RC_ABR
1655 || h
->param
.rc
.i_rc_method
== X264_RC_CRF
1656 || h
->param
.i_bframe_adaptive
1657 || h
->param
.i_scenecut_threshold
1658 || h
->param
.rc
.b_mb_tree
1659 || h
->param
.analyse
.i_weighted_pred
);
1660 h
->frames
.b_have_lowres
|= h
->param
.rc
.b_stat_read
&& h
->param
.rc
.i_vbv_buffer_size
> 0;
1661 h
->frames
.b_have_sub8x8_esa
= !!(h
->param
.analyse
.inter
& X264_ANALYSE_PSUB8x8
);
1663 h
->frames
.i_last_idr
=
1664 h
->frames
.i_last_keyframe
= - h
->param
.i_keyint_max
;
1665 h
->frames
.i_input
= 0;
1666 h
->frames
.i_largest_pts
= h
->frames
.i_second_largest_pts
= -1;
1667 h
->frames
.i_poc_last_open_gop
= -1;
1669 CHECKED_MALLOCZERO( h
->frames
.unused
[0], (h
->frames
.i_delay
+ 3) * sizeof(x264_frame_t
*) );
1670 /* Allocate room for max refs plus a few extra just in case. */
1671 CHECKED_MALLOCZERO( h
->frames
.unused
[1], (h
->i_thread_frames
+ X264_REF_MAX
+ 4) * sizeof(x264_frame_t
*) );
1672 CHECKED_MALLOCZERO( h
->frames
.current
, (h
->param
.i_sync_lookahead
+ h
->param
.i_bframe
1673 + h
->i_thread_frames
+ 3) * sizeof(x264_frame_t
*) );
1674 if( h
->param
.analyse
.i_weighted_pred
> 0 )
1675 CHECKED_MALLOCZERO( h
->frames
.blank_unused
, h
->i_thread_frames
* (X264_DUPS_MAX
+ 4) * sizeof(x264_frame_t
*) );
1676 h
->i_ref
[0] = h
->i_ref
[1] = 0;
1677 h
->i_cpb_delay
= h
->i_coded_fields
= h
->i_disp_fields
= 0;
1678 h
->i_prev_duration
= ((uint64_t)h
->param
.i_fps_den
* h
->sps
->vui
.i_time_scale
) / ((uint64_t)h
->param
.i_fps_num
* h
->sps
->vui
.i_num_units_in_tick
);
1679 h
->i_disp_fields_last_frame
= -1;
1682 /* init CPU functions */
1683 #if (ARCH_X86 || ARCH_X86_64) && HIGH_BIT_DEPTH
1684 /* FIXME: Only 8-bit has been optimized for AVX-512 so far. The few AVX-512 functions
1685 * enabled in high bit-depth are insignificant and just causes potential issues with
1686 * unnecessary thermal throttling and whatnot, so keep it disabled for now. */
1687 h
->param
.cpu
&= ~X264_CPU_AVX512
;
1689 x264_predict_16x16_init( h
->param
.cpu
, h
->predict_16x16
);
1690 x264_predict_8x8c_init( h
->param
.cpu
, h
->predict_8x8c
);
1691 x264_predict_8x16c_init( h
->param
.cpu
, h
->predict_8x16c
);
1692 x264_predict_8x8_init( h
->param
.cpu
, h
->predict_8x8
, &h
->predict_8x8_filter
);
1693 x264_predict_4x4_init( h
->param
.cpu
, h
->predict_4x4
);
1694 x264_pixel_init( h
->param
.cpu
, &h
->pixf
);
1695 x264_dct_init( h
->param
.cpu
, &h
->dctf
);
1696 x264_zigzag_init( h
->param
.cpu
, &h
->zigzagf_progressive
, &h
->zigzagf_interlaced
);
1697 memcpy( &h
->zigzagf
, PARAM_INTERLACED
? &h
->zigzagf_interlaced
: &h
->zigzagf_progressive
, sizeof(h
->zigzagf
) );
1698 x264_mc_init( h
->param
.cpu
, &h
->mc
, h
->param
.b_cpu_independent
);
1699 x264_quant_init( h
, h
->param
.cpu
, &h
->quantf
);
1700 x264_deblock_init( h
->param
.cpu
, &h
->loopf
, PARAM_INTERLACED
);
1701 x264_bitstream_init( h
->param
.cpu
, &h
->bsf
);
1702 if( h
->param
.b_cabac
)
1703 x264_cabac_init( h
);
1705 x264_stack_align( x264_cavlc_init
, h
);
1708 chroma_dsp_init( h
);
1710 p
= buf
+ sprintf( buf
, "using cpu capabilities:" );
1711 for( int i
= 0; x264_cpu_names
[i
].flags
; i
++ )
1713 if( !strcmp(x264_cpu_names
[i
].name
, "SSE")
1714 && h
->param
.cpu
& (X264_CPU_SSE2
) )
1716 if( !strcmp(x264_cpu_names
[i
].name
, "SSE2")
1717 && h
->param
.cpu
& (X264_CPU_SSE2_IS_FAST
|X264_CPU_SSE2_IS_SLOW
) )
1719 if( !strcmp(x264_cpu_names
[i
].name
, "SSE3")
1720 && (h
->param
.cpu
& X264_CPU_SSSE3
|| !(h
->param
.cpu
& X264_CPU_CACHELINE_64
)) )
1722 if( !strcmp(x264_cpu_names
[i
].name
, "SSE4.1")
1723 && (h
->param
.cpu
& X264_CPU_SSE42
) )
1725 if( !strcmp(x264_cpu_names
[i
].name
, "LZCNT")
1726 && (h
->param
.cpu
& X264_CPU_BMI1
) )
1728 if( !strcmp(x264_cpu_names
[i
].name
, "BMI1")
1729 && (h
->param
.cpu
& X264_CPU_BMI2
) )
1731 if( !strcmp(x264_cpu_names
[i
].name
, "FMA4")
1732 && (h
->param
.cpu
& X264_CPU_FMA3
) )
1734 if( (h
->param
.cpu
& x264_cpu_names
[i
].flags
) == x264_cpu_names
[i
].flags
1735 && (!i
|| x264_cpu_names
[i
].flags
!= x264_cpu_names
[i
-1].flags
) )
1736 p
+= sprintf( p
, " %s", x264_cpu_names
[i
].name
);
1739 p
+= sprintf( p
, " none!" );
1740 x264_log( h
, X264_LOG_INFO
, "%s\n", buf
);
1742 if( x264_analyse_init_costs( h
) )
1745 /* Must be volatile or else GCC will optimize it out. */
1746 volatile int temp
= 392;
1747 if( x264_clz( temp
) != 23 )
1749 x264_log( h
, X264_LOG_ERROR
, "CLZ test failed: x264 has been miscompiled!\n" );
1750 #if ARCH_X86 || ARCH_X86_64
1751 x264_log( h
, X264_LOG_ERROR
, "Are you attempting to run an SSE4a/LZCNT-targeted build on a CPU that\n" );
1752 x264_log( h
, X264_LOG_ERROR
, "doesn't support it?\n" );
1758 h
->out
.i_bitstream
= X264_MAX( 1000000, h
->param
.i_width
* h
->param
.i_height
* 4
1759 * ( h
->param
.rc
.i_rc_method
== X264_RC_ABR
? pow( 0.95, h
->param
.rc
.i_qp_min_min
)
1760 : pow( 0.95, h
->param
.rc
.i_qp_constant
) * X264_MAX( 1, h
->param
.rc
.f_ip_factor
)));
1762 h
->nal_buffer_size
= h
->out
.i_bitstream
* 3/2 + 4 + 64; /* +4 for startcode, +64 for nal_escape assembly padding */
1763 CHECKED_MALLOC( h
->nal_buffer
, h
->nal_buffer_size
);
1765 CHECKED_MALLOC( h
->reconfig_h
, sizeof(x264_t
) );
1767 if( h
->param
.i_threads
> 1 &&
1768 x264_threadpool_init( &h
->threadpool
, h
->param
.i_threads
, (void*)x264_encoder_thread_init
, h
) )
1770 if( h
->param
.i_lookahead_threads
> 1 &&
1771 x264_threadpool_init( &h
->lookaheadpool
, h
->param
.i_lookahead_threads
, NULL
, NULL
) )
1775 if( h
->param
.b_opencl
)
1777 h
->opencl
.ocl
= x264_opencl_load_library();
1778 if( !h
->opencl
.ocl
)
1780 x264_log( h
, X264_LOG_WARNING
, "failed to load OpenCL\n" );
1781 h
->param
.b_opencl
= 0;
1787 for( int i
= 1; i
< h
->param
.i_threads
+ !!h
->param
.i_sync_lookahead
; i
++ )
1788 CHECKED_MALLOC( h
->thread
[i
], sizeof(x264_t
) );
1789 if( h
->param
.i_lookahead_threads
> 1 )
1790 for( int i
= 0; i
< h
->param
.i_lookahead_threads
; i
++ )
1792 CHECKED_MALLOC( h
->lookahead_thread
[i
], sizeof(x264_t
) );
1793 *h
->lookahead_thread
[i
] = *h
;
1795 *h
->reconfig_h
= *h
;
1797 for( int i
= 0; i
< h
->param
.i_threads
; i
++ )
1799 int init_nal_count
= h
->param
.i_slice_count
+ 3;
1800 int allocate_threadlocal_data
= !h
->param
.b_sliced_threads
|| !i
;
1804 if( x264_pthread_mutex_init( &h
->thread
[i
]->mutex
, NULL
) )
1806 if( x264_pthread_cond_init( &h
->thread
[i
]->cv
, NULL
) )
1809 if( allocate_threadlocal_data
)
1811 h
->thread
[i
]->fdec
= x264_frame_pop_unused( h
, 1 );
1812 if( !h
->thread
[i
]->fdec
)
1816 h
->thread
[i
]->fdec
= h
->thread
[0]->fdec
;
1818 CHECKED_MALLOC( h
->thread
[i
]->out
.p_bitstream
, h
->out
.i_bitstream
);
1819 /* Start each thread with room for init_nal_count NAL units; it'll realloc later if needed. */
1820 CHECKED_MALLOC( h
->thread
[i
]->out
.nal
, init_nal_count
*sizeof(x264_nal_t
) );
1821 h
->thread
[i
]->out
.i_nals_allocated
= init_nal_count
;
1823 if( allocate_threadlocal_data
&& x264_macroblock_cache_allocate( h
->thread
[i
] ) < 0 )
1828 if( h
->param
.b_opencl
&& x264_opencl_lookahead_init( h
) < 0 )
1829 h
->param
.b_opencl
= 0;
1832 if( x264_lookahead_init( h
, i_slicetype_length
) )
1835 for( int i
= 0; i
< h
->param
.i_threads
; i
++ )
1836 if( x264_macroblock_thread_allocate( h
->thread
[i
], 0 ) < 0 )
1839 if( x264_ratecontrol_new( h
) < 0 )
1842 if( h
->param
.i_nal_hrd
)
1844 x264_log( h
, X264_LOG_DEBUG
, "HRD bitrate: %i bits/sec\n", h
->sps
->vui
.hrd
.i_bit_rate_unscaled
);
1845 x264_log( h
, X264_LOG_DEBUG
, "CPB size: %i bits\n", h
->sps
->vui
.hrd
.i_cpb_size_unscaled
);
1848 if( h
->param
.psz_dump_yuv
)
1850 /* create or truncate the reconstructed video file */
1851 FILE *f
= x264_fopen( h
->param
.psz_dump_yuv
, "w" );
1854 x264_log( h
, X264_LOG_ERROR
, "dump_yuv: can't write to %s\n", h
->param
.psz_dump_yuv
);
1857 else if( !x264_is_regular_file( f
) )
1859 x264_log( h
, X264_LOG_ERROR
, "dump_yuv: incompatible with non-regular file %s\n", h
->param
.psz_dump_yuv
);
1866 const char *profile
= h
->sps
->i_profile_idc
== PROFILE_BASELINE
? "Constrained Baseline" :
1867 h
->sps
->i_profile_idc
== PROFILE_MAIN
? "Main" :
1868 h
->sps
->i_profile_idc
== PROFILE_HIGH
? "High" :
1869 h
->sps
->i_profile_idc
== PROFILE_HIGH10
? (h
->sps
->b_constraint_set3
== 1 ? "High 10 Intra" : "High 10") :
1870 h
->sps
->i_profile_idc
== PROFILE_HIGH422
? (h
->sps
->b_constraint_set3
== 1 ? "High 4:2:2 Intra" : "High 4:2:2") :
1871 h
->sps
->b_constraint_set3
== 1 ? "High 4:4:4 Intra" : "High 4:4:4 Predictive";
1873 snprintf( level
, sizeof(level
), "%d.%d", h
->sps
->i_level_idc
/10, h
->sps
->i_level_idc
%10 );
1874 if( h
->sps
->i_level_idc
== 9 || ( h
->sps
->i_level_idc
== 11 && h
->sps
->b_constraint_set3
&&
1875 (h
->sps
->i_profile_idc
== PROFILE_BASELINE
|| h
->sps
->i_profile_idc
== PROFILE_MAIN
) ) )
1876 strcpy( level
, "1b" );
1878 static const char * const subsampling
[4] = { "4:0:0", "4:2:0", "4:2:2", "4:4:4" };
1879 x264_log( h
, X264_LOG_INFO
, "AVC Encoder x264 core %d%s\n", X264_BUILD
, X264_VERSION
);
1880 x264_log( h
, X264_LOG_INFO
, "profile: %s, level: %s, subsampling: %s, bit-depth: %d-bit\n",
1881 profile
, level
, subsampling
[CHROMA_FORMAT
], BIT_DEPTH
);
1883 char *opts
= x264_param2string( &h
->param
, 0 );
1886 x264_log( h
, X264_LOG_INFO
, "%s\n", opts
);
1896 /****************************************************************************/
1897 static int x264_encoder_try_reconfig( x264_t
*h
, x264_param_t
*param
, int *rc_reconfig
)
1900 x264_set_aspect_ratio( h
, param
, 0 );
1901 #define COPY(var) h->param.var = param->var
1902 COPY( i_frame_reference
); // but never uses more refs than initially specified
1903 COPY( i_bframe_bias
);
1904 if( h
->param
.i_scenecut_threshold
)
1905 COPY( i_scenecut_threshold
); // can't turn it on or off, only vary the threshold
1906 COPY( b_deblocking_filter
);
1907 COPY( i_deblocking_filter_alphac0
);
1908 COPY( i_deblocking_filter_beta
);
1909 COPY( i_frame_packing
);
1910 COPY( analyse
.inter
);
1911 COPY( analyse
.intra
);
1912 COPY( analyse
.i_direct_mv_pred
);
1913 /* Scratch buffer prevents me_range from being increased for esa/tesa */
1914 if( h
->param
.analyse
.i_me_method
< X264_ME_ESA
|| param
->analyse
.i_me_range
< h
->param
.analyse
.i_me_range
)
1915 COPY( analyse
.i_me_range
);
1916 COPY( analyse
.i_noise_reduction
);
1917 /* We can't switch out of subme=0 during encoding. */
1918 if( h
->param
.analyse
.i_subpel_refine
)
1919 COPY( analyse
.i_subpel_refine
);
1920 COPY( analyse
.i_trellis
);
1921 COPY( analyse
.b_chroma_me
);
1922 COPY( analyse
.b_dct_decimate
);
1923 COPY( analyse
.b_fast_pskip
);
1924 COPY( analyse
.b_mixed_references
);
1925 COPY( analyse
.f_psy_rd
);
1926 COPY( analyse
.f_psy_trellis
);
1927 COPY( analyse
.i_fgo
);
1929 // can only twiddle these if they were enabled to begin with:
1930 if( h
->param
.analyse
.i_me_method
>= X264_ME_ESA
|| param
->analyse
.i_me_method
< X264_ME_ESA
)
1931 COPY( analyse
.i_me_method
);
1932 if( h
->param
.analyse
.i_me_method
>= X264_ME_ESA
&& !h
->frames
.b_have_sub8x8_esa
)
1933 h
->param
.analyse
.inter
&= ~X264_ANALYSE_PSUB8x8
;
1934 if( h
->pps
->b_transform_8x8_mode
)
1935 COPY( analyse
.b_transform_8x8
);
1936 if( h
->frames
.i_max_ref1
> 1 )
1937 COPY( i_bframe_pyramid
);
1938 COPY( i_slice_max_size
);
1939 COPY( i_slice_max_mbs
);
1940 COPY( i_slice_min_mbs
);
1941 COPY( i_slice_count
);
1942 COPY( i_slice_count_max
);
1945 /* VBV can't be turned on if it wasn't on to begin with */
1946 if( h
->param
.rc
.i_vbv_max_bitrate
> 0 && h
->param
.rc
.i_vbv_buffer_size
> 0 &&
1947 param
->rc
.i_vbv_max_bitrate
> 0 && param
->rc
.i_vbv_buffer_size
> 0 )
1949 *rc_reconfig
|= h
->param
.rc
.i_vbv_max_bitrate
!= param
->rc
.i_vbv_max_bitrate
;
1950 *rc_reconfig
|= h
->param
.rc
.i_vbv_buffer_size
!= param
->rc
.i_vbv_buffer_size
;
1951 *rc_reconfig
|= h
->param
.rc
.i_bitrate
!= param
->rc
.i_bitrate
;
1952 COPY( rc
.i_vbv_max_bitrate
);
1953 COPY( rc
.i_vbv_buffer_size
);
1954 COPY( rc
.i_bitrate
);
1956 *rc_reconfig
|= h
->param
.rc
.f_rf_constant
!= param
->rc
.f_rf_constant
;
1957 *rc_reconfig
|= h
->param
.rc
.f_rf_constant_max
!= param
->rc
.f_rf_constant_max
;
1958 COPY( rc
.f_rf_constant
);
1959 COPY( rc
.f_rf_constant_max
);
1962 return x264_validate_parameters( h
, 0 );
1965 int x264_encoder_reconfig_apply( x264_t
*h
, x264_param_t
*param
)
1968 int ret
= x264_encoder_try_reconfig( h
, param
, &rc_reconfig
);
1972 x264_sps_init_reconfigurable( h
->sps
, &h
->param
);
1974 /* Supported reconfiguration options (1-pass only):
1978 * bitrate (CBR only) */
1979 if( !ret
&& rc_reconfig
)
1980 x264_ratecontrol_init_reconfigurable( h
, 0 );
1985 /****************************************************************************
1986 * x264_encoder_reconfig:
1987 ****************************************************************************/
1988 int x264_encoder_reconfig( x264_t
*h
, x264_param_t
*param
)
1990 h
= h
->thread
[h
->thread
[0]->i_thread_phase
];
1991 x264_param_t param_save
= h
->reconfig_h
->param
;
1992 h
->reconfig_h
->param
= h
->param
;
1995 int ret
= x264_encoder_try_reconfig( h
->reconfig_h
, param
, &rc_reconfig
);
1999 h
->reconfig_h
->param
= param_save
;
2004 /****************************************************************************
2005 * x264_encoder_parameters:
2006 ****************************************************************************/
2007 void x264_encoder_parameters( x264_t
*h
, x264_param_t
*param
)
2009 memcpy( param
, &h
->thread
[h
->i_thread_phase
]->param
, sizeof(x264_param_t
) );
2012 /* internal usage */
2013 static void x264_nal_start( x264_t
*h
, int i_type
, int i_ref_idc
)
2015 x264_nal_t
*nal
= &h
->out
.nal
[h
->out
.i_nal
];
2017 nal
->i_ref_idc
= i_ref_idc
;
2018 nal
->i_type
= i_type
;
2019 nal
->b_long_startcode
= 1;
2022 nal
->p_payload
= &h
->out
.p_bitstream
[bs_pos( &h
->out
.bs
) / 8];
2026 /* if number of allocated nals is not enough, re-allocate a larger one. */
2027 static int x264_nal_check_buffer( x264_t
*h
)
2029 if( h
->out
.i_nal
>= h
->out
.i_nals_allocated
)
2031 x264_nal_t
*new_out
= x264_malloc( sizeof(x264_nal_t
) * (h
->out
.i_nals_allocated
*2) );
2034 memcpy( new_out
, h
->out
.nal
, sizeof(x264_nal_t
) * (h
->out
.i_nals_allocated
) );
2035 x264_free( h
->out
.nal
);
2036 h
->out
.nal
= new_out
;
2037 h
->out
.i_nals_allocated
*= 2;
2042 static int x264_nal_end( x264_t
*h
)
2044 x264_nal_t
*nal
= &h
->out
.nal
[h
->out
.i_nal
];
2045 uint8_t *end
= &h
->out
.p_bitstream
[bs_pos( &h
->out
.bs
) / 8];
2046 nal
->i_payload
= end
- nal
->p_payload
;
2047 /* Assembly implementation of nal_escape reads past the end of the input.
2048 * While undefined padding wouldn't actually affect the output, it makes valgrind unhappy. */
2049 memset( end
, 0xff, 64 );
2050 if( h
->param
.nalu_process
)
2051 h
->param
.nalu_process( h
, nal
, h
->fenc
->opaque
);
2054 return x264_nal_check_buffer( h
);
2057 static int x264_check_encapsulated_buffer( x264_t
*h
, x264_t
*h0
, int start
,
2058 int previous_nal_size
, int necessary_size
)
2060 if( h0
->nal_buffer_size
< necessary_size
)
2062 necessary_size
*= 2;
2063 uint8_t *buf
= x264_malloc( necessary_size
);
2066 if( previous_nal_size
)
2067 memcpy( buf
, h0
->nal_buffer
, previous_nal_size
);
2069 intptr_t delta
= buf
- h0
->nal_buffer
;
2070 for( int i
= 0; i
< start
; i
++ )
2071 h
->out
.nal
[i
].p_payload
+= delta
;
2073 x264_free( h0
->nal_buffer
);
2074 h0
->nal_buffer
= buf
;
2075 h0
->nal_buffer_size
= necessary_size
;
2081 static int x264_encoder_encapsulate_nals( x264_t
*h
, int start
)
2083 x264_t
*h0
= h
->thread
[0];
2084 int nal_size
= 0, previous_nal_size
= 0;
2086 if( h
->param
.nalu_process
)
2088 for( int i
= start
; i
< h
->out
.i_nal
; i
++ )
2089 nal_size
+= h
->out
.nal
[i
].i_payload
;
2093 for( int i
= 0; i
< start
; i
++ )
2094 previous_nal_size
+= h
->out
.nal
[i
].i_payload
;
2096 for( int i
= start
; i
< h
->out
.i_nal
; i
++ )
2097 nal_size
+= h
->out
.nal
[i
].i_payload
;
2099 /* Worst-case NAL unit escaping: reallocate the buffer if it's too small. */
2100 int necessary_size
= previous_nal_size
+ nal_size
* 3/2 + h
->out
.i_nal
* 4 + 4 + 64;
2101 for( int i
= start
; i
< h
->out
.i_nal
; i
++ )
2102 necessary_size
+= h
->out
.nal
[i
].i_padding
;
2103 if( x264_check_encapsulated_buffer( h
, h0
, start
, previous_nal_size
, necessary_size
) )
2106 uint8_t *nal_buffer
= h0
->nal_buffer
+ previous_nal_size
;
2108 for( int i
= start
; i
< h
->out
.i_nal
; i
++ )
2110 h
->out
.nal
[i
].b_long_startcode
= !i
|| h
->out
.nal
[i
].i_type
== NAL_SPS
|| h
->out
.nal
[i
].i_type
== NAL_PPS
||
2111 h
->param
.i_avcintra_class
;
2112 x264_nal_encode( h
, nal_buffer
, &h
->out
.nal
[i
] );
2113 nal_buffer
+= h
->out
.nal
[i
].i_payload
;
2118 return nal_buffer
- (h0
->nal_buffer
+ previous_nal_size
);
2121 /****************************************************************************
2122 * x264_encoder_headers:
2123 ****************************************************************************/
2124 int x264_encoder_headers( x264_t
*h
, x264_nal_t
**pp_nal
, int *pi_nal
)
2127 /* init bitstream context */
2129 bs_init( &h
->out
.bs
, h
->out
.p_bitstream
, h
->out
.i_bitstream
);
2131 /* Write SEI, SPS and PPS. */
2133 /* generate sequence parameters */
2134 x264_nal_start( h
, NAL_SPS
, NAL_PRIORITY_HIGHEST
);
2135 x264_sps_write( &h
->out
.bs
, h
->sps
);
2136 if( x264_nal_end( h
) )
2139 /* generate picture parameters */
2140 x264_nal_start( h
, NAL_PPS
, NAL_PRIORITY_HIGHEST
);
2141 x264_pps_write( &h
->out
.bs
, h
->sps
, h
->pps
);
2142 if( x264_nal_end( h
) )
2145 /* identify ourselves */
2146 x264_nal_start( h
, NAL_SEI
, NAL_PRIORITY_DISPOSABLE
);
2147 if( x264_sei_version_write( h
, &h
->out
.bs
) )
2149 if( x264_nal_end( h
) )
2152 frame_size
= x264_encoder_encapsulate_nals( h
, 0 );
2153 if( frame_size
< 0 )
2157 *pi_nal
= h
->out
.i_nal
;
2158 *pp_nal
= &h
->out
.nal
[0];
2164 /* Check to see whether we have chosen a reference list ordering different
2165 * from the standard's default. */
2166 static inline void x264_reference_check_reorder( x264_t
*h
)
2168 /* The reorder check doesn't check for missing frames, so just
2169 * force a reorder if one of the reference list is corrupt. */
2170 for( int i
= 0; h
->frames
.reference
[i
]; i
++ )
2171 if( h
->frames
.reference
[i
]->b_corrupt
)
2173 h
->b_ref_reorder
[0] = 1;
2176 for( int list
= 0; list
<= (h
->sh
.i_type
== SLICE_TYPE_B
); list
++ )
2177 for( int i
= 0; i
< h
->i_ref
[list
] - 1; i
++ )
2179 int framenum_diff
= h
->fref
[list
][i
+1]->i_frame_num
- h
->fref
[list
][i
]->i_frame_num
;
2180 int poc_diff
= h
->fref
[list
][i
+1]->i_poc
- h
->fref
[list
][i
]->i_poc
;
2181 /* P and B-frames use different default orders. */
2182 if( h
->sh
.i_type
== SLICE_TYPE_P
? framenum_diff
> 0 : list
== 1 ? poc_diff
< 0 : poc_diff
> 0 )
2184 h
->b_ref_reorder
[list
] = 1;
2190 /* return -1 on failure, else return the index of the new reference frame */
2191 static int x264_weighted_reference_duplicate( x264_t
*h
, int i_ref
, const x264_weight_t
*w
)
2193 int i
= h
->i_ref
[0];
2195 x264_frame_t
*newframe
;
2196 /* If the list is empty, we can't duplicate frames.
2197 * For weightp modes other than kmeans, also don't insert dupes when there is only one reference frame.
2198 * Do note that this reference frame will still be weighted. */
2199 if ( i
< 1 || (h
->param
.analyse
.i_weighted_pred
!= X264_WEIGHTP_KMEAN
&& i
<= 1) )
2202 //Duplication is only used in X264_WEIGHTP_SMART and X264_WEIGHTP_KMEAN
2203 if( h
->param
.analyse
.i_weighted_pred
< X264_WEIGHTP_SMART
)
2206 /* Duplication is a hack to compensate for crappy rounding in motion compensation.
2207 * With high bit depth, it's not worth doing, so turn it off except in the case of
2208 * unweighted dupes. */
2209 if( BIT_DEPTH
> 8 && w
!= x264_weight_none
)
2212 newframe
= x264_frame_pop_blank_unused( h
);
2216 //FIXME: probably don't need to copy everything
2217 *newframe
= *h
->fref
[0][i_ref
];
2218 newframe
->i_reference_count
= 1;
2219 newframe
->orig
= h
->fref
[0][i_ref
];
2220 newframe
->b_duplicate
= 1;
2221 if( h
->param
.analyse
.i_weighted_pred
== X264_WEIGHTP_SMART
||
2222 (h
->param
.analyse
.i_weighted_pred
== X264_WEIGHTP_KMEAN
&& w
!= x264_weight_none
) )
2223 memcpy( h
->fenc
->weight
[j
], w
, sizeof(h
->fenc
->weight
[i
]) );
2225 /* shift the frames to make space for the dupe. */
2226 h
->b_ref_reorder
[0] = 1;
2227 if( h
->i_ref
[0] < X264_REF_MAX
)
2229 h
->fref
[0][X264_REF_MAX
-1] = NULL
;
2230 x264_frame_unshift( &h
->fref
[0][j
], newframe
);
2235 static void x264_weighted_pred_init( x264_t
*h
)
2237 /* for now no analysis and set all weights to nothing */
2238 for( int i_ref
= 0; i_ref
< h
->i_ref
[0]; i_ref
++ )
2239 h
->fenc
->weighted
[i_ref
] = h
->fref
[0][i_ref
]->filtered
[0][0];
2241 // FIXME: This only supports weighting of one reference frame
2242 // and duplicates of that frame.
2243 h
->fenc
->i_lines_weighted
= 0;
2245 for( int i_ref
= 0; i_ref
< (h
->i_ref
[0] << SLICE_MBAFF
); i_ref
++ )
2246 for( int i
= 0; i
< 3; i
++ )
2247 h
->sh
.weight
[i_ref
][i
].weightfn
= NULL
;
2250 if( h
->sh
.i_type
!= SLICE_TYPE_P
|| h
->param
.analyse
.i_weighted_pred
<= 0 )
2253 int i_padv
= PADV
<< PARAM_INTERLACED
;
2255 int weightplane
[2] = { 0, 0 };
2256 int buffer_next
= 0;
2257 for( int i
= 0; i
< 3; i
++ )
2259 for( int j
= 0; j
< h
->i_ref
[0]; j
++ )
2261 if( h
->fenc
->weight
[j
][i
].weightfn
)
2263 h
->sh
.weight
[j
][i
] = h
->fenc
->weight
[j
][i
];
2264 if( !(h
->sh
.weight
[j
][i
].i_scale
== 1<<h
->sh
.weight
[j
][i
].i_denom
&& h
->sh
.weight
[j
][i
].i_offset
== 0) )
2266 if( !weightplane
[!!i
] )
2268 weightplane
[!!i
] = 1;
2269 h
->sh
.weight
[0][!!i
].i_denom
= denom
= h
->sh
.weight
[j
][i
].i_denom
;
2270 assert( x264_clip3( denom
, 0, 7 ) == denom
);
2273 assert( h
->sh
.weight
[j
][i
].i_denom
== denom
);
2276 h
->fenc
->weighted
[j
] = h
->mb
.p_weight_buf
[buffer_next
++] + h
->fenc
->i_stride
[0] * i_padv
+ PADH
;
2277 //scale full resolution frame
2278 if( h
->param
.i_threads
== 1 )
2280 pixel
*src
= h
->fref
[0][j
]->filtered
[0][0] - h
->fref
[0][j
]->i_stride
[0]*i_padv
- PADH
;
2281 pixel
*dst
= h
->fenc
->weighted
[j
] - h
->fenc
->i_stride
[0]*i_padv
- PADH
;
2282 int stride
= h
->fenc
->i_stride
[0];
2283 int width
= h
->fenc
->i_width
[0] + PADH
*2;
2284 int height
= h
->fenc
->i_lines
[0] + i_padv
*2;
2285 x264_weight_scale_plane( h
, dst
, stride
, src
, stride
, width
, height
, &h
->sh
.weight
[j
][0] );
2286 h
->fenc
->i_lines_weighted
= height
;
2294 if( weightplane
[1] )
2295 for( int i
= 0; i
< h
->i_ref
[0]; i
++ )
2297 if( h
->sh
.weight
[i
][1].weightfn
&& !h
->sh
.weight
[i
][2].weightfn
)
2299 h
->sh
.weight
[i
][2].i_scale
= 1 << h
->sh
.weight
[0][1].i_denom
;
2300 h
->sh
.weight
[i
][2].i_offset
= 0;
2302 else if( h
->sh
.weight
[i
][2].weightfn
&& !h
->sh
.weight
[i
][1].weightfn
)
2304 h
->sh
.weight
[i
][1].i_scale
= 1 << h
->sh
.weight
[0][1].i_denom
;
2305 h
->sh
.weight
[i
][1].i_offset
= 0;
2309 if( !weightplane
[0] )
2310 h
->sh
.weight
[0][0].i_denom
= 0;
2311 if( !weightplane
[1] )
2312 h
->sh
.weight
[0][1].i_denom
= 0;
2313 h
->sh
.weight
[0][2].i_denom
= h
->sh
.weight
[0][1].i_denom
;
2316 static inline int x264_reference_distance( x264_t
*h
, x264_frame_t
*frame
)
2318 if( h
->param
.i_frame_packing
== 5 )
2319 return abs((h
->fenc
->i_frame
&~1) - (frame
->i_frame
&~1)) +
2320 ((h
->fenc
->i_frame
&1) != (frame
->i_frame
&1));
2322 return abs(h
->fenc
->i_frame
- frame
->i_frame
);
2325 static inline void x264_reference_build_list( x264_t
*h
, int i_poc
)
2329 /* build ref list 0/1 */
2330 h
->mb
.pic
.i_fref
[0] = h
->i_ref
[0] = 0;
2331 h
->mb
.pic
.i_fref
[1] = h
->i_ref
[1] = 0;
2332 if( h
->sh
.i_type
== SLICE_TYPE_I
)
2335 for( int i
= 0; h
->frames
.reference
[i
]; i
++ )
2337 if( h
->frames
.reference
[i
]->b_corrupt
)
2339 if( h
->frames
.reference
[i
]->i_poc
< i_poc
)
2340 h
->fref
[0][h
->i_ref
[0]++] = h
->frames
.reference
[i
];
2341 else if( h
->frames
.reference
[i
]->i_poc
> i_poc
)
2342 h
->fref
[1][h
->i_ref
[1]++] = h
->frames
.reference
[i
];
2345 if( h
->sh
.i_mmco_remove_from_end
)
2347 /* Order ref0 for MMCO remove */
2351 for( int i
= 0; i
< h
->i_ref
[0] - 1; i
++ )
2353 if( h
->fref
[0][i
]->i_frame
< h
->fref
[0][i
+1]->i_frame
)
2355 XCHG( x264_frame_t
*, h
->fref
[0][i
], h
->fref
[0][i
+1] );
2362 for( int i
= h
->i_ref
[0]-1; i
>= h
->i_ref
[0] - h
->sh
.i_mmco_remove_from_end
; i
-- )
2364 int diff
= h
->i_frame_num
- h
->fref
[0][i
]->i_frame_num
;
2365 h
->sh
.mmco
[h
->sh
.i_mmco_command_count
].i_poc
= h
->fref
[0][i
]->i_poc
;
2366 h
->sh
.mmco
[h
->sh
.i_mmco_command_count
++].i_difference_of_pic_nums
= diff
;
2370 /* Order reference lists by distance from the current frame. */
2371 for( int list
= 0; list
< 2; list
++ )
2373 h
->fref_nearest
[list
] = h
->fref
[list
][0];
2377 for( int i
= 0; i
< h
->i_ref
[list
] - 1; i
++ )
2379 if( list
? h
->fref
[list
][i
+1]->i_poc
< h
->fref_nearest
[list
]->i_poc
2380 : h
->fref
[list
][i
+1]->i_poc
> h
->fref_nearest
[list
]->i_poc
)
2381 h
->fref_nearest
[list
] = h
->fref
[list
][i
+1];
2382 if( x264_reference_distance( h
, h
->fref
[list
][i
] ) > x264_reference_distance( h
, h
->fref
[list
][i
+1] ) )
2384 XCHG( x264_frame_t
*, h
->fref
[list
][i
], h
->fref
[list
][i
+1] );
2392 x264_reference_check_reorder( h
);
2394 h
->i_ref
[1] = X264_MIN( h
->i_ref
[1], h
->frames
.i_max_ref1
);
2395 h
->i_ref
[0] = X264_MIN( h
->i_ref
[0], h
->frames
.i_max_ref0
);
2396 h
->i_ref
[0] = X264_MIN( h
->i_ref
[0], h
->param
.i_frame_reference
); // if reconfig() has lowered the limit
2398 /* For Blu-ray compliance, don't reference frames outside of the minigop. */
2399 if( IS_X264_TYPE_B( h
->fenc
->i_type
) && h
->param
.b_bluray_compat
)
2400 h
->i_ref
[0] = X264_MIN( h
->i_ref
[0], IS_X264_TYPE_B( h
->fref
[0][0]->i_type
) + 1 );
2402 /* add duplicates */
2403 if( h
->fenc
->i_type
== X264_TYPE_P
)
2406 if( h
->param
.analyse
.i_weighted_pred
== X264_WEIGHTP_SIMPLE
||
2407 h
->param
.analyse
.i_weighted_pred
== X264_WEIGHTP_SMART
)
2410 w
[1].weightfn
= w
[2].weightfn
= NULL
;
2411 if( h
->param
.rc
.b_stat_read
)
2412 x264_ratecontrol_set_weights( h
, h
->fenc
);
2414 if( !h
->fenc
->weight
[0][0].weightfn
)
2416 h
->fenc
->weight
[0][0].i_denom
= 0;
2417 SET_WEIGHT( w
[0], 1, 1, 0, -1 );
2418 idx
= x264_weighted_reference_duplicate( h
, 0, w
);
2422 if( h
->fenc
->weight
[0][0].i_scale
== 1<<h
->fenc
->weight
[0][0].i_denom
)
2424 SET_WEIGHT( h
->fenc
->weight
[0][0], 1, 1, 0, h
->fenc
->weight
[0][0].i_offset
);
2426 x264_weighted_reference_duplicate( h
, 0, x264_weight_none
);
2427 if( h
->fenc
->weight
[0][0].i_offset
> -128 )
2429 w
[0] = h
->fenc
->weight
[0][0];
2431 h
->mc
.weight_cache( h
, &w
[0] );
2432 idx
= x264_weighted_reference_duplicate( h
, 0, w
);
2436 else if( h
->param
.analyse
.i_weighted_pred
== X264_WEIGHTP_KMEAN
)
2439 w
[1].weightfn
= w
[2].weightfn
= NULL
;
2440 if( h
->param
.rc
.b_stat_read
)
2441 x264_ratecontrol_set_weights( h
, h
->fenc
);
2443 if( !h
->fenc
->weight
[0][0].weightfn
)
2445 SET_WEIGHT( w
[0], 1, 1, 0, -1 );
2446 idx
= x264_weighted_reference_duplicate( h
, 0, w
);
2448 for ( int i
= 0; h
->fenc
->weight
[i
][0].weightfn
&& i
< X264_DUPS_MAX
; i
++ )
2450 x264_weighted_reference_duplicate( h
, 0, x264_weight_none
);
2453 h
->mb
.ref_blind_dupe
= idx
;
2456 assert( h
->i_ref
[0] + h
->i_ref
[1] <= X264_REF_MAX
);
2457 h
->mb
.pic
.i_fref
[0] = h
->i_ref
[0];
2458 h
->mb
.pic
.i_fref
[1] = h
->i_ref
[1];
2461 static void x264_fdec_filter_row( x264_t
*h
, int mb_y
, int pass
)
2463 /* mb_y is the mb to be encoded next, not the mb to be filtered here */
2464 int b_hpel
= h
->fdec
->b_kept_as_ref
;
2465 int b_deblock
= h
->sh
.i_disable_deblocking_filter_idc
!= 1;
2466 int b_end
= mb_y
== h
->i_threadslice_end
;
2467 int b_measure_quality
= 1;
2468 int min_y
= mb_y
- (1 << SLICE_MBAFF
);
2469 int b_start
= min_y
== h
->i_threadslice_start
;
2470 /* Even in interlaced mode, deblocking never modifies more than 4 pixels
2471 * above each MB, as bS=4 doesn't happen for the top of interlaced mbpairs. */
2472 int minpix_y
= min_y
*16 - 4 * !b_start
;
2473 int maxpix_y
= mb_y
*16 - 4 * !b_end
;
2474 b_deblock
&= b_hpel
|| h
->param
.b_full_recon
|| h
->param
.psz_dump_yuv
;
2475 if( h
->param
.b_sliced_threads
)
2479 /* During encode: only do deblock if asked for */
2482 b_deblock
&= h
->param
.b_full_recon
;
2485 /* During post-encode pass: do deblock if not done yet, do hpel for all
2486 * rows except those between slices. */
2488 b_deblock
&= !h
->param
.b_full_recon
;
2489 b_hpel
&= !(b_start
&& min_y
> 0);
2490 b_measure_quality
= 0;
2492 /* Final pass: do the rows between slices in sequence. */
2495 b_measure_quality
= 0;
2499 if( mb_y
& SLICE_MBAFF
)
2501 if( min_y
< h
->i_threadslice_start
)
2505 for( int y
= min_y
; y
< mb_y
; y
+= (1 << SLICE_MBAFF
) )
2506 x264_frame_deblock_row( h
, y
);
2508 /* FIXME: Prediction requires different borders for interlaced/progressive mc,
2509 * but the actual image data is equivalent. For now, maintain this
2510 * consistency by copying deblocked pixels between planes. */
2511 if( PARAM_INTERLACED
&& (!h
->param
.b_sliced_threads
|| pass
== 1) )
2512 for( int p
= 0; p
< h
->fdec
->i_plane
; p
++ )
2513 for( int i
= minpix_y
>>(CHROMA_V_SHIFT
&& p
); i
< maxpix_y
>>(CHROMA_V_SHIFT
&& p
); i
++ )
2514 memcpy( h
->fdec
->plane_fld
[p
] + i
*h
->fdec
->i_stride
[p
],
2515 h
->fdec
->plane
[p
] + i
*h
->fdec
->i_stride
[p
],
2516 h
->mb
.i_mb_width
*16*sizeof(pixel
) );
2518 if( h
->fdec
->b_kept_as_ref
&& (!h
->param
.b_sliced_threads
|| pass
== 1) )
2519 x264_frame_expand_border( h
, h
->fdec
, min_y
);
2522 int end
= mb_y
== h
->mb
.i_mb_height
;
2523 /* Can't do hpel until the previous slice is done encoding. */
2524 if( h
->param
.analyse
.i_subpel_refine
)
2526 x264_frame_filter( h
, h
->fdec
, min_y
, end
);
2527 x264_frame_expand_border_filtered( h
, h
->fdec
, min_y
, end
);
2531 if( SLICE_MBAFF
&& pass
== 0 )
2532 for( int i
= 0; i
< 3; i
++ )
2534 XCHG( pixel
*, h
->intra_border_backup
[0][i
], h
->intra_border_backup
[3][i
] );
2535 XCHG( pixel
*, h
->intra_border_backup
[1][i
], h
->intra_border_backup
[4][i
] );
2538 if( h
->i_thread_frames
> 1 && h
->fdec
->b_kept_as_ref
)
2539 x264_frame_cond_broadcast( h
->fdec
, mb_y
*16 + (b_end
? 10000 : -(X264_THREAD_HEIGHT
<< SLICE_MBAFF
)) );
2541 if( b_measure_quality
)
2543 maxpix_y
= X264_MIN( maxpix_y
, h
->param
.i_height
);
2544 if( h
->param
.analyse
.b_psnr
)
2546 for( int p
= 0; p
< (CHROMA444
? 3 : 1); p
++ )
2547 h
->stat
.frame
.i_ssd
[p
] += x264_pixel_ssd_wxh( &h
->pixf
,
2548 h
->fdec
->plane
[p
] + minpix_y
* h
->fdec
->i_stride
[p
], h
->fdec
->i_stride
[p
],
2549 h
->fenc
->plane
[p
] + minpix_y
* h
->fenc
->i_stride
[p
], h
->fenc
->i_stride
[p
],
2550 h
->param
.i_width
, maxpix_y
-minpix_y
);
2553 uint64_t ssd_u
, ssd_v
;
2554 int v_shift
= CHROMA_V_SHIFT
;
2555 x264_pixel_ssd_nv12( &h
->pixf
,
2556 h
->fdec
->plane
[1] + (minpix_y
>>v_shift
) * h
->fdec
->i_stride
[1], h
->fdec
->i_stride
[1],
2557 h
->fenc
->plane
[1] + (minpix_y
>>v_shift
) * h
->fenc
->i_stride
[1], h
->fenc
->i_stride
[1],
2558 h
->param
.i_width
>>1, (maxpix_y
-minpix_y
)>>v_shift
, &ssd_u
, &ssd_v
);
2559 h
->stat
.frame
.i_ssd
[1] += ssd_u
;
2560 h
->stat
.frame
.i_ssd
[2] += ssd_v
;
2564 if( h
->param
.analyse
.b_ssim
)
2568 /* offset by 2 pixels to avoid alignment of ssim blocks with dct blocks,
2569 * and overlap by 4 */
2570 minpix_y
+= b_start
? 2 : -6;
2571 h
->stat
.frame
.f_ssim
+=
2572 x264_pixel_ssim_wxh( &h
->pixf
,
2573 h
->fdec
->plane
[0] + 2+minpix_y
*h
->fdec
->i_stride
[0], h
->fdec
->i_stride
[0],
2574 h
->fenc
->plane
[0] + 2+minpix_y
*h
->fenc
->i_stride
[0], h
->fenc
->i_stride
[0],
2575 h
->param
.i_width
-2, maxpix_y
-minpix_y
, h
->scratch_buffer
, &ssim_cnt
);
2576 h
->stat
.frame
.i_ssim_cnt
+= ssim_cnt
;
2581 static inline int x264_reference_update( x264_t
*h
)
2583 if( !h
->fdec
->b_kept_as_ref
)
2585 if( h
->i_thread_frames
> 1 )
2587 x264_frame_push_unused( h
, h
->fdec
);
2588 h
->fdec
= x264_frame_pop_unused( h
, 1 );
2595 /* apply mmco from previous frame. */
2596 for( int i
= 0; i
< h
->sh
.i_mmco_command_count
; i
++ )
2597 for( int j
= 0; h
->frames
.reference
[j
]; j
++ )
2598 if( h
->frames
.reference
[j
]->i_poc
== h
->sh
.mmco
[i
].i_poc
)
2599 x264_frame_push_unused( h
, x264_frame_shift( &h
->frames
.reference
[j
] ) );
2601 /* move frame in the buffer */
2602 x264_frame_push( h
->frames
.reference
, h
->fdec
);
2603 if( h
->frames
.reference
[h
->sps
->i_num_ref_frames
] )
2604 x264_frame_push_unused( h
, x264_frame_shift( h
->frames
.reference
) );
2605 h
->fdec
= x264_frame_pop_unused( h
, 1 );
2611 static inline void x264_reference_reset( x264_t
*h
)
2613 while( h
->frames
.reference
[0] )
2614 x264_frame_push_unused( h
, x264_frame_pop( h
->frames
.reference
) );
2619 static inline void x264_reference_hierarchy_reset( x264_t
*h
)
2622 int b_hasdelayframe
= 0;
2624 /* look for delay frames -- chain must only contain frames that are disposable */
2625 for( int i
= 0; h
->frames
.current
[i
] && IS_DISPOSABLE( h
->frames
.current
[i
]->i_type
); i
++ )
2626 b_hasdelayframe
|= h
->frames
.current
[i
]->i_coded
2627 != h
->frames
.current
[i
]->i_frame
+ h
->sps
->vui
.i_num_reorder_frames
;
2629 /* This function must handle b-pyramid and clear frames for open-gop */
2630 if( h
->param
.i_bframe_pyramid
!= X264_B_PYRAMID_STRICT
&& !b_hasdelayframe
&& h
->frames
.i_poc_last_open_gop
== -1 )
2633 /* Remove last BREF. There will never be old BREFs in the
2634 * dpb during a BREF decode when pyramid == STRICT */
2635 for( ref
= 0; h
->frames
.reference
[ref
]; ref
++ )
2637 if( ( h
->param
.i_bframe_pyramid
== X264_B_PYRAMID_STRICT
2638 && h
->frames
.reference
[ref
]->i_type
== X264_TYPE_BREF
)
2639 || ( h
->frames
.reference
[ref
]->i_poc
< h
->frames
.i_poc_last_open_gop
2640 && h
->sh
.i_type
!= SLICE_TYPE_B
) )
2642 int diff
= h
->i_frame_num
- h
->frames
.reference
[ref
]->i_frame_num
;
2643 h
->sh
.mmco
[h
->sh
.i_mmco_command_count
].i_difference_of_pic_nums
= diff
;
2644 h
->sh
.mmco
[h
->sh
.i_mmco_command_count
++].i_poc
= h
->frames
.reference
[ref
]->i_poc
;
2645 x264_frame_push_unused( h
, x264_frame_shift( &h
->frames
.reference
[ref
] ) );
2646 h
->b_ref_reorder
[0] = 1;
2651 /* Prepare room in the dpb for the delayed display time of the later b-frame's */
2652 if( h
->param
.i_bframe_pyramid
)
2653 h
->sh
.i_mmco_remove_from_end
= X264_MAX( ref
+ 2 - h
->frames
.i_max_dpb
, 0 );
2656 static inline void x264_slice_init( x264_t
*h
, int i_nal_type
, int i_global_qp
)
2658 /* ------------------------ Create slice header ----------------------- */
2659 if( i_nal_type
== NAL_SLICE_IDR
)
2661 x264_slice_header_init( h
, &h
->sh
, h
->sps
, h
->pps
, h
->i_idr_pic_id
, h
->i_frame_num
, i_global_qp
);
2664 if( h
->param
.i_avcintra_class
)
2666 switch( h
->i_idr_pic_id
)
2669 h
->i_idr_pic_id
= 3;
2672 h
->i_idr_pic_id
= 4;
2676 h
->i_idr_pic_id
= 5;
2681 h
->i_idr_pic_id
^= 1;
2685 x264_slice_header_init( h
, &h
->sh
, h
->sps
, h
->pps
, -1, h
->i_frame_num
, i_global_qp
);
2687 h
->sh
.i_num_ref_idx_l0_active
= h
->i_ref
[0] <= 0 ? 1 : h
->i_ref
[0];
2688 h
->sh
.i_num_ref_idx_l1_active
= h
->i_ref
[1] <= 0 ? 1 : h
->i_ref
[1];
2689 if( h
->sh
.i_num_ref_idx_l0_active
!= h
->pps
->i_num_ref_idx_l0_default_active
||
2690 (h
->sh
.i_type
== SLICE_TYPE_B
&& h
->sh
.i_num_ref_idx_l1_active
!= h
->pps
->i_num_ref_idx_l1_default_active
) )
2692 h
->sh
.b_num_ref_idx_override
= 1;
2696 if( h
->fenc
->i_type
== X264_TYPE_BREF
&& h
->param
.b_bluray_compat
&& h
->sh
.i_mmco_command_count
)
2699 h
->sh_backup
= h
->sh
;
2702 h
->fdec
->i_frame_num
= h
->sh
.i_frame_num
;
2704 if( h
->sps
->i_poc_type
== 0 )
2706 h
->sh
.i_poc
= h
->fdec
->i_poc
;
2707 if( PARAM_INTERLACED
)
2709 h
->sh
.i_delta_poc_bottom
= h
->param
.b_tff
? 1 : -1;
2710 h
->sh
.i_poc
+= h
->sh
.i_delta_poc_bottom
== -1;
2713 h
->sh
.i_delta_poc_bottom
= 0;
2714 h
->fdec
->i_delta_poc
[0] = h
->sh
.i_delta_poc_bottom
== -1;
2715 h
->fdec
->i_delta_poc
[1] = h
->sh
.i_delta_poc_bottom
== 1;
2719 /* Nothing to do ? */
2722 x264_macroblock_slice_init( h
);
2728 uint8_t cabac_prevbyte
;
2731 x264_frame_stat_t stat
;
2734 int field_decoding_flag
;
2737 static ALWAYS_INLINE
void x264_bitstream_backup( x264_t
*h
, x264_bs_bak_t
*bak
, int i_skip
, int full
)
2741 bak
->stat
= h
->stat
.frame
;
2742 bak
->last_qp
= h
->mb
.i_last_qp
;
2743 bak
->last_dqp
= h
->mb
.i_last_dqp
;
2744 bak
->field_decoding_flag
= h
->mb
.field_decoding_flag
;
2748 bak
->stat
.i_mv_bits
= h
->stat
.frame
.i_mv_bits
;
2749 bak
->stat
.i_tex_bits
= h
->stat
.frame
.i_tex_bits
;
2751 /* In the per-MB backup, we don't need the contexts because flushing the CABAC
2752 * encoder has no context dependency and in this case, a slice is ended (and
2753 * thus the content of all contexts are thrown away). */
2754 if( h
->param
.b_cabac
)
2757 memcpy( &bak
->cabac
, &h
->cabac
, sizeof(x264_cabac_t
) );
2759 memcpy( &bak
->cabac
, &h
->cabac
, offsetof(x264_cabac_t
, f8_bits_encoded
) );
2760 /* x264's CABAC writer modifies the previous byte during carry, so it has to be
2762 bak
->cabac_prevbyte
= h
->cabac
.p
[-1];
2766 bak
->bs
= h
->out
.bs
;
2771 static ALWAYS_INLINE
void x264_bitstream_restore( x264_t
*h
, x264_bs_bak_t
*bak
, int *skip
, int full
)
2775 h
->stat
.frame
= bak
->stat
;
2776 h
->mb
.i_last_qp
= bak
->last_qp
;
2777 h
->mb
.i_last_dqp
= bak
->last_dqp
;
2778 h
->mb
.field_decoding_flag
= bak
->field_decoding_flag
;
2782 h
->stat
.frame
.i_mv_bits
= bak
->stat
.i_mv_bits
;
2783 h
->stat
.frame
.i_tex_bits
= bak
->stat
.i_tex_bits
;
2785 if( h
->param
.b_cabac
)
2788 memcpy( &h
->cabac
, &bak
->cabac
, sizeof(x264_cabac_t
) );
2790 memcpy( &h
->cabac
, &bak
->cabac
, offsetof(x264_cabac_t
, f8_bits_encoded
) );
2791 h
->cabac
.p
[-1] = bak
->cabac_prevbyte
;
2795 h
->out
.bs
= bak
->bs
;
2800 static intptr_t x264_slice_write( x264_t
*h
)
2803 int mb_xy
, i_mb_x
, i_mb_y
;
2804 /* NALUs other than the first use a 3-byte startcode.
2805 * Add one extra byte for the rbsp, and one more for the final CABAC putbyte.
2806 * Then add an extra 5 bytes just in case, to account for random NAL escapes and
2807 * other inaccuracies. */
2808 int overhead_guess
= (NALU_OVERHEAD
- (h
->param
.b_annexb
&& h
->out
.i_nal
)) + 1 + h
->param
.b_cabac
+ 5;
2809 int slice_max_size
= h
->param
.i_slice_max_size
> 0 ? (h
->param
.i_slice_max_size
-overhead_guess
)*8 : 0;
2810 int back_up_bitstream_cavlc
= !h
->param
.b_cabac
&& h
->sps
->i_profile_idc
< PROFILE_HIGH
;
2811 int back_up_bitstream
= slice_max_size
|| back_up_bitstream_cavlc
;
2812 int starting_bits
= bs_pos(&h
->out
.bs
);
2813 int b_deblock
= h
->sh
.i_disable_deblocking_filter_idc
!= 1;
2814 int b_hpel
= h
->fdec
->b_kept_as_ref
;
2815 int orig_last_mb
= h
->sh
.i_last_mb
;
2816 int thread_last_mb
= h
->i_threadslice_end
* h
->mb
.i_mb_width
- 1;
2817 uint8_t *last_emu_check
;
2818 #define BS_BAK_SLICE_MAX_SIZE 0
2819 #define BS_BAK_CAVLC_OVERFLOW 1
2820 #define BS_BAK_SLICE_MIN_MBS 2
2821 #define BS_BAK_ROW_VBV 3
2822 x264_bs_bak_t bs_bak
[4];
2823 b_deblock
&= b_hpel
|| h
->param
.b_full_recon
|| h
->param
.psz_dump_yuv
;
2824 bs_realign( &h
->out
.bs
);
2827 x264_nal_start( h
, h
->i_nal_type
, h
->i_nal_ref_idc
);
2828 h
->out
.nal
[h
->out
.i_nal
].i_first_mb
= h
->sh
.i_first_mb
;
2831 x264_macroblock_thread_init( h
);
2833 /* Set the QP equal to the first QP in the slice for more accurate CABAC initialization. */
2834 h
->mb
.i_mb_xy
= h
->sh
.i_first_mb
;
2835 h
->sh
.i_qp
= x264_ratecontrol_mb_qp( h
);
2836 h
->sh
.i_qp
= SPEC_QP( h
->sh
.i_qp
);
2837 h
->sh
.i_qp_delta
= h
->sh
.i_qp
- h
->pps
->i_pic_init_qp
;
2839 x264_slice_header_write( &h
->out
.bs
, &h
->sh
, h
->i_nal_ref_idc
);
2840 if( h
->param
.b_cabac
)
2842 /* alignment needed */
2843 bs_align_1( &h
->out
.bs
);
2846 x264_cabac_context_init( h
, &h
->cabac
, h
->sh
.i_type
, x264_clip3( h
->sh
.i_qp
-QP_BD_OFFSET
, 0, 51 ), h
->sh
.i_cabac_init_idc
);
2847 x264_cabac_encode_init ( &h
->cabac
, h
->out
.bs
.p
, h
->out
.bs
.p_end
);
2848 last_emu_check
= h
->cabac
.p
;
2851 last_emu_check
= h
->out
.bs
.p
;
2852 h
->mb
.i_last_qp
= h
->sh
.i_qp
;
2853 h
->mb
.i_last_dqp
= 0;
2854 h
->mb
.field_decoding_flag
= 0;
2856 i_mb_y
= h
->sh
.i_first_mb
/ h
->mb
.i_mb_width
;
2857 i_mb_x
= h
->sh
.i_first_mb
% h
->mb
.i_mb_width
;
2862 mb_xy
= i_mb_x
+ i_mb_y
* h
->mb
.i_mb_width
;
2863 int mb_spos
= bs_pos(&h
->out
.bs
) + x264_cabac_pos(&h
->cabac
);
2867 if( x264_bitstream_check_buffer( h
) )
2869 if( !(i_mb_y
& SLICE_MBAFF
) && h
->param
.rc
.i_vbv_buffer_size
)
2870 x264_bitstream_backup( h
, &bs_bak
[BS_BAK_ROW_VBV
], i_skip
, 1 );
2871 if( !h
->mb
.b_reencode_mb
)
2872 x264_fdec_filter_row( h
, i_mb_y
, 0 );
2875 if( back_up_bitstream
)
2877 if( back_up_bitstream_cavlc
)
2878 x264_bitstream_backup( h
, &bs_bak
[BS_BAK_CAVLC_OVERFLOW
], i_skip
, 0 );
2879 if( slice_max_size
&& !(i_mb_y
& SLICE_MBAFF
) )
2881 x264_bitstream_backup( h
, &bs_bak
[BS_BAK_SLICE_MAX_SIZE
], i_skip
, 0 );
2882 if( (thread_last_mb
+1-mb_xy
) == h
->param
.i_slice_min_mbs
)
2883 x264_bitstream_backup( h
, &bs_bak
[BS_BAK_SLICE_MIN_MBS
], i_skip
, 0 );
2887 if( PARAM_INTERLACED
)
2889 if( h
->mb
.b_adaptive_mbaff
)
2893 /* FIXME: VSAD is fast but fairly poor at choosing the best interlace type. */
2894 h
->mb
.b_interlaced
= x264_field_vsad( h
, i_mb_x
, i_mb_y
);
2895 memcpy( &h
->zigzagf
, MB_INTERLACED
? &h
->zigzagf_interlaced
: &h
->zigzagf_progressive
, sizeof(h
->zigzagf
) );
2896 if( !MB_INTERLACED
&& (i_mb_y
+2) == h
->mb
.i_mb_height
)
2897 x264_expand_border_mbpair( h
, i_mb_x
, i_mb_y
);
2900 h
->mb
.field
[mb_xy
] = MB_INTERLACED
;
2905 x264_macroblock_cache_load_interlaced( h
, i_mb_x
, i_mb_y
);
2907 x264_macroblock_cache_load_progressive( h
, i_mb_x
, i_mb_y
);
2909 x264_macroblock_analyse( h
);
2911 /* encode this macroblock -> be careful it can change the mb type to P_SKIP if needed */
2913 x264_macroblock_encode( h
);
2915 if( h
->param
.b_cabac
)
2917 if( mb_xy
> h
->sh
.i_first_mb
&& !(SLICE_MBAFF
&& (i_mb_y
&1)) )
2918 x264_cabac_encode_terminal( &h
->cabac
);
2920 if( IS_SKIP( h
->mb
.i_type
) )
2921 x264_cabac_mb_skip( h
, 1 );
2924 if( h
->sh
.i_type
!= SLICE_TYPE_I
)
2925 x264_cabac_mb_skip( h
, 0 );
2926 x264_macroblock_write_cabac( h
, &h
->cabac
);
2931 if( IS_SKIP( h
->mb
.i_type
) )
2935 if( h
->sh
.i_type
!= SLICE_TYPE_I
)
2937 bs_write_ue( &h
->out
.bs
, i_skip
); /* skip run */
2940 x264_macroblock_write_cavlc( h
);
2941 /* If there was a CAVLC level code overflow, try again at a higher QP. */
2942 if( h
->mb
.b_overflow
)
2944 h
->mb
.i_chroma_qp
= h
->chroma_qp_table
[++h
->mb
.i_qp
];
2945 h
->mb
.i_skip_intra
= 0;
2946 h
->mb
.b_skip_mc
= 0;
2947 h
->mb
.b_overflow
= 0;
2948 x264_bitstream_restore( h
, &bs_bak
[BS_BAK_CAVLC_OVERFLOW
], &i_skip
, 0 );
2954 int total_bits
= bs_pos(&h
->out
.bs
) + x264_cabac_pos(&h
->cabac
);
2955 int mb_size
= total_bits
- mb_spos
;
2957 if( slice_max_size
&& (!SLICE_MBAFF
|| (i_mb_y
&1)) )
2959 /* Count the skip run, just in case. */
2960 if( !h
->param
.b_cabac
)
2961 total_bits
+= bs_size_ue_big( i_skip
);
2962 /* Check for escape bytes. */
2963 uint8_t *end
= h
->param
.b_cabac
? h
->cabac
.p
: h
->out
.bs
.p
;
2964 for( ; last_emu_check
< end
- 2; last_emu_check
++ )
2965 if( last_emu_check
[0] == 0 && last_emu_check
[1] == 0 && last_emu_check
[2] <= 3 )
2967 slice_max_size
-= 8;
2970 /* We'll just re-encode this last macroblock if we go over the max slice size. */
2971 if( total_bits
- starting_bits
> slice_max_size
&& !h
->mb
.b_reencode_mb
)
2973 if( !x264_frame_new_slice( h
, h
->fdec
) )
2975 /* Handle the most obnoxious slice-min-mbs edge case: we need to end the slice
2976 * because it's gone over the maximum size, but doing so would violate slice-min-mbs.
2977 * If possible, roll back to the last checkpoint and try again.
2978 * We could try raising QP, but that would break in the case where a slice spans multiple
2979 * rows, which the re-encoding infrastructure can't currently handle. */
2980 if( mb_xy
<= thread_last_mb
&& (thread_last_mb
+1-mb_xy
) < h
->param
.i_slice_min_mbs
)
2982 if( thread_last_mb
-h
->param
.i_slice_min_mbs
< h
->sh
.i_first_mb
+h
->param
.i_slice_min_mbs
)
2984 x264_log( h
, X264_LOG_WARNING
, "slice-max-size violated (frame %d, cause: slice-min-mbs)\n", h
->i_frame
);
2988 x264_bitstream_restore( h
, &bs_bak
[BS_BAK_SLICE_MIN_MBS
], &i_skip
, 0 );
2989 h
->mb
.b_reencode_mb
= 1;
2990 h
->sh
.i_last_mb
= thread_last_mb
-h
->param
.i_slice_min_mbs
;
2993 if( mb_xy
-SLICE_MBAFF
*h
->mb
.i_mb_stride
!= h
->sh
.i_first_mb
)
2995 x264_bitstream_restore( h
, &bs_bak
[BS_BAK_SLICE_MAX_SIZE
], &i_skip
, 0 );
2996 h
->mb
.b_reencode_mb
= 1;
2999 // set to bottom of previous mbpair
3001 h
->sh
.i_last_mb
= mb_xy
-1+h
->mb
.i_mb_stride
*(!(i_mb_y
&1));
3003 h
->sh
.i_last_mb
= (i_mb_y
-2+!(i_mb_y
&1))*h
->mb
.i_mb_stride
+ h
->mb
.i_mb_width
- 1;
3006 h
->sh
.i_last_mb
= mb_xy
-1;
3010 h
->sh
.i_last_mb
= mb_xy
;
3017 h
->mb
.b_reencode_mb
= 0;
3020 x264_macroblock_cache_save( h
);
3022 if( x264_ratecontrol_mb( h
, mb_size
) < 0 )
3024 x264_bitstream_restore( h
, &bs_bak
[BS_BAK_ROW_VBV
], &i_skip
, 1 );
3025 h
->mb
.b_reencode_mb
= 1;
3027 i_mb_y
= i_mb_y
- SLICE_MBAFF
;
3028 h
->mb
.i_mb_prev_xy
= i_mb_y
* h
->mb
.i_mb_stride
- 1;
3029 h
->sh
.i_last_mb
= orig_last_mb
;
3033 /* accumulate mb stats */
3034 h
->stat
.frame
.i_mb_count
[h
->mb
.i_type
]++;
3036 int b_intra
= IS_INTRA( h
->mb
.i_type
);
3037 int b_skip
= IS_SKIP( h
->mb
.i_type
);
3038 if( h
->param
.i_log_level
>= X264_LOG_INFO
|| (h
->param
.psz_log_file
&& h
->param
.i_log_file_level
>= X264_LOG_INFO
) || h
->param
.rc
.b_stat_write
)
3040 if( !b_intra
&& !b_skip
&& !IS_DIRECT( h
->mb
.i_type
) )
3042 if( h
->mb
.i_partition
!= D_8x8
)
3043 h
->stat
.frame
.i_mb_partition
[h
->mb
.i_partition
] += 4;
3045 for( int i
= 0; i
< 4; i
++ )
3046 h
->stat
.frame
.i_mb_partition
[h
->mb
.i_sub_partition
[i
]] ++;
3047 if( h
->param
.i_frame_reference
> 1 )
3048 for( int i_list
= 0; i_list
<= (h
->sh
.i_type
== SLICE_TYPE_B
); i_list
++ )
3049 for( int i
= 0; i
< 4; i
++ )
3051 int i_ref
= h
->mb
.cache
.ref
[i_list
][ x264_scan8
[4*i
] ];
3053 h
->stat
.frame
.i_mb_count_ref
[i_list
][i_ref
] ++;
3058 if( h
->param
.i_log_level
>= X264_LOG_INFO
|| (h
->param
.psz_log_file
&& h
->param
.i_log_file_level
>= X264_LOG_INFO
) )
3060 if( h
->mb
.i_cbp_luma
| h
->mb
.i_cbp_chroma
)
3064 for( int i
= 0; i
< 4; i
++ )
3065 if( h
->mb
.i_cbp_luma
& (1 << i
) )
3066 for( int p
= 0; p
< 3; p
++ )
3069 int nnz8x8
= M16( &h
->mb
.cache
.non_zero_count
[x264_scan8
[s8
]+0] )
3070 | M16( &h
->mb
.cache
.non_zero_count
[x264_scan8
[s8
]+8] );
3071 h
->stat
.frame
.i_mb_cbp
[!b_intra
+ p
*2] += !!nnz8x8
;
3076 int cbpsum
= (h
->mb
.i_cbp_luma
&1) + ((h
->mb
.i_cbp_luma
>>1)&1)
3077 + ((h
->mb
.i_cbp_luma
>>2)&1) + (h
->mb
.i_cbp_luma
>>3);
3078 h
->stat
.frame
.i_mb_cbp
[!b_intra
+ 0] += cbpsum
;
3079 h
->stat
.frame
.i_mb_cbp
[!b_intra
+ 2] += !!h
->mb
.i_cbp_chroma
;
3080 h
->stat
.frame
.i_mb_cbp
[!b_intra
+ 4] += h
->mb
.i_cbp_chroma
>> 1;
3083 if( h
->mb
.i_cbp_luma
&& !b_intra
)
3085 h
->stat
.frame
.i_mb_count_8x8dct
[0] ++;
3086 h
->stat
.frame
.i_mb_count_8x8dct
[1] += h
->mb
.b_transform_8x8
;
3088 if( b_intra
&& h
->mb
.i_type
!= I_PCM
)
3090 if( h
->mb
.i_type
== I_16x16
)
3091 h
->stat
.frame
.i_mb_pred_mode
[0][h
->mb
.i_intra16x16_pred_mode
]++;
3092 else if( h
->mb
.i_type
== I_8x8
)
3093 for( int i
= 0; i
< 16; i
+= 4 )
3094 h
->stat
.frame
.i_mb_pred_mode
[1][h
->mb
.cache
.intra4x4_pred_mode
[x264_scan8
[i
]]]++;
3095 else //if( h->mb.i_type == I_4x4 )
3096 for( int i
= 0; i
< 16; i
++ )
3097 h
->stat
.frame
.i_mb_pred_mode
[2][h
->mb
.cache
.intra4x4_pred_mode
[x264_scan8
[i
]]]++;
3098 h
->stat
.frame
.i_mb_pred_mode
[3][x264_mb_chroma_pred_mode_fix
[h
->mb
.i_chroma_pred_mode
]]++;
3100 h
->stat
.frame
.i_mb_field
[b_intra
?0:b_skip
?2:1] += MB_INTERLACED
;
3103 /* calculate deblock strength values (actual deblocking is done per-row along with hpel) */
3105 x264_macroblock_deblock_strength( h
);
3107 if( mb_xy
== h
->sh
.i_last_mb
)
3112 i_mb_x
+= i_mb_y
& 1;
3113 i_mb_y
^= i_mb_x
< h
->mb
.i_mb_width
;
3117 if( i_mb_x
== h
->mb
.i_mb_width
)
3123 if( h
->sh
.i_last_mb
< h
->sh
.i_first_mb
)
3126 h
->out
.nal
[h
->out
.i_nal
].i_last_mb
= h
->sh
.i_last_mb
;
3128 if( h
->param
.b_cabac
)
3130 x264_cabac_encode_flush( h
, &h
->cabac
);
3131 h
->out
.bs
.p
= h
->cabac
.p
;
3136 bs_write_ue( &h
->out
.bs
, i_skip
); /* last skip run */
3137 /* rbsp_slice_trailing_bits */
3138 bs_rbsp_trailing( &h
->out
.bs
);
3139 bs_flush( &h
->out
.bs
);
3141 if( x264_nal_end( h
) )
3144 if( h
->sh
.i_last_mb
== (h
->i_threadslice_end
* h
->mb
.i_mb_width
- 1) )
3146 h
->stat
.frame
.i_misc_bits
= bs_pos( &h
->out
.bs
)
3147 + (h
->out
.i_nal
*NALU_OVERHEAD
* 8)
3148 - h
->stat
.frame
.i_tex_bits
3149 - h
->stat
.frame
.i_mv_bits
;
3150 x264_fdec_filter_row( h
, h
->i_threadslice_end
, 0 );
3152 if( h
->param
.b_sliced_threads
)
3154 /* Tell the main thread we're done. */
3155 x264_threadslice_cond_broadcast( h
, 1 );
3157 for( int mb_y
= h
->i_threadslice_start
; mb_y
<= h
->i_threadslice_end
; mb_y
++ )
3158 x264_fdec_filter_row( h
, mb_y
, 1 );
3159 x264_threadslice_cond_broadcast( h
, 2 );
3160 /* Do the first row of hpel, now that the previous slice is done */
3161 if( h
->i_thread_idx
> 0 )
3163 x264_threadslice_cond_wait( h
->thread
[h
->i_thread_idx
-1], 2 );
3164 x264_fdec_filter_row( h
, h
->i_threadslice_start
+ (1 << SLICE_MBAFF
), 2 );
3168 /* Free mb info after the last thread's done using it */
3169 if( h
->fdec
->mb_info_free
&& (!h
->param
.b_sliced_threads
|| h
->i_thread_idx
== (h
->param
.i_threads
-1)) )
3171 h
->fdec
->mb_info_free( h
->fdec
->mb_info
);
3172 h
->fdec
->mb_info
= NULL
;
3173 h
->fdec
->mb_info_free
= NULL
;
3180 static void x264_thread_sync_context( x264_t
*dst
, x264_t
*src
)
3185 // reference counting
3186 for( x264_frame_t
**f
= src
->frames
.reference
; *f
; f
++ )
3187 (*f
)->i_reference_count
++;
3188 for( x264_frame_t
**f
= dst
->frames
.reference
; *f
; f
++ )
3189 x264_frame_push_unused( src
, *f
);
3190 src
->fdec
->i_reference_count
++;
3191 x264_frame_push_unused( src
, dst
->fdec
);
3193 // copy everything except the per-thread pointers and the constants.
3194 memcpy( &dst
->i_frame
, &src
->i_frame
, offsetof(x264_t
, mb
.base
) - offsetof(x264_t
, i_frame
) );
3195 dst
->param
= src
->param
;
3196 dst
->stat
= src
->stat
;
3197 dst
->pixf
= src
->pixf
;
3198 dst
->reconfig
= src
->reconfig
;
3201 static void x264_thread_sync_stat( x264_t
*dst
, x264_t
*src
)
3204 memcpy( &dst
->stat
, &src
->stat
, offsetof(x264_t
, stat
.frame
) - offsetof(x264_t
, stat
) );
3207 static void *x264_slices_write( x264_t
*h
)
3209 int i_slice_num
= 0;
3210 int last_thread_mb
= h
->sh
.i_last_mb
;
3213 memset( &h
->stat
.frame
, 0, sizeof(h
->stat
.frame
) );
3214 h
->mb
.b_reencode_mb
= 0;
3215 while( h
->sh
.i_first_mb
+ SLICE_MBAFF
*h
->mb
.i_mb_stride
<= last_thread_mb
)
3217 h
->sh
.i_last_mb
= last_thread_mb
;
3218 if( !i_slice_num
|| !x264_frame_new_slice( h
, h
->fdec
) )
3220 if( h
->param
.i_slice_max_mbs
)
3224 // convert first to mbaff form, add slice-max-mbs, then convert back to normal form
3225 int last_mbaff
= 2*(h
->sh
.i_first_mb
% h
->mb
.i_mb_width
)
3226 + h
->mb
.i_mb_width
*(h
->sh
.i_first_mb
/ h
->mb
.i_mb_width
)
3227 + h
->param
.i_slice_max_mbs
- 1;
3228 int last_x
= (last_mbaff
% (2*h
->mb
.i_mb_width
))/2;
3229 int last_y
= (last_mbaff
/ (2*h
->mb
.i_mb_width
))*2 + 1;
3230 h
->sh
.i_last_mb
= last_x
+ h
->mb
.i_mb_stride
*last_y
;
3234 h
->sh
.i_last_mb
= h
->sh
.i_first_mb
+ h
->param
.i_slice_max_mbs
- 1;
3235 if( h
->sh
.i_last_mb
< last_thread_mb
&& last_thread_mb
- h
->sh
.i_last_mb
< h
->param
.i_slice_min_mbs
)
3236 h
->sh
.i_last_mb
= last_thread_mb
- h
->param
.i_slice_min_mbs
;
3240 else if( h
->param
.i_slice_count
&& !h
->param
.b_sliced_threads
)
3242 int height
= h
->mb
.i_mb_height
>> PARAM_INTERLACED
;
3243 int width
= h
->mb
.i_mb_width
<< PARAM_INTERLACED
;
3245 h
->sh
.i_last_mb
= (height
* i_slice_num
+ h
->param
.i_slice_count
/2) / h
->param
.i_slice_count
* width
- 1;
3248 h
->sh
.i_last_mb
= X264_MIN( h
->sh
.i_last_mb
, last_thread_mb
);
3249 if( x264_stack_align( x264_slice_write
, h
) )
3251 h
->sh
.i_first_mb
= h
->sh
.i_last_mb
+ 1;
3252 // if i_first_mb is not the last mb in a row then go to the next mb in MBAFF order
3253 if( SLICE_MBAFF
&& h
->sh
.i_first_mb
% h
->mb
.i_mb_width
)
3254 h
->sh
.i_first_mb
-= h
->mb
.i_mb_stride
;
3260 /* Tell other threads we're done, so they wouldn't wait for it */
3261 if( h
->param
.b_sliced_threads
)
3262 x264_threadslice_cond_broadcast( h
, 2 );
3266 static int x264_threaded_slices_write( x264_t
*h
)
3268 /* set first/last mb and sync contexts */
3269 for( int i
= 0; i
< h
->param
.i_threads
; i
++ )
3271 x264_t
*t
= h
->thread
[i
];
3274 t
->param
= h
->param
;
3275 memcpy( &t
->i_frame
, &h
->i_frame
, offsetof(x264_t
, rc
) - offsetof(x264_t
, i_frame
) );
3277 int height
= h
->mb
.i_mb_height
>> PARAM_INTERLACED
;
3278 t
->i_threadslice_start
= ((height
* i
+ h
->param
.i_slice_count
/2) / h
->param
.i_threads
) << PARAM_INTERLACED
;
3279 t
->i_threadslice_end
= ((height
* (i
+1) + h
->param
.i_slice_count
/2) / h
->param
.i_threads
) << PARAM_INTERLACED
;
3280 t
->sh
.i_first_mb
= t
->i_threadslice_start
* h
->mb
.i_mb_width
;
3281 t
->sh
.i_last_mb
= t
->i_threadslice_end
* h
->mb
.i_mb_width
- 1;
3284 x264_stack_align( x264_analyse_weight_frame
, h
, h
->mb
.i_mb_height
*16 + 16 );
3286 x264_threads_distribute_ratecontrol( h
);
3289 for( int i
= 0; i
< h
->param
.i_threads
; i
++ )
3291 h
->thread
[i
]->i_thread_idx
= i
;
3292 h
->thread
[i
]->b_thread_active
= 1;
3293 x264_threadslice_cond_broadcast( h
->thread
[i
], 0 );
3296 for( int i
= 0; i
< h
->param
.i_threads
; i
++ )
3297 x264_threadpool_run( h
->threadpool
, (void*)x264_slices_write
, h
->thread
[i
] );
3299 for( int i
= 0; i
< h
->param
.i_threads
; i
++ )
3300 x264_threadslice_cond_wait( h
->thread
[i
], 1 );
3302 x264_threads_merge_ratecontrol( h
);
3304 for( int i
= 1; i
< h
->param
.i_threads
; i
++ )
3306 x264_t
*t
= h
->thread
[i
];
3307 for( int j
= 0; j
< t
->out
.i_nal
; j
++ )
3309 h
->out
.nal
[h
->out
.i_nal
] = t
->out
.nal
[j
];
3311 x264_nal_check_buffer( h
);
3313 /* All entries in stat.frame are ints except for ssd/ssim. */
3314 for( int j
= 0; j
< (offsetof(x264_t
,stat
.frame
.i_ssd
) - offsetof(x264_t
,stat
.frame
.i_mv_bits
)) / sizeof(int); j
++ )
3315 ((int*)&h
->stat
.frame
)[j
] += ((int*)&t
->stat
.frame
)[j
];
3316 for( int j
= 0; j
< 3; j
++ )
3317 h
->stat
.frame
.i_ssd
[j
] += t
->stat
.frame
.i_ssd
[j
];
3318 h
->stat
.frame
.f_ssim
+= t
->stat
.frame
.f_ssim
;
3319 h
->stat
.frame
.i_ssim_cnt
+= t
->stat
.frame
.i_ssim_cnt
;
3325 void x264_encoder_intra_refresh( x264_t
*h
)
3327 h
= h
->thread
[h
->i_thread_phase
];
3328 h
->b_queued_intra_refresh
= 1;
3331 int x264_encoder_invalidate_reference( x264_t
*h
, int64_t pts
)
3333 if( h
->param
.i_bframe
)
3335 x264_log( h
, X264_LOG_ERROR
, "x264_encoder_invalidate_reference is not supported with B-frames enabled\n" );
3338 if( h
->param
.b_intra_refresh
)
3340 x264_log( h
, X264_LOG_ERROR
, "x264_encoder_invalidate_reference is not supported with intra refresh enabled\n" );
3343 h
= h
->thread
[h
->i_thread_phase
];
3344 if( pts
>= h
->i_last_idr_pts
)
3346 for( int i
= 0; h
->frames
.reference
[i
]; i
++ )
3347 if( pts
<= h
->frames
.reference
[i
]->i_pts
)
3348 h
->frames
.reference
[i
]->b_corrupt
= 1;
3349 if( pts
<= h
->fdec
->i_pts
)
3350 h
->fdec
->b_corrupt
= 1;
3355 /****************************************************************************
3356 * x264_encoder_encode:
3357 * XXX: i_poc : is the poc of the current given picture
3358 * i_frame : is the number of the frame being coded
3359 * ex: type frame poc
3367 ****************************************************************************/
3368 int x264_encoder_encode( x264_t
*h
,
3369 x264_nal_t
**pp_nal
, int *pi_nal
,
3370 x264_picture_t
*pic_in
,
3371 x264_picture_t
*pic_out
)
3373 x264_t
*thread_current
, *thread_prev
, *thread_oldest
;
3374 int i_nal_type
, i_nal_ref_idc
, i_global_qp
;
3375 int overhead
= NALU_OVERHEAD
;
3378 if( h
->opencl
.b_fatal_error
)
3382 if( h
->i_thread_frames
> 1 )
3384 thread_prev
= h
->thread
[ h
->i_thread_phase
];
3385 h
->i_thread_phase
= (h
->i_thread_phase
+ 1) % h
->i_thread_frames
;
3386 thread_current
= h
->thread
[ h
->i_thread_phase
];
3387 thread_oldest
= h
->thread
[ (h
->i_thread_phase
+ 1) % h
->i_thread_frames
];
3388 x264_thread_sync_context( thread_current
, thread_prev
);
3389 x264_thread_sync_ratecontrol( thread_current
, thread_prev
, thread_oldest
);
3397 h
->i_cpb_delay_pir_offset
= h
->i_cpb_delay_pir_offset_next
;
3403 /* ------------------- Setup new frame from picture -------------------- */
3404 if( pic_in
!= NULL
)
3406 if( h
->lookahead
->b_exit_thread
)
3408 x264_log( h
, X264_LOG_ERROR
, "lookahead thread is already stopped\n" );
3412 /* 1: Copy the picture to a frame and move it to a buffer */
3413 x264_frame_t
*fenc
= x264_frame_pop_unused( h
, 0 );
3417 if( x264_frame_copy_picture( h
, fenc
, pic_in
) < 0 )
3420 if( h
->param
.i_width
!= 16 * h
->mb
.i_mb_width
||
3421 h
->param
.i_height
!= 16 * h
->mb
.i_mb_height
)
3422 x264_frame_expand_border_mod16( h
, fenc
);
3424 fenc
->i_frame
= h
->frames
.i_input
++;
3426 if( fenc
->i_frame
== 0 )
3427 h
->frames
.i_first_pts
= fenc
->i_pts
;
3428 if( h
->frames
.i_bframe_delay
&& fenc
->i_frame
== h
->frames
.i_bframe_delay
)
3429 h
->frames
.i_bframe_delay_time
= fenc
->i_pts
- h
->frames
.i_first_pts
;
3431 if( h
->param
.b_vfr_input
&& fenc
->i_pts
<= h
->frames
.i_largest_pts
)
3432 x264_log( h
, X264_LOG_WARNING
, "non-strictly-monotonic PTS\n" );
3434 h
->frames
.i_second_largest_pts
= h
->frames
.i_largest_pts
;
3435 h
->frames
.i_largest_pts
= fenc
->i_pts
;
3437 if( (fenc
->i_pic_struct
< PIC_STRUCT_AUTO
) || (fenc
->i_pic_struct
> PIC_STRUCT_TRIPLE
) )
3438 fenc
->i_pic_struct
= PIC_STRUCT_AUTO
;
3440 if( fenc
->i_pic_struct
== PIC_STRUCT_AUTO
)
3443 int b_interlaced
= fenc
->param
? fenc
->param
->b_interlaced
: h
->param
.b_interlaced
;
3445 int b_interlaced
= 0;
3449 int b_tff
= fenc
->param
? fenc
->param
->b_tff
: h
->param
.b_tff
;
3450 fenc
->i_pic_struct
= b_tff
? PIC_STRUCT_TOP_BOTTOM
: PIC_STRUCT_BOTTOM_TOP
;
3453 fenc
->i_pic_struct
= PIC_STRUCT_PROGRESSIVE
;
3456 if( h
->param
.rc
.b_mb_tree
&& h
->param
.rc
.b_stat_read
)
3458 if( x264_macroblock_tree_read( h
, fenc
, pic_in
->prop
.quant_offsets
) )
3462 x264_stack_align( x264_adaptive_quant_frame
, h
, fenc
, pic_in
->prop
.quant_offsets
);
3464 if( pic_in
->prop
.quant_offsets_free
)
3465 pic_in
->prop
.quant_offsets_free( pic_in
->prop
.quant_offsets
);
3467 if( h
->frames
.b_have_lowres
)
3468 x264_frame_init_lowres( h
, fenc
);
3470 /* 2: Place the frame into the queue for its slice type decision */
3471 x264_lookahead_put_frame( h
, fenc
);
3473 if( h
->frames
.i_input
<= h
->frames
.i_delay
+ 1 - h
->i_thread_frames
)
3475 /* Nothing yet to encode, waiting for filling of buffers */
3476 pic_out
->i_type
= X264_TYPE_AUTO
;
3482 /* signal kills for lookahead thread */
3483 x264_pthread_mutex_lock( &h
->lookahead
->ifbuf
.mutex
);
3484 h
->lookahead
->b_exit_thread
= 1;
3485 x264_pthread_cond_broadcast( &h
->lookahead
->ifbuf
.cv_fill
);
3486 x264_pthread_mutex_unlock( &h
->lookahead
->ifbuf
.mutex
);
3490 /* 3: The picture is analyzed in the lookahead */
3491 if( !h
->frames
.current
[0] )
3492 x264_lookahead_get_frames( h
);
3494 if( !h
->frames
.current
[0] && x264_lookahead_is_empty( h
) )
3495 return x264_encoder_frame_end( thread_oldest
, thread_current
, pp_nal
, pi_nal
, pic_out
);
3497 /* ------------------- Get frame to be encoded ------------------------- */
3498 /* 4: get picture to encode */
3499 h
->fenc
= x264_frame_shift( h
->frames
.current
);
3501 /* If applicable, wait for previous frame reconstruction to finish */
3502 if( h
->param
.b_sliced_threads
)
3503 if( x264_threadpool_wait_all( h
) < 0 )
3506 if( h
->i_frame
== 0 )
3507 h
->i_reordered_pts_delay
= h
->fenc
->i_reordered_pts
;
3510 x264_encoder_reconfig_apply( h
, &h
->reconfig_h
->param
);
3513 if( h
->fenc
->param
)
3515 x264_encoder_reconfig_apply( h
, h
->fenc
->param
);
3516 if( h
->fenc
->param
->param_free
)
3518 h
->fenc
->param
->param_free( h
->fenc
->param
);
3519 h
->fenc
->param
= NULL
;
3522 x264_ratecontrol_zone_init( h
);
3524 // ok to call this before encoding any frames, since the initial values of fdec have b_kept_as_ref=0
3525 if( x264_reference_update( h
) )
3527 h
->fdec
->i_lines_completed
= -1;
3529 if( !IS_X264_TYPE_I( h
->fenc
->i_type
) )
3531 int valid_refs_left
= 0;
3532 for( int i
= 0; h
->frames
.reference
[i
]; i
++ )
3533 if( !h
->frames
.reference
[i
]->b_corrupt
)
3535 /* No valid reference frames left: force an IDR. */
3536 if( !valid_refs_left
)
3538 h
->fenc
->b_keyframe
= 1;
3539 h
->fenc
->i_type
= X264_TYPE_IDR
;
3543 if( h
->fenc
->b_keyframe
)
3545 h
->frames
.i_last_keyframe
= h
->fenc
->i_frame
;
3546 if( h
->fenc
->i_type
== X264_TYPE_IDR
)
3549 h
->frames
.i_last_idr
= h
->fenc
->i_frame
;
3552 h
->sh
.i_mmco_command_count
=
3553 h
->sh
.i_mmco_remove_from_end
= 0;
3554 h
->b_ref_reorder
[0] =
3555 h
->b_ref_reorder
[1] = 0;
3557 h
->fenc
->i_poc
= 2 * ( h
->fenc
->i_frame
- X264_MAX( h
->frames
.i_last_idr
, 0 ) );
3559 /* ------------------- Setup frame context ----------------------------- */
3560 /* 5: Init data dependent of frame type */
3561 if( h
->fenc
->i_type
== X264_TYPE_IDR
)
3563 /* reset ref pictures */
3564 i_nal_type
= NAL_SLICE_IDR
;
3565 i_nal_ref_idc
= NAL_PRIORITY_HIGHEST
;
3566 h
->sh
.i_type
= SLICE_TYPE_I
;
3567 x264_reference_reset( h
);
3568 h
->frames
.i_poc_last_open_gop
= -1;
3570 else if( h
->fenc
->i_type
== X264_TYPE_I
)
3572 i_nal_type
= NAL_SLICE
;
3573 i_nal_ref_idc
= NAL_PRIORITY_HIGH
; /* Not completely true but for now it is (as all I/P are kept as ref)*/
3574 h
->sh
.i_type
= SLICE_TYPE_I
;
3575 x264_reference_hierarchy_reset( h
);
3576 if( h
->param
.b_open_gop
)
3577 h
->frames
.i_poc_last_open_gop
= h
->fenc
->b_keyframe
? h
->fenc
->i_poc
: -1;
3579 else if( h
->fenc
->i_type
== X264_TYPE_P
)
3581 i_nal_type
= NAL_SLICE
;
3582 i_nal_ref_idc
= NAL_PRIORITY_HIGH
; /* Not completely true but for now it is (as all I/P are kept as ref)*/
3583 h
->sh
.i_type
= SLICE_TYPE_P
;
3584 x264_reference_hierarchy_reset( h
);
3585 h
->frames
.i_poc_last_open_gop
= -1;
3587 else if( h
->fenc
->i_type
== X264_TYPE_BREF
)
3589 i_nal_type
= NAL_SLICE
;
3590 i_nal_ref_idc
= h
->param
.i_bframe_pyramid
== X264_B_PYRAMID_STRICT
? NAL_PRIORITY_LOW
: NAL_PRIORITY_HIGH
;
3591 h
->sh
.i_type
= SLICE_TYPE_B
;
3592 x264_reference_hierarchy_reset( h
);
3596 i_nal_type
= NAL_SLICE
;
3597 i_nal_ref_idc
= NAL_PRIORITY_DISPOSABLE
;
3598 h
->sh
.i_type
= SLICE_TYPE_B
;
3601 h
->fdec
->i_type
= h
->fenc
->i_type
;
3602 h
->fdec
->i_frame
= h
->fenc
->i_frame
;
3603 h
->fenc
->b_kept_as_ref
=
3604 h
->fdec
->b_kept_as_ref
= i_nal_ref_idc
!= NAL_PRIORITY_DISPOSABLE
&& h
->param
.i_keyint_max
> 1;
3606 h
->fdec
->mb_info
= h
->fenc
->mb_info
;
3607 h
->fdec
->mb_info_free
= h
->fenc
->mb_info_free
;
3608 h
->fenc
->mb_info
= NULL
;
3609 h
->fenc
->mb_info_free
= NULL
;
3611 h
->fdec
->i_pts
= h
->fenc
->i_pts
;
3612 if( h
->frames
.i_bframe_delay
)
3614 int64_t *prev_reordered_pts
= thread_current
->frames
.i_prev_reordered_pts
;
3615 h
->fdec
->i_dts
= h
->i_frame
> h
->frames
.i_bframe_delay
3616 ? prev_reordered_pts
[ (h
->i_frame
- h
->frames
.i_bframe_delay
) % h
->frames
.i_bframe_delay
]
3617 : h
->fenc
->i_reordered_pts
- h
->frames
.i_bframe_delay_time
;
3618 prev_reordered_pts
[ h
->i_frame
% h
->frames
.i_bframe_delay
] = h
->fenc
->i_reordered_pts
;
3621 h
->fdec
->i_dts
= h
->fenc
->i_reordered_pts
;
3622 if( h
->fenc
->i_type
== X264_TYPE_IDR
)
3623 h
->i_last_idr_pts
= h
->fdec
->i_pts
;
3625 /* ------------------- Init ----------------------------- */
3626 /* build ref list 0/1 */
3627 x264_reference_build_list( h
, h
->fdec
->i_poc
);
3629 /* ---------------------- Write the bitstream -------------------------- */
3630 /* Init bitstream context */
3631 if( h
->param
.b_sliced_threads
)
3633 for( int i
= 0; i
< h
->param
.i_threads
; i
++ )
3635 bs_init( &h
->thread
[i
]->out
.bs
, h
->thread
[i
]->out
.p_bitstream
, h
->thread
[i
]->out
.i_bitstream
);
3636 h
->thread
[i
]->out
.i_nal
= 0;
3641 bs_init( &h
->out
.bs
, h
->out
.p_bitstream
, h
->out
.i_bitstream
);
3645 if( h
->param
.b_aud
)
3649 if( h
->sh
.i_type
== SLICE_TYPE_I
)
3651 else if( h
->sh
.i_type
== SLICE_TYPE_P
)
3653 else if( h
->sh
.i_type
== SLICE_TYPE_B
)
3658 x264_nal_start( h
, NAL_AUD
, NAL_PRIORITY_DISPOSABLE
);
3659 bs_write( &h
->out
.bs
, 3, pic_type
);
3660 bs_rbsp_trailing( &h
->out
.bs
);
3661 if( x264_nal_end( h
) )
3663 overhead
+= h
->out
.nal
[h
->out
.i_nal
-1].i_payload
+ NALU_OVERHEAD
;
3666 h
->i_nal_type
= i_nal_type
;
3667 h
->i_nal_ref_idc
= i_nal_ref_idc
;
3669 if( h
->param
.b_intra_refresh
)
3671 if( IS_X264_TYPE_I( h
->fenc
->i_type
) )
3673 h
->fdec
->i_frames_since_pir
= 0;
3674 h
->b_queued_intra_refresh
= 0;
3675 /* PIR is currently only supported with ref == 1, so any intra frame effectively refreshes
3676 * the whole frame and counts as an intra refresh. */
3677 h
->fdec
->f_pir_position
= h
->mb
.i_mb_width
;
3679 else if( h
->fenc
->i_type
== X264_TYPE_P
)
3681 int pocdiff
= (h
->fdec
->i_poc
- h
->fref
[0][0]->i_poc
)/2;
3682 float increment
= X264_MAX( ((float)h
->mb
.i_mb_width
-1) / h
->param
.i_keyint_max
, 1 );
3683 h
->fdec
->f_pir_position
= h
->fref
[0][0]->f_pir_position
;
3684 h
->fdec
->i_frames_since_pir
= h
->fref
[0][0]->i_frames_since_pir
+ pocdiff
;
3685 if( h
->fdec
->i_frames_since_pir
>= h
->param
.i_keyint_max
||
3686 (h
->b_queued_intra_refresh
&& h
->fdec
->f_pir_position
+ 0.5 >= h
->mb
.i_mb_width
) )
3688 h
->fdec
->f_pir_position
= 0;
3689 h
->fdec
->i_frames_since_pir
= 0;
3690 h
->b_queued_intra_refresh
= 0;
3691 h
->fenc
->b_keyframe
= 1;
3693 h
->fdec
->i_pir_start_col
= h
->fdec
->f_pir_position
+0.5;
3694 h
->fdec
->f_pir_position
+= increment
* pocdiff
;
3695 h
->fdec
->i_pir_end_col
= h
->fdec
->f_pir_position
+0.5;
3696 /* If our intra refresh has reached the right side of the frame, we're done. */
3697 if( h
->fdec
->i_pir_end_col
>= h
->mb
.i_mb_width
- 1 )
3699 h
->fdec
->f_pir_position
= h
->mb
.i_mb_width
;
3700 h
->fdec
->i_pir_end_col
= h
->mb
.i_mb_width
- 1;
3705 if( h
->fenc
->b_keyframe
)
3707 /* Write SPS and PPS */
3708 if( h
->param
.b_repeat_headers
)
3710 /* generate sequence parameters */
3711 x264_nal_start( h
, NAL_SPS
, NAL_PRIORITY_HIGHEST
);
3712 x264_sps_write( &h
->out
.bs
, h
->sps
);
3713 if( x264_nal_end( h
) )
3715 /* Pad AUD/SPS to 256 bytes like Panasonic */
3716 if( h
->param
.i_avcintra_class
)
3717 h
->out
.nal
[h
->out
.i_nal
-1].i_padding
= 256 - bs_pos( &h
->out
.bs
) / 8 - 2*NALU_OVERHEAD
;
3718 overhead
+= h
->out
.nal
[h
->out
.i_nal
-1].i_payload
+ h
->out
.nal
[h
->out
.i_nal
-1].i_padding
+ NALU_OVERHEAD
;
3720 /* generate picture parameters */
3721 x264_nal_start( h
, NAL_PPS
, NAL_PRIORITY_HIGHEST
);
3722 x264_pps_write( &h
->out
.bs
, h
->sps
, h
->pps
);
3723 if( x264_nal_end( h
) )
3725 if( h
->param
.i_avcintra_class
)
3726 h
->out
.nal
[h
->out
.i_nal
-1].i_padding
= 256 - h
->out
.nal
[h
->out
.i_nal
-1].i_payload
- NALU_OVERHEAD
;
3727 overhead
+= h
->out
.nal
[h
->out
.i_nal
-1].i_payload
+ h
->out
.nal
[h
->out
.i_nal
-1].i_padding
+ NALU_OVERHEAD
;
3730 /* when frame threading is used, buffering period sei is written in x264_encoder_frame_end */
3731 if( h
->i_thread_frames
== 1 && h
->sps
->vui
.b_nal_hrd_parameters_present
)
3733 x264_hrd_fullness( h
);
3734 x264_nal_start( h
, NAL_SEI
, NAL_PRIORITY_DISPOSABLE
);
3735 x264_sei_buffering_period_write( h
, &h
->out
.bs
);
3736 if( x264_nal_end( h
) )
3738 overhead
+= h
->out
.nal
[h
->out
.i_nal
-1].i_payload
+ SEI_OVERHEAD
;
3742 /* write extra sei */
3743 for( int i
= 0; i
< h
->fenc
->extra_sei
.num_payloads
; i
++ )
3745 x264_nal_start( h
, NAL_SEI
, NAL_PRIORITY_DISPOSABLE
);
3746 x264_sei_write( &h
->out
.bs
, h
->fenc
->extra_sei
.payloads
[i
].payload
, h
->fenc
->extra_sei
.payloads
[i
].payload_size
,
3747 h
->fenc
->extra_sei
.payloads
[i
].payload_type
);
3748 if( x264_nal_end( h
) )
3750 overhead
+= h
->out
.nal
[h
->out
.i_nal
-1].i_payload
+ SEI_OVERHEAD
;
3751 if( h
->fenc
->extra_sei
.sei_free
)
3753 h
->fenc
->extra_sei
.sei_free( h
->fenc
->extra_sei
.payloads
[i
].payload
);
3754 h
->fenc
->extra_sei
.payloads
[i
].payload
= NULL
;
3758 if( h
->fenc
->extra_sei
.sei_free
)
3760 h
->fenc
->extra_sei
.sei_free( h
->fenc
->extra_sei
.payloads
);
3761 h
->fenc
->extra_sei
.payloads
= NULL
;
3762 h
->fenc
->extra_sei
.sei_free
= NULL
;
3765 if( h
->fenc
->b_keyframe
)
3767 /* Avid's decoder strictly wants two SEIs for AVC-Intra so we can't insert the x264 SEI */
3768 if( h
->param
.b_repeat_headers
&& h
->fenc
->i_frame
== 0 && !h
->param
.i_avcintra_class
)
3770 /* identify ourself */
3771 x264_nal_start( h
, NAL_SEI
, NAL_PRIORITY_DISPOSABLE
);
3772 if( x264_sei_version_write( h
, &h
->out
.bs
) )
3774 if( x264_nal_end( h
) )
3776 overhead
+= h
->out
.nal
[h
->out
.i_nal
-1].i_payload
+ SEI_OVERHEAD
;
3779 if( h
->fenc
->i_type
!= X264_TYPE_IDR
)
3781 int time_to_recovery
= h
->param
.b_open_gop
? 0 : X264_MIN( h
->mb
.i_mb_width
- 1, h
->param
.i_keyint_max
) + h
->param
.i_bframe
- 1;
3782 x264_nal_start( h
, NAL_SEI
, NAL_PRIORITY_DISPOSABLE
);
3783 x264_sei_recovery_point_write( h
, &h
->out
.bs
, time_to_recovery
);
3784 if( x264_nal_end( h
) )
3786 overhead
+= h
->out
.nal
[h
->out
.i_nal
-1].i_payload
+ SEI_OVERHEAD
;
3790 if( h
->param
.i_frame_packing
>= 0 && (h
->fenc
->b_keyframe
|| h
->param
.i_frame_packing
== 5) )
3792 x264_nal_start( h
, NAL_SEI
, NAL_PRIORITY_DISPOSABLE
);
3793 x264_sei_frame_packing_write( h
, &h
->out
.bs
);
3794 if( x264_nal_end( h
) )
3796 overhead
+= h
->out
.nal
[h
->out
.i_nal
-1].i_payload
+ SEI_OVERHEAD
;
3799 /* generate sei pic timing */
3800 if( h
->sps
->vui
.b_pic_struct_present
|| h
->sps
->vui
.b_nal_hrd_parameters_present
)
3802 x264_nal_start( h
, NAL_SEI
, NAL_PRIORITY_DISPOSABLE
);
3803 x264_sei_pic_timing_write( h
, &h
->out
.bs
);
3804 if( x264_nal_end( h
) )
3806 overhead
+= h
->out
.nal
[h
->out
.i_nal
-1].i_payload
+ SEI_OVERHEAD
;
3809 /* As required by Blu-ray. */
3810 if( !IS_X264_TYPE_B( h
->fenc
->i_type
) && h
->b_sh_backup
)
3813 x264_nal_start( h
, NAL_SEI
, NAL_PRIORITY_DISPOSABLE
);
3814 x264_sei_dec_ref_pic_marking_write( h
, &h
->out
.bs
);
3815 if( x264_nal_end( h
) )
3817 overhead
+= h
->out
.nal
[h
->out
.i_nal
-1].i_payload
+ SEI_OVERHEAD
;
3820 if( h
->fenc
->b_keyframe
&& h
->param
.b_intra_refresh
)
3821 h
->i_cpb_delay_pir_offset_next
= h
->fenc
->i_cpb_delay
;
3823 /* Filler space: 10 or 18 SEIs' worth of space, depending on resolution */
3824 if( h
->param
.i_avcintra_class
)
3826 /* Write an empty filler NAL to mimic the AUD in the P2 format*/
3827 x264_nal_start( h
, NAL_FILLER
, NAL_PRIORITY_DISPOSABLE
);
3828 x264_filler_write( h
, &h
->out
.bs
, 0 );
3829 if( x264_nal_end( h
) )
3831 overhead
+= h
->out
.nal
[h
->out
.i_nal
-1].i_payload
+ NALU_OVERHEAD
;
3833 /* All lengths are magic lengths that decoders expect to see */
3835 x264_nal_start( h
, NAL_SEI
, NAL_PRIORITY_DISPOSABLE
);
3836 if( x264_sei_avcintra_umid_write( h
, &h
->out
.bs
) < 0 )
3838 if( x264_nal_end( h
) )
3840 overhead
+= h
->out
.nal
[h
->out
.i_nal
-1].i_payload
+ SEI_OVERHEAD
;
3844 if( h
->param
.i_height
== 1080 )
3846 unpadded_len
= 5780;
3851 unpadded_len
= 2900;
3855 x264_nal_start( h
, NAL_SEI
, NAL_PRIORITY_DISPOSABLE
);
3856 if( x264_sei_avcintra_vanc_write( h
, &h
->out
.bs
, unpadded_len
) < 0 )
3858 if( x264_nal_end( h
) )
3861 h
->out
.nal
[h
->out
.i_nal
-1].i_padding
= total_len
- h
->out
.nal
[h
->out
.i_nal
-1].i_payload
- SEI_OVERHEAD
;
3862 overhead
+= h
->out
.nal
[h
->out
.i_nal
-1].i_payload
+ h
->out
.nal
[h
->out
.i_nal
-1].i_padding
+ SEI_OVERHEAD
;
3865 /* Init the rate control */
3866 /* FIXME: Include slice header bit cost. */
3867 x264_ratecontrol_start( h
, h
->fenc
->i_qpplus1
, overhead
*8 );
3868 i_global_qp
= x264_ratecontrol_qp( h
);
3870 pic_out
->i_qpplus1
=
3871 h
->fdec
->i_qpplus1
= i_global_qp
+ 1;
3873 if( h
->param
.rc
.b_stat_read
&& h
->sh
.i_type
!= SLICE_TYPE_I
)
3875 x264_reference_build_list_optimal( h
);
3876 x264_reference_check_reorder( h
);
3880 h
->fdec
->i_poc_l0ref0
= h
->fref
[0][0]->i_poc
;
3882 /* ------------------------ Create slice header ----------------------- */
3883 x264_slice_init( h
, i_nal_type
, i_global_qp
);
3885 /*------------------------- Weights -------------------------------------*/
3886 if( h
->sh
.i_type
== SLICE_TYPE_B
)
3887 x264_macroblock_bipred_init( h
);
3889 x264_weighted_pred_init( h
);
3891 if( i_nal_ref_idc
!= NAL_PRIORITY_DISPOSABLE
)
3895 h
->i_threadslice_start
= 0;
3896 h
->i_threadslice_end
= h
->mb
.i_mb_height
;
3897 if( h
->i_thread_frames
> 1 )
3899 x264_threadpool_run( h
->threadpool
, (void*)x264_slices_write
, h
);
3900 h
->b_thread_active
= 1;
3902 else if( h
->param
.b_sliced_threads
)
3904 if( x264_threaded_slices_write( h
) )
3908 if( (intptr_t)x264_slices_write( h
) )
3911 return x264_encoder_frame_end( thread_oldest
, thread_current
, pp_nal
, pi_nal
, pic_out
);
3914 static int x264_encoder_frame_end( x264_t
*h
, x264_t
*thread_current
,
3915 x264_nal_t
**pp_nal
, int *pi_nal
,
3916 x264_picture_t
*pic_out
)
3918 char psz_message
[80];
3920 if( !h
->param
.b_sliced_threads
&& h
->b_thread_active
)
3922 h
->b_thread_active
= 0;
3923 if( (intptr_t)x264_threadpool_wait( h
->threadpool
, h
) )
3928 pic_out
->i_type
= X264_TYPE_AUTO
;
3934 /* generate buffering period sei and insert it into place */
3935 if( h
->i_thread_frames
> 1 && h
->fenc
->b_keyframe
&& h
->sps
->vui
.b_nal_hrd_parameters_present
)
3937 x264_hrd_fullness( h
);
3938 x264_nal_start( h
, NAL_SEI
, NAL_PRIORITY_DISPOSABLE
);
3939 x264_sei_buffering_period_write( h
, &h
->out
.bs
);
3940 if( x264_nal_end( h
) )
3942 /* buffering period sei must follow AUD, SPS and PPS and precede all other SEIs */
3944 while( h
->out
.nal
[idx
].i_type
== NAL_AUD
||
3945 h
->out
.nal
[idx
].i_type
== NAL_SPS
||
3946 h
->out
.nal
[idx
].i_type
== NAL_PPS
)
3948 x264_nal_t nal_tmp
= h
->out
.nal
[h
->out
.i_nal
-1];
3949 memmove( &h
->out
.nal
[idx
+1], &h
->out
.nal
[idx
], (h
->out
.i_nal
-idx
-1)*sizeof(x264_nal_t
) );
3950 h
->out
.nal
[idx
] = nal_tmp
;
3953 int frame_size
= x264_encoder_encapsulate_nals( h
, 0 );
3954 if( frame_size
< 0 )
3957 /* Set output picture properties */
3958 pic_out
->i_type
= h
->fenc
->i_type
;
3960 pic_out
->b_keyframe
= h
->fenc
->b_keyframe
;
3961 pic_out
->i_pic_struct
= h
->fenc
->i_pic_struct
;
3963 pic_out
->i_frame_num
= h
->fdec
->i_frame_num
;
3965 pic_out
->i_pts
= h
->fdec
->i_pts
;
3966 pic_out
->i_dts
= h
->fdec
->i_dts
;
3968 if( pic_out
->i_pts
< pic_out
->i_dts
)
3969 x264_log( h
, X264_LOG_WARNING
, "invalid DTS: PTS is less than DTS\n" );
3971 pic_out
->opaque
= h
->fenc
->opaque
;
3973 pic_out
->img
.i_csp
= h
->fdec
->i_csp
;
3975 pic_out
->img
.i_csp
|= X264_CSP_HIGH_DEPTH
;
3977 pic_out
->img
.i_plane
= h
->fdec
->i_plane
;
3978 for( int i
= 0; i
< pic_out
->img
.i_plane
; i
++ )
3980 pic_out
->img
.i_stride
[i
] = h
->fdec
->i_stride
[i
] * sizeof(pixel
);
3981 pic_out
->img
.plane
[i
] = (uint8_t*)h
->fdec
->plane
[i
];
3984 x264_frame_push_unused( thread_current
, h
->fenc
);
3986 /* ---------------------- Update encoder state ------------------------- */
3990 if( x264_ratecontrol_end( h
, frame_size
* 8, &filler
) < 0 )
3993 pic_out
->hrd_timing
= h
->fenc
->hrd_timing
;
3994 pic_out
->prop
.f_crf_avg
= h
->fdec
->f_crf_avg
;
3996 /* Filler in AVC-Intra mode is written as zero bytes to the last slice
3997 * We don't know the size of the last slice until encapsulation so we add filler to the encapsulated NAL */
3998 if( h
->param
.i_avcintra_class
)
4000 if( x264_check_encapsulated_buffer( h
, h
->thread
[0], h
->out
.i_nal
, frame_size
, frame_size
+ filler
) < 0 )
4003 x264_nal_t
*nal
= &h
->out
.nal
[h
->out
.i_nal
-1];
4004 memset( nal
->p_payload
+ nal
->i_payload
, 0, filler
);
4005 nal
->i_payload
+= filler
;
4006 nal
->i_padding
= filler
;
4007 frame_size
+= filler
;
4009 /* Fix up the size header for mp4/etc */
4010 if( !h
->param
.b_annexb
)
4012 /* Size doesn't include the size of the header we're writing now. */
4013 uint8_t *nal_data
= nal
->p_payload
;
4014 int chunk_size
= nal
->i_payload
- 4;
4015 nal_data
[0] = chunk_size
>> 24;
4016 nal_data
[1] = chunk_size
>> 16;
4017 nal_data
[2] = chunk_size
>> 8;
4018 nal_data
[3] = chunk_size
>> 0;
4025 int f
, overhead
= FILLER_OVERHEAD
- h
->param
.b_annexb
;
4026 if( h
->param
.i_slice_max_size
&& filler
> h
->param
.i_slice_max_size
)
4028 int next_size
= filler
- h
->param
.i_slice_max_size
;
4029 int overflow
= X264_MAX( overhead
- next_size
, 0 );
4030 f
= h
->param
.i_slice_max_size
- overhead
- overflow
;
4033 f
= X264_MAX( 0, filler
- overhead
);
4035 if( x264_bitstream_check_buffer_filler( h
, f
) )
4037 x264_nal_start( h
, NAL_FILLER
, NAL_PRIORITY_DISPOSABLE
);
4038 x264_filler_write( h
, &h
->out
.bs
, f
);
4039 if( x264_nal_end( h
) )
4041 int total_size
= x264_encoder_encapsulate_nals( h
, h
->out
.i_nal
-1 );
4042 if( total_size
< 0 )
4044 frame_size
+= total_size
;
4045 filler
-= total_size
;
4049 /* End bitstream, set output */
4050 *pi_nal
= h
->out
.i_nal
;
4051 *pp_nal
= h
->out
.nal
;
4055 x264_noise_reduction_update( h
);
4057 /* ---------------------- Compute/Print statistics --------------------- */
4058 x264_thread_sync_stat( h
, h
->thread
[0] );
4061 h
->stat
.i_frame_count
[h
->sh
.i_type
]++;
4062 h
->stat
.i_frame_size
[h
->sh
.i_type
] += frame_size
;
4063 h
->stat
.f_frame_qp
[h
->sh
.i_type
] += h
->fdec
->f_qp_avg_aq
;
4065 for( int i
= 0; i
< X264_MBTYPE_MAX
; i
++ )
4066 h
->stat
.i_mb_count
[h
->sh
.i_type
][i
] += h
->stat
.frame
.i_mb_count
[i
];
4067 for( int i
= 0; i
< X264_PARTTYPE_MAX
; i
++ )
4068 h
->stat
.i_mb_partition
[h
->sh
.i_type
][i
] += h
->stat
.frame
.i_mb_partition
[i
];
4069 for( int i
= 0; i
< 2; i
++ )
4070 h
->stat
.i_mb_count_8x8dct
[i
] += h
->stat
.frame
.i_mb_count_8x8dct
[i
];
4071 for( int i
= 0; i
< 6; i
++ )
4072 h
->stat
.i_mb_cbp
[i
] += h
->stat
.frame
.i_mb_cbp
[i
];
4073 for( int i
= 0; i
< 4; i
++ )
4074 for( int j
= 0; j
< 13; j
++ )
4075 h
->stat
.i_mb_pred_mode
[i
][j
] += h
->stat
.frame
.i_mb_pred_mode
[i
][j
];
4076 if( h
->sh
.i_type
!= SLICE_TYPE_I
)
4077 for( int i_list
= 0; i_list
< 2; i_list
++ )
4078 for( int i
= 0; i
< X264_REF_MAX
*2; i
++ )
4079 h
->stat
.i_mb_count_ref
[h
->sh
.i_type
][i_list
][i
] += h
->stat
.frame
.i_mb_count_ref
[i_list
][i
];
4080 for( int i
= 0; i
< 3; i
++ )
4081 h
->stat
.i_mb_field
[i
] += h
->stat
.frame
.i_mb_field
[i
];
4082 if( h
->sh
.i_type
== SLICE_TYPE_P
&& h
->param
.analyse
.i_weighted_pred
>= X264_WEIGHTP_SIMPLE
)
4084 h
->stat
.i_wpred
[0] += !!h
->sh
.weight
[0][0].weightfn
|| !!h
->sh
.weight
[1][0].weightfn
;
4085 h
->stat
.i_wpred
[1] += !!h
->sh
.weight
[0][1].weightfn
|| !!h
->sh
.weight
[0][2].weightfn
;
4087 if( h
->sh
.i_type
== SLICE_TYPE_B
)
4089 h
->stat
.i_direct_frames
[ h
->sh
.b_direct_spatial_mv_pred
] ++;
4090 if( h
->mb
.b_direct_auto_write
)
4092 //FIXME somewhat arbitrary time constants
4093 if( h
->stat
.i_direct_score
[0] + h
->stat
.i_direct_score
[1] > h
->mb
.i_mb_count
)
4094 for( int i
= 0; i
< 2; i
++ )
4095 h
->stat
.i_direct_score
[i
] = h
->stat
.i_direct_score
[i
] * 9/10;
4096 for( int i
= 0; i
< 2; i
++ )
4097 h
->stat
.i_direct_score
[i
] += h
->stat
.frame
.i_direct_score
[i
];
4101 h
->stat
.i_consecutive_bframes
[h
->fenc
->i_bframes
]++;
4103 psz_message
[0] = '\0';
4104 double dur
= h
->fenc
->f_duration
;
4105 h
->stat
.f_frame_duration
[h
->sh
.i_type
] += dur
;
4106 if( h
->param
.analyse
.b_psnr
)
4110 h
->stat
.frame
.i_ssd
[0],
4111 h
->stat
.frame
.i_ssd
[1],
4112 h
->stat
.frame
.i_ssd
[2],
4114 int luma_size
= h
->param
.i_width
* h
->param
.i_height
;
4115 int chroma_size
= CHROMA_SIZE( luma_size
);
4116 pic_out
->prop
.f_psnr
[0] = x264_psnr( ssd
[0], luma_size
);
4117 pic_out
->prop
.f_psnr
[1] = x264_psnr( ssd
[1], chroma_size
);
4118 pic_out
->prop
.f_psnr
[2] = x264_psnr( ssd
[2], chroma_size
);
4119 pic_out
->prop
.f_psnr_avg
= x264_psnr( ssd
[0] + ssd
[1] + ssd
[2], luma_size
+ chroma_size
*2 );
4121 h
->stat
.f_ssd_global
[h
->sh
.i_type
] += dur
* (ssd
[0] + ssd
[1] + ssd
[2]);
4122 h
->stat
.f_psnr_average
[h
->sh
.i_type
] += dur
* pic_out
->prop
.f_psnr_avg
;
4123 h
->stat
.f_psnr_mean_y
[h
->sh
.i_type
] += dur
* pic_out
->prop
.f_psnr
[0];
4124 h
->stat
.f_psnr_mean_u
[h
->sh
.i_type
] += dur
* pic_out
->prop
.f_psnr
[1];
4125 h
->stat
.f_psnr_mean_v
[h
->sh
.i_type
] += dur
* pic_out
->prop
.f_psnr
[2];
4127 snprintf( psz_message
, 80, " PSNR Y:%5.2f U:%5.2f V:%5.2f", pic_out
->prop
.f_psnr
[0],
4128 pic_out
->prop
.f_psnr
[1],
4129 pic_out
->prop
.f_psnr
[2] );
4132 if( h
->param
.analyse
.b_ssim
)
4134 pic_out
->prop
.f_ssim
= h
->stat
.frame
.f_ssim
/ h
->stat
.frame
.i_ssim_cnt
;
4135 h
->stat
.f_ssim_mean_y
[h
->sh
.i_type
] += pic_out
->prop
.f_ssim
* dur
;
4136 int msg_len
= strlen(psz_message
);
4137 snprintf( psz_message
+ msg_len
, 80 - msg_len
, " SSIM Y:%.5f", pic_out
->prop
.f_ssim
);
4139 psz_message
[79] = '\0';
4141 x264_log( h
, X264_LOG_DEBUG
,
4142 "frame=%4d QP=%.2f NAL=%d Slice:%c Poc:%-3d I:%-4d P:%-4d SKIP:%-4d size=%d bytes%s\n",
4144 h
->fdec
->f_qp_avg_aq
,
4146 h
->sh
.i_type
== SLICE_TYPE_I
? 'I' : (h
->sh
.i_type
== SLICE_TYPE_P
? 'P' : 'B' ),
4148 h
->stat
.frame
.i_mb_count_i
,
4149 h
->stat
.frame
.i_mb_count_p
,
4150 h
->stat
.frame
.i_mb_count_skip
,
4154 // keep stats all in one place
4155 x264_thread_sync_stat( h
->thread
[0], h
);
4156 // for the use of the next frame
4157 x264_thread_sync_stat( thread_current
, h
);
4159 #ifdef DEBUG_MB_TYPE
4161 static const char mb_chars
[] = { 'i', 'i', 'I', 'C', 'P', '8', 'S',
4162 'D', '<', 'X', 'B', 'X', '>', 'B', 'B', 'B', 'B', '8', 'S' };
4163 for( int mb_xy
= 0; mb_xy
< h
->mb
.i_mb_width
* h
->mb
.i_mb_height
; mb_xy
++ )
4165 if( h
->mb
.type
[mb_xy
] < X264_MBTYPE_MAX
&& h
->mb
.type
[mb_xy
] >= 0 )
4166 fprintf( stderr
, "%c ", mb_chars
[ h
->mb
.type
[mb_xy
] ] );
4168 fprintf( stderr
, "? " );
4170 if( (mb_xy
+1) % h
->mb
.i_mb_width
== 0 )
4171 fprintf( stderr
, "\n" );
4176 /* Remove duplicates, must be done near the end as breaks h->fref0 array
4177 * by freeing some of its pointers. */
4178 for( int i
= 0; i
< h
->i_ref
[0]; i
++ )
4179 if( h
->fref
[0][i
] && h
->fref
[0][i
]->b_duplicate
)
4181 x264_frame_push_blank_unused( h
, h
->fref
[0][i
] );
4185 if( h
->param
.psz_dump_yuv
)
4186 x264_frame_dump( h
);
4192 static void x264_print_intra( int64_t *i_mb_count
, double i_count
, int b_print_pcm
, char *intra
)
4194 intra
+= sprintf( intra
, "I16..4%s: %4.1f%% %4.1f%% %4.1f%%",
4195 b_print_pcm
? "..PCM" : "",
4196 i_mb_count
[I_16x16
]/ i_count
,
4197 i_mb_count
[I_8x8
] / i_count
,
4198 i_mb_count
[I_4x4
] / i_count
);
4200 sprintf( intra
, " %4.1f%%", i_mb_count
[I_PCM
] / i_count
);
4203 /****************************************************************************
4204 * x264_encoder_close:
4205 ****************************************************************************/
4206 void x264_encoder_close ( x264_t
*h
)
4208 int64_t i_yuv_size
= FRAME_SIZE( h
->param
.i_width
* h
->param
.i_height
);
4209 int64_t i_mb_count_size
[2][7] = {{0}};
4211 int b_print_pcm
= h
->stat
.i_mb_count
[SLICE_TYPE_I
][I_PCM
]
4212 || h
->stat
.i_mb_count
[SLICE_TYPE_P
][I_PCM
]
4213 || h
->stat
.i_mb_count
[SLICE_TYPE_B
][I_PCM
];
4215 x264_lookahead_delete( h
);
4218 x264_opencl_lookahead_delete( h
);
4219 x264_opencl_function_t
*ocl
= h
->opencl
.ocl
;
4222 if( h
->param
.b_sliced_threads
)
4223 x264_threadpool_wait_all( h
);
4224 if( h
->param
.i_threads
> 1 )
4225 x264_threadpool_delete( h
->threadpool
);
4226 if( h
->param
.i_lookahead_threads
> 1 )
4227 x264_threadpool_delete( h
->lookaheadpool
);
4228 if( h
->i_thread_frames
> 1 )
4230 for( int i
= 0; i
< h
->i_thread_frames
; i
++ )
4231 if( h
->thread
[i
]->b_thread_active
)
4233 assert( h
->thread
[i
]->fenc
->i_reference_count
== 1 );
4234 x264_frame_delete( h
->thread
[i
]->fenc
);
4237 x264_t
*thread_prev
= h
->thread
[h
->i_thread_phase
];
4238 x264_thread_sync_ratecontrol( h
, thread_prev
, h
);
4239 x264_thread_sync_ratecontrol( thread_prev
, thread_prev
, h
);
4240 h
->i_frame
= thread_prev
->i_frame
+ 1 - h
->i_thread_frames
;
4244 /* Slices used and PSNR */
4245 for( int i
= 0; i
< 3; i
++ )
4247 static const uint8_t slice_order
[] = { SLICE_TYPE_I
, SLICE_TYPE_P
, SLICE_TYPE_B
};
4248 int i_slice
= slice_order
[i
];
4250 if( h
->stat
.i_frame_count
[i_slice
] > 0 )
4252 int i_count
= h
->stat
.i_frame_count
[i_slice
];
4253 double dur
= h
->stat
.f_frame_duration
[i_slice
];
4254 if( h
->param
.analyse
.b_psnr
)
4256 x264_log( h
, X264_LOG_INFO
,
4257 "frame %c:%-5d Avg QP:%5.2f size:%6.0f PSNR Mean Y:%5.2f U:%5.2f V:%5.2f Avg:%5.2f Global:%5.2f\n",
4258 slice_type_to_char
[i_slice
],
4260 h
->stat
.f_frame_qp
[i_slice
] / i_count
,
4261 (double)h
->stat
.i_frame_size
[i_slice
] / i_count
,
4262 h
->stat
.f_psnr_mean_y
[i_slice
] / dur
, h
->stat
.f_psnr_mean_u
[i_slice
] / dur
, h
->stat
.f_psnr_mean_v
[i_slice
] / dur
,
4263 h
->stat
.f_psnr_average
[i_slice
] / dur
,
4264 x264_psnr( h
->stat
.f_ssd_global
[i_slice
], dur
* i_yuv_size
) );
4268 x264_log( h
, X264_LOG_INFO
,
4269 "frame %c:%-5d Avg QP:%5.2f size:%6.0f\n",
4270 slice_type_to_char
[i_slice
],
4272 h
->stat
.f_frame_qp
[i_slice
] / i_count
,
4273 (double)h
->stat
.i_frame_size
[i_slice
] / i_count
);
4277 if( h
->param
.i_bframe
&& h
->stat
.i_frame_count
[SLICE_TYPE_B
] )
4281 // weight by number of frames (including the I/P-frames) that are in a sequence of N B-frames
4282 for( int i
= 0; i
<= h
->param
.i_bframe
; i
++ )
4283 den
+= (i
+1) * h
->stat
.i_consecutive_bframes
[i
];
4284 for( int i
= 0; i
<= h
->param
.i_bframe
; i
++ )
4285 p
+= sprintf( p
, " %4.1f%%", 100. * (i
+1) * h
->stat
.i_consecutive_bframes
[i
] / den
);
4286 x264_log( h
, X264_LOG_INFO
, "consecutive B-frames:%s\n", buf
);
4289 for( int i_type
= 0; i_type
< 2; i_type
++ )
4290 for( int i
= 0; i
< X264_PARTTYPE_MAX
; i
++ )
4292 if( i
== D_DIRECT_8x8
) continue; /* direct is counted as its own type */
4293 i_mb_count_size
[i_type
][x264_mb_partition_pixel_table
[i
]] += h
->stat
.i_mb_partition
[i_type
][i
];
4297 if( h
->stat
.i_frame_count
[SLICE_TYPE_I
] > 0 )
4299 int64_t *i_mb_count
= h
->stat
.i_mb_count
[SLICE_TYPE_I
];
4300 double i_count
= (double)h
->stat
.i_frame_count
[SLICE_TYPE_I
] * h
->mb
.i_mb_count
/ 100.0;
4301 x264_print_intra( i_mb_count
, i_count
, b_print_pcm
, buf
);
4302 x264_log( h
, X264_LOG_INFO
, "mb I %s\n", buf
);
4304 if( h
->stat
.i_frame_count
[SLICE_TYPE_P
] > 0 )
4306 int64_t *i_mb_count
= h
->stat
.i_mb_count
[SLICE_TYPE_P
];
4307 double i_count
= (double)h
->stat
.i_frame_count
[SLICE_TYPE_P
] * h
->mb
.i_mb_count
/ 100.0;
4308 int64_t *i_mb_size
= i_mb_count_size
[SLICE_TYPE_P
];
4309 x264_print_intra( i_mb_count
, i_count
, b_print_pcm
, buf
);
4310 x264_log( h
, X264_LOG_INFO
,
4311 "mb P %s P16..4: %4.1f%% %4.1f%% %4.1f%% %4.1f%% %4.1f%% skip:%4.1f%%\n",
4313 i_mb_size
[PIXEL_16x16
] / (i_count
*4),
4314 (i_mb_size
[PIXEL_16x8
] + i_mb_size
[PIXEL_8x16
]) / (i_count
*4),
4315 i_mb_size
[PIXEL_8x8
] / (i_count
*4),
4316 (i_mb_size
[PIXEL_8x4
] + i_mb_size
[PIXEL_4x8
]) / (i_count
*4),
4317 i_mb_size
[PIXEL_4x4
] / (i_count
*4),
4318 i_mb_count
[P_SKIP
] / i_count
);
4320 if( h
->stat
.i_frame_count
[SLICE_TYPE_B
] > 0 )
4322 int64_t *i_mb_count
= h
->stat
.i_mb_count
[SLICE_TYPE_B
];
4323 double i_count
= (double)h
->stat
.i_frame_count
[SLICE_TYPE_B
] * h
->mb
.i_mb_count
/ 100.0;
4324 double i_mb_list_count
;
4325 int64_t *i_mb_size
= i_mb_count_size
[SLICE_TYPE_B
];
4326 int64_t list_count
[3] = {0}; /* 0 == L0, 1 == L1, 2 == BI */
4327 x264_print_intra( i_mb_count
, i_count
, b_print_pcm
, buf
);
4328 for( int i
= 0; i
< X264_PARTTYPE_MAX
; i
++ )
4329 for( int j
= 0; j
< 2; j
++ )
4331 int l0
= x264_mb_type_list_table
[i
][0][j
];
4332 int l1
= x264_mb_type_list_table
[i
][1][j
];
4334 list_count
[l1
+l0
*l1
] += h
->stat
.i_mb_count
[SLICE_TYPE_B
][i
] * 2;
4336 list_count
[0] += h
->stat
.i_mb_partition
[SLICE_TYPE_B
][D_L0_8x8
];
4337 list_count
[1] += h
->stat
.i_mb_partition
[SLICE_TYPE_B
][D_L1_8x8
];
4338 list_count
[2] += h
->stat
.i_mb_partition
[SLICE_TYPE_B
][D_BI_8x8
];
4339 i_mb_count
[B_DIRECT
] += (h
->stat
.i_mb_partition
[SLICE_TYPE_B
][D_DIRECT_8x8
]+2)/4;
4340 i_mb_list_count
= (list_count
[0] + list_count
[1] + list_count
[2]) / 100.0;
4341 sprintf( buf
+ strlen(buf
), " B16..8: %4.1f%% %4.1f%% %4.1f%% direct:%4.1f%% skip:%4.1f%%",
4342 i_mb_size
[PIXEL_16x16
] / (i_count
*4),
4343 (i_mb_size
[PIXEL_16x8
] + i_mb_size
[PIXEL_8x16
]) / (i_count
*4),
4344 i_mb_size
[PIXEL_8x8
] / (i_count
*4),
4345 i_mb_count
[B_DIRECT
] / i_count
,
4346 i_mb_count
[B_SKIP
] / i_count
);
4347 if( i_mb_list_count
!= 0 )
4348 sprintf( buf
+ strlen(buf
), " L0:%4.1f%% L1:%4.1f%% BI:%4.1f%%",
4349 list_count
[0] / i_mb_list_count
,
4350 list_count
[1] / i_mb_list_count
,
4351 list_count
[2] / i_mb_list_count
);
4352 x264_log( h
, X264_LOG_INFO
, "mb B %s\n", buf
);
4355 x264_ratecontrol_summary( h
);
4357 if( h
->stat
.i_frame_count
[SLICE_TYPE_I
] + h
->stat
.i_frame_count
[SLICE_TYPE_P
] + h
->stat
.i_frame_count
[SLICE_TYPE_B
] > 0 )
4359 #define SUM3(p) (p[SLICE_TYPE_I] + p[SLICE_TYPE_P] + p[SLICE_TYPE_B])
4360 #define SUM3b(p,o) (p[SLICE_TYPE_I][o] + p[SLICE_TYPE_P][o] + p[SLICE_TYPE_B][o])
4361 int64_t i_i8x8
= SUM3b( h
->stat
.i_mb_count
, I_8x8
);
4362 int64_t i_intra
= i_i8x8
+ SUM3b( h
->stat
.i_mb_count
, I_4x4
)
4363 + SUM3b( h
->stat
.i_mb_count
, I_16x16
);
4364 int64_t i_all_intra
= i_intra
+ SUM3b( h
->stat
.i_mb_count
, I_PCM
);
4365 int64_t i_skip
= SUM3b( h
->stat
.i_mb_count
, P_SKIP
)
4366 + SUM3b( h
->stat
.i_mb_count
, B_SKIP
);
4367 const int i_count
= h
->stat
.i_frame_count
[SLICE_TYPE_I
] +
4368 h
->stat
.i_frame_count
[SLICE_TYPE_P
] +
4369 h
->stat
.i_frame_count
[SLICE_TYPE_B
];
4370 int64_t i_mb_count
= (int64_t)i_count
* h
->mb
.i_mb_count
;
4371 int64_t i_inter
= i_mb_count
- i_skip
- i_intra
;
4372 const double duration
= h
->stat
.f_frame_duration
[SLICE_TYPE_I
] +
4373 h
->stat
.f_frame_duration
[SLICE_TYPE_P
] +
4374 h
->stat
.f_frame_duration
[SLICE_TYPE_B
];
4375 float f_bitrate
= SUM3(h
->stat
.i_frame_size
) / duration
/ 125;
4377 if( PARAM_INTERLACED
)
4379 char *fieldstats
= buf
;
4382 fieldstats
+= sprintf( fieldstats
, " inter:%.1f%%", h
->stat
.i_mb_field
[1] * 100.0 / i_inter
);
4384 fieldstats
+= sprintf( fieldstats
, " skip:%.1f%%", h
->stat
.i_mb_field
[2] * 100.0 / i_skip
);
4385 x264_log( h
, X264_LOG_INFO
, "field mbs: intra: %.1f%%%s\n",
4386 h
->stat
.i_mb_field
[0] * 100.0 / i_intra
, buf
);
4389 if( h
->pps
->b_transform_8x8_mode
)
4392 if( h
->stat
.i_mb_count_8x8dct
[0] )
4393 sprintf( buf
, " inter:%.1f%%", 100. * h
->stat
.i_mb_count_8x8dct
[1] / h
->stat
.i_mb_count_8x8dct
[0] );
4394 x264_log( h
, X264_LOG_INFO
, "8x8 transform intra:%.1f%%%s\n", 100. * i_i8x8
/ i_intra
, buf
);
4397 if( (h
->param
.analyse
.i_direct_mv_pred
== X264_DIRECT_PRED_AUTO
||
4398 (h
->stat
.i_direct_frames
[0] && h
->stat
.i_direct_frames
[1]))
4399 && h
->stat
.i_frame_count
[SLICE_TYPE_B
] )
4401 x264_log( h
, X264_LOG_INFO
, "direct mvs spatial:%.1f%% temporal:%.1f%%\n",
4402 h
->stat
.i_direct_frames
[1] * 100. / h
->stat
.i_frame_count
[SLICE_TYPE_B
],
4403 h
->stat
.i_direct_frames
[0] * 100. / h
->stat
.i_frame_count
[SLICE_TYPE_B
] );
4407 int csize
= CHROMA444
? 4 : 1;
4408 if( i_mb_count
!= i_all_intra
)
4409 sprintf( buf
, " inter: %.1f%% %.1f%% %.1f%%",
4410 h
->stat
.i_mb_cbp
[1] * 100.0 / ((i_mb_count
- i_all_intra
)*4),
4411 h
->stat
.i_mb_cbp
[3] * 100.0 / ((i_mb_count
- i_all_intra
)*csize
),
4412 h
->stat
.i_mb_cbp
[5] * 100.0 / ((i_mb_count
- i_all_intra
)*csize
) );
4413 x264_log( h
, X264_LOG_INFO
, "coded y,%s,%s intra: %.1f%% %.1f%% %.1f%%%s\n",
4414 CHROMA444
?"u":"uvDC", CHROMA444
?"v":"uvAC",
4415 h
->stat
.i_mb_cbp
[0] * 100.0 / (i_all_intra
*4),
4416 h
->stat
.i_mb_cbp
[2] * 100.0 / (i_all_intra
*csize
),
4417 h
->stat
.i_mb_cbp
[4] * 100.0 / (i_all_intra
*csize
), buf
);
4419 int64_t fixed_pred_modes
[4][9] = {{0}};
4420 int64_t sum_pred_modes
[4] = {0};
4421 for( int i
= 0; i
<= I_PRED_16x16_DC_128
; i
++ )
4423 fixed_pred_modes
[0][x264_mb_pred_mode16x16_fix
[i
]] += h
->stat
.i_mb_pred_mode
[0][i
];
4424 sum_pred_modes
[0] += h
->stat
.i_mb_pred_mode
[0][i
];
4426 if( sum_pred_modes
[0] )
4427 x264_log( h
, X264_LOG_INFO
, "i16 v,h,dc,p: %2.0f%% %2.0f%% %2.0f%% %2.0f%%\n",
4428 fixed_pred_modes
[0][0] * 100.0 / sum_pred_modes
[0],
4429 fixed_pred_modes
[0][1] * 100.0 / sum_pred_modes
[0],
4430 fixed_pred_modes
[0][2] * 100.0 / sum_pred_modes
[0],
4431 fixed_pred_modes
[0][3] * 100.0 / sum_pred_modes
[0] );
4432 for( int i
= 1; i
<= 2; i
++ )
4434 for( int j
= 0; j
<= I_PRED_8x8_DC_128
; j
++ )
4436 fixed_pred_modes
[i
][x264_mb_pred_mode4x4_fix(j
)] += h
->stat
.i_mb_pred_mode
[i
][j
];
4437 sum_pred_modes
[i
] += h
->stat
.i_mb_pred_mode
[i
][j
];
4439 if( sum_pred_modes
[i
] )
4440 x264_log( h
, X264_LOG_INFO
, "i%d v,h,dc,ddl,ddr,vr,hd,vl,hu: %2.0f%% %2.0f%% %2.0f%% %2.0f%% %2.0f%% %2.0f%% %2.0f%% %2.0f%% %2.0f%%\n", (3-i
)*4,
4441 fixed_pred_modes
[i
][0] * 100.0 / sum_pred_modes
[i
],
4442 fixed_pred_modes
[i
][1] * 100.0 / sum_pred_modes
[i
],
4443 fixed_pred_modes
[i
][2] * 100.0 / sum_pred_modes
[i
],
4444 fixed_pred_modes
[i
][3] * 100.0 / sum_pred_modes
[i
],
4445 fixed_pred_modes
[i
][4] * 100.0 / sum_pred_modes
[i
],
4446 fixed_pred_modes
[i
][5] * 100.0 / sum_pred_modes
[i
],
4447 fixed_pred_modes
[i
][6] * 100.0 / sum_pred_modes
[i
],
4448 fixed_pred_modes
[i
][7] * 100.0 / sum_pred_modes
[i
],
4449 fixed_pred_modes
[i
][8] * 100.0 / sum_pred_modes
[i
] );
4451 for( int i
= 0; i
<= I_PRED_CHROMA_DC_128
; i
++ )
4453 fixed_pred_modes
[3][x264_mb_chroma_pred_mode_fix
[i
]] += h
->stat
.i_mb_pred_mode
[3][i
];
4454 sum_pred_modes
[3] += h
->stat
.i_mb_pred_mode
[3][i
];
4456 if( sum_pred_modes
[3] && !CHROMA444
)
4457 x264_log( h
, X264_LOG_INFO
, "i8c dc,h,v,p: %2.0f%% %2.0f%% %2.0f%% %2.0f%%\n",
4458 fixed_pred_modes
[3][0] * 100.0 / sum_pred_modes
[3],
4459 fixed_pred_modes
[3][1] * 100.0 / sum_pred_modes
[3],
4460 fixed_pred_modes
[3][2] * 100.0 / sum_pred_modes
[3],
4461 fixed_pred_modes
[3][3] * 100.0 / sum_pred_modes
[3] );
4463 if( h
->param
.analyse
.i_weighted_pred
>= X264_WEIGHTP_SIMPLE
&& h
->stat
.i_frame_count
[SLICE_TYPE_P
] > 0 )
4464 x264_log( h
, X264_LOG_INFO
, "Weighted P-Frames: Y:%.1f%% UV:%.1f%%\n",
4465 h
->stat
.i_wpred
[0] * 100.0 / h
->stat
.i_frame_count
[SLICE_TYPE_P
],
4466 h
->stat
.i_wpred
[1] * 100.0 / h
->stat
.i_frame_count
[SLICE_TYPE_P
] );
4468 for( int i_list
= 0; i_list
< 2; i_list
++ )
4469 for( int i_slice
= 0; i_slice
< 2; i_slice
++ )
4474 for( int i
= 0; i
< X264_REF_MAX
*2; i
++ )
4475 if( h
->stat
.i_mb_count_ref
[i_slice
][i_list
][i
] )
4477 i_den
+= h
->stat
.i_mb_count_ref
[i_slice
][i_list
][i
];
4482 for( int i
= 0; i
<= i_max
; i
++ )
4483 p
+= sprintf( p
, " %4.1f%%", 100. * h
->stat
.i_mb_count_ref
[i_slice
][i_list
][i
] / i_den
);
4484 x264_log( h
, X264_LOG_INFO
, "ref %c L%d:%s\n", "PB"[i_slice
], i_list
, buf
);
4487 if( h
->param
.analyse
.b_ssim
)
4489 float ssim
= SUM3( h
->stat
.f_ssim_mean_y
) / duration
;
4490 x264_log( h
, X264_LOG_INFO
, "SSIM Mean Y:%.7f (%6.3fdb)\n", ssim
, x264_ssim( ssim
) );
4492 if( h
->param
.analyse
.b_psnr
)
4494 x264_log( h
, X264_LOG_INFO
,
4495 "PSNR Mean Y:%6.3f U:%6.3f V:%6.3f Avg:%6.3f Global:%6.3f kb/s:%.2f\n",
4496 SUM3( h
->stat
.f_psnr_mean_y
) / duration
,
4497 SUM3( h
->stat
.f_psnr_mean_u
) / duration
,
4498 SUM3( h
->stat
.f_psnr_mean_v
) / duration
,
4499 SUM3( h
->stat
.f_psnr_average
) / duration
,
4500 x264_psnr( SUM3( h
->stat
.f_ssd_global
), duration
* i_yuv_size
),
4504 x264_log( h
, X264_LOG_INFO
, "kb/s:%.2f\n", f_bitrate
);
4508 x264_ratecontrol_delete( h
);
4511 if( h
->param
.rc
.psz_stat_out
)
4512 free( h
->param
.rc
.psz_stat_out
);
4513 if( h
->param
.rc
.psz_stat_in
)
4514 free( h
->param
.rc
.psz_stat_in
);
4516 x264_cqm_delete( h
);
4517 x264_free( h
->nal_buffer
);
4518 x264_free( h
->reconfig_h
);
4519 x264_analyse_free_costs( h
);
4521 if( h
->i_thread_frames
> 1 )
4522 h
= h
->thread
[h
->i_thread_phase
];
4525 x264_frame_delete_list( h
->frames
.unused
[0] );
4526 x264_frame_delete_list( h
->frames
.unused
[1] );
4527 x264_frame_delete_list( h
->frames
.current
);
4528 x264_frame_delete_list( h
->frames
.blank_unused
);
4532 for( int i
= 0; i
< h
->i_thread_frames
; i
++ )
4533 if( h
->thread
[i
]->b_thread_active
)
4534 for( int j
= 0; j
< h
->thread
[i
]->i_ref
[0]; j
++ )
4535 if( h
->thread
[i
]->fref
[0][j
] && h
->thread
[i
]->fref
[0][j
]->b_duplicate
)
4536 x264_frame_delete( h
->thread
[i
]->fref
[0][j
] );
4538 if( h
->param
.i_lookahead_threads
> 1 )
4539 for( int i
= 0; i
< h
->param
.i_lookahead_threads
; i
++ )
4540 x264_free( h
->lookahead_thread
[i
] );
4542 for( int i
= h
->param
.i_threads
- 1; i
>= 0; i
-- )
4544 x264_frame_t
**frame
;
4546 if( !h
->param
.b_sliced_threads
|| i
== 0 )
4548 for( frame
= h
->thread
[i
]->frames
.reference
; *frame
; frame
++ )
4550 assert( (*frame
)->i_reference_count
> 0 );
4551 (*frame
)->i_reference_count
--;
4552 if( (*frame
)->i_reference_count
== 0 )
4553 x264_frame_delete( *frame
);
4555 frame
= &h
->thread
[i
]->fdec
;
4558 assert( (*frame
)->i_reference_count
> 0 );
4559 (*frame
)->i_reference_count
--;
4560 if( (*frame
)->i_reference_count
== 0 )
4561 x264_frame_delete( *frame
);
4563 x264_macroblock_cache_free( h
->thread
[i
] );
4565 x264_macroblock_thread_free( h
->thread
[i
], 0 );
4566 x264_free( h
->thread
[i
]->out
.p_bitstream
);
4567 x264_free( h
->thread
[i
]->out
.nal
);
4568 x264_pthread_mutex_destroy( &h
->thread
[i
]->mutex
);
4569 x264_pthread_cond_destroy( &h
->thread
[i
]->cv
);
4570 x264_free( h
->thread
[i
] );
4573 x264_opencl_close_library( ocl
);
4577 int x264_encoder_delayed_frames( x264_t
*h
)
4579 int delayed_frames
= 0;
4580 if( h
->i_thread_frames
> 1 )
4582 for( int i
= 0; i
< h
->i_thread_frames
; i
++ )
4583 delayed_frames
+= h
->thread
[i
]->b_thread_active
;
4584 h
= h
->thread
[h
->i_thread_phase
];
4586 for( int i
= 0; h
->frames
.current
[i
]; i
++ )
4588 x264_pthread_mutex_lock( &h
->lookahead
->ofbuf
.mutex
);
4589 x264_pthread_mutex_lock( &h
->lookahead
->ifbuf
.mutex
);
4590 x264_pthread_mutex_lock( &h
->lookahead
->next
.mutex
);
4591 delayed_frames
+= h
->lookahead
->ifbuf
.i_size
+ h
->lookahead
->next
.i_size
+ h
->lookahead
->ofbuf
.i_size
;
4592 x264_pthread_mutex_unlock( &h
->lookahead
->next
.mutex
);
4593 x264_pthread_mutex_unlock( &h
->lookahead
->ifbuf
.mutex
);
4594 x264_pthread_mutex_unlock( &h
->lookahead
->ofbuf
.mutex
);
4595 return delayed_frames
;
4598 int x264_encoder_maximum_delayed_frames( x264_t
*h
)
4600 return h
->frames
.i_delay
;