* [FFmpeg-devel] [PATCH 0/2] lavc/videotoolbox: use objpool instead of allocating memory each frame
@ 2022-11-19 21:35 Rick Kern
2022-11-19 21:35 ` [FFmpeg-devel] [PATCH 1/2] fftools/objpool: move objpool to libavutil Rick Kern
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: Rick Kern @ 2022-11-19 21:35 UTC (permalink / raw)
To: ffmpeg-devel
objpool was only being used in fftools, but it's useful in other components to reduce alloation time and memory fragmentation.
This patch set moves objpool to libavutil, so it can be used by lavc/videotoolboxenc for data being passed from input frames to output packets.
Rick Kern (2):
fftools/objpool: move objpool to libavutil
lavc/videotoolboxenc: use objectpool for encoder output information
fftools/Makefile | 1 -
fftools/ffmpeg_mux.c | 2 +-
fftools/sync_queue.c | 2 +-
fftools/thread_queue.c | 2 +-
fftools/thread_queue.h | 2 +-
libavcodec/videotoolboxenc.c | 80 ++++++++++++++++++++++++++++----
libavutil/Makefile | 2 +
{fftools => libavutil}/objpool.c | 0
{fftools => libavutil}/objpool.h | 0
9 files changed, 77 insertions(+), 14 deletions(-)
rename {fftools => libavutil}/objpool.c (100%)
rename {fftools => libavutil}/objpool.h (100%)
--
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".
^ permalink raw reply [flat|nested] 11+ messages in thread
* [FFmpeg-devel] [PATCH 1/2] fftools/objpool: move objpool to libavutil
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 ` 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
2 siblings, 0 replies; 11+ messages in thread
From: Rick Kern @ 2022-11-19 21:35 UTC (permalink / raw)
To: ffmpeg-devel
Moved objpool to libavutil to make it available outside of fftools.
Signed-off-by: Rick Kern <kernrj@gmail.com>
---
fftools/Makefile | 1 -
fftools/ffmpeg_mux.c | 2 +-
fftools/sync_queue.c | 2 +-
fftools/thread_queue.c | 2 +-
fftools/thread_queue.h | 2 +-
libavutil/Makefile | 2 ++
{fftools => libavutil}/objpool.c | 0
{fftools => libavutil}/objpool.h | 0
8 files changed, 6 insertions(+), 5 deletions(-)
rename {fftools => libavutil}/objpool.c (100%)
rename {fftools => libavutil}/objpool.h (100%)
diff --git a/fftools/Makefile b/fftools/Makefile
index 8ac38e75d2..25b11deaff 100644
--- a/fftools/Makefile
+++ b/fftools/Makefile
@@ -16,7 +16,6 @@ OBJS-ffmpeg += \
fftools/ffmpeg_mux.o \
fftools/ffmpeg_mux_init.o \
fftools/ffmpeg_opt.o \
- fftools/objpool.o \
fftools/sync_queue.o \
fftools/thread_queue.o \
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index ad04f5049d..bd1a1ed403 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -22,7 +22,6 @@
#include "ffmpeg.h"
#include "ffmpeg_mux.h"
-#include "objpool.h"
#include "sync_queue.h"
#include "thread_queue.h"
@@ -30,6 +29,7 @@
#include "libavutil/intreadwrite.h"
#include "libavutil/log.h"
#include "libavutil/mem.h"
+#include "libavutil/objpool.h"
#include "libavutil/timestamp.h"
#include "libavutil/thread.h"
diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c
index c2b23ee4f5..75b8dc72fc 100644
--- a/fftools/sync_queue.c
+++ b/fftools/sync_queue.c
@@ -24,8 +24,8 @@
#include "libavutil/fifo.h"
#include "libavutil/mathematics.h"
#include "libavutil/mem.h"
+#include "libavutil/objpool.h"
-#include "objpool.h"
#include "sync_queue.h"
typedef struct SyncQueueStream {
diff --git a/fftools/thread_queue.c b/fftools/thread_queue.c
index a1ab4ce92e..ced7b02fba 100644
--- a/fftools/thread_queue.c
+++ b/fftools/thread_queue.c
@@ -24,9 +24,9 @@
#include "libavutil/fifo.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mem.h"
+#include "libavutil/objpool.h"
#include "libavutil/thread.h"
-#include "objpool.h"
#include "thread_queue.h"
enum {
diff --git a/fftools/thread_queue.h b/fftools/thread_queue.h
index 0cc8c71ebd..6d5862bd1f 100644
--- a/fftools/thread_queue.h
+++ b/fftools/thread_queue.h
@@ -21,7 +21,7 @@
#include <string.h>
-#include "objpool.h"
+#include "libavutil/objpool.h"
typedef struct ThreadQueue ThreadQueue;
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 3d9c07aea8..bd9ac28335 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -62,6 +62,7 @@ HEADERS = adler32.h \
mem.h \
motion_vector.h \
murmur3.h \
+ objpool.h \
opt.h \
parseutils.h \
pixdesc.h \
@@ -149,6 +150,7 @@ OBJS = adler32.o \
md5.o \
mem.o \
murmur3.o \
+ objpool.o \
opt.o \
parseutils.o \
pixdesc.o \
diff --git a/fftools/objpool.c b/libavutil/objpool.c
similarity index 100%
rename from fftools/objpool.c
rename to libavutil/objpool.c
diff --git a/fftools/objpool.h b/libavutil/objpool.h
similarity index 100%
rename from fftools/objpool.h
rename to libavutil/objpool.h
--
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".
^ permalink raw reply [flat|nested] 11+ messages in thread
* [FFmpeg-devel] [PATCH 2/2] lavc/videotoolboxenc: use objectpool for encoder output information
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 ` Rick Kern
2022-11-20 14:47 ` [FFmpeg-devel] [PATCH 0/2] lavc/videotoolbox: use objpool instead of allocating memory each frame Zhao Zhili
2 siblings, 0 replies; 11+ messages in thread
From: Rick Kern @ 2022-11-19 21:35 UTC (permalink / raw)
To: ffmpeg-devel
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..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;
}
--
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".
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [FFmpeg-devel] [PATCH 0/2] lavc/videotoolbox: use objpool instead of allocating memory each frame
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 ` Zhao Zhili
2022-12-03 15:50 ` [FFmpeg-devel] [PATCH v2 " Rick Kern
2 siblings, 1 reply; 11+ messages in thread
From: Zhao Zhili @ 2022-11-20 14:47 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Rick Kern
On Sat, 2022-11-19 at 16:35 -0500, Rick Kern wrote:
> objpool was only being used in fftools, but it's useful in other
> components to reduce alloation time and memory fragmentation.
>
> This patch set moves objpool to libavutil, so it can be used by
> lavc/videotoolboxenc for data being passed from input frames to
> output packets.
>
> Rick Kern (2):
> fftools/objpool: move objpool to libavutil
> lavc/videotoolboxenc: use objectpool for encoder output information
>
> fftools/Makefile | 1 -
> fftools/ffmpeg_mux.c | 2 +-
> fftools/sync_queue.c | 2 +-
> fftools/thread_queue.c | 2 +-
> fftools/thread_queue.h | 2 +-
> libavcodec/videotoolboxenc.c | 80 ++++++++++++++++++++++++++++
> ----
> libavutil/Makefile | 2 +
> {fftools => libavutil}/objpool.c | 0
> {fftools => libavutil}/objpool.h | 0
> 9 files changed, 77 insertions(+), 14 deletions(-)
> rename {fftools => libavutil}/objpool.c (100%)
> rename {fftools => libavutil}/objpool.h (100%)
>
libavutil.v specifies that only symbols with av prefix can be visible
outside of libavutil. So it doesn't work without rename those
functions.
Meanwhile I don't know if it's appropriate to add these functions to
libavutil.
_______________________________________________
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".
^ permalink raw reply [flat|nested] 11+ messages in thread
* [FFmpeg-devel] [PATCH v2 0/2] lavc/videotoolbox: use objpool instead of allocating memory each frame
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 ` Rick Kern
2022-12-03 15:50 ` [FFmpeg-devel] [PATCH v2 1/2] fftools/objpool: move objpool to libavutil Rick Kern
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: Rick Kern @ 2022-12-03 15:50 UTC (permalink / raw)
To: ffmpeg-devel
Added 'av' prefix to ObjPool and its functions.
Rick Kern (2):
fftools/objpool: move objpool to libavutil
lavc/videotoolboxenc: use objectpool for encoder output information
fftools/Makefile | 1 -
fftools/ffmpeg_mux.c | 8 ++--
fftools/sync_queue.c | 18 +++----
fftools/thread_queue.c | 14 +++---
fftools/thread_queue.h | 4 +-
libavcodec/videotoolboxenc.c | 80 ++++++++++++++++++++++++++++----
libavutil/Makefile | 2 +
{fftools => libavutil}/objpool.c | 32 +++++++------
{fftools => libavutil}/objpool.h | 24 +++++-----
9 files changed, 125 insertions(+), 58 deletions(-)
rename {fftools => libavutil}/objpool.c (77%)
rename {fftools => libavutil}/objpool.h (62%)
--
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".
^ permalink raw reply [flat|nested] 11+ messages in thread
* [FFmpeg-devel] [PATCH v2 1/2] fftools/objpool: move objpool to libavutil
2022-12-03 15:50 ` [FFmpeg-devel] [PATCH v2 " Rick Kern
@ 2022-12-03 15:50 ` Rick Kern
2022-12-03 15:50 ` [FFmpeg-devel] [PATCH v2 2/2] lavc/videotoolboxenc: use objectpool for encoder output information Rick Kern
2022-12-03 16:07 ` [FFmpeg-devel] [PATCH v3 0/2] lavc/videotoolbox: use objpool instead of allocating memory each frame Rick Kern
2 siblings, 0 replies; 11+ messages in thread
From: Rick Kern @ 2022-12-03 15:50 UTC (permalink / raw)
To: ffmpeg-devel
Moved objpool to libavutil to make it available outside of fftools.
Signed-off-by: Rick Kern <kernrj@gmail.com>
---
fftools/Makefile | 1 -
fftools/ffmpeg_mux.c | 8 ++++----
fftools/sync_queue.c | 18 +++++++++---------
fftools/thread_queue.c | 14 +++++++-------
fftools/thread_queue.h | 4 ++--
libavutil/Makefile | 2 ++
{fftools => libavutil}/objpool.c | 32 +++++++++++++++++---------------
{fftools => libavutil}/objpool.h | 24 +++++++++++++-----------
8 files changed, 54 insertions(+), 49 deletions(-)
rename {fftools => libavutil}/objpool.c (77%)
rename {fftools => libavutil}/objpool.h (62%)
diff --git a/fftools/Makefile b/fftools/Makefile
index 8ac38e75d2..25b11deaff 100644
--- a/fftools/Makefile
+++ b/fftools/Makefile
@@ -16,7 +16,6 @@ OBJS-ffmpeg += \
fftools/ffmpeg_mux.o \
fftools/ffmpeg_mux_init.o \
fftools/ffmpeg_opt.o \
- fftools/objpool.o \
fftools/sync_queue.o \
fftools/thread_queue.o \
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 20524e5a28..8e4dd0facf 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -22,7 +22,6 @@
#include "ffmpeg.h"
#include "ffmpeg_mux.h"
-#include "objpool.h"
#include "sync_queue.h"
#include "thread_queue.h"
@@ -30,6 +29,7 @@
#include "libavutil/intreadwrite.h"
#include "libavutil/log.h"
#include "libavutil/mem.h"
+#include "libavutil/objpool.h"
#include "libavutil/timestamp.h"
#include "libavutil/thread.h"
@@ -398,16 +398,16 @@ static void pkt_move(void *dst, void *src)
static int thread_start(Muxer *mux)
{
AVFormatContext *fc = mux->fc;
- ObjPool *op;
+ AVObjPool *op;
int ret;
- op = objpool_alloc_packets();
+ op = av_objpool_alloc_packets();
if (!op)
return AVERROR(ENOMEM);
mux->tq = tq_alloc(fc->nb_streams, mux->thread_queue_size, op, pkt_move);
if (!mux->tq) {
- objpool_free(&op);
+ av_objpool_free(&op);
return AVERROR(ENOMEM);
}
diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c
index c2b23ee4f5..bfa91bc129 100644
--- a/fftools/sync_queue.c
+++ b/fftools/sync_queue.c
@@ -24,8 +24,8 @@
#include "libavutil/fifo.h"
#include "libavutil/mathematics.h"
#include "libavutil/mem.h"
+#include "libavutil/objpool.h"
-#include "objpool.h"
#include "sync_queue.h"
typedef struct SyncQueueStream {
@@ -60,7 +60,7 @@ struct SyncQueue {
unsigned int nb_streams;
// pool of preallocated frames to avoid constant allocations
- ObjPool *pool;
+ AVObjPool *pool;
};
static void frame_move(const SyncQueue *sq, SyncQueueFrame dst,
@@ -250,7 +250,7 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
if (st->finished)
return AVERROR_EOF;
- ret = objpool_get(sq->pool, (void**)&dst);
+ ret = av_objpool_get(sq->pool, (void**)&dst);
if (ret < 0)
return ret;
@@ -261,7 +261,7 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
ret = av_fifo_write(st->fifo, &dst, 1);
if (ret < 0) {
frame_move(sq, frame, dst);
- objpool_release(sq->pool, (void**)&dst);
+ av_objpool_release(sq->pool, (void**)&dst);
return ret;
}
@@ -302,7 +302,7 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,
*/
if (cmp <= 0 || ts == AV_NOPTS_VALUE) {
frame_move(sq, frame, peek);
- objpool_release(sq->pool, (void**)&peek);
+ av_objpool_release(sq->pool, (void**)&peek);
av_fifo_drain2(st->fifo, 1);
return 0;
}
@@ -415,8 +415,8 @@ SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us)
sq->head_stream = -1;
sq->head_finished_stream = -1;
- sq->pool = (type == SYNC_QUEUE_PACKETS) ? objpool_alloc_packets() :
- objpool_alloc_frames();
+ sq->pool = (type == SYNC_QUEUE_PACKETS) ? av_objpool_alloc_packets() :
+ av_objpool_alloc_frames();
if (!sq->pool) {
av_freep(&sq);
return NULL;
@@ -435,14 +435,14 @@ void sq_free(SyncQueue **psq)
for (unsigned int i = 0; i < sq->nb_streams; i++) {
SyncQueueFrame frame;
while (av_fifo_read(sq->streams[i].fifo, &frame, 1) >= 0)
- objpool_release(sq->pool, (void**)&frame);
+ av_objpool_release(sq->pool, (void**)&frame);
av_fifo_freep2(&sq->streams[i].fifo);
}
av_freep(&sq->streams);
- objpool_free(&sq->pool);
+ av_objpool_free(&sq->pool);
av_freep(psq);
}
diff --git a/fftools/thread_queue.c b/fftools/thread_queue.c
index a1ab4ce92e..514d12bb6a 100644
--- a/fftools/thread_queue.c
+++ b/fftools/thread_queue.c
@@ -24,9 +24,9 @@
#include "libavutil/fifo.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mem.h"
+#include "libavutil/objpool.h"
#include "libavutil/thread.h"
-#include "objpool.h"
#include "thread_queue.h"
enum {
@@ -45,7 +45,7 @@ struct ThreadQueue {
AVFifo *fifo;
- ObjPool *obj_pool;
+ AVObjPool *obj_pool;
void (*obj_move)(void *dst, void *src);
pthread_mutex_t lock;
@@ -62,11 +62,11 @@ void tq_free(ThreadQueue **ptq)
if (tq->fifo) {
FifoElem elem;
while (av_fifo_read(tq->fifo, &elem, 1) >= 0)
- objpool_release(tq->obj_pool, &elem.obj);
+ av_objpool_release(tq->obj_pool, &elem.obj);
}
av_fifo_freep2(&tq->fifo);
- objpool_free(&tq->obj_pool);
+ av_objpool_free(&tq->obj_pool);
av_freep(&tq->finished);
@@ -77,7 +77,7 @@ void tq_free(ThreadQueue **ptq)
}
ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
- ObjPool *obj_pool, void (*obj_move)(void *dst, void *src))
+ AVObjPool *obj_pool, void (*obj_move)(void *dst, void *src))
{
ThreadQueue *tq;
int ret;
@@ -141,7 +141,7 @@ int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data)
} else {
FifoElem elem = { .stream_idx = stream_idx };
- ret = objpool_get(tq->obj_pool, &elem.obj);
+ ret = av_objpool_get(tq->obj_pool, &elem.obj);
if (ret < 0)
goto finish;
@@ -166,7 +166,7 @@ static int receive_locked(ThreadQueue *tq, int *stream_idx,
if (av_fifo_read(tq->fifo, &elem, 1) >= 0) {
tq->obj_move(data, elem.obj);
- objpool_release(tq->obj_pool, &elem.obj);
+ av_objpool_release(tq->obj_pool, &elem.obj);
*stream_idx = elem.stream_idx;
return 0;
}
diff --git a/fftools/thread_queue.h b/fftools/thread_queue.h
index 0cc8c71ebd..e61260687f 100644
--- a/fftools/thread_queue.h
+++ b/fftools/thread_queue.h
@@ -21,7 +21,7 @@
#include <string.h>
-#include "objpool.h"
+#include "libavutil/objpool.h"
typedef struct ThreadQueue ThreadQueue;
@@ -37,7 +37,7 @@ typedef struct ThreadQueue ThreadQueue;
* @param callback that moves the contents between two data pointers
*/
ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
- ObjPool *obj_pool, void (*obj_move)(void *dst, void *src));
+ AVObjPool *obj_pool, void (*obj_move)(void *dst, void *src));
void tq_free(ThreadQueue **tq);
/**
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 3d9c07aea8..bd9ac28335 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -62,6 +62,7 @@ HEADERS = adler32.h \
mem.h \
motion_vector.h \
murmur3.h \
+ objpool.h \
opt.h \
parseutils.h \
pixdesc.h \
@@ -149,6 +150,7 @@ OBJS = adler32.o \
md5.o \
mem.o \
murmur3.o \
+ objpool.o \
opt.o \
parseutils.o \
pixdesc.o \
diff --git a/fftools/objpool.c b/libavutil/objpool.c
similarity index 77%
rename from fftools/objpool.c
rename to libavutil/objpool.c
index 87237cf724..2309afb5b2 100644
--- a/fftools/objpool.c
+++ b/libavutil/objpool.c
@@ -27,19 +27,21 @@
#include "objpool.h"
-struct ObjPool {
+struct AVObjPool {
void *pool[32];
unsigned int pool_count;
- ObjPoolCBAlloc alloc;
- ObjPoolCBReset reset;
- ObjPoolCBFree free;
+ AVObjPoolCBAlloc alloc;
+ AVObjPoolCBReset reset;
+ AVObjPoolCBFree free;
};
-ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset,
- ObjPoolCBFree cb_free)
+AVObjPool *av_objpool_alloc(
+ AVObjPoolCBAlloc cb_alloc,
+ AVObjPoolCBReset cb_reset,
+ AVObjPoolCBFree cb_free)
{
- ObjPool *op = av_mallocz(sizeof(*op));
+ AVObjPool *op = av_mallocz(sizeof(*op));
if (!op)
return NULL;
@@ -51,9 +53,9 @@ ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset,
return op;
}
-void objpool_free(ObjPool **pop)
+void av_objpool_free(AVObjPool **pop)
{
- ObjPool *op = *pop;
+ AVObjPool *op = *pop;
if (!op)
return;
@@ -64,7 +66,7 @@ void objpool_free(ObjPool **pop)
av_freep(pop);
}
-int objpool_get(ObjPool *op, void **obj)
+int av_objpool_get(AVObjPool *op, void **obj)
{
if (op->pool_count) {
*obj = op->pool[--op->pool_count];
@@ -75,7 +77,7 @@ int objpool_get(ObjPool *op, void **obj)
return *obj ? 0 : AVERROR(ENOMEM);
}
-void objpool_release(ObjPool *op, void **obj)
+void av_objpool_release(AVObjPool *op, void **obj)
{
if (!*obj)
return;
@@ -121,11 +123,11 @@ static void free_frame(void **obj)
*obj = NULL;
}
-ObjPool *objpool_alloc_packets(void)
+AVObjPool *av_objpool_alloc_packets(void)
{
- return objpool_alloc(alloc_packet, reset_packet, free_packet);
+ return av_objpool_alloc(alloc_packet, reset_packet, free_packet);
}
-ObjPool *objpool_alloc_frames(void)
+AVObjPool *av_objpool_alloc_frames(void)
{
- return objpool_alloc(alloc_frame, reset_frame, free_frame);
+ return av_objpool_alloc(alloc_frame, reset_frame, free_frame);
}
diff --git a/fftools/objpool.h b/libavutil/objpool.h
similarity index 62%
rename from fftools/objpool.h
rename to libavutil/objpool.h
index 1b2aea6aca..9dcdc999a4 100644
--- a/fftools/objpool.h
+++ b/libavutil/objpool.h
@@ -19,19 +19,21 @@
#ifndef FFTOOLS_OBJPOOL_H
#define FFTOOLS_OBJPOOL_H
-typedef struct ObjPool ObjPool;
+typedef struct AVObjPool AVObjPool;
-typedef void* (*ObjPoolCBAlloc)(void);
-typedef void (*ObjPoolCBReset)(void *);
-typedef void (*ObjPoolCBFree)(void **);
+typedef void* (*AVObjPoolCBAlloc)(void);
+typedef void (*AVObjPoolCBReset)(void *);
+typedef void (*AVObjPoolCBFree)(void **);
-void objpool_free(ObjPool **op);
-ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset,
- ObjPoolCBFree cb_free);
-ObjPool *objpool_alloc_packets(void);
-ObjPool *objpool_alloc_frames(void);
+void av_objpool_free(AVObjPool **op);
+AVObjPool *av_objpool_alloc(
+ AVObjPoolCBAlloc cb_alloc,
+ AVObjPoolCBReset cb_reset,
+ AVObjPoolCBFree cb_free);
+AVObjPool *av_objpool_alloc_packets(void);
+AVObjPool *av_objpool_alloc_frames(void);
-int objpool_get(ObjPool *op, void **obj);
-void objpool_release(ObjPool *op, void **obj);
+int av_objpool_get(AVObjPool *op, void **obj);
+void av_objpool_release(AVObjPool *op, void **obj);
#endif // FFTOOLS_OBJPOOL_H
--
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".
^ permalink raw reply [flat|nested] 11+ messages in thread
* [FFmpeg-devel] [PATCH v2 2/2] lavc/videotoolboxenc: use objectpool for encoder output information
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
2022-12-03 16:07 ` [FFmpeg-devel] [PATCH v3 0/2] lavc/videotoolbox: use objpool instead of allocating memory each frame Rick Kern
2 siblings, 0 replies; 11+ messages in thread
From: Rick Kern @ 2022-12-03 15:50 UTC (permalink / raw)
To: ffmpeg-devel
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".
^ permalink raw reply [flat|nested] 11+ messages in thread
* [FFmpeg-devel] [PATCH v3 0/2] lavc/videotoolbox: use objpool instead of allocating memory each frame
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 ` [FFmpeg-devel] [PATCH v2 2/2] lavc/videotoolboxenc: use objectpool for encoder output information Rick Kern
@ 2022-12-03 16:07 ` Rick Kern
2022-12-03 16:07 ` [FFmpeg-devel] [PATCH v3 1/2] fftools/objpool: move objpool to libavutil Rick Kern
2022-12-03 16:07 ` [FFmpeg-devel] [PATCH v3 2/2] lavc/videotoolboxenc: use objectpool for encoder output information Rick Kern
2 siblings, 2 replies; 11+ messages in thread
From: Rick Kern @ 2022-12-03 16:07 UTC (permalink / raw)
To: ffmpeg-devel
Update inclusion guards in objpool.h start with AVUTIL_ instead of FFTOOLS_.
Rick Kern (2):
fftools/objpool: move objpool to libavutil
lavc/videotoolboxenc: use objectpool for encoder output information
fftools/Makefile | 1 -
fftools/ffmpeg_mux.c | 8 ++--
fftools/sync_queue.c | 18 +++----
fftools/thread_queue.c | 14 +++---
fftools/thread_queue.h | 4 +-
libavcodec/videotoolboxenc.c | 80 ++++++++++++++++++++++++++++----
libavutil/Makefile | 2 +
{fftools => libavutil}/objpool.c | 32 +++++++------
{fftools => libavutil}/objpool.h | 30 ++++++------
9 files changed, 128 insertions(+), 61 deletions(-)
rename {fftools => libavutil}/objpool.c (77%)
rename {fftools => libavutil}/objpool.h (56%)
--
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".
^ permalink raw reply [flat|nested] 11+ messages in thread
* [FFmpeg-devel] [PATCH v3 1/2] fftools/objpool: move objpool to libavutil
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 ` 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
1 sibling, 1 reply; 11+ messages in thread
From: Rick Kern @ 2022-12-03 16:07 UTC (permalink / raw)
To: ffmpeg-devel
Moved objpool to libavutil to make it available outside of fftools.
Signed-off-by: Rick Kern <kernrj@gmail.com>
---
fftools/Makefile | 1 -
fftools/ffmpeg_mux.c | 8 ++++----
fftools/sync_queue.c | 18 +++++++++---------
fftools/thread_queue.c | 14 +++++++-------
fftools/thread_queue.h | 4 ++--
libavutil/Makefile | 2 ++
{fftools => libavutil}/objpool.c | 32 +++++++++++++++++---------------
{fftools => libavutil}/objpool.h | 30 ++++++++++++++++--------------
8 files changed, 57 insertions(+), 52 deletions(-)
rename {fftools => libavutil}/objpool.c (77%)
rename {fftools => libavutil}/objpool.h (56%)
diff --git a/fftools/Makefile b/fftools/Makefile
index 8ac38e75d2..25b11deaff 100644
--- a/fftools/Makefile
+++ b/fftools/Makefile
@@ -16,7 +16,6 @@ OBJS-ffmpeg += \
fftools/ffmpeg_mux.o \
fftools/ffmpeg_mux_init.o \
fftools/ffmpeg_opt.o \
- fftools/objpool.o \
fftools/sync_queue.o \
fftools/thread_queue.o \
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 20524e5a28..8e4dd0facf 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -22,7 +22,6 @@
#include "ffmpeg.h"
#include "ffmpeg_mux.h"
-#include "objpool.h"
#include "sync_queue.h"
#include "thread_queue.h"
@@ -30,6 +29,7 @@
#include "libavutil/intreadwrite.h"
#include "libavutil/log.h"
#include "libavutil/mem.h"
+#include "libavutil/objpool.h"
#include "libavutil/timestamp.h"
#include "libavutil/thread.h"
@@ -398,16 +398,16 @@ static void pkt_move(void *dst, void *src)
static int thread_start(Muxer *mux)
{
AVFormatContext *fc = mux->fc;
- ObjPool *op;
+ AVObjPool *op;
int ret;
- op = objpool_alloc_packets();
+ op = av_objpool_alloc_packets();
if (!op)
return AVERROR(ENOMEM);
mux->tq = tq_alloc(fc->nb_streams, mux->thread_queue_size, op, pkt_move);
if (!mux->tq) {
- objpool_free(&op);
+ av_objpool_free(&op);
return AVERROR(ENOMEM);
}
diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c
index c2b23ee4f5..bfa91bc129 100644
--- a/fftools/sync_queue.c
+++ b/fftools/sync_queue.c
@@ -24,8 +24,8 @@
#include "libavutil/fifo.h"
#include "libavutil/mathematics.h"
#include "libavutil/mem.h"
+#include "libavutil/objpool.h"
-#include "objpool.h"
#include "sync_queue.h"
typedef struct SyncQueueStream {
@@ -60,7 +60,7 @@ struct SyncQueue {
unsigned int nb_streams;
// pool of preallocated frames to avoid constant allocations
- ObjPool *pool;
+ AVObjPool *pool;
};
static void frame_move(const SyncQueue *sq, SyncQueueFrame dst,
@@ -250,7 +250,7 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
if (st->finished)
return AVERROR_EOF;
- ret = objpool_get(sq->pool, (void**)&dst);
+ ret = av_objpool_get(sq->pool, (void**)&dst);
if (ret < 0)
return ret;
@@ -261,7 +261,7 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
ret = av_fifo_write(st->fifo, &dst, 1);
if (ret < 0) {
frame_move(sq, frame, dst);
- objpool_release(sq->pool, (void**)&dst);
+ av_objpool_release(sq->pool, (void**)&dst);
return ret;
}
@@ -302,7 +302,7 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,
*/
if (cmp <= 0 || ts == AV_NOPTS_VALUE) {
frame_move(sq, frame, peek);
- objpool_release(sq->pool, (void**)&peek);
+ av_objpool_release(sq->pool, (void**)&peek);
av_fifo_drain2(st->fifo, 1);
return 0;
}
@@ -415,8 +415,8 @@ SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us)
sq->head_stream = -1;
sq->head_finished_stream = -1;
- sq->pool = (type == SYNC_QUEUE_PACKETS) ? objpool_alloc_packets() :
- objpool_alloc_frames();
+ sq->pool = (type == SYNC_QUEUE_PACKETS) ? av_objpool_alloc_packets() :
+ av_objpool_alloc_frames();
if (!sq->pool) {
av_freep(&sq);
return NULL;
@@ -435,14 +435,14 @@ void sq_free(SyncQueue **psq)
for (unsigned int i = 0; i < sq->nb_streams; i++) {
SyncQueueFrame frame;
while (av_fifo_read(sq->streams[i].fifo, &frame, 1) >= 0)
- objpool_release(sq->pool, (void**)&frame);
+ av_objpool_release(sq->pool, (void**)&frame);
av_fifo_freep2(&sq->streams[i].fifo);
}
av_freep(&sq->streams);
- objpool_free(&sq->pool);
+ av_objpool_free(&sq->pool);
av_freep(psq);
}
diff --git a/fftools/thread_queue.c b/fftools/thread_queue.c
index a1ab4ce92e..514d12bb6a 100644
--- a/fftools/thread_queue.c
+++ b/fftools/thread_queue.c
@@ -24,9 +24,9 @@
#include "libavutil/fifo.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mem.h"
+#include "libavutil/objpool.h"
#include "libavutil/thread.h"
-#include "objpool.h"
#include "thread_queue.h"
enum {
@@ -45,7 +45,7 @@ struct ThreadQueue {
AVFifo *fifo;
- ObjPool *obj_pool;
+ AVObjPool *obj_pool;
void (*obj_move)(void *dst, void *src);
pthread_mutex_t lock;
@@ -62,11 +62,11 @@ void tq_free(ThreadQueue **ptq)
if (tq->fifo) {
FifoElem elem;
while (av_fifo_read(tq->fifo, &elem, 1) >= 0)
- objpool_release(tq->obj_pool, &elem.obj);
+ av_objpool_release(tq->obj_pool, &elem.obj);
}
av_fifo_freep2(&tq->fifo);
- objpool_free(&tq->obj_pool);
+ av_objpool_free(&tq->obj_pool);
av_freep(&tq->finished);
@@ -77,7 +77,7 @@ void tq_free(ThreadQueue **ptq)
}
ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
- ObjPool *obj_pool, void (*obj_move)(void *dst, void *src))
+ AVObjPool *obj_pool, void (*obj_move)(void *dst, void *src))
{
ThreadQueue *tq;
int ret;
@@ -141,7 +141,7 @@ int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data)
} else {
FifoElem elem = { .stream_idx = stream_idx };
- ret = objpool_get(tq->obj_pool, &elem.obj);
+ ret = av_objpool_get(tq->obj_pool, &elem.obj);
if (ret < 0)
goto finish;
@@ -166,7 +166,7 @@ static int receive_locked(ThreadQueue *tq, int *stream_idx,
if (av_fifo_read(tq->fifo, &elem, 1) >= 0) {
tq->obj_move(data, elem.obj);
- objpool_release(tq->obj_pool, &elem.obj);
+ av_objpool_release(tq->obj_pool, &elem.obj);
*stream_idx = elem.stream_idx;
return 0;
}
diff --git a/fftools/thread_queue.h b/fftools/thread_queue.h
index 0cc8c71ebd..e61260687f 100644
--- a/fftools/thread_queue.h
+++ b/fftools/thread_queue.h
@@ -21,7 +21,7 @@
#include <string.h>
-#include "objpool.h"
+#include "libavutil/objpool.h"
typedef struct ThreadQueue ThreadQueue;
@@ -37,7 +37,7 @@ typedef struct ThreadQueue ThreadQueue;
* @param callback that moves the contents between two data pointers
*/
ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
- ObjPool *obj_pool, void (*obj_move)(void *dst, void *src));
+ AVObjPool *obj_pool, void (*obj_move)(void *dst, void *src));
void tq_free(ThreadQueue **tq);
/**
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 3d9c07aea8..bd9ac28335 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -62,6 +62,7 @@ HEADERS = adler32.h \
mem.h \
motion_vector.h \
murmur3.h \
+ objpool.h \
opt.h \
parseutils.h \
pixdesc.h \
@@ -149,6 +150,7 @@ OBJS = adler32.o \
md5.o \
mem.o \
murmur3.o \
+ objpool.o \
opt.o \
parseutils.o \
pixdesc.o \
diff --git a/fftools/objpool.c b/libavutil/objpool.c
similarity index 77%
rename from fftools/objpool.c
rename to libavutil/objpool.c
index 87237cf724..2309afb5b2 100644
--- a/fftools/objpool.c
+++ b/libavutil/objpool.c
@@ -27,19 +27,21 @@
#include "objpool.h"
-struct ObjPool {
+struct AVObjPool {
void *pool[32];
unsigned int pool_count;
- ObjPoolCBAlloc alloc;
- ObjPoolCBReset reset;
- ObjPoolCBFree free;
+ AVObjPoolCBAlloc alloc;
+ AVObjPoolCBReset reset;
+ AVObjPoolCBFree free;
};
-ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset,
- ObjPoolCBFree cb_free)
+AVObjPool *av_objpool_alloc(
+ AVObjPoolCBAlloc cb_alloc,
+ AVObjPoolCBReset cb_reset,
+ AVObjPoolCBFree cb_free)
{
- ObjPool *op = av_mallocz(sizeof(*op));
+ AVObjPool *op = av_mallocz(sizeof(*op));
if (!op)
return NULL;
@@ -51,9 +53,9 @@ ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset,
return op;
}
-void objpool_free(ObjPool **pop)
+void av_objpool_free(AVObjPool **pop)
{
- ObjPool *op = *pop;
+ AVObjPool *op = *pop;
if (!op)
return;
@@ -64,7 +66,7 @@ void objpool_free(ObjPool **pop)
av_freep(pop);
}
-int objpool_get(ObjPool *op, void **obj)
+int av_objpool_get(AVObjPool *op, void **obj)
{
if (op->pool_count) {
*obj = op->pool[--op->pool_count];
@@ -75,7 +77,7 @@ int objpool_get(ObjPool *op, void **obj)
return *obj ? 0 : AVERROR(ENOMEM);
}
-void objpool_release(ObjPool *op, void **obj)
+void av_objpool_release(AVObjPool *op, void **obj)
{
if (!*obj)
return;
@@ -121,11 +123,11 @@ static void free_frame(void **obj)
*obj = NULL;
}
-ObjPool *objpool_alloc_packets(void)
+AVObjPool *av_objpool_alloc_packets(void)
{
- return objpool_alloc(alloc_packet, reset_packet, free_packet);
+ return av_objpool_alloc(alloc_packet, reset_packet, free_packet);
}
-ObjPool *objpool_alloc_frames(void)
+AVObjPool *av_objpool_alloc_frames(void)
{
- return objpool_alloc(alloc_frame, reset_frame, free_frame);
+ return av_objpool_alloc(alloc_frame, reset_frame, free_frame);
}
diff --git a/fftools/objpool.h b/libavutil/objpool.h
similarity index 56%
rename from fftools/objpool.h
rename to libavutil/objpool.h
index 1b2aea6aca..a2d16e8687 100644
--- a/fftools/objpool.h
+++ b/libavutil/objpool.h
@@ -16,22 +16,24 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#ifndef FFTOOLS_OBJPOOL_H
-#define FFTOOLS_OBJPOOL_H
+#ifndef AVUTIL_OBJPOOL_H
+#define AVUTIL_OBJPOOL_H
-typedef struct ObjPool ObjPool;
+typedef struct AVObjPool AVObjPool;
-typedef void* (*ObjPoolCBAlloc)(void);
-typedef void (*ObjPoolCBReset)(void *);
-typedef void (*ObjPoolCBFree)(void **);
+typedef void* (*AVObjPoolCBAlloc)(void);
+typedef void (*AVObjPoolCBReset)(void *);
+typedef void (*AVObjPoolCBFree)(void **);
-void objpool_free(ObjPool **op);
-ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset,
- ObjPoolCBFree cb_free);
-ObjPool *objpool_alloc_packets(void);
-ObjPool *objpool_alloc_frames(void);
+void av_objpool_free(AVObjPool **op);
+AVObjPool *av_objpool_alloc(
+ AVObjPoolCBAlloc cb_alloc,
+ AVObjPoolCBReset cb_reset,
+ AVObjPoolCBFree cb_free);
+AVObjPool *av_objpool_alloc_packets(void);
+AVObjPool *av_objpool_alloc_frames(void);
-int objpool_get(ObjPool *op, void **obj);
-void objpool_release(ObjPool *op, void **obj);
+int av_objpool_get(AVObjPool *op, void **obj);
+void av_objpool_release(AVObjPool *op, void **obj);
-#endif // FFTOOLS_OBJPOOL_H
+#endif // AVUTIL_OBJPOOL_H
--
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".
^ permalink raw reply [flat|nested] 11+ messages in thread
* [FFmpeg-devel] [PATCH v3 2/2] lavc/videotoolboxenc: use objectpool for encoder output information
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 16:07 ` Rick Kern
1 sibling, 0 replies; 11+ messages in thread
From: Rick Kern @ 2022-12-03 16:07 UTC (permalink / raw)
To: ffmpeg-devel
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".
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [FFmpeg-devel] [PATCH v3 1/2] fftools/objpool: move objpool to libavutil
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
0 siblings, 0 replies; 11+ messages in thread
From: Lynne @ 2022-12-03 17:29 UTC (permalink / raw)
To: FFmpeg development discussions and patches
Dec 3, 2022, 17:07 by kernrj@gmail.com:
> Moved objpool to libavutil to make it available outside of fftools.
>
> Signed-off-by: Rick Kern <> kernrj@gmail.com> >
>
No, we already have better pooling APIs, we don't need 3rd or fourth one
in libavutil. Use AVBufferPool or AVFifo.
_______________________________________________
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".
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2022-12-03 17:29 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [FFmpeg-devel] [PATCH v2 2/2] lavc/videotoolboxenc: use objectpool for encoder output information Rick Kern
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
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