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