📄 ratecontrol.c
字号:
&rce->mv_bits, &rce->misc_bits, &rce->i_count, &rce->p_count, &rce->s_count, &rce->direct_mode); switch(pict_type){ case 'I': rce->kept_as_ref = 1; case 'i': rce->pict_type = SLICE_TYPE_I; break; case 'P': rce->pict_type = SLICE_TYPE_P; break; case 'B': rce->kept_as_ref = 1; case 'b': rce->pict_type = SLICE_TYPE_B; break; default: e = -1; break; } if(e < 10){ x264_log(h, X264_LOG_ERROR, "statistics are damaged at line %d, parser out=%d\n", i, e); return -1; } rce->qscale = qp2qscale(qp); p = next; } x264_free(stats_buf); if(h->param.rc.i_rc_method == X264_RC_ABR) { if(init_pass2(h) < 0) return -1; } /* else we're using constant quant, so no need to run the bitrate allocation */ } /* Open output file */ /* If input and output files are the same, output to a temp file * and move it to the real name only when it's complete */ if( h->param.rc.b_stat_write ) { char *p; 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 ); } for( i=1; i<h->param.i_threads; i++ ) { h->thread[i]->rc = rc+i; rc[i] = rc[0]; } return 0;}static int parse_zone( x264_t *h, x264_zone_t *z, char *p ){ int len = 0; char *tok, *saveptr; z->param = NULL; z->f_bitrate_factor = 1; if( 3 <= sscanf(p, "%u,%u,q=%u%n", &z->i_start, &z->i_end, &z->i_qp, &len) ) z->b_force_qp = 1; else if( 3 <= sscanf(p, "%u,%u,b=%f%n", &z->i_start, &z->i_end, &z->f_bitrate_factor, &len) ) z->b_force_qp = 0; else if( 2 <= sscanf(p, "%u,%u%n", &z->i_start, &z->i_end, &len) ) z->b_force_qp = 0; else { x264_log( h, X264_LOG_ERROR, "invalid zone: \"%s\"\n", p ); return -1; } p += len; if( !*p ) return 0; z->param = malloc( sizeof(x264_param_t) ); memcpy( z->param, &h->param, sizeof(x264_param_t) ); while( (tok = strtok_r( p, ",", &saveptr )) ) { char *val = strchr( tok, '=' ); if( val ) { *val = '\0'; val++; } if( x264_param_parse( z->param, tok, val ) ) { x264_log( h, X264_LOG_ERROR, "invalid zone param: %s = %s\n", tok, val ); return -1; } p = NULL; } 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, *tok, *saveptr; char *psz_zones = x264_malloc( strlen(h->param.rc.psz_zones)+1 ); strcpy( psz_zones, h->param.rc.psz_zones ); h->param.rc.i_zones = 1; for( p = 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 = psz_zones; for( i = 0; i < h->param.rc.i_zones; i++ ) { tok = strtok_r( p, "/", &saveptr ); if( !tok || parse_zone( h, &h->param.rc.zones[i], tok ) ) return -1; p = NULL; } x264_free( psz_zones ); } 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 + 1; rc->zones = x264_malloc( rc->i_zones * sizeof(x264_zone_t) ); memcpy( rc->zones+1, h->param.rc.zones, (rc->i_zones-1) * sizeof(x264_zone_t) ); // default zone to fall back to if none of the others match rc->zones[0].i_start = 0; rc->zones[0].i_end = INT_MAX; rc->zones[0].b_force_qp = 0; rc->zones[0].f_bitrate_factor = 1; rc->zones[0].param = x264_malloc( sizeof(x264_param_t) ); memcpy( rc->zones[0].param, &h->param, sizeof(x264_param_t) ); for( i = 1; i < rc->i_zones; i++ ) { if( !rc->zones[i].param ) rc->zones[i].param = rc->zones[0].param; } } return 0;}x264_zone_t *get_zone( x264_t *h, int frame_num ){ int i; for( i = h->rc->i_zones-1; i >= 0; i-- ) { x264_zone_t *z = &h->rc->zones[i]; if( frame_num >= z->i_start && frame_num <= z->i_end ) return z; } return NULL;}void x264_ratecontrol_summary( x264_t *h ){ x264_ratecontrol_t *rc = h->rc; if( rc->b_abr && h->param.rc.i_rc_method == X264_RC_ABR && rc->cbr_decay > .9999 ) { 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; int i; 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->pred ); x264_free( rc->pred_b_from_p ); x264_free( rc->entry ); if( rc->zones ) { x264_free( rc->zones[0].param ); if( h->param.rc.psz_zones ) for( i=1; i<rc->i_zones; i++ ) if( rc->zones[i].param != rc->zones[0].param ) x264_free( rc->zones[i].param ); x264_free( rc->zones ); } x264_free( rc );}static void accum_p_qp_update( x264_t *h, float qp ){ x264_ratecontrol_t *rc = h->rc; rc->accum_p_qp *= .95; rc->accum_p_norm *= .95; rc->accum_p_norm += 1; if( h->sh.i_type == SLICE_TYPE_I ) rc->accum_p_qp += qp + rc->ip_offset; else rc->accum_p_qp += qp;}/* Before encoding a frame, choose a QP for it */void x264_ratecontrol_start( x264_t *h, int i_force_qp ){ x264_ratecontrol_t *rc = h->rc; ratecontrol_entry_t *rce = NULL; x264_zone_t *zone = get_zone( h, h->fenc->i_frame ); float q; x264_cpu_restore( h->param.cpu ); if( zone && (!rc->prev_zone || zone->param != rc->prev_zone->param) ) x264_encoder_reconfig( h, zone->param ); rc->prev_zone = zone; rc->qp_force = i_force_qp; if( h->param.rc.b_stat_read ) { int frame = h->fenc->i_frame; assert( frame >= 0 && frame < rc->num_entries ); rce = h->rc->rce = &h->rc->entry[frame]; if( h->sh.i_type == SLICE_TYPE_B && h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_AUTO ) { h->sh.b_direct_spatial_mv_pred = ( rce->direct_mode == 's' ); h->mb.b_direct_auto_read = ( rce->direct_mode == 's' || rce->direct_mode == 't' ); } } if( rc->b_vbv ) { memset( h->fdec->i_row_bits, 0, h->sps->i_mb_height * sizeof(int) ); rc->row_pred = &rc->row_preds[h->sh.i_type]; update_vbv_plan( h ); } if( h->sh.i_type != SLICE_TYPE_B ) { rc->bframes = 0; while( h->frames.current[rc->bframes] && IS_X264_TYPE_B(h->frames.current[rc->bframes]->i_type) ) rc->bframes++; } rc->qpa = 0; if( i_force_qp ) { q = i_force_qp - 1; } else if( rc->b_abr ) { q = qscale2qp( rate_estimate_qscale( h ) ); } else if( rc->b_2pass ) { rce->new_qscale = rate_estimate_qscale( h ); q = qscale2qp( rce->new_qscale ); } else /* CQP */ { if( h->sh.i_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[ h->sh.i_type ]; if( zone ) { if( zone->b_force_qp ) q += zone->i_qp - rc->qp_constant[SLICE_TYPE_P]; else q -= 6*log(zone->f_bitrate_factor)/log(2); } } h->fdec->f_qp_avg = rc->qpm = rc->qp = x264_clip3( (int)(q + 0.5), 0, 51 ); if( rce ) rce->new_qp = rc->qp; /* accum_p_qp needs to be here so that future frames can benefit from the * data before this frame is done. but this only works because threading * guarantees to not re-encode any frames. so the non-threaded case does * accum_p_qp later. */ if( h->param.i_threads > 1 ) accum_p_qp_update( h, rc->qp ); if( h->sh.i_type != SLICE_TYPE_B ) rc->last_non_b_pict_type = h->sh.i_type;}double predict_row_size( x264_t *h, int y, int qp ){ /* average between two predictors: * absolute SATD, and scaled bit cost of the colocated row in the previous frame */ x264_ratecontrol_t *rc = h->rc; double pred_s = predict_size( rc->row_pred, qp2qscale(qp), h->fdec->i_row_satd[y] ); double pred_t = 0; if( h->sh.i_type != SLICE_TYPE_I && h->fref0[0]->i_type == h->fdec->i_type && h->fref0[0]->i_row_satd[y] > 0 ) { pred_t = h->fref0[0]->i_row_bits[y] * h->fdec->i_row_satd[y] / h->fref0[0]->i_row_satd[y] * qp2qscale(h->fref0[0]->i_row_qp[y]) / qp2qscale(qp); } if( pred_t == 0 ) pred_t = pred_s; return (pred_s + pred_t) / 2;}double predict_row_size_sum( x264_t *h, int y, int qp ){ int i; double bits = 0; for( i = 0; i <= y; i++ ) bits += h->fdec->i_row_bits[i]; for( i = y+1; i < h->sps->i_mb_height; i++ ) bits += predict_row_size( h, i, qp ); return bits;}void x264_ratecontrol_mb( x264_t *h, int bits ){ x264_ratecontrol_t *rc = h->rc; const int y = h->mb.i_mb_y; x264_cpu_restore( h->param.cpu ); h->fdec->i_row_bits[y] += bits; rc->qpa += rc->qpm; if( h->mb.i_mb_x != h->sps->i_mb_width - 1 || !h->mb.b_variable_qp ) return; h->fdec->i_row_qp[y] = rc->qpm; if( h->sh.i_type == SLICE_TYPE_B ) { /* B-frames shouldn't use lower QP than their reference frames */ if( y < h->sps->i_mb_height-1 ) { rc->qpm = X264_MAX( rc->qp, X264_MIN( h->fref0[0]->i_row_qp[y+1], h->fref1[0]->i_row_qp[y+1] )); } } else { update_predictor( rc->row_pred, qp2qscale(rc->qpm), h->fdec->i_row_satd[y], h->fdec->i_row_bits[y] ); /* tweak quality based on difference from predicted size */ if( y < h->sps->i_mb_height-1 && h->stat.i_slice_count[h->sh.i_type] > 0 ) { int prev_row_qp = h->fdec->i_row_qp[y]; int b0 = predict_row_size_sum( h, y, rc->qpm ); int b1 = b0; int i_qp_max = X264_MIN( prev_row_qp + h->param.rc.i_qp_step, h->param.rc.i_qp_max ); int i_qp_min = X264_MAX( prev_row_qp - h->param.rc.i_qp_step, h->param.rc.i_qp_min ); float buffer_left_planned = rc->buffer_fill - rc->frame_size_planned; if( !rc->b_vbv_min_rate ) i_qp_min = X264_MAX( i_qp_min, h->sh.i_qp ); while( rc->qpm < i_qp_max && (b1 > rc->frame_size_planned * 1.15 || (rc->buffer_fill - b1 < buffer_left_planned * 0.5))) { rc->qpm ++; b1 = predict_row_size_sum( h, y, rc->qpm ); } while( rc->qpm > i_qp_min && buffer_left_planned > rc->buffer_size * 0.4 && ((b1 < rc->frame_size_planned * 0.8 && rc->qpm <= prev_row_qp) || b1 < (rc->buffer_fill - rc->buffer_size + rc->buffer_rate) * 1.1) ) { rc->qpm --;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -