Line |
Branch |
Exec |
Source |
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/channel_layout.h" |
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; |
32 |
|
|
int sample_rate; |
33 |
|
|
double amplitude; |
34 |
|
|
double density; |
35 |
|
|
int64_t duration; |
36 |
|
|
int color; |
37 |
|
|
int64_t seed; |
38 |
|
|
int nb_samples; |
39 |
|
|
|
40 |
|
|
int64_t pts; |
41 |
|
|
int infinite; |
42 |
|
|
double (*filter)(double white, double *buf); |
43 |
|
|
double buf[7]; |
44 |
|
|
AVLFG c; |
45 |
|
|
} ANoiseSrcContext; |
46 |
|
|
|
47 |
|
|
enum NoiseMode { |
48 |
|
|
NM_WHITE, |
49 |
|
|
NM_PINK, |
50 |
|
|
NM_BROWN, |
51 |
|
|
NM_BLUE, |
52 |
|
|
NM_VIOLET, |
53 |
|
|
NM_VELVET, |
54 |
|
|
NM_NB |
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 |
|
|
|
86 |
|
✗ |
static av_cold int query_formats(const AVFilterContext *ctx, |
87 |
|
|
AVFilterFormatsConfig **cfg_in, |
88 |
|
|
AVFilterFormatsConfig **cfg_out) |
89 |
|
|
{ |
90 |
|
✗ |
const ANoiseSrcContext *s = ctx->priv; |
91 |
|
|
static const AVChannelLayout chlayouts[] = { AV_CHANNEL_LAYOUT_MONO, { 0 } }; |
92 |
|
✗ |
int sample_rates[] = { s->sample_rate, -1 }; |
93 |
|
|
static const enum AVSampleFormat sample_fmts[] = { |
94 |
|
|
AV_SAMPLE_FMT_DBL, |
95 |
|
|
AV_SAMPLE_FMT_NONE |
96 |
|
|
}; |
97 |
|
✗ |
int ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, sample_fmts); |
98 |
|
✗ |
if (ret < 0) |
99 |
|
✗ |
return ret; |
100 |
|
|
|
101 |
|
✗ |
ret = ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, chlayouts); |
102 |
|
✗ |
if (ret < 0) |
103 |
|
✗ |
return ret; |
104 |
|
|
|
105 |
|
✗ |
return ff_set_common_samplerates_from_list2(ctx, cfg_in, cfg_out, sample_rates); |
106 |
|
|
} |
107 |
|
|
|
108 |
|
✗ |
static double white_filter(double white, double *buf) |
109 |
|
|
{ |
110 |
|
✗ |
return white; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
✗ |
static double pink_filter(double white, double *buf) |
114 |
|
|
{ |
115 |
|
|
double pink; |
116 |
|
|
|
117 |
|
|
/* http://www.musicdsp.org/files/pink.txt */ |
118 |
|
✗ |
buf[0] = 0.99886 * buf[0] + white * 0.0555179; |
119 |
|
✗ |
buf[1] = 0.99332 * buf[1] + white * 0.0750759; |
120 |
|
✗ |
buf[2] = 0.96900 * buf[2] + white * 0.1538520; |
121 |
|
✗ |
buf[3] = 0.86650 * buf[3] + white * 0.3104856; |
122 |
|
✗ |
buf[4] = 0.55000 * buf[4] + white * 0.5329522; |
123 |
|
✗ |
buf[5] = -0.7616 * buf[5] - white * 0.0168980; |
124 |
|
✗ |
pink = buf[0] + buf[1] + buf[2] + buf[3] + buf[4] + buf[5] + buf[6] + white * 0.5362; |
125 |
|
✗ |
buf[6] = white * 0.115926; |
126 |
|
✗ |
return pink * 0.11; |
127 |
|
|
} |
128 |
|
|
|
129 |
|
✗ |
static double blue_filter(double white, double *buf) |
130 |
|
|
{ |
131 |
|
|
double blue; |
132 |
|
|
|
133 |
|
|
/* Same as pink_filter but subtract the offsets rather than add */ |
134 |
|
✗ |
buf[0] = 0.0555179 * white - 0.99886 * buf[0]; |
135 |
|
✗ |
buf[1] = 0.0750759 * white - 0.99332 * buf[1]; |
136 |
|
✗ |
buf[2] = 0.1538520 * white - 0.96900 * buf[2]; |
137 |
|
✗ |
buf[3] = 0.3104856 * white - 0.86650 * buf[3]; |
138 |
|
✗ |
buf[4] = 0.5329522 * white - 0.55000 * buf[4]; |
139 |
|
✗ |
buf[5] = -0.016898 * white + 0.76160 * buf[5]; |
140 |
|
✗ |
blue = buf[0] + buf[1] + buf[2] + buf[3] + buf[4] + buf[5] + buf[6] + white * 0.5362; |
141 |
|
✗ |
buf[6] = white * 0.115926; |
142 |
|
✗ |
return blue * 0.11; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
✗ |
static double brown_filter(double white, double *buf) |
146 |
|
|
{ |
147 |
|
|
double brown; |
148 |
|
|
|
149 |
|
✗ |
brown = ((0.02 * white) + buf[0]) / 1.02; |
150 |
|
✗ |
buf[0] = brown; |
151 |
|
✗ |
return brown * 3.5; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
✗ |
static double violet_filter(double white, double *buf) |
155 |
|
|
{ |
156 |
|
|
double violet; |
157 |
|
|
|
158 |
|
✗ |
violet = ((0.02 * white) - buf[0]) / 1.02; |
159 |
|
✗ |
buf[0] = violet; |
160 |
|
✗ |
return violet * 3.5; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
✗ |
static double velvet_filter(double white, double *buf) |
164 |
|
|
{ |
165 |
|
✗ |
double awhite = fabs(white); |
166 |
|
✗ |
return FFDIFFSIGN(white, 0.0) * buf[1] * (awhite < buf[0]); |
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; |
180 |
|
✗ |
s->duration = av_rescale(s->duration, s->sample_rate, AV_TIME_BASE); |
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 |
|
✗ |
case NM_VELVET: s->buf[0] = s->amplitude * s->density; |
189 |
|
✗ |
s->buf[1] = s->amplitude; |
190 |
|
✗ |
s->filter = velvet_filter; break; |
191 |
|
|
} |
192 |
|
|
|
193 |
|
✗ |
return 0; |
194 |
|
|
} |
195 |
|
|
|
196 |
|
✗ |
static int activate(AVFilterContext *ctx) |
197 |
|
|
{ |
198 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
199 |
|
✗ |
ANoiseSrcContext *s = ctx->priv; |
200 |
|
|
AVFrame *frame; |
201 |
|
|
int nb_samples, i; |
202 |
|
|
double *dst; |
203 |
|
|
|
204 |
|
✗ |
if (!ff_outlink_frame_wanted(outlink)) |
205 |
|
✗ |
return FFERROR_NOT_READY; |
206 |
|
|
|
207 |
|
✗ |
if (!s->infinite && s->duration <= 0) { |
208 |
|
✗ |
ff_outlink_set_status(outlink, AVERROR_EOF, s->pts); |
209 |
|
✗ |
return 0; |
210 |
|
✗ |
} else if (!s->infinite && s->duration < s->nb_samples) { |
211 |
|
✗ |
nb_samples = s->duration; |
212 |
|
|
} else { |
213 |
|
✗ |
nb_samples = s->nb_samples; |
214 |
|
|
} |
215 |
|
|
|
216 |
|
✗ |
if (!(frame = ff_get_audio_buffer(outlink, nb_samples))) |
217 |
|
✗ |
return AVERROR(ENOMEM); |
218 |
|
|
|
219 |
|
✗ |
dst = (double *)frame->data[0]; |
220 |
|
✗ |
for (i = 0; i < nb_samples; i++) { |
221 |
|
|
double white; |
222 |
|
✗ |
white = s->amplitude * ((2 * ((double) av_lfg_get(&s->c) / 0xffffffff)) - 1); |
223 |
|
✗ |
dst[i] = s->filter(white, s->buf); |
224 |
|
|
} |
225 |
|
|
|
226 |
|
✗ |
if (!s->infinite) |
227 |
|
✗ |
s->duration -= nb_samples; |
228 |
|
|
|
229 |
|
✗ |
frame->pts = s->pts; |
230 |
|
✗ |
s->pts += nb_samples; |
231 |
|
✗ |
return ff_filter_frame(outlink, frame); |
232 |
|
|
} |
233 |
|
|
|
234 |
|
|
static const AVFilterPad anoisesrc_outputs[] = { |
235 |
|
|
{ |
236 |
|
|
.name = "default", |
237 |
|
|
.type = AVMEDIA_TYPE_AUDIO, |
238 |
|
|
.config_props = config_props, |
239 |
|
|
}, |
240 |
|
|
}; |
241 |
|
|
|
242 |
|
|
const AVFilter ff_asrc_anoisesrc = { |
243 |
|
|
.name = "anoisesrc", |
244 |
|
|
.description = NULL_IF_CONFIG_SMALL("Generate a noise audio signal."), |
245 |
|
|
.priv_size = sizeof(ANoiseSrcContext), |
246 |
|
|
.inputs = NULL, |
247 |
|
|
.activate = activate, |
248 |
|
|
FILTER_OUTPUTS(anoisesrc_outputs), |
249 |
|
|
FILTER_QUERY_FUNC2(query_formats), |
250 |
|
|
.priv_class = &anoisesrc_class, |
251 |
|
|
}; |
252 |
|
|
|