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  { "f", "A '|'-separated list of sample formats.", OFFSET(formats_str), AV_OPT_TYPE_STRING, .flags = A|F },
54  { "sample_rates", "A '|'-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A|F },
55  { "r", "A '|'-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A|F },
56  { "channel_layouts", "A '|'-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A|F },
57  { "cl", "A '|'-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A|F },
58  { NULL }
59 };
60 
61 AVFILTER_DEFINE_CLASS(aformat);
62 
63 #define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc) \
64 do { \
65  char *next, *cur = str; \
66  int ret; \
67  \
68  while (cur) { \
69  type fmt; \
70  next = strchr(cur, '|'); \
71  if (next) \
72  *next++ = 0; \
73  \
74  if ((fmt = get_fmt(cur)) == none) { \
75  av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
76  return AVERROR(EINVAL); \
77  } \
78  if ((ret = add_to_list(&list, fmt)) < 0) { \
79  return ret; \
80  } \
81  \
82  cur = next; \
83  } \
84 } while (0)
85 
86 static int get_sample_rate(const char *samplerate)
87 {
88  int ret = strtol(samplerate, NULL, 0);
89  return FFMAX(ret, 0);
90 }
91 
93 {
94  AFormatContext *s = ctx->priv;
95  char *next, *cur = s->channel_layouts_str;
96  AVChannelLayout fmt = { 0 };
97  int ret;
98 
99  while (cur) {
100  next = strchr(cur, '|');
101  if (next)
102  *next++ = 0;
103 
104  ret = av_channel_layout_from_string(&fmt, cur);
105  if (ret < 0) {
106 #if FF_API_OLD_CHANNEL_LAYOUT
107  uint64_t mask;
110  if (!mask) {
111 #endif
112  av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: %s.\n", cur);
113  return AVERROR(EINVAL);
114 #if FF_API_OLD_CHANNEL_LAYOUT
115  }
117  av_log(ctx, AV_LOG_WARNING, "Channel layout '%s' uses a deprecated syntax.\n",
118  cur);
120 #endif
121  }
122  ret = ff_add_channel_layout(&s->channel_layouts, &fmt);
124  if (ret < 0)
125  return ret;
126 
127  cur = next;
128  }
129 
130  return 0;
131 }
132 
134 {
135  AFormatContext *s = ctx->priv;
136  int ret;
137 
138  PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
140  PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format,
141  get_sample_rate, 0, "sample rate");
143  if (ret < 0)
144  return ret;
145 
146  return 0;
147 }
148 
150 {
151  AFormatContext *s = ctx->priv;
152 
153  ff_formats_unref(&s->formats);
154  ff_formats_unref(&s->sample_rates);
155  ff_channel_layouts_unref(&s->channel_layouts);
156 }
157 
159 {
160  AFormatContext *s = ctx->priv;
161  int ret;
162 
163  ret = ff_set_common_formats(ctx, s->formats ? s->formats :
165  s->formats = NULL;
166  if (ret < 0)
167  return ret;
168  ret = ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
170  s->sample_rates = NULL;
171  if (ret < 0)
172  return ret;
173  ret = ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
175  s->channel_layouts = NULL;
176  return ret;
177 }
178 
180  {
181  .name = "default",
182  .type = AVMEDIA_TYPE_AUDIO,
183  },
184 };
185 
187  {
188  .name = "default",
189  .type = AVMEDIA_TYPE_AUDIO
190  },
191 };
192 
194  .name = "aformat",
195  .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
196  .init = init,
197  .uninit = uninit,
198  .priv_size = sizeof(AFormatContext),
199  .priv_class = &aformat_class,
204 };
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:82
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
PARSE_FORMATS
#define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc)
Definition: af_aformat.c:63
parse_channel_layouts
static int parse_channel_layouts(AVFilterContext *ctx)
Definition: af_aformat.c:92
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:566
AVOption
AVOption.
Definition: opt.h:251
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:171
AFormatContext::channel_layouts
AVFilterChannelLayouts * channel_layouts
Definition: af_aformat.c:41
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_aformat.c:149
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:165
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:480
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
OFFSET
#define OFFSET(x)
Definition: af_aformat.c:48
av_cold
#define av_cold
Definition: attributes.h:90
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:749
mask
static const uint16_t mask[17]
Definition: lzw.c:38
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_channel_layout_from_mask
FF_ENABLE_DEPRECATION_WARNINGS int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
Definition: channel_layout.c:391
F
#define F
Definition: af_aformat.c:50
get_sample_rate
static int get_sample_rate(const char *samplerate)
Definition: af_aformat.c:86
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_aformat.c:158
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:194
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:66
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:133
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:449
AFormatContext::sample_rates
AVFilterFormats * sample_rates
Definition: af_aformat.c:40
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:239
ff_add_channel_layout
int ff_add_channel_layout(AVFilterChannelLayouts **l, const AVChannelLayout *channel_layout)
Definition: formats.c:466
ff_channel_layouts_unref
void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
Remove a reference to a channel layouts list.
Definition: formats.c:647
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:115
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:301
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AFormatContext
Definition: af_aformat.c:36
avfilter_af_aformat_inputs
static const AVFilterPad avfilter_af_aformat_inputs[]
Definition: af_aformat.c:179
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:635
av_channel_layout_from_string
int av_channel_layout_from_string(AVChannelLayout *channel_layout, const char *str)
Initialize a channel layout from a given string description.
Definition: channel_layout.c:404
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:58
common.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
aformat_options
static const AVOption aformat_options[]
Definition: af_aformat.c:51
AVFilter
Filter definition.
Definition: avfilter.h:161
ret
ret
Definition: filter_design.txt:187
ff_af_aformat
const AVFilter ff_af_aformat
Definition: af_aformat.c:193
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:551
channel_layout.h
avfilter.h
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:632
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:133
AVFilterContext
An instance of a filter.
Definition: avfilter.h:392
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:81
audio.h
avfilter_af_aformat_outputs
static const AVFilterPad avfilter_af_aformat_outputs[]
Definition: af_aformat.c:186
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:195
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_set_common_samplerates
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:726
AFormatContext::formats_str
char * formats_str
Definition: af_aformat.c:43
avstring.h
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
AFormatContext::formats
AVFilterFormats * formats
Definition: af_aformat.c:39
A
#define A
Definition: af_aformat.c:49
ff_set_common_channel_layouts
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
Helpers for query_formats() which set all free audio links to the same list of channel layouts/sample...
Definition: formats.c:708