x264_slicetype_decide函数(代码所在位置为“x264-snapshot-20171128-2245-stable\encoder\slicetype.c”)的主要作用为:真正用于分析帧类型(I\P\B帧)
调用顺序如下:
x264_encoder_encode —> x264_lookahead_get_frames —> x264_slicetype_decide —> x264_slicetype_analyse
分析x264_slicetype_analyse函数,需有以下认知作为前提:
- 帧类型分析是以GOP为单位进行的,即当待分析帧队列达到一个GOP时,才开始根据约束条件进行帧类型的确认;
- 以GOP为单位,先分析里面的关键帧,再确认里面的P帧,最后根据不同算法确认GOP内的B帧;
- 以下暂时只是简要记述,详细的解释需再了解B帧自适应确认算法和码率控制部分代码之后再进行补充。
x264_slicetype_analyse函数:
// 分析帧类型
void x264_slicetype_analyse( x264_t *h, int intra_minigop )
{
x264_mb_analysis_t a;
x264_frame_t *frames[X264_LOOKAHEAD_MAX+3] = { NULL, }; // lookahead线程内帧数
int num_frames, orig_num_frames, keyint_limit, framecnt;
int i_max_search = X264_MIN( h->lookahead->next.i_size, X264_LOOKAHEAD_MAX ); // 最大搜索范围
int vbv_lookahead = h->param.rc.i_vbv_buffer_size && h->param.rc.i_lookahead;
/* For determinism we should limit the search to the number of frames lookahead has for sure
* in h->lookahead->next.list buffer, except at the end of stream.
* For normal calls with (intra_minigop == 0) that is h->lookahead->i_slicetype_length + 1 frames.
* And for I-frame calls (intra_minigop != 0) we already removed intra_minigop frames from there. */
if( h->param.b_deterministic )
i_max_search = X264_MIN( i_max_search, h->lookahead->i_slicetype_length + 1 - intra_minigop );
int keyframe = !!intra_minigop;
assert( h->frames.b_have_lowres );
if( !h->lookahead->last_nonb )
return;
frames[0] = h->lookahead->last_nonb; // 最后一个非B帧赋值给frames[0]
for( framecnt = 0; framecnt < i_max_search; framecnt++ )
frames[framecnt+1] = h->lookahead->next.list[framecnt]; // 将剩下的h->lookahead->next.list帧赋值给frames
x264_lowres_context_init( h, &a ); // 低层级(宏块级)上下文参数初始化
if( !framecnt )
{
if( h->param.rc.b_mb_tree ) // 判断宏块树(mb_tree)码率控制算法是否开启
x264_macroblock_tree( h, &a, frames, 0, keyframe );
return;
}
// 关键帧限制值,当其值为0时,则此帧需强制设置为关键帧
keyint_limit = h->param.i_keyint_max - frames[0]->i_frame + h->lookahead->i_las