FFmpeg
container_fifo.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 "avassert.h"
20 #include "container_fifo.h"
21 #include "error.h"
22 #include "fifo.h"
23 #include "frame.h"
24 #include "mem.h"
25 #include "refstruct.h"
26 
30 
31  void *opaque;
32  void* (*container_alloc)(void *opaque);
33  void (*container_reset)(void *opaque, void *obj);
34  void (*container_free) (void *opaque, void *obj);
35  int (*fifo_transfer) (void *opaque, void *dst, void *src, unsigned flags);
36 
37 };
38 
39 static int container_fifo_init_entry(AVRefStructOpaque opaque, void *obj)
40 {
41  AVContainerFifo *cf = opaque.nc;
42  void **pobj = obj;
43 
44  *pobj = cf->container_alloc(cf->opaque);
45  if (!*pobj)
46  return AVERROR(ENOMEM);
47 
48  return 0;
49 }
50 
51 static void container_fifo_reset_entry(AVRefStructOpaque opaque, void *obj)
52 {
53  AVContainerFifo *cf = opaque.nc;
54  cf->container_reset(cf->opaque, *(void**)obj);
55 }
56 
57 static void container_fifo_free_entry(AVRefStructOpaque opaque, void *obj)
58 {
59  AVContainerFifo *cf = opaque.nc;
60  cf->container_free(cf->opaque, *(void**)obj);
61 }
62 
65  void* (*container_alloc)(void *opaque),
66  void (*container_reset)(void *opaque, void *obj),
67  void (*container_free) (void *opaque, void *obj),
68  int (*fifo_transfer) (void *opaque, void *dst, void *src, unsigned flags),
69  unsigned flags)
70 {
71  AVContainerFifo *cf;
72 
73  cf = av_mallocz(sizeof(*cf));
74  if (!cf)
75  return NULL;
76 
77  cf->opaque = opaque;
78  cf->container_alloc = container_alloc;
79  cf->container_reset = container_reset;
80  cf->container_free = container_free;
81  cf->fifo_transfer = fifo_transfer;
82 
83  cf->fifo = av_fifo_alloc2(1, sizeof(void*), AV_FIFO_FLAG_AUTO_GROW);
84  if (!cf->fifo)
85  goto fail;
86 
87  cf->pool = av_refstruct_pool_alloc_ext(sizeof(void*), 0, cf,
91  NULL);
92  if (!cf->pool)
93  goto fail;
94 
95  return cf;
96 fail:
98  return NULL;
99 }
100 
102 {
103  AVContainerFifo *cf;
104 
105  if (!*pcf)
106  return;
107 
108  cf = *pcf;
109 
110  if (cf->fifo) {
111  void *obj;
112  while (av_fifo_read(cf->fifo, &obj, 1) >= 0)
113  av_refstruct_unref(&obj);
114  av_fifo_freep2(&cf->fifo);
115  }
116 
118 
119  av_freep(pcf);
120 }
121 
122 int av_container_fifo_read(AVContainerFifo *cf, void *obj, unsigned flags)
123 {
124  void **psrc;
125  int ret;
126 
127  ret = av_fifo_read(cf->fifo, &psrc, 1);
128  if (ret < 0)
129  return ret;
130 
131  ret = cf->fifo_transfer(cf->opaque, obj, *psrc, flags);
132  av_refstruct_unref(&psrc);
133 
134  return ret;
135 }
136 
137 int av_container_fifo_peek(AVContainerFifo *cf, void **pdst, size_t offset)
138 {
139  void **pobj;
140  int ret;
141 
142  ret = av_fifo_peek(cf->fifo, &pobj, 1, offset);
143  if (ret < 0)
144  return ret;
145 
146  *pdst = *pobj;
147 
148  return 0;
149 }
150 
151 void av_container_fifo_drain(AVContainerFifo *cf, size_t nb_elems)
152 {
153  av_assert0(nb_elems <= av_fifo_can_read(cf->fifo));
154  while (nb_elems--) {
155  void **pobj;
156  int ret = av_fifo_read(cf->fifo, &pobj, 1);
157  av_assert0(ret >= 0);
158  av_refstruct_unref(&pobj);
159  }
160 }
161 
162 int av_container_fifo_write(AVContainerFifo *cf, void *obj, unsigned flags)
163 {
164  void **pdst;
165  int ret;
166 
167  pdst = av_refstruct_pool_get(cf->pool);
168  if (!pdst)
169  return AVERROR(ENOMEM);
170 
171  ret = cf->fifo_transfer(cf->opaque, *pdst, obj, flags);
172  if (ret < 0)
173  goto fail;
174 
175  ret = av_fifo_write(cf->fifo, &pdst, 1);
176  if (ret < 0)
177  goto fail;
178 
179  return 0;
180 fail:
181  av_refstruct_unref(&pdst);
182  return ret;
183 }
184 
186 {
187  return av_fifo_can_read(cf->fifo);
188 }
189 
190 static void *frame_alloc(void *opaque)
191 {
192  return av_frame_alloc();
193 }
194 
195 static void frame_reset(void *opaque, void *obj)
196 {
197  av_frame_unref(obj);
198 }
199 
200 static void frame_free(void *opaque, void *obj)
201 {
202  AVFrame *frame = obj;
204 }
205 
206 static int frame_transfer(void *opaque, void *dst, void *src, unsigned flags)
207 {
209  return av_frame_ref(dst, src);
210 
212  return 0;
213 }
214 
216 {
218  frame_transfer, 0);
219 }
AV_CONTAINER_FIFO_FLAG_REF
@ AV_CONTAINER_FIFO_FLAG_REF
Signal to av_container_fifo_write() that it should make a new reference to data in src rather than co...
Definition: container_fifo.h:39
av_container_fifo_write
int av_container_fifo_write(AVContainerFifo *cf, void *obj, unsigned flags)
Write the contents of obj to the FIFO.
Definition: container_fifo.c:162
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
av_container_fifo_alloc_avframe
AVContainerFifo * av_container_fifo_alloc_avframe(unsigned flags)
Allocate an AVContainerFifo instance for AVFrames.
Definition: container_fifo.c:215
AVRefStructOpaque
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition: refstruct.h:58
AVRefStructOpaque::nc
void * nc
Definition: refstruct.h:59
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
container_fifo.h
av_fifo_peek
int av_fifo_peek(const AVFifo *f, void *buf, size_t nb_elems, size_t offset)
Read data from a FIFO without modifying FIFO state.
Definition: fifo.c:255
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
container_fifo_init_entry
static int container_fifo_init_entry(AVRefStructOpaque opaque, void *obj)
Definition: container_fifo.c:39
fifo.h
fail
#define fail()
Definition: checkasm.h:193
av_fifo_write
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition: fifo.c:188
refstruct.h
AVContainerFifo::fifo
AVFifo * fifo
Definition: container_fifo.c:28
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
avassert.h
av_fifo_read
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
Definition: fifo.c:240
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
AVRefStructPool
AVRefStructPool is an API for a thread-safe pool of objects managed via the RefStruct API.
Definition: refstruct.c:183
av_container_fifo_read
int av_container_fifo_read(AVContainerFifo *cf, void *obj, unsigned flags)
Read the next available object from the FIFO into obj.
Definition: container_fifo.c:122
AVContainerFifo
AVContainerFifo is a FIFO for "containers" - dynamically allocated reusable structs (e....
Definition: container_fifo.c:27
AVContainerFifo::opaque
void * opaque
Definition: container_fifo.c:31
NULL
#define NULL
Definition: coverity.c:32
frame_reset
static void frame_reset(void *opaque, void *obj)
Definition: container_fifo.c:195
av_fifo_can_read
size_t av_fifo_can_read(const AVFifo *f)
Definition: fifo.c:87
av_refstruct_pool_alloc_ext
static AVRefStructPool * av_refstruct_pool_alloc_ext(size_t size, unsigned flags, void *opaque, int(*init_cb)(AVRefStructOpaque opaque, void *obj), void(*reset_cb)(AVRefStructOpaque opaque, void *obj), void(*free_entry_cb)(AVRefStructOpaque opaque, void *obj), void(*free_cb)(AVRefStructOpaque opaque))
A wrapper around av_refstruct_pool_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition: refstruct.h:258
av_refstruct_pool_get
void * av_refstruct_pool_get(AVRefStructPool *pool)
Get an object from the pool, reusing an old one from the pool when available.
Definition: refstruct.c:297
error.h
AVContainerFifo::container_alloc
void *(* container_alloc)(void *opaque)
Definition: container_fifo.c:32
AVFifo
Definition: fifo.c:35
av_frame_ref
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:400
AVContainerFifo::container_free
void(* container_free)(void *opaque, void *obj)
Definition: container_fifo.c:34
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
AVContainerFifo::fifo_transfer
int(* fifo_transfer)(void *opaque, void *dst, void *src, unsigned flags)
Definition: container_fifo.c:35
frame.h
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
frame_free
static void frame_free(void *opaque, void *obj)
Definition: container_fifo.c:200
frame_alloc
static void * frame_alloc(void *opaque)
Definition: container_fifo.c:190
av_refstruct_unref
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
AVContainerFifo::container_reset
void(* container_reset)(void *opaque, void *obj)
Definition: container_fifo.c:33
av_frame_move_ref
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:649
container_fifo_free_entry
static void container_fifo_free_entry(AVRefStructOpaque opaque, void *obj)
Definition: container_fifo.c:57
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:622
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
av_container_fifo_peek
int av_container_fifo_peek(AVContainerFifo *cf, void **pdst, size_t offset)
Access objects stored in the FIFO without retrieving them.
Definition: container_fifo.c:137
AVContainerFifo::pool
AVRefStructPool * pool
Definition: container_fifo.c:29
ret
ret
Definition: filter_design.txt:187
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
av_container_fifo_free
void av_container_fifo_free(AVContainerFifo **pcf)
Free a AVContainerFifo and everything in it.
Definition: container_fifo.c:101
av_fifo_alloc2
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
Definition: fifo.c:47
av_container_fifo_can_read
size_t av_container_fifo_can_read(const AVContainerFifo *cf)
Definition: container_fifo.c:185
mem.h
container_fifo_reset_entry
static void container_fifo_reset_entry(AVRefStructOpaque opaque, void *obj)
Definition: container_fifo.c:51
frame_transfer
static int frame_transfer(void *opaque, void *dst, void *src, unsigned flags)
Definition: container_fifo.c:206
av_refstruct_pool_uninit
static void av_refstruct_pool_uninit(AVRefStructPool **poolp)
Mark the pool as being available for freeing.
Definition: refstruct.h:292
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
av_fifo_freep2
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
Definition: fifo.c:286
src
#define src
Definition: vp8dsp.c:248
AV_FIFO_FLAG_AUTO_GROW
#define AV_FIFO_FLAG_AUTO_GROW
Automatically resize the FIFO on writes, so that the data fits.
Definition: fifo.h:63
av_container_fifo_drain
void av_container_fifo_drain(AVContainerFifo *cf, size_t nb_elems)
Discard the specified number of elements from the FIFO.
Definition: container_fifo.c:151
av_container_fifo_alloc
AVContainerFifo * av_container_fifo_alloc(void *opaque, void *(*container_alloc)(void *opaque), void(*container_reset)(void *opaque, void *obj), void(*container_free)(void *opaque, void *obj), int(*fifo_transfer)(void *opaque, void *dst, void *src, unsigned flags), unsigned flags)
Allocate a new AVContainerFifo for the container type defined by provided callbacks.
Definition: container_fifo.c:64