/* Fill "default" values */
-static void x264_slice_header_init( x264_slice_header_t *sh, x264_param_t *param,
+static void x264_slice_header_init( x264_t *h, x264_slice_header_t *sh,
x264_sps_t *sps, x264_pps_t *pps,
- int i_type, int i_idr_pic_id, int i_frame )
+ int i_type, int i_idr_pic_id, int i_frame, int i_qp )
{
+ x264_param_t *param = &h->param;
+
/* First we fill all field */
sh->sps = sps;
sh->pps = pps;
sh->i_cabac_init_idc = param->i_cabac_init_idc;
- sh->i_qp_delta = 0;
+ sh->i_qp_delta = i_qp - pps->i_pic_init_qp;
sh->b_sp_for_swidth = 0;
sh->i_qs_delta = 0;
- if( param->b_deblocking_filter )
+ /* If effective qp <= 15, deblocking would have no effect anyway */
+ if( param->b_deblocking_filter
+ && ( h->mb.b_variable_qp
+ || 15 < i_qp + X264_MAX(param->i_deblocking_filter_alphac0, param->i_deblocking_filter_beta) ) )
{
sh->i_disable_deblocking_filter_idc = 0;
}
int i;
/* apply deblocking filter to the current decoded picture */
- if( h->param.b_deblocking_filter )
+ if( !h->sh.i_disable_deblocking_filter_idc )
{
TIMER_START( i_mtime_filter );
x264_frame_deblocking_filter( h, h->sh.i_type );
/* ------------------------ Create slice header ----------------------- */
if( i_nal_type == NAL_SLICE_IDR )
{
- x264_slice_header_init( &h->sh, &h->param, h->sps, h->pps, i_slice_type, h->i_idr_pic_id, h->i_frame_num - 1 );
+ x264_slice_header_init( h, &h->sh, h->sps, h->pps, i_slice_type, h->i_idr_pic_id, h->i_frame_num - 1, i_global_qp );
/* increment id */
h->i_idr_pic_id = ( h->i_idr_pic_id + 1 ) % 65536;
}
else
{
- x264_slice_header_init( &h->sh, &h->param, h->sps, h->pps, i_slice_type, -1, h->i_frame_num - 1 );
+ x264_slice_header_init( h, &h->sh, h->sps, h->pps, i_slice_type, -1, h->i_frame_num - 1, i_global_qp );
/* always set the real higher num of ref frame used */
h->sh.b_num_ref_idx_override = 1;
/* Nothing to do ? */
}
- /* global qp */
- h->sh.i_qp_delta = i_global_qp - h->pps->i_pic_init_qp;
-
/* get adapative cabac model if needed */
if( h->param.b_cabac )
{
h->stat.frame.i_mb_count[h->mb.i_type]++;
- x264_ratecontrol_mb(h, bs_pos(&h->out.bs) - mb_spos);
+ if( h->mb.b_variable_qp )
+ x264_ratecontrol_mb(h, bs_pos(&h->out.bs) - mb_spos);
}
if( h->param.b_cabac )
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 );
+ /* Currently there is no adaptive quant, and per-MB ratecontrol is used only in CBR. */
+ h->mb.b_variable_qp = h->param.rc.b_cbr && !h->param.rc.b_stat_read;
+
/* Init 1pass CBR algo */
if( h->param.rc.b_cbr ){
rc->buffer_size = h->param.rc.i_rc_buffer_size * 1000;
int dqp;
int i;
- if( !h->param.rc.b_cbr || h->param.rc.b_stat_read )
- return;
-
x264_cpu_restore( h->param.cpu );
rc->qps += rc->qpm;
}
/* weighted average of cplx of past frames */
weight = 1.0;
- for(j=0; j<cplxblur*2 && j<=i; j++){
+ for(j=0; j<=cplxblur*2 && j<=i; j++){
ratecontrol_entry_t *rcj = &rcc->entry[i-j];
weight_sum += weight;
cplx_sum += weight * qscale2bits(rcj, 1);