fe2147e9 |
/* |
11a1033c |
* ffmpeg filter configuration |
fe2147e9 |
* |
11a1033c |
* This file is part of FFmpeg. |
fe2147e9 |
* |
11a1033c |
* FFmpeg is free software; you can redistribute it and/or |
fe2147e9 |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* |
11a1033c |
* FFmpeg is distributed in the hope that it will be useful, |
fe2147e9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public |
11a1033c |
* License along with FFmpeg; if not, write to the Free Software |
fe2147e9 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
|
8f8bc923 |
#include <stdint.h>
|
11a1033c |
#include "ffmpeg.h" |
fe2147e9 |
#include "libavfilter/avfilter.h" |
11a1033c |
#include "libavfilter/buffersink.h" |
fe2147e9 |
|
6db37c94 |
#include "libavresample/avresample.h"
|
fe2147e9 |
#include "libavutil/avassert.h" |
11a1033c |
#include "libavutil/avstring.h"
#include "libavutil/bprint.h" |
03b07872 |
#include "libavutil/channel_layout.h" |
4bd6d73f |
#include "libavutil/opt.h" |
fe2147e9 |
#include "libavutil/pixdesc.h"
#include "libavutil/pixfmt.h" |
11a1033c |
#include "libavutil/imgutils.h" |
fe2147e9 |
#include "libavutil/samplefmt.h"
|
758f929d |
enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *enc_ctx, AVCodec *codec, enum AVPixelFormat target) |
11a1033c |
{
if (codec && codec->pix_fmts) { |
ac627b3d |
const enum AVPixelFormat *p = codec->pix_fmts; |
c3778df2 |
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
int has_alpha = desc ? desc->nb_components % 2 == 0 : 0; |
ac627b3d |
enum AVPixelFormat best= AV_PIX_FMT_NONE; |
d80e7ba9 |
static const enum AVPixelFormat mjpeg_formats[] =
{ AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
static const enum AVPixelFormat ljpeg_formats[] =
{ AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE }; |
09b16619 |
|
758f929d |
if (enc_ctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
if (enc_ctx->codec_id == AV_CODEC_ID_MJPEG) { |
09b16619 |
p = mjpeg_formats; |
758f929d |
} else if (enc_ctx->codec_id == AV_CODEC_ID_LJPEG) { |
09b16619 |
p =ljpeg_formats; |
11a1033c |
}
} |
ac627b3d |
for (; *p != AV_PIX_FMT_NONE; p++) { |
704a5a06 |
best= avcodec_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL); |
11a1033c |
if (*p == target)
break;
} |
ac627b3d |
if (*p == AV_PIX_FMT_NONE) {
if (target != AV_PIX_FMT_NONE) |
11a1033c |
av_log(NULL, AV_LOG_WARNING,
"Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n", |
79393a83 |
av_get_pix_fmt_name(target), |
11a1033c |
codec->name, |
79393a83 |
av_get_pix_fmt_name(best)); |
11a1033c |
return best;
}
}
return target;
}
void choose_sample_fmt(AVStream *st, AVCodec *codec)
{
if (codec && codec->sample_fmts) {
const enum AVSampleFormat *p = codec->sample_fmts;
for (; *p != -1; p++) {
if (*p == st->codec->sample_fmt)
break;
}
if (*p == -1) {
if((codec->capabilities & CODEC_CAP_LOSSLESS) && av_get_sample_fmt_name(st->codec->sample_fmt) > av_get_sample_fmt_name(codec->sample_fmts[0]))
av_log(NULL, AV_LOG_ERROR, "Conversion will not be lossless.\n");
if(av_get_sample_fmt_name(st->codec->sample_fmt))
av_log(NULL, AV_LOG_WARNING,
"Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
av_get_sample_fmt_name(st->codec->sample_fmt),
codec->name,
av_get_sample_fmt_name(codec->sample_fmts[0]));
st->codec->sample_fmt = codec->sample_fmts[0];
}
}
}
static char *choose_pix_fmts(OutputStream *ost)
{ |
d5ec1ddf |
AVDictionaryEntry *strict_dict = av_dict_get(ost->encoder_opts, "strict", NULL, 0); |
d9bc251d |
if (strict_dict)
// used by choose_pixel_fmt() and below |
0949ff93 |
av_opt_set(ost->enc_ctx, "strict", strict_dict->value, 0); |
d9bc251d |
|
11a1033c |
if (ost->keep_pix_fmt) {
if (ost->filter)
avfilter_graph_set_auto_convert(ost->filter->graph->graph,
AVFILTER_AUTO_CONVERT_NONE); |
0949ff93 |
if (ost->enc_ctx->pix_fmt == AV_PIX_FMT_NONE) |
11a1033c |
return NULL; |
0949ff93 |
return av_strdup(av_get_pix_fmt_name(ost->enc_ctx->pix_fmt)); |
11a1033c |
} |
0949ff93 |
if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) { |
758f929d |
return av_strdup(av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc_ctx, ost->enc, ost->enc_ctx->pix_fmt))); |
11a1033c |
} else if (ost->enc && ost->enc->pix_fmts) { |
ac627b3d |
const enum AVPixelFormat *p; |
11a1033c |
AVIOContext *s = NULL;
uint8_t *ret;
int len;
if (avio_open_dyn_buf(&s) < 0) |
efce76a8 |
exit_program(1); |
11a1033c |
p = ost->enc->pix_fmts; |
0949ff93 |
if (ost->enc_ctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
if (ost->enc_ctx->codec_id == AV_CODEC_ID_MJPEG) { |
ac627b3d |
p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE }; |
0949ff93 |
} else if (ost->enc_ctx->codec_id == AV_CODEC_ID_LJPEG) { |
ac627b3d |
p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE }; |
11a1033c |
}
}
|
ac627b3d |
for (; *p != AV_PIX_FMT_NONE; p++) { |
11a1033c |
const char *name = av_get_pix_fmt_name(*p); |
eabc0c73 |
avio_printf(s, "%s|", name); |
11a1033c |
}
len = avio_close_dyn_buf(s, &ret);
ret[len - 1] = 0;
return ret;
} else
return NULL;
}
|
b4ca1b15 |
/* Define a function for building a string containing a list of
* allowed formats. */ |
7b6c0bda |
#define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name) \ |
fe2147e9 |
static char *choose_ ## var ## s(OutputStream *ost) \
{ \ |
38313626 |
if (ost->enc_ctx->var != none) { \
get_name(ost->enc_ctx->var); \ |
fe2147e9 |
return av_strdup(name); \ |
38d40ac1 |
} else if (ost->enc && ost->enc->supported_list) { \ |
fe2147e9 |
const type *p; \
AVIOContext *s = NULL; \
uint8_t *ret; \
int len; \
\
if (avio_open_dyn_buf(&s) < 0) \ |
efce76a8 |
exit_program(1); \ |
fe2147e9 |
\
for (p = ost->enc->supported_list; *p != none; p++) { \
get_name(*p); \ |
7b6c0bda |
avio_printf(s, "%s|", name); \ |
fe2147e9 |
} \
len = avio_close_dyn_buf(s, &ret); \
ret[len - 1] = 0; \
return ret; \
} else \
return NULL; \
}
|
ac627b3d |
// DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE, |
7b6c0bda |
// GET_PIX_FMT_NAME) |
fe2147e9 |
DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts, |
7b6c0bda |
AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME) |
fe2147e9 |
DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0, |
7b6c0bda |
GET_SAMPLE_RATE_NAME) |
fe2147e9 |
DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0, |
7b6c0bda |
GET_CH_LAYOUT_NAME) |
fe2147e9 |
FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
{
FilterGraph *fg = av_mallocz(sizeof(*fg));
if (!fg) |
efce76a8 |
exit_program(1); |
fe2147e9 |
fg->index = nb_filtergraphs;
|
330983b7 |
GROW_ARRAY(fg->outputs, fg->nb_outputs); |
fe2147e9 |
if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0])))) |
efce76a8 |
exit_program(1); |
fe2147e9 |
fg->outputs[0]->ost = ost;
fg->outputs[0]->graph = fg;
ost->filter = fg->outputs[0];
|
330983b7 |
GROW_ARRAY(fg->inputs, fg->nb_inputs); |
fe2147e9 |
if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0])))) |
efce76a8 |
exit_program(1); |
fe2147e9 |
fg->inputs[0]->ist = ist;
fg->inputs[0]->graph = fg;
|
330983b7 |
GROW_ARRAY(ist->filters, ist->nb_filters); |
fe2147e9 |
ist->filters[ist->nb_filters - 1] = fg->inputs[0];
|
330983b7 |
GROW_ARRAY(filtergraphs, nb_filtergraphs); |
fe2147e9 |
filtergraphs[nb_filtergraphs - 1] = fg;
return fg;
}
static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
{
InputStream *ist = NULL;
enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
int i;
// TODO: support other filter types
if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
"currently.\n"); |
efce76a8 |
exit_program(1); |
fe2147e9 |
}
if (in->name) {
AVFormatContext *s;
AVStream *st = NULL;
char *p;
int file_idx = strtol(in->name, &p, 0);
if (file_idx < 0 || file_idx >= nb_input_files) { |
11a1033c |
av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n", |
fe2147e9 |
file_idx, fg->graph_desc); |
efce76a8 |
exit_program(1); |
fe2147e9 |
}
s = input_files[file_idx]->ctx;
for (i = 0; i < s->nb_streams; i++) { |
11a1033c |
enum AVMediaType stream_type = s->streams[i]->codec->codec_type;
if (stream_type != type &&
!(stream_type == AVMEDIA_TYPE_SUBTITLE &&
type == AVMEDIA_TYPE_VIDEO /* sub2video hack */)) |
fe2147e9 |
continue;
if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
st = s->streams[i];
break;
}
}
if (!st) {
av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
"matches no streams.\n", p, fg->graph_desc); |
efce76a8 |
exit_program(1); |
fe2147e9 |
}
ist = input_streams[input_files[file_idx]->ist_index + st->index];
} else {
/* find the first unused stream of corresponding type */
for (i = 0; i < nb_input_streams; i++) {
ist = input_streams[i]; |
41776ba9 |
if (ist->dec_ctx->codec_type == type && ist->discard) |
fe2147e9 |
break;
}
if (i == nb_input_streams) {
av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for " |
11a1033c |
"unlabeled input pad %d on filter %s\n", in->pad_idx, |
fe2147e9 |
in->filter_ctx->name); |
efce76a8 |
exit_program(1); |
fe2147e9 |
}
}
av_assert0(ist);
ist->discard = 0; |
b01d2a55 |
ist->decoding_needed |= DECODING_FOR_FILTER; |
fe2147e9 |
ist->st->discard = AVDISCARD_NONE;
|
330983b7 |
GROW_ARRAY(fg->inputs, fg->nb_inputs); |
fe2147e9 |
if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0])))) |
efce76a8 |
exit_program(1); |
fe2147e9 |
fg->inputs[fg->nb_inputs - 1]->ist = ist;
fg->inputs[fg->nb_inputs - 1]->graph = fg;
|
330983b7 |
GROW_ARRAY(ist->filters, ist->nb_filters); |
fe2147e9 |
ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
}
|
811bd078 |
static int insert_trim(int64_t start_time, int64_t duration,
AVFilterContext **last_filter, int *pad_idx,
const char *filter_name) |
a83c0da5 |
{
AVFilterGraph *graph = (*last_filter)->graph;
AVFilterContext *ctx;
const AVFilter *trim; |
811bd078 |
enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim"; |
a83c0da5 |
int ret = 0;
|
811bd078 |
if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE) |
a83c0da5 |
return 0;
trim = avfilter_get_by_name(name);
if (!trim) {
av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
"recording time.\n", name);
return AVERROR_FILTER_NOT_FOUND;
}
ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
if (!ctx)
return AVERROR(ENOMEM);
|
811bd078 |
if (duration != INT64_MAX) { |
d4ab1292 |
ret = av_opt_set_int(ctx, "durationi", duration, |
8cd472d3 |
AV_OPT_SEARCH_CHILDREN);
} |
811bd078 |
if (ret >= 0 && start_time != AV_NOPTS_VALUE) { |
d4ab1292 |
ret = av_opt_set_int(ctx, "starti", start_time, |
8cd472d3 |
AV_OPT_SEARCH_CHILDREN);
} |
a83c0da5 |
if (ret < 0) {
av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
return ret;
}
ret = avfilter_init_str(ctx, NULL);
if (ret < 0)
return ret;
ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
if (ret < 0)
return ret;
*last_filter = ctx;
*pad_idx = 0;
return 0;
}
|
fe2147e9 |
static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
{
char *pix_fmts;
OutputStream *ost = ofilter->ost; |
811bd078 |
OutputFile *of = output_files[ost->file_index]; |
38313626 |
AVCodecContext *codec = ost->enc_ctx; |
fe2147e9 |
AVFilterContext *last_filter = out->filter_ctx;
int pad_idx = out->pad_idx;
int ret;
char name[255];
snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
ret = avfilter_graph_create_filter(&ofilter->filter, |
ceac5c54 |
avfilter_get_by_name("buffersink"), |
58dee6e6 |
name, NULL, NULL, fg->graph); |
11a1033c |
|
fe2147e9 |
if (ret < 0)
return ret;
if (codec->width || codec->height) {
char args[255];
AVFilterContext *filter;
|
72fbc968 |
snprintf(args, sizeof(args), "%d:%d:0x%X", |
fe2147e9 |
codec->width,
codec->height,
(unsigned)ost->sws_flags);
snprintf(name, sizeof(name), "scaler for output stream %d:%d",
ost->file_index, ost->index);
if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
name, args, NULL, fg->graph)) < 0)
return ret;
if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
return ret;
last_filter = filter;
pad_idx = 0;
}
if ((pix_fmts = choose_pix_fmts(ost))) {
AVFilterContext *filter;
snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
ost->file_index, ost->index); |
14369f59 |
ret = avfilter_graph_create_filter(&filter, |
fe2147e9 |
avfilter_get_by_name("format"),
"format", pix_fmts, NULL, |
14369f59 |
fg->graph);
av_freep(&pix_fmts);
if (ret < 0) |
fe2147e9 |
return ret;
if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
return ret;
last_filter = filter;
pad_idx = 0;
}
|
11a1033c |
if (ost->frame_rate.num && 0) { |
fe2147e9 |
AVFilterContext *fps;
char args[255];
snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
ost->frame_rate.den);
snprintf(name, sizeof(name), "fps for output stream %d:%d",
ost->file_index, ost->index);
ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
name, args, NULL, fg->graph);
if (ret < 0)
return ret;
ret = avfilter_link(last_filter, pad_idx, fps, 0);
if (ret < 0)
return ret;
last_filter = fps;
pad_idx = 0;
}
|
811bd078 |
snprintf(name, sizeof(name), "trim for output stream %d:%d",
ost->file_index, ost->index);
ret = insert_trim(of->start_time, of->recording_time,
&last_filter, &pad_idx, name); |
a83c0da5 |
if (ret < 0)
return ret;
|
fe2147e9 |
if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
return ret;
return 0;
}
static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
{
OutputStream *ost = ofilter->ost; |
811bd078 |
OutputFile *of = output_files[ost->file_index]; |
38313626 |
AVCodecContext *codec = ost->enc_ctx; |
fe2147e9 |
AVFilterContext *last_filter = out->filter_ctx;
int pad_idx = out->pad_idx;
char *sample_fmts, *sample_rates, *channel_layouts;
char name[255];
int ret;
snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
ret = avfilter_graph_create_filter(&ofilter->filter, |
ceac5c54 |
avfilter_get_by_name("abuffersink"), |
62d888ef |
name, NULL, NULL, fg->graph); |
fe2147e9 |
if (ret < 0)
return ret; |
62d888ef |
if ((ret = av_opt_set_int(ofilter->filter, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
return ret; |
fe2147e9 |
|
11a1033c |
#define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
AVFilterContext *filt_ctx; \
\
av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
"similarly to -af " filter_name "=%s.\n", arg); \
\
ret = avfilter_graph_create_filter(&filt_ctx, \
avfilter_get_by_name(filter_name), \
filter_name, arg, NULL, fg->graph); \
if (ret < 0) \
return ret; \
\
ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
if (ret < 0) \
return ret; \
\
last_filter = filt_ctx; \
pad_idx = 0; \
} while (0)
if (ost->audio_channels_mapped) {
int i;
AVBPrint pan_buf;
av_bprint_init(&pan_buf, 256, 8192);
av_bprintf(&pan_buf, "0x%"PRIx64,
av_get_default_channel_layout(ost->audio_channels_mapped));
for (i = 0; i < ost->audio_channels_mapped; i++)
if (ost->audio_channels_map[i] != -1)
av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
AUTO_INSERT_FILTER("-map_channel", "pan", pan_buf.str);
av_bprint_finalize(&pan_buf, NULL);
}
|
fe2147e9 |
if (codec->channels && !codec->channel_layout)
codec->channel_layout = av_get_default_channel_layout(codec->channels);
sample_fmts = choose_sample_fmts(ost);
sample_rates = choose_sample_rates(ost);
channel_layouts = choose_channel_layouts(ost);
if (sample_fmts || sample_rates || channel_layouts) {
AVFilterContext *format;
char args[256]; |
d7cb5a8d |
args[0] = 0; |
fe2147e9 |
if (sample_fmts) |
d7cb5a8d |
av_strlcatf(args, sizeof(args), "sample_fmts=%s:", |
fe2147e9 |
sample_fmts);
if (sample_rates) |
d7cb5a8d |
av_strlcatf(args, sizeof(args), "sample_rates=%s:", |
fe2147e9 |
sample_rates);
if (channel_layouts) |
d7cb5a8d |
av_strlcatf(args, sizeof(args), "channel_layouts=%s:", |
fe2147e9 |
channel_layouts);
av_freep(&sample_fmts);
av_freep(&sample_rates);
av_freep(&channel_layouts);
snprintf(name, sizeof(name), "audio format for output stream %d:%d",
ost->file_index, ost->index);
ret = avfilter_graph_create_filter(&format,
avfilter_get_by_name("aformat"),
name, args, NULL, fg->graph);
if (ret < 0)
return ret;
ret = avfilter_link(last_filter, pad_idx, format, 0);
if (ret < 0)
return ret;
last_filter = format;
pad_idx = 0;
}
|
11a1033c |
if (audio_volume != 256 && 0) {
char args[256];
snprintf(args, sizeof(args), "%f", audio_volume / 256.);
AUTO_INSERT_FILTER("-vol", "volume", args);
}
|
33ff943f |
if (ost->apad && of->shortest) {
char args[256];
int i;
for (i=0; i<of->ctx->nb_streams; i++)
if (of->ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
break;
if (i<of->ctx->nb_streams) {
snprintf(args, sizeof(args), "%s", ost->apad);
AUTO_INSERT_FILTER("-apad", "apad", args);
}
}
|
811bd078 |
snprintf(name, sizeof(name), "trim for output stream %d:%d",
ost->file_index, ost->index);
ret = insert_trim(of->start_time, of->recording_time,
&last_filter, &pad_idx, name); |
a83c0da5 |
if (ret < 0)
return ret;
|
fe2147e9 |
if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
return ret;
return 0;
}
#define DESCRIBE_FILTER_LINK(f, inout, in) \
{ \
AVFilterContext *ctx = inout->filter_ctx; \
AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \ |
06cd4c5a |
int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs; \ |
fe2147e9 |
AVIOContext *pb; \
\
if (avio_open_dyn_buf(&pb) < 0) \ |
efce76a8 |
exit_program(1); \ |
fe2147e9 |
\
avio_printf(pb, "%s", ctx->filter->name); \
if (nb_pads > 1) \
avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
avio_w8(pb, 0); \
avio_close_dyn_buf(pb, &f->name); \
}
int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
{
av_freep(&ofilter->name);
DESCRIBE_FILTER_LINK(ofilter, out, 0);
switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
default: av_assert0(0);
}
}
|
11a1033c |
static int sub2video_prepare(InputStream *ist)
{
AVFormatContext *avf = input_files[ist->file_index]->ctx; |
f9b34b8b |
int i, w, h; |
11a1033c |
/* Compute the size of the canvas for the subtitles stream.
If the subtitles codec has set a size, use it. Otherwise use the
maximum dimensions of the video streams in the same file. */ |
921d5ae3 |
w = ist->dec_ctx->width;
h = ist->dec_ctx->height; |
11a1033c |
if (!(w && h)) {
for (i = 0; i < avf->nb_streams; i++) {
if (avf->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
w = FFMAX(w, avf->streams[i]->codec->width);
h = FFMAX(h, avf->streams[i]->codec->height);
}
}
if (!(w && h)) {
w = FFMAX(w, 720);
h = FFMAX(h, 576);
}
av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
} |
921d5ae3 |
ist->sub2video.w = ist->dec_ctx->width = ist->resample_width = w;
ist->sub2video.h = ist->dec_ctx->height = ist->resample_height = h; |
11a1033c |
|
ac627b3d |
/* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the |
11a1033c |
palettes for all rectangles are identical or compatible */ |
921d5ae3 |
ist->resample_pix_fmt = ist->dec_ctx->pix_fmt = AV_PIX_FMT_RGB32; |
11a1033c |
|
f9b34b8b |
ist->sub2video.frame = av_frame_alloc();
if (!ist->sub2video.frame) |
11a1033c |
return AVERROR(ENOMEM);
return 0;
}
|
fe2147e9 |
static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
AVFilterInOut *in)
{ |
2e661f26 |
AVFilterContext *last_filter; |
51fc88e7 |
const AVFilter *buffer_filt = avfilter_get_by_name("buffer"); |
fe2147e9 |
InputStream *ist = ifilter->ist; |
811bd078 |
InputFile *f = input_files[ist->file_index]; |
fe2147e9 |
AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
ist->st->time_base; |
ef7b6b48 |
AVRational fr = ist->framerate; |
fe2147e9 |
AVRational sar; |
11a1033c |
AVBPrint args;
char name[255]; |
811bd078 |
int ret, pad_idx = 0; |
fe2147e9 |
|
921d5ae3 |
if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) { |
640a36a0 |
av_log(NULL, AV_LOG_ERROR, "Cannot connect video filter to audio input\n");
return AVERROR(EINVAL);
}
|
ef7b6b48 |
if (!fr.num)
fr = av_guess_frame_rate(input_files[ist->file_index]->ctx, ist->st, NULL); |
1b8f4d77 |
|
921d5ae3 |
if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE) { |
11a1033c |
ret = sub2video_prepare(ist);
if (ret < 0)
return ret;
}
|
fe2147e9 |
sar = ist->st->sample_aspect_ratio.num ?
ist->st->sample_aspect_ratio : |
41776ba9 |
ist->dec_ctx->sample_aspect_ratio; |
11a1033c |
if(!sar.den)
sar = (AVRational){0,1};
av_bprint_init(&args, 0, 1);
av_bprintf(&args,
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:" |
04a530f7 |
"pixel_aspect=%d/%d:sws_param=flags=%d", ist->resample_width, |
62e10c37 |
ist->resample_height,
ist->hwaccel_retrieve_data ? ist->hwaccel_retrieved_pix_fmt : ist->resample_pix_fmt, |
11a1033c |
tb.num, tb.den, sar.num, sar.den, |
d0979357 |
SWS_BILINEAR + ((ist->dec_ctx->flags&CODEC_FLAG_BITEXACT) ? SWS_BITEXACT:0)); |
11a1033c |
if (fr.num && fr.den)
av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den); |
fe2147e9 |
snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
ist->file_index, ist->st->index);
|
51fc88e7 |
if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name, |
11a1033c |
args.str, NULL, fg->graph)) < 0) |
fe2147e9 |
return ret; |
2e661f26 |
last_filter = ifilter->filter; |
fe2147e9 |
if (ist->framerate.num) {
AVFilterContext *setpts;
snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
ist->file_index, ist->st->index);
if ((ret = avfilter_graph_create_filter(&setpts,
avfilter_get_by_name("setpts"),
name, "N", NULL,
fg->graph)) < 0)
return ret;
|
2e661f26 |
if ((ret = avfilter_link(last_filter, 0, setpts, 0)) < 0) |
fe2147e9 |
return ret;
|
2e661f26 |
last_filter = setpts; |
fe2147e9 |
}
|
4257b804 |
if (do_deinterlace) {
AVFilterContext *yadif;
snprintf(name, sizeof(name), "deinterlace input from stream %d:%d",
ist->file_index, ist->st->index);
if ((ret = avfilter_graph_create_filter(&yadif,
avfilter_get_by_name("yadif"),
name, "", NULL,
fg->graph)) < 0)
return ret;
|
84bc3170 |
if ((ret = avfilter_link(last_filter, 0, yadif, 0)) < 0) |
4257b804 |
return ret;
|
84bc3170 |
last_filter = yadif; |
4257b804 |
}
|
811bd078 |
snprintf(name, sizeof(name), "trim for input stream %d:%d",
ist->file_index, ist->st->index);
ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ? |
488a0fa6 |
AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name); |
811bd078 |
if (ret < 0)
return ret;
|
2e661f26 |
if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0) |
fe2147e9 |
return ret;
return 0;
}
static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
AVFilterInOut *in)
{ |
2e661f26 |
AVFilterContext *last_filter; |
51fc88e7 |
const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer"); |
fe2147e9 |
InputStream *ist = ifilter->ist; |
811bd078 |
InputFile *f = input_files[ist->file_index]; |
41f025df |
AVBPrint args;
char name[255]; |
811bd078 |
int ret, pad_idx = 0; |
fe2147e9 |
|
d0979357 |
if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_AUDIO) { |
640a36a0 |
av_log(NULL, AV_LOG_ERROR, "Cannot connect audio filter to non audio input\n");
return AVERROR(EINVAL);
}
|
41f025df |
av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s", |
41776ba9 |
1, ist->dec_ctx->sample_rate,
ist->dec_ctx->sample_rate, |
d0979357 |
av_get_sample_fmt_name(ist->dec_ctx->sample_fmt));
if (ist->dec_ctx->channel_layout) |
41f025df |
av_bprintf(&args, ":channel_layout=0x%"PRIx64, |
d0979357 |
ist->dec_ctx->channel_layout); |
41f025df |
else |
d0979357 |
av_bprintf(&args, ":channels=%d", ist->dec_ctx->channels); |
fe2147e9 |
snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
ist->file_index, ist->st->index);
|
51fc88e7 |
if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt, |
41f025df |
name, args.str, NULL, |
fe2147e9 |
fg->graph)) < 0)
return ret; |
2e661f26 |
last_filter = ifilter->filter; |
fe2147e9 |
|
11a1033c |
#define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
AVFilterContext *filt_ctx; \
\
av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
"similarly to -af " filter_name "=%s.\n", arg); \
\
snprintf(name, sizeof(name), "graph %d %s for input stream %d:%d", \
fg->index, filter_name, ist->file_index, ist->st->index); \
ret = avfilter_graph_create_filter(&filt_ctx, \
avfilter_get_by_name(filter_name), \
name, arg, NULL, fg->graph); \
if (ret < 0) \
return ret; \
\ |
84bc3170 |
ret = avfilter_link(last_filter, 0, filt_ctx, 0); \ |
11a1033c |
if (ret < 0) \
return ret; \
\ |
84bc3170 |
last_filter = filt_ctx; \ |
11a1033c |
} while (0) |
fe2147e9 |
|
11a1033c |
if (audio_sync_method > 0) {
char args[256] = {0}; |
fe2147e9 |
|
a752b9b8 |
av_strlcatf(args, sizeof(args), "async=%d", audio_sync_method);
if (audio_drift_threshold != 0.1)
av_strlcatf(args, sizeof(args), ":min_hard_comp=%f", audio_drift_threshold); |
e4202892 |
if (!fg->reconfiguration)
av_strlcatf(args, sizeof(args), ":first_pts=0"); |
11a1033c |
AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
} |
fe2147e9 |
|
11a1033c |
// if (ost->audio_channels_mapped) {
// int i;
// AVBPrint pan_buf;
// av_bprint_init(&pan_buf, 256, 8192);
// av_bprintf(&pan_buf, "0x%"PRIx64,
// av_get_default_channel_layout(ost->audio_channels_mapped));
// for (i = 0; i < ost->audio_channels_mapped; i++)
// if (ost->audio_channels_map[i] != -1)
// av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
// AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
// av_bprint_finalize(&pan_buf, NULL);
// }
if (audio_volume != 256) {
char args[256]; |
fe2147e9 |
|
54a71f2e |
av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
"audio filter instead.\n");
|
11a1033c |
snprintf(args, sizeof(args), "%f", audio_volume / 256.);
AUTO_INSERT_FILTER_INPUT("-vol", "volume", args); |
fe2147e9 |
} |
811bd078 |
snprintf(name, sizeof(name), "trim for input stream %d:%d",
ist->file_index, ist->st->index);
ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ? |
488a0fa6 |
AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name); |
811bd078 |
if (ret < 0)
return ret;
|
2e661f26 |
if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0) |
fe2147e9 |
return ret;
return 0;
}
static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
AVFilterInOut *in)
{
av_freep(&ifilter->name);
DESCRIBE_FILTER_LINK(ifilter, in, 1);
|
91244073 |
if (!ifilter->ist->dec) {
av_log(NULL, AV_LOG_ERROR,
"No decoder for stream #%d:%d, filtering impossible\n",
ifilter->ist->file_index, ifilter->ist->st->index);
return AVERROR_DECODER_NOT_FOUND;
} |
fe2147e9 |
switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
default: av_assert0(0);
}
}
int configure_filtergraph(FilterGraph *fg)
{
AVFilterInOut *inputs, *outputs, *cur;
int ret, i, init = !fg->graph, simple = !fg->graph_desc;
const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
fg->graph_desc;
avfilter_graph_free(&fg->graph);
if (!(fg->graph = avfilter_graph_alloc()))
return AVERROR(ENOMEM);
if (simple) {
OutputStream *ost = fg->outputs[0]->ost; |
6db37c94 |
char args[512];
AVDictionaryEntry *e = NULL;
|
fe2147e9 |
snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
fg->graph->scale_sws_opts = av_strdup(args); |
4bd6d73f |
args[0] = 0; |
ad899522 |
while ((e = av_dict_get(ost->swr_opts, "", e,
AV_DICT_IGNORE_SUFFIX))) {
av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
} |
4bd6d73f |
if (strlen(args))
args[strlen(args)-1] = 0;
av_opt_set(fg->graph, "aresample_swr_opts", args, 0); |
6db37c94 |
args[0] = '\0';
while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
AV_DICT_IGNORE_SUFFIX))) {
av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
}
if (strlen(args))
args[strlen(args) - 1] = '\0';
fg->graph->resample_lavr_opts = av_strdup(args); |
da54ad9a |
|
d5ec1ddf |
e = av_dict_get(ost->encoder_opts, "threads", NULL, 0); |
da54ad9a |
if (e)
av_opt_set(fg->graph, "threads", e->value, 0); |
fe2147e9 |
}
if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
return ret;
if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
"exactly one input and output.\n", graph_desc);
return AVERROR(EINVAL);
}
for (cur = inputs; !simple && init && cur; cur = cur->next)
init_input_filter(fg, cur);
for (cur = inputs, i = 0; cur; cur = cur->next, i++) |
494cbc42 |
if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0) {
avfilter_inout_free(&inputs);
avfilter_inout_free(&outputs); |
fe2147e9 |
return ret; |
494cbc42 |
} |
fe2147e9 |
avfilter_inout_free(&inputs);
if (!init || simple) {
/* we already know the mappings between lavfi outputs and output streams,
* so we can finish the setup */
for (cur = outputs, i = 0; cur; cur = cur->next, i++)
configure_output_filter(fg, fg->outputs[i], cur);
avfilter_inout_free(&outputs);
if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
return ret;
} else {
/* wait until output mappings are processed */
for (cur = outputs; cur;) { |
330983b7 |
GROW_ARRAY(fg->outputs, fg->nb_outputs); |
fe2147e9 |
if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0])))) |
efce76a8 |
exit_program(1); |
fe2147e9 |
fg->outputs[fg->nb_outputs - 1]->graph = fg;
fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
cur = cur->next;
fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
}
}
|
e4202892 |
fg->reconfiguration = 1; |
16de4d92 |
for (i = 0; i < fg->nb_outputs; i++) {
OutputStream *ost = fg->outputs[i]->ost;
if (ost &&
ost->enc->type == AVMEDIA_TYPE_AUDIO &&
!(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
av_buffersink_set_frame_size(ost->filter->filter,
ost->enc_ctx->frame_size);
}
|
fe2147e9 |
return 0;
}
int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
{
int i;
for (i = 0; i < fg->nb_inputs; i++)
if (fg->inputs[i]->ist == ist)
return 1;
return 0;
}
|