avfilter_graph_parse_ptr(avfilter_graph_parse2类同)实现中主要涉及的四个函数:
1、parse_inputs
2、parse_filter
3、link_filter_inouts
4、parse_outputs
函数本身及主要函数以源代码加注释的方式体现:
static int link_filter_inouts(AVFilterContext *filt_ctx,
AVFilterInOut **curr_inputs,
AVFilterInOut **open_inputs, void *log_ctx)
{
int pad, ret;
for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
AVFilterInOut *p = *curr_inputs;
if (p) { // 从curr_inputs中提取一个AVFilterInOut
*curr_inputs = (*curr_inputs)->next;
p->next = NULL;
} else if (!(p = av_mallocz(sizeof(*p)))) // 如果p不是事先定义的,进行创建
return AVERROR(ENOMEM);
if (p->filter_ctx) {
ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx); // 将从curr_inputs中获取的AVFilterInOut与传入的filter实例对应的pad进行连接
av_freep(&p->name);
av_freep(&p);
if (ret < 0)
return ret;
} else { // 构建open_input放入反馈列表中. 例如:[in]fps=fps=20[fps];[fps]scale=w=1440:h=960[out], p代表scale的输入
p->filter_ctx = filt_ctx;
p->pad_idx = pad;
append_inout(open_inputs, &p);
}
}
if (*curr_inputs) { // 之前解析出该filter的inputs数量超过filter实例中设置的数量
av_log(log_ctx, AV_LOG_ERROR,
"Too many inputs specified for the \"%s\" filter.\n",
filt_ctx->filter->name);
return AVERROR(EINVAL);
}
pad = filt_ctx->nb_outputs;
while (pad--) {
AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut)); // 创建该filter的输出AVFilterInOut
if (!currlinkn)
return AVERROR(ENOMEM);
currlinkn->filter_ctx = filt_ctx;
currlinkn->pad_idx = pad;
insert_inout(curr_inputs, currlinkn); // 放入curr_inputs中输出,供之后使用
}
return 0;
}
static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
AVFilterInOut **open_outputs, void *log_ctx)
{
AVFilterInOut *parsed_inputs = NULL;
int pad = 0;
while (**buf == '[') { // 此处while循环是为了对应多个input的情况
char *name = parse_link_name(buf, log_ctx); // 解析link字符串中的[]中的name
AVFilterInOut *match;
if (!name) {
avfilter_inout_free(&parsed_inputs);
return AVERROR(EINVAL);
}
/* First check if the label is not in the open_outputs list */
match = extract_inout(name, open_outputs); // 根据输入连接name, 首先从打开的输出open_outputs中查找AVFilterInOut->name=name的AVFilterInOut
if (match) {
av_free(name); // 释放的是name,而不是AVFilterInOut,该name为parse_link_name获取时申请的空间,在此进行释放
} else {
/* Not in the list, so add it as an input */
if (!(match = av_mallocz(sizeof(AVFilterInOut)))) {
avfilter_inout_free(&parsed_inputs);
av_free(name);
return AVERROR(ENOMEM);
}
match->name = name;
match->pad_idx = pad;
}
append_inout(&parsed_inputs, &match); // 将match连接到parsed_inputs的后面
*buf += strspn(*buf, WHITESPACES);
pad++;
}
append_inout(&parsed_inputs, curr_inputs);
*curr_inputs = parsed_inputs;
return pad;
}
static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
AVFilterInOut **open_inputs,
AVFilterInOut **open_outputs, void *log_ctx)
{
int ret, pad = 0;
while (**buf == '[') { // 抽出输出link的name
char *name = parse_link_name(buf, log_ctx);
AVFilterInOut *match;
AVFilterInOut *input = *curr_inputs;
if (!name)
return AVERROR(EINVAL);
if (!input) {
av_log(log_ctx, AV_LOG_ERROR,
"No output pad can be associated to link label '%s'.\n", name);
av_free(name);
return AVERROR(EINVAL);
}
*curr_inputs = (*curr_inputs)->next;
/* First check if the label is not in the open_inputs list */
match = extract_inout(name, open_inputs); // 找出输出名为name的AVFilterInOut
if (match) { // 前一个filter与找到输出AVFilterInOut进行连接
ret = link_filter(input->filter_ctx, input->pad_idx, // input代表前一个filter的输出
match->filter_ctx, match->pad_idx, log_ctx);
av_freep(&match->name);
av_freep(&name);
av_freep(&match);
av_freep(&input);
if (ret < 0)
return ret;
} else {
/* Not in the list, so add the first input as an open_output */
input->name = name;
insert_inout(open_outputs, input);
}
*buf += strspn(*buf, WHITESPACES);
pad++;
}
return pad;
}
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) // 对filters的第一组的[in]s,从opens_outputs 找到或生成 对应AVFilterInOut(从curr_inputs中输出)
goto end;
if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0) // 创建第一个filter实例
goto end;
if (filter->nb_inputs == 1 && !curr_inputs && !index) {
/* First input pad, assume it is "[in]" if not specified */
const char *tmp = "[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) // 将filter与其输入进行连接,并生成其对应的输出,通过curr_inputs反馈出来
goto end;
if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs, // 查找filter对应的输出AVFilter。如果存在,就进行连接;如果不存在,将该filter的输出AVFilter加入到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]";
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;
}