]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavfilter/avfiltergraph.c
I should not have merged the graph parser with the graph
[frescor/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 /**
37  * @todo insert in sorted order
38  */
39 void avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
40 {
41     graph->filters = av_realloc(graph->filters,
42                                 sizeof(AVFilterContext*) * ++graph->filter_count);
43     graph->filters[graph->filter_count - 1] = filter;
44 }
45
46 /*
47  * @todo search intelligently, once we insert in order
48  */
49 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
50 {
51     int i;
52
53     if(!name)
54         return NULL;
55
56     for(i = 0; i < graph->filter_count; i ++)
57         if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
58             return graph->filters[i];
59
60     return NULL;
61 }
62
63 static int query_formats(AVFilterGraph *graph)
64 {
65     int i, j;
66
67     /* ask all the sub-filters for their supported colorspaces */
68     for(i = 0; i < graph->filter_count; i ++) {
69         if(graph->filters[i]->filter->query_formats)
70             graph->filters[i]->filter->query_formats(graph->filters[i]);
71         else
72             avfilter_default_query_formats(graph->filters[i]);
73     }
74
75     /* go through and merge as many format lists as possible */
76     for(i = 0; i < graph->filter_count; i ++) {
77         AVFilterContext *filter = graph->filters[i];
78
79         for(j = 0; j < filter->input_count; j ++) {
80             AVFilterLink *link;
81             if(!(link = filter->inputs[j]))
82                 continue;
83             if(link->in_formats != link->out_formats) {
84                 if(!avfilter_merge_formats(link->in_formats,
85                                            link->out_formats)) {
86                     /* couldn't merge format lists. auto-insert scale filter */
87                     AVFilterContext *scale;
88
89                     if(!(scale =
90                          avfilter_open(avfilter_get_by_name("scale"), NULL)))
91                         return -1;
92                     if(scale->filter->init(scale, NULL, NULL) ||
93                        avfilter_insert_filter(link, scale, 0, 0)) {
94                         avfilter_destroy(scale);
95                         return -1;
96                     }
97
98                     avfilter_graph_add_filter(graph, scale);
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