我是靠谱客的博主 彩色故事,最近开发中收集的这篇文章主要介绍X264码率控制:ABR模式,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

因为ABR模式在控制过程中会产生较大的码率波动,进而导致图像质量不稳定,同时在Http Adaptive Streaming中,也会导致视频segment大小不稳定,在接收端产生卡顿。所以ABR模式一般配合vbv使用,使用vbv buffer来限制码率的波动。

ABR模式的流程图如下:

ABR会在帧级、行级和宏块级去调整qscale(QP)。帧级和行级调整的目的主要有两个:1,总码率逼近目标码率;2,vbvbuffer不会overflow或者underflow,即码流大小稳定。宏块级的调整主要是Adaptive Quantization(AQ)和MB-tree,前者作用于空域,根据宏块的平滑程度调整qscale,后者作用于时域,根据宏块被后续帧参考的多少来决定其重要性,继而调整qscale。

和码率控制相关的函数可以参见

zhanghui_cuc x264源码分析与应用示例(二)——码率控制 http://blog.csdn.net/nonmarking/article/details/50737198

qscale的初始化和调整过程主要在 rate_estimate_qscale( )函数中完成,我们先从这个函数开始看起。

rate_estimate_qscale( )

  1. static float rate_estimate_qscale( x264_t *h )
  2. {
  3.     float q;
  4.     x264_ratecontrol_t *rcc = h->rc;
  5.     ratecontrol_entry_t rce = {0};
  6.     int pict_type = h->sh.i_type;
  7.     int64_t total_bits = 8*(h->stat.i_frame_size[SLICE_TYPE_I]       //total bits已编码比特数
  8.                           + h->stat.i_frame_size[SLICE_TYPE_P]
  9.                           + h->stat.i_frame_size[SLICE_TYPE_B])
  10.                        - rcc->filler_bits_sum;                      //编码完成帧的filler data大小(不计入视频大小)
  11.  
  12.     if( rcc->b_2pass )
  13.     {
  14.         rce = *rcc->rce;
  15.         if( pict_type != rce.pict_type )
  16.         {
  17.             x264_log( h, X264_LOG_ERROR, "slice=%c but 2pass stats say %cn",
  18.                       slice_type_to_char[pict_type], slice_type_to_char[rce.pict_type] );
  19.         }
  20.     }
  21.  
  22.     if( pict_type == SLICE_TYPE_B )
  23.     {
  24.         /* B-frames don"t have independent ratecontrol, but rather get the
  25.          * average QP of the two adjacent P-frames + an offset */
  26. //B帧的QP由两个相邻的P帧的QP计算而来
  27.         int i0 = IS_X264_TYPE_I(h->fref_nearest[0]->i_type);
  28.         int i1 = IS_X264_TYPE_I(h->fref_nearest[1]->i_type);
  29.         int dt0 = abs(h->fenc->i_poc - h->fref_nearest[0]->i_poc);
  30.         int dt1 = abs(h->fenc->i_poc - h->fref_nearest[1]->i_poc);
  31.         float q0 = h->fref_nearest[0]->f_qp_avg_rc;
  32.         float q1 = h->fref_nearest[1]->f_qp_avg_rc;
  33.  
  34.         if( h->fref_nearest[0]->i_type == X264_TYPE_BREF )
  35.             q0 -= rcc->pb_offset/2;
  36.         if( h->fref_nearest[1]->i_type == X264_TYPE_BREF )
  37.             q1 -= rcc->pb_offset/2;
  38.  
  39.         if( i0 && i1 )
  40.             q = (q0 + q1) / 2 + rcc->ip_offset;
  41.         else if( i0 )
  42.             q = q1;
  43.         else if( i1 )
  44.             q = q0;
  45.         else
  46.             q = (q0*dt1 + q1*dt0) / (dt0 + dt1);
  47.  
  48.         if( h->fenc->b_kept_as_ref )
  49.             q += rcc->pb_offset/2;
  50.         else
  51.             q += rcc->pb_offset;
  52.  
  53.         rcc->qp_novbv = q;
  54.         q = qp2qscale( q );
  55.         if( rcc->b_2pass )
  56.             rcc->frame_size_planned = qscale2bits( &rce, q );
  57.         else
  58.             rcc->frame_size_planned = predict_size( rcc->pred_b_from_p, q, h->fref[1][h->i_ref[1]-1]->i_satd );
  59.         /* Limit planned size by MinCR */
  60.         if( rcc->b_vbv )
  61.             rcc->frame_size_planned = X264_MIN( rcc->frame_size_planned, rcc->frame_size_maximum );
  62.         h->rc->frame_size_estimated = rcc->frame_size_planned;
  63.  
  64.         /* For row SATDs */
  65.         if( rcc->b_vbv )
  66.             rcc->last_satd = x264_rc_analyse_slice( h );   //根据mb-tree的结果重新计算frame SATD cost,计算每一行的SATD cost
  67.         return q;
  68.     }
  69.     else
  70.     {
  71.         double abr_buffer = 2 * rcc->rate_tolerance * rcc->bitrate;//abr缓存,用于控制实际码流大小和目标大小的差值
  72.         double predicted_bits = total_bits;    
  73.         if( h->i_thread_frames > 1 )            //预测当前线程中的编码帧大小
  74.         {
  75.             int j = h->rc - h->thread[0]->rc;
  76.             for( int i = 1; i < h->i_thread_frames; i++ )
  77.             {
  78.                 x264_t *t = h->thread[(j+i) % h->i_thread_frames];
  79.                 double bits = t->rc->frame_size_planned;
  80.                 if( !t->b_thread_active )
  81.                     continue;
  82.                 bits = X264_MAX(bits, t->rc->frame_size_estimated);
  83.                 predicted_bits += bits;      //已编码码流大小+线程中编码帧的大小
  84.             }
  85.         }
  86.  
  87.         if( rcc->b_2pass )                  //2pass部分暂时跳过
  88.         {
  89.             double lmin = rcc->lmin[pict_type];
  90.             double lmax = rcc->lmax[pict_type];
  91.             double diff;
  92.  
  93.             /* Adjust ABR buffer based on distance to the end of the video. */
  94.             if( rcc->num_entries > h->i_frame )
  95.             {
  96.                 double final_bits = rcc->entry_out[rcc->num_entries-1]->expected_bits;
  97.                 double video_pos = rce.expected_bits / final_bits;
  98.                 double scale_factor = sqrt( (1 - video_pos) * rcc->num_entries );
  99.                 abr_buffer *= 0.5 * X264_MAX( scale_factor, 0.5 );
  100.             }
  101.  
  102.             diff = predicted_bits - rce.expected_bits;
  103.             q = rce.new_qscale;
  104.             q /= x264_clip3f((abr_buffer - diff) / abr_buffer, .5, 2);
  105.             if( h->i_frame >= rcc->fps && rcc->expected_bits_sum >= 1 )
  106.             {
  107.                 /* Adjust quant based on the difference between
  108.                  * achieved and expected bitrate so far */
  109.                 double cur_time = (double)h->i_frame / rcc->num_entries;
  110.                 double w = x264_clip3f( cur_time*100, 0.0, 1.0 );
  111.                 q *= pow( (double)total_bits / rcc->expected_bits_sum, w );
  112.             }
  113.             rcc->qp_novbv = qscale2qp( q );
  114.             if( rcc->b_vbv )
  115.             {
  116.                 /* Do not overflow vbv */
  117.                 double expected_size = qscale2bits( &rce, q );
  118.                 double expected_vbv = rcc->buffer_fill + rcc->buffer_rate - expected_size;
  119.                 double expected_fullness = rce.expected_vbv / rcc->buffer_size;
  120.                 double qmax = q*(2 - expected_fullness);
  121.                 double size_constraint = 1 + expected_fullness;
  122.                 qmax = X264_MAX( qmax, rce.new_qscale );
  123.                 if( expected_fullness < .05 )
  124.                     qmax = lmax;
  125.                 qmax = X264_MIN(qmax, lmax);
  126.                 while( ((expected_vbv < rce.expected_vbv/size_constraint) && (q < qmax)) ||
  127.                         ((expected_vbv < 0) && (q < lmax)))
  128.                 {
  129.                     q *= 1.05;
  130.                     expected_size = qscale2bits(&rce, q);
  131.                     expected_vbv = rcc->buffer_fill + rcc->buffer_rate - expected_size;
  132.                 }
  133.                 rcc->last_satd = x264_rc_analyse_slice( h );
  134.             }
  135.             q = x264_clip3f( q, lmin, lmax );
  136.         }
  137.         else /* 1pass ABR */
  138.         {
  139.             /* Calculate the quantizer which would have produced the desired
  140.              * average bitrate if it had been applied to all frames so far.
  141.              * Then modulate that quant based on the current frame"s complexity
  142.              * relative to the average complexity so far (using the 2pass RCEQ).
  143.              * Then bias the quant up or down if total size so far was far from
  144.              * the target.
  145.              * Result: Depending on the value of rate_tolerance, there is a
  146.              * tradeoff between quality and bitrate precision. But at large
  147.              * tolerances, the bit distribution approaches that of 2pass. */
  148.             double wanted_bits, overflow = 1;
  149.  
  150.             rcc->last_satd = x264_rc_analyse_slice( h );  //根据mb-tree的结果重新计算frame SATD cost,计算每一行的SATD cost
  151.             rcc->short_term_cplxsum *= 0.5;
  152.             rcc->short_term_cplxcount *= 0.5;
  153.             rcc->short_term_cplxsum += rcc->last_satd / (CLIP_DURATION(h->fenc->f_duration) / BASE_FRAME_DURATION);
  154.             rcc->short_term_cplxcount ++;
  155.  
  156.             rce.tex_bits = rcc->last_satd;
  157.             rce.blurred_complexity = rcc->short_term_cplxsum / rcc->short_term_cplxcount;   //旧版x264所用的复杂度公式
  158.             rce.mv_bits = 0;
  159.             rce.p_count = rcc->nmb;
  160.             rce.i_count = 0;
  161.             rce.s_count = 0;
  162.             rce.qscale = 1;
  163.             rce.pict_type = pict_type;
  164.             rce.i_duration = h->fenc->i_duration;
  165.  
  166.             if( h->param.rc.i_rc_method == X264_RC_CRF )
  167.             {
  168.                 q = get_qscale( h, &rce, rcc->rate_factor_constant, h->fenc->i_frame );
  169.             }
  170.             else
  171.             {
  172.                 q = get_qscale( h, &rce, rcc->wanted_bits_window / rcc->cplxr_sum, h->fenc->i_frame );//计算每一帧的qscale初值并根据rate_factor调整
  173.  
  174.                 /* ABR code can potentially be counterproductive in CBR, so just don"t bother.
  175.                  * Don"t run it if the frame complexity is zero either. */
  176.                 if( !rcc->b_vbv_min_rate && rcc->last_satd )  //不开启vbv时会增加一次调整
  177.                 {
  178.                     // FIXME is it simpler to keep track of wanted_bits in ratecontrol_end?
  179.                     int i_frame_done = h->i_frame;
  180.                     double time_done = i_frame_done / rcc->fps;
  181.                     if( h->param.b_vfr_input && i_frame_done > 0 )
  182.                         time_done = ((double)(h->fenc->i_reordered_pts - h->i_reordered_pts_delay)) * h->param.i_timebase_num / h->param.i_timebase_den;
  183.                     wanted_bits = time_done * rcc->bitrate; 
  184.                     if( wanted_bits > 0 )
  185.                     {
  186.                         abr_buffer *= X264_MAX( 1, sqrt( time_done ) );  //abr_buffer:用来控制实际比特数跟目标比特数的差值
  187.                         overflow = x264_clip3f( 1.0 + (predicted_bits - wanted_bits) / abr_buffer, .5, 2 );
  188.                         q *= overflow;                                  //根据差值调整qscale
  189.                     }
  190.                 }
  191.             }
  192.  
  193.             if( pict_type == SLICE_TYPE_I && h->param.i_keyint_max > 1
  194.                 /* should test _next_ pict type, but that isn"t decided yet */
  195.                 && rcc->last_non_b_pict_type != SLICE_TYPE_I )
  196.             {
  197.                 q = qp2qscale( rcc->accum_p_qp / rcc->accum_p_norm );
  198.                 q /= fabs( h->param.rc.f_ip_factor );
  199.             }
  200.             else if( h->i_frame > 0 )
  201.             {
  202.                 if( h->param.rc.i_rc_method != X264_RC_CRF )
  203.                 {
  204.                     /* Asymmetric clipping, because symmetric would prevent
  205.                      * overflow control in areas of rapidly oscillating complexity */
  206.                     double lmin = rcc->last_qscale_for[pict_type] / rcc->lstep;
  207.                     double lmax = rcc->last_qscale_for[pict_type] * rcc->lstep;
  208.                     if( overflow > 1.1 && h->i_frame > 3 )
  209.                         lmax *= rcc->lstep;
  210.                     else if( overflow < 0.9 )
  211.                         lmin /= rcc->lstep;
  212.  
  213.                     q = x264_clip3f(q, lmin, lmax);
  214.                 }
  215.             }
  216.             else if( h->param.rc.i_rc_method == X264_RC_CRF && rcc->qcompress != 1 )
  217.             {
  218.                 q = qp2qscale( ABR_INIT_QP ) / fabs( h->param.rc.f_ip_factor );
  219.             }
  220.             rcc->qp_novbv = qscale2qp( q );
  221.  
  222.             //FIXME use get_diff_limited_q() ?
  223.             q = clip_qscale( h, pict_type, q );          //根据vbv buffer的状态调整qscale
  224.         }
  225.  
  226.         rcc->last_qscale_for[pict_type] =
  227.         rcc->last_qscale = q;
  228.  
  229.         if( !(rcc->b_2pass && !rcc->b_vbv) && h->fenc->i_frame == 0 )
  230.             rcc->last_qscale_for[SLICE_TYPE_P] = q * fabs( h->param.rc.f_ip_factor );
  231.  
  232.         if( rcc->b_2pass )
  233.             rcc->frame_size_planned = qscale2bits( &rce, q );
  234.         else
  235.             rcc->frame_size_planned = predict_size( &rcc->pred[h->sh.i_type], q, rcc->last_satd );
  236.  
  237.         /* Always use up the whole VBV in this case. */
  238.         if( rcc->single_frame_vbv )
  239.             rcc->frame_size_planned = rcc->buffer_rate;
  240.         /* Limit planned size by MinCR */
  241.         if( rcc->b_vbv )
  242.             rcc->frame_size_planned = X264_MIN( rcc->frame_size_planned, rcc->frame_size_maximum );
  243.         h->rc->frame_size_estimated = rcc->frame_size_planned;   //size-estimated在当前帧编码完后会更新成实际编码大小
  244.         return q;
  245.     }
  246. }

