Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize
@ 2022-03-22 14:11 Fei Wang
  2022-03-22 14:11 ` [FFmpeg-devel] [PATCH v4 2/2] doc/vaapi_encode: add documentations for max_frame_size Fei Wang
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Fei Wang @ 2022-03-22 14:11 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Fei Wang, Linjie Fu

From: Linjie Fu <linjie.fu@intel.com>

Add support for max frame size:
    - max_frame_size (bytes) to indicate the max allowed size for frame.

If the frame size exceeds the limitation, encoder will to control the frame
size by adjusting QP value.
    - MFS_NUM_PASSES to indicate number of passes for QP adjust.
    - MFS_DELTA_QP to indicate adjust qp value per pass.

To simplify the usage, default QP adjust is set to delta_qp[4] = {1, 1, 1, 1}.
Use new_qp for encoder if frame size exceeds the limitation:
    new_qp = base_qp + delta_qp[0] + delta_qp[1] + ...

ffmpeg -hwaccel vaapi -vaapi_device /dev/dri/renderD128 -f rawvideo \
        -v verbose -s:v 352x288 -i ./input.yuv -vf format=nv12,hwupload \
        -c:v h264_vaapi -profile:v main -g 30 -bf 3 -max_frame_size 40000 \
        -vframes 100 -y ./max_frame_size.h264

Max frame size was enabled since VA-API version (1, 3, 0), but query is available
since (1, 5, 0). It will be passed as a parameter in picParam and should be set
for each frame.

Signed-off-by: Linjie Fu <linjie.fu@intel.com>
Signed-off-by: Fei Wang <fei.w.wang@intel.com>
---
1. re-send the 2 legacy patch:
https://patchwork.ffmpeg.org/project/ffmpeg/patch/20190715105936.4860-1-linjie.fu@intel.com/
https://patchwork.ffmpeg.org/project/ffmpeg/patch/20190715110000.5069-1-linjie.fu@intel.com/

 libavcodec/vaapi_encode.c | 67 +++++++++++++++++++++++++++++++++++++++
 libavcodec/vaapi_encode.h | 19 +++++++++--
 2 files changed, 84 insertions(+), 2 deletions(-)

diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index ffd6cb1c25..b2782e6f9e 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -365,6 +365,17 @@ static int vaapi_encode_issue(AVCodecContext *avctx,
             goto fail;
     }
 
+#if VA_CHECK_VERSION(1, 5, 0)
+    if (ctx->max_frame_size) {
+        err = vaapi_encode_make_misc_param_buffer(avctx, pic,
+                                            VAEncMiscParameterTypeMultiPassFrameSize,
+                                            &ctx->mfs_params,
+                                            sizeof(ctx->mfs_params));
+        if (err < 0)
+            goto fail;
+    }
+#endif
+
     if (pic->type == PICTURE_TYPE_IDR) {
         if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
             ctx->codec->write_sequence_header) {
@@ -1869,6 +1880,54 @@ rc_mode_found:
     return 0;
 }
 
+static av_cold int vaapi_encode_init_max_frame_size(AVCodecContext *avctx)
+{
+#if VA_CHECK_VERSION(1, 5, 0)
+    VAAPIEncodeContext  *ctx = avctx->priv_data;
+    VAConfigAttrib      attr = { VAConfigAttribMaxFrameSize };
+    VAStatus vas;
+    int i;
+
+    vas = vaGetConfigAttributes(ctx->hwctx->display,
+                                    ctx->va_profile,
+                                    ctx->va_entrypoint,
+                                    &attr, 1);
+    if (vas != VA_STATUS_SUCCESS) {
+        ctx->max_frame_size = 0;
+        av_log(avctx, AV_LOG_ERROR, "Failed to query max frame size "
+               "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
+        return AVERROR_EXTERNAL;
+    }
+
+    if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
+        ctx->max_frame_size = 0;
+        av_log(avctx, AV_LOG_WARNING, "Max frame size attribute "
+               "is not supported.\n");
+    } else {
+        ctx->delta_qp = av_calloc(MFS_NUM_PASSES, sizeof(uint8_t));
+        if (!ctx->delta_qp) {
+            return AVERROR(ENOMEM);
+        }
+        for (i = 0; i <MFS_NUM_PASSES; i++)
+            ctx->delta_qp[i] = MFS_DELTA_QP;
+
+        ctx->mfs_params = (VAEncMiscParameterBufferMultiPassFrameSize){
+            .max_frame_size = ctx->max_frame_size,
+            .num_passes     = MFS_NUM_PASSES,
+            .delta_qp       = ctx->delta_qp,
+        };
+
+        av_log(avctx, AV_LOG_VERBOSE, "Max Frame Size: %d bytes.\n ",
+               ctx->max_frame_size);
+    }
+#else
+    av_log(avctx, AV_LOG_WARNING, "Max Frame Size is "
+                                    "not supported with this VA version.\n");
+#endif
+
+    return 0;
+}
+
 static av_cold int vaapi_encode_init_gop_structure(AVCodecContext *avctx)
 {
     VAAPIEncodeContext *ctx = avctx->priv_data;
@@ -2475,6 +2534,12 @@ av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
             goto fail;
     }
 
+    if (ctx->max_frame_size) {
+        err = vaapi_encode_init_max_frame_size(avctx);
+        if (err < 0)
+            goto fail;
+    }
+
     vas = vaCreateConfig(ctx->hwctx->display,
                          ctx->va_profile, ctx->va_entrypoint,
                          ctx->config_attributes, ctx->nb_config_attributes,
@@ -2618,6 +2683,8 @@ av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
 
     av_frame_free(&ctx->frame);
 
+    if (ctx->delta_qp)
+        av_freep(&ctx->delta_qp);
     av_freep(&ctx->codec_sequence_params);
     av_freep(&ctx->codec_picture_params);
     av_fifo_freep2(&ctx->encode_fifo);
diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h
index 1b40819c69..72f9256a8e 100644
--- a/libavcodec/vaapi_encode.h
+++ b/libavcodec/vaapi_encode.h
@@ -60,6 +60,11 @@ enum {
     PICTURE_TYPE_B   = 3,
 };
 
+enum {
+    MFS_NUM_PASSES = 4,
+    MFS_DELTA_QP   = 1,
+};
+
 typedef struct VAAPIEncodeSlice {
     int             index;
     int             row_start;
@@ -191,6 +196,9 @@ typedef struct VAAPIEncodeContext {
     // Desired B frame reference depth.
     int             desired_b_depth;
 
+    // Max Frame Size
+    int             max_frame_size;
+
     // Explicitly set RC mode (otherwise attempt to pick from
     // available modes).
     int             explicit_rc_mode;
@@ -271,7 +279,9 @@ typedef struct VAAPIEncodeContext {
 #if VA_CHECK_VERSION(0, 36, 0)
     VAEncMiscParameterBufferQualityLevel quality_params;
 #endif
-
+#if VA_CHECK_VERSION(1, 3, 0)
+    VAEncMiscParameterBufferMultiPassFrameSize mfs_params;
+#endif
     // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
     void           *codec_sequence_params;
 
@@ -331,6 +341,7 @@ typedef struct VAAPIEncodeContext {
     int idr_counter;
     int gop_counter;
     int end_of_stream;
+    uint8_t *delta_qp;
 
     // Whether the driver supports ROI at all.
     int             roi_allowed;
@@ -470,7 +481,11 @@ int ff_vaapi_encode_close(AVCodecContext *avctx);
       "Increase this to improve single channel performance. This option " \
       "doesn't work if driver doesn't implement vaSyncBuffer function.", \
       OFFSET(common.async_depth), AV_OPT_TYPE_INT, \
-      { .i64 = 2 }, 1, MAX_ASYNC_DEPTH, FLAGS }
+      { .i64 = 2 }, 1, MAX_ASYNC_DEPTH, FLAGS }, \
+    { "max_frame_size", \
+      "Maximum frame size (in bytes)",\
+      OFFSET(common.max_frame_size), AV_OPT_TYPE_INT, \
+      { .i64 = 0 }, 0, INT_MAX, FLAGS }
 
 #define VAAPI_ENCODE_RC_MODE(name, desc) \
     { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \
-- 
2.25.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [FFmpeg-devel] [PATCH v4 2/2] doc/vaapi_encode: add documentations for max_frame_size
  2022-03-22 14:11 [FFmpeg-devel] [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize Fei Wang
@ 2022-03-22 14:11 ` Fei Wang
  2022-03-23  8:51 ` [FFmpeg-devel] [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize Wang, Fei W
  2022-03-29 12:06 ` Xiang, Haihao
  2 siblings, 0 replies; 7+ messages in thread
