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 graph_load_from_desc2(AVFilterGraph *ctx, AVFilterGraphDesc *desc)
165 AVFilterGraphDescFilter *curfilt;
166 AVFilterGraphDescLink *curlink;
167 AVFilterContext *filt, *filtb;
172 /* create all filters */
173 for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
174 snprintf(tmp, 20, "%d", curfilt->index);
175 if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
176 !(filt = avfilter_open(filterdef, tmp))) {
177 av_log(&log_ctx, AV_LOG_ERROR,
178 "error creating filter '%s'\n", curfilt->filter);
181 avfilter_graph_add_filter(ctx, filt);
182 if(avfilter_init_filter(filt, curfilt->args, NULL)) {
183 av_log(&log_ctx, AV_LOG_ERROR,
184 "error initializing filter '%s'\n", curfilt->filter);
189 /* create all links */
190 for(curlink = desc->links; curlink; curlink = curlink->next) {
191 snprintf(tmp, 20, "%d", curlink->src);
192 if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
193 av_log(&log_ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
196 snprintf(tmp, 20, "%d", curlink->dst);
197 if(!(filtb = avfilter_graph_get_filter(ctx, tmp))) {
198 av_log(&log_ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
201 if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
202 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
214 int graph_load_from_desc3(AVFilterGraph *graph, AVFilterGraphDesc *desc, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
216 AVFilterGraphDescExport *curpad;
218 AVFilterContext *filt;
220 if (graph_load_from_desc2(graph, desc) < 0)
223 /* export all input pads */
224 for(curpad = desc->inputs; curpad; curpad = curpad->next) {
225 snprintf(tmp, 20, "%d", curpad->filter);
226 if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
227 av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
230 if(avfilter_link(in, inpad, filt, curpad->pad)) {
231 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
236 /* export all output pads */
237 for(curpad = desc->outputs; 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");
244 if(avfilter_link(filt, curpad->pad, out, outpad)) {
245 av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
258 static void consume_whitespace(const char **buf)
260 *buf += strspn(*buf, " \n\t");
264 * get the next non-whitespace char
266 static char consume_char(const char **buf)
269 consume_whitespace(buf);
280 * remove the quotation marks from a string. Ex: "aaa'bb'cc" -> "aaabbcc"
282 static void unquote(char *str)
296 * Consumes a string from *buf.
297 * @return a copy of the consumed string, which should be free'd after use
299 static char *consume_string(const char **buf)
305 consume_whitespace(buf);
308 return av_mallocz(1);
312 *buf += strcspn(*buf, " ()=,'");
315 char *p = strchr(*buf + 1, '\'');
319 *buf += strlen(*buf); // Move the pointer to the null end byte
322 size = *buf - start + 1;
323 ret = av_malloc(size);
324 memcpy(ret, start, size - 1);
334 * @arg name a pointer (that need to be free'd after use) to the name between
337 static void parse_link_name(const char **buf, char **name)
341 *name = consume_string(buf);
346 if (consume_char(buf) != ')')
352 av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
356 * Parse "filter=params"
357 * @arg name a pointer (that need to be free'd after use) to the name of the
359 * @arg ars a pointer (that need to be free'd after use) to the args of the
362 static void parse_filter(const char **buf, char **name, char **opts)
364 *name = consume_string(buf);
368 *opts = consume_string(buf);
381 * A linked-list of the inputs/outputs of the filter chain.
383 typedef struct AVFilterInOut {
389 struct AVFilterInOut *next;
392 static void free_inout(AVFilterInOut *head)
403 * Parse "(a1)(link2) ... (etc)"
405 static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
406 enum LinkType type, int instance)
409 while (**buf == '(') {
410 AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
411 parse_link_name(buf, &inoutn->name);
413 inoutn->instance = instance;
414 inoutn->pad_idx = pad++;
415 inoutn->next = *inout;
421 static AVFilterGraphDesc *parse_chain(const char *filters, int has_in)
423 AVFilterGraphDesc *ret;
424 AVFilterGraphDescFilter **filterp, *filtern;
425 AVFilterGraphDescLink **linkp, *linkn;
426 AVFilterInOut *inout=NULL;
434 consume_whitespace(&filters);
436 if(!(ret = av_mallocz(sizeof(AVFilterGraphDesc))))
439 filterp = &ret->filters;
444 linkn = av_mallocz(sizeof(AVFilterGraphDescLink));
445 linkn->src = index-1;
451 linkp = &linkn->next;
453 pad = parse_inouts(&filters, &inout, chr == ',' || (!has_in),
456 filtern = av_mallocz(sizeof(AVFilterGraphDescFilter));
457 filtern->index = index;
458 parse_filter(&filters, &filtern->filter, &filtern->args);
460 filterp = &filtern->next;
462 pad = parse_inouts(&filters, &inout, 0,
464 chr = consume_char(&filters);
466 } while (chr == ',' || chr == ';');
469 for (; inout != NULL; inout = inout->next) {
470 if (inout->instance == -1)
471 continue; // Already processed
473 if (!strcmp(inout->name, "in")) {
476 ret->inputs = av_mallocz(sizeof(AVFilterGraphDescExport));
477 ret->inputs->filter = inout->instance;
478 ret->inputs->pad = inout->pad_idx;
479 } else if (!strcmp(inout->name, "out")) {
481 ret->outputs = av_mallocz(sizeof(AVFilterGraphDescExport));
482 ret->outputs->filter = inout->instance;
483 ret->outputs->pad = inout->pad_idx;
485 AVFilterInOut *p, *src, *dst;
486 for (p = inout->next;
487 p && strcmp(p->name,inout->name); p = p->next);
490 av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
495 if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
498 } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
502 av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
506 linkn = av_mallocz(sizeof(AVFilterGraphDescLink));
508 linkn->src = src->instance;
509 linkn->srcpad = src->pad_idx;
510 linkn->dst = dst->instance;
511 linkn->dstpad = dst->pad_idx;
514 linkp = &linkn->next;
524 ret->inputs = av_mallocz(sizeof(AVFilterGraphDescExport));
525 ret->inputs->filter = 0;
528 ret->outputs = av_mallocz(sizeof(AVFilterGraphDescExport));
529 ret->outputs->filter = index-1;
537 avfilter_graph_free_desc(ret);
542 * Parse a string describing a filter graph.
544 AVFilterGraphDesc *avfilter_graph_parse_chain(const char *filters)
546 AVFilterGraphDesc *ret;
548 /* Try first to parse supposing there is no (in) element */
549 if ((ret = parse_chain(filters, 0)))
552 /* Parse supposing there is an (in) element */
553 return parse_chain(filters, 1);
557 * Free a graph description.
559 void avfilter_graph_free_desc(AVFilterGraphDesc *desc)
563 while(desc->filters) {
564 next = desc->filters->next;
565 av_free(desc->filters->filter);
566 av_free(desc->filters->args);
567 av_free(desc->filters);
568 desc->filters = next;
572 next = desc->links->next;
573 av_free(desc->links);
577 while(desc->inputs) {
578 next = desc->inputs->next;
579 av_free(desc->inputs);
583 while(desc->outputs) {
584 next = desc->outputs->next;
585 av_free(desc->outputs);
586 desc->outputs = next;