x264代码剖析(八):encode()函数之x264_encoder_close()函数
encode()函数是x264的主干函数,主要包括x264_encoder_open()函数、x264_encoder_headers()函数、x264_encoder_encode()函数与x264_encoder_close()函数四大部分,其中x264_encoder_encode()函数是其核心部分,具体的H.264视频编码算法均在此模块。上三篇博文主要分析了x264_encoder_open()函数、x264_encoder_headers()函数与x264_encoder_encode()函数(后续会继续深入),本文主要学习x264_encoder_close()函数。
x264_encoder_close()是libx264的一个API函数。该函数用于关闭编码器,同时输出一些统计信息。如下图所示,调用了以下函数:
x264_lookahead_delete():释放Lookahead相关的变量。
x264_ratecontrol_summary():汇总码率控制信息。
x264_ratecontrol_delete():关闭码率控制。
对应的代码如下:
/******************************************************************/
/******************************************************************/
/*
======Analysed by RuiDong Fang
======Csdn Blog:http://blog.csdn.net/frd2009041510
======Date:2016.03.10
*/
/******************************************************************/
/******************************************************************/
/************====== x264_encoder_close()函数 ======************/
/*
功能:关闭编码器,同时输出一些统计信息。
*/
/****************************************************************************
* x264_encoder_close:
****************************************************************************/
void x264_encoder_close ( x264_t *h )
{
int64_t i_yuv_size = FRAME_SIZE( h->param.i_width * h->param.i_height );
int64_t i_mb_count_size[2][7] = {
{0}};
char buf[200];
int b_print_pcm = h->stat.i_mb_count[SLICE_TYPE_I][I_PCM]
|| h->stat.i_mb_count[SLICE_TYPE_P][I_PCM]
|| h->stat.i_mb_count[SLICE_TYPE_B][I_PCM];
x264_lookahead_delete( h ); 释放lookahead相关的变量
#if HAVE_OPENCL
x264_opencl_lookahead_delete( h );
x264_opencl_function_t *ocl = h->opencl.ocl;
#endif
if( h->param.b_sliced_threads )
x264_threadpool_wait_all( h );
if( h->param.i_threads > 1 )
x264_threadpool_delete( h->threadpool );
if( h->param.i_lookahead_threads > 1 )
x264_threadpool_delete( h->lookaheadpool );
if( h->i_thread_frames > 1 )
{
for( int i = 0; i < h->i_thread_frames; i++ )
if( h->thread[i]->b_thread_active )
{
assert( h->thread[i]->fenc->i_reference_count == 1 );
x264_frame_delete( h->thread[i]->fenc );
}
x264_t *thread_prev = h->thread[h->i_thread_phase];
x264_thread_sync_ratecontrol( h, thread_prev, h );
x264_thread_sync_ratecontrol( thread_prev, thread_prev, h );
h->i_frame = thread_prev->i_frame + 1 - h->i_thread_frames;
}
h->i_frame++;
/* Slices used and PSNR */
/* 示例
* x264 [info]: frame I:2 Avg QP:20.51 size: 20184 PSNR Mean Y:45.32 U:47.54 V:47.62 Avg:45.94 Global:45.52
* x264 [info]: frame P:33 Avg QP:23.08 size: 3230 PSNR Mean Y:43.23 U:47.06 V:46.87 Avg:44.15 Global:44.00
* x264 [info]: frame B:65 Avg QP:27.87 siz