Use full path for #includes from another directory.
[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 void avfilter_destroy_graph(AVFilterGraph *graph)
30 {
31     for(; graph->filter_count > 0; graph->filter_count --)
32         avfilter_destroy(graph->filters[graph->filter_count - 1]);
33     av_freep(&graph->filters);
34 }
35
36 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
37 {
38     graph->filters = av_realloc(graph->filters,
39                                 sizeof(AVFilterContext*) * ++graph->filter_count);
40
41     if (!graph->filters)
42         return -1;
43
44     graph->filters[graph->filter_count - 1] = filter;
45
46     return 0;
47 }
48
49 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
50 {
51     int i;
52
53     for(i = 0; i < graph->filter_count; i ++)
54         if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
55             return graph->filters[i];
56
57     return NULL;
58 }
59
60 static int query_formats(AVFilterGraph *graph)
61 {
62     int i, j;
63     int scaler_count = 0;
64     char inst_name[30];
65
66     /* ask all the sub-filters for their supported colorspaces */
67     for(i = 0; i < graph->filter_count; i ++) {
68         if(graph->filters[i]->filter->query_formats)
69             graph->filters[i]->filter->query_formats(graph->filters[i]);
70         else
71             avfilter_default_query_formats(graph->filters[i]);
72     }
73
74     /* go through and merge as many format lists as possible */
75     for(i = 0; i < graph->filter_count; i ++) {
76         AVFilterContext *filter = graph->filters[i];
77
78         for(j = 0; j < filter->input_count; j ++) {
79             AVFilterLink *link = filter->inputs[j];
80             if(link && link->in_formats != link->out_formats) {
81                 if(!avfilter_merge_formats(link->in_formats,
82                                            link->out_formats)) {
83                     AVFilterContext *scale;
84                     /* couldn't merge format lists. auto-insert scale filter */
85                     snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
86                              scaler_count);
87                     scale =
88                         avfilter_open(avfilter_get_by_name("scale"),inst_name);
89
90                     if(!scale || scale->filter->init(scale, NULL, NULL) ||
91                                  avfilter_insert_filter(link, scale, 0, 0)) {
92                         avfilter_destroy(scale);
93                         return -1;
94                     }
95
96                     if (avfilter_graph_add_filter(graph, scale) < 0)
97                         return -1;
98
99                     scale->filter->query_formats(scale);
100                     if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
101                                                scale-> inputs[0]->out_formats)||
102                        !avfilter_merge_formats(scale->outputs[0]->in_formats,
103                                                scale->outputs[0]->out_formats))
104                         return -1;
105                 }
106             }
107         }
108     }
109
110     return 0;
111 }
112
113 static void pick_format(AVFilterLink *link)
114 {
115     if(!link || !link->in_formats)
116         return;
117
118     link->in_formats->format_count = 1;
119     link->format = link->in_formats->formats[0];
120
121     avfilter_formats_unref(&link->in_formats);
122     avfilter_formats_unref(&link->out_formats);
123 }
124
125 static void pick_formats(AVFilterGraph *graph)
126 {
127     int i, j;
128
129     for(i = 0; i < graph->filter_count; i ++) {
130         AVFilterContext *filter = graph->filters[i];
131
132         for(j = 0; j < filter->input_count; j ++)
133             pick_format(filter->inputs[j]);
134         for(j = 0; j < filter->output_count; j ++)
135             pick_format(filter->outputs[j]);
136     }
137 }
138
139 int avfilter_graph_config_formats(AVFilterGraph *graph)
140 {
141     /* find supported formats from sub-filters, and merge along links */
142     if(query_formats(graph))
143         return -1;
144
145     /* Once everything is merged, it's possible that we'll still have
146      * multiple valid colorspace choices. We pick the first one. */
147     pick_formats(graph);
148
149     return 0;
150 }
151