]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfiltergraph.c
Merge remote-tracking branch 'qatar/master'
[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 "libavutil/audioconvert.h"
27 #include "avfilter.h"
28 #include "avfiltergraph.h"
29 #include "internal.h"
30
31 #include "libavutil/log.h"
32
33 static const AVClass filtergraph_class = {
34     .class_name = "AVFilterGraph",
35     .item_name  = av_default_item_name,
36     .version    = LIBAVUTIL_VERSION_INT,
37 };
38
39 AVFilterGraph *avfilter_graph_alloc(void)
40 {
41     AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
42     if (!ret)
43         return NULL;
44 #if FF_API_GRAPH_AVCLASS
45     ret->av_class = &filtergraph_class;
46 #endif
47     return ret;
48 }
49
50 void avfilter_graph_free(AVFilterGraph **graph)
51 {
52     if (!*graph)
53         return;
54     for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
55         avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
56     av_freep(&(*graph)->scale_sws_opts);
57     av_freep(&(*graph)->filters);
58     av_freep(graph);
59 }
60
61 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
62 {
63     AVFilterContext **filters = av_realloc(graph->filters,
64                                            sizeof(AVFilterContext*) * (graph->filter_count+1));
65     if (!filters)
66         return AVERROR(ENOMEM);
67
68     graph->filters = filters;
69     graph->filters[graph->filter_count++] = filter;
70
71     return 0;
72 }
73
74 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
75                                  const char *name, const char *args, void *opaque,
76                                  AVFilterGraph *graph_ctx)
77 {
78     int ret;
79
80     if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
81         goto fail;
82     if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
83         goto fail;
84     if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
85         goto fail;
86     return 0;
87
88 fail:
89     if (*filt_ctx)
90         avfilter_free(*filt_ctx);
91     *filt_ctx = NULL;
92     return ret;
93 }
94
95 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
96 {
97     AVFilterContext *filt;
98     int i, j;
99
100     for (i = 0; i < graph->filter_count; i++) {
101         filt = graph->filters[i];
102
103         for (j = 0; j < filt->input_count; j++) {
104             if (!filt->inputs[j] || !filt->inputs[j]->src) {
105                 av_log(log_ctx, AV_LOG_ERROR,
106                        "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
107                        filt->input_pads[j].name, filt->name, filt->filter->name);
108                 return AVERROR(EINVAL);
109             }
110         }
111
112         for (j = 0; j < filt->output_count; j++) {
113             if (!filt->outputs[j] || !filt->outputs[j]->dst) {
114                 av_log(log_ctx, AV_LOG_ERROR,
115                        "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
116                        filt->output_pads[j].name, filt->name, filt->filter->name);
117                 return AVERROR(EINVAL);
118             }
119         }
120     }
121
122     return 0;
123 }
124
125 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
126 {
127     AVFilterContext *filt;
128     int i, ret;
129
130     for (i=0; i < graph->filter_count; i++) {
131         filt = graph->filters[i];
132
133         if (!filt->output_count) {
134             if ((ret = avfilter_config_links(filt)))
135                 return ret;
136         }
137     }
138
139     return 0;
140 }
141
142 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
143 {
144     int i;
145
146     for (i = 0; i < graph->filter_count; i++)
147         if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
148             return graph->filters[i];
149
150     return NULL;
151 }
152
153 static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
154                               const char *filt_name, const char *filt_args)
155 {
156     static int auto_count = 0, ret;
157     char inst_name[32];
158     AVFilterContext *filt_ctx;
159
160     snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
161             filt_name, auto_count++);
162
163     if ((ret = avfilter_graph_create_filter(&filt_ctx,
164                                             avfilter_get_by_name(filt_name),
165                                             inst_name, filt_args, NULL, graph)) < 0)
166         return ret;
167     if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
168         return ret;
169
170     filt_ctx->filter->query_formats(filt_ctx);
171
172     if ( ((link = filt_ctx-> inputs[0]) &&
173            !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
174          ((link = filt_ctx->outputs[0]) &&
175            !avfilter_merge_formats(link->in_formats, link->out_formats))
176        ) {
177         av_log(NULL, AV_LOG_ERROR,
178                "Impossible to convert between the formats supported by the filter "
179                "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
180         return AVERROR(EINVAL);
181     }
182
183     if (link->type == AVMEDIA_TYPE_AUDIO &&
184          (((link = filt_ctx-> inputs[0]) &&
185            (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
186             !avfilter_merge_formats(link->in_packing,   link->out_packing))) ||
187          ((link = filt_ctx->outputs[0]) &&
188            (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
189             !avfilter_merge_formats(link->in_packing,   link->out_packing))))
190        ) {
191         av_log(NULL, AV_LOG_ERROR,
192                "Impossible to convert between the channel layouts/packing formats supported by the filter "
193                "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
194         return AVERROR(EINVAL);
195     }
196
197     return 0;
198 }
199
200 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
201 {
202     int i, j, ret;
203     char filt_args[128];
204     AVFilterFormats *formats, *chlayouts, *packing;
205
206     /* ask all the sub-filters for their supported media formats */
207     for (i = 0; i < graph->filter_count; i++) {
208         if (graph->filters[i]->filter->query_formats)
209             graph->filters[i]->filter->query_formats(graph->filters[i]);
210         else
211             avfilter_default_query_formats(graph->filters[i]);
212     }
213
214     /* go through and merge as many format lists as possible */
215     for (i = 0; i < graph->filter_count; i++) {
216         AVFilterContext *filter = graph->filters[i];
217
218         for (j = 0; j < filter->input_count; j++) {
219             AVFilterLink *link = filter->inputs[j];
220             if (!link) continue;
221
222             if (!link->in_formats || !link->out_formats)
223                 return AVERROR(EINVAL);
224
225             if (link->type == AVMEDIA_TYPE_VIDEO &&
226                 !avfilter_merge_formats(link->in_formats, link->out_formats)) {
227
228                 /* couldn't merge format lists, auto-insert scale filter */
229                 snprintf(filt_args, sizeof(filt_args), "0:0:%s",
230                          graph->scale_sws_opts);
231                 if (ret = insert_conv_filter(graph, link, "scale", filt_args))
232                     return ret;
233             }
234             else if (link->type == AVMEDIA_TYPE_AUDIO) {
235                 if (!link->in_chlayouts || !link->out_chlayouts ||
236                     !link->in_packing   || !link->out_packing)
237                     return AVERROR(EINVAL);
238
239                 /* Merge all three list before checking: that way, in all
240                  * three categories, aconvert will use a common format
241                  * whenever possible. */
242                 formats   = avfilter_merge_formats(link->in_formats,   link->out_formats);
243                 chlayouts = avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts);
244                 packing   = avfilter_merge_formats(link->in_packing,   link->out_packing);
245                 if (!formats || !chlayouts || !packing)
246                     if (ret = insert_conv_filter(graph, link, "aconvert", NULL))
247                        return ret;
248             }
249         }
250     }
251
252     return 0;
253 }
254
255 static void pick_format(AVFilterLink *link)
256 {
257     if (!link || !link->in_formats)
258         return;
259
260     link->in_formats->format_count = 1;
261     link->format = link->in_formats->formats[0];
262     avfilter_formats_unref(&link->in_formats);
263     avfilter_formats_unref(&link->out_formats);
264
265     if (link->type == AVMEDIA_TYPE_AUDIO) {
266         link->in_chlayouts->format_count = 1;
267         link->channel_layout = link->in_chlayouts->formats[0];
268         avfilter_formats_unref(&link->in_chlayouts);
269         avfilter_formats_unref(&link->out_chlayouts);
270
271         link->in_packing->format_count = 1;
272         link->planar = link->in_packing->formats[0] == AVFILTER_PLANAR;
273         avfilter_formats_unref(&link->in_packing);
274         avfilter_formats_unref(&link->out_packing);
275     }
276 }
277
278 static int reduce_formats_on_filter(AVFilterContext *filter)
279 {
280     int i, j, k, ret = 0;
281
282     for (i = 0; i < filter->input_count; i++) {
283         AVFilterLink *link = filter->inputs[i];
284         int         format = link->out_formats->formats[0];
285
286         if (link->out_formats->format_count != 1)
287             continue;
288
289         for (j = 0; j < filter->output_count; j++) {
290             AVFilterLink *out_link = filter->outputs[j];
291             AVFilterFormats  *fmts = out_link->in_formats;
292
293             if (link->type != out_link->type ||
294                 out_link->in_formats->format_count == 1)
295                 continue;
296
297             for (k = 0; k < out_link->in_formats->format_count; k++)
298                 if (fmts->formats[k] == format) {
299                     fmts->formats[0]   = format;
300                     fmts->format_count = 1;
301                     ret = 1;
302                     break;
303                 }
304         }
305     }
306     return ret;
307 }
308
309 static void reduce_formats(AVFilterGraph *graph)
310 {
311     int i, reduced;
312
313     do {
314         reduced = 0;
315
316         for (i = 0; i < graph->filter_count; i++)
317             reduced |= reduce_formats_on_filter(graph->filters[i]);
318     } while (reduced);
319 }
320
321 static void pick_formats(AVFilterGraph *graph)
322 {
323     int i, j;
324
325     for (i = 0; i < graph->filter_count; i++) {
326         AVFilterContext *filter = graph->filters[i];
327
328         for (j = 0; j < filter->input_count; j++)
329             pick_format(filter->inputs[j]);
330         for (j = 0; j < filter->output_count; j++)
331             pick_format(filter->outputs[j]);
332     }
333 }
334
335 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
336 {
337     int ret;
338
339     /* find supported formats from sub-filters, and merge along links */
340     if ((ret = query_formats(graph, log_ctx)) < 0)
341         return ret;
342
343     /* Once everything is merged, it's possible that we'll still have
344      * multiple valid media format choices. We try to minimize the amount
345      * of format conversion inside filters */
346     reduce_formats(graph);
347
348     pick_formats(graph);
349
350     return 0;
351 }
352
353 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
354 {
355     int ret;
356
357     if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
358         return ret;
359     if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
360         return ret;
361     if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
362         return ret;
363
364     return 0;
365 }
366
367 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
368 {
369     int i, r = AVERROR(ENOSYS);
370
371     if(!graph)
372         return r;
373
374     if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
375         r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
376         if(r != AVERROR(ENOSYS))
377             return r;
378     }
379
380     if(res_len && res)
381         res[0]= 0;
382
383     for (i = 0; i < graph->filter_count; i++) {
384         AVFilterContext *filter = graph->filters[i];
385         if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
386             r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
387             if(r != AVERROR(ENOSYS)) {
388                 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
389                     return r;
390             }
391         }
392     }
393
394     return r;
395 }
396
397 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
398 {
399     int i;
400
401     if(!graph)
402         return 0;
403
404     for (i = 0; i < graph->filter_count; i++) {
405         AVFilterContext *filter = graph->filters[i];
406         if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
407             AVFilterCommand **que = &filter->command_queue, *next;
408             while(*que && (*que)->time <= ts)
409                 que = &(*que)->next;
410             next= *que;
411             *que= av_mallocz(sizeof(AVFilterCommand));
412             (*que)->command = av_strdup(command);
413             (*que)->arg     = av_strdup(arg);
414             (*que)->time    = ts;
415             (*que)->flags   = flags;
416             (*que)->next    = next;
417             if(flags & AVFILTER_CMD_FLAG_ONE)
418                 return 0;
419         }
420     }
421
422     return 0;
423 }