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 },
fb604ae8
     { NULL },
 };
 
c17808ce
 AVFILTER_DEFINE_CLASS(aformat);
fb604ae8
 
 #define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc)    \
 do {                                                                        \
     char *next, *cur = str;                                                 \
     while (cur) {                                                           \
         type fmt;                                                           \
         next = strchr(cur, ',');                                            \
         if (next)                                                           \
             *next++ = 0;                                                    \
                                                                             \
         if ((fmt = get_fmt(cur)) == none) {                                 \
             av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
             ret = AVERROR(EINVAL);                                          \
             goto fail;                                                      \
         }                                                                   \
         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);
 }
 
a5e8c41c
 static av_cold int init(AVFilterContext *ctx, const char *args)
0a229581
 {
fb604ae8
     AFormatContext *s = ctx->priv;
0a229581
     int ret;
 
fb604ae8
     if (!args) {
         av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n");
         return AVERROR(EINVAL);
     }
0a229581
 
fb604ae8
     s->class = &aformat_class;
     av_opt_set_defaults(s);
0a229581
 
cc650cf0
     if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
fb604ae8
         return ret;
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");
 
 fail:
     av_opt_free(s);
     return ret;
0a229581
 }
 
 static int query_formats(AVFilterContext *ctx)
 {
fb604ae8
     AFormatContext *s = ctx->priv;
0a229581
 
b74a1da4
     ff_set_common_formats(ctx, s->formats ? s->formats :
                                                   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 :
                                                             ff_all_channel_layouts());
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 }
 };
 
0a229581
 AVFilter avfilter_af_aformat = {
     .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),
 
568c70e7
     .inputs        = avfilter_af_aformat_inputs,
     .outputs       = avfilter_af_aformat_outputs,
526cb36e
     .priv_class    = &aformat_class,
0a229581
 };