diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c
index 87056520dd..c02408548c 100644
--- a/libavcodec/av1dec.c
+++ b/libavcodec/av1dec.c
@@ -27,7 +27,6 @@
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "av1_parse.h"
-#include "decode.h"
 #include "av1dec.h"
 #include "atsc_a53.h"
 #include "bytestream.h"
@@ -640,8 +639,7 @@ static int get_pixel_format(AVCodecContext *avctx)
 static void av1_frame_unref(AVCodecContext *avctx, AV1Frame *f)
 {
     ff_thread_release_buffer(avctx, f->f);
-    av_buffer_unref(&f->hwaccel_priv_buf);
-    f->hwaccel_picture_private = NULL;
+    ff_refstruct_unref(&f->hwaccel_picture_private);
     ff_refstruct_unref(&f->header_ref);
     f->raw_frame_header = NULL;
     f->spatial_id = f->temporal_id = 0;
@@ -666,12 +664,8 @@ static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *s
     if (ret < 0)
         goto fail;
 
-    if (src->hwaccel_picture_private) {
-        dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
-        if (!dst->hwaccel_priv_buf)
-            goto fail;
-        dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
-    }
+    ff_refstruct_replace(&dst->hwaccel_picture_private,
+                          src->hwaccel_picture_private);
 
     dst->spatial_id = src->spatial_id;
     dst->temporal_id = src->temporal_id;
@@ -915,8 +909,7 @@ static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
         break;
     }
 
-    ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private,
-                                      &f->hwaccel_priv_buf);
+    ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private);
     if (ret < 0)
         goto fail;
 
diff --git a/libavcodec/av1dec.h b/libavcodec/av1dec.h
index acbeec4af3..b6a0c08e48 100644
--- a/libavcodec/av1dec.h
+++ b/libavcodec/av1dec.h
@@ -24,7 +24,6 @@
 #include <stdint.h>
 
 #include "libavutil/fifo.h"
-#include "libavutil/buffer.h"
 #include "libavutil/frame.h"
 #include "libavutil/pixfmt.h"
 #include "avcodec.h"
@@ -35,8 +34,7 @@
 typedef struct AV1Frame {
     AVFrame *f;
 
-    AVBufferRef *hwaccel_priv_buf;
-    void *hwaccel_picture_private;
+    void *hwaccel_picture_private; ///< RefStruct reference
 
     AV1RawOBU *header_ref; ///< RefStruct reference backing raw_frame_header.
     AV1RawFrameHeader *raw_frame_header;
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 169ee79acd..7abfe7f0ce 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -28,18 +28,13 @@
 #endif
 
 #include "libavutil/avassert.h"
-#include "libavutil/avstring.h"
-#include "libavutil/bprint.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
 #include "libavutil/emms.h"
-#include "libavutil/fifo.h"
 #include "libavutil/frame.h"
 #include "libavutil/hwcontext.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
-#include "libavutil/intmath.h"
-#include "libavutil/opt.h"
 
 #include "avcodec.h"
 #include "avcodec_internal.h"
@@ -51,6 +46,7 @@
 #include "hwconfig.h"
 #include "internal.h"
 #include "packet_internal.h"
+#include "refstruct.h"
 #include "thread.h"
 
 typedef struct DecodeContext {
@@ -1790,34 +1786,22 @@ int ff_copy_palette(void *dst, const AVPacket *src, void *logctx)
     return 0;
 }
 
-int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private,
-                                AVBufferRef **hwaccel_priv_buf)
+int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private)
 {
     const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
-    AVBufferRef *ref;
     AVHWFramesContext *frames_ctx;
-    uint8_t *data;
 
     if (!hwaccel || !hwaccel->frame_priv_data_size)
         return 0;
 
     av_assert0(!*hwaccel_picture_private);
-    data       = av_mallocz(hwaccel->frame_priv_data_size);
-    if (!data)
-        return AVERROR(ENOMEM);
 
     frames_ctx = (AVHWFramesContext *)avctx->hw_frames_ctx->data;
-
-    ref = av_buffer_create(data, hwaccel->frame_priv_data_size,
-                           hwaccel->free_frame_priv,
-                           frames_ctx->device_ctx, 0);
-    if (!ref) {
-        av_free(data);
+    *hwaccel_picture_private = ff_refstruct_alloc_ext(hwaccel->frame_priv_data_size, 0,
+                                                      frames_ctx->device_ctx,
+                                                      hwaccel->free_frame_priv);
+    if (!*hwaccel_picture_private)
         return AVERROR(ENOMEM);
-    }
-
-    *hwaccel_priv_buf        = ref;
-    *hwaccel_picture_private = ref->data;
 
     return 0;
 }
