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 ////////////////////////////////////////////////////////////
202 // The following are protected by Scheduler.schedule_lock //
203
204 /* dts+duration of the last packet sent to this stream
205 in AV_TIME_BASE_Q */
207 // this stream no longer accepts input
209 ////////////////////////////////////////////////////////////
211
212typedef struct SchMux {
213 const AVClass *class;
214
216 unsigned nb_streams;
218
219 int (*init)(void *arg);
220
222 /**
223 * Set to 1 after starting the muxer task and flushing the
224 * pre-muxing queues.
225 * Set either before any tasks have started, or with
226 * Scheduler.mux_ready_lock held.
227 */
230 unsigned queue_size;
231
233} SchMux;
234
240
244
245typedef struct SchFilterGraph {
246 const AVClass *class;
247
249 unsigned nb_inputs;
252
254 unsigned nb_outputs;
255
257 // input queue, nb_inputs+1 streams
258 // last stream is control
261
262 // protected by schedule_lock
263 unsigned best_input;
266
272
313
314/**
315 * Wait until this task is allowed to proceed.
316 *
317 * @retval 0 the caller should proceed
318 * @retval 1 the caller should terminate
319 */
321{
322 int terminate;
323
324 if (!atomic_load(&w->choked))
325 return 0;
326
327 pthread_mutex_lock(&w->lock);
328
329 while (atomic_load(&w->choked) && !atomic_load(&sch->terminate))
330 pthread_cond_wait(&w->cond, &w->lock);
331
332 terminate = atomic_load(&sch->terminate);
333
334 pthread_mutex_unlock(&w->lock);
335
336 return terminate;
337}
338
339static void waiter_set(SchWaiter *w, int choked)
340{
341 pthread_mutex_lock(&w->lock);
342
343 atomic_store(&w->choked, choked);
344 pthread_cond_signal(&w->cond);
345
346 pthread_mutex_unlock(&w->lock);
347}
348
350{
351 int ret;
352
353 atomic_init(&w->choked, 0);
354
355 ret = pthread_mutex_init(&w->lock, NULL);
356 if (ret)
357 return AVERROR(ret);
358
359 ret = pthread_cond_init(&w->cond, NULL);
360 if (ret)
361 return AVERROR(ret);
362
363 return 0;
364}
365
367{
368 pthread_mutex_destroy(&w->lock);
369 pthread_cond_destroy(&w->cond);
370}
371
372static int queue_alloc(ThreadQueue **ptq, unsigned nb_streams, unsigned queue_size,
373 enum QueueType type)
374{
375 ThreadQueue *tq;
376
377 if (queue_size <= 0) {
378 if (type == QUEUE_FRAMES)
380 else
382 }
383
384 if (type == QUEUE_FRAMES) {
385 // This queue length is used in the decoder code to ensure that
386 // there are enough entries in fixed-size frame pools to account
387 // for frames held in queues inside the ffmpeg utility. If this
388 // can ever dynamically change then the corresponding decode
389 // code needs to be updated as well.
391 }
392
393 tq = tq_alloc(nb_streams, queue_size,
395 if (!tq)
396 return AVERROR(ENOMEM);
397
398 *ptq = tq;
399 return 0;
400}
401
402static void *task_wrapper(void *arg);
403
404static int task_start(SchTask *task)
405{
406 int ret;
407
408 if (!task->parent)
409 return 0;
410
411 av_log(task->func_arg, AV_LOG_VERBOSE, "Starting thread...\n");
412
414
415 ret = pthread_create(&task->thread, NULL, task_wrapper, task);
416 if (ret) {
417 av_log(task->func_arg, AV_LOG_ERROR, "pthread_create() failed: %s\n",
418 strerror(ret));
419 return AVERROR(ret);
420 }
421
422 task->thread_running = 1;
423 return 0;
424}
425
426static void task_init(Scheduler *sch, SchTask *task, enum SchedulerNodeType type, unsigned idx,
427 SchThreadFunc func, void *func_arg)
428{
429 task->parent = sch;
430
431 task->node.type = type;
432 task->node.idx = idx;
433
434 task->func = func;
435 task->func_arg = func_arg;
436}
437
439{
440 int64_t min_dts = INT64_MAX;
441
442 for (unsigned i = 0; i < sch->nb_mux; i++) {
443 const SchMux *mux = &sch->mux[i];
444
445 for (unsigned j = 0; j < mux->nb_streams; j++) {
446 const SchMuxStream *ms = &mux->streams[j];
447
448 if (ms->source_finished)
449 continue;
450 if (ms->last_dts == AV_NOPTS_VALUE)
451 return AV_NOPTS_VALUE;
452
453 min_dts = FFMIN(min_dts, ms->last_dts);
454 }
455 }
456
457 return min_dts == INT64_MAX ? AV_NOPTS_VALUE : min_dts;
458}
459
460static int64_t progressing_dts(const Scheduler *sch, int count_finished)
461{
462 int64_t max_dts = INT64_MIN;
463
464 for (unsigned i = 0; i < sch->nb_mux; i++) {
465 const SchMux *mux = &sch->mux[i];
466
467 for (unsigned j = 0; j < mux->nb_streams; j++) {
468 const SchMuxStream *ms = &mux->streams[j];
469
470 if (ms->source_finished && !count_finished)
471 continue;
472 if (ms->last_dts != AV_NOPTS_VALUE)
473 max_dts = FFMAX(max_dts, ms->last_dts);
474 }
475 }
476
477 return max_dts == INT64_MIN ? AV_NOPTS_VALUE : max_dts;
478}
479
481{
482 SchFilterGraph *fg = &sch->filters[idx];
483
485 memset(&fg->task, 0, sizeof(fg->task));
486
487 tq_free(&fg->queue);
488
489 av_freep(&fg->inputs);
490 fg->nb_inputs = 0;
491 av_freep(&fg->outputs);
492 fg->nb_outputs = 0;
493
494 fg->task_exited = 1;
495}
496
497void sch_free(Scheduler **psch)
498{
499 Scheduler *sch = *psch;
500
501 if (!sch)
502 return;
503
504 sch_stop(sch, NULL);
505
506 for (unsigned i = 0; i < sch->nb_demux; i++) {
507 SchDemux *d = &sch->demux[i];
508
509 for (unsigned j = 0; j < d->nb_streams; j++) {
510 SchDemuxStream *ds = &d->streams[j];
511 av_freep(&ds->dst);
513 }
514 av_freep(&d->streams);
515
517
519 }
520 av_freep(&sch->demux);
521
522 for (unsigned i = 0; i < sch->nb_mux; i++) {
523 SchMux *mux = &sch->mux[i];
524
525 for (unsigned j = 0; j < mux->nb_streams; j++) {
526 SchMuxStream *ms = &mux->streams[j];
527
528 if (ms->pre_mux_queue.fifo) {
529 AVPacket *pkt;
530 while (av_fifo_read(ms->pre_mux_queue.fifo, &pkt, 1) >= 0)
533 }
534
536 }
537 av_freep(&mux->streams);
538
540
541 tq_free(&mux->queue);
542 }
543 av_freep(&sch->mux);
544
545 for (unsigned i = 0; i < sch->nb_dec; i++) {
546 SchDec *dec = &sch->dec[i];
547
548 tq_free(&dec->queue);
549
551
552 for (unsigned j = 0; j < dec->nb_outputs; j++) {
553 SchDecOutput *o = &dec->outputs[j];
554
555 av_freep(&o->dst);
557 }
558
559 av_freep(&dec->outputs);
560
562 }
563 av_freep(&sch->dec);
564
565 for (unsigned i = 0; i < sch->nb_enc; i++) {
566 SchEnc *enc = &sch->enc[i];
567
568 tq_free(&enc->queue);
569
571
572 av_freep(&enc->dst);
573 av_freep(&enc->dst_finished);
574 }
575 av_freep(&sch->enc);
576
577 for (unsigned i = 0; i < sch->nb_sq_enc; i++) {
578 SchSyncQueue *sq = &sch->sq_enc[i];
579 sq_free(&sq->sq);
580 av_frame_free(&sq->frame);
582 av_freep(&sq->enc_idx);
583 }
584 av_freep(&sch->sq_enc);
585
586 for (unsigned i = 0; i < sch->nb_filters; i++) {
587 SchFilterGraph *fg = &sch->filters[i];
588
589 tq_free(&fg->queue);
590
591 av_freep(&fg->inputs);
592 av_freep(&fg->outputs);
593
594 waiter_uninit(&fg->waiter);
595 }
596 av_freep(&sch->filters);
597
598 av_freep(&sch->sdp_filename);
599
601
603
606
607 av_freep(psch);
608}
609
610static const AVClass scheduler_class = {
611 .class_name = "Scheduler",
612 .version = LIBAVUTIL_VERSION_INT,
613};
614
616{
617 Scheduler *sch;
618 int ret;
619
620 sch = av_mallocz(sizeof(*sch));
621 if (!sch)
622 return NULL;
623
624 sch->class = &scheduler_class;
625 sch->sdp_auto = 1;
626
628 if (ret)
629 goto fail;
630
632 if (ret)
633 goto fail;
634
635 ret = pthread_mutex_init(&sch->finish_lock, NULL);
636 if (ret)
637 goto fail;
638
639 ret = pthread_cond_init(&sch->finish_cond, NULL);
640 if (ret)
641 goto fail;
642
643 return sch;
644fail:
645 sch_free(&sch);
646 return NULL;
647}
648
649int sch_sdp_filename(Scheduler *sch, const char *sdp_filename)
650{
651 av_freep(&sch->sdp_filename);
652 sch->sdp_filename = av_strdup(sdp_filename);
653 return sch->sdp_filename ? 0 : AVERROR(ENOMEM);
654}
655
656static const AVClass sch_mux_class = {
657 .class_name = "SchMux",
658 .version = LIBAVUTIL_VERSION_INT,
659 .parent_log_context_offset = offsetof(SchMux, task.func_arg),
660};
661
662int sch_add_mux(Scheduler *sch, SchThreadFunc func, int (*init)(void *),
663 void *arg, int sdp_auto, unsigned thread_queue_size)
664{
665 const unsigned idx = sch->nb_mux;
666
667 SchMux *mux;
668 int ret;
669
670 ret = GROW_ARRAY(sch->mux, sch->nb_mux);
671 if (ret < 0)
672 return ret;
673
674 mux = &sch->mux[idx];
675 mux->class = &sch_mux_class;
676 mux->init = init;
677 mux->queue_size = thread_queue_size;
678
679 task_init(sch, &mux->task, SCH_NODE_TYPE_MUX, idx, func, arg);
680
681 sch->sdp_auto &= sdp_auto;
682
683 return idx;
684}
685
686int sch_add_mux_stream(Scheduler *sch, unsigned mux_idx)
687{
688 SchMux *mux;
689 SchMuxStream *ms;
690 unsigned stream_idx;
691 int ret;
692
693 av_assert0(mux_idx < sch->nb_mux);
694 mux = &sch->mux[mux_idx];
695
696 ret = GROW_ARRAY(mux->streams, mux->nb_streams);
697 if (ret < 0)
698 return ret;
699 stream_idx = mux->nb_streams - 1;
700
701 ms = &mux->streams[stream_idx];
702
703 ms->pre_mux_queue.fifo = av_fifo_alloc2(8, sizeof(AVPacket*), 0);
704 if (!ms->pre_mux_queue.fifo)
705 return AVERROR(ENOMEM);
706
708
709 return stream_idx;
710}
711
712static const AVClass sch_demux_class = {
713 .class_name = "SchDemux",
714 .version = LIBAVUTIL_VERSION_INT,
715 .parent_log_context_offset = offsetof(SchDemux, task.func_arg),
716};
717
719{
720 const unsigned idx = sch->nb_demux;
721
722 SchDemux *d;
723 int ret;
724
725 ret = GROW_ARRAY(sch->demux, sch->nb_demux);
726 if (ret < 0)
727 return ret;
728
729 d = &sch->demux[idx];
730
731 task_init(sch, &d->task, SCH_NODE_TYPE_DEMUX, idx, func, ctx);
732
735 if (!d->send_pkt)
736 return AVERROR(ENOMEM);
737
738 ret = waiter_init(&d->waiter);
739 if (ret < 0)
740 return ret;
741
742 return idx;
743}
744
745int sch_add_demux_stream(Scheduler *sch, unsigned demux_idx)
746{
747 SchDemux *d;
748 int ret;
749
750 av_assert0(demux_idx < sch->nb_demux);
751 d = &sch->demux[demux_idx];
752
753 ret = GROW_ARRAY(d->streams, d->nb_streams);
754 return ret < 0 ? ret : d->nb_streams - 1;
755}
756
757int sch_add_dec_output(Scheduler *sch, unsigned dec_idx)
758{
759 SchDec *dec;
760 int ret;
761
762 av_assert0(dec_idx < sch->nb_dec);
763 dec = &sch->dec[dec_idx];
764
765 ret = GROW_ARRAY(dec->outputs, dec->nb_outputs);
766 if (ret < 0)
767 return ret;
768
769 return dec->nb_outputs - 1;
770}
771
772static const AVClass sch_dec_class = {
773 .class_name = "SchDec",
774 .version = LIBAVUTIL_VERSION_INT,
775 .parent_log_context_offset = offsetof(SchDec, task.func_arg),
776};
777
778int sch_add_dec(Scheduler *sch, SchThreadFunc func, void *ctx, int send_end_ts)
779{
780 const unsigned idx = sch->nb_dec;
781
782 SchDec *dec;
783 int ret;
784
785 ret = GROW_ARRAY(sch->dec, sch->nb_dec);
786 if (ret < 0)
787 return ret;
788
789 dec = &sch->dec[idx];
790
791 task_init(sch, &dec->task, SCH_NODE_TYPE_DEC, idx, func, ctx);
792
793 dec->class = &sch_dec_class;
794 dec->send_frame = av_frame_alloc();
795 if (!dec->send_frame)
796 return AVERROR(ENOMEM);
797
798 ret = sch_add_dec_output(sch, idx);
799 if (ret < 0)
800 return ret;
801
802 ret = queue_alloc(&dec->queue, 1, 0, QUEUE_PACKETS);
803 if (ret < 0)
804 return ret;
805
806 if (send_end_ts) {
808 if (ret < 0)
809 return ret;
810 }
811
812 return idx;
813}
814
815static const AVClass sch_enc_class = {
816 .class_name = "SchEnc",
817 .version = LIBAVUTIL_VERSION_INT,
818 .parent_log_context_offset = offsetof(SchEnc, task.func_arg),
819};
820
822 int (*open_cb)(void *opaque, const AVFrame *frame))
823{
824 const unsigned idx = sch->nb_enc;
825
826 SchEnc *enc;
827 int ret;
828
829 ret = GROW_ARRAY(sch->enc, sch->nb_enc);
830 if (ret < 0)
831 return ret;
832
833 enc = &sch->enc[idx];
834
835 enc->class = &sch_enc_class;
836 enc->open_cb = open_cb;
837 enc->sq_idx[0] = -1;
838 enc->sq_idx[1] = -1;
839
840 task_init(sch, &enc->task, SCH_NODE_TYPE_ENC, idx, func, ctx);
841
842 enc->send_pkt = av_packet_alloc();
843 if (!enc->send_pkt)
844 return AVERROR(ENOMEM);
845
846 ret = queue_alloc(&enc->queue, 1, 0, QUEUE_FRAMES);
847 if (ret < 0)
848 return ret;
849
850 return idx;
851}
852
853static const AVClass sch_fg_class = {
854 .class_name = "SchFilterGraph",
855 .version = LIBAVUTIL_VERSION_INT,
856 .parent_log_context_offset = offsetof(SchFilterGraph, task.func_arg),
857};
858
859int sch_add_filtergraph(Scheduler *sch, unsigned nb_inputs, unsigned nb_outputs,
860 SchThreadFunc func, void *ctx)
861{
862 const unsigned idx = sch->nb_filters;
863
864 SchFilterGraph *fg;
865 int ret;
866
867 ret = GROW_ARRAY(sch->filters, sch->nb_filters);
868 if (ret < 0)
869 return ret;
870 fg = &sch->filters[idx];
871
872 fg->class = &sch_fg_class;
873
874 task_init(sch, &fg->task, SCH_NODE_TYPE_FILTER_IN, idx, func, ctx);
875
876 if (nb_inputs) {
877 fg->inputs = av_calloc(nb_inputs, sizeof(*fg->inputs));
878 if (!fg->inputs)
879 return AVERROR(ENOMEM);
880 fg->nb_inputs = nb_inputs;
881 }
882
883 if (nb_outputs) {
884 fg->outputs = av_calloc(nb_outputs, sizeof(*fg->outputs));
885 if (!fg->outputs)
886 return AVERROR(ENOMEM);
887 fg->nb_outputs = nb_outputs;
888 }
889
890 ret = waiter_init(&fg->waiter);
891 if (ret < 0)
892 return ret;
893
894 ret = queue_alloc(&fg->queue, fg->nb_inputs + 1, 0, QUEUE_FRAMES);
895 if (ret < 0)
896 return ret;
897
898 return idx;
899}
900
901int sch_add_sq_enc(Scheduler *sch, uint64_t buf_size_us, void *logctx)
902{
903 SchSyncQueue *sq;
904 int ret;
905
906 ret = GROW_ARRAY(sch->sq_enc, sch->nb_sq_enc);
907 if (ret < 0)
908 return ret;
909 sq = &sch->sq_enc[sch->nb_sq_enc - 1];
910
911 sq->sq = sq_alloc(SYNC_QUEUE_FRAMES, buf_size_us, logctx);
912 if (!sq->sq)
913 return AVERROR(ENOMEM);
914
915 sq->frame = av_frame_alloc();
916 if (!sq->frame)
917 return AVERROR(ENOMEM);
918
919 ret = pthread_mutex_init(&sq->lock, NULL);
920 if (ret)
921 return AVERROR(ret);
922
923 return sq - sch->sq_enc;
924}
925
926int sch_sq_add_enc(Scheduler *sch, unsigned sq_idx, unsigned enc_idx,
927 int limiting, uint64_t max_frames)
928{
929 SchSyncQueue *sq;
930 SchEnc *enc;
931 int ret;
932
933 av_assert0(sq_idx < sch->nb_sq_enc);
934 sq = &sch->sq_enc[sq_idx];
935
936 av_assert0(enc_idx < sch->nb_enc);
937 enc = &sch->enc[enc_idx];
938
939 ret = GROW_ARRAY(sq->enc_idx, sq->nb_enc_idx);
940 if (ret < 0)
941 return ret;
942 sq->enc_idx[sq->nb_enc_idx - 1] = enc_idx;
943
944 ret = sq_add_stream(sq->sq, limiting);
945 if (ret < 0)
946 return ret;
947
948 enc->sq_idx[0] = sq_idx;
949 enc->sq_idx[1] = ret;
950
951 if (max_frames != INT64_MAX)
952 sq_limit_frames(sq->sq, enc->sq_idx[1], max_frames);
953
954 return 0;
955}
956
958{
959 int ret;
960
961 switch (src.type) {
962 case SCH_NODE_TYPE_DEMUX: {
963 SchDemuxStream *ds;
964
965 av_assert0(src.idx < sch->nb_demux &&
966 src.idx_stream < sch->demux[src.idx].nb_streams);
967 ds = &sch->demux[src.idx].streams[src.idx_stream];
968
969 ret = GROW_ARRAY(ds->dst, ds->nb_dst);
970 if (ret < 0)
971 return ret;
972
973 ds->dst[ds->nb_dst - 1] = dst;
974
975 // demuxed packets go to decoding or streamcopy
976 switch (dst.type) {
977 case SCH_NODE_TYPE_DEC: {
978 SchDec *dec;
979
980 av_assert0(dst.idx < sch->nb_dec);
981 dec = &sch->dec[dst.idx];
982
983 av_assert0(!dec->src.type);
984 dec->src = src;
985 break;
986 }
987 case SCH_NODE_TYPE_MUX: {
988 SchMuxStream *ms;
989
990 av_assert0(dst.idx < sch->nb_mux &&
991 dst.idx_stream < sch->mux[dst.idx].nb_streams);
992 ms = &sch->mux[dst.idx].streams[dst.idx_stream];
993
994 av_assert0(!ms->src.type);
995 ms->src = src;
996
997 break;
998 }
999 default: av_assert0(0);
1000 }
1001
1002 break;
1003 }
1004 case SCH_NODE_TYPE_DEC: {
1005 SchDec *dec;
1006 SchDecOutput *o;
1007
1008 av_assert0(src.idx < sch->nb_dec);
1009 dec = &sch->dec[src.idx];
1010
1011 av_assert0(src.idx_stream < dec->nb_outputs);
1012 o = &dec->outputs[src.idx_stream];
1013
1014 ret = GROW_ARRAY(o->dst, o->nb_dst);
1015 if (ret < 0)
1016 return ret;
1017
1018 o->dst[o->nb_dst - 1] = dst;
1019
1020 // decoded frames go to filters or encoding
1021 switch (dst.type) {
1023 SchFilterIn *fi;
1024
1025 av_assert0(dst.idx < sch->nb_filters &&
1026 dst.idx_stream < sch->filters[dst.idx].nb_inputs);
1027 fi = &sch->filters[dst.idx].inputs[dst.idx_stream];
1028
1029 av_assert0(!fi->src.type);
1030 fi->src = src;
1031 break;
1032 }
1033 case SCH_NODE_TYPE_ENC: {
1034 SchEnc *enc;
1035
1036 av_assert0(dst.idx < sch->nb_enc);
1037 enc = &sch->enc[dst.idx];
1038
1039 av_assert0(!enc->src.type);
1040 enc->src = src;
1041 break;
1042 }
1043 default: av_assert0(0);
1044 }
1045
1046 break;
1047 }
1049 SchFilterOut *fo;
1050
1051 av_assert0(src.idx < sch->nb_filters &&
1052 src.idx_stream < sch->filters[src.idx].nb_outputs);
1053 fo = &sch->filters[src.idx].outputs[src.idx_stream];
1054
1055 av_assert0(!fo->dst.type);
1056 fo->dst = dst;
1057
1058 // filtered frames go to encoding or another filtergraph
1059 switch (dst.type) {
1060 case SCH_NODE_TYPE_ENC: {
1061 SchEnc *enc;
1062
1063 av_assert0(dst.idx < sch->nb_enc);
1064 enc = &sch->enc[dst.idx];
1065
1066 av_assert0(!enc->src.type);
1067 enc->src = src;
1068 break;
1069 }
1071 SchFilterIn *fi;
1072
1073 av_assert0(dst.idx < sch->nb_filters &&
1074 dst.idx_stream < sch->filters[dst.idx].nb_inputs);
1075 fi = &sch->filters[dst.idx].inputs[dst.idx_stream];
1076
1077 av_assert0(!fi->src.type);
1078 fi->src = src;
1079 break;
1080 }
1081 default: av_assert0(0);
1082 }
1083
1084
1085 break;
1086 }
1087 case SCH_NODE_TYPE_ENC: {
1088 SchEnc *enc;
1089
1090 av_assert0(src.idx < sch->nb_enc);
1091 enc = &sch->enc[src.idx];
1092
1093 ret = GROW_ARRAY(enc->dst, enc->nb_dst);
1094 if (ret < 0)
1095 return ret;
1096
1097 enc->dst[enc->nb_dst - 1] = dst;
1098
1099 // encoding packets go to muxing or decoding
1100 switch (dst.type) {
1101 case SCH_NODE_TYPE_MUX: {
1102 SchMuxStream *ms;
1103
1104 av_assert0(dst.idx < sch->nb_mux &&
1105 dst.idx_stream < sch->mux[dst.idx].nb_streams);
1106 ms = &sch->mux[dst.idx].streams[dst.idx_stream];
1107
1108 av_assert0(!ms->src.type);
1109 ms->src = src;
1110
1111 break;
1112 }
1113 case SCH_NODE_TYPE_DEC: {
1114 SchDec *dec;
1115
1116 av_assert0(dst.idx < sch->nb_dec);
1117 dec = &sch->dec[dst.idx];
1118
1119 av_assert0(!dec->src.type);
1120 dec->src = src;
1121
1122 break;
1123 }
1124 default: av_assert0(0);
1125 }
1126
1127 break;
1128 }
1129 default: av_assert0(0);
1130 }
1131
1132 return 0;
1133}
1134
1135static int mux_task_start(SchMux *mux)
1136{
1137 int ret = 0;
1138
1139 ret = task_start(&mux->task);
1140 if (ret < 0)
1141 return ret;
1142
1143 /* flush the pre-muxing queues */
1144 while (1) {
1145 int min_stream = -1;
1146 Timestamp min_ts = { .ts = AV_NOPTS_VALUE };
1147
1148 AVPacket *pkt;
1149
1150 // find the stream with the earliest dts or EOF in pre-muxing queue
1151 for (unsigned i = 0; i < mux->nb_streams; i++) {
1152 SchMuxStream *ms = &mux->streams[i];
1153
1154 if (av_fifo_peek(ms->pre_mux_queue.fifo, &pkt, 1, 0) < 0)
1155 continue;
1156
1157 if (!pkt || pkt->dts == AV_NOPTS_VALUE) {
1158 min_stream = i;
1159 break;
1160 }
1161
1162 if (min_ts.ts == AV_NOPTS_VALUE ||
1163 av_compare_ts(min_ts.ts, min_ts.tb, pkt->dts, pkt->time_base) > 0) {
1164 min_stream = i;
1165 min_ts = (Timestamp){ .ts = pkt->dts, .tb = pkt->time_base };
1166 }
1167 }
1168
1169 if (min_stream >= 0) {
1170 SchMuxStream *ms = &mux->streams[min_stream];
1171
1172 ret = av_fifo_read(ms->pre_mux_queue.fifo, &pkt, 1);
1173 av_assert0(ret >= 0);
1174
1175 if (pkt) {
1176 if (!ms->init_eof)
1177 ret = tq_send(mux->queue, min_stream, pkt);
1179 if (ret == AVERROR_EOF)
1180 ms->init_eof = 1;
1181 else if (ret < 0)
1182 return ret;
1183 } else
1184 tq_send_finish(mux->queue, min_stream);
1185
1186 continue;
1187 }
1188
1189 break;
1190 }
1191
1192 atomic_store(&mux->mux_started, 1);
1193
1194 return 0;
1195}
1196
1197int print_sdp(const char *filename);
1198
1199static int mux_init(Scheduler *sch, SchMux *mux)
1200{
1201 int ret;
1202
1203 ret = mux->init(mux->task.func_arg);
1204 if (ret < 0)
1205 return ret;
1206
1207 sch->nb_mux_ready++;
1208
1209 if (sch->sdp_filename || sch->sdp_auto) {
1210 if (sch->nb_mux_ready < sch->nb_mux)
1211 return 0;
1212
1213 ret = print_sdp(sch->sdp_filename);
1214 if (ret < 0) {
1215 av_log(sch, AV_LOG_ERROR, "Error writing the SDP.\n");
1216 return ret;
1217 }
1218
1219 /* SDP is written only after all the muxers are ready, so now we
1220 * start ALL the threads */
1221 for (unsigned i = 0; i < sch->nb_mux; i++) {
1222 ret = mux_task_start(&sch->mux[i]);
1223 if (ret < 0)
1224 return ret;
1225 }
1226 } else {
1227 ret = mux_task_start(mux);
1228 if (ret < 0)
1229 return ret;
1230 }
1231
1232 return 0;
1233}
1234
1235void sch_mux_stream_buffering(Scheduler *sch, unsigned mux_idx, unsigned stream_idx,
1236 size_t data_threshold, int max_packets)
1237{
1238 SchMux *mux;
1239 SchMuxStream *ms;
1240
1241 av_assert0(mux_idx < sch->nb_mux);
1242 mux = &sch->mux[mux_idx];
1243
1244 av_assert0(stream_idx < mux->nb_streams);
1245 ms = &mux->streams[stream_idx];
1246
1247 ms->pre_mux_queue.max_packets = max_packets;
1248 ms->pre_mux_queue.data_threshold = data_threshold;
1249}
1250
1251int sch_mux_stream_ready(Scheduler *sch, unsigned mux_idx, unsigned stream_idx)
1252{
1253 SchMux *mux;
1254 int ret = 0;
1255
1256 av_assert0(mux_idx < sch->nb_mux);
1257 mux = &sch->mux[mux_idx];
1258
1259 av_assert0(stream_idx < mux->nb_streams);
1260
1262
1264
1265 // this may be called during initialization - do not start
1266 // threads before sch_start() is called
1267 if (++mux->nb_streams_ready == mux->nb_streams &&
1268 sch->state >= SCH_STATE_STARTED)
1269 ret = mux_init(sch, mux);
1270
1272
1273 return ret;
1274}
1275
1276int sch_mux_sub_heartbeat_add(Scheduler *sch, unsigned mux_idx, unsigned stream_idx,
1277 unsigned dec_idx)
1278{
1279 SchMux *mux;
1280 SchMuxStream *ms;
1281 int ret = 0;
1282
1283 av_assert0(mux_idx < sch->nb_mux);
1284 mux = &sch->mux[mux_idx];
1285
1286 av_assert0(stream_idx < mux->nb_streams);
1287 ms = &mux->streams[stream_idx];
1288
1290 if (ret < 0)
1291 return ret;
1292
1293 av_assert0(dec_idx < sch->nb_dec);
1294 ms->sub_heartbeat_dst[ms->nb_sub_heartbeat_dst - 1] = dec_idx;
1295
1296 if (!mux->sub_heartbeat_pkt) {
1298 if (!mux->sub_heartbeat_pkt)
1299 return AVERROR(ENOMEM);
1300 }
1301
1302 return 0;
1303}
1304
1305enum {
1306 UNCHOKE_DEMUX = (1 << 0),
1307 UNCHOKE_FILTER = (1 << 1),
1308
1310};
1311
1312static void unchoke_for_stream(Scheduler *sch, SchedulerNode src, int flags);
1313
1314// Unchoke any filter graphs that are downstream of this node, to prevent it
1315// from getting stuck trying to push data to a full queue
1317{
1318 SchFilterGraph *fg;
1319 SchDec *dec;
1320 SchEnc *enc;
1321 switch (dst->type) {
1322 case SCH_NODE_TYPE_DEC:
1323 dec = &sch->dec[dst->idx];
1324 for (int i = 0; i < dec->nb_outputs; i++)
1325 unchoke_downstream(sch, dec->outputs[i].dst);
1326 break;
1327 case SCH_NODE_TYPE_ENC:
1328 enc = &sch->enc[dst->idx];
1329 for (int i = 0; i < enc->nb_dst; i++)
1330 unchoke_downstream(sch, &enc->dst[i]);
1331 break;
1332 case SCH_NODE_TYPE_MUX:
1333 // muxers are never choked
1334 break;
1336 fg = &sch->filters[dst->idx];
1337 if (fg->best_input == fg->nb_inputs) {
1338 fg->waiter.choked_next = 0;
1339 } else {
1340 // ensure that this filter graph is not stuck waiting for
1341 // input from a different upstream source
1343 }
1344 break;
1345 default:
1346 av_unreachable("Invalid destination node type?");
1347 break;
1348 }
1349}
1350
1352{
1353 while (1) {
1354 SchFilterGraph *fg;
1355 SchDemux *demux;
1356 switch (src.type) {
1358 // fed directly by a demuxer (i.e. not through a filtergraph)
1359 demux = &sch->demux[src.idx];
1360 if (demux->waiter.choked_next == 0)
1361 return; // prevent infinite loop
1362 if (flags & UNCHOKE_DEMUX) {
1363 demux->waiter.choked_next = 0;
1364 for (int i = 0; i < demux->nb_streams; i++)
1365 unchoke_downstream(sch, demux->streams[i].dst);
1366 }
1367 return;
1368 case SCH_NODE_TYPE_DEC:
1369 src = sch->dec[src.idx].src;
1370 continue;
1371 case SCH_NODE_TYPE_ENC:
1372 src = sch->enc[src.idx].src;
1373 continue;
1375 fg = &sch->filters[src.idx];
1376 // the filtergraph contains internal sources and
1377 // requested to be scheduled directly
1378 if (fg->best_input == fg->nb_inputs) {
1379 if (flags & UNCHOKE_FILTER)
1380 fg->waiter.choked_next = 0;
1381 return;
1382 }
1383 src = fg->inputs[fg->best_input].src;
1384 continue;
1385 default:
1386 av_unreachable("Invalid source node type?");
1387 return;
1388 }
1389 }
1390}
1391
1392static void choke_demux(const Scheduler *sch, int demux_id, int choked)
1393{
1394 av_assert1(demux_id < sch->nb_demux);
1395 SchDemux *demux = &sch->demux[demux_id];
1396
1397 for (int i = 0; i < demux->nb_streams; i++) {
1398 SchedulerNode *dst = demux->streams[i].dst;
1399 SchFilterGraph *fg;
1400
1401 switch (dst->type) {
1402 case SCH_NODE_TYPE_DEC:
1403 tq_choke(sch->dec[dst->idx].queue, choked);
1404 break;
1405 case SCH_NODE_TYPE_ENC:
1406 tq_choke(sch->enc[dst->idx].queue, choked);
1407 break;
1408 case SCH_NODE_TYPE_MUX:
1409 break;
1411 fg = &sch->filters[dst->idx];
1412 if (fg->nb_inputs == 1)
1413 tq_choke(fg->queue, choked);
1414 break;
1415 default:
1416 av_unreachable("Invalid destination node type?");
1417 break;
1418 }
1419 }
1420}
1421
1423{
1424 int64_t dts;
1425 int have_unchoked = 0;
1426
1427 // on termination request all waiters are choked,
1428 // we are not to unchoke them
1429 if (atomic_load(&sch->terminate))
1430 return;
1431
1432 dts = trailing_dts(sch);
1433
1434 atomic_store(&sch->last_dts, progressing_dts(sch, 0));
1435
1436 // initialize our internal state
1437#define RESET_WAITER(field) \
1438 do { \
1439 for (unsigned i = 0; i < sch->nb_##field; i++) { \
1440 SchWaiter *w = &sch->field[i].waiter; \
1441 w->choked_prev = atomic_load(&w->choked); \
1442 w->choked_next = 1; \
1443 } \
1444 } while (0)
1445
1446 RESET_WAITER(demux);
1448
1449 // figure out the sources that are allowed to proceed
1450 for (unsigned i = 0; i < sch->nb_mux; i++) {
1451 SchMux *mux = &sch->mux[i];
1452
1453 for (unsigned j = 0; j < mux->nb_streams; j++) {
1454 SchMuxStream *ms = &mux->streams[j];
1455
1456 // unblock sources for output streams that are not finished
1457 // and not too far ahead of the trailing stream
1458 if (ms->source_finished)
1459 continue;
1460 if (dts == AV_NOPTS_VALUE && ms->last_dts != AV_NOPTS_VALUE)
1461 continue;
1462 if (dts != AV_NOPTS_VALUE && ms->last_dts - dts >= SCHEDULE_TOLERANCE)
1463 continue;
1464
1465 // resolve the source to unchoke
1467 have_unchoked = 1;
1468 }
1469 }
1470
1471 // also unchoke any sources feeding into closed filter graph inputs, so
1472 // that they can observe the downstream EOF
1473 for (unsigned i = 0; i < sch->nb_filters; i++) {
1474 SchFilterGraph *fg = &sch->filters[i];
1475
1476 for (unsigned j = 0; j < fg->nb_inputs; j++) {
1477 SchFilterIn *fi = &fg->inputs[j];
1478 if (fi->receive_finished && !fi->send_finished)
1480 }
1481 }
1482
1483 // make sure to unchoke at least one source, if still available
1484#define UNCHOKE_ONCE(field) \
1485 do { \
1486 for (unsigned i = 0; !have_unchoked && i < sch->nb_##field; i++) { \
1487 SchWaiter *w = &sch->field[i].waiter; \
1488 if (!sch->field[i].task_exited) { \
1489 w->choked_next = 0; \
1490 have_unchoked = 1; \
1491 break; \
1492 } \
1493 } \
1494 } while (0)
1495
1496 UNCHOKE_ONCE(demux);
1498
1499#define UPDATE_WAITER(field) \
1500 do { \
1501 for (unsigned i = 0; i < sch->nb_##field; i++) { \
1502 SchWaiter *w = &sch->field[i].waiter; \
1503 if (w->choked_prev != w->choked_next) { \
1504 waiter_set(w, w->choked_next); \
1505 if (offsetof(Scheduler, field) == offsetof(Scheduler, demux)) \
1506 choke_demux(sch, i, w->choked_next); \
1507 } \
1508 } \
1509 } while (0)
1510
1511 UPDATE_WAITER(demux);
1513}
1514
1515enum {
1519};
1520
1521// Finds the filtergraph or muxer upstream of a scheduler node
1523{
1524 while (1) {
1525 switch (src.type) {
1528 return src;
1529 case SCH_NODE_TYPE_DEC:
1530 src = sch->dec[src.idx].src;
1531 continue;
1532 case SCH_NODE_TYPE_ENC:
1533 src = sch->enc[src.idx].src;
1534 continue;
1535 default:
1536 av_unreachable("Invalid source node type?");
1537 return (SchedulerNode) {0};
1538 }
1539 }
1540}
1541
1542static int
1544 uint8_t *filters_visited, SchedulerNode *filters_stack)
1545{
1546 unsigned nb_filters_stack = 0;
1547
1548 memset(filters_visited, 0, sch->nb_filters * sizeof(*filters_visited));
1549
1550 while (1) {
1551 const SchFilterGraph *fg = &sch->filters[src.idx];
1552
1553 filters_visited[src.idx] = CYCLE_NODE_STARTED;
1554
1555 // descend into every input, depth first
1556 if (src.idx_stream < fg->nb_inputs) {
1557 const SchFilterIn *fi = &fg->inputs[src.idx_stream++];
1558 SchedulerNode node = src_filtergraph(sch, fi->src);
1559
1560 // connected to demuxer, no cycles possible
1561 if (node.type == SCH_NODE_TYPE_DEMUX)
1562 continue;
1563
1564 // otherwise connected to another filtergraph
1566
1567 // found a cycle
1568 if (filters_visited[node.idx] == CYCLE_NODE_STARTED)
1569 return AVERROR(EINVAL);
1570
1571 // place current position on stack and descend
1572 av_assert0(nb_filters_stack < sch->nb_filters);
1573 filters_stack[nb_filters_stack++] = src;
1574 src = (SchedulerNode){ .idx = node.idx, .idx_stream = 0 };
1575 continue;
1576 }
1577
1578 filters_visited[src.idx] = CYCLE_NODE_DONE;
1579
1580 // previous search finished,
1581 if (nb_filters_stack) {
1582 src = filters_stack[--nb_filters_stack];
1583 continue;
1584 }
1585 return 0;
1586 }
1587}
1588
1589static int check_acyclic(Scheduler *sch)
1590{
1591 uint8_t *filters_visited = NULL;
1592 SchedulerNode *filters_stack = NULL;
1593
1594 int ret = 0;
1595
1596 if (!sch->nb_filters)
1597 return 0;
1598
1599 filters_visited = av_malloc_array(sch->nb_filters, sizeof(*filters_visited));
1600 if (!filters_visited)
1601 return AVERROR(ENOMEM);
1602
1603 filters_stack = av_malloc_array(sch->nb_filters, sizeof(*filters_stack));
1604 if (!filters_stack) {
1605 ret = AVERROR(ENOMEM);
1606 goto fail;
1607 }
1608
1609 // trace the transcoding graph upstream from every filtegraph
1610 for (unsigned i = 0; i < sch->nb_filters; i++) {
1611 ret = check_acyclic_for_output(sch, (SchedulerNode){ .idx = i },
1612 filters_visited, filters_stack);
1613 if (ret < 0) {
1614 av_log(&sch->filters[i], AV_LOG_ERROR, "Transcoding graph has a cycle\n");
1615 goto fail;
1616 }
1617 }
1618
1619fail:
1620 av_freep(&filters_visited);
1621 av_freep(&filters_stack);
1622 return ret;
1623}
1624
1625static int start_prepare(Scheduler *sch)
1626{
1627 int ret;
1628
1629 for (unsigned i = 0; i < sch->nb_demux; i++) {
1630 SchDemux *d = &sch->demux[i];
1631
1632 for (unsigned j = 0; j < d->nb_streams; j++) {
1633 SchDemuxStream *ds = &d->streams[j];
1634
1635 if (!ds->nb_dst) {
1637 "Demuxer stream %u not connected to any sink\n", j);
1638 return AVERROR(EINVAL);
1639 }
1640
1641 ds->dst_finished = av_calloc(ds->nb_dst, sizeof(*ds->dst_finished));
1642 if (!ds->dst_finished)
1643 return AVERROR(ENOMEM);
1644 }
1645 }
1646
1647 for (unsigned i = 0; i < sch->nb_dec; i++) {
1648 SchDec *dec = &sch->dec[i];
1649
1650 if (!dec->src.type) {
1651 av_log(dec, AV_LOG_ERROR,
1652 "Decoder not connected to a source\n");
1653 return AVERROR(EINVAL);
1654 }
1655
1656 for (unsigned j = 0; j < dec->nb_outputs; j++) {
1657 SchDecOutput *o = &dec->outputs[j];
1658
1659 if (!o->nb_dst) {
1660 av_log(dec, AV_LOG_ERROR,
1661 "Decoder output %u not connected to any sink\n", j);
1662 return AVERROR(EINVAL);
1663 }
1664
1665 o->dst_finished = av_calloc(o->nb_dst, sizeof(*o->dst_finished));
1666 if (!o->dst_finished)
1667 return AVERROR(ENOMEM);
1668 }
1669 }
1670
1671 for (unsigned i = 0; i < sch->nb_enc; i++) {
1672 SchEnc *enc = &sch->enc[i];
1673
1674 if (!enc->src.type) {
1675 av_log(enc, AV_LOG_ERROR,
1676 "Encoder not connected to a source\n");
1677 return AVERROR(EINVAL);
1678 }
1679 if (!enc->nb_dst) {
1680 av_log(enc, AV_LOG_ERROR,
1681 "Encoder not connected to any sink\n");
1682 return AVERROR(EINVAL);
1683 }
1684
1685 enc->dst_finished = av_calloc(enc->nb_dst, sizeof(*enc->dst_finished));
1686 if (!enc->dst_finished)
1687 return AVERROR(ENOMEM);
1688 }
1689
1690 for (unsigned i = 0; i < sch->nb_mux; i++) {
1691 SchMux *mux = &sch->mux[i];
1692
1693 for (unsigned j = 0; j < mux->nb_streams; j++) {
1694 SchMuxStream *ms = &mux->streams[j];
1695
1696 if (!ms->src.type) {
1697 av_log(mux, AV_LOG_ERROR,
1698 "Muxer stream #%u not connected to a source\n", j);
1699 return AVERROR(EINVAL);
1700 }
1701 }
1702
1703 ret = queue_alloc(&mux->queue, mux->nb_streams, mux->queue_size,
1705 if (ret < 0)
1706 return ret;
1707 }
1708
1709 for (unsigned i = 0; i < sch->nb_filters; i++) {
1710 SchFilterGraph *fg = &sch->filters[i];
1711
1712 for (unsigned j = 0; j < fg->nb_inputs; j++) {
1713 SchFilterIn *fi = &fg->inputs[j];
1714
1715 if (!fi->src.type) {
1716 av_log(fg, AV_LOG_ERROR,
1717 "Filtergraph input %u not connected to a source\n", j);
1718 return AVERROR(EINVAL);
1719 }
1720 }
1721
1722 for (unsigned j = 0; j < fg->nb_outputs; j++) {
1723 SchFilterOut *fo = &fg->outputs[j];
1724
1725 if (!fo->dst.type) {
1726 av_log(fg, AV_LOG_ERROR,
1727 "Filtergraph %u output %u not connected to a sink\n", i, j);
1728 return AVERROR(EINVAL);
1729 }
1730 }
1731 }
1732
1733 // Check that the transcoding graph has no cycles.
1734 ret = check_acyclic(sch);
1735 if (ret < 0)
1736 return ret;
1737
1738 return 0;
1739}
1740
1742{
1743 int ret;
1744
1745 ret = start_prepare(sch);
1746 if (ret < 0)
1747 return ret;
1748
1750 sch->state = SCH_STATE_STARTED;
1751
1752 for (unsigned i = 0; i < sch->nb_mux; i++) {
1753 SchMux *mux = &sch->mux[i];
1754
1755 if (mux->nb_streams_ready == mux->nb_streams) {
1756 ret = mux_init(sch, mux);
1757 if (ret < 0)
1758 goto fail;
1759 }
1760 }
1761
1762 for (unsigned i = 0; i < sch->nb_enc; i++) {
1763 SchEnc *enc = &sch->enc[i];
1764
1765 ret = task_start(&enc->task);
1766 if (ret < 0)
1767 goto fail;
1768 }
1769
1770 for (unsigned i = 0; i < sch->nb_filters; i++) {
1771 SchFilterGraph *fg = &sch->filters[i];
1772
1773 ret = task_start(&fg->task);
1774 if (ret < 0)
1775 goto fail;
1776 }
1777
1778 for (unsigned i = 0; i < sch->nb_dec; i++) {
1779 SchDec *dec = &sch->dec[i];
1780
1781 ret = task_start(&dec->task);
1782 if (ret < 0)
1783 goto fail;
1784 }
1785
1786 for (unsigned i = 0; i < sch->nb_demux; i++) {
1787 SchDemux *d = &sch->demux[i];
1788
1789 if (!d->nb_streams)
1790 continue;
1791
1792 ret = task_start(&d->task);
1793 if (ret < 0)
1794 goto fail;
1795 }
1796
1800
1801 return 0;
1802fail:
1803 sch_stop(sch, NULL);
1804 return ret;
1805}
1806
1807int sch_wait(Scheduler *sch, uint64_t timeout_us, int64_t *transcode_ts)
1808{
1809 int ret;
1810
1811 // convert delay to absolute timestamp
1812 timeout_us += av_gettime();
1813
1815
1816 if (sch->nb_mux_done < sch->nb_mux) {
1817 struct timespec tv = { .tv_sec = timeout_us / 1000000,
1818 .tv_nsec = (timeout_us % 1000000) * 1000 };
1820 }
1821
1822 // abort transcoding if any task failed
1823 ret = sch->nb_mux_done == sch->nb_mux || sch->task_failed;
1824
1826
1827 *transcode_ts = atomic_load(&sch->last_dts);
1828
1829 return ret;
1830}
1831
1832static int enc_open(Scheduler *sch, SchEnc *enc, const AVFrame *frame)
1833{
1834 int ret;
1835
1836 ret = enc->open_cb(enc->task.func_arg, frame);
1837 if (ret < 0)
1838 return ret;
1839
1840 // ret>0 signals audio frame size, which means sync queue must
1841 // have been enabled during encoder creation
1842 if (ret > 0) {
1843 SchSyncQueue *sq;
1844
1845 av_assert0(enc->sq_idx[0] >= 0);
1846 sq = &sch->sq_enc[enc->sq_idx[0]];
1847
1849
1850 sq_frame_samples(sq->sq, enc->sq_idx[1], ret);
1851
1853 }
1854
1855 return 0;
1856}
1857
1859{
1860 int ret;
1861
1862 if (!frame) {
1863 tq_send_finish(enc->queue, 0);
1864 return 0;
1865 }
1866
1867 if (enc->in_finished)
1868 return AVERROR_EOF;
1869
1870 ret = tq_send(enc->queue, 0, frame);
1871 if (ret < 0)
1872 enc->in_finished = 1;
1873
1874 return ret;
1875}
1876
1878{
1879 SchSyncQueue *sq = &sch->sq_enc[enc->sq_idx[0]];
1880 int ret = 0;
1881
1882 // inform the scheduling code that no more input will arrive along this path;
1883 // this is necessary because the sync queue may not send an EOF downstream
1884 // until other streams finish
1885 // TODO: consider a cleaner way of passing this information through
1886 // the pipeline
1887 if (!frame) {
1888 for (unsigned i = 0; i < enc->nb_dst; i++) {
1889 SchMux *mux;
1890 SchMuxStream *ms;
1891
1892 if (enc->dst[i].type != SCH_NODE_TYPE_MUX)
1893 continue;
1894
1895 mux = &sch->mux[enc->dst[i].idx];
1896 ms = &mux->streams[enc->dst[i].idx_stream];
1897
1899
1900 ms->source_finished = 1;
1902
1904 }
1905 }
1906
1908
1909 ret = sq_send(sq->sq, enc->sq_idx[1], SQFRAME(frame));
1910 if (ret < 0)
1911 goto finish;
1912
1913 while (1) {
1914 SchEnc *enc;
1915
1916 // TODO: the SQ API should be extended to allow returning EOF
1917 // for individual streams
1918 ret = sq_receive(sq->sq, -1, SQFRAME(sq->frame));
1919 if (ret < 0) {
1920 ret = (ret == AVERROR(EAGAIN)) ? 0 : ret;
1921 break;
1922 }
1923
1924 enc = &sch->enc[sq->enc_idx[ret]];
1925 ret = send_to_enc_thread(sch, enc, sq->frame);
1926 if (ret < 0) {
1927 av_frame_unref(sq->frame);
1928 if (ret != AVERROR_EOF)
1929 break;
1930
1931 sq_send(sq->sq, enc->sq_idx[1], SQFRAME(NULL));
1932 continue;
1933 }
1934 }
1935
1936 if (ret < 0) {
1937 // close all encoders fed from this sync queue
1938 for (unsigned i = 0; i < sq->nb_enc_idx; i++) {
1939 int err = send_to_enc_thread(sch, &sch->enc[sq->enc_idx[i]], NULL);
1940
1941 // if the sync queue error is EOF and closing the encoder
1942 // produces a more serious error, make sure to pick the latter
1943 ret = err_merge((ret == AVERROR_EOF && err < 0) ? 0 : ret, err);
1944 }
1945 }
1946
1947finish:
1949
1950 return ret;
1951}
1952
1953static int send_to_enc(Scheduler *sch, SchEnc *enc, AVFrame *frame)
1954{
1955 if (enc->open_cb && frame && !enc->opened) {
1956 int ret = enc_open(sch, enc, frame);
1957 if (ret < 0)
1958 return ret;
1959 enc->opened = 1;
1960
1961 // discard empty frames that only carry encoder init parameters
1962 if (!frame->buf[0]) {
1964 return 0;
1965 }
1966 }
1967
1968 return (enc->sq_idx[0] >= 0) ?
1969 send_to_enc_sq (sch, enc, frame) :
1970 send_to_enc_thread(sch, enc, frame);
1971}
1972
1974{
1975 PreMuxQueue *q = &ms->pre_mux_queue;
1976 AVPacket *tmp_pkt = NULL;
1977 int ret;
1978
1979 if (!av_fifo_can_write(q->fifo)) {
1980 size_t packets = av_fifo_can_read(q->fifo);
1981 size_t pkt_size = pkt ? pkt->size : 0;
1982 int thresh_reached = (q->data_size + pkt_size) > q->data_threshold;
1983 size_t max_packets = thresh_reached ? q->max_packets : SIZE_MAX;
1984 size_t new_size = FFMIN(2 * packets, max_packets);
1985
1986 if (new_size <= packets) {
1987 av_log(mux, AV_LOG_ERROR,
1988 "Too many packets buffered for output stream.\n");
1990 }
1991 ret = av_fifo_grow2(q->fifo, new_size - packets);
1992 if (ret < 0)
1993 return ret;
1994 }
1995
1996 if (pkt) {
1997 tmp_pkt = av_packet_alloc();
1998 if (!tmp_pkt)
1999 return AVERROR(ENOMEM);
2000
2001 av_packet_move_ref(tmp_pkt, pkt);
2002 q->data_size += tmp_pkt->size;
2003 }
2004 av_fifo_write(q->fifo, &tmp_pkt, 1);
2005
2006 return 0;
2007}
2008
2009static int send_to_mux(Scheduler *sch, SchMux *mux, unsigned stream_idx,
2010 AVPacket *pkt)
2011{
2012 SchMuxStream *ms = &mux->streams[stream_idx];
2013 int64_t dts = (pkt && pkt->dts != AV_NOPTS_VALUE) ?
2014 av_rescale_q(pkt->dts + pkt->duration, pkt->time_base, AV_TIME_BASE_Q) :
2016
2017 // queue the packet if the muxer cannot be started yet
2018 if (!atomic_load(&mux->mux_started)) {
2019 int queued = 0;
2020
2021 // the muxer could have started between the above atomic check and
2022 // locking the mutex, then this block falls through to normal send path
2024
2025 if (!atomic_load(&mux->mux_started)) {
2026 int ret = mux_queue_packet(mux, ms, pkt);
2027 queued = ret < 0 ? ret : 1;
2028 }
2029
2031
2032 if (queued < 0)
2033 return queued;
2034 else if (queued)
2035 goto update_schedule;
2036 }
2037
2038 if (pkt) {
2039 int ret;
2040
2041 if (ms->init_eof)
2042 return AVERROR_EOF;
2043
2044 ret = tq_send(mux->queue, stream_idx, pkt);
2045 if (ret < 0)
2046 return ret;
2047 } else
2048 tq_send_finish(mux->queue, stream_idx);
2049
2050update_schedule:
2051 // TODO: use atomics to check whether this changes trailing dts
2052 // to avoid locking unnecessarily
2053 if (dts != AV_NOPTS_VALUE || !pkt) {
2055
2056 if (pkt) ms->last_dts = dts;
2057 else ms->source_finished = 1;
2058
2060
2062 }
2063
2064 return 0;
2065}
2066
2067static int
2069 uint8_t *dst_finished, AVPacket *pkt, unsigned flags)
2070{
2071 int ret;
2072
2073 if (*dst_finished)
2074 return AVERROR_EOF;
2075
2076 if (pkt && dst.type == SCH_NODE_TYPE_MUX &&
2079 pkt = NULL;
2080 }
2081
2082 if (!pkt)
2083 goto finish;
2084
2085 ret = (dst.type == SCH_NODE_TYPE_MUX) ?
2086 send_to_mux(sch, &sch->mux[dst.idx], dst.idx_stream, pkt) :
2087 tq_send(sch->dec[dst.idx].queue, 0, pkt);
2088 if (ret == AVERROR_EOF)
2089 goto finish;
2090
2091 return ret;
2092
2093finish:
2094 if (dst.type == SCH_NODE_TYPE_MUX)
2095 send_to_mux(sch, &sch->mux[dst.idx], dst.idx_stream, NULL);
2096 else
2097 tq_send_finish(sch->dec[dst.idx].queue, 0);
2098
2099 *dst_finished = 1;
2100 return AVERROR_EOF;
2101}
2102
2104 AVPacket *pkt, unsigned flags)
2105{
2106 unsigned nb_done = 0;
2107
2108 for (unsigned i = 0; i < ds->nb_dst; i++) {
2109 AVPacket *to_send = pkt;
2110 uint8_t *finished = &ds->dst_finished[i];
2111
2112 int ret;
2113
2114 // sending a packet consumes it, so make a temporary reference if needed
2115 if (pkt && i < ds->nb_dst - 1) {
2116 to_send = d->send_pkt;
2117
2118 ret = av_packet_ref(to_send, pkt);
2119 if (ret < 0)
2120 return ret;
2121 }
2122
2123 ret = demux_stream_send_to_dst(sch, ds->dst[i], finished, to_send, flags);
2124 if (to_send)
2125 av_packet_unref(to_send);
2126 if (ret == AVERROR_EOF)
2127 nb_done++;
2128 else if (ret < 0)
2129 return ret;
2130 }
2131
2132 return (nb_done == ds->nb_dst) ? AVERROR_EOF : 0;
2133}
2134
2136{
2137 Timestamp max_end_ts = (Timestamp){ .ts = AV_NOPTS_VALUE };
2138
2139 av_assert0(!pkt->buf && !pkt->data && !pkt->side_data_elems);
2140
2141 for (unsigned i = 0; i < d->nb_streams; i++) {
2142 SchDemuxStream *ds = &d->streams[i];
2143
2144 for (unsigned j = 0; j < ds->nb_dst; j++) {
2145 const SchedulerNode *dst = &ds->dst[j];
2146 SchDec *dec;
2147 int ret;
2148
2149 if (ds->dst_finished[j] || dst->type != SCH_NODE_TYPE_DEC)
2150 continue;
2151
2152 dec = &sch->dec[dst->idx];
2153
2154 ret = tq_send(dec->queue, 0, pkt);
2155 if (ret < 0)
2156 return ret;
2157
2158 if (dec->queue_end_ts) {
2159 Timestamp ts;
2160 ret = av_thread_message_queue_recv(dec->queue_end_ts, &ts, 0);
2161 if (ret < 0)
2162 return ret;
2163
2164 if (max_end_ts.ts == AV_NOPTS_VALUE ||
2165 (ts.ts != AV_NOPTS_VALUE &&
2166 av_compare_ts(max_end_ts.ts, max_end_ts.tb, ts.ts, ts.tb) < 0))
2167 max_end_ts = ts;
2168
2169 }
2170 }
2171 }
2172
2173 pkt->pts = max_end_ts.ts;
2174 pkt->time_base = max_end_ts.tb;
2175
2176 return 0;
2177}
2178
2179int sch_demux_send(Scheduler *sch, unsigned demux_idx, AVPacket *pkt,
2180 unsigned flags)
2181{
2182 SchDemux *d;
2183 int terminate;
2184
2185 av_assert0(demux_idx < sch->nb_demux);
2186 d = &sch->demux[demux_idx];
2187
2188 terminate = waiter_wait(sch, &d->waiter);
2189 if (terminate)
2190 return AVERROR_EXIT;
2191
2192 // flush the downstreams after seek
2193 if (pkt->stream_index == -1)
2194 return demux_flush(sch, d, pkt);
2195
2196 av_assert0(pkt->stream_index < d->nb_streams);
2197
2198 return demux_send_for_stream(sch, d, &d->streams[pkt->stream_index], pkt, flags);
2199}
2200
2201static int demux_done(Scheduler *sch, unsigned demux_idx)
2202{
2203 SchDemux *d = &sch->demux[demux_idx];
2204 int ret = 0;
2205
2206 for (unsigned i = 0; i < d->nb_streams; i++) {
2207 int err = demux_send_for_stream(sch, d, &d->streams[i], NULL, 0);
2208 if (err != AVERROR_EOF)
2209 ret = err_merge(ret, err);
2210 }
2211
2213
2214 d->task_exited = 1;
2215
2217
2219
2220 return ret;
2221}
2222
2223int sch_mux_receive(Scheduler *sch, unsigned mux_idx, AVPacket *pkt)
2224{
2225 SchMux *mux;
2226 int ret, stream_idx;
2227
2228 av_assert0(mux_idx < sch->nb_mux);
2229 mux = &sch->mux[mux_idx];
2230
2231 ret = tq_receive(mux->queue, &stream_idx, pkt, 0);
2232 pkt->stream_index = stream_idx;
2233 return ret;
2234}
2235
2236void sch_mux_receive_finish(Scheduler *sch, unsigned mux_idx, unsigned stream_idx)
2237{
2238 SchMux *mux;
2239
2240 av_assert0(mux_idx < sch->nb_mux);
2241 mux = &sch->mux[mux_idx];
2242
2243 av_assert0(stream_idx < mux->nb_streams);
2244 tq_receive_finish(mux->queue, stream_idx);
2245
2247 mux->streams[stream_idx].source_finished = 1;
2248
2250
2252}
2253
2254int sch_mux_sub_heartbeat(Scheduler *sch, unsigned mux_idx, unsigned stream_idx,
2255 const AVPacket *pkt)
2256{
2257 SchMux *mux;
2258 SchMuxStream *ms;
2259
2260 av_assert0(mux_idx < sch->nb_mux);
2261 mux = &sch->mux[mux_idx];
2262
2263 av_assert0(stream_idx < mux->nb_streams);
2264 ms = &mux->streams[stream_idx];
2265
2266 for (unsigned i = 0; i < ms->nb_sub_heartbeat_dst; i++) {
2267 SchDec *dst = &sch->dec[ms->sub_heartbeat_dst[i]];
2268 int ret;
2269
2271 if (ret < 0)
2272 return ret;
2273
2274 tq_send(dst->queue, 0, mux->sub_heartbeat_pkt);
2275 }
2276
2277 return 0;
2278}
2279
2280static int mux_done(Scheduler *sch, unsigned mux_idx)
2281{
2282 SchMux *mux = &sch->mux[mux_idx];
2283
2285
2286 for (unsigned i = 0; i < mux->nb_streams; i++) {
2287 tq_receive_finish(mux->queue, i);
2288 mux->streams[i].source_finished = 1;
2289 }
2290
2292
2294
2296
2297 av_assert0(sch->nb_mux_done < sch->nb_mux);
2298 sch->nb_mux_done++;
2299
2301
2303
2304 return 0;
2305}
2306
2307int sch_dec_receive(Scheduler *sch, unsigned dec_idx, AVPacket *pkt)
2308{
2309 SchDec *dec;
2310 int ret, dummy;
2311
2312 av_assert0(dec_idx < sch->nb_dec);
2313 dec = &sch->dec[dec_idx];
2314
2315 // the decoder should have given us post-flush end timestamp in pkt
2316 if (dec->expect_end_ts) {
2317 Timestamp ts = (Timestamp){ .ts = pkt->pts, .tb = pkt->time_base };
2318 ret = av_thread_message_queue_send(dec->queue_end_ts, &ts, 0);
2319 if (ret < 0)
2320 return ret;
2321
2322 dec->expect_end_ts = 0;
2323 }
2324
2325 ret = tq_receive(dec->queue, &dummy, pkt, 0);
2326 av_assert0(dummy <= 0);
2327
2328 // got a flush packet, on the next call to this function the decoder
2329 // will give us post-flush end timestamp
2330 if (ret >= 0 && !pkt->data && !pkt->side_data_elems && dec->queue_end_ts)
2331 dec->expect_end_ts = 1;
2332
2333 return ret;
2334}
2335
2337 unsigned in_idx, AVFrame *frame)
2338{
2339 if (frame)
2340 return tq_send(fg->queue, in_idx, frame);
2341
2343
2344 if (!fg->inputs[in_idx].send_finished) {
2345 fg->inputs[in_idx].send_finished = 1;
2346 tq_send_finish(fg->queue, in_idx);
2347
2348 // close the control stream when all actual inputs are done
2349 if (++fg->nb_inputs_finished_send == fg->nb_inputs)
2350 tq_send_finish(fg->queue, fg->nb_inputs);
2351
2353 }
2354
2356 return 0;
2357}
2358
2360 uint8_t *dst_finished, AVFrame *frame)
2361{
2362 int ret;
2363
2364 if (*dst_finished)
2365 return AVERROR_EOF;
2366
2367 if (!frame)
2368 goto finish;
2369
2370 ret = (dst.type == SCH_NODE_TYPE_FILTER_IN) ?
2371 send_to_filter(sch, &sch->filters[dst.idx], dst.idx_stream, frame) :
2372 send_to_enc(sch, &sch->enc[dst.idx], frame);
2373 if (ret == AVERROR_EOF)
2374 goto finish;
2375
2376 return ret;
2377
2378finish:
2379 if (dst.type == SCH_NODE_TYPE_FILTER_IN)
2380 send_to_filter(sch, &sch->filters[dst.idx], dst.idx_stream, NULL);
2381 else
2382 send_to_enc(sch, &sch->enc[dst.idx], NULL);
2383
2384 *dst_finished = 1;
2385
2386 return AVERROR_EOF;
2387}
2388
2389int sch_dec_send(Scheduler *sch, unsigned dec_idx,
2390 unsigned out_idx, AVFrame *frame)
2391{
2392 SchDec *dec;
2393 SchDecOutput *o;
2394 int ret;
2395 unsigned nb_done = 0;
2396
2397 av_assert0(dec_idx < sch->nb_dec);
2398 dec = &sch->dec[dec_idx];
2399
2400 av_assert0(out_idx < dec->nb_outputs);
2401 o = &dec->outputs[out_idx];
2402
2403 for (unsigned i = 0; i < o->nb_dst; i++) {
2404 uint8_t *finished = &o->dst_finished[i];
2405 AVFrame *to_send = frame;
2406
2407 // sending a frame consumes it, so make a temporary reference if needed
2408 if (i < o->nb_dst - 1) {
2409 to_send = dec->send_frame;
2410
2411 // frame may sometimes contain props only,
2412 // e.g. to signal EOF timestamp
2413 ret = frame->buf[0] ? av_frame_ref(to_send, frame) :
2414 av_frame_copy_props(to_send, frame);
2415 if (ret < 0)
2416 return ret;
2417 }
2418
2419 ret = dec_send_to_dst(sch, o->dst[i], finished, to_send);
2420 if (ret < 0) {
2421 av_frame_unref(to_send);
2422 if (ret == AVERROR_EOF) {
2423 nb_done++;
2424 continue;
2425 }
2426 return ret;
2427 }
2428 }
2429
2430 return (nb_done == o->nb_dst) ? AVERROR_EOF : 0;
2431}
2432
2433static int dec_done(Scheduler *sch, unsigned dec_idx)
2434{
2435 SchDec *dec = &sch->dec[dec_idx];
2436 int ret = 0;
2437
2438 tq_receive_finish(dec->queue, 0);
2439
2440 // make sure our source does not get stuck waiting for end timestamps
2441 // that will never arrive
2442 if (dec->queue_end_ts)
2444
2445 for (unsigned i = 0; i < dec->nb_outputs; i++) {
2446 SchDecOutput *o = &dec->outputs[i];
2447
2448 for (unsigned j = 0; j < o->nb_dst; j++) {
2449 int err = dec_send_to_dst(sch, o->dst[j], &o->dst_finished[j], NULL);
2450 if (err < 0 && err != AVERROR_EOF)
2451 ret = err_merge(ret, err);
2452 }
2453 }
2454
2455 return ret;
2456}
2457
2458int sch_enc_receive(Scheduler *sch, unsigned enc_idx, AVFrame *frame)
2459{
2460 SchEnc *enc;
2461 int ret, dummy;
2462
2463 av_assert0(enc_idx < sch->nb_enc);
2464 enc = &sch->enc[enc_idx];
2465
2466 ret = tq_receive(enc->queue, &dummy, frame, 0);
2467 av_assert0(dummy <= 0);
2468
2469 return ret;
2470}
2471
2473 uint8_t *dst_finished, AVPacket *pkt)
2474{
2475 int ret;
2476
2477 if (*dst_finished)
2478 return AVERROR_EOF;
2479
2480 if (!pkt)
2481 goto finish;
2482
2483 ret = (dst.type == SCH_NODE_TYPE_MUX) ?
2484 send_to_mux(sch, &sch->mux[dst.idx], dst.idx_stream, pkt) :
2485 tq_send(sch->dec[dst.idx].queue, 0, pkt);
2486 if (ret == AVERROR_EOF)
2487 goto finish;
2488
2489 return ret;
2490
2491finish:
2492 if (dst.type == SCH_NODE_TYPE_MUX)
2493 send_to_mux(sch, &sch->mux[dst.idx], dst.idx_stream, NULL);
2494 else
2495 tq_send_finish(sch->dec[dst.idx].queue, 0);
2496
2497 *dst_finished = 1;
2498
2499 return AVERROR_EOF;
2500}
2501
2502int sch_enc_send(Scheduler *sch, unsigned enc_idx, AVPacket *pkt)
2503{
2504 SchEnc *enc;
2505 int ret;
2506
2507 av_assert0(enc_idx < sch->nb_enc);
2508 enc = &sch->enc[enc_idx];
2509
2510 for (unsigned i = 0; i < enc->nb_dst; i++) {
2511 uint8_t *finished = &enc->dst_finished[i];
2512 AVPacket *to_send = pkt;
2513
2514 // sending a packet consumes it, so make a temporary reference if needed
2515 if (i < enc->nb_dst - 1) {
2516 to_send = enc->send_pkt;
2517
2518 ret = av_packet_ref(to_send, pkt);
2519 if (ret < 0)
2520 return ret;
2521 }
2522
2523 ret = enc_send_to_dst(sch, enc->dst[i], finished, to_send);
2524 if (ret < 0) {
2525 av_packet_unref(to_send);
2526 if (ret == AVERROR_EOF)
2527 continue;
2528 return ret;
2529 }
2530 }
2531
2532 return 0;
2533}
2534
2535static int enc_done(Scheduler *sch, unsigned enc_idx)
2536{
2537 SchEnc *enc = &sch->enc[enc_idx];
2538 int ret = 0;
2539
2540 tq_receive_finish(enc->queue, 0);
2541
2542 for (unsigned i = 0; i < enc->nb_dst; i++) {
2543 int err = enc_send_to_dst(sch, enc->dst[i], &enc->dst_finished[i], NULL);
2544 if (err < 0 && err != AVERROR_EOF)
2545 ret = err_merge(ret, err);
2546 }
2547
2548 return ret;
2549}
2550
2551int sch_filter_receive(Scheduler *sch, unsigned fg_idx,
2552 unsigned *in_idx, AVFrame *frame)
2553{
2554 SchFilterGraph *fg;
2555 int ret, idx;
2556
2557 av_assert0(fg_idx < sch->nb_filters);
2558 fg = &sch->filters[fg_idx];
2559
2560 av_assert0(*in_idx <= fg->nb_inputs);
2561
2562 // update scheduling to account for desired input stream, if it changed
2563 //
2564 // this check needs no locking because only the filtering thread
2565 // updates this value
2566 if (*in_idx != fg->best_input) {
2568
2569 fg->best_input = *in_idx;
2571
2573 }
2574
2575 if (*in_idx == fg->nb_inputs) {
2576 // drain incoming frames before waiting, to avoid blocking downstream
2578 if (ret >= 0) {
2579 av_assert0(idx >= 0);
2580 *in_idx = idx;
2581 return 0;
2582 }
2583
2584 int terminate = waiter_wait(sch, &fg->waiter);
2585 return terminate ? AVERROR_EOF : AVERROR(EAGAIN);
2586 }
2587
2588 while (1) {
2589 ret = tq_receive(fg->queue, &idx, frame, 0);
2590 if (idx < 0)
2591 return AVERROR_EOF;
2592 else if (ret >= 0) {
2593 *in_idx = idx;
2594 return 0;
2595 }
2596
2597 // disregard EOFs for specific streams - they should always be
2598 // preceded by an EOF frame
2599 }
2600}
2601
2602void sch_filter_receive_finish(Scheduler *sch, unsigned fg_idx, unsigned in_idx)
2603{
2604 SchFilterGraph *fg;
2605 SchFilterIn *fi;
2606
2607 av_assert0(fg_idx < sch->nb_filters);
2608 fg = &sch->filters[fg_idx];
2609
2610 av_assert0(in_idx < fg->nb_inputs);
2611 fi = &fg->inputs[in_idx];
2612
2614
2615 if (!fi->receive_finished) {
2616 fi->receive_finished = 1;
2617 tq_receive_finish(fg->queue, in_idx);
2618
2619 // close the control stream when all actual inputs are done
2620 if (++fg->nb_inputs_finished_receive == fg->nb_inputs)
2622
2624 }
2625
2627}
2628
2629int sch_filter_send(Scheduler *sch, unsigned fg_idx, unsigned out_idx, AVFrame *frame)
2630{
2631 SchFilterGraph *fg;
2633 int ret;
2634
2635 av_assert0(fg_idx < sch->nb_filters);
2636 fg = &sch->filters[fg_idx];
2637
2638 av_assert0(out_idx < fg->nb_outputs);
2639 dst = fg->outputs[out_idx].dst;
2640
2641 if (dst.type == SCH_NODE_TYPE_ENC) {
2642 ret = send_to_enc(sch, &sch->enc[dst.idx], frame);
2643 if (ret == AVERROR_EOF)
2644 send_to_enc(sch, &sch->enc[dst.idx], NULL);
2645 } else {
2646 ret = send_to_filter(sch, &sch->filters[dst.idx], dst.idx_stream, frame);
2647 if (ret == AVERROR_EOF)
2648 send_to_filter(sch, &sch->filters[dst.idx], dst.idx_stream, NULL);
2649 }
2650 return ret;
2651}
2652
2653static int filter_done(Scheduler *sch, unsigned fg_idx)
2654{
2655 SchFilterGraph *fg = &sch->filters[fg_idx];
2656 int ret = 0;
2657
2658 for (unsigned i = 0; i <= fg->nb_inputs; i++)
2660
2661 for (unsigned i = 0; i < fg->nb_outputs; i++) {
2662 SchedulerNode dst = fg->outputs[i].dst;
2663 int err = (dst.type == SCH_NODE_TYPE_ENC) ?
2664 send_to_enc (sch, &sch->enc[dst.idx], NULL) :
2665 send_to_filter(sch, &sch->filters[dst.idx], dst.idx_stream, NULL);
2666
2667 if (err < 0 && err != AVERROR_EOF)
2668 ret = err_merge(ret, err);
2669 }
2670
2672
2673 fg->task_exited = 1;
2674
2676
2678
2679 return ret;
2680}
2681
2682int sch_filter_command(Scheduler *sch, unsigned fg_idx, AVFrame *frame)
2683{
2684 SchFilterGraph *fg;
2685
2686 av_assert0(fg_idx < sch->nb_filters);
2687 fg = &sch->filters[fg_idx];
2688
2689 return send_to_filter(sch, fg, fg->nb_inputs, frame);
2690}
2691
2692void sch_filter_choke_inputs(Scheduler *sch, unsigned fg_idx)
2693{
2694 SchFilterGraph *fg;
2695 av_assert0(fg_idx < sch->nb_filters);
2696 fg = &sch->filters[fg_idx];
2697
2699 fg->best_input = fg->nb_inputs;
2702}
2703
2705{
2706 switch (node.type) {
2707 case SCH_NODE_TYPE_DEMUX: return demux_done (sch, node.idx);
2708 case SCH_NODE_TYPE_MUX: return mux_done (sch, node.idx);
2709 case SCH_NODE_TYPE_DEC: return dec_done (sch, node.idx);
2710 case SCH_NODE_TYPE_ENC: return enc_done (sch, node.idx);
2711 case SCH_NODE_TYPE_FILTER_IN: return filter_done(sch, node.idx);
2712 default: av_unreachable("Invalid node type?");
2713 }
2714}
2715
2716static void *task_wrapper(void *arg)
2717{
2718 SchTask *task = arg;
2719 Scheduler *sch = task->parent;
2720 int ret;
2721 int err = 0;
2722
2723 ret = task->func(task->func_arg);
2724 if (ret < 0)
2726 "Task finished with error: %s\n", av_err2str(ret));
2727
2728 err = task_cleanup(sch, task->node);
2729 ret = err_merge(ret, err);
2730
2731 // EOF is considered normal termination
2732 if (ret == AVERROR_EOF)
2733 ret = 0;
2734 if (ret < 0) {
2736 sch->task_failed = 1;
2739 }
2740
2741 if (ret < 0)
2743 "Terminating thread with error: %s\n", av_err2str(ret));
2744 else
2746 "Terminating thread with success\n");
2747
2748 return (void*)(intptr_t)ret;
2749}
2750
2751static int task_stop(Scheduler *sch, SchTask *task)
2752{
2753 int ret;
2754 void *thread_ret;
2755
2756 if (!task->parent)
2757 return 0;
2758
2759 if (!task->thread_running)
2760 return task_cleanup(sch, task->node);
2761
2762 ret = pthread_join(task->thread, &thread_ret);
2763 av_assert0(ret == 0);
2764
2765 task->thread_running = 0;
2766
2767 return (intptr_t)thread_ret;
2768}
2769
2770int sch_stop(Scheduler *sch, int64_t *finish_ts)
2771{
2772 int ret = 0, err;
2773
2774 if (sch->state != SCH_STATE_STARTED)
2775 return 0;
2776
2777 atomic_store(&sch->terminate, 1);
2778
2779 // Ensure no other thread is currently in schedule_update_locked while
2780 // we are choking all demuxers
2782
2783 for (unsigned type = 0; type < 2; type++)
2784 for (unsigned i = 0; i < (type ? sch->nb_demux : sch->nb_filters); i++) {
2785 SchWaiter *w = type ? &sch->demux[i].waiter : &sch->filters[i].waiter;
2786 waiter_set(w, 1);
2787 if (type)
2788 choke_demux(sch, i, 0); // unfreeze to allow draining
2789 }
2790
2792
2793 for (unsigned i = 0; i < sch->nb_demux; i++) {
2794 SchDemux *d = &sch->demux[i];
2795
2796 err = task_stop(sch, &d->task);
2797 ret = err_merge(ret, err);
2798 }
2799
2800 for (unsigned i = 0; i < sch->nb_dec; i++) {
2801 SchDec *dec = &sch->dec[i];
2802
2803 err = task_stop(sch, &dec->task);
2804 ret = err_merge(ret, err);
2805 }
2806
2807 for (unsigned i = 0; i < sch->nb_filters; i++) {
2808 SchFilterGraph *fg = &sch->filters[i];
2809
2810 err = task_stop(sch, &fg->task);
2811 ret = err_merge(ret, err);
2812 }
2813
2814 for (unsigned i = 0; i < sch->nb_enc; i++) {
2815 SchEnc *enc = &sch->enc[i];
2816
2817 err = task_stop(sch, &enc->task);
2818 ret = err_merge(ret, err);
2819 }
2820
2821 for (unsigned i = 0; i < sch->nb_mux; i++) {
2822 SchMux *mux = &sch->mux[i];
2823
2824 err = task_stop(sch, &mux->task);
2825 ret = err_merge(ret, err);
2826 }
2827
2828 if (finish_ts)
2829 *finish_ts = progressing_dts(sch, 1);
2830
2831 sch->state = SCH_STATE_STOPPED;
2832
2833 return ret;
2834}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
#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:608
#define atomic_store(object, desired)
Definition stdatomic.h:85
intptr_t atomic_int
Definition stdatomic.h:55
intptr_t atomic_int_least64_t
Definition stdatomic.h:68
#define atomic_load(object)
Definition stdatomic.h:93
#define atomic_init(obj, value)
Definition stdatomic.h:33
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:502
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)
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,...
static int dummy
Definition ffplay.c:3754
static unsigned int nb_streams
Definition ffprobe.c:352
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:434
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition packet.c:491
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:442
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition packet.c:397
#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:496
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:278
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#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
cl_device_type type
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition jacosubdec.c:66
const char * arg
Definition jacosubdec.c:65
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:264
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
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:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
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
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:90
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:333
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:628
int sq_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
Read a frame from the queue.
Definition sync_queue.c:586
void sq_free(SyncQueue **psq)
Definition sync_queue.c:671
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:640
int sq_add_stream(SyncQueue *sq, int limiting)
Add a new stream to the sync queue.
Definition sync_queue.c:598
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:654
#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