/*
对lookahead队列中的帧分析,确定他们的帧类型
过程:
1. 若第一帧是AUTO/I,检查其相对于上一个非B帧是否场景切换,若场景切换则将当前帧设置为I,return
2. 遍历,将所有的关键帧根据openGOP设置成I/IDR
3. 将所有IDR帧前的AUTO/B帧改为P帧
4. 若允许使用B帧
· 使用X264_B_ADAPT_TRELLIS方法自适应设置B帧
· 使用X264_B_ADAPT_FAST方法自适应设置B帧
· 不自适应设置B帧
若不允许使用B帧,则将全部的AUTO/B设置成P
5. 将最后一帧AUTO/B帧改为P帧
6. 若允许宏块树分析则进行宏块树分析
7. 根据关键帧之间的距离限制检查帧类型
8. 若允许vbv则进行vbv检查
*/
void x264_slicetype_analyse( x264_t *h, int intra_minigop )
{
x264_mb_analysis_t a;
x264_frame_t *frames[X264_LOOKAHEAD_MAX+3] = {
NULL, };
int num_frames, orig_num_frames, keyint_limit, framecnt;
int i_max_search = X264_MIN( h->lookahead->next.i_size, X264_LOOKAHEAD_MAX );
int b_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 ) //若前面没有非B帧,则return
return;
//frame[0]存储前面最近的非B帧,frame[1~i_max_search]存储要分析的帧
frames[0] = h->lookahead->last_nonb; //取最近的非B帧为frame0
for( framecnt = 0; framecnt < i_max_search; framecnt++ ) //从list中拷贝到frames中
frames[framecnt+1] = h->lookahead->next.list[framecnt];
//低分辨率上下文初始化
lowres_context_init( h, &a );
if( !framecnt ) //若framecnt = 0,即i_max_search = 0,则无帧分析,return
{
if( h->param.rc.b_mb_tree )
macroblock_tree( h, &a, frames, 0, keyframe );
return;
}
//计算keyint_limit = 最大关键帧距离 - 最近的非B帧 + 最近一个关键帧 - 1
keyint_limit = h->param.i_keyint_max - frames[0]->i_frame + h->lookahead->i_last_keyframe - 1;
//计算orig_num_frames和num_frames
orig_num_frames = num_frames = h->param.b_intra_refresh ? framecnt : X264_MIN( framecnt, keyint_limit );
/* This is important psy-wise: if we have a non-scenecut keyframe,
* there will be significant visual artifacts if the frames just before
* go down in quality due to being referenced less, despite it being
* more RD-optimal.
* 如果我们有一个非场景切换的关键帧,将会有一个明显的视觉影响
*/
if( (h->param.analyse.b_psy && h->param.rc.b_mb_tree) || b_vbv_lookahead )
num_frames = framecnt;
else if( h->param.b_open_gop && num_frames < framecnt )
num_frames++;
else if( num_frames == 0 )
{
frames[1]->i_type = X264_TYPE_I;
return;
}
if( IS_X264_TYPE_AUTO_OR_I( frames[1]->i_type ) &&
h->param.i_scenecut_threshold && scenecut( h, &a, frames, 0, 1, 1, orig_num_frames, i_max_search ) )
{
//若是第一帧是auto/I 且 相对于上一个非B帧有场景切换,则设置为I帧
if( frames[1]->i_type == X264_TYPE_AUTO )
frames[1]->i_type = X264_TYPE_I; //当前帧定为I帧
return;
}
/* Replace forced keyframes with I/IDR-frames */
for( int j = 1; j <= num_frames; j++ )
{
////遍历检查是否关键帧,依openGOP设定为I/IDR
if( frames[j]->i_type == X264_TYPE_KEYFRAME )
frames[j]->i_type = h->param.b_open_gop ? X264_TYPE_I : X264_TYPE_IDR;
}
/* Close GOP at IDR-frames */
for( int j = 2; j <= num_frames; j++ )
{
//遍历检查IDR帧,将IDR帧前的AUTO/B帧设置为P帧
if( frames[j]->i_type == X264_TYPE_IDR && IS_X264_TYPE_AUTO_OR_B( frames[j-1]->i_type ) )
frames[j-1]->i_type = X264_TYPE_P;
}
int num_analysed_frames = num_frames;
int reset_start;
if( h->param.i_bframe ) //若允许B帧
{
/* 根据i_bframe_adaptive来进行自适应B帧设置
i_bframe_adaptive = X2
帧类型决策-x264_slicetype_analyse()
最新推荐文章于 2024-06-12 11:45:45 发布
本文详细解析x264编码器如何通过自适应算法分析lookahead队列中的帧,确定帧类型,包括场景切换判断、关键帧设置、B帧自适应等过程,确保编码效率和视频质量。

最低0.47元/天 解锁文章
268

被折叠的 条评论
为什么被折叠?



