]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavfilter/avfiltergraph.c
Add backslash '\' support to the parser
[frescor/ffmpeg.git] / libavfilter / avfiltergraph.c
1 /*
2  * filter graphs
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 "avfilter.h"
27 #include "avfiltergraph.h"
28
29 /**
30  * For use in av_log
31  */
32 static const char *log_name(void *p)
33 {
34     return "Filter parser";
35 }
36
37 static const AVClass filter_parser_class = {
38     "Filter parser",
39     log_name
40 };
41
42 static const AVClass *log_ctx = &filter_parser_class;
43
44 void avfilter_destroy_graph(AVFilterGraph *graph)
45 {
46     for(; graph->filter_count > 0; graph->filter_count --)
47         avfilter_destroy(graph->filters[graph->filter_count - 1]);
48     av_freep(&graph->filters);
49 }
50
51 /* TODO: insert in sorted order */
52 void avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
53 {
54     graph->filters = av_realloc(graph->filters,
55                                 sizeof(AVFilterContext*) * ++graph->filter_count);
56     graph->filters[graph->filter_count - 1] = filter;
57 }
58
59 /* search intelligently, once we insert in order */
60 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
61 {
62     int i;
63
64     if(!name)
65         return NULL;
66
67     for(i = 0; i < graph->filter_count; i ++)
68         if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
69             return graph->filters[i];
70
71     return NULL;
72 }
73
74 static int query_formats(AVFilterGraph *graph)
75 {
76     int i, j;
77
78     /* ask all the sub-filters for their supported colorspaces */
79     for(i = 0; i < graph->filter_count; i ++) {
80         if(graph->filters[i]->filter->query_formats)
81             graph->filters[i]->filter->query_formats(graph->filters[i]);
82         else
83             avfilter_default_query_formats(graph->filters[i]);
84     }
85
86     /* go through and merge as many format lists as possible */
87     for(i = 0; i < graph->filter_count; i ++) {
88         AVFilterContext *filter = graph->filters[i];
89
90         for(j = 0; j < filter->input_count; j ++) {
91             AVFilterLink *link;
92             if(!(link = filter->inputs[j]))
93                 continue;
94             if(link->in_formats != link->out_formats) {
95                 if(!avfilter_merge_formats(link->in_formats,
96                                            link->out_formats)) {
97                     /* couldn't merge format lists. auto-insert scale filter */
98                     AVFilterContext *scale;
99
100                     if(!(scale =
101                          avfilter_open(avfilter_get_by_name("scale"), NULL)))
102                         return -1;
103                     if(scale->filter->init(scale, NULL, NULL) ||
104                        avfilter_insert_filter(link, scale, 0, 0)) {
105                         avfilter_destroy(scale);
106                         return -1;
107                     }
108
109                     avfilter_graph_add_filter(graph, scale);
110                     scale->filter->query_formats(scale);
111                     if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
112                                               scale-> inputs[0]->out_formats) ||
113                        !avfilter_merge_formats(scale->outputs[0]->in_formats,
114                                               scale->outputs[0]->out_formats))
115                         return -1;
116                 }
117             }
118         }
119     }
120
121     return 0;
122 }
123
124 static void pick_format(AVFilterLink *link)
125 {
126     if(!link || !link->in_formats)
127         return;
128
129     link->in_formats->format_count = 1;
130     link->format = link->in_formats->formats[0];
131
132     avfilter_formats_unref(&link->in_formats);
133     avfilter_formats_unref(&link->out_formats);
134 }
135
136 static void pick_formats(AVFilterGraph *graph)
137 {
138     int i, j;
139
140     for(i = 0; i < graph->filter_count; i ++) {
141         AVFilterContext *filter = graph->filters[i];
142
143         for(j = 0; j < filter->input_count; j ++)
144             pick_format(filter->inputs[j]);
145         for(j = 0; j < filter->output_count; j ++)
146             pick_format(filter->outputs[j]);
147     }
148 }
149
150 int avfilter_graph_config_formats(AVFilterGraph *graph)
151 {
152     /* find supported formats from sub-filters, and merge along links */
153     if(query_formats(graph))
154         return -1;
155
156     /* Once everything is merged, it's possible that we'll still have
157      * multiple valid colorspace choices. We pick the first one. */
158     pick_formats(graph);
159
160     return 0;
161 }
162
163 static int create_filter(AVFilterGraph *ctx, int index, char *name,
164                          char *args)
165 {
166     AVFilterContext *filt;
167
168     AVFilter *filterdef;
169     char tmp[20];
170
171     snprintf(tmp, 20, "%d", index);
172     if(!(filterdef = avfilter_get_by_name(name)) ||
173        !(filt = avfilter_open(filterdef, tmp))) {
174         av_log(&log_ctx, AV_LOG_ERROR,
175                "error creating filter '%s'\n", name);
176         return -1;
177     }
178     avfilter_graph_add_filter(ctx, filt);
179     if(avfilter_init_filter(filt, args, NULL)) {
180         av_log(&log_ctx, AV_LOG_ERROR,
181                "error initializing filter '%s'\n", name);
182         return -1;
183     }
184
185     return 0;
186 }
187
188 static int link_filter(AVFilterGraph *ctx, int src, int srcpad,
189                        int dst, int dstpad)
190 {
191     AVFilterContext *filt, *filtb;
192
193     char tmp[20];
194
195     snprintf(tmp, 20, "%d", src);
196     if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
197         av_log(&log_ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
198         return -1;
199     }
200     snprintf(tmp, 20, "%d", dst);
201     if(!(filtb = avfilter_graph_get_filter(ctx, tmp))) {
202         av_log(&log_ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
203         return -1;
204     }
205     if(avfilter_link(filt, srcpad, filtb, dstpad)) {
206         av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
207         return -1;
208     }
209
210     return 0;
211 }
212
213 static void consume_whitespace(const char **buf)
214 {
215     *buf += strspn(*buf, " \n\t");
216 }
217
218 /**
219  * get the next non-whitespace char
220  */
221 static char consume_char(const char **buf)
222 {
223     char out;
224     consume_whitespace(buf);
225
226     out = **buf;
227
228     if (out)
229         (*buf)++;
230
231     return out;
232 }
233
234 /**
235  * Copy the first size bytes of input string to a null-terminated string,
236  * removing any control character. Ex: "aaa'bb'c\'c\\" -> "aaabbc'c\"
237  */
238 static void copy_unquoted(char *out, const char *in, int size)
239 {
240     int i;
241     for (i=0; i < size; i++) {
242         if (in[i] == '\'')
243             continue;
244         else if (in[i] == '\\') {
245             if (i+1 == size) {
246                 *out = 0;
247                 return;
248             }
249             i++;
250         }
251         *out++ = in[i];
252     }
253     *out=0;
254 }
255
256 /**
257  * Consumes a string from *buf.
258  * @return a copy of the consumed string, which should be free'd after use
259  */
260 static char *consume_string(const char **buf)
261 {
262     const char *start;
263     char *ret;
264     int size;
265
266     consume_whitespace(buf);
267
268     if (!(**buf))
269         return av_mallocz(1);
270
271     start = *buf;
272
273     while(1) {
274         *buf += strcspn(*buf, " ()=,'\\");
275         if (**buf == '\\')
276             *buf+=2;
277         else
278             break;
279     }
280
281     if (**buf == '\'') {
282         const char *p = *buf;
283         do {
284             p++;
285             p = strchr(p, '\'');
286         } while (p && p[-1] == '\\');
287         if (p)
288             *buf = p + 1;
289         else
290             *buf += strlen(*buf); // Move the pointer to the null end byte
291     }
292
293     size = *buf - start + 1;
294     ret = av_malloc(size);
295     copy_unquoted(ret, start, size-1);
296
297     return ret;
298 }
299
300 /**
301  * Parse "(linkname)"
302  * @arg name a pointer (that need to be free'd after use) to the name between
303  *           parenthesis
304  */
305 static void parse_link_name(const char **buf, char **name)
306 {
307     consume_char(buf);
308
309     *name = consume_string(buf);
310
311     if (!*name[0])
312         goto fail;
313
314     if (consume_char(buf) != ')')
315         goto fail;
316
317     return;
318  fail:
319     av_freep(name);
320     av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
321 }
322
323 /**
324  * Parse "filter=params"
325  * @arg name a pointer (that need to be free'd after use) to the name of the
326  *           filter
327  * @arg ars  a pointer (that need to be free'd after use) to the args of the
328  *           filter
329  */
330 static int parse_filter(const char **buf, AVFilterGraph *graph, int index)
331 {
332     char *name, *opts;
333     name = consume_string(buf);
334
335     if (**buf == '=') {
336         consume_char(buf);
337         opts = consume_string(buf);
338     } else {
339         opts = NULL;
340     }
341
342     return create_filter(graph, index, name, opts);
343 }
344
345 enum LinkType {
346     LinkTypeIn,
347     LinkTypeOut,
348 };
349
350 /**
351  * A linked-list of the inputs/outputs of the filter chain.
352  */
353 typedef struct AVFilterInOut {
354     enum LinkType type;
355     char *name;
356     int instance;
357     int pad_idx;
358
359     struct AVFilterInOut *next;
360 } AVFilterInOut;
361
362 static void free_inout(AVFilterInOut *head)
363 {
364     while (head) {
365         AVFilterInOut *next;
366         next = head->next;
367         av_free(head);
368         head = next;
369     }
370 }
371
372 /**
373  * Parse "(a1)(link2) ... (etc)"
374  */
375 static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
376                         enum LinkType type, int instance)
377 {
378     int pad = firstpad;
379     while (**buf == '(') {
380         AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
381         parse_link_name(buf, &inoutn->name);
382         inoutn->type = type;
383         inoutn->instance = instance;
384         inoutn->pad_idx = pad++;
385         inoutn->next = *inout;
386         *inout = inoutn;
387     }
388     return pad;
389 }
390
391 /**
392  * Parse a string describing a filter graph.
393  */
394 int avfilter_graph_parse_chain(AVFilterGraph *graph, const char *filters, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
395 {
396     AVFilterInOut           *inout=NULL;
397     AVFilterInOut           *head=NULL;
398
399     int index = 0;
400     char chr = 0;
401     int pad = 0;
402     int has_out = 0;
403
404     char tmp[20];
405     AVFilterContext *filt;
406
407     consume_whitespace(&filters);
408
409     do {
410         int oldpad = pad;
411
412         pad = parse_inouts(&filters, &inout, chr == ',', LinkTypeIn, index);
413
414         if (parse_filter(&filters, graph, index) < 0)
415             goto fail;
416
417         // If the first filter has an input and none was given, it is
418         // implicitly the input of the whole graph.
419         if (pad == 0 && graph->filters[graph->filter_count-1]->input_count == 1) {
420             snprintf(tmp, 20, "%d", index);
421             if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
422                 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
423                 goto fail;
424             }
425             if(avfilter_link(in, inpad, filt, 0)) {
426                 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
427                 goto fail;
428             }
429         }
430
431         if(chr == ',') {
432             if (link_filter(graph, index-1, oldpad, index, 0) < 0)
433                 goto fail;
434
435         }
436         pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, index);
437         chr = consume_char(&filters);
438         index++;
439     } while (chr == ',' || chr == ';');
440
441     head = inout;
442     for (; inout != NULL; inout = inout->next) {
443         if (inout->instance == -1)
444             continue; // Already processed
445
446         if (!strcmp(inout->name, "in")) {
447             snprintf(tmp, 20, "%d", inout->instance);
448             if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
449                 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
450                 goto fail;
451             }
452             if(avfilter_link(in, inpad, filt, inout->pad_idx)) {
453                 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
454                 goto fail;
455             }
456         } else if (!strcmp(inout->name, "out")) {
457             has_out = 1;
458             snprintf(tmp, 20, "%d", inout->instance);
459             if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
460                 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
461                 goto fail;
462             }
463
464             if(avfilter_link(filt, inout->pad_idx, out, outpad)) {
465                 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
466                 goto fail;
467         }
468
469         } else {
470             AVFilterInOut *p, *src, *dst;
471             for (p = inout->next;
472                  p && strcmp(p->name,inout->name); p = p->next);
473
474             if (!p) {
475                 av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
476                        inout->name);
477                 goto fail;
478             }
479
480             if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
481                 src = inout;
482                 dst = p;
483             } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
484                 src = p;
485                 dst = inout;
486             } else {
487                 av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
488                        inout->name);
489                 goto fail;
490             }
491
492             if (link_filter(graph, src->instance, src->pad_idx, dst->instance, dst->pad_idx) < 0)
493                 goto fail;
494
495             src->instance = -1;
496             dst->instance = -1;
497         }
498     }
499
500     free_inout(head);
501
502     if (!has_out) {
503         snprintf(tmp, 20, "%d", index-1);
504         if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
505             av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
506             goto fail;
507         }
508
509         if(avfilter_link(filt, pad, out, outpad)) {
510             av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
511             goto fail;
512         }
513
514     }
515
516     return 0;
517
518  fail:
519     free_inout(head);
520     avfilter_destroy_graph(graph);
521     return -1;
522 }