2012-08-02 09:24:02 +03:00
|
|
|
/*
|
2012-08-09 01:26:38 +03:00
|
|
|
* ffmpeg filter configuration
|
2012-08-02 09:24:02 +03:00
|
|
|
*
|
2012-08-09 01:26:38 +03:00
|
|
|
* This file is part of FFmpeg.
|
2012-08-02 09:24:02 +03:00
|
|
|
*
|
2012-08-09 01:26:38 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-08-02 09:24:02 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2012-08-09 01:26:38 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-08-02 09:24:02 +03:00
|
|
|
* 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
|
2012-08-09 01:26:38 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-08-02 09:24:02 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2013-11-23 23:32:55 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
#include "ffmpeg.h"
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
#include "libavfilter/avfilter.h"
|
2012-08-09 01:26:38 +03:00
|
|
|
#include "libavfilter/buffersink.h"
|
2016-02-12 10:22:15 +02:00
|
|
|
#include "libavfilter/buffersrc.h"
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
#include "libavutil/avassert.h"
|
2012-08-09 01:26:38 +03:00
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/bprint.h"
|
2012-11-10 18:00:00 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
2015-04-29 16:59:01 +02:00
|
|
|
#include "libavutil/display.h"
|
2012-11-23 17:35:48 +03:00
|
|
|
#include "libavutil/opt.h"
|
2012-08-02 09:24:02 +03:00
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "libavutil/pixfmt.h"
|
2012-08-09 01:26:38 +03:00
|
|
|
#include "libavutil/imgutils.h"
|
2012-08-02 09:24:02 +03:00
|
|
|
#include "libavutil/samplefmt.h"
|
|
|
|
|
2021-04-06 03:01:14 +02:00
|
|
|
// FIXME: YUV420P etc. are actually supported with full color range,
|
|
|
|
// yet the latter information isn't available here.
|
2021-04-13 19:13:39 +02:00
|
|
|
static const enum AVPixelFormat *get_compliance_normal_pix_fmts(const AVCodec *codec, const enum AVPixelFormat default_formats[])
|
2016-02-10 13:57:11 +02:00
|
|
|
{
|
|
|
|
static const enum AVPixelFormat mjpeg_formats[] =
|
|
|
|
{ AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
|
|
|
|
AV_PIX_FMT_NONE };
|
|
|
|
|
2021-04-13 19:13:39 +02:00
|
|
|
if (!strcmp(codec->name, "mjpeg")) {
|
2016-02-10 13:57:11 +02:00
|
|
|
return mjpeg_formats;
|
|
|
|
} else {
|
|
|
|
return default_formats;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 14:26:47 +02:00
|
|
|
static enum AVPixelFormat
|
|
|
|
choose_pixel_fmt(const AVCodec *codec, enum AVPixelFormat target,
|
|
|
|
int strict_std_compliance)
|
2012-08-09 01:26:38 +03:00
|
|
|
{
|
|
|
|
if (codec && codec->pix_fmts) {
|
2012-10-08 21:54:00 +03:00
|
|
|
const enum AVPixelFormat *p = codec->pix_fmts;
|
2012-10-22 14:58:19 +03:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
|
2018-04-19 23:14:24 +02:00
|
|
|
//FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
|
2012-10-22 14:58:19 +03:00
|
|
|
int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
|
2012-10-08 21:54:00 +03:00
|
|
|
enum AVPixelFormat best= AV_PIX_FMT_NONE;
|
2014-04-27 04:45:12 +03:00
|
|
|
|
2022-06-19 14:26:47 +02:00
|
|
|
if (strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
|
2021-04-13 19:13:39 +02:00
|
|
|
p = get_compliance_normal_pix_fmts(codec, p);
|
2012-08-09 01:26:38 +03:00
|
|
|
}
|
2012-10-08 21:54:00 +03:00
|
|
|
for (; *p != AV_PIX_FMT_NONE; p++) {
|
2021-02-26 09:38:26 +02:00
|
|
|
best = av_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
|
2012-08-09 01:26:38 +03:00
|
|
|
if (*p == target)
|
|
|
|
break;
|
|
|
|
}
|
2012-10-08 21:54:00 +03:00
|
|
|
if (*p == AV_PIX_FMT_NONE) {
|
|
|
|
if (target != AV_PIX_FMT_NONE)
|
2012-08-09 01:26:38 +03:00
|
|
|
av_log(NULL, AV_LOG_WARNING,
|
|
|
|
"Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
|
2012-10-20 07:43:48 +03:00
|
|
|
av_get_pix_fmt_name(target),
|
2012-08-09 01:26:38 +03:00
|
|
|
codec->name,
|
2012-10-20 07:43:48 +03:00
|
|
|
av_get_pix_fmt_name(best));
|
2012-08-09 01:26:38 +03:00
|
|
|
return best;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2021-12-01 18:13:32 +02:00
|
|
|
/* May return NULL (no pixel format found), a static string or a string
|
|
|
|
* backed by the bprint. Nothing has been written to the AVBPrint in case
|
|
|
|
* NULL is returned. The AVBPrint provided should be clean. */
|
|
|
|
static const char *choose_pix_fmts(OutputFilter *ofilter, AVBPrint *bprint)
|
2012-08-09 01:26:38 +03:00
|
|
|
{
|
2016-11-13 16:33:39 +02:00
|
|
|
OutputStream *ost = ofilter->ost;
|
2022-08-25 10:01:19 +02:00
|
|
|
AVCodecContext *enc = ost->enc_ctx;
|
2021-10-19 00:27:01 +02:00
|
|
|
const AVDictionaryEntry *strict_dict = av_dict_get(ost->encoder_opts, "strict", NULL, 0);
|
2013-10-02 15:16:12 +03:00
|
|
|
if (strict_dict)
|
|
|
|
// used by choose_pixel_fmt() and below
|
2014-06-01 17:06:18 +03:00
|
|
|
av_opt_set(ost->enc_ctx, "strict", strict_dict->value, 0);
|
2013-10-02 15:16:12 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
if (ost->keep_pix_fmt) {
|
2016-11-13 16:33:39 +02:00
|
|
|
avfilter_graph_set_auto_convert(ofilter->graph->graph,
|
2012-08-09 01:26:38 +03:00
|
|
|
AVFILTER_AUTO_CONVERT_NONE);
|
2014-06-01 17:06:18 +03:00
|
|
|
if (ost->enc_ctx->pix_fmt == AV_PIX_FMT_NONE)
|
2012-08-09 01:26:38 +03:00
|
|
|
return NULL;
|
2021-12-01 18:13:32 +02:00
|
|
|
return av_get_pix_fmt_name(ost->enc_ctx->pix_fmt);
|
2012-08-09 01:26:38 +03:00
|
|
|
}
|
2014-06-01 17:06:18 +03:00
|
|
|
if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
|
2022-08-25 10:01:19 +02:00
|
|
|
return av_get_pix_fmt_name(choose_pixel_fmt(enc->codec, enc->pix_fmt,
|
2022-06-19 14:26:47 +02:00
|
|
|
ost->enc_ctx->strict_std_compliance));
|
2022-08-25 10:01:19 +02:00
|
|
|
} else if (enc->codec->pix_fmts) {
|
2012-10-08 21:54:00 +03:00
|
|
|
const enum AVPixelFormat *p;
|
2012-08-09 01:26:38 +03:00
|
|
|
|
2022-08-25 10:01:19 +02:00
|
|
|
p = enc->codec->pix_fmts;
|
2021-04-06 03:01:14 +02:00
|
|
|
if (ost->enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
|
2022-08-25 10:01:19 +02:00
|
|
|
p = get_compliance_normal_pix_fmts(enc->codec, p);
|
2012-08-09 01:26:38 +03:00
|
|
|
}
|
|
|
|
|
2012-10-08 21:54:00 +03:00
|
|
|
for (; *p != AV_PIX_FMT_NONE; p++) {
|
2012-08-09 01:26:38 +03:00
|
|
|
const char *name = av_get_pix_fmt_name(*p);
|
2021-12-01 18:13:32 +02:00
|
|
|
av_bprintf(bprint, "%s%c", name, p[1] == AV_PIX_FMT_NONE ? '\0' : '|');
|
2012-08-09 01:26:38 +03:00
|
|
|
}
|
2021-12-01 18:13:32 +02:00
|
|
|
if (!av_bprint_is_complete(bprint))
|
2022-08-27 15:41:16 +02:00
|
|
|
report_and_exit(AVERROR(ENOMEM));
|
2021-12-01 18:13:32 +02:00
|
|
|
return bprint->str;
|
2012-08-09 01:26:38 +03:00
|
|
|
} else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-04-06 05:26:31 +02:00
|
|
|
/* Define a function for appending a list of allowed formats
|
|
|
|
* to an AVBPrint. If nonempty, the list will have a header. */
|
|
|
|
#define DEF_CHOOSE_FORMAT(name, type, var, supported_list, none, printf_format, get_name) \
|
|
|
|
static void choose_ ## name (OutputFilter *ofilter, AVBPrint *bprint) \
|
2012-08-02 09:24:02 +03:00
|
|
|
{ \
|
2021-04-06 05:26:31 +02:00
|
|
|
if (ofilter->var == none && !ofilter->supported_list) \
|
|
|
|
return; \
|
|
|
|
av_bprintf(bprint, #name "="); \
|
2016-05-23 14:09:08 +02:00
|
|
|
if (ofilter->var != none) { \
|
2021-04-06 05:26:31 +02:00
|
|
|
av_bprintf(bprint, printf_format, get_name(ofilter->var)); \
|
|
|
|
} else { \
|
2012-08-02 09:24:02 +03:00
|
|
|
const type *p; \
|
|
|
|
\
|
2016-05-23 14:09:08 +02:00
|
|
|
for (p = ofilter->supported_list; *p != none; p++) { \
|
2021-04-06 05:26:31 +02:00
|
|
|
av_bprintf(bprint, printf_format "|", get_name(*p)); \
|
2012-08-02 09:24:02 +03:00
|
|
|
} \
|
2021-04-06 05:26:31 +02:00
|
|
|
if (bprint->len > 0) \
|
|
|
|
bprint->str[--bprint->len] = '\0'; \
|
|
|
|
} \
|
|
|
|
av_bprint_chars(bprint, ':', 1); \
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
2016-11-13 16:33:39 +02:00
|
|
|
//DEF_CHOOSE_FORMAT(pix_fmts, enum AVPixelFormat, format, formats, AV_PIX_FMT_NONE,
|
|
|
|
// GET_PIX_FMT_NAME)
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2016-05-23 14:09:08 +02:00
|
|
|
DEF_CHOOSE_FORMAT(sample_fmts, enum AVSampleFormat, format, formats,
|
2021-04-06 05:26:31 +02:00
|
|
|
AV_SAMPLE_FMT_NONE, "%s", av_get_sample_fmt_name)
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2016-05-23 14:09:08 +02:00
|
|
|
DEF_CHOOSE_FORMAT(sample_rates, int, sample_rate, sample_rates, 0,
|
2021-04-06 05:26:31 +02:00
|
|
|
"%d", )
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2021-09-03 02:04:30 +02:00
|
|
|
static void choose_channel_layouts(OutputFilter *ofilter, AVBPrint *bprint)
|
|
|
|
{
|
|
|
|
if (av_channel_layout_check(&ofilter->ch_layout)) {
|
|
|
|
av_bprintf(bprint, "channel_layouts=");
|
|
|
|
av_channel_layout_describe_bprint(&ofilter->ch_layout, bprint);
|
|
|
|
} else if (ofilter->ch_layouts) {
|
|
|
|
const AVChannelLayout *p;
|
|
|
|
|
|
|
|
av_bprintf(bprint, "channel_layouts=");
|
|
|
|
for (p = ofilter->ch_layouts; p->nb_channels; p++) {
|
|
|
|
av_channel_layout_describe_bprint(p, bprint);
|
|
|
|
av_bprintf(bprint, "|");
|
|
|
|
}
|
|
|
|
if (bprint->len > 0)
|
|
|
|
bprint->str[--bprint->len] = '\0';
|
|
|
|
} else
|
|
|
|
return;
|
|
|
|
av_bprint_chars(bprint, ':', 1);
|
|
|
|
}
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2016-05-22 19:57:36 +02:00
|
|
|
int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
|
2012-08-02 09:24:02 +03:00
|
|
|
{
|
|
|
|
FilterGraph *fg = av_mallocz(sizeof(*fg));
|
2021-12-03 14:38:48 +02:00
|
|
|
OutputFilter *ofilter;
|
|
|
|
InputFilter *ifilter;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
if (!fg)
|
2022-08-27 15:41:16 +02:00
|
|
|
report_and_exit(AVERROR(ENOMEM));
|
2012-08-02 09:24:02 +03:00
|
|
|
fg->index = nb_filtergraphs;
|
|
|
|
|
2021-12-03 15:34:47 +02:00
|
|
|
ofilter = ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
|
2021-12-03 14:38:48 +02:00
|
|
|
ofilter->ost = ost;
|
|
|
|
ofilter->graph = fg;
|
|
|
|
ofilter->format = -1;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2021-12-03 14:38:48 +02:00
|
|
|
ost->filter = ofilter;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2021-12-03 15:34:47 +02:00
|
|
|
ifilter = ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
|
2021-12-03 14:38:48 +02:00
|
|
|
ifilter->ist = ist;
|
|
|
|
ifilter->graph = fg;
|
|
|
|
ifilter->format = -1;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2022-01-10 12:04:36 +02:00
|
|
|
ifilter->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
|
2021-12-03 14:38:48 +02:00
|
|
|
if (!ifilter->frame_queue)
|
2022-08-27 15:41:16 +02:00
|
|
|
report_and_exit(AVERROR(ENOMEM));
|
2016-05-27 12:14:33 +02:00
|
|
|
|
2012-06-07 22:52:07 +03:00
|
|
|
GROW_ARRAY(ist->filters, ist->nb_filters);
|
2021-12-03 14:38:48 +02:00
|
|
|
ist->filters[ist->nb_filters - 1] = ifilter;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2012-06-07 22:52:07 +03:00
|
|
|
GROW_ARRAY(filtergraphs, nb_filtergraphs);
|
2012-08-02 09:24:02 +03:00
|
|
|
filtergraphs[nb_filtergraphs - 1] = fg;
|
|
|
|
|
2016-05-22 19:57:36 +02:00
|
|
|
return 0;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
2017-02-08 13:55:58 +02:00
|
|
|
static char *describe_filter_link(FilterGraph *fg, AVFilterInOut *inout, int in)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inout->filter_ctx;
|
|
|
|
AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads;
|
|
|
|
int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs;
|
2021-12-01 17:36:19 +02:00
|
|
|
char *res;
|
2017-02-08 13:55:58 +02:00
|
|
|
|
|
|
|
if (nb_pads > 1)
|
2021-12-01 17:36:19 +02:00
|
|
|
res = av_strdup(ctx->filter->name);
|
|
|
|
else
|
|
|
|
res = av_asprintf("%s:%s", ctx->filter->name,
|
|
|
|
avfilter_pad_get_name(pads, inout->pad_idx));
|
|
|
|
if (!res)
|
2022-08-27 15:41:16 +02:00
|
|
|
report_and_exit(AVERROR(ENOMEM));
|
2017-02-08 13:55:58 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
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);
|
2021-12-03 14:38:48 +02:00
|
|
|
InputFilter *ifilter;
|
2012-08-02 09:24:02 +03:00
|
|
|
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");
|
2013-07-08 12:09:22 +03:00
|
|
|
exit_program(1);
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2012-08-09 01:26:38 +03:00
|
|
|
av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
|
2012-08-02 09:24:02 +03:00
|
|
|
file_idx, fg->graph_desc);
|
2013-07-08 12:09:22 +03:00
|
|
|
exit_program(1);
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
s = input_files[file_idx]->ctx;
|
|
|
|
|
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
2016-09-01 16:48:45 +02:00
|
|
|
enum AVMediaType stream_type = s->streams[i]->codecpar->codec_type;
|
2012-08-09 01:26:38 +03:00
|
|
|
if (stream_type != type &&
|
|
|
|
!(stream_type == AVMEDIA_TYPE_SUBTITLE &&
|
|
|
|
type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
|
2012-08-02 09:24:02 +03:00
|
|
|
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);
|
2013-07-08 12:09:22 +03:00
|
|
|
exit_program(1);
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
ist = input_streams[input_files[file_idx]->ist_index + st->index];
|
2018-12-29 12:47:05 +02:00
|
|
|
if (ist->user_set_discard == AVDISCARD_ALL) {
|
|
|
|
av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
|
|
|
|
"matches a disabled input stream.\n", p, fg->graph_desc);
|
|
|
|
exit_program(1);
|
|
|
|
}
|
2012-08-02 09:24:02 +03:00
|
|
|
} else {
|
|
|
|
/* find the first unused stream of corresponding type */
|
|
|
|
for (i = 0; i < nb_input_streams; i++) {
|
|
|
|
ist = input_streams[i];
|
2018-12-29 12:47:05 +02:00
|
|
|
if (ist->user_set_discard == AVDISCARD_ALL)
|
|
|
|
continue;
|
2014-04-05 09:48:24 +03:00
|
|
|
if (ist->dec_ctx->codec_type == type && ist->discard)
|
2012-08-02 09:24:02 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == nb_input_streams) {
|
|
|
|
av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
|
2012-08-09 01:26:38 +03:00
|
|
|
"unlabeled input pad %d on filter %s\n", in->pad_idx,
|
2012-08-02 09:24:02 +03:00
|
|
|
in->filter_ctx->name);
|
2013-07-08 12:09:22 +03:00
|
|
|
exit_program(1);
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
av_assert0(ist);
|
|
|
|
|
|
|
|
ist->discard = 0;
|
2014-09-01 05:13:49 +03:00
|
|
|
ist->decoding_needed |= DECODING_FOR_FILTER;
|
2022-02-14 19:07:01 +02:00
|
|
|
ist->processing_needed = 1;
|
2012-08-02 09:24:02 +03:00
|
|
|
ist->st->discard = AVDISCARD_NONE;
|
|
|
|
|
2021-12-03 15:34:47 +02:00
|
|
|
ifilter = ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
|
2021-12-03 14:38:48 +02:00
|
|
|
ifilter->ist = ist;
|
|
|
|
ifilter->graph = fg;
|
|
|
|
ifilter->format = -1;
|
|
|
|
ifilter->type = ist->st->codecpar->codec_type;
|
|
|
|
ifilter->name = describe_filter_link(fg, in, 1);
|
|
|
|
|
2022-01-10 12:04:36 +02:00
|
|
|
ifilter->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
|
2021-12-03 14:38:48 +02:00
|
|
|
if (!ifilter->frame_queue)
|
2022-08-27 15:41:16 +02:00
|
|
|
report_and_exit(AVERROR(ENOMEM));
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2012-06-07 22:52:07 +03:00
|
|
|
GROW_ARRAY(ist->filters, ist->nb_filters);
|
2021-12-03 14:38:48 +02:00
|
|
|
ist->filters[ist->nb_filters - 1] = ifilter;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
2015-07-15 19:49:24 +02:00
|
|
|
int init_complex_filtergraph(FilterGraph *fg)
|
|
|
|
{
|
|
|
|
AVFilterInOut *inputs, *outputs, *cur;
|
|
|
|
AVFilterGraph *graph;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* this graph is only used for determining the kinds of inputs
|
|
|
|
* and outputs we have, and is discarded on exit from this function */
|
|
|
|
graph = avfilter_graph_alloc();
|
|
|
|
if (!graph)
|
|
|
|
return AVERROR(ENOMEM);
|
2017-11-17 03:09:37 +02:00
|
|
|
graph->nb_threads = 1;
|
2015-07-15 19:49:24 +02:00
|
|
|
|
|
|
|
ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
for (cur = inputs; cur; cur = cur->next)
|
|
|
|
init_input_filter(fg, cur);
|
|
|
|
|
|
|
|
for (cur = outputs; cur;) {
|
2021-12-03 15:34:47 +02:00
|
|
|
OutputFilter *const ofilter = ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
|
2015-07-15 19:49:24 +02:00
|
|
|
|
2021-12-03 14:38:48 +02:00
|
|
|
ofilter->graph = fg;
|
|
|
|
ofilter->out_tmp = cur;
|
|
|
|
ofilter->type = avfilter_pad_get_type(cur->filter_ctx->output_pads,
|
2015-07-15 19:49:24 +02:00
|
|
|
cur->pad_idx);
|
2021-12-03 14:38:48 +02:00
|
|
|
ofilter->name = describe_filter_link(fg, cur, 0);
|
2015-07-15 19:49:24 +02:00
|
|
|
cur = cur->next;
|
2021-12-03 14:38:48 +02:00
|
|
|
ofilter->out_tmp->next = NULL;
|
2015-07-15 19:49:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
avfilter_inout_free(&inputs);
|
|
|
|
avfilter_graph_free(&graph);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-15 10:59:40 +03:00
|
|
|
static int insert_trim(int64_t start_time, int64_t duration,
|
|
|
|
AVFilterContext **last_filter, int *pad_idx,
|
|
|
|
const char *filter_name)
|
2013-04-10 16:02:01 +03:00
|
|
|
{
|
|
|
|
AVFilterGraph *graph = (*last_filter)->graph;
|
|
|
|
AVFilterContext *ctx;
|
|
|
|
const AVFilter *trim;
|
2013-06-15 10:59:40 +03:00
|
|
|
enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
|
|
|
|
const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
|
2013-04-10 16:02:01 +03:00
|
|
|
int ret = 0;
|
|
|
|
|
2013-06-15 10:59:40 +03:00
|
|
|
if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
|
2013-04-10 16:02:01 +03:00
|
|
|
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);
|
|
|
|
|
2013-06-15 10:59:40 +03:00
|
|
|
if (duration != INT64_MAX) {
|
2013-07-13 00:40:58 +03:00
|
|
|
ret = av_opt_set_int(ctx, "durationi", duration,
|
2013-04-10 16:02:01 +03:00
|
|
|
AV_OPT_SEARCH_CHILDREN);
|
|
|
|
}
|
2013-06-15 10:59:40 +03:00
|
|
|
if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
|
2013-07-13 00:40:58 +03:00
|
|
|
ret = av_opt_set_int(ctx, "starti", start_time,
|
2013-04-10 16:02:01 +03:00
|
|
|
AV_OPT_SEARCH_CHILDREN);
|
|
|
|
}
|
2013-04-10 16:02:01 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-04-29 16:59:01 +02:00
|
|
|
static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
|
|
|
|
const char *filter_name, const char *args)
|
|
|
|
{
|
|
|
|
AVFilterGraph *graph = (*last_filter)->graph;
|
|
|
|
AVFilterContext *ctx;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = avfilter_graph_create_filter(&ctx,
|
|
|
|
avfilter_get_by_name(filter_name),
|
|
|
|
filter_name, args, NULL, graph);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
|
|
|
|
{
|
|
|
|
OutputStream *ost = ofilter->ost;
|
2013-06-15 10:59:40 +03:00
|
|
|
OutputFile *of = output_files[ost->file_index];
|
2012-08-02 09:24:02 +03:00
|
|
|
AVFilterContext *last_filter = out->filter_ctx;
|
2021-12-01 18:13:32 +02:00
|
|
|
AVBPrint bprint;
|
2012-08-02 09:24:02 +03:00
|
|
|
int pad_idx = out->pad_idx;
|
|
|
|
int ret;
|
2021-12-01 18:13:32 +02:00
|
|
|
const char *pix_fmts;
|
2012-08-02 09:24:02 +03:00
|
|
|
char name[255];
|
|
|
|
|
2016-04-28 14:47:43 +02:00
|
|
|
snprintf(name, sizeof(name), "out_%d_%d", ost->file_index, ost->index);
|
2012-08-02 09:24:02 +03:00
|
|
|
ret = avfilter_graph_create_filter(&ofilter->filter,
|
2013-03-11 23:49:04 +03:00
|
|
|
avfilter_get_by_name("buffersink"),
|
2012-08-18 17:15:42 +03:00
|
|
|
name, NULL, NULL, fg->graph);
|
2012-08-09 01:26:38 +03:00
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2020-06-08 10:58:05 +02:00
|
|
|
if ((ofilter->width || ofilter->height) && ofilter->ost->autoscale) {
|
2012-08-02 09:24:02 +03:00
|
|
|
char args[255];
|
|
|
|
AVFilterContext *filter;
|
2021-10-19 00:27:01 +02:00
|
|
|
const AVDictionaryEntry *e = NULL;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2015-08-08 13:33:46 +02:00
|
|
|
snprintf(args, sizeof(args), "%d:%d",
|
2016-11-13 16:33:39 +02:00
|
|
|
ofilter->width, ofilter->height);
|
2015-08-08 13:33:46 +02:00
|
|
|
|
|
|
|
while ((e = av_dict_get(ost->sws_dict, "", e,
|
|
|
|
AV_DICT_IGNORE_SUFFIX))) {
|
|
|
|
av_strlcatf(args, sizeof(args), ":%s=%s", e->key, e->value);
|
|
|
|
}
|
|
|
|
|
2016-04-28 14:47:43 +02:00
|
|
|
snprintf(name, sizeof(name), "scaler_out_%d_%d",
|
2012-08-02 09:24:02 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-01 18:13:32 +02:00
|
|
|
av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
|
|
|
|
if ((pix_fmts = choose_pix_fmts(ofilter, &bprint))) {
|
2012-08-02 09:24:02 +03:00
|
|
|
AVFilterContext *filter;
|
2021-04-06 06:21:22 +02:00
|
|
|
|
2013-04-21 12:59:34 +03:00
|
|
|
ret = avfilter_graph_create_filter(&filter,
|
2014-10-14 18:46:44 +03:00
|
|
|
avfilter_get_by_name("format"),
|
|
|
|
"format", pix_fmts, NULL, fg->graph);
|
2021-12-01 18:13:32 +02:00
|
|
|
av_bprint_finalize(&bprint, NULL);
|
2013-04-21 12:59:34 +03:00
|
|
|
if (ret < 0)
|
2012-08-02 09:24:02 +03:00
|
|
|
return ret;
|
|
|
|
if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
last_filter = filter;
|
|
|
|
pad_idx = 0;
|
|
|
|
}
|
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
if (ost->frame_rate.num && 0) {
|
2012-08-02 09:24:02 +03:00
|
|
|
AVFilterContext *fps;
|
|
|
|
char args[255];
|
|
|
|
|
|
|
|
snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
|
|
|
|
ost->frame_rate.den);
|
2016-04-28 14:47:43 +02:00
|
|
|
snprintf(name, sizeof(name), "fps_out_%d_%d",
|
2012-08-02 09:24:02 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-28 14:47:43 +02:00
|
|
|
snprintf(name, sizeof(name), "trim_out_%d_%d",
|
2013-06-15 10:59:40 +03:00
|
|
|
ost->file_index, ost->index);
|
|
|
|
ret = insert_trim(of->start_time, of->recording_time,
|
|
|
|
&last_filter, &pad_idx, name);
|
2013-04-10 16:02:01 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
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;
|
2013-06-15 10:59:40 +03:00
|
|
|
OutputFile *of = output_files[ost->file_index];
|
2014-04-05 09:48:24 +03:00
|
|
|
AVCodecContext *codec = ost->enc_ctx;
|
2012-08-02 09:24:02 +03:00
|
|
|
AVFilterContext *last_filter = out->filter_ctx;
|
|
|
|
int pad_idx = out->pad_idx;
|
2021-04-06 05:26:31 +02:00
|
|
|
AVBPrint args;
|
2012-08-02 09:24:02 +03:00
|
|
|
char name[255];
|
|
|
|
int ret;
|
|
|
|
|
2016-04-28 14:47:43 +02:00
|
|
|
snprintf(name, sizeof(name), "out_%d_%d", ost->file_index, ost->index);
|
2012-08-02 09:24:02 +03:00
|
|
|
ret = avfilter_graph_create_filter(&ofilter->filter,
|
2013-03-11 23:49:04 +03:00
|
|
|
avfilter_get_by_name("abuffersink"),
|
2013-04-11 15:58:08 +03:00
|
|
|
name, NULL, NULL, fg->graph);
|
2012-08-02 09:24:02 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-04-11 15:58:08 +03:00
|
|
|
if ((ret = av_opt_set_int(ofilter->filter, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
|
|
|
|
return ret;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
#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) \
|
2021-04-06 05:26:31 +02:00
|
|
|
goto fail; \
|
2012-08-09 01:26:38 +03:00
|
|
|
\
|
|
|
|
ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
|
|
|
|
if (ret < 0) \
|
2021-04-06 05:26:31 +02:00
|
|
|
goto fail; \
|
2012-08-09 01:26:38 +03:00
|
|
|
\
|
|
|
|
last_filter = filt_ctx; \
|
|
|
|
pad_idx = 0; \
|
|
|
|
} while (0)
|
2021-04-06 05:26:31 +02:00
|
|
|
av_bprint_init(&args, 0, AV_BPRINT_SIZE_UNLIMITED);
|
2022-06-19 20:09:24 +02:00
|
|
|
#if FFMPEG_OPT_MAP_CHANNEL
|
2012-08-09 01:26:38 +03:00
|
|
|
if (ost->audio_channels_mapped) {
|
2021-09-03 02:04:30 +02:00
|
|
|
AVChannelLayout mapped_layout = { 0 };
|
2012-08-09 01:26:38 +03:00
|
|
|
int i;
|
2021-09-03 02:04:30 +02:00
|
|
|
av_channel_layout_default(&mapped_layout, ost->audio_channels_mapped);
|
|
|
|
av_channel_layout_describe_bprint(&mapped_layout, &args);
|
2012-08-09 01:26:38 +03:00
|
|
|
for (i = 0; i < ost->audio_channels_mapped; i++)
|
|
|
|
if (ost->audio_channels_map[i] != -1)
|
2021-04-06 05:26:31 +02:00
|
|
|
av_bprintf(&args, "|c%d=c%d", i, ost->audio_channels_map[i]);
|
2012-08-09 01:26:38 +03:00
|
|
|
|
2021-04-06 05:26:31 +02:00
|
|
|
AUTO_INSERT_FILTER("-map_channel", "pan", args.str);
|
|
|
|
av_bprint_clear(&args);
|
2012-08-09 01:26:38 +03:00
|
|
|
}
|
2022-06-19 20:09:24 +02:00
|
|
|
#endif
|
2012-08-09 01:26:38 +03:00
|
|
|
|
2021-09-03 02:04:30 +02:00
|
|
|
if (codec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
|
|
|
|
av_channel_layout_default(&codec->ch_layout, codec->ch_layout.nb_channels);
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2021-04-06 05:26:31 +02:00
|
|
|
choose_sample_fmts(ofilter, &args);
|
|
|
|
choose_sample_rates(ofilter, &args);
|
|
|
|
choose_channel_layouts(ofilter, &args);
|
|
|
|
if (!av_bprint_is_complete(&args)) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (args.len) {
|
2012-08-02 09:24:02 +03:00
|
|
|
AVFilterContext *format;
|
|
|
|
|
2016-04-28 14:47:43 +02:00
|
|
|
snprintf(name, sizeof(name), "format_out_%d_%d",
|
2012-08-02 09:24:02 +03:00
|
|
|
ost->file_index, ost->index);
|
|
|
|
ret = avfilter_graph_create_filter(&format,
|
|
|
|
avfilter_get_by_name("aformat"),
|
2021-04-06 05:26:31 +02:00
|
|
|
name, args.str, NULL, fg->graph);
|
2012-08-02 09:24:02 +03:00
|
|
|
if (ret < 0)
|
2021-04-06 05:26:31 +02:00
|
|
|
goto fail;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
ret = avfilter_link(last_filter, pad_idx, format, 0);
|
|
|
|
if (ret < 0)
|
2021-04-06 05:26:31 +02:00
|
|
|
goto fail;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
last_filter = format;
|
|
|
|
pad_idx = 0;
|
|
|
|
}
|
|
|
|
|
2013-05-09 00:43:19 +03:00
|
|
|
if (ost->apad && of->shortest) {
|
|
|
|
int i;
|
|
|
|
|
2022-03-30 10:50:37 +02:00
|
|
|
for (i = 0; i < of->nb_streams; i++)
|
2022-10-14 10:57:19 +02:00
|
|
|
if (of->streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
|
2013-05-09 00:43:19 +03:00
|
|
|
break;
|
|
|
|
|
2022-03-30 10:50:37 +02:00
|
|
|
if (i < of->nb_streams) {
|
2021-04-06 07:05:00 +02:00
|
|
|
AUTO_INSERT_FILTER("-apad", "apad", ost->apad);
|
2013-05-09 00:43:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 10:59:40 +03:00
|
|
|
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);
|
2013-04-10 16:02:01 +03:00
|
|
|
if (ret < 0)
|
2021-04-06 05:26:31 +02:00
|
|
|
goto fail;
|
2013-04-10 16:02:01 +03:00
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
|
2021-04-06 05:26:31 +02:00
|
|
|
goto fail;
|
|
|
|
fail:
|
|
|
|
av_bprint_finalize(&args, NULL);
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2021-04-06 05:26:31 +02:00
|
|
|
return ret;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 05:39:54 +02:00
|
|
|
static int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter,
|
|
|
|
AVFilterInOut *out)
|
2012-08-02 09:24:02 +03:00
|
|
|
{
|
2015-07-19 14:56:09 +02:00
|
|
|
if (!ofilter->ost) {
|
2016-03-29 00:07:47 +02:00
|
|
|
av_log(NULL, AV_LOG_FATAL, "Filter %s has an unconnected output\n", ofilter->name);
|
2015-07-19 14:56:09 +02:00
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
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);
|
2021-08-06 15:28:35 +02:00
|
|
|
default: av_assert0(0); return 0;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-05 00:15:15 +02:00
|
|
|
void check_filter_outputs(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < nb_filtergraphs; i++) {
|
|
|
|
int n;
|
|
|
|
for (n = 0; n < filtergraphs[i]->nb_outputs; n++) {
|
|
|
|
OutputFilter *output = filtergraphs[i]->outputs[n];
|
|
|
|
if (!output->ost) {
|
|
|
|
av_log(NULL, AV_LOG_FATAL, "Filter %s has an unconnected output\n", output->name);
|
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 15:53:58 +02:00
|
|
|
static int sub2video_prepare(InputStream *ist, InputFilter *ifilter)
|
2012-08-09 01:26:38 +03:00
|
|
|
{
|
|
|
|
AVFormatContext *avf = input_files[ist->file_index]->ctx;
|
2013-03-10 13:51:02 +03:00
|
|
|
int i, w, h;
|
2012-08-09 01:26:38 +03:00
|
|
|
|
|
|
|
/* Compute the size of the canvas for the subtitles stream.
|
2016-09-01 16:48:45 +02:00
|
|
|
If the subtitles codecpar has set a size, use it. Otherwise use the
|
2012-08-09 01:26:38 +03:00
|
|
|
maximum dimensions of the video streams in the same file. */
|
2016-11-03 15:53:58 +02:00
|
|
|
w = ifilter->width;
|
|
|
|
h = ifilter->height;
|
2012-08-09 01:26:38 +03:00
|
|
|
if (!(w && h)) {
|
|
|
|
for (i = 0; i < avf->nb_streams; i++) {
|
2016-09-01 16:48:45 +02:00
|
|
|
if (avf->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
w = FFMAX(w, avf->streams[i]->codecpar->width);
|
|
|
|
h = FFMAX(h, avf->streams[i]->codecpar->height);
|
2012-08-09 01:26:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2016-05-27 12:14:33 +02:00
|
|
|
ist->sub2video.w = ifilter->width = w;
|
|
|
|
ist->sub2video.h = ifilter->height = h;
|
|
|
|
|
|
|
|
ifilter->width = ist->dec_ctx->width ? ist->dec_ctx->width : ist->sub2video.w;
|
|
|
|
ifilter->height = ist->dec_ctx->height ? ist->dec_ctx->height : ist->sub2video.h;
|
2012-08-09 01:26:38 +03:00
|
|
|
|
2012-10-08 21:54:00 +03:00
|
|
|
/* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
|
2012-08-09 01:26:38 +03:00
|
|
|
palettes for all rectangles are identical or compatible */
|
2016-05-27 12:14:33 +02:00
|
|
|
ifilter->format = AV_PIX_FMT_RGB32;
|
2012-08-09 01:26:38 +03:00
|
|
|
|
2013-03-10 13:51:02 +03:00
|
|
|
ist->sub2video.frame = av_frame_alloc();
|
|
|
|
if (!ist->sub2video.frame)
|
2012-08-09 01:26:38 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2014-11-01 15:38:02 +02:00
|
|
|
ist->sub2video.last_pts = INT64_MIN;
|
2019-02-20 20:54:11 +02:00
|
|
|
ist->sub2video.end_pts = INT64_MIN;
|
2019-02-23 01:48:08 +02:00
|
|
|
|
|
|
|
/* sub2video structure has been (re-)initialized.
|
|
|
|
Mark it as such so that the system will be
|
|
|
|
initialized with the first received heartbeat. */
|
|
|
|
ist->sub2video.initialize = 1;
|
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
|
|
|
|
AVFilterInOut *in)
|
|
|
|
{
|
2013-06-14 19:25:30 +03:00
|
|
|
AVFilterContext *last_filter;
|
2013-06-14 19:12:23 +03:00
|
|
|
const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
|
2021-09-21 14:29:15 +02:00
|
|
|
const AVPixFmtDescriptor *desc;
|
2012-08-02 09:24:02 +03:00
|
|
|
InputStream *ist = ifilter->ist;
|
2013-06-15 10:59:40 +03:00
|
|
|
InputFile *f = input_files[ist->file_index];
|
2012-08-02 09:24:02 +03:00
|
|
|
AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
|
|
|
|
ist->st->time_base;
|
2013-03-28 00:08:53 +03:00
|
|
|
AVRational fr = ist->framerate;
|
2012-08-02 09:24:02 +03:00
|
|
|
AVRational sar;
|
2012-08-09 01:26:38 +03:00
|
|
|
AVBPrint args;
|
|
|
|
char name[255];
|
2013-06-15 10:59:40 +03:00
|
|
|
int ret, pad_idx = 0;
|
2014-11-24 07:31:20 +02:00
|
|
|
int64_t tsoffset = 0;
|
2016-05-08 23:39:20 +02:00
|
|
|
AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2016-02-12 10:22:15 +02:00
|
|
|
if (!par)
|
|
|
|
return AVERROR(ENOMEM);
|
2016-05-08 23:39:20 +02:00
|
|
|
memset(par, 0, sizeof(*par));
|
|
|
|
par->format = AV_PIX_FMT_NONE;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2014-06-01 15:53:34 +03:00
|
|
|
if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
|
2013-08-14 03:11:30 +03:00
|
|
|
av_log(NULL, AV_LOG_ERROR, "Cannot connect video filter to audio input\n");
|
2016-05-19 00:10:27 +02:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
2013-08-14 03:11:30 +03:00
|
|
|
}
|
|
|
|
|
2013-03-28 00:08:53 +03:00
|
|
|
if (!fr.num)
|
2022-08-18 15:08:26 +02:00
|
|
|
fr = ist->framerate_guessed;
|
2012-10-15 01:14:43 +03:00
|
|
|
|
2014-06-01 15:53:34 +03:00
|
|
|
if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
|
2016-11-03 15:53:58 +02:00
|
|
|
ret = sub2video_prepare(ist, ifilter);
|
2012-08-09 01:26:38 +03:00
|
|
|
if (ret < 0)
|
2016-05-19 00:10:27 +02:00
|
|
|
goto fail;
|
2012-08-09 01:26:38 +03:00
|
|
|
}
|
|
|
|
|
2016-11-03 15:53:58 +02:00
|
|
|
sar = ifilter->sample_aspect_ratio;
|
2012-08-09 01:26:38 +03:00
|
|
|
if(!sar.den)
|
|
|
|
sar = (AVRational){0,1};
|
2018-06-10 09:57:26 +02:00
|
|
|
av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
|
2012-08-09 01:26:38 +03:00
|
|
|
av_bprintf(&args,
|
|
|
|
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
|
2019-12-04 15:41:46 +02:00
|
|
|
"pixel_aspect=%d/%d",
|
2016-11-03 15:53:58 +02:00
|
|
|
ifilter->width, ifilter->height, ifilter->format,
|
2019-12-04 15:41:46 +02:00
|
|
|
tb.num, tb.den, sar.num, sar.den);
|
2012-08-09 01:26:38 +03:00
|
|
|
if (fr.num && fr.den)
|
|
|
|
av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
|
2012-08-02 09:24:02 +03:00
|
|
|
snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
|
|
|
|
ist->file_index, ist->st->index);
|
|
|
|
|
2016-05-08 23:39:20 +02:00
|
|
|
|
2013-06-14 19:12:23 +03:00
|
|
|
if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name,
|
2012-08-09 01:26:38 +03:00
|
|
|
args.str, NULL, fg->graph)) < 0)
|
2016-05-19 00:10:27 +02:00
|
|
|
goto fail;
|
2016-11-03 15:53:58 +02:00
|
|
|
par->hw_frames_ctx = ifilter->hw_frames_ctx;
|
2016-05-08 23:39:20 +02:00
|
|
|
ret = av_buffersrc_parameters_set(ifilter->filter, par);
|
2016-02-12 10:22:15 +02:00
|
|
|
if (ret < 0)
|
2016-05-19 00:10:27 +02:00
|
|
|
goto fail;
|
2016-05-08 23:39:20 +02:00
|
|
|
av_freep(&par);
|
2013-06-14 19:25:30 +03:00
|
|
|
last_filter = ifilter->filter;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2021-09-21 14:29:15 +02:00
|
|
|
desc = av_pix_fmt_desc_get(ifilter->format);
|
|
|
|
av_assert0(desc);
|
|
|
|
|
|
|
|
// TODO: insert hwaccel enabled filters like transpose_vaapi into the graph
|
|
|
|
if (ist->autorotate && !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
|
2021-09-08 20:34:25 +02:00
|
|
|
int32_t *displaymatrix = ifilter->displaymatrix;
|
|
|
|
double theta;
|
|
|
|
|
|
|
|
if (!displaymatrix)
|
|
|
|
displaymatrix = (int32_t *)av_stream_get_side_data(ist->st, AV_PKT_DATA_DISPLAYMATRIX, NULL);
|
|
|
|
theta = get_rotation(displaymatrix);
|
2015-05-03 23:53:32 +02:00
|
|
|
|
|
|
|
if (fabs(theta - 90) < 1.0) {
|
2021-12-19 00:19:05 +02:00
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "transpose",
|
|
|
|
displaymatrix[3] > 0 ? "cclock_flip" : "clock");
|
2015-05-03 23:53:32 +02:00
|
|
|
} else if (fabs(theta - 180) < 1.0) {
|
2021-09-08 20:34:26 +02:00
|
|
|
if (displaymatrix[0] < 0) {
|
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (displaymatrix[4] < 0) {
|
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
|
|
|
|
}
|
2015-05-03 23:53:32 +02:00
|
|
|
} else if (fabs(theta - 270) < 1.0) {
|
2021-12-19 00:19:05 +02:00
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "transpose",
|
|
|
|
displaymatrix[3] < 0 ? "clock_flip" : "cclock");
|
2015-05-03 23:53:32 +02:00
|
|
|
} else if (fabs(theta) > 1.0) {
|
|
|
|
char rotate_buf[64];
|
|
|
|
snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta);
|
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "rotate", rotate_buf);
|
2021-09-08 20:34:26 +02:00
|
|
|
} else if (fabs(theta) < 1.0) {
|
|
|
|
if (displaymatrix && displaymatrix[4] < 0) {
|
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
|
|
|
|
}
|
2015-04-29 16:59:01 +02:00
|
|
|
}
|
2015-05-03 23:53:32 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2015-04-29 16:59:01 +02:00
|
|
|
}
|
|
|
|
|
2016-04-28 14:47:43 +02:00
|
|
|
snprintf(name, sizeof(name), "trim_in_%d_%d",
|
2013-06-15 10:59:40 +03:00
|
|
|
ist->file_index, ist->st->index);
|
2014-11-24 07:31:20 +02:00
|
|
|
if (copy_ts) {
|
|
|
|
tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
|
|
|
|
if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
|
|
|
|
tsoffset += f->ctx->start_time;
|
|
|
|
}
|
2013-06-15 10:59:40 +03:00
|
|
|
ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
|
2014-11-24 07:31:20 +02:00
|
|
|
AV_NOPTS_VALUE : tsoffset, f->recording_time,
|
|
|
|
&last_filter, &pad_idx, name);
|
2013-06-15 10:59:40 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-06-14 19:25:30 +03:00
|
|
|
if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
|
2012-08-02 09:24:02 +03:00
|
|
|
return ret;
|
|
|
|
return 0;
|
2016-05-19 00:10:27 +02:00
|
|
|
fail:
|
|
|
|
av_freep(&par);
|
|
|
|
|
|
|
|
return ret;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
|
|
|
|
AVFilterInOut *in)
|
|
|
|
{
|
2013-06-14 19:25:30 +03:00
|
|
|
AVFilterContext *last_filter;
|
2013-06-14 19:12:23 +03:00
|
|
|
const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
|
2012-08-02 09:24:02 +03:00
|
|
|
InputStream *ist = ifilter->ist;
|
2013-06-15 10:59:40 +03:00
|
|
|
InputFile *f = input_files[ist->file_index];
|
2012-12-26 18:53:02 +03:00
|
|
|
AVBPrint args;
|
|
|
|
char name[255];
|
2013-06-15 10:59:40 +03:00
|
|
|
int ret, pad_idx = 0;
|
2014-11-24 07:31:20 +02:00
|
|
|
int64_t tsoffset = 0;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2014-06-01 15:35:53 +03:00
|
|
|
if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_AUDIO) {
|
2013-08-14 03:11:30 +03:00
|
|
|
av_log(NULL, AV_LOG_ERROR, "Cannot connect audio filter to non audio input\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2012-12-26 18:53:02 +03:00
|
|
|
av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
|
|
|
|
av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
|
2016-11-03 15:53:58 +02:00
|
|
|
1, ifilter->sample_rate,
|
|
|
|
ifilter->sample_rate,
|
|
|
|
av_get_sample_fmt_name(ifilter->format));
|
2021-09-03 02:04:30 +02:00
|
|
|
if (av_channel_layout_check(&ifilter->ch_layout) &&
|
|
|
|
ifilter->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
|
|
|
|
av_bprintf(&args, ":channel_layout=");
|
|
|
|
av_channel_layout_describe_bprint(&ifilter->ch_layout, &args);
|
|
|
|
} else
|
|
|
|
av_bprintf(&args, ":channels=%d", ifilter->ch_layout.nb_channels);
|
2016-04-28 14:47:43 +02:00
|
|
|
snprintf(name, sizeof(name), "graph_%d_in_%d_%d", fg->index,
|
2012-08-02 09:24:02 +03:00
|
|
|
ist->file_index, ist->st->index);
|
|
|
|
|
2013-06-14 19:12:23 +03:00
|
|
|
if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
|
2012-12-26 18:53:02 +03:00
|
|
|
name, args.str, NULL,
|
2012-08-02 09:24:02 +03:00
|
|
|
fg->graph)) < 0)
|
|
|
|
return ret;
|
2013-06-14 19:25:30 +03:00
|
|
|
last_filter = ifilter->filter;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
#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); \
|
|
|
|
\
|
2016-04-28 14:47:43 +02:00
|
|
|
snprintf(name, sizeof(name), "graph_%d_%s_in_%d_%d", \
|
2012-08-09 01:26:38 +03:00
|
|
|
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; \
|
|
|
|
\
|
2013-08-06 11:08:17 +03:00
|
|
|
ret = avfilter_link(last_filter, 0, filt_ctx, 0); \
|
2012-08-09 01:26:38 +03:00
|
|
|
if (ret < 0) \
|
|
|
|
return ret; \
|
|
|
|
\
|
2013-08-06 11:08:17 +03:00
|
|
|
last_filter = filt_ctx; \
|
2012-08-09 01:26:38 +03:00
|
|
|
} while (0)
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2013-06-15 10:59:40 +03:00
|
|
|
snprintf(name, sizeof(name), "trim for input stream %d:%d",
|
|
|
|
ist->file_index, ist->st->index);
|
2014-11-24 07:31:20 +02:00
|
|
|
if (copy_ts) {
|
|
|
|
tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
|
|
|
|
if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
|
|
|
|
tsoffset += f->ctx->start_time;
|
|
|
|
}
|
2013-06-15 10:59:40 +03:00
|
|
|
ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
|
2014-11-24 07:31:20 +02:00
|
|
|
AV_NOPTS_VALUE : tsoffset, f->recording_time,
|
|
|
|
&last_filter, &pad_idx, name);
|
2013-06-15 10:59:40 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-06-14 19:25:30 +03:00
|
|
|
if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
|
2012-08-02 09:24:02 +03:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
|
|
|
|
AVFilterInOut *in)
|
|
|
|
{
|
2014-07-18 11:34:39 +03:00
|
|
|
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;
|
|
|
|
}
|
2012-08-02 09:24:02 +03:00
|
|
|
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);
|
2021-08-06 15:28:35 +02:00
|
|
|
default: av_assert0(0); return 0;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 20:14:58 +02:00
|
|
|
static void cleanup_filtergraph(FilterGraph *fg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < fg->nb_outputs; i++)
|
|
|
|
fg->outputs[i]->filter = (AVFilterContext *)NULL;
|
|
|
|
for (i = 0; i < fg->nb_inputs; i++)
|
|
|
|
fg->inputs[i]->filter = (AVFilterContext *)NULL;
|
|
|
|
avfilter_graph_free(&fg->graph);
|
|
|
|
}
|
|
|
|
|
2021-11-23 12:22:57 +02:00
|
|
|
static int filter_is_buffersrc(const AVFilterContext *f)
|
|
|
|
{
|
|
|
|
return f->nb_inputs == 0 &&
|
2022-01-21 10:11:50 +02:00
|
|
|
(!strcmp(f->filter->name, "buffer") ||
|
|
|
|
!strcmp(f->filter->name, "abuffer"));
|
2021-11-23 12:22:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int graph_is_meta(AVFilterGraph *graph)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < graph->nb_filters; i++) {
|
|
|
|
const AVFilterContext *f = graph->filters[i];
|
|
|
|
|
|
|
|
/* in addition to filters flagged as meta, also
|
|
|
|
* disregard sinks and buffersources (but not other sources,
|
|
|
|
* since they introduce data we are not aware of)
|
|
|
|
*/
|
|
|
|
if (!((f->filter->flags & AVFILTER_FLAG_METADATA_ONLY) ||
|
|
|
|
f->nb_outputs == 0 ||
|
|
|
|
filter_is_buffersrc(f)))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
int configure_filtergraph(FilterGraph *fg)
|
|
|
|
{
|
|
|
|
AVFilterInOut *inputs, *outputs, *cur;
|
2016-05-22 17:57:59 +02:00
|
|
|
int ret, i, simple = filtergraph_is_simple(fg);
|
2012-08-02 09:24:02 +03:00
|
|
|
const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
|
|
|
|
fg->graph_desc;
|
|
|
|
|
2017-03-02 20:14:58 +02:00
|
|
|
cleanup_filtergraph(fg);
|
2012-08-02 09:24:02 +03:00
|
|
|
if (!(fg->graph = avfilter_graph_alloc()))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
if (simple) {
|
|
|
|
OutputStream *ost = fg->outputs[0]->ost;
|
2012-12-19 05:47:28 +03:00
|
|
|
char args[512];
|
2021-10-19 00:27:01 +02:00
|
|
|
const AVDictionaryEntry *e = NULL;
|
2012-12-19 05:47:28 +03:00
|
|
|
|
2021-09-03 15:30:32 +02:00
|
|
|
if (filter_nbthreads) {
|
|
|
|
ret = av_opt_set(fg->graph, "threads", filter_nbthreads, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
} else {
|
2021-08-08 11:42:20 +02:00
|
|
|
e = av_dict_get(ost->encoder_opts, "threads", NULL, 0);
|
|
|
|
if (e)
|
|
|
|
av_opt_set(fg->graph, "threads", e->value, 0);
|
|
|
|
}
|
2016-10-30 15:25:39 +02:00
|
|
|
|
2015-08-08 13:33:46 +02:00
|
|
|
args[0] = 0;
|
2021-08-08 13:02:13 +02:00
|
|
|
e = NULL;
|
2015-08-08 13:33:46 +02:00
|
|
|
while ((e = av_dict_get(ost->sws_dict, "", e,
|
|
|
|
AV_DICT_IGNORE_SUFFIX))) {
|
|
|
|
av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
|
|
|
|
}
|
2021-08-24 09:24:55 +02:00
|
|
|
if (strlen(args)) {
|
2015-08-08 13:33:46 +02:00
|
|
|
args[strlen(args)-1] = 0;
|
2021-08-30 15:26:31 +02:00
|
|
|
fg->graph->scale_sws_opts = av_strdup(args);
|
2021-08-01 12:58:19 +02:00
|
|
|
}
|
2012-11-23 17:35:48 +03:00
|
|
|
|
|
|
|
args[0] = 0;
|
2021-08-08 13:02:13 +02:00
|
|
|
e = NULL;
|
2013-02-24 21:07:42 +03:00
|
|
|
while ((e = av_dict_get(ost->swr_opts, "", e,
|
|
|
|
AV_DICT_IGNORE_SUFFIX))) {
|
|
|
|
av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
|
|
|
|
}
|
2012-11-23 17:35:48 +03:00
|
|
|
if (strlen(args))
|
|
|
|
args[strlen(args)-1] = 0;
|
|
|
|
av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
|
2016-10-30 15:25:39 +02:00
|
|
|
} else {
|
2016-11-24 17:14:27 +02:00
|
|
|
fg->graph->nb_threads = filter_complex_nbthreads;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
|
2017-03-02 20:14:58 +02:00
|
|
|
goto fail;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2020-04-13 17:33:19 +02:00
|
|
|
ret = hw_device_setup_for_filter(fg);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2016-03-24 01:48:13 +02:00
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
|
2015-07-01 21:23:55 +02:00
|
|
|
const char *num_inputs;
|
|
|
|
const char *num_outputs;
|
|
|
|
if (!outputs) {
|
|
|
|
num_outputs = "0";
|
|
|
|
} else if (outputs->next) {
|
|
|
|
num_outputs = ">1";
|
|
|
|
} else {
|
|
|
|
num_outputs = "1";
|
|
|
|
}
|
|
|
|
if (!inputs) {
|
|
|
|
num_inputs = "0";
|
|
|
|
} else if (inputs->next) {
|
|
|
|
num_inputs = ">1";
|
|
|
|
} else {
|
|
|
|
num_inputs = "1";
|
|
|
|
}
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' was expected "
|
|
|
|
"to have exactly 1 input and 1 output."
|
|
|
|
" However, it had %s input(s) and %s output(s)."
|
|
|
|
" Please adjust, or use a complex filtergraph (-filter_complex) instead.\n",
|
|
|
|
graph_desc, num_inputs, num_outputs);
|
2017-03-02 20:14:58 +02:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (cur = inputs, i = 0; cur; cur = cur->next, i++)
|
2014-08-30 13:27:50 +03:00
|
|
|
if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0) {
|
|
|
|
avfilter_inout_free(&inputs);
|
|
|
|
avfilter_inout_free(&outputs);
|
2017-03-02 20:14:58 +02:00
|
|
|
goto fail;
|
2014-08-30 13:27:50 +03:00
|
|
|
}
|
2012-08-02 09:24:02 +03:00
|
|
|
avfilter_inout_free(&inputs);
|
|
|
|
|
2015-10-11 12:10:42 +02:00
|
|
|
for (cur = outputs, i = 0; cur; cur = cur->next, i++)
|
|
|
|
configure_output_filter(fg, fg->outputs[i], cur);
|
2015-07-15 19:49:24 +02:00
|
|
|
avfilter_inout_free(&outputs);
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2020-08-14 13:06:34 +02:00
|
|
|
if (!auto_conversion_filters)
|
|
|
|
avfilter_graph_set_auto_convert(fg->graph, AVFILTER_AUTO_CONVERT_NONE);
|
2015-07-15 19:49:24 +02:00
|
|
|
if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
|
2017-03-02 20:14:58 +02:00
|
|
|
goto fail;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2021-11-23 12:22:57 +02:00
|
|
|
fg->is_meta = graph_is_meta(fg->graph);
|
|
|
|
|
2016-05-23 14:09:08 +02:00
|
|
|
/* limit the lists of allowed formats to the ones selected, to
|
|
|
|
* make sure they stay the same if the filtergraph is reconfigured later */
|
|
|
|
for (i = 0; i < fg->nb_outputs; i++) {
|
|
|
|
OutputFilter *ofilter = fg->outputs[i];
|
2016-12-18 13:34:35 +02:00
|
|
|
AVFilterContext *sink = ofilter->filter;
|
2016-05-23 14:09:08 +02:00
|
|
|
|
2016-12-18 13:34:35 +02:00
|
|
|
ofilter->format = av_buffersink_get_format(sink);
|
2016-05-23 14:09:08 +02:00
|
|
|
|
2016-12-18 13:34:35 +02:00
|
|
|
ofilter->width = av_buffersink_get_w(sink);
|
|
|
|
ofilter->height = av_buffersink_get_h(sink);
|
2016-05-23 14:09:08 +02:00
|
|
|
|
2016-12-18 13:34:35 +02:00
|
|
|
ofilter->sample_rate = av_buffersink_get_sample_rate(sink);
|
2021-09-03 02:04:30 +02:00
|
|
|
av_channel_layout_uninit(&ofilter->ch_layout);
|
|
|
|
ret = av_buffersink_get_ch_layout(sink, &ofilter->ch_layout);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2016-05-23 14:09:08 +02:00
|
|
|
}
|
|
|
|
|
2013-02-25 05:00:58 +03:00
|
|
|
fg->reconfiguration = 1;
|
2014-09-11 05:55:58 +03:00
|
|
|
|
|
|
|
for (i = 0; i < fg->nb_outputs; i++) {
|
|
|
|
OutputStream *ost = fg->outputs[i]->ost;
|
2022-08-25 10:01:19 +02:00
|
|
|
if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_AUDIO &&
|
|
|
|
!(ost->enc_ctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
|
2014-09-11 05:55:58 +03:00
|
|
|
av_buffersink_set_frame_size(ost->filter->filter,
|
|
|
|
ost->enc_ctx->frame_size);
|
|
|
|
}
|
|
|
|
|
2016-06-27 18:59:23 +02:00
|
|
|
for (i = 0; i < fg->nb_inputs; i++) {
|
2022-01-10 12:04:36 +02:00
|
|
|
AVFrame *tmp;
|
|
|
|
while (av_fifo_read(fg->inputs[i]->frame_queue, &tmp, 1) >= 0) {
|
2016-06-27 18:59:23 +02:00
|
|
|
ret = av_buffersrc_add_frame(fg->inputs[i]->filter, tmp);
|
|
|
|
av_frame_free(&tmp);
|
|
|
|
if (ret < 0)
|
2017-03-02 20:14:58 +02:00
|
|
|
goto fail;
|
2016-06-27 19:03:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send the EOFs for the finished inputs */
|
|
|
|
for (i = 0; i < fg->nb_inputs; i++) {
|
|
|
|
if (fg->inputs[i]->eof) {
|
|
|
|
ret = av_buffersrc_add_frame(fg->inputs[i]->filter, NULL);
|
|
|
|
if (ret < 0)
|
2017-03-02 20:14:58 +02:00
|
|
|
goto fail;
|
2016-06-27 18:59:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 17:01:01 +02:00
|
|
|
/* process queued up subtitle packets */
|
|
|
|
for (i = 0; i < fg->nb_inputs; i++) {
|
|
|
|
InputStream *ist = fg->inputs[i]->ist;
|
|
|
|
if (ist->sub2video.sub_queue && ist->sub2video.frame) {
|
2022-01-10 12:04:36 +02:00
|
|
|
AVSubtitle tmp;
|
|
|
|
while (av_fifo_read(ist->sub2video.sub_queue, &tmp, 1) >= 0) {
|
2019-02-23 01:48:08 +02:00
|
|
|
sub2video_update(ist, INT64_MIN, &tmp);
|
2017-03-02 17:01:01 +02:00
|
|
|
avsubtitle_free(&tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
return 0;
|
2017-03-02 20:14:58 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
cleanup_filtergraph(fg);
|
|
|
|
return ret;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
2016-05-23 14:09:08 +02:00
|
|
|
int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)
|
|
|
|
{
|
2021-09-08 20:34:25 +02:00
|
|
|
AVFrameSideData *sd;
|
2021-09-03 02:04:30 +02:00
|
|
|
int ret;
|
2021-09-08 20:34:25 +02:00
|
|
|
|
2016-05-23 14:09:08 +02:00
|
|
|
av_buffer_unref(&ifilter->hw_frames_ctx);
|
|
|
|
|
|
|
|
ifilter->format = frame->format;
|
|
|
|
|
|
|
|
ifilter->width = frame->width;
|
|
|
|
ifilter->height = frame->height;
|
|
|
|
ifilter->sample_aspect_ratio = frame->sample_aspect_ratio;
|
|
|
|
|
|
|
|
ifilter->sample_rate = frame->sample_rate;
|
2021-09-03 02:04:30 +02:00
|
|
|
ret = av_channel_layout_copy(&ifilter->ch_layout, &frame->ch_layout);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-05-23 14:09:08 +02:00
|
|
|
|
2021-09-08 20:34:25 +02:00
|
|
|
av_freep(&ifilter->displaymatrix);
|
|
|
|
sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX);
|
|
|
|
if (sd)
|
|
|
|
ifilter->displaymatrix = av_memdup(sd->data, sizeof(int32_t) * 9);
|
|
|
|
|
2016-05-23 14:09:08 +02:00
|
|
|
if (frame->hw_frames_ctx) {
|
|
|
|
ifilter->hw_frames_ctx = av_buffer_ref(frame->hw_frames_ctx);
|
|
|
|
if (!ifilter->hw_frames_ctx)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-22 17:57:59 +02:00
|
|
|
int filtergraph_is_simple(FilterGraph *fg)
|
|
|
|
{
|
|
|
|
return !fg->graph_desc;
|
|
|
|
}
|