h264: on reference overflow, reset the reference count to 0, not 1.
[FFMpeg-mirror/mplayer-patches.git] / libavfilter / af_aformat.c
blob2059cf2b49570f80480bfa04b183d92e429c887e
1 /*
2 * Copyright (c) 2011 Mina Nagy Zaki
4 * This file is part of Libav.
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.
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.
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
21 /**
22 * @file
23 * format audio filter
26 #include "libavutil/avstring.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/common.h"
29 #include "libavutil/opt.h"
31 #include "audio.h"
32 #include "avfilter.h"
33 #include "formats.h"
34 #include "internal.h"
36 typedef struct AFormatContext {
37 const AVClass *class;
39 AVFilterFormats *formats;
40 AVFilterFormats *sample_rates;
41 AVFilterChannelLayouts *channel_layouts;
43 char *formats_str;
44 char *sample_rates_str;
45 char *channel_layouts_str;
46 } AFormatContext;
48 #define OFFSET(x) offsetof(AFormatContext, x)
49 #define A AV_OPT_FLAG_AUDIO_PARAM
50 static 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 },
57 static const AVClass aformat_class = {
58 .class_name = "aformat filter",
59 .item_name = av_default_item_name,
60 .option = options,
61 .version = LIBAVUTIL_VERSION_INT,
64 #define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc) \
65 do { \
66 char *next, *cur = str; \
67 while (cur) { \
68 type fmt; \
69 next = strchr(cur, ','); \
70 if (next) \
71 *next++ = 0; \
73 if ((fmt = get_fmt(cur)) == none) { \
74 av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
75 ret = AVERROR(EINVAL); \
76 goto fail; \
77 } \
78 add_to_list(&list, fmt); \
80 cur = next; \
81 } \
82 } while (0)
84 static int get_sample_rate(const char *samplerate)
86 int ret = strtol(samplerate, NULL, 0);
87 return FFMAX(ret, 0);
90 static av_cold int init(AVFilterContext *ctx, const char *args)
92 AFormatContext *s = ctx->priv;
93 int ret;
95 if (!args) {
96 av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n");
97 return AVERROR(EINVAL);
100 s->class = &aformat_class;
101 av_opt_set_defaults(s);
103 if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
104 av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
105 return ret;
108 PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
109 ff_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
110 PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format,
111 get_sample_rate, 0, "sample rate");
112 PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts,
113 ff_add_channel_layout, av_get_channel_layout, 0,
114 "channel layout");
116 fail:
117 av_opt_free(s);
118 return ret;
121 static int query_formats(AVFilterContext *ctx)
123 AFormatContext *s = ctx->priv;
125 ff_set_common_formats(ctx, s->formats ? s->formats :
126 ff_all_formats(AVMEDIA_TYPE_AUDIO));
127 ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
128 ff_all_samplerates());
129 ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
130 ff_all_channel_layouts());
132 return 0;
135 static const AVFilterPad avfilter_af_aformat_inputs[] = {
137 .name = "default",
138 .type = AVMEDIA_TYPE_AUDIO,
140 { NULL }
143 static const AVFilterPad avfilter_af_aformat_outputs[] = {
145 .name = "default",
146 .type = AVMEDIA_TYPE_AUDIO
148 { NULL }
151 AVFilter avfilter_af_aformat = {
152 .name = "aformat",
153 .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
154 .init = init,
155 .query_formats = query_formats,
156 .priv_size = sizeof(AFormatContext),
158 .inputs = avfilter_af_aformat_inputs,
159 .outputs = avfilter_af_aformat_outputs,