[FFmpeg-devel] [PATCH] hw_base_encode: refactor picture allocation/freeing

Tong Wu wutong1208 at outlook.com
Mon Aug 19 18:07:44 EEST 2024


Lynne:
>Subject: [PATCH] hw_base_encode: refactor picture allocation/freeing
>
>This commit cleans up and refactors the mess of private state upon private state
>that used to be.
>
>Now, FFHWBaseEncodePicture is fully initialized upon call-time, and, most
>importantly, this lets APIs which require initialization data for frames
>(VkImageViews) to initialize this for both the input image, and the reconstruction
>(DPB) image.
>---
> libavcodec/d3d12va_encode.c     | 24 +++++-------
> libavcodec/hw_base_encode.c     | 33 ++++++++++-------
> libavcodec/hw_base_encode.h     | 21 ++++++-----
> libavcodec/vaapi_encode.c       | 66 ++++++++++++++-------------------
> libavcodec/vaapi_encode.h       |  4 +-
> libavcodec/vaapi_encode_av1.c   | 20 +++++-----
> libavcodec/vaapi_encode_h264.c  | 42 ++++++++++-----------
>libavcodec/vaapi_encode_h265.c  | 20 +++++-----
>libavcodec/vaapi_encode_mjpeg.c |  7 ++--  libavcodec/vaapi_encode_mpeg2.c |
>7 ++--
> libavcodec/vaapi_encode_vp8.c   |  4 +-
> libavcodec/vaapi_encode_vp9.c   | 14 +++----
> 12 files changed, 128 insertions(+), 134 deletions(-)
>
>diff --git a/libavcodec/d3d12va_encode.c b/libavcodec/d3d12va_encode.c index
>9ee9da41e3..681044c5c8 100644
>--- a/libavcodec/d3d12va_encode.c
>+++ b/libavcodec/d3d12va_encode.c
>@@ -186,7 +186,7 @@ static int
>d3d12va_encode_create_metadata_buffers(AVCodecContext *avctx,  }
>
> static int d3d12va_encode_issue(AVCodecContext *avctx,
>-                                const FFHWBaseEncodePicture *base_pic)
>+                                FFHWBaseEncodePicture *base_pic)
> {
>     FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
>     D3D12VAEncodeContext       *ctx = avctx->priv_data;
>@@ -560,27 +560,20 @@ static int
>d3d12va_encode_free_rc_params(AVCodecContext *avctx)
>     return 0;
> }
>
>-static FFHWBaseEncodePicture *d3d12va_encode_alloc(AVCodecContext *avctx,
>-                                                   const AVFrame *frame)
>+static int d3d12va_encode_init(AVCodecContext *avctx,
>+FFHWBaseEncodePicture *base)

The function name might be confusing since ff_d3d12va_encode_init is already  there.

> {
>     D3D12VAEncodeContext *ctx = avctx->priv_data;
>-    D3D12VAEncodePicture *pic;
>-
>-    pic = av_mallocz(sizeof(*pic));
>-    if (!pic)
>-        return NULL;
>+    D3D12VAEncodePicture *pic = base->priv;

base->priv is never initialized

>
>     if (ctx->codec->picture_priv_data_size > 0) {
>-        pic->base.priv_data = av_mallocz(ctx->codec->picture_priv_data_size);
>-        if (!pic->base.priv_data) {
>-            av_freep(&pic);
>-            return NULL;
>-        }
>+        pic->base.codec_priv = av_mallocz(ctx->codec->picture_priv_data_size);
>+        if (!pic->base.codec_priv)
>+            return AVERROR(ENOMEM);
>     }
>
>     pic->input_surface = (AVD3D12VAFrame *)frame->data[0];
>
>-    return &pic->base;
>+    return 0;
> }
>
> static int d3d12va_encode_free(AVCodecContext *avctx, @@ -680,7 +673,7 @@
>end:
> }
>
> static int d3d12va_encode_output(AVCodecContext *avctx,
>-                                 const FFHWBaseEncodePicture *base_pic, AVPacket *pkt)
>+                                 FFHWBaseEncodePicture *base_pic,
>+ AVPacket *pkt)
> {

Mark was suggesting to add the const to the callback. Any reason to remove it?

>     FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
>     D3D12VAEncodePicture *pic = (D3D12VAEncodePicture *)base_pic; @@ -
>1389,6 +1382,7 @@ static int
>d3d12va_encode_create_recon_frames(AVCodecContext *avctx)  }
>
> static const FFHWEncodePictureOperation d3d12va_type = {
>+    .priv_size = sizeof(D3D12VAEncodePicture),
>     .alloc  = &d3d12va_encode_alloc,

.alloc no longer exists

>
>     .issue  = &d3d12va_encode_issue,
>diff --git a/libavcodec/hw_base_encode.c b/libavcodec/hw_base_encode.c index
>c1346e866c..60d862f1b6 100644
>--- a/libavcodec/hw_base_encode.c
>+++ b/libavcodec/hw_base_encode.c
>@@ -27,6 +27,19 @@
> #include "avcodec.h"
> #include "hw_base_encode.h"
>
>+static int base_encode_pic_free(FFHWBaseEncodePicture *pic) {
>+    av_frame_free(&pic->input_image);
>+    av_frame_free(&pic->recon_image);
>+
>+    av_buffer_unref(&pic->opaque_ref);
>+    av_freep(&pic->codec_priv);
>+    av_freep(&pic->priv);
>+    av_free(pic);
>+
>+    return 0;
>+}
>+
> static void hw_base_encode_add_ref(FFHWBaseEncodePicture *pic,
>                                    FFHWBaseEncodePicture *target,
>                                    int is_ref, int in_dpb, int prev) @@ -370,6 +383,7 @@ static
>int hw_base_encode_clear_old(AVCodecContext *avctx,
>FFHWBaseEncodeContext
>             else
>                 ctx->pic_start = next;
>             ctx->op->free(avctx, pic);
>+            base_encode_pic_free(pic);
>         } else {
>             prev = pic;
>         }
>@@ -416,7 +430,7 @@ static int hw_base_encode_send_frame(AVCodecContext
>*avctx, FFHWBaseEncodeContex
>         if (err < 0)
>             return err;
>
>-        pic = ctx->op->alloc(avctx, frame);
>+        pic = av_mallocz(sizeof(*pic));
>         if (!pic)
>             return AVERROR(ENOMEM);
>
>@@ -467,6 +481,9 @@ static int hw_base_encode_send_frame(AVCodecContext
>*avctx, FFHWBaseEncodeContex
>             ctx->pic_end       = pic;
>         }
>
>+        err = ctx->op->init(avctx, pic);
>+        if (err < 0)
>+            goto fail;
>     } else {
>         ctx->end_of_stream = 1;
>
>@@ -480,6 +497,7 @@ static int hw_base_encode_send_frame(AVCodecContext
>*avctx, FFHWBaseEncodeContex
>
> fail:
>     ctx->op->free(avctx, pic);
>+    base_encode_pic_free(pic);
>     return err;
> }
>
>@@ -529,7 +547,7 @@ int
>ff_hw_base_encode_receive_packet(FFHWBaseEncodeContext *ctx,
>     AVFrame *frame = ctx->frame;
>     int err;
>
>-    av_assert0(ctx->op && ctx->op->alloc && ctx->op->issue &&
>+    av_assert0(ctx->op && ctx->op->init && ctx->op->issue &&
>                ctx->op->output && ctx->op->free);
>
> start:
>@@ -737,17 +755,6 @@ fail:
>     return err;
> }
>
>-int ff_hw_base_encode_free(FFHWBaseEncodePicture *pic) -{
>-    av_frame_free(&pic->input_image);
>-    av_frame_free(&pic->recon_image);
>-
>-    av_buffer_unref(&pic->opaque_ref);
>-    av_freep(&pic->priv_data);
>-
>-    return 0;
>-}

There's still somewhere in d3d12va_encode that uses this function.

>-
> int ff_hw_base_encode_init(AVCodecContext *avctx, FFHWBaseEncodeContext
>*ctx)  {
>     ctx->log_ctx = (void *)avctx;
>diff --git a/libavcodec/hw_base_encode.h b/libavcodec/hw_base_encode.h index
>ac0cc7b80c..d6ec5a611a 100644
>--- a/libavcodec/hw_base_encode.h
>+++ b/libavcodec/hw_base_encode.h
>@@ -59,6 +59,11 @@ enum {
> };
>
> typedef struct FFHWBaseEncodePicture {
>+    /* API-specific private data */
>+    void *priv;
>+    /* Codec-specific private data */
>+    void *codec_priv;
>+
>     struct FFHWBaseEncodePicture *next;
>
>     int64_t         display_order;
>@@ -78,8 +83,6 @@ typedef struct FFHWBaseEncodePicture {
>     AVFrame        *input_image;
>     AVFrame        *recon_image;
>
>-    void           *priv_data;
>-
>     // Whether this picture is a reference picture.
>     int             is_reference;
>
>@@ -104,13 +107,15 @@ typedef struct FFHWBaseEncodePicture {  }
>FFHWBaseEncodePicture;
>
> typedef struct FFHWEncodePictureOperation {
>-    // Alloc memory for the picture structure and initialize the API-specific internals
>-    // based of the given frame.
>-    FFHWBaseEncodePicture * (*alloc)(AVCodecContext *avctx, const AVFrame
>*frame);
>+    /* Size of API-specific internal picture data */
>+    size_t priv_size;
>+    /* Initialize API-specific internals */
>+    int (*init)(AVCodecContext *avctx, FFHWBaseEncodePicture *pic);
>+
>     // Issue the picture structure, which will send the frame surface to HW Encode
>API.
>-    int (*issue)(AVCodecContext *avctx, const FFHWBaseEncodePicture
>*base_pic);
>+    int (*issue)(AVCodecContext *avctx, FFHWBaseEncodePicture
>+ *base_pic);
>     // Get the output AVPacket.
>-    int (*output)(AVCodecContext *avctx, const FFHWBaseEncodePicture
>*base_pic, AVPacket *pkt);
>+    int (*output)(AVCodecContext *avctx, FFHWBaseEncodePicture
>+ *base_pic, AVPacket *pkt);
>     // Free the picture structure.
>     int (*free)(AVCodecContext *avctx, FFHWBaseEncodePicture *base_pic);  }
>FFHWEncodePictureOperation; @@ -228,8 +233,6 @@ int
>ff_hw_base_init_gop_structure(FFHWBaseEncodeContext *ctx, AVCodecContext
>*av  int ff_hw_base_get_recon_format(FFHWBaseEncodeContext *ctx, const void
>*hwconfig,
>                                 enum AVPixelFormat *fmt);
>
>-int ff_hw_base_encode_free(FFHWBaseEncodePicture *pic);
>-
> int ff_hw_base_encode_init(AVCodecContext *avctx, FFHWBaseEncodeContext
>*ctx);
>
> int ff_hw_base_encode_close(FFHWBaseEncodeContext *ctx); diff --git
>a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c index
>afdf3fe3d8..97a967b1d4 100644
>--- a/libavcodec/vaapi_encode.c
>+++ b/libavcodec/vaapi_encode.c
>@@ -267,7 +267,7 @@ static int vaapi_encode_make_tile_slice(AVCodecContext
>*avctx,  }
>
> static int vaapi_encode_issue(AVCodecContext *avctx,
>-                              const FFHWBaseEncodePicture *base_pic)
>+                              FFHWBaseEncodePicture *base_pic)
> {
>     FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
>     VAAPIEncodeContext         *ctx = avctx->priv_data;
>@@ -364,7 +364,7 @@ static int vaapi_encode_issue(AVCodecContext *avctx,
>     }
>
>     if (ctx->codec->init_picture_params) {
>-        err = ctx->codec->init_picture_params(avctx, pic);
>+        err = ctx->codec->init_picture_params(avctx, base_pic);
>         if (err < 0) {
>             av_log(avctx, AV_LOG_ERROR, "Failed to initialise picture "
>                    "parameters: %d.\n", err); @@ -493,7 +493,7 @@ static int
>vaapi_encode_issue(AVCodecContext *avctx,
>         }
>
>         if (ctx->codec->init_slice_params) {
>-            err = ctx->codec->init_slice_params(avctx, pic, slice);
>+            err = ctx->codec->init_slice_params(avctx, base_pic,
>+ slice);
>             if (err < 0) {
>                 av_log(avctx, AV_LOG_ERROR, "Failed to initialise slice "
>                        "parameters: %d.\n", err); @@ -773,7 +773,7 @@ end:
> }
>
> static int vaapi_encode_output(AVCodecContext *avctx,
>-                               const FFHWBaseEncodePicture *base_pic, AVPacket *pkt)
>+                               FFHWBaseEncodePicture *base_pic,
>+ AVPacket *pkt)
> {
>     FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
>     VAAPIEncodeContext         *ctx = avctx->priv_data;
>@@ -839,56 +839,45 @@ static int vaapi_encode_discard(AVCodecContext
>*avctx,
>     return 0;
> }
>
>-static FFHWBaseEncodePicture *vaapi_encode_alloc(AVCodecContext *avctx,
>-                                                 const AVFrame *frame)
>+static int vaapi_encode_init(AVCodecContext *avctx,
>+FFHWBaseEncodePicture *pic)
> {
>     VAAPIEncodeContext *ctx = avctx->priv_data;
>-    VAAPIEncodePicture *pic;
>-
>-    pic = av_mallocz(sizeof(*pic));
>-    if (!pic)
>-        return NULL;
>+    VAAPIEncodePicture *priv = pic->priv;
>+    AVFrame *frame = pic->input_image;
>
>     if (ctx->codec->picture_priv_data_size > 0) {
>-        pic->base.priv_data = av_mallocz(ctx->codec->picture_priv_data_size);
>-        if (!pic->base.priv_data) {
>-            av_freep(&pic);
>-            return NULL;
>-        }
>+        pic->codec_priv = av_mallocz(ctx->codec->picture_priv_data_size);
>+        if (!pic->codec_priv)
>+            return AVERROR(ENOMEM);
>     }
>
>-    pic->input_surface = (VASurfaceID)(uintptr_t)frame->data[3];
>-    pic->recon_surface = VA_INVALID_ID;
>-    pic->output_buffer = VA_INVALID_ID;
>+    priv->input_surface = (VASurfaceID)(uintptr_t)frame->data[3];
>+    priv->recon_surface = VA_INVALID_ID;
>+    priv->output_buffer = VA_INVALID_ID;
>
>-    return &pic->base;
>+    return 0;
> }
>
>-static int vaapi_encode_free(AVCodecContext *avctx,
>-                             FFHWBaseEncodePicture *base_pic)
>+static int vaapi_encode_free(AVCodecContext *avctx,
>+FFHWBaseEncodePicture *pic)
> {
>-    VAAPIEncodePicture *pic = (VAAPIEncodePicture*)base_pic;
>+    VAAPIEncodePicture *priv = pic->priv;
>     int i;
>
>-    if (base_pic->encode_issued)
>-        vaapi_encode_discard(avctx, pic);
>+    if (pic->encode_issued)
>+        vaapi_encode_discard(avctx, priv);
>
>-    if (pic->slices) {
>-        for (i = 0; i < pic->nb_slices; i++)
>-            av_freep(&pic->slices[i].codec_slice_params);
>+    if (priv->slices) {
>+        for (i = 0; i < priv->nb_slices; i++)
>+            av_freep(&priv->slices[i].codec_slice_params);
>     }
>
>-    ff_hw_base_encode_free(base_pic);
>-
>-    av_freep(&pic->param_buffers);
>-    av_freep(&pic->slices);
>+    av_freep(&priv->param_buffers);
>+    av_freep(&priv->slices);
>     // Output buffer should already be destroyed.
>-    av_assert0(pic->output_buffer == VA_INVALID_ID);
>-
>-    av_freep(&pic->codec_picture_params);
>-    av_freep(&pic->roi);
>+    av_assert0(priv->output_buffer == VA_INVALID_ID);
>
>-    av_free(pic);
>+    av_freep(&priv->codec_picture_params);
>+    av_freep(&priv->roi);
>
>     return 0;
> }
>@@ -2090,7 +2079,8 @@ static av_cold int
>vaapi_encode_create_recon_frames(AVCodecContext *avctx)  }
>
> static const FFHWEncodePictureOperation vaapi_op = {
>-    .alloc  = &vaapi_encode_alloc,
>+    .priv_size = sizeof(VAAPIEncodePicture),
>+    .init = &vaapi_encode_init,
>
>     .issue  = &vaapi_encode_issue,
>
>diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h index
>d76cdb8662..78e02799c2 100644
>--- a/libavcodec/vaapi_encode.h
>+++ b/libavcodec/vaapi_encode.h
>@@ -301,9 +301,9 @@ typedef struct VAAPIEncodeType {
>     // Fill the parameter structures.
>     int  (*init_sequence_params)(AVCodecContext *avctx);
>     int   (*init_picture_params)(AVCodecContext *avctx,
>-                                 VAAPIEncodePicture *pic);
>+                                 FFHWBaseEncodePicture *pic);
>     int     (*init_slice_params)(AVCodecContext *avctx,
>-                                 VAAPIEncodePicture *pic,
>+                                 FFHWBaseEncodePicture *pice,
>                                  VAAPIEncodeSlice *slice);
>
>     // The type used by the packed header: this should look like diff --git
>a/libavcodec/vaapi_encode_av1.c b/libavcodec/vaapi_encode_av1.c index
>26a5707bf5..d3ffde3949 100644
>--- a/libavcodec/vaapi_encode_av1.c
>+++ b/libavcodec/vaapi_encode_av1.c
>@@ -466,12 +466,12 @@ end:
> }
>
> static int vaapi_encode_av1_init_picture_params(AVCodecContext *avctx,
>-                                                VAAPIEncodePicture *vaapi_pic)
>+                                                FFHWBaseEncodePicture
>+ *pic)
> {
>     VAAPIEncodeContext              *ctx = avctx->priv_data;
>     VAAPIEncodeAV1Context          *priv = avctx->priv_data;
>-    const FFHWBaseEncodePicture     *pic = &vaapi_pic->base;
>-    VAAPIEncodeAV1Picture          *hpic = pic->priv_data;
>+    VAAPIEncodePicture        *vaapi_pic = pic->priv;
>+    VAAPIEncodeAV1Picture          *hpic = pic->codec_priv;
>     AV1RawOBU                    *fh_obu = &priv->fh;
>     AV1RawFrameHeader                *fh = &fh_obu->obu.frame.header;
>     VAEncPictureParameterBufferAV1 *vpic = vaapi_pic->codec_picture_params;
>@@ -503,7 +503,7 @@ static int
>vaapi_encode_av1_init_picture_params(AVCodecContext *avctx,
>         fh->frame_type = AV1_FRAME_INTER;
>         fh->base_q_idx = priv->q_idx_p;
>         ref = pic->refs[0][pic->nb_refs[0] - 1];
>-        href = ref->priv_data;
>+        href = ref->codec_priv;
>         hpic->slot = !href->slot;
>         hpic->last_idr_frame = href->last_idr_frame;
>         fh->refresh_frame_flags = 1 << hpic->slot; @@ -519,7 +519,7 @@ static int
>vaapi_encode_av1_init_picture_params(AVCodecContext *avctx,
>         /** set the 2nd nearest frame in L0 as Golden frame. */
>         if (pic->nb_refs[0] > 1) {
>             ref = pic->refs[0][pic->nb_refs[0] - 2];
>-            href = ref->priv_data;
>+            href = ref->codec_priv;
>             fh->ref_frame_idx[3] = href->slot;
>             fh->ref_order_hint[href->slot] = ref->display_order - href->last_idr_frame;
>             vpic->ref_frame_ctrl_l0.fields.search_idx1 = AV1_REF_FRAME_GOLDEN;
>@@ -540,7 +540,7 @@ static int
>vaapi_encode_av1_init_picture_params(AVCodecContext *avctx,
>         vpic->ref_frame_ctrl_l1.fields.search_idx0 = AV1_REF_FRAME_BWDREF;
>
>         ref                            = pic->refs[0][pic->nb_refs[0] - 1];
>-        href                           = ref->priv_data;
>+        href                           = ref->codec_priv;
>         hpic->last_idr_frame           = href->last_idr_frame;
>         fh->primary_ref_frame          = href->slot;
>         fh->ref_order_hint[href->slot] = ref->display_order - href->last_idr_frame;
>@@ -549,7 +549,7 @@ static int
>vaapi_encode_av1_init_picture_params(AVCodecContext *avctx,
>         }
>
>         ref                            = pic->refs[1][pic->nb_refs[1] - 1];
>-        href                           = ref->priv_data;
>+        href                           = ref->codec_priv;
>         fh->ref_order_hint[href->slot] = ref->display_order - href->last_idr_frame;
>         for (i = AV1_REF_FRAME_GOLDEN; i < AV1_REFS_PER_FRAME; i++) {
>             fh->ref_frame_idx[i] = href->slot; @@ -634,7 +634,7 @@ static int
>vaapi_encode_av1_init_picture_params(AVCodecContext *avctx,
>         for (int j = 0; j < pic->nb_refs[i]; j++) {
>             FFHWBaseEncodePicture *ref_pic = pic->refs[i][j];
>
>-            slot = ((VAAPIEncodeAV1Picture*)ref_pic->priv_data)->slot;
>+            slot = ((VAAPIEncodeAV1Picture*)ref_pic->codec_priv)->slot;
>             av_assert0(vpic->reference_frames[slot] == VA_INVALID_SURFACE);
>
>             vpic->reference_frames[slot] = ((VAAPIEncodePicture *)ref_pic)-
>>recon_surface; @@ -732,7 +732,7 @@ end:
> }
>
> static int vaapi_encode_av1_init_slice_params(AVCodecContext *avctx,
>-                                              VAAPIEncodePicture *pic,
>+                                              FFHWBaseEncodePicture
>+ *base,
>                                               VAAPIEncodeSlice *slice)  {
>     VAAPIEncodeAV1Context      *priv = avctx->priv_data;
>@@ -770,7 +770,7 @@ static int
>vaapi_encode_av1_write_picture_header(AVCodecContext *avctx,
>     /** Pack repeat frame header. */
>     if (pic->display_order > pic->encode_order) {
>         memset(fh_obu, 0, sizeof(*fh_obu));
>-        href = pic->refs[0][pic->nb_refs[0] - 1]->priv_data;
>+        href = pic->refs[0][pic->nb_refs[0] - 1]->codec_priv;
>         fh_obu->header.obu_type = AV1_OBU_FRAME_HEADER;
>         fh_obu->header.obu_has_size_field = 1;
>
>diff --git a/libavcodec/vaapi_encode_h264.c b/libavcodec/vaapi_encode_h264.c
>index d156719728..f9e86d07f3 100644
>--- a/libavcodec/vaapi_encode_h264.c
>+++ b/libavcodec/vaapi_encode_h264.c
>@@ -620,14 +620,14 @@ static int
>vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)  }
>
> static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
>-                                                 VAAPIEncodePicture *vaapi_pic)
>+                                                 FFHWBaseEncodePicture
>+ *pic)
> {
>     FFHWBaseEncodeContext       *base_ctx = avctx->priv_data;
>     VAAPIEncodeH264Context          *priv = avctx->priv_data;
>-    const FFHWBaseEncodePicture      *pic = &vaapi_pic->base;
>-    VAAPIEncodeH264Picture          *hpic = pic->priv_data;
>+    VAAPIEncodePicture         *vaapi_pic = pic->priv;
>+    VAAPIEncodeH264Picture          *hpic = pic->codec_priv;
>     FFHWBaseEncodePicture           *prev = pic->prev;
>-    VAAPIEncodeH264Picture         *hprev = prev ? prev->priv_data : NULL;
>+    VAAPIEncodeH264Picture         *hprev = prev ? prev->codec_priv : NULL;
>     VAEncPictureParameterBufferH264 *vpic = vaapi_pic->codec_picture_params;
>     int i, j = 0;
>
>@@ -736,7 +736,7 @@ static int
>vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
>             VAAPIEncodeH264Picture *href;
>
>             av_assert0(ref && ref->encode_order < pic->encode_order);
>-            href = ref->priv_data;
>+            href = ref->codec_priv;
>
>             vpic->ReferenceFrames[j++] = (VAPictureH264) {
>                 .picture_id          = ((VAAPIEncodePicture *)ref)->recon_surface,
>@@ -778,15 +778,15 @@ static void
>vaapi_encode_h264_default_ref_pic_list(AVCodecContext *avctx,
>
>     prev = pic->prev;
>     av_assert0(prev);
>-    hp = pic->priv_data;
>+    hp = pic->codec_priv;
>
>     for (i = 0; i < pic->prev->nb_dpb_pics; i++) {
>-        hn = prev->dpb[i]->priv_data;
>+        hn = prev->dpb[i]->codec_priv;
>         av_assert0(hn->frame_num < hp->frame_num);
>
>         if (pic->type == FF_HW_PICTURE_TYPE_P) {
>             for (j = n; j > 0; j--) {
>-                hc = rpl0[j - 1]->base.priv_data;
>+                hc = rpl0[j - 1]->base.codec_priv;
>                 av_assert0(hc->frame_num != hn->frame_num);
>                 if (hc->frame_num > hn->frame_num)
>                     break;
>@@ -796,7 +796,7 @@ static void
>vaapi_encode_h264_default_ref_pic_list(AVCodecContext *avctx,
>
>         } else if (pic->type == FF_HW_PICTURE_TYPE_B) {
>             for (j = n; j > 0; j--) {
>-                hc = rpl0[j - 1]->base.priv_data;
>+                hc = rpl0[j - 1]->base.codec_priv;
>                 av_assert0(hc->pic_order_cnt != hp->pic_order_cnt);
>                 if (hc->pic_order_cnt < hp->pic_order_cnt) {
>                     if (hn->pic_order_cnt > hp->pic_order_cnt || @@ -811,7 +811,7 @@
>static void vaapi_encode_h264_default_ref_pic_list(AVCodecContext *avctx,
>             rpl0[j] = (VAAPIEncodePicture *)prev->dpb[i];
>
>             for (j = n; j > 0; j--) {
>-                hc = rpl1[j - 1]->base.priv_data;
>+                hc = rpl1[j - 1]->base.codec_priv;
>                 av_assert0(hc->pic_order_cnt != hp->pic_order_cnt);
>                 if (hc->pic_order_cnt > hp->pic_order_cnt) {
>                     if (hn->pic_order_cnt < hp->pic_order_cnt || @@ -843,7 +843,7 @@
>static void vaapi_encode_h264_default_ref_pic_list(AVCodecContext *avctx,
>         av_log(avctx, AV_LOG_DEBUG, "Default RefPicList0 for fn=%d/poc=%d:",
>                hp->frame_num, hp->pic_order_cnt);
>         for (i = 0; i < n; i++) {
>-            hn = rpl0[i]->base.priv_data;
>+            hn = rpl0[i]->base.codec_priv;
>             av_log(avctx, AV_LOG_DEBUG, "  fn=%d/poc=%d",
>                    hn->frame_num, hn->pic_order_cnt);
>         }
>@@ -853,7 +853,7 @@ static void
>vaapi_encode_h264_default_ref_pic_list(AVCodecContext *avctx,
>         av_log(avctx, AV_LOG_DEBUG, "Default RefPicList1 for fn=%d/poc=%d:",
>                hp->frame_num, hp->pic_order_cnt);
>         for (i = 0; i < n; i++) {
>-            hn = rpl1[i]->base.priv_data;
>+            hn = rpl1[i]->base.codec_priv;
>             av_log(avctx, AV_LOG_DEBUG, "  fn=%d/poc=%d",
>                    hn->frame_num, hn->pic_order_cnt);
>         }
>@@ -864,12 +864,12 @@ static void
>vaapi_encode_h264_default_ref_pic_list(AVCodecContext *avctx,  }
>
> static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
>-                                               VAAPIEncodePicture *vaapi_pic,
>+                                               FFHWBaseEncodePicture
>+ *pic,
>                                                VAAPIEncodeSlice *slice)  {
>     VAAPIEncodeH264Context          *priv = avctx->priv_data;
>-    const FFHWBaseEncodePicture      *pic = &vaapi_pic->base;
>-    VAAPIEncodeH264Picture          *hpic = pic->priv_data;
>+    VAAPIEncodePicture         *vaapi_pic = pic->priv;
>+    VAAPIEncodeH264Picture          *hpic = pic->codec_priv;
>     FFHWBaseEncodePicture           *prev = pic->prev;
>     H264RawSPS                       *sps = &priv->raw_sps;
>     H264RawPPS                       *pps = &priv->raw_pps;
>@@ -931,7 +931,7 @@ static int
>vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
>         } else {
>             sh->adaptive_ref_pic_marking_mode_flag = 1;
>             for (i = 0; i < discard; i++) {
>-                VAAPIEncodeH264Picture *old = discard_list[i]->priv_data;
>+                VAAPIEncodeH264Picture *old =
>+ discard_list[i]->codec_priv;
>                 av_assert0(old->frame_num < hpic->frame_num);
>                 sh->mmco[i].memory_management_control_operation = 1;
>                 sh->mmco[i].difference_of_pic_nums_minus1 = @@ -963,7 +963,7 @@
>static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
>             if (need_rplm) {
>                 int pic_num = hpic->frame_num;
>                 for (i = 0; i < pic->nb_refs[0]; i++) {
>-                    href = pic->refs[0][i]->priv_data;
>+                    href = pic->refs[0][i]->codec_priv;
>                     av_assert0(href->frame_num != pic_num);
>                     if (href->frame_num < pic_num) {
>                         sh->rplm_l0[i].modification_of_pic_nums_idc = 0; @@ -984,7
>+984,7 @@ static int vaapi_encode_h264_init_slice_params(AVCodecContext
>*avctx,
>             int n0 = 0, n1 = 0;
>             for (i = 0; i < pic->nb_refs[0]; i++) {
>                 av_assert0(pic->refs[0][i]);
>-                href = pic->refs[0][i]->priv_data;
>+                href = pic->refs[0][i]->codec_priv;
>                 av_assert0(href->pic_order_cnt < hpic->pic_order_cnt);
>                 if (pic->refs[0][i] != (FFHWBaseEncodePicture *)def_l0[n0])
>                     need_rplm_l0 = 1;
>@@ -993,7 +993,7 @@ static int
>vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
>
>             for (i = 0; i < pic->nb_refs[1]; i++) {
>                 av_assert0(pic->refs[1][i]);
>-                href = pic->refs[1][i]->priv_data;
>+                href = pic->refs[1][i]->codec_priv;
>                 av_assert0(href->pic_order_cnt > hpic->pic_order_cnt);
>                 if (pic->refs[1][i] != (FFHWBaseEncodePicture *)def_l1[n1])
>                     need_rplm_l1 = 1;
>@@ -1004,7 +1004,7 @@ static int
>vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
>             if (need_rplm_l0) {
>                 int pic_num = hpic->frame_num;
>                 for (i = j = 0; i < pic->nb_refs[0]; i++) {
>-                    href = pic->refs[0][i]->priv_data;
>+                    href = pic->refs[0][i]->codec_priv;
>                     av_assert0(href->frame_num != pic_num);
>                     if (href->frame_num < pic_num) {
>                         sh->rplm_l0[j].modification_of_pic_nums_idc = 0; @@ -1026,7
>+1026,7 @@ static int vaapi_encode_h264_init_slice_params(AVCodecContext
>*avctx,
>             if (need_rplm_l1) {
>                 int pic_num = hpic->frame_num;
>                 for (i = j = 0; i < pic->nb_refs[1]; i++) {
>-                    href = pic->refs[1][i]->priv_data;
>+                    href = pic->refs[1][i]->codec_priv;
>                     av_assert0(href->frame_num != pic_num);
>                     if (href->frame_num < pic_num) {
>                         sh->rplm_l1[j].modification_of_pic_nums_idc = 0; diff --git
>a/libavcodec/vaapi_encode_h265.c b/libavcodec/vaapi_encode_h265.c index
>bbd174f975..3ec5aef045 100644
>--- a/libavcodec/vaapi_encode_h265.c
>+++ b/libavcodec/vaapi_encode_h265.c
>@@ -757,14 +757,14 @@ static int
>vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)  }
>
> static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
>-                                                 VAAPIEncodePicture *vaapi_pic)
>+                                                 FFHWBaseEncodePicture
>+ *pic)
> {
>     FFHWBaseEncodeContext       *base_ctx = avctx->priv_data;
>     VAAPIEncodeH265Context          *priv = avctx->priv_data;
>-    FFHWBaseEncodePicture            *pic = &vaapi_pic->base;
>-    VAAPIEncodeH265Picture          *hpic = pic->priv_data;
>+    VAAPIEncodePicture         *vaapi_pic = pic->priv;
>+    VAAPIEncodeH265Picture          *hpic = pic->codec_priv;

Same changes should be made in d3d12va_encode_hevc

>     FFHWBaseEncodePicture           *prev = pic->prev;
>-    VAAPIEncodeH265Picture         *hprev = prev ? prev->priv_data : NULL;
>+    VAAPIEncodeH265Picture         *hprev = prev ? prev->codec_priv : NULL;
>     VAEncPictureParameterBufferHEVC *vpic = vaapi_pic->codec_picture_params;
>     int i, j = 0;
>
>@@ -923,7 +923,7 @@ static int
>vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
>             VAAPIEncodeH265Picture *href;
>
>             av_assert0(ref && ref->encode_order < pic->encode_order);
>-            href = ref->priv_data;
>+            href = ref->codec_priv;
>
>             vpic->reference_frames[j++] = (VAPictureHEVC) {
>                 .picture_id    = ((VAAPIEncodePicture *)ref)->recon_surface,
>@@ -973,13 +973,13 @@ static int
>vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,  }
>
> static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
>-                                               VAAPIEncodePicture *vaapi_pic,
>+                                               FFHWBaseEncodePicture
>+ *pic,
>                                                VAAPIEncodeSlice *slice)  {
>     FFHWBaseEncodeContext        *base_ctx = avctx->priv_data;
>     VAAPIEncodeH265Context           *priv = avctx->priv_data;
>-    const FFHWBaseEncodePicture       *pic = &vaapi_pic->base;
>-    VAAPIEncodeH265Picture           *hpic = pic->priv_data;
>+    VAAPIEncodePicture          *vaapi_pic = pic->priv;
>+    VAAPIEncodeH265Picture           *hpic = pic->codec_priv;
>     const H265RawSPS                  *sps = &priv->raw_sps;
>     const H265RawPPS                  *pps = &priv->raw_pps;
>     H265RawSliceHeader                 *sh = &priv->raw_slice.header;
>@@ -1021,7 +1021,7 @@ static int
>vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
>         rps_pics = 0;
>         for (i = 0; i < MAX_REFERENCE_LIST_NUM; i++) {
>             for (j = 0; j < pic->nb_refs[i]; j++) {
>-                strp = pic->refs[i][j]->priv_data;
>+                strp = pic->refs[i][j]->codec_priv;
>                 rps_poc[rps_pics]  = strp->pic_order_cnt;
>                 rps_used[rps_pics] = 1;
>                 ++rps_pics;
>@@ -1046,7 +1046,7 @@ static int
>vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
>             if (j < pic->nb_refs[1])
>                 continue;
>
>-            strp = pic->dpb[i]->priv_data;
>+            strp = pic->dpb[i]->codec_priv;
>             rps_poc[rps_pics]  = strp->pic_order_cnt;
>             rps_used[rps_pics] = 0;
>             ++rps_pics;
>diff --git a/libavcodec/vaapi_encode_mjpeg.c
>b/libavcodec/vaapi_encode_mjpeg.c index 0ca8d676dd..5a244bb3aa 100644
>--- a/libavcodec/vaapi_encode_mjpeg.c
>+++ b/libavcodec/vaapi_encode_mjpeg.c
>@@ -220,11 +220,11 @@ static int
>vaapi_encode_mjpeg_write_extra_buffer(AVCodecContext *avctx,  }
>
> static int vaapi_encode_mjpeg_init_picture_params(AVCodecContext *avctx,
>-                                                  VAAPIEncodePicture *vaapi_pic)
>+                                                  FFHWBaseEncodePicture
>+ *pic)
> {
>     FFHWBaseEncodeContext       *base_ctx = avctx->priv_data;
>     VAAPIEncodeMJPEGContext         *priv = avctx->priv_data;
>-    const FFHWBaseEncodePicture      *pic = &vaapi_pic->base;
>+    VAAPIEncodePicture         *vaapi_pic = pic->priv;
>     JPEGRawFrameHeader                *fh = &priv->frame_header;
>     JPEGRawScanHeader                 *sh = &priv->scan.header;
>     VAEncPictureParameterBufferJPEG *vpic = vaapi_pic->codec_picture_params;
>@@ -414,10 +414,11 @@ static int
>vaapi_encode_mjpeg_init_picture_params(AVCodecContext *avctx,  }
>
> static int vaapi_encode_mjpeg_init_slice_params(AVCodecContext *avctx,
>-                                                VAAPIEncodePicture *pic,
>+                                                FFHWBaseEncodePicture
>+ *base,
>                                                 VAAPIEncodeSlice *slice)  {
>     VAAPIEncodeMJPEGContext         *priv = avctx->priv_data;
>+    VAAPIEncodePicture               *pic = base->priv;
>     JPEGRawScanHeader                 *sh = &priv->scan.header;
>     VAEncSliceParameterBufferJPEG *vslice = slice->codec_slice_params;
>     int i;
>diff --git a/libavcodec/vaapi_encode_mpeg2.c
>b/libavcodec/vaapi_encode_mpeg2.c index be801d21d2..7dadcf9140 100644
>--- a/libavcodec/vaapi_encode_mpeg2.c
>+++ b/libavcodec/vaapi_encode_mpeg2.c
>@@ -418,10 +418,10 @@ static int
>vaapi_encode_mpeg2_init_sequence_params(AVCodecContext *avctx)  }
>
> static int vaapi_encode_mpeg2_init_picture_params(AVCodecContext *avctx,
>-                                                  VAAPIEncodePicture *vaapi_pic)
>+                                                  FFHWBaseEncodePicture
>+ *pic)
> {
>     VAAPIEncodeMPEG2Context          *priv = avctx->priv_data;
>-    const FFHWBaseEncodePicture       *pic = &vaapi_pic->base;
>+    VAAPIEncodePicture          *vaapi_pic = pic->priv;
>     MPEG2RawPictureHeader              *ph = &priv->picture_header;
>     MPEG2RawPictureCodingExtension    *pce = &priv-
>>picture_coding_extension.data.picture_coding;
>     VAEncPictureParameterBufferMPEG2 *vpic = vaapi_pic-
>>codec_picture_params; @@ -481,10 +481,9 @@ static int
>vaapi_encode_mpeg2_init_picture_params(AVCodecContext *avctx,  }
>
> static int vaapi_encode_mpeg2_init_slice_params(AVCodecContext *avctx,
>-                                                VAAPIEncodePicture *vaapi_pic,
>+                                                FFHWBaseEncodePicture
>+ *pic,
>                                                 VAAPIEncodeSlice *slice)  {
>-    const FFHWBaseEncodePicture       *pic = &vaapi_pic->base;
>     VAAPIEncodeMPEG2Context          *priv = avctx->priv_data;
>     VAEncSliceParameterBufferMPEG2 *vslice = slice->codec_slice_params;
>     int qp;
>diff --git a/libavcodec/vaapi_encode_vp8.c b/libavcodec/vaapi_encode_vp8.c
>index 634b849cd2..8b9cac9ebc 100644
>--- a/libavcodec/vaapi_encode_vp8.c
>+++ b/libavcodec/vaapi_encode_vp8.c
>@@ -74,9 +74,9 @@ static int
>vaapi_encode_vp8_init_sequence_params(AVCodecContext *avctx)  }
>
> static int vaapi_encode_vp8_init_picture_params(AVCodecContext *avctx,
>-                                                VAAPIEncodePicture *vaapi_pic)
>+                                                FFHWBaseEncodePicture
>+ *pic)
> {
>-    const FFHWBaseEncodePicture     *pic = &vaapi_pic->base;
>+    VAAPIEncodePicture        *vaapi_pic = pic->priv;
>     VAAPIEncodeVP8Context          *priv = avctx->priv_data;
>     VAEncPictureParameterBufferVP8 *vpic = vaapi_pic->codec_picture_params;
>     int i;
>diff --git a/libavcodec/vaapi_encode_vp9.c b/libavcodec/vaapi_encode_vp9.c
>index eac9be82b0..b424357757 100644
>--- a/libavcodec/vaapi_encode_vp9.c
>+++ b/libavcodec/vaapi_encode_vp9.c
>@@ -77,12 +77,12 @@ static int
>vaapi_encode_vp9_init_sequence_params(AVCodecContext *avctx)  }
>
> static int vaapi_encode_vp9_init_picture_params(AVCodecContext *avctx,
>-                                                VAAPIEncodePicture *vaapi_pic)
>+                                                FFHWBaseEncodePicture
>+ *pic)
> {
>     FFHWBaseEncodeContext      *base_ctx = avctx->priv_data;
>     VAAPIEncodeVP9Context          *priv = avctx->priv_data;
>-    const FFHWBaseEncodePicture     *pic = &vaapi_pic->base;
>-    VAAPIEncodeVP9Picture          *hpic = pic->priv_data;
>+    VAAPIEncodePicture        *vaapi_pic = pic->priv;
>+    VAAPIEncodeVP9Picture          *hpic = pic->codec_priv;
>     VAEncPictureParameterBufferVP9 *vpic = vaapi_pic->codec_picture_params;
>     int i;
>     int num_tile_columns;
>@@ -106,7 +106,7 @@ static int
>vaapi_encode_vp9_init_picture_params(AVCodecContext *avctx,
>     case FF_HW_PICTURE_TYPE_P:
>         av_assert0(!pic->nb_refs[1]);
>         {
>-            VAAPIEncodeVP9Picture *href = pic->refs[0][0]->priv_data;
>+            VAAPIEncodeVP9Picture *href = pic->refs[0][0]->codec_priv;
>             av_assert0(href->slot == 0 || href->slot == 1);
>
>             if (base_ctx->max_b_depth > 0) { @@ -124,8 +124,8 @@ static int
>vaapi_encode_vp9_init_picture_params(AVCodecContext *avctx,
>     case FF_HW_PICTURE_TYPE_B:
>         av_assert0(pic->nb_refs[0] && pic->nb_refs[1]);
>         {
>-            VAAPIEncodeVP9Picture *href0 = pic->refs[0][0]->priv_data,
>-                                  *href1 = pic->refs[1][0]->priv_data;
>+            VAAPIEncodeVP9Picture *href0 = pic->refs[0][0]->codec_priv,
>+                                  *href1 = pic->refs[1][0]->codec_priv;
>             av_assert0(href0->slot < pic->b_depth + 1 &&
>                        href1->slot < pic->b_depth + 1);
>
>@@ -163,7 +163,7 @@ static int
>vaapi_encode_vp9_init_picture_params(AVCodecContext *avctx,
>         for (int j = 0; j < pic->nb_refs[i]; j++) {
>             FFHWBaseEncodePicture *ref_pic = pic->refs[i][j];
>             int slot;
>-            slot = ((VAAPIEncodeVP9Picture*)ref_pic->priv_data)->slot;
>+            slot = ((VAAPIEncodeVP9Picture*)ref_pic->codec_priv)->slot;
>             av_assert0(vpic->reference_frames[slot] == VA_INVALID_SURFACE);
>             vpic->reference_frames[slot] = ((VAAPIEncodePicture *)ref_pic)-
>>recon_surface;
>         }
>--
>2.45.2.753.g447d99e1c3b

Thanks for the refactor.
It looks like an untested patch which still has some compile errors.
Anyways I could help test and verify when everything's ready.

Best Regards,
Tong




More information about the ffmpeg-devel mailing list