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