]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_aformat.c
avutil/opt: check return value of av_bprint_finalize()
[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/avstring.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/common.h"
29 #include "libavutil/opt.h"
30
31 #include "audio.h"
32 #include "avfilter.h"
33 #include "formats.h"
34 #include "internal.h"
35
36 typedef 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
50 #define F AV_OPT_FLAG_FILTERING_PARAM
51 static const AVOption aformat_options[] = {
52     { "sample_fmts",     "A '|'-separated list of sample formats.",  OFFSET(formats_str),         AV_OPT_TYPE_STRING, .flags = A|F },
53     { "f",               "A '|'-separated list of sample formats.",  OFFSET(formats_str),         AV_OPT_TYPE_STRING, .flags = A|F },
54     { "sample_rates",    "A '|'-separated list of sample rates.",    OFFSET(sample_rates_str),    AV_OPT_TYPE_STRING, .flags = A|F },
55     { "r",               "A '|'-separated list of sample rates.",    OFFSET(sample_rates_str),    AV_OPT_TYPE_STRING, .flags = A|F },
56     { "channel_layouts", "A '|'-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A|F },
57     { "cl",              "A '|'-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A|F },
58     { NULL }
59 };
60
61 AVFILTER_DEFINE_CLASS(aformat);
62
63 #define PARSE_FORMATS(str, type, list, add_to_list, unref_fn, get_fmt, none, desc)    \
64 do {                                                                        \
65     char *next, *cur = str, sep;                                            \
66     int ret;                                                                \
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                                                                             \
75     while (cur) {                                                           \
76         type fmt;                                                           \
77         next = strchr(cur, sep);                                            \
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);\
83             return AVERROR(EINVAL);                                         \
84         }                                                                   \
85         if ((ret = add_to_list(&list, fmt)) < 0) {                          \
86             unref_fn(&list);                                                \
87             return ret;                                                     \
88         }                                                                   \
89                                                                             \
90         cur = next;                                                         \
91     }                                                                       \
92 } while (0)
93
94 static int get_sample_rate(const char *samplerate)
95 {
96     int ret = strtol(samplerate, NULL, 0);
97     return FFMAX(ret, 0);
98 }
99
100 static av_cold int init(AVFilterContext *ctx)
101 {
102     AFormatContext *s = ctx->priv;
103
104     PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
105                   ff_add_format, ff_formats_unref, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
106     PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format, ff_formats_unref,
107                   get_sample_rate, 0, "sample rate");
108     PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts,
109                   ff_add_channel_layout, ff_channel_layouts_unref, av_get_channel_layout, 0,
110                   "channel layout");
111
112     return 0;
113 }
114
115 static int query_formats(AVFilterContext *ctx)
116 {
117     AFormatContext *s = ctx->priv;
118     int ret;
119
120     ret = ff_set_common_formats(ctx, s->formats ? s->formats :
121                                             ff_all_formats(AVMEDIA_TYPE_AUDIO));
122     if (ret < 0)
123         return ret;
124     ret = ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
125                                                      ff_all_samplerates());
126     if (ret < 0)
127         return ret;
128     return ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
129                                                             ff_all_channel_counts());
130 }
131
132 static const AVFilterPad avfilter_af_aformat_inputs[] = {
133     {
134         .name = "default",
135         .type = AVMEDIA_TYPE_AUDIO,
136     },
137     { NULL }
138 };
139
140 static const AVFilterPad avfilter_af_aformat_outputs[] = {
141     {
142         .name = "default",
143         .type = AVMEDIA_TYPE_AUDIO
144     },
145     { NULL }
146 };
147
148 AVFilter ff_af_aformat = {
149     .name          = "aformat",
150     .description   = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
151     .init          = init,
152     .query_formats = query_formats,
153     .priv_size     = sizeof(AFormatContext),
154     .priv_class    = &aformat_class,
155     .inputs        = avfilter_af_aformat_inputs,
156     .outputs       = avfilter_af_aformat_outputs,
157 };