| FFmpegKit Linux API 6.0

Loading...
Searching...
No Matches
fftools_objpool.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 * Copyright (c) 2023 ARTHENICA LTD
4 *
5 * FFmpeg is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * FFmpeg is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with FFmpeg; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/*
21 * This file is the modified version of objpool.c file living in ffmpeg source code under the fftools folder. We
22 * manually update it each time we depend on a new ffmpeg version. Below you can see the list of changes applied
23 * by us to develop ffmpeg-kit library.
24 *
25 * ffmpeg-kit changes by ARTHENICA LTD
26 *
27 * 07.2023
28 * --------------------------------------------------------
29 * - FFmpeg 6.0 changes migrated
30 * - fftools header names updated
31 */
32
33#include <stdint.h>
34
35#include "libavcodec/packet.h"
36
37#include "libavutil/common.h"
38#include "libavutil/error.h"
39#include "libavutil/frame.h"
40#include "libavutil/mem.h"
41
42#include "fftools_objpool.h"
43
44 struct ObjPool {
45 void *pool[32];
46 unsigned int pool_count;
47
48 ObjPoolCBAlloc alloc;
49 ObjPoolCBReset reset;
50 ObjPoolCBFree free;
51};
52
53 ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset,
54 ObjPoolCBFree cb_free)
55{
56 ObjPool *op = av_mallocz(sizeof(*op));
57
58 if (!op)
59 return NULL;
60
61 op->alloc = cb_alloc;
62 op->reset = cb_reset;
63 op->free = cb_free;
64
65 return op;
66}
67
68 void objpool_free(ObjPool **pop)
69{
70 ObjPool *op = *pop;
71
72 if (!op)
73 return;
74
75 for (unsigned int i = 0; i < op->pool_count; i++)
76 op->free(&op->pool[i]);
77
78 av_freep(pop);
79}
80
81 int objpool_get(ObjPool *op, void **obj)
82{
83 if (op->pool_count) {
84 *obj = op->pool[--op->pool_count];
85 op->pool[op->pool_count] = NULL;
86 } else
87 *obj = op->alloc();
88
89 return *obj ? 0 : AVERROR(ENOMEM);
90}
91
92 void objpool_release(ObjPool *op, void **obj)
93{
94 if (!*obj)
95 return;
96
97 op->reset(*obj);
98
99 if (op->pool_count < FF_ARRAY_ELEMS(op->pool))
100 op->pool[op->pool_count++] = *obj;
101 else
102 op->free(obj);
103
104 *obj = NULL;
105}
106
107 static void *alloc_packet(void)
108{
109 return av_packet_alloc();
110}
111 static void *alloc_frame(void)
112{
113 return av_frame_alloc();
114}
115
116 static void reset_packet(void *obj)
117{
118 av_packet_unref(obj);
119}
120 static void reset_frame(void *obj)
121{
122 av_frame_unref(obj);
123}
124
125 static void free_packet(void **obj)
126{
127 AVPacket *pkt = *obj;
128 av_packet_free(&pkt);
129 *obj = NULL;
130}
131 static void free_frame(void **obj)
132{
133 AVFrame *frame = *obj;
134 av_frame_free(&frame);
135 *obj = NULL;
136}
137
138 ObjPool *objpool_alloc_packets(void)
139{
140 return objpool_alloc(alloc_packet, reset_packet, free_packet);
141}
142 ObjPool *objpool_alloc_frames(void)
143{
144 return objpool_alloc(alloc_frame, reset_frame, free_frame);
145}
alloc_frame
static void * alloc_frame(void)
Definition fftools_objpool.c:111
objpool_alloc_packets
ObjPool * objpool_alloc_packets(void)
Definition fftools_objpool.c:138
objpool_alloc_frames
ObjPool * objpool_alloc_frames(void)
Definition fftools_objpool.c:142
free_packet
static void free_packet(void **obj)
Definition fftools_objpool.c:125
objpool_release
void objpool_release(ObjPool *op, void **obj)
Definition fftools_objpool.c:92
alloc_packet
static void * alloc_packet(void)
Definition fftools_objpool.c:107
reset_frame
static void reset_frame(void *obj)
Definition fftools_objpool.c:120
objpool_alloc
ObjPool * objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset, ObjPoolCBFree cb_free)
Definition fftools_objpool.c:53
free_frame
static void free_frame(void **obj)
Definition fftools_objpool.c:131
objpool_free
void objpool_free(ObjPool **pop)
Definition fftools_objpool.c:68
reset_packet
static void reset_packet(void *obj)
Definition fftools_objpool.c:116
objpool_get
int objpool_get(ObjPool *op, void **obj)
Definition fftools_objpool.c:81
fftools_objpool.h
ObjPoolCBAlloc
void *(* ObjPoolCBAlloc)(void)
Definition fftools_objpool.h:37
ObjPoolCBFree
void(* ObjPoolCBFree)(void **)
Definition fftools_objpool.h:39
ObjPoolCBReset
void(* ObjPoolCBReset)(void *)
Definition fftools_objpool.h:38
ObjPool
Definition fftools_objpool.c:44
ObjPool::alloc
ObjPoolCBAlloc alloc
Definition fftools_objpool.c:48
ObjPool::reset
ObjPoolCBReset reset
Definition fftools_objpool.c:49
ObjPool::pool_count
unsigned int pool_count
Definition fftools_objpool.c:46
ObjPool::free
ObjPoolCBFree free
Definition fftools_objpool.c:50
ObjPool::pool
void * pool[32]
Definition fftools_objpool.c:45