📄 encoder.c.svn-base
字号:
h->fdec->i_frame_num = h->sh.i_frame_num; if( h->sps->i_poc_type == 0 ) { h->sh.i_poc_lsb = h->fdec->i_poc & ( (1 << h->sps->i_log2_max_poc_lsb) - 1 ); h->sh.i_delta_poc_bottom = 0; /* XXX won't work for field */ } else if( h->sps->i_poc_type == 1 ) { /* FIXME TODO FIXME */ } else { /* Nothing to do ? */ } /* get adapative cabac model if needed */ if( h->param.b_cabac ) { if( h->param.i_cabac_init_idc == -1 ) { h->sh.i_cabac_init_idc = x264_cabac_model_get( &h->cabac, i_slice_type ); } } x264_macroblock_slice_init( h );}static inline void x264_slice_write( x264_t *h, int i_nal_type, int i_nal_ref_idc ){ int i_skip; int mb_xy; int i; /* Init stats */ h->stat.frame.i_hdr_bits = h->stat.frame.i_itex_bits = h->stat.frame.i_ptex_bits = h->stat.frame.i_misc_bits = h->stat.frame.i_intra_cost = h->stat.frame.i_inter_cost = 0; for( i = 0; i < 18; i++ ) h->stat.frame.i_mb_count[i] = 0; /* Slice */ x264_nal_start( h, i_nal_type, i_nal_ref_idc ); /* Slice header */ x264_slice_header_write( &h->out.bs, &h->sh, i_nal_ref_idc ); if( h->param.b_cabac ) { /* alignment needed */ bs_align_1( &h->out.bs ); /* init cabac */ 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 ); x264_cabac_encode_init ( &h->cabac, &h->out.bs ); } h->mb.i_last_qp = h->pps->i_pic_init_qp + h->sh.i_qp_delta; h->mb.i_last_dqp = 0;#if VISUALIZE if( h->param.b_visualize ) x264_visualize_init( h );#endif for( mb_xy = 0, i_skip = 0; mb_xy < h->sps->i_mb_width * h->sps->i_mb_height; mb_xy++ ) { const int i_mb_y = mb_xy / h->sps->i_mb_width; const int i_mb_x = mb_xy % h->sps->i_mb_width; int mb_spos = bs_pos(&h->out.bs); /* load cache */ x264_macroblock_cache_load( h, i_mb_x, i_mb_y ); /* analyse parameters * Slice I: choose I_4x4 or I_16x16 mode * Slice P: choose between using P mode or intra (4x4 or 16x16) * */ TIMER_START( i_mtime_analyse ); x264_macroblock_analyse( h ); TIMER_STOP( i_mtime_analyse ); /* encode this macrobock -> be carefull it can change the mb type to P_SKIP if needed */ TIMER_START( i_mtime_encode ); x264_macroblock_encode( h ); TIMER_STOP( i_mtime_encode ); TIMER_START( i_mtime_write ); if( IS_SKIP( h->mb.i_type ) ) { if( h->param.b_cabac ) { if( mb_xy > 0 ) { /* not end_of_slice_flag */ x264_cabac_encode_terminal( &h->cabac, 0 ); } x264_cabac_mb_skip( h, 1 ); } else { i_skip++; } } else { if( h->param.b_cabac ) { if( mb_xy > 0 ) { /* not end_of_slice_flag */ x264_cabac_encode_terminal( &h->cabac, 0 ); } if( h->sh.i_type != SLICE_TYPE_I ) { x264_cabac_mb_skip( h, 0 ); } x264_macroblock_write_cabac( h, &h->out.bs ); } else { if( h->sh.i_type != SLICE_TYPE_I ) { bs_write_ue( &h->out.bs, i_skip ); /* skip run */ i_skip = 0; } x264_macroblock_write_cavlc( h, &h->out.bs ); } } TIMER_STOP( i_mtime_write );#if VISUALIZE if( h->param.b_visualize ) x264_visualize_mb( h );#endif /* save cache */ x264_macroblock_cache_save( h ); h->stat.frame.i_mb_count[h->mb.i_type]++; if( h->mb.b_variable_qp ) x264_ratecontrol_mb(h, bs_pos(&h->out.bs) - mb_spos); } if( h->param.b_cabac ) { /* end of slice */ x264_cabac_encode_terminal( &h->cabac, 1 ); } else if( i_skip > 0 ) { bs_write_ue( &h->out.bs, i_skip ); /* last skip run */ } if( h->param.b_cabac ) { int i_cabac_word; x264_cabac_encode_flush( &h->cabac ); /* TODO cabac stuffing things (p209) */ 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; while( i_cabac_word > 0 ) { bs_write( &h->out.bs, 16, 0x0000 ); i_cabac_word--; } } else { /* rbsp_slice_trailing_bits */ bs_rbsp_trailing( &h->out.bs ); } x264_nal_end( h );#if VISUALIZE if( h->param.b_visualize ) { x264_visualize_show( h ); x264_visualize_close( h ); }#endif /* Compute misc bits */ h->stat.frame.i_misc_bits = bs_pos( &h->out.bs ) + NALU_OVERHEAD * 8 - h->stat.frame.i_itex_bits - h->stat.frame.i_ptex_bits - h->stat.frame.i_hdr_bits;}/**************************************************************************** * x264_encoder_encode: * XXX: i_poc : is the poc of the current given picture * i_frame : is the number of the frame being coded * ex: type frame poc * I 0 2*0 * P 1 2*3 * B 2 2*1 * B 3 2*2 * P 4 2*6 * B 5 2*4 * B 6 2*5 ****************************************************************************/int x264_encoder_encode( x264_t *h, x264_nal_t **pp_nal, int *pi_nal, x264_picture_t *pic_in, x264_picture_t *pic_out ){ x264_frame_t *frame_psnr = h->fdec; /* just to keep the current decoded frame for psnr calculation */ int i_nal_type; int i_nal_ref_idc; int i_slice_type; int i; int i_global_qp; char psz_message[80]; /* no data out */ *pi_nal = 0; *pp_nal = NULL; /* ------------------- Setup new frame from picture -------------------- */ TIMER_START( i_mtime_encode_frame ); if( pic_in != NULL ) { /* 1: Copy the picture to a frame and move it to a buffer */ x264_frame_t *fenc = x264_frame_get( h->frames.unused ); x264_frame_copy_picture( h, fenc, pic_in ); fenc->i_frame = h->frames.i_input++; x264_frame_put( h->frames.next, fenc ); x264_frame_init_lowres( h->param.cpu, fenc ); if( h->frames.i_input <= h->frames.i_delay ) { /* Nothing yet to encode */ /* waiting for filling bframe buffer */ pic_out->i_type = X264_TYPE_AUTO; return 0; } } if( h->frames.current[0] == NULL ) { int bframes = 0; /* 2: Select frame types */ if( h->frames.next[0] == NULL ) return 0; x264_slicetype_decide( h ); /* 3: move some B-frames and 1 non-B to encode queue */ while( IS_X264_TYPE_B( h->frames.next[bframes]->i_type ) ) bframes++; x264_frame_put( h->frames.current, x264_frame_get( &h->frames.next[bframes] ) ); /* FIXME: when max B-frames > 3, BREF may no longer be centered after GOP closing */ if( h->param.b_bframe_pyramid && bframes > 1 ) { x264_frame_t *mid = x264_frame_get( &h->frames.next[bframes/2] ); mid->i_type = X264_TYPE_BREF; x264_frame_put( h->frames.current, mid ); bframes--; } while( bframes-- ) x264_frame_put( h->frames.current, x264_frame_get( h->frames.next ) ); } TIMER_STOP( i_mtime_encode_frame ); /* ------------------- Get frame to be encoded ------------------------- */ /* 4: get picture to encode */ h->fenc = x264_frame_get( h->frames.current ); if( h->fenc == NULL ) { /* Nothing yet to encode (ex: waiting for I/P with B frames) */ /* waiting for filling bframe buffer */ pic_out->i_type = X264_TYPE_AUTO; return 0; }do_encode: if( h->fenc->i_type == X264_TYPE_IDR ) { h->frames.i_last_idr = h->fenc->i_frame; } /* ------------------- Setup frame context ----------------------------- */ /* 5: Init data dependant of frame type */ TIMER_START( i_mtime_encode_frame ); if( h->fenc->i_type == X264_TYPE_IDR ) { /* reset ref pictures */ x264_reference_reset( h ); i_nal_type = NAL_SLICE_IDR; i_nal_ref_idc = NAL_PRIORITY_HIGHEST; i_slice_type = SLICE_TYPE_I; } else if( h->fenc->i_type == X264_TYPE_I ) { i_nal_type = NAL_SLICE; i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/ i_slice_type = SLICE_TYPE_I; } else if( h->fenc->i_type == X264_TYPE_P ) { i_nal_type = NAL_SLICE; i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/ i_slice_type = SLICE_TYPE_P; } else if( h->fenc->i_type == X264_TYPE_BREF ) { i_nal_type = NAL_SLICE; i_nal_ref_idc = NAL_PRIORITY_HIGH; /* maybe add MMCO to forget it? -> low */ i_slice_type = SLICE_TYPE_B; } else /* B frame */ { i_nal_type = NAL_SLICE; i_nal_ref_idc = NAL_PRIORITY_DISPOSABLE; i_slice_type = SLICE_TYPE_B; } h->fdec->i_poc = h->fenc->i_poc = 2 * (h->fenc->i_frame - h->frames.i_last_idr); h->fdec->i_type = h->fenc->i_type; h->fdec->i_frame = h->fenc->i_frame; h->fenc->b_kept_as_ref = h->fdec->b_kept_as_ref = i_nal_ref_idc != NAL_PRIORITY_DISPOSABLE; /* ------------------- Init ----------------------------- */ /* build ref list 0/1 */ x264_reference_build_list( h, h->fdec->i_poc, i_slice_type ); /* Init the rate control */ x264_ratecontrol_start( h, i_slice_type, h->fenc->i_qpplus1 ); i_global_qp = x264_ratecontrol_qp( h ); pic_out->i_qpplus1 = h->fdec->i_qpplus1 = i_global_qp + 1; if( i_slice_type == SLICE_TYPE_B ) x264_macroblock_bipred_init( h ); /* increase frame num but only once for B frame */ if( i_slice_type != SLICE_TYPE_B || h->sh.i_type != SLICE_TYPE_B ) { h->i_frame_num++; } /* ------------------------ Create slice header ----------------------- */ x264_slice_init( h, i_nal_type, i_slice_type, i_global_qp ); /* ---------------------- Write the bitstream -------------------------- */ /* Init bitstream context */ h->out.i_nal = 0; bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream ); if(h->param.b_aud){ int pic_type; if(i_slice_type == SLICE_TYPE_I) pic_type = 0; else if(i_slice_type == SLICE_TYPE_P) pic_type = 1; else if(i_slice_type == SLICE_TYPE_B) pic_type = 2; else pic_type = 7; x264_nal_start(h, NAL_AUD, NAL_PRIORITY_DISPOSABLE); bs_write(&h->out.bs, 3, pic_type); bs_rbsp_trailing(&h->out.bs); x264_nal_end(h); } /* Write SPS and PPS */ if( i_nal_type == NAL_SLICE_IDR ) { if( h->fenc->i_frame == 0 ) { /* identify ourself */ x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE ); x264_sei_version_write( &h->out.bs ); x264_nal_end( h ); } /* generate sequence parameters */ x264_nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST ); x264_sps_write( &h->out.bs, h->sps ); x264_nal_end( h );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -