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 RIAACurve {
36 typedef struct AudioEmphasisContext {
39 double level_in, level_out;
44 } AudioEmphasisContext;
46 #define OFFSET(x) offsetof(AudioEmphasisContext, x)
47 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
49 static const AVOption aemphasis_options[] = {
50 { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, FLAGS },
51 { "level_out", "set output gain", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, FLAGS },
52 { "mode", "set filter mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "mode" },
53 { "reproduction", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
54 { "production", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
55 { "type", "set filter type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=4}, 0, 8, FLAGS, "type" },
56 { "col", "Columbia", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "type" },
57 { "emi", "EMI", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "type" },
58 { "bsi", "BSI (78RPM)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "type" },
59 { "riaa", "RIAA", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "type" },
60 { "cd", "Compact Disc (CD)", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, FLAGS, "type" },
61 { "50fm", "50µs (FM)", 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, FLAGS, "type" },
62 { "75fm", "75µs (FM)", 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, FLAGS, "type" },
63 { "50kf", "50µs (FM-KF)", 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, FLAGS, "type" },
64 { "75kf", "75µs (FM-KF)", 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, FLAGS, "type" },
68 AVFILTER_DEFINE_CLASS(aemphasis);
70 static inline void biquad_process(BiquadCoeffs *bq, double *dst, const double *src, int nb_samples,
71 double *w, double level_in, double level_out)
73 const double a0 = bq->a0;
74 const double a1 = bq->a1;
75 const double a2 = bq->a2;
76 const double b1 = bq->b1;
77 const double b2 = bq->b2;
81 for (int i = 0; i < nb_samples; i++) {
82 double n = src[i] * level_in;
83 double tmp = n - w1 * b1 - w2 * b2;
84 double out = tmp * a0 + w1 * a1 + w2 * a2;
89 dst[i] = out * level_out;
96 typedef struct ThreadData {
100 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
102 AudioEmphasisContext *s = ctx->priv;
103 const double level_out = s->level_out;
104 const double level_in = s->level_in;
105 ThreadData *td = arg;
106 AVFrame *out = td->out;
107 AVFrame *in = td->in;
108 const int start = (in->channels * jobnr) / nb_jobs;
109 const int end = (in->channels * (jobnr+1)) / nb_jobs;
111 for (int ch = start; ch < end; ch++) {
112 const double *src = (const double *)in->extended_data[ch];
113 double *w = (double *)s->w->extended_data[ch];
114 double *dst = (double *)out->extended_data[ch];
116 if (s->rc.use_brickw) {
117 biquad_process(&s->rc.brickw, dst, src, in->nb_samples, w + 2, level_in, 1.);
118 biquad_process(&s->rc.r1, dst, dst, in->nb_samples, w, 1., level_out);
120 biquad_process(&s->rc.r1, dst, src, in->nb_samples, w, level_in, level_out);
127 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
129 AVFilterContext *ctx = inlink->dst;
130 AVFilterLink *outlink = ctx->outputs[0];
134 if (av_frame_is_writable(in)) {
137 out = ff_get_audio_buffer(outlink, in->nb_samples);
140 return AVERROR(ENOMEM);
142 av_frame_copy_props(out, in);
145 td.in = in; td.out = out;
146 ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(inlink->channels,
147 ff_filter_get_nb_threads(ctx)));
151 return ff_filter_frame(outlink, out);
154 static int query_formats(AVFilterContext *ctx)
156 AVFilterChannelLayouts *layouts;
157 AVFilterFormats *formats;
158 static const enum AVSampleFormat sample_fmts[] = {
164 layouts = ff_all_channel_counts();
166 return AVERROR(ENOMEM);
167 ret = ff_set_common_channel_layouts(ctx, layouts);
171 formats = ff_make_format_list(sample_fmts);
173 return AVERROR(ENOMEM);
174 ret = ff_set_common_formats(ctx, formats);
178 formats = ff_all_samplerates();
180 return AVERROR(ENOMEM);
181 return ff_set_common_samplerates(ctx, formats);
184 static inline void set_highshelf_rbj(BiquadCoeffs *bq, double freq, double q, double peak, double sr)
186 double A = sqrt(peak);
187 double w0 = freq * 2 * M_PI / sr;
188 double alpha = sin(w0) / (2 * q);
189 double cw0 = cos(w0);
190 double tmp = 2 * sqrt(A) * alpha;
191 double b0 = 0, ib0 = 0;
193 bq->a0 = A*( (A+1) + (A-1)*cw0 + tmp);
194 bq->a1 = -2*A*( (A-1) + (A+1)*cw0);
195 bq->a2 = A*( (A+1) + (A-1)*cw0 - tmp);
196 b0 = (A+1) - (A-1)*cw0 + tmp;
197 bq->b1 = 2*( (A-1) - (A+1)*cw0);
198 bq->b2 = (A+1) - (A-1)*cw0 - tmp;
208 static inline void set_lp_rbj(BiquadCoeffs *bq, double fc, double q, double sr, double gain)
210 double omega = 2.0 * M_PI * fc / sr;
211 double sn = sin(omega);
212 double cs = cos(omega);
213 double alpha = sn/(2 * q);
214 double inv = 1.0/(1.0 + alpha);
216 bq->a2 = bq->a0 = gain * inv * (1.0 - cs) * 0.5;
217 bq->a1 = bq->a0 + bq->a0;
218 bq->b1 = (-2.0 * cs * inv);
219 bq->b2 = ((1.0 - alpha) * inv);
222 static double freq_gain(BiquadCoeffs *c, double freq, double sr)
226 freq *= 2.0 * M_PI / sr;
230 /* |(a0 + a1*z + a2*z^2)/(1 + b1*z + b2*z^2)| */
231 return hypot(c->a0 + c->a1*zr + c->a2*(zr*zr-zi*zi), c->a1*zi + 2*c->a2*zr*zi) /
232 hypot(1 + c->b1*zr + c->b2*(zr*zr-zi*zi), c->b1*zi + 2*c->b2*zr*zi);
235 static int config_input(AVFilterLink *inlink)
237 double i, j, k, g, t, a0, a1, a2, b1, b2, tau1, tau2, tau3;
238 double cutfreq, gain1kHz, gc, sr = inlink->sample_rate;
239 AVFilterContext *ctx = inlink->dst;
240 AudioEmphasisContext *s = ctx->priv;
244 s->w = ff_get_audio_buffer(inlink, 4);
246 return AVERROR(ENOMEM);
259 case 2: //"BSI(78rpm)"
269 i = 1. / (2. * M_PI * tau1);
270 j = 1. / (2. * M_PI * tau2);
271 k = 1. / (2. * M_PI * tau3);
273 case 4: //"CD Mastering"
276 tau3 = 0.0000001;// 1.6MHz out of audible range for null impact
277 i = 1. / (2. * M_PI * tau1);
278 j = 1. / (2. * M_PI * tau2);
279 k = 1. / (2. * M_PI * tau3);
281 case 5: //"50µs FM (Europe)"
283 tau2 = tau1 / 20;// not used
285 i = 1. / (2. * M_PI * tau1);
286 j = 1. / (2. * M_PI * tau2);
287 k = 1. / (2. * M_PI * tau3);
289 case 6: //"75µs FM (US)"
291 tau2 = tau1 / 20;// not used
293 i = 1. / (2. * M_PI * tau1);
294 j = 1. / (2. * M_PI * tau2);
295 k = 1. / (2. * M_PI * tau3);
306 if (s->type == 7 || s->type == 8) {
307 double tau = (s->type == 7 ? 0.000050 : 0.000075);
308 double f = 1.0 / (2 * M_PI * tau);
309 double nyq = sr * 0.5;
310 double gain = sqrt(1.0 + nyq * nyq / (f * f)); // gain at Nyquist
311 double cfreq = sqrt((gain - 1.0) * f * f); // frequency
315 q = pow((sr / 3269.0) + 19.5, -0.25); // somewhat poor curve-fit
317 q = pow((sr / 4750.0) + 19.5, -0.25);
319 set_highshelf_rbj(&s->rc.r1, cfreq, q, 1. / gain, sr);
321 set_highshelf_rbj(&s->rc.r1, cfreq, q, gain, sr);
322 s->rc.use_brickw = 0;
324 s->rc.use_brickw = 1;
325 if (s->mode == 0) { // Reproduction
326 g = 1. / (4.+2.*i*t+2.*k*t+i*k*t*t);
329 a2 = (-2.*t+j*t*t)*g;
330 b1 = (-8.+2.*i*k*t*t)*g;
331 b2 = (4.-2.*i*t-2.*k*t+i*k*t*t)*g;
332 } else { // Production
333 g = 1. / (2.*t+j*t*t);
334 a0 = (4.+2.*i*t+2.*k*t+i*k*t*t)*g;
335 a1 = (-8.+2.*i*k*t*t)*g;
336 a2 = (4.-2.*i*t-2.*k*t+i*k*t*t)*g;
338 b2 = (-2.*t+j*t*t)*g;
347 // the coeffs above give non-normalized value, so it should be normalized to produce 0dB at 1 kHz
349 // Note: for FM emphasis, use 100 Hz for normalization instead
350 gain1kHz = freq_gain(&coeffs, 1000.0, sr);
351 // divide one filter's x[n-m] coefficients by that value
353 s->rc.r1.a0 = coeffs.a0 * gc;
354 s->rc.r1.a1 = coeffs.a1 * gc;
355 s->rc.r1.a2 = coeffs.a2 * gc;
356 s->rc.r1.b1 = coeffs.b1;
357 s->rc.r1.b2 = coeffs.b2;
360 cutfreq = FFMIN(0.45 * sr, 21000.);
361 set_lp_rbj(&s->rc.brickw, cutfreq, 0.707, sr, 1.);
366 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
367 char *res, int res_len, int flags)
371 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
375 return config_input(ctx->inputs[0]);
378 static av_cold void uninit(AVFilterContext *ctx)
380 AudioEmphasisContext *s = ctx->priv;
382 av_frame_free(&s->w);
385 static const AVFilterPad avfilter_af_aemphasis_inputs[] = {
388 .type = AVMEDIA_TYPE_AUDIO,
389 .config_props = config_input,
390 .filter_frame = filter_frame,
395 static const AVFilterPad avfilter_af_aemphasis_outputs[] = {
398 .type = AVMEDIA_TYPE_AUDIO,
403 const AVFilter ff_af_aemphasis = {
405 .description = NULL_IF_CONFIG_SMALL("Audio emphasis."),
406 .priv_size = sizeof(AudioEmphasisContext),
407 .priv_class = &aemphasis_class,
409 .query_formats = query_formats,
410 .inputs = avfilter_af_aemphasis_inputs,
411 .outputs = avfilter_af_aemphasis_outputs,
412 .process_command = process_command,
413 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
414 AVFILTER_FLAG_SLICE_THREADS,