1、创建一个滤镜图 | AVFilterGraph *filter_graph = avfilter_graph_alloc(); |
2、创建滤镜的输入和输出 | AVFilterInOut *inputs = avfilter_inout_alloc(); AVFilterInOut *outputs = avfilter_inout_alloc(); |
3、每个滤镜创建上下文 | AVFilterContext *filter1_ctx = avfilter_graph_alloc_filter(filter_graph, filter1, "filter1"); AVFilterContext *filter2_ctx = avfilter_graph_alloc_filter(filter_graph, filter2, "filter2"); |
4、设置滤镜参数 | snprintf(args, sizeof(args), "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d", codecContext->width, codecContext->height, codecContext->pix_fmt, codecContext->time_base.num, codecContext->time_base.den, codecContext->sample_aspect_ratio.num, codecContext->sample_aspect_ratio.den); |
5、添加输入滤镜 | avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "input", args, nullptr, filter_graph); |
6、添加输出滤镜 | avfilter_graph_create_filter(&buffersink_ctx, buffersink, "output", nullptr, nullptr, filter_graph); |
7、将滤镜的输入和输出与对应的滤镜上下文关联起来 | inputs->name = av_strdup("input"); inputs->filter_ctx = filter1_ctx; inputs->pad_idx = 0; inputs->next = NULL; outputs->name = av_strdup("output"); outputs->filter_ctx = filter2_ctx; outputs->pad_idx = 0; outputs->next = NULL; |
8、解析滤镜描述 | avfilter_graph_parse_ptr(filter_graph, filter_descr, &inputs, &outputs, nullptr); |
9、连接滤镜链接 | avfilter_graph_config(filter_graph, nullptr); |
10、清理资源 | avfilter_inout_free(&inputs); avfilter_inout_free(&outputs); avfilter_graph_free(&filter_graph); |