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