Skip to content
Open
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
36 changes: 30 additions & 6 deletions src/audio/aria/aria.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ const int32_t sof_aria_index_tab[] = {
static size_t get_required_memory(size_t chan_cnt, size_t smpl_group_cnt)
{
/* Current implementation is able to apply 1 ms transition */
/* internal circular buffer aligned to 8 bytes */
const size_t num_of_ms = 1;

return ALIGN_UP(num_of_ms * chan_cnt * smpl_group_cnt, 2) * sizeof(int32_t);
return num_of_ms * chan_cnt * smpl_group_cnt * sizeof(int32_t);
}

static void aria_set_gains(struct aria_data *cd)
Expand All @@ -66,12 +65,19 @@ static int aria_algo_init(struct aria_data *cd, void *buffer_desc,
{
cd->chan_cnt = chan_cnt;
cd->smpl_group_cnt = smpl_group_cnt;
/* ensures buffer size is aligned to 8 bytes */
cd->buff_size = ALIGN_UP(cd->chan_cnt * cd->smpl_group_cnt, 2);
cd->offset = (cd->chan_cnt * cd->smpl_group_cnt) & 1;
/* The ring holds exactly one chunk, a whole number of frames, so a
Comment thread
lgirdwood marked this conversation as resolved.
Resolved
* frame never straddles its end and the delay stays smpl_group_cnt
* frames. The sample count is odd only for an odd channel count,
* and the odd channel HiFi paths access the ring with 32-bit scalar
* loads and stores. The even channel paths load two samples at a
* time; there the ring is a multiple of 8 bytes and the pointers
* advance by whole frames, so they keep the alignment of the
* allocation. get_required_memory() allocates exactly this size.
*/
cd->buff_size = cd->chan_cnt * cd->smpl_group_cnt;
cd->att = att;
cd->data_addr = buffer_desc;
cd->data_ptr = cd->data_addr + cd->offset;
cd->data_ptr = cd->data_addr;
cd->data_end = cd->data_addr + cd->buff_size;
cd->buff_pos = 0;

Expand Down Expand Up @@ -177,6 +183,8 @@ static int aria_prepare(struct processing_module *mod,
int ret;
struct comp_dev *dev = mod->dev;
struct aria_data *cd = module_get_private_data(mod);
struct comp_buffer *src_buf, *snk_buf;
size_t frame_bytes;

comp_info(dev, "entry");

Expand All @@ -196,6 +204,22 @@ static int aria_prepare(struct processing_module *mod,
return -EINVAL;
}

/* The get_data paths bound each chunk by the distance to the sink
* buffer end in samples but write whole frames, and the HiFi5 path
* wraps both buffers with hardware circular addressing, so a buffer
* that is not a whole number of frames would be overrun at its end.
*/
frame_bytes = source_get_frame_bytes(sources[0]);
src_buf = comp_dev_get_first_data_producer(dev);
snk_buf = comp_dev_get_first_data_consumer(dev);
if (!src_buf || !snk_buf || !frame_bytes ||
audio_stream_get_size(&src_buf->stream) % frame_bytes ||
audio_stream_get_size(&snk_buf->stream) % frame_bytes) {
comp_err(dev, "source and sink buffers must hold whole %zu byte frames",
frame_bytes);
return -EINVAL;
}

if (dev->state == COMP_STATE_ACTIVE) {
comp_info(dev, "Component is in active state.");
return 0;
Expand Down
3 changes: 0 additions & 3 deletions src/audio/aria/aria.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ struct aria_data {
/* cyclic buffer start address of data */
int32_t *data_addr;

/* internal buffer offset helps to keep algorithmic delay constant */
size_t offset;

aria_get_data_func aria_get_data;
};

Expand Down
2 changes: 1 addition & 1 deletion src/audio/aria/aria_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static void aria_algo_get_data(struct processing_module *mod,
while (samples) {
m = cir_buf_samples_without_wrap_s32(out, sink->buf_end);
n = MIN(m, samples);
m = cir_buf_samples_without_wrap_s32(cd->data_ptr, cd->data_end);
m = cir_buf_samples_without_wrap_s32(in, cd->data_end);
n = MIN(m, n);
for (i = 0; i < n; i += ch_n) {
for (ch = 0; ch < ch_n; ch++) {
Comment on lines 70 to 75

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

While cd->data_end - cd->data_addr is now guaranteed to be a multiple of ch_n, sink->buf_end - sink->buf_start is not guaranteed to be a multiple of frame_bytes (ipc4_comp_connect() sizes inter-module buffers as MAX(ibs, obs) * 2 in raw bytes).

For ch_n = 3, 5, 6, 7, if a connected module specifies a power-of-2 ibs/obs (e.g. 1024 -> 2048 bytes = 512 int32_t samples, 512 % 3 == 2), m = cir_buf_samples_without_wrap_s32(out, sink->buf_end) near the end of the sink buffer is not divisible by ch_n (e.g. n = 2 or n = 5 for ch_n = 3). for (i = 0; i < n; i += ch_n) will then execute a full ch_n-sample inner iteration on the tail, writing up to ch_n - 1 samples past sink->buf_end while samples -= n only subtracts n (same loop bound in aria_hifi3.c:96, 156).

Either keep ch across outer-loop iterations (for (i = 0; i < n; i++) stepping gain += step every ch_n samples) or reject non-frame-aligned snk_size / src_size (snk_size % frame_bytes != 0, which aria_hifi5.c's hardware circular buffer AE_SETCEND1(sink->buf_end) also requires).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Went with rejecting it at prepare: aria_prepare() now checks that the source and sink buffers hold a whole number of frames and fails with -EINVAL otherwise. Handling a partial frame at the sink wrap would have meant restructuring all three get_data loops, and the HiFi5 circular addressing needs frame sized buffers anyway. It's its own commit, with a ztest case that connects a sink of 70 frames plus one sample and a source of 94 frames plus one sample and expects prepare to fail.

Expand Down
16 changes: 6 additions & 10 deletions src/audio/aria/aria_hifi3.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ static void aria_algo_get_data_odd_channel(struct processing_module *mod,
/* do linear approximation between points gain_begin and gain_end */
int32_t gain_end = cd->gains[sof_aria_index_tab[gain_state_add_3]];
size_t samples = frames * cd->chan_cnt;
ae_int32x2 *out = sink->ptr;
const ae_int32x2 *in = (const ae_int32x2 *)cd->data_ptr;
ae_valign inu = AE_ZALIGN64();
ae_valign outu = AE_ZALIGN64();
ae_int32 *out = sink->ptr;
const ae_int32 *in = (const ae_int32 *)cd->data_ptr;
ae_int32x2 in_sample, out_sample;
const int inc = sizeof(ae_int32);
ae_int32x2 gain;
Expand All @@ -90,22 +88,20 @@ static void aria_algo_get_data_odd_channel(struct processing_module *mod,
while (samples) {
m = cir_buf_samples_without_wrap_s32(out, sink->buf_end);
n = MIN(m, samples);
m = cir_buf_samples_without_wrap_s32(cd->data_ptr, cd->data_end);
m = cir_buf_samples_without_wrap_s32(in, cd->data_end);
n = MIN(m, n);
inu = AE_LA64_PP(in);
for (i = 0; i < n; i += ch_n) {
Comment on lines +91 to 93

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Bug on HiFi3/HiFi4 when in is 4-byte-only aligned (((uintptr_t)in & 7) == 4):

aria_algo_get_data_odd_channel() uses scalar 32-bit AE_L32_XP / AE_S32_L_XP in the inner loop (inu and outu are never used), so inu = AE_LA64_PP(in) at line 95 and AE_SA64POS_FP(outu, out) at line 108 are leftover from aria_algo_get_data_even_channel().

However, AE_LA64_PP(in) (ae_la64.pp) modifies in in place when in is not 8-byte aligned: for ((uintptr_t)in & 7) == 4, it primes inu from in & ~7 and increments in by +8 bytes (2 samples). With an odd-sample ring (chan_cnt * smpl_group_cnt odd, e.g. 3 * 47 = 141) or after any odd-frame period / sink wrap, in enters this loop at 4 mod 8. AE_LA64_PP(in) then advances in by 2 samples after m = cir_buf_samples_without_wrap_s32(in, cd->data_end) was computed, so the AE_L32_XP loop:

  1. skips 2 samples (rotating channels),
  2. reads 2 samples (8 bytes) past cd->data_end, and
  3. wraps in to cd->data_addr + 2 (off frame boundary).

Also note that typing in / out as const ae_int32 *in = cd->data_ptr; and ae_int32 *out = sink->ptr; (matching aria_hifi5.c:83-84) and removing inu, outu, AE_LA64_PP(in), and AE_SA64POS_FP(outu, out) avoids the (ae_int32 *)in / (ae_int32 *)out cast-as-lvalue extension on lines 99 & 104, which fails to compile under Xtensa LLVM Clang (cannot take the address of an rvalue of type 'ae_int32 *').

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

AE_LA64_PP takes the pointer by value, so it can't move in; the prime and the AE_SA64POS_FP flush were dead code left over from the even channel path. The casts on the AE_L32_XP and AE_S32_L_XP operands are a real problem for clang though, so the odd channel path now uses ae_int32 pointers like the HiFi5 one and drops the align registers. It's a separate commit since it's a cleanup with no behaviour change. I still can't build the HiFi3 file here.

/*process data one by one if ch_n is odd*/
for (ch = 0; ch < ch_n; ch++) {
AE_L32_XP(in_sample, (ae_int32 *)in, inc);
AE_L32_XP(in_sample, in, inc);
in_sample = AE_SRAI32(AE_SLAI32(in_sample, 8), 8);
out1 = AE_MUL32_HH(in_sample, gain);
out1 = AE_SRAA64(out1, shift_bits);
out_sample = AE_ROUND24X2F48SSYM(out1, out1);
AE_S32_L_XP(out_sample, (ae_int32 *)out, inc);
AE_S32_L_XP(out_sample, out, inc);
}
gain = AE_ADD32S(gain, step);
}
AE_SA64POS_FP(outu, out);
samples -= n;
in = cir_buf_wrap(in, cd->data_addr, cd->data_end);
out = cir_buf_wrap(out, sink->buf_start, sink->buf_end);
Expand Down Expand Up @@ -150,7 +146,7 @@ static void aria_algo_get_data_even_channel(struct processing_module *mod,
while (samples) {
m = cir_buf_samples_without_wrap_s32(out, sink->buf_end);
n = MIN(m, samples);
m = cir_buf_samples_without_wrap_s32(cd->data_ptr, cd->data_end);
m = cir_buf_samples_without_wrap_s32(in, cd->data_end);
n = MIN(m, n);
inu = AE_LA64_PP(in);
for (i = 0; i < n; i += ch_n) {
Expand Down
67 changes: 67 additions & 0 deletions test/ztest/unit/audio/aria/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# SPDX-License-Identifier: BSD-3-Clause

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(sof_unit_audio_aria)

set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../../../")
set(sof_top_dir ${SOF_ROOT})

include(${SOF_ROOT}/scripts/cmake/misc.cmake)
include(${SOF_ROOT}/scripts/cmake/uuid-registry.cmake)

target_include_directories(app PRIVATE
${SOF_ROOT}/zephyr/include
${SOF_ROOT}/src/include
${SOF_ROOT}/src/kernel/platform/posix/include
${SOF_ROOT}/src/platform/posix/include
${SOF_ROOT}/src/audio/aria
${SOF_ROOT}/tools/rimage/src/include
${PROJECT_BINARY_DIR}/include/generated
)

target_compile_definitions(app PRIVATE
-DCONFIG_ZEPHYR_POSIX=1
-DCONFIG_LIBRARY=1
-DUNIT_TEST=1
)

target_sources(app PRIVATE
aria_ztest.c
${SOF_ROOT}/test/ztest/unit/common/alloc.c
${SOF_ROOT}/test/ztest/unit/common/sof_context.c
${SOF_ROOT}/src/audio/aria/aria.c
${SOF_ROOT}/src/audio/aria/aria_generic.c
${SOF_ROOT}/src/audio/module_adapter/module_adapter.c
${SOF_ROOT}/src/audio/module_adapter/module_adapter_ipc4.c
${SOF_ROOT}/src/audio/module_adapter/module/generic.c
${SOF_ROOT}/src/audio/buffers/comp_buffer.c
${SOF_ROOT}/src/audio/buffers/audio_buffer.c
${SOF_ROOT}/src/audio/source_api_helper.c
${SOF_ROOT}/src/audio/sink_api_helper.c
${SOF_ROOT}/src/audio/sink_source_utils.c
${SOF_ROOT}/src/audio/audio_stream.c
${SOF_ROOT}/src/audio/component.c
${SOF_ROOT}/src/audio/data_blob.c
${SOF_ROOT}/src/module/audio/source_api.c
${SOF_ROOT}/src/module/audio/sink_api.c
${SOF_ROOT}/src/ipc/ipc4/helper.c
${SOF_ROOT}/src/ipc/ipc-common.c
${SOF_ROOT}/src/ipc/ipc-helper.c
${SOF_ROOT}/src/lib/objpool.c
${SOF_ROOT}/src/math/numbers.c
)

target_link_libraries(app PRIVATE
"-Wl,--wrap=rzalloc,--wrap=rfree,--wrap=sof_heap_alloc,--wrap=sof_heap_free,--wrap=sof_sys_user_heap_get"
)

# The standalone target compiles the full IPC4 helper translation unit while
# linking only its format helpers; other helper paths leave one local ID unused.
set_source_files_properties(
${SOF_ROOT}/src/ipc/ipc4/helper.c
PROPERTIES COMPILE_OPTIONS "-Wno-unused-variable"
)

sof_append_relative_path_definitions(app)
Loading
Loading