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 "libavutil/error.h"
20 #include "libavutil/fifo.h"
21 #include "libavutil/frame.h"
22 #include "libavutil/mem.h"
23 
24 #include "container_fifo.h"
25 #include "refstruct.h"
26 
27 struct ContainerFifo {
30 
31  void* (*container_alloc)(void);
32  void (*container_reset)(void *obj);
33  void (*container_free) (void *obj);
34  int (*fifo_write) (void *dst, void *src);
35  int (*fifo_read) (void *dst, void *src);
36 
37 };
38 
39 static int container_fifo_init_entry(FFRefStructOpaque opaque, void *obj)
40 {
41  ContainerFifo *cf = opaque.nc;
42  void **pobj = obj;
43 
44  *pobj = cf->container_alloc();
45  if (!*pobj)
46  return AVERROR(ENOMEM);
47 
48  return 0;
49 }
50 
51 static void container_fifo_reset_entry(FFRefStructOpaque opaque, void *obj)
52 {
53  ContainerFifo *cf = opaque.nc;
54  cf->container_reset(*(void**)obj);
55 }
56 
57 static void container_fifo_free_entry(FFRefStructOpaque opaque, void *obj)
58 {
59  ContainerFifo *cf = opaque.nc;
60  cf->container_free(*(void**)obj);
61 }
62 
64 ff_container_fifo_alloc(void* (*container_alloc)(void),
65  void (*container_reset)(void *obj),
66  void (*container_free) (void *obj),
67  int (*fifo_write) (void *dst, void *src),
68  int (*fifo_read) (void *dst, void *src))
69 {
70  ContainerFifo *cf;
71 
72  cf = av_mallocz(sizeof(*cf));
73  if (!cf)
74  return NULL;
75 
76  cf->container_alloc = container_alloc;
77  cf->container_reset = container_reset;
78  cf->container_free = container_free;
79  cf->fifo_write = fifo_write;
80  cf->fifo_read = fifo_read;
81 
82  cf->fifo = av_fifo_alloc2(1, sizeof(void*), AV_FIFO_FLAG_AUTO_GROW);
83  if (!cf->fifo)
84  goto fail;
85 
86  cf->pool = ff_refstruct_pool_alloc_ext(sizeof(void*), 0, cf,
90  NULL);
91  if (!cf->pool)
92  goto fail;
93 
94  return cf;
95 fail:
97  return NULL;
98 }
99 
101 {
102  ContainerFifo *cf;
103 
104  if (!*pcf)
105  return;
106 
107  cf = *pcf;
108 
109  if (cf->fifo) {
110  void *obj;
111  while (av_fifo_read(cf->fifo, &obj, 1) >= 0)
112  ff_refstruct_unref(&obj);
113  av_fifo_freep2(&cf->fifo);
114  }
115 
117 
118  av_freep(pcf);
119 }
120 
122 {
123  void **psrc;
124  int ret;
125 
126  ret = av_fifo_read(cf->fifo, &psrc, 1);
127  if (ret < 0)
128  return ret;
129 
130  ret = cf->fifo_read(obj, *psrc);
131  ff_refstruct_unref(&psrc);
132 
133  return ret;
134 }
135 
137 {
138  void **pdst;
139  int ret;
140 
141  pdst = ff_refstruct_pool_get(cf->pool);
142  if (!pdst)
143  return AVERROR(ENOMEM);
144 
145  ret = cf->fifo_write(*pdst, obj);
146  if (ret < 0)
147  goto fail;
148 
149  ret = av_fifo_write(cf->fifo, &pdst, 1);
150  if (ret < 0)
151  goto fail;
152 
153  return 0;
154 fail:
155  ff_refstruct_unref(&pdst);
156  return ret;
157 }
158 
160 {
161  return av_fifo_can_read(cf->fifo);
162 }
163 
164 static void *frame_alloc(void)
165 {
166  return av_frame_alloc();
167 }
168 
169 static void frame_reset(void *obj)
170 {
171  av_frame_unref(obj);
172 }
173 
174 static void frame_free(void *obj)
175 {
176  AVFrame *frame = obj;
178 }
179 
180 static int frame_ref(void *dst, void *src)
181 {
182  return av_frame_ref(dst, src);
183 }
184 
185 static int frame_move_ref(void *dst, void *src)
186 {
188  return 0;
189 }
190 
192 {
195 }
ContainerFifo::container_free
void(* container_free)(void *obj)
Definition: container_fifo.c:33
ff_container_fifo_write
int ff_container_fifo_write(ContainerFifo *cf, void *obj)
Write the contents of obj to the FIFO.
Definition: container_fifo.c:136
frame_reset
static void frame_reset(void *obj)
Definition: container_fifo.c:169
frame_free
static void frame_free(void *obj)
Definition: container_fifo.c:174
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
container_fifo_free_entry
static void container_fifo_free_entry(FFRefStructOpaque opaque, void *obj)
Definition: container_fifo.c:57
ff_container_fifo_alloc
ContainerFifo * ff_container_fifo_alloc(void *(*container_alloc)(void), void(*container_reset)(void *obj), void(*container_free)(void *obj), int(*fifo_write)(void *dst, void *src), int(*fifo_read)(void *dst, void *src))
Allocate a new ContainerFifo for the container type defined by provided callbacks.
Definition: container_fifo.c:64
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
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
FFRefStructOpaque
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition: refstruct.h:58
ff_container_fifo_read
int ff_container_fifo_read(ContainerFifo *cf, void *obj)
Read the next available object from the FIFO into obj.
Definition: container_fifo.c:121
fifo.h
ff_container_fifo_alloc_avframe
ContainerFifo * ff_container_fifo_alloc_avframe(unsigned flags)
Allocate a ContainerFifo instance for AVFrames.
Definition: container_fifo.c:191
fail
#define fail()
Definition: checkasm.h:188
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
ff_refstruct_pool_uninit
static void ff_refstruct_pool_uninit(FFRefStructPool **poolp)
Mark the pool as being available for freeing.
Definition: refstruct.h:292
container_fifo_init_entry
static int container_fifo_init_entry(FFRefStructOpaque opaque, void *obj)
Definition: container_fifo.c:39
refstruct.h
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
frame_move_ref
static int frame_move_ref(void *dst, void *src)
Definition: container_fifo.c:185
av_fifo_read
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
Definition: fifo.c:240
FFRefStructPool
FFRefStructPool is an API for a thread-safe pool of objects managed via the RefStruct API.
Definition: refstruct.c:183
NULL
#define NULL
Definition: coverity.c:32
frame_alloc
static void * frame_alloc(void)
Definition: container_fifo.c:164
av_fifo_can_read
size_t av_fifo_can_read(const AVFifo *f)
Definition: fifo.c:87
error.h
container_fifo_reset_entry
static void container_fifo_reset_entry(FFRefStructOpaque opaque, void *obj)
Definition: container_fifo.c:51
ff_container_fifo_free
void ff_container_fifo_free(ContainerFifo **pcf)
Free a ContainerFifo and everything in it.
Definition: container_fifo.c:100
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:388
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
ContainerFifo::fifo
AVFifo * fifo
Definition: container_fifo.c:28
ContainerFifo
ContainerFifo is a FIFO for "containers" - dynamically allocated reusable structs (e....
Definition: container_fifo.c:27
FFRefStructOpaque::nc
void * nc
Definition: refstruct.h:59
frame.h
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:637
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
ContainerFifo::fifo_write
int(* fifo_write)(void *dst, void *src)
Definition: container_fifo.c:34
ContainerFifo::fifo_read
int(* fifo_read)(void *dst, void *src)
Definition: container_fifo.c:35
frame_ref
static int frame_ref(void *dst, void *src)
Definition: container_fifo.c:180
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_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
ff_container_fifo_can_read
size_t ff_container_fifo_can_read(ContainerFifo *cf)
Definition: container_fifo.c:159
ff_refstruct_pool_alloc_ext
static FFRefStructPool * ff_refstruct_pool_alloc_ext(size_t size, unsigned flags, void *opaque, int(*init_cb)(FFRefStructOpaque opaque, void *obj), void(*reset_cb)(FFRefStructOpaque opaque, void *obj), void(*free_entry_cb)(FFRefStructOpaque opaque, void *obj), void(*free_cb)(FFRefStructOpaque opaque))
A wrapper around ff_refstruct_pool_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition: refstruct.h:258
mem.h
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
ContainerFifo::container_alloc
void *(* container_alloc)(void)
Definition: container_fifo.c:31
ContainerFifo::container_reset
void(* container_reset)(void *obj)
Definition: container_fifo.c:32
ff_refstruct_unref
void ff_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
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
ContainerFifo::pool
FFRefStructPool * pool
Definition: container_fifo.c:29
ff_refstruct_pool_get
void * ff_refstruct_pool_get(FFRefStructPool *pool)
Get an object from the pool, reusing an old one from the pool when available.
Definition: refstruct.c:297