📄 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; } p = x264_param2string( &h->param, 1 ); fprintf( rc->p_stat_file_out, "#options: %s\n", p ); x264_free( p ); } return 0;}static int parse_zones( x264_t *h ){ x264_ratecontrol_t *rc = h->rc; int i; if( h->param.rc.psz_zones && !h->param.rc.i_zones ) { char *p; h->param.rc.i_zones = 1; for( p = h->param.rc.psz_zones; *p; p++ ) h->param.rc.i_zones += (*p == '/'); h->param.rc.zones = x264_malloc( h->param.rc.i_zones * sizeof(x264_zone_t) ); p = h->param.rc.psz_zones; for( i = 0; i < h->param.rc.i_zones; i++) { x264_zone_t *z = &h->param.rc.zones[i]; if( 3 == sscanf(p, "%u,%u,q=%u", &z->i_start, &z->i_end, &z->i_qp) ) z->b_force_qp = 1; else if( 3 == sscanf(p, "%u,%u,b=%f", &z->i_start, &z->i_end, &z->f_bitrate_factor) ) z->b_force_qp = 0; else { char *slash = strchr(p, '/'); if(slash) *slash = '\0'; x264_log( h, X264_LOG_ERROR, "invalid zone: \"%s\"\n", p ); return -1; } p = strchr(p, '/') + 1; } } if( h->param.rc.i_zones > 0 ) { for( i = 0; i < h->param.rc.i_zones; i++ ) { x264_zone_t z = h->param.rc.zones[i]; if( z.i_start < 0 || z.i_start > z.i_end ) { x264_log( h, X264_LOG_ERROR, "invalid zone: start=%d end=%d\n", z.i_start, z.i_end ); return -1; } else if( !z.b_force_qp && z.f_bitrate_factor <= 0 ) { x264_log( h, X264_LOG_ERROR, "invalid zone: bitrate_factor=%f\n", z.f_bitrate_factor ); return -1; } } rc->i_zones = h->param.rc.i_zones; rc->zones = x264_malloc( rc->i_zones * sizeof(x264_zone_t) ); memcpy( rc->zones, h->param.rc.zones, rc->i_zones * sizeof(x264_zone_t) ); } return 0;}void x264_ratecontrol_summary( x264_t *h ){ x264_ratecontrol_t *rc = h->rc; if( rc->b_abr && !h->param.rc.i_rf_constant && !h->param.rc.i_vbv_max_bitrate ) { double base_cplx = h->mb.i_mb_count * (h->param.i_bframe ? 120 : 80); x264_log( h, X264_LOG_INFO, "final ratefactor: %.2f\n", qscale2qp( pow( base_cplx, 1 - h->param.rc.f_qcompress ) * rc->cplxr_sum / rc->wanted_bits_window ) ); }}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 ); } x264_free( rc->entry ); x264_free( rc->zones ); 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 ){ x264_ratecontrol_t *rc = h->rc; if( h->param.rc.b_stat_read ) { if( frame_num >= rc->num_entries ) { /* We could try to initialize everything required for ABR and * adaptive B-frames, but that would be complicated. * So just calculate the average QP used so far. */ h->param.rc.i_qp_constant = (h->stat.i_slice_count[SLICE_TYPE_P] == 0) ? 24 : 1 + h->stat.i_slice_qp[SLICE_TYPE_P] / h->stat.i_slice_count[SLICE_TYPE_P]; rc->qp_constant[SLICE_TYPE_P] = x264_clip3( h->param.rc.i_qp_constant, 0, 51 ); rc->qp_constant[SLICE_TYPE_I] = x264_clip3( (int)( qscale2qp( qp2qscale( h->param.rc.i_qp_constant ) / fabs( h->param.rc.f_ip_factor )) + 0.5 ), 0, 51 ); rc->qp_constant[SLICE_TYPE_B] = x264_clip3( (int)( qscale2qp( qp2qscale( h->param.rc.i_qp_constant ) * fabs( h->param.rc.f_pb_factor )) + 0.5 ), 0, 51 ); x264_log(h, X264_LOG_ERROR, "2nd pass has more frames than 1st pass (%d)\n", rc->num_entries); x264_log(h, X264_LOG_ERROR, "continuing anyway, at constant QP=%d\n", h->param.rc.i_qp_constant); if( h->param.b_bframe_adaptive ) x264_log(h, X264_LOG_ERROR, "disabling adaptive B-frames\n"); rc->b_abr = 0; rc->b_2pass = 0; h->param.rc.b_cbr = 0; h->param.rc.b_stat_read = 0; h->param.b_bframe_adaptive = 0; if( h->param.i_bframe > 1 ) h->param.i_bframe = 1; return X264_TYPE_P; } switch( rc->entry[frame_num].pict_type ) { case SLICE_TYPE_I: return rc->entry[frame_num].kept_as_ref ? X264_TYPE_IDR : X264_TYPE_I; case SLICE_TYPE_B: return 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; const int *mbs = h->stat.frame.i_mb_count; int i; x264_cpu_restore( h->param.cpu ); h->stat.frame.i_mb_count_skip = mbs[P_SKIP] + mbs[B_SKIP]; h->stat.frame.i_mb_count_i = mbs[I_16x16] + mbs[I_8x8] + mbs[I_4x4]; h->stat.frame.i_mb_count_p = mbs[P_L0] + mbs[P_8x8]; for( i = B_DIRECT; i < B_8x8; i++ ) h->stat.frame.i_mb_count_p += mbs[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, 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, int frame_num){ x264_ratecontrol_t *rcc= h->rc; const int pict_type = rce->pict_type; double q; int i; 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); // avoid NaN's in the rc_eq if(!isfinite(q) || rce->i_tex_bits + rce->p_tex_bits + rce->mv_bits == 0) q = rcc->last_qscale; else { rcc->last_rceq = q; q /= rate_factor; rcc->last_qscale = q; } for( i = rcc->i_zones-1; i >= 0; i-- ) { x264_zone_t *z = &rcc->zones[i]; if( frame_num >= z->i_start && frame_num <= z->i_end ) { if( z->b_force_qp ) q = qp2qscale(z->i_qp); else q /= z->f_bitrate_factor; break; } } 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];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -