FFmpeg
Loading...
Searching...
No Matches
ffmpeg_sched.c
Go to the documentation of this file.
1/*
2 * Inter-thread scheduling/synchronization.
3 * Copyright (c) 2023 Anton Khirnov
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdatomic.h>
23#include <stddef.h>
24#include <stdint.h>
25
26#include "cmdutils.h"
27#include "ffmpeg_sched.h"
28#include "ffmpeg_utils.h"
29#include "sync_queue.h"
30#include "thread_queue.h"
31
32#include "libavcodec/packet.h"
33
34#include "libavutil/avassert.h"
35#include "libavutil/error.h"
36#include "libavutil/fifo.h"
37#include "libavutil/frame.h"
38#include "libavutil/mem.h"
39#include "libavutil/thread.h"
41#include "libavutil/time.h"
42
43// 100 ms
44// FIXME: some other value? make this dynamic?
45#define SCHEDULE_TOLERANCE (100 * 1000)
46
51
52typedef struct SchWaiter {
56
57 // the following are internal state of schedule_update_locked() and must not
58 // be accessed outside of it
61} SchWaiter;
62
73
74typedef struct SchDecOutput {
76 uint8_t *dst_finished;
77 unsigned nb_dst;
79
80typedef struct SchDec {
81 const AVClass *class;
82
84
86 unsigned nb_outputs;
87
89 // Queue for receiving input packets, one stream.
91
92 // Queue for sending post-flush end timestamps back to the source
95
96 // temporary storage used by sch_dec_send()
98} SchDec;
99
108
109typedef struct SchEnc {
110 const AVClass *class;
111
114 uint8_t *dst_finished;
115 unsigned nb_dst;
116
117 // [0] - index of the sync queue in Scheduler.sq_enc,
118 // [1] - index of this encoder in the sq
119 int sq_idx[2];
120
121 /* Opening encoders is somewhat nontrivial due to their interaction with
122 * sync queues, which are (among other things) responsible for maintaining
123 * constant audio frame size, when it is required by the encoder.
124 *
125 * Opening the encoder requires stream parameters, obtained from the first
126 * frame. However, that frame cannot be properly chunked by the sync queue
127 * without knowing the required frame size, which is only available after
128 * opening the encoder.
129 *
130 * This apparent circular dependency is resolved in the following way:
131 * - the caller creating the encoder gives us a callback which opens the
132 * encoder and returns the required frame size (if any)
133 * - when the first frame is sent to the encoder, the sending thread
134 * - calls this callback, opening the encoder
135 * - passes the returned frame size to the sync queue
136 */
137 int (*open_cb)(void *opaque, const AVFrame *frame);
139
141 // Queue for receiving input frames, one stream.
143 // tq_send() to queue returned EOF
145
146 // temporary storage used by sch_enc_send()
148} SchEnc;
149
155
156typedef struct SchDemux {
157 const AVClass *class;
158
160 unsigned nb_streams;
161
164
165 // temporary storage used by sch_demux_send()
167
168 // protected by schedule_lock
170} SchDemux;
171
172typedef struct PreMuxQueue {
173 /**
174 * Queue for buffering the packets before the muxer task can be started.
175 */
177 /**
178 * Maximum number of packets in fifo.
179 */
181 /*
182 * The size of the AVPackets' buffers in queue.
183 * Updated when a packet is either pushed or pulled from the queue.
184 */
185 size_t data_size;
186 /* Threshold after which max_packets will be in effect */
189
190typedef struct SchMuxStream {
192
195
197
198 // an EOF was generated while flushing the pre-mux queue
200
201 // offset (in AV_TIME_BASE_Q) subtracted by the upstream pipeline
202 // (e.g. filter ts_offset), added back when computing last_dts_paced
203 // so that cross-stream pacing compares on a common source timeline
205
206 ////////////////////////////////////////////////////////////
207 // The following are protected by Scheduler.schedule_lock //
208
209 /* dts+duration of the last packet sent to this stream
210 in AV_TIME_BASE_Q, as observed by the muxer (no offset applied);
211 feeds progressing_dts() -> sch->last_dts -> CLI progress display */
213 /* last_dts + dts_pacing_offset, kept on a common source timeline
214 across streams; consumed by trailing_dts() and the choke check
215 in schedule_update_locked() */
217 // this stream no longer accepts input
219 ////////////////////////////////////////////////////////////
221
222typedef struct SchMux {
223 const AVClass *class;
224
226 unsigned nb_streams;
228
229 int (*init)(void *arg);
230
232 /**
233 * Set to 1 after starting the muxer task and flushing the
234 * pre-muxing queues.
235 * Set either before any tasks have started, or with
236 * Scheduler.mux_ready_lock held.
237 */
240 unsigned queue_size;
241
243} SchMux;
244
250
254
255typedef struct SchFilterGraph {
256 const AVClass *class;
257
259 unsigned nb_inputs;
262
264 unsigned nb_outputs;
265
267 // input queue, nb_inputs+1 streams
268 // last stream is control
271
272 // protected by schedule_lock
273 unsigned best_input;
276
282
323
324/**
325 * Wait until this task is allowed to proceed.
326 *
327 * @retval 0 the caller should proceed
328 * @retval 1 the caller should terminate
329 */
331{
332 int terminate;
333
334 if (!atomic_load(&w->choked))
335 return 0;
336
337 pthread_mutex_lock(&w->lock);
338
339 while (atomic_load(&w->choked) && !atomic_load(&sch->terminate))
340 pthread_cond_wait(&w->cond, &w->lock);
341
342 terminate = atomic_load(&sch->terminate);
343
344 pthread_mutex_unlock(&w->lock);
345
346 return terminate;
347}
348
349static void waiter_set(SchWaiter *w, int choked)
350{
351 pthread_mutex_lock(&w->lock);
352
353 atomic_store(&w->choked, choked);
354 pthread_cond_signal(&w->cond);
355
356 pthread_mutex_unlock(&w->lock);
357}
358
360{
361 int ret;
362
363 atomic_init(&w->choked, 0);
364
365 ret = pthread_mutex_init(&w->lock, NULL);
366 if (ret)
367 return AVERROR(ret);
368
369 ret = pthread_cond_init(&w->cond, NULL);
370 if (ret)
371 return AVERROR(ret);
372
373 return 0;
374}
375
377{
378 pthread_mutex_destroy(&w->lock);
379 pthread_cond_destroy(&w->cond);
380}
381
382static int queue_alloc(ThreadQueue **ptq, unsigned nb_streams, unsigned queue_size,
383 enum QueueType type)
384{
385 ThreadQueue *tq;
386
387 if (queue_size <= 0) {
388 if (type == QUEUE_FRAMES)
390 else
392 }
393
394 if (type == QUEUE_FRAMES) {
395 // This queue length is used in the decoder code to ensure that
396 // there are enough entries in fixed-size frame pools to account
397 // for frames held in queues inside the ffmpeg utility. If this
398 // can ever dynamically change then the corresponding decode
399 // code needs to be updated as well.
401 }
402
403 tq = tq_alloc(nb_streams, queue_size,
405 if (!tq)
406 return AVERROR(ENOMEM);
407
408 *ptq = tq;
409 return 0;
410}
411
412static void *task_wrapper(void *arg);
413
414static int task_start(SchTask *task)
415{
416 int ret;
417
418 if (!task->parent)
419 return 0;
420
421 av_log(task->func_arg, AV_LOG_VERBOSE, "Starting thread...\n");
422
424
425 ret = pthread_create(&task->thread, NULL, task_wrapper, task);
426 if (ret) {
427 av_log(task->func_arg, AV_LOG_ERROR, "pthread_create() failed: %s\n",
428 strerror(ret));
429 return AVERROR(ret);
430 }
431
432 task->thread_running = 1;
433 return 0;
434}
435
436static void task_init(Scheduler *sch, SchTask *task, enum SchedulerNodeType type, unsigned idx,
437 SchThreadFunc func, void *func_arg)
438{
439 task->parent = sch;
440
441 task->node.type = type;
442 task->node.idx = idx;
443
444 task->func = func;
445 task->func_arg = func_arg;
446}
447
449{
450 int64_t min_dts = INT64_MAX;
451
452 for (unsigned i = 0; i < sch->nb_mux; i++) {
453 const SchMux *mux = &sch->mux[i];
454
455 for (unsigned j = 0; j < mux->nb_streams; j++) {
456 const SchMuxStream *ms = &mux->streams[j];
457
458 if (ms->source_finished)
459 continue;
461 return AV_NOPTS_VALUE;
462
463 min_dts = FFMIN(min_dts, ms->last_dts_paced);
464 }
465 }
466
467 return min_dts == INT64_MAX ? AV_NOPTS_VALUE : min_dts;
468}
469
470static int64_t progressing_dts(const Scheduler *sch, int count_finished)
471{
472 int64_t max_dts = INT64_MIN;
473
474 for (unsigned i = 0; i < sch->nb_mux; i++) {
475 const SchMux *mux = &sch->mux[i];
476
477 for (unsigned j = 0; j < mux->nb_streams; j++) {
478 const SchMuxStream *ms = &mux->streams[j];
479
480 if (ms->source_finished && !count_finished)
481 continue;
482 if (ms->last_dts != AV_NOPTS_VALUE)
483 max_dts = FFMAX(max_dts, ms->last_dts);
484 }
485 }
486
487 return max_dts == INT64_MIN ? AV_NOPTS_VALUE : max_dts;
488}
489
491{
492 SchFilterGraph *fg = &sch->filters[idx];
493
495 memset(&fg->task, 0, sizeof(fg->task));
496
497 tq_free(&fg->queue);
498
499 av_freep(&fg->inputs);
500 fg->nb_inputs = 0;
501 av_freep(&fg->outputs);
502 fg->nb_outputs = 0;
503
504 fg->task_exited = 1;
505}
506
507void sch_free(Scheduler **psch)
508{
509 Scheduler *sch = *psch;
510
511 if (!sch)
512 return;
513
514 sch_stop(sch, NULL);
515
516 for (unsigned i = 0; i < sch->nb_demux; i++) {
517 SchDemux *d = &sch->demux[i];
518
519 for (unsigned j = 0; j < d->nb_streams; j++) {
520 SchDemuxStream *ds = &d->streams[j];
521 av_freep(&ds->dst);
523 }
524 av_freep(&d->streams);
525
527
529 }
530 av_freep(&sch->demux);
531
532 for (unsigned i = 0; i < sch->nb_mux; i++) {
533 SchMux *mux = &sch->mux[i];
534
535 for (unsigned j = 0; j < mux->nb_streams; j++) {
536 SchMuxStream *ms = &mux->streams[j];
537
538 if (ms->pre_mux_queue.fifo) {
539 AVPacket *pkt;
540 while (av_fifo_read(ms->pre_mux_queue.fifo, &pkt, 1) >= 0)
543 }
544
546 }
547 av_freep(&mux->streams);
548
550
551 tq_free(&mux->queue);
552 }
553 av_freep(&sch->mux);
554
555 for (unsigned i = 0; i < sch->nb_dec; i++) {
556 SchDec *dec = &sch->dec[i];
557
558 tq_free(&dec->queue);
559
561
562 for (unsigned j = 0; j < dec->nb_outputs; j++) {
563 SchDecOutput *o = &dec->outputs[j];
564
565 av_freep(&o->dst);
567 }
568
569 av_freep(&dec->outputs);
570
572 }
573 av_freep(&sch->dec);
574
575 for (unsigned i = 0; i < sch->nb_enc; i++) {
576 SchEnc *enc = &sch->enc[i];
577
578 tq_free(&enc->queue);
579
581
582 av_freep(&enc->dst);
583 av_freep(&enc->dst_finished);
584 }
585 av_freep(&sch->enc);
586
587 for (unsigned i = 0; i < sch->nb_sq_enc; i++) {
588 SchSyncQueue *sq = &sch->sq_enc[i];
589 sq_free(&sq->sq);
590 av_frame_free(&sq->frame);
592 av_freep(&sq->enc_idx);
593 }
594 av_freep(&sch->sq_enc);
595
596 for (unsigned i = 0; i < sch->nb_filters; i++) {
597 SchFilterGraph *fg = &sch->filters[i];
598
599 tq_free(&fg->queue);
600
601 av_freep(&fg->inputs);
602 av_freep(&fg->outputs);
603
604 waiter_uninit(&fg->waiter);
605 }
606 av_freep(&sch->filters);
607
608 av_freep(&sch->sdp_filename);
609
611
613
616
617 av_freep(psch);
618}
619
620static const AVClass scheduler_class = {
621 .class_name = "Scheduler",
622 .version = LIBAVUTIL_VERSION_INT,
623};
624
626{
627 Scheduler *sch;
628 int ret;
629
630 sch = av_mallocz(sizeof(*sch));
631 if (!sch)
632 return NULL;
633
634 sch->class = &scheduler_class;
635 sch->sdp_auto = 1;
636
638 if (ret)
639 goto fail;
640
642 if (ret)
643 goto fail;
644
646 if (ret)
647 goto fail;
648
650 if (ret)
651 goto fail;
652
653 return sch;
654fail:
655 sch_free(&sch);
656 return NULL;
657}
658
659int sch_sdp_filename(Scheduler *sch, const char *sdp_filename)
660{
661 av_freep(&sch->sdp_filename);
662 sch->sdp_filename = av_strdup(sdp_filename);
663 return sch->sdp_filename ? 0 : AVERROR(ENOMEM);
664}
665
666static const AVClass sch_mux_class = {
667 .class_name = "SchMux",
668 .version = LIBAVUTIL_VERSION_INT,
669 .parent_log_context_offset = offsetof(SchMux, task.func_arg),
670};
671
672int sch_add_mux(Scheduler *sch, SchThreadFunc func, int (*init)(void *),
673 void *arg, int sdp_auto, unsigned thread_queue_size)
674{
675 const unsigned idx = sch->nb_mux;
676
677 SchMux *mux;
678 int ret;
679
680 ret = GROW_ARRAY(sch->mux, sch->nb_mux);
681 if (ret < 0)
682 return ret;
683
684 mux = &sch->mux[idx];
685 mux->class = &sch_mux_class;
686 mux->init = init;
687 mux->queue_size = thread_queue_size;
688
689 task_init(sch, &mux->task, SCH_NODE_TYPE_MUX, idx, func, arg);
690
691 sch->sdp_auto &= sdp_auto;
692
693 return idx;
694}
695
696int sch_add_mux_stream(Scheduler *sch, unsigned mux_idx)
697{
698 SchMux *mux;
699 SchMuxStream *ms;
700 unsigned stream_idx;
701 int ret;
702
703 av_assert0(mux_idx < sch->nb_mux);
704 mux = &sch->mux[mux_idx];
705
706 ret = GROW_ARRAY(mux->streams, mux->nb_streams);
707 if (ret < 0)
708 return ret;
709 stream_idx = mux->nb_streams - 1;
710
711 ms = &mux->streams[stream_idx];
712
713 ms->pre_mux_queue.fifo = av_fifo_alloc2(8, sizeof(AVPacket*), 0);
714 if (!ms->pre_mux_queue.fifo)
715 return AVERROR(ENOMEM);
716
719
720 return stream_idx;
721}
722
723static const AVClass sch_demux_class = {
724 .class_name = "SchDemux",
725 .version = LIBAVUTIL_VERSION_INT,
726 .parent_log_context_offset = offsetof(SchDemux, task.func_arg),
727};
728
730{
731 const unsigned idx = sch->nb_demux;
732
733 SchDemux *d;
734 int ret;
735
736 ret = GROW_ARRAY(sch->demux, sch->nb_demux);
737 if (ret < 0)
738 return ret;
739
740 d = &sch->demux[idx];
741
742 task_init(sch, &d->task, SCH_NODE_TYPE_DEMUX, idx, func, ctx);
743
746 if (!d->send_pkt)
747 return AVERROR(ENOMEM);
748
749 ret = waiter_init(&d->waiter);
750 if (ret < 0)
751 return ret;
752
753 return idx;
754}
755
756int sch_add_demux_stream(Scheduler *sch, unsigned demux_idx)
757{
758 SchDemux *d;
759 int ret;
760
761 av_assert0(demux_idx < sch->nb_demux);
762 d = &sch->demux[demux_idx];
763
765 return ret < 0 ? ret : d->nb_streams - 1;
766}
767
768int sch_add_dec_output(Scheduler *sch, unsigned dec_idx)
769{
770 SchDec *dec;
771 int ret;
772
773 av_assert0(dec_idx < sch->nb_dec);
774 dec = &sch->dec[dec_idx];
775
776 ret = GROW_ARRAY(dec->outputs, dec->nb_outputs);
777 if (ret < 0)
778 return ret;
779
780 return dec->nb_outputs - 1;
781}
782
783static const AVClass sch_dec_class = {
784 .class_name = "SchDec",
785 .version = LIBAVUTIL_VERSION_INT,
786 .parent_log_context_offset = offsetof(SchDec, task.func_arg),
787};
788
789int sch_add_dec(Scheduler *sch, SchThreadFunc func, void *ctx, int send_end_ts)
790{
791 const unsigned idx = sch->nb_dec;
792
793 SchDec *dec;
794 int ret;
795
796 ret = GROW_ARRAY(sch->dec, sch->nb_dec);
797 if (ret < 0)
798 return ret;
799
800 dec = &sch->dec[idx];
801
802 task_init(sch, &dec->task, SCH_NODE_TYPE_DEC, idx, func, ctx);
803
804 dec->class = &sch_dec_class;
805 dec->send_frame = av_frame_alloc();
806 if (!dec->send_frame)
807 return AVERROR(ENOMEM);
808
809 ret = sch_add_dec_output(sch, idx);
810 if (ret < 0)
811 return ret;
812
813 ret = queue_alloc(&dec->queue, 1, 0, QUEUE_PACKETS);
814 if (ret < 0)
815 return ret;
816
817 if (send_end_ts) {
819 if (ret < 0)
820 return ret;
821 }
822
823 return idx;
824}
825
826static const AVClass sch_enc_class = {
827 .class_name = "SchEnc",
828 .version = LIBAVUTIL_VERSION_INT,
829 .parent_log_context_offset = offsetof(SchEnc, task.func_arg),
830};
831
833 int (*open_cb)(void *opaque, const AVFrame *frame))
834{
835 const unsigned idx = sch->nb_enc;
836
837 SchEnc *enc;
838 int ret;
839
840 ret = GROW_ARRAY(sch->enc, sch->nb_enc);
841 if (ret < 0)
842 return ret;
843
844 enc = &sch->enc[idx];
845
846 enc->class = &sch_enc_class;
847 enc->open_cb = open_cb;
848 enc->sq_idx[0] = -1;
849 enc->sq_idx[1] = -1;
850
851 task_init(sch, &enc->task, SCH_NODE_TYPE_ENC, idx, func, ctx);
852
853 enc->send_pkt = av_packet_alloc();
854 if (!enc->send_pkt)
855 return AVERROR(ENOMEM);
856
857 ret = queue_alloc(&enc->queue, 1, 0, QUEUE_FRAMES);
858 if (ret < 0)
859 return ret;
860
861 return idx;
862}
863
864static const AVClass sch_fg_class = {
865 .class_name = "SchFilterGraph",
866 .version = LIBAVUTIL_VERSION_INT,
867 .parent_log_context_offset = offsetof(SchFilterGraph, task.func_arg),
868};
869
870int sch_add_filtergraph(Scheduler *sch, unsigned nb_inputs, unsigned nb_outputs,
871 SchThreadFunc func, void *ctx)
872{
873 const unsigned idx = sch->nb_filters;
874
875 SchFilterGraph *fg;
876 int ret;
877
878 ret = GROW_ARRAY(sch->filters, sch->nb_filters);
879 if (ret < 0)
880 return ret;
881 fg = &sch->filters[idx];
882
883 fg->class = &sch_fg_class;
884
885 task_init(sch, &fg->task, SCH_NODE_TYPE_FILTER_IN, idx, func, ctx);
886
887 if (nb_inputs) {
888 fg->inputs = av_calloc(nb_inputs, sizeof(*fg->inputs));
889 if (!fg->inputs)
890 return AVERROR(ENOMEM);
891 fg->nb_inputs = nb_inputs;
892 }
893
894 if (nb_outputs) {
895 fg->outputs = av_calloc(nb_outputs, sizeof(*fg->outputs));
896 if (!fg->outputs)
897 return AVERROR(ENOMEM);
898 fg->nb_outputs = nb_outputs;
899 }
900
901 ret = waiter_init(&fg->waiter);
902 if (ret < 0)
903 return ret;
904
905 ret = queue_alloc(&fg->queue, fg->nb_inputs + 1, 0, QUEUE_FRAMES);
906 if (ret < 0)
907 return ret;
908
909 return idx;
910}
911
912int sch_add_sq_enc(Scheduler *sch, uint64_t buf_size_us, void *logctx)
913{
914 SchSyncQueue *sq;
915 int ret;
916
917 ret = GROW_ARRAY(sch->sq_enc, sch->nb_sq_enc);
918 if (ret < 0)
919 return ret;
920 sq = &sch->sq_enc[sch->nb_sq_enc - 1];
921
922 sq->sq = sq_alloc(SYNC_QUEUE_FRAMES, buf_size_us, logctx);
923 if (!sq->sq)
924 return AVERROR(ENOMEM);
925
926 sq->frame = av_frame_alloc();
927 if (!sq->frame)
928 return AVERROR(ENOMEM);
929
931 if (ret)
932 return AVERROR(ret);
933
934 return sq - sch->sq_enc;
935}
936
937int sch_sq_add_enc(Scheduler *sch, unsigned sq_idx, unsigned enc_idx,
938 int limiting, uint64_t max_frames)
939{
940 SchSyncQueue *sq;
941 SchEnc *enc;
942 int ret;
943
944 av_assert0(sq_idx < sch->nb_sq_enc);
945 sq = &sch->sq_enc[sq_idx];
946
947 av_assert0(enc_idx < sch->nb_enc);
948 enc = &sch->enc[enc_idx];
949
950 ret = GROW_ARRAY(sq->enc_idx, sq->nb_enc_idx);
951 if (ret < 0)
952 return ret;
953 sq->enc_idx[sq->nb_enc_idx - 1] = enc_idx;
954
955 ret = sq_add_stream(sq->sq, limiting);
956 if (ret < 0)
957 return ret;
958
959 enc->sq_idx[0] = sq_idx;
960 enc->sq_idx[1] = ret;
961
962 if (max_frames != INT64_MAX)
963 sq_limit_frames(sq->sq, enc->sq_idx[1], max_frames);
964
965 return 0;
966}
967
969{
970 int ret;
971
972 switch (src.type) {
973 case SCH_NODE_TYPE_DEMUX: {
974 SchDemuxStream *ds;
975
976 av_assert0(src.idx < sch->nb_demux &&
977 src.idx_stream < sch->demux[src.idx].nb_streams);
978 ds = &sch->demux[src.idx].streams[src.idx_stream];
979
980 ret = GROW_ARRAY(ds->dst, ds->nb_dst);
981 if (ret < 0)
982 return ret;
983
984 ds->dst[ds->nb_dst - 1] = dst;
985
986 // demuxed packets go to decoding or streamcopy
987 switch (dst.type) {
988 case SCH_NODE_TYPE_DEC: {
989 SchDec *dec;
990
991 av_assert0(dst.idx < sch->nb_dec);
992 dec = &sch->dec[dst.idx];
993
994 av_assert0(!dec->src.type);
995 dec->src = src;
996 break;
997 }
998 case SCH_NODE_TYPE_MUX: {
999 SchMuxStream *ms;
1000
1001 av_assert0(dst.idx < sch->nb_mux &&
1002 dst.idx_stream < sch->mux[dst.idx].nb_streams);
1003 ms = &sch->mux[dst.idx].streams[dst.idx_stream];
1004
1005 av_assert0(!ms->src.type);
1006 ms->src = src;
1007
1008 break;
1009 }
1010 default: av_assert0(0);
1011 }
1012
1013 break;
1014 }
1015 case SCH_NODE_TYPE_DEC: {
1016 SchDec *dec;
1017 SchDecOutput *o;
1018
1019 av_assert0(src.idx < sch->nb_dec);
1020 dec = &sch->dec[src.idx];
1021
1022 av_assert0(src.idx_stream < dec->nb_outputs);
1023 o = &dec->outputs[src.idx_stream];
1024
1025 ret = GROW_ARRAY(o->dst, o->nb_dst);
1026 if (ret < 0)
1027 return ret;
1028
1029 o->dst[o->nb_dst - 1] = dst;
1030
1031 // decoded frames go to filters or encoding
1032 switch (dst.type) {
1034 SchFilterIn *fi;
1035
1036 av_assert0(dst.idx < sch->nb_filters &&
1037 dst.idx_stream < sch->filters[dst.idx].nb_inputs);
1038 fi = &sch->filters[dst.idx].inputs[dst.idx_stream];
1039
1040 av_assert0(!fi->src.type);
1041 fi->src = src;
1042 break;
1043 }
1044 case SCH_NODE_TYPE_ENC: {
1045 SchEnc *enc;
1046
1047 av_assert0(dst.idx < sch->nb_enc);
1048 enc = &sch->enc[dst.idx];
1049
1050 av_assert0(!enc->src.type);
1051 enc->src = src;
1052 break;
1053 }
1054 default: av_assert0(0);
1055 }
1056
1057 break;
1058 }
1060 SchFilterOut *fo;
1061
1062 av_assert0(src.idx < sch->nb_filters &&
1063 src.idx_stream < sch->filters[src.idx].nb_outputs);
1064 fo = &sch->filters[src.idx].outputs[src.idx_stream];
1065
1066 av_assert0(!fo->dst.type);
1067 fo->dst = dst;
1068
1069 // filtered frames go to encoding or another filtergraph
1070 switch (dst.type) {
1071 case SCH_NODE_TYPE_ENC: {
1072 SchEnc *enc;
1073
1074 av_assert0(dst.idx < sch->nb_enc);
1075 enc = &sch->enc[dst.idx];
1076
1077 av_assert0(!enc->src.type);
1078 enc->src = src;
1079 break;
1080 }
1082 SchFilterIn *fi;
1083
1084 av_assert0(dst.idx < sch->nb_filters &&
1085 dst.idx_stream < sch->filters[dst.idx].nb_inputs);
1086 fi = &sch->filters[dst.idx].inputs[dst.idx_stream];
1087
1088 av_assert0(!fi->src.type);
1089 fi->src = src;
1090 break;
1091 }
1092 default: av_assert0(0);
1093 }
1094
1095
1096 break;
1097 }
1098 case SCH_NODE_TYPE_ENC: {
1099 SchEnc *enc;
1100
1101 av_assert0(src.idx < sch->nb_enc);
1102 enc = &sch->enc[src.idx];
1103
1104 ret = GROW_ARRAY(enc->dst, enc->nb_dst);
1105 if (ret < 0)
1106 return ret;
1107
1108 enc->dst[enc->nb_dst - 1] = dst;
1109
1110 // encoding packets go to muxing or decoding
1111 switch (dst.type) {
1112 case SCH_NODE_TYPE_MUX: {
1113 SchMuxStream *ms;
1114
1115 av_assert0(dst.idx < sch->nb_mux &&
1116 dst.idx_stream < sch->mux[dst.idx].nb_streams);
1117 ms = &sch->mux[dst.idx].streams[dst.idx_stream];
1118
1119 av_assert0(!ms->src.type);
1120 ms->src = src;
1121
1122 break;
1123 }
1124 case SCH_NODE_TYPE_DEC: {
1125 SchDec *dec;
1126
1127 av_assert0(dst.idx < sch->nb_dec);
1128 dec = &sch->dec[dst.idx];
1129
1130 av_assert0(!dec->src.type);
1131 dec->src = src;
1132
1133 break;
1134 }
1135 default: av_assert0(0);
1136 }
1137
1138 break;
1139 }
1140 default: av_assert0(0);
1141 }
1142
1143 return 0;
1144}
1145
1146static int mux_task_start(SchMux *mux)
1147{
1148 int ret = 0;
1149
1150 ret = task_start(&mux->task);
1151 if (ret < 0)
1152 return ret;
1153
1154 /* flush the pre-muxing queues */
1155 while (1) {
1156 int min_stream = -1;
1157 Timestamp min_ts = { .ts = AV_NOPTS_VALUE };
1158
1159 AVPacket *pkt;
1160
1161 // find the stream with the earliest dts or EOF in pre-muxing queue
1162 for (unsigned i = 0; i < mux->nb_streams; i++) {
1163 SchMuxStream *ms = &mux->streams[i];
1164
1165 if (av_fifo_peek(ms->pre_mux_queue.fifo, &pkt, 1, 0) < 0)
1166 continue;
1167
1168 if (!pkt || pkt->dts == AV_NOPTS_VALUE) {
1169 min_stream = i;
1170 break;
1171 }
1172
1173 if (min_ts.ts == AV_NOPTS_VALUE ||
1174 av_compare_ts(min_ts.ts, min_ts.tb, pkt->dts, pkt->time_base) > 0) {
1175 min_stream = i;
1176 min_ts = (Timestamp){ .ts = pkt->dts, .tb = pkt->time_base };
1177 }
1178 }
1179
1180 if (min_stream >= 0) {
1181 SchMuxStream *ms = &mux->streams[min_stream];
1182
1184 av_assert0(ret >= 0);
1185
1186 if (pkt) {
1187 if (!ms->init_eof)
1188 ret = tq_send(mux->queue, min_stream, pkt);
1190 if (ret == AVERROR_EOF)
1191 ms->init_eof = 1;
1192 else if (ret < 0)
1193 return ret;
1194 } else
1195 tq_send_finish(mux->queue, min_stream);
1196
1197 continue;
1198 }
1199
1200 break;
1201 }
1202
1203 atomic_store(&mux->mux_started, 1);
1204
1205 return 0;
1206}
1207
1208int print_sdp(const char *filename);
1209
1210static int mux_init(Scheduler *sch, SchMux *mux)
1211{
1212 int ret;
1213
1214 ret = mux->init(mux->task.func_arg);
1215 if (ret < 0)
1216 return ret;
1217
1218 sch->nb_mux_ready++;
1219
1220 if (sch->sdp_filename || sch->sdp_auto) {
1221 if (sch->nb_mux_ready < sch->nb_mux)
1222 return 0;
1223
1224 ret = print_sdp(sch->sdp_filename);
1225 if (ret < 0) {
1226 av_log(sch, AV_LOG_ERROR, "Error writing the SDP.\n");
1227 return ret;
1228 }
1229
1230 /* SDP is written only after all the muxers are ready, so now we
1231 * start ALL the threads */
1232 for (unsigned i = 0; i < sch->nb_mux; i++) {
1233 ret = mux_task_start(&sch->mux[i]);
1234 if (ret < 0)
1235 return ret;
1236 }
1237 } else {
1238 ret = mux_task_start(mux);
1239 if (ret < 0)
1240 return ret;
1241 }
1242
1243 return 0;
1244}
1245
1246void sch_mux_stream_buffering(Scheduler *sch, unsigned mux_idx, unsigned stream_idx,
1247 size_t data_threshold, int max_packets)
1248{
1249 SchMux *mux;
1250 SchMuxStream *ms;
1251
1252 av_assert0(mux_idx < sch->nb_mux);
1253 mux = &sch->mux[mux_idx];
1254
1255 av_assert0(stream_idx < mux->nb_streams);
1256 ms = &mux->streams[stream_idx];
1257
1258 ms->pre_mux_queue.max_packets = max_packets;
1259 ms->pre_mux_queue.data_threshold = data_threshold;
1260}
1261
1263 unsigned stream_idx, int64_t offset)
1264{
1265 SchMux *mux;
1266 SchMuxStream *ms;
1267
1268 av_assert0(mux_idx < sch->nb_mux);
1269 mux = &sch->mux[mux_idx];
1270
1271 av_assert0(stream_idx < mux->nb_streams);
1272 ms = &mux->streams[stream_idx];
1273
1275}
1276
1277int sch_mux_stream_ready(Scheduler *sch, unsigned mux_idx, unsigned stream_idx)
1278{
1279 SchMux *mux;
1280 int ret = 0;
1281
1282 av_assert0(mux_idx < sch->nb_mux);
1283 mux = &sch->mux[mux_idx];
1284
1285 av_assert0(stream_idx < mux->nb_streams);
1286
1288
1290
1291 // this may be called during initialization - do not start
1292 // threads before sch_start() is called
1293 if (++mux->nb_streams_ready == mux->nb_streams &&
1294 sch->state >= SCH_STATE_STARTED)
1295 ret = mux_init(sch, mux);
1296
1298
1299 return ret;
1300}
1301
1302int sch_mux_sub_heartbeat_add(Scheduler *sch, unsigned mux_idx, unsigned stream_idx,
1303 unsigned dec_idx)
1304{
1305 SchMux *mux;
1306 SchMuxStream *ms;
1307 int ret = 0;
1308
1309 av_assert0(mux_idx < sch->nb_mux);
1310 mux = &sch->mux[mux_idx];
1311
1312 av_assert0(stream_idx < mux->nb_streams);
1313 ms = &mux->streams[stream_idx];
1314
1316 if (ret < 0)
1317 return ret;
1318
1319 av_assert0(dec_idx < sch->nb_dec);
1320 ms->sub_heartbeat_dst[ms->nb_sub_heartbeat_dst - 1] = dec_idx;
1321
1322 if (!mux->sub_heartbeat_pkt) {
1324 if (!mux->sub_heartbeat_pkt)
1325 return AVERROR(ENOMEM);
1326 }
1327
1328 return 0;
1329}
1330
1331enum {
1332 UNCHOKE_DEMUX = (1 << 0),
1333 UNCHOKE_FILTER = (1 << 1),
1334
1336};
1337
1338static void unchoke_for_stream(Scheduler *sch, SchedulerNode src, int flags);
1339
1340// Unchoke any filter graphs that are downstream of this node, to prevent it
1341// from getting stuck trying to push data to a full queue
1343{
1344 SchFilterGraph *fg;
1345 SchDec *dec;
1346 SchEnc *enc;
1347 switch (dst->type) {
1348 case SCH_NODE_TYPE_DEC:
1349 dec = &sch->dec[dst->idx];
1350 for (int i = 0; i < dec->nb_outputs; i++)
1351 unchoke_downstream(sch, dec->outputs[i].dst);
1352 break;
1353 case SCH_NODE_TYPE_ENC:
1354 enc = &sch->enc[dst->idx];
1355 for (int i = 0; i < enc->nb_dst; i++)
1356 unchoke_downstream(sch, &enc->dst[i]);
1357 break;
1358 case SCH_NODE_TYPE_MUX:
1359 // muxers are never choked
1360 break;
1362 fg = &sch->filters[dst->idx];
1363 if (fg->best_input == fg->nb_inputs) {
1364 fg->waiter.choked_next = 0;
1365 } else {
1366 // ensure that this filter graph is not stuck waiting for
1367 // input from a different upstream source
1369 }
1370 break;
1371 default:
1372 av_unreachable("Invalid destination node type?");
1373 break;
1374 }
1375}
1376
1378{
1379 while (1) {
1380 SchFilterGraph *fg;
1381 SchDemux *demux;
1382 switch (src.type) {
1384 // fed directly by a demuxer (i.e. not through a filtergraph)
1385 demux = &sch->demux[src.idx];
1386 if (demux->waiter.choked_next == 0)
1387 return; // prevent infinite loop
1388 if (flags & UNCHOKE_DEMUX) {
1389 demux->waiter.choked_next = 0;
1390 for (int i = 0; i < demux->nb_streams; i++)
1391 unchoke_downstream(sch, demux->streams[i].dst);
1392 }
1393 return;
1394 case SCH_NODE_TYPE_DEC:
1395 src = sch->dec[src.idx].src;
1396 continue;
1397 case SCH_NODE_TYPE_ENC:
1398 src = sch->enc[src.idx].src;
1399 continue;
1401 fg = &sch->filters[src.idx];
1402 // the filtergraph contains internal sources and
1403 // requested to be scheduled directly
1404 if (fg->best_input == fg->nb_inputs) {
1405 if (flags & UNCHOKE_FILTER)
1406 fg->waiter.choked_next = 0;
1407 return;
1408 }
1409 src = fg->inputs[fg->best_input].src;
1410 continue;
1411 default:
1412 av_unreachable("Invalid source node type?");
1413 return;
1414 }
1415 }
1416}
1417
1418static void choke_demux(const Scheduler *sch, int demux_id, int choked)
1419{
1420 av_assert1(demux_id < sch->nb_demux);
1421 SchDemux *demux = &sch->demux[demux_id];
1422
1423 for (int i = 0; i < demux->nb_streams; i++) {
1424 SchedulerNode *dst = demux->streams[i].dst;
1425 SchFilterGraph *fg;
1426
1427 switch (dst->type) {
1428 case SCH_NODE_TYPE_DEC:
1429 tq_choke(sch->dec[dst->idx].queue, choked);
1430 break;
1431 case SCH_NODE_TYPE_ENC:
1432 tq_choke(sch->enc[dst->idx].queue, choked);
1433 break;
1434 case SCH_NODE_TYPE_MUX:
1435 break;
1437 fg = &sch->filters[dst->idx];
1438 if (fg->nb_inputs == 1)
1439 tq_choke(fg->queue, choked);
1440 break;
1441 default:
1442 av_unreachable("Invalid destination node type?");
1443 break;
1444 }
1445 }
1446}
1447
1449{
1450 int64_t dts;
1451 int have_unchoked = 0;
1452
1453 // on termination request all waiters are choked,
1454 // we are not to unchoke them
1455 if (atomic_load(&sch->terminate))
1456 return;
1457
1458 dts = trailing_dts(sch);
1459
1460 atomic_store(&sch->last_dts, progressing_dts(sch, 0));
1461
1462 // initialize our internal state
1463#define RESET_WAITER(field) \
1464 do { \
1465 for (unsigned i = 0; i < sch->nb_##field; i++) { \
1466 SchWaiter *w = &sch->field[i].waiter; \
1467 w->choked_prev = atomic_load(&w->choked); \
1468 w->choked_next = 1; \
1469 } \
1470 } while (0)
1471
1472 RESET_WAITER(demux);
1474
1475 // figure out the sources that are allowed to proceed
1476 for (unsigned i = 0; i < sch->nb_mux; i++) {
1477 SchMux *mux = &sch->mux[i];
1478
1479 for (unsigned j = 0; j < mux->nb_streams; j++) {
1480 SchMuxStream *ms = &mux->streams[j];
1481
1482 // unblock sources for output streams that are not finished
1483 // and not too far ahead of the trailing stream
1484 if (ms->source_finished)
1485 continue;
1486 if (dts == AV_NOPTS_VALUE && ms->last_dts_paced != AV_NOPTS_VALUE)
1487 continue;
1488 if (dts != AV_NOPTS_VALUE && ms->last_dts_paced - dts >= SCHEDULE_TOLERANCE)
1489 continue;
1490
1491 // resolve the source to unchoke
1493 have_unchoked = 1;
1494 }
1495 }
1496
1497 // also unchoke any sources feeding into closed filter graph inputs, so
1498 // that they can observe the downstream EOF
1499 for (unsigned i = 0; i < sch->nb_filters; i++) {
1500 SchFilterGraph *fg = &sch->filters[i];
1501
1502 for (unsigned j = 0; j < fg->nb_inputs; j++) {
1503 SchFilterIn *fi = &fg->inputs[j];
1504 if (fi->receive_finished && !fi->send_finished)
1506 }
1507 }
1508
1509 // make sure to unchoke at least one source, if still available
1510#define UNCHOKE_ONCE(field) \
1511 do { \
1512 for (unsigned i = 0; !have_unchoked && i < sch->nb_##field; i++) { \
1513 SchWaiter *w = &sch->field[i].waiter; \
1514 if (!sch->field[i].task_exited) { \
1515 w->choked_next = 0; \
1516 have_unchoked = 1; \
1517 break; \
1518 } \
1519 } \
1520 } while (0)
1521
1522 UNCHOKE_ONCE(demux);
1524
1525#define UPDATE_WAITER(field) \
1526 do { \
1527 for (unsigned i = 0; i < sch->nb_##field; i++) { \
1528 SchWaiter *w = &sch->field[i].waiter; \
1529 if (w->choked_prev != w->choked_next) { \
1530 waiter_set(w, w->choked_next); \
1531 if (offsetof(Scheduler, field) == offsetof(Scheduler, demux)) \
1532 choke_demux(sch, i, w->choked_next); \
1533 } \
1534 } \
1535 } while (0)
1536
1537 UPDATE_WAITER(demux);
1539}
1540
1541enum {
1545};
1546
1547// Finds the filtergraph or muxer upstream of a scheduler node
1549{
1550 while (1) {
1551 switch (src.type) {
1554 return src;
1555 case SCH_NODE_TYPE_DEC:
1556 src = sch->dec[src.idx].src;
1557 continue;
1558 case SCH_NODE_TYPE_ENC:
1559 src = sch->enc[src.idx].src;
1560 continue;
1561 default:
1562 av_unreachable("Invalid source node type?");
1563 return (SchedulerNode) {0};
1564 }
1565 }
1566}
1567
1568static int
1570 uint8_t *filters_visited, SchedulerNode *filters_stack)
1571{
1572 unsigned nb_filters_stack = 0;
1573
1574 memset(filters_visited, 0, sch->nb_filters * sizeof(*filters_visited));
1575
1576 while (1) {
1577 const SchFilterGraph *fg = &sch->filters[src.idx];
1578
1579 filters_visited[src.idx] = CYCLE_NODE_STARTED;
1580
1581 // descend into every input, depth first
1582 if (src.idx_stream < fg->nb_inputs) {
1583 const SchFilterIn *fi = &fg->inputs[src.idx_stream++];
1584 SchedulerNode node = src_filtergraph(sch, fi->src);
1585
1586 // connected to demuxer, no cycles possible
1587 if (node.type == SCH_NODE_TYPE_DEMUX)
1588 continue;
1589
1590 // otherwise connected to another filtergraph
1592
1593 // found a cycle
1594 if (filters_visited[node.idx] == CYCLE_NODE_STARTED)
1595 return AVERROR(EINVAL);
1596
1597 // place current position on stack and descend
1598 av_assert0(nb_filters_stack < sch->nb_filters);
1599 filters_stack[nb_filters_stack++] = src;
1600 src = (SchedulerNode){ .idx = node.idx, .idx_stream = 0 };
1601 continue;
1602 }
1603
1604 filters_visited[src.idx] = CYCLE_NODE_DONE;
1605
1606 // previous search finished,
1607 if (nb_filters_stack) {
1608 src = filters_stack[--nb_filters_stack];
1609 continue;
1610 }
1611 return 0;
1612 }
1613}
1614
1615static int check_acyclic(Scheduler *sch)
1616{
1617 uint8_t *filters_visited = NULL;
1618 SchedulerNode *filters_stack = NULL;
1619
1620 int ret = 0;
1621
1622 if (!sch->nb_filters)
1623 return 0;
1624
1625 filters_visited = av_malloc_array(sch->nb_filters, sizeof(*filters_visited));
1626 if (!filters_visited)
1627 return AVERROR(ENOMEM);
1628
1629 filters_stack = av_malloc_array(sch->nb_filters, sizeof(*filters_stack));
1630 if (!filters_stack) {
1631 ret = AVERROR(ENOMEM);
1632 goto fail;
1633 }
1634
1635 // trace the transcoding graph upstream from every filtegraph
1636 for (unsigned i = 0; i < sch->nb_filters; i++) {
1637 ret = check_acyclic_for_output(sch, (SchedulerNode){ .idx = i },
1638 filters_visited, filters_stack);
1639 if (ret < 0) {
1640 av_log(&sch->filters[i], AV_LOG_ERROR, "Transcoding graph has a cycle\n");
1641 goto fail;
1642 }
1643 }
1644
1645fail:
1646 av_freep(&filters_visited);
1647 av_freep(&filters_stack);
1648 return ret;
1649}
1650
1651static int start_prepare(Scheduler *sch)
1652{
1653 int ret;
1654
1655 for (unsigned i = 0; i < sch->nb_demux; i++) {
1656 SchDemux *d = &sch->demux[i];
1657
1658 for (unsigned j = 0; j < d->nb_streams; j++) {
1659 SchDemuxStream *ds = &d->streams[j];
1660
1661 if (!ds->nb_dst) {
1663 "Demuxer stream %u not connected to any sink\n", j);
1664 return AVERROR(EINVAL);
1665 }
1666
1667 ds->dst_finished = av_calloc(ds->nb_dst, sizeof(*ds->dst_finished));
1668 if (!ds->dst_finished)
1669 return AVERROR(ENOMEM);
1670 }
1671 }
1672
1673 for (unsigned i = 0; i < sch->nb_dec; i++) {
1674 SchDec *dec = &sch->dec[i];
1675
1676 if (!dec->src.type) {
1677 av_log(dec, AV_LOG_ERROR,
1678 "Decoder not connected to a source\n");
1679 return AVERROR(EINVAL);
1680 }
1681
1682 for (unsigned j = 0; j < dec->nb_outputs; j++) {
1683 SchDecOutput *o = &dec->outputs[j];
1684
1685 if (!o->nb_dst) {
1686 av_log(dec, AV_LOG_ERROR,
1687 "Decoder output %u not connected to any sink\n", j);
1688 return AVERROR(EINVAL);
1689 }
1690
1691 o->dst_finished = av_calloc(o->nb_dst, sizeof(*o->dst_finished));
1692 if (!o->dst_finished)
1693 return AVERROR(ENOMEM);
1694 }
1695 }
1696
1697 for (unsigned i = 0; i < sch->nb_enc; i++) {
1698 SchEnc *enc = &sch->enc[i];
1699
1700 if (!enc->src.type) {
1701 av_log(enc, AV_LOG_ERROR,
1702 "Encoder not connected to a source\n");
1703 return AVERROR(EINVAL);
1704 }
1705 if (!enc->nb_dst) {
1706 av_log(enc, AV_LOG_ERROR,
1707 "Encoder not connected to any sink\n");
1708 return AVERROR(EINVAL);
1709 }
1710
1711 enc->dst_finished = av_calloc(enc->nb_dst, sizeof(*enc->dst_finished));
1712 if (!enc->dst_finished)
1713 return AVERROR(ENOMEM);
1714 }
1715
1716 for (unsigned i = 0; i < sch->nb_mux; i++) {
1717 SchMux *mux = &sch->mux[i];
1718
1719 for (unsigned j = 0; j < mux->nb_streams; j++) {
1720 SchMuxStream *ms = &mux->streams[j];
1721
1722 if (!ms->src.type) {
1723 av_log(mux, AV_LOG_ERROR,
1724 "Muxer stream #%u not connected to a source\n", j);
1725 return AVERROR(EINVAL);
1726 }
1727 }
1728
1729 ret = queue_alloc(&mux->queue, mux->nb_streams, mux->queue_size,
1731 if (ret < 0)
1732 return ret;
1733 }
1734
1735 for (unsigned i = 0; i < sch->nb_filters; i++) {
1736 SchFilterGraph *fg = &sch->filters[i];
1737
1738 for (unsigned j = 0; j < fg->nb_inputs; j++) {
1739 SchFilterIn *fi = &fg->inputs[j];
1740
1741 if (!fi->src.type) {
1742 av_log(fg, AV_LOG_ERROR,
1743 "Filtergraph input %u not connected to a source\n", j);
1744 return AVERROR(EINVAL);
1745 }
1746 }
1747
1748 for (unsigned j = 0; j < fg->nb_outputs; j++) {
1749 SchFilterOut *fo = &fg->outputs[j];
1750
1751 if (!fo->dst.type) {
1752 av_log(fg, AV_LOG_ERROR,
1753 "Filtergraph %u output %u not connected to a sink\n", i, j);
1754 return AVERROR(EINVAL);
1755 }
1756 }
1757 }
1758
1759 // Check that the transcoding graph has no cycles.
1760 ret = check_acyclic(sch);
1761 if (ret < 0)
1762 return ret;
1763
1764 return 0;
1765}
1766
1768{
1769 int ret;
1770
1771 ret = start_prepare(sch);
1772 if (ret < 0)
1773 return ret;
1774
1776 sch->state = SCH_STATE_STARTED;
1777
1778 for (unsigned i = 0; i < sch->nb_mux; i++) {
1779 SchMux *mux = &sch->mux[i];
1780
1781 if (mux->nb_streams_ready == mux->nb_streams) {
1782 ret = mux_init(sch, mux);
1783 if (ret < 0)
1784 goto fail;
1785 }
1786 }
1787
1788 for (unsigned i = 0; i < sch->nb_enc; i++) {
1789 SchEnc *enc = &sch->enc[i];
1790
1791 ret = task_start(&enc->task);
1792 if (ret < 0)
1793 goto fail;
1794 }
1795
1796 for (unsigned i = 0; i < sch->nb_filters; i++) {
1797 SchFilterGraph *fg = &sch->filters[i];
1798
1799 ret = task_start(&fg->task);
1800 if (ret < 0)
1801 goto fail;
1802 }
1803
1804 for (unsigned i = 0; i < sch->nb_dec; i++) {
1805 SchDec *dec = &sch->dec[i];
1806
1807 ret = task_start(&dec->task);
1808 if (ret < 0)
1809 goto fail;
1810 }
1811
1812 for (unsigned i = 0; i < sch->nb_demux; i++) {
1813 SchDemux *d = &sch->demux[i];
1814
1815 if (!d->nb_streams)
1816 continue;
1817
1818 ret = task_start(&d->task);
1819 if (ret < 0)
1820 goto fail;
1821 }
1822
1826
1827 return 0;
1828fail:
1829 sch_stop(sch, NULL);
1830 return ret;
1831}
1832
1833int sch_wait(Scheduler *sch, uint64_t timeout_us, int64_t *transcode_ts)
1834{
1835 int ret;
1836
1837 // convert delay to absolute timestamp
1838 timeout_us += av_gettime();
1839
1841
1842 if (sch->nb_mux_done < sch->nb_mux) {
1843 struct timespec tv = { .tv_sec = timeout_us / 1000000,
1844 .tv_nsec = (timeout_us % 1000000) * 1000 };
1846 }
1847
1848 // abort transcoding if any task failed
1849 ret = sch->nb_mux_done == sch->nb_mux || sch->task_failed;
1850
1852
1853 *transcode_ts = atomic_load(&sch->last_dts);
1854
1855 return ret;
1856}
1857
1858static int enc_open(Scheduler *sch, SchEnc *enc, const AVFrame *frame)
1859{
1860 int ret;
1861
1862 ret = enc->open_cb(enc->task.func_arg, frame);
1863 if (ret < 0)
1864 return ret;
1865
1866 // ret>0 signals audio frame size, which means sync queue must
1867 // have been enabled during encoder creation
1868 if (ret > 0) {
1869 SchSyncQueue *sq;
1870
1871 av_assert0(enc->sq_idx[0] >= 0);
1872 sq = &sch->sq_enc[enc->sq_idx[0]];
1873
1875
1876 sq_frame_samples(sq->sq, enc->sq_idx[1], ret);
1877
1879 }
1880
1881 return 0;
1882}
1883
1885{
1886 int ret;
1887
1888 if (!frame) {
1889 tq_send_finish(enc->queue, 0);
1890 return 0;
1891 }
1892
1893 if (enc->in_finished)
1894 return AVERROR_EOF;
1895
1896 ret = tq_send(enc->queue, 0, frame);
1897 if (ret < 0)
1898 enc->in_finished = 1;
1899
1900 return ret;
1901}
1902
1904{
1905 SchSyncQueue *sq = &sch->sq_enc[enc->sq_idx[0]];
1906 int ret = 0;
1907
1908 // inform the scheduling code that no more input will arrive along this path;
1909 // this is necessary because the sync queue may not send an EOF downstream
1910 // until other streams finish
1911 // TODO: consider a cleaner way of passing this information through
1912 // the pipeline
1913 if (!frame) {
1914 for (unsigned i = 0; i < enc->nb_dst; i++) {
1915 SchMux *mux;
1916 SchMuxStream *ms;
1917
1918 if (enc->dst[i].type != SCH_NODE_TYPE_MUX)
1919 continue;
1920
1921 mux = &sch->mux[enc->dst[i].idx];
1922 ms = &mux->streams[enc->dst[i].idx_stream];
1923
1925
1926 ms->source_finished = 1;
1928
1930 }
1931 }
1932
1934
1935 ret = sq_send(sq->sq, enc->sq_idx[1], SQFRAME(frame));
1936 if (ret < 0)
1937 goto finish;
1938
1939 while (1) {
1940 SchEnc *enc;
1941
1942 // TODO: the SQ API should be extended to allow returning EOF
1943 // for individual streams
1944 ret = sq_receive(sq->sq, -1, SQFRAME(sq->frame));
1945 if (ret < 0) {
1946 ret = (ret == AVERROR(EAGAIN)) ? 0 : ret;
1947 break;
1948 }
1949
1950 enc = &sch->enc[sq->enc_idx[ret]];
1951 ret = send_to_enc_thread(sch, enc, sq->frame);
1952 if (ret < 0) {
1953 av_frame_unref(sq->frame);
1954 if (ret != AVERROR_EOF)
1955 break;
1956
1957 sq_send(sq->sq, enc->sq_idx[1], SQFRAME(NULL));
1958 continue;
1959 }
1960 }
1961
1962 if (ret < 0) {
1963 // close all encoders fed from this sync queue
1964 for (unsigned i = 0; i < sq->nb_enc_idx; i++) {
1965 int err = send_to_enc_thread(sch, &sch->enc[sq->enc_idx[i]], NULL);
1966
1967 // if the sync queue error is EOF and closing the encoder
1968 // produces a more serious error, make sure to pick the latter
1969 ret = err_merge((ret == AVERROR_EOF && err < 0) ? 0 : ret, err);
1970 }
1971 }
1972
1973finish:
1975
1976 return ret;
1977}
1978
1979static int send_to_enc(Scheduler *sch, SchEnc *enc, AVFrame *frame)
1980{
1981 if (enc->open_cb && frame && !enc->opened) {
1982 int ret = enc_open(sch, enc, frame);
1983 if (ret < 0)
1984 return ret;
1985 enc->opened = 1;
1986
1987 // discard empty frames that only carry encoder init parameters
1988 if (!frame->buf[0]) {
1990 return 0;
1991 }
1992 }
1993
1994 return (enc->sq_idx[0] >= 0) ?
1995 send_to_enc_sq (sch, enc, frame) :
1996 send_to_enc_thread(sch, enc, frame);
1997}
1998
2000{
2001 PreMuxQueue *q = &ms->pre_mux_queue;
2002 AVPacket *tmp_pkt = NULL;
2003 int ret;
2004
2005 if (!av_fifo_can_write(q->fifo)) {
2006 size_t packets = av_fifo_can_read(q->fifo);
2007 size_t pkt_size = pkt ? pkt->size : 0;
2008 int thresh_reached = (q->data_size + pkt_size) > q->data_threshold;
2009 size_t max_packets = thresh_reached ? q->max_packets : SIZE_MAX;
2010 size_t new_size = FFMIN(2 * packets, max_packets);
2011
2012 if (new_size <= packets) {
2013 av_log(mux, AV_LOG_ERROR,
2014 "Too many packets buffered for output stream.\n");
2016 }
2017 ret = av_fifo_grow2(q->fifo, new_size - packets);
2018 if (ret < 0)
2019 return ret;
2020 }
2021
2022 if (pkt) {
2023 tmp_pkt = av_packet_alloc();
2024 if (!tmp_pkt)
2025 return AVERROR(ENOMEM);
2026
2027 av_packet_move_ref(tmp_pkt, pkt);
2028 q->data_size += tmp_pkt->size;
2029 }
2030 av_fifo_write(q->fifo, &tmp_pkt, 1);
2031
2032 return 0;
2033}
2034
2035static int send_to_mux(Scheduler *sch, SchMux *mux, unsigned stream_idx,
2036 AVPacket *pkt)
2037{
2038 SchMuxStream *ms = &mux->streams[stream_idx];
2039 int64_t dts = (pkt && pkt->dts != AV_NOPTS_VALUE) ?
2040 av_rescale_q(pkt->dts + pkt->duration, pkt->time_base, AV_TIME_BASE_Q) :
2042
2043 // queue the packet if the muxer cannot be started yet
2044 if (!atomic_load(&mux->mux_started)) {
2045 int queued = 0;
2046
2047 // the muxer could have started between the above atomic check and
2048 // locking the mutex, then this block falls through to normal send path
2050
2051 if (!atomic_load(&mux->mux_started)) {
2052 int ret = mux_queue_packet(mux, ms, pkt);
2053 queued = ret < 0 ? ret : 1;
2054 }
2055
2057
2058 if (queued < 0)
2059 return queued;
2060 else if (queued)
2061 goto update_schedule;
2062 }
2063
2064 if (pkt) {
2065 int ret;
2066
2067 if (ms->init_eof)
2068 return AVERROR_EOF;
2069
2070 ret = tq_send(mux->queue, stream_idx, pkt);
2071 if (ret < 0)
2072 return ret;
2073 } else
2074 tq_send_finish(mux->queue, stream_idx);
2075
2076update_schedule:
2077 // TODO: use atomics to check whether this changes trailing dts
2078 // to avoid locking unnecessarily
2079 if (dts != AV_NOPTS_VALUE || !pkt) {
2081
2082 if (pkt) {
2083 ms->last_dts = dts;
2084 ms->last_dts_paced = dts + ms->dts_pacing_offset;
2085 } else {
2086 ms->source_finished = 1;
2087 }
2088
2090
2092 }
2093
2094 return 0;
2095}
2096
2097static int
2099 uint8_t *dst_finished, AVPacket *pkt, unsigned flags)
2100{
2101 int ret;
2102
2103 if (*dst_finished)
2104 return AVERROR_EOF;
2105
2106 if (pkt && dst.type == SCH_NODE_TYPE_MUX &&
2109 pkt = NULL;
2110 }
2111
2112 if (!pkt)
2113 goto finish;
2114
2115 ret = (dst.type == SCH_NODE_TYPE_MUX) ?
2116 send_to_mux(sch, &sch->mux[dst.idx], dst.idx_stream, pkt) :
2117 tq_send(sch->dec[dst.idx].queue, 0, pkt);
2118 if (ret == AVERROR_EOF)
2119 goto finish;
2120
2121 return ret;
2122
2123finish:
2124 if (dst.type == SCH_NODE_TYPE_MUX)
2125 send_to_mux(sch, &sch->mux[dst.idx], dst.idx_stream, NULL);
2126 else
2127 tq_send_finish(sch->dec[dst.idx].queue, 0);
2128
2129 *dst_finished = 1;
2130 return AVERROR_EOF;
2131}
2132
2134 AVPacket *pkt, unsigned flags)
2135{
2136 unsigned nb_done = 0;
2137
2138 for (unsigned i = 0; i < ds->nb_dst; i++) {
2139 AVPacket *to_send = pkt;
2140 uint8_t *finished = &ds->dst_finished[i];
2141
2142 int ret;
2143
2144 // sending a packet consumes it, so make a temporary reference if needed
2145 if (pkt && i < ds->nb_dst - 1) {
2146 to_send = d->send_pkt;
2147
2148 ret = av_packet_ref(to_send, pkt);
2149 if (ret < 0)
2150 return ret;
2151 }
2152
2153 ret = demux_stream_send_to_dst(sch, ds->dst[i], finished, to_send, flags);
2154 if (to_send)
2155 av_packet_unref(to_send);
2156 if (ret == AVERROR_EOF)
2157 nb_done++;
2158 else if (ret < 0)
2159 return ret;
2160 }
2161
2162 return (nb_done == ds->nb_dst) ? AVERROR_EOF : 0;
2163}
2164
2166{
2167 Timestamp max_end_ts = (Timestamp){ .ts = AV_NOPTS_VALUE };
2168
2169 av_assert0(!pkt->buf && !pkt->data && !pkt->side_data_elems);
2170
2171 for (unsigned i = 0; i < d->nb_streams; i++) {
2172 SchDemuxStream *ds = &d->streams[i];
2173
2174 for (unsigned j = 0; j < ds->nb_dst; j++) {
2175 const SchedulerNode *dst = &ds->dst[j];
2176 SchDec *dec;
2177 int ret;
2178
2179 if (ds->dst_finished[j] || dst->type != SCH_NODE_TYPE_DEC)
2180 continue;
2181
2182 dec = &sch->dec[dst->idx];
2183
2184 ret = tq_send(dec->queue, 0, pkt);
2185 if (ret < 0)
2186 return ret;
2187
2188 if (dec->queue_end_ts) {
2189 Timestamp ts;
2191 if (ret < 0)
2192 return ret;
2193
2194 if (max_end_ts.ts == AV_NOPTS_VALUE ||
2195 (ts.ts != AV_NOPTS_VALUE &&
2196 av_compare_ts(max_end_ts.ts, max_end_ts.tb, ts.ts, ts.tb) < 0))
2197 max_end_ts = ts;
2198
2199 }
2200 }
2201 }
2202
2203 pkt->pts = max_end_ts.ts;
2204 pkt->time_base = max_end_ts.tb;
2205
2206 return 0;
2207}
2208
2209int sch_demux_send(Scheduler *sch, unsigned demux_idx, AVPacket *pkt,
2210 unsigned flags)
2211{
2212 SchDemux *d;
2213 int terminate;
2214
2215 av_assert0(demux_idx < sch->nb_demux);
2216 d = &sch->demux[demux_idx];
2217
2218 terminate = waiter_wait(sch, &d->waiter);
2219 if (terminate)
2220 return AVERROR_EXIT;
2221
2222 // flush the downstreams after seek
2223 if (pkt->stream_index == -1)
2224 return demux_flush(sch, d, pkt);
2225
2226 av_assert0(pkt->stream_index < d->nb_streams);
2227
2228 return demux_send_for_stream(sch, d, &d->streams[pkt->stream_index], pkt, flags);
2229}
2230
2231static int demux_done(Scheduler *sch, unsigned demux_idx)
2232{
2233 SchDemux *d = &sch->demux[demux_idx];
2234 int ret = 0;
2235
2236 for (unsigned i = 0; i < d->nb_streams; i++) {
2237 int err = demux_send_for_stream(sch, d, &d->streams[i], NULL, 0);
2238 if (err != AVERROR_EOF)
2239 ret = err_merge(ret, err);
2240 }
2241
2243
2244 d->task_exited = 1;
2245
2247
2249
2250 return ret;
2251}
2252
2253int sch_mux_receive(Scheduler *sch, unsigned mux_idx, AVPacket *pkt)
2254{
2255 SchMux *mux;
2256 int ret, stream_idx;
2257
2258 av_assert0(mux_idx < sch->nb_mux);
2259 mux = &sch->mux[mux_idx];
2260
2261 ret = tq_receive(mux->queue, &stream_idx, pkt, 0);
2262 pkt->stream_index = stream_idx;
2263 return ret;
2264}
2265
2266void sch_mux_receive_finish(Scheduler *sch, unsigned mux_idx, unsigned stream_idx)
2267{
2268 SchMux *mux;
2269
2270 av_assert0(mux_idx < sch->nb_mux);
2271 mux = &sch->mux[mux_idx];
2272
2273 av_assert0(stream_idx < mux->nb_streams);
2274 tq_receive_finish(mux->queue, stream_idx);
2275
2277 mux->streams[stream_idx].source_finished = 1;
2278
2280
2282}
2283
2284int sch_mux_sub_heartbeat(Scheduler *sch, unsigned mux_idx, unsigned stream_idx,
2285 const AVPacket *pkt)
2286{
2287 SchMux *mux;
2288 SchMuxStream *ms;
2289
2290 av_assert0(mux_idx < sch->nb_mux);
2291 mux = &sch->mux[mux_idx];
2292
2293 av_assert0(stream_idx < mux->nb_streams);
2294 ms = &mux->streams[stream_idx];
2295
2296 for (unsigned i = 0; i < ms->nb_sub_heartbeat_dst; i++) {
2297 SchDec *dst = &sch->dec[ms->sub_heartbeat_dst[i]];
2298 int ret;
2299
2301 if (ret < 0)
2302 return ret;
2303
2304 tq_send(dst->queue, 0, mux->sub_heartbeat_pkt);
2305 }
2306
2307 return 0;
2308}
2309
2310static int mux_done(Scheduler *sch, unsigned mux_idx)
2311{
2312 SchMux *mux = &sch->mux[mux_idx];
2313
2315
2316 for (unsigned i = 0; i < mux->nb_streams; i++) {
2317 tq_receive_finish(mux->queue, i);
2318 mux->streams[i].source_finished = 1;
2319 }
2320
2322
2324
2326
2327 av_assert0(sch->nb_mux_done < sch->nb_mux);
2328 sch->nb_mux_done++;
2329
2331
2333
2334 return 0;
2335}
2336
2337int sch_dec_receive(Scheduler *sch, unsigned dec_idx, AVPacket *pkt)
2338{
2339 SchDec *dec;
2340 int ret, dummy;
2341
2342 av_assert0(dec_idx < sch->nb_dec);
2343 dec = &sch->dec[dec_idx];
2344
2345 // the decoder should have given us post-flush end timestamp in pkt
2346 if (dec->expect_end_ts) {
2347 Timestamp ts = (Timestamp){ .ts = pkt->pts, .tb = pkt->time_base };
2349 if (ret < 0)
2350 return ret;
2351
2352 dec->expect_end_ts = 0;
2353 }
2354
2355 ret = tq_receive(dec->queue, &dummy, pkt, 0);
2356 av_assert0(dummy <= 0);
2357
2358 // got a flush packet, on the next call to this function the decoder
2359 // will give us post-flush end timestamp
2360 if (ret >= 0 && !pkt->data && !pkt->side_data_elems && dec->queue_end_ts)
2361 dec->expect_end_ts = 1;
2362
2363 return ret;
2364}
2365
2367 unsigned in_idx, AVFrame *frame)
2368{
2369 if (frame)
2370 return tq_send(fg->queue, in_idx, frame);
2371
2373
2374 if (!fg->inputs[in_idx].send_finished) {
2375 fg->inputs[in_idx].send_finished = 1;
2376 tq_send_finish(fg->queue, in_idx);
2377
2378 // close the control stream when all actual inputs are done
2379 if (++fg->nb_inputs_finished_send == fg->nb_inputs)
2380 tq_send_finish(fg->queue, fg->nb_inputs);
2381
2383 }
2384
2386 return 0;
2387}
2388
2390 uint8_t *dst_finished, AVFrame *frame)
2391{
2392 int ret;
2393
2394 if (*dst_finished)
2395 return AVERROR_EOF;
2396
2397 if (!frame)
2398 goto finish;
2399
2400 ret = (dst.type == SCH_NODE_TYPE_FILTER_IN) ?
2401 send_to_filter(sch, &sch->filters[dst.idx], dst.idx_stream, frame) :
2402 send_to_enc(sch, &sch->enc[dst.idx], frame);
2403 if (ret == AVERROR_EOF)
2404 goto finish;
2405
2406 return ret;
2407
2408finish:
2409 if (dst.type == SCH_NODE_TYPE_FILTER_IN)
2410 send_to_filter(sch, &sch->filters[dst.idx], dst.idx_stream, NULL);
2411 else
2412 send_to_enc(sch, &sch->enc[dst.idx], NULL);
2413
2414 *dst_finished = 1;
2415
2416 return AVERROR_EOF;
2417}
2418
2419int sch_dec_send(Scheduler *sch, unsigned dec_idx,
2420 unsigned out_idx, AVFrame *frame)
2421{
2422 SchDec *dec;
2423 SchDecOutput *o;
2424 int ret;
2425 unsigned nb_done = 0;
2426
2427 av_assert0(dec_idx < sch->nb_dec);
2428 dec = &sch->dec[dec_idx];
2429
2430 av_assert0(out_idx < dec->nb_outputs);
2431 o = &dec->outputs[out_idx];
2432
2433 for (unsigned i = 0; i < o->nb_dst; i++) {
2434 uint8_t *finished = &o->dst_finished[i];
2435 AVFrame *to_send = frame;
2436
2437 // sending a frame consumes it, so make a temporary reference if needed
2438 if (i < o->nb_dst - 1) {
2439 to_send = dec->send_frame;
2440
2441 // frame may sometimes contain props only,
2442 // e.g. to signal EOF timestamp
2443 ret = frame->buf[0] ? av_frame_ref(to_send, frame) :
2444 av_frame_copy_props(to_send, frame);
2445 if (ret < 0)
2446 return ret;
2447 }
2448
2449 ret = dec_send_to_dst(sch, o->dst[i], finished, to_send);
2450 if (ret < 0) {
2451 av_frame_unref(to_send);
2452 if (ret == AVERROR_EOF) {
2453 nb_done++;
2454 continue;
2455 }
2456 return ret;
2457 }
2458 }
2459
2460 return (nb_done == o->nb_dst) ? AVERROR_EOF : 0;
2461}
2462
2463static int dec_done(Scheduler *sch, unsigned dec_idx)
2464{
2465 SchDec *dec = &sch->dec[dec_idx];
2466 int ret = 0;
2467
2468 tq_receive_finish(dec->queue, 0);
2469
2470 // make sure our source does not get stuck waiting for end timestamps
2471 // that will never arrive
2472 if (dec->queue_end_ts)
2474
2475 for (unsigned i = 0; i < dec->nb_outputs; i++) {
2476 SchDecOutput *o = &dec->outputs[i];
2477
2478 for (unsigned j = 0; j < o->nb_dst; j++) {
2479 int err = dec_send_to_dst(sch, o->dst[j], &o->dst_finished[j], NULL);
2480 if (err < 0 && err != AVERROR_EOF)
2481 ret = err_merge(ret, err);
2482 }
2483 }
2484
2485 return ret;
2486}
2487
2488int sch_enc_receive(Scheduler *sch, unsigned enc_idx, AVFrame *frame)
2489{
2490 SchEnc *enc;
2491 int ret, dummy;
2492
2493 av_assert0(enc_idx < sch->nb_enc);
2494 enc = &sch->enc[enc_idx];
2495
2496 ret = tq_receive(enc->queue, &dummy, frame, 0);
2497 av_assert0(dummy <= 0);
2498
2499 return ret;
2500}
2501
2503 uint8_t *dst_finished, AVPacket *pkt)
2504{
2505 int ret;
2506
2507 if (*dst_finished)
2508 return AVERROR_EOF;
2509
2510 if (!pkt)
2511 goto finish;
2512
2513 ret = (dst.type == SCH_NODE_TYPE_MUX) ?
2514 send_to_mux(sch, &sch->mux[dst.idx], dst.idx_stream, pkt) :
2515 tq_send(sch->dec[dst.idx].queue, 0, pkt);
2516 if (ret == AVERROR_EOF)
2517 goto finish;
2518
2519 return ret;
2520
2521finish:
2522 if (dst.type == SCH_NODE_TYPE_MUX)
2523 send_to_mux(sch, &sch->mux[dst.idx], dst.idx_stream, NULL);
2524 else
2525 tq_send_finish(sch->dec[dst.idx].queue, 0);
2526
2527 *dst_finished = 1;
2528
2529 return AVERROR_EOF;
2530}
2531
2532int sch_enc_send(Scheduler *sch, unsigned enc_idx, AVPacket *pkt)
2533{
2534 SchEnc *enc;
2535 int ret;
2536
2537 av_assert0(enc_idx < sch->nb_enc);
2538 enc = &sch->enc[enc_idx];
2539
2540 for (unsigned i = 0; i < enc->nb_dst; i++) {
2541 uint8_t *finished = &enc->dst_finished[i];
2542 AVPacket *to_send = pkt;
2543
2544 // sending a packet consumes it, so make a temporary reference if needed
2545 if (i < enc->nb_dst - 1) {
2546 to_send = enc->send_pkt;
2547
2548 ret = av_packet_ref(to_send, pkt);
2549 if (ret < 0)
2550 return ret;
2551 }
2552
2553 ret = enc_send_to_dst(sch, enc->dst[i], finished, to_send);
2554 if (ret < 0) {
2555 av_packet_unref(to_send);
2556 if (ret == AVERROR_EOF)
2557 continue;
2558 return ret;
2559 }
2560 }
2561
2562 return 0;
2563}
2564
2565static int enc_done(Scheduler *sch, unsigned enc_idx)
2566{
2567 SchEnc *enc = &sch->enc[enc_idx];
2568 int ret = 0;
2569
2570 tq_receive_finish(enc->queue, 0);
2571
2572 for (unsigned i = 0; i < enc->nb_dst; i++) {
2573 int err = enc_send_to_dst(sch, enc->dst[i], &enc->dst_finished[i], NULL);
2574 if (err < 0 && err != AVERROR_EOF)
2575 ret = err_merge(ret, err);
2576 }
2577
2578 return ret;
2579}
2580
2581int sch_filter_receive(Scheduler *sch, unsigned fg_idx,
2582 unsigned *in_idx, AVFrame *frame)
2583{
2584 SchFilterGraph *fg;
2585 int ret, idx;
2586
2587 av_assert0(fg_idx < sch->nb_filters);
2588 fg = &sch->filters[fg_idx];
2589
2590 av_assert0(*in_idx <= fg->nb_inputs);
2591
2592 // update scheduling to account for desired input stream, if it changed
2593 //
2594 // this check needs no locking because only the filtering thread
2595 // updates this value
2596 if (*in_idx != fg->best_input) {
2598
2599 fg->best_input = *in_idx;
2601
2603 }
2604
2605 if (*in_idx == fg->nb_inputs) {
2606 // drain incoming frames before waiting, to avoid blocking downstream
2608 if (ret >= 0) {
2609 av_assert0(idx >= 0);
2610 *in_idx = idx;
2611 return 0;
2612 }
2613
2614 int terminate = waiter_wait(sch, &fg->waiter);
2615 return terminate ? AVERROR_EOF : AVERROR(EAGAIN);
2616 }
2617
2618 while (1) {
2619 ret = tq_receive(fg->queue, &idx, frame, 0);
2620 if (idx < 0)
2621 return AVERROR_EOF;
2622 else if (ret >= 0) {
2623 *in_idx = idx;
2624 return 0;
2625 }
2626
2627 // disregard EOFs for specific streams - they should always be
2628 // preceded by an EOF frame
2629 }
2630}
2631
2632void sch_filter_receive_finish(Scheduler *sch, unsigned fg_idx, unsigned in_idx)
2633{
2634 SchFilterGraph *fg;
2635 SchFilterIn *fi;
2636
2637 av_assert0(fg_idx < sch->nb_filters);
2638 fg = &sch->filters[fg_idx];
2639
2640 av_assert0(in_idx < fg->nb_inputs);
2641 fi = &fg->inputs[in_idx];
2642
2644
2645 if (!fi->receive_finished) {
2646 fi->receive_finished = 1;
2647 tq_receive_finish(fg->queue, in_idx);
2648
2649 // close the control stream when all actual inputs are done
2650 if (++fg->nb_inputs_finished_receive == fg->nb_inputs)
2652
2654 }
2655
2657}
2658
2659int sch_filter_send(Scheduler *sch, unsigned fg_idx, unsigned out_idx, AVFrame *frame)
2660{
2661 SchFilterGraph *fg;
2663 int ret;
2664
2665 av_assert0(fg_idx < sch->nb_filters);
2666 fg = &sch->filters[fg_idx];
2667
2668 av_assert0(out_idx < fg->nb_outputs);
2669 dst = fg->outputs[out_idx].dst;
2670
2671 if (dst.type == SCH_NODE_TYPE_ENC) {
2672 ret = send_to_enc(sch, &sch->enc[dst.idx], frame);
2673 if (ret == AVERROR_EOF)
2674 send_to_enc(sch, &sch->enc[dst.idx], NULL);
2675 } else {
2676 ret = send_to_filter(sch, &sch->filters[dst.idx], dst.idx_stream, frame);
2677 if (ret == AVERROR_EOF)
2678 send_to_filter(sch, &sch->filters[dst.idx], dst.idx_stream, NULL);
2679 }
2680 return ret;
2681}
2682
2683static int filter_done(Scheduler *sch, unsigned fg_idx)
2684{
2685 SchFilterGraph *fg = &sch->filters[fg_idx];
2686 int ret = 0;
2687
2688 for (unsigned i = 0; i <= fg->nb_inputs; i++)
2690
2691 for (unsigned i = 0; i < fg->nb_outputs; i++) {
2692 SchedulerNode dst = fg->outputs[i].dst;
2693 int err = (dst.type == SCH_NODE_TYPE_ENC) ?
2694 send_to_enc (sch, &sch->enc[dst.idx], NULL) :
2695 send_to_filter(sch, &sch->filters[dst.idx], dst.idx_stream, NULL);
2696
2697 if (err < 0 && err != AVERROR_EOF)
2698 ret = err_merge(ret, err);
2699 }
2700
2702
2703 fg->task_exited = 1;
2704
2706
2708
2709 return ret;
2710}
2711
2712int sch_filter_command(Scheduler *sch, unsigned fg_idx, AVFrame *frame)
2713{
2714 SchFilterGraph *fg;
2715
2716 av_assert0(fg_idx < sch->nb_filters);
2717 fg = &sch->filters[fg_idx];
2718
2719 return send_to_filter(sch, fg, fg->nb_inputs, frame);
2720}
2721
2722void sch_filter_choke_inputs(Scheduler *sch, unsigned fg_idx)
2723{
2724 SchFilterGraph *fg;
2725 av_assert0(fg_idx < sch->nb_filters);
2726 fg = &sch->filters[fg_idx];
2727
2729 fg->best_input = fg->nb_inputs;
2732}
2733
2735{
2736 switch (node.type) {
2737 case SCH_NODE_TYPE_DEMUX: return demux_done (sch, node.idx);
2738 case SCH_NODE_TYPE_MUX: return mux_done (sch, node.idx);
2739 case SCH_NODE_TYPE_DEC: return dec_done (sch, node.idx);
2740 case SCH_NODE_TYPE_ENC: return enc_done (sch, node.idx);
2741 case SCH_NODE_TYPE_FILTER_IN: return filter_done(sch, node.idx);
2742 default: av_unreachable("Invalid node type?");
2743 }
2744}
2745
2746static void *task_wrapper(void *arg)
2747{
2748 SchTask *task = arg;
2749 Scheduler *sch = task->parent;
2750 int ret;
2751 int err = 0;
2752
2753 ret = task->func(task->func_arg);
2754 if (ret < 0)
2756 "Task finished with error: %s\n", av_err2str(ret));
2757
2758 err = task_cleanup(sch, task->node);
2759 ret = err_merge(ret, err);
2760
2761 // EOF is considered normal termination
2762 if (ret == AVERROR_EOF)
2763 ret = 0;
2764 if (ret < 0) {
2766 sch->task_failed = 1;
2769 }
2770
2771 if (ret < 0)
2773 "Terminating thread with error: %s\n", av_err2str(ret));
2774 else
2776 "Terminating thread with success\n");
2777
2778 return (void*)(intptr_t)ret;
2779}
2780
2781static int task_stop(Scheduler *sch, SchTask *task)
2782{
2783 int ret;
2784 void *thread_ret;
2785
2786 if (!task->parent)
2787 return 0;
2788
2789 if (!task->thread_running)
2790 return task_cleanup(sch, task->node);
2791
2792 ret = pthread_join(task->thread, &thread_ret);
2793 av_assert0(ret == 0);
2794
2795 task->thread_running = 0;
2796
2797 return (intptr_t)thread_ret;
2798}
2799
2800int sch_stop(Scheduler *sch, int64_t *finish_ts)
2801{
2802 int ret = 0, err;
2803
2804 if (sch->state != SCH_STATE_STARTED)
2805 return 0;
2806
2807 atomic_store(&sch->terminate, 1);
2808
2809 // Ensure no other thread is currently in schedule_update_locked while
2810 // we are choking all demuxers
2812
2813 for (unsigned type = 0; type < 2; type++)
2814 for (unsigned i = 0; i < (type ? sch->nb_demux : sch->nb_filters); i++) {
2815 SchWaiter *w = type ? &sch->demux[i].waiter : &sch->filters[i].waiter;
2816 waiter_set(w, 1);
2817 if (type)
2818 choke_demux(sch, i, 0); // unfreeze to allow draining
2819 }
2820
2822
2823 for (unsigned i = 0; i < sch->nb_demux; i++) {
2824 SchDemux *d = &sch->demux[i];
2825
2826 err = task_stop(sch, &d->task);
2827 ret = err_merge(ret, err);
2828 }
2829
2830 for (unsigned i = 0; i < sch->nb_dec; i++) {
2831 SchDec *dec = &sch->dec[i];
2832
2833 err = task_stop(sch, &dec->task);
2834 ret = err_merge(ret, err);
2835 }
2836
2837 for (unsigned i = 0; i < sch->nb_filters; i++) {
2838 SchFilterGraph *fg = &sch->filters[i];
2839
2840 err = task_stop(sch, &fg->task);
2841 ret = err_merge(ret, err);
2842 }
2843
2844 for (unsigned i = 0; i < sch->nb_enc; i++) {
2845 SchEnc *enc = &sch->enc[i];
2846
2847 err = task_stop(sch, &enc->task);
2848 ret = err_merge(ret, err);
2849 }
2850
2851 for (unsigned i = 0; i < sch->nb_mux; i++) {
2852 SchMux *mux = &sch->mux[i];
2853
2854 err = task_stop(sch, &mux->task);
2855 ret = err_merge(ret, err);
2856 }
2857
2858 if (finish_ts)
2859 *finish_ts = progressing_dts(sch, 1);
2860
2861 sch->state = SCH_STATE_STOPPED;
2862
2863 return ret;
2864}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
#define filters(fmt, type, inverse, clp, inverset, clip, one, clip_fn, packed)
static AVFormatContext * ctx
static void finish(void)
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition avassert.h:109
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define GROW_ARRAY(array, nb_elems)
Definition cmdutils.h:536
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
error code definitions
static int filter_done(Scheduler *sch, unsigned fg_idx)
int sch_add_dec(Scheduler *sch, SchThreadFunc func, void *ctx, int send_end_ts)
Add a decoder to the scheduler.
int sch_filter_send(Scheduler *sch, unsigned fg_idx, unsigned out_idx, AVFrame *frame)
Called by filtergraph tasks to send a filtered frame or EOF to consumers.
static int demux_send_for_stream(Scheduler *sch, SchDemux *d, SchDemuxStream *ds, AVPacket *pkt, unsigned flags)
static void unchoke_downstream(Scheduler *sch, SchedulerNode *dst)
static int enc_done(Scheduler *sch, unsigned enc_idx)
static int64_t progressing_dts(const Scheduler *sch, int count_finished)
#define SCHEDULE_TOLERANCE
Scheduler * sch_alloc(void)
int sch_add_filtergraph(Scheduler *sch, unsigned nb_inputs, unsigned nb_outputs, SchThreadFunc func, void *ctx)
Add a filtergraph to the scheduler.
void sch_filter_receive_finish(Scheduler *sch, unsigned fg_idx, unsigned in_idx)
Called by filter tasks to signal that a filter input will no longer accept input.
int sch_demux_send(Scheduler *sch, unsigned demux_idx, AVPacket *pkt, unsigned flags)
Called by demuxer tasks to communicate with their downstreams.
static void task_init(Scheduler *sch, SchTask *task, enum SchedulerNodeType type, unsigned idx, SchThreadFunc func, void *func_arg)
int sch_mux_sub_heartbeat_add(Scheduler *sch, unsigned mux_idx, unsigned stream_idx, unsigned dec_idx)
int sch_start(Scheduler *sch)
#define RESET_WAITER(field)
static int dec_done(Scheduler *sch, unsigned dec_idx)
static int waiter_wait(Scheduler *sch, SchWaiter *w)
Wait until this task is allowed to proceed.
static int dec_send_to_dst(Scheduler *sch, const SchedulerNode dst, uint8_t *dst_finished, AVFrame *frame)
#define UPDATE_WAITER(field)
void sch_remove_filtergraph(Scheduler *sch, int idx)
int sch_add_dec_output(Scheduler *sch, unsigned dec_idx)
Add another output to decoder (e.g.
static int send_to_enc_thread(Scheduler *sch, SchEnc *enc, AVFrame *frame)
static void waiter_uninit(SchWaiter *w)
int sch_mux_stream_ready(Scheduler *sch, unsigned mux_idx, unsigned stream_idx)
Signal to the scheduler that the specified muxed stream is initialized and ready.
static SchedulerNode src_filtergraph(const Scheduler *sch, SchedulerNode src)
static const AVClass sch_enc_class
static void waiter_set(SchWaiter *w, int choked)
static int demux_flush(Scheduler *sch, SchDemux *d, AVPacket *pkt)
@ UNCHOKE_DEMUX
@ UNCHOKE_FILTER
@ UNCHOKE_ALL
int print_sdp(const char *filename)
Definition ffmpeg_mux.c:501
int sch_stop(Scheduler *sch, int64_t *finish_ts)
void sch_mux_receive_finish(Scheduler *sch, unsigned mux_idx, unsigned stream_idx)
Called by muxer tasks to signal that a stream will no longer accept input.
int sch_dec_send(Scheduler *sch, unsigned dec_idx, unsigned out_idx, AVFrame *frame)
Called by decoder tasks to send a decoded frame downstream.
static int check_acyclic(Scheduler *sch)
static void choke_demux(const Scheduler *sch, int demux_id, int choked)
static void * task_wrapper(void *arg)
static int task_start(SchTask *task)
static int start_prepare(Scheduler *sch)
int sch_add_sq_enc(Scheduler *sch, uint64_t buf_size_us, void *logctx)
Add an pre-encoding sync queue to the scheduler.
static const AVClass scheduler_class
int sch_enc_receive(Scheduler *sch, unsigned enc_idx, AVFrame *frame)
Called by encoder tasks to obtain frames for encoding.
static int send_to_enc_sq(Scheduler *sch, SchEnc *enc, AVFrame *frame)
int sch_sq_add_enc(Scheduler *sch, unsigned sq_idx, unsigned enc_idx, int limiting, uint64_t max_frames)
int sch_wait(Scheduler *sch, uint64_t timeout_us, int64_t *transcode_ts)
Wait until transcoding terminates or the specified timeout elapses.
static int mux_queue_packet(SchMux *mux, SchMuxStream *ms, AVPacket *pkt)
void sch_mux_stream_buffering(Scheduler *sch, unsigned mux_idx, unsigned stream_idx, size_t data_threshold, int max_packets)
Configure limits on packet buffering performed before the muxer task is started.
static int check_acyclic_for_output(const Scheduler *sch, SchedulerNode src, uint8_t *filters_visited, SchedulerNode *filters_stack)
static int queue_alloc(ThreadQueue **ptq, unsigned nb_streams, unsigned queue_size, enum QueueType type)
static int enc_open(Scheduler *sch, SchEnc *enc, const AVFrame *frame)
static int mux_task_start(SchMux *mux)
int sch_enc_send(Scheduler *sch, unsigned enc_idx, AVPacket *pkt)
Called by encoder tasks to send encoded packets downstream.
static int mux_done(Scheduler *sch, unsigned mux_idx)
int sch_add_enc(Scheduler *sch, SchThreadFunc func, void *ctx, int(*open_cb)(void *opaque, const AVFrame *frame))
Add an encoder to the scheduler.
int sch_sdp_filename(Scheduler *sch, const char *sdp_filename)
Set the file path for the SDP.
static const AVClass sch_fg_class
SchedulerState
@ SCH_STATE_UNINIT
@ SCH_STATE_STOPPED
@ SCH_STATE_STARTED
static int task_stop(Scheduler *sch, SchTask *task)
static int demux_done(Scheduler *sch, unsigned demux_idx)
int sch_filter_receive(Scheduler *sch, unsigned fg_idx, unsigned *in_idx, AVFrame *frame)
Called by filtergraph tasks to obtain frames for filtering.
void sch_filter_choke_inputs(Scheduler *sch, unsigned fg_idx)
Called by filtergraph tasks to choke all filter inputs, preventing them from receiving more frames un...
int sch_add_demux_stream(Scheduler *sch, unsigned demux_idx)
Add a demuxed stream for a previously added demuxer.
int sch_connect(Scheduler *sch, SchedulerNode src, SchedulerNode dst)
int sch_filter_command(Scheduler *sch, unsigned fg_idx, AVFrame *frame)
static int64_t trailing_dts(const Scheduler *sch)
int sch_dec_receive(Scheduler *sch, unsigned dec_idx, AVPacket *pkt)
Called by decoder tasks to receive a packet for decoding.
int sch_add_demux(Scheduler *sch, SchThreadFunc func, void *ctx)
Add a demuxer to the scheduler.
static int enc_send_to_dst(Scheduler *sch, const SchedulerNode dst, uint8_t *dst_finished, AVPacket *pkt)
int sch_add_mux(Scheduler *sch, SchThreadFunc func, int(*init)(void *), void *arg, int sdp_auto, unsigned thread_queue_size)
Add a muxer to the scheduler.
@ CYCLE_NODE_STARTED
@ CYCLE_NODE_DONE
@ CYCLE_NODE_NEW
int sch_mux_sub_heartbeat(Scheduler *sch, unsigned mux_idx, unsigned stream_idx, const AVPacket *pkt)
static int send_to_filter(Scheduler *sch, SchFilterGraph *fg, unsigned in_idx, AVFrame *frame)
QueueType
@ QUEUE_PACKETS
@ QUEUE_FRAMES
static const AVClass sch_mux_class
static int demux_stream_send_to_dst(Scheduler *sch, const SchedulerNode dst, uint8_t *dst_finished, AVPacket *pkt, unsigned flags)
static const AVClass sch_dec_class
static int send_to_mux(Scheduler *sch, SchMux *mux, unsigned stream_idx, AVPacket *pkt)
static void schedule_update_locked(Scheduler *sch)
int sch_mux_receive(Scheduler *sch, unsigned mux_idx, AVPacket *pkt)
Called by muxer tasks to obtain packets for muxing.
static int send_to_enc(Scheduler *sch, SchEnc *enc, AVFrame *frame)
static int task_cleanup(Scheduler *sch, SchedulerNode node)
void sch_mux_stream_set_dts_pacing_offset(Scheduler *sch, unsigned mux_idx, unsigned stream_idx, int64_t offset)
Set the timestamp offset (in AV_TIME_BASE_Q) that the upstream pipeline subtracted from this stream's...
int sch_add_mux_stream(Scheduler *sch, unsigned mux_idx)
Add a muxed stream for a previously added muxer.
void sch_free(Scheduler **psch)
static int waiter_init(SchWaiter *w)
#define UNCHOKE_ONCE(field)
static int mux_init(Scheduler *sch, SchMux *mux)
static void unchoke_for_stream(Scheduler *sch, SchedulerNode src, int flags)
static const AVClass sch_demux_class
int(* SchThreadFunc)(void *arg)
#define DEFAULT_PACKET_THREAD_QUEUE_SIZE
Default size of a packet thread queue.
#define DEFAULT_FRAME_THREAD_QUEUE_SIZE
Default size of a frame thread queue.
@ DEMUX_SEND_STREAMCOPY_EOF
Treat the packet as an EOF for SCH_NODE_TYPE_MUX destinations send normally to other types.
SchedulerNodeType
@ SCH_NODE_TYPE_FILTER_OUT
@ SCH_NODE_TYPE_ENC
@ SCH_NODE_TYPE_MUX
@ SCH_NODE_TYPE_FILTER_IN
@ SCH_NODE_TYPE_DEC
@ SCH_NODE_TYPE_DEMUX
static int err_merge(int err0, int err1)
Merge two return codes - return one of the error codes if at least one of them was negative,...
int ret
Definition ffplay.c:391
static int dummy
Definition ffplay.c:4453
static unsigned int nb_streams
Definition ffprobe.c:390
A generic FIFO API.
reference-counted frame API
#define fail
Definition test.h:479
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition packet.c:492
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition packet.c:443
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition packet.c:398
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition error.h:58
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition error.h:53
#define AVERROR_EOF
End of file.
Definition error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
Definition fifo.c:47
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
Definition fifo.c:286
size_t av_fifo_can_write(const AVFifo *f)
Definition fifo.c:94
size_t av_fifo_can_read(const AVFifo *f)
Definition fifo.c:87
int av_fifo_peek(const AVFifo *f, void *buf, size_t nb_elems, size_t offset)
Read data from a FIFO without modifying FIFO state.
Definition fifo.c:255
int av_fifo_grow2(AVFifo *f, size_t inc)
Enlarge an AVFifo.
Definition fifo.c:99
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition fifo.c:188
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
Definition fifo.c:240
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_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition frame.c:281
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:602
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
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.
#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
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
enum MPFType type
Definition jpegmpfenc.c:55
unsigned offset
Definition libaomenc.c:812
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition jacosubdec.c:67
const char * arg
Definition jacosubdec.c:66
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
#define av_strdup(s)
Definition ops_static.c:55
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition os2threads.h:158
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition os2threads.h:119
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition os2threads.h:150
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition os2threads.h:104
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition os2threads.h:94
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition os2threads.h:139
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition os2threads.h:80
_fmutex pthread_mutex_t
Definition os2threads.h:53
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition os2threads.h:132
static av_always_inline int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
Definition os2threads.h:176
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition os2threads.h:198
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition os2threads.h:112
FF_ATOMIC_ALIGN64 int_least64_t atomic_int_least64_t
Definition stdatomic.h:79
#define atomic_store(object, desired)
Definition stdatomic.h:256
int atomic_int
Definition stdatomic.h:63
#define atomic_load(object)
Definition stdatomic.h:250
#define atomic_init(obj, value)
Definition stdatomic.h:119
Describe the class of an AVClass context structure.
Definition log.h:76
Definition fifo.c:35
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
size_t data_size
int max_packets
Maximum number of packets in fifo.
size_t data_threshold
AVFifo * fifo
Queue for buffering the packets before the muxer task can be started.
unsigned nb_dst
uint8_t * dst_finished
SchedulerNode * dst
SchedulerNode src
unsigned nb_outputs
SchTask task
AVFrame * send_frame
int expect_end_ts
ThreadQueue * queue
const AVClass * class
SchDecOutput * outputs
AVThreadMessageQueue * queue_end_ts
uint8_t * dst_finished
SchedulerNode * dst
AVPacket * send_pkt
int task_exited
SchDemuxStream * streams
const AVClass * class
SchWaiter waiter
unsigned nb_streams
SchTask task
SchedulerNode src
int in_finished
SchedulerNode * dst
unsigned nb_dst
const AVClass * class
SchTask task
uint8_t * dst_finished
AVPacket * send_pkt
int(* open_cb)(void *opaque, const AVFrame *frame)
ThreadQueue * queue
int sq_idx[2]
unsigned nb_inputs
unsigned best_input
SchFilterOut * outputs
unsigned nb_outputs
unsigned nb_inputs_finished_receive
ThreadQueue * queue
SchWaiter waiter
unsigned nb_inputs_finished_send
SchFilterIn * inputs
const AVClass * class
SchedulerNode src
SchedulerNode dst
int64_t last_dts
int64_t last_dts_paced
int64_t dts_pacing_offset
unsigned * sub_heartbeat_dst
unsigned nb_sub_heartbeat_dst
SchedulerNode src
PreMuxQueue pre_mux_queue
SchMuxStream * streams
unsigned nb_streams_ready
SchTask task
unsigned queue_size
int(* init)(void *arg)
ThreadQueue * queue
unsigned nb_streams
const AVClass * class
atomic_int mux_started
Set to 1 after starting the muxer task and flushing the pre-muxing queues.
AVPacket * sub_heartbeat_pkt
pthread_mutex_t lock
unsigned nb_enc_idx
AVFrame * frame
unsigned * enc_idx
SyncQueue * sq
void * func_arg
SchedulerNode node
int thread_running
SchThreadFunc func
Scheduler * parent
pthread_t thread
int choked_prev
atomic_int choked
int choked_next
pthread_mutex_t lock
pthread_cond_t cond
unsigned idx_stream
enum SchedulerNodeType type
unsigned task_failed
atomic_int_least64_t last_dts
SchSyncQueue * sq_enc
unsigned nb_mux_ready
unsigned nb_sq_enc
unsigned nb_mux
unsigned nb_filters
SchDec * dec
unsigned nb_demux
pthread_cond_t finish_cond
unsigned nb_mux_done
unsigned nb_dec
SchEnc * enc
enum SchedulerState state
unsigned nb_enc
char * sdp_filename
atomic_int terminate
const AVClass * class
SchFilterGraph * filters
SchDemux * demux
pthread_mutex_t schedule_lock
pthread_mutex_t finish_lock
pthread_mutex_t mux_ready_lock
SchMux * mux
A sync queue provides timestamp synchronization between multiple streams.
Definition sync_queue.c:92
int64_t ts
AVRational tb
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:345
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:666
int sq_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
Read a frame from the queue.
Definition sync_queue.c:624
void sq_free(SyncQueue **psq)
Definition sync_queue.c:717
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:686
int sq_add_stream(SyncQueue *sq, int limiting)
Add a new stream to the sync queue.
Definition sync_queue.c:636
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:700
#define SQFRAME(frame)
Definition sync_queue.h:38
@ SYNC_QUEUE_FRAMES
Definition sync_queue.h:30
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
ThreadQueue * tq_alloc(unsigned int nb_streams, size_t queue_size, enum ThreadQueueType type)
Allocate a queue for sending data between threads.
void tq_send_finish(ThreadQueue *tq, unsigned int stream_idx)
Mark the given stream finished from the sending side.
int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data)
Send an item for the given stream to the queue.
void tq_choke(ThreadQueue *tq, int choked)
Prevent further reads from the thread queue until it is unchoked.
int tq_receive(ThreadQueue *tq, int *stream_idx, void *data, int flags)
Read the next item from the queue.
void tq_free(ThreadQueue **ptq)
void tq_receive_finish(ThreadQueue *tq, unsigned int stream_idx)
Mark the given stream finished from the receiving side.
@ THREAD_QUEUE_FRAMES
@ THREAD_QUEUE_PACKETS
@ THREAD_QUEUE_FLAG_NO_BLOCK
int av_thread_message_queue_recv(AVThreadMessageQueue *mq, void *msg, unsigned flags)
Receive a message from the queue.
int av_thread_message_queue_alloc(AVThreadMessageQueue **mq, unsigned nelem, unsigned elsize)
Allocate a new message queue.
void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq, int err)
Set the receiving error code.
int av_thread_message_queue_send(AVThreadMessageQueue *mq, void *msg, unsigned flags)
Send a message on the queue.
void av_thread_message_queue_free(AVThreadMessageQueue **mq)
Free a message queue.
int64_t av_gettime(void)
Get the current time in microseconds.
Definition time.c:40