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 
22 #include "libavutil/opt.h"
23 #include "audio.h"
24 #include "avfilter.h"
25 #include "filters.h"
26 #include "formats.h"
27 #include "libavutil/lfg.h"
28 #include "libavutil/random_seed.h"
29 
30 typedef struct ANoiseSrcContext {
31  const AVClass *class;
33  double amplitude;
34  double density;
36  int color;
39 
41  int infinite;
42  double (*filter)(double white, double *buf);
43  double buf[7];
46 
47 enum NoiseMode {
55 };
56 
57 #define OFFSET(x) offsetof(ANoiseSrcContext, x)
58 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
59 
60 static const AVOption anoisesrc_options[] = {
61  { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 15, INT_MAX, FLAGS },
62  { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 15, INT_MAX, FLAGS },
63  { "amplitude", "set amplitude", OFFSET(amplitude), AV_OPT_TYPE_DOUBLE, {.dbl = 1.}, 0., 1., FLAGS },
64  { "a", "set amplitude", OFFSET(amplitude), AV_OPT_TYPE_DOUBLE, {.dbl = 1.}, 0., 1., FLAGS },
65  { "duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS },
66  { "d", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS },
67  { "color", "set noise color", OFFSET(color), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NM_NB - 1, FLAGS, .unit = "color" },
68  { "colour", "set noise color", OFFSET(color), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NM_NB - 1, FLAGS, .unit = "color" },
69  { "c", "set noise color", OFFSET(color), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NM_NB - 1, FLAGS, .unit = "color" },
70  { "white", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_WHITE}, 0, 0, FLAGS, .unit = "color" },
71  { "pink", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_PINK}, 0, 0, FLAGS, .unit = "color" },
72  { "brown", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_BROWN}, 0, 0, FLAGS, .unit = "color" },
73  { "blue", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_BLUE}, 0, 0, FLAGS, .unit = "color" },
74  { "violet", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_VIOLET}, 0, 0, FLAGS, .unit = "color" },
75  { "velvet", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_VELVET}, 0, 0, FLAGS, .unit = "color" },
76  { "seed", "set random seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT_MAX, FLAGS },
77  { "s", "set random seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT_MAX, FLAGS },
78  { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
79  { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
80  { "density", "set density", OFFSET(density), AV_OPT_TYPE_DOUBLE, {.dbl = 0.05}, 0., 1., FLAGS },
81  {NULL}
82 };
83 
84 AVFILTER_DEFINE_CLASS(anoisesrc);
85 
87 {
88  ANoiseSrcContext *s = ctx->priv;
89  static const AVChannelLayout chlayouts[] = { AV_CHANNEL_LAYOUT_MONO, { 0 } };
90  int sample_rates[] = { s->sample_rate, -1 };
91  static const enum AVSampleFormat sample_fmts[] = {
94  };
96  if (ret < 0)
97  return ret;
98 
100  if (ret < 0)
101  return ret;
102 
104 }
105 
106 static double white_filter(double white, double *buf)
107 {
108  return white;
109 }
110 
111 static double pink_filter(double white, double *buf)
112 {
113  double pink;
114 
115  /* http://www.musicdsp.org/files/pink.txt */
116  buf[0] = 0.99886 * buf[0] + white * 0.0555179;
117  buf[1] = 0.99332 * buf[1] + white * 0.0750759;
118  buf[2] = 0.96900 * buf[2] + white * 0.1538520;
119  buf[3] = 0.86650 * buf[3] + white * 0.3104856;
120  buf[4] = 0.55000 * buf[4] + white * 0.5329522;
121  buf[5] = -0.7616 * buf[5] - white * 0.0168980;
122  pink = buf[0] + buf[1] + buf[2] + buf[3] + buf[4] + buf[5] + buf[6] + white * 0.5362;
123  buf[6] = white * 0.115926;
124  return pink * 0.11;
125 }
126 
127 static double blue_filter(double white, double *buf)
128 {
129  double blue;
130 
131  /* Same as pink_filter but subtract the offsets rather than add */
132  buf[0] = 0.0555179 * white - 0.99886 * buf[0];
133  buf[1] = 0.0750759 * white - 0.99332 * buf[1];
134  buf[2] = 0.1538520 * white - 0.96900 * buf[2];
135  buf[3] = 0.3104856 * white - 0.86650 * buf[3];
136  buf[4] = 0.5329522 * white - 0.55000 * buf[4];
137  buf[5] = -0.016898 * white + 0.76160 * buf[5];
138  blue = buf[0] + buf[1] + buf[2] + buf[3] + buf[4] + buf[5] + buf[6] + white * 0.5362;
139  buf[6] = white * 0.115926;
140  return blue * 0.11;
141 }
142 
143 static double brown_filter(double white, double *buf)
144 {
145  double brown;
146 
147  brown = ((0.02 * white) + buf[0]) / 1.02;
148  buf[0] = brown;
149  return brown * 3.5;
150 }
151 
152 static double violet_filter(double white, double *buf)
153 {
154  double violet;
155 
156  violet = ((0.02 * white) - buf[0]) / 1.02;
157  buf[0] = violet;
158  return violet * 3.5;
159 }
160 
161 static double velvet_filter(double white, double *buf)
162 {
163  double awhite = fabs(white);
164  return FFDIFFSIGN(white, 0.0) * buf[1] * (awhite < buf[0]);
165 }
166 
167 static av_cold int config_props(AVFilterLink *outlink)
168 {
169  AVFilterContext *ctx = outlink->src;
170  ANoiseSrcContext *s = ctx->priv;
171 
172  if (s->seed == -1)
173  s->seed = av_get_random_seed();
174  av_lfg_init(&s->c, s->seed);
175 
176  if (s->duration == 0)
177  s->infinite = 1;
178  s->duration = av_rescale(s->duration, s->sample_rate, AV_TIME_BASE);
179 
180  switch (s->color) {
181  case NM_WHITE: s->filter = white_filter; break;
182  case NM_PINK: s->filter = pink_filter; break;
183  case NM_BROWN: s->filter = brown_filter; break;
184  case NM_BLUE: s->filter = blue_filter; break;
185  case NM_VIOLET: s->filter = violet_filter; break;
186  case NM_VELVET: s->buf[0] = s->amplitude * s->density;
187  s->buf[1] = s->amplitude;
188  s->filter = velvet_filter; break;
189  }
190 
191  return 0;
192 }
193 
195 {
196  AVFilterLink *outlink = ctx->outputs[0];
197  ANoiseSrcContext *s = ctx->priv;
198  AVFrame *frame;
199  int nb_samples, i;
200  double *dst;
201 
202  if (!ff_outlink_frame_wanted(outlink))
203  return FFERROR_NOT_READY;
204 
205  if (!s->infinite && s->duration <= 0) {
206  ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
207  return 0;
208  } else if (!s->infinite && s->duration < s->nb_samples) {
209  nb_samples = s->duration;
210  } else {
211  nb_samples = s->nb_samples;
212  }
213 
214  if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
215  return AVERROR(ENOMEM);
216 
217  dst = (double *)frame->data[0];
218  for (i = 0; i < nb_samples; i++) {
219  double white;
220  white = s->amplitude * ((2 * ((double) av_lfg_get(&s->c) / 0xffffffff)) - 1);
221  dst[i] = s->filter(white, s->buf);
222  }
223 
224  if (!s->infinite)
225  s->duration -= nb_samples;
226 
227  frame->pts = s->pts;
228  s->pts += nb_samples;
229  return ff_filter_frame(outlink, frame);
230 }
231 
232 static const AVFilterPad anoisesrc_outputs[] = {
233  {
234  .name = "default",
235  .type = AVMEDIA_TYPE_AUDIO,
236  .config_props = config_props,
237  },
238 };
239 
241  .name = "anoisesrc",
242  .description = NULL_IF_CONFIG_SMALL("Generate a noise audio signal."),
243  .priv_size = sizeof(ANoiseSrcContext),
244  .inputs = NULL,
245  .activate = activate,
248  .priv_class = &anoisesrc_class,
249 };
ANoiseSrcContext::density
double density
Definition: asrc_anoisesrc.c:34
ff_get_audio_buffer
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:98
NM_PINK
@ NM_PINK
Definition: asrc_anoisesrc.c:49
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
anoisesrc_options
static const AVOption anoisesrc_options[]
Definition: asrc_anoisesrc.c:60
color
Definition: vf_paletteuse.c:511
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
violet_filter
static double violet_filter(double white, double *buf)
Definition: asrc_anoisesrc.c:152
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1023
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
activate
static int activate(AVFilterContext *ctx)
Definition: asrc_anoisesrc.c:194
int64_t
long long int64_t
Definition: coverity.c:34
ff_set_common_samplerates_from_list
int ff_set_common_samplerates_from_list(AVFilterContext *ctx, const int *samplerates)
Equivalent to ff_set_common_samplerates(ctx, ff_make_format_list(samplerates))
Definition: formats.c:815
brown_filter
static double brown_filter(double white, double *buf)
Definition: asrc_anoisesrc.c:143
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
AVOption
AVOption.
Definition: opt.h:429
ANoiseSrcContext::c
AVLFG c
Definition: asrc_anoisesrc.c:44
NoiseMode
NoiseMode
Definition: asrc_anoisesrc.c:47
AV_OPT_TYPE_DURATION
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
Definition: opt.h:319
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
sample_rate
sample_rate
Definition: ffmpeg_filter.c:424
av_get_random_seed
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:167
formats.h
anoisesrc_outputs
static const AVFilterPad anoisesrc_outputs[]
Definition: asrc_anoisesrc.c:232
ANoiseSrcContext
Definition: asrc_anoisesrc.c:30
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
FFDIFFSIGN
#define FFDIFFSIGN(x, y)
Comparator.
Definition: macros.h:45
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(anoisesrc)
av_cold
#define av_cold
Definition: attributes.h:90
NM_WHITE
@ NM_WHITE
Definition: asrc_anoisesrc.c:48
duration
int64_t duration
Definition: movenc.c:65
ff_outlink_set_status
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:424
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_lfg_get
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition: opt.h:267
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
lfg.h
ff_set_common_formats_from_list
int ff_set_common_formats_from_list(AVFilterContext *ctx, const int *fmts)
Equivalent to ff_set_common_formats(ctx, ff_make_format_list(fmts))
Definition: formats.c:873
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition: opt.h:263
filters.h
NM_VIOLET
@ NM_VIOLET
Definition: asrc_anoisesrc.c:52
ctx
AVFormatContext * ctx
Definition: movenc.c:49
ff_set_common_channel_layouts_from_list
int ff_set_common_channel_layouts_from_list(AVFilterContext *ctx, const AVChannelLayout *fmts)
Equivalent to ff_set_common_channel_layouts(ctx, ff_make_channel_layout_list(fmts))
Definition: formats.c:797
NM_NB
@ NM_NB
Definition: asrc_anoisesrc.c:54
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
NM_BROWN
@ NM_BROWN
Definition: asrc_anoisesrc.c:50
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
NULL
#define NULL
Definition: coverity.c:32
ANoiseSrcContext::infinite
int infinite
Definition: asrc_anoisesrc.c:41
query_formats
static av_cold int query_formats(AVFilterContext *ctx)
Definition: asrc_anoisesrc.c:86
ANoiseSrcContext::amplitude
double amplitude
Definition: asrc_anoisesrc.c:33
double
double
Definition: af_crystalizer.c:132
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
seed
static unsigned int seed
Definition: videogen.c:78
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
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:311
blue_filter
static double blue_filter(double white, double *buf)
Definition: asrc_anoisesrc.c:127
velvet_filter
static double velvet_filter(double white, double *buf)
Definition: asrc_anoisesrc.c:161
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
config_props
static av_cold int config_props(AVFilterLink *outlink)
Definition: asrc_anoisesrc.c:167
ANoiseSrcContext::pts
int64_t pts
Definition: asrc_anoisesrc.c:40
NM_VELVET
@ NM_VELVET
Definition: asrc_anoisesrc.c:53
sample_rates
sample_rates
Definition: ffmpeg_filter.c:424
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
ANoiseSrcContext::sample_rate
int sample_rate
Definition: asrc_anoisesrc.c:32
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
OFFSET
#define OFFSET(x)
Definition: asrc_anoisesrc.c:57
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
av_rescale
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
NM_BLUE
@ NM_BLUE
Definition: asrc_anoisesrc.c:51
ANoiseSrcContext::color
int color
Definition: asrc_anoisesrc.c:36
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
frame
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
Definition: filter_design.txt:264
FLAGS
#define FLAGS
Definition: asrc_anoisesrc.c:58
ANoiseSrcContext::seed
int64_t seed
Definition: asrc_anoisesrc.c:37
random_seed.h
ff_asrc_anoisesrc
const AVFilter ff_asrc_anoisesrc
Definition: asrc_anoisesrc.c:240
channel_layout.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
ANoiseSrcContext::nb_samples
int nb_samples
Definition: asrc_anoisesrc.c:38
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
ANoiseSrcContext::buf
double buf[7]
Definition: asrc_anoisesrc.c:43
audio.h
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:386
white_filter
static double white_filter(double white, double *buf)
Definition: asrc_anoisesrc.c:106
ANoiseSrcContext::filter
double(* filter)(double white, double *buf)
Definition: asrc_anoisesrc.c:42
ff_outlink_frame_wanted
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:61
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: filters.h:236
pink_filter
static double pink_filter(double white, double *buf)
Definition: asrc_anoisesrc.c:111
ANoiseSrcContext::duration
int64_t duration
Definition: asrc_anoisesrc.c:35