X264学习笔记 2009-01-28 10:44:34 阅读89 评论0 字号:大中小 订阅
(1) /* build ref list 0/1 */
x264_reference_build_list( h, h->fdec->i_poc, i_slice_type );
//创建list0和list1(ref0和ref1),ref0从大到小,ref1从小到大
//x264_reference_build_list( h, h->fdec->i_poc, i_slice_type )定义在encoder.c中,如下:
static inline void x264_reference_build_list( x264_t *h, int i_poc, int i_slice_type )
{
int i;
int b_ok;
/* build ref list 0/1, Order ref0 from higher to lower poc */
...
}
(2) /* 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;
// 初始化速率和量化步长
(3) x264_slice_init( h, i_nal_type, i_slice_type, i_global_qp );
// 创建片头,片头初始化;
(4) /* Write SPS and PPS */
if( i_nal_type == NAL_SLICE_IDR && h->param.b_repeat_headers )
{
if( h->fenc->i_frame == 0 )
{
x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE );
x264_sei_version_write( h, &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 );
/* generate picture parameters */
x264_nal_start( h, NAL_PPS, NAL_PRIORITY_HIGHEST );
x264_pps_write( &h->out.bs, h->pps );
x264_nal_end( h );
}
(5) /* Write frame */
i_frame_size = x264_slices_write( h );
// encoder.c中,原型:
static inline int x264_slices_write( x264_t *h )
{
...
}
关键部分为x264_slice_write( h );该函数定义在encoder.c中,实现一帧的编码。
注1:for( mb_xy = h->sh.i_first_mb, i_skip = 0; mb_xy < h->sh.i_last_mb; mb_xy++ )
//循环22*18=396次对396个宏块进行编码,mb_xy是16*16宏块序号0~395,
//其赋值过程在x264_encoder_encode 中的x264_slice_init里。
const int i_mb_y = mb_xy / h->sps->i_mb_width;
//计算当前宏块垂直坐标(行坐标,以宏块为单位) i_mb_width=22
const int i_mb_x = mb_xy % h->sps->i_mb_width;
//计算当前宏块水平坐标(列坐标,以宏块为单位)
int mb_spos = bs_pos(&h->out.bs);
//在bs.h中,静态内联函数,如下:
static inline int bs_pos( bs_t *s )
{
return( 8 * ( s->p - s->p_start ) + 8 - s->i_left );
}
x264_macroblock_cache_load( h, i_mb_x, i_mb_y );
//它是将要编码的宏块的周围宏块的值读进来。要想得到当前块的预测值,要先知道上面,左面的预测值。
// 分析参数选择合适的块编码模式,加载相邻块的信息。
x264_macroblock_analyse( h );
//对一个16*16块进行预测模式选择,通过比较得出最佳预测模式。
// 定义在analyse.c 中,计算sad值分析是否要对16*16的宏块进行分割和采用哪种分割方式合适。
x264_macroblock_encode( h );
// 根据前面分析得到的h->mb.i_type的类型,对一个16*16块进行编码。
评论