📄 slicetype.c
字号:
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; frames[b]->i_cost_est[0][0] = 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 %6d imb:%d \n", frames[b]->i_frame,// (p1==0?'I':b<p1?'B':'P'), b-p0, p1-b, i_score, frames[b]->i_cost_est[0][0], frames[b]->i_intra_mbs[b-p0] ); x264_cpu_restore( h->param.cpu ); } if( b_intra_penalty ) { // arbitrary penalty for I-blocks after B-frames int nmb = (h->sps->i_mb_width - 2) * (h->sps->i_mb_height - 2); i_score += i_score * frames[b]->i_intra_mbs[b-p0] / (nmb * 8); } return i_score;}static int scenecut( x264_t *h, x264_frame_t *frame, int pdist ){ int icost = frame->i_cost_est[0][0]; int pcost = frame->i_cost_est[pdist][0]; float f_bias; int i_gop_size = frame->i_frame - h->frames.i_last_idr; float f_thresh_max = h->param.i_scenecut_threshold / 100.0; /* magic numbers pulled out of thin air */ float f_thresh_min = f_thresh_max * h->param.i_keyint_min / ( h->param.i_keyint_max * 4 ); int res; if( h->param.i_keyint_min == h->param.i_keyint_max ) f_thresh_min= f_thresh_max; if( i_gop_size < h->param.i_keyint_min / 4 ) f_bias = f_thresh_min / 4; else if( i_gop_size <= h->param.i_keyint_min ) f_bias = f_thresh_min * i_gop_size / h->param.i_keyint_min; else { f_bias = f_thresh_min + ( f_thresh_max - f_thresh_min ) * ( i_gop_size - h->param.i_keyint_min ) / ( h->param.i_keyint_max - h->param.i_keyint_min ); } res = pcost >= (1.0 - f_bias) * icost; if( res ) { int imb = frame->i_intra_mbs[pdist]; int pmb = (h->sps->i_mb_width - 2) * (h->sps->i_mb_height - 2) - imb; x264_log( h, X264_LOG_DEBUG, "scene cut at %d Icost:%d Pcost:%d ratio:%.4f bias:%.4f gop:%d (imb:%d pmb:%d)\n", frame->i_frame, icost, pcost, 1. - (double)pcost / icost, f_bias, i_gop_size, imb, pmb ); } return res;}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; int idr_frame_type; assert( h->frames.b_have_lowres ); 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; x264_lowres_context_init( h, &a ); idr_frame_type = frames[1]->i_frame - h->frames.i_last_idr >= h->param.i_keyint_min ? X264_TYPE_IDR : X264_TYPE_I; if( num_frames == 1 ) {no_b_frames: frames[1]->i_type = X264_TYPE_P; if( h->param.b_pre_scenecut ) { x264_slicetype_frame_cost( h, &a, frames, 0, 1, 1, 0 ); if( scenecut( h, frames[1], 1 ) ) frames[1]->i_type = idr_frame_type; } return; } cost2p1 = x264_slicetype_frame_cost( h, &a, frames, 0, 2, 2, 1 ); 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, 0 ); cost1p0 = x264_slicetype_frame_cost( h, &a, frames, 0, 1, 1, 0 ); cost2p0 = x264_slicetype_frame_cost( h, &a, frames, 1, 2, 2, 0 );// 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, 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) || h->param.b_pre_scenecut ) 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, 0 ); 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 + -