Skip to content

audio: webrtc_aec: add per instance configuration and aec3 for stereo reference - #11618

Draft
singalsu wants to merge 17 commits into
thesofproject:mainfrom
singalsu:webrtc_aec3_update
Draft

singalsu wants to merge 17 commits into
thesofproject:mainfrom
singalsu:webrtc_aec3_update

Conversation

@singalsu

@singalsu singalsu commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

The float AEC trusts the reported sound card delay, and the hardcoded
20 ms was larger than the real reference to echo delay, so the echo
arrived before the aligned reference. The filter update is also
clamped per block, so a quiet reference made convergence take tens of
seconds. Add Kconfig options for the reported delay, default 0 ms, and
for a linear reference gain, default 16. The echo reference latency
also varies by a few ms between stream starts, so add a microphone
delay line, default 8 ms, that keeps the echo causal. This gives a
stable delay estimate and about 6 - 13 dB ERLE with music.

Add optional debug traces of the AEC delay estimate with a coarse
energy envelope cross-correlation lag, the reference and microphone
peak levels, and once per second the ERLE and levels over frames with
an active reference.

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
The Zephyr minimal C++ library, enabled with CONFIG_CPP e.g. for the
microwakeword module, defines the global new and delete operators. The
AEC3 shims define them too, so a firmware with both AEC3 and MWW fails
to link with multiple definitions.

Wrap the operators with the linker --wrap option when
CONFIG_MINIMAL_LIBCPP is set. All new and delete calls in the image
then use the webrtc_malloc() and webrtc_free() versions in the AEC3
shims instead of the small libc arena. Throwing new calls k_panic() on
failure since exceptions are disabled. The __cxa_atexit() and
__cxa_pure_virtual() shims are built only without CONFIG_CPP since the
Zephyr C++ runtime provides them.

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
Make the output channel count follow the sink format. With a mono
output and a multi-channel microphone the microphone channels are
averaged to mono before the AEC. This allows e.g. a stereo DMIC with a
stereo echo reference to feed a mono WoV KPB without a separate micsel
downmix in front of the AEC. Both input pins then have the same format,
which the kernel requires since it matches the stream parameters only
against the pin 0 formats when the input formats differ. With AEC3 the
processing runs as 2R x 1C.

The source channel count is stored in the new mic_in_channels and
mic_channels is the processed and output channel count. Without the
downmix a sink channel count different from the microphone channel
count is rejected in prepare().

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
Count the bytes allocated by the WebRTC library in webrtc_malloc() and
webrtc_calloc() and print the total after the backend configure in
prepare(). A failed allocation is logged with the requested size and
the amount allocated before it.

This helps to size the heap for the AEC3 backend, e.g. 2R x 1C AEC3
at 16 kHz allocates about 600 kB, and to see the reason when AEC3
panics on an out of memory new.

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
The webrtc_aec sources are compiled with the Clang target feature -fp,
so all float operations are soft-float library calls. For AEC3 this
is far too slow, 2R x 1C AEC3 at 16 kHz needs about 13.6 ms of DSP time
per 10 ms frame on PTL, i.e. about 600 MCPS.

Add CONFIG_WEBRTC_AEC_AEC3_HW_FPU to drop the flag from the AEC3
build. The ACE 3.0 core has the FPU in the HiFi4 audio engine
coprocessor, and its state including the FCR and FSR registers is
preserved with the HiFi register sharing. The default is n until the
hardware FPU build has been tested.

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
Add an overlay to build the WebRTC AEC3 backend into the base firmware
for a stereo echo reference with a mono microphone (2R x 1C) at 16 kHz.

The stereo content detector is disabled since it increases the peak
AEC3 memory from about 600 kB to about 870 kB. AEC3 allocates from the
system heap without CONFIG_USERSPACE, so the heap is increased to 1 MB.
The hardware FPU is used since soft-float AEC3 is slower than real
time. The tone component is enabled for the siggen in the echo
reference pipeline.

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
Destroy the previous EchoCanceller3 instance and audio buffers before
creating the new ones. The unique_ptr assignment frees the old object
only after the new one is constructed, so a re-creation needed heap
for two instances, e.g. about 1.2 MB for 2R x 1C at 16 kHz.

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
…ut options

Add Kconfig options to tune AEC3 for a real time stereo echo reference
on the DSP:

- WEBRTC_AEC_AEC3_COARSE_FILTER_BLOCKS sets a shorter coarse filter.
  It is always adapted, also when its output is not used, so a short
  coarse filter saves MCPS.
- WEBRTC_AEC_AEC3_ADAPT_RATE_PCT slows down the converged state
  adaptation, the coarse filter step and the refined filter leakage,
  for a more stable filter during double talk and music.
- WEBRTC_AEC_MIC_DELAY_MS is used also with AEC3 as the fixed capture
  delay. AEC3 cancels nothing if the echo arrives less than about 3 ms
  after the reference, as happens on PTL.
