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
31
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
73
74 /* number of audio samples in fifo */
76 /* stream head: largest timestamp seen */
79 /* no more frames will be sent for this stream */
81
87
90
92
93 /* no more frames will be sent for any stream */
95 /* sync head: the stream with the _smallest_ head timestamp
96 * this stream determines which frames can be output */
98 /* the finished stream with the smallest finish timestamp or -1 */
100
101 // maximum buffering duration in microseconds
103
106
107 // pool of preallocated frames to avoid constant allocations
109
111
113 };
114
117 {
120 else
122 }
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 */
129 {
130 if (nb_samples) {
133 return frame.f->pts + d;
134 }
135
139 }
140
142 {
144 }
145
147 {
149 }
150
153 {
156
158
160 return;
161
162 // timebase should not change after the first frame
164
167
169 }
170
172 {
174
177 "sq: finish %u; head ts %s\n", stream_idx,
179
181
183 /* check if this stream is the new finished head */
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 */
200 "sq: finish secondary %u; head ts %s\n",
i,
202
204 }
205 }
206 }
207
208 /* mark the whole queue as finished if all streams are finished */
211 return;
212 }
214
216 }
217
219 {
221
223 unsigned first_limiting = UINT_MAX;
224
225 /* wait for one timestamp in each stream before determining
226 * the queue head */
230 continue;
232 return;
233 if (first_limiting == UINT_MAX)
235 }
236
237 // placeholder value, correct one will be found below
240 }
241
249 }
250 }
251
252 /* update this stream's head timestamp */
254 {
256
259 return;
260
262
263 /* if this stream is now ahead of some finished stream, then
264 * this stream is also finished */
270
271 /* update the overall head timestamp if it could have changed */
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 */
283 {
287
288 /* if no stream specified, pick the one that is most ahead */
289 if (stream_idx < 0) {
291
300 }
301 }
302 /* no stream has a timestamp yet -> nothing to do */
303 if (stream_idx < 0)
304 return 0;
305 }
306
308
309 /* get the chosen stream's tail timestamp */
313
314 /* overflow triggers when the tail is over specified duration behind the head */
317 return 0;
318
319 /* signal a fake timestamp for all streams that prevent tail_ts from being output */
320 tail_ts++;
324
328 continue;
329
333
336
338 }
339
340 return 1;
341 }
342
344 {
349
352
356 return 0;
357 }
360
362
366
368
370 // make sure frame duration is consistent with sample count
371 if (nb_samples) {
375 }
376
378
381
387 }
388
390
393
396 else
398
402
404 }
405
406 return 0;
407 }
408
410 {
414 const int offset = nb_samples *
bps * (
planar ? 1 :
f->ch_layout.nb_channels);
415
418
422 f->data[
i] =
f->extended_data[
i];
423 }
425 f->nb_samples -= nb_samples;
430 }
431
433 {
434 // only checks linesize[0], so only works for audio
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
443 return 1;
444
445 return 0;
446 }
447
450 {
453
455
458
459 // peeked frame has enough samples and its data is aligned
460 // -> we can just make a reference and limit its sample count
465
466 dst->nb_samples = nb_samples;
469
471 }
472
473 // otherwise allocate a new frame and copy the data
477
478 dst->format =
src.f->format;
479 dst->nb_samples = nb_samples;
480
484
488
490 while (
dst->nb_samples < nb_samples) {
491 int to_copy;
492
495
496 to_copy =
FFMIN(nb_samples -
dst->nb_samples,
src.f->nb_samples);
497
499 0, to_copy,
dst->ch_layout.nb_channels,
dst->format);
500
501 if (to_copy < src.f->nb_samples)
503 else {
507 }
509
510 dst->nb_samples += to_copy;
511 }
512
516
517 return 0;
518
522 }
523
526 {
530
533
540
543
546
547 /* check if this stream's tail timestamp does not overtake
548 * the overall queue head */
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 if (cmp <= 0 || ts == AV_NOPTS_VALUE || !sq->have_limiting) {
557 if (nb_samples &&
562 } else {
568 }
569
571 "sq: receive %u ts %s queue head %d ts %s\n", stream_idx,
575
576 return 0;
577 }
578 }
579
582 }
583
585 {
586 int nb_eof = 0;
588
589 /* read a frame for a specific stream */
590 if (stream_idx >= 0) {
592 return (
ret < 0) ?
ret : stream_idx;
593 }
594
595 /* read a frame for any stream with available output */
600 continue;
601 }
603 }
604
606 }
607
609 {
611
612 /* try again if the queue overflowed and triggered a fake heartbeat
613 * for lagging streams */
616
618 }
619
621 {
623
628
630 memset(st, 0, sizeof(*st));
631
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() */
643
645
647 }
648
650 {
652
655
659 }
660
663 {
665
669
671
673 }
674
676 {
678
679 if (!sq)
681
685
688
694 }
695
696 return sq;
697 }
698
700 {
702
703 if (!sq)
704 return;
705
710
712 }
713
715
717
719 }