diff --git a/src/audio/aria/aria.c b/src/audio/aria/aria.c index c2e18597ce13..22af0adbca27 100644 --- a/src/audio/aria/aria.c +++ b/src/audio/aria/aria.c @@ -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) @@ -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 + * 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; @@ -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"); @@ -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; diff --git a/src/audio/aria/aria.h b/src/audio/aria/aria.h index cc29ea28af3b..a86d8f8c0655 100644 --- a/src/audio/aria/aria.h +++ b/src/audio/aria/aria.h @@ -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; }; diff --git a/src/audio/aria/aria_generic.c b/src/audio/aria/aria_generic.c index c277726dbfe5..a56814884ac1 100644 --- a/src/audio/aria/aria_generic.c +++ b/src/audio/aria/aria_generic.c @@ -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++) { diff --git a/src/audio/aria/aria_hifi3.c b/src/audio/aria/aria_hifi3.c index 43e337d33fe6..e02f30c6bb1f 100644 --- a/src/audio/aria/aria_hifi3.c +++ b/src/audio/aria/aria_hifi3.c @@ -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; @@ -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) { /*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); @@ -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) { diff --git a/test/ztest/unit/audio/aria/CMakeLists.txt b/test/ztest/unit/audio/aria/CMakeLists.txt new file mode 100644 index 000000000000..a781f61cbc1a --- /dev/null +++ b/test/ztest/unit/audio/aria/CMakeLists.txt @@ -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) diff --git a/test/ztest/unit/audio/aria/aria_ztest.c b/test/ztest/unit/audio/aria/aria_ztest.c new file mode 100644 index 000000000000..d029771313a0 --- /dev/null +++ b/test/ztest/unit/audio/aria/aria_ztest.c @@ -0,0 +1,315 @@ +// SPDX-License-Identifier: BSD-3-Clause + +/** + * @file aria_ztest.c + * @brief Aria internal ring buffer tests. + * + * Aria keeps the last smpl_group_cnt frames in an internal ring and outputs + * them delayed by that amount, scaled by its gain. These tests run the real + * module through the module adapter with buffer sizes and short periods that + * make the ring wrap at a different point than the sink buffer, and check the + * output frame by frame. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "aria.h" + +#define TEST_RATE 48000 +#define TEST_PERIODS 40 +/* aria ramps its gain across the gain states, compare once it has settled */ +#define TEST_SETTLED (2 * ARIA_MAX_GAIN_STATES) + +struct aria_fixture { + struct comp_dev *dev; + struct comp_buffer *source; + struct comp_buffer *sink; + struct sof_source *sources[1]; + struct sof_sink *sinks[1]; +}; + +/** + * @brief Test sample for a frame and channel, small enough to keep full gain. + * @param frame Input frame index. + * @param ch Channel index. + * @return S24 sample value. + */ +static int32_t test_sample(size_t frame, unsigned int ch) +{ + return (int32_t)(((frame & 0xfff) << 4) | ch); +} + +/** + * @brief Allocate a buffer and connect it to the aria component. + * @param dev Aria component. + * @param source True to connect as the aria source, false for the sink. + * @param size Buffer size in bytes. + * @param channels Channels count. + * @return Connected buffer. + */ +static struct comp_buffer *create_test_buffer(struct comp_dev *dev, bool source, size_t size, + unsigned int channels) +{ + struct sof_ipc_buffer desc = { + .size = size, + }; + struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); + + zassert_not_null(buffer, "Failed to allocate test buffer"); + memset(audio_stream_get_addr(&buffer->stream), 0, size); + audio_stream_set_frm_fmt(&buffer->stream, SOF_IPC_FRAME_S32_LE); + audio_stream_set_valid_fmt(&buffer->stream, SOF_IPC_FRAME_S24_4LE); + audio_stream_set_rate(&buffer->stream, TEST_RATE); + audio_stream_set_channels(&buffer->stream, channels); + + if (source) { + buffer->source = calloc(1, sizeof(*buffer->source)); + zassert_not_null(buffer->source); + buffer->source->state = COMP_STATE_PREPARE; + list_item_append(&buffer->sink_list, &dev->bsource_list); + } else { + buffer->sink = calloc(1, sizeof(*buffer->sink)); + zassert_not_null(buffer->sink); + buffer->sink->state = COMP_STATE_PREPARE; + list_item_append(&buffer->source_list, &dev->bsink_list); + } + + return buffer; +} + +/** + * @brief Create an aria instance from an IPC4 init payload and connect its buffers. + * @param channels Channels count in the base config. + * @param ibs Input buffer size in bytes, sets the 1 ms chunk. + * @param source_bytes Source buffer size in bytes. + * @param sink_bytes Sink buffer size in bytes. + * @param attenuation Aria attenuation parameter. + * @return Fixture, not yet prepared. + */ +static struct aria_fixture *create_fixture(unsigned int channels, uint32_t ibs, + size_t source_bytes, size_t sink_bytes, + uint32_t attenuation) +{ + const struct sof_uuid uuid = SOF_REG_UUID(aria); + const struct comp_driver *driver = comp_driver_find(&uuid, SOF_COMP_MODULE_ADAPTER); + struct ipc4_aria_module_cfg cfg = { + .base_cfg = + { + .ibs = ibs, + .obs = ibs, + .audio_fmt = + { + .sampling_frequency = TEST_RATE, + .depth = IPC4_DEPTH_32BIT, + .ch_map = 0x76543210, + .ch_cfg = IPC4_CHANNEL_CONFIG_INVALID, + .interleaving_style = IPC4_CHANNELS_INTERLEAVED, + .channels_count = channels, + .valid_bit_depth = IPC4_DEPTH_24BIT, + .s_type = IPC4_TYPE_SIGNED_INTEGER, + }, + }, + .attenuation = attenuation, + }; + struct ipc_config_process spec = { + .size = sizeof(cfg), + .data = (const uint8_t *)&cfg, + }; + struct comp_ipc_config ipc_cfg = { + .id = 1, + .proc_domain = COMP_PROCESSING_DOMAIN_LL, + .type = SOF_COMP_MODULE_ADAPTER, + .ipc_config_size = spec.size, + }; + struct aria_fixture *fixture; + + zassert_not_null(driver, "Aria driver not registered"); + fixture = calloc(1, sizeof(*fixture)); + zassert_not_null(fixture); + fixture->dev = driver->ops.create(driver, &ipc_cfg, &spec); + zassert_not_null(fixture->dev, "Failed to create aria"); + + fixture->source = create_test_buffer(fixture->dev, true, source_bytes, channels); + fixture->sink = create_test_buffer(fixture->dev, false, sink_bytes, channels); + fixture->sources[0] = audio_buffer_get_source(&fixture->source->audio_buffer); + fixture->sinks[0] = audio_buffer_get_sink(&fixture->sink->audio_buffer); + + return fixture; +} + +/** + * @brief Prepare the aria instance of a fixture with its connected buffers. + * @param fixture Fixture to prepare. + * @return Status from the aria prepare handler. + */ +static int prepare_fixture(struct aria_fixture *fixture) +{ + const struct module_interface *ops = fixture->dev->drv->adapter_ops; + + return ops->prepare(comp_mod(fixture->dev), fixture->sources, 1, fixture->sinks, 1); +} + +/** + * @brief Free all resources owned by an aria fixture. + * @param fixture Fixture to free. + */ +static void destroy_fixture(struct aria_fixture *fixture) +{ + free(fixture->source->source); + buffer_free(fixture->source); + free(fixture->sink->sink); + buffer_free(fixture->sink); + fixture->dev->drv->ops.free(fixture->dev); + free(fixture); +} + +/** + * @brief Run aria period by period and check the delayed, scaled output. + * @param channels Channels count. + * @param chunk_frames Frames per 1 ms chunk, ibs in frames. + * @param source_frames Source buffer size in frames. + * @param sink_frames Sink buffer size in frames. + * @param first_frames Frames available in the first period. + * @param attenuation Aria attenuation parameter. + */ +static void run_aria(unsigned int channels, size_t chunk_frames, size_t source_frames, + size_t sink_frames, size_t first_frames, uint32_t attenuation) +{ + const size_t frame_bytes = channels * sizeof(int32_t); + struct aria_fixture *fixture = create_fixture(channels, chunk_frames * frame_bytes, + source_frames * frame_bytes, + sink_frames * frame_bytes, attenuation); + struct processing_module *mod = comp_mod(fixture->dev); + const struct module_interface *ops = fixture->dev->drv->adapter_ops; + struct audio_stream *source = &fixture->source->stream; + struct audio_stream *sink = &fixture->sink->stream; + size_t in_frame = 0; + size_t out_frame = 0; + + zassert_ok(prepare_fixture(fixture)); + + for (int period = 0; period < TEST_PERIODS; period++) { + size_t frames = period ? chunk_frames : first_frames; + int32_t *w = audio_stream_get_wptr(source); + int32_t *r; + size_t avail; + + for (size_t f = 0; f < frames; f++) { + for (unsigned int ch = 0; ch < channels; ch++) { + *w = test_sample(in_frame + f, ch); + w = audio_stream_wrap(source, w + 1); + } + } + audio_stream_produce(source, frames * frame_bytes); + in_frame += frames; + + zassert_ok(ops->process(mod, fixture->sources, 1, fixture->sinks, 1)); + + avail = audio_stream_get_avail_frames(sink); + zassert_equal(avail, frames, "period %d: %zu frames out, %zu in", period, avail, + frames); + + r = audio_stream_get_rptr(sink); + for (size_t f = 0; f < avail; f++, out_frame++) { + for (unsigned int ch = 0; ch < channels; ch++) { + int32_t expected = test_sample(out_frame - chunk_frames, ch) + << attenuation; + + if (period >= TEST_SETTLED) { + zassert_equal(*r, expected, + "frame %zu ch %u: got %#x expected %#x", + out_frame, ch, *r, expected); + } + r = audio_stream_wrap(sink, r + 1); + } + } + audio_stream_consume(sink, avail * frame_bytes); + } + + destroy_fixture(fixture); +} + +/** + * @brief Initialize the aria component registry for the suite. + * @return Suite fixture, unused. + */ +static void *aria_suite_setup(void) +{ + sys_comp_init(sof_get()); + sys_comp_module_aria_interface_init(); + return NULL; +} + +/* + * A short first period leaves the read position inside the ring, and with a + * 70 frame sink the sink wraps before the ring in later periods. The ring + * wrap distance must follow the read pointer across the sink wrap. + */ +ZTEST(aria_suite, test_sink_wrap_before_ring_wrap) +{ + run_aria(2, 48, 96, 70, 20, 1); +} + +/* + * 3 channels and a 47 frame chunk give an odd number of samples per chunk. + * The ring must still hold whole frames, so no frame straddles its end and + * channels stay in place. + */ +ZTEST(aria_suite, test_odd_samples_per_chunk) +{ + run_aria(3, 47, 94, 94, 47, 1); +} + +/* + * Odd samples per chunk, a short first period and a sink that wraps before + * the ring in the same run. + */ +ZTEST(aria_suite, test_odd_samples_sink_wrap_before_ring_wrap) +{ + run_aria(3, 47, 94, 70, 20, 1); +} + +/* + * With attenuation 0 aria bypasses the gain and copies the delayed input + * from the ring with cir_buf_copy(), which must wrap the same way. + */ +ZTEST(aria_suite, test_bypass_sink_wrap_before_ring_wrap) +{ + run_aria(3, 47, 94, 70, 20, 0); +} + +/* + * The get_data paths write whole frames up to the sink buffer end, so a + * source or sink buffer that is not a whole number of frames is rejected at + * prepare instead of overrunning the sink on the wrap. + */ +ZTEST(aria_suite, test_rejects_partial_frame_buffers) +{ + const size_t frame_bytes = 3 * sizeof(int32_t); + struct aria_fixture *fixture; + + fixture = create_fixture(3, 47 * frame_bytes, 94 * frame_bytes, + 70 * frame_bytes + sizeof(int32_t), 1); + zassert_equal(prepare_fixture(fixture), -EINVAL); + destroy_fixture(fixture); + + fixture = create_fixture(3, 47 * frame_bytes, 94 * frame_bytes + sizeof(int32_t), + 70 * frame_bytes, 1); + zassert_equal(prepare_fixture(fixture), -EINVAL); + destroy_fixture(fixture); +} + +ZTEST_SUITE(aria_suite, NULL, aria_suite_setup, NULL, NULL, NULL); diff --git a/test/ztest/unit/audio/aria/prj.conf b/test/ztest/unit/audio/aria/prj.conf new file mode 100644 index 000000000000..d999cf26c9ef --- /dev/null +++ b/test/ztest/unit/audio/aria/prj.conf @@ -0,0 +1,10 @@ +CONFIG_ZTEST=y +CONFIG_LOG=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n +CONFIG_LIBRARY=y +CONFIG_IPC_MAJOR_4=y +CONFIG_COMP_ARIA=y +CONFIG_ARIA_HIFI_NONE=y +CONFIG_STATIC_PIPELINE=n +CONFIG_FORMAT_S24LE=y +CONFIG_FORMAT_S32LE=y diff --git a/test/ztest/unit/audio/aria/testcase.yaml b/test/ztest/unit/audio/aria/testcase.yaml new file mode 100644 index 000000000000..76d5eae7b62a --- /dev/null +++ b/test/ztest/unit/audio/aria/testcase.yaml @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: BSD-3-Clause + +common: + tags: + - SOF + - unit_test + - audio + - aria + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.audio.aria: + platform_allow: + - native_sim