libavfilter/af_aformat.c
0a229581
 /*
  * Copyright (c) 2011 Mina Nagy Zaki
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
  * @file
  * format audio filter
  */
 
86ca51ac
 #include "libavutil/avstring.h"
a903f8f0
 #include "libavutil/channel_layout.h"
1d9c2dc8
 #include "libavutil/common.h"
fb604ae8
 #include "libavutil/opt.h"
 
4522df52
 #include "audio.h"
fb604ae8
 #include "avfilter.h"
 #include "formats.h"
0a229581
 #include "internal.h"
 
fb604ae8
 typedef struct AFormatContext {
     const AVClass   *class;
 
     AVFilterFormats *formats;
     AVFilterFormats *sample_rates;
     AVFilterChannelLayouts *channel_layouts;
 
     char *formats_str;
     char *sample_rates_str;
     char *channel_layouts_str;
0a229581
 } AFormatContext;
 
fb604ae8
 #define OFFSET(x) offsetof(AFormatContext, x)
 #define A AV_OPT_FLAG_AUDIO_PARAM
42d621d1
 #define F AV_OPT_FLAG_FILTERING_PARAM
c17808ce
 static const AVOption aformat_options[] = {
42d621d1
     { "sample_fmts",     "A comma-separated list of sample formats.",  OFFSET(formats_str),         AV_OPT_TYPE_STRING, .flags = A|F },
     { "sample_rates",    "A comma-separated list of sample rates.",    OFFSET(sample_rates_str),    AV_OPT_TYPE_STRING, .flags = A|F },
     { "channel_layouts", "A comma-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A|F },
b211607b
     { NULL }
fb604ae8
 };
 
c17808ce
 AVFILTER_DEFINE_CLASS(aformat);
fb604ae8
 
 #define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc)    \
 do {                                                                        \
0af7fe1f
     char *next, *cur = str, sep;                                            \
                                                                             \
     if (str && strchr(str, ',')) {                                          \
         av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "\
                "separate %s.\n", desc);                                     \
         sep = ',';                                                          \
     } else                                                                  \
         sep = '|';                                                          \
                                                                             \
fb604ae8
     while (cur) {                                                           \
         type fmt;                                                           \
0af7fe1f
         next = strchr(cur, sep);                                            \
fb604ae8
         if (next)                                                           \
             *next++ = 0;                                                    \
                                                                             \
         if ((fmt = get_fmt(cur)) == none) {                                 \
             av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
0af7fe1f
             return AVERROR(EINVAL);                                         \
fb604ae8
         }                                                                   \
         add_to_list(&list, fmt);                                            \
                                                                             \
         cur = next;                                                         \
     }                                                                       \
 } while (0)
 
 static int get_sample_rate(const char *samplerate)
 {
     int ret = strtol(samplerate, NULL, 0);
     return FFMAX(ret, 0);
 }
 
d69a4177
 static av_cold int init(AVFilterContext *ctx)
0a229581
 {
fb604ae8
     AFormatContext *s = ctx->priv;
0a229581
 
fb604ae8
     PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
b74a1da4
                   ff_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
     PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format,
fb604ae8
                   get_sample_rate, 0, "sample rate");
     PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts,
                   ff_add_channel_layout, av_get_channel_layout, 0,
                   "channel layout");
 
0af7fe1f
     return 0;
0a229581
 }
 
 static int query_formats(AVFilterContext *ctx)
 {
fb604ae8
     AFormatContext *s = ctx->priv;
0a229581
 
b74a1da4
     ff_set_common_formats(ctx, s->formats ? s->formats :
c5ab6088
                                             ff_all_formats(AVMEDIA_TYPE_AUDIO));
fb604ae8
     ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
                                                      ff_all_samplerates());
     ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
6d962aec
                                                             ff_all_channel_counts());
0a229581
 
fb604ae8
     return 0;
0a229581
 }
 
568c70e7
 static const AVFilterPad avfilter_af_aformat_inputs[] = {
     {
         .name = "default",
         .type = AVMEDIA_TYPE_AUDIO,
     },
     { NULL }
 };
 
 static const AVFilterPad avfilter_af_aformat_outputs[] = {
     {
         .name = "default",
         .type = AVMEDIA_TYPE_AUDIO
     },
     { NULL }
 };
 
cd43ca04
 AVFilter ff_af_aformat = {
0a229581
     .name          = "aformat",
     .description   = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
     .init          = init,
     .query_formats = query_formats,
     .priv_size     = sizeof(AFormatContext),
0af7fe1f
     .priv_class    = &aformat_class,
568c70e7
     .inputs        = avfilter_af_aformat_inputs,
     .outputs       = avfilter_af_aformat_outputs,
0a229581
 };