From d21053a0755415ea25d3350ed5bf04a64cff9d24 Mon Sep 17 00:00:00 2001 From: Seppo Ingalsuo Date: Mon, 16 Nov 2026 12:05:45 +0000 Subject: [PATCH] audio: ecns: add ECNS DP module Add the ECNS (Echo Cancellation and Noise Suppression) DP component (CONFIG_COMP_ECNS) supporting dual-rate 16kHz/48kHz processing with a 20 ms period: - Supports 16kHz single-producer fallback, 16/32-bit formats, built-in IIR high-pass filters with +30 dB KPB gain / capture boost switch, and optional monotonic test signal generation (CONFIG_ECNS_TEST_SIGNAL). Signed-off-by: Seppo Ingalsuo Co-authored-by: Liam Girdwood Signed-off-by: Liam Girdwood --- src/audio/CMakeLists.txt | 3 + src/audio/Kconfig | 67 ++ src/audio/ecns/CMakeLists.txt | 3 + src/audio/ecns/README.md | 152 ++++ src/audio/ecns/ecns.c | 1200 +++++++++++++++++++++++++++++ src/audio/ecns/ecns.toml | 19 + src/include/sof/audio/component.h | 1 + src/include/sof/audio/ecns.h | 33 + tools/rimage/config/nvl.toml.h | 4 + tools/rimage/config/ptl.toml.h | 4 + tools/rimage/config/tgl.toml.h | 4 + tools/rimage/config/wcl.toml.h | 4 + uuid-registry.txt | 1 + 13 files changed, 1495 insertions(+) create mode 100644 src/audio/ecns/CMakeLists.txt create mode 100644 src/audio/ecns/README.md create mode 100644 src/audio/ecns/ecns.c create mode 100644 src/audio/ecns/ecns.toml create mode 100644 src/include/sof/audio/ecns.h diff --git a/src/audio/CMakeLists.txt b/src/audio/CMakeLists.txt index 0ca532950f5c..c05401b73ea6 100644 --- a/src/audio/CMakeLists.txt +++ b/src/audio/CMakeLists.txt @@ -35,6 +35,9 @@ endif() if(CONFIG_COMP_DRC) add_subdirectory(drc) endif() +if(CONFIG_COMP_ECNS) + add_subdirectory(ecns) +endif() if(CONFIG_COMP_FFMPEG_DEC) add_subdirectory(ffmpeg_dec) endif() diff --git a/src/audio/Kconfig b/src/audio/Kconfig index fecbf0d25afa..3fa407d196fa 100644 --- a/src/audio/Kconfig +++ b/src/audio/Kconfig @@ -153,6 +153,73 @@ config COMP_STUBS Select to force all 3P blocks to link against stubs rather than their libraries. This should only be used in testing environments like fuzzers or CI. +config COMP_ECNS + bool "ECNS data processing component" + default y + depends on IPC_MAJOR_4 + select MATH_IIR + select MATH_IIR_DF1 + help + Select to build the ECNS (Echo Cancellation and Noise Suppression) + Data Processing (DP) component. Operates with a 20ms period to process + 4 DMIC channels (2 mics + 2 echo references) and outputs clean mono + to KPB and clean stereo to host capture. + +if COMP_ECNS + +choice + prompt "ECNS mono downmix source channels for KPB output" + default ECNS_KPB_MONO_AVG_ALL + help + Selects which pin0 input channels ECNS averages into the mono + detection output sent to KPB. Channel indices out of range for the + current input stream are skipped, falling back to all channels. + +config ECNS_KPB_MONO_AVG_ALL + bool "Average all available input channels" + help + Average every available pin0 input channel into the mono output. + +config ECNS_KPB_MONO_AVG_CH_1_2 + bool "Average input channels 1 and 2" + help + Average the first two input channels (indices 0 and 1). + +config ECNS_KPB_MONO_AVG_CH_3_4 + bool "Average input channels 3 and 4" + help + Average the third and fourth input channels (indices 2 and 3). + Useful when the first mic pair is poor quality. + +endchoice + +config ECNS_KPB_HPF + bool "ECNS 100 Hz high-pass filter on KPB output" + default y + help + Filter the ECNS mono KPB output (Pin 0) with a built-in 100 Hz + second-order high-pass IIR with +20 dB gain. When disabled, the + mono downmix is passed through with only s16/s32 format conversion. + +config ECNS_HOST_HPF + bool "ECNS 40 Hz high-pass filter on host output" + default y + help + Filter each channel of the ECNS host output (Pin 1) with a built-in + 40 Hz second-order high-pass IIR with +20 dB gain. When disabled, + the Pin 1 data is passed through with only s16/s32 format conversion. + +config ECNS_TEST_SIGNAL + bool "ECNS test signal generator support" + default n + help + Enable support for the test signal generator on the ECNS clean mono + output pin (Pin 0 to KPB). When enabled, a monotonic incrementing + counter can be selected via ALSA control instead of microphone audio + to verify data continuity through the detection path. + +endif # COMP_ECNS + config COMP_KPB bool "KPB component" default y diff --git a/src/audio/ecns/CMakeLists.txt b/src/audio/ecns/CMakeLists.txt new file mode 100644 index 000000000000..deeeed3258da --- /dev/null +++ b/src/audio/ecns/CMakeLists.txt @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: BSD-3-Clause + +add_local_sources(sof ecns.c) diff --git a/src/audio/ecns/README.md b/src/audio/ecns/README.md new file mode 100644 index 000000000000..48e6ca59b9c7 --- /dev/null +++ b/src/audio/ecns/README.md @@ -0,0 +1,152 @@ +# ECNS (Echo Cancellation & Noise Suppression) Processing Module + +## Overview + +The `ecns` component is an IPC4 Data Processing (DP) module operating at a 20 ms period. It supports dual-stream, dual-rate microphone capture and preprocessing, connecting directly to physical PDM digital microphone interfaces: +- **Input Pin 0 (`dmic16k` on PDM1)**: 16 kHz stereo (2-channel, 16-bit `S16_LE`) dedicated speech capture stream. +- **Input Pin 1 (`dmic01` on PDM0)**: 48 kHz stereo (2-channel, 16-bit `S16_LE`) high-resolution capture stream. + +The module performs echo cancellation, noise suppression, and channel routing, delivering two distinct output streams via separate output pins: +1. **Output Pin 0 (Mono Clean Speech / Keyword Stream)**: Operates at 16 kHz. Extracts the Left channel from Input Pin 0 and delivers a single-channel mono clean stream to the Key Phrase Buffer (`kpb.116.1`) and downstream microWakeWord (`mww`) keyword detection slots. Triggers deliver pre-roll and live speech to the host via the WOV Arbiter (`PCM 12`, `hw:0,12`). +2. **Output Pin 1 (Stereo Clean Speech Stream)**: Operates at 48 kHz. Filters each channel of Input Pin 1 with a built-in 40 Hz second-order high-pass IIR (DF1, +20 dB gain, `CONFIG_ECNS_HOST_HPF`, default `y`; when disabled the data is passed through with only s16/s32 conversion) and delivers the stereo or 4-channel clean stream to host capture (`host-copier.11.capture`, `PCM 11`, `hw:0,11`). + +Both capture endpoints support low-power D0i3 listening and wake (`capture_compatible_d0i3: true`). + +The `ECNS capture boost` switch control selects between the boosted high-pass responses (+30 dB 100 Hz on Pin 0 with `CONFIG_ECNS_KPB_HPF`, +20 dB 40 Hz on Pin 1) and the same filters with 0 dB gain. The firmware keeps duplicate coefficient sets and only moves the IIR coefficient pointers, so the filter state is kept. The firmware starts boosted, but the kernel sends the switch value (default off, 0 dB) when the widget is set up. The `ecns_kpb_test_signal` switch replaces Pin 0 output with a test ramp (`CONFIG_ECNS_TEST_SIGNAL`). + +--- + +## Topology Architecture + +```mermaid +flowchart TD + %% Hardware PDM Interfaces + subgraph PDM_INTERFACES ["Digital Microphone Interfaces (2 Separate PDM DAIs)"] + direction LR + PDM_48K["PDM Interface: dmic01
DAI Index: 0 (PDM0)
Rate: 48 kHz · 2ch Stereo · 16-bit"] + PDM_16K["PDM Interface: dmic16k
DAI Index: 1 (PDM1)
Rate: 16 kHz · 2ch Stereo · 16-bit"] + end + + %% Pipeline 110: 48 kHz DAI Capture + subgraph P110 ["Pipeline 110: 48 kHz DAI Capture (Core 0, LL 1ms)"] + DAI_48K["dai-copier.DMIC.dmic01.capture
(48 kHz · 2ch · S16_LE)"] + MIXIN_110["mixin.110.1
(48 kHz · 2ch)"] + DAI_48K --> MIXIN_110 + end + + %% Pipeline 119: 16 kHz DAI Capture + subgraph P119 ["Pipeline 119: 16 kHz DAI Capture (Core 0, LL 1ms)"] + DAI_16K["dai-copier.DMIC.dmic16k.capture
(16 kHz · 2ch · S16_LE)"] + MIXIN_119["mixin.119.1
(16 kHz · 2ch)"] + DAI_16K --> MIXIN_119 + end + + PDM_48K --> DAI_48K + PDM_16K --> DAI_16K + + %% Pipeline 115: Dual-Rate ECNS DP Pipeline + subgraph P115 ["Pipeline 115: ECNS Engine (Core 0, DP 20ms, lp_mode 1)"] + direction TB + + MIXOUT_115_2["mixout.115.2
(Pin 0 In: 16 kHz · 2ch stereo)"] + MIXOUT_115_1["mixout.115.1
(Pin 1 In: 48 kHz · 2ch stereo)"] + + ECNS["ecns.115.1 (DP Component · 20ms Period)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Pin 0 In: 16 kHz, 2ch (IBS 1280 bytes)
Pin 1 In: 48 kHz, 2ch (IBS 3840 bytes)
─────────────────────────────
Processing:
• Pin 0: Extract Left Ch → 16k Mono Clean
• Pin 1: 40 Hz HPF +20 dB → 48k Stereo Clean
─────────────────────────────
Pin 0 Out: 16 kHz, 1ch mono (OBS 640 bytes)
Pin 1 Out: 48 kHz, 2ch stereo (OBS 3840 bytes)"] + + MIXOUT_115_2 -->|"Pin 0 In (16k)"| ECNS + MIXOUT_115_1 -->|"Pin 1 In (48k)"| ECNS + end + + MIXIN_119 --> MIXOUT_115_2 + MIXIN_110 --> MIXOUT_115_1 + + %% Pipeline 117: Host Capture PCM 11 + subgraph P117 ["Pipeline 117: ECNS Clean Host Capture (Core 0, LL 1ms)"] + HOST_11["host-copier.11.capture
(PCM 11: 'DMIC ECNS Capture'
hw:0,11 · d0i3_compatible=true)"] + end + + %% Pipeline 116: KPB History Buffer + subgraph P116 ["Pipeline 116: Key Phrase Buffer (Core 0, DP 20ms)"] + KPB["kpb.116.1
(2000 ms mono history = 64 KB)"] + MIXIN_116["mixin.116.1
(3-way detector fanout)"] + KPB -->|"Pin 0 (Live Feed)"| MIXIN_116 + end + + ECNS -->|"Pin 1 Out (Stereo 48k clean direct)"| HOST_11 + ECNS -->|"Pin 0 Out (Mono 16k clean direct)"| KPB + + %% WoV 3-Slot Detector Subgraph + subgraph SLOTS ["Wake-on-Voice Keyword Detector Slots (16 kHz mono)"] + subgraph S1 ["Slot 0 (Pipeline 111)"] + M1["mixout.111.1"] --> MF1["mfcc.111.1"] --> MW1["mww.111.1"] + end + subgraph S2 ["Slot 1 (Pipeline 112)"] + M2["mixout.112.1"] --> MF2["mfcc.112.1"] --> MW2["mww.112.1"] + end + subgraph S3 ["Slot 2 (Pipeline 113)"] + M3["mixout.113.1"] --> MF3["mfcc.113.1"] --> MW3["mww.113.1"] + end + end + + MIXIN_116 --> M1 + MIXIN_116 --> M2 + MIXIN_116 --> M3 + + %% Pipeline 114: WoV Arbiter & Host Capture PCM 12 + subgraph P114 ["Pipeline 114: WoV Arbiter (Core 0, LL 1ms)"] + ARB["wov-arbiter.114.1
• Pin 0: Drained audio stream
• Pins 1-3: Keyword triggers"] + HOST_12["host-copier.12.capture
(PCM 12: 'DMIC Multi-WOV'
hw:0,12 · d0i3_compatible=true)"] + ARB --> HOST_12 + end + + KPB -->|"Pin 1 (Host Sink Drain)"| ARB + MW1 -->|"Pin 1 (Trigger 0)"| ARB + MW2 -->|"Pin 2 (Trigger 1)"| ARB + MW3 -->|"Pin 3 (Trigger 2)"| ARB + + style ECNS fill:#1b4f72,stroke:#555,color:#fff + style KPB fill:#1c4966,stroke:#555,color:#fff + style ARB fill:#4a235a,stroke:#555,color:#fff + style HOST_11 fill:#1e8449,stroke:#555,color:#fff + style HOST_12 fill:#2d5a27,stroke:#555,color:#fff +``` + +--- + +## Specifications + +| Parameter | Specification | +|---|---| +| Module UUID | `6c:8a:4f:21:3a:49:1e:4b:8b:1e:0d:3b:6f:8a:2c:15` | +| Processing Domain | DP (Data Processing) thread (`lp_mode 1`) | +| Execution Period | 20 ms (320 frames at 16 kHz / 960 frames at 48 kHz) | +| Input Pin 0 | 16 kHz · 2 channels (Stereo from `dmic16k`) · `S16_LE` · IBS = 1280 bytes (20ms) | +| Input Pin 1 | 48 kHz · 2 channels (Stereo from `dmic01`) · `S16_LE` · IBS = 3840 bytes (20ms) | +| Output Pin 0 | 16 kHz · 1 channel (Mono left channel clean) · `S16_LE` · OBS = 640 bytes (20ms) | +| Output Pin 1 | 48 kHz · 2 channels (Stereo clean, 40 Hz HPF +20 dB) · `S16_LE` · OBS = 3840 bytes (20ms) | +| Sinks Acceptance | Independent sink buffer limit checks (`kpb_frames` vs `host_frames`) | +| Dynamic Pipeline | Compatible (`dynamic_pipeline 1`) | +| Power States | S0 active streaming and D0i3 low-power listening supported | + +--- + +## Verification & Usage + +### 1. Recording Clean Stereo Audio (Output Pin 1 / PCM 11) +Capture the continuous 48 kHz clean stereo microphone stream: +```bash +arecord -D hw:0,11 -c 2 -r 48000 -f S16_LE -d 3 /tmp/ecns_stereo_48k.wav +``` + +### 2. Recording Keyword Detection & History Buffer Audio (Output Pin 0 / PCM 12) +Capture the gated 16 kHz mono speech stream delivered by the WOV Arbiter upon keyword trigger: +```bash +arecord -D hw:0,12 -c 1 -r 16000 -f S16_LE -d 3 /tmp/wov_mono_16k.wav +``` + +--- + +## Further Reading +- [Topology2 Architecture & Verification Runbook](../../../tools/topology/topology2/README.md) +- [WOV Arbiter Architecture & State Machine](../wov_arbiter/README.md) +- [Developer Integration Guide: Custom WOV & ECNS Algorithms](../../../doc/developer_guides/wov_ecns_integration_guide.md) diff --git a/src/audio/ecns/ecns.c b/src/audio/ecns/ecns.c new file mode 100644 index 000000000000..3a03cc6e9210 --- /dev/null +++ b/src/audio/ecns/ecns.c @@ -0,0 +1,1200 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2026 Intel Corporation. All rights reserved. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(ecns, CONFIG_SOF_LOG_LEVEL); + +SOF_DEFINE_REG_UUID(ecns); +DECLARE_TR_CTX(ecns_tr, SOF_UUID(ecns_uuid), LOG_LEVEL_INFO); + +/* Determine the source-channel averaging window [start, start+cnt) used to + * downmix the pin0 input to the mono KPB detection output. Selection is a + * Kconfig choice; out-of-range channels fall back to all available channels. + */ +static inline void ecns_mono_window(uint32_t ch, uint32_t *start, uint32_t *cnt) +{ +#if CONFIG_ECNS_KPB_MONO_AVG_CH_1_2 + *start = 0; + *cnt = MIN(2, ch); +#elif CONFIG_ECNS_KPB_MONO_AVG_CH_3_4 + if (ch > 2) { + *start = 2; + *cnt = MIN(2, ch - 2); + } else { + *start = 0; + *cnt = ch; + } +#else /* CONFIG_ECNS_KPB_MONO_AVG_ALL */ + *start = 0; + *cnt = ch; +#endif + if (*cnt == 0) + *cnt = 1; +} + +static inline int32_t ecns_mono_s32(const int32_t *frame, uint32_t start, uint32_t cnt) +{ + int64_t acc = 0; + + for (uint32_t c = 0; c < cnt; c++) + acc += frame[start + c]; + return (int32_t)(acc / (int32_t)cnt); +} + +static inline int16_t ecns_mono_s16(const int16_t *frame, uint32_t start, uint32_t cnt) +{ + int32_t acc = 0; + + for (uint32_t c = 0; c < cnt; c++) + acc += frame[start + c]; + return (int16_t)(acc / (int32_t)cnt); +} + +#if CONFIG_ECNS_KPB_HPF +/** + * \brief 100 Hz second-order high-pass filter with +30 dB gain at 16 kHz. + * + * Built-in coefficient set for the KPB detection path (Pin 0 mono mix). + * Converted from tools/topology/topology2/include/components/eqiir/highpass_100hz_30db_16khz.conf + * consisting of struct sof_eq_iir_header (6 words) followed by one + * struct sof_eq_iir_biquad (7 words): + * header: num_sections=1, num_sections_in_series=1, reserved[4]={0,0,0,0} + * biquad: a2, a1, b2, b1, b0 in Q2.30, shift=-5, gain=0x7e33 (32307 in Q2.14) + */ +static const uint32_t hp_100hz_30db_16k[13] = { + 0x00000001, 0x00000001, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0xc37518f5, 0x7c72526b, + 0x1f32926e, 0xc19adb24, 0x1f32926e, 0xfffffffb, + 0x00007e33 +}; + +/** \brief As hp_100hz_30db_16k with 0 dB gain, from highpass_100hz_0db_16khz.conf. */ +static const uint32_t hp_100hz_0db_16k[13] = { + 0x00000001, 0x00000001, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0xc37518f5, 0x7c72526b, + 0x1f32926e, 0xc19adb24, 0x1f32926e, 0x00000000, + 0x00007fb4 +}; +#endif + +#if CONFIG_ECNS_HOST_HPF +/** + * \brief 40 Hz second-order high-pass filters with +20 dB gain for Pin 1. + * + * Converted from highpass_40hz_20db_48khz.conf and highpass_40hz_20db_16khz.conf + * in tools/topology/topology2/include/components/eqiir/ with the same layout + * as hp_100hz_30db_16k. + */ +static const uint32_t hp_40hz_20db_48k[13] = { + 0x00000001, 0x00000001, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0xc078df4c, 0x7f86ae23, + 0x1ff51b12, 0xc015c9db, 0x1ff51b12, 0xfffffffc, + 0x00004fd0 +}; + +static const uint32_t hp_40hz_20db_16k[13] = { + 0x00000001, 0x00000001, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0xc167f2ba, 0x7e940dc3, + 0x1fb8bfff, 0xc08e8003, 0x1fb8bfff, 0xfffffffc, + 0x00004fd0 +}; + +/** + * \brief 40 Hz high-pass filters with 0 dB gain for Pin 1. + * + * From highpass_40hz_0db_48khz.conf and highpass_40hz_0db_16khz.conf. + */ +static const uint32_t hp_40hz_0db_48k[13] = { + 0x00000001, 0x00000001, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0xc078df4c, 0x7f86ae23, + 0x1ff51b12, 0xc015c9db, 0x1ff51b12, 0x00000000, + 0x00007fb2 +}; + +static const uint32_t hp_40hz_0db_16k[13] = { + 0x00000001, 0x00000001, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0xc167f2ba, 0x7e940dc3, + 0x1fb8bfff, 0xc08e8003, 0x1fb8bfff, 0x00000000, + 0x00007fb2 +}; + +#define ECNS_HOST_MAX_CHANNELS 4 +#endif + +/* Switch control indexes, mixer."1" and mixer."2" in ecns.conf */ +#define ECNS_CTRL_INDEX_CAPTURE_BOOST 0 +#define ECNS_CTRL_INDEX_TEST_SIGNAL 1 + +struct ecns_comp_data { + struct ipc4_base_module_cfg base_cfg; + uint32_t sample_rate; + uint32_t channels; + uint32_t sample_width; + uint32_t test_signal_enabled; + bool capture_boost; + bool capture_boost_req; + uint16_t test_signal_val; + struct iir_state_df1 hp_iir; + int32_t *hp_delay; +#if CONFIG_ECNS_HOST_HPF + struct iir_state_df1 host_iir[ECNS_HOST_MAX_CHANNELS]; + int32_t *host_delay; + int host_delay_size; + uint32_t host_rate; +#endif +}; + +#if CONFIG_ECNS_KPB_HPF +static struct sof_eq_iir_header *ecns_kpb_iir_coef(struct ecns_comp_data *cd) +{ + return (struct sof_eq_iir_header *) + (cd->capture_boost ? hp_100hz_30db_16k : hp_100hz_0db_16k); +} +#endif + +#if CONFIG_ECNS_HOST_HPF +static struct sof_eq_iir_header *ecns_host_iir_coef(struct ecns_comp_data *cd) +{ + if (cd->host_rate == 16000) + return (struct sof_eq_iir_header *) + (cd->capture_boost ? hp_40hz_20db_16k : hp_40hz_0db_16k); + + return (struct sof_eq_iir_header *) + (cd->capture_boost ? hp_40hz_20db_48k : hp_40hz_0db_48k); +} +#endif + +/* Only the coefficient pointers move, the filter delay lines are kept. */ +static void ecns_update_iir_coef(struct ecns_comp_data *cd) +{ +#if CONFIG_ECNS_KPB_HPF + iir_init_coef_df1(&cd->hp_iir, ecns_kpb_iir_coef(cd)); +#endif +#if CONFIG_ECNS_HOST_HPF + for (int c = 0; c < ECNS_HOST_MAX_CHANNELS; c++) + iir_init_coef_df1(&cd->host_iir[c], ecns_host_iir_coef(cd)); +#endif +} + +static struct comp_dev *ecns_new(const struct comp_driver *drv, + const struct comp_ipc_config *config, + const void *spec) +{ + struct comp_dev *dev; + struct ecns_comp_data *cd; + + comp_cl_info(drv, "ecns_new"); + + dev = comp_alloc(drv, sizeof(*dev)); + if (!dev) + return NULL; + + dev->ipc_config = *config; + + cd = rzalloc(SOF_MEM_FLAG_USER, sizeof(*cd)); + if (!cd) { + comp_free_device(dev); + return NULL; + } + + const struct ipc4_base_module_cfg *base_cfg = spec; + if (base_cfg) { + memcpy_s(&cd->base_cfg, sizeof(cd->base_cfg), base_cfg, sizeof(*base_cfg)); + cd->sample_rate = base_cfg->audio_fmt.sampling_frequency; + cd->channels = base_cfg->audio_fmt.channels_count; + cd->sample_width = base_cfg->audio_fmt.valid_bit_depth; + } else { + cd->sample_rate = 48000; + cd->channels = ECNS_IN_CHANNELS; + cd->sample_width = 16; + } + + comp_set_drvdata(dev, cd); + dev->direction = SOF_IPC_STREAM_CAPTURE; + dev->direction_set = true; + dev->state = COMP_STATE_READY; + + /* Boosted until the "ECNS capture boost" control says otherwise */ + cd->capture_boost = true; + cd->capture_boost_req = true; + +#if CONFIG_ECNS_KPB_HPF + struct sof_eq_iir_header *hp_hdr = ecns_kpb_iir_coef(cd); + int hp_size = iir_delay_size_df1(hp_hdr); + int32_t *delay; + + if (hp_size < 0) { + rfree(cd); + comp_free_device(dev); + return NULL; + } + + cd->hp_delay = rzalloc(SOF_MEM_FLAG_USER, hp_size); + if (!cd->hp_delay) { + rfree(cd); + comp_free_device(dev); + return NULL; + } + + delay = cd->hp_delay; + iir_init_coef_df1(&cd->hp_iir, hp_hdr); + iir_init_delay_df1(&cd->hp_iir, &delay); +#endif + +#if CONFIG_ECNS_HOST_HPF + /* The 48 kHz and 16 kHz Pin 1 filters have the same section count */ + int host_size = iir_delay_size_df1((struct sof_eq_iir_header *)hp_40hz_20db_48k); + + if (host_size < 0) { + rfree(cd->hp_delay); + rfree(cd); + comp_free_device(dev); + return NULL; + } + + cd->host_delay_size = host_size * ECNS_HOST_MAX_CHANNELS; + cd->host_delay = rzalloc(SOF_MEM_FLAG_USER, cd->host_delay_size); + if (!cd->host_delay) { + rfree(cd->hp_delay); + rfree(cd); + comp_free_device(dev); + return NULL; + } +#endif + + comp_info(dev, "ecns_new: rate=%u in_ch=%u sample_width=%u", + cd->sample_rate, cd->channels, cd->sample_width); + + return dev; +} + +static void ecns_free(struct comp_dev *dev) +{ + struct ecns_comp_data *cd = comp_get_drvdata(dev); + + comp_info(dev, "ecns_free"); +#if CONFIG_ECNS_HOST_HPF + rfree(cd->host_delay); +#endif + rfree(cd->hp_delay); + rfree(cd); + comp_free_device(dev); +} + +static int ecns_params(struct comp_dev *dev, struct sof_ipc_stream_params *params) +{ + struct ecns_comp_data *cd = comp_get_drvdata(dev); + + comp_info(dev, "ecns_params"); + + ipc4_base_module_cfg_to_stream_params(&cd->base_cfg, params); + component_set_nearest_period_frames(dev, params->rate); + + return 0; +} + +#if CONFIG_ECNS_HOST_HPF +static void ecns_host_iir_init(struct ecns_comp_data *cd, uint32_t rate) +{ + struct sof_eq_iir_header *hdr; + int32_t *delay = cd->host_delay; + + cd->host_rate = rate; + hdr = ecns_host_iir_coef(cd); + memset_s(cd->host_delay, cd->host_delay_size, 0, cd->host_delay_size); + for (int c = 0; c < ECNS_HOST_MAX_CHANNELS; c++) { + iir_init_coef_df1(&cd->host_iir[c], hdr); + iir_init_delay_df1(&cd->host_iir[c], &delay); + } +} + +static inline int32_t ecns_host_filter(struct ecns_comp_data *cd, uint32_t c, int32_t x) +{ + return c < ECNS_HOST_MAX_CHANNELS ? iir_df1(&cd->host_iir[c], x) : x; +} +#else +static void ecns_host_iir_init(struct ecns_comp_data *cd, uint32_t rate) +{ +} + +static inline int32_t ecns_host_filter(struct ecns_comp_data *cd, uint32_t c, int32_t x) +{ + return x; +} +#endif + +static int ecns_prepare(struct comp_dev *dev) +{ + struct ecns_comp_data *cd = comp_get_drvdata(dev); + struct comp_buffer *src0 = NULL; + struct comp_buffer *src1 = NULL; + struct comp_buffer *source; + struct comp_buffer *sink; + + comp_info(dev, "ecns_prepare"); + + /* Output buffer formats must match the topology-declared pins: + * pin0 -> KPB mono 16 kHz, pin1 -> host. + * When src1 (48 kHz producer) is present, use dual-rate 16-bit formats. + * When src1 is absent (single 16 kHz DMIC input), use 32-bit formats matching the manifest. + */ + comp_dev_for_each_producer(dev, source) { + uint32_t pin = IPC4_SINK_QUEUE_ID(buf_get_id(source)); + + if (pin == ECNS_PIN_16K_IN && !src0) + src0 = source; + else if (pin == ECNS_PIN_48K_IN && !src1) + src1 = source; + } + + /* Pin 1 output follows the dmic01 input channel count (2 or 4) */ + uint32_t pin1_ch = 2; + + if (src1) + pin1_ch = audio_stream_get_channels(&src1->stream) == 2 ? 2 : 4; + + struct ipc4_audio_format pin0_fmt = { + .sampling_frequency = 16000, + .channels_count = 1, + .depth = src1 ? 16 : 32, + .valid_bit_depth = src1 ? 16 : 32, + .s_type = src1 ? IPC4_TYPE_SIGNED_INTEGER : IPC4_TYPE_MSB_INTEGER, + .interleaving_style = IPC4_CHANNELS_INTERLEAVED, + .ch_map = ECNS_CH_MAP_MONO, + .ch_cfg = IPC4_CHANNEL_CONFIG_MONO, + }; + struct ipc4_audio_format pin1_fmt = { + .sampling_frequency = src1 ? 48000 : 16000, + .channels_count = pin1_ch, + .depth = src1 ? 16 : 32, + .valid_bit_depth = src1 ? 16 : 32, + .s_type = src1 ? IPC4_TYPE_SIGNED_INTEGER : IPC4_TYPE_MSB_INTEGER, + .interleaving_style = IPC4_CHANNELS_INTERLEAVED, + .ch_map = pin1_ch == 4 ? ECNS_CH_MAP_4CH : ECNS_CH_MAP_STEREO, + .ch_cfg = pin1_ch == 4 ? IPC4_CHANNEL_CONFIG_3_POINT_1 : IPC4_CHANNEL_CONFIG_STEREO, + }; + + comp_dev_for_each_consumer(dev, sink) { + uint32_t pin = IPC4_SRC_QUEUE_ID(buf_get_id(sink)); + + if (pin == ECNS_PIN_16K_OUT) + ipc4_update_buffer_format(sink, &pin0_fmt); + else if (pin == ECNS_PIN_48K_OUT) + ipc4_update_buffer_format(sink, &pin1_fmt); + } + + cd->capture_boost = cd->capture_boost_req; + ecns_update_iir_coef(cd); + ecns_host_iir_init(cd, pin1_fmt.sampling_frequency); + + return comp_set_state(dev, COMP_TRIGGER_PREPARE); +} + +static int ecns_reset(struct comp_dev *dev) +{ + struct ecns_comp_data *cd = comp_get_drvdata(dev); + + comp_info(dev, "ecns_reset"); + cd->test_signal_val = 0; +#if CONFIG_ECNS_KPB_HPF + if (cd->hp_delay) { + struct sof_eq_iir_header *hp_hdr = ecns_kpb_iir_coef(cd); + int hp_size = iir_delay_size_df1(hp_hdr); + + if (hp_size > 0) + memset_s(cd->hp_delay, hp_size, 0, hp_size); + } +#endif +#if CONFIG_ECNS_HOST_HPF + if (cd->host_delay) + memset_s(cd->host_delay, cd->host_delay_size, 0, cd->host_delay_size); +#endif + return comp_set_state(dev, COMP_TRIGGER_RESET); +} + +static int ecns_trigger(struct comp_dev *dev, int cmd) +{ + comp_info(dev, "ecns_trigger cmd=%d", cmd); + return comp_set_state(dev, cmd); +} + +#if CONFIG_ECNS_TEST_SIGNAL +/** + * \brief Generate 32-bit test signal on Pin 0 (KPB) + */ +static void ecns_kpb_test_signal_s32(struct ecns_comp_data *cd, + struct comp_buffer *snk0, + uint32_t snk0_ch, + uint32_t frames) +{ + int32_t *snk_ptr = audio_stream_get_wptr(&snk0->stream); + uint32_t remaining = frames; + + while (remaining > 0) { + uint32_t f_snk = audio_stream_frames_without_wrap(&snk0->stream, snk_ptr); + uint32_t n = MIN(remaining, f_snk); + + for (uint32_t i = 0; i < n; i++) { + *snk_ptr++ = ((int32_t)cd->test_signal_val++) << 16; + for (uint32_t c = 1; c < snk0_ch; c++) + *snk_ptr++ = 0; + } + + remaining -= n; + snk_ptr = audio_stream_wrap(&snk0->stream, snk_ptr); + } +} + +/** + * \brief Generate 16-bit test signal on Pin 0 (KPB) + */ +static void ecns_kpb_test_signal_s16(struct ecns_comp_data *cd, + struct comp_buffer *snk0, + uint32_t snk0_ch, + uint32_t frames) +{ + int16_t *snk_ptr = audio_stream_get_wptr(&snk0->stream); + uint32_t remaining = frames; + + while (remaining > 0) { + uint32_t f_snk = audio_stream_frames_without_wrap(&snk0->stream, snk_ptr); + uint32_t n = MIN(remaining, f_snk); + + for (uint32_t i = 0; i < n; i++) { + *snk_ptr++ = (int16_t)cd->test_signal_val++; + for (uint32_t c = 1; c < snk0_ch; c++) + *snk_ptr++ = 0; + } + + remaining -= n; + snk_ptr = audio_stream_wrap(&snk0->stream, snk_ptr); + } +} +#endif + +static inline int32_t ecns_kpb_filter(struct ecns_comp_data *cd, int32_t x) +{ +#if CONFIG_ECNS_KPB_HPF + return iir_df1(&cd->hp_iir, x); +#else + return x; +#endif +} + +/** + * \brief Process 32-bit DMIC input to 32-bit KPB mono output + */ +static void ecns_kpb_s32_to_s32(struct ecns_comp_data *cd, + struct comp_buffer *src0, + uint32_t src0_ch, + struct comp_buffer *snk0, + uint32_t snk0_ch, + uint32_t frames, + uint32_t avg_start, + uint32_t avg_cnt) +{ + int32_t *src_ptr = audio_stream_get_rptr(&src0->stream); + int32_t *snk_ptr = audio_stream_get_wptr(&snk0->stream); + uint32_t remaining = frames; + + while (remaining > 0) { + uint32_t f_src = audio_stream_frames_without_wrap(&src0->stream, src_ptr); + uint32_t f_snk = audio_stream_frames_without_wrap(&snk0->stream, snk_ptr); + uint32_t n = MIN(remaining, MIN(f_src, f_snk)); + + for (uint32_t i = 0; i < n; i++) { + int32_t mono = ecns_mono_s32(src_ptr, avg_start, avg_cnt); + + mono = ecns_kpb_filter(cd, mono); + *snk_ptr++ = mono; + for (uint32_t c = 1; c < snk0_ch; c++) + *snk_ptr++ = 0; + src_ptr += src0_ch; + } + + remaining -= n; + src_ptr = audio_stream_wrap(&src0->stream, src_ptr); + snk_ptr = audio_stream_wrap(&snk0->stream, snk_ptr); + } +} + +/** + * \brief Process 16-bit DMIC input to 32-bit KPB mono output + */ +static void ecns_kpb_s16_to_s32(struct ecns_comp_data *cd, + struct comp_buffer *src0, + uint32_t src0_ch, + struct comp_buffer *snk0, + uint32_t snk0_ch, + uint32_t frames, + uint32_t avg_start, + uint32_t avg_cnt) +{ + int16_t *src_ptr = audio_stream_get_rptr(&src0->stream); + int32_t *snk_ptr = audio_stream_get_wptr(&snk0->stream); + uint32_t remaining = frames; + + while (remaining > 0) { + uint32_t f_src = audio_stream_frames_without_wrap(&src0->stream, src_ptr); + uint32_t f_snk = audio_stream_frames_without_wrap(&snk0->stream, snk_ptr); + uint32_t n = MIN(remaining, MIN(f_src, f_snk)); + + for (uint32_t i = 0; i < n; i++) { + int32_t mono = ((int32_t)ecns_mono_s16(src_ptr, avg_start, avg_cnt)) << 16; + + mono = ecns_kpb_filter(cd, mono); + *snk_ptr++ = mono; + for (uint32_t c = 1; c < snk0_ch; c++) + *snk_ptr++ = 0; + src_ptr += src0_ch; + } + + remaining -= n; + src_ptr = audio_stream_wrap(&src0->stream, src_ptr); + snk_ptr = audio_stream_wrap(&snk0->stream, snk_ptr); + } +} + +/** + * \brief Process 32-bit DMIC input to 16-bit KPB mono output + */ +static void ecns_kpb_s32_to_s16(struct ecns_comp_data *cd, + struct comp_buffer *src0, + uint32_t src0_ch, + struct comp_buffer *snk0, + uint32_t snk0_ch, + uint32_t frames, + uint32_t avg_start, + uint32_t avg_cnt) +{ + int32_t *src_ptr = audio_stream_get_rptr(&src0->stream); + int16_t *snk_ptr = audio_stream_get_wptr(&snk0->stream); + uint32_t remaining = frames; + + while (remaining > 0) { + uint32_t f_src = audio_stream_frames_without_wrap(&src0->stream, src_ptr); + uint32_t f_snk = audio_stream_frames_without_wrap(&snk0->stream, snk_ptr); + uint32_t n = MIN(remaining, MIN(f_src, f_snk)); + + for (uint32_t i = 0; i < n; i++) { + int32_t mono = ecns_mono_s32(src_ptr, avg_start, avg_cnt); + + mono = ecns_kpb_filter(cd, mono); + *snk_ptr++ = sat_int16(Q_SHIFT_RND(mono, 31, 15)); + for (uint32_t c = 1; c < snk0_ch; c++) + *snk_ptr++ = 0; + src_ptr += src0_ch; + } + + remaining -= n; + src_ptr = audio_stream_wrap(&src0->stream, src_ptr); + snk_ptr = audio_stream_wrap(&snk0->stream, snk_ptr); + } +} + +/** + * \brief Process 16-bit DMIC input to 16-bit KPB mono output + */ +static void ecns_kpb_s16_to_s16(struct ecns_comp_data *cd, + struct comp_buffer *src0, + uint32_t src0_ch, + struct comp_buffer *snk0, + uint32_t snk0_ch, + uint32_t frames, + uint32_t avg_start, + uint32_t avg_cnt) +{ + int16_t *src_ptr = audio_stream_get_rptr(&src0->stream); + int16_t *snk_ptr = audio_stream_get_wptr(&snk0->stream); + uint32_t remaining = frames; + + while (remaining > 0) { + uint32_t f_src = audio_stream_frames_without_wrap(&src0->stream, src_ptr); + uint32_t f_snk = audio_stream_frames_without_wrap(&snk0->stream, snk_ptr); + uint32_t n = MIN(remaining, MIN(f_src, f_snk)); + + for (uint32_t i = 0; i < n; i++) { + int32_t mono = ((int32_t)ecns_mono_s16(src_ptr, avg_start, avg_cnt)) << 16; + + mono = ecns_kpb_filter(cd, mono); + *snk_ptr++ = sat_int16(Q_SHIFT_RND(mono, 31, 15)); + for (uint32_t c = 1; c < snk0_ch; c++) + *snk_ptr++ = 0; + src_ptr += src0_ch; + } + + remaining -= n; + src_ptr = audio_stream_wrap(&src0->stream, src_ptr); + snk_ptr = audio_stream_wrap(&snk0->stream, snk_ptr); + } +} + +/** + * \brief Process Pin 0: 16 kHz detection stream -> Mono clean to KPB + * + * Averages selected source channels into a mono stream, filters with the + * built-in 100 Hz HPF (+30 dB gain, CONFIG_ECNS_KPB_HPF), and writes to the sink buffer (either + * 16-bit or 32-bit container). Supports test signal generation when enabled. + * + * \param[in,out] cd ECNS component data + * \param[in] src0 Input buffer from 16 kHz DMIC + * \param[in,out] snk0 Output buffer to KPB + * \return Number of frames produced to snk0 + */ +static uint32_t ecns_process_kpb(struct ecns_comp_data *cd, + struct comp_buffer *src0, + struct comp_buffer *snk0) +{ + uint32_t src0_ch = audio_stream_get_channels(&src0->stream); + if (!src0_ch) + src0_ch = 4; + uint32_t src_sample_bytes = audio_stream_sample_bytes(&src0->stream); + if (!src_sample_bytes) + src_sample_bytes = sizeof(int32_t); + uint32_t src0_frame_bytes = src0_ch * src_sample_bytes; + uint32_t avail0_bytes = audio_stream_get_avail_bytes(&src0->stream); + uint32_t avail0_frames = avail0_bytes / src0_frame_bytes; + + uint32_t snk0_ch = audio_stream_get_channels(&snk0->stream); + if (!snk0_ch) + snk0_ch = 1; + uint32_t snk_sample_bytes0 = audio_stream_sample_bytes(&snk0->stream); + if (!snk_sample_bytes0) + snk_sample_bytes0 = sizeof(int32_t); + uint32_t snk0_frame_bytes = snk0_ch * snk_sample_bytes0; + uint32_t free0_bytes = audio_stream_get_free_bytes(&snk0->stream); + uint32_t free0_frames = free0_bytes / snk0_frame_bytes; + + /* Drain all available input bounded by sink space; a one-period cap + * would let the source ring overflow under DMIC vs DSP clock drift. + */ + uint32_t frames0 = MIN(avail0_frames, free0_frames); + if (!frames0) + return 0; + + uint32_t src_bytes = frames0 * src0_frame_bytes; + uint32_t snk_bytes = frames0 * snk0_frame_bytes; + + buffer_stream_invalidate(src0, src_bytes); + + uint32_t avg_start, avg_cnt; + + ecns_mono_window(src0_ch, &avg_start, &avg_cnt); + +#if CONFIG_ECNS_TEST_SIGNAL + if (cd->test_signal_enabled) { + if (snk_sample_bytes0 == sizeof(int32_t)) + ecns_kpb_test_signal_s32(cd, snk0, snk0_ch, frames0); + else + ecns_kpb_test_signal_s16(cd, snk0, snk0_ch, frames0); + } else +#endif + if (snk_sample_bytes0 == sizeof(int32_t)) { + if (src_sample_bytes == sizeof(int32_t)) + ecns_kpb_s32_to_s32(cd, src0, src0_ch, snk0, snk0_ch, frames0, + avg_start, avg_cnt); + else + ecns_kpb_s16_to_s32(cd, src0, src0_ch, snk0, snk0_ch, frames0, + avg_start, avg_cnt); + } else { + if (src_sample_bytes == sizeof(int32_t)) + ecns_kpb_s32_to_s16(cd, src0, src0_ch, snk0, snk0_ch, frames0, + avg_start, avg_cnt); + else + ecns_kpb_s16_to_s16(cd, src0, src0_ch, snk0, snk0_ch, frames0, + avg_start, avg_cnt); + } + + buffer_stream_writeback(snk0, snk_bytes); + comp_update_buffer_produce(snk0, snk_bytes); + + return frames0; +} + +/** + * \brief Filter 32-bit input to 32-bit host capture sink + */ +static void ecns_host_s32_to_s32(struct ecns_comp_data *cd, + struct comp_buffer *in1, + uint32_t in1_ch, + struct comp_buffer *snk1, + uint32_t snk1_ch, + uint32_t frames) +{ + int32_t *src_ptr = audio_stream_get_rptr(&in1->stream); + int32_t *snk_ptr = audio_stream_get_wptr(&snk1->stream); + uint32_t remaining = frames; + + while (remaining > 0) { + uint32_t f_src = audio_stream_frames_without_wrap(&in1->stream, src_ptr); + uint32_t f_snk = audio_stream_frames_without_wrap(&snk1->stream, snk_ptr); + uint32_t n = MIN(remaining, MIN(f_src, f_snk)); + + for (uint32_t i = 0; i < n; i++) { + for (uint32_t c = 0; c < snk1_ch; c++) + snk_ptr[c] = (c < in1_ch) ? ecns_host_filter(cd, c, src_ptr[c]) : 0; + snk_ptr += snk1_ch; + src_ptr += in1_ch; + } + + remaining -= n; + src_ptr = audio_stream_wrap(&in1->stream, src_ptr); + snk_ptr = audio_stream_wrap(&snk1->stream, snk_ptr); + } +} + +/** + * \brief Filter 16-bit input to 32-bit host capture sink + */ +static void ecns_host_s16_to_s32(struct ecns_comp_data *cd, + struct comp_buffer *in1, + uint32_t in1_ch, + struct comp_buffer *snk1, + uint32_t snk1_ch, + uint32_t frames) +{ + int16_t *src_ptr = audio_stream_get_rptr(&in1->stream); + int32_t *snk_ptr = audio_stream_get_wptr(&snk1->stream); + uint32_t remaining = frames; + + while (remaining > 0) { + uint32_t f_src = audio_stream_frames_without_wrap(&in1->stream, src_ptr); + uint32_t f_snk = audio_stream_frames_without_wrap(&snk1->stream, snk_ptr); + uint32_t n = MIN(remaining, MIN(f_src, f_snk)); + + for (uint32_t i = 0; i < n; i++) { + for (uint32_t c = 0; c < snk1_ch; c++) + snk_ptr[c] = (c < in1_ch) ? + ecns_host_filter(cd, c, ((int32_t)src_ptr[c]) << 16) : 0; + snk_ptr += snk1_ch; + src_ptr += in1_ch; + } + + remaining -= n; + src_ptr = audio_stream_wrap(&in1->stream, src_ptr); + snk_ptr = audio_stream_wrap(&snk1->stream, snk_ptr); + } +} + +/** + * \brief Filter 32-bit input to 16-bit host capture sink + */ +static void ecns_host_s32_to_s16(struct ecns_comp_data *cd, + struct comp_buffer *in1, + uint32_t in1_ch, + struct comp_buffer *snk1, + uint32_t snk1_ch, + uint32_t frames) +{ + int32_t *src_ptr = audio_stream_get_rptr(&in1->stream); + int16_t *snk_ptr = audio_stream_get_wptr(&snk1->stream); + uint32_t remaining = frames; + + while (remaining > 0) { + uint32_t f_src = audio_stream_frames_without_wrap(&in1->stream, src_ptr); + uint32_t f_snk = audio_stream_frames_without_wrap(&snk1->stream, snk_ptr); + uint32_t n = MIN(remaining, MIN(f_src, f_snk)); + + for (uint32_t i = 0; i < n; i++) { + for (uint32_t c = 0; c < snk1_ch; c++) + snk_ptr[c] = (c < in1_ch) ? + sat_int16(Q_SHIFT_RND(ecns_host_filter(cd, c, src_ptr[c]), + 31, 15)) : 0; + snk_ptr += snk1_ch; + src_ptr += in1_ch; + } + + remaining -= n; + src_ptr = audio_stream_wrap(&in1->stream, src_ptr); + snk_ptr = audio_stream_wrap(&snk1->stream, snk_ptr); + } +} + +/** + * \brief Filter 16-bit input to 16-bit host capture sink + */ +static void ecns_host_s16_to_s16(struct ecns_comp_data *cd, + struct comp_buffer *in1, + uint32_t in1_ch, + struct comp_buffer *snk1, + uint32_t snk1_ch, + uint32_t frames) +{ + int16_t *src_ptr = audio_stream_get_rptr(&in1->stream); + int16_t *snk_ptr = audio_stream_get_wptr(&snk1->stream); + uint32_t remaining = frames; + + while (remaining > 0) { + uint32_t f_src = audio_stream_frames_without_wrap(&in1->stream, src_ptr); + uint32_t f_snk = audio_stream_frames_without_wrap(&snk1->stream, snk_ptr); + uint32_t n = MIN(remaining, MIN(f_src, f_snk)); + + for (uint32_t i = 0; i < n; i++) { + for (uint32_t c = 0; c < snk1_ch; c++) + snk_ptr[c] = (c < in1_ch) ? + sat_int16(Q_SHIFT_RND(ecns_host_filter(cd, c, + ((int32_t)src_ptr[c]) << 16), + 31, 15)) : 0; + snk_ptr += snk1_ch; + src_ptr += in1_ch; + } + + remaining -= n; + src_ptr = audio_stream_wrap(&in1->stream, src_ptr); + snk_ptr = audio_stream_wrap(&snk1->stream, snk_ptr); + } +} + +/** + * \brief Process Pin 1: 48 kHz host capture stream + * + * Filters the audio from the input producer (48 kHz dmic01 or fallback + * src0) with the built-in 40 Hz HPF (+20 dB gain) per channel and writes + * it to the host sink buffer (supporting s16 or s32). + * + * \param[in,out] cd ECNS component data + * \param[in] in1 Input buffer + * \param[in,out] snk1 Output buffer to host + * \return Number of frames produced to snk1 + */ +static uint32_t ecns_process_host(struct ecns_comp_data *cd, + struct comp_buffer *in1, + struct comp_buffer *snk1) +{ + uint32_t in1_ch = audio_stream_get_channels(&in1->stream); + if (!in1_ch) + in1_ch = 2; + uint32_t in1_sample_bytes = audio_stream_sample_bytes(&in1->stream); + if (!in1_sample_bytes) + in1_sample_bytes = sizeof(int32_t); + uint32_t in1_frame_bytes = in1_ch * in1_sample_bytes; + uint32_t avail1_bytes = audio_stream_get_avail_bytes(&in1->stream); + uint32_t avail1_frames = avail1_bytes / in1_frame_bytes; + + uint32_t snk1_ch = audio_stream_get_channels(&snk1->stream); + if (!snk1_ch) + snk1_ch = 2; + uint32_t snk_sample_bytes1 = audio_stream_sample_bytes(&snk1->stream); + if (!snk_sample_bytes1) + snk_sample_bytes1 = sizeof(int32_t); + uint32_t snk1_frame_bytes = snk1_ch * snk_sample_bytes1; + uint32_t free1_bytes = audio_stream_get_free_bytes(&snk1->stream); + uint32_t free1_frames = free1_bytes / snk1_frame_bytes; + + /* Drain all available input bounded by sink space; a one-period cap + * would let the source ring overflow under DMIC vs DSP clock drift. + */ + uint32_t frames1 = MIN(avail1_frames, free1_frames); + if (!frames1) + return 0; + + uint32_t in1_bytes = frames1 * in1_frame_bytes; + uint32_t snk_bytes = frames1 * snk1_frame_bytes; + + buffer_stream_invalidate(in1, in1_bytes); + + if (snk_sample_bytes1 == sizeof(int32_t)) { + if (in1_sample_bytes == sizeof(int32_t)) + ecns_host_s32_to_s32(cd, in1, in1_ch, snk1, snk1_ch, frames1); + else + ecns_host_s16_to_s32(cd, in1, in1_ch, snk1, snk1_ch, frames1); + } else { + if (in1_sample_bytes == sizeof(int32_t)) + ecns_host_s32_to_s16(cd, in1, in1_ch, snk1, snk1_ch, frames1); + else + ecns_host_s16_to_s16(cd, in1, in1_ch, snk1, snk1_ch, frames1); + } + + buffer_stream_writeback(snk1, snk_bytes); + comp_update_buffer_produce(snk1, snk_bytes); + + return frames1; +} + +/** + * \brief Consume from ECNS source buffers and drain inactive inputs + * + * Updates consumption for produced audio frames and drains unconsumed + * input data when downstream consumer sinks are inactive or full to + * prevent upstream DAI FIFO overruns. + * + * \param[in,out] src0 16 kHz input buffer + * \param[in,out] src1 48 kHz input buffer + * \param[in] snk0 Pin 0 (KPB) output buffer + * \param[in] snk1 Pin 1 (Host) output buffer + * \param[in] frames0_produced Frames produced to snk0 + * \param[in] frames1_produced Frames produced to snk1 + */ +static void ecns_consume_sources(struct comp_buffer *src0, + struct comp_buffer *src1, + struct comp_buffer *snk0, + struct comp_buffer *snk1, + uint32_t frames0_produced, + uint32_t frames1_produced) +{ + uint32_t snk0_free = snk0 ? audio_stream_get_free_bytes(&snk0->stream) : 0; + uint32_t snk1_free = snk1 ? audio_stream_get_free_bytes(&snk1->stream) : 0; + + if (src1) { + uint32_t src1_ch = audio_stream_get_channels(&src1->stream); + if (!src1_ch) + src1_ch = 4; + uint32_t src1_sb = audio_stream_sample_bytes(&src1->stream); + if (!src1_sb) + src1_sb = sizeof(int32_t); + uint32_t src1_fb = src1_ch * src1_sb; + + if (frames1_produced > 0) { + comp_update_buffer_consume(src1, frames1_produced * src1_fb); + } else if (!snk1 || comp_buffer_get_sink_state(snk1) != COMP_STATE_ACTIVE || !snk1_free) { + uint32_t avail = audio_stream_get_avail_bytes(&src1->stream); + + if (src1_fb && avail >= src1_fb) { + avail = ROUND_DOWN(avail, src1_fb); + comp_update_buffer_consume(src1, avail); + } + } + } + + if (src0) { + uint32_t src0_ch = audio_stream_get_channels(&src0->stream); + if (!src0_ch) + src0_ch = 4; + uint32_t src0_sb = audio_stream_sample_bytes(&src0->stream); + if (!src0_sb) + src0_sb = sizeof(int32_t); + uint32_t src0_fb = src0_ch * src0_sb; + + uint32_t consumed0 = frames0_produced; + if (!src1 && frames1_produced > consumed0) + consumed0 = frames1_produced; + + if (consumed0 > 0) { + comp_update_buffer_consume(src0, consumed0 * src0_fb); + } else if ((!snk0 || comp_buffer_get_sink_state(snk0) != COMP_STATE_ACTIVE || !snk0_free) && + (src1 || !snk1 || comp_buffer_get_sink_state(snk1) != COMP_STATE_ACTIVE || !snk1_free)) { + uint32_t avail = audio_stream_get_avail_bytes(&src0->stream); + + if (src0_fb && avail >= src0_fb) { + avail = ROUND_DOWN(avail, src0_fb); + comp_update_buffer_consume(src0, avail); + } + } + } +} + +/* + * Main 20ms DP processing function: + * Consumes: + * - Pin 0 in: 16 kHz stream from dmic16k (2 or 4 channels, 16/32-bit) + * - Pin 1 in: 48 kHz stream from dmic01 (2 or 4 channels, 16/32-bit) + * Produces: + * - Pin 0 out: 16 kHz mono clean stream to KPB (averaged mono per Kconfig, 320 frames per 20ms) + * - Pin 1 out: 48 kHz stereo/4ch clean stream to Host PCM 11 (40 Hz HPF +20 dB, 960 frames per 20ms) + * Sampling rates remain identical between input and output on each pin. + */ +static int ecns_copy(struct comp_dev *dev) +{ + struct ecns_comp_data *cd = comp_get_drvdata(dev); + struct comp_buffer *source; + struct comp_buffer *sink; + struct comp_buffer *src0 = NULL; + struct comp_buffer *src1 = NULL; + struct comp_buffer *snk0 = NULL; + struct comp_buffer *snk1 = NULL; + uint32_t frames0_produced = 0; + uint32_t frames1_produced = 0; + + if (list_is_empty(&dev->bsource_list)) + return 0; + + /* Apply a capture boost change from the control in the processing context */ + if (cd->capture_boost != cd->capture_boost_req) { + cd->capture_boost = cd->capture_boost_req; + ecns_update_iir_coef(cd); + comp_info(dev, "capture boost %s", + cd->capture_boost ? "enabled" : "disabled"); + } + + /* Identify input pins (producers to ECNS) */ + comp_dev_for_each_producer(dev, source) { + uint32_t pin = IPC4_SINK_QUEUE_ID(buf_get_id(source)); + + if (pin == ECNS_PIN_16K_IN && !src0) + src0 = source; + else if (pin == ECNS_PIN_48K_IN && !src1) + src1 = source; + } + + /* Identify output pins (consumers from ECNS) */ + comp_dev_for_each_consumer(dev, sink) { + uint32_t pin = IPC4_SRC_QUEUE_ID(buf_get_id(sink)); + + if (pin == ECNS_PIN_16K_OUT && !snk0) + snk0 = sink; + else if (pin == ECNS_PIN_48K_OUT && !snk1) + snk1 = sink; + } + + /* Process Pin 0: 16 kHz stream -> Mono clean to KPB */ + if (src0 && snk0) + frames0_produced = ecns_process_kpb(cd, src0, snk0); + + /* Process Pin 1: 48 kHz stream -> Host capture */ + struct comp_buffer *in1 = src1 ? src1 : src0; + + if (in1 && snk1) + frames1_produced = ecns_process_host(cd, in1, snk1); + + /* Consume from source buffers */ + ecns_consume_sources(src0, src1, snk0, snk1, frames0_produced, frames1_produced); + + return 0; +} + +static int ecns_get_attribute(struct comp_dev *dev, uint32_t type, void *value) +{ + struct ecns_comp_data *cd = comp_get_drvdata(dev); + + if (type == COMP_ATTR_BASE_CONFIG) { + *(struct ipc4_base_module_cfg *)value = cd->base_cfg; + return 0; + } + return -EINVAL; +} + +static int ecns_set_large_config(struct comp_dev *dev, + uint32_t param_id, + bool first_block, + bool last_block, + uint32_t data_offset, + const char *data) +{ + struct ecns_comp_data *cd = comp_get_drvdata(dev); + + switch (param_id) { + case SOF_IPC4_SWITCH_CONTROL_PARAM_ID: { + const struct sof_ipc4_control_msg_payload *cp = + (const struct sof_ipc4_control_msg_payload *)data; + + if (cp->num_elems < 1) + return -EINVAL; + + switch (cp->id) { + case ECNS_CTRL_INDEX_TEST_SIGNAL: +#if CONFIG_ECNS_TEST_SIGNAL + cd->test_signal_enabled = cp->chanv[0].value ? 1 : 0; + if (cd->test_signal_enabled) + cd->test_signal_val = 0; + + comp_info(dev, "test signal %s (kpb mono output)", + cd->test_signal_enabled ? "enabled" : "disabled"); +#else + if (cp->chanv[0].value) + comp_warn(dev, "test signal not supported (CONFIG_ECNS_TEST_SIGNAL=n)"); +#endif + return 0; + case ECNS_CTRL_INDEX_CAPTURE_BOOST: + cd->capture_boost_req = !!cp->chanv[0].value; + return 0; + default: + comp_err(dev, "illegal switch control id %u", cp->id); + return -EINVAL; + } + } + default: + return -EINVAL; + } +} + +static int ecns_get_large_config(struct comp_dev *dev, + uint32_t param_id, + bool first_block, + bool last_block, + uint32_t *data_offset, + char *data) +{ + struct ecns_comp_data *cd = comp_get_drvdata(dev); + + switch (param_id) { + case SOF_IPC4_SWITCH_CONTROL_PARAM_ID: { + struct sof_ipc4_control_msg_payload *cp = + (struct sof_ipc4_control_msg_payload *)data; + uint16_t ctl_id = cp->id; + uint32_t resp_size = sizeof(struct sof_ipc4_control_msg_payload) + + sizeof(struct sof_ipc4_ctrl_value_chan); + + if (resp_size > *data_offset) { + comp_err(dev, "wrong switch control response size %u vs %u", + resp_size, *data_offset); + return -EINVAL; + } + + *data_offset = resp_size; + memset_s(cp, resp_size, 0, resp_size); + cp->id = ctl_id; + cp->num_elems = 1; + cp->chanv[0].channel = 0; + cp->chanv[0].value = ctl_id == ECNS_CTRL_INDEX_CAPTURE_BOOST ? + cd->capture_boost_req : cd->test_signal_enabled; + return 0; + } + default: + return -EINVAL; + } +} + +static const struct comp_driver ecns_drv = { + .type = SOF_COMP_NONE, + .uid = SOF_RT_UUID(ecns_uuid), +#if !CONFIG_ZEPHYR_LOG + .tctx = &ecns_tr, +#endif + .ops = { + .create = ecns_new, + .free = ecns_free, + .params = ecns_params, + .prepare = ecns_prepare, + .reset = ecns_reset, + .trigger = ecns_trigger, + .copy = ecns_copy, + .set_large_config = ecns_set_large_config, + .get_large_config = ecns_get_large_config, + .get_attribute = ecns_get_attribute, + }, +}; + +static struct comp_driver_info ecns_info = { + .drv = &ecns_drv, +}; + +UT_STATIC void sys_comp_ecns_init(void) +{ + comp_register(&ecns_info); +} + +DECLARE_MODULE(sys_comp_ecns_init); +SOF_MODULE_INIT(ecns, sys_comp_ecns_init); diff --git a/src/audio/ecns/ecns.toml b/src/audio/ecns/ecns.toml new file mode 100644 index 000000000000..123c93505311 --- /dev/null +++ b/src/audio/ecns/ecns.toml @@ -0,0 +1,19 @@ + [[module.entry]] + name = "ECNS" + uuid = UUIDREG_STR_ECNS + affinity_mask = "0x1" + instance_count = "2" + domain_types = "0" + load_type = "0" + module_type = "0xB" + auto_start = "0" + sched_caps = [1, 0x00008000] + + REM # pin = [dir, type, sample rate, size, container, channel-cfg] + pin = [0, 0, 0xfeef, 0xf, 0xf, 0x45ff, + 1, 0, 0xfeef, 0xf, 0xa, 0x45ff] + + REM # mod_cfg [PAR_0 PAR_1 PAR_2 PAR_3 IS_BYTES CPS IBS OBS MOD_FLAGS CPC OBLS] + mod_cfg = [0, 0, 0, 0, 14400, 1114000, 2560, 640, 0, 0, 0] + + index = __COUNTER__ diff --git a/src/include/sof/audio/component.h b/src/include/sof/audio/component.h index e879888053b0..395da1105d9a 100644 --- a/src/include/sof/audio/component.h +++ b/src/include/sof/audio/component.h @@ -883,6 +883,7 @@ static inline struct processing_module *comp_mod(const struct comp_dev *dev) /* declared modules */ void sys_comp_dai_init(void); +void sys_comp_ecns_init(void); void sys_comp_host_init(void); void sys_comp_kpb_init(void); void sys_comp_selector_init(void); diff --git a/src/include/sof/audio/ecns.h b/src/include/sof/audio/ecns.h new file mode 100644 index 000000000000..7bf2ab983dda --- /dev/null +++ b/src/include/sof/audio/ecns.h @@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2026 Intel Corporation. All rights reserved. + */ + +#ifndef __SOF_AUDIO_ECNS_H__ +#define __SOF_AUDIO_ECNS_H__ + +#include +#include + +#define ECNS_IN_CHANNELS 2 +#define ECNS_PIN_KPB 0 /* Output Pin 0: Channel 0 mono to KPB */ +#define ECNS_PIN_HOST 1 /* Output Pin 1: Channels 0,1 stereo to Host Copier */ + +#define ECNS_PIN_16K_IN 0 /* Input Pin 0: 16 kHz stream from dmic16k */ +#define ECNS_PIN_48K_IN 1 /* Input Pin 1: 48 kHz stream from dmic01 */ +#define ECNS_PIN_16K_OUT 0 /* Output Pin 0: 16 kHz mono clean to KPB */ +#define ECNS_PIN_48K_OUT 1 /* Output Pin 1: 48 kHz stereo clean to Host Copier */ + +#define ECNS_PERIOD_MS 20 +#define ECNS_FRAME_SAMPLES_16K 320 /* 20ms @ 16 kHz */ +#define ECNS_FRAME_SAMPLES_48K 960 /* 20ms @ 48 kHz */ +#define ECNS_FRAME_SAMPLES ECNS_FRAME_SAMPLES_16K + +/* IPC4 channel maps, one nibble per channel index, 0xF marks unused */ +#define ECNS_CH_MAP_MONO 0xFFFFFFF0 +#define ECNS_CH_MAP_STEREO 0xFFFFFF10 +#define ECNS_CH_MAP_4CH 0xFFFF3210 + +void sys_comp_ecns_init(void); + +#endif /* __SOF_AUDIO_ECNS_H__ */ diff --git a/tools/rimage/config/nvl.toml.h b/tools/rimage/config/nvl.toml.h index e1f4ebfb6336..063364295810 100644 --- a/tools/rimage/config/nvl.toml.h +++ b/tools/rimage/config/nvl.toml.h @@ -189,5 +189,9 @@ index = __COUNTER__ #include