]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_aformat.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / af_aformat.c
1 /*
2  * Copyright (c) 2011 Mina Nagy Zaki
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
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 aformat_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 AVFILTER_DEFINE_CLASS(aformat);
57
58 #define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc)    \
59 do {                                                                        \
60     char *next, *cur = str;                                                 \
61     while (cur) {                                                           \
62         type fmt;                                                           \
63         next = strchr(cur, ',');                                            \
64         if (next)                                                           \
65             *next++ = 0;                                                    \
66                                                                             \
67         if ((fmt = get_fmt(cur)) == none) {                                 \
68             av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
69             ret = AVERROR(EINVAL);                                          \
70             goto fail;                                                      \
71         }                                                                   \
72         add_to_list(&list, fmt);                                            \
73                                                                             \
74         cur = next;                                                         \
75     }                                                                       \
76 } while (0)
77
78 static int get_sample_rate(const char *samplerate)
79 {
80     int ret = strtol(samplerate, NULL, 0);
81     return FFMAX(ret, 0);
82 }
83
84 static av_cold int init(AVFilterContext *ctx, const char *args)
85 {
86     AFormatContext *s = ctx->priv;
87     int ret;
88
89     if (!args) {
90         av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n");
91         return AVERROR(EINVAL);
92     }
93
94     s->class = &aformat_class;
95     av_opt_set_defaults(s);
96
97     if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
98         av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
99         return ret;
100     }
101
102     PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
103                   ff_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
104     PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format,
105                   get_sample_rate, 0, "sample rate");
106     PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts,
107                   ff_add_channel_layout, av_get_channel_layout, 0,
108                   "channel layout");
109
110 fail:
111     av_opt_free(s);
112     return ret;
113 }
114
115 static int query_formats(AVFilterContext *ctx)
116 {
117     AFormatContext *s = ctx->priv;
118
119     ff_set_common_formats(ctx, s->formats ? s->formats :
120                                                   ff_all_formats(AVMEDIA_TYPE_AUDIO));
121     ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
122                                                      ff_all_samplerates());
123     ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
124                                                             ff_all_channel_layouts());
125
126     return 0;
127 }
128
129 AVFilter avfilter_af_aformat = {
130     .name          = "aformat",
131     .description   = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
132     .init          = init,
133     .query_formats = query_formats,
134     .priv_size     = sizeof(AFormatContext),
135
136     .inputs        = (AVFilterPad[]) {{ .name            = "default",
137                                         .type            = AVMEDIA_TYPE_AUDIO, },
138                                       { .name = NULL}},
139     .outputs       = (AVFilterPad[]) {{ .name            = "default",
140                                         .type            = AVMEDIA_TYPE_AUDIO},
141                                       { .name = NULL}},
142 };