FFmpeg
objpool.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <stdint.h>
20 
21 #include "libavcodec/packet.h"
22 
23 #include "libavutil/common.h"
24 #include "libavutil/error.h"
25 #include "libavutil/frame.h"
26 #include "libavutil/mem.h"
27 
28 #include "objpool.h"
29 
30 struct ObjPool {
31  void *pool[32];
32  unsigned int pool_count;
33 
37 };
38 
40  ObjPoolCBFree cb_free)
41 {
42  ObjPool *op = av_mallocz(sizeof(*op));
43 
44  if (!op)
45  return NULL;
46 
47  op->alloc = cb_alloc;
48  op->reset = cb_reset;
49  op->free = cb_free;
50 
51  return op;
52 }
53 
55 {
56  ObjPool *op = *pop;
57 
58  if (!op)
59  return;
60 
61  for (unsigned int i = 0; i < op->pool_count; i++)
62  op->free(&op->pool[i]);
63 
64  av_freep(pop);
65 }
66 
67 int objpool_get(ObjPool *op, void **obj)
68 {
69  if (op->pool_count) {
70  *obj = op->pool[--op->pool_count];
71  op->pool[op->pool_count] = NULL;
72  } else
73  *obj = op->alloc();
74 
75  return *obj ? 0 : AVERROR(ENOMEM);
76 }
77 
78 void objpool_release(ObjPool *op, void **obj)
79 {
80  if (!*obj)
81  return;
82 
83  op->reset(*obj);
84 
85  if (op->pool_count < FF_ARRAY_ELEMS(op->pool))
86  op->pool[op->pool_count++] = *obj;
87  else
88  op->free(obj);
89 
90  *obj = NULL;
91 }
92 
93 static void *alloc_packet(void)
94 {
95  return av_packet_alloc();
96 }
97 static void *alloc_frame(void)
98 {
99  return av_frame_alloc();
100 }
101 
102 static void reset_packet(void *obj)
103 {
104  av_packet_unref(obj);
105 }
106 static void reset_frame(void *obj)
107 {
108  av_frame_unref(obj);
109 }
110 
111 static void free_packet(void **obj)
112 {
113  AVPacket *pkt = *obj;
115  *obj = NULL;
116 }
117 static void free_frame(void **obj)
118 {
119  AVFrame *frame = *obj;
121  *obj = NULL;
122 }
123 
125 {
127 }
129 {
131 }
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:429
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ObjPoolCBAlloc
void *(* ObjPoolCBAlloc)(void)
Definition: objpool.h:24
ObjPool::free
ObjPoolCBFree free
Definition: objpool.c:36
ObjPool::alloc
ObjPoolCBAlloc alloc
Definition: objpool.c:34
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
objpool_free
void objpool_free(ObjPool **pop)
Definition: objpool.c:54
objpool_alloc_packets
ObjPool * objpool_alloc_packets(void)
Definition: objpool.c:124
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: packet.c:74
objpool.h
objpool_release
void objpool_release(ObjPool *op, void **obj)
Definition: objpool.c:78
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
ObjPool::reset
ObjPoolCBReset reset
Definition: objpool.c:35
pkt
AVPacket * pkt
Definition: movenc.c:60
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
reset_packet
static void reset_packet(void *obj)
Definition: objpool.c:102
op
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition: anm.c:76
objpool_alloc_frames
ObjPool * objpool_alloc_frames(void)
Definition: objpool.c:128
NULL
#define NULL
Definition: coverity.c:32
ObjPool::pool_count
unsigned int pool_count
Definition: objpool.c:32
ObjPoolCBReset
void(* ObjPoolCBReset)(void *)
Definition: objpool.h:25
reset_frame
static void reset_frame(void *obj)
Definition: objpool.c:106
objpool_get
int objpool_get(ObjPool *op, void **obj)
Definition: objpool.c:67
error.h
alloc_packet
static void * alloc_packet(void)
Definition: objpool.c:93
ObjPool::pool
void * pool[32]
Definition: objpool.c:31
frame.h
ObjPool
Definition: objpool.c:30
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: packet.c:63
ObjPoolCBFree
void(* ObjPoolCBFree)(void **)
Definition: objpool.h:26
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
free_packet
static void free_packet(void **obj)
Definition: objpool.c:111
packet.h
alloc_frame
static void * alloc_frame(void)
Definition: objpool.c:97
common.h
objpool_alloc
ObjPool * objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset, ObjPoolCBFree cb_free)
Definition: objpool.c:39
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:610
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
mem.h
free_frame
static void free_frame(void **obj)
Definition: objpool.c:117
AVPacket
This structure stores compressed data.
Definition: packet.h:516
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
pop
static void pop(HysteresisContext *s, int *x, int *y)
Definition: vf_hysteresis.c:146