FFmpegKit Linux API 6.0
Loading...
Searching...
No Matches
fftools_ffmpeg_filter.c
Go to the documentation of this file.
1/*
2 * ffmpeg filter configuration
3 * Copyright (c) 2018 Taner Sener
4 * Copyright (c) 2023 ARTHENICA LTD
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/*
24 * This file is the modified version of ffmpeg_filter.c file living in ffmpeg source code under the fftools folder.
25 * We manually update it each time we depend on a new ffmpeg version. Below you can see the list of changes applied
26 * by us to develop mobile-ffmpeg and later ffmpeg-kit libraries.
27 *
28 * ffmpeg-kit changes by ARTHENICA LTD
29 *
30 * 07.2023
31 * --------------------------------------------------------
32 * - FFmpeg 6.0 changes migrated
33 *
34 * mobile-ffmpeg / ffmpeg-kit changes by Taner Sener
35 *
36 * 08.2018
37 * --------------------------------------------------------
38 * - fftools_ prefix added to file name and parent header
39 *
40 * 07.2018
41 * --------------------------------------------------------
42 * - unused headers removed
43 */
44
45#include <stdint.h>
46
47#include "fftools_ffmpeg.h"
48
49#include "libavfilter/avfilter.h"
50#include "libavfilter/buffersink.h"
51#include "libavfilter/buffersrc.h"
52
53#include "libavutil/avassert.h"
54#include "libavutil/avstring.h"
55#include "libavutil/bprint.h"
56#include "libavutil/channel_layout.h"
57#include "libavutil/display.h"
58#include "libavutil/opt.h"
59#include "libavutil/pixdesc.h"
60#include "libavutil/pixfmt.h"
61#include "libavutil/imgutils.h"
62#include "libavutil/samplefmt.h"
63
64// FIXME: YUV420P etc. are actually supported with full color range,
65// yet the latter information isn't available here.
66static const enum AVPixelFormat *get_compliance_normal_pix_fmts(const AVCodec *codec, const enum AVPixelFormat default_formats[])
67{
68 static const enum AVPixelFormat mjpeg_formats[] =
69 { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
70 AV_PIX_FMT_NONE };
71
72 if (!strcmp(codec->name, "mjpeg")) {
73 return mjpeg_formats;
74 } else {
75 return default_formats;
76 }
77}
78
79enum AVPixelFormat
80choose_pixel_fmt(const AVCodec *codec, enum AVPixelFormat target,
81 int strict_std_compliance)
82{
83 if (codec && codec->pix_fmts) {
84 const enum AVPixelFormat *p = codec->pix_fmts;
85 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
86 //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
87 int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
88 enum AVPixelFormat best= AV_PIX_FMT_NONE;
89
90 if (strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
92 }
93 for (; *p != AV_PIX_FMT_NONE; p++) {
94 best = av_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
95 if (*p == target)
96 break;
97 }
98 if (*p == AV_PIX_FMT_NONE) {
99 if (target != AV_PIX_FMT_NONE)
100 av_log(NULL, AV_LOG_WARNING,
101 "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
102 av_get_pix_fmt_name(target),
103 codec->name,
104 av_get_pix_fmt_name(best));
105 return best;
106 }
107 }
108 return target;
109}
110
111/* May return NULL (no pixel format found), a static string or a string
112 * backed by the bprint. Nothing has been written to the AVBPrint in case
113 * NULL is returned. The AVBPrint provided should be clean. */
114static const char *choose_pix_fmts(OutputFilter *ofilter, AVBPrint *bprint)
115{
116 OutputStream *ost = ofilter->ost;
117 AVCodecContext *enc = ost->enc_ctx;
118 const AVDictionaryEntry *strict_dict = av_dict_get(ost->encoder_opts, "strict", NULL, 0);
119 if (strict_dict)
120 // used by choose_pixel_fmt() and below
121 av_opt_set(ost->enc_ctx, "strict", strict_dict->value, 0);
122
123 if (ost->keep_pix_fmt) {
124 avfilter_graph_set_auto_convert(ofilter->graph->graph,
125 AVFILTER_AUTO_CONVERT_NONE);
126 if (ost->enc_ctx->pix_fmt == AV_PIX_FMT_NONE)
127 return NULL;
128 return av_get_pix_fmt_name(ost->enc_ctx->pix_fmt);
129 }
130 if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
131 return av_get_pix_fmt_name(choose_pixel_fmt(enc->codec, enc->pix_fmt,
132 ost->enc_ctx->strict_std_compliance));
133 } else if (enc->codec->pix_fmts) {
134 const enum AVPixelFormat *p;
135
136 p = enc->codec->pix_fmts;
137 if (ost->enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
138 p = get_compliance_normal_pix_fmts(enc->codec, p);
139 }
140
141 for (; *p != AV_PIX_FMT_NONE; p++) {
142 const char *name = av_get_pix_fmt_name(*p);
143 av_bprintf(bprint, "%s%c", name, p[1] == AV_PIX_FMT_NONE ? '\0' : '|');
144 }
145 if (!av_bprint_is_complete(bprint))
146 report_and_exit(AVERROR(ENOMEM));
147 return bprint->str;
148 } else
149 return NULL;
150}
151
152/* Define a function for appending a list of allowed formats
153 * to an AVBPrint. If nonempty, the list will have a header. */
154#define DEF_CHOOSE_FORMAT(name, type, var, supported_list, none, printf_format, get_name) \
155static void choose_ ## name (OutputFilter *ofilter, AVBPrint *bprint) \
156{ \
157 if (ofilter->var == none && !ofilter->supported_list) \
158 return; \
159 av_bprintf(bprint, #name "="); \
160 if (ofilter->var != none) { \
161 av_bprintf(bprint, printf_format, get_name(ofilter->var)); \
162 } else { \
163 const type *p; \
164 \
165 for (p = ofilter->supported_list; *p != none; p++) { \
166 av_bprintf(bprint, printf_format "|", get_name(*p)); \
167 } \
168 if (bprint->len > 0) \
169 bprint->str[--bprint->len] = '\0'; \
170 } \
171 av_bprint_chars(bprint, ':', 1); \
172}
173
174//DEF_CHOOSE_FORMAT(pix_fmts, enum AVPixelFormat, format, formats, AV_PIX_FMT_NONE,
175// GET_PIX_FMT_NAME)
176
177DEF_CHOOSE_FORMAT(sample_fmts, enum AVSampleFormat, format, formats,
178 AV_SAMPLE_FMT_NONE, "%s", av_get_sample_fmt_name)
179
181 "%d", )
182
183static void choose_channel_layouts(OutputFilter *ofilter, AVBPrint *bprint)
184{
185 if (av_channel_layout_check(&ofilter->ch_layout)) {
186 av_bprintf(bprint, "channel_layouts=");
187 av_channel_layout_describe_bprint(&ofilter->ch_layout, bprint);
188 } else if (ofilter->ch_layouts) {
189 const AVChannelLayout *p;
190
191 av_bprintf(bprint, "channel_layouts=");
192 for (p = ofilter->ch_layouts; p->nb_channels; p++) {
193 av_channel_layout_describe_bprint(p, bprint);
194 av_bprintf(bprint, "|");
195 }
196 if (bprint->len > 0)
197 bprint->str[--bprint->len] = '\0';
198 } else
199 return;
200 av_bprint_chars(bprint, ':', 1);
201}
202
204{
205 FilterGraph *fg = av_mallocz(sizeof(*fg));
206 OutputFilter *ofilter;
207 InputFilter *ifilter;
208
209 if (!fg)
210 report_and_exit(AVERROR(ENOMEM));
212
213 ofilter = ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
214 ofilter->ost = ost;
215 ofilter->graph = fg;
216 ofilter->format = -1;
217
218 ost->filter = ofilter;
219
220 ifilter = ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
221 ifilter->ist = ist;
222 ifilter->graph = fg;
223 ifilter->format = -1;
224
225 ifilter->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
226 if (!ifilter->frame_queue)
227 report_and_exit(AVERROR(ENOMEM));
228
229 GROW_ARRAY(ist->filters, ist->nb_filters);
230 ist->filters[ist->nb_filters - 1] = ifilter;
231
234
235 return 0;
236}
237
238static char *describe_filter_link(FilterGraph *fg, AVFilterInOut *inout, int in)
239{
240 AVFilterContext *ctx = inout->filter_ctx;
241 AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads;
242 int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs;
243 char *res;
244
245 if (nb_pads > 1)
246 res = av_strdup(ctx->filter->name);
247 else
248 res = av_asprintf("%s:%s", ctx->filter->name,
249 avfilter_pad_get_name(pads, inout->pad_idx));
250 if (!res)
251 report_and_exit(AVERROR(ENOMEM));
252 return res;
253}
254
255static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
256{
257 InputStream *ist = NULL;
258 enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
259 InputFilter *ifilter;
260 int i;
261
262 // TODO: support other filter types
263 if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
264 av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
265 "currently.\n");
266 exit_program(1);
267 }
268
269 if (in->name) {
270 AVFormatContext *s;
271 AVStream *st = NULL;
272 char *p;
273 int file_idx = strtol(in->name, &p, 0);
274
275 if (file_idx < 0 || file_idx >= nb_input_files) {
276 av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
277 file_idx, fg->graph_desc);
278 exit_program(1);
279 }
280 s = input_files[file_idx]->ctx;
281
282 for (i = 0; i < s->nb_streams; i++) {
283 enum AVMediaType stream_type = s->streams[i]->codecpar->codec_type;
284 if (stream_type != type &&
285 !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
286 type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
287 continue;
288 if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
289 st = s->streams[i];
290 break;
291 }
292 }
293 if (!st) {
294 av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
295 "matches no streams.\n", p, fg->graph_desc);
296 exit_program(1);
297 }
298 ist = input_files[file_idx]->streams[st->index];
299 if (ist->user_set_discard == AVDISCARD_ALL) {
300 av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
301 "matches a disabled input stream.\n", p, fg->graph_desc);
302 exit_program(1);
303 }
304 } else {
305 /* find the first unused stream of corresponding type */
306 for (ist = ist_iter(NULL); ist; ist = ist_iter(ist)) {
307 if (ist->user_set_discard == AVDISCARD_ALL)
308 continue;
309 if (ist->dec_ctx->codec_type == type && ist->discard)
310 break;
311 }
312 if (!ist) {
313 av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
314 "unlabeled input pad %d on filter %s\n", in->pad_idx,
315 in->filter_ctx->name);
316 exit_program(1);
317 }
318 }
319 av_assert0(ist);
320
321 ist->discard = 0;
323 ist->processing_needed = 1;
324 ist->st->discard = AVDISCARD_NONE;
325
326 ifilter = ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
327 ifilter->ist = ist;
328 ifilter->graph = fg;
329 ifilter->format = -1;
330 ifilter->type = ist->st->codecpar->codec_type;
331 ifilter->name = describe_filter_link(fg, in, 1);
332
333 ifilter->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
334 if (!ifilter->frame_queue)
335 report_and_exit(AVERROR(ENOMEM));
336
337 GROW_ARRAY(ist->filters, ist->nb_filters);
338 ist->filters[ist->nb_filters - 1] = ifilter;
339}
340
341static int read_binary(const char *path, uint8_t **data, int *len)
342{
343 AVIOContext *io = NULL;
344 int64_t fsize;
345 int ret;
346
347 *data = NULL;
348 *len = 0;
349
350 ret = avio_open2(&io, path, AVIO_FLAG_READ, &int_cb, NULL);
351 if (ret < 0) {
352 av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s': %s\n",
353 path, av_err2str(ret));
354 return ret;
355 }
356
357 fsize = avio_size(io);
358 if (fsize < 0 || fsize > INT_MAX) {
359 av_log(NULL, AV_LOG_ERROR, "Cannot obtain size of file %s\n", path);
360 ret = AVERROR(EIO);
361 goto fail;
362 }
363
364 *data = av_malloc(fsize);
365 if (!*data) {
366 ret = AVERROR(ENOMEM);
367 goto fail;
368 }
369
370 ret = avio_read(io, *data, fsize);
371 if (ret != fsize) {
372 av_log(NULL, AV_LOG_ERROR, "Error reading file %s\n", path);
373 ret = ret < 0 ? ret : AVERROR(EIO);
374 goto fail;
375 }
376
377 *len = fsize;
378
379 return 0;
380fail:
381 avio_close(io);
382 av_freep(data);
383 *len = 0;
384 return ret;
385}
386
387static int filter_opt_apply(AVFilterContext *f, const char *key, const char *val)
388{
389 const AVOption *o = NULL;
390 int ret;
391
392 ret = av_opt_set(f, key, val, AV_OPT_SEARCH_CHILDREN);
393 if (ret >= 0)
394 return 0;
395
396 if (ret == AVERROR_OPTION_NOT_FOUND && key[0] == '/')
397 o = av_opt_find(f, key + 1, NULL, 0, AV_OPT_SEARCH_CHILDREN);
398 if (!o)
399 goto err_apply;
400
401 // key is a valid option name prefixed with '/'
402 // interpret value as a path from which to load the actual option value
403 key++;
404
405 if (o->type == AV_OPT_TYPE_BINARY) {
406 uint8_t *data;
407 int len;
408
409 ret = read_binary(val, &data, &len);
410 if (ret < 0)
411 goto err_load;
412
413 ret = av_opt_set_bin(f, key, data, len, AV_OPT_SEARCH_CHILDREN);
414 av_freep(&data);
415 } else {
416 char *data = file_read(val);
417 if (!data) {
418 ret = AVERROR(EIO);
419 goto err_load;
420 }
421
422 ret = av_opt_set(f, key, data, AV_OPT_SEARCH_CHILDREN);
423 av_freep(&data);
424 }
425 if (ret < 0)
426 goto err_apply;
427
428 return 0;
429
430err_apply:
431 av_log(NULL, AV_LOG_ERROR,
432 "Error applying option '%s' to filter '%s': %s\n",
433 key, f->filter->name, av_err2str(ret));
434 return ret;
435err_load:
436 av_log(NULL, AV_LOG_ERROR,
437 "Error loading value for option '%s' from file '%s'\n",
438 key, val);
439 return ret;
440}
441
442static int graph_opts_apply(AVFilterGraphSegment *seg)
443{
444 for (size_t i = 0; i < seg->nb_chains; i++) {
445 AVFilterChain *ch = seg->chains[i];
446
447 for (size_t j = 0; j < ch->nb_filters; j++) {
448 AVFilterParams *p = ch->filters[j];
449 const AVDictionaryEntry *e = NULL;
450
451 av_assert0(p->filter);
452
453 while ((e = av_dict_iterate(p->opts, e))) {
454 int ret = filter_opt_apply(p->filter, e->key, e->value);
455 if (ret < 0)
456 return ret;
457 }
458
459 av_dict_free(&p->opts);
460 }
461 }
462
463 return 0;
464}
465
466static int graph_parse(AVFilterGraph *graph, const char *desc,
467 AVFilterInOut **inputs, AVFilterInOut **outputs)
468{
469 AVFilterGraphSegment *seg;
470 int ret;
471
472 ret = avfilter_graph_segment_parse(graph, desc, 0, &seg);
473 if (ret < 0)
474 return ret;
475
476 ret = avfilter_graph_segment_create_filters(seg, 0);
477 if (ret < 0)
478 goto fail;
479
480 ret = graph_opts_apply(seg);
481 if (ret < 0)
482 goto fail;
483
484 ret = avfilter_graph_segment_apply(seg, 0, inputs, outputs);
485
486fail:
487 avfilter_graph_segment_free(&seg);
488 return ret;
489}
490
492{
493 AVFilterInOut *inputs, *outputs, *cur;
494 AVFilterGraph *graph;
495 int ret = 0;
496
497 /* this graph is only used for determining the kinds of inputs
498 * and outputs we have, and is discarded on exit from this function */
499 graph = avfilter_graph_alloc();
500 if (!graph)
501 return AVERROR(ENOMEM);
502 graph->nb_threads = 1;
503
504 ret = graph_parse(graph, fg->graph_desc, &inputs, &outputs);
505 if (ret < 0)
506 goto fail;
507
508 for (cur = inputs; cur; cur = cur->next)
509 init_input_filter(fg, cur);
510
511 for (cur = outputs; cur;) {
512 OutputFilter *const ofilter = ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
513
514 ofilter->graph = fg;
515 ofilter->out_tmp = cur;
516 ofilter->type = avfilter_pad_get_type(cur->filter_ctx->output_pads,
517 cur->pad_idx);
518 ofilter->name = describe_filter_link(fg, cur, 0);
519 cur = cur->next;
520 ofilter->out_tmp->next = NULL;
521 }
522
523fail:
524 avfilter_inout_free(&inputs);
525 avfilter_graph_free(&graph);
526 return ret;
527}
528
529static int insert_trim(int64_t start_time, int64_t duration,
530 AVFilterContext **last_filter, int *pad_idx,
531 const char *filter_name)
532{
533 AVFilterGraph *graph = (*last_filter)->graph;
534 AVFilterContext *ctx;
535 const AVFilter *trim;
536 enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
537 const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
538 int ret = 0;
539
540 if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
541 return 0;
542
543 trim = avfilter_get_by_name(name);
544 if (!trim) {
545 av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
546 "recording time.\n", name);
547 return AVERROR_FILTER_NOT_FOUND;
548 }
549
550 ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
551 if (!ctx)
552 return AVERROR(ENOMEM);
553
554 if (duration != INT64_MAX) {
555 ret = av_opt_set_int(ctx, "durationi", duration,
556 AV_OPT_SEARCH_CHILDREN);
557 }
558 if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
559 ret = av_opt_set_int(ctx, "starti", start_time,
560 AV_OPT_SEARCH_CHILDREN);
561 }
562 if (ret < 0) {
563 av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
564 return ret;
565 }
566
567 ret = avfilter_init_str(ctx, NULL);
568 if (ret < 0)
569 return ret;
570
571 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
572 if (ret < 0)
573 return ret;
574
575 *last_filter = ctx;
576 *pad_idx = 0;
577 return 0;
578}
579
580static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
581 const char *filter_name, const char *args)
582{
583 AVFilterGraph *graph = (*last_filter)->graph;
584 AVFilterContext *ctx;
585 int ret;
586
587 ret = avfilter_graph_create_filter(&ctx,
588 avfilter_get_by_name(filter_name),
589 filter_name, args, NULL, graph);
590 if (ret < 0)
591 return ret;
592
593 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
594 if (ret < 0)
595 return ret;
596
597 *last_filter = ctx;
598 *pad_idx = 0;
599 return 0;
600}
601
602static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
603{
604 OutputStream *ost = ofilter->ost;
606 AVFilterContext *last_filter = out->filter_ctx;
607 AVBPrint bprint;
608 int pad_idx = out->pad_idx;
609 int ret;
610 const char *pix_fmts;
611 char name[255];
612
613 snprintf(name, sizeof(name), "out_%d_%d", ost->file_index, ost->index);
614 ret = avfilter_graph_create_filter(&ofilter->filter,
615 avfilter_get_by_name("buffersink"),
616 name, NULL, NULL, fg->graph);
617
618 if (ret < 0)
619 return ret;
620
621 if ((ofilter->width || ofilter->height) && ofilter->ost->autoscale) {
622 char args[255];
623 AVFilterContext *filter;
624 const AVDictionaryEntry *e = NULL;
625
626 snprintf(args, sizeof(args), "%d:%d",
627 ofilter->width, ofilter->height);
628
629 while ((e = av_dict_iterate(ost->sws_dict, e))) {
630 av_strlcatf(args, sizeof(args), ":%s=%s", e->key, e->value);
631 }
632
633 snprintf(name, sizeof(name), "scaler_out_%d_%d",
634 ost->file_index, ost->index);
635 if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
636 name, args, NULL, fg->graph)) < 0)
637 return ret;
638 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
639 return ret;
640
641 last_filter = filter;
642 pad_idx = 0;
643 }
644
645 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
646 if ((pix_fmts = choose_pix_fmts(ofilter, &bprint))) {
647 AVFilterContext *filter;
648
649 ret = avfilter_graph_create_filter(&filter,
650 avfilter_get_by_name("format"),
651 "format", pix_fmts, NULL, fg->graph);
652 av_bprint_finalize(&bprint, NULL);
653 if (ret < 0)
654 return ret;
655 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
656 return ret;
657
658 last_filter = filter;
659 pad_idx = 0;
660 }
661
662 if (ost->frame_rate.num && 0) {
663 AVFilterContext *fps;
664 char args[255];
665
666 snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
667 ost->frame_rate.den);
668 snprintf(name, sizeof(name), "fps_out_%d_%d",
669 ost->file_index, ost->index);
670 ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
671 name, args, NULL, fg->graph);
672 if (ret < 0)
673 return ret;
674
675 ret = avfilter_link(last_filter, pad_idx, fps, 0);
676 if (ret < 0)
677 return ret;
678 last_filter = fps;
679 pad_idx = 0;
680 }
681
682 snprintf(name, sizeof(name), "trim_out_%d_%d",
683 ost->file_index, ost->index);
685 &last_filter, &pad_idx, name);
686 if (ret < 0)
687 return ret;
688
689
690 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
691 return ret;
692
693 return 0;
694}
695
696static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
697{
698 OutputStream *ost = ofilter->ost;
700 AVCodecContext *codec = ost->enc_ctx;
701 AVFilterContext *last_filter = out->filter_ctx;
702 int pad_idx = out->pad_idx;
703 AVBPrint args;
704 char name[255];
705 int ret;
706
707 snprintf(name, sizeof(name), "out_%d_%d", ost->file_index, ost->index);
708 ret = avfilter_graph_create_filter(&ofilter->filter,
709 avfilter_get_by_name("abuffersink"),
710 name, NULL, NULL, fg->graph);
711 if (ret < 0)
712 return ret;
713 if ((ret = av_opt_set_int(ofilter->filter, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
714 return ret;
715
716#define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
717 AVFilterContext *filt_ctx; \
718 \
719 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
720 "similarly to -af " filter_name "=%s.\n", arg); \
721 \
722 ret = avfilter_graph_create_filter(&filt_ctx, \
723 avfilter_get_by_name(filter_name), \
724 filter_name, arg, NULL, fg->graph); \
725 if (ret < 0) \
726 goto fail; \
727 \
728 ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
729 if (ret < 0) \
730 goto fail; \
731 \
732 last_filter = filt_ctx; \
733 pad_idx = 0; \
734} while (0)
735 av_bprint_init(&args, 0, AV_BPRINT_SIZE_UNLIMITED);
736#if FFMPEG_OPT_MAP_CHANNEL
737 if (ost->audio_channels_mapped) {
738 AVChannelLayout mapped_layout = { 0 };
739 int i;
740 av_channel_layout_default(&mapped_layout, ost->audio_channels_mapped);
741 av_channel_layout_describe_bprint(&mapped_layout, &args);
742 for (i = 0; i < ost->audio_channels_mapped; i++)
743 if (ost->audio_channels_map[i] != -1)
744 av_bprintf(&args, "|c%d=c%d", i, ost->audio_channels_map[i]);
745
746 AUTO_INSERT_FILTER("-map_channel", "pan", args.str);
747 av_bprint_clear(&args);
748 }
749#endif
750
751 if (codec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
752 av_channel_layout_default(&codec->ch_layout, codec->ch_layout.nb_channels);
753
754 choose_sample_fmts(ofilter, &args);
755 choose_sample_rates(ofilter, &args);
756 choose_channel_layouts(ofilter, &args);
757 if (!av_bprint_is_complete(&args)) {
758 ret = AVERROR(ENOMEM);
759 goto fail;
760 }
761 if (args.len) {
762 AVFilterContext *format;
763
764 snprintf(name, sizeof(name), "format_out_%d_%d",
765 ost->file_index, ost->index);
766 ret = avfilter_graph_create_filter(&format,
767 avfilter_get_by_name("aformat"),
768 name, args.str, NULL, fg->graph);
769 if (ret < 0)
770 goto fail;
771
772 ret = avfilter_link(last_filter, pad_idx, format, 0);
773 if (ret < 0)
774 goto fail;
775
776 last_filter = format;
777 pad_idx = 0;
778 }
779
780 if (ost->apad && of->shortest) {
781 int i;
782
783 for (i = 0; i < of->nb_streams; i++)
784 if (of->streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
785 break;
786
787 if (i < of->nb_streams) {
788 AUTO_INSERT_FILTER("-apad", "apad", ost->apad);
789 }
790 }
791
792 snprintf(name, sizeof(name), "trim for output stream %d:%d",
793 ost->file_index, ost->index);
795 &last_filter, &pad_idx, name);
796 if (ret < 0)
797 goto fail;
798
799 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
800 goto fail;
801fail:
802 av_bprint_finalize(&args, NULL);
803
804 return ret;
805}
806
808 AVFilterInOut *out)
809{
810 if (!ofilter->ost) {
811 av_log(NULL, AV_LOG_FATAL, "Filter %s has an unconnected output\n", ofilter->name);
812 exit_program(1);
813 }
814
815 switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
816 case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
817 case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
818 default: av_assert0(0); return 0;
819 }
820}
821
823{
824 int i;
825 for (i = 0; i < nb_filtergraphs; i++) {
826 int n;
827 for (n = 0; n < filtergraphs[i]->nb_outputs; n++) {
828 OutputFilter *output = filtergraphs[i]->outputs[n];
829 if (!output->ost) {
830 av_log(NULL, AV_LOG_FATAL, "Filter %s has an unconnected output\n", output->name);
831 exit_program(1);
832 }
833 }
834 }
835}
836
837static int sub2video_prepare(InputStream *ist, InputFilter *ifilter)
838{
839 AVFormatContext *avf = input_files[ist->file_index]->ctx;
840 int i, w, h;
841
842 /* Compute the size of the canvas for the subtitles stream.
843 If the subtitles codecpar has set a size, use it. Otherwise use the
844 maximum dimensions of the video streams in the same file. */
845 w = ifilter->width;
846 h = ifilter->height;
847 if (!(w && h)) {
848 for (i = 0; i < avf->nb_streams; i++) {
849 if (avf->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
850 w = FFMAX(w, avf->streams[i]->codecpar->width);
851 h = FFMAX(h, avf->streams[i]->codecpar->height);
852 }
853 }
854 if (!(w && h)) {
855 w = FFMAX(w, 720);
856 h = FFMAX(h, 576);
857 }
858 av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
859 }
860 ist->sub2video.w = ifilter->width = w;
861 ist->sub2video.h = ifilter->height = h;
862
863 ifilter->width = ist->dec_ctx->width ? ist->dec_ctx->width : ist->sub2video.w;
864 ifilter->height = ist->dec_ctx->height ? ist->dec_ctx->height : ist->sub2video.h;
865
866 /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
867 palettes for all rectangles are identical or compatible */
868 ifilter->format = AV_PIX_FMT_RGB32;
869
870 ist->sub2video.frame = av_frame_alloc();
871 if (!ist->sub2video.frame)
872 return AVERROR(ENOMEM);
873 ist->sub2video.last_pts = INT64_MIN;
874 ist->sub2video.end_pts = INT64_MIN;
875
876 /* sub2video structure has been (re-)initialized.
877 Mark it as such so that the system will be
878 initialized with the first received heartbeat. */
879 ist->sub2video.initialize = 1;
880
881 return 0;
882}
883
885 AVFilterInOut *in)
886{
887 AVFilterContext *last_filter;
888 const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
889 const AVPixFmtDescriptor *desc;
890 InputStream *ist = ifilter->ist;
892 AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
893 ist->st->time_base;
894 AVRational fr = ist->framerate;
895 AVRational sar;
896 AVBPrint args;
897 char name[255];
898 int ret, pad_idx = 0;
899 int64_t tsoffset = 0;
900 AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
901
902 if (!par)
903 return AVERROR(ENOMEM);
904 memset(par, 0, sizeof(*par));
905 par->format = AV_PIX_FMT_NONE;
906
907 if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
908 av_log(NULL, AV_LOG_ERROR, "Cannot connect video filter to audio input\n");
909 ret = AVERROR(EINVAL);
910 goto fail;
911 }
912
913 if (!fr.num)
914 fr = ist->framerate_guessed;
915
916 if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
917 ret = sub2video_prepare(ist, ifilter);
918 if (ret < 0)
919 goto fail;
920 }
921
922 sar = ifilter->sample_aspect_ratio;
923 if(!sar.den)
924 sar = (AVRational){0,1};
925 av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
926 av_bprintf(&args,
927 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
928 "pixel_aspect=%d/%d",
929 ifilter->width, ifilter->height, ifilter->format,
930 tb.num, tb.den, sar.num, sar.den);
931 if (fr.num && fr.den)
932 av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
933 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
934 ist->file_index, ist->st->index);
935
936
937 if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name,
938 args.str, NULL, fg->graph)) < 0)
939 goto fail;
940 par->hw_frames_ctx = ifilter->hw_frames_ctx;
941 ret = av_buffersrc_parameters_set(ifilter->filter, par);
942 if (ret < 0)
943 goto fail;
944 av_freep(&par);
945 last_filter = ifilter->filter;
946
947 desc = av_pix_fmt_desc_get(ifilter->format);
948 av_assert0(desc);
949
950 // TODO: insert hwaccel enabled filters like transpose_vaapi into the graph
951 if (ist->autorotate && !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
952 int32_t *displaymatrix = ifilter->displaymatrix;
953 double theta;
954
955 if (!displaymatrix)
956 displaymatrix = (int32_t *)av_stream_get_side_data(ist->st, AV_PKT_DATA_DISPLAYMATRIX, NULL);
957 theta = get_rotation(displaymatrix);
958
959 if (fabs(theta - 90) < 1.0) {
960 ret = insert_filter(&last_filter, &pad_idx, "transpose",
961 displaymatrix[3] > 0 ? "cclock_flip" : "clock");
962 } else if (fabs(theta - 180) < 1.0) {
963 if (displaymatrix[0] < 0) {
964 ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
965 if (ret < 0)
966 return ret;
967 }
968 if (displaymatrix[4] < 0) {
969 ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
970 }
971 } else if (fabs(theta - 270) < 1.0) {
972 ret = insert_filter(&last_filter, &pad_idx, "transpose",
973 displaymatrix[3] < 0 ? "clock_flip" : "cclock");
974 } else if (fabs(theta) > 1.0) {
975 char rotate_buf[64];
976 snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta);
977 ret = insert_filter(&last_filter, &pad_idx, "rotate", rotate_buf);
978 } else if (fabs(theta) < 1.0) {
979 if (displaymatrix && displaymatrix[4] < 0) {
980 ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
981 }
982 }
983 if (ret < 0)
984 return ret;
985 }
986
987 snprintf(name, sizeof(name), "trim_in_%d_%d",
988 ist->file_index, ist->st->index);
989 if (copy_ts) {
990 tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
991 if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
992 tsoffset += f->ctx->start_time;
993 }
994 ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
995 AV_NOPTS_VALUE : tsoffset, f->recording_time,
996 &last_filter, &pad_idx, name);
997 if (ret < 0)
998 return ret;
999
1000 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
1001 return ret;
1002 return 0;
1003fail:
1004 av_freep(&par);
1005
1006 return ret;
1007}
1008
1010 AVFilterInOut *in)
1011{
1012 AVFilterContext *last_filter;
1013 const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
1014 InputStream *ist = ifilter->ist;
1015 InputFile *f = input_files[ist->file_index];
1016 AVBPrint args;
1017 char name[255];
1018 int ret, pad_idx = 0;
1019 int64_t tsoffset = 0;
1020
1021 if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_AUDIO) {
1022 av_log(NULL, AV_LOG_ERROR, "Cannot connect audio filter to non audio input\n");
1023 return AVERROR(EINVAL);
1024 }
1025
1026 av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
1027 av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
1028 1, ifilter->sample_rate,
1029 ifilter->sample_rate,
1030 av_get_sample_fmt_name(ifilter->format));
1031 if (av_channel_layout_check(&ifilter->ch_layout) &&
1032 ifilter->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
1033 av_bprintf(&args, ":channel_layout=");
1034 av_channel_layout_describe_bprint(&ifilter->ch_layout, &args);
1035 } else
1036 av_bprintf(&args, ":channels=%d", ifilter->ch_layout.nb_channels);
1037 snprintf(name, sizeof(name), "graph_%d_in_%d_%d", fg->index,
1038 ist->file_index, ist->st->index);
1039
1040 if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
1041 name, args.str, NULL,
1042 fg->graph)) < 0)
1043 return ret;
1044 last_filter = ifilter->filter;
1045
1046#define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
1047 AVFilterContext *filt_ctx; \
1048 \
1049 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
1050 "similarly to -af " filter_name "=%s.\n", arg); \
1051 \
1052 snprintf(name, sizeof(name), "graph_%d_%s_in_%d_%d", \
1053 fg->index, filter_name, ist->file_index, ist->st->index); \
1054 ret = avfilter_graph_create_filter(&filt_ctx, \
1055 avfilter_get_by_name(filter_name), \
1056 name, arg, NULL, fg->graph); \
1057 if (ret < 0) \
1058 return ret; \
1059 \
1060 ret = avfilter_link(last_filter, 0, filt_ctx, 0); \
1061 if (ret < 0) \
1062 return ret; \
1063 \
1064 last_filter = filt_ctx; \
1065} while (0)
1066
1067 snprintf(name, sizeof(name), "trim for input stream %d:%d",
1068 ist->file_index, ist->st->index);
1069 if (copy_ts) {
1070 tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
1071 if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
1072 tsoffset += f->ctx->start_time;
1073 }
1074 ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
1075 AV_NOPTS_VALUE : tsoffset, f->recording_time,
1076 &last_filter, &pad_idx, name);
1077 if (ret < 0)
1078 return ret;
1079
1080 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
1081 return ret;
1082
1083 return 0;
1084}
1085
1087 AVFilterInOut *in)
1088{
1089 if (!ifilter->ist->dec) {
1090 av_log(NULL, AV_LOG_ERROR,
1091 "No decoder for stream #%d:%d, filtering impossible\n",
1092 ifilter->ist->file_index, ifilter->ist->st->index);
1093 return AVERROR_DECODER_NOT_FOUND;
1094 }
1095 switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
1096 case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
1097 case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
1098 default: av_assert0(0); return 0;
1099 }
1100}
1101
1103{
1104 int i;
1105 for (i = 0; i < fg->nb_outputs; i++)
1106 fg->outputs[i]->filter = (AVFilterContext *)NULL;
1107 for (i = 0; i < fg->nb_inputs; i++)
1108 fg->inputs[i]->filter = (AVFilterContext *)NULL;
1109 avfilter_graph_free(&fg->graph);
1110}
1111
1112static int filter_is_buffersrc(const AVFilterContext *f)
1113{
1114 return f->nb_inputs == 0 &&
1115 (!strcmp(f->filter->name, "buffer") ||
1116 !strcmp(f->filter->name, "abuffer"));
1117}
1118
1119static int graph_is_meta(AVFilterGraph *graph)
1120{
1121 for (unsigned i = 0; i < graph->nb_filters; i++) {
1122 const AVFilterContext *f = graph->filters[i];
1123
1124 /* in addition to filters flagged as meta, also
1125 * disregard sinks and buffersources (but not other sources,
1126 * since they introduce data we are not aware of)
1127 */
1128 if (!((f->filter->flags & AVFILTER_FLAG_METADATA_ONLY) ||
1129 f->nb_outputs == 0 ||
1131 return 0;
1132 }
1133 return 1;
1134}
1135
1137{
1138 AVFilterInOut *inputs, *outputs, *cur;
1139 int ret, i, simple = filtergraph_is_simple(fg);
1140 const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
1141 fg->graph_desc;
1142
1144 if (!(fg->graph = avfilter_graph_alloc()))
1145 return AVERROR(ENOMEM);
1146
1147 if (simple) {
1148 OutputStream *ost = fg->outputs[0]->ost;
1149
1150 if (filter_nbthreads) {
1151 ret = av_opt_set(fg->graph, "threads", filter_nbthreads, 0);
1152 if (ret < 0)
1153 goto fail;
1154 } else {
1155 const AVDictionaryEntry *e = NULL;
1156 e = av_dict_get(ost->encoder_opts, "threads", NULL, 0);
1157 if (e)
1158 av_opt_set(fg->graph, "threads", e->value, 0);
1159 }
1160
1161 if (av_dict_count(ost->sws_dict)) {
1162 ret = av_dict_get_string(ost->sws_dict,
1163 &fg->graph->scale_sws_opts,
1164 '=', ':');
1165 if (ret < 0)
1166 goto fail;
1167 }
1168
1169 if (av_dict_count(ost->swr_opts)) {
1170 char *args;
1171 ret = av_dict_get_string(ost->swr_opts, &args, '=', ':');
1172 if (ret < 0)
1173 goto fail;
1174 av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
1175 av_free(args);
1176 }
1177 } else {
1178 fg->graph->nb_threads = filter_complex_nbthreads;
1179 }
1180
1181 if ((ret = graph_parse(fg->graph, graph_desc, &inputs, &outputs)) < 0)
1182 goto fail;
1183
1185 if (ret < 0)
1186 goto fail;
1187
1188 if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
1189 const char *num_inputs;
1190 const char *num_outputs;
1191 if (!outputs) {
1192 num_outputs = "0";
1193 } else if (outputs->next) {
1194 num_outputs = ">1";
1195 } else {
1196 num_outputs = "1";
1197 }
1198 if (!inputs) {
1199 num_inputs = "0";
1200 } else if (inputs->next) {
1201 num_inputs = ">1";
1202 } else {
1203 num_inputs = "1";
1204 }
1205 av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' was expected "
1206 "to have exactly 1 input and 1 output."
1207 " However, it had %s input(s) and %s output(s)."
1208 " Please adjust, or use a complex filtergraph (-filter_complex) instead.\n",
1209 graph_desc, num_inputs, num_outputs);
1210 ret = AVERROR(EINVAL);
1211 goto fail;
1212 }
1213
1214 for (cur = inputs, i = 0; cur; cur = cur->next, i++)
1215 if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0) {
1216 avfilter_inout_free(&inputs);
1217 avfilter_inout_free(&outputs);
1218 goto fail;
1219 }
1220 avfilter_inout_free(&inputs);
1221
1222 for (cur = outputs, i = 0; cur; cur = cur->next, i++)
1223 configure_output_filter(fg, fg->outputs[i], cur);
1224 avfilter_inout_free(&outputs);
1225
1227 avfilter_graph_set_auto_convert(fg->graph, AVFILTER_AUTO_CONVERT_NONE);
1228 if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
1229 goto fail;
1230
1231 fg->is_meta = graph_is_meta(fg->graph);
1232
1233 /* limit the lists of allowed formats to the ones selected, to
1234 * make sure they stay the same if the filtergraph is reconfigured later */
1235 for (i = 0; i < fg->nb_outputs; i++) {
1236 OutputFilter *ofilter = fg->outputs[i];
1237 AVFilterContext *sink = ofilter->filter;
1238
1239 ofilter->format = av_buffersink_get_format(sink);
1240
1241 ofilter->width = av_buffersink_get_w(sink);
1242 ofilter->height = av_buffersink_get_h(sink);
1243
1244 ofilter->sample_rate = av_buffersink_get_sample_rate(sink);
1245 av_channel_layout_uninit(&ofilter->ch_layout);
1246 ret = av_buffersink_get_ch_layout(sink, &ofilter->ch_layout);
1247 if (ret < 0)
1248 goto fail;
1249 }
1250
1251 fg->reconfiguration = 1;
1252
1253 for (i = 0; i < fg->nb_outputs; i++) {
1254 OutputStream *ost = fg->outputs[i]->ost;
1255 if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1256 !(ost->enc_ctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
1257 av_buffersink_set_frame_size(ost->filter->filter,
1258 ost->enc_ctx->frame_size);
1259 }
1260
1261 for (i = 0; i < fg->nb_inputs; i++) {
1262 AVFrame *tmp;
1263 while (av_fifo_read(fg->inputs[i]->frame_queue, &tmp, 1) >= 0) {
1264 ret = av_buffersrc_add_frame(fg->inputs[i]->filter, tmp);
1265 av_frame_free(&tmp);
1266 if (ret < 0)
1267 goto fail;
1268 }
1269 }
1270
1271 /* send the EOFs for the finished inputs */
1272 for (i = 0; i < fg->nb_inputs; i++) {
1273 if (fg->inputs[i]->eof) {
1274 ret = av_buffersrc_add_frame(fg->inputs[i]->filter, NULL);
1275 if (ret < 0)
1276 goto fail;
1277 }
1278 }
1279
1280 /* process queued up subtitle packets */
1281 for (i = 0; i < fg->nb_inputs; i++) {
1282 InputStream *ist = fg->inputs[i]->ist;
1283 if (ist->sub2video.sub_queue && ist->sub2video.frame) {
1284 AVSubtitle tmp;
1285 while (av_fifo_read(ist->sub2video.sub_queue, &tmp, 1) >= 0) {
1286 sub2video_update(ist, INT64_MIN, &tmp);
1287 avsubtitle_free(&tmp);
1288 }
1289 }
1290 }
1291
1292 return 0;
1293
1294fail:
1296 return ret;
1297}
1298
1299int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)
1300{
1301 AVFrameSideData *sd;
1302 int ret;
1303
1304 av_buffer_unref(&ifilter->hw_frames_ctx);
1305
1306 ifilter->format = frame->format;
1307
1308 ifilter->width = frame->width;
1309 ifilter->height = frame->height;
1310 ifilter->sample_aspect_ratio = frame->sample_aspect_ratio;
1311
1312 ifilter->sample_rate = frame->sample_rate;
1313 ret = av_channel_layout_copy(&ifilter->ch_layout, &frame->ch_layout);
1314 if (ret < 0)
1315 return ret;
1316
1317 av_freep(&ifilter->displaymatrix);
1318 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX);
1319 if (sd)
1320 ifilter->displaymatrix = av_memdup(sd->data, sizeof(int32_t) * 9);
1321
1322 if (frame->hw_frames_ctx) {
1323 ifilter->hw_frames_ctx = av_buffer_ref(frame->hw_frames_ctx);
1324 if (!ifilter->hw_frames_ctx)
1325 return AVERROR(ENOMEM);
1326 }
1327
1328 return 0;
1329}
1330
1332{
1333 return !fg->graph_desc;
1334}
void exit_program(int ret)
double get_rotation(int32_t *displaymatrix)
void report_and_exit(int ret)
int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
#define ALLOC_ARRAY_ELEM(array, nb_elems)
#define GROW_ARRAY(array, nb_elems)
__thread const AVIOInterruptCB int_cb
__thread OutputFile ** output_files
__thread int nb_input_files
__thread InputFile ** input_files
void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub)
__thread FilterGraph ** filtergraphs
__thread int nb_filtergraphs
InputStream * ist_iter(InputStream *prev)
__thread int filter_complex_nbthreads
__thread int copy_ts
#define DECODING_FOR_FILTER
__thread char * filter_nbthreads
int hw_device_setup_for_filter(FilterGraph *fg)
__thread int start_at_zero
__thread int auto_conversion_filters
char * file_read(const char *filename)
static void cleanup_filtergraph(FilterGraph *fg)
int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)
static int sub2video_prepare(InputStream *ist, InputFilter *ifilter)
static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
static enum AVPixelFormat * get_compliance_normal_pix_fmts(const AVCodec *codec, const enum AVPixelFormat default_formats[])
static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
static int filter_is_buffersrc(const AVFilterContext *f)
int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter, AVFilterInOut *in)
enum AVPixelFormat choose_pixel_fmt(const AVCodec *codec, enum AVPixelFormat target, int strict_std_compliance)
static int insert_trim(int64_t start_time, int64_t duration, AVFilterContext **last_filter, int *pad_idx, const char *filter_name)
static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter, AVFilterInOut *in)
static char * describe_filter_link(FilterGraph *fg, AVFilterInOut *inout, int in)
#define AUTO_INSERT_FILTER(opt_name, filter_name, arg)
static int filter_opt_apply(AVFilterContext *f, const char *key, const char *val)
static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter, AVFilterInOut *in)
static int read_binary(const char *path, uint8_t **data, int *len)
static int graph_is_meta(AVFilterGraph *graph)
int filtergraph_is_simple(FilterGraph *fg)
void check_filter_outputs(void)
static int insert_filter(AVFilterContext **last_filter, int *pad_idx, const char *filter_name, const char *args)
static const char * choose_pix_fmts(OutputFilter *ofilter, AVBPrint *bprint)
#define DEF_CHOOSE_FORMAT(name, type, var, supported_list, none, printf_format, get_name)
static int graph_parse(AVFilterGraph *graph, const char *desc, AVFilterInOut **inputs, AVFilterInOut **outputs)
int configure_filtergraph(FilterGraph *fg)
static void choose_channel_layouts(OutputFilter *ofilter, AVBPrint *bprint)
static int graph_opts_apply(AVFilterGraphSegment *seg)
int init_complex_filtergraph(FilterGraph *fg)
__thread int nb_streams
OutputFilter ** outputs
const char * graph_desc
AVFilterGraph * graph
InputFilter ** inputs
AVFormatContext * ctx
int64_t recording_time
InputStream ** streams
int64_t start_time
AVBufferRef * hw_frames_ctx
uint8_t * name
struct InputStream * ist
int32_t * displaymatrix
AVFifo * frame_queue
AVFilterContext * filter
AVChannelLayout ch_layout
enum AVMediaType type
struct FilterGraph * graph
AVRational sample_aspect_ratio
unsigned int initialize
marks if sub2video_update should force an initialization
AVFifo * sub_queue
queue of AVSubtitle* before filter init
AVCodecContext * dec_ctx
struct InputStream::sub2video sub2video
AVStream * st
InputFilter ** filters
AVRational framerate_guessed
const AVCodec * dec
AVRational framerate
OutputStream ** streams
int64_t start_time
start time in microseconds == AV_TIME_BASE units
int64_t recording_time
desired length of the resulting file in microseconds == AV_TIME_BASE units
AVFilterInOut * out_tmp
struct OutputStream * ost
AVFilterContext * filter
struct FilterGraph * graph
AVChannelLayout ch_layout
enum AVMediaType type
AVDictionary * swr_opts
int * audio_channels_map
AVRational frame_rate
AVCodecContext * enc_ctx
AVDictionary * encoder_opts
AVStream * st
AVDictionary * sws_dict
OutputFilter * filter