From: Rick Kern <kernrj@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH v2 2/2] lavc/videotoolboxenc: use objectpool for encoder output information
Date: Sat, 3 Dec 2022 10:50:56 -0500
Message-ID: <20221203155056.74093-3-kernrj@gmail.com> (raw)
In-Reply-To: <20221203155056.74093-1-kernrj@gmail.com>
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 --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index dc9e321d3d..184feb029b 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;
+
+ AVObjPool *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(AVObjPool **pool_out) {
+ AVObjPool *pool = NULL;
+
+ if (pool_out == NULL) {
+ return AVERROR(EINVAL);
+ }
+
+ pool = av_objpool_alloc(
+ (AVObjPoolCBAlloc)output_node_alloc,
+ (AVObjPoolCBReset)output_node_reset,
+ (AVObjPoolCBFree)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);
+ av_objpool_release(vtctx->output_node_pool, (void**)&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);
+ av_objpool_release(vtctx->output_node_pool, (void**)&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 = av_objpool_get(vtctx->output_node_pool, (void**)&info);
+
+ if (status < 0) {
+ set_async_error(vtctx, status);
+ return;
+ } else if (!info) {
set_async_error(vtctx, AVERROR(ENOMEM));
return;
}
@@ -1569,6 +1617,16 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
pthread_mutex_init(&vtctx->lock, NULL);
pthread_cond_init(&vtctx->cv_sample_sent, NULL);
+ 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);
+ }
+
vtctx->session = NULL;
status = vtenc_configure_encoder(avctx);
if (status) return status;
@@ -1589,7 +1647,7 @@ static av_cold int vtenc_init(AVCodecContext *avctx)
}
avctx->has_b_frames = vtctx->has_b_frames;
- return 0;
+ 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) {
+ av_objpool_free(&vtctx->output_node_pool);
+ }
+
return 0;
}
--
2.38.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2022-12-03 15:51 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-19 21:35 [FFmpeg-devel] [PATCH 0/2] lavc/videotoolbox: use objpool instead of allocating memory each frame Rick Kern
2022-11-19 21:35 ` [FFmpeg-devel] [PATCH 1/2] fftools/objpool: move objpool to libavutil Rick Kern
2022-11-19 21:35 ` [FFmpeg-devel] [PATCH 2/2] lavc/videotoolboxenc: use objectpool for encoder output information Rick Kern
2022-11-20 14:47 ` [FFmpeg-devel] [PATCH 0/2] lavc/videotoolbox: use objpool instead of allocating memory each frame Zhao Zhili
2022-12-03 15:50 ` [FFmpeg-devel] [PATCH v2 " Rick Kern
2022-12-03 15:50 ` [FFmpeg-devel] [PATCH v2 1/2] fftools/objpool: move objpool to libavutil Rick Kern
2022-12-03 15:50 ` Rick Kern [this message]
2022-12-03 16:07 ` [FFmpeg-devel] [PATCH v3 0/2] lavc/videotoolbox: use objpool instead of allocating memory each frame Rick Kern
2022-12-03 16:07 ` [FFmpeg-devel] [PATCH v3 1/2] fftools/objpool: move objpool to libavutil Rick Kern
2022-12-03 17:29 ` Lynne
2022-12-03 16:07 ` [FFmpeg-devel] [PATCH v3 2/2] lavc/videotoolboxenc: use objectpool for encoder output information Rick Kern
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221203155056.74093-3-kernrj@gmail.com \
--to=kernrj@gmail.com \
--cc=ffmpeg-devel@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git