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
44struct ObjPool {
45 void *pool[32];
46 unsigned int pool_count;
47
51};
52
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
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
81int 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
92void 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
107static void *alloc_packet(void)
108{
109 return av_packet_alloc();
110}
111static void *alloc_frame(void)
112{
113 return av_frame_alloc();
114}
115
116static void reset_packet(void *obj)
117{
118 av_packet_unref(obj);
119}
120static void reset_frame(void *obj)
121{
122 av_frame_unref(obj);
123}
124
125static void free_packet(void **obj)
126{
127 AVPacket *pkt = *obj;
128 av_packet_free(&pkt);
129 *obj = NULL;
130}
131static void free_frame(void **obj)
132{
133 AVFrame *frame = *obj;
134 av_frame_free(&frame);
135 *obj = NULL;
136}
137
139{
141}
143{
145}
static void * alloc_frame(void)
ObjPool * objpool_alloc_packets(void)
ObjPool * objpool_alloc_frames(void)
static void free_packet(void **obj)
void objpool_release(ObjPool *op, void **obj)
static void * alloc_packet(void)
static void reset_frame(void *obj)
ObjPool * objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset, ObjPoolCBFree cb_free)
static void free_frame(void **obj)
void objpool_free(ObjPool **pop)
static void reset_packet(void *obj)
int objpool_get(ObjPool *op, void **obj)
void *(* ObjPoolCBAlloc)(void)
void(* ObjPoolCBFree)(void **)
void(* ObjPoolCBReset)(void *)
ObjPoolCBAlloc alloc
ObjPoolCBReset reset
unsigned int pool_count
ObjPoolCBFree free
void * pool[32]