【FFmpeg】avfilter_graph_parse_ptr 函数注解拆解

接口如下:
int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters,
                             AVFilterInOut **inputs, AVFilterInOut **outputs,
                             void *log_ctx);

原文注释如下:
>>>>>>>>>
 * In the graph filters description, if the input label of the first
 * filter is not specified, "in" is assumed; if the output label of
 * the last filter is not specified, "out" is assumed.

<<<<<<<<<

这段话翻译过来的大白话如下:
在graph filters描述中,
如果     第一个filter的输入label没有指定,就假定为"in";
如果 最后一个filter的输出label没有指定,就假定为"out";

如果不了解filter的机制,看起来这句话很晦涩。

下面就以fps这个filter进行说明,
假定将帧率设置为20
filters 参数可以设置为"fps=fps=20",将该值传入上述接口,接口内部会对该参数进行加头尾处理,处理结果为:[in]fps=fps=20[out]
此处的[in]和[out] 就是上述英文的意思

对应的源码解析(中文注释表明了添加in]和[out]的位置):

int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters,
                         AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
                         void *log_ctx)
{
    int index = 0, ret = 0;
    char chr = 0;

    AVFilterInOut *curr_inputs = NULL;
    AVFilterInOut *open_inputs  = open_inputs_ptr  ? *open_inputs_ptr  : NULL;
    AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;

    if ((ret = parse_sws_flags(&filters, graph)) < 0)
        goto end;

    do {
        AVFilterContext *filter;
        const char *filterchain = filters;
        filters += strspn(filters, WHITESPACES);

        if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
            goto end;

        if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
            goto end;

        if (filter->nb_inputs == 1 && !curr_inputs && !index) {
            /* First input pad, assume it is "[in]" if not specified */
            const char *tmp = "[in]";	// 第一次循环时,在此处补充的[in]
            if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
                goto end;
        }

        if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
            goto end;

        if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
                                 log_ctx)) < 0)
            goto end;

        filters += strspn(filters, WHITESPACES);
        chr = *filters++;

        if (chr == ';' && curr_inputs) {
            av_log(log_ctx, AV_LOG_ERROR,
                   "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
                   filterchain);
            ret = AVERROR(EINVAL);
            goto end;
        }
        index++;
    } while (chr == ',' || chr == ';');

    if (chr) {
        av_log(log_ctx, AV_LOG_ERROR,
               "Unable to parse graph description substring: \"%s\"\n",
               filters - 1);
        ret = AVERROR(EINVAL);
        goto end;
    }

    if (curr_inputs) {
        /* Last output pad, assume it is "[out]" if not specified */
        const char *tmp = "[out]";	// 在此处补充的[out]
        if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
                                 log_ctx)) < 0)
            goto end;
    }

end:
    /* clear open_in/outputs only if not passed as parameters */
    if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
    else avfilter_inout_free(&open_inputs);
    if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
    else avfilter_inout_free(&open_outputs);
    avfilter_inout_free(&curr_inputs);

    if (ret < 0) {
        while (graph->nb_filters)
            avfilter_free(graph->filters[0]);
        av_freep(&graph->filters);
    }
    return ret;
}

因此,在创建AVFilterInOut时,一定要将
连接   src_buffer的name设置为"in"
连接 sink_buffer的name设置为"out"
否则通过avfilter_graph_config会报告错误

AVFilterInOut *outputs = avfilter_inout_alloc();
outputs->name       = av_strdup("in");
outputs->filter_ctx = buffersrc_ctx;
outputs->pad_idx    = 0;
outputs->next       = NULL;

AVFilterInOut *inputs  = avfilter_inout_alloc();
inputs->name       = av_strdup("out");
inputs->filter_ctx = m_stFilterCtx.buffersink_ctx;
inputs->pad_idx    = 0;
inputs->next       = NULL;

注:如果在接口avfilter_graph_parse_ptr中将filters前后加上[in]和[out], 接口内部的循环能减少一次。
===================================================================
如果知道这其中的道理,可以将avfilter_graph_create_filter接口中的名字使用自己喜欢的名字,之后在其中插入的AVFilterInOut的名字能够与其进行连接即可。

