diff --git a/src/audio/eq_iir/tune/sof_cmocka_data_eq_fir.m b/src/audio/eq_iir/tune/sof_cmocka_data_eq_fir.m index fd9e4a91f797..cfc75dbb1a3a 100644 --- a/src/audio/eq_iir/tune/sof_cmocka_data_eq_fir.m +++ b/src/audio/eq_iir/tune/sof_cmocka_data_eq_fir.m @@ -13,10 +13,11 @@ function sof_cmocka_data_eq_fir() % Output files and paths sof_cmocka = '../../../../test/cmocka'; +sof_ztest = '../../../../test/ztest/unit/audio'; chirp_fn = fullfile(sof_cmocka, 'include/cmocka_chirp_2ch.h'); -ref_fn = fullfile(sof_cmocka, 'src/audio/eq_fir/cmocka_fir_ref.h'); -coef_fn = fullfile(sof_cmocka, 'src/audio/eq_fir/cmocka_fir_coef_2ch.h'); +ref_fn = fullfile(sof_ztest, 'eq_fir/cmocka_fir_ref.h'); +coef_fn = fullfile(sof_ztest, 'eq_fir/cmocka_fir_coef_2ch.h'); % Input data fs = 48e3; diff --git a/src/audio/eq_iir/tune/sof_cmocka_data_eq_iir.m b/src/audio/eq_iir/tune/sof_cmocka_data_eq_iir.m index 6c9b3732f283..944f83e22c87 100644 --- a/src/audio/eq_iir/tune/sof_cmocka_data_eq_iir.m +++ b/src/audio/eq_iir/tune/sof_cmocka_data_eq_iir.m @@ -13,9 +13,10 @@ function sof_cmocka_data_eq_iir() % Output files and paths sof_cmocka = '../../../../test/cmocka'; +sof_ztest = '../../../../test/ztest/unit/audio'; chirp_fn = fullfile(sof_cmocka, 'include/cmocka_chirp_2ch.h'); -ref_fn = fullfile(sof_cmocka, 'src/audio/eq_iir/cmocka_chirp_iir_ref_2ch.h'); -coef_fn = fullfile(sof_cmocka, 'src/audio/eq_iir/cmocka_iir_coef_2ch.h'); +ref_fn = fullfile(sof_ztest, 'eq_iir/cmocka_chirp_iir_ref_2ch.h'); +coef_fn = fullfile(sof_ztest, 'eq_iir/cmocka_iir_coef_2ch.h'); % Input data fs = 48e3; diff --git a/test/cmocka/src/audio/CMakeLists.txt b/test/cmocka/src/audio/CMakeLists.txt index ea0b424069b1..1f1f9205bf9a 100644 --- a/test/cmocka/src/audio/CMakeLists.txt +++ b/test/cmocka/src/audio/CMakeLists.txt @@ -1,22 +1,4 @@ # SPDX-License-Identifier: BSD-3-Clause add_subdirectory(component) -if(CONFIG_COMP_MIXER) - add_subdirectory(mixer) -endif() add_subdirectory(pipeline) -if(CONFIG_COMP_VOLUME) - add_subdirectory(volume) -endif() -if(CONFIG_COMP_SEL) - add_subdirectory(selector) -endif() -if(CONFIG_COMP_IIR) - add_subdirectory(eq_iir) -endif() -if(CONFIG_COMP_FIR) - add_subdirectory(eq_fir) -endif() -if(CONFIG_COMP_DCBLOCK) - add_subdirectory(dcblock) -endif() diff --git a/test/cmocka/src/audio/dcblock/CMakeLists.txt b/test/cmocka/src/audio/dcblock/CMakeLists.txt deleted file mode 100644 index fe6294d64635..000000000000 --- a/test/cmocka/src/audio/dcblock/CMakeLists.txt +++ /dev/null @@ -1,49 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -cmocka_test(dcblock_process - dcblock_process.c -) - -target_include_directories(dcblock_process PRIVATE ${PROJECT_SOURCE_DIR}/src/audio) -target_include_directories(dcblock_process PRIVATE ${PROJECT_SOURCE_DIR}/src/audio/dcblock) - -# make small version of libaudio so we don't have to care -# about unused missing references - -add_compile_options(-DUNIT_TEST) - -add_library(audio_for_dcblock STATIC - ${PROJECT_SOURCE_DIR}/src/audio/dcblock/dcblock.c - ${PROJECT_SOURCE_DIR}/src/audio/dcblock/dcblock_generic.c - ${PROJECT_SOURCE_DIR}/src/audio/dcblock/dcblock_ipc3.c - ${PROJECT_SOURCE_DIR}/src/math/numbers.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter_ipc3.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module/generic.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/comp_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/audio_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c - ${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c - ${PROJECT_SOURCE_DIR}/src/audio/component.c - ${PROJECT_SOURCE_DIR}/src/audio/data_blob.c - ${PROJECT_SOURCE_DIR}/src/module/audio/source_api.c - ${PROJECT_SOURCE_DIR}/src/module/audio/sink_api.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc3/helper.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc-common-kernel.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c - ${PROJECT_SOURCE_DIR}/src/lib/objpool.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-schedule.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-stream.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-xrun.c -) -sof_append_relative_path_definitions(audio_for_dcblock) - -target_link_libraries(audio_for_dcblock PRIVATE sof_options) - -target_link_libraries(dcblock_process PRIVATE audio_for_dcblock) diff --git a/test/cmocka/src/audio/dcblock/dcblock_process.c b/test/cmocka/src/audio/dcblock/dcblock_process.c deleted file mode 100644 index 53868fe17287..000000000000 --- a/test/cmocka/src/audio/dcblock/dcblock_process.c +++ /dev/null @@ -1,618 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2026 Intel Corporation. - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "dcblock.h" - -#include "../../util.h" -#include "../../../include/cmocka_chirp_2ch.h" - -/* Allow a few LSB of error due to the internal Q-format rounding. */ -#define ERROR_TOLERANCE_S16 2 -#define ERROR_TOLERANCE_S24 4 -#define ERROR_TOLERANCE_S32 2048 - -/* Q2.30 DC blocking coefficient (~0.98) used for all channels. */ -#define R_COEF 1052266987 - -/* Thresholds for frames count jitter for rand() function */ -#define THR_RAND_PLUS_ONE ((RAND_MAX >> 1) + (RAND_MAX >> 2)) -#define THR_RAND_MINUS_ONE ((RAND_MAX >> 1) - (RAND_MAX >> 2)) - -struct buffer_state { - int idx; -}; - -static struct buffer_state buffer_fill_data; -static struct buffer_state buffer_verify_data; - -/* Floating point reference filter state, one per channel. */ -struct ref_state { - double x_prev; - double y_prev; -}; - -static struct ref_state ref_states[CONFIG_PLATFORM_MAX_CHANNELS]; - -static double dcblock_ref(int ch, double x) -{ - struct ref_state *s = &ref_states[ch]; - double r = (double)R_COEF / (double)ONE_Q2_30; - double y = x - s->x_prev + r * s->y_prev; - - /* The fixed point filter saturates the Q1.31 state after every - * sample (sat_int32). The DC blocker has a passband gain slightly - * above one, so without this clamp the reference state would drift - * away from the implementation for near full-scale input. - */ - if (y > 2147483647.0 / 2147483648.0) - y = 2147483647.0 / 2147483648.0; - else if (y < -1.0) - y = -1.0; - - s->x_prev = x; - s->y_prev = y; - return y; -} - -struct test_parameters { - uint32_t channels; - uint32_t frames; - uint32_t buffer_size_mult; - uint32_t source_format; - uint32_t sink_format; -}; - -struct test_data { - struct comp_dev *dev; - struct comp_buffer *sink; - struct comp_buffer *source; - struct test_parameters *params; - struct processing_module *mod; - bool continue_loop; - size_t input_frames; - size_t output_bytes; -}; - -static int setup_group(void **state) -{ - sys_comp_init(sof_get()); - sys_comp_module_dcblock_interface_init(); - return 0; -} - -static struct sof_ipc_comp_process *create_dcblock_comp_ipc(struct test_data *td) -{ - struct sof_ipc_comp_process *ipc; - size_t ipc_size = sizeof(struct sof_ipc_comp_process); - const struct sof_uuid uuid = SOF_REG_UUID(dcblock); - - ipc = calloc(1, ipc_size + SOF_UUID_SIZE); - if (!ipc) - return NULL; - - memcpy_s(ipc + 1, SOF_UUID_SIZE, &uuid, SOF_UUID_SIZE); - ipc->comp.hdr.size = ipc_size + SOF_UUID_SIZE; - ipc->comp.type = SOF_COMP_MODULE_ADAPTER; - ipc->config.hdr.size = sizeof(struct sof_ipc_comp_config); - ipc->size = 0; - ipc->comp.ext_data_length = SOF_UUID_SIZE; - return ipc; -} - -/* Send a configuration blob holding the per-channel R coefficients. */ -static int dcblock_send_config(struct processing_module *mod) -{ - const size_t coeff_size = CONFIG_PLATFORM_MAX_CHANNELS * sizeof(int32_t); - size_t cdata_size = sizeof(struct sof_ipc_ctrl_data) + - sizeof(struct sof_abi_hdr) + coeff_size; - struct sof_ipc_ctrl_data *cdata; - int32_t coeffs[CONFIG_PLATFORM_MAX_CHANNELS]; - int ret; - int i; - - cdata = calloc(1, cdata_size); - if (!cdata) - return -ENOMEM; - - for (i = 0; i < CONFIG_PLATFORM_MAX_CHANNELS; i++) - coeffs[i] = R_COEF; - - cdata->cmd = SOF_CTRL_CMD_BINARY; - cdata->num_elems = coeff_size; - cdata->data[0].magic = SOF_ABI_MAGIC; - cdata->data[0].type = 0; - cdata->data[0].size = coeff_size; - cdata->data[0].abi = SOF_ABI_VERSION; - memcpy_s(cdata->data[0].data, coeff_size, coeffs, coeff_size); - - ret = module_adapter_cmd(mod->dev, COMP_CMD_SET_DATA, cdata, cdata_size); - - free(cdata); - return ret; -} - -static void prepare_sink(struct test_data *td, struct processing_module *mod) -{ - struct test_parameters *parameters = td->params; - struct module_data *md = &mod->priv; - size_t size; - size_t free; - - size = parameters->frames * get_frame_bytes(parameters->sink_format, parameters->channels) * - parameters->buffer_size_mult; - - md->mpd.out_buff_size = parameters->frames * get_frame_bytes(parameters->sink_format, - parameters->channels); - - td->sink = create_test_sink(td->dev, 0, parameters->sink_format, - parameters->channels, size); - free = audio_stream_get_free_bytes(&td->sink->stream); - assert_int_equal(free, size); -} - -static void prepare_source(struct test_data *td, struct processing_module *mod) -{ - struct test_parameters *parameters = td->params; - struct module_data *md = &mod->priv; - size_t size; - size_t free; - - md->mpd.in_buff_size = parameters->frames * get_frame_bytes(parameters->source_format, - parameters->channels); - - size = parameters->frames * get_frame_bytes(parameters->source_format, - parameters->channels) * parameters->buffer_size_mult; - - td->source = create_test_source(td->dev, 0, parameters->source_format, - parameters->channels, size); - free = audio_stream_get_free_bytes(&td->source->stream); - assert_int_equal(free, size); -} - -static void cleanup_test_data(struct test_data *td) -{ - struct processing_module *mod; - - if (!td) - return; - - if (td->dev) { - mod = comp_mod(td->dev); - if (mod) { - test_free(mod->stream_params); - mod->stream_params = NULL; - } - } - - if (td->source) { - free_test_source(td->source); - td->source = NULL; - } - - if (td->sink) { - free_test_sink(td->sink); - td->sink = NULL; - } - - if (td->dev) { - comp_free(td->dev); - td->dev = NULL; - } - - test_free(td->params); - test_free(td); -} - -static int setup(void **state) -{ - struct test_parameters *params = *state; - struct processing_module *mod; - struct test_data *td; - struct sof_ipc_comp_process *ipc = NULL; - struct comp_dev *dev; - struct sof_source *sources[1]; - struct sof_sink *sinks[1]; - int ret; - int i; - - td = test_malloc(sizeof(*td)); - if (!td) { - *state = NULL; - return -EINVAL; - } - - *td = (struct test_data){ 0 }; - *state = td; - td->params = test_malloc(sizeof(*params)); - if (!td->params) { - ret = -ENOMEM; - goto error; - } - - memcpy_s(td->params, sizeof(*td->params), params, sizeof(*params)); - ipc = create_dcblock_comp_ipc(td); - if (!ipc) { - ret = -ENOMEM; - goto error; - } - - buffer_fill_data.idx = 0; - buffer_verify_data.idx = 0; - for (i = 0; i < CONFIG_PLATFORM_MAX_CHANNELS; i++) { - ref_states[i].x_prev = 0.0; - ref_states[i].y_prev = 0.0; - } - - dev = comp_new((struct sof_ipc_comp *)ipc); - free(ipc); - ipc = NULL; - if (!dev) { - ret = -EINVAL; - goto error; - } - - td->dev = dev; - dev->frames = params->frames; - mod = comp_mod(dev); - - ret = dcblock_send_config(mod); - if (ret) - goto error; - - prepare_sink(td, mod); - prepare_source(td, mod); - - mod->stream_params = test_malloc(sizeof(struct sof_ipc_stream_params)); - if (!mod->stream_params) { - ret = -ENOMEM; - goto error; - } - mod->stream_params->channels = params->channels; - mod->period_bytes = get_frame_bytes(params->source_format, params->channels) * 48000 / 1000; - - sources[0] = audio_buffer_get_source(&td->source->audio_buffer); - sinks[0] = audio_buffer_get_sink(&td->sink->audio_buffer); - ret = module_prepare(mod, sources, 1, sinks, 1); - if (ret) - goto error; - - td->continue_loop = true; - - *state = td; - return 0; - -error: - free(ipc); - cleanup_test_data(td); - *state = NULL; - return ret; -} - -static int teardown(void **state) -{ - struct test_data *td = *state; - - cleanup_test_data(td); - return 0; -} - -#if CONFIG_FORMAT_S16LE -static void fill_source_s16(struct test_data *td, int frames_max) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int16_t *x; - int bytes_total; - int samples; - int frames; - int i; - int samples_processed = 0; - - sb = comp_dev_get_first_data_producer(dev); - ss = &sb->stream; - frames = MIN(audio_stream_get_free_frames(ss), frames_max); - samples = frames * audio_stream_get_channels(ss); - for (i = 0; i < samples; i++) { - x = audio_stream_write_frag_s16(ss, i); - *x = sat_int16(Q_SHIFT_RND(chirp_2ch[buffer_fill_data.idx++], 31, 15)); - samples_processed++; - if (buffer_fill_data.idx == CHIRP_2CH_LENGTH) { - td->continue_loop = false; - break; - } - } - - if (samples_processed > 0) { - bytes_total = samples_processed * audio_stream_sample_bytes(ss); - comp_update_buffer_produce(sb, bytes_total); - } - - td->input_frames = samples_processed / audio_stream_get_channels(ss); -} - -static void verify_sink_s16(struct test_data *td) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int nch = td->params->channels; - int32_t delta; - int32_t ref; - int32_t out; - int16_t *x; - int16_t in; - int samples; - int i; - - sb = comp_dev_get_first_data_consumer(dev); - ss = &sb->stream; - samples = td->output_bytes >> 1; - for (i = 0; i < samples; i++) { - x = audio_stream_read_frag_s16(ss, i); - out = *x; - in = sat_int16(Q_SHIFT_RND(chirp_2ch[buffer_verify_data.idx], 31, 15)); - ref = (int32_t)round(dcblock_ref(buffer_verify_data.idx % nch, - (double)in / 32768.0) * 32768.0); - ref = sat_int16(ref); - buffer_verify_data.idx++; - delta = ref - out; - if (delta > ERROR_TOLERANCE_S16 || delta < -ERROR_TOLERANCE_S16) - assert_int_equal(out, ref); - } -} -#endif /* CONFIG_FORMAT_S16LE */ - -#if CONFIG_FORMAT_S24LE -static void fill_source_s24(struct test_data *td, int frames_max) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int32_t *x; - int bytes_total; - int samples; - int frames; - int i; - int samples_processed = 0; - - sb = comp_dev_get_first_data_producer(dev); - ss = &sb->stream; - frames = MIN(audio_stream_get_free_frames(ss), frames_max); - samples = frames * audio_stream_get_channels(ss); - for (i = 0; i < samples; i++) { - x = audio_stream_write_frag_s32(ss, i); - *x = sat_int24(Q_SHIFT_RND(chirp_2ch[buffer_fill_data.idx++], 31, 23)); - samples_processed++; - if (buffer_fill_data.idx == CHIRP_2CH_LENGTH) { - td->continue_loop = false; - break; - } - } - - if (samples_processed > 0) { - bytes_total = samples_processed * audio_stream_sample_bytes(ss); - comp_update_buffer_produce(sb, bytes_total); - } - - td->input_frames = samples_processed / audio_stream_get_channels(ss); -} - -static void verify_sink_s24(struct test_data *td) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int nch = td->params->channels; - int32_t delta; - int32_t ref; - int32_t out; - int32_t *x; - int32_t in; - int samples; - int i; - - sb = comp_dev_get_first_data_consumer(dev); - ss = &sb->stream; - samples = td->output_bytes >> 2; - for (i = 0; i < samples; i++) { - x = audio_stream_read_frag_s32(ss, i); - out = (*x << 8) >> 8; /* Make sure there's no 24 bit overflow */ - in = sat_int24(Q_SHIFT_RND(chirp_2ch[buffer_verify_data.idx], 31, 23)); - ref = (int32_t)round(dcblock_ref(buffer_verify_data.idx % nch, - (double)in / 8388608.0) * 8388608.0); - ref = sat_int24(ref); - buffer_verify_data.idx++; - delta = ref - out; - if (delta > ERROR_TOLERANCE_S24 || delta < -ERROR_TOLERANCE_S24) - assert_int_equal(out, ref); - } -} -#endif /* CONFIG_FORMAT_S24LE */ - -#if CONFIG_FORMAT_S32LE -static void fill_source_s32(struct test_data *td, int frames_max) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int32_t *x; - int bytes_total; - int samples; - int frames; - int i; - int samples_processed = 0; - - sb = comp_dev_get_first_data_producer(dev); - ss = &sb->stream; - frames = MIN(audio_stream_get_free_frames(ss), frames_max); - samples = frames * audio_stream_get_channels(ss); - for (i = 0; i < samples; i++) { - x = audio_stream_write_frag_s32(ss, i); - *x = chirp_2ch[buffer_fill_data.idx++]; - samples_processed++; - if (buffer_fill_data.idx == CHIRP_2CH_LENGTH) { - td->continue_loop = false; - break; - } - } - - if (samples_processed > 0) { - bytes_total = samples_processed * audio_stream_sample_bytes(ss); - comp_update_buffer_produce(sb, bytes_total); - } - - td->input_frames = samples_processed / audio_stream_get_channels(ss); -} - -static void verify_sink_s32(struct test_data *td) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int nch = td->params->channels; - int64_t delta; - int32_t ref; - int32_t out; - int32_t *x; - int32_t in; - int samples; - int i; - - sb = comp_dev_get_first_data_consumer(dev); - ss = &sb->stream; - samples = td->output_bytes >> 2; - for (i = 0; i < samples; i++) { - x = audio_stream_read_frag_s32(ss, i); - out = *x; - in = chirp_2ch[buffer_verify_data.idx]; - ref = (int32_t)round(dcblock_ref(buffer_verify_data.idx % nch, - (double)in / 2147483648.0) * 2147483648.0); - buffer_verify_data.idx++; - delta = (int64_t)ref - (int64_t)out; - if (delta > ERROR_TOLERANCE_S32 || delta < -ERROR_TOLERANCE_S32) - assert_int_equal(out, ref); - } -} -#endif /* CONFIG_FORMAT_S32LE */ - -static int frames_jitter(int frames) -{ - int r = rand(); - - if (r > THR_RAND_PLUS_ONE) - return frames + 1; - else if (r < THR_RAND_MINUS_ONE) - return frames - 1; - else - return frames; -} - -static void test_audio_dcblock(void **state) -{ - struct test_data *td = *state; - struct processing_module *mod = comp_mod(td->dev); - struct comp_buffer *source = td->source; - struct comp_buffer *sink = td->sink; - struct sof_source *sources[1]; - struct sof_sink *sinks[1]; - uint32_t avail_before; - int ret; - int frames; - - sources[0] = audio_buffer_get_source(&source->audio_buffer); - sinks[0] = audio_buffer_get_sink(&sink->audio_buffer); - - while (td->continue_loop) { - frames = frames_jitter(td->params->frames); - switch (audio_stream_get_frm_fmt(&source->stream)) { - case SOF_IPC_FRAME_S16_LE: - fill_source_s16(td, frames); - break; - case SOF_IPC_FRAME_S24_4LE: - fill_source_s24(td, frames); - break; - case SOF_IPC_FRAME_S32_LE: - fill_source_s32(td, frames); - break; - default: - assert(0); - break; - } - - /* Process exactly the frames just filled. The source/sink API - * updates the buffer read/write positions internally, so record - * the sink fill level to know how many bytes were produced. - */ - td->dev->frames = td->input_frames; - avail_before = audio_stream_get_avail_bytes(&sink->stream); - ret = module_process_sink_src(mod, sources, 1, sinks, 1); - assert_int_equal(ret, 0); - td->output_bytes = audio_stream_get_avail_bytes(&sink->stream) - avail_before; - - switch (audio_stream_get_frm_fmt(&sink->stream)) { - case SOF_IPC_FRAME_S16_LE: - verify_sink_s16(td); - break; - case SOF_IPC_FRAME_S24_4LE: - verify_sink_s24(td); - break; - case SOF_IPC_FRAME_S32_LE: - verify_sink_s32(td); - break; - default: - assert(0); - break; - } - - comp_update_buffer_consume(sink, td->output_bytes); - } -} - -static struct test_parameters parameters[] = { -#if CONFIG_FORMAT_S16LE - { 2, 48, 2, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE }, -#endif /* CONFIG_FORMAT_S16LE */ -#if CONFIG_FORMAT_S24LE - { 2, 48, 2, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE }, -#endif /* CONFIG_FORMAT_S24LE */ -#if CONFIG_FORMAT_S32LE - { 2, 48, 2, SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S32_LE }, -#endif /* CONFIG_FORMAT_S32LE */ -}; - -int main(void) -{ - int i; - - struct CMUnitTest tests[ARRAY_SIZE(parameters)]; - - for (i = 0; i < ARRAY_SIZE(parameters); i++) { - tests[i].name = "test_audio_dcblock"; - tests[i].test_func = test_audio_dcblock; - tests[i].setup_func = setup; - tests[i].teardown_func = teardown; - tests[i].initial_state = ¶meters[i]; - } - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, setup_group, NULL); -} diff --git a/test/cmocka/src/audio/eq_fir/CMakeLists.txt b/test/cmocka/src/audio/eq_fir/CMakeLists.txt deleted file mode 100644 index 485d1996dd8d..000000000000 --- a/test/cmocka/src/audio/eq_fir/CMakeLists.txt +++ /dev/null @@ -1,54 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -cmocka_test(eq_fir_process - eq_fir_process.c -) - -target_include_directories(eq_fir_process PRIVATE ${PROJECT_SOURCE_DIR}/src/audio) - -# make small version of libaudio so we don't have to care -# about unused missing references - -add_compile_options(-DUNIT_TEST) - -add_library(audio_for_eq_fir STATIC - ${PROJECT_SOURCE_DIR}/src/audio/eq_fir/eq_fir.c - ${PROJECT_SOURCE_DIR}/src/audio/eq_fir/eq_fir_ipc3.c - ${PROJECT_SOURCE_DIR}/src/audio/eq_fir/eq_fir_generic.c - ${PROJECT_SOURCE_DIR}/src/audio/eq_fir/eq_fir_hifi2ep.c - ${PROJECT_SOURCE_DIR}/src/audio/eq_fir/eq_fir_hifi3.c - ${PROJECT_SOURCE_DIR}/src/math/fir_generic.c - ${PROJECT_SOURCE_DIR}/src/math/fir_float.c - ${PROJECT_SOURCE_DIR}/src/math/fir_hifi2ep.c - ${PROJECT_SOURCE_DIR}/src/math/fir_hifi3.c - ${PROJECT_SOURCE_DIR}/src/math/numbers.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter_ipc3.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module/generic.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/comp_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/audio_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c - ${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c - ${PROJECT_SOURCE_DIR}/src/audio/component.c - ${PROJECT_SOURCE_DIR}/src/audio/data_blob.c - ${PROJECT_SOURCE_DIR}/src/module/audio/source_api.c - ${PROJECT_SOURCE_DIR}/src/module/audio/sink_api.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc3/helper.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc-common-kernel.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c - ${PROJECT_SOURCE_DIR}/src/lib/objpool.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-schedule.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-stream.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-xrun.c -) -sof_append_relative_path_definitions(audio_for_eq_fir) - -target_link_libraries(audio_for_eq_fir PRIVATE sof_options) - -target_link_libraries(eq_fir_process PRIVATE audio_for_eq_fir) diff --git a/test/cmocka/src/audio/eq_fir/eq_fir_process.c b/test/cmocka/src/audio/eq_fir/eq_fir_process.c deleted file mode 100644 index cd3183e2ca19..000000000000 --- a/test/cmocka/src/audio/eq_fir/eq_fir_process.c +++ /dev/null @@ -1,750 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2022 Intel Corporation. All rights reserved. - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "../../util.h" -#include "../../../include/cmocka_chirp_2ch.h" -#include "cmocka_fir_ref.h" -#include "cmocka_fir_coef_2ch.h" - -/* Allow some small error for fixed point */ -#define ERROR_TOLERANCE_S16 1 -#define ERROR_TOLERANCE_S24 2 -#define ERROR_TOLERANCE_S32 4 - -/* Thresholds for frames count jitter for rand() function */ -#define THR_RAND_PLUS_ONE ((RAND_MAX >> 1) + (RAND_MAX >> 2)) -#define THR_RAND_MINUS_ONE ((RAND_MAX >> 1) - (RAND_MAX >> 2)) - -/* Export optionally data to files for easier debug */ -#undef DEBUG_FILES -#ifdef DEBUG_FILES -FILE *debug_fh_16; -FILE *debug_fh_24; -FILE *debug_fh_32; -#endif - -struct buffer_fill { - int idx; -} buffer_fill_data; - -struct buffer_verify { - int idx; -} buffer_verify_data; - -struct test_parameters { - unsigned int channels; - size_t frames; - uint32_t buffer_size_mult; - uint32_t source_format; - uint32_t sink_format; -}; - -struct test_data { - struct comp_dev *dev; - struct comp_buffer *sink; - struct comp_buffer *source; - struct test_parameters *params; - struct processing_module *mod; - bool continue_loop; - size_t input_frames; - size_t output_bytes; -}; - -static int setup_group(void **state) -{ - sys_comp_init(sof_get()); - sys_comp_module_eq_fir_interface_init(); - return 0; -} - -static struct sof_ipc_comp_process *create_eq_fir_comp_ipc(struct test_data *td) -{ - struct sof_ipc_comp_process *ipc; - size_t ipc_size = sizeof(struct sof_ipc_comp_process); - const struct sof_uuid uuid = SOF_REG_UUID(eq_fir); - - ipc = calloc(1, ipc_size + SOF_UUID_SIZE); - memcpy_s(ipc + 1, SOF_UUID_SIZE, &uuid, SOF_UUID_SIZE); - ipc->comp.hdr.size = ipc_size + SOF_UUID_SIZE; - ipc->comp.type = SOF_COMP_MODULE_ADAPTER; - ipc->config.hdr.size = sizeof(struct sof_ipc_comp_config); - ipc->size = 0; - ipc->comp.ext_data_length = SOF_UUID_SIZE; - return ipc; -} - -static int eq_fir_send_blob(struct processing_module *mod, - const struct sof_abi_hdr *blob, - size_t blob_storage_size) -{ - const struct module_interface *const ops = mod->dev->drv->adapter_ops; - size_t cdata_size = sizeof(struct sof_ipc_ctrl_data) + sizeof(struct sof_abi_hdr) + - blob->size; - struct sof_ipc_ctrl_data *cdata; - size_t copy_size; - int ret; - - cdata = calloc(1, cdata_size); - if (!cdata) - return -ENOMEM; - - cdata->cmd = SOF_CTRL_CMD_BINARY; - cdata->num_elems = blob->size; - cdata->data[0].magic = blob->magic; - cdata->data[0].type = blob->type; - cdata->data[0].size = blob->size; - cdata->data[0].abi = blob->abi; - copy_size = blob_storage_size > sizeof(*blob) ? blob_storage_size - sizeof(*blob) : 0; - copy_size = MIN(copy_size, (size_t)blob->size); - if (copy_size) - memcpy_s(cdata->data[0].data, blob->size, blob->data, copy_size); - - ret = ops->set_configuration(mod, 0, MODULE_CFG_FRAGMENT_SINGLE, - blob->size, (const uint8_t *)cdata, - blob->size, NULL, 0); - - free(cdata); - return ret; -} - -static int eq_fir_send_config(struct processing_module *mod) -{ - return eq_fir_send_blob(mod, (const struct sof_abi_hdr *)fir_coef_2ch, - sizeof(fir_coef_2ch)); -} - -static void copy_eq_fir_blob(uint32_t *blob) -{ - assert_int_equal(memcpy_s(blob, sizeof(fir_coef_2ch), fir_coef_2ch, - sizeof(fir_coef_2ch)), 0); -} - -static void prepare_sink(struct test_data *td, struct processing_module *mod) -{ - struct test_parameters *parameters = td->params; - struct module_data *md = &mod->priv; - size_t size; - size_t free; - - /* allocate new sink buffer */ - size = parameters->frames * get_frame_bytes(parameters->sink_format, parameters->channels) * - parameters->buffer_size_mult; - - md->mpd.out_buff_size = parameters->frames * get_frame_bytes(parameters->sink_format, - parameters->channels); - - td->sink = create_test_sink(td->dev, 0, parameters->sink_format, - parameters->channels, size); - free = audio_stream_get_free_bytes(&td->sink->stream); - assert_int_equal(free, size); -} - -static void prepare_source(struct test_data *td, struct processing_module *mod) -{ - struct test_parameters *parameters = td->params; - struct module_data *md = &mod->priv; - size_t size; - size_t free; - - md->mpd.in_buff_size = parameters->frames * get_frame_bytes(parameters->source_format, - parameters->channels); - - size = parameters->frames * get_frame_bytes(parameters->source_format, - parameters->channels) * parameters->buffer_size_mult; - - td->source = create_test_source(td->dev, 0, parameters->source_format, - parameters->channels, size); - free = audio_stream_get_free_bytes(&td->source->stream); - assert_int_equal(free, size); -} - -static int setup(void **state) -{ - struct test_parameters *params = *state; - struct processing_module *mod; - struct test_data *td; - struct sof_ipc_comp_process *ipc; - struct comp_dev *dev; - struct sof_source *sources[1]; - struct sof_sink *sinks[1]; - int ret; - - td = test_malloc(sizeof(*td)); - if (!td) - return -EINVAL; - - td->params = test_malloc(sizeof(*params)); - if (!td->params) - return -EINVAL; - - memcpy_s(td->params, sizeof(*td->params), params, sizeof(*params)); - ipc = create_eq_fir_comp_ipc(td); - buffer_fill_data.idx = 0; - buffer_verify_data.idx = 0; - - dev = comp_new((struct sof_ipc_comp *)ipc); - free(ipc); - if (!dev) - return -EINVAL; - - td->dev = dev; - dev->frames = params->frames; - mod = comp_mod(dev); - - ret = eq_fir_send_config(mod); - if (ret) - return ret; - - prepare_sink(td, mod); - prepare_source(td, mod); - - mod->stream_params = test_malloc(sizeof(struct sof_ipc_stream_params)); - mod->stream_params->channels = params->channels; - mod->period_bytes = get_frame_bytes(params->source_format, params->channels) * 48000 / 1000; - - sources[0] = audio_buffer_get_source(&td->source->audio_buffer); - sinks[0] = audio_buffer_get_sink(&td->sink->audio_buffer); - ret = module_prepare(mod, sources, 1, sinks, 1); - if (ret) - return ret; - - td->continue_loop = true; - - *state = td; - return 0; -} - -static int teardown(void **state) -{ - struct test_data *td = *state; - struct processing_module *mod = comp_mod(td->dev); - - test_free(mod->stream_params); - mod->stream_params = NULL; - test_free(td->params); - free_test_source(td->source); - free_test_sink(td->sink); - comp_free(td->dev); - test_free(td); - return 0; -} - -#if CONFIG_FORMAT_S16LE -static void fill_source_s16(struct test_data *td, size_t frames_max) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int16_t *x; - size_t bytes_total; - size_t samples; - size_t frames; - size_t i; - size_t samples_processed = 0; - - sb = comp_dev_get_first_data_producer(dev); - ss = &sb->stream; - frames = MIN(audio_stream_get_free_frames(ss), frames_max); - samples = frames * audio_stream_get_channels(ss); - for (i = 0; i < samples; i++) { - x = audio_stream_write_frag_s16(ss, i); - *x = sat_int16(Q_SHIFT_RND(chirp_2ch[buffer_fill_data.idx++], 31, 15)); - samples_processed++; - if (buffer_fill_data.idx == CHIRP_2CH_LENGTH) { - td->continue_loop = false; - break; - } - } - - if (samples_processed > 0) { - bytes_total = samples_processed * audio_stream_sample_bytes(ss); - comp_update_buffer_produce(sb, bytes_total); - } - - td->input_frames = samples_processed / audio_stream_get_channels(ss); -} - -static void verify_sink_s16(struct test_data *td) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int32_t delta; - int32_t ref; - int32_t out; - int16_t *x; - size_t samples; - size_t i; - - sb = comp_dev_get_first_data_consumer(dev); - ss = &sb->stream; - samples = td->output_bytes >> 1; - for (i = 0; i < samples; i++) { - x = audio_stream_read_frag_s16(ss, i); - out = *x; - ref = sat_int16(Q_SHIFT_RND(fir_ref_2ch[buffer_verify_data.idx++], 31, 15)); - delta = ref - out; - if (delta > ERROR_TOLERANCE_S16 || delta < -ERROR_TOLERANCE_S16) - assert_int_equal(out, ref); -#ifdef DEBUG_FILES - fprintf(debug_fh_16, "%d %d\n", ref, out); -#endif - } -} -#endif /* CONFIG_FORMAT_S16LE */ - -#if CONFIG_FORMAT_S24LE -static void fill_source_s24(struct test_data *td, size_t frames_max) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int32_t *x; - size_t bytes_total; - size_t samples; - size_t frames; - size_t i; - size_t samples_processed = 0; - - sb = comp_dev_get_first_data_producer(dev); - ss = &sb->stream; - frames = MIN(audio_stream_get_free_frames(ss), frames_max); - samples = frames * audio_stream_get_channels(ss); - for (i = 0; i < samples; i++) { - x = audio_stream_write_frag_s32(ss, i); - *x = sat_int24(Q_SHIFT_RND(chirp_2ch[buffer_fill_data.idx++], 31, 23)); - samples_processed++; - if (buffer_fill_data.idx == CHIRP_2CH_LENGTH) { - td->continue_loop = false; - break; - } - } - - if (samples_processed > 0) { - bytes_total = samples_processed * audio_stream_sample_bytes(ss); - comp_update_buffer_produce(sb, bytes_total); - } - - td->input_frames = samples_processed / audio_stream_get_channels(ss); -} - -static void verify_sink_s24(struct test_data *td) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int32_t delta; - int32_t ref; - int32_t out; - int32_t *x; - size_t samples; - size_t i; - - sb = comp_dev_get_first_data_consumer(dev); - ss = &sb->stream; - samples = td->output_bytes >> 2; - for (i = 0; i < samples; i++) { - x = audio_stream_read_frag_s32(ss, i); - out = (*x << 8) >> 8; /* Make sure there's no 24 bit overflow */ - ref = sat_int24(Q_SHIFT_RND(fir_ref_2ch[buffer_verify_data.idx++], 31, 23)); - delta = ref - out; - if (delta > ERROR_TOLERANCE_S24 || delta < -ERROR_TOLERANCE_S24) - assert_int_equal(out, ref); -#ifdef DEBUG_FILES - fprintf(debug_fh_24, "%d %d\n", ref, out); -#endif - } -} -#endif /* CONFIG_FORMAT_S24LE */ - -#if CONFIG_FORMAT_S32LE -static void fill_source_s32(struct test_data *td, size_t frames_max) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int32_t *x; - size_t bytes_total; - size_t samples; - size_t frames; - size_t i; - size_t samples_processed = 0; - - sb = comp_dev_get_first_data_producer(dev); - ss = &sb->stream; - frames = MIN(audio_stream_get_free_frames(ss), frames_max); - samples = frames * audio_stream_get_channels(ss); - for (i = 0; i < samples; i++) { - x = audio_stream_write_frag_s32(ss, i); - *x = chirp_2ch[buffer_fill_data.idx++]; - samples_processed++; - if (buffer_fill_data.idx == CHIRP_2CH_LENGTH) { - td->continue_loop = false; - break; - } - } - - if (samples_processed > 0) { - bytes_total = samples_processed * audio_stream_sample_bytes(ss); - comp_update_buffer_produce(sb, bytes_total); - } - - td->input_frames = samples_processed / audio_stream_get_channels(ss); -} - -static void verify_sink_s32(struct test_data *td) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int64_t delta; - int32_t ref; - int32_t out; - int32_t *x; - size_t samples; - size_t i; - - sb = comp_dev_get_first_data_consumer(dev); - ss = &sb->stream; - samples = td->output_bytes >> 2; - for (i = 0; i < samples; i++) { - x = audio_stream_read_frag_s32(ss, i); - out = *x; - ref = fir_ref_2ch[buffer_verify_data.idx++]; - delta = (int64_t)ref - (int64_t)out; - if (delta > ERROR_TOLERANCE_S32 || delta < -ERROR_TOLERANCE_S32) - assert_int_equal(out, ref); -#ifdef DEBUG_FILES - fprintf(debug_fh_32, "%d %d\n", ref, out); -#endif - } -} -#endif /* CONFIG_FORMAT_S32LE */ - -static void fill_source_for_test(struct test_data *td, size_t frames) -{ - switch (audio_stream_get_frm_fmt(&td->source->stream)) { -#if CONFIG_FORMAT_S16LE - case SOF_IPC_FRAME_S16_LE: - fill_source_s16(td, frames); - break; -#endif -#if CONFIG_FORMAT_S24LE - case SOF_IPC_FRAME_S24_4LE: - fill_source_s24(td, frames); - break; -#endif -#if CONFIG_FORMAT_S32LE - case SOF_IPC_FRAME_S32_LE: - fill_source_s32(td, frames); - break; -#endif - default: - assert_true(false); - break; - } -} - -static void test_eq_fir_rounds_down_odd_frame_count(void **state) -{ - struct test_data *td = *state; - struct processing_module *mod = comp_mod(td->dev); - struct sof_source *sources[1]; - struct sof_sink *sinks[1]; - const size_t frame_bytes = get_frame_bytes(td->params->source_format, - td->params->channels); - size_t source_avail_before; - size_t sink_avail_before; - int ret; - - sources[0] = audio_buffer_get_source(&td->source->audio_buffer); - sinks[0] = audio_buffer_get_sink(&td->sink->audio_buffer); - fill_source_for_test(td, 3); - source_avail_before = audio_stream_get_avail_bytes(&td->source->stream); - sink_avail_before = audio_stream_get_avail_bytes(&td->sink->stream); - - ret = module_process_sink_src(mod, sources, 1, sinks, 1); - assert_int_equal(ret, 0); - assert_int_equal(source_avail_before - - audio_stream_get_avail_bytes(&td->source->stream), - 2 * frame_bytes); - assert_int_equal(audio_stream_get_avail_bytes(&td->sink->stream) - - sink_avail_before, 2 * frame_bytes); -} - -static void test_eq_fir_rejects_invalid_configurations(void **state) -{ - struct test_data *td = *state; - struct processing_module *mod = comp_mod(td->dev); - uint32_t blob_copy[ARRAY_SIZE(fir_coef_2ch)]; - struct sof_abi_hdr *abi; - struct sof_eq_fir_config *config; - struct sof_fir_coef_data *coef; - int ret; - - /* Reject a blob with no channel assignments or coefficient data. */ - copy_eq_fir_blob(blob_copy); - abi = (struct sof_abi_hdr *)blob_copy; - config = (struct sof_eq_fir_config *)abi->data; - abi->size = sizeof(*config); - config->size = abi->size; - ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); - assert_int_equal(ret, -EINVAL); - - /* Reject a blob with a truncated coefficient header. */ - copy_eq_fir_blob(blob_copy); - abi = (struct sof_abi_hdr *)blob_copy; - config = (struct sof_eq_fir_config *)abi->data; - abi->size = sizeof(*config) + 2 * sizeof(int16_t) + - (SOF_FIR_COEF_NHEADER - 1) * sizeof(int16_t); - config->size = abi->size; - ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); - assert_int_equal(ret, -EINVAL); - - /* Reject a blob smaller than the configuration header. */ - copy_eq_fir_blob(blob_copy); - abi = (struct sof_abi_hdr *)blob_copy; - abi->size = sizeof(struct sof_eq_fir_config) - 1; - ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); - assert_int_equal(ret, -EINVAL); - - /* Reject a blob larger than the maximum configuration size. */ - copy_eq_fir_blob(blob_copy); - abi = (struct sof_abi_hdr *)blob_copy; - abi->size = SOF_EQ_FIR_MAX_SIZE + 1; - ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); - assert_int_equal(ret, -EINVAL); - - /* Reject mismatched ABI and configuration payload sizes. */ - copy_eq_fir_blob(blob_copy); - abi = (struct sof_abi_hdr *)blob_copy; - config = (struct sof_eq_fir_config *)abi->data; - config->size = abi->size - sizeof(int16_t); - ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); - assert_int_equal(ret, -EINVAL); - - /* Reject an odd channel count that misaligns coefficient data. */ - copy_eq_fir_blob(blob_copy); - abi = (struct sof_abi_hdr *)blob_copy; - config = (struct sof_eq_fir_config *)abi->data; - config->channels_in_config = 1; - ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); - assert_int_equal(ret, -EINVAL); - - /* Reject more responses than the configuration format supports. */ - copy_eq_fir_blob(blob_copy); - abi = (struct sof_abi_hdr *)blob_copy; - config = (struct sof_eq_fir_config *)abi->data; - config->number_of_responses = SOF_EQ_FIR_MAX_RESPONSES + 1; - ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); - assert_int_equal(ret, -EINVAL); - - /* Reject an empty FIR response. */ - copy_eq_fir_blob(blob_copy); - abi = (struct sof_abi_hdr *)blob_copy; - config = (struct sof_eq_fir_config *)abi->data; - coef = (struct sof_fir_coef_data *)&config->data[config->channels_in_config]; - coef->length = 0; - ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); - assert_int_equal(ret, -EINVAL); - - /* Reject a filter length that is not a multiple of four. */ - copy_eq_fir_blob(blob_copy); - abi = (struct sof_abi_hdr *)blob_copy; - config = (struct sof_eq_fir_config *)abi->data; - coef = (struct sof_fir_coef_data *)&config->data[config->channels_in_config]; - coef->length = 3; - ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); - assert_int_equal(ret, -EINVAL); - - /* Reject a response whose coefficients extend past the blob. */ - copy_eq_fir_blob(blob_copy); - abi = (struct sof_abi_hdr *)blob_copy; - config = (struct sof_eq_fir_config *)abi->data; - coef = (struct sof_fir_coef_data *)&config->data[config->channels_in_config]; - coef->length = SOF_FIR_MAX_LENGTH; - ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); - assert_int_equal(ret, -EINVAL); - - /* Reject a channel assignment that references an undefined response. */ - copy_eq_fir_blob(blob_copy); - abi = (struct sof_abi_hdr *)blob_copy; - config = (struct sof_eq_fir_config *)abi->data; - config->data[0] = 1; - ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); - assert_int_equal(ret, -EINVAL); -} - -#if CONFIG_IPC_MAJOR_3 -static void test_eq_fir_rejects_invalid_frame_format(void **state) -{ - struct test_data *td = *state; - struct processing_module *mod = comp_mod(td->dev); - - assert_int_equal(set_fir_func(mod, (enum sof_ipc_frame)0xffff), -EINVAL); -} -#endif - -static size_t frames_jitter(size_t frames) -{ - int r = rand(); - - if (r > THR_RAND_PLUS_ONE) - return frames + 1; - else if (r < THR_RAND_MINUS_ONE) - return frames - 1; - else - return frames; -} - -static void test_audio_eq_fir(void **state) -{ - struct test_data *td = *state; - struct processing_module *mod = comp_mod(td->dev); - - struct comp_buffer *source = td->source; - struct comp_buffer *sink = td->sink; - struct sof_source *sources[1]; - struct sof_sink *sinks[1]; - size_t avail_before; - size_t frames; - int ret; - - sources[0] = audio_buffer_get_source(&source->audio_buffer); - sinks[0] = audio_buffer_get_sink(&sink->audio_buffer); - - while (td->continue_loop) { - frames = frames_jitter(td->params->frames); - switch (audio_stream_get_frm_fmt(&source->stream)) { - case SOF_IPC_FRAME_S16_LE: - fill_source_s16(td, frames); - break; - case SOF_IPC_FRAME_S24_4LE: - fill_source_s24(td, frames); - break; - case SOF_IPC_FRAME_S32_LE: - fill_source_s32(td, frames); - break; - case SOF_IPC_FRAME_S24_3LE: - break; - default: - assert(0); - break; - } - - td->dev->frames = td->input_frames; - avail_before = audio_stream_get_avail_bytes(&sink->stream); - ret = module_process_sink_src(mod, sources, 1, sinks, 1); - assert_int_equal(ret, 0); - - td->output_bytes = audio_stream_get_avail_bytes(&sink->stream) - avail_before; - - switch (audio_stream_get_frm_fmt(&sink->stream)) { - case SOF_IPC_FRAME_S16_LE: - verify_sink_s16(td); - break; - case SOF_IPC_FRAME_S24_4LE: - verify_sink_s24(td); - break; - case SOF_IPC_FRAME_S32_LE: - verify_sink_s32(td); - break; - default: - assert(0); - break; - } - - comp_update_buffer_consume(sink, td->output_bytes); - } -} - -static struct test_parameters parameters[] = { -#if CONFIG_FORMAT_S16LE - { 2, 48, 2, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE }, -#endif /* CONFIG_FORMAT_S16LE */ -#if CONFIG_FORMAT_S24LE - { 2, 48, 2, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE }, -#endif /* CONFIG_FORMAT_S24LE */ - -#if CONFIG_FORMAT_S32LE - { 2, 48, 2, SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S32_LE }, -#endif /* CONFIG_FORMAT_S32LE */ -}; - -int main(void) -{ - int ret; - int i; - -#if CONFIG_IPC_MAJOR_3 - struct CMUnitTest tests[ARRAY_SIZE(parameters) + 3]; -#else - struct CMUnitTest tests[ARRAY_SIZE(parameters) + 2]; -#endif - - for (i = 0; i < ARRAY_SIZE(parameters); i++) { - tests[i].name = "test_audio_eq_fir"; - tests[i].test_func = test_audio_eq_fir; - tests[i].setup_func = setup; - tests[i].teardown_func = teardown; - tests[i].initial_state = ¶meters[i]; - } - - i = ARRAY_SIZE(parameters); - tests[i].name = "test_eq_fir_rounds_down_odd_frame_count"; - tests[i].test_func = test_eq_fir_rounds_down_odd_frame_count; - tests[i].setup_func = setup; - tests[i].teardown_func = teardown; - tests[i].initial_state = ¶meters[0]; - i++; - - tests[i].name = "test_eq_fir_rejects_invalid_configurations"; - tests[i].test_func = test_eq_fir_rejects_invalid_configurations; - tests[i].setup_func = setup; - tests[i].teardown_func = teardown; - tests[i].initial_state = ¶meters[0]; - i++; - -#if CONFIG_IPC_MAJOR_3 - tests[i].name = "test_eq_fir_rejects_invalid_frame_format"; - tests[i].test_func = test_eq_fir_rejects_invalid_frame_format; - tests[i].setup_func = setup; - tests[i].teardown_func = teardown; - tests[i].initial_state = ¶meters[0]; - i++; -#endif - - cmocka_set_message_output(CM_OUTPUT_TAP); - -#ifdef DEBUG_FILES - debug_fh_16 = fopen("fir_test_16.txt", "w"); - debug_fh_24 = fopen("fir_test_24.txt", "w"); - debug_fh_32 = fopen("fir_test_32.txt", "w"); -#endif - ret = cmocka_run_group_tests(tests, setup_group, NULL); - -#ifdef DEBUG_FILES - fclose(debug_fh_16); - fclose(debug_fh_24); - fclose(debug_fh_32); -#endif - return ret; -} diff --git a/test/cmocka/src/audio/eq_iir/CMakeLists.txt b/test/cmocka/src/audio/eq_iir/CMakeLists.txt deleted file mode 100644 index 53a550b6b6cf..000000000000 --- a/test/cmocka/src/audio/eq_iir/CMakeLists.txt +++ /dev/null @@ -1,57 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -cmocka_test(eq_iir_process - eq_iir_process.c -) - -target_include_directories(eq_iir_process PRIVATE ${PROJECT_SOURCE_DIR}/src/audio) - -# make small version of libaudio so we don't have to care -# about unused missing references - -add_compile_options(-DUNIT_TEST) - -add_library(audio_for_eq_iir STATIC - ${PROJECT_SOURCE_DIR}/src/audio/eq_iir/eq_iir.c - ${PROJECT_SOURCE_DIR}/src/audio/eq_iir/eq_iir_generic.c - ${PROJECT_SOURCE_DIR}/src/audio/eq_iir/eq_iir_ipc3.c - ${PROJECT_SOURCE_DIR}/src/math/iir_df1.c - ${PROJECT_SOURCE_DIR}/src/math/iir_df1_float.c - ${PROJECT_SOURCE_DIR}/src/math/iir_df1_generic.c - ${PROJECT_SOURCE_DIR}/src/math/iir_df1_hifi3.c - ${PROJECT_SOURCE_DIR}/src/math/iir_df1_hifi4.c - ${PROJECT_SOURCE_DIR}/src/math/iir_df1_hifi5.c - ${PROJECT_SOURCE_DIR}/src/math/iir_df2t.c - ${PROJECT_SOURCE_DIR}/src/math/iir_df2t_generic.c - ${PROJECT_SOURCE_DIR}/src/math/iir_df2t_hifi3.c - ${PROJECT_SOURCE_DIR}/src/math/numbers.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter_ipc3.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module/generic.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/comp_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/audio_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c - ${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c - ${PROJECT_SOURCE_DIR}/src/audio/component.c - ${PROJECT_SOURCE_DIR}/src/audio/data_blob.c - ${PROJECT_SOURCE_DIR}/src/module/audio/source_api.c - ${PROJECT_SOURCE_DIR}/src/module/audio/sink_api.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc3/helper.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc-common-kernel.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c - ${PROJECT_SOURCE_DIR}/src/lib/objpool.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-schedule.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-stream.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-xrun.c -) -sof_append_relative_path_definitions(audio_for_eq_iir) - -target_link_libraries(audio_for_eq_iir PRIVATE sof_options) - -target_link_libraries(eq_iir_process PRIVATE audio_for_eq_iir) diff --git a/test/cmocka/src/audio/eq_iir/eq_iir_process.c b/test/cmocka/src/audio/eq_iir/eq_iir_process.c deleted file mode 100644 index 4c1196d8a163..000000000000 --- a/test/cmocka/src/audio/eq_iir/eq_iir_process.c +++ /dev/null @@ -1,549 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2021 Intel Corporation. All rights reserved. -// -// Author: Seppo Ingalsuo - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "../../util.h" -#include "../../../include/cmocka_chirp_2ch.h" -#include "cmocka_chirp_iir_ref_2ch.h" -#include "cmocka_iir_coef_2ch.h" - -/* Allow some small error for fixed point. In IIR case due to float - * reference with float coefficients the difference can be quite - * large to scaled integer bi-quads. This could be re-visited with - * a more implementation like reference in Octave test vector - * generate script. - */ -#define ERROR_TOLERANCE_S16 2 -#define ERROR_TOLERANCE_S24 128 -#define ERROR_TOLERANCE_S32 32768 - -/* Thresholds for frames count jitter for rand() function */ -#define THR_RAND_PLUS_ONE ((RAND_MAX >> 1) + (RAND_MAX >> 2)) -#define THR_RAND_MINUS_ONE ((RAND_MAX >> 1) - (RAND_MAX >> 2)) - -struct buffer_fill { - int idx; -} buffer_fill_data; - -struct buffer_verify { - int idx; -} buffer_verify_data; - -struct test_parameters { - uint32_t channels; - uint32_t frames; - uint32_t buffer_size_mult; - uint32_t source_format; - uint32_t sink_format; -}; - -struct test_data { - struct comp_dev *dev; - struct comp_buffer *sink; - struct comp_buffer *source; - struct test_parameters *params; - struct processing_module *mod; - bool continue_loop; - size_t input_frames; - size_t output_bytes; -}; - -static int setup_group(void **state) -{ - sys_comp_init(sof_get()); - sys_comp_module_eq_iir_interface_init(); - return 0; -} - -static struct sof_ipc_comp_process *create_eq_iir_comp_ipc(struct test_data *td) -{ - struct sof_ipc_comp_process *ipc; - size_t ipc_size = sizeof(struct sof_ipc_comp_process); - const struct sof_uuid uuid = SOF_REG_UUID(eq_iir); - - ipc = calloc(1, ipc_size + SOF_UUID_SIZE); - memcpy_s(ipc + 1, SOF_UUID_SIZE, &uuid, SOF_UUID_SIZE); - ipc->comp.hdr.size = ipc_size + SOF_UUID_SIZE; - ipc->comp.type = SOF_COMP_MODULE_ADAPTER; - ipc->config.hdr.size = sizeof(struct sof_ipc_comp_config); - ipc->size = 0; - ipc->comp.ext_data_length = SOF_UUID_SIZE; - return ipc; -} - -static int eq_iir_send_config(struct processing_module *mod) -{ - const struct module_interface *const ops = mod->dev->drv->adapter_ops; - struct sof_abi_hdr *blob = (struct sof_abi_hdr *)iir_coef_2ch; - size_t cdata_size = sizeof(struct sof_ipc_ctrl_data) + sizeof(struct sof_abi_hdr) + - blob->size; - struct sof_ipc_ctrl_data *cdata; - int ret; - - cdata = calloc(1, cdata_size); - if (!cdata) - return -ENOMEM; - - cdata->cmd = SOF_CTRL_CMD_BINARY; - cdata->num_elems = blob->size; - cdata->data[0].magic = blob->magic; - cdata->data[0].type = blob->type; - cdata->data[0].size = blob->size; - cdata->data[0].abi = blob->abi; - memcpy_s(cdata->data[0].data, blob->size, blob->data, blob->size); - - ret = ops->set_configuration(mod, 0, MODULE_CFG_FRAGMENT_SINGLE, - blob->size, (const uint8_t *)cdata, - blob->size, NULL, 0); - - free(cdata); - return ret; -} - -static void prepare_sink(struct test_data *td, struct processing_module *mod) -{ - struct test_parameters *parameters = td->params; - struct module_data *md = &mod->priv; - size_t size; - size_t free; - - /* allocate new sink buffer */ - size = parameters->frames * get_frame_bytes(parameters->sink_format, parameters->channels) * - parameters->buffer_size_mult; - - md->mpd.out_buff_size = parameters->frames * get_frame_bytes(parameters->sink_format, - parameters->channels); - - td->sink = create_test_sink(td->dev, 0, parameters->sink_format, - parameters->channels, size); - free = audio_stream_get_free_bytes(&td->sink->stream); - assert_int_equal(free, size); -} - -static void prepare_source(struct test_data *td, struct processing_module *mod) -{ - struct test_parameters *parameters = td->params; - struct module_data *md = &mod->priv; - size_t size; - size_t free; - - md->mpd.in_buff_size = parameters->frames * get_frame_bytes(parameters->source_format, - parameters->channels); - - size = parameters->frames * get_frame_bytes(parameters->source_format, - parameters->channels) * parameters->buffer_size_mult; - - td->source = create_test_source(td->dev, 0, parameters->source_format, - parameters->channels, size); - free = audio_stream_get_free_bytes(&td->source->stream); - assert_int_equal(free, size); -} - -static int setup(void **state) -{ - struct test_parameters *params = *state; - struct processing_module *mod; - struct test_data *td; - struct sof_ipc_comp_process *ipc; - struct comp_dev *dev; - struct sof_source *sources[1]; - struct sof_sink *sinks[1]; - int ret; - - td = test_malloc(sizeof(*td)); - if (!td) - return -EINVAL; - - td->params = test_malloc(sizeof(*params)); - if (!td->params) - return -EINVAL; - - memcpy_s(td->params, sizeof(*td->params), params, sizeof(*params)); - ipc = create_eq_iir_comp_ipc(td); - buffer_fill_data.idx = 0; - buffer_verify_data.idx = 0; - - dev = comp_new((struct sof_ipc_comp *)ipc); - free(ipc); - if (!dev) - return -EINVAL; - - td->dev = dev; - dev->frames = params->frames; - mod = comp_mod(dev); - - ret = eq_iir_send_config(mod); - if (ret) - return ret; - - prepare_sink(td, mod); - prepare_source(td, mod); - - mod->stream_params = test_malloc(sizeof(struct sof_ipc_stream_params)); - mod->stream_params->channels = params->channels; - mod->period_bytes = get_frame_bytes(params->source_format, params->channels) * 48000 / 1000; - - sources[0] = audio_buffer_get_source(&td->source->audio_buffer); - sinks[0] = audio_buffer_get_sink(&td->sink->audio_buffer); - ret = module_prepare(mod, sources, 1, sinks, 1); - if (ret) - return ret; - - td->continue_loop = true; - - *state = td; - return 0; -} - -static int teardown(void **state) -{ - struct test_data *td = *state; - struct processing_module *mod = comp_mod(td->dev); - - test_free(mod->stream_params); - mod->stream_params = NULL; - test_free(td->params); - free_test_source(td->source); - free_test_sink(td->sink); - comp_free(td->dev); - test_free(td); - return 0; -} - -#if CONFIG_FORMAT_S16LE -static void fill_source_s16(struct test_data *td, int frames_max) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int16_t *x; - int bytes_total; - int samples; - int frames; - int i; - int samples_processed = 0; - - sb = comp_dev_get_first_data_producer(dev); - ss = &sb->stream; - frames = MIN(audio_stream_get_free_frames(ss), frames_max); - samples = frames * audio_stream_get_channels(ss); - for (i = 0; i < samples; i++) { - x = audio_stream_write_frag_s16(ss, i); - *x = sat_int16(Q_SHIFT_RND(chirp_2ch[buffer_fill_data.idx++], 31, 15)); - samples_processed++; - if (buffer_fill_data.idx == CHIRP_2CH_LENGTH) { - td->continue_loop = false; - break; - } - } - - if (samples_processed > 0) { - bytes_total = samples_processed * audio_stream_sample_bytes(ss); - comp_update_buffer_produce(sb, bytes_total); - } - - td->input_frames = samples_processed / audio_stream_get_channels(ss); -} - -static void verify_sink_s16(struct test_data *td) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int32_t delta; - int32_t ref; - int32_t out; - int16_t *x; - int samples; - int i; - - sb = comp_dev_get_first_data_consumer(dev); - ss = &sb->stream; - samples = td->output_bytes >> 1; - for (i = 0; i < samples; i++) { - x = audio_stream_read_frag_s16(ss, i); - out = *x; - ref = sat_int16(Q_SHIFT_RND(chirp_iir_ref_2ch[buffer_verify_data.idx++], 31, 15)); - delta = ref - out; - if (delta > ERROR_TOLERANCE_S16 || delta < -ERROR_TOLERANCE_S16) - assert_int_equal(out, ref); - } -} -#endif /* CONFIG_FORMAT_S16LE */ - -#if CONFIG_FORMAT_S24LE -static void fill_source_s24(struct test_data *td, int frames_max) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int32_t *x; - int bytes_total; - int samples; - int frames; - int i; - int samples_processed = 0; - - sb = comp_dev_get_first_data_producer(dev); - ss = &sb->stream; - frames = MIN(audio_stream_get_free_frames(ss), frames_max); - samples = frames * audio_stream_get_channels(ss); - for (i = 0; i < samples; i++) { - x = audio_stream_write_frag_s32(ss, i); - *x = sat_int24(Q_SHIFT_RND(chirp_2ch[buffer_fill_data.idx++], 31, 23)); - samples_processed++; - if (buffer_fill_data.idx == CHIRP_2CH_LENGTH) { - td->continue_loop = false; - break; - } - } - - if (samples_processed > 0) { - bytes_total = samples_processed * audio_stream_sample_bytes(ss); - comp_update_buffer_produce(sb, bytes_total); - } - - td->input_frames = samples_processed / audio_stream_get_channels(ss); -} - -static void verify_sink_s24(struct test_data *td) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int32_t delta; - int32_t ref; - int32_t out; - int32_t *x; - int samples; - int i; - - sb = comp_dev_get_first_data_consumer(dev); - ss = &sb->stream; - samples = td->output_bytes >> 2; - for (i = 0; i < samples; i++) { - x = audio_stream_read_frag_s32(ss, i); - out = (*x << 8) >> 8; /* Make sure there's no 24 bit overflow */ - ref = sat_int24(Q_SHIFT_RND(chirp_iir_ref_2ch[buffer_verify_data.idx++], 31, 23)); - delta = ref - out; - if (delta > ERROR_TOLERANCE_S24 || delta < -ERROR_TOLERANCE_S24) - assert_int_equal(out, ref); - } -} -#endif /* CONFIG_FORMAT_S24LE */ - -#if CONFIG_FORMAT_S32LE -static void fill_source_s32(struct test_data *td, int frames_max) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int32_t *x; - int bytes_total; - int samples; - int frames; - int i; - int samples_processed = 0; - - sb = comp_dev_get_first_data_producer(dev); - ss = &sb->stream; - frames = MIN(audio_stream_get_free_frames(ss), frames_max); - samples = frames * audio_stream_get_channels(ss); - for (i = 0; i < samples; i++) { - x = audio_stream_write_frag_s32(ss, i); - *x = chirp_2ch[buffer_fill_data.idx++]; - samples_processed++; - if (buffer_fill_data.idx == CHIRP_2CH_LENGTH) { - td->continue_loop = false; - break; - } - } - - if (samples_processed > 0) { - bytes_total = samples_processed * audio_stream_sample_bytes(ss); - comp_update_buffer_produce(sb, bytes_total); - } - - td->input_frames = samples_processed / audio_stream_get_channels(ss); -} - -static void verify_sink_s32(struct test_data *td) -{ - struct comp_dev *dev = td->dev; - struct comp_buffer *sb; - struct audio_stream *ss; - int64_t delta; - int32_t ref; - int32_t out; - int32_t *x; - int samples; - int i; - - sb = comp_dev_get_first_data_consumer(dev); - ss = &sb->stream; - samples = td->output_bytes >> 2; - for (i = 0; i < samples; i++) { - x = audio_stream_read_frag_s32(ss, i); - out = *x; - ref = chirp_iir_ref_2ch[buffer_verify_data.idx++]; - delta = (int64_t)ref - (int64_t)out; - if (delta > ERROR_TOLERANCE_S32 || delta < -ERROR_TOLERANCE_S32) - assert_int_equal(out, ref); - } -} -#endif /* CONFIG_FORMAT_S32LE */ - -static int frames_jitter(int frames) -{ - int r = rand(); - - if (r > THR_RAND_PLUS_ONE) - return frames + 1; - else if (r < THR_RAND_MINUS_ONE) - return frames - 1; - else - return frames; -} - -static void test_audio_eq_iir(void **state) -{ - struct test_data *td = *state; - struct processing_module *mod = comp_mod(td->dev); - - struct comp_buffer *source = td->source; - struct comp_buffer *sink = td->sink; - struct sof_source *sources[1]; - struct sof_sink *sinks[1]; - size_t sink_avail_before; - int ret; - int frames; - - sources[0] = audio_buffer_get_source(&source->audio_buffer); - sinks[0] = audio_buffer_get_sink(&sink->audio_buffer); - - while (td->continue_loop) { - frames = frames_jitter(td->params->frames); - switch (audio_stream_get_frm_fmt(&source->stream)) { - case SOF_IPC_FRAME_S16_LE: - fill_source_s16(td, frames); - break; - case SOF_IPC_FRAME_S24_4LE: - fill_source_s24(td, frames); - break; - case SOF_IPC_FRAME_S32_LE: - fill_source_s32(td, frames); - break; - case SOF_IPC_FRAME_S24_3LE: - break; - default: - assert(0); - break; - } - - sink_avail_before = audio_stream_get_avail_bytes(&sink->stream); - ret = module_process_sink_src(mod, sources, 1, sinks, 1); - assert_int_equal(ret, 0); - - td->output_bytes = audio_stream_get_avail_bytes(&sink->stream) - sink_avail_before; - - switch (audio_stream_get_frm_fmt(&sink->stream)) { - case SOF_IPC_FRAME_S16_LE: - verify_sink_s16(td); - break; - case SOF_IPC_FRAME_S24_4LE: - verify_sink_s24(td); - break; - case SOF_IPC_FRAME_S32_LE: - verify_sink_s32(td); - break; - default: - assert(0); - break; - } - - comp_update_buffer_consume(sink, td->output_bytes); - } -} - -static struct test_parameters parameters[] = { -#if CONFIG_FORMAT_S16LE - { 2, 48, 2, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE }, -#endif /* CONFIG_FORMAT_S16LE */ -#if CONFIG_FORMAT_S24LE - { 2, 48, 2, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE }, -#endif /* CONFIG_FORMAT_S24LE */ - -#if CONFIG_FORMAT_S32LE - { 2, 48, 2, SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S32_LE }, -#endif /* CONFIG_FORMAT_S32LE */ - -#if CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S16LE - { 2, 48, 2, SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S16_LE }, -#endif /* CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S16LE */ - -#if CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S24LE - { 2, 48, 2, SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S24_4LE }, -#endif /* CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S24LE */ - -}; - -static void test_iir_delay_size_df1_series(void **state) -{ - struct sof_eq_iir_header hdr = { .num_sections = 11 }; - const size_t size = 4 * 11 * sizeof(int32_t); - - (void)state; - - hdr.num_sections_in_series = 11; - assert_int_equal(iir_delay_size_df1(&hdr), size); - - hdr.num_sections_in_series = 1; - assert_int_equal(iir_delay_size_df1(&hdr), size); - - /* iir_df1() would run 20 biquads against 11 sections of state */ - hdr.num_sections_in_series = 10; - assert_int_equal(iir_delay_size_df1(&hdr), 0); -} - -int main(void) -{ - int i, ret; - - struct CMUnitTest tests[ARRAY_SIZE(parameters)]; - const struct CMUnitTest validator_tests[] = { - cmocka_unit_test(test_iir_delay_size_df1_series), - }; - - for (i = 0; i < ARRAY_SIZE(parameters); i++) { - tests[i].name = "test_audio_eq_iir"; - tests[i].test_func = test_audio_eq_iir; - tests[i].setup_func = setup; - tests[i].teardown_func = teardown; - tests[i].initial_state = ¶meters[i]; - } - - cmocka_set_message_output(CM_OUTPUT_TAP); - - ret = cmocka_run_group_tests(tests, setup_group, NULL); - ret += cmocka_run_group_tests(validator_tests, NULL, NULL); - - return ret; -} diff --git a/test/cmocka/src/audio/mixer/CMakeLists.txt b/test/cmocka/src/audio/mixer/CMakeLists.txt deleted file mode 100644 index b0b086816e3b..000000000000 --- a/test/cmocka/src/audio/mixer/CMakeLists.txt +++ /dev/null @@ -1,37 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -cmocka_test(mixer - mixer_test.c - ../module_adapter_test.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c - ${PROJECT_SOURCE_DIR}/src/audio/mixer/mixer.c - ${PROJECT_SOURCE_DIR}/src/audio/mixer/mixer_generic.c - ${PROJECT_SOURCE_DIR}/src/audio/mixer/mixer_hifi3.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc3/helper.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc-common-kernel.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c - ${PROJECT_SOURCE_DIR}/src/lib/objpool.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter_ipc3.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module/generic.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/comp_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/audio_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c - ${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c - ${PROJECT_SOURCE_DIR}/src/audio/component.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-schedule.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-stream.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-xrun.c - ${PROJECT_SOURCE_DIR}/src/audio/data_blob.c - ${PROJECT_SOURCE_DIR}/src/module/audio/source_api.c - ${PROJECT_SOURCE_DIR}/src/module/audio/sink_api.c - ${PROJECT_SOURCE_DIR}/src/math/numbers.c -) - -target_include_directories(mixer PRIVATE ${PROJECT_SOURCE_DIR}/src/audio) -target_link_libraries(mixer PRIVATE -lm) diff --git a/test/cmocka/src/audio/mixer/comp_mock.c b/test/cmocka/src/audio/mixer/comp_mock.c deleted file mode 100644 index 3016c6b86afc..000000000000 --- a/test/cmocka/src/audio/mixer/comp_mock.c +++ /dev/null @@ -1,83 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2018 Intel Corporation. All rights reserved. -// -// Author: Slawomir Blauciak - -#include -#include -#include -#include -#include - -#ifdef HAVE_MALLOC_H -#include -#else -#include -#endif - -#include "comp_mock.h" - -static struct comp_dev *mock_comp_new(const struct comp_driver *drv, - const struct comp_ipc_config *config, - const void *spec) -{ - struct comp_dev *dev = comp_alloc(drv, sizeof(*dev)); - - return dev; -} - -static void mock_comp_free(struct comp_dev *dev) -{ - free(dev); -} - -static int mock_comp_params(struct comp_dev *dev, - struct sof_ipc_stream_params *params) -{ - return 0; -} - -static int mock_comp_cmd(struct comp_dev *dev, int cmd, void *data, - int max_data_size) -{ - return 0; -} - -static int mock_comp_copy(struct comp_dev *dev) -{ - return 0; -} - -static int mock_comp_reset(struct comp_dev *dev) -{ - return 0; -} - -static int mock_comp_prepare(struct comp_dev *dev) -{ - return 0; -} - -static const struct comp_driver comp_mock = { - .type = SOF_COMP_MOCK, - .ops = { - .create = mock_comp_new, - .free = mock_comp_free, - .params = mock_comp_params, - .cmd = mock_comp_cmd, - .copy = mock_comp_copy, - .prepare = mock_comp_prepare, - .reset = mock_comp_reset, - }, -}; - -static struct comp_driver_info comp_mock_info = { - .drv = &comp_mock, -}; - -void sys_comp_mock_init(void) -{ - mock_comp_register(&comp_mock_info); -} - diff --git a/test/cmocka/src/audio/mixer/comp_mock.h b/test/cmocka/src/audio/mixer/comp_mock.h deleted file mode 100644 index 9a83c5a84c24..000000000000 --- a/test/cmocka/src/audio/mixer/comp_mock.h +++ /dev/null @@ -1,14 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause - * - * Copyright(c) 2018 Intel Corporation. All rights reserved. - * - * Author: Slawomir Blauciak - */ - -#include -#include - -#define SOF_COMP_MOCK ((uint32_t)-1) - -void sys_comp_mock_init(void); -int mock_comp_register(struct comp_driver_info *info); diff --git a/test/cmocka/src/audio/mixer/mixer_test.c b/test/cmocka/src/audio/mixer/mixer_test.c deleted file mode 100644 index a5c3bbbce620..000000000000 --- a/test/cmocka/src/audio/mixer/mixer_test.c +++ /dev/null @@ -1,180 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2018 Intel Corporation. All rights reserved. -// -// Author: Slawomir Blauciak - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "mixer/mixer.h" -#include "../module_adapter.h" - -#ifdef HAVE_MALLOC_H -#include -#else -#include -#endif - -#include "comp_mock.h" - -#define MIX_TEST_SAMPLES 32 - -struct mix_test_case { - int num_sources; - int num_chans; - const char *name; -}; - -#define TEST_CASE(_num_sources, _num_chans) \ - { \ - .num_sources = (_num_sources), \ - .num_chans = (_num_chans), \ - .name = ("test_audio_mixer_copy_" \ - #_num_sources "_srcs_" \ - #_num_chans "ch"), \ - } - -static struct mix_test_case mix_test_cases[] = { - TEST_CASE(1, 2), - TEST_CASE(1, 4), - TEST_CASE(1, 8), - TEST_CASE(2, 2), - TEST_CASE(2, 4), - TEST_CASE(2, 8), - TEST_CASE(3, 2), - TEST_CASE(4, 2), - TEST_CASE(6, 2), - TEST_CASE(8, 2) -}; - -static int test_setup(void **state) -{ - struct mix_test_case *tc = *((struct mix_test_case **)state); - struct processing_module_test_parameters test_parameters = { - tc->num_chans, 48, 1, SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S32_LE, NULL - }; - struct processing_module_test_data *test_data; - struct module_data *mod_data; - struct mixer_data *md; - - - /* allocate and set new processing module, device and mixer data */ - test_data = test_malloc(sizeof(*test_data)); - test_data->parameters = test_parameters; - test_data->num_sources = tc->num_sources; - test_data->num_sinks = 1; - module_adapter_test_setup(test_data); - - mod_data = &test_data->mod->priv; - md = test_malloc(sizeof(*md)); - mod_data->private = md; - - md->mix_func = mixer_get_processing_function(test_data->mod->dev, - audio_stream_get_frm_fmt(&test_data->sinks[0]->stream)); - - *state = test_data; - - return 0; -} - -static int test_teardown(void **state) -{ - struct processing_module_test_data *test_data = *state; - struct mixer_data *md = module_get_private_data(test_data->mod); - - test_free(md); - module_adapter_test_free(test_data); - test_free(test_data); - - return 0; -} - -static void test_audio_mixer_copy(void **state) -{ - int src_idx; - int smp; - struct processing_module_test_data *tc = *state; - struct processing_module *mod = tc->mod; - struct mixer_data *md = module_get_private_data(tc->mod); - struct cir_buf_source sources_buf[CONFIG_PLATFORM_MAX_STREAMS]; - struct cir_buf_sink sink_buf; - - for (src_idx = 0; src_idx < tc->num_sources; ++src_idx) { - uint32_t *samples = tc->sources[src_idx]->stream.addr; - - for (smp = 0; smp < tc->sources[src_idx]->stream.size / sizeof(int32_t); ++smp) { - double rad = M_PI / (180.0 / (smp * (src_idx + 1))); - - samples[smp] = ((sin(rad) + 1) / 2) * (0xFFFFFFFF / 2); - } - - audio_stream_produce(&tc->sources[src_idx]->stream, - tc->sources[src_idx]->stream.size / sizeof(int32_t)); - - sources_buf[src_idx].buf_start = - audio_stream_get_addr(&tc->sources[src_idx]->stream); - sources_buf[src_idx].buf_end = - audio_stream_get_end_addr(&tc->sources[src_idx]->stream); - sources_buf[src_idx].ptr = - audio_stream_get_rptr(&tc->sources[src_idx]->stream); - } - - sink_buf.buf_start = audio_stream_get_addr(&tc->sinks[0]->stream); - sink_buf.buf_end = audio_stream_get_end_addr(&tc->sinks[0]->stream); - sink_buf.ptr = audio_stream_get_wptr(&tc->sinks[0]->stream); - - md->mix_func(&sink_buf, sources_buf, tc->num_sources, - mod->dev->frames * tc->parameters.channels); - - for (smp = 0; smp < tc->sinks[0]->stream.size / sizeof(int32_t); ++smp) { - uint64_t sum = 0; - - for (src_idx = 0; src_idx < tc->num_sources; ++src_idx) { - assert_non_null(tc->sources[src_idx]); - - uint32_t *samples = tc->sources[src_idx]->stream.addr; - - sum += samples[smp]; - } - - sum = sat_int32(sum); - - uint32_t *out_samples = tc->sinks[0]->stream.addr; - - assert_int_equal(out_samples[smp], sum); - } -} - -int main(void) -{ - struct CMUnitTest tests[ARRAY_SIZE(mix_test_cases)]; - - int i; - int cur_test_case = 0; - - for (i = 0; i < ARRAY_SIZE(tests); (++i, ++cur_test_case)) { - tests[i].test_func = test_audio_mixer_copy; - tests[i].initial_state = &mix_test_cases[cur_test_case]; - tests[i].setup_func = test_setup; - tests[i].teardown_func = test_teardown; - tests[i].name = mix_test_cases[cur_test_case].name; - } - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test/cmocka/src/audio/module_adapter.h b/test/cmocka/src/audio/module_adapter.h deleted file mode 100644 index deb8c49ba8e7..000000000000 --- a/test/cmocka/src/audio/module_adapter.h +++ /dev/null @@ -1,28 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2022 Intel Corporation. All rights reserved. -// - -struct processing_module_test_parameters { - uint32_t channels; - uint32_t frames; - uint32_t buffer_size_ms; - uint32_t source_format; - uint32_t sink_format; - void (*verify)(struct processing_module *mod, struct comp_buffer *sink, - struct comp_buffer *source); -}; - -struct processing_module_test_data { - struct processing_module *mod; - struct comp_buffer **sinks; - struct comp_buffer **sources; - uint32_t num_sources; - uint32_t num_sinks; - struct processing_module_test_parameters parameters; - void (*verify)(struct processing_module *mod, struct comp_buffer *sink, - struct comp_buffer *source); -}; - -int module_adapter_test_setup(struct processing_module_test_data *test_data); -void module_adapter_test_free(struct processing_module_test_data *test_data); diff --git a/test/cmocka/src/audio/module_adapter_test.c b/test/cmocka/src/audio/module_adapter_test.c deleted file mode 100644 index 798c3545f97f..000000000000 --- a/test/cmocka/src/audio/module_adapter_test.c +++ /dev/null @@ -1,73 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2020 Intel Corporation. All rights reserved. -// - -#include "../util.h" - -#include -#include -#include -#include -#include -#include -#include -#include "module_adapter.h" - -int module_adapter_test_setup(struct processing_module_test_data *test_data) -{ - struct processing_module_test_parameters *parameters = &test_data->parameters; - struct processing_module *mod; - struct comp_dev *dev; - uint32_t size; - int i; - - /* allocate and set new device */ - mod = test_malloc(sizeof(struct processing_module)); - test_data->mod = mod; - dev = test_malloc(sizeof(struct comp_dev)); - dev->frames = parameters->frames; - mod->dev = dev; - dev->mod = mod; - - test_data->sinks = test_calloc(test_data->num_sinks, sizeof(struct comp_buffer *)); - test_data->sources = test_calloc(test_data->num_sources, sizeof(struct comp_buffer *)); - - list_init(&dev->bsource_list); - list_init(&dev->bsink_list); - - /* allocate sink buffers */ - size = parameters->frames * - get_frame_bytes(parameters->sink_format, parameters->channels) * - parameters->buffer_size_ms; - for (i = 0; i < test_data->num_sinks; i++) - test_data->sinks[i] = create_test_sink(dev, 0, parameters->sink_format, - parameters->channels, size); - - /* allocate source buffers */ - size = parameters->frames * get_frame_bytes(parameters->source_format, - parameters->channels) * parameters->buffer_size_ms; - for (i = 0; i < test_data->num_sources; i++) - test_data->sources[i] = create_test_source(dev, 0, parameters->source_format, - parameters->channels, size); - - test_data->verify = parameters->verify; - - return 0; -} - -void module_adapter_test_free(struct processing_module_test_data *test_data) -{ - int i; - - for (i = 0; i < test_data->num_sinks; i++) - free_test_sink(test_data->sinks[i]); - - for (i = 0; i < test_data->num_sources; i++) - free_test_source(test_data->sources[i]); - - test_free(test_data->sinks); - test_free(test_data->sources); - test_free(test_data->mod->dev); - test_free(test_data->mod); -} diff --git a/test/cmocka/src/audio/selector/CMakeLists.txt b/test/cmocka/src/audio/selector/CMakeLists.txt deleted file mode 100644 index eb8f742cc5f5..000000000000 --- a/test/cmocka/src/audio/selector/CMakeLists.txt +++ /dev/null @@ -1,43 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -cmocka_test(selector_test - selector_test.c -) - -target_include_directories(selector_test PRIVATE ${PROJECT_SOURCE_DIR}/src/audio) - -# make small version of libaudio so we don't have to care -# about unused missing references - -add_compile_options(-DUNIT_TEST) - -add_library(audio_for_selector STATIC - ${PROJECT_SOURCE_DIR}/src/math/numbers.c - ${PROJECT_SOURCE_DIR}/src/audio/selector/selector.c - ${PROJECT_SOURCE_DIR}/src/audio/selector/selector_generic.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/comp_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/audio_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c - ${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c - ${PROJECT_SOURCE_DIR}/src/module/audio/source_api.c - ${PROJECT_SOURCE_DIR}/src/module/audio/sink_api.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc3/helper.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc-common-kernel.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-schedule.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-stream.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-xrun.c - ${PROJECT_SOURCE_DIR}/src/audio/component.c -) -sof_append_relative_path_definitions(audio_for_selector) - -target_link_libraries(audio_for_selector PRIVATE sof_options) - -target_link_libraries(selector_test PRIVATE audio_for_selector) diff --git a/test/cmocka/src/audio/selector/selector_test.c b/test/cmocka/src/audio/selector/selector_test.c deleted file mode 100644 index a2dd0dfae741..000000000000 --- a/test/cmocka/src/audio/selector/selector_test.c +++ /dev/null @@ -1,505 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2019 Intel Corporation. All rights reserved. -// -// Author: Lech Betlej - -#include "../../util.h" - -#include -#include -#include -#include -#include -#include -#include -#if CONFIG_IPC_MAJOR_4 -#include -#endif -#include - -#if CONFIG_IPC_MAJOR_3 -struct sel_test_state { - struct comp_dev *dev; - struct comp_buffer *sink; - struct comp_buffer *source; - void (*verify)(struct comp_dev *dev, struct audio_stream *sink, - struct audio_stream *source); -}; - -struct sel_test_parameters { - uint32_t in_channels; - uint32_t out_channels; - uint32_t sel_channel; - uint32_t frames; - uint32_t buffer_size_ms; - uint32_t source_format; - uint32_t sink_format; - void (*verify)(struct comp_dev *dev, struct audio_stream *sink, - struct audio_stream *source); -}; - -static int setup(void **state) -{ - struct sel_test_parameters *parameters = *state; - struct sel_test_state *sel_state; - struct comp_data *cd; - uint32_t size = 0; - - /* allocate new state */ - sel_state = test_malloc(sizeof(*sel_state)); - - /* allocate and set new device */ - sel_state->dev = test_malloc(sizeof(struct comp_dev)); - sel_state->dev->frames = parameters->frames; - - list_init(&sel_state->dev->bsink_list); - list_init(&sel_state->dev->bsource_list); - - /* allocate and set new data */ - cd = test_malloc(sizeof(*cd)); - comp_set_drvdata(sel_state->dev, cd); - cd->source_format = parameters->source_format; - cd->sink_format = parameters->sink_format; - - /* prepare paramters that will be used by sel_get_processing_function */ - cd->config.in_channels_count = parameters->in_channels; - cd->config.out_channels_count = parameters->out_channels; - cd->config.sel_channel = parameters->sel_channel; - - cd->sel_func = sel_get_processing_function(sel_state->dev); - - /* allocate new sink buffer */ - size = parameters->frames * get_frame_bytes(parameters->sink_format, - parameters->out_channels) * parameters->buffer_size_ms; - - sel_state->sink = create_test_sink(sel_state->dev, 0, parameters->sink_format, - parameters->out_channels, size); - - /* allocate new source buffer */ - size = parameters->frames * get_frame_bytes(parameters->source_format, - parameters->in_channels) * parameters->buffer_size_ms; - - sel_state->source = create_test_source(sel_state->dev, 0, parameters->source_format, - parameters->in_channels, size); - - /* assigns verification function */ - sel_state->verify = parameters->verify; - - /* assign test state */ - *state = sel_state; - - return 0; -} - -static int teardown(void **state) -{ - struct sel_test_state *sel_state = *state; - struct comp_data *cd = comp_get_drvdata(sel_state->dev); - - /* free everything */ - test_free(cd); - test_free(sel_state->dev); - free_test_sink(sel_state->sink); - free_test_source(sel_state->source); - test_free(sel_state); - - return 0; -} -#else -struct sel_test_state { - struct processing_module *mod; - struct comp_buffer *sink; - struct comp_buffer *source; - void (*verify)(struct processing_module *mod, struct audio_stream *sink, - struct audio_stream *source); -}; - -struct sel_test_parameters { - uint32_t in_channels; - uint32_t out_channels; - uint32_t sel_channel; - uint32_t frames; - uint32_t buffer_size_ms; - uint32_t source_format; - uint32_t sink_format; - void (*verify)(struct processing_module *mod, struct audio_stream *sink, - struct audio_stream *source); -}; - -static int setup(void **state) -{ - struct sel_test_parameters *parameters = *state; - struct sel_test_state *sel_state; - struct module_data *md; - struct comp_dev *dev; - struct comp_data *cd; - uint32_t size = 0; - - /* allocate new state */ - sel_state = test_malloc(sizeof(*sel_state)); - - /* allocate and set new device */ - sel_state->mod = test_malloc(sizeof(struct processing_module)); - dev = test_malloc(sizeof(struct comp_dev)); - dev->frames = parameters->frames; - sel_state->mod->dev = dev; - - list_init(&dev->bsink_list); - list_init(&dev->bsource_list); - - /* allocate and set new data */ - cd = test_malloc(sizeof(*cd)); - md = &sel_state->mod->priv; - comp_set_drvdata(dev, sel_state->mod); - md->private = cd; - - cd->source_format = parameters->source_format; - cd->sink_format = parameters->sink_format; - - /* prepare parameters that will be used by sel_get_processing_function */ - cd->config.in_channels_count = parameters->in_channels; - cd->config.out_channels_count = parameters->out_channels; - cd->config.sel_channel = parameters->sel_channel; - - cd->sel_func = sel_get_processing_function(sel_state->mod); - - /* allocate new sink buffer */ - size = parameters->frames * get_frame_bytes(parameters->sink_format, - parameters->out_channels) * parameters->buffer_size_ms; - sel_state->sink = create_test_sink(dev, 0, parameters->sink_format, - parameters->out_channels, size); - - /* allocate new source buffer */ - size = parameters->frames * get_frame_bytes(parameters->source_format, - parameters->in_channels) * parameters->buffer_size_ms; - - sel_state->source = create_test_source(dev, 0, parameters->source_format, - parameters->in_channels, size); - - /* assigns verification function */ - sel_state->verify = parameters->verify; - - /* assign test state */ - *state = sel_state; - - return 0; -} - -static int teardown(void **state) -{ - struct sel_test_state *sel_state = *state; - struct comp_data *cd = module_get_private_data(sel_state->mod); - - /* free everything */ - test_free(cd); - test_free(sel_state->mod->dev); - test_free(sel_state->mod); - free_test_sink(sel_state->sink); - free_test_source(sel_state->source); - test_free(sel_state); - - return 0; -} -#endif - -#if CONFIG_FORMAT_S16LE -static void fill_source_s16(struct sel_test_state *sel_state) -{ - struct audio_stream *stream = &sel_state->source->stream; - int16_t *w_ptr; - int i; - - for (i = 0; i < audio_stream_get_free_samples(stream); i++) { - w_ptr = audio_stream_write_frag_s16(stream, i); - *w_ptr = i; - } - - audio_stream_produce(stream, audio_stream_get_free_bytes(stream)); -} - -#if CONFIG_IPC_MAJOR_3 -static void verify_s16le_Xch_to_1ch(struct comp_dev *dev, - struct audio_stream *sink, - struct audio_stream *source) -{ - struct comp_data *cd = comp_get_drvdata(dev); -#else -static void verify_s16le_Xch_to_1ch(struct processing_module *mod, - struct audio_stream *sink, - struct audio_stream *source) -{ - struct comp_data *cd = module_get_private_data(mod); -#endif - const uint16_t *src = (uint16_t *)source->r_ptr; - const uint16_t *dst = (uint16_t *)sink->w_ptr; - uint32_t in_channels = cd->config.in_channels_count; - uint32_t channel; - uint32_t i; - uint32_t j = 0; - uint16_t source_in; - uint16_t destination; - - for (i = 0; i < source->size / sizeof(uint16_t); i += in_channels) { - for (channel = 0; channel < in_channels; channel++) { - if (channel == cd->config.sel_channel) { - source_in = src[i + cd->config.sel_channel]; - destination = dst[j++]; - assert_int_equal(source_in, destination); - } - } - } -} - -#if CONFIG_IPC_MAJOR_3 -static void verify_s16le_2ch_to_2ch(struct comp_dev *dev, - struct audio_stream *sink, - struct audio_stream *source) -#else -static void verify_s16le_2ch_to_2ch(struct processing_module *mod, - struct audio_stream *sink, - struct audio_stream *source) -#endif -{ - const uint16_t *src = (uint16_t *)source->r_ptr; - const uint16_t *dst = (uint16_t *)sink->w_ptr; - uint32_t channels = audio_stream_get_channels(source); - uint32_t channel; - uint32_t i; - double processed; - - for (i = 0; i < sink->size / sizeof(uint16_t); i += channels) { - for (channel = 0; channel < channels; channel++) { - processed = src[i + channel]; - assert_int_equal(dst[i + channel], processed); - } - } -} - -#if CONFIG_IPC_MAJOR_3 -static void verify_s16le_4ch_to_4ch(struct comp_dev *dev, - struct audio_stream *sink, - struct audio_stream *source) -#else -static void verify_s16le_4ch_to_4ch(struct processing_module *mod, - struct audio_stream *sink, - struct audio_stream *source) -#endif -{ - const uint16_t *src = (uint16_t *)source->r_ptr; - const uint16_t *dst = (uint16_t *)sink->w_ptr; - uint32_t channels = audio_stream_get_channels(source); - uint32_t channel; - uint32_t i; - double processed; - - for (i = 0; i < sink->size / sizeof(uint16_t); i += channels) { - for (channel = 0; channel < channels; channel++) { - processed = src[i + channel]; - assert_int_equal(dst[i + channel], processed); - } - } -} - -#endif /* CONFIG_FORMAT_S16LE */ - -#if CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE -static void fill_source_s32(struct sel_test_state *sel_state) -{ - struct audio_stream *stream = &sel_state->source->stream; - int32_t *w_ptr; - int i; - - for (i = 0; i < audio_stream_get_free_samples(stream); i++) { - w_ptr = audio_stream_write_frag_s32(stream, i); - *w_ptr = i << 16; - } - - audio_stream_produce(stream, audio_stream_get_free_bytes(stream)); -} - -#if CONFIG_IPC_MAJOR_3 -static void verify_s32le_Xch_to_1ch(struct comp_dev *dev, - struct audio_stream *sink, - struct audio_stream *source) -{ - struct comp_data *cd = comp_get_drvdata(dev); - -#else -static void verify_s32le_Xch_to_1ch(struct processing_module *mod, - struct audio_stream *sink, - struct audio_stream *source) -{ - struct comp_data *cd = module_get_private_data(mod); -#endif - const uint32_t *src = (uint32_t *)source->r_ptr; - const uint32_t *dst = (uint32_t *)sink->w_ptr; - uint32_t in_channels = cd->config.in_channels_count; - uint32_t channel; - uint32_t i; - uint32_t j = 0; - uint16_t source_in; - uint16_t destination; - - for (i = 0; i < source->size / sizeof(uint32_t); i += in_channels) { - for (channel = 0; channel < in_channels; channel++) { - if (channel == cd->config.sel_channel) { - source_in = src[i + cd->config.sel_channel]; - destination = dst[j++]; - assert_int_equal(source_in, destination); - } - } - } -} - -#if CONFIG_IPC_MAJOR_3 -static void verify_s32le_2ch_to_2ch(struct comp_dev *dev, - struct audio_stream *sink, - struct audio_stream *source) -#else -static void verify_s32le_2ch_to_2ch(struct processing_module *mod, - struct audio_stream *sink, - struct audio_stream *source) -#endif -{ - const uint32_t *src = (uint32_t *)source->r_ptr; - const uint32_t *dst = (uint32_t *)sink->w_ptr; - uint32_t channels = audio_stream_get_channels(source); - uint32_t channel; - uint32_t i; - uint32_t processed; - - for (i = 0; i < sink->size / sizeof(uint32_t); i += channels) { - for (channel = 0; channel < channels; channel++) { - processed = src[i + channel]; - assert_int_equal(dst[i + channel], processed); - } - } -} - -#if CONFIG_IPC_MAJOR_3 -static void verify_s32le_4ch_to_4ch(struct comp_dev *dev, - struct audio_stream *sink, - struct audio_stream *source) -#else -static void verify_s32le_4ch_to_4ch(struct processing_module *mod, - struct audio_stream *sink, - struct audio_stream *source) -#endif -{ - const uint32_t *src = (uint32_t *)source->r_ptr; - const uint32_t *dst = (uint32_t *)sink->w_ptr; - uint32_t channels = audio_stream_get_channels(source); - uint32_t channel; - uint32_t i; - uint32_t processed; - - for (i = 0; i < sink->size / sizeof(uint32_t); i += channels) { - for (channel = 0; channel < channels; channel++) { - processed = src[i + channel]; - assert_int_equal(dst[i + channel], processed); - } - } -} -#endif /* CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE */ - -static void test_audio_sel(void **state) -{ - struct sel_test_state *sel_state = *state; -#if CONFIG_IPC_MAJOR_3 - struct sof_source *source = audio_buffer_get_source(&sel_state->source->audio_buffer); - struct sof_sink *sink = audio_buffer_get_sink(&sel_state->sink->audio_buffer); - int ret; - struct comp_data *cd = comp_get_drvdata(sel_state->dev); -#else - struct processing_module *mod = sel_state->mod; - struct comp_data *cd = module_get_private_data(mod); - const struct audio_stream *source_stream = &sel_state->source->stream; - struct audio_stream *sink_stream = &sel_state->sink->stream; - struct cir_buf_source source_buf = { - .buf_start = audio_stream_get_addr(source_stream), - .buf_end = audio_stream_get_end_addr(source_stream), - .ptr = audio_stream_get_rptr(source_stream), - }; - struct cir_buf_sink sink_buf = { - .buf_start = audio_stream_get_addr(sink_stream), - .buf_end = audio_stream_get_end_addr(sink_stream), - .ptr = audio_stream_get_wptr(sink_stream), - }; -#endif - - switch (cd->source_format) { -#if CONFIG_FORMAT_S16LE - case SOF_IPC_FRAME_S16_LE: - fill_source_s16(sel_state); - break; -#endif /* CONFIG_FORMAT_S16LE */ -#if CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE - case SOF_IPC_FRAME_S24_4LE: - case SOF_IPC_FRAME_S32_LE: - case SOF_IPC_FRAME_FLOAT: - fill_source_s32(sel_state); - break; -#endif /* CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE */ - - /* TODO: add S24_3LE support */ - /* TODO: add S24_4LE_MSB support */ - /* TODO: add U8 support */ - default: - break; - } - -#if CONFIG_IPC_MAJOR_3 - ret = cd->sel_func(sel_state->dev, sink, source, sel_state->dev->frames); - assert_int_equal(ret, 0); - - sel_state->verify(sel_state->dev, &sel_state->sink->stream, &sel_state->source->stream); - -#else - cd->sel_func(mod, &source_buf, &sink_buf, mod->dev->frames); - - sel_state->verify(mod, &sel_state->sink->stream, &sel_state->source->stream); -#endif -} - -static struct sel_test_parameters parameters[] = { -#if CONFIG_FORMAT_S16LE - { 2, 1, 0, 16, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_Xch_to_1ch }, - { 2, 1, 1, 16, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_Xch_to_1ch }, - { 2, 1, 0, 48, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_Xch_to_1ch }, - { 2, 1, 1, 48, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_Xch_to_1ch }, - { 2, 2, 0, 48, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_2ch_to_2ch }, - { 4, 4, 0, 48, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_4ch_to_4ch }, - { 2, 1, 0, 48, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_Xch_to_1ch }, - { 4, 1, 0, 48, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_Xch_to_1ch }, -#endif /* CONFIG_FORMAT_S16LE */ -#if CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE - { 2, 1, 0, 16, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_Xch_to_1ch }, - { 2, 1, 1, 16, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_Xch_to_1ch }, - { 2, 1, 0, 48, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_Xch_to_1ch }, - { 2, 1, 1, 48, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_Xch_to_1ch }, - { 2, 2, 0, 48, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_2ch_to_2ch }, - { 4, 4, 0, 48, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_4ch_to_4ch }, - { 2, 1, 0, 48, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_Xch_to_1ch }, - { 4, 1, 0, 48, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_Xch_to_1ch }, -#endif /* CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE */ -}; - -int main(void) -{ - int i; - - struct CMUnitTest tests[ARRAY_SIZE(parameters)]; - - for (i = 0; i < ARRAY_SIZE(parameters); i++) { - tests[i].name = "test_audio_sel"; - tests[i].test_func = test_audio_sel; - tests[i].setup_func = setup; - tests[i].teardown_func = teardown; - tests[i].initial_state = ¶meters[i]; - } - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test/cmocka/src/audio/volume/CMakeLists.txt b/test/cmocka/src/audio/volume/CMakeLists.txt deleted file mode 100644 index 7a6dc7b2c854..000000000000 --- a/test/cmocka/src/audio/volume/CMakeLists.txt +++ /dev/null @@ -1,52 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -cmocka_test(volume_process - volume_process.c ../module_adapter_test.c -) - -target_include_directories(volume_process PRIVATE ${PROJECT_SOURCE_DIR}/src/audio) - -# make small version of libaudio so we don't have to care -# about unused missing references - -add_compile_options(-DUNIT_TEST) - -add_library(audio_for_volume STATIC - ${PROJECT_SOURCE_DIR}/src/audio/volume/volume.c - ${PROJECT_SOURCE_DIR}/src/audio/volume/volume_ipc3.c - ${PROJECT_SOURCE_DIR}/src/audio/volume/volume_generic.c - ${PROJECT_SOURCE_DIR}/src/audio/volume/volume_hifi3.c - ${PROJECT_SOURCE_DIR}/src/audio/volume/volume_hifi4.c - ${PROJECT_SOURCE_DIR}/src/audio/volume/volume_generic_with_peakvol.c - ${PROJECT_SOURCE_DIR}/src/audio/volume/volume_hifi3_with_peakvol.c - ${PROJECT_SOURCE_DIR}/src/audio/volume/volume_hifi4_with_peakvol.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter_ipc3.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module/generic.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/comp_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/audio_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c - ${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c - ${PROJECT_SOURCE_DIR}/src/module/audio/source_api.c - ${PROJECT_SOURCE_DIR}/src/module/audio/sink_api.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc3/helper.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc-common-kernel.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c - ${PROJECT_SOURCE_DIR}/src/lib/objpool.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-schedule.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-stream.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-xrun.c - ${PROJECT_SOURCE_DIR}/src/audio/component.c - ${PROJECT_SOURCE_DIR}/src/math/numbers.c -) -sof_append_relative_path_definitions(audio_for_volume) - -target_link_libraries(audio_for_volume PRIVATE sof_options) - -target_link_libraries(volume_process PRIVATE audio_for_volume) diff --git a/test/cmocka/src/audio/volume/volume_process.c b/test/cmocka/src/audio/volume/volume_process.c deleted file mode 100644 index 267277692d2d..000000000000 --- a/test/cmocka/src/audio/volume/volume_process.c +++ /dev/null @@ -1,356 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2018 Intel Corporation. All rights reserved. -// -// Author: Tomasz Lauda - -#include "../../util.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include "../module_adapter.h" - -/* Add macro for a volume test level. The levels to test with this code - * are: - * - * VOL_MAX +42 dB is maximum for Q8.16 format - * VOL_ZERO_DB 0 dB is the unity gain gain and default volume - * VOL_MINUS_80dB -80 dB is a low gain or large attenuation test - */ -#define VOL_MINUS_80DB (VOL_ZERO_DB / 10000) - -/* Max S24_4LE format value */ -#define INT24_MAX 8388607 - -/* Min S24_4LE format value */ -#define INT24_MIN -8388608 - -struct vol_test_parameters { - int32_t volume; - struct processing_module_test_parameters module_parameters; -}; - -static void set_volume(int32_t *vol, int32_t value, uint32_t channels) -{ - int i; - - for (i = 0; i < channels; i++) - vol[i] = value; -} - -static int setup(void **state) -{ - struct vol_test_parameters *vol_parameters = *state; - struct processing_module_test_data *vol_state; - struct module_data *md; - struct vol_data *cd; - - /* allocate new state */ - vol_state = test_malloc(sizeof(*vol_state)); - vol_state->parameters = vol_parameters->module_parameters; - vol_state->num_sources = 1; - vol_state->num_sinks = 1; - module_adapter_test_setup(vol_state); - - /* allocate and set new data */ - cd = test_malloc(sizeof(*cd)); - md = &vol_state->mod->priv; - md->private = cd; - cd->is_passthrough = false; - - /* malloc memory to store current volume 4 times to ensure the address - * is 8-byte aligned for multi-way xtensa intrinsic operations. - */ - const size_t vol_size = sizeof(int32_t) * SOF_IPC_MAX_CHANNELS * 4; - - cd->vol = test_malloc(vol_size); - - /* number of channels used by the processing functions */ - cd->channels = vol_state->parameters.channels; - - /* set processing function and volume */ -#if CONFIG_IPC_MAJOR_4 - cd->scale_vol = vol_get_processing_function(vol_state->mod->dev, cd); -#else - cd->scale_vol = vol_get_processing_function(vol_state->mod->dev, vol_state->sinks[0], cd); -#endif - set_volume(cd->volume, vol_parameters->volume, vol_state->parameters.channels); - - /* assign test state */ - *state = vol_state; - - return 0; -} - -static int teardown(void **state) -{ - struct processing_module_test_data *vol_state = *state; - struct vol_data *cd = module_get_private_data(vol_state->mod); - - test_free(cd->vol); - test_free(cd); - module_adapter_test_free(vol_state); - test_free(vol_state); - - return 0; -} - -#if CONFIG_FORMAT_S16LE -static void fill_source_s16(struct processing_module_test_data *vol_state) -{ - int64_t val; - int16_t *src = (int16_t *)vol_state->sources[0]->stream.r_ptr; - int i; - int sign = 1; - - for (i = 0; i < vol_state->sources[0]->stream.size / sizeof(int16_t); i++) { - val = (INT16_MIN + (i >> 1)) * sign; - val = (val > INT16_MAX) ? INT16_MAX : val; - src[i] = (int16_t)val; - sign = -sign; - } -} - -static void verify_s16_to_s16(struct processing_module *mod, struct comp_buffer *sink, - struct comp_buffer *source) -{ - struct vol_data *cd = module_get_private_data(mod); - const int16_t *src = (int16_t *)source->stream.r_ptr; - const int16_t *dst = (int16_t *)sink->stream.w_ptr; - double processed; - int channels = audio_stream_get_channels(&sink->stream); - int channel; - int delta; - int i; - int16_t sample; - - for (i = 0; i < sink->stream.size / sizeof(uint16_t); i += channels) { - for (channel = 0; channel < channels; channel++) { - processed = src[i + channel] * - (double)cd->volume[channel] / - (double)VOL_ZERO_DB + 0.5; - if (processed > INT16_MAX) - processed = INT16_MAX; - - if (processed < INT16_MIN) - processed = INT16_MIN; - - sample = (int16_t)processed; - delta = dst[i + channel] - sample; - if (delta > 1 || delta < -1) - assert_int_equal(dst[i + channel], sample); - } - } -} -#endif /* CONFIG_FORMAT_S16LE */ - -#if CONFIG_FORMAT_S24LE -static void fill_source_s24(struct processing_module_test_data *vol_state) -{ - int64_t val; - int32_t *src = (int32_t *)vol_state->sources[0]->stream.r_ptr; - int i; - int sign = 1; - - for (i = 0; i < vol_state->sources[0]->stream.size / sizeof(int32_t); i++) { - val = (INT24_MIN + (i >> 1)) * sign; - val = (val > INT24_MAX) ? INT24_MAX : val; - src[i] = (int32_t)val; - sign = -sign; - } -} - -static void verify_s24_to_s24_s32(struct processing_module *mod, - struct comp_buffer *sink, - struct comp_buffer *source) -{ - struct vol_data *cd = module_get_private_data(mod); - const int32_t *src = (int32_t *)source->stream.r_ptr; - const int32_t *dst = (int32_t *)sink->stream.w_ptr; - double processed; - int32_t dst_sample; - int32_t sample; - int channels = audio_stream_get_channels(&sink->stream); - int channel; - int delta; - int i; - int shift = 8; - - for (i = 0; i < sink->stream.size / sizeof(uint32_t); i += channels) { - for (channel = 0; channel < channels; channel++) { - processed = (src[i + channel] << 8) * - (double)cd->volume[channel] / - (double)VOL_ZERO_DB + 0.5 * (1 << shift); - if (processed > INT32_MAX) - processed = INT32_MAX; - - if (processed < INT32_MIN) - processed = INT32_MIN; - - sample = ((int32_t)processed) >> shift; - dst_sample = dst[i + channel]; - delta = dst_sample - sample; - if (delta > 1 || delta < -1) - assert_int_equal(dst_sample, sample); - - if (shift && (dst_sample < INT24_MIN || - dst_sample > INT24_MAX)) - assert_int_equal(dst_sample, sample); - } - } -} -#endif /* CONFIG_FORMAT_S24LE */ - -#if CONFIG_FORMAT_S32LE -static void fill_source_s32(struct processing_module_test_data *vol_state) -{ - int64_t val; - int32_t *src = (int32_t *)vol_state->sources[0]->stream.r_ptr; - int i; - int sign = 1; - - for (i = 0; i < vol_state->sources[0]->stream.size / sizeof(int32_t); i++) { - val = (INT32_MIN + (i >> 1)) * sign; - val = (val > INT32_MAX) ? INT32_MAX : val; - src[i] = (int32_t)val; - sign = -sign; - } -} - -static void verify_s32_to_s24_s32(struct processing_module *mod, - struct comp_buffer *sink, - struct comp_buffer *source) -{ - struct vol_data *cd = module_get_private_data(mod); - double processed; - const int32_t *src = (int32_t *)source->stream.r_ptr; - const int32_t *dst = (int32_t *)sink->stream.w_ptr; - int32_t dst_sample; - int32_t sample; - int channels = audio_stream_get_channels(&sink->stream); - int channel; - int delta; - int i; - int shift = 0; - - for (i = 0; i < sink->stream.size / sizeof(uint32_t); i += channels) { - for (channel = 0; channel < channels; channel++) { - processed = src[i + channel] * - (double)cd->volume[channel] / - (double)VOL_ZERO_DB + 0.5 * (1 << shift); - if (processed > INT32_MAX) - processed = INT32_MAX; - - if (processed < INT32_MIN) - processed = INT32_MIN; - - sample = ((int32_t)processed) >> shift; - dst_sample = dst[i + channel]; - delta = dst_sample - sample; - if (delta > 1 || delta < -1) - assert_int_equal(dst_sample, sample); - - if (shift && (dst_sample < INT24_MIN || - dst_sample > INT24_MAX)) - assert_int_equal(dst_sample, sample); - } - } -} -#endif /* CONFIG_FORMAT_S32LE */ - -static void test_audio_vol(void **state) -{ - struct processing_module_test_data *vol_state = *state; - struct processing_module *mod = vol_state->mod; - struct vol_data *cd = module_get_private_data(mod); - struct cir_buf_source source_buf; - struct cir_buf_sink sink_buf; - - switch (audio_stream_get_frm_fmt(&vol_state->sinks[0]->stream)) { - case SOF_IPC_FRAME_S16_LE: - fill_source_s16(vol_state); - break; - case SOF_IPC_FRAME_S24_4LE: - fill_source_s24(vol_state); - break; - case SOF_IPC_FRAME_S32_LE: - case SOF_IPC_FRAME_FLOAT: - fill_source_s32(vol_state); - break; - - /* TODO: add 3LE support */ - /* TODO: add 4LE_MSB support */ - /* TODO: add U8 support */ - default: - break; - } - - source_buf.buf_start = audio_stream_get_addr(&vol_state->sources[0]->stream); - source_buf.buf_end = audio_stream_get_end_addr(&vol_state->sources[0]->stream); - source_buf.ptr = audio_stream_get_rptr(&vol_state->sources[0]->stream); - - sink_buf.buf_start = audio_stream_get_addr(&vol_state->sinks[0]->stream); - sink_buf.buf_end = audio_stream_get_end_addr(&vol_state->sinks[0]->stream); - sink_buf.ptr = audio_stream_get_wptr(&vol_state->sinks[0]->stream); - - cd->scale_vol(mod, &source_buf, &sink_buf, - mod->dev->frames, cd->attenuation); - - vol_state->verify(mod, vol_state->sinks[0], vol_state->sources[0]); -} - -static struct processing_module_test_parameters test_parameters[] = { -#if CONFIG_FORMAT_S16LE - { 2, 48, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16_to_s16 }, -#endif /* CONFIG_FORMAT_S16LE */ - -#if CONFIG_FORMAT_S24LE - { 2, 48, 1, SOF_IPC_FRAME_S24_4LE, - SOF_IPC_FRAME_S24_4LE, verify_s24_to_s24_s32 }, -#endif /* CONFIG_FORMAT_S24LE */ - -#if CONFIG_FORMAT_S32LE - { 2, 48, 1, SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S32_LE, verify_s32_to_s24_s32 }, -#endif /* CONFIG_FORMAT_S32LE */ -}; - -int main(void) -{ - struct vol_test_parameters *parameters; - uint32_t volume_values[] = {VOL_MAX, VOL_ZERO_DB, VOL_MINUS_80DB}; - int num_tests = ARRAY_SIZE(test_parameters) * ARRAY_SIZE(volume_values); - int i, j, ret; - - parameters = test_calloc(num_tests, sizeof(struct vol_test_parameters)); - for (i = 0; i < ARRAY_SIZE(test_parameters); i++) { - for (j = 0; j < ARRAY_SIZE(volume_values); j++) { - parameters[i * ARRAY_SIZE(test_parameters) + j].volume = volume_values[j]; - parameters[i * ARRAY_SIZE(test_parameters) + j].module_parameters = - test_parameters[i]; - } - } - - struct CMUnitTest tests[num_tests]; - - for (i = 0; i < num_tests; i++) { - tests[i].name = "test_audio_vol"; - tests[i].test_func = test_audio_vol; - tests[i].setup_func = setup; - tests[i].teardown_func = teardown; - tests[i].initial_state = ¶meters[i]; - } - - cmocka_set_message_output(CM_OUTPUT_TAP); - - ret = cmocka_run_group_tests(tests, NULL, NULL); - - test_free(parameters); - - return ret; -} diff --git a/test/ztest/unit/audio/dcblock/CMakeLists.txt b/test/ztest/unit/audio/dcblock/CMakeLists.txt new file mode 100644 index 000000000000..d38e4a45c4de --- /dev/null +++ b/test/ztest/unit/audio/dcblock/CMakeLists.txt @@ -0,0 +1,68 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# DC blocking filter processing unit tests converted from CMock to Ztest + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(sof_unit_audio_dcblock) + +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/include + ${SOF_ROOT}/src/kernel/platform/posix/include + ${SOF_ROOT}/src/platform/posix/include + ${SOF_ROOT}/src/audio + ${SOF_ROOT}/src/audio/dcblock + # cmocka_chirp_2ch.h, still shared with the remaining CMocka tests + ${SOF_ROOT}/test/cmocka/include + ${PROJECT_BINARY_DIR}/include/generated +) + +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_LIBRARY=1 + -DUNIT_TEST=1 +) + +# Keep full ASan/LSan allocation stacks (twister --enable-asan --enable-lsan) +if(CONFIG_ASAN) + target_compile_options(app PRIVATE -fno-omit-frame-pointer) +endif() + +target_sources(app PRIVATE + dcblock_process_ztest.c + ${SOF_ROOT}/test/ztest/unit/common/sof_context.c + ${SOF_ROOT}/zephyr/lib/alloc.c + ${SOF_ROOT}/src/audio/dcblock/dcblock.c + ${SOF_ROOT}/src/audio/dcblock/dcblock_generic.c + ${SOF_ROOT}/src/audio/dcblock/dcblock_ipc3.c + ${SOF_ROOT}/src/math/numbers.c + ${SOF_ROOT}/src/audio/module_adapter/module_adapter.c + ${SOF_ROOT}/src/audio/module_adapter/module_adapter_ipc3.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/kernel/ipc/ipc3/helper.c + ${SOF_ROOT}/src/ipc/ipc-helper.c + ${SOF_ROOT}/src/lib/objpool.c +) + +sof_append_relative_path_definitions(app) diff --git a/test/ztest/unit/audio/dcblock/dcblock_process_ztest.c b/test/ztest/unit/audio/dcblock/dcblock_process_ztest.c new file mode 100644 index 000000000000..b188e2d539b9 --- /dev/null +++ b/test/ztest/unit/audio/dcblock/dcblock_process_ztest.c @@ -0,0 +1,790 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/dcblock/dcblock_process.c +// Author: Piotr Hoppe + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "dcblock.h" + +/* Shared with the remaining CMocka tests, from test/cmocka/include */ +#include + +/* Allow a few LSB of error due to the internal Q-format rounding. */ +#define ERROR_TOLERANCE_S16 2 +#define ERROR_TOLERANCE_S24 4 +#define ERROR_TOLERANCE_S32 2048 + +/* Q2.30 DC blocking coefficient (~0.98) used for all channels. */ +#define R_COEF 1052266987 + +/* Thresholds for frames count jitter for rand() function */ +#define THR_RAND_PLUS_ONE ((RAND_MAX >> 1) + (RAND_MAX >> 2)) +#define THR_RAND_MINUS_ONE ((RAND_MAX >> 1) - (RAND_MAX >> 2)) + +/** @brief Floating point reference filter state of one channel. */ +struct ref_state { + double x_prev; + double y_prev; +}; + +static struct ref_state ref_states[CONFIG_PLATFORM_MAX_CHANNELS]; + +/** + * @brief Run one sample of a channel through the floating point reference. + * @param ch Channel index. + * @param x Input sample, normalised to [-1, 1). + * @return Output sample, normalised to [-1, 1). + */ +static double dcblock_ref(int ch, double x) +{ + struct ref_state *s = &ref_states[ch]; + double r = (double)R_COEF / (double)ONE_Q2_30; + double y = x - s->x_prev + r * s->y_prev; + + /* The fixed point filter saturates the Q1.31 state after every + * sample (sat_int32). The DC blocker has a passband gain slightly + * above one, so without this clamp the reference state would drift + * away from the implementation for near full-scale input. + */ + if (y > 2147483647.0 / 2147483648.0) + y = 2147483647.0 / 2147483648.0; + else if (y < -1.0) + y = -1.0; + + s->x_prev = x; + s->y_prev = y; + return y; +} + +/** @brief Stream configuration of one test case. */ +struct test_parameters { + uint32_t channels; + uint32_t frames; + uint32_t buffer_size_mult; + uint32_t source_format; + uint32_t sink_format; +}; + +/** @brief Component, buffers and stream positions of the running test case. */ +struct dcblock_process_fixture { + struct comp_dev *dev; + struct comp_buffer *sink; + struct comp_buffer *source; + const struct test_parameters *params; + bool continue_loop; + size_t input_frames; + size_t output_bytes; + int fill_idx; + int verify_idx; +}; + +static struct dcblock_process_fixture dcblock_fixture; + +/* Each parameter set is run through the whole chirp. */ +static const struct test_parameters parameters[] = { +#if CONFIG_FORMAT_S16LE + { 2, 48, 2, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE }, +#endif /* CONFIG_FORMAT_S16LE */ +#if CONFIG_FORMAT_S24LE + { 2, 48, 2, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE }, +#endif /* CONFIG_FORMAT_S24LE */ +#if CONFIG_FORMAT_S32LE + { 2, 48, 2, SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S32_LE }, +#endif /* CONFIG_FORMAT_S32LE */ +}; + +/** + * @brief Look up the parameter set of a source and sink format pair. + */ +static const struct test_parameters *get_parameters(uint32_t source_format, + uint32_t sink_format) +{ + size_t i; + + for (i = 0; i < ARRAY_SIZE(parameters); i++) + if (parameters[i].source_format == source_format && + parameters[i].sink_format == sink_format) + return ¶meters[i]; + + zassert_unreachable("No parameters for formats %u -> %u", source_format, sink_format); + return NULL; +} + +/** + * @brief Create a test sink buffer connected to a component. + */ +static struct comp_buffer *create_test_sink(struct comp_dev *dev, + uint32_t pipeline_id, + uint32_t frame_fmt, + uint16_t channels, + uint16_t buffer_size) +{ + struct sof_ipc_buffer desc = { + .comp = { + .pipeline_id = pipeline_id, + }, + .size = buffer_size, + }; + struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); + + if (!buffer) + return NULL; + + memset(buffer->stream.addr, 0, buffer_size); + + buffer->sink = calloc(1, sizeof(*buffer->sink)); + if (!buffer->sink) { + buffer_free(buffer); + return NULL; + } + + if (dev) + list_item_append(&buffer->source_list, &dev->bsink_list); + + buffer->sink->state = COMP_STATE_PREPARE; + audio_stream_set_frm_fmt(&buffer->stream, frame_fmt); + audio_stream_set_channels(&buffer->stream, channels); + + return buffer; +} + +/** + * @brief Release a test sink buffer and its component endpoint. + */ +static void free_test_sink(struct comp_buffer *buffer) +{ + if (!buffer) + return; + + free(comp_buffer_get_sink_component(buffer)); + buffer_free(buffer); +} + +/** + * @brief Create a test source buffer connected to a component. + */ +static struct comp_buffer *create_test_source(struct comp_dev *dev, + uint32_t pipeline_id, + uint32_t frame_fmt, + uint16_t channels, + uint16_t buffer_size) +{ + struct sof_ipc_buffer desc = { + .comp = { + .pipeline_id = pipeline_id, + }, + .size = buffer_size, + }; + struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); + + if (!buffer) + return NULL; + + memset(buffer->stream.addr, 0, buffer_size); + + buffer->source = calloc(1, sizeof(*buffer->source)); + if (!buffer->source) { + buffer_free(buffer); + return NULL; + } + + if (dev) + list_item_append(&buffer->sink_list, &dev->bsource_list); + + buffer->source->state = COMP_STATE_PREPARE; + audio_stream_set_frm_fmt(&buffer->stream, frame_fmt); + audio_stream_set_channels(&buffer->stream, channels); + + return buffer; +} + +/** + * @brief Release a test source buffer and its component endpoint. + */ +static void free_test_source(struct comp_buffer *buffer) +{ + if (!buffer) + return; + + free(buffer->source); + buffer_free(buffer); +} + +/** + * @brief Create the IPC description used to instantiate the DC blocking module. + */ +static struct sof_ipc_comp_process *create_dcblock_comp_ipc(void) +{ + struct sof_ipc_comp_process *ipc; + const size_t ipc_size = sizeof(*ipc); + const struct sof_uuid uuid = SOF_REG_UUID(dcblock); + + ipc = calloc(1, ipc_size + SOF_UUID_SIZE); + if (!ipc) + return NULL; + + memcpy_s(ipc + 1, SOF_UUID_SIZE, &uuid, SOF_UUID_SIZE); + ipc->comp.hdr.size = ipc_size + SOF_UUID_SIZE; + ipc->comp.type = SOF_COMP_MODULE_ADAPTER; + ipc->config.hdr.size = sizeof(struct sof_ipc_comp_config); + ipc->size = 0; + ipc->comp.ext_data_length = SOF_UUID_SIZE; + + return ipc; +} + +/** + * @brief Send a configuration blob holding the per-channel R coefficients. + */ +static int dcblock_send_config(struct processing_module *mod) +{ + const size_t coeff_size = CONFIG_PLATFORM_MAX_CHANNELS * sizeof(int32_t); + const size_t cdata_size = sizeof(struct sof_ipc_ctrl_data) + + sizeof(struct sof_abi_hdr) + coeff_size; + struct sof_ipc_ctrl_data *cdata; + int32_t coeffs[CONFIG_PLATFORM_MAX_CHANNELS]; + int ret; + int i; + + cdata = calloc(1, cdata_size); + if (!cdata) + return -ENOMEM; + + for (i = 0; i < CONFIG_PLATFORM_MAX_CHANNELS; i++) + coeffs[i] = R_COEF; + + cdata->cmd = SOF_CTRL_CMD_BINARY; + cdata->num_elems = coeff_size; + cdata->data[0].magic = SOF_ABI_MAGIC; + cdata->data[0].type = 0; + cdata->data[0].size = coeff_size; + cdata->data[0].abi = SOF_ABI_VERSION; + memcpy_s(cdata->data[0].data, coeff_size, coeffs, coeff_size); + + ret = module_adapter_cmd(mod->dev, COMP_CMD_SET_DATA, cdata, cdata_size); + + free(cdata); + return ret; +} + +/** + * @brief Allocate the sink buffer and set the module output buffer size. + */ +static void prepare_sink(struct dcblock_process_fixture *fx, struct processing_module *mod) +{ + const struct test_parameters *parameters = fx->params; + struct module_data *md = &mod->priv; + size_t size; + size_t free_bytes; + + /* allocate new sink buffer */ + size = parameters->frames * get_frame_bytes(parameters->sink_format, parameters->channels) * + parameters->buffer_size_mult; + + md->mpd.out_buff_size = parameters->frames * get_frame_bytes(parameters->sink_format, + parameters->channels); + + fx->sink = create_test_sink(fx->dev, 0, parameters->sink_format, + parameters->channels, size); + zassert_not_null(fx->sink, "Failed to create the sink buffer"); + free_bytes = audio_stream_get_free_bytes(&fx->sink->stream); + zassert_equal(free_bytes, size); +} + +/** + * @brief Allocate the source buffer and set the module input buffer size. + */ +static void prepare_source(struct dcblock_process_fixture *fx, struct processing_module *mod) +{ + const struct test_parameters *parameters = fx->params; + struct module_data *md = &mod->priv; + size_t size; + size_t free_bytes; + + md->mpd.in_buff_size = parameters->frames * get_frame_bytes(parameters->source_format, + parameters->channels); + + size = parameters->frames * get_frame_bytes(parameters->source_format, + parameters->channels) * parameters->buffer_size_mult; + + fx->source = create_test_source(fx->dev, 0, parameters->source_format, + parameters->channels, size); + zassert_not_null(fx->source, "Failed to create the source buffer"); + free_bytes = audio_stream_get_free_bytes(&fx->source->stream); + zassert_equal(free_bytes, size); +} + +/** + * @brief Create, configure and prepare the DC blocking module for one test case. + * + * Anything allocated here is released by dcblock_after(), also when an + * assertion fails half way. + */ +static void dcblock_test_setup(struct dcblock_process_fixture *fx, const struct test_parameters *params) +{ + struct processing_module *mod; + struct sof_ipc_comp_process *ipc; + struct sof_source *sources[1]; + struct sof_sink *sinks[1]; + int ret; + + fx->params = params; + fx->fill_idx = 0; + fx->verify_idx = 0; + memset(ref_states, 0, sizeof(ref_states)); + + ipc = create_dcblock_comp_ipc(); + zassert_not_null(ipc, "Failed to allocate the component IPC"); + + fx->dev = comp_new((struct sof_ipc_comp *)ipc); + free(ipc); + zassert_not_null(fx->dev, "Failed to create the DC blocking component"); + + fx->dev->frames = params->frames; + mod = comp_mod(fx->dev); + + ret = dcblock_send_config(mod); + zassert_equal(ret, 0, "Sending the configuration failed: %d", ret); + + prepare_sink(fx, mod); + prepare_source(fx, mod); + + /* Zeroed, CMocka test_malloc() filled all but channels with 0xba bytes */ + mod->stream_params = calloc(1, sizeof(*mod->stream_params)); + zassert_not_null(mod->stream_params, "Failed to allocate the stream parameters"); + mod->stream_params->channels = params->channels; + mod->period_bytes = get_frame_bytes(params->source_format, params->channels) * 48000 / 1000; + + sources[0] = audio_buffer_get_source(&fx->source->audio_buffer); + sinks[0] = audio_buffer_get_sink(&fx->sink->audio_buffer); + ret = module_prepare(mod, sources, 1, sinks, 1); + zassert_equal(ret, 0, "module_prepare() failed: %d", ret); + + fx->continue_loop = true; +} + +#if CONFIG_FORMAT_S16LE +/** + * @brief Write up to @p frames_max frames of the chirp to the S16 source buffer. + */ +static void fill_source_s16(struct dcblock_process_fixture *fx, int frames_max) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int16_t *x; + int bytes_total; + int samples; + int frames; + int i; + int samples_processed = 0; + + sb = comp_dev_get_first_data_producer(fx->dev); + ss = &sb->stream; + frames = MIN(audio_stream_get_free_frames(ss), frames_max); + samples = frames * audio_stream_get_channels(ss); + for (i = 0; i < samples; i++) { + x = audio_stream_write_frag_s16(ss, i); + *x = sat_int16(Q_SHIFT_RND(chirp_2ch[fx->fill_idx++], 31, 15)); + samples_processed++; + if (fx->fill_idx == CHIRP_2CH_LENGTH) { + fx->continue_loop = false; + break; + } + } + + if (samples_processed > 0) { + bytes_total = samples_processed * audio_stream_sample_bytes(ss); + comp_update_buffer_produce(sb, bytes_total); + } + + fx->input_frames = samples_processed / audio_stream_get_channels(ss); +} + +/** + * @brief Compare the S16 output of the last processing call to the reference. + */ +static void verify_sink_s16(struct dcblock_process_fixture *fx) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int nch = fx->params->channels; + int32_t delta; + int32_t ref; + int32_t out; + int16_t *x; + int16_t in; + int samples; + int i; + + sb = comp_dev_get_first_data_consumer(fx->dev); + ss = &sb->stream; + samples = fx->output_bytes >> 1; + for (i = 0; i < samples; i++) { + x = audio_stream_read_frag_s16(ss, i); + out = *x; + in = sat_int16(Q_SHIFT_RND(chirp_2ch[fx->verify_idx], 31, 15)); + ref = (int32_t)round(dcblock_ref(fx->verify_idx % nch, + (double)in / 32768.0) * 32768.0); + ref = sat_int16(ref); + fx->verify_idx++; + delta = ref - out; + if (delta > ERROR_TOLERANCE_S16 || delta < -ERROR_TOLERANCE_S16) + zassert_equal(out, ref, "S16 sample %d: output %d, reference %d", + fx->verify_idx - 1, out, ref); + } +} +#endif /* CONFIG_FORMAT_S16LE */ + +#if CONFIG_FORMAT_S24LE +/** + * @brief Write up to @p frames_max frames of the chirp to the S24 source buffer. + */ +static void fill_source_s24(struct dcblock_process_fixture *fx, int frames_max) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int32_t *x; + int bytes_total; + int samples; + int frames; + int i; + int samples_processed = 0; + + sb = comp_dev_get_first_data_producer(fx->dev); + ss = &sb->stream; + frames = MIN(audio_stream_get_free_frames(ss), frames_max); + samples = frames * audio_stream_get_channels(ss); + for (i = 0; i < samples; i++) { + x = audio_stream_write_frag_s32(ss, i); + *x = sat_int24(Q_SHIFT_RND(chirp_2ch[fx->fill_idx++], 31, 23)); + samples_processed++; + if (fx->fill_idx == CHIRP_2CH_LENGTH) { + fx->continue_loop = false; + break; + } + } + + if (samples_processed > 0) { + bytes_total = samples_processed * audio_stream_sample_bytes(ss); + comp_update_buffer_produce(sb, bytes_total); + } + + fx->input_frames = samples_processed / audio_stream_get_channels(ss); +} + +/** + * @brief Compare the S24 output of the last processing call to the reference. + */ +static void verify_sink_s24(struct dcblock_process_fixture *fx) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int nch = fx->params->channels; + int32_t delta; + int32_t ref; + int32_t out; + int32_t *x; + int32_t in; + int samples; + int i; + + sb = comp_dev_get_first_data_consumer(fx->dev); + ss = &sb->stream; + samples = fx->output_bytes >> 2; + for (i = 0; i < samples; i++) { + x = audio_stream_read_frag_s32(ss, i); + out = (*x << 8) >> 8; /* Make sure there's no 24 bit overflow */ + in = sat_int24(Q_SHIFT_RND(chirp_2ch[fx->verify_idx], 31, 23)); + ref = (int32_t)round(dcblock_ref(fx->verify_idx % nch, + (double)in / 8388608.0) * 8388608.0); + ref = sat_int24(ref); + fx->verify_idx++; + delta = ref - out; + if (delta > ERROR_TOLERANCE_S24 || delta < -ERROR_TOLERANCE_S24) + zassert_equal(out, ref, "S24 sample %d: output %d, reference %d", + fx->verify_idx - 1, out, ref); + } +} +#endif /* CONFIG_FORMAT_S24LE */ + +#if CONFIG_FORMAT_S32LE +/** + * @brief Write up to @p frames_max frames of the chirp to the S32 source buffer. + */ +static void fill_source_s32(struct dcblock_process_fixture *fx, int frames_max) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int32_t *x; + int bytes_total; + int samples; + int frames; + int i; + int samples_processed = 0; + + sb = comp_dev_get_first_data_producer(fx->dev); + ss = &sb->stream; + frames = MIN(audio_stream_get_free_frames(ss), frames_max); + samples = frames * audio_stream_get_channels(ss); + for (i = 0; i < samples; i++) { + x = audio_stream_write_frag_s32(ss, i); + *x = chirp_2ch[fx->fill_idx++]; + samples_processed++; + if (fx->fill_idx == CHIRP_2CH_LENGTH) { + fx->continue_loop = false; + break; + } + } + + if (samples_processed > 0) { + bytes_total = samples_processed * audio_stream_sample_bytes(ss); + comp_update_buffer_produce(sb, bytes_total); + } + + fx->input_frames = samples_processed / audio_stream_get_channels(ss); +} + +/** + * @brief Compare the S32 output of the last processing call to the reference. + */ +static void verify_sink_s32(struct dcblock_process_fixture *fx) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int nch = fx->params->channels; + int64_t delta; + int32_t ref; + int32_t out; + int32_t *x; + int32_t in; + int samples; + int i; + + sb = comp_dev_get_first_data_consumer(fx->dev); + ss = &sb->stream; + samples = fx->output_bytes >> 2; + for (i = 0; i < samples; i++) { + x = audio_stream_read_frag_s32(ss, i); + out = *x; + in = chirp_2ch[fx->verify_idx]; + ref = (int32_t)round(dcblock_ref(fx->verify_idx % nch, + (double)in / 2147483648.0) * 2147483648.0); + fx->verify_idx++; + delta = (int64_t)ref - (int64_t)out; + if (delta > ERROR_TOLERANCE_S32 || delta < -ERROR_TOLERANCE_S32) + zassert_equal(out, ref, "S32 sample %d: output %d, reference %d", + fx->verify_idx - 1, out, ref); + } +} +#endif /* CONFIG_FORMAT_S32LE */ + +/** + * @brief Fill the source buffer with up to @p frames frames of the chirp. + */ +static void fill_source_for_test(struct dcblock_process_fixture *fx, int frames) +{ + switch (audio_stream_get_frm_fmt(&fx->source->stream)) { +#if CONFIG_FORMAT_S16LE + case SOF_IPC_FRAME_S16_LE: + fill_source_s16(fx, frames); + break; +#endif +#if CONFIG_FORMAT_S24LE + case SOF_IPC_FRAME_S24_4LE: + fill_source_s24(fx, frames); + break; +#endif +#if CONFIG_FORMAT_S32LE + case SOF_IPC_FRAME_S32_LE: + fill_source_s32(fx, frames); + break; +#endif + default: + zassert_unreachable("Unsupported source format"); + break; + } +} + +/** + * @brief Check the output of the last processing call against the reference. + */ +static void verify_sink_for_test(struct dcblock_process_fixture *fx) +{ + switch (audio_stream_get_frm_fmt(&fx->sink->stream)) { +#if CONFIG_FORMAT_S16LE + case SOF_IPC_FRAME_S16_LE: + verify_sink_s16(fx); + break; +#endif +#if CONFIG_FORMAT_S24LE + case SOF_IPC_FRAME_S24_4LE: + verify_sink_s24(fx); + break; +#endif +#if CONFIG_FORMAT_S32LE + case SOF_IPC_FRAME_S32_LE: + verify_sink_s32(fx); + break; +#endif + default: + zassert_unreachable("Unsupported sink format"); + break; + } +} + +/** + * @brief Randomly add or remove one frame to vary the processed block size. + */ +static int frames_jitter(int frames) +{ + int r = rand(); + + if (r > THR_RAND_PLUS_ONE) + return frames + 1; + else if (r < THR_RAND_MINUS_ONE) + return frames - 1; + else + return frames; +} + +/** + * @brief Run the whole chirp through the DC blocker and compare to the reference. + */ +static void run_dcblock_test(struct dcblock_process_fixture *fx, + const struct test_parameters *params) +{ + struct processing_module *mod; + struct sof_source *sources[1]; + struct sof_sink *sinks[1]; + uint32_t avail_before; + int frames; + int ret; + + dcblock_test_setup(fx, params); + mod = comp_mod(fx->dev); + + sources[0] = audio_buffer_get_source(&fx->source->audio_buffer); + sinks[0] = audio_buffer_get_sink(&fx->sink->audio_buffer); + + while (fx->continue_loop) { + frames = frames_jitter(fx->params->frames); + fill_source_for_test(fx, frames); + + /* Process exactly the frames just filled. The source/sink API + * updates the buffer read/write positions internally, so record + * the sink fill level to know how many bytes were produced. + */ + fx->dev->frames = fx->input_frames; + avail_before = audio_stream_get_avail_bytes(&fx->sink->stream); + ret = module_process_sink_src(mod, sources, 1, sinks, 1); + zassert_equal(ret, 0, "module_process_sink_src() failed: %d", ret); + fx->output_bytes = audio_stream_get_avail_bytes(&fx->sink->stream) - avail_before; + + verify_sink_for_test(fx); + + comp_update_buffer_consume(fx->sink, fx->output_bytes); + } +} + +#if CONFIG_FORMAT_S16LE +/** + * @brief Verify the DC blocker S16 output against the reference filter. + */ +ZTEST_F(dcblock_process, test_audio_dcblock_s16) +{ + run_dcblock_test(fixture, get_parameters(SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE)); +} +#endif /* CONFIG_FORMAT_S16LE */ + +#if CONFIG_FORMAT_S24LE +/** + * @brief Verify the DC blocker S24 output against the reference filter. + */ +ZTEST_F(dcblock_process, test_audio_dcblock_s24) +{ + run_dcblock_test(fixture, get_parameters(SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE)); +} +#endif /* CONFIG_FORMAT_S24LE */ + +#if CONFIG_FORMAT_S32LE +/** + * @brief Verify the DC blocker S32 output against the reference filter. + */ +ZTEST_F(dcblock_process, test_audio_dcblock_s32) +{ + run_dcblock_test(fixture, get_parameters(SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S32_LE)); +} +#endif /* CONFIG_FORMAT_S32LE */ + +/** + * @brief Register the DC blocking driver, once for the whole suite. + */ +static void *dcblock_process_setup(void) +{ + sys_comp_init(sof_get()); + sys_comp_module_dcblock_interface_init(); + + return &dcblock_fixture; +} + +/** + * @brief Start every test case without a component or buffers. + */ +static void dcblock_before(void *data) +{ + struct dcblock_process_fixture *fx = data; + + memset(fx, 0, sizeof(*fx)); +} + +/** + * @brief Release the component and buffers of a test case. + */ +static void dcblock_after(void *data) +{ + struct dcblock_process_fixture *fx = data; + struct processing_module *mod; + + if (fx->dev) { + mod = comp_mod(fx->dev); + free(mod->stream_params); + mod->stream_params = NULL; + } + + free_test_source(fx->source); + free_test_sink(fx->sink); + if (fx->dev) + comp_free(fx->dev); + + memset(fx, 0, sizeof(*fx)); +} + +ZTEST_SUITE(dcblock_process, NULL, dcblock_process_setup, dcblock_before, dcblock_after, NULL); diff --git a/test/ztest/unit/audio/dcblock/prj.conf b/test/ztest/unit/audio/dcblock/prj.conf new file mode 100644 index 000000000000..c0907070aa20 --- /dev/null +++ b/test/ztest/unit/audio/dcblock/prj.conf @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n +CONFIG_COMP_DCBLOCK=y +CONFIG_FORMAT_S16LE=y +CONFIG_FORMAT_S24LE=y +CONFIG_FORMAT_S32LE=y +# Route the SOF heap to the host malloc()/free() of the native simulator, so +# that ASan and LSan can see it. They cannot see blocks carved out of the +# static sys_heap arena that zephyr/lib/alloc.c uses by default. +CONFIG_SOF_NATIVE_SIM_HOST_HEAP=y +# The sys_heap arena is still reserved and initialised by heap_init(), and its +# 256 KiB are too large for SYS_HEAP_SMALL_ONLY (see app/boards/native_sim.conf) +CONFIG_SYS_HEAP_BIG_ONLY=y diff --git a/test/ztest/unit/audio/dcblock/testcase.yaml b/test/ztest/unit/audio/dcblock/testcase.yaml new file mode 100644 index 000000000000..3901e553ec5f --- /dev/null +++ b/test/ztest/unit/audio/dcblock/testcase.yaml @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# DC blocking filter processing unit tests converted from CMock to Ztest +# +# These contents may have been developed with support from one or more Intel-operated +# generative artificial intelligence solutions. +# + +common: + tags: + - SOF + - unit_test + - audio + - dcblock + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.audio.dcblock: + platform_allow: + - native_sim + build_only: false diff --git a/test/ztest/unit/audio/eq_fir/CMakeLists.txt b/test/ztest/unit/audio/eq_fir/CMakeLists.txt new file mode 100644 index 000000000000..6e6744395c4f --- /dev/null +++ b/test/ztest/unit/audio/eq_fir/CMakeLists.txt @@ -0,0 +1,74 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# FIR equalizer processing unit tests converted from CMock to Ztest + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(sof_unit_audio_eq_fir) + +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/include + ${SOF_ROOT}/src/kernel/platform/posix/include + ${SOF_ROOT}/src/platform/posix/include + ${SOF_ROOT}/src/audio + # cmocka_chirp_2ch.h, still shared with the remaining CMocka tests + ${SOF_ROOT}/test/cmocka/include + ${PROJECT_BINARY_DIR}/include/generated +) + +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_LIBRARY=1 + -DUNIT_TEST=1 +) + +# Keep full ASan/LSan allocation stacks (twister --enable-asan --enable-lsan) +if(CONFIG_ASAN) + target_compile_options(app PRIVATE -fno-omit-frame-pointer) +endif() + +target_sources(app PRIVATE + eq_fir_process_ztest.c + ${SOF_ROOT}/test/ztest/unit/common/sof_context.c + ${SOF_ROOT}/zephyr/lib/alloc.c + ${SOF_ROOT}/src/audio/eq_fir/eq_fir.c + ${SOF_ROOT}/src/audio/eq_fir/eq_fir_ipc3.c + ${SOF_ROOT}/src/audio/eq_fir/eq_fir_generic.c + ${SOF_ROOT}/src/math/fir_generic.c + ${SOF_ROOT}/src/math/fir_float.c + # The HiFi variants compile to nothing on native_sim + ${SOF_ROOT}/src/audio/eq_fir/eq_fir_hifi2ep.c + ${SOF_ROOT}/src/audio/eq_fir/eq_fir_hifi3.c + ${SOF_ROOT}/src/math/fir_hifi2ep.c + ${SOF_ROOT}/src/math/fir_hifi3.c + ${SOF_ROOT}/src/math/numbers.c + ${SOF_ROOT}/src/audio/module_adapter/module_adapter.c + ${SOF_ROOT}/src/audio/module_adapter/module_adapter_ipc3.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/kernel/ipc/ipc3/helper.c + ${SOF_ROOT}/src/ipc/ipc-helper.c + ${SOF_ROOT}/src/lib/objpool.c +) + +sof_append_relative_path_definitions(app) diff --git a/test/cmocka/src/audio/eq_fir/cmocka_fir_coef_2ch.h b/test/ztest/unit/audio/eq_fir/cmocka_fir_coef_2ch.h similarity index 100% rename from test/cmocka/src/audio/eq_fir/cmocka_fir_coef_2ch.h rename to test/ztest/unit/audio/eq_fir/cmocka_fir_coef_2ch.h diff --git a/test/cmocka/src/audio/eq_fir/cmocka_fir_ref.h b/test/ztest/unit/audio/eq_fir/cmocka_fir_ref.h similarity index 100% rename from test/cmocka/src/audio/eq_fir/cmocka_fir_ref.h rename to test/ztest/unit/audio/eq_fir/cmocka_fir_ref.h diff --git a/test/cmocka/src/audio/eq_fir/debug_files_plot.m b/test/ztest/unit/audio/eq_fir/debug_files_plot.m similarity index 80% rename from test/cmocka/src/audio/eq_fir/debug_files_plot.m rename to test/ztest/unit/audio/eq_fir/debug_files_plot.m index c8cd62998594..13575f877630 100644 --- a/test/cmocka/src/audio/eq_fir/debug_files_plot.m +++ b/test/ztest/unit/audio/eq_fir/debug_files_plot.m @@ -4,7 +4,10 @@ % % Copyright(c) 2022 Intel Corporation. All rights reserved. -load ../../../../../build_ut/test/cmocka/src/audio/eq_fir/fir_test_16.txt; +% Run in the directory where eq_fir_process_ztest.c with DEBUG_FILES defined +% wrote its output, with twister the build directory of the suite. + +load fir_test_16.txt; iref = fir_test_16(:,1); iout = fir_test_16(:,2); ref = reshape(iref, size(iref, 1)/2, 2); @@ -21,7 +24,7 @@ plot(ref - out); grid on -load ../../../../../build_ut/test/cmocka/src/audio/eq_fir/fir_test_24.txt; +load fir_test_24.txt; iref = fir_test_24(:,1); iout = fir_test_24(:,2); ref = reshape(iref, size(iref, 1)/2, 2); @@ -38,7 +41,7 @@ plot(ref - out); grid on -load ../../../../../build_ut/test/cmocka/src/audio/eq_fir/fir_test_32.txt; +load fir_test_32.txt; iref = fir_test_32(:,1); iout = fir_test_32(:,2); ref = reshape(iref, size(iref, 1)/2, 2); diff --git a/test/ztest/unit/audio/eq_fir/eq_fir_process_ztest.c b/test/ztest/unit/audio/eq_fir/eq_fir_process_ztest.c new file mode 100644 index 000000000000..d7dc1ff41c49 --- /dev/null +++ b/test/ztest/unit/audio/eq_fir/eq_fir_process_ztest.c @@ -0,0 +1,962 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/eq_fir/eq_fir_process.c +// Author: Rander Wang + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +/* Shared with the remaining CMocka tests, from test/cmocka/include */ +#include + +#include "cmocka_fir_ref.h" +#include "cmocka_fir_coef_2ch.h" + +/* Allow some small error for fixed point */ +#define ERROR_TOLERANCE_S16 1 +#define ERROR_TOLERANCE_S24 2 +#define ERROR_TOLERANCE_S32 4 + +/* Thresholds for frames count jitter for rand() function */ +#define THR_RAND_PLUS_ONE ((RAND_MAX >> 1) + (RAND_MAX >> 2)) +#define THR_RAND_MINUS_ONE ((RAND_MAX >> 1) - (RAND_MAX >> 2)) + +/* Export optionally data to files for easier debug, see debug_files_plot.m */ +#undef DEBUG_FILES +#ifdef DEBUG_FILES +#include + +static FILE *debug_fh_16; +static FILE *debug_fh_24; +static FILE *debug_fh_32; +#endif + +/** @brief Stream configuration of one test case. */ +struct test_parameters { + unsigned int channels; + size_t frames; + uint32_t buffer_size_mult; + uint32_t source_format; + uint32_t sink_format; +}; + +/** @brief Component, buffers and stream positions of the running test case. */ +struct eq_fir_process_fixture { + struct comp_dev *dev; + struct comp_buffer *sink; + struct comp_buffer *source; + const struct test_parameters *params; + bool continue_loop; + size_t input_frames; + size_t output_bytes; + int fill_idx; + int verify_idx; +}; + +static struct eq_fir_process_fixture eq_fir_fixture; + +/* + * Each parameter set is run through the whole chirp. The other test cases use + * the first set, like the CMocka test did. + */ +static const struct test_parameters parameters[] = { +#if CONFIG_FORMAT_S16LE + { 2, 48, 2, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE }, +#endif /* CONFIG_FORMAT_S16LE */ +#if CONFIG_FORMAT_S24LE + { 2, 48, 2, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE }, +#endif /* CONFIG_FORMAT_S24LE */ +#if CONFIG_FORMAT_S32LE + { 2, 48, 2, SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S32_LE }, +#endif /* CONFIG_FORMAT_S32LE */ +}; + +/** + * @brief Look up the parameter set of a source and sink format pair. + */ +static const struct test_parameters *get_parameters(uint32_t source_format, + uint32_t sink_format) +{ + size_t i; + + for (i = 0; i < ARRAY_SIZE(parameters); i++) + if (parameters[i].source_format == source_format && + parameters[i].sink_format == sink_format) + return ¶meters[i]; + + zassert_unreachable("No parameters for formats %u -> %u", source_format, sink_format); + return NULL; +} + +/** + * @brief Create a test sink buffer connected to a component. + */ +static struct comp_buffer *create_test_sink(struct comp_dev *dev, + uint32_t pipeline_id, + uint32_t frame_fmt, + uint16_t channels, + uint16_t buffer_size) +{ + struct sof_ipc_buffer desc = { + .comp = { + .pipeline_id = pipeline_id, + }, + .size = buffer_size, + }; + struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); + + if (!buffer) + return NULL; + + memset(buffer->stream.addr, 0, buffer_size); + + buffer->sink = calloc(1, sizeof(*buffer->sink)); + if (!buffer->sink) { + buffer_free(buffer); + return NULL; + } + + if (dev) + list_item_append(&buffer->source_list, &dev->bsink_list); + + buffer->sink->state = COMP_STATE_PREPARE; + audio_stream_set_frm_fmt(&buffer->stream, frame_fmt); + audio_stream_set_channels(&buffer->stream, channels); + + return buffer; +} + +/** + * @brief Release a test sink buffer and its component endpoint. + */ +static void free_test_sink(struct comp_buffer *buffer) +{ + if (!buffer) + return; + + free(comp_buffer_get_sink_component(buffer)); + buffer_free(buffer); +} + +/** + * @brief Create a test source buffer connected to a component. + */ +static struct comp_buffer *create_test_source(struct comp_dev *dev, + uint32_t pipeline_id, + uint32_t frame_fmt, + uint16_t channels, + uint16_t buffer_size) +{ + struct sof_ipc_buffer desc = { + .comp = { + .pipeline_id = pipeline_id, + }, + .size = buffer_size, + }; + struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); + + if (!buffer) + return NULL; + + memset(buffer->stream.addr, 0, buffer_size); + + buffer->source = calloc(1, sizeof(*buffer->source)); + if (!buffer->source) { + buffer_free(buffer); + return NULL; + } + + if (dev) + list_item_append(&buffer->sink_list, &dev->bsource_list); + + buffer->source->state = COMP_STATE_PREPARE; + audio_stream_set_frm_fmt(&buffer->stream, frame_fmt); + audio_stream_set_channels(&buffer->stream, channels); + + return buffer; +} + +/** + * @brief Release a test source buffer and its component endpoint. + */ +static void free_test_source(struct comp_buffer *buffer) +{ + if (!buffer) + return; + + free(buffer->source); + buffer_free(buffer); +} + +/** + * @brief Create the IPC description used to instantiate the FIR EQ module. + */ +static struct sof_ipc_comp_process *create_eq_fir_comp_ipc(void) +{ + struct sof_ipc_comp_process *ipc; + const size_t ipc_size = sizeof(*ipc); + const struct sof_uuid uuid = SOF_REG_UUID(eq_fir); + + ipc = calloc(1, ipc_size + SOF_UUID_SIZE); + if (!ipc) + return NULL; + + memcpy_s(ipc + 1, SOF_UUID_SIZE, &uuid, SOF_UUID_SIZE); + ipc->comp.hdr.size = ipc_size + SOF_UUID_SIZE; + ipc->comp.type = SOF_COMP_MODULE_ADAPTER; + ipc->config.hdr.size = sizeof(struct sof_ipc_comp_config); + ipc->size = 0; + ipc->comp.ext_data_length = SOF_UUID_SIZE; + + return ipc; +} + +/** + * @brief Send a FIR EQ configuration blob through the module API. + * @param mod Module to configure. + * @param blob Blob to send, its header size field may be larger than the + * storage behind it. + * @param blob_storage_size Number of valid bytes at @p blob, including the + * ABI header. Only this much of the payload is copied. + * @return The set_configuration() result. + */ +static int eq_fir_send_blob(struct processing_module *mod, + const struct sof_abi_hdr *blob, + size_t blob_storage_size) +{ + const struct module_interface *const ops = mod->dev->drv->adapter_ops; + const size_t cdata_size = sizeof(struct sof_ipc_ctrl_data) + + sizeof(struct sof_abi_hdr) + blob->size; + struct sof_ipc_ctrl_data *cdata; + size_t copy_size; + int ret; + + cdata = calloc(1, cdata_size); + if (!cdata) + return -ENOMEM; + + cdata->cmd = SOF_CTRL_CMD_BINARY; + cdata->num_elems = blob->size; + cdata->data[0].magic = blob->magic; + cdata->data[0].type = blob->type; + cdata->data[0].size = blob->size; + cdata->data[0].abi = blob->abi; + copy_size = blob_storage_size > sizeof(*blob) ? blob_storage_size - sizeof(*blob) : 0; + copy_size = MIN(copy_size, (size_t)blob->size); + if (copy_size) + memcpy_s(cdata->data[0].data, blob->size, blob->data, copy_size); + + ret = ops->set_configuration(mod, 0, MODULE_CFG_FRAGMENT_SINGLE, + blob->size, (const uint8_t *)cdata, + blob->size, NULL, 0); + + free(cdata); + return ret; +} + +/** + * @brief Send the reference two channel FIR EQ configuration. + */ +static int eq_fir_send_config(struct processing_module *mod) +{ + return eq_fir_send_blob(mod, (const struct sof_abi_hdr *)fir_coef_2ch, + sizeof(fir_coef_2ch)); +} + +/** + * @brief Copy the reference FIR EQ configuration into a modifiable blob. + */ +static void copy_eq_fir_blob(uint32_t *blob) +{ + zassert_equal(memcpy_s(blob, sizeof(fir_coef_2ch), fir_coef_2ch, + sizeof(fir_coef_2ch)), 0); +} + +/** + * @brief Allocate the sink buffer and set the module output buffer size. + */ +static void prepare_sink(struct eq_fir_process_fixture *fx, struct processing_module *mod) +{ + const struct test_parameters *parameters = fx->params; + struct module_data *md = &mod->priv; + size_t size; + size_t free_bytes; + + /* allocate new sink buffer */ + size = parameters->frames * get_frame_bytes(parameters->sink_format, parameters->channels) * + parameters->buffer_size_mult; + + md->mpd.out_buff_size = parameters->frames * get_frame_bytes(parameters->sink_format, + parameters->channels); + + fx->sink = create_test_sink(fx->dev, 0, parameters->sink_format, + parameters->channels, size); + zassert_not_null(fx->sink, "Failed to create the sink buffer"); + free_bytes = audio_stream_get_free_bytes(&fx->sink->stream); + zassert_equal(free_bytes, size); +} + +/** + * @brief Allocate the source buffer and set the module input buffer size. + */ +static void prepare_source(struct eq_fir_process_fixture *fx, struct processing_module *mod) +{ + const struct test_parameters *parameters = fx->params; + struct module_data *md = &mod->priv; + size_t size; + size_t free_bytes; + + md->mpd.in_buff_size = parameters->frames * get_frame_bytes(parameters->source_format, + parameters->channels); + + size = parameters->frames * get_frame_bytes(parameters->source_format, + parameters->channels) * parameters->buffer_size_mult; + + fx->source = create_test_source(fx->dev, 0, parameters->source_format, + parameters->channels, size); + zassert_not_null(fx->source, "Failed to create the source buffer"); + free_bytes = audio_stream_get_free_bytes(&fx->source->stream); + zassert_equal(free_bytes, size); +} + +/** + * @brief Create, configure and prepare the FIR EQ module for one test case. + * + * Anything allocated here is released by eq_fir_after(), also when an + * assertion fails half way. + */ +static void eq_fir_setup(struct eq_fir_process_fixture *fx, const struct test_parameters *params) +{ + struct processing_module *mod; + struct sof_ipc_comp_process *ipc; + struct sof_source *sources[1]; + struct sof_sink *sinks[1]; + int ret; + + fx->params = params; + fx->fill_idx = 0; + fx->verify_idx = 0; + + ipc = create_eq_fir_comp_ipc(); + zassert_not_null(ipc, "Failed to allocate the component IPC"); + + fx->dev = comp_new((struct sof_ipc_comp *)ipc); + free(ipc); + zassert_not_null(fx->dev, "Failed to create the FIR EQ component"); + + fx->dev->frames = params->frames; + mod = comp_mod(fx->dev); + + ret = eq_fir_send_config(mod); + zassert_equal(ret, 0, "Sending the configuration failed: %d", ret); + + prepare_sink(fx, mod); + prepare_source(fx, mod); + + /* Zeroed, CMocka test_malloc() filled all but channels with 0xba bytes */ + mod->stream_params = calloc(1, sizeof(*mod->stream_params)); + zassert_not_null(mod->stream_params, "Failed to allocate the stream parameters"); + mod->stream_params->channels = params->channels; + mod->period_bytes = get_frame_bytes(params->source_format, params->channels) * 48000 / 1000; + + sources[0] = audio_buffer_get_source(&fx->source->audio_buffer); + sinks[0] = audio_buffer_get_sink(&fx->sink->audio_buffer); + ret = module_prepare(mod, sources, 1, sinks, 1); + zassert_equal(ret, 0, "module_prepare() failed: %d", ret); + + fx->continue_loop = true; +} + +#if CONFIG_FORMAT_S16LE +/** + * @brief Write up to @p frames_max frames of the chirp to the S16 source buffer. + */ +static void fill_source_s16(struct eq_fir_process_fixture *fx, size_t frames_max) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int16_t *x; + size_t bytes_total; + size_t samples; + size_t frames; + size_t i; + size_t samples_processed = 0; + + sb = comp_dev_get_first_data_producer(fx->dev); + ss = &sb->stream; + frames = MIN(audio_stream_get_free_frames(ss), frames_max); + samples = frames * audio_stream_get_channels(ss); + for (i = 0; i < samples; i++) { + x = audio_stream_write_frag_s16(ss, i); + *x = sat_int16(Q_SHIFT_RND(chirp_2ch[fx->fill_idx++], 31, 15)); + samples_processed++; + if (fx->fill_idx == CHIRP_2CH_LENGTH) { + fx->continue_loop = false; + break; + } + } + + if (samples_processed > 0) { + bytes_total = samples_processed * audio_stream_sample_bytes(ss); + comp_update_buffer_produce(sb, bytes_total); + } + + fx->input_frames = samples_processed / audio_stream_get_channels(ss); +} + +/** + * @brief Compare the S16 output of the last processing call to the reference. + */ +static void verify_sink_s16(struct eq_fir_process_fixture *fx) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int32_t delta; + int32_t ref; + int32_t out; + int16_t *x; + size_t samples; + size_t i; + + sb = comp_dev_get_first_data_consumer(fx->dev); + ss = &sb->stream; + samples = fx->output_bytes >> 1; + for (i = 0; i < samples; i++) { + x = audio_stream_read_frag_s16(ss, i); + out = *x; + ref = sat_int16(Q_SHIFT_RND(fir_ref_2ch[fx->verify_idx++], 31, 15)); + delta = ref - out; + if (delta > ERROR_TOLERANCE_S16 || delta < -ERROR_TOLERANCE_S16) + zassert_equal(out, ref, "S16 sample %d: output %d, reference %d", + fx->verify_idx - 1, out, ref); +#ifdef DEBUG_FILES + fprintf(debug_fh_16, "%d %d\n", ref, out); +#endif + } +} +#endif /* CONFIG_FORMAT_S16LE */ + +#if CONFIG_FORMAT_S24LE +/** + * @brief Write up to @p frames_max frames of the chirp to the S24 source buffer. + */ +static void fill_source_s24(struct eq_fir_process_fixture *fx, size_t frames_max) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int32_t *x; + size_t bytes_total; + size_t samples; + size_t frames; + size_t i; + size_t samples_processed = 0; + + sb = comp_dev_get_first_data_producer(fx->dev); + ss = &sb->stream; + frames = MIN(audio_stream_get_free_frames(ss), frames_max); + samples = frames * audio_stream_get_channels(ss); + for (i = 0; i < samples; i++) { + x = audio_stream_write_frag_s32(ss, i); + *x = sat_int24(Q_SHIFT_RND(chirp_2ch[fx->fill_idx++], 31, 23)); + samples_processed++; + if (fx->fill_idx == CHIRP_2CH_LENGTH) { + fx->continue_loop = false; + break; + } + } + + if (samples_processed > 0) { + bytes_total = samples_processed * audio_stream_sample_bytes(ss); + comp_update_buffer_produce(sb, bytes_total); + } + + fx->input_frames = samples_processed / audio_stream_get_channels(ss); +} + +/** + * @brief Compare the S24 output of the last processing call to the reference. + */ +static void verify_sink_s24(struct eq_fir_process_fixture *fx) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int32_t delta; + int32_t ref; + int32_t out; + int32_t *x; + size_t samples; + size_t i; + + sb = comp_dev_get_first_data_consumer(fx->dev); + ss = &sb->stream; + samples = fx->output_bytes >> 2; + for (i = 0; i < samples; i++) { + x = audio_stream_read_frag_s32(ss, i); + out = (*x << 8) >> 8; /* Make sure there's no 24 bit overflow */ + ref = sat_int24(Q_SHIFT_RND(fir_ref_2ch[fx->verify_idx++], 31, 23)); + delta = ref - out; + if (delta > ERROR_TOLERANCE_S24 || delta < -ERROR_TOLERANCE_S24) + zassert_equal(out, ref, "S24 sample %d: output %d, reference %d", + fx->verify_idx - 1, out, ref); +#ifdef DEBUG_FILES + fprintf(debug_fh_24, "%d %d\n", ref, out); +#endif + } +} +#endif /* CONFIG_FORMAT_S24LE */ + +#if CONFIG_FORMAT_S32LE +/** + * @brief Write up to @p frames_max frames of the chirp to the S32 source buffer. + */ +static void fill_source_s32(struct eq_fir_process_fixture *fx, size_t frames_max) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int32_t *x; + size_t bytes_total; + size_t samples; + size_t frames; + size_t i; + size_t samples_processed = 0; + + sb = comp_dev_get_first_data_producer(fx->dev); + ss = &sb->stream; + frames = MIN(audio_stream_get_free_frames(ss), frames_max); + samples = frames * audio_stream_get_channels(ss); + for (i = 0; i < samples; i++) { + x = audio_stream_write_frag_s32(ss, i); + *x = chirp_2ch[fx->fill_idx++]; + samples_processed++; + if (fx->fill_idx == CHIRP_2CH_LENGTH) { + fx->continue_loop = false; + break; + } + } + + if (samples_processed > 0) { + bytes_total = samples_processed * audio_stream_sample_bytes(ss); + comp_update_buffer_produce(sb, bytes_total); + } + + fx->input_frames = samples_processed / audio_stream_get_channels(ss); +} + +/** + * @brief Compare the S32 output of the last processing call to the reference. + */ +static void verify_sink_s32(struct eq_fir_process_fixture *fx) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int64_t delta; + int32_t ref; + int32_t out; + int32_t *x; + size_t samples; + size_t i; + + sb = comp_dev_get_first_data_consumer(fx->dev); + ss = &sb->stream; + samples = fx->output_bytes >> 2; + for (i = 0; i < samples; i++) { + x = audio_stream_read_frag_s32(ss, i); + out = *x; + ref = fir_ref_2ch[fx->verify_idx++]; + delta = (int64_t)ref - (int64_t)out; + if (delta > ERROR_TOLERANCE_S32 || delta < -ERROR_TOLERANCE_S32) + zassert_equal(out, ref, "S32 sample %d: output %d, reference %d", + fx->verify_idx - 1, out, ref); +#ifdef DEBUG_FILES + fprintf(debug_fh_32, "%d %d\n", ref, out); +#endif + } +} +#endif /* CONFIG_FORMAT_S32LE */ + +/** + * @brief Fill the source buffer with up to @p frames frames of the chirp. + */ +static void fill_source_for_test(struct eq_fir_process_fixture *fx, size_t frames) +{ + switch (audio_stream_get_frm_fmt(&fx->source->stream)) { +#if CONFIG_FORMAT_S16LE + case SOF_IPC_FRAME_S16_LE: + fill_source_s16(fx, frames); + break; +#endif +#if CONFIG_FORMAT_S24LE + case SOF_IPC_FRAME_S24_4LE: + fill_source_s24(fx, frames); + break; +#endif +#if CONFIG_FORMAT_S32LE + case SOF_IPC_FRAME_S32_LE: + fill_source_s32(fx, frames); + break; +#endif + default: + zassert_unreachable("Unsupported source format"); + break; + } +} + +/** + * @brief Check the output of the last processing call against the reference. + */ +static void verify_sink_for_test(struct eq_fir_process_fixture *fx) +{ + switch (audio_stream_get_frm_fmt(&fx->sink->stream)) { +#if CONFIG_FORMAT_S16LE + case SOF_IPC_FRAME_S16_LE: + verify_sink_s16(fx); + break; +#endif +#if CONFIG_FORMAT_S24LE + case SOF_IPC_FRAME_S24_4LE: + verify_sink_s24(fx); + break; +#endif +#if CONFIG_FORMAT_S32LE + case SOF_IPC_FRAME_S32_LE: + verify_sink_s32(fx); + break; +#endif + default: + zassert_unreachable("Unsupported sink format"); + break; + } +} + +/** + * @brief Randomly add or remove one frame to vary the processed block size. + */ +static size_t frames_jitter(size_t frames) +{ + int r = rand(); + + if (r > THR_RAND_PLUS_ONE) + return frames + 1; + else if (r < THR_RAND_MINUS_ONE) + return frames - 1; + else + return frames; +} + +/** + * @brief Run the whole chirp through the FIR EQ and compare to the reference. + */ +static void run_eq_fir_test(struct eq_fir_process_fixture *fx, const struct test_parameters *params) +{ + struct processing_module *mod; + struct sof_source *sources[1]; + struct sof_sink *sinks[1]; + size_t avail_before; + size_t frames; + int ret; + + eq_fir_setup(fx, params); + mod = comp_mod(fx->dev); + + sources[0] = audio_buffer_get_source(&fx->source->audio_buffer); + sinks[0] = audio_buffer_get_sink(&fx->sink->audio_buffer); + + while (fx->continue_loop) { + frames = frames_jitter(fx->params->frames); + fill_source_for_test(fx, frames); + + fx->dev->frames = fx->input_frames; + avail_before = audio_stream_get_avail_bytes(&fx->sink->stream); + ret = module_process_sink_src(mod, sources, 1, sinks, 1); + zassert_equal(ret, 0, "module_process_sink_src() failed: %d", ret); + + fx->output_bytes = audio_stream_get_avail_bytes(&fx->sink->stream) - avail_before; + + verify_sink_for_test(fx); + + comp_update_buffer_consume(fx->sink, fx->output_bytes); + } +} + +#if CONFIG_FORMAT_S16LE +/** + * @brief Verify the FIR EQ S16 output against the reference. + */ +ZTEST_F(eq_fir_process, test_audio_eq_fir_s16) +{ + run_eq_fir_test(fixture, get_parameters(SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE)); +} +#endif /* CONFIG_FORMAT_S16LE */ + +#if CONFIG_FORMAT_S24LE +/** + * @brief Verify the FIR EQ S24 output against the reference. + */ +ZTEST_F(eq_fir_process, test_audio_eq_fir_s24) +{ + run_eq_fir_test(fixture, get_parameters(SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE)); +} +#endif /* CONFIG_FORMAT_S24LE */ + +#if CONFIG_FORMAT_S32LE +/** + * @brief Verify the FIR EQ S32 output against the reference. + */ +ZTEST_F(eq_fir_process, test_audio_eq_fir_s32) +{ + run_eq_fir_test(fixture, get_parameters(SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S32_LE)); +} +#endif /* CONFIG_FORMAT_S32LE */ + +/** + * @brief Verify that an odd frame count is rounded down to an even one. + */ +ZTEST_F(eq_fir_process, test_eq_fir_rounds_down_odd_frame_count) +{ + struct processing_module *mod; + struct sof_source *sources[1]; + struct sof_sink *sinks[1]; + size_t frame_bytes; + size_t source_avail_before; + size_t sink_avail_before; + int ret; + + eq_fir_setup(fixture, ¶meters[0]); + mod = comp_mod(fixture->dev); + frame_bytes = get_frame_bytes(fixture->params->source_format, fixture->params->channels); + + sources[0] = audio_buffer_get_source(&fixture->source->audio_buffer); + sinks[0] = audio_buffer_get_sink(&fixture->sink->audio_buffer); + fill_source_for_test(fixture, 3); + source_avail_before = audio_stream_get_avail_bytes(&fixture->source->stream); + sink_avail_before = audio_stream_get_avail_bytes(&fixture->sink->stream); + + ret = module_process_sink_src(mod, sources, 1, sinks, 1); + zassert_equal(ret, 0); + zassert_equal(source_avail_before - + audio_stream_get_avail_bytes(&fixture->source->stream), + 2 * frame_bytes); + zassert_equal(audio_stream_get_avail_bytes(&fixture->sink->stream) - + sink_avail_before, 2 * frame_bytes); +} + +/** + * @brief Verify that malformed configuration blobs are rejected. + */ +ZTEST_F(eq_fir_process, test_eq_fir_rejects_invalid_configurations) +{ + struct processing_module *mod; + uint32_t blob_copy[ARRAY_SIZE(fir_coef_2ch)]; + struct sof_abi_hdr *abi; + struct sof_eq_fir_config *config; + struct sof_fir_coef_data *coef; + int ret; + + eq_fir_setup(fixture, ¶meters[0]); + mod = comp_mod(fixture->dev); + + /* Reject a blob with no channel assignments or coefficient data. */ + copy_eq_fir_blob(blob_copy); + abi = (struct sof_abi_hdr *)blob_copy; + config = (struct sof_eq_fir_config *)abi->data; + abi->size = sizeof(*config); + config->size = abi->size; + ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); + zassert_equal(ret, -EINVAL); + + /* Reject a blob with a truncated coefficient header. */ + copy_eq_fir_blob(blob_copy); + abi = (struct sof_abi_hdr *)blob_copy; + config = (struct sof_eq_fir_config *)abi->data; + abi->size = sizeof(*config) + 2 * sizeof(int16_t) + + (SOF_FIR_COEF_NHEADER - 1) * sizeof(int16_t); + config->size = abi->size; + ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); + zassert_equal(ret, -EINVAL); + + /* Reject a blob smaller than the configuration header. */ + copy_eq_fir_blob(blob_copy); + abi = (struct sof_abi_hdr *)blob_copy; + abi->size = sizeof(struct sof_eq_fir_config) - 1; + ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); + zassert_equal(ret, -EINVAL); + + /* Reject a blob larger than the maximum configuration size. */ + copy_eq_fir_blob(blob_copy); + abi = (struct sof_abi_hdr *)blob_copy; + abi->size = SOF_EQ_FIR_MAX_SIZE + 1; + ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); + zassert_equal(ret, -EINVAL); + + /* Reject mismatched ABI and configuration payload sizes. */ + copy_eq_fir_blob(blob_copy); + abi = (struct sof_abi_hdr *)blob_copy; + config = (struct sof_eq_fir_config *)abi->data; + config->size = abi->size - sizeof(int16_t); + ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); + zassert_equal(ret, -EINVAL); + + /* Reject an odd channel count that misaligns coefficient data. */ + copy_eq_fir_blob(blob_copy); + abi = (struct sof_abi_hdr *)blob_copy; + config = (struct sof_eq_fir_config *)abi->data; + config->channels_in_config = 1; + ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); + zassert_equal(ret, -EINVAL); + + /* Reject more responses than the configuration format supports. */ + copy_eq_fir_blob(blob_copy); + abi = (struct sof_abi_hdr *)blob_copy; + config = (struct sof_eq_fir_config *)abi->data; + config->number_of_responses = SOF_EQ_FIR_MAX_RESPONSES + 1; + ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); + zassert_equal(ret, -EINVAL); + + /* Reject an empty FIR response. */ + copy_eq_fir_blob(blob_copy); + abi = (struct sof_abi_hdr *)blob_copy; + config = (struct sof_eq_fir_config *)abi->data; + coef = (struct sof_fir_coef_data *)&config->data[config->channels_in_config]; + coef->length = 0; + ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); + zassert_equal(ret, -EINVAL); + + /* Reject a filter length that is not a multiple of four. */ + copy_eq_fir_blob(blob_copy); + abi = (struct sof_abi_hdr *)blob_copy; + config = (struct sof_eq_fir_config *)abi->data; + coef = (struct sof_fir_coef_data *)&config->data[config->channels_in_config]; + coef->length = 3; + ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); + zassert_equal(ret, -EINVAL); + + /* Reject a response whose coefficients extend past the blob. */ + copy_eq_fir_blob(blob_copy); + abi = (struct sof_abi_hdr *)blob_copy; + config = (struct sof_eq_fir_config *)abi->data; + coef = (struct sof_fir_coef_data *)&config->data[config->channels_in_config]; + coef->length = SOF_FIR_MAX_LENGTH; + ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); + zassert_equal(ret, -EINVAL); + + /* Reject a channel assignment that references an undefined response. */ + copy_eq_fir_blob(blob_copy); + abi = (struct sof_abi_hdr *)blob_copy; + config = (struct sof_eq_fir_config *)abi->data; + config->data[0] = 1; + ret = eq_fir_send_blob(mod, abi, sizeof(blob_copy)); + zassert_equal(ret, -EINVAL); +} + +#if CONFIG_IPC_MAJOR_3 +/** + * @brief Verify that an unknown frame format has no processing function. + */ +ZTEST_F(eq_fir_process, test_eq_fir_rejects_invalid_frame_format) +{ + struct processing_module *mod; + + eq_fir_setup(fixture, ¶meters[0]); + mod = comp_mod(fixture->dev); + + zassert_equal(set_fir_func(mod, (enum sof_ipc_frame)0xffff), -EINVAL); +} +#endif + +/** + * @brief Register the FIR EQ driver, once for the whole suite. + */ +static void *eq_fir_process_setup(void) +{ + sys_comp_init(sof_get()); + sys_comp_module_eq_fir_interface_init(); + +#ifdef DEBUG_FILES + /* Written to the current directory, the suite build directory with twister */ + debug_fh_16 = fopen("fir_test_16.txt", "w"); + debug_fh_24 = fopen("fir_test_24.txt", "w"); + debug_fh_32 = fopen("fir_test_32.txt", "w"); +#endif + + return &eq_fir_fixture; +} + +#ifdef DEBUG_FILES +/** + * @brief Close the debug output files. + */ +static void eq_fir_process_teardown(void *data) +{ + (void)data; + + fclose(debug_fh_16); + fclose(debug_fh_24); + fclose(debug_fh_32); +} +#else +#define eq_fir_process_teardown NULL +#endif + +/** + * @brief Start every test case without a component or buffers. + */ +static void eq_fir_before(void *data) +{ + struct eq_fir_process_fixture *fx = data; + + memset(fx, 0, sizeof(*fx)); +} + +/** + * @brief Release the component and buffers of a test case. + */ +static void eq_fir_after(void *data) +{ + struct eq_fir_process_fixture *fx = data; + struct processing_module *mod; + + if (fx->dev) { + mod = comp_mod(fx->dev); + free(mod->stream_params); + mod->stream_params = NULL; + } + + free_test_source(fx->source); + free_test_sink(fx->sink); + if (fx->dev) + comp_free(fx->dev); + + memset(fx, 0, sizeof(*fx)); +} + +ZTEST_SUITE(eq_fir_process, NULL, eq_fir_process_setup, eq_fir_before, eq_fir_after, + eq_fir_process_teardown); diff --git a/test/ztest/unit/audio/eq_fir/prj.conf b/test/ztest/unit/audio/eq_fir/prj.conf new file mode 100644 index 000000000000..08416e7c2f02 --- /dev/null +++ b/test/ztest/unit/audio/eq_fir/prj.conf @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n +CONFIG_COMP_FIR=y +CONFIG_FORMAT_S16LE=y +CONFIG_FORMAT_S24LE=y +CONFIG_FORMAT_S32LE=y +# Route the SOF heap to the host malloc()/free() of the native simulator, so +# that ASan and LSan can see it. They cannot see blocks carved out of the +# static sys_heap arena that zephyr/lib/alloc.c uses by default. +CONFIG_SOF_NATIVE_SIM_HOST_HEAP=y +# The sys_heap arena is still reserved and initialised by heap_init(), and its +# 256 KiB are too large for SYS_HEAP_SMALL_ONLY (see app/boards/native_sim.conf) +CONFIG_SYS_HEAP_BIG_ONLY=y diff --git a/test/ztest/unit/audio/eq_fir/testcase.yaml b/test/ztest/unit/audio/eq_fir/testcase.yaml new file mode 100644 index 000000000000..a0c1575e3955 --- /dev/null +++ b/test/ztest/unit/audio/eq_fir/testcase.yaml @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# FIR equalizer processing unit tests converted from CMock to Ztest +# +# These contents may have been developed with support from one or more Intel-operated +# generative artificial intelligence solutions. +# + +common: + tags: + - SOF + - unit_test + - audio + - eq_fir + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.audio.eq_fir: + platform_allow: + - native_sim + build_only: false diff --git a/test/ztest/unit/audio/eq_iir/CMakeLists.txt b/test/ztest/unit/audio/eq_iir/CMakeLists.txt new file mode 100644 index 000000000000..f19a9b0d2483 --- /dev/null +++ b/test/ztest/unit/audio/eq_iir/CMakeLists.txt @@ -0,0 +1,74 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# IIR equalizer processing unit tests converted from CMock to Ztest + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(sof_unit_audio_eq_iir) + +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/include + ${SOF_ROOT}/src/kernel/platform/posix/include + ${SOF_ROOT}/src/platform/posix/include + ${SOF_ROOT}/src/audio + # cmocka_chirp_2ch.h, still shared with the remaining CMocka tests + ${SOF_ROOT}/test/cmocka/include + ${PROJECT_BINARY_DIR}/include/generated +) + +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_LIBRARY=1 + -DUNIT_TEST=1 +) + +# Keep full ASan/LSan allocation stacks (twister --enable-asan --enable-lsan) +if(CONFIG_ASAN) + target_compile_options(app PRIVATE -fno-omit-frame-pointer) +endif() + +target_sources(app PRIVATE + eq_iir_process_ztest.c + ${SOF_ROOT}/test/ztest/unit/common/sof_context.c + ${SOF_ROOT}/zephyr/lib/alloc.c + ${SOF_ROOT}/src/audio/eq_iir/eq_iir.c + ${SOF_ROOT}/src/audio/eq_iir/eq_iir_generic.c + ${SOF_ROOT}/src/audio/eq_iir/eq_iir_ipc3.c + ${SOF_ROOT}/src/math/iir_df1.c + ${SOF_ROOT}/src/math/iir_df1_float.c + ${SOF_ROOT}/src/math/iir_df1_generic.c + # The HiFi variants compile to nothing on native_sim + ${SOF_ROOT}/src/math/iir_df1_hifi3.c + ${SOF_ROOT}/src/math/iir_df1_hifi4.c + ${SOF_ROOT}/src/math/iir_df1_hifi5.c + ${SOF_ROOT}/src/math/numbers.c + ${SOF_ROOT}/src/audio/module_adapter/module_adapter.c + ${SOF_ROOT}/src/audio/module_adapter/module_adapter_ipc3.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/kernel/ipc/ipc3/helper.c + ${SOF_ROOT}/src/ipc/ipc-helper.c + ${SOF_ROOT}/src/lib/objpool.c +) + +sof_append_relative_path_definitions(app) diff --git a/test/cmocka/src/audio/eq_iir/cmocka_chirp_iir_ref_2ch.h b/test/ztest/unit/audio/eq_iir/cmocka_chirp_iir_ref_2ch.h similarity index 100% rename from test/cmocka/src/audio/eq_iir/cmocka_chirp_iir_ref_2ch.h rename to test/ztest/unit/audio/eq_iir/cmocka_chirp_iir_ref_2ch.h diff --git a/test/cmocka/src/audio/eq_iir/cmocka_iir_coef_2ch.h b/test/ztest/unit/audio/eq_iir/cmocka_iir_coef_2ch.h similarity index 100% rename from test/cmocka/src/audio/eq_iir/cmocka_iir_coef_2ch.h rename to test/ztest/unit/audio/eq_iir/cmocka_iir_coef_2ch.h diff --git a/test/ztest/unit/audio/eq_iir/eq_iir_process_ztest.c b/test/ztest/unit/audio/eq_iir/eq_iir_process_ztest.c new file mode 100644 index 000000000000..32cf5d3eb6b3 --- /dev/null +++ b/test/ztest/unit/audio/eq_iir/eq_iir_process_ztest.c @@ -0,0 +1,783 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/eq_iir/eq_iir_process.c +// Author: Seppo Ingalsuo + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +/* Shared with the remaining CMocka tests, from test/cmocka/include */ +#include + +#include "cmocka_chirp_iir_ref_2ch.h" +#include "cmocka_iir_coef_2ch.h" + +/* Allow some small error for fixed point. In IIR case due to float + * reference with float coefficients the difference can be quite + * large to scaled integer bi-quads. This could be re-visited with + * a more implementation like reference in Octave test vector + * generate script. + */ +#define ERROR_TOLERANCE_S16 2 +#define ERROR_TOLERANCE_S24 128 +#define ERROR_TOLERANCE_S32 32768 + +/* Thresholds for frames count jitter for rand() function */ +#define THR_RAND_PLUS_ONE ((RAND_MAX >> 1) + (RAND_MAX >> 2)) +#define THR_RAND_MINUS_ONE ((RAND_MAX >> 1) - (RAND_MAX >> 2)) + +/** @brief Stream configuration of one test case. */ +struct test_parameters { + uint32_t channels; + uint32_t frames; + uint32_t buffer_size_mult; + uint32_t source_format; + uint32_t sink_format; +}; + +/** @brief Component, buffers and stream positions of the running test case. */ +struct eq_iir_process_fixture { + struct comp_dev *dev; + struct comp_buffer *sink; + struct comp_buffer *source; + const struct test_parameters *params; + bool continue_loop; + size_t input_frames; + size_t output_bytes; + int fill_idx; + int verify_idx; +}; + +static struct eq_iir_process_fixture eq_iir_fixture; + +/* Each parameter set is run through the whole chirp. */ +static const struct test_parameters parameters[] = { +#if CONFIG_FORMAT_S16LE + { 2, 48, 2, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE }, +#endif /* CONFIG_FORMAT_S16LE */ +#if CONFIG_FORMAT_S24LE + { 2, 48, 2, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE }, +#endif /* CONFIG_FORMAT_S24LE */ +#if CONFIG_FORMAT_S32LE + { 2, 48, 2, SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S32_LE }, +#endif /* CONFIG_FORMAT_S32LE */ +#if CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S16LE + { 2, 48, 2, SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S16_LE }, +#endif /* CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S16LE */ +#if CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S24LE + { 2, 48, 2, SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S24_4LE }, +#endif /* CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S24LE */ +}; + +/** + * @brief Look up the parameter set of a source and sink format pair. + */ +static const struct test_parameters *get_parameters(uint32_t source_format, + uint32_t sink_format) +{ + size_t i; + + for (i = 0; i < ARRAY_SIZE(parameters); i++) + if (parameters[i].source_format == source_format && + parameters[i].sink_format == sink_format) + return ¶meters[i]; + + zassert_unreachable("No parameters for formats %u -> %u", source_format, sink_format); + return NULL; +} + +/** + * @brief Create a test sink buffer connected to a component. + */ +static struct comp_buffer *create_test_sink(struct comp_dev *dev, + uint32_t pipeline_id, + uint32_t frame_fmt, + uint16_t channels, + uint16_t buffer_size) +{ + struct sof_ipc_buffer desc = { + .comp = { + .pipeline_id = pipeline_id, + }, + .size = buffer_size, + }; + struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); + + if (!buffer) + return NULL; + + memset(buffer->stream.addr, 0, buffer_size); + + buffer->sink = calloc(1, sizeof(*buffer->sink)); + if (!buffer->sink) { + buffer_free(buffer); + return NULL; + } + + if (dev) + list_item_append(&buffer->source_list, &dev->bsink_list); + + buffer->sink->state = COMP_STATE_PREPARE; + audio_stream_set_frm_fmt(&buffer->stream, frame_fmt); + audio_stream_set_channels(&buffer->stream, channels); + + return buffer; +} + +/** + * @brief Release a test sink buffer and its component endpoint. + */ +static void free_test_sink(struct comp_buffer *buffer) +{ + if (!buffer) + return; + + free(comp_buffer_get_sink_component(buffer)); + buffer_free(buffer); +} + +/** + * @brief Create a test source buffer connected to a component. + */ +static struct comp_buffer *create_test_source(struct comp_dev *dev, + uint32_t pipeline_id, + uint32_t frame_fmt, + uint16_t channels, + uint16_t buffer_size) +{ + struct sof_ipc_buffer desc = { + .comp = { + .pipeline_id = pipeline_id, + }, + .size = buffer_size, + }; + struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); + + if (!buffer) + return NULL; + + memset(buffer->stream.addr, 0, buffer_size); + + buffer->source = calloc(1, sizeof(*buffer->source)); + if (!buffer->source) { + buffer_free(buffer); + return NULL; + } + + if (dev) + list_item_append(&buffer->sink_list, &dev->bsource_list); + + buffer->source->state = COMP_STATE_PREPARE; + audio_stream_set_frm_fmt(&buffer->stream, frame_fmt); + audio_stream_set_channels(&buffer->stream, channels); + + return buffer; +} + +/** + * @brief Release a test source buffer and its component endpoint. + */ +static void free_test_source(struct comp_buffer *buffer) +{ + if (!buffer) + return; + + free(buffer->source); + buffer_free(buffer); +} + +/** + * @brief Create the IPC description used to instantiate the IIR EQ module. + */ +static struct sof_ipc_comp_process *create_eq_iir_comp_ipc(void) +{ + struct sof_ipc_comp_process *ipc; + const size_t ipc_size = sizeof(*ipc); + const struct sof_uuid uuid = SOF_REG_UUID(eq_iir); + + ipc = calloc(1, ipc_size + SOF_UUID_SIZE); + if (!ipc) + return NULL; + + memcpy_s(ipc + 1, SOF_UUID_SIZE, &uuid, SOF_UUID_SIZE); + ipc->comp.hdr.size = ipc_size + SOF_UUID_SIZE; + ipc->comp.type = SOF_COMP_MODULE_ADAPTER; + ipc->config.hdr.size = sizeof(struct sof_ipc_comp_config); + ipc->size = 0; + ipc->comp.ext_data_length = SOF_UUID_SIZE; + + return ipc; +} + +/** + * @brief Send the reference two channel IIR EQ configuration. + */ +static int eq_iir_send_config(struct processing_module *mod) +{ + const struct module_interface *const ops = mod->dev->drv->adapter_ops; + const struct sof_abi_hdr *blob = (const struct sof_abi_hdr *)iir_coef_2ch; + const size_t cdata_size = sizeof(struct sof_ipc_ctrl_data) + + sizeof(struct sof_abi_hdr) + blob->size; + struct sof_ipc_ctrl_data *cdata; + int ret; + + cdata = calloc(1, cdata_size); + if (!cdata) + return -ENOMEM; + + cdata->cmd = SOF_CTRL_CMD_BINARY; + cdata->num_elems = blob->size; + cdata->data[0].magic = blob->magic; + cdata->data[0].type = blob->type; + cdata->data[0].size = blob->size; + cdata->data[0].abi = blob->abi; + memcpy_s(cdata->data[0].data, blob->size, blob->data, blob->size); + + ret = ops->set_configuration(mod, 0, MODULE_CFG_FRAGMENT_SINGLE, + blob->size, (const uint8_t *)cdata, + blob->size, NULL, 0); + + free(cdata); + return ret; +} + +/** + * @brief Allocate the sink buffer and set the module output buffer size. + */ +static void prepare_sink(struct eq_iir_process_fixture *fx, struct processing_module *mod) +{ + const struct test_parameters *parameters = fx->params; + struct module_data *md = &mod->priv; + size_t size; + size_t free_bytes; + + /* allocate new sink buffer */ + size = parameters->frames * get_frame_bytes(parameters->sink_format, parameters->channels) * + parameters->buffer_size_mult; + + md->mpd.out_buff_size = parameters->frames * get_frame_bytes(parameters->sink_format, + parameters->channels); + + fx->sink = create_test_sink(fx->dev, 0, parameters->sink_format, + parameters->channels, size); + zassert_not_null(fx->sink, "Failed to create the sink buffer"); + free_bytes = audio_stream_get_free_bytes(&fx->sink->stream); + zassert_equal(free_bytes, size); +} + +/** + * @brief Allocate the source buffer and set the module input buffer size. + */ +static void prepare_source(struct eq_iir_process_fixture *fx, struct processing_module *mod) +{ + const struct test_parameters *parameters = fx->params; + struct module_data *md = &mod->priv; + size_t size; + size_t free_bytes; + + md->mpd.in_buff_size = parameters->frames * get_frame_bytes(parameters->source_format, + parameters->channels); + + size = parameters->frames * get_frame_bytes(parameters->source_format, + parameters->channels) * parameters->buffer_size_mult; + + fx->source = create_test_source(fx->dev, 0, parameters->source_format, + parameters->channels, size); + zassert_not_null(fx->source, "Failed to create the source buffer"); + free_bytes = audio_stream_get_free_bytes(&fx->source->stream); + zassert_equal(free_bytes, size); +} + +/** + * @brief Create, configure and prepare the IIR EQ module for one test case. + * + * Anything allocated here is released by eq_iir_after(), also when an + * assertion fails half way. + */ +static void eq_iir_test_setup(struct eq_iir_process_fixture *fx, const struct test_parameters *params) +{ + struct processing_module *mod; + struct sof_ipc_comp_process *ipc; + struct sof_source *sources[1]; + struct sof_sink *sinks[1]; + int ret; + + fx->params = params; + fx->fill_idx = 0; + fx->verify_idx = 0; + + ipc = create_eq_iir_comp_ipc(); + zassert_not_null(ipc, "Failed to allocate the component IPC"); + + fx->dev = comp_new((struct sof_ipc_comp *)ipc); + free(ipc); + zassert_not_null(fx->dev, "Failed to create the IIR EQ component"); + + fx->dev->frames = params->frames; + mod = comp_mod(fx->dev); + + ret = eq_iir_send_config(mod); + zassert_equal(ret, 0, "Sending the configuration failed: %d", ret); + + prepare_sink(fx, mod); + prepare_source(fx, mod); + + /* Zeroed, CMocka test_malloc() filled all but channels with 0xba bytes */ + mod->stream_params = calloc(1, sizeof(*mod->stream_params)); + zassert_not_null(mod->stream_params, "Failed to allocate the stream parameters"); + mod->stream_params->channels = params->channels; + mod->period_bytes = get_frame_bytes(params->source_format, params->channels) * 48000 / 1000; + + sources[0] = audio_buffer_get_source(&fx->source->audio_buffer); + sinks[0] = audio_buffer_get_sink(&fx->sink->audio_buffer); + ret = module_prepare(mod, sources, 1, sinks, 1); + zassert_equal(ret, 0, "module_prepare() failed: %d", ret); + + fx->continue_loop = true; +} + +#if CONFIG_FORMAT_S16LE +/** + * @brief Write up to @p frames_max frames of the chirp to the S16 source buffer. + */ +static void fill_source_s16(struct eq_iir_process_fixture *fx, int frames_max) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int16_t *x; + int bytes_total; + int samples; + int frames; + int i; + int samples_processed = 0; + + sb = comp_dev_get_first_data_producer(fx->dev); + ss = &sb->stream; + frames = MIN(audio_stream_get_free_frames(ss), frames_max); + samples = frames * audio_stream_get_channels(ss); + for (i = 0; i < samples; i++) { + x = audio_stream_write_frag_s16(ss, i); + *x = sat_int16(Q_SHIFT_RND(chirp_2ch[fx->fill_idx++], 31, 15)); + samples_processed++; + if (fx->fill_idx == CHIRP_2CH_LENGTH) { + fx->continue_loop = false; + break; + } + } + + if (samples_processed > 0) { + bytes_total = samples_processed * audio_stream_sample_bytes(ss); + comp_update_buffer_produce(sb, bytes_total); + } + + fx->input_frames = samples_processed / audio_stream_get_channels(ss); +} + +/** + * @brief Compare the S16 output of the last processing call to the reference. + */ +static void verify_sink_s16(struct eq_iir_process_fixture *fx) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int32_t delta; + int32_t ref; + int32_t out; + int16_t *x; + int samples; + int i; + + sb = comp_dev_get_first_data_consumer(fx->dev); + ss = &sb->stream; + samples = fx->output_bytes >> 1; + for (i = 0; i < samples; i++) { + x = audio_stream_read_frag_s16(ss, i); + out = *x; + ref = sat_int16(Q_SHIFT_RND(chirp_iir_ref_2ch[fx->verify_idx++], 31, 15)); + delta = ref - out; + if (delta > ERROR_TOLERANCE_S16 || delta < -ERROR_TOLERANCE_S16) + zassert_equal(out, ref, "S16 sample %d: output %d, reference %d", + fx->verify_idx - 1, out, ref); + } +} +#endif /* CONFIG_FORMAT_S16LE */ + +#if CONFIG_FORMAT_S24LE +/** + * @brief Write up to @p frames_max frames of the chirp to the S24 source buffer. + */ +static void fill_source_s24(struct eq_iir_process_fixture *fx, int frames_max) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int32_t *x; + int bytes_total; + int samples; + int frames; + int i; + int samples_processed = 0; + + sb = comp_dev_get_first_data_producer(fx->dev); + ss = &sb->stream; + frames = MIN(audio_stream_get_free_frames(ss), frames_max); + samples = frames * audio_stream_get_channels(ss); + for (i = 0; i < samples; i++) { + x = audio_stream_write_frag_s32(ss, i); + *x = sat_int24(Q_SHIFT_RND(chirp_2ch[fx->fill_idx++], 31, 23)); + samples_processed++; + if (fx->fill_idx == CHIRP_2CH_LENGTH) { + fx->continue_loop = false; + break; + } + } + + if (samples_processed > 0) { + bytes_total = samples_processed * audio_stream_sample_bytes(ss); + comp_update_buffer_produce(sb, bytes_total); + } + + fx->input_frames = samples_processed / audio_stream_get_channels(ss); +} + +/** + * @brief Compare the S24 output of the last processing call to the reference. + */ +static void verify_sink_s24(struct eq_iir_process_fixture *fx) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int32_t delta; + int32_t ref; + int32_t out; + int32_t *x; + int samples; + int i; + + sb = comp_dev_get_first_data_consumer(fx->dev); + ss = &sb->stream; + samples = fx->output_bytes >> 2; + for (i = 0; i < samples; i++) { + x = audio_stream_read_frag_s32(ss, i); + out = (*x << 8) >> 8; /* Make sure there's no 24 bit overflow */ + ref = sat_int24(Q_SHIFT_RND(chirp_iir_ref_2ch[fx->verify_idx++], 31, 23)); + delta = ref - out; + if (delta > ERROR_TOLERANCE_S24 || delta < -ERROR_TOLERANCE_S24) + zassert_equal(out, ref, "S24 sample %d: output %d, reference %d", + fx->verify_idx - 1, out, ref); + } +} +#endif /* CONFIG_FORMAT_S24LE */ + +#if CONFIG_FORMAT_S32LE +/** + * @brief Write up to @p frames_max frames of the chirp to the S32 source buffer. + */ +static void fill_source_s32(struct eq_iir_process_fixture *fx, int frames_max) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int32_t *x; + int bytes_total; + int samples; + int frames; + int i; + int samples_processed = 0; + + sb = comp_dev_get_first_data_producer(fx->dev); + ss = &sb->stream; + frames = MIN(audio_stream_get_free_frames(ss), frames_max); + samples = frames * audio_stream_get_channels(ss); + for (i = 0; i < samples; i++) { + x = audio_stream_write_frag_s32(ss, i); + *x = chirp_2ch[fx->fill_idx++]; + samples_processed++; + if (fx->fill_idx == CHIRP_2CH_LENGTH) { + fx->continue_loop = false; + break; + } + } + + if (samples_processed > 0) { + bytes_total = samples_processed * audio_stream_sample_bytes(ss); + comp_update_buffer_produce(sb, bytes_total); + } + + fx->input_frames = samples_processed / audio_stream_get_channels(ss); +} + +/** + * @brief Compare the S32 output of the last processing call to the reference. + */ +static void verify_sink_s32(struct eq_iir_process_fixture *fx) +{ + struct comp_buffer *sb; + struct audio_stream *ss; + int64_t delta; + int32_t ref; + int32_t out; + int32_t *x; + int samples; + int i; + + sb = comp_dev_get_first_data_consumer(fx->dev); + ss = &sb->stream; + samples = fx->output_bytes >> 2; + for (i = 0; i < samples; i++) { + x = audio_stream_read_frag_s32(ss, i); + out = *x; + ref = chirp_iir_ref_2ch[fx->verify_idx++]; + delta = (int64_t)ref - (int64_t)out; + if (delta > ERROR_TOLERANCE_S32 || delta < -ERROR_TOLERANCE_S32) + zassert_equal(out, ref, "S32 sample %d: output %d, reference %d", + fx->verify_idx - 1, out, ref); + } +} +#endif /* CONFIG_FORMAT_S32LE */ + +/** + * @brief Fill the source buffer with up to @p frames frames of the chirp. + */ +static void fill_source_for_test(struct eq_iir_process_fixture *fx, int frames) +{ + switch (audio_stream_get_frm_fmt(&fx->source->stream)) { +#if CONFIG_FORMAT_S16LE + case SOF_IPC_FRAME_S16_LE: + fill_source_s16(fx, frames); + break; +#endif +#if CONFIG_FORMAT_S24LE + case SOF_IPC_FRAME_S24_4LE: + fill_source_s24(fx, frames); + break; +#endif +#if CONFIG_FORMAT_S32LE + case SOF_IPC_FRAME_S32_LE: + fill_source_s32(fx, frames); + break; +#endif + default: + zassert_unreachable("Unsupported source format"); + break; + } +} + +/** + * @brief Check the output of the last processing call against the reference. + */ +static void verify_sink_for_test(struct eq_iir_process_fixture *fx) +{ + switch (audio_stream_get_frm_fmt(&fx->sink->stream)) { +#if CONFIG_FORMAT_S16LE + case SOF_IPC_FRAME_S16_LE: + verify_sink_s16(fx); + break; +#endif +#if CONFIG_FORMAT_S24LE + case SOF_IPC_FRAME_S24_4LE: + verify_sink_s24(fx); + break; +#endif +#if CONFIG_FORMAT_S32LE + case SOF_IPC_FRAME_S32_LE: + verify_sink_s32(fx); + break; +#endif + default: + zassert_unreachable("Unsupported sink format"); + break; + } +} + +/** + * @brief Randomly add or remove one frame to vary the processed block size. + */ +static int frames_jitter(int frames) +{ + int r = rand(); + + if (r > THR_RAND_PLUS_ONE) + return frames + 1; + else if (r < THR_RAND_MINUS_ONE) + return frames - 1; + else + return frames; +} + +/** + * @brief Run the whole chirp through the IIR EQ and compare to the reference. + */ +static void run_eq_iir_test(struct eq_iir_process_fixture *fx, const struct test_parameters *params) +{ + struct processing_module *mod; + struct sof_source *sources[1]; + struct sof_sink *sinks[1]; + size_t sink_avail_before; + int frames; + int ret; + + eq_iir_test_setup(fx, params); + mod = comp_mod(fx->dev); + + sources[0] = audio_buffer_get_source(&fx->source->audio_buffer); + sinks[0] = audio_buffer_get_sink(&fx->sink->audio_buffer); + + while (fx->continue_loop) { + frames = frames_jitter(fx->params->frames); + fill_source_for_test(fx, frames); + + sink_avail_before = audio_stream_get_avail_bytes(&fx->sink->stream); + ret = module_process_sink_src(mod, sources, 1, sinks, 1); + zassert_equal(ret, 0, "module_process_sink_src() failed: %d", ret); + + fx->output_bytes = audio_stream_get_avail_bytes(&fx->sink->stream) - + sink_avail_before; + + verify_sink_for_test(fx); + + comp_update_buffer_consume(fx->sink, fx->output_bytes); + } +} + +#if CONFIG_FORMAT_S16LE +/** + * @brief Verify the IIR EQ S16 to S16 output against the reference. + */ +ZTEST_F(eq_iir_process, test_audio_eq_iir_s16) +{ + run_eq_iir_test(fixture, get_parameters(SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE)); +} +#endif /* CONFIG_FORMAT_S16LE */ + +#if CONFIG_FORMAT_S24LE +/** + * @brief Verify the IIR EQ S24 to S24 output against the reference. + */ +ZTEST_F(eq_iir_process, test_audio_eq_iir_s24) +{ + run_eq_iir_test(fixture, get_parameters(SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE)); +} +#endif /* CONFIG_FORMAT_S24LE */ + +#if CONFIG_FORMAT_S32LE +/** + * @brief Verify the IIR EQ S32 to S32 output against the reference. + */ +ZTEST_F(eq_iir_process, test_audio_eq_iir_s32) +{ + run_eq_iir_test(fixture, get_parameters(SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S32_LE)); +} +#endif /* CONFIG_FORMAT_S32LE */ + +#if CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S16LE +/** + * @brief Verify the IIR EQ S32 to S16 output against the reference. + */ +ZTEST_F(eq_iir_process, test_audio_eq_iir_s32_s16) +{ + run_eq_iir_test(fixture, get_parameters(SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S16_LE)); +} +#endif /* CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S16LE */ + +#if CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S24LE +/** + * @brief Verify the IIR EQ S32 to S24 output against the reference. + */ +ZTEST_F(eq_iir_process, test_audio_eq_iir_s32_s24) +{ + run_eq_iir_test(fixture, get_parameters(SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_S24_4LE)); +} +#endif /* CONFIG_FORMAT_S32LE && CONFIG_FORMAT_S24LE */ + +/** + * @brief Register the IIR EQ driver, once for the whole suite. + */ +static void *eq_iir_process_setup(void) +{ + sys_comp_init(sof_get()); + sys_comp_module_eq_iir_interface_init(); + + return &eq_iir_fixture; +} + +/** + * @brief Start every test case without a component or buffers. + */ +static void eq_iir_before(void *data) +{ + struct eq_iir_process_fixture *fx = data; + + memset(fx, 0, sizeof(*fx)); +} + +/** + * @brief Release the component and buffers of a test case. + */ +static void eq_iir_after(void *data) +{ + struct eq_iir_process_fixture *fx = data; + struct processing_module *mod; + + if (fx->dev) { + mod = comp_mod(fx->dev); + free(mod->stream_params); + mod->stream_params = NULL; + } + + free_test_source(fx->source); + free_test_sink(fx->sink); + if (fx->dev) + comp_free(fx->dev); + + memset(fx, 0, sizeof(*fx)); +} + +ZTEST_SUITE(eq_iir_process, NULL, eq_iir_process_setup, eq_iir_before, eq_iir_after, NULL); + +/** + * @brief Verify the DF1 delay line size for valid and invalid series counts. + */ +ZTEST(eq_iir_validator, test_iir_delay_size_df1_series) +{ + struct sof_eq_iir_header hdr = { .num_sections = 11 }; + const size_t size = 4 * 11 * sizeof(int32_t); + + hdr.num_sections_in_series = 11; + zassert_equal(iir_delay_size_df1(&hdr), size); + + hdr.num_sections_in_series = 1; + zassert_equal(iir_delay_size_df1(&hdr), size); + + /* iir_df1() would run 20 biquads against 11 sections of state */ + hdr.num_sections_in_series = 10; + zassert_equal(iir_delay_size_df1(&hdr), 0); +} + +ZTEST_SUITE(eq_iir_validator, NULL, NULL, NULL, NULL, NULL); diff --git a/test/ztest/unit/audio/eq_iir/prj.conf b/test/ztest/unit/audio/eq_iir/prj.conf new file mode 100644 index 000000000000..13e2e88a1998 --- /dev/null +++ b/test/ztest/unit/audio/eq_iir/prj.conf @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n +CONFIG_COMP_IIR=y +CONFIG_FORMAT_S16LE=y +CONFIG_FORMAT_S24LE=y +CONFIG_FORMAT_S32LE=y +# Route the SOF heap to the host malloc()/free() of the native simulator, so +# that ASan and LSan can see it. They cannot see blocks carved out of the +# static sys_heap arena that zephyr/lib/alloc.c uses by default. +CONFIG_SOF_NATIVE_SIM_HOST_HEAP=y +# The sys_heap arena is still reserved and initialised by heap_init(), and its +# 256 KiB are too large for SYS_HEAP_SMALL_ONLY (see app/boards/native_sim.conf) +CONFIG_SYS_HEAP_BIG_ONLY=y diff --git a/test/ztest/unit/audio/eq_iir/testcase.yaml b/test/ztest/unit/audio/eq_iir/testcase.yaml new file mode 100644 index 000000000000..d2aba599a06f --- /dev/null +++ b/test/ztest/unit/audio/eq_iir/testcase.yaml @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# IIR equalizer processing unit tests converted from CMock to Ztest +# +# These contents may have been developed with support from one or more Intel-operated +# generative artificial intelligence solutions. +# + +common: + tags: + - SOF + - unit_test + - audio + - eq_iir + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.audio.eq_iir: + platform_allow: + - native_sim + build_only: false diff --git a/test/ztest/unit/audio/mixer/CMakeLists.txt b/test/ztest/unit/audio/mixer/CMakeLists.txt new file mode 100644 index 000000000000..533349f84c58 --- /dev/null +++ b/test/ztest/unit/audio/mixer/CMakeLists.txt @@ -0,0 +1,58 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# Mixer unit tests converted from CMock to Ztest + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(sof_unit_audio_mixer) + +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/include + ${SOF_ROOT}/src/kernel/platform/posix/include + ${SOF_ROOT}/src/platform/posix/include + ${SOF_ROOT}/src/audio + ${PROJECT_BINARY_DIR}/include/generated +) + +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_LIBRARY=1 + -DUNIT_TEST=1 +) + +# Keep full ASan/LSan allocation stacks (twister --enable-asan --enable-lsan) +if(CONFIG_ASAN) + target_compile_options(app PRIVATE -fno-omit-frame-pointer) +endif() + +target_sources(app PRIVATE + mixer_ztest.c + # the generic C mixing functions under test + ${SOF_ROOT}/src/audio/mixer/mixer_generic.c + # the test buffers + ${SOF_ROOT}/zephyr/lib/alloc.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/module/audio/source_api.c + ${SOF_ROOT}/src/module/audio/sink_api.c + ${SOF_ROOT}/src/ipc/ipc-helper.c + # audio_stream_recalc_align() needs gcd() + ${SOF_ROOT}/src/math/numbers.c +) + +sof_append_relative_path_definitions(app) diff --git a/test/ztest/unit/audio/mixer/mixer_ztest.c b/test/ztest/unit/audio/mixer/mixer_ztest.c new file mode 100644 index 000000000000..ca28b994f0f0 --- /dev/null +++ b/test/ztest/unit/audio/mixer/mixer_ztest.c @@ -0,0 +1,222 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/mixer/mixer_test.c: +// Author: Slawomir Blauciak + +#include + +#include +#include +#include +#include + +#include "mixer/mixer.h" + +#include +#include +#include +#include + +/* math.h's M_PI is only exposed under glibc's GNU/POSIX feature test macros, + * which Zephyr's strict -std=c17 build does not define. + */ +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +#define TEST_FRAMES 48 +#define TEST_BUFFER_SIZE_MS 1 +#define TEST_MAX_SOURCES 8 + +struct mix_test_state { + struct comp_dev *dev; + struct comp_buffer *sources[TEST_MAX_SOURCES]; + struct comp_buffer *sink; + struct mixer_data md; + int num_sources; + int num_chans; +}; + +/** + * @brief Create a test sink buffer connected to a component. + */ +static struct comp_buffer *create_test_sink(struct comp_dev *dev, uint32_t frame_fmt, + uint16_t channels, uint16_t buffer_size) +{ + struct sof_ipc_buffer desc = { + .size = buffer_size, + }; + struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); + + zassert_not_null(buffer, "Failed to allocate the sink buffer"); + memset(buffer->stream.addr, 0, buffer_size); + + list_item_append(&buffer->source_list, &dev->bsink_list); + + buffer->sink = calloc(1, sizeof(*buffer->sink)); + zassert_not_null(buffer->sink); + buffer->sink->state = COMP_STATE_PREPARE; + audio_stream_set_frm_fmt(&buffer->stream, frame_fmt); + audio_stream_set_channels(&buffer->stream, channels); + + return buffer; +} + +/** + * @brief Create a test source buffer connected to a component. + */ +static struct comp_buffer *create_test_source(struct comp_dev *dev, uint32_t frame_fmt, + uint16_t channels, uint16_t buffer_size) +{ + struct sof_ipc_buffer desc = { + .size = buffer_size, + }; + struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); + + zassert_not_null(buffer, "Failed to allocate a source buffer"); + memset(buffer->stream.addr, 0, buffer_size); + + list_item_append(&buffer->sink_list, &dev->bsource_list); + + buffer->source = calloc(1, sizeof(*buffer->source)); + zassert_not_null(buffer->source); + buffer->source->state = COMP_STATE_PREPARE; + audio_stream_set_frm_fmt(&buffer->stream, frame_fmt); + audio_stream_set_channels(&buffer->stream, channels); + + return buffer; +} + +/** + * @brief Set up a component with S32_LE source buffers and one sink buffer. + * @param[out] state Test state to fill. + * @param[in] num_sources Number of mixer sources. + * @param[in] num_chans Number of channels of every stream. + */ +static void setup(struct mix_test_state *state, int num_sources, int num_chans) +{ + uint32_t size; + int i; + + memset(state, 0, sizeof(*state)); + state->num_sources = num_sources; + state->num_chans = num_chans; + + state->dev = calloc(1, sizeof(*state->dev)); + zassert_not_null(state->dev); + state->dev->frames = TEST_FRAMES; + list_init(&state->dev->bsource_list); + list_init(&state->dev->bsink_list); + + size = TEST_FRAMES * get_frame_bytes(SOF_IPC_FRAME_S32_LE, num_chans) * + TEST_BUFFER_SIZE_MS; + state->sink = create_test_sink(state->dev, SOF_IPC_FRAME_S32_LE, num_chans, size); + for (i = 0; i < num_sources; i++) + state->sources[i] = create_test_source(state->dev, SOF_IPC_FRAME_S32_LE, + num_chans, size); + + state->md.mix_func = mixer_get_processing_function(state->dev, + audio_stream_get_frm_fmt(&state->sink->stream)); + zassert_not_null(state->md.mix_func, "No S32_LE mixer processing function"); +} + +static void teardown(struct mix_test_state *state) +{ + int i; + + free(state->sink->sink); + buffer_free(state->sink); + for (i = 0; i < state->num_sources; i++) { + free(state->sources[i]->source); + buffer_free(state->sources[i]); + } + free(state->dev); +} + +/** + * @brief Mix sine waves from num_sources S32_LE sources and check that every + * output sample is the saturated sum of the input samples. + * @param[in] num_sources Number of mixer sources. + * @param[in] num_chans Number of channels of every stream. + */ +static void test_audio_mixer_copy(int num_sources, int num_chans) +{ + struct mix_test_state tc; + struct cir_buf_source sources_buf[CONFIG_PLATFORM_MAX_STREAMS]; + struct cir_buf_sink sink_buf; + int src_idx; + int smp; + + setup(&tc, num_sources, num_chans); + + for (src_idx = 0; src_idx < tc.num_sources; ++src_idx) { + uint32_t *samples = tc.sources[src_idx]->stream.addr; + + for (smp = 0; smp < tc.sources[src_idx]->stream.size / sizeof(int32_t); ++smp) { + double rad = M_PI / (180.0 / (smp * (src_idx + 1))); + + samples[smp] = ((sin(rad) + 1) / 2) * (0xFFFFFFFF / 2); + } + + audio_stream_produce(&tc.sources[src_idx]->stream, + tc.sources[src_idx]->stream.size / sizeof(int32_t)); + + sources_buf[src_idx].buf_start = + audio_stream_get_addr(&tc.sources[src_idx]->stream); + sources_buf[src_idx].buf_end = + audio_stream_get_end_addr(&tc.sources[src_idx]->stream); + sources_buf[src_idx].ptr = + audio_stream_get_rptr(&tc.sources[src_idx]->stream); + } + + sink_buf.buf_start = audio_stream_get_addr(&tc.sink->stream); + sink_buf.buf_end = audio_stream_get_end_addr(&tc.sink->stream); + sink_buf.ptr = audio_stream_get_wptr(&tc.sink->stream); + + tc.md.mix_func(&sink_buf, sources_buf, tc.num_sources, + tc.dev->frames * tc.num_chans); + + for (smp = 0; smp < tc.sink->stream.size / sizeof(int32_t); ++smp) { + uint64_t sum = 0; + + for (src_idx = 0; src_idx < tc.num_sources; ++src_idx) { + zassert_not_null(tc.sources[src_idx]); + + uint32_t *samples = tc.sources[src_idx]->stream.addr; + + sum += samples[smp]; + } + + sum = sat_int32(sum); + + uint32_t *out_samples = tc.sink->stream.addr; + + zassert_equal(out_samples[smp], sum, "sample %d: output %u, expected %llu", + smp, out_samples[smp], (unsigned long long)sum); + } + + teardown(&tc); +} + +#define MIXER_TEST_CASE(_num_sources, _num_chans) \ + ZTEST(audio_mixer_suite, \ + test_audio_mixer_copy_##_num_sources##_srcs_##_num_chans##ch) \ + { \ + test_audio_mixer_copy(_num_sources, _num_chans); \ + } + +MIXER_TEST_CASE(1, 2) +MIXER_TEST_CASE(1, 4) +MIXER_TEST_CASE(1, 8) +MIXER_TEST_CASE(2, 2) +MIXER_TEST_CASE(2, 4) +MIXER_TEST_CASE(2, 8) +MIXER_TEST_CASE(3, 2) +MIXER_TEST_CASE(4, 2) +MIXER_TEST_CASE(6, 2) +MIXER_TEST_CASE(8, 2) + +ZTEST_SUITE(audio_mixer_suite, NULL, NULL, NULL, NULL, NULL); diff --git a/test/ztest/unit/audio/mixer/prj.conf b/test/ztest/unit/audio/mixer/prj.conf new file mode 100644 index 000000000000..84664ec05afd --- /dev/null +++ b/test/ztest/unit/audio/mixer/prj.conf @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n +CONFIG_COMP_MIXER=y +CONFIG_FORMAT_S16LE=y +CONFIG_FORMAT_S24LE=y +CONFIG_FORMAT_S32LE=y +# Route the SOF heap to the host malloc()/free() of the native simulator, so +# that ASan and LSan can see it. They cannot see blocks carved out of the +# static sys_heap arena that zephyr/lib/alloc.c uses by default. +CONFIG_SOF_NATIVE_SIM_HOST_HEAP=y +# The sys_heap arena is still reserved and initialised by heap_init(), and its +# 256 KiB are too large for SYS_HEAP_SMALL_ONLY (see app/boards/native_sim.conf) +CONFIG_SYS_HEAP_BIG_ONLY=y diff --git a/test/ztest/unit/audio/mixer/testcase.yaml b/test/ztest/unit/audio/mixer/testcase.yaml new file mode 100644 index 000000000000..62dd69003666 --- /dev/null +++ b/test/ztest/unit/audio/mixer/testcase.yaml @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# Mixer unit tests converted from CMock to Ztest +# +# These contents may have been developed with support from one or more Intel-operated +# generative artificial intelligence solutions. +# + +common: + tags: + - SOF + - unit_test + - audio + - mixer + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.audio.mixer: + platform_allow: + - native_sim + build_only: false diff --git a/test/ztest/unit/audio/selector/CMakeLists.txt b/test/ztest/unit/audio/selector/CMakeLists.txt index c3082f21587f..57f4dcc45224 100644 --- a/test/ztest/unit/audio/selector/CMakeLists.txt +++ b/test/ztest/unit/audio/selector/CMakeLists.txt @@ -29,41 +29,61 @@ target_compile_definitions(app PRIVATE -DUNIT_TEST=1 ) -target_sources(app PRIVATE - selector_ztest.c - ${SOF_ROOT}/test/ztest/unit/common/alloc.c - ${SOF_ROOT}/test/ztest/unit/common/sof_context.c - ${SOF_ROOT}/src/audio/selector/selector.c - ${SOF_ROOT}/src/audio/selector/selector_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 -) +if(CONFIG_IPC_MAJOR_3) + # The cases ported from the CMocka selector test, which is built for IPC3 + # and tests the IPC3 channel selection functions in selector_generic.c + target_sources(app PRIVATE + selector_ipc3_ztest.c + ${SOF_ROOT}/test/ztest/unit/common/alloc.c + ${SOF_ROOT}/src/audio/selector/selector_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/module/audio/source_api.c + ${SOF_ROOT}/src/module/audio/sink_api.c + ${SOF_ROOT}/src/ipc/ipc-helper.c + ${SOF_ROOT}/src/math/numbers.c + ) +else() + target_sources(app PRIVATE + selector_ztest.c + ${SOF_ROOT}/test/ztest/unit/common/alloc.c + ${SOF_ROOT}/test/ztest/unit/common/sof_context.c + ${SOF_ROOT}/src/audio/selector/selector.c + ${SOF_ROOT}/src/audio/selector/selector_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 + ) + + # 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" + ) +endif() 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/selector/prj_ipc3.conf b/test/ztest/unit/audio/selector/prj_ipc3.conf new file mode 100644 index 000000000000..f8adcce0e55f --- /dev/null +++ b/test/ztest/unit/audio/selector/prj_ipc3.conf @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# IPC3 build of the suite, for the cases ported from the CMocka selector test + +CONFIG_ZTEST=y +CONFIG_LOG=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n +CONFIG_LIBRARY=y +CONFIG_IPC_MAJOR_3=y +CONFIG_COMP_SEL=y +CONFIG_STATIC_PIPELINE=n +CONFIG_FORMAT_S16LE=y +CONFIG_FORMAT_S24LE=y +CONFIG_FORMAT_S32LE=y diff --git a/test/ztest/unit/audio/selector/selector_ipc3_ztest.c b/test/ztest/unit/audio/selector/selector_ipc3_ztest.c new file mode 100644 index 000000000000..362ff555bcf3 --- /dev/null +++ b/test/ztest/unit/audio/selector/selector_ipc3_ztest.c @@ -0,0 +1,365 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/selector/selector_test.c: +// Author: Lech Betlej +// +// The CMocka test is built for IPC3 (all the CMocka defconfigs are IPC3), so +// it tests the IPC3 channel selection functions. This file is built in the +// IPC3 scenario of this suite (prj_ipc3.conf), selector_ztest.c in the IPC4 +// one. + +#include + +#include +#include +#include +#include + +#include +#include +#include + +struct sel_test_state { + struct comp_dev *dev; + struct comp_buffer *sink; + struct comp_buffer *source; + struct comp_data *cd; +}; + +struct sel_test_parameters { + uint32_t in_channels; + uint32_t out_channels; + uint32_t sel_channel; + uint32_t frames; + uint32_t buffer_size_ms; + uint32_t source_format; + uint32_t sink_format; + void (*verify)(struct comp_dev *dev, struct audio_stream *sink, + struct audio_stream *source); +}; + +/** + * @brief Create a test sink buffer connected to a component. + */ +static struct comp_buffer *create_test_sink(struct comp_dev *dev, uint32_t frame_fmt, + uint16_t channels, uint16_t buffer_size) +{ + struct sof_ipc_buffer desc = { + .size = buffer_size, + }; + struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); + + zassert_not_null(buffer, "Failed to allocate the sink buffer"); + memset(buffer->stream.addr, 0, buffer_size); + + list_item_append(&buffer->source_list, &dev->bsink_list); + + buffer->sink = calloc(1, sizeof(*buffer->sink)); + zassert_not_null(buffer->sink); + buffer->sink->state = COMP_STATE_PREPARE; + audio_stream_set_frm_fmt(&buffer->stream, frame_fmt); + audio_stream_set_channels(&buffer->stream, channels); + + return buffer; +} + +/** + * @brief Create a test source buffer connected to a component. + */ +static struct comp_buffer *create_test_source(struct comp_dev *dev, uint32_t frame_fmt, + uint16_t channels, uint16_t buffer_size) +{ + struct sof_ipc_buffer desc = { + .size = buffer_size, + }; + struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); + + zassert_not_null(buffer, "Failed to allocate the source buffer"); + memset(buffer->stream.addr, 0, buffer_size); + + list_item_append(&buffer->sink_list, &dev->bsource_list); + + buffer->source = calloc(1, sizeof(*buffer->source)); + zassert_not_null(buffer->source); + buffer->source->state = COMP_STATE_PREPARE; + audio_stream_set_frm_fmt(&buffer->stream, frame_fmt); + audio_stream_set_channels(&buffer->stream, channels); + + return buffer; +} + +static void setup(struct sel_test_state *sel_state, const struct sel_test_parameters *parameters) +{ + struct comp_data *cd; + uint32_t size; + + sel_state->dev = calloc(1, sizeof(*sel_state->dev)); + zassert_not_null(sel_state->dev); + sel_state->dev->frames = parameters->frames; + + list_init(&sel_state->dev->bsink_list); + list_init(&sel_state->dev->bsource_list); + + cd = calloc(1, sizeof(*cd)); + zassert_not_null(cd); + comp_set_drvdata(sel_state->dev, cd); + cd->source_format = parameters->source_format; + cd->sink_format = parameters->sink_format; + + /* prepare parameters that will be used by sel_get_processing_function */ + cd->config.in_channels_count = parameters->in_channels; + cd->config.out_channels_count = parameters->out_channels; + cd->config.sel_channel = parameters->sel_channel; + + cd->sel_func = sel_get_processing_function(sel_state->dev); + zassert_not_null(cd->sel_func, "No processing function for format %u, %u channels", + parameters->source_format, parameters->out_channels); + + size = parameters->frames * get_frame_bytes(parameters->sink_format, + parameters->out_channels) * parameters->buffer_size_ms; + sel_state->sink = create_test_sink(sel_state->dev, parameters->sink_format, + parameters->out_channels, size); + + size = parameters->frames * get_frame_bytes(parameters->source_format, + parameters->in_channels) * parameters->buffer_size_ms; + sel_state->source = create_test_source(sel_state->dev, parameters->source_format, + parameters->in_channels, size); + + sel_state->cd = cd; +} + +static void teardown(struct sel_test_state *sel_state) +{ + free(sel_state->sink->sink); + buffer_free(sel_state->sink); + free(sel_state->source->source); + buffer_free(sel_state->source); + free(sel_state->cd); + free(sel_state->dev); +} + +#if CONFIG_FORMAT_S16LE +static void fill_source_s16(struct sel_test_state *sel_state) +{ + struct audio_stream *stream = &sel_state->source->stream; + int16_t *w_ptr; + int i; + + for (i = 0; i < audio_stream_get_free_samples(stream); i++) { + w_ptr = audio_stream_write_frag_s16(stream, i); + *w_ptr = i; + } + + audio_stream_produce(stream, audio_stream_get_free_bytes(stream)); +} + +static void verify_s16le_Xch_to_1ch(struct comp_dev *dev, struct audio_stream *sink, + struct audio_stream *source) +{ + struct comp_data *cd = comp_get_drvdata(dev); + const uint16_t *src = (uint16_t *)source->r_ptr; + const uint16_t *dst = (uint16_t *)sink->w_ptr; + uint32_t in_channels = cd->config.in_channels_count; + uint32_t channel; + uint32_t i; + uint32_t j = 0; + uint16_t source_in; + uint16_t destination; + + for (i = 0; i < source->size / sizeof(uint16_t); i += in_channels) { + for (channel = 0; channel < in_channels; channel++) { + if (channel == cd->config.sel_channel) { + source_in = src[i + cd->config.sel_channel]; + destination = dst[j++]; + zassert_equal(source_in, destination, + "frame %u: output %u, expected %u", + j - 1, destination, source_in); + } + } + } +} + +/* Also used for the 4 to 4 channel cases, which the CMocka test verified with + * an identical copy of this function. + */ +static void verify_s16le_nch_to_nch(struct comp_dev *dev, struct audio_stream *sink, + struct audio_stream *source) +{ + const uint16_t *src = (uint16_t *)source->r_ptr; + const uint16_t *dst = (uint16_t *)sink->w_ptr; + uint32_t channels = audio_stream_get_channels(source); + uint32_t channel; + uint32_t i; + + for (i = 0; i < sink->size / sizeof(uint16_t); i += channels) { + for (channel = 0; channel < channels; channel++) + zassert_equal(dst[i + channel], src[i + channel], + "sample %u: output %u, expected %u", i + channel, + dst[i + channel], src[i + channel]); + } +} +#endif /* CONFIG_FORMAT_S16LE */ + +#if CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE +static void fill_source_s32(struct sel_test_state *sel_state) +{ + struct audio_stream *stream = &sel_state->source->stream; + int32_t *w_ptr; + int i; + + for (i = 0; i < audio_stream_get_free_samples(stream); i++) { + w_ptr = audio_stream_write_frag_s32(stream, i); + *w_ptr = i << 16; + } + + audio_stream_produce(stream, audio_stream_get_free_bytes(stream)); +} + +static void verify_s32le_Xch_to_1ch(struct comp_dev *dev, struct audio_stream *sink, + struct audio_stream *source) +{ + struct comp_data *cd = comp_get_drvdata(dev); + const uint32_t *src = (uint32_t *)source->r_ptr; + const uint32_t *dst = (uint32_t *)sink->w_ptr; + uint32_t in_channels = cd->config.in_channels_count; + uint32_t channel; + uint32_t i; + uint32_t j = 0; + /* The CMocka test compared these as uint16_t, i.e. only the low 16 + * bits, which are always zero in this test signal. + */ + uint32_t source_in; + uint32_t destination; + + for (i = 0; i < source->size / sizeof(uint32_t); i += in_channels) { + for (channel = 0; channel < in_channels; channel++) { + if (channel == cd->config.sel_channel) { + source_in = src[i + cd->config.sel_channel]; + destination = dst[j++]; + zassert_equal(source_in, destination, + "frame %u: output 0x%x, expected 0x%x", + j - 1, destination, source_in); + } + } + } +} + +/* Also used for the 4 to 4 channel cases, which the CMocka test verified with + * an identical copy of this function. + */ +static void verify_s32le_nch_to_nch(struct comp_dev *dev, struct audio_stream *sink, + struct audio_stream *source) +{ + const uint32_t *src = (uint32_t *)source->r_ptr; + const uint32_t *dst = (uint32_t *)sink->w_ptr; + uint32_t channels = audio_stream_get_channels(source); + uint32_t channel; + uint32_t i; + + for (i = 0; i < sink->size / sizeof(uint32_t); i += channels) { + for (channel = 0; channel < channels; channel++) + zassert_equal(dst[i + channel], src[i + channel], + "sample %u: output 0x%x, expected 0x%x", i + channel, + dst[i + channel], src[i + channel]); + } +} +#endif /* CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE */ + +/** + * @brief Run the selector on one period of a ramp and verify the output. + * @param[in] parameters Channel counts, selected channel, frames and formats. + */ +static void test_audio_sel(const struct sel_test_parameters *parameters) +{ + struct sel_test_state sel_state; + struct sof_source *source; + struct sof_sink *sink; + struct comp_data *cd; + int ret; + + setup(&sel_state, parameters); + cd = sel_state.cd; + source = audio_buffer_get_source(&sel_state.source->audio_buffer); + sink = audio_buffer_get_sink(&sel_state.sink->audio_buffer); + + switch (cd->source_format) { +#if CONFIG_FORMAT_S16LE + case SOF_IPC_FRAME_S16_LE: + fill_source_s16(&sel_state); + break; +#endif /* CONFIG_FORMAT_S16LE */ +#if CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE + case SOF_IPC_FRAME_S24_4LE: + case SOF_IPC_FRAME_S32_LE: + case SOF_IPC_FRAME_FLOAT: + fill_source_s32(&sel_state); + break; +#endif /* CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE */ + default: + break; + } + + ret = cd->sel_func(sel_state.dev, sink, source, sel_state.dev->frames); + zassert_equal(ret, 0, "Selector processing failed: %d", ret); + + parameters->verify(sel_state.dev, &sel_state.sink->stream, &sel_state.source->stream); + + teardown(&sel_state); +} + +#define SEL_TEST_CASE(_name, _in, _out, _sel, _frames, _fmt, _verify) \ + ZTEST(selector_ipc3_suite, test_audio_sel_##_name) \ + { \ + const struct sel_test_parameters parameters = { \ + _in, _out, _sel, _frames, 1, _fmt, _fmt, _verify \ + }; \ + \ + test_audio_sel(¶meters); \ + } + +/* The CMocka parameter table lists the 2 to 1 channel, channel 0, 48 frame + * case twice for each format; both entries are kept, the second one with the + * _repeat suffix. + */ +#if CONFIG_FORMAT_S16LE +SEL_TEST_CASE(s16le_2ch_to_1ch_sel0_16_frames, 2, 1, 0, 16, SOF_IPC_FRAME_S16_LE, + verify_s16le_Xch_to_1ch) +SEL_TEST_CASE(s16le_2ch_to_1ch_sel1_16_frames, 2, 1, 1, 16, SOF_IPC_FRAME_S16_LE, + verify_s16le_Xch_to_1ch) +SEL_TEST_CASE(s16le_2ch_to_1ch_sel0_48_frames, 2, 1, 0, 48, SOF_IPC_FRAME_S16_LE, + verify_s16le_Xch_to_1ch) +SEL_TEST_CASE(s16le_2ch_to_1ch_sel1_48_frames, 2, 1, 1, 48, SOF_IPC_FRAME_S16_LE, + verify_s16le_Xch_to_1ch) +SEL_TEST_CASE(s16le_2ch_to_2ch_48_frames, 2, 2, 0, 48, SOF_IPC_FRAME_S16_LE, + verify_s16le_nch_to_nch) +SEL_TEST_CASE(s16le_4ch_to_4ch_48_frames, 4, 4, 0, 48, SOF_IPC_FRAME_S16_LE, + verify_s16le_nch_to_nch) +SEL_TEST_CASE(s16le_2ch_to_1ch_sel0_48_frames_repeat, 2, 1, 0, 48, SOF_IPC_FRAME_S16_LE, + verify_s16le_Xch_to_1ch) +SEL_TEST_CASE(s16le_4ch_to_1ch_sel0_48_frames, 4, 1, 0, 48, SOF_IPC_FRAME_S16_LE, + verify_s16le_Xch_to_1ch) +#endif /* CONFIG_FORMAT_S16LE */ + +#if CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE +SEL_TEST_CASE(s24le_2ch_to_1ch_sel0_16_frames, 2, 1, 0, 16, SOF_IPC_FRAME_S24_4LE, + verify_s32le_Xch_to_1ch) +SEL_TEST_CASE(s24le_2ch_to_1ch_sel1_16_frames, 2, 1, 1, 16, SOF_IPC_FRAME_S24_4LE, + verify_s32le_Xch_to_1ch) +SEL_TEST_CASE(s24le_2ch_to_1ch_sel0_48_frames, 2, 1, 0, 48, SOF_IPC_FRAME_S24_4LE, + verify_s32le_Xch_to_1ch) +SEL_TEST_CASE(s24le_2ch_to_1ch_sel1_48_frames, 2, 1, 1, 48, SOF_IPC_FRAME_S24_4LE, + verify_s32le_Xch_to_1ch) +SEL_TEST_CASE(s24le_2ch_to_2ch_48_frames, 2, 2, 0, 48, SOF_IPC_FRAME_S24_4LE, + verify_s32le_nch_to_nch) +SEL_TEST_CASE(s24le_4ch_to_4ch_48_frames, 4, 4, 0, 48, SOF_IPC_FRAME_S24_4LE, + verify_s32le_nch_to_nch) +SEL_TEST_CASE(s24le_2ch_to_1ch_sel0_48_frames_repeat, 2, 1, 0, 48, SOF_IPC_FRAME_S24_4LE, + verify_s32le_Xch_to_1ch) +SEL_TEST_CASE(s24le_4ch_to_1ch_sel0_48_frames, 4, 1, 0, 48, SOF_IPC_FRAME_S24_4LE, + verify_s32le_Xch_to_1ch) +#endif /* CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE */ + +ZTEST_SUITE(selector_ipc3_suite, NULL, NULL, NULL, NULL, NULL); diff --git a/test/ztest/unit/audio/selector/testcase.yaml b/test/ztest/unit/audio/selector/testcase.yaml index 7367d8555768..ea3302e621a1 100644 --- a/test/ztest/unit/audio/selector/testcase.yaml +++ b/test/ztest/unit/audio/selector/testcase.yaml @@ -16,3 +16,8 @@ tests: sof.unit.audio.selector: platform_allow: - native_sim + sof.unit.audio.selector.ipc3: + platform_allow: + - native_sim + extra_args: + - CONF_FILE=prj_ipc3.conf diff --git a/test/ztest/unit/audio/volume/CMakeLists.txt b/test/ztest/unit/audio/volume/CMakeLists.txt new file mode 100644 index 000000000000..a80536cddf65 --- /dev/null +++ b/test/ztest/unit/audio/volume/CMakeLists.txt @@ -0,0 +1,58 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# Volume processing unit tests converted from CMock to Ztest + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(sof_unit_audio_volume) + +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/include + ${SOF_ROOT}/src/kernel/platform/posix/include + ${SOF_ROOT}/src/platform/posix/include + ${SOF_ROOT}/src/audio + ${PROJECT_BINARY_DIR}/include/generated +) + +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_LIBRARY=1 + -DUNIT_TEST=1 +) + +# Keep full ASan/LSan allocation stacks (twister --enable-asan --enable-lsan) +if(CONFIG_ASAN) + target_compile_options(app PRIVATE -fno-omit-frame-pointer) +endif() + +target_sources(app PRIVATE + volume_process_ztest.c + # the generic C volume processing functions under test + ${SOF_ROOT}/src/audio/volume/volume_generic.c + # the test buffers + ${SOF_ROOT}/zephyr/lib/alloc.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/module/audio/source_api.c + ${SOF_ROOT}/src/module/audio/sink_api.c + ${SOF_ROOT}/src/ipc/ipc-helper.c + # audio_stream_recalc_align() needs gcd() + ${SOF_ROOT}/src/math/numbers.c +) + +sof_append_relative_path_definitions(app) diff --git a/test/ztest/unit/audio/volume/prj.conf b/test/ztest/unit/audio/volume/prj.conf new file mode 100644 index 000000000000..e7b74aa33014 --- /dev/null +++ b/test/ztest/unit/audio/volume/prj.conf @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n +CONFIG_COMP_VOLUME=y +CONFIG_FORMAT_S16LE=y +CONFIG_FORMAT_S24LE=y +CONFIG_FORMAT_S32LE=y +# Route the SOF heap to the host malloc()/free() of the native simulator, so +# that ASan and LSan can see it. They cannot see blocks carved out of the +# static sys_heap arena that zephyr/lib/alloc.c uses by default. +CONFIG_SOF_NATIVE_SIM_HOST_HEAP=y +# The sys_heap arena is still reserved and initialised by heap_init(), and its +# 256 KiB are too large for SYS_HEAP_SMALL_ONLY (see app/boards/native_sim.conf) +CONFIG_SYS_HEAP_BIG_ONLY=y diff --git a/test/ztest/unit/audio/volume/testcase.yaml b/test/ztest/unit/audio/volume/testcase.yaml new file mode 100644 index 000000000000..614aac1a4476 --- /dev/null +++ b/test/ztest/unit/audio/volume/testcase.yaml @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# Volume processing unit tests converted from CMock to Ztest +# +# These contents may have been developed with support from one or more Intel-operated +# generative artificial intelligence solutions. +# + +common: + tags: + - SOF + - unit_test + - audio + - volume + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.audio.volume: + platform_allow: + - native_sim + build_only: false diff --git a/test/ztest/unit/audio/volume/volume_process_ztest.c b/test/ztest/unit/audio/volume/volume_process_ztest.c new file mode 100644 index 000000000000..ed57a124fa76 --- /dev/null +++ b/test/ztest/unit/audio/volume/volume_process_ztest.c @@ -0,0 +1,443 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/volume/volume_process.c: +// Author: Tomasz Lauda + +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include + +/* Volume levels to test with: + * + * VOL_MAX +42 dB is maximum for Q8.16 format + * VOL_ZERO_DB 0 dB is the unity gain gain and default volume + * VOL_MINUS_80dB -80 dB is a low gain or large attenuation test + */ +#define VOL_MINUS_80DB (VOL_ZERO_DB / 10000) + +/* Max S24_4LE format value */ +#define INT24_MAX 8388607 + +/* Min S24_4LE format value */ +#define INT24_MIN -8388608 + +#define TEST_CHANNELS 2 +#define TEST_FRAMES 48 +#define TEST_BUFFER_SIZE_MS 1 + +struct vol_test_state { + struct processing_module *mod; + struct comp_buffer *sink; + struct comp_buffer *source; + struct vol_data *cd; +}; + +typedef void (*vol_verify_func)(struct processing_module *mod, struct comp_buffer *sink, + struct comp_buffer *source); + +/** + * @brief Create a test sink buffer connected to a component. + */ +static struct comp_buffer *create_test_sink(struct comp_dev *dev, uint32_t frame_fmt, + uint16_t channels, uint16_t buffer_size) +{ + struct sof_ipc_buffer desc = { + .size = buffer_size, + }; + struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); + + zassert_not_null(buffer, "Failed to allocate the sink buffer"); + memset(buffer->stream.addr, 0, buffer_size); + + list_item_append(&buffer->source_list, &dev->bsink_list); + + buffer->sink = calloc(1, sizeof(*buffer->sink)); + zassert_not_null(buffer->sink); + buffer->sink->state = COMP_STATE_PREPARE; + audio_stream_set_frm_fmt(&buffer->stream, frame_fmt); + audio_stream_set_channels(&buffer->stream, channels); + + return buffer; +} + +/** + * @brief Create a test source buffer connected to a component. + */ +static struct comp_buffer *create_test_source(struct comp_dev *dev, uint32_t frame_fmt, + uint16_t channels, uint16_t buffer_size) +{ + struct sof_ipc_buffer desc = { + .size = buffer_size, + }; + struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); + + zassert_not_null(buffer, "Failed to allocate the source buffer"); + memset(buffer->stream.addr, 0, buffer_size); + + list_item_append(&buffer->sink_list, &dev->bsource_list); + + buffer->source = calloc(1, sizeof(*buffer->source)); + zassert_not_null(buffer->source); + buffer->source->state = COMP_STATE_PREPARE; + audio_stream_set_frm_fmt(&buffer->stream, frame_fmt); + audio_stream_set_channels(&buffer->stream, channels); + + return buffer; +} + +static void set_volume(int32_t *vol, int32_t value, uint32_t channels) +{ + int i; + + for (i = 0; i < channels; i++) + vol[i] = value; +} + +/** + * @brief Set up a volume module with one source and one sink buffer. + * @param[out] state Test state to fill. + * @param[in] frame_fmt Source and sink frame format. + * @param[in] volume Gain to set on all channels. + */ +static void setup(struct vol_test_state *state, uint32_t frame_fmt, int32_t volume) +{ + struct processing_module *mod; + struct comp_dev *dev; + struct vol_data *cd; + uint32_t size; + + mod = calloc(1, sizeof(*mod)); + zassert_not_null(mod); + dev = calloc(1, sizeof(*dev)); + zassert_not_null(dev); + dev->frames = TEST_FRAMES; + mod->dev = dev; + dev->mod = mod; + + list_init(&dev->bsource_list); + list_init(&dev->bsink_list); + + size = TEST_FRAMES * get_frame_bytes(frame_fmt, TEST_CHANNELS) * TEST_BUFFER_SIZE_MS; + state->sink = create_test_sink(dev, frame_fmt, TEST_CHANNELS, size); + state->source = create_test_source(dev, frame_fmt, TEST_CHANNELS, size); + + cd = calloc(1, sizeof(*cd)); + zassert_not_null(cd); + mod->priv.private = cd; + cd->is_passthrough = false; + + /* number of channels used by the processing functions */ + cd->channels = TEST_CHANNELS; + + /* set processing function and volume */ + cd->scale_vol = vol_get_processing_function(dev, state->sink, cd); + zassert_not_null(cd->scale_vol, "No volume processing function for format %u", + frame_fmt); + set_volume(cd->volume, volume, TEST_CHANNELS); + + state->mod = mod; + state->cd = cd; +} + +static void teardown(struct vol_test_state *state) +{ + free(state->sink->sink); + buffer_free(state->sink); + free(state->source->source); + buffer_free(state->source); + free(state->cd); + free(state->mod->dev); + free(state->mod); +} + +#if CONFIG_FORMAT_S16LE +static void fill_source_s16(struct comp_buffer *source) +{ + int64_t val; + int16_t *src = (int16_t *)source->stream.r_ptr; + int i; + int sign = 1; + + for (i = 0; i < source->stream.size / sizeof(int16_t); i++) { + val = (INT16_MIN + (i >> 1)) * sign; + val = (val > INT16_MAX) ? INT16_MAX : val; + src[i] = (int16_t)val; + sign = -sign; + } +} + +static void verify_s16_to_s16(struct processing_module *mod, struct comp_buffer *sink, + struct comp_buffer *source) +{ + struct vol_data *cd = module_get_private_data(mod); + const int16_t *src = (int16_t *)source->stream.r_ptr; + const int16_t *dst = (int16_t *)sink->stream.w_ptr; + double processed; + int channels = audio_stream_get_channels(&sink->stream); + int channel; + int delta; + int i; + int16_t sample; + + for (i = 0; i < sink->stream.size / sizeof(uint16_t); i += channels) { + for (channel = 0; channel < channels; channel++) { + processed = src[i + channel] * + (double)cd->volume[channel] / + (double)VOL_ZERO_DB + 0.5; + if (processed > INT16_MAX) + processed = INT16_MAX; + + if (processed < INT16_MIN) + processed = INT16_MIN; + + sample = (int16_t)processed; + delta = dst[i + channel] - sample; + if (delta > 1 || delta < -1) + zassert_equal(dst[i + channel], sample, + "sample %d: output %d, expected %d", + i + channel, dst[i + channel], sample); + } + } +} +#endif /* CONFIG_FORMAT_S16LE */ + +#if CONFIG_FORMAT_S24LE +static void fill_source_s24(struct comp_buffer *source) +{ + int64_t val; + int32_t *src = (int32_t *)source->stream.r_ptr; + int i; + int sign = 1; + + for (i = 0; i < source->stream.size / sizeof(int32_t); i++) { + val = (INT24_MIN + (i >> 1)) * sign; + val = (val > INT24_MAX) ? INT24_MAX : val; + src[i] = (int32_t)val; + sign = -sign; + } +} + +static void verify_s24_to_s24_s32(struct processing_module *mod, struct comp_buffer *sink, + struct comp_buffer *source) +{ + struct vol_data *cd = module_get_private_data(mod); + const int32_t *src = (int32_t *)source->stream.r_ptr; + const int32_t *dst = (int32_t *)sink->stream.w_ptr; + double processed; + int32_t dst_sample; + int32_t sample; + int channels = audio_stream_get_channels(&sink->stream); + int channel; + int delta; + int i; + int shift = 8; + + for (i = 0; i < sink->stream.size / sizeof(uint32_t); i += channels) { + for (channel = 0; channel < channels; channel++) { + /* src * 256 is the CMocka (src << 8) without shifting a + * negative value + */ + processed = (src[i + channel] * 256) * + (double)cd->volume[channel] / + (double)VOL_ZERO_DB + 0.5 * (1 << shift); + if (processed > INT32_MAX) + processed = INT32_MAX; + + if (processed < INT32_MIN) + processed = INT32_MIN; + + sample = ((int32_t)processed) >> shift; + dst_sample = dst[i + channel]; + delta = dst_sample - sample; + if (delta > 1 || delta < -1) + zassert_equal(dst_sample, sample, + "sample %d: output %d, expected %d", + i + channel, dst_sample, sample); + + if (shift && (dst_sample < INT24_MIN || dst_sample > INT24_MAX)) + zassert_equal(dst_sample, sample, + "sample %d: output %d out of the S24 range", + i + channel, dst_sample); + } + } +} +#endif /* CONFIG_FORMAT_S24LE */ + +#if CONFIG_FORMAT_S32LE +static void fill_source_s32(struct comp_buffer *source) +{ + int64_t val; + int32_t *src = (int32_t *)source->stream.r_ptr; + int i; + int sign = 1; + + for (i = 0; i < source->stream.size / sizeof(int32_t); i++) { + val = (INT32_MIN + (i >> 1)) * sign; + val = (val > INT32_MAX) ? INT32_MAX : val; + src[i] = (int32_t)val; + sign = -sign; + } +} + +static void verify_s32_to_s24_s32(struct processing_module *mod, struct comp_buffer *sink, + struct comp_buffer *source) +{ + struct vol_data *cd = module_get_private_data(mod); + double processed; + const int32_t *src = (int32_t *)source->stream.r_ptr; + const int32_t *dst = (int32_t *)sink->stream.w_ptr; + int32_t dst_sample; + int32_t sample; + int channels = audio_stream_get_channels(&sink->stream); + int channel; + int delta; + int i; + int shift = 0; + + for (i = 0; i < sink->stream.size / sizeof(uint32_t); i += channels) { + for (channel = 0; channel < channels; channel++) { + processed = src[i + channel] * + (double)cd->volume[channel] / + (double)VOL_ZERO_DB + 0.5 * (1 << shift); + if (processed > INT32_MAX) + processed = INT32_MAX; + + if (processed < INT32_MIN) + processed = INT32_MIN; + + sample = ((int32_t)processed) >> shift; + dst_sample = dst[i + channel]; + delta = dst_sample - sample; + if (delta > 1 || delta < -1) + zassert_equal(dst_sample, sample, + "sample %d: output %d, expected %d", + i + channel, dst_sample, sample); + + if (shift && (dst_sample < INT24_MIN || dst_sample > INT24_MAX)) + zassert_equal(dst_sample, sample, + "sample %d: output %d out of the S24 range", + i + channel, dst_sample); + } + } +} +#endif /* CONFIG_FORMAT_S32LE */ + +/** + * @brief Scale one period of a test signal and verify it against a double + * precision reference. + * @param[in] frame_fmt Source and sink frame format. + * @param[in] volume Gain to set on all channels. + * @param[in] verify Format specific verification function. + */ +static void test_audio_vol(uint32_t frame_fmt, int32_t volume, vol_verify_func verify) +{ + struct vol_test_state state; + struct processing_module *mod; + struct vol_data *cd; + struct cir_buf_source source_buf; + struct cir_buf_sink sink_buf; + + setup(&state, frame_fmt, volume); + mod = state.mod; + cd = state.cd; + + switch (audio_stream_get_frm_fmt(&state.sink->stream)) { +#if CONFIG_FORMAT_S16LE + case SOF_IPC_FRAME_S16_LE: + fill_source_s16(state.source); + break; +#endif +#if CONFIG_FORMAT_S24LE + case SOF_IPC_FRAME_S24_4LE: + fill_source_s24(state.source); + break; +#endif +#if CONFIG_FORMAT_S32LE + case SOF_IPC_FRAME_S32_LE: + case SOF_IPC_FRAME_FLOAT: + fill_source_s32(state.source); + break; +#endif + default: + break; + } + + source_buf.buf_start = audio_stream_get_addr(&state.source->stream); + source_buf.buf_end = audio_stream_get_end_addr(&state.source->stream); + source_buf.ptr = audio_stream_get_rptr(&state.source->stream); + + sink_buf.buf_start = audio_stream_get_addr(&state.sink->stream); + sink_buf.buf_end = audio_stream_get_end_addr(&state.sink->stream); + sink_buf.ptr = audio_stream_get_wptr(&state.sink->stream); + + cd->scale_vol(mod, &source_buf, &sink_buf, mod->dev->frames, cd->attenuation); + + verify(mod, state.sink, state.source); + + teardown(&state); +} + +#if CONFIG_FORMAT_S16LE +ZTEST(audio_volume_suite, test_audio_vol_s16_max) +{ + test_audio_vol(SOF_IPC_FRAME_S16_LE, VOL_MAX, verify_s16_to_s16); +} + +ZTEST(audio_volume_suite, test_audio_vol_s16_zero_db) +{ + test_audio_vol(SOF_IPC_FRAME_S16_LE, VOL_ZERO_DB, verify_s16_to_s16); +} + +ZTEST(audio_volume_suite, test_audio_vol_s16_minus_80db) +{ + test_audio_vol(SOF_IPC_FRAME_S16_LE, VOL_MINUS_80DB, verify_s16_to_s16); +} +#endif /* CONFIG_FORMAT_S16LE */ + +#if CONFIG_FORMAT_S24LE +ZTEST(audio_volume_suite, test_audio_vol_s24_max) +{ + test_audio_vol(SOF_IPC_FRAME_S24_4LE, VOL_MAX, verify_s24_to_s24_s32); +} + +ZTEST(audio_volume_suite, test_audio_vol_s24_zero_db) +{ + test_audio_vol(SOF_IPC_FRAME_S24_4LE, VOL_ZERO_DB, verify_s24_to_s24_s32); +} + +ZTEST(audio_volume_suite, test_audio_vol_s24_minus_80db) +{ + test_audio_vol(SOF_IPC_FRAME_S24_4LE, VOL_MINUS_80DB, verify_s24_to_s24_s32); +} +#endif /* CONFIG_FORMAT_S24LE */ + +#if CONFIG_FORMAT_S32LE +ZTEST(audio_volume_suite, test_audio_vol_s32_max) +{ + test_audio_vol(SOF_IPC_FRAME_S32_LE, VOL_MAX, verify_s32_to_s24_s32); +} + +ZTEST(audio_volume_suite, test_audio_vol_s32_zero_db) +{ + test_audio_vol(SOF_IPC_FRAME_S32_LE, VOL_ZERO_DB, verify_s32_to_s24_s32); +} + +ZTEST(audio_volume_suite, test_audio_vol_s32_minus_80db) +{ + test_audio_vol(SOF_IPC_FRAME_S32_LE, VOL_MINUS_80DB, verify_s32_to_s24_s32); +} +#endif /* CONFIG_FORMAT_S32LE */ + +ZTEST_SUITE(audio_volume_suite, NULL, NULL, NULL, NULL, NULL);