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 "filters.h" |
25 |
|
|
#include "internal.h" |
26 |
|
|
#include "libavutil/lfg.h" |
27 |
|
|
#include "libavutil/random_seed.h" |
28 |
|
|
|
29 |
|
|
typedef struct ANoiseSrcContext { |
30 |
|
|
const AVClass *class; |
31 |
|
|
int sample_rate; |
32 |
|
|
double amplitude; |
33 |
|
|
int64_t duration; |
34 |
|
|
int color; |
35 |
|
|
int64_t seed; |
36 |
|
|
int nb_samples; |
37 |
|
|
|
38 |
|
|
int64_t pts; |
39 |
|
|
int infinite; |
40 |
|
|
double (*filter)(double white, double *buf, double half_amplitude); |
41 |
|
|
double buf[7]; |
42 |
|
|
AVLFG c; |
43 |
|
|
} ANoiseSrcContext; |
44 |
|
|
|
45 |
|
|
enum NoiseMode { |
46 |
|
|
NM_WHITE, |
47 |
|
|
NM_PINK, |
48 |
|
|
NM_BROWN, |
49 |
|
|
NM_BLUE, |
50 |
|
|
NM_VIOLET, |
51 |
|
|
NM_VELVET, |
52 |
|
|
NM_NB |
53 |
|
|
}; |
54 |
|
|
|
55 |
|
|
#define OFFSET(x) offsetof(ANoiseSrcContext, x) |
56 |
|
|
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
57 |
|
|
|
58 |
|
|
static const AVOption anoisesrc_options[] = { |
59 |
|
|
{ "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 15, INT_MAX, FLAGS }, |
60 |
|
|
{ "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 15, INT_MAX, FLAGS }, |
61 |
|
|
{ "amplitude", "set amplitude", OFFSET(amplitude), AV_OPT_TYPE_DOUBLE, {.dbl = 1.}, 0., 1., FLAGS }, |
62 |
|
|
{ "a", "set amplitude", OFFSET(amplitude), AV_OPT_TYPE_DOUBLE, {.dbl = 1.}, 0., 1., FLAGS }, |
63 |
|
|
{ "duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS }, |
64 |
|
|
{ "d", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS }, |
65 |
|
|
{ "color", "set noise color", OFFSET(color), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NM_NB - 1, FLAGS, "color" }, |
66 |
|
|
{ "colour", "set noise color", OFFSET(color), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NM_NB - 1, FLAGS, "color" }, |
67 |
|
|
{ "c", "set noise color", OFFSET(color), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NM_NB - 1, FLAGS, "color" }, |
68 |
|
|
{ "white", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_WHITE}, 0, 0, FLAGS, "color" }, |
69 |
|
|
{ "pink", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_PINK}, 0, 0, FLAGS, "color" }, |
70 |
|
|
{ "brown", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_BROWN}, 0, 0, FLAGS, "color" }, |
71 |
|
|
{ "blue", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_BLUE}, 0, 0, FLAGS, "color" }, |
72 |
|
|
{ "violet", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_VIOLET}, 0, 0, FLAGS, "color" }, |
73 |
|
|
{ "velvet", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_VELVET}, 0, 0, FLAGS, "color" }, |
74 |
|
|
{ "seed", "set random seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT_MAX, FLAGS }, |
75 |
|
|
{ "s", "set random seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT_MAX, FLAGS }, |
76 |
|
|
{ "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS }, |
77 |
|
|
{ "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS }, |
78 |
|
|
{NULL} |
79 |
|
|
}; |
80 |
|
|
|
81 |
|
|
AVFILTER_DEFINE_CLASS(anoisesrc); |
82 |
|
|
|
83 |
|
|
static av_cold int query_formats(AVFilterContext *ctx) |
84 |
|
|
{ |
85 |
|
|
ANoiseSrcContext *s = ctx->priv; |
86 |
|
|
static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 }; |
87 |
|
|
int sample_rates[] = { s->sample_rate, -1 }; |
88 |
|
|
static const enum AVSampleFormat sample_fmts[] = { |
89 |
|
|
AV_SAMPLE_FMT_DBL, |
90 |
|
|
AV_SAMPLE_FMT_NONE |
91 |
|
|
}; |
92 |
|
|
|
93 |
|
|
AVFilterFormats *formats; |
94 |
|
|
AVFilterChannelLayouts *layouts; |
95 |
|
|
int ret; |
96 |
|
|
|
97 |
|
|
formats = ff_make_format_list(sample_fmts); |
98 |
|
|
if (!formats) |
99 |
|
|
return AVERROR(ENOMEM); |
100 |
|
|
ret = ff_set_common_formats (ctx, formats); |
101 |
|
|
if (ret < 0) |
102 |
|
|
return ret; |
103 |
|
|
|
104 |
|
|
layouts = ff_make_format64_list(chlayouts); |
105 |
|
|
if (!layouts) |
106 |
|
|
return AVERROR(ENOMEM); |
107 |
|
|
ret = ff_set_common_channel_layouts(ctx, layouts); |
108 |
|
|
if (ret < 0) |
109 |
|
|
return ret; |
110 |
|
|
|
111 |
|
|
formats = ff_make_format_list(sample_rates); |
112 |
|
|
if (!formats) |
113 |
|
|
return AVERROR(ENOMEM); |
114 |
|
|
return ff_set_common_samplerates(ctx, formats); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
static double white_filter(double white, double *buf, double ha) |
118 |
|
|
{ |
119 |
|
|
return white; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
static double pink_filter(double white, double *buf, double ha) |
123 |
|
|
{ |
124 |
|
|
double pink; |
125 |
|
|
|
126 |
|
|
/* http://www.musicdsp.org/files/pink.txt */ |
127 |
|
|
buf[0] = 0.99886 * buf[0] + white * 0.0555179; |
128 |
|
|
buf[1] = 0.99332 * buf[1] + white * 0.0750759; |
129 |
|
|
buf[2] = 0.96900 * buf[2] + white * 0.1538520; |
130 |
|
|
buf[3] = 0.86650 * buf[3] + white * 0.3104856; |
131 |
|
|
buf[4] = 0.55000 * buf[4] + white * 0.5329522; |
132 |
|
|
buf[5] = -0.7616 * buf[5] - white * 0.0168980; |
133 |
|
|
pink = buf[0] + buf[1] + buf[2] + buf[3] + buf[4] + buf[5] + buf[6] + white * 0.5362; |
134 |
|
|
buf[6] = white * 0.115926; |
135 |
|
|
return pink * 0.11; |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
static double blue_filter(double white, double *buf, double ha) |
139 |
|
|
{ |
140 |
|
|
double blue; |
141 |
|
|
|
142 |
|
|
/* Same as pink_filter but subtract the offsets rather than add */ |
143 |
|
|
buf[0] = 0.0555179 * white - 0.99886 * buf[0]; |
144 |
|
|
buf[1] = 0.0750759 * white - 0.99332 * buf[1]; |
145 |
|
|
buf[2] = 0.1538520 * white - 0.96900 * buf[2]; |
146 |
|
|
buf[3] = 0.3104856 * white - 0.86650 * buf[3]; |
147 |
|
|
buf[4] = 0.5329522 * white - 0.55000 * buf[4]; |
148 |
|
|
buf[5] = -0.016898 * white + 0.76160 * buf[5]; |
149 |
|
|
blue = buf[0] + buf[1] + buf[2] + buf[3] + buf[4] + buf[5] + buf[6] + white * 0.5362; |
150 |
|
|
buf[6] = white * 0.115926; |
151 |
|
|
return blue * 0.11; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
static double brown_filter(double white, double *buf, double ha) |
155 |
|
|
{ |
156 |
|
|
double brown; |
157 |
|
|
|
158 |
|
|
brown = ((0.02 * white) + buf[0]) / 1.02; |
159 |
|
|
buf[0] = brown; |
160 |
|
|
return brown * 3.5; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
static double violet_filter(double white, double *buf, double ha) |
164 |
|
|
{ |
165 |
|
|
double violet; |
166 |
|
|
|
167 |
|
|
violet = ((0.02 * white) - buf[0]) / 1.02; |
168 |
|
|
buf[0] = violet; |
169 |
|
|
return violet * 3.5; |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
static double velvet_filter(double white, double *buf, double ha) |
173 |
|
|
{ |
174 |
|
|
return 2. * ha * ((white > ha) - (white < -ha)); |
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
static av_cold int config_props(AVFilterLink *outlink) |
178 |
|
|
{ |
179 |
|
|
AVFilterContext *ctx = outlink->src; |
180 |
|
|
ANoiseSrcContext *s = ctx->priv; |
181 |
|
|
|
182 |
|
|
if (s->seed == -1) |
183 |
|
|
s->seed = av_get_random_seed(); |
184 |
|
|
av_lfg_init(&s->c, s->seed); |
185 |
|
|
|
186 |
|
|
if (s->duration == 0) |
187 |
|
|
s->infinite = 1; |
188 |
|
|
s->duration = av_rescale(s->duration, s->sample_rate, AV_TIME_BASE); |
189 |
|
|
|
190 |
|
|
switch (s->color) { |
191 |
|
|
case NM_WHITE: s->filter = white_filter; break; |
192 |
|
|
case NM_PINK: s->filter = pink_filter; break; |
193 |
|
|
case NM_BROWN: s->filter = brown_filter; break; |
194 |
|
|
case NM_BLUE: s->filter = blue_filter; break; |
195 |
|
|
case NM_VIOLET: s->filter = violet_filter; break; |
196 |
|
|
case NM_VELVET: s->filter = velvet_filter; break; |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
return 0; |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
static int activate(AVFilterContext *ctx) |
203 |
|
|
{ |
204 |
|
|
AVFilterLink *outlink = ctx->outputs[0]; |
205 |
|
|
ANoiseSrcContext *s = ctx->priv; |
206 |
|
|
AVFrame *frame; |
207 |
|
|
int nb_samples, i; |
208 |
|
|
double *dst; |
209 |
|
|
|
210 |
|
|
if (!ff_outlink_frame_wanted(outlink)) |
211 |
|
|
return FFERROR_NOT_READY; |
212 |
|
|
|
213 |
|
|
if (!s->infinite && s->duration <= 0) { |
214 |
|
|
ff_outlink_set_status(outlink, AVERROR_EOF, s->pts); |
215 |
|
|
return 0; |
216 |
|
|
} else if (!s->infinite && s->duration < s->nb_samples) { |
217 |
|
|
nb_samples = s->duration; |
218 |
|
|
} else { |
219 |
|
|
nb_samples = s->nb_samples; |
220 |
|
|
} |
221 |
|
|
|
222 |
|
|
if (!(frame = ff_get_audio_buffer(outlink, nb_samples))) |
223 |
|
|
return AVERROR(ENOMEM); |
224 |
|
|
|
225 |
|
|
dst = (double *)frame->data[0]; |
226 |
|
|
for (i = 0; i < nb_samples; i++) { |
227 |
|
|
double white; |
228 |
|
|
white = s->amplitude * ((2 * ((double) av_lfg_get(&s->c) / 0xffffffff)) - 1); |
229 |
|
|
dst[i] = s->filter(white, s->buf, s->amplitude * 0.5); |
230 |
|
|
} |
231 |
|
|
|
232 |
|
|
if (!s->infinite) |
233 |
|
|
s->duration -= nb_samples; |
234 |
|
|
|
235 |
|
|
frame->pts = s->pts; |
236 |
|
|
s->pts += nb_samples; |
237 |
|
|
return ff_filter_frame(outlink, frame); |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
static const AVFilterPad anoisesrc_outputs[] = { |
241 |
|
|
{ |
242 |
|
|
.name = "default", |
243 |
|
|
.type = AVMEDIA_TYPE_AUDIO, |
244 |
|
|
.config_props = config_props, |
245 |
|
|
}, |
246 |
|
|
{ NULL } |
247 |
|
|
}; |
248 |
|
|
|
249 |
|
|
AVFilter ff_asrc_anoisesrc = { |
250 |
|
|
.name = "anoisesrc", |
251 |
|
|
.description = NULL_IF_CONFIG_SMALL("Generate a noise audio signal."), |
252 |
|
|
.query_formats = query_formats, |
253 |
|
|
.priv_size = sizeof(ANoiseSrcContext), |
254 |
|
|
.inputs = NULL, |
255 |
|
|
.activate = activate, |
256 |
|
|
.outputs = anoisesrc_outputs, |
257 |
|
|
.priv_class = &anoisesrc_class, |
258 |
|
|
}; |