简体   繁体   中英

How to apply multiple filters on a frame by AVFilterGraph of FFMPEG in c++

I'm trying to do a "crop" and then "eq" filter on an AVFrame but seems i don't understand AVFiltergraph syntax. here is the code:

AVFrame *FFmpegDecoder::cropFrame(AVFrame *frame, int left, int top, int right, int bottom) {
AVFilterContext *buffersink_ctx;
AVFilterContext *buffersrc_ctx;
AVFilterGraph *filter_graph = avfilter_graph_alloc();
AVFrame *f = av_frame_alloc();
AVFilterInOut *inputs = NULL, *outputs = NULL;
char args[512];
int ret;
auto out_w = frame->width - left - right;
auto out_h = frame->height - top - bottom;
snprintf(args, sizeof(args),
         "buffer=video_size=%dx%d:pix_fmt=%d:time_base=1/1:pixel_aspect=0/1[src];"
         "[src]crop=x=%d:y=%d:out_w=%d:out_h=%d[cropped];"
         "[cropped]eq=brightness=0.06:saturation=2:contrast=1[out];"
         "[out]buffersink",
         frame->width, frame->height, frame->format,
         left, top, out_w, out_h);
ret = avfilter_graph_parse2(filter_graph, args, &inputs, &outputs);
if (ret < 0) return NULL;
assert(inputs == NULL && outputs == NULL);
ret = avfilter_graph_config(filter_graph, NULL);
if (ret < 0) return NULL;

buffersrc_ctx = avfilter_graph_get_filter(filter_graph, "Parsed_buffer_0");
buffersink_ctx = avfilter_graph_get_filter(filter_graph, "Parsed_buffersink_2");
assert(buffersrc_ctx != NULL);
assert(buffersink_ctx != NULL);

av_frame_ref(f, frame);
ret = av_buffersrc_add_frame(buffersrc_ctx, f);
if (ret < 0) return NULL;
ret = av_buffersink_get_frame(buffersink_ctx, f);
if (ret < 0) return NULL;

avfilter_graph_free(&filter_graph);

return f;

}

In addition, i've tried this:

"buffer=video_size=%dx%d:pix_fmt=%d:time_base=1/1:pixel_aspect=0/1[src];"

"[src]crop=x=%d:y=%d:out_w=%d:out_h=%d,eq=brightness=0.06:saturation=2:contrast=1[out];"

"[out]buffersink"

but no luck. Thanks in advance.

  1. I believe the input graph config string is incorrect. Buffersrc and buffersink filters are configured with avfilter_graph_create_filter , not in the graph string.

  2. Have you tried ffmpeg/doc/examples/filter_video.c example? It shows a useful routine to use avfilter including your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM