3 * copyright (c) 2008 Vitor Sessak
4 * copyright (c) 2007 Bobby Bingham
6 * This file is part of FFmpeg.
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.
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.
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
27 #include "avfiltergraph.h"
30 /** Linked-list of filters to create for an AVFilterGraphDesc */
31 typedef struct AVFilterGraphDescFilter
33 int index; ///< filter instance index
34 char *filter; ///< name of filter type
35 char *args; ///< filter parameters
36 struct AVFilterGraphDescFilter *next;
37 } AVFilterGraphDescFilter;
39 /** Linked-list of links between filters */
40 typedef struct AVFilterGraphDescLink
42 /* TODO: allow referencing pads by name, not just by index */
43 int src; ///< index of the source filter
44 unsigned srcpad; ///< index of the output pad on the source filter
46 int dst; ///< index of the dest filter
47 unsigned dstpad; ///< index of the input pad on the dest filter
49 struct AVFilterGraphDescLink *next;
50 } AVFilterGraphDescLink;
52 /** Linked-list of filter pads to be exported from the graph */
53 typedef struct AVFilterGraphDescExport
55 /* TODO: allow referencing pads by name, not just by index */
56 char *name; ///< name of the exported pad
57 int filter; ///< index of the filter
58 unsigned pad; ///< index of the pad to be exported
60 struct AVFilterGraphDescExport *next;
61 } AVFilterGraphDescExport;
63 /** Description of a graph to be loaded from a file, etc */
66 AVFilterGraphDescFilter *filters; ///< filters in the graph
67 AVFilterGraphDescLink *links; ///< links between the filters
68 AVFilterGraphDescExport *inputs; ///< inputs to export
69 AVFilterGraphDescExport *outputs; ///< outputs to export
75 static const char *log_name(void *p)
77 return "Filter parser";
80 static const AVClass filter_parser_class = {
85 static const AVClass *log_ctx = &filter_parser_class;
87 void avfilter_destroy_graph(AVFilterGraph *graph)
89 for(; graph->filter_count > 0; graph->filter_count --)
90 avfilter_destroy(graph->filters[graph->filter_count - 1]);
91 av_freep(&graph->filters);
94 /* TODO: insert in sorted order */
95 void avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
97 graph->filters = av_realloc(graph->filters,
98 sizeof(AVFilterContext*) * ++graph->filter_count);
99 graph->filters[graph->filter_count - 1] = filter;
102 /* search intelligently, once we insert in order */
103 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
110 for(i = 0; i < graph->filter_count; i ++)
111 if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
112 return graph->filters[i];
117 static int query_formats(AVFilterGraph *graph)
121 /* ask all the sub-filters for their supported colorspaces */
122 for(i = 0; i < graph->filter_count; i ++) {
123 if(graph->filters[i]->filter->query_formats)
124 graph->filters[i]->filter->query_formats(graph->filters[i]);
126 avfilter_default_query_formats(graph->filters[i]);
129 /* go through and merge as many format lists as possible */
130 for(i = 0; i < graph->filter_count; i ++) {
131 AVFilterContext *filter = graph->filters[i];
133 for(j = 0; j < filter->input_count; j ++) {
135 if(!(link = filter->inputs[j]))
137 if(link->in_formats != link->out_formats) {
138 if(!avfilter_merge_formats(link->in_formats,
139 link->out_formats)) {
140 /* couldn't merge format lists. auto-insert scale filter */
141 AVFilterContext *scale;
144 avfilter_open(avfilter_get_by_name("scale"), NULL)))
146 if(scale->filter->init(scale, NULL, NULL) ||
147 avfilter_insert_filter(link, scale, 0, 0)) {
148 avfilter_destroy(scale);
152 avfilter_graph_add_filter(graph, scale);
153 scale->filter->query_formats(scale);
154 if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
155 scale-> inputs[0]->out_formats) ||
156 !avfilter_merge_formats(scale->outputs[0]->in_formats,
157 scale->outputs[0]->out_formats))
167 static void pick_format(AVFilterLink *link)
169 if(!link || !link->in_formats)
172 link->in_formats->format_count = 1;
173 link->format = link->in_formats->formats[0];
175 avfilter_formats_unref(&link->in_formats);
176 avfilter_formats_unref(&link->out_formats);
179 static void pick_formats(AVFilterGraph *graph)
183 for(i = 0; i < graph->filter_count; i ++) {
184 AVFilterContext *filter = graph->filters[i];
186 for(j = 0; j < filter->input_count; j ++)
187 pick_format(filter->inputs[j]);
188 for(j = 0; j < filter->output_count; j ++)
189 pick_format(filter->outputs[j]);
193 int avfilter_graph_config_formats(AVFilterGraph *graph)
195 /* find supported formats from sub-filters, and merge along links */
196 if(query_formats(graph))
199 /* Once everything is merged, it's possible that we'll still have
200 * multiple valid colorspace choices. We pick the first one. */
206 static int create_filter(AVFilterGraph *ctx, int index, char *name,
209 AVFilterContext *filt;
214 snprintf(tmp, 20, "%d", index);
215 if(!(filterdef = avfilter_get_by_name(name)) ||
216 !(filt = avfilter_open(filterdef, tmp))) {
217 av_log(&log_ctx, AV_LOG_ERROR,
218 "error creating filter '%s'\n", name);
221 avfilter_graph_add_filter(ctx, filt);
222 if(avfilter_init_filter(filt, args, NULL)) {
223 av_log(&log_ctx, AV_LOG_ERROR,
224 "error initializing filter '%s'\n", name);
231 static int link_filter(AVFilterGraph *ctx, int src, int srcpad,
234 AVFilterContext *filt, *filtb;
238 snprintf(tmp, 20, "%d", src);
239 if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
240 av_log(&log_ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
243 snprintf(tmp, 20, "%d", dst);
244 if(!(filtb = avfilter_graph_get_filter(ctx, tmp))) {
245 av_log(&log_ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
248 if(avfilter_link(filt, srcpad, filtb, dstpad)) {
249 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
256 static int load_from_desc(AVFilterGraph *graph, AVFilterGraphDesc *desc, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
258 AVFilterGraphDescExport *curpad;
260 AVFilterContext *filt;
261 AVFilterGraphDescFilter *curfilt;
262 AVFilterGraphDescLink *curlink;
265 /* create all filters */
266 for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
267 if (create_filter(graph, curfilt->index, curfilt->filter,
272 /* create all links */
273 for(curlink = desc->links; curlink; curlink = curlink->next) {
274 if (link_filter(graph, curlink->src, curlink->srcpad,
275 curlink->dst, curlink->dstpad) < 0)
279 /* export all input pads */
280 for(curpad = desc->inputs; curpad; curpad = curpad->next) {
281 snprintf(tmp, 20, "%d", curpad->filter);
282 if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
283 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
286 if(avfilter_link(in, inpad, filt, curpad->pad)) {
287 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
292 /* export all output pads */
293 for(curpad = desc->outputs; curpad; curpad = curpad->next) {
294 snprintf(tmp, 20, "%d", curpad->filter);
295 if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
296 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
300 if(avfilter_link(filt, curpad->pad, out, outpad)) {
301 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
309 avfilter_destroy_graph(graph);
314 static void consume_whitespace(const char **buf)
316 *buf += strspn(*buf, " \n\t");
320 * get the next non-whitespace char
322 static char consume_char(const char **buf)
325 consume_whitespace(buf);
336 * remove the quotation marks from a string. Ex: "aaa'bb'cc" -> "aaabbcc"
338 static void unquote(char *str)
352 * Consumes a string from *buf.
353 * @return a copy of the consumed string, which should be free'd after use
355 static char *consume_string(const char **buf)
361 consume_whitespace(buf);
364 return av_mallocz(1);
368 *buf += strcspn(*buf, " ()=,'");
371 char *p = strchr(*buf + 1, '\'');
375 *buf += strlen(*buf); // Move the pointer to the null end byte
378 size = *buf - start + 1;
379 ret = av_malloc(size);
380 memcpy(ret, start, size - 1);
390 * @arg name a pointer (that need to be free'd after use) to the name between
393 static void parse_link_name(const char **buf, char **name)
397 *name = consume_string(buf);
402 if (consume_char(buf) != ')')
408 av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
412 * Parse "filter=params"
413 * @arg name a pointer (that need to be free'd after use) to the name of the
415 * @arg ars a pointer (that need to be free'd after use) to the args of the
418 static void parse_filter(const char **buf, char **name, char **opts)
420 *name = consume_string(buf);
424 *opts = consume_string(buf);
437 * A linked-list of the inputs/outputs of the filter chain.
439 typedef struct AVFilterInOut {
445 struct AVFilterInOut *next;
448 static void free_inout(AVFilterInOut *head)
459 * Parse "(a1)(link2) ... (etc)"
461 static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
462 enum LinkType type, int instance)
465 while (**buf == '(') {
466 AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
467 parse_link_name(buf, &inoutn->name);
469 inoutn->instance = instance;
470 inoutn->pad_idx = pad++;
471 inoutn->next = *inout;
478 * Free a graph description.
480 static void free_desc(AVFilterGraphDesc *desc)
484 while(desc->filters) {
485 next = desc->filters->next;
486 av_free(desc->filters->filter);
487 av_free(desc->filters->args);
488 av_free(desc->filters);
489 desc->filters = next;
493 next = desc->links->next;
494 av_free(desc->links);
498 while(desc->inputs) {
499 next = desc->inputs->next;
500 av_free(desc->inputs);
504 while(desc->outputs) {
505 next = desc->outputs->next;
506 av_free(desc->outputs);
507 desc->outputs = next;
511 static AVFilterGraphDesc *parse_chain(const char *filters, int has_in)
513 AVFilterGraphDesc *ret;
514 AVFilterGraphDescFilter **filterp, *filtern;
515 AVFilterGraphDescLink **linkp, *linkn;
516 AVFilterInOut *inout=NULL;
524 consume_whitespace(&filters);
526 if(!(ret = av_mallocz(sizeof(AVFilterGraphDesc))))
529 filterp = &ret->filters;
534 linkn = av_mallocz(sizeof(AVFilterGraphDescLink));
535 linkn->src = index-1;
541 linkp = &linkn->next;
543 pad = parse_inouts(&filters, &inout, chr == ',' || (!has_in),
546 filtern = av_mallocz(sizeof(AVFilterGraphDescFilter));
547 filtern->index = index;
548 parse_filter(&filters, &filtern->filter, &filtern->args);
550 filterp = &filtern->next;
552 pad = parse_inouts(&filters, &inout, 0,
554 chr = consume_char(&filters);
556 } while (chr == ',' || chr == ';');
559 for (; inout != NULL; inout = inout->next) {
560 if (inout->instance == -1)
561 continue; // Already processed
563 if (!strcmp(inout->name, "in")) {
566 ret->inputs = av_mallocz(sizeof(AVFilterGraphDescExport));
567 ret->inputs->filter = inout->instance;
568 ret->inputs->pad = inout->pad_idx;
569 } else if (!strcmp(inout->name, "out")) {
571 ret->outputs = av_mallocz(sizeof(AVFilterGraphDescExport));
572 ret->outputs->filter = inout->instance;
573 ret->outputs->pad = inout->pad_idx;
575 AVFilterInOut *p, *src, *dst;
576 for (p = inout->next;
577 p && strcmp(p->name,inout->name); p = p->next);
580 av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
585 if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
588 } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
592 av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
596 linkn = av_mallocz(sizeof(AVFilterGraphDescLink));
598 linkn->src = src->instance;
599 linkn->srcpad = src->pad_idx;
600 linkn->dst = dst->instance;
601 linkn->dstpad = dst->pad_idx;
604 linkp = &linkn->next;
614 ret->inputs = av_mallocz(sizeof(AVFilterGraphDescExport));
615 ret->inputs->filter = 0;
618 ret->outputs = av_mallocz(sizeof(AVFilterGraphDescExport));
619 ret->outputs->filter = index-1;
632 * Parse a string describing a filter graph.
634 int avfilter_graph_parse_chain(AVFilterGraph *graph, const char *filters, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
636 AVFilterGraphDesc *desc;
638 /* Try first to parse supposing there is no (in) element */
639 if (!(desc = parse_chain(filters, 0))) {
640 /* If it didn't work, parse supposing there is an (in) element */
641 desc = parse_chain(filters, 1);
646 if (load_from_desc(graph, desc, in, inpad, out, outpad) < 0) {