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
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
75
76 /* number of audio samples in fifo */
78 /* stream head: largest timestamp seen */
81 /* no more frames will be sent for this stream */
83
89
92
94
95 /* no more frames will be sent for any stream */
97 /* sync head: the stream with the _smallest_ head timestamp
98 * this stream determines which frames can be output */
100 /* the finished stream with the smallest finish timestamp or -1 */
102
103 // maximum buffering duration in microseconds
105
108
110
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 */
119 {
120 if (nb_samples) {
123 return frame.f->pts + d;
124 }
125
129 }
130
132 {
134 }
135
137 {
139 }
140
143 {
146
148
150 return;
151
152 // timebase should not change after the first frame
154
157
159 }
160
162 {
164
167 "sq: finish %u; head ts %s\n", stream_idx,
169
171
173 /* check if this stream is the new finished head */
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 */
190 "sq: finish secondary %u; head ts %s\n",
i,
192
194 }
195 }
196 }
197
198 /* mark the whole queue as finished if all streams are finished */
201 return;
202 }
204
206 }
207
209 {
211
213 unsigned first_limiting = UINT_MAX;
214
215 /* wait for one timestamp in each stream before determining
216 * the queue head */
220 continue;
222 return;
223 if (first_limiting == UINT_MAX)
225 }
226
227 // placeholder value, correct one will be found below
230 }
231
239 }
240 }
241
242 /* update this stream's head timestamp */
244 {
246
249 return;
250
252
253 /* if this stream is now ahead of some finished stream, then
254 * this stream is also finished */
260
261 /* update the overall head timestamp if it could have changed */
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 */
273 {
277
278 /* if no stream specified, pick the one that is most ahead */
279 if (stream_idx < 0) {
281
290 }
291 }
292 /* no stream has a timestamp yet -> nothing to do */
293 if (stream_idx < 0)
294 return 0;
295 }
296
298
299 /* get the chosen stream's tail timestamp */
303
304 /* overflow triggers when the tail is over specified duration behind the head */
307 return 0;
308
309 /* signal a fake timestamp for all streams that prevent tail_ts from being output */
310 tail_ts++;
314
318 continue;
319
323
326
328 }
329
330 return 1;
331 }
332
334 {
338
341
345 return 0;
346 }
349
351
353 // make sure frame duration is consistent with sample count
354 if (nb_samples) {
358 }
359
361
364
368
370
373
376 else
378
382
384 }
385
386 return 0;
387 }
388
390 {
394 const int offset = nb_samples *
bps * (
planar ? 1 :
f->ch_layout.nb_channels);
395
398
402 f->data[
i] =
f->extended_data[
i];
403 }
405 f->nb_samples -= nb_samples;
410 }
411
413 {
414 // only checks linesize[0], so only works for audio
417
418 // only check data[0], because we always offset all data pointers
419 // by the same offset, so if one is aligned, all are
423 return 1;
424
425 return 0;
426 }
427
430 {
433
435
438
439 // peeked frame has enough samples and its data is aligned
440 // -> we can just make a reference and limit its sample count
445
446 dst->nb_samples = nb_samples;
449
451 }
452
453 // otherwise allocate a new frame and copy the data
457
458 dst->format =
src.f->format;
459 dst->nb_samples = nb_samples;
460
464
468
470 while (
dst->nb_samples < nb_samples) {
471 int to_copy;
472
475
476 to_copy =
FFMIN(nb_samples -
dst->nb_samples,
src.f->nb_samples);
477
479 0, to_copy,
dst->ch_layout.nb_channels,
dst->format);
480
481 if (to_copy < src.f->nb_samples)
483 else
485
487
488 dst->nb_samples += to_copy;
489 }
490
494
495 return 0;
496
500 }
501
504 {
508
511
518
521
524
525 /* check if this stream's tail timestamp does not overtake
526 * the overall queue head */
529
530 /* We can release frames that do not end after the queue head.
531 * Frames with no timestamps are just passed through with no conditions.
532 * Frames are also passed through when there are no limiting streams.
533 */
534 if (cmp <= 0 || ts == AV_NOPTS_VALUE || !sq->have_limiting) {
535 if (nb_samples &&
540 } else {
543
546 }
547
549 "sq: receive %u ts %s queue head %d ts %s\n", stream_idx,
553
554 return 0;
555 }
556 }
557
560 }
561
563 {
564 int nb_eof = 0;
566
567 /* read a frame for a specific stream */
568 if (stream_idx >= 0) {
570 return (
ret < 0) ?
ret : stream_idx;
571 }
572
573 /* read a frame for any stream with available output */
578 continue;
579 }
581 }
582
584 }
585
587 {
589
590 /* try again if the queue overflowed and triggered a fake heartbeat
591 * for lagging streams */
594
596 }
597
599 {
601
606
608 memset(st, 0, sizeof(*st));
609
614
615 /* we set a valid default, so that a pathological stream that never
616 * receives even a real timebase (and no frames) won't stall all other
617 * streams forever; cf. overflow_heartbeat() */
622
624
626 }
627
629 {
631
634
638 }
639
642 {
644
648
650
652 }
653
655 {
657
658 if (!sq)
660
664
667
668 return sq;
669 }
670
672 {
674
675 if (!sq)
676 return;
677
680
682
684 }