[x264-devel] reconfigure bitrate in ABR is by design or not
Zhenan Lin
zhenanlin at gmail.com
Mon Jan 23 11:27:12 CET 2017
Hi, dear experts,
We find that bitrate can be reconfigured in ABR. However, there are some
codes and comments showing that users can ONLY reconfigure bitrate in CBR:
x264_encoder_reconfig_apply @ encoder.c:
/* Supported reconfiguration options (1-pass only):
* vbv-maxrate
* vbv-bufsize
* crf
* bitrate (CBR only) */
if( !ret && rc_reconfig )
x264_ratecontrol_init_reconfigurable( h, 0 );
x264_ratecontrol_init_reconfigurable @ ratecontrol.c:
/* We don't support changing the ABR bitrate right now,
so if the stream starts as CBR, keep it CBR. */
if( rc->b_vbv_min_rate )
h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate;
..
if( rc->b_vbv_min_rate )
rc->bitrate = (double)h->param.rc.i_bitrate * kilobit_size;
That means that we can ONLY reconfigure bitrate only when b_vbv_min_rate=1,
and it was set when we initialized the encoder (b_init = 1) with CBR
(i_vbv_max_bitrate<=i_bitrate)
x264_validate_parameters @ encoder.c:
else if( h->param.rc.i_vbv_max_bitrate < h->param.rc.i_bitrate &&
h->param.rc.i_rc_method == X264_RC_ABR )
{
x264_log( h, X264_LOG_WARNING, "max bitrate less than average
bitrate, assuming CBR\n" );
h->param.rc.i_bitrate = h->param.rc.i_vbv_max_bitrate;
x264_ratecontrol_init_reconfigurable @ ratecontrol.c:
if( b_init )
{
..
rc->b_vbv_min_rate = !rc->b_2pass
&& h->param.rc.i_rc_method == X264_RC_ABR
&& h->param.rc.i_vbv_max_bitrate <=
h->param.rc.i_bitrate;
}
However, if we initialize the encoder with ABR (i.e. --bitrate 499
--vbv-maxrate 500 --vbv-bufsize 500),
then we call x264_encoder_reconfig to reconfigure bitrate (i.e
param.rc.i_bitrate = 100, param.rc.i_vbv_max_bitrate = 100, param.rc.
i_vbv_buffer_size = 500),
the new values in param.rc will be copy to h->reconfig_h->param.rc DIRECTLY
in x264_encoder_try_reconfig:
int x264_encoder_reconfig( x264_t *h, x264_param_t *param )
{
h = h->thread[h->thread[0]->i_thread_phase];
x264_param_t param_save = h->reconfig_h->param;
h->reconfig_h->param = h->param;
int rc_reconfig;
int ret = x264_encoder_try_reconfig( h->reconfig_h, param, &rc_reconfig
);
if( !ret )
h->reconfig = 1;
else
h->reconfig_h->param = param_save;
return ret;
}
x264_encoder_try_reconfig at encoder.c:
/* VBV can't be turned on if it wasn't on to begin with */
if( h->param.rc.i_vbv_max_bitrate > 0 && h->param.rc.i_vbv_buffer_size >
0 &&
param->rc.i_vbv_max_bitrate > 0 && param->rc.i_vbv_buffer_size >
0 )
{
*rc_reconfig |= h->param.rc.i_vbv_max_bitrate !=
param->rc.i_vbv_max_bitrate;
*rc_reconfig |= h->param.rc.i_vbv_buffer_size !=
param->rc.i_vbv_buffer_size;
*rc_reconfig |= h->param.rc.i_bitrate != param->rc.i_bitrate;
COPY( rc.i_vbv_max_bitrate );
COPY( rc.i_vbv_buffer_size );
COPY( rc.i_bitrate );
}
After that, h->reconfig=1, and x264_encoder_reconfig_apply will be called in
x264_encoder_encode,
Then, the new values in h->reconfig_h->param.rc will be copy to h->param.rc
DIRECTLY in x264_encoder_try_reconfig.
As a result, the checking "if the stream starts as CBR, keep it CBR"in
x264_ratecontrol_init_reconfigurable will not take effects because new
values have been set into h->param.
X264_encoder_encode at encoder.c
if( h->reconfig )
{
x264_encoder_reconfig_apply( h, &h->reconfig_h->param );
h->reconfig = 0;
}
int x264_encoder_reconfig_apply( x264_t *h, x264_param_t *param )
{
int rc_reconfig;
int ret = x264_encoder_try_reconfig( h, param, &rc_reconfig );
mbcmp_init( h );
if( !ret )
x264_sps_init_reconfigurable( h->sps, &h->param );
/* Supported reconfiguration options (1-pass only):
* vbv-maxrate
* vbv-bufsize
* crf
* bitrate (CBR only) */
if( !ret && rc_reconfig )
x264_ratecontrol_init_reconfigurable( h, 0 );
return ret;
}
Therefore, we can change the bitrate in ABR indeed.
I wonder whether reconfigure bitrate in ABR is by design (the comments and
checking codes are legacy) or not (this reconfiguration behavior is
unexpected) ?
Thanks,
Zhenan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/x264-devel/attachments/20170123/72e44afb/attachment.html>
More information about the x264-devel
mailing list