diff mbox series

[FFmpeg-devel] avcodec/bsf/dts2pts: use a RefStruct pool to allocate nodes

Message ID 20240914004533.6382-1-jamrial@gmail.com
State New
Headers show
Series [FFmpeg-devel] avcodec/bsf/dts2pts: use a RefStruct pool to allocate nodes | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

James Almer Sept. 14, 2024, 12:45 a.m. UTC
Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/bsf/dts2pts.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/bsf/dts2pts.c b/libavcodec/bsf/dts2pts.c
index ba4dc43f84..2be79c624b 100644
--- a/libavcodec/bsf/dts2pts.c
+++ b/libavcodec/bsf/dts2pts.c
@@ -34,6 +34,7 @@ 
 #include "cbs_h264.h"
 #include "h264_parse.h"
 #include "h264_ps.h"
+#include "refstruct.h"
 
 typedef struct DTS2PTSNode {
     int64_t      dts;
@@ -61,6 +62,7 @@  typedef struct DTS2PTSH264Context {
 typedef struct DTS2PTSContext {
     struct AVTreeNode *root;
     AVFifo *fifo;
+    FFRefStructPool *node_pool;
 
     // Codec specific function pointers and constants
     int (*init)(AVBSFContext *ctx);
@@ -110,7 +112,7 @@  static int dec_poc(void *opaque, void *elem)
 static int free_node(void *opaque, void *elem)
 {
     DTS2PTSNode *node = elem;
-    av_free(node);
+    ff_refstruct_unref(&node);
     return 0;
 }
 
@@ -124,7 +126,7 @@  static int alloc_and_insert_node(AVBSFContext *ctx, int64_t ts, int64_t duration
         DTS2PTSNode *poc_node, *ret;
         if (!node)
             return AVERROR(ENOMEM);
-        poc_node = av_malloc(sizeof(*poc_node));
+        poc_node = ff_refstruct_pool_get(s->node_pool);
         if (!poc_node) {
             av_free(node);
             return AVERROR(ENOMEM);
@@ -135,7 +137,7 @@  static int alloc_and_insert_node(AVBSFContext *ctx, int64_t ts, int64_t duration
         ret = av_tree_insert(&s->root, poc_node, cmp_insert, &node);
         if (ret && ret != poc_node) {
             *ret = *poc_node;
-            av_free(poc_node);
+            ff_refstruct_unref(&poc_node);
             av_free(node);
         }
     }
@@ -394,6 +396,11 @@  static int dts2pts_init(AVBSFContext *ctx)
     if (!s->fifo)
         return AVERROR(ENOMEM);
 
+    s->node_pool = ff_refstruct_pool_alloc(sizeof(DTS2PTSNode), 0);
+
+    if (!s->node_pool)
+        return AVERROR(ENOMEM);
+
     ret = ff_cbs_init(&s->cbc, ctx->par_in->codec_id, ctx);
     if (ret < 0)
         return ret;
@@ -459,7 +466,7 @@  static int dts2pts_filter(AVBSFContext *ctx, AVPacket *out)
                 if (!poc_node || poc_node->dts != out->pts)
                     continue;
                 av_tree_insert(&s->root, poc_node, cmp_insert, &node);
-                av_free(poc_node);
+                ff_refstruct_unref(&poc_node);
                 av_free(node);
                 poc_node = av_tree_find(s->root, &dup, cmp_find, NULL);
             }
@@ -521,6 +528,7 @@  static void dts2pts_close(AVBSFContext *ctx)
     dts2pts_flush(ctx);
 
     av_fifo_freep2(&s->fifo);
+    ff_refstruct_pool_uninit(&s->node_pool);
     ff_cbs_fragment_free(&s->au);
     ff_cbs_close(&s->cbc);
 }