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
11 changes: 8 additions & 3 deletions src/audio/webrtc_aec/webrtc_aec-math.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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


Expand All @@ -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)));
}

Expand Down
53 changes: 30 additions & 23 deletions src/audio/webrtc_aec/webrtc_aec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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++)
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
4 changes: 3 additions & 1 deletion src/audio/webrtc_aec/webrtc_aec.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
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()
7 changes: 4 additions & 3 deletions src/audio/webrtc_agc/webrtc_agc-agc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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++) {
Expand All @@ -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++) {
Expand Down
25 changes: 22 additions & 3 deletions src/audio/webrtc_agc/webrtc_agc-stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading
Loading