diff --git a/libavcodec/decode.h b/libavcodec/decode.h
index a52152e4a7..ce97c53933 100644
--- a/libavcodec/decode.h
+++ b/libavcodec/decode.h
@@ -21,7 +21,6 @@
 #ifndef AVCODEC_DECODE_H
 #define AVCODEC_DECODE_H
 
-#include "libavutil/buffer.h"
 #include "libavutil/frame.h"
 #include "libavutil/hwcontext.h"
 
@@ -141,16 +140,13 @@ int ff_side_data_update_matrix_encoding(AVFrame *frame,
 
 /**
  * Allocate a hwaccel frame private data if the provided avctx
- * uses a hwaccel method that needs it. The private data will
- * be refcounted via the AVBuffer API (if allocated).
+ * uses a hwaccel method that needs it. The returned data is
+ * a RefStruct reference (if allocated).
  *
  * @param  avctx                   The codec context
  * @param  hwaccel_picture_private Pointer to return hwaccel_picture_private
- * @param  hwaccel_priv_buf        Pointer to return the AVBufferRef owning
- *                                 hwaccel_picture_private
  * @return 0 on success, < 0 on error
  */
-int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private,
-                                AVBufferRef **hwaccel_priv_buf);
+int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private);
 
 #endif /* AVCODEC_DECODE_H */
diff --git a/libavcodec/h264_picture.c b/libavcodec/h264_picture.c
index 25d0d96ddb..9353e4b445 100644
--- a/libavcodec/h264_picture.c
+++ b/libavcodec/h264_picture.c
@@ -46,7 +46,7 @@ void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
 
     ff_thread_release_ext_buffer(h->avctx, &pic->tf);
     ff_thread_release_buffer(h->avctx, pic->f_grain);
-    av_buffer_unref(&pic->hwaccel_priv_buf);
+    ff_refstruct_unref(&pic->hwaccel_picture_private);
 
     av_buffer_unref(&pic->qscale_table_buf);
     av_buffer_unref(&pic->mb_type_buf);
@@ -129,14 +129,8 @@ int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
         }
     }
 
-    if (src->hwaccel_picture_private) {
-        dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
-        if (!dst->hwaccel_priv_buf) {
-            ret = AVERROR(ENOMEM);
-            goto fail;
-        }
-        dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
-    }
+    ff_refstruct_replace(&dst->hwaccel_picture_private,
+                          src->hwaccel_picture_private);
 
     ret = av_buffer_replace(&dst->decode_error_flags, src->decode_error_flags);
     if (ret < 0)
@@ -185,11 +179,8 @@ int ff_h264_replace_picture(H264Context *h, H264Picture *dst, const H264Picture
             goto fail;
     }
 
-    ret = av_buffer_replace(&dst->hwaccel_priv_buf, src->hwaccel_priv_buf);
-    if (ret < 0)
-        goto fail;
-
-    dst->hwaccel_picture_private = src->hwaccel_picture_private;
+    ff_refstruct_replace(&dst->hwaccel_picture_private,
+                          src->hwaccel_picture_private);
 
     ret = av_buffer_replace(&dst->decode_error_flags, src->decode_error_flags);
     if (ret < 0)
diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
index 632f5b23b2..6df80c9522 100644
--- a/libavcodec/h264_slice.c
+++ b/libavcodec/h264_slice.c
@@ -206,8 +206,7 @@ static int alloc_picture(H264Context *h, H264Picture *pic)
             goto fail;
     }
 
