📄 encoder.c
字号:
/// {
/* 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 )
{
x264_cabac_encode_flush( &h->cabac );
}
else
{*/
/* rbsp_slice_trailing_bits */
bs_rbsp_trailing( &h->out.bs );
/// }
x264_nal_end( h );//NAL单元编码结束
/* 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;
return 0;
}
static inline int x264_slices_write( x264_t *h )
{
int i_frame_size;
fprintf(stderr,"x264_slices_write:h->param.b_visualize=%d\n",h->param.b_visualize);//add
fprintf(stderr,"x264_slices_write:h->param.i_threads=%d\n",h->param.i_threads);//add
fprintf(stderr,"x264_slices_write:h->out.nal[h->out.i_nal-1].i_payload=%d\n",h->out.nal[h->out.i_nal-1].i_payload);//add
// fprintf(stderr,"x264_slices_write:hout=%d\n",hout);//add
// fprintf(stderr,"x264_slices_write:hout=%d\n",hout);//add
// fprintf(stderr,"x264_slices_write:hout=%d\n",hout);//add
// fprintf(stderr,"x264_slices_write:hout=%d\n",hout);//add
#if VISUALIZE
if( h->param.b_visualize )
x264_visualize_init( h );
#endif
if( h->param.i_threads == 1 )
{
x264_ratecontrol_threads_start( h );
x264_slice_write( h );
i_frame_size = h->out.nal[h->out.i_nal-1].i_payload;
}
else
{
int i_nal = h->out.i_nal;
int i_bs_size = h->out.i_bitstream / h->param.i_threads;
int i;
/* duplicate contexts */
for( i = 0; i < h->param.i_threads; i++ )
{
x264_t *t = h->thread[i];
if( i > 0 )
{
memcpy( t, h, sizeof(x264_t) );
t->out.p_bitstream += i*i_bs_size;
bs_init( &t->out.bs, t->out.p_bitstream, i_bs_size );
t->i_thread_num = i;
}
t->sh.i_first_mb = (i * h->sps->i_mb_height / h->param.i_threads) * h->sps->i_mb_width;
t->sh.i_last_mb = ((i+1) * h->sps->i_mb_height / h->param.i_threads) * h->sps->i_mb_width;
t->out.i_nal = i_nal + i;
}
x264_ratecontrol_threads_start( h );
/* dispatch */
#ifdef HAVE_PTHREAD
{
pthread_t handles[X264_SLICE_MAX];
for( i = 0; i < h->param.i_threads; i++ )
pthread_create( &handles[i], NULL, (void*)x264_slice_write, (void*)h->thread[i] );
for( i = 0; i < h->param.i_threads; i++ )
pthread_join( handles[i], NULL );
}
#else
for( i = 0; i < h->param.i_threads; i++ )
x264_slice_write( h->thread[i] );
#endif
/* merge contexts */
i_frame_size = h->out.nal[i_nal].i_payload;
for( i = 1; i < h->param.i_threads; i++ )
{
int j;
x264_t *t = h->thread[i];
h->out.nal[i_nal+i] = t->out.nal[i_nal+i];
i_frame_size += t->out.nal[i_nal+i].i_payload;
// all entries in stat.frame are ints
for( j = 0; j < sizeof(h->stat.frame) / sizeof(int); j++ )
((int*)&h->stat.frame)[j] += ((int*)&t->stat.frame)[j];
}
h->out.i_nal = i_nal + h->param.i_threads;
}
#if VISUALIZE
if( h->param.b_visualize )
{
x264_visualize_show( h );
x264_visualize_close( h );
}
#endif
return i_frame_size;
}
/****************************************************************************
* 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_frame_size;
int a;//add
int i;
int i_global_qp;
char psz_message[80];
/* no data out */
*pi_nal = 0;
*pp_nal = NULL;
fprintf(stderr,"x264_encoder_encode:h->i_frame=%d\n",h->i_frame);//add
fprintf(stderr,"x264_encoder_encode:h->i_frame_num=%d\n",h->i_frame_num);//add
fprintf(stderr,"x264_encoder_encode:h->i_frame_offset=%d\n",h->i_frame_offset);//add
fprintf(stderr,"x264_encoder_encode:h->i_idr_pic_id=%d\n",h->i_idr_pic_id);//add
fprintf(stderr,"x264_encoder_encode:h->i_nal_ref_idc=%d\n",h->i_nal_ref_idc);//add
fprintf(stderr,"x264_encoder_encode:h->i_nal_type=%d\n",h->i_nal_type);//add
fprintf(stderr,"x264_encoder_encode:h->i_poc=%d\n",h->i_poc);//add
fprintf(stderr,"x264_encoder_encode:h->i_poc_lsb=%d\n",h->i_poc_lsb);//add
fprintf(stderr,"x264_encoder_encode:h->i_poc_msb=%d\n",h->i_poc_msb);//add
fprintf(stderr,"x264_encoder_encode:h->i_ref0=%d\n",h->i_ref0);//add
fprintf(stderr,"x264_encoder_encode:h->i_ref1=%d\n",h->i_ref1);//add
fprintf(stderr,"x264_encoder_encode:h->i_thread_num=%d\n",h->i_thread_num);//add
// fprintf(stderr,"x264_encoder_encode:h->i_nal_type=%d\n",h->i_nal_type);//add
/* ------------------- 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 );//(1)
fprintf(stderr,"x264_encoder_encode:fenc=%d\n",*fenc);//add
x264_frame_copy_picture( h, fenc, pic_in );//(2)
if( h->param.i_width % 16 || h->param.i_height % 16 )
x264_frame_expand_border_mod16( h, fenc );
fenc->i_frame = h->frames.i_input++;//(3)
x264_frame_put( h->frames.next, fenc );//(4)
if( h->frames.b_have_lowres )
x264_frame_init_lowres( h->param.cpu, fenc );//(1)(2)(3)(4)里面包含低象素的扩展,很多for循环,应该是抽头计算和半精度象素的扩展,要认真看!
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 );//对slice类型的判定,里面也要看一下!
/* 3: move some B-frames and 1 non-B to encode queue *///移动一些B帧和一个非B帧到编码队列中
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] ) );//这主要是因为B帧必须等后面的非B帧编码结束后才能编码,所以把暂时不编的一系列B帧存入队列中,一直到非B帧取出进行编码之后再进行前面的B帧编码
/* 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 );//建立list0和list1参考列表
/* Init the rate control */
x264_ratecontrol_start( h, i_slice_type, h->fenc->i_qpplus1 );//比特(bit)率控制初始化
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 );
/* ------------------------ Create slice header ----------------------- */
x264_slice_init( h, i_nal_type, i_slice_type, i_global_qp );//创建slice的头部数据
if( h->fenc->b_kept_as_ref )
h->i_frame_num++;
/* ---------------------- Write the bitstream -------------------------- *///写入bit流
/* Init bitstream context *///初始化bit流上下文
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);
}
h->i_nal_type = i_nal_type;
h->i_nal_ref_idc = i_nal_ref_idc;
/* Write SPS and PPS *///写入序列参数信号和图片参数信号
if( i_nal_type == NAL_SLICE_IDR && h->param.b_repeat_headers )
{
if( h->fenc->i_frame == 0 )
{
/* identify ourself */
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 );
}
/* Write frame */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -