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 Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20 
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 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 },
55 };
56 
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,
62 };
63 
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; \
72  \
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); \
79  \
80  cur = next; \
81  } \
82 } while (0)
83 
84 static int get_sample_rate(const char *samplerate)
85 {
86  int ret = strtol(samplerate, NULL, 0);
87  return FFMAX(ret, 0);
88 }
89 
90 static av_cold int init(AVFilterContext *ctx, const char *args)
91 {
92  AFormatContext *s = ctx->priv;
93  int ret;
94 
95  if (!args) {
96  av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n");
97  return AVERROR(EINVAL);
98  }
99 
100  s->class = &aformat_class;
102 
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;
106  }
107 
111  get_sample_rate, 0, "sample rate");
114  "channel layout");
115 
116 fail:
117  av_opt_free(s);
118  return ret;
119 }
120 
122 {
123  AFormatContext *s = ctx->priv;
124 
125  ff_set_common_formats(ctx, s->formats ? s->formats :
131 
132  return 0;
133 }
134 
136  {
137  .name = "default",
138  .type = AVMEDIA_TYPE_AUDIO,
139  },
140  { NULL }
141 };
142 
144  {
145  .name = "default",
146  .type = AVMEDIA_TYPE_AUDIO
147  },
148  { NULL }
149 };
150 
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),
157 
158  .inputs = avfilter_af_aformat_inputs,
159  .outputs = avfilter_af_aformat_outputs,
160 };
AVOption.
Definition: opt.h:233
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
static const AVFilterPad avfilter_af_aformat_inputs[]
Definition: af_aformat.c:135
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:505
struct AFormatContext AFormatContext
int av_set_options_string(void *ctx, const char *opts, const char *key_val_sep, const char *pairs_sep)
Parse the key/value pairs list in opts.
Definition: opt.c:587
static int query_formats(AVFilterContext *ctx)
Definition: af_aformat.c:121
AVFilterFormats * sample_rates
Definition: af_aformat.c:40
const char * name
Pad name.
Definition: internal.h:39
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
static const AVOption options[]
Definition: af_aformat.c:50
static const AVClass aformat_class
Definition: af_aformat.c:57
AVOptions.
AVFilter avfilter_af_aformat
Definition: af_aformat.c:151
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:375
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by Libav for the given media type.
Definition: formats.c:209
#define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc)
Definition: af_aformat.c:64
A filter pad used for either input or output.
Definition: internal.h:33
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:204
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void * priv
private data for use by the filter
Definition: avfilter.h:439
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
AVFilterFormats * formats
Definition: af_aformat.c:39
audio channel layout utility functions
#define A
Definition: af_aformat.c:49
const AVClass * class
Definition: af_aformat.c:37
#define OFFSET(x)
Definition: af_aformat.c:48
LIBAVUTIL_VERSION_INT
Definition: eval.c:52
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout/...
Definition: formats.c:244
NULL
Definition: eval.c:52
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:63
av_default_item_name
Definition: dnxhdenc.c:43
char * formats_str
Definition: af_aformat.c:43
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:371
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:110
const char * name
filter name
Definition: avfilter.h:372
char * channel_layouts_str
Definition: af_aformat.c:45
char * sample_rates_str
Definition: af_aformat.c:44
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:238
void ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:363
void av_opt_free(void *obj)
Free all string and binary options in obj.
Definition: opt.c:607
common internal and external API header
static av_cold int init(AVFilterContext *ctx, const char *args)
Definition: af_aformat.c:90
AVFilterChannelLayouts * channel_layouts
Definition: af_aformat.c:41
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
int ff_add_format(AVFilterFormats **avff, int fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:199
uint64_t av_get_channel_layout(const char *name)
Return a channel layout id that matches name, or 0 if no match is found.
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:418
static const AVFilterPad avfilter_af_aformat_outputs[]
Definition: af_aformat.c:143
static int get_sample_rate(const char *samplerate)
Definition: af_aformat.c:84
void ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:356
internal API functions
enum AVSampleFormat av_get_sample_fmt(const char *name)
Return a sample format corresponding to name, or AV_SAMPLE_FMT_NONE on error.
Definition: samplefmt.c:54