FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/thread_queue.c
Date: 2026-10-19 01:23:10
Exec Total Coverage
Lines: 119 139 85.6%
Functions: 9 9 100.0%
Branches: 56 74 75.7%

Line Branch Exec Source
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"
23 #include "libavutil/container_fifo.h"
24 #include "libavutil/error.h"
25 #include "libavutil/fifo.h"
26 #include "libavutil/frame.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/thread.h"
30
31 #include "libavcodec/packet.h"
32
33 #include "thread_queue.h"
34
35 enum {
36 FINISHED_SEND = (1 << 0),
37 FINISHED_RECV = (1 << 1),
38 };
39
40 struct ThreadQueue {
41 int choked;
42 int *finished;
43 unsigned int nb_streams;
44 size_t queue_size;
45
46 enum ThreadQueueType type;
47
48 AVContainerFifo *fifo;
49 AVFifo *fifo_stream_index;
50 size_t *stream_count;
51
52 pthread_mutex_t lock;
53 pthread_cond_t cond_read;
54 pthread_cond_t *cond_write;
55 unsigned int nb_cond_write;
56 };
57
58 33062 void tq_free(ThreadQueue **ptq)
59 {
60 33062 ThreadQueue *tq = *ptq;
61
62
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 33054 times.
33062 if (!tq)
63 8 return;
64
65 33054 av_container_fifo_free(&tq->fifo);
66 33054 av_fifo_freep2(&tq->fifo_stream_index);
67 33054 av_freep(&tq->stream_count);
68
69 33054 av_freep(&tq->finished);
70
71
2/2
✓ Branch 0 taken 40927 times.
✓ Branch 1 taken 33054 times.
73981 for (unsigned i = 0; i < tq->nb_cond_write; i++)
72 40927 pthread_cond_destroy(&tq->cond_write[i]);
73 33054 av_freep(&tq->cond_write);
74
75 33054 pthread_cond_destroy(&tq->cond_read);
76 33054 pthread_mutex_destroy(&tq->lock);
77
78 33054 av_freep(ptq);
79 }
80
81 33054 ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
82 enum ThreadQueueType type)
83 {
84 ThreadQueue *tq;
85 int ret;
86
87 33054 tq = av_mallocz(sizeof(*tq));
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33054 times.
33054 if (!tq)
89 ✗ return NULL;
90
91 33054 ret = pthread_cond_init(&tq->cond_read, NULL);
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33054 times.
33054 if (ret) {
93 ✗ av_freep(&tq);
94 ✗ return NULL;
95 }
96
97 33054 ret = pthread_mutex_init(&tq->lock, NULL);
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33054 times.
33054 if (ret) {
99 ✗ pthread_cond_destroy(&tq->cond_read);
100 ✗ av_freep(&tq);
101 ✗ return NULL;
102 }
103
104 33054 tq->cond_write = av_calloc(nb_streams, sizeof(*tq->cond_write));
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33054 times.
33054 if (!tq->cond_write)
106 ✗ goto fail;
107
2/2
✓ Branch 0 taken 40927 times.
✓ Branch 1 taken 33054 times.
73981 for (tq->nb_cond_write = 0; tq->nb_cond_write < nb_streams; tq->nb_cond_write++) {
108 40927 ret = pthread_cond_init(&tq->cond_write[tq->nb_cond_write], NULL);
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40927 times.
40927 if (ret)
110 ✗ goto fail;
111 }
112
113 33054 tq->finished = av_calloc(nb_streams, sizeof(*tq->finished));
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33054 times.
33054 if (!tq->finished)
115 ✗ goto fail;
116 33054 tq->nb_streams = nb_streams;
117 33054 tq->queue_size = queue_size;
118 33054 tq->type = type;
119
120 33054 tq->fifo = (type == THREAD_QUEUE_FRAMES) ?
121
2/2
✓ Branch 0 taken 16949 times.
✓ Branch 1 taken 16105 times.
33054 av_container_fifo_alloc_avframe(0) : av_container_fifo_alloc_avpacket(0);
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33054 times.
33054 if (!tq->fifo)
123 ✗ goto fail;
124
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33054 times.
33054 av_assert0(queue_size);
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33054 times.
33054 if (nb_streams > SIZE_MAX / queue_size)
127 ✗ goto fail; // treat like OOM
128
129 33054 tq->fifo_stream_index = av_fifo_alloc2(queue_size * nb_streams, sizeof(unsigned), 0);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33054 times.
33054 if (!tq->fifo_stream_index)
131 ✗ goto fail;
132
133 33054 tq->stream_count = av_calloc(nb_streams, sizeof(*tq->stream_count));
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33054 times.
33054 if (!tq->stream_count)
135 ✗ goto fail;
136
137 33054 return tq;
138 ✗ fail:
139 ✗ tq_free(&tq);
140 ✗ return NULL;
141 }
142
143 2682087 static int can_write(ThreadQueue *tq, unsigned int stream_idx)
144 {
145 2682087 return tq->stream_count[stream_idx] < tq->queue_size;
146 }
147
148 2025171 int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data)
149 {
150 int *finished;
151 int ret;
152
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2025171 times.
2025171 av_assert0(stream_idx < tq->nb_streams);
154 2025171 finished = &tq->finished[stream_idx];
155
156 2025171 pthread_mutex_lock(&tq->lock);
157
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2025171 times.
2025171 if (*finished & FINISHED_SEND) {
159 ✗ ret = AVERROR(EINVAL);
160 ✗ goto finish;
161 }
162
163
4/4
✓ Branch 0 taken 2682087 times.
✓ Branch 1 taken 6963 times.
✓ Branch 3 taken 663879 times.
✓ Branch 4 taken 2018208 times.
2689050 while (!(*finished & FINISHED_RECV) && !can_write(tq, stream_idx))
164 663879 pthread_cond_wait(&tq->cond_write[stream_idx], &tq->lock);
165
166
2/2
✓ Branch 0 taken 6963 times.
✓ Branch 1 taken 2018208 times.
2025171 if (*finished & FINISHED_RECV) {
167 6963 ret = AVERROR_EOF;
168 6963 *finished |= FINISHED_SEND;
169 } else {
170 2018208 ret = av_fifo_write(tq->fifo_stream_index, &stream_idx, 1);
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2018208 times.
2018208 if (ret < 0)
172 ✗ goto finish;
173
174 2018208 ret = av_container_fifo_write(tq->fifo, data, 0);
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2018208 times.
2018208 if (ret < 0)
176 ✗ goto finish;
177
178 2018208 tq->stream_count[stream_idx]++;
179 2018208 pthread_cond_broadcast(&tq->cond_read); // signal downstream
180 }
181
182 2025171 finish:
183 2025171 pthread_mutex_unlock(&tq->lock);
184
185 2025171 return ret;
186 }
187
188 3188475 static int receive_locked(ThreadQueue *tq, int *stream_idx,
189 void *data)
190 {
191 3188475 unsigned int nb_finished = 0;
192
193
2/2
✓ Branch 0 taken 7626 times.
✓ Branch 1 taken 3180849 times.
3188475 if (tq->choked)
194 7626 return AVERROR(EAGAIN);
195
196
2/2
✓ Branch 1 taken 2004959 times.
✓ Branch 2 taken 1176546 times.
3181505 while (av_container_fifo_read(tq->fifo, data, 0) >= 0) {
197 unsigned idx;
198 int ret;
199
200 2004959 ret = av_fifo_read(tq->fifo_stream_index, &idx, 1);
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2004959 times.
2004959 av_assert0(ret >= 0);
202
203 // signal upstream if the fifo is no longer full
204
2/2
✓ Branch 0 taken 800585 times.
✓ Branch 1 taken 1204374 times.
2004959 if (tq->stream_count[idx]-- == tq->queue_size)
205 800585 pthread_cond_broadcast(&tq->cond_write[idx]);
206
207
2/2
✓ Branch 0 taken 656 times.
✓ Branch 1 taken 2004303 times.
2004959 if (tq->finished[idx] & FINISHED_RECV) {
208 656 (tq->type == THREAD_QUEUE_FRAMES) ?
209
2/2
✓ Branch 0 taken 549 times.
✓ Branch 1 taken 107 times.
656 av_frame_unref(data) : av_packet_unref(data);
210 656 continue;
211 }
212
213 2004303 *stream_idx = idx;
214 2004303 return 0;
215 }
216
217
2/2
✓ Branch 0 taken 1551538 times.
✓ Branch 1 taken 1154889 times.
2706427 for (unsigned int i = 0; i < tq->nb_streams; i++) {
218
2/2
✓ Branch 0 taken 1507195 times.
✓ Branch 1 taken 44343 times.
1551538 if (!tq->finished[i])
219 1507195 continue;
220
221 /* return EOF to the consumer at most once for each stream */
222
2/2
✓ Branch 0 taken 21657 times.
✓ Branch 1 taken 22686 times.
44343 if (!(tq->finished[i] & FINISHED_RECV)) {
223 21657 tq->finished[i] |= FINISHED_RECV;
224 21657 *stream_idx = i;
225 21657 return AVERROR_EOF;
226 }
227
228 22686 nb_finished++;
229 }
230
231
2/2
✓ Branch 0 taken 9242 times.
✓ Branch 1 taken 1145647 times.
1154889 return nb_finished == tq->nb_streams ? AVERROR_EOF : AVERROR(EAGAIN);
232 }
233
234 2067948 int tq_receive(ThreadQueue *tq, int *stream_idx, void *data, int flags)
235 {
236 int ret;
237
238 2067948 *stream_idx = -1;
239
240 2067948 pthread_mutex_lock(&tq->lock);
241
242 while (1) {
243 3188475 ret = receive_locked(tq, stream_idx, data);
244
245
4/4
✓ Branch 0 taken 1153273 times.
✓ Branch 1 taken 2035202 times.
✓ Branch 2 taken 1120527 times.
✓ Branch 3 taken 32746 times.
3188475 if (ret == AVERROR(EAGAIN) && !(flags & THREAD_QUEUE_FLAG_NO_BLOCK)) {
246 1120527 pthread_cond_wait(&tq->cond_read, &tq->lock);
247 1120527 continue;
248 }
249
250 2067948 break;
251 }
252
253 2067948 pthread_mutex_unlock(&tq->lock);
254
255 2067948 return ret;
256 }
257
258 61861 void tq_send_finish(ThreadQueue *tq, unsigned int stream_idx)
259 {
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61861 times.
61861 av_assert0(stream_idx < tq->nb_streams);
261
262 61861 pthread_mutex_lock(&tq->lock);
263
264 /* mark the stream as send-finished;
265 * next time the consumer thread tries to read this stream it will get
266 * an EOF and recv-finished flag will be set */
267 61861 tq->finished[stream_idx] |= FINISHED_SEND;
268 61861 tq->choked = 0;
269 61861 pthread_cond_broadcast(&tq->cond_read);
270
271 61861 pthread_mutex_unlock(&tq->lock);
272 61861 }
273
274 41943 void tq_receive_finish(ThreadQueue *tq, unsigned int stream_idx)
275 {
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41943 times.
41943 av_assert0(stream_idx < tq->nb_streams);
277
278 41943 pthread_mutex_lock(&tq->lock);
279
280 /* mark the stream as recv-finished;
281 * next time the producer thread tries to send for this stream, it will
282 * get an EOF and send-finished flag will be set */
283 41943 tq->finished[stream_idx] |= FINISHED_RECV;
284 41943 pthread_cond_broadcast(&tq->cond_write[stream_idx]);
285
286 41943 pthread_mutex_unlock(&tq->lock);
287 41943 }
288
289 27666 void tq_choke(ThreadQueue *tq, int choked)
290 {
291 27666 pthread_mutex_lock(&tq->lock);
292
293 27666 int prev_choked = tq->choked;
294 27666 tq->choked = choked;
295
2/2
✓ Branch 0 taken 24676 times.
✓ Branch 1 taken 2990 times.
27666 if (choked != prev_choked)
296 24676 pthread_cond_broadcast(&tq->cond_read);
297
298 27666 pthread_mutex_unlock(&tq->lock);
299 27666 }
300