27afb09d |
/*
* filter graph parser |
3fa77bde |
* Copyright (c) 2008 Vitor Sessak
* Copyright (c) 2007 Bobby Bingham |
27afb09d |
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <ctype.h>
#include <string.h>
|
372e2884 |
#include "libavutil/avstring.h" |
27afb09d |
#include "avfilter.h"
#include "avfiltergraph.h"
|
fd548e5b |
#define WHITESPACES " \n\t"
|
d2874a9d |
/**
* Link two filters together.
*
* @see avfilter_link()
*/ |
9710beaf |
static int link_filter(AVFilterContext *src, int srcpad, |
3a70bb2d |
AVFilterContext *dst, int dstpad, |
86909dd5 |
void *log_ctx) |
27afb09d |
{ |
99ac59ca |
int ret;
if ((ret = avfilter_link(src, srcpad, dst, dstpad))) { |
3a70bb2d |
av_log(log_ctx, AV_LOG_ERROR, |
bb90d855 |
"Cannot create the link %s:%d -> %s:%d\n", |
9710beaf |
src->filter->name, srcpad, dst->filter->name, dstpad); |
99ac59ca |
return ret; |
27afb09d |
}
return 0;
}
/** |
94b2120d |
* Parse the name of a link, which has the format "[linkname]".
*
* @return a pointer (that need to be freed after use) to the name
* between parenthesis |
27afb09d |
*/ |
86909dd5 |
static char *parse_link_name(const char **buf, void *log_ctx) |
27afb09d |
{ |
22260824 |
const char *start = *buf; |
bd80b349 |
char *name; |
27afb09d |
(*buf)++;
|
dd04911c |
name = av_get_token(buf, "]"); |
27afb09d |
|
f219eee5 |
if (!name[0]) { |
3a70bb2d |
av_log(log_ctx, AV_LOG_ERROR, |
22260824 |
"Bad (empty?) label found in the following: \"%s\".\n", start); |
27afb09d |
goto fail; |
22260824 |
} |
27afb09d |
|
f219eee5 |
if (*(*buf)++ != ']') { |
3a70bb2d |
av_log(log_ctx, AV_LOG_ERROR, |
22260824 |
"Mismatched '[' found in the following: \"%s\".\n", start); |
85cb8af7 |
fail: |
bd80b349 |
av_freep(&name); |
22260824 |
} |
bd80b349 |
return name; |
27afb09d |
}
|
98137a1a |
/**
* Create an instance of a filter, initialize and insert it in the
* filtergraph in *ctx.
*
* @param ctx the filtergraph context |
c58572f8 |
* @param put here a filter context in case of successful creation and configuration, NULL otherwise. |
98137a1a |
* @param index an index which is supposed to be unique for each filter instance added to the filtergraph
* @param filt_name the name of the filter to create
* @param args the arguments provided to the filter during its initialization
* @param log_ctx the log context to use |
c58572f8 |
* @return 0 in case of success, a negative AVERROR code otherwise |
98137a1a |
*/ |
c58572f8 |
static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index, |
86909dd5 |
const char *filt_name, const char *args, void *log_ctx) |
8e74c889 |
{ |
e4a5f397 |
AVFilter *filt; |
8e74c889 |
char inst_name[30]; |
b5049814 |
char tmp_args[256]; |
c58572f8 |
int ret; |
8e74c889 |
|
3bf3fc0e |
snprintf(inst_name, sizeof(inst_name), "Parsed filter %d %s", index, filt_name); |
8e74c889 |
|
e4a5f397 |
filt = avfilter_get_by_name(filt_name); |
5e600185 |
|
f219eee5 |
if (!filt) { |
8e74c889 |
av_log(log_ctx, AV_LOG_ERROR, |
bb90d855 |
"No such filter: '%s'\n", filt_name); |
c58572f8 |
return AVERROR(EINVAL); |
8e74c889 |
}
|
c58572f8 |
ret = avfilter_open(filt_ctx, filt, inst_name);
if (!*filt_ctx) { |
8e74c889 |
av_log(log_ctx, AV_LOG_ERROR, |
bb90d855 |
"Error creating filter '%s'\n", filt_name); |
c58572f8 |
return ret; |
8e74c889 |
}
|
c58572f8 |
if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) { |
24de0edb |
avfilter_free(*filt_ctx); |
c58572f8 |
return ret; |
64b164f4 |
} |
8e74c889 |
|
2f86e7bd |
if (!strcmp(filt_name, "scale") && args && !strstr(args, "flags")) { |
b5049814 |
snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
args, ctx->scale_sws_opts);
args = tmp_args;
}
|
c58572f8 |
if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) { |
8e74c889 |
av_log(log_ctx, AV_LOG_ERROR, |
bb90d855 |
"Error initializing filter '%s' with args '%s'\n", filt_name, args); |
c58572f8 |
return ret; |
8e74c889 |
}
|
c58572f8 |
return 0; |
8e74c889 |
}
|
f5cbde2e |
/** |
55b2a9ba |
* Parse a string of the form FILTER_NAME[=PARAMS], and create a
* corresponding filter instance which is added to graph with
* create_filter().
*
* @param filt_ctx put here a pointer to the created filter context on
* success, NULL otherwise
* @param buf pointer to the buffer to parse, *buf will be updated to
* point to the char next after the parsed string
* @param index an index which is assigned to the created filter
* instance, and which is supposed to be unique for each filter
* instance added to the filtergraph
* @return 0 in case of success, a negative AVERROR code otherwise |
f5cbde2e |
*/ |
688b9dad |
static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph, |
86909dd5 |
int index, void *log_ctx) |
f5cbde2e |
{ |
ba3fed2f |
char *opts = NULL; |
15a316c0 |
char *name = av_get_token(buf, "=,;[\n"); |
c58572f8 |
int ret; |
f5cbde2e |
|
f219eee5 |
if (**buf == '=') { |
f5cbde2e |
(*buf)++; |
15a316c0 |
opts = av_get_token(buf, "[],;\n"); |
12849837 |
} |
f5cbde2e |
|
688b9dad |
ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx); |
64b164f4 |
av_free(name);
av_free(opts); |
688b9dad |
return ret; |
f5cbde2e |
}
|
c5354942 |
AVFilterInOut *avfilter_inout_alloc(void) |
27afb09d |
{ |
c5354942 |
return av_mallocz(sizeof(AVFilterInOut));
}
void avfilter_inout_free(AVFilterInOut **inout)
{
while (*inout) {
AVFilterInOut *next = (*inout)->next;
av_freep(&(*inout)->name);
av_freep(inout);
*inout = next; |
27afb09d |
}
}
|
c9987633 |
static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
{
AVFilterInOut *ret;
|
f219eee5 |
while (*links && strcmp((*links)->name, label)) |
f6557d5e |
links = &((*links)->next); |
c9987633 |
|
f6557d5e |
ret = *links; |
c9987633 |
|
f219eee5 |
if (ret) |
f6557d5e |
*links = ret->next; |
c9987633 |
return ret;
}
|
e97908ee |
static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
{
element->next = *inouts;
*inouts = element;
} |
c9987633 |
|
ed581e65 |
static int link_filter_inouts(AVFilterContext *filt_ctx, |
7f9b3266 |
AVFilterInOut **curr_inputs, |
86909dd5 |
AVFilterInOut **open_inputs, void *log_ctx) |
c9987633 |
{ |
ed581e65 |
int pad = filt_ctx->input_count, ret; |
c9987633 |
|
f219eee5 |
while (pad--) { |
7f9b3266 |
AVFilterInOut *p = *curr_inputs; |
f219eee5 |
if (!p) { |
c9987633 |
av_log(log_ctx, AV_LOG_ERROR,
"Not enough inputs specified for the \"%s\" filter.\n", |
ed581e65 |
filt_ctx->filter->name); |
684ade49 |
return AVERROR(EINVAL); |
c9987633 |
}
|
7f9b3266 |
*curr_inputs = (*curr_inputs)->next; |
4d11beb2 |
|
7313132b |
if (p->filter_ctx) {
if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0) |
684ade49 |
return ret; |
64b164f4 |
av_free(p->name); |
c9987633 |
av_free(p);
} else { |
7313132b |
p->filter_ctx = filt_ctx; |
c9987633 |
p->pad_idx = pad; |
7f9b3266 |
insert_inout(open_inputs, p); |
c9987633 |
}
}
|
f219eee5 |
if (*curr_inputs) { |
c9987633 |
av_log(log_ctx, AV_LOG_ERROR,
"Too many inputs specified for the \"%s\" filter.\n", |
ed581e65 |
filt_ctx->filter->name); |
684ade49 |
return AVERROR(EINVAL); |
c9987633 |
}
|
ed581e65 |
pad = filt_ctx->output_count; |
f219eee5 |
while (pad--) { |
c956dd43 |
AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut)); |
fbd97184 |
if (!currlinkn)
return AVERROR(ENOMEM); |
7313132b |
currlinkn->filter_ctx = filt_ctx; |
c9987633 |
currlinkn->pad_idx = pad; |
7f9b3266 |
insert_inout(curr_inputs, currlinkn); |
c9987633 |
}
return 0;
}
|
7f9b3266 |
static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs, |
86909dd5 |
AVFilterInOut **open_outputs, void *log_ctx) |
27afb09d |
{ |
c9987633 |
int pad = 0;
|
f219eee5 |
while (**buf == '[') { |
bd80b349 |
char *name = parse_link_name(buf, log_ctx); |
b2ac16da |
AVFilterInOut *match; |
22260824 |
|
f219eee5 |
if (!name) |
42e7f6d7 |
return AVERROR(EINVAL); |
22260824 |
|
7f9b3266 |
/* First check if the label is not in the open_outputs list */
match = extract_inout(name, open_outputs); |
cf4f7d38 |
|
f219eee5 |
if (match) { |
64fbf5e2 |
av_free(name); |
0de3407b |
} else {
/* Not in the list, so add it as an input */ |
00b3ca3c |
if (!(match = av_mallocz(sizeof(AVFilterInOut))))
return AVERROR(ENOMEM); |
c880791f |
match->name = name;
match->pad_idx = pad; |
cf4f7d38 |
} |
e97908ee |
|
7f9b3266 |
insert_inout(curr_inputs, match); |
e97908ee |
|
fd548e5b |
*buf += strspn(*buf, WHITESPACES); |
c9987633 |
pad++; |
27afb09d |
} |
cf4f7d38 |
|
27afb09d |
return pad;
}
|
7f9b3266 |
static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
AVFilterInOut **open_inputs, |
86909dd5 |
AVFilterInOut **open_outputs, void *log_ctx) |
9710beaf |
{ |
0cc8b659 |
int ret, pad = 0; |
c9987633 |
|
f219eee5 |
while (**buf == '[') { |
bd80b349 |
char *name = parse_link_name(buf, log_ctx); |
c9987633 |
AVFilterInOut *match;
|
7f9b3266 |
AVFilterInOut *input = *curr_inputs; |
668673f1 |
if (!input) {
av_log(log_ctx, AV_LOG_ERROR,
"No output pad can be associated to link label '%s'.\n",
name);
return AVERROR(EINVAL);
} |
7f9b3266 |
*curr_inputs = (*curr_inputs)->next; |
443c10ef |
|
f219eee5 |
if (!name) |
0cc8b659 |
return AVERROR(EINVAL); |
c9987633 |
|
7f9b3266 |
/* First check if the label is not in the open_inputs list */
match = extract_inout(name, open_inputs); |
c9987633 |
|
f219eee5 |
if (match) { |
7313132b |
if ((ret = link_filter(input->filter_ctx, input->pad_idx,
match->filter_ctx, match->pad_idx, log_ctx)) < 0) |
0cc8b659 |
return ret; |
64b164f4 |
av_free(match->name);
av_free(name); |
c9987633 |
av_free(match); |
7baa6210 |
av_free(input); |
0de3407b |
} else { |
7f9b3266 |
/* Not in the list, so add the first input as a open_output */ |
7baa6210 |
input->name = name; |
7f9b3266 |
insert_inout(open_outputs, input); |
c9987633 |
} |
fd548e5b |
*buf += strspn(*buf, WHITESPACES); |
c9987633 |
pad++; |
8095a014 |
} |
9710beaf |
|
c9987633 |
return pad;
} |
9710beaf |
|
86a47378 |
int avfilter_graph_parse(AVFilterGraph *graph, const char *filters, |
6119b23a |
AVFilterInOut **open_inputs, AVFilterInOut **open_outputs,
void *log_ctx) |
27afb09d |
{ |
c24f76b9 |
int index = 0, ret; |
27afb09d |
char chr = 0;
|
7f9b3266 |
AVFilterInOut *curr_inputs = NULL; |
27afb09d |
do { |
9710beaf |
AVFilterContext *filter; |
9241cd20 |
const char *filterchain = filters; |
fd548e5b |
filters += strspn(filters, WHITESPACES); |
27afb09d |
|
6119b23a |
if ((ret = parse_inputs(&filters, &curr_inputs, open_outputs, log_ctx)) < 0) |
27afb09d |
goto fail;
|
c24f76b9 |
if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0) |
22260824 |
goto fail;
|
f219eee5 |
if (filter->input_count == 1 && !curr_inputs && !index) { |
e916c2ac |
/* First input can be omitted if it is "[in]" */ |
c9987633 |
const char *tmp = "[in]"; |
6119b23a |
if ((ret = parse_inputs(&tmp, &curr_inputs, open_outputs, log_ctx)) < 0) |
27afb09d |
goto fail;
}
|
6119b23a |
if ((ret = link_filter_inouts(filter, &curr_inputs, open_inputs, log_ctx)) < 0) |
c9987633 |
goto fail; |
da790674 |
|
6119b23a |
if ((ret = parse_outputs(&filters, &curr_inputs, open_inputs, open_outputs, |
c24f76b9 |
log_ctx)) < 0) |
e84f0b62 |
goto fail;
|
fd548e5b |
filters += strspn(filters, WHITESPACES); |
27afb09d |
chr = *filters++;
|
f219eee5 |
if (chr == ';' && curr_inputs) { |
c9987633 |
av_log(log_ctx, AV_LOG_ERROR, |
9241cd20 |
"Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
filterchain); |
c24f76b9 |
ret = AVERROR(EINVAL); |
c9987633 |
goto fail; |
27afb09d |
} |
c9987633 |
index++; |
f219eee5 |
} while (chr == ',' || chr == ';'); |
27afb09d |
|
fd51ff16 |
if (chr) { |
78471234 |
av_log(log_ctx, AV_LOG_ERROR,
"Unable to parse graph description substring: \"%s\"\n",
filters - 1); |
c24f76b9 |
ret = AVERROR(EINVAL); |
78471234 |
goto fail;
}
|
8055433b |
if (open_inputs && *open_inputs && !strcmp((*open_inputs)->name, "out") && curr_inputs) { |
e916c2ac |
/* Last output can be omitted if it is "[out]" */ |
c9987633 |
const char *tmp = "[out]"; |
6119b23a |
if ((ret = parse_outputs(&tmp, &curr_inputs, open_inputs, open_outputs, |
c24f76b9 |
log_ctx)) < 0) |
27afb09d |
goto fail;
}
return 0;
fail: |
c68be8e1 |
for (; graph->filter_count > 0; graph->filter_count--)
avfilter_free(graph->filters[graph->filter_count - 1]);
av_freep(&graph->filters); |
c5354942 |
avfilter_inout_free(open_inputs);
avfilter_inout_free(open_outputs);
avfilter_inout_free(&curr_inputs); |
c24f76b9 |
return ret; |
27afb09d |
} |