James Almer:
> It's been a year since the last bump, so lets get rid of old deprecated API
> in time for ffmpeg 8.0
> 

I want to add the attached patch to that set. There will probably be a
minor conflict with the FF_API_FRAME_KEY patch (your github account
didn't have a branch of this, so I based it on master, not on your
patchset), but that is trivial to fix.

One more thing: Did you check whether you can now remove inclusions of
version*.h and attributes.h (for attribute_deprecated) headers? I don't
see you do this and it is often forgotten.

- Andreas
From 3a89a0be20d21688f4134cd80eae3bd71cfcd996 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinha...@outlook.com>
Date: Tue, 25 Feb 2025 12:59:17 +0100
Subject: [PATCH] avutil/frame: Port AVFrame.private_ref to RefStruct API

This is possible without deprecation period, because said field
is documented as only for our libav* libraries and not the general
public.

Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com>
---
 libavcodec/decode.c       | 28 +++++++++-------------------
 libavcodec/lcevcdec.c     |  2 +-
 libavcodec/nvdec.c        |  8 ++++----
 libavcodec/nvdec_av1.c    |  2 +-
 libavcodec/nvdec_h264.c   |  4 ++--
 libavcodec/nvdec_hevc.c   |  4 ++--
 libavcodec/nvdec_mjpeg.c  |  2 +-
 libavcodec/nvdec_mpeg12.c |  2 +-
 libavcodec/nvdec_mpeg4.c  |  2 +-
 libavcodec/nvdec_vc1.c    |  2 +-
 libavcodec/nvdec_vp8.c    |  2 +-
 libavcodec/nvdec_vp9.c    |  2 +-
 libavcodec/videotoolbox.c |  2 +-
 libavutil/frame.c         | 12 ++++--------
 libavutil/frame.h         | 10 +++-------
 15 files changed, 33 insertions(+), 51 deletions(-)

diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index cac7e620d2..c822bd55f5 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -703,11 +703,11 @@ FF_ENABLE_DEPRECATION_WARNINGS
 
         /* the only case where decode data is not set should be decoders
          * that do not call ff_get_buffer() */
-        av_assert0((frame->private_ref && frame->private_ref->size == sizeof(FrameDecodeData)) ||
+        av_assert0(frame->private_ref ||
                    !(avctx->codec->capabilities & AV_CODEC_CAP_DR1));
 
         if (frame->private_ref) {
-            FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
+            FrameDecodeData *fdd = frame->private_ref;
 
             if (fdd->post_process) {
                 ret = fdd->post_process(avctx, frame);
@@ -720,7 +720,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
     }
 
     /* free the per-frame decode data */
-    av_buffer_unref(&frame->private_ref);
+    av_refstruct_unref(&frame->private_ref);
 
     return ret;
 }
@@ -1623,39 +1623,29 @@ static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame)
     }
 }
 
-static void decode_data_free(void *opaque, uint8_t *data)
+static void decode_data_free(AVRefStructOpaque unused, void *obj)
 {
-    FrameDecodeData *fdd = (FrameDecodeData*)data;
+    FrameDecodeData *fdd = obj;
 
     if (fdd->post_process_opaque_free)
         fdd->post_process_opaque_free(fdd->post_process_opaque);
 
     if (fdd->hwaccel_priv_free)
         fdd->hwaccel_priv_free(fdd->hwaccel_priv);
-
-    av_freep(&fdd);
 }
 
 int ff_attach_decode_data(AVFrame *frame)
 {
-    AVBufferRef *fdd_buf;
     FrameDecodeData *fdd;
 
     av_assert1(!frame->private_ref);
-    av_buffer_unref(&frame->private_ref);
+    av_refstruct_unref(&frame->private_ref);
 
-    fdd = av_mallocz(sizeof(*fdd));
+    fdd = av_refstruct_alloc_ext(sizeof(*fdd), 0, NULL, decode_data_free);
     if (!fdd)
         return AVERROR(ENOMEM);
 
-    fdd_buf = av_buffer_create((uint8_t*)fdd, sizeof(*fdd), decode_data_free,
-                               NULL, AV_BUFFER_FLAG_READONLY);
-    if (!fdd_buf) {
-        av_freep(&fdd);
-        return AVERROR(ENOMEM);
-    }
-
-    frame->private_ref = fdd_buf;
+    frame->private_ref = fdd;
 
     return 0;
 }
