]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfiltergraph.c
Remove usage of AVFilterGraphDesc outside avfiltergraph.c
[ffmpeg] / 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 static void uninit(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 int graph_load_from_desc3(AVFilterGraph *graph, AVFilterGraphDesc *desc, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
214 {
215     AVFilterGraphDescExport *curpad;
216     char tmp[20];
217     AVFilterContext *filt;
218     AVFilterGraphDescFilter *curfilt;
219     AVFilterGraphDescLink   *curlink;
220
221
222     /* create all filters */
223     for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
224         if (create_filter(graph, curfilt->index, curfilt->filter,
225                           curfilt->args) < 0)
226             goto fail;
227     }
228
229     /* create all links */
230     for(curlink = desc->links; curlink; curlink = curlink->next) {
231         if (link_filter(graph, curlink->src, curlink->srcpad,
232                           curlink->dst, curlink->dstpad) < 0)
233             goto fail;
234     }
235
236     /* export all input pads */
237     for(curpad = desc->inputs; curpad; curpad = curpad->next) {
238         snprintf(tmp, 20, "%d", curpad->filter);
239         if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
240             av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
241             goto fail;
242         }
243         if(avfilter_link(in, inpad, filt, curpad->pad)) {
244             av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
245             goto fail;
246         }
247     }
248
249     /* export all output pads */
250     for(curpad = desc->outputs; curpad; curpad = curpad->next) {
251         snprintf(tmp, 20, "%d", curpad->filter);
252         if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
253             av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
254             goto fail;
255         }
256
257         if(avfilter_link(filt, curpad->pad, out, outpad)) {
258             av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
259             goto fail;
260         }
261     }
262
263     return 0;
264
265 fail:
266     uninit(graph);
267     return -1;
268 }
269
270
271 static void consume_whitespace(const char **buf)
272 {
273     *buf += strspn(*buf, " \n\t");
274 }
275
276 /**
277  * get the next non-whitespace char
278  */
279 static char consume_char(const char **buf)
280 {
281     char out;
282     consume_whitespace(buf);
283
284     out = **buf;
285
286     if (out)
287         (*buf)++;
288
289     return out;
290 }
291
292 /**
293  * remove the quotation marks from a string. Ex: "aaa'bb'cc" -> "aaabbcc"
294  */
295 static void unquote(char *str)
296 {
297     char *p1, *p2;
298     p1=p2=str;
299     while (*p1 != 0) {
300         if (*p1 != '\'')
301             *p2++ = *p1;
302         p1++;
303     }
304
305     *p2 = 0;
306 }
307
308 /**
309  * Consumes a string from *buf.
310  * @return a copy of the consumed string, which should be free'd after use
311  */
312 static char *consume_string(const char **buf)
313 {
314     const char *start;
315     char *ret;
316     int size;
317
318     consume_whitespace(buf);
319
320     if (!(**buf))
321         return av_mallocz(1);
322
323     start = *buf;
324
325     *buf += strcspn(*buf, " ()=,'");
326
327     if (**buf == '\'') {
328         char *p = strchr(*buf + 1, '\'');
329         if (p)
330             *buf = p + 1;
331         else
332             *buf += strlen(*buf); // Move the pointer to the null end byte
333     }
334
335     size = *buf - start + 1;
336     ret = av_malloc(size);
337     memcpy(ret, start, size - 1);
338     ret[size-1] = 0;
339
340     unquote(ret);
341
342     return ret;
343 }
344
345 /**
346  * Parse "(linkname)"
347  * @arg name a pointer (that need to be free'd after use) to the name between
348  *           parenthesis
349  */
350 static void parse_link_name(const char **buf, char **name)
351 {
352     consume_char(buf);
353
354     *name = consume_string(buf);
355
356     if (!*name[0])
357         goto fail;
358
359     if (consume_char(buf) != ')')
360         goto fail;
361
362     return;
363  fail:
364     av_freep(name);
365     av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
366 }
367
368 /**
369  * Parse "filter=params"
370  * @arg name a pointer (that need to be free'd after use) to the name of the
371  *           filter
372  * @arg ars  a pointer (that need to be free'd after use) to the args of the
373  *           filter
374  */
375 static void parse_filter(const char **buf, char **name, char **opts)
376 {
377     *name = consume_string(buf);
378
379     if (**buf == '=') {
380         consume_char(buf);
381         *opts = consume_string(buf);
382     } else {
383         *opts = NULL;
384     }
385
386 }
387
388 enum LinkType {
389     LinkTypeIn,
390     LinkTypeOut,
391 };
392
393 /**
394  * A linked-list of the inputs/outputs of the filter chain.
395  */
396 typedef struct AVFilterInOut {
397     enum LinkType type;
398     char *name;
399     int instance;
400     int pad_idx;
401
402     struct AVFilterInOut *next;
403 } AVFilterInOut;
404
405 static void free_inout(AVFilterInOut *head)
406 {
407     while (head) {
408         AVFilterInOut *next;
409         next = head->next;
410         av_free(head);
411         head = next;
412     }
413 }
414
415 /**
416  * Parse "(a1)(link2) ... (etc)"
417  */
418 static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
419                         enum LinkType type, int instance)
420 {
421     int pad = firstpad;
422     while (**buf == '(') {
423         AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
424         parse_link_name(buf, &inoutn->name);
425         inoutn->type = type;
426         inoutn->instance = instance;
427         inoutn->pad_idx = pad++;
428         inoutn->next = *inout;
429         *inout = inoutn;
430     }
431     return pad;
432 }
433
434 static AVFilterGraphDesc *parse_chain(const char *filters, int has_in)
435 {
436     AVFilterGraphDesc        *ret;
437     AVFilterGraphDescFilter **filterp, *filtern;
438     AVFilterGraphDescLink   **linkp,   *linkn;
439     AVFilterInOut           *inout=NULL;
440     AVFilterInOut           *head;
441
442     int index = 0;
443     char chr = 0;
444     int pad = 0;
445     int has_out = 0;
446
447     consume_whitespace(&filters);
448
449     if(!(ret = av_mallocz(sizeof(AVFilterGraphDesc))))
450         return NULL;
451
452     filterp = &ret->filters;
453     linkp   = &ret->links;
454
455     do {
456         if(chr == ',') {
457             linkn = av_mallocz(sizeof(AVFilterGraphDescLink));
458             linkn->src = index-1;
459             linkn->srcpad = pad;
460             linkn->dst = index;
461             linkn->dstpad = 0;
462
463             *linkp = linkn;
464             linkp = &linkn->next;
465         }
466         pad = parse_inouts(&filters, &inout, chr == ',' || (!has_in),
467                            LinkTypeIn, index);
468
469         filtern = av_mallocz(sizeof(AVFilterGraphDescFilter));
470         filtern->index = index;
471         parse_filter(&filters, &filtern->filter, &filtern->args);
472         *filterp = filtern;
473         filterp = &filtern->next;
474
475         pad = parse_inouts(&filters, &inout, 0,
476                            LinkTypeOut, index);
477         chr = consume_char(&filters);
478         index++;
479     } while (chr == ',' || chr == ';');
480
481     head = inout;
482     for (; inout != NULL; inout = inout->next) {
483         if (inout->instance == -1)
484             continue; // Already processed
485
486         if (!strcmp(inout->name, "in")) {
487             if (!has_in)
488                 goto fail;
489             ret->inputs = av_mallocz(sizeof(AVFilterGraphDescExport));
490             ret->inputs->filter = inout->instance;
491             ret->inputs->pad = inout->pad_idx;
492         } else if (!strcmp(inout->name, "out")) {
493             has_out = 1;
494             ret->outputs = av_mallocz(sizeof(AVFilterGraphDescExport));
495             ret->outputs->filter = inout->instance;
496             ret->outputs->pad = inout->pad_idx;
497         } else {
498             AVFilterInOut *p, *src, *dst;
499             for (p = inout->next;
500                  p && strcmp(p->name,inout->name); p = p->next);
501
502             if (!p) {
503                 av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
504                        inout->name);
505                 goto fail;
506             }
507
508             if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
509                 src = inout;
510                 dst = p;
511             } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
512                 src = p;
513                 dst = inout;
514             } else {
515                 av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
516                        inout->name);
517                 goto fail;
518             }
519             linkn = av_mallocz(sizeof(AVFilterGraphDescLink));
520
521             linkn->src = src->instance;
522             linkn->srcpad = src->pad_idx;
523             linkn->dst = dst->instance;
524             linkn->dstpad = dst->pad_idx;
525
526             *linkp = linkn;
527             linkp = &linkn->next;
528
529             src->instance = -1;
530             dst->instance = -1;
531         }
532     }
533
534     free_inout(head);
535
536     if (!has_in) {
537         ret->inputs = av_mallocz(sizeof(AVFilterGraphDescExport));
538         ret->inputs->filter = 0;
539     }
540     if (!has_out) {
541         ret->outputs = av_mallocz(sizeof(AVFilterGraphDescExport));
542         ret->outputs->filter = index-1;
543     }
544
545     return ret;
546
547  fail:
548     free_inout(head);
549
550     avfilter_graph_free_desc(ret);
551     return NULL;
552 }
553
554 /**
555  * Parse a string describing a filter graph.
556  */
557 int avfilter_graph_parse_chain(AVFilterGraph *graph, const char *filters, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
558 {
559     AVFilterGraphDesc *desc;
560
561     /* Try first to parse supposing there is no (in) element */
562     if (!(desc = parse_chain(filters, 0))) {
563         /* If it didn't work, parse supposing there is an (in) element */
564         desc = parse_chain(filters, 1);
565     }
566     if (!desc)
567         return -1;
568
569     if (graph_load_from_desc3(graph, desc, in, inpad, out, outpad) < 0) {
570         avfilter_graph_free_desc(desc);
571         return -1;
572     }
573
574     avfilter_graph_free_desc(desc);
575     return 0;
576 }
577
578 /**
579  * Free a graph description.
580  */
581 void avfilter_graph_free_desc(AVFilterGraphDesc *desc)
582 {
583     void *next;
584
585     while(desc->filters) {
586         next = desc->filters->next;
587         av_free(desc->filters->filter);
588         av_free(desc->filters->args);
589         av_free(desc->filters);
590         desc->filters = next;
591     }
592
593     while(desc->links) {
594         next = desc->links->next;
595         av_free(desc->links);
596         desc->links = next;
597     }
598
599     while(desc->inputs) {
600         next = desc->inputs->next;
601         av_free(desc->inputs);
602         desc->inputs = next;
603     }
604
605     while(desc->outputs) {
606         next = desc->outputs->next;
607         av_free(desc->outputs);
608         desc->outputs = next;
609     }
610 }
611