]> git.sesse.net Git - ffmpeg/blob - libavfilter/graphparser.c
graphparser: simplify condition in avfilter_graph_parse()
[ffmpeg] / libavfilter / graphparser.c
1 /*
2  * filter graph parser
3  * Copyright (c) 2008 Vitor Sessak
4  * Copyright (c) 2007 Bobby Bingham
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include <ctype.h>
24 #include <string.h>
25
26 #include "libavutil/avstring.h"
27 #include "avfilter.h"
28 #include "avfiltergraph.h"
29
30 #define WHITESPACES " \n\t"
31
32 /**
33  * Link two filters together.
34  *
35  * @see avfilter_link()
36  */
37 static int link_filter(AVFilterContext *src, int srcpad,
38                        AVFilterContext *dst, int dstpad,
39                        void *log_ctx)
40 {
41     int ret;
42     if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
43         av_log(log_ctx, AV_LOG_ERROR,
44                "Cannot create the link %s:%d -> %s:%d\n",
45                src->filter->name, srcpad, dst->filter->name, dstpad);
46         return ret;
47     }
48
49     return 0;
50 }
51
52 /**
53  * Parse the name of a link, which has the format "[linkname]".
54  *
55  * @return a pointer (that need to be freed after use) to the name
56  * between parenthesis
57  */
58 static char *parse_link_name(const char **buf, void *log_ctx)
59 {
60     const char *start = *buf;
61     char *name;
62     (*buf)++;
63
64     name = av_get_token(buf, "]");
65
66     if (!name[0]) {
67         av_log(log_ctx, AV_LOG_ERROR,
68                "Bad (empty?) label found in the following: \"%s\".\n", start);
69         goto fail;
70     }
71
72     if (*(*buf)++ != ']') {
73         av_log(log_ctx, AV_LOG_ERROR,
74                "Mismatched '[' found in the following: \"%s\".\n", start);
75     fail:
76         av_freep(&name);
77     }
78
79     return name;
80 }
81
82 /**
83  * Create an instance of a filter, initialize and insert it in the
84  * filtergraph in *ctx.
85  *
86  * @param filt_ctx put here a filter context in case of successful creation and configuration, NULL otherwise.
87  * @param ctx the filtergraph context
88  * @param index an index which is supposed to be unique for each filter instance added to the filtergraph
89  * @param filt_name the name of the filter to create
90  * @param args the arguments provided to the filter during its initialization
91  * @param log_ctx the log context to use
92  * @return 0 in case of success, a negative AVERROR code otherwise
93  */
94 static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
95                          const char *filt_name, const char *args, void *log_ctx)
96 {
97     AVFilter *filt;
98     char inst_name[30];
99     char tmp_args[256];
100     int ret;
101
102     snprintf(inst_name, sizeof(inst_name), "Parsed filter %d %s", index, filt_name);
103
104     filt = avfilter_get_by_name(filt_name);
105
106     if (!filt) {
107         av_log(log_ctx, AV_LOG_ERROR,
108                "No such filter: '%s'\n", filt_name);
109         return AVERROR(EINVAL);
110     }
111
112     ret = avfilter_open(filt_ctx, filt, inst_name);
113     if (!*filt_ctx) {
114         av_log(log_ctx, AV_LOG_ERROR,
115                "Error creating filter '%s'\n", filt_name);
116         return ret;
117     }
118
119     if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) {
120         avfilter_free(*filt_ctx);
121         return ret;
122     }
123
124     if (!strcmp(filt_name, "scale") && args && !strstr(args, "flags")) {
125         snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
126                  args, ctx->scale_sws_opts);
127         args = tmp_args;
128     }
129
130     if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
131         av_log(log_ctx, AV_LOG_ERROR,
132                "Error initializing filter '%s' with args '%s'\n", filt_name, args);
133         return ret;
134     }
135
136     return 0;
137 }
138
139 /**
140  * Parse a string of the form FILTER_NAME[=PARAMS], and create a
141  * corresponding filter instance which is added to graph with
142  * create_filter().
143  *
144  * @param filt_ctx put here a pointer to the created filter context on
145  * success, NULL otherwise
146  * @param buf pointer to the buffer to parse, *buf will be updated to
147  * point to the char next after the parsed string
148  * @param index an index which is assigned to the created filter
149  * instance, and which is supposed to be unique for each filter
150  * instance added to the filtergraph
151  * @return 0 in case of success, a negative AVERROR code otherwise
152  */
153 static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
154                         int index, void *log_ctx)
155 {
156     char *opts = NULL;
157     char *name = av_get_token(buf, "=,;[\n");
158     int ret;
159
160     if (**buf == '=') {
161         (*buf)++;
162         opts = av_get_token(buf, "[],;\n");
163     }
164
165     ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
166     av_free(name);
167     av_free(opts);
168     return ret;
169 }
170
171 AVFilterInOut *avfilter_inout_alloc(void)
172 {
173     return av_mallocz(sizeof(AVFilterInOut));
174 }
175
176 void avfilter_inout_free(AVFilterInOut **inout)
177 {
178     while (*inout) {
179         AVFilterInOut *next = (*inout)->next;
180         av_freep(&(*inout)->name);
181         av_freep(inout);
182         *inout = next;
183     }
184 }
185
186 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
187 {
188     AVFilterInOut *ret;
189
190     while (*links && strcmp((*links)->name, label))
191         links = &((*links)->next);
192
193     ret = *links;
194
195     if (ret)
196         *links = ret->next;
197
198     return ret;
199 }
200
201 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
202 {
203     element->next = *inouts;
204     *inouts = element;
205 }
206
207 static int link_filter_inouts(AVFilterContext *filt_ctx,
208                               AVFilterInOut **curr_inputs,
209                               AVFilterInOut **open_inputs, void *log_ctx)
210 {
211     int pad = filt_ctx->input_count, ret;
212
213     while (pad--) {
214         AVFilterInOut *p = *curr_inputs;
215         if (!p) {
216             av_log(log_ctx, AV_LOG_ERROR,
217                    "Not enough inputs specified for the \"%s\" filter.\n",
218                    filt_ctx->filter->name);
219             return AVERROR(EINVAL);
220         }
221
222         *curr_inputs = (*curr_inputs)->next;
223
224         if (p->filter_ctx) {
225             if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
226                 return ret;
227             av_free(p->name);
228             av_free(p);
229         } else {
230             p->filter_ctx = filt_ctx;
231             p->pad_idx = pad;
232             insert_inout(open_inputs, p);
233         }
234     }
235
236     if (*curr_inputs) {
237         av_log(log_ctx, AV_LOG_ERROR,
238                "Too many inputs specified for the \"%s\" filter.\n",
239                filt_ctx->filter->name);
240         return AVERROR(EINVAL);
241     }
242
243     pad = filt_ctx->output_count;
244     while (pad--) {
245         AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
246         if (!currlinkn)
247             return AVERROR(ENOMEM);
248         currlinkn->filter_ctx  = filt_ctx;
249         currlinkn->pad_idx = pad;
250         insert_inout(curr_inputs, currlinkn);
251     }
252
253     return 0;
254 }
255
256 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
257                         AVFilterInOut **open_outputs, void *log_ctx)
258 {
259     int pad = 0;
260
261     while (**buf == '[') {
262         char *name = parse_link_name(buf, log_ctx);
263         AVFilterInOut *match;
264
265         if (!name)
266             return AVERROR(EINVAL);
267
268         /* First check if the label is not in the open_outputs list */
269         match = extract_inout(name, open_outputs);
270
271         if (match) {
272             av_free(name);
273         } else {
274             /* Not in the list, so add it as an input */
275             if (!(match = av_mallocz(sizeof(AVFilterInOut))))
276                 return AVERROR(ENOMEM);
277             match->name    = name;
278             match->pad_idx = pad;
279         }
280
281         insert_inout(curr_inputs, match);
282
283         *buf += strspn(*buf, WHITESPACES);
284         pad++;
285     }
286
287     return pad;
288 }
289
290 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
291                          AVFilterInOut **open_inputs,
292                          AVFilterInOut **open_outputs, void *log_ctx)
293 {
294     int ret, pad = 0;
295
296     while (**buf == '[') {
297         char *name = parse_link_name(buf, log_ctx);
298         AVFilterInOut *match;
299
300         AVFilterInOut *input = *curr_inputs;
301         if (!input) {
302             av_log(log_ctx, AV_LOG_ERROR,
303                    "No output pad can be associated to link label '%s'.\n",
304                    name);
305             return AVERROR(EINVAL);
306         }
307         *curr_inputs = (*curr_inputs)->next;
308
309         if (!name)
310             return AVERROR(EINVAL);
311
312         /* First check if the label is not in the open_inputs list */
313         match = extract_inout(name, open_inputs);
314
315         if (match) {
316             if ((ret = link_filter(input->filter_ctx, input->pad_idx,
317                                    match->filter_ctx, match->pad_idx, log_ctx)) < 0)
318                 return ret;
319             av_free(match->name);
320             av_free(name);
321             av_free(match);
322             av_free(input);
323         } else {
324             /* Not in the list, so add the first input as a open_output */
325             input->name = name;
326             insert_inout(open_outputs, input);
327         }
328         *buf += strspn(*buf, WHITESPACES);
329         pad++;
330     }
331
332     return pad;
333 }
334
335 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
336                          AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
337                          void *log_ctx)
338 {
339     int index = 0, ret = 0;
340     char chr = 0;
341
342     AVFilterInOut *curr_inputs = NULL;
343     AVFilterInOut *open_inputs  = open_inputs_ptr  ? *open_inputs_ptr  : NULL;
344     AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
345
346     do {
347         AVFilterContext *filter;
348         const char *filterchain = filters;
349         filters += strspn(filters, WHITESPACES);
350
351         if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
352             goto end;
353
354         if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
355             goto end;
356
357         if (filter->input_count == 1 && !curr_inputs && !index) {
358             /* First input pad, assume it is "[in]" if not specified */
359             const char *tmp = "[in]";
360             if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
361                 goto end;
362         }
363
364         if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
365             goto end;
366
367         if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
368                                  log_ctx)) < 0)
369             goto end;
370
371         filters += strspn(filters, WHITESPACES);
372         chr = *filters++;
373
374         if (chr == ';' && curr_inputs) {
375             av_log(log_ctx, AV_LOG_ERROR,
376                    "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
377                    filterchain);
378             ret = AVERROR(EINVAL);
379             goto end;
380         }
381         index++;
382     } while (chr == ',' || chr == ';');
383
384     if (chr) {
385         av_log(log_ctx, AV_LOG_ERROR,
386                "Unable to parse graph description substring: \"%s\"\n",
387                filters - 1);
388         ret = AVERROR(EINVAL);
389         goto end;
390     }
391
392     if (curr_inputs) {
393         /* Last output pad, assume it is "[out]" if not specified */
394         const char *tmp = "[out]";
395         if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
396                                  log_ctx)) < 0)
397             goto end;
398     }
399
400 end:
401     /* clear open_in/outputs only if not passed as parameters */
402     if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
403     else avfilter_inout_free(&open_inputs);
404     if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
405     else avfilter_inout_free(&open_outputs);
406     avfilter_inout_free(&curr_inputs);
407
408     if (ret < 0) {
409         for (; graph->filter_count > 0; graph->filter_count--)
410             avfilter_free(graph->filters[graph->filter_count - 1]);
411         av_freep(&graph->filters);
412     }
413     return ret;
414 }