2011-07-14 11:18:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Mina Nagy Zaki
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* format audio filter
|
|
|
|
*/
|
|
|
|
|
2011-08-16 00:02:44 +00:00
|
|
|
#include "libavutil/avstring.h"
|
2012-11-10 15:00:00 +00:00
|
|
|
#include "libavutil/channel_layout.h"
|
2012-08-06 13:49:32 +00:00
|
|
|
#include "libavutil/common.h"
|
2012-05-06 07:00:53 +00:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
2012-05-12 15:58:14 +00:00
|
|
|
#include "audio.h"
|
2012-05-06 07:00:53 +00:00
|
|
|
#include "avfilter.h"
|
|
|
|
#include "formats.h"
|
2011-07-14 11:18:13 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
2012-05-06 07:00:53 +00:00
|
|
|
typedef struct AFormatContext {
|
|
|
|
const AVClass *class;
|
|
|
|
|
|
|
|
AVFilterFormats *formats;
|
|
|
|
AVFilterFormats *sample_rates;
|
|
|
|
AVFilterChannelLayouts *channel_layouts;
|
|
|
|
|
|
|
|
char *formats_str;
|
|
|
|
char *sample_rates_str;
|
|
|
|
char *channel_layouts_str;
|
2011-07-14 11:18:13 +00:00
|
|
|
} AFormatContext;
|
|
|
|
|
2012-05-06 07:00:53 +00:00
|
|
|
#define OFFSET(x) offsetof(AFormatContext, x)
|
|
|
|
#define A AV_OPT_FLAG_AUDIO_PARAM
|
2012-08-13 11:40:01 +00:00
|
|
|
#define F AV_OPT_FLAG_FILTERING_PARAM
|
2012-06-22 12:33:09 +00:00
|
|
|
static const AVOption aformat_options[] = {
|
2016-02-07 21:47:39 +00:00
|
|
|
{ "sample_fmts", "A '|'-separated list of sample formats.", OFFSET(formats_str), AV_OPT_TYPE_STRING, .flags = A|F },
|
2020-01-07 10:20:09 +00:00
|
|
|
{ "f", "A '|'-separated list of sample formats.", OFFSET(formats_str), AV_OPT_TYPE_STRING, .flags = A|F },
|
2016-02-07 21:47:39 +00:00
|
|
|
{ "sample_rates", "A '|'-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A|F },
|
2020-01-07 10:20:09 +00:00
|
|
|
{ "r", "A '|'-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A|F },
|
2016-02-07 21:47:39 +00:00
|
|
|
{ "channel_layouts", "A '|'-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A|F },
|
2020-01-07 10:20:09 +00:00
|
|
|
{ "cl", "A '|'-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A|F },
|
2013-09-07 12:13:50 +00:00
|
|
|
{ NULL }
|
2012-05-06 07:00:53 +00:00
|
|
|
};
|
|
|
|
|
2012-06-22 12:33:09 +00:00
|
|
|
AVFILTER_DEFINE_CLASS(aformat);
|
2012-05-06 07:00:53 +00:00
|
|
|
|
2020-08-07 21:46:33 +00:00
|
|
|
#define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc) \
|
2012-05-06 07:00:53 +00:00
|
|
|
do { \
|
2013-02-25 20:21:29 +00:00
|
|
|
char *next, *cur = str, sep; \
|
2015-10-05 03:39:25 +00:00
|
|
|
int ret; \
|
2013-02-25 20:21:29 +00:00
|
|
|
\
|
|
|
|
if (str && strchr(str, ',')) { \
|
|
|
|
av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "\
|
|
|
|
"separate %s.\n", desc); \
|
|
|
|
sep = ','; \
|
|
|
|
} else \
|
|
|
|
sep = '|'; \
|
|
|
|
\
|
2012-05-06 07:00:53 +00:00
|
|
|
while (cur) { \
|
|
|
|
type fmt; \
|
2013-02-25 20:21:29 +00:00
|
|
|
next = strchr(cur, sep); \
|
2012-05-06 07:00:53 +00:00
|
|
|
if (next) \
|
|
|
|
*next++ = 0; \
|
|
|
|
\
|
|
|
|
if ((fmt = get_fmt(cur)) == none) { \
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
|
2013-02-25 20:21:29 +00:00
|
|
|
return AVERROR(EINVAL); \
|
2012-05-06 07:00:53 +00:00
|
|
|
} \
|
2015-10-05 03:39:25 +00:00
|
|
|
if ((ret = add_to_list(&list, fmt)) < 0) { \
|
|
|
|
return ret; \
|
|
|
|
} \
|
2012-05-06 07:00:53 +00:00
|
|
|
\
|
|
|
|
cur = next; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static int get_sample_rate(const char *samplerate)
|
|
|
|
{
|
|
|
|
int ret = strtol(samplerate, NULL, 0);
|
|
|
|
return FFMAX(ret, 0);
|
|
|
|
}
|
|
|
|
|
2013-03-13 07:26:39 +00:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2011-07-14 11:18:13 +00:00
|
|
|
{
|
2012-05-06 07:00:53 +00:00
|
|
|
AFormatContext *s = ctx->priv;
|
2011-07-14 11:18:13 +00:00
|
|
|
|
2012-05-06 07:00:53 +00:00
|
|
|
PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
|
2020-08-07 21:46:33 +00:00
|
|
|
ff_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
|
|
|
|
PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format,
|
2012-05-06 07:00:53 +00:00
|
|
|
get_sample_rate, 0, "sample rate");
|
|
|
|
PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts,
|
2020-08-07 21:46:33 +00:00
|
|
|
ff_add_channel_layout, av_get_channel_layout, 0,
|
2012-05-06 07:00:53 +00:00
|
|
|
"channel layout");
|
|
|
|
|
2013-02-25 20:21:29 +00:00
|
|
|
return 0;
|
2011-07-14 11:18:13 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 14:42:37 +00:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AFormatContext *s = ctx->priv;
|
|
|
|
|
|
|
|
ff_formats_unref(&s->formats);
|
|
|
|
ff_formats_unref(&s->sample_rates);
|
|
|
|
ff_channel_layouts_unref(&s->channel_layouts);
|
|
|
|
}
|
|
|
|
|
2011-07-14 11:18:13 +00:00
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2012-05-06 07:00:53 +00:00
|
|
|
AFormatContext *s = ctx->priv;
|
2015-04-03 17:55:18 +00:00
|
|
|
int ret;
|
2011-07-14 11:18:13 +00:00
|
|
|
|
2015-04-03 17:55:18 +00:00
|
|
|
ret = ff_set_common_formats(ctx, s->formats ? s->formats :
|
2013-03-31 14:52:26 +00:00
|
|
|
ff_all_formats(AVMEDIA_TYPE_AUDIO));
|
2020-08-09 14:42:37 +00:00
|
|
|
s->formats = NULL;
|
2015-04-03 17:55:18 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
ret = ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
|
2012-05-06 07:00:53 +00:00
|
|
|
ff_all_samplerates());
|
2020-08-09 14:42:37 +00:00
|
|
|
s->sample_rates = NULL;
|
2015-04-03 17:55:18 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2020-08-09 14:42:37 +00:00
|
|
|
ret = ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
|
2012-12-26 15:51:26 +00:00
|
|
|
ff_all_channel_counts());
|
2020-08-09 14:42:37 +00:00
|
|
|
s->channel_layouts = NULL;
|
|
|
|
return ret;
|
2011-07-14 11:18:13 +00:00
|
|
|
}
|
|
|
|
|
2012-07-24 13:14:01 +00:00
|
|
|
static const AVFilterPad avfilter_af_aformat_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_af_aformat_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2021-04-19 16:33:56 +00:00
|
|
|
const AVFilter ff_af_aformat = {
|
2011-07-14 11:18:13 +00:00
|
|
|
.name = "aformat",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
|
|
|
|
.init = init,
|
2020-08-09 14:42:37 +00:00
|
|
|
.uninit = uninit,
|
2011-07-14 11:18:13 +00:00
|
|
|
.query_formats = query_formats,
|
|
|
|
.priv_size = sizeof(AFormatContext),
|
2013-02-25 20:21:29 +00:00
|
|
|
.priv_class = &aformat_class,
|
2012-07-24 13:14:01 +00:00
|
|
|
.inputs = avfilter_af_aformat_inputs,
|
|
|
|
.outputs = avfilter_af_aformat_outputs,
|
2011-07-14 11:18:13 +00:00
|
|
|
};
|