FFmpeg
Loading...
Searching...
No Matches
af_aemphasis.c
Go to the documentation of this file.
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 "libavutil/opt.h"
22#include "avfilter.h"
23#include "filters.h"
24#include "audio.h"
25
26typedef struct BiquadCoeffs {
27 double a0, a1, a2, b1, b2;
29
35
45
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
48
49static 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, .unit = "mode" },
53 { "reproduction", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "mode" },
54 { "production", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "mode" },
55 { "type", "set filter type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=4}, 0, 8, FLAGS, .unit = "type" },
56 { "col", "Columbia", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "type" },
57 { "emi", "EMI", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "type" },
58 { "bsi", "BSI (78RPM)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, .unit = "type" },
59 { "riaa", "RIAA", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, .unit = "type" },
60 { "cd", "Compact Disc (CD)", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, FLAGS, .unit = "type" },
61 { "50fm", "50µs (FM)", 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, FLAGS, .unit = "type" },
62 { "75fm", "75µs (FM)", 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, FLAGS, .unit = "type" },
63 { "50kf", "50µs (FM-KF)", 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, FLAGS, .unit = "type" },
64 { "75kf", "75µs (FM-KF)", 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, FLAGS, .unit = "type" },
65 { NULL }
66};
67
69
70static inline void biquad_process(BiquadCoeffs *bq, double *dst, const double *src, int nb_samples,
71 double *w, double level_in, double level_out)
72{
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;
78 double w1 = w[0];
79 double w2 = w[1];
80
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;
85
86 w2 = w1;
87 w1 = tmp;
88
89 dst[i] = out * level_out;
90 }
91
92 w[0] = w1;
93 w[1] = w2;
94}
95
96typedef struct ThreadData {
97 AVFrame *in, *out;
99
100static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
101{
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 = ff_slice_pos(in->ch_layout.nb_channels, jobnr, nb_jobs);
109 const int end = ff_slice_pos(in->ch_layout.nb_channels, jobnr + 1, nb_jobs);
110
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];
115
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);
119 } else {
120 biquad_process(&s->rc.r1, dst, src, in->nb_samples, w, level_in, level_out);
121 }
122 }
123
124 return 0;
125}
126
127static int filter_frame(AVFilterLink *inlink, AVFrame *in)
128{
129 AVFilterContext *ctx = inlink->dst;
130 AVFilterLink *outlink = ctx->outputs[0];
131 ThreadData td;
132 AVFrame *out;
133
134 if (av_frame_is_writable(in)) {
135 out = in;
136 } else {
137 out = ff_get_audio_buffer(outlink, in->nb_samples);
138 if (!out) {
139 av_frame_free(&in);
140 return AVERROR(ENOMEM);
141 }
143 }
144
145 td.in = in; td.out = out;
148
149 if (in != out)
150 av_frame_free(&in);
151 return ff_filter_frame(outlink, out);
152}
153
154static inline void set_highshelf_rbj(BiquadCoeffs *bq, double freq, double q, double peak, double sr)
155{
156 double A = sqrt(peak);
157 double w0 = freq * 2 * M_PI / sr;
158 double alpha = sin(w0) / (2 * q);
159 double cw0 = cos(w0);
160 double tmp = 2 * sqrt(A) * alpha;
161 double b0 = 0, ib0 = 0;
162
163 bq->a0 = A*( (A+1) + (A-1)*cw0 + tmp);
164 bq->a1 = -2*A*( (A-1) + (A+1)*cw0);
165 bq->a2 = A*( (A+1) + (A-1)*cw0 - tmp);
166 b0 = (A+1) - (A-1)*cw0 + tmp;
167 bq->b1 = 2*( (A-1) - (A+1)*cw0);
168 bq->b2 = (A+1) - (A-1)*cw0 - tmp;
169
170 ib0 = 1 / b0;
171 bq->b1 *= ib0;
172 bq->b2 *= ib0;
173 bq->a0 *= ib0;
174 bq->a1 *= ib0;
175 bq->a2 *= ib0;
176}
177
178static inline void set_lp_rbj(BiquadCoeffs *bq, double fc, double q, double sr, double gain)
179{
180 double omega = 2.0 * M_PI * fc / sr;
181 double sn = sin(omega);
182 double cs = cos(omega);
183 double alpha = sn/(2 * q);
184 double inv = 1.0/(1.0 + alpha);
185
186 bq->a2 = bq->a0 = gain * inv * (1.0 - cs) * 0.5;
187 bq->a1 = bq->a0 + bq->a0;
188 bq->b1 = (-2.0 * cs * inv);
189 bq->b2 = ((1.0 - alpha) * inv);
190}
191
192static double freq_gain(BiquadCoeffs *c, double freq, double sr)
193{
194 double zr, zi;
195
196 freq *= 2.0 * M_PI / sr;
197 zr = cos(freq);
198 zi = -sin(freq);
199
200 /* |(a0 + a1*z + a2*z^2)/(1 + b1*z + b2*z^2)| */
201 return hypot(c->a0 + c->a1*zr + c->a2*(zr*zr-zi*zi), c->a1*zi + 2*c->a2*zr*zi) /
202 hypot(1 + c->b1*zr + c->b2*(zr*zr-zi*zi), c->b1*zi + 2*c->b2*zr*zi);
203}
204
205static int config_input(AVFilterLink *inlink)
206{
207 double i, j, k, g, t, a0, a1, a2, b1, b2, tau1, tau2, tau3;
208 double cutfreq, gain1kHz, gc, sr = inlink->sample_rate;
209 AVFilterContext *ctx = inlink->dst;
210 AudioEmphasisContext *s = ctx->priv;
211 BiquadCoeffs coeffs;
212
213 if (!s->w)
214 s->w = ff_get_audio_buffer(inlink, 4);
215 if (!s->w)
216 return AVERROR(ENOMEM);
217
218 switch (s->type) {
219 case 0: //"Columbia"
220 i = 100.;
221 j = 500.;
222 k = 1590.;
223 break;
224 case 1: //"EMI"
225 i = 70.;
226 j = 500.;
227 k = 2500.;
228 break;
229 case 2: //"BSI(78rpm)"
230 i = 50.;
231 j = 353.;
232 k = 3180.;
233 break;
234 case 3: //"RIAA"
235 default:
236 tau1 = 0.003180;
237 tau2 = 0.000318;
238 tau3 = 0.000075;
239 i = 1. / (2. * M_PI * tau1);
240 j = 1. / (2. * M_PI * tau2);
241 k = 1. / (2. * M_PI * tau3);
242 break;
243 case 4: //"CD Mastering"
244 tau1 = 0.000050;
245 tau2 = 0.000015;
246 tau3 = 0.0000001;// 1.6MHz out of audible range for null impact
247 i = 1. / (2. * M_PI * tau1);
248 j = 1. / (2. * M_PI * tau2);
249 k = 1. / (2. * M_PI * tau3);
250 break;
251 case 5: //"50µs FM (Europe)"
252 tau1 = 0.000050;
253 tau2 = tau1 / 20;// not used
254 tau3 = tau1 / 50;//
255 i = 1. / (2. * M_PI * tau1);
256 j = 1. / (2. * M_PI * tau2);
257 k = 1. / (2. * M_PI * tau3);
258 break;
259 case 6: //"75µs FM (US)"
260 tau1 = 0.000075;
261 tau2 = tau1 / 20;// not used
262 tau3 = tau1 / 50;//
263 i = 1. / (2. * M_PI * tau1);
264 j = 1. / (2. * M_PI * tau2);
265 k = 1. / (2. * M_PI * tau3);
266 break;
267 }
268
269 i *= 2 * M_PI;
270 j *= 2 * M_PI;
271 k *= 2 * M_PI;
272
273 t = 1. / sr;
274
275 //swap a1 b1, a2 b2
276 if (s->type == 7 || s->type == 8) {
277 double tau = (s->type == 7 ? 0.000050 : 0.000075);
278 double f = 1.0 / (2 * M_PI * tau);
279 double nyq = sr * 0.5;
280 double gain = sqrt(1.0 + nyq * nyq / (f * f)); // gain at Nyquist
281 double cfreq = sqrt((gain - 1.0) * f * f); // frequency
282 double q = 1.0;
283
284 if (s->type == 8)
285 q = pow((sr / 3269.0) + 19.5, -0.25); // somewhat poor curve-fit
286 if (s->type == 7)
287 q = pow((sr / 4750.0) + 19.5, -0.25);
288 if (s->mode == 0)
289 set_highshelf_rbj(&s->rc.r1, cfreq, q, 1. / gain, sr);
290 else
291 set_highshelf_rbj(&s->rc.r1, cfreq, q, gain, sr);
292 s->rc.use_brickw = 0;
293 } else {
294 s->rc.use_brickw = 1;
295 if (s->mode == 0) { // Reproduction
296 g = 1. / (4.+2.*i*t+2.*k*t+i*k*t*t);
297 a0 = (2.*t+j*t*t)*g;
298 a1 = (2.*j*t*t)*g;
299 a2 = (-2.*t+j*t*t)*g;
300 b1 = (-8.+2.*i*k*t*t)*g;
301 b2 = (4.-2.*i*t-2.*k*t+i*k*t*t)*g;
302 } else { // Production
303 g = 1. / (2.*t+j*t*t);
304 a0 = (4.+2.*i*t+2.*k*t+i*k*t*t)*g;
305 a1 = (-8.+2.*i*k*t*t)*g;
306 a2 = (4.-2.*i*t-2.*k*t+i*k*t*t)*g;
307 b1 = (2.*j*t*t)*g;
308 b2 = (-2.*t+j*t*t)*g;
309 }
310
311 coeffs.a0 = a0;
312 coeffs.a1 = a1;
313 coeffs.a2 = a2;
314 coeffs.b1 = b1;
315 coeffs.b2 = b2;
316
317 // the coeffs above give non-normalized value, so it should be normalized to produce 0dB at 1 kHz
318 // find actual gain
319 // Note: for FM emphasis, use 100 Hz for normalization instead
320 gain1kHz = freq_gain(&coeffs, 1000.0, sr);
321 // divide one filter's x[n-m] coefficients by that value
322 gc = 1.0 / gain1kHz;
323 s->rc.r1.a0 = coeffs.a0 * gc;
324 s->rc.r1.a1 = coeffs.a1 * gc;
325 s->rc.r1.a2 = coeffs.a2 * gc;
326 s->rc.r1.b1 = coeffs.b1;
327 s->rc.r1.b2 = coeffs.b2;
328 }
329
330 cutfreq = FFMIN(0.45 * sr, 21000.);
331 set_lp_rbj(&s->rc.brickw, cutfreq, 0.707, sr, 1.);
332
333 return 0;
334}
335
336static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
337 char *res, int res_len, int flags)
338{
339 int ret;
340
341 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
342 if (ret < 0)
343 return ret;
344
345 return config_input(ctx->inputs[0]);
346}
347
349{
350 AudioEmphasisContext *s = ctx->priv;
351
352 av_frame_free(&s->w);
353}
354
356 {
357 .name = "default",
358 .type = AVMEDIA_TYPE_AUDIO,
359 .config_props = config_input,
360 .filter_frame = filter_frame,
361 },
362};
363
365 .p.name = "aemphasis",
366 .p.description = NULL_IF_CONFIG_SMALL("Audio emphasis."),
367 .p.priv_class = &aemphasis_class,
370 .priv_size = sizeof(AudioEmphasisContext),
371 .uninit = uninit,
375 .process_command = process_command,
376};
static int fn filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int config_input(AVFilterLink *inlink)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
const FFFilter ff_af_aemphasis
static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static int config_input(AVFilterLink *inlink)
static const AVFilterPad avfilter_af_aemphasis_inputs[]
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static av_cold void uninit(AVFilterContext *ctx)
static double freq_gain(BiquadCoeffs *c, double freq, double sr)
static void biquad_process(BiquadCoeffs *bq, double *dst, const double *src, int nb_samples, double *w, double level_in, double level_out)
static void set_lp_rbj(BiquadCoeffs *bq, double fc, double q, double sr, double gain)
static void set_highshelf_rbj(BiquadCoeffs *bq, double freq, double q, double peak, double sr)
#define OFFSET(x)
static const AVOption aemphasis_options[]
#define A(x)
Definition vpx_arith.h:28
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:906
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
Main libavfilter public API header.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
static const uint16_t fc[]
Definition dcaenc.h:43
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition samplefmt.h:67
cl_device_type type
static const int16_t alpha[]
Definition ilbcdata.h:55
static av_cold void uninit(AVBitStreamFilterContext *ctx)
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition filters.h:763
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition filters.h:257
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_const double hypot(double x, double y)
Definition libm.h:368
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
#define M_PI
Definition mathematics.h:67
AVOptions.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition frame.h:815
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:533
AVOption.
Definition opt.h:428
BiquadCoeffs brickw
BiquadCoeffs r1
Used for passing data between threads.
Definition dsddec.c:71
AVFrame * out
Definition swscale.c:71
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
const char * g
Definition vf_curves.c:128
static double b1(void *priv, double x, double y)
Definition vf_xfade.c:2034
static double a0(void *priv, double x, double y)
Definition vf_xfade.c:2028
static double b2(void *priv, double x, double y)
Definition vf_xfade.c:2035
static double b0(void *priv, double x, double y)
Definition vf_xfade.c:2033
static double a2(void *priv, double x, double y)
Definition vf_xfade.c:2030
static double a1(void *priv, double x, double y)
Definition vf_xfade.c:2029
static double c[64]