Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/audio/webrtc_aec/llext/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion src/audio/webrtc_aec/webrtc_aec.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand Down
2 changes: 0 additions & 2 deletions src/audio/webrtc_agc/llext/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
2 changes: 0 additions & 2 deletions src/audio/webrtc_hpf/llext/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 0 additions & 2 deletions src/audio/webrtc_ns/llext/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
218 changes: 198 additions & 20 deletions src/audio/webrtc_ns/webrtc_ns-shims.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <string.h>
#include <sof/audio/module_adapter/module/generic.h>
#include <rtos/alloc.h>
#if !defined(LLEXT_MODULE)
#if !defined(LLEXT_MODULE) && !defined(LL_EXTENSION_BUILD)
#include <rtos/userspace_helper.h>
static APP_SYSUSER_BSS struct processing_module *active_ns_mod;
#else
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -124,53 +124,231 @@ 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;
double e2 = exp(2.0 * 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);
}
12 changes: 11 additions & 1 deletion src/audio/webrtc_ns/webrtc_ns.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/audio/webrtc_ns2/llext/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Loading
Loading