]> git.sesse.net Git - ffmpeg/blob - libavfilter/graphparser.c
1c6d0ba92ff7b051a988bd40c39f6efa11cc6af0
[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 "graphparser.h"
27 #include "avfilter.h"
28 #include "avfiltergraph.h"
29 #include "parseutils.h"
30
31 #define WHITESPACES " \n\t"
32
33 static int link_filter(AVFilterContext *src, int srcpad,
34                        AVFilterContext *dst, int dstpad,
35                        AVClass *log_ctx)
36 {
37     int ret;
38     if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
39         av_log(log_ctx, AV_LOG_ERROR,
40                "Cannot create the link %s:%d -> %s:%d\n",
41                src->filter->name, srcpad, dst->filter->name, dstpad);
42         return ret;
43     }
44
45     return 0;
46 }
47
48 /**
49  * Parse the name of a link, which has the format "[linkname]".
50  *
51  * @return a pointer (that need to be freed after use) to the name
52  * between parenthesis
53  */
54 static char *parse_link_name(const char **buf, AVClass *log_ctx)
55 {
56     const char *start = *buf;
57     char *name;
58     (*buf)++;
59
60     name = av_get_token(buf, "]");
61
62     if (!name[0]) {
63         av_log(log_ctx, AV_LOG_ERROR,
64                "Bad (empty?) label found in the following: \"%s\".\n", start);
65         goto fail;
66     }
67
68     if (*(*buf)++ != ']') {
69         av_log(log_ctx, AV_LOG_ERROR,
70                "Mismatched '[' found in the following: \"%s\".\n", start);
71     fail:
72         av_freep(&name);
73     }
74
75     return name;
76 }
77
78 static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
79                                       const char *filt_name, const char *args,
80                                       AVClass *log_ctx)
81 {
82     AVFilterContext *filt_ctx;
83
84     AVFilter *filt;
85     char inst_name[30];
86     char tmp_args[256];
87
88     snprintf(inst_name, sizeof(inst_name), "Filter %d %s", index, filt_name);
89
90     filt = avfilter_get_by_name(filt_name);
91
92     if (!filt) {
93         av_log(log_ctx, AV_LOG_ERROR,
94                "No such filter: '%s'\n", filt_name);
95         return NULL;
96     }
97
98     filt_ctx = avfilter_open(filt, inst_name);
99     if (!filt_ctx) {
100         av_log(log_ctx, AV_LOG_ERROR,
101                "Error creating filter '%s'\n", filt_name);
102         return NULL;
103     }
104
105     if (avfilter_graph_add_filter(ctx, filt_ctx) < 0) {
106         avfilter_destroy(filt_ctx);
107         return NULL;
108     }
109
110     if (!strcmp(filt_name, "scale") && !strstr(args, "flags")) {
111         snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
112                  args, ctx->scale_sws_opts);
113         args = tmp_args;
114     }
115
116     if (avfilter_init_filter(filt_ctx, args, NULL)) {
117         av_log(log_ctx, AV_LOG_ERROR,
118                "Error initializing filter '%s' with args '%s'\n", filt_name, args);
119         return NULL;
120     }
121
122     return filt_ctx;
123 }
124
125 /**
126  * Parse "filter=params"
127  */
128 static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
129                                      int index, AVClass *log_ctx)
130 {
131     char *opts = NULL;
132     char *name = av_get_token(buf, "=,;[\n");
133     AVFilterContext *ret;
134
135     if (**buf == '=') {
136         (*buf)++;
137         opts = av_get_token(buf, "[],;\n");
138     }
139
140     ret = create_filter(graph, index, name, opts, log_ctx);
141     av_free(name);
142     av_free(opts);
143     return ret;
144 }
145
146 static void free_inout(AVFilterInOut *head)
147 {
148     while (head) {
149         AVFilterInOut *next = head->next;
150         av_free(head->name);
151         av_free(head);
152         head = next;
153     }
154 }
155
156 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
157 {
158     AVFilterInOut *ret;
159
160     while (*links && strcmp((*links)->name, label))
161         links = &((*links)->next);
162
163     ret = *links;
164
165     if (ret)
166         *links = ret->next;
167
168     return ret;
169 }
170
171 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
172 {
173     element->next = *inouts;
174     *inouts = element;
175 }
176
177 static int link_filter_inouts(AVFilterContext *filter,
178                               AVFilterInOut **curr_inputs,
179                               AVFilterInOut **open_inputs, AVClass *log_ctx)
180 {
181     int pad = filter->input_count;
182
183     while (pad--) {
184         AVFilterInOut *p = *curr_inputs;
185         if (!p) {
186             av_log(log_ctx, AV_LOG_ERROR,
187                    "Not enough inputs specified for the \"%s\" filter.\n",
188                    filter->filter->name);
189             return -1;
190         }
191
192         *curr_inputs = (*curr_inputs)->next;
193
194         if (p->filter) {
195             if (link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
196                 return -1;
197             av_free(p->name);
198             av_free(p);
199         } else {
200             p->filter = filter;
201             p->pad_idx = pad;
202             insert_inout(open_inputs, p);
203         }
204     }
205
206     if (*curr_inputs) {
207         av_log(log_ctx, AV_LOG_ERROR,
208                "Too many inputs specified for the \"%s\" filter.\n",
209                filter->filter->name);
210         return -1;
211     }
212
213     pad = filter->output_count;
214     while (pad--) {
215         AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
216         currlinkn->filter  = filter;
217         currlinkn->pad_idx = pad;
218         insert_inout(curr_inputs, currlinkn);
219     }
220
221     return 0;
222 }
223
224 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
225                         AVFilterInOut **open_outputs, AVClass *log_ctx)
226 {
227     int pad = 0;
228
229     while (**buf == '[') {
230         char *name = parse_link_name(buf, log_ctx);
231         AVFilterInOut *match;
232
233         if (!name)
234             return -1;
235
236         /* First check if the label is not in the open_outputs list */
237         match = extract_inout(name, open_outputs);
238
239         if (match) {
240             av_free(name);
241         } else {
242             /* Not in the list, so add it as an input */
243             match = av_mallocz(sizeof(AVFilterInOut));
244             match->name    = name;
245             match->pad_idx = pad;
246         }
247
248         insert_inout(curr_inputs, match);
249
250         *buf += strspn(*buf, WHITESPACES);
251         pad++;
252     }
253
254     return pad;
255 }
256
257 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
258                          AVFilterInOut **open_inputs,
259                          AVFilterInOut **open_outputs, AVClass *log_ctx)
260 {
261     int pad = 0;
262
263     while (**buf == '[') {
264         char *name = parse_link_name(buf, log_ctx);
265         AVFilterInOut *match;
266
267         AVFilterInOut *input = *curr_inputs;
268         *curr_inputs = (*curr_inputs)->next;
269
270         if (!name)
271             return -1;
272
273         /* First check if the label is not in the open_inputs list */
274         match = extract_inout(name, open_inputs);
275
276         if (match) {
277             if (link_filter(input->filter, input->pad_idx,
278                             match->filter, match->pad_idx, log_ctx) < 0)
279                 return -1;
280             av_free(match->name);
281             av_free(name);
282             av_free(match);
283             av_free(input);
284         } else {
285             /* Not in the list, so add the first input as a open_output */
286             input->name = name;
287             insert_inout(open_outputs, input);
288         }
289         *buf += strspn(*buf, WHITESPACES);
290         pad++;
291     }
292
293     return pad;
294 }
295
296 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
297                          AVFilterInOut *open_inputs,
298                          AVFilterInOut *open_outputs, AVClass *log_ctx)
299 {
300     int index = 0;
301     char chr = 0;
302
303     AVFilterInOut *curr_inputs = NULL;
304
305     do {
306         AVFilterContext *filter;
307         filters += strspn(filters, WHITESPACES);
308
309         if (parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx) < 0)
310             goto fail;
311
312         filter = parse_filter(&filters, graph, index, log_ctx);
313
314         if (!filter)
315             goto fail;
316
317         if (filter->input_count == 1 && !curr_inputs && !index) {
318             /* First input can be omitted if it is "[in]" */
319             const char *tmp = "[in]";
320             if(parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx) < 0)
321                 goto fail;
322         }
323
324         if (link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx) < 0)
325             goto fail;
326
327         if (parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
328                          log_ctx) < 0)
329             goto fail;
330
331         filters += strspn(filters, WHITESPACES);
332         chr = *filters++;
333
334         if (chr == ';' && curr_inputs) {
335             av_log(log_ctx, AV_LOG_ERROR,
336                    "Could not find a output to link when parsing \"%s\"\n",
337                    filters - 1);
338             goto fail;
339         }
340         index++;
341     } while (chr == ',' || chr == ';');
342
343     if (chr) {
344         av_log(log_ctx, AV_LOG_ERROR,
345                "Unable to parse graph description substring: \"%s\"\n",
346                filters - 1);
347         goto fail;
348     }
349
350     if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
351         /* Last output can be omitted if it is "[out]" */
352         const char *tmp = "[out]";
353         if (parse_outputs(&tmp, &curr_inputs, &open_inputs,
354                          &open_outputs, log_ctx) < 0)
355             goto fail;
356     }
357
358     return 0;
359
360  fail:
361     avfilter_graph_destroy(graph);
362     free_inout(open_inputs);
363     free_inout(open_outputs);
364     free_inout(curr_inputs);
365     return -1;
366 }