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