After generate AVFilterContext *
,how can i change parameters?
Such as "drawbox=x=%d:y=%d:width=%d:height=%d:color=%s"
,
I used av_opt_set(filter_ctx->priv, "x", std::to_string(x).c_str(), 0);
But it didn t take effect.
What should i do for move the rect?
https://github.com/zhuziaho/drawRectOnFrame
int FilterUtils::initFilter(AVCodecContext *codecContext, const char *filter_str)
{
// closeFilter();
int ret = -1;
char args[512];
filter_graph = avfilter_graph_alloc();
if (!filter_graph)
{
ret = AVERROR(ENOMEM);
return -1;
}
buffersrc = avfilter_get_by_name("buffer");
buffersink = avfilter_get_by_name("buffersink");
sprintf(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);
printf("%s
", args);
ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
args, NULL, filter_graph);
if (ret < 0)
{
return -1;
}
ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
NULL, NULL, filter_graph);
if (ret < 0)
{
av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink
");
return -1;
}
ret = av_opt_set_int_list(buffersink_ctx, "pix_fmts", pix_fmts,
AV_PIX_FMT_YUV420P, AV_OPT_SEARCH_CHILDREN);
if (ret < 0)
{
av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format
");
return -1;
}
outputs = avfilter_inout_alloc();
inputs = avfilter_inout_alloc();
if (!outputs || !inputs)
{
ret = AVERROR(ENOMEM);
return -1;
}
outputs->name = av_strdup("in");
outputs->filter_ctx = buffersrc_ctx;
outputs->pad_idx = 0;
outputs->next = NULL;
inputs->name = av_strdup("out");
inputs->filter_ctx = buffersink_ctx;
inputs->pad_idx = 0;
inputs->next = NULL;
if ((ret = avfilter_graph_parse_ptr(filter_graph, "drawbox=x=100:y=100:width=50:height=50:color=#FFFFFF",
&inputs, &outputs, NULL)) < 0)
return -1;
if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
return -1;
for (int i = 0; i < filter_graph->nb_filters; i++)
{
AVFilterContext *filter_ctxn = filter_graph->filters[i];
fprintf(stdout, "[%s](%d) filter_ctxn->name=%s
", __func__, __LINE__, filter_ctxn->name);
}
filter_ctx = avfilter_graph_get_filter(filter_graph, "Parsed_drawbox_0");
ret = 1;
avfilter_inout_free(&inputs);
avfilter_inout_free(&outputs);
return ret;
};
AVFrame *FilterUtils::setFrame(AVFrame *inFrame)
{
int ret = -1;
AVFrame *pFrame = av_frame_alloc();
// I want to move the drawbox by modifying the Filter parameter here
av_opt_set(filter_ctx->priv, "x", std::to_string(500).c_str(), 0);
ret = av_buffersrc_add_frame_flags(buffersrc_ctx, inFrame, AV_BUFFERSRC_FLAG_KEEP_REF);
if (ret < 0)
{
return NULL;
}
ret = av_buffersink_get_frame_flags(buffersink_ctx, pFrame, 0);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
return NULL;
}
return pFrame;
}