]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_apulsator.c
Merge commit '62825236dba31a2240e25974a3ba41c1303e4edc'
[ffmpeg] / libavfilter / af_apulsator.c
1 /*
2  * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
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
8  * License 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavutil/opt.h"
22 #include "avfilter.h"
23 #include "internal.h"
24 #include "audio.h"
25
26 enum PulsatorModes { SINE, TRIANGLE, SQUARE, SAWUP, SAWDOWN, NB_MODES };
27 enum PulsatorTimings { UNIT_BPM, UNIT_MS, UNIT_HZ, NB_TIMINGS };
28
29 typedef struct SimpleLFO {
30     double phase;
31     double freq;
32     double offset;
33     double amount;
34     double pwidth;
35     int mode;
36     int srate;
37 } SimpleLFO;
38
39 typedef struct AudioPulsatorContext {
40     const AVClass *class;
41     int mode;
42     double level_in;
43     double level_out;
44     double amount;
45     double offset_l;
46     double offset_r;
47     double pwidth;
48     double bpm;
49     double hz;
50     int ms;
51     int timing;
52
53     SimpleLFO lfoL, lfoR;
54 } AudioPulsatorContext;
55
56 #define OFFSET(x) offsetof(AudioPulsatorContext, x)
57 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
58
59 static const AVOption apulsator_options[] = {
60     { "level_in",   "set input gain", OFFSET(level_in),  AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
61     { "level_out", "set output gain", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
62     { "mode",             "set mode", OFFSET(mode),      AV_OPT_TYPE_INT,    {.i64=SINE}, SINE,   NB_MODES-1, FLAGS, "mode" },
63     {   "sine",                 NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=SINE},    0,            0, FLAGS, "mode" },
64     {   "triangle",             NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=TRIANGLE},0,            0, FLAGS, "mode" },
65     {   "square",               NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=SQUARE},  0,            0, FLAGS, "mode" },
66     {   "sawup",                NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=SAWUP},   0,            0, FLAGS, "mode" },
67     {   "sawdown",              NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=SAWDOWN}, 0,            0, FLAGS, "mode" },
68     { "amount",     "set modulation", OFFSET(amount),    AV_OPT_TYPE_DOUBLE, {.dbl=1},       0,            1, FLAGS },
69     { "offset_l",     "set offset L", OFFSET(offset_l),  AV_OPT_TYPE_DOUBLE, {.dbl=0},       0,            1, FLAGS },
70     { "offset_r",     "set offset R", OFFSET(offset_r),  AV_OPT_TYPE_DOUBLE, {.dbl=.5},      0,            1, FLAGS },
71     { "width",     "set pulse width", OFFSET(pwidth),    AV_OPT_TYPE_DOUBLE, {.dbl=1},       0,            2, FLAGS },
72     { "timing",         "set timing", OFFSET(timing),    AV_OPT_TYPE_INT,    {.i64=2},       0, NB_TIMINGS-1, FLAGS, "timing" },
73     {   "bpm",                  NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=UNIT_BPM},  0,          0, FLAGS, "timing" },
74     {   "ms",                   NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=UNIT_MS},   0,          0, FLAGS, "timing" },
75     {   "hz",                   NULL, 0,                 AV_OPT_TYPE_CONST,  {.i64=UNIT_HZ},   0,          0, FLAGS, "timing" },
76     { "bpm",               "set BPM", OFFSET(bpm),       AV_OPT_TYPE_DOUBLE, {.dbl=120},    30,          300, FLAGS },
77     { "ms",                 "set ms", OFFSET(ms),        AV_OPT_TYPE_INT,    {.i64=500},    10,         2000, FLAGS },
78     { "hz",          "set frequency", OFFSET(hz),        AV_OPT_TYPE_DOUBLE, {.dbl=2},    0.01,          100, FLAGS },
79     { NULL }
80 };
81
82 AVFILTER_DEFINE_CLASS(apulsator);
83
84 static void lfo_advance(SimpleLFO *lfo, unsigned count)
85 {
86     lfo->phase = fabs(lfo->phase + count * lfo->freq / lfo->srate);
87     if (lfo->phase >= 1)
88         lfo->phase = fmod(lfo->phase, 1);
89 }
90
91 static double lfo_get_value(SimpleLFO *lfo)
92 {
93     double phs = FFMIN(100, lfo->phase / FFMIN(1.99, FFMAX(0.01, lfo->pwidth)) + lfo->offset);
94     double val;
95
96     if (phs > 1)
97         phs = fmod(phs, 1.);
98
99     switch (lfo->mode) {
100     case SINE:
101         val = sin(phs * 2 * M_PI);
102         break;
103     case TRIANGLE:
104         if (phs > 0.75)
105             val = (phs - 0.75) * 4 - 1;
106         else if (phs > 0.25)
107             val = -4 * phs + 2;
108         else
109             val = phs * 4;
110         break;
111     case SQUARE:
112         val = phs < 0.5 ? -1 : +1;
113         break;
114     case SAWUP:
115         val = phs * 2 - 1;
116         break;
117     case SAWDOWN:
118         val = 1 - phs * 2;
119         break;
120     }
121
122     return val * lfo->amount;
123 }
124
125 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
126 {
127     AVFilterContext *ctx = inlink->dst;
128     AVFilterLink *outlink = ctx->outputs[0];
129     AudioPulsatorContext *s = ctx->priv;
130     const double *src = (const double *)in->data[0];
131     const int nb_samples = in->nb_samples;
132     const double level_out = s->level_out;
133     const double level_in = s->level_in;
134     const double amount = s->amount;
135     AVFrame *out;
136     double *dst;
137     int n;
138
139     if (av_frame_is_writable(in)) {
140         out = in;
141     } else {
142         out = ff_get_audio_buffer(inlink, in->nb_samples);
143         if (!out) {
144             av_frame_free(&in);
145             return AVERROR(ENOMEM);
146         }
147         av_frame_copy_props(out, in);
148     }
149     dst = (double *)out->data[0];
150
151     for (n = 0; n < nb_samples; n++) {
152         double outL;
153         double outR;
154         double inL = src[0] * level_in;
155         double inR = src[1] * level_in;
156         double procL = inL;
157         double procR = inR;
158
159         procL *= lfo_get_value(&s->lfoL) * 0.5 + amount / 2;
160         procR *= lfo_get_value(&s->lfoR) * 0.5 + amount / 2;
161
162         outL = procL + inL * (1 - amount);
163         outR = procR + inR * (1 - amount);
164
165         outL *= level_out;
166         outR *= level_out;
167
168         dst[0] = outL;
169         dst[1] = outR;
170
171         lfo_advance(&s->lfoL, 1);
172         lfo_advance(&s->lfoR, 1);
173
174         dst += 2;
175         src += 2;
176     }
177
178     if (in != out)
179         av_frame_free(&in);
180
181     return ff_filter_frame(outlink, out);
182 }
183
184 static int query_formats(AVFilterContext *ctx)
185 {
186     AVFilterChannelLayouts *layout = NULL;
187     AVFilterFormats *formats = NULL;
188     int ret;
189
190     if ((ret = ff_add_format                 (&formats, AV_SAMPLE_FMT_DBL  )) < 0 ||
191         (ret = ff_set_common_formats         (ctx     , formats            )) < 0 ||
192         (ret = ff_add_channel_layout         (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
193         (ret = ff_set_common_channel_layouts (ctx     , layout             )) < 0)
194         return ret;
195
196     formats = ff_all_samplerates();
197     return ff_set_common_samplerates(ctx, formats);
198 }
199
200 static int config_input(AVFilterLink *inlink)
201 {
202     AVFilterContext *ctx = inlink->dst;
203     AudioPulsatorContext *s = ctx->priv;
204     double freq;
205
206     switch (s->timing) {
207     case UNIT_BPM:  freq = s->bpm / 60;         break;
208     case UNIT_MS:   freq = 1 / (s->ms / 1000.); break;
209     case UNIT_HZ:   freq = s->hz;               break;
210     }
211
212     s->lfoL.freq   = freq;
213     s->lfoR.freq   = freq;
214     s->lfoL.mode   = s->mode;
215     s->lfoR.mode   = s->mode;
216     s->lfoL.offset = s->offset_l;
217     s->lfoR.offset = s->offset_r;
218     s->lfoL.srate  = inlink->sample_rate;
219     s->lfoR.srate  = inlink->sample_rate;
220     s->lfoL.amount = s->amount;
221     s->lfoR.amount = s->amount;
222     s->lfoL.pwidth = s->pwidth;
223     s->lfoR.pwidth = s->pwidth;
224
225     return 0;
226 }
227
228 static const AVFilterPad inputs[] = {
229     {
230         .name         = "default",
231         .type         = AVMEDIA_TYPE_AUDIO,
232         .config_props = config_input,
233         .filter_frame = filter_frame,
234     },
235     { NULL }
236 };
237
238 static const AVFilterPad outputs[] = {
239     {
240         .name = "default",
241         .type = AVMEDIA_TYPE_AUDIO,
242     },
243     { NULL }
244 };
245
246 AVFilter ff_af_apulsator = {
247     .name          = "apulsator",
248     .description   = NULL_IF_CONFIG_SMALL("Audio pulsator."),
249     .priv_size     = sizeof(AudioPulsatorContext),
250     .priv_class    = &apulsator_class,
251     .query_formats = query_formats,
252     .inputs        = inputs,
253     .outputs       = outputs,
254 };