- WEBRTC_AEC_AEC3_LINEAR_OUTPUT outputs the linear filter result
  without the residual echo suppression and comfort noise, for a more
  linear signal to keyword and speech recognition. The suppressor is
  then not run, which saves MCPS.

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
With WEBRTC_AEC_PERF_TELEMETRY trace every 1 s the AEC3 delay
estimate, ERL, ERLE and the maximum frame load in the last second.
Support WEBRTC_AEC_ERLE_TRACE also with AEC3, with the same measure
and trace format as the Float AEC. The mean of the reference channels
is used, and with the linear output it is the linear filter ERLE.

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
Without C++ exceptions a failed allocation inside AEC3 panics the DSP.
With WEBRTC_AEC_AEC3_ARENA_SIZE the backend allocates one block for the
instance in prepare, which fails gracefully with -ENOMEM, and creates
AEC3 into it. The arena memory is not freed one by one; the whole arena
is rewound when the instance is re-created. On reset the instance is
destroyed and created again from the start of the arena, so stopping a
stream needs no more heap. Allocations that do not fit come from the
heap, and the needed size is traced.

The C++ new and delete operators are global and serve also the other
C++ modules, so they use the arena only while it is open for the
creating thread, and the system heap otherwise.

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
Enable the AEC3 arena, linear output without the suppressor, 8 block
refined and 4 block coarse filters, half speed adaptation, fixed 8 ms
external delay with an 8 ms microphone delay, and the ERLE trace.

The AEC3 matched filter delay search takes about 160 of 205 MCPS, the
fixed delay brings the load down to about 48 MCPS. With the 8 ms
microphone delay AEC3 measured a 16 ms delay on PTL, a fixed delay of
4 - 18 ms cancels it.

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
Allocate the FIFOs, the 16 kHz buffers and the resampler states in
prepare() as one block sized for the negotiated channel counts and
rate instead of worst case arrays for four channels at 48 kHz in the
module data. The 16 kHz buffers and resampler states exist only when
resampling. The FIFOs hold two 10 ms frames at the pipeline rate. The
backend data and instance are created in prepare() too, and reset()
frees everything, so a stopped stream holds no AEC memory. The AEC3
float frame buffers are sized for the configured channels.

Replace the global active module, allocation counter and AEC3 arena
with a per instance memory context. The module enters the context
around the backend calls and the WebRTC library allocations of that
thread go to the module allocator of the instance. Each AEC3 instance
has its own arena, a list of the arenas lets a C++ delete in any
thread find arena memory. This allows several AEC instances, e.g. a
mono Float AEC and a stereo reference AEC3, at the same time.

The backend reset operation is removed since prepare() now creates the
backend from scratch. The unit test prepares again after the reset.

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
Make the Float AEC, AEC3 and AECm backend Kconfig options independent
so that several backends can be built into the module, and select the
backend of an instance in prepare(). The new WEBRTC_AEC_BACKEND_SELECT
choice selects automatically by the echo reference channel count, AEC3
for a multi-channel reference and the Float AEC for a mono reference,
or always one backend. This allows e.g. a stereo reference WoV capture
with AEC3 and a mono reference VoIP capture with the Float AEC side by
side. The linker removes a backend that the selection cannot pick.

Each backend exports its own operations and default echo path delay.
WEBRTC_AEC_DELAY_MS is now only for the Float AEC with its 0 - 15 ms
range, AEC3 and AECm get WEBRTC_AEC_AEC3_DELAY_MS and
WEBRTC_AEC_AECM_DELAY_MS. The AEC3 overlay is updated for this, and
the unit test configuration uses a Float AEC delay inside its range.

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
Update webrtc-apm to df7293e5dd7113ddbad0a886cd8108801355e40f for the native float AEC3 and the runtime
selection of the Float AEC linear only output and step size with
WebRtcAec_enable_linear_only() and WebRtcAec_set_normal_mu(). The
soft-float AEC3 optimizations are reverted, AEC3 is built with the
hardware FPU.

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
Add struct webrtc_aec_config, set with a bytes control of IPC4 param_id
1, to configure each instance separately. It selects the backend,
automatic by the echo reference channel count or a built backend, and
sets the tuning that was so far only in Kconfig: the suppression level,
microphone delay and reference downmix, the Float AEC delay, reference
gain, linear output without residual echo suppression, step size and
delay agnostic mode, the AEC3 delay, external delay estimation, filter
lengths, linear output, adaptation rate, stereo content detector and
arena size, and the AECm delay and filter length.

The Kconfig options are now the defaults of an instance without a
blob, and a blob shorter than the struct sets only its first fields.
The blob is checked when received and used in the next prepare. The
new WEBRTC_AEC_FLOAT_LINEAR_OUTPUT is the default of the Float AEC
linear output. The Float AEC microphone delay line is allocated in
prepare for the configured delay. The AEC3 arena is available whenever
AEC3 is built, its size comes from the configuration and zero
allocates AEC3 from the heap.

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
Add the bytes control of struct webrtc_aec_config to the webrtc-aec
widget class and the script tune/sof_webrtc_aec_blobs.m that exports
the blobs for the topology and for sof-ctl: default with the Kconfig
defaults, wov with the Float AEC linear output and the AEC3 settings
for a stereo echo reference, and voip with the residual echo
suppression enabled.

