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"
32 static const char *log_name(void *p)
34 return "Filter parser";
37 static const AVClass filter_parser_class = {
42 static const AVClass *log_ctx = &filter_parser_class;
44 static void uninit(AVFilterGraph *graph)
46 for(; graph->filter_count > 0; graph->filter_count --)
47 avfilter_destroy(graph->filters[graph->filter_count - 1]);
48 av_freep(&graph->filters);
51 /* TODO: insert in sorted order */
52 void avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
54 graph->filters = av_realloc(graph->filters,
55 sizeof(AVFilterContext*) * ++graph->filter_count);
56 graph->filters[graph->filter_count - 1] = filter;
59 /* search intelligently, once we insert in order */
60 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
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];
74 static int query_formats(AVFilterGraph *graph)
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]);
83 avfilter_default_query_formats(graph->filters[i]);
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];
90 for(j = 0; j < filter->input_count; j ++) {
92 if(!(link = filter->inputs[j]))
94 if(link->in_formats != link->out_formats) {
95 if(!avfilter_merge_formats(link->in_formats,
97 /* couldn't merge format lists. auto-insert scale filter */
98 AVFilterContext *scale;
101 avfilter_open(avfilter_get_by_name("scale"), NULL)))
103 if(scale->filter->init(scale, NULL, NULL) ||
104 avfilter_insert_filter(link, scale, 0, 0)) {
105 avfilter_destroy(scale);
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))
124 static void pick_format(AVFilterLink *link)
126 if(!link || !link->in_formats)
129 link->in_formats->format_count = 1;
130 link->format = link->in_formats->formats[0];
132 avfilter_formats_unref(&link->in_formats);
133 avfilter_formats_unref(&link->out_formats);
136 static void pick_formats(AVFilterGraph *graph)
140 for(i = 0; i < graph->filter_count; i ++) {
141 AVFilterContext *filter = graph->filters[i];
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]);
150 int avfilter_graph_config_formats(AVFilterGraph *graph)
152 /* find supported formats from sub-filters, and merge along links */
153 if(query_formats(graph))
156 /* Once everything is merged, it's possible that we'll still have
157 * multiple valid colorspace choices. We pick the first one. */
163 static int create_filter(AVFilterGraph *ctx, int index, char *name,
166 AVFilterContext *filt;
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);
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);
188 static int link_filter(AVFilterGraph *ctx, int src, int srcpad,
191 AVFilterContext *filt, *filtb;
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");
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");
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");
213 int graph_load_from_desc3(AVFilterGraph *graph, AVFilterGraphDesc *desc, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
215 AVFilterGraphDescExport *curpad;
217 AVFilterContext *filt;
218 AVFilterGraphDescFilter *curfilt;
219 AVFilterGraphDescLink *curlink;
222 /* create all filters */
223 for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
224 if (create_filter(graph, curfilt->index, curfilt->filter,
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)
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");
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");
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");
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");
271 static void consume_whitespace(const char **buf)
273 *buf += strspn(*buf, " \n\t");
277 * get the next non-whitespace char
279 static char consume_char(const char **buf)
282 consume_whitespace(buf);
293 * remove the quotation marks from a string. Ex: "aaa'bb'cc" -> "aaabbcc"
295 static void unquote(char *str)
309 * Consumes a string from *buf.
310 * @return a copy of the consumed string, which should be free'd after use
312 static char *consume_string(const char **buf)
318 consume_whitespace(buf);
321 return av_mallocz(1);
325 *buf += strcspn(*buf, " ()=,'");
328 char *p = strchr(*buf + 1, '\'');
332 *buf += strlen(*buf); // Move the pointer to the null end byte
335 size = *buf - start + 1;
336 ret = av_malloc(size);
337 memcpy(ret, start, size - 1);
347 * @arg name a pointer (that need to be free'd after use) to the name between
350 static void parse_link_name(const char **buf, char **name)
354 *name = consume_string(buf);
359 if (consume_char(buf) != ')')
365 av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
369 * Parse "filter=params"
370 * @arg name a pointer (that need to be free'd after use) to the name of the
372 * @arg ars a pointer (that need to be free'd after use) to the args of the
375 static void parse_filter(const char **buf, char **name, char **opts)
377 *name = consume_string(buf);
381 *opts = consume_string(buf);
394 * A linked-list of the inputs/outputs of the filter chain.
396 typedef struct AVFilterInOut {
402 struct AVFilterInOut *next;
405 static void free_inout(AVFilterInOut *head)
416 * Parse "(a1)(link2) ... (etc)"
418 static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
419 enum LinkType type, int instance)
422 while (**buf == '(') {
423 AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
424 parse_link_name(buf, &inoutn->name);
426 inoutn->instance = instance;
427 inoutn->pad_idx = pad++;
428 inoutn->next = *inout;
434 static AVFilterGraphDesc *parse_chain(const char *filters, int has_in)
436 AVFilterGraphDesc *ret;
437 AVFilterGraphDescFilter **filterp, *filtern;
438 AVFilterGraphDescLink **linkp, *linkn;
439 AVFilterInOut *inout=NULL;
447 consume_whitespace(&filters);
449 if(!(ret = av_mallocz(sizeof(AVFilterGraphDesc))))
452 filterp = &ret->filters;
457 linkn = av_mallocz(sizeof(AVFilterGraphDescLink));
458 linkn->src = index-1;
464 linkp = &linkn->next;
466 pad = parse_inouts(&filters, &inout, chr == ',' || (!has_in),
469 filtern = av_mallocz(sizeof(AVFilterGraphDescFilter));
470 filtern->index = index;
471 parse_filter(&filters, &filtern->filter, &filtern->args);
473 filterp = &filtern->next;
475 pad = parse_inouts(&filters, &inout, 0,
477 chr = consume_char(&filters);
479 } while (chr == ',' || chr == ';');
482 for (; inout != NULL; inout = inout->next) {
483 if (inout->instance == -1)
484 continue; // Already processed
486 if (!strcmp(inout->name, "in")) {
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")) {
494 ret->outputs = av_mallocz(sizeof(AVFilterGraphDescExport));
495 ret->outputs->filter = inout->instance;
496 ret->outputs->pad = inout->pad_idx;
498 AVFilterInOut *p, *src, *dst;
499 for (p = inout->next;
500 p && strcmp(p->name,inout->name); p = p->next);
503 av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
508 if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
511 } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
515 av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
519 linkn = av_mallocz(sizeof(AVFilterGraphDescLink));
521 linkn->src = src->instance;
522 linkn->srcpad = src->pad_idx;
523 linkn->dst = dst->instance;
524 linkn->dstpad = dst->pad_idx;
527 linkp = &linkn->next;
537 ret->inputs = av_mallocz(sizeof(AVFilterGraphDescExport));
538 ret->inputs->filter = 0;
541 ret->outputs = av_mallocz(sizeof(AVFilterGraphDescExport));
542 ret->outputs->filter = index-1;
550 avfilter_graph_free_desc(ret);
555 * Parse a string describing a filter graph.
557 int avfilter_graph_parse_chain(AVFilterGraph *graph, const char *filters, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
559 AVFilterGraphDesc *desc;
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);
569 if (graph_load_from_desc3(graph, desc, in, inpad, out, outpad) < 0) {
570 avfilter_graph_free_desc(desc);
574 avfilter_graph_free_desc(desc);
579 * Free a graph description.
581 void avfilter_graph_free_desc(AVFilterGraphDesc *desc)
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;
594 next = desc->links->next;
595 av_free(desc->links);
599 while(desc->inputs) {
600 next = desc->inputs->next;
601 av_free(desc->inputs);
605 while(desc->outputs) {
606 next = desc->outputs->next;
607 av_free(desc->outputs);
608 desc->outputs = next;