[FFmpeg-devel] [PATCH v2 1/2] fftools/objpool: move objpool to libavutil

Rick Kern kernrj at gmail.com
Sat Dec 3 17:50:55 EET 2022


Moved objpool to libavutil to make it available outside of fftools.

Signed-off-by: Rick Kern <kernrj at 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



More information about the ffmpeg-devel mailing list