The SDW_SPK_AEC_CAPTURE_PCM AEC gets the default blob, selected with
the new SDW_SPK_AEC_PARAMS. The control is named "AEC config".

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
Add SDW_SPK_AEC_STEREO_REF to build the SoundWire speaker AEC capture
topology for an AEC with a stereo echo reference, such as WebRTC AEC3.
The default false keeps the current mono topologies unchanged.

With true the microphone and the echo reference are not downmixed with
micsel, the SRC outputs feed the AEC directly. Both AEC input pins are
16 kHz stereo since the kernel does not support different formats on
the input pins. The AEC output is mono, the AEC averages the
microphone channels.

Add the target sof-ptl-rt713-l3-rt1320-l12-stereo-aec. It uses the wov
configuration blob with the AEC3 settings for real time processing
with a stereo echo reference.

Signed-off-by: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical allocator and arena-lifecycle defects can cause userspace faults and use-after-free, while the undersized stereo private heap prevents capture preparation.

3 open issues
What changed in this PR

Adds per-instance WebRTC echo-cancellation configuration and stereo-reference AEC3 support to SOF’s audio processing and SoundWire topologies.

Changes:

  • Adds runtime backend selection, tuning controls, and configuration presets.
  • Sizes processing memory during preparation and releases it on reset.
  • Adds stereo capture topology support, AEC3 tuning options, and lifecycle test updates.
File Description
west.yml Updates the WebRTC dependency revision.
tools/​topology/​topology2/​platform/​intel/​sdw-amp-aec.conf Adds stereo routing and configuration controls.
tools/​topology/​topology2/​include/​components/​webrtc-aec/​wov.conf Adds wake-on-voice tuning.
tools/​topology/​topology2/​include/​components/​webrtc-aec/​voip.conf Adds voice-call tuning.
tools/​topology/​topology2/​include/​components/​webrtc-aec/​default.conf Adds default tuning.
tools/​topology/​topology2/​include/​components/​webrtc-aec.conf Defines the bytes configuration control.
tools/​topology/​topology2/​development/​tplg-targets.cmake Registers a stereo-reference development topology.
tools/​topology/​topology2/​cavs-sdw.conf Adds stereo-reference and preset defaults.
tools/​ctl/​ipc4/​webrtc_aec/​wov.txt Adds the wake-on-voice control blob.
tools/​ctl/​ipc4/​webrtc_aec/​voip.txt Adds the voice-call control blob.
tools/​ctl/​ipc4/​webrtc_aec/​default.txt Adds the default control blob.
test/​ztest/​unit/​audio/​webrtc_aec/​webrtc_aec_ztest.c Checks reset cleanup and subsequent preparation.
test/​ztest/​unit/​audio/​webrtc_aec/​prj.conf Adjusts the test delay setting.
src/​audio/​webrtc_aec/​webrtc_aec3_wrapper.h Extends AEC3 configuration fields.
src/​audio/​webrtc_aec/​webrtc_aec3_wrapper.cpp Implements linear output and filter tuning.
src/​audio/​webrtc_aec/​webrtc_aec3_shims.cpp Updates global C++ allocation routing.
src/​audio/​webrtc_aec/​webrtc_aec.h Defines per-instance configuration and memory interfaces.
src/​audio/​webrtc_aec/​webrtc_aec.cmake Supports building multiple C backends.
src/​audio/​webrtc_aec/​webrtc_aec.c Implements configuration, selection, downmixing, and lifecycle changes.
src/​audio/​webrtc_aec/​webrtc_aec-stub.c Updates the stub backend interface.
src/​audio/​webrtc_aec/​webrtc_aec-shims.c Adds memory contexts and AEC3 arenas.
src/​audio/​webrtc_aec/​webrtc_aec-aecm.c Updates AECm lifecycle integration.
src/​audio/​webrtc_aec/​webrtc_aec-aec3.c Adds instance tuning, arenas, and telemetry.
src/​audio/​webrtc_aec/​webrtc_aec-aec.c Adds Float AEC tuning and diagnostics.
src/​audio/​webrtc_aec/​tune/​sof_webrtc_aec_blobs.m Generates topology and control presets.
src/​audio/​webrtc_aec/​README.md Documents configuration and memory behavior.
src/​audio/​webrtc_aec/​llext/​CMakeLists.txt Supports multiple loadable C backends.
src/​audio/​webrtc_aec/​Kconfig Adds backend selection and tuning options.
src/​audio/​webrtc_aec/​CMakeLists.txt Integrates multiple backends and C++ allocator wrapping.
app/​webrtc_aec3_static_overlay.conf Adds a tuned built-in AEC3 configuration.

Review effort: Balanced

Comment on lines +217 to +219
arena = webrtc_arenas[i];
if (arena && arena->base && p >= arena->base &&
p < arena->base + arena->size)
if (p)
return p;

p = rmalloc(SOF_MEM_FLAG_USER, size);
}
]
}
"true" {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants