lavfi: make formats API private on next bump.
[ffmpeg.git] / 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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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 "formats.h"
29 #include "internal.h"
30
31 #include "libavutil/audioconvert.h"
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 /**
97  * Check for the validity of graph.
98  *
99  * A graph is considered valid if all its input and output pads are
100  * connected.
101  *
102  * @return 0 in case of success, a negative value otherwise
103  */
104 static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
105 {
106     AVFilterContext *filt;
107     int i, j;
108
109     for (i = 0; i < graph->filter_count; i++) {
110         filt = graph->filters[i];
111
112         for (j = 0; j < filt->input_count; j++) {
113             if (!filt->inputs[j] || !filt->inputs[j]->src) {
114                 av_log(log_ctx, AV_LOG_ERROR,
115                        "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
116                        filt->input_pads[j].name, filt->name, filt->filter->name);
117                 return AVERROR(EINVAL);
118             }
119         }
120
121         for (j = 0; j < filt->output_count; j++) {
122             if (!filt->outputs[j] || !filt->outputs[j]->dst) {
123                 av_log(log_ctx, AV_LOG_ERROR,
124                        "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
125                        filt->output_pads[j].name, filt->name, filt->filter->name);
126                 return AVERROR(EINVAL);
127             }
128         }
129     }
130
131     return 0;
132 }
133
134 /**
135  * Configure all the links of graphctx.
136  *
137  * @return 0 in case of success, a negative value otherwise
138  */
139 static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
140 {
141     AVFilterContext *filt;
142     int i, ret;
143
144     for (i=0; i < graph->filter_count; i++) {
145         filt = graph->filters[i];
146
147         if (!filt->output_count) {
148             if ((ret = avfilter_config_links(filt)))
149                 return ret;
150         }
151     }
152
153     return 0;
154 }
155
156 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
157 {
158     int i;
159
160     for (i = 0; i < graph->filter_count; i++)
161         if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
162             return graph->filters[i];
163
164     return NULL;
165 }
166
167 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
168 {
169     int i, j, ret;
170     int scaler_count = 0, resampler_count = 0;
171
172     /* ask all the sub-filters for their supported media formats */
173     for (i = 0; i < graph->filter_count; i++) {
174         if (graph->filters[i]->filter->query_formats)
175             graph->filters[i]->filter->query_formats(graph->filters[i]);
176         else
177             ff_default_query_formats(graph->filters[i]);
178     }
179
180     /* go through and merge as many format lists as possible */
181     for (i = 0; i < graph->filter_count; i++) {
182         AVFilterContext *filter = graph->filters[i];
183
184         for (j = 0; j < filter->input_count; j++) {
185             AVFilterLink *link = filter->inputs[j];
186             int convert_needed = 0;
187
188             if (!link)
189                 continue;
190
191             if (link->in_formats != link->out_formats &&
192                 !ff_merge_formats(link->in_formats,
193                                         link->out_formats))
194                 convert_needed = 1;
195             if (link->type == AVMEDIA_TYPE_AUDIO) {
196                 if (link->in_channel_layouts != link->out_channel_layouts &&
197                     !ff_merge_channel_layouts(link->in_channel_layouts,
198                                               link->out_channel_layouts))
199                     convert_needed = 1;
200                 if (link->in_samplerates != link->out_samplerates &&
201                     !ff_merge_samplerates(link->in_samplerates,
202                                           link->out_samplerates))
203                     convert_needed = 1;
204             }
205
206             if (convert_needed) {
207                 AVFilterContext *convert;
208                 AVFilter *filter;
209                 AVFilterLink *inlink, *outlink;
210                 char scale_args[256];
211                 char inst_name[30];
212
213                 /* couldn't merge format lists. auto-insert conversion filter */
214                 switch (link->type) {
215                 case AVMEDIA_TYPE_VIDEO:
216                     snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
217                              scaler_count++);
218                     snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
219                     if ((ret = avfilter_graph_create_filter(&convert,
220                                                             avfilter_get_by_name("scale"),
221                                                             inst_name, scale_args, NULL,
222                                                             graph)) < 0)
223                         return ret;
224                     break;
225                 case AVMEDIA_TYPE_AUDIO:
226                     if (!(filter = avfilter_get_by_name("resample"))) {
227                         av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
228                                "not present, cannot convert audio formats.\n");
229                         return AVERROR(EINVAL);
230                     }
231
232                     snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
233                              resampler_count++);
234                     if ((ret = avfilter_graph_create_filter(&convert,
235                                                             avfilter_get_by_name("resample"),
236                                                             inst_name, NULL, NULL, graph)) < 0)
237                         return ret;
238                     break;
239                 default:
240                     return AVERROR(EINVAL);
241                 }
242
243                 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
244                     return ret;
245
246                 convert->filter->query_formats(convert);
247                 inlink  = convert->inputs[0];
248                 outlink = convert->outputs[0];
249                 if (!ff_merge_formats( inlink->in_formats,  inlink->out_formats) ||
250                     !ff_merge_formats(outlink->in_formats, outlink->out_formats))
251                     ret |= AVERROR(ENOSYS);
252                 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
253                     (!ff_merge_samplerates(inlink->in_samplerates,
254                                            inlink->out_samplerates) ||
255                      !ff_merge_channel_layouts(inlink->in_channel_layouts,
256                                                inlink->out_channel_layouts)))
257                     ret |= AVERROR(ENOSYS);
258                 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
259                     (!ff_merge_samplerates(outlink->in_samplerates,
260                                            outlink->out_samplerates) ||
261                      !ff_merge_channel_layouts(outlink->in_channel_layouts,
262                                                outlink->out_channel_layouts)))
263                     ret |= AVERROR(ENOSYS);
264
265                 if (ret < 0) {
266                     av_log(log_ctx, AV_LOG_ERROR,
267                            "Impossible to convert between the formats supported by the filter "
268                            "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
269                     return ret;
270                 }
271             }
272         }
273     }
274
275     return 0;
276 }
277
278 static int pick_format(AVFilterLink *link)
279 {
280     if (!link || !link->in_formats)
281         return 0;
282
283     link->in_formats->format_count = 1;
284     link->format = link->in_formats->formats[0];
285
286     if (link->type == AVMEDIA_TYPE_AUDIO) {
287         if (!link->in_samplerates->format_count) {
288             av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
289                    " the link between filters %s and %s.\n", link->src->name,
290                    link->dst->name);
291             return AVERROR(EINVAL);
292         }
293         link->in_samplerates->format_count = 1;
294         link->sample_rate = link->in_samplerates->formats[0];
295
296         if (!link->in_channel_layouts->nb_channel_layouts) {
297             av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
298                    "the link between filters %s and %s.\n", link->src->name,
299                    link->dst->name);
300             return AVERROR(EINVAL);
301         }
302         link->in_channel_layouts->nb_channel_layouts = 1;
303         link->channel_layout = link->in_channel_layouts->channel_layouts[0];
304     }
305
306     ff_formats_unref(&link->in_formats);
307     ff_formats_unref(&link->out_formats);
308     ff_formats_unref(&link->in_samplerates);
309     ff_formats_unref(&link->out_samplerates);
310     ff_channel_layouts_unref(&link->in_channel_layouts);
311     ff_channel_layouts_unref(&link->out_channel_layouts);
312
313     return 0;
314 }
315
316 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
317 do {                                                                   \
318     for (i = 0; i < filter->input_count; i++) {                        \
319         AVFilterLink *link = filter->inputs[i];                        \
320         fmt_type fmt;                                                  \
321                                                                        \
322         if (!link->out_ ## list || link->out_ ## list->nb != 1)        \
323             continue;                                                  \
324         fmt = link->out_ ## list->var[0];                              \
325                                                                        \
326         for (j = 0; j < filter->output_count; j++) {                   \
327             AVFilterLink *out_link = filter->outputs[j];               \
328             list_type *fmts;                                           \
329                                                                        \
330             if (link->type != out_link->type ||                        \
331                 out_link->in_ ## list->nb == 1)                        \
332                 continue;                                              \
333             fmts = out_link->in_ ## list;                              \
334                                                                        \
335             if (!out_link->in_ ## list->nb) {                          \
336                 add_format(&out_link->in_ ##list, fmt);                \
337                 break;                                                 \
338             }                                                          \
339                                                                        \
340             for (k = 0; k < out_link->in_ ## list->nb; k++)            \
341                 if (fmts->var[k] == fmt) {                             \
342                     fmts->var[0]  = fmt;                               \
343                     fmts->nb = 1;                                      \
344                     ret = 1;                                           \
345                     break;                                             \
346                 }                                                      \
347         }                                                              \
348     }                                                                  \
349 } while (0)
350
351 static int reduce_formats_on_filter(AVFilterContext *filter)
352 {
353     int i, j, k, ret = 0;
354
355     REDUCE_FORMATS(int,      AVFilterFormats,        formats,         formats,
356                    format_count, ff_add_format);
357     REDUCE_FORMATS(int,      AVFilterFormats,        samplerates,     formats,
358                    format_count, ff_add_format);
359     REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
360                    channel_layouts, nb_channel_layouts, ff_add_channel_layout);
361
362     return ret;
363 }
364
365 static void reduce_formats(AVFilterGraph *graph)
366 {
367     int i, reduced;
368
369     do {
370         reduced = 0;
371
372         for (i = 0; i < graph->filter_count; i++)
373             reduced |= reduce_formats_on_filter(graph->filters[i]);
374     } while (reduced);
375 }
376
377 static void swap_samplerates_on_filter(AVFilterContext *filter)
378 {
379     AVFilterLink *link = NULL;
380     int sample_rate;
381     int i, j;
382
383     for (i = 0; i < filter->input_count; i++) {
384         link = filter->inputs[i];
385
386         if (link->type == AVMEDIA_TYPE_AUDIO &&
387             link->out_samplerates->format_count == 1)
388             break;
389     }
390     if (i == filter->input_count)
391         return;
392
393     sample_rate = link->out_samplerates->formats[0];
394
395     for (i = 0; i < filter->output_count; i++) {
396         AVFilterLink *outlink = filter->outputs[i];
397         int best_idx, best_diff = INT_MAX;
398
399         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
400             outlink->in_samplerates->format_count < 2)
401             continue;
402
403         for (j = 0; j < outlink->in_samplerates->format_count; j++) {
404             int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
405
406             if (diff < best_diff) {
407                 best_diff = diff;
408                 best_idx  = j;
409             }
410         }
411         FFSWAP(int, outlink->in_samplerates->formats[0],
412                outlink->in_samplerates->formats[best_idx]);
413     }
414 }
415
416 static void swap_samplerates(AVFilterGraph *graph)
417 {
418     int i;
419
420     for (i = 0; i < graph->filter_count; i++)
421         swap_samplerates_on_filter(graph->filters[i]);
422 }
423
424 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
425 {
426     AVFilterLink *link = NULL;
427     uint64_t chlayout;
428     int i, j;
429
430     for (i = 0; i < filter->input_count; i++) {
431         link = filter->inputs[i];
432
433         if (link->type == AVMEDIA_TYPE_AUDIO &&
434             link->out_channel_layouts->nb_channel_layouts == 1)
435             break;
436     }
437     if (i == filter->input_count)
438         return;
439
440     chlayout = link->out_channel_layouts->channel_layouts[0];
441
442     for (i = 0; i < filter->output_count; i++) {
443         AVFilterLink *outlink = filter->outputs[i];
444         int best_idx, best_score = INT_MIN;
445
446         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
447             outlink->in_channel_layouts->nb_channel_layouts < 2)
448             continue;
449
450         for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
451             uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
452             int matched_channels  = av_get_channel_layout_nb_channels(chlayout &
453                                                                       out_chlayout);
454             int extra_channels     = av_get_channel_layout_nb_channels(out_chlayout &
455                                                                        (~chlayout));
456             int score = matched_channels - extra_channels;
457
458             if (score > best_score) {
459                 best_score = score;
460                 best_idx   = j;
461             }
462         }
463         FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
464                outlink->in_channel_layouts->channel_layouts[best_idx]);
465     }
466
467 }
468
469 static void swap_channel_layouts(AVFilterGraph *graph)
470 {
471     int i;
472
473     for (i = 0; i < graph->filter_count; i++)
474         swap_channel_layouts_on_filter(graph->filters[i]);
475 }
476
477 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
478 {
479     AVFilterLink *link = NULL;
480     int format, bps;
481     int i, j;
482
483     for (i = 0; i < filter->input_count; i++) {
484         link = filter->inputs[i];
485
486         if (link->type == AVMEDIA_TYPE_AUDIO &&
487             link->out_formats->format_count == 1)
488             break;
489     }
490     if (i == filter->input_count)
491         return;
492
493     format = link->out_formats->formats[0];
494     bps    = av_get_bytes_per_sample(format);
495
496     for (i = 0; i < filter->output_count; i++) {
497         AVFilterLink *outlink = filter->outputs[i];
498         int best_idx, best_score = INT_MIN;
499
500         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
501             outlink->in_formats->format_count < 2)
502             continue;
503
504         for (j = 0; j < outlink->in_formats->format_count; j++) {
505             int out_format = outlink->in_formats->formats[j];
506             int out_bps    = av_get_bytes_per_sample(out_format);
507             int score;
508
509             if (av_get_packed_sample_fmt(out_format) == format ||
510                 av_get_planar_sample_fmt(out_format) == format) {
511                 best_idx   = j;
512                 break;
513             }
514
515             /* for s32 and float prefer double to prevent loss of information */
516             if (bps == 4 && out_bps == 8) {
517                 best_idx = j;
518                 break;
519             }
520
521             /* prefer closest higher or equal bps */
522             score = -abs(out_bps - bps);
523             if (out_bps >= bps)
524                 score += INT_MAX/2;
525
526             if (score > best_score) {
527                 best_score = score;
528                 best_idx   = j;
529             }
530         }
531         FFSWAP(int, outlink->in_formats->formats[0],
532                outlink->in_formats->formats[best_idx]);
533     }
534 }
535
536 static void swap_sample_fmts(AVFilterGraph *graph)
537 {
538     int i;
539
540     for (i = 0; i < graph->filter_count; i++)
541         swap_sample_fmts_on_filter(graph->filters[i]);
542
543 }
544
545 static int pick_formats(AVFilterGraph *graph)
546 {
547     int i, j, ret;
548
549     for (i = 0; i < graph->filter_count; i++) {
550         AVFilterContext *filter = graph->filters[i];
551
552         for (j = 0; j < filter->input_count; j++)
553             if ((ret = pick_format(filter->inputs[j])) < 0)
554                 return ret;
555         for (j = 0; j < filter->output_count; j++)
556             if ((ret = pick_format(filter->outputs[j])) < 0)
557                 return ret;
558     }
559     return 0;
560 }
561
562 /**
563  * Configure the formats of all the links in the graph.
564  */
565 static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
566 {
567     int ret;
568
569     /* find supported formats from sub-filters, and merge along links */
570     if ((ret = query_formats(graph, log_ctx)) < 0)
571         return ret;
572
573     /* Once everything is merged, it's possible that we'll still have
574      * multiple valid media format choices. We try to minimize the amount
575      * of format conversion inside filters */
576     reduce_formats(graph);
577
578     /* for audio filters, ensure the best format, sample rate and channel layout
579      * is selected */
580     swap_sample_fmts(graph);
581     swap_samplerates(graph);
582     swap_channel_layouts(graph);
583
584     if ((ret = pick_formats(graph)) < 0)
585         return ret;
586
587     return 0;
588 }
589
590 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
591 {
592     int ret;
593
594     if ((ret = graph_check_validity(graphctx, log_ctx)))
595         return ret;
596     if ((ret = graph_config_formats(graphctx, log_ctx)))
597         return ret;
598     if ((ret = graph_config_links(graphctx, log_ctx)))
599         return ret;
600
601     return 0;
602 }