diff --git a/scripts/cmake/xtensa_ext_cc.cmake b/scripts/cmake/xtensa_ext_cc.cmake new file mode 100644 index 000000000000..7aeb98f2bedc --- /dev/null +++ b/scripts/cmake/xtensa_ext_cc.cmake @@ -0,0 +1,80 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Resolve compiler target flags and archiver for external pure-C static +# libraries built via shell scripts under standalone LLEXT targets. +# +# Usage: +# include("${sof_top_dir}/scripts/cmake/xtensa_ext_cc.cmake") +# xtensa_ext_cc_get_flags(_ext_arch_cflags _ext_ar) +# +# When building Xtensa with Clang/LLVM, the generic Clang driver defaults to +# the host target unless passed explicit --target, -mcpu, --sysroot, and +# -fintegrated-as flags matching Zephyr's toolchain configuration. + +function(xtensa_ext_cc_get_flags out_cflags out_ar) + if(DEFINED CMAKE_AR) + set(_ar "${CMAKE_AR}") + else() + set(_ar "ar") + endif() + + if("${ARCH}" STREQUAL "xtensa") + if(CONFIG_LLEXT_BUILD_PIC) + set(_pic_flags "-fPIC") + else() + set(_pic_flags "-fno-pic -fno-pie") + endif() + + if(CMAKE_C_COMPILER_ID STREQUAL "Clang") + set(_sdk_prefix "") + get_filename_component(_ar_name "${_ar}" NAME) + if(_ar_name MATCHES "^xtensa-.+_zephyr-elf-ar$") + string(REGEX REPLACE "ar$" "" _sdk_prefix "${_ar}") + elseif(CMAKE_EXE_LINKER_FLAGS MATCHES "--ld-path=([^ ]+xtensa-[^ /]+_zephyr-elf)-ld") + set(_sdk_prefix "${CMAKE_MATCH_1}-") + set(_ar "${_sdk_prefix}ar") + endif() + + set(_core "${XTENSA_CORE_ID}") + if(NOT _core) + set(_core "$ENV{XTENSA_CORE_ID}") + endif() + if(NOT _core AND _sdk_prefix) + get_filename_component(_triple_dash "${_sdk_prefix}" NAME) + string(REGEX REPLACE "^xtensa-" "" _sdk_core "${_triple_dash}") + string(REGEX REPLACE "_zephyr-elf-$" "" _sdk_core "${_sdk_core}") + if(_sdk_core STREQUAL "intel_ace30_ptl") + set(_core "intel_ace30_adsp") + elseif(_sdk_core STREQUAL "intel_ace40") + set(_core "intel_ace40_adsp") + elseif(_sdk_core STREQUAL "intel_ace15_mtpm") + set(_core "intel_ace15_adsp") + else() + set(_core "${_sdk_core}") + endif() + endif() + + set(_cflags "--target=xtensa -fintegrated-as ${_pic_flags}") + if(_core) + set(_cflags "${_cflags} -mcpu=${_core}") + endif() + if(_sdk_prefix) + get_filename_component(_tc_bin "${_sdk_prefix}" DIRECTORY) + get_filename_component(_gnu_root "${_tc_bin}" DIRECTORY) + get_filename_component(_triple "${_sdk_prefix}" NAME) + string(REGEX REPLACE "-$" "" _triple "${_triple}") + set(_sysroot "${_gnu_root}/${_triple}") + if(IS_DIRECTORY "${_sysroot}") + set(_cflags "${_cflags} --sysroot=${_sysroot}") + endif() + endif() + else() + set(_cflags "${_pic_flags} -mlongcalls -mtext-section-literals") + endif() + else() + set(_cflags "-fPIC") + endif() + + set(${out_cflags} "${_cflags}" PARENT_SCOPE) + set(${out_ar} "${_ar}" PARENT_SCOPE) +endfunction() diff --git a/src/audio/webrtc_aec/llext/CMakeLists.txt b/src/audio/webrtc_aec/llext/CMakeLists.txt index 68632b2498f3..795a80f28ec4 100644 --- a/src/audio/webrtc_aec/llext/CMakeLists.txt +++ b/src/audio/webrtc_aec/llext/CMakeLists.txt @@ -5,7 +5,6 @@ if(CONFIG_COMP_WEBRTC_AEC_STUB) sof_llext_build("webrtc_aec" SOURCES ../webrtc_aec.c ../webrtc_aec-stub.c - LIB openmodules ) else() include(${CMAKE_CURRENT_LIST_DIR}/../webrtc_aec.cmake) @@ -25,7 +24,6 @@ else() "${SOF_WEBRTC_APM_SRC_DIR}/webrtc/common_audio/signal_processing/include" LIBS_PATH "${WEBRTC_AECM_INSTALL_DIR}/lib" LIBS webrtc_aecm - LIB openmodules ) add_dependencies(webrtc_aec_llext_lib webrtc_aecm_ext) diff --git a/src/audio/webrtc_aec/webrtc_aec-math.c b/src/audio/webrtc_aec/webrtc_aec-math.c index 672e2a9ecef1..2b9ad52bda79 100644 --- a/src/audio/webrtc_aec/webrtc_aec-math.c +++ b/src/audio/webrtc_aec/webrtc_aec-math.c @@ -75,7 +75,8 @@ WEBRTC_MATH_WEAK float exp2f(float x) return p * aec_ldexp2(n); } -WEBRTC_MATH_WEAK float sqrtf(float x) +/* --- sqrtf: bit estimate + 3 Newton steps --- */ +static inline float aec_sqrtf(float x) { union { float f; uint32_t i; } u = { .f = x }; float y; @@ -91,6 +92,11 @@ WEBRTC_MATH_WEAK float sqrtf(float x) return y; } +WEBRTC_MATH_WEAK float sqrtf(float x) +{ + return aec_sqrtf(x); +} + /* --- powf: x^y = 2^(y * log2(x)) --- */ WEBRTC_MATH_WEAK float powf(float x, float y) @@ -106,7 +112,7 @@ WEBRTC_MATH_WEAK float powf(float x, float y) if (y == 2.0f) return x * x; if (y == 0.5f) - return sqrtf(x); + return aec_sqrtf(x); return exp2f((float)(y * aec_log2f(x))); } diff --git a/src/audio/webrtc_aec/webrtc_aec.c b/src/audio/webrtc_aec/webrtc_aec.c index 1560b3f2a281..f0eec961fa3a 100644 --- a/src/audio/webrtc_aec/webrtc_aec.c +++ b/src/audio/webrtc_aec/webrtc_aec.c @@ -290,7 +290,7 @@ __cold static int webrtc_aec_prepare(struct processing_module *mod, { struct webrtc_aec_comp_data *cd = module_get_private_data(mod); struct comp_dev *dev = mod->dev; - int mic_fmt, ref_fmt, mic_rate, ref_rate, ret; + int mic_fmt, ref_fmt, out_fmt, mic_rate, ref_rate, ret; webrtc_aec_set_active_mod(mod); assert_can_be_cold(); @@ -309,9 +309,12 @@ __cold static int webrtc_aec_prepare(struct processing_module *mod, if (cd->ref_src >= 0) { /* Pin 1 may come from a separate render pipeline; apply its IPC4 pin format. */ - if (mod->priv.cfg.nb_input_pins > 1 && mod->priv.cfg.input_pins) - ipc4_update_source_format(sources[cd->ref_src], - &mod->priv.cfg.input_pins[1].audio_fmt); + if (!mod->priv.cfg.input_pins || mod->priv.cfg.nb_input_pins < 2) { + comp_err(dev, "webrtc_aec: no reference pin format"); + return -EINVAL; + } + ipc4_update_source_format(sources[cd->ref_src], + &mod->priv.cfg.input_pins[1].audio_fmt); ref_fmt = source_get_frm_fmt(sources[cd->ref_src]); ref_rate = source_get_rate(sources[cd->ref_src]); cd->ref_channels = source_get_channels(sources[cd->ref_src]); @@ -362,14 +365,18 @@ __cold static int webrtc_aec_prepare(struct processing_module *mod, } webrtc_aec_reset_resamplers(cd); + out_fmt = sink_get_frm_fmt(sinks[0]); if ((mic_fmt != SOF_IPC_FRAME_S16_LE && mic_fmt != SOF_IPC_FRAME_S32_LE) || - (ref_fmt != SOF_IPC_FRAME_S16_LE && ref_fmt != SOF_IPC_FRAME_S32_LE)) { - comp_err(dev, "webrtc_aec: unsupported format mic=%d ref=%d", - mic_fmt, ref_fmt); + (ref_fmt != SOF_IPC_FRAME_S16_LE && ref_fmt != SOF_IPC_FRAME_S32_LE) || + (out_fmt != SOF_IPC_FRAME_S16_LE && out_fmt != SOF_IPC_FRAME_S32_LE)) { + comp_err(dev, "webrtc_aec: unsupported format mic=%d ref=%d out=%d", + mic_fmt, ref_fmt, out_fmt); return -EINVAL; } - cd->is_s32 = (mic_fmt == SOF_IPC_FRAME_S32_LE); + cd->mic_is_s32 = (mic_fmt == SOF_IPC_FRAME_S32_LE); + cd->ref_is_s32 = (ref_fmt == SOF_IPC_FRAME_S32_LE); + cd->out_is_s32 = (out_fmt == SOF_IPC_FRAME_S32_LE); cd->mic_frame_bytes = source_get_frame_bytes(sources[cd->mic_src]); cd->ref_frame_bytes = cd->ref_src >= 0 ? source_get_frame_bytes(sources[cd->ref_src]) : cd->mic_frame_bytes; @@ -381,10 +388,12 @@ __cold static int webrtc_aec_prepare(struct processing_module *mod, return -EINVAL; } - comp_info(dev, "webrtc_aec: mic_src=%d ref_src=%d rate=%d/%d mic_ch=%d ref_ch=%d (eff_ref=%d) frame=%d %s resamp=%d", + comp_info(dev, "webrtc_aec: mic_src=%d ref_src=%d rate=%d/%d mic_ch=%d ref_ch=%d (eff_ref=%d) frame=%d %s/%s->%s resamp=%d", cd->mic_src, cd->ref_src, cd->rate, cd->proc_rate, cd->mic_channels, cd->ref_channels, cd->effective_ref_ch, - cd->frame_samples, cd->is_s32 ? "S32" : "S16", cd->resamp_mode); + cd->frame_samples, cd->mic_is_s32 ? "S32" : "S16", + cd->ref_is_s32 ? "S32" : "S16", cd->out_is_s32 ? "S32" : "S16", + cd->resamp_mode); if (cd->backend->configure) { ret = cd->backend->configure(mod, cd->proc_rate, @@ -635,7 +644,7 @@ static int webrtc_aec_process(struct processing_module *mod, } /* Convert mic input to per-channel S16 */ - src_to_s16(mic, n, cd->mic_buf, cd->buffered_in_frames, cd->mic_channels, cd->is_s32); + src_to_s16(mic, n, cd->mic_buf, cd->buffered_in_frames, cd->mic_channels, cd->mic_is_s32); /* * Validate dynamically attached reference source format/channels and trim @@ -655,12 +664,12 @@ static int webrtc_aec_process(struct processing_module *mod, } src_drop_frames(ref, fref); fref = 0; - ref_s32 = cd->is_s32; + ref_s32 = cd->ref_is_s32; } else { ref_s32 = (ref_fmt == SOF_IPC_FRAME_S32_LE); } } else { - ref_s32 = cd->is_s32; + ref_s32 = cd->ref_is_s32; } /* Keep reference aligned with mic cadence and zero-pad if render underruns or is silent. */ @@ -706,7 +715,7 @@ static int webrtc_aec_process(struct processing_module *mod, /* Drain exactly n frames from out_fifo to sink to maintain symmetric period I/O */ drain = MIN(n, cd->buffered_out_frames); if (drain > 0) { - s16_to_sink(out, cd->out_fifo, drain, cd->mic_channels, cd->is_s32); + s16_to_sink(out, cd->out_fifo, drain, cd->mic_channels, cd->out_is_s32); cd->buffered_out_frames -= drain; if (cd->buffered_out_frames > 0) { for (c = 0; c < cd->mic_channels; c++) diff --git a/src/audio/webrtc_aec/webrtc_aec.cmake b/src/audio/webrtc_aec/webrtc_aec.cmake index 131a4180c084..5425a1e56eb3 100644 --- a/src/audio/webrtc_aec/webrtc_aec.cmake +++ b/src/audio/webrtc_aec/webrtc_aec.cmake @@ -22,27 +22,14 @@ if(NOT EXISTS "${SOF_WEBRTC_APM_SRC_DIR}/webrtc/modules/audio_processing/aecm/ae "Run 'west update' or pass -DSOF_WEBRTC_APM_SRC_DIR=.") endif() -if(NOT DEFINED CMAKE_AR) - set(_aecm_ar "ar") -else() - set(_aecm_ar "${CMAKE_AR}") -endif() +include("${sof_top_dir}/scripts/cmake/xtensa_ext_cc.cmake") +xtensa_ext_cc_get_flags(_aecm_arch_cflags _aecm_ar) set(WEBRTC_AECM_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/webrtc-aecm-install" CACHE INTERNAL "webrtc_aec: AECm library install prefix") set(_aecm_src "${SOF_WEBRTC_APM_SRC_DIR}/webrtc") -if("${ARCH}" STREQUAL "xtensa") - if(CONFIG_LLEXT_BUILD_PIC) - set(_aecm_arch_cflags "-fPIC -mlongcalls -mtext-section-literals") - else() - set(_aecm_arch_cflags "-fno-pic -fno-pie -mlongcalls -mtext-section-literals") - endif() -else() - set(_aecm_arch_cflags "-fPIC") -endif() - if(CONFIG_WEBRTC_AEC_BACKEND_FLOAT) set(_aec_backend_srcs " $SRC/modules/audio_processing/aec/echo_cancellation.c diff --git a/src/audio/webrtc_aec/webrtc_aec.h b/src/audio/webrtc_aec/webrtc_aec.h index 734c43137e93..bde87c5edf4f 100644 --- a/src/audio/webrtc_aec/webrtc_aec.h +++ b/src/audio/webrtc_aec/webrtc_aec.h @@ -163,7 +163,9 @@ struct webrtc_aec_comp_data { int mic_frame_bytes; /* bytes per pipeline frame on mic input */ int ref_frame_bytes; /* bytes per pipeline frame on ref input */ int out_frame_bytes; - bool is_s32; /* true for S32_LE pipeline */ + bool mic_is_s32; /* true for S32_LE mic input */ + bool ref_is_s32; /* true for S32_LE ref input */ + bool out_is_s32; /* true for S32_LE sink output */ enum webrtc_aec_resamp_mode resamp_mode; /* Processing frame size at pipeline rate. */ diff --git a/src/audio/webrtc_agc/Kconfig b/src/audio/webrtc_agc/Kconfig index cf9effa0104a..01037ab501b4 100644 --- a/src/audio/webrtc_agc/Kconfig +++ b/src/audio/webrtc_agc/Kconfig @@ -2,6 +2,7 @@ config COMP_WEBRTC_AGC tristate "WebRTC Automatic Gain Control (AGC) module" + depends on IPC_MAJOR_4 default n help Select to enable the WebRTC Automatic Gain Control (digital_agc) module. diff --git a/src/audio/webrtc_agc/llext/CMakeLists.txt b/src/audio/webrtc_agc/llext/CMakeLists.txt index 17c94f8e7776..efa84076445d 100644 --- a/src/audio/webrtc_agc/llext/CMakeLists.txt +++ b/src/audio/webrtc_agc/llext/CMakeLists.txt @@ -5,7 +5,6 @@ if(CONFIG_COMP_WEBRTC_AGC_STUB) sof_llext_build("webrtc_agc" SOURCES ../webrtc_agc.c ../webrtc_agc-stub.c - LIB openmodules ) else() if(NOT DEFINED SOF_WEBRTC_APM_SRC_DIR) @@ -34,6 +33,5 @@ else() "${SOF_WEBRTC_APM_SRC_DIR}/webrtc/modules/audio_processing/agc/legacy" "${SOF_WEBRTC_APM_SRC_DIR}/webrtc/common_audio/signal_processing/include" CFLAGS -DWEBRTC_ARCH_32_BITS= -DWEBRTC_ARCH_LITTLE_ENDIAN= - LIB openmodules ) endif() diff --git a/src/audio/webrtc_agc/webrtc_agc-agc.c b/src/audio/webrtc_agc/webrtc_agc-agc.c index a493430b633d..217fac59e50d 100644 --- a/src/audio/webrtc_agc/webrtc_agc-agc.c +++ b/src/audio/webrtc_agc/webrtc_agc-agc.c @@ -218,7 +218,8 @@ static void webrtc_agc_process_multiband(struct webrtc_agc_comp_data *cd, static int webrtc_agc_real_process(struct processing_module *mod, const void *src, void *dst, int frames, - enum sof_ipc_frame fmt) + enum sof_ipc_frame in_fmt, + enum sof_ipc_frame out_fmt) { struct webrtc_agc_comp_data *cd = module_get_private_data(mod); size_t ch_count = cd->channels; @@ -249,7 +250,7 @@ static int webrtc_agc_real_process(struct processing_module *mod, size_t chunk = (rem < space) ? rem : space; /* 1. Drain chunk of processed frames from out_fifo to dst */ - if (fmt == SOF_IPC_FRAME_S16_LE) { + if (out_fmt == SOF_IPC_FRAME_S16_LE) { int16_t *d = (int16_t *)dst + offset * ch_count; for (i = 0; i < chunk; i++) { @@ -270,7 +271,7 @@ static int webrtc_agc_real_process(struct processing_module *mod, cd->out_fifo_rd += chunk; /* 2. Enqueue chunk of input frames into planar in_fifo */ - if (fmt == SOF_IPC_FRAME_S16_LE) { + if (in_fmt == SOF_IPC_FRAME_S16_LE) { const int16_t *s = (const int16_t *)src + offset * ch_count; for (i = 0; i < chunk; i++) { diff --git a/src/audio/webrtc_agc/webrtc_agc-stub.c b/src/audio/webrtc_agc/webrtc_agc-stub.c index 35fcdfd58731..05483fa92c6a 100644 --- a/src/audio/webrtc_agc/webrtc_agc-stub.c +++ b/src/audio/webrtc_agc/webrtc_agc-stub.c @@ -21,12 +21,31 @@ static int webrtc_agc_stub_prepare(struct processing_module *mod) static int webrtc_agc_stub_process(struct processing_module *mod, const void *src, void *dst, int frames, - enum sof_ipc_frame fmt) + enum sof_ipc_frame in_fmt, + enum sof_ipc_frame out_fmt) { struct webrtc_agc_comp_data *cd = module_get_private_data(mod); - size_t sample_bytes = (fmt == SOF_IPC_FRAME_S16_LE) ? sizeof(int16_t) : sizeof(int32_t); + size_t samples = (size_t)frames * cd->channels; + size_t i; - memcpy(dst, src, (size_t)frames * cd->channels * sample_bytes); + if (in_fmt == out_fmt) { + size_t sample_bytes = (in_fmt == SOF_IPC_FRAME_S16_LE) ? + sizeof(int16_t) : sizeof(int32_t); + + memcpy(dst, src, samples * sample_bytes); + } else if (in_fmt == SOF_IPC_FRAME_S32_LE) { + const int32_t *s = src; + int16_t *d = dst; + + for (i = 0; i < samples; i++) + d[i] = (int16_t)(s[i] >> 16); + } else { + const int16_t *s = src; + int32_t *d = dst; + + for (i = 0; i < samples; i++) + d[i] = ((int32_t)s[i]) << 16; + } return 0; } diff --git a/src/audio/webrtc_agc/webrtc_agc.c b/src/audio/webrtc_agc/webrtc_agc.c index 7576962196c8..6dedf752109e 100644 --- a/src/audio/webrtc_agc/webrtc_agc.c +++ b/src/audio/webrtc_agc/webrtc_agc.c @@ -60,12 +60,64 @@ __cold static int webrtc_agc_init(struct processing_module *mod) return 0; } +static void webrtc_agc_cir_passthrough(struct cir_buf_source *source_buf, + struct cir_buf_sink *sink_buf, + size_t frames, size_t channels, + enum sof_ipc_frame in_fmt, + enum sof_ipc_frame out_fmt) +{ + size_t in_sample_bytes = (in_fmt == SOF_IPC_FRAME_S16_LE) ? + sizeof(int16_t) : sizeof(int32_t); + size_t out_sample_bytes = (out_fmt == SOF_IPC_FRAME_S16_LE) ? + sizeof(int16_t) : sizeof(int32_t); + size_t in_frame_bytes = in_sample_bytes * channels; + size_t out_frame_bytes = out_sample_bytes * channels; + + if (in_fmt == out_fmt) { + cir_buf_copy(source_buf->ptr, source_buf->buf_start, source_buf->buf_end, + sink_buf->ptr, sink_buf->buf_start, sink_buf->buf_end, + frames * in_frame_bytes); + return; + } + + while (frames) { + size_t fmax_src = circ_buf_frames_without_wrap(source_buf->ptr, source_buf->buf_end, + in_sample_bytes, channels); + size_t fmax_snk = circ_buf_frames_without_wrap(sink_buf->ptr, sink_buf->buf_end, + out_sample_bytes, channels); + size_t f = MIN(frames, MIN(fmax_src, fmax_snk)); + size_t samples = f * channels; + size_t i; + + if (in_fmt == SOF_IPC_FRAME_S32_LE) { + const int32_t *s = source_buf->ptr; + int16_t *d = sink_buf->ptr; + + for (i = 0; i < samples; i++) + d[i] = (int16_t)(s[i] >> 16); + } else { + const int16_t *s = source_buf->ptr; + int32_t *d = sink_buf->ptr; + + for (i = 0; i < samples; i++) + d[i] = ((int32_t)s[i]) << 16; + } + + frames -= f; + source_buf->ptr = source_cir_buf_wrap((const char *)source_buf->ptr + f * in_frame_bytes, + source_buf->buf_start, source_buf->buf_end); + sink_buf->ptr = cir_buf_wrap((char *)sink_buf->ptr + f * out_frame_bytes, + sink_buf->buf_start, sink_buf->buf_end); + } +} + __cold static int webrtc_agc_prepare(struct processing_module *mod, struct sof_source **sources, int num_of_sources, struct sof_sink **sinks, int num_of_sinks) { struct webrtc_agc_comp_data *cd = module_get_private_data(mod); struct comp_dev *dev = mod->dev; + enum sof_ipc_frame in_fmt, out_fmt; int ret; if (num_of_sources != 1 || num_of_sinks != 1) { @@ -75,6 +127,20 @@ __cold static int webrtc_agc_prepare(struct processing_module *mod, cd->rate = source_get_rate(sources[0]); cd->channels = source_get_channels(sources[0]); + in_fmt = source_get_frm_fmt(sources[0]); + out_fmt = sink_get_frm_fmt(sinks[0]); + + if (cd->channels != sink_get_channels(sinks[0]) || + cd->rate != sink_get_rate(sinks[0])) { + comp_err(dev, "webrtc_agc: source/sink mismatch"); + return -EINVAL; + } + + if ((in_fmt != SOF_IPC_FRAME_S16_LE && in_fmt != SOF_IPC_FRAME_S32_LE) || + (out_fmt != SOF_IPC_FRAME_S16_LE && out_fmt != SOF_IPC_FRAME_S32_LE)) { + comp_err(dev, "webrtc_agc: unsupported format in=%d out=%d", in_fmt, out_fmt); + return -EINVAL; + } if (cd->rate != 8000 && cd->rate != 16000 && cd->rate != 32000 && cd->rate != 48000) { @@ -89,7 +155,8 @@ __cold static int webrtc_agc_prepare(struct processing_module *mod, return -EINVAL; } - comp_info(dev, "webrtc_agc: prepare rate=%d ch=%d", cd->rate, cd->channels); + comp_info(dev, "webrtc_agc: prepare rate=%d ch=%d in_fmt=%d out_fmt=%d", + cd->rate, cd->channels, in_fmt, out_fmt); if (cd->backend->prepare) { ret = cd->backend->prepare(mod); @@ -109,13 +176,16 @@ static int webrtc_agc_process(struct processing_module *mod, struct webrtc_agc_comp_data *cd = module_get_private_data(mod); struct sof_source *src = sources[0]; struct sof_sink *snk = sinks[0]; - enum sof_ipc_frame fmt = source_get_frm_fmt(src); - size_t sample_bytes = (fmt == SOF_IPC_FRAME_S16_LE) ? sizeof(int16_t) : sizeof(int32_t); - size_t frame_bytes = source_get_frame_bytes(src); + enum sof_ipc_frame in_fmt = source_get_frm_fmt(src); + enum sof_ipc_frame out_fmt = sink_get_frm_fmt(snk); + size_t in_sample_bytes = (in_fmt == SOF_IPC_FRAME_S16_LE) ? sizeof(int16_t) : sizeof(int32_t); + size_t out_sample_bytes = (out_fmt == SOF_IPC_FRAME_S16_LE) ? sizeof(int16_t) : sizeof(int32_t); + size_t in_frame_bytes = source_get_frame_bytes(src); + size_t out_frame_bytes = sink_get_frame_bytes(snk); int avail = (int)source_get_data_frames_available(src); int free_frames = (int)sink_get_free_frames(snk); int n = MIN(avail, free_frames); - size_t nbytes, src_bytes, snk_bytes; + size_t in_nbytes, out_nbytes, src_bytes, snk_bytes; struct cir_buf_source source_buf; struct cir_buf_sink sink_buf; int ret; @@ -123,14 +193,15 @@ static int webrtc_agc_process(struct processing_module *mod, if (n <= 0) return 0; - nbytes = (size_t)n * frame_bytes; + in_nbytes = (size_t)n * in_frame_bytes; + out_nbytes = (size_t)n * out_frame_bytes; - ret = source_get_data(src, nbytes, &source_buf.ptr, &source_buf.buf_start, &src_bytes); + ret = source_get_data(src, in_nbytes, &source_buf.ptr, &source_buf.buf_start, &src_bytes); if (ret) return ret; source_buf.buf_end = (const char *)source_buf.buf_start + src_bytes; - ret = sink_get_buffer(snk, nbytes, &sink_buf.ptr, &sink_buf.buf_start, &snk_bytes); + ret = sink_get_buffer(snk, out_nbytes, &sink_buf.ptr, &sink_buf.buf_start, &snk_bytes); if (ret) { source_release_data(src, 0); return ret; @@ -142,33 +213,31 @@ static int webrtc_agc_process(struct processing_module *mod, while (remaining_frames) { size_t fmax_src = circ_buf_frames_without_wrap(source_buf.ptr, source_buf.buf_end, - sample_bytes, cd->channels); + in_sample_bytes, cd->channels); size_t fmax_snk = circ_buf_frames_without_wrap(sink_buf.ptr, sink_buf.buf_end, - sample_bytes, cd->channels); + out_sample_bytes, cd->channels); size_t f = MIN(remaining_frames, MIN(fmax_src, fmax_snk)); - ret = cd->backend->process(mod, source_buf.ptr, sink_buf.ptr, f, fmt); + ret = cd->backend->process(mod, source_buf.ptr, sink_buf.ptr, f, + in_fmt, out_fmt); if (ret) { - cir_buf_copy(source_buf.ptr, source_buf.buf_start, source_buf.buf_end, - sink_buf.ptr, sink_buf.buf_start, sink_buf.buf_end, - remaining_frames * frame_bytes); + webrtc_agc_cir_passthrough(&source_buf, &sink_buf, remaining_frames, + cd->channels, in_fmt, out_fmt); break; } remaining_frames -= f; - source_buf.ptr = source_cir_buf_wrap((const char *)source_buf.ptr + f * frame_bytes, + source_buf.ptr = source_cir_buf_wrap((const char *)source_buf.ptr + f * in_frame_bytes, source_buf.buf_start, source_buf.buf_end); - sink_buf.ptr = cir_buf_wrap((char *)sink_buf.ptr + f * frame_bytes, + sink_buf.ptr = cir_buf_wrap((char *)sink_buf.ptr + f * out_frame_bytes, sink_buf.buf_start, sink_buf.buf_end); } } else { - cir_buf_copy(source_buf.ptr, source_buf.buf_start, source_buf.buf_end, - sink_buf.ptr, sink_buf.buf_start, sink_buf.buf_end, - nbytes); + webrtc_agc_cir_passthrough(&source_buf, &sink_buf, n, cd->channels, in_fmt, out_fmt); } - source_release_data(src, nbytes); - sink_commit_buffer(snk, nbytes); + source_release_data(src, in_nbytes); + sink_commit_buffer(snk, out_nbytes); return 0; } diff --git a/src/audio/webrtc_agc/webrtc_agc.h b/src/audio/webrtc_agc/webrtc_agc.h index b647d5f262ea..87cf60d4cdeb 100644 --- a/src/audio/webrtc_agc/webrtc_agc.h +++ b/src/audio/webrtc_agc/webrtc_agc.h @@ -43,7 +43,7 @@ struct webrtc_agc_backend { int (*prepare)(struct processing_module *mod); int (*process)(struct processing_module *mod, const void *src, void *dst, int frames, - enum sof_ipc_frame fmt); + enum sof_ipc_frame in_fmt, enum sof_ipc_frame out_fmt); int (*reset)(struct processing_module *mod); int (*free)(struct processing_module *mod); }; diff --git a/src/audio/webrtc_hpf/Kconfig b/src/audio/webrtc_hpf/Kconfig index 6d6d57a2e728..e0cdf1609a7e 100644 --- a/src/audio/webrtc_hpf/Kconfig +++ b/src/audio/webrtc_hpf/Kconfig @@ -2,6 +2,7 @@ config COMP_WEBRTC_HPF tristate "WebRTC High-Pass Filter (HPF) module" + depends on IPC_MAJOR_4 default n help Select to enable the WebRTC High-Pass Filter module. diff --git a/src/audio/webrtc_hpf/llext/CMakeLists.txt b/src/audio/webrtc_hpf/llext/CMakeLists.txt index 86ab8dcdb1f8..0882d93c94d7 100644 --- a/src/audio/webrtc_hpf/llext/CMakeLists.txt +++ b/src/audio/webrtc_hpf/llext/CMakeLists.txt @@ -5,12 +5,10 @@ if(CONFIG_COMP_WEBRTC_HPF_STUB) sof_llext_build("webrtc_hpf" SOURCES ../webrtc_hpf.c ../webrtc_hpf-stub.c - LIB openmodules ) else() sof_llext_build("webrtc_hpf" SOURCES ../webrtc_hpf.c ../webrtc_hpf-filter.c - LIB openmodules ) endif() diff --git a/src/audio/webrtc_hpf/webrtc_hpf-filter.c b/src/audio/webrtc_hpf/webrtc_hpf-filter.c index 586eaa2b0563..2ddb3f8cb5ac 100644 --- a/src/audio/webrtc_hpf/webrtc_hpf-filter.c +++ b/src/audio/webrtc_hpf/webrtc_hpf-filter.c @@ -24,20 +24,37 @@ static const int16_t kFilterCoefficients48kHz[5] = { 4058, -8116, 4058, 8116, -4021 }; -void webrtc_hpf_filter_channel_s16(struct webrtc_hpf_state *hpf, - const int16_t *src, int16_t *dst, - size_t samples, size_t stride) +void webrtc_hpf_filter_channel(struct webrtc_hpf_state *hpf, + const void *src, void *dst, + size_t samples, size_t stride, + enum sof_ipc_frame in_fmt, + enum sof_ipc_frame out_fmt) { + const bool in_s16 = (in_fmt == SOF_IPC_FRAME_S16_LE); + const bool out_s16 = (out_fmt == SOF_IPC_FRAME_S16_LE); + const int16_t *s16 = src; + const int32_t *s32 = src; + int16_t *d16 = dst; + int32_t *d32 = dst; int16_t *y = hpf->y; int16_t *x = hpf->x; const int16_t *ba = hpf->ba; size_t i; for (i = 0; i < samples; i++) { - int16_t sample = *src; - src += stride; - - int32_t tmp = y[1] * ba[3] + y[3] * ba[4]; + int16_t sample; + int16_t out16; + int32_t tmp; + + if (in_s16) { + sample = *s16; + s16 += stride; + } else { + sample = (int16_t)(*s32 >> 16); + s32 += stride; + } + + tmp = y[1] * ba[3] + y[3] * ba[4]; tmp = (tmp >> 15); tmp += y[0] * ba[3] + y[2] * ba[4]; tmp = (tmp << 1); @@ -58,48 +75,14 @@ void webrtc_hpf_filter_channel_s16(struct webrtc_hpf_state *hpf, else if (tmp < -134217728) tmp = -134217728; - *dst = (int16_t)(tmp >> 12); - dst += stride; - } -} - -void webrtc_hpf_filter_channel_s32(struct webrtc_hpf_state *hpf, - const int32_t *src, int32_t *dst, - size_t samples, size_t stride) -{ - int16_t *y = hpf->y; - int16_t *x = hpf->x; - const int16_t *ba = hpf->ba; - size_t i; - - for (i = 0; i < samples; i++) { - int16_t sample = (int16_t)(*src >> 16); - src += stride; - - int32_t tmp = y[1] * ba[3] + y[3] * ba[4]; - tmp = (tmp >> 15); - tmp += y[0] * ba[3] + y[2] * ba[4]; - tmp = (tmp << 1); - - tmp += sample * ba[0] + x[0] * ba[1] + x[1] * ba[2]; - - x[1] = x[0]; - x[0] = sample; - - y[2] = y[0]; - y[3] = y[1]; - y[0] = (int16_t)(tmp >> 13); - y[1] = (int16_t)((tmp - ((int32_t)y[0] << 13)) << 2); - - tmp += 2048; - if (tmp > 134217727) - tmp = 134217727; - else if (tmp < -134217728) - tmp = -134217728; - - int16_t out16 = (int16_t)(tmp >> 12); - *dst = ((int32_t)out16) << 16; - dst += stride; + out16 = (int16_t)(tmp >> 12); + if (out_s16) { + *d16 = out16; + d16 += stride; + } else { + *d32 = ((int32_t)out16) << 16; + d32 += stride; + } } } @@ -132,25 +115,22 @@ static int webrtc_hpf_real_prepare(struct processing_module *mod) static int webrtc_hpf_real_process(struct processing_module *mod, const void *src, void *dst, int frames, - enum sof_ipc_frame fmt) + enum sof_ipc_frame in_fmt, + enum sof_ipc_frame out_fmt) { struct webrtc_hpf_comp_data *cd = module_get_private_data(mod); + size_t in_sample_bytes = (in_fmt == SOF_IPC_FRAME_S16_LE) ? + sizeof(int16_t) : sizeof(int32_t); + size_t out_sample_bytes = (out_fmt == SOF_IPC_FRAME_S16_LE) ? + sizeof(int16_t) : sizeof(int32_t); size_t ch = cd->channels; size_t c; - if (fmt == SOF_IPC_FRAME_S16_LE) { - const int16_t *s = src; - int16_t *d = dst; - - for (c = 0; c < ch; c++) - webrtc_hpf_filter_channel_s16(&cd->state[c], s + c, d + c, frames, ch); - } else { - const int32_t *s = src; - int32_t *d = dst; - - for (c = 0; c < ch; c++) - webrtc_hpf_filter_channel_s32(&cd->state[c], s + c, d + c, frames, ch); - } + for (c = 0; c < ch; c++) + webrtc_hpf_filter_channel(&cd->state[c], + (const char *)src + c * in_sample_bytes, + (char *)dst + c * out_sample_bytes, + frames, ch, in_fmt, out_fmt); return 0; } diff --git a/src/audio/webrtc_hpf/webrtc_hpf-stub.c b/src/audio/webrtc_hpf/webrtc_hpf-stub.c index 1b32a9be2ba3..cdc6c728da95 100644 --- a/src/audio/webrtc_hpf/webrtc_hpf-stub.c +++ b/src/audio/webrtc_hpf/webrtc_hpf-stub.c @@ -18,12 +18,31 @@ static int webrtc_hpf_stub_prepare(struct processing_module *mod) static int webrtc_hpf_stub_process(struct processing_module *mod, const void *src, void *dst, int frames, - enum sof_ipc_frame fmt) + enum sof_ipc_frame in_fmt, + enum sof_ipc_frame out_fmt) { struct webrtc_hpf_comp_data *cd = module_get_private_data(mod); - size_t sample_bytes = (fmt == SOF_IPC_FRAME_S16_LE) ? sizeof(int16_t) : sizeof(int32_t); + size_t samples = (size_t)frames * cd->channels; + size_t i; - memcpy(dst, src, (size_t)frames * cd->channels * sample_bytes); + if (in_fmt == out_fmt) { + size_t sample_bytes = (in_fmt == SOF_IPC_FRAME_S16_LE) ? + sizeof(int16_t) : sizeof(int32_t); + + memcpy(dst, src, samples * sample_bytes); + } else if (in_fmt == SOF_IPC_FRAME_S32_LE) { + const int32_t *s = src; + int16_t *d = dst; + + for (i = 0; i < samples; i++) + d[i] = (int16_t)(s[i] >> 16); + } else { + const int16_t *s = src; + int32_t *d = dst; + + for (i = 0; i < samples; i++) + d[i] = ((int32_t)s[i]) << 16; + } return 0; } diff --git a/src/audio/webrtc_hpf/webrtc_hpf.c b/src/audio/webrtc_hpf/webrtc_hpf.c index 70b443261c9e..87f2763da942 100644 --- a/src/audio/webrtc_hpf/webrtc_hpf.c +++ b/src/audio/webrtc_hpf/webrtc_hpf.c @@ -54,12 +54,64 @@ __cold static int webrtc_hpf_init(struct processing_module *mod) return 0; } +static void webrtc_hpf_cir_passthrough(struct cir_buf_source *source_buf, + struct cir_buf_sink *sink_buf, + size_t frames, size_t channels, + enum sof_ipc_frame in_fmt, + enum sof_ipc_frame out_fmt) +{ + size_t in_sample_bytes = (in_fmt == SOF_IPC_FRAME_S16_LE) ? + sizeof(int16_t) : sizeof(int32_t); + size_t out_sample_bytes = (out_fmt == SOF_IPC_FRAME_S16_LE) ? + sizeof(int16_t) : sizeof(int32_t); + size_t in_frame_bytes = in_sample_bytes * channels; + size_t out_frame_bytes = out_sample_bytes * channels; + + if (in_fmt == out_fmt) { + cir_buf_copy(source_buf->ptr, source_buf->buf_start, source_buf->buf_end, + sink_buf->ptr, sink_buf->buf_start, sink_buf->buf_end, + frames * in_frame_bytes); + return; + } + + while (frames) { + size_t fmax_src = circ_buf_frames_without_wrap(source_buf->ptr, source_buf->buf_end, + in_sample_bytes, channels); + size_t fmax_snk = circ_buf_frames_without_wrap(sink_buf->ptr, sink_buf->buf_end, + out_sample_bytes, channels); + size_t f = MIN(frames, MIN(fmax_src, fmax_snk)); + size_t samples = f * channels; + size_t i; + + if (in_fmt == SOF_IPC_FRAME_S32_LE) { + const int32_t *s = source_buf->ptr; + int16_t *d = sink_buf->ptr; + + for (i = 0; i < samples; i++) + d[i] = (int16_t)(s[i] >> 16); + } else { + const int16_t *s = source_buf->ptr; + int32_t *d = sink_buf->ptr; + + for (i = 0; i < samples; i++) + d[i] = ((int32_t)s[i]) << 16; + } + + frames -= f; + source_buf->ptr = source_cir_buf_wrap((const char *)source_buf->ptr + f * in_frame_bytes, + source_buf->buf_start, source_buf->buf_end); + sink_buf->ptr = cir_buf_wrap((char *)sink_buf->ptr + f * out_frame_bytes, + sink_buf->buf_start, sink_buf->buf_end); + } +} + __cold static int webrtc_hpf_prepare(struct processing_module *mod, struct sof_source **sources, int num_of_sources, struct sof_sink **sinks, int num_of_sinks) { struct webrtc_hpf_comp_data *cd = module_get_private_data(mod); struct comp_dev *dev = mod->dev; + enum sof_ipc_frame in_fmt, out_fmt; int ret; if (num_of_sources != 1 || num_of_sinks != 1) { @@ -69,6 +121,20 @@ __cold static int webrtc_hpf_prepare(struct processing_module *mod, cd->rate = source_get_rate(sources[0]); cd->channels = source_get_channels(sources[0]); + in_fmt = source_get_frm_fmt(sources[0]); + out_fmt = sink_get_frm_fmt(sinks[0]); + + if (cd->channels != sink_get_channels(sinks[0]) || + cd->rate != sink_get_rate(sinks[0])) { + comp_err(dev, "webrtc_hpf: source/sink mismatch"); + return -EINVAL; + } + + if ((in_fmt != SOF_IPC_FRAME_S16_LE && in_fmt != SOF_IPC_FRAME_S32_LE) || + (out_fmt != SOF_IPC_FRAME_S16_LE && out_fmt != SOF_IPC_FRAME_S32_LE)) { + comp_err(dev, "webrtc_hpf: unsupported format in=%d out=%d", in_fmt, out_fmt); + return -EINVAL; + } if (cd->channels > WEBRTC_HPF_CHANNELS_MAX) { comp_err(dev, "webrtc_hpf: channels %d exceeds max %d", @@ -76,7 +142,11 @@ __cold static int webrtc_hpf_prepare(struct processing_module *mod, return -EINVAL; } - comp_info(dev, "webrtc_hpf: prepare rate=%d ch=%d", cd->rate, cd->channels); + cd->in_fmt = in_fmt; + cd->out_fmt = out_fmt; + + comp_info(dev, "webrtc_hpf: prepare rate=%d ch=%d in_fmt=%d out_fmt=%d", + cd->rate, cd->channels, in_fmt, out_fmt); if (cd->backend->prepare) { ret = cd->backend->prepare(mod); @@ -96,13 +166,16 @@ static int webrtc_hpf_process(struct processing_module *mod, struct webrtc_hpf_comp_data *cd = module_get_private_data(mod); struct sof_source *src = sources[0]; struct sof_sink *snk = sinks[0]; - enum sof_ipc_frame fmt = source_get_frm_fmt(src); - size_t sample_bytes = (fmt == SOF_IPC_FRAME_S16_LE) ? sizeof(int16_t) : sizeof(int32_t); - size_t frame_bytes = source_get_frame_bytes(src); + enum sof_ipc_frame in_fmt = source_get_frm_fmt(src); + enum sof_ipc_frame out_fmt = sink_get_frm_fmt(snk); + size_t in_sample_bytes = (in_fmt == SOF_IPC_FRAME_S16_LE) ? sizeof(int16_t) : sizeof(int32_t); + size_t out_sample_bytes = (out_fmt == SOF_IPC_FRAME_S16_LE) ? sizeof(int16_t) : sizeof(int32_t); + size_t in_frame_bytes = source_get_frame_bytes(src); + size_t out_frame_bytes = sink_get_frame_bytes(snk); int avail = (int)source_get_data_frames_available(src); int free_frames = (int)sink_get_free_frames(snk); int n = MIN(avail, free_frames); - size_t nbytes, src_bytes, snk_bytes; + size_t in_nbytes, out_nbytes, src_bytes, snk_bytes; struct cir_buf_source source_buf; struct cir_buf_sink sink_buf; int ret; @@ -110,14 +183,15 @@ static int webrtc_hpf_process(struct processing_module *mod, if (n <= 0) return 0; - nbytes = (size_t)n * frame_bytes; + in_nbytes = (size_t)n * in_frame_bytes; + out_nbytes = (size_t)n * out_frame_bytes; - ret = source_get_data(src, nbytes, &source_buf.ptr, &source_buf.buf_start, &src_bytes); + ret = source_get_data(src, in_nbytes, &source_buf.ptr, &source_buf.buf_start, &src_bytes); if (ret) return ret; source_buf.buf_end = (const char *)source_buf.buf_start + src_bytes; - ret = sink_get_buffer(snk, nbytes, &sink_buf.ptr, &sink_buf.buf_start, &snk_bytes); + ret = sink_get_buffer(snk, out_nbytes, &sink_buf.ptr, &sink_buf.buf_start, &snk_bytes); if (ret) { source_release_data(src, 0); return ret; @@ -129,33 +203,31 @@ static int webrtc_hpf_process(struct processing_module *mod, while (remaining_frames) { size_t fmax_src = circ_buf_frames_without_wrap(source_buf.ptr, source_buf.buf_end, - sample_bytes, cd->channels); + in_sample_bytes, cd->channels); size_t fmax_snk = circ_buf_frames_without_wrap(sink_buf.ptr, sink_buf.buf_end, - sample_bytes, cd->channels); + out_sample_bytes, cd->channels); size_t f = MIN(remaining_frames, MIN(fmax_src, fmax_snk)); - ret = cd->backend->process(mod, source_buf.ptr, sink_buf.ptr, f, fmt); + ret = cd->backend->process(mod, source_buf.ptr, sink_buf.ptr, f, + in_fmt, out_fmt); if (ret) { - cir_buf_copy(source_buf.ptr, source_buf.buf_start, source_buf.buf_end, - sink_buf.ptr, sink_buf.buf_start, sink_buf.buf_end, - remaining_frames * frame_bytes); + webrtc_hpf_cir_passthrough(&source_buf, &sink_buf, remaining_frames, + cd->channels, in_fmt, out_fmt); break; } remaining_frames -= f; - source_buf.ptr = source_cir_buf_wrap((const char *)source_buf.ptr + f * frame_bytes, + source_buf.ptr = source_cir_buf_wrap((const char *)source_buf.ptr + f * in_frame_bytes, source_buf.buf_start, source_buf.buf_end); - sink_buf.ptr = cir_buf_wrap((char *)sink_buf.ptr + f * frame_bytes, + sink_buf.ptr = cir_buf_wrap((char *)sink_buf.ptr + f * out_frame_bytes, sink_buf.buf_start, sink_buf.buf_end); } } else { - cir_buf_copy(source_buf.ptr, source_buf.buf_start, source_buf.buf_end, - sink_buf.ptr, sink_buf.buf_start, sink_buf.buf_end, - nbytes); + webrtc_hpf_cir_passthrough(&source_buf, &sink_buf, n, cd->channels, in_fmt, out_fmt); } - source_release_data(src, nbytes); - sink_commit_buffer(snk, nbytes); + source_release_data(src, in_nbytes); + sink_commit_buffer(snk, out_nbytes); return 0; } diff --git a/src/audio/webrtc_hpf/webrtc_hpf.h b/src/audio/webrtc_hpf/webrtc_hpf.h index c31dbf5e1d2b..846b5a70d4dd 100644 --- a/src/audio/webrtc_hpf/webrtc_hpf.h +++ b/src/audio/webrtc_hpf/webrtc_hpf.h @@ -26,7 +26,7 @@ struct webrtc_hpf_backend { int (*prepare)(struct processing_module *mod); int (*process)(struct processing_module *mod, const void *src, void *dst, int frames, - enum sof_ipc_frame fmt); + enum sof_ipc_frame in_fmt, enum sof_ipc_frame out_fmt); int (*reset)(struct processing_module *mod); int (*free)(struct processing_module *mod); }; @@ -34,6 +34,8 @@ struct webrtc_hpf_backend { struct webrtc_hpf_comp_data { struct webrtc_hpf_state state[WEBRTC_HPF_CHANNELS_MAX]; const struct webrtc_hpf_backend *backend; + enum sof_ipc_frame in_fmt; + enum sof_ipc_frame out_fmt; int channels; int rate; bool enabled; @@ -42,12 +44,10 @@ struct webrtc_hpf_comp_data { extern const struct webrtc_hpf_backend webrtc_hpf_real_backend; extern const struct webrtc_hpf_backend webrtc_hpf_stub_backend; -void webrtc_hpf_filter_channel_s16(struct webrtc_hpf_state *hpf, - const int16_t *src, int16_t *dst, - size_t samples, size_t stride); - -void webrtc_hpf_filter_channel_s32(struct webrtc_hpf_state *hpf, - const int32_t *src, int32_t *dst, - size_t samples, size_t stride); +void webrtc_hpf_filter_channel(struct webrtc_hpf_state *hpf, + const void *src, void *dst, + size_t samples, size_t stride, + enum sof_ipc_frame in_fmt, + enum sof_ipc_frame out_fmt); #endif /* __SOF_AUDIO_WEBRTC_HPF_H__ */ diff --git a/src/audio/webrtc_ns/Kconfig b/src/audio/webrtc_ns/Kconfig index 062c6cd8b248..a240f1e8a4fd 100644 --- a/src/audio/webrtc_ns/Kconfig +++ b/src/audio/webrtc_ns/Kconfig @@ -2,6 +2,7 @@ config COMP_WEBRTC_NS tristate "WebRTC Noise Suppression (classic spectral Wiener filter)" + depends on IPC_MAJOR_4 help Select to include the WebRTC Noise Suppression module. It wraps the classic WebRTC NS algorithm — a frequency-domain spectral diff --git a/src/audio/webrtc_ns/llext/CMakeLists.txt b/src/audio/webrtc_ns/llext/CMakeLists.txt index 19b7e9aa3508..070841352923 100644 --- a/src/audio/webrtc_ns/llext/CMakeLists.txt +++ b/src/audio/webrtc_ns/llext/CMakeLists.txt @@ -6,7 +6,6 @@ if(CONFIG_COMP_WEBRTC_NS_STUB) sof_llext_build("webrtc_ns" SOURCES ../webrtc_ns.c ../webrtc_ns-stub.c - LIB openmodules ) else() # Real NS: cross-build webrtc-apm NS subset and link it. @@ -20,7 +19,6 @@ else() "${SOF_WEBRTC_APM_SRC_DIR}" LIBS_PATH "${WEBRTC_NS_INSTALL_DIR}/lib" LIBS webrtc_ns_core - LIB openmodules ) add_dependencies(webrtc_ns_llext_lib webrtc_ns_ext) diff --git a/src/audio/webrtc_ns/webrtc_ns-shims.c b/src/audio/webrtc_ns/webrtc_ns-shims.c index 4f5b91362079..6d953cc4614a 100644 --- a/src/audio/webrtc_ns/webrtc_ns-shims.c +++ b/src/audio/webrtc_ns/webrtc_ns-shims.c @@ -13,7 +13,7 @@ #include #include #include -#if !defined(LLEXT_MODULE) +#if !defined(LLEXT_MODULE) && !defined(LL_EXTENSION_BUILD) #include static APP_SYSUSER_BSS struct processing_module *active_ns_mod; #else @@ -60,7 +60,7 @@ void *webrtc_ns_calloc(size_t nmemb, size_t size) return p; } -#if defined(LLEXT_MODULE) +#if defined(LLEXT_MODULE) || defined(LL_EXTENSION_BUILD) /* ============================ Memory ============================ */ void *malloc(size_t size) @@ -124,42 +124,64 @@ void *memmove(void *dest, const void *src, size_t n) */ -float fabsf(float x) +/* + * TODO: Remove this workaround and restore uniform __attribute__((weak)) + * linkage once Zephyr's Xtensa LLEXT loader (llext_link_plt()) supports + * STB_WEAK relocations. Currently it only relocates STB_GLOBAL and STB_LOCAL + * symbols, leaving STB_WEAK slots zeroed, while standalone LLEXTs have + * isolated private symbol tables and will not collide across modules. + */ +#if defined(LLEXT_MODULE) || defined(LL_EXTENSION_BUILD) +#define NS_SHIM_WEAK +#else +#define NS_SHIM_WEAK __attribute__((weak)) +#endif + +NS_SHIM_WEAK float fabsf(float x) { return (x < 0.0f) ? -x : x; } -double fabs(double x) +NS_SHIM_WEAK double fabs(double x) { return (x < 0.0) ? -x : x; } static inline float ns_exp2f(float x) { - if (x > 127.0f) return 3.40282347e+38f; - if (x < -126.0f) return 0.0f; - int n = (int)x; - if (x < 0.0f && (float)n != x) n--; - float f = x - (float)n; - float p = 1.0f + f * (0.6931472f + f * (0.2402265f + f * 0.0555041f)); union { float f; uint32_t i; } u; - if (n < -126) return 0.0f; - if (n > 127) n = 127; + float f, p; + int n; + + if (x >= 127.0f) + return 3.40282347e+38f; + if (x < -126.0f) + return 0.0f; + + n = (int)(x >= 0.0f ? x + 0.5f : x - 0.5f); + if (n > 127) + return 3.40282347e+38f; + if (n < -126) + return 0.0f; + + f = (x - (float)n) * 0.69314718f; + p = 1.0f + f * (1.0f + f * (0.5f + f * (0.16666667f + + f * (0.04166667f + f * (0.00833333f + f * 0.00138889f))))); u.i = (uint32_t)(n + 127) << 23; return p * u.f; } -float expf(float x) +NS_SHIM_WEAK float expf(float x) { return ns_exp2f(x * 1.4426950408889634f); } -double exp(double x) +NS_SHIM_WEAK double exp(double x) { return (double)expf((float)x); } -double tanh(double x) +NS_SHIM_WEAK double tanh(double x) { if (x > 10.0) return 1.0; if (x < -10.0) return -1.0; @@ -167,10 +189,188 @@ double tanh(double x) return (e2 - 1.0) / (e2 + 1.0); } -float atanf(float x) +NS_SHIM_WEAK float atanf(float x) +{ + float ax = fabsf(x); + float base = 0.0f; + float u, u2, res; + + if (ax > 2.41421356f) { + base = 1.5707963267948966f; + u = -1.0f / ax; + } else if (ax > 0.41421356f) { + base = 0.7853981633974483f; + u = (ax - 1.0f) / (ax + 1.0f); + } else { + u = ax; + } + + u2 = u * u; + res = base + u * (1.0f + u2 * (-0.33333333f + u2 * (0.20000000f + + u2 * (-0.14285714f + u2 * 0.11111111f)))); + return (x < 0.0f) ? -res : res; +} + +static float ns_log2f(float x) +{ + union { float f; uint32_t i; } u = { .f = x }; + int e, e_bias = 127; + float m, r, s, poly; + + if (x <= 0.0f) + return -127.0f; + + if (u.i < 0x00800000U) { + x *= 16777216.0f; + u.f = x; + e_bias = 151; + } + + e = (int)((u.i >> 23) & 0xff) - e_bias; + u.i = (u.i & 0x007fffff) | 0x3f800000; + m = u.f; + if (m > 1.4142135623730951f) { + m *= 0.5f; + e++; + } + + r = (m - 1.0f) / (m + 1.0f); + s = r * r; + poly = 1.0f + s * (0.33333333f + s * (0.20000000f + + s * (0.14285714f + s * 0.11111111f))); + + return (float)e + (2.8853900817779268f * r) * poly; +} + +NS_SHIM_WEAK float logf(float x) +{ + return ns_log2f(x) * 0.6931471805599453f; +} + +NS_SHIM_WEAK double log(double x) +{ + return (double)logf((float)x); +} + +NS_SHIM_WEAK float sqrtf(float x) +{ + union { float f; uint32_t i; } u = { .f = x }; + float y; + + if (x <= 0.0f) + return 0.0f; + + u.i = 0x1fbd1df5U + (u.i >> 1); + y = u.f; + y = 0.5f * (y + x / y); + y = 0.5f * (y + x / y); + y = 0.5f * (y + x / y); + return y; +} + +NS_SHIM_WEAK double sqrt(double x) +{ + return (double)sqrtf((float)x); +} + +NS_SHIM_WEAK float powf(float x, float y) +{ + if (x <= 0.0f) + return (x == 0.0f && y > 0.0f) ? 0.0f : 1.0f; + if (y == 0.0f) + return 1.0f; + if (y == 1.0f) + return x; + if (y == 2.0f) + return x * x; + if (y == 0.5f) + return sqrtf(x); + return ns_exp2f(y * ns_log2f(x)); +} + +static inline float ns_floorf(float x) +{ + int i = (int)x; + + if (x < 0.0f && (float)i != x) + i--; + return (float)i; +} + +NS_SHIM_WEAK float floorf(float x) +{ + return ns_floorf(x); +} + +NS_SHIM_WEAK double floor(double x) +{ + return (double)ns_floorf((float)x); +} + +static inline float ns_ceilf(float x) +{ + int i = (int)x; + + if (x > 0.0f && (float)i != x) + i++; + return (float)i; +} + +static inline float ns_roundf(float x) +{ + return (x >= 0.0f) ? ns_floorf(x + 0.5f) : ns_ceilf(x - 0.5f); +} + +static inline void ns_sincosf_poly(float z, float *s_out, float *c_out) +{ + float z2 = z * z; + + *s_out = z * (1.0f + z2 * (-0.166666667f + z2 * (0.008333333f - z2 * 0.0001984127f))); + *c_out = 1.0f + z2 * (-0.5f + z2 * (0.041666667f + z2 * (-0.0013888889f + z2 * 0.0000248016f))); +} + +NS_SHIM_WEAK float sinf(float x) +{ + const float inv_half_pi = 0.636619772367581343f; + const float half_pi_hi = 1.5707963267948966f; + int k = (int)ns_roundf(x * inv_half_pi); + float z = x - (float)k * half_pi_hi; + float s, c; + + ns_sincosf_poly(z, &s, &c); + + switch ((k % 4 + 4) % 4) { + case 0: return s; + case 1: return c; + case 2: return -s; + case 3: default: return -c; + } +} + +NS_SHIM_WEAK double sin(double x) +{ + return (double)sinf((float)x); +} + +NS_SHIM_WEAK float cosf(float x) +{ + const float inv_half_pi = 0.636619772367581343f; + const float half_pi_hi = 1.5707963267948966f; + int k = (int)ns_roundf(x * inv_half_pi); + float z = x - (float)k * half_pi_hi; + float s, c; + + ns_sincosf_poly(z, &s, &c); + + switch ((k % 4 + 4) % 4) { + case 0: return c; + case 1: return -s; + case 2: return -c; + case 3: default: return s; + } +} + +NS_SHIM_WEAK double cos(double x) { - if (x == 1.0f) - return 0.7853981633974483f; - float x2 = x * x; - return x * (1.0f + 0.28f * x2) / (1.0f + 0.613f * x2); + return (double)cosf((float)x); } diff --git a/src/audio/webrtc_ns/webrtc_ns.c b/src/audio/webrtc_ns/webrtc_ns.c index 53be280cf124..568cc7803241 100644 --- a/src/audio/webrtc_ns/webrtc_ns.c +++ b/src/audio/webrtc_ns/webrtc_ns.c @@ -83,7 +83,7 @@ __cold static int webrtc_ns_prepare(struct processing_module *mod, { struct webrtc_ns_comp_data *cd = module_get_private_data(mod); struct comp_dev *dev = mod->dev; - int rate, fmt, ret; + int rate, in_fmt, out_fmt, ret; assert_can_be_cold(); @@ -93,9 +93,16 @@ __cold static int webrtc_ns_prepare(struct processing_module *mod, } rate = source_get_rate(sources[0]); - fmt = source_get_frm_fmt(sources[0]); + in_fmt = source_get_frm_fmt(sources[0]); + out_fmt = sink_get_frm_fmt(sinks[0]); cd->channels = source_get_channels(sources[0]); + if (cd->channels != sink_get_channels(sinks[0]) || + rate != sink_get_rate(sinks[0])) { + comp_err(dev, "webrtc_ns: source/sink mismatch"); + return -EINVAL; + } + if (cd->channels > WEBRTC_NS_CHANNELS_MAX) { comp_err(dev, "webrtc_ns: too many channels %d (max %d)", cd->channels, WEBRTC_NS_CHANNELS_MAX); @@ -108,12 +115,9 @@ __cold static int webrtc_ns_prepare(struct processing_module *mod, return -EINVAL; } - switch (fmt) { - case SOF_IPC_FRAME_S16_LE: - case SOF_IPC_FRAME_S32_LE: - break; - default: - comp_err(dev, "webrtc_ns: unsupported frame format %d", fmt); + if ((in_fmt != SOF_IPC_FRAME_S16_LE && in_fmt != SOF_IPC_FRAME_S32_LE) || + (out_fmt != SOF_IPC_FRAME_S16_LE && out_fmt != SOF_IPC_FRAME_S32_LE)) { + comp_err(dev, "webrtc_ns: unsupported format in=%d out=%d", in_fmt, out_fmt); return -EINVAL; } @@ -123,8 +127,9 @@ __cold static int webrtc_ns_prepare(struct processing_module *mod, cd->proc_frame_samples = (cd->proc_rate * 10) / 1000; /* 10 ms */ cd->in_frame_samples = cd->proc_frame_samples; - comp_info(dev, "webrtc_ns: in_rate=%d proc_rate=%d ch=%d proc_frame=%d", - cd->in_rate, cd->proc_rate, cd->channels, cd->proc_frame_samples); + comp_info(dev, "webrtc_ns: in_rate=%d proc_rate=%d ch=%d in_fmt=%d out_fmt=%d proc_frame=%d", + cd->in_rate, cd->proc_rate, cd->channels, in_fmt, out_fmt, + cd->proc_frame_samples); webrtc_ns_set_active_mod(mod); @@ -225,13 +230,16 @@ static int webrtc_ns_process(struct processing_module *mod, struct webrtc_ns_comp_data *cd = module_get_private_data(mod); struct sof_source *src = sources[0]; struct sof_sink *snk = sinks[0]; - enum sof_ipc_frame fmt = source_get_frm_fmt(src); - size_t sample_bytes = (fmt == SOF_IPC_FRAME_S16_LE) ? sizeof(int16_t) : sizeof(int32_t); - size_t frame_bytes = source_get_frame_bytes(src); + enum sof_ipc_frame in_fmt = source_get_frm_fmt(src); + enum sof_ipc_frame out_fmt = sink_get_frm_fmt(snk); + size_t in_sample_bytes = (in_fmt == SOF_IPC_FRAME_S16_LE) ? sizeof(int16_t) : sizeof(int32_t); + size_t out_sample_bytes = (out_fmt == SOF_IPC_FRAME_S16_LE) ? sizeof(int16_t) : sizeof(int32_t); + size_t in_frame_bytes = source_get_frame_bytes(src); + size_t out_frame_bytes = sink_get_frame_bytes(snk); int avail = (int)source_get_data_frames_available(src); int free = (int)sink_get_free_frames(snk); int n = MIN(avail, free); - size_t nbytes, src_bytes, snk_bytes; + size_t in_nbytes, out_nbytes, src_bytes, snk_bytes; struct cir_buf_source source_buf; struct cir_buf_sink sink_buf; int ret, drain, c; @@ -244,14 +252,15 @@ static int webrtc_ns_process(struct processing_module *mod, if (n <= 0) return 0; - nbytes = (size_t)n * frame_bytes; + in_nbytes = (size_t)n * in_frame_bytes; + out_nbytes = (size_t)n * out_frame_bytes; - ret = source_get_data(src, nbytes, &source_buf.ptr, &source_buf.buf_start, &src_bytes); + ret = source_get_data(src, in_nbytes, &source_buf.ptr, &source_buf.buf_start, &src_bytes); if (ret) return ret; source_buf.buf_end = (const char *)source_buf.buf_start + src_bytes; - ret = sink_get_buffer(snk, nbytes, &sink_buf.ptr, &sink_buf.buf_start, &snk_bytes); + ret = sink_get_buffer(snk, out_nbytes, &sink_buf.ptr, &sink_buf.buf_start, &snk_bytes); if (ret) { source_release_data(src, 0); return ret; @@ -266,22 +275,22 @@ static int webrtc_ns_process(struct processing_module *mod, while (remaining) { size_t fmax = circ_buf_frames_without_wrap(r_ptr, source_buf.buf_end, - sample_bytes, cd->channels); + in_sample_bytes, cd->channels); size_t f = MIN(remaining, fmax); - if (fmt == SOF_IPC_FRAME_S32_LE) + if (in_fmt == SOF_IPC_FRAME_S32_LE) s32_to_float(cd, r_ptr, f, offset); else s16_to_float(cd, r_ptr, f, offset); remaining -= f; offset += f; - r_ptr = source_cir_buf_wrap((const char *)r_ptr + f * frame_bytes, + r_ptr = source_cir_buf_wrap((const char *)r_ptr + f * in_frame_bytes, source_buf.buf_start, source_buf.buf_end); } } - source_release_data(src, nbytes); + source_release_data(src, in_nbytes); cd->buffered_in_frames += n; /* @@ -327,17 +336,17 @@ static int webrtc_ns_process(struct processing_module *mod, while (remaining) { size_t fmax = circ_buf_frames_without_wrap(w_ptr, sink_buf.buf_end, - sample_bytes, cd->channels); + out_sample_bytes, cd->channels); size_t f = MIN(remaining, fmax); - if (fmt == SOF_IPC_FRAME_S32_LE) + if (out_fmt == SOF_IPC_FRAME_S32_LE) float_to_s32(cd, w_ptr, f, offset); else float_to_s16(cd, w_ptr, f, offset); remaining -= f; offset += f; - w_ptr = cir_buf_wrap((char *)w_ptr + f * frame_bytes, + w_ptr = cir_buf_wrap((char *)w_ptr + f * out_frame_bytes, sink_buf.buf_start, sink_buf.buf_end); } @@ -352,22 +361,22 @@ static int webrtc_ns_process(struct processing_module *mod, /* If buffered_out_frames was somehow less than n, zero remainder */ if (drain < n) { size_t remaining = n - drain; - void *w_ptr = (char *)sink_buf.ptr + drain * frame_bytes; + void *w_ptr = (char *)sink_buf.ptr + drain * out_frame_bytes; w_ptr = cir_buf_wrap(w_ptr, sink_buf.buf_start, sink_buf.buf_end); while (remaining) { size_t fmax = circ_buf_frames_without_wrap(w_ptr, sink_buf.buf_end, - sample_bytes, cd->channels); + out_sample_bytes, cd->channels); size_t f = MIN(remaining, fmax); - memset(w_ptr, 0, f * frame_bytes); + memset(w_ptr, 0, f * out_frame_bytes); remaining -= f; - w_ptr = cir_buf_wrap((char *)w_ptr + f * frame_bytes, + w_ptr = cir_buf_wrap((char *)w_ptr + f * out_frame_bytes, sink_buf.buf_start, sink_buf.buf_end); } } - sink_commit_buffer(snk, nbytes); + sink_commit_buffer(snk, out_nbytes); return 0; } diff --git a/src/audio/webrtc_ns/webrtc_ns.cmake b/src/audio/webrtc_ns/webrtc_ns.cmake index fb39b40e1793..7663fde6e99e 100644 --- a/src/audio/webrtc_ns/webrtc_ns.cmake +++ b/src/audio/webrtc_ns/webrtc_ns.cmake @@ -26,11 +26,8 @@ if(NOT EXISTS "${SOF_WEBRTC_APM_SRC_DIR}/webrtc/modules/audio_processing/ns/ns_c "-DSOF_WEBRTC_APM_SRC_DIR=.") endif() -if(NOT DEFINED CMAKE_AR) - set(_ns_ar "ar") -else() - set(_ns_ar "${CMAKE_AR}") -endif() +include("${sof_top_dir}/scripts/cmake/xtensa_ext_cc.cmake") +xtensa_ext_cc_get_flags(_ns_arch_cflags _ns_ar) set(WEBRTC_NS_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/webrtc-ns-install" CACHE INTERNAL "webrtc_ns: NS library install prefix") @@ -46,7 +43,7 @@ OBJ=${CMAKE_CURRENT_BINARY_DIR}/webrtc-ns-obj mkdir -p \"$OBJ\" \"$INST/lib\" \"$INST/include\" -CFLAGS=\"-O2 -fPIC -DWEBRTC_ARCH_32_BITS= -DWEBRTC_ARCH_LITTLE_ENDIAN= -I${SOF_WEBRTC_APM_SRC_DIR} -I$SRC/modules/audio_processing/ns/include -Dmalloc=webrtc_ns_malloc -Dcalloc=webrtc_ns_calloc -Dfree=webrtc_ns_free\" +CFLAGS=\"-O2 ${_ns_arch_cflags} -DNDEBUG -DWEBRTC_ARCH_32_BITS= -DWEBRTC_ARCH_LITTLE_ENDIAN= -I${SOF_WEBRTC_APM_SRC_DIR} -I$SRC/modules/audio_processing/ns/include -Dmalloc=webrtc_ns_malloc -Dcalloc=webrtc_ns_calloc -Dfree=webrtc_ns_free\" SRCS=\" $SRC/modules/audio_processing/ns/noise_suppression.c diff --git a/src/audio/webrtc_ns2/Kconfig b/src/audio/webrtc_ns2/Kconfig index 26bd5b055f9c..b3e7b50bb197 100644 --- a/src/audio/webrtc_ns2/Kconfig +++ b/src/audio/webrtc_ns2/Kconfig @@ -2,6 +2,7 @@ config COMP_WEBRTC_NS2 tristate "RNNoise deep-learning noise suppressor (floating-point)" + depends on IPC_MAJOR_4 help Select to include the RNNoise module — a recurrent neural network noise suppressor originally developed by Jean-Marc Valin (Mozilla / diff --git a/src/audio/webrtc_ns2/llext/CMakeLists.txt b/src/audio/webrtc_ns2/llext/CMakeLists.txt index 9df3309b1797..2f709a01d8cd 100644 --- a/src/audio/webrtc_ns2/llext/CMakeLists.txt +++ b/src/audio/webrtc_ns2/llext/CMakeLists.txt @@ -5,7 +5,6 @@ if(CONFIG_COMP_WEBRTC_NS2_STUB) sof_llext_build("webrtc_ns2" SOURCES ../webrtc_ns2.c ../webrtc_ns2-stub.c - LIB openmodules ) else() include(${CMAKE_CURRENT_LIST_DIR}/../webrtc_ns2.cmake) @@ -17,7 +16,6 @@ else() INCLUDES "${WEBRTC_NS2_INSTALL_DIR}/include" LIBS_PATH "${WEBRTC_NS2_INSTALL_DIR}/lib" LIBS rnnoise - LIB openmodules ) add_dependencies(webrtc_ns2_llext_lib rnnoise_ext) diff --git a/src/audio/webrtc_ns2/webrtc_ns2-rnn.c b/src/audio/webrtc_ns2/webrtc_ns2-rnn.c index e8986a51263d..b7885eb67ec6 100644 --- a/src/audio/webrtc_ns2/webrtc_ns2-rnn.c +++ b/src/audio/webrtc_ns2/webrtc_ns2-rnn.c @@ -41,9 +41,9 @@ LOG_MODULE_DECLARE(webrtc_ns2, CONFIG_SOF_LOG_LEVEL); struct webrtc_ns2_rnn_data { DenoiseState *st[WEBRTC_NS2_CHANNELS_MAX]; + void *scratch; int num_channels; - float rnn_in[WEBRTC_NS2_FRAME_SAMPLES]; - float rnn_out[WEBRTC_NS2_FRAME_SAMPLES]; + float rnn_buf[WEBRTC_NS2_FRAME_SAMPLES]; }; static int webrtc_ns2_rnn_init(struct processing_module *mod) @@ -69,6 +69,20 @@ static int webrtc_ns2_rnn_configure(struct processing_module *mod, int num_chann webrtc_ns2_set_active_mod(mod); + if (!rd->scratch) { + rd->scratch = mod_zalloc(mod, (size_t)rnnoise_get_scratch_size()); + if (!rd->scratch) { + comp_err(mod->dev, "webrtc_ns2: scratch alloc failed"); + return -ENOMEM; + } + } + if (rnnoise_init_scratch(rd->scratch) < 0) { + comp_err(mod->dev, "webrtc_ns2: scratch init failed"); + mod_free(mod, rd->scratch); + rd->scratch = NULL; + return -ENOMEM; + } + /* Free any previous instances from a re-prepare. */ for (c = 0; c < rd->num_channels; c++) { if (rd->st[c]) { @@ -86,6 +100,7 @@ static int webrtc_ns2_rnn_configure(struct processing_module *mod, int num_chann "webrtc_ns2: rnnoise_create() failed ch%d", c); goto err; } + rnnoise_set_scratch(rd->st[c], rd->scratch); } rd->num_channels = num_channels; @@ -116,12 +131,12 @@ static float webrtc_ns2_rnn_process_ch(struct processing_module *mod, * down afterward. */ for (i = 0; i < WEBRTC_NS2_FRAME_SAMPLES; i++) - rd->rnn_in[i] = in[i] * RNN_FULL_SCALE; + rd->rnn_buf[i] = in[i] * RNN_FULL_SCALE; - vad_prob = rnnoise_process_frame(rd->st[ch], rd->rnn_out, rd->rnn_in); + vad_prob = rnnoise_process_frame(rd->st[ch], rd->rnn_buf, rd->rnn_buf); for (i = 0; i < WEBRTC_NS2_FRAME_SAMPLES; i++) - out[i] = rd->rnn_out[i] / RNN_FULL_SCALE; + out[i] = rd->rnn_buf[i] / RNN_FULL_SCALE; return vad_prob; } @@ -145,6 +160,7 @@ static int webrtc_ns2_rnn_reset(struct processing_module *mod) comp_err(mod->dev, "webrtc_ns2: reset failed ch%d", c); return -ENOMEM; } + rnnoise_set_scratch(rd->st[c], rd->scratch); } } return 0; @@ -167,6 +183,11 @@ static int webrtc_ns2_rnn_free(struct processing_module *mod) rd->st[c] = NULL; } } + if (rd->scratch) { + rnnoise_free_scratch(rd->scratch); + mod_free(mod, rd->scratch); + rd->scratch = NULL; + } mod_free(mod, rd); cd->backend_data = NULL; return 0; diff --git a/src/audio/webrtc_ns2/webrtc_ns2-shims.c b/src/audio/webrtc_ns2/webrtc_ns2-shims.c index e6064d31e0e6..ed509d05a8c2 100644 --- a/src/audio/webrtc_ns2/webrtc_ns2-shims.c +++ b/src/audio/webrtc_ns2/webrtc_ns2-shims.c @@ -9,7 +9,7 @@ #include #include #include -#if !defined(LLEXT_MODULE) +#if !defined(LLEXT_MODULE) && !defined(LL_EXTENSION_BUILD) #include static APP_SYSUSER_BSS struct processing_module *active_ns2_mod; #else @@ -56,7 +56,7 @@ void *webrtc_ns2_calloc(size_t nmemb, size_t size) return p; } -#if defined(LLEXT_MODULE) +#if defined(LLEXT_MODULE) || defined(LL_EXTENSION_BUILD) /* ============================ Memory ============================ */ void *malloc(size_t size) @@ -111,6 +111,7 @@ void *memmove(void *dest, const void *src, size_t n) } return dest; } +#endif /* LLEXT_MODULE || LL_EXTENSION_BUILD */ /* ============================ Math ============================== */ @@ -129,31 +130,58 @@ static inline float ns2_ldexp2(int n) static float ns2_log2f(float x) { union { float f; uint32_t i; } u = { .f = x }; - int e; - float m, z; + int e, e_bias = 127; + float m, r, s, poly; if (x <= 0.0f) return -127.0f; - e = (int)((u.i >> 23) & 0xff) - 127; + if (u.i < 0x00800000U) { + x *= 16777216.0f; + u.f = x; + e_bias = 151; + } + + e = (int)((u.i >> 23) & 0xff) - e_bias; u.i = (u.i & 0x007fffff) | 0x3f800000; m = u.f; - z = m - 1.0f; + if (m > 1.4142135623730951f) { + m *= 0.5f; + e++; + } + + r = (m - 1.0f) / (m + 1.0f); + s = r * r; + poly = 1.0f + s * (0.33333333f + s * (0.20000000f + + s * (0.14285714f + s * 0.11111111f))); - return (float)e + z * (1.44269504f - z * (0.72134752f - z * 0.48089834f)); + return (float)e + (2.8853900817779268f * r) * poly; } -float log10f(float x) +/* + * TODO: Remove this workaround and restore uniform __attribute__((weak)) + * linkage once Zephyr's Xtensa LLEXT loader (llext_link_plt()) supports + * STB_WEAK relocations. Currently it only relocates STB_GLOBAL and STB_LOCAL + * symbols, leaving STB_WEAK slots zeroed, while standalone LLEXTs have + * isolated private symbol tables and will not collide across modules. + */ +#if defined(LLEXT_MODULE) || defined(LL_EXTENSION_BUILD) +#define NS2_SHIM_WEAK +#else +#define NS2_SHIM_WEAK __attribute__((weak)) +#endif + +NS2_SHIM_WEAK float log10f(float x) { return ns2_log2f(x) * 0.3010299956639812f; } -double log10(double x) +NS2_SHIM_WEAK double log10(double x) { return (double)log10f((float)x); } -float floorf(float x) +NS2_SHIM_WEAK float floorf(float x) { int i = (int)x; @@ -162,12 +190,12 @@ float floorf(float x) return (float)i; } -double floor(double x) +NS2_SHIM_WEAK double floor(double x) { return (double)floorf((float)x); } -float ceilf(float x) +NS2_SHIM_WEAK float ceilf(float x) { int i = (int)x; @@ -181,24 +209,33 @@ static inline float ns2_roundf(float x) return (x >= 0.0f) ? floorf(x + 0.5f) : ceilf(x - 0.5f); } -float fabsf(float x) +NS2_SHIM_WEAK float fabsf(float x) { return (x < 0.0f) ? -x : x; } -double fabs(double x) +NS2_SHIM_WEAK double fabs(double x) { return (x < 0.0) ? -x : x; } -float sqrtf(float x) +NS2_SHIM_WEAK float sqrtf(float x) { + union { float f; uint32_t i; } u = { .f = x }; + float y; + if (x <= 0.0f) return 0.0f; - return __builtin_sqrtf(x); + + u.i = 0x1fbd1df5U + (u.i >> 1); + y = u.f; + y = 0.5f * (y + x / y); + y = 0.5f * (y + x / y); + y = 0.5f * (y + x / y); + return y; } -double sqrt(double x) +NS2_SHIM_WEAK double sqrt(double x) { return (double)sqrtf((float)x); } @@ -211,7 +248,7 @@ static inline void ns2_sincosf_poly(float z, float *s_out, float *c_out) *c_out = 1.0f + z2 * (-0.5f + z2 * (0.041666667f + z2 * (-0.0013888889f + z2 * 0.0000248016f))); } -float sinf(float x) +NS2_SHIM_WEAK float sinf(float x) { const float inv_half_pi = 0.636619772367581343f; const float half_pi_hi = 1.5707963267948966f; @@ -229,12 +266,12 @@ float sinf(float x) } } -double sin(double x) +NS2_SHIM_WEAK double sin(double x) { return (double)sinf((float)x); } -float cosf(float x) +NS2_SHIM_WEAK float cosf(float x) { const float inv_half_pi = 0.636619772367581343f; const float half_pi_hi = 1.5707963267948966f; @@ -252,37 +289,39 @@ float cosf(float x) } } -double cos(double x) +NS2_SHIM_WEAK double cos(double x) { return (double)cosf((float)x); } -float exp2f(float x) +NS2_SHIM_WEAK float exp2f(float x) { int n; float f, p; - if (x > 127.0f) + if (x >= 127.0f) return 3.40282347e+38f; if (x < -126.0f) return 0.0f; - n = (int)x; - if (x < 0.0f && (float)n != x) - n--; - f = x - (float)n; + n = (int)(x >= 0.0f ? x + 0.5f : x - 0.5f); + if (n > 127) + return 3.40282347e+38f; + if (n < -126) + return 0.0f; - p = 1.0f + f * (0.6931472f + f * (0.2402265f + f * 0.0555041f)); + f = (x - (float)n) * 0.69314718f; + p = 1.0f + f * (1.0f + f * (0.5f + f * (0.16666667f + + f * (0.04166667f + f * (0.00833333f + f * 0.00138889f))))); return p * ns2_ldexp2(n); } -float expf(float x) +NS2_SHIM_WEAK float expf(float x) { return exp2f(x * 1.4426950408889634f); } -double exp(double x) +NS2_SHIM_WEAK double exp(double x) { return (double)expf((float)x); } -#endif /* LLEXT_MODULE */ diff --git a/src/audio/webrtc_ns2/webrtc_ns2.c b/src/audio/webrtc_ns2/webrtc_ns2.c index d47987325383..cc857df4f2c4 100644 --- a/src/audio/webrtc_ns2/webrtc_ns2.c +++ b/src/audio/webrtc_ns2/webrtc_ns2.c @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -42,7 +41,7 @@ static inline void src_to_float(struct webrtc_ns2_comp_data *cd, { int i, c, ch = cd->channels; - if (cd->is_s32) { + if (cd->in_s32) { const int32_t *p = raw; for (i = 0; i < n_frames; i++) @@ -64,7 +63,7 @@ static inline void float_to_sink(struct webrtc_ns2_comp_data *cd, { int i, c, ch = cd->channels; - if (cd->is_s32) { + if (cd->out_s32) { int32_t *p = raw; for (i = 0; i < n_frames; i++) { @@ -138,7 +137,7 @@ __cold static int webrtc_ns2_prepare(struct processing_module *mod, { struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); struct comp_dev *dev = mod->dev; - int rate, fmt, ret; + int rate, in_fmt, out_fmt, ret; assert_can_be_cold(); @@ -148,9 +147,16 @@ __cold static int webrtc_ns2_prepare(struct processing_module *mod, } rate = source_get_rate(sources[0]); - fmt = source_get_frm_fmt(sources[0]); + in_fmt = source_get_frm_fmt(sources[0]); + out_fmt = sink_get_frm_fmt(sinks[0]); cd->channels = source_get_channels(sources[0]); + if (cd->channels != sink_get_channels(sinks[0]) || + rate != sink_get_rate(sinks[0])) { + comp_err(dev, "webrtc_ns2: source/sink mismatch"); + return -EINVAL; + } + /* RNNoise is hard-wired to 48 kHz. */ if (rate != WEBRTC_NS2_SAMPLE_RATE) { comp_err(dev, "webrtc_ns2: rate %d != required %d Hz; " @@ -164,21 +170,19 @@ __cold static int webrtc_ns2_prepare(struct processing_module *mod, return -EINVAL; } - switch (fmt) { - case SOF_IPC_FRAME_S16_LE: - cd->is_s32 = false; - break; - case SOF_IPC_FRAME_S32_LE: - cd->is_s32 = true; - break; - default: - comp_err(dev, "webrtc_ns2: unsupported format %d", fmt); + if ((in_fmt != SOF_IPC_FRAME_S16_LE && in_fmt != SOF_IPC_FRAME_S32_LE) || + (out_fmt != SOF_IPC_FRAME_S16_LE && out_fmt != SOF_IPC_FRAME_S32_LE)) { + comp_err(dev, "webrtc_ns2: unsupported format in=%d out=%d", + in_fmt, out_fmt); return -EINVAL; } - comp_info(dev, "webrtc_ns2: rate=%d ch=%d %s frame=%d", - rate, cd->channels, cd->is_s32 ? "S32" : "S16", - WEBRTC_NS2_FRAME_SAMPLES); + cd->in_s32 = (in_fmt == SOF_IPC_FRAME_S32_LE); + cd->out_s32 = (out_fmt == SOF_IPC_FRAME_S32_LE); + + comp_info(dev, "webrtc_ns2: rate=%d ch=%d %s->%s frame=%d", + rate, cd->channels, cd->in_s32 ? "S32" : "S16", + cd->out_s32 ? "S32" : "S16", WEBRTC_NS2_FRAME_SAMPLES); if (cd->backend->configure) { ret = cd->backend->configure(mod, cd->channels); @@ -204,12 +208,14 @@ static int webrtc_ns2_process(struct processing_module *mod, struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); struct sof_source *src = sources[0]; struct sof_sink *snk = sinks[0]; - size_t sample_bytes = cd->is_s32 ? sizeof(int32_t) : sizeof(int16_t); - int frame_bytes = source_get_frame_bytes(src); + size_t in_sample_bytes = cd->in_s32 ? sizeof(int32_t) : sizeof(int16_t); + size_t out_sample_bytes = cd->out_s32 ? sizeof(int32_t) : sizeof(int16_t); + int in_frame_bytes = source_get_frame_bytes(src); + int out_frame_bytes = sink_get_frame_bytes(snk); int avail = (int)source_get_data_frames_available(src); int free_f = (int)sink_get_free_frames(snk); int n = MIN(avail, free_f); - size_t nbytes, src_bytes, snk_bytes; + size_t in_nbytes, out_nbytes, src_bytes, snk_bytes; struct cir_buf_source source_buf; struct cir_buf_sink sink_buf; int ret, drain, c; @@ -222,14 +228,15 @@ static int webrtc_ns2_process(struct processing_module *mod, if (n <= 0) return 0; - nbytes = (size_t)n * frame_bytes; + in_nbytes = (size_t)n * in_frame_bytes; + out_nbytes = (size_t)n * out_frame_bytes; - ret = source_get_data(src, nbytes, &source_buf.ptr, &source_buf.buf_start, &src_bytes); + ret = source_get_data(src, in_nbytes, &source_buf.ptr, &source_buf.buf_start, &src_bytes); if (ret) return ret; source_buf.buf_end = (const char *)source_buf.buf_start + src_bytes; - ret = sink_get_buffer(snk, nbytes, &sink_buf.ptr, &sink_buf.buf_start, &snk_bytes); + ret = sink_get_buffer(snk, out_nbytes, &sink_buf.ptr, &sink_buf.buf_start, &snk_bytes); if (ret) { source_release_data(src, 0); return ret; @@ -244,17 +251,17 @@ static int webrtc_ns2_process(struct processing_module *mod, while (remaining) { size_t fmax = circ_buf_frames_without_wrap(r_ptr, source_buf.buf_end, - sample_bytes, cd->channels); + in_sample_bytes, cd->channels); size_t f = MIN(remaining, fmax); src_to_float(cd, r_ptr, f, offset); remaining -= f; offset += f; - r_ptr = source_cir_buf_wrap((const char *)r_ptr + f * frame_bytes, + r_ptr = source_cir_buf_wrap((const char *)r_ptr + f * in_frame_bytes, source_buf.buf_start, source_buf.buf_end); } } - source_release_data(src, nbytes); + source_release_data(src, in_nbytes); cd->buffered_in_frames += n; /* Process complete 480-sample blocks into out_fifo */ @@ -287,9 +294,13 @@ static int webrtc_ns2_process(struct processing_module *mod, int vad = (vad_prob >= cd->vad_threshold) ? 1 : 0; if (vad != cd->last_vad) { - notifier_event(mod->dev, NOTIFIER_ID_VAD, - NOTIFIER_TARGET_CORE_LOCAL, - &vad, sizeof(vad)); + /* + * TODO: Deliver VAD state changes via AMS / Zephyr + * k_msgq or an IPC4 control notification once a + * consumer is wired; legacy notifier_event() is + * deprecated for userspace LLEXTs. + */ + comp_dbg(mod->dev, "webrtc_ns2: vad=%d", vad); cd->last_vad = vad; } } @@ -305,13 +316,13 @@ static int webrtc_ns2_process(struct processing_module *mod, while (remaining) { size_t fmax = circ_buf_frames_without_wrap(w_ptr, sink_buf.buf_end, - sample_bytes, cd->channels); + out_sample_bytes, cd->channels); size_t f = MIN(remaining, fmax); float_to_sink(cd, w_ptr, f, offset); remaining -= f; offset += f; - w_ptr = cir_buf_wrap((char *)w_ptr + f * frame_bytes, + w_ptr = cir_buf_wrap((char *)w_ptr + f * out_frame_bytes, sink_buf.buf_start, sink_buf.buf_end); } @@ -326,22 +337,22 @@ static int webrtc_ns2_process(struct processing_module *mod, if (drain < n) { size_t remaining = n - drain; - void *w_ptr = (char *)sink_buf.ptr + drain * frame_bytes; + void *w_ptr = (char *)sink_buf.ptr + drain * out_frame_bytes; w_ptr = cir_buf_wrap(w_ptr, sink_buf.buf_start, sink_buf.buf_end); while (remaining) { size_t fmax = circ_buf_frames_without_wrap(w_ptr, sink_buf.buf_end, - sample_bytes, cd->channels); + out_sample_bytes, cd->channels); size_t f = MIN(remaining, fmax); - memset(w_ptr, 0, f * frame_bytes); + memset(w_ptr, 0, f * out_frame_bytes); remaining -= f; - w_ptr = cir_buf_wrap((char *)w_ptr + f * frame_bytes, + w_ptr = cir_buf_wrap((char *)w_ptr + f * out_frame_bytes, sink_buf.buf_start, sink_buf.buf_end); } } - sink_commit_buffer(snk, nbytes); + sink_commit_buffer(snk, out_nbytes); return 0; } diff --git a/src/audio/webrtc_ns2/webrtc_ns2.cmake b/src/audio/webrtc_ns2/webrtc_ns2.cmake index b9c78c55e6ca..68f479d9f239 100644 --- a/src/audio/webrtc_ns2/webrtc_ns2.cmake +++ b/src/audio/webrtc_ns2/webrtc_ns2.cmake @@ -22,11 +22,8 @@ if(NOT EXISTS "${SOF_RNNOISE_SRC_DIR}/src/denoise.c") "-DSOF_RNNOISE_SRC_DIR=.") endif() -if(NOT DEFINED CMAKE_AR) - set(_rnn_ar "ar") -else() - set(_rnn_ar "${CMAKE_AR}") -endif() +include("${sof_top_dir}/scripts/cmake/xtensa_ext_cc.cmake") +xtensa_ext_cc_get_flags(_rnn_arch_cflags _rnn_ar) set(WEBRTC_NS2_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/rnnoise-install" CACHE INTERNAL "webrtc_ns2: RNNoise library install prefix") @@ -44,7 +41,7 @@ OBJ=${CMAKE_CURRENT_BINARY_DIR}/rnnoise-obj mkdir -p \"$OBJ\" \"$INST/lib\" \"$INST/include\" -CFLAGS=\"-O2 -fPIC -I$SRC -I$INC -DOUTSIDE_SPEEX -DRANDOM_PREFIX=rnn -DFLOAT_APPROX\" +CFLAGS=\"-O2 ${_rnn_arch_cflags} -DNDEBUG -I$SRC -I$INC -DOUTSIDE_SPEEX -DRANDOM_PREFIX=rnn -DFLOAT_APPROX\" # Core RNNoise sources (excludes dump_features.c and train-only files). SRCS=\" diff --git a/src/audio/webrtc_ns2/webrtc_ns2.h b/src/audio/webrtc_ns2/webrtc_ns2.h index 1caf78459397..3ff41e778f6d 100644 --- a/src/audio/webrtc_ns2/webrtc_ns2.h +++ b/src/audio/webrtc_ns2/webrtc_ns2.h @@ -65,7 +65,8 @@ struct webrtc_ns2_comp_data { void *backend_data; int channels; - bool is_s32; /* true when pipeline format is S32_LE */ + bool in_s32; /* true when source format is S32_LE */ + bool out_s32; /* true when sink format is S32_LE */ int buffered_in_frames; int buffered_out_frames; diff --git a/src/audio/webrtc_ns2/webrtc_ns2.toml b/src/audio/webrtc_ns2/webrtc_ns2.toml index 8a35dfb1c38e..716b7c8877e4 100644 --- a/src/audio/webrtc_ns2/webrtc_ns2.toml +++ b/src/audio/webrtc_ns2/webrtc_ns2.toml @@ -9,7 +9,7 @@ uuid = UUIDREG_STR_WEBRTC_NS2 affinity_mask = "0x1" instance_count = "40" - domain_types = "0" + domain_types = "1" load_type = LOAD_TYPE module_type = "9" auto_start = "0" diff --git a/src/audio/webrtc_vad/Kconfig b/src/audio/webrtc_vad/Kconfig index d90a6e6a75ec..44a548257ca9 100644 --- a/src/audio/webrtc_vad/Kconfig +++ b/src/audio/webrtc_vad/Kconfig @@ -2,6 +2,7 @@ config COMP_WEBRTC_VAD tristate "WebRTC Voice Activity Detection (libfvad)" + depends on IPC_MAJOR_4 help Select to include the WebRTC Voice Activity Detection module. It wraps the libfvad library (a standalone pure-C extraction of the diff --git a/src/audio/webrtc_vad/llext/CMakeLists.txt b/src/audio/webrtc_vad/llext/CMakeLists.txt index 7d12eb109978..7bf517baa76c 100644 --- a/src/audio/webrtc_vad/llext/CMakeLists.txt +++ b/src/audio/webrtc_vad/llext/CMakeLists.txt @@ -6,7 +6,6 @@ if(CONFIG_COMP_WEBRTC_VAD_STUB) sof_llext_build("webrtc_vad" SOURCES ../webrtc_vad.c ../webrtc_vad-stub.c - LIB openmodules ) else() # Real VAD: cross-build libfvad and link it. @@ -19,7 +18,6 @@ else() INCLUDES "${LIBFVAD_INSTALL_DIR}/include" LIBS_PATH "${LIBFVAD_INSTALL_DIR}/lib" LIBS fvad - LIB openmodules ) # The archive must be built before the module compiles/links against it. diff --git a/src/audio/webrtc_vad/webrtc_vad-fvad.c b/src/audio/webrtc_vad/webrtc_vad-fvad.c index bfb9b3f7cdbc..ae7ea47075fd 100644 --- a/src/audio/webrtc_vad/webrtc_vad-fvad.c +++ b/src/audio/webrtc_vad/webrtc_vad-fvad.c @@ -29,7 +29,7 @@ LOG_MODULE_DECLARE(webrtc_vad, CONFIG_SOF_LOG_LEVEL); -#if defined(LLEXT_MODULE) +#if defined(LLEXT_MODULE) || defined(LL_EXTENSION_BUILD) void webrtc_vad_set_active_mod(struct processing_module *mod); #else static inline void webrtc_vad_set_active_mod(struct processing_module *mod) { (void)mod; } diff --git a/src/audio/webrtc_vad/webrtc_vad-shims.c b/src/audio/webrtc_vad/webrtc_vad-shims.c index b8ac6860a2c5..52346cca6de3 100644 --- a/src/audio/webrtc_vad/webrtc_vad-shims.c +++ b/src/audio/webrtc_vad/webrtc_vad-shims.c @@ -17,7 +17,7 @@ #include #include #include -#if !defined(LLEXT_MODULE) +#if !defined(LLEXT_MODULE) && !defined(LL_EXTENSION_BUILD) #include static APP_SYSUSER_BSS struct processing_module *active_vad_mod; #else @@ -29,7 +29,7 @@ void webrtc_vad_set_active_mod(struct processing_module *mod) active_vad_mod = mod; } -#if defined(LLEXT_MODULE) +#if defined(LLEXT_MODULE) || defined(LL_EXTENSION_BUILD) /* ============================ Memory ============================ */ void *malloc(size_t size) @@ -110,4 +110,4 @@ int memcmp(const void *s1, const void *s2, size_t n) } return 0; } -#endif /* LLEXT_MODULE */ +#endif /* LLEXT_MODULE || LL_EXTENSION_BUILD */ diff --git a/src/audio/webrtc_vad/webrtc_vad.c b/src/audio/webrtc_vad/webrtc_vad.c index 972ac594a9bc..7f2fdb8b9f62 100644 --- a/src/audio/webrtc_vad/webrtc_vad.c +++ b/src/audio/webrtc_vad/webrtc_vad.c @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include @@ -86,13 +85,26 @@ __cold static int webrtc_vad_init(struct processing_module *mod) * * Return: 0 on success, negative errno on failure. */ +static int webrtc_vad_sample_bytes(enum sof_ipc_frame fmt) +{ + switch (fmt) { + case SOF_IPC_FRAME_S16_LE: + return sizeof(int16_t); + case SOF_IPC_FRAME_S32_LE: + return sizeof(int32_t); + default: + return -EINVAL; + } +} + __cold static int webrtc_vad_prepare(struct processing_module *mod, struct sof_source **sources, int num_of_sources, struct sof_sink **sinks, int num_of_sinks) { struct webrtc_vad_comp_data *cd = module_get_private_data(mod); struct comp_dev *dev = mod->dev; - int rate, fmt, ret; + enum sof_ipc_frame in_fmt, out_fmt; + int rate, in_sb, out_sb, ret; int frame_ms = CONFIG_WEBRTC_VAD_FRAME_MS; assert_can_be_cold(); @@ -108,27 +120,34 @@ __cold static int webrtc_vad_prepare(struct processing_module *mod, } rate = source_get_rate(sources[0]); - fmt = source_get_frm_fmt(sources[0]); + in_fmt = source_get_frm_fmt(sources[0]); + out_fmt = sink_get_frm_fmt(sinks[0]); cd->channels = source_get_channels(sources[0]); cd->sample_rate = rate; + if (cd->channels != sink_get_channels(sinks[0]) || + rate != sink_get_rate(sinks[0])) { + comp_err(dev, "webrtc_vad: source/sink mismatch"); + return -EINVAL; + } + /* libfvad accepts 8/16/32/48 kHz. */ if (rate != 8000 && rate != 16000 && rate != 32000 && rate != 48000) { comp_err(dev, "webrtc_vad: unsupported rate %d", rate); return -EINVAL; } - switch (fmt) { - case SOF_IPC_FRAME_S16_LE: - cd->sample_bytes = sizeof(int16_t); - break; - case SOF_IPC_FRAME_S32_LE: - cd->sample_bytes = sizeof(int32_t); - break; - default: - comp_err(dev, "webrtc_vad: unsupported frame format %d", fmt); + in_sb = webrtc_vad_sample_bytes(in_fmt); + out_sb = webrtc_vad_sample_bytes(out_fmt); + if (in_sb < 0 || out_sb < 0) { + comp_err(dev, "webrtc_vad: unsupported format in=%d out=%d", + in_fmt, out_fmt); return -EINVAL; } + cd->in_fmt = in_fmt; + cd->out_fmt = out_fmt; + cd->in_sample_bytes = in_sb; + cd->out_sample_bytes = out_sb; /* Number of samples per VAD frame (mono). */ cd->frame_samples = (rate * frame_ms) / 1000; @@ -137,8 +156,8 @@ __cold static int webrtc_vad_prepare(struct processing_module *mod, return -EINVAL; } - comp_info(dev, "webrtc_vad: rate=%d ch=%d frame_ms=%d frame_samples=%d", - rate, cd->channels, frame_ms, cd->frame_samples); + comp_info(dev, "webrtc_vad: rate=%d ch=%d in_fmt=%d out_fmt=%d frame_ms=%d frame_samples=%d", + rate, cd->channels, in_fmt, out_fmt, frame_ms, cd->frame_samples); if (cd->backend->configure) { ret = cd->backend->configure(mod, rate, CONFIG_WEBRTC_VAD_MODE); @@ -172,7 +191,7 @@ static void webrtc_vad_accumulate(struct webrtc_vad_comp_data *cd, for (i = 0; i < n_frames && cd->accum_samples < WEBRTC_VAD_MAX_FRAME_SAMPLES; i++) { int16_t s; - if (cd->sample_bytes == sizeof(int16_t)) { + if (cd->in_sample_bytes == sizeof(int16_t)) { const int16_t *p = raw; s = p[i * ch]; /* channel 0 */ @@ -199,6 +218,8 @@ static void webrtc_vad_classify_pending(struct processing_module *mod) int decision, remaining; while (cd->accum_samples >= cd->frame_samples) { + int prev_decision = cd->last_decision; + decision = cd->backend->classify(mod, cd->accumulator, cd->frame_samples); if (decision < 0) { @@ -216,10 +237,13 @@ static void webrtc_vad_classify_pending(struct processing_module *mod) (size_t)remaining * sizeof(int16_t)); cd->accum_samples = remaining; - /* Broadcast VAD decision to registered listeners. */ - notifier_event(dev, NOTIFIER_ID_VAD, - NOTIFIER_TARGET_CORE_LOCAL, &decision, - sizeof(decision)); + /* + * TODO: Deliver VAD state changes via AMS / Zephyr k_msgq or an + * IPC4 control notification once a consumer is wired; legacy + * notifier_event() is deprecated for userspace LLEXTs. + */ + if (decision != prev_decision) + comp_dbg(dev, "webrtc_vad: state=%d", decision); } } @@ -244,11 +268,12 @@ static int webrtc_vad_process(struct processing_module *mod, struct webrtc_vad_comp_data *cd = module_get_private_data(mod); struct sof_source *src = sources[0]; struct sof_sink *snk = sinks[0]; - size_t frame_bytes = source_get_frame_bytes(src); + size_t in_frame_bytes = source_get_frame_bytes(src); + size_t out_frame_bytes = sink_get_frame_bytes(snk); int avail = (int)source_get_data_frames_available(src); int free = (int)sink_get_free_frames(snk); int n = MIN(avail, free); - size_t nbytes; + size_t in_nbytes, out_nbytes; struct cir_buf_source source_buf; struct cir_buf_sink sink_buf; size_t src_bytes, snk_bytes; @@ -258,16 +283,17 @@ static int webrtc_vad_process(struct processing_module *mod, if (n <= 0) return 0; - nbytes = (size_t)n * frame_bytes; + in_nbytes = (size_t)n * in_frame_bytes; + out_nbytes = (size_t)n * out_frame_bytes; /* ---- Obtain source data pointer (read-only, zero-copy) ---- */ - ret = source_get_data(src, nbytes, &source_buf.ptr, &source_buf.buf_start, &src_bytes); + ret = source_get_data(src, in_nbytes, &source_buf.ptr, &source_buf.buf_start, &src_bytes); if (ret) return ret; source_buf.buf_end = (const char *)source_buf.buf_start + src_bytes; /* ---- Obtain sink write pointer ---- */ - ret = sink_get_buffer(snk, nbytes, &sink_buf.ptr, &sink_buf.buf_start, &snk_bytes); + ret = sink_get_buffer(snk, out_nbytes, &sink_buf.ptr, &sink_buf.buf_start, &snk_bytes); if (ret) { source_release_data(src, 0); /* release without consuming */ return ret; @@ -277,27 +303,47 @@ static int webrtc_vad_process(struct processing_module *mod, remaining_frames = n; while (remaining_frames) { size_t fmax_src = circ_buf_frames_without_wrap(source_buf.ptr, source_buf.buf_end, - cd->sample_bytes, cd->channels); + cd->in_sample_bytes, cd->channels); size_t fmax_snk = circ_buf_frames_without_wrap(sink_buf.ptr, sink_buf.buf_end, - cd->sample_bytes, cd->channels); + cd->out_sample_bytes, cd->channels); size_t f = MIN(remaining_frames, MIN(fmax_src, fmax_snk)); - /* ---- Pass-through copy ---- */ - memcpy_s(sink_buf.ptr, f * frame_bytes, source_buf.ptr, f * frame_bytes); + /* ---- Pass-through copy / format conversion ---- */ + if (cd->in_fmt == cd->out_fmt) { + memcpy_s(sink_buf.ptr, f * out_frame_bytes, + source_buf.ptr, f * in_frame_bytes); + } else { + size_t samples = f * cd->channels; + size_t i; + + if (cd->in_fmt == SOF_IPC_FRAME_S32_LE) { + const int32_t *s = source_buf.ptr; + int16_t *d = sink_buf.ptr; + + for (i = 0; i < samples; i++) + d[i] = (int16_t)(s[i] >> 16); + } else { + const int16_t *s = source_buf.ptr; + int32_t *d = sink_buf.ptr; + + for (i = 0; i < samples; i++) + d[i] = ((int32_t)s[i]) << 16; + } + } /* ---- Accumulate channel-0 samples for VAD ---- */ webrtc_vad_accumulate(cd, source_buf.ptr, (int)f); remaining_frames -= f; - source_buf.ptr = source_cir_buf_wrap((const char *)source_buf.ptr + f * frame_bytes, + source_buf.ptr = source_cir_buf_wrap((const char *)source_buf.ptr + f * in_frame_bytes, source_buf.buf_start, source_buf.buf_end); - sink_buf.ptr = cir_buf_wrap((char *)sink_buf.ptr + f * frame_bytes, + sink_buf.ptr = cir_buf_wrap((char *)sink_buf.ptr + f * out_frame_bytes, sink_buf.buf_start, sink_buf.buf_end); } /* ---- Commit / release ---- */ - source_release_data(src, nbytes); - sink_commit_buffer(snk, nbytes); + source_release_data(src, in_nbytes); + sink_commit_buffer(snk, out_nbytes); /* ---- Classify any complete frames ---- */ webrtc_vad_classify_pending(mod); diff --git a/src/audio/webrtc_vad/webrtc_vad.cmake b/src/audio/webrtc_vad/webrtc_vad.cmake index d0124c7ded5f..335b5262ce08 100644 --- a/src/audio/webrtc_vad/webrtc_vad.cmake +++ b/src/audio/webrtc_vad/webrtc_vad.cmake @@ -25,20 +25,13 @@ if(NOT EXISTS "${SOF_LIBFVAD_SRC_DIR}/src/fvad.c") "-DSOF_LIBFVAD_SRC_DIR=.") endif() -if(NOT DEFINED CMAKE_AR) - set(_fvad_ar "ar") -else() - set(_fvad_ar "${CMAKE_AR}") -endif() +include("${sof_top_dir}/scripts/cmake/xtensa_ext_cc.cmake") +xtensa_ext_cc_get_flags(_fvad_arch_cflags _fvad_ar) # --- 3. Output directories --- set(LIBFVAD_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/libfvad-install" CACHE INTERNAL "webrtc_vad: libfvad install prefix") -# libfvad has exactly two source files (fvad.c and the internal vad/ sources). -# Rather than invoking its CMakeLists (which would need a CMake toolchain file), -# we compile the objects directly and archive them. This mirrors the libshine -# approach in ffmpeg.cmake. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/fvad-build.sh" "#!/bin/sh set -e @@ -66,7 +59,7 @@ SRCS=\" for f in \$SRCS; do bn=\$(basename \"\$f\" .c) - ${CMAKE_C_COMPILER} -O2 -fPIC \ + ${CMAKE_C_COMPILER} -O2 ${_fvad_arch_cflags} -DNDEBUG \ -I$SRC/include \ -I$SRC/src \ -c \"\$f\" -o \"$OBJ/\${bn}.o\" diff --git a/src/audio/webrtc_vad/webrtc_vad.h b/src/audio/webrtc_vad/webrtc_vad.h index d6fb31ae4791..ab51e70209c3 100644 --- a/src/audio/webrtc_vad/webrtc_vad.h +++ b/src/audio/webrtc_vad/webrtc_vad.h @@ -79,7 +79,10 @@ struct webrtc_vad_comp_data { int sample_rate; int channels; int frame_samples; - int sample_bytes; + enum sof_ipc_frame in_fmt; + enum sof_ipc_frame out_fmt; + int in_sample_bytes; + int out_sample_bytes; int16_t accumulator[WEBRTC_VAD_MAX_FRAME_SAMPLES]; int accum_samples; int last_decision; diff --git a/tools/topology/topology2/include/components/webrtc-ns2.conf b/tools/topology/topology2/include/components/webrtc-ns2.conf index 351ec1caf3fc..522da50b6c7f 100644 --- a/tools/topology/topology2/include/components/webrtc-ns2.conf +++ b/tools/topology/topology2/include/components/webrtc-ns2.conf @@ -123,10 +123,13 @@ Class.Widget."webrtc-ns2" { ] # UUID: eacfacdc-2a87-c942-97e894c917a740db - uuid "dc:fa:cf:ea:87:2a:42:c9:97:e8:94:c9:17:a7:40:db" + uuid "dc:ac:cf:ea:87:2a:42:c9:97:e8:94:c9:17:a7:40:db" type "effect" no_pm "true" cpc 5000 + scheduler_domain "DP" + stack_bytes_requirement 16384 + heap_bytes_requirement 163840 num_input_pins 1 num_output_pins 1 } diff --git a/west.yml b/west.yml index 3f15d72ac3ff..dc2952b891fb 100644 --- a/west.yml +++ b/west.yml @@ -57,7 +57,7 @@ manifest: - name: rnnoise repo-path: rnnoise path: modules/audio/rnnoise - revision: 24dbb35115c6d27f9f14cc654bf0a32417f70bf9 + revision: 5bd733d7a950715774582dda49aeff0e8f71ccc2 # FFmpeg (libavcodec) source for the ffmpeg_dec module. Cross-built by # src/audio/ffmpeg_dec/ffmpeg.cmake, enabling only the Kconfig-selected