FFmpeg
Loading...
Searching...
No Matches
sync_queue.c
Go to the documentation of this file.
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"
25#include "libavutil/cpu.h"
26#include "libavutil/error.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
72typedef struct SyncQueueStream {
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
84 uint64_t frames_sent;
85 uint64_t samples_sent;
86 uint64_t frames_max;
89
90struct SyncQueue {
92
93 void *logctx;
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
107 unsigned int nb_streams;
108
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 */
118static int64_t frame_end(const SyncQueue *sq, SyncQueueFrame frame, int nb_samples)
119{
120 if (nb_samples) {
121 int64_t d = av_rescale_q(nb_samples, (AVRational){ 1, frame.f->sample_rate},
122 frame.f->time_base);
123 return frame.f->pts + d;
124 }
125
126 return (sq->type == SYNC_QUEUE_PACKETS) ?
127 frame.p->pts + frame.p->duration :
128 frame.f->pts + frame.f->duration;
129}
130
132{
133 return (sq->type == SYNC_QUEUE_PACKETS) ? 0 : frame.f->nb_samples;
134}
135
137{
138 return (sq->type == SYNC_QUEUE_PACKETS) ? (frame.p == NULL) : (frame.f == NULL);
139}
140
141static void tb_update(const SyncQueue *sq, SyncQueueStream *st,
142 const SyncQueueFrame frame)
143{
144 AVRational tb = (sq->type == SYNC_QUEUE_PACKETS) ?
145 frame.p->time_base : frame.f->time_base;
146
147 av_assert0(tb.num > 0 && tb.den > 0);
148
149 if (tb.num == st->tb.num && tb.den == st->tb.den)
150 return;
151
152 // timebase should not change after the first frame
154
155 if (st->head_ts != AV_NOPTS_VALUE)
156 st->head_ts = av_rescale_q(st->head_ts, st->tb, tb);
157
158 st->tb = tb;
159}
160
161static void finish_stream(SyncQueue *sq, unsigned int stream_idx)
162{
163 SyncQueueStream *st = &sq->streams[stream_idx];
164
165 if (!st->finished)
167 "sq: finish %u; head ts %s\n", stream_idx,
168 av_ts2timestr(st->head_ts, &st->tb));
169
170 st->finished = 1;
171
172 if (st->limiting && st->head_ts != AV_NOPTS_VALUE) {
173 /* check if this stream is the new finished head */
174 if (sq->head_finished_stream < 0 ||
175 av_compare_ts(st->head_ts, st->tb,
177 sq->streams[sq->head_finished_stream].tb) < 0) {
178 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 st = &sq->streams[sq->head_finished_stream];
184 for (unsigned int i = 0; i < sq->nb_streams; i++) {
185 SyncQueueStream *st1 = &sq->streams[i];
186 if (st != st1 && st1->head_ts != AV_NOPTS_VALUE &&
187 av_compare_ts(st->head_ts, st->tb, st1->head_ts, st1->tb) <= 0) {
188 if (!st1->finished)
190 "sq: finish secondary %u; head ts %s\n", i,
191 av_ts2timestr(st1->head_ts, &st1->tb));
192
193 st1->finished = 1;
194 }
195 }
196 }
197
198 /* mark the whole queue as finished if all streams are finished */
199 for (unsigned int i = 0; i < sq->nb_streams; i++) {
200 if (!sq->streams[i].finished)
201 return;
202 }
203 sq->finished = 1;
204
205 av_log(sq->logctx, AV_LOG_DEBUG, "sq: finish queue\n");
206}
207
209{
211
212 if (sq->head_stream < 0) {
213 unsigned first_limiting = UINT_MAX;
214
215 /* wait for one timestamp in each stream before determining
216 * the queue head */
217 for (unsigned int i = 0; i < sq->nb_streams; i++) {
218 SyncQueueStream *st = &sq->streams[i];
219 if (!st->limiting)
220 continue;
221 if (st->head_ts == AV_NOPTS_VALUE)
222 return;
223 if (first_limiting == UINT_MAX)
224 first_limiting = i;
225 }
226
227 // placeholder value, correct one will be found below
228 av_assert0(first_limiting < UINT_MAX);
229 sq->head_stream = first_limiting;
230 }
231
232 for (unsigned int i = 0; i < sq->nb_streams; i++) {
233 SyncQueueStream *st_head = &sq->streams[sq->head_stream];
234 SyncQueueStream *st_other = &sq->streams[i];
235 if (st_other->limiting && st_other->head_ts != AV_NOPTS_VALUE &&
236 av_compare_ts(st_other->head_ts, st_other->tb,
237 st_head->head_ts, st_head->tb) < 0)
238 sq->head_stream = i;
239 }
240}
241
242/* update this stream's head timestamp */
243static void stream_update_ts(SyncQueue *sq, unsigned int stream_idx, int64_t ts)
244{
245 SyncQueueStream *st = &sq->streams[stream_idx];
246
247 if (ts == AV_NOPTS_VALUE ||
248 (st->head_ts != AV_NOPTS_VALUE && st->head_ts >= ts))
249 return;
250
251 st->head_ts = ts;
252
253 /* if this stream is now ahead of some finished stream, then
254 * this stream is also finished */
255 if (sq->head_finished_stream >= 0 &&
258 ts, st->tb) <= 0)
259 finish_stream(sq, stream_idx);
260
261 /* update the overall head timestamp if it could have changed */
262 if (st->limiting &&
263 (sq->head_stream < 0 || sq->head_stream == stream_idx))
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 */
272static int overflow_heartbeat(SyncQueue *sq, int stream_idx)
273{
274 SyncQueueStream *st;
276 int64_t tail_ts = AV_NOPTS_VALUE;
277
278 /* if no stream specified, pick the one that is most ahead */
279 if (stream_idx < 0) {
281
282 for (int i = 0; i < sq->nb_streams; i++) {
283 st = &sq->streams[i];
284 if (st->head_ts != AV_NOPTS_VALUE &&
285 (ts == AV_NOPTS_VALUE ||
286 av_compare_ts(ts, sq->streams[stream_idx].tb,
287 st->head_ts, st->tb) < 0)) {
288 ts = st->head_ts;
289 stream_idx = i;
290 }
291 }
292 /* no stream has a timestamp yet -> nothing to do */
293 if (stream_idx < 0)
294 return 0;
295 }
296
297 st = &sq->streams[stream_idx];
298
299 /* get the chosen stream's tail timestamp */
300 for (size_t i = 0; tail_ts == AV_NOPTS_VALUE &&
301 av_container_fifo_peek(st->fifo, (void**)&frame, i) >= 0; i++) {
302 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 if (sq->type == SYNC_QUEUE_FRAMES &&
308 frame.f->nb_samples < st->frame_samples)
309 nb_samples = st->frame_samples;
310
311 tail_ts = frame_end(sq, frame, nb_samples);
312 }
313
314 /* overflow triggers when the tail is over specified duration behind the head */
315 if (tail_ts == AV_NOPTS_VALUE || tail_ts >= st->head_ts ||
316 av_rescale_q(st->head_ts - tail_ts, st->tb, AV_TIME_BASE_Q) < sq->buf_size_us)
317 return 0;
318
319 /* signal a fake timestamp for all streams that prevent tail_ts from being output */
320 tail_ts++;
321 for (unsigned int i = 0; i < sq->nb_streams; i++) {
322 const SyncQueueStream *st1 = &sq->streams[i];
323 int64_t ts;
324
325 if (st == st1 || st1->finished ||
326 (st1->head_ts != AV_NOPTS_VALUE &&
327 av_compare_ts(tail_ts, st->tb, st1->head_ts, st1->tb) <= 0))
328 continue;
329
330 ts = av_rescale_q(tail_ts, st->tb, st1->tb);
331 if (st1->head_ts != AV_NOPTS_VALUE)
332 ts = FFMAX(st1->head_ts + 1, ts);
333
334 av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u overflow heardbeat %s -> %s\n",
335 i, av_ts2timestr(st1->head_ts, &st1->tb), av_ts2timestr(ts, &st1->tb));
336
337 stream_update_ts(sq, i, ts);
338 }
339
340 return 1;
341}
342
343int 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 av_assert0(stream_idx < sq->nb_streams);
350 st = &sq->streams[stream_idx];
351
352 if (frame_null(sq, frame)) {
353 av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u EOF\n", stream_idx);
354 finish_stream(sq, stream_idx);
355 return 0;
356 }
357 if (st->finished)
358 return AVERROR_EOF;
359
360 tb_update(sq, st, frame);
361
362 nb_samples = frame_samples(sq, frame);
363 // make sure frame duration is consistent with sample count
364 if (nb_samples) {
365 av_assert0(frame.f->sample_rate > 0);
366 frame.f->duration = av_rescale_q(nb_samples, (AVRational){ 1, frame.f->sample_rate },
367 frame.f->time_base);
368 }
369
370 ts = frame_end(sq, frame, 0);
371
372 av_log(sq->logctx, AV_LOG_DEBUG, "sq: send %u ts %s\n", stream_idx,
373 av_ts2timestr(ts, &st->tb));
374
376 if (ret < 0)
377 return ret;
378
379 stream_update_ts(sq, stream_idx, ts);
380
381 st->samples_queued += nb_samples;
382 st->samples_sent += nb_samples;
383
384 if (st->frame_samples)
385 st->frames_sent = st->samples_sent / st->frame_samples;
386 else
387 st->frames_sent++;
388
389 if (st->frames_sent >= st->frames_max) {
390 av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u frames_max %"PRIu64" reached\n",
391 stream_idx, st->frames_max);
392
393 finish_stream(sq, stream_idx);
394 }
395
396 return 0;
397}
398
399static void offset_audio(AVFrame *f, int nb_samples)
400{
401 const int planar = av_sample_fmt_is_planar(f->format);
402 const int planes = planar ? f->ch_layout.nb_channels : 1;
403 const int bps = av_get_bytes_per_sample(f->format);
404 const int offset = nb_samples * bps * (planar ? 1 : f->ch_layout.nb_channels);
405
406 av_assert0(bps > 0);
407 av_assert0(nb_samples < f->nb_samples);
408
409 for (int i = 0; i < planes; i++) {
410 f->extended_data[i] += offset;
411 if (i < FF_ARRAY_ELEMS(f->data))
412 f->data[i] = f->extended_data[i];
413 }
414 f->linesize[0] -= offset;
415 f->nb_samples -= nb_samples;
416 f->duration = av_rescale_q(f->nb_samples, (AVRational){ 1, f->sample_rate },
417 f->time_base);
418 f->pts += av_rescale_q(nb_samples, (AVRational){ 1, f->sample_rate },
419 f->time_base);
420}
421
422static int frame_is_aligned(const SyncQueue *sq, const AVFrame *frame)
423{
424 // only checks linesize[0], so only works for audio
425 av_assert0(frame->nb_samples > 0);
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 if (!((uintptr_t)frame->data[0] & sq->align_mask) &&
431 !(frame->linesize[0] & sq->align_mask) &&
432 frame->linesize[0] > sq->align_mask)
433 return 1;
434
435 return 0;
436}
437
439 AVFrame *dst, int nb_samples)
440{
442 int ret;
443
444 av_assert0(st->samples_queued >= nb_samples);
445
446 ret = av_container_fifo_peek(st->fifo, (void**)&src, 0);
447 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 if (src.f->nb_samples > nb_samples && frame_is_aligned(sq, src.f)) {
452 ret = av_frame_ref(dst, src.f);
453 if (ret < 0)
454 return ret;
455
456 dst->nb_samples = nb_samples;
457 offset_audio(src.f, nb_samples);
458 st->samples_queued -= nb_samples;
459
460 goto finish;
461 }
462
463 // otherwise allocate a new frame and copy the data
464 ret = av_channel_layout_copy(&dst->ch_layout, &src.f->ch_layout);
465 if (ret < 0)
466 return ret;
467
468 dst->format = src.f->format;
469 dst->nb_samples = nb_samples;
470
472 if (ret < 0)
473 goto fail;
474
476 if (ret < 0)
477 goto fail;
478
479 dst->nb_samples = 0;
480 while (dst->nb_samples < nb_samples) {
481 int to_copy;
482
483 ret = av_container_fifo_peek(st->fifo, (void**)&src, 0);
484 av_assert0(ret >= 0);
485
486 to_copy = FFMIN(nb_samples - dst->nb_samples, src.f->nb_samples);
487
488 av_samples_copy(dst->extended_data, src.f->extended_data, dst->nb_samples,
489 0, to_copy, dst->ch_layout.nb_channels, dst->format);
490
491 if (to_copy < src.f->nb_samples)
492 offset_audio(src.f, to_copy);
493 else
495
496 st->samples_queued -= to_copy;
497
498 dst->nb_samples += to_copy;
499 }
500
501finish:
502 dst->duration = av_rescale_q(nb_samples, (AVRational){ 1, dst->sample_rate },
503 dst->time_base);
504
505 return 0;
506
507fail:
509 return ret;
510}
511
512static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,
514{
515 const SyncQueueStream *st_head = sq->head_stream >= 0 ?
516 &sq->streams[sq->head_stream] : NULL;
517 SyncQueueStream *st;
518
519 av_assert0(stream_idx < sq->nb_streams);
520 st = &sq->streams[stream_idx];
521
523 (st->frame_samples <= st->samples_queued || st->finished)) {
524 int nb_samples = st->frame_samples;
526 int64_t ts, end = 0, cut = 0;
527 int cmp = 1;
528
529 if (st->finished)
530 nb_samples = FFMIN(nb_samples, st->samples_queued);
531
532 av_container_fifo_peek(st->fifo, (void**)&peek, 0);
533 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 if (ts != AV_NOPTS_VALUE && st_head) {
538 cmp = av_compare_ts(ts, st->tb, st_head->head_ts, st_head->tb);
539
540 /* A frame ending after the final queue head is cut there when
541 * the encoder takes any number of samples, so that the stream
542 * does not end up to a frame before the limiting one. */
543 if (cmp > 0 && st_head->finished && !st->frame_samples &&
544 frame_samples(sq, peek)) {
545 AVRational sample_tb = { 1, peek.f->sample_rate };
546
547 end = av_rescale_q(st_head->head_ts, st_head->tb, st->tb);
548 cut = av_rescale_q(st_head->head_ts, st_head->tb, sample_tb) -
549 av_rescale_q(peek.f->pts, st->tb, sample_tb);
550 cut = FFMIN(cut, peek.f->nb_samples);
551 }
552 }
553
554 /* We can release frames that do not end after the queue head.
555 * Frames with no timestamps are just passed through with no conditions.
556 * Frames are also passed through when there are no limiting streams.
557 */
558 if (cmp <= 0 || ts == AV_NOPTS_VALUE || !sq->have_limiting || cut > 0) {
559 if (nb_samples &&
560 (nb_samples != peek.f->nb_samples || !frame_is_aligned(sq, peek.f))) {
561 int ret = receive_samples(sq, st, frame.f, nb_samples);
562 if (ret < 0)
563 return ret;
564 } else {
565 int ret = av_container_fifo_read(st->fifo, SQPTR(sq, frame), 0);
566 av_assert0(ret >= 0);
567
570
571 if (cut > 0) {
572 frame.f->nb_samples = cut;
573 frame.f->duration = end - frame.f->pts;
574 }
575 }
576
578 "sq: receive %u ts %s queue head %d ts %s\n", stream_idx,
579 av_ts2timestr(frame_end(sq, frame, 0), &st->tb),
580 sq->head_stream,
581 st_head ? av_ts2timestr(st_head->head_ts, &st_head->tb) : "N/A");
582
583 return 0;
584 }
585 }
586
587 return (sq->finished || (st->finished && !av_container_fifo_can_read(st->fifo))) ?
588 AVERROR_EOF : AVERROR(EAGAIN);
589}
590
591static int receive_internal(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
592{
593 int nb_eof = 0;
594 int ret;
595
596 /* read a frame for a specific stream */
597 if (stream_idx >= 0) {
598 ret = receive_for_stream(sq, stream_idx, frame);
599 return (ret < 0) ? ret : stream_idx;
600 }
601
602 /* read a frame for any stream with available output */
603 for (unsigned int i = 0; i < sq->nb_streams; i++) {
605 if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) {
606 nb_eof += (ret == AVERROR_EOF);
607 continue;
608 }
609 return (ret < 0) ? ret : i;
610 }
611
612 return (nb_eof == sq->nb_streams) ? AVERROR_EOF : AVERROR(EAGAIN);
613}
614
615int sq_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
616{
617 int ret = receive_internal(sq, stream_idx, frame);
618
619 /* try again if the queue overflowed and triggered a fake heartbeat
620 * for lagging streams */
621 if (ret == AVERROR(EAGAIN) && overflow_heartbeat(sq, stream_idx))
622 ret = receive_internal(sq, stream_idx, frame);
623
624 return ret;
625}
626
627int sq_add_stream(SyncQueue *sq, int limiting)
628{
629 SyncQueueStream *tmp, *st;
630
631 tmp = av_realloc_array(sq->streams, sq->nb_streams + 1, sizeof(*sq->streams));
632 if (!tmp)
633 return AVERROR(ENOMEM);
634 sq->streams = tmp;
635
636 st = &sq->streams[sq->nb_streams];
637 memset(st, 0, sizeof(*st));
638
639 st->fifo = (sq->type == SYNC_QUEUE_FRAMES) ?
641 if (!st->fifo)
642 return AVERROR(ENOMEM);
643
644 /* we set a valid default, so that a pathological stream that never
645 * receives even a real timebase (and no frames) won't stall all other
646 * streams forever; cf. overflow_heartbeat() */
647 st->tb = (AVRational){ 1, 1 };
649 st->frames_max = UINT64_MAX;
650 st->limiting = limiting;
651
652 sq->have_limiting |= limiting;
653
654 return sq->nb_streams++;
655}
656
657void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, uint64_t frames)
658{
659 SyncQueueStream *st;
660
661 av_assert0(stream_idx < sq->nb_streams);
662 st = &sq->streams[stream_idx];
663
664 st->frames_max = frames;
665 if (st->frames_sent >= st->frames_max)
666 finish_stream(sq, stream_idx);
667}
668
669void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx,
670 int frame_samples)
671{
672 SyncQueueStream *st;
673
675 av_assert0(stream_idx < sq->nb_streams);
676 st = &sq->streams[stream_idx];
677
679
680 sq->align_mask = av_cpu_max_align() - 1;
681}
682
683SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us, void *logctx)
684{
685 SyncQueue *sq = av_mallocz(sizeof(*sq));
686
687 if (!sq)
688 return NULL;
689
690 sq->type = type;
691 sq->buf_size_us = buf_size_us;
692 sq->logctx = logctx;
693
694 sq->head_stream = -1;
695 sq->head_finished_stream = -1;
696
697 return sq;
698}
699
701{
702 SyncQueue *sq = *psq;
703
704 if (!sq)
705 return;
706
707 for (unsigned int i = 0; i < sq->nb_streams; i++)
709
710 av_freep(&sq->streams);
711
712 av_freep(psq);
713}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static int frames
static void finish(void)
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
static uint32_t BS_FUNC peek(BSCTX *bc, unsigned int n)
Return n bits from the buffer but do not change the buffer state.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
Public libavutil channel layout APIs header.
int av_container_fifo_write(AVContainerFifo *cf, void *obj, unsigned flags)
Write the contents of obj to the FIFO.
void av_container_fifo_drain(AVContainerFifo *cf, size_t nb_elems)
Discard the specified number of elements from the FIFO.
void av_container_fifo_free(AVContainerFifo **pcf)
Free a AVContainerFifo and everything in it.
int av_container_fifo_peek(AVContainerFifo *cf, void **pdst, size_t offset)
Access objects stored in the FIFO without retrieving them.
int av_container_fifo_read(AVContainerFifo *cf, void *obj, unsigned flags)
Read the next available object from the FIFO into obj.
size_t av_container_fifo_can_read(const AVContainerFifo *cf)
AVContainerFifo * av_container_fifo_alloc_avframe(unsigned flags)
Allocate an AVContainerFifo instance for AVFrames.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVFrame * frame
error code definitions
int ret
Definition ffplay.c:391
static unsigned int nb_streams
Definition ffprobe.c:390
#define fail
Definition test.h:479
AVContainerFifo * av_container_fifo_alloc_avpacket(unsigned flags)
Allocate an AVContainerFifo instance for AVPacket.
Definition packet.c:696
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:499
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition frame.c:209
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition frame.c:281
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:602
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare two timestamps each in its own time base.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:318
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition samplefmt.c:115
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition samplefmt.c:109
int av_samples_copy(uint8_t *const *dst, uint8_t *const *src, int dst_offset, int src_offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Copy samples from src to dst.
Definition samplefmt.c:223
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
enum MPFType type
Definition jpegmpfenc.c:55
unsigned offset
Definition libaomenc.c:763
size_t av_cpu_max_align(void)
Get the maximum data alignment that may be required by FFmpeg.
Definition cpu.c:289
static const struct @257111027162314367033347246032313251342043035002 planes[]
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
static av_always_inline int cmp(MPVEncContext *const s, const int x, const int y, const int subx, const int suby, const int size, const int h, int ref_index, int src_index, me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags)
compares a block (either a full macroblock or a partition thereof) against a proposed motion-compensa...
Definition motion_est.c:263
unsigned bps
Definition movenc.c:2145
#define FF_ARRAY_ELEMS(a)
AVContainerFifo is a FIFO for "containers" - dynamically allocated reusable structs (e....
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
AVRational tb
Definition sync_queue.c:74
AVContainerFifo * fifo
Definition sync_queue.c:73
uint64_t samples_queued
Definition sync_queue.c:77
uint64_t samples_sent
Definition sync_queue.c:85
uint64_t frames_sent
Definition sync_queue.c:84
uint64_t frames_max
Definition sync_queue.c:86
int64_t head_ts
Definition sync_queue.c:79
A sync queue provides timestamp synchronization between multiple streams.
Definition sync_queue.c:90
int have_limiting
Definition sync_queue.c:109
int finished
Definition sync_queue.c:96
void * logctx
Definition sync_queue.c:93
SyncQueueStream * streams
Definition sync_queue.c:106
int head_stream
Definition sync_queue.c:99
enum SyncQueueType type
Definition sync_queue.c:91
uintptr_t align_mask
Definition sync_queue.c:111
int64_t buf_size_us
Definition sync_queue.c:104
unsigned int nb_streams
Definition sync_queue.c:107
int head_finished_stream
Definition sync_queue.c:101
#define SQPTR(sq, frame)
Definition sync_queue.c:69
static int64_t frame_end(const SyncQueue *sq, SyncQueueFrame frame, int nb_samples)
Compute the end timestamp of a frame.
Definition sync_queue.c:118
static int receive_samples(SyncQueue *sq, SyncQueueStream *st, AVFrame *dst, int nb_samples)
Definition sync_queue.c:438
static void queue_head_update(SyncQueue *sq)
Definition sync_queue.c:208
static void offset_audio(AVFrame *f, int nb_samples)
Definition sync_queue.c:399
static void tb_update(const SyncQueue *sq, SyncQueueStream *st, const SyncQueueFrame frame)
Definition sync_queue.c:141
static void finish_stream(SyncQueue *sq, unsigned int stream_idx)
Definition sync_queue.c:161
int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
Submit a frame for the stream with index stream_idx.
Definition sync_queue.c:343
static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
Definition sync_queue.c:512
static int frame_is_aligned(const SyncQueue *sq, const AVFrame *frame)
Definition sync_queue.c:422
static void stream_update_ts(SyncQueue *sq, unsigned int stream_idx, int64_t ts)
Definition sync_queue.c:243
static int frame_samples(const SyncQueue *sq, SyncQueueFrame frame)
Definition sync_queue.c:131
static int frame_null(const SyncQueue *sq, SyncQueueFrame frame)
Definition sync_queue.c:136
void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, uint64_t frames)
Limit the number of output frames for stream with index stream_idx to max_frames.
Definition sync_queue.c:657
int sq_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
Read a frame from the queue.
Definition sync_queue.c:615
static int overflow_heartbeat(SyncQueue *sq, int stream_idx)
Definition sync_queue.c:272
static int receive_internal(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
Definition sync_queue.c:591
void sq_free(SyncQueue **psq)
Definition sync_queue.c:700
void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx, int frame_samples)
Set a constant output audio frame size, in samples.
Definition sync_queue.c:669
int sq_add_stream(SyncQueue *sq, int limiting)
Add a new stream to the sync queue.
Definition sync_queue.c:627
SyncQueue * sq_alloc(enum SyncQueueType type, int64_t buf_size_us, void *logctx)
Allocate a sync queue of the given type.
Definition sync_queue.c:683
SyncQueueType
Definition sync_queue.h:28
@ SYNC_QUEUE_FRAMES
Definition sync_queue.h:30
@ SYNC_QUEUE_PACKETS
Definition sync_queue.h:29
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
timestamp utils, mostly useful for debugging/logging purposes
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition timestamp.h:83