FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/asrc_anoisesrc.c
Date: 2024-04-18 20:30:25
Exec Total Coverage
Lines: 0 84 0.0%
Functions: 0 9 0.0%
Branches: 0 33 0.0%

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