FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/sync_queue.c
Date: 2024-04-23 06:12:56
Exec Total Coverage
Lines: 309 329 93.9%
Functions: 21 21 100.0%
Branches: 223 266 83.8%

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/channel_layout.h"
24 #include "libavutil/cpu.h"
25 #include "libavutil/error.h"
26 #include "libavutil/fifo.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/samplefmt.h"
30 #include "libavutil/timestamp.h"
31
32 #include "objpool.h"
33 #include "sync_queue.h"
34
35 /*
36 * How this works:
37 * --------------
38 * time: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
39 * -------------------------------------------------------------------
40 * | | | | | | | | | | | | | |
41 * | ┌───┐┌────────┐┌───┐┌─────────────┐
42 * stream 0| │d=1││ d=2 ││d=1││ d=3 │
43 * | └───┘└────────┘└───┘└─────────────┘
44 * ┌───┐ ┌───────────────────────┐
45 * stream 1│d=1│ │ d=5 │
46 * └───┘ └───────────────────────┘
47 * | ┌───┐┌───┐┌───┐┌───┐
48 * stream 2| │d=1││d=1││d=1││d=1│ <- stream 2 is the head stream of the queue
49 * | └───┘└───┘└───┘└───┘
50 * ^ ^
51 * [stream 2 tail] [stream 2 head]
52 *
53 * We have N streams (N=3 in the diagram), each stream is a FIFO. The *tail* of
54 * each FIFO is the frame with smallest end time, the *head* is the frame with
55 * the largest end time. Frames submitted to the queue with sq_send() are placed
56 * after the head, frames returned to the caller with sq_receive() are taken
57 * from the tail.
58 *
59 * The head stream of the whole queue (SyncQueue.head_stream) is the limiting
60 * stream with the *smallest* head timestamp, i.e. the stream whose source lags
61 * furthest behind all other streams. It determines which frames can be output
62 * from the queue.
63 *
64 * In the diagram, the head stream is 2, because it head time is t=5, while
65 * streams 0 and 1 end at t=8 and t=9 respectively. All frames that _end_ at
66 * or before t=5 can be output, i.e. the first 3 frames from stream 0, first
67 * frame from stream 1, and all 4 frames from stream 2.
68 */
69
70 typedef struct SyncQueueStream {
71 AVFifo *fifo;
72 AVRational tb;
73
74 /* number of audio samples in fifo */
75 uint64_t samples_queued;
76 /* stream head: largest timestamp seen */
77 int64_t head_ts;
78 int limiting;
79 /* no more frames will be sent for this stream */
80 int finished;
81
82 uint64_t frames_sent;
83 uint64_t samples_sent;
84 uint64_t frames_max;
85 int frame_samples;
86 } SyncQueueStream;
87
88 struct SyncQueue {
89 enum SyncQueueType type;
90
91 void *logctx;
92
93 /* no more frames will be sent for any stream */
94 int finished;
95 /* sync head: the stream with the _smallest_ head timestamp
96 * this stream determines which frames can be output */
97 int head_stream;
98 /* the finished stream with the smallest finish timestamp or -1 */
99 int head_finished_stream;
100
101 // maximum buffering duration in microseconds
102 int64_t buf_size_us;
103
104 SyncQueueStream *streams;
105 unsigned int nb_streams;
106
107 // pool of preallocated frames to avoid constant allocations
108 ObjPool *pool;
109
110 int have_limiting;
111
112 uintptr_t align_mask;
113 };
114
115 48119 static void frame_move(const SyncQueue *sq, SyncQueueFrame dst,
116 SyncQueueFrame src)
117 {
118
2/2
✓ Branch 0 taken 1538 times.
✓ Branch 1 taken 46581 times.
48119 if (sq->type == SYNC_QUEUE_PACKETS)
119 1538 av_packet_move_ref(dst.p, src.p);
120 else
121 46581 av_frame_move_ref(dst.f, src.f);
122 48119 }
123
124 /**
125 * Compute the end timestamp of a frame. If nb_samples is provided, consider
126 * the frame to have this number of audio samples, otherwise use frame duration.
127 */
128 213325 static int64_t frame_end(const SyncQueue *sq, SyncQueueFrame frame, int nb_samples)
129 {
130
2/2
✓ Branch 0 taken 70636 times.
✓ Branch 1 taken 142689 times.
213325 if (nb_samples) {
131 70636 int64_t d = av_rescale_q(nb_samples, (AVRational){ 1, frame.f->sample_rate},
132 70636 frame.f->time_base);
133 70636 return frame.f->pts + d;
134 }
135
136 142689 return (sq->type == SYNC_QUEUE_PACKETS) ?
137
2/2
✓ Branch 0 taken 4066 times.
✓ Branch 1 taken 138623 times.
281312 frame.p->pts + frame.p->duration :
138 138623 frame.f->pts + frame.f->duration;
139 }
140
141 64463 static int frame_samples(const SyncQueue *sq, SyncQueueFrame frame)
142 {
143
2/2
✓ Branch 0 taken 62180 times.
✓ Branch 1 taken 2283 times.
64463 return (sq->type == SYNC_QUEUE_PACKETS) ? 0 : frame.f->nb_samples;
144 }
145
146 34793 static int frame_null(const SyncQueue *sq, SyncQueueFrame frame)
147 {
148
2/2
✓ Branch 0 taken 819 times.
✓ Branch 1 taken 33974 times.
34793 return (sq->type == SYNC_QUEUE_PACKETS) ? (frame.p == NULL) : (frame.f == NULL);
149 }
150
151 31775 static void tb_update(const SyncQueue *sq, SyncQueueStream *st,
152 const SyncQueueFrame frame)
153 {
154 63550 AVRational tb = (sq->type == SYNC_QUEUE_PACKETS) ?
155
2/2
✓ Branch 0 taken 793 times.
✓ Branch 1 taken 30982 times.
31775 frame.p->time_base : frame.f->time_base;
156
157
2/4
✓ Branch 0 taken 31775 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31775 times.
31775 av_assert0(tb.num > 0 && tb.den > 0);
158
159
4/4
✓ Branch 0 taken 31659 times.
✓ Branch 1 taken 116 times.
✓ Branch 2 taken 28933 times.
✓ Branch 3 taken 2726 times.
31775 if (tb.num == st->tb.num && tb.den == st->tb.den)
160 28933 return;
161
162 // timebase should not change after the first frame
163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2842 times.
2842 av_assert0(!av_fifo_can_read(st->fifo));
164
165
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2841 times.
2842 if (st->head_ts != AV_NOPTS_VALUE)
166 1 st->head_ts = av_rescale_q(st->head_ts, st->tb, tb);
167
168 2842 st->tb = tb;
169 }
170
171 5674 static void finish_stream(SyncQueue *sq, unsigned int stream_idx)
172 {
173 5674 SyncQueueStream *st = &sq->streams[stream_idx];
174
175
2/2
✓ Branch 0 taken 2853 times.
✓ Branch 1 taken 2821 times.
5674 if (!st->finished)
176 2853 av_log(sq->logctx, AV_LOG_DEBUG,
177 "sq: finish %u; head ts %s\n", stream_idx,
178 2853 av_ts2timestr(st->head_ts, &st->tb));
179
180 5674 st->finished = 1;
181
182
4/4
✓ Branch 0 taken 5335 times.
✓ Branch 1 taken 339 times.
✓ Branch 2 taken 5321 times.
✓ Branch 3 taken 14 times.
5674 if (st->limiting && st->head_ts != AV_NOPTS_VALUE) {
183 /* check if this stream is the new finished head */
184
4/4
✓ Branch 0 taken 2659 times.
✓ Branch 1 taken 2662 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2658 times.
7980 if (sq->head_finished_stream < 0 ||
185 2659 av_compare_ts(st->head_ts, st->tb,
186 2659 sq->streams[sq->head_finished_stream].head_ts,
187 2659 sq->streams[sq->head_finished_stream].tb) < 0) {
188 2663 sq->head_finished_stream = stream_idx;
189 }
190
191 /* mark as finished all streams that should no longer receive new frames,
192 * due to them being ahead of some finished stream */
193 5321 st = &sq->streams[sq->head_finished_stream];
194
2/2
✓ Branch 0 taken 5337 times.
✓ Branch 1 taken 5321 times.
10658 for (unsigned int i = 0; i < sq->nb_streams; i++) {
195 5337 SyncQueueStream *st1 = &sq->streams[i];
196
6/6
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 5321 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 5 times.
5351 if (st != st1 && st1->head_ts != AV_NOPTS_VALUE &&
197 14 av_compare_ts(st->head_ts, st->tb, st1->head_ts, st1->tb) <= 0) {
198
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 if (!st1->finished)
199 3 av_log(sq->logctx, AV_LOG_DEBUG,
200 "sq: finish secondary %u; head ts %s\n", i,
201 3 av_ts2timestr(st1->head_ts, &st1->tb));
202
203 9 st1->finished = 1;
204 }
205 }
206 }
207
208 /* mark the whole queue as finished if all streams are finished */
209
2/2
✓ Branch 0 taken 6056 times.
✓ Branch 1 taken 5604 times.
11660 for (unsigned int i = 0; i < sq->nb_streams; i++) {
210
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 5986 times.
6056 if (!sq->streams[i].finished)
211 70 return;
212 }
213 5604 sq->finished = 1;
214
215 5604 av_log(sq->logctx, AV_LOG_DEBUG, "sq: finish queue\n");
216 }
217
218 11504 static void queue_head_update(SyncQueue *sq)
219 {
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11504 times.
11504 av_assert0(sq->have_limiting);
221
222
2/2
✓ Branch 0 taken 2763 times.
✓ Branch 1 taken 8741 times.
11504 if (sq->head_stream < 0) {
223 2763 unsigned first_limiting = UINT_MAX;
224
225 /* wait for one timestamp in each stream before determining
226 * the queue head */
227
2/2
✓ Branch 0 taken 2872 times.
✓ Branch 1 taken 2662 times.
5534 for (unsigned int i = 0; i < sq->nb_streams; i++) {
228 2872 SyncQueueStream *st = &sq->streams[i];
229
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2868 times.
2872 if (!st->limiting)
230 4 continue;
231
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 2767 times.
2868 if (st->head_ts == AV_NOPTS_VALUE)
232 101 return;
233
2/2
✓ Branch 0 taken 2763 times.
✓ Branch 1 taken 4 times.
2767 if (first_limiting == UINT_MAX)
234 2763 first_limiting = i;
235 }
236
237 // placeholder value, correct one will be found below
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2662 times.
2662 av_assert0(first_limiting < UINT_MAX);
239 2662 sq->head_stream = first_limiting;
240 }
241
242
2/2
✓ Branch 0 taken 11779 times.
✓ Branch 1 taken 11403 times.
23182 for (unsigned int i = 0; i < sq->nb_streams; i++) {
243 11779 SyncQueueStream *st_head = &sq->streams[sq->head_stream];
244 11779 SyncQueueStream *st_other = &sq->streams[i];
245
5/6
✓ Branch 0 taken 11745 times.
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 11745 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 11738 times.
23524 if (st_other->limiting && st_other->head_ts != AV_NOPTS_VALUE &&
246 11745 av_compare_ts(st_other->head_ts, st_other->tb,
247 st_head->head_ts, st_head->tb) < 0)
248 7 sq->head_stream = i;
249 }
250 }
251
252 /* update this stream's head timestamp */
253 31869 static void stream_update_ts(SyncQueue *sq, unsigned int stream_idx, int64_t ts)
254 {
255 31869 SyncQueueStream *st = &sq->streams[stream_idx];
256
257
2/2
✓ Branch 0 taken 31866 times.
✓ Branch 1 taken 3 times.
31869 if (ts == AV_NOPTS_VALUE ||
258
4/4
✓ Branch 0 taken 29027 times.
✓ Branch 1 taken 2839 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 29005 times.
31866 (st->head_ts != AV_NOPTS_VALUE && st->head_ts >= ts))
259 25 return;
260
261 31844 st->head_ts = ts;
262
263 /* if this stream is now ahead of some finished stream, then
264 * this stream is also finished */
265
4/4
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 31710 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 130 times.
31978 if (sq->head_finished_stream >= 0 &&
266 134 av_compare_ts(sq->streams[sq->head_finished_stream].head_ts,
267 134 sq->streams[sq->head_finished_stream].tb,
268 ts, st->tb) <= 0)
269 4 finish_stream(sq, stream_idx);
270
271 /* update the overall head timestamp if it could have changed */
272
2/2
✓ Branch 0 taken 11953 times.
✓ Branch 1 taken 19891 times.
31844 if (st->limiting &&
273
4/4
✓ Branch 0 taken 9190 times.
✓ Branch 1 taken 2763 times.
✓ Branch 2 taken 8741 times.
✓ Branch 3 taken 449 times.
11953 (sq->head_stream < 0 || sq->head_stream == stream_idx))
274 11504 queue_head_update(sq);
275 }
276
277 /* If the queue for the given stream (or all streams when stream_idx=-1)
278 * is overflowing, trigger a fake heartbeat on lagging streams.
279 *
280 * @return 1 if heartbeat triggered, 0 otherwise
281 */
282 29281 static int overflow_heartbeat(SyncQueue *sq, int stream_idx)
283 {
284 SyncQueueStream *st;
285 SyncQueueFrame frame;
286 29281 int64_t tail_ts = AV_NOPTS_VALUE;
287
288 /* if no stream specified, pick the one that is most ahead */
289
1/2
✓ Branch 0 taken 29281 times.
✗ Branch 1 not taken.
29281 if (stream_idx < 0) {
290 29281 int64_t ts = AV_NOPTS_VALUE;
291
292
2/2
✓ Branch 0 taken 33079 times.
✓ Branch 1 taken 29281 times.
62360 for (int i = 0; i < sq->nb_streams; i++) {
293 33079 st = &sq->streams[i];
294
4/4
✓ Branch 0 taken 32606 times.
✓ Branch 1 taken 473 times.
✓ Branch 2 taken 3325 times.
✓ Branch 3 taken 29281 times.
33079 if (st->head_ts != AV_NOPTS_VALUE &&
295
2/2
✓ Branch 0 taken 527 times.
✓ Branch 1 taken 2798 times.
3325 (ts == AV_NOPTS_VALUE ||
296 3325 av_compare_ts(ts, sq->streams[stream_idx].tb,
297 st->head_ts, st->tb) < 0)) {
298 29808 ts = st->head_ts;
299 29808 stream_idx = i;
300 }
301 }
302 /* no stream has a timestamp yet -> nothing to do */
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29281 times.
29281 if (stream_idx < 0)
304 return 0;
305 }
306
307 29281 st = &sq->streams[stream_idx];
308
309 /* get the chosen stream's tail timestamp */
310
4/4
✓ Branch 0 taken 29281 times.
✓ Branch 1 taken 15209 times.
✓ Branch 2 taken 15209 times.
✓ Branch 3 taken 14072 times.
73771 for (size_t i = 0; tail_ts == AV_NOPTS_VALUE &&
311 44490 av_fifo_peek(st->fifo, &frame, 1, i) >= 0; i++)
312 15209 tail_ts = frame_end(sq, frame, 0);
313
314 /* overflow triggers when the tail is over specified duration behind the head */
315
4/4
✓ Branch 0 taken 15209 times.
✓ Branch 1 taken 14072 times.
✓ Branch 2 taken 1363 times.
✓ Branch 3 taken 13846 times.
29281 if (tail_ts == AV_NOPTS_VALUE || tail_ts >= st->head_ts ||
316
2/2
✓ Branch 0 taken 1269 times.
✓ Branch 1 taken 94 times.
1363 av_rescale_q(st->head_ts - tail_ts, st->tb, AV_TIME_BASE_Q) < sq->buf_size_us)
317 29187 return 0;
318
319 /* signal a fake timestamp for all streams that prevent tail_ts from being output */
320 94 tail_ts++;
321
2/2
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 94 times.
282 for (unsigned int i = 0; i < sq->nb_streams; i++) {
322 188 const SyncQueueStream *st1 = &sq->streams[i];
323 int64_t ts;
324
325
3/4
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 94 times.
✓ Branch 2 taken 94 times.
✗ Branch 3 not taken.
188 if (st == st1 || st1->finished ||
326
3/4
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
187 (st1->head_ts != AV_NOPTS_VALUE &&
327 93 av_compare_ts(tail_ts, st->tb, st1->head_ts, st1->tb) <= 0))
328 94 continue;
329
330 94 ts = av_rescale_q(tail_ts, st->tb, st1->tb);
331
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1 times.
94 if (st1->head_ts != AV_NOPTS_VALUE)
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 ts = FFMAX(st1->head_ts + 1, ts);
333
334 94 av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u overflow heardbeat %s -> %s\n",
335 94 i, av_ts2timestr(st1->head_ts, &st1->tb), av_ts2timestr(ts, &st1->tb));
336
337 94 stream_update_ts(sq, i, ts);
338 }
339
340 94 return 1;
341 }
342
343 34793 int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
344 {
345 SyncQueueStream *st;
346 SyncQueueFrame dst;
347 int64_t ts;
348 int ret, nb_samples;
349
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34793 times.
34793 av_assert0(stream_idx < sq->nb_streams);
351 34793 st = &sq->streams[stream_idx];
352
353
2/2
✓ Branch 1 taken 3002 times.
✓ Branch 2 taken 31791 times.
34793 if (frame_null(sq, frame)) {
354 3002 av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u EOF\n", stream_idx);
355 3002 finish_stream(sq, stream_idx);
356 3002 return 0;
357 }
358
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 31775 times.
31791 if (st->finished)
359 16 return AVERROR_EOF;
360
361 31775 tb_update(sq, st, frame);
362
363 31775 ret = objpool_get(sq->pool, (void**)&dst);
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31775 times.
31775 if (ret < 0)
365 return ret;
366
367 31775 frame_move(sq, dst, frame);
368
369 31775 nb_samples = frame_samples(sq, dst);
370 // make sure frame duration is consistent with sample count
371
2/2
✓ Branch 0 taken 23874 times.
✓ Branch 1 taken 7901 times.
31775 if (nb_samples) {
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23874 times.
23874 av_assert0(dst.f->sample_rate > 0);
373 23874 dst.f->duration = av_rescale_q(nb_samples, (AVRational){ 1, dst.f->sample_rate },
374 23874 dst.f->time_base);
375 }
376
377 31775 ts = frame_end(sq, dst, 0);
378
379 31775 av_log(sq->logctx, AV_LOG_DEBUG, "sq: send %u ts %s\n", stream_idx,
380 31775 av_ts2timestr(ts, &st->tb));
381
382 31775 ret = av_fifo_write(st->fifo, &dst, 1);
383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31775 times.
31775 if (ret < 0) {
384 frame_move(sq, frame, dst);
385 objpool_release(sq->pool, (void**)&dst);
386 return ret;
387 }
388
389 31775 stream_update_ts(sq, stream_idx, ts);
390
391 31775 st->samples_queued += nb_samples;
392 31775 st->samples_sent += nb_samples;
393
394
2/2
✓ Branch 0 taken 19776 times.
✓ Branch 1 taken 11999 times.
31775 if (st->frame_samples)
395 19776 st->frames_sent = st->samples_sent / st->frame_samples;
396 else
397 11999 st->frames_sent++;
398
399
2/2
✓ Branch 0 taken 2654 times.
✓ Branch 1 taken 29121 times.
31775 if (st->frames_sent >= st->frames_max) {
400 2654 av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u frames_max %"PRIu64" reached\n",
401 stream_idx, st->frames_max);
402
403 2654 finish_stream(sq, stream_idx);
404 }
405
406 31775 return 0;
407 }
408
409 64967 static void offset_audio(AVFrame *f, int nb_samples)
410 {
411 64967 const int planar = av_sample_fmt_is_planar(f->format);
412
2/2
✓ Branch 0 taken 29371 times.
✓ Branch 1 taken 35596 times.
64967 const int planes = planar ? f->ch_layout.nb_channels : 1;
413 64967 const int bps = av_get_bytes_per_sample(f->format);
414
2/2
✓ Branch 0 taken 35596 times.
✓ Branch 1 taken 29371 times.
64967 const int offset = nb_samples * bps * (planar ? 1 : f->ch_layout.nb_channels);
415
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64967 times.
64967 av_assert0(bps > 0);
417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64967 times.
64967 av_assert0(nb_samples < f->nb_samples);
418
419
2/2
✓ Branch 0 taken 93753 times.
✓ Branch 1 taken 64967 times.
158720 for (int i = 0; i < planes; i++) {
420 93753 f->extended_data[i] += offset;
421
1/2
✓ Branch 0 taken 93753 times.
✗ Branch 1 not taken.
93753 if (i < FF_ARRAY_ELEMS(f->data))
422 93753 f->data[i] = f->extended_data[i];
423 }
424 64967 f->linesize[0] -= offset;
425 64967 f->nb_samples -= nb_samples;
426 64967 f->duration = av_rescale_q(f->nb_samples, (AVRational){ 1, f->sample_rate },
427 f->time_base);
428 64967 f->pts += av_rescale_q(nb_samples, (AVRational){ 1, f->sample_rate },
429 f->time_base);
430 64967 }
431
432 56982 static int frame_is_aligned(const SyncQueue *sq, const AVFrame *frame)
433 {
434 // only checks linesize[0], so only works for audio
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56982 times.
56982 av_assert0(frame->nb_samples > 0);
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56982 times.
56982 av_assert0(sq->align_mask);
437
438 // only check data[0], because we always offset all data pointers
439 // by the same offset, so if one is aligned, all are
440
2/2
✓ Branch 0 taken 56434 times.
✓ Branch 1 taken 548 times.
56982 if (!((uintptr_t)frame->data[0] & sq->align_mask) &&
441
1/2
✓ Branch 0 taken 56434 times.
✗ Branch 1 not taken.
56434 !(frame->linesize[0] & sq->align_mask) &&
442
1/2
✓ Branch 0 taken 56434 times.
✗ Branch 1 not taken.
56434 frame->linesize[0] > sq->align_mask)
443 56434 return 1;
444
445 548 return 0;
446 }
447
448 66240 static int receive_samples(SyncQueue *sq, SyncQueueStream *st,
449 AVFrame *dst, int nb_samples)
450 {
451 SyncQueueFrame src;
452 int ret;
453
454
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66240 times.
66240 av_assert0(st->samples_queued >= nb_samples);
455
456 66240 ret = av_fifo_peek(st->fifo, &src, 1, 0);
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66240 times.
66240 av_assert0(ret >= 0);
458
459 // peeked frame has enough samples and its data is aligned
460 // -> we can just make a reference and limit its sample count
461
4/4
✓ Branch 0 taken 52588 times.
✓ Branch 1 taken 13652 times.
✓ Branch 3 taken 52041 times.
✓ Branch 4 taken 547 times.
66240 if (src.f->nb_samples > nb_samples && frame_is_aligned(sq, src.f)) {
462 52041 ret = av_frame_ref(dst, src.f);
463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52041 times.
52041 if (ret < 0)
464 return ret;
465
466 52041 dst->nb_samples = nb_samples;
467 52041 offset_audio(src.f, nb_samples);
468 52041 st->samples_queued -= nb_samples;
469
470 52041 goto finish;
471 }
472
473 // otherwise allocate a new frame and copy the data
474 14199 ret = av_channel_layout_copy(&dst->ch_layout, &src.f->ch_layout);
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14199 times.
14199 if (ret < 0)
476 return ret;
477
478 14199 dst->format = src.f->format;
479 14199 dst->nb_samples = nb_samples;
480
481 14199 ret = av_frame_get_buffer(dst, 0);
482
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14199 times.
14199 if (ret < 0)
483 goto fail;
484
485 14199 ret = av_frame_copy_props(dst, src.f);
486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14199 times.
14199 if (ret < 0)
487 goto fail;
488
489 14199 dst->nb_samples = 0;
490
2/2
✓ Branch 0 taken 28307 times.
✓ Branch 1 taken 14199 times.
42506 while (dst->nb_samples < nb_samples) {
491 int to_copy;
492
493 28307 ret = av_fifo_peek(st->fifo, &src, 1, 0);
494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28307 times.
28307 av_assert0(ret >= 0);
495
496 28307 to_copy = FFMIN(nb_samples - dst->nb_samples, src.f->nb_samples);
497
498 28307 av_samples_copy(dst->extended_data, src.f->extended_data, dst->nb_samples,
499 28307 0, to_copy, dst->ch_layout.nb_channels, dst->format);
500
501
2/2
✓ Branch 0 taken 12926 times.
✓ Branch 1 taken 15381 times.
28307 if (to_copy < src.f->nb_samples)
502 12926 offset_audio(src.f, to_copy);
503 else {
504 15381 av_frame_unref(src.f);
505 15381 objpool_release(sq->pool, (void**)&src);
506 15381 av_fifo_drain2(st->fifo, 1);
507 }
508 28307 st->samples_queued -= to_copy;
509
510 28307 dst->nb_samples += to_copy;
511 }
512
513 14199 finish:
514 66240 dst->duration = av_rescale_q(nb_samples, (AVRational){ 1, dst->sample_rate },
515 dst->time_base);
516
517 66240 return 0;
518
519 fail:
520 av_frame_unref(dst);
521 return ret;
522 }
523
524 123162 static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,
525 SyncQueueFrame frame)
526 {
527 246324 const SyncQueueStream *st_head = sq->head_stream >= 0 ?
528
2/2
✓ Branch 0 taken 27594 times.
✓ Branch 1 taken 95568 times.
123162 &sq->streams[sq->head_stream] : NULL;
529 SyncQueueStream *st;
530
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 123162 times.
123162 av_assert0(stream_idx < sq->nb_streams);
532 123162 st = &sq->streams[stream_idx];
533
534
2/2
✓ Branch 1 taken 100468 times.
✓ Branch 2 taken 22694 times.
123162 if (av_fifo_can_read(st->fifo) &&
535
4/4
✓ Branch 0 taken 16856 times.
✓ Branch 1 taken 83612 times.
✓ Branch 2 taken 145 times.
✓ Branch 3 taken 16711 times.
100468 (st->frame_samples <= st->samples_queued || st->finished)) {
536 83757 int nb_samples = st->frame_samples;
537 SyncQueueFrame peek;
538 int64_t ts;
539 83757 int cmp = 1;
540
541
2/2
✓ Branch 0 taken 3121 times.
✓ Branch 1 taken 80636 times.
83757 if (st->finished)
542 3121 nb_samples = FFMIN(nb_samples, st->samples_queued);
543
544 83757 av_fifo_peek(st->fifo, &peek, 1, 0);
545 83757 ts = frame_end(sq, peek, nb_samples);
546
547 /* check if this stream's tail timestamp does not overtake
548 * the overall queue head */
549
4/4
✓ Branch 0 taken 83754 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 12852 times.
✓ Branch 3 taken 70902 times.
83757 if (ts != AV_NOPTS_VALUE && st_head)
550 12852 cmp = av_compare_ts(ts, st->tb, st_head->head_ts, st_head->tb);
551
552 /* We can release frames that do not end after the queue head.
553 * Frames with no timestamps are just passed through with no conditions.
554 * Frames are also passed through when there are no limiting streams.
555 */
556
6/6
✓ Branch 0 taken 71973 times.
✓ Branch 1 taken 11784 times.
✓ Branch 2 taken 71970 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 70797 times.
✓ Branch 5 taken 1173 times.
83757 if (cmp <= 0 || ts == AV_NOPTS_VALUE || !sq->have_limiting) {
557
2/2
✓ Branch 0 taken 70633 times.
✓ Branch 1 taken 11951 times.
82584 if (nb_samples &&
558
4/4
✓ Branch 0 taken 4394 times.
✓ Branch 1 taken 66239 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 4393 times.
136873 (nb_samples != peek.f->nb_samples || !frame_is_aligned(sq, peek.f))) {
559 66240 int ret = receive_samples(sq, st, frame.f, nb_samples);
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66240 times.
66240 if (ret < 0)
561 return ret;
562 } else {
563 16344 frame_move(sq, frame, peek);
564 16344 objpool_release(sq->pool, (void**)&peek);
565 16344 av_fifo_drain2(st->fifo, 1);
566
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16344 times.
16344 av_assert0(st->samples_queued >= frame_samples(sq, frame));
567 16344 st->samples_queued -= frame_samples(sq, frame);
568 }
569
570
2/2
✓ Branch 0 taken 11784 times.
✓ Branch 1 taken 70800 times.
165168 av_log(sq->logctx, AV_LOG_DEBUG,
571 "sq: receive %u ts %s queue head %d ts %s\n", stream_idx,
572 82584 av_ts2timestr(frame_end(sq, frame, 0), &st->tb),
573 sq->head_stream,
574 11784 st_head ? av_ts2timestr(st_head->head_ts, &st_head->tb) : "N/A");
575
576 82584 return 0;
577 }
578 }
579
580
4/4
✓ Branch 0 taken 720 times.
✓ Branch 1 taken 33996 times.
✓ Branch 3 taken 493 times.
✓ Branch 4 taken 227 times.
34716 return (sq->finished || (st->finished && !av_fifo_can_read(st->fifo))) ?
581
2/2
✓ Branch 0 taken 34716 times.
✓ Branch 1 taken 5862 times.
75294 AVERROR_EOF : AVERROR(EAGAIN);
582 }
583
584 117455 static int receive_internal(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
585 {
586 117455 int nb_eof = 0;
587 int ret;
588
589 /* read a frame for a specific stream */
590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117455 times.
117455 if (stream_idx >= 0) {
591 ret = receive_for_stream(sq, stream_idx, frame);
592 return (ret < 0) ? ret : stream_idx;
593 }
594
595 /* read a frame for any stream with available output */
596
2/2
✓ Branch 0 taken 123162 times.
✓ Branch 1 taken 34871 times.
158033 for (unsigned int i = 0; i < sq->nb_streams; i++) {
597 123162 ret = receive_for_stream(sq, i, frame);
598
4/4
✓ Branch 0 taken 116807 times.
✓ Branch 1 taken 6355 times.
✓ Branch 2 taken 34223 times.
✓ Branch 3 taken 82584 times.
123162 if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) {
599 40578 nb_eof += (ret == AVERROR_EOF);
600 40578 continue;
601 }
602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82584 times.
82584 return (ret < 0) ? ret : i;
603 }
604
605
2/2
✓ Branch 0 taken 5590 times.
✓ Branch 1 taken 29281 times.
34871 return (nb_eof == sq->nb_streams) ? AVERROR_EOF : AVERROR(EAGAIN);
606 }
607
608 117361 int sq_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
609 {
610 117361 int ret = receive_internal(sq, stream_idx, frame);
611
612 /* try again if the queue overflowed and triggered a fake heartbeat
613 * for lagging streams */
614
4/4
✓ Branch 0 taken 29281 times.
✓ Branch 1 taken 88080 times.
✓ Branch 3 taken 94 times.
✓ Branch 4 taken 29187 times.
117361 if (ret == AVERROR(EAGAIN) && overflow_heartbeat(sq, stream_idx))
615 94 ret = receive_internal(sq, stream_idx, frame);
616
617 117361 return ret;
618 }
619
620 2856 int sq_add_stream(SyncQueue *sq, int limiting)
621 {
622 SyncQueueStream *tmp, *st;
623
624 2856 tmp = av_realloc_array(sq->streams, sq->nb_streams + 1, sizeof(*sq->streams));
625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2856 times.
2856 if (!tmp)
626 return AVERROR(ENOMEM);
627 2856 sq->streams = tmp;
628
629 2856 st = &sq->streams[sq->nb_streams];
630 2856 memset(st, 0, sizeof(*st));
631
632 2856 st->fifo = av_fifo_alloc2(1, sizeof(SyncQueueFrame), AV_FIFO_FLAG_AUTO_GROW);
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2856 times.
2856 if (!st->fifo)
634 return AVERROR(ENOMEM);
635
636 /* we set a valid default, so that a pathological stream that never
637 * receives even a real timebase (and no frames) won't stall all other
638 * streams forever; cf. overflow_heartbeat() */
639 2856 st->tb = (AVRational){ 1, 1 };
640 2856 st->head_ts = AV_NOPTS_VALUE;
641 2856 st->frames_max = UINT64_MAX;
642 2856 st->limiting = limiting;
643
644 2856 sq->have_limiting |= limiting;
645
646 2856 return sq->nb_streams++;
647 }
648
649 2670 void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, uint64_t frames)
650 {
651 SyncQueueStream *st;
652
653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2670 times.
2670 av_assert0(stream_idx < sq->nb_streams);
654 2670 st = &sq->streams[stream_idx];
655
656 2670 st->frames_max = frames;
657
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2656 times.
2670 if (st->frames_sent >= st->frames_max)
658 14 finish_stream(sq, stream_idx);
659 2670 }
660
661 156 void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx,
662 int frame_samples)
663 {
664 SyncQueueStream *st;
665
666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 av_assert0(sq->type == SYNC_QUEUE_FRAMES);
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 av_assert0(stream_idx < sq->nb_streams);
668 156 st = &sq->streams[stream_idx];
669
670 156 st->frame_samples = frame_samples;
671
672 156 sq->align_mask = av_cpu_max_align() - 1;
673 156 }
674
675 2789 SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us, void *logctx)
676 {
677 2789 SyncQueue *sq = av_mallocz(sizeof(*sq));
678
679
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2789 times.
2789 if (!sq)
680 return NULL;
681
682 2789 sq->type = type;
683 2789 sq->buf_size_us = buf_size_us;
684 2789 sq->logctx = logctx;
685
686 2789 sq->head_stream = -1;
687 2789 sq->head_finished_stream = -1;
688
689
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 2776 times.
2789 sq->pool = (type == SYNC_QUEUE_PACKETS) ? objpool_alloc_packets() :
690 2776 objpool_alloc_frames();
691
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2789 times.
2789 if (!sq->pool) {
692 av_freep(&sq);
693 return NULL;
694 }
695
696 2789 return sq;
697 }
698
699 9534 void sq_free(SyncQueue **psq)
700 {
701 9534 SyncQueue *sq = *psq;
702
703
2/2
✓ Branch 0 taken 6745 times.
✓ Branch 1 taken 2789 times.
9534 if (!sq)
704 6745 return;
705
706
2/2
✓ Branch 0 taken 2856 times.
✓ Branch 1 taken 2789 times.
5645 for (unsigned int i = 0; i < sq->nb_streams; i++) {
707 SyncQueueFrame frame;
708
2/2
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 2856 times.
2906 while (av_fifo_read(sq->streams[i].fifo, &frame, 1) >= 0)
709 50 objpool_release(sq->pool, (void**)&frame);
710
711 2856 av_fifo_freep2(&sq->streams[i].fifo);
712 }
713
714 2789 av_freep(&sq->streams);
715
716 2789 objpool_free(&sq->pool);
717
718 2789 av_freep(psq);
719 }
720