0a229581 |
/*
* 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
*/
|
86ca51ac |
#include "libavutil/avstring.h" |
a903f8f0 |
#include "libavutil/channel_layout.h" |
1d9c2dc8 |
#include "libavutil/common.h" |
fb604ae8 |
#include "libavutil/opt.h"
|
4522df52 |
#include "audio.h" |
fb604ae8 |
#include "avfilter.h"
#include "formats.h" |
0a229581 |
#include "internal.h"
|
fb604ae8 |
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; |
0a229581 |
} AFormatContext;
|
fb604ae8 |
#define OFFSET(x) offsetof(AFormatContext, x)
#define A AV_OPT_FLAG_AUDIO_PARAM |
42d621d1 |
#define F AV_OPT_FLAG_FILTERING_PARAM |
c17808ce |
static const AVOption aformat_options[] = { |
6bdeac24 |
{ "sample_fmts", "A '|'-separated list of sample formats.", OFFSET(formats_str), AV_OPT_TYPE_STRING, .flags = A|F },
{ "sample_rates", "A '|'-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A|F },
{ "channel_layouts", "A '|'-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A|F }, |
b211607b |
{ NULL } |
fb604ae8 |
};
|
c17808ce |
AVFILTER_DEFINE_CLASS(aformat); |
fb604ae8 |
|
6aaac24d |
#define PARSE_FORMATS(str, type, list, add_to_list, unref_fn, get_fmt, none, desc) \ |
fb604ae8 |
do { \ |
0af7fe1f |
char *next, *cur = str, sep; \ |
6aaac24d |
int ret; \ |
0af7fe1f |
\
if (str && strchr(str, ',')) { \
av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "\
"separate %s.\n", desc); \
sep = ','; \
} else \
sep = '|'; \
\ |
fb604ae8 |
while (cur) { \
type fmt; \ |
0af7fe1f |
next = strchr(cur, sep); \ |
fb604ae8 |
if (next) \
*next++ = 0; \
\
if ((fmt = get_fmt(cur)) == none) { \
av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\ |
0af7fe1f |
return AVERROR(EINVAL); \ |
fb604ae8 |
} \ |
6aaac24d |
if ((ret = add_to_list(&list, fmt)) < 0) { \
unref_fn(&list); \
return ret; \
} \ |
fb604ae8 |
\
cur = next; \
} \
} while (0)
static int get_sample_rate(const char *samplerate)
{
int ret = strtol(samplerate, NULL, 0);
return FFMAX(ret, 0);
}
|
d69a4177 |
static av_cold int init(AVFilterContext *ctx) |
0a229581 |
{ |
fb604ae8 |
AFormatContext *s = ctx->priv; |
0a229581 |
|
fb604ae8 |
PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats, |
6aaac24d |
ff_add_format, ff_formats_unref, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format, ff_formats_unref, |
fb604ae8 |
get_sample_rate, 0, "sample rate");
PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts, |
6aaac24d |
ff_add_channel_layout, ff_channel_layouts_unref, av_get_channel_layout, 0, |
fb604ae8 |
"channel layout");
|
0af7fe1f |
return 0; |
0a229581 |
}
static int query_formats(AVFilterContext *ctx)
{ |
fb604ae8 |
AFormatContext *s = ctx->priv; |
a0854c08 |
int ret; |
0a229581 |
|
a0854c08 |
ret = ff_set_common_formats(ctx, s->formats ? s->formats : |
c5ab6088 |
ff_all_formats(AVMEDIA_TYPE_AUDIO)); |
a0854c08 |
if (ret < 0)
return ret;
ret = ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates : |
fb604ae8 |
ff_all_samplerates()); |
a0854c08 |
if (ret < 0)
return ret;
return ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts : |
6d962aec |
ff_all_channel_counts()); |
0a229581 |
}
|
568c70e7 |
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 }
};
|
cd43ca04 |
AVFilter ff_af_aformat = { |
0a229581 |
.name = "aformat",
.description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
.init = init,
.query_formats = query_formats,
.priv_size = sizeof(AFormatContext), |
0af7fe1f |
.priv_class = &aformat_class, |
568c70e7 |
.inputs = avfilter_af_aformat_inputs,
.outputs = avfilter_af_aformat_outputs, |
0a229581 |
}; |