2 * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen, Damien Zammit and others
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/opt.h"
26 typedef struct BiquadCoeffs {
27 double a0, a1, a2, b1, b2;
30 typedef struct BiquadD2 {
31 double a0, a1, a2, b1, b2, w1, w2;
34 typedef struct RIAACurve {
40 typedef struct AudioEmphasisContext {
43 double level_in, level_out;
46 } AudioEmphasisContext;
48 #define OFFSET(x) offsetof(AudioEmphasisContext, x)
49 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51 static const AVOption aemphasis_options[] = {
52 { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, FLAGS },
53 { "level_out", "set output gain", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, FLAGS },
54 { "mode", "set filter mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "mode" },
55 { "reproduction", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
56 { "production", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
57 { "type", "set filter type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=4}, 0, 8, FLAGS, "type" },
58 { "col", "Columbia", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "type" },
59 { "emi", "EMI", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "type" },
60 { "bsi", "BSI (78RPM)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "type" },
61 { "riaa", "RIAA", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "type" },
62 { "cd", "Compact Disc (CD)", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, FLAGS, "type" },
63 { "50fm", "50µs (FM)", 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, FLAGS, "type" },
64 { "75fm", "75µs (FM)", 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, FLAGS, "type" },
65 { "50kf", "50µs (FM-KF)", 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, FLAGS, "type" },
66 { "75kf", "75µs (FM-KF)", 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, FLAGS, "type" },
70 AVFILTER_DEFINE_CLASS(aemphasis);
72 static inline void biquad_process(BiquadD2 *bq, double *dst, const double *src, int nb_samples,
73 double level_in, double level_out)
75 const double a0 = bq->a0;
76 const double a1 = bq->a1;
77 const double a2 = bq->a2;
78 const double b1 = bq->b1;
79 const double b2 = bq->b2;
83 for (int i = 0; i < nb_samples; i++) {
84 double n = src[i] * level_in;
85 double tmp = n - w1 * b1 - w2 * b2;
86 double out = tmp * a0 + w1 * a1 + w2 * a2;
91 dst[i] = out * level_out;
98 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
100 AVFilterContext *ctx = inlink->dst;
101 AVFilterLink *outlink = ctx->outputs[0];
102 AudioEmphasisContext *s = ctx->priv;
103 const double level_out = s->level_out;
104 const double level_in = s->level_in;
107 if (av_frame_is_writable(in)) {
110 out = ff_get_audio_buffer(outlink, in->nb_samples);
113 return AVERROR(ENOMEM);
115 av_frame_copy_props(out, in);
118 for (int ch = 0; ch < inlink->channels; ch++) {
119 const double *src = (const double *)in->extended_data[ch];
120 double *dst = (double *)out->extended_data[ch];
122 if (s->rc[ch].use_brickw) {
123 biquad_process(&s->rc[ch].brickw, dst, src, in->nb_samples, level_in, 1.);
124 biquad_process(&s->rc[ch].r1, dst, dst, in->nb_samples, 1., level_out);
126 biquad_process(&s->rc[ch].r1, dst, src, in->nb_samples, level_in, level_out);
132 return ff_filter_frame(outlink, out);
135 static int query_formats(AVFilterContext *ctx)
137 AVFilterChannelLayouts *layouts;
138 AVFilterFormats *formats;
139 static const enum AVSampleFormat sample_fmts[] = {
145 layouts = ff_all_channel_counts();
147 return AVERROR(ENOMEM);
148 ret = ff_set_common_channel_layouts(ctx, layouts);
152 formats = ff_make_format_list(sample_fmts);
154 return AVERROR(ENOMEM);
155 ret = ff_set_common_formats(ctx, formats);
159 formats = ff_all_samplerates();
161 return AVERROR(ENOMEM);
162 return ff_set_common_samplerates(ctx, formats);
165 static inline void set_highshelf_rbj(BiquadD2 *bq, double freq, double q, double peak, double sr)
167 double A = sqrt(peak);
168 double w0 = freq * 2 * M_PI / sr;
169 double alpha = sin(w0) / (2 * q);
170 double cw0 = cos(w0);
171 double tmp = 2 * sqrt(A) * alpha;
172 double b0 = 0, ib0 = 0;
174 bq->a0 = A*( (A+1) + (A-1)*cw0 + tmp);
175 bq->a1 = -2*A*( (A-1) + (A+1)*cw0);
176 bq->a2 = A*( (A+1) + (A-1)*cw0 - tmp);
177 b0 = (A+1) - (A-1)*cw0 + tmp;
178 bq->b1 = 2*( (A-1) - (A+1)*cw0);
179 bq->b2 = (A+1) - (A-1)*cw0 - tmp;
189 static inline void set_lp_rbj(BiquadD2 *bq, double fc, double q, double sr, double gain)
191 double omega = 2.0 * M_PI * fc / sr;
192 double sn = sin(omega);
193 double cs = cos(omega);
194 double alpha = sn/(2 * q);
195 double inv = 1.0/(1.0 + alpha);
197 bq->a2 = bq->a0 = gain * inv * (1.0 - cs) * 0.5;
198 bq->a1 = bq->a0 + bq->a0;
199 bq->b1 = (-2.0 * cs * inv);
200 bq->b2 = ((1.0 - alpha) * inv);
203 static double freq_gain(BiquadCoeffs *c, double freq, double sr)
207 freq *= 2.0 * M_PI / sr;
211 /* |(a0 + a1*z + a2*z^2)/(1 + b1*z + b2*z^2)| */
212 return hypot(c->a0 + c->a1*zr + c->a2*(zr*zr-zi*zi), c->a1*zi + 2*c->a2*zr*zi) /
213 hypot(1 + c->b1*zr + c->b2*(zr*zr-zi*zi), c->b1*zi + 2*c->b2*zr*zi);
216 static int config_input(AVFilterLink *inlink)
218 double i, j, k, g, t, a0, a1, a2, b1, b2, tau1, tau2, tau3;
219 double cutfreq, gain1kHz, gc, sr = inlink->sample_rate;
220 AVFilterContext *ctx = inlink->dst;
221 AudioEmphasisContext *s = ctx->priv;
225 s->rc = av_calloc(inlink->channels, sizeof(*s->rc));
227 return AVERROR(ENOMEM);
240 case 2: //"BSI(78rpm)"
250 i = 1. / (2. * M_PI * tau1);
251 j = 1. / (2. * M_PI * tau2);
252 k = 1. / (2. * M_PI * tau3);
254 case 4: //"CD Mastering"
257 tau3 = 0.0000001;// 1.6MHz out of audible range for null impact
258 i = 1. / (2. * M_PI * tau1);
259 j = 1. / (2. * M_PI * tau2);
260 k = 1. / (2. * M_PI * tau3);
262 case 5: //"50µs FM (Europe)"
264 tau2 = tau1 / 20;// not used
266 i = 1. / (2. * M_PI * tau1);
267 j = 1. / (2. * M_PI * tau2);
268 k = 1. / (2. * M_PI * tau3);
270 case 6: //"75µs FM (US)"
272 tau2 = tau1 / 20;// not used
274 i = 1. / (2. * M_PI * tau1);
275 j = 1. / (2. * M_PI * tau2);
276 k = 1. / (2. * M_PI * tau3);
287 if (s->type == 7 || s->type == 8) {
288 double tau = (s->type == 7 ? 0.000050 : 0.000075);
289 double f = 1.0 / (2 * M_PI * tau);
290 double nyq = sr * 0.5;
291 double gain = sqrt(1.0 + nyq * nyq / (f * f)); // gain at Nyquist
292 double cfreq = sqrt((gain - 1.0) * f * f); // frequency
296 q = pow((sr / 3269.0) + 19.5, -0.25); // somewhat poor curve-fit
298 q = pow((sr / 4750.0) + 19.5, -0.25);
300 set_highshelf_rbj(&s->rc[0].r1, cfreq, q, 1. / gain, sr);
302 set_highshelf_rbj(&s->rc[0].r1, cfreq, q, gain, sr);
303 s->rc[0].use_brickw = 0;
305 s->rc[0].use_brickw = 1;
306 if (s->mode == 0) { // Reproduction
307 g = 1. / (4.+2.*i*t+2.*k*t+i*k*t*t);
310 a2 = (-2.*t+j*t*t)*g;
311 b1 = (-8.+2.*i*k*t*t)*g;
312 b2 = (4.-2.*i*t-2.*k*t+i*k*t*t)*g;
313 } else { // Production
314 g = 1. / (2.*t+j*t*t);
315 a0 = (4.+2.*i*t+2.*k*t+i*k*t*t)*g;
316 a1 = (-8.+2.*i*k*t*t)*g;
317 a2 = (4.-2.*i*t-2.*k*t+i*k*t*t)*g;
319 b2 = (-2.*t+j*t*t)*g;
328 // the coeffs above give non-normalized value, so it should be normalized to produce 0dB at 1 kHz
330 // Note: for FM emphasis, use 100 Hz for normalization instead
331 gain1kHz = freq_gain(&coeffs, 1000.0, sr);
332 // divide one filter's x[n-m] coefficients by that value
334 s->rc[0].r1.a0 = coeffs.a0 * gc;
335 s->rc[0].r1.a1 = coeffs.a1 * gc;
336 s->rc[0].r1.a2 = coeffs.a2 * gc;
337 s->rc[0].r1.b1 = coeffs.b1;
338 s->rc[0].r1.b2 = coeffs.b2;
341 cutfreq = FFMIN(0.45 * sr, 21000.);
342 set_lp_rbj(&s->rc[0].brickw, cutfreq, 0.707, sr, 1.);
344 for (ch = 1; ch < inlink->channels; ch++) {
345 memcpy(&s->rc[ch], &s->rc[0], sizeof(RIAACurve));
351 static av_cold void uninit(AVFilterContext *ctx)
353 AudioEmphasisContext *s = ctx->priv;
357 static const AVFilterPad avfilter_af_aemphasis_inputs[] = {
360 .type = AVMEDIA_TYPE_AUDIO,
361 .config_props = config_input,
362 .filter_frame = filter_frame,
367 static const AVFilterPad avfilter_af_aemphasis_outputs[] = {
370 .type = AVMEDIA_TYPE_AUDIO,
375 AVFilter ff_af_aemphasis = {
377 .description = NULL_IF_CONFIG_SMALL("Audio emphasis."),
378 .priv_size = sizeof(AudioEmphasisContext),
379 .priv_class = &aemphasis_class,
381 .query_formats = query_formats,
382 .inputs = avfilter_af_aemphasis_inputs,
383 .outputs = avfilter_af_aemphasis_outputs,