@@ -1682,7 +1672,7 @@ static void attach_post_process_data(AVCodecContext *avctx, AVFrame *frame)
     DecodeContext        *dc = decode_ctx(avci);
 
     if (dc->lcevc_frame) {
-        FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
+        FrameDecodeData *fdd = frame->private_ref;
 
         fdd->post_process_opaque = av_refstruct_ref(dc->lcevc);
         fdd->post_process_opaque_free = ff_lcevc_unref;
diff --git a/libavcodec/lcevcdec.c b/libavcodec/lcevcdec.c
index 4ca5dfa0a6..2fe06b8800 100644
--- a/libavcodec/lcevcdec.c
+++ b/libavcodec/lcevcdec.c
@@ -278,7 +278,7 @@ static int lcevc_init(FFLCEVCContext *lcevc, void *logctx)
 
 int ff_lcevc_process(void *logctx, AVFrame *frame)
 {
-    FrameDecodeData  *fdd = (FrameDecodeData*)frame->private_ref->data;
+    FrameDecodeData  *fdd = frame->private_ref;
     FFLCEVCContext *lcevc = fdd->post_process_opaque;
     int ret;
 
diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c
index 99351661ea..d444135fd7 100644
--- a/libavcodec/nvdec.c
+++ b/libavcodec/nvdec.c
@@ -494,7 +494,7 @@ finish:
 
 static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
 {
-    FrameDecodeData  *fdd = (FrameDecodeData*)frame->private_ref->data;
+    FrameDecodeData  *fdd = frame->private_ref;
     NVDECFrame        *cf = (NVDECFrame*)fdd->hwaccel_priv;
     NVDECDecoder *decoder = cf->decoder;
 
@@ -575,7 +575,7 @@ finish:
 int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
 {
     NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
-    FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
+    FrameDecodeData *fdd = frame->private_ref;
     NVDECFrame *cf = NULL;
     int ret;
 
@@ -613,7 +613,7 @@ fail:
 int ff_nvdec_start_frame_sep_ref(AVCodecContext *avctx, AVFrame *frame, int has_sep_ref)
 {
     NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
-    FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
+    FrameDecodeData *fdd = frame->private_ref;
     NVDECFrame *cf;
     int ret;
 
@@ -780,7 +780,7 @@ int ff_nvdec_get_ref_idx(AVFrame *frame)
     if (!frame || !frame->private_ref)
         return -1;
 
-    fdd = (FrameDecodeData*)frame->private_ref->data;
+    fdd = frame->private_ref;
     cf  = (NVDECFrame*)fdd->hwaccel_priv;
     if (!cf)
         return -1;
diff --git a/libavcodec/nvdec_av1.c b/libavcodec/nvdec_av1.c
index 6b408edb87..74fcdb6b11 100644
--- a/libavcodec/nvdec_av1.c
+++ b/libavcodec/nvdec_av1.c
@@ -62,7 +62,7 @@ static int nvdec_av1_start_frame(AVCodecContext *avctx, const uint8_t *buffer, u
     if (ret < 0)
         return ret;
 
-    fdd = (FrameDecodeData*)cur_frame->private_ref->data;
+    fdd = cur_frame->private_ref;
     cf  = (NVDECFrame*)fdd->hwaccel_priv;
 
     *pp = (CUVIDPICPARAMS) {
diff --git a/libavcodec/nvdec_h264.c b/libavcodec/nvdec_h264.c
index 1ae3dfd032..51813e1abe 100644
--- a/libavcodec/nvdec_h264.c
+++ b/libavcodec/nvdec_h264.c
@@ -34,7 +34,7 @@
 static void dpb_add(const H264Context *h, CUVIDH264DPBENTRY *dst, const H264Picture *src,
                     int frame_idx)
 {
-    FrameDecodeData *fdd = (FrameDecodeData*)src->f->private_ref->data;
+    FrameDecodeData *fdd = src->f->private_ref;
     const NVDECFrame *cf = fdd->hwaccel_priv;
 
     dst->PicIdx             = cf ? cf->idx : -1;
@@ -65,7 +65,7 @@ static int nvdec_h264_start_frame(AVCodecContext *avctx,
     if (ret < 0)
         return ret;
 
-    fdd = (FrameDecodeData*)h->cur_pic_ptr->f->private_ref->data;
+    fdd = h->cur_pic_ptr->f->private_ref;
     cf  = (NVDECFrame*)fdd->hwaccel_priv;
 
     *pp = (CUVIDPICPARAMS) {
diff --git a/libavcodec/nvdec_hevc.c b/libavcodec/nvdec_hevc.c
index e01ce4c782..f071480b26 100644
--- a/libavcodec/nvdec_hevc.c
+++ b/libavcodec/nvdec_hevc.c
@@ -34,7 +34,7 @@
 
 static void dpb_add(CUVIDHEVCPICPARAMS *pp, int idx, const HEVCFrame *src)
 {
-    FrameDecodeData *fdd = (FrameDecodeData*)src->f->private_ref->data;
+    FrameDecodeData *fdd = src->f->private_ref;
     const NVDECFrame *cf = fdd->hwaccel_priv;
 
     pp->RefPicIdx[idx]      = cf ? cf->idx : -1;
@@ -89,7 +89,7 @@ static int nvdec_hevc_start_frame(AVCodecContext *avctx,
     if (ret < 0)
         return ret;
 
-    fdd = (FrameDecodeData*)s->cur_frame->f->private_ref->data;
+    fdd = s->cur_frame->f->private_ref;
     cf  = (NVDECFrame*)fdd->hwaccel_priv;
 
     *pp = (CUVIDPICPARAMS) {
diff --git a/libavcodec/nvdec_mjpeg.c b/libavcodec/nvdec_mjpeg.c
index 850634bf1a..b48eb73cfa 100644
--- a/libavcodec/nvdec_mjpeg.c
+++ b/libavcodec/nvdec_mjpeg.c
@@ -43,7 +43,7 @@ static int nvdec_mjpeg_start_frame(AVCodecContext *avctx, const uint8_t *buffer,
     if (ret < 0)
         return ret;
 
-    fdd = (FrameDecodeData*)cur_frame->private_ref->data;
+    fdd = cur_frame->private_ref;
     cf  = (NVDECFrame*)fdd->hwaccel_priv;
 
     *pp = (CUVIDPICPARAMS) {
diff --git a/libavcodec/nvdec_mpeg12.c b/libavcodec/nvdec_mpeg12.c
index 99b2b14f1f..2e9c413b1d 100644
--- a/libavcodec/nvdec_mpeg12.c
+++ b/libavcodec/nvdec_mpeg12.c
@@ -47,7 +47,7 @@ static int nvdec_mpeg12_start_frame(AVCodecContext *avctx, const uint8_t *buffer
     if (ret < 0)
         return ret;
 
-    fdd = (FrameDecodeData*)cur_frame->private_ref->data;
+    fdd = cur_frame->private_ref;
     cf  = (NVDECFrame*)fdd->hwaccel_priv;
 
     *pp = (CUVIDPICPARAMS) {
diff --git a/libavcodec/nvdec_mpeg4.c b/libavcodec/nvdec_mpeg4.c
index 80da11b5b1..21ee123996 100644
--- a/libavcodec/nvdec_mpeg4.c
+++ b/libavcodec/nvdec_mpeg4.c
@@ -46,7 +46,7 @@ static int nvdec_mpeg4_start_frame(AVCodecContext *avctx, const uint8_t *buffer,
     if (ret < 0)
         return ret;
 
-    fdd = (FrameDecodeData*)cur_frame->private_ref->data;
+    fdd = cur_frame->private_ref;
     cf  = (NVDECFrame*)fdd->hwaccel_priv;
 
     *pp = (CUVIDPICPARAMS) {
diff --git a/libavcodec/nvdec_vc1.c b/libavcodec/nvdec_vc1.c
index 0668863cb4..671c1ed5eb 100644
--- a/libavcodec/nvdec_vc1.c
+++ b/libavcodec/nvdec_vc1.c
@@ -46,7 +46,7 @@ static int nvdec_vc1_start_frame(AVCodecContext *avctx, const uint8_t *buffer, u
     if (ret < 0)
         return ret;
 
-    fdd = (FrameDecodeData*)cur_frame->private_ref->data;
+    fdd = cur_frame->private_ref;
     cf  = (NVDECFrame*)fdd->hwaccel_priv;
 
     *pp = (CUVIDPICPARAMS) {
diff --git a/libavcodec/nvdec_vp8.c b/libavcodec/nvdec_vp8.c
index ff3b3f259c..b052a8ad87 100644
--- a/libavcodec/nvdec_vp8.c
+++ b/libavcodec/nvdec_vp8.c
@@ -48,7 +48,7 @@ static int nvdec_vp8_start_frame(AVCodecContext *avctx, const uint8_t *buffer, u
     if (ret < 0)
         return ret;
 
-    fdd = (FrameDecodeData*)cur_frame->private_ref->data;
+    fdd = cur_frame->private_ref;
     cf  = (NVDECFrame*)fdd->hwaccel_priv;
 
     *pp = (CUVIDPICPARAMS) {
diff --git a/libavcodec/nvdec_vp9.c b/libavcodec/nvdec_vp9.c
index e196391c6d..21455a8fa2 100644
--- a/libavcodec/nvdec_vp9.c
+++ b/libavcodec/nvdec_vp9.c
@@ -47,7 +47,7 @@ static int nvdec_vp9_start_frame(AVCodecContext *avctx, const uint8_t *buffer, u
     if (ret < 0)
         return ret;
 
-    fdd = (FrameDecodeData*)cur_frame->private_ref->data;
+    fdd = cur_frame->private_ref;
     cf  = (NVDECFrame*)fdd->hwaccel_priv;
 
     *pp = (CUVIDPICPARAMS) {
diff --git a/libavcodec/videotoolbox.c b/libavcodec/videotoolbox.c
index a606c29ded..ec1783f7d5 100644
--- a/libavcodec/videotoolbox.c
+++ b/libavcodec/videotoolbox.c
@@ -167,7 +167,7 @@ int ff_videotoolbox_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
     }
     frame->buf[0] = buf;
 
-    fdd = (FrameDecodeData*)frame->private_ref->data;
+    fdd = frame->private_ref;
     fdd->post_process = videotoolbox_postproc_frame;
 
     frame->width  = avctx->width;
diff --git a/libavutil/frame.c b/libavutil/frame.c
index 492b467ebd..271dd37af2 100644
--- a/libavutil/frame.c
+++ b/libavutil/frame.c
@@ -19,12 +19,11 @@
 #include "channel_layout.h"
 #include "avassert.h"
 #include "buffer.h"
-#include "common.h"
-#include "cpu.h"
 #include "dict.h"
 #include "frame.h"
 #include "imgutils.h"
 #include "mem.h"
+#include "refstruct.h"
 #include "samplefmt.h"
 #include "hwcontext.h"
 
@@ -318,8 +317,6 @@ int av_frame_get_buffer(AVFrame *frame, int align)
 
 static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
 {
-    int ret;
-
 #if FF_API_FRAME_KEY
 FF_DISABLE_DEPRECATION_WARNINGS
     dst->key_frame              = src->key_frame;
@@ -393,9 +390,8 @@ FF_ENABLE_DEPRECATION_WARNINGS
         av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
     }
 
-    ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
-    ret |= av_buffer_replace(&dst->private_ref, src->private_ref);
-    return ret;
+    av_refstruct_replace(&dst->private_ref, src->private_ref);
+    return av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
 }
 
 int av_frame_ref(AVFrame *dst, const AVFrame *src)
@@ -637,7 +633,7 @@ void av_frame_unref(AVFrame *frame)
     av_buffer_unref(&frame->hw_frames_ctx);
 
     av_buffer_unref(&frame->opaque_ref);
-    av_buffer_unref(&frame->private_ref);
+    av_refstruct_unref(&frame->private_ref);
 
     if (frame->extended_data != frame->data)
         av_freep(&frame->extended_data);
diff --git a/libavutil/frame.h b/libavutil/frame.h
index 49260ae2dd..33a808ac9c 100644
--- a/libavutil/frame.h
+++ b/libavutil/frame.h
@@ -801,17 +801,13 @@ typedef struct AVFrame {
      */
 
     /**
-     * AVBufferRef for internal use by a single libav* library.
+     * RefStruct reference for internal use by a single libav* library.
      * Must not be used to transfer data between libraries.
      * Has to be NULL when ownership of the frame leaves the respective library.
      *
-     * Code outside the FFmpeg libs should never check or change the contents of the buffer ref.
-     *
-     * FFmpeg calls av_buffer_unref() on it when the frame is unreferenced.
-     * av_frame_copy_props() calls create a new reference with av_buffer_ref()
-     * for the target frame's private_ref field.
+     * Code outside the FFmpeg libs must never check or change private_ref.
      */
-    AVBufferRef *private_ref;
+    void *private_ref;
 
     /**
      * Channel layout of the audio data.
-- 
2.45.2

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

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

Reply via email to