]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfiltergraph.c
In libavfilter, use consistently "Copyright (c)" in the license
[ffmpeg] / 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 #include "internal.h"
29
30 AVFilterGraph *avfilter_graph_alloc(void)
31 {
32     return av_mallocz(sizeof(AVFilterGraph));
33 }
34
35 void avfilter_graph_free(AVFilterGraph *graph)
36 {
37     for(; graph->filter_count > 0; graph->filter_count --)
38         avfilter_free(graph->filters[graph->filter_count - 1]);
39     av_freep(&graph->scale_sws_opts);
40     av_freep(&graph->filters);
41 }
42
43 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
44 {
45     AVFilterContext **filters = av_realloc(graph->filters,
46                                            sizeof(AVFilterContext*) * (graph->filter_count+1));
47     if (!filters)
48         return AVERROR(ENOMEM);
49
50     graph->filters = filters;
51     graph->filters[graph->filter_count++] = filter;
52
53     return 0;
54 }
55
56 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
57 {
58     AVFilterContext *filt;
59     int i, j;
60
61     for (i=0; i < graph->filter_count; i++) {
62         filt = graph->filters[i];
63
64         for (j = 0; j < filt->input_count; j++) {
65             if (!filt->inputs[j] || !filt->inputs[j]->src) {
66                 av_log(log_ctx, AV_LOG_ERROR,
67                        "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
68                        filt->input_pads[j].name, filt->name, filt->filter->name);
69                 return -1;
70             }
71         }
72
73         for (j = 0; j < filt->output_count; j++) {
74             if (!filt->outputs[j] || !filt->outputs[j]->dst) {
75                 av_log(log_ctx, AV_LOG_ERROR,
76                        "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
77                        filt->output_pads[j].name, filt->name, filt->filter->name);
78                 return -1;
79             }
80         }
81     }
82
83     return 0;
84 }
85
86 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
87 {
88     AVFilterContext *filt;
89     int i, ret;
90
91     for (i=0; i < graph->filter_count; i++) {
92         filt = graph->filters[i];
93
94         if (!filt->output_count) {
95             if ((ret = avfilter_config_links(filt)))
96                 return ret;
97         }
98     }
99
100     return 0;
101 }
102
103 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
104 {
105     int i;
106
107     for(i = 0; i < graph->filter_count; i ++)
108         if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
109             return graph->filters[i];
110
111     return NULL;
112 }
113
114 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
115 {
116     int i, j;
117     int scaler_count = 0;
118     char inst_name[30];
119
120     /* ask all the sub-filters for their supported media formats */
121     for(i = 0; i < graph->filter_count; i ++) {
122         if(graph->filters[i]->filter->query_formats)
123             graph->filters[i]->filter->query_formats(graph->filters[i]);
124         else
125             avfilter_default_query_formats(graph->filters[i]);
126     }
127
128     /* go through and merge as many format lists as possible */
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             AVFilterLink *link = filter->inputs[j];
134             if(link && link->in_formats != link->out_formats) {
135                 if(!avfilter_merge_formats(link->in_formats,
136                                            link->out_formats)) {
137                     AVFilterContext *scale;
138                     char scale_args[256];
139                     /* couldn't merge format lists. auto-insert scale filter */
140                     snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
141                              scaler_count++);
142                     avfilter_open(&scale, avfilter_get_by_name("scale"), inst_name);
143
144                     snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
145                     if(!scale || scale->filter->init(scale, scale_args, NULL) ||
146                                  avfilter_insert_filter(link, scale, 0, 0)) {
147                         avfilter_free(scale);
148                         return -1;
149                     }
150
151                     if (avfilter_graph_add_filter(graph, scale) < 0)
152                         return -1;
153
154                     scale->filter->query_formats(scale);
155                     if (((link = scale-> inputs[0]) &&
156                          !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
157                         ((link = scale->outputs[0]) &&
158                          !avfilter_merge_formats(link->in_formats, link->out_formats))) {
159                         av_log(log_ctx, AV_LOG_ERROR,
160                                "Impossible to convert between the formats supported by the filter "
161                                "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
162                         return -1;
163                     }
164                 }
165             }
166         }
167     }
168
169     return 0;
170 }
171
172 static void pick_format(AVFilterLink *link)
173 {
174     if(!link || !link->in_formats)
175         return;
176
177     link->in_formats->format_count = 1;
178     link->format = link->in_formats->formats[0];
179
180     avfilter_formats_unref(&link->in_formats);
181     avfilter_formats_unref(&link->out_formats);
182 }
183
184 static void pick_formats(AVFilterGraph *graph)
185 {
186     int i, j;
187
188     for(i = 0; i < graph->filter_count; i ++) {
189         AVFilterContext *filter = graph->filters[i];
190
191         for(j = 0; j < filter->input_count; j ++)
192             pick_format(filter->inputs[j]);
193         for(j = 0; j < filter->output_count; j ++)
194             pick_format(filter->outputs[j]);
195     }
196 }
197
198 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
199 {
200     /* find supported formats from sub-filters, and merge along links */
201     if(query_formats(graph, log_ctx))
202         return -1;
203
204     /* Once everything is merged, it's possible that we'll still have
205      * multiple valid media format choices. We pick the first one. */
206     pick_formats(graph);
207
208     return 0;
209 }
210
211 int avfilter_graph_config(AVFilterGraph *graphctx, AVClass *log_ctx)
212 {
213     int ret;
214
215     if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
216         return ret;
217     if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
218         return ret;
219     if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
220         return ret;
221
222     return 0;
223 }