1 // SPDX-License-Identifier: BSD-3-Clause
2 //
3 // Copyright(c) 2017-2022 Intel Corporation. All rights reserved.
4 //
5 // Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
6 // Liam Girdwood <liam.r.girdwood@linux.intel.com>
7 // Keyon Jie <yang.jie@linux.intel.com>
8
9 #include <sof/audio/component.h>
10 #include <sof/audio/module_adapter/module/generic.h>
11 #include <sof/audio/data_blob.h>
12 #include <sof/audio/buffer.h>
13 #include <sof/audio/eq_iir/eq_iir.h>
14 #include <sof/audio/format.h>
15 #include <sof/audio/pipeline.h>
16 #include <sof/audio/ipc-config.h>
17 #include <sof/common.h>
18 #include <rtos/panic.h>
19 #include <sof/ipc/msg.h>
20 #include <rtos/alloc.h>
21 #include <rtos/init.h>
22 #include <sof/lib/memory.h>
23 #include <sof/lib/uuid.h>
24 #include <sof/list.h>
25 #include <sof/math/iir_df1.h>
26 #include <sof/platform.h>
27 #include <rtos/string.h>
28 #include <sof/ut.h>
29 #include <sof/trace/trace.h>
30 #include <ipc/control.h>
31 #include <ipc/stream.h>
32 #include <ipc/topology.h>
33 #include <user/eq.h>
34 #include <user/trace.h>
35 #include <errno.h>
36 #include <stddef.h>
37 #include <stdint.h>
38
39 LOG_MODULE_REGISTER(eq_iir, CONFIG_SOF_LOG_LEVEL);
40
41 /* 5150c0e6-27f9-4ec8-8351-c705b642d12f */
42 DECLARE_SOF_RT_UUID("eq-iir", eq_iir_uuid, 0x5150c0e6, 0x27f9, 0x4ec8,
43 0x83, 0x51, 0xc7, 0x05, 0xb6, 0x42, 0xd1, 0x2f);
44
45 DECLARE_TR_CTX(eq_iir_tr, SOF_UUID(eq_iir_uuid), LOG_LEVEL_INFO);
46
47 /* IIR component private data */
48 struct comp_data {
49 struct iir_state_df1 iir[PLATFORM_MAX_CHANNELS]; /**< filters state */
50 struct comp_data_blob_handler *model_handler;
51 struct sof_eq_iir_config *config;
52 int32_t *iir_delay; /**< pointer to allocated RAM */
53 size_t iir_delay_size; /**< allocated size */
54 eq_iir_func eq_iir_func; /**< processing function */
55 };
56
57 #if CONFIG_FORMAT_S16LE
58
59 /*
60 * EQ IIR algorithm code
61 */
62
eq_iir_s16_default(struct processing_module * mod,struct input_stream_buffer * bsource,struct output_stream_buffer * bsink,uint32_t frames)63 static void eq_iir_s16_default(struct processing_module *mod, struct input_stream_buffer *bsource,
64 struct output_stream_buffer *bsink, uint32_t frames)
65 {
66 struct comp_data *cd = module_get_private_data(mod);
67 struct audio_stream __sparse_cache *source = bsource->data;
68 struct audio_stream __sparse_cache *sink = bsink->data;
69 struct iir_state_df1 *filter;
70 int16_t *x0;
71 int16_t *y0;
72 int16_t *x;
73 int16_t *y;
74 int nmax;
75 int n1;
76 int n2;
77 int i;
78 int j;
79 int n;
80 const int nch = source->channels;
81 const int samples = frames * nch;
82 int processed = 0;
83
84 x = source->r_ptr;
85 y = sink->w_ptr;
86 while (processed < samples) {
87 nmax = samples - processed;
88 n1 = audio_stream_bytes_without_wrap(source, x) >> 1;
89 n2 = audio_stream_bytes_without_wrap(sink, y) >> 1;
90 n = MIN(n1, n2);
91 n = MIN(n, nmax);
92 for (i = 0; i < nch; i++) {
93 x0 = x + i;
94 y0 = y + i;
95 filter = &cd->iir[i];
96 for (j = 0; j < n; j += nch) {
97 *y0 = iir_df1_s16(filter, *x0);
98 x0 += nch;
99 y0 += nch;
100 }
101 }
102 processed += n;
103 x = audio_stream_wrap(source, x + n);
104 y = audio_stream_wrap(sink, y + n);
105 }
106 }
107 #endif /* CONFIG_FORMAT_S16LE */
108
109 #if CONFIG_FORMAT_S24LE
110
eq_iir_s24_default(struct processing_module * mod,struct input_stream_buffer * bsource,struct output_stream_buffer * bsink,uint32_t frames)111 static void eq_iir_s24_default(struct processing_module *mod, struct input_stream_buffer *bsource,
112 struct output_stream_buffer *bsink, uint32_t frames)
113 {
114 struct comp_data *cd = module_get_private_data(mod);
115 struct audio_stream __sparse_cache *source = bsource->data;
116 struct audio_stream __sparse_cache *sink = bsink->data;
117 struct iir_state_df1 *filter;
118 int32_t *x0;
119 int32_t *y0;
120 int32_t *x;
121 int32_t *y;
122 int nmax;
123 int n1;
124 int n2;
125 int i;
126 int j;
127 int n;
128 const int nch = source->channels;
129 const int samples = frames * nch;
130 int processed = 0;
131
132 x = source->r_ptr;
133 y = sink->w_ptr;
134 while (processed < samples) {
135 nmax = samples - processed;
136 n1 = audio_stream_bytes_without_wrap(source, x) >> 2;
137 n2 = audio_stream_bytes_without_wrap(sink, y) >> 2;
138 n = MIN(n1, n2);
139 n = MIN(n, nmax);
140 for (i = 0; i < nch; i++) {
141 x0 = x + i;
142 y0 = y + i;
143 filter = &cd->iir[i];
144 for (j = 0; j < n; j += nch) {
145 *y0 = iir_df1_s24(filter, *x0);
146 x0 += nch;
147 y0 += nch;
148 }
149 }
150 processed += n;
151 x = audio_stream_wrap(source, x + n);
152 y = audio_stream_wrap(sink, y + n);
153 }
154 }
155 #endif /* CONFIG_FORMAT_S24LE */
156
157 #if CONFIG_FORMAT_S32LE
158
eq_iir_s32_default(struct processing_module * mod,struct input_stream_buffer * bsource,struct output_stream_buffer * bsink,uint32_t frames)159 static void eq_iir_s32_default(struct processing_module *mod, struct input_stream_buffer *bsource,
160 struct output_stream_buffer *bsink, uint32_t frames)
161 {
162 struct comp_data *cd = module_get_private_data(mod);
163 struct audio_stream __sparse_cache *source = bsource->data;
164 struct audio_stream __sparse_cache *sink = bsink->data;
165 struct iir_state_df1 *filter;
166 int32_t *x0;
167 int32_t *y0;
168 int32_t *x;
169 int32_t *y;
170 int nmax;
171 int n1;
172 int n2;
173 int i;
174 int j;
175 int n;
176 const int nch = source->channels;
177 const int samples = frames * nch;
178 int processed = 0;
179
180 x = source->r_ptr;
181 y = sink->w_ptr;
182 while (processed < samples) {
183 nmax = samples - processed;
184 n1 = audio_stream_bytes_without_wrap(source, x) >> 2;
185 n2 = audio_stream_bytes_without_wrap(sink, y) >> 2;
186 n = MIN(n1, n2);
187 n = MIN(n, nmax);
188 for (i = 0; i < nch; i++) {
189 x0 = x + i;
190 y0 = y + i;
191 filter = &cd->iir[i];
192 for (j = 0; j < n; j += nch) {
193 *y0 = iir_df1(filter, *x0);
194 x0 += nch;
195 y0 += nch;
196 }
197 }
198 processed += n;
199 x = audio_stream_wrap(source, x + n);
200 y = audio_stream_wrap(sink, y + n);
201 }
202 }
203 #endif /* CONFIG_FORMAT_S32LE */
204
205 #if CONFIG_IPC_MAJOR_3
206 #if CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S16LE
eq_iir_s32_16_default(struct processing_module * mod,struct input_stream_buffer * bsource,struct output_stream_buffer * bsink,uint32_t frames)207 static void eq_iir_s32_16_default(struct processing_module *mod,
208 struct input_stream_buffer *bsource,
209 struct output_stream_buffer *bsink, uint32_t frames)
210 {
211 struct comp_data *cd = module_get_private_data(mod);
212 struct audio_stream __sparse_cache *source = bsource->data;
213 struct audio_stream __sparse_cache *sink = bsink->data;
214 struct iir_state_df1 *filter;
215 int32_t *x0;
216 int16_t *y0;
217 int32_t *x;
218 int16_t *y;
219 int nmax;
220 int n1;
221 int n2;
222 int i;
223 int j;
224 int n;
225 const int nch = source->channels;
226 const int samples = frames * nch;
227 int processed = 0;
228
229 x = source->r_ptr;
230 y = sink->w_ptr;
231 while (processed < samples) {
232 nmax = samples - processed;
233 n1 = audio_stream_bytes_without_wrap(source, x) >> 2; /* divide 4 */
234 n2 = audio_stream_bytes_without_wrap(sink, y) >> 1; /* divide 2 */
235 n = MIN(n1, n2);
236 n = MIN(n, nmax);
237 for (i = 0; i < nch; i++) {
238 x0 = x + i;
239 y0 = y + i;
240 filter = &cd->iir[i];
241 for (j = 0; j < n; j += nch) {
242 *y0 = iir_df1_s32_s16(filter, *x0);
243 x0 += nch;
244 y0 += nch;
245 }
246 }
247 processed += n;
248 x = audio_stream_wrap(source, x + n);
249 y = audio_stream_wrap(sink, y + n);
250 }
251 }
252 #endif /* CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S16LE */
253
254 #if CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S24LE
eq_iir_s32_24_default(struct processing_module * mod,struct input_stream_buffer * bsource,struct output_stream_buffer * bsink,uint32_t frames)255 static void eq_iir_s32_24_default(struct processing_module *mod,
256 struct input_stream_buffer *bsource,
257 struct output_stream_buffer *bsink, uint32_t frames)
258 {
259 struct comp_data *cd = module_get_private_data(mod);
260 struct audio_stream __sparse_cache *source = bsource->data;
261 struct audio_stream __sparse_cache *sink = bsink->data;
262 struct iir_state_df1 *filter;
263 int32_t *x0;
264 int32_t *y0;
265 int32_t *x;
266 int32_t *y;
267 int nmax;
268 int n1;
269 int n2;
270 int i;
271 int j;
272 int n;
273 const int nch = source->channels;
274 const int samples = frames * nch;
275 int processed = 0;
276
277 x = source->r_ptr;
278 y = sink->w_ptr;
279 while (processed < samples) {
280 nmax = samples - processed;
281 n1 = audio_stream_bytes_without_wrap(source, x) >> 2;
282 n2 = audio_stream_bytes_without_wrap(sink, y) >> 2;
283 n = MIN(n1, n2);
284 n = MIN(n, nmax);
285 for (i = 0; i < nch; i++) {
286 x0 = x + i;
287 y0 = y + i;
288 filter = &cd->iir[i];
289 for (j = 0; j < n; j += nch) {
290 *y0 = iir_df1_s32_s24(filter, *x0);
291 x0 += nch;
292 y0 += nch;
293 }
294 }
295 processed += n;
296 x = audio_stream_wrap(source, x + n);
297 y = audio_stream_wrap(sink, y + n);
298 }
299 }
300 #endif /* CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S24LE */
301 #endif /* CONFIG_IPC_MAJOR_3 */
302
eq_iir_pass(struct processing_module * mod,struct input_stream_buffer * bsource,struct output_stream_buffer * bsink,uint32_t frames)303 static void eq_iir_pass(struct processing_module *mod, struct input_stream_buffer *bsource,
304 struct output_stream_buffer *bsink, uint32_t frames)
305 {
306 struct audio_stream __sparse_cache *source = bsource->data;
307 struct audio_stream __sparse_cache *sink = bsink->data;
308
309 audio_stream_copy(source, 0, sink, 0, frames * source->channels);
310 }
311
312 #if CONFIG_IPC_MAJOR_3
313 #if CONFIG_FORMAT_S16LE && CONFIG_FORMAT_S32LE
eq_iir_s32_s16_pass(struct processing_module * mod,struct input_stream_buffer * bsource,struct output_stream_buffer * bsink,uint32_t frames)314 static void eq_iir_s32_s16_pass(struct processing_module *mod, struct input_stream_buffer *bsource,
315 struct output_stream_buffer *bsink, uint32_t frames)
316 {
317 struct audio_stream __sparse_cache *source = bsource->data;
318 struct audio_stream __sparse_cache *sink = bsink->data;
319 int32_t *x = source->r_ptr;
320 int16_t *y = sink->w_ptr;
321 int nmax;
322 int n;
323 int i;
324 int remaining_samples = frames * source->channels;
325
326 while (remaining_samples) {
327 nmax = EQ_IIR_BYTES_TO_S32_SAMPLES(audio_stream_bytes_without_wrap(source, x));
328 n = MIN(remaining_samples, nmax);
329 nmax = EQ_IIR_BYTES_TO_S16_SAMPLES(audio_stream_bytes_without_wrap(sink, y));
330 n = MIN(n, nmax);
331 for (i = 0; i < n; i++) {
332 *y = sat_int16(Q_SHIFT_RND(*x, 31, 15));
333 x++;
334 y++;
335 }
336 remaining_samples -= n;
337 x = audio_stream_wrap(source, x);
338 y = audio_stream_wrap(sink, y);
339 }
340 }
341 #endif /* CONFIG_FORMAT_S16LE && CONFIG_FORMAT_S32LE */
342
343 #if CONFIG_FORMAT_S24LE && CONFIG_FORMAT_S32LE
eq_iir_s32_s24_pass(struct processing_module * mod,struct input_stream_buffer * bsource,struct output_stream_buffer * bsink,uint32_t frames)344 static void eq_iir_s32_s24_pass(struct processing_module *mod, struct input_stream_buffer *bsource,
345 struct output_stream_buffer *bsink, uint32_t frames)
346 {
347 struct audio_stream __sparse_cache *source = bsource->data;
348 struct audio_stream __sparse_cache *sink = bsink->data;
349 int32_t *x = source->r_ptr;
350 int32_t *y = sink->w_ptr;
351 int nmax;
352 int n;
353 int i;
354 int remaining_samples = frames * source->channels;
355
356 while (remaining_samples) {
357 nmax = EQ_IIR_BYTES_TO_S32_SAMPLES(audio_stream_bytes_without_wrap(source, x));
358 n = MIN(remaining_samples, nmax);
359 nmax = EQ_IIR_BYTES_TO_S32_SAMPLES(audio_stream_bytes_without_wrap(sink, y));
360 n = MIN(n, nmax);
361 for (i = 0; i < n; i++) {
362 *y = sat_int24(Q_SHIFT_RND(*x, 31, 23));
363 x++;
364 y++;
365 }
366 remaining_samples -= n;
367 x = audio_stream_wrap(source, x);
368 y = audio_stream_wrap(sink, y);
369 }
370 }
371 #endif /* CONFIG_FORMAT_S24LE && CONFIG_FORMAT_S32LE */
372 #endif /* CONFIG_IPC_MAJOR_3 */
373
374 #if CONFIG_IPC_MAJOR_3
375 const struct eq_iir_func_map fm_configured[] = {
376 #if CONFIG_FORMAT_S16LE
377 {SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, eq_iir_s16_default},
378 #endif /* CONFIG_FORMAT_S16LE */
379 #if CONFIG_FORMAT_S16LE && CONFIG_FORMAT_S24LE
380 {SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S24_4LE, NULL},
381 {SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S16_LE, NULL},
382
383 #endif /* CONFIG_FORMAT_S16LE && CONFIG_FORMAT_S24LE */
384 #if CONFIG_FORMAT_S16LE && CONFIG_FORMAT_S32LE
385 {SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S32_LE, NULL},
386 {SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S16_LE, eq_iir_s32_16_default},
387 #endif /* CONFIG_FORMAT_S16LE && CONFIG_FORMAT_S32LE */
388 #if CONFIG_FORMAT_S24LE
389 {SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, eq_iir_s24_default},
390 #endif /* CONFIG_FORMAT_S24LE */
391 #if CONFIG_FORMAT_S24LE && CONFIG_FORMAT_S32LE
392 {SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S32_LE, NULL},
393 {SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S24_4LE, eq_iir_s32_24_default},
394 #endif /* CONFIG_FORMAT_S24LE && CONFIG_FORMAT_S32LE */
395 #if CONFIG_FORMAT_S32LE
396 {SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S32_LE, eq_iir_s32_default},
397 #endif /* CONFIG_FORMAT_S32LE */
398 };
399
400 const struct eq_iir_func_map fm_passthrough[] = {
401 #if CONFIG_FORMAT_S16LE
402 {SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, eq_iir_pass},
403 #endif /* CONFIG_FORMAT_S16LE */
404 #if CONFIG_FORMAT_S16LE && CONFIG_FORMAT_S24LE
405 {SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S24_4LE, NULL},
406 {SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S16_LE, NULL},
407
408 #endif /* CONFIG_FORMAT_S16LE && CONFIG_FORMAT_S24LE*/
409 #if CONFIG_FORMAT_S16LE && CONFIG_FORMAT_S32LE
410 {SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S32_LE, NULL},
411 {SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S16_LE, eq_iir_s32_s16_pass},
412 #endif /* CONFIG_FORMAT_S16LE && CONFIG_FORMAT_S32LE*/
413 #if CONFIG_FORMAT_S24LE
414 {SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, eq_iir_pass},
415 #endif /* CONFIG_FORMAT_S24LE */
416 #if CONFIG_FORMAT_S24LE && CONFIG_FORMAT_S32LE
417 {SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S32_LE, NULL},
418 {SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S24_4LE, eq_iir_s32_s24_pass},
419 #endif /* CONFIG_FORMAT_S24LE */
420 #if CONFIG_FORMAT_S32LE
421 {SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S32_LE, eq_iir_pass},
422 #endif /* CONFIG_FORMAT_S32LE */
423 };
424
eq_iir_find_func(enum sof_ipc_frame source_format,enum sof_ipc_frame sink_format,const struct eq_iir_func_map * map,int n)425 static eq_iir_func eq_iir_find_func(enum sof_ipc_frame source_format,
426 enum sof_ipc_frame sink_format,
427 const struct eq_iir_func_map *map,
428 int n)
429 {
430 int i;
431
432 /* Find suitable processing function from map. */
433 for (i = 0; i < n; i++) {
434 if ((uint8_t)source_format != map[i].source)
435 continue;
436 if ((uint8_t)sink_format != map[i].sink)
437 continue;
438
439 return map[i].func;
440 }
441
442 return NULL;
443 }
444
445 #elif CONFIG_IPC_MAJOR_4
446
eq_iir_find_func(struct processing_module * mod)447 static eq_iir_func eq_iir_find_func(struct processing_module *mod)
448 {
449 unsigned int valid_bit_depth = mod->priv.cfg.base_cfg.audio_fmt.valid_bit_depth;
450
451 comp_dbg(mod->dev, "eq_iir_find_func(): valid_bit_depth %d", valid_bit_depth);
452 switch (valid_bit_depth) {
453 #if CONFIG_FORMAT_S16LE
454 case IPC4_DEPTH_16BIT:
455 return eq_iir_s16_default;
456 #endif /* CONFIG_FORMAT_S16LE */
457 #if CONFIG_FORMAT_S24LE
458 case IPC4_DEPTH_24BIT:
459 return eq_iir_s24_default;
460 #endif /* CONFIG_FORMAT_S24LE */
461 #if CONFIG_FORMAT_S32LE
462 case IPC4_DEPTH_32BIT:
463 return eq_iir_s32_default;
464 #endif /* CONFIG_FORMAT_S32LE */
465 default:
466 comp_err(mod->dev, "set_fir_func(), invalid valid_bith_depth");
467 }
468 return NULL;
469 }
470 #endif /* CONFIG_IPC_MAJOR_4 */
471
eq_iir_free_delaylines(struct comp_data * cd)472 static void eq_iir_free_delaylines(struct comp_data *cd)
473 {
474 struct iir_state_df1 *iir = cd->iir;
475 int i = 0;
476
477 /* Free the common buffer for all EQs and point then
478 * each IIR channel delay line to NULL.
479 */
480 rfree(cd->iir_delay);
481 cd->iir_delay = NULL;
482 cd->iir_delay_size = 0;
483 for (i = 0; i < PLATFORM_MAX_CHANNELS; i++)
484 iir[i].delay = NULL;
485 }
486
eq_iir_init_coef(struct processing_module * mod,int nch)487 static int eq_iir_init_coef(struct processing_module *mod, int nch)
488 {
489 struct comp_data *cd = module_get_private_data(mod);
490 struct sof_eq_iir_config *config = cd->config;
491 struct iir_state_df1 *iir = cd->iir;
492 struct sof_eq_iir_header *lookup[SOF_EQ_IIR_MAX_RESPONSES];
493 struct sof_eq_iir_header *eq;
494 int32_t *assign_response;
495 int32_t *coef_data;
496 int size_sum = 0;
497 int resp = 0;
498 int i;
499 int j;
500 int s;
501
502 comp_info(mod->dev, "eq_iir_init_coef(): %u responses, %u channels, stream %d channels",
503 config->number_of_responses, config->channels_in_config, nch);
504
505 /* Sanity checks */
506 if (nch > PLATFORM_MAX_CHANNELS ||
507 config->channels_in_config > PLATFORM_MAX_CHANNELS ||
508 !config->channels_in_config) {
509 comp_err(mod->dev, "eq_iir_init_coef(), invalid channels count");
510 return -EINVAL;
511 }
512 if (config->number_of_responses > SOF_EQ_IIR_MAX_RESPONSES) {
513 comp_err(mod->dev, "eq_iir_init_coef(), # of resp exceeds max");
514 return -EINVAL;
515 }
516
517 /* Collect index of response start positions in all_coefficients[] */
518 j = 0;
519 assign_response = ASSUME_ALIGNED(&config->data[0], 4);
520 coef_data = ASSUME_ALIGNED(&config->data[config->channels_in_config],
521 4);
522 for (i = 0; i < SOF_EQ_IIR_MAX_RESPONSES; i++) {
523 if (i < config->number_of_responses) {
524 eq = (struct sof_eq_iir_header *)&coef_data[j];
525 lookup[i] = eq;
526 j += SOF_EQ_IIR_NHEADER
527 + SOF_EQ_IIR_NBIQUAD * eq->num_sections;
528 } else {
529 lookup[i] = NULL;
530 }
531 }
532
533 /* Initialize 1st phase */
534 for (i = 0; i < nch; i++) {
535 /* Check for not reading past blob response to channel assign
536 * map. The previous channel response is assigned for any
537 * additional channels in the stream. It allows to use single
538 * channel configuration to setup multi channel equalization
539 * with the same response.
540 */
541 if (i < config->channels_in_config)
542 resp = assign_response[i];
543
544 if (resp < 0) {
545 /* Initialize EQ channel to bypass and continue with
546 * next channel response.
547 */
548 comp_info(mod->dev, "eq_iir_init_coef(), ch %d is set to bypass", i);
549 iir_reset_df1(&iir[i]);
550 continue;
551 }
552
553 if (resp >= config->number_of_responses) {
554 comp_err(mod->dev, "eq_iir_init_coef(), requested response %d exceeds defined",
555 resp);
556 return -EINVAL;
557 }
558
559 /* Initialize EQ coefficients */
560 eq = lookup[resp];
561 s = iir_delay_size_df1(eq);
562 if (s > 0) {
563 size_sum += s;
564 } else {
565 comp_err(mod->dev, "eq_iir_init_coef(), sections count %d exceeds max",
566 eq->num_sections);
567 return -EINVAL;
568 }
569
570 iir_init_coef_df1(&iir[i], eq);
571 comp_info(mod->dev, "eq_iir_init_coef(), ch %d is set to response %d", i, resp);
572 }
573
574 return size_sum;
575 }
576
eq_iir_init_delay(struct iir_state_df1 * iir,int32_t * delay_start,int nch)577 static void eq_iir_init_delay(struct iir_state_df1 *iir,
578 int32_t *delay_start, int nch)
579 {
580 int32_t *delay = delay_start;
581 int i;
582
583 /* Initialize second phase to set EQ delay lines pointers. A
584 * bypass mode filter is indicated by biquads count of zero.
585 */
586 for (i = 0; i < nch; i++) {
587 if (iir[i].biquads > 0)
588 iir_init_delay_df1(&iir[i], &delay);
589 }
590 }
591
eq_iir_setup(struct processing_module * mod,int nch)592 static int eq_iir_setup(struct processing_module *mod, int nch)
593 {
594 struct comp_data *cd = module_get_private_data(mod);
595 int delay_size;
596
597 /* Free existing IIR channels data if it was allocated */
598 eq_iir_free_delaylines(cd);
599
600 /* Set coefficients for each channel EQ from coefficient blob */
601 delay_size = eq_iir_init_coef(mod, nch);
602 if (delay_size < 0)
603 return delay_size; /* Contains error code */
604
605 /* If all channels were set to bypass there's no need to
606 * allocate delay. Just return with success.
607 */
608 if (!delay_size)
609 return 0;
610
611 /* Allocate all IIR channels data in a big chunk and clear it */
612 cd->iir_delay = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
613 delay_size);
614 if (!cd->iir_delay) {
615 comp_err(mod->dev, "eq_iir_setup(), delay allocation fail");
616 return -ENOMEM;
617 }
618
619 cd->iir_delay_size = delay_size;
620
621 /* Assign delay line to each channel EQ */
622 eq_iir_init_delay(cd->iir, cd->iir_delay, nch);
623 return 0;
624 }
625
626 /*
627 * End of EQ setup code. Next the standard component methods.
628 */
eq_iir_init(struct processing_module * mod)629 static int eq_iir_init(struct processing_module *mod)
630 {
631 struct module_data *md = &mod->priv;
632 struct comp_dev *dev = mod->dev;
633 struct module_config *cfg = &md->cfg;
634 struct comp_data *cd;
635 size_t bs = cfg->size;
636 int i, ret;
637
638 comp_info(dev, "eq_iir_init()");
639
640 /* Check first before proceeding with dev and cd that coefficients blob size is sane */
641 if (bs > SOF_EQ_IIR_MAX_SIZE) {
642 comp_err(dev, "eq_iir_init(), coefficients blob size %u exceeds maximum", bs);
643 return -EINVAL;
644 }
645
646 cd = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, sizeof(*cd));
647 if (!cd)
648 return -ENOMEM;
649
650 md->private = cd;
651
652 /* component model data handler */
653 cd->model_handler = comp_data_blob_handler_new(dev);
654 if (!cd->model_handler) {
655 comp_err(dev, "eq_iir_init(): comp_data_blob_handler_new() failed.");
656 ret = -ENOMEM;
657 goto err;
658 }
659
660 /* Allocate and make a copy of the coefficients blob and reset IIR. If
661 * the EQ is configured later in run-time the size is zero.
662 */
663 ret = comp_init_data_blob(cd->model_handler, bs, cfg->data);
664 if (ret < 0) {
665 comp_err(dev, "eq_iir_init(): comp_init_data_blob() failed with error: %d", ret);
666 comp_data_blob_handler_free(cd->model_handler);
667 goto err;
668 }
669
670 for (i = 0; i < PLATFORM_MAX_CHANNELS; i++)
671 iir_reset_df1(&cd->iir[i]);
672
673 /*
674 * set the simple_copy flag as the eq_iir component always produces period_bytes
675 * every period and has only 1 input/output buffer
676 */
677 mod->simple_copy = true;
678
679 return 0;
680 err:
681 rfree(cd);
682 return ret;
683 }
684
eq_iir_free(struct processing_module * mod)685 static int eq_iir_free(struct processing_module *mod)
686 {
687 struct comp_data *cd = module_get_private_data(mod);
688
689 comp_info(mod->dev, "eq_iir_free()");
690
691 eq_iir_free_delaylines(cd);
692 comp_data_blob_handler_free(cd->model_handler);
693
694 rfree(cd);
695 return 0;
696 }
697
698 #if CONFIG_IPC_MAJOR_3
eq_iir_verify_params(struct comp_dev * dev,struct sof_ipc_stream_params * params)699 static int eq_iir_verify_params(struct comp_dev *dev,
700 struct sof_ipc_stream_params *params)
701 {
702 struct comp_buffer *sourceb, *sinkb;
703 struct comp_buffer __sparse_cache *source_c, *sink_c;
704 uint32_t buffer_flag;
705 int ret;
706
707 comp_dbg(dev, "eq_iir_verify_params()");
708
709 /* EQ component will only ever have 1 source and 1 sink buffer */
710 sourceb = list_first_item(&dev->bsource_list, struct comp_buffer,
711 sink_list);
712 sinkb = list_first_item(&dev->bsink_list, struct comp_buffer,
713 source_list);
714 source_c = buffer_acquire(sourceb);
715 sink_c = buffer_acquire(sinkb);
716
717 /* we check whether we can support frame_fmt conversion (whether we have
718 * such conversion function) due to source and sink buffer frame_fmt's.
719 * If not, we will overwrite sink (playback) and source (capture) with
720 * pcm frame_fmt and will not make any conversion (sink and source
721 * frame_fmt will be equal).
722 */
723 buffer_flag = eq_iir_find_func(source_c->stream.frame_fmt,
724 sink_c->stream.frame_fmt, fm_configured,
725 ARRAY_SIZE(fm_configured)) ?
726 BUFF_PARAMS_FRAME_FMT : 0;
727
728 buffer_release(sink_c);
729 buffer_release(source_c);
730
731 ret = comp_verify_params(dev, buffer_flag, params);
732 if (ret < 0) {
733 comp_err(dev, "eq_iir_verify_params(): comp_verify_params() failed.");
734 return ret;
735 }
736
737 return 0;
738 }
739 #endif /* CONFIG_IPC_MAJOR_3 */
740
741 /* used to pass standard and bespoke commands (with data) to component */
eq_iir_set_config(struct processing_module * mod,uint32_t config_id,enum module_cfg_fragment_position pos,uint32_t data_offset_size,const uint8_t * fragment,size_t fragment_size,uint8_t * response,size_t response_size)742 static int eq_iir_set_config(struct processing_module *mod, uint32_t config_id,
743 enum module_cfg_fragment_position pos, uint32_t data_offset_size,
744 const uint8_t *fragment, size_t fragment_size, uint8_t *response,
745 size_t response_size)
746 {
747 struct comp_data *cd = module_get_private_data(mod);
748
749 comp_info(mod->dev, "eq_iir_set_config()");
750
751 return comp_data_blob_set(cd->model_handler, pos, data_offset_size, fragment,
752 fragment_size);
753 }
754
eq_iir_get_config(struct processing_module * mod,uint32_t config_id,uint32_t * data_offset_size,uint8_t * fragment,size_t fragment_size)755 static int eq_iir_get_config(struct processing_module *mod,
756 uint32_t config_id, uint32_t *data_offset_size,
757 uint8_t *fragment, size_t fragment_size)
758 {
759 struct sof_ipc_ctrl_data *cdata = (struct sof_ipc_ctrl_data *)fragment;
760 struct comp_data *cd = module_get_private_data(mod);
761
762 comp_info(mod->dev, "eq_iir_get_config()");
763
764 return comp_data_blob_get_cmd(cd->model_handler, cdata, fragment_size);
765 }
766
eq_iir_new_blob(struct processing_module * mod,struct comp_data * cd,enum sof_ipc_frame source_format,enum sof_ipc_frame sink_format,int channels)767 static int eq_iir_new_blob(struct processing_module *mod, struct comp_data *cd,
768 enum sof_ipc_frame source_format, enum sof_ipc_frame sink_format,
769 int channels)
770 {
771 int ret;
772
773 ret = eq_iir_setup(mod, channels);
774 if (ret < 0) {
775 comp_err(mod->dev, "eq_iir_new_blob(), failed IIR setup");
776 return ret;
777 } else if (cd->iir_delay_size) {
778 comp_dbg(mod->dev, "eq_iir_new_blob(), active");
779 #if CONFIG_IPC_MAJOR_3
780 cd->eq_iir_func = eq_iir_find_func(source_format, sink_format, fm_configured,
781 ARRAY_SIZE(fm_configured));
782 #elif CONFIG_IPC_MAJOR_4
783 cd->eq_iir_func = eq_iir_find_func(mod);
784 #endif
785 } else {
786 comp_dbg(mod->dev, "eq_iir_new_blob(), pass-through");
787 #if CONFIG_IPC_MAJOR_3
788 cd->eq_iir_func = eq_iir_find_func(source_format, sink_format, fm_passthrough,
789 ARRAY_SIZE(fm_passthrough));
790 #elif CONFIG_IPC_MAJOR_4
791 cd->eq_iir_func = eq_iir_pass;
792 #endif
793 }
794
795 return 0;
796 }
797
eq_iir_process(struct processing_module * mod,struct input_stream_buffer * input_buffers,int num_input_buffers,struct output_stream_buffer * output_buffers,int num_output_buffers)798 static int eq_iir_process(struct processing_module *mod,
799 struct input_stream_buffer *input_buffers, int num_input_buffers,
800 struct output_stream_buffer *output_buffers, int num_output_buffers)
801 {
802 struct comp_data *cd = module_get_private_data(mod);
803 struct audio_stream __sparse_cache *source = input_buffers[0].data;
804 struct audio_stream __sparse_cache *sink = output_buffers[0].data;
805 uint32_t frame_count = input_buffers[0].size;
806 int ret;
807
808 /* Check for changed configuration */
809 if (comp_is_new_data_blob_available(cd->model_handler)) {
810 cd->config = comp_get_data_blob(cd->model_handler, NULL, NULL);
811 ret = eq_iir_new_blob(mod, cd, source->frame_fmt,
812 sink->frame_fmt, source->channels);
813 if (ret)
814 return ret;
815 }
816
817 if (frame_count) {
818 cd->eq_iir_func(mod, &input_buffers[0], &output_buffers[0], frame_count);
819 module_update_buffer_position(&input_buffers[0], &output_buffers[0], frame_count);
820 }
821 return 0;
822 }
823
824 /**
825 * \brief Set EQ IIR frames alignment limit.
826 * \param[in,out] source Structure pointer of source.
827 * \param[in,out] sink Structure pointer of sink.
828 */
eq_iir_set_alignment(struct audio_stream __sparse_cache * source,struct audio_stream __sparse_cache * sink)829 static void eq_iir_set_alignment(struct audio_stream __sparse_cache *source,
830 struct audio_stream __sparse_cache *sink)
831 {
832 const uint32_t byte_align = 8;
833 const uint32_t frame_align_req = 2;
834
835 audio_stream_init_alignment_constants(byte_align, frame_align_req, source);
836 audio_stream_init_alignment_constants(byte_align, frame_align_req, sink);
837 }
838
839 #if CONFIG_IPC_MAJOR_4
eq_iir_params(struct processing_module * mod)840 static int eq_iir_params(struct processing_module *mod)
841 {
842 struct sof_ipc_stream_params *params = mod->stream_params;
843 struct sof_ipc_stream_params comp_params;
844 struct comp_dev *dev = mod->dev;
845 struct comp_buffer *sinkb;
846 struct comp_buffer __sparse_cache *sink_c;
847 uint32_t __sparse_cache valid_fmt, frame_fmt;
848 int i, ret;
849
850 comp_dbg(dev, "eq_iir_params()");
851 comp_params = *params;
852 comp_params.channels = mod->priv.cfg.base_cfg.audio_fmt.channels_count;
853 comp_params.rate = mod->priv.cfg.base_cfg.audio_fmt.sampling_frequency;
854 comp_params.buffer_fmt = mod->priv.cfg.base_cfg.audio_fmt.interleaving_style;
855
856 audio_stream_fmt_conversion(mod->priv.cfg.base_cfg.audio_fmt.depth,
857 mod->priv.cfg.base_cfg.audio_fmt.valid_bit_depth,
858 &frame_fmt, &valid_fmt,
859 mod->priv.cfg.base_cfg.audio_fmt.s_type);
860
861 comp_params.frame_fmt = frame_fmt;
862
863 for (i = 0; i < SOF_IPC_MAX_CHANNELS; i++)
864 comp_params.chmap[i] = (mod->priv.cfg.base_cfg.audio_fmt.ch_map >> i * 4) & 0xf;
865
866 component_set_nearest_period_frames(dev, comp_params.rate);
867 sinkb = list_first_item(&dev->bsink_list, struct comp_buffer, source_list);
868 sink_c = buffer_acquire(sinkb);
869 ret = buffer_set_params(sink_c, &comp_params, true);
870 buffer_release(sink_c);
871 return ret;
872 }
873 #endif
874
eq_iir_set_passthrough_func(struct comp_data * cd,enum sof_ipc_frame source_format,enum sof_ipc_frame sink_format)875 static void eq_iir_set_passthrough_func(struct comp_data *cd,
876 enum sof_ipc_frame source_format,
877 enum sof_ipc_frame sink_format)
878 {
879 #if CONFIG_IPC_MAJOR_3
880 cd->eq_iir_func = eq_iir_find_func(source_format, sink_format, fm_passthrough,
881 ARRAY_SIZE(fm_passthrough));
882 #else
883 cd->eq_iir_func = eq_iir_pass;
884 #endif
885 }
886
eq_iir_prepare(struct processing_module * mod)887 static int eq_iir_prepare(struct processing_module *mod)
888 {
889 struct comp_data *cd = module_get_private_data(mod);
890 struct comp_buffer *sourceb, *sinkb;
891 struct comp_buffer __sparse_cache *source_c, *sink_c;
892 struct comp_dev *dev = mod->dev;
893 enum sof_ipc_frame source_format;
894 enum sof_ipc_frame sink_format;
895 int channels;
896 int ret = 0;
897
898 comp_dbg(dev, "eq_iir_prepare()");
899
900 #if CONFIG_IPC_MAJOR_3
901 ret = eq_iir_verify_params(dev, mod->stream_params);
902 if (ret < 0)
903 return ret;
904
905 #elif CONFIG_IPC_MAJOR_4
906 ret = eq_iir_params(mod);
907 if (ret < 0) {
908 comp_set_state(dev, COMP_TRIGGER_RESET);
909 return ret;
910 }
911 #endif
912
913 /* EQ component will only ever have 1 source and 1 sink buffer */
914 sourceb = list_first_item(&dev->bsource_list, struct comp_buffer, sink_list);
915 sinkb = list_first_item(&dev->bsink_list, struct comp_buffer, source_list);
916 source_c = buffer_acquire(sourceb);
917 sink_c = buffer_acquire(sinkb);
918 eq_iir_set_alignment(&source_c->stream, &sink_c->stream);
919
920 /* get source and sink data format */
921 channels = sink_c->stream.channels;
922 source_format = source_c->stream.frame_fmt;
923 sink_format = sink_c->stream.frame_fmt;
924 buffer_release(sink_c);
925 buffer_release(source_c);
926
927 cd->config = comp_get_data_blob(cd->model_handler, NULL, NULL);
928
929 /* Initialize EQ */
930 comp_info(dev, "eq_iir_prepare(), source_format=%d, sink_format=%d",
931 source_format, sink_format);
932
933 eq_iir_set_passthrough_func(cd, source_format, sink_format);
934
935 /* Initialize EQ */
936 if (cd->config) {
937 ret = eq_iir_new_blob(mod, cd, source_format, sink_format, channels);
938 if (ret)
939 return ret;
940 }
941
942 if (!cd->eq_iir_func) {
943 comp_err(dev, "eq_iir_prepare(), No processing function found");
944 ret = -EINVAL;
945 }
946
947 return ret;
948 }
949
eq_iir_reset(struct processing_module * mod)950 static int eq_iir_reset(struct processing_module *mod)
951 {
952 struct comp_data *cd = module_get_private_data(mod);
953 int i;
954
955 comp_info(mod->dev, "eq_iir_reset()");
956
957 eq_iir_free_delaylines(cd);
958
959 cd->eq_iir_func = NULL;
960 for (i = 0; i < PLATFORM_MAX_CHANNELS; i++)
961 iir_reset_df1(&cd->iir[i]);
962
963 return 0;
964 }
965
966 static struct module_interface eq_iir_interface = {
967 .init = eq_iir_init,
968 .prepare = eq_iir_prepare,
969 .process = eq_iir_process,
970 .set_configuration = eq_iir_set_config,
971 .get_configuration = eq_iir_get_config,
972 .reset = eq_iir_reset,
973 .free = eq_iir_free
974 };
975
976 DECLARE_MODULE_ADAPTER(eq_iir_interface, eq_iir_uuid, eq_iir_tr);
977 SOF_MODULE_INIT(eq_iir, sys_comp_module_eq_iir_interface_init);
978