From: Fei Wang @ 2022-03-22 14:11 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Fei Wang, Linjie Fu

From: Linjie Fu <linjie.fu@intel.com>

Add docs for max_frame_size option.

Signed-off-by: Linjie Fu <linjie.fu@intel.com>
Signed-off-by: Fei Wang <fei.w.wang@intel.com>
---
 doc/encoders.texi | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index 1bd38671ca..93fb535e98 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -3602,6 +3602,12 @@ performance. This option doesn't work if driver doesn't implement vaSyncBuffer
 function. Please make sure there are enough hw_frames allocated if a large
 number of async_depth is used.
 
+@item max_frame_size
+Set the allowed max size in bytes for each frame. If the frame size exceeds
+the limitation, encoder will adjust the QP value by adding delta_qp for each
+pass to control the frame size. To simplify the usage, delta_qp is set to
+default.
+
 @item rc_mode
 Set the rate control mode to use.  A given driver may only support a subset of
 modes.
-- 
2.25.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [FFmpeg-devel] [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize
  2022-03-22 14:11 [FFmpeg-devel] [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize Fei Wang
  2022-03-22 14:11 ` [FFmpeg-devel] [PATCH v4 2/2] doc/vaapi_encode: add documentations for max_frame_size Fei Wang
@ 2022-03-23  8:51 ` Wang, Fei W
  2022-03-26 15:20   ` Andriy Gelman
  2022-03-29 12:06 ` Xiang, Haihao
  2 siblings, 1 reply; 7+ messages in thread
From: Wang, Fei W @ 2022-03-23  8:51 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andriy Gelman

> -----Original Message-----
> From: Wang, Fei W <fei.w.wang@intel.com>
> Sent: Tuesday, March 22, 2022 10:11 PM
> To: ffmpeg-devel@ffmpeg.org
> Cc: Linjie Fu <linjie.fu@intel.com>; Wang, Fei W <fei.w.wang@intel.com>
> Subject: [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize
> 
> From: Linjie Fu <linjie.fu@intel.com>
> 
> Add support for max frame size:
>     - max_frame_size (bytes) to indicate the max allowed size for frame.
> 
> If the frame size exceeds the limitation, encoder will to control the frame size by
> adjusting QP value.
>     - MFS_NUM_PASSES to indicate number of passes for QP adjust.
>     - MFS_DELTA_QP to indicate adjust qp value per pass.
> 
> To simplify the usage, default QP adjust is set to delta_qp[4] = {1, 1, 1, 1}.
> Use new_qp for encoder if frame size exceeds the limitation:
>     new_qp = base_qp + delta_qp[0] + delta_qp[1] + ...
> 
> ffmpeg -hwaccel vaapi -vaapi_device /dev/dri/renderD128 -f rawvideo \
>         -v verbose -s:v 352x288 -i ./input.yuv -vf format=nv12,hwupload \
>         -c:v h264_vaapi -profile:v main -g 30 -bf 3 -max_frame_size 40000 \
>         -vframes 100 -y ./max_frame_size.h264
> 
> Max frame size was enabled since VA-API version (1, 3, 0), but query is available
> since (1, 5, 0). It will be passed as a parameter in picParam and should be set for
> each frame.
> 
> Signed-off-by: Linjie Fu <linjie.fu@intel.com>
> Signed-off-by: Fei Wang <fei.w.wang@intel.com>
> ---
> 1. re-send the 2 legacy patch:
> https://patchwork.ffmpeg.org/project/ffmpeg/patch/20190715105936.4860-1-
> linjie.fu@intel.com/
> https://patchwork.ffmpeg.org/project/ffmpeg/patch/20190715110000.5069-1-
> linjie.fu@intel.com/
> 
>  libavcodec/vaapi_encode.c | 67
> +++++++++++++++++++++++++++++++++++++++
>  libavcodec/vaapi_encode.h | 19 +++++++++--
>  2 files changed, 84 insertions(+), 2 deletions(-)

Hi Andriy,

Is there any way to know the details of failure for this patch? Like OS, configuration,
gcc version, etc. It looks good on my local Ubuntu with gcc 9.3, but show fails in patchwork checks:
https://patchwork.ffmpeg.org/project/ffmpeg/patch/20220322141119.595627-1-fei.w.wang@intel.com/

Fei
Thanks
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [FFmpeg-devel] [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize
  2022-03-23  8:51 ` [FFmpeg-devel] [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize Wang, Fei W
@ 2022-03-26 15:20   ` Andriy Gelman
  2022-03-27 10:16     ` Wang, Fei W
  0 siblings, 1 reply; 7+ messages in thread
From: Andriy Gelman @ 2022-03-26 15:20 UTC (permalink / raw)
  To: Wang, Fei W; +Cc: ffmpeg-devel

On Wed, 23. Mar 08:51, Wang, Fei W wrote:
> > -----Original Message-----
> > From: Wang, Fei W <fei.w.wang@intel.com>
> > Sent: Tuesday, March 22, 2022 10:11 PM
> > To: ffmpeg-devel@ffmpeg.org
> > Cc: Linjie Fu <linjie.fu@intel.com>; Wang, Fei W <fei.w.wang@intel.com>
> > Subject: [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize
> > 
> > From: Linjie Fu <linjie.fu@intel.com>
> > 
> > Add support for max frame size:
> >     - max_frame_size (bytes) to indicate the max allowed size for frame.
> > 
> > If the frame size exceeds the limitation, encoder will to control the frame size by
> > adjusting QP value.
> >     - MFS_NUM_PASSES to indicate number of passes for QP adjust.
> >     - MFS_DELTA_QP to indicate adjust qp value per pass.
> > 
> > To simplify the usage, default QP adjust is set to delta_qp[4] = {1, 1, 1, 1}.
> > Use new_qp for encoder if frame size exceeds the limitation:
> >     new_qp = base_qp + delta_qp[0] + delta_qp[1] + ...
> > 
> > ffmpeg -hwaccel vaapi -vaapi_device /dev/dri/renderD128 -f rawvideo \
> >         -v verbose -s:v 352x288 -i ./input.yuv -vf format=nv12,hwupload \
> >         -c:v h264_vaapi -profile:v main -g 30 -bf 3 -max_frame_size 40000 \
> >         -vframes 100 -y ./max_frame_size.h264
> > 
> > Max frame size was enabled since VA-API version (1, 3, 0), but query is available
> > since (1, 5, 0). It will be passed as a parameter in picParam and should be set for
> > each frame.
> > 
> > Signed-off-by: Linjie Fu <linjie.fu@intel.com>
> > Signed-off-by: Fei Wang <fei.w.wang@intel.com>
> > ---
> > 1. re-send the 2 legacy patch:
> > https://patchwork.ffmpeg.org/project/ffmpeg/patch/20190715105936.4860-1-
> > linjie.fu@intel.com/
> > https://patchwork.ffmpeg.org/project/ffmpeg/patch/20190715110000.5069-1-
> > linjie.fu@intel.com/
> > 
> >  libavcodec/vaapi_encode.c | 67
> > +++++++++++++++++++++++++++++++++++++++
> >  libavcodec/vaapi_encode.h | 19 +++++++++--
> >  2 files changed, 84 insertions(+), 2 deletions(-)

Hi,

> 
> Hi Andriy,
> 
> Is there any way to know the details of failure for this patch? Like OS, configuration,
> gcc version, etc. It looks good on my local Ubuntu with gcc 9.3, but show fails in patchwork checks:
> https://patchwork.ffmpeg.org/project/ffmpeg/patch/20220322141119.595627-1-fei.w.wang@intel.com/
> 

I don't think it was a real issue. I was upgrading the x86 runner at the time.

-- 
Andriy
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [FFmpeg-devel] [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize
  2022-03-26 15:20   ` Andriy Gelman
@ 2022-03-27 10:16     ` Wang, Fei W
  0 siblings, 0 replies; 7+ messages in thread
From: Wang, Fei W @ 2022-03-27 10:16 UTC (permalink / raw)
  To: Andriy Gelman; +Cc: ffmpeg-devel



> -----Original Message-----
> From: Andriy Gelman <andriy.gelman@gmail.com>
> Sent: Saturday, March 26, 2022 11:21 PM
> To: Wang, Fei W <fei.w.wang@intel.com>
> Cc: ffmpeg-devel@ffmpeg.org
> Subject: Re: [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize
> 
> On Wed, 23. Mar 08:51, Wang, Fei W wrote:
> > > -----Original Message-----
> > > From: Wang, Fei W <fei.w.wang@intel.com>
> > > Sent: Tuesday, March 22, 2022 10:11 PM
> > > To: ffmpeg-devel@ffmpeg.org
> > > Cc: Linjie Fu <linjie.fu@intel.com>; Wang, Fei W
> > > <fei.w.wang@intel.com>
> > > Subject: [PATCH v4 1/2] lavc/vaapi_encode: add support for
> > > maxframesize
> > >
> > > From: Linjie Fu <linjie.fu@intel.com>
> > >
> > > Add support for max frame size:
> > >     - max_frame_size (bytes) to indicate the max allowed size for frame.
> > >
> > > If the frame size exceeds the limitation, encoder will to control
> > > the frame size by adjusting QP value.
> > >     - MFS_NUM_PASSES to indicate number of passes for QP adjust.
> > >     - MFS_DELTA_QP to indicate adjust qp value per pass.
> > >
> > > To simplify the usage, default QP adjust is set to delta_qp[4] = {1, 1, 1, 1}.
> > > Use new_qp for encoder if frame size exceeds the limitation:
> > >     new_qp = base_qp + delta_qp[0] + delta_qp[1] + ...
> > >
> > > ffmpeg -hwaccel vaapi -vaapi_device /dev/dri/renderD128 -f rawvideo \
> > >         -v verbose -s:v 352x288 -i ./input.yuv -vf format=nv12,hwupload \
> > >         -c:v h264_vaapi -profile:v main -g 30 -bf 3 -max_frame_size 40000 \
> > >         -vframes 100 -y ./max_frame_size.h264
> > >
> > > Max frame size was enabled since VA-API version (1, 3, 0), but query
> > > is available since (1, 5, 0). It will be passed as a parameter in
> > > picParam and should be set for each frame.
> > >
> > > Signed-off-by: Linjie Fu <linjie.fu@intel.com>
> > > Signed-off-by: Fei Wang <fei.w.wang@intel.com>
> > > ---
> > > 1. re-send the 2 legacy patch:
> > > https://patchwork.ffmpeg.org/project/ffmpeg/patch/20190715105936.486
> > > 0-1-
> > > linjie.fu@intel.com/
> > > https://patchwork.ffmpeg.org/project/ffmpeg/patch/20190715110000.506
> > > 9-1-
> > > linjie.fu@intel.com/
> > >
> > >  libavcodec/vaapi_encode.c | 67
> > > +++++++++++++++++++++++++++++++++++++++
> > >  libavcodec/vaapi_encode.h | 19 +++++++++--
> > >  2 files changed, 84 insertions(+), 2 deletions(-)
> 
> Hi,
> 
> >
> > Hi Andriy,
> >
> > Is there any way to know the details of failure for this patch? Like
> > OS, configuration, gcc version, etc. It looks good on my local Ubuntu with gcc
> 9.3, but show fails in patchwork checks:
> > https://patchwork.ffmpeg.org/project/ffmpeg/patch/20220322141119.59562
> > 7-1-fei.w.wang@intel.com/
> >
> 
> I don't think it was a real issue. I was upgrading the x86 runner at the time.

Thanks for your confirmation. 

@Haihao, could you help to review this patchset? Thanks.

Thanks
Fei
> 
> --
> Andriy
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [FFmpeg-devel] [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize
  2022-03-22 14:11 [FFmpeg-devel] [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize Fei Wang
  2022-03-22 14:11 ` [FFmpeg-devel] [PATCH v4 2/2] doc/vaapi_encode: add documentations for max_frame_size Fei Wang
  2022-03-23  8:51 ` [FFmpeg-devel] [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize Wang, Fei W
@ 2022-03-29 12:06 ` Xiang, Haihao
  2022-03-30  8:27   ` Wang, Fei W
  2 siblings, 1 reply; 7+ messages in thread
From: Xiang, Haihao @ 2022-03-29 12:06 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Wang, Fei W, linjie.fu

On Tue, 2022-03-22 at 22:11 +0800, Fei Wang wrote:
> From: Linjie Fu <linjie.fu@intel.com>
> 
> Add support for max frame size:
>     - max_frame_size (bytes) to indicate the max allowed size for frame.
> 
> If the frame size exceeds the limitation, encoder will to control the frame
> size by adjusting QP value.
>     - MFS_NUM_PASSES to indicate number of passes for QP adjust.
>     - MFS_DELTA_QP to indicate adjust qp value per pass.
> 
> To simplify the usage, default QP adjust is set to delta_qp[4] = {1, 1, 1, 1}.
> Use new_qp for encoder if frame size exceeds the limitation:
>     new_qp = base_qp + delta_qp[0] + delta_qp[1] + ...
> 
> ffmpeg -hwaccel vaapi -vaapi_device /dev/dri/renderD128 -f rawvideo \
>         -v verbose -s:v 352x288 -i ./input.yuv -vf format=nv12,hwupload \
>         -c:v h264_vaapi -profile:v main -g 30 -bf 3 -max_frame_size 40000 \
>         -vframes 100 -y ./max_frame_size.h264
> 
> Max frame size was enabled since VA-API version (1, 3, 0), but query is
> available
> since (1, 5, 0). It will be passed as a parameter in picParam and should be
> set
> for each frame.
> 
> Signed-off-by: Linjie Fu <linjie.fu@intel.com>
> Signed-off-by: Fei Wang <fei.w.wang@intel.com>
> ---
> 1. re-send the 2 legacy patch:
> 
https://patchwork.ffmpeg.org/project/ffmpeg/patch/20190715105936.4860-1-linjie.fu@intel.com/
> 
https://patchwork.ffmpeg.org/project/ffmpeg/patch/20190715110000.5069-1-linjie.fu@intel.com/
> 
>  libavcodec/vaapi_encode.c | 67 +++++++++++++++++++++++++++++++++++++++
>  libavcodec/vaapi_encode.h | 19 +++++++++--
>  2 files changed, 84 insertions(+), 2 deletions(-)
> 
> diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
> index ffd6cb1c25..b2782e6f9e 100644
> --- a/libavcodec/vaapi_encode.c
> +++ b/libavcodec/vaapi_encode.c
> @@ -365,6 +365,17 @@ static int vaapi_encode_issue(AVCodecContext *avctx,
>              goto fail;
>      }
>  
> +#if VA_CHECK_VERSION(1, 5, 0)
> +    if (ctx->max_frame_size) {
> +        err = vaapi_encode_make_misc_param_buffer(avctx, pic,
> +                                            VAEncMiscParameterTypeMultiPassFr
> ameSize,
> +                                            &ctx->mfs_params,
> +                                            sizeof(ctx->mfs_params));
> +        if (err < 0)
> +            goto fail;
> +    }
> +#endif
> +
>      if (pic->type == PICTURE_TYPE_IDR) {
>          if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
>              ctx->codec->write_sequence_header) {
> @@ -1869,6 +1880,54 @@ rc_mode_found:
>      return 0;
>  }
>  
> +static av_cold int vaapi_encode_init_max_frame_size(AVCodecContext *avctx)
> +{
> +#if VA_CHECK_VERSION(1, 5, 0)
> +    VAAPIEncodeContext  *ctx = avctx->priv_data;
> +    VAConfigAttrib      attr = { VAConfigAttribMaxFrameSize };
> +    VAStatus vas;
> +    int i;
> +
> +    vas = vaGetConfigAttributes(ctx->hwctx->display,
> +                                    ctx->va_profile,
> +                                    ctx->va_entrypoint,
> +                                    &attr, 1);
> +    if (vas != VA_STATUS_SUCCESS) {
> +        ctx->max_frame_size = 0;
> +        av_log(avctx, AV_LOG_ERROR, "Failed to query max frame size "
> +               "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
> +        return AVERROR_EXTERNAL;
> +    }
> +
> +    if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
> +        ctx->max_frame_size = 0;
> +        av_log(avctx, AV_LOG_WARNING, "Max frame size attribute "
> +               "is not supported.\n");
> +    } else {
> +        ctx->delta_qp = av_calloc(MFS_NUM_PASSES, sizeof(uint8_t));
> +        if (!ctx->delta_qp) {
> +            return AVERROR(ENOMEM);
> +        }
> +        for (i = 0; i <MFS_NUM_PASSES; i++)
> +            ctx->delta_qp[i] = MFS_DELTA_QP;
> +
> +        ctx->mfs_params = (VAEncMiscParameterBufferMultiPassFrameSize){
> +            .max_frame_size = ctx->max_frame_size,
> +            .num_passes     = MFS_NUM_PASSES,
> +            .delta_qp       = ctx->delta_qp,
> +        };
> +
> +        av_log(avctx, AV_LOG_VERBOSE, "Max Frame Size: %d bytes.\n ",
> +               ctx->max_frame_size);
> +    }
> +#else
> +    av_log(avctx, AV_LOG_WARNING, "Max Frame Size is "
> +                                    "not supported with this VA version.\n");
> +#endif
> +
> +    return 0;
> +}
> +
>  static av_cold int vaapi_encode_init_gop_structure(AVCodecContext *avctx)
>  {
>      VAAPIEncodeContext *ctx = avctx->priv_data;
> @@ -2475,6 +2534,12 @@ av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
>              goto fail;
>      }
>  
> +    if (ctx->max_frame_size) {
> +        err = vaapi_encode_init_max_frame_size(avctx);

Is this feature supported when the given rc mode is CQP ? 

Thanks
Haihao

> +        if (err < 0)
> +            goto fail;
> +    }
> +
>      vas = vaCreateConfig(ctx->hwctx->display,
>                           ctx->va_profile, ctx->va_entrypoint,
>                           ctx->config_attributes, ctx->nb_config_attributes,
> @@ -2618,6 +2683,8 @@ av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
>  
>      av_frame_free(&ctx->frame);
>  
> +    if (ctx->delta_qp)
> +        av_freep(&ctx->delta_qp);
>      av_freep(&ctx->codec_sequence_params);
>      av_freep(&ctx->codec_picture_params);
>      av_fifo_freep2(&ctx->encode_fifo);
> diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h
> index 1b40819c69..72f9256a8e 100644
> --- a/libavcodec/vaapi_encode.h
> +++ b/libavcodec/vaapi_encode.h
> @@ -60,6 +60,11 @@ enum {
>      PICTURE_TYPE_B   = 3,
>  };
>  
> +enum {
> +    MFS_NUM_PASSES = 4,
> +    MFS_DELTA_QP   = 1,
> +};
> +
>  typedef struct VAAPIEncodeSlice {
>      int             index;
>      int             row_start;
> @@ -191,6 +196,9 @@ typedef struct VAAPIEncodeContext {
>      // Desired B frame reference depth.
>      int             desired_b_depth;
>  
> +    // Max Frame Size
> +    int             max_frame_size;
> +
>      // Explicitly set RC mode (otherwise attempt to pick from
>      // available modes).
>      int             explicit_rc_mode;
> @@ -271,7 +279,9 @@ typedef struct VAAPIEncodeContext {
>  #if VA_CHECK_VERSION(0, 36, 0)
>      VAEncMiscParameterBufferQualityLevel quality_params;
>  #endif
> -
> +#if VA_CHECK_VERSION(1, 3, 0)
> +    VAEncMiscParameterBufferMultiPassFrameSize mfs_params;
> +#endif
>      // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
>      void           *codec_sequence_params;
>  
> @@ -331,6 +341,7 @@ typedef struct VAAPIEncodeContext {
>      int idr_counter;
>      int gop_counter;
>      int end_of_stream;
> +    uint8_t *delta_qp;
>  
>      // Whether the driver supports ROI at all.
>      int             roi_allowed;
> @@ -470,7 +481,11 @@ int ff_vaapi_encode_close(AVCodecContext *avctx);
>        "Increase this to improve single channel performance. This option " \
>        "doesn't work if driver doesn't implement vaSyncBuffer function.", \
>        OFFSET(common.async_depth), AV_OPT_TYPE_INT, \
> -      { .i64 = 2 }, 1, MAX_ASYNC_DEPTH, FLAGS }
> +      { .i64 = 2 }, 1, MAX_ASYNC_DEPTH, FLAGS }, \
> +    { "max_frame_size", \
> +      "Maximum frame size (in bytes)",\
> +      OFFSET(common.max_frame_size), AV_OPT_TYPE_INT, \
> +      { .i64 = 0 }, 0, INT_MAX, FLAGS }
>  
>  #define VAAPI_ENCODE_RC_MODE(name, desc) \
>      { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [FFmpeg-devel] [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize
  2022-03-29 12:06 ` Xiang, Haihao
@ 2022-03-30  8:27   ` Wang, Fei W
  0 siblings, 0 replies; 7+ messages in thread
From: Wang, Fei W @ 2022-03-30  8:27 UTC (permalink / raw)
  To: Xiang, Haihao, ffmpeg-devel; +Cc: linjie.fu



> -----Original Message-----
> From: Xiang, Haihao <haihao.xiang@intel.com>
> Sent: Tuesday, March 29, 2022 8:07 PM
> To: ffmpeg-devel@ffmpeg.org
> Cc: Wang, Fei W <fei.w.wang@intel.com>; linjie.fu@intel.com
> Subject: Re: [FFmpeg-devel] [PATCH v4 1/2] lavc/vaapi_encode: add support for
> maxframesize
> 
> On Tue, 2022-03-22 at 22:11 +0800, Fei Wang wrote:
> > From: Linjie Fu <linjie.fu@intel.com>
> >
> > Add support for max frame size:
> >     - max_frame_size (bytes) to indicate the max allowed size for frame.
> >
> > If the frame size exceeds the limitation, encoder will to control the
> > frame size by adjusting QP value.
> >     - MFS_NUM_PASSES to indicate number of passes for QP adjust.
> >     - MFS_DELTA_QP to indicate adjust qp value per pass.
> >
> > To simplify the usage, default QP adjust is set to delta_qp[4] = {1, 1, 1, 1}.
> > Use new_qp for encoder if frame size exceeds the limitation:
> >     new_qp = base_qp + delta_qp[0] + delta_qp[1] + ...
> >
> > ffmpeg -hwaccel vaapi -vaapi_device /dev/dri/renderD128 -f rawvideo \
> >         -v verbose -s:v 352x288 -i ./input.yuv -vf format=nv12,hwupload \
> >         -c:v h264_vaapi -profile:v main -g 30 -bf 3 -max_frame_size 40000 \
> >         -vframes 100 -y ./max_frame_size.h264
> >
> > Max frame size was enabled since VA-API version (1, 3, 0), but query
> > is available since (1, 5, 0). It will be passed as a parameter in
> > picParam and should be set for each frame.
> >
> > Signed-off-by: Linjie Fu <linjie.fu@intel.com>
> > Signed-off-by: Fei Wang <fei.w.wang@intel.com>
> > ---
> > 1. re-send the 2 legacy patch:
> >
> https://patchwork.ffmpeg.org/project/ffmpeg/patch/20190715105936.4860-1-
> linjie.fu@intel.com/
> >
> https://patchwork.ffmpeg.org/project/ffmpeg/patch/20190715110000.5069-1-
> linjie.fu@intel.com/
> >
> >  libavcodec/vaapi_encode.c | 67
> > +++++++++++++++++++++++++++++++++++++++
> >  libavcodec/vaapi_encode.h | 19 +++++++++--
> >  2 files changed, 84 insertions(+), 2 deletions(-)
> >
> > diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
> > index ffd6cb1c25..b2782e6f9e 100644
> > --- a/libavcodec/vaapi_encode.c
> > +++ b/libavcodec/vaapi_encode.c
> > @@ -365,6 +365,17 @@ static int vaapi_encode_issue(AVCodecContext
> *avctx,
> >              goto fail;
> >      }
> >
> > +#if VA_CHECK_VERSION(1, 5, 0)
> > +    if (ctx->max_frame_size) {
> > +        err = vaapi_encode_make_misc_param_buffer(avctx, pic,
> > +
> > +VAEncMiscParameterTypeMultiPassFr
> > ameSize,
> > +                                            &ctx->mfs_params,
> > +                                            sizeof(ctx->mfs_params));
> > +        if (err < 0)
> > +            goto fail;
> > +    }
> > +#endif
> > +
> >      if (pic->type == PICTURE_TYPE_IDR) {
> >          if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
> >              ctx->codec->write_sequence_header) { @@ -1869,6 +1880,54
> > @@ rc_mode_found:
> >      return 0;
> >  }
> >
> > +static av_cold int vaapi_encode_init_max_frame_size(AVCodecContext
> > +*avctx) { #if VA_CHECK_VERSION(1, 5, 0)
> > +    VAAPIEncodeContext  *ctx = avctx->priv_data;
> > +    VAConfigAttrib      attr = { VAConfigAttribMaxFrameSize };
> > +    VAStatus vas;
> > +    int i;
> > +
> > +    vas = vaGetConfigAttributes(ctx->hwctx->display,
> > +                                    ctx->va_profile,
> > +                                    ctx->va_entrypoint,
> > +                                    &attr, 1);
> > +    if (vas != VA_STATUS_SUCCESS) {
> > +        ctx->max_frame_size = 0;
> > +        av_log(avctx, AV_LOG_ERROR, "Failed to query max frame size "
> > +               "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
> > +        return AVERROR_EXTERNAL;
> > +    }
> > +
> > +    if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
> > +        ctx->max_frame_size = 0;
> > +        av_log(avctx, AV_LOG_WARNING, "Max frame size attribute "
> > +               "is not supported.\n");
> > +    } else {
> > +        ctx->delta_qp = av_calloc(MFS_NUM_PASSES, sizeof(uint8_t));
> > +        if (!ctx->delta_qp) {
> > +            return AVERROR(ENOMEM);
> > +        }
> > +        for (i = 0; i <MFS_NUM_PASSES; i++)
> > +            ctx->delta_qp[i] = MFS_DELTA_QP;
> > +
> > +        ctx->mfs_params = (VAEncMiscParameterBufferMultiPassFrameSize){
> > +            .max_frame_size = ctx->max_frame_size,
> > +            .num_passes     = MFS_NUM_PASSES,
> > +            .delta_qp       = ctx->delta_qp,
> > +        };
> > +
> > +        av_log(avctx, AV_LOG_VERBOSE, "Max Frame Size: %d bytes.\n ",
> > +               ctx->max_frame_size);
> > +    }
> > +#else
> > +    av_log(avctx, AV_LOG_WARNING, "Max Frame Size is "
> > +                                    "not supported with this VA
> > +version.\n"); #endif
> > +
> > +    return 0;
> > +}
> > +
> >  static av_cold int vaapi_encode_init_gop_structure(AVCodecContext
> > *avctx)  {
> >      VAAPIEncodeContext *ctx = avctx->priv_data; @@ -2475,6 +2534,12
> > @@ av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
> >              goto fail;
> >      }
> >
> > +    if (ctx->max_frame_size) {
> > +        err = vaapi_encode_init_max_frame_size(avctx);
> 
> Is this feature supported when the given rc mode is CQP ?

Yes, it can support CQP mode.

Fei
> 
> Thanks
> Haihao
> 
> > +        if (err < 0)
> > +            goto fail;
> > +    }
> > +
> >      vas = vaCreateConfig(ctx->hwctx->display,
> >                           ctx->va_profile, ctx->va_entrypoint,
> >                           ctx->config_attributes,
> > ctx->nb_config_attributes, @@ -2618,6 +2683,8 @@ av_cold int
> > ff_vaapi_encode_close(AVCodecContext *avctx)
> >
> >      av_frame_free(&ctx->frame);
> >
> > +    if (ctx->delta_qp)
> > +        av_freep(&ctx->delta_qp);
> >      av_freep(&ctx->codec_sequence_params);
> >      av_freep(&ctx->codec_picture_params);
> >      av_fifo_freep2(&ctx->encode_fifo);
> > diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h
> > index 1b40819c69..72f9256a8e 100644
> > --- a/libavcodec/vaapi_encode.h
> > +++ b/libavcodec/vaapi_encode.h
> > @@ -60,6 +60,11 @@ enum {
> >      PICTURE_TYPE_B   = 3,
> >  };
> >
> > +enum {
> > +    MFS_NUM_PASSES = 4,
> > +    MFS_DELTA_QP   = 1,
> > +};
> > +
> >  typedef struct VAAPIEncodeSlice {
> >      int             index;
> >      int             row_start;
> > @@ -191,6 +196,9 @@ typedef struct VAAPIEncodeContext {
> >      // Desired B frame reference depth.
> >      int             desired_b_depth;
> >
> > +    // Max Frame Size
> > +    int             max_frame_size;
> > +
> >      // Explicitly set RC mode (otherwise attempt to pick from
> >      // available modes).
> >      int             explicit_rc_mode;
> > @@ -271,7 +279,9 @@ typedef struct VAAPIEncodeContext {  #if
> > VA_CHECK_VERSION(0, 36, 0)
> >      VAEncMiscParameterBufferQualityLevel quality_params;  #endif
> > -
> > +#if VA_CHECK_VERSION(1, 3, 0)
> > +    VAEncMiscParameterBufferMultiPassFrameSize mfs_params; #endif
> >      // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
> >      void           *codec_sequence_params;
> >
> > @@ -331,6 +341,7 @@ typedef struct VAAPIEncodeContext {
> >      int idr_counter;
> >      int gop_counter;
> >      int end_of_stream;
> > +    uint8_t *delta_qp;
> >
> >      // Whether the driver supports ROI at all.
> >      int             roi_allowed;
> > @@ -470,7 +481,11 @@ int ff_vaapi_encode_close(AVCodecContext *avctx);
> >        "Increase this to improve single channel performance. This option " \
> >        "doesn't work if driver doesn't implement vaSyncBuffer function.", \
> >        OFFSET(common.async_depth), AV_OPT_TYPE_INT, \
> > -      { .i64 = 2 }, 1, MAX_ASYNC_DEPTH, FLAGS }
> > +      { .i64 = 2 }, 1, MAX_ASYNC_DEPTH, FLAGS }, \
> > +    { "max_frame_size", \
> > +      "Maximum frame size (in bytes)",\
> > +      OFFSET(common.max_frame_size), AV_OPT_TYPE_INT, \
> > +      { .i64 = 0 }, 0, INT_MAX, FLAGS }
> >
> >  #define VAAPI_ENCODE_RC_MODE(name, desc) \
> >      { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name },
> > \
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-03-30  8:27 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-22 14:11 [FFmpeg-devel] [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize Fei Wang
2022-03-22 14:11 ` [FFmpeg-devel] [PATCH v4 2/2] doc/vaapi_encode: add documentations for max_frame_size Fei Wang
2022-03-23  8:51 ` [FFmpeg-devel] [PATCH v4 1/2] lavc/vaapi_encode: add support for maxframesize Wang, Fei W
2022-03-26 15:20   ` Andriy Gelman
2022-03-27 10:16     ` Wang, Fei W
2022-03-29 12:06 ` Xiang, Haihao
2022-03-30  8:27   ` Wang, Fei W

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git