-    ret = ff_hwaccel_frame_priv_alloc(h->avctx, &pic->hwaccel_picture_private,
-                                      &pic->hwaccel_priv_buf);
+    ret = ff_hwaccel_frame_priv_alloc(h->avctx, &pic->hwaccel_picture_private);
     if (ret < 0)
         goto fail;
 
diff --git a/libavcodec/h264dec.h b/libavcodec/h264dec.h
index 513856749a..7436e0a54f 100644
--- a/libavcodec/h264dec.h
+++ b/libavcodec/h264dec.h
@@ -117,7 +117,6 @@ typedef struct H264Picture {
     AVBufferRef *mb_type_buf;
     uint32_t *mb_type;
 
-    AVBufferRef *hwaccel_priv_buf;
     void *hwaccel_picture_private; ///< hardware accelerator private data
 
     AVBufferRef *ref_index_buf[2];
diff --git a/libavcodec/hevc_refs.c b/libavcodec/hevc_refs.c
index c5c1203ef8..8f49704b54 100644
--- a/libavcodec/hevc_refs.c
+++ b/libavcodec/hevc_refs.c
@@ -27,6 +27,7 @@
 #include "thread.h"
 #include "hevc.h"
 #include "hevcdec.h"
+#include "refstruct.h"
 #include "threadframe.h"
 
 void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags)
@@ -51,8 +52,7 @@ void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags)
 
         frame->collocated_ref = NULL;
 
-        av_buffer_unref(&frame->hwaccel_priv_buf);
-        frame->hwaccel_picture_private = NULL;
+        ff_refstruct_unref(&frame->hwaccel_picture_private);
     }
 }
 
@@ -118,8 +118,7 @@ static HEVCFrame *alloc_frame(HEVCContext *s)
             (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD))
             frame->frame->flags |= AV_FRAME_FLAG_INTERLACED;
 
-        ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private,
-                                          &frame->hwaccel_priv_buf);
+        ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private);
         if (ret < 0)
             goto fail;
 
diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index 44a9680415..8316a815e7 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -28,7 +28,6 @@
 #include "libavutil/attributes.h"
 #include "libavutil/avstring.h"
 #include "libavutil/common.h"
-#include "libavutil/display.h"
 #include "libavutil/film_grain_params.h"
 #include "libavutil/internal.h"
 #include "libavutil/md5.h"
@@ -37,13 +36,11 @@
 #include "libavutil/timecode.h"
 
 #include "bswapdsp.h"
-#include "bytestream.h"
 #include "cabac_functions.h"
 #include "codec_internal.h"
 #include "decode.h"
 #include "golomb.h"
 #include "hevc.h"
-#include "hevc_data.h"
 #include "hevc_parse.h"
 #include "hevcdec.h"
 #include "hwaccel_internal.h"
@@ -3416,12 +3413,8 @@ static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
     dst->flags      = src->flags;
     dst->sequence   = src->sequence;
 
-    if (src->hwaccel_picture_private) {
-        dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
-        if (!dst->hwaccel_priv_buf)
-            goto fail;
-        dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
-    }
+    ff_refstruct_replace(&dst->hwaccel_picture_private,
+                          src->hwaccel_picture_private);
 
     return 0;
 fail:
