FFmpeg
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 '|'-separated list of sample formats.", OFFSET(formats_str), AV_OPT_TYPE_STRING, .flags = A|F },
53  { "sample_rates", "A '|'-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A|F },
54  { "channel_layouts", "A '|'-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, unref_fn, get_fmt, none, desc) \
61 do { \
62  char *next, *cur = str, sep; \
63  int ret; \
64  \
65  if (str && strchr(str, ',')) { \
66  av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "\
67  "separate %s.\n", desc); \
68  sep = ','; \
69  } else \
70  sep = '|'; \
71  \
72  while (cur) { \
73  type fmt; \
74  next = strchr(cur, sep); \
75  if (next) \
76  *next++ = 0; \
77  \
78  if ((fmt = get_fmt(cur)) == none) { \
79  av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
80  return AVERROR(EINVAL); \
81  } \
82  if ((ret = add_to_list(&list, fmt)) < 0) { \
83  unref_fn(&list); \
84  return ret; \
85  } \
86  \
87  cur = next; \
88  } \
89 } while (0)
90 
91 static int get_sample_rate(const char *samplerate)
92 {
93  int ret = strtol(samplerate, NULL, 0);
94  return FFMAX(ret, 0);
95 }
96 
98 {
99  AFormatContext *s = ctx->priv;
100 
101  PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
103  PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format, ff_formats_unref,
104  get_sample_rate, 0, "sample rate");
105  PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts,
107  "channel layout");
108 
109  return 0;
110 }
111 
113 {
114  AFormatContext *s = ctx->priv;
115  int ret;
116 
117  ret = ff_set_common_formats(ctx, s->formats ? s->formats :
119  if (ret < 0)
120  return ret;
121  ret = ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
123  if (ret < 0)
124  return ret;
125  return ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
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  .priv_class = &aformat_class,
154 };
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
opt.h
ff_set_common_channel_layouts
int 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:549
ff_all_channel_counts
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition.
Definition: formats.c:410
AVOption
AVOption.
Definition: opt.h:246
AFormatContext::channel_layouts
AVFilterChannelLayouts * channel_layouts
Definition: af_aformat.c:41
av_get_channel_layout
uint64_t av_get_channel_layout(const char *name)
Return a channel layout id that matches name, or 0 if no match is found.
Definition: channel_layout.c:139
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
AFormatContext::sample_rates_str
char * sample_rates_str
Definition: af_aformat.c:44
ff_all_formats
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition: formats.c:350
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
OFFSET
#define OFFSET(x)
Definition: af_aformat.c:48
av_cold
#define av_cold
Definition: attributes.h:84
ff_set_common_formats
int 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:568
ff_add_channel_layout
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:343
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
F
#define F
Definition: af_aformat.c:50
get_sample_rate
static int get_sample_rate(const char *samplerate)
Definition: af_aformat.c:91
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_aformat.c:112
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AFormatContext::channel_layouts_str
char * channel_layouts_str
Definition: af_aformat.c:45
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(aformat)
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_aformat.c:97
ff_add_format
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:337
AFormatContext::sample_rates
AVFilterFormats * sample_rates
Definition: af_aformat.c:40
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
ff_channel_layouts_unref
void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
Remove a reference to a channel layouts list.
Definition: formats.c:481
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
FFMAX
#define FFMAX(a, b)
Definition: common.h:94
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
AFormatContext
Definition: af_aformat.c:36
avfilter_af_aformat_inputs
static const AVFilterPad avfilter_af_aformat_inputs[]
Definition: af_aformat.c:129
internal.h
ff_formats_unref
void ff_formats_unref(AVFilterFormats **ref)
If *ref is non-NULL, remove *ref as a reference to the format list it currently points to,...
Definition: formats.c:476
av_get_sample_fmt
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:56
common.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
aformat_options
static const AVOption aformat_options[]
Definition: af_aformat.c:51
AVFilter
Filter definition.
Definition: avfilter.h:144
ret
ret
Definition: filter_design.txt:187
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
channel_layout.h
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:338
audio.h
PARSE_FORMATS
#define PARSE_FORMATS(str, type, list, add_to_list, unref_fn, get_fmt, none, desc)
Definition: af_aformat.c:60
ff_af_aformat
AVFilter ff_af_aformat
Definition: af_aformat.c:145
avfilter_af_aformat_outputs
static const AVFilterPad avfilter_af_aformat_outputs[]
Definition: af_aformat.c:137
ff_set_common_samplerates
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
AFormatContext::formats_str
char * formats_str
Definition: af_aformat.c:43
avstring.h
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:227
AFormatContext::formats
AVFilterFormats * formats
Definition: af_aformat.c:39
A
#define A
Definition: af_aformat.c:49