/*
对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 = X264_B_ADAPT_TRELLIS 使用最佳判别方法,即viterbi
i_bframe_adaptive = X264_B_ADAPT_FAST 使用快速判别方法
i_bframe_adaptive = X264_B_ADAPT_NONE 不使用自适应B帧 */
if( h->param.i_bframe_adaptive == X264_B_ADAPT_TRELLIS )//b_adaptive=2,使用最佳算法,即viterbi
{
if( num_frames > 1 )
{
//初始化最优帧类型路径 和 路径索引
char best_paths[X264_BFRAME_MAX+1][X264_LOOKAHEAD_MAX+1] = {"","P"};
int best_path_index = num_frames % (X264_BFRAME_MAX+1);
/* Perform the frametype analysis. */
for( int j = 2; j <= num_frames; j++ )
//执行viterbi计算最优帧类型路径,将最优帧类型路径写入best_paths
slicetype_path( h, &a, frames, j, best_paths );
/* Load the results of the analysis into the frame types. */
//根据分析的最优帧类型路径设置各个帧
for( int j = 1; j < num_frames; j++ )
{
if( best_paths[best_path_index][j-1] != 'B' )
{ //若是'P'则将AUTO/B该成P
if( IS_X264_TYPE_AUTO_OR_B( frames[j]->i_type ) )
frames[j]->i_type = X264_TYPE_P;
}
else
{ //若是'B'则将AUTO改成B
if( frames[j]->i_type == X264_TYPE_AUTO )
frames[j]->i_type = X264_TYPE_B;
}
}
}
} //end of b_adaptive = X264_B_ADAPT_TRELLIS
else if( h->param.i_bframe_adaptive == X264_B_ADAPT_FAST ) //b_adaptive=1,快速决定
{
int last_nonb = 0;
int num_bframes = h->param.i_bframe; //得到两个参考帧中最多所允许的B帧数量
char path[X264_LOOKAHEAD_MAX+1];
for( int j = 1; j < num_frames; j++ ) //遍历list里的每一帧,除了最后一帧
{
/*
针对前一帧来更新 B坑数num_bframes 和 最近非B帧last_nonb
*/
if( j-1 > 0 && IS_X264_TYPE_B( frames[j-1]->i_type ) ) //若前一帧为B帧
num_bframes--; //则占一个B帧的坑
else //若前一帧为非B帧
{
last_nonb = j-1; //更新最近非B帧为前一帧
num_bframes = h->param.i_bframe; //这重置B帧的坑数了
}
/*
设置当前帧的帧类型
*/
if( !num_bframes ) //若B帧坑占满了,且当前帧B/auto则改P,其余不变
{
if( IS_X264_TYPE_AUTO_OR_B( frames[j]->i_type ) )
frames[j]->i_type = X264_TYPE_P;
continue;
}
// else 允许到这里的帧允许使用B帧 (还有B坑)
if( frames[j]->i_type != X264_TYPE_AUTO ) //若已经有类型了,则下一个
continue;
// else 运行到这里的帧全是AUTO帧
if( IS_X264_TYPE_B( frames[j+1]->i_type ) ) //若后面一帧为B帧
{
frames[j]->i_type = X264_TYPE_P; //当前帧改P,为什么??
continue;
}
/* else 运行到这里的帧后面一帧一定是P帧,不可能是I帧,
因为之前将所有I帧的前一帧设置成P帧,即只可能PI,不可能BI; */
int bframes = j - last_nonb - 1; //得到上一个非B帧到最后一帧之间的B帧数量bframes
memset( path, 'B', bframes ); //在path中填充bframes个B
// 将当前帧设置为P帧,计算BBB...BBB(P)P帧类型路径的开销
strcpy( path+bframes, "PP" );
uint64_t cost_p = slicetype_path_cost( h, &a, frames+last_nonb, path, COST_MAX64 );
// 将当前帧设置为B帧,计算BBB...BBB(B)P帧类型路径的开销
strcpy( path+bframes, "BP" );
uint64_t cost_b = slicetype_path_cost( h, &a, frames+last_nonb, path, cost_p );
//选择开销最小的,定当前帧的帧类型
if( cost_b < cost_p )
frames[j]->i_type = X264_TYPE_B;
else
frames[j]->i_type = X264_TYPE_P;
}
} //end of b_adaptive = X264_B_ADAPT_FAST
else //b_adaptive=0,即不适用自适应B帧
{
int num_bframes = h->param.i_bframe;
for( int j = 1; j < num_frames; j++ )
{
if( !num_bframes ) //没有B帧坑位了
{
if( IS_X264_TYPE_AUTO_OR_B( frames[j]->i_type ) )
frames[j]->i_type = X264_TYPE_P; //设置为P
}
else if( frames[j]->i_type == X264_TYPE_AUTO ) //有B帧坑位
{
if( IS_X264_TYPE_B( frames[j+1]->i_type ) ) //后面是B则当前设P,同样为什么?
frames[j]->i_type = X264_TYPE_P;
else //后面是P则当前设B
frames[j]->i_type = X264_TYPE_B;
}
//更新坑位
if( IS_X264_TYPE_B( frames[j]->i_type ) ) //B帧
num_bframes--; //占一个坑位
else
num_bframes = h->param.i_bframe; //更新坑位
}
} //end of b_adaptive = X264_B_ADAPT_NONE 自适应B帧判别结束
if( IS_X264_TYPE_AUTO_OR_B( frames[num_frames]->i_type ) ) //如果最后一帧是B,则改P
frames[num_frames]->i_type = X264_TYPE_P;
//从第1帧开始统计连续的B帧数量
int num_bframes = 0;
while( num_bframes < num_frames && IS_X264_TYPE_B( frames[num_bframes+1]->i_type ) )
num_bframes++;
/* Check scenecut on the first minigop. */
for( int j = 1; j < num_bframes+1; j++ ) //遍历连续的B帧
{
if( frames[j]->i_forced_type == X264_TYPE_AUTO && IS_X264_TYPE_AUTO_OR_I( frames[j+1]->i_forced_type ) &&
h->param.i_scenecut_threshold && scenecut( h, &a, frames, j, j+1, 0, orig_num_frames, i_max_search ) )
{ //若后一帧是相对当前帧是场景切换,则当前帧设置为P帧
frames[j]->i_type = X264_TYPE_P;
num_analysed_frames = j;
break;
}
}
reset_start = keyframe ? 1 : X264_MIN( num_bframes+2, num_analysed_frames+1 );
}
else //不允许B帧
{
for( int j = 1; j <= num_frames; j++ )
if( IS_X264_TYPE_AUTO_OR_B( frames[j]->i_type ) ) //所有AUTO/B帧全改P
frames[j]->i_type = X264_TYPE_P;
reset_start = !keyframe + 1;
}
/* Perform the actual macroblock tree analysis.
* Don't go farther than the maximum keyframe interval; this helps in short GOPs. */
if( h->param.rc.b_mb_tree ) //使用mb tree
macroblock_tree( h, &a, frames, X264_MIN(num_frames, h->param.i_keyint_max), keyframe );
/* Enforce keyframe limit. 进行关键帧限制 */
if( !h->param.b_intra_refresh ) //是否用定期的帧内刷新来替代IDR
{
int last_keyframe = h->lookahead->i_last_keyframe; //取最近一个关键帧
int last_possible = 0;
for( int j = 1; j <= num_frames; j++ ) //遍历所有帧
{
x264_frame_t *frm = frames[j];
int keyframe_dist = frm->i_frame - last_keyframe; //计算距离上一个关键帧的距离
if( IS_X264_TYPE_AUTO_OR_I( frm->i_forced_type ) ) //若i_forced_type是I或auto
{
if( h->param.b_open_gop || !IS_X264_TYPE_B( frames[j-1]->i_forced_type ) )//openGOP || 前一帧非B
last_possible = j;
}
if( keyframe_dist >= h->param.i_keyint_max )
{ //若当前帧距离上一关键帧的距离 >= 最大关键帧间距离,则依据openGOP设置当前帧为I/IDR
if( last_possible != 0 && last_possible != j )
{
j = last_possible;
frm = frames[j];
keyframe_dist = frm->i_frame - last_keyframe;
}
last_possible = 0;
if( frm->i_type != X264_TYPE_IDR ) //非IDR,根据openGOP设置I/IDR
frm->i_type = h->param.b_open_gop ? X264_TYPE_I : X264_TYPE_IDR;
}
if( frm->i_type == X264_TYPE_I && keyframe_dist >= h->param.i_keyint_min )
{ //若是I帧 && 当前帧距离上一关键帧的距离 >= 最小关键帧间距离
if( h->param.b_open_gop ) //openGOP
{
last_keyframe = frm->i_frame; //更新当前帧为上一关键帧
if( h->param.b_bluray_compat )
{
// Use bluray order
int bframes = 0;
while( bframes < j-1 && IS_X264_TYPE_B( frames[j-1-bframes]->i_type ) )
bframes++;
last_keyframe -= bframes;
}
}
else if( frm->i_forced_type != X264_TYPE_I )//非openGOP,且i_forced_type不是I帧,则设置为IDR
frm->i_type = X264_TYPE_IDR;
}
if( frm->i_type == X264_TYPE_IDR ) //若是IDR
{
last_keyframe = frm->i_frame; //更新 最近关键帧
if( j > 1 && IS_X264_TYPE_B( frames[j-1]->i_type ) ) //若前一帧是B
frames[j-1]->i_type = X264_TYPE_P; //改为P
}
}
}
if( b_vbv_lookahead ) //若允许vbv,则进行vbv
vbv_lookahead( h, &a, frames, num_frames, keyframe );
/* Restore frametypes for all frames that haven't actually been decided yet. */
//将需要重新设置帧类型的帧全部重置
for( int j = reset_start; j <= num_frames; j++ )
frames[j]->i_type = frames[j]->i_forced_type;
}
其中使用自适应B帧 X264_B_ADAPT_TRELLIS方法部分:
/* Viterbi/trellis slicetype decision algorithm. */
/* Uses strings due to the fact that the speed of the control functions is
negligible compared to the cost of running slicetype_frame_cost, and because
it makes debugging easier.
使用viterbi算法来确定frames[1~length]的最优帧类型路径
将结果存放在best_paths[length % (X264_BFRAME_MAX+1)]中,以字符I/P/B表示
*/
static void slicetype_path( x264_t *h, x264_mb_analysis_t *a, x264_frame_t **frames, int length, char (*best_paths)[X264_LOOKAHEAD_MAX+1] )
{
char paths[2][X264_LOOKAHEAD_MAX+1];
//分析的路径数量最多i_bframe+1个,即最多向前分析到BBB...BBBP,length表示需要1~length帧,取两者最小值
int num_paths = X264_MIN( h->param.i_bframe+1, length );
uint64_t best_cost = COST_MAX64; //初始化best_cost为MAX
int best_possible = 0;
int idx = 0;
/* Iterate over all currently possible paths */
for( int path = 0; path < num_paths; path++ ) //遍历每条路径
{
/* Add suffixes to the current path 构造路径
* 结构:
* | length |
* 即 ( best-path ,B ... B, P )
* | len个 | path个 |1个|
* path range:
* | len个 |1个| path = 0
* | len个 |1个|1个| path = 1
* | len个 | 2个|1个| path = 2
* | len个 | 3个 |1个| path = 3
* ... ...
* | len个 | num_paths个 |1个| path = num_paths */
int len = length - (path + 1);
memcpy( paths[idx], best_paths[len % (X264_BFRAME_MAX+1)], len );//拷贝之前已经计算好的最优路径
memset( paths[idx]+len, 'B', path ); //再到后面添加path个'B'
strcpy( paths[idx]+len+path, "P" ); //最后一帧改成P帧
int possible = 1;
for( int i = 1; i <= length; i++ ) //遍历待确定类型的每一帧,来修改path
{
int i_type = frames[i]->i_type;
if( i_type == X264_TYPE_AUTO ) //若auto则不改path
continue;
if( IS_X264_TYPE_B( i_type ) ) //若是B帧
//该帧在已确定的最优路径内 || 该帧为待分析的最后一帧 || 该帧在路径中标识为B
possible = possible && (i < len || i == length || paths[idx][i-1] == 'B');
else //若非B则改path
{
//该帧在已确定的最优路径内 || 该帧在路径中标识为非B
possible = possible && (i < len || paths[idx][i-1] != 'B');
paths[idx][i-1] = IS_X264_TYPE_I( i_type ) ? 'I' : 'P'; //根据帧类型修改路径中的标识
}
}
if( possible || !best_possible ) //possible和best_possible什么意思???
{
if( possible && !best_possible )
best_cost = COST_MAX64;
/* Calculate the actual cost of the current path 计算当前路径paths[idx]的开销*/
uint64_t cost = slicetype_path_cost( h, a, frames, paths[idx], best_cost );
if( cost < best_cost ) //更新best,且切换paths idx
{
best_cost = cost;
best_possible = possible;
idx ^= 1;
}
}
} //end of for( int path = 0; path < num_paths; path++ )
/* Store the best path. 存储最优路径*/
memcpy( best_paths[length % (X264_BFRAME_MAX+1)], paths[idx^1], length );
}
其中使用自适应B帧 X264_B_ADAPT_FAST方法部分:
/*
过程:
1. 根据前一帧的帧类型来更新B帧的坑位以及最近的非B帧
2. 若B帧没坑位,则将AUTO/B改P,则continue
3. 若已经确定了帧类型,即非AUTO,则continue
4. 若后面一帧是B帧,设置当前帧为P帧
若后面一帧是P帧,计算当前帧为B帧时和P帧时的帧类型路径开销,选开销小的作为当前帧类型
*/
if( h->param.i_bframe_adaptive == X264_B_ADAPT_FAST ) //b_adaptive=1,快速决定
{
int last_nonb = 0;
int num_bframes = h->param.i_bframe; //得到两个参考帧中最多所允许的B帧数量
char path[X264_LOOKAHEAD_MAX+1];
for( int j = 1; j < num_frames; j++ ) //遍历list里的每一帧,除了最后一帧
{
/*
针对前一帧来更新 B坑数num_bframes 和 最近非B帧last_nonb
*/
if( j-1 > 0 && IS_X264_TYPE_B( frames[j-1]->i_type ) ) //若前一帧为B帧
num_bframes--; //则占一个B帧的坑
else //若前一帧为非B帧
{
last_nonb = j-1; //更新最近非B帧为前一帧
num_bframes = h->param.i_bframe; //这重置B帧的坑数了
}
/*
设置当前帧的帧类型
*/
if( !num_bframes ) //若B帧坑占满了,且当前帧B/auto则改P,其余不变
{
if( IS_X264_TYPE_AUTO_OR_B( frames[j]->i_type ) )
frames[j]->i_type = X264_TYPE_P;
continue;
}
// else 允许到这里的帧允许使用B帧 (还有B坑)
if( frames[j]->i_type != X264_TYPE_AUTO ) //若已经有类型了,则下一个
continue;
// else 运行到这里的帧全是AUTO帧
if( IS_X264_TYPE_B( frames[j+1]->i_type ) ) //若后面一帧为B帧
{
frames[j]->i_type = X264_TYPE_P; //当前帧改P,为什么??
continue;
}
/* else 运行到这里的帧后面一帧一定是P帧,不可能是I帧,
因为之前将所有I帧的前一帧设置成P帧,即只可能PI,不可能BI; */
int bframes = j - last_nonb - 1; //得到上一个非B帧到最后一帧之间的B帧数量bframes
memset( path, 'B', bframes ); //在path中填充bframes个B
// 将当前帧设置为P帧,计算BBB...BBB(P)P帧类型路径的开销
strcpy( path+bframes, "PP" );
uint64_t cost_p = slicetype_path_cost( h, &a, frames+last_nonb, path, COST_MAX64 );
// 将当前帧设置为B帧,计算BBB...BBB(B)P帧类型路径的开销
strcpy( path+bframes, "BP" );
uint64_t cost_b = slicetype_path_cost( h, &a, frames+last_nonb, path, cost_p );
//选择开销最小的,定当前帧的帧类型
if( cost_b < cost_p )
frames[j]->i_type = X264_TYPE_B;
else
frames[j]->i_type = X264_TYPE_P;
}
} //end of b_adaptive = X264_B_ADAPT_FAST
其中不使用自适应B帧(X264_B_ADAPT_NONE)部分:
/*
过程:
1. 若没有B帧的坑位了则将AUTO/B改P
2. 若有B帧坑位,则
· 后面一帧为B,当前设P
· 后面一帧为P,当前设B
3. 更新B帧的坑位
*/
else //b_adaptive=0,即不使用自适应B帧
{
int num_bframes = h->param.i_bframe;
for( int j = 1; j < num_frames; j++ )
{
if( !num_bframes ) //没有B帧坑位了
{
if( IS_X264_TYPE_AUTO_OR_B( frames[j]->i_type ) )
frames[j]->i_type = X264_TYPE_P; //设置为P
}
else if( frames[j]->i_type == X264_TYPE_AUTO ) //有B帧坑位
{
if( IS_X264_TYPE_B( frames[j+1]->i_type ) ) //后面是B则当前设P,同样为什么?
frames[j]->i_type = X264_TYPE_P;
else //后面是P则当前设B
frames[j]->i_type = X264_TYPE_B;
}
//更新B帧的坑位
if( IS_X264_TYPE_B( frames[j]->i_type ) ) //B帧
num_bframes--; //占一个坑位
else
num_bframes = h->param.i_bframe; //更新坑位
}
} //end of b_adaptive = X264_B_ADAPT_NONE