|
| 1 | +#include <libavfilter/avfilter.h> |
| 2 | + |
| 3 | +#include "filter_graph_complex.h" |
| 4 | + |
| 5 | +// Function to set up the filter graph |
| 6 | +int setup_filter_graph(AVFilterGraph **graph, AVFilterContext **buffersrc_ctx, AVFilterContext **buffersink_ctx, AVCodecContext *dec_ctx[], int num_inputs) { |
| 7 | + AVFilterGraph *filter_graph = avfilter_graph_alloc(); |
| 8 | + if (!filter_graph) { |
| 9 | + fprintf(stderr, "Could not allocate filter graph\n"); |
| 10 | + return -1; |
| 11 | + } |
| 12 | + |
| 13 | + char args[512]; |
| 14 | + AVFilterContext *hwupload_ctx[num_inputs]; |
| 15 | + AVFilterContext *scale_ctx[num_inputs]; |
| 16 | + AVFilterContext *xstack_ctx; |
| 17 | + const AVFilter *buffersrc = avfilter_get_by_name("buffer"); |
| 18 | + const AVFilter *buffersink = avfilter_get_by_name("buffersink"); |
| 19 | + const AVFilter *hwupload = avfilter_get_by_name("hwupload"); |
| 20 | + const AVFilter *scale_qsv = avfilter_get_by_name("scale_qsv"); |
| 21 | + const AVFilter *xstack_qsv = avfilter_get_by_name("xstack_qsv"); |
| 22 | + const AVFilter *format = avfilter_get_by_name("format"); |
| 23 | + |
| 24 | + // Create buffer source filters |
| 25 | + for (int i = 0; i < num_inputs; i++) { |
| 26 | + snprintf(args, sizeof(args), |
| 27 | + "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d", |
| 28 | + dec_ctx[i]->width, dec_ctx[i]->height, dec_ctx[i]->pix_fmt, |
| 29 | + dec_ctx[i]->time_base.num, dec_ctx[i]->time_base.den, |
| 30 | + dec_ctx[i]->sample_aspect_ratio.num, dec_ctx[i]->sample_aspect_ratio.den); |
| 31 | + if (avfilter_graph_create_filter(&buffersrc_ctx[i], buffersrc, NULL, args, NULL, filter_graph) < 0) { |
| 32 | + fprintf(stderr, "Could not create buffer source filter for input %d\n", i); |
| 33 | + avfilter_graph_free(&filter_graph); |
| 34 | + return -1; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Create hwupload and scale filters |
| 39 | + for (int i = 0; i < num_inputs; i++) { |
| 40 | + if (avfilter_graph_create_filter(&hwupload_ctx[i], hwupload, NULL, NULL, NULL, filter_graph) < 0) { |
| 41 | + fprintf(stderr, "Could not create hwupload filter for input %d\n", i); |
| 42 | + avfilter_graph_free(&filter_graph); |
| 43 | + return -1; |
| 44 | + } |
| 45 | + snprintf(args, sizeof(args), "iw/4:ih/2"); |
| 46 | + if (avfilter_graph_create_filter(&scale_ctx[i], scale_qsv, NULL, args, NULL, filter_graph) < 0) { |
| 47 | + fprintf(stderr, "Could not create scale_qsv filter for input %d\n", i); |
| 48 | + avfilter_graph_free(&filter_graph); |
| 49 | + return -1; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // Create xstack filter |
| 54 | + if (avfilter_graph_create_filter(&xstack_ctx, xstack_qsv, NULL, "inputs=8:layout=0_0|w0_0|0_h0|w0_h0|w0+w1_0|w0+w1+w2_0|w0+w1_h0|w0+w1+w2_h0", NULL, filter_graph) < 0) { |
| 55 | + fprintf(stderr, "Could not create xstack_qsv filter\n"); |
| 56 | + avfilter_graph_free(&filter_graph); |
| 57 | + return -1; |
| 58 | + } |
| 59 | + |
| 60 | + // Create format filters |
| 61 | + AVFilterContext *format_ctx1; |
| 62 | + if (avfilter_graph_create_filter(&format_ctx1, format, NULL, "y210le", NULL, filter_graph) < 0) { |
| 63 | + fprintf(stderr, "Could not create format filter y210le\n"); |
| 64 | + avfilter_graph_free(&filter_graph); |
| 65 | + return -1; |
| 66 | + } |
| 67 | + AVFilterContext *format_ctx2; |
| 68 | + if (avfilter_graph_create_filter(&format_ctx2, format, NULL, "yuv422p10le", NULL, filter_graph) < 0) { |
| 69 | + fprintf(stderr, "Could not create format filter yuv422p10le\n"); |
| 70 | + avfilter_graph_free(&filter_graph); |
| 71 | + return -1; |
| 72 | + } |
| 73 | + |
| 74 | + // Create buffer sink filter |
| 75 | + if (avfilter_graph_create_filter(buffersink_ctx, buffersink, NULL, NULL, NULL, filter_graph) < 0) { |
| 76 | + fprintf(stderr, "Could not create buffer sink filter\n"); |
| 77 | + avfilter_graph_free(&filter_graph); |
| 78 | + return -1; |
| 79 | + } |
| 80 | + |
| 81 | + // Link filters |
| 82 | + for (int i = 0; i < num_inputs; i++) { |
| 83 | + if (avfilter_link(buffersrc_ctx[i], 0, hwupload_ctx[i], 0) < 0 || |
| 84 | + avfilter_link(hwupload_ctx[i], 0, scale_ctx[i], 0) < 0 || |
| 85 | + avfilter_link(scale_ctx[i], 0, xstack_ctx, i) < 0) { |
| 86 | + fprintf(stderr, "Error linking filters for input %d\n", i); |
| 87 | + avfilter_graph_free(&filter_graph); |
| 88 | + return -1; |
| 89 | + } |
| 90 | + } |
| 91 | + if (avfilter_link(xstack_ctx, 0, format_ctx1, 0) < 0 || |
| 92 | + avfilter_link(format_ctx1, 0, format_ctx2, 0) < 0 || |
| 93 | + avfilter_link(format_ctx2, 0, *buffersink_ctx, 0) < 0) { |
| 94 | + fprintf(stderr, "Error linking xstack and format filters\n"); |
| 95 | + avfilter_graph_free(&filter_graph); |
| 96 | + return -1; |
| 97 | + } |
| 98 | + |
| 99 | + // Configure the filter graph |
| 100 | + if (avfilter_graph_config(filter_graph, NULL) < 0) { |
| 101 | + fprintf(stderr, "Error configuring the filter graph\n"); |
| 102 | + avfilter_graph_free(&filter_graph); |
| 103 | + return -1; |
| 104 | + } |
| 105 | + |
| 106 | + *graph = filter_graph; |
| 107 | + return 0; |
| 108 | +} |
0 commit comments