configure: When disabling a library disable all the related components
[libav.git] / avconv_filter.c
CommitLineData
fe2147e9
AK
1/*
2 * avconv filter configuration
3 *
4 * This file is part of Libav.
5 *
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
8f8bc923
DB
21#include <stdint.h>
22
fe2147e9
AK
23#include "avconv.h"
24
25#include "libavfilter/avfilter.h"
fe2147e9 26
5c7db097
JR
27#include "libavresample/avresample.h"
28
fe2147e9 29#include "libavutil/avassert.h"
5c7db097 30#include "libavutil/avstring.h"
a903f8f0 31#include "libavutil/channel_layout.h"
16302246 32#include "libavutil/display.h"
5c7db097 33#include "libavutil/opt.h"
fe2147e9
AK
34#include "libavutil/pixdesc.h"
35#include "libavutil/pixfmt.h"
36#include "libavutil/samplefmt.h"
37
c1ef30a6
DB
38/* Define a function for building a string containing a list of
39 * allowed formats. */
bee2d75b 40#define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name) \
fe2147e9
AK
41static char *choose_ ## var ## s(OutputStream *ost) \
42{ \
38313626
AK
43 if (ost->enc_ctx->var != none) { \
44 get_name(ost->enc_ctx->var); \
fe2147e9 45 return av_strdup(name); \
e760e1d4 46 } else if (ost->enc && ost->enc->supported_list) { \
fe2147e9
AK
47 const type *p; \
48 AVIOContext *s = NULL; \
49 uint8_t *ret; \
50 int len; \
51 \
52 if (avio_open_dyn_buf(&s) < 0) \
5e3f9979 53 exit(1); \
fe2147e9
AK
54 \
55 for (p = ost->enc->supported_list; *p != none; p++) { \
56 get_name(*p); \
bee2d75b 57 avio_printf(s, "%s|", name); \
fe2147e9
AK
58 } \
59 len = avio_close_dyn_buf(s, &ret); \
60 ret[len - 1] = 0; \
61 return ret; \
62 } else \
63 return NULL; \
64}
65
716d413c 66DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
bee2d75b 67 GET_PIX_FMT_NAME)
fe2147e9 68
fe2147e9 69DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
bee2d75b 70 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME)
fe2147e9 71
fe2147e9 72DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
bee2d75b 73 GET_SAMPLE_RATE_NAME)
fe2147e9 74
fe2147e9 75DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
bee2d75b 76 GET_CH_LAYOUT_NAME)
fe2147e9
AK
77
78FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
79{
80 FilterGraph *fg = av_mallocz(sizeof(*fg));
81
82 if (!fg)
5e3f9979 83 exit(1);
fe2147e9
AK
84 fg->index = nb_filtergraphs;
85
10bca661 86 GROW_ARRAY(fg->outputs, fg->nb_outputs);
fe2147e9 87 if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
5e3f9979 88 exit(1);
fe2147e9
AK
89 fg->outputs[0]->ost = ost;
90 fg->outputs[0]->graph = fg;
91
92 ost->filter = fg->outputs[0];
93
10bca661 94 GROW_ARRAY(fg->inputs, fg->nb_inputs);
fe2147e9 95 if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
5e3f9979 96 exit(1);
fe2147e9
AK
97 fg->inputs[0]->ist = ist;
98 fg->inputs[0]->graph = fg;
99
10bca661 100 GROW_ARRAY(ist->filters, ist->nb_filters);
fe2147e9
AK
101 ist->filters[ist->nb_filters - 1] = fg->inputs[0];
102
10bca661 103 GROW_ARRAY(filtergraphs, nb_filtergraphs);
fe2147e9
AK
104 filtergraphs[nb_filtergraphs - 1] = fg;
105
106 return fg;
107}
108
109static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
110{
111 InputStream *ist = NULL;
112 enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
113 int i;
114
115 // TODO: support other filter types
116 if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
117 av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
118 "currently.\n");
5e3f9979 119 exit(1);
fe2147e9
AK
120 }
121
122 if (in->name) {
123 AVFormatContext *s;
124 AVStream *st = NULL;
125 char *p;
126 int file_idx = strtol(in->name, &p, 0);
127
128 if (file_idx < 0 || file_idx >= nb_input_files) {
129 av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtegraph description %s.\n",
130 file_idx, fg->graph_desc);
5e3f9979 131 exit(1);
fe2147e9
AK
132 }
133 s = input_files[file_idx]->ctx;
134
135 for (i = 0; i < s->nb_streams; i++) {
136 if (s->streams[i]->codec->codec_type != type)
137 continue;
138 if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
139 st = s->streams[i];
140 break;
141 }
142 }
143 if (!st) {
144 av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
145 "matches no streams.\n", p, fg->graph_desc);
5e3f9979 146 exit(1);
fe2147e9
AK
147 }
148 ist = input_streams[input_files[file_idx]->ist_index + st->index];
149 } else {
150 /* find the first unused stream of corresponding type */
151 for (i = 0; i < nb_input_streams; i++) {
152 ist = input_streams[i];
41776ba9 153 if (ist->dec_ctx->codec_type == type && ist->discard)
fe2147e9
AK
154 break;
155 }
156 if (i == nb_input_streams) {
157 av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
d28fc7b2 158 "unlabeled input pad %d on filter %s\n", in->pad_idx,
fe2147e9 159 in->filter_ctx->name);
5e3f9979 160 exit(1);
fe2147e9
AK
161 }
162 }
163 av_assert0(ist);
164
165 ist->discard = 0;
166 ist->decoding_needed = 1;
167 ist->st->discard = AVDISCARD_NONE;
168
10bca661 169 GROW_ARRAY(fg->inputs, fg->nb_inputs);
fe2147e9 170 if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
5e3f9979 171 exit(1);
fe2147e9
AK
172 fg->inputs[fg->nb_inputs - 1]->ist = ist;
173 fg->inputs[fg->nb_inputs - 1]->graph = fg;
174
10bca661 175 GROW_ARRAY(ist->filters, ist->nb_filters);
fe2147e9
AK
176 ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
177}
178
6d592fbd
AK
179int init_complex_filtergraph(FilterGraph *fg)
180{
181 AVFilterInOut *inputs, *outputs, *cur;
182 AVFilterGraph *graph;
183 int ret = 0;
184
185 /* this graph is only used for determining the kinds of inputs
186 * and outputs we have, and is discarded on exit from this function */
187 graph = avfilter_graph_alloc();
188 if (!graph)
189 return AVERROR(ENOMEM);
190
191 ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs);
192 if (ret < 0)
193 goto fail;
194
195 for (cur = inputs; cur; cur = cur->next)
196 init_input_filter(fg, cur);
197
198 for (cur = outputs; cur;) {
199 GROW_ARRAY(fg->outputs, fg->nb_outputs);
200 fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]));
201 if (!fg->outputs[fg->nb_outputs - 1])
202 exit(1);
203
204 fg->outputs[fg->nb_outputs - 1]->graph = fg;
205 fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
206 fg->outputs[fg->nb_outputs - 1]->type = avfilter_pad_get_type(cur->filter_ctx->output_pads,
207 cur->pad_idx);
208 cur = cur->next;
209 fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
210 }
211
212fail:
213 avfilter_inout_free(&inputs);
214 avfilter_graph_free(&graph);
215 return ret;
216}
217
811bd078
AK
218static int insert_trim(int64_t start_time, int64_t duration,
219 AVFilterContext **last_filter, int *pad_idx,
220 const char *filter_name)
a83c0da5 221{
a83c0da5
AK
222 AVFilterGraph *graph = (*last_filter)->graph;
223 AVFilterContext *ctx;
224 const AVFilter *trim;
811bd078
AK
225 enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
226 const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
a83c0da5
AK
227 int ret = 0;
228
811bd078 229 if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
a83c0da5
AK
230 return 0;
231
232 trim = avfilter_get_by_name(name);
233 if (!trim) {
234 av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
235 "recording time.\n", name);
236 return AVERROR_FILTER_NOT_FOUND;
237 }
238
a83c0da5
AK
239 ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
240 if (!ctx)
241 return AVERROR(ENOMEM);
242
811bd078
AK
243 if (duration != INT64_MAX) {
244 ret = av_opt_set_double(ctx, "duration", (double)duration / 1e6,
8cd472d3
AK
245 AV_OPT_SEARCH_CHILDREN);
246 }
811bd078
AK
247 if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
248 ret = av_opt_set_double(ctx, "start", (double)start_time / 1e6,
8cd472d3
AK
249 AV_OPT_SEARCH_CHILDREN);
250 }
a83c0da5
AK
251 if (ret < 0) {
252 av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
253 return ret;
254 }
255
256 ret = avfilter_init_str(ctx, NULL);
257 if (ret < 0)
258 return ret;
259
260 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
261 if (ret < 0)
262 return ret;
263
264 *last_filter = ctx;
265 *pad_idx = 0;
266 return 0;
267}
268
16302246
MS
269static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
270 const char *filter_name, const char *args)
271{
272 AVFilterGraph *graph = (*last_filter)->graph;
273 AVFilterContext *ctx;
274 int ret;
275
276 ret = avfilter_graph_create_filter(&ctx,
277 avfilter_get_by_name(filter_name),
278 filter_name, args, NULL, graph);
279 if (ret < 0)
280 return ret;
281
282 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
283 if (ret < 0)
284 return ret;
285
286 *last_filter = ctx;
287 *pad_idx = 0;
288 return 0;
289}
290
fe2147e9
AK
291static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
292{
293 char *pix_fmts;
294 OutputStream *ost = ofilter->ost;
811bd078 295 OutputFile *of = output_files[ost->file_index];
38313626 296 AVCodecContext *codec = ost->enc_ctx;
fe2147e9
AK
297 AVFilterContext *last_filter = out->filter_ctx;
298 int pad_idx = out->pad_idx;
299 int ret;
300 char name[255];
301
302 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
303 ret = avfilter_graph_create_filter(&ofilter->filter,
304 avfilter_get_by_name("buffersink"),
58dee6e6 305 name, NULL, NULL, fg->graph);
fe2147e9
AK
306 if (ret < 0)
307 return ret;
308
309 if (codec->width || codec->height) {
310 char args[255];
311 AVFilterContext *filter;
312
72fbc968 313 snprintf(args, sizeof(args), "%d:%d:0x%X",
fe2147e9
AK
314 codec->width,
315 codec->height,
316 (unsigned)ost->sws_flags);
317 snprintf(name, sizeof(name), "scaler for output stream %d:%d",
318 ost->file_index, ost->index);
319 if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
320 name, args, NULL, fg->graph)) < 0)
321 return ret;
322 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
323 return ret;
324
325 last_filter = filter;
326 pad_idx = 0;
327 }
328
329 if ((pix_fmts = choose_pix_fmts(ost))) {
330 AVFilterContext *filter;
331 snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
332 ost->file_index, ost->index);
c802a2e7
VG
333 ret = avfilter_graph_create_filter(&filter,
334 avfilter_get_by_name("format"),
335 "format", pix_fmts, NULL, fg->graph);
336 av_freep(&pix_fmts);
337 if (ret < 0)
fe2147e9
AK
338 return ret;
339 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
340 return ret;
341
342 last_filter = filter;
343 pad_idx = 0;
fe2147e9
AK
344 }
345
346 if (ost->frame_rate.num) {
347 AVFilterContext *fps;
348 char args[255];
349
350 snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
351 ost->frame_rate.den);
352 snprintf(name, sizeof(name), "fps for output stream %d:%d",
353 ost->file_index, ost->index);
354 ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
355 name, args, NULL, fg->graph);
356 if (ret < 0)
357 return ret;
358
359 ret = avfilter_link(last_filter, pad_idx, fps, 0);
360 if (ret < 0)
361 return ret;
362 last_filter = fps;
363 pad_idx = 0;
364 }
365
811bd078
AK
366 snprintf(name, sizeof(name), "trim for output stream %d:%d",
367 ost->file_index, ost->index);
368 ret = insert_trim(of->start_time, of->recording_time,
369 &last_filter, &pad_idx, name);
a83c0da5
AK
370 if (ret < 0)
371 return ret;
372
373
fe2147e9
AK
374 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
375 return ret;
376
377 return 0;
378}
379
380static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
381{
382 OutputStream *ost = ofilter->ost;
811bd078 383 OutputFile *of = output_files[ost->file_index];
38313626 384 AVCodecContext *codec = ost->enc_ctx;
fe2147e9
AK
385 AVFilterContext *last_filter = out->filter_ctx;
386 int pad_idx = out->pad_idx;
387 char *sample_fmts, *sample_rates, *channel_layouts;
388 char name[255];
389 int ret;
390
391
392 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
393 ret = avfilter_graph_create_filter(&ofilter->filter,
394 avfilter_get_by_name("abuffersink"),
395 name, NULL, NULL, fg->graph);
396 if (ret < 0)
397 return ret;
398
399 if (codec->channels && !codec->channel_layout)
400 codec->channel_layout = av_get_default_channel_layout(codec->channels);
401
402 sample_fmts = choose_sample_fmts(ost);
403 sample_rates = choose_sample_rates(ost);
404 channel_layouts = choose_channel_layouts(ost);
405 if (sample_fmts || sample_rates || channel_layouts) {
406 AVFilterContext *format;
407 char args[256];
408 int len = 0;
409
410 if (sample_fmts)
411 len += snprintf(args + len, sizeof(args) - len, "sample_fmts=%s:",
412 sample_fmts);
413 if (sample_rates)
414 len += snprintf(args + len, sizeof(args) - len, "sample_rates=%s:",
415 sample_rates);
416 if (channel_layouts)
417 len += snprintf(args + len, sizeof(args) - len, "channel_layouts=%s:",
418 channel_layouts);
419 args[len - 1] = 0;
420
421 av_freep(&sample_fmts);
422 av_freep(&sample_rates);
423 av_freep(&channel_layouts);
424
425 snprintf(name, sizeof(name), "audio format for output stream %d:%d",
426 ost->file_index, ost->index);
427 ret = avfilter_graph_create_filter(&format,
428 avfilter_get_by_name("aformat"),
429 name, args, NULL, fg->graph);
430 if (ret < 0)
431 return ret;
432
433 ret = avfilter_link(last_filter, pad_idx, format, 0);
434 if (ret < 0)
435 return ret;
436
437 last_filter = format;
438 pad_idx = 0;
439 }
440
811bd078
AK
441 snprintf(name, sizeof(name), "trim for output stream %d:%d",
442 ost->file_index, ost->index);
443 ret = insert_trim(of->start_time, of->recording_time,
444 &last_filter, &pad_idx, name);
a83c0da5
AK
445 if (ret < 0)
446 return ret;
447
fe2147e9
AK
448 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
449 return ret;
450
451 return 0;
452}
453
454#define DESCRIBE_FILTER_LINK(f, inout, in) \
455{ \
456 AVFilterContext *ctx = inout->filter_ctx; \
457 AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
06cd4c5a 458 int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs; \
fe2147e9
AK
459 AVIOContext *pb; \
460 \
461 if (avio_open_dyn_buf(&pb) < 0) \
5e3f9979 462 exit(1); \
fe2147e9
AK
463 \
464 avio_printf(pb, "%s", ctx->filter->name); \
465 if (nb_pads > 1) \
466 avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
467 avio_w8(pb, 0); \
468 avio_close_dyn_buf(pb, &f->name); \
469}
470
471int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
472{
473 av_freep(&ofilter->name);
474 DESCRIBE_FILTER_LINK(ofilter, out, 0);
475
476 switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
477 case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
478 case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
479 default: av_assert0(0);
480 }
481}
482
483static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
484 AVFilterInOut *in)
485{
2e661f26 486 AVFilterContext *last_filter;
51fc88e7 487 const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
fe2147e9 488 InputStream *ist = ifilter->ist;
811bd078 489 InputFile *f = input_files[ist->file_index];
fe2147e9
AK
490 AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
491 ist->st->time_base;
492 AVRational sar;
493 char args[255], name[255];
811bd078 494 int ret, pad_idx = 0;
fe2147e9
AK
495
496 sar = ist->st->sample_aspect_ratio.num ?
497 ist->st->sample_aspect_ratio :
41776ba9 498 ist->dec_ctx->sample_aspect_ratio;
18adc1fd
VG
499 snprintf(args, sizeof(args),
500 "width=%d:height=%d:pix_fmt=%d:time_base=%d/%d:sar=%d/%d",
501 ist->dec_ctx->width, ist->dec_ctx->height,
41776ba9 502 ist->hwaccel_retrieve_data ? ist->hwaccel_retrieved_pix_fmt : ist->dec_ctx->pix_fmt,
fe2147e9
AK
503 tb.num, tb.den, sar.num, sar.den);
504 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
505 ist->file_index, ist->st->index);
506
51fc88e7 507 if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name,
fe2147e9
AK
508 args, NULL, fg->graph)) < 0)
509 return ret;
2e661f26 510 last_filter = ifilter->filter;
fe2147e9 511
16302246
MS
512 if (ist->autorotate) {
513 uint8_t* displaymatrix = av_stream_get_side_data(ist->st,
514 AV_PKT_DATA_DISPLAYMATRIX, NULL);
515 if (displaymatrix) {
516 double rot = av_display_rotation_get((int32_t*) displaymatrix);
517 if (rot < -135 || rot > 135) {
518 ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
519 if (ret < 0)
520 return ret;
521 ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
522 } else if (rot < -45) {
523 ret = insert_filter(&last_filter, &pad_idx, "transpose", "dir=clock");
524 } else if (rot > 45) {
525 ret = insert_filter(&last_filter, &pad_idx, "transpose", "dir=cclock");
526 }
527 if (ret < 0)
528 return ret;
529 }
530 }
531
fe2147e9
AK
532 if (ist->framerate.num) {
533 AVFilterContext *setpts;
534
535 snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
536 ist->file_index, ist->st->index);
537 if ((ret = avfilter_graph_create_filter(&setpts,
538 avfilter_get_by_name("setpts"),
539 name, "N", NULL,
540 fg->graph)) < 0)
541 return ret;
542
2e661f26 543 if ((ret = avfilter_link(last_filter, 0, setpts, 0)) < 0)
fe2147e9
AK
544 return ret;
545
2e661f26 546 last_filter = setpts;
fe2147e9
AK
547 }
548
811bd078
AK
549 snprintf(name, sizeof(name), "trim for input stream %d:%d",
550 ist->file_index, ist->st->index);
551 ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
488a0fa6 552 AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
811bd078
AK
553 if (ret < 0)
554 return ret;
555
2e661f26 556 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
fe2147e9
AK
557 return ret;
558 return 0;
559}
560
561static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
562 AVFilterInOut *in)
563{
2e661f26 564 AVFilterContext *last_filter;
51fc88e7 565 const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
fe2147e9 566 InputStream *ist = ifilter->ist;
811bd078 567 InputFile *f = input_files[ist->file_index];
fe2147e9 568 char args[255], name[255];
811bd078 569 int ret, pad_idx = 0;
fe2147e9
AK
570
571 snprintf(args, sizeof(args), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s"
572 ":channel_layout=0x%"PRIx64,
41776ba9
AK
573 1, ist->dec_ctx->sample_rate,
574 ist->dec_ctx->sample_rate,
575 av_get_sample_fmt_name(ist->dec_ctx->sample_fmt),
576 ist->dec_ctx->channel_layout);
fe2147e9
AK
577 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
578 ist->file_index, ist->st->index);
579
51fc88e7 580 if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
fe2147e9
AK
581 name, args, NULL,
582 fg->graph)) < 0)
583 return ret;
2e661f26 584 last_filter = ifilter->filter;
fe2147e9
AK
585
586 if (audio_sync_method > 0) {
587 AVFilterContext *async;
fe2147e9
AK
588 int len = 0;
589
590 av_log(NULL, AV_LOG_WARNING, "-async has been deprecated. Used the "
591 "asyncts audio filter instead.\n");
592
593 if (audio_sync_method > 1)
594 len += snprintf(args + len, sizeof(args) - len, "compensate=1:"
595 "max_comp=%d:", audio_sync_method);
596 snprintf(args + len, sizeof(args) - len, "min_delta=%f",
597 audio_drift_threshold);
598
599 snprintf(name, sizeof(name), "graph %d audio sync for input stream %d:%d",
600 fg->index, ist->file_index, ist->st->index);
601 ret = avfilter_graph_create_filter(&async,
602 avfilter_get_by_name("asyncts"),
603 name, args, NULL, fg->graph);
604 if (ret < 0)
605 return ret;
606
2e661f26 607 ret = avfilter_link(last_filter, 0, async, 0);
fe2147e9
AK
608 if (ret < 0)
609 return ret;
610
2e661f26 611 last_filter = async;
fe2147e9 612 }
9a71d362
JR
613 if (audio_volume != 256) {
614 AVFilterContext *volume;
615
616 av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
617 "audio filter instead.\n");
618
619 snprintf(args, sizeof(args), "volume=%f", audio_volume / 256.0);
620
621 snprintf(name, sizeof(name), "graph %d volume for input stream %d:%d",
622 fg->index, ist->file_index, ist->st->index);
623 ret = avfilter_graph_create_filter(&volume,
624 avfilter_get_by_name("volume"),
625 name, args, NULL, fg->graph);
626 if (ret < 0)
627 return ret;
628
2e661f26 629 ret = avfilter_link(last_filter, 0, volume, 0);
9a71d362
JR
630 if (ret < 0)
631 return ret;
632
2e661f26 633 last_filter = volume;
9a71d362 634 }
811bd078
AK
635
636 snprintf(name, sizeof(name), "trim for input stream %d:%d",
637 ist->file_index, ist->st->index);
638 ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
488a0fa6 639 AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
811bd078
AK
640 if (ret < 0)
641 return ret;
642
2e661f26 643 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
fe2147e9
AK
644 return ret;
645
646 return 0;
647}
648
649static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
650 AVFilterInOut *in)
651{
652 av_freep(&ifilter->name);
653 DESCRIBE_FILTER_LINK(ifilter, in, 1);
654
655 switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
656 case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
657 case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
658 default: av_assert0(0);
659 }
660}
661
662int configure_filtergraph(FilterGraph *fg)
663{
664 AVFilterInOut *inputs, *outputs, *cur;
6d592fbd 665 int ret, i, simple = !fg->graph_desc;
fe2147e9
AK
666 const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
667 fg->graph_desc;
668
669 avfilter_graph_free(&fg->graph);
670 if (!(fg->graph = avfilter_graph_alloc()))
671 return AVERROR(ENOMEM);
672
673 if (simple) {
674 OutputStream *ost = fg->outputs[0]->ost;
5c7db097
JR
675 char args[512];
676 AVDictionaryEntry *e = NULL;
5c7db097 677
fe2147e9
AK
678 snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
679 fg->graph->scale_sws_opts = av_strdup(args);
5c7db097
JR
680
681 args[0] = '\0';
682 while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
683 AV_DICT_IGNORE_SUFFIX))) {
684 av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
685 }
686 if (strlen(args))
687 args[strlen(args) - 1] = '\0';
688 fg->graph->resample_lavr_opts = av_strdup(args);
fe2147e9
AK
689 }
690
691 if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
692 return ret;
693
694 if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
695 av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
696 "exactly one input and output.\n", graph_desc);
697 return AVERROR(EINVAL);
698 }
699
fe2147e9
AK
700 for (cur = inputs, i = 0; cur; cur = cur->next, i++)
701 if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
702 return ret;
703 avfilter_inout_free(&inputs);
704
6d592fbd
AK
705 for (cur = outputs, i = 0; cur; cur = cur->next, i++)
706 configure_output_filter(fg, fg->outputs[i], cur);
707 avfilter_inout_free(&outputs);
fe2147e9 708
6d592fbd
AK
709 if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
710 return ret;
fe2147e9
AK
711
712 return 0;
713}
714
715int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
716{
717 int i;
718 for (i = 0; i < fg->nb_inputs; i++)
719 if (fg->inputs[i]->ist == ist)
720 return 1;
721 return 0;
722}
723