2 * Copyright (c) 2011 Mina Nagy Zaki
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/avstring.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/common.h"
29 #include "libavutil/opt.h"
36 typedef struct AFormatContext
{
39 AVFilterFormats
*formats
;
40 AVFilterFormats
*sample_rates
;
41 AVFilterChannelLayouts
*channel_layouts
;
44 char *sample_rates_str
;
45 char *channel_layouts_str
;
48 #define OFFSET(x) offsetof(AFormatContext, x)
49 #define A AV_OPT_FLAG_AUDIO_PARAM
50 #define F AV_OPT_FLAG_FILTERING_PARAM
51 static const AVOption aformat_options
[] = {
52 { "sample_fmts", "A comma-separated list of sample formats.", OFFSET(formats_str
), AV_OPT_TYPE_STRING
, .flags
= A
|F
},
53 { "sample_rates", "A comma-separated list of sample rates.", OFFSET(sample_rates_str
), AV_OPT_TYPE_STRING
, .flags
= A
|F
},
54 { "channel_layouts", "A comma-separated list of channel layouts.", OFFSET(channel_layouts_str
), AV_OPT_TYPE_STRING
, .flags
= A
|F
},
58 AVFILTER_DEFINE_CLASS(aformat
);
60 #define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc) \
62 char *next, *cur = str, sep; \
64 if (str && strchr(str, ',')) { \
65 av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "\
66 "separate %s.\n", desc); \
73 next = strchr(cur, sep); \
77 if ((fmt = get_fmt(cur)) == none) { \
78 av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
79 return AVERROR(EINVAL); \
81 add_to_list(&list, fmt); \
87 static int get_sample_rate(const char *samplerate
)
89 int ret
= strtol(samplerate
, NULL
, 0);
93 static av_cold
int init(AVFilterContext
*ctx
)
95 AFormatContext
*s
= ctx
->priv
;
97 PARSE_FORMATS(s
->formats_str
, enum AVSampleFormat
, s
->formats
,
98 ff_add_format
, av_get_sample_fmt
, AV_SAMPLE_FMT_NONE
, "sample format");
99 PARSE_FORMATS(s
->sample_rates_str
, int, s
->sample_rates
, ff_add_format
,
100 get_sample_rate
, 0, "sample rate");
101 PARSE_FORMATS(s
->channel_layouts_str
, uint64_t, s
->channel_layouts
,
102 ff_add_channel_layout
, av_get_channel_layout
, 0,
108 static int query_formats(AVFilterContext
*ctx
)
110 AFormatContext
*s
= ctx
->priv
;
112 ff_set_common_formats(ctx
, s
->formats
? s
->formats
:
113 ff_all_formats(AVMEDIA_TYPE_AUDIO
));
114 ff_set_common_samplerates(ctx
, s
->sample_rates
? s
->sample_rates
:
115 ff_all_samplerates());
116 ff_set_common_channel_layouts(ctx
, s
->channel_layouts
? s
->channel_layouts
:
117 ff_all_channel_counts());
122 static const AVFilterPad avfilter_af_aformat_inputs
[] = {
125 .type
= AVMEDIA_TYPE_AUDIO
,
130 static const AVFilterPad avfilter_af_aformat_outputs
[] = {
133 .type
= AVMEDIA_TYPE_AUDIO
138 AVFilter ff_af_aformat
= {
140 .description
= NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
142 .query_formats
= query_formats
,
143 .priv_size
= sizeof(AFormatContext
),
144 .priv_class
= &aformat_class
,
145 .inputs
= avfilter_af_aformat_inputs
,
146 .outputs
= avfilter_af_aformat_outputs
,