]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_aemphasis.c
Merge commit 'be00ec832c519427cd92218abac77dafdc1d5487'
[ffmpeg] / libavfilter / af_aemphasis.c
1 /*
2  * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen, Damien Zammit 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 <complex.h>
22
23 #include "libavutil/opt.h"
24 #include "avfilter.h"
25 #include "internal.h"
26 #include "audio.h"
27
28 typedef struct BiquadCoeffs {
29     double a0, a1, a2, b1, b2;
30 } BiquadCoeffs;
31
32 typedef struct BiquadD2 {
33     double a0, a1, a2, b1, b2, w1, w2;
34 } BiquadD2;
35
36 typedef struct RIAACurve {
37     BiquadD2 r1;
38     BiquadD2 brickw;
39     int use_brickw;
40 } RIAACurve;
41
42 typedef struct AudioEmphasisContext {
43     const AVClass *class;
44     int mode, type;
45     double level_in, level_out;
46
47     RIAACurve *rc;
48 } AudioEmphasisContext;
49
50 #define OFFSET(x) offsetof(AudioEmphasisContext, x)
51 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
52
53 static const AVOption aemphasis_options[] = {
54     { "level_in",      "set input gain", OFFSET(level_in),  AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, FLAGS },
55     { "level_out",    "set output gain", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, FLAGS },
56     { "mode",         "set filter mode", OFFSET(mode), AV_OPT_TYPE_INT,   {.i64=0}, 0, 1, FLAGS, "mode" },
57     { "reproduction",              NULL,            0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
58     { "production",                NULL,            0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
59     { "type",         "set filter type", OFFSET(type), AV_OPT_TYPE_INT,   {.i64=4}, 0, 8, FLAGS, "type" },
60     { "col",                 "Columbia",            0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "type" },
61     { "emi",                      "EMI",            0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "type" },
62     { "bsi",              "BSI (78RPM)",            0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "type" },
63     { "riaa",                    "RIAA",            0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "type" },
64     { "cd",         "Compact Disc (CD)",            0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, FLAGS, "type" },
65     { "50fm",               "50µs (FM)",            0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, FLAGS, "type" },
66     { "75fm",               "75µs (FM)",            0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, FLAGS, "type" },
67     { "50kf",            "50µs (FM-KF)",            0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, FLAGS, "type" },
68     { "75kf",            "75µs (FM-KF)",            0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, FLAGS, "type" },
69     { NULL }
70 };
71
72 AVFILTER_DEFINE_CLASS(aemphasis);
73
74 static inline double biquad(BiquadD2 *bq, double in)
75 {
76     double n = in;
77     double tmp = n - bq->w1 * bq->b1 - bq->w2 * bq->b2;
78     double out = tmp * bq->a0 + bq->w1 * bq->a1 + bq->w2 * bq->a2;
79
80     bq->w2 = bq->w1;
81     bq->w1 = tmp;
82
83     return out;
84 }
85
86 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
87 {
88     AVFilterContext *ctx = inlink->dst;
89     AVFilterLink *outlink = ctx->outputs[0];
90     AudioEmphasisContext *s = ctx->priv;
91     const double *src = (const double *)in->data[0];
92     const double level_out = s->level_out;
93     const double level_in = s->level_in;
94     AVFrame *out;
95     double *dst;
96     int n, c;
97
98     if (av_frame_is_writable(in)) {
99         out = in;
100     } else {
101         out = ff_get_audio_buffer(inlink, in->nb_samples);
102         if (!out) {
103             av_frame_free(&in);
104             return AVERROR(ENOMEM);
105         }
106         av_frame_copy_props(out, in);
107     }
108     dst = (double *)out->data[0];
109
110     for (n = 0; n < in->nb_samples; n++) {
111         for (c = 0; c < inlink->channels; c++)
112             dst[c] = level_out * biquad(&s->rc[c].r1, s->rc[c].use_brickw ? biquad(&s->rc[c].brickw, src[c] * level_in) : src[c] * level_in);
113         dst += inlink->channels;
114         src += inlink->channels;
115     }
116
117     if (in != out)
118         av_frame_free(&in);
119     return ff_filter_frame(outlink, out);
120 }
121
122 static int query_formats(AVFilterContext *ctx)
123 {
124     AVFilterChannelLayouts *layouts;
125     AVFilterFormats *formats;
126     static const enum AVSampleFormat sample_fmts[] = {
127         AV_SAMPLE_FMT_DBL,
128         AV_SAMPLE_FMT_NONE
129     };
130     int ret;
131
132     layouts = ff_all_channel_counts();
133     if (!layouts)
134         return AVERROR(ENOMEM);
135     ret = ff_set_common_channel_layouts(ctx, layouts);
136     if (ret < 0)
137         return ret;
138
139     formats = ff_make_format_list(sample_fmts);
140     if (!formats)
141         return AVERROR(ENOMEM);
142     ret = ff_set_common_formats(ctx, formats);
143     if (ret < 0)
144         return ret;
145
146     formats = ff_all_samplerates();
147     if (!formats)
148         return AVERROR(ENOMEM);
149     return ff_set_common_samplerates(ctx, formats);
150 }
151
152 static inline void set_highshelf_rbj(BiquadD2 *bq, double freq, double q, double peak, double sr)
153 {
154     double A = sqrt(peak);
155     double w0 = freq * 2 * M_PI / sr;
156     double alpha = sin(w0) / (2 * q);
157     double cw0 = cos(w0);
158     double tmp = 2 * sqrt(A) * alpha;
159     double b0 = 0, ib0 = 0;
160
161     bq->a0 =    A*( (A+1) + (A-1)*cw0 + tmp);
162     bq->a1 = -2*A*( (A-1) + (A+1)*cw0);
163     bq->a2 =    A*( (A+1) + (A-1)*cw0 - tmp);
164         b0 =        (A+1) - (A-1)*cw0 + tmp;
165     bq->b1 =    2*( (A-1) - (A+1)*cw0);
166     bq->b2 =        (A+1) - (A-1)*cw0 - tmp;
167
168     ib0     = 1 / b0;
169     bq->b1 *= ib0;
170     bq->b2 *= ib0;
171     bq->a0 *= ib0;
172     bq->a1 *= ib0;
173     bq->a2 *= ib0;
174 }
175
176 static inline void set_lp_rbj(BiquadD2 *bq, double fc, double q, double sr, double gain)
177 {
178     double omega = 2.0 * M_PI * fc / sr;
179     double sn = sin(omega);
180     double cs = cos(omega);
181     double alpha = sn/(2 * q);
182     double inv = 1.0/(1.0 + alpha);
183
184     bq->a2 = bq->a0 = gain * inv * (1.0 - cs) * 0.5;
185     bq->a1 = bq->a0 + bq->a0;
186     bq->b1 = (-2.0 * cs * inv);
187     bq->b2 = ((1.0 - alpha) * inv);
188 }
189
190 static double freq_gain(BiquadCoeffs *c, double freq, double sr)
191 {
192     double complex z, w;
193
194     freq *= 2.0 * M_PI / sr;
195     w = 0 + I * freq;
196     z = 1.0 / cexp(w);
197
198     return cabs(((double complex)c->a0 + c->a1 * z + c->a2 * z*z) /
199                 ((double complex)1.0 + c->b1 * z + c->b2 * z*z));
200 }
201
202 static int config_input(AVFilterLink *inlink)
203 {
204     double i, j, k, g, t, a0, a1, a2, b1, b2, tau1, tau2, tau3;
205     double cutfreq, gain1kHz, gc, sr = inlink->sample_rate;
206     AVFilterContext *ctx = inlink->dst;
207     AudioEmphasisContext *s = ctx->priv;
208     BiquadCoeffs coeffs;
209     int ch;
210
211     s->rc = av_calloc(inlink->channels, sizeof(*s->rc));
212     if (!s->rc)
213         return AVERROR(ENOMEM);
214
215     switch (s->type) {
216     case 0: //"Columbia"
217         i = 100.;
218         j = 500.;
219         k = 1590.;
220         break;
221     case 1: //"EMI"
222         i = 70.;
223         j = 500.;
224         k = 2500.;
225         break;
226     case 2: //"BSI(78rpm)"
227         i = 50.;
228         j = 353.;
229         k = 3180.;
230         break;
231     case 3: //"RIAA"
232     default:
233         tau1 = 0.003180;
234         tau2 = 0.000318;
235         tau3 = 0.000075;
236         i = 1. / (2. * M_PI * tau1);
237         j = 1. / (2. * M_PI * tau2);
238         k = 1. / (2. * M_PI * tau3);
239         break;
240     case 4: //"CD Mastering"
241         tau1 = 0.000050;
242         tau2 = 0.000015;
243         tau3 = 0.0000001;// 1.6MHz out of audible range for null impact
244         i = 1. / (2. * M_PI * tau1);
245         j = 1. / (2. * M_PI * tau2);
246         k = 1. / (2. * M_PI * tau3);
247         break;
248     case 5: //"50µs FM (Europe)"
249         tau1 = 0.000050;
250         tau2 = tau1 / 20;// not used
251         tau3 = tau1 / 50;//
252         i = 1. / (2. * M_PI * tau1);
253         j = 1. / (2. * M_PI * tau2);
254         k = 1. / (2. * M_PI * tau3);
255         break;
256     case 6: //"75µs FM (US)"
257         tau1 = 0.000075;
258         tau2 = tau1 / 20;// not used
259         tau3 = tau1 / 50;//
260         i = 1. / (2. * M_PI * tau1);
261         j = 1. / (2. * M_PI * tau2);
262         k = 1. / (2. * M_PI * tau3);
263         break;
264     }
265
266     i *= 2 * M_PI;
267     j *= 2 * M_PI;
268     k *= 2 * M_PI;
269
270     t = 1. / sr;
271
272     //swap a1 b1, a2 b2
273     if (s->type == 7 || s->type == 8) {
274         double tau = (s->type == 7 ? 0.000050 : 0.000075);
275         double f = 1.0 / (2 * M_PI * tau);
276         double nyq = sr * 0.5;
277         double gain = sqrt(1.0 + nyq * nyq / (f * f)); // gain at Nyquist
278         double cfreq = sqrt((gain - 1.0) * f * f); // frequency
279         double q = 1.0;
280
281         if (s->type == 8)
282             q = pow((sr / 3269.0) + 19.5, -0.25); // somewhat poor curve-fit
283         if (s->type == 7)
284             q = pow((sr / 4750.0) + 19.5, -0.25);
285         if (s->mode == 0)
286             set_highshelf_rbj(&s->rc[0].r1, cfreq, q, 1. / gain, sr);
287         else
288             set_highshelf_rbj(&s->rc[0].r1, cfreq, q, gain, sr);
289         s->rc[0].use_brickw = 0;
290     } else {
291         s->rc[0].use_brickw = 1;
292         if (s->mode == 0) { // Reproduction
293             g  = 1. / (4.+2.*i*t+2.*k*t+i*k*t*t);
294             a0 = (2.*t+j*t*t)*g;
295             a1 = (2.*j*t*t)*g;
296             a2 = (-2.*t+j*t*t)*g;
297             b1 = (-8.+2.*i*k*t*t)*g;
298             b2 = (4.-2.*i*t-2.*k*t+i*k*t*t)*g;
299         } else {  // Production
300             g  = 1. / (2.*t+j*t*t);
301             a0 = (4.+2.*i*t+2.*k*t+i*k*t*t)*g;
302             a1 = (-8.+2.*i*k*t*t)*g;
303             a2 = (4.-2.*i*t-2.*k*t+i*k*t*t)*g;
304             b1 = (2.*j*t*t)*g;
305             b2 = (-2.*t+j*t*t)*g;
306         }
307
308         coeffs.a0 = a0;
309         coeffs.a1 = a1;
310         coeffs.a2 = a2;
311         coeffs.b1 = b1;
312         coeffs.b2 = b2;
313
314         // the coeffs above give non-normalized value, so it should be normalized to produce 0dB at 1 kHz
315         // find actual gain
316         // Note: for FM emphasis, use 100 Hz for normalization instead
317         gain1kHz = freq_gain(&coeffs, 1000.0, sr);
318         // divide one filter's x[n-m] coefficients by that value
319         gc = 1.0 / gain1kHz;
320         s->rc[0].r1.a0 = coeffs.a0 * gc;
321         s->rc[0].r1.a1 = coeffs.a1 * gc;
322         s->rc[0].r1.a2 = coeffs.a2 * gc;
323         s->rc[0].r1.b1 = coeffs.b1;
324         s->rc[0].r1.b2 = coeffs.b2;
325     }
326
327     cutfreq = FFMIN(0.45 * sr, 21000.);
328     set_lp_rbj(&s->rc[0].brickw, cutfreq, 0.707, sr, 1.);
329
330     for (ch = 1; ch < inlink->channels; ch++) {
331         memcpy(&s->rc[ch], &s->rc[0], sizeof(RIAACurve));
332     }
333
334     return 0;
335 }
336
337 static av_cold void uninit(AVFilterContext *ctx)
338 {
339     AudioEmphasisContext *s = ctx->priv;
340     av_freep(&s->rc);
341 }
342
343 static const AVFilterPad avfilter_af_aemphasis_inputs[] = {
344     {
345         .name         = "default",
346         .type         = AVMEDIA_TYPE_AUDIO,
347         .config_props = config_input,
348         .filter_frame = filter_frame,
349     },
350     { NULL }
351 };
352
353 static const AVFilterPad avfilter_af_aemphasis_outputs[] = {
354     {
355         .name = "default",
356         .type = AVMEDIA_TYPE_AUDIO,
357     },
358     { NULL }
359 };
360
361 AVFilter ff_af_aemphasis = {
362     .name          = "aemphasis",
363     .description   = NULL_IF_CONFIG_SMALL("Audio emphasis."),
364     .priv_size     = sizeof(AudioEmphasisContext),
365     .priv_class    = &aemphasis_class,
366     .uninit        = uninit,
367     .query_formats = query_formats,
368     .inputs        = avfilter_af_aemphasis_inputs,
369     .outputs       = avfilter_af_aemphasis_outputs,
370 };