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;
66 const AVCodec *codec,
enum AVPixelFormat target)
68 if (codec && codec->pix_fmts) {
69 const enum AVPixelFormat *p = codec->pix_fmts;
70 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
72 int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
73 enum AVPixelFormat best= AV_PIX_FMT_NONE;
75 if (enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
78 for (; *p != AV_PIX_FMT_NONE; p++) {
79 best = av_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
83 if (*p == AV_PIX_FMT_NONE) {
84 if (target != AV_PIX_FMT_NONE)
85 av_log(NULL, AV_LOG_WARNING,
86 "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
87 av_get_pix_fmt_name(target),
89 av_get_pix_fmt_name(best));
99 AVDictionaryEntry *strict_dict = av_dict_get(
ost->
encoder_opts,
"strict", NULL, 0);
102 av_opt_set(
ost->
enc_ctx,
"strict", strict_dict->value, 0);
105 avfilter_graph_set_auto_convert(ofilter->
graph->
graph,
106 AVFILTER_AUTO_CONVERT_NONE);
109 return av_strdup(av_get_pix_fmt_name(
ost->
enc_ctx->pix_fmt));
111 if (
ost->
enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
114 const enum AVPixelFormat *p;
115 AVIOContext *s = NULL;
119 if (avio_open_dyn_buf(&s) < 0)
123 if (
ost->
enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
127 for (; *p != AV_PIX_FMT_NONE; p++) {
128 const char *name = av_get_pix_fmt_name(*p);
129 avio_printf(s,
"%s|", name);
131 len = avio_close_dyn_buf(s, &ret);
140#define DEF_CHOOSE_FORMAT(name, type, var, supported_list, none, printf_format, get_name) \
141static void choose_ ## name (OutputFilter *ofilter, AVBPrint *bprint) \
143 if (ofilter->var == none && !ofilter->supported_list) \
145 av_bprintf(bprint, #name "="); \
146 if (ofilter->var != none) { \
147 av_bprintf(bprint, printf_format, get_name(ofilter->var)); \
151 for (p = ofilter->supported_list; *p != none; p++) { \
152 av_bprintf(bprint, printf_format "|", get_name(*p)); \
154 if (bprint->len > 0) \
155 bprint->str[--bprint->len] = '\0'; \
157 av_bprint_chars(bprint, ':', 1); \
164 AV_SAMPLE_FMT_NONE,
"%s", av_get_sample_fmt_name)
211 AVFilterContext *ctx = inout->filter_ctx;
212 AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads;
213 int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs;
217 if (avio_open_dyn_buf(&pb) < 0)
220 avio_printf(pb,
"%s", ctx->filter->name);
222 avio_printf(pb,
":%s", avfilter_pad_get_name(pads, inout->pad_idx));
224 avio_close_dyn_buf(pb, &res);
231 enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
235 if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
236 av_log(NULL, AV_LOG_FATAL,
"Only video and audio filters supported "
245 int file_idx = strtol(in->name, &p, 0);
248 av_log(NULL, AV_LOG_FATAL,
"Invalid file index %d in filtergraph description %s.\n",
254 for (i = 0; i < s->nb_streams; i++) {
255 enum AVMediaType stream_type = s->streams[i]->codecpar->codec_type;
256 if (stream_type != type &&
257 !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
258 type == AVMEDIA_TYPE_VIDEO ))
266 av_log(NULL, AV_LOG_FATAL,
"Stream specifier '%s' in filtergraph description %s "
271 if (
ist->user_set_discard == AVDISCARD_ALL) {
272 av_log(NULL, AV_LOG_FATAL,
"Stream specifier '%s' in filtergraph description %s "
273 "matches a disabled input stream.\n", p, fg->
graph_desc);
280 if (
ist->user_set_discard == AVDISCARD_ALL)
282 if (
ist->dec_ctx->codec_type == type &&
ist->discard)
286 av_log(NULL, AV_LOG_FATAL,
"Cannot find a matching stream for "
287 "unlabeled input pad %d on filter %s\n", in->pad_idx,
288 in->filter_ctx->name);
296 ist->st->discard = AVDISCARD_NONE;
317 AVFilterInOut *inputs, *outputs, *cur;
318 AVFilterGraph *
graph;
323 graph = avfilter_graph_alloc();
325 return AVERROR(ENOMEM);
326 graph->nb_threads = 1;
332 for (cur = inputs; cur; cur = cur->next)
335 for (cur = outputs; cur;) {
351 avfilter_inout_free(&inputs);
352 avfilter_graph_free(&
graph);
357 AVFilterContext **last_filter,
int *pad_idx,
358 const char *filter_name)
360 AVFilterGraph *
graph = (*last_filter)->graph;
361 AVFilterContext *ctx;
362 const AVFilter *trim;
363 enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
364 const char *name = (type == AVMEDIA_TYPE_VIDEO) ?
"trim" :
"atrim";
367 if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
370 trim = avfilter_get_by_name(name);
372 av_log(NULL, AV_LOG_ERROR,
"%s filter not present, cannot limit "
373 "recording time.\n", name);
374 return AVERROR_FILTER_NOT_FOUND;
377 ctx = avfilter_graph_alloc_filter(
graph, trim, filter_name);
379 return AVERROR(ENOMEM);
381 if (duration != INT64_MAX) {
382 ret = av_opt_set_int(ctx,
"durationi", duration,
383 AV_OPT_SEARCH_CHILDREN);
385 if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
386 ret = av_opt_set_int(ctx,
"starti", start_time,
387 AV_OPT_SEARCH_CHILDREN);
390 av_log(ctx, AV_LOG_ERROR,
"Error configuring the %s filter", name);
394 ret = avfilter_init_str(ctx, NULL);
398 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
408 const char *filter_name,
const char *args)
410 AVFilterGraph *
graph = (*last_filter)->graph;
411 AVFilterContext *ctx;
414 ret = avfilter_graph_create_filter(&ctx,
415 avfilter_get_by_name(filter_name),
416 filter_name, args, NULL,
graph);
420 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
434 AVFilterContext *last_filter = out->filter_ctx;
435 int pad_idx = out->pad_idx;
440 ret = avfilter_graph_create_filter(&ofilter->
filter,
441 avfilter_get_by_name(
"buffersink"),
442 name, NULL, NULL, fg->
graph);
450 AVDictionaryEntry *e = NULL;
452 snprintf(args,
sizeof(args),
"%d:%d",
456 AV_DICT_IGNORE_SUFFIX))) {
457 av_strlcatf(args,
sizeof(args),
":%s=%s", e->key, e->value);
460 snprintf(name,
sizeof(name),
"scaler_out_%d_%d",
462 if ((ret = avfilter_graph_create_filter(&
filter, avfilter_get_by_name(
"scale"),
463 name, args, NULL, fg->
graph)) < 0)
465 if ((ret = avfilter_link(last_filter, pad_idx,
filter, 0)) < 0)
475 ret = avfilter_graph_create_filter(&
filter,
476 avfilter_get_by_name(
"format"),
477 "format", pix_fmts, NULL, fg->
graph);
481 if ((ret = avfilter_link(last_filter, pad_idx,
filter, 0)) < 0)
489 AVFilterContext *fps;
492 snprintf(args,
sizeof(args),
"fps=%d/%d",
ost->
frame_rate.num,
494 snprintf(name,
sizeof(name),
"fps_out_%d_%d",
496 ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name(
"fps"),
497 name, args, NULL, fg->
graph);
501 ret = avfilter_link(last_filter, pad_idx, fps, 0);
508 snprintf(name,
sizeof(name),
"trim_out_%d_%d",
511 &last_filter, &pad_idx, name);
516 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->
filter, 0)) < 0)
527 AVFilterContext *last_filter = out->filter_ctx;
528 int pad_idx = out->pad_idx;
534 ret = avfilter_graph_create_filter(&ofilter->
filter,
535 avfilter_get_by_name(
"abuffersink"),
536 name, NULL, NULL, fg->
graph);
539 if ((ret = av_opt_set_int(ofilter->
filter,
"all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
542#define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
543 AVFilterContext *filt_ctx; \
545 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
546 "similarly to -af " filter_name "=%s.\n", arg); \
548 ret = avfilter_graph_create_filter(&filt_ctx, \
549 avfilter_get_by_name(filter_name), \
550 filter_name, arg, NULL, fg->graph); \
554 ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
558 last_filter = filt_ctx; \
561 av_bprint_init(&args, 0, AV_BPRINT_SIZE_UNLIMITED);
564 av_bprintf(&args,
"0x%"PRIx64,
571 av_bprint_clear(&args);
574 if (codec->channels && !codec->channel_layout)
575 codec->channel_layout = av_get_default_channel_layout(codec->channels);
577 choose_sample_fmts(ofilter, &args);
578 choose_sample_rates(ofilter, &args);
579 choose_channel_layouts(ofilter, &args);
580 if (!av_bprint_is_complete(&args)) {
581 ret = AVERROR(ENOMEM);
587 snprintf(name,
sizeof(name),
"format_out_%d_%d",
589 ret = avfilter_graph_create_filter(&
format,
590 avfilter_get_by_name(
"aformat"),
591 name, args.str, NULL, fg->
graph);
595 ret = avfilter_link(last_filter, pad_idx,
format, 0);
606 for (i=0; i<of->
ctx->nb_streams; i++)
607 if (of->
ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
610 if (i<of->ctx->nb_streams) {
615 snprintf(name,
sizeof(name),
"trim for output stream %d:%d",
618 &last_filter, &pad_idx, name);
622 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->
filter, 0)) < 0)
625 av_bprint_finalize(&args, NULL);
634 av_log(NULL, AV_LOG_FATAL,
"Filter %s has an unconnected output\n", ofilter->
name);
638 switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
641 default: av_assert0(0);
return 0;
653 av_log(NULL, AV_LOG_FATAL,
"Filter %s has an unconnected output\n", output->
name);
671 for (i = 0; i < avf->nb_streams; i++) {
672 if (avf->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
673 w = FFMAX(w, avf->streams[i]->codecpar->width);
674 h = FFMAX(h, avf->streams[i]->codecpar->height);
681 av_log(avf, AV_LOG_INFO,
"sub2video: using %dx%d canvas\n", w, h);
683 ist->sub2video.w = ifilter->
width = w;
686 ifilter->
width =
ist->dec_ctx->width ?
ist->dec_ctx->width :
ist->sub2video.w;
687 ifilter->
height =
ist->dec_ctx->height ?
ist->dec_ctx->height :
ist->sub2video.h;
691 ifilter->
format = AV_PIX_FMT_RGB32;
693 ist->sub2video.frame = av_frame_alloc();
694 if (!
ist->sub2video.frame)
695 return AVERROR(ENOMEM);
696 ist->sub2video.last_pts = INT64_MIN;
697 ist->sub2video.end_pts = INT64_MIN;
702 ist->sub2video.initialize = 1;
710 AVFilterContext *last_filter;
711 const AVFilter *buffer_filt = avfilter_get_by_name(
"buffer");
714 AVRational tb =
ist->framerate.num ? av_inv_q(
ist->framerate) :
716 AVRational fr =
ist->framerate;
720 int ret, pad_idx = 0;
721 int64_t tsoffset = 0;
722 AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
725 return AVERROR(ENOMEM);
726 memset(par, 0,
sizeof(*par));
727 par->format = AV_PIX_FMT_NONE;
729 if (
ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
730 av_log(NULL, AV_LOG_ERROR,
"Cannot connect video filter to audio input\n");
731 ret = AVERROR(EINVAL);
738 if (
ist->dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
746 sar = (AVRational){0,1};
747 av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
749 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
750 "pixel_aspect=%d/%d",
752 tb.num, tb.den, sar.num, sar.den);
753 if (fr.num && fr.den)
754 av_bprintf(&args,
":frame_rate=%d/%d", fr.num, fr.den);
755 snprintf(name,
sizeof(name),
"graph %d input from stream %d:%d", fg->
index,
756 ist->file_index,
ist->st->index);
759 if ((ret = avfilter_graph_create_filter(&ifilter->
filter, buffer_filt, name,
760 args.str, NULL, fg->
graph)) < 0)
763 ret = av_buffersrc_parameters_set(ifilter->
filter, par);
767 last_filter = ifilter->
filter;
769 if (
ist->autorotate) {
772 if (fabs(theta - 90) < 1.0) {
773 ret =
insert_filter(&last_filter, &pad_idx,
"transpose",
"clock");
774 }
else if (fabs(theta - 180) < 1.0) {
779 }
else if (fabs(theta - 270) < 1.0) {
780 ret =
insert_filter(&last_filter, &pad_idx,
"transpose",
"cclock");
781 }
else if (fabs(theta) > 1.0) {
783 snprintf(rotate_buf,
sizeof(rotate_buf),
"%f*PI/180", theta);
784 ret =
insert_filter(&last_filter, &pad_idx,
"rotate", rotate_buf);
791 AVFilterContext *yadif;
793 snprintf(name,
sizeof(name),
"deinterlace_in_%d_%d",
794 ist->file_index,
ist->st->index);
795 if ((ret = avfilter_graph_create_filter(&yadif,
796 avfilter_get_by_name(
"yadif"),
801 if ((ret = avfilter_link(last_filter, 0, yadif, 0)) < 0)
807 snprintf(name,
sizeof(name),
"trim_in_%d_%d",
808 ist->file_index,
ist->st->index);
812 tsoffset += f->
ctx->start_time;
816 &last_filter, &pad_idx, name);
820 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
832 AVFilterContext *last_filter;
833 const AVFilter *abuffer_filt = avfilter_get_by_name(
"abuffer");
838 int ret, pad_idx = 0;
839 int64_t tsoffset = 0;
841 if (
ist->dec_ctx->codec_type != AVMEDIA_TYPE_AUDIO) {
842 av_log(NULL, AV_LOG_ERROR,
"Cannot connect audio filter to non audio input\n");
843 return AVERROR(EINVAL);
846 av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
847 av_bprintf(&args,
"time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
850 av_get_sample_fmt_name(ifilter->
format));
852 av_bprintf(&args,
":channel_layout=0x%"PRIx64,
855 av_bprintf(&args,
":channels=%d", ifilter->
channels);
856 snprintf(name,
sizeof(name),
"graph_%d_in_%d_%d", fg->
index,
857 ist->file_index,
ist->st->index);
859 if ((ret = avfilter_graph_create_filter(&ifilter->
filter, abuffer_filt,
860 name, args.str, NULL,
863 last_filter = ifilter->
filter;
865#define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
866 AVFilterContext *filt_ctx; \
868 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
869 "similarly to -af " filter_name "=%s.\n", arg); \
871 snprintf(name, sizeof(name), "graph_%d_%s_in_%d_%d", \
872 fg->index, filter_name, ist->file_index, ist->st->index); \
873 ret = avfilter_graph_create_filter(&filt_ctx, \
874 avfilter_get_by_name(filter_name), \
875 name, arg, NULL, fg->graph); \
879 ret = avfilter_link(last_filter, 0, filt_ctx, 0); \
883 last_filter = filt_ctx; \
887 char args[256] = {0};
893 av_strlcatf(args,
sizeof(args),
":first_pts=0");
913 av_log(NULL, AV_LOG_WARNING,
"-vol has been deprecated. Use the volume "
914 "audio filter instead.\n");
916 snprintf(args,
sizeof(args),
"%f",
audio_volume / 256.);
920 snprintf(name,
sizeof(name),
"trim for input stream %d:%d",
921 ist->file_index,
ist->st->index);
925 tsoffset += f->
ctx->start_time;
929 &last_filter, &pad_idx, name);
933 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
943 av_log(NULL, AV_LOG_ERROR,
944 "No decoder for stream #%d:%d, filtering impossible\n",
946 return AVERROR_DECODER_NOT_FOUND;
948 switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
951 default: av_assert0(0);
return 0;
962 avfilter_graph_free(&fg->
graph);
967 AVFilterInOut *inputs, *outputs, *cur;
973 if (!(fg->
graph = avfilter_graph_alloc()))
974 return AVERROR(ENOMEM);
979 AVDictionaryEntry *e = NULL;
985 AV_DICT_IGNORE_SUFFIX))) {
986 av_strlcatf(args,
sizeof(args),
"%s=%s:", e->key, e->value);
989 args[strlen(args)-1] = 0;
991 if (!strncmp(args,
"sws_flags=", 10)) {
993 fg->
graph->scale_sws_opts = av_strdup(args+4);
998 AV_DICT_IGNORE_SUFFIX))) {
999 av_strlcatf(args,
sizeof(args),
"%s=%s:", e->key, e->value);
1002 args[strlen(args)-1] = 0;
1003 av_opt_set(fg->
graph,
"aresample_swr_opts", args, 0);
1007 AV_DICT_IGNORE_SUFFIX))) {
1008 av_strlcatf(args,
sizeof(args),
"%s=%s:", e->key, e->value);
1011 args[strlen(args) - 1] =
'\0';
1015 av_opt_set(fg->
graph,
"threads", e->value, 0);
1020 if ((ret = avfilter_graph_parse2(fg->
graph, graph_desc, &inputs, &outputs)) < 0)
1027 if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
1028 const char *num_inputs;
1029 const char *num_outputs;
1032 }
else if (outputs->next) {
1039 }
else if (inputs->next) {
1044 av_log(NULL, AV_LOG_ERROR,
"Simple filtergraph '%s' was expected "
1045 "to have exactly 1 input and 1 output."
1046 " However, it had %s input(s) and %s output(s)."
1047 " Please adjust, or use a complex filtergraph (-filter_complex) instead.\n",
1048 graph_desc, num_inputs, num_outputs);
1049 ret = AVERROR(EINVAL);
1053 for (cur = inputs, i = 0; cur; cur = cur->next, i++)
1055 avfilter_inout_free(&inputs);
1056 avfilter_inout_free(&outputs);
1059 avfilter_inout_free(&inputs);
1061 for (cur = outputs, i = 0; cur; cur = cur->next, i++)
1063 avfilter_inout_free(&outputs);
1066 avfilter_graph_set_auto_convert(fg->
graph, AVFILTER_AUTO_CONVERT_NONE);
1067 if ((ret = avfilter_graph_config(fg->
graph, NULL)) < 0)
1074 AVFilterContext *sink = ofilter->
filter;
1076 ofilter->
format = av_buffersink_get_format(sink);
1078 ofilter->
width = av_buffersink_get_w(sink);
1079 ofilter->
height = av_buffersink_get_h(sink);
1081 ofilter->
sample_rate = av_buffersink_get_sample_rate(sink);
1082 ofilter->
channel_layout = av_buffersink_get_channel_layout(sink);
1092 av_log(NULL, AV_LOG_ERROR,
"Encoder (codec %s) not found for output stream #%d:%d\n",
1094 ret = AVERROR(EINVAL);
1097 if (
ost->
enc->type == AVMEDIA_TYPE_AUDIO &&
1098 !(
ost->
enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
1107 ret = av_buffersrc_add_frame(fg->
inputs[i]->
filter, tmp);
1108 av_frame_free(&tmp);
1117 ret = av_buffersrc_add_frame(fg->
inputs[i]->
filter, NULL);
1126 if (
ist->sub2video.sub_queue &&
ist->sub2video.frame) {
1127 while (av_fifo_size(
ist->sub2video.sub_queue)) {
1129 av_fifo_generic_read(
ist->sub2video.sub_queue, &tmp,
sizeof(tmp), NULL);
1131 avsubtitle_free(&tmp);
1147 ifilter->
format = frame->format;
1149 ifilter->
width = frame->width;
1150 ifilter->
height = frame->height;
1154 ifilter->
channels = frame->channels;
1157 if (frame->hw_frames_ctx) {
1158 ifilter->
hw_frames_ctx = av_buffer_ref(frame->hw_frames_ctx);
1160 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