📄 ratecontrol.c.svn-base
字号:
{ rc->psz_stat_file_tmpname = x264_malloc( strlen(h->param.rc.psz_stat_out) + 6 ); strcpy( rc->psz_stat_file_tmpname, h->param.rc.psz_stat_out ); strcat( rc->psz_stat_file_tmpname, ".temp" ); rc->p_stat_file_out = fopen( rc->psz_stat_file_tmpname, "wb" ); if( rc->p_stat_file_out == NULL ) { x264_log(h, X264_LOG_ERROR, "ratecontrol_init: can't open stats file\n"); return -1; } } return 0;}void x264_ratecontrol_delete( x264_t *h ){ x264_ratecontrol_t *rc = h->rc; if( rc->p_stat_file_out ) { fclose( rc->p_stat_file_out ); if( h->i_frame >= rc->num_entries - h->param.i_bframe ) if( rename( rc->psz_stat_file_tmpname, h->param.rc.psz_stat_out ) != 0 ) { x264_log( h, X264_LOG_ERROR, "failed to rename \"%s\" to \"%s\"\n", rc->psz_stat_file_tmpname, h->param.rc.psz_stat_out ); } x264_free( rc->psz_stat_file_tmpname ); } if( rc->entry ) x264_free(rc->entry); x264_free( rc );}/* Before encoding a frame, choose a QP for it */void x264_ratecontrol_start( x264_t *h, int i_slice_type, int i_force_qp ){ x264_ratecontrol_t *rc = h->rc; x264_cpu_restore( h->param.cpu ); rc->qp_force = i_force_qp; rc->slice_type = i_slice_type; if( i_force_qp ) { rc->qpa = rc->qp = i_force_qp - 1; } else if( rc->b_abr ) { rc->qpa = rc->qp = x264_clip3( (int)(qscale2qp( rate_estimate_qscale( h, i_slice_type ) ) + .5), 0, 51 ); } else if( rc->b_2pass ) { int frame = h->fenc->i_frame; ratecontrol_entry_t *rce; assert( frame >= 0 && frame < rc->num_entries ); rce = h->rc->rce = &h->rc->entry[frame]; rce->new_qscale = rate_estimate_qscale( h, i_slice_type ); rc->qpa = rc->qp = rce->new_qp = x264_clip3( (int)(qscale2qp(rce->new_qscale) + 0.5), 0, 51 ); } else /* CQP */ { int q; if( i_slice_type == SLICE_TYPE_B && h->fdec->b_kept_as_ref ) q = ( rc->qp_constant[ SLICE_TYPE_B ] + rc->qp_constant[ SLICE_TYPE_P ] ) / 2; else q = rc->qp_constant[ i_slice_type ]; rc->qpa = rc->qp = q; }}void x264_ratecontrol_mb( x264_t *h, int bits ){ /* currently no adaptive quant */}int x264_ratecontrol_qp( x264_t *h ){ return h->rc->qp;}/* In 2pass, force the same frame types as in the 1st pass */int x264_ratecontrol_slice_type( x264_t *h, int frame_num ){ if( h->param.rc.b_stat_read ) { if( frame_num >= h->rc->num_entries ) { x264_log(h, X264_LOG_ERROR, "More input frames than in the 1st pass\n"); return X264_TYPE_P; } switch( h->rc->entry[frame_num].pict_type ) { case SLICE_TYPE_I: return h->rc->entry[frame_num].kept_as_ref ? X264_TYPE_IDR : X264_TYPE_I; case SLICE_TYPE_B: return h->rc->entry[frame_num].kept_as_ref ? X264_TYPE_BREF : X264_TYPE_B; case SLICE_TYPE_P: default: return X264_TYPE_P; } } else { return X264_TYPE_AUTO; }}/* After encoding one frame, save stats and update ratecontrol state */void x264_ratecontrol_end( x264_t *h, int bits ){ x264_ratecontrol_t *rc = h->rc; int i; x264_cpu_restore( h->param.cpu ); h->stat.frame.i_mb_count_skip = h->stat.frame.i_mb_count[P_SKIP] + h->stat.frame.i_mb_count[B_SKIP]; h->stat.frame.i_mb_count_p = h->stat.frame.i_mb_count[P_L0] + h->stat.frame.i_mb_count[P_8x8]; for( i = B_DIRECT; i < B_8x8; i++ ) h->stat.frame.i_mb_count_p += h->stat.frame.i_mb_count[i]; if( h->param.rc.b_stat_write ) { char c_type = rc->slice_type==SLICE_TYPE_I ? (h->fenc->i_poc==0 ? 'I' : 'i') : rc->slice_type==SLICE_TYPE_P ? 'P' : h->fenc->b_kept_as_ref ? 'B' : 'b'; fprintf( rc->p_stat_file_out, "in:%d out:%d type:%c q:%.2f itex:%d ptex:%d mv:%d misc:%d imb:%d pmb:%d smb:%d;\n", h->fenc->i_frame, h->i_frame-1, c_type, rc->qpa, h->stat.frame.i_itex_bits, h->stat.frame.i_ptex_bits, h->stat.frame.i_hdr_bits, h->stat.frame.i_misc_bits, h->stat.frame.i_mb_count[I_4x4] + h->stat.frame.i_mb_count[I_16x16], h->stat.frame.i_mb_count_p, h->stat.frame.i_mb_count_skip); } if( rc->b_abr ) { if( rc->slice_type != SLICE_TYPE_B ) rc->cplxr_sum += bits * qp2qscale(rc->qpa) / rc->last_rceq; else { /* Depends on the fact that B-frame's QP is an offset from the following P-frame's. * Not perfectly accurate with B-refs, but good enough. */ rc->cplxr_sum += bits * qp2qscale(rc->qpa) / (rc->last_rceq * fabs(h->param.rc.f_pb_factor)); } rc->cplxr_sum *= rc->cbr_decay; rc->wanted_bits_window += rc->bitrate / rc->fps; rc->wanted_bits_window *= rc->cbr_decay; rc->accum_p_qp *= .95; rc->accum_p_norm *= .95; rc->accum_p_norm += 1; if( rc->slice_type == SLICE_TYPE_I ) rc->accum_p_qp += rc->qpa * fabs(h->param.rc.f_ip_factor); else rc->accum_p_qp += rc->qpa; } if( rc->b_2pass ) { rc->expected_bits_sum += qscale2bits( rc->rce, qp2qscale(rc->rce->new_qp) ); } update_vbv( h, bits ); if( rc->slice_type != SLICE_TYPE_B ) rc->last_non_b_pict_type = rc->slice_type;}/**************************************************************************** * 2 pass functions ***************************************************************************/double x264_eval( char *s, double *const_value, const char **const_name, double (**func1)(void *, double), const char **func1_name, double (**func2)(void *, double, double), char **func2_name, void *opaque );/** * modify the bitrate curve from pass1 for one frame */static double get_qscale(x264_t *h, ratecontrol_entry_t *rce, double rate_factor){ x264_ratecontrol_t *rcc= h->rc; const int pict_type = rce->pict_type; double q; double const_values[]={ rce->i_tex_bits * rce->qscale, rce->p_tex_bits * rce->qscale, (rce->i_tex_bits + rce->p_tex_bits) * rce->qscale, rce->mv_bits * rce->qscale, (double)rce->i_count / rcc->nmb, (double)rce->p_count / rcc->nmb, (double)rce->s_count / rcc->nmb, rce->pict_type == SLICE_TYPE_I, rce->pict_type == SLICE_TYPE_P, rce->pict_type == SLICE_TYPE_B, h->param.rc.f_qcompress, rcc->i_cplx_sum[SLICE_TYPE_I] / rcc->frame_count[SLICE_TYPE_I], rcc->i_cplx_sum[SLICE_TYPE_P] / rcc->frame_count[SLICE_TYPE_P], rcc->p_cplx_sum[SLICE_TYPE_P] / rcc->frame_count[SLICE_TYPE_P], rcc->p_cplx_sum[SLICE_TYPE_B] / rcc->frame_count[SLICE_TYPE_B], (rcc->i_cplx_sum[pict_type] + rcc->p_cplx_sum[pict_type]) / rcc->frame_count[pict_type], rce->blurred_complexity, 0 }; static const char *const_names[]={ "iTex", "pTex", "tex", "mv", "iCount", "pCount", "sCount", "isI", "isP", "isB", "qComp", "avgIITex", "avgPITex", "avgPPTex", "avgBPTex", "avgTex", "blurCplx", NULL }; static double (*func1[])(void *, double)={// (void *)bits2qscale, (void *)qscale2bits, NULL }; static const char *func1_names[]={// "bits2qp", "qp2bits", NULL }; q = x264_eval((char*)h->param.rc.psz_rc_eq, const_values, const_names, func1, func1_names, NULL, NULL, rce); q /= rate_factor; // avoid NaN's in the rc_eq if(q != q || rce->i_tex_bits + rce->p_tex_bits + rce->mv_bits == 0) q = rcc->last_qscale; else rcc->last_qscale = q; return q;}static double get_diff_limited_q(x264_t *h, ratecontrol_entry_t *rce, double q){ x264_ratecontrol_t *rcc = h->rc; const int pict_type = rce->pict_type; // force I/B quants as a function of P quants const double last_p_q = rcc->last_qscale_for[SLICE_TYPE_P]; const double last_non_b_q= rcc->last_qscale_for[rcc->last_non_b_pict_type]; if( pict_type == SLICE_TYPE_I ) { double iq = q; double pq = qp2qscale( rcc->accum_p_qp / rcc->accum_p_norm ); double ip_factor = fabs( h->param.rc.f_ip_factor ); /* don't apply ip_factor if the following frame is also I */ if( rcc->accum_p_norm <= 0 ) q = iq; else if( h->param.rc.f_ip_factor < 0 ) q = iq / ip_factor; else if( rcc->accum_p_norm >= 1 ) q = pq / ip_factor; else q = rcc->accum_p_norm * pq / ip_factor + (1 - rcc->accum_p_norm) * iq; } else if( pict_type == SLICE_TYPE_B ) { if( h->param.rc.f_pb_factor > 0 ) q = last_non_b_q; if( !rce->kept_as_ref ) q *= fabs( h->param.rc.f_pb_factor ); } else if( pict_type == SLICE_TYPE_P && rcc->last_non_b_pict_type == SLICE_TYPE_P && rce->i_tex_bits + rce->p_tex_bits == 0 ) { q = last_p_q; } /* last qscale / qdiff stuff */ if(rcc->last_non_b_pict_type==pict_type && (pict_type!=SLICE_TYPE_I || rcc->last_accum_p_norm < 1)) { double last_q = rcc->last_qscale_for[pict_type]; double max_qscale = last_q * rcc->lstep; double min_qscale = last_q / rcc->lstep; if (q > max_qscale) q = max_qscale; else if(q < min_qscale) q = min_qscale; } rcc->last_qscale_for[pict_type] = q; if(pict_type!=SLICE_TYPE_B) rcc->last_non_b_pict_type = pict_type; if(pict_type==SLICE_TYPE_I) { rcc->last_accum_p_norm = rcc->accum_p_norm; rcc->accum_p_norm = 0; rcc->accum_p_qp = 0; } if(pict_type==SLICE_TYPE_P) { float mask = 1 - pow( (float)rce->i_count / rcc->nmb, 2 ); rcc->accum_p_qp = mask * (qscale2qp(q) + rcc->accum_p_qp); rcc->accum_p_norm = mask * (1 + rcc->accum_p_norm); } return q;}static double predict_size( predictor_t *p, double q, double var ){ return p->coeff*var / (q*p->count);}static void update_predictor( predictor_t *p, double q, double var, double bits ){ p->count *= p->decay; p->coeff *= p->decay; p->count ++; p->coeff += bits*q / var;}static void update_vbv( x264_t *h, int bits ){ x264_ratecontrol_t *rcc = h->rc; if( !rcc->buffer_size ) return; rcc->buffer_fill += rcc->buffer_rate - bits; if( rcc->buffer_fill < 0 && !rcc->b_2pass ) x264_log( h, X264_LOG_WARNING, "VBV underflow (%.0f bits)\n", rcc->buffer_fill ); rcc->buffer_fill = x264_clip3( rcc->buffer_fill, 0, rcc->buffer_size ); if(rcc->last_satd > 100) update_predictor( &rcc->pred[rcc->slice_type], qp2qscale(rcc->qpa), rcc->last_satd, bits );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -