From b053a6f9362ff291f9b31c472c20dd916f1f0bc4 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 13 Nov 2026 17:12:32 +0000 Subject: [PATCH 1/8] audio: webrtc: use non-PIC longcalls flags for external LLEXT libs Compile external WebRTC static libraries (AECm, NS, RNNoise, libfvad) with -fno-pic -fno-pie -mlongcalls -mtext-section-literals -DNDEBUG when building relocatable Xtensa LLEXTs. Without these flags, GCC emits R_XTENSA_PLT internal relocations that the Zephyr LLEXT loader cannot resolve against static archive symbols. Signed-off-by: Liam Girdwood --- src/audio/webrtc_aec/webrtc_aec.cmake | 12 +++++++++++- src/audio/webrtc_ns/webrtc_ns.cmake | 12 +++++++++++- src/audio/webrtc_ns2/webrtc_ns2.cmake | 12 +++++++++++- src/audio/webrtc_vad/webrtc_vad.cmake | 12 +++++++++++- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/audio/webrtc_aec/webrtc_aec.cmake b/src/audio/webrtc_aec/webrtc_aec.cmake index 13e56d89af22..b7648f891e16 100644 --- a/src/audio/webrtc_aec/webrtc_aec.cmake +++ b/src/audio/webrtc_aec/webrtc_aec.cmake @@ -33,6 +33,16 @@ set(WEBRTC_AECM_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/webrtc-aecm-install" 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 @@ -58,7 +68,7 @@ OBJ=${CMAKE_CURRENT_BINARY_DIR}/webrtc-aecm-obj rm -rf \"$OBJ\" \"$INST/lib/libwebrtc_aecm.a\" mkdir -p \"$OBJ\" \"$INST/lib\" \"$INST/include\" \"$INST/include/webrtc\" -CFLAGS=\"-O2 -fPIC -DNDEBUG -I${SOF_WEBRTC_APM_SRC_DIR} -I$SRC -I$SRC/common_audio/signal_processing/include -DWEBRTC_ARCH_32_BITS= -DWEBRTC_ARCH_LITTLE_ENDIAN= -Dmalloc=webrtc_malloc -Dcalloc=webrtc_calloc -Drealloc=webrtc_realloc -Dfree=webrtc_free\" +CFLAGS=\"-O2 ${_aecm_arch_cflags} -DNDEBUG -I${SOF_WEBRTC_APM_SRC_DIR} -I$SRC -I$SRC/common_audio/signal_processing/include -DWEBRTC_ARCH_32_BITS= -DWEBRTC_ARCH_LITTLE_ENDIAN= -Dmalloc=webrtc_malloc -Dcalloc=webrtc_calloc -Drealloc=webrtc_realloc -Dfree=webrtc_free\" # AEC backend core SRCS=\"${_aec_backend_srcs} diff --git a/src/audio/webrtc_ns/webrtc_ns.cmake b/src/audio/webrtc_ns/webrtc_ns.cmake index fb39b40e1793..771001fc8427 100644 --- a/src/audio/webrtc_ns/webrtc_ns.cmake +++ b/src/audio/webrtc_ns/webrtc_ns.cmake @@ -37,6 +37,16 @@ set(WEBRTC_NS_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/webrtc-ns-install" set(_ns_src "${SOF_WEBRTC_APM_SRC_DIR}/webrtc") +if("${ARCH}" STREQUAL "xtensa") + if(CONFIG_LLEXT_BUILD_PIC) + set(_ns_arch_cflags "-fPIC -mlongcalls -mtext-section-literals") + else() + set(_ns_arch_cflags "-fno-pic -fno-pie -mlongcalls -mtext-section-literals") + endif() +else() + set(_ns_arch_cflags "-fPIC") +endif() + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/webrtc-ns-build.sh" "#!/bin/sh set -e @@ -46,7 +56,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/webrtc_ns2.cmake b/src/audio/webrtc_ns2/webrtc_ns2.cmake index b9c78c55e6ca..d6bd7dda5790 100644 --- a/src/audio/webrtc_ns2/webrtc_ns2.cmake +++ b/src/audio/webrtc_ns2/webrtc_ns2.cmake @@ -34,6 +34,16 @@ set(WEBRTC_NS2_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/rnnoise-install" set(_rnn_src "${SOF_RNNOISE_SRC_DIR}/src") set(_rnn_inc "${SOF_RNNOISE_SRC_DIR}/include") +if("${ARCH}" STREQUAL "xtensa") + if(CONFIG_LLEXT_BUILD_PIC) + set(_rnn_arch_cflags "-fPIC -mlongcalls -mtext-section-literals") + else() + set(_rnn_arch_cflags "-fno-pic -fno-pie -mlongcalls -mtext-section-literals") + endif() +else() + set(_rnn_arch_cflags "-fPIC") +endif() + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/rnnoise-build.sh" "#!/bin/sh set -e @@ -44,7 +54,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_vad/webrtc_vad.cmake b/src/audio/webrtc_vad/webrtc_vad.cmake index d0124c7ded5f..49afd97c3cb4 100644 --- a/src/audio/webrtc_vad/webrtc_vad.cmake +++ b/src/audio/webrtc_vad/webrtc_vad.cmake @@ -39,6 +39,16 @@ set(LIBFVAD_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/libfvad-install" # 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. +if("${ARCH}" STREQUAL "xtensa") + if(CONFIG_LLEXT_BUILD_PIC) + set(_fvad_arch_cflags "-fPIC -mlongcalls -mtext-section-literals") + else() + set(_fvad_arch_cflags "-fno-pic -fno-pie -mlongcalls -mtext-section-literals") + endif() +else() + set(_fvad_arch_cflags "-fPIC") +endif() + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/fvad-build.sh" "#!/bin/sh set -e @@ -66,7 +76,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\" From 19f962748b756d398d70287da558731ea7fce30d Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 20 Nov 2026 18:50:48 +0000 Subject: [PATCH 2/8] audio: webrtc: handle Zephyr LL_EXTENSION_BUILD macro in module shims Zephyr sets LL_EXTENSION_BUILD rather than LLEXT_MODULE when compiling relocatable LLEXT sources; check both macros in the WebRTC shim files and in webrtc_vad-fvad.c's webrtc_vad_set_active_mod() declaration so active_vad_mod, malloc/free wrappers, and non-userspace BSS placement match the build mode. Also remove the unused include and k_panic() call from webrtc_aec-shims.c. Signed-off-by: Liam Girdwood --- src/audio/webrtc_ns/webrtc_ns-shims.c | 4 ++-- src/audio/webrtc_ns2/webrtc_ns2-shims.c | 6 +++--- src/audio/webrtc_vad/webrtc_vad-fvad.c | 2 +- src/audio/webrtc_vad/webrtc_vad-shims.c | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/audio/webrtc_ns/webrtc_ns-shims.c b/src/audio/webrtc_ns/webrtc_ns-shims.c index 4f5b91362079..ecbe56b7858e 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) diff --git a/src/audio/webrtc_ns2/webrtc_ns2-shims.c b/src/audio/webrtc_ns2/webrtc_ns2-shims.c index e6064d31e0e6..944c214c583a 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) @@ -285,4 +285,4 @@ double exp(double x) { return (double)expf((float)x); } -#endif /* LLEXT_MODULE */ +#endif /* LLEXT_MODULE || LL_EXTENSION_BUILD */ 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 */ From 21f701dc61a11aa8f801144b839c1f18c2dc62a2 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 13 Nov 2026 17:12:32 +0000 Subject: [PATCH 3/8] audio: webrtc_ns, webrtc_ns2: add weak soft-float math shims Provide __attribute__((weak)) soft-float logf, log10f, expf, cosf, sinf, and atanf shims compiled unconditionally so both relocatable LLEXT and monolithic builds (e.g. CAVS 2.5 where CONFIG_LIBRARY_MANAGER=n) link without an external libm or duplicate symbol collisions. Implement log2f and exp2f via range reduction into [sqrt(2)/2, sqrt(2)] and [-0.5, 0.5] so inputs across the full positive domain stay accurate and continuous. Signed-off-by: Liam Girdwood --- src/audio/webrtc_ns/webrtc_ns-shims.c | 214 ++++++++++++++++++++++-- src/audio/webrtc_ns2/webrtc_ns2-shims.c | 73 ++++---- 2 files changed, 241 insertions(+), 46 deletions(-) diff --git a/src/audio/webrtc_ns/webrtc_ns-shims.c b/src/audio/webrtc_ns/webrtc_ns-shims.c index ecbe56b7858e..c19045f8fbc5 100644 --- a/src/audio/webrtc_ns/webrtc_ns-shims.c +++ b/src/audio/webrtc_ns/webrtc_ns-shims.c @@ -124,42 +124,51 @@ void *memmove(void *dest, const void *src, size_t n) */ -float fabsf(float x) +__attribute__((weak)) float fabsf(float x) { return (x < 0.0f) ? -x : x; } -double fabs(double x) +__attribute__((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) +__attribute__((weak)) float expf(float x) { return ns_exp2f(x * 1.4426950408889634f); } -double exp(double x) +__attribute__((weak)) double exp(double x) { return (double)expf((float)x); } -double tanh(double x) +__attribute__((weak)) double tanh(double x) { if (x > 10.0) return 1.0; if (x < -10.0) return -1.0; @@ -167,10 +176,179 @@ double tanh(double x) return (e2 - 1.0) / (e2 + 1.0); } -float atanf(float x) +__attribute__((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; +} + +__attribute__((weak)) float logf(float x) +{ + return ns_log2f(x) * 0.6931471805599453f; +} + +__attribute__((weak)) double log(double x) +{ + return (double)logf((float)x); +} + +__attribute__((weak)) float sqrtf(float x) +{ + if (x <= 0.0f) + return 0.0f; + return __builtin_sqrtf(x); +} + +__attribute__((weak)) double sqrt(double x) +{ + return (double)sqrtf((float)x); +} + +__attribute__((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; +} + +__attribute__((weak)) float floorf(float x) +{ + return ns_floorf(x); +} + +__attribute__((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))); +} + +__attribute__((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; + } +} + +__attribute__((weak)) double sin(double x) +{ + return (double)sinf((float)x); +} + +__attribute__((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; + } +} + +__attribute__((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_ns2/webrtc_ns2-shims.c b/src/audio/webrtc_ns2/webrtc_ns2-shims.c index 944c214c583a..9fe0ad0a5558 100644 --- a/src/audio/webrtc_ns2/webrtc_ns2-shims.c +++ b/src/audio/webrtc_ns2/webrtc_ns2-shims.c @@ -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,45 @@ 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) +__attribute__((weak)) float log10f(float x) { return ns2_log2f(x) * 0.3010299956639812f; } -double log10(double x) +__attribute__((weak)) double log10(double x) { return (double)log10f((float)x); } -float floorf(float x) +__attribute__((weak)) float floorf(float x) { int i = (int)x; @@ -162,12 +177,12 @@ float floorf(float x) return (float)i; } -double floor(double x) +__attribute__((weak)) double floor(double x) { return (double)floorf((float)x); } -float ceilf(float x) +__attribute__((weak)) float ceilf(float x) { int i = (int)x; @@ -181,24 +196,24 @@ static inline float ns2_roundf(float x) return (x >= 0.0f) ? floorf(x + 0.5f) : ceilf(x - 0.5f); } -float fabsf(float x) +__attribute__((weak)) float fabsf(float x) { return (x < 0.0f) ? -x : x; } -double fabs(double x) +__attribute__((weak)) double fabs(double x) { return (x < 0.0) ? -x : x; } -float sqrtf(float x) +__attribute__((weak)) float sqrtf(float x) { if (x <= 0.0f) return 0.0f; return __builtin_sqrtf(x); } -double sqrt(double x) +__attribute__((weak)) double sqrt(double x) { return (double)sqrtf((float)x); } @@ -211,7 +226,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) +__attribute__((weak)) float sinf(float x) { const float inv_half_pi = 0.636619772367581343f; const float half_pi_hi = 1.5707963267948966f; @@ -229,12 +244,12 @@ float sinf(float x) } } -double sin(double x) +__attribute__((weak)) double sin(double x) { return (double)sinf((float)x); } -float cosf(float x) +__attribute__((weak)) float cosf(float x) { const float inv_half_pi = 0.636619772367581343f; const float half_pi_hi = 1.5707963267948966f; @@ -252,37 +267,39 @@ float cosf(float x) } } -double cos(double x) +__attribute__((weak)) double cos(double x) { return (double)cosf((float)x); } -float exp2f(float x) +__attribute__((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) +__attribute__((weak)) float expf(float x) { return exp2f(x * 1.4426950408889634f); } -double exp(double x) +__attribute__((weak)) double exp(double x) { return (double)expf((float)x); } -#endif /* LLEXT_MODULE || LL_EXTENSION_BUILD */ From 7c663e5879d92a0e1ad9c080d1845ad9b53bb08d Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sun, 15 Nov 2026 15:29:28 +0000 Subject: [PATCH 4/8] lib: notifier: expose notifier_event as validated Zephyr syscall When CONFIG_USERSPACE is enabled, webrtc_vad runs in a user thread and invokes notifier_event() on VAD state changes. Instead of exporting raw notifier_event() directly to LLEXT/userspace (where host_notify state is not partitioned for user access), expose notifier_event() via a Zephyr __syscall wrapper. In z_vrfy_notifier_event(), restrict userspace delivery to NOTIFIER_ID_VAD on the local core only, verify the exact int payload size, and copy the VAD state into kernel stack storage via k_usermode_from_copy() before invoking z_impl_notifier_event(). Signed-off-by: Liam Girdwood --- src/include/sof/lib/notifier.h | 13 ++++++++++-- src/lib/notifier.c | 5 +++-- zephyr/CMakeLists.txt | 3 +++ zephyr/syscall/notifier.c | 38 ++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 zephyr/syscall/notifier.c diff --git a/src/include/sof/lib/notifier.h b/src/include/sof/lib/notifier.h index e5413a512cb4..cf93ab359d55 100644 --- a/src/include/sof/lib/notifier.h +++ b/src/include/sof/lib/notifier.h @@ -88,8 +88,17 @@ void notifier_notify_remote(void); /* data_size is required to manage cache coherency for notifications * across cores. */ -void notifier_event(const void *caller_id, enum notify_id event_type, uint32_t core_mask, - void *caller_data, uint32_t data_size); +#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#include + +__syscall void notifier_event(const void *caller_id, enum notify_id event_type, + uint32_t core_mask, void *caller_data, uint32_t data_size); +#include +#else +void z_impl_notifier_event(const void *caller_id, enum notify_id event_type, + uint32_t core_mask, void *caller_data, uint32_t data_size); +#define notifier_event z_impl_notifier_event +#endif void init_system_notify(struct sof *sof); diff --git a/src/lib/notifier.c b/src/lib/notifier.c index cbc9ff5cd7f8..acff1bf46a09 100644 --- a/src/lib/notifier.c +++ b/src/lib/notifier.c @@ -145,8 +145,8 @@ void notifier_notify_remote(void) } -void notifier_event(const void *caller, enum notify_id type, uint32_t core_mask, - void *data, uint32_t data_size) +void z_impl_notifier_event(const void *caller, enum notify_id type, uint32_t core_mask, + void *data, uint32_t data_size) { struct notify_data *notify_data; struct idc_msg notify_msg = { IDC_MSG_NOTIFY, IDC_MSG_NOTIFY_EXT }; @@ -177,6 +177,7 @@ void notifier_event(const void *caller, enum notify_id type, uint32_t core_mask, } } } +EXPORT_SYMBOL(z_impl_notifier_event); __cold void init_system_notify(struct sof *sof) { diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 23ebaa87f6f5..d55636c7af4b 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -643,6 +643,9 @@ zephyr_library_sources_ifdef(CONFIG_USERSPACE syscall/dai.c) zephyr_syscall_header(${SOF_SRC_PATH}/include/user/debug_stream_slot.h) +zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/lib/notifier.h) +zephyr_library_sources_ifdef(CONFIG_USERSPACE syscall/notifier.c) + zephyr_syscall_header(${SOF_SRC_PATH}/include/ipc4/notification.h) zephyr_library_link_libraries(SOF) diff --git a/zephyr/syscall/notifier.c b/zephyr/syscall/notifier.c new file mode 100644 index 000000000000..d071dad4c73e --- /dev/null +++ b/zephyr/syscall/notifier.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. + +#include +#include +#include +#include + +/** + * \brief Userspace verification wrapper for notifier_event(). + * + * Validates the notification type and verifies read access to the caller + * payload buffer before delegating dispatch to supervisor context so user + * threads never dereference kernel-only host_notify state. + */ +static inline void z_vrfy_notifier_event(const void *caller_id, + enum notify_id event_type, + uint32_t core_mask, + void *caller_data, + uint32_t data_size) +{ + int vad_state; + + /* + * Restrict userspace notifications to explicit, fixed-payload events + * dispatched synchronously on the local core so privileged kernel + * events and cross-core pointers to user memory cannot escape. + */ + K_OOPS(K_SYSCALL_VERIFY(event_type == NOTIFIER_ID_VAD)); + K_OOPS(K_SYSCALL_VERIFY((core_mask & ~NOTIFIER_TARGET_CORE_LOCAL) == 0)); + K_OOPS(K_SYSCALL_VERIFY(caller_data != NULL && data_size == sizeof(vad_state))); + K_OOPS(k_usermode_from_copy(&vad_state, caller_data, sizeof(vad_state))); + + z_impl_notifier_event(caller_id, event_type, core_mask, + &vad_state, sizeof(vad_state)); +} +#include From 71654d71493e74cace0c260e2838c582b0bc97c0 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sun, 22 Nov 2026 19:01:33 +0000 Subject: [PATCH 5/8] audio: webrtc: build all WebRTC modules as standalone LLEXTs Drop 'LIB openmodules' from all six WebRTC module LLEXT builds (webrtc_aec, webrtc_agc, webrtc_hpf, webrtc_ns, webrtc_ns2, webrtc_vad) so each builds and signs as an individual .llext with its .bin symlink instead of being bundled inside sof--openmodules.ri. Each standalone module keeps sequential module_size VMA advancement so multiple WebRTC stages (e.g. HPF -> AEC -> NS/NS2 -> AGC -> VAD) can load and run concurrently in the same capture pipeline (total 0xbc000 on PTL relocatable). Signed-off-by: Liam Girdwood --- src/audio/webrtc_aec/llext/CMakeLists.txt | 2 -- src/audio/webrtc_agc/llext/CMakeLists.txt | 2 -- src/audio/webrtc_hpf/llext/CMakeLists.txt | 2 -- src/audio/webrtc_ns/llext/CMakeLists.txt | 2 -- src/audio/webrtc_ns2/llext/CMakeLists.txt | 2 -- src/audio/webrtc_vad/llext/CMakeLists.txt | 2 -- 6 files changed, 12 deletions(-) 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_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_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_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_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_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. From e21d7b7029b8e3b12e416204a735b385c2ea2a88 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Mon, 23 Nov 2026 23:42:43 +0000 Subject: [PATCH 6/8] audio: webrtc: support S16_LE/S32_LE multi-format pipelines and fix LLEXT shims Support both 16-bit and 32-bit PCM formats cleanly in webrtc_{aec,agc,hpf,ns,vad}, make LLEXT soft-float math shims non-weak with safe Newton sqrtf in webrtc_ns-shims.c and webrtc_aec-math.c (avoiding __builtin_sqrtf recursion), and preserve multi-format routing. Signed-off-by: Liam Girdwood --- src/audio/webrtc_aec/webrtc_aec-math.c | 11 ++- src/audio/webrtc_aec/webrtc_aec.c | 53 ++++++----- src/audio/webrtc_aec/webrtc_aec.h | 4 +- src/audio/webrtc_agc/webrtc_agc-agc.c | 7 +- src/audio/webrtc_agc/webrtc_agc-stub.c | 25 ++++- src/audio/webrtc_agc/webrtc_agc.c | 111 +++++++++++++++++----- src/audio/webrtc_agc/webrtc_agc.h | 2 +- src/audio/webrtc_hpf/webrtc_hpf-filter.c | 106 +++++++++------------ src/audio/webrtc_hpf/webrtc_hpf-stub.c | 25 ++++- src/audio/webrtc_hpf/webrtc_hpf.c | 114 ++++++++++++++++++----- src/audio/webrtc_hpf/webrtc_hpf.h | 16 ++-- src/audio/webrtc_ns/webrtc_ns-shims.c | 56 +++++++---- src/audio/webrtc_ns/webrtc_ns.c | 67 +++++++------ src/audio/webrtc_vad/webrtc_vad.c | 96 +++++++++++++------ src/audio/webrtc_vad/webrtc_vad.h | 5 +- 15 files changed, 473 insertions(+), 225 deletions(-) diff --git a/src/audio/webrtc_aec/webrtc_aec-math.c b/src/audio/webrtc_aec/webrtc_aec-math.c index 0d7e61f8d1ae..746447ef0c55 100644 --- a/src/audio/webrtc_aec/webrtc_aec-math.c +++ b/src/audio/webrtc_aec/webrtc_aec-math.c @@ -61,9 +61,8 @@ float exp2f(float x) return p * aec_ldexp2(n); } -#ifdef LL_EXTENSION_BUILD /* --- sqrtf: not exported by the base firmware, bit estimate + 3 Newton steps --- */ -float sqrtf(float x) +static inline float aec_sqrtf(float x) { union { float f; uint32_t i; } u = { .f = x }; float y; @@ -78,6 +77,12 @@ float sqrtf(float x) y = 0.5f * (y + x / y); return y; } + +#ifdef LL_EXTENSION_BUILD +float sqrtf(float x) +{ + return aec_sqrtf(x); +} #endif @@ -95,7 +100,7 @@ float powf(float x, float y) if (y == 2.0f) return x * x; if (y == 0.5f) - return __builtin_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 7b27f44f1746..6201588e8d04 100644 --- a/src/audio/webrtc_aec/webrtc_aec.c +++ b/src/audio/webrtc_aec/webrtc_aec.c @@ -216,7 +216,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(); @@ -289,14 +289,31 @@ __cold static int webrtc_aec_prepare(struct processing_module *mod, return -EINVAL; } +#ifdef CONFIG_IPC_MAJOR_4 + /* Apply reference format override from topology pin descriptor. */ + if (cd->ref_src >= 0) { + 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]); + } +#endif + + 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; @@ -308,21 +325,11 @@ __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 ch=%d frame=%d %s resamp=%d", + comp_info(dev, "webrtc_aec: mic_src=%d ref_src=%d rate=%d/%d ch=%d frame=%d %s/%s->%s resamp=%d", cd->mic_src, cd->ref_src, cd->rate, cd->proc_rate, cd->channels, - cd->frame_samples, cd->is_s32 ? "S32" : "S16", cd->needs_resample); - -#ifdef CONFIG_IPC_MAJOR_4 - /* Apply reference format override from topology pin descriptor. */ - if (cd->ref_src >= 0) { - 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); - } -#endif + cd->frame_samples, cd->mic_is_s32 ? "S32" : "S16", + cd->ref_is_s32 ? "S32" : "S16", cd->out_is_s32 ? "S32" : "S16", + cd->needs_resample); if (cd->backend->configure) { ret = cd->backend->configure(mod, cd->proc_rate, @@ -365,13 +372,13 @@ static int webrtc_aec_process(struct processing_module *mod, return 0; /* Convert mic input to per-channel S16 */ - src_to_s16(mic, n, cd->mic_buf, cd->buffered_in_frames, cd->channels, cd->is_s32); + src_to_s16(mic, n, cd->mic_buf, cd->buffered_in_frames, cd->channels, cd->mic_is_s32); /* Convert ref input to per-channel S16; if starved/silent, fill with silence */ if (fref >= n) { - src_to_s16(ref, n, cd->ref_buf, cd->buffered_in_frames, cd->channels, cd->is_s32); + src_to_s16(ref, n, cd->ref_buf, cd->buffered_in_frames, cd->channels, cd->ref_is_s32); } else if (fref > 0) { - src_to_s16(ref, fref, cd->ref_buf, cd->buffered_in_frames, cd->channels, cd->is_s32); + src_to_s16(ref, fref, cd->ref_buf, cd->buffered_in_frames, cd->channels, cd->ref_is_s32); for (c = 0; c < cd->channels; c++) memset(&cd->ref_buf[c][cd->buffered_in_frames + fref], 0, (size_t)(n - fref) * sizeof(int16_t)); @@ -529,7 +536,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->channels, cd->is_s32); + s16_to_sink(out, cd->out_fifo, drain, cd->channels, cd->out_is_s32); cd->buffered_out_frames -= drain; if (cd->buffered_out_frames > 0) { for (c = 0; c < cd->channels; c++) diff --git a/src/audio/webrtc_aec/webrtc_aec.h b/src/audio/webrtc_aec/webrtc_aec.h index cc79fea7239c..e4a38ccf8d3c 100644 --- a/src/audio/webrtc_aec/webrtc_aec.h +++ b/src/audio/webrtc_aec/webrtc_aec.h @@ -133,7 +133,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 */ bool needs_resample; /* true when rate == 48000 and proc_rate == 16000 */ /* Processing frame size at proc_rate. */ 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/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/webrtc_ns-shims.c b/src/audio/webrtc_ns/webrtc_ns-shims.c index c19045f8fbc5..a12f54ede3e8 100644 --- a/src/audio/webrtc_ns/webrtc_ns-shims.c +++ b/src/audio/webrtc_ns/webrtc_ns-shims.c @@ -124,12 +124,23 @@ void *memmove(void *dest, const void *src, size_t n) */ -__attribute__((weak)) float fabsf(float x) +/* + * Zephyr llext_link_plt() 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; } -__attribute__((weak)) double fabs(double x) +NS_SHIM_WEAK double fabs(double x) { return (x < 0.0) ? -x : x; } @@ -158,17 +169,17 @@ static inline float ns_exp2f(float x) return p * u.f; } -__attribute__((weak)) float expf(float x) +NS_SHIM_WEAK float expf(float x) { return ns_exp2f(x * 1.4426950408889634f); } -__attribute__((weak)) double exp(double x) +NS_SHIM_WEAK double exp(double x) { return (double)expf((float)x); } -__attribute__((weak)) 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; @@ -176,7 +187,7 @@ __attribute__((weak)) double tanh(double x) return (e2 - 1.0) / (e2 + 1.0); } -__attribute__((weak)) float atanf(float x) +NS_SHIM_WEAK float atanf(float x) { float ax = fabsf(x); float base = 0.0f; @@ -229,29 +240,38 @@ static float ns_log2f(float x) return (float)e + (2.8853900817779268f * r) * poly; } -__attribute__((weak)) float logf(float x) +NS_SHIM_WEAK float logf(float x) { return ns_log2f(x) * 0.6931471805599453f; } -__attribute__((weak)) double log(double x) +NS_SHIM_WEAK double log(double x) { return (double)logf((float)x); } -__attribute__((weak)) float sqrtf(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; - 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; } -__attribute__((weak)) double sqrt(double x) +NS_SHIM_WEAK double sqrt(double x) { return (double)sqrtf((float)x); } -__attribute__((weak)) float powf(float x, float y) +NS_SHIM_WEAK float powf(float x, float y) { if (x <= 0.0f) return (x == 0.0f && y > 0.0f) ? 0.0f : 1.0f; @@ -275,12 +295,12 @@ static inline float ns_floorf(float x) return (float)i; } -__attribute__((weak)) float floorf(float x) +NS_SHIM_WEAK float floorf(float x) { return ns_floorf(x); } -__attribute__((weak)) double floor(double x) +NS_SHIM_WEAK double floor(double x) { return (double)ns_floorf((float)x); } @@ -307,7 +327,7 @@ static inline void ns_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))); } -__attribute__((weak)) float sinf(float x) +NS_SHIM_WEAK float sinf(float x) { const float inv_half_pi = 0.636619772367581343f; const float half_pi_hi = 1.5707963267948966f; @@ -325,12 +345,12 @@ __attribute__((weak)) float sinf(float x) } } -__attribute__((weak)) double sin(double x) +NS_SHIM_WEAK double sin(double x) { return (double)sinf((float)x); } -__attribute__((weak)) float cosf(float x) +NS_SHIM_WEAK float cosf(float x) { const float inv_half_pi = 0.636619772367581343f; const float half_pi_hi = 1.5707963267948966f; @@ -348,7 +368,7 @@ __attribute__((weak)) float cosf(float x) } } -__attribute__((weak)) double cos(double x) +NS_SHIM_WEAK double cos(double x) { 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_vad/webrtc_vad.c b/src/audio/webrtc_vad/webrtc_vad.c index 972ac594a9bc..caf7e3258e32 100644 --- a/src/audio/webrtc_vad/webrtc_vad.c +++ b/src/audio/webrtc_vad/webrtc_vad.c @@ -86,13 +86,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 +121,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 +157,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 +192,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 */ @@ -244,11 +264,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 +279,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 +299,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.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; From 5580c2cd9c5e4b2e0c79a540ec551e3f476567b0 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Mon, 23 Nov 2026 23:42:44 +0000 Subject: [PATCH 7/8] audio: webrtc_ns2: fix LLEXT shims, OOM handling, and DP topology config Fix LLEXT weak symbol / sqrtf math shims, add RNNoise scratch pre-init and OOM NULL checks, switch webrtc_ns2 to DP domain with required stack/heap sizes and updated topology UUID. Signed-off-by: Liam Girdwood --- src/audio/webrtc_ns2/webrtc_ns2-rnn.c | 31 ++++++-- src/audio/webrtc_ns2/webrtc_ns2-shims.c | 54 +++++++++----- src/audio/webrtc_ns2/webrtc_ns2.c | 72 ++++++++++--------- src/audio/webrtc_ns2/webrtc_ns2.h | 3 +- src/audio/webrtc_ns2/webrtc_ns2.toml | 2 +- .../include/components/webrtc-ns2.conf | 5 +- 6 files changed, 110 insertions(+), 57 deletions(-) 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 9fe0ad0a5558..6afb8416503e 100644 --- a/src/audio/webrtc_ns2/webrtc_ns2-shims.c +++ b/src/audio/webrtc_ns2/webrtc_ns2-shims.c @@ -158,17 +158,28 @@ static float ns2_log2f(float x) return (float)e + (2.8853900817779268f * r) * poly; } -__attribute__((weak)) float log10f(float x) +/* + * Zephyr llext_link_plt() 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; } -__attribute__((weak)) double log10(double x) +NS2_SHIM_WEAK double log10(double x) { return (double)log10f((float)x); } -__attribute__((weak)) float floorf(float x) +NS2_SHIM_WEAK float floorf(float x) { int i = (int)x; @@ -177,12 +188,12 @@ __attribute__((weak)) float floorf(float x) return (float)i; } -__attribute__((weak)) double floor(double x) +NS2_SHIM_WEAK double floor(double x) { return (double)floorf((float)x); } -__attribute__((weak)) float ceilf(float x) +NS2_SHIM_WEAK float ceilf(float x) { int i = (int)x; @@ -196,24 +207,33 @@ static inline float ns2_roundf(float x) return (x >= 0.0f) ? floorf(x + 0.5f) : ceilf(x - 0.5f); } -__attribute__((weak)) float fabsf(float x) +NS2_SHIM_WEAK float fabsf(float x) { return (x < 0.0f) ? -x : x; } -__attribute__((weak)) double fabs(double x) +NS2_SHIM_WEAK double fabs(double x) { return (x < 0.0) ? -x : x; } -__attribute__((weak)) 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; } -__attribute__((weak)) double sqrt(double x) +NS2_SHIM_WEAK double sqrt(double x) { return (double)sqrtf((float)x); } @@ -226,7 +246,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))); } -__attribute__((weak)) 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; @@ -244,12 +264,12 @@ __attribute__((weak)) float sinf(float x) } } -__attribute__((weak)) double sin(double x) +NS2_SHIM_WEAK double sin(double x) { return (double)sinf((float)x); } -__attribute__((weak)) 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; @@ -267,12 +287,12 @@ __attribute__((weak)) float cosf(float x) } } -__attribute__((weak)) double cos(double x) +NS2_SHIM_WEAK double cos(double x) { return (double)cosf((float)x); } -__attribute__((weak)) float exp2f(float x) +NS2_SHIM_WEAK float exp2f(float x) { int n; float f, p; @@ -294,12 +314,12 @@ __attribute__((weak)) float exp2f(float x) return p * ns2_ldexp2(n); } -__attribute__((weak)) float expf(float x) +NS2_SHIM_WEAK float expf(float x) { return exp2f(x * 1.4426950408889634f); } -__attribute__((weak)) double exp(double x) +NS2_SHIM_WEAK double exp(double x) { return (double)expf((float)x); } diff --git a/src/audio/webrtc_ns2/webrtc_ns2.c b/src/audio/webrtc_ns2/webrtc_ns2.c index d47987325383..667bb53e770f 100644 --- a/src/audio/webrtc_ns2/webrtc_ns2.c +++ b/src/audio/webrtc_ns2/webrtc_ns2.c @@ -42,7 +42,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 +64,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 +138,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 +148,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 +171,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 +209,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 +229,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 +252,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 */ @@ -305,13 +313,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 +334,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.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/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 } From 473f6f9f46a475ab1e8e4030932fc7b844de42ba Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Tue, 24 Nov 2026 13:50:17 +0000 Subject: [PATCH 8/8] west.yml: bump rnnoise revision for scratch pre-init Update rnnoise from 24dbb35115c6 to 5bd733d7a950 to bring in rnnoise scratch pre-initialization and OOM allocation guards required by webrtc_ns2: 5bd733d rnnoise: add scratch pre-init and OOM allocation guards Signed-off-by: Liam Girdwood --- west.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/west.yml b/west.yml index 6a89bbecf3cd..ef8653c75f26 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