可以看到,ABR模式下,qscale的初值由get_qscale函数计算得出,然后在不开启vbv的情况下,根据(predicted_bits- wanted_bits)来调整qscale;在开启vbv的情况下,使用clip_qscale( ),根据vbv buffer的状态来调整qscale.

get_qscale( )

get_qscale( )函数用于计算qscale的初值,在MB-tree默认开启的情况下,不再使用模糊复杂度公式计算qscale,而是会根据fps计算出一个固定值,然后通过rate_factor进行调整。

q /=rate_factor;

rate_factor的更新是在x264_ratecontrol_end( ),公式如下

rate_factor=rcc->wanted_bits_window / rcc->cplxr_sum;

rc->cplxr_sum+= bits * qp2qscale( rc->qpa_rc ) / rc->last_rceq;

rc->wanted_bits_window+= h->fenc->f_duration * rc->bitrate;

bits是每一帧的实际比特数,rc->qpa_rc是最后帧级和行级调整完后的qp, rc->last_rceq上面求出的qscale初值。rc->wanted_bits_window是当前目标比特数。

由以上公式可以看到,rate_factor由两个比值决定:

目标比特数/实际比特数,比值越大→rate_factor越大→qscale和QP越小。

上一帧调整前qscale/上一帧调整后qscale,这个比值一般处于(0,1),比值越小说明上一帧调整幅度越大→rate_factor越小→qscale和QP越大。

  1. static double get_qscale(x264_t *h, ratecontrol_entry_t *rce, double rate_factor, int frame_num)
  2. {
  3.     x264_ratecontrol_t *rcc= h->rc;
  4.     x264_zone_t *zone = get_zone( h, frame_num );
  5.     double q;
  6.     if( h->param.rc.b_mb_tree )      //在新版本x264中mb-tree默认开启,所以不使用传统的复杂度公式来计算qscale,而是通过fps计算出一个固定值
  7.     {
  8.         double timescale = (double)h->sps->vui.i_num_units_in_tick / h->sps->vui.i_time_scale;
  9.         q = pow( BASE_FRAME_DURATION / CLIP_DURATION(rce->i_duration * timescale), 1 - h->param.rc.f_qcompress );    //fps越大 底数越大
  10. //BASE_FRAME_DURATION=0.04 即fps=25,rce->i_duration * timescale编码所用的1/fps
  11.     }
  12.     else
  13.         q = pow( rce->blurred_complexity, 1 - rcc->qcompress );
  14.  
  15.     // avoid NaN"s in the rc_eq
  16.  
  17.     if( !isfinite(q) || rce->tex_bits + rce->mv_bits == 0 )
  18.         q = rcc->last_qscale_for[rce->pict_type];
  19.     else
  20.     {
  21.         rcc->last_rceq = q;      
  22.         q /= rate_factor;     //根据rate_factor调整qp初值
  23.         rcc->last_qscale = q;
  24.     }
  25.  
  26.     if( zone )
  27.     {
  28.         if( zone->b_force_qp )
  29.             q = qp2qscale( zone->i_qp );
  30.         else
  31.             q /= zone->f_bitrate_factor;
  32.     }
  33.  
  34.     return q;
  35. }


