1
/*****************************************************************************
3
*****************************************************************************
4
* Copyright (C) 2003 Laurent Aimar
5
* $Id: encoder.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
7
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
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., 59 Temple Place - Suite 330, 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
39
//#define DEBUG_DUMP_FRAME
40
//#define DEBUG_BENCHMARK
42
#ifdef DEBUG_BENCHMARK
43
static int64_t i_mtime_encode_frame = 0;
44
static int64_t i_mtime_analyse = 0;
45
static int64_t i_mtime_encode = 0;
46
static int64_t i_mtime_write = 0;
47
static int64_t i_mtime_filter = 0;
48
#define TIMER_START( d ) \
50
int64_t d##start = x264_mdate();
52
#define TIMER_STOP( d ) \
53
d += x264_mdate() - d##start;\
56
#define TIMER_START( d )
57
#define TIMER_STOP( d )
60
#define NALU_OVERHEAD 5 // startcode + NAL type costs 5 bytes per frame
62
static void x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
63
x264_nal_t **pp_nal, int *pi_nal,
64
x264_picture_t *pic_out );
66
/****************************************************************************
68
******************************* x264 libs **********************************
70
****************************************************************************/
71
static float x264_psnr( int64_t i_sqe, int64_t i_size )
73
double f_mse = (double)i_sqe / ((double)65025.0 * (double)i_size);
74
if( f_mse <= 0.0000000001 ) /* Max 100dB */
77
return (float)(-10.0 * log( f_mse ) / log( 10.0 ));
80
#ifdef DEBUG_DUMP_FRAME
81
static void x264_frame_dump( x264_t *h, x264_frame_t *fr, char *name )
83
FILE *f = fopen( name, "r+b" );
88
/* Write the frame in display order */
89
fseek( f, fr->i_frame * h->param.i_height * h->param.i_width * 3 / 2, SEEK_SET );
91
for( i = 0; i < fr->i_plane; i++ )
93
for( y = 0; y < h->param.i_height / ( i == 0 ? 1 : 2 ); y++ )
95
fwrite( &fr->plane[i][y*fr->i_stride[i]], 1, h->param.i_width / ( i == 0 ? 1 : 2 ), f );
103
/* Fill "default" values */
104
static void x264_slice_header_init( x264_t *h, x264_slice_header_t *sh,
105
x264_sps_t *sps, x264_pps_t *pps,
106
int i_idr_pic_id, int i_frame, int i_qp )
108
x264_param_t *param = &h->param;
111
/* First we fill all field */
116
sh->i_last_mb = h->sps->i_mb_width * h->sps->i_mb_height;
117
sh->i_pps_id = pps->i_id;
119
sh->i_frame_num = i_frame;
121
sh->b_mbaff = h->param.b_interlaced;
122
sh->b_field_pic = 0; /* no field support for now */
123
sh->b_bottom_field = 0; /* not yet used */
125
sh->i_idr_pic_id = i_idr_pic_id;
127
/* poc stuff, fixed later */
129
sh->i_delta_poc_bottom = 0;
130
sh->i_delta_poc[0] = 0;
131
sh->i_delta_poc[1] = 0;
133
sh->i_redundant_pic_cnt = 0;
135
if( !h->mb.b_direct_auto_read )
137
if( h->mb.b_direct_auto_write )
138
sh->b_direct_spatial_mv_pred = ( h->stat.i_direct_score[1] > h->stat.i_direct_score[0] );
140
sh->b_direct_spatial_mv_pred = ( param->analyse.i_direct_mv_pred == X264_DIRECT_PRED_SPATIAL );
142
/* else b_direct_spatial_mv_pred was read from the 2pass statsfile */
144
sh->b_num_ref_idx_override = 0;
145
sh->i_num_ref_idx_l0_active = 1;
146
sh->i_num_ref_idx_l1_active = 1;
148
sh->b_ref_pic_list_reordering_l0 = h->b_ref_reorder[0];
149
sh->b_ref_pic_list_reordering_l1 = h->b_ref_reorder[1];
151
/* If the ref list isn't in the default order, construct reordering header */
152
/* List1 reordering isn't needed yet */
153
if( sh->b_ref_pic_list_reordering_l0 )
155
int pred_frame_num = i_frame;
156
for( i = 0; i < h->i_ref0; i++ )
158
int diff = h->fref0[i]->i_frame_num - pred_frame_num;
160
x264_log( h, X264_LOG_ERROR, "diff frame num == 0\n" );
161
sh->ref_pic_list_order[0][i].idc = ( diff > 0 );
162
sh->ref_pic_list_order[0][i].arg = abs( diff ) - 1;
163
pred_frame_num = h->fref0[i]->i_frame_num;
167
sh->i_cabac_init_idc = param->i_cabac_init_idc;
170
sh->i_qp_delta = i_qp - pps->i_pic_init_qp;
171
sh->b_sp_for_swidth = 0;
174
/* If effective qp <= 15, deblocking would have no effect anyway */
175
if( param->b_deblocking_filter
176
&& ( h->mb.b_variable_qp
177
|| 15 < i_qp + 2 * X264_MAX(param->i_deblocking_filter_alphac0, param->i_deblocking_filter_beta) ) )
179
sh->i_disable_deblocking_filter_idc = 0;
183
sh->i_disable_deblocking_filter_idc = 1;
185
sh->i_alpha_c0_offset = param->i_deblocking_filter_alphac0 << 1;
186
sh->i_beta_offset = param->i_deblocking_filter_beta << 1;
189
static void x264_slice_header_write( bs_t *s, x264_slice_header_t *sh, int i_nal_ref_idc )
195
assert( sh->i_first_mb % (2*sh->sps->i_mb_width) == 0 );
196
bs_write_ue( s, sh->i_first_mb >> 1 );
199
bs_write_ue( s, sh->i_first_mb );
201
bs_write_ue( s, sh->i_type + 5 ); /* same type things */
202
bs_write_ue( s, sh->i_pps_id );
203
bs_write( s, sh->sps->i_log2_max_frame_num, sh->i_frame_num );
205
if( !sh->sps->b_frame_mbs_only )
207
bs_write1( s, sh->b_field_pic );
208
if ( sh->b_field_pic )
209
bs_write1( s, sh->b_bottom_field );
212
if( sh->i_idr_pic_id >= 0 ) /* NAL IDR */
214
bs_write_ue( s, sh->i_idr_pic_id );
217
if( sh->sps->i_poc_type == 0 )
219
bs_write( s, sh->sps->i_log2_max_poc_lsb, sh->i_poc_lsb );
220
if( sh->pps->b_pic_order && !sh->b_field_pic )
222
bs_write_se( s, sh->i_delta_poc_bottom );
225
else if( sh->sps->i_poc_type == 1 && !sh->sps->b_delta_pic_order_always_zero )
227
bs_write_se( s, sh->i_delta_poc[0] );
228
if( sh->pps->b_pic_order && !sh->b_field_pic )
230
bs_write_se( s, sh->i_delta_poc[1] );
234
if( sh->pps->b_redundant_pic_cnt )
236
bs_write_ue( s, sh->i_redundant_pic_cnt );
239
if( sh->i_type == SLICE_TYPE_B )
241
bs_write1( s, sh->b_direct_spatial_mv_pred );
243
if( sh->i_type == SLICE_TYPE_P || sh->i_type == SLICE_TYPE_SP || sh->i_type == SLICE_TYPE_B )
245
bs_write1( s, sh->b_num_ref_idx_override );
246
if( sh->b_num_ref_idx_override )
248
bs_write_ue( s, sh->i_num_ref_idx_l0_active - 1 );
249
if( sh->i_type == SLICE_TYPE_B )
251
bs_write_ue( s, sh->i_num_ref_idx_l1_active - 1 );
256
/* ref pic list reordering */
257
if( sh->i_type != SLICE_TYPE_I )
259
bs_write1( s, sh->b_ref_pic_list_reordering_l0 );
260
if( sh->b_ref_pic_list_reordering_l0 )
262
for( i = 0; i < sh->i_num_ref_idx_l0_active; i++ )
264
bs_write_ue( s, sh->ref_pic_list_order[0][i].idc );
265
bs_write_ue( s, sh->ref_pic_list_order[0][i].arg );
271
if( sh->i_type == SLICE_TYPE_B )
273
bs_write1( s, sh->b_ref_pic_list_reordering_l1 );
274
if( sh->b_ref_pic_list_reordering_l1 )
276
for( i = 0; i < sh->i_num_ref_idx_l1_active; i++ )
278
bs_write_ue( s, sh->ref_pic_list_order[1][i].idc );
279
bs_write_ue( s, sh->ref_pic_list_order[1][i].arg );
285
if( ( sh->pps->b_weighted_pred && ( sh->i_type == SLICE_TYPE_P || sh->i_type == SLICE_TYPE_SP ) ) ||
286
( sh->pps->b_weighted_bipred == 1 && sh->i_type == SLICE_TYPE_B ) )
291
if( i_nal_ref_idc != 0 )
293
if( sh->i_idr_pic_id >= 0 )
295
bs_write1( s, 0 ); /* no output of prior pics flag */
296
bs_write1( s, 0 ); /* long term reference flag */
300
bs_write1( s, 0 ); /* adaptive_ref_pic_marking_mode_flag */
304
if( sh->pps->b_cabac && sh->i_type != SLICE_TYPE_I )
306
bs_write_ue( s, sh->i_cabac_init_idc );
308
bs_write_se( s, sh->i_qp_delta ); /* slice qp delta */
310
if( sh->pps->b_deblocking_filter_control )
312
bs_write_ue( s, sh->i_disable_deblocking_filter_idc );
313
if( sh->i_disable_deblocking_filter_idc != 1 )
315
bs_write_se( s, sh->i_alpha_c0_offset >> 1 );
316
bs_write_se( s, sh->i_beta_offset >> 1 );
321
/****************************************************************************
323
****************************************************************************
324
****************************** External API*********************************
325
****************************************************************************
327
****************************************************************************/
329
static int x264_validate_parameters( x264_t *h )
331
if( h->param.i_width <= 0 || h->param.i_height <= 0 )
333
x264_log( h, X264_LOG_ERROR, "invalid width x height (%dx%d)\n",
334
h->param.i_width, h->param.i_height );
338
if( h->param.i_width % 2 || h->param.i_height % 2 )
340
x264_log( h, X264_LOG_ERROR, "width or height not divisible by 2 (%dx%d)\n",
341
h->param.i_width, h->param.i_height );
344
if( h->param.i_csp != X264_CSP_I420 )
346
x264_log( h, X264_LOG_ERROR, "invalid CSP (only I420 supported)\n" );
350
if( h->param.i_threads == 0 )
351
h->param.i_threads = x264_cpu_num_processors() * 3/2;
352
h->param.i_threads = x264_clip3( h->param.i_threads, 1, X264_THREAD_MAX );
353
if( h->param.i_threads > 1 )
356
x264_log( h, X264_LOG_WARNING, "not compiled with pthread support!\n");
357
h->param.i_threads = 1;
359
if( h->param.i_scenecut_threshold >= 0 )
360
h->param.b_pre_scenecut = 1;
364
if( h->param.b_interlaced )
366
if( h->param.analyse.i_me_method >= X264_ME_ESA )
368
x264_log( h, X264_LOG_WARNING, "interlace + me=esa is not implemented\n" );
369
h->param.analyse.i_me_method = X264_ME_UMH;
371
if( h->param.analyse.i_direct_mv_pred > X264_DIRECT_PRED_SPATIAL )
373
x264_log( h, X264_LOG_WARNING, "interlace + direct=temporal is not implemented\n" );
374
h->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
378
if( h->param.rc.i_rc_method < 0 || h->param.rc.i_rc_method > 2 )
380
x264_log( h, X264_LOG_ERROR, "no ratecontrol method specified\n" );
383
h->param.rc.f_rf_constant = x264_clip3f( h->param.rc.f_rf_constant, 0, 51 );
384
h->param.rc.i_qp_constant = x264_clip3( h->param.rc.i_qp_constant, 0, 51 );
385
if( h->param.rc.i_rc_method == X264_RC_CRF )
386
h->param.rc.i_qp_constant = h->param.rc.f_rf_constant;
387
if( (h->param.rc.i_rc_method == X264_RC_CQP || h->param.rc.i_rc_method == X264_RC_CRF)
388
&& h->param.rc.i_qp_constant == 0 )
390
h->mb.b_lossless = 1;
391
h->param.i_cqm_preset = X264_CQM_FLAT;
392
h->param.psz_cqm_file = NULL;
393
h->param.rc.i_rc_method = X264_RC_CQP;
394
h->param.rc.f_ip_factor = 1;
395
h->param.rc.f_pb_factor = 1;
396
h->param.analyse.b_transform_8x8 = 0;
397
h->param.analyse.b_psnr = 0;
398
h->param.analyse.b_ssim = 0;
399
h->param.analyse.i_chroma_qp_offset = 0;
400
h->param.analyse.i_trellis = 0;
401
h->param.analyse.b_fast_pskip = 0;
402
h->param.analyse.i_noise_reduction = 0;
403
h->param.analyse.i_subpel_refine = x264_clip3( h->param.analyse.i_subpel_refine, 1, 6 );
405
if( h->param.rc.i_rc_method == X264_RC_CQP )
407
float qp_p = h->param.rc.i_qp_constant;
408
float qp_i = qp_p - 6*log(h->param.rc.f_ip_factor)/log(2);
409
float qp_b = qp_p + 6*log(h->param.rc.f_pb_factor)/log(2);
410
h->param.rc.i_qp_min = x264_clip3( (int)(X264_MIN3( qp_p, qp_i, qp_b )), 0, 51 );
411
h->param.rc.i_qp_max = x264_clip3( (int)(X264_MAX3( qp_p, qp_i, qp_b ) + .999), 0, 51 );
414
if( ( h->param.i_width % 16 || h->param.i_height % 16 )
415
&& h->param.i_height != 1080 && !h->mb.b_lossless )
417
// There's nothing special about 1080 in that the warning still applies to it,
418
// but chances are the user can't help it if his content is already 1080p,
419
// so there's no point in warning in that case.
420
x264_log( h, X264_LOG_WARNING,
421
"width or height not divisible by 16 (%dx%d), compression will suffer.\n",
422
h->param.i_width, h->param.i_height );
425
h->param.i_frame_reference = x264_clip3( h->param.i_frame_reference, 1, 16 );
426
if( h->param.i_keyint_max <= 0 )
427
h->param.i_keyint_max = 1;
428
h->param.i_keyint_min = x264_clip3( h->param.i_keyint_min, 1, h->param.i_keyint_max/2+1 );
430
h->param.i_bframe = x264_clip3( h->param.i_bframe, 0, X264_BFRAME_MAX );
431
h->param.i_bframe_bias = x264_clip3( h->param.i_bframe_bias, -90, 100 );
432
h->param.b_bframe_pyramid = h->param.b_bframe_pyramid && h->param.i_bframe > 1;
433
h->param.b_bframe_adaptive = h->param.b_bframe_adaptive && h->param.i_bframe > 0;
434
h->param.analyse.b_weighted_bipred = h->param.analyse.b_weighted_bipred && h->param.i_bframe > 0;
435
h->mb.b_direct_auto_write = h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_AUTO
437
&& ( h->param.rc.b_stat_write || !h->param.rc.b_stat_read );
438
if( h->param.i_scenecut_threshold < 0 )
439
h->param.b_pre_scenecut = 0;
441
h->param.i_deblocking_filter_alphac0 = x264_clip3( h->param.i_deblocking_filter_alphac0, -6, 6 );
442
h->param.i_deblocking_filter_beta = x264_clip3( h->param.i_deblocking_filter_beta, -6, 6 );
443
h->param.analyse.i_luma_deadzone[0] = x264_clip3( h->param.analyse.i_luma_deadzone[0], 0, 32 );
444
h->param.analyse.i_luma_deadzone[1] = x264_clip3( h->param.analyse.i_luma_deadzone[1], 0, 32 );
446
h->param.i_cabac_init_idc = x264_clip3( h->param.i_cabac_init_idc, 0, 2 );
448
if( h->param.i_cqm_preset < X264_CQM_FLAT || h->param.i_cqm_preset > X264_CQM_CUSTOM )
449
h->param.i_cqm_preset = X264_CQM_FLAT;
451
if( h->param.analyse.i_me_method < X264_ME_DIA ||
452
h->param.analyse.i_me_method > X264_ME_TESA )
453
h->param.analyse.i_me_method = X264_ME_HEX;
454
if( h->param.analyse.i_me_range < 4 )
455
h->param.analyse.i_me_range = 4;
456
if( h->param.analyse.i_me_range > 16 && h->param.analyse.i_me_method <= X264_ME_HEX )
457
h->param.analyse.i_me_range = 16;
458
if( h->param.analyse.i_me_method == X264_ME_TESA &&
459
(h->mb.b_lossless || h->param.analyse.i_subpel_refine <= 1) )
460
h->param.analyse.i_me_method = X264_ME_ESA;
461
h->param.analyse.i_subpel_refine = x264_clip3( h->param.analyse.i_subpel_refine, 1, 7 );
462
h->param.analyse.b_bframe_rdo = h->param.analyse.b_bframe_rdo && h->param.analyse.i_subpel_refine >= 6;
463
h->param.analyse.b_mixed_references = h->param.analyse.b_mixed_references && h->param.i_frame_reference > 1;
464
h->param.analyse.inter &= X264_ANALYSE_PSUB16x16|X264_ANALYSE_PSUB8x8|X264_ANALYSE_BSUB16x16|
465
X264_ANALYSE_I4x4|X264_ANALYSE_I8x8;
466
h->param.analyse.intra &= X264_ANALYSE_I4x4|X264_ANALYSE_I8x8;
467
if( !(h->param.analyse.inter & X264_ANALYSE_PSUB16x16) )
468
h->param.analyse.inter &= ~X264_ANALYSE_PSUB8x8;
469
if( !h->param.analyse.b_transform_8x8 )
471
h->param.analyse.inter &= ~X264_ANALYSE_I8x8;
472
h->param.analyse.intra &= ~X264_ANALYSE_I8x8;
474
h->param.analyse.i_chroma_qp_offset = x264_clip3(h->param.analyse.i_chroma_qp_offset, -12, 12);
475
if( !h->param.b_cabac )
476
h->param.analyse.i_trellis = 0;
477
h->param.analyse.i_trellis = x264_clip3( h->param.analyse.i_trellis, 0, 2 );
478
h->param.analyse.i_noise_reduction = x264_clip3( h->param.analyse.i_noise_reduction, 0, 1<<16 );
481
const x264_level_t *l = x264_levels;
482
while( l->level_idc != 0 && l->level_idc != h->param.i_level_idc )
484
if( l->level_idc == 0 )
486
x264_log( h, X264_LOG_ERROR, "invalid level_idc: %d\n", h->param.i_level_idc );
489
if( h->param.analyse.i_mv_range <= 0 )
490
h->param.analyse.i_mv_range = l->mv_range;
492
h->param.analyse.i_mv_range = x264_clip3(h->param.analyse.i_mv_range, 32, 512);
493
if( h->param.analyse.i_direct_8x8_inference < 0 )
494
h->param.analyse.i_direct_8x8_inference = l->direct8x8;
497
if( h->param.i_threads > 1 )
499
int r = h->param.analyse.i_mv_range_thread;
503
// half of the available space is reserved and divided evenly among the threads,
504
// the rest is allocated to whichever thread is far enough ahead to use it.
505
// reserving more space increases quality for some videos, but costs more time
506
// in thread synchronization.
507
int max_range = (h->param.i_height + X264_THREAD_HEIGHT) / h->param.i_threads - X264_THREAD_HEIGHT;
510
r = X264_MAX( r, h->param.analyse.i_me_range );
511
r = X264_MIN( r, h->param.analyse.i_mv_range );
512
// round up to use the whole mb row
513
r2 = (r & ~15) + ((-X264_THREAD_HEIGHT) & 15);
516
x264_log( h, X264_LOG_DEBUG, "using mv_range_thread = %d\n", r2 );
517
h->param.analyse.i_mv_range_thread = r2;
520
if( h->param.rc.f_qblur < 0 )
521
h->param.rc.f_qblur = 0;
522
if( h->param.rc.f_complexity_blur < 0 )
523
h->param.rc.f_complexity_blur = 0;
525
h->param.i_sps_id &= 31;
527
if( h->param.i_log_level < X264_LOG_INFO )
529
h->param.analyse.b_psnr = 0;
530
h->param.analyse.b_ssim = 0;
533
/* ensure the booleans are 0 or 1 so they can be used in math */
534
#define BOOLIFY(x) h->param.x = !!h->param.x
536
BOOLIFY( b_deblocking_filter );
537
BOOLIFY( b_interlaced );
538
BOOLIFY( analyse.b_transform_8x8 );
539
BOOLIFY( analyse.i_direct_8x8_inference );
540
BOOLIFY( analyse.b_bidir_me );
541
BOOLIFY( analyse.b_chroma_me );
542
BOOLIFY( analyse.b_fast_pskip );
543
BOOLIFY( rc.b_stat_write );
544
BOOLIFY( rc.b_stat_read );
550
static void mbcmp_init( x264_t *h )
552
int satd = !h->mb.b_lossless && h->param.analyse.i_subpel_refine > 1;
553
memcpy( h->pixf.mbcmp, satd ? h->pixf.satd : h->pixf.sad, sizeof(h->pixf.mbcmp) );
554
satd &= h->param.analyse.i_me_method == X264_ME_TESA;
555
memcpy( h->pixf.fpelcmp, satd ? h->pixf.satd : h->pixf.sad, sizeof(h->pixf.fpelcmp) );
556
memcpy( h->pixf.fpelcmp_x3, satd ? h->pixf.satd_x3 : h->pixf.sad_x3, sizeof(h->pixf.fpelcmp_x3) );
557
memcpy( h->pixf.fpelcmp_x4, satd ? h->pixf.satd_x4 : h->pixf.sad_x4, sizeof(h->pixf.fpelcmp_x4) );
560
/****************************************************************************
562
****************************************************************************/
563
x264_t *x264_encoder_open ( x264_param_t *param )
565
x264_t *h = x264_malloc( sizeof( x264_t ) );
568
memset( h, 0, sizeof( x264_t ) );
570
/* Create a copy of param */
571
memcpy( &h->param, param, sizeof( x264_param_t ) );
573
if( x264_validate_parameters( h ) < 0 )
579
if( h->param.psz_cqm_file )
580
if( x264_cqm_parse_file( h, h->param.psz_cqm_file ) < 0 )
586
if( h->param.rc.psz_stat_out )
587
h->param.rc.psz_stat_out = strdup( h->param.rc.psz_stat_out );
588
if( h->param.rc.psz_stat_in )
589
h->param.rc.psz_stat_in = strdup( h->param.rc.psz_stat_in );
590
if( h->param.rc.psz_rc_eq )
591
h->param.rc.psz_rc_eq = strdup( h->param.rc.psz_rc_eq );
594
if( h->param.vui.i_sar_width > 0 && h->param.vui.i_sar_height > 0 )
596
int i_w = param->vui.i_sar_width;
597
int i_h = param->vui.i_sar_height;
599
x264_reduce_fraction( &i_w, &i_h );
601
while( i_w > 65535 || i_h > 65535 )
607
h->param.vui.i_sar_width = 0;
608
h->param.vui.i_sar_height = 0;
609
if( i_w == 0 || i_h == 0 )
611
x264_log( h, X264_LOG_WARNING, "cannot create valid sample aspect ratio\n" );
615
x264_log( h, X264_LOG_INFO, "using SAR=%d/%d\n", i_w, i_h );
616
h->param.vui.i_sar_width = i_w;
617
h->param.vui.i_sar_height = i_h;
621
x264_reduce_fraction( &h->param.i_fps_num, &h->param.i_fps_den );
628
h->sps = &h->sps_array[0];
629
x264_sps_init( h->sps, h->param.i_sps_id, &h->param );
631
h->pps = &h->pps_array[0];
632
x264_pps_init( h->pps, h->param.i_sps_id, &h->param, h->sps);
634
x264_validate_levels( h );
636
if( x264_cqm_init( h ) < 0 )
642
h->mb.i_mb_count = h->sps->i_mb_width * h->sps->i_mb_height;
645
h->frames.i_delay = h->param.i_bframe + h->param.i_threads - 1;
646
h->frames.i_max_ref0 = h->param.i_frame_reference;
647
h->frames.i_max_ref1 = h->sps->vui.i_num_reorder_frames;
648
h->frames.i_max_dpb = h->sps->vui.i_max_dec_frame_buffering;
649
h->frames.b_have_lowres = !h->param.rc.b_stat_read
650
&& ( h->param.rc.i_rc_method == X264_RC_ABR
651
|| h->param.rc.i_rc_method == X264_RC_CRF
652
|| h->param.b_bframe_adaptive
653
|| h->param.b_pre_scenecut );
655
h->frames.i_last_idr = - h->param.i_keyint_max;
656
h->frames.i_input = 0;
657
h->frames.last_nonb = NULL;
664
/* init CPU functions */
665
x264_predict_16x16_init( h->param.cpu, h->predict_16x16 );
666
x264_predict_8x8c_init( h->param.cpu, h->predict_8x8c );
667
x264_predict_8x8_init( h->param.cpu, h->predict_8x8 );
668
x264_predict_4x4_init( h->param.cpu, h->predict_4x4 );
670
x264_pixel_init( h->param.cpu, &h->pixf );
671
x264_dct_init( h->param.cpu, &h->dctf );
672
x264_zigzag_init( h->param.cpu, &h->zigzagf, h->param.b_interlaced );
673
x264_mc_init( h->param.cpu, &h->mc );
674
x264_quant_init( h, h->param.cpu, &h->quantf );
675
x264_deblock_init( h->param.cpu, &h->loopf );
676
x264_dct_init_weights();
680
x264_log( h, X264_LOG_INFO, "using cpu capabilities: %s%s%s%s%s%s%s%s%s%s\n",
681
param->cpu&X264_CPU_MMX ? "MMX " : "",
682
param->cpu&X264_CPU_MMXEXT ? "MMXEXT " : "",
683
param->cpu&X264_CPU_SSE ? "SSE " : "",
684
param->cpu&X264_CPU_SSE2 ? "SSE2 " : "",
685
param->cpu&X264_CPU_SSE3 ? "SSE3 " : "",
686
param->cpu&X264_CPU_SSSE3 ? "SSSE3 " : "",
687
param->cpu&X264_CPU_3DNOW ? "3DNow! " : "",
688
param->cpu&X264_CPU_ALTIVEC ? "Altivec " : "",
689
param->cpu&X264_CPU_CACHELINE_SPLIT ?
690
param->cpu&X264_CPU_CACHELINE_32 ? "Cache32 " :
691
param->cpu&X264_CPU_CACHELINE_64 ? "Cache64 " : "Cache? " : "",
692
param->cpu ? "" : "none!" );
695
h->out.i_bitstream = X264_MAX( 1000000, h->param.i_width * h->param.i_height * 4
696
* ( h->param.rc.i_rc_method == X264_RC_ABR ? pow( 0.95, h->param.rc.i_qp_min )
697
: pow( 0.95, h->param.rc.i_qp_constant ) * X264_MAX( 1, h->param.rc.f_ip_factor )));
701
for( i = 1; i < h->param.i_threads; i++ )
702
h->thread[i] = x264_malloc( sizeof(x264_t) );
704
for( i = 0; i < h->param.i_threads; i++ )
708
h->thread[i]->fdec = x264_frame_pop_unused( h );
709
h->thread[i]->out.p_bitstream = x264_malloc( h->out.i_bitstream );
710
if( x264_macroblock_cache_init( h->thread[i] ) < 0 )
714
if( x264_ratecontrol_new( h ) < 0 )
717
#ifdef DEBUG_DUMP_FRAME
719
/* create or truncate the reconstructed video file */
720
FILE *f = fopen( "fdec.yuv", "w" );
725
x264_log( h, X264_LOG_ERROR, "can't write to fdec.yuv\n" );
735
/****************************************************************************
736
* x264_encoder_reconfig:
737
****************************************************************************/
738
int x264_encoder_reconfig( x264_t *h, x264_param_t *param )
740
#define COPY(var) h->param.var = param->var
741
COPY( i_frame_reference ); // but never uses more refs than initially specified
742
COPY( i_bframe_bias );
743
if( h->param.i_scenecut_threshold >= 0 && param->i_scenecut_threshold >= 0 )
744
COPY( i_scenecut_threshold ); // can't turn it on or off, only vary the threshold
745
COPY( b_deblocking_filter );
746
COPY( i_deblocking_filter_alphac0 );
747
COPY( i_deblocking_filter_beta );
748
COPY( analyse.intra );
749
COPY( analyse.inter );
750
COPY( analyse.i_direct_mv_pred );
751
COPY( analyse.i_me_method );
752
COPY( analyse.i_me_range );
753
COPY( analyse.i_noise_reduction );
754
COPY( analyse.i_subpel_refine );
755
COPY( analyse.i_trellis );
756
COPY( analyse.b_bidir_me );
757
COPY( analyse.b_bframe_rdo );
758
COPY( analyse.b_chroma_me );
759
COPY( analyse.b_dct_decimate );
760
COPY( analyse.b_fast_pskip );
761
COPY( analyse.b_mixed_references );
762
// can only twiddle these if they were enabled to begin with:
763
if( h->pps->b_transform_8x8_mode )
764
COPY( analyse.b_transform_8x8 );
765
if( h->frames.i_max_ref1 > 1 )
766
COPY( b_bframe_pyramid );
771
return x264_validate_parameters( h );
775
static void x264_nal_start( x264_t *h, int i_type, int i_ref_idc )
777
x264_nal_t *nal = &h->out.nal[h->out.i_nal];
779
nal->i_ref_idc = i_ref_idc;
780
nal->i_type = i_type;
783
nal->p_payload= &h->out.p_bitstream[bs_pos( &h->out.bs ) / 8];
785
static void x264_nal_end( x264_t *h )
787
x264_nal_t *nal = &h->out.nal[h->out.i_nal];
788
nal->i_payload = &h->out.p_bitstream[bs_pos( &h->out.bs ) / 8] - nal->p_payload;
792
/****************************************************************************
793
* x264_encoder_headers:
794
****************************************************************************/
795
int x264_encoder_headers( x264_t *h, x264_nal_t **pp_nal, int *pi_nal )
797
/* init bitstream context */
799
bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream );
801
/* Put SPS and PPS */
802
if( h->i_frame == 0 )
804
/* identify ourself */
805
x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE );
806
x264_sei_version_write( h, &h->out.bs );
809
/* generate sequence parameters */
810
x264_nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST );
811
x264_sps_write( &h->out.bs, h->sps );
814
/* generate picture parameters */
815
x264_nal_start( h, NAL_PPS, NAL_PRIORITY_HIGHEST );
816
x264_pps_write( &h->out.bs, h->pps );
820
*pi_nal = h->out.i_nal;
821
*pp_nal = &h->out.nal[0];
827
static inline void x264_reference_build_list( x264_t *h, int i_poc )
832
/* build ref list 0/1 */
835
for( i = 0; h->frames.reference[i]; i++ )
837
if( h->frames.reference[i]->i_poc < i_poc )
839
h->fref0[h->i_ref0++] = h->frames.reference[i];
841
else if( h->frames.reference[i]->i_poc > i_poc )
843
h->fref1[h->i_ref1++] = h->frames.reference[i];
847
/* Order ref0 from higher to lower poc */
851
for( i = 0; i < h->i_ref0 - 1; i++ )
853
if( h->fref0[i]->i_poc < h->fref0[i+1]->i_poc )
855
XCHG( x264_frame_t*, h->fref0[i], h->fref0[i+1] );
861
/* Order ref1 from lower to higher poc (bubble sort) for B-frame */
865
for( i = 0; i < h->i_ref1 - 1; i++ )
867
if( h->fref1[i]->i_poc > h->fref1[i+1]->i_poc )
869
XCHG( x264_frame_t*, h->fref1[i], h->fref1[i+1] );
876
/* In the standard, a P-frame's ref list is sorted by frame_num.
877
* We use POC, but check whether explicit reordering is needed */
878
h->b_ref_reorder[0] =
879
h->b_ref_reorder[1] = 0;
880
if( h->sh.i_type == SLICE_TYPE_P )
882
for( i = 0; i < h->i_ref0 - 1; i++ )
883
if( h->fref0[i]->i_frame_num < h->fref0[i+1]->i_frame_num )
885
h->b_ref_reorder[0] = 1;
890
h->i_ref1 = X264_MIN( h->i_ref1, h->frames.i_max_ref1 );
891
h->i_ref0 = X264_MIN( h->i_ref0, h->frames.i_max_ref0 );
892
h->i_ref0 = X264_MIN( h->i_ref0, h->param.i_frame_reference ); // if reconfig() has lowered the limit
893
assert( h->i_ref0 + h->i_ref1 <= 16 );
894
h->mb.pic.i_fref[0] = h->i_ref0;
895
h->mb.pic.i_fref[1] = h->i_ref1;
898
static void x264_fdec_filter_row( x264_t *h, int mb_y )
900
/* mb_y is the mb to be encoded next, not the mb to be filtered here */
901
int b_hpel = h->fdec->b_kept_as_ref;
902
int b_deblock = !h->sh.i_disable_deblocking_filter_idc;
903
int b_end = mb_y == h->sps->i_mb_height;
904
int min_y = mb_y - (1 << h->sh.b_mbaff);
905
#ifndef DEBUG_DUMP_FRAME
908
if( mb_y & h->sh.b_mbaff )
916
for( j=0; j<=h->sh.b_mbaff; j++ )
919
memcpy( h->mb.intra_border_backup[j][i],
920
h->fdec->plane[i] + ((mb_y*16 >> !!i) + j - 1 - h->sh.b_mbaff) * h->fdec->i_stride[i],
921
h->sps->i_mb_width*16 >> !!i );
927
int max_y = b_end ? h->sps->i_mb_height : mb_y;
929
for( y = min_y; y < max_y; y += (1 << h->sh.b_mbaff) )
930
x264_frame_deblock_row( h, y );
935
x264_frame_expand_border( h, h->fdec, min_y, b_end );
936
x264_frame_filter( h, h->fdec, min_y, b_end );
937
x264_frame_expand_border_filtered( h, h->fdec, min_y, b_end );
940
if( h->param.i_threads > 1 && h->fdec->b_kept_as_ref )
942
x264_frame_cond_broadcast( h->fdec, mb_y*16 + (b_end ? 10000 : -(X264_THREAD_HEIGHT << h->sh.b_mbaff)) );
946
static inline void x264_reference_update( x264_t *h )
950
if( h->fdec->i_frame >= 0 )
953
if( !h->fdec->b_kept_as_ref )
955
if( h->param.i_threads > 1 )
957
x264_frame_push_unused( h, h->fdec );
958
h->fdec = x264_frame_pop_unused( h );
963
/* move lowres copy of the image to the ref frame */
964
for( i = 0; i < 4; i++)
966
XCHG( uint8_t*, h->fdec->lowres[i], h->fenc->lowres[i] );
967
XCHG( uint8_t*, h->fdec->buffer_lowres[i], h->fenc->buffer_lowres[i] );
970
/* adaptive B decision needs a pointer, since it can't use the ref lists */
971
if( h->sh.i_type != SLICE_TYPE_B )
972
h->frames.last_nonb = h->fdec;
974
/* move frame in the buffer */
975
x264_frame_push( h->frames.reference, h->fdec );
976
if( h->frames.reference[h->frames.i_max_dpb] )
977
x264_frame_push_unused( h, x264_frame_shift( h->frames.reference ) );
978
h->fdec = x264_frame_pop_unused( h );
981
static inline void x264_reference_reset( x264_t *h )
983
while( h->frames.reference[0] )
984
x264_frame_push_unused( h, x264_frame_pop( h->frames.reference ) );
989
static inline void x264_slice_init( x264_t *h, int i_nal_type, int i_global_qp )
991
/* ------------------------ Create slice header ----------------------- */
992
if( i_nal_type == NAL_SLICE_IDR )
994
x264_slice_header_init( h, &h->sh, h->sps, h->pps, h->i_idr_pic_id, h->i_frame_num, i_global_qp );
997
h->i_idr_pic_id = ( h->i_idr_pic_id + 1 ) % 65536;
1001
x264_slice_header_init( h, &h->sh, h->sps, h->pps, -1, h->i_frame_num, i_global_qp );
1003
/* always set the real higher num of ref frame used */
1004
h->sh.b_num_ref_idx_override = 1;
1005
h->sh.i_num_ref_idx_l0_active = h->i_ref0 <= 0 ? 1 : h->i_ref0;
1006
h->sh.i_num_ref_idx_l1_active = h->i_ref1 <= 0 ? 1 : h->i_ref1;
1009
h->fdec->i_frame_num = h->sh.i_frame_num;
1011
if( h->sps->i_poc_type == 0 )
1013
h->sh.i_poc_lsb = h->fdec->i_poc & ( (1 << h->sps->i_log2_max_poc_lsb) - 1 );
1014
h->sh.i_delta_poc_bottom = 0; /* XXX won't work for field */
1016
else if( h->sps->i_poc_type == 1 )
1018
/* FIXME TODO FIXME */
1022
/* Nothing to do ? */
1025
x264_macroblock_slice_init( h );
1028
static void x264_slice_write( x264_t *h )
1035
memset( &h->stat.frame, 0, sizeof(h->stat.frame) );
1038
x264_nal_start( h, h->i_nal_type, h->i_nal_ref_idc );
1041
x264_slice_header_write( &h->out.bs, &h->sh, h->i_nal_ref_idc );
1042
if( h->param.b_cabac )
1044
/* alignment needed */
1045
bs_align_1( &h->out.bs );
1048
x264_cabac_context_init( &h->cabac, h->sh.i_type, h->sh.i_qp, h->sh.i_cabac_init_idc );
1049
x264_cabac_encode_init ( &h->cabac, h->out.bs.p, h->out.bs.p_end );
1051
h->mb.i_last_qp = h->sh.i_qp;
1052
h->mb.i_last_dqp = 0;
1054
for( mb_xy = h->sh.i_first_mb, i_skip = 0; mb_xy < h->sh.i_last_mb; )
1056
const int i_mb_y = mb_xy / h->sps->i_mb_width;
1057
const int i_mb_x = mb_xy % h->sps->i_mb_width;
1058
int mb_spos = bs_pos(&h->out.bs) + x264_cabac_pos(&h->cabac);
1061
x264_fdec_filter_row( h, i_mb_y );
1064
x264_macroblock_cache_load( h, i_mb_x, i_mb_y );
1066
/* analyse parameters
1067
* Slice I: choose I_4x4 or I_16x16 mode
1068
* Slice P: choose between using P mode or intra (4x4 or 16x16)
1070
TIMER_START( i_mtime_analyse );
1071
x264_macroblock_analyse( h );
1072
TIMER_STOP( i_mtime_analyse );
1074
/* encode this macroblock -> be careful it can change the mb type to P_SKIP if needed */
1075
TIMER_START( i_mtime_encode );
1076
x264_macroblock_encode( h );
1077
TIMER_STOP( i_mtime_encode );
1079
TIMER_START( i_mtime_write );
1080
if( h->param.b_cabac )
1082
if( mb_xy > h->sh.i_first_mb && !(h->sh.b_mbaff && (i_mb_y&1)) )
1083
x264_cabac_encode_terminal( &h->cabac );
1085
if( IS_SKIP( h->mb.i_type ) )
1086
x264_cabac_mb_skip( h, 1 );
1089
if( h->sh.i_type != SLICE_TYPE_I )
1090
x264_cabac_mb_skip( h, 0 );
1091
x264_macroblock_write_cabac( h, &h->cabac );
1096
if( IS_SKIP( h->mb.i_type ) )
1100
if( h->sh.i_type != SLICE_TYPE_I )
1102
bs_write_ue( &h->out.bs, i_skip ); /* skip run */
1105
x264_macroblock_write_cavlc( h, &h->out.bs );
1108
TIMER_STOP( i_mtime_write );
1111
if( h->param.b_visualize )
1112
x264_visualize_mb( h );
1116
x264_macroblock_cache_save( h );
1118
/* accumulate mb stats */
1119
h->stat.frame.i_mb_count[h->mb.i_type]++;
1120
if( !IS_SKIP(h->mb.i_type) && !IS_INTRA(h->mb.i_type) && !IS_DIRECT(h->mb.i_type) )
1122
if( h->mb.i_partition != D_8x8 )
1123
h->stat.frame.i_mb_count_size[ x264_mb_partition_pixel_table[ h->mb.i_partition ] ] += 4;
1125
for( i = 0; i < 4; i++ )
1126
h->stat.frame.i_mb_count_size[ x264_mb_partition_pixel_table[ h->mb.i_sub_partition[i] ] ] ++;
1127
if( h->param.i_frame_reference > 1 )
1129
for( i = 0; i < 4; i++ )
1131
int i_ref = h->mb.cache.ref[0][ x264_scan8[4*i] ];
1133
h->stat.frame.i_mb_count_ref[i_ref] ++;
1137
if( h->mb.i_cbp_luma && !IS_INTRA(h->mb.i_type) )
1139
h->stat.frame.i_mb_count_8x8dct[0] ++;
1140
h->stat.frame.i_mb_count_8x8dct[1] += h->mb.b_transform_8x8;
1143
x264_ratecontrol_mb( h, bs_pos(&h->out.bs) + x264_cabac_pos(&h->cabac) - mb_spos );
1147
if( (i_mb_y&1) && i_mb_x == h->sps->i_mb_width - 1 )
1150
mb_xy += 1 - h->sps->i_mb_width;
1152
mb_xy += h->sps->i_mb_width;
1158
if( h->param.b_cabac )
1160
x264_cabac_encode_flush( h, &h->cabac );
1161
h->out.bs.p = h->cabac.p;
1166
bs_write_ue( &h->out.bs, i_skip ); /* last skip run */
1167
/* rbsp_slice_trailing_bits */
1168
bs_rbsp_trailing( &h->out.bs );
1173
/* Compute misc bits */
1174
h->stat.frame.i_misc_bits = bs_pos( &h->out.bs )
1176
- h->stat.frame.i_itex_bits
1177
- h->stat.frame.i_ptex_bits
1178
- h->stat.frame.i_hdr_bits;
1181
static void x264_thread_sync_context( x264_t *dst, x264_t *src )
1187
// reference counting
1188
for( f = src->frames.reference; *f; f++ )
1189
(*f)->i_reference_count++;
1190
for( f = dst->frames.reference; *f; f++ )
1191
x264_frame_push_unused( src, *f );
1192
src->fdec->i_reference_count++;
1193
x264_frame_push_unused( src, dst->fdec );
1195
// copy everything except the per-thread pointers and the constants.
1196
memcpy( &dst->i_frame, &src->i_frame, offsetof(x264_t, mb.type) - offsetof(x264_t, i_frame) );
1197
memcpy( &dst->mb.i_type, &src->mb.i_type, offsetof(x264_t, rc) - offsetof(x264_t, mb.i_type) );
1198
dst->stat = src->stat;
1201
static void x264_thread_sync_stat( x264_t *dst, x264_t *src )
1205
memcpy( &dst->stat.i_slice_count, &src->stat.i_slice_count, sizeof(dst->stat) - sizeof(dst->stat.frame) );
1208
static int x264_slices_write( x264_t *h )
1213
if( h->param.b_visualize )
1214
x264_visualize_init( h );
1217
x264_stack_align( x264_slice_write, h );
1218
i_frame_size = h->out.nal[h->out.i_nal-1].i_payload;
1219
x264_fdec_filter_row( h, h->sps->i_mb_height );
1222
if( h->param.b_visualize )
1224
x264_visualize_show( h );
1225
x264_visualize_close( h );
1229
h->out.i_frame_size = i_frame_size;
1233
/****************************************************************************
1234
* x264_encoder_encode:
1235
* XXX: i_poc : is the poc of the current given picture
1236
* i_frame : is the number of the frame being coded
1237
* ex: type frame poc
1245
****************************************************************************/
1246
int x264_encoder_encode( x264_t *h,
1247
x264_nal_t **pp_nal, int *pi_nal,
1248
x264_picture_t *pic_in,
1249
x264_picture_t *pic_out )
1251
x264_t *thread_current, *thread_prev, *thread_oldest;
1257
if( h->param.i_threads > 1)
1259
int i = ++h->i_thread_phase;
1260
int t = h->param.i_threads;
1261
thread_current = h->thread[ i%t ];
1262
thread_prev = h->thread[ (i-1)%t ];
1263
thread_oldest = h->thread[ (i+1)%t ];
1264
x264_thread_sync_context( thread_current, thread_prev );
1265
x264_thread_sync_ratecontrol( thread_current, thread_prev, thread_oldest );
1267
// fprintf(stderr, "current: %p prev: %p oldest: %p \n", thread_current, thread_prev, thread_oldest);
1276
// ok to call this before encoding any frames, since the initial values of fdec have b_kept_as_ref=0
1277
x264_reference_update( h );
1278
h->fdec->i_lines_completed = -1;
1284
/* ------------------- Setup new frame from picture -------------------- */
1285
TIMER_START( i_mtime_encode_frame );
1286
if( pic_in != NULL )
1288
/* 1: Copy the picture to a frame and move it to a buffer */
1289
x264_frame_t *fenc = x264_frame_pop_unused( h );
1291
if( x264_frame_copy_picture( h, fenc, pic_in ) < 0 )
1294
if( h->param.i_width != 16 * h->sps->i_mb_width ||
1295
h->param.i_height != 16 * h->sps->i_mb_height )
1296
x264_frame_expand_border_mod16( h, fenc );
1298
fenc->i_frame = h->frames.i_input++;
1300
x264_frame_push( h->frames.next, fenc );
1302
if( h->frames.b_have_lowres )
1303
x264_frame_init_lowres( h, fenc );
1305
if( h->frames.i_input <= h->frames.i_delay + 1 - h->param.i_threads )
1307
/* Nothing yet to encode */
1308
/* waiting for filling bframe buffer */
1309
pic_out->i_type = X264_TYPE_AUTO;
1314
if( h->frames.current[0] == NULL )
1317
/* 2: Select frame types */
1318
if( h->frames.next[0] == NULL )
1320
x264_encoder_frame_end( thread_oldest, thread_current, pp_nal, pi_nal, pic_out );
1324
x264_slicetype_decide( h );
1326
/* 3: move some B-frames and 1 non-B to encode queue */
1327
while( IS_X264_TYPE_B( h->frames.next[bframes]->i_type ) )
1329
x264_frame_push( h->frames.current, x264_frame_shift( &h->frames.next[bframes] ) );
1330
/* FIXME: when max B-frames > 3, BREF may no longer be centered after GOP closing */
1331
if( h->param.b_bframe_pyramid && bframes > 1 )
1333
x264_frame_t *mid = x264_frame_shift( &h->frames.next[bframes/2] );
1334
mid->i_type = X264_TYPE_BREF;
1335
x264_frame_push( h->frames.current, mid );
1339
x264_frame_push( h->frames.current, x264_frame_shift( h->frames.next ) );
1341
TIMER_STOP( i_mtime_encode_frame );
1343
/* ------------------- Get frame to be encoded ------------------------- */
1344
/* 4: get picture to encode */
1345
h->fenc = x264_frame_shift( h->frames.current );
1346
if( h->fenc == NULL )
1348
/* Nothing yet to encode (ex: waiting for I/P with B frames) */
1349
/* waiting for filling bframe buffer */
1350
pic_out->i_type = X264_TYPE_AUTO;
1356
if( h->fenc->i_type == X264_TYPE_IDR )
1358
h->frames.i_last_idr = h->fenc->i_frame;
1361
/* ------------------- Setup frame context ----------------------------- */
1362
/* 5: Init data dependent of frame type */
1363
TIMER_START( i_mtime_encode_frame );
1364
if( h->fenc->i_type == X264_TYPE_IDR )
1366
/* reset ref pictures */
1367
x264_reference_reset( h );
1369
i_nal_type = NAL_SLICE_IDR;
1370
i_nal_ref_idc = NAL_PRIORITY_HIGHEST;
1371
h->sh.i_type = SLICE_TYPE_I;
1373
else if( h->fenc->i_type == X264_TYPE_I )
1375
i_nal_type = NAL_SLICE;
1376
i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/
1377
h->sh.i_type = SLICE_TYPE_I;
1379
else if( h->fenc->i_type == X264_TYPE_P )
1381
i_nal_type = NAL_SLICE;
1382
i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/
1383
h->sh.i_type = SLICE_TYPE_P;
1385
else if( h->fenc->i_type == X264_TYPE_BREF )
1387
i_nal_type = NAL_SLICE;
1388
i_nal_ref_idc = NAL_PRIORITY_HIGH; /* maybe add MMCO to forget it? -> low */
1389
h->sh.i_type = SLICE_TYPE_B;
1393
i_nal_type = NAL_SLICE;
1394
i_nal_ref_idc = NAL_PRIORITY_DISPOSABLE;
1395
h->sh.i_type = SLICE_TYPE_B;
1399
h->fenc->i_poc = 2 * (h->fenc->i_frame - h->frames.i_last_idr);
1400
h->fdec->i_type = h->fenc->i_type;
1401
h->fdec->i_frame = h->fenc->i_frame;
1402
h->fenc->b_kept_as_ref =
1403
h->fdec->b_kept_as_ref = i_nal_ref_idc != NAL_PRIORITY_DISPOSABLE && h->param.i_keyint_max > 1;
1407
/* ------------------- Init ----------------------------- */
1408
/* build ref list 0/1 */
1409
x264_reference_build_list( h, h->fdec->i_poc );
1411
/* Init the rate control */
1412
x264_ratecontrol_start( h, h->fenc->i_qpplus1 );
1413
i_global_qp = x264_ratecontrol_qp( h );
1415
pic_out->i_qpplus1 =
1416
h->fdec->i_qpplus1 = i_global_qp + 1;
1418
if( h->sh.i_type == SLICE_TYPE_B )
1419
x264_macroblock_bipred_init( h );
1421
/* ------------------------ Create slice header ----------------------- */
1422
x264_slice_init( h, i_nal_type, i_global_qp );
1424
if( i_nal_ref_idc != NAL_PRIORITY_DISPOSABLE )
1427
/* ---------------------- Write the bitstream -------------------------- */
1428
/* Init bitstream context */
1430
bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream );
1435
if(h->sh.i_type == SLICE_TYPE_I)
1437
else if(h->sh.i_type == SLICE_TYPE_P)
1439
else if(h->sh.i_type == SLICE_TYPE_B)
1444
x264_nal_start(h, NAL_AUD, NAL_PRIORITY_DISPOSABLE);
1445
bs_write(&h->out.bs, 3, pic_type);
1446
bs_rbsp_trailing(&h->out.bs);
1450
h->i_nal_type = i_nal_type;
1451
h->i_nal_ref_idc = i_nal_ref_idc;
1453
/* Write SPS and PPS */
1454
if( i_nal_type == NAL_SLICE_IDR && h->param.b_repeat_headers )
1456
if( h->fenc->i_frame == 0 )
1458
/* identify ourself */
1459
x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE );
1460
x264_sei_version_write( h, &h->out.bs );
1464
/* generate sequence parameters */
1465
x264_nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST );
1466
x264_sps_write( &h->out.bs, h->sps );
1469
/* generate picture parameters */
1470
x264_nal_start( h, NAL_PPS, NAL_PRIORITY_HIGHEST );
1471
x264_pps_write( &h->out.bs, h->pps );
1476
if( h->param.i_threads > 1 )
1478
x264_pthread_create( &h->thread_handle, NULL, (void*)x264_slices_write, h );
1479
h->b_thread_active = 1;
1482
x264_slices_write( h );
1484
/* restore CPU state (before using float again) */
1485
x264_cpu_restore( h->param.cpu );
1487
if( h->sh.i_type == SLICE_TYPE_P && !h->param.rc.b_stat_read
1488
&& h->param.i_scenecut_threshold >= 0
1489
&& !h->param.b_pre_scenecut )
1491
const int *mbs = h->stat.frame.i_mb_count;
1492
int i_mb_i = mbs[I_16x16] + mbs[I_8x8] + mbs[I_4x4];
1493
int i_mb_p = mbs[P_L0] + mbs[P_8x8];
1494
int i_mb_s = mbs[P_SKIP];
1495
int i_mb = h->sps->i_mb_width * h->sps->i_mb_height;
1496
int64_t i_inter_cost = h->stat.frame.i_inter_cost;
1497
int64_t i_intra_cost = h->stat.frame.i_intra_cost;
1500
int i_gop_size = h->fenc->i_frame - h->frames.i_last_idr;
1501
float f_thresh_max = h->param.i_scenecut_threshold / 100.0;
1502
/* magic numbers pulled out of thin air */
1503
float f_thresh_min = f_thresh_max * h->param.i_keyint_min
1504
/ ( h->param.i_keyint_max * 4 );
1505
if( h->param.i_keyint_min == h->param.i_keyint_max )
1506
f_thresh_min= f_thresh_max;
1508
/* macroblock_analyse() doesn't further analyse skipped mbs,
1509
* so we have to guess their cost */
1510
if( h->stat.frame.i_mbs_analysed > 0 )
1511
i_intra_cost = i_intra_cost * i_mb / h->stat.frame.i_mbs_analysed;
1513
if( i_gop_size < h->param.i_keyint_min / 4 )
1514
f_bias = f_thresh_min / 4;
1515
else if( i_gop_size <= h->param.i_keyint_min )
1516
f_bias = f_thresh_min * i_gop_size / h->param.i_keyint_min;
1519
f_bias = f_thresh_min
1520
+ ( f_thresh_max - f_thresh_min )
1521
* ( i_gop_size - h->param.i_keyint_min )
1522
/ ( h->param.i_keyint_max - h->param.i_keyint_min );
1524
f_bias = X264_MIN( f_bias, 1.0 );
1526
/* Bad P will be reencoded as I */
1527
if( h->stat.frame.i_mbs_analysed > 0 &&
1528
i_inter_cost >= (1.0 - f_bias) * i_intra_cost )
1532
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",
1534
(double)i_intra_cost, (double)i_inter_cost,
1535
1. - (double)i_inter_cost / i_intra_cost,
1537
i_mb_i, i_mb_p, i_mb_s );
1539
/* Restore frame num */
1542
for( b = 0; h->frames.current[b] && IS_X264_TYPE_B( h->frames.current[b]->i_type ); b++ );
1545
/* If using B-frames, force GOP to be closed.
1546
* Even if this frame is going to be I and not IDR, forcing a
1547
* P-frame before the scenecut will probably help compression.
1549
* We don't yet know exactly which frame is the scene cut, so
1550
* we can't assign an I-frame. Instead, change the previous
1551
* B-frame to P, and rearrange coding order. */
1553
if( h->param.b_bframe_adaptive || b > 1 )
1554
h->fenc->i_type = X264_TYPE_AUTO;
1555
x264_frame_sort_pts( h->frames.current );
1556
x264_frame_unshift( h->frames.next, h->fenc );
1557
h->fenc = h->frames.current[b-1];
1558
h->frames.current[b-1] = NULL;
1559
h->fenc->i_type = X264_TYPE_P;
1560
x264_frame_sort_dts( h->frames.current );
1562
/* Do IDR if needed */
1563
else if( i_gop_size >= h->param.i_keyint_min )
1568
/* Reinit field of fenc */
1569
h->fenc->i_type = X264_TYPE_IDR;
1572
/* Put enqueued frames back in the pool */
1573
while( h->frames.current[0] )
1574
x264_frame_push( h->frames.next, x264_frame_shift( h->frames.current ) );
1575
x264_frame_sort_pts( h->frames.next );
1579
h->fenc->i_type = X264_TYPE_I;
1585
x264_encoder_frame_end( thread_oldest, thread_current, pp_nal, pi_nal, pic_out );
1589
static void x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
1590
x264_nal_t **pp_nal, int *pi_nal,
1591
x264_picture_t *pic_out )
1594
char psz_message[80];
1596
if( h->b_thread_active )
1598
x264_pthread_join( h->thread_handle, NULL );
1599
h->b_thread_active = 0;
1603
pic_out->i_type = X264_TYPE_AUTO;
1607
x264_frame_push_unused( thread_current, h->fenc );
1609
/* End bitstream, set output */
1610
*pi_nal = h->out.i_nal;
1611
*pp_nal = h->out.nal;
1614
/* Set output picture properties */
1615
if( h->sh.i_type == SLICE_TYPE_I )
1616
pic_out->i_type = h->i_nal_type == NAL_SLICE_IDR ? X264_TYPE_IDR : X264_TYPE_I;
1617
else if( h->sh.i_type == SLICE_TYPE_P )
1618
pic_out->i_type = X264_TYPE_P;
1620
pic_out->i_type = X264_TYPE_B;
1621
pic_out->i_pts = h->fenc->i_pts;
1623
pic_out->img.i_plane = h->fdec->i_plane;
1624
for(i = 0; i < 4; i++){
1625
pic_out->img.i_stride[i] = h->fdec->i_stride[i];
1626
pic_out->img.plane[i] = h->fdec->plane[i];
1629
/* ---------------------- Update encoder state ------------------------- */
1632
x264_cpu_restore( h->param.cpu );
1633
x264_ratecontrol_end( h, h->out.i_frame_size * 8 );
1635
/* restore CPU state (before using float again) */
1636
x264_cpu_restore( h->param.cpu );
1638
x264_noise_reduction_update( h );
1640
TIMER_STOP( i_mtime_encode_frame );
1642
/* ---------------------- Compute/Print statistics --------------------- */
1643
x264_thread_sync_stat( h, h->thread[0] );
1646
h->stat.i_slice_count[h->sh.i_type]++;
1647
h->stat.i_slice_size[h->sh.i_type] += h->out.i_frame_size + NALU_OVERHEAD;
1648
h->stat.f_slice_qp[h->sh.i_type] += h->fdec->f_qp_avg_aq;
1650
for( i = 0; i < X264_MBTYPE_MAX; i++ )
1651
h->stat.i_mb_count[h->sh.i_type][i] += h->stat.frame.i_mb_count[i];
1652
for( i = 0; i < 2; i++ )
1653
h->stat.i_mb_count_8x8dct[i] += h->stat.frame.i_mb_count_8x8dct[i];
1654
if( h->sh.i_type != SLICE_TYPE_I )
1656
for( i = 0; i < 7; i++ )
1657
h->stat.i_mb_count_size[h->sh.i_type][i] += h->stat.frame.i_mb_count_size[i];
1658
for( i = 0; i < 32; i++ )
1659
h->stat.i_mb_count_ref[h->sh.i_type][i] += h->stat.frame.i_mb_count_ref[i];
1661
if( h->sh.i_type == SLICE_TYPE_B )
1663
h->stat.i_direct_frames[ h->sh.b_direct_spatial_mv_pred ] ++;
1664
if( h->mb.b_direct_auto_write )
1666
//FIXME somewhat arbitrary time constants
1667
if( h->stat.i_direct_score[0] + h->stat.i_direct_score[1] > h->mb.i_mb_count )
1669
for( i = 0; i < 2; i++ )
1670
h->stat.i_direct_score[i] = h->stat.i_direct_score[i] * 9/10;
1672
for( i = 0; i < 2; i++ )
1673
h->stat.i_direct_score[i] += h->stat.frame.i_direct_score[i];
1677
psz_message[0] = '\0';
1678
if( h->param.analyse.b_psnr )
1682
for( i=0; i<3; i++ )
1684
sqe[i] = x264_pixel_ssd_wxh( &h->pixf,
1685
h->fdec->plane[i], h->fdec->i_stride[i],
1686
h->fenc->plane[i], h->fenc->i_stride[i],
1687
h->param.i_width >> !!i, h->param.i_height >> !!i );
1689
x264_cpu_restore( h->param.cpu );
1691
h->stat.i_sqe_global[h->sh.i_type] += sqe[0] + sqe[1] + sqe[2];
1692
h->stat.f_psnr_average[h->sh.i_type] += x264_psnr( sqe[0] + sqe[1] + sqe[2], 3 * h->param.i_width * h->param.i_height / 2 );
1693
h->stat.f_psnr_mean_y[h->sh.i_type] += x264_psnr( sqe[0], h->param.i_width * h->param.i_height );
1694
h->stat.f_psnr_mean_u[h->sh.i_type] += x264_psnr( sqe[1], h->param.i_width * h->param.i_height / 4 );
1695
h->stat.f_psnr_mean_v[h->sh.i_type] += x264_psnr( sqe[2], h->param.i_width * h->param.i_height / 4 );
1697
snprintf( psz_message, 80, " PSNR Y:%5.2f U:%5.2f V:%5.2f",
1698
x264_psnr( sqe[0], h->param.i_width * h->param.i_height ),
1699
x264_psnr( sqe[1], h->param.i_width * h->param.i_height / 4),
1700
x264_psnr( sqe[2], h->param.i_width * h->param.i_height / 4) );
1703
if( h->param.analyse.b_ssim )
1705
// offset by 2 pixels to avoid alignment of ssim blocks with dct blocks
1706
float ssim_y = x264_pixel_ssim_wxh( &h->pixf,
1707
h->fdec->plane[0] + 2+2*h->fdec->i_stride[0], h->fdec->i_stride[0],
1708
h->fenc->plane[0] + 2+2*h->fenc->i_stride[0], h->fenc->i_stride[0],
1709
h->param.i_width-2, h->param.i_height-2 );
1710
h->stat.f_ssim_mean_y[h->sh.i_type] += ssim_y;
1711
snprintf( psz_message + strlen(psz_message), 80 - strlen(psz_message),
1712
" SSIM Y:%.5f", ssim_y );
1714
psz_message[79] = '\0';
1716
x264_log( h, X264_LOG_DEBUG,
1717
"frame=%4d QP=%.2f NAL=%d Slice:%c Poc:%-3d I:%-4d P:%-4d SKIP:%-4d size=%d bytes%s\n",
1719
h->fdec->f_qp_avg_aq,
1721
h->sh.i_type == SLICE_TYPE_I ? 'I' : (h->sh.i_type == SLICE_TYPE_P ? 'P' : 'B' ),
1723
h->stat.frame.i_mb_count_i,
1724
h->stat.frame.i_mb_count_p,
1725
h->stat.frame.i_mb_count_skip,
1726
h->out.i_frame_size,
1729
// keep stats all in one place
1730
x264_thread_sync_stat( h->thread[0], h );
1731
// for the use of the next frame
1732
x264_thread_sync_stat( thread_current, h );
1734
#ifdef DEBUG_MB_TYPE
1736
static const char mb_chars[] = { 'i', 'i', 'I', 'C', 'P', '8', 'S',
1737
'D', '<', 'X', 'B', 'X', '>', 'B', 'B', 'B', 'B', '8', 'S' };
1739
for( mb_xy = 0; mb_xy < h->sps->i_mb_width * h->sps->i_mb_height; mb_xy++ )
1741
if( h->mb.type[mb_xy] < X264_MBTYPE_MAX && h->mb.type[mb_xy] >= 0 )
1742
fprintf( stderr, "%c ", mb_chars[ h->mb.type[mb_xy] ] );
1744
fprintf( stderr, "? " );
1746
if( (mb_xy+1) % h->sps->i_mb_width == 0 )
1747
fprintf( stderr, "\n" );
1752
#ifdef DEBUG_DUMP_FRAME
1753
/* Dump reconstructed frame */
1754
x264_frame_dump( h, h->fdec, "fdec.yuv" );
1758
/****************************************************************************
1759
* x264_encoder_close:
1760
****************************************************************************/
1761
void x264_encoder_close ( x264_t *h )
1763
#ifdef DEBUG_BENCHMARK
1764
int64_t i_mtime_total = i_mtime_analyse + i_mtime_encode + i_mtime_write + i_mtime_filter + 1;
1766
int64_t i_yuv_size = 3 * h->param.i_width * h->param.i_height / 2;
1769
for( i=0; i<h->param.i_threads; i++ )
1771
// don't strictly have to wait for the other threads, but it's simpler than cancelling them
1772
if( h->thread[i]->b_thread_active )
1773
x264_pthread_join( h->thread[i]->thread_handle, NULL );
1776
#ifdef DEBUG_BENCHMARK
1777
x264_log( h, X264_LOG_INFO,
1778
"analyse=%d(%lldms) encode=%d(%lldms) write=%d(%lldms) filter=%d(%lldms)\n",
1779
(int)(100*i_mtime_analyse/i_mtime_total), i_mtime_analyse/1000,
1780
(int)(100*i_mtime_encode/i_mtime_total), i_mtime_encode/1000,
1781
(int)(100*i_mtime_write/i_mtime_total), i_mtime_write/1000,
1782
(int)(100*i_mtime_filter/i_mtime_total), i_mtime_filter/1000 );
1785
/* Slices used and PSNR */
1786
for( i=0; i<5; i++ )
1788
static const int slice_order[] = { SLICE_TYPE_I, SLICE_TYPE_SI, SLICE_TYPE_P, SLICE_TYPE_SP, SLICE_TYPE_B };
1789
static const char *slice_name[] = { "P", "B", "I", "SP", "SI" };
1790
int i_slice = slice_order[i];
1792
if( h->stat.i_slice_count[i_slice] > 0 )
1794
const int i_count = h->stat.i_slice_count[i_slice];
1795
if( h->param.analyse.b_psnr )
1797
x264_log( h, X264_LOG_INFO,
1798
"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",
1799
slice_name[i_slice],
1801
h->stat.f_slice_qp[i_slice] / i_count,
1802
(double)h->stat.i_slice_size[i_slice] / i_count,
1803
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,
1804
h->stat.f_psnr_average[i_slice] / i_count,
1805
x264_psnr( h->stat.i_sqe_global[i_slice], i_count * i_yuv_size ) );
1809
x264_log( h, X264_LOG_INFO,
1810
"slice %s:%-5d Avg QP:%5.2f size:%6.0f\n",
1811
slice_name[i_slice],
1813
h->stat.f_slice_qp[i_slice] / i_count,
1814
(double)h->stat.i_slice_size[i_slice] / i_count );
1820
if( h->stat.i_slice_count[SLICE_TYPE_I] > 0 )
1822
const int64_t *i_mb_count = h->stat.i_mb_count[SLICE_TYPE_I];
1823
const double i_count = h->stat.i_slice_count[SLICE_TYPE_I] * h->mb.i_mb_count / 100.0;
1824
x264_log( h, X264_LOG_INFO,
1825
"mb I I16..4: %4.1f%% %4.1f%% %4.1f%%\n",
1826
i_mb_count[I_16x16]/ i_count,
1827
i_mb_count[I_8x8] / i_count,
1828
i_mb_count[I_4x4] / i_count );
1830
if( h->stat.i_slice_count[SLICE_TYPE_P] > 0 )
1832
const int64_t *i_mb_count = h->stat.i_mb_count[SLICE_TYPE_P];
1833
const int64_t *i_mb_size = h->stat.i_mb_count_size[SLICE_TYPE_P];
1834
const double i_count = h->stat.i_slice_count[SLICE_TYPE_P] * h->mb.i_mb_count / 100.0;
1835
x264_log( h, X264_LOG_INFO,
1836
"mb P I16..4: %4.1f%% %4.1f%% %4.1f%% P16..4: %4.1f%% %4.1f%% %4.1f%% %4.1f%% %4.1f%% skip:%4.1f%%\n",
1837
i_mb_count[I_16x16]/ i_count,
1838
i_mb_count[I_8x8] / i_count,
1839
i_mb_count[I_4x4] / i_count,
1840
i_mb_size[PIXEL_16x16] / (i_count*4),
1841
(i_mb_size[PIXEL_16x8] + i_mb_size[PIXEL_8x16]) / (i_count*4),
1842
i_mb_size[PIXEL_8x8] / (i_count*4),
1843
(i_mb_size[PIXEL_8x4] + i_mb_size[PIXEL_4x8]) / (i_count*4),
1844
i_mb_size[PIXEL_4x4] / (i_count*4),
1845
i_mb_count[P_SKIP] / i_count );
1847
if( h->stat.i_slice_count[SLICE_TYPE_B] > 0 )
1849
const int64_t *i_mb_count = h->stat.i_mb_count[SLICE_TYPE_B];
1850
const int64_t *i_mb_size = h->stat.i_mb_count_size[SLICE_TYPE_B];
1851
const double i_count = h->stat.i_slice_count[SLICE_TYPE_B] * h->mb.i_mb_count / 100.0;
1852
x264_log( h, X264_LOG_INFO,
1853
"mb B I16..4: %4.1f%% %4.1f%% %4.1f%% B16..8: %4.1f%% %4.1f%% %4.1f%% direct:%4.1f%% skip:%4.1f%%\n",
1854
i_mb_count[I_16x16] / i_count,
1855
i_mb_count[I_8x8] / i_count,
1856
i_mb_count[I_4x4] / i_count,
1857
i_mb_size[PIXEL_16x16] / (i_count*4),
1858
(i_mb_size[PIXEL_16x8] + i_mb_size[PIXEL_8x16]) / (i_count*4),
1859
i_mb_size[PIXEL_8x8] / (i_count*4),
1860
i_mb_count[B_DIRECT] / i_count,
1861
i_mb_count[B_SKIP] / i_count );
1864
x264_ratecontrol_summary( h );
1866
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 )
1868
const int i_count = h->stat.i_slice_count[SLICE_TYPE_I] +
1869
h->stat.i_slice_count[SLICE_TYPE_P] +
1870
h->stat.i_slice_count[SLICE_TYPE_B];
1871
float fps = (float) h->param.i_fps_num / h->param.i_fps_den;
1872
#define SUM3(p) (p[SLICE_TYPE_I] + p[SLICE_TYPE_P] + p[SLICE_TYPE_B])
1873
#define SUM3b(p,o) (p[SLICE_TYPE_I][o] + p[SLICE_TYPE_P][o] + p[SLICE_TYPE_B][o])
1874
float f_bitrate = fps * SUM3(h->stat.i_slice_size) / i_count / 125;
1876
if( h->pps->b_transform_8x8_mode )
1878
int64_t i_i8x8 = SUM3b( h->stat.i_mb_count, I_8x8 );
1879
int64_t i_intra = i_i8x8 + SUM3b( h->stat.i_mb_count, I_4x4 )
1880
+ SUM3b( h->stat.i_mb_count, I_16x16 );
1881
x264_log( h, X264_LOG_INFO, "8x8 transform intra:%.1f%% inter:%.1f%%\n",
1882
100. * i_i8x8 / i_intra,
1883
100. * h->stat.i_mb_count_8x8dct[1] / h->stat.i_mb_count_8x8dct[0] );
1886
if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_AUTO
1887
&& h->stat.i_slice_count[SLICE_TYPE_B] )
1889
x264_log( h, X264_LOG_INFO, "direct mvs spatial:%.1f%% temporal:%.1f%%\n",
1890
h->stat.i_direct_frames[1] * 100. / h->stat.i_slice_count[SLICE_TYPE_B],
1891
h->stat.i_direct_frames[0] * 100. / h->stat.i_slice_count[SLICE_TYPE_B] );
1894
if( h->frames.i_max_ref0 > 1 )
1897
for( i_slice = 0; i_slice < 2; i_slice++ )
1903
for( i = 0; i < h->frames.i_max_ref0 << h->param.b_interlaced; i++ )
1904
if( h->stat.i_mb_count_ref[i_slice][i] )
1906
i_den += h->stat.i_mb_count_ref[i_slice][i];
1911
for( i = 0; i <= i_max; i++ )
1912
p += sprintf( p, " %4.1f%%", 100. * h->stat.i_mb_count_ref[i_slice][i] / i_den );
1913
x264_log( h, X264_LOG_INFO, "ref %c %s\n", i_slice==SLICE_TYPE_P ? 'P' : 'B', buf );
1917
if( h->param.analyse.b_ssim )
1919
x264_log( h, X264_LOG_INFO,
1920
"SSIM Mean Y:%.7f\n",
1921
SUM3( h->stat.f_ssim_mean_y ) / i_count );
1923
if( h->param.analyse.b_psnr )
1925
x264_log( h, X264_LOG_INFO,
1926
"PSNR Mean Y:%6.3f U:%6.3f V:%6.3f Avg:%6.3f Global:%6.3f kb/s:%.2f\n",
1927
SUM3( h->stat.f_psnr_mean_y ) / i_count,
1928
SUM3( h->stat.f_psnr_mean_u ) / i_count,
1929
SUM3( h->stat.f_psnr_mean_v ) / i_count,
1930
SUM3( h->stat.f_psnr_average ) / i_count,
1931
x264_psnr( SUM3( h->stat.i_sqe_global ), i_count * i_yuv_size ),
1935
x264_log( h, X264_LOG_INFO, "kb/s:%.1f\n", f_bitrate );
1939
for( i = 0; h->frames.current[i]; i++ )
1940
x264_frame_delete( h->frames.current[i] );
1941
for( i = 0; h->frames.next[i]; i++ )
1942
x264_frame_delete( h->frames.next[i] );
1943
for( i = 0; h->frames.unused[i]; i++ )
1944
x264_frame_delete( h->frames.unused[i] );
1945
for( i = 0; h->frames.reference[i]; i++ )
1946
x264_frame_delete( h->frames.reference[i] );
1949
x264_ratecontrol_delete( h );
1952
if( h->param.rc.psz_stat_out )
1953
free( h->param.rc.psz_stat_out );
1954
if( h->param.rc.psz_stat_in )
1955
free( h->param.rc.psz_stat_in );
1956
if( h->param.rc.psz_rc_eq )
1957
free( h->param.rc.psz_rc_eq );
1959
x264_cqm_delete( h );
1960
for( i = h->param.i_threads - 1; i >= 0; i-- )
1962
x264_macroblock_cache_end( h->thread[i] );
1963
x264_free( h->thread[i]->out.p_bitstream );
1964
x264_free( h->thread[i] );