]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_aformat.c
lavfi: make formats API private on next bump.
[ffmpeg] / libavfilter / af_aformat.c
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
26 #include "libavutil/audioconvert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/opt.h"
29
30 #include "audio.h"
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "internal.h"
34
35 typedef struct AFormatContext {
36     const AVClass   *class;
37
38     AVFilterFormats *formats;
39     AVFilterFormats *sample_rates;
40     AVFilterChannelLayouts *channel_layouts;
41
42     char *formats_str;
43     char *sample_rates_str;
44     char *channel_layouts_str;
45 } AFormatContext;
46
47 #define OFFSET(x) offsetof(AFormatContext, x)
48 #define A AV_OPT_FLAG_AUDIO_PARAM
49 static const AVOption options[] = {
50     { "sample_fmts",     "A comma-separated list of sample formats.",  OFFSET(formats_str),         AV_OPT_TYPE_STRING, .flags = A },
51     { "sample_rates",    "A comma-separated list of sample rates.",    OFFSET(sample_rates_str),    AV_OPT_TYPE_STRING, .flags = A },
52     { "channel_layouts", "A comma-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A },
53     { NULL },
54 };
55
56 static const AVClass aformat_class = {
57     .class_name = "aformat filter",
58     .item_name  = av_default_item_name,
59     .option     = options,
60     .version    = LIBAVUTIL_VERSION_INT,
61 };
62
63 #define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc)    \
64 do {                                                                        \
65     char *next, *cur = str;                                                 \
66     while (cur) {                                                           \
67         type fmt;                                                           \
68         next = strchr(cur, ',');                                            \
69         if (next)                                                           \
70             *next++ = 0;                                                    \
71                                                                             \
72         if ((fmt = get_fmt(cur)) == none) {                                 \
73             av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
74             ret = AVERROR(EINVAL);                                          \
75             goto fail;                                                      \
76         }                                                                   \
77         add_to_list(&list, fmt);                                            \
78                                                                             \
79         cur = next;                                                         \
80     }                                                                       \
81 } while (0)
82
83 static int get_sample_rate(const char *samplerate)
84 {
85     int ret = strtol(samplerate, NULL, 0);
86     return FFMAX(ret, 0);
87 }
88
89 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
90 {
91     AFormatContext *s = ctx->priv;
92     int ret;
93
94     if (!args) {
95         av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n");
96         return AVERROR(EINVAL);
97     }
98
99     s->class = &aformat_class;
100     av_opt_set_defaults(s);
101
102     if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
103         av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
104         return ret;
105     }
106
107     PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
108                   ff_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
109     PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format,
110                   get_sample_rate, 0, "sample rate");
111     PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts,
112                   ff_add_channel_layout, av_get_channel_layout, 0,
113                   "channel layout");
114
115 fail:
116     av_opt_free(s);
117     return ret;
118 }
119
120 static int query_formats(AVFilterContext *ctx)
121 {
122     AFormatContext *s = ctx->priv;
123
124     ff_set_common_formats(ctx, s->formats ? s->formats :
125                                                   ff_all_formats(AVMEDIA_TYPE_AUDIO));
126     ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
127                                                      ff_all_samplerates());
128     ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
129                                                             ff_all_channel_layouts());
130
131     return 0;
132 }
133
134 AVFilter avfilter_af_aformat = {
135     .name          = "aformat",
136     .description   = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
137     .init          = init,
138     .query_formats = query_formats,
139     .priv_size     = sizeof(AFormatContext),
140
141     .inputs        = (AVFilterPad[]) {{ .name            = "default",
142                                         .type            = AVMEDIA_TYPE_AUDIO,
143                                         .filter_samples  = ff_null_filter_samples },
144                                       { .name = NULL}},
145     .outputs       = (AVFilterPad[]) {{ .name            = "default",
146                                         .type            = AVMEDIA_TYPE_AUDIO},
147                                       { .name = NULL}},
148 };