clip_qscale( )

clip_qscale( )通过预测当前qscale下未来n帧的大小来估计vbv的状态,如果vbv buffer将overflow,则增大qscale;如果buffer将会underflow,则减小qscale。

  1. static double clip_qscale( x264_t *h, int pict_type, double q )
  2. {
  3.     x264_ratecontrol_t *rcc = h->rc;
  4.     double lmin = rcc->lmin[pict_type];
  5.     double lmax = rcc->lmax[pict_type];
  6.     if( rcc->rate_factor_max_increment )
  7.         lmax = X264_MIN( lmax, qp2qscale( rcc->qp_novbv + rcc->rate_factor_max_increment ) );
  8.     double q0 = q;
  9.  
  10.     /* B-frames are not directly subject to VBV,
  11.      * since they are controlled by the P-frames" QPs. */
  12.  
  13.     if( rcc->b_vbv && rcc->last_satd > 0 )
  14.     {
  15.         double fenc_cpb_duration = (double)h->fenc->i_cpb_duration *
  16.                                    h->sps->vui.i_num_units_in_tick / h->sps->vui.i_time_scale;
  17.         /* Lookahead VBV: raise the quantizer as necessary such that no frames in
  18.          * the lookahead overflow and such that the buffer is in a reasonable state
  19.          * by the end of the lookahead. */
  20.         if( h->param.rc.i_lookahead )
  21.         {
  22.             int terminate = 0;
  23.  
  24.             /* Avoid an infinite loop. */
  25.             for( int iterations = 0; iterations < 1000 && terminate != 3; iterations++ )
  26.             {
  27.                 double frame_q[3];
  28.                 double cur_bits = predict_size( &rcc->pred[h->sh.i_type], q, rcc->last_satd ); 
  29.                 double buffer_fill_cur = rcc->buffer_fill - cur_bits;
  30.                 double target_fill;
  31.                 double total_duration = 0;
  32.                 double last_duration = fenc_cpb_duration;
  33.                 frame_q[0] = h->sh.i_type == SLICE_TYPE_I ? q * h->param.rc.f_ip_factor : q;
  34.                 frame_q[1] = frame_q[0] * h->param.rc.f_pb_factor;
  35.                 frame_q[2] = frame_q[0] / h->param.rc.f_ip_factor;
  36.  
  37.                 /* Loop over the planned future frames. */
  38.                 for( int j = 0; buffer_fill_cur >= 0 && buffer_fill_cur <= rcc->buffer_size; j++ ) //向前预测n帧大小,一般情况下等于lookahead的数量
  39.                 {
  40.                     total_duration += last_duration;
  41.                     buffer_fill_cur += rcc->vbv_max_rate * last_duration;
  42.                     int i_type = h->fenc->i_planned_type[j];
  43.                     int i_satd = h->fenc->i_planned_satd[j];
  44.                     if( i_type == X264_TYPE_AUTO )     //一直预测到类型为AUTO的帧
  45.                         break;
  46.                     i_type = IS_X264_TYPE_I( i_type ) ? SLICE_TYPE_I : IS_X264_TYPE_B( i_type ) ? SLICE_TYPE_B : SLICE_TYPE_P;
  47.                     cur_bits = predict_size( &rcc->pred[i_type], frame_q[i_type], i_satd );    //使用线性模型预测未来帧大小
  48.                     buffer_fill_cur -= cur_bits;
  49.                     last_duration = h->fenc->f_planned_cpb_duration[j];
  50.                 }
  51.                 /* Try to get to get the buffer at least 50% filled, but don"t set an impossible goal. */
  52.                 target_fill = X264_MIN( rcc->buffer_fill + total_duration * rcc->vbv_max_rate * 0.5, rcc->buffer_size * 0.5 );
  53. //buffer_fill_cur模拟客户端buffer状态,在输入和输出帧率一定的情况下,如果buffer占用率低,说明当前输出的帧码率过大,需要增大qscale
  54.                 if( buffer_fill_cur < target_fill )  
  55.                 {
  56.                     q *= 1.01;
  57.                     terminate |= 1;
  58.                     continue;
  59.                 }
  60.                 /* Try to get the buffer no more than 80% filled, but don"t set an impossible goal. */
  61.                 target_fill = x264_clip3f( rcc->buffer_fill - total_duration * rcc->vbv_max_rate * 0.5, rcc->buffer_size * 0.8, rcc->buffer_size );
  62.                 if( rcc->b_vbv_min_rate && buffer_fill_cur > target_fill ) 
  63.                 {
  64.                     q /= 1.01;
  65.                     terminate |= 2;
  66.                     continue;
  67.                 }
  68.                 break;
  69.             }
  70.         }
  71.         /* Fallback to old purely-reactive algorithm: no lookahead. */
  72.         else
  73.         {
  74.             if( ( pict_type == SLICE_TYPE_P ||
  75.                 ( pict_type == SLICE_TYPE_I && rcc->last_non_b_pict_type == SLICE_TYPE_I ) ) &&
  76.                 rcc->buffer_fill/rcc->buffer_size < 0.5 )
  77.             {
  78.                 q /= x264_clip3f( 2.0*rcc->buffer_fill/rcc->buffer_size, 0.5, 1.0 );
  79.             }
  80.  
  81.             /* Now a hard threshold to make sure the frame fits in VBV.
  82.              * This one is mostly for I-frames. */
  83.             double bits = predict_size( &rcc->pred[h->sh.i_type], q, rcc->last_satd );
  84.             /* For small VBVs, allow the frame to use up the entire VBV. */
  85.             double max_fill_factor = h->param.rc.i_vbv_buffer_size >= 5*h->param.rc.i_vbv_max_bitrate / rcc->fps ? 2 : 1;
  86.             /* For single-frame VBVs, request that the frame use up the entire VBV. */
  87.             double min_fill_factor = rcc->single_frame_vbv ? 1 : 2;
  88.  
  89.             if( bits > rcc->buffer_fill/max_fill_factor )
  90.             {
  91.                 double qf = x264_clip3f( rcc->buffer_fill/(max_fill_factor*bits), 0.2, 1.0 );
  92.                 q /= qf;
  93.                 bits *= qf;
  94.             }
  95.             if( bits < rcc->buffer_rate/min_fill_factor )
  96.             {
  97.                 double qf = x264_clip3f( bits*min_fill_factor/rcc->buffer_rate, 0.001, 1.0 );
  98.                 q *= qf;
  99.            }
  100.             q = X264_MAX( q0, q );
  101.         }
  102.  
  103.         /* Check B-frame complexity, and use up any bits that would
  104.          * overflow before the next P-frame. */
  105.         if( h->sh.i_type == SLICE_TYPE_P && !rcc->single_frame_vbv )
  106.         {
  107.             int nb = rcc->bframes;
  108.             double bits = predict_size( &rcc->pred[h->sh.i_type], q, rcc->last_satd );
  109.             double pbbits = bits;
  110.             double bbits = predict_size( rcc->pred_b_from_p, q * h->param.rc.f_pb_factor, rcc->last_satd );
  111.             double space;
  112.             double bframe_cpb_duration = 0;
  113.             double minigop_cpb_duration;
  114.             for( int i = 0; i < nb; i++ )
  115.                 bframe_cpb_duration += h->fenc->f_planned_cpb_duration[i];
  116.  
  117.             if( bbits * nb > bframe_cpb_duration * rcc->vbv_max_rate )
  118.                 nb = 0;
  119.             pbbits += nb * bbits;
  120.  
  121.             minigop_cpb_duration = bframe_cpb_duration + fenc_cpb_duration;
  122.             space = rcc->buffer_fill + minigop_cpb_duration*rcc->vbv_max_rate - rcc->buffer_size;
  123.             if( pbbits < space )
  124.             {
  125.                 q *= X264_MAX( pbbits / space, bits / (0.5 * rcc->buffer_size) );
  126.             }
  127.             q = X264_MAX( q0/2, q );
  128.         }
  129.  
  130.         /* Apply MinCR and buffer fill restrictions */
  131.         double bits = predict_size( &rcc->pred[h->sh.i_type], q, rcc->last_satd );
  132.         double frame_size_maximum = X264_MIN( rcc->frame_size_maximum, X264_MAX( rcc->buffer_fill, 0.001 ) );
  133.         if( bits > frame_size_maximum )
  134.             q *= bits / frame_size_maximum;
  135.  
  136.         if( !rcc->b_vbv_min_rate )
  137.             q = X264_MAX( q0, q );
  138.     }
  139.  
  140.     if( lmin==lmax )
  141.         return lmin;
  142.     else if( rcc->b_2pass )
  143.     {
  144.         double min2 = log( lmin );
  145.         double max2 = log( lmax );
  146.         q = (log(q) - min2)/(max2-min2) - 0.5;
  147.         q = 1.0/(1.0 + exp( -4*q ));
  148.         q = q*(max2-min2) + min2;
  149.         return exp( q );
  150.     }
  151.     else
  152.         return x264_clip3f( q, lmin, lmax );
  153. }

