[FFmpeg-devel] [PATCH 2/2] lavf/vaapi_encode: fix to set the default max bitrate for AVC VBR

Qu, Pengfei pengfei.qu at intel.com
Tue Mar 13 04:42:36 EET 2018

-----Original Message-----
From: ffmpeg-devel [mailto:ffmpeg-devel-bounces at ffmpeg.org] On Behalf Of Mark Thompson
Sent: Monday, March 12, 2018 8:54 PM
To: ffmpeg-devel at ffmpeg.org
Subject: Re: [FFmpeg-devel] [PATCH 2/2] lavf/vaapi_encode: fix to set the default max bitrate for AVC VBR

On 12/03/18 05:38, Pengfei Qu wrote:
>     And for VBR mode, generally the max bit rate is bigger than the taraget
>     bitrate. For CBR mode, the max bitrate is same as the target bitrate.
>      when there is no specfic setting for the max bit rate parameter,
>     here the default value 95% is used to caculate the default max bitrate accordingly.
> Signed-off-by: Pengfei Qu <Pengfei.Qu at intel.com>
> ---
>  libavcodec/vaapi_encode.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c 
> index 78347d4..47110cf 100644
> --- a/libavcodec/vaapi_encode.c
> +++ b/libavcodec/vaapi_encode.c
> @@ -1164,8 +1164,8 @@ static av_cold int vaapi_encode_init_rate_control(AVCodecContext *avctx)
>      } else {
>          if (avctx->rc_max_rate < avctx->bit_rate) {
>              // Max rate is unset or invalid, just use the normal bitrate.
> -            rc_bits_per_second   = avctx->bit_rate;
> -            rc_target_percentage = 100;
> +            rc_target_percentage = 95;
> +            rc_bits_per_second   = (unsigned long)(avctx->bit_rate * 100.0 / rc_target_percentage);
>          } else {
>              rc_bits_per_second   = avctx->rc_max_rate;
>              rc_target_percentage = (unsigned long)(avctx->bit_rate * 
> 100) / rc_bits_per_second;

Can you give more detail about the motivation for this change?

In particular:
* You say you are fixing something - what is the issue?
* Where has the 95% number come from?
* Do you want this to apply to all codecs?  (The commit message says AVC.)


The current RC(CBR/VBR) mode in ffmpeg-vaapi depends on the target bitrate and max bitrate. CBR when target bitrate equal to max bitrate, or VBR. 

Here is the background to have this solution. One issue I saw when doing transcoding Mpeg2(interlace)->AVC on VBR mode, the real output bitrate go away too much compared to target bitrate when there is no max bitrate parameter in the command line. It work well when set the correct max bitrate parameter in the command line.

For this situation, the value rc_max_rate will be zero if there is not specified parameter for max bitrate in the command line. And then the execution fall into this condition" if (avctx->rc_max_rate < avctx->bit_rate) ", here the default max bitrate same as the target bitrate as the rc_target_percentage=100. The value "95" is also the experienced value and i give an initial max bitrate closer to the target bitrate and avoid bigger fluctuation in the default "max bitrate" mode. 

I think it is common rule to all codecs.

- Mark
ffmpeg-devel mailing list
ffmpeg-devel at ffmpeg.org

More information about the ffmpeg-devel mailing list