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