1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003-2008 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 *****************************************************************************/
26 #include "common/common.h"
27 #include "common/cpu.h"
31 #include "ratecontrol.h"
32 #include "macroblock.h"
35 #include "common/visualize.h"
38 //#define DEBUG_MB_TYPE
40 #define NALU_OVERHEAD 5 // startcode + NAL type costs 5 bytes per frame
42 #define bs_write_ue bs_write_ue_big
44 static void x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
45 x264_nal_t **pp_nal, int *pi_nal,
46 x264_picture_t *pic_out );
48 /****************************************************************************
50 ******************************* x264 libs **********************************
52 ****************************************************************************/
53 static float x264_psnr( int64_t i_sqe, int64_t i_size )
55 double f_mse = (double)i_sqe / ((double)65025.0 * (double)i_size);
56 if( f_mse <= 0.0000000001 ) /* Max 100dB */
59 return (float)(-10.0 * log( f_mse ) / log( 10.0 ));
62 static void x264_frame_dump( x264_t *h )
64 FILE *f = fopen( h->param.psz_dump_yuv, "r+b" );
68 /* Write the frame in display order */
69 fseek( f, h->fdec->i_frame * h->param.i_height * h->param.i_width * 3/2, SEEK_SET );
70 for( i = 0; i < h->fdec->i_plane; i++ )
71 for( y = 0; y < h->param.i_height >> !!i; y++ )
72 fwrite( &h->fdec->plane[i][y*h->fdec->i_stride[i]], 1, h->param.i_width >> !!i, f );
77 /* Fill "default" values */
78 static void x264_slice_header_init( x264_t *h, x264_slice_header_t *sh,
79 x264_sps_t *sps, x264_pps_t *pps,
80 int i_idr_pic_id, int i_frame, int i_qp )
82 x264_param_t *param = &h->param;
85 /* First we fill all field */
90 sh->i_last_mb = h->sps->i_mb_width * h->sps->i_mb_height;
91 sh->i_pps_id = pps->i_id;
93 sh->i_frame_num = i_frame;
95 sh->b_mbaff = h->param.b_interlaced;
96 sh->b_field_pic = 0; /* no field support for now */
97 sh->b_bottom_field = 0; /* not yet used */
99 sh->i_idr_pic_id = i_idr_pic_id;
101 /* poc stuff, fixed later */
103 sh->i_delta_poc_bottom = 0;
104 sh->i_delta_poc[0] = 0;
105 sh->i_delta_poc[1] = 0;
107 sh->i_redundant_pic_cnt = 0;
109 if( !h->mb.b_direct_auto_read )
111 if( h->mb.b_direct_auto_write )
112 sh->b_direct_spatial_mv_pred = ( h->stat.i_direct_score[1] > h->stat.i_direct_score[0] );
114 sh->b_direct_spatial_mv_pred = ( param->analyse.i_direct_mv_pred == X264_DIRECT_PRED_SPATIAL );
116 /* else b_direct_spatial_mv_pred was read from the 2pass statsfile */
118 sh->b_num_ref_idx_override = 0;
119 sh->i_num_ref_idx_l0_active = 1;
120 sh->i_num_ref_idx_l1_active = 1;
122 sh->b_ref_pic_list_reordering_l0 = h->b_ref_reorder[0];
123 sh->b_ref_pic_list_reordering_l1 = h->b_ref_reorder[1];
125 /* If the ref list isn't in the default order, construct reordering header */
126 /* List1 reordering isn't needed yet */
127 if( sh->b_ref_pic_list_reordering_l0 )
129 int pred_frame_num = i_frame;
130 for( i = 0; i < h->i_ref0; i++ )
132 int diff = h->fref0[i]->i_frame_num - pred_frame_num;
134 x264_log( h, X264_LOG_ERROR, "diff frame num == 0\n" );
135 sh->ref_pic_list_order[0][i].idc = ( diff > 0 );
136 sh->ref_pic_list_order[0][i].arg = abs( diff ) - 1;
137 pred_frame_num = h->fref0[i]->i_frame_num;
141 sh->i_cabac_init_idc = param->i_cabac_init_idc;
144 sh->i_qp_delta = i_qp - pps->i_pic_init_qp;
145 sh->b_sp_for_swidth = 0;
148 /* If effective qp <= 15, deblocking would have no effect anyway */
149 if( param->b_deblocking_filter
150 && ( h->mb.b_variable_qp
151 || 15 < i_qp + 2 * X264_MIN(param->i_deblocking_filter_alphac0, param->i_deblocking_filter_beta) ) )
153 sh->i_disable_deblocking_filter_idc = 0;
157 sh->i_disable_deblocking_filter_idc = 1;
159 sh->i_alpha_c0_offset = param->i_deblocking_filter_alphac0 << 1;
160 sh->i_beta_offset = param->i_deblocking_filter_beta << 1;
163 static void x264_slice_header_write( bs_t *s, x264_slice_header_t *sh, int i_nal_ref_idc )
169 assert( sh->i_first_mb % (2*sh->sps->i_mb_width) == 0 );
170 bs_write_ue( s, sh->i_first_mb >> 1 );
173 bs_write_ue( s, sh->i_first_mb );
175 bs_write_ue( s, sh->i_type + 5 ); /* same type things */
176 bs_write_ue( s, sh->i_pps_id );
177 bs_write( s, sh->sps->i_log2_max_frame_num, sh->i_frame_num );
179 if( !sh->sps->b_frame_mbs_only )
181 bs_write1( s, sh->b_field_pic );
182 if ( sh->b_field_pic )
183 bs_write1( s, sh->b_bottom_field );
186 if( sh->i_idr_pic_id >= 0 ) /* NAL IDR */
188 bs_write_ue( s, sh->i_idr_pic_id );
191 if( sh->sps->i_poc_type == 0 )
193 bs_write( s, sh->sps->i_log2_max_poc_lsb, sh->i_poc_lsb );
194 if( sh->pps->b_pic_order && !sh->b_field_pic )
196 bs_write_se( s, sh->i_delta_poc_bottom );
199 else if( sh->sps->i_poc_type == 1 && !sh->sps->b_delta_pic_order_always_zero )
201 bs_write_se( s, sh->i_delta_poc[0] );
202 if( sh->pps->b_pic_order && !sh->b_field_pic )
204 bs_write_se( s, sh->i_delta_poc[1] );
208 if( sh->pps->b_redundant_pic_cnt )
210 bs_write_ue( s, sh->i_redundant_pic_cnt );
213 if( sh->i_type == SLICE_TYPE_B )
215 bs_write1( s, sh->b_direct_spatial_mv_pred );
217 if( sh->i_type == SLICE_TYPE_P || sh->i_type == SLICE_TYPE_SP || sh->i_type == SLICE_TYPE_B )
219 bs_write1( s, sh->b_num_ref_idx_override );
220 if( sh->b_num_ref_idx_override )
222 bs_write_ue( s, sh->i_num_ref_idx_l0_active - 1 );
223 if( sh->i_type == SLICE_TYPE_B )
225 bs_write_ue( s, sh->i_num_ref_idx_l1_active - 1 );
230 /* ref pic list reordering */
231 if( sh->i_type != SLICE_TYPE_I )
233 bs_write1( s, sh->b_ref_pic_list_reordering_l0 );
234 if( sh->b_ref_pic_list_reordering_l0 )
236 for( i = 0; i < sh->i_num_ref_idx_l0_active; i++ )
238 bs_write_ue( s, sh->ref_pic_list_order[0][i].idc );
239 bs_write_ue( s, sh->ref_pic_list_order[0][i].arg );
245 if( sh->i_type == SLICE_TYPE_B )
247 bs_write1( s, sh->b_ref_pic_list_reordering_l1 );
248 if( sh->b_ref_pic_list_reordering_l1 )
250 for( i = 0; i < sh->i_num_ref_idx_l1_active; i++ )
252 bs_write_ue( s, sh->ref_pic_list_order[1][i].idc );
253 bs_write_ue( s, sh->ref_pic_list_order[1][i].arg );
259 if( ( sh->pps->b_weighted_pred && ( sh->i_type == SLICE_TYPE_P || sh->i_type == SLICE_TYPE_SP ) ) ||
260 ( sh->pps->b_weighted_bipred == 1 && sh->i_type == SLICE_TYPE_B ) )
265 if( i_nal_ref_idc != 0 )
267 if( sh->i_idr_pic_id >= 0 )
269 bs_write1( s, 0 ); /* no output of prior pics flag */
270 bs_write1( s, 0 ); /* long term reference flag */
274 bs_write1( s, 0 ); /* adaptive_ref_pic_marking_mode_flag */
278 if( sh->pps->b_cabac && sh->i_type != SLICE_TYPE_I )
280 bs_write_ue( s, sh->i_cabac_init_idc );
282 bs_write_se( s, sh->i_qp_delta ); /* slice qp delta */
284 if( sh->pps->b_deblocking_filter_control )
286 bs_write_ue( s, sh->i_disable_deblocking_filter_idc );
287 if( sh->i_disable_deblocking_filter_idc != 1 )
289 bs_write_se( s, sh->i_alpha_c0_offset >> 1 );
290 bs_write_se( s, sh->i_beta_offset >> 1 );
295 /* If we are within a reasonable distance of the end of the memory allocated for the bitstream, */
296 /* reallocate, adding an arbitrary amount of space (100 kilobytes). */
297 static void x264_bitstream_check_buffer( x264_t *h )
299 if( ( h->param.b_cabac && (h->cabac.p_end - h->cabac.p < 2500) )
300 || ( h->out.bs.p_end - h->out.bs.p < 2500 ) )
302 uint8_t *bs_bak = h->out.p_bitstream;
306 h->out.i_bitstream += 100000;
307 h->out.p_bitstream = x264_realloc( h->out.p_bitstream, h->out.i_bitstream );
308 delta = h->out.p_bitstream - bs_bak;
310 h->out.bs.p_start += delta;
311 h->out.bs.p += delta;
312 h->out.bs.p_end = h->out.p_bitstream + h->out.i_bitstream;
314 h->cabac.p_start += delta;
316 h->cabac.p_end = h->out.p_bitstream + h->out.i_bitstream;
318 for( i = 0; i <= h->out.i_nal; i++ )
319 h->out.nal[i].p_payload += delta;
323 /****************************************************************************
325 ****************************************************************************
326 ****************************** External API*********************************
327 ****************************************************************************
329 ****************************************************************************/
331 static int x264_validate_parameters( x264_t *h )
334 if( !(x264_cpu_detect() & X264_CPU_MMXEXT) )
336 x264_log( h, X264_LOG_ERROR, "your cpu does not support MMXEXT, but x264 was compiled with asm support\n");
337 x264_log( h, X264_LOG_ERROR, "to run x264, recompile without asm support (configure --disable-asm)\n");
341 if( h->param.i_width <= 0 || h->param.i_height <= 0 )
343 x264_log( h, X264_LOG_ERROR, "invalid width x height (%dx%d)\n",
344 h->param.i_width, h->param.i_height );
348 if( h->param.i_width % 2 || h->param.i_height % 2 )
350 x264_log( h, X264_LOG_ERROR, "width or height not divisible by 2 (%dx%d)\n",
351 h->param.i_width, h->param.i_height );
354 if( h->param.i_csp != X264_CSP_I420 )
356 x264_log( h, X264_LOG_ERROR, "invalid CSP (only I420 supported)\n" );
360 if( h->param.i_threads == 0 )
361 h->param.i_threads = x264_cpu_num_processors() * 3/2;
362 h->param.i_threads = x264_clip3( h->param.i_threads, 1, X264_THREAD_MAX );
363 if( h->param.i_threads > 1 )
366 x264_log( h, X264_LOG_WARNING, "not compiled with pthread support!\n");
367 h->param.i_threads = 1;
369 if( h->param.i_scenecut_threshold >= 0 )
370 h->param.b_pre_scenecut = 1;
374 if( h->param.b_interlaced )
376 if( h->param.analyse.i_me_method >= X264_ME_ESA )
378 x264_log( h, X264_LOG_WARNING, "interlace + me=esa is not implemented\n" );
379 h->param.analyse.i_me_method = X264_ME_UMH;
381 if( h->param.analyse.i_direct_mv_pred > X264_DIRECT_PRED_SPATIAL )
383 x264_log( h, X264_LOG_WARNING, "interlace + direct=temporal is not implemented\n" );
384 h->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
388 if( h->param.rc.i_rc_method < 0 || h->param.rc.i_rc_method > 2 )
390 x264_log( h, X264_LOG_ERROR, "no ratecontrol method specified\n" );
393 h->param.rc.f_rf_constant = x264_clip3f( h->param.rc.f_rf_constant, 0, 51 );
394 h->param.rc.i_qp_constant = x264_clip3( h->param.rc.i_qp_constant, 0, 51 );
395 if( h->param.rc.i_rc_method == X264_RC_CRF )
396 h->param.rc.i_qp_constant = h->param.rc.f_rf_constant;
397 if( (h->param.rc.i_rc_method == X264_RC_CQP || h->param.rc.i_rc_method == X264_RC_CRF)
398 && h->param.rc.i_qp_constant == 0 )
400 h->mb.b_lossless = 1;
401 h->param.i_cqm_preset = X264_CQM_FLAT;
402 h->param.psz_cqm_file = NULL;
403 h->param.rc.i_rc_method = X264_RC_CQP;
404 h->param.rc.f_ip_factor = 1;
405 h->param.rc.f_pb_factor = 1;
406 h->param.analyse.b_transform_8x8 = 0;
407 h->param.analyse.b_psnr = 0;
408 h->param.analyse.b_ssim = 0;
409 h->param.analyse.i_chroma_qp_offset = 0;
410 h->param.analyse.i_trellis = 0;
411 h->param.analyse.b_fast_pskip = 0;
412 h->param.analyse.i_noise_reduction = 0;
414 if( h->param.rc.i_rc_method == X264_RC_CQP )
416 float qp_p = h->param.rc.i_qp_constant;
417 float qp_i = qp_p - 6*log(h->param.rc.f_ip_factor)/log(2);
418 float qp_b = qp_p + 6*log(h->param.rc.f_pb_factor)/log(2);
419 h->param.rc.i_qp_min = x264_clip3( (int)(X264_MIN3( qp_p, qp_i, qp_b )), 0, 51 );
420 h->param.rc.i_qp_max = x264_clip3( (int)(X264_MAX3( qp_p, qp_i, qp_b ) + .999), 0, 51 );
421 h->param.rc.i_aq_mode = 0;
423 h->param.rc.i_qp_max = x264_clip3( h->param.rc.i_qp_max, 0, 51 );
424 h->param.rc.i_qp_min = x264_clip3( h->param.rc.i_qp_min, 0, h->param.rc.i_qp_max );
426 if( ( h->param.i_width % 16 || h->param.i_height % 16 )
427 && h->param.i_height != 1080 && !h->mb.b_lossless )
429 // There's nothing special about 1080 in that the warning still applies to it,
430 // but chances are the user can't help it if his content is already 1080p,
431 // so there's no point in warning in that case.
432 x264_log( h, X264_LOG_WARNING,
433 "width or height not divisible by 16 (%dx%d), compression will suffer.\n",
434 h->param.i_width, h->param.i_height );
437 h->param.i_frame_reference = x264_clip3( h->param.i_frame_reference, 1, 16 );
438 if( h->param.i_keyint_max <= 0 )
439 h->param.i_keyint_max = 1;
440 h->param.i_keyint_min = x264_clip3( h->param.i_keyint_min, 1, h->param.i_keyint_max/2+1 );
442 h->param.i_bframe = x264_clip3( h->param.i_bframe, 0, X264_BFRAME_MAX );
443 h->param.i_bframe_bias = x264_clip3( h->param.i_bframe_bias, -90, 100 );
444 h->param.b_bframe_pyramid = h->param.b_bframe_pyramid && h->param.i_bframe > 1;
445 h->param.b_bframe_adaptive = h->param.b_bframe_adaptive && h->param.i_bframe > 0;
446 h->param.analyse.b_weighted_bipred = h->param.analyse.b_weighted_bipred && h->param.i_bframe > 0;
447 h->mb.b_direct_auto_write = h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_AUTO
449 && ( h->param.rc.b_stat_write || !h->param.rc.b_stat_read );
450 if( h->param.i_scenecut_threshold < 0 )
451 h->param.b_pre_scenecut = 0;
453 h->param.i_deblocking_filter_alphac0 = x264_clip3( h->param.i_deblocking_filter_alphac0, -6, 6 );
454 h->param.i_deblocking_filter_beta = x264_clip3( h->param.i_deblocking_filter_beta, -6, 6 );
455 h->param.analyse.i_luma_deadzone[0] = x264_clip3( h->param.analyse.i_luma_deadzone[0], 0, 32 );
456 h->param.analyse.i_luma_deadzone[1] = x264_clip3( h->param.analyse.i_luma_deadzone[1], 0, 32 );
458 h->param.i_cabac_init_idc = x264_clip3( h->param.i_cabac_init_idc, 0, 2 );
460 if( h->param.i_cqm_preset < X264_CQM_FLAT || h->param.i_cqm_preset > X264_CQM_CUSTOM )
461 h->param.i_cqm_preset = X264_CQM_FLAT;
463 if( h->param.analyse.i_me_method < X264_ME_DIA ||
464 h->param.analyse.i_me_method > X264_ME_TESA )
465 h->param.analyse.i_me_method = X264_ME_HEX;
466 if( h->param.analyse.i_me_range < 4 )
467 h->param.analyse.i_me_range = 4;
468 if( h->param.analyse.i_me_range > 16 && h->param.analyse.i_me_method <= X264_ME_HEX )
469 h->param.analyse.i_me_range = 16;
470 if( h->param.analyse.i_me_method == X264_ME_TESA &&
471 (h->mb.b_lossless || h->param.analyse.i_subpel_refine <= 1) )
472 h->param.analyse.i_me_method = X264_ME_ESA;
473 h->param.analyse.i_subpel_refine = x264_clip3( h->param.analyse.i_subpel_refine, 1, 7 );
474 h->param.analyse.b_bframe_rdo = h->param.analyse.b_bframe_rdo && h->param.analyse.i_subpel_refine >= 6;
475 h->param.analyse.b_mixed_references = h->param.analyse.b_mixed_references && h->param.i_frame_reference > 1;
476 h->param.analyse.inter &= X264_ANALYSE_PSUB16x16|X264_ANALYSE_PSUB8x8|X264_ANALYSE_BSUB16x16|
477 X264_ANALYSE_I4x4|X264_ANALYSE_I8x8;
478 h->param.analyse.intra &= X264_ANALYSE_I4x4|X264_ANALYSE_I8x8;
479 if( !(h->param.analyse.inter & X264_ANALYSE_PSUB16x16) )
480 h->param.analyse.inter &= ~X264_ANALYSE_PSUB8x8;
481 if( !h->param.analyse.b_transform_8x8 )
483 h->param.analyse.inter &= ~X264_ANALYSE_I8x8;
484 h->param.analyse.intra &= ~X264_ANALYSE_I8x8;
486 h->param.analyse.i_chroma_qp_offset = x264_clip3(h->param.analyse.i_chroma_qp_offset, -12, 12);
487 if( !h->param.b_cabac )
488 h->param.analyse.i_trellis = 0;
489 h->param.analyse.i_trellis = x264_clip3( h->param.analyse.i_trellis, 0, 2 );
490 h->param.rc.i_aq_mode = x264_clip3( h->param.rc.i_aq_mode, 0, 1 );
491 if( h->param.rc.f_aq_strength <= 0 )
492 h->param.rc.i_aq_mode = 0;
493 h->param.analyse.i_noise_reduction = x264_clip3( h->param.analyse.i_noise_reduction, 0, 1<<16 );
496 const x264_level_t *l = x264_levels;
497 if( h->param.i_level_idc < 0 )
499 if( h->param.rc.i_rc_method == X264_RC_ABR && h->param.rc.i_vbv_buffer_size <= 0 )
500 h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate * 2;
501 h->sps = h->sps_array;
502 x264_sps_init( h->sps, h->param.i_sps_id, &h->param );
503 do h->param.i_level_idc = l->level_idc;
504 while( l[1].level_idc && x264_validate_levels( h, 0 ) && l++ );
505 if( h->param.rc.i_vbv_buffer_size <= 0 )
506 h->param.rc.i_vbv_max_bitrate = 0;
507 x264_log( h, X264_LOG_DEBUG, "level_idc: %d\n", h->param.i_level_idc );
511 while( l->level_idc && l->level_idc != h->param.i_level_idc )
513 if( l->level_idc == 0 )
515 x264_log( h, X264_LOG_ERROR, "invalid level_idc: %d\n", h->param.i_level_idc );
519 if( h->param.analyse.i_mv_range <= 0 )
520 h->param.analyse.i_mv_range = l->mv_range >> h->param.b_interlaced;
522 h->param.analyse.i_mv_range = x264_clip3(h->param.analyse.i_mv_range, 32, 512 >> h->param.b_interlaced);
523 if( h->param.analyse.i_direct_8x8_inference < 0 )
524 h->param.analyse.i_direct_8x8_inference = l->direct8x8;
527 if( h->param.i_threads > 1 )
529 int r = h->param.analyse.i_mv_range_thread;
533 // half of the available space is reserved and divided evenly among the threads,
534 // the rest is allocated to whichever thread is far enough ahead to use it.
535 // reserving more space increases quality for some videos, but costs more time
536 // in thread synchronization.
537 int max_range = (h->param.i_height + X264_THREAD_HEIGHT) / h->param.i_threads - X264_THREAD_HEIGHT;
540 r = X264_MAX( r, h->param.analyse.i_me_range );
541 r = X264_MIN( r, h->param.analyse.i_mv_range );
542 // round up to use the whole mb row
543 r2 = (r & ~15) + ((-X264_THREAD_HEIGHT) & 15);
546 x264_log( h, X264_LOG_DEBUG, "using mv_range_thread = %d\n", r2 );
547 h->param.analyse.i_mv_range_thread = r2;
550 if( h->param.rc.f_qblur < 0 )
551 h->param.rc.f_qblur = 0;
552 if( h->param.rc.f_complexity_blur < 0 )
553 h->param.rc.f_complexity_blur = 0;
555 h->param.i_sps_id &= 31;
557 if( h->param.i_log_level < X264_LOG_INFO )
559 h->param.analyse.b_psnr = 0;
560 h->param.analyse.b_ssim = 0;
563 /* ensure the booleans are 0 or 1 so they can be used in math */
564 #define BOOLIFY(x) h->param.x = !!h->param.x
566 BOOLIFY( b_deblocking_filter );
567 BOOLIFY( b_interlaced );
568 BOOLIFY( analyse.b_transform_8x8 );
569 BOOLIFY( analyse.i_direct_8x8_inference );
570 BOOLIFY( analyse.b_bidir_me );
571 BOOLIFY( analyse.b_chroma_me );
572 BOOLIFY( analyse.b_fast_pskip );
573 BOOLIFY( rc.b_stat_write );
574 BOOLIFY( rc.b_stat_read );
580 static void mbcmp_init( x264_t *h )
582 int satd = !h->mb.b_lossless && h->param.analyse.i_subpel_refine > 1;
583 memcpy( h->pixf.mbcmp, satd ? h->pixf.satd : h->pixf.sad_aligned, sizeof(h->pixf.mbcmp) );
584 memcpy( h->pixf.mbcmp_unaligned, satd ? h->pixf.satd : h->pixf.sad, sizeof(h->pixf.mbcmp_unaligned) );
585 h->pixf.intra_mbcmp_x3_16x16 = satd ? h->pixf.intra_satd_x3_16x16 : h->pixf.intra_sad_x3_16x16;
586 satd &= h->param.analyse.i_me_method == X264_ME_TESA;
587 memcpy( h->pixf.fpelcmp, satd ? h->pixf.satd : h->pixf.sad, sizeof(h->pixf.fpelcmp) );
588 memcpy( h->pixf.fpelcmp_x3, satd ? h->pixf.satd_x3 : h->pixf.sad_x3, sizeof(h->pixf.fpelcmp_x3) );
589 memcpy( h->pixf.fpelcmp_x4, satd ? h->pixf.satd_x4 : h->pixf.sad_x4, sizeof(h->pixf.fpelcmp_x4) );
592 /****************************************************************************
594 ****************************************************************************/
595 x264_t *x264_encoder_open ( x264_param_t *param )
597 x264_t *h = x264_malloc( sizeof( x264_t ) );
601 memset( h, 0, sizeof( x264_t ) );
603 /* Create a copy of param */
604 memcpy( &h->param, param, sizeof( x264_param_t ) );
606 if( x264_validate_parameters( h ) < 0 )
612 if( h->param.psz_cqm_file )
613 if( x264_cqm_parse_file( h, h->param.psz_cqm_file ) < 0 )
619 if( h->param.rc.psz_stat_out )
620 h->param.rc.psz_stat_out = strdup( h->param.rc.psz_stat_out );
621 if( h->param.rc.psz_stat_in )
622 h->param.rc.psz_stat_in = strdup( h->param.rc.psz_stat_in );
625 if( h->param.vui.i_sar_width > 0 && h->param.vui.i_sar_height > 0 )
627 int i_w = param->vui.i_sar_width;
628 int i_h = param->vui.i_sar_height;
630 x264_reduce_fraction( &i_w, &i_h );
632 while( i_w > 65535 || i_h > 65535 )
638 h->param.vui.i_sar_width = 0;
639 h->param.vui.i_sar_height = 0;
640 if( i_w == 0 || i_h == 0 )
642 x264_log( h, X264_LOG_WARNING, "cannot create valid sample aspect ratio\n" );
646 x264_log( h, X264_LOG_INFO, "using SAR=%d/%d\n", i_w, i_h );
647 h->param.vui.i_sar_width = i_w;
648 h->param.vui.i_sar_height = i_h;
652 x264_reduce_fraction( &h->param.i_fps_num, &h->param.i_fps_den );
659 h->sps = &h->sps_array[0];
660 x264_sps_init( h->sps, h->param.i_sps_id, &h->param );
662 h->pps = &h->pps_array[0];
663 x264_pps_init( h->pps, h->param.i_sps_id, &h->param, h->sps);
665 x264_validate_levels( h, 1 );
667 if( x264_cqm_init( h ) < 0 )
673 h->mb.i_mb_count = h->sps->i_mb_width * h->sps->i_mb_height;
676 h->frames.i_delay = h->param.i_bframe + h->param.i_threads - 1;
677 h->frames.i_max_ref0 = h->param.i_frame_reference;
678 h->frames.i_max_ref1 = h->sps->vui.i_num_reorder_frames;
679 h->frames.i_max_dpb = h->sps->vui.i_max_dec_frame_buffering;
680 h->frames.b_have_lowres = !h->param.rc.b_stat_read
681 && ( h->param.rc.i_rc_method == X264_RC_ABR
682 || h->param.rc.i_rc_method == X264_RC_CRF
683 || h->param.b_bframe_adaptive
684 || h->param.b_pre_scenecut );
685 h->frames.b_have_lowres |= (h->param.rc.b_stat_read && h->param.rc.i_vbv_buffer_size > 0);
687 h->frames.i_last_idr = - h->param.i_keyint_max;
688 h->frames.i_input = 0;
689 h->frames.last_nonb = NULL;
694 h->chroma_qp_table = i_chroma_qp_table + 12 + h->pps->i_chroma_qp_index_offset;
698 /* init CPU functions */
699 x264_predict_16x16_init( h->param.cpu, h->predict_16x16 );
700 x264_predict_8x8c_init( h->param.cpu, h->predict_8x8c );
701 x264_predict_8x8_init( h->param.cpu, h->predict_8x8 );
702 x264_predict_4x4_init( h->param.cpu, h->predict_4x4 );
704 x264_pixel_init( h->param.cpu, &h->pixf );
705 x264_dct_init( h->param.cpu, &h->dctf );
706 x264_zigzag_init( h->param.cpu, &h->zigzagf, h->param.b_interlaced );
707 x264_mc_init( h->param.cpu, &h->mc );
708 x264_quant_init( h, h->param.cpu, &h->quantf );
709 x264_deblock_init( h->param.cpu, &h->loopf );
710 x264_dct_init_weights();
714 p = buf + sprintf( buf, "using cpu capabilities:" );
715 for( i=0; x264_cpu_names[i].flags; i++ )
717 if( !strcmp(x264_cpu_names[i].name, "SSE2")
718 && param->cpu & (X264_CPU_SSE2_IS_FAST|X264_CPU_SSE2_IS_SLOW) )
720 if( !strcmp(x264_cpu_names[i].name, "SSE3")
721 && (param->cpu & X264_CPU_SSSE3 || !(param->cpu & X264_CPU_CACHELINE_64)) )
723 if( (param->cpu & x264_cpu_names[i].flags) == x264_cpu_names[i].flags
724 && (!i || x264_cpu_names[i].flags != x264_cpu_names[i-1].flags) )
725 p += sprintf( p, " %s", x264_cpu_names[i].name );
728 p += sprintf( p, " none!" );
729 x264_log( h, X264_LOG_INFO, "%s\n", buf );
732 h->out.i_bitstream = X264_MAX( 1000000, h->param.i_width * h->param.i_height * 4
733 * ( h->param.rc.i_rc_method == X264_RC_ABR ? pow( 0.95, h->param.rc.i_qp_min )
734 : pow( 0.95, h->param.rc.i_qp_constant ) * X264_MAX( 1, h->param.rc.f_ip_factor )));
738 for( i = 1; i < h->param.i_threads; i++ )
739 h->thread[i] = x264_malloc( sizeof(x264_t) );
741 for( i = 0; i < h->param.i_threads; i++ )
745 h->thread[i]->fdec = x264_frame_pop_unused( h );
746 h->thread[i]->out.p_bitstream = x264_malloc( h->out.i_bitstream );
747 if( x264_macroblock_cache_init( h->thread[i] ) < 0 )
751 if( x264_ratecontrol_new( h ) < 0 )
754 if( h->param.psz_dump_yuv )
756 /* create or truncate the reconstructed video file */
757 FILE *f = fopen( h->param.psz_dump_yuv, "w" );
762 x264_log( h, X264_LOG_ERROR, "can't write to fdec.yuv\n" );
771 /****************************************************************************
772 * x264_encoder_reconfig:
773 ****************************************************************************/
774 int x264_encoder_reconfig( x264_t *h, x264_param_t *param )
776 #define COPY(var) h->param.var = param->var
777 COPY( i_frame_reference ); // but never uses more refs than initially specified
778 COPY( i_bframe_bias );
779 if( h->param.i_scenecut_threshold >= 0 && param->i_scenecut_threshold >= 0 )
780 COPY( i_scenecut_threshold ); // can't turn it on or off, only vary the threshold
781 COPY( b_deblocking_filter );
782 COPY( i_deblocking_filter_alphac0 );
783 COPY( i_deblocking_filter_beta );
784 COPY( analyse.intra );
785 COPY( analyse.inter );
786 COPY( analyse.i_direct_mv_pred );
787 COPY( analyse.i_me_method );
788 COPY( analyse.i_me_range );
789 COPY( analyse.i_noise_reduction );
790 COPY( analyse.i_subpel_refine );
791 COPY( analyse.i_trellis );
792 COPY( analyse.b_bidir_me );
793 COPY( analyse.b_bframe_rdo );
794 COPY( analyse.b_chroma_me );
795 COPY( analyse.b_dct_decimate );
796 COPY( analyse.b_fast_pskip );
797 COPY( analyse.b_mixed_references );
798 // can only twiddle these if they were enabled to begin with:
799 if( h->pps->b_transform_8x8_mode )
800 COPY( analyse.b_transform_8x8 );
801 if( h->frames.i_max_ref1 > 1 )
802 COPY( b_bframe_pyramid );
807 return x264_validate_parameters( h );
811 static void x264_nal_start( x264_t *h, int i_type, int i_ref_idc )
813 x264_nal_t *nal = &h->out.nal[h->out.i_nal];
815 nal->i_ref_idc = i_ref_idc;
816 nal->i_type = i_type;
819 nal->p_payload= &h->out.p_bitstream[bs_pos( &h->out.bs ) / 8];
821 static void x264_nal_end( x264_t *h )
823 x264_nal_t *nal = &h->out.nal[h->out.i_nal];
824 nal->i_payload = &h->out.p_bitstream[bs_pos( &h->out.bs ) / 8] - nal->p_payload;
828 /****************************************************************************
829 * x264_encoder_headers:
830 ****************************************************************************/
831 int x264_encoder_headers( x264_t *h, x264_nal_t **pp_nal, int *pi_nal )
833 /* init bitstream context */
835 bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream );
837 /* Put SPS and PPS */
838 if( h->i_frame == 0 )
840 /* identify ourself */
841 x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE );
842 x264_sei_version_write( h, &h->out.bs );
845 /* generate sequence parameters */
846 x264_nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST );
847 x264_sps_write( &h->out.bs, h->sps );
850 /* generate picture parameters */
851 x264_nal_start( h, NAL_PPS, NAL_PRIORITY_HIGHEST );
852 x264_pps_write( &h->out.bs, h->pps );
856 *pi_nal = h->out.i_nal;
857 *pp_nal = &h->out.nal[0];
863 static inline void x264_reference_build_list( x264_t *h, int i_poc )
868 /* build ref list 0/1 */
871 for( i = 0; h->frames.reference[i]; i++ )
873 if( h->frames.reference[i]->i_poc < i_poc )
875 h->fref0[h->i_ref0++] = h->frames.reference[i];
877 else if( h->frames.reference[i]->i_poc > i_poc )
879 h->fref1[h->i_ref1++] = h->frames.reference[i];
883 /* Order ref0 from higher to lower poc */
887 for( i = 0; i < h->i_ref0 - 1; i++ )
889 if( h->fref0[i]->i_poc < h->fref0[i+1]->i_poc )
891 XCHG( x264_frame_t*, h->fref0[i], h->fref0[i+1] );
897 /* Order ref1 from lower to higher poc (bubble sort) for B-frame */
901 for( i = 0; i < h->i_ref1 - 1; i++ )
903 if( h->fref1[i]->i_poc > h->fref1[i+1]->i_poc )
905 XCHG( x264_frame_t*, h->fref1[i], h->fref1[i+1] );
912 /* In the standard, a P-frame's ref list is sorted by frame_num.
913 * We use POC, but check whether explicit reordering is needed */
914 h->b_ref_reorder[0] =
915 h->b_ref_reorder[1] = 0;
916 if( h->sh.i_type == SLICE_TYPE_P )
918 for( i = 0; i < h->i_ref0 - 1; i++ )
919 if( h->fref0[i]->i_frame_num < h->fref0[i+1]->i_frame_num )
921 h->b_ref_reorder[0] = 1;
926 h->i_ref1 = X264_MIN( h->i_ref1, h->frames.i_max_ref1 );
927 h->i_ref0 = X264_MIN( h->i_ref0, h->frames.i_max_ref0 );
928 h->i_ref0 = X264_MIN( h->i_ref0, h->param.i_frame_reference ); // if reconfig() has lowered the limit
929 assert( h->i_ref0 + h->i_ref1 <= 16 );
930 h->mb.pic.i_fref[0] = h->i_ref0;
931 h->mb.pic.i_fref[1] = h->i_ref1;
934 static void x264_fdec_filter_row( x264_t *h, int mb_y )
936 /* mb_y is the mb to be encoded next, not the mb to be filtered here */
937 int b_hpel = h->fdec->b_kept_as_ref;
938 int b_deblock = !h->sh.i_disable_deblocking_filter_idc;
939 int b_end = mb_y == h->sps->i_mb_height;
940 int min_y = mb_y - (1 << h->sh.b_mbaff);
941 int max_y = b_end ? h->sps->i_mb_height : mb_y;
942 b_deblock &= b_hpel || h->param.psz_dump_yuv;
943 if( mb_y & h->sh.b_mbaff )
951 for( j=0; j<=h->sh.b_mbaff; j++ )
954 memcpy( h->mb.intra_border_backup[j][i],
955 h->fdec->plane[i] + ((mb_y*16 >> !!i) + j - 1 - h->sh.b_mbaff) * h->fdec->i_stride[i],
956 h->sps->i_mb_width*16 >> !!i );
963 for( y = min_y; y < max_y; y += (1 << h->sh.b_mbaff) )
964 x264_frame_deblock_row( h, y );
969 x264_frame_expand_border( h, h->fdec, min_y, b_end );
970 x264_frame_filter( h, h->fdec, min_y, b_end );
971 x264_frame_expand_border_filtered( h, h->fdec, min_y, b_end );
974 if( h->param.i_threads > 1 && h->fdec->b_kept_as_ref )
976 x264_frame_cond_broadcast( h->fdec, mb_y*16 + (b_end ? 10000 : -(X264_THREAD_HEIGHT << h->sh.b_mbaff)) );
979 min_y = X264_MAX( min_y*16-8, 0 );
980 max_y = b_end ? h->param.i_height : mb_y*16-8;
982 if( h->param.analyse.b_psnr )
986 h->stat.frame.i_ssd[i] +=
987 x264_pixel_ssd_wxh( &h->pixf,
988 h->fdec->plane[i] + (min_y>>!!i) * h->fdec->i_stride[i], h->fdec->i_stride[i],
989 h->fenc->plane[i] + (min_y>>!!i) * h->fenc->i_stride[i], h->fenc->i_stride[i],
990 h->param.i_width >> !!i, (max_y-min_y) >> !!i );
993 if( h->param.analyse.b_ssim )
996 /* offset by 2 pixels to avoid alignment of ssim blocks with dct blocks,
997 * and overlap by 4 */
998 min_y += min_y == 0 ? 2 : -6;
999 h->stat.frame.f_ssim +=
1000 x264_pixel_ssim_wxh( &h->pixf,
1001 h->fdec->plane[0] + 2+min_y*h->fdec->i_stride[0], h->fdec->i_stride[0],
1002 h->fenc->plane[0] + 2+min_y*h->fenc->i_stride[0], h->fenc->i_stride[0],
1003 h->param.i_width-2, max_y-min_y );
1007 static inline void x264_reference_update( x264_t *h )
1011 if( h->fdec->i_frame >= 0 )
1014 if( !h->fdec->b_kept_as_ref )
1016 if( h->param.i_threads > 1 )
1018 x264_frame_push_unused( h, h->fdec );
1019 h->fdec = x264_frame_pop_unused( h );
1024 /* move lowres copy of the image to the ref frame */
1025 for( i = 0; i < 4; i++)
1027 XCHG( uint8_t*, h->fdec->lowres[i], h->fenc->lowres[i] );
1028 XCHG( uint8_t*, h->fdec->buffer_lowres[i], h->fenc->buffer_lowres[i] );
1031 /* adaptive B decision needs a pointer, since it can't use the ref lists */
1032 if( h->sh.i_type != SLICE_TYPE_B )
1033 h->frames.last_nonb = h->fdec;
1035 /* move frame in the buffer */
1036 x264_frame_push( h->frames.reference, h->fdec );
1037 if( h->frames.reference[h->frames.i_max_dpb] )
1038 x264_frame_push_unused( h, x264_frame_shift( h->frames.reference ) );
1039 h->fdec = x264_frame_pop_unused( h );
1042 static inline void x264_reference_reset( x264_t *h )
1044 while( h->frames.reference[0] )
1045 x264_frame_push_unused( h, x264_frame_pop( h->frames.reference ) );
1050 static inline void x264_slice_init( x264_t *h, int i_nal_type, int i_global_qp )
1052 /* ------------------------ Create slice header ----------------------- */
1053 if( i_nal_type == NAL_SLICE_IDR )
1055 x264_slice_header_init( h, &h->sh, h->sps, h->pps, h->i_idr_pic_id, h->i_frame_num, i_global_qp );
1058 h->i_idr_pic_id = ( h->i_idr_pic_id + 1 ) % 65536;
1062 x264_slice_header_init( h, &h->sh, h->sps, h->pps, -1, h->i_frame_num, i_global_qp );
1064 /* always set the real higher num of ref frame used */
1065 h->sh.b_num_ref_idx_override = 1;
1066 h->sh.i_num_ref_idx_l0_active = h->i_ref0 <= 0 ? 1 : h->i_ref0;
1067 h->sh.i_num_ref_idx_l1_active = h->i_ref1 <= 0 ? 1 : h->i_ref1;
1070 h->fdec->i_frame_num = h->sh.i_frame_num;
1072 if( h->sps->i_poc_type == 0 )
1074 h->sh.i_poc_lsb = h->fdec->i_poc & ( (1 << h->sps->i_log2_max_poc_lsb) - 1 );
1075 h->sh.i_delta_poc_bottom = 0; /* XXX won't work for field */
1077 else if( h->sps->i_poc_type == 1 )
1079 /* FIXME TODO FIXME */
1083 /* Nothing to do ? */
1086 x264_macroblock_slice_init( h );
1089 static void x264_slice_write( x264_t *h )
1092 int mb_xy, i_mb_x, i_mb_y;
1093 int i, i_list, i_ref;
1096 memset( &h->stat.frame, 0, sizeof(h->stat.frame) );
1099 x264_nal_start( h, h->i_nal_type, h->i_nal_ref_idc );
1102 x264_slice_header_write( &h->out.bs, &h->sh, h->i_nal_ref_idc );
1103 if( h->param.b_cabac )
1105 /* alignment needed */
1106 bs_align_1( &h->out.bs );
1109 x264_cabac_context_init( &h->cabac, h->sh.i_type, h->sh.i_qp, h->sh.i_cabac_init_idc );
1110 x264_cabac_encode_init ( &h->cabac, h->out.bs.p, h->out.bs.p_end );
1112 h->mb.i_last_qp = h->sh.i_qp;
1113 h->mb.i_last_dqp = 0;
1115 i_mb_y = h->sh.i_first_mb / h->sps->i_mb_width;
1116 i_mb_x = h->sh.i_first_mb % h->sps->i_mb_width;
1119 while( (mb_xy = i_mb_x + i_mb_y * h->sps->i_mb_width) < h->sh.i_last_mb )
1121 int mb_spos = bs_pos(&h->out.bs) + x264_cabac_pos(&h->cabac);
1124 x264_fdec_filter_row( h, i_mb_y );
1127 x264_macroblock_cache_load( h, i_mb_x, i_mb_y );
1129 /* analyse parameters
1130 * Slice I: choose I_4x4 or I_16x16 mode
1131 * Slice P: choose between using P mode or intra (4x4 or 16x16)
1133 x264_macroblock_analyse( h );
1135 /* encode this macroblock -> be careful it can change the mb type to P_SKIP if needed */
1136 x264_macroblock_encode( h );
1138 x264_bitstream_check_buffer( h );
1140 if( h->param.b_cabac )
1142 if( mb_xy > h->sh.i_first_mb && !(h->sh.b_mbaff && (i_mb_y&1)) )
1143 x264_cabac_encode_terminal( &h->cabac );
1145 if( IS_SKIP( h->mb.i_type ) )
1146 x264_cabac_mb_skip( h, 1 );
1149 if( h->sh.i_type != SLICE_TYPE_I )
1150 x264_cabac_mb_skip( h, 0 );
1151 x264_macroblock_write_cabac( h, &h->cabac );
1156 if( IS_SKIP( h->mb.i_type ) )
1160 if( h->sh.i_type != SLICE_TYPE_I )
1162 bs_write_ue( &h->out.bs, i_skip ); /* skip run */
1165 x264_macroblock_write_cavlc( h, &h->out.bs );
1170 if( h->param.b_visualize )
1171 x264_visualize_mb( h );
1175 x264_macroblock_cache_save( h );
1177 /* accumulate mb stats */
1178 h->stat.frame.i_mb_count[h->mb.i_type]++;
1179 if( !IS_SKIP(h->mb.i_type) && !IS_INTRA(h->mb.i_type) && !IS_DIRECT(h->mb.i_type) )
1181 if( h->mb.i_partition != D_8x8 )
1182 h->stat.frame.i_mb_partition[h->mb.i_partition] += 4;
1184 for( i = 0; i < 4; i++ )
1185 h->stat.frame.i_mb_partition[h->mb.i_sub_partition[i]] ++;
1186 if( h->param.i_frame_reference > 1 )
1187 for( i_list = 0; i_list <= (h->sh.i_type == SLICE_TYPE_B); i_list++ )
1188 for( i = 0; i < 4; i++ )
1190 i_ref = h->mb.cache.ref[i_list][ x264_scan8[4*i] ];
1192 h->stat.frame.i_mb_count_ref[i_list][i_ref] ++;
1195 if( h->mb.i_cbp_luma && !IS_INTRA(h->mb.i_type) )
1197 h->stat.frame.i_mb_count_8x8dct[0] ++;
1198 h->stat.frame.i_mb_count_8x8dct[1] += h->mb.b_transform_8x8;
1201 x264_ratecontrol_mb( h, bs_pos(&h->out.bs) + x264_cabac_pos(&h->cabac) - mb_spos );
1205 i_mb_x += i_mb_y & 1;
1206 i_mb_y ^= i_mb_x < h->sps->i_mb_width;
1210 if(i_mb_x == h->sps->i_mb_width)
1217 if( h->param.b_cabac )
1219 x264_cabac_encode_flush( h, &h->cabac );
1220 h->out.bs.p = h->cabac.p;
1225 bs_write_ue( &h->out.bs, i_skip ); /* last skip run */
1226 /* rbsp_slice_trailing_bits */
1227 bs_rbsp_trailing( &h->out.bs );
1232 x264_fdec_filter_row( h, h->sps->i_mb_height );
1234 /* Compute misc bits */
1235 h->stat.frame.i_misc_bits = bs_pos( &h->out.bs )
1237 - h->stat.frame.i_tex_bits
1238 - h->stat.frame.i_mv_bits;
1241 static void x264_thread_sync_context( x264_t *dst, x264_t *src )
1247 // reference counting
1248 for( f = src->frames.reference; *f; f++ )
1249 (*f)->i_reference_count++;
1250 for( f = dst->frames.reference; *f; f++ )
1251 x264_frame_push_unused( src, *f );
1252 src->fdec->i_reference_count++;
1253 x264_frame_push_unused( src, dst->fdec );
1255 // copy everything except the per-thread pointers and the constants.
1256 memcpy( &dst->i_frame, &src->i_frame, offsetof(x264_t, mb.type) - offsetof(x264_t, i_frame) );
1257 dst->stat = src->stat;
1260 static void x264_thread_sync_stat( x264_t *dst, x264_t *src )
1264 memcpy( &dst->stat.i_slice_count, &src->stat.i_slice_count, sizeof(dst->stat) - sizeof(dst->stat.frame) );
1267 static int x264_slices_write( x264_t *h )
1272 if( h->param.b_visualize )
1273 x264_visualize_init( h );
1276 x264_stack_align( x264_slice_write, h );
1277 i_frame_size = h->out.nal[h->out.i_nal-1].i_payload;
1280 if( h->param.b_visualize )
1282 x264_visualize_show( h );
1283 x264_visualize_close( h );
1287 h->out.i_frame_size = i_frame_size;
1291 /****************************************************************************
1292 * x264_encoder_encode:
1293 * XXX: i_poc : is the poc of the current given picture
1294 * i_frame : is the number of the frame being coded
1295 * ex: type frame poc
1303 ****************************************************************************/
1304 int x264_encoder_encode( x264_t *h,
1305 x264_nal_t **pp_nal, int *pi_nal,
1306 x264_picture_t *pic_in,
1307 x264_picture_t *pic_out )
1309 x264_t *thread_current, *thread_prev, *thread_oldest;
1315 if( h->param.i_threads > 1)
1317 int i = ++h->i_thread_phase;
1318 int t = h->param.i_threads;
1319 thread_current = h->thread[ i%t ];
1320 thread_prev = h->thread[ (i-1)%t ];
1321 thread_oldest = h->thread[ (i+1)%t ];
1322 x264_thread_sync_context( thread_current, thread_prev );
1323 x264_thread_sync_ratecontrol( thread_current, thread_prev, thread_oldest );
1325 // fprintf(stderr, "current: %p prev: %p oldest: %p \n", thread_current, thread_prev, thread_oldest);
1334 // ok to call this before encoding any frames, since the initial values of fdec have b_kept_as_ref=0
1335 x264_reference_update( h );
1336 h->fdec->i_lines_completed = -1;
1342 /* ------------------- Setup new frame from picture -------------------- */
1343 if( pic_in != NULL )
1345 /* 1: Copy the picture to a frame and move it to a buffer */
1346 x264_frame_t *fenc = x264_frame_pop_unused( h );
1348 if( x264_frame_copy_picture( h, fenc, pic_in ) < 0 )
1351 if( h->param.i_width != 16 * h->sps->i_mb_width ||
1352 h->param.i_height != 16 * h->sps->i_mb_height )
1353 x264_frame_expand_border_mod16( h, fenc );
1355 fenc->i_frame = h->frames.i_input++;
1357 x264_frame_push( h->frames.next, fenc );
1359 if( h->frames.b_have_lowres )
1360 x264_frame_init_lowres( h, fenc );
1362 if( h->param.rc.i_aq_mode )
1363 x264_adaptive_quant_frame( h, fenc );
1365 if( h->frames.i_input <= h->frames.i_delay + 1 - h->param.i_threads )
1367 /* Nothing yet to encode */
1368 /* waiting for filling bframe buffer */
1369 pic_out->i_type = X264_TYPE_AUTO;
1374 if( h->frames.current[0] == NULL )
1377 /* 2: Select frame types */
1378 if( h->frames.next[0] == NULL )
1380 x264_encoder_frame_end( thread_oldest, thread_current, pp_nal, pi_nal, pic_out );
1384 x264_slicetype_decide( h );
1386 /* 3: move some B-frames and 1 non-B to encode queue */
1387 while( IS_X264_TYPE_B( h->frames.next[bframes]->i_type ) )
1389 x264_frame_push( h->frames.current, x264_frame_shift( &h->frames.next[bframes] ) );
1390 /* FIXME: when max B-frames > 3, BREF may no longer be centered after GOP closing */
1391 if( h->param.b_bframe_pyramid && bframes > 1 )
1393 x264_frame_t *mid = x264_frame_shift( &h->frames.next[bframes/2] );
1394 mid->i_type = X264_TYPE_BREF;
1395 x264_frame_push( h->frames.current, mid );
1399 x264_frame_push( h->frames.current, x264_frame_shift( h->frames.next ) );
1402 /* ------------------- Get frame to be encoded ------------------------- */
1403 /* 4: get picture to encode */
1404 h->fenc = x264_frame_shift( h->frames.current );
1405 if( h->fenc == NULL )
1407 /* Nothing yet to encode (ex: waiting for I/P with B frames) */
1408 /* waiting for filling bframe buffer */
1409 pic_out->i_type = X264_TYPE_AUTO;
1415 if( h->fenc->i_type == X264_TYPE_IDR )
1417 h->frames.i_last_idr = h->fenc->i_frame;
1420 /* ------------------- Setup frame context ----------------------------- */
1421 /* 5: Init data dependent of frame type */
1422 if( h->fenc->i_type == X264_TYPE_IDR )
1424 /* reset ref pictures */
1425 x264_reference_reset( h );
1427 i_nal_type = NAL_SLICE_IDR;
1428 i_nal_ref_idc = NAL_PRIORITY_HIGHEST;
1429 h->sh.i_type = SLICE_TYPE_I;
1431 else if( h->fenc->i_type == X264_TYPE_I )
1433 i_nal_type = NAL_SLICE;
1434 i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/
1435 h->sh.i_type = SLICE_TYPE_I;
1437 else if( h->fenc->i_type == X264_TYPE_P )
1439 i_nal_type = NAL_SLICE;
1440 i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/
1441 h->sh.i_type = SLICE_TYPE_P;
1443 else if( h->fenc->i_type == X264_TYPE_BREF )
1445 i_nal_type = NAL_SLICE;
1446 i_nal_ref_idc = NAL_PRIORITY_HIGH; /* maybe add MMCO to forget it? -> low */
1447 h->sh.i_type = SLICE_TYPE_B;
1451 i_nal_type = NAL_SLICE;
1452 i_nal_ref_idc = NAL_PRIORITY_DISPOSABLE;
1453 h->sh.i_type = SLICE_TYPE_B;
1457 h->fenc->i_poc = 2 * (h->fenc->i_frame - h->frames.i_last_idr);
1458 h->fdec->i_type = h->fenc->i_type;
1459 h->fdec->i_frame = h->fenc->i_frame;
1460 h->fenc->b_kept_as_ref =
1461 h->fdec->b_kept_as_ref = i_nal_ref_idc != NAL_PRIORITY_DISPOSABLE && h->param.i_keyint_max > 1;
1465 /* ------------------- Init ----------------------------- */
1466 /* build ref list 0/1 */
1467 x264_reference_build_list( h, h->fdec->i_poc );
1469 /* Init the rate control */
1470 x264_ratecontrol_start( h, h->fenc->i_qpplus1 );
1471 i_global_qp = x264_ratecontrol_qp( h );
1473 pic_out->i_qpplus1 =
1474 h->fdec->i_qpplus1 = i_global_qp + 1;
1476 if( h->sh.i_type == SLICE_TYPE_B )
1477 x264_macroblock_bipred_init( h );
1479 /* ------------------------ Create slice header ----------------------- */
1480 x264_slice_init( h, i_nal_type, i_global_qp );
1482 if( i_nal_ref_idc != NAL_PRIORITY_DISPOSABLE )
1485 /* ---------------------- Write the bitstream -------------------------- */
1486 /* Init bitstream context */
1488 bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream );
1493 if(h->sh.i_type == SLICE_TYPE_I)
1495 else if(h->sh.i_type == SLICE_TYPE_P)
1497 else if(h->sh.i_type == SLICE_TYPE_B)
1502 x264_nal_start(h, NAL_AUD, NAL_PRIORITY_DISPOSABLE);
1503 bs_write(&h->out.bs, 3, pic_type);
1504 bs_rbsp_trailing(&h->out.bs);
1508 h->i_nal_type = i_nal_type;
1509 h->i_nal_ref_idc = i_nal_ref_idc;
1511 /* Write SPS and PPS */
1512 if( i_nal_type == NAL_SLICE_IDR && h->param.b_repeat_headers )
1514 if( h->fenc->i_frame == 0 )
1516 /* identify ourself */
1517 x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE );
1518 x264_sei_version_write( h, &h->out.bs );
1522 /* generate sequence parameters */
1523 x264_nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST );
1524 x264_sps_write( &h->out.bs, h->sps );
1527 /* generate picture parameters */
1528 x264_nal_start( h, NAL_PPS, NAL_PRIORITY_HIGHEST );
1529 x264_pps_write( &h->out.bs, h->pps );
1534 if( h->param.i_threads > 1 )
1536 x264_pthread_create( &h->thread_handle, NULL, (void*)x264_slices_write, h );
1537 h->b_thread_active = 1;
1540 x264_slices_write( h );
1542 /* restore CPU state (before using float again) */
1545 if( h->sh.i_type == SLICE_TYPE_P && !h->param.rc.b_stat_read
1546 && h->param.i_scenecut_threshold >= 0
1547 && !h->param.b_pre_scenecut )
1549 const int *mbs = h->stat.frame.i_mb_count;
1550 int i_mb_i = mbs[I_16x16] + mbs[I_8x8] + mbs[I_4x4];
1551 int i_mb_p = mbs[P_L0] + mbs[P_8x8];
1552 int i_mb_s = mbs[P_SKIP];
1553 int i_mb = h->sps->i_mb_width * h->sps->i_mb_height;
1554 int64_t i_inter_cost = h->stat.frame.i_inter_cost;
1555 int64_t i_intra_cost = h->stat.frame.i_intra_cost;
1558 int i_gop_size = h->fenc->i_frame - h->frames.i_last_idr;
1559 float f_thresh_max = h->param.i_scenecut_threshold / 100.0;
1560 /* magic numbers pulled out of thin air */
1561 float f_thresh_min = f_thresh_max * h->param.i_keyint_min
1562 / ( h->param.i_keyint_max * 4 );
1563 if( h->param.i_keyint_min == h->param.i_keyint_max )
1564 f_thresh_min= f_thresh_max;
1566 /* macroblock_analyse() doesn't further analyse skipped mbs,
1567 * so we have to guess their cost */
1568 if( h->stat.frame.i_mbs_analysed > 0 )
1569 i_intra_cost = i_intra_cost * i_mb / h->stat.frame.i_mbs_analysed;
1571 if( i_gop_size < h->param.i_keyint_min / 4 )
1572 f_bias = f_thresh_min / 4;
1573 else if( i_gop_size <= h->param.i_keyint_min )
1574 f_bias = f_thresh_min * i_gop_size / h->param.i_keyint_min;
1577 f_bias = f_thresh_min
1578 + ( f_thresh_max - f_thresh_min )
1579 * ( i_gop_size - h->param.i_keyint_min )
1580 / ( h->param.i_keyint_max - h->param.i_keyint_min );
1582 f_bias = X264_MIN( f_bias, 1.0 );
1584 /* Bad P will be reencoded as I */
1585 if( h->stat.frame.i_mbs_analysed > 0 &&
1586 i_inter_cost >= (1.0 - f_bias) * i_intra_cost )
1590 x264_log( h, X264_LOG_DEBUG, "scene cut at %d Icost:%.0f Pcost:%.0f ratio:%.4f bias:%.4f gop:%d (imb:%d pmb:%d smb:%d)\n",
1592 (double)i_intra_cost, (double)i_inter_cost,
1593 1. - (double)i_inter_cost / i_intra_cost,
1595 i_mb_i, i_mb_p, i_mb_s );
1597 /* Restore frame num */
1600 for( b = 0; h->frames.current[b] && IS_X264_TYPE_B( h->frames.current[b]->i_type ); b++ );
1603 /* If using B-frames, force GOP to be closed.
1604 * Even if this frame is going to be I and not IDR, forcing a
1605 * P-frame before the scenecut will probably help compression.
1607 * We don't yet know exactly which frame is the scene cut, so
1608 * we can't assign an I-frame. Instead, change the previous
1609 * B-frame to P, and rearrange coding order. */
1611 if( h->param.b_bframe_adaptive || b > 1 )
1612 h->fenc->i_type = X264_TYPE_AUTO;
1613 x264_frame_sort_pts( h->frames.current );
1614 x264_frame_unshift( h->frames.next, h->fenc );
1615 h->fenc = h->frames.current[b-1];
1616 h->frames.current[b-1] = NULL;
1617 h->fenc->i_type = X264_TYPE_P;
1618 x264_frame_sort_dts( h->frames.current );
1620 /* Do IDR if needed */
1621 else if( i_gop_size >= h->param.i_keyint_min )
1626 /* Reinit field of fenc */
1627 h->fenc->i_type = X264_TYPE_IDR;
1630 /* Put enqueued frames back in the pool */
1631 while( h->frames.current[0] )
1632 x264_frame_push( h->frames.next, x264_frame_shift( h->frames.current ) );
1633 x264_frame_sort_pts( h->frames.next );
1637 h->fenc->i_type = X264_TYPE_I;
1643 x264_encoder_frame_end( thread_oldest, thread_current, pp_nal, pi_nal, pic_out );
1647 static void x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
1648 x264_nal_t **pp_nal, int *pi_nal,
1649 x264_picture_t *pic_out )
1652 char psz_message[80];
1654 if( h->b_thread_active )
1656 x264_pthread_join( h->thread_handle, NULL );
1657 h->b_thread_active = 0;
1661 pic_out->i_type = X264_TYPE_AUTO;
1665 x264_frame_push_unused( thread_current, h->fenc );
1667 /* End bitstream, set output */
1668 *pi_nal = h->out.i_nal;
1669 *pp_nal = h->out.nal;
1672 /* Set output picture properties */
1673 if( h->sh.i_type == SLICE_TYPE_I )
1674 pic_out->i_type = h->i_nal_type == NAL_SLICE_IDR ? X264_TYPE_IDR : X264_TYPE_I;
1675 else if( h->sh.i_type == SLICE_TYPE_P )
1676 pic_out->i_type = X264_TYPE_P;
1678 pic_out->i_type = X264_TYPE_B;
1679 pic_out->i_pts = h->fenc->i_pts;
1681 pic_out->img.i_plane = h->fdec->i_plane;
1682 for(i = 0; i < 4; i++){
1683 pic_out->img.i_stride[i] = h->fdec->i_stride[i];
1684 pic_out->img.plane[i] = h->fdec->plane[i];
1687 /* ---------------------- Update encoder state ------------------------- */
1691 x264_ratecontrol_end( h, h->out.i_frame_size * 8 );
1693 /* restore CPU state (before using float again) */
1696 x264_noise_reduction_update( thread_current );
1698 /* ---------------------- Compute/Print statistics --------------------- */
1699 x264_thread_sync_stat( h, h->thread[0] );
1702 h->stat.i_slice_count[h->sh.i_type]++;
1703 h->stat.i_slice_size[h->sh.i_type] += h->out.i_frame_size + NALU_OVERHEAD;
1704 h->stat.f_slice_qp[h->sh.i_type] += h->fdec->f_qp_avg_aq;
1706 for( i = 0; i < X264_MBTYPE_MAX; i++ )
1707 h->stat.i_mb_count[h->sh.i_type][i] += h->stat.frame.i_mb_count[i];
1708 for( i = 0; i < X264_PARTTYPE_MAX; i++ )
1709 h->stat.i_mb_partition[h->sh.i_type][i] += h->stat.frame.i_mb_partition[i];
1710 for( i = 0; i < 2; i++ )
1711 h->stat.i_mb_count_8x8dct[i] += h->stat.frame.i_mb_count_8x8dct[i];
1712 if( h->sh.i_type != SLICE_TYPE_I )
1713 for( i_list = 0; i_list < 2; i_list++ )
1714 for( i = 0; i < 32; i++ )
1715 h->stat.i_mb_count_ref[h->sh.i_type][i_list][i] += h->stat.frame.i_mb_count_ref[i_list][i];
1716 if( h->sh.i_type == SLICE_TYPE_P )
1717 h->stat.i_consecutive_bframes[h->fdec->i_frame - h->fref0[0]->i_frame - 1]++;
1718 if( h->sh.i_type == SLICE_TYPE_B )
1720 h->stat.i_direct_frames[ h->sh.b_direct_spatial_mv_pred ] ++;
1721 if( h->mb.b_direct_auto_write )
1723 //FIXME somewhat arbitrary time constants
1724 if( h->stat.i_direct_score[0] + h->stat.i_direct_score[1] > h->mb.i_mb_count )
1726 for( i = 0; i < 2; i++ )
1727 h->stat.i_direct_score[i] = h->stat.i_direct_score[i] * 9/10;
1729 for( i = 0; i < 2; i++ )
1730 h->stat.i_direct_score[i] += h->stat.frame.i_direct_score[i];
1734 psz_message[0] = '\0';
1735 if( h->param.analyse.b_psnr )
1738 h->stat.frame.i_ssd[0],
1739 h->stat.frame.i_ssd[1],
1740 h->stat.frame.i_ssd[2],
1743 h->stat.i_ssd_global[h->sh.i_type] += ssd[0] + ssd[1] + ssd[2];
1744 h->stat.f_psnr_average[h->sh.i_type] += x264_psnr( ssd[0] + ssd[1] + ssd[2], 3 * h->param.i_width * h->param.i_height / 2 );
1745 h->stat.f_psnr_mean_y[h->sh.i_type] += x264_psnr( ssd[0], h->param.i_width * h->param.i_height );
1746 h->stat.f_psnr_mean_u[h->sh.i_type] += x264_psnr( ssd[1], h->param.i_width * h->param.i_height / 4 );
1747 h->stat.f_psnr_mean_v[h->sh.i_type] += x264_psnr( ssd[2], h->param.i_width * h->param.i_height / 4 );
1749 snprintf( psz_message, 80, " PSNR Y:%5.2f U:%5.2f V:%5.2f",
1750 x264_psnr( ssd[0], h->param.i_width * h->param.i_height ),
1751 x264_psnr( ssd[1], h->param.i_width * h->param.i_height / 4),
1752 x264_psnr( ssd[2], h->param.i_width * h->param.i_height / 4) );
1755 if( h->param.analyse.b_ssim )
1757 double ssim_y = h->stat.frame.f_ssim
1758 / (((h->param.i_width-6)>>2) * ((h->param.i_height-6)>>2));
1759 h->stat.f_ssim_mean_y[h->sh.i_type] += ssim_y;
1760 snprintf( psz_message + strlen(psz_message), 80 - strlen(psz_message),
1761 " SSIM Y:%.5f", ssim_y );
1763 psz_message[79] = '\0';
1765 x264_log( h, X264_LOG_DEBUG,
1766 "frame=%4d QP=%.2f NAL=%d Slice:%c Poc:%-3d I:%-4d P:%-4d SKIP:%-4d size=%d bytes%s\n",
1768 h->fdec->f_qp_avg_aq,
1770 h->sh.i_type == SLICE_TYPE_I ? 'I' : (h->sh.i_type == SLICE_TYPE_P ? 'P' : 'B' ),
1772 h->stat.frame.i_mb_count_i,
1773 h->stat.frame.i_mb_count_p,
1774 h->stat.frame.i_mb_count_skip,
1775 h->out.i_frame_size,
1778 // keep stats all in one place
1779 x264_thread_sync_stat( h->thread[0], h );
1780 // for the use of the next frame
1781 x264_thread_sync_stat( thread_current, h );
1783 #ifdef DEBUG_MB_TYPE
1785 static const char mb_chars[] = { 'i', 'i', 'I', 'C', 'P', '8', 'S',
1786 'D', '<', 'X', 'B', 'X', '>', 'B', 'B', 'B', 'B', '8', 'S' };
1788 for( mb_xy = 0; mb_xy < h->sps->i_mb_width * h->sps->i_mb_height; mb_xy++ )
1790 if( h->mb.type[mb_xy] < X264_MBTYPE_MAX && h->mb.type[mb_xy] >= 0 )
1791 fprintf( stderr, "%c ", mb_chars[ h->mb.type[mb_xy] ] );
1793 fprintf( stderr, "? " );
1795 if( (mb_xy+1) % h->sps->i_mb_width == 0 )
1796 fprintf( stderr, "\n" );
1801 if( h->param.psz_dump_yuv )
1802 x264_frame_dump( h );
1805 static void x264_print_intra( int64_t *i_mb_count, double i_count, int b_print_pcm, char *intra )
1807 intra += sprintf( intra, "I16..4%s: %4.1f%% %4.1f%% %4.1f%%",
1808 b_print_pcm ? "..PCM" : "",
1809 i_mb_count[I_16x16]/ i_count,
1810 i_mb_count[I_8x8] / i_count,
1811 i_mb_count[I_4x4] / i_count );
1813 sprintf( intra, " %4.1f%%", i_mb_count[I_PCM] / i_count );
1816 /****************************************************************************
1817 * x264_encoder_close:
1818 ****************************************************************************/
1819 void x264_encoder_close ( x264_t *h )
1821 int64_t i_yuv_size = 3 * h->param.i_width * h->param.i_height / 2;
1822 int64_t i_mb_count_size[2][7] = {{0}};
1824 int i, j, i_list, i_type;
1825 int b_print_pcm = h->stat.i_mb_count[SLICE_TYPE_I][I_PCM]
1826 || h->stat.i_mb_count[SLICE_TYPE_P][I_PCM]
1827 || h->stat.i_mb_count[SLICE_TYPE_B][I_PCM];
1829 for( i=0; i<h->param.i_threads; i++ )
1831 // don't strictly have to wait for the other threads, but it's simpler than canceling them
1832 if( h->thread[i]->b_thread_active )
1834 x264_pthread_join( h->thread[i]->thread_handle, NULL );
1835 assert( h->thread[i]->fenc->i_reference_count == 1 );
1836 x264_frame_delete( h->thread[i]->fenc );
1840 /* Slices used and PSNR */
1841 for( i=0; i<5; i++ )
1843 static const int slice_order[] = { SLICE_TYPE_I, SLICE_TYPE_SI, SLICE_TYPE_P, SLICE_TYPE_SP, SLICE_TYPE_B };
1844 static const char *slice_name[] = { "P", "B", "I", "SP", "SI" };
1845 int i_slice = slice_order[i];
1847 if( h->stat.i_slice_count[i_slice] > 0 )
1849 const int i_count = h->stat.i_slice_count[i_slice];
1850 if( h->param.analyse.b_psnr )
1852 x264_log( h, X264_LOG_INFO,
1853 "slice %s:%-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",
1854 slice_name[i_slice],
1856 h->stat.f_slice_qp[i_slice] / i_count,
1857 (double)h->stat.i_slice_size[i_slice] / i_count,
1858 h->stat.f_psnr_mean_y[i_slice] / i_count, h->stat.f_psnr_mean_u[i_slice] / i_count, h->stat.f_psnr_mean_v[i_slice] / i_count,
1859 h->stat.f_psnr_average[i_slice] / i_count,
1860 x264_psnr( h->stat.i_ssd_global[i_slice], i_count * i_yuv_size ) );
1864 x264_log( h, X264_LOG_INFO,
1865 "slice %s:%-5d Avg QP:%5.2f size:%6.0f\n",
1866 slice_name[i_slice],
1868 h->stat.f_slice_qp[i_slice] / i_count,
1869 (double)h->stat.i_slice_size[i_slice] / i_count );
1873 if( h->param.i_bframe && h->stat.i_slice_count[SLICE_TYPE_P] )
1877 // weight by number of frames (including the P-frame) that are in a sequence of N B-frames
1878 for( i=0; i<=h->param.i_bframe; i++ )
1879 den += (i+1) * h->stat.i_consecutive_bframes[i];
1880 for( i=0; i<=h->param.i_bframe; i++ )
1881 p += sprintf( p, " %4.1f%%", 100. * (i+1) * h->stat.i_consecutive_bframes[i] / den );
1882 x264_log( h, X264_LOG_INFO, "consecutive B-frames:%s\n", buf );
1885 for( i_type = 0; i_type < 2; i_type++ )
1886 for( i = 0; i < X264_PARTTYPE_MAX; i++ )
1888 if( i == D_DIRECT_8x8 ) continue; /* direct is counted as its own type */
1889 i_mb_count_size[i_type][x264_mb_partition_pixel_table[i]] += h->stat.i_mb_partition[i_type][i];
1893 if( h->stat.i_slice_count[SLICE_TYPE_I] > 0 )
1895 int64_t *i_mb_count = h->stat.i_mb_count[SLICE_TYPE_I];
1896 double i_count = h->stat.i_slice_count[SLICE_TYPE_I] * h->mb.i_mb_count / 100.0;
1897 x264_print_intra( i_mb_count, i_count, b_print_pcm, buf );
1898 x264_log( h, X264_LOG_INFO, "mb I %s\n", buf );
1900 if( h->stat.i_slice_count[SLICE_TYPE_P] > 0 )
1902 int64_t *i_mb_count = h->stat.i_mb_count[SLICE_TYPE_P];
1903 double i_count = h->stat.i_slice_count[SLICE_TYPE_P] * h->mb.i_mb_count / 100.0;
1904 int64_t *i_mb_size = i_mb_count_size[SLICE_TYPE_P];
1905 x264_print_intra( i_mb_count, i_count, b_print_pcm, buf );
1906 x264_log( h, X264_LOG_INFO,
1907 "mb P %s P16..4: %4.1f%% %4.1f%% %4.1f%% %4.1f%% %4.1f%% skip:%4.1f%%\n",
1909 i_mb_size[PIXEL_16x16] / (i_count*4),
1910 (i_mb_size[PIXEL_16x8] + i_mb_size[PIXEL_8x16]) / (i_count*4),
1911 i_mb_size[PIXEL_8x8] / (i_count*4),
1912 (i_mb_size[PIXEL_8x4] + i_mb_size[PIXEL_4x8]) / (i_count*4),
1913 i_mb_size[PIXEL_4x4] / (i_count*4),
1914 i_mb_count[P_SKIP] / i_count );
1916 if( h->stat.i_slice_count[SLICE_TYPE_B] > 0 )
1918 int64_t *i_mb_count = h->stat.i_mb_count[SLICE_TYPE_B];
1919 double i_count = h->stat.i_slice_count[SLICE_TYPE_B] * h->mb.i_mb_count / 100.0;
1920 double i_mb_list_count;
1921 int64_t *i_mb_size = i_mb_count_size[SLICE_TYPE_B];
1922 int64_t list_count[3] = {0}; /* 0 == L0, 1 == L1, 2 == BI */
1923 x264_print_intra( i_mb_count, i_count, b_print_pcm, buf );
1924 for( i = 0; i < X264_PARTTYPE_MAX; i++ )
1925 for( j = 0; j < 2; j++ )
1927 int l0 = x264_mb_type_list0_table[i][j];
1928 int l1 = x264_mb_type_list1_table[i][j];
1930 list_count[l1+l0*l1] += h->stat.i_mb_count[SLICE_TYPE_B][i] * 2;
1932 list_count[0] += h->stat.i_mb_partition[SLICE_TYPE_B][D_L0_8x8];
1933 list_count[1] += h->stat.i_mb_partition[SLICE_TYPE_B][D_L1_8x8];
1934 list_count[2] += h->stat.i_mb_partition[SLICE_TYPE_B][D_BI_8x8];
1935 i_mb_count[B_DIRECT] += (h->stat.i_mb_partition[SLICE_TYPE_B][D_DIRECT_8x8]+2)/4;
1936 i_mb_list_count = (list_count[0] + list_count[1] + list_count[2]) / 100.0;
1937 x264_log( h, X264_LOG_INFO,
1938 "mb B %s B16..8: %4.1f%% %4.1f%% %4.1f%% direct:%4.1f%% skip:%4.1f%% L0:%4.1f%% L1:%4.1f%% BI:%4.1f%%\n",
1940 i_mb_size[PIXEL_16x16] / (i_count*4),
1941 (i_mb_size[PIXEL_16x8] + i_mb_size[PIXEL_8x16]) / (i_count*4),
1942 i_mb_size[PIXEL_8x8] / (i_count*4),
1943 i_mb_count[B_DIRECT] / i_count,
1944 i_mb_count[B_SKIP] / i_count,
1945 list_count[0] / i_mb_list_count,
1946 list_count[1] / i_mb_list_count,
1947 list_count[2] / i_mb_list_count );
1950 x264_ratecontrol_summary( h );
1952 if( h->stat.i_slice_count[SLICE_TYPE_I] + h->stat.i_slice_count[SLICE_TYPE_P] + h->stat.i_slice_count[SLICE_TYPE_B] > 0 )
1954 const int i_count = h->stat.i_slice_count[SLICE_TYPE_I] +
1955 h->stat.i_slice_count[SLICE_TYPE_P] +
1956 h->stat.i_slice_count[SLICE_TYPE_B];
1957 float fps = (float) h->param.i_fps_num / h->param.i_fps_den;
1958 #define SUM3(p) (p[SLICE_TYPE_I] + p[SLICE_TYPE_P] + p[SLICE_TYPE_B])
1959 #define SUM3b(p,o) (p[SLICE_TYPE_I][o] + p[SLICE_TYPE_P][o] + p[SLICE_TYPE_B][o])
1960 float f_bitrate = fps * SUM3(h->stat.i_slice_size) / i_count / 125;
1962 if( h->pps->b_transform_8x8_mode )
1964 int64_t i_i8x8 = SUM3b( h->stat.i_mb_count, I_8x8 );
1965 int64_t i_intra = i_i8x8 + SUM3b( h->stat.i_mb_count, I_4x4 )
1966 + SUM3b( h->stat.i_mb_count, I_16x16 );
1967 x264_log( h, X264_LOG_INFO, "8x8 transform intra:%.1f%% inter:%.1f%%\n",
1968 100. * i_i8x8 / i_intra,
1969 100. * h->stat.i_mb_count_8x8dct[1] / h->stat.i_mb_count_8x8dct[0] );
1972 if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_AUTO
1973 && h->stat.i_slice_count[SLICE_TYPE_B] )
1975 x264_log( h, X264_LOG_INFO, "direct mvs spatial:%.1f%% temporal:%.1f%%\n",
1976 h->stat.i_direct_frames[1] * 100. / h->stat.i_slice_count[SLICE_TYPE_B],
1977 h->stat.i_direct_frames[0] * 100. / h->stat.i_slice_count[SLICE_TYPE_B] );
1980 for( i_list = 0; i_list < 2; i_list++ )
1983 for( i_slice = 0; i_slice < 2; i_slice++ )
1988 for( i = 0; i < 32; i++ )
1989 if( h->stat.i_mb_count_ref[i_slice][i_list][i] )
1991 i_den += h->stat.i_mb_count_ref[i_slice][i_list][i];
1996 for( i = 0; i <= i_max; i++ )
1997 p += sprintf( p, " %4.1f%%", 100. * h->stat.i_mb_count_ref[i_slice][i_list][i] / i_den );
1998 x264_log( h, X264_LOG_INFO, "ref %c L%d %s\n", "PB"[i_slice], i_list, buf );
2002 if( h->param.analyse.b_ssim )
2004 x264_log( h, X264_LOG_INFO,
2005 "SSIM Mean Y:%.7f\n",
2006 SUM3( h->stat.f_ssim_mean_y ) / i_count );
2008 if( h->param.analyse.b_psnr )
2010 x264_log( h, X264_LOG_INFO,
2011 "PSNR Mean Y:%6.3f U:%6.3f V:%6.3f Avg:%6.3f Global:%6.3f kb/s:%.2f\n",
2012 SUM3( h->stat.f_psnr_mean_y ) / i_count,
2013 SUM3( h->stat.f_psnr_mean_u ) / i_count,
2014 SUM3( h->stat.f_psnr_mean_v ) / i_count,
2015 SUM3( h->stat.f_psnr_average ) / i_count,
2016 x264_psnr( SUM3( h->stat.i_ssd_global ), i_count * i_yuv_size ),
2020 x264_log( h, X264_LOG_INFO, "kb/s:%.1f\n", f_bitrate );
2024 x264_ratecontrol_delete( h );
2027 if( h->param.rc.psz_stat_out )
2028 free( h->param.rc.psz_stat_out );
2029 if( h->param.rc.psz_stat_in )
2030 free( h->param.rc.psz_stat_in );
2032 x264_cqm_delete( h );
2034 if( h->param.i_threads > 1)
2035 h = h->thread[ h->i_thread_phase % h->param.i_threads ];
2038 for( i = 0; h->frames.current[i]; i++ )
2040 assert( h->frames.current[i]->i_reference_count == 1 );
2041 x264_frame_delete( h->frames.current[i] );
2043 for( i = 0; h->frames.next[i]; i++ )
2045 assert( h->frames.next[i]->i_reference_count == 1 );
2046 x264_frame_delete( h->frames.next[i] );
2048 for( i = 0; h->frames.unused[i]; i++ )
2050 assert( h->frames.unused[i]->i_reference_count == 0 );
2051 x264_frame_delete( h->frames.unused[i] );
2056 for( i = h->param.i_threads - 1; i >= 0; i-- )
2058 x264_frame_t **frame;
2060 for( frame = h->thread[i]->frames.reference; *frame; frame++ )
2062 assert( (*frame)->i_reference_count > 0 );
2063 (*frame)->i_reference_count--;
2064 if( (*frame)->i_reference_count == 0 )
2065 x264_frame_delete( *frame );
2067 frame = &h->thread[i]->fdec;
2068 assert( (*frame)->i_reference_count > 0 );
2069 (*frame)->i_reference_count--;
2070 if( (*frame)->i_reference_count == 0 )
2071 x264_frame_delete( *frame );
2073 x264_macroblock_cache_end( h->thread[i] );
2074 x264_free( h->thread[i]->out.p_bitstream );
2075 x264_free( h->thread[i] );