avfiltergraph: remove a redundant call to avfilter_get_by_name().
[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->nb_inputs; 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->nb_outputs; 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->nb_outputs) {
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->nb_inputs; 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                     if (!(filter = avfilter_get_by_name("scale"))) {
217                         av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
218                                "not present, cannot convert pixel formats.\n");
219                         return AVERROR(EINVAL);
220                     }
221
222                     snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
223                              scaler_count++);
224                     snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
225                     if ((ret = avfilter_graph_create_filter(&convert, filter,
226                                                             inst_name, scale_args, NULL,
227                                                             graph)) < 0)
228                         return ret;
229                     break;
230                 case AVMEDIA_TYPE_AUDIO:
231                     if (!(filter = avfilter_get_by_name("resample"))) {
232                         av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
233                                "not present, cannot convert audio formats.\n");
234                         return AVERROR(EINVAL);
235                     }
236
237                     snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
238                              resampler_count++);
239                     if ((ret = avfilter_graph_create_filter(&convert, filter,
240                                                             inst_name, NULL, NULL, graph)) < 0)
241                         return ret;
242                     break;
243                 default:
244                     return AVERROR(EINVAL);
245                 }
246
247                 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
248                     return ret;
249
250                 convert->filter->query_formats(convert);
251                 inlink  = convert->inputs[0];
252                 outlink = convert->outputs[0];
253                 if (!ff_merge_formats( inlink->in_formats,  inlink->out_formats) ||
254                     !ff_merge_formats(outlink->in_formats, outlink->out_formats))
255                     ret |= AVERROR(ENOSYS);
256                 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
257                     (!ff_merge_samplerates(inlink->in_samplerates,
258                                            inlink->out_samplerates) ||
259                      !ff_merge_channel_layouts(inlink->in_channel_layouts,
260                                                inlink->out_channel_layouts)))
261                     ret |= AVERROR(ENOSYS);
262                 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
263                     (!ff_merge_samplerates(outlink->in_samplerates,
264                                            outlink->out_samplerates) ||
265                      !ff_merge_channel_layouts(outlink->in_channel_layouts,
266                                                outlink->out_channel_layouts)))
267                     ret |= AVERROR(ENOSYS);
268
269                 if (ret < 0) {
270                     av_log(log_ctx, AV_LOG_ERROR,
271                            "Impossible to convert between the formats supported by the filter "
272                            "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
273                     return ret;
274                 }
275             }
276         }
277     }
278
279     return 0;
280 }
281
282 static int pick_format(AVFilterLink *link)
283 {
284     if (!link || !link->in_formats)
285         return 0;
286
287     link->in_formats->format_count = 1;
288     link->format = link->in_formats->formats[0];
289
290     if (link->type == AVMEDIA_TYPE_AUDIO) {
291         if (!link->in_samplerates->format_count) {
292             av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
293                    " the link between filters %s and %s.\n", link->src->name,
294                    link->dst->name);
295             return AVERROR(EINVAL);
296         }
297         link->in_samplerates->format_count = 1;
298         link->sample_rate = link->in_samplerates->formats[0];
299
300         if (!link->in_channel_layouts->nb_channel_layouts) {
301             av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
302                    "the link between filters %s and %s.\n", link->src->name,
303                    link->dst->name);
304             return AVERROR(EINVAL);
305         }
306         link->in_channel_layouts->nb_channel_layouts = 1;
307         link->channel_layout = link->in_channel_layouts->channel_layouts[0];
308     }
309
310     ff_formats_unref(&link->in_formats);
311     ff_formats_unref(&link->out_formats);
312     ff_formats_unref(&link->in_samplerates);
313     ff_formats_unref(&link->out_samplerates);
314     ff_channel_layouts_unref(&link->in_channel_layouts);
315     ff_channel_layouts_unref(&link->out_channel_layouts);
316
317     return 0;
318 }
319
320 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
321 do {                                                                   \
322     for (i = 0; i < filter->nb_inputs; i++) {                          \
323         AVFilterLink *link = filter->inputs[i];                        \
324         fmt_type fmt;                                                  \
325                                                                        \
326         if (!link->out_ ## list || link->out_ ## list->nb != 1)        \
327             continue;                                                  \
328         fmt = link->out_ ## list->var[0];                              \
329                                                                        \
330         for (j = 0; j < filter->nb_outputs; j++) {                     \
331             AVFilterLink *out_link = filter->outputs[j];               \
332             list_type *fmts;                                           \
333                                                                        \
334             if (link->type != out_link->type ||                        \
335                 out_link->in_ ## list->nb == 1)                        \
336                 continue;                                              \
337             fmts = out_link->in_ ## list;                              \
338                                                                        \
339             if (!out_link->in_ ## list->nb) {                          \
340                 add_format(&out_link->in_ ##list, fmt);                \
341                 break;                                                 \
342             }                                                          \
343                                                                        \
344             for (k = 0; k < out_link->in_ ## list->nb; k++)            \
345                 if (fmts->var[k] == fmt) {                             \
346                     fmts->var[0]  = fmt;                               \
347                     fmts->nb = 1;                                      \
348                     ret = 1;                                           \
349                     break;                                             \
350                 }                                                      \
351         }                                                              \
352     }                                                                  \
353 } while (0)
354
355 static int reduce_formats_on_filter(AVFilterContext *filter)
356 {
357     int i, j, k, ret = 0;
358
359     REDUCE_FORMATS(int,      AVFilterFormats,        formats,         formats,
360                    format_count, ff_add_format);
361     REDUCE_FORMATS(int,      AVFilterFormats,        samplerates,     formats,
362                    format_count, ff_add_format);
363     REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
364                    channel_layouts, nb_channel_layouts, ff_add_channel_layout);
365
366     return ret;
367 }
368
369 static void reduce_formats(AVFilterGraph *graph)
370 {
371     int i, reduced;
372
373     do {
374         reduced = 0;
375
376         for (i = 0; i < graph->filter_count; i++)
377             reduced |= reduce_formats_on_filter(graph->filters[i]);
378     } while (reduced);
379 }
380
381 static void swap_samplerates_on_filter(AVFilterContext *filter)
382 {
383     AVFilterLink *link = NULL;
384     int sample_rate;
385     int i, j;
386
387     for (i = 0; i < filter->nb_inputs; i++) {
388         link = filter->inputs[i];
389
390         if (link->type == AVMEDIA_TYPE_AUDIO &&
391             link->out_samplerates->format_count == 1)
392             break;
393     }
394     if (i == filter->nb_inputs)
395         return;
396
397     sample_rate = link->out_samplerates->formats[0];
398
399     for (i = 0; i < filter->nb_outputs; i++) {
400         AVFilterLink *outlink = filter->outputs[i];
401         int best_idx, best_diff = INT_MAX;
402
403         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
404             outlink->in_samplerates->format_count < 2)
405             continue;
406
407         for (j = 0; j < outlink->in_samplerates->format_count; j++) {
408             int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
409
410             if (diff < best_diff) {
411                 best_diff = diff;
412                 best_idx  = j;
413             }
414         }
415         FFSWAP(int, outlink->in_samplerates->formats[0],
416                outlink->in_samplerates->formats[best_idx]);
417     }
418 }
419
420 static void swap_samplerates(AVFilterGraph *graph)
421 {
422     int i;
423
424     for (i = 0; i < graph->filter_count; i++)
425         swap_samplerates_on_filter(graph->filters[i]);
426 }
427
428 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
429 {
430     AVFilterLink *link = NULL;
431     uint64_t chlayout;
432     int i, j;
433
434     for (i = 0; i < filter->nb_inputs; i++) {
435         link = filter->inputs[i];
436
437         if (link->type == AVMEDIA_TYPE_AUDIO &&
438             link->out_channel_layouts->nb_channel_layouts == 1)
439             break;
440     }
441     if (i == filter->nb_inputs)
442         return;
443
444     chlayout = link->out_channel_layouts->channel_layouts[0];
445
446     for (i = 0; i < filter->nb_outputs; i++) {
447         AVFilterLink *outlink = filter->outputs[i];
448         int best_idx, best_score = INT_MIN;
449
450         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
451             outlink->in_channel_layouts->nb_channel_layouts < 2)
452             continue;
453
454         for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
455             uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
456             int matched_channels  = av_get_channel_layout_nb_channels(chlayout &
457                                                                       out_chlayout);
458             int extra_channels     = av_get_channel_layout_nb_channels(out_chlayout &
459                                                                        (~chlayout));
460             int score = matched_channels - extra_channels;
461
462             if (score > best_score) {
463                 best_score = score;
464                 best_idx   = j;
465             }
466         }
467         FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
468                outlink->in_channel_layouts->channel_layouts[best_idx]);
469     }
470
471 }
472
473 static void swap_channel_layouts(AVFilterGraph *graph)
474 {
475     int i;
476
477     for (i = 0; i < graph->filter_count; i++)
478         swap_channel_layouts_on_filter(graph->filters[i]);
479 }
480
481 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
482 {
483     AVFilterLink *link = NULL;
484     int format, bps;
485     int i, j;
486
487     for (i = 0; i < filter->nb_inputs; i++) {
488         link = filter->inputs[i];
489
490         if (link->type == AVMEDIA_TYPE_AUDIO &&
491             link->out_formats->format_count == 1)
492             break;
493     }
494     if (i == filter->nb_inputs)
495         return;
496
497     format = link->out_formats->formats[0];
498     bps    = av_get_bytes_per_sample(format);
499
500     for (i = 0; i < filter->nb_outputs; i++) {
501         AVFilterLink *outlink = filter->outputs[i];
502         int best_idx, best_score = INT_MIN;
503
504         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
505             outlink->in_formats->format_count < 2)
506             continue;
507
508         for (j = 0; j < outlink->in_formats->format_count; j++) {
509             int out_format = outlink->in_formats->formats[j];
510             int out_bps    = av_get_bytes_per_sample(out_format);
511             int score;
512
513             if (av_get_packed_sample_fmt(out_format) == format ||
514                 av_get_planar_sample_fmt(out_format) == format) {
515                 best_idx   = j;
516                 break;
517             }
518
519             /* for s32 and float prefer double to prevent loss of information */
520             if (bps == 4 && out_bps == 8) {
521                 best_idx = j;
522                 break;
523             }
524
525             /* prefer closest higher or equal bps */
526             score = -abs(out_bps - bps);
527             if (out_bps >= bps)
528                 score += INT_MAX/2;
529
530             if (score > best_score) {
531                 best_score = score;
532                 best_idx   = j;
533             }
534         }
535         FFSWAP(int, outlink->in_formats->formats[0],
536                outlink->in_formats->formats[best_idx]);
537     }
538 }
539
540 static void swap_sample_fmts(AVFilterGraph *graph)
541 {
542     int i;
543
544     for (i = 0; i < graph->filter_count; i++)
545         swap_sample_fmts_on_filter(graph->filters[i]);
546
547 }
548
549 static int pick_formats(AVFilterGraph *graph)
550 {
551     int i, j, ret;
552
553     for (i = 0; i < graph->filter_count; i++) {
554         AVFilterContext *filter = graph->filters[i];
555
556         for (j = 0; j < filter->nb_inputs; j++)
557             if ((ret = pick_format(filter->inputs[j])) < 0)
558                 return ret;
559         for (j = 0; j < filter->nb_outputs; j++)
560             if ((ret = pick_format(filter->outputs[j])) < 0)
561                 return ret;
562     }
563     return 0;
564 }
565
566 /**
567  * Configure the formats of all the links in the graph.
568  */
569 static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
570 {
571     int ret;
572
573     /* find supported formats from sub-filters, and merge along links */
574     if ((ret = query_formats(graph, log_ctx)) < 0)
575         return ret;
576
577     /* Once everything is merged, it's possible that we'll still have
578      * multiple valid media format choices. We try to minimize the amount
579      * of format conversion inside filters */
580     reduce_formats(graph);
581
582     /* for audio filters, ensure the best format, sample rate and channel layout
583      * is selected */
584     swap_sample_fmts(graph);
585     swap_samplerates(graph);
586     swap_channel_layouts(graph);
587
588     if ((ret = pick_formats(graph)) < 0)
589         return ret;
590
591     return 0;
592 }
593
594 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
595 {
596     int ret;
597
598     if ((ret = graph_check_validity(graphctx, log_ctx)))
599         return ret;
600     if ((ret = graph_config_formats(graphctx, log_ctx)))
601         return ret;
602     if ((ret = graph_config_links(graphctx, log_ctx)))
603         return ret;
604
605     return 0;
606 }