-
Notifications
You must be signed in to change notification settings - Fork 364
Expand file tree
/
Copy patheq_iir_generic.c
More file actions
413 lines (374 loc) · 11.2 KB
/
Copy patheq_iir_generic.c
File metadata and controls
413 lines (374 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2017-2022 Intel Corporation. All rights reserved.
//
// Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
// Liam Girdwood <liam.r.girdwood@linux.intel.com>
// Keyon Jie <yang.jie@linux.intel.com>
#include "eq_iir.h"
#include <sof/audio/component.h>
#include <sof/audio/module_adapter/module/generic.h>
#include <sof/audio/data_blob.h>
#include <sof/audio/buffer.h>
#include <sof/audio/format.h>
#include <sof/audio/pipeline.h>
#include <sof/audio/ipc-config.h>
#include <sof/common.h>
#include <rtos/panic.h>
#include <sof/ipc/msg.h>
#include <rtos/alloc.h>
#include <rtos/init.h>
#include <sof/lib/uuid.h>
#include <sof/list.h>
#include <sof/math/iir_df1.h>
#include <sof/platform.h>
#include <rtos/string.h>
#include <sof/ut.h>
#include <sof/trace/trace.h>
#include <ipc/control.h>
#include <ipc/stream.h>
#include <ipc/topology.h>
#include <user/eq.h>
#include <user/trace.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
LOG_MODULE_DECLARE(eq_iir, CONFIG_SOF_LOG_LEVEL);
#if CONFIG_FORMAT_S16LE
void eq_iir_s16_default(struct processing_module *mod, struct input_stream_buffer *bsource,
struct output_stream_buffer *bsink, uint32_t frames)
{
struct comp_data *cd = module_get_private_data(mod);
struct audio_stream *source = bsource->data;
struct audio_stream *sink = bsink->data;
struct iir_state_df1 *filter;
int16_t *x0;
int16_t *y0;
int16_t *x;
int16_t *y;
int nmax;
int n1;
int n2;
int i;
int j;
int n;
const int nch = audio_stream_get_channels(source);
const int samples = frames * nch;
int processed = 0;
x = audio_stream_get_rptr(source);
y = audio_stream_get_wptr(sink);
while (processed < samples) {
nmax = samples - processed;
n1 = audio_stream_bytes_without_wrap(source, x) >> 1;
n2 = audio_stream_bytes_without_wrap(sink, y) >> 1;
n = MIN(n1, n2);
n = MIN(n, nmax);
for (i = 0; i < nch; i++) {
x0 = x + i;
y0 = y + i;
filter = &cd->iir[i];
for (j = 0; j < n; j += nch) {
*y0 = iir_df1_s16(filter, *x0);
x0 += nch;
y0 += nch;
}
}
processed += n;
x = audio_stream_wrap(source, x + n);
y = audio_stream_wrap(sink, y + n);
}
}
#endif /* CONFIG_FORMAT_S16LE */
#if CONFIG_FORMAT_S24LE
void eq_iir_s24_default(struct processing_module *mod, struct input_stream_buffer *bsource,
struct output_stream_buffer *bsink, uint32_t frames)
{
struct comp_data *cd = module_get_private_data(mod);
struct audio_stream *source = bsource->data;
struct audio_stream *sink = bsink->data;
struct iir_state_df1 *filter;
int32_t *x0;
int32_t *y0;
int32_t *x;
int32_t *y;
int nmax;
int n1;
int n2;
int i;
int j;
int n;
const int nch = audio_stream_get_channels(source);
const int samples = frames * nch;
int processed = 0;
x = audio_stream_get_rptr(source);
y = audio_stream_get_wptr(sink);
while (processed < samples) {
nmax = samples - processed;
n1 = audio_stream_bytes_without_wrap(source, x) >> 2;
n2 = audio_stream_bytes_without_wrap(sink, y) >> 2;
n = MIN(n1, n2);
n = MIN(n, nmax);
for (i = 0; i < nch; i++) {
x0 = x + i;
y0 = y + i;
filter = &cd->iir[i];
for (j = 0; j < n; j += nch) {
*y0 = iir_df1_s24(filter, *x0);
x0 += nch;
y0 += nch;
}
}
processed += n;
x = audio_stream_wrap(source, x + n);
y = audio_stream_wrap(sink, y + n);
}
}
#endif /* CONFIG_FORMAT_S24LE */
#if CONFIG_FORMAT_S32LE
void eq_iir_s32_default(struct processing_module *mod, struct input_stream_buffer *bsource,
struct output_stream_buffer *bsink, uint32_t frames)
{
struct comp_data *cd = module_get_private_data(mod);
struct audio_stream *source = bsource->data;
struct audio_stream *sink = bsink->data;
struct iir_state_df1 *filter;
int32_t *x0;
int32_t *y0;
int32_t *x;
int32_t *y;
int nmax;
int n1;
int n2;
int i;
int j;
int n;
const int nch = audio_stream_get_channels(source);
const int samples = frames * nch;
int processed = 0;
x = audio_stream_get_rptr(source);
y = audio_stream_get_wptr(sink);
while (processed < samples) {
nmax = samples - processed;
n1 = audio_stream_bytes_without_wrap(source, x) >> 2;
n2 = audio_stream_bytes_without_wrap(sink, y) >> 2;
n = MIN(n1, n2);
n = MIN(n, nmax);
for (i = 0; i < nch; i++) {
x0 = x + i;
y0 = y + i;
filter = &cd->iir[i];
for (j = 0; j < n; j += nch) {
*y0 = iir_df1(filter, *x0);
x0 += nch;
y0 += nch;
}
}
processed += n;
x = audio_stream_wrap(source, x + n);
y = audio_stream_wrap(sink, y + n);
}
}
#endif /* CONFIG_FORMAT_S32LE */
static int eq_iir_blob_words_max(struct comp_dev *dev,
const struct sof_eq_iir_config *config,
size_t blob_size,
uint32_t *coef_words_max)
{
size_t payload_bytes;
/* Compute the size of the coefficient area in int32_t words from the
* framework-reported blob size. The blob layout is:
* sizeof(*config) header bytes
* channels_in_config int32_t assign_response[]
* coefficient data[]
* channels_in_config is bounded above, so the multiply fits in size_t.
* The blob's self-declared config->size is cross-checked against the
* authoritative blob_size so all later parsing stays within the buffer.
*/
if (blob_size < sizeof(*config) || config->size != blob_size) {
comp_err(dev, "blob size %zu / header size %u mismatch or too small",
blob_size, config->size);
return -EINVAL;
}
payload_bytes = blob_size - sizeof(*config);
if (payload_bytes % sizeof(int32_t) ||
payload_bytes < (size_t)config->channels_in_config * sizeof(int32_t)) {
comp_err(dev, "blob size %zu misaligned or too small", blob_size);
return -EINVAL;
}
*coef_words_max = payload_bytes / sizeof(int32_t) - config->channels_in_config;
return 0;
}
static int eq_iir_init_response(struct comp_dev *dev, int idx,
int32_t *coef_data, uint32_t coef_words_max,
uint32_t *j, struct sof_eq_iir_header **eq_out)
{
struct sof_eq_iir_header *eq;
uint32_t header_end = *j + SOF_EQ_IIR_NHEADER;
uint32_t section_end;
/* Header must fit before reading num_sections */
if (header_end > coef_words_max) {
comp_err(dev, "response %d header out of bounds", idx);
return -EINVAL;
}
eq = (struct sof_eq_iir_header *)&coef_data[*j];
/* Bound num_sections so the multiply cannot overflow and the section
* data stays within the blob.
*/
section_end = header_end + (uint32_t)SOF_EQ_IIR_NBIQUAD * eq->num_sections;
if (eq->num_sections > SOF_EQ_IIR_BIQUADS_MAX || section_end > coef_words_max) {
comp_err(dev, "response %d num_sections %u out of bounds",
idx, eq->num_sections);
return -EINVAL;
}
*eq_out = eq;
*j = section_end;
return 0;
}
static int eq_iir_init_coef(struct processing_module *mod, int nch)
{
struct comp_data *cd = module_get_private_data(mod);
struct sof_eq_iir_config *config = cd->config;
struct iir_state_df1 *iir = cd->iir;
struct sof_eq_iir_header *lookup[SOF_EQ_IIR_MAX_RESPONSES];
struct sof_eq_iir_header *eq;
uint32_t coef_words_max;
int32_t *assign_response;
int32_t *coef_data;
int size_sum = 0;
int resp = 0;
int i;
uint32_t j;
int s;
int ret;
comp_info(mod->dev, "%u responses, %u channels, stream %d channels",
config->number_of_responses, config->channels_in_config, nch);
/* Sanity checks */
if (nch > PLATFORM_MAX_CHANNELS ||
config->channels_in_config > PLATFORM_MAX_CHANNELS ||
!config->channels_in_config) {
comp_err(mod->dev, "invalid channels count");
return -EINVAL;
}
if (config->number_of_responses > SOF_EQ_IIR_MAX_RESPONSES) {
comp_err(mod->dev, "# of resp exceeds max");
return -EINVAL;
}
ret = eq_iir_blob_words_max(mod->dev, config, cd->config_size, &coef_words_max);
if (ret < 0)
return ret;
/* Collect index of response start positions in all_coefficients[] */
j = 0;
assign_response = ASSUME_ALIGNED(&config->data[0], 4);
coef_data = ASSUME_ALIGNED(&config->data[config->channels_in_config], 4);
for (i = 0; i < SOF_EQ_IIR_MAX_RESPONSES; i++) {
if (i < config->number_of_responses) {
ret = eq_iir_init_response(mod->dev, i, coef_data,
coef_words_max, &j, &eq);
if (ret < 0)
return ret;
lookup[i] = eq;
} else {
lookup[i] = NULL;
}
}
/* Initialize 1st phase */
for (i = 0; i < nch; i++) {
/* Check for not reading past blob response to channel assign
* map. The previous channel response is assigned for any
* additional channels in the stream. It allows to use single
* channel configuration to setup multi channel equalization
* with the same response.
*/
if (i < config->channels_in_config)
resp = assign_response[i];
if (resp < 0) {
/* Initialize EQ channel to bypass and continue with
* next channel response.
*/
comp_info(mod->dev, "ch %d is set to bypass", i);
iir_reset_df1(&iir[i]);
continue;
}
if (resp >= config->number_of_responses) {
comp_err(mod->dev, "requested response %d exceeds defined",
resp);
return -EINVAL;
}
/* Initialize EQ coefficients */
eq = lookup[resp];
s = iir_delay_size_df1(eq);
if (s > 0) {
size_sum += s;
} else {
comp_err(mod->dev, "sections count %d exceeds max",
eq->num_sections);
return -EINVAL;
}
iir_init_coef_df1(&iir[i], eq);
comp_info(mod->dev, "ch %d is set to response %d", i, resp);
}
return size_sum;
}
static void eq_iir_init_delay(struct iir_state_df1 *iir,
int32_t *delay_start, int nch)
{
int32_t *delay = delay_start;
int i;
/* Initialize second phase to set EQ delay lines pointers. A
* bypass mode filter is indicated by biquads count of zero.
*/
for (i = 0; i < nch; i++) {
if (iir[i].biquads > 0)
iir_init_delay_df1(&iir[i], &delay);
}
}
void eq_iir_free_delaylines(struct processing_module *mod)
{
struct comp_data *cd = module_get_private_data(mod);
struct iir_state_df1 *iir = cd->iir;
int i = 0;
/* Free the common buffer for all EQs and point then
* each IIR channel delay line to NULL.
*/
mod_free(mod, cd->iir_delay);
cd->iir_delay = NULL;
cd->iir_delay_size = 0;
for (i = 0; i < PLATFORM_MAX_CHANNELS; i++)
iir[i].delay = NULL;
}
void eq_iir_pass(struct processing_module *mod, struct input_stream_buffer *bsource,
struct output_stream_buffer *bsink, uint32_t frames)
{
struct audio_stream *source = bsource->data;
struct audio_stream *sink = bsink->data;
audio_stream_copy(source, 0, sink, 0, frames * audio_stream_get_channels(source));
}
int eq_iir_setup(struct processing_module *mod, int nch)
{
struct comp_data *cd = module_get_private_data(mod);
int delay_size;
/* Free existing IIR channels data if it was allocated */
eq_iir_free_delaylines(mod);
/* Set coefficients for each channel EQ from coefficient blob.
* eq_iir_init_coef() / eq_iir_blob_words_max() perform all blob size
* sanity checks, including config->size vs cd->config_size.
*/
delay_size = eq_iir_init_coef(mod, nch);
if (delay_size < 0)
return delay_size; /* Contains error code */
/* If all channels were set to bypass there's no need to
* allocate delay. Just return with success.
*/
if (!delay_size)
return 0;
/* Allocate all IIR channels data in a big chunk and clear it */
cd->iir_delay = mod_zalloc(mod, delay_size);
if (!cd->iir_delay) {
comp_err(mod->dev, "delay allocation fail");
return -ENOMEM;
}
cd->iir_delay_size = delay_size;
/* Assign delay line to each channel EQ */
eq_iir_init_delay(cd->iir, cd->iir_delay, nch);
return 0;
}