FFmpeg
asrc_anoisesrc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Kyle Swanson <k@ylo.ph>.
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 License
8  * 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
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/opt.h"
22 #include "audio.h"
23 #include "avfilter.h"
24 #include "internal.h"
25 #include "libavutil/lfg.h"
26 #include "libavutil/random_seed.h"
27 
28 typedef struct ANoiseSrcContext {
29  const AVClass *class;
31  double amplitude;
32  int64_t duration;
33  int color;
34  int64_t seed;
36 
37  int64_t pts;
38  int infinite;
39  double (*filter)(double white, double *buf);
40  double buf[7];
43 
44 enum NoiseMode {
51 };
52 
53 #define OFFSET(x) offsetof(ANoiseSrcContext, x)
54 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
55 
56 static const AVOption anoisesrc_options[] = {
57  { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 15, INT_MAX, FLAGS },
58  { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 15, INT_MAX, FLAGS },
59  { "amplitude", "set amplitude", OFFSET(amplitude), AV_OPT_TYPE_DOUBLE, {.dbl = 1.}, 0., 1., FLAGS },
60  { "a", "set amplitude", OFFSET(amplitude), AV_OPT_TYPE_DOUBLE, {.dbl = 1.}, 0., 1., FLAGS },
61  { "duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS },
62  { "d", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS },
63  { "color", "set noise color", OFFSET(color), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NM_NB - 1, FLAGS, "color" },
64  { "colour", "set noise color", OFFSET(color), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NM_NB - 1, FLAGS, "color" },
65  { "c", "set noise color", OFFSET(color), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NM_NB - 1, FLAGS, "color" },
66  { "white", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_WHITE}, 0, 0, FLAGS, "color" },
67  { "pink", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_PINK}, 0, 0, FLAGS, "color" },
68  { "brown", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_BROWN}, 0, 0, FLAGS, "color" },
69  { "blue", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_BLUE}, 0, 0, FLAGS, "color" },
70  { "violet", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_VIOLET}, 0, 0, FLAGS, "color" },
71  { "seed", "set random seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT_MAX, FLAGS },
72  { "s", "set random seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT_MAX, FLAGS },
73  { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
74  { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
75  {NULL}
76 };
77 
78 AVFILTER_DEFINE_CLASS(anoisesrc);
79 
81 {
82  ANoiseSrcContext *s = ctx->priv;
83  static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 };
84  int sample_rates[] = { s->sample_rate, -1 };
85  static const enum AVSampleFormat sample_fmts[] = {
88  };
89 
92  int ret;
93 
94  formats = ff_make_format_list(sample_fmts);
95  if (!formats)
96  return AVERROR(ENOMEM);
97  ret = ff_set_common_formats (ctx, formats);
98  if (ret < 0)
99  return ret;
100 
101  layouts = avfilter_make_format64_list(chlayouts);
102  if (!layouts)
103  return AVERROR(ENOMEM);
104  ret = ff_set_common_channel_layouts(ctx, layouts);
105  if (ret < 0)
106  return ret;
107 
108  formats = ff_make_format_list(sample_rates);
109  if (!formats)
110  return AVERROR(ENOMEM);
111  return ff_set_common_samplerates(ctx, formats);
112 }
113 
114 static double white_filter(double white, double *buf)
115 {
116  return white;
117 }
118 
119 static double pink_filter(double white, double *buf)
120 {
121  double pink;
122 
123  /* http://www.musicdsp.org/files/pink.txt */
124  buf[0] = 0.99886 * buf[0] + white * 0.0555179;
125  buf[1] = 0.99332 * buf[1] + white * 0.0750759;
126  buf[2] = 0.96900 * buf[2] + white * 0.1538520;
127  buf[3] = 0.86650 * buf[3] + white * 0.3104856;
128  buf[4] = 0.55000 * buf[4] + white * 0.5329522;
129  buf[5] = -0.7616 * buf[5] - white * 0.0168980;
130  pink = buf[0] + buf[1] + buf[2] + buf[3] + buf[4] + buf[5] + buf[6] + white * 0.5362;
131  buf[6] = white * 0.115926;
132  return pink * 0.11;
133 }
134 
135 static double blue_filter(double white, double *buf)
136 {
137  double blue;
138 
139  /* Same as pink_filter but subtract the offsets rather than add */
140  buf[0] = 0.0555179 * white - 0.99886 * buf[0];
141  buf[1] = 0.0750759 * white - 0.99332 * buf[1];
142  buf[2] = 0.1538520 * white - 0.96900 * buf[2];
143  buf[3] = 0.3104856 * white - 0.86650 * buf[3];
144  buf[4] = 0.5329522 * white - 0.55000 * buf[4];
145  buf[5] = -0.016898 * white + 0.76160 * buf[5];
146  blue = buf[0] + buf[1] + buf[2] + buf[3] + buf[4] + buf[5] + buf[6] + white * 0.5362;
147  buf[6] = white * 0.115926;
148  return blue * 0.11;
149 }
150 
151 static double brown_filter(double white, double *buf)
152 {
153  double brown;
154 
155  brown = ((0.02 * white) + buf[0]) / 1.02;
156  buf[0] = brown;
157  return brown * 3.5;
158 }
159 
160 static double violet_filter(double white, double *buf)
161 {
162  double violet;
163 
164  violet = ((0.02 * white) - buf[0]) / 1.02;
165  buf[0] = violet;
166  return violet * 3.5;
167 }
168 
169 static av_cold int config_props(AVFilterLink *outlink)
170 {
171  AVFilterContext *ctx = outlink->src;
172  ANoiseSrcContext *s = ctx->priv;
173 
174  if (s->seed == -1)
175  s->seed = av_get_random_seed();
176  av_lfg_init(&s->c, s->seed);
177 
178  if (s->duration == 0)
179  s->infinite = 1;
181 
182  switch (s->color) {
183  case NM_WHITE: s->filter = white_filter; break;
184  case NM_PINK: s->filter = pink_filter; break;
185  case NM_BROWN: s->filter = brown_filter; break;
186  case NM_BLUE: s->filter = blue_filter; break;
187  case NM_VIOLET: s->filter = violet_filter; break;
188  }
189 
190  return 0;
191 }
192 
193 static int request_frame(AVFilterLink *outlink)
194 {
195  AVFilterContext *ctx = outlink->src;
196  ANoiseSrcContext *s = ctx->priv;
197  AVFrame *frame;
198  int nb_samples, i;
199  double *dst;
200 
201  if (!s->infinite && s->duration <= 0) {
202  return AVERROR_EOF;
203  } else if (!s->infinite && s->duration < s->nb_samples) {
204  nb_samples = s->duration;
205  } else {
206  nb_samples = s->nb_samples;
207  }
208 
209  if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
210  return AVERROR(ENOMEM);
211 
212  dst = (double *)frame->data[0];
213  for (i = 0; i < nb_samples; i++) {
214  double white;
215  white = s->amplitude * ((2 * ((double) av_lfg_get(&s->c) / 0xffffffff)) - 1);
216  dst[i] = s->filter(white, s->buf);
217  }
218 
219  if (!s->infinite)
220  s->duration -= nb_samples;
221 
222  frame->pts = s->pts;
223  s->pts += nb_samples;
224  return ff_filter_frame(outlink, frame);
225 }
226 
227 static const AVFilterPad anoisesrc_outputs[] = {
228  {
229  .name = "default",
230  .type = AVMEDIA_TYPE_AUDIO,
231  .request_frame = request_frame,
232  .config_props = config_props,
233  },
234  { NULL }
235 };
236 
238  .name = "anoisesrc",
239  .description = NULL_IF_CONFIG_SMALL("Generate a noise audio signal."),
240  .query_formats = query_formats,
241  .priv_size = sizeof(ANoiseSrcContext),
242  .inputs = NULL,
243  .outputs = anoisesrc_outputs,
244  .priv_class = &anoisesrc_class,
245 };
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
#define NULL
Definition: coverity.c:32
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:550
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
AVOption.
Definition: opt.h:246
Main libavfilter public API header.
static const AVOption anoisesrc_options[]
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:60
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1093
#define av_cold
Definition: attributes.h:82
static av_cold int query_formats(AVFilterContext *ctx)
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:388
AVFilter ff_asrc_anoisesrc
#define OFFSET(x)
#define AVERROR_EOF
End of file.
Definition: error.h:55
static int request_frame(AVFilterLink *outlink)
AVFILTER_DEFINE_CLASS(anoisesrc)
A filter pad used for either input or output.
Definition: internal.h:54
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
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:569
#define FLAGS
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
static double blue_filter(double white, double *buf)
AVFormatContext * ctx
Definition: movenc.c:48
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 the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define s(width, name)
Definition: cbs_vp9.c:257
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
A list of supported channel layouts.
Definition: formats.h:85
static av_cold int config_props(AVFilterLink *outlink)
AVFilterChannelLayouts * avfilter_make_format64_list(const int64_t *fmts)
Definition: formats.c:303
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
static double white_filter(double white, double *buf)
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
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
double(* filter)(double white, double *buf)
Describe the class of an AVClass context structure.
Definition: log.h:67
static double brown_filter(double white, double *buf)
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
NoiseMode
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
static double pink_filter(double white, double *buf)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
static const AVFilterPad anoisesrc_outputs[]
sample_rates
static double violet_filter(double white, double *buf)
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
formats
Definition: signature.h:48
internal API functions
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
#define AV_CH_LAYOUT_MONO
for(j=16;j >0;--j)
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:557