FFmpeg
Loading...
Searching...
No Matches
thread_queue.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#include <string.h>
21
22#include "libavutil/avassert.h"
24#include "libavutil/error.h"
25#include "libavutil/fifo.h"
26#include "libavutil/frame.h"
28#include "libavutil/mem.h"
29#include "libavutil/thread.h"
30
31#include "libavcodec/packet.h"
32
33#include "thread_queue.h"
34
35enum {
36 FINISHED_SEND = (1 << 0),
37 FINISHED_RECV = (1 << 1),
38};
39
53
55{
56 ThreadQueue *tq = *ptq;
57
58 if (!tq)
59 return;
60
63
64 av_freep(&tq->finished);
65
68
69 av_freep(ptq);
70}
71
72ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
74{
75 ThreadQueue *tq;
76 int ret;
77
78 tq = av_mallocz(sizeof(*tq));
79 if (!tq)
80 return NULL;
81
82 ret = pthread_cond_init(&tq->cond, NULL);
83 if (ret) {
84 av_freep(&tq);
85 return NULL;
86 }
87
88 ret = pthread_mutex_init(&tq->lock, NULL);
89 if (ret) {
91 av_freep(&tq);
92 return NULL;
93 }
94
95 tq->finished = av_calloc(nb_streams, sizeof(*tq->finished));
96 if (!tq->finished)
97 goto fail;
99
100 tq->type = type;
101
102 tq->fifo = (type == THREAD_QUEUE_FRAMES) ?
104 if (!tq->fifo)
105 goto fail;
106
107 tq->fifo_stream_index = av_fifo_alloc2(queue_size, sizeof(unsigned), 0);
108 if (!tq->fifo_stream_index)
109 goto fail;
110
111 return tq;
112fail:
113 tq_free(&tq);
114 return NULL;
115}
116
117int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data)
118{
119 int *finished;
120 int ret;
121
122 av_assert0(stream_idx < tq->nb_streams);
123 finished = &tq->finished[stream_idx];
124
126
127 if (*finished & FINISHED_SEND) {
128 ret = AVERROR(EINVAL);
129 goto finish;
130 }
131
132 while (!(*finished & FINISHED_RECV) && !av_fifo_can_write(tq->fifo_stream_index))
133 pthread_cond_wait(&tq->cond, &tq->lock);
134
135 if (*finished & FINISHED_RECV) {
136 ret = AVERROR_EOF;
137 *finished |= FINISHED_SEND;
138 } else {
139 ret = av_fifo_write(tq->fifo_stream_index, &stream_idx, 1);
140 if (ret < 0)
141 goto finish;
142
143 ret = av_container_fifo_write(tq->fifo, data, 0);
144 if (ret < 0)
145 goto finish;
146
148 }
149
150finish:
152
153 return ret;
154}
155
156static int receive_locked(ThreadQueue *tq, int *stream_idx,
157 void *data)
158{
159 unsigned int nb_finished = 0;
160
161 if (tq->choked)
162 return AVERROR(EAGAIN);
163
164 while (av_container_fifo_read(tq->fifo, data, 0) >= 0) {
165 unsigned idx;
166 int ret;
167
168 ret = av_fifo_read(tq->fifo_stream_index, &idx, 1);
169 av_assert0(ret >= 0);
170 if (tq->finished[idx] & FINISHED_RECV) {
171 (tq->type == THREAD_QUEUE_FRAMES) ?
173 continue;
174 }
175
176 *stream_idx = idx;
177 return 0;
178 }
179
180 for (unsigned int i = 0; i < tq->nb_streams; i++) {
181 if (!tq->finished[i])
182 continue;
183
184 /* return EOF to the consumer at most once for each stream */
185 if (!(tq->finished[i] & FINISHED_RECV)) {
186 tq->finished[i] |= FINISHED_RECV;
187 *stream_idx = i;
188 return AVERROR_EOF;
189 }
190
191 nb_finished++;
192 }
193
194 return nb_finished == tq->nb_streams ? AVERROR_EOF : AVERROR(EAGAIN);
195}
196
197int tq_receive(ThreadQueue *tq, int *stream_idx, void *data, int flags)
198{
199 int ret;
200
201 *stream_idx = -1;
202
204
205 while (1) {
206 size_t can_read = av_container_fifo_can_read(tq->fifo);
207
208 ret = receive_locked(tq, stream_idx, data);
209
210 // signal other threads if the fifo state changed
211 if (can_read != av_container_fifo_can_read(tq->fifo))
213
214 if (ret == AVERROR(EAGAIN) && !(flags & THREAD_QUEUE_FLAG_NO_BLOCK)) {
215 pthread_cond_wait(&tq->cond, &tq->lock);
216 continue;
217 }
218
219 break;
220 }
221
223
224 return ret;
225}
226
227void tq_send_finish(ThreadQueue *tq, unsigned int stream_idx)
228{
229 av_assert0(stream_idx < tq->nb_streams);
230
232
233 /* mark the stream as send-finished;
234 * next time the consumer thread tries to read this stream it will get
235 * an EOF and recv-finished flag will be set */
236 tq->finished[stream_idx] |= FINISHED_SEND;
237 tq->choked = 0;
239
241}
242
243void tq_receive_finish(ThreadQueue *tq, unsigned int stream_idx)
244{
245 av_assert0(stream_idx < tq->nb_streams);
246
248
249 /* mark the stream as recv-finished;
250 * next time the producer thread tries to send for this stream, it will
251 * get an EOF and send-finished flag will be set */
252 tq->finished[stream_idx] |= FINISHED_RECV;
254
256}
257
258void tq_choke(ThreadQueue *tq, int choked)
259{
261
262 int prev_choked = tq->choked;
263 tq->choked = choked;
264 if (choked != prev_choked)
266
268}
static void finish(void)
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
int av_container_fifo_write(AVContainerFifo *cf, void *obj, unsigned flags)
Write the contents of obj to the FIFO.
void av_container_fifo_free(AVContainerFifo **pcf)
Free a AVContainerFifo and everything in it.
int av_container_fifo_read(AVContainerFifo *cf, void *obj, unsigned flags)
Read the next available object from the FIFO into obj.
size_t av_container_fifo_can_read(const AVContainerFifo *cf)
AVContainerFifo * av_container_fifo_alloc_avframe(unsigned flags)
Allocate an AVContainerFifo instance for AVFrames.
#define NULL
Definition coverity.c:32
error code definitions
static unsigned int nb_streams
Definition ffprobe.c:352
A generic FIFO API.
reference-counted frame API
#define fail
Definition test.h:479
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
AVContainerFifo * av_container_fifo_alloc_avpacket(unsigned flags)
Allocate an AVContainerFifo instance for AVPacket.
Definition packet.c:695
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
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
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
Definition fifo.c:286
size_t av_fifo_can_write(const AVFifo *f)
Definition fifo.c:94
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition fifo.c:188
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
Definition fifo.c:240
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:496
cl_device_type type
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
const char data[16]
Definition mxf.c:149
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
Definition os2threads.h:168
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition os2threads.h:119
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition os2threads.h:150
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition os2threads.h:104
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition os2threads.h:139
_fmutex pthread_mutex_t
Definition os2threads.h:53
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition os2threads.h:132
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition os2threads.h:198
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition os2threads.h:112
AVContainerFifo is a FIFO for "containers" - dynamically allocated reusable structs (e....
Definition fifo.c:35
pthread_mutex_t lock
unsigned int nb_streams
enum ThreadQueueType type
pthread_cond_t cond
AVContainerFifo * fifo
AVFifo * fifo_stream_index
#define av_mallocz(s)
#define av_freep(p)
ThreadQueue * tq_alloc(unsigned int nb_streams, size_t queue_size, enum ThreadQueueType type)
Allocate a queue for sending data between threads.
void tq_send_finish(ThreadQueue *tq, unsigned int stream_idx)
Mark the given stream finished from the sending side.
int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data)
Send an item for the given stream to the queue.
static int receive_locked(ThreadQueue *tq, int *stream_idx, void *data)
void tq_choke(ThreadQueue *tq, int choked)
Prevent further reads from the thread queue until it is unchoked.
int tq_receive(ThreadQueue *tq, int *stream_idx, void *data, int flags)
Read the next item from the queue.
void tq_free(ThreadQueue **ptq)
@ FINISHED_SEND
@ FINISHED_RECV
void tq_receive_finish(ThreadQueue *tq, unsigned int stream_idx)
Mark the given stream finished from the receiving side.
ThreadQueueType
@ THREAD_QUEUE_FRAMES
@ THREAD_QUEUE_FLAG_NO_BLOCK