在帧级调整完QP后,编码每个MB时,Adaptive Quantization和MB-tree会使用qp_offset对QP进行调整,这一步由函数x264_ratecontrol_mb_qp()实现。

AQ产生的qp_offset在函数x264_adaptive_quant_frame( )中,根据宏块的AC系数来调整qp_offset,相比于DC系数是图像的低频成分,反映了像素值的平均大小,AC系数是图像的高频成分,反映了图像的复杂程度。如果AC系数越低,则认为该宏块越平滑,其QP越小,如果AC系数越大,该宏块越复杂,则增大其QP。

MB-tree产生的offset在x264_macroblock_tree_finish( )中,如果一个宏块被后面的帧参考越多,则认为其重要性越高,则减小该宏块的QP,给该宏块分配更多码率,反之增大QP,MB-tree相关算法具体参见http://blog.csdn.net/zhoudegui88/article/details/73017216

每编码完一行后,会利用预测模型对当前帧大小进行预测,然后根据VBV buffer fill 对QP进行微调。这一步是在x264_ratecontrol_mb()中进行。

x264_ratecontrol_mb( )

  1. int x264_ratecontrol_mb( x264_t *h, int bits )
  2. {
  3.     x264_ratecontrol_t *rc = h->rc;
  4.     const int y = h->mb.i_mb_y;    //行数
  5.  
  6.     h->fdec->i_row_bits[y] += bits;
  7.     rc->qpa_aq += h->mb.i_qp;           //qpa_ap 自适应量化后的qp均值,即加上qpoffset后的qp值
  8.  
  9.     if( h->mb.i_mb_x != h->mb.i_mb_width - 1 )   //没到行尾,返回
  10.         return 0;
  11.  
  12.     x264_emms();
  13.     rc->qpa_rc += rc->qpm * h->mb.i_mb_width; //qpm  estimal_qsale的结果 自适应量化前的qp均值
  14.  
  15.     if( !rc->b_vbv )
  16.         return 0;
  17.  
  18.     float qscale = qp2qscale( rc->qpm );
  19.     h->fdec->f_row_qp[y] = rc->qpm;
  20.     h->fdec->f_row_qscale[y] = qscale;
  21.  
  22.     update_predictor( &rc->row_pred[0], qscale, h->fdec->i_row_satd[y], h->fdec->i_row_bits[y] ); //模型系数的更新
  23.     if( h->sh.i_type != SLICE_TYPE_I && rc->qpm < h->fref[0][0]->f_row_qp[y] )
  24.         update_predictor( &rc->row_pred[1], qscale, h->fdec->i_row_satds[0][0][y], h->fdec->i_row_bits[y] );
  25.  
  26.     /* update ratecontrol per-mbpair in MBAFF */
  27.     if( SLICE_MBAFF && !(y&1) )
  28.         return 0;
  29.  
  30.     /* FIXME: We don"t currently support the case where there"s a slice
  31.      * boundary in between. */
  32.     int can_reencode_row = h->sh.i_first_mb <= ((h->mb.i_mb_y - SLICE_MBAFF) * h->mb.i_mb_stride);
  33.  
  34.     /* tweak quality based on difference from predicted size */
  35.     float prev_row_qp = h->fdec->f_row_qp[y];
  36.     float qp_absolute_max = h->param.rc.i_qp_max;
  37.     if( rc->rate_factor_max_increment )
  38.         qp_absolute_max = X264_MIN( qp_absolute_max, rc->qp_novbv + rc->rate_factor_max_increment );
  39.     float qp_max = X264_MIN( prev_row_qp + h->param.rc.i_qp_step, qp_absolute_max );
  40.     float qp_min = X264_MAX( prev_row_qp - h->param.rc.i_qp_step, h->param.rc.i_qp_min );
  41.     float step_size = 0.5f;
  42.     float slice_size_planned = h->param.b_sliced_threads ? rc->slice_size_planned : rc->frame_size_planned;
  43.     float bits_so_far = row_bits_so_far( h, y );
  44.     float max_frame_error = x264_clip3f( 1.0 / h->mb.i_mb_height, 0.05, 0.25 );
  45.     float max_frame_size = rc->frame_size_maximum - rc->frame_size_maximum * max_frame_error;
  46.     max_frame_size = X264_MIN( max_frame_size, rc->buffer_fill - rc->buffer_rate * max_frame_error );
  47.     float size_of_other_slices = 0;
  48.     if( h->param.b_sliced_threads )
  49.     {
  50.         float size_of_other_slices_planned = 0;
  51.         for( int i = 0; i < h->param.i_threads; i++ )
  52.             if( h != h->thread[i] )
  53.             {
  54.                 size_of_other_slices += h->thread[i]->rc->frame_size_estimated;
  55.                 size_of_other_slices_planned += h->thread[i]->rc->slice_size_planned;
  56.             }
  57.         float weight = rc->slice_size_planned / rc->frame_size_planned;
  58.         size_of_other_slices = (size_of_other_slices - size_of_other_slices_planned) * weight + size_of_other_slices_planned;
  59.     }
  60.     if( y < h->i_threadslice_end-1 )        //如果不是当前帧的最后一行
  61.     {
  62.         /* B-frames shouldn"t use lower QP than their reference frames. */
  63.         if( h->sh.i_type == SLICE_TYPE_B )
  64.         {
  65.             qp_min = X264_MAX( qp_min, X264_MAX( h->fref[0][0]->f_row_qp[y+1], h->fref[1][0]->f_row_qp[y+1] ) );
  66.             rc->qpm = X264_MAX( rc->qpm, qp_min );
  67.         }
  68.  
  69.         float buffer_left_planned = rc->buffer_fill - rc->frame_size_planned;
  70.         buffer_left_planned = X264_MAX( buffer_left_planned, 0.f );
  71.         /* More threads means we have to be more cautious in letting ratecontrol use up extra bits. */
  72.         float rc_tol = buffer_left_planned / h->param.i_threads * rc->rate_tolerance;
  73.         float b1 = bits_so_far + predict_row_size_to_end( h, y, rc->qpm ) + size_of_other_slices;
  74.         float trust_coeff = x264_clip3f( bits_so_far / slice_size_planned, 0.0, 1.0 );
  75.  
  76.         /* Don"t increase the row QPs until a sufficent amount of the bits of the frame have been processed, in case a flat */
  77.         /* area at the top of the frame was measured inaccurately. */
  78.         if( trust_coeff < 0.05f )
  79.             qp_max = qp_absolute_max = prev_row_qp;
  80.  
  81.         if( h->sh.i_type != SLICE_TYPE_I )
  82.             rc_tol *= 0.5f;
  83.  
  84.         if( !rc->b_vbv_min_rate )
  85.             qp_min = X264_MAX( qp_min, rc->qp_novbv );
  86.  
  87.         while( rc->qpm <  qp_max                                       //根据与预测值的差距还有VBV buffer的状态调整qpm
  88.                && ((b1 > rc->frame_size_planned + rc_tol) ||                 
  89.                    (b1 > rc->frame_size_planned && rc->qpm < rc->qp_novbv) ||
  90.                    (b1 > rc->buffer_fill - buffer_left_planned * 0.5f)) )               //预测大小超出 增大qpm
  91.         {
  92.             rc->qpm += step_size;
  93.             b1 = bits_so_far + predict_row_size_to_end( h, y, rc->qpm ) + size_of_other_slices;
  94.         }
  95.  
  96.         float b_max = b1 + ((rc->buffer_fill - rc->buffer_size + rc7->buffer_rate) * 0.90f - b1) * trust_coeff;
  97.         rc->qpm -= step_size;
  98.         float b2 = bits_so_far + predict_row_size_to_end( h, y, rc->qpm ) + size_of_other_slices;
  99.         while( rc->qpm > qp_min  && rc->qpm < prev_row_qp
  100.                && (rc->qpm > h->fdec->f_row_qp[0] || rc->single_frame_vbv)
  101.                && (b2 < max_frame_size)
  102.                && ((b2 < rc->frame_size_planned * 0.8f) || (b2 < b_max)) )
  103.         {
  104.             b1 = b2;
  105.             rc->qpm -= step_size;
  106.             b2 = bits_so_far + predict_row_size_to_end( h, y, rc->qpm ) + size_of_other_slices;
  107.         }
  108.         rc->qpm += step_size;
  109.  
  110.         /* avoid VBV underflow or MinCR violation */
  111.         while( rc->qpm < qp_absolute_max && (b1 > max_frame_size) )
  112.         {
  113.             rc->qpm += step_size;
  114.             b1 = bits_so_far + predict_row_size_to_end( h, y, rc->qpm ) + size_of_other_slices;
  115.         }
  116.  
  117.         h->rc->frame_size_estimated = b1 - size_of_other_slices;
  118.  
  119. //
  120.  
  121.         /* If the current row was large enough to cause a large QP jump, try re-encoding it. */
  122.         if( rc->qpm > qp_max && prev_row_qp < qp_max && can_reencode_row )   //如果调整后QP超出范围,重编当前行
  123.         {
  124.             /* Bump QP to halfway in between... close enough. */
  125.             rc->qpm = x264_clip3f( (prev_row_qp + rc->qpm)*0.5f, prev_row_qp + 1.0f, qp_max );
  126.             rc->qpa_rc = rc->qpa_rc_prev;
  127.             rc->qpa_aq = rc->qpa_aq_prev;
  128.             h->fdec->i_row_bits[y] = 0;
  129.             h->fdec->i_row_bits[y-SLICE_MBAFF] = 0;
  130.             return -1;
  131.         }
  132.     }
  133.     else
  134.     {
  135.         h->rc->frame_size_estimated = bits_so_far;        //用来参与更新下一帧的vbv plan
  136.  
  137.         /* Last-ditch attempt: if the last row of the frame underflowed the VBV,
  138.          * try again. */
  139.         if( rc->qpm < qp_max && can_reencode_row          //如果overflow,重编最后一行
  140.             && (h->rc->frame_size_estimated + size_of_other_slices > X264_MIN( rc->frame_size_maximum, rc->buffer_fill )) )
  141.         {
  142.             rc->qpm = qp_max;
  143.             rc->qpa_rc = rc->qpa_rc_prev;
  144.             rc->qpa_aq = rc->qpa_aq_prev;
  145.             h->fdec->i_row_bits[y] = 0;
  146.             h->fdec->i_row_bits[y-SLICE_MBAFF] = 0;
  147.             return -1;
  148.         }
  149.     }
  150.  
  151.     rc->qpa_rc_prev = rc->qpa_rc;
  152.     rc->qpa_aq_prev = rc->qpa_aq;
  153.  
  154.     return 0;
  155. }

编完一帧后,会在x264_ratecontrol_end()计算该帧的一些数据 如各种宏块类型的数量、平均QP、写入stat file(2 pass),更新VBV buffer状态和上面提到的rate factor。

 

 

最后

以上就是彩色故事为你收集整理的X264码率控制:ABR模式的全部内容,希望文章能够帮你解决X264码率控制:ABR模式所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部