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