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 *****************************************************************************/
31 #include "../core/common.h"
32 #include "../core/cpu.h"
36 #include "ratecontrol.h"
37 #include "macroblock.h"
39 //#define DEBUG_MB_TYPE
40 #define DEBUG_DUMP_FRAME 1
42 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;\
57 /****************************************************************************
59 ******************************* x264 libs **********************************
61 ****************************************************************************/
64 static float x264_psnr( uint8_t *pix1, int i_pix_stride, uint8_t *pix2, int i_pix2_stride, int i_width, int i_height )
70 for( y = 0; y < i_height; y++ )
72 for( x = 0; x < i_width; x++ )
76 tmp = pix1[y*i_pix_stride+x] - pix2[y*i_pix2_stride+x];
86 return (float)(10.0 * log( (double)65025.0 * (double)i_height * (double)i_width / (double)i_sqe ) / log( 10.0 ));
90 static void x264_frame_dump( x264_t *h, x264_frame_t *fr, char *name )
92 FILE * f = fopen( name, "a" );
95 fseek( f, 0, SEEK_END );
97 for( i = 0; i < fr->i_plane; i++ )
99 for( y = 0; y < h->param.i_height / ( i == 0 ? 1 : 2 ); y++ )
101 fwrite( &fr->plane[i][y*fr->i_stride[i]], 1, h->param.i_width / ( i == 0 ? 1 : 2 ), f );
109 /* Fill "default" values */
110 static void x264_slice_header_init( x264_slice_header_t *sh, x264_param_t *param,
111 x264_sps_t *sps, x264_pps_t *pps,
112 int i_type, int i_idr_pic_id, int i_frame )
114 /* First we fill all field */
120 sh->i_pps_id = pps->i_id;
122 sh->i_frame_num = i_frame;
124 sh->b_field_pic = 0; /* Not field support for now */
125 sh->b_bottom_field = 1; /* not yet used */
127 sh->i_idr_pic_id = i_idr_pic_id;
129 /* poc stuff, fixed later */
131 sh->i_delta_poc_bottom = 0;
132 sh->i_delta_poc[0] = 0;
133 sh->i_delta_poc[1] = 0;
135 sh->i_redundant_pic_cnt = 0;
137 sh->b_direct_spatial_mv_pred = 1;
139 sh->b_num_ref_idx_override = 0;
140 sh->i_num_ref_idx_l0_active = 1;
141 sh->i_num_ref_idx_l1_active = 1;
143 sh->i_cabac_init_idc = param->i_cabac_init_idc;
146 sh->b_sp_for_swidth = 0;
149 if( param->b_deblocking_filter )
151 sh->i_disable_deblocking_filter_idc = 0;
155 sh->i_disable_deblocking_filter_idc = 1;
157 sh->i_alpha_c0_offset = param->i_deblocking_filter_alphac0 << 1;
158 sh->i_beta_offset = param->i_deblocking_filter_beta << 1;
161 static void x264_slice_header_write( bs_t *s, x264_slice_header_t *sh, int i_nal_ref_idc )
163 bs_write_ue( s, sh->i_first_mb );
164 bs_write_ue( s, sh->i_type + 5 ); /* same type things */
165 bs_write_ue( s, sh->i_pps_id );
166 bs_write( s, sh->sps->i_log2_max_frame_num, sh->i_frame_num );
168 if( sh->i_idr_pic_id >= 0 ) /* NAL IDR */
170 bs_write_ue( s, sh->i_idr_pic_id );
173 if( sh->sps->i_poc_type == 0 )
175 bs_write( s, sh->sps->i_log2_max_poc_lsb, sh->i_poc_lsb );
176 if( sh->pps->b_pic_order && !sh->b_field_pic )
178 bs_write_se( s, sh->i_delta_poc_bottom );
181 else if( sh->sps->i_poc_type == 1 && !sh->sps->b_delta_pic_order_always_zero )
183 bs_write_se( s, sh->i_delta_poc[0] );
184 if( sh->pps->b_pic_order && !sh->b_field_pic )
186 bs_write_se( s, sh->i_delta_poc[1] );
190 if( sh->pps->b_redundant_pic_cnt )
192 bs_write_ue( s, sh->i_redundant_pic_cnt );
195 if( sh->i_type == SLICE_TYPE_B )
197 bs_write1( s, sh->b_direct_spatial_mv_pred );
199 if( sh->i_type == SLICE_TYPE_P || sh->i_type == SLICE_TYPE_SP || sh->i_type == SLICE_TYPE_B )
201 bs_write1( s, sh->b_num_ref_idx_override );
202 if( sh->b_num_ref_idx_override )
204 bs_write_ue( s, sh->i_num_ref_idx_l0_active - 1 );
205 if( sh->i_type == SLICE_TYPE_B )
207 bs_write_ue( s, sh->i_num_ref_idx_l1_active - 1 );
212 /* ref pic list reordering */
213 if( sh->i_type != SLICE_TYPE_I )
215 int b_ref_pic_list_reordering_l0 = 0;
216 bs_write1( s, b_ref_pic_list_reordering_l0 );
217 if( b_ref_pic_list_reordering_l0 )
222 if( sh->i_type == SLICE_TYPE_B )
224 int b_ref_pic_list_reordering_l1 = 0;
225 bs_write1( s, b_ref_pic_list_reordering_l1 );
226 if( b_ref_pic_list_reordering_l1 )
232 if( ( sh->pps->b_weighted_pred && ( sh->i_type == SLICE_TYPE_P || sh->i_type == SLICE_TYPE_SP ) ) ||
233 ( sh->pps->b_weighted_bipred == 1 && sh->i_type == SLICE_TYPE_B ) )
238 if( i_nal_ref_idc != 0 )
240 if( sh->i_idr_pic_id >= 0 )
242 bs_write1( s, 0 ); /* no output of prior pics flag */
243 bs_write1( s, 0 ); /* long term reference flag */
247 bs_write1( s, 0 ); /* adaptive_ref_pic_marking_mode_flag */
252 if( sh->pps->b_cabac && sh->i_type != SLICE_TYPE_I )
254 bs_write_ue( s, sh->i_cabac_init_idc );
256 bs_write_se( s, sh->i_qp_delta ); /* slice qp delta */
258 if( sh->i_type == SLICE_TYPE_SP || sh->i_type == SLICE_TYPE_SI )
260 if( sh->i_type == SLICE_TYPE_SP )
262 bs_write1( s, sh->b_sp_for_swidth );
264 bs_write_se( s, sh->i_qs_delta );
268 if( sh->pps->b_deblocking_filter_control )
270 bs_write_ue( s, sh->i_disable_deblocking_filter_idc );
271 if( sh->i_disable_deblocking_filter_idc != 1 )
273 bs_write_se( s, sh->i_alpha_c0_offset >> 1 );
274 bs_write_se( s, sh->i_beta_offset >> 1 );
279 /****************************************************************************
281 ****************************************************************************
282 ****************************** External API*********************************
283 ****************************************************************************
285 ****************************************************************************/
287 /****************************************************************************
289 ****************************************************************************/
290 x264_t *x264_encoder_open ( x264_param_t *param )
292 x264_t *h = x264_malloc( sizeof( x264_t ) );
295 /* Check parameters validity */
296 if( param->i_width <= 0 || param->i_height <= 0 )
298 fprintf( stderr, "invalid width x height (%dx%d)\n",
299 param->i_width, param->i_height );
304 if( param->i_width % 16 != 0 || param->i_height % 16 != 0 )
306 fprintf( stderr, "width %% 16 != 0 pr height %% 16 != 0 (%dx%d)\n",
307 param->i_width, param->i_height );
311 if( param->i_csp != X264_CSP_I420 )
313 fprintf( stderr, "invalid CSP (only I420 supported)\n" );
318 /* Fix parameters values */
319 memcpy( &h->param, param, sizeof( x264_param_t ) );
320 h->param.i_frame_reference = x264_clip3( h->param.i_frame_reference, 1, 15 );
321 if( h->param.i_idrframe <= 0 )
323 h->param.i_idrframe = 1;
325 if( h->param.i_iframe <= 0 )
327 h->param.i_iframe = 1;
329 h->param.i_bframe = x264_clip3( h->param.i_bframe , 0, X264_BFRAME_MAX );
331 if( h->param.i_bframe > 0 && h->param.b_cabac )
333 fprintf( stderr, "cabac not supported with B frame (cabac disabled)\n" );
334 h->param.b_cabac = 0;
338 h->param.i_deblocking_filter_alphac0 = x264_clip3( h->param.i_deblocking_filter_alphac0, -6, 6 );
339 h->param.i_deblocking_filter_beta = x264_clip3( h->param.i_deblocking_filter_beta, -6, 6 );
341 h->param.i_cabac_init_idc = x264_clip3( h->param.i_cabac_init_idc, -1, 2 );
345 h->out.i_bitstream = 1000000; /* FIXME estimate max size (idth/height) */
346 h->out.p_bitstream = x264_malloc( h->out.i_bitstream );
353 h->sps = &h->sps_array[0];
354 x264_sps_init( h->sps, 0, &h->param );
356 h->pps = &h->pps_array[0];
357 x264_pps_init( h->pps, 0, &h->param, h->sps);
360 for( i = 0; i < X264_BFRAME_MAX + 1; i++ )
362 h->frames.current[i] = NULL;
363 h->frames.next[i] = NULL;
364 h->frames.unused[i] = NULL;
366 for( i = 0; i < 1 + h->param.i_bframe; i++ )
368 h->frames.unused[i] = x264_frame_new( h );
370 for( i = 0; i < 2 + h->param.i_frame_reference; i++ )
372 /* 2 = 1 backward ref + 1 fdec */
373 h->frames.reference[i] = x264_frame_new( h );
375 h->frames.i_last_idr = h->param.i_idrframe;
376 h->frames.i_last_i = h->param.i_iframe;
381 h->fdec = h->frames.reference[0];
384 x264_macroblock_cache_init( h );
386 /* init cabac adaptive model */
387 x264_cabac_model_init( &h->cabac );
389 /* init CPU functions */
390 x264_predict_16x16_init( h->param.cpu, h->predict_16x16 );
391 x264_predict_8x8_init( h->param.cpu, h->predict_8x8 );
392 x264_predict_4x4_init( h->param.cpu, h->predict_4x4 );
394 x264_pixel_init( h->param.cpu, &h->pixf );
395 x264_dct_init( h->param.cpu, &h->dctf );
396 x264_mc_init( h->param.cpu, h->mc );
397 x264_csp_init( h->param.cpu, h->param.i_csp, &h->csp );
400 h->rc = x264_ratecontrol_new( &h->param );
403 h->stat.i_slice_count[SLICE_TYPE_I] = 0;
404 h->stat.i_slice_count[SLICE_TYPE_P] = 0;
405 h->stat.i_slice_count[SLICE_TYPE_B] = 0;
406 h->stat.i_slice_size[SLICE_TYPE_I] = 0;
407 h->stat.i_slice_size[SLICE_TYPE_P] = 0;
408 h->stat.i_slice_size[SLICE_TYPE_B] = 0;
410 h->stat.f_psnr_y[SLICE_TYPE_I] = 0.0; h->stat.f_psnr_u[SLICE_TYPE_I] = 0.0; h->stat.f_psnr_v[SLICE_TYPE_I] = 0.0;
411 h->stat.f_psnr_y[SLICE_TYPE_P] = 0.0; h->stat.f_psnr_u[SLICE_TYPE_P] = 0.0; h->stat.f_psnr_v[SLICE_TYPE_P] = 0.0;
412 h->stat.f_psnr_y[SLICE_TYPE_B] = 0.0; h->stat.f_psnr_u[SLICE_TYPE_B] = 0.0; h->stat.f_psnr_v[SLICE_TYPE_B] = 0.0;
414 for( i = 0; i < 17; i++ )
416 h->stat.i_mb_count[SLICE_TYPE_I][i] = 0;
417 h->stat.i_mb_count[SLICE_TYPE_P][i] = 0;
418 h->stat.i_mb_count[SLICE_TYPE_B][i] = 0;
424 static void x264_nal_start( x264_t *h, int i_type, int i_ref_idc )
426 x264_nal_t *nal = &h->out.nal[h->out.i_nal];
428 nal->i_ref_idc = i_ref_idc;
429 nal->i_type = i_type;
431 bs_align_0( &h->out.bs ); /* not needed */
434 nal->p_payload= &h->out.p_bitstream[bs_pos( &h->out.bs) / 8];
436 static void x264_nal_end( x264_t *h )
438 x264_nal_t *nal = &h->out.nal[h->out.i_nal];
440 bs_align_0( &h->out.bs ); /* not needed */
442 nal->i_payload = &h->out.p_bitstream[bs_pos( &h->out.bs)/8] - nal->p_payload;
447 /****************************************************************************
448 * x264_encoder_headers:
449 ****************************************************************************/
450 int x264_encoder_headers( x264_t *h, x264_nal_t **pp_nal, int *pi_nal )
452 /* init bitstream context */
454 bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream );
456 /* Put SPS and PPS */
457 if( h->i_frame == 0 )
459 /* generate sequence parameters */
460 x264_nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST );
461 x264_sps_write( &h->out.bs, h->sps );
464 /* generate picture parameters */
465 x264_nal_start( h, NAL_PPS, NAL_PRIORITY_HIGHEST );
466 x264_pps_write( &h->out.bs, h->pps );
470 *pi_nal = h->out.i_nal;
471 *pp_nal = &h->out.nal[0];
477 static void x264_frame_put( x264_frame_t *list[X264_BFRAME_MAX], x264_frame_t *frame )
481 while( list[i] != NULL ) i++;
486 static x264_frame_t *x264_frame_get( x264_frame_t *list[X264_BFRAME_MAX+1] )
488 x264_frame_t *frame = list[0];
491 for( i = 0; i < X264_BFRAME_MAX; i++ )
495 list[X264_BFRAME_MAX] = NULL;
500 static inline void x264_reference_build_list( x264_t *h, int i_poc )
505 /* build ref list 0/1 */
508 for( i = 1; i < h->param.i_frame_reference+2; i++ )
510 if( h->frames.reference[i]->i_poc >= 0 )
512 if( h->frames.reference[i]->i_poc < i_poc )
514 h->fref0[h->i_ref0++] = h->frames.reference[i];
516 else if( h->frames.reference[i]->i_poc > i_poc )
518 h->fref1[h->i_ref1++] = h->frames.reference[i];
522 /* Order ref0 from higher to lower poc */
526 for( i = 0; i < h->i_ref0 - 1; i++ )
528 if( h->fref0[i]->i_poc < h->fref0[i+1]->i_poc )
530 x264_frame_t *tmp = h->fref0[i+1];
532 h->fref0[i+1] = h->fref0[i];
539 /* Order ref1 from lower to higher poc (bubble sort) for B-frame */
543 for( i = 0; i < h->i_ref1 - 1; i++ )
545 if( h->fref1[i]->i_poc > h->fref1[i+1]->i_poc )
547 x264_frame_t *tmp = h->fref1[i+1];
549 h->fref1[i+1] = h->fref1[i];
557 if( h->i_ref0 > h->param.i_frame_reference )
559 h->i_ref0 = h->param.i_frame_reference;
567 static inline void x264_reference_update( x264_t *h )
571 /* apply deblocking filter to the current decoded picture */
572 if( h->param.b_deblocking_filter )
574 TIMER_START( i_mtime_filter );
575 x264_frame_deblocking_filter( h, h->sh.i_type );
576 TIMER_STOP( i_mtime_filter );
579 x264_frame_expand_border( h->fdec );
581 /* move frame in the buffer */
582 h->fdec = h->frames.reference[h->param.i_frame_reference+1];
583 for( i = h->param.i_frame_reference+1; i > 0; i-- )
585 h->frames.reference[i] = h->frames.reference[i-1];
587 h->frames.reference[0] = h->fdec;
590 static inline void x264_reference_reset( x264_t *h )
594 /* reset ref pictures */
595 for( i = 1; i < h->param.i_frame_reference+2; i++ )
597 h->frames.reference[i]->i_poc = -1;
599 h->frames.reference[0]->i_poc = 0;
602 static inline void x264_slice_init( x264_t *h, int i_nal_type, int i_slice_type, int i_global_qp )
604 /* ------------------------ Create slice header ----------------------- */
605 if( i_nal_type == NAL_SLICE_IDR )
607 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 );
610 h->i_idr_pic_id = ( h->i_idr_pic_id + 1 ) % 65535;
614 x264_slice_header_init( &h->sh, &h->param, h->sps, h->pps, i_slice_type, -1, h->i_frame_num - 1 );
616 /* always set the real higher num of ref frame used */
617 h->sh.b_num_ref_idx_override = 1;
618 h->sh.i_num_ref_idx_l0_active = h->i_ref0 <= 0 ? 1 : h->i_ref0;
619 h->sh.i_num_ref_idx_l1_active = h->i_ref1 <= 0 ? 1 : h->i_ref1;
622 if( h->sps->i_poc_type == 0 )
624 h->sh.i_poc_lsb = h->fdec->i_poc & ( (1 << h->sps->i_log2_max_poc_lsb) - 1 );
625 h->sh.i_delta_poc_bottom = 0; /* XXX won't work for field */
627 else if( h->sps->i_poc_type == 1 )
629 /* FIXME TODO FIXME */
633 /* Nothing to do ? */
637 h->sh.i_qp_delta = i_global_qp - h->pps->i_pic_init_qp;
639 /* get adapative cabac model if needed */
640 if( h->param.b_cabac )
642 if( h->param.i_cabac_init_idc == -1 )
644 h->sh.i_cabac_init_idc = x264_cabac_model_get( &h->cabac, i_slice_type );
649 static inline void x264_slice_write( x264_t *h, int i_nal_type, int i_nal_ref_idc, int i_mb_count[18] )
656 for( i = 0; i < 17; i++ ) i_mb_count[i] = 0;
659 x264_nal_start( h, i_nal_type, i_nal_ref_idc );
662 x264_slice_header_write( &h->out.bs, &h->sh, i_nal_ref_idc );
663 if( h->param.b_cabac )
665 /* alignement needed */
666 bs_align_1( &h->out.bs );
669 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 );
670 x264_cabac_encode_init ( &h->cabac, &h->out.bs );
672 h->mb.i_last_qp = h->pps->i_pic_init_qp + h->sh.i_qp_delta;
673 h->mb.i_last_dqp = 0;
675 for( mb_xy = 0, i_skip = 0; mb_xy < h->sps->i_mb_width * h->sps->i_mb_height; mb_xy++ )
677 const int i_mb_y = mb_xy / h->sps->i_mb_width;
678 const int i_mb_x = mb_xy % h->sps->i_mb_width;
681 x264_macroblock_cache_load( h, i_mb_x, i_mb_y );
683 /* analyse parameters
684 * Slice I: choose I_4x4 or I_16x16 mode
685 * Slice P: choose between using P mode or intra (4x4 or 16x16)
687 TIMER_START( i_mtime_analyse );
688 x264_macroblock_analyse( h );
689 TIMER_STOP( i_mtime_analyse );
691 /* encode this macrobock -> be carefull it can change the mb type to P_SKIP if needed */
692 TIMER_START( i_mtime_encode );
693 x264_macroblock_encode( h );
694 TIMER_STOP( i_mtime_encode );
696 TIMER_START( i_mtime_write );
697 if( IS_SKIP( h->mb.i_type ) )
699 if( h->param.b_cabac )
703 /* not end_of_slice_flag */
704 x264_cabac_encode_terminal( &h->cabac, 0 );
707 x264_cabac_mb_skip( h, 1 );
716 if( h->param.b_cabac )
720 /* not end_of_slice_flag */
721 x264_cabac_encode_terminal( &h->cabac, 0 );
723 if( h->sh.i_type != SLICE_TYPE_I )
725 x264_cabac_mb_skip( h, 0 );
727 x264_macroblock_write_cabac( h, &h->out.bs );
731 if( h->sh.i_type != SLICE_TYPE_I )
733 bs_write_ue( &h->out.bs, i_skip ); /* skip run */
736 x264_macroblock_write_cavlc( h, &h->out.bs );
739 TIMER_STOP( i_mtime_write );
742 x264_macroblock_cache_save( h );
744 i_mb_count[h->mb.i_type]++;
747 if( h->param.b_cabac )
750 x264_cabac_encode_terminal( &h->cabac, 1 );
752 else if( i_skip > 0 )
754 bs_write_ue( &h->out.bs, i_skip ); /* last skip run */
757 if( h->param.b_cabac )
760 x264_cabac_encode_flush( &h->cabac );
761 /* TODO cabac stuffing things (p209) */
762 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;
764 while( i_cabac_word > 0 )
766 bs_write( &h->out.bs, 16, 0x0000 );
772 /* rbsp_slice_trailing_bits */
773 bs_rbsp_trailing( &h->out.bs );
779 /****************************************************************************
780 * x264_encoder_encode:
781 * XXX: i_poc : is the poc of the current given picture
782 * i_frame : is the number of the frame being coded
791 ****************************************************************************/
792 int x264_encoder_encode( x264_t *h,
793 x264_nal_t **pp_nal, int *pi_nal,
794 x264_picture_t *pic )
796 x264_frame_t *frame_psnr = h->fdec; /* just to kept the current decoded frame for psnr calculation */
803 float psnr_y, psnr_u, psnr_v;
814 /* ------------------- Setup new frame from picture -------------------- */
815 TIMER_START( i_mtime_encode_frame );
818 /* Copy the picture to a frame, init the frame and move it to a buffer */
820 x264_frame_t *fenc = x264_frame_get( h->frames.unused );
822 x264_frame_copy_picture( h, fenc, pic );
824 /* 2: get its type */
825 if( ( h->frames.i_last_i + 1 >= h->param.i_iframe && h->frames.i_last_idr + 1 >= h->param.i_idrframe ) ||
826 pic->i_type == X264_TYPE_IDR )
829 fenc->i_type = X264_TYPE_IDR;
834 /* Last schedule B frames need to be encoded as P */
835 if( h->frames.next[0] != NULL )
840 while( h->frames.next[i+1] != NULL ) i++;
841 h->frames.next[i]->i_type = X264_TYPE_P;
843 /* remove this P from next */
844 tmp = h->frames.next[i];
845 h->frames.next[i] = NULL;
847 /* move this P + Bs to current */
848 x264_frame_put( h->frames.current, tmp );
849 while( ( tmp = x264_frame_get( h->frames.next ) ) )
851 x264_frame_put( h->frames.current, tmp );
855 else if( h->param.i_bframe > 0 )
857 if( h->frames.i_last_i + 1 >= h->param.i_iframe )
858 fenc->i_type = X264_TYPE_I;
859 else if( h->frames.next[h->param.i_bframe-1] != NULL )
860 fenc->i_type = X264_TYPE_P;
861 else if( pic->i_type == X264_TYPE_AUTO )
862 fenc->i_type = X264_TYPE_B;
864 fenc->i_type = pic->i_type;
868 if( pic->i_type == X264_TYPE_AUTO )
870 if( h->frames.i_last_i + 1 >= h->param.i_iframe )
871 fenc->i_type = X264_TYPE_I;
873 fenc->i_type = X264_TYPE_P;
877 fenc->i_type = pic->i_type;
881 fenc->i_poc = h->i_poc;
882 if( fenc->i_type == X264_TYPE_IDR )
884 h->frames.i_last_idr = 0;
885 h->frames.i_last_i = 0;
887 else if( fenc->i_type == X264_TYPE_I )
889 h->frames.i_last_idr++;
890 h->frames.i_last_i = 0;
894 h->frames.i_last_i++;
897 /* 3: Update current/next */
898 if( fenc->i_type == X264_TYPE_B )
900 x264_frame_put( h->frames.next, fenc );
904 x264_frame_put( h->frames.current, fenc );
905 while( ( fenc = x264_frame_get( h->frames.next ) ) )
907 x264_frame_put( h->frames.current, fenc );
912 else /* No more picture, begin encoding of last frames */
914 /* Move all next frames to current and mark the last one as a P */
917 while( h->frames.next[i+1] != NULL ) i++;
920 h->frames.next[i]->i_type = X264_TYPE_P;
921 tmp = h->frames.next[i];
922 h->frames.next[i] = NULL;
924 x264_frame_put( h->frames.current, tmp );
925 while( ( tmp = x264_frame_get( h->frames.next ) ) )
927 x264_frame_put( h->frames.current, tmp );
931 TIMER_STOP( i_mtime_encode_frame );
933 /* ------------------- Get frame to be encoded ------------------------- */
934 /* 4: get picture to encode */
935 h->fenc = x264_frame_get( h->frames.current );
936 if( h->fenc == NULL )
938 /* Nothing yet to encode (ex: waiting for I/P with B frames) */
939 /* waiting for filling bframe buffer */
940 pic->i_type = X264_TYPE_AUTO;
943 x264_frame_put( h->frames.unused, h->fenc ); /* Safe to do it now, we don't use frames.unused for the rest */
945 /* ------------------- Setup frame context ----------------------------- */
946 /* 5: Init data dependant of frame type */
947 TIMER_START( i_mtime_encode_frame );
948 if( h->fenc->i_type == X264_TYPE_IDR )
950 /* reset ref pictures */
951 x264_reference_reset( h );
953 i_nal_type = NAL_SLICE_IDR;
954 i_nal_ref_idc = NAL_PRIORITY_HIGHEST;
955 i_slice_type = SLICE_TYPE_I;
957 else if( h->fenc->i_type == X264_TYPE_I )
959 i_nal_type = NAL_SLICE;
960 i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/
961 i_slice_type = SLICE_TYPE_I;
963 else if( h->fenc->i_type == X264_TYPE_P )
965 i_nal_type = NAL_SLICE;
966 i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/
967 i_slice_type = SLICE_TYPE_P;
971 i_nal_type = NAL_SLICE;
972 i_nal_ref_idc = NAL_PRIORITY_DISPOSABLE;
973 i_slice_type = SLICE_TYPE_B;
977 h->fdec->i_type = h->fenc->i_type;
978 h->fdec->i_poc = h->fenc->i_poc;
982 /* ------------------- Init ----------------------------- */
983 /* Init the rate control */
984 x264_ratecontrol_start( h->rc, i_slice_type );
985 i_global_qp = x264_ratecontrol_qp( h->rc );
986 if( h->fenc->i_qpplus1 > 0 )
988 i_global_qp = x264_clip3( h->fenc->i_qpplus1 - 1, 0, 51 );
991 /* build ref list 0/1 */
992 x264_reference_build_list( h, h->fdec->i_poc );
994 /* increase frame num but only once for B frame */
995 if( i_slice_type != SLICE_TYPE_B || h->sh.i_type != SLICE_TYPE_B )
1000 /* ------------------------ Create slice header ----------------------- */
1001 x264_slice_init( h, i_nal_type, i_slice_type, i_global_qp );
1003 /* ---------------------- Write the bitstream -------------------------- */
1004 /* Init bitstream context */
1006 bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream );
1008 /* Write SPS and PPS */
1009 if( i_nal_type == NAL_SLICE_IDR )
1011 /* generate sequence parameters */
1012 x264_nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST );
1013 x264_sps_write( &h->out.bs, h->sps );
1016 /* generate picture parameters */
1017 x264_nal_start( h, NAL_PPS, NAL_PRIORITY_HIGHEST );
1018 x264_pps_write( &h->out.bs, h->pps );
1022 /* Write the slice */
1023 x264_slice_write( h, i_nal_type, i_nal_ref_idc, i_mb_count );
1025 /* End bitstream, set output */
1026 *pi_nal = h->out.i_nal;
1027 *pp_nal = &h->out.nal[0];
1029 /* Set output picture properties */
1030 if( i_slice_type == SLICE_TYPE_I )
1031 pic->i_type = i_nal_type == NAL_SLICE_IDR ? X264_TYPE_IDR : X264_TYPE_I;
1032 else if( i_slice_type == SLICE_TYPE_P )
1033 pic->i_type = X264_TYPE_P;
1035 pic->i_type = X264_TYPE_B;
1036 pic->i_pts = h->fenc->i_pts;
1038 /* ---------------------- Update encoder state ------------------------- */
1040 if( h->param.b_cabac )
1042 x264_cabac_model_update( &h->cabac, i_slice_type, h->sh.pps->i_pic_init_qp + h->sh.i_qp_delta );
1045 /* handle references */
1046 if( i_nal_ref_idc != NAL_PRIORITY_DISPOSABLE )
1048 x264_reference_update( h );
1051 /* increase frame count */
1055 x264_ratecontrol_end( h->rc, h->out.nal[h->out.i_nal-1].i_payload * 8 );
1057 /* restore CPU state (before using float again) */
1058 x264_cpu_restore( h->param.cpu );
1060 TIMER_STOP( i_mtime_encode_frame );
1062 /* ---------------------- Compute/Print statistics --------------------- */
1064 psnr_y = x264_psnr( 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 );
1065 psnr_u = x264_psnr( 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);
1066 psnr_v = x264_psnr( 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);
1069 h->stat.i_slice_count[i_slice_type]++;
1070 h->stat.i_slice_size[i_slice_type] += bs_pos( &h->out.bs) / 8;
1071 h->stat.f_psnr_y[i_slice_type] += psnr_y;
1072 h->stat.f_psnr_u[i_slice_type] += psnr_u;
1073 h->stat.f_psnr_v[i_slice_type] += psnr_v;
1075 for( i = 0; i < 17; i++ )
1077 h->stat.i_mb_count[h->sh.i_type][i] += i_mb_count[i];
1081 fprintf( stderr, "frame=%4d NAL=%d Slice:%c Poc:%-3d I4x4:%-5d I16x16:%-5d P:%-5d SKIP:%-3d size=%d bytes PSNR Y:%2.2f U:%2.2f V:%2.2f\n",
1084 i_slice_type == SLICE_TYPE_I ? 'I' : (i_slice_type == SLICE_TYPE_P ? 'P' : 'B' ),
1087 i_mb_count[I_16x16],
1088 i_mb_count[P_L0] + i_mb_count[P_8x8],
1090 h->out.nal[h->out.i_nal-1].i_payload,
1091 psnr_y, psnr_u, psnr_v );
1092 #ifdef DEBUG_MB_TYPE
1093 for( mb_xy = 0; mb_xy < h->sps->i_mb_width * h->sps->i_mb_height; mb_xy++ )
1095 const int i_mb_y = mb_xy / h->sps->i_mb_width;
1096 const int i_mb_x = mb_xy % h->sps->i_mb_width;
1098 if( i_mb_y > 0 && i_mb_x == 0 )
1099 fprintf( stderr, "\n" );
1101 if( h->mb.type[mb_xy] == I_4x4 )
1102 fprintf( stderr, "i" );
1103 else if( h->mb.type[mb_xy] == I_16x16 )
1104 fprintf( stderr, "I" );
1105 else if( h->mb.type[mb_xy] == P_SKIP )
1106 fprintf( stderr, "S" );
1107 else if( h->mb.type[mb_xy] == P_8x8 )
1108 fprintf( stderr, "8" );
1109 else if( h->mb.type[mb_xy] == P_L0 )
1110 fprintf( stderr, "P" );
1112 fprintf( stderr, "?" );
1114 fprintf( stderr, " " );
1118 #if DEBUG_DUMP_FRAME
1119 /* Dump reconstructed frame */
1120 x264_frame_dump( h, frame_psnr, "fdec.yuv" );
1125 x264_frame_dump( h, h->fref0[0], "ref0.yuv" );
1129 x264_frame_dump( h, h->fref1[0], "ref1.yuv" );
1135 /****************************************************************************
1136 * x264_encoder_close:
1137 ****************************************************************************/
1138 void x264_encoder_close ( x264_t *h )
1140 int64_t i_mtime_total = i_mtime_analyse + i_mtime_encode + i_mtime_write + i_mtime_filter + 1;
1143 fprintf( stderr, "x264: analyse=%d(%lldms) encode=%d(%lldms) write=%d(%lldms) filter=%d(%lldms)\n",
1144 (int)(100*i_mtime_analyse/i_mtime_total), i_mtime_analyse/1000,
1145 (int)(100*i_mtime_encode/i_mtime_total), i_mtime_encode/1000,
1146 (int)(100*i_mtime_write/i_mtime_total), i_mtime_write/1000,
1147 (int)(100*i_mtime_filter/i_mtime_total), i_mtime_filter/1000 );
1149 /* Slices used and PNSR */
1150 if( h->stat.i_slice_count[SLICE_TYPE_I] > 0 )
1152 const int i_count = h->stat.i_slice_count[SLICE_TYPE_I];
1153 fprintf( stderr, "x264: slice I:%-4d Avg size:%-5d PSNR Y:%2.2f U:%2.2f V:%2.2f PSNR-Y/Size:%2.2f\n",
1155 h->stat.i_slice_size[SLICE_TYPE_I] / i_count,
1156 h->stat.f_psnr_y[SLICE_TYPE_I] / i_count,
1157 h->stat.f_psnr_u[SLICE_TYPE_I] / i_count,
1158 h->stat.f_psnr_v[SLICE_TYPE_I] / i_count,
1159 1000*h->stat.f_psnr_y[SLICE_TYPE_I] / h->stat.i_slice_size[SLICE_TYPE_I] );
1161 if( h->stat.i_slice_count[SLICE_TYPE_P] > 0 )
1163 const int i_count = h->stat.i_slice_count[SLICE_TYPE_P];
1164 fprintf( stderr, "x264: slice P:%-4d Avg size:%-5d PSNR Y:%2.2f U:%2.2f V:%2.2f PSNR-Y/Size:%2.2f\n",
1166 h->stat.i_slice_size[SLICE_TYPE_P] / i_count,
1167 h->stat.f_psnr_y[SLICE_TYPE_P] / i_count,
1168 h->stat.f_psnr_u[SLICE_TYPE_P] / i_count,
1169 h->stat.f_psnr_v[SLICE_TYPE_P] / i_count,
1170 1000.0*h->stat.f_psnr_y[SLICE_TYPE_P] / h->stat.i_slice_size[SLICE_TYPE_P] );
1172 if( h->stat.i_slice_count[SLICE_TYPE_B] > 0 )
1174 fprintf( stderr, "x264: slice B:%-4d Avg size:%-5d PSNR Y:%2.2f U:%2.2f V:%2.2f PSNR-Y/Size:%2.2f\n",
1175 h->stat.i_slice_count[SLICE_TYPE_B],
1176 h->stat.i_slice_size[SLICE_TYPE_B] / h->stat.i_slice_count[SLICE_TYPE_B],
1177 h->stat.f_psnr_y[SLICE_TYPE_B] / h->stat.i_slice_count[SLICE_TYPE_B],
1178 h->stat.f_psnr_u[SLICE_TYPE_B] / h->stat.i_slice_count[SLICE_TYPE_B],
1179 h->stat.f_psnr_v[SLICE_TYPE_B] / h->stat.i_slice_count[SLICE_TYPE_B],
1180 1000*h->stat.f_psnr_y[SLICE_TYPE_B] / h->stat.i_slice_size[SLICE_TYPE_B] );
1184 if( h->stat.i_slice_count[SLICE_TYPE_I] > 0 )
1186 const int i_count = h->stat.i_slice_count[SLICE_TYPE_I];
1187 fprintf( stderr, "x264: slice I Avg I4x4:%-5d I16x16:%-5d\n",
1188 h->stat.i_mb_count[SLICE_TYPE_I][I_4x4] / i_count,
1189 h->stat.i_mb_count[SLICE_TYPE_I][I_16x16]/ i_count );
1191 if( h->stat.i_slice_count[SLICE_TYPE_P] > 0 )
1193 const int i_count = h->stat.i_slice_count[SLICE_TYPE_P];
1194 fprintf( stderr, "x264: slice P Avg I4x4:%-5d I16x16:%-5d P:%-5d P8x8:%-5d PSKIP:%-5d\n",
1195 h->stat.i_mb_count[SLICE_TYPE_P][I_4x4] / i_count,
1196 h->stat.i_mb_count[SLICE_TYPE_P][I_16x16]/ i_count,
1197 h->stat.i_mb_count[SLICE_TYPE_P][P_L0] / i_count,
1198 h->stat.i_mb_count[SLICE_TYPE_P][P_8x8] / i_count,
1199 h->stat.i_mb_count[SLICE_TYPE_P][P_SKIP] /i_count );
1203 const int i_count = h->stat.i_slice_count[SLICE_TYPE_I] +
1204 h->stat.i_slice_count[SLICE_TYPE_P] +
1205 h->stat.i_slice_count[SLICE_TYPE_B];
1207 fprintf( stderr, "x264: overall PSNR Y:%2.2f U:%2.2f V:%2.2f kb/s:%.1f fps:%.3f\n",
1208 (h->stat.f_psnr_y[SLICE_TYPE_I]+h->stat.f_psnr_y[SLICE_TYPE_P]+h->stat.f_psnr_y[SLICE_TYPE_B]) / i_count,
1209 (h->stat.f_psnr_u[SLICE_TYPE_I]+h->stat.f_psnr_u[SLICE_TYPE_P]+h->stat.f_psnr_u[SLICE_TYPE_B]) / i_count,
1210 (h->stat.f_psnr_v[SLICE_TYPE_I]+h->stat.f_psnr_v[SLICE_TYPE_P]+h->stat.f_psnr_v[SLICE_TYPE_B]) / i_count,
1211 h->param.f_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 / 1024,
1212 (double)1000000.0 * (double)i_count / (double)i_mtime_encode_frame );
1216 for( i = 0; i < X264_BFRAME_MAX + 1; i++ )
1218 if( h->frames.current[i] ) x264_frame_delete( h->frames.current[i] );
1219 if( h->frames.next[i] ) x264_frame_delete( h->frames.next[i] );
1220 if( h->frames.unused[i] ) x264_frame_delete( h->frames.unused[i] );
1223 for( i = 0; i < h->param.i_frame_reference+2; i++ )
1225 x264_frame_delete( h->frames.reference[i] );
1229 x264_ratecontrol_delete( h->rc );
1231 x264_macroblock_cache_end( h );
1232 x264_free( h->out.p_bitstream );