diff mbox series

[FFmpeg-devel,v1] lavc/vaapi_encode: Enable Macroblock based bitrate control

Message ID 20240207004045.3242406-1-fei.w.wang@intel.com
State New
Headers show
Series [FFmpeg-devel,v1] lavc/vaapi_encode: Enable Macroblock based bitrate control | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Wang, Fei W Feb. 7, 2024, 12:40 a.m. UTC
From: Fei Wang <fei.w.wang@intel.com>

Signed-off-by: Fei Wang <fei.w.wang@intel.com>
---
 doc/encoders.texi         |  3 +++
 libavcodec/vaapi_encode.c | 11 ++++++++++-
 libavcodec/vaapi_encode.h |  9 ++++++++-
 3 files changed, 21 insertions(+), 2 deletions(-)

Comments

Xiang, Haihao Feb. 23, 2024, 5:11 a.m. UTC | #1
On Wo, 2024-02-07 at 08:40 +0800, fei.w.wang-at-intel.com@ffmpeg.org wrote:
> From: Fei Wang <fei.w.wang@intel.com>
> 
> Signed-off-by: Fei Wang <fei.w.wang@intel.com>
> ---
>  doc/encoders.texi         |  3 +++
>  libavcodec/vaapi_encode.c | 11 ++++++++++-
>  libavcodec/vaapi_encode.h |  9 ++++++++-
>  3 files changed, 21 insertions(+), 2 deletions(-)
> 
> diff --git a/doc/encoders.texi b/doc/encoders.texi
> index c9fe6d6143..b66d5da31f 100644
> --- a/doc/encoders.texi
> +++ b/doc/encoders.texi
> @@ -4080,6 +4080,9 @@ Quality-defined variable-bitrate.
>  Average variable bitrate.
>  @end table
>  
> +@item mbbrc
> +Enable Macroblock based rate control. Invalid for CQP mode.
> +
>  @end table
>  
>  Each encoder also has its own specific options:
> diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
> index 86f4110cd2..83eb881428 100644
> --- a/libavcodec/vaapi_encode.c
> +++ b/libavcodec/vaapi_encode.c
> @@ -1806,6 +1806,9 @@ static av_cold int
> vaapi_encode_init_rate_control(AVCodecContext *avctx)
>          int i, first = 1, res;
>  
>          supported_va_rc_modes = rc_attr.value;
> +        if (ctx->mbbrc && !(supported_va_rc_modes & VA_RC_MB))
> +            ctx->mbbrc = 0;
> +
>          for (i = 0; i < FF_ARRAY_ELEMS(vaapi_encode_rc_modes); i++) {
>              rc_mode = &vaapi_encode_rc_modes[i];
>              if (supported_va_rc_modes & rc_mode->va_mode) {
> @@ -2017,13 +2020,18 @@ rc_mode_found:
>      ctx->va_bit_rate = rc_bits_per_second;
>  
>      av_log(avctx, AV_LOG_VERBOSE, "RC mode: %s.\n", rc_mode->name);
> +
> +    if (ctx->mbbrc && ctx->va_rc_mode == VA_RC_CQP)
> +        ctx->mbbrc = 0;
> +    av_log(avctx, AV_LOG_VERBOSE, "MB bitrate control: %d.\n", ctx->mbbrc);

Better to use a string representation of the boolean value, E.g. on/off, or
enabled/disabled.

Thanks
Haihao

> +
>      if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
>          // This driver does not want the RC mode attribute to be set.
>      } else {
>          ctx->config_attributes[ctx->nb_config_attributes++] =
>              (VAConfigAttrib) {
>              .type  = VAConfigAttribRateControl,
> -            .value = ctx->va_rc_mode,
> +            .value = ctx->mbbrc ? ctx->va_rc_mode | VA_RC_MB : ctx-
> >va_rc_mode,
>          };
>      }
>  
> @@ -2052,6 +2060,7 @@ rc_mode_found:
>  #if VA_CHECK_VERSION(1, 1, 0)
>              .ICQ_quality_factor = av_clip(rc_quality, 1, 51),
>              .max_qp             = (avctx->qmax > 0 ? avctx->qmax : 0),
> +            .rc_flags.bits.mb_rate_control = ctx->mbbrc ? 1 : 2,
>  #endif
>  #if VA_CHECK_VERSION(1, 3, 0)
>              .quality_factor     = rc_quality,
> diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h
> index d5d6d5eb1b..f2abfed459 100644
> --- a/libavcodec/vaapi_encode.h
> +++ b/libavcodec/vaapi_encode.h
> @@ -216,6 +216,9 @@ typedef struct VAAPIEncodeContext {
>      // available modes).
>      int             explicit_rc_mode;
>  
> +    // Macroblock based rate control.
> +    int             mbbrc;
> +
>      // Explicitly-set QP, for use with the "qp" options.
>      // (Forces CQP mode when set, overriding everything else.)
>      int             explicit_qp;
> @@ -538,7 +541,11 @@ int ff_vaapi_encode_close(AVCodecContext *avctx);
>      VAAPI_ENCODE_RC_MODE(VBR,  "Variable-bitrate"), \
>      VAAPI_ENCODE_RC_MODE(ICQ,  "Intelligent constant-quality"), \
>      VAAPI_ENCODE_RC_MODE(QVBR, "Quality-defined variable-bitrate"), \
> -    VAAPI_ENCODE_RC_MODE(AVBR, "Average variable-bitrate")
> +    VAAPI_ENCODE_RC_MODE(AVBR, "Average variable-bitrate"), \
> +    { "mbbrc", \
> +      "Macroblock based bitrate control",\
> +      OFFSET(common.mbbrc), AV_OPT_TYPE_BOOL, \
> +      { .i64 = 0 }, 0, 1, FLAGS }
>  
>  
>  #endif /* AVCODEC_VAAPI_ENCODE_H */
Wang, Fei W Feb. 23, 2024, 7:14 a.m. UTC | #2
On Fri, 2024-02-23 at 05:11 +0000, Xiang, Haihao wrote:
> On Wo, 2024-02-07 at 08:40 +0800, fei.w.wang-at-intel.com@ffmpeg.org
> wrote:
> > From: Fei Wang <fei.w.wang@intel.com>
> > 
> > Signed-off-by: Fei Wang <fei.w.wang@intel.com>
> > ---
> >  doc/encoders.texi         |  3 +++
> >  libavcodec/vaapi_encode.c | 11 ++++++++++-
> >  libavcodec/vaapi_encode.h |  9 ++++++++-
> >  3 files changed, 21 insertions(+), 2 deletions(-)
> > 
> > diff --git a/doc/encoders.texi b/doc/encoders.texi
> > index c9fe6d6143..b66d5da31f 100644
> > --- a/doc/encoders.texi
> > +++ b/doc/encoders.texi
> > @@ -4080,6 +4080,9 @@ Quality-defined variable-bitrate.
> >  Average variable bitrate.
> >  @end table
> >  
> > +@item mbbrc
> > +Enable Macroblock based rate control. Invalid for CQP mode.
> > +
> >  @end table
> >  
> >  Each encoder also has its own specific options:
> > diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
> > index 86f4110cd2..83eb881428 100644
> > --- a/libavcodec/vaapi_encode.c
> > +++ b/libavcodec/vaapi_encode.c
> > @@ -1806,6 +1806,9 @@ static av_cold int
> > vaapi_encode_init_rate_control(AVCodecContext *avctx)
> >          int i, first = 1, res;
> >  
> >          supported_va_rc_modes = rc_attr.value;
> > +        if (ctx->mbbrc && !(supported_va_rc_modes & VA_RC_MB))
> > +            ctx->mbbrc = 0;
> > +
> >          for (i = 0; i < FF_ARRAY_ELEMS(vaapi_encode_rc_modes);
> > i++) {
> >              rc_mode = &vaapi_encode_rc_modes[i];
> >              if (supported_va_rc_modes & rc_mode->va_mode) {
> > @@ -2017,13 +2020,18 @@ rc_mode_found:
> >      ctx->va_bit_rate = rc_bits_per_second;
> >  
> >      av_log(avctx, AV_LOG_VERBOSE, "RC mode: %s.\n", rc_mode-
> > >name);
> > +
> > +    if (ctx->mbbrc && ctx->va_rc_mode == VA_RC_CQP)
> > +        ctx->mbbrc = 0;
> > +    av_log(avctx, AV_LOG_VERBOSE, "MB bitrate control: %d.\n",
> > ctx->mbbrc);
> 
> Better to use a string representation of the boolean value, E.g.
> on/off, or
> enabled/disabled.

Fixed in V2.

Thanks
Fei

> 
> Thanks
> Haihao
> 
> > +
> >      if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
> >          // This driver does not want the RC mode attribute to be
> > set.
> >      } else {
> >          ctx->config_attributes[ctx->nb_config_attributes++] =
> >              (VAConfigAttrib) {
> >              .type  = VAConfigAttribRateControl,
> > -            .value = ctx->va_rc_mode,
> > +            .value = ctx->mbbrc ? ctx->va_rc_mode | VA_RC_MB :
> > ctx-
> > > va_rc_mode,
> >          };
> >      }
> >  
> > @@ -2052,6 +2060,7 @@ rc_mode_found:
> >  #if VA_CHECK_VERSION(1, 1, 0)
> >              .ICQ_quality_factor = av_clip(rc_quality, 1, 51),
> >              .max_qp             = (avctx->qmax > 0 ? avctx->qmax :
> > 0),
> > +            .rc_flags.bits.mb_rate_control = ctx->mbbrc ? 1 : 2,
> >  #endif
> >  #if VA_CHECK_VERSION(1, 3, 0)
> >              .quality_factor     = rc_quality,
> > diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h
> > index d5d6d5eb1b..f2abfed459 100644
> > --- a/libavcodec/vaapi_encode.h
> > +++ b/libavcodec/vaapi_encode.h
> > @@ -216,6 +216,9 @@ typedef struct VAAPIEncodeContext {
> >      // available modes).
> >      int             explicit_rc_mode;
> >  
> > +    // Macroblock based rate control.
> > +    int             mbbrc;
> > +
> >      // Explicitly-set QP, for use with the "qp" options.
> >      // (Forces CQP mode when set, overriding everything else.)
> >      int             explicit_qp;
> > @@ -538,7 +541,11 @@ int ff_vaapi_encode_close(AVCodecContext
> > *avctx);
> >      VAAPI_ENCODE_RC_MODE(VBR,  "Variable-bitrate"), \
> >      VAAPI_ENCODE_RC_MODE(ICQ,  "Intelligent constant-quality"), \
> >      VAAPI_ENCODE_RC_MODE(QVBR, "Quality-defined variable-
> > bitrate"), \
> > -    VAAPI_ENCODE_RC_MODE(AVBR, "Average variable-bitrate")
> > +    VAAPI_ENCODE_RC_MODE(AVBR, "Average variable-bitrate"), \
> > +    { "mbbrc", \
> > +      "Macroblock based bitrate control",\
> > +      OFFSET(common.mbbrc), AV_OPT_TYPE_BOOL, \
> > +      { .i64 = 0 }, 0, 1, FLAGS }
> >  
> >  
> >  #endif /* AVCODEC_VAAPI_ENCODE_H */
diff mbox series

Patch

diff --git a/doc/encoders.texi b/doc/encoders.texi
index c9fe6d6143..b66d5da31f 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -4080,6 +4080,9 @@  Quality-defined variable-bitrate.
 Average variable bitrate.
 @end table
 
+@item mbbrc
+Enable Macroblock based rate control. Invalid for CQP mode.
+
 @end table
 
 Each encoder also has its own specific options:
diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index 86f4110cd2..83eb881428 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -1806,6 +1806,9 @@  static av_cold int vaapi_encode_init_rate_control(AVCodecContext *avctx)
         int i, first = 1, res;
 
         supported_va_rc_modes = rc_attr.value;
+        if (ctx->mbbrc && !(supported_va_rc_modes & VA_RC_MB))
+            ctx->mbbrc = 0;
+
         for (i = 0; i < FF_ARRAY_ELEMS(vaapi_encode_rc_modes); i++) {
             rc_mode = &vaapi_encode_rc_modes[i];
             if (supported_va_rc_modes & rc_mode->va_mode) {
@@ -2017,13 +2020,18 @@  rc_mode_found:
     ctx->va_bit_rate = rc_bits_per_second;
 
     av_log(avctx, AV_LOG_VERBOSE, "RC mode: %s.\n", rc_mode->name);
+
+    if (ctx->mbbrc && ctx->va_rc_mode == VA_RC_CQP)
+        ctx->mbbrc = 0;
+    av_log(avctx, AV_LOG_VERBOSE, "MB bitrate control: %d.\n", ctx->mbbrc);
+
     if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
         // This driver does not want the RC mode attribute to be set.
     } else {
         ctx->config_attributes[ctx->nb_config_attributes++] =
             (VAConfigAttrib) {
             .type  = VAConfigAttribRateControl,
-            .value = ctx->va_rc_mode,
+            .value = ctx->mbbrc ? ctx->va_rc_mode | VA_RC_MB : ctx->va_rc_mode,
         };
     }
 
@@ -2052,6 +2060,7 @@  rc_mode_found:
 #if VA_CHECK_VERSION(1, 1, 0)
             .ICQ_quality_factor = av_clip(rc_quality, 1, 51),
             .max_qp             = (avctx->qmax > 0 ? avctx->qmax : 0),
+            .rc_flags.bits.mb_rate_control = ctx->mbbrc ? 1 : 2,
 #endif
 #if VA_CHECK_VERSION(1, 3, 0)
             .quality_factor     = rc_quality,
diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h
index d5d6d5eb1b..f2abfed459 100644
--- a/libavcodec/vaapi_encode.h
+++ b/libavcodec/vaapi_encode.h
@@ -216,6 +216,9 @@  typedef struct VAAPIEncodeContext {
     // available modes).
     int             explicit_rc_mode;
 
+    // Macroblock based rate control.
+    int             mbbrc;
+
     // Explicitly-set QP, for use with the "qp" options.
     // (Forces CQP mode when set, overriding everything else.)
     int             explicit_qp;
@@ -538,7 +541,11 @@  int ff_vaapi_encode_close(AVCodecContext *avctx);
     VAAPI_ENCODE_RC_MODE(VBR,  "Variable-bitrate"), \
     VAAPI_ENCODE_RC_MODE(ICQ,  "Intelligent constant-quality"), \
     VAAPI_ENCODE_RC_MODE(QVBR, "Quality-defined variable-bitrate"), \
-    VAAPI_ENCODE_RC_MODE(AVBR, "Average variable-bitrate")
+    VAAPI_ENCODE_RC_MODE(AVBR, "Average variable-bitrate"), \
+    { "mbbrc", \
+      "Macroblock based bitrate control",\
+      OFFSET(common.mbbrc), AV_OPT_TYPE_BOOL, \
+      { .i64 = 0 }, 0, 1, FLAGS }
 
 
 #endif /* AVCODEC_VAAPI_ENCODE_H */