d3c4ad9c09d0e2c06c6b664850d7fde5cf2e10ae
[ffmpeg.git] / 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 typedef struct AVFilterGraph {
26     unsigned filter_count;
27     AVFilterContext **filters;
28
29     /** fake filters to handle links to internal filters */
30     AVFilterContext *link_filter_in;
31     AVFilterContext *link_filter_out;
32 } GraphContext;
33
34 typedef struct {
35     AVFilterContext *graph;
36 } GraphLinkContext;
37
38 static int link_init(AVFilterContext *ctx, const char *args, void *opaque)
39 {
40     GraphLinkContext *linkctx = ctx->priv;
41     linkctx->graph = opaque;
42     return !opaque;
43 }
44
45 /**
46  * Given the link between the dummy filter and an internal filter whose input
47  * is being exported outside the graph, this returns the externally visible
48  * link.
49  */
50 static inline AVFilterLink *get_extern_input_link(AVFilterLink *link)
51 {
52     GraphLinkContext *lctx = link->src->priv;
53     return lctx->graph->inputs[link->srcpad];
54 }
55
56 /**
57  * Given the link between the dummy filter and an internal filter whose output
58  * is being exported outside the graph, this returns the externally visible
59  * link.
60  */
61 static inline AVFilterLink *get_extern_output_link(AVFilterLink *link)
62 {
63     GraphLinkContext *lctx = link->dst->priv;
64     return lctx->graph->outputs[link->dstpad];
65 }
66
67
68 /** dummy filter used to help export filters pads outside the graph */
69 static AVFilter vf_graph_dummy =
70 {
71     .name      = "graph_dummy",
72
73     .priv_size = sizeof(GraphLinkContext),
74
75     .init      = link_init,
76
77     .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
78     .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
79 };
80
81 static void uninit(AVFilterContext *ctx)
82 {
83     GraphContext *graph = ctx->priv;
84
85     if(graph->link_filter_in) {
86         avfilter_destroy(graph->link_filter_in);
87         graph->link_filter_in = NULL;
88     }
89     if(graph->link_filter_out) {
90         avfilter_destroy(graph->link_filter_out);
91         graph->link_filter_out = NULL;
92     }
93     for(; graph->filter_count > 0; graph->filter_count --)
94         avfilter_destroy(graph->filters[graph->filter_count - 1]);
95     av_freep(&graph->filters);
96 }
97
98 /* TODO: insert in sorted order */
99 void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
100 {
101     GraphContext *graph = graphctx->priv;
102
103     graph->filters = av_realloc(graph->filters,
104                                 sizeof(AVFilterContext*) * ++graph->filter_count);
105     graph->filters[graph->filter_count - 1] = filter;
106 }
107
108 /* search intelligently, once we insert in order */
109 AVFilterContext *avfilter_graph_get_filter(AVFilterContext *ctx, char *name)
110 {
111     GraphContext *graph = ctx->priv;
112     int i;
113
114     if(!name)
115         return NULL;
116
117     for(i = 0; i < graph->filter_count; i ++)
118         if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
119             return graph->filters[i];
120
121     return NULL;
122 }
123
124 static int query_formats(AVFilterContext *graphctx)
125 {
126     GraphContext *graph = graphctx->priv;
127     AVFilterContext *linkfiltin  = graph->link_filter_in;
128     AVFilterContext *linkfiltout = graph->link_filter_out;
129     int i, j;
130
131     /* ask all the sub-filters for their supported colorspaces */
132     for(i = 0; i < graph->filter_count; i ++) {
133         if(graph->filters[i]->filter->query_formats)
134             graph->filters[i]->filter->query_formats(graph->filters[i]);
135         else
136             avfilter_default_query_formats(graph->filters[i]);
137     }
138
139     /* use these formats on our exported links */
140     for(i = 0; i < linkfiltout->input_count; i ++) {
141         avfilter_formats_ref( linkfiltout->inputs[i]->in_formats,
142                              &linkfiltout->inputs[i]->out_formats);
143
144         if(graphctx->outputs[i])
145             avfilter_formats_ref(linkfiltout->inputs[i]->in_formats,
146                                  &graphctx->outputs[i]->in_formats);
147     }
148     for(i = 0; i < linkfiltin->output_count; i ++) {
149         avfilter_formats_ref( linkfiltin->outputs[i]->out_formats,
150                              &linkfiltin->outputs[i]->in_formats);
151
152         if(graphctx->inputs[i])
153             avfilter_formats_ref(linkfiltin->outputs[i]->out_formats,
154                                  &graphctx-> inputs[i]->out_formats);
155     }
156
157     /* go through and merge as many format lists as possible */
158     for(i = 0; i < graph->filter_count; i ++) {
159         AVFilterContext *filter = graph->filters[i];
160
161         for(j = 0; j < filter->input_count; j ++) {
162             AVFilterLink *link;
163             if(!(link = filter->inputs[j]))
164                 continue;
165             if(link->in_formats != link->out_formats) {
166                 if(!avfilter_merge_formats(link->in_formats,
167                                            link->out_formats)) {
168                     /* couldn't merge format lists. auto-insert scale filter */
169                     AVFilterContext *scale;
170
171                     if(!(scale =
172                          avfilter_open(avfilter_get_by_name("scale"), NULL)))
173                         return -1;
174                     if(scale->filter->init(scale, NULL, NULL) ||
175                        avfilter_insert_filter(link, scale, 0, 0)) {
176                         avfilter_destroy(scale);
177                         return -1;
178                     }
179
180                     avfilter_graph_add_filter(graphctx, scale);
181                     scale->filter->query_formats(scale);
182                     if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
183                                               scale-> inputs[0]->out_formats) ||
184                        !avfilter_merge_formats(scale->outputs[0]->in_formats,
185                                               scale->outputs[0]->out_formats))
186                         return -1;
187                 }
188             }
189         }
190     }
191
192     return 0;
193 }
194
195 static void pick_format(AVFilterLink *link)
196 {
197     if(!link || !link->in_formats)
198         return;
199
200     link->in_formats->format_count = 1;
201     link->format = link->in_formats->formats[0];
202
203     avfilter_formats_unref(&link->in_formats);
204     avfilter_formats_unref(&link->out_formats);
205 }
206
207 static void pick_formats(GraphContext *graph)
208 {
209     int i, j;
210
211     for(i = 0; i < graph->filter_count; i ++) {
212         AVFilterContext *filter = graph->filters[i];
213
214         if(filter->filter == &avfilter_vf_graph)
215             pick_formats(filter->priv);
216
217         for(j = 0; j < filter->input_count; j ++)
218             pick_format(filter->inputs[j]);
219         for(j = 0; j < filter->output_count; j ++)
220             pick_format(filter->outputs[j]);
221     }
222 }
223
224 int avfilter_graph_config_formats(AVFilterContext *graphctx)
225 {
226     GraphContext *graph = graphctx->priv;
227
228     /* find supported formats from sub-filters, and merge along links */
229     if(query_formats(graphctx))
230         return -1;
231
232     /* Once everything is merged, it's possible that we'll still have
233      * multiple valid colorspace choices. We pick the first one. */
234     pick_formats(graph);
235
236     return 0;
237 }
238
239 static int graph_load_from_desc2(AVFilterContext *ctx, AVFilterGraphDesc *desc)
240 {
241     AVFilterGraphDescFilter *curfilt;
242     AVFilterGraphDescLink   *curlink;
243     AVFilterContext *filt, *filtb;
244
245     AVFilter *filterdef;
246     char tmp[20];
247
248     /* create all filters */
249     for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
250         snprintf(tmp, 20, "%d", curfilt->index);
251         if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
252            !(filt = avfilter_open(filterdef, tmp))) {
253             av_log(ctx, AV_LOG_ERROR,
254                "error creating filter '%s'\n", curfilt->filter);
255             goto fail;
256         }
257         avfilter_graph_add_filter(ctx, filt);
258         if(avfilter_init_filter(filt, curfilt->args, NULL)) {
259             av_log(ctx, AV_LOG_ERROR,
260                 "error initializing filter '%s'\n", curfilt->filter);
261             goto fail;
262         }
263     }
264
265     /* create all links */
266     for(curlink = desc->links; curlink; curlink = curlink->next) {
267         snprintf(tmp, 20, "%d", curlink->src);
268         if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
269             av_log(ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
270             goto fail;
271         }
272         snprintf(tmp, 20, "%d", curlink->dst);
273         if(!(filtb = avfilter_graph_get_filter(ctx, tmp))) {
274             av_log(ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
275             goto fail;
276         }
277         if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
278             av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
279             goto fail;
280         }
281     }
282
283     return 0;
284
285 fail:
286     uninit(ctx);
287     return -1;
288 }
289
290 int graph_load_from_desc3(AVFilterContext *ctx, AVFilterGraphDesc *desc, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
291 {
292     AVFilterGraphDescExport *curpad;
293     char tmp[20];
294     AVFilterContext *filt;
295
296     if (graph_load_from_desc2(ctx, desc) < 0)
297         goto fail;
298
299     /* export all input pads */
300     for(curpad = desc->inputs; curpad; curpad = curpad->next) {
301         snprintf(tmp, 20, "%d", curpad->filter);
302         if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
303             av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
304             goto fail;
305         }
306         if(avfilter_link(in, inpad, filt, curpad->pad)) {
307             av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
308             goto fail;
309         }
310     }
311
312     /* export all output pads */
313     for(curpad = desc->outputs; curpad; curpad = curpad->next) {
314         snprintf(tmp, 20, "%d", curpad->filter);
315         if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
316             av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
317             goto fail;
318         }
319
320         if(avfilter_link(filt, curpad->pad, out, outpad)) {
321             av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
322             goto fail;
323         }
324     }
325
326     return 0;
327
328 fail:
329     uninit(ctx);
330     return -1;
331 }
332
333 static int init(AVFilterContext *ctx, const char *args, void *opaque)
334 {
335     GraphContext *gctx = ctx->priv;
336
337     if(!(gctx->link_filter_in = avfilter_open(&vf_graph_dummy, NULL)))
338         return -1;
339     if(avfilter_init_filter(gctx->link_filter_in, NULL, ctx))
340         goto fail;
341     if(!(gctx->link_filter_out = avfilter_open(&vf_graph_dummy, NULL)))
342         goto fail;
343     if(avfilter_init_filter(gctx->link_filter_out, NULL, ctx))
344         goto fail;
345
346     return 0;
347
348 fail:
349     avfilter_destroy(gctx->link_filter_in);
350     if(gctx->link_filter_out)
351         avfilter_destroy(gctx->link_filter_out);
352     return -1;
353 }
354
355 AVFilter avfilter_vf_graph =
356 {
357     .name      = "graph",
358
359     .priv_size = sizeof(GraphContext),
360
361     .init      = init,
362     .uninit    = uninit,
363
364     .query_formats = query_formats,
365
366     .inputs    = (AVFilterPad[]) {{ .name = NULL, }},
367     .outputs   = (AVFilterPad[]) {{ .name = NULL, }},
368 };
369