forked from FFmpeg/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg_sched.c
2520 lines (1920 loc) · 64.9 KB
/
ffmpeg_sched.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Inter-thread scheduling/synchronization.
* Copyright (c) 2023 Anton Khirnov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdatomic.h>
#include <stddef.h>
#include <stdint.h>
#include "cmdutils.h"
#include "ffmpeg_sched.h"
#include "ffmpeg_utils.h"
#include "sync_queue.h"
#include "thread_queue.h"
#include "libavcodec/packet.h"
#include "libavutil/avassert.h"
#include "libavutil/error.h"
#include "libavutil/fifo.h"
#include "libavutil/frame.h"
#include "libavutil/mem.h"
#include "libavutil/thread.h"
#include "libavutil/threadmessage.h"
#include "libavutil/time.h"
// 100 ms
// FIXME: some other value? make this dynamic?
#define SCHEDULE_TOLERANCE (100 * 1000)
enum QueueType {
QUEUE_PACKETS,
QUEUE_FRAMES,
};
typedef struct SchWaiter {
pthread_mutex_t lock;
pthread_cond_t cond;
atomic_int choked;
// the following are internal state of schedule_update_locked() and must not
// be accessed outside of it
int choked_prev;
int choked_next;
} SchWaiter;
typedef struct SchTask {
Scheduler *parent;
SchedulerNode node;
SchThreadFunc func;
void *func_arg;
pthread_t thread;
int thread_running;
} SchTask;
typedef struct SchDec {
const AVClass *class;
SchedulerNode src;
SchedulerNode *dst;
uint8_t *dst_finished;
unsigned nb_dst;
SchTask task;
// Queue for receiving input packets, one stream.
ThreadQueue *queue;
// Queue for sending post-flush end timestamps back to the source
AVThreadMessageQueue *queue_end_ts;
int expect_end_ts;
// temporary storage used by sch_dec_send()
AVFrame *send_frame;
} SchDec;
typedef struct SchSyncQueue {
SyncQueue *sq;
AVFrame *frame;
pthread_mutex_t lock;
unsigned *enc_idx;
unsigned nb_enc_idx;
} SchSyncQueue;
typedef struct SchEnc {
const AVClass *class;
SchedulerNode src;
SchedulerNode *dst;
uint8_t *dst_finished;
unsigned nb_dst;
// [0] - index of the sync queue in Scheduler.sq_enc,
// [1] - index of this encoder in the sq
int sq_idx[2];
/* Opening encoders is somewhat nontrivial due to their interaction with
* sync queues, which are (among other things) responsible for maintaining
* constant audio frame size, when it is required by the encoder.
*
* Opening the encoder requires stream parameters, obtained from the first
* frame. However, that frame cannot be properly chunked by the sync queue
* without knowing the required frame size, which is only available after
* opening the encoder.
*
* This apparent circular dependency is resolved in the following way:
* - the caller creating the encoder gives us a callback which opens the
* encoder and returns the required frame size (if any)
* - when the first frame is sent to the encoder, the sending thread
* - calls this callback, opening the encoder
* - passes the returned frame size to the sync queue
*/
int (*open_cb)(void *opaque, const AVFrame *frame);
int opened;
SchTask task;
// Queue for receiving input frames, one stream.
ThreadQueue *queue;
// tq_send() to queue returned EOF
int in_finished;
// temporary storage used by sch_enc_send()
AVPacket *send_pkt;
} SchEnc;
typedef struct SchDemuxStream {
SchedulerNode *dst;
uint8_t *dst_finished;
unsigned nb_dst;
} SchDemuxStream;
typedef struct SchDemux {
const AVClass *class;
SchDemuxStream *streams;
unsigned nb_streams;
SchTask task;
SchWaiter waiter;
// temporary storage used by sch_demux_send()
AVPacket *send_pkt;
// protected by schedule_lock
int task_exited;
} SchDemux;
typedef struct PreMuxQueue {
/**
* Queue for buffering the packets before the muxer task can be started.
*/
AVFifo *fifo;
/**
* Maximum number of packets in fifo.
*/
int max_packets;
/*
* The size of the AVPackets' buffers in queue.
* Updated when a packet is either pushed or pulled from the queue.
*/
size_t data_size;
/* Threshold after which max_packets will be in effect */
size_t data_threshold;
} PreMuxQueue;
typedef struct SchMuxStream {
SchedulerNode src;
SchedulerNode src_sched;
unsigned *sub_heartbeat_dst;
unsigned nb_sub_heartbeat_dst;
PreMuxQueue pre_mux_queue;
// an EOF was generated while flushing the pre-mux queue
int init_eof;
////////////////////////////////////////////////////////////
// The following are protected by Scheduler.schedule_lock //
/* dts+duration of the last packet sent to this stream
in AV_TIME_BASE_Q */
int64_t last_dts;
// this stream no longer accepts input
int source_finished;
////////////////////////////////////////////////////////////
} SchMuxStream;
typedef struct SchMux {
const AVClass *class;
SchMuxStream *streams;
unsigned nb_streams;
unsigned nb_streams_ready;
int (*init)(void *arg);
SchTask task;
/**
* Set to 1 after starting the muxer task and flushing the
* pre-muxing queues.
* Set either before any tasks have started, or with
* Scheduler.mux_ready_lock held.
*/
atomic_int mux_started;
ThreadQueue *queue;
unsigned queue_size;
AVPacket *sub_heartbeat_pkt;
} SchMux;
typedef struct SchFilterIn {
SchedulerNode src;
SchedulerNode src_sched;
int send_finished;
int receive_finished;
} SchFilterIn;
typedef struct SchFilterOut {
SchedulerNode dst;
} SchFilterOut;
typedef struct SchFilterGraph {
const AVClass *class;
SchFilterIn *inputs;
unsigned nb_inputs;
atomic_uint nb_inputs_finished_send;
unsigned nb_inputs_finished_receive;
SchFilterOut *outputs;
unsigned nb_outputs;
SchTask task;
// input queue, nb_inputs+1 streams
// last stream is control
ThreadQueue *queue;
SchWaiter waiter;
// protected by schedule_lock
unsigned best_input;
int task_exited;
} SchFilterGraph;
struct Scheduler {
const AVClass *class;
SchDemux *demux;
unsigned nb_demux;
SchMux *mux;
unsigned nb_mux;
unsigned nb_mux_ready;
pthread_mutex_t mux_ready_lock;
unsigned nb_mux_done;
pthread_mutex_t mux_done_lock;
pthread_cond_t mux_done_cond;
SchDec *dec;
unsigned nb_dec;
SchEnc *enc;
unsigned nb_enc;
SchSyncQueue *sq_enc;
unsigned nb_sq_enc;
SchFilterGraph *filters;
unsigned nb_filters;
char *sdp_filename;
int sdp_auto;
int transcode_started;
atomic_int terminate;
atomic_int task_failed;
pthread_mutex_t schedule_lock;
atomic_int_least64_t last_dts;
};
/**
* Wait until this task is allowed to proceed.
*
* @retval 0 the caller should proceed
* @retval 1 the caller should terminate
*/
static int waiter_wait(Scheduler *sch, SchWaiter *w)
{
int terminate;
if (!atomic_load(&w->choked))
return 0;
pthread_mutex_lock(&w->lock);
while (atomic_load(&w->choked) && !atomic_load(&sch->terminate))
pthread_cond_wait(&w->cond, &w->lock);
terminate = atomic_load(&sch->terminate);
pthread_mutex_unlock(&w->lock);
return terminate;
}
static void waiter_set(SchWaiter *w, int choked)
{
pthread_mutex_lock(&w->lock);
atomic_store(&w->choked, choked);
pthread_cond_signal(&w->cond);
pthread_mutex_unlock(&w->lock);
}
static int waiter_init(SchWaiter *w)
{
int ret;
atomic_init(&w->choked, 0);
ret = pthread_mutex_init(&w->lock, NULL);
if (ret)
return AVERROR(ret);
ret = pthread_cond_init(&w->cond, NULL);
if (ret)
return AVERROR(ret);
return 0;
}
static void waiter_uninit(SchWaiter *w)
{
pthread_mutex_destroy(&w->lock);
pthread_cond_destroy(&w->cond);
}
static int queue_alloc(ThreadQueue **ptq, unsigned nb_streams, unsigned queue_size,
enum QueueType type)
{
ThreadQueue *tq;
ObjPool *op;
if (queue_size <= 0) {
if (type == QUEUE_FRAMES)
queue_size = DEFAULT_FRAME_THREAD_QUEUE_SIZE;
else
queue_size = DEFAULT_PACKET_THREAD_QUEUE_SIZE;
}
if (type == QUEUE_FRAMES) {
// This queue length is used in the decoder code to ensure that
// there are enough entries in fixed-size frame pools to account
// for frames held in queues inside the ffmpeg utility. If this
// can ever dynamically change then the corresponding decode
// code needs to be updated as well.
av_assert0(queue_size == DEFAULT_FRAME_THREAD_QUEUE_SIZE);
}
op = (type == QUEUE_PACKETS) ? objpool_alloc_packets() :
objpool_alloc_frames();
if (!op)
return AVERROR(ENOMEM);
tq = tq_alloc(nb_streams, queue_size, op,
(type == QUEUE_PACKETS) ? pkt_move : frame_move);
if (!tq) {
objpool_free(&op);
return AVERROR(ENOMEM);
}
*ptq = tq;
return 0;
}
static void *task_wrapper(void *arg);
static int task_stop(SchTask *task)
{
int ret;
void *thread_ret;
if (!task->thread_running)
return 0;
ret = pthread_join(task->thread, &thread_ret);
av_assert0(ret == 0);
task->thread_running = 0;
return (intptr_t)thread_ret;
}
static int task_start(SchTask *task)
{
int ret;
av_log(task->func_arg, AV_LOG_VERBOSE, "Starting thread...\n");
av_assert0(!task->thread_running);
ret = pthread_create(&task->thread, NULL, task_wrapper, task);
if (ret) {
av_log(task->func_arg, AV_LOG_ERROR, "pthread_create() failed: %s\n",
strerror(ret));
return AVERROR(ret);
}
task->thread_running = 1;
return 0;
}
static void task_init(Scheduler *sch, SchTask *task, enum SchedulerNodeType type, unsigned idx,
SchThreadFunc func, void *func_arg)
{
task->parent = sch;
task->node.type = type;
task->node.idx = idx;
task->func = func;
task->func_arg = func_arg;
}
static int64_t trailing_dts(const Scheduler *sch, int count_finished)
{
int64_t min_dts = INT64_MAX;
for (unsigned i = 0; i < sch->nb_mux; i++) {
const SchMux *mux = &sch->mux[i];
for (unsigned j = 0; j < mux->nb_streams; j++) {
const SchMuxStream *ms = &mux->streams[j];
if (ms->source_finished && !count_finished)
continue;
if (ms->last_dts == AV_NOPTS_VALUE)
return AV_NOPTS_VALUE;
min_dts = FFMIN(min_dts, ms->last_dts);
}
}
return min_dts == INT64_MAX ? AV_NOPTS_VALUE : min_dts;
}
int sch_stop(Scheduler *sch, int64_t *finish_ts)
{
int ret = 0, err;
atomic_store(&sch->terminate, 1);
for (unsigned type = 0; type < 2; type++)
for (unsigned i = 0; i < (type ? sch->nb_demux : sch->nb_filters); i++) {
SchWaiter *w = type ? &sch->demux[i].waiter : &sch->filters[i].waiter;
waiter_set(w, 1);
}
for (unsigned i = 0; i < sch->nb_demux; i++) {
SchDemux *d = &sch->demux[i];
err = task_stop(&d->task);
ret = err_merge(ret, err);
}
for (unsigned i = 0; i < sch->nb_dec; i++) {
SchDec *dec = &sch->dec[i];
err = task_stop(&dec->task);
ret = err_merge(ret, err);
}
for (unsigned i = 0; i < sch->nb_filters; i++) {
SchFilterGraph *fg = &sch->filters[i];
err = task_stop(&fg->task);
ret = err_merge(ret, err);
}
for (unsigned i = 0; i < sch->nb_enc; i++) {
SchEnc *enc = &sch->enc[i];
err = task_stop(&enc->task);
ret = err_merge(ret, err);
}
for (unsigned i = 0; i < sch->nb_mux; i++) {
SchMux *mux = &sch->mux[i];
err = task_stop(&mux->task);
ret = err_merge(ret, err);
}
if (finish_ts)
*finish_ts = trailing_dts(sch, 1);
return ret;
}
void sch_free(Scheduler **psch)
{
Scheduler *sch = *psch;
if (!sch)
return;
sch_stop(sch, NULL);
for (unsigned i = 0; i < sch->nb_demux; i++) {
SchDemux *d = &sch->demux[i];
for (unsigned j = 0; j < d->nb_streams; j++) {
SchDemuxStream *ds = &d->streams[j];
av_freep(&ds->dst);
av_freep(&ds->dst_finished);
}
av_freep(&d->streams);
av_packet_free(&d->send_pkt);
waiter_uninit(&d->waiter);
}
av_freep(&sch->demux);
for (unsigned i = 0; i < sch->nb_mux; i++) {
SchMux *mux = &sch->mux[i];
for (unsigned j = 0; j < mux->nb_streams; j++) {
SchMuxStream *ms = &mux->streams[j];
if (ms->pre_mux_queue.fifo) {
AVPacket *pkt;
while (av_fifo_read(ms->pre_mux_queue.fifo, &pkt, 1) >= 0)
av_packet_free(&pkt);
av_fifo_freep2(&ms->pre_mux_queue.fifo);
}
av_freep(&ms->sub_heartbeat_dst);
}
av_freep(&mux->streams);
av_packet_free(&mux->sub_heartbeat_pkt);
tq_free(&mux->queue);
}
av_freep(&sch->mux);
for (unsigned i = 0; i < sch->nb_dec; i++) {
SchDec *dec = &sch->dec[i];
tq_free(&dec->queue);
av_thread_message_queue_free(&dec->queue_end_ts);
av_freep(&dec->dst);
av_freep(&dec->dst_finished);
av_frame_free(&dec->send_frame);
}
av_freep(&sch->dec);
for (unsigned i = 0; i < sch->nb_enc; i++) {
SchEnc *enc = &sch->enc[i];
tq_free(&enc->queue);
av_packet_free(&enc->send_pkt);
av_freep(&enc->dst);
av_freep(&enc->dst_finished);
}
av_freep(&sch->enc);
for (unsigned i = 0; i < sch->nb_sq_enc; i++) {
SchSyncQueue *sq = &sch->sq_enc[i];
sq_free(&sq->sq);
av_frame_free(&sq->frame);
pthread_mutex_destroy(&sq->lock);
av_freep(&sq->enc_idx);
}
av_freep(&sch->sq_enc);
for (unsigned i = 0; i < sch->nb_filters; i++) {
SchFilterGraph *fg = &sch->filters[i];
tq_free(&fg->queue);
av_freep(&fg->inputs);
av_freep(&fg->outputs);
waiter_uninit(&fg->waiter);
}
av_freep(&sch->filters);
av_freep(&sch->sdp_filename);
pthread_mutex_destroy(&sch->schedule_lock);
pthread_mutex_destroy(&sch->mux_ready_lock);
pthread_mutex_destroy(&sch->mux_done_lock);
pthread_cond_destroy(&sch->mux_done_cond);
av_freep(psch);
}
static const AVClass scheduler_class = {
.class_name = "Scheduler",
.version = LIBAVUTIL_VERSION_INT,
};
Scheduler *sch_alloc(void)
{
Scheduler *sch;
int ret;
sch = av_mallocz(sizeof(*sch));
if (!sch)
return NULL;
sch->class = &scheduler_class;
sch->sdp_auto = 1;
ret = pthread_mutex_init(&sch->schedule_lock, NULL);
if (ret)
goto fail;
ret = pthread_mutex_init(&sch->mux_ready_lock, NULL);
if (ret)
goto fail;
ret = pthread_mutex_init(&sch->mux_done_lock, NULL);
if (ret)
goto fail;
ret = pthread_cond_init(&sch->mux_done_cond, NULL);
if (ret)
goto fail;
return sch;
fail:
sch_free(&sch);
return NULL;
}
int sch_sdp_filename(Scheduler *sch, const char *sdp_filename)
{
av_freep(&sch->sdp_filename);
sch->sdp_filename = av_strdup(sdp_filename);
return sch->sdp_filename ? 0 : AVERROR(ENOMEM);
}
static const AVClass sch_mux_class = {
.class_name = "SchMux",
.version = LIBAVUTIL_VERSION_INT,
.parent_log_context_offset = offsetof(SchMux, task.func_arg),
};
int sch_add_mux(Scheduler *sch, SchThreadFunc func, int (*init)(void *),
void *arg, int sdp_auto, unsigned thread_queue_size)
{
const unsigned idx = sch->nb_mux;
SchMux *mux;
int ret;
ret = GROW_ARRAY(sch->mux, sch->nb_mux);
if (ret < 0)
return ret;
mux = &sch->mux[idx];
mux->class = &sch_mux_class;
mux->init = init;
mux->queue_size = thread_queue_size;
task_init(sch, &mux->task, SCH_NODE_TYPE_MUX, idx, func, arg);
sch->sdp_auto &= sdp_auto;
return idx;
}
int sch_add_mux_stream(Scheduler *sch, unsigned mux_idx)
{
SchMux *mux;
SchMuxStream *ms;
unsigned stream_idx;
int ret;
av_assert0(mux_idx < sch->nb_mux);
mux = &sch->mux[mux_idx];
ret = GROW_ARRAY(mux->streams, mux->nb_streams);
if (ret < 0)
return ret;
stream_idx = mux->nb_streams - 1;
ms = &mux->streams[stream_idx];
ms->pre_mux_queue.fifo = av_fifo_alloc2(8, sizeof(AVPacket*), 0);
if (!ms->pre_mux_queue.fifo)
return AVERROR(ENOMEM);
ms->last_dts = AV_NOPTS_VALUE;
return stream_idx;
}
static const AVClass sch_demux_class = {
.class_name = "SchDemux",
.version = LIBAVUTIL_VERSION_INT,
.parent_log_context_offset = offsetof(SchDemux, task.func_arg),
};
int sch_add_demux(Scheduler *sch, SchThreadFunc func, void *ctx)
{
const unsigned idx = sch->nb_demux;
SchDemux *d;
int ret;
ret = GROW_ARRAY(sch->demux, sch->nb_demux);
if (ret < 0)
return ret;
d = &sch->demux[idx];
task_init(sch, &d->task, SCH_NODE_TYPE_DEMUX, idx, func, ctx);
d->class = &sch_demux_class;
d->send_pkt = av_packet_alloc();
if (!d->send_pkt)
return AVERROR(ENOMEM);
ret = waiter_init(&d->waiter);
if (ret < 0)
return ret;
return idx;
}
int sch_add_demux_stream(Scheduler *sch, unsigned demux_idx)
{
SchDemux *d;
int ret;
av_assert0(demux_idx < sch->nb_demux);
d = &sch->demux[demux_idx];
ret = GROW_ARRAY(d->streams, d->nb_streams);
return ret < 0 ? ret : d->nb_streams - 1;
}
static const AVClass sch_dec_class = {
.class_name = "SchDec",
.version = LIBAVUTIL_VERSION_INT,
.parent_log_context_offset = offsetof(SchDec, task.func_arg),
};
int sch_add_dec(Scheduler *sch, SchThreadFunc func, void *ctx,
int send_end_ts)
{
const unsigned idx = sch->nb_dec;
SchDec *dec;
int ret;
ret = GROW_ARRAY(sch->dec, sch->nb_dec);
if (ret < 0)
return ret;
dec = &sch->dec[idx];
task_init(sch, &dec->task, SCH_NODE_TYPE_DEC, idx, func, ctx);
dec->class = &sch_dec_class;
dec->send_frame = av_frame_alloc();
if (!dec->send_frame)
return AVERROR(ENOMEM);
ret = queue_alloc(&dec->queue, 1, 0, QUEUE_PACKETS);
if (ret < 0)
return ret;
if (send_end_ts) {
ret = av_thread_message_queue_alloc(&dec->queue_end_ts, 1, sizeof(Timestamp));
if (ret < 0)
return ret;
}
return idx;
}
static const AVClass sch_enc_class = {
.class_name = "SchEnc",
.version = LIBAVUTIL_VERSION_INT,
.parent_log_context_offset = offsetof(SchEnc, task.func_arg),
};
int sch_add_enc(Scheduler *sch, SchThreadFunc func, void *ctx,
int (*open_cb)(void *opaque, const AVFrame *frame))
{
const unsigned idx = sch->nb_enc;
SchEnc *enc;
int ret;
ret = GROW_ARRAY(sch->enc, sch->nb_enc);
if (ret < 0)
return ret;
enc = &sch->enc[idx];
enc->class = &sch_enc_class;
enc->open_cb = open_cb;
enc->sq_idx[0] = -1;
enc->sq_idx[1] = -1;
task_init(sch, &enc->task, SCH_NODE_TYPE_ENC, idx, func, ctx);
enc->send_pkt = av_packet_alloc();
if (!enc->send_pkt)
return AVERROR(ENOMEM);
ret = queue_alloc(&enc->queue, 1, 0, QUEUE_FRAMES);
if (ret < 0)
return ret;
return idx;
}
static const AVClass sch_fg_class = {
.class_name = "SchFilterGraph",
.version = LIBAVUTIL_VERSION_INT,
.parent_log_context_offset = offsetof(SchFilterGraph, task.func_arg),
};
int sch_add_filtergraph(Scheduler *sch, unsigned nb_inputs, unsigned nb_outputs,
SchThreadFunc func, void *ctx)
{
const unsigned idx = sch->nb_filters;
SchFilterGraph *fg;
int ret;
ret = GROW_ARRAY(sch->filters, sch->nb_filters);
if (ret < 0)
return ret;
fg = &sch->filters[idx];
fg->class = &sch_fg_class;
task_init(sch, &fg->task, SCH_NODE_TYPE_FILTER_IN, idx, func, ctx);
if (nb_inputs) {
fg->inputs = av_calloc(nb_inputs, sizeof(*fg->inputs));
if (!fg->inputs)
return AVERROR(ENOMEM);
fg->nb_inputs = nb_inputs;
}
if (nb_outputs) {
fg->outputs = av_calloc(nb_outputs, sizeof(*fg->outputs));
if (!fg->outputs)
return AVERROR(ENOMEM);
fg->nb_outputs = nb_outputs;
}
ret = waiter_init(&fg->waiter);
if (ret < 0)
return ret;
ret = queue_alloc(&fg->queue, fg->nb_inputs + 1, 0, QUEUE_FRAMES);
if (ret < 0)
return ret;
return idx;
}
int sch_add_sq_enc(Scheduler *sch, uint64_t buf_size_us, void *logctx)
{
SchSyncQueue *sq;
int ret;
ret = GROW_ARRAY(sch->sq_enc, sch->nb_sq_enc);
if (ret < 0)
return ret;
sq = &sch->sq_enc[sch->nb_sq_enc - 1];
sq->sq = sq_alloc(SYNC_QUEUE_FRAMES, buf_size_us, logctx);
if (!sq->sq)
return AVERROR(ENOMEM);
sq->frame = av_frame_alloc();
if (!sq->frame)
return AVERROR(ENOMEM);
ret = pthread_mutex_init(&sq->lock, NULL);
if (ret)
return AVERROR(ret);
return sq - sch->sq_enc;
}
int sch_sq_add_enc(Scheduler *sch, unsigned sq_idx, unsigned enc_idx,
int limiting, uint64_t max_frames)
{
SchSyncQueue *sq;
SchEnc *enc;
int ret;
av_assert0(sq_idx < sch->nb_sq_enc);
sq = &sch->sq_enc[sq_idx];
av_assert0(enc_idx < sch->nb_enc);
enc = &sch->enc[enc_idx];
ret = GROW_ARRAY(sq->enc_idx, sq->nb_enc_idx);
if (ret < 0)
return ret;
sq->enc_idx[sq->nb_enc_idx - 1] = enc_idx;
ret = sq_add_stream(sq->sq, limiting);
if (ret < 0)
return ret;
enc->sq_idx[0] = sq_idx;
enc->sq_idx[1] = ret;
if (max_frames != INT64_MAX)
sq_limit_frames(sq->sq, enc->sq_idx[1], max_frames);
return 0;
}
int sch_connect(Scheduler *sch, SchedulerNode src, SchedulerNode dst)
{
int ret;
switch (src.type) {
case SCH_NODE_TYPE_DEMUX: {
SchDemuxStream *ds;
av_assert0(src.idx < sch->nb_demux &&
src.idx_stream < sch->demux[src.idx].nb_streams);
ds = &sch->demux[src.idx].streams[src.idx_stream];
ret = GROW_ARRAY(ds->dst, ds->nb_dst);
if (ret < 0)
return ret;
ds->dst[ds->nb_dst - 1] = dst;
// demuxed packets go to decoding or streamcopy
switch (dst.type) {
case SCH_NODE_TYPE_DEC: {
SchDec *dec;
av_assert0(dst.idx < sch->nb_dec);
dec = &sch->dec[dst.idx];
av_assert0(!dec->src.type);
dec->src = src;
break;
}
case SCH_NODE_TYPE_MUX: {
SchMuxStream *ms;
av_assert0(dst.idx < sch->nb_mux &&
dst.idx_stream < sch->mux[dst.idx].nb_streams);
ms = &sch->mux[dst.idx].streams[dst.idx_stream];
av_assert0(!ms->src.type);
ms->src = src;
break;