diff --git a/libavcodec/hevcdec.h b/libavcodec/hevcdec.h
index 94609e4699..1d29fc24d7 100644
--- a/libavcodec/hevcdec.h
+++ b/libavcodec/hevcdec.h
@@ -419,8 +419,7 @@ typedef struct HEVCFrame {
     AVBufferRef *rpl_tab_buf;
     AVBufferRef *rpl_buf;
 
-    AVBufferRef *hwaccel_priv_buf;
-    void *hwaccel_picture_private;
+    void *hwaccel_picture_private; ///< RefStruct reference
 
     /**
      * A sequence counter, so that old frames are output first
diff --git a/libavcodec/hwaccel_internal.h b/libavcodec/hwaccel_internal.h
index edfe283150..057b07323d 100644
--- a/libavcodec/hwaccel_internal.h
+++ b/libavcodec/hwaccel_internal.h
@@ -26,6 +26,7 @@
 #include <stdint.h>
 
 #include "avcodec.h"
+#include "refstruct.h"
 
 #define HWACCEL_CAP_ASYNC_SAFE      (1 << 0)
 #define HWACCEL_CAP_THREAD_SAFE     (1 << 1)
@@ -154,7 +155,7 @@ typedef struct FFHWAccel {
      * @param hwctx a pointer to an AVHWDeviceContext.
      * @param data the per-frame hardware accelerator private data to be freed.
      */
-    void (*free_frame_priv)(void *hwctx, uint8_t *data);
+    void (*free_frame_priv)(FFRefStructOpaque hwctx, void *data);
 
     /**
      * Callback to flush the hwaccel state.
diff --git a/libavcodec/mpegpicture.c b/libavcodec/mpegpicture.c
index b7c804c8ec..4126756e9b 100644
--- a/libavcodec/mpegpicture.c
+++ b/libavcodec/mpegpicture.c
@@ -27,11 +27,11 @@
 
 #include "avcodec.h"
 #include "encode.h"
-#include "internal.h"
 #include "decode.h"
 #include "motion_est.h"
 #include "mpegpicture.h"
 #include "mpegutils.h"
+#include "refstruct.h"
 #include "threadframe.h"
 
 static void av_noinline free_picture_tables(Picture *pic)
@@ -171,8 +171,7 @@ static int alloc_frame_buffer(AVCodecContext *avctx,  Picture *pic,
         pic->f->height = avctx->height;
     }
 
-    ret = ff_hwaccel_frame_priv_alloc(avctx, &pic->hwaccel_picture_private,
-                                      &pic->hwaccel_priv_buf);
+    ret = ff_hwaccel_frame_priv_alloc(avctx, &pic->hwaccel_picture_private);
     if (ret < 0)
         return ret;
 
@@ -316,12 +315,11 @@ void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
     else if (pic->f)
         av_frame_unref(pic->f);
 
-    av_buffer_unref(&pic->hwaccel_priv_buf);
+    ff_refstruct_unref(&pic->hwaccel_picture_private);
 
     if (pic->needs_realloc)
         free_picture_tables(pic);
 
-    pic->hwaccel_picture_private = NULL;
     pic->field_picture = 0;
     pic->b_frame_score = 0;
     pic->needs_realloc = 0;
@@ -380,14 +378,8 @@ int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
     if (ret < 0)
         goto fail;
 
-    if (src->hwaccel_picture_private) {
-        dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
-        if (!dst->hwaccel_priv_buf) {
-            ret = AVERROR(ENOMEM);
-            goto fail;
-        }
-        dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
-    }
+    ff_refstruct_replace(&dst->hwaccel_picture_private,
+                          src->hwaccel_picture_private);
 
     dst->field_picture           = src->field_picture;
     dst->b_frame_score           = src->b_frame_score;
diff --git a/libavcodec/mpegpicture.h b/libavcodec/mpegpicture.h
index 7919aa402c..477b3ac535 100644
--- a/libavcodec/mpegpicture.h
+++ b/libavcodec/mpegpicture.h
@@ -66,7 +66,6 @@ typedef struct Picture {
     int alloc_mb_height;        ///< mb_height used to allocate tables
     int alloc_mb_stride;        ///< mb_stride used to allocate tables
 
-    AVBufferRef *hwaccel_priv_buf;
     void *hwaccel_picture_private; ///< Hardware accelerator private data
 
     int field_picture;          ///< whether or not the picture was encoded in separate fields
diff --git a/libavcodec/vp8.c b/libavcodec/vp8.c
index db325dc90b..4a51c551b8 100644
--- a/libavcodec/vp8.c
+++ b/libavcodec/vp8.c
@@ -108,8 +108,7 @@ static int vp8_alloc_frame(VP8Context *s, VP8Frame *f, int ref)
         return ret;
     if (!(f->seg_map = ff_refstruct_allocz(s->mb_width * s->mb_height)))
         goto fail;
-    ret = ff_hwaccel_frame_priv_alloc(s->avctx, &f->hwaccel_picture_private,
-                                      &f->hwaccel_priv_buf);
+    ret = ff_hwaccel_frame_priv_alloc(s->avctx, &f->hwaccel_picture_private);
     if (ret < 0)
         goto fail;
 
@@ -124,8 +123,7 @@ fail:
 static void vp8_release_frame(VP8Context *s, VP8Frame *f)
 {
     ff_refstruct_unref(&f->seg_map);
-    av_buffer_unref(&f->hwaccel_priv_buf);
-    f->hwaccel_picture_private = NULL;
+    ff_refstruct_unref(&f->hwaccel_picture_private);
     ff_thread_release_ext_buffer(s->avctx, &f->tf);
 }
 
@@ -139,12 +137,8 @@ static int vp8_ref_frame(VP8Context *s, VP8Frame *dst, const VP8Frame *src)
     if ((ret = ff_thread_ref_frame(&dst->tf, &src->tf)) < 0)
         return ret;
     ff_refstruct_replace(&dst->seg_map, src->seg_map);
-    if (src->hwaccel_picture_private) {
-        dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
-        if (!dst->hwaccel_priv_buf)
-            return AVERROR(ENOMEM);
-        dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
-    }
+    ff_refstruct_replace(&dst->hwaccel_picture_private,
+                          src->hwaccel_picture_private);
 
     return 0;
 }
diff --git a/libavcodec/vp8.h b/libavcodec/vp8.h
index cb752d4498..eb9fa2f166 100644
--- a/libavcodec/vp8.h
+++ b/libavcodec/vp8.h
@@ -28,7 +28,6 @@
 
 #include <stdatomic.h>
 
-#include "libavutil/buffer.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 
@@ -154,8 +153,7 @@ typedef struct VP8Frame {
     ThreadFrame tf;
     uint8_t *seg_map; ///< RefStruct reference
 
-    AVBufferRef *hwaccel_priv_buf;
-    void *hwaccel_picture_private;
+    void *hwaccel_picture_private; ///< RefStruct reference
 } VP8Frame;
 
 #define MAX_THREADS 8
diff --git a/libavcodec/vp9.c b/libavcodec/vp9.c
index 3cc27aa812..c9cc81ec94 100644
--- a/libavcodec/vp9.c
+++ b/libavcodec/vp9.c
@@ -30,6 +30,7 @@
 #include "hwaccel_internal.h"
 #include "hwconfig.h"
 #include "profiles.h"
+#include "refstruct.h"
 #include "thread.h"
 #include "threadframe.h"
 #include "pthread_internal.h"
@@ -100,9 +101,8 @@ static void vp9_frame_unref(AVCodecContext *avctx, VP9Frame *f)
 {
     ff_thread_release_ext_buffer(avctx, &f->tf);
     av_buffer_unref(&f->extradata);
-    av_buffer_unref(&f->hwaccel_priv_buf);
+    ff_refstruct_unref(&f->hwaccel_picture_private);
     f->segmentation_map = NULL;
-    f->hwaccel_picture_private = NULL;
 }
 
 static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f)
@@ -135,8 +135,7 @@ static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f)
     f->segmentation_map = f->extradata->data;
     f->mv = (VP9mvrefPair *) (f->extradata->data + sz);
 
-    ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private,
-                                      &f->hwaccel_priv_buf);
+    ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private);
     if (ret < 0)
         goto fail;
 
@@ -163,12 +162,8 @@ static int vp9_frame_ref(AVCodecContext *avctx, VP9Frame *dst, VP9Frame *src)
     dst->mv = src->mv;
     dst->uses_2pass = src->uses_2pass;
 
-    if (src->hwaccel_picture_private) {
-        dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
-        if (!dst->hwaccel_priv_buf)
-            goto fail;
-        dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
-    }
+    ff_refstruct_replace(&dst->hwaccel_picture_private,
+                          src->hwaccel_picture_private);
 
     return 0;
 
diff --git a/libavcodec/vp9shared.h b/libavcodec/vp9shared.h
index 543a496df8..e54f23544e 100644
--- a/libavcodec/vp9shared.h
+++ b/libavcodec/vp9shared.h
@@ -69,8 +69,7 @@ typedef struct VP9Frame {
     VP9mvrefPair *mv;
     int uses_2pass;
 
-    AVBufferRef *hwaccel_priv_buf;
-    void *hwaccel_picture_private;
+    void *hwaccel_picture_private; ///< RefStruct reference
 } VP9Frame;
 
 enum BlockLevel {
diff --git a/libavcodec/vulkan_av1.c b/libavcodec/vulkan_av1.c
index b1373722ef..7c8dda7798 100644
--- a/libavcodec/vulkan_av1.c
+++ b/libavcodec/vulkan_av1.c
@@ -536,10 +536,10 @@ static int vk_av1_end_frame(AVCodecContext *avctx)
     return ff_vk_decode_frame(avctx, pic->f, vp, rav, rvp);
 }
 
-static void vk_av1_free_frame_priv(void *_hwctx, uint8_t *data)
+static void vk_av1_free_frame_priv(FFRefStructOpaque _hwctx, void *data)
 {
-    AVHWDeviceContext *hwctx = _hwctx;
-    AV1VulkanDecodePicture *ap = (AV1VulkanDecodePicture *)data;
+    AVHWDeviceContext *hwctx = _hwctx.nc;
+    AV1VulkanDecodePicture *ap = data;
 
     /* Workaround for a spec issue. */
     if (ap->frame_id_set)
@@ -547,9 +547,6 @@ static void vk_av1_free_frame_priv(void *_hwctx, uint8_t *data)
 
     /* Free frame resources, this also destroys the session parameters. */
     ff_vk_decode_free_frame(hwctx, &ap->vp);
-
-    /* Free frame context */
-    av_free(ap);
 }
 
 const FFHWAccel ff_av1_vulkan_hwaccel = {
diff --git a/libavcodec/vulkan_h264.c b/libavcodec/vulkan_h264.c
index cdc2c7fe30..2cb54bb1ff 100644
--- a/libavcodec/vulkan_h264.c
+++ b/libavcodec/vulkan_h264.c
@@ -530,16 +530,13 @@ static int vk_h264_end_frame(AVCodecContext *avctx)
     return ff_vk_decode_frame(avctx, pic->f, vp, rav, rvp);
 }
 
