English 中文(简体)
如何改变AVFilterContext参数?
原标题:How to change AVFilterContext parameters?

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;
}
问题回答

如果你想动态修改<条码>AVFilterContext参数,则使用vfilter_graph_send_command,而不是v-opt_set 。

int FilterUtils::reset(const char *filterName, const char *param, const char *value)
{
    int ret = -1;
    // I want to move the drawbox by modifying the Filter parameter here
     ret = avfilter_graph_send_command(filter_graph, filterName, param, value, NULL, 0, 1);
    // ret = av_opt_set(filter_ctx, param, value, 0);
    std::cout << av_err2str(ret) << std::endl;

    return 0;
};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签