[FFmpeg-cvslog] avcodec/get_buffer: Use RefStruct API for FramePool

Andreas Rheinhardt git at videolan.org
Sat Oct 7 23:49:33 EEST 2023


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Thu Aug  4 15:57:41 2022 +0200| [78c9ed26b6e83ea29b55420c2c88dc1860810042] | committer: Andreas Rheinhardt

avcodec/get_buffer: Use RefStruct API for FramePool

Avoids allocations and frees and error checks for said allocations;
also avoids a few indirections and casts.

Reviewed-by: Anton Khirnov <anton at khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=78c9ed26b6e83ea29b55420c2c88dc1860810042
---

 libavcodec/avcodec.c       |  3 ++-
 libavcodec/get_buffer.c    | 44 +++++++++++---------------------------------
 libavcodec/internal.h      |  2 +-
 libavcodec/pthread_frame.c |  7 +++----
 4 files changed, 17 insertions(+), 39 deletions(-)

diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index f2dc7b0edd..2dda310e91 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -44,6 +44,7 @@
 #include "frame_thread_encoder.h"
 #include "hwconfig.h"
 #include "internal.h"
+#include "refstruct.h"
 #include "thread.h"
 
 /**
@@ -459,7 +460,7 @@ av_cold int avcodec_close(AVCodecContext *avctx)
         av_frame_free(&avci->in_frame);
         av_frame_free(&avci->recon_frame);
 
-        av_buffer_unref(&avci->pool);
+        ff_refstruct_unref(&avci->pool);
 
         ff_hwaccel_uninit(avctx);
 
diff --git a/libavcodec/get_buffer.c b/libavcodec/get_buffer.c
index a04fd878de..647f8a3df7 100644
--- a/libavcodec/get_buffer.c
+++ b/libavcodec/get_buffer.c
@@ -32,6 +32,7 @@
 
 #include "avcodec.h"
 #include "internal.h"
+#include "refstruct.h"
 
 typedef struct FramePool {
     /**
@@ -52,40 +53,18 @@ typedef struct FramePool {
     int samples;
 } FramePool;
 
-static void frame_pool_free(void *opaque, uint8_t *data)
+static void frame_pool_free(FFRefStructOpaque unused, void *obj)
 {
-    FramePool *pool = (FramePool*)data;
+    FramePool *pool = obj;
     int i;
 
     for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
         av_buffer_pool_uninit(&pool->pools[i]);
-
-    av_freep(&data);
-}
-
-static AVBufferRef *frame_pool_alloc(void)
-{
-    FramePool *pool = av_mallocz(sizeof(*pool));
-    AVBufferRef *buf;
-
-    if (!pool)
-        return NULL;
-
-    buf = av_buffer_create((uint8_t*)pool, sizeof(*pool),
-                           frame_pool_free, NULL, 0);
-    if (!buf) {
-        av_freep(&pool);
-        return NULL;
-    }
-
-    return buf;
 }
 
 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
 {
-    FramePool *pool = avctx->internal->pool ?
-                      (FramePool*)avctx->internal->pool->data : NULL;
-    AVBufferRef *pool_buf;
+    FramePool *pool = avctx->internal->pool;
     int i, ret, ch, planes;
 
     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
@@ -109,10 +88,9 @@ FF_ENABLE_DEPRECATION_WARNINGS
             return 0;
     }
 
-    pool_buf = frame_pool_alloc();
-    if (!pool_buf)
+    pool = ff_refstruct_alloc_ext(sizeof(*pool), 0, NULL, frame_pool_free);
+    if (!pool)
         return AVERROR(ENOMEM);
-    pool = (FramePool*)pool_buf->data;
 
     switch (avctx->codec_type) {
     case AVMEDIA_TYPE_VIDEO: {
@@ -189,18 +167,18 @@ FF_ENABLE_DEPRECATION_WARNINGS
     default: av_assert0(0);
     }
 
-    av_buffer_unref(&avctx->internal->pool);
-    avctx->internal->pool = pool_buf;
+    ff_refstruct_unref(&avctx->internal->pool);
+    avctx->internal->pool = pool;
 
     return 0;
 fail:
-    av_buffer_unref(&pool_buf);
+    ff_refstruct_unref(&pool);
     return ret;
 }
 
 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
 {
-    FramePool *pool = (FramePool*)avctx->internal->pool->data;
+    FramePool *pool = avctx->internal->pool;
     int planes = pool->planes;
     int i;
 
@@ -245,7 +223,7 @@ fail:
 
 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
 {
-    FramePool *pool = (FramePool*)s->internal->pool->data;
+    FramePool *pool = s->internal->pool;
     int i;
 
     if (pic->data[0] || pic->data[1] || pic->data[2] || pic->data[3]) {
diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index 83e0bc3fb2..eb9e0d707c 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -62,7 +62,7 @@ typedef struct AVCodecInternal {
      */
     int pad_samples;
 
-    AVBufferRef *pool;
+    struct FramePool *pool;
 
     void *thread_ctx;
 
diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c
index 138576778d..7e274b0559 100644
--- a/libavcodec/pthread_frame.c
+++ b/libavcodec/pthread_frame.c
@@ -35,6 +35,7 @@
 #include "hwconfig.h"
 #include "internal.h"
 #include "pthread_internal.h"
+#include "refstruct.h"
 #include "thread.h"
 #include "threadframe.h"
 #include "version_major.h"
@@ -329,9 +330,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
 
         dst->hwaccel_flags = src->hwaccel_flags;
 
-        err = av_buffer_replace(&dst->internal->pool, src->internal->pool);
-        if (err < 0)
-            return err;
+        ff_refstruct_replace(&dst->internal->pool, src->internal->pool);
     }
 
     if (for_user) {
@@ -740,7 +739,7 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
                 av_freep(&ctx->priv_data);
             }
 
-            av_buffer_unref(&ctx->internal->pool);
+            ff_refstruct_unref(&ctx->internal->pool);
             av_packet_free(&ctx->internal->last_pkt_props);
             av_freep(&ctx->internal);
             av_buffer_unref(&ctx->hw_frames_ctx);



More information about the ffmpeg-cvslog mailing list