FFmpeg 中,可以使用 AVFilter 来实现在视频中画线条的功能。具体实现步骤如下: 1. 使用 avfilter_graph_alloc 函数创建一个 AVFilterGraph 结构体。 2. 使用 avfilter_get_by_name 函数获取 AVFilterContext 结构体中的 drawtext 滤镜。 3. 使用 avfilter_graph_create_filter 函数将 drawtext 滤镜添加到 AVFilterGraph 中。 4. 使用 avfilter_inout_alloc 函数创建输入和输出滤镜链。 5. 使用 avfilter_graph_parse_ptr 函数将输入和输出滤镜链添加到 AVFilterGraph 中。 6. 使用 avfilter_graph_config 函数进行过滤器图的配置。 7. 使用 avcodec_send_packet 和 avcodec_receive_frame 函数将原始视频数据发送给 AVFilter。 以下是一个简单的示例代码,用于在视频中画一条绿色的线: ```c AVFilterGraph *filter_graph = NULL; AVFilterContext *drawtext_ctx = NULL; AVFilterInOut *outputs = NULL, *inputs = NULL; int ret; //1. 创建 AVFilterGraph 结构体 filter_graph = avfilter_graph_alloc(); if (!filter_graph) { printf("Error creating filter graph.\n"); return -1; } //2. 获取 drawtext 滤镜 const AVFilter *filter = avfilter_get_by_name("drawtext"); if (!filter) { printf("Error finding drawtext filter.\n"); return -1; } //3. 将 drawtext 滤镜添加到 AVFilterGraph 中 ret = avfilter_graph_create_filter(&drawtext_ctx, filter, "line_filter", "text='This is a line', fontcolor=green, fontsize=20, x=100, y=100", NULL, filter_graph); if (ret < 0) { printf("Error creating drawtext filter.\n"); return -1; } //4. 创建输入和输出滤镜链 inputs = avfilter_inout_alloc(); outputs = avfilter_inout_alloc(); outputs->name = av_strdup("in"); outputs->filter_ctx = drawtext_ctx; outputs->pad_idx = 0; outputs->next = NULL; inputs->name = av_strdup("out"); inputs->filter_ctx = avfilter_graph_get_filter(filter_graph, "buffer"); inputs->pad_idx = 0; inputs->next = NULL; //5. 将输入和输出滤镜链添加到 AVFilterGraph 中 ret = avfilter_graph_parse_ptr(filter_graph, "buffer=video_size=1920x1080:pix_fmt=yuv420p:time_base=1/25:pixel_aspect=1/1 [in]; [in] line_filter [out]", &inputs, &outputs, NULL); if (ret < 0) { printf("Error parsing filter graph.\n"); return -1; } //6. 进行过滤器图的配置 ret = avfilter_graph_config(filter_graph, NULL); if (ret < 0) { printf("Error configuring filter graph.\n"); return -1; } //7. 发送视频帧到 AVFilter 中进行处理 AVPacket packet; AVFrame *frame = av_frame_alloc(); while (av_read_frame(format_context, &packet) >= 0) { if (packet.stream_index == video_stream_index) { ret = avcodec_send_packet(video_codec_context, &packet); if (ret < 0) { printf("Error sending packet.\n"); break; } while (ret >= 0) { ret = avcodec_receive_frame(video_codec_context, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } // 将视频帧发送给 AVFilter 进行处理 ret = av_buffersrc_add_frame_flags(inputs->filter_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF); if (ret < 0) { printf("Error adding frame to buffer source.\n"); break; } AVFrame *filtered_frame = av_frame_alloc(); ret = av_buffersink_get_frame(drawtext_ctx->inputs[0]->filter_ctx, filtered_frame); if (ret < 0) { printf("Error getting filtered frame.\n"); break; } // 将处理后的视频帧输出 ret = avcodec_send_frame(video_codec_context, filtered_frame); if (ret < 0) { printf("Error sending filtered frame.\n"); break; } } } av_packet_unref(&packet); } ``` 以上代码仅供参考,具体实现需要根据实际情况进行调整和修改。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值