[FFmpeg-devel] [PATCH 1/5] lavc/refstruct: move to lavu and make public

Anton Khirnov anton at khirnov.net
Wed Dec 11 16:05:03 EET 2024


It is highly versatile and generally useful.
---
 doc/APIchanges                        |   4 +
 libavcodec/Makefile                   |   1 -
 libavcodec/av1dec.c                   |  28 +++---
 libavcodec/avcodec.c                  |   6 +-
 libavcodec/bsf/dts2pts.c              |  18 ++--
 libavcodec/cbs.c                      |  22 ++---
 libavcodec/cbs_av1.c                  |  18 ++--
 libavcodec/cbs_h2645.c                |  48 +++++-----
 libavcodec/cbs_h266_syntax_template.c |   2 +-
 libavcodec/cbs_internal.h             |   4 +-
 libavcodec/cbs_sei.c                  |  24 ++---
 libavcodec/container_fifo.c           |  22 ++---
 libavcodec/decode.c                   |  32 +++----
 libavcodec/dovi_rpu.c                 |  20 ++---
 libavcodec/dovi_rpudec.c              |  10 +--
 libavcodec/dovi_rpuenc.c              |  12 +--
 libavcodec/ffv1.c                     |  10 +--
 libavcodec/ffv1dec.c                  |  12 +--
 libavcodec/get_buffer.c               |  10 +--
 libavcodec/h2645_sei.c                |  10 +--
 libavcodec/h264_parser.c              |   4 +-
 libavcodec/h264_picture.c             |  30 +++----
 libavcodec/h264_ps.c                  |  28 +++---
 libavcodec/h264_slice.c               |  38 ++++----
 libavcodec/h264dec.c                  |  14 +--
 libavcodec/h264dec.h                  |  10 +--
 libavcodec/hevc/hevcdec.c             |  46 +++++-----
 libavcodec/hevc/hevcdec.h             |   4 +-
 libavcodec/hevc/ps.c                  |  44 ++++-----
 libavcodec/hevc/refs.c                |  20 ++---
 libavcodec/hwaccel_internal.h         |   4 +-
 libavcodec/internal.h                 |   2 +-
 libavcodec/lcevcdec.c                 |   8 +-
 libavcodec/lcevcdec.h                 |   1 -
 libavcodec/mpegpicture.c              |  34 +++----
 libavcodec/mpegpicture.h              |  12 +--
 libavcodec/mpegvideo.c                |  18 ++--
 libavcodec/mpegvideo.h                |   2 +-
 libavcodec/mpegvideo_dec.c            |   6 +-
 libavcodec/mpegvideo_enc.c            |  18 ++--
 libavcodec/nvdec.c                    |  42 ++++-----
 libavcodec/nvdec.h                    |   2 +-
 libavcodec/pthread_frame.c            |  14 +--
 libavcodec/qsv.c                      |  14 +--
 libavcodec/qsvdec.c                   |   4 +-
 libavcodec/qsvenc.c                   |   4 +-
 libavcodec/rkmppdec.c                 |  12 +--
 libavcodec/utils.c                    |   6 +-
 libavcodec/v4l2_buffers.c             |   6 +-
 libavcodec/v4l2_m2m.c                 |  10 +--
 libavcodec/vaapi_encode.c             |  24 ++---
 libavcodec/vaapi_encode.h             |   2 +-
 libavcodec/vp3.c                      |  10 +--
 libavcodec/vp8.c                      |  14 +--
 libavcodec/vp9.c                      |  22 ++---
 libavcodec/vp9dec.h                   |   2 +-
 libavcodec/vulkan_av1.c               |   2 +-
 libavcodec/vulkan_decode.c            |  14 +--
 libavcodec/vulkan_h264.c              |   2 +-
 libavcodec/vulkan_hevc.c              |   2 +-
 libavcodec/vvc/ctu.c                  |  10 +--
 libavcodec/vvc/dec.c                  |  44 ++++-----
 libavcodec/vvc/dec.h                  |   8 +-
 libavcodec/vvc/ps.c                   |  78 ++++++++--------
 libavcodec/vvc/refs.c                 |  32 +++----
 libavcodec/wavpack.c                  |  32 +++----
 libavutil/Makefile                    |   2 +
 {libavcodec => libavutil}/refstruct.c | 112 +++++++++++------------
 {libavcodec => libavutil}/refstruct.h | 124 +++++++++++++-------------
 libavutil/version.h                   |   2 +-
 70 files changed, 657 insertions(+), 651 deletions(-)
 rename {libavcodec => libavutil}/refstruct.c (74%)
 rename {libavcodec => libavutil}/refstruct.h (71%)

diff --git a/doc/APIchanges b/doc/APIchanges
index b9af3de933..13789fcea4 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,10 @@ The last version increases of all libraries were on 2024-03-07
 
 API changes, most recent first:
 
+2024-12-xx - xxxxxxxxxx - lavu 59.50.100 - refstruct.h
+  Add a new public header refstruct.h with new API for
+  reference-counted objects.
+
 2024-12-05 - xxxxxxxxxx - lavu 59.49.100 - csp.h
   Add av_csp_itu_eotf() and av_csp_itu_eotf_inv().
 
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 9a08a8441f..cd64013998 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -57,7 +57,6 @@ OBJS = ac3_parser.o                                                     \
        profiles.o                                                       \
        qsv_api.o                                                        \
        raw.o                                                            \
-       refstruct.o                                                      \
        threadprogress.o                                                 \
        utils.o                                                          \
        version.o                                                        \
diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c
index bc4ef63e68..17651e640f 100644
--- a/libavcodec/av1dec.c
+++ b/libavcodec/av1dec.c
@@ -39,7 +39,7 @@
 #include "hwconfig.h"
 #include "profiles.h"
 #include "progressframe.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 /**< same with Div_Lut defined in spec 7.11.3.7 */
 static const uint16_t div_lut[AV1_DIV_LUT_NUM] = {
@@ -687,8 +687,8 @@ static int get_pixel_format(AVCodecContext *avctx)
 static void av1_frame_unref(AV1Frame *f)
 {
     ff_progress_frame_unref(&f->pf);
-    ff_refstruct_unref(&f->hwaccel_picture_private);
-    ff_refstruct_unref(&f->header_ref);
+    av_refstruct_unref(&f->hwaccel_picture_private);
+    av_refstruct_unref(&f->header_ref);
     f->raw_frame_header = NULL;
     f->spatial_id = f->temporal_id = 0;
     memset(f->skip_mode_frame_idx, 0,
@@ -701,13 +701,13 @@ static void av1_frame_replace(AV1Frame *dst, const AV1Frame *src)
 {
     av_assert1(dst != src);
 
-    ff_refstruct_replace(&dst->header_ref, src->header_ref);
+    av_refstruct_replace(&dst->header_ref, src->header_ref);
 
     dst->raw_frame_header = src->raw_frame_header;
 
     ff_progress_frame_replace(&dst->pf, &src->pf);
 
-    ff_refstruct_replace(&dst->hwaccel_picture_private,
+    av_refstruct_replace(&dst->hwaccel_picture_private,
                           src->hwaccel_picture_private);
 
     dst->spatial_id = src->spatial_id;
@@ -747,10 +747,10 @@ static av_cold int av1_decode_free(AVCodecContext *avctx)
         av1_frame_unref(&s->ref[i]);
     av1_frame_unref(&s->cur_frame);
     av_buffer_unref(&s->seq_data_ref);
-    ff_refstruct_unref(&s->seq_ref);
-    ff_refstruct_unref(&s->header_ref);
-    ff_refstruct_unref(&s->cll_ref);
-    ff_refstruct_unref(&s->mdcv_ref);
+    av_refstruct_unref(&s->seq_ref);
+    av_refstruct_unref(&s->header_ref);
+    av_refstruct_unref(&s->cll_ref);
+    av_refstruct_unref(&s->mdcv_ref);
     av_freep(&s->tile_group_info);
 
     while (s->itut_t35_fifo && av_fifo_read(s->itut_t35_fifo, &itut_t35, 1) >= 0)
@@ -1218,7 +1218,7 @@ static int get_current_frame(AVCodecContext *avctx)
 
     av1_frame_unref(&s->cur_frame);
 
-    s->cur_frame.header_ref = ff_refstruct_ref(s->header_ref);
+    s->cur_frame.header_ref = av_refstruct_ref(s->header_ref);
 
     s->cur_frame.raw_frame_header = s->raw_frame_header;
 
@@ -1306,7 +1306,7 @@ static int av1_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
 
             s->seq_data_ref->data = unit->data;
             s->seq_data_ref->size = unit->data_size;
-            ff_refstruct_replace(&s->seq_ref, unit->content_ref);
+            av_refstruct_replace(&s->seq_ref, unit->content_ref);
 
             s->raw_seq = &obu->obu.sequence_header;
 
@@ -1334,7 +1334,7 @@ static int av1_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
                 goto end;
             }
 
-            ff_refstruct_replace(&s->header_ref, unit->content_ref);
+            av_refstruct_replace(&s->header_ref, unit->content_ref);
 
             if (unit->type == AV1_OBU_FRAME)
                 s->raw_frame_header = &obu->obu.frame.header;
@@ -1414,11 +1414,11 @@ static int av1_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
         case AV1_OBU_METADATA:
             switch (obu->obu.metadata.metadata_type) {
             case AV1_METADATA_TYPE_HDR_CLL:
-                ff_refstruct_replace(&s->cll_ref, unit->content_ref);
+                av_refstruct_replace(&s->cll_ref, unit->content_ref);
                 s->cll = &obu->obu.metadata.metadata.hdr_cll;
                 break;
             case AV1_METADATA_TYPE_HDR_MDCV:
-                ff_refstruct_replace(&s->mdcv_ref, unit->content_ref);
+                av_refstruct_replace(&s->mdcv_ref, unit->content_ref);
                 s->mdcv = &obu->obu.metadata.metadata.hdr_mdcv;
                 break;
             case AV1_METADATA_TYPE_ITUT_T35: {
diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index 1fa8704c9d..f91549ddba 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -45,7 +45,7 @@
 #include "frame_thread_encoder.h"
 #include "hwconfig.h"
 #include "internal.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "thread.h"
 
 /**
@@ -453,8 +453,8 @@ av_cold void ff_codec_close(AVCodecContext *avctx)
         av_frame_free(&avci->in_frame);
         av_frame_free(&avci->recon_frame);
 
-        ff_refstruct_unref(&avci->pool);
-        ff_refstruct_pool_uninit(&avci->progress_frame_pool);
+        av_refstruct_unref(&avci->pool);
+        av_refstruct_pool_uninit(&avci->progress_frame_pool);
         if (av_codec_is_decoder(avctx->codec))
             ff_decode_internal_uninit(avctx);
 
diff --git a/libavcodec/bsf/dts2pts.c b/libavcodec/bsf/dts2pts.c
index 62838d4f7b..9d31d7dc08 100644
--- a/libavcodec/bsf/dts2pts.c
+++ b/libavcodec/bsf/dts2pts.c
@@ -34,7 +34,7 @@
 #include "cbs_h264.h"
 #include "h264_parse.h"
 #include "h264_ps.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 typedef struct DTS2PTSNode {
     int64_t      dts;
@@ -62,7 +62,7 @@ typedef struct DTS2PTSH264Context {
 typedef struct DTS2PTSContext {
     struct AVTreeNode *root;
     AVFifo *fifo;
-    FFRefStructPool *node_pool;
+    AVRefStructPool *node_pool;
 
     // Codec specific function pointers and constants
     int (*init)(AVBSFContext *ctx);
@@ -112,7 +112,7 @@ static int dec_poc(void *opaque, void *elem)
 static int free_node(void *opaque, void *elem)
 {
     DTS2PTSNode *node = elem;
-    ff_refstruct_unref(&node);
+    av_refstruct_unref(&node);
     return 0;
 }
 
@@ -126,7 +126,7 @@ static int alloc_and_insert_node(AVBSFContext *ctx, int64_t ts, int64_t duration
         DTS2PTSNode *poc_node, *ret;
         if (!node)
             return AVERROR(ENOMEM);
-        poc_node = ff_refstruct_pool_get(s->node_pool);
+        poc_node = av_refstruct_pool_get(s->node_pool);
         if (!poc_node) {
             av_free(node);
             return AVERROR(ENOMEM);
@@ -137,7 +137,7 @@ static int alloc_and_insert_node(AVBSFContext *ctx, int64_t ts, int64_t duration
         ret = av_tree_insert(&s->root, poc_node, cmp_insert, &node);
         if (ret && ret != poc_node) {
             *ret = *poc_node;
-            ff_refstruct_unref(&poc_node);
+            av_refstruct_unref(&poc_node);
             av_free(node);
         }
     }
@@ -396,8 +396,8 @@ static int dts2pts_init(AVBSFContext *ctx)
     if (!s->fifo)
         return AVERROR(ENOMEM);
 
-    s->node_pool = ff_refstruct_pool_alloc(sizeof(DTS2PTSNode),
-                                           FF_REFSTRUCT_POOL_FLAG_NO_ZEROING);
+    s->node_pool = av_refstruct_pool_alloc(sizeof(DTS2PTSNode),
+                                           AV_REFSTRUCT_POOL_FLAG_NO_ZEROING);
 
     if (!s->node_pool)
         return AVERROR(ENOMEM);
@@ -467,7 +467,7 @@ static int dts2pts_filter(AVBSFContext *ctx, AVPacket *out)
                 if (!poc_node || poc_node->dts != out->pts)
                     continue;
                 av_tree_insert(&s->root, poc_node, cmp_insert, &node);
-                ff_refstruct_unref(&poc_node);
+                av_refstruct_unref(&poc_node);
                 av_free(node);
                 poc_node = av_tree_find(s->root, &dup, cmp_find, NULL);
             }
@@ -529,7 +529,7 @@ static void dts2pts_close(AVBSFContext *ctx)
     dts2pts_flush(ctx);
 
     av_fifo_freep2(&s->fifo);
-    ff_refstruct_pool_uninit(&s->node_pool);
+    av_refstruct_pool_uninit(&s->node_pool);
     ff_cbs_fragment_free(&s->au);
     ff_cbs_close(&s->cbc);
 }
diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
index b26e39eab4..01dd916d81 100644
--- a/libavcodec/cbs.c
+++ b/libavcodec/cbs.c
@@ -29,7 +29,7 @@
 #include "avcodec.h"
 #include "cbs.h"
 #include "cbs_internal.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 
 static const CodedBitstreamType *const cbs_type_table[] = {
@@ -160,7 +160,7 @@ av_cold void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
 
 static void cbs_unit_uninit(CodedBitstreamUnit *unit)
 {
-    ff_refstruct_unref(&unit->content_ref);
+    av_refstruct_unref(&unit->content_ref);
     unit->content = NULL;
 
     av_buffer_unref(&unit->data_ref);
@@ -208,7 +208,7 @@ static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
                 continue;
         }
 
-        ff_refstruct_unref(&unit->content_ref);
+        av_refstruct_unref(&unit->content_ref);
         unit->content = NULL;
 
         av_assert0(unit->data && unit->data_ref);
@@ -222,7 +222,7 @@ static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
             av_log(ctx->log_ctx, AV_LOG_VERBOSE,
                    "Skipping decomposition of unit %d "
                    "(type %"PRIu32").\n", i, unit->type);
-            ff_refstruct_unref(&unit->content_ref);
+            av_refstruct_unref(&unit->content_ref);
             unit->content = NULL;
         } else if (err < 0) {
             av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
@@ -799,7 +799,7 @@ int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag,
 
     if (content_ref) {
         // Create our own reference out of the user-supplied one.
-        content_ref = ff_refstruct_ref(content_ref);
+        content_ref = av_refstruct_ref(content_ref);
     }
 
     unit = &frag->units[position];
@@ -873,7 +873,7 @@ void ff_cbs_delete_unit(CodedBitstreamFragment *frag,
                 (frag->nb_units - position) * sizeof(*frag->units));
 }
 
-static void cbs_default_free_unit_content(FFRefStructOpaque opaque, void *content)
+static void cbs_default_free_unit_content(AVRefStructOpaque opaque, void *content)
 {
     const CodedBitstreamUnitTypeDescriptor *desc = opaque.c;
 
@@ -913,8 +913,8 @@ static const CodedBitstreamUnitTypeDescriptor
 
 static void *cbs_alloc_content(const CodedBitstreamUnitTypeDescriptor *desc)
 {
-    return ff_refstruct_alloc_ext_c(desc->content_size, 0,
-                                    (FFRefStructOpaque){ .c = desc },
+    return av_refstruct_alloc_ext_c(desc->content_size, 0,
+                                    (AVRefStructOpaque){ .c = desc },
                                     desc->content_type == CBS_CONTENT_TYPE_COMPLEX
                                             ? desc->type.complex.content_free
                                             : cbs_default_free_unit_content);
@@ -989,7 +989,7 @@ static int cbs_clone_noncomplex_unit_content(void **clonep,
     return 0;
 
 fail:
-    ff_refstruct_unref(&copy);
+    av_refstruct_unref(&copy);
     return err;
 }
 
@@ -1048,13 +1048,13 @@ int ff_cbs_make_unit_writable(CodedBitstreamContext *ctx,
     int err;
 
     av_assert0(unit->content);
-    if (ref && ff_refstruct_exclusive(ref))
+    if (ref && av_refstruct_exclusive(ref))
         return 0;
 
     err = cbs_clone_unit_content(ctx, unit);
     if (err < 0)
         return err;
-    ff_refstruct_unref(&ref);
+    av_refstruct_unref(&ref);
     return 0;
 }
 
diff --git a/libavcodec/cbs_av1.c b/libavcodec/cbs_av1.c
index 458381f038..be086b81cb 100644
--- a/libavcodec/cbs_av1.c
+++ b/libavcodec/cbs_av1.c
@@ -24,7 +24,7 @@
 #include "cbs_internal.h"
 #include "cbs_av1.h"
 #include "defs.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 
 static int cbs_av1_read_uvlc(CodedBitstreamContext *ctx, GetBitContext *gbc,
@@ -878,7 +878,7 @@ static int cbs_av1_read_unit(CodedBitstreamContext *ctx,
                 priv->operating_point_idc = sequence_header->operating_point_idc[priv->operating_point];
             }
 
-            ff_refstruct_replace(&priv->sequence_header_ref, unit->content_ref);
+            av_refstruct_replace(&priv->sequence_header_ref, unit->content_ref);
             priv->sequence_header = &obu->obu.sequence_header;
         }
         break;
@@ -997,7 +997,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
     av1ctx = *priv;
 
     if (priv->sequence_header_ref) {
-        av1ctx.sequence_header_ref = ff_refstruct_ref(priv->sequence_header_ref);
+        av1ctx.sequence_header_ref = av_refstruct_ref(priv->sequence_header_ref);
     }
 
     if (priv->frame_header_ref) {
@@ -1035,14 +1035,14 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
             if (err < 0)
                 goto error;
 
-            ff_refstruct_unref(&priv->sequence_header_ref);
+            av_refstruct_unref(&priv->sequence_header_ref);
             priv->sequence_header = NULL;
 
             err = ff_cbs_make_unit_refcounted(ctx, unit);
             if (err < 0)
                 goto error;
 
-            priv->sequence_header_ref = ff_refstruct_ref(unit->content_ref);
+            priv->sequence_header_ref = av_refstruct_ref(unit->content_ref);
             priv->sequence_header = &obu->obu.sequence_header;
         }
         break;
@@ -1146,7 +1146,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
     av_assert0(data_pos <= start_pos);
 
     if (8 * obu->obu_size > put_bits_left(pbc)) {
-        ff_refstruct_unref(&priv->sequence_header_ref);
+        av_refstruct_unref(&priv->sequence_header_ref);
         av_buffer_unref(&priv->frame_header_ref);
         *priv = av1ctx;
 
@@ -1175,7 +1175,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
     err = 0;
 
 error:
-    ff_refstruct_unref(&av1ctx.sequence_header_ref);
+    av_refstruct_unref(&av1ctx.sequence_header_ref);
     av_buffer_unref(&av1ctx.frame_header_ref);
 
     return err;
@@ -1227,11 +1227,11 @@ static void cbs_av1_close(CodedBitstreamContext *ctx)
 {
     CodedBitstreamAV1Context *priv = ctx->priv_data;
 
-    ff_refstruct_unref(&priv->sequence_header_ref);
+    av_refstruct_unref(&priv->sequence_header_ref);
     av_buffer_unref(&priv->frame_header_ref);
 }
 
-static void cbs_av1_free_metadata(FFRefStructOpaque unused, void *content)
+static void cbs_av1_free_metadata(AVRefStructOpaque unused, void *content)
 {
     AV1RawOBU *obu = content;
     AV1RawMetadata *md;
diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index 2de74691cb..369e3ac876 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -28,7 +28,7 @@
 #include "cbs_h266.h"
 #include "h264.h"
 #include "h2645_parse.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "vvc.h"
 
 #include "hevc/hevc.h"
@@ -345,7 +345,7 @@ static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
 /* The CBS SEI code uses the refstruct API for the allocation
  * of its child buffers. */
 #define allocate(name, size) do { \
-        name  = ff_refstruct_allocz(size + \
+        name  = av_refstruct_allocz(size + \
                                         AV_INPUT_BUFFER_PADDING_SIZE); \
         if (!name) \
             return AVERROR(ENOMEM); \
@@ -769,7 +769,7 @@ static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
     if (priv->ps_var[id] == priv->active_ ## ps_var) \
         priv->active_ ## ps_var = NULL ; \
     av_assert0(unit->content_ref); \
-    ff_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
+    av_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
     return 0; \
 }
 
@@ -790,7 +790,7 @@ static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
     if (err < 0) \
         return err; \
     av_assert0(unit->content_ref); \
-    ff_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
+    av_refstruct_replace(&priv->ps_var[id], unit->content_ref); \
     return 0; \
 }
 
@@ -810,10 +810,10 @@ static int cbs_h266_replace_sps(CodedBitstreamContext *ctx,
     if (priv->sps[id] && memcmp(priv->sps[id], unit->content_ref, sizeof(*priv->sps[id]))) {
         for (unsigned int i = 0; i < VVC_MAX_PPS_COUNT; i++) {
             if (priv->pps[i] && priv->pps[i]->pps_seq_parameter_set_id == id)
-                ff_refstruct_unref(&priv->pps[i]);
+                av_refstruct_unref(&priv->pps[i]);
         }
     }
-    ff_refstruct_replace(&priv->sps[id], unit->content_ref);
+    av_refstruct_replace(&priv->sps[id], unit->content_ref);
     return 0;
 }
 
@@ -828,7 +828,7 @@ static int cbs_h266_replace_ph(CodedBitstreamContext *ctx,
     if (err < 0)
         return err;
     av_assert0(unit->content_ref);
-    ff_refstruct_replace(&h266->ph_ref, unit->content_ref);
+    av_refstruct_replace(&h266->ph_ref, unit->content_ref);
     h266->ph = ph;
     return 0;
 }
@@ -1910,9 +1910,9 @@ static void cbs_h264_flush(CodedBitstreamContext *ctx)
     CodedBitstreamH264Context *h264 = ctx->priv_data;
 
     for (int i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
-        ff_refstruct_unref(&h264->sps[i]);
+        av_refstruct_unref(&h264->sps[i]);
     for (int i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
-        ff_refstruct_unref(&h264->pps[i]);
+        av_refstruct_unref(&h264->pps[i]);
 
     h264->active_sps = NULL;
     h264->active_pps = NULL;
@@ -1927,9 +1927,9 @@ static void cbs_h264_close(CodedBitstreamContext *ctx)
     ff_h2645_packet_uninit(&h264->common.read_packet);
 
     for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
-        ff_refstruct_unref(&h264->sps[i]);
+        av_refstruct_unref(&h264->sps[i]);
     for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
-        ff_refstruct_unref(&h264->pps[i]);
+        av_refstruct_unref(&h264->pps[i]);
 }
 
 static void cbs_h265_flush(CodedBitstreamContext *ctx)
@@ -1937,11 +1937,11 @@ static void cbs_h265_flush(CodedBitstreamContext *ctx)
     CodedBitstreamH265Context *h265 = ctx->priv_data;
 
     for (int i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
-        ff_refstruct_unref(&h265->vps[i]);
+        av_refstruct_unref(&h265->vps[i]);
     for (int i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
-        ff_refstruct_unref(&h265->sps[i]);
+        av_refstruct_unref(&h265->sps[i]);
     for (int i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
-        ff_refstruct_unref(&h265->pps[i]);
+        av_refstruct_unref(&h265->pps[i]);
 
     h265->active_vps = NULL;
     h265->active_sps = NULL;
@@ -1956,11 +1956,11 @@ static void cbs_h265_close(CodedBitstreamContext *ctx)
     ff_h2645_packet_uninit(&h265->common.read_packet);
 
     for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
-        ff_refstruct_unref(&h265->vps[i]);
+        av_refstruct_unref(&h265->vps[i]);
     for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
-        ff_refstruct_unref(&h265->sps[i]);
+        av_refstruct_unref(&h265->sps[i]);
     for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
-        ff_refstruct_unref(&h265->pps[i]);
+        av_refstruct_unref(&h265->pps[i]);
 }
 
 static void cbs_h266_flush(CodedBitstreamContext *ctx)
@@ -1968,12 +1968,12 @@ static void cbs_h266_flush(CodedBitstreamContext *ctx)
     CodedBitstreamH266Context *h266 = ctx->priv_data;
 
     for (int i = 0; i < FF_ARRAY_ELEMS(h266->vps); i++)
-        ff_refstruct_unref(&h266->vps[i]);
+        av_refstruct_unref(&h266->vps[i]);
     for (int i = 0; i < FF_ARRAY_ELEMS(h266->sps); i++)
-        ff_refstruct_unref(&h266->sps[i]);
+        av_refstruct_unref(&h266->sps[i]);
     for (int i = 0; i < FF_ARRAY_ELEMS(h266->pps); i++)
-        ff_refstruct_unref(&h266->pps[i]);
-    ff_refstruct_unref(&h266->ph_ref);
+        av_refstruct_unref(&h266->pps[i]);
+    av_refstruct_unref(&h266->ph_ref);
 }
 
 static void cbs_h266_close(CodedBitstreamContext *ctx)
@@ -1984,7 +1984,7 @@ static void cbs_h266_close(CodedBitstreamContext *ctx)
     ff_h2645_packet_uninit(&h266->common.read_packet);
  }
 
-static void cbs_h264_free_sei(FFRefStructOpaque unused, void *content)
+static void cbs_h264_free_sei(AVRefStructOpaque unused, void *content)
 {
     H264RawSEI *sei = content;
     ff_cbs_sei_free_message_list(&sei->message_list);
@@ -2010,7 +2010,7 @@ static const CodedBitstreamUnitTypeDescriptor cbs_h264_unit_types[] = {
     CBS_UNIT_TYPE_END_OF_LIST
 };
 
-static void cbs_h265_free_sei(FFRefStructOpaque unused, void *content)
+static void cbs_h265_free_sei(AVRefStructOpaque unused, void *content)
 {
     H265RawSEI *sei = content;
     ff_cbs_sei_free_message_list(&sei->message_list);
@@ -2037,7 +2037,7 @@ static const CodedBitstreamUnitTypeDescriptor cbs_h265_unit_types[] = {
     CBS_UNIT_TYPE_END_OF_LIST
 };
 
-static void cbs_h266_free_sei(FFRefStructOpaque unused, void *content)
+static void cbs_h266_free_sei(AVRefStructOpaque unused, void *content)
 {
     H266RawSEI *sei = content;
     ff_cbs_sei_free_message_list(&sei->message_list);
diff --git a/libavcodec/cbs_h266_syntax_template.c b/libavcodec/cbs_h266_syntax_template.c
index 9a6e6f5e01..58211026bb 100644
--- a/libavcodec/cbs_h266_syntax_template.c
+++ b/libavcodec/cbs_h266_syntax_template.c
@@ -1078,7 +1078,7 @@ static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
     ub(4, sps_seq_parameter_set_id);
     ub(4, sps_video_parameter_set_id);
     if (current->sps_video_parameter_set_id == 0 && !h266->vps[0]) {
-        H266RawVPS *vps = ff_refstruct_allocz(sizeof(*vps));
+        H266RawVPS *vps = av_refstruct_allocz(sizeof(*vps));
         if (!vps)
             return AVERROR(ENOMEM);
         vps->vps_max_layers_minus1 = 0;
diff --git a/libavcodec/cbs_internal.h b/libavcodec/cbs_internal.h
index d982262bd9..80cad2b162 100644
--- a/libavcodec/cbs_internal.h
+++ b/libavcodec/cbs_internal.h
@@ -28,7 +28,7 @@
 #include "codec_id.h"
 #include "get_bits.h"
 #include "put_bits.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 
 enum CBSContentType {
@@ -93,7 +93,7 @@ typedef const struct CodedBitstreamUnitTypeDescriptor {
         } ref;
 
         struct {
-            void (*content_free)(FFRefStructOpaque opaque, void *content);
+            void (*content_free)(AVRefStructOpaque opaque, void *content);
             int  (*content_clone)(void **new_content, CodedBitstreamUnit *unit);
         } complex;
     } type;
diff --git a/libavcodec/cbs_sei.c b/libavcodec/cbs_sei.c
index 458751d92e..acc3578aa9 100644
--- a/libavcodec/cbs_sei.c
+++ b/libavcodec/cbs_sei.c
@@ -23,24 +23,24 @@
 #include "cbs_h265.h"
 #include "cbs_h266.h"
 #include "cbs_sei.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
-static void cbs_free_user_data_registered(FFRefStructOpaque unused, void *obj)
+static void cbs_free_user_data_registered(AVRefStructOpaque unused, void *obj)
 {
     SEIRawUserDataRegistered *udr = obj;
-    ff_refstruct_unref(&udr->data);
+    av_refstruct_unref(&udr->data);
 }
 
-static void cbs_free_user_data_unregistered(FFRefStructOpaque unused, void *obj)
+static void cbs_free_user_data_unregistered(AVRefStructOpaque unused, void *obj)
 {
     SEIRawUserDataUnregistered *udu = obj;
-    ff_refstruct_unref(&udu->data);
+    av_refstruct_unref(&udu->data);
 }
 
 int ff_cbs_sei_alloc_message_payload(SEIRawMessage *message,
                                      const SEIMessageTypeDescriptor *desc)
 {
-    void (*free_func)(FFRefStructOpaque, void*);
+    void (*free_func)(AVRefStructOpaque, void*);
 
     av_assert0(message->payload     == NULL &&
                message->payload_ref == NULL);
@@ -54,7 +54,7 @@ int ff_cbs_sei_alloc_message_payload(SEIRawMessage *message,
         free_func = NULL;
     }
 
-    message->payload_ref = ff_refstruct_alloc_ext(desc->size, 0,
+    message->payload_ref = av_refstruct_alloc_ext(desc->size, 0,
                                                   NULL, free_func);
     if (!message->payload_ref)
         return AVERROR(ENOMEM);
@@ -92,8 +92,8 @@ void ff_cbs_sei_free_message_list(SEIRawMessageList *list)
 {
     for (int i = 0; i < list->nb_messages; i++) {
         SEIRawMessage *message = &list->messages[i];
-        ff_refstruct_unref(&message->payload_ref);
-        ff_refstruct_unref(&message->extension_data);
+        av_refstruct_unref(&message->payload_ref);
+        av_refstruct_unref(&message->extension_data);
     }
     av_free(list->messages);
 }
@@ -299,7 +299,7 @@ int ff_cbs_sei_add_message(CodedBitstreamContext *ctx,
     if (payload_ref) {
         /* The following just increments payload_ref's refcount,
          * so that payload_ref is now owned by us. */
-        payload_ref = ff_refstruct_ref(payload_ref);
+        payload_ref = av_refstruct_ref(payload_ref);
     }
 
     message = &list->messages[list->nb_messages - 1];
@@ -352,8 +352,8 @@ static void cbs_sei_delete_message(SEIRawMessageList *list,
     av_assert0(0 <= position && position < list->nb_messages);
 
     message = &list->messages[position];
-    ff_refstruct_unref(&message->payload_ref);
-    ff_refstruct_unref(&message->extension_data);
+    av_refstruct_unref(&message->payload_ref);
+    av_refstruct_unref(&message->extension_data);
 
     --list->nb_messages;
 
diff --git a/libavcodec/container_fifo.c b/libavcodec/container_fifo.c
index 82e86d9465..e1799e5eb7 100644
--- a/libavcodec/container_fifo.c
+++ b/libavcodec/container_fifo.c
@@ -22,11 +22,11 @@
 #include "libavutil/mem.h"
 
 #include "container_fifo.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 struct ContainerFifo {
     AVFifo             *fifo;
-    FFRefStructPool    *pool;
+    AVRefStructPool    *pool;
 
     void*             (*container_alloc)(void);
     void              (*container_reset)(void *obj);
@@ -36,7 +36,7 @@ struct ContainerFifo {
 
 };
 
-static int container_fifo_init_entry(FFRefStructOpaque opaque, void *obj)
+static int container_fifo_init_entry(AVRefStructOpaque opaque, void *obj)
 {
     ContainerFifo *cf = opaque.nc;
     void **pobj = obj;
@@ -48,13 +48,13 @@ static int container_fifo_init_entry(FFRefStructOpaque opaque, void *obj)
     return 0;
 }
 
-static void container_fifo_reset_entry(FFRefStructOpaque opaque, void *obj)
+static void container_fifo_reset_entry(AVRefStructOpaque opaque, void *obj)
 {
     ContainerFifo *cf = opaque.nc;
     cf->container_reset(*(void**)obj);
 }
 
-static void container_fifo_free_entry(FFRefStructOpaque opaque, void *obj)
+static void container_fifo_free_entry(AVRefStructOpaque opaque, void *obj)
 {
     ContainerFifo *cf = opaque.nc;
     cf->container_free(*(void**)obj);
@@ -83,7 +83,7 @@ ff_container_fifo_alloc(void* (*container_alloc)(void),
     if (!cf->fifo)
         goto fail;
 
-    cf->pool = ff_refstruct_pool_alloc_ext(sizeof(void*), 0, cf,
+    cf->pool = av_refstruct_pool_alloc_ext(sizeof(void*), 0, cf,
                                            container_fifo_init_entry,
                                            container_fifo_reset_entry,
                                            container_fifo_free_entry,
@@ -109,11 +109,11 @@ void ff_container_fifo_free(ContainerFifo **pcf)
     if (cf->fifo) {
         void *obj;
         while (av_fifo_read(cf->fifo, &obj, 1) >= 0)
-            ff_refstruct_unref(&obj);
+            av_refstruct_unref(&obj);
         av_fifo_freep2(&cf->fifo);
     }
 
-    ff_refstruct_pool_uninit(&cf->pool);
+    av_refstruct_pool_uninit(&cf->pool);
 
     av_freep(pcf);
 }
@@ -128,7 +128,7 @@ int ff_container_fifo_read(ContainerFifo *cf, void *obj)
         return ret;
 
     ret = cf->fifo_read(obj, *psrc);
-    ff_refstruct_unref(&psrc);
+    av_refstruct_unref(&psrc);
 
     return ret;
 }
@@ -138,7 +138,7 @@ int ff_container_fifo_write(ContainerFifo *cf, void *obj)
     void **pdst;
     int ret;
 
-    pdst = ff_refstruct_pool_get(cf->pool);
+    pdst = av_refstruct_pool_get(cf->pool);
     if (!pdst)
         return AVERROR(ENOMEM);
 
@@ -152,7 +152,7 @@ int ff_container_fifo_write(ContainerFifo *cf, void *obj)
 
     return 0;
 fail:
-    ff_refstruct_unref(&pdst);
+    av_refstruct_unref(&pdst);
     return ret;
 }
 
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index d1e10223f2..cac7e620d2 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -53,7 +53,7 @@
 #include "lcevcdec.h"
 #include "packet_internal.h"
 #include "progressframe.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "thread.h"
 #include "threadprogress.h"
 
@@ -1684,7 +1684,7 @@ static void attach_post_process_data(AVCodecContext *avctx, AVFrame *frame)
     if (dc->lcevc_frame) {
         FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
 
-        fdd->post_process_opaque = ff_refstruct_ref(dc->lcevc);
+        fdd->post_process_opaque = av_refstruct_ref(dc->lcevc);
         fdd->post_process_opaque_free = ff_lcevc_unref;
         fdd->post_process = ff_lcevc_process;
 
@@ -1833,11 +1833,11 @@ static void check_progress_consistency(const ProgressFrame *f)
 
 int ff_progress_frame_alloc(AVCodecContext *avctx, ProgressFrame *f)
 {
-    FFRefStructPool *pool = avctx->internal->progress_frame_pool;
+    AVRefStructPool *pool = avctx->internal->progress_frame_pool;
 
     av_assert1(!f->f && !f->progress);
 
-    f->progress = ff_refstruct_pool_get(pool);
+    f->progress = av_refstruct_pool_get(pool);
     if (!f->progress)
         return AVERROR(ENOMEM);
 
@@ -1859,7 +1859,7 @@ int ff_progress_frame_get_buffer(AVCodecContext *avctx, ProgressFrame *f, int fl
     ret = ff_thread_get_buffer(avctx, f->progress->f, flags);
     if (ret < 0) {
         f->f = NULL;
-        ff_refstruct_unref(&f->progress);
+        av_refstruct_unref(&f->progress);
         return ret;
     }
     return 0;
@@ -1870,14 +1870,14 @@ void ff_progress_frame_ref(ProgressFrame *dst, const ProgressFrame *src)
     av_assert1(src->progress && src->f && src->f == src->progress->f);
     av_assert1(!dst->f && !dst->progress);
     dst->f = src->f;
-    dst->progress = ff_refstruct_ref(src->progress);
+    dst->progress = av_refstruct_ref(src->progress);
 }
 
 void ff_progress_frame_unref(ProgressFrame *f)
 {
     check_progress_consistency(f);
     f->f = NULL;
-    ff_refstruct_unref(&f->progress);
+    av_refstruct_unref(&f->progress);
 }
 
 void ff_progress_frame_replace(ProgressFrame *dst, const ProgressFrame *src)
@@ -1907,7 +1907,7 @@ enum ThreadingStatus ff_thread_sync_ref(AVCodecContext *avctx, size_t offset)
 }
 #endif /* !HAVE_THREADS */
 
-static av_cold int progress_frame_pool_init_cb(FFRefStructOpaque opaque, void *obj)
+static av_cold int progress_frame_pool_init_cb(AVRefStructOpaque opaque, void *obj)
 {
     const AVCodecContext *avctx = opaque.nc;
     ProgressInternal *progress = obj;
@@ -1924,7 +1924,7 @@ static av_cold int progress_frame_pool_init_cb(FFRefStructOpaque opaque, void *o
     return 0;
 }
 
-static void progress_frame_pool_reset_cb(FFRefStructOpaque unused, void *obj)
+static void progress_frame_pool_reset_cb(AVRefStructOpaque unused, void *obj)
 {
     ProgressInternal *progress = obj;
 
@@ -1932,7 +1932,7 @@ static void progress_frame_pool_reset_cb(FFRefStructOpaque unused, void *obj)
     av_frame_unref(progress->f);
 }
 
-static av_cold void progress_frame_pool_free_entry_cb(FFRefStructOpaque opaque, void *obj)
+static av_cold void progress_frame_pool_free_entry_cb(AVRefStructOpaque opaque, void *obj)
 {
     ProgressInternal *progress = obj;
 
@@ -2047,8 +2047,8 @@ int ff_decode_preinit(AVCodecContext *avctx)
 
     if (ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_USES_PROGRESSFRAMES) {
         avci->progress_frame_pool =
-            ff_refstruct_pool_alloc_ext(sizeof(ProgressInternal),
-                                        FF_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR,
+            av_refstruct_pool_alloc_ext(sizeof(ProgressInternal),
+                                        AV_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR,
                                         avctx, progress_frame_pool_init_cb,
                                         progress_frame_pool_reset_cb,
                                         progress_frame_pool_free_entry_cb, NULL);
@@ -2264,11 +2264,11 @@ int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_pr
             return AVERROR(EINVAL);
 
         frames_ctx = (AVHWFramesContext *) avctx->hw_frames_ctx->data;
-        *hwaccel_picture_private = ff_refstruct_alloc_ext(hwaccel->frame_priv_data_size, 0,
+        *hwaccel_picture_private = av_refstruct_alloc_ext(hwaccel->frame_priv_data_size, 0,
                                                           frames_ctx->device_ctx,
                                                           hwaccel->free_frame_priv);
     } else {
-        *hwaccel_picture_private = ff_refstruct_allocz(hwaccel->frame_priv_data_size);
+        *hwaccel_picture_private = av_refstruct_allocz(hwaccel->frame_priv_data_size);
     }
 
     if (!*hwaccel_picture_private)
@@ -2305,7 +2305,7 @@ void ff_decode_internal_sync(AVCodecContext *dst, const AVCodecContext *src)
     const DecodeContext *src_dc = decode_ctx(src->internal);
     DecodeContext *dst_dc = decode_ctx(dst->internal);
 
-    ff_refstruct_replace(&dst_dc->lcevc, src_dc->lcevc);
+    av_refstruct_replace(&dst_dc->lcevc, src_dc->lcevc);
 }
 
 void ff_decode_internal_uninit(AVCodecContext *avctx)
@@ -2313,5 +2313,5 @@ void ff_decode_internal_uninit(AVCodecContext *avctx)
     AVCodecInternal *avci = avctx->internal;
     DecodeContext *dc = decode_ctx(avci);
 
-    ff_refstruct_unref(&dc->lcevc);
+    av_refstruct_unref(&dc->lcevc);
 }
diff --git a/libavcodec/dovi_rpu.c b/libavcodec/dovi_rpu.c
index 5130a9598d..04e48d2b95 100644
--- a/libavcodec/dovi_rpu.c
+++ b/libavcodec/dovi_rpu.c
@@ -24,14 +24,14 @@
 #include "libavutil/mem.h"
 
 #include "dovi_rpu.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 void ff_dovi_ctx_unref(DOVIContext *s)
 {
-    ff_refstruct_unref(&s->dm);
+    av_refstruct_unref(&s->dm);
     for (int i = 0; i < FF_ARRAY_ELEMS(s->vdr); i++)
-        ff_refstruct_unref(&s->vdr[i]);
-    ff_refstruct_unref(&s->ext_blocks);
+        av_refstruct_unref(&s->vdr[i]);
+    av_refstruct_unref(&s->ext_blocks);
     av_free(s->rpu_buf);
 
     *s = (DOVIContext) {
@@ -41,10 +41,10 @@ void ff_dovi_ctx_unref(DOVIContext *s)
 
 void ff_dovi_ctx_flush(DOVIContext *s)
 {
-    ff_refstruct_unref(&s->dm);
+    av_refstruct_unref(&s->dm);
     for (int i = 0; i < FF_ARRAY_ELEMS(s->vdr); i++)
-        ff_refstruct_unref(&s->vdr[i]);
-    ff_refstruct_unref(&s->ext_blocks);
+        av_refstruct_unref(&s->vdr[i]);
+    av_refstruct_unref(&s->ext_blocks);
 
     *s = (DOVIContext) {
         .logctx = s->logctx,
@@ -62,10 +62,10 @@ void ff_dovi_ctx_replace(DOVIContext *s, const DOVIContext *s0)
     s->header = s0->header;
     s->mapping = s0->mapping;
     s->color = s0->color;
-    ff_refstruct_replace(&s->dm, s0->dm);
+    av_refstruct_replace(&s->dm, s0->dm);
     for (int i = 0; i <= DOVI_MAX_DM_ID; i++)
-        ff_refstruct_replace(&s->vdr[i], s0->vdr[i]);
-    ff_refstruct_replace(&s->ext_blocks, s0->ext_blocks);
+        av_refstruct_replace(&s->vdr[i], s0->vdr[i]);
+    av_refstruct_replace(&s->ext_blocks, s0->ext_blocks);
 }
 
 int ff_dovi_guess_profile_hevc(const AVDOVIRpuDataHeader *hdr)
diff --git a/libavcodec/dovi_rpudec.c b/libavcodec/dovi_rpudec.c
index 878950d66d..c231d1c492 100644
--- a/libavcodec/dovi_rpudec.c
+++ b/libavcodec/dovi_rpudec.c
@@ -28,7 +28,7 @@
 #include "dovi_rpu.h"
 #include "golomb.h"
 #include "get_bits.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 int ff_dovi_get_metadata(DOVIContext *s, AVDOVIMetadata **out_metadata)
 {
@@ -291,7 +291,7 @@ static int parse_ext_blocks(DOVIContext *s, GetBitContext *gb, int ver,
     align_get_bits(gb);
 
     if (num_ext_blocks && !ext) {
-        ext = s->ext_blocks = ff_refstruct_allocz(sizeof(*s->ext_blocks));
+        ext = s->ext_blocks = av_refstruct_allocz(sizeof(*s->ext_blocks));
         if (!ext)
             return AVERROR(ENOMEM);
     }
@@ -556,7 +556,7 @@ int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size,
         int vdr_rpu_id = get_ue_golomb_31(gb);
         VALIDATE(vdr_rpu_id, 0, DOVI_MAX_DM_ID);
         if (!s->vdr[vdr_rpu_id]) {
-            s->vdr[vdr_rpu_id] = ff_refstruct_allocz(sizeof(AVDOVIDataMapping));
+            s->vdr[vdr_rpu_id] = av_refstruct_allocz(sizeof(AVDOVIDataMapping));
             if (!s->vdr[vdr_rpu_id]) {
                 ff_dovi_ctx_unref(s);
                 return AVERROR(ENOMEM);
@@ -675,7 +675,7 @@ int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size,
         }
 
         if (!s->dm) {
-            s->dm = ff_refstruct_allocz(sizeof(AVDOVIColorMetadata));
+            s->dm = av_refstruct_allocz(sizeof(AVDOVIColorMetadata));
             if (!s->dm) {
                 ff_dovi_ctx_unref(s);
                 return AVERROR(ENOMEM);
@@ -735,7 +735,7 @@ int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size,
         }
     } else {
         s->color = &ff_dovi_color_default;
-        ff_refstruct_unref(&s->ext_blocks);
+        av_refstruct_unref(&s->ext_blocks);
     }
 
     return 0;
diff --git a/libavcodec/dovi_rpuenc.c b/libavcodec/dovi_rpuenc.c
index 8113ec44bf..eb79a3af5d 100644
--- a/libavcodec/dovi_rpuenc.c
+++ b/libavcodec/dovi_rpuenc.c
@@ -29,7 +29,7 @@
 #include "itut35.h"
 #include "put_bits.h"
 #include "put_golomb.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 static struct {
     uint64_t pps; // maximum pixels per second
@@ -601,7 +601,7 @@ int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata,
     use_prev_vdr_rpu = 0;
 
     if (!s->vdr[vdr_rpu_id]) {
-        s->vdr[vdr_rpu_id] = ff_refstruct_allocz(sizeof(AVDOVIDataMapping));
+        s->vdr[vdr_rpu_id] = av_refstruct_allocz(sizeof(AVDOVIDataMapping));
         if (!s->vdr[vdr_rpu_id])
             return AVERROR(ENOMEM);
     }
@@ -625,12 +625,12 @@ int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata,
          * references requires extended compression */
         for (int i = 0; i <= DOVI_MAX_DM_ID; i++) {
             if (i != vdr_rpu_id)
-                ff_refstruct_unref(&s->vdr[i]);
+                av_refstruct_unref(&s->vdr[i]);
         }
     }
 
     if (metadata->num_ext_blocks && !s->ext_blocks) {
-        s->ext_blocks = ff_refstruct_allocz(sizeof(*s->ext_blocks));
+        s->ext_blocks = av_refstruct_allocz(sizeof(*s->ext_blocks));
         if (!s->ext_blocks)
             return AVERROR(ENOMEM);
     }
@@ -640,7 +640,7 @@ int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata,
         vdr_dm_metadata_present = 1;
 
     if (vdr_dm_metadata_present && !s->dm) {
-        s->dm = ff_refstruct_allocz(sizeof(AVDOVIColorMetadata));
+        s->dm = av_refstruct_allocz(sizeof(AVDOVIColorMetadata));
         if (!s->dm)
             return AVERROR(ENOMEM);
     }
@@ -864,7 +864,7 @@ int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata,
         }
     } else {
         s->color = &ff_dovi_color_default;
-        ff_refstruct_unref(&s->ext_blocks);
+        av_refstruct_unref(&s->ext_blocks);
     }
 
     flush_put_bits(pb);
diff --git a/libavcodec/ffv1.c b/libavcodec/ffv1.c
index 6c953e860f..927763e24d 100644
--- a/libavcodec/ffv1.c
+++ b/libavcodec/ffv1.c
@@ -31,7 +31,7 @@
 
 #include "avcodec.h"
 #include "ffv1.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 av_cold int ff_ffv1_common_init(AVCodecContext *avctx)
 {
@@ -53,7 +53,7 @@ av_cold int ff_ffv1_common_init(AVCodecContext *avctx)
     return 0;
 }
 
-static void planes_free(FFRefStructOpaque opaque, void *obj)
+static void planes_free(AVRefStructOpaque opaque, void *obj)
 {
     PlaneContext *planes = obj;
 
@@ -67,7 +67,7 @@ static void planes_free(FFRefStructOpaque opaque, void *obj)
 
 PlaneContext* ff_ffv1_planes_alloc(void)
 {
-    return ff_refstruct_alloc_ext(sizeof(PlaneContext) * MAX_PLANES,
+    return av_refstruct_alloc_ext(sizeof(PlaneContext) * MAX_PLANES,
                                   0, NULL, planes_free);
 }
 
@@ -233,10 +233,10 @@ av_cold int ff_ffv1_close(AVCodecContext *avctx)
         av_freep(&sc->sample_buffer);
         av_freep(&sc->sample_buffer32);
 
-        ff_refstruct_unref(&sc->plane);
+        av_refstruct_unref(&sc->plane);
     }
 
-    ff_refstruct_unref(&s->slice_damaged);
+    av_refstruct_unref(&s->slice_damaged);
 
     av_freep(&avctx->stats_out);
     for (j = 0; j < s->quant_table_count; j++) {
diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c
index de5abfe9b4..7845815873 100644
--- a/libavcodec/ffv1dec.c
+++ b/libavcodec/ffv1dec.c
@@ -38,7 +38,7 @@
 #include "mathops.h"
 #include "ffv1.h"
 #include "progressframe.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "thread.h"
 
 static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
@@ -780,8 +780,8 @@ static int read_header(FFV1Context *f)
         return AVERROR_INVALIDDATA;
     }
 
-    ff_refstruct_unref(&f->slice_damaged);
-    f->slice_damaged = ff_refstruct_allocz(f->slice_count * sizeof(*f->slice_damaged));
+    av_refstruct_unref(&f->slice_damaged);
+    f->slice_damaged = av_refstruct_allocz(f->slice_count * sizeof(*f->slice_damaged));
     if (!f->slice_damaged)
         return AVERROR(ENOMEM);
 
@@ -810,7 +810,7 @@ static int read_header(FFV1Context *f)
                         && (unsigned)sc->slice_y + (uint64_t)sc->slice_height <= f->height);
         }
 
-        ff_refstruct_unref(&sc->plane);
+        av_refstruct_unref(&sc->plane);
         sc->plane = ff_ffv1_planes_alloc();
         if (!sc->plane)
             return AVERROR(ENOMEM);
@@ -1059,7 +1059,7 @@ static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
         FFV1SliceContext       *sc  = &fdst->slices[i];
         const FFV1SliceContext *sc0 = &fsrc->slices[i];
 
-        ff_refstruct_replace(&sc->plane, sc0->plane);
+        av_refstruct_replace(&sc->plane, sc0->plane);
 
         if (fsrc->version < 3) {
             sc->slice_x             = sc0->slice_x;
@@ -1069,7 +1069,7 @@ static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
         }
     }
 
-    ff_refstruct_replace(&fdst->slice_damaged, fsrc->slice_damaged);
+    av_refstruct_replace(&fdst->slice_damaged, fsrc->slice_damaged);
 
     av_assert1(fdst->max_slice_count == fsrc->max_slice_count);
 
diff --git a/libavcodec/get_buffer.c b/libavcodec/get_buffer.c
index ff19f61e86..12aa543f67 100644
--- a/libavcodec/get_buffer.c
+++ b/libavcodec/get_buffer.c
@@ -32,7 +32,7 @@
 
 #include "avcodec.h"
 #include "internal.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 typedef struct FramePool {
     /**
@@ -53,7 +53,7 @@ typedef struct FramePool {
     int samples;
 } FramePool;
 
-static void frame_pool_free(FFRefStructOpaque unused, void *obj)
+static void frame_pool_free(AVRefStructOpaque unused, void *obj)
 {
     FramePool *pool = obj;
     int i;
@@ -77,7 +77,7 @@ static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
             return 0;
     }
 
-    pool = ff_refstruct_alloc_ext(sizeof(*pool), 0, NULL, frame_pool_free);
+    pool = av_refstruct_alloc_ext(sizeof(*pool), 0, NULL, frame_pool_free);
     if (!pool)
         return AVERROR(ENOMEM);
 
@@ -157,12 +157,12 @@ static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
     default: av_assert0(0);
     }
 
-    ff_refstruct_unref(&avctx->internal->pool);
+    av_refstruct_unref(&avctx->internal->pool);
     avctx->internal->pool = pool;
 
     return 0;
 fail:
-    ff_refstruct_unref(&pool);
+    av_refstruct_unref(&pool);
     return ret;
 }
 
diff --git a/libavcodec/h2645_sei.c b/libavcodec/h2645_sei.c
index 986d1d250a..c8fa975bf4 100644
--- a/libavcodec/h2645_sei.c
+++ b/libavcodec/h2645_sei.c
@@ -32,6 +32,7 @@
 #include "libavutil/film_grain_params.h"
 #include "libavutil/mastering_display_metadata.h"
 #include "libavutil/mem.h"
+#include "libavutil/refstruct.h"
 #include "libavutil/stereo3d.h"
 
 #include "atsc_a53.h"
@@ -42,7 +43,6 @@
 #include "golomb.h"
 #include "h2645_sei.h"
 #include "itut35.h"
-#include "refstruct.h"
 
 #define IS_H264(codec_id) (CONFIG_H264_SEI && CONFIG_HEVC_SEI ? codec_id == AV_CODEC_ID_H264 : CONFIG_H264_SEI)
 #define IS_HEVC(codec_id) (CONFIG_H264_SEI && CONFIG_HEVC_SEI ? codec_id == AV_CODEC_ID_HEVC : CONFIG_HEVC_SEI)
@@ -496,8 +496,8 @@ int ff_h2645_sei_message_decode(H2645SEI *h, enum SEIType type,
     case SEI_TYPE_DISPLAY_ORIENTATION:
         return decode_display_orientation(&h->display_orientation, gb);
     case SEI_TYPE_FILM_GRAIN_CHARACTERISTICS:
-        ff_refstruct_unref(&h->film_grain_characteristics);
-        h->film_grain_characteristics = ff_refstruct_allocz(sizeof(*h->film_grain_characteristics));
+        av_refstruct_unref(&h->film_grain_characteristics);
+        h->film_grain_characteristics = av_refstruct_allocz(sizeof(*h->film_grain_characteristics));
         if (!h->film_grain_characteristics)
             return AVERROR(ENOMEM);
         return decode_film_grain_characteristics(h->film_grain_characteristics, codec_id, gb);
@@ -559,7 +559,7 @@ int ff_h2645_sei_ctx_replace(H2645SEI *dst, const H2645SEI *src)
     dst->mastering_display     = src->mastering_display;
     dst->content_light         = src->content_light;
 
-    ff_refstruct_replace(&dst->film_grain_characteristics,
+    av_refstruct_replace(&dst->film_grain_characteristics,
                           src->film_grain_characteristics);
 
     return 0;
@@ -934,6 +934,6 @@ void ff_h2645_sei_reset(H2645SEI *s)
     s->mastering_display.present = 0;
     s->content_light.present = 0;
 
-    ff_refstruct_unref(&s->film_grain_characteristics);
+    av_refstruct_unref(&s->film_grain_characteristics);
     ff_aom_uninit_film_grain_params(&s->aom_film_grain);
 }
diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c
index 94cfbc481e..006f4059e8 100644
--- a/libavcodec/h264_parser.c
+++ b/libavcodec/h264_parser.c
@@ -47,7 +47,7 @@
 #include "h264data.h"
 #include "mpegutils.h"
 #include "parser.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "startcode.h"
 
 typedef struct H264ParseContext {
@@ -374,7 +374,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
                 goto fail;
             }
 
-            ff_refstruct_replace(&p->ps.pps, p->ps.pps_list[pps_id]);
+            av_refstruct_replace(&p->ps.pps, p->ps.pps_list[pps_id]);
             p->ps.sps = p->ps.pps->sps;
             sps       = p->ps.sps;
 
diff --git a/libavcodec/h264_picture.c b/libavcodec/h264_picture.c
index 3234141dbd..f5d2b31cd6 100644
--- a/libavcodec/h264_picture.c
+++ b/libavcodec/h264_picture.c
@@ -32,7 +32,7 @@
 #include "h264dec.h"
 #include "hwaccel_internal.h"
 #include "mpegutils.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "thread.h"
 #include "threadframe.h"
 
@@ -46,35 +46,35 @@ void ff_h264_unref_picture(H264Picture *pic)
 
     ff_thread_release_ext_buffer(&pic->tf);
     av_frame_unref(pic->f_grain);
-    ff_refstruct_unref(&pic->hwaccel_picture_private);
+    av_refstruct_unref(&pic->hwaccel_picture_private);
 
-    ff_refstruct_unref(&pic->qscale_table_base);
-    ff_refstruct_unref(&pic->mb_type_base);
-    ff_refstruct_unref(&pic->pps);
+    av_refstruct_unref(&pic->qscale_table_base);
+    av_refstruct_unref(&pic->mb_type_base);
+    av_refstruct_unref(&pic->pps);
     for (i = 0; i < 2; i++) {
-        ff_refstruct_unref(&pic->motion_val_base[i]);
-        ff_refstruct_unref(&pic->ref_index[i]);
+        av_refstruct_unref(&pic->motion_val_base[i]);
+        av_refstruct_unref(&pic->ref_index[i]);
     }
-    ff_refstruct_unref(&pic->decode_error_flags);
+    av_refstruct_unref(&pic->decode_error_flags);
 
     memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
 }
 
 static void h264_copy_picture_params(H264Picture *dst, const H264Picture *src)
 {
-    ff_refstruct_replace(&dst->qscale_table_base, src->qscale_table_base);
-    ff_refstruct_replace(&dst->mb_type_base,      src->mb_type_base);
-    ff_refstruct_replace(&dst->pps, src->pps);
+    av_refstruct_replace(&dst->qscale_table_base, src->qscale_table_base);
+    av_refstruct_replace(&dst->mb_type_base,      src->mb_type_base);
+    av_refstruct_replace(&dst->pps, src->pps);
 
     for (int i = 0; i < 2; i++) {
-        ff_refstruct_replace(&dst->motion_val_base[i], src->motion_val_base[i]);
-        ff_refstruct_replace(&dst->ref_index[i],       src->ref_index[i]);
+        av_refstruct_replace(&dst->motion_val_base[i], src->motion_val_base[i]);
+        av_refstruct_replace(&dst->ref_index[i],       src->ref_index[i]);
     }
 
-    ff_refstruct_replace(&dst->hwaccel_picture_private,
+    av_refstruct_replace(&dst->hwaccel_picture_private,
                           src->hwaccel_picture_private);
 
-    ff_refstruct_replace(&dst->decode_error_flags, src->decode_error_flags);
+    av_refstruct_replace(&dst->decode_error_flags, src->decode_error_flags);
 
     dst->qscale_table = src->qscale_table;
     dst->mb_type      = src->mb_type;
diff --git a/libavcodec/h264_ps.c b/libavcodec/h264_ps.c
index 3c8fc33c7f..c698f1b80d 100644
--- a/libavcodec/h264_ps.c
+++ b/libavcodec/h264_ps.c
@@ -34,7 +34,7 @@
 #include "h2645_vui.h"
 #include "h264_ps.h"
 #include "golomb.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 #define MIN_LOG2_MAX_FRAME_NUM    4
 
@@ -86,7 +86,7 @@ static const int level_max_dpb_mbs[][2] = {
 
 static void remove_pps(H264ParamSets *s, int id)
 {
-    ff_refstruct_unref(&s->pps_list[id]);
+    av_refstruct_unref(&s->pps_list[id]);
 }
 
 static void remove_sps(H264ParamSets *s, int id)
@@ -100,7 +100,7 @@ static void remove_sps(H264ParamSets *s, int id)
                 remove_pps(s, i);
     }
 #endif
-    ff_refstruct_unref(&s->sps_list[id]);
+    av_refstruct_unref(&s->sps_list[id]);
 }
 
 static inline int decode_hrd_parameters(GetBitContext *gb, void *logctx,
@@ -272,12 +272,12 @@ void ff_h264_ps_uninit(H264ParamSets *ps)
     int i;
 
     for (i = 0; i < MAX_SPS_COUNT; i++)
-        ff_refstruct_unref(&ps->sps_list[i]);
+        av_refstruct_unref(&ps->sps_list[i]);
 
     for (i = 0; i < MAX_PPS_COUNT; i++)
-        ff_refstruct_unref(&ps->pps_list[i]);
+        av_refstruct_unref(&ps->pps_list[i]);
 
-    ff_refstruct_unref(&ps->pps);
+    av_refstruct_unref(&ps->pps);
     ps->sps = NULL;
 }
 
@@ -290,7 +290,7 @@ int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
     SPS *sps;
     int ret;
 
-    sps = ff_refstruct_allocz(sizeof(*sps));
+    sps = av_refstruct_allocz(sizeof(*sps));
     if (!sps)
         return AVERROR(ENOMEM);
 
@@ -578,7 +578,7 @@ int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
      * otherwise drop all PPSes that depend on it */
     if (ps->sps_list[sps_id] &&
         !memcmp(ps->sps_list[sps_id], sps, sizeof(*sps))) {
-        ff_refstruct_unref(&sps);
+        av_refstruct_unref(&sps);
     } else {
         remove_sps(ps, sps_id);
         ps->sps_list[sps_id] = sps;
@@ -587,7 +587,7 @@ int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
     return 0;
 
 fail:
-    ff_refstruct_unref(&sps);
+    av_refstruct_unref(&sps);
     return AVERROR_INVALIDDATA;
 }
 
@@ -686,11 +686,11 @@ static int more_rbsp_data_in_pps(const SPS *sps, void *logctx)
     return 1;
 }
 
-static void pps_free(FFRefStructOpaque unused, void *obj)
+static void pps_free(AVRefStructOpaque unused, void *obj)
 {
     PPS *pps = obj;
 
-    ff_refstruct_unref(&pps->sps);
+    av_refstruct_unref(&pps->sps);
 }
 
 int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
@@ -708,7 +708,7 @@ int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avct
         return AVERROR_INVALIDDATA;
     }
 
-    pps = ff_refstruct_alloc_ext(sizeof(*pps), 0, NULL, pps_free);
+    pps = av_refstruct_alloc_ext(sizeof(*pps), 0, NULL, pps_free);
     if (!pps)
         return AVERROR(ENOMEM);
 
@@ -733,7 +733,7 @@ int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avct
         ret = AVERROR_INVALIDDATA;
         goto fail;
     }
-    pps->sps = ff_refstruct_ref_c(ps->sps_list[pps->sps_id]);
+    pps->sps = av_refstruct_ref_c(ps->sps_list[pps->sps_id]);
     sps      = pps->sps;
 
     if (sps->bit_depth_luma > 14) {
@@ -840,6 +840,6 @@ int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avct
     return 0;
 
 fail:
-    ff_refstruct_unref(&pps);
+    av_refstruct_unref(&pps);
     return ret;
 }
diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
index c35ad9b910..5108fa0921 100644
--- a/libavcodec/h264_slice.c
+++ b/libavcodec/h264_slice.c
@@ -45,7 +45,7 @@
 #include "mathops.h"
 #include "mpegutils.h"
 #include "rectangle.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "thread.h"
 #include "threadframe.h"
 
@@ -166,19 +166,19 @@ static int init_table_pools(H264Context *h)
     const int b4_stride     = h->mb_width * 4 + 1;
     const int b4_array_size = b4_stride * h->mb_height * 4;
 
-    h->qscale_table_pool = ff_refstruct_pool_alloc(big_mb_num + h->mb_stride, 0);
-    h->mb_type_pool      = ff_refstruct_pool_alloc((big_mb_num + h->mb_stride) *
+    h->qscale_table_pool = av_refstruct_pool_alloc(big_mb_num + h->mb_stride, 0);
+    h->mb_type_pool      = av_refstruct_pool_alloc((big_mb_num + h->mb_stride) *
                                                    sizeof(uint32_t), 0);
-    h->motion_val_pool   = ff_refstruct_pool_alloc(2 * (b4_array_size + 4) *
+    h->motion_val_pool   = av_refstruct_pool_alloc(2 * (b4_array_size + 4) *
                                                    sizeof(int16_t), 0);
-    h->ref_index_pool    = ff_refstruct_pool_alloc(4 * mb_array_size, 0);
+    h->ref_index_pool    = av_refstruct_pool_alloc(4 * mb_array_size, 0);
 
     if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
         !h->ref_index_pool) {
-        ff_refstruct_pool_uninit(&h->qscale_table_pool);
-        ff_refstruct_pool_uninit(&h->mb_type_pool);
-        ff_refstruct_pool_uninit(&h->motion_val_pool);
-        ff_refstruct_pool_uninit(&h->ref_index_pool);
+        av_refstruct_pool_uninit(&h->qscale_table_pool);
+        av_refstruct_pool_uninit(&h->mb_type_pool);
+        av_refstruct_pool_uninit(&h->motion_val_pool);
+        av_refstruct_pool_uninit(&h->ref_index_pool);
         return AVERROR(ENOMEM);
     }
 
@@ -211,7 +211,7 @@ static int alloc_picture(H264Context *h, H264Picture *pic)
         goto fail;
 
     if (h->decode_error_flags_pool) {
-        pic->decode_error_flags = ff_refstruct_pool_get(h->decode_error_flags_pool);
+        pic->decode_error_flags = av_refstruct_pool_get(h->decode_error_flags_pool);
         if (!pic->decode_error_flags)
             goto fail;
         atomic_init(pic->decode_error_flags, 0);
@@ -236,8 +236,8 @@ static int alloc_picture(H264Context *h, H264Picture *pic)
             goto fail;
     }
 
-    pic->qscale_table_base = ff_refstruct_pool_get(h->qscale_table_pool);
-    pic->mb_type_base      = ff_refstruct_pool_get(h->mb_type_pool);
+    pic->qscale_table_base = av_refstruct_pool_get(h->qscale_table_pool);
+    pic->mb_type_base      = av_refstruct_pool_get(h->mb_type_pool);
     if (!pic->qscale_table_base || !pic->mb_type_base)
         goto fail;
 
@@ -245,15 +245,15 @@ static int alloc_picture(H264Context *h, H264Picture *pic)
     pic->qscale_table = pic->qscale_table_base + 2 * h->mb_stride + 1;
 
     for (i = 0; i < 2; i++) {
-        pic->motion_val_base[i] = ff_refstruct_pool_get(h->motion_val_pool);
-        pic->ref_index[i]       = ff_refstruct_pool_get(h->ref_index_pool);
+        pic->motion_val_base[i] = av_refstruct_pool_get(h->motion_val_pool);
+        pic->ref_index[i]       = av_refstruct_pool_get(h->ref_index_pool);
         if (!pic->motion_val_base[i] || !pic->ref_index[i])
             goto fail;
 
         pic->motion_val[i] = pic->motion_val_base[i] + 4;
     }
 
-    pic->pps = ff_refstruct_ref_c(h->ps.pps);
+    pic->pps = av_refstruct_ref_c(h->ps.pps);
 
     pic->mb_width  = h->mb_width;
     pic->mb_height = h->mb_height;
@@ -358,11 +358,11 @@ int ff_h264_update_thread_context(AVCodecContext *dst,
 
     // SPS/PPS
     for (int i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++)
-        ff_refstruct_replace(&h->ps.sps_list[i], h1->ps.sps_list[i]);
+        av_refstruct_replace(&h->ps.sps_list[i], h1->ps.sps_list[i]);
     for (int i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++)
-        ff_refstruct_replace(&h->ps.pps_list[i], h1->ps.pps_list[i]);
+        av_refstruct_replace(&h->ps.pps_list[i], h1->ps.pps_list[i]);
 
-    ff_refstruct_replace(&h->ps.pps, h1->ps.pps);
+    av_refstruct_replace(&h->ps.pps, h1->ps.pps);
     h->ps.sps = h1->ps.sps;
 
     if (need_reinit || !inited) {
@@ -1050,7 +1050,7 @@ static int h264_init_ps(H264Context *h, const H264SliceContext *sl, int first_sl
     int needs_reinit = 0, must_reinit, ret;
 
     if (first_slice)
-        ff_refstruct_replace(&h->ps.pps, h->ps.pps_list[sl->pps_id]);
+        av_refstruct_replace(&h->ps.pps, h->ps.pps_list[sl->pps_id]);
 
     if (h->ps.sps != h->ps.pps->sps) {
         h->ps.sps = h->ps.pps->sps;
diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c
index 0154fe17b6..8feb5558aa 100644
--- a/libavcodec/h264dec.c
+++ b/libavcodec/h264dec.c
@@ -52,7 +52,7 @@
 #include "mpegutils.h"
 #include "profiles.h"
 #include "rectangle.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "thread.h"
 #include "threadframe.h"
 
@@ -156,10 +156,10 @@ void ff_h264_free_tables(H264Context *h)
     av_freep(&h->mb2b_xy);
     av_freep(&h->mb2br_xy);
 
-    ff_refstruct_pool_uninit(&h->qscale_table_pool);
-    ff_refstruct_pool_uninit(&h->mb_type_pool);
-    ff_refstruct_pool_uninit(&h->motion_val_pool);
-    ff_refstruct_pool_uninit(&h->ref_index_pool);
+    av_refstruct_pool_uninit(&h->qscale_table_pool);
+    av_refstruct_pool_uninit(&h->mb_type_pool);
+    av_refstruct_pool_uninit(&h->motion_val_pool);
+    av_refstruct_pool_uninit(&h->ref_index_pool);
 
 #if CONFIG_ERROR_RESILIENCE
     av_freep(&h->er.mb_index2xy);
@@ -313,7 +313,7 @@ static int h264_init_context(AVCodecContext *avctx, H264Context *h)
     ff_h264_sei_uninit(&h->sei);
 
     if (avctx->active_thread_type & FF_THREAD_FRAME) {
-        h->decode_error_flags_pool = ff_refstruct_pool_alloc(sizeof(atomic_int), 0);
+        h->decode_error_flags_pool = av_refstruct_pool_alloc(sizeof(atomic_int), 0);
         if (!h->decode_error_flags_pool)
             return AVERROR(ENOMEM);
     }
@@ -364,7 +364,7 @@ static av_cold int h264_decode_end(AVCodecContext *avctx)
 
     h->cur_pic_ptr = NULL;
 
-    ff_refstruct_pool_uninit(&h->decode_error_flags_pool);
+    av_refstruct_pool_uninit(&h->decode_error_flags_pool);
 
     av_freep(&h->slice_ctx);
     h->nb_slice_ctx = 0;
diff --git a/libavcodec/h264dec.h b/libavcodec/h264dec.h
index ccd7583bf4..5abc60c09d 100644
--- a/libavcodec/h264dec.h
+++ b/libavcodec/h264dec.h
@@ -570,11 +570,11 @@ typedef struct H264Context {
 
     H264SEIContext sei;
 
-    struct FFRefStructPool *qscale_table_pool;
-    struct FFRefStructPool *mb_type_pool;
-    struct FFRefStructPool *motion_val_pool;
-    struct FFRefStructPool *ref_index_pool;
-    struct FFRefStructPool *decode_error_flags_pool;
+    struct AVRefStructPool *qscale_table_pool;
+    struct AVRefStructPool *mb_type_pool;
+    struct AVRefStructPool *motion_val_pool;
+    struct AVRefStructPool *ref_index_pool;
+    struct AVRefStructPool *decode_error_flags_pool;
     int ref2frm[MAX_SLICES][2][64];     ///< reference to frame number lists, used in the loop filter, the first 2 are for -2,-1
 
     int non_gray;                       ///< Did we encounter a intra frame after a gray gap frame
diff --git a/libavcodec/hevc/hevcdec.c b/libavcodec/hevc/hevcdec.c
index be35a9de82..421579efe5 100644
--- a/libavcodec/hevc/hevcdec.c
+++ b/libavcodec/hevc/hevcdec.c
@@ -52,7 +52,7 @@
 #include "internal.h"
 #include "profiles.h"
 #include "progressframe.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "thread.h"
 #include "threadprogress.h"
 
@@ -92,8 +92,8 @@ static void pic_arrays_free(HEVCLayerContext *l)
         av_freep(&l->sao_pixel_buffer_v[i]);
     }
 
-    ff_refstruct_pool_uninit(&l->tab_mvf_pool);
-    ff_refstruct_pool_uninit(&l->rpl_tab_pool);
+    av_refstruct_pool_uninit(&l->tab_mvf_pool);
+    av_refstruct_pool_uninit(&l->rpl_tab_pool);
 }
 
 /* allocate arrays that depend on frame dimensions */
@@ -139,8 +139,8 @@ static int pic_arrays_init(HEVCLayerContext *l, const HEVCSPS *sps)
     if (!l->horizontal_bs || !l->vertical_bs)
         goto fail;
 
-    l->tab_mvf_pool = ff_refstruct_pool_alloc(min_pu_size * sizeof(MvField), 0);
-    l->rpl_tab_pool = ff_refstruct_pool_alloc(ctb_count   * sizeof(RefPicListTab), 0);
+    l->tab_mvf_pool = av_refstruct_pool_alloc(min_pu_size * sizeof(MvField), 0);
+    l->rpl_tab_pool = av_refstruct_pool_alloc(ctb_count   * sizeof(RefPicListTab), 0);
     if (!l->tab_mvf_pool || !l->rpl_tab_pool)
         goto fail;
 
@@ -677,8 +677,8 @@ static int set_sps(HEVCContext *s, HEVCLayerContext *l, const HEVCSPS *sps)
     int ret;
 
     pic_arrays_free(l);
-    ff_refstruct_unref(&l->sps);
-    ff_refstruct_unref(&s->vps);
+    av_refstruct_unref(&l->sps);
+    av_refstruct_unref(&s->vps);
 
     if (!sps)
         return 0;
@@ -691,14 +691,14 @@ static int set_sps(HEVCContext *s, HEVCLayerContext *l, const HEVCSPS *sps)
     ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
     ff_videodsp_init (&s->vdsp,    sps->bit_depth);
 
-    l->sps    = ff_refstruct_ref_c(sps);
-    s->vps    = ff_refstruct_ref_c(sps->vps);
+    l->sps    = av_refstruct_ref_c(sps);
+    s->vps    = av_refstruct_ref_c(sps->vps);
 
     return 0;
 
 fail:
     pic_arrays_free(l);
-    ff_refstruct_unref(&l->sps);
+    av_refstruct_unref(&l->sps);
     return ret;
 }
 
@@ -3162,7 +3162,7 @@ static int hevc_frame_start(HEVCContext *s, HEVCLayerContext *l,
         return AVERROR_INVALIDDATA;
     }
 
-    ff_refstruct_replace(&s->pps, pps);
+    av_refstruct_replace(&s->pps, pps);
     if (l->sps != sps) {
         const HEVCSPS *sps_base = s->layers[0].sps;
         enum AVPixelFormat pix_fmt = sps->pix_fmt;
@@ -3811,10 +3811,10 @@ static int hevc_ref_frame(HEVCFrame *dst, const HEVCFrame *src)
         dst->needs_fg = 1;
     }
 
-    dst->pps     = ff_refstruct_ref_c(src->pps);
-    dst->tab_mvf = ff_refstruct_ref(src->tab_mvf);
-    dst->rpl_tab = ff_refstruct_ref(src->rpl_tab);
-    dst->rpl = ff_refstruct_ref(src->rpl);
+    dst->pps     = av_refstruct_ref_c(src->pps);
+    dst->tab_mvf = av_refstruct_ref(src->tab_mvf);
+    dst->rpl_tab = av_refstruct_ref(src->rpl_tab);
+    dst->rpl = av_refstruct_ref(src->rpl);
     dst->nb_rpl_elems = src->nb_rpl_elems;
 
     dst->poc        = src->poc;
@@ -3823,7 +3823,7 @@ static int hevc_ref_frame(HEVCFrame *dst, const HEVCFrame *src)
 
     dst->base_layer_frame = src->base_layer_frame;
 
-    ff_refstruct_replace(&dst->hwaccel_picture_private,
+    av_refstruct_replace(&dst->hwaccel_picture_private,
                           src->hwaccel_picture_private);
 
     return 0;
@@ -3835,11 +3835,11 @@ static av_cold int hevc_decode_free(AVCodecContext *avctx)
 
     for (int i = 0; i < FF_ARRAY_ELEMS(s->layers); i++) {
         pic_arrays_free(&s->layers[i]);
-        ff_refstruct_unref(&s->layers[i].sps);
+        av_refstruct_unref(&s->layers[i].sps);
     }
 
-    ff_refstruct_unref(&s->vps);
-    ff_refstruct_unref(&s->pps);
+    av_refstruct_unref(&s->vps);
+    av_refstruct_unref(&s->pps);
 
     ff_dovi_ctx_unref(&s->dovi_ctx);
     av_buffer_unref(&s->rpu_buf);
@@ -3945,16 +3945,16 @@ static int hevc_update_thread_context(AVCodecContext *dst,
     }
 
     for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++)
-        ff_refstruct_replace(&s->ps.vps_list[i], s0->ps.vps_list[i]);
+        av_refstruct_replace(&s->ps.vps_list[i], s0->ps.vps_list[i]);
 
     for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++)
-        ff_refstruct_replace(&s->ps.sps_list[i], s0->ps.sps_list[i]);
+        av_refstruct_replace(&s->ps.sps_list[i], s0->ps.sps_list[i]);
 
     for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++)
-        ff_refstruct_replace(&s->ps.pps_list[i], s0->ps.pps_list[i]);
+        av_refstruct_replace(&s->ps.pps_list[i], s0->ps.pps_list[i]);
 
     // PPS do not persist between frames
-    ff_refstruct_unref(&s->pps);
+    av_refstruct_unref(&s->pps);
 
     s->poc_tid0   = s0->poc_tid0;
     s->eos        = s0->eos;
diff --git a/libavcodec/hevc/hevcdec.h b/libavcodec/hevc/hevcdec.h
index 473709b4e8..b7a762089b 100644
--- a/libavcodec/hevc/hevcdec.h
+++ b/libavcodec/hevc/hevcdec.h
@@ -480,8 +480,8 @@ typedef struct HEVCLayerContext {
     uint8_t                *sao_pixel_buffer_h[3];
     uint8_t                *sao_pixel_buffer_v[3];
 
-    struct FFRefStructPool *tab_mvf_pool;
-    struct FFRefStructPool *rpl_tab_pool;
+    struct AVRefStructPool *tab_mvf_pool;
+    struct AVRefStructPool *rpl_tab_pool;
 } HEVCLayerContext;
 
 typedef struct HEVCContext {
diff --git a/libavcodec/hevc/ps.c b/libavcodec/hevc/ps.c
index a1d352eec5..285084685b 100644
--- a/libavcodec/hevc/ps.c
+++ b/libavcodec/hevc/ps.c
@@ -30,7 +30,7 @@
 #include "data.h"
 #include "ps.h"
 #include "profiles.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 static const uint8_t default_scaling_list_intra[] = {
     16, 16, 16, 16, 17, 18, 21, 24,
@@ -69,9 +69,9 @@ static void remove_sps(HEVCParamSets *s, int id)
         /* drop all PPS that depend on this SPS */
         for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
             if (s->pps_list[i] && s->pps_list[i]->sps_id == id)
-                ff_refstruct_unref(&s->pps_list[i]);
+                av_refstruct_unref(&s->pps_list[i]);
 
-        ff_refstruct_unref(&s->sps_list[id]);
+        av_refstruct_unref(&s->sps_list[id]);
     }
 }
 
@@ -82,7 +82,7 @@ static void remove_vps(HEVCParamSets *s, int id)
         for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
             if (s->sps_list[i] && s->sps_list[i]->vps_id == id)
                 remove_sps(s, i);
-        ff_refstruct_unref(&s->vps_list[id]);
+        av_refstruct_unref(&s->vps_list[id]);
     }
 }
 
@@ -442,7 +442,7 @@ static int decode_hrd(GetBitContext *gb, int common_inf_present,
     return 0;
 }
 
-static void hevc_vps_free(FFRefStructOpaque opaque, void *obj)
+static void hevc_vps_free(AVRefStructOpaque opaque, void *obj)
 {
     HEVCVPS *vps = obj;
 
@@ -730,7 +730,7 @@ int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
             return 0;
     }
 
-    vps = ff_refstruct_alloc_ext(sizeof(*vps), 0, NULL, hevc_vps_free);
+    vps = av_refstruct_alloc_ext(sizeof(*vps), 0, NULL, hevc_vps_free);
     if (!vps)
         return AVERROR(ENOMEM);
 
@@ -864,7 +864,7 @@ int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
     return 0;
 
 err:
-    ff_refstruct_unref(&vps);
+    av_refstruct_unref(&vps);
     return ret;
 }
 
@@ -1171,7 +1171,7 @@ int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
                    sps->vps_id);
             return AVERROR_INVALIDDATA;
         }
-        sps->vps = ff_refstruct_ref_c(vps_list[sps->vps_id]);
+        sps->vps = av_refstruct_ref_c(vps_list[sps->vps_id]);
     }
 
     sps->max_sub_layers = get_bits(gb, 3) + 1;
@@ -1632,11 +1632,11 @@ int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
     return 0;
 }
 
-static void hevc_sps_free(FFRefStructOpaque opaque, void *obj)
+static void hevc_sps_free(AVRefStructOpaque opaque, void *obj)
 {
     HEVCSPS *sps = obj;
 
-    ff_refstruct_unref(&sps->vps);
+    av_refstruct_unref(&sps->vps);
 
     av_freep(&sps->data);
 }
@@ -1651,7 +1651,7 @@ int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
                            HEVCParamSets *ps, unsigned nuh_layer_id,
                            int apply_defdispwin)
 {
-    HEVCSPS *sps = ff_refstruct_alloc_ext(sizeof(*sps), 0, NULL, hevc_sps_free);
+    HEVCSPS *sps = av_refstruct_alloc_ext(sizeof(*sps), 0, NULL, hevc_sps_free);
     unsigned int sps_id;
     int ret;
 
@@ -1688,7 +1688,7 @@ int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
      * otherwise drop all PPSes that depend on it */
     if (ps->sps_list[sps_id] &&
         compare_sps(ps->sps_list[sps_id], sps)) {
-        ff_refstruct_unref(&sps);
+        av_refstruct_unref(&sps);
     } else {
         remove_sps(ps, sps_id);
         ps->sps_list[sps_id] = sps;
@@ -1696,15 +1696,15 @@ int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
 
     return 0;
 err:
-    ff_refstruct_unref(&sps);
+    av_refstruct_unref(&sps);
     return ret;
 }
 
-static void hevc_pps_free(FFRefStructOpaque unused, void *obj)
+static void hevc_pps_free(AVRefStructOpaque unused, void *obj)
 {
     HEVCPPS *pps = obj;
 
-    ff_refstruct_unref(&pps->sps);
+    av_refstruct_unref(&pps->sps);
 
     av_freep(&pps->column_width);
     av_freep(&pps->row_height);
@@ -2134,7 +2134,7 @@ int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx,
             return 0;
     }
 
-    pps = ff_refstruct_alloc_ext(sizeof(*pps), 0, NULL, hevc_pps_free);
+    pps = av_refstruct_alloc_ext(sizeof(*pps), 0, NULL, hevc_pps_free);
     if (!pps)
         return AVERROR(ENOMEM);
 
@@ -2171,7 +2171,7 @@ int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx,
     sps = ps->sps_list[pps->sps_id];
     vps = ps->vps_list[sps->vps_id];
 
-    pps->sps = ff_refstruct_ref_c(sps);
+    pps->sps = av_refstruct_ref_c(sps);
 
     pps->dependent_slice_segments_enabled_flag = get_bits1(gb);
     pps->output_flag_present_flag              = get_bits1(gb);
@@ -2371,13 +2371,13 @@ int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx,
                "Overread PPS by %d bits\n", -get_bits_left(gb));
     }
 
-    ff_refstruct_unref(&ps->pps_list[pps_id]);
+    av_refstruct_unref(&ps->pps_list[pps_id]);
     ps->pps_list[pps_id] = pps;
 
     return 0;
 
 err:
-    ff_refstruct_unref(&pps);
+    av_refstruct_unref(&pps);
     return ret;
 }
 
@@ -2386,11 +2386,11 @@ void ff_hevc_ps_uninit(HEVCParamSets *ps)
     int i;
 
     for (i = 0; i < FF_ARRAY_ELEMS(ps->vps_list); i++)
-        ff_refstruct_unref(&ps->vps_list[i]);
+        av_refstruct_unref(&ps->vps_list[i]);
     for (i = 0; i < FF_ARRAY_ELEMS(ps->sps_list); i++)
-        ff_refstruct_unref(&ps->sps_list[i]);
+        av_refstruct_unref(&ps->sps_list[i]);
     for (i = 0; i < FF_ARRAY_ELEMS(ps->pps_list); i++)
-        ff_refstruct_unref(&ps->pps_list[i]);
+        av_refstruct_unref(&ps->pps_list[i]);
 }
 
 int ff_hevc_compute_poc(const HEVCSPS *sps, int pocTid0, int poc_lsb, int nal_unit_type)
diff --git a/libavcodec/hevc/refs.c b/libavcodec/hevc/refs.c
index 6ba667e9f5..a467786f38 100644
--- a/libavcodec/hevc/refs.c
+++ b/libavcodec/hevc/refs.c
@@ -29,7 +29,7 @@
 #include "hevc.h"
 #include "hevcdec.h"
 #include "progressframe.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 void ff_hevc_unref_frame(HEVCFrame *frame, int flags)
 {
@@ -39,15 +39,15 @@ void ff_hevc_unref_frame(HEVCFrame *frame, int flags)
         av_frame_unref(frame->frame_grain);
         frame->needs_fg = 0;
 
-        ff_refstruct_unref(&frame->pps);
-        ff_refstruct_unref(&frame->tab_mvf);
+        av_refstruct_unref(&frame->pps);
+        av_refstruct_unref(&frame->tab_mvf);
 
-        ff_refstruct_unref(&frame->rpl);
+        av_refstruct_unref(&frame->rpl);
         frame->nb_rpl_elems = 0;
-        ff_refstruct_unref(&frame->rpl_tab);
+        av_refstruct_unref(&frame->rpl_tab);
         frame->refPicList = NULL;
 
-        ff_refstruct_unref(&frame->hwaccel_picture_private);
+        av_refstruct_unref(&frame->hwaccel_picture_private);
     }
 }
 
@@ -135,16 +135,16 @@ static HEVCFrame *alloc_frame(HEVCContext *s, HEVCLayerContext *l)
         if (ret < 0)
             return NULL;
 
-        frame->rpl = ff_refstruct_allocz(s->pkt.nb_nals * sizeof(*frame->rpl));
+        frame->rpl = av_refstruct_allocz(s->pkt.nb_nals * sizeof(*frame->rpl));
         if (!frame->rpl)
             goto fail;
         frame->nb_rpl_elems = s->pkt.nb_nals;
 
-        frame->tab_mvf = ff_refstruct_pool_get(l->tab_mvf_pool);
+        frame->tab_mvf = av_refstruct_pool_get(l->tab_mvf_pool);
         if (!frame->tab_mvf)
             goto fail;
 
-        frame->rpl_tab = ff_refstruct_pool_get(l->rpl_tab_pool);
+        frame->rpl_tab = av_refstruct_pool_get(l->rpl_tab_pool);
         if (!frame->rpl_tab)
             goto fail;
         frame->ctb_count = l->sps->ctb_width * l->sps->ctb_height;
@@ -161,7 +161,7 @@ static HEVCFrame *alloc_frame(HEVCContext *s, HEVCLayerContext *l)
         if (ret < 0)
             goto fail;
 
-        frame->pps = ff_refstruct_ref_c(s->pps);
+        frame->pps = av_refstruct_ref_c(s->pps);
 
         return frame;
 fail:
diff --git a/libavcodec/hwaccel_internal.h b/libavcodec/hwaccel_internal.h
index b0cc22bb68..77df4e0904 100644
--- a/libavcodec/hwaccel_internal.h
+++ b/libavcodec/hwaccel_internal.h
@@ -26,7 +26,7 @@
 #include <stdint.h>
 
 #include "avcodec.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 #define HWACCEL_CAP_ASYNC_SAFE      (1 << 0)
 #define HWACCEL_CAP_THREAD_SAFE     (1 << 1)
@@ -155,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)(FFRefStructOpaque hwctx, void *data);
+    void (*free_frame_priv)(AVRefStructOpaque hwctx, void *data);
 
     /**
      * Callback to flush the hwaccel state.
diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index 98ab2797ce..62a37f473f 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -68,7 +68,7 @@ typedef struct AVCodecInternal {
 
     struct FramePool *pool;
 
-    struct FFRefStructPool *progress_frame_pool;
+    struct AVRefStructPool *progress_frame_pool;
 
     void *thread_ctx;
 
diff --git a/libavcodec/lcevcdec.c b/libavcodec/lcevcdec.c
index ceeece3aa9..4ca5dfa0a6 100644
--- a/libavcodec/lcevcdec.c
+++ b/libavcodec/lcevcdec.c
@@ -23,6 +23,8 @@
 #include "libavutil/imgutils.h"
 #include "libavutil/log.h"
 #include "libavutil/mem.h"
+#include "libavutil/refstruct.h"
+
 #include "decode.h"
 #include "lcevcdec.h"
 
@@ -233,7 +235,7 @@ static void event_callback(LCEVC_DecoderHandle dec, LCEVC_Event event,
     }
 }
 
-static void lcevc_free(FFRefStructOpaque unused, void *obj)
+static void lcevc_free(AVRefStructOpaque unused, void *obj)
 {
     FFLCEVCContext *lcevc = obj;
     if (lcevc->initialized)
@@ -305,7 +307,7 @@ int ff_lcevc_alloc(FFLCEVCContext **plcevc)
 {
     FFLCEVCContext *lcevc = NULL;
 #if CONFIG_LIBLCEVC_DEC
-    lcevc = ff_refstruct_alloc_ext(sizeof(*lcevc), 0, NULL, lcevc_free);
+    lcevc = av_refstruct_alloc_ext(sizeof(*lcevc), 0, NULL, lcevc_free);
     if (!lcevc)
         return AVERROR(ENOMEM);
 #endif
@@ -315,5 +317,5 @@ int ff_lcevc_alloc(FFLCEVCContext **plcevc)
 
 void ff_lcevc_unref(void *opaque)
 {
-    ff_refstruct_unref(&opaque);
+    av_refstruct_unref(&opaque);
 }
diff --git a/libavcodec/lcevcdec.h b/libavcodec/lcevcdec.h
index 7334d3a645..147491ae42 100644
--- a/libavcodec/lcevcdec.h
+++ b/libavcodec/lcevcdec.h
@@ -27,7 +27,6 @@
 #else
 typedef uintptr_t LCEVC_DecoderHandle;
 #endif
-#include "refstruct.h"
 
 typedef struct FFLCEVCContext {
     LCEVC_DecoderHandle decoder;
diff --git a/libavcodec/mpegpicture.c b/libavcodec/mpegpicture.c
index cde060aa1f..6e96389c34 100644
--- a/libavcodec/mpegpicture.c
+++ b/libavcodec/mpegpicture.c
@@ -26,24 +26,24 @@
 
 #include "avcodec.h"
 #include "mpegpicture.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
-static void mpv_pic_reset(FFRefStructOpaque unused, void *obj)
+static void mpv_pic_reset(AVRefStructOpaque unused, void *obj)
 {
     MPVPicture *pic = obj;
 
     av_frame_unref(pic->f);
     ff_thread_progress_reset(&pic->progress);
 
-    ff_refstruct_unref(&pic->hwaccel_picture_private);
+    av_refstruct_unref(&pic->hwaccel_picture_private);
 
-    ff_refstruct_unref(&pic->mbskip_table);
-    ff_refstruct_unref(&pic->qscale_table_base);
-    ff_refstruct_unref(&pic->mb_type_base);
+    av_refstruct_unref(&pic->mbskip_table);
+    av_refstruct_unref(&pic->qscale_table_base);
+    av_refstruct_unref(&pic->mb_type_base);
 
     for (int i = 0; i < 2; i++) {
-        ff_refstruct_unref(&pic->motion_val_base[i]);
-        ff_refstruct_unref(&pic->ref_index[i]);
+        av_refstruct_unref(&pic->motion_val_base[i]);
+        av_refstruct_unref(&pic->ref_index[i]);
 
         pic->motion_val[i] = NULL;
     }
@@ -64,7 +64,7 @@ static void mpv_pic_reset(FFRefStructOpaque unused, void *obj)
     pic->coded_picture_number   = 0;
 }
 
-static int av_cold mpv_pic_init(FFRefStructOpaque opaque, void *obj)
+static int av_cold mpv_pic_init(AVRefStructOpaque opaque, void *obj)
 {
     MPVPicture *pic = obj;
     int ret, init_progress = (uintptr_t)opaque.nc;
@@ -79,7 +79,7 @@ static int av_cold mpv_pic_init(FFRefStructOpaque opaque, void *obj)
     return 0;
 }
 
-static void av_cold mpv_pic_free(FFRefStructOpaque unused, void *obj)
+static void av_cold mpv_pic_free(AVRefStructOpaque unused, void *obj)
 {
     MPVPicture *pic = obj;
 
@@ -87,17 +87,17 @@ static void av_cold mpv_pic_free(FFRefStructOpaque unused, void *obj)
     av_frame_free(&pic->f);
 }
 
-av_cold FFRefStructPool *ff_mpv_alloc_pic_pool(int init_progress)
+av_cold AVRefStructPool *ff_mpv_alloc_pic_pool(int init_progress)
 {
-    return ff_refstruct_pool_alloc_ext(sizeof(MPVPicture),
-                                       FF_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR,
+    return av_refstruct_pool_alloc_ext(sizeof(MPVPicture),
+                                       AV_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR,
                                        (void*)(uintptr_t)init_progress,
                                        mpv_pic_init, mpv_pic_reset, mpv_pic_free, NULL);
 }
 
 void ff_mpv_unref_picture(MPVWorkPicture *pic)
 {
-    ff_refstruct_unref(&pic->ptr);
+    av_refstruct_unref(&pic->ptr);
     memset(pic, 0, sizeof(*pic));
 }
 
@@ -121,13 +121,13 @@ static void set_workpic_from_pic(MPVWorkPicture *wpic, const MPVPicture *pic)
 void ff_mpv_replace_picture(MPVWorkPicture *dst, const MPVWorkPicture *src)
 {
     av_assert1(dst != src);
-    ff_refstruct_replace(&dst->ptr, src->ptr);
+    av_refstruct_replace(&dst->ptr, src->ptr);
     memcpy(dst, src, sizeof(*dst));
 }
 
 void ff_mpv_workpic_from_pic(MPVWorkPicture *wpic, MPVPicture *pic)
 {
-    ff_refstruct_replace(&wpic->ptr, pic);
+    av_refstruct_replace(&wpic->ptr, pic);
     if (!pic) {
         memset(wpic, 0, sizeof(*wpic));
         return;
@@ -207,7 +207,7 @@ static int alloc_picture_tables(BufferPoolContext *pools, MPVPicture *pic,
                                 int mb_height)
 {
 #define GET_BUFFER(name, buf_suffix, idx_suffix) do { \
-    pic->name ## buf_suffix idx_suffix = ff_refstruct_pool_get(pools->name ## _pool); \
+    pic->name ## buf_suffix idx_suffix = av_refstruct_pool_get(pools->name ## _pool); \
     if (!pic->name ## buf_suffix idx_suffix) \
         return AVERROR(ENOMEM); \
 } while (0)
diff --git a/libavcodec/mpegpicture.h b/libavcodec/mpegpicture.h
index 196aa9b744..d24093656a 100644
--- a/libavcodec/mpegpicture.h
+++ b/libavcodec/mpegpicture.h
@@ -42,11 +42,11 @@ typedef struct ScratchpadContext {
 } ScratchpadContext;
 
 typedef struct BufferPoolContext {
-    struct FFRefStructPool *mbskip_table_pool;
-    struct FFRefStructPool *qscale_table_pool;
-    struct FFRefStructPool *mb_type_pool;
-    struct FFRefStructPool *motion_val_pool;
-    struct FFRefStructPool *ref_index_pool;
+    struct AVRefStructPool *mbskip_table_pool;
+    struct AVRefStructPool *qscale_table_pool;
+    struct AVRefStructPool *mb_type_pool;
+    struct AVRefStructPool *motion_val_pool;
+    struct AVRefStructPool *ref_index_pool;
     int alloc_mb_width;                         ///< mb_width  used to allocate tables
     int alloc_mb_height;                        ///< mb_height used to allocate tables
     int alloc_mb_stride;                        ///< mb_stride used to allocate tables
@@ -114,7 +114,7 @@ typedef struct MPVWorkPicture {
 /**
  * Allocate a pool of MPVPictures.
  */
-struct FFRefStructPool *ff_mpv_alloc_pic_pool(int init_progress);
+struct AVRefStructPool *ff_mpv_alloc_pic_pool(int init_progress);
 
 /**
  * Allocate an MPVPicture's accessories (but not the AVFrame's buffer itself)
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 01e310e483..804c8095b2 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -41,7 +41,7 @@
 #include "mpegutils.h"
 #include "mpegvideo.h"
 #include "mpegvideodata.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
                                    int16_t *block, int n, int qscale)
@@ -499,11 +499,11 @@ void ff_mpv_common_defaults(MpegEncContext *s)
 
 static void free_buffer_pools(BufferPoolContext *pools)
 {
-    ff_refstruct_pool_uninit(&pools->mbskip_table_pool);
-    ff_refstruct_pool_uninit(&pools->qscale_table_pool);
-    ff_refstruct_pool_uninit(&pools->mb_type_pool);
-    ff_refstruct_pool_uninit(&pools->motion_val_pool);
-    ff_refstruct_pool_uninit(&pools->ref_index_pool);
+    av_refstruct_pool_uninit(&pools->mbskip_table_pool);
+    av_refstruct_pool_uninit(&pools->qscale_table_pool);
+    av_refstruct_pool_uninit(&pools->mb_type_pool);
+    av_refstruct_pool_uninit(&pools->motion_val_pool);
+    av_refstruct_pool_uninit(&pools->ref_index_pool);
     pools->alloc_mb_height = pools->alloc_mb_width = pools->alloc_mb_stride = 0;
 }
 
@@ -557,7 +557,7 @@ int ff_mpv_init_context_frame(MpegEncContext *s)
     s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
 
 #define ALLOC_POOL(name, size, flags) do { \
-    pools->name ##_pool = ff_refstruct_pool_alloc((size), (flags)); \
+    pools->name ##_pool = av_refstruct_pool_alloc((size), (flags)); \
     if (!pools->name ##_pool) \
         return AVERROR(ENOMEM); \
 } while (0)
@@ -578,7 +578,7 @@ int ff_mpv_init_context_frame(MpegEncContext *s)
         }
         if (s->codec_id == AV_CODEC_ID_MPEG4) {
             ALLOC_POOL(mbskip_table, mb_array_size + 2,
-                       !s->encoding ? FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME : 0);
+                       !s->encoding ? AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME : 0);
             if (!s->encoding) {
                 /* cbp, pred_dir */
                 if (!(s->cbp_table      = av_mallocz(mb_array_size)) ||
@@ -626,7 +626,7 @@ int ff_mpv_init_context_frame(MpegEncContext *s)
         /* FIXME: The output of H.263 with OBMC depends upon
          * the earlier content of the buffer; therefore we set
          * the flags to always reset returned buffers here. */
-        ALLOC_POOL(motion_val, mv_size, FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME);
+        ALLOC_POOL(motion_val, mv_size, AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME);
         ALLOC_POOL(ref_index, ref_index_size, 0);
     }
 #undef ALLOC_POOL
diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index 8083299b66..eb71198190 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -128,7 +128,7 @@ typedef struct MpegEncContext {
     int mb_num;                ///< number of MBs of a picture
     ptrdiff_t linesize;        ///< line size, in bytes, may be different from width
     ptrdiff_t uvlinesize;      ///< line size, for chroma in bytes, may be different from width
-    struct FFRefStructPool *picture_pool; ///< Pool for MPVPictures
+    struct AVRefStructPool *picture_pool; ///< Pool for MPVPictures
     MPVPicture **input_picture;///< next pictures on display order for encoding
     MPVPicture **reordered_input_picture; ///< pointer to the next pictures in coded order for encoding
 
diff --git a/libavcodec/mpegvideo_dec.c b/libavcodec/mpegvideo_dec.c
index 1cab108935..3684511fd7 100644
--- a/libavcodec/mpegvideo_dec.c
+++ b/libavcodec/mpegvideo_dec.c
@@ -38,7 +38,7 @@
 #include "mpegvideo.h"
 #include "mpegvideodec.h"
 #include "mpeg4videodec.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "thread.h"
 #include "threadprogress.h"
 #include "wmv2dec.h"
@@ -164,7 +164,7 @@ int ff_mpv_decode_close(AVCodecContext *avctx)
 {
     MpegEncContext *s = avctx->priv_data;
 
-    ff_refstruct_pool_uninit(&s->picture_pool);
+    av_refstruct_pool_uninit(&s->picture_pool);
     ff_mpv_common_end(s);
     return 0;
 }
@@ -216,7 +216,7 @@ int ff_mpv_common_frame_size_change(MpegEncContext *s)
 static int alloc_picture(MpegEncContext *s, MPVWorkPicture *dst, int reference)
 {
     AVCodecContext *avctx = s->avctx;
-    MPVPicture *pic = ff_refstruct_pool_get(s->picture_pool);
+    MPVPicture *pic = av_refstruct_pool_get(s->picture_pool);
     int ret;
 
     if (!pic)
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index a332edd1ae..c5f20c2d85 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -75,7 +75,7 @@
 #include "wmv2enc.h"
 #include "rv10enc.h"
 #include "packet_internal.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include <limits.h>
 #include "sp5x.h"
 
@@ -1033,12 +1033,12 @@ av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
     ff_rate_control_uninit(&s->rc_context);
 
     ff_mpv_common_end(s);
-    ff_refstruct_pool_uninit(&s->picture_pool);
+    av_refstruct_pool_uninit(&s->picture_pool);
 
     if (s->input_picture && s->reordered_input_picture) {
         for (int i = 0; i < MAX_B_FRAMES + 1; i++) {
-            ff_refstruct_unref(&s->input_picture[i]);
-            ff_refstruct_unref(&s->reordered_input_picture[i]);
+            av_refstruct_unref(&s->input_picture[i]);
+            av_refstruct_unref(&s->reordered_input_picture[i]);
         }
     }
     for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
@@ -1232,7 +1232,7 @@ static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
         ff_dlog(s->avctx, "%d %d %"PTRDIFF_SPECIFIER" %"PTRDIFF_SPECIFIER"\n", pic_arg->linesize[0],
                 pic_arg->linesize[1], s->linesize, s->uvlinesize);
 
-        pic = ff_refstruct_pool_get(s->picture_pool);
+        pic = av_refstruct_pool_get(s->picture_pool);
         if (!pic)
             return AVERROR(ENOMEM);
 
@@ -1311,7 +1311,7 @@ static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
 
     return 0;
 fail:
-    ff_refstruct_unref(&pic);
+    av_refstruct_unref(&pic);
     return ret;
 }
 
@@ -1541,7 +1541,7 @@ static int set_bframe_chain_length(MpegEncContext *s)
             s->next_pic.ptr &&
             skip_check(s, s->input_picture[0], s->next_pic.ptr)) {
             // FIXME check that the gop check above is +-1 correct
-            ff_refstruct_unref(&s->input_picture[0]);
+            av_refstruct_unref(&s->input_picture[0]);
 
             ff_vbv_update(s, 0);
 
@@ -1607,7 +1607,7 @@ static int set_bframe_chain_length(MpegEncContext *s)
         } else if (s->b_frame_strategy == 2) {
             b_frames = estimate_best_b_count(s);
             if (b_frames < 0) {
-                ff_refstruct_unref(&s->input_picture[0]);
+                av_refstruct_unref(&s->input_picture[0]);
                 return b_frames;
             }
         }
@@ -1714,7 +1714,7 @@ static int select_input_picture(MpegEncContext *s)
     }
     return 0;
 fail:
-    ff_refstruct_unref(&s->reordered_input_picture[0]);
+    av_refstruct_unref(&s->reordered_input_picture[0]);
     return ret;
 }
 
diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c
index 932544564a..db9d353c61 100644
--- a/libavcodec/nvdec.c
+++ b/libavcodec/nvdec.c
@@ -36,7 +36,7 @@
 #include "decode.h"
 #include "nvdec.h"
 #include "internal.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 #if !NVDECAPI_CHECK_VERSION(9, 0)
 #define cudaVideoSurfaceFormat_YUV444 2
@@ -163,7 +163,7 @@ static int nvdec_test_capabilities(NVDECDecoder *decoder,
     return 0;
 }
 
-static void nvdec_decoder_free(FFRefStructOpaque unused, void *obj)
+static void nvdec_decoder_free(AVRefStructOpaque unused, void *obj)
 {
     NVDECDecoder *decoder = obj;
 
@@ -192,7 +192,7 @@ static int nvdec_decoder_create(NVDECDecoder **out, AVBufferRef *hw_device_ref,
     CUcontext dummy;
     int ret;
 
-    decoder = ff_refstruct_alloc_ext(sizeof(*decoder), 0,
+    decoder = av_refstruct_alloc_ext(sizeof(*decoder), 0,
                                      NULL, nvdec_decoder_free);
     if (!decoder)
         return AVERROR(ENOMEM);
@@ -234,11 +234,11 @@ static int nvdec_decoder_create(NVDECDecoder **out, AVBufferRef *hw_device_ref,
 
     return 0;
 fail:
-    ff_refstruct_unref(&decoder);
+    av_refstruct_unref(&decoder);
     return ret;
 }
 
-static int nvdec_decoder_frame_init(FFRefStructOpaque opaque, void *obj)
+static int nvdec_decoder_frame_init(AVRefStructOpaque opaque, void *obj)
 {
     NVDECFramePool *pool = opaque.nc;
     unsigned int *intp = obj;
@@ -251,7 +251,7 @@ static int nvdec_decoder_frame_init(FFRefStructOpaque opaque, void *obj)
     return 0;
 }
 
-static void nvdec_decoder_frame_pool_free(FFRefStructOpaque opaque)
+static void nvdec_decoder_frame_pool_free(AVRefStructOpaque opaque)
 {
     av_free(opaque.nc);
 }
@@ -269,8 +269,8 @@ int ff_nvdec_decode_uninit(AVCodecContext *avctx)
     ctx->nb_slices               = 0;
     ctx->slice_offsets_allocated = 0;
 
-    ff_refstruct_unref(&ctx->decoder);
-    ff_refstruct_pool_uninit(&ctx->decoder_pool);
+    av_refstruct_unref(&ctx->decoder);
+    av_refstruct_pool_uninit(&ctx->decoder_pool);
 
     return 0;
 }
@@ -426,7 +426,7 @@ int ff_nvdec_decode_init(AVCodecContext *avctx)
     }
     pool->dpb_size = frames_ctx->initial_pool_size;
 
-    ctx->decoder_pool = ff_refstruct_pool_alloc_ext(sizeof(unsigned int), 0, pool,
+    ctx->decoder_pool = av_refstruct_pool_alloc_ext(sizeof(unsigned int), 0, pool,
                                                     nvdec_decoder_frame_init,
                                                     NULL, NULL, nvdec_decoder_frame_pool_free);
     if (!ctx->decoder_pool) {
@@ -447,9 +447,9 @@ static void nvdec_fdd_priv_free(void *priv)
     if (!cf)
         return;
 
-    ff_refstruct_unref(&cf->idx_ref);
-    ff_refstruct_unref(&cf->ref_idx_ref);
-    ff_refstruct_unref(&cf->decoder);
+    av_refstruct_unref(&cf->idx_ref);
+    av_refstruct_unref(&cf->ref_idx_ref);
+    av_refstruct_unref(&cf->decoder);
 
     av_freep(&priv);
 }
@@ -472,9 +472,9 @@ static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data)
     CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
 
 finish:
-    ff_refstruct_unref(&unmap_data->idx_ref);
-    ff_refstruct_unref(&unmap_data->ref_idx_ref);
-    ff_refstruct_unref(&unmap_data->decoder);
+    av_refstruct_unref(&unmap_data->idx_ref);
+    av_refstruct_unref(&unmap_data->ref_idx_ref);
+    av_refstruct_unref(&unmap_data->decoder);
     av_free(unmap_data);
 }
 
@@ -529,8 +529,8 @@ static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
         goto copy_fail;
 
     unmap_data->idx = cf->idx;
-    unmap_data->idx_ref = ff_refstruct_ref(cf->idx_ref);
-    unmap_data->decoder = ff_refstruct_ref(cf->decoder);
+    unmap_data->idx_ref = av_refstruct_ref(cf->idx_ref);
+    unmap_data->decoder = av_refstruct_ref(cf->decoder);
 
     av_pix_fmt_get_chroma_sub_sample(hwctx->sw_format, &shift_h, &shift_v);
     for (i = 0; frame->linesize[i]; i++) {
@@ -575,9 +575,9 @@ int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
     if (!cf)
         return AVERROR(ENOMEM);
 
-    cf->decoder = ff_refstruct_ref(ctx->decoder);
+    cf->decoder = av_refstruct_ref(ctx->decoder);
 
-    cf->idx_ref = ff_refstruct_pool_get(ctx->decoder_pool);
+    cf->idx_ref = av_refstruct_pool_get(ctx->decoder_pool);
     if (!cf->idx_ref) {
         av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
         ret = AVERROR(ENOMEM);
@@ -611,7 +611,7 @@ int ff_nvdec_start_frame_sep_ref(AVCodecContext *avctx, AVFrame *frame, int has_
 
     if (has_sep_ref) {
         if (!cf->ref_idx_ref) {
-            cf->ref_idx_ref = ff_refstruct_pool_get(ctx->decoder_pool);
+            cf->ref_idx_ref = av_refstruct_pool_get(ctx->decoder_pool);
             if (!cf->ref_idx_ref) {
                 av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n");
                 ret = AVERROR(ENOMEM);
@@ -620,7 +620,7 @@ int ff_nvdec_start_frame_sep_ref(AVCodecContext *avctx, AVFrame *frame, int has_
         }
         cf->ref_idx = *cf->ref_idx_ref;
     } else {
-        ff_refstruct_unref(&cf->ref_idx_ref);
+        av_refstruct_unref(&cf->ref_idx_ref);
         cf->ref_idx = cf->idx;
     }
 
diff --git a/libavcodec/nvdec.h b/libavcodec/nvdec.h
index 555300d27d..5e22f672d1 100644
--- a/libavcodec/nvdec.h
+++ b/libavcodec/nvdec.h
@@ -52,7 +52,7 @@ typedef struct NVDECFrame {
 typedef struct NVDECContext {
     CUVIDPICPARAMS pic_params;
 
-    struct FFRefStructPool *decoder_pool;
+    struct AVRefStructPool *decoder_pool;
 
     struct NVDECDecoder  *decoder; ///< RefStruct reference
 
diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c
index 1b1b96623f..4960352307 100644
--- a/libavcodec/pthread_frame.c
+++ b/libavcodec/pthread_frame.c
@@ -34,7 +34,7 @@
 #include "internal.h"
 #include "packet_internal.h"
 #include "pthread_internal.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "thread.h"
 #include "threadframe.h"
 #include "version_major.h"
@@ -405,7 +405,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
 
         dst->hwaccel_flags = src->hwaccel_flags;
 
-        ff_refstruct_replace(&dst->internal->pool, src->internal->pool);
+        av_refstruct_replace(&dst->internal->pool, src->internal->pool);
         ff_decode_internal_sync(dst, src);
     }
 
@@ -780,7 +780,7 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
                 av_freep(&ctx->priv_data);
             }
 
-            ff_refstruct_unref(&ctx->internal->pool);
+            av_refstruct_unref(&ctx->internal->pool);
             av_packet_free(&ctx->internal->in_pkt);
             av_packet_free(&ctx->internal->last_pkt_props);
             ff_decode_internal_uninit(ctx);
@@ -1062,7 +1062,7 @@ int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
     if (!(avctx->active_thread_type & FF_THREAD_FRAME))
         return ff_get_buffer(avctx, f->f, flags);
 
-    f->progress = ff_refstruct_allocz(sizeof(*f->progress));
+    f->progress = av_refstruct_allocz(sizeof(*f->progress));
     if (!f->progress)
         return AVERROR(ENOMEM);
 
@@ -1071,13 +1071,13 @@ int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
 
     ret = ff_thread_get_buffer(avctx, f->f, flags);
     if (ret)
-        ff_refstruct_unref(&f->progress);
+        av_refstruct_unref(&f->progress);
     return ret;
 }
 
 void ff_thread_release_ext_buffer(ThreadFrame *f)
 {
-    ff_refstruct_unref(&f->progress);
+    av_refstruct_unref(&f->progress);
     f->owner[0] = f->owner[1] = NULL;
     if (f->f)
         av_frame_unref(f->f);
@@ -1098,7 +1098,7 @@ enum ThreadingStatus ff_thread_sync_ref(AVCodecContext *avctx, size_t offset)
 
     memcpy(&ref, (const char*)p->parent->threads[0].avctx->priv_data + offset, sizeof(ref));
     av_assert1(ref);
-    ff_refstruct_replace((char*)avctx->priv_data + offset, ref);
+    av_refstruct_replace((char*)avctx->priv_data + offset, ref);
 
     return FF_THREAD_IS_COPY;
 }
diff --git a/libavcodec/qsv.c b/libavcodec/qsv.c
index 8a3dc95706..cd5195a54b 100644
--- a/libavcodec/qsv.c
+++ b/libavcodec/qsv.c
@@ -35,7 +35,7 @@
 
 #include "avcodec.h"
 #include "qsv_internal.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 #define MFX_IMPL_VIA_MASK(impl) (0x0f00 & (impl))
 #define QSV_HAVE_USER_PLUGIN    !QSV_ONEVPL
@@ -745,7 +745,7 @@ int ff_qsv_init_internal_session(AVCodecContext *avctx, QSVSession *qs,
     return 0;
 }
 
-static void mids_buf_free(FFRefStructOpaque opaque, void *obj)
+static void mids_buf_free(AVRefStructOpaque opaque, void *obj)
 {
     AVBufferRef *hw_frames_ref = opaque.nc;
     av_buffer_unref(&hw_frames_ref);
@@ -765,7 +765,7 @@ static QSVMid *qsv_create_mids(AVBufferRef *hw_frames_ref)
     if (!hw_frames_ref1)
         return NULL;
 
-    mids = ff_refstruct_alloc_ext(nb_surfaces * sizeof(*mids), 0,
+    mids = av_refstruct_alloc_ext(nb_surfaces * sizeof(*mids), 0,
                                   hw_frames_ref1, mids_buf_free);
     if (!mids) {
         av_buffer_unref(&hw_frames_ref1);
@@ -806,7 +806,7 @@ static int qsv_setup_mids(mfxFrameAllocResponse *resp, AVBufferRef *hw_frames_re
         return AVERROR(ENOMEM);
     }
 
-    resp->mids[resp->NumFrameActual + 1] = ff_refstruct_ref(mids);
+    resp->mids[resp->NumFrameActual + 1] = av_refstruct_ref(mids);
 
     return 0;
 }
@@ -899,7 +899,7 @@ static mfxStatus qsv_frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
         }
 
         ret = qsv_setup_mids(resp, frames_ref, mids);
-        ff_refstruct_unref(&mids);
+        av_refstruct_unref(&mids);
         av_buffer_unref(&frames_ref);
         if (ret < 0) {
             av_log(ctx->logctx, AV_LOG_ERROR,
@@ -919,7 +919,7 @@ static mfxStatus qsv_frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
         return MFX_ERR_NONE;
 
     av_buffer_unref((AVBufferRef**)&resp->mids[resp->NumFrameActual]);
-    ff_refstruct_unref(&resp->mids[resp->NumFrameActual + 1]);
+    av_refstruct_unref(&resp->mids[resp->NumFrameActual + 1]);
     av_freep(&resp->mids);
     return MFX_ERR_NONE;
 }
@@ -1139,7 +1139,7 @@ int ff_qsv_init_session_frames(AVCodecContext *avctx, mfxSession *psession,
 
         /* allocate the memory ids for the external frames */
         if (frames_hwctx->nb_surfaces) {
-            ff_refstruct_unref(&qsv_frames_ctx->mids);
+            av_refstruct_unref(&qsv_frames_ctx->mids);
             qsv_frames_ctx->mids = qsv_create_mids(qsv_frames_ctx->hw_frames_ctx);
             if (!qsv_frames_ctx->mids)
                 return AVERROR(ENOMEM);
diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c
index 039ba62484..19fa791c10 100644
--- a/libavcodec/qsvdec.c
+++ b/libavcodec/qsvdec.c
@@ -51,7 +51,7 @@
 #include "hwconfig.h"
 #include "qsv.h"
 #include "qsv_internal.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 #if QSV_ONEVPL
 #include <mfxdispatcher.h>
@@ -984,7 +984,7 @@ static void qsv_decode_close_qsvcontext(QSVContext *q)
     ff_qsv_close_internal_session(&q->internal_qs);
 
     av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
-    ff_refstruct_unref(&q->frames_ctx.mids);
+    av_refstruct_unref(&q->frames_ctx.mids);
     av_buffer_pool_uninit(&q->pool);
 }
 
diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
index 8200a14012..f0b572ec29 100644
--- a/libavcodec/qsvenc.c
+++ b/libavcodec/qsvenc.c
@@ -42,7 +42,7 @@
 #include "qsv.h"
 #include "qsv_internal.h"
 #include "qsvenc.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 struct profile_names {
     mfxU16 profile;
@@ -2716,7 +2716,7 @@ int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q)
     ff_qsv_close_internal_session(&q->internal_qs);
 
     av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
-    ff_refstruct_unref(&q->frames_ctx.mids);
+    av_refstruct_unref(&q->frames_ctx.mids);
 
     cur = q->work_frames;
     while (cur) {
diff --git a/libavcodec/rkmppdec.c b/libavcodec/rkmppdec.c
index 47b076dbd8..07d299f144 100644
--- a/libavcodec/rkmppdec.c
+++ b/libavcodec/rkmppdec.c
@@ -30,7 +30,7 @@
 #include "codec_internal.h"
 #include "decode.h"
 #include "hwconfig.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "libavutil/buffer.h"
 #include "libavutil/common.h"
 #include "libavutil/frame.h"
@@ -126,11 +126,11 @@ static int rkmpp_write_data(AVCodecContext *avctx, uint8_t *buffer, int size, in
 static int rkmpp_close_decoder(AVCodecContext *avctx)
 {
     RKMPPDecodeContext *rk_context = avctx->priv_data;
-    ff_refstruct_unref(&rk_context->decoder);
+    av_refstruct_unref(&rk_context->decoder);
     return 0;
 }
 
-static void rkmpp_release_decoder(FFRefStructOpaque unused, void *obj)
+static void rkmpp_release_decoder(AVRefStructOpaque unused, void *obj)
 {
     RKMPPDecoder *decoder = obj;
 
@@ -161,7 +161,7 @@ static int rkmpp_init_decoder(AVCodecContext *avctx)
     avctx->pix_fmt = AV_PIX_FMT_DRM_PRIME;
 
     // create a decoder and a ref to it
-    decoder = ff_refstruct_alloc_ext(sizeof(*decoder), 0,
+    decoder = av_refstruct_alloc_ext(sizeof(*decoder), 0,
                                      NULL, rkmpp_release_decoder);
     if (!decoder) {
         ret = AVERROR(ENOMEM);
@@ -304,7 +304,7 @@ static void rkmpp_release_frame(void *opaque, uint8_t *data)
     RKMPPFrameContext *framecontext = opaque;
 
     mpp_frame_deinit(&framecontext->frame);
-    ff_refstruct_unref(&framecontext->decoder_ref);
+    av_refstruct_unref(&framecontext->decoder_ref);
 
     av_free(desc);
 }
@@ -449,7 +449,7 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame)
                 ret = AVERROR(ENOMEM);
                 goto fail;
             }
-            framecontext->decoder_ref = ff_refstruct_ref(rk_context->decoder);
+            framecontext->decoder_ref = av_refstruct_ref(rk_context->decoder);
 
             frame->hw_frames_ctx = av_buffer_ref(decoder->frames_ref);
             if (!frame->hw_frames_ctx) {
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 28023a4a4d..2dbd677920 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -40,7 +40,7 @@
 #include "codec_par.h"
 #include "decode.h"
 #include "hwconfig.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "thread.h"
 #include "threadframe.h"
 #include "internal.h"
@@ -856,7 +856,7 @@ int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
     av_assert0(!dst->progress);
 
     if (src->progress)
-        dst->progress = ff_refstruct_ref(src->progress);
+        dst->progress = av_refstruct_ref(src->progress);
 
     return 0;
 }
@@ -872,7 +872,7 @@ int ff_thread_replace_frame(ThreadFrame *dst, const ThreadFrame *src)
     if (ret < 0)
         return ret;
 
-    ff_refstruct_replace(&dst->progress, src->progress);
+    av_refstruct_replace(&dst->progress, src->progress);
 
     return 0;
 }
diff --git a/libavcodec/v4l2_buffers.c b/libavcodec/v4l2_buffers.c
index 23474ee143..aba9cfad83 100644
--- a/libavcodec/v4l2_buffers.c
+++ b/libavcodec/v4l2_buffers.c
@@ -29,7 +29,7 @@
 #include <poll.h>
 #include "libavcodec/avcodec.h"
 #include "libavutil/pixdesc.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "v4l2_context.h"
 #include "v4l2_buffers.h"
 #include "v4l2_m2m.h"
@@ -230,7 +230,7 @@ static void v4l2_free_buffer(void *opaque, uint8_t *unused)
                 ff_v4l2_buffer_enqueue(avbuf);
         }
 
-        ff_refstruct_unref(&avbuf->context_ref);
+        av_refstruct_unref(&avbuf->context_ref);
     }
 }
 
@@ -241,7 +241,7 @@ static int v4l2_buf_increase_ref(V4L2Buffer *in)
     if (in->context_ref)
         atomic_fetch_add(&in->context_refcount, 1);
     else {
-        in->context_ref = ff_refstruct_ref(s->self_ref);
+        in->context_ref = av_refstruct_ref(s->self_ref);
 
         in->context_refcount = 1;
     }
diff --git a/libavcodec/v4l2_m2m.c b/libavcodec/v4l2_m2m.c
index 15415cfc4e..8f633e2aeb 100644
--- a/libavcodec/v4l2_m2m.c
+++ b/libavcodec/v4l2_m2m.c
@@ -32,7 +32,7 @@
 #include "libavutil/pixdesc.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/pixfmt.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "v4l2_context.h"
 #include "v4l2_fmt.h"
 #include "v4l2_m2m.h"
@@ -248,7 +248,7 @@ int ff_v4l2_m2m_codec_reinit(V4L2m2mContext *s)
     return 0;
 }
 
-static void v4l2_m2m_destroy_context(FFRefStructOpaque unused, void *context)
+static void v4l2_m2m_destroy_context(AVRefStructOpaque unused, void *context)
 {
     V4L2m2mContext *s = context;
 
@@ -282,7 +282,7 @@ int ff_v4l2_m2m_codec_end(V4L2m2mPriv *priv)
     ff_v4l2_context_release(&s->output);
 
     s->self_ref = NULL;
-    ff_refstruct_unref(&priv->context);
+    av_refstruct_unref(&priv->context);
 
     return 0;
 }
@@ -327,7 +327,7 @@ int ff_v4l2_m2m_codec_init(V4L2m2mPriv *priv)
 
 int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, V4L2m2mContext **s)
 {
-    *s = ff_refstruct_alloc_ext(sizeof(**s), 0, NULL,
+    *s = av_refstruct_alloc_ext(sizeof(**s), 0, NULL,
                                 &v4l2_m2m_destroy_context);
     if (!*s)
         return AVERROR(ENOMEM);
@@ -344,7 +344,7 @@ int ff_v4l2_m2m_create_context(V4L2m2mPriv *priv, V4L2m2mContext **s)
 
     priv->context->frame = av_frame_alloc();
     if (!priv->context->frame) {
-        ff_refstruct_unref(&priv->context);
+        av_refstruct_unref(&priv->context);
         *s = NULL; /* freed when unreferencing context */
         return AVERROR(ENOMEM);
     }
diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index 16a9a364f0..b593d976ef 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -31,7 +31,7 @@
 #include "vaapi_encode.h"
 #include "encode.h"
 #include "avcodec.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 
 const AVCodecHWConfigInternal *const ff_vaapi_encode_hw_configs[] = {
     HW_CONFIG_ENCODER_FRAMES(VAAPI, VAAPI),
@@ -316,7 +316,7 @@ static int vaapi_encode_issue(AVCodecContext *avctx,
     pic->recon_surface = (VASurfaceID)(uintptr_t)base_pic->recon_image->data[3];
     av_log(avctx, AV_LOG_DEBUG, "Recon surface is %#x.\n", pic->recon_surface);
 
-    pic->output_buffer_ref = ff_refstruct_pool_get(ctx->output_buffer_pool);
+    pic->output_buffer_ref = av_refstruct_pool_get(ctx->output_buffer_pool);
     if (!pic->output_buffer_ref) {
         err = AVERROR(ENOMEM);
         goto fail;
@@ -649,7 +649,7 @@ fail_at_end:
     av_freep(&pic->param_buffers);
     av_freep(&pic->slices);
     av_freep(&pic->roi);
-    ff_refstruct_unref(&pic->output_buffer_ref);
+    av_refstruct_unref(&pic->output_buffer_ref);
     pic->output_buffer = VA_INVALID_ID;
     return err;
 }
@@ -759,8 +759,8 @@ static int vaapi_encode_get_coded_data(AVCodecContext *avctx,
         goto end;
 
 end:
-    ff_refstruct_unref(&ctx->coded_buffer_ref);
-    ff_refstruct_unref(&pic->output_buffer_ref);
+    av_refstruct_unref(&ctx->coded_buffer_ref);
+    av_refstruct_unref(&pic->output_buffer_ref);
     pic->output_buffer = VA_INVALID_ID;
 
     return ret;
@@ -781,7 +781,7 @@ static int vaapi_encode_output(AVCodecContext *avctx,
 
     if (pic->non_independent_frame) {
         av_assert0(!ctx->coded_buffer_ref);
-        ctx->coded_buffer_ref = ff_refstruct_ref(pic->output_buffer_ref);
+        ctx->coded_buffer_ref = av_refstruct_ref(pic->output_buffer_ref);
 
         if (pic->tail_size) {
             if (base_ctx->tail_pkt->size) {
@@ -809,7 +809,7 @@ static int vaapi_encode_output(AVCodecContext *avctx,
                                           ctx->codec->flags & FLAG_TIMESTAMP_NO_DELAY);
 
 end:
-    ff_refstruct_unref(&pic->output_buffer_ref);
+    av_refstruct_unref(&pic->output_buffer_ref);
     pic->output_buffer = VA_INVALID_ID;
     return err;
 }
@@ -825,7 +825,7 @@ static int vaapi_encode_discard(AVCodecContext *avctx, FFHWBaseEncodePicture *ba
                "%"PRId64"/%"PRId64".\n",
                base_pic->display_order, base_pic->encode_order);
 
-        ff_refstruct_unref(&pic->output_buffer_ref);
+        av_refstruct_unref(&pic->output_buffer_ref);
         pic->output_buffer = VA_INVALID_ID;
     }
 
@@ -1987,7 +1987,7 @@ static av_cold int vaapi_encode_init_roi(AVCodecContext *avctx)
     return 0;
 }
 
-static void vaapi_encode_free_output_buffer(FFRefStructOpaque opaque,
+static void vaapi_encode_free_output_buffer(AVRefStructOpaque opaque,
                                             void *obj)
 {
     AVCodecContext   *avctx = opaque.nc;
@@ -2000,7 +2000,7 @@ static void vaapi_encode_free_output_buffer(FFRefStructOpaque opaque,
     av_log(avctx, AV_LOG_DEBUG, "Freed output buffer %#x\n", buffer_id);
 }
 
-static int vaapi_encode_alloc_output_buffer(FFRefStructOpaque opaque, void *obj)
+static int vaapi_encode_alloc_output_buffer(AVRefStructOpaque opaque, void *obj)
 {
     AVCodecContext   *avctx = opaque.nc;
     FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
@@ -2187,7 +2187,7 @@ av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
     }
 
     ctx->output_buffer_pool =
-        ff_refstruct_pool_alloc_ext(sizeof(VABufferID), 0, avctx,
+        av_refstruct_pool_alloc_ext(sizeof(VABufferID), 0, avctx,
                                     &vaapi_encode_alloc_output_buffer, NULL,
                                     vaapi_encode_free_output_buffer, NULL);
     if (!ctx->output_buffer_pool) {
@@ -2288,7 +2288,7 @@ av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
         vaapi_encode_free(avctx, pic);
     }
 
-    ff_refstruct_pool_uninit(&ctx->output_buffer_pool);
+    av_refstruct_pool_uninit(&ctx->output_buffer_pool);
 
     if (ctx->va_context != VA_INVALID_ID) {
         if (ctx->hwctx)
diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h
index c4f85397a2..40a3f4e064 100644
--- a/libavcodec/vaapi_encode.h
+++ b/libavcodec/vaapi_encode.h
@@ -202,7 +202,7 @@ typedef struct VAAPIEncodeContext {
     AVVAAPIDeviceContext *hwctx;
 
     // Pool of (reusable) bitstream output buffers.
-    struct FFRefStructPool *output_buffer_pool;
+    struct AVRefStructPool *output_buffer_pool;
 
     // Global parameters which will be applied at the start of the
     // sequence (includes rate control parameters below).
diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c
index d03a1c9dbc..3089499a0f 100644
--- a/libavcodec/vp3.c
+++ b/libavcodec/vp3.c
@@ -50,7 +50,7 @@
 #include "jpegquanttables.h"
 #include "mathops.h"
 #include "progressframe.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "thread.h"
 #include "videodsp.h"
 #include "vp3data.h"
@@ -370,7 +370,7 @@ static av_cold int vp3_decode_end(AVCodecContext *avctx)
     /* release all frames */
     vp3_decode_flush(avctx);
 
-    ff_refstruct_unref(&s->coeff_vlc);
+    av_refstruct_unref(&s->coeff_vlc);
 
     return 0;
 }
@@ -2346,7 +2346,7 @@ static av_cold int allocate_tables(AVCodecContext *avctx)
 }
 
 
-static av_cold void free_vlc_tables(FFRefStructOpaque unused, void *obj)
+static av_cold void free_vlc_tables(AVRefStructOpaque unused, void *obj)
 {
     CoeffVLCs *vlcs = obj;
 
@@ -2459,7 +2459,7 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx)
     }
 
     if (!avctx->internal->is_copy) {
-        CoeffVLCs *vlcs = ff_refstruct_alloc_ext(sizeof(*s->coeff_vlc), 0,
+        CoeffVLCs *vlcs = av_refstruct_alloc_ext(sizeof(*s->coeff_vlc), 0,
                                                  NULL, free_vlc_tables);
         if (!vlcs)
             return AVERROR(ENOMEM);
@@ -2527,7 +2527,7 @@ static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *
     const Vp3DecodeContext *s1 = src->priv_data;
     int qps_changed = 0;
 
-    ff_refstruct_replace(&s->coeff_vlc, s1->coeff_vlc);
+    av_refstruct_replace(&s->coeff_vlc, s1->coeff_vlc);
 
     // copy previous frame data
     ref_frames(s, s1);
diff --git a/libavcodec/vp8.c b/libavcodec/vp8.c
index d6df018655..3651688c10 100644
--- a/libavcodec/vp8.c
+++ b/libavcodec/vp8.c
@@ -36,7 +36,7 @@
 #include "hwconfig.h"
 #include "mathops.h"
 #include "progressframe.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "thread.h"
 #include "vp8.h"
 #include "vp89_rac.h"
@@ -107,7 +107,7 @@ static int vp8_alloc_frame(VP8Context *s, VP8Frame *f, int ref)
                                            ref ? AV_GET_BUFFER_FLAG_REF : 0);
     if (ret < 0)
         return ret;
-    f->seg_map = ff_refstruct_allocz(s->mb_width * s->mb_height);
+    f->seg_map = av_refstruct_allocz(s->mb_width * s->mb_height);
     if (!f->seg_map) {
         ret = AVERROR(ENOMEM);
         goto fail;
@@ -119,15 +119,15 @@ static int vp8_alloc_frame(VP8Context *s, VP8Frame *f, int ref)
     return 0;
 
 fail:
-    ff_refstruct_unref(&f->seg_map);
+    av_refstruct_unref(&f->seg_map);
     ff_progress_frame_unref(&f->tf);
     return ret;
 }
 
 static void vp8_release_frame(VP8Frame *f)
 {
-    ff_refstruct_unref(&f->seg_map);
-    ff_refstruct_unref(&f->hwaccel_picture_private);
+    av_refstruct_unref(&f->seg_map);
+    av_refstruct_unref(&f->hwaccel_picture_private);
     ff_progress_frame_unref(&f->tf);
 }
 
@@ -2905,8 +2905,8 @@ av_cold int ff_vp8_decode_init(AVCodecContext *avctx)
 static void vp8_replace_frame(VP8Frame *dst, const VP8Frame *src)
 {
     ff_progress_frame_replace(&dst->tf, &src->tf);
-    ff_refstruct_replace(&dst->seg_map, src->seg_map);
-    ff_refstruct_replace(&dst->hwaccel_picture_private,
+    av_refstruct_replace(&dst->seg_map, src->seg_map);
+    av_refstruct_replace(&dst->hwaccel_picture_private,
                           src->hwaccel_picture_private);
 }
 
diff --git a/libavcodec/vp9.c b/libavcodec/vp9.c
index 8ede2e2eb3..c867626b3f 100644
--- a/libavcodec/vp9.c
+++ b/libavcodec/vp9.c
@@ -31,7 +31,7 @@
 #include "hwconfig.h"
 #include "profiles.h"
 #include "progressframe.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "thread.h"
 #include "pthread_internal.h"
 
@@ -97,8 +97,8 @@ static void vp9_tile_data_free(VP9TileData *td)
 static void vp9_frame_unref(VP9Frame *f)
 {
     ff_progress_frame_unref(&f->tf);
-    ff_refstruct_unref(&f->extradata);
-    ff_refstruct_unref(&f->hwaccel_picture_private);
+    av_refstruct_unref(&f->extradata);
+    av_refstruct_unref(&f->hwaccel_picture_private);
     f->segmentation_map = NULL;
 }
 
@@ -113,9 +113,9 @@ static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f)
 
     sz = 64 * s->sb_cols * s->sb_rows;
     if (sz != s->frame_extradata_pool_size) {
-        ff_refstruct_pool_uninit(&s->frame_extradata_pool);
-        s->frame_extradata_pool = ff_refstruct_pool_alloc(sz * (1 + sizeof(VP9mvrefPair)),
-                                                          FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME);
+        av_refstruct_pool_uninit(&s->frame_extradata_pool);
+        s->frame_extradata_pool = av_refstruct_pool_alloc(sz * (1 + sizeof(VP9mvrefPair)),
+                                                          AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME);
         if (!s->frame_extradata_pool) {
             s->frame_extradata_pool_size = 0;
             ret = AVERROR(ENOMEM);
@@ -123,7 +123,7 @@ static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f)
         }
         s->frame_extradata_pool_size = sz;
     }
-    f->extradata = ff_refstruct_pool_get(s->frame_extradata_pool);
+    f->extradata = av_refstruct_pool_get(s->frame_extradata_pool);
     if (!f->extradata) {
         ret = AVERROR(ENOMEM);
         goto fail;
@@ -147,13 +147,13 @@ static void vp9_frame_replace(VP9Frame *dst, const VP9Frame *src)
 {
     ff_progress_frame_replace(&dst->tf, &src->tf);
 
-    ff_refstruct_replace(&dst->extradata, src->extradata);
+    av_refstruct_replace(&dst->extradata, src->extradata);
 
     dst->segmentation_map = src->segmentation_map;
     dst->mv = src->mv;
     dst->uses_2pass = src->uses_2pass;
 
-    ff_refstruct_replace(&dst->hwaccel_picture_private,
+    av_refstruct_replace(&dst->hwaccel_picture_private,
                           src->hwaccel_picture_private);
 }
 
@@ -1239,7 +1239,7 @@ static av_cold int vp9_decode_free(AVCodecContext *avctx)
 
     for (int i = 0; i < 3; i++)
         vp9_frame_unref(&s->s.frames[i]);
-    ff_refstruct_pool_uninit(&s->frame_extradata_pool);
+    av_refstruct_pool_uninit(&s->frame_extradata_pool);
     for (i = 0; i < 8; i++) {
         ff_progress_frame_unref(&s->s.refs[i]);
         ff_progress_frame_unref(&s->next_refs[i]);
@@ -1802,7 +1802,7 @@ static int vp9_decode_update_thread_context(AVCodecContext *dst, const AVCodecCo
         vp9_frame_replace(&s->s.frames[i], &ssrc->s.frames[i]);
     for (int i = 0; i < 8; i++)
         ff_progress_frame_replace(&s->s.refs[i], &ssrc->next_refs[i]);
-    ff_refstruct_replace(&s->frame_extradata_pool, ssrc->frame_extradata_pool);
+    av_refstruct_replace(&s->frame_extradata_pool, ssrc->frame_extradata_pool);
     s->frame_extradata_pool_size = ssrc->frame_extradata_pool_size;
 
     s->s.h.invisible = ssrc->s.h.invisible;
diff --git a/libavcodec/vp9dec.h b/libavcodec/vp9dec.h
index 81dc801052..851ee9f6dd 100644
--- a/libavcodec/vp9dec.h
+++ b/libavcodec/vp9dec.h
@@ -160,7 +160,7 @@ typedef struct VP9Context {
     uint8_t mvstep[3][2];
 
     // frame specific buffer pools
-    struct FFRefStructPool *frame_extradata_pool;
+    struct AVRefStructPool *frame_extradata_pool;
     int frame_extradata_pool_size;
 } VP9Context;
 
diff --git a/libavcodec/vulkan_av1.c b/libavcodec/vulkan_av1.c
index a0befb9c4e..290607d24c 100644
--- a/libavcodec/vulkan_av1.c
+++ b/libavcodec/vulkan_av1.c
@@ -606,7 +606,7 @@ 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(FFRefStructOpaque _hwctx, void *data)
+static void vk_av1_free_frame_priv(AVRefStructOpaque _hwctx, void *data)
 {
     AVHWDeviceContext *hwctx = _hwctx.nc;
     AV1VulkanDecodePicture *ap = data;
diff --git a/libavcodec/vulkan_decode.c b/libavcodec/vulkan_decode.c
index dc5d57ec67..5936c0bc4a 100644
--- a/libavcodec/vulkan_decode.c
+++ b/libavcodec/vulkan_decode.c
@@ -16,7 +16,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "vulkan_video.h"
 #include "vulkan_decode.h"
 #include "config_components.h"
@@ -102,7 +102,7 @@ int ff_vk_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
             return err;
     }
 
-    ff_refstruct_replace(&dst_ctx->shared_ctx, src_ctx->shared_ctx);
+    av_refstruct_replace(&dst_ctx->shared_ctx, src_ctx->shared_ctx);
 
     if (src_ctx->session_params) {
         err = av_buffer_replace(&dst_ctx->session_params, src_ctx->session_params);
@@ -571,7 +571,7 @@ void ff_vk_decode_free_frame(AVHWDeviceContext *dev_ctx, FFVulkanDecodePicture *
     av_frame_free(&vp->dpb_frame);
 }
 
-static void free_common(FFRefStructOpaque unused, void *obj)
+static void free_common(AVRefStructOpaque unused, void *obj)
 {
     FFVulkanDecodeShared *ctx = obj;
     FFVulkanContext *s = &ctx->s;
@@ -605,7 +605,7 @@ static int vulkan_decode_bootstrap(AVCodecContext *avctx, AVBufferRef *frames_re
     if (dec->shared_ctx)
         return 0;
 
-    dec->shared_ctx = ff_refstruct_alloc_ext(sizeof(*ctx), 0, NULL,
+    dec->shared_ctx = av_refstruct_alloc_ext(sizeof(*ctx), 0, NULL,
                                              free_common);
     if (!dec->shared_ctx)
         return AVERROR(ENOMEM);
@@ -618,13 +618,13 @@ static int vulkan_decode_bootstrap(AVCodecContext *avctx, AVBufferRef *frames_re
     if (!(ctx->s.extensions & FF_VK_EXT_VIDEO_DECODE_QUEUE)) {
         av_log(avctx, AV_LOG_ERROR, "Device does not support the %s extension!\n",
                VK_KHR_VIDEO_DECODE_QUEUE_EXTENSION_NAME);
-        ff_refstruct_unref(&dec->shared_ctx);
+        av_refstruct_unref(&dec->shared_ctx);
         return AVERROR(ENOSYS);
     }
 
     err = ff_vk_load_functions(device, &ctx->s.vkfn, ctx->s.extensions, 1, 1);
     if (err < 0) {
-        ff_refstruct_unref(&dec->shared_ctx);
+        av_refstruct_unref(&dec->shared_ctx);
         return err;
     }
 
@@ -1073,7 +1073,7 @@ int ff_vk_decode_uninit(AVCodecContext *avctx)
 
     av_freep(&dec->hevc_headers);
     av_buffer_unref(&dec->session_params);
-    ff_refstruct_unref(&dec->shared_ctx);
+    av_refstruct_unref(&dec->shared_ctx);
     av_freep(&dec->slice_off);
     return 0;
 }
diff --git a/libavcodec/vulkan_h264.c b/libavcodec/vulkan_h264.c
index 05ac884138..79447dbb39 100644
--- a/libavcodec/vulkan_h264.c
+++ b/libavcodec/vulkan_h264.c
@@ -541,7 +541,7 @@ 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(FFRefStructOpaque _hwctx, void *data)
+static void vk_h264_free_frame_priv(AVRefStructOpaque _hwctx, void *data)
 {
     AVHWDeviceContext *hwctx = _hwctx.nc;
     H264VulkanDecodePicture *hp = data;
diff --git a/libavcodec/vulkan_hevc.c b/libavcodec/vulkan_hevc.c
index 0b20005687..e31e0fc8c5 100644
--- a/libavcodec/vulkan_hevc.c
+++ b/libavcodec/vulkan_hevc.c
@@ -897,7 +897,7 @@ static int vk_hevc_end_frame(AVCodecContext *avctx)
     return ff_vk_decode_frame(avctx, pic->f, vp, rav, rvp);
 }
 
-static void vk_hevc_free_frame_priv(FFRefStructOpaque _hwctx, void *data)
+static void vk_hevc_free_frame_priv(AVRefStructOpaque _hwctx, void *data)
 {
     AVHWDeviceContext *hwctx = _hwctx.nc;
     HEVCVulkanDecodePicture *hp = data;
diff --git a/libavcodec/vvc/ctu.c b/libavcodec/vvc/ctu.c
index 3624337fd7..f80bce637c 100644
--- a/libavcodec/vvc/ctu.c
+++ b/libavcodec/vvc/ctu.c
@@ -20,7 +20,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavcodec/refstruct.h"
+#include "libavutil/refstruct.h"
 
 #include "cabac.h"
 #include "ctu.h"
@@ -209,7 +209,7 @@ static void set_qp_c(VVCLocalContext *lc)
 
 static TransformUnit* alloc_tu(VVCFrameContext *fc, CodingUnit *cu)
 {
-    TransformUnit *tu = ff_refstruct_pool_get(fc->tu_pool);
+    TransformUnit *tu = av_refstruct_pool_get(fc->tu_pool);
     if (!tu)
         return NULL;
 
@@ -1184,7 +1184,7 @@ static CodingUnit* alloc_cu(VVCLocalContext *lc, const int x0, const int y0)
     const int rx        = x0 >> sps->ctb_log2_size_y;
     const int ry        = y0 >> sps->ctb_log2_size_y;
     CodingUnit **cus    = fc->tab.cus + ry * pps->ctb_width + rx;
-    CodingUnit *cu      = ff_refstruct_pool_get(fc->cu_pool);
+    CodingUnit *cu      = av_refstruct_pool_get(fc->cu_pool);
 
     if (!cu)
         return NULL;
@@ -2550,11 +2550,11 @@ void ff_vvc_ctu_free_cus(CodingUnit **cus)
         while (*head) {
             TransformUnit *tu = *head;
             *head = tu->next;
-            ff_refstruct_unref(&tu);
+            av_refstruct_unref(&tu);
         }
         cu->tus.tail = NULL;
 
-        ff_refstruct_unref(&cu);
+        av_refstruct_unref(&cu);
     }
 }
 
diff --git a/libavcodec/vvc/dec.c b/libavcodec/vvc/dec.c
index fef7339294..1cb168de7e 100644
--- a/libavcodec/vvc/dec.c
+++ b/libavcodec/vvc/dec.c
@@ -25,7 +25,7 @@
 #include "libavcodec/hwaccel_internal.h"
 #include "libavcodec/hwconfig.h"
 #include "libavcodec/profiles.h"
-#include "libavcodec/refstruct.h"
+#include "libavutil/refstruct.h"
 #include "libavutil/cpu.h"
 #include "libavutil/mem.h"
 #include "libavutil/thread.h"
@@ -352,8 +352,8 @@ static void pic_arrays_free(VVCFrameContext *fc)
 {
     free_cus(fc);
     frame_context_for_each_tl(fc, tl_free);
-    ff_refstruct_pool_uninit(&fc->rpl_tab_pool);
-    ff_refstruct_pool_uninit(&fc->tab_dmvr_mvf_pool);
+    av_refstruct_pool_uninit(&fc->rpl_tab_pool);
+    av_refstruct_pool_uninit(&fc->tab_dmvr_mvf_pool);
 
     memset(&fc->tab.sz, 0, sizeof(fc->tab.sz));
 }
@@ -378,16 +378,16 @@ static int pic_arrays_init(VVCContext *s, VVCFrameContext *fc)
     memset(fc->tab.slice_idx, -1, sizeof(*fc->tab.slice_idx) * ctu_count);
 
     if (fc->tab.sz.ctu_count != ctu_count) {
-        ff_refstruct_pool_uninit(&fc->rpl_tab_pool);
-        fc->rpl_tab_pool = ff_refstruct_pool_alloc(ctu_count * sizeof(RefPicListTab), 0);
+        av_refstruct_pool_uninit(&fc->rpl_tab_pool);
+        fc->rpl_tab_pool = av_refstruct_pool_alloc(ctu_count * sizeof(RefPicListTab), 0);
         if (!fc->rpl_tab_pool)
             return AVERROR(ENOMEM);
     }
 
     if (fc->tab.sz.pic_size_in_min_pu != pic_size_in_min_pu) {
-        ff_refstruct_pool_uninit(&fc->tab_dmvr_mvf_pool);
-        fc->tab_dmvr_mvf_pool = ff_refstruct_pool_alloc(
-            pic_size_in_min_pu * sizeof(MvField), FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME);
+        av_refstruct_pool_uninit(&fc->tab_dmvr_mvf_pool);
+        fc->tab_dmvr_mvf_pool = av_refstruct_pool_alloc(
+            pic_size_in_min_pu * sizeof(MvField), AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME);
         if (!fc->tab_dmvr_mvf_pool)
             return AVERROR(ENOMEM);
     }
@@ -468,8 +468,8 @@ static void slices_free(VVCFrameContext *fc)
         for (int i = 0; i < fc->nb_slices_allocated; i++) {
             SliceContext *slice = fc->slices[i];
             if (slice) {
-                ff_refstruct_unref(&slice->ref);
-                ff_refstruct_unref(&slice->sh.r);
+                av_refstruct_unref(&slice->ref);
+                av_refstruct_unref(&slice->sh.r);
                 eps_free(slice);
                 av_free(slice);
             }
@@ -598,16 +598,16 @@ static int ref_frame(VVCFrame *dst, const VVCFrame *src)
     if (ret < 0)
         return ret;
 
-    ff_refstruct_replace(&dst->sps, src->sps);
-    ff_refstruct_replace(&dst->pps, src->pps);
+    av_refstruct_replace(&dst->sps, src->sps);
+    av_refstruct_replace(&dst->pps, src->pps);
 
-    ff_refstruct_replace(&dst->progress, src->progress);
+    av_refstruct_replace(&dst->progress, src->progress);
 
-    ff_refstruct_replace(&dst->tab_dmvr_mvf, src->tab_dmvr_mvf);
+    av_refstruct_replace(&dst->tab_dmvr_mvf, src->tab_dmvr_mvf);
 
-    ff_refstruct_replace(&dst->rpl_tab, src->rpl_tab);
-    ff_refstruct_replace(&dst->rpl, src->rpl);
-    ff_refstruct_replace(&dst->hwaccel_picture_private,
+    av_refstruct_replace(&dst->rpl_tab, src->rpl_tab);
+    av_refstruct_replace(&dst->rpl, src->rpl);
+    av_refstruct_replace(&dst->hwaccel_picture_private,
                           src->hwaccel_picture_private);
     dst->nb_rpl_elems = src->nb_rpl_elems;
 
@@ -628,8 +628,8 @@ static av_cold void frame_context_free(VVCFrameContext *fc)
 {
     slices_free(fc);
 
-    ff_refstruct_pool_uninit(&fc->tu_pool);
-    ff_refstruct_pool_uninit(&fc->cu_pool);
+    av_refstruct_pool_uninit(&fc->tu_pool);
+    av_refstruct_pool_uninit(&fc->cu_pool);
 
     for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
         ff_vvc_unref_frame(fc, &fc->DPB[i], ~0);
@@ -656,11 +656,11 @@ static av_cold int frame_context_init(VVCFrameContext *fc, AVCodecContext *avctx
         if (!fc->DPB[j].frame)
             return AVERROR(ENOMEM);
     }
-    fc->cu_pool = ff_refstruct_pool_alloc(sizeof(CodingUnit), 0);
+    fc->cu_pool = av_refstruct_pool_alloc(sizeof(CodingUnit), 0);
     if (!fc->cu_pool)
         return AVERROR(ENOMEM);
 
-    fc->tu_pool = ff_refstruct_pool_alloc(sizeof(TransformUnit), 0);
+    fc->tu_pool = av_refstruct_pool_alloc(sizeof(TransformUnit), 0);
     if (!fc->tu_pool)
         return AVERROR(ENOMEM);
 
@@ -743,7 +743,7 @@ static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc,
     if (ret < 0)
         return ret;
 
-    ff_refstruct_replace(&sc->ref, unit->content_ref);
+    av_refstruct_replace(&sc->ref, unit->content_ref);
 
     if (is_first_slice) {
         ret = frame_start(s, fc, sc);
diff --git a/libavcodec/vvc/dec.h b/libavcodec/vvc/dec.h
index 0f8f1f721d..382311e3be 100644
--- a/libavcodec/vvc/dec.h
+++ b/libavcodec/vvc/dec.h
@@ -138,11 +138,11 @@ typedef struct VVCFrameContext {
 
     uint64_t decode_order;
 
-    struct FFRefStructPool *tab_dmvr_mvf_pool;
-    struct FFRefStructPool *rpl_tab_pool;
+    struct AVRefStructPool *tab_dmvr_mvf_pool;
+    struct AVRefStructPool *rpl_tab_pool;
 
-    struct FFRefStructPool *cu_pool;
-    struct FFRefStructPool *tu_pool;
+    struct AVRefStructPool *cu_pool;
+    struct AVRefStructPool *tu_pool;
 
     struct {
         int16_t *slice_idx;
diff --git a/libavcodec/vvc/ps.c b/libavcodec/vvc/ps.c
index 2dfa680884..54f017dfec 100644
--- a/libavcodec/vvc/ps.c
+++ b/libavcodec/vvc/ps.c
@@ -24,7 +24,7 @@
 #include "libavcodec/cbs_h266.h"
 #include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
-#include "libavcodec/refstruct.h"
+#include "libavutil/refstruct.h"
 #include "data.h"
 #include "ps.h"
 #include "dec.h"
@@ -200,21 +200,21 @@ static int sps_derive(VVCSPS *sps, void *log_ctx)
     return 0;
 }
 
-static void sps_free(FFRefStructOpaque opaque, void *obj)
+static void sps_free(AVRefStructOpaque opaque, void *obj)
 {
     VVCSPS *sps = obj;
-    ff_refstruct_unref(&sps->r);
+    av_refstruct_unref(&sps->r);
 }
 
 static const VVCSPS *sps_alloc(const H266RawSPS *rsps, void *log_ctx)
 {
     int ret;
-    VVCSPS *sps = ff_refstruct_alloc_ext(sizeof(*sps), 0, NULL, sps_free);
+    VVCSPS *sps = av_refstruct_alloc_ext(sizeof(*sps), 0, NULL, sps_free);
 
     if (!sps)
         return NULL;
 
-    ff_refstruct_replace(&sps->r, rsps);
+    av_refstruct_replace(&sps->r, rsps);
 
     ret = sps_derive(sps, log_ctx);
     if (ret < 0)
@@ -223,7 +223,7 @@ static const VVCSPS *sps_alloc(const H266RawSPS *rsps, void *log_ctx)
     return sps;
 
 fail:
-    ff_refstruct_unref(&sps);
+    av_refstruct_unref(&sps);
     return NULL;
 }
 
@@ -248,7 +248,7 @@ static int decode_sps(VVCParamSets *ps, const H266RawSPS *rsps, void *log_ctx, i
     if (!sps)
         return AVERROR(ENOMEM);
 
-    ff_refstruct_unref(&ps->sps_list[sps_id]);
+    av_refstruct_unref(&ps->sps_list[sps_id]);
     ps->sps_list[sps_id] = sps;
     ps->sps_id_used |= (1 << sps_id);
 
@@ -572,11 +572,11 @@ static int pps_derive(VVCPPS *pps, const VVCSPS *sps)
     return 0;
 }
 
-static void pps_free(FFRefStructOpaque opaque, void *obj)
+static void pps_free(AVRefStructOpaque opaque, void *obj)
 {
     VVCPPS *pps = obj;
 
-    ff_refstruct_unref(&pps->r);
+    av_refstruct_unref(&pps->r);
 
     av_freep(&pps->col_bd);
     av_freep(&pps->row_bd);
@@ -588,12 +588,12 @@ static void pps_free(FFRefStructOpaque opaque, void *obj)
 static const VVCPPS *pps_alloc(const H266RawPPS *rpps, const VVCSPS *sps)
 {
     int ret;
-    VVCPPS *pps = ff_refstruct_alloc_ext(sizeof(*pps), 0, NULL, pps_free);
+    VVCPPS *pps = av_refstruct_alloc_ext(sizeof(*pps), 0, NULL, pps_free);
 
     if (!pps)
         return NULL;
 
-    ff_refstruct_replace(&pps->r, rpps);
+    av_refstruct_replace(&pps->r, rpps);
 
     ret = pps_derive(pps, sps);
     if (ret < 0)
@@ -602,7 +602,7 @@ static const VVCPPS *pps_alloc(const H266RawPPS *rpps, const VVCSPS *sps)
     return pps;
 
 fail:
-    ff_refstruct_unref(&pps);
+    av_refstruct_unref(&pps);
     return NULL;
 }
 
@@ -621,7 +621,7 @@ static int decode_pps(VVCParamSets *ps, const H266RawPPS *rpps)
     if (!pps)
         return AVERROR(ENOMEM);
 
-    ff_refstruct_unref(&ps->pps_list[pps_id]);
+    av_refstruct_unref(&ps->pps_list[pps_id]);
     ps->pps_list[pps_id] = pps;
 
     return ret;
@@ -868,7 +868,7 @@ static int decode_ph(VVCFrameParamSets *fps, const H266RawPictureHeader *rph, vo
     const H266RawPPS *pps = fps->pps->r;
 
     ph->r = rph;
-    ff_refstruct_replace(&ph->rref, rph_ref);
+    av_refstruct_replace(&ph->rref, rph_ref);
     ret = ph_derive(ph, sps, pps, poc_tid0, is_clvss);
     if (ret < 0)
         return ret;
@@ -890,15 +890,15 @@ static int decode_frame_ps(VVCFrameParamSets *fps, const VVCParamSets *ps,
     if (!rpps)
         return AVERROR_INVALIDDATA;
 
-    ff_refstruct_replace(&fps->sps, ps->sps_list[rpps->pps_seq_parameter_set_id]);
-    ff_refstruct_replace(&fps->pps, ps->pps_list[rpps->pps_pic_parameter_set_id]);
+    av_refstruct_replace(&fps->sps, ps->sps_list[rpps->pps_seq_parameter_set_id]);
+    av_refstruct_replace(&fps->pps, ps->pps_list[rpps->pps_pic_parameter_set_id]);
 
     ret = decode_ph(fps, ph, h266->ph_ref, poc_tid0, is_clvss);
     if (ret < 0)
         return ret;
 
     if (ph->ph_explicit_scaling_list_enabled_flag)
-        ff_refstruct_replace(&fps->sl, ps->scaling_list[ph->ph_scaling_list_aps_id]);
+        av_refstruct_replace(&fps->sl, ps->scaling_list[ph->ph_scaling_list_aps_id]);
 
     if (ph->ph_lmcs_enabled_flag) {
         ret = lmcs_derive_lut(&fps->lmcs, ps->lmcs_list[ph->ph_lmcs_aps_id], fps->sps->r);
@@ -907,7 +907,7 @@ static int decode_frame_ps(VVCFrameParamSets *fps, const VVCParamSets *ps,
     }
 
     for (int i = 0; i < FF_ARRAY_ELEMS(fps->alf_list); i++)
-        ff_refstruct_replace(&fps->alf_list[i], ps->alf_list[i]);
+        av_refstruct_replace(&fps->alf_list[i], ps->alf_list[i]);
 
     return 0;
 }
@@ -951,26 +951,26 @@ int ff_vvc_decode_frame_ps(VVCFrameParamSets *fps, struct VVCContext *s)
 
 void ff_vvc_frame_ps_free(VVCFrameParamSets *fps)
 {
-    ff_refstruct_unref(&fps->sps);
-    ff_refstruct_unref(&fps->pps);
-    ff_refstruct_unref(&fps->ph.rref);
-    ff_refstruct_unref(&fps->sl);
+    av_refstruct_unref(&fps->sps);
+    av_refstruct_unref(&fps->pps);
+    av_refstruct_unref(&fps->ph.rref);
+    av_refstruct_unref(&fps->sl);
     for (int i = 0; i < FF_ARRAY_ELEMS(fps->alf_list); i++)
-        ff_refstruct_unref(&fps->alf_list[i]);
+        av_refstruct_unref(&fps->alf_list[i]);
 }
 
 void ff_vvc_ps_uninit(VVCParamSets *ps)
 {
     for (int i = 0; i < FF_ARRAY_ELEMS(ps->scaling_list); i++)
-        ff_refstruct_unref(&ps->scaling_list[i]);
+        av_refstruct_unref(&ps->scaling_list[i]);
     for (int i = 0; i < FF_ARRAY_ELEMS(ps->lmcs_list); i++)
-        ff_refstruct_unref(&ps->lmcs_list[i]);
+        av_refstruct_unref(&ps->lmcs_list[i]);
     for (int i = 0; i < FF_ARRAY_ELEMS(ps->alf_list); i++)
-        ff_refstruct_unref(&ps->alf_list[i]);
+        av_refstruct_unref(&ps->alf_list[i]);
     for (int i = 0; i < FF_ARRAY_ELEMS(ps->sps_list); i++)
-        ff_refstruct_unref(&ps->sps_list[i]);
+        av_refstruct_unref(&ps->sps_list[i]);
     for (int i = 0; i < FF_ARRAY_ELEMS(ps->pps_list); i++)
-        ff_refstruct_unref(&ps->pps_list[i]);
+        av_refstruct_unref(&ps->pps_list[i]);
 }
 
 static void alf_coeff(int16_t *coeff,
@@ -1049,23 +1049,23 @@ static void alf_derive(VVCALF *alf, const H266RawAPS *aps)
     alf_cc(alf, aps);
 }
 
-static void alf_free(FFRefStructOpaque unused, void *obj)
+static void alf_free(AVRefStructOpaque unused, void *obj)
 {
     VVCALF *alf = obj;
 
-    ff_refstruct_unref(&alf->r);
+    av_refstruct_unref(&alf->r);
 }
 
 static int aps_decode_alf(const VVCALF **alf, const H266RawAPS *aps)
 {
-    VVCALF *a = ff_refstruct_alloc_ext(sizeof(*a), 0, NULL, alf_free);
+    VVCALF *a = av_refstruct_alloc_ext(sizeof(*a), 0, NULL, alf_free);
     if (!a)
         return AVERROR(ENOMEM);
 
     alf_derive(a, aps);
-    ff_refstruct_replace(&a->r, aps);
-    ff_refstruct_replace(alf, a);
-    ff_refstruct_unref(&a);
+    av_refstruct_replace(&a->r, aps);
+    av_refstruct_replace(alf, a);
+    av_refstruct_unref(&a);
 
     return 0;
 }
@@ -1145,13 +1145,13 @@ static void scaling_derive(VVCScalingList *sl, const H266RawAPS *aps)
 
 static int aps_decode_scaling(const VVCScalingList **scaling, const H266RawAPS *aps)
 {
-    VVCScalingList *sl = ff_refstruct_allocz(sizeof(*sl));
+    VVCScalingList *sl = av_refstruct_allocz(sizeof(*sl));
     if (!sl)
         return AVERROR(ENOMEM);
 
     scaling_derive(sl, aps);
-    ff_refstruct_replace(scaling, sl);
-    ff_refstruct_unref(&sl);
+    av_refstruct_replace(scaling, sl);
+    av_refstruct_unref(&sl);
 
     return 0;
 }
@@ -1169,7 +1169,7 @@ int ff_vvc_decode_aps(VVCParamSets *ps, const CodedBitstreamUnit *unit)
             ret = aps_decode_alf(&ps->alf_list[aps->aps_adaptation_parameter_set_id], aps);
             break;
         case VVC_ASP_TYPE_LMCS:
-            ff_refstruct_replace(&ps->lmcs_list[aps->aps_adaptation_parameter_set_id], aps);
+            av_refstruct_replace(&ps->lmcs_list[aps->aps_adaptation_parameter_set_id], aps);
             break;
         case VVC_ASP_TYPE_SCALING:
             ret = aps_decode_scaling(&ps->scaling_list[aps->aps_adaptation_parameter_set_id], aps);
@@ -1352,7 +1352,7 @@ int ff_vvc_decode_sh(VVCSH *sh, const VVCFrameParamSets *fps, const CodedBitstre
     if (!fps->sps || !fps->pps)
         return AVERROR_INVALIDDATA;
 
-    ff_refstruct_replace(&sh->r, unit->content_ref);
+    av_refstruct_replace(&sh->r, unit->content_ref);
 
     ret = sh_derive(sh, fps);
     if (ret < 0)
diff --git a/libavcodec/vvc/refs.c b/libavcodec/vvc/refs.c
index 281410091e..bc3b3d0d13 100644
--- a/libavcodec/vvc/refs.c
+++ b/libavcodec/vvc/refs.c
@@ -24,7 +24,7 @@
 
 #include "libavutil/mem.h"
 #include "libavutil/thread.h"
-#include "libavcodec/refstruct.h"
+#include "libavutil/refstruct.h"
 #include "libavcodec/thread.h"
 #include "libavcodec/decode.h"
 
@@ -49,18 +49,18 @@ void ff_vvc_unref_frame(VVCFrameContext *fc, VVCFrame *frame, int flags)
     frame->flags &= ~flags;
     if (!frame->flags) {
         av_frame_unref(frame->frame);
-        ff_refstruct_unref(&frame->sps);
-        ff_refstruct_unref(&frame->pps);
-        ff_refstruct_unref(&frame->progress);
+        av_refstruct_unref(&frame->sps);
+        av_refstruct_unref(&frame->pps);
+        av_refstruct_unref(&frame->progress);
 
-        ff_refstruct_unref(&frame->tab_dmvr_mvf);
+        av_refstruct_unref(&frame->tab_dmvr_mvf);
 
-        ff_refstruct_unref(&frame->rpl);
+        av_refstruct_unref(&frame->rpl);
         frame->nb_rpl_elems = 0;
-        ff_refstruct_unref(&frame->rpl_tab);
+        av_refstruct_unref(&frame->rpl_tab);
 
         frame->collocated_ref = NULL;
-        ff_refstruct_unref(&frame->hwaccel_picture_private);
+        av_refstruct_unref(&frame->hwaccel_picture_private);
     }
 }
 
@@ -87,7 +87,7 @@ void ff_vvc_flush_dpb(VVCFrameContext *fc)
         ff_vvc_unref_frame(fc, &fc->DPB[i], ~0);
 }
 
-static void free_progress(FFRefStructOpaque unused, void *obj)
+static void free_progress(AVRefStructOpaque unused, void *obj)
 {
     FrameProgress *p = (FrameProgress *)obj;
 
@@ -99,13 +99,13 @@ static void free_progress(FFRefStructOpaque unused, void *obj)
 
 static FrameProgress *alloc_progress(void)
 {
-    FrameProgress *p = ff_refstruct_alloc_ext(sizeof(*p), 0, NULL, free_progress);
+    FrameProgress *p = av_refstruct_alloc_ext(sizeof(*p), 0, NULL, free_progress);
 
     if (p) {
         p->has_lock = !ff_mutex_init(&p->lock, NULL);
         p->has_cond = !ff_cond_init(&p->cond, NULL);
         if (!p->has_lock || !p->has_cond)
-            ff_refstruct_unref(&p);
+            av_refstruct_unref(&p);
     }
     return p;
 }
@@ -121,23 +121,23 @@ static VVCFrame *alloc_frame(VVCContext *s, VVCFrameContext *fc)
         if (frame->frame->buf[0])
             continue;
 
-        frame->sps = ff_refstruct_ref_c(fc->ps.sps);
-        frame->pps = ff_refstruct_ref_c(fc->ps.pps);
+        frame->sps = av_refstruct_ref_c(fc->ps.sps);
+        frame->pps = av_refstruct_ref_c(fc->ps.pps);
 
         ret = ff_thread_get_buffer(s->avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
         if (ret < 0)
             return NULL;
 
-        frame->rpl = ff_refstruct_allocz(s->current_frame.nb_units * sizeof(RefPicListTab));
+        frame->rpl = av_refstruct_allocz(s->current_frame.nb_units * sizeof(RefPicListTab));
         if (!frame->rpl)
             goto fail;
         frame->nb_rpl_elems = s->current_frame.nb_units;
 
-        frame->tab_dmvr_mvf = ff_refstruct_pool_get(fc->tab_dmvr_mvf_pool);
+        frame->tab_dmvr_mvf = av_refstruct_pool_get(fc->tab_dmvr_mvf_pool);
         if (!frame->tab_dmvr_mvf)
             goto fail;
 
-        frame->rpl_tab = ff_refstruct_pool_get(fc->rpl_tab_pool);
+        frame->rpl_tab = av_refstruct_pool_get(fc->rpl_tab_pool);
         if (!frame->rpl_tab)
             goto fail;
         frame->ctb_count = pps->ctb_width * pps->ctb_height;
diff --git a/libavcodec/wavpack.c b/libavcodec/wavpack.c
index bf9aa0cdce..9c74967f74 100644
--- a/libavcodec/wavpack.c
+++ b/libavcodec/wavpack.c
@@ -28,7 +28,7 @@
 #include "bytestream.h"
 #include "codec_internal.h"
 #include "get_bits.h"
-#include "refstruct.h"
+#include "libavutil/refstruct.h"
 #include "thread.h"
 #include "threadprogress.h"
 #include "unary.h"
@@ -110,7 +110,7 @@ typedef struct WavpackContext {
 
     DSDContext *dsdctx; ///< RefStruct reference
     ThreadProgress *curr_progress, *prev_progress; ///< RefStruct references
-    FFRefStructPool *progress_pool; ///< RefStruct reference
+    AVRefStructPool *progress_pool; ///< RefStruct reference
     int dsd_channels;
 } WavpackContext;
 
@@ -992,9 +992,9 @@ static int wv_dsd_reset(WavpackContext *s, int channels)
     int i;
 
     s->dsd_channels = 0;
-    ff_refstruct_unref(&s->dsdctx);
-    ff_refstruct_unref(&s->curr_progress);
-    ff_refstruct_unref(&s->prev_progress);
+    av_refstruct_unref(&s->dsdctx);
+    av_refstruct_unref(&s->curr_progress);
+    av_refstruct_unref(&s->prev_progress);
 
     if (!channels)
         return 0;
@@ -1003,7 +1003,7 @@ static int wv_dsd_reset(WavpackContext *s, int channels)
         channels > SIZE_MAX / sizeof(*s->dsdctx))
         return AVERROR(EINVAL);
 
-    s->dsdctx = ff_refstruct_allocz(channels * sizeof(*s->dsdctx));
+    s->dsdctx = av_refstruct_allocz(channels * sizeof(*s->dsdctx));
     if (!s->dsdctx)
         return AVERROR(ENOMEM);
     s->dsd_channels = channels;
@@ -1022,26 +1022,26 @@ static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
     WavpackContext *fsrc = src->priv_data;
     WavpackContext *fdst = dst->priv_data;
 
-    ff_refstruct_replace(&fdst->curr_progress, fsrc->curr_progress);
-    ff_refstruct_replace(&fdst->dsdctx, fsrc->dsdctx);
+    av_refstruct_replace(&fdst->curr_progress, fsrc->curr_progress);
+    av_refstruct_replace(&fdst->dsdctx, fsrc->dsdctx);
     fdst->dsd_channels = fsrc->dsd_channels;
 
     return 0;
 }
 
-static av_cold int progress_pool_init_cb(FFRefStructOpaque opaque, void *obj)
+static av_cold int progress_pool_init_cb(AVRefStructOpaque opaque, void *obj)
 {
     ThreadProgress *progress = obj;
     return ff_thread_progress_init(progress, 1);
 }
 
-static void progress_pool_reset_cb(FFRefStructOpaque opaque, void *obj)
+static void progress_pool_reset_cb(AVRefStructOpaque opaque, void *obj)
 {
     ThreadProgress *progress = obj;
     ff_thread_progress_reset(progress);
 }
 
-static av_cold void progress_pool_free_entry_cb(FFRefStructOpaque opaque, void *obj)
+static av_cold void progress_pool_free_entry_cb(AVRefStructOpaque opaque, void *obj)
 {
     ThreadProgress *progress = obj;
     ff_thread_progress_destroy(progress);
@@ -1058,8 +1058,8 @@ static av_cold int wavpack_decode_init(AVCodecContext *avctx)
 
 #if HAVE_THREADS
     if (ff_thread_sync_ref(avctx, offsetof(WavpackContext, progress_pool)) == FF_THREAD_IS_FIRST_THREAD) {
-        s->progress_pool = ff_refstruct_pool_alloc_ext(sizeof(*s->curr_progress),
-                                                       FF_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR, NULL,
+        s->progress_pool = av_refstruct_pool_alloc_ext(sizeof(*s->curr_progress),
+                                                       AV_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR, NULL,
                                                        progress_pool_init_cb,
                                                        progress_pool_reset_cb,
                                                        progress_pool_free_entry_cb, NULL);
@@ -1080,7 +1080,7 @@ static av_cold int wavpack_decode_end(AVCodecContext *avctx)
     av_freep(&s->fdec);
     s->fdec_num = 0;
 
-    ff_refstruct_pool_uninit(&s->progress_pool);
+    av_refstruct_pool_uninit(&s->progress_pool);
     wv_dsd_reset(s, 0);
 
     return 0;
@@ -1552,8 +1552,8 @@ static int wavpack_decode_block(AVCodecContext *avctx, AVFrame *frame, int block
         av_assert1(!!wc->progress_pool == !!(avctx->active_thread_type & FF_THREAD_FRAME));
         if (wc->progress_pool) {
             if (wc->dsdctx) {
-                ff_refstruct_unref(&wc->prev_progress);
-                wc->prev_progress = ff_refstruct_pool_get(wc->progress_pool);
+                av_refstruct_unref(&wc->prev_progress);
+                wc->prev_progress = av_refstruct_pool_get(wc->progress_pool);
                 if (!wc->prev_progress)
                     return AVERROR(ENOMEM);
                 FFSWAP(ThreadProgress*, wc->prev_progress, wc->curr_progress);
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 847878d7a4..ae1ad5199a 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -75,6 +75,7 @@ HEADERS = adler32.h                                                     \
           random_seed.h                                                 \
           rc4.h                                                         \
           rational.h                                                    \
+          refstruct.h                                                   \
           replaygain.h                                                  \
           ripemd.h                                                      \
           samplefmt.h                                                   \
@@ -163,6 +164,7 @@ OBJS = adler32.o                                                        \
        pixelutils.o                                                     \
        random_seed.o                                                    \
        rational.o                                                       \
+       refstruct.o                                                      \
        reverse.o                                                        \
        rc4.o                                                            \
        ripemd.o                                                         \
diff --git a/libavcodec/refstruct.c b/libavutil/refstruct.c
similarity index 74%
rename from libavcodec/refstruct.c
rename to libavutil/refstruct.c
index f89af156c2..ce804f781a 100644
--- a/libavcodec/refstruct.c
+++ b/libavutil/refstruct.c
@@ -22,12 +22,12 @@
 
 #include "refstruct.h"
 
-#include "libavutil/avassert.h"
-#include "libavutil/error.h"
-#include "libavutil/macros.h"
-#include "libavutil/mem.h"
-#include "libavutil/mem_internal.h"
-#include "libavutil/thread.h"
+#include "avassert.h"
+#include "error.h"
+#include "macros.h"
+#include "mem.h"
+#include "mem_internal.h"
+#include "thread.h"
 
 #ifndef REFSTRUCT_CHECKED
 #ifndef ASSERT_LEVEL
@@ -58,8 +58,8 @@ typedef struct RefCount {
      * the user does not throw away references.
      */
     atomic_uintptr_t  refcount;
-    FFRefStructOpaque opaque;
-    void (*free_cb)(FFRefStructOpaque opaque, void *obj);
+    AVRefStructOpaque opaque;
+    void (*free_cb)(AVRefStructOpaque opaque, void *obj);
     void (*free)(void *ref);
 
 #if REFSTRUCT_CHECKED
@@ -86,8 +86,8 @@ static void *get_userdata(void *buf)
     return (char*)buf + REFCOUNT_OFFSET;
 }
 
-static void refcount_init(RefCount *ref, FFRefStructOpaque opaque,
-                          void (*free_cb)(FFRefStructOpaque opaque, void *obj))
+static void refcount_init(RefCount *ref, AVRefStructOpaque opaque,
+                          void (*free_cb)(AVRefStructOpaque opaque, void *obj))
 {
     atomic_init(&ref->refcount, 1);
     ref->opaque  = opaque;
@@ -99,8 +99,8 @@ static void refcount_init(RefCount *ref, FFRefStructOpaque opaque,
 #endif
 }
 
-void *ff_refstruct_alloc_ext_c(size_t size, unsigned flags, FFRefStructOpaque opaque,
-                               void (*free_cb)(FFRefStructOpaque opaque, void *obj))
+void *av_refstruct_alloc_ext_c(size_t size, unsigned flags, AVRefStructOpaque opaque,
+                               void (*free_cb)(AVRefStructOpaque opaque, void *obj))
 {
     void *buf, *obj;
 
@@ -111,13 +111,13 @@ void *ff_refstruct_alloc_ext_c(size_t size, unsigned flags, FFRefStructOpaque op
         return NULL;
     refcount_init(buf, opaque, free_cb);
     obj = get_userdata(buf);
-    if (!(flags & FF_REFSTRUCT_FLAG_NO_ZEROING))
+    if (!(flags & AV_REFSTRUCT_FLAG_NO_ZEROING))
         memset(obj, 0, size);
 
     return obj;
 }
 
-void ff_refstruct_unref(void *objp)
+void av_refstruct_unref(void *objp)
 {
     void *obj;
     RefCount *ref;
@@ -137,7 +137,7 @@ void ff_refstruct_unref(void *objp)
     return;
 }
 
-void *ff_refstruct_ref(void *obj)
+void *av_refstruct_ref(void *obj)
 {
     RefCount *ref = get_refcount(obj);
 
@@ -146,7 +146,7 @@ void *ff_refstruct_ref(void *obj)
     return obj;
 }
 
-const void *ff_refstruct_ref_c(const void *obj)
+const void *av_refstruct_ref_c(const void *obj)
 {
     /* Casting const away here is fine, as it is only supposed
      * to apply to the user's data and not our bookkeeping data. */
@@ -157,21 +157,21 @@ const void *ff_refstruct_ref_c(const void *obj)
     return obj;
 }
 
-void ff_refstruct_replace(void *dstp, const void *src)
+void av_refstruct_replace(void *dstp, const void *src)
 {
     const void *dst;
     memcpy(&dst, dstp, sizeof(dst));
 
     if (src == dst)
         return;
-    ff_refstruct_unref(dstp);
+    av_refstruct_unref(dstp);
     if (src) {
-        dst = ff_refstruct_ref_c(src);
+        dst = av_refstruct_ref_c(src);
         memcpy(dstp, &dst, sizeof(dst));
     }
 }
 
-int ff_refstruct_exclusive(const void *obj)
+int av_refstruct_exclusive(const void *obj)
 {
     const RefCount *ref = cget_refcount(obj);
     /* Casting const away here is safe, because it is a load.
@@ -180,13 +180,13 @@ int ff_refstruct_exclusive(const void *obj)
     return atomic_load_explicit((atomic_uintptr_t*)&ref->refcount, memory_order_acquire) == 1;
 }
 
-struct FFRefStructPool {
+struct AVRefStructPool {
     size_t size;
-    FFRefStructOpaque opaque;
-    int  (*init_cb)(FFRefStructOpaque opaque, void *obj);
-    void (*reset_cb)(FFRefStructOpaque opaque, void *obj);
-    void (*free_entry_cb)(FFRefStructOpaque opaque, void *obj);
-    void (*free_cb)(FFRefStructOpaque opaque);
+    AVRefStructOpaque opaque;
+    int  (*init_cb)(AVRefStructOpaque opaque, void *obj);
+    void (*reset_cb)(AVRefStructOpaque opaque, void *obj);
+    void (*free_entry_cb)(AVRefStructOpaque opaque, void *obj);
+    void (*free_cb)(AVRefStructOpaque opaque);
 
     int uninited;
     unsigned entry_flags;
@@ -199,13 +199,13 @@ struct FFRefStructPool {
      * the RefCount's opaque pointer is used as next pointer
      * for available entries.
      * While the entries are in use, the opaque is a pointer
-     * to the corresponding FFRefStructPool.
+     * to the corresponding AVRefStructPool.
      */
     RefCount *available_entries;
     AVMutex mutex;
 };
 
-static void pool_free(FFRefStructPool *pool)
+static void pool_free(AVRefStructPool *pool)
 {
     ff_mutex_destroy(&pool->mutex);
     if (pool->free_cb)
@@ -213,7 +213,7 @@ static void pool_free(FFRefStructPool *pool)
     av_free(get_refcount(pool));
 }
 
-static void pool_free_entry(FFRefStructPool *pool, RefCount *ref)
+static void pool_free_entry(AVRefStructPool *pool, RefCount *ref)
 {
     if (pool->free_entry_cb)
         pool->free_entry_cb(pool->opaque, get_userdata(ref));
@@ -223,7 +223,7 @@ static void pool_free_entry(FFRefStructPool *pool, RefCount *ref)
 static void pool_return_entry(void *ref_)
 {
     RefCount *ref = ref_;
-    FFRefStructPool *pool = ref->opaque.nc;
+    AVRefStructPool *pool = ref->opaque.nc;
 
     ff_mutex_lock(&pool->mutex);
     if (!pool->uninited) {
@@ -240,14 +240,14 @@ static void pool_return_entry(void *ref_)
         pool_free(pool);
 }
 
-static void pool_reset_entry(FFRefStructOpaque opaque, void *entry)
+static void pool_reset_entry(AVRefStructOpaque opaque, void *entry)
 {
-    FFRefStructPool *pool = opaque.nc;
+    AVRefStructPool *pool = opaque.nc;
 
     pool->reset_cb(pool->opaque, entry);
 }
 
-static int refstruct_pool_get_ext(void *datap, FFRefStructPool *pool)
+static int refstruct_pool_get_ext(void *datap, AVRefStructPool *pool)
 {
     void *ret = NULL;
 
@@ -266,7 +266,7 @@ static int refstruct_pool_get_ext(void *datap, FFRefStructPool *pool)
 
     if (!ret) {
         RefCount *ref;
-        ret = ff_refstruct_alloc_ext(pool->size, pool->entry_flags, pool,
+        ret = av_refstruct_alloc_ext(pool->size, pool->entry_flags, pool,
                                      pool->reset_cb ? pool_reset_entry : NULL);
         if (!ret)
             return AVERROR(ENOMEM);
@@ -275,9 +275,9 @@ static int refstruct_pool_get_ext(void *datap, FFRefStructPool *pool)
         if (pool->init_cb) {
             int err = pool->init_cb(pool->opaque, ret);
             if (err < 0) {
-                if (pool->pool_flags & FF_REFSTRUCT_POOL_FLAG_RESET_ON_INIT_ERROR)
+                if (pool->pool_flags & AV_REFSTRUCT_POOL_FLAG_RESET_ON_INIT_ERROR)
                     pool->reset_cb(pool->opaque, ret);
-                if (pool->pool_flags & FF_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR)
+                if (pool->pool_flags & AV_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR)
                     pool->free_entry_cb(pool->opaque, ret);
                 av_free(ref);
                 return err;
@@ -286,7 +286,7 @@ static int refstruct_pool_get_ext(void *datap, FFRefStructPool *pool)
     }
     atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);
 
-    if (pool->pool_flags & FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME)
+    if (pool->pool_flags & AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME)
         memset(ret, 0, pool->size);
 
     memcpy(datap, &ret, sizeof(ret));
@@ -294,7 +294,7 @@ static int refstruct_pool_get_ext(void *datap, FFRefStructPool *pool)
     return 0;
 }
 
-void *ff_refstruct_pool_get(FFRefStructPool *pool)
+void *av_refstruct_pool_get(AVRefStructPool *pool)
 {
     void *ret;
     refstruct_pool_get_ext(&ret, pool);
@@ -308,14 +308,14 @@ void *ff_refstruct_pool_get(FFRefStructPool *pool)
  */
 static void pool_unref(void *ref)
 {
-    FFRefStructPool *pool = get_userdata(ref);
+    AVRefStructPool *pool = get_userdata(ref);
     if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
         pool_free(pool);
 }
 
-static void refstruct_pool_uninit(FFRefStructOpaque unused, void *obj)
+static void refstruct_pool_uninit(AVRefStructOpaque unused, void *obj)
 {
-    FFRefStructPool *pool = obj;
+    AVRefStructPool *pool = obj;
     RefCount *entry;
 
     ff_mutex_lock(&pool->mutex);
@@ -332,19 +332,19 @@ static void refstruct_pool_uninit(FFRefStructOpaque unused, void *obj)
     }
 }
 
-FFRefStructPool *ff_refstruct_pool_alloc(size_t size, unsigned flags)
+AVRefStructPool *av_refstruct_pool_alloc(size_t size, unsigned flags)
 {
-    return ff_refstruct_pool_alloc_ext(size, flags, NULL, NULL, NULL, NULL, NULL);
+    return av_refstruct_pool_alloc_ext(size, flags, NULL, NULL, NULL, NULL, NULL);
 }
 
-FFRefStructPool *ff_refstruct_pool_alloc_ext_c(size_t size, unsigned flags,
-                                               FFRefStructOpaque opaque,
-                                               int  (*init_cb)(FFRefStructOpaque opaque, void *obj),
-                                               void (*reset_cb)(FFRefStructOpaque opaque, void *obj),
-                                               void (*free_entry_cb)(FFRefStructOpaque opaque, void *obj),
-                                               void (*free_cb)(FFRefStructOpaque opaque))
+AVRefStructPool *av_refstruct_pool_alloc_ext_c(size_t size, unsigned flags,
+                                               AVRefStructOpaque opaque,
+                                               int  (*init_cb)(AVRefStructOpaque opaque, void *obj),
+                                               void (*reset_cb)(AVRefStructOpaque opaque, void *obj),
+                                               void (*free_entry_cb)(AVRefStructOpaque opaque, void *obj),
+                                               void (*free_cb)(AVRefStructOpaque opaque))
 {
-    FFRefStructPool *pool = ff_refstruct_alloc_ext(sizeof(*pool), 0, NULL,
+    AVRefStructPool *pool = av_refstruct_alloc_ext(sizeof(*pool), 0, NULL,
                                                    refstruct_pool_uninit);
     int err;
 
@@ -358,26 +358,26 @@ FFRefStructPool *ff_refstruct_pool_alloc_ext_c(size_t size, unsigned flags,
     pool->reset_cb      = reset_cb;
     pool->free_entry_cb = free_entry_cb;
     pool->free_cb       = free_cb;
-#define COMMON_FLAGS FF_REFSTRUCT_POOL_FLAG_NO_ZEROING
+#define COMMON_FLAGS AV_REFSTRUCT_POOL_FLAG_NO_ZEROING
     pool->entry_flags   = flags & COMMON_FLAGS;
     // Filter out nonsense combinations to avoid checks later.
     if (!pool->reset_cb)
-        flags &= ~FF_REFSTRUCT_POOL_FLAG_RESET_ON_INIT_ERROR;
+        flags &= ~AV_REFSTRUCT_POOL_FLAG_RESET_ON_INIT_ERROR;
     if (!pool->free_entry_cb)
-        flags &= ~FF_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR;
+        flags &= ~AV_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR;
     pool->pool_flags    = flags;
 
-    if (flags & FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME) {
+    if (flags & AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME) {
         // We will zero the buffer before every use, so zeroing
         // upon allocating the buffer is unnecessary.
-        pool->entry_flags |= FF_REFSTRUCT_FLAG_NO_ZEROING;
+        pool->entry_flags |= AV_REFSTRUCT_FLAG_NO_ZEROING;
     }
 
     atomic_init(&pool->refcount, 1);
 
     err = ff_mutex_init(&pool->mutex, NULL);
     if (err) {
-        // Don't call ff_refstruct_uninit() on pool, as it hasn't been properly
+        // Don't call av_refstruct_uninit() on pool, as it hasn't been properly
         // set up and is just a POD right now.
         av_free(get_refcount(pool));
         return NULL;
diff --git a/libavcodec/refstruct.h b/libavutil/refstruct.h
similarity index 71%
rename from libavcodec/refstruct.h
rename to libavutil/refstruct.h
index c64ad62b6b..644d888284 100644
--- a/libavcodec/refstruct.h
+++ b/libavutil/refstruct.h
@@ -16,8 +16,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#ifndef AVCODEC_REFSTRUCT_H
-#define AVCODEC_REFSTRUCT_H
+#ifndef AVUTIL_REFSTRUCT_H
+#define AVUTIL_REFSTRUCT_H
 
 #include <stddef.h>
 
@@ -51,20 +51,20 @@
  * This union is used for all opaque parameters in this API to spare the user
  * to cast const away in case the opaque to use is const-qualified.
  *
- * The functions provided by this API with an FFRefStructOpaque come in pairs
+ * The functions provided by this API with an AVRefStructOpaque come in pairs
  * named foo_c and foo. The foo function accepts void* as opaque and is just
  * a wrapper around the foo_c function; "_c" means "(potentially) const".
  */
 typedef union {
     void *nc;
     const void *c;
-} FFRefStructOpaque;
+} AVRefStructOpaque;
 
 /**
- * If this flag is set in ff_refstruct_alloc_ext_c(), the object will not
+ * If this flag is set in av_refstruct_alloc_ext_c(), the object will not
  * be initially zeroed.
  */
-#define FF_REFSTRUCT_FLAG_NO_ZEROING (1 << 0)
+#define AV_REFSTRUCT_FLAG_NO_ZEROING (1 << 0)
 
 /**
  * Allocate a refcounted object of usable size `size` managed via
@@ -74,37 +74,37 @@ typedef union {
  * the returned object is initially zeroed.
  *
  * @param size    Desired usable size of the returned object.
- * @param flags   A bitwise combination of FF_REFSTRUCT_FLAG_* flags.
+ * @param flags   A bitwise combination of av_refstruct_FLAG_* flags.
  * @param opaque  A pointer that will be passed to the free_cb callback.
  * @param free_cb A callback for freeing this object's content
  *                when its reference count reaches zero;
  *                it must not free the object itself.
  * @return A pointer to an object of the desired size or NULL on failure.
  */
-void *ff_refstruct_alloc_ext_c(size_t size, unsigned flags, FFRefStructOpaque opaque,
-                               void (*free_cb)(FFRefStructOpaque opaque, void *obj));
+void *av_refstruct_alloc_ext_c(size_t size, unsigned flags, AVRefStructOpaque opaque,
+                               void (*free_cb)(AVRefStructOpaque opaque, void *obj));
 
 /**
- * A wrapper around ff_refstruct_alloc_ext_c() for the common case
+ * A wrapper around av_refstruct_alloc_ext_c() for the common case
  * of a non-const qualified opaque.
  *
- * @see ff_refstruct_alloc_ext_c()
+ * @see av_refstruct_alloc_ext_c()
  */
 static inline
-void *ff_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque,
-                             void (*free_cb)(FFRefStructOpaque opaque, void *obj))
+void *av_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque,
+                             void (*free_cb)(AVRefStructOpaque opaque, void *obj))
 {
-    return ff_refstruct_alloc_ext_c(size, flags, (FFRefStructOpaque){.nc = opaque},
+    return av_refstruct_alloc_ext_c(size, flags, (AVRefStructOpaque){.nc = opaque},
                                     free_cb);
 }
 
 /**
- * Equivalent to ff_refstruct_alloc_ext(size, 0, NULL, NULL)
+ * Equivalent to av_refstruct_alloc_ext(size, 0, NULL, NULL)
  */
 static inline
-void *ff_refstruct_allocz(size_t size)
+void *av_refstruct_allocz(size_t size)
 {
-    return ff_refstruct_alloc_ext(size, 0, NULL, NULL);
+    return av_refstruct_alloc_ext(size, 0, NULL, NULL);
 }
 
 /**
@@ -116,7 +116,7 @@ void *ff_refstruct_allocz(size_t size)
  * @param objp Pointer to a pointer that is either NULL or points to an object
  *             managed via this API. `*objp` is set to NULL on return.
  */
-void ff_refstruct_unref(void *objp);
+void av_refstruct_unref(void *objp);
 
 /**
  * Create a new reference to an object managed via this API,
@@ -124,13 +124,13 @@ void ff_refstruct_unref(void *objp);
  * and return obj.
  * @return a pointer equal to obj.
  */
-void *ff_refstruct_ref(void *obj);
+void *av_refstruct_ref(void *obj);
 
 /**
- * Analog of ff_refstruct_ref(), but for constant objects.
- * @see ff_refstruct_ref()
+ * Analog of av_refstruct_ref(), but for constant objects.
+ * @see av_refstruct_ref()
  */
-const void *ff_refstruct_ref_c(const void *obj);
+const void *av_refstruct_ref_c(const void *obj);
 
 /**
  * Ensure `*dstp` refers to the same object as src.
@@ -144,7 +144,7 @@ const void *ff_refstruct_ref_c(const void *obj);
  *             managed via this API.
  * @param src  A pointer to an object managed via this API or NULL.
  */
-void ff_refstruct_replace(void *dstp, const void *src);
+void av_refstruct_replace(void *dstp, const void *src);
 
 /**
  * Check whether the reference count of an object managed
@@ -153,10 +153,10 @@ void ff_refstruct_replace(void *dstp, const void *src);
  * @param obj A pointer to an object managed via this API.
  * @return 1 if the reference count of obj is 1; 0 otherwise.
  */
-int ff_refstruct_exclusive(const void *obj);
+int av_refstruct_exclusive(const void *obj);
 
 /**
- * FFRefStructPool is an API for a thread-safe pool of objects managed
+ * AVRefStructPool is an API for a thread-safe pool of objects managed
  * via the RefStruct API.
  *
  * Frequently allocating and freeing large or complicated objects may be slow
@@ -164,16 +164,16 @@ int ff_refstruct_exclusive(const void *obj);
  * needs a set of interchangable objects.
  *
  * At the beginning, the user must call allocate the pool via
- * ff_refstruct_pool_alloc() or its analogue ff_refstruct_pool_alloc_ext().
- * Then whenever an object is needed, call ff_refstruct_pool_get() to
+ * av_refstruct_pool_alloc() or its analogue av_refstruct_pool_alloc_ext().
+ * Then whenever an object is needed, call av_refstruct_pool_get() to
  * get a new or reused object from the pool. This new object works in all
- * aspects the same way as the ones created by ff_refstruct_alloc_ext().
+ * aspects the same way as the ones created by av_refstruct_alloc_ext().
  * However, when the last reference to this object is unreferenced, it is
  * (optionally) reset and returned to the pool instead of being freed and
- * will be reused for subsequent ff_refstruct_pool_get() calls.
+ * will be reused for subsequent av_refstruct_pool_get() calls.
  *
  * When the caller is done with the pool and no longer needs to create any new
- * objects, ff_refstruct_pool_uninit() must be called to mark the pool as
+ * objects, av_refstruct_pool_uninit() must be called to mark the pool as
  * freeable. Then entries returned to the pool will then be freed.
  * Once all the entries are freed, the pool will automatically be freed.
  *
@@ -184,56 +184,56 @@ int ff_refstruct_exclusive(const void *obj);
 /**
  * The buffer pool. This structure is opaque and not meant to be accessed
  * directly. It is allocated with the allocators below and freed with
- * ff_refstruct_pool_uninit().
+ * av_refstruct_pool_uninit().
  */
-typedef struct FFRefStructPool FFRefStructPool;
+typedef struct AVRefStructPool AVRefStructPool;
 
 /**
  * If this flag is not set, every object in the pool will be zeroed before
  * the init callback is called or before it is turned over to the user
  * for the first time if no init callback has been provided.
  */
-#define FF_REFSTRUCT_POOL_FLAG_NO_ZEROING         FF_REFSTRUCT_FLAG_NO_ZEROING
+#define AV_REFSTRUCT_POOL_FLAG_NO_ZEROING         AV_REFSTRUCT_FLAG_NO_ZEROING
 /**
  * If this flag is set and both init_cb and reset_cb callbacks are provided,
  * then reset_cb will be called if init_cb fails.
  * The object passed to reset_cb will be in the state left by init_cb.
  */
-#define FF_REFSTRUCT_POOL_FLAG_RESET_ON_INIT_ERROR                   (1 << 16)
+#define AV_REFSTRUCT_POOL_FLAG_RESET_ON_INIT_ERROR                   (1 << 16)
 /**
  * If this flag is set and both init_cb and free_entry_cb callbacks are
  * provided, then free_cb will be called if init_cb fails.
  *
  * It will be called after reset_cb in case reset_cb and the
- * FF_REFSTRUCT_POOL_FLAG_RESET_ON_INIT_ERROR flag are also set.
+ * av_refstruct_POOL_FLAG_RESET_ON_INIT_ERROR flag are also set.
  *
  * The object passed to free_cb will be in the state left by
  * the callbacks applied earlier (init_cb potentially followed by reset_cb).
  */
-#define FF_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR                    (1 << 17)
+#define AV_REFSTRUCT_POOL_FLAG_FREE_ON_INIT_ERROR                    (1 << 17)
 /**
  * If this flag is set, the entries will be zeroed before
  * being returned to the user (after the init or reset callbacks
  * have been called (if provided)). Furthermore, to avoid zeroing twice
- * it also makes the pool behave as if the FF_REFSTRUCT_POOL_FLAG_NO_ZEROING
+ * it also makes the pool behave as if the av_refstruct_POOL_FLAG_NO_ZEROING
  * flag had been provided.
  */
-#define FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME                       (1 << 18)
+#define AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME                       (1 << 18)
 
 /**
- * Equivalent to ff_refstruct_pool_alloc(size, flags, NULL, NULL, NULL, NULL, NULL)
+ * Equivalent to av_refstruct_pool_alloc(size, flags, NULL, NULL, NULL, NULL, NULL)
  */
-FFRefStructPool *ff_refstruct_pool_alloc(size_t size, unsigned flags);
+AVRefStructPool *av_refstruct_pool_alloc(size_t size, unsigned flags);
 
 /**
- * Allocate an FFRefStructPool, potentially using complex callbacks.
+ * Allocate an AVRefStructPool, potentially using complex callbacks.
  *
  * @param size size of the entries of the pool
- * @param flags a bitwise combination of FF_REFSTRUCT_POOL_FLAG_* flags
+ * @param flags a bitwise combination of av_refstruct_POOL_FLAG_* flags
  * @param opaque A pointer that will be passed to the callbacks below.
  * @param init  A callback that will be called directly after a new entry
  *              has been allocated. obj has already been zeroed unless
- *              the FF_REFSTRUCT_POOL_FLAG_NO_ZEROING flag is in use.
+ *              the av_refstruct_POOL_FLAG_NO_ZEROING flag is in use.
  * @param reset A callback that will be called after an entry has been
  *              returned to the pool and before it is reused.
  * @param free_entry A callback that will be called when an entry is freed
@@ -241,28 +241,28 @@ FFRefStructPool *ff_refstruct_pool_alloc(size_t size, unsigned flags);
  * @param free       A callback that will be called when the pool itself is
  *                   freed (after the last entry has been returned and freed).
  */
-FFRefStructPool *ff_refstruct_pool_alloc_ext_c(size_t size, unsigned flags,
-                                               FFRefStructOpaque opaque,
-                                               int  (*init_cb)(FFRefStructOpaque opaque, void *obj),
-                                               void (*reset_cb)(FFRefStructOpaque opaque, void *obj),
-                                               void (*free_entry_cb)(FFRefStructOpaque opaque, void *obj),
-                                               void (*free_cb)(FFRefStructOpaque opaque));
+AVRefStructPool *av_refstruct_pool_alloc_ext_c(size_t size, unsigned flags,
+                                               AVRefStructOpaque opaque,
+                                               int  (*init_cb)(AVRefStructOpaque opaque, void *obj),
+                                               void (*reset_cb)(AVRefStructOpaque opaque, void *obj),
+                                               void (*free_entry_cb)(AVRefStructOpaque opaque, void *obj),
+                                               void (*free_cb)(AVRefStructOpaque opaque));
 
 /**
- * A wrapper around ff_refstruct_pool_alloc_ext_c() for the common case
+ * A wrapper around av_refstruct_pool_alloc_ext_c() for the common case
  * of a non-const qualified opaque.
  *
- * @see ff_refstruct_pool_alloc_ext_c()
+ * @see av_refstruct_pool_alloc_ext_c()
  */
 static inline
-FFRefStructPool *ff_refstruct_pool_alloc_ext(size_t size, unsigned flags,
+AVRefStructPool *av_refstruct_pool_alloc_ext(size_t size, unsigned flags,
                                              void *opaque,
-                                             int  (*init_cb)(FFRefStructOpaque opaque, void *obj),
-                                             void (*reset_cb)(FFRefStructOpaque opaque, void *obj),
-                                             void (*free_entry_cb)(FFRefStructOpaque opaque, void *obj),
-                                             void (*free_cb)(FFRefStructOpaque opaque))
+                                             int  (*init_cb)(AVRefStructOpaque opaque, void *obj),
+                                             void (*reset_cb)(AVRefStructOpaque opaque, void *obj),
+                                             void (*free_entry_cb)(AVRefStructOpaque opaque, void *obj),
+                                             void (*free_cb)(AVRefStructOpaque opaque))
 {
-    return ff_refstruct_pool_alloc_ext_c(size, flags, (FFRefStructOpaque){.nc = opaque},
+    return av_refstruct_pool_alloc_ext_c(size, flags, (AVRefStructOpaque){.nc = opaque},
                                          init_cb, reset_cb, free_entry_cb, free_cb);
 }
 
@@ -270,13 +270,13 @@ FFRefStructPool *ff_refstruct_pool_alloc_ext(size_t size, unsigned flags,
  * Get an object from the pool, reusing an old one from the pool when
  * available.
  *
- * Every call to this function must happen before ff_refstruct_pool_uninit().
+ * Every call to this function must happen before av_refstruct_pool_uninit().
  * Otherwise undefined behaviour may occur.
  *
  * @param pool the pool from which to get the object
  * @return a reference to the object on success, NULL on error.
  */
-void *ff_refstruct_pool_get(FFRefStructPool *pool);
+void *av_refstruct_pool_get(AVRefStructPool *pool);
 
 /**
  * Mark the pool as being available for freeing. It will actually be freed
@@ -289,9 +289,9 @@ void *ff_refstruct_pool_get(FFRefStructPool *pool);
  * @param poolp pointer to a pointer to either NULL or a pool to be freed.
  *              `*poolp` will be set to NULL.
  */
-static inline void ff_refstruct_pool_uninit(FFRefStructPool **poolp)
+static inline void av_refstruct_pool_uninit(AVRefStructPool **poolp)
 {
-    ff_refstruct_unref(poolp);
+    av_refstruct_unref(poolp);
 }
 
-#endif /* AVCODEC_REFSTRUCT_H */
+#endif /* AVUTIL_REFSTRUCT_H */
diff --git a/libavutil/version.h b/libavutil/version.h
index ad1f3e68ba..e8c50ea4b2 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,7 +79,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  59
-#define LIBAVUTIL_VERSION_MINOR  49
+#define LIBAVUTIL_VERSION_MINOR  50
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
-- 
2.43.0



More information about the ffmpeg-devel mailing list