Loading...
Searching...
No Matches
fftools_objpool.c
Go to the documentation of this file.
/*
* This file is part of FFmpeg.
* Copyright (c) 2023 ARTHENICA LTD
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* This file is the modified version of objpool.c file living in ffmpeg source code under the fftools folder. We
* manually update it each time we depend on a new ffmpeg version. Below you can see the list of changes applied
* by us to develop ffmpeg-kit library.
*
* ffmpeg-kit changes by ARTHENICA LTD
*
* 07.2023
* --------------------------------------------------------
* - FFmpeg 6.0 changes migrated
* - fftools header names updated
*/
#include <stdint.h>
#include "libavcodec/packet.h"
#include "libavutil/common.h"
#include "libavutil/error.h"
#include "libavutil/frame.h"
#include "libavutil/mem.h"
#include "fftools_objpool.h"
struct ObjPool {
void *pool[32];
unsigned int pool_count;
ObjPoolCBAlloc alloc;
ObjPoolCBReset reset;
ObjPoolCBFree free;
};
ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset,
ObjPoolCBFree cb_free)
{
ObjPool *op = av_mallocz(sizeof(*op));
if (!op)
return NULL;
op->alloc = cb_alloc;
op->reset = cb_reset;
op->free = cb_free;
return op;
}
void objpool_free(ObjPool **pop)
{
ObjPool *op = *pop;
if (!op)
return;
for (unsigned int i = 0; i < op->pool_count; i++)
op->free(&op->pool[i]);
av_freep(pop);
}
int objpool_get(ObjPool *op, void **obj)
{
if (op->pool_count) {
*obj = op->pool[--op->pool_count];
op->pool[op->pool_count] = NULL;
} else
*obj = op->alloc();
return *obj ? 0 : AVERROR(ENOMEM);
}
void objpool_release(ObjPool *op, void **obj)
{
if (!*obj)
return;
op->reset(*obj);
if (op->pool_count < FF_ARRAY_ELEMS(op->pool))
op->pool[op->pool_count++] = *obj;
else
op->free(obj);
*obj = NULL;
}
static void *alloc_packet(void)
{
return av_packet_alloc();
}
static void *alloc_frame(void)
{
return av_frame_alloc();
}
static void reset_packet(void *obj)
{
av_packet_unref(obj);
}
static void reset_frame(void *obj)
{
av_frame_unref(obj);
}
static void free_packet(void **obj)
{
AVPacket *pkt = *obj;
av_packet_free(&pkt);
*obj = NULL;
}
static void free_frame(void **obj)
{
AVFrame *frame = *obj;
av_frame_free(&frame);
*obj = NULL;
}
ObjPool *objpool_alloc_packets(void)
{
return objpool_alloc(alloc_packet, reset_packet, free_packet);
}
ObjPool *objpool_alloc_frames(void)
{
return objpool_alloc(alloc_frame, reset_frame, free_frame);
}
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