-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.cpp
5504 lines (4660 loc) · 209 KB
/
main.cpp
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
#include <algorithm>
#include <atomic>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <sstream>
#include <vector>
#include "imgui.h"
#include "imgui_internal.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "imnodes.h"
#include <stdio.h>
#include <unistd.h>
#define GLFW_INCLUDE_NONE
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <AL/alc.h>
#include <AL/al.h>
#include <AL/alext.h>
typedef struct Buffer {
union {
ALuint a;
GLuint v;
} u;
} Buffer;
extern "C" {
#include <libavutil/avutil.h>
#include <libavutil/avstring.h>
#include <libavutil/bprint.h>
#include <libavutil/dict.h>
#include <libavutil/opt.h>
#include <libavutil/parseutils.h>
#include <libavutil/pixdesc.h>
#include <libavutil/time.h>
#include <libavcodec/avcodec.h>
#include <libavfilter/avfilter.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
#include <libavformat/avformat.h>
#include <libavdevice/avdevice.h>
#include "ringbuffer/ringbuffer.c"
}
static const AVSampleFormat all_sample_fmts[] = {
AV_SAMPLE_FMT_NONE,
AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32,
AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL,
AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64P,
};
#define AL_BUFFERS 16
typedef struct FrameInfo {
int width, height;
int nb_samples;
int format;
int key_frame;
enum AVPictureType pict_type;
AVChannelLayout ch_layout;
AVRational sample_aspect_ratio;
int64_t pts;
AVRational time_base;
int interlaced_frame;
int top_field_first;
int sample_rate;
enum AVColorRange color_range;
enum AVColorPrimaries color_primaries;
enum AVColorTransferCharacteristic color_trc;
enum AVColorSpace colorspace;
enum AVChromaLocation chroma_location;
int64_t duration;
size_t crop_top;
size_t crop_bottom;
size_t crop_left;
size_t crop_right;
} FrameInfo;
typedef struct Edge2Pad {
unsigned node;
bool removed;
bool is_output;
unsigned pad_index;
enum AVMediaType type;
} Edge2Pad;
typedef struct ColorItem {
float c[4];
} ColorItem;
typedef struct OptStorage {
union {
int32_t i32;
uint32_t u32;
float flt;
int64_t i64;
uint64_t u64;
double dbl;
AVRational q;
char *str;
ColorItem col;
int32_t *i32_array;
uint32_t *u32_array;
float *flt_array;
int64_t *i64_array;
uint64_t *u64_array;
double *dbl_array;
AVRational *q_array;
char **str_array;
ColorItem *col_array;
} u;
unsigned nb_items;
} OptStorage;
typedef struct BufferSource {
std::string stream_url;
int stream_index;
enum AVMediaType type;
AVFilterContext *ctx;
AVFormatContext *fmt_ctx;
AVCodecContext *dec_ctx;
} BufferSource;
typedef struct BufferSink {
unsigned id;
char *label;
char *description;
AVFilterContext *ctx;
AVRational time_base;
AVRational frame_rate;
ring_buffer_t empty_frames;
ring_buffer_t consume_frames;
ring_buffer_t render_frames;
double speed;
bool ready;
bool fullscreen;
bool muted;
bool show_osd;
bool have_window_pos;
ImVec2 window_pos;
ALuint source;
ALenum format;
float gain;
float position[3];
std::vector<Buffer> buffers;
GLint downscale_interpolator;
GLint upscale_interpolator;
int64_t frame_number;
int64_t qpts;
int64_t pts;
ALint audio_queue_size;
int sample_rate;
int frame_nb_samples;
float *samples;
unsigned nb_samples;
unsigned sample_index;
GLuint *texture;
int width;
int height;
FrameInfo frame_info;
} BufferSink;
typedef struct FilterNode {
unsigned id;
bool imported_id;
bool set_pos;
ImVec2 pos;
int edge;
bool colapsed;
bool have_exports;
bool show_exports;
const AVFilter *filter;
char *filter_name;
char *filter_label;
char *ctx_options;
char *filter_options;
AVFilterContext *probe;
AVFilterContext *ctx;
std::string stream_url;
std::vector<int> inpad_edges;
std::vector<int> outpad_edges;
std::vector<OptStorage> opt_storage;
} FilterNode;
static void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}
bool full_screen = false;
bool show_osd_all = false;
bool muted_all = false;
bool restart_display = false;
int filter_graph_nb_threads = 0;
int filter_graph_auto_convert_flags = 0;
unsigned last_buffersink_window = 0;
unsigned last_abuffersink_window = 0;
unsigned focus_buffersink_window = UINT_MAX;
unsigned focus_abuffersink_window = UINT_MAX;
bool show_abuffersink_window = true;
bool show_buffersink_window = true;
bool show_dumpgraph_window = false;
bool show_commands_window = false;
bool show_filtergraph_editor_window = true;
bool show_mini_map = true;
int mini_map_location = ImNodesMiniMapLocation_BottomRight;
int style_colors = 1;
char *import_script_file_name = NULL;
bool need_filters_reinit = true;
std::atomic<bool> framestep = false;
std::atomic<bool> paused = true;
bool show_info = false;
bool show_help = false;
bool show_version = false;
bool show_console = false;
bool show_log_window = false;
int log_level = AV_LOG_INFO;
ImGuiTextBuffer log_buffer;
ImVector<int> log_lines_offsets;
ImVector<int> log_lines_levels;
std::mutex log_mutex;
GLint global_upscale_interpolation = GL_NEAREST;
GLint global_downscale_interpolation = GL_NEAREST;
int output_sample_rate = 44100;
int audio_queue_size = AL_BUFFERS;
int display_w;
int display_h;
int width = 1280;
int height = 720;
unsigned depth = 0;
unsigned audio_format = 0;
bool filter_graph_is_valid = false;
AVFilterGraph *filter_graph = NULL;
AVFilterGraph *probe_graph = NULL;
char *graphdump_text = NULL;
float audio_sample_range[2] = { 1.f, 1.f };
float audio_window_size[2] = { 0, 100 };
float osd_fullscreen_pos[2] = { 0.01f, 0.01f };
float osd_alpha = 0.5f;
float commands_alpha = 0.8f;
float console_alpha = 0.5f;
float dump_alpha = 0.7f;
float editor_alpha = 1.0f;
float help_alpha = 0.5f;
float info_alpha = 0.7f;
float log_alpha = 0.7f;
float sink_alpha = 1.f;
float version_alpha = 0.8f;
int editor_edge = 0;
ImNodesEditorContext *node_editor_context;
std::mutex filtergraph_mutex;
std::vector<ALuint> play_sources;
std::thread play_sound_thread;
std::vector<BufferSource> buffer_sources;
std::vector<BufferSink> abuffer_sinks;
std::vector<BufferSink> buffer_sinks;
std::vector<std::condition_variable> acv;
std::vector<std::condition_variable> cv;
std::vector<std::mutex> amutexes;
std::vector<std::mutex> mutexes;
std::vector<std::thread> audio_sink_threads;
std::vector<std::thread> video_sink_threads;
std::vector<std::thread> source_threads;
std::vector<FilterNode> filter_nodes;
std::vector<std::pair<int, int>> filter_links;
std::vector<Edge2Pad> edge2pad;
static const GLenum gl_fmts[] = { GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT };
static const enum AVPixelFormat hi_pix_fmts[] = { AV_PIX_FMT_RGBA64, AV_PIX_FMT_NONE };
static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
static const enum AVSampleFormat hi_sample_fmts[] = { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE };
static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
static int sample_rates[] = { output_sample_rate, 0 };
ALCdevice *al_dev = NULL;
ALCcontext *al_ctx = NULL;
float listener_direction[6] = { 0, 0, -1, 0, 1, 0 };
float listener_position[3] = { 0, 0, 0 };
static void alloc_ring_buffer(ring_buffer_t *ring_buffer, Buffer *id)
{
if (!id)
return;
while (!ring_buffer_is_full(ring_buffer)) {
AVFrame *empty_frame = av_frame_alloc();
if (empty_frame) {
unsigned i = ring_buffer_num_items(ring_buffer);
ring_item_t item = { empty_frame, id[i] };
ring_buffer_enqueue(ring_buffer, item);
}
}
}
static void clear_ring_buffer(ring_buffer_t *ring_buffer)
{
while (!ring_buffer_is_empty(ring_buffer)) {
ring_item_t item = { NULL, 0 };
ring_buffer_dequeue(ring_buffer, &item);
av_frame_free(&item.frame);
}
}
static void sound_thread(ALsizei nb_sources, std::vector<ALuint> *sources)
{
bool step = framestep;
bool state = paused;
if ((state == true) || (step == false))
alSourceStopv(nb_sources, sources->data());
while (!need_filters_reinit && filter_graph_is_valid) {
bool new_step = framestep;
bool new_state = paused;
if (sources->size() == 0)
break;
if ((state != new_state) || (step != new_step)) {
state = new_state;
step = new_step;
if (state == true && step == false)
alSourcePausev(nb_sources, sources->data());
else
alSourcePlayv(nb_sources, sources->data());
}
av_usleep(100000);
}
}
static void worker_thread(BufferSink *sink, std::mutex *mutex, std::condition_variable *cv)
{
int ret = 0;
while (sink->ctx) {
if (need_filters_reinit)
break;
std::unique_lock<std::mutex> lk(*mutex);
cv->wait(lk, [sink]{ return sink->ready; });
if (sink->ready == false)
continue;
sink->ready = false;
while (!ring_buffer_is_empty(&sink->empty_frames)) {
ring_item_t item = { NULL, 0 };
int64_t start, end;
if (ring_buffer_is_full(&sink->consume_frames))
break;
ring_buffer_dequeue(&sink->empty_frames, &item);
if (!item.frame)
break;
filtergraph_mutex.lock();
start = av_gettime_relative();
ret = av_buffersink_get_frame_flags(sink->ctx, item.frame, 0);
end = av_gettime_relative();
filtergraph_mutex.unlock();
if (end > start && item.frame)
sink->speed = 1000000. * (std::max(item.frame->nb_samples, 1)) * av_q2d(av_inv_q(sink->frame_rate)) / (end - start);
if (ret < 0) {
av_frame_unref(item.frame);
ring_buffer_enqueue(&sink->empty_frames, item);
if (ret != AVERROR(EAGAIN))
break;
item.frame = NULL;
}
if (item.frame)
ring_buffer_enqueue(&sink->consume_frames, item);
}
if (ret < 0 && ret != AVERROR(EAGAIN))
break;
}
clear_ring_buffer(&sink->empty_frames);
clear_ring_buffer(&sink->consume_frames);
clear_ring_buffer(&sink->render_frames);
}
static void notify_worker(BufferSink *sink, std::mutex *mutex, std::condition_variable *cv)
{
std::unique_lock<std::mutex> lk(*mutex);
sink->ready = true;
cv->notify_one();
}
static void kill_audio_sink_threads()
{
for (unsigned i = 0; i < audio_sink_threads.size(); i++) {
BufferSink *sink = &abuffer_sinks[i];
if (audio_sink_threads[i].joinable()) {
notify_worker(sink, &amutexes[i], &acv[i]);
audio_sink_threads[i].join();
}
av_freep(&sink->label);
av_freep(&sink->description);
av_freep(&sink->samples);
alDeleteSources(1, &sink->source);
for (unsigned n = 0; n < sink->buffers.size(); n++)
alDeleteBuffers(1, &sink->buffers[n].u.a);
ring_buffer_free(&sink->empty_frames);
ring_buffer_free(&sink->consume_frames);
ring_buffer_free(&sink->render_frames);
sink->buffers.clear();
}
}
static void kill_video_sink_threads()
{
for (unsigned i = 0; i < video_sink_threads.size(); i++) {
BufferSink *sink = &buffer_sinks[i];
if (video_sink_threads[i].joinable()) {
notify_worker(sink, &mutexes[i], &cv[i]);
video_sink_threads[i].join();
}
av_freep(&sink->label);
av_freep(&sink->description);
glDeleteTextures(1, &sink->buffers[0].u.v);
ring_buffer_free(&sink->empty_frames);
ring_buffer_free(&sink->consume_frames);
ring_buffer_free(&sink->render_frames);
sink->buffers.clear();
}
}
static void kill_source_threads()
{
for (unsigned i = 0; i < source_threads.size(); i++) {
if (source_threads[i].joinable()) {
source_threads[i].join();
}
}
}
static int get_nb_filter_threads(const AVFilter *filter)
{
if (filter->flags & AVFILTER_FLAG_SLICE_THREADS)
return 0;
return 1;
}
static void source_worker_thread(BufferSource *source)
{
const int stream_index = source->stream_index;
AVFilterContext *buffersrc_ctx = source->ctx;
AVFormatContext *fmt_ctx = source->fmt_ctx;
AVCodecContext *dec_ctx = source->dec_ctx;
AVPacket *packet = av_packet_alloc();
AVFrame *frame = av_frame_alloc();
int ret;
while (source->ctx) {
if (need_filters_reinit)
break;
if (!av_buffersrc_get_nb_failed_requests(buffersrc_ctx)) {
av_usleep(50000);
continue;
}
if ((ret = av_read_frame(fmt_ctx, packet)) < 0)
break;
if (packet->stream_index == stream_index) {
ret = avcodec_send_packet(dec_ctx, packet);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while sending a packet to the decoder\n");
break;
}
while (ret >= 0) {
ret = avcodec_receive_frame(dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while receiving a frame from the decoder\n");
break;
}
if (ret >= 0) {
if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF) < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
break;
}
}
}
}
av_packet_unref(packet);
}
if (ret == AVERROR_EOF) {
/* signal EOF to the filtergraph */
if (av_buffersrc_add_frame_flags(buffersrc_ctx, NULL, 0) < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while closing the filter source\n");
}
}
}
static void find_source_params(BufferSource *source)
{
AVFilterContext *buffersrc_ctx = source->ctx;
int stream_index, ret;
const AVCodec *dec;
if (source->stream_url.empty())
return;
if ((ret = avformat_open_input(&source->fmt_ctx, source->stream_url.c_str(), NULL, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open input\n");
return;
}
if ((ret = avformat_find_stream_info(source->fmt_ctx, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
return;
}
/* select the audio stream */
ret = av_find_best_stream(source->fmt_ctx, source->type, -1, -1, &dec, 0);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find an stream in the input file\n");
return;
}
stream_index = source->stream_index = ret;
/* create decoding context */
source->dec_ctx = avcodec_alloc_context3(dec);
if (!source->dec_ctx)
return;
avcodec_parameters_to_context(source->dec_ctx, source->fmt_ctx->streams[stream_index]->codecpar);
if ((ret = avcodec_open2(source->dec_ctx, dec, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open decoder\n");
return;
}
AVBufferSrcParameters *params = av_buffersrc_parameters_alloc();
params->time_base = source->fmt_ctx->streams[stream_index]->time_base;
switch (source->type) {
case AVMEDIA_TYPE_AUDIO:
params->format = source->dec_ctx->sample_fmt;
params->sample_rate = source->dec_ctx->sample_rate;
params->ch_layout = source->dec_ctx->ch_layout;
break;
case AVMEDIA_TYPE_VIDEO:
params->format = source->dec_ctx->pix_fmt;
params->frame_rate = source->dec_ctx->framerate;
params->width = source->dec_ctx->width;
params->height = source->dec_ctx->height;
break;
default:
break;
}
av_buffersrc_parameters_set(buffersrc_ctx, params);
av_free(params);
}
static int filters_setup()
{
const AVFilter *new_filter;
int ret;
if (need_filters_reinit == false)
return 0;
if (play_sound_thread.joinable())
play_sound_thread.join();
play_sources.clear();
kill_source_threads();
kill_audio_sink_threads();
kill_video_sink_threads();
source_threads.clear();
audio_sink_threads.clear();
video_sink_threads.clear();
need_filters_reinit = false;
filter_graph_is_valid = false;
if (filter_nodes.size() == 0)
return 0;
buffer_sources.clear();
buffer_sinks.clear();
abuffer_sinks.clear();
cv.clear();
acv.clear();
mutexes.clear();
amutexes.clear();
av_freep(&graphdump_text);
avfilter_graph_free(&filter_graph);
filter_graph = avfilter_graph_alloc();
if (!filter_graph) {
av_log(NULL, AV_LOG_ERROR, "Cannot allocate filter graph.\n");
ret = AVERROR(ENOMEM);
goto error;
}
filter_graph->nb_threads = filter_graph_nb_threads;
avfilter_graph_set_auto_convert(filter_graph, filter_graph_auto_convert_flags);
for (unsigned i = 0; i < filter_nodes.size(); i++) {
AVFilterContext *filter_ctx;
new_filter = filter_nodes[i].filter;
if (!new_filter) {
av_log(NULL, AV_LOG_ERROR, "Cannot [%d] get filter by name: %s.\n", i, filter_nodes[i].filter_name);
ret = AVERROR(ENOSYS);
goto error;
}
filter_ctx = avfilter_graph_alloc_filter(filter_graph, new_filter, filter_nodes[i].filter_label);
if (!filter_ctx) {
av_log(NULL, AV_LOG_ERROR, "Cannot allocate filter context.\n");
ret = AVERROR(ENOMEM);
goto error;
}
av_opt_set_defaults(filter_ctx);
filter_ctx->nb_threads = get_nb_filter_threads(filter_ctx->filter);
filter_nodes[i].ctx = filter_ctx;
ret = av_opt_copy(filter_ctx, filter_nodes[i].probe);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot copy options for filter.\n");
goto error;
}
ret = av_opt_copy(filter_ctx->priv, filter_nodes[i].probe->priv);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot copy private options for filter.\n");
goto error;
}
if (!strcmp(filter_ctx->filter->name, "buffer")) {
BufferSource new_source;
new_source.fmt_ctx = NULL;
new_source.dec_ctx = NULL;
new_source.ctx = filter_ctx;
if (!filter_nodes[i].stream_url.empty())
new_source.stream_url = filter_nodes[i].stream_url;
new_source.type = AVMEDIA_TYPE_VIDEO;
find_source_params(&new_source);
buffer_sources.push_back(new_source);
} else if (!strcmp(filter_ctx->filter->name, "abuffer")) {
BufferSource new_source;
new_source.fmt_ctx = NULL;
new_source.dec_ctx = NULL;
new_source.ctx = filter_ctx;
if (!filter_nodes[i].stream_url.empty())
new_source.stream_url = filter_nodes[i].stream_url;
new_source.type = AVMEDIA_TYPE_AUDIO;
find_source_params(&new_source);
buffer_sources.push_back(new_source);
} else if (!strcmp(filter_ctx->filter->name, "buffersink")) {
BufferSink new_sink = {0};
new_sink.ctx = filter_ctx;
new_sink.ready = false;
new_sink.have_window_pos = false;
new_sink.fullscreen = false;
new_sink.muted = false;
new_sink.show_osd = true;
new_sink.frame_number = 0;
new_sink.upscale_interpolator = global_upscale_interpolation;
new_sink.downscale_interpolator = global_downscale_interpolation;
ret = av_opt_set_int_list(filter_ctx, "pix_fmts", depth ? hi_pix_fmts : pix_fmts,
AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
if (ret < 0) {
ret = av_opt_set_array(filter_ctx, "pixel_formats", AV_OPT_SEARCH_CHILDREN, 0, 1,
AV_OPT_TYPE_PIXEL_FMT, depth ? hi_pix_fmts : pix_fmts);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot set buffersink output pixel format.\n");
goto error;
}
}
buffer_sinks.push_back(new_sink);
} else if (!strcmp(filter_ctx->filter->name, "abuffersink")) {
BufferSink new_sink = {0};
alcGetIntegerv(al_dev, ALC_FREQUENCY, 1, &new_sink.sample_rate);
sample_rates[0] = new_sink.sample_rate;
new_sink.ctx = filter_ctx;
new_sink.ready = false;
new_sink.have_window_pos = false;
new_sink.fullscreen = false;
new_sink.muted = false;
new_sink.show_osd = true;
new_sink.upscale_interpolator = 0;
new_sink.downscale_interpolator = 0;
new_sink.frame_number = 0;
ret = av_opt_set_int_list(filter_ctx, "sample_fmts", audio_format ? hi_sample_fmts : sample_fmts,
AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
if (ret < 0) {
ret = av_opt_set_array(filter_ctx, "sample_formats", AV_OPT_SEARCH_CHILDREN, 0, 1,
AV_OPT_TYPE_SAMPLE_FMT, audio_format ? hi_sample_fmts : sample_fmts);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot set abuffersink output sample formats.\n");
goto error;
}
}
ret = av_opt_set_int_list(filter_ctx, "sample_rates", sample_rates,
0, AV_OPT_SEARCH_CHILDREN);
if (ret < 0) {
ret = av_opt_set_array(filter_ctx, "samplerates", AV_OPT_SEARCH_CHILDREN, 0, 1,
AV_OPT_TYPE_INT, sample_rates);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot set abuffersink output sample rates.\n");
goto error;
}
}
abuffer_sinks.push_back(new_sink);
}
ret = avfilter_init_str(filter_ctx, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot init filter.\n");
goto error;
}
}
for (unsigned i = 0; i < filter_links.size(); i++) {
const std::pair<int, int> p = filter_links[i];
if ((unsigned)p.first >= edge2pad.size() ||
(unsigned)p.second >= edge2pad.size()) {
av_log(NULL, AV_LOG_ERROR, "Cannot link filters: edges out of range (%d, %d) >= (%ld).\n", p.first, p.second, edge2pad.size());
ret = AVERROR(EINVAL);
goto error;
}
unsigned x = edge2pad[p.first].node;
unsigned y = edge2pad[p.second].node;
unsigned x_pad = edge2pad[p.first].pad_index;
unsigned y_pad = edge2pad[p.second].pad_index;
bool x_out = edge2pad[p.first].is_output;
bool y_out = edge2pad[p.second].is_output;
if (x >= filter_nodes.size() || y >= filter_nodes.size()) {
av_log(NULL, AV_LOG_ERROR, "Cannot link filters: index (%d, %d) out of range (%ld)\n",
x, y, filter_nodes.size());
ret = AVERROR(EINVAL);
goto error;
}
if (y_out == true) {
std::swap(x, y);
std::swap(x_pad, y_pad);
std::swap(x_out, y_out);
}
if ((ret = avfilter_link(filter_nodes[x].ctx, x_pad, filter_nodes[y].ctx, y_pad)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot link filters: %s(%d|%d) <-> %s(%d|%d)\n",
filter_nodes[x].filter_label, x_pad, x_out, filter_nodes[y].filter_label, y_pad, y_out);
goto error;
}
}
if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot configure graph.\n");
goto error;
}
filter_graph_is_valid = true;
framestep = false;
paused = true;
graphdump_text = avfilter_graph_dump(filter_graph, NULL);
show_abuffersink_window = true;
show_buffersink_window = true;
error:
if (ret < 0)
return ret;
std::vector<std::condition_variable> cv_list(buffer_sinks.size());
cv.swap(cv_list);
std::vector<std::mutex> mutex_list(buffer_sinks.size());
mutexes.swap(mutex_list);
std::vector<std::thread> thread_list(buffer_sinks.size());
video_sink_threads.swap(thread_list);
for (unsigned i = 0; i < buffer_sinks.size(); i++) {
BufferSink *sink = &buffer_sinks[i];
sink->id = i;
sink->label = av_asprintf("Video FilterGraph Output %d", i);
sink->description = NULL;
sink->time_base = av_buffersink_get_time_base(sink->ctx);
sink->frame_rate = av_buffersink_get_frame_rate(sink->ctx);
sink->pts = AV_NOPTS_VALUE;
sink->sample_index = 0;
sink->samples = NULL;
sink->frame_number = 0;
sink->frame_nb_samples = 0;
sink->nb_samples = 0;
sink->texture = NULL;
sink->width = 0;
sink->height = 0;
sink->buffers.resize(4);
ring_buffer_init(&sink->empty_frames, sink->buffers.size());
ring_buffer_init(&sink->consume_frames, sink->buffers.size());
ring_buffer_init(&sink->render_frames, sink->buffers.size());
glGenTextures(1, &sink->buffers[0].u.v);
for (unsigned n = 1; n < sink->buffers.size(); n++)
sink->buffers[n].u.v = sink->buffers[0].u.v;
alloc_ring_buffer(&sink->empty_frames, sink->buffers.data());
std::thread sink_thread(worker_thread, &buffer_sinks[i], &mutexes[i], &cv[i]);
video_sink_threads[i].swap(sink_thread);
}
std::vector<std::condition_variable> acv_list(abuffer_sinks.size());
acv.swap(acv_list);
std::vector<std::mutex> amutex_list(abuffer_sinks.size());
amutexes.swap(amutex_list);
std::vector<std::thread> athread_list(abuffer_sinks.size());
audio_sink_threads.swap(athread_list);
for (unsigned i = 0; i < abuffer_sinks.size(); i++) {
char layout_name[512] = { 0 };
BufferSink *sink = &abuffer_sinks[i];
AVChannelLayout layout;
av_buffersink_get_ch_layout(sink->ctx, &layout);
av_channel_layout_describe(&layout, layout_name, sizeof(layout_name));
sink->id = i;
sink->label = av_asprintf("Audio FilterGraph Output %d", i);
sink->description = av_asprintf("%s", layout_name);
sink->time_base = av_buffersink_get_time_base(sink->ctx);
sink->frame_rate = av_make_q(av_buffersink_get_sample_rate(sink->ctx), 1);
sink->sample_index = 0;
sink->nb_samples = 512;
sink->frame_number = 0;
sink->frame_nb_samples = 0;
sink->pts = AV_NOPTS_VALUE;
sink->samples = (float *)av_calloc(sink->nb_samples, sizeof(float));
sink->audio_queue_size = audio_queue_size;
sink->texture = NULL;
sink->width = 0;
sink->height = 0;
sink->buffers.resize(sink->audio_queue_size);
ring_buffer_init(&sink->empty_frames, audio_queue_size);
ring_buffer_init(&sink->consume_frames, audio_queue_size);
ring_buffer_init(&sink->render_frames, audio_queue_size);
sink->format = audio_format ? AL_FORMAT_MONO_DOUBLE_EXT : AL_FORMAT_MONO_FLOAT32;
for (unsigned n = 0; n < sink->buffers.size(); n++)
alGenBuffers(1, &sink->buffers[n].u.a);
alGenSources(1, &sink->source);
play_sources.push_back(sink->source);
sink->gain = 1.f;
sink->position[0] = 0.f;
sink->position[1] = 0.f;
sink->position[2] = -1.f;
alSource3f(sink->source, AL_POSITION, sink->position[0], sink->position[1], sink->position[2]);
alSourcei(sink->source, AL_SOURCE_RELATIVE, AL_TRUE);
alSourcei(sink->source, AL_ROLLOFF_FACTOR, 0);
alloc_ring_buffer(&sink->empty_frames, sink->buffers.data());
std::thread asink_thread(worker_thread, &abuffer_sinks[i], &amutexes[i], &acv[i]);
audio_sink_threads[i].swap(asink_thread);
}
std::thread new_sound_thread(sound_thread, abuffer_sinks.size(), &play_sources);
play_sound_thread.swap(new_sound_thread);
std::vector<std::thread> bufferthread_list(buffer_sources.size());
source_threads.swap(bufferthread_list);
for (unsigned i = 0; i < buffer_sources.size(); i++) {
std::thread source_thread(source_worker_thread, &buffer_sources[i]);
source_threads[i].swap(source_thread);
}
return 0;
}
static void load_frame(GLuint *out_texture, int *width, int *height, AVFrame *frame,
BufferSink *sink)
{
const unsigned idx = (frame->format == AV_PIX_FMT_RGBA) ? 0 : 1;
const size_t pixel_size = idx ? 4 * sizeof(uint16_t) : 4 * sizeof(uint8_t);
*width = frame->width;
*height = frame->height;
glBindTexture(GL_TEXTURE_2D, *out_texture);
if (sink->pts != frame->pts) {
sink->texture = out_texture;
sink->width = *width;
sink->height = *height;
sink->pts = frame->pts;
sink->frame_number++;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, sink->downscale_interpolator);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, sink->upscale_interpolator);
glPixelStorei(GL_UNPACK_ROW_LENGTH, frame->linesize[0] / pixel_size);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, frame->width, frame->height, 0, GL_RGBA, gl_fmts[idx], frame->data[0]);
}
}
static void draw_info(bool *p_open, bool full)
{
BufferSink *last_sink = NULL;
FrameInfo *frame = NULL;
int nb_columns = 0;
const ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDecoration |
ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoSavedSettings |
ImGuiWindowFlags_NoNav |
ImGuiWindowFlags_NoFocusOnAppearing |
ImGuiWindowFlags_HorizontalScrollbar |
ImGuiWindowFlags_NoMove;
if (full == false) {
if (last_buffersink_window >= 0 && last_buffersink_window < buffer_sinks.size()) {
last_sink = &buffer_sinks[last_buffersink_window];
frame = &last_sink->frame_info;
}
if (last_abuffersink_window >= 0 && last_abuffersink_window < abuffer_sinks.size()) {
last_sink = &abuffer_sinks[last_abuffersink_window];
frame = &last_sink->frame_info;
}
if (!frame)
return;
} else {
if (!(last_buffersink_window >= 0 && last_buffersink_window < buffer_sinks.size()) &&
!(last_abuffersink_window >= 0 && last_abuffersink_window < abuffer_sinks.size()))
return;
}
ImGui::SetNextWindowPos(ImVec2(display_w/2, display_h/2), 0, ImVec2(0.5, 0.5));
ImGui::SetNextWindowBgAlpha(info_alpha);
ImGui::SetNextWindowFocus();
if (!ImGui::Begin("##Info", p_open, window_flags)) {