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/mem.h"
30 #include "libavutil/opt.h"
31 
32 #include "audio.h"
33 #include "avfilter.h"
34 #include "filters.h"
35 #include "formats.h"
36 
37 typedef struct AFormatContext {
38  const AVClass *class;
39 
41  unsigned nb_formats;
42 
44  unsigned nb_sample_rates;
45 
49 
50 static const AVOptionArrayDef array_def = { .sep = '|' };
51 
52 #define OFFSET(x) offsetof(AFormatContext, x)
53 #define A AV_OPT_FLAG_AUDIO_PARAM
54 #define F AV_OPT_FLAG_FILTERING_PARAM
55 static const AVOption aformat_options[] = {
56  { "sample_fmts", "A '|'-separated list of sample formats.", OFFSET(formats),
57  AV_OPT_TYPE_SAMPLE_FMT | AV_OPT_TYPE_FLAG_ARRAY, .default_val.arr = &array_def, .flags = A|F },
58  { "f", "A '|'-separated list of sample formats.", OFFSET(formats),
59  AV_OPT_TYPE_SAMPLE_FMT | AV_OPT_TYPE_FLAG_ARRAY, .default_val.arr = &array_def, .flags = A|F },
60  { "sample_rates", "A '|'-separated list of sample rates.", OFFSET(sample_rates),
61  AV_OPT_TYPE_INT | AV_OPT_TYPE_FLAG_ARRAY, .default_val.arr = &array_def, .min = 1, .max = INT_MAX, .flags = A|F },
62  { "r", "A '|'-separated list of sample rates.", OFFSET(sample_rates),
63  AV_OPT_TYPE_INT | AV_OPT_TYPE_FLAG_ARRAY, .default_val.arr = &array_def, .min = 1, .max = INT_MAX, .flags = A|F },
64  { "channel_layouts", "A '|'-separated list of channel layouts.", OFFSET(channel_layouts),
65  AV_OPT_TYPE_CHLAYOUT | AV_OPT_TYPE_FLAG_ARRAY, .default_val.arr = &array_def, .flags = A|F },
66  { "cl", "A '|'-separated list of channel layouts.", OFFSET(channel_layouts),
67  AV_OPT_TYPE_CHLAYOUT | AV_OPT_TYPE_FLAG_ARRAY, .default_val.arr = &array_def, .flags = A|F },
68  { NULL }
69 };
70 
71 AVFILTER_DEFINE_CLASS(aformat);
72 
74 {
75  AFormatContext *s = ctx->priv;
76 
77  // terminate format lists for ff_set*_from_list()
78  if (s->nb_formats) {
79  void *tmp = av_realloc_array(s->formats, s->nb_formats + 1,
80  sizeof(*s->formats));
81  if (!tmp)
82  return AVERROR(ENOMEM);
83  s->formats = tmp;
84  s->formats[s->nb_formats] = AV_SAMPLE_FMT_NONE;
85 
86  }
87  if (s->nb_sample_rates) {
88  void *tmp = av_realloc_array(s->sample_rates, s->nb_sample_rates + 1,
89  sizeof(*s->sample_rates));
90  if (!tmp)
91  return AVERROR(ENOMEM);
92  s->sample_rates = tmp;
93  s->sample_rates[s->nb_sample_rates] = -1;
94  }
95  if (s->nb_channel_layouts) {
96  void *tmp = av_realloc_array(s->channel_layouts, s->nb_channel_layouts + 1,
97  sizeof(*s->channel_layouts));
98  if (!tmp)
99  return AVERROR(ENOMEM);
100  s->channel_layouts = tmp;
101  s->channel_layouts[s->nb_channel_layouts] = (AVChannelLayout){ .nb_channels = 0 };
102  }
103 
104  return 0;
105 }
106 
108  AVFilterFormatsConfig **cfg_in,
109  AVFilterFormatsConfig **cfg_out)
110 {
111  const AFormatContext *s = ctx->priv;
112  int ret;
113 
114  if (s->nb_formats) {
115  ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, s->formats);
116  if (ret < 0)
117  return ret;
118  }
119 
120  if (s->nb_sample_rates) {
121  ret = ff_set_common_samplerates_from_list2(ctx, cfg_in, cfg_out, s->sample_rates);
122  if (ret < 0)
123  return ret;
124  }
125 
126  if (s->nb_channel_layouts) {
127  ret = ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, s->channel_layouts);
128  if (ret < 0)
129  return ret;
130  }
131 
132  return 0;
133 }
134 
136  .name = "aformat",
137  .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
138  .init = init,
139  .priv_size = sizeof(AFormatContext),
140  .priv_class = &aformat_class,
145 };
formats
formats
Definition: signature.h:47
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
AV_OPT_TYPE_SAMPLE_FMT
@ AV_OPT_TYPE_SAMPLE_FMT
Underlying C type is enum AVSampleFormat.
Definition: opt.h:311
AVOptionArrayDef::sep
char sep
Separator between array elements in string representations of this option, used by av_opt_set() and a...
Definition: opt.h:423
AVOptionArrayDef
May be set as default_val for AV_OPT_TYPE_FLAG_ARRAY options.
Definition: opt.h:395
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
sample_rates
static const int sample_rates[]
Definition: dcaenc.h:34
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AVOption
AVOption.
Definition: opt.h:429
AFormatContext::nb_sample_rates
unsigned nb_sample_rates
Definition: af_aformat.c:44
ff_set_common_channel_layouts_from_list2
int ff_set_common_channel_layouts_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const AVChannelLayout *fmts)
Definition: formats.c:920
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
formats.h
array_def
static const AVOptionArrayDef array_def
Definition: af_aformat.c:50
OFFSET
#define OFFSET(x)
Definition: af_aformat.c:52
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:217
F
#define F
Definition: af_aformat.c:54
filters.h
ff_set_common_samplerates_from_list2
int ff_set_common_samplerates_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *samplerates)
Definition: formats.c:944
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
AFormatContext::formats
enum AVSampleFormat * formats
Definition: af_aformat.c:40
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(aformat)
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_aformat.c:73
AFormatContext::channel_layouts
AVChannelLayout * channel_layouts
Definition: af_aformat.c:46
ff_audio_default_filterpad
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition: audio.c:34
AV_OPT_TYPE_CHLAYOUT
@ AV_OPT_TYPE_CHLAYOUT
Underlying C type is AVChannelLayout.
Definition: opt.h:331
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:111
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:94
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:317
AV_OPT_TYPE_FLAG_ARRAY
@ AV_OPT_TYPE_FLAG_ARRAY
May be combined with another regular option type to declare an array option.
Definition: opt.h:346
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AFormatContext::sample_rates
int * sample_rates
Definition: af_aformat.c:43
AFormatContext
Definition: af_aformat.c:37
common.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
FILTER_QUERY_FUNC2
#define FILTER_QUERY_FUNC2(func)
Definition: filters.h:239
aformat_options
static const AVOption aformat_options[]
Definition: af_aformat.c:55
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
AFormatContext::nb_formats
unsigned nb_formats
Definition: af_aformat.c:41
ff_af_aformat
const AVFilter ff_af_aformat
Definition: af_aformat.c:135
ff_set_common_formats_from_list2
int ff_set_common_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *fmts)
Definition: formats.c:1016
channel_layout.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
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:168
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
mem.h
audio.h
AFormatContext::nb_channel_layouts
unsigned nb_channel_layouts
Definition: af_aformat.c:47
query_formats
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition: af_aformat.c:107
channel_layouts
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:112
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
avstring.h
A
#define A
Definition: af_aformat.c:53