examples/transcode_aac: Drop pointless return value const qualifier
[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"
1bf34134 26#include "libavfilter/buffersrc.h"
fe2147e9 27
5c7db097
JR
28#include "libavresample/avresample.h"
29
fe2147e9 30#include "libavutil/avassert.h"
5c7db097 31#include "libavutil/avstring.h"
a903f8f0 32#include "libavutil/channel_layout.h"
16302246 33#include "libavutil/display.h"
5c7db097 34#include "libavutil/opt.h"
fe2147e9
AK
35#include "libavutil/pixdesc.h"
36#include "libavutil/pixfmt.h"
37#include "libavutil/samplefmt.h"
38
c1ef30a6
DB
39/* Define a function for building a string containing a list of
40 * allowed formats. */
50722b4f
AK
41#define DEF_CHOOSE_FORMAT(suffix, type, var, supported_list, none, get_name) \
42static char *choose_ ## suffix (OutputFilter *ofilter) \
fe2147e9 43{ \
50722b4f
AK
44 if (ofilter->var != none) { \
45 get_name(ofilter->var); \
fe2147e9 46 return av_strdup(name); \
50722b4f 47 } else if (ofilter->supported_list) { \
fe2147e9
AK
48 const type *p; \
49 AVIOContext *s = NULL; \
50 uint8_t *ret; \
51 int len; \
52 \
53 if (avio_open_dyn_buf(&s) < 0) \
5e3f9979 54 exit(1); \
fe2147e9 55 \
50722b4f 56 for (p = ofilter->supported_list; *p != none; p++) { \
fe2147e9 57 get_name(*p); \
bee2d75b 58 avio_printf(s, "%s|", name); \
fe2147e9
AK
59 } \
60 len = avio_close_dyn_buf(s, &ret); \
61 ret[len - 1] = 0; \
62 return ret; \
63 } else \
64 return NULL; \
65}
66
50722b4f 67DEF_CHOOSE_FORMAT(pix_fmts, enum AVPixelFormat, format, formats, AV_PIX_FMT_NONE,
bee2d75b 68 GET_PIX_FMT_NAME)
fe2147e9 69
50722b4f 70DEF_CHOOSE_FORMAT(sample_fmts, enum AVSampleFormat, format, formats,
bee2d75b 71 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME)
fe2147e9 72
50722b4f 73DEF_CHOOSE_FORMAT(sample_rates, int, sample_rate, sample_rates, 0,
bee2d75b 74 GET_SAMPLE_RATE_NAME)
fe2147e9 75
50722b4f 76DEF_CHOOSE_FORMAT(channel_layouts, uint64_t, channel_layout, channel_layouts, 0,
bee2d75b 77 GET_CH_LAYOUT_NAME)
fe2147e9 78
73c6ec6d 79int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
fe2147e9
AK
80{
81 FilterGraph *fg = av_mallocz(sizeof(*fg));
82
83 if (!fg)
5e3f9979 84 exit(1);
fe2147e9
AK
85 fg->index = nb_filtergraphs;
86
10bca661 87 GROW_ARRAY(fg->outputs, fg->nb_outputs);
fe2147e9 88 if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
5e3f9979 89 exit(1);
fe2147e9
AK
90 fg->outputs[0]->ost = ost;
91 fg->outputs[0]->graph = fg;
50722b4f 92 fg->outputs[0]->format = -1;
fe2147e9
AK
93
94 ost->filter = fg->outputs[0];
95
10bca661 96 GROW_ARRAY(fg->inputs, fg->nb_inputs);
fe2147e9 97 if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
5e3f9979 98 exit(1);
fe2147e9
AK
99 fg->inputs[0]->ist = ist;
100 fg->inputs[0]->graph = fg;
722ec3eb 101 fg->inputs[0]->format = -1;
fe2147e9 102
a3a0230a
AK
103 fg->inputs[0]->frame_queue = av_fifo_alloc(8 * sizeof(AVFrame*));
104 if (!fg->inputs[0])
105 exit_program(1);
106
10bca661 107 GROW_ARRAY(ist->filters, ist->nb_filters);
fe2147e9
AK
108 ist->filters[ist->nb_filters - 1] = fg->inputs[0];
109
10bca661 110 GROW_ARRAY(filtergraphs, nb_filtergraphs);
fe2147e9
AK
111 filtergraphs[nb_filtergraphs - 1] = fg;
112
73c6ec6d 113 return 0;
fe2147e9
AK
114}
115
116static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
117{
118 InputStream *ist = NULL;
119 enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
120 int i;
121
122 // TODO: support other filter types
123 if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
124 av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
125 "currently.\n");
5e3f9979 126 exit(1);
fe2147e9
AK
127 }
128
129 if (in->name) {
130 AVFormatContext *s;
131 AVStream *st = NULL;
132 char *p;
133 int file_idx = strtol(in->name, &p, 0);
134
135 if (file_idx < 0 || file_idx >= nb_input_files) {
136 av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtegraph description %s.\n",
137 file_idx, fg->graph_desc);
5e3f9979 138 exit(1);
fe2147e9
AK
139 }
140 s = input_files[file_idx]->ctx;
141
142 for (i = 0; i < s->nb_streams; i++) {
15e84ed3 143 if (s->streams[i]->codecpar->codec_type != type)
fe2147e9
AK
144 continue;
145 if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
146 st = s->streams[i];
147 break;
148 }
149 }
150 if (!st) {
151 av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
152 "matches no streams.\n", p, fg->graph_desc);
5e3f9979 153 exit(1);
fe2147e9
AK
154 }
155 ist = input_streams[input_files[file_idx]->ist_index + st->index];
156 } else {
157 /* find the first unused stream of corresponding type */
158 for (i = 0; i < nb_input_streams; i++) {
159 ist = input_streams[i];
41776ba9 160 if (ist->dec_ctx->codec_type == type && ist->discard)
fe2147e9
AK
161 break;
162 }
163 if (i == nb_input_streams) {
164 av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
d28fc7b2 165 "unlabeled input pad %d on filter %s\n", in->pad_idx,
fe2147e9 166 in->filter_ctx->name);
5e3f9979 167 exit(1);
fe2147e9
AK
168 }
169 }
170 av_assert0(ist);
171
172 ist->discard = 0;
173 ist->decoding_needed = 1;
174 ist->st->discard = AVDISCARD_NONE;
175
10bca661 176 GROW_ARRAY(fg->inputs, fg->nb_inputs);
fe2147e9 177 if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
5e3f9979 178 exit(1);
fe2147e9
AK
179 fg->inputs[fg->nb_inputs - 1]->ist = ist;
180 fg->inputs[fg->nb_inputs - 1]->graph = fg;
722ec3eb 181 fg->inputs[fg->nb_inputs - 1]->format = -1;
fe2147e9 182
a3a0230a 183 fg->inputs[fg->nb_inputs - 1]->frame_queue = av_fifo_alloc(8 * sizeof(AVFrame*));
602abe77 184 if (!fg->inputs[fg->nb_inputs - 1]->frame_queue)
a3a0230a
AK
185 exit_program(1);
186
10bca661 187 GROW_ARRAY(ist->filters, ist->nb_filters);
fe2147e9
AK
188 ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
189}
190
6d592fbd
AK
191int init_complex_filtergraph(FilterGraph *fg)
192{
193 AVFilterInOut *inputs, *outputs, *cur;
194 AVFilterGraph *graph;
195 int ret = 0;
196
197 /* this graph is only used for determining the kinds of inputs
198 * and outputs we have, and is discarded on exit from this function */
199 graph = avfilter_graph_alloc();
200 if (!graph)
201 return AVERROR(ENOMEM);
202
203 ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs);
204 if (ret < 0)
205 goto fail;
206
207 for (cur = inputs; cur; cur = cur->next)
208 init_input_filter(fg, cur);
209
210 for (cur = outputs; cur;) {
211 GROW_ARRAY(fg->outputs, fg->nb_outputs);
212 fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]));
213 if (!fg->outputs[fg->nb_outputs - 1])
214 exit(1);
215
216 fg->outputs[fg->nb_outputs - 1]->graph = fg;
217 fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
218 fg->outputs[fg->nb_outputs - 1]->type = avfilter_pad_get_type(cur->filter_ctx->output_pads,
219 cur->pad_idx);
220 cur = cur->next;
221 fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
222 }
223
224fail:
225 avfilter_inout_free(&inputs);
226 avfilter_graph_free(&graph);
227 return ret;
228}
229
811bd078
AK
230static int insert_trim(int64_t start_time, int64_t duration,
231 AVFilterContext **last_filter, int *pad_idx,
232 const char *filter_name)
a83c0da5 233{
a83c0da5
AK
234 AVFilterGraph *graph = (*last_filter)->graph;
235 AVFilterContext *ctx;
236 const AVFilter *trim;
811bd078
AK
237 enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
238 const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
a83c0da5
AK
239 int ret = 0;
240
811bd078 241 if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
a83c0da5
AK
242 return 0;
243
244 trim = avfilter_get_by_name(name);
245 if (!trim) {
246 av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
247 "recording time.\n", name);
248 return AVERROR_FILTER_NOT_FOUND;
249 }
250
a83c0da5
AK
251 ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
252 if (!ctx)
253 return AVERROR(ENOMEM);
254
811bd078
AK
255 if (duration != INT64_MAX) {
256 ret = av_opt_set_double(ctx, "duration", (double)duration / 1e6,
8cd472d3
AK
257 AV_OPT_SEARCH_CHILDREN);
258 }
811bd078
AK
259 if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
260 ret = av_opt_set_double(ctx, "start", (double)start_time / 1e6,
8cd472d3
AK
261 AV_OPT_SEARCH_CHILDREN);
262 }
a83c0da5
AK
263 if (ret < 0) {
264 av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
265 return ret;
266 }
267
268 ret = avfilter_init_str(ctx, NULL);
269 if (ret < 0)
270 return ret;
271
272 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
273 if (ret < 0)
274 return ret;
275
276 *last_filter = ctx;
277 *pad_idx = 0;
278 return 0;
279}
280
16302246
MS
281static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
282 const char *filter_name, const char *args)
283{
284 AVFilterGraph *graph = (*last_filter)->graph;
285 AVFilterContext *ctx;
286 int ret;
287
288 ret = avfilter_graph_create_filter(&ctx,
289 avfilter_get_by_name(filter_name),
290 filter_name, args, NULL, graph);
291 if (ret < 0)
292 return ret;
293
294 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
295 if (ret < 0)
296 return ret;
297
298 *last_filter = ctx;
299 *pad_idx = 0;
300 return 0;
301}
302
fe2147e9
AK
303static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
304{
305 char *pix_fmts;
306 OutputStream *ost = ofilter->ost;
811bd078 307 OutputFile *of = output_files[ost->file_index];
fe2147e9
AK
308 AVFilterContext *last_filter = out->filter_ctx;
309 int pad_idx = out->pad_idx;
310 int ret;
311 char name[255];
312
313 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
314 ret = avfilter_graph_create_filter(&ofilter->filter,
315 avfilter_get_by_name("buffersink"),
58dee6e6 316 name, NULL, NULL, fg->graph);
fe2147e9
AK
317 if (ret < 0)
318 return ret;
319
bd31c61c 320 if (ofilter->width || ofilter->height) {
fe2147e9
AK
321 char args[255];
322 AVFilterContext *filter;
323
72fbc968 324 snprintf(args, sizeof(args), "%d:%d:0x%X",
50722b4f 325 ofilter->width, ofilter->height,
fe2147e9
AK
326 (unsigned)ost->sws_flags);
327 snprintf(name, sizeof(name), "scaler for output stream %d:%d",
328 ost->file_index, ost->index);
329 if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
330 name, args, NULL, fg->graph)) < 0)
331 return ret;
332 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
333 return ret;
334
335 last_filter = filter;
336 pad_idx = 0;
337 }
338
50722b4f 339 if ((pix_fmts = choose_pix_fmts(ofilter))) {
fe2147e9
AK
340 AVFilterContext *filter;
341 snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
342 ost->file_index, ost->index);
c802a2e7
VG
343 ret = avfilter_graph_create_filter(&filter,
344 avfilter_get_by_name("format"),
345 "format", pix_fmts, NULL, fg->graph);
346 av_freep(&pix_fmts);
347 if (ret < 0)
fe2147e9
AK
348 return ret;
349 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
350 return ret;
351
352 last_filter = filter;
353 pad_idx = 0;
fe2147e9
AK
354 }
355
356 if (ost->frame_rate.num) {
357 AVFilterContext *fps;
358 char args[255];
359
360 snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
361 ost->frame_rate.den);
362 snprintf(name, sizeof(name), "fps for output stream %d:%d",
363 ost->file_index, ost->index);
364 ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
365 name, args, NULL, fg->graph);
366 if (ret < 0)
367 return ret;
368
369 ret = avfilter_link(last_filter, pad_idx, fps, 0);
370 if (ret < 0)
371 return ret;
372 last_filter = fps;
373 pad_idx = 0;
374 }
375
811bd078
AK
376 snprintf(name, sizeof(name), "trim for output stream %d:%d",
377 ost->file_index, ost->index);
378 ret = insert_trim(of->start_time, of->recording_time,
379 &last_filter, &pad_idx, name);
a83c0da5
AK
380 if (ret < 0)
381 return ret;
382
383
fe2147e9
AK
384 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
385 return ret;
386
387 return 0;
388}
389
390static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
391{
392 OutputStream *ost = ofilter->ost;
811bd078 393 OutputFile *of = output_files[ost->file_index];
38313626 394 AVCodecContext *codec = ost->enc_ctx;
fe2147e9
AK
395 AVFilterContext *last_filter = out->filter_ctx;
396 int pad_idx = out->pad_idx;
397 char *sample_fmts, *sample_rates, *channel_layouts;
398 char name[255];
399 int ret;
400
401
402 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
403 ret = avfilter_graph_create_filter(&ofilter->filter,
404 avfilter_get_by_name("abuffersink"),
405 name, NULL, NULL, fg->graph);
406 if (ret < 0)
407 return ret;
408
409 if (codec->channels && !codec->channel_layout)
410 codec->channel_layout = av_get_default_channel_layout(codec->channels);
411
50722b4f
AK
412 sample_fmts = choose_sample_fmts(ofilter);
413 sample_rates = choose_sample_rates(ofilter);
414 channel_layouts = choose_channel_layouts(ofilter);
fe2147e9
AK
415 if (sample_fmts || sample_rates || channel_layouts) {
416 AVFilterContext *format;
417 char args[256];
418 int len = 0;
419
420 if (sample_fmts)
421 len += snprintf(args + len, sizeof(args) - len, "sample_fmts=%s:",
422 sample_fmts);
423 if (sample_rates)
424 len += snprintf(args + len, sizeof(args) - len, "sample_rates=%s:",
425 sample_rates);
426 if (channel_layouts)
427 len += snprintf(args + len, sizeof(args) - len, "channel_layouts=%s:",
428 channel_layouts);
429 args[len - 1] = 0;
430
431 av_freep(&sample_fmts);
432 av_freep(&sample_rates);
433 av_freep(&channel_layouts);
434
435 snprintf(name, sizeof(name), "audio format for output stream %d:%d",
436 ost->file_index, ost->index);
437 ret = avfilter_graph_create_filter(&format,
438 avfilter_get_by_name("aformat"),
439 name, args, NULL, fg->graph);
440 if (ret < 0)
441 return ret;
442
443 ret = avfilter_link(last_filter, pad_idx, format, 0);
444 if (ret < 0)
445 return ret;
446
447 last_filter = format;
448 pad_idx = 0;
449 }
450
811bd078
AK
451 snprintf(name, sizeof(name), "trim for output stream %d:%d",
452 ost->file_index, ost->index);
453 ret = insert_trim(of->start_time, of->recording_time,
454 &last_filter, &pad_idx, name);
a83c0da5
AK
455 if (ret < 0)
456 return ret;
457
fe2147e9
AK
458 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
459 return ret;
460
461 return 0;
462}
463
464#define DESCRIBE_FILTER_LINK(f, inout, in) \
465{ \
466 AVFilterContext *ctx = inout->filter_ctx; \
467 AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
06cd4c5a 468 int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs; \
fe2147e9
AK
469 AVIOContext *pb; \
470 \
471 if (avio_open_dyn_buf(&pb) < 0) \
5e3f9979 472 exit(1); \
fe2147e9
AK
473 \
474 avio_printf(pb, "%s", ctx->filter->name); \
475 if (nb_pads > 1) \
476 avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
477 avio_w8(pb, 0); \
478 avio_close_dyn_buf(pb, &f->name); \
479}
480
481int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
482{
483 av_freep(&ofilter->name);
484 DESCRIBE_FILTER_LINK(ofilter, out, 0);
485
486 switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
487 case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
488 case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
489 default: av_assert0(0);
490 }
491}
492
493static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
494 AVFilterInOut *in)
495{
2e661f26 496 AVFilterContext *last_filter;
51fc88e7 497 const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
fe2147e9 498 InputStream *ist = ifilter->ist;
811bd078 499 InputFile *f = input_files[ist->file_index];
fe2147e9
AK
500 AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
501 ist->st->time_base;
1bf34134
AK
502 AVBufferSrcParameters *par;
503 char name[255];
811bd078 504 int ret, pad_idx = 0;
fe2147e9 505
fe2147e9
AK
506 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
507 ist->file_index, ist->st->index);
508
1bf34134
AK
509 ifilter->filter = avfilter_graph_alloc_filter(fg->graph, buffer_filt, name);
510 if (!ifilter->filter)
511 return AVERROR(ENOMEM);
512
513 par = av_buffersrc_parameters_alloc();
514 if (!par)
515 return AVERROR(ENOMEM);
516
722ec3eb
AK
517 par->sample_aspect_ratio = ifilter->sample_aspect_ratio;
518 par->width = ifilter->width;
519 par->height = ifilter->height;
520 par->format = ifilter->format;
1bf34134 521 par->time_base = tb;
b0f36a00
AK
522 if (ist->framerate.num)
523 par->frame_rate = ist->framerate;
722ec3eb 524 par->hw_frames_ctx = ifilter->hw_frames_ctx;
1bf34134
AK
525
526 ret = av_buffersrc_parameters_set(ifilter->filter, par);
527 av_freep(&par);
528 if (ret < 0)
529 return ret;
530
531 ret = avfilter_init_str(ifilter->filter, NULL);
532 if (ret < 0)
fe2147e9 533 return ret;
1bf34134 534
2e661f26 535 last_filter = ifilter->filter;
fe2147e9 536
16302246
MS
537 if (ist->autorotate) {
538 uint8_t* displaymatrix = av_stream_get_side_data(ist->st,
539 AV_PKT_DATA_DISPLAYMATRIX, NULL);
540 if (displaymatrix) {
541 double rot = av_display_rotation_get((int32_t*) displaymatrix);
542 if (rot < -135 || rot > 135) {
543 ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
544 if (ret < 0)
545 return ret;
546 ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
547 } else if (rot < -45) {
548 ret = insert_filter(&last_filter, &pad_idx, "transpose", "dir=clock");
549 } else if (rot > 45) {
550 ret = insert_filter(&last_filter, &pad_idx, "transpose", "dir=cclock");
551 }
552 if (ret < 0)
553 return ret;
554 }
555 }
556
811bd078
AK
557 snprintf(name, sizeof(name), "trim for input stream %d:%d",
558 ist->file_index, ist->st->index);
559 ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
488a0fa6 560 AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
811bd078
AK
561 if (ret < 0)
562 return ret;
563
2e661f26 564 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
fe2147e9
AK
565 return ret;
566 return 0;
567}
568
569static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
570 AVFilterInOut *in)
571{
2e661f26 572 AVFilterContext *last_filter;
51fc88e7 573 const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
fe2147e9 574 InputStream *ist = ifilter->ist;
811bd078 575 InputFile *f = input_files[ist->file_index];
1bf34134 576 AVBufferSrcParameters *par;
fe2147e9 577 char args[255], name[255];
811bd078 578 int ret, pad_idx = 0;
fe2147e9 579
fe2147e9
AK
580 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
581 ist->file_index, ist->st->index);
582
1bf34134
AK
583 ifilter->filter = avfilter_graph_alloc_filter(fg->graph, abuffer_filt, name);
584 if (!ifilter->filter)
585 return AVERROR(ENOMEM);
586
587 par = av_buffersrc_parameters_alloc();
588 if (!par)
589 return AVERROR(ENOMEM);
590
722ec3eb
AK
591 par->time_base = (AVRational){ 1, ifilter->sample_rate };
592 par->sample_rate = ifilter->sample_rate;
593 par->format = ifilter->format;
594 par->channel_layout = ifilter->channel_layout;
1bf34134
AK
595
596 ret = av_buffersrc_parameters_set(ifilter->filter, par);
597 av_freep(&par);
598 if (ret < 0)
599 return ret;
600
601 ret = avfilter_init_str(ifilter->filter, NULL);
602 if (ret < 0)
fe2147e9 603 return ret;
2e661f26 604 last_filter = ifilter->filter;
fe2147e9
AK
605
606 if (audio_sync_method > 0) {
607 AVFilterContext *async;
fe2147e9
AK
608 int len = 0;
609
610 av_log(NULL, AV_LOG_WARNING, "-async has been deprecated. Used the "
611 "asyncts audio filter instead.\n");
612
613 if (audio_sync_method > 1)
614 len += snprintf(args + len, sizeof(args) - len, "compensate=1:"
615 "max_comp=%d:", audio_sync_method);
616 snprintf(args + len, sizeof(args) - len, "min_delta=%f",
617 audio_drift_threshold);
618
619 snprintf(name, sizeof(name), "graph %d audio sync for input stream %d:%d",
620 fg->index, ist->file_index, ist->st->index);
621 ret = avfilter_graph_create_filter(&async,
622 avfilter_get_by_name("asyncts"),
623 name, args, NULL, fg->graph);
624 if (ret < 0)
625 return ret;
626
2e661f26 627 ret = avfilter_link(last_filter, 0, async, 0);
fe2147e9
AK
628 if (ret < 0)
629 return ret;
630
2e661f26 631 last_filter = async;
fe2147e9 632 }
9a71d362
JR
633 if (audio_volume != 256) {
634 AVFilterContext *volume;
635
636 av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
637 "audio filter instead.\n");
638
639 snprintf(args, sizeof(args), "volume=%f", audio_volume / 256.0);
640
641 snprintf(name, sizeof(name), "graph %d volume for input stream %d:%d",
642 fg->index, ist->file_index, ist->st->index);
643 ret = avfilter_graph_create_filter(&volume,
644 avfilter_get_by_name("volume"),
645 name, args, NULL, fg->graph);
646 if (ret < 0)
647 return ret;
648
2e661f26 649 ret = avfilter_link(last_filter, 0, volume, 0);
9a71d362
JR
650 if (ret < 0)
651 return ret;
652
2e661f26 653 last_filter = volume;
9a71d362 654 }
811bd078
AK
655
656 snprintf(name, sizeof(name), "trim for input stream %d:%d",
657 ist->file_index, ist->st->index);
658 ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
488a0fa6 659 AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
811bd078
AK
660 if (ret < 0)
661 return ret;
662
2e661f26 663 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
fe2147e9
AK
664 return ret;
665
666 return 0;
667}
668
669static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
670 AVFilterInOut *in)
671{
672 av_freep(&ifilter->name);
673 DESCRIBE_FILTER_LINK(ifilter, in, 1);
674
675 switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
676 case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
677 case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
678 default: av_assert0(0);
679 }
680}
681
682int configure_filtergraph(FilterGraph *fg)
683{
684 AVFilterInOut *inputs, *outputs, *cur;
49670e42 685 int ret, i, simple = filtergraph_is_simple(fg);
fe2147e9
AK
686 const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
687 fg->graph_desc;
688
689 avfilter_graph_free(&fg->graph);
690 if (!(fg->graph = avfilter_graph_alloc()))
691 return AVERROR(ENOMEM);
692
693 if (simple) {
694 OutputStream *ost = fg->outputs[0]->ost;
5c7db097
JR
695 char args[512];
696 AVDictionaryEntry *e = NULL;
5c7db097 697
fe2147e9
AK
698 snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
699 fg->graph->scale_sws_opts = av_strdup(args);
5c7db097
JR
700
701 args[0] = '\0';
702 while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
703 AV_DICT_IGNORE_SUFFIX))) {
704 av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
705 }
706 if (strlen(args))
707 args[strlen(args) - 1] = '\0';
708 fg->graph->resample_lavr_opts = av_strdup(args);
fe2147e9
AK
709 }
710
711 if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
f6772e9b 712 goto fail;
fe2147e9 713
5d273d3e
MT
714 if (hw_device_ctx) {
715 for (i = 0; i < fg->graph->nb_filters; i++) {
716 fg->graph->filters[i]->hw_device_ctx = av_buffer_ref(hw_device_ctx);
717 }
718 }
719
fe2147e9
AK
720 if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
721 av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
722 "exactly one input and output.\n", graph_desc);
f6772e9b
AK
723 ret = AVERROR(EINVAL);
724 goto fail;
fe2147e9
AK
725 }
726
fe2147e9
AK
727 for (cur = inputs, i = 0; cur; cur = cur->next, i++)
728 if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
f6772e9b 729 goto fail;
fe2147e9
AK
730 avfilter_inout_free(&inputs);
731
8b830ee9
LB
732 for (cur = outputs, i = 0; cur; cur = cur->next, i++) {
733 OutputFilter *ofilter = fg->outputs[i];
734 if (ofilter->ost)
735 configure_output_filter(fg, ofilter, cur);
736 }
737
6d592fbd 738 avfilter_inout_free(&outputs);
fe2147e9 739
6d592fbd 740 if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
f6772e9b 741 goto fail;
fe2147e9 742
50722b4f
AK
743 /* limit the lists of allowed formats to the ones selected, to
744 * make sure they stay the same if the filtergraph is reconfigured later */
745 for (i = 0; i < fg->nb_outputs; i++) {
746 OutputFilter *ofilter = fg->outputs[i];
747 AVFilterLink *link = ofilter->filter->inputs[0];
748
749 ofilter->format = link->format;
750
751 ofilter->width = link->w;
752 ofilter->height = link->h;
753
754 ofilter->sample_rate = link->sample_rate;
755 ofilter->channel_layout = link->channel_layout;
756 }
757
d2e56cf7
AK
758 for (i = 0; i < fg->nb_inputs; i++) {
759 while (av_fifo_size(fg->inputs[i]->frame_queue)) {
760 AVFrame *tmp;
761 av_fifo_generic_read(fg->inputs[i]->frame_queue, &tmp, sizeof(tmp), NULL);
762 ret = av_buffersrc_add_frame(fg->inputs[i]->filter, tmp);
763 av_frame_free(&tmp);
764 if (ret < 0)
f6772e9b 765 goto fail;
d2e56cf7
AK
766 }
767 }
768
94ebf556
AK
769 /* send the EOFs for the finished inputs */
770 for (i = 0; i < fg->nb_inputs; i++) {
771 if (fg->inputs[i]->eof) {
772 ret = av_buffersrc_add_frame(fg->inputs[i]->filter, NULL);
773 if (ret < 0)
f6772e9b 774 goto fail;
94ebf556
AK
775 }
776 }
777
fe2147e9 778 return 0;
f6772e9b
AK
779fail:
780 avfilter_graph_free(&fg->graph);
781 return ret;
fe2147e9
AK
782}
783
722ec3eb
AK
784int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)
785{
786 av_buffer_unref(&ifilter->hw_frames_ctx);
787
788 ifilter->format = frame->format;
789
790 ifilter->width = frame->width;
791 ifilter->height = frame->height;
792 ifilter->sample_aspect_ratio = frame->sample_aspect_ratio;
793
794 ifilter->sample_rate = frame->sample_rate;
795 ifilter->channel_layout = frame->channel_layout;
796
797 if (frame->hw_frames_ctx) {
798 ifilter->hw_frames_ctx = av_buffer_ref(frame->hw_frames_ctx);
799 if (!ifilter->hw_frames_ctx)
800 return AVERROR(ENOMEM);
801 }
802
803 return 0;
804}
805
fe2147e9
AK
806int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
807{
808 int i;
809 for (i = 0; i < fg->nb_inputs; i++)
810 if (fg->inputs[i]->ist == ist)
811 return 1;
812 return 0;
813}
814
49670e42
AK
815int filtergraph_is_simple(FilterGraph *fg)
816{
817 return !fg->graph_desc;
818}