FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
af_aformat.c
Go to the documentation of this file.
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"
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 
42 
43  char *formats_str;
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 comma-separated list of sample formats.", OFFSET(formats_str), AV_OPT_TYPE_STRING, .flags = A|F },
53  { "sample_rates", "A comma-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A|F },
54  { "channel_layouts", "A comma-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A|F },
55  { NULL },
56 };
57 
58 AVFILTER_DEFINE_CLASS(aformat);
59 
60 #define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc) \
61 do { \
62  char *next, *cur = str; \
63  while (cur) { \
64  type fmt; \
65  next = strchr(cur, ','); \
66  if (next) \
67  *next++ = 0; \
68  \
69  if ((fmt = get_fmt(cur)) == none) { \
70  av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
71  ret = AVERROR(EINVAL); \
72  goto fail; \
73  } \
74  add_to_list(&list, fmt); \
75  \
76  cur = next; \
77  } \
78 } while (0)
79 
80 static int get_sample_rate(const char *samplerate)
81 {
82  int ret = strtol(samplerate, NULL, 0);
83  return FFMAX(ret, 0);
84 }
85 
86 static av_cold int init(AVFilterContext *ctx, const char *args)
87 {
88  AFormatContext *s = ctx->priv;
89  int ret;
90 
91  if (!args) {
92  av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n");
93  return AVERROR(EINVAL);
94  }
95 
96  s->class = &aformat_class;
98 
99  if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
100  return ret;
101 
105  get_sample_rate, 0, "sample rate");
108  "channel layout");
109 
110 fail:
111  av_opt_free(s);
112  return ret;
113 }
114 
116 {
117  AFormatContext *s = ctx->priv;
118 
119  ff_set_common_formats(ctx, s->formats ? s->formats :
125 
126  return 0;
127 }
128 
130  {
131  .name = "default",
132  .type = AVMEDIA_TYPE_AUDIO,
133  },
134  { NULL }
135 };
136 
138  {
139  .name = "default",
140  .type = AVMEDIA_TYPE_AUDIO
141  },
142  { NULL }
143 };
144 
146  .name = "aformat",
147  .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
148  .init = init,
149  .query_formats = query_formats,
150  .priv_size = sizeof(AFormatContext),
151 
152  .inputs = avfilter_af_aformat_inputs,
153  .outputs = avfilter_af_aformat_outputs,
154  .priv_class = &aformat_class,
155 };