1 /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6 /* Copyright (C) 2011 Google Inc. All rights reserved.
7 * Use of this source code is governed by a BSD-style license that can be
8 * found in the LICENSE.WEBKIT file.
9 */
10
11 #include <assert.h>
12 #include <stdlib.h>
13
14 #include "drc.h"
15 #include "drc_math.h"
16
17 static void set_default_parameters(struct drc *drc);
18 static void init_data_buffer(struct drc *drc);
19 static void init_emphasis_eq(struct drc *drc);
20 static void init_crossover(struct drc *drc);
21 static void init_kernel(struct drc *drc);
22 static void free_data_buffer(struct drc *drc);
23 static void free_emphasis_eq(struct drc *drc);
24 static void free_kernel(struct drc *drc);
25
drc_new(float sample_rate)26 struct drc *drc_new(float sample_rate)
27 {
28 struct drc *drc = (struct drc *)calloc(1, sizeof(struct drc));
29 drc->sample_rate = sample_rate;
30 set_default_parameters(drc);
31 return drc;
32 }
33
drc_init(struct drc * drc)34 void drc_init(struct drc *drc)
35 {
36 init_data_buffer(drc);
37 init_emphasis_eq(drc);
38 init_crossover(drc);
39 init_kernel(drc);
40 }
41
drc_free(struct drc * drc)42 void drc_free(struct drc *drc)
43 {
44 free_kernel(drc);
45 free_emphasis_eq(drc);
46 free_data_buffer(drc);
47 free(drc);
48 }
49
50 /* Allocates temporary buffers used during drc_process(). */
init_data_buffer(struct drc * drc)51 static void init_data_buffer(struct drc *drc)
52 {
53 int i;
54 size_t size = sizeof(float) * DRC_PROCESS_MAX_FRAMES;
55
56 for (i = 0; i < DRC_NUM_CHANNELS; i++) {
57 drc->data1[i] = (float *)calloc(1, size);
58 drc->data2[i] = (float *)calloc(1, size);
59 }
60 }
61
62 /* Frees temporary buffers */
free_data_buffer(struct drc * drc)63 static void free_data_buffer(struct drc *drc)
64 {
65 int i;
66
67 for (i = 0; i < DRC_NUM_CHANNELS; i++) {
68 free(drc->data1[i]);
69 free(drc->data2[i]);
70 }
71 }
72
drc_set_param(struct drc * drc,int index,unsigned paramID,float value)73 void drc_set_param(struct drc *drc, int index, unsigned paramID, float value)
74 {
75 assert(paramID < PARAM_LAST);
76 if (paramID < PARAM_LAST)
77 drc->parameters[index][paramID] = value;
78 }
79
drc_get_param(struct drc * drc,int index,unsigned paramID)80 static float drc_get_param(struct drc *drc, int index, unsigned paramID)
81 {
82 assert(paramID < PARAM_LAST);
83 return drc->parameters[index][paramID];
84 }
85
86 /* Initializes parameters to default values. */
set_default_parameters(struct drc * drc)87 static void set_default_parameters(struct drc *drc)
88 {
89 float nyquist = drc->sample_rate / 2;
90 int i;
91
92 for (i = 0; i < DRC_NUM_KERNELS; i++) {
93 float *param = drc->parameters[i];
94 param[PARAM_THRESHOLD] = -24; /* dB */
95 param[PARAM_KNEE] = 30; /* dB */
96 param[PARAM_RATIO] = 12; /* unit-less */
97 param[PARAM_ATTACK] = 0.003f; /* seconds */
98 param[PARAM_RELEASE] = 0.250f; /* seconds */
99 param[PARAM_PRE_DELAY] = DRC_DEFAULT_PRE_DELAY; /* seconds */
100
101 /* Release zone values 0 -> 1. */
102 param[PARAM_RELEASE_ZONE1] = 0.09f;
103 param[PARAM_RELEASE_ZONE2] = 0.16f;
104 param[PARAM_RELEASE_ZONE3] = 0.42f;
105 param[PARAM_RELEASE_ZONE4] = 0.98f;
106
107 /* This is effectively a master volume on the compressed
108 * signal */
109 param[PARAM_POST_GAIN] = 0; /* dB */
110 param[PARAM_ENABLED] = 0;
111 }
112
113 drc->parameters[0][PARAM_CROSSOVER_LOWER_FREQ] = 0;
114 drc->parameters[1][PARAM_CROSSOVER_LOWER_FREQ] = 200 / nyquist;
115 drc->parameters[2][PARAM_CROSSOVER_LOWER_FREQ] = 2000 / nyquist;
116
117 /* These parameters has only one copy */
118 drc->parameters[0][PARAM_FILTER_STAGE_GAIN] = 4.4f; /* dB */
119 drc->parameters[0][PARAM_FILTER_STAGE_RATIO] = 2;
120 drc->parameters[0][PARAM_FILTER_ANCHOR] = 15000 / nyquist;
121 }
122
123 /* Finds the zero and pole for one stage of the emphasis filter */
emphasis_stage_roots(float gain,float normalized_frequency,float * zero,float * pole)124 static void emphasis_stage_roots(float gain, float normalized_frequency,
125 float *zero, float *pole)
126 {
127 float gk = 1 - gain / 20;
128 float f1 = normalized_frequency * gk;
129 float f2 = normalized_frequency / gk;
130 *zero = expf(-f1 * PI_FLOAT);
131 *pole = expf(-f2 * PI_FLOAT);
132 }
133
134 /* Calculates the biquad coefficients for two emphasis stages. */
emphasis_stage_pair_biquads(float gain,float f1,float f2,struct biquad * emphasis,struct biquad * deemphasis)135 static void emphasis_stage_pair_biquads(float gain, float f1, float f2,
136 struct biquad *emphasis,
137 struct biquad *deemphasis)
138 {
139 float z1, p1;
140 float z2, p2;
141
142 emphasis_stage_roots(gain, f1, &z1, &p1);
143 emphasis_stage_roots(gain, f2, &z2, &p2);
144
145 float b0 = 1;
146 float b1 = -(z1 + z2);
147 float b2 = z1 * z2;
148 float a0 = 1;
149 float a1 = -(p1 + p2);
150 float a2 = p1 * p2;
151
152 /* Gain compensation to make 0dB @ 0Hz */
153 float alpha = (a0 + a1 + a2) / (b0 + b1 + b2);
154
155 emphasis->b0 = b0 * alpha;
156 emphasis->b1 = b1 * alpha;
157 emphasis->b2 = b2 * alpha;
158 emphasis->a1 = a1;
159 emphasis->a2 = a2;
160
161 float beta = (b0 + b1 + b2) / (a0 + a1 + a2);
162
163 deemphasis->b0 = a0 * beta;
164 deemphasis->b1 = a1 * beta;
165 deemphasis->b2 = a2 * beta;
166 deemphasis->a1 = b1;
167 deemphasis->a2 = b2;
168 }
169
170 /* Initializes the emphasis and deemphasis filter */
init_emphasis_eq(struct drc * drc)171 static void init_emphasis_eq(struct drc *drc)
172 {
173 struct biquad e = { 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f };
174 struct biquad d = { 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f };
175 int i, j;
176
177 float stage_gain = drc_get_param(drc, 0, PARAM_FILTER_STAGE_GAIN);
178 float stage_ratio = drc_get_param(drc, 0, PARAM_FILTER_STAGE_RATIO);
179 float anchor_freq = drc_get_param(drc, 0, PARAM_FILTER_ANCHOR);
180
181 drc->emphasis_eq = eq2_new();
182 drc->deemphasis_eq = eq2_new();
183
184 for (i = 0; i < 2; i++) {
185 emphasis_stage_pair_biquads(stage_gain, anchor_freq,
186 anchor_freq / stage_ratio, &e, &d);
187 for (j = 0; j < 2; j++) {
188 eq2_append_biquad_direct(drc->emphasis_eq, j, &e);
189 eq2_append_biquad_direct(drc->deemphasis_eq, j, &d);
190 }
191 anchor_freq /= (stage_ratio * stage_ratio);
192 }
193 }
194
195 /* Frees the emphasis and deemphasis filter */
free_emphasis_eq(struct drc * drc)196 static void free_emphasis_eq(struct drc *drc)
197 {
198 eq2_free(drc->emphasis_eq);
199 eq2_free(drc->deemphasis_eq);
200 }
201
202 /* Initializes the crossover filter */
init_crossover(struct drc * drc)203 static void init_crossover(struct drc *drc)
204 {
205 float freq1 = drc->parameters[1][PARAM_CROSSOVER_LOWER_FREQ];
206 float freq2 = drc->parameters[2][PARAM_CROSSOVER_LOWER_FREQ];
207
208 crossover2_init(&drc->xo2, freq1, freq2);
209 }
210
211 /* Initializes the compressor kernels */
init_kernel(struct drc * drc)212 static void init_kernel(struct drc *drc)
213 {
214 int i;
215
216 for (i = 0; i < DRC_NUM_KERNELS; i++) {
217 dk_init(&drc->kernel[i], drc->sample_rate);
218
219 float db_threshold = drc_get_param(drc, i, PARAM_THRESHOLD);
220 float db_knee = drc_get_param(drc, i, PARAM_KNEE);
221 float ratio = drc_get_param(drc, i, PARAM_RATIO);
222 float attack_time = drc_get_param(drc, i, PARAM_ATTACK);
223 float release_time = drc_get_param(drc, i, PARAM_RELEASE);
224 float pre_delay_time = drc_get_param(drc, i, PARAM_PRE_DELAY);
225 float releaseZone1 = drc_get_param(drc, i, PARAM_RELEASE_ZONE1);
226 float releaseZone2 = drc_get_param(drc, i, PARAM_RELEASE_ZONE2);
227 float releaseZone3 = drc_get_param(drc, i, PARAM_RELEASE_ZONE3);
228 float releaseZone4 = drc_get_param(drc, i, PARAM_RELEASE_ZONE4);
229 float db_post_gain = drc_get_param(drc, i, PARAM_POST_GAIN);
230 int enabled = drc_get_param(drc, i, PARAM_ENABLED);
231
232 dk_set_parameters(&drc->kernel[i], db_threshold, db_knee, ratio,
233 attack_time, release_time, pre_delay_time,
234 db_post_gain, releaseZone1, releaseZone2,
235 releaseZone3, releaseZone4);
236
237 dk_set_enabled(&drc->kernel[i], enabled);
238 }
239 }
240
241 /* Frees the compressor kernels */
free_kernel(struct drc * drc)242 static void free_kernel(struct drc *drc)
243 {
244 int i;
245 for (i = 0; i < DRC_NUM_KERNELS; i++)
246 dk_free(&drc->kernel[i]);
247 }
248
249 // Note gcc 4.9+ with -O2 on aarch64 produces vectorized version of C
250 // that is comparable performance, but twice as large. -O1 and -Os produce
251 // small but slower code (4x slower than Neon).
252 #if defined(__aarch64__)
sum3(float * data,const float * data1,const float * data2,int n)253 static void sum3(float *data, const float *data1, const float *data2, int n)
254 {
255 int count = n / 4;
256 int i;
257
258 if (count) {
259 // clang-format off
260 __asm__ __volatile(
261 "1: \n"
262 "ld1 {v0.4s}, [%[data1]], #16 \n"
263 "ld1 {v1.4s}, [%[data2]], #16 \n"
264 "ld1 {v2.4s}, [%[data]] \n"
265 "fadd v0.4s, v0.4s, v1.4s \n"
266 "fadd v0.4s, v0.4s, v2.4s \n"
267 "st1 {v0.4s}, [%[data]], #16 \n"
268 "subs %w[count], %w[count], #1 \n"
269 "b.ne 1b \n"
270 : /* output */
271 [data]"+r"(data),
272 [data1]"+r"(data1),
273 [data2]"+r"(data2),
274 [count]"+r"(count)
275 : /* input */
276 : /* clobber */
277 "v0", "v1", "v2", "memory", "cc");
278 // clang-format on
279 }
280
281 n &= 3;
282 for (i = 0; i < n; i++)
283 data[i] += data1[i] + data2[i];
284 }
285 #elif defined(__ARM_NEON__)
sum3(float * data,const float * data1,const float * data2,int n)286 static void sum3(float *data, const float *data1, const float *data2, int n)
287 {
288 int count = n / 4;
289 int i;
290
291 if (count) {
292 // clang-format off
293 __asm__ __volatile(
294 "1: \n"
295 "vld1.32 {q0}, [%[data1]]! \n"
296 "vld1.32 {q1}, [%[data2]]! \n"
297 "vld1.32 {q2}, [%[data]] \n"
298 "vadd.f32 q0, q0, q1 \n"
299 "vadd.f32 q0, q0, q2 \n"
300 "vst1.32 {q0}, [%[data]]! \n"
301 "subs %[count], #1 \n"
302 "bne 1b \n"
303 : /* output */
304 [data]"+r"(data),
305 [data1]"+r"(data1),
306 [data2]"+r"(data2),
307 [count]"+r"(count)
308 : /* input */
309 : /* clobber */
310 "q0", "q1", "q2", "memory", "cc");
311 // clang-format on
312 }
313
314 n &= 3;
315 for (i = 0; i < n; i++)
316 data[i] += data1[i] + data2[i];
317 }
318 #elif defined(__SSE3__)
319 #include <emmintrin.h>
sum3(float * data,const float * data1,const float * data2,int n)320 static void sum3(float *data, const float *data1, const float *data2, int n)
321 {
322 __m128 x, y, z;
323 int count = n / 4;
324 int i;
325
326 if (count) {
327 // clang-format off
328 __asm__ __volatile(
329 "1: \n"
330 "lddqu (%[data1]), %[x] \n"
331 "lddqu (%[data2]), %[y] \n"
332 "lddqu (%[data]), %[z] \n"
333 "addps %[x], %[y] \n"
334 "addps %[y], %[z] \n"
335 "movdqu %[z], (%[data]) \n"
336 "add $16, %[data1] \n"
337 "add $16, %[data2] \n"
338 "add $16, %[data] \n"
339 "sub $1, %[count] \n"
340 "jne 1b \n"
341 : /* output */
342 [data]"+r"(data),
343 [data1]"+r"(data1),
344 [data2]"+r"(data2),
345 [count]"+r"(count),
346 [x]"=x"(x),
347 [y]"=x"(y),
348 [z]"=x"(z)
349 : /* input */
350 : /* clobber */
351 "memory", "cc");
352 // clang-format on
353 }
354
355 n &= 3;
356 for (i = 0; i < n; i++)
357 data[i] += data1[i] + data2[i];
358 }
359 #else
sum3(float * data,const float * data1,const float * data2,int n)360 static void sum3(float *data, const float *data1, const float *data2, int n)
361 {
362 int i;
363 for (i = 0; i < n; i++)
364 data[i] += data1[i] + data2[i];
365 }
366 #endif
367
drc_process(struct drc * drc,float ** data,int frames)368 void drc_process(struct drc *drc, float **data, int frames)
369 {
370 int i;
371 float **data1 = drc->data1;
372 float **data2 = drc->data2;
373
374 /* Apply pre-emphasis filter if it is not disabled. */
375 if (!drc->emphasis_disabled)
376 eq2_process(drc->emphasis_eq, data[0], data[1], frames);
377
378 /* Crossover */
379 crossover2_process(&drc->xo2, frames, data[0], data[1], data1[0],
380 data1[1], data2[0], data2[1]);
381
382 /* Apply compression to each band of the signal. The processing is
383 * performed in place.
384 */
385 dk_process(&drc->kernel[0], data, frames);
386 dk_process(&drc->kernel[1], data1, frames);
387 dk_process(&drc->kernel[2], data2, frames);
388
389 /* Sum the three bands of signal */
390 for (i = 0; i < DRC_NUM_CHANNELS; i++)
391 sum3(data[i], data1[i], data2[i], frames);
392
393 /* Apply de-emphasis filter if emphasis is not disabled. */
394 if (!drc->emphasis_disabled)
395 eq2_process(drc->deemphasis_eq, data[0], data[1], frames);
396 }
397