../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
298 / 312
95.5%
Branches:
229 / 268
85.4%
Functions:
20 / 20
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 } SyncQueueStream;
89
90 struct SyncQueue {
91 enum SyncQueueType type;
92
93 void *logctx;
94
95 /* no more frames will be sent for any stream */
96 int finished;
97 /* sync head: the stream with the _smallest_ head timestamp
98 * this stream determines which frames can be output */
99 int head_stream;
100 /* the finished stream with the smallest finish timestamp or -1 */
101 int head_finished_stream;
102
103 // maximum buffering duration in microseconds
104 int64_t buf_size_us;
105
106 SyncQueueStream *streams;
107 unsigned int nb_streams;
108
109 int have_limiting;
110
111 uintptr_t align_mask;
112 };
113
114 /**
115 * Compute the end timestamp of a frame. If nb_samples is provided, consider
116 * the frame to have this number of audio samples, otherwise use frame duration.
117 */
118 797477 static int64_t frame_end(const SyncQueue *sq, SyncQueueFrame frame, int nb_samples)
119 {
120
2/2
✓ Branch 2 → 3 taken 230266 times.
✓ Branch 2 → 4 taken 567211 times.
797477 if (nb_samples) {
121 230266 int64_t d = av_rescale_q(nb_samples, (AVRational){ 1, frame.f->sample_rate},
122 230266 frame.f->time_base);
123 230266 return frame.f->pts + d;
124 }
125
126 567211 return (sq->type == SYNC_QUEUE_PACKETS) ?
127
2/2
✓ Branch 4 → 5 taken 4535 times.
✓ Branch 4 → 6 taken 562676 times.
567211 frame.p->pts + frame.p->duration :
128 562676 frame.f->pts + frame.f->duration;
129 }
130
131 450601 static int frame_samples(const SyncQueue *sq, SyncQueueFrame frame)
132 {
133
2/2
✓ Branch 2 → 3 taken 447844 times.
✓ Branch 2 → 4 taken 2757 times.
450601 return (sq->type == SYNC_QUEUE_PACKETS) ? 0 : frame.f->nb_samples;
134 }
135
136 416424 static int frame_null(const SyncQueue *sq, SyncQueueFrame frame)
137 {
138
2/2
✓ Branch 2 → 3 taken 1028 times.
✓ Branch 2 → 4 taken 415396 times.
416424 return (sq->type == SYNC_QUEUE_PACKETS) ? (frame.p == NULL) : (frame.f == NULL);
139 }
140
141 408987 static void tb_update(const SyncQueue *sq, SyncQueueStream *st,
142 const SyncQueueFrame frame)
143 {
144 817974 AVRational tb = (sq->type == SYNC_QUEUE_PACKETS) ?
145
2/2
✓ Branch 2 → 3 taken 951 times.
✓ Branch 2 → 4 taken 408036 times.
408987 frame.p->time_base : frame.f->time_base;
146
147
2/4
✓ Branch 5 → 6 taken 408987 times.
✗ Branch 5 → 7 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 408987 times.
408987 av_assert0(tb.num > 0 && tb.den > 0);
148
149
4/4
✓ Branch 9 → 10 taken 408871 times.
✓ Branch 9 → 12 taken 116 times.
✓ Branch 10 → 11 taken 405411 times.
✓ Branch 10 → 12 taken 3460 times.
408987 if (tb.num == st->tb.num && tb.den == st->tb.den)
150 405411 return;
151
152 // timebase should not change after the first frame
153
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 16 taken 3576 times.
3576 av_assert0(!av_container_fifo_can_read(st->fifo));
154
155
2/2
✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 18 taken 3575 times.
3576 if (st->head_ts != AV_NOPTS_VALUE)
156 1 st->head_ts = av_rescale_q(st->head_ts, st->tb, tb);
157
158 3576 st->tb = tb;
159 }
160
161 10647 static void finish_stream(SyncQueue *sq, unsigned int stream_idx)
162 {
163 10647 SyncQueueStream *st = &sq->streams[stream_idx];
164
165
2/2
✓ Branch 2 → 3 taken 3659 times.
✓ Branch 2 → 6 taken 6988 times.
10647 if (!st->finished)
166 3659 av_log(sq->logctx, AV_LOG_DEBUG,
167 "sq: finish %u; head ts %s\n", stream_idx,
168 3659 av_ts2timestr(st->head_ts, &st->tb));
169
170 10647 st->finished = 1;
171
172
4/4
✓ Branch 6 → 7 taken 9679 times.
✓ Branch 6 → 24 taken 968 times.
✓ Branch 7 → 8 taken 9614 times.
✓ Branch 7 → 24 taken 65 times.
10647 if (st->limiting && st->head_ts != AV_NOPTS_VALUE) {
173 /* check if this stream is the new finished head */
174
4/4
✓ Branch 8 → 9 taken 6398 times.
✓ Branch 8 → 11 taken 3216 times.
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 12 taken 6397 times.
16012 if (sq->head_finished_stream < 0 ||
175 6398 av_compare_ts(st->head_ts, st->tb,
176 6398 sq->streams[sq->head_finished_stream].head_ts,
177 6398 sq->streams[sq->head_finished_stream].tb) < 0) {
178 3217 sq->head_finished_stream = stream_idx;
179 }
180
181 /* mark as finished all streams that should no longer receive new frames,
182 * due to them being ahead of some finished stream */
183 9614 st = &sq->streams[sq->head_finished_stream];
184
2/2
✓ Branch 23 → 13 taken 9646 times.
✓ Branch 23 → 24 taken 9614 times.
19260 for (unsigned int i = 0; i < sq->nb_streams; i++) {
185 9646 SyncQueueStream *st1 = &sq->streams[i];
186
6/6
✓ Branch 13 → 14 taken 32 times.
✓ Branch 13 → 22 taken 9614 times.
✓ Branch 14 → 15 taken 31 times.
✓ Branch 14 → 22 taken 1 time.
✓ Branch 16 → 17 taken 22 times.
✓ Branch 16 → 22 taken 9 times.
9677 if (st != st1 && st1->head_ts != AV_NOPTS_VALUE &&
187 31 av_compare_ts(st->head_ts, st->tb, st1->head_ts, st1->tb) <= 0) {
188
2/2
✓ Branch 17 → 18 taken 3 times.
✓ Branch 17 → 21 taken 19 times.
22 if (!st1->finished)
189 3 av_log(sq->logctx, AV_LOG_DEBUG,
190 "sq: finish secondary %u; head ts %s\n", i,
191 3 av_ts2timestr(st1->head_ts, &st1->tb));
192
193 22 st1->finished = 1;
194 }
195 }
196 }
197
198 /* mark the whole queue as finished if all streams are finished */
199
2/2
✓ Branch 28 → 25 taken 26657 times.
✓ Branch 28 → 29 taken 10451 times.
37108 for (unsigned int i = 0; i < sq->nb_streams; i++) {
200
2/2
✓ Branch 25 → 26 taken 196 times.
✓ Branch 25 → 27 taken 26461 times.
26657 if (!sq->streams[i].finished)
201 196 return;
202 }
203 10451 sq->finished = 1;
204
205 10451 av_log(sq->logctx, AV_LOG_DEBUG, "sq: finish queue\n");
206 }
207
208 307786 static void queue_head_update(SyncQueue *sq)
209 {
210
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 307786 times.
307786 av_assert0(sq->have_limiting);
211
212
2/2
✓ Branch 5 → 6 taken 3275 times.
✓ Branch 5 → 19 taken 304511 times.
307786 if (sq->head_stream < 0) {
213 3275 unsigned first_limiting = UINT_MAX;
214
215 /* wait for one timestamp in each stream before determining
216 * the queue head */
217
2/2
✓ Branch 14 → 7 taken 3342 times.
✓ Branch 14 → 15 taken 3217 times.
6559 for (unsigned int i = 0; i < sq->nb_streams; i++) {
218 3342 SyncQueueStream *st = &sq->streams[i];
219
2/2
✓ Branch 7 → 8 taken 4 times.
✓ Branch 7 → 9 taken 3338 times.
3342 if (!st->limiting)
220 4 continue;
221
2/2
✓ Branch 9 → 10 taken 58 times.
✓ Branch 9 → 11 taken 3280 times.
3338 if (st->head_ts == AV_NOPTS_VALUE)
222 58 return;
223
2/2
✓ Branch 11 → 12 taken 3274 times.
✓ Branch 11 → 13 taken 6 times.
3280 if (first_limiting == UINT_MAX)
224 3274 first_limiting = i;
225 }
226
227 // placeholder value, correct one will be found below
228
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 3217 times.
3217 av_assert0(first_limiting < UINT_MAX);
229 3217 sq->head_stream = first_limiting;
230 }
231
232
2/2
✓ Branch 26 → 20 taken 602813 times.
✓ Branch 26 → 27 taken 307728 times.
910541 for (unsigned int i = 0; i < sq->nb_streams; i++) {
233 602813 SyncQueueStream *st_head = &sq->streams[sq->head_stream];
234 602813 SyncQueueStream *st_other = &sq->streams[i];
235
5/6
✓ Branch 20 → 21 taken 602781 times.
✓ Branch 20 → 25 taken 32 times.
✓ Branch 21 → 22 taken 602781 times.
✗ Branch 21 → 25 not taken.
✓ Branch 23 → 24 taken 98 times.
✓ Branch 23 → 25 taken 602683 times.
1205594 if (st_other->limiting && st_other->head_ts != AV_NOPTS_VALUE &&
236 602781 av_compare_ts(st_other->head_ts, st_other->tb,
237 st_head->head_ts, st_head->tb) < 0)
238 98 sq->head_stream = i;
239 }
240 }
241
242 /* update this stream's head timestamp */
243 409125 static void stream_update_ts(SyncQueue *sq, unsigned int stream_idx, int64_t ts)
244 {
245 409125 SyncQueueStream *st = &sq->streams[stream_idx];
246
247
2/2
✓ Branch 2 → 3 taken 409122 times.
✓ Branch 2 → 5 taken 3 times.
409125 if (ts == AV_NOPTS_VALUE ||
248
4/4
✓ Branch 3 → 4 taken 405526 times.
✓ Branch 3 → 6 taken 3596 times.
✓ Branch 4 → 5 taken 8134 times.
✓ Branch 4 → 6 taken 397392 times.
409122 (st->head_ts != AV_NOPTS_VALUE && st->head_ts >= ts))
249 8137 return;
250
251 400988 st->head_ts = ts;
252
253 /* if this stream is now ahead of some finished stream, then
254 * this stream is also finished */
255
4/4
✓ Branch 6 → 7 taken 132 times.
✓ Branch 6 → 10 taken 400856 times.
✓ Branch 8 → 9 taken 6 times.
✓ Branch 8 → 10 taken 126 times.
401120 if (sq->head_finished_stream >= 0 &&
256 132 av_compare_ts(sq->streams[sq->head_finished_stream].head_ts,
257 132 sq->streams[sq->head_finished_stream].tb,
258 ts, st->tb) <= 0)
259 6 finish_stream(sq, stream_idx);
260
261 /* update the overall head timestamp if it could have changed */
262
2/2
✓ Branch 10 → 11 taken 365613 times.
✓ Branch 10 → 14 taken 35375 times.
400988 if (st->limiting &&
263
4/4
✓ Branch 11 → 12 taken 362338 times.
✓ Branch 11 → 13 taken 3275 times.
✓ Branch 12 → 13 taken 304511 times.
✓ Branch 12 → 14 taken 57827 times.
365613 (sq->head_stream < 0 || sq->head_stream == stream_idx))
264 307786 queue_head_update(sq);
265 }
266
267 /* If the queue for the given stream (or all streams when stream_idx=-1)
268 * is overflowing, trigger a fake heartbeat on lagging streams.
269 *
270 * @return 1 if heartbeat triggered, 0 otherwise
271 */
272 406109 static int overflow_heartbeat(SyncQueue *sq, int stream_idx)
273 {
274 SyncQueueStream *st;
275 SyncQueueFrame frame;
276 406109 int64_t tail_ts = AV_NOPTS_VALUE;
277
278 /* if no stream specified, pick the one that is most ahead */
279
1/2
✓ Branch 2 → 3 taken 406109 times.
✗ Branch 2 → 13 not taken.
406109 if (stream_idx < 0) {
280 406109 int64_t ts = AV_NOPTS_VALUE;
281
282
2/2
✓ Branch 10 → 4 taken 1098894 times.
✓ Branch 10 → 11 taken 406109 times.
1505003 for (int i = 0; i < sq->nb_streams; i++) {
283 1098894 st = &sq->streams[i];
284
4/4
✓ Branch 4 → 5 taken 1093534 times.
✓ Branch 4 → 9 taken 5360 times.
✓ Branch 5 → 6 taken 687428 times.
✓ Branch 5 → 8 taken 406106 times.
1098894 if (st->head_ts != AV_NOPTS_VALUE &&
285
2/2
✓ Branch 7 → 8 taken 57843 times.
✓ Branch 7 → 9 taken 629585 times.
687428 (ts == AV_NOPTS_VALUE ||
286 687428 av_compare_ts(ts, sq->streams[stream_idx].tb,
287 st->head_ts, st->tb) < 0)) {
288 463949 ts = st->head_ts;
289 463949 stream_idx = i;
290 }
291 }
292 /* no stream has a timestamp yet -> nothing to do */
293
2/2
✓ Branch 11 → 12 taken 3 times.
✓ Branch 11 → 13 taken 406106 times.
406109 if (stream_idx < 0)
294 3 return 0;
295 }
296
297 406106 st = &sq->streams[stream_idx];
298
299 /* get the chosen stream's tail timestamp */
300
4/4
✓ Branch 19 → 20 taken 406106 times.
✓ Branch 19 → 22 taken 91949 times.
✓ Branch 21 → 14 taken 91949 times.
✓ Branch 21 → 22 taken 314157 times.
904161 for (size_t i = 0; tail_ts == AV_NOPTS_VALUE &&
301 498055 av_container_fifo_peek(st->fifo, (void**)&frame, i) >= 0; i++) {
302 91949 int nb_samples = 0;
303
304 /* Use the chunk end from receive_for_stream() only when the queued
305 * frame is shorter than the encoder frame size; otherwise keep the
306 * frame's own end timestamp. */
307
2/2
✓ Branch 14 → 15 taken 91142 times.
✓ Branch 14 → 17 taken 807 times.
91949 if (sq->type == SYNC_QUEUE_FRAMES &&
308
2/2
✓ Branch 15 → 16 taken 82997 times.
✓ Branch 15 → 17 taken 8145 times.
91142 frame.f->nb_samples < st->frame_samples)
309 82997 nb_samples = st->frame_samples;
310
311 91949 tail_ts = frame_end(sq, frame, nb_samples);
312 }
313
314 /* overflow triggers when the tail is over specified duration behind the head */
315
4/4
✓ Branch 22 → 23 taken 91949 times.
✓ Branch 22 → 25 taken 314157 times.
✓ Branch 23 → 24 taken 35267 times.
✓ Branch 23 → 25 taken 56682 times.
406106 if (tail_ts == AV_NOPTS_VALUE || tail_ts >= st->head_ts ||
316
2/2
✓ Branch 24 → 25 taken 35129 times.
✓ Branch 24 → 26 taken 138 times.
35267 av_rescale_q(st->head_ts - tail_ts, st->tb, AV_TIME_BASE_Q) < sq->buf_size_us)
317 405968 return 0;
318
319 /* signal a fake timestamp for all streams that prevent tail_ts from being output */
320 138 tail_ts++;
321
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++) {
322 276 const SyncQueueStream *st1 = &sq->streams[i];
323 int64_t ts;
324
325
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 ||
326
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 &&
327 137 av_compare_ts(tail_ts, st->tb, st1->head_ts, st1->tb) <= 0))
328 138 continue;
329
330 138 ts = av_rescale_q(tail_ts, st->tb, st1->tb);
331
2/2
✓ Branch 33 → 34 taken 137 times.
✓ Branch 33 → 37 taken 1 time.
138 if (st1->head_ts != AV_NOPTS_VALUE)
332
2/2
✓ Branch 34 → 35 taken 43 times.
✓ Branch 34 → 36 taken 94 times.
137 ts = FFMAX(st1->head_ts + 1, ts);
333
334 138 av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u overflow heardbeat %s -> %s\n",
335 138 i, av_ts2timestr(st1->head_ts, &st1->tb), av_ts2timestr(ts, &st1->tb));
336
337 138 stream_update_ts(sq, i, ts);
338 }
339
340 138 return 1;
341 }
342
343 416424 int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
344 {
345 SyncQueueStream *st;
346 int64_t ts;
347 int ret, nb_samples;
348
349
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 416424 times.
416424 av_assert0(stream_idx < sq->nb_streams);
350 416424 st = &sq->streams[stream_idx];
351
352
2/2
✓ Branch 6 → 7 taken 7372 times.
✓ Branch 6 → 10 taken 409052 times.
416424 if (frame_null(sq, frame)) {
353 7372 av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u EOF\n", stream_idx);
354 7372 finish_stream(sq, stream_idx);
355 7372 return 0;
356 }
357
2/2
✓ Branch 10 → 11 taken 65 times.
✓ Branch 10 → 12 taken 408987 times.
409052 if (st->finished)
358 65 return AVERROR_EOF;
359
360 408987 tb_update(sq, st, frame);
361
362 408987 nb_samples = frame_samples(sq, frame);
363 // make sure frame duration is consistent with sample count
364
2/2
✓ Branch 14 → 15 taken 399397 times.
✓ Branch 14 → 19 taken 9590 times.
408987 if (nb_samples) {
365
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 399397 times.
399397 av_assert0(frame.f->sample_rate > 0);
366 399397 frame.f->duration = av_rescale_q(nb_samples, (AVRational){ 1, frame.f->sample_rate },
367 399397 frame.f->time_base);
368 }
369
370 408987 ts = frame_end(sq, frame, 0);
371
372 408987 av_log(sq->logctx, AV_LOG_DEBUG, "sq: send %u ts %s\n", stream_idx,
373 408987 av_ts2timestr(ts, &st->tb));
374
375
2/2
✓ Branch 22 → 23 taken 408036 times.
✓ Branch 22 → 24 taken 951 times.
408987 ret = av_container_fifo_write(st->fifo, SQPTR(sq, frame), 0);
376
1/2
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 28 taken 408987 times.
408987 if (ret < 0)
377 ✗ return ret;
378
379 408987 stream_update_ts(sq, stream_idx, ts);
380
381 408987 st->samples_queued += nb_samples;
382 408987 st->samples_sent += nb_samples;
383
384
2/2
✓ Branch 29 → 30 taken 395298 times.
✓ Branch 29 → 31 taken 13689 times.
408987 if (st->frame_samples)
385 395298 st->frames_sent = st->samples_sent / st->frame_samples;
386 else
387 13689 st->frames_sent++;
388
389
2/2
✓ Branch 32 → 33 taken 3206 times.
✓ Branch 32 → 35 taken 405781 times.
408987 if (st->frames_sent >= st->frames_max) {
390 3206 av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u frames_max %"PRIu64" reached\n",
391 stream_idx, st->frames_max);
392
393 3206 finish_stream(sq, stream_idx);
394 }
395
396 408987 return 0;
397 }
398
399 102588 static void offset_audio(AVFrame *f, int nb_samples)
400 {
401 102588 const int planar = av_sample_fmt_is_planar(f->format);
402
2/2
✓ Branch 3 → 4 taken 23670 times.
✓ Branch 3 → 5 taken 78918 times.
102588 const int planes = planar ? f->ch_layout.nb_channels : 1;
403 102588 const int bps = av_get_bytes_per_sample(f->format);
404
2/2
✓ Branch 7 → 8 taken 78918 times.
✓ Branch 7 → 9 taken 23670 times.
102588 const int offset = nb_samples * bps * (planar ? 1 : f->ch_layout.nb_channels);
405
406
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 13 taken 102588 times.
102588 av_assert0(bps > 0);
407
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 16 taken 102588 times.
102588 av_assert0(nb_samples < f->nb_samples);
408
409
2/2
✓ Branch 20 → 17 taken 126116 times.
✓ Branch 20 → 21 taken 102588 times.
228704 for (int i = 0; i < planes; i++) {
410 126116 f->extended_data[i] += offset;
411
2/2
✓ Branch 17 → 18 taken 125900 times.
✓ Branch 17 → 19 taken 216 times.
126116 if (i < FF_ARRAY_ELEMS(f->data))
412 125900 f->data[i] = f->extended_data[i];
413 }
414 102588 f->linesize[0] -= offset;
415 102588 f->nb_samples -= nb_samples;
416 102588 f->duration = av_rescale_q(f->nb_samples, (AVRational){ 1, f->sample_rate },
417 f->time_base);
418 102588 f->pts += av_rescale_q(nb_samples, (AVRational){ 1, f->sample_rate },
419 f->time_base);
420 102588 }
421
422 87655 static int frame_is_aligned(const SyncQueue *sq, const AVFrame *frame)
423 {
424 // only checks linesize[0], so only works for audio
425
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 87655 times.
87655 av_assert0(frame->nb_samples > 0);
426
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 87655 times.
87655 av_assert0(sq->align_mask);
427
428 // only check data[0], because we always offset all data pointers
429 // by the same offset, so if one is aligned, all are
430
2/2
✓ Branch 8 → 9 taken 86793 times.
✓ Branch 8 → 12 taken 862 times.
87655 if (!((uintptr_t)frame->data[0] & sq->align_mask) &&
431
1/2
✓ Branch 9 → 10 taken 86793 times.
✗ Branch 9 → 12 not taken.
86793 !(frame->linesize[0] & sq->align_mask) &&
432
1/2
✓ Branch 10 → 11 taken 86793 times.
✗ Branch 10 → 12 not taken.
86793 frame->linesize[0] > sq->align_mask)
433 86793 return 1;
434
435 862 return 0;
436 }
437
438 105650 static int receive_samples(SyncQueue *sq, SyncQueueStream *st,
439 AVFrame *dst, int nb_samples)
440 {
441 SyncQueueFrame src;
442 int ret;
443
444
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 105650 times.
105650 av_assert0(st->samples_queued >= nb_samples);
445
446 105650 ret = av_container_fifo_peek(st->fifo, (void**)&src, 0);
447
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 105650 times.
105650 av_assert0(ret >= 0);
448
449 // peeked frame has enough samples and its data is aligned
450 // -> we can just make a reference and limit its sample count
451
4/4
✓ Branch 9 → 10 taken 80486 times.
✓ Branch 9 → 17 taken 25164 times.
✓ Branch 11 → 12 taken 79627 times.
✓ Branch 11 → 17 taken 859 times.
105650 if (src.f->nb_samples > nb_samples && frame_is_aligned(sq, src.f)) {
452 79627 ret = av_frame_ref(dst, src.f);
453
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 79627 times.
79627 if (ret < 0)
454 ✗ return ret;
455
456 79627 dst->nb_samples = nb_samples;
457 79627 offset_audio(src.f, nb_samples);
458 79627 st->samples_queued -= nb_samples;
459
460 79627 goto finish;
461 }
462
463 // otherwise allocate a new frame and copy the data
464 26023 ret = av_channel_layout_copy(&dst->ch_layout, &src.f->ch_layout);
465
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 26023 times.
26023 if (ret < 0)
466 ✗ return ret;
467
468 26023 dst->format = src.f->format;
469 26023 dst->nb_samples = nb_samples;
470
471 26023 ret = av_frame_get_buffer(dst, 0);
472
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 26023 times.
26023 if (ret < 0)
473 ✗ goto fail;
474
475 26023 ret = av_frame_copy_props(dst, src.f);
476
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 26023 times.
26023 if (ret < 0)
477 ✗ goto fail;
478
479 26023 dst->nb_samples = 0;
480
2/2
✓ Branch 36 → 27 taken 411091 times.
✓ Branch 36 → 37 taken 26023 times.
437114 while (dst->nb_samples < nb_samples) {
481 int to_copy;
482
483 411091 ret = av_container_fifo_peek(st->fifo, (void**)&src, 0);
484
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 31 taken 411091 times.
411091 av_assert0(ret >= 0);
485
486 411091 to_copy = FFMIN(nb_samples - dst->nb_samples, src.f->nb_samples);
487
488 411091 av_samples_copy(dst->extended_data, src.f->extended_data, dst->nb_samples,
489 411091 0, to_copy, dst->ch_layout.nb_channels, dst->format);
490
491
2/2
✓ Branch 32 → 33 taken 22961 times.
✓ Branch 32 → 34 taken 388130 times.
411091 if (to_copy < src.f->nb_samples)
492 22961 offset_audio(src.f, to_copy);
493 else
494 388130 av_container_fifo_drain(st->fifo, 1);
495
496 411091 st->samples_queued -= to_copy;
497
498 411091 dst->nb_samples += to_copy;
499 }
500
501 26023 finish:
502 105650 dst->duration = av_rescale_q(nb_samples, (AVRational){ 1, dst->sample_rate },
503 dst->time_base);
504
505 105650 return 0;
506
507 ✗ fail:
508 ✗ av_frame_unref(dst);
509 ✗ return ret;
510 }
511
512 1388207 static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,
513 SyncQueueFrame frame)
514 {
515 632335 const SyncQueueStream *st_head = sq->head_stream >= 0 ?
516
2/2
✓ Branch 2 → 3 taken 755872 times.
✓ Branch 2 → 4 taken 632335 times.
1388207 &sq->streams[sq->head_stream] : NULL;
517 SyncQueueStream *st;
518
519
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 1388207 times.
1388207 av_assert0(stream_idx < sq->nb_streams);
520 1388207 st = &sq->streams[stream_idx];
521
522
2/2
✓ Branch 9 → 10 taken 927732 times.
✓ Branch 9 → 52 taken 460475 times.
1388207 if (av_container_fifo_can_read(st->fifo) &&
523
4/4
✓ Branch 10 → 11 taken 757973 times.
✓ Branch 10 → 12 taken 169759 times.
✓ Branch 11 → 12 taken 325 times.
✓ Branch 11 → 52 taken 757648 times.
927732 (st->frame_samples <= st->samples_queued || st->finished)) {
524 170084 int nb_samples = st->frame_samples;
525 SyncQueueFrame peek;
526 int64_t ts;
527 170084 int cmp = 1;
528
529
2/2
✓ Branch 12 → 13 taken 3891 times.
✓ Branch 12 → 14 taken 166193 times.
170084 if (st->finished)
530 3891 nb_samples = FFMIN(nb_samples, st->samples_queued);
531
532 170084 av_container_fifo_peek(st->fifo, (void**)&peek, 0);
533 170084 ts = frame_end(sq, peek, nb_samples);
534
535 /* check if this stream's tail timestamp does not overtake
536 * the overall queue head */
537
4/4
✓ Branch 16 → 17 taken 170081 times.
✓ Branch 16 → 19 taken 3 times.
✓ Branch 17 → 18 taken 57612 times.
✓ Branch 17 → 19 taken 112469 times.
170084 if (ts != AV_NOPTS_VALUE && st_head)
538 57612 cmp = av_compare_ts(ts, st->tb, st_head->head_ts, st_head->tb);
539
540 /* We can release frames that do not end after the queue head.
541 * Frames with no timestamps are just passed through with no conditions.
542 * Frames are also passed through when there are no limiting streams.
543 */
544
6/6
✓ Branch 19 → 20 taken 156039 times.
✓ Branch 19 → 22 taken 14045 times.
✓ Branch 20 → 21 taken 156036 times.
✓ Branch 20 → 22 taken 3 times.
✓ Branch 21 → 22 taken 112409 times.
✓ Branch 21 → 51 taken 43627 times.
170084 if (cmp <= 0 || ts == AV_NOPTS_VALUE || !sq->have_limiting) {
545
2/2
✓ Branch 22 → 23 taken 112816 times.
✓ Branch 22 → 30 taken 13641 times.
126457 if (nb_samples &&
546
4/4
✓ Branch 23 → 24 taken 7169 times.
✓ Branch 23 → 26 taken 105647 times.
✓ Branch 25 → 26 taken 3 times.
✓ Branch 25 → 30 taken 7166 times.
218466 (nb_samples != peek.f->nb_samples || !frame_is_aligned(sq, peek.f))) {
547 105650 int ret = receive_samples(sq, st, frame.f, nb_samples);
548
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 105650 times.
105650 if (ret < 0)
549 ✗ return ret;
550 } else {
551
2/2
✓ Branch 30 → 31 taken 19904 times.
✓ Branch 30 → 32 taken 903 times.
20807 int ret = av_container_fifo_read(st->fifo, SQPTR(sq, frame), 0);
552
1/2
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 37 taken 20807 times.
20807 av_assert0(ret >= 0);
553
554
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 41 taken 20807 times.
20807 av_assert0(st->samples_queued >= frame_samples(sq, frame));
555 20807 st->samples_queued -= frame_samples(sq, frame);
556 }
557
558 238869 av_log(sq->logctx, AV_LOG_DEBUG,
559 "sq: receive %u ts %s queue head %d ts %s\n", stream_idx,
560 126457 av_ts2timestr(frame_end(sq, frame, 0), &st->tb),
561 sq->head_stream,
562
2/2
✓ Branch 43 → 44 taken 14045 times.
✓ Branch 43 → 45 taken 112412 times.
126457 st_head ? av_ts2timestr(st_head->head_ts, &st_head->tb) : "N/A");
563
564 126457 return 0;
565 }
566 }
567
568
4/4
✓ Branch 53 → 54 taken 10487 times.
✓ Branch 53 → 57 taken 1230148 times.
✓ Branch 55 → 56 taken 10271 times.
✓ Branch 55 → 57 taken 216 times.
1240635 return (sq->finished || (st->finished && !av_container_fifo_can_read(st->fifo))) ?
569
2/2
✓ Branch 52 → 53 taken 1240635 times.
✓ Branch 52 → 56 taken 21115 times.
2502385 AVERROR_EOF : AVERROR(EAGAIN);
570 }
571
572 542929 static int receive_internal(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
573 {
574 542929 int nb_eof = 0;
575 int ret;
576
577 /* read a frame for a specific stream */
578
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 8 taken 542929 times.
542929 if (stream_idx >= 0) {
579 ✗ ret = receive_for_stream(sq, stream_idx, frame);
580
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ return (ret < 0) ? ret : stream_idx;
581 }
582
583 /* read a frame for any stream with available output */
584
2/2
✓ Branch 18 → 9 taken 1388207 times.
✓ Branch 18 → 19 taken 416472 times.
1804679 for (unsigned int i = 0; i < sq->nb_streams; i++) {
585 1388207 ret = receive_for_stream(sq, i, frame);
586
4/4
✓ Branch 10 → 11 taken 1356821 times.
✓ Branch 10 → 12 taken 31386 times.
✓ Branch 11 → 12 taken 1230364 times.
✓ Branch 11 → 13 taken 126457 times.
1388207 if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) {
587 1261750 nb_eof += (ret == AVERROR_EOF);
588 1261750 continue;
589 }
590
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 126457 times.
126457 return (ret < 0) ? ret : i;
591 }
592
593
2/2
✓ Branch 19 → 20 taken 10363 times.
✓ Branch 19 → 21 taken 406109 times.
416472 return (nb_eof == sq->nb_streams) ? AVERROR_EOF : AVERROR(EAGAIN);
594 }
595
596 542791 int sq_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
597 {
598 542791 int ret = receive_internal(sq, stream_idx, frame);
599
600 /* try again if the queue overflowed and triggered a fake heartbeat
601 * for lagging streams */
602
4/4
✓ Branch 3 → 4 taken 406109 times.
✓ Branch 3 → 7 taken 136682 times.
✓ Branch 5 → 6 taken 138 times.
✓ Branch 5 → 7 taken 405971 times.
542791 if (ret == AVERROR(EAGAIN) && overflow_heartbeat(sq, stream_idx))
603 138 ret = receive_internal(sq, stream_idx, frame);
604
605 542791 return ret;
606 }
607
608 3664 int sq_add_stream(SyncQueue *sq, int limiting)
609 {
610 SyncQueueStream *tmp, *st;
611
612 3664 tmp = av_realloc_array(sq->streams, sq->nb_streams + 1, sizeof(*sq->streams));
613
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 3664 times.
3664 if (!tmp)
614 ✗ return AVERROR(ENOMEM);
615 3664 sq->streams = tmp;
616
617 3664 st = &sq->streams[sq->nb_streams];
618 3664 memset(st, 0, sizeof(*st));
619
620 7328 st->fifo = (sq->type == SYNC_QUEUE_FRAMES) ?
621
2/2
✓ Branch 5 → 6 taken 3562 times.
✓ Branch 5 → 7 taken 102 times.
3664 av_container_fifo_alloc_avframe(0) : av_container_fifo_alloc_avpacket(0);
622
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 3664 times.
3664 if (!st->fifo)
623 ✗ return AVERROR(ENOMEM);
624
625 /* we set a valid default, so that a pathological stream that never
626 * receives even a real timebase (and no frames) won't stall all other
627 * streams forever; cf. overflow_heartbeat() */
628 3664 st->tb = (AVRational){ 1, 1 };
629 3664 st->head_ts = AV_NOPTS_VALUE;
630 3664 st->frames_max = UINT64_MAX;
631 3664 st->limiting = limiting;
632
633 3664 sq->have_limiting |= limiting;
634
635 3664 return sq->nb_streams++;
636 }
637
638 3276 void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, uint64_t frames)
639 {
640 SyncQueueStream *st;
641
642
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 3276 times.
3276 av_assert0(stream_idx < sq->nb_streams);
643 3276 st = &sq->streams[stream_idx];
644
645 3276 st->frames_max = frames;
646
2/2
✓ Branch 5 → 6 taken 63 times.
✓ Branch 5 → 7 taken 3213 times.
3276 if (st->frames_sent >= st->frames_max)
647 63 finish_stream(sq, stream_idx);
648 3276 }
649
650 366 void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx,
651 int frame_samples)
652 {
653 SyncQueueStream *st;
654
655
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 366 times.
366 av_assert0(sq->type == SYNC_QUEUE_FRAMES);
656
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 366 times.
366 av_assert0(stream_idx < sq->nb_streams);
657 366 st = &sq->streams[stream_idx];
658
659 366 st->frame_samples = frame_samples;
660
661 366 sq->align_mask = av_cpu_max_align() - 1;
662 366 }
663
664 3433 SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us, void *logctx)
665 {
666 3433 SyncQueue *sq = av_mallocz(sizeof(*sq));
667
668
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 3433 times.
3433 if (!sq)
669 ✗ return NULL;
670
671 3433 sq->type = type;
672 3433 sq->buf_size_us = buf_size_us;
673 3433 sq->logctx = logctx;
674
675 3433 sq->head_stream = -1;
676 3433 sq->head_finished_stream = -1;
677
678 3433 return sq;
679 }
680
681 12571 void sq_free(SyncQueue **psq)
682 {
683 12571 SyncQueue *sq = *psq;
684
685
2/2
✓ Branch 2 → 3 taken 9138 times.
✓ Branch 2 → 4 taken 3433 times.
12571 if (!sq)
686 9138 return;
687
688
2/2
✓ Branch 7 → 5 taken 3664 times.
✓ Branch 7 → 8 taken 3433 times.
7097 for (unsigned int i = 0; i < sq->nb_streams; i++)
689 3664 av_container_fifo_free(&sq->streams[i].fifo);
690
691 3433 av_freep(&sq->streams);
692
693 3433 av_freep(psq);
694 }
695