📄 slicetype_decision.c
字号:
}
if( p1 > p0+1 )
i_bcost = i_bcost * 9 / 8; // arbitray penalty for I-blocks in and after B-frames
}
}
return i_bcost;
}
#undef TRY_BIDIR
#undef SAVE_MVS
int x264_slicetype_frame_cost( x264_t *h, x264_mb_analysis_t *a,
x264_frame_t **frames, int p0, int p1, int b )
{
int i_score = 0;
int dist_scale_factor = 128;
int *row_satd = frames[b]->i_row_satds[b-p0][p1-b];
/* Check whether we already evaluated this frame
* If we have tried this frame as P, then we have also tried
* the preceding frames as B. (is this still true?) */
if( frames[b]->i_cost_est[b-p0][p1-b] >= 0 )
return frames[b]->i_cost_est[b-p0][p1-b];
/* Init MVs so that we don't have to check edge conditions when loading predictors. */
/* FIXME: not needed every time */
memset( frames[b]->mv[0], 0, h->sps->i_mb_height * h->sps->i_mb_width * 2*sizeof(int16_t) );
if( b != p1 )
memset( frames[b]->mv[1], 0, h->sps->i_mb_height * h->sps->i_mb_width * 2*sizeof(int16_t) );
if( b == p1 )
frames[b]->i_intra_mbs[b-p0] = 0;
if( p1 != p0 )
dist_scale_factor = ( ((b-p0) << 8) + ((p1-p0) >> 1) ) / (p1-p0);
/* the edge mbs seem to reduce the predictive quality of the
* whole frame's score, but are needed for a spatial distribution. */
if( h->param.rc.i_vbv_buffer_size )
{
for( h->mb.i_mb_y = 0; h->mb.i_mb_y < h->sps->i_mb_height; h->mb.i_mb_y++ )
{
row_satd[ h->mb.i_mb_y ] = 0;
for( h->mb.i_mb_x = 0; h->mb.i_mb_x < h->sps->i_mb_width; h->mb.i_mb_x++ )
{
int i_mb_cost = x264_slicetype_mb_cost( h, a, frames, p0, p1, b, dist_scale_factor );
row_satd[ h->mb.i_mb_y ] += i_mb_cost;
if( h->mb.i_mb_y > 0 && h->mb.i_mb_y < h->sps->i_mb_height - 1 &&
h->mb.i_mb_x > 0 && h->mb.i_mb_x < h->sps->i_mb_width - 1 )
{
i_score += i_mb_cost;
}
}
}
}
else
{
for( h->mb.i_mb_y = 1; h->mb.i_mb_y < h->sps->i_mb_height - 1; h->mb.i_mb_y++ )
for( h->mb.i_mb_x = 1; h->mb.i_mb_x < h->sps->i_mb_width - 1; h->mb.i_mb_x++ )
i_score += x264_slicetype_mb_cost( h, a, frames, p0, p1, b, dist_scale_factor );
}
if( b != p1 )
i_score = i_score * 100 / (120 + h->param.i_bframe_bias);
frames[b]->i_cost_est[b-p0][p1-b] = i_score;
// fprintf( stderr, "frm %d %c(%d,%d): %6d I:%d \n", frames[b]->i_frame,
// (p1==0?'I':b<p1?'B':'P'), b-p0, p1-b, i_score, frames[b]->i_intra_mbs[b-p0] );
/// x264_cpu_restore( h->param.cpu );
return i_score;
}
void x264_slicetype_analyse( x264_t *h )
{
x264_mb_analysis_t a;
x264_frame_t *frames[X264_BFRAME_MAX+3] = { NULL, };
int num_frames;
int keyint_limit;
int j;
int i_mb_count = (h->sps->i_mb_width - 2) * (h->sps->i_mb_height - 2);
int cost1p0, cost2p0, cost1b1, cost2p1;
if( !h->frames.last_nonb )
return;
frames[0] = h->frames.last_nonb;
for( j = 0; h->frames.next[j]; j++ )
frames[j+1] = h->frames.next[j];
keyint_limit = h->param.i_keyint_max - frames[0]->i_frame + h->frames.i_last_idr - 1;
num_frames = X264_MIN( j, keyint_limit );
if( num_frames == 0 )
return;
if( num_frames == 1 )
{
no_b_frames:
frames[1]->i_type = X264_TYPE_P;
return;
}
x264_lowres_context_init( h, &a );
cost2p1 = x264_slicetype_frame_cost( h, &a, frames, 0, 2, 2 );
if( frames[2]->i_intra_mbs[2] > i_mb_count / 2 )
goto no_b_frames;
cost1b1 = x264_slicetype_frame_cost( h, &a, frames, 0, 2, 1 );
cost1p0 = x264_slicetype_frame_cost( h, &a, frames, 0, 1, 1 );
cost2p0 = x264_slicetype_frame_cost( h, &a, frames, 1, 2, 2 );
// fprintf( stderr, "PP: %d + %d <=> BP: %d + %d \n",
// cost1p0, cost2p0, cost1b1, cost2p1 );
if( cost1p0 + cost2p0 < cost1b1 + cost2p1 )
goto no_b_frames;
// arbitrary and untuned
#define INTER_THRESH 300
#define P_SENS_BIAS (50 - h->param.i_bframe_bias)
frames[1]->i_type = X264_TYPE_B;
for( j = 2; j <= X264_MIN( h->param.i_bframe, num_frames-1 ); j++ )
{
int pthresh = X264_MAX(INTER_THRESH - P_SENS_BIAS * (j-1), INTER_THRESH/10);
int pcost = x264_slicetype_frame_cost( h, &a, frames, 0, j+1, j+1 );
// fprintf( stderr, "frm%d+%d: %d <=> %d, I:%d/%d \n",
// frames[0]->i_frame, j-1, pthresh, pcost/i_mb_count,
// frames[j+1]->i_intra_mbs[j+1], i_mb_count );
if( pcost > pthresh*i_mb_count || frames[j+1]->i_intra_mbs[j+1] > i_mb_count/3 )
{
frames[j]->i_type = X264_TYPE_P;
break;
}
else
frames[j]->i_type = X264_TYPE_B;
}
}
void x264_slicetype_decide( x264_t *h )
{
x264_frame_t *frm;
int bframes;
int i;
if( h->frames.next[0] == NULL )
return;
if( h->param.rc.b_stat_read )
{
/* Use the frame types from the first pass */
for( i = 0; h->frames.next[i] != NULL; i++ )
h->frames.next[i]->i_type =
x264_ratecontrol_slice_type( h, h->frames.next[i]->i_frame );
}
else if( h->param.i_bframe && h->param.b_bframe_adaptive )
x264_slicetype_analyse( h );
for( bframes = 0;; bframes++ )
{
frm = h->frames.next[bframes];
/* Limit GOP size */
if( frm->i_frame - h->frames.i_last_idr >= h->param.i_keyint_max )
{
if( frm->i_type == X264_TYPE_AUTO )
frm->i_type = X264_TYPE_IDR;
if( frm->i_type != X264_TYPE_IDR )
x264_log( h, X264_LOG_WARNING, "specified frame type (%d) is not compatible with keyframe interval\n", frm->i_type );
}
if( frm->i_type == X264_TYPE_IDR )
{
/* Close GOP */
if( bframes > 0 )
{
bframes--;
h->frames.next[bframes]->i_type = X264_TYPE_P;
}
else
{
h->i_frame_num = 0;
}
}
if( bframes == h->param.i_bframe
|| h->frames.next[bframes+1] == NULL )
{
if( IS_X264_TYPE_B( frm->i_type ) )
x264_log( h, X264_LOG_WARNING, "specified frame type is not compatible with max B-frames\n" );
if( frm->i_type == X264_TYPE_AUTO
|| IS_X264_TYPE_B( frm->i_type ) )
frm->i_type = X264_TYPE_P;
}
if( frm->i_type != X264_TYPE_AUTO && frm->i_type != X264_TYPE_B && frm->i_type != X264_TYPE_BREF )
break;
frm->i_type = X264_TYPE_B;
}
}
int x264_rc_analyse_slice( x264_t *h )
{
x264_mb_analysis_t a;
x264_frame_t *frames[X264_BFRAME_MAX+2] = { NULL, };
int p0=0, p1, b;
int cost;
x264_lowres_context_init( h, &a );
if( IS_X264_TYPE_I(h->fenc->i_type) )
{
p1 = b = 0;
}
else if( X264_TYPE_P == h->fenc->i_type )
{
p1 = 0;
while( h->frames.current[p1] && IS_X264_TYPE_B( h->frames.current[p1]->i_type ) )
p1++;
p1++;
b = p1;
}
else //B
{
p1 = (h->fref1[0]->i_poc - h->fref0[0]->i_poc)/2;
b = (h->fref1[0]->i_poc - h->fenc->i_poc)/2;
frames[p1] = h->fref1[0];
}
frames[p0] = h->fref0[0];
frames[b] = h->fenc;
cost = x264_slicetype_frame_cost( h, &a, frames, p0, p1, b );
h->fenc->i_row_satd = h->fenc->i_row_satds[b-p0][p1-b];
h->fdec->i_row_satd = h->fdec->i_row_satds[b-p0][p1-b];
h->fdec->i_satd = cost;
memcpy( h->fdec->i_row_satd, h->fenc->i_row_satd, h->sps->i_mb_height * sizeof(int) );
return cost;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -