lavfi: allow building without swscale.
[libav.git] / libavfilter / avfiltergraph.c
CommitLineData
eccb6fb7 1/*
ec8398a3 2 * filter graphs
3fa77bde
SS
3 * Copyright (c) 2008 Vitor Sessak
4 * Copyright (c) 2007 Bobby Bingham
eccb6fb7 5 *
2912e87a 6 * This file is part of Libav.
eccb6fb7 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
eccb6fb7
VS
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 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
eccb6fb7
VS
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
2912e87a 19 * License along with Libav; if not, write to the Free Software
eccb6fb7
VS
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
5c2ff9fd
VS
23#include <ctype.h>
24#include <string.h>
25
eccb6fb7
VS
26#include "avfilter.h"
27#include "avfiltergraph.h"
ff1f51a8 28#include "formats.h"
4fcbb2af 29#include "internal.h"
eccb6fb7 30
ff1f51a8 31#include "libavutil/audioconvert.h"
996f9f0c
AK
32#include "libavutil/log.h"
33
34static const AVClass filtergraph_class = {
35 .class_name = "AVFilterGraph",
36 .item_name = av_default_item_name,
37 .version = LIBAVUTIL_VERSION_INT,
38};
39
e15aeea6
SS
40AVFilterGraph *avfilter_graph_alloc(void)
41{
996f9f0c
AK
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;
e15aeea6
SS
49}
50
4359288c 51void avfilter_graph_free(AVFilterGraph **graph)
f2ee8083 52{
4359288c 53 if (!*graph)
caeb0c30 54 return;
4359288c
SS
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);
ffcaef34
VS
60}
61
4e4a3e23 62int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
ffcaef34 63{
3748b2b8
SS
64 AVFilterContext **filters = av_realloc(graph->filters,
65 sizeof(AVFilterContext*) * (graph->filter_count+1));
66 if (!filters)
22b7b23c 67 return AVERROR(ENOMEM);
4e4a3e23 68
3748b2b8
SS
69 graph->filters = filters;
70 graph->filters[graph->filter_count++] = filter;
4e4a3e23
VS
71
72 return 0;
eccb6fb7
VS
73}
74
037be76e
SS
75int 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
89fail:
90 if (*filt_ctx)
91 avfilter_free(*filt_ctx);
92 *filt_ctx = NULL;
93 return ret;
94}
95
8eb9bf09
AK
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 */
104static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
822005ed
SS
105{
106 AVFilterContext *filt;
107 int i, j;
108
b59dd1ea 109 for (i = 0; i < graph->filter_count; i++) {
822005ed
SS
110 filt = graph->filters[i];
111
9baeff95 112 for (j = 0; j < filt->nb_inputs; j++) {
822005ed
SS
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);
59cef18c 117 return AVERROR(EINVAL);
822005ed
SS
118 }
119 }
120
9baeff95 121 for (j = 0; j < filt->nb_outputs; j++) {
822005ed
SS
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);
59cef18c 126 return AVERROR(EINVAL);
822005ed
SS
127 }
128 }
129 }
130
131 return 0;
132}
133
8eb9bf09
AK
134/**
135 * Configure all the links of graphctx.
136 *
137 * @return 0 in case of success, a negative value otherwise
138 */
139static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
4ec42240
SS
140{
141 AVFilterContext *filt;
142 int i, ret;
143
144 for (i=0; i < graph->filter_count; i++) {
145 filt = graph->filters[i];
146
9baeff95 147 if (!filt->nb_outputs) {
4ec42240
SS
148 if ((ret = avfilter_config_links(filt)))
149 return ret;
150 }
151 }
152
153 return 0;
154}
155
9f39bb37 156AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
57d4a157 157{
57d4a157
VS
158 int i;
159
b59dd1ea
SS
160 for (i = 0; i < graph->filter_count; i++)
161 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
57d4a157
VS
162 return graph->filters[i];
163
164 return NULL;
165}
166
3fb8e77a 167static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
7b470878 168{
037be76e 169 int i, j, ret;
012f04a2 170 int scaler_count = 0, resampler_count = 0;
7b470878 171
bdab614b 172 /* ask all the sub-filters for their supported media formats */
b59dd1ea
SS
173 for (i = 0; i < graph->filter_count; i++) {
174 if (graph->filters[i]->filter->query_formats)
7b470878
VS
175 graph->filters[i]->filter->query_formats(graph->filters[i]);
176 else
43c7a01e 177 ff_default_query_formats(graph->filters[i]);
7b470878
VS
178 }
179
7b470878 180 /* go through and merge as many format lists as possible */
b59dd1ea 181 for (i = 0; i < graph->filter_count; i++) {
7b470878
VS
182 AVFilterContext *filter = graph->filters[i];
183
9baeff95 184 for (j = 0; j < filter->nb_inputs; j++) {
779cc07a 185 AVFilterLink *link = filter->inputs[j];
ff1f51a8
AK
186 int convert_needed = 0;
187
188 if (!link)
189 continue;
190
191 if (link->in_formats != link->out_formats &&
b74a1da4 192 !ff_merge_formats(link->in_formats,
ff1f51a8
AK
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) {
fd127ede
AK
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:
2f296e39
AK
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
fd127ede
AK
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);
2f296e39 225 if ((ret = avfilter_graph_create_filter(&convert, filter,
fd127ede
AK
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");
012f04a2
AK
234 return AVERROR(EINVAL);
235 }
236
fd127ede
AK
237 snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
238 resampler_count++);
239 if ((ret = avfilter_graph_create_filter(&convert,
240 avfilter_get_by_name("resample"),
241 inst_name, NULL, NULL, graph)) < 0)
ff1f51a8 242 return ret;
fd127ede
AK
243 break;
244 default:
245 return AVERROR(EINVAL);
246 }
247
248 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
249 return ret;
250
251 convert->filter->query_formats(convert);
252 inlink = convert->inputs[0];
253 outlink = convert->outputs[0];
b74a1da4
AK
254 if (!ff_merge_formats( inlink->in_formats, inlink->out_formats) ||
255 !ff_merge_formats(outlink->in_formats, outlink->out_formats))
fd127ede
AK
256 ret |= AVERROR(ENOSYS);
257 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
258 (!ff_merge_samplerates(inlink->in_samplerates,
259 inlink->out_samplerates) ||
260 !ff_merge_channel_layouts(inlink->in_channel_layouts,
261 inlink->out_channel_layouts)))
262 ret |= AVERROR(ENOSYS);
263 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
264 (!ff_merge_samplerates(outlink->in_samplerates,
265 outlink->out_samplerates) ||
266 !ff_merge_channel_layouts(outlink->in_channel_layouts,
267 outlink->out_channel_layouts)))
268 ret |= AVERROR(ENOSYS);
269
270 if (ret < 0) {
271 av_log(log_ctx, AV_LOG_ERROR,
272 "Impossible to convert between the formats supported by the filter "
273 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
274 return ret;
275 }
7b470878
VS
276 }
277 }
278 }
279
280 return 0;
281}
282
ff1f51a8 283static int pick_format(AVFilterLink *link)
7b470878 284{
b59dd1ea 285 if (!link || !link->in_formats)
ff1f51a8 286 return 0;
7b470878
VS
287
288 link->in_formats->format_count = 1;
289 link->format = link->in_formats->formats[0];
290
ff1f51a8
AK
291 if (link->type == AVMEDIA_TYPE_AUDIO) {
292 if (!link->in_samplerates->format_count) {
293 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
294 " the link between filters %s and %s.\n", link->src->name,
295 link->dst->name);
296 return AVERROR(EINVAL);
297 }
298 link->in_samplerates->format_count = 1;
299 link->sample_rate = link->in_samplerates->formats[0];
300
301 if (!link->in_channel_layouts->nb_channel_layouts) {
302 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
303 "the link between filters %s and %s.\n", link->src->name,
304 link->dst->name);
305 return AVERROR(EINVAL);
306 }
307 link->in_channel_layouts->nb_channel_layouts = 1;
308 link->channel_layout = link->in_channel_layouts->channel_layouts[0];
309 }
310
b74a1da4
AK
311 ff_formats_unref(&link->in_formats);
312 ff_formats_unref(&link->out_formats);
313 ff_formats_unref(&link->in_samplerates);
314 ff_formats_unref(&link->out_samplerates);
ff1f51a8
AK
315 ff_channel_layouts_unref(&link->in_channel_layouts);
316 ff_channel_layouts_unref(&link->out_channel_layouts);
317
318 return 0;
7b470878
VS
319}
320
ff1f51a8
AK
321#define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
322do { \
9baeff95 323 for (i = 0; i < filter->nb_inputs; i++) { \
ff1f51a8
AK
324 AVFilterLink *link = filter->inputs[i]; \
325 fmt_type fmt; \
326 \
327 if (!link->out_ ## list || link->out_ ## list->nb != 1) \
328 continue; \
329 fmt = link->out_ ## list->var[0]; \
330 \
9baeff95 331 for (j = 0; j < filter->nb_outputs; j++) { \
ff1f51a8
AK
332 AVFilterLink *out_link = filter->outputs[j]; \
333 list_type *fmts; \
334 \
335 if (link->type != out_link->type || \
336 out_link->in_ ## list->nb == 1) \
337 continue; \
338 fmts = out_link->in_ ## list; \
339 \
340 if (!out_link->in_ ## list->nb) { \
341 add_format(&out_link->in_ ##list, fmt); \
342 break; \
343 } \
344 \
345 for (k = 0; k < out_link->in_ ## list->nb; k++) \
346 if (fmts->var[k] == fmt) { \
347 fmts->var[0] = fmt; \
348 fmts->nb = 1; \
349 ret = 1; \
350 break; \
351 } \
352 } \
353 } \
354} while (0)
355
63736fe4
AK
356static int reduce_formats_on_filter(AVFilterContext *filter)
357{
358 int i, j, k, ret = 0;
359
ff1f51a8 360 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
b74a1da4 361 format_count, ff_add_format);
ff1f51a8 362 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
b74a1da4 363 format_count, ff_add_format);
ff1f51a8
AK
364 REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
365 channel_layouts, nb_channel_layouts, ff_add_channel_layout);
63736fe4 366
63736fe4
AK
367 return ret;
368}
369
370static void reduce_formats(AVFilterGraph *graph)
371{
372 int i, reduced;
373
374 do {
375 reduced = 0;
376
377 for (i = 0; i < graph->filter_count; i++)
378 reduced |= reduce_formats_on_filter(graph->filters[i]);
379 } while (reduced);
380}
381
ff1f51a8
AK
382static void swap_samplerates_on_filter(AVFilterContext *filter)
383{
384 AVFilterLink *link = NULL;
385 int sample_rate;
386 int i, j;
387
9baeff95 388 for (i = 0; i < filter->nb_inputs; i++) {
ff1f51a8
AK
389 link = filter->inputs[i];
390
391 if (link->type == AVMEDIA_TYPE_AUDIO &&
392 link->out_samplerates->format_count == 1)
393 break;
394 }
9baeff95 395 if (i == filter->nb_inputs)
ff1f51a8
AK
396 return;
397
398 sample_rate = link->out_samplerates->formats[0];
399
9baeff95 400 for (i = 0; i < filter->nb_outputs; i++) {
ff1f51a8
AK
401 AVFilterLink *outlink = filter->outputs[i];
402 int best_idx, best_diff = INT_MAX;
403
404 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
405 outlink->in_samplerates->format_count < 2)
406 continue;
407
408 for (j = 0; j < outlink->in_samplerates->format_count; j++) {
409 int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
410
411 if (diff < best_diff) {
412 best_diff = diff;
413 best_idx = j;
414 }
415 }
416 FFSWAP(int, outlink->in_samplerates->formats[0],
417 outlink->in_samplerates->formats[best_idx]);
418 }
419}
420
421static void swap_samplerates(AVFilterGraph *graph)
422{
423 int i;
424
425 for (i = 0; i < graph->filter_count; i++)
426 swap_samplerates_on_filter(graph->filters[i]);
427}
428
429static void swap_channel_layouts_on_filter(AVFilterContext *filter)
7b470878 430{
ff1f51a8
AK
431 AVFilterLink *link = NULL;
432 uint64_t chlayout;
7b470878
VS
433 int i, j;
434
9baeff95 435 for (i = 0; i < filter->nb_inputs; i++) {
ff1f51a8
AK
436 link = filter->inputs[i];
437
438 if (link->type == AVMEDIA_TYPE_AUDIO &&
439 link->out_channel_layouts->nb_channel_layouts == 1)
440 break;
441 }
9baeff95 442 if (i == filter->nb_inputs)
ff1f51a8
AK
443 return;
444
445 chlayout = link->out_channel_layouts->channel_layouts[0];
446
9baeff95 447 for (i = 0; i < filter->nb_outputs; i++) {
ff1f51a8
AK
448 AVFilterLink *outlink = filter->outputs[i];
449 int best_idx, best_score = INT_MIN;
450
451 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
452 outlink->in_channel_layouts->nb_channel_layouts < 2)
453 continue;
454
455 for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
456 uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
457 int matched_channels = av_get_channel_layout_nb_channels(chlayout &
458 out_chlayout);
459 int extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
460 (~chlayout));
461 int score = matched_channels - extra_channels;
462
463 if (score > best_score) {
464 best_score = score;
465 best_idx = j;
466 }
467 }
468 FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
469 outlink->in_channel_layouts->channel_layouts[best_idx]);
470 }
471
472}
473
474static void swap_channel_layouts(AVFilterGraph *graph)
475{
476 int i;
477
478 for (i = 0; i < graph->filter_count; i++)
479 swap_channel_layouts_on_filter(graph->filters[i]);
480}
481
ccefa89f
AK
482static void swap_sample_fmts_on_filter(AVFilterContext *filter)
483{
484 AVFilterLink *link = NULL;
485 int format, bps;
486 int i, j;
487
9baeff95 488 for (i = 0; i < filter->nb_inputs; i++) {
ccefa89f
AK
489 link = filter->inputs[i];
490
491 if (link->type == AVMEDIA_TYPE_AUDIO &&
492 link->out_formats->format_count == 1)
493 break;
494 }
9baeff95 495 if (i == filter->nb_inputs)
ccefa89f
AK
496 return;
497
498 format = link->out_formats->formats[0];
499 bps = av_get_bytes_per_sample(format);
500
9baeff95 501 for (i = 0; i < filter->nb_outputs; i++) {
ccefa89f
AK
502 AVFilterLink *outlink = filter->outputs[i];
503 int best_idx, best_score = INT_MIN;
504
505 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
506 outlink->in_formats->format_count < 2)
507 continue;
508
509 for (j = 0; j < outlink->in_formats->format_count; j++) {
510 int out_format = outlink->in_formats->formats[j];
511 int out_bps = av_get_bytes_per_sample(out_format);
512 int score;
513
514 if (av_get_packed_sample_fmt(out_format) == format ||
515 av_get_planar_sample_fmt(out_format) == format) {
516 best_idx = j;
517 break;
518 }
519
520 /* for s32 and float prefer double to prevent loss of information */
521 if (bps == 4 && out_bps == 8) {
522 best_idx = j;
523 break;
524 }
525
526 /* prefer closest higher or equal bps */
527 score = -abs(out_bps - bps);
528 if (out_bps >= bps)
529 score += INT_MAX/2;
530
531 if (score > best_score) {
532 best_score = score;
533 best_idx = j;
534 }
535 }
536 FFSWAP(int, outlink->in_formats->formats[0],
537 outlink->in_formats->formats[best_idx]);
538 }
539}
540
541static void swap_sample_fmts(AVFilterGraph *graph)
542{
543 int i;
544
545 for (i = 0; i < graph->filter_count; i++)
546 swap_sample_fmts_on_filter(graph->filters[i]);
547
548}
549
ff1f51a8
AK
550static int pick_formats(AVFilterGraph *graph)
551{
552 int i, j, ret;
553
b59dd1ea 554 for (i = 0; i < graph->filter_count; i++) {
7b470878
VS
555 AVFilterContext *filter = graph->filters[i];
556
9baeff95 557 for (j = 0; j < filter->nb_inputs; j++)
ff1f51a8
AK
558 if ((ret = pick_format(filter->inputs[j])) < 0)
559 return ret;
9baeff95 560 for (j = 0; j < filter->nb_outputs; j++)
ff1f51a8
AK
561 if ((ret = pick_format(filter->outputs[j])) < 0)
562 return ret;
7b470878 563 }
ff1f51a8 564 return 0;
7b470878
VS
565}
566
8eb9bf09
AK
567/**
568 * Configure the formats of all the links in the graph.
569 */
570static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
7b470878 571{
59cef18c
SS
572 int ret;
573
ec8398a3 574 /* find supported formats from sub-filters, and merge along links */
59cef18c
SS
575 if ((ret = query_formats(graph, log_ctx)) < 0)
576 return ret;
7b470878
VS
577
578 /* Once everything is merged, it's possible that we'll still have
63736fe4
AK
579 * multiple valid media format choices. We try to minimize the amount
580 * of format conversion inside filters */
581 reduce_formats(graph);
582
ccefa89f 583 /* for audio filters, ensure the best format, sample rate and channel layout
ff1f51a8 584 * is selected */
ccefa89f 585 swap_sample_fmts(graph);
ff1f51a8
AK
586 swap_samplerates(graph);
587 swap_channel_layouts(graph);
588
589 if ((ret = pick_formats(graph)) < 0)
590 return ret;
7b470878
VS
591
592 return 0;
593}
594
57fa3140 595int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
2a24df93
SS
596{
597 int ret;
598
8eb9bf09 599 if ((ret = graph_check_validity(graphctx, log_ctx)))
2a24df93 600 return ret;
8eb9bf09 601 if ((ret = graph_config_formats(graphctx, log_ctx)))
2a24df93 602 return ret;
8eb9bf09 603 if ((ret = graph_config_links(graphctx, log_ctx)))
2a24df93
SS
604 return ret;
605
606 return 0;
607}