35#include "libavfilter/avfilter.h"
36#include "libavfilter/buffersink.h"
37#include "libavfilter/buffersrc.h"
39#include "libavutil/avassert.h"
40#include "libavutil/avstring.h"
41#include "libavutil/bprint.h"
42#include "libavutil/channel_layout.h"
43#include "libavutil/display.h"
44#include "libavutil/opt.h"
45#include "libavutil/pixdesc.h"
46#include "libavutil/pixfmt.h"
47#include "libavutil/imgutils.h"
48#include "libavutil/samplefmt.h"
54 static const enum AVPixelFormat mjpeg_formats[] =
55 { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
58 if (!strcmp(codec->name,
"mjpeg")) {
61 return default_formats;
65enum AVPixelFormat
choose_pixel_fmt(AVStream *st, AVCodecContext *enc_ctx,
const AVCodec *codec,
enum AVPixelFormat target)
67 if (codec && codec->pix_fmts) {
68 const enum AVPixelFormat *p = codec->pix_fmts;
69 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
71 int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
72 enum AVPixelFormat best= AV_PIX_FMT_NONE;
74 if (enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
77 for (; *p != AV_PIX_FMT_NONE; p++) {
78 best = av_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
82 if (*p == AV_PIX_FMT_NONE) {
83 if (target != AV_PIX_FMT_NONE)
84 av_log(NULL, AV_LOG_WARNING,
85 "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
86 av_get_pix_fmt_name(target),
88 av_get_pix_fmt_name(best));
98 AVDictionaryEntry *strict_dict = av_dict_get(
ost->
encoder_opts,
"strict", NULL, 0);
101 av_opt_set(
ost->
enc_ctx,
"strict", strict_dict->value, 0);
104 avfilter_graph_set_auto_convert(ofilter->
graph->
graph,
105 AVFILTER_AUTO_CONVERT_NONE);
108 return av_strdup(av_get_pix_fmt_name(
ost->
enc_ctx->pix_fmt));
110 if (
ost->
enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
113 const enum AVPixelFormat *p;
114 AVIOContext *s = NULL;
118 if (avio_open_dyn_buf(&s) < 0)
122 if (
ost->
enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
126 for (; *p != AV_PIX_FMT_NONE; p++) {
127 const char *name = av_get_pix_fmt_name(*p);
128 avio_printf(s,
"%s|", name);
130 len = avio_close_dyn_buf(s, &ret);
139#define DEF_CHOOSE_FORMAT(name, type, var, supported_list, none, printf_format, get_name) \
140static void choose_ ## name (OutputFilter *ofilter, AVBPrint *bprint) \
142 if (ofilter->var == none && !ofilter->supported_list) \
144 av_bprintf(bprint, #name "="); \
145 if (ofilter->var != none) { \
146 av_bprintf(bprint, printf_format, get_name(ofilter->var)); \
150 for (p = ofilter->supported_list; *p != none; p++) { \
151 av_bprintf(bprint, printf_format "|", get_name(*p)); \
153 if (bprint->len > 0) \
154 bprint->str[--bprint->len] = '\0'; \
156 av_bprint_chars(bprint, ':', 1); \
163 AV_SAMPLE_FMT_NONE,
"%s", av_get_sample_fmt_name)
210 AVFilterContext *ctx = inout->filter_ctx;
211 AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads;
212 int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs;
216 if (avio_open_dyn_buf(&pb) < 0)
219 avio_printf(pb,
"%s", ctx->filter->name);
221 avio_printf(pb,
":%s", avfilter_pad_get_name(pads, inout->pad_idx));
223 avio_close_dyn_buf(pb, &res);
230 enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
234 if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
235 av_log(NULL, AV_LOG_FATAL,
"Only video and audio filters supported "
244 int file_idx = strtol(in->name, &p, 0);
247 av_log(NULL, AV_LOG_FATAL,
"Invalid file index %d in filtergraph description %s.\n",
253 for (i = 0; i < s->nb_streams; i++) {
254 enum AVMediaType stream_type = s->streams[i]->codecpar->codec_type;
255 if (stream_type != type &&
256 !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
257 type == AVMEDIA_TYPE_VIDEO ))
265 av_log(NULL, AV_LOG_FATAL,
"Stream specifier '%s' in filtergraph description %s "
270 if (
ist->user_set_discard == AVDISCARD_ALL) {
271 av_log(NULL, AV_LOG_FATAL,
"Stream specifier '%s' in filtergraph description %s "
272 "matches a disabled input stream.\n", p, fg->
graph_desc);
279 if (
ist->user_set_discard == AVDISCARD_ALL)
281 if (
ist->dec_ctx->codec_type == type &&
ist->discard)
285 av_log(NULL, AV_LOG_FATAL,
"Cannot find a matching stream for "
286 "unlabeled input pad %d on filter %s\n", in->pad_idx,
287 in->filter_ctx->name);
295 ist->st->discard = AVDISCARD_NONE;
316 AVFilterInOut *inputs, *outputs, *cur;
317 AVFilterGraph *
graph;
322 graph = avfilter_graph_alloc();
324 return AVERROR(ENOMEM);
325 graph->nb_threads = 1;
331 for (cur = inputs; cur; cur = cur->next)
334 for (cur = outputs; cur;) {
350 avfilter_inout_free(&inputs);
351 avfilter_graph_free(&
graph);
356 AVFilterContext **last_filter,
int *pad_idx,
357 const char *filter_name)
359 AVFilterGraph *
graph = (*last_filter)->graph;
360 AVFilterContext *ctx;
361 const AVFilter *trim;
362 enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
363 const char *name = (type == AVMEDIA_TYPE_VIDEO) ?
"trim" :
"atrim";
366 if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
369 trim = avfilter_get_by_name(name);
371 av_log(NULL, AV_LOG_ERROR,
"%s filter not present, cannot limit "
372 "recording time.\n", name);
373 return AVERROR_FILTER_NOT_FOUND;
376 ctx = avfilter_graph_alloc_filter(
graph, trim, filter_name);
378 return AVERROR(ENOMEM);
380 if (duration != INT64_MAX) {
381 ret = av_opt_set_int(ctx,
"durationi", duration,
382 AV_OPT_SEARCH_CHILDREN);
384 if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
385 ret = av_opt_set_int(ctx,
"starti", start_time,
386 AV_OPT_SEARCH_CHILDREN);
389 av_log(ctx, AV_LOG_ERROR,
"Error configuring the %s filter", name);
393 ret = avfilter_init_str(ctx, NULL);
397 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
407 const char *filter_name,
const char *args)
409 AVFilterGraph *
graph = (*last_filter)->graph;
410 AVFilterContext *ctx;
413 ret = avfilter_graph_create_filter(&ctx,
414 avfilter_get_by_name(filter_name),
415 filter_name, args, NULL,
graph);
419 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
433 AVFilterContext *last_filter = out->filter_ctx;
434 int pad_idx = out->pad_idx;
439 ret = avfilter_graph_create_filter(&ofilter->
filter,
440 avfilter_get_by_name(
"buffersink"),
441 name, NULL, NULL, fg->
graph);
449 AVDictionaryEntry *e = NULL;
451 snprintf(args,
sizeof(args),
"%d:%d",
455 AV_DICT_IGNORE_SUFFIX))) {
456 av_strlcatf(args,
sizeof(args),
":%s=%s", e->key, e->value);
459 snprintf(name,
sizeof(name),
"scaler_out_%d_%d",
461 if ((ret = avfilter_graph_create_filter(&
filter, avfilter_get_by_name(
"scale"),
462 name, args, NULL, fg->
graph)) < 0)
464 if ((ret = avfilter_link(last_filter, pad_idx,
filter, 0)) < 0)
474 ret = avfilter_graph_create_filter(&
filter,
475 avfilter_get_by_name(
"format"),
476 "format", pix_fmts, NULL, fg->
graph);
480 if ((ret = avfilter_link(last_filter, pad_idx,
filter, 0)) < 0)
488 AVFilterContext *fps;
491 snprintf(args,
sizeof(args),
"fps=%d/%d",
ost->
frame_rate.num,
493 snprintf(name,
sizeof(name),
"fps_out_%d_%d",
495 ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name(
"fps"),
496 name, args, NULL, fg->
graph);
500 ret = avfilter_link(last_filter, pad_idx, fps, 0);
507 snprintf(name,
sizeof(name),
"trim_out_%d_%d",
510 &last_filter, &pad_idx, name);
515 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->
filter, 0)) < 0)
526 AVFilterContext *last_filter = out->filter_ctx;
527 int pad_idx = out->pad_idx;
533 ret = avfilter_graph_create_filter(&ofilter->
filter,
534 avfilter_get_by_name(
"abuffersink"),
535 name, NULL, NULL, fg->
graph);
538 if ((ret = av_opt_set_int(ofilter->
filter,
"all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
541#define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
542 AVFilterContext *filt_ctx; \
544 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
545 "similarly to -af " filter_name "=%s.\n", arg); \
547 ret = avfilter_graph_create_filter(&filt_ctx, \
548 avfilter_get_by_name(filter_name), \
549 filter_name, arg, NULL, fg->graph); \
553 ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
557 last_filter = filt_ctx; \
560 av_bprint_init(&args, 0, AV_BPRINT_SIZE_UNLIMITED);
563 av_bprintf(&args,
"0x%"PRIx64,
570 av_bprint_clear(&args);
573 if (codec->channels && !codec->channel_layout)
574 codec->channel_layout = av_get_default_channel_layout(codec->channels);
576 choose_sample_fmts(ofilter, &args);
577 choose_sample_rates(ofilter, &args);
578 choose_channel_layouts(ofilter, &args);
579 if (!av_bprint_is_complete(&args)) {
580 ret = AVERROR(ENOMEM);
586 snprintf(name,
sizeof(name),
"format_out_%d_%d",
588 ret = avfilter_graph_create_filter(&
format,
589 avfilter_get_by_name(
"aformat"),
590 name, args.str, NULL, fg->
graph);
594 ret = avfilter_link(last_filter, pad_idx,
format, 0);
605 for (i=0; i<of->
ctx->nb_streams; i++)
606 if (of->
ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
609 if (i<of->ctx->nb_streams) {
614 snprintf(name,
sizeof(name),
"trim for output stream %d:%d",
617 &last_filter, &pad_idx, name);
621 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->
filter, 0)) < 0)
624 av_bprint_finalize(&args, NULL);
632 av_log(NULL, AV_LOG_FATAL,
"Filter %s has an unconnected output\n", ofilter->
name);
636 switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
639 default: av_assert0(0);
return 0;
651 av_log(NULL, AV_LOG_FATAL,
"Filter %s has an unconnected output\n", output->
name);
669 for (i = 0; i < avf->nb_streams; i++) {
670 if (avf->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
671 w = FFMAX(w, avf->streams[i]->codecpar->width);
672 h = FFMAX(h, avf->streams[i]->codecpar->height);
679 av_log(avf, AV_LOG_INFO,
"sub2video: using %dx%d canvas\n", w, h);
681 ist->sub2video.w = ifilter->
width = w;
684 ifilter->
width =
ist->dec_ctx->width ?
ist->dec_ctx->width :
ist->sub2video.w;
685 ifilter->
height =
ist->dec_ctx->height ?
ist->dec_ctx->height :
ist->sub2video.h;
689 ifilter->
format = AV_PIX_FMT_RGB32;
691 ist->sub2video.frame = av_frame_alloc();
692 if (!
ist->sub2video.frame)
693 return AVERROR(ENOMEM);
694 ist->sub2video.last_pts = INT64_MIN;
695 ist->sub2video.end_pts = INT64_MIN;
700 ist->sub2video.initialize = 1;
708 AVFilterContext *last_filter;
709 const AVFilter *buffer_filt = avfilter_get_by_name(
"buffer");
712 AVRational tb =
ist->framerate.num ? av_inv_q(
ist->framerate) :
714 AVRational fr =
ist->framerate;
718 int ret, pad_idx = 0;
719 int64_t tsoffset = 0;
720 AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
723 return AVERROR(ENOMEM);
724 memset(par, 0,
sizeof(*par));
725 par->format = AV_PIX_FMT_NONE;
727 if (
ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
728 av_log(NULL, AV_LOG_ERROR,
"Cannot connect video filter to audio input\n");
729 ret = AVERROR(EINVAL);
736 if (
ist->dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
744 sar = (AVRational){0,1};
745 av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
747 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
748 "pixel_aspect=%d/%d",
750 tb.num, tb.den, sar.num, sar.den);
751 if (fr.num && fr.den)
752 av_bprintf(&args,
":frame_rate=%d/%d", fr.num, fr.den);
753 snprintf(name,
sizeof(name),
"graph %d input from stream %d:%d", fg->
index,
754 ist->file_index,
ist->st->index);
757 if ((ret = avfilter_graph_create_filter(&ifilter->
filter, buffer_filt, name,
758 args.str, NULL, fg->
graph)) < 0)
761 ret = av_buffersrc_parameters_set(ifilter->
filter, par);
765 last_filter = ifilter->
filter;
767 if (
ist->autorotate) {
770 if (fabs(theta - 90) < 1.0) {
771 ret =
insert_filter(&last_filter, &pad_idx,
"transpose",
"clock");
772 }
else if (fabs(theta - 180) < 1.0) {
777 }
else if (fabs(theta - 270) < 1.0) {
778 ret =
insert_filter(&last_filter, &pad_idx,
"transpose",
"cclock");
779 }
else if (fabs(theta) > 1.0) {
781 snprintf(rotate_buf,
sizeof(rotate_buf),
"%f*PI/180", theta);
782 ret =
insert_filter(&last_filter, &pad_idx,
"rotate", rotate_buf);
789 AVFilterContext *yadif;
791 snprintf(name,
sizeof(name),
"deinterlace_in_%d_%d",
792 ist->file_index,
ist->st->index);
793 if ((ret = avfilter_graph_create_filter(&yadif,
794 avfilter_get_by_name(
"yadif"),
799 if ((ret = avfilter_link(last_filter, 0, yadif, 0)) < 0)
805 snprintf(name,
sizeof(name),
"trim_in_%d_%d",
806 ist->file_index,
ist->st->index);
810 tsoffset += f->
ctx->start_time;
814 &last_filter, &pad_idx, name);
818 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
830 AVFilterContext *last_filter;
831 const AVFilter *abuffer_filt = avfilter_get_by_name(
"abuffer");
836 int ret, pad_idx = 0;
837 int64_t tsoffset = 0;
839 if (
ist->dec_ctx->codec_type != AVMEDIA_TYPE_AUDIO) {
840 av_log(NULL, AV_LOG_ERROR,
"Cannot connect audio filter to non audio input\n");
841 return AVERROR(EINVAL);
844 av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
845 av_bprintf(&args,
"time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
848 av_get_sample_fmt_name(ifilter->
format));
850 av_bprintf(&args,
":channel_layout=0x%"PRIx64,
853 av_bprintf(&args,
":channels=%d", ifilter->
channels);
854 snprintf(name,
sizeof(name),
"graph_%d_in_%d_%d", fg->
index,
855 ist->file_index,
ist->st->index);
857 if ((ret = avfilter_graph_create_filter(&ifilter->
filter, abuffer_filt,
858 name, args.str, NULL,
861 last_filter = ifilter->
filter;
863#define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
864 AVFilterContext *filt_ctx; \
866 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
867 "similarly to -af " filter_name "=%s.\n", arg); \
869 snprintf(name, sizeof(name), "graph_%d_%s_in_%d_%d", \
870 fg->index, filter_name, ist->file_index, ist->st->index); \
871 ret = avfilter_graph_create_filter(&filt_ctx, \
872 avfilter_get_by_name(filter_name), \
873 name, arg, NULL, fg->graph); \
877 ret = avfilter_link(last_filter, 0, filt_ctx, 0); \
881 last_filter = filt_ctx; \
885 char args[256] = {0};
891 av_strlcatf(args,
sizeof(args),
":first_pts=0");
911 av_log(NULL, AV_LOG_WARNING,
"-vol has been deprecated. Use the volume "
912 "audio filter instead.\n");
914 snprintf(args,
sizeof(args),
"%f",
audio_volume / 256.);
918 snprintf(name,
sizeof(name),
"trim for input stream %d:%d",
919 ist->file_index,
ist->st->index);
923 tsoffset += f->
ctx->start_time;
927 &last_filter, &pad_idx, name);
931 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
941 av_log(NULL, AV_LOG_ERROR,
942 "No decoder for stream #%d:%d, filtering impossible\n",
944 return AVERROR_DECODER_NOT_FOUND;
946 switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
949 default: av_assert0(0);
return 0;
960 avfilter_graph_free(&fg->
graph);
965 AVFilterInOut *inputs, *outputs, *cur;
971 if (!(fg->
graph = avfilter_graph_alloc()))
972 return AVERROR(ENOMEM);
977 AVDictionaryEntry *e = NULL;
983 AV_DICT_IGNORE_SUFFIX))) {
984 av_strlcatf(args,
sizeof(args),
"%s=%s:", e->key, e->value);
987 args[strlen(args)-1] = 0;
989 if (!strncmp(args,
"sws_flags=", 10)) {
991 fg->
graph->scale_sws_opts = av_strdup(args+4);
996 AV_DICT_IGNORE_SUFFIX))) {
997 av_strlcatf(args,
sizeof(args),
"%s=%s:", e->key, e->value);
1000 args[strlen(args)-1] = 0;
1001 av_opt_set(fg->
graph,
"aresample_swr_opts", args, 0);
1005 AV_DICT_IGNORE_SUFFIX))) {
1006 av_strlcatf(args,
sizeof(args),
"%s=%s:", e->key, e->value);
1009 args[strlen(args) - 1] =
'\0';
1013 av_opt_set(fg->
graph,
"threads", e->value, 0);
1018 if ((ret = avfilter_graph_parse2(fg->
graph, graph_desc, &inputs, &outputs)) < 0)
1025 if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
1026 const char *num_inputs;
1027 const char *num_outputs;
1030 }
else if (outputs->next) {
1037 }
else if (inputs->next) {
1042 av_log(NULL, AV_LOG_ERROR,
"Simple filtergraph '%s' was expected "
1043 "to have exactly 1 input and 1 output."
1044 " However, it had %s input(s) and %s output(s)."
1045 " Please adjust, or use a complex filtergraph (-filter_complex) instead.\n",
1046 graph_desc, num_inputs, num_outputs);
1047 ret = AVERROR(EINVAL);
1051 for (cur = inputs, i = 0; cur; cur = cur->next, i++)
1053 avfilter_inout_free(&inputs);
1054 avfilter_inout_free(&outputs);
1057 avfilter_inout_free(&inputs);
1059 for (cur = outputs, i = 0; cur; cur = cur->next, i++)
1061 avfilter_inout_free(&outputs);
1064 avfilter_graph_set_auto_convert(fg->
graph, AVFILTER_AUTO_CONVERT_NONE);
1065 if ((ret = avfilter_graph_config(fg->
graph, NULL)) < 0)
1072 AVFilterContext *sink = ofilter->
filter;
1074 ofilter->
format = av_buffersink_get_format(sink);
1076 ofilter->
width = av_buffersink_get_w(sink);
1077 ofilter->
height = av_buffersink_get_h(sink);
1079 ofilter->
sample_rate = av_buffersink_get_sample_rate(sink);
1080 ofilter->
channel_layout = av_buffersink_get_channel_layout(sink);
1090 av_log(NULL, AV_LOG_ERROR,
"Encoder (codec %s) not found for output stream #%d:%d\n",
1092 ret = AVERROR(EINVAL);
1095 if (
ost->
enc->type == AVMEDIA_TYPE_AUDIO &&
1096 !(
ost->
enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
1105 ret = av_buffersrc_add_frame(fg->
inputs[i]->
filter, tmp);
1106 av_frame_free(&tmp);
1115 ret = av_buffersrc_add_frame(fg->
inputs[i]->
filter, NULL);
1124 if (
ist->sub2video.sub_queue &&
ist->sub2video.frame) {
1125 while (av_fifo_size(
ist->sub2video.sub_queue)) {
1127 av_fifo_generic_read(
ist->sub2video.sub_queue, &tmp,
sizeof(tmp), NULL);
1129 avsubtitle_free(&tmp);
1145 ifilter->
format = frame->format;
1147 ifilter->
width = frame->width;
1148 ifilter->
height = frame->height;
1152 ifilter->
channels = frame->channels;
1155 if (frame->hw_frames_ctx) {
1156 ifilter->
hw_frames_ctx = av_buffer_ref(frame->hw_frames_ctx);
1158 return AVERROR(ENOMEM);
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
struct OutputStream * ost
struct FilterGraph * graph
int audio_channels_mapped
AVDictionary * resample_opts
AVDictionary * encoder_opts