h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / af_aformat.c
CommitLineData
fb604ae8
AK
1/*
2 * Copyright (c) 2011 Mina Nagy Zaki
3 *
4 * This file is part of Libav.
5 *
6 * Libav 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.
10 *
11 * Libav 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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file
23 * format audio filter
24 */
25
fb604ae8 26#include "libavutil/avstring.h"
a903f8f0 27#include "libavutil/channel_layout.h"
1d9c2dc8 28#include "libavutil/common.h"
fb604ae8
AK
29#include "libavutil/opt.h"
30
31#include "audio.h"
32#include "avfilter.h"
33#include "formats.h"
34#include "internal.h"
35
36typedef struct AFormatContext {
37 const AVClass *class;
38
39 AVFilterFormats *formats;
40 AVFilterFormats *sample_rates;
41 AVFilterChannelLayouts *channel_layouts;
42
43 char *formats_str;
44 char *sample_rates_str;
45 char *channel_layouts_str;
46} AFormatContext;
47
48#define OFFSET(x) offsetof(AFormatContext, x)
49#define A AV_OPT_FLAG_AUDIO_PARAM
50static const AVOption options[] = {
51 { "sample_fmts", "A comma-separated list of sample formats.", OFFSET(formats_str), AV_OPT_TYPE_STRING, .flags = A },
52 { "sample_rates", "A comma-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A },
53 { "channel_layouts", "A comma-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A },
54 { NULL },
55};
56
57static const AVClass aformat_class = {
58 .class_name = "aformat filter",
59 .item_name = av_default_item_name,
60 .option = options,
61 .version = LIBAVUTIL_VERSION_INT,
62};
63
64#define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc) \
65do { \
0af7fe1f
AK
66 char *next, *cur = str, sep; \
67 \
68 if (str && strchr(str, ',')) { \
69 av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "\
70 "separate %s.\n", desc); \
71 sep = ','; \
72 } else \
73 sep = '|'; \
74 \
fb604ae8
AK
75 while (cur) { \
76 type fmt; \
0af7fe1f 77 next = strchr(cur, sep); \
fb604ae8
AK
78 if (next) \
79 *next++ = 0; \
80 \
81 if ((fmt = get_fmt(cur)) == none) { \
82 av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
0af7fe1f 83 return AVERROR(EINVAL); \
fb604ae8
AK
84 } \
85 add_to_list(&list, fmt); \
86 \
87 cur = next; \
88 } \
89} while (0)
90
91static int get_sample_rate(const char *samplerate)
92{
93 int ret = strtol(samplerate, NULL, 0);
94 return FFMAX(ret, 0);
95}
96
c43a7eca 97static av_cold int init(AVFilterContext *ctx)
fb604ae8
AK
98{
99 AFormatContext *s = ctx->priv;
fb604ae8
AK
100
101 PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
b74a1da4
AK
102 ff_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
103 PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format,
fb604ae8
AK
104 get_sample_rate, 0, "sample rate");
105 PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts,
106 ff_add_channel_layout, av_get_channel_layout, 0,
107 "channel layout");
108
0af7fe1f 109 return 0;
fb604ae8
AK
110}
111
112static int query_formats(AVFilterContext *ctx)
113{
114 AFormatContext *s = ctx->priv;
115
b74a1da4 116 ff_set_common_formats(ctx, s->formats ? s->formats :
c5ab6088 117 ff_all_formats(AVMEDIA_TYPE_AUDIO));
fb604ae8
AK
118 ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
119 ff_all_samplerates());
120 ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
121 ff_all_channel_layouts());
122
123 return 0;
124}
125
568c70e7
MR
126static const AVFilterPad avfilter_af_aformat_inputs[] = {
127 {
128 .name = "default",
129 .type = AVMEDIA_TYPE_AUDIO,
130 },
131 { NULL }
132};
133
134static const AVFilterPad avfilter_af_aformat_outputs[] = {
135 {
136 .name = "default",
137 .type = AVMEDIA_TYPE_AUDIO
138 },
139 { NULL }
140};
141
cd43ca04 142AVFilter ff_af_aformat = {
fb604ae8
AK
143 .name = "aformat",
144 .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
145 .init = init,
146 .query_formats = query_formats,
147 .priv_size = sizeof(AFormatContext),
0af7fe1f 148 .priv_class = &aformat_class,
fb604ae8 149
568c70e7
MR
150 .inputs = avfilter_af_aformat_inputs,
151 .outputs = avfilter_af_aformat_outputs,
fb604ae8 152};