../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
318 / 332
95.8%
Branches:
251 / 294
85.4%
Functions:
21 / 21
100.0%

fftools/sync_queue.c
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/channel_layout.h"
25 #include "libavutil/cpu.h"
26 #include "libavutil/error.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/samplefmt.h"
30 #include "libavutil/timestamp.h"
31
32 #include "sync_queue.h"
33
34 /*
35 * How this works:
36 * --------------
37 * time: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
38 * -------------------------------------------------------------------
39 * | | | | | | | | | | | | | |
40 * | ┌───┐┌────────┐┌───┐┌─────────────┐
41 * stream 0| │d=1││ d=2 ││d=1││ d=3 │
42 * | └───┘└────────┘└───┘└─────────────┘
43 * ┌───┐ ┌───────────────────────┐
44 * stream 1│d=1│ │ d=5 │
45 * └───┘ └───────────────────────┘
46 * | ┌───┐┌───┐┌───┐┌───┐
47 * stream 2| │d=1││d=1││d=1││d=1│ <- stream 2 is the head stream of the queue
48 * | └───┘└───┘└───┘└───┘
49 * ^ ^
50 * [stream 2 tail] [stream 2 head]
51 *
52 * We have N streams (N=3 in the diagram), each stream is a FIFO. The *tail* of
53 * each FIFO is the frame with smallest end time, the *head* is the frame with
54 * the largest end time. Frames submitted to the queue with sq_send() are placed
55 * after the head, frames returned to the caller with sq_receive() are taken
56 * from the tail.
57 *
58 * The head stream of the whole queue (SyncQueue.head_stream) is the limiting
59 * stream with the *smallest* head timestamp, i.e. the stream whose source lags
60 * furthest behind all other streams. It determines which frames can be output
61 * from the queue.
62 *
63 * In the diagram, the head stream is 2, because it head time is t=5, while
64 * streams 0 and 1 end at t=8 and t=9 respectively. All frames that _end_ at
65 * or before t=5 can be output, i.e. the first 3 frames from stream 0, first
66 * frame from stream 1, and all 4 frames from stream 2.
67 */
68
69 #define SQPTR(sq, frame) ((sq->type == SYNC_QUEUE_FRAMES) ? \
70 (void*)frame.f : (void*)frame.p)
71
72 typedef struct SyncQueueStream {
73 AVContainerFifo *fifo;
74 AVRational tb;
75
76 /* number of audio samples in fifo */
77 uint64_t samples_queued;
78 /* stream head: largest timestamp seen */
79 int64_t head_ts;
80 int limiting;
81 /* no more frames will be sent for this stream */
82 int finished;
83
84 uint64_t frames_sent;
85 uint64_t samples_sent;
86 uint64_t frames_max;
87 int frame_samples;
88 /* sample rate for audio that can be cut by a frame queue, or zero */
89 int cut_sample_rate;
90 } SyncQueueStream;
91
92 struct SyncQueue {
93 enum SyncQueueType type;
94
95 void *logctx;
96
97 /* no more frames will be sent for any stream */
98 int finished;
99 /* sync head: the stream with the _smallest_ head timestamp
100 * this stream determines which frames can be output */
101 int head_stream;
102 /* the finished stream with the smallest finish timestamp or -1 */
103 int head_finished_stream;
104
105 // maximum buffering duration in microseconds
106 int64_t buf_size_us;
107
108 SyncQueueStream *streams;
109 unsigned int nb_streams;
110
111 int have_limiting;
112
113 uintptr_t align_mask;
114 };
115
116 /**
117 * Compute the end timestamp of a frame. If nb_samples is provided, consider
118 * the frame to have this number of audio samples, otherwise use frame duration.
119 */
120 820439 static int64_t frame_end(const SyncQueue *sq, SyncQueueFrame frame, int nb_samples)
121 {
122
2/2
✓ Branch 2 → 3 taken 230793 times.
✓ Branch 2 → 4 taken 589646 times.
820439 if (nb_samples) {
123 230793 int64_t d = av_rescale_q(nb_samples, (AVRational){ 1, frame.f->sample_rate},
124 230793 frame.f->time_base);
125 230793 return frame.f->pts + d;
126 }
127
128 589646 return (sq->type == SYNC_QUEUE_PACKETS) ?
129
2/2
✓ Branch 4 → 5 taken 8069 times.
✓ Branch 4 → 6 taken 581577 times.
589646 frame.p->pts + frame.p->duration :
130 581577 frame.f->pts + frame.f->duration;
131 }
132
133 473044 static int frame_samples(const SyncQueue *sq, SyncQueueFrame frame)
134 {
135
2/2
✓ Branch 2 → 3 taken 466793 times.
✓ Branch 2 → 4 taken 6251 times.
473044 return (sq->type == SYNC_QUEUE_PACKETS) ? 0 : frame.f->nb_samples;
136 }
137
138 424154 static int frame_null(const SyncQueue *sq, SyncQueueFrame frame)
139 {
140
2/2
✓ Branch 2 → 3 taken 2192 times.
✓ Branch 2 → 4 taken 421962 times.
424154 return (sq->type == SYNC_QUEUE_PACKETS) ? (frame.p == NULL) : (frame.f == NULL);
141 }
142
143 416567 static void tb_update(const SyncQueue *sq, SyncQueueStream *st,
144 const SyncQueueFrame frame)
145 {
146 833134 AVRational tb = (sq->type == SYNC_QUEUE_PACKETS) ?
147
2/2
✓ Branch 2 → 3 taken 2114 times.
✓ Branch 2 → 4 taken 414453 times.
416567 frame.p->time_base : frame.f->time_base;
148
149
2/4
✓ Branch 5 → 6 taken 416567 times.
✗ Branch 5 → 7 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 416567 times.
416567 av_assert0(tb.num > 0 && tb.den > 0);
150
151
4/4
✓ Branch 9 → 10 taken 416446 times.
✓ Branch 9 → 12 taken 121 times.
✓ Branch 10 → 11 taken 412920 times.
✓ Branch 10 → 12 taken 3526 times.
416567 if (tb.num == st->tb.num && tb.den == st->tb.den)
152 412920 return;
153
154 // timebase should not change after the first frame
155
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 16 taken 3647 times.
3647 av_assert0(!av_container_fifo_can_read(st->fifo));
156
157
2/2
✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 18 taken 3646 times.
3647 if (st->head_ts != AV_NOPTS_VALUE)
158 1 st->head_ts = av_rescale_q(st->head_ts, st->tb, tb);
159
160 3647 st->tb = tb;
161 }
162
163 10840 static void finish_stream(SyncQueue *sq, unsigned int stream_idx)
164 {
165 10840 SyncQueueStream *st = &sq->streams[stream_idx];
166
167
2/2
✓ Branch 2 → 3 taken 3734 times.
✓ Branch 2 → 6 taken 7106 times.
10840 if (!st->finished)
168 3734 av_log(sq->logctx, AV_LOG_DEBUG,
169 "sq: finish %u; head ts %s\n", stream_idx,
170 3734 av_ts2timestr(st->head_ts, &st->tb));
171
172 10840 st->finished = 1;
173
174
4/4
✓ Branch 6 → 7 taken 9781 times.
✓ Branch 6 → 24 taken 1059 times.
✓ Branch 7 → 8 taken 9716 times.
✓ Branch 7 → 24 taken 65 times.
10840 if (st->limiting && st->head_ts != AV_NOPTS_VALUE) {
175 /* check if this stream is the new finished head */
176
4/4
✓ Branch 8 → 9 taken 6456 times.
✓ Branch 8 → 11 taken 3260 times.
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 12 taken 6455 times.
16172 if (sq->head_finished_stream < 0 ||
177 6456 av_compare_ts(st->head_ts, st->tb,
178 6456 sq->streams[sq->head_finished_stream].head_ts,
179 6456 sq->streams[sq->head_finished_stream].tb) < 0) {
180 3261 sq->head_finished_stream = stream_idx;
181 }
182
183 /* mark as finished all streams that should no longer receive new frames,
184 * due to them being ahead of some finished stream */
185 9716 st = &sq->streams[sq->head_finished_stream];
186
2/2
✓ Branch 23 → 13 taken 9772 times.
✓ Branch 23 → 24 taken 9716 times.
19488 for (unsigned int i = 0; i < sq->nb_streams; i++) {
187 9772 SyncQueueStream *st1 = &sq->streams[i];
188
6/6
✓ Branch 13 → 14 taken 56 times.
✓ Branch 13 → 22 taken 9716 times.
✓ Branch 14 → 15 taken 48 times.
✓ Branch 14 → 22 taken 8 times.
✓ Branch 16 → 17 taken 34 times.
✓ Branch 16 → 22 taken 14 times.
9820 if (st != st1 && st1->head_ts != AV_NOPTS_VALUE &&
189 48 av_compare_ts(st->head_ts, st->tb, st1->head_ts, st1->tb) <= 0) {
190
2/2
✓ Branch 17 → 18 taken 6 times.
✓ Branch 17 → 21 taken 28 times.
34 if (!st1->finished)
191 6 av_log(sq->logctx, AV_LOG_DEBUG,
192 "sq: finish secondary %u; head ts %s\n", i,
193 6 av_ts2timestr(st1->head_ts, &st1->tb));
194
195 34 st1->finished = 1;
196 }
197 }
198 }
199
200 /* mark the whole queue as finished if all streams are finished */
201
2/2
✓ Branch 28 → 25 taken 26927 times.
✓ Branch 28 → 29 taken 10632 times.
37559 for (unsigned int i = 0; i < sq->nb_streams; i++) {
202
2/2
✓ Branch 25 → 26 taken 208 times.
✓ Branch 25 → 27 taken 26719 times.
26927 if (!sq->streams[i].finished)
203 208 return;
204 }
205 10632 sq->finished = 1;
206
207 10632 av_log(sq->logctx, AV_LOG_DEBUG, "sq: finish queue\n");
208 }
209
210 315007 static void queue_head_update(SyncQueue *sq)
211 {
212
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 315007 times.
315007 av_assert0(sq->have_limiting);
213
214
2/2
✓ Branch 5 → 6 taken 3343 times.
✓ Branch 5 → 19 taken 311664 times.
315007 if (sq->head_stream < 0) {
215 3343 unsigned first_limiting = UINT_MAX;
216
217 /* wait for one timestamp in each stream before determining
218 * the queue head */
219
2/2
✓ Branch 14 → 7 taken 3409 times.
✓ Branch 14 → 15 taken 3261 times.
6670 for (unsigned int i = 0; i < sq->nb_streams; i++) {
220 3409 SyncQueueStream *st = &sq->streams[i];
221
2/2
✓ Branch 7 → 8 taken 13 times.
✓ Branch 7 → 9 taken 3396 times.
3409 if (!st->limiting)
222 13 continue;
223
2/2
✓ Branch 9 → 10 taken 82 times.
✓ Branch 9 → 11 taken 3314 times.
3396 if (st->head_ts == AV_NOPTS_VALUE)
224 82 return;
225
2/2
✓ Branch 11 → 12 taken 3308 times.
✓ Branch 11 → 13 taken 6 times.
3314 if (first_limiting == UINT_MAX)
226 3308 first_limiting = i;
227 }
228
229 // placeholder value, correct one will be found below
230
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 3261 times.
3261 av_assert0(first_limiting < UINT_MAX);
231 3261 sq->head_stream = first_limiting;
232 }
233
234
2/2
✓ Branch 26 → 20 taken 610029 times.
✓ Branch 26 → 27 taken 314925 times.
924954 for (unsigned int i = 0; i < sq->nb_streams; i++) {
235 610029 SyncQueueStream *st_head = &sq->streams[sq->head_stream];
236 610029 SyncQueueStream *st_other = &sq->streams[i];
237
5/6
✓ Branch 20 → 21 taken 609948 times.
✓ Branch 20 → 25 taken 81 times.
✓ Branch 21 → 22 taken 609948 times.
✗ Branch 21 → 25 not taken.
✓ Branch 23 → 24 taken 96 times.
✓ Branch 23 → 25 taken 609852 times.
1219977 if (st_other->limiting && st_other->head_ts != AV_NOPTS_VALUE &&
238 609948 av_compare_ts(st_other->head_ts, st_other->tb,
239 st_head->head_ts, st_head->tb) < 0)
240 96 sq->head_stream = i;
241 }
242 }
243
244 /* update this stream's head timestamp */
245 416705 static void stream_update_ts(SyncQueue *sq, unsigned int stream_idx, int64_t ts)
246 {
247 416705 SyncQueueStream *st = &sq->streams[stream_idx];
248
249
2/2
✓ Branch 2 → 3 taken 416702 times.
✓ Branch 2 → 5 taken 3 times.
416705 if (ts == AV_NOPTS_VALUE ||
250
4/4
✓ Branch 3 → 4 taken 413028 times.
✓ Branch 3 → 6 taken 3674 times.
✓ Branch 4 → 5 taken 8132 times.
✓ Branch 4 → 6 taken 404896 times.
416702 (st->head_ts != AV_NOPTS_VALUE && st->head_ts >= ts))
251 8135 return;
252
253 408570 st->head_ts = ts;
254
255 /* if this stream is now ahead of some finished stream, then
256 * this stream is also finished */
257
4/4
✓ Branch 6 → 7 taken 133 times.
✓ Branch 6 → 10 taken 408437 times.
✓ Branch 8 → 9 taken 12 times.
✓ Branch 8 → 10 taken 121 times.
408703 if (sq->head_finished_stream >= 0 &&
258 133 av_compare_ts(sq->streams[sq->head_finished_stream].head_ts,
259 133 sq->streams[sq->head_finished_stream].tb,
260 ts, st->tb) <= 0)
261 12 finish_stream(sq, stream_idx);
262
263 /* update the overall head timestamp if it could have changed */
264
2/2
✓ Branch 10 → 11 taken 372842 times.
✓ Branch 10 → 14 taken 35728 times.
408570 if (st->limiting &&
265
4/4
✓ Branch 11 → 12 taken 369499 times.
✓ Branch 11 → 13 taken 3343 times.
✓ Branch 12 → 13 taken 311664 times.
✓ Branch 12 → 14 taken 57835 times.
372842 (sq->head_stream < 0 || sq->head_stream == stream_idx))
266 315007 queue_head_update(sq);
267 }
268
269 /* If the queue for the given stream (or all streams when stream_idx=-1)
270 * is overflowing, trigger a fake heartbeat on lagging streams.
271 *
272 * @return 1 if heartbeat triggered, 0 otherwise
273 */
274 413651 static int overflow_heartbeat(SyncQueue *sq, int stream_idx)
275 {
276 SyncQueueStream *st;
277 SyncQueueFrame frame;
278 413651 int64_t tail_ts = AV_NOPTS_VALUE;
279
280 /* if no stream specified, pick the one that is most ahead */
281
1/2
✓ Branch 2 → 3 taken 413651 times.
✗ Branch 2 → 13 not taken.
413651 if (stream_idx < 0) {
282 413651 int64_t ts = AV_NOPTS_VALUE;
283
284
2/2
✓ Branch 10 → 4 taken 1106672 times.
✓ Branch 10 → 11 taken 413651 times.
1520323 for (int i = 0; i < sq->nb_streams; i++) {
285 1106672 st = &sq->streams[i];
286
4/4
✓ Branch 4 → 5 taken 1101219 times.
✓ Branch 4 → 9 taken 5453 times.
✓ Branch 5 → 6 taken 687571 times.
✓ Branch 5 → 8 taken 413648 times.
1106672 if (st->head_ts != AV_NOPTS_VALUE &&
287
2/2
✓ Branch 7 → 8 taken 57885 times.
✓ Branch 7 → 9 taken 629686 times.
687571 (ts == AV_NOPTS_VALUE ||
288 687571 av_compare_ts(ts, sq->streams[stream_idx].tb,
289 st->head_ts, st->tb) < 0)) {
290 471533 ts = st->head_ts;
291 471533 stream_idx = i;
292 }
293 }
294 /* no stream has a timestamp yet -> nothing to do */
295
2/2
✓ Branch 11 → 12 taken 3 times.
✓ Branch 11 → 13 taken 413648 times.
413651 if (stream_idx < 0)
296 3 return 0;
297 }
298
299 413648 st = &sq->streams[stream_idx];
300
301 /* get the chosen stream's tail timestamp */
302
4/4
✓ Branch 19 → 20 taken 413648 times.
✓ Branch 19 → 22 taken 92129 times.
✓ Branch 21 → 14 taken 92129 times.
✓ Branch 21 → 22 taken 321519 times.
919425 for (size_t i = 0; tail_ts == AV_NOPTS_VALUE &&
303 505777 av_container_fifo_peek(st->fifo, (void**)&frame, i) >= 0; i++) {
304 92129 int nb_samples = 0;
305
306 /* Use the chunk end from receive_for_stream() only when the queued
307 * frame is shorter than the encoder frame size; otherwise keep the
308 * frame's own end timestamp. */
309
2/2
✓ Branch 14 → 15 taken 91303 times.
✓ Branch 14 → 17 taken 826 times.
92129 if (sq->type == SYNC_QUEUE_FRAMES &&
310
2/2
✓ Branch 15 → 16 taken 83186 times.
✓ Branch 15 → 17 taken 8117 times.
91303 frame.f->nb_samples < st->frame_samples)
311 83186 nb_samples = st->frame_samples;
312
313 92129 tail_ts = frame_end(sq, frame, nb_samples);
314 }
315
316 /* overflow triggers when the tail is over specified duration behind the head */
317
4/4
✓ Branch 22 → 23 taken 92129 times.
✓ Branch 22 → 25 taken 321519 times.
✓ Branch 23 → 24 taken 35293 times.
✓ Branch 23 → 25 taken 56836 times.
413648 if (tail_ts == AV_NOPTS_VALUE || tail_ts >= st->head_ts ||
318
2/2
✓ Branch 24 → 25 taken 35155 times.
✓ Branch 24 → 26 taken 138 times.
35293 av_rescale_q(st->head_ts - tail_ts, st->tb, AV_TIME_BASE_Q) < sq->buf_size_us)
319 413510 return 0;
320
321 /* signal a fake timestamp for all streams that prevent tail_ts from being output */
322 138 tail_ts++;
323
2/2
✓ Branch 44 → 27 taken 276 times.
✓ Branch 44 → 45 taken 138 times.
414 for (unsigned int i = 0; i < sq->nb_streams; i++) {
324 276 const SyncQueueStream *st1 = &sq->streams[i];
325 int64_t ts;
326
327
3/4
✓ Branch 27 → 28 taken 138 times.
✓ Branch 27 → 32 taken 138 times.
✓ Branch 28 → 29 taken 138 times.
✗ Branch 28 → 32 not taken.
276 if (st == st1 || st1->finished ||
328
3/4
✓ Branch 29 → 30 taken 137 times.
✓ Branch 29 → 33 taken 1 time.
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 33 taken 137 times.
275 (st1->head_ts != AV_NOPTS_VALUE &&
329 137 av_compare_ts(tail_ts, st->tb, st1->head_ts, st1->tb) <= 0))
330 138 continue;
331
332 138 ts = av_rescale_q(tail_ts, st->tb, st1->tb);
333
2/2
✓ Branch 33 → 34 taken 137 times.
✓ Branch 33 → 37 taken 1 time.
138 if (st1->head_ts != AV_NOPTS_VALUE)
334
2/2
✓ Branch 34 → 35 taken 43 times.
✓ Branch 34 → 36 taken 94 times.
137 ts = FFMAX(st1->head_ts + 1, ts);
335
336 138 av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u overflow heardbeat %s -> %s\n",
337 138 i, av_ts2timestr(st1->head_ts, &st1->tb), av_ts2timestr(ts, &st1->tb));
338
339 138 stream_update_ts(sq, i, ts);
340 }
341
342 138 return 1;
343 }
344
345 424154 int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
346 {
347 SyncQueueStream *st;
348 int64_t ts;
349 int ret, nb_samples;
350
351
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 424154 times.
424154 av_assert0(stream_idx < sq->nb_streams);
352 424154 st = &sq->streams[stream_idx];
353
354
2/2
✓ Branch 6 → 7 taken 7515 times.
✓ Branch 6 → 10 taken 416639 times.
424154 if (frame_null(sq, frame)) {
355 7515 av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u EOF\n", stream_idx);
356 7515 finish_stream(sq, stream_idx);
357 7515 return 0;
358 }
359
2/2
✓ Branch 10 → 11 taken 72 times.
✓ Branch 10 → 12 taken 416567 times.
416639 if (st->finished)
360 72 return AVERROR_EOF;
361
362 416567 tb_update(sq, st, frame);
363
364 416567 nb_samples = frame_samples(sq, frame);
365 // make sure frame duration is consistent with sample count
366
2/2
✓ Branch 14 → 15 taken 399723 times.
✓ Branch 14 → 19 taken 16844 times.
416567 if (nb_samples) {
367
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 399723 times.
399723 av_assert0(frame.f->sample_rate > 0);
368 399723 frame.f->duration = av_rescale_q(nb_samples, (AVRational){ 1, frame.f->sample_rate },
369 399723 frame.f->time_base);
370 }
371
372 416567 ts = frame_end(sq, frame, 0);
373
374 416567 av_log(sq->logctx, AV_LOG_DEBUG, "sq: send %u ts %s\n", stream_idx,
375 416567 av_ts2timestr(ts, &st->tb));
376
377
2/2
✓ Branch 22 → 23 taken 414453 times.
✓ Branch 22 → 24 taken 2114 times.
416567 ret = av_container_fifo_write(st->fifo, SQPTR(sq, frame), 0);
378
1/2
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 28 taken 416567 times.
416567 if (ret < 0)
379 ✗ return ret;
380
381 416567 stream_update_ts(sq, stream_idx, ts);
382
383 416567 st->samples_queued += nb_samples;
384 416567 st->samples_sent += nb_samples;
385
386
2/2
✓ Branch 29 → 30 taken 395600 times.
✓ Branch 29 → 31 taken 20967 times.
416567 if (st->frame_samples)
387 395600 st->frames_sent = st->samples_sent / st->frame_samples;
388 else
389 20967 st->frames_sent++;
390
391
2/2
✓ Branch 32 → 33 taken 3250 times.
✓ Branch 32 → 35 taken 413317 times.
416567 if (st->frames_sent >= st->frames_max) {
392 3250 av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u frames_max %"PRIu64" reached\n",
393 stream_idx, st->frames_max);
394
395 3250 finish_stream(sq, stream_idx);
396 }
397
398 416567 return 0;
399 }
400
401 102736 static void offset_audio(AVFrame *f, int nb_samples)
402 {
403 102736 const int planar = av_sample_fmt_is_planar(f->format);
404
2/2
✓ Branch 3 → 4 taken 23809 times.
✓ Branch 3 → 5 taken 78927 times.
102736 const int planes = planar ? f->ch_layout.nb_channels : 1;
405 102736 const int bps = av_get_bytes_per_sample(f->format);
406
2/2
✓ Branch 7 → 8 taken 78927 times.
✓ Branch 7 → 9 taken 23809 times.
102736 const int offset = nb_samples * bps * (planar ? 1 : f->ch_layout.nb_channels);
407
408
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 13 taken 102736 times.
102736 av_assert0(bps > 0);
409
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 16 taken 102736 times.
102736 av_assert0(nb_samples < f->nb_samples);
410
411
2/2
✓ Branch 20 → 17 taken 126274 times.
✓ Branch 20 → 21 taken 102736 times.
229010 for (int i = 0; i < planes; i++) {
412 126274 f->extended_data[i] += offset;
413
2/2
✓ Branch 17 → 18 taken 126058 times.
✓ Branch 17 → 19 taken 216 times.
126274 if (i < FF_ARRAY_ELEMS(f->data))
414 126058 f->data[i] = f->extended_data[i];
415 }
416 102736 f->linesize[0] -= offset;
417 102736 f->nb_samples -= nb_samples;
418 102736 f->duration = av_rescale_q(f->nb_samples, (AVRational){ 1, f->sample_rate },
419 f->time_base);
420 102736 f->pts += av_rescale_q(nb_samples, (AVRational){ 1, f->sample_rate },
421 f->time_base);
422 102736 }
423
424 87873 static int frame_is_aligned(const SyncQueue *sq, const AVFrame *frame)
425 {
426 // only checks linesize[0], so only works for audio
427
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 87873 times.
87873 av_assert0(frame->nb_samples > 0);
428
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 87873 times.
87873 av_assert0(sq->align_mask);
429
430 // only check data[0], because we always offset all data pointers
431 // by the same offset, so if one is aligned, all are
432
2/2
✓ Branch 8 → 9 taken 86994 times.
✓ Branch 8 → 12 taken 879 times.
87873 if (!((uintptr_t)frame->data[0] & sq->align_mask) &&
433
1/2
✓ Branch 9 → 10 taken 86994 times.
✗ Branch 9 → 12 not taken.
86994 !(frame->linesize[0] & sq->align_mask) &&
434
1/2
✓ Branch 10 → 11 taken 86994 times.
✗ Branch 10 → 12 not taken.
86994 frame->linesize[0] > sq->align_mask)
435 86994 return 1;
436
437 879 return 0;
438 }
439
440 105822 static int receive_samples(SyncQueue *sq, SyncQueueStream *st,
441 AVFrame *dst, int nb_samples)
442 {
443 SyncQueueFrame src;
444 int ret;
445
446
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 105822 times.
105822 av_assert0(st->samples_queued >= nb_samples);
447
448 105822 ret = av_container_fifo_peek(st->fifo, (void**)&src, 0);
449
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 105822 times.
105822 av_assert0(ret >= 0);
450
451 // peeked frame has enough samples and its data is aligned
452 // -> we can just make a reference and limit its sample count
453
4/4
✓ Branch 9 → 10 taken 80549 times.
✓ Branch 9 → 17 taken 25273 times.
✓ Branch 11 → 12 taken 79678 times.
✓ Branch 11 → 17 taken 871 times.
105822 if (src.f->nb_samples > nb_samples && frame_is_aligned(sq, src.f)) {
454 79678 ret = av_frame_ref(dst, src.f);
455
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 79678 times.
79678 if (ret < 0)
456 ✗ return ret;
457
458 79678 dst->nb_samples = nb_samples;
459 79678 offset_audio(src.f, nb_samples);
460 79678 st->samples_queued -= nb_samples;
461
462 79678 goto finish;
463 }
464
465 // otherwise allocate a new frame and copy the data
466 26144 ret = av_channel_layout_copy(&dst->ch_layout, &src.f->ch_layout);
467
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 26144 times.
26144 if (ret < 0)
468 ✗ return ret;
469
470 26144 dst->format = src.f->format;
471 26144 dst->nb_samples = nb_samples;
472
473 26144 ret = av_frame_get_buffer(dst, 0);
474
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 26144 times.
26144 if (ret < 0)
475 ✗ goto fail;
476
477 26144 ret = av_frame_copy_props(dst, src.f);
478
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 26144 times.
26144 if (ret < 0)
479 ✗ goto fail;
480
481 26144 dst->nb_samples = 0;
482
2/2
✓ Branch 36 → 27 taken 411340 times.
✓ Branch 36 → 37 taken 26144 times.
437484 while (dst->nb_samples < nb_samples) {
483 int to_copy;
484
485 411340 ret = av_container_fifo_peek(st->fifo, (void**)&src, 0);
486
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 31 taken 411340 times.
411340 av_assert0(ret >= 0);
487
488 411340 to_copy = FFMIN(nb_samples - dst->nb_samples, src.f->nb_samples);
489
490 411340 av_samples_copy(dst->extended_data, src.f->extended_data, dst->nb_samples,
491 411340 0, to_copy, dst->ch_layout.nb_channels, dst->format);
492
493
2/2
✓ Branch 32 → 33 taken 23058 times.
✓ Branch 32 → 34 taken 388282 times.
411340 if (to_copy < src.f->nb_samples)
494 23058 offset_audio(src.f, to_copy);
495 else
496 388282 av_container_fifo_drain(st->fifo, 1);
497
498 411340 st->samples_queued -= to_copy;
499
500 411340 dst->nb_samples += to_copy;
501 }
502
503 26144 finish:
504 105822 dst->duration = av_rescale_q(nb_samples, (AVRational){ 1, dst->sample_rate },
505 dst->time_base);
506
507 105822 return 0;
508
509 ✗ fail:
510 ✗ av_frame_unref(dst);
511 ✗ return ret;
512 }
513
514 1403917 static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,
515 SyncQueueFrame frame)
516 {
517 633335 const SyncQueueStream *st_head = sq->head_stream >= 0 ?
518
2/2
✓ Branch 2 → 3 taken 770582 times.
✓ Branch 2 → 4 taken 633335 times.
1403917 &sq->streams[sq->head_stream] : NULL;
519 SyncQueueStream *st;
520
521
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 1403917 times.
1403917 av_assert0(stream_idx < sq->nb_streams);
522 1403917 st = &sq->streams[stream_idx];
523
524
2/2
✓ Branch 9 → 10 taken 935537 times.
✓ Branch 9 → 67 taken 468380 times.
1403917 if (av_container_fifo_can_read(st->fifo) &&
525
4/4
✓ Branch 10 → 11 taken 758188 times.
✓ Branch 10 → 12 taken 177349 times.
✓ Branch 11 → 12 taken 344 times.
✓ Branch 11 → 67 taken 757844 times.
935537 (st->frame_samples <= st->samples_queued || st->finished)) {
526 177693 int nb_samples = st->frame_samples;
527 SyncQueueFrame peek;
528 177693 int64_t ts, end = 0, cut = 0;
529 177693 int cmp = 1;
530
531
2/2
✓ Branch 12 → 13 taken 3948 times.
✓ Branch 12 → 14 taken 173745 times.
177693 if (st->finished)
532 3948 nb_samples = FFMIN(nb_samples, st->samples_queued);
533
534 177693 av_container_fifo_peek(st->fifo, (void**)&peek, 0);
535 177693 ts = frame_end(sq, peek, nb_samples);
536
537 /* check if this stream's tail timestamp does not overtake
538 * the overall queue head */
539
4/4
✓ Branch 16 → 17 taken 177690 times.
✓ Branch 16 → 32 taken 3 times.
✓ Branch 17 → 18 taken 64872 times.
✓ Branch 17 → 32 taken 112818 times.
177693 if (ts != AV_NOPTS_VALUE && st_head) {
540 64872 cmp = av_compare_ts(ts, st->tb, st_head->head_ts, st_head->tb);
541
542 /* A frame ending after the final queue head is cut there when
543 * the encoder takes any number of samples, so that the stream
544 * does not end up to a frame before the limiting one. */
545
8/8
✓ Branch 19 → 20 taken 43586 times.
✓ Branch 19 → 25 taken 21286 times.
✓ Branch 20 → 21 taken 24 times.
✓ Branch 20 → 25 taken 43562 times.
✓ Branch 21 → 22 taken 21 times.
✓ Branch 21 → 25 taken 3 times.
✓ Branch 23 → 24 taken 12 times.
✓ Branch 23 → 25 taken 9 times.
64893 if (cmp > 0 && st_head->finished && !st->frame_samples &&
546 21 frame_samples(sq, peek)) {
547 12 AVRational sample_tb = { 1, peek.f->sample_rate };
548
549 12 end = av_rescale_q(st_head->head_ts, st_head->tb, st->tb);
550 12 cut = av_rescale_q(st_head->head_ts, st_head->tb, sample_tb) -
551 12 av_rescale_q(peek.f->pts, st->tb, sample_tb);
552 12 cut = FFMIN(cut, peek.f->nb_samples);
553 }
554
555 /* Allow the nearest tick for cut audio if that tick is no longer
556 * than a sample. A coarse packet time base can otherwise admit a
557 * whole packet that the frame queue released without cutting. */
558
7/8
✓ Branch 25 → 26 taken 43586 times.
✓ Branch 25 → 31 taken 21286 times.
✓ Branch 26 → 27 taken 24 times.
✓ Branch 26 → 31 taken 43562 times.
✓ Branch 27 → 28 taken 1 time.
✓ Branch 27 → 31 taken 23 times.
✓ Branch 29 → 30 taken 1 time.
✗ Branch 29 → 31 not taken.
64873 if (cmp > 0 && st_head->finished && st->cut_sample_rate &&
559 1 av_cmp_q(st->tb, (AVRational){ 1, st->cut_sample_rate }) <= 0)
560 1 cmp = ts > av_rescale_q(st_head->head_ts, st_head->tb, st->tb);
561 }
562
563 /* We can release frames that do not end after the queue head.
564 * Frames with no timestamps are just passed through with no conditions.
565 * Frames are also passed through when there are no limiting streams.
566 */
567
8/8
✓ Branch 32 → 33 taken 156406 times.
✓ Branch 32 → 36 taken 21287 times.
✓ Branch 33 → 34 taken 156403 times.
✓ Branch 33 → 36 taken 3 times.
✓ Branch 34 → 35 taken 43650 times.
✓ Branch 34 → 36 taken 112753 times.
✓ Branch 35 → 36 taken 7 times.
✓ Branch 35 → 66 taken 43643 times.
177693 if (cmp <= 0 || ts == AV_NOPTS_VALUE || !sq->have_limiting || cut > 0) {
568
2/2
✓ Branch 36 → 37 taken 113138 times.
✓ Branch 36 → 44 taken 20912 times.
134050 if (nb_samples &&
569
4/4
✓ Branch 37 → 38 taken 7324 times.
✓ Branch 37 → 40 taken 105814 times.
✓ Branch 39 → 40 taken 8 times.
✓ Branch 39 → 44 taken 7316 times.
218960 (nb_samples != peek.f->nb_samples || !frame_is_aligned(sq, peek.f))) {
570 105822 int ret = receive_samples(sq, st, frame.f, nb_samples);
571
1/2
✗ Branch 41 → 42 not taken.
✓ Branch 41 → 43 taken 105822 times.
105822 if (ret < 0)
572 ✗ return ret;
573 } else {
574
2/2
✓ Branch 44 → 45 taken 26164 times.
✓ Branch 44 → 46 taken 2064 times.
28228 int ret = av_container_fifo_read(st->fifo, SQPTR(sq, frame), 0);
575
1/2
✗ Branch 48 → 49 not taken.
✓ Branch 48 → 51 taken 28228 times.
28228 av_assert0(ret >= 0);
576
577
1/2
✗ Branch 52 → 53 not taken.
✓ Branch 52 → 55 taken 28228 times.
28228 av_assert0(st->samples_queued >= frame_samples(sq, frame));
578 28228 st->samples_queued -= frame_samples(sq, frame);
579
580
2/2
✓ Branch 56 → 57 taken 7 times.
✓ Branch 56 → 58 taken 28221 times.
28228 if (cut > 0) {
581 7 frame.f->nb_samples = cut;
582 7 frame.f->duration = end - frame.f->pts;
583 }
584 }
585
586 246806 av_log(sq->logctx, AV_LOG_DEBUG,
587 "sq: receive %u ts %s queue head %d ts %s\n", stream_idx,
588 134050 av_ts2timestr(frame_end(sq, frame, 0), &st->tb),
589 sq->head_stream,
590
2/2
✓ Branch 58 → 59 taken 21294 times.
✓ Branch 58 → 60 taken 112756 times.
134050 st_head ? av_ts2timestr(st_head->head_ts, &st_head->tb) : "N/A");
591
592 134050 return 0;
593 }
594 }
595
596
4/4
✓ Branch 68 → 69 taken 10530 times.
✓ Branch 68 → 72 taken 1237967 times.
✓ Branch 70 → 71 taken 10328 times.
✓ Branch 70 → 72 taken 202 times.
1248497 return (sq->finished || (st->finished && !av_container_fifo_can_read(st->fifo))) ?
597
2/2
✓ Branch 67 → 68 taken 1248497 times.
✓ Branch 67 → 71 taken 21370 times.
2518364 AVERROR_EOF : AVERROR(EAGAIN);
598 }
599
600 558245 static int receive_internal(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
601 {
602 558245 int nb_eof = 0;
603 int ret;
604
605 /* read a frame for a specific stream */
606
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 8 taken 558245 times.
558245 if (stream_idx >= 0) {
607 ✗ ret = receive_for_stream(sq, stream_idx, frame);
608
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ return (ret < 0) ? ret : stream_idx;
609 }
610
611 /* read a frame for any stream with available output */
612
2/2
✓ Branch 18 → 9 taken 1403917 times.
✓ Branch 18 → 19 taken 424195 times.
1828112 for (unsigned int i = 0; i < sq->nb_streams; i++) {
613 1403917 ret = receive_for_stream(sq, i, frame);
614
4/4
✓ Branch 10 → 11 taken 1372219 times.
✓ Branch 10 → 12 taken 31698 times.
✓ Branch 11 → 12 taken 1238169 times.
✓ Branch 11 → 13 taken 134050 times.
1403917 if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) {
615 1269867 nb_eof += (ret == AVERROR_EOF);
616 1269867 continue;
617 }
618
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 134050 times.
134050 return (ret < 0) ? ret : i;
619 }
620
621
2/2
✓ Branch 19 → 20 taken 10544 times.
✓ Branch 19 → 21 taken 413651 times.
424195 return (nb_eof == sq->nb_streams) ? AVERROR_EOF : AVERROR(EAGAIN);
622 }
623
624 558107 int sq_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
625 {
626 558107 int ret = receive_internal(sq, stream_idx, frame);
627
628 /* try again if the queue overflowed and triggered a fake heartbeat
629 * for lagging streams */
630
4/4
✓ Branch 3 → 4 taken 413651 times.
✓ Branch 3 → 7 taken 144456 times.
✓ Branch 5 → 6 taken 138 times.
✓ Branch 5 → 7 taken 413513 times.
558107 if (ret == AVERROR(EAGAIN) && overflow_heartbeat(sq, stream_idx))
631 138 ret = receive_internal(sq, stream_idx, frame);
632
633 558107 return ret;
634 }
635
636 3742 int sq_add_stream(SyncQueue *sq, int limiting)
637 {
638 SyncQueueStream *tmp, *st;
639
640 3742 tmp = av_realloc_array(sq->streams, sq->nb_streams + 1, sizeof(*sq->streams));
641
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 3742 times.
3742 if (!tmp)
642 ✗ return AVERROR(ENOMEM);
643 3742 sq->streams = tmp;
644
645 3742 st = &sq->streams[sq->nb_streams];
646 3742 memset(st, 0, sizeof(*st));
647
648 7484 st->fifo = (sq->type == SYNC_QUEUE_FRAMES) ?
649
2/2
✓ Branch 5 → 6 taken 3623 times.
✓ Branch 5 → 7 taken 119 times.
3742 av_container_fifo_alloc_avframe(0) : av_container_fifo_alloc_avpacket(0);
650
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 3742 times.
3742 if (!st->fifo)
651 ✗ return AVERROR(ENOMEM);
652
653 /* we set a valid default, so that a pathological stream that never
654 * receives even a real timebase (and no frames) won't stall all other
655 * streams forever; cf. overflow_heartbeat() */
656 3742 st->tb = (AVRational){ 1, 1 };
657 3742 st->head_ts = AV_NOPTS_VALUE;
658 3742 st->frames_max = UINT64_MAX;
659 3742 st->limiting = limiting;
660
661 3742 sq->have_limiting |= limiting;
662
663 3742 return sq->nb_streams++;
664 }
665
666 3320 void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, uint64_t frames)
667 {
668 SyncQueueStream *st;
669
670
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 3320 times.
3320 av_assert0(stream_idx < sq->nb_streams);
671 3320 st = &sq->streams[stream_idx];
672
673 3320 st->frames_max = frames;
674
2/2
✓ Branch 5 → 6 taken 63 times.
✓ Branch 5 → 7 taken 3257 times.
3320 if (st->frames_sent >= st->frames_max)
675 63 finish_stream(sq, stream_idx);
676 3320 }
677
678 1 void sq_cut_upstream(SyncQueue *sq, unsigned int stream_idx, int sample_rate)
679 {
680
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 1 time.
1 av_assert0(sq->type == SYNC_QUEUE_PACKETS);
681
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 1 time.
1 av_assert0(stream_idx < sq->nb_streams);
682
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 11 taken 1 time.
1 av_assert0(sample_rate > 0);
683 1 sq->streams[stream_idx].cut_sample_rate = sample_rate;
684 1 }
685
686 390 void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx,
687 int frame_samples)
688 {
689 SyncQueueStream *st;
690
691
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 390 times.
390 av_assert0(sq->type == SYNC_QUEUE_FRAMES);
692
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 390 times.
390 av_assert0(stream_idx < sq->nb_streams);
693 390 st = &sq->streams[stream_idx];
694
695 390 st->frame_samples = frame_samples;
696
697 390 sq->align_mask = av_cpu_max_align() - 1;
698 390 }
699
700 3499 SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us, void *logctx)
701 {
702 3499 SyncQueue *sq = av_mallocz(sizeof(*sq));
703
704
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 3499 times.
3499 if (!sq)
705 ✗ return NULL;
706
707 3499 sq->type = type;
708 3499 sq->buf_size_us = buf_size_us;
709 3499 sq->logctx = logctx;
710
711 3499 sq->head_stream = -1;
712 3499 sq->head_finished_stream = -1;
713
714 3499 return sq;
715 }
716
717 12878 void sq_free(SyncQueue **psq)
718 {
719 12878 SyncQueue *sq = *psq;
720
721
2/2
✓ Branch 2 → 3 taken 9379 times.
✓ Branch 2 → 4 taken 3499 times.
12878 if (!sq)
722 9379 return;
723
724
2/2
✓ Branch 7 → 5 taken 3742 times.
✓ Branch 7 → 8 taken 3499 times.
7241 for (unsigned int i = 0; i < sq->nb_streams; i++)
725 3742 av_container_fifo_free(&sq->streams[i].fifo);
726
727 3499 av_freep(&sq->streams);
728
729 3499 av_freep(psq);
730 }
731