使用FFmpeg的avfilter时,流程如下:
1.使用avfilter_get_by_name 创建AVFilter的实例,返回指针
2.构建buffersrc和buffersink,作为graph的输入输出端口
3.构建类型为AVFilterContext的对象,作为实际操作数据的对象
4.将filter与filtercontext绑定连接,使用avfilter_graph_create_filter函数。
正常代码如下:
transform_ctx = avfilter_graph_alloc_filter(filter_graph, transform, "transform");
ret = avfilter_init_dict(transform_ctx, &pOptions);
if (ret <= 0)
{
//goto ERROR;
}
/*
加入buffersrc和buffersink
*/
ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "buffer", NULL, NULL, filter_graph);
ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "buffersink", NULL, NULL, filter_graph);
if (ret < 0)
{
//goto ERROR;
}
此时,在
ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "buffer", NULL,NULL, filter_graph);
该段执行完后,buffersrc_ctx为空,且返回ret为-22
排错:
1.同样的buffersink初始化buffersink_ctx获得了正常的对象
2.buffersrc正常初始化
原因:
buffersrc_ctx的初始化需要args作为参数,否则无法正常初始化。
初始化语句应该改为:
ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "buffer", args,NULL, filter_graph);
args为描述输入帧序列的参数信息:
snprintf(args, sizeof(args),
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
time_base.num, time_base.den,
pCodecCtx->sample_aspect_ratio.num, pCodecCtx->sample_aspect_ratio.den);
引用于stackoverflow:
https://stackoverflow.com/questions/43038296/ffmpegavfilter-graph-create-filter-method-failed-when-initializing-filter