-static void vk_h264_free_frame_priv(void *_hwctx, uint8_t *data)
+static void vk_h264_free_frame_priv(FFRefStructOpaque _hwctx, void *data)
 {
-    AVHWDeviceContext *hwctx = _hwctx;
-    H264VulkanDecodePicture *hp = (H264VulkanDecodePicture *)data;
+    AVHWDeviceContext *hwctx = _hwctx.nc;
+    H264VulkanDecodePicture *hp = data;
 
     /* Free frame resources, this also destroys the session parameters. */
     ff_vk_decode_free_frame(hwctx, &hp->vp);
-
-    /* Free frame context */
-    av_free(hp);
 }
 
 const FFHWAccel ff_h264_vulkan_hwaccel = {
diff --git a/libavcodec/vulkan_hevc.c b/libavcodec/vulkan_hevc.c
index 395cbd3008..d5116bb48c 100644
--- a/libavcodec/vulkan_hevc.c
+++ b/libavcodec/vulkan_hevc.c
@@ -901,16 +901,13 @@ static int vk_hevc_end_frame(AVCodecContext *avctx)
     return ff_vk_decode_frame(avctx, pic->frame, vp, rav, rvp);
 }
 
-static void vk_hevc_free_frame_priv(void *_hwctx, uint8_t *data)
+static void vk_hevc_free_frame_priv(FFRefStructOpaque _hwctx, void *data)
 {
-    AVHWDeviceContext *hwctx = _hwctx;
-    HEVCVulkanDecodePicture *hp = (HEVCVulkanDecodePicture *)data;
+    AVHWDeviceContext *hwctx = _hwctx.nc;
+    HEVCVulkanDecodePicture *hp = data;
 
     /* Free frame resources */
     ff_vk_decode_free_frame(hwctx, &hp->vp);
-
-    /* Free frame context */
-    av_free(hp);
 }
 
 const FFHWAccel ff_hevc_vulkan_hwaccel = {
