diff mbox series

[FFmpeg-devel,2/2] lavc/videotoolboxenc: use objectpool for encoder output information

Message ID 20221119213516.45148-3-kernrj@gmail.com
State New
Headers show
Series lavc/videotoolbox: use objpool instead of allocating memory each frame | expand

Checks

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

Commit Message

Rick Kern Nov. 19, 2022, 9:35 p.m. UTC
Instead of allocating/freeing, use objpool for data passed from an
input frame to an output packet.

Signed-off-by: Rick Kern <kernrj@gmail.com>
---
 libavcodec/videotoolboxenc.c | 80 ++++++++++++++++++++++++++++++++----
 1 file changed, 71 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index dc9e321d3d..205cbfbc68 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -24,6 +24,7 @@ 
 #include <TargetConditionals.h>
 #include <Availability.h>
 #include "avcodec.h"
+#include "libavutil/objpool.h"
 #include "libavutil/opt.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
@@ -251,8 +252,49 @@  typedef struct VTEncContext {
 
     /* can't be bool type since AVOption will access it as int */
     int a53_cc;
+
+    ObjPool *output_node_pool;
 } VTEncContext;
 
+static BufNode *output_node_alloc(void) { return av_mallocz(sizeof(BufNode)); }
+static void output_node_free(BufNode **node) { av_freep(node); }
+static void output_node_reset(BufNode *node) {
+  if (node->cm_buffer != NULL) {
+    CFRelease(node->cm_buffer);
+  }
+
+  if (node->sei != NULL) {
+    if (node->sei->data != NULL) {
+      av_free(node->sei->data);
+    }
+
+    av_free(node->sei);
+  }
+
+  memset(node, 0, sizeof(*node));
+}
+
+static int alloc_output_node_pool(ObjPool **pool_out) {
+  ObjPool *pool = NULL;
+
+  if (pool_out == NULL) {
+    return AVERROR(EINVAL);
+  }
+
+  pool = objpool_alloc(
+      output_node_alloc,
+      output_node_reset,
+      output_node_free);
+
+  if (pool == NULL) {
+    return AVERROR(ENOMEM);
+  }
+
+  *pool_out = pool;
+
+  return 0;
+}
+
 static int vtenc_populate_extradata(AVCodecContext   *avctx,
                                     CMVideoCodecType codec_type,
                                     CFStringRef      profile_level,
@@ -285,8 +327,7 @@  static void set_async_error(VTEncContext *vtctx, int err)
 
     while (info) {
         BufNode *next = info->next;
-        CFRelease(info->cm_buffer);
-        av_free(info);
+        objpool_release(vtctx->output_node_pool, &info);
         info = next;
     }
 
@@ -336,22 +377,29 @@  static int vtenc_q_pop(VTEncContext *vtctx, bool wait, CMSampleBufferRef *buf, E
     pthread_mutex_unlock(&vtctx->lock);
 
     *buf = info->cm_buffer;
+    if (*buf != NULL) {
+      CFRetain(*buf);
+    }
+
     if (sei && *buf) {
         *sei = info->sei;
-    } else if (info->sei) {
-        if (info->sei->data) av_free(info->sei->data);
-        av_free(info->sei);
+        info->sei = NULL;
     }
-    av_free(info);
 
+    objpool_release(vtctx->output_node_pool, &info);
 
     return 0;
 }
 
 static void vtenc_q_push(VTEncContext *vtctx, CMSampleBufferRef buffer, ExtraSEI *sei)
 {
-    BufNode *info = av_malloc(sizeof(BufNode));
-    if (!info) {
+    BufNode *info = NULL;
+    int status = objpool_get(vtctx->output_node_pool, &info);
+
+    if (status < 0) {
+      set_async_error(vtctx, status);
+      return;
+    } else if (!info) {
         set_async_error(vtctx, AVERROR(ENOMEM));
         return;
     }
@@ -1589,7 +1637,17 @@  static av_cold int vtenc_init(AVCodecContext *avctx)
     }
     avctx->has_b_frames = vtctx->has_b_frames;
 
-    return 0;
+    status = alloc_output_node_pool(&vtctx->output_node_pool);
+
+    if (status < 0) {
+        av_log(
+          avctx,
+          AV_LOG_ERROR,
+          "Failed to allocate output pool (%d)\n",
+          status);
+    }
+
+    return status;
 }
 
 static void vtenc_get_frame_info(CMSampleBufferRef buffer, bool *is_key_frame)
@@ -2647,6 +2705,10 @@  static av_cold int vtenc_close(AVCodecContext *avctx)
         vtctx->ycbcr_matrix = NULL;
     }
 
+    if (vtctx->output_node_pool != NULL) {
+      objpool_free(&vtctx->output_node_pool);
+    }
+
     return 0;
 }