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