]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfiltergraph.c
Remove avfilter_vf_graph
[ffmpeg] / libavfilter / avfiltergraph.c
1 /*
2  * filter graphs
3  * copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "avfilter.h"
23 #include "avfiltergraph.h"
24
25 /**
26  * For use in av_log
27  */
28 static const char *log_name(void *p)
29 {
30     return "Filter parser";
31 }
32
33 static const AVClass filter_parser_class = {
34     "Filter parser",
35     log_name
36 };
37
38 static const AVClass *log_ctx = &filter_parser_class;
39
40 static void uninit(GraphContext *graph)
41 {
42     for(; graph->filter_count > 0; graph->filter_count --)
43         avfilter_destroy(graph->filters[graph->filter_count - 1]);
44     av_freep(&graph->filters);
45 }
46
47 /* TODO: insert in sorted order */
48 void avfilter_graph_add_filter(GraphContext *graph, AVFilterContext *filter)
49 {
50     graph->filters = av_realloc(graph->filters,
51                                 sizeof(AVFilterContext*) * ++graph->filter_count);
52     graph->filters[graph->filter_count - 1] = filter;
53 }
54
55 /* search intelligently, once we insert in order */
56 AVFilterContext *avfilter_graph_get_filter(GraphContext *graph, char *name)
57 {
58     int i;
59
60     if(!name)
61         return NULL;
62
63     for(i = 0; i < graph->filter_count; i ++)
64         if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
65             return graph->filters[i];
66
67     return NULL;
68 }
69
70 static int query_formats(GraphContext *graph)
71 {
72     int i, j;
73
74     /* ask all the sub-filters for their supported colorspaces */
75     for(i = 0; i < graph->filter_count; i ++) {
76         if(graph->filters[i]->filter->query_formats)
77             graph->filters[i]->filter->query_formats(graph->filters[i]);
78         else
79             avfilter_default_query_formats(graph->filters[i]);
80     }
81
82     /* go through and merge as many format lists as possible */
83     for(i = 0; i < graph->filter_count; i ++) {
84         AVFilterContext *filter = graph->filters[i];
85
86         for(j = 0; j < filter->input_count; j ++) {
87             AVFilterLink *link;
88             if(!(link = filter->inputs[j]))
89                 continue;
90             if(link->in_formats != link->out_formats) {
91                 if(!avfilter_merge_formats(link->in_formats,
92                                            link->out_formats)) {
93                     /* couldn't merge format lists. auto-insert scale filter */
94                     AVFilterContext *scale;
95
96                     if(!(scale =
97                          avfilter_open(avfilter_get_by_name("scale"), NULL)))
98                         return -1;
99                     if(scale->filter->init(scale, NULL, NULL) ||
100                        avfilter_insert_filter(link, scale, 0, 0)) {
101                         avfilter_destroy(scale);
102                         return -1;
103                     }
104
105                     avfilter_graph_add_filter(graph, scale);
106                     scale->filter->query_formats(scale);
107                     if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
108                                               scale-> inputs[0]->out_formats) ||
109                        !avfilter_merge_formats(scale->outputs[0]->in_formats,
110                                               scale->outputs[0]->out_formats))
111                         return -1;
112                 }
113             }
114         }
115     }
116
117     return 0;
118 }
119
120 static void pick_format(AVFilterLink *link)
121 {
122     if(!link || !link->in_formats)
123         return;
124
125     link->in_formats->format_count = 1;
126     link->format = link->in_formats->formats[0];
127
128     avfilter_formats_unref(&link->in_formats);
129     avfilter_formats_unref(&link->out_formats);
130 }
131
132 static void pick_formats(GraphContext *graph)
133 {
134     int i, j;
135
136     for(i = 0; i < graph->filter_count; i ++) {
137         AVFilterContext *filter = graph->filters[i];
138
139         for(j = 0; j < filter->input_count; j ++)
140             pick_format(filter->inputs[j]);
141         for(j = 0; j < filter->output_count; j ++)
142             pick_format(filter->outputs[j]);
143     }
144 }
145
146 int avfilter_graph_config_formats(GraphContext *graph)
147 {
148     /* find supported formats from sub-filters, and merge along links */
149     if(query_formats(graph))
150         return -1;
151
152     /* Once everything is merged, it's possible that we'll still have
153      * multiple valid colorspace choices. We pick the first one. */
154     pick_formats(graph);
155
156     return 0;
157 }
158
159 static int graph_load_from_desc2(GraphContext *ctx, AVFilterGraphDesc *desc)
160 {
161     AVFilterGraphDescFilter *curfilt;
162     AVFilterGraphDescLink   *curlink;
163     AVFilterContext *filt, *filtb;
164
165     AVFilter *filterdef;
166     char tmp[20];
167
168     /* create all filters */
169     for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
170         snprintf(tmp, 20, "%d", curfilt->index);
171         if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
172            !(filt = avfilter_open(filterdef, tmp))) {
173             av_log(&log_ctx, AV_LOG_ERROR,
174                "error creating filter '%s'\n", curfilt->filter);
175             goto fail;
176         }
177         avfilter_graph_add_filter(ctx, filt);
178         if(avfilter_init_filter(filt, curfilt->args, NULL)) {
179             av_log(&log_ctx, AV_LOG_ERROR,
180                 "error initializing filter '%s'\n", curfilt->filter);
181             goto fail;
182         }
183     }
184
185     /* create all links */
186     for(curlink = desc->links; curlink; curlink = curlink->next) {
187         snprintf(tmp, 20, "%d", curlink->src);
188         if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
189             av_log(&log_ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
190             goto fail;
191         }
192         snprintf(tmp, 20, "%d", curlink->dst);
193         if(!(filtb = avfilter_graph_get_filter(ctx, tmp))) {
194             av_log(&log_ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
195             goto fail;
196         }
197         if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
198             av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
199             goto fail;
200         }
201     }
202
203     return 0;
204
205 fail:
206     uninit(ctx);
207     return -1;
208 }
209
210 int graph_load_from_desc3(GraphContext *graph, AVFilterGraphDesc *desc, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
211 {
212     AVFilterGraphDescExport *curpad;
213     char tmp[20];
214     AVFilterContext *filt;
215
216     if (graph_load_from_desc2(graph, desc) < 0)
217         goto fail;
218
219     /* export all input pads */
220     for(curpad = desc->inputs; curpad; curpad = curpad->next) {
221         snprintf(tmp, 20, "%d", curpad->filter);
222         if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
223             av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
224             goto fail;
225         }
226         if(avfilter_link(in, inpad, filt, curpad->pad)) {
227             av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
228             goto fail;
229         }
230     }
231
232     /* export all output pads */
233     for(curpad = desc->outputs; curpad; curpad = curpad->next) {
234         snprintf(tmp, 20, "%d", curpad->filter);
235         if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
236             av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
237             goto fail;
238         }
239
240         if(avfilter_link(filt, curpad->pad, out, outpad)) {
241             av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
242             goto fail;
243         }
244     }
245
246     return 0;
247
248 fail:
249     uninit(graph);
250     return -1;
251 }