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 *****************************************************************************/
30 #include "common/common.h"
31 #include "common/cpu.h"
35 #include "ratecontrol.h"
36 #include "macroblock.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 )
61 /****************************************************************************
63 ******************************* x264 libs **********************************
65 ****************************************************************************/
66 static int64_t x264_sqe( uint8_t *pix1, int i_pix_stride, uint8_t *pix2, int i_pix2_stride, int i_width, int i_height )
72 for( y = 0; y < i_height; y++ )
74 for( x = 0; x < i_width; x++ )
78 tmp = pix1[y*i_pix_stride+x] - pix2[y*i_pix2_stride+x];
86 static float x264_mse( int64_t i_sqe, int64_t i_size )
88 return (double)i_sqe / ((double)65025.0 * (double)i_size);
91 static float x264_psnr( int64_t i_sqe, int64_t i_size )
93 double f_mse = (double)i_sqe / ((double)65025.0 * (double)i_size);
94 if( f_mse <= 0.0000000001 ) /* Max 100dB */
97 return (float)(-10.0 * log( f_mse ) / log( 10.0 ));
100 #ifdef DEBUG_DUMP_FRAME
101 static void x264_frame_dump( x264_t *h, x264_frame_t *fr, char *name )
103 FILE * f = fopen( name, "a" );
106 fseek( f, 0, SEEK_END );
108 for( i = 0; i < fr->i_plane; i++ )
110 for( y = 0; y < h->param.i_height / ( i == 0 ? 1 : 2 ); y++ )
112 fwrite( &fr->plane[i][y*fr->i_stride[i]], 1, h->param.i_width / ( i == 0 ? 1 : 2 ), f );
120 /* Fill "default" values */
121 static void x264_slice_header_init( x264_slice_header_t *sh, x264_param_t *param,
122 x264_sps_t *sps, x264_pps_t *pps,
123 int i_type, int i_idr_pic_id, int i_frame )
125 /* First we fill all field */
131 sh->i_pps_id = pps->i_id;
133 sh->i_frame_num = i_frame;
135 sh->b_field_pic = 0; /* Not field support for now */
136 sh->b_bottom_field = 1; /* not yet used */
138 sh->i_idr_pic_id = i_idr_pic_id;
140 /* poc stuff, fixed later */
142 sh->i_delta_poc_bottom = 0;
143 sh->i_delta_poc[0] = 0;
144 sh->i_delta_poc[1] = 0;
146 sh->i_redundant_pic_cnt = 0;
148 sh->b_direct_spatial_mv_pred = ( param->analyse.i_direct_mv_pred == X264_DIRECT_PRED_SPATIAL );
150 sh->b_num_ref_idx_override = 0;
151 sh->i_num_ref_idx_l0_active = 1;
152 sh->i_num_ref_idx_l1_active = 1;
154 sh->i_cabac_init_idc = param->i_cabac_init_idc;
157 sh->b_sp_for_swidth = 0;
160 if( param->b_deblocking_filter )
162 sh->i_disable_deblocking_filter_idc = 0;
166 sh->i_disable_deblocking_filter_idc = 1;
168 sh->i_alpha_c0_offset = param->i_deblocking_filter_alphac0 << 1;
169 sh->i_beta_offset = param->i_deblocking_filter_beta << 1;
172 static void x264_slice_header_write( bs_t *s, x264_slice_header_t *sh, int i_nal_ref_idc )
174 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->i_idr_pic_id >= 0 ) /* NAL IDR */
181 bs_write_ue( s, sh->i_idr_pic_id );
184 if( sh->sps->i_poc_type == 0 )
186 bs_write( s, sh->sps->i_log2_max_poc_lsb, sh->i_poc_lsb );
187 if( sh->pps->b_pic_order && !sh->b_field_pic )
189 bs_write_se( s, sh->i_delta_poc_bottom );
192 else if( sh->sps->i_poc_type == 1 && !sh->sps->b_delta_pic_order_always_zero )
194 bs_write_se( s, sh->i_delta_poc[0] );
195 if( sh->pps->b_pic_order && !sh->b_field_pic )
197 bs_write_se( s, sh->i_delta_poc[1] );
201 if( sh->pps->b_redundant_pic_cnt )
203 bs_write_ue( s, sh->i_redundant_pic_cnt );
206 if( sh->i_type == SLICE_TYPE_B )
208 bs_write1( s, sh->b_direct_spatial_mv_pred );
210 if( sh->i_type == SLICE_TYPE_P || sh->i_type == SLICE_TYPE_SP || sh->i_type == SLICE_TYPE_B )
212 bs_write1( s, sh->b_num_ref_idx_override );
213 if( sh->b_num_ref_idx_override )
215 bs_write_ue( s, sh->i_num_ref_idx_l0_active - 1 );
216 if( sh->i_type == SLICE_TYPE_B )
218 bs_write_ue( s, sh->i_num_ref_idx_l1_active - 1 );
223 /* ref pic list reordering */
224 if( sh->i_type != SLICE_TYPE_I )
226 int b_ref_pic_list_reordering_l0 = 0;
227 bs_write1( s, b_ref_pic_list_reordering_l0 );
228 if( b_ref_pic_list_reordering_l0 )
233 if( sh->i_type == SLICE_TYPE_B )
235 int b_ref_pic_list_reordering_l1 = 0;
236 bs_write1( s, b_ref_pic_list_reordering_l1 );
237 if( b_ref_pic_list_reordering_l1 )
243 if( ( sh->pps->b_weighted_pred && ( sh->i_type == SLICE_TYPE_P || sh->i_type == SLICE_TYPE_SP ) ) ||
244 ( sh->pps->b_weighted_bipred == 1 && sh->i_type == SLICE_TYPE_B ) )
249 if( i_nal_ref_idc != 0 )
251 if( sh->i_idr_pic_id >= 0 )
253 bs_write1( s, 0 ); /* no output of prior pics flag */
254 bs_write1( s, 0 ); /* long term reference flag */
258 bs_write1( s, 0 ); /* adaptive_ref_pic_marking_mode_flag */
263 if( sh->pps->b_cabac && sh->i_type != SLICE_TYPE_I )
265 bs_write_ue( s, sh->i_cabac_init_idc );
267 bs_write_se( s, sh->i_qp_delta ); /* slice qp delta */
269 if( sh->i_type == SLICE_TYPE_SP || sh->i_type == SLICE_TYPE_SI )
271 if( sh->i_type == SLICE_TYPE_SP )
273 bs_write1( s, sh->b_sp_for_swidth );
275 bs_write_se( s, sh->i_qs_delta );
279 if( sh->pps->b_deblocking_filter_control )
281 bs_write_ue( s, sh->i_disable_deblocking_filter_idc );
282 if( sh->i_disable_deblocking_filter_idc != 1 )
284 bs_write_se( s, sh->i_alpha_c0_offset >> 1 );
285 bs_write_se( s, sh->i_beta_offset >> 1 );
290 /****************************************************************************
292 ****************************************************************************
293 ****************************** External API*********************************
294 ****************************************************************************
296 ****************************************************************************/
298 /****************************************************************************
300 ****************************************************************************/
301 x264_t *x264_encoder_open ( x264_param_t *param )
303 x264_t *h = x264_malloc( sizeof( x264_t ) );
306 /* Create a copy of param */
307 memcpy( &h->param, param, sizeof( x264_param_t ) );
308 if( h->param.rc.psz_stat_out )
309 h->param.rc.psz_stat_out = strdup( h->param.rc.psz_stat_out );
310 if( h->param.rc.psz_stat_in )
311 h->param.rc.psz_stat_in = strdup( h->param.rc.psz_stat_in );
312 if( h->param.rc.psz_rc_eq )
313 h->param.rc.psz_rc_eq = strdup( h->param.rc.psz_rc_eq );
315 /* Check parameters validity */
316 if( param->i_width <= 0 || param->i_height <= 0 )
318 x264_log( h, X264_LOG_ERROR, "invalid width x height (%dx%d)\n",
319 param->i_width, param->i_height );
324 if( param->i_width % 16 != 0 || param->i_height % 16 != 0 )
326 x264_log( h, X264_LOG_ERROR, "width %% 16 != 0 or height %% 16 != 0 (%dx%d)\n",
327 param->i_width, param->i_height );
331 if( param->i_csp != X264_CSP_I420 )
333 x264_log( h, X264_LOG_ERROR, "invalid CSP (only I420 supported)\n" );
338 /* Fix parameters values */
339 h->param.i_frame_reference = x264_clip3( h->param.i_frame_reference, 1, 15 );
340 if( h->param.i_keyint_max <= 0 )
341 h->param.i_keyint_max = 1;
342 h->param.i_keyint_min = x264_clip3( h->param.i_keyint_min, 1, h->param.i_keyint_max/2+1 );
343 h->param.i_bframe = x264_clip3( h->param.i_bframe, 0, X264_BFRAME_MAX );
345 h->param.i_deblocking_filter_alphac0 = x264_clip3( h->param.i_deblocking_filter_alphac0, -6, 6 );
346 h->param.i_deblocking_filter_beta = x264_clip3( h->param.i_deblocking_filter_beta, -6, 6 );
348 h->param.i_cabac_init_idc = x264_clip3( h->param.i_cabac_init_idc, -1, 2 );
350 h->param.analyse.i_subpel_refine = x264_clip3( h->param.analyse.i_subpel_refine, 1, 5 );
351 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
352 h->param.analyse.inter |= X264_ANALYSE_PSUB16x16;
354 if( h->param.rc.f_qblur < 0 )
355 h->param.rc.f_qblur = 0;
356 if( h->param.rc.f_complexity_blur < 0 )
357 h->param.rc.f_complexity_blur = 0;
360 if( h->param.vui.i_sar_width > 0 && h->param.vui.i_sar_height > 0 )
362 int i_w = param->vui.i_sar_width;
363 int i_h = param->vui.i_sar_height;
364 int a = i_w, b = i_h;
376 while( i_w > 65535 || i_h > 65535 )
382 h->param.vui.i_sar_width = 0;
383 h->param.vui.i_sar_height = 0;
384 if( i_w == 0 || i_h == 0 )
386 x264_log( h, X264_LOG_ERROR, "cannot create valid sample aspect ratio\n" );
388 else if( i_w == i_h )
390 x264_log( h, X264_LOG_INFO, "no need for a SAR\n" );
394 x264_log( h, X264_LOG_INFO, "using SAR=%d/%d\n", i_w, i_h );
395 h->param.vui.i_sar_width = i_w;
396 h->param.vui.i_sar_height = i_h;
403 h->out.i_bitstream = 1000000; /* FIXME estimate max size (idth/height) */
404 h->out.p_bitstream = x264_malloc( h->out.i_bitstream );
411 h->sps = &h->sps_array[0];
412 x264_sps_init( h->sps, 0, &h->param );
414 h->pps = &h->pps_array[0];
415 x264_pps_init( h->pps, 0, &h->param, h->sps);
417 h->mb.i_mb_count = h->sps->i_mb_width * h->sps->i_mb_height;
420 for( i = 0; i < X264_BFRAME_MAX + 1; i++ )
422 h->frames.current[i] = NULL;
423 h->frames.next[i] = NULL;
424 h->frames.unused[i] = NULL;
426 for( i = 0; i < 1 + h->param.i_bframe; i++ )
428 h->frames.unused[i] = x264_frame_new( h );
430 for( i = 0; i < 2 + h->param.i_frame_reference; i++ )
432 /* 2 = 1 backward ref + 1 fdec */
433 h->frames.reference[i] = x264_frame_new( h );
435 h->frames.i_last_idr = - h->param.i_keyint_max;
436 h->frames.i_input = 0;
441 h->fdec = h->frames.reference[0];
444 x264_macroblock_cache_init( h );
446 /* init cabac adaptive model */
447 x264_cabac_model_init( &h->cabac );
449 /* init CPU functions */
450 x264_predict_16x16_init( h->param.cpu, h->predict_16x16 );
451 x264_predict_8x8_init( h->param.cpu, h->predict_8x8 );
452 x264_predict_4x4_init( h->param.cpu, h->predict_4x4 );
454 x264_pixel_init( h->param.cpu, &h->pixf );
455 x264_dct_init( h->param.cpu, &h->dctf );
456 x264_mc_init( h->param.cpu, &h->mc );
457 x264_csp_init( h->param.cpu, h->param.i_csp, &h->csp );
460 if( x264_ratecontrol_new( h ) < 0 )
463 h->i_last_intra_size = 0;
464 h->i_last_inter_size = 0;
467 for( i_slice = 0; i_slice < 5; i_slice++ )
469 h->stat.i_slice_count[i_slice] = 0;
470 h->stat.i_slice_size[i_slice] = 0;
471 h->stat.i_slice_qp[i_slice] = 0;
473 h->stat.i_sqe_global[i_slice] = 0;
474 h->stat.f_psnr_average[i_slice] = 0.0;
475 h->stat.f_psnr_mean_y[i_slice] = h->stat.f_psnr_mean_u[i_slice] = h->stat.f_psnr_mean_v[i_slice] = 0.0;
477 for( i = 0; i < 18; i++ )
478 h->stat.i_mb_count[i_slice][i] = 0;
481 x264_log( h, X264_LOG_INFO, "using cpu capabilities %s%s%s%s%s%s\n",
482 param->cpu&X264_CPU_MMX ? "MMX " : "",
483 param->cpu&X264_CPU_MMXEXT ? "MMXEXT " : "",
484 param->cpu&X264_CPU_SSE ? "SSE " : "",
485 param->cpu&X264_CPU_SSE2 ? "SSE2 " : "",
486 param->cpu&X264_CPU_3DNOW ? "3DNow! " : "",
487 param->cpu&X264_CPU_ALTIVEC ? "Altivec " : "" );
493 static void x264_nal_start( x264_t *h, int i_type, int i_ref_idc )
495 x264_nal_t *nal = &h->out.nal[h->out.i_nal];
497 nal->i_ref_idc = i_ref_idc;
498 nal->i_type = i_type;
500 bs_align_0( &h->out.bs ); /* not needed */
503 nal->p_payload= &h->out.p_bitstream[bs_pos( &h->out.bs) / 8];
505 static void x264_nal_end( x264_t *h )
507 x264_nal_t *nal = &h->out.nal[h->out.i_nal];
509 bs_align_0( &h->out.bs ); /* not needed */
511 nal->i_payload = &h->out.p_bitstream[bs_pos( &h->out.bs)/8] - nal->p_payload;
516 /****************************************************************************
517 * x264_encoder_headers:
518 ****************************************************************************/
519 int x264_encoder_headers( x264_t *h, x264_nal_t **pp_nal, int *pi_nal )
521 /* init bitstream context */
523 bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream );
525 /* Put SPS and PPS */
526 if( h->i_frame == 0 )
528 /* generate sequence parameters */
529 x264_nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST );
530 x264_sps_write( &h->out.bs, h->sps );
533 /* generate picture parameters */
534 x264_nal_start( h, NAL_PPS, NAL_PRIORITY_HIGHEST );
535 x264_pps_write( &h->out.bs, h->pps );
539 *pi_nal = h->out.i_nal;
540 *pp_nal = &h->out.nal[0];
546 static void x264_frame_put( x264_frame_t *list[X264_BFRAME_MAX], x264_frame_t *frame )
550 while( list[i] != NULL ) i++;
555 static x264_frame_t *x264_frame_get( x264_frame_t *list[X264_BFRAME_MAX+1] )
557 x264_frame_t *frame = list[0];
560 for( i = 0; i < X264_BFRAME_MAX; i++ )
564 list[X264_BFRAME_MAX] = NULL;
569 /* Sort queued frames into input order */
570 static void x264_frame_sort( x264_frame_t *list[X264_BFRAME_MAX+1] )
575 for( i = 0; i < X264_BFRAME_MAX && list[i+1]; i++ )
577 if( list[i]->i_frame > list[i+1]->i_frame )
579 x264_frame_t *tmp = list[i+1];
588 static inline void x264_reference_build_list( x264_t *h, int i_poc )
593 /* build ref list 0/1 */
596 for( i = 1; i < h->param.i_frame_reference+2; i++ )
598 if( h->frames.reference[i]->i_poc >= 0 )
600 if( h->frames.reference[i]->i_poc < i_poc )
602 h->fref0[h->i_ref0++] = h->frames.reference[i];
604 else if( h->frames.reference[i]->i_poc > i_poc )
606 h->fref1[h->i_ref1++] = h->frames.reference[i];
610 /* Order ref0 from higher to lower poc */
614 for( i = 0; i < h->i_ref0 - 1; i++ )
616 if( h->fref0[i]->i_poc < h->fref0[i+1]->i_poc )
618 x264_frame_t *tmp = h->fref0[i+1];
620 h->fref0[i+1] = h->fref0[i];
627 /* Order ref1 from lower to higher poc (bubble sort) for B-frame */
631 for( i = 0; i < h->i_ref1 - 1; i++ )
633 if( h->fref1[i]->i_poc > h->fref1[i+1]->i_poc )
635 x264_frame_t *tmp = h->fref1[i+1];
637 h->fref1[i+1] = h->fref1[i];
645 if( h->i_ref0 > h->param.i_frame_reference )
647 h->i_ref0 = h->param.i_frame_reference;
655 static inline void x264_reference_update( x264_t *h )
659 /* apply deblocking filter to the current decoded picture */
660 if( h->param.b_deblocking_filter )
662 TIMER_START( i_mtime_filter );
663 x264_frame_deblocking_filter( h, h->sh.i_type );
664 TIMER_STOP( i_mtime_filter );
667 x264_frame_expand_border( h->fdec );
669 /* create filtered images */
670 x264_frame_filter( h->param.cpu, h->fdec );
672 /* expand border of filtered images */
673 x264_frame_expand_border_filtered( h->fdec );
675 /* move frame in the buffer */
676 h->fdec = h->frames.reference[h->param.i_frame_reference+1];
677 for( i = h->param.i_frame_reference+1; i > 0; i-- )
679 h->frames.reference[i] = h->frames.reference[i-1];
681 h->frames.reference[0] = h->fdec;
684 static inline void x264_reference_reset( x264_t *h )
688 /* reset ref pictures */
689 for( i = 1; i < h->param.i_frame_reference+2; i++ )
691 h->frames.reference[i]->i_poc = -1;
693 h->frames.reference[0]->i_poc = 0;
696 static inline void x264_slice_init( x264_t *h, int i_nal_type, int i_slice_type, int i_global_qp )
698 /* ------------------------ Create slice header ----------------------- */
699 if( i_nal_type == NAL_SLICE_IDR )
701 x264_slice_header_init( &h->sh, &h->param, h->sps, h->pps, i_slice_type, h->i_idr_pic_id, h->i_frame_num - 1 );
704 h->i_idr_pic_id = ( h->i_idr_pic_id + 1 ) % 65536;
708 x264_slice_header_init( &h->sh, &h->param, h->sps, h->pps, i_slice_type, -1, h->i_frame_num - 1 );
710 /* always set the real higher num of ref frame used */
711 h->sh.b_num_ref_idx_override = 1;
712 h->sh.i_num_ref_idx_l0_active = h->i_ref0 <= 0 ? 1 : h->i_ref0;
713 h->sh.i_num_ref_idx_l1_active = h->i_ref1 <= 0 ? 1 : h->i_ref1;
716 if( h->sps->i_poc_type == 0 )
718 h->sh.i_poc_lsb = h->fdec->i_poc & ( (1 << h->sps->i_log2_max_poc_lsb) - 1 );
719 h->sh.i_delta_poc_bottom = 0; /* XXX won't work for field */
721 else if( h->sps->i_poc_type == 1 )
723 /* FIXME TODO FIXME */
727 /* Nothing to do ? */
731 h->sh.i_qp_delta = i_global_qp - h->pps->i_pic_init_qp;
733 /* get adapative cabac model if needed */
734 if( h->param.b_cabac )
736 if( h->param.i_cabac_init_idc == -1 )
738 h->sh.i_cabac_init_idc = x264_cabac_model_get( &h->cabac, i_slice_type );
742 x264_macroblock_slice_init( h );
745 static inline void x264_slice_write( x264_t *h, int i_nal_type, int i_nal_ref_idc )
752 h->stat.frame.i_hdr_bits =
753 h->stat.frame.i_itex_bits =
754 h->stat.frame.i_ptex_bits =
755 h->stat.frame.i_misc_bits =
756 h->stat.frame.i_intra_cost =
757 h->stat.frame.i_inter_cost = 0;
758 for( i = 0; i < 18; i++ )
759 h->stat.frame.i_mb_count[i] = 0;
762 x264_nal_start( h, i_nal_type, i_nal_ref_idc );
765 x264_slice_header_write( &h->out.bs, &h->sh, i_nal_ref_idc );
766 if( h->param.b_cabac )
768 /* alignment needed */
769 bs_align_1( &h->out.bs );
772 x264_cabac_context_init( &h->cabac, h->sh.i_type, h->sh.pps->i_pic_init_qp + h->sh.i_qp_delta, h->sh.i_cabac_init_idc );
773 x264_cabac_encode_init ( &h->cabac, &h->out.bs );
775 h->mb.i_last_qp = h->pps->i_pic_init_qp + h->sh.i_qp_delta;
776 h->mb.i_last_dqp = 0;
778 for( mb_xy = 0, i_skip = 0; mb_xy < h->sps->i_mb_width * h->sps->i_mb_height; mb_xy++ )
780 const int i_mb_y = mb_xy / h->sps->i_mb_width;
781 const int i_mb_x = mb_xy % h->sps->i_mb_width;
783 int mb_spos = bs_pos(&h->out.bs);
786 x264_macroblock_cache_load( h, i_mb_x, i_mb_y );
788 /* analyse parameters
789 * Slice I: choose I_4x4 or I_16x16 mode
790 * Slice P: choose between using P mode or intra (4x4 or 16x16)
792 TIMER_START( i_mtime_analyse );
793 x264_macroblock_analyse( h );
794 TIMER_STOP( i_mtime_analyse );
796 /* encode this macrobock -> be carefull it can change the mb type to P_SKIP if needed */
797 TIMER_START( i_mtime_encode );
798 x264_macroblock_encode( h );
799 TIMER_STOP( i_mtime_encode );
801 TIMER_START( i_mtime_write );
802 if( IS_SKIP( h->mb.i_type ) )
804 if( h->param.b_cabac )
808 /* not end_of_slice_flag */
809 x264_cabac_encode_terminal( &h->cabac, 0 );
812 x264_cabac_mb_skip( h, 1 );
821 if( h->param.b_cabac )
825 /* not end_of_slice_flag */
826 x264_cabac_encode_terminal( &h->cabac, 0 );
828 if( h->sh.i_type != SLICE_TYPE_I )
830 x264_cabac_mb_skip( h, 0 );
832 x264_macroblock_write_cabac( h, &h->out.bs );
836 if( h->sh.i_type != SLICE_TYPE_I )
838 bs_write_ue( &h->out.bs, i_skip ); /* skip run */
841 x264_macroblock_write_cavlc( h, &h->out.bs );
844 TIMER_STOP( i_mtime_write );
847 x264_macroblock_cache_save( h );
849 h->stat.frame.i_mb_count[h->mb.i_type]++;
851 x264_ratecontrol_mb(h, bs_pos(&h->out.bs) - mb_spos);
854 if( h->param.b_cabac )
857 x264_cabac_encode_terminal( &h->cabac, 1 );
859 else if( i_skip > 0 )
861 bs_write_ue( &h->out.bs, i_skip ); /* last skip run */
864 if( h->param.b_cabac )
867 x264_cabac_encode_flush( &h->cabac );
868 /* TODO cabac stuffing things (p209) */
869 i_cabac_word = (((3 * h->cabac.i_sym_cnt - 3 * 96 * h->sps->i_mb_width * h->sps->i_mb_height)/32) - bs_pos( &h->out.bs)/8)/3;
871 while( i_cabac_word > 0 )
873 bs_write( &h->out.bs, 16, 0x0000 );
879 /* rbsp_slice_trailing_bits */
880 bs_rbsp_trailing( &h->out.bs );
885 /* Compute misc bits */
886 h->stat.frame.i_misc_bits = bs_pos( &h->out.bs )
887 - h->stat.frame.i_itex_bits
888 - h->stat.frame.i_ptex_bits
889 - h->stat.frame.i_hdr_bits;
892 /****************************************************************************
893 * x264_encoder_encode:
894 * XXX: i_poc : is the poc of the current given picture
895 * i_frame : is the number of the frame being coded
904 ****************************************************************************/
905 int x264_encoder_encode( x264_t *h,
906 x264_nal_t **pp_nal, int *pi_nal,
907 x264_picture_t *pic )
909 x264_frame_t *frame_psnr = h->fdec; /* just to keep the current decoded frame for psnr calculation */
918 char psz_message[80];
925 /* ------------------- Setup new frame from picture -------------------- */
926 TIMER_START( i_mtime_encode_frame );
929 /* 1: Copy the picture to a frame and move it to a buffer */
930 x264_frame_t *fenc = x264_frame_get( h->frames.unused );
932 x264_frame_copy_picture( h, fenc, pic );
934 fenc->i_frame = h->frames.i_input++;
936 x264_frame_put( h->frames.next, fenc );
938 if( h->frames.i_input <= h->param.i_bframe )
940 /* Nothing yet to encode */
941 /* waiting for filling bframe buffer */
942 pic->i_type = X264_TYPE_AUTO;
947 if( h->frames.current[0] == NULL )
949 /* 2: Select frame types */
953 if( h->frames.next[0] == NULL )
956 if( h->param.rc.b_stat_read )
958 /* Use the frame types from the first pass */
959 for( i = 0; h->frames.next[i] != NULL; i++ )
960 h->frames.next[i]->i_type =
961 x264_ratecontrol_slice_type( h, h->frames.next[i]->i_frame );
964 for( bframes = 0;; bframes++ )
966 frm = h->frames.next[bframes];
969 if( frm->i_frame - h->frames.i_last_idr >= h->param.i_keyint_max )
971 if( frm->i_type == X264_TYPE_AUTO )
972 frm->i_type = X264_TYPE_IDR;
973 if( frm->i_type != X264_TYPE_IDR )
974 x264_log( h, X264_LOG_ERROR, "specified frame type (%d) is not compatible with keyframe interval\n", frm->i_type );
976 if( frm->i_type == X264_TYPE_IDR )
985 h->frames.next[bframes]->i_type = X264_TYPE_P;
989 if( bframes == h->param.i_bframe
990 || h->frames.next[bframes+1] == NULL )
992 if( frm->i_type == X264_TYPE_B )
993 x264_log( h, X264_LOG_ERROR, "specified frame type is not compatible with max B-frames\n" );
994 if( frm->i_type == X264_TYPE_AUTO
995 || frm->i_type == X264_TYPE_B )
996 frm->i_type = X264_TYPE_P;
999 frm->i_poc = h->i_poc;
1002 if( frm->i_type != X264_TYPE_AUTO && frm->i_type != X264_TYPE_B )
1005 frm->i_type = X264_TYPE_B;
1008 /* 3: move some B-frames and 1 non-B to encode queue */
1009 x264_frame_put( h->frames.current, h->frames.next[bframes] );
1011 x264_frame_put( h->frames.current, x264_frame_get( h->frames.next ) );
1012 x264_frame_get( h->frames.next );
1014 TIMER_STOP( i_mtime_encode_frame );
1016 /* ------------------- Get frame to be encoded ------------------------- */
1017 /* 4: get picture to encode */
1018 h->fenc = x264_frame_get( h->frames.current );
1019 if( h->fenc == NULL )
1021 /* Nothing yet to encode (ex: waiting for I/P with B frames) */
1022 /* waiting for filling bframe buffer */
1023 pic->i_type = X264_TYPE_AUTO;
1029 if( h->fenc->i_type == X264_TYPE_IDR )
1031 h->frames.i_last_idr = h->fenc->i_frame;
1034 /* ------------------- Setup frame context ----------------------------- */
1035 /* 5: Init data dependant of frame type */
1036 TIMER_START( i_mtime_encode_frame );
1037 if( h->fenc->i_type == X264_TYPE_IDR )
1039 /* reset ref pictures */
1040 x264_reference_reset( h );
1042 i_nal_type = NAL_SLICE_IDR;
1043 i_nal_ref_idc = NAL_PRIORITY_HIGHEST;
1044 i_slice_type = SLICE_TYPE_I;
1046 else if( h->fenc->i_type == X264_TYPE_I )
1048 i_nal_type = NAL_SLICE;
1049 i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/
1050 i_slice_type = SLICE_TYPE_I;
1052 else if( h->fenc->i_type == X264_TYPE_P )
1054 i_nal_type = NAL_SLICE;
1055 i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/
1056 i_slice_type = SLICE_TYPE_P;
1060 i_nal_type = NAL_SLICE;
1061 i_nal_ref_idc = NAL_PRIORITY_DISPOSABLE;
1062 i_slice_type = SLICE_TYPE_B;
1066 h->fdec->i_type = h->fenc->i_type;
1067 h->fdec->i_poc = h->fenc->i_poc;
1068 h->fdec->i_frame = h->fenc->i_frame;
1072 /* ------------------- Init ----------------------------- */
1073 /* Init the rate control */
1074 x264_ratecontrol_start( h, i_slice_type );
1075 i_global_qp = x264_ratecontrol_qp( h );
1076 if( h->fenc->i_qpplus1 > 0 )
1078 i_global_qp = x264_clip3( h->fenc->i_qpplus1 - 1, 0, 51 );
1081 /* build ref list 0/1 */
1082 x264_reference_build_list( h, h->fdec->i_poc );
1084 /* increase frame num but only once for B frame */
1085 if( i_slice_type != SLICE_TYPE_B || h->sh.i_type != SLICE_TYPE_B )
1090 /* ------------------------ Create slice header ----------------------- */
1091 x264_slice_init( h, i_nal_type, i_slice_type, i_global_qp );
1093 /* ---------------------- Write the bitstream -------------------------- */
1094 /* Init bitstream context */
1096 bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream );
1098 /* Write SPS and PPS */
1099 if( i_nal_type == NAL_SLICE_IDR )
1101 /* generate sequence parameters */
1102 x264_nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST );
1103 x264_sps_write( &h->out.bs, h->sps );
1106 /* generate picture parameters */
1107 x264_nal_start( h, NAL_PPS, NAL_PRIORITY_HIGHEST );
1108 x264_pps_write( &h->out.bs, h->pps );
1112 /* Write the slice */
1113 x264_slice_write( h, i_nal_type, i_nal_ref_idc );
1115 /* restore CPU state (before using float again) */
1116 x264_cpu_restore( h->param.cpu );
1118 /* XXX: this scene cut won't work with B frame (it may never create IDR -> bad) */
1119 if( i_slice_type == SLICE_TYPE_P && !h->param.rc.b_stat_read
1120 && h->param.i_scenecut_threshold >= 0 )
1122 int i_mb_i = h->stat.frame.i_mb_count[I_4x4] + h->stat.frame.i_mb_count[I_16x16];
1123 int i_mb_p = h->stat.frame.i_mb_count[P_L0] + h->stat.frame.i_mb_count[P_8x8];
1124 int i_mb_s = h->stat.frame.i_mb_count[P_SKIP];
1125 int i_mb = h->sps->i_mb_width * h->sps->i_mb_height;
1126 int64_t i_inter_cost = h->stat.frame.i_inter_cost;
1127 int64_t i_intra_cost = h->stat.frame.i_intra_cost;
1130 int i_gop_size = h->fenc->i_frame - h->frames.i_last_idr;
1131 float f_thresh_max = h->param.i_scenecut_threshold / 100.0;
1132 /* ratio of 10 pulled out of thin air */
1133 float f_thresh_min = f_thresh_max * h->param.i_keyint_min
1134 / ( h->param.i_keyint_max * 4 );
1135 if( h->param.i_keyint_min == h->param.i_keyint_max )
1136 f_thresh_min= f_thresh_max;
1138 /* macroblock_analyse() doesn't further analyse skipped mbs,
1139 * so we have to guess their cost */
1141 i_intra_cost = i_intra_cost * i_mb / (i_mb - i_mb_s);
1143 if( i_gop_size < h->param.i_keyint_min / 4 )
1144 f_bias = f_thresh_min / 4;
1145 else if( i_gop_size <= h->param.i_keyint_min )
1146 f_bias = f_thresh_min * i_gop_size / h->param.i_keyint_min;
1149 f_bias = f_thresh_min
1150 + ( f_thresh_max - f_thresh_min )
1151 * ( i_gop_size - h->param.i_keyint_min )
1152 / ( h->param.i_keyint_max - h->param.i_keyint_min );
1154 f_bias = X264_MIN( f_bias, 1.0 );
1156 /* Bad P will be reencoded as I */
1157 if( i_mb_s < i_mb &&
1158 i_inter_cost >= (1.0 - f_bias) * i_intra_cost )
1159 /* i_mb_i >= (1.0 - f_bias) * i_mb ) */
1161 h->out.nal[h->out.i_nal-1].i_payload > h->i_last_intra_size +
1162 h->i_last_intra_size * (3+h->i_last_intra_qp - i_global_qp) / 16 &&
1163 i_mb_count[I_4x4] + i_mb_count[I_16x16] > i_mb_count[P_SKIP] + i_mb_count[P_L0]/2 &&
1164 h->out.nal[h->out.i_nal-1].i_payload > 2 * h->i_last_inter_size &&
1165 h->frames.i_last_i > 4)*/
1168 x264_log( h, X264_LOG_DEBUG, "scene cut at %d size=%d Icost:%.0f Pcost:%.0f ratio:%.3f bias=%.3f lastIDR:%d (I:%d P:%d Skip:%d)\n",
1170 h->out.nal[h->out.i_nal-1].i_payload,
1171 (double)i_intra_cost, (double)i_inter_cost,
1172 (double)i_inter_cost / i_intra_cost,
1174 i_mb_i, i_mb_p, i_mb_s );
1176 /* Restore frame num */
1179 for( i = 0; h->frames.current[i] && h->frames.current[i]->i_type == X264_TYPE_B; i++ );
1182 /* If using B-frames, force GOP to be closed.
1183 * Even if this frame is going to be I and not IDR, forcing a
1184 * P-frame before the scenecut will probably help compression.
1186 * We don't yet know exactly which frame is the scene cut, so
1187 * we can't assign an I-frame. Instead, change the previous
1188 * B-frame to P, and rearrange coding order. */
1190 x264_frame_t *tmp = h->frames.current[i-1];
1191 h->frames.current[i-1] = h->fenc;
1193 h->fenc->i_type = X264_TYPE_P;
1195 /* Do IDR if needed */
1196 else if( i_gop_size >= h->param.i_keyint_min )
1204 /* Reinit field of fenc */
1205 h->fenc->i_type = X264_TYPE_IDR;
1211 /* Put enqueued frames back in the pool */
1212 while( (tmp = x264_frame_get( h->frames.current ) ) != NULL )
1213 x264_frame_put( h->frames.next, tmp );
1214 x264_frame_sort( h->frames.next );
1218 h->fenc->i_type = X264_TYPE_I;
1222 h->i_last_inter_size = h->out.nal[h->out.i_nal-1].i_payload;
1226 h->i_last_intra_size = h->out.nal[h->out.i_nal-1].i_payload;
1227 h->i_last_intra_qp = i_global_qp;
1230 /* End bitstream, set output */
1231 *pi_nal = h->out.i_nal;
1232 *pp_nal = &h->out.nal[0];
1234 /* Set output picture properties */
1235 if( i_slice_type == SLICE_TYPE_I )
1236 pic->i_type = i_nal_type == NAL_SLICE_IDR ? X264_TYPE_IDR : X264_TYPE_I;
1237 else if( i_slice_type == SLICE_TYPE_P )
1238 pic->i_type = X264_TYPE_P;
1240 pic->i_type = X264_TYPE_B;
1241 pic->i_pts = h->fenc->i_pts;
1243 /* ---------------------- Update encoder state ------------------------- */
1245 if( h->param.b_cabac )
1247 x264_cabac_model_update( &h->cabac, i_slice_type, h->sh.pps->i_pic_init_qp + h->sh.i_qp_delta );
1250 /* handle references */
1251 if( i_nal_ref_idc != NAL_PRIORITY_DISPOSABLE )
1253 x264_reference_update( h );
1256 /* increase frame count */
1259 /* restore CPU state (before using float again) */
1260 /* XXX: not needed? (done above) */
1261 x264_cpu_restore( h->param.cpu );
1264 x264_ratecontrol_end( h, h->out.nal[h->out.i_nal-1].i_payload * 8 );
1266 x264_frame_put( h->frames.unused, h->fenc );
1268 TIMER_STOP( i_mtime_encode_frame );
1270 /* ---------------------- Compute/Print statistics --------------------- */
1272 h->stat.i_slice_count[i_slice_type]++;
1273 h->stat.i_slice_size[i_slice_type] += bs_pos( &h->out.bs) / 8;
1274 h->stat.i_slice_qp[i_slice_type] += i_global_qp;
1276 for( i = 0; i < 18; i++ )
1278 h->stat.i_mb_count[h->sh.i_type][i] += h->stat.frame.i_mb_count[i];
1281 if( h->param.analyse.b_psnr )
1283 int64_t i_sqe_y, i_sqe_u, i_sqe_v;
1286 i_sqe_y = x264_sqe( frame_psnr->plane[0], frame_psnr->i_stride[0], h->fenc->plane[0], h->fenc->i_stride[0], h->param.i_width, h->param.i_height );
1287 i_sqe_u = x264_sqe( frame_psnr->plane[1], frame_psnr->i_stride[1], h->fenc->plane[1], h->fenc->i_stride[1], h->param.i_width/2, h->param.i_height/2);
1288 i_sqe_v = x264_sqe( frame_psnr->plane[2], frame_psnr->i_stride[2], h->fenc->plane[2], h->fenc->i_stride[2], h->param.i_width/2, h->param.i_height/2);
1290 h->stat.i_sqe_global[i_slice_type] += i_sqe_y + i_sqe_u + i_sqe_v;
1291 h->stat.f_psnr_average[i_slice_type] += x264_psnr( i_sqe_y + i_sqe_u + i_sqe_v, 3 * h->param.i_width * h->param.i_height / 2 );
1292 h->stat.f_psnr_mean_y[i_slice_type] += x264_psnr( i_sqe_y, h->param.i_width * h->param.i_height );
1293 h->stat.f_psnr_mean_u[i_slice_type] += x264_psnr( i_sqe_u, h->param.i_width * h->param.i_height / 4 );
1294 h->stat.f_psnr_mean_v[i_slice_type] += x264_psnr( i_sqe_v, h->param.i_width * h->param.i_height / 4 );
1296 snprintf( psz_message, 80, " PSNR Y:%2.2f U:%2.2f V:%2.2f",
1297 x264_psnr( i_sqe_y, h->param.i_width * h->param.i_height ),
1298 x264_psnr( i_sqe_u, h->param.i_width * h->param.i_height / 4),
1299 x264_psnr( i_sqe_v, h->param.i_width * h->param.i_height / 4) );
1300 psz_message[79] = '\0';
1304 psz_message[0] = '\0';
1307 x264_log( h, X264_LOG_DEBUG,
1308 "frame=%4d QP=%i NAL=%d Slice:%c Poc:%-3d I4x4:%-4d I16x16:%-4d P:%-4d SKIP:%-4d size=%d bytes%s\n",
1312 i_slice_type == SLICE_TYPE_I ? 'I' : (i_slice_type == SLICE_TYPE_P ? 'P' : 'B' ),
1314 h->stat.frame.i_mb_count[I_4x4],
1315 h->stat.frame.i_mb_count[I_16x16],
1316 h->stat.frame.i_mb_count_p,
1317 h->stat.frame.i_mb_count_skip,
1318 h->out.nal[h->out.i_nal-1].i_payload,
1322 #ifdef DEBUG_MB_TYPE
1324 static const char mb_chars[] = { 'i', 'I', 'C', 'P', '8', 'S',
1325 'D', '<', 'X', 'B', 'X', '>', 'B', 'B', 'B', 'B', '8', 'S' };
1327 for( mb_xy = 0; mb_xy < h->sps->i_mb_width * h->sps->i_mb_height; mb_xy++ )
1329 if( h->mb.type[mb_xy] < 18 && h->mb.type[mb_xy] >= 0 )
1330 fprintf( stderr, "%c ", mb_chars[ h->mb.type[mb_xy] ] );
1332 fprintf( stderr, "? " );
1334 if( (mb_xy+1) % h->sps->i_mb_width == 0 )
1335 fprintf( stderr, "\n" );
1340 #ifdef DEBUG_DUMP_FRAME
1341 /* Dump reconstructed frame */
1342 x264_frame_dump( h, frame_psnr, "fdec.yuv" );
1347 x264_frame_dump( h, h->fref0[0], "ref0.yuv" );
1351 x264_frame_dump( h, h->fref1[0], "ref1.yuv" );
1357 /****************************************************************************
1358 * x264_encoder_close:
1359 ****************************************************************************/
1360 void x264_encoder_close ( x264_t *h )
1362 #ifdef DEBUG_BENCHMARK
1363 int64_t i_mtime_total = i_mtime_analyse + i_mtime_encode + i_mtime_write + i_mtime_filter + 1;
1365 int64_t i_yuv_size = 3 * h->param.i_width * h->param.i_height / 2;
1368 #ifdef DEBUG_BENCHMARK
1369 x264_log( h, X264_LOG_INFO,
1370 "analyse=%d(%lldms) encode=%d(%lldms) write=%d(%lldms) filter=%d(%lldms)\n",
1371 (int)(100*i_mtime_analyse/i_mtime_total), i_mtime_analyse/1000,
1372 (int)(100*i_mtime_encode/i_mtime_total), i_mtime_encode/1000,
1373 (int)(100*i_mtime_write/i_mtime_total), i_mtime_write/1000,
1374 (int)(100*i_mtime_filter/i_mtime_total), i_mtime_filter/1000 );
1377 /* Slices used and PSNR */
1378 for( i=0; i<5; i++ )
1380 static const int slice_order[] = { SLICE_TYPE_I, SLICE_TYPE_SI, SLICE_TYPE_P, SLICE_TYPE_SP, SLICE_TYPE_B };
1381 static const char *slice_name[] = { "P", "B", "I", "SP", "SI" };
1382 int i_slice = slice_order[i];
1384 if( h->stat.i_slice_count[i_slice] > 0 )
1386 const int i_count = h->stat.i_slice_count[i_slice];
1387 if( h->param.analyse.b_psnr )
1389 x264_log( h, X264_LOG_INFO,
1390 "slice %s:%-4d Avg QP:%5.2f Avg size:%6.0f PSNR Mean Y:%5.2f U:%5.2f V:%5.2f Avg:%5.2f Global:%5.2f MSE*Size:%5.3f\n",
1391 slice_name[i_slice],
1393 (double)h->stat.i_slice_qp[i_slice] / i_count,
1394 (double)h->stat.i_slice_size[i_slice] / i_count,
1395 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,
1396 h->stat.f_psnr_average[i_slice] / i_count,
1397 x264_psnr( h->stat.i_sqe_global[i_slice], i_count * i_yuv_size ),
1398 x264_mse( h->stat.i_sqe_global[i_slice], i_count * i_yuv_size ) * h->stat.i_slice_size[i_slice] / i_count );
1402 x264_log( h, X264_LOG_INFO,
1403 "slice %s:%-4d Avg QP:%5.2f Avg size:%6.0f\n",
1404 slice_name[i_slice],
1406 (double)h->stat.i_slice_qp[i_slice] / i_count,
1407 (double)h->stat.i_slice_size[i_slice] / i_count );
1413 if( h->stat.i_slice_count[SLICE_TYPE_I] > 0 )
1415 const int64_t *i_mb_count = h->stat.i_mb_count[SLICE_TYPE_I];
1416 const double i_count = h->stat.i_slice_count[SLICE_TYPE_I] * h->mb.i_mb_count / 100.0;
1417 x264_log( h, X264_LOG_INFO,
1418 "slice I Avg I4x4:%.1f%% I16x16:%.1f%%\n",
1419 i_mb_count[I_4x4] / i_count,
1420 i_mb_count[I_16x16]/ i_count );
1422 if( h->stat.i_slice_count[SLICE_TYPE_P] > 0 )
1424 const int64_t *i_mb_count = h->stat.i_mb_count[SLICE_TYPE_P];
1425 const double i_count = h->stat.i_slice_count[SLICE_TYPE_P] * h->mb.i_mb_count / 100.0;
1426 x264_log( h, X264_LOG_INFO,
1427 "slice P Avg I4x4:%.1f%% I16x16:%.1f%% P:%.1f%% P8x8:%.1f%% PSKIP:%.1f%%\n",
1428 i_mb_count[I_4x4] / i_count,
1429 i_mb_count[I_16x16]/ i_count,
1430 i_mb_count[P_L0] / i_count,
1431 i_mb_count[P_8x8] / i_count,
1432 i_mb_count[P_SKIP] / i_count );
1434 if( h->stat.i_slice_count[SLICE_TYPE_B] > 0 )
1436 const int64_t *i_mb_count = h->stat.i_mb_count[SLICE_TYPE_B];
1437 const double i_count = h->stat.i_slice_count[SLICE_TYPE_B] * h->mb.i_mb_count / 100.0;
1438 x264_log( h, X264_LOG_INFO,
1439 "slice B Avg I4x4:%.1f%% I16x16:%.1f%% P:%.1f%% B:%.1f%% B8x8:%.1f%% DIRECT:%.1f%% BSKIP:%.1f%%\n",
1440 i_mb_count[I_4x4] / i_count,
1441 i_mb_count[I_16x16] / i_count,
1442 (i_mb_count[B_L0_L0] + i_mb_count[B_L1_L1] + i_mb_count[B_L1_L0] + i_mb_count[B_L0_L1]) / i_count,
1443 (i_mb_count[B_BI_BI] + i_mb_count[B_L0_BI] + i_mb_count[B_L1_BI] + i_mb_count[B_BI_L0] + i_mb_count[B_BI_L1]) / i_count,
1444 i_mb_count[B_8x8] / i_count,
1445 i_mb_count[B_DIRECT] / i_count,
1446 i_mb_count[B_SKIP] / i_count );
1449 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 )
1451 const int i_count = h->stat.i_slice_count[SLICE_TYPE_I] +
1452 h->stat.i_slice_count[SLICE_TYPE_P] +
1453 h->stat.i_slice_count[SLICE_TYPE_B];
1454 float fps = (float) h->param.i_fps_num / h->param.i_fps_den;
1456 if( h->param.analyse.b_psnr )
1457 x264_log( h, X264_LOG_INFO,
1458 "PSNR Mean Y:%5.2f U:%5.2f V:%5.2f Avg:%5.2f Global:%5.2f kb/s:%.1f\n",
1459 (h->stat.f_psnr_mean_y[SLICE_TYPE_I] + h->stat.f_psnr_mean_y[SLICE_TYPE_P] + h->stat.f_psnr_mean_y[SLICE_TYPE_B]) / i_count,
1460 (h->stat.f_psnr_mean_u[SLICE_TYPE_I] + h->stat.f_psnr_mean_u[SLICE_TYPE_P] + h->stat.f_psnr_mean_u[SLICE_TYPE_B]) / i_count,
1461 (h->stat.f_psnr_mean_v[SLICE_TYPE_I] + h->stat.f_psnr_mean_v[SLICE_TYPE_P] + h->stat.f_psnr_mean_v[SLICE_TYPE_B]) / i_count,
1463 (h->stat.f_psnr_average[SLICE_TYPE_I] + h->stat.f_psnr_average[SLICE_TYPE_P] + h->stat.f_psnr_average[SLICE_TYPE_B]) / i_count,
1465 x264_psnr( h->stat.i_sqe_global[SLICE_TYPE_I] + h->stat.i_sqe_global[SLICE_TYPE_P]+ h->stat.i_sqe_global[SLICE_TYPE_B],
1466 i_count * i_yuv_size ),
1467 fps * 8*(h->stat.i_slice_size[SLICE_TYPE_I]+h->stat.i_slice_size[SLICE_TYPE_P]+h->stat.i_slice_size[SLICE_TYPE_B]) / i_count / 1000 );
1469 x264_log( h, X264_LOG_INFO,
1471 fps * 8*(h->stat.i_slice_size[SLICE_TYPE_I]+h->stat.i_slice_size[SLICE_TYPE_P]+h->stat.i_slice_size[SLICE_TYPE_B]) / i_count / 1000 );
1475 for( i = 0; i < X264_BFRAME_MAX + 1; i++ )
1477 if( h->frames.current[i] ) x264_frame_delete( h->frames.current[i] );
1478 if( h->frames.next[i] ) x264_frame_delete( h->frames.next[i] );
1479 if( h->frames.unused[i] ) x264_frame_delete( h->frames.unused[i] );
1482 for( i = 0; i < h->param.i_frame_reference+2; i++ )
1484 x264_frame_delete( h->frames.reference[i] );
1488 x264_ratecontrol_delete( h );
1491 if( h->param.rc.psz_stat_out )
1492 free( h->param.rc.psz_stat_out );
1493 if( h->param.rc.psz_stat_in )
1494 free( h->param.rc.psz_stat_in );
1495 if( h->param.rc.psz_rc_eq )
1496 free( h->param.rc.psz_rc_eq );
1498 x264_macroblock_cache_end( h );
1499 x264_free( h->out.p_bitstream );