[FFmpeg-devel,03/42] avcodec/get_buffer: Use RefStruct API for FramePool

Message ID AS8P250MB074495448653CB99DF194B2D8FFAA@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM
State Accepted
Commit 78c9ed26b6e83ea29b55420c2c88dc1860810042
Headers
Series New API for reference counting and ThreadFrames |

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt Sept. 19, 2023, 7:56 p.m. UTC
Avoids allocations and frees and error checks for said allocations;
also avoids a few indirections and casts.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 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(-)
  

Comments

Anton Khirnov Sept. 28, 2023, 12:36 p.m. UTC | #1
Quoting Andreas Rheinhardt (2023-09-19 21:56:55)
> Avoids allocations and frees and error checks for said allocations;
> also avoids a few indirections and casts.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  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(-)

LGTM
  

Patch

diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index 131834b6de..6365ab87a6 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -43,6 +43,7 @@ 
 #include "frame_thread_encoder.h"
 #include "hwconfig.h"
 #include "internal.h"
+#include "refstruct.h"
 #include "thread.h"
 
 /**
@@ -458,7 +459,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);