|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | + |
| 3 | +/** |
| 4 | + * @file aria_ztest.c |
| 5 | + * @brief Aria internal ring buffer tests. |
| 6 | + * |
| 7 | + * Aria keeps the last smpl_group_cnt frames in an internal ring and outputs |
| 8 | + * them delayed by that amount, scaled by its gain. These tests run the real |
| 9 | + * module through the module adapter with buffer sizes and short periods that |
| 10 | + * make the ring wrap at a different point than the sink buffer, and check the |
| 11 | + * output frame by frame. |
| 12 | + */ |
| 13 | + |
| 14 | +#include <zephyr/ztest.h> |
| 15 | + |
| 16 | +#include <rtos/sof.h> |
| 17 | +#include <sof/audio/buffer.h> |
| 18 | +#include <sof/audio/component.h> |
| 19 | +#include <sof/audio/component_ext.h> |
| 20 | +#include <sof/audio/module_adapter/module/generic.h> |
| 21 | +#include <ipc/stream.h> |
| 22 | +#include <ipc4/base-config.h> |
| 23 | + |
| 24 | +#include <stdint.h> |
| 25 | +#include <stdlib.h> |
| 26 | +#include <string.h> |
| 27 | + |
| 28 | +#include "aria.h" |
| 29 | + |
| 30 | +#define TEST_RATE 48000 |
| 31 | +#define TEST_PERIODS 40 |
| 32 | +/* aria ramps its gain across the gain states, compare once it has settled */ |
| 33 | +#define TEST_SETTLED (2 * ARIA_MAX_GAIN_STATES) |
| 34 | + |
| 35 | +struct aria_fixture { |
| 36 | + struct comp_dev *dev; |
| 37 | + struct comp_buffer *source; |
| 38 | + struct comp_buffer *sink; |
| 39 | + struct sof_source *sources[1]; |
| 40 | + struct sof_sink *sinks[1]; |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * @brief Test sample for a frame and channel, small enough to keep full gain. |
| 45 | + * @param frame Input frame index. |
| 46 | + * @param ch Channel index. |
| 47 | + * @return S24 sample value. |
| 48 | + */ |
| 49 | +static int32_t test_sample(size_t frame, unsigned int ch) |
| 50 | +{ |
| 51 | + return (int32_t)(((frame & 0xfff) << 4) | ch); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * @brief Allocate a buffer and connect it to the aria component. |
| 56 | + * @param dev Aria component. |
| 57 | + * @param source True to connect as the aria source, false for the sink. |
| 58 | + * @param size Buffer size in bytes. |
| 59 | + * @param channels Channels count. |
| 60 | + * @return Connected buffer. |
| 61 | + */ |
| 62 | +static struct comp_buffer *create_test_buffer(struct comp_dev *dev, bool source, size_t size, |
| 63 | + unsigned int channels) |
| 64 | +{ |
| 65 | + struct sof_ipc_buffer desc = { |
| 66 | + .size = size, |
| 67 | + }; |
| 68 | + struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); |
| 69 | + |
| 70 | + zassert_not_null(buffer, "Failed to allocate test buffer"); |
| 71 | + memset(audio_stream_get_addr(&buffer->stream), 0, size); |
| 72 | + audio_stream_set_frm_fmt(&buffer->stream, SOF_IPC_FRAME_S32_LE); |
| 73 | + audio_stream_set_valid_fmt(&buffer->stream, SOF_IPC_FRAME_S24_4LE); |
| 74 | + audio_stream_set_rate(&buffer->stream, TEST_RATE); |
| 75 | + audio_stream_set_channels(&buffer->stream, channels); |
| 76 | + |
| 77 | + if (source) { |
| 78 | + buffer->source = calloc(1, sizeof(*buffer->source)); |
| 79 | + zassert_not_null(buffer->source); |
| 80 | + buffer->source->state = COMP_STATE_PREPARE; |
| 81 | + list_item_append(&buffer->sink_list, &dev->bsource_list); |
| 82 | + } else { |
| 83 | + buffer->sink = calloc(1, sizeof(*buffer->sink)); |
| 84 | + zassert_not_null(buffer->sink); |
| 85 | + buffer->sink->state = COMP_STATE_PREPARE; |
| 86 | + list_item_append(&buffer->source_list, &dev->bsink_list); |
| 87 | + } |
| 88 | + |
| 89 | + return buffer; |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * @brief Create an aria instance from an IPC4 init payload and prepare it. |
| 94 | + * @param channels Channels count in the base config. |
| 95 | + * @param ibs Input buffer size in bytes, sets the 1 ms chunk. |
| 96 | + * @param source_frames Source buffer size in frames. |
| 97 | + * @param sink_frames Sink buffer size in frames. |
| 98 | + * @param attenuation Aria attenuation parameter. |
| 99 | + * @return Prepared fixture. |
| 100 | + */ |
| 101 | +static struct aria_fixture *create_fixture(unsigned int channels, uint32_t ibs, |
| 102 | + size_t source_frames, size_t sink_frames, |
| 103 | + uint32_t attenuation) |
| 104 | +{ |
| 105 | + const struct sof_uuid uuid = SOF_REG_UUID(aria); |
| 106 | + const struct comp_driver *driver = comp_driver_find(&uuid, SOF_COMP_MODULE_ADAPTER); |
| 107 | + const size_t frame_bytes = channels * sizeof(int32_t); |
| 108 | + struct ipc4_aria_module_cfg cfg = { |
| 109 | + .base_cfg = |
| 110 | + { |
| 111 | + .ibs = ibs, |
| 112 | + .obs = ibs, |
| 113 | + .audio_fmt = |
| 114 | + { |
| 115 | + .sampling_frequency = TEST_RATE, |
| 116 | + .depth = IPC4_DEPTH_32BIT, |
| 117 | + .ch_map = 0x76543210, |
| 118 | + .ch_cfg = IPC4_CHANNEL_CONFIG_INVALID, |
| 119 | + .interleaving_style = IPC4_CHANNELS_INTERLEAVED, |
| 120 | + .channels_count = channels, |
| 121 | + .valid_bit_depth = IPC4_DEPTH_24BIT, |
| 122 | + .s_type = IPC4_TYPE_SIGNED_INTEGER, |
| 123 | + }, |
| 124 | + }, |
| 125 | + .attenuation = attenuation, |
| 126 | + }; |
| 127 | + struct ipc_config_process spec = { |
| 128 | + .size = sizeof(cfg), |
| 129 | + .data = (const uint8_t *)&cfg, |
| 130 | + }; |
| 131 | + struct comp_ipc_config ipc_cfg = { |
| 132 | + .id = 1, |
| 133 | + .proc_domain = COMP_PROCESSING_DOMAIN_LL, |
| 134 | + .type = SOF_COMP_MODULE_ADAPTER, |
| 135 | + .ipc_config_size = spec.size, |
| 136 | + }; |
| 137 | + struct aria_fixture *fixture; |
| 138 | + struct processing_module *mod; |
| 139 | + const struct module_interface *ops; |
| 140 | + |
| 141 | + zassert_not_null(driver, "Aria driver not registered"); |
| 142 | + fixture = calloc(1, sizeof(*fixture)); |
| 143 | + zassert_not_null(fixture); |
| 144 | + fixture->dev = driver->ops.create(driver, &ipc_cfg, &spec); |
| 145 | + zassert_not_null(fixture->dev, "Failed to create aria"); |
| 146 | + mod = comp_mod(fixture->dev); |
| 147 | + |
| 148 | + fixture->source = |
| 149 | + create_test_buffer(fixture->dev, true, source_frames * frame_bytes, channels); |
| 150 | + fixture->sink = |
| 151 | + create_test_buffer(fixture->dev, false, sink_frames * frame_bytes, channels); |
| 152 | + fixture->sources[0] = audio_buffer_get_source(&fixture->source->audio_buffer); |
| 153 | + fixture->sinks[0] = audio_buffer_get_sink(&fixture->sink->audio_buffer); |
| 154 | + |
| 155 | + ops = fixture->dev->drv->adapter_ops; |
| 156 | + zassert_ok(ops->prepare(mod, fixture->sources, 1, fixture->sinks, 1)); |
| 157 | + |
| 158 | + return fixture; |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * @brief Free all resources owned by an aria fixture. |
| 163 | + * @param fixture Fixture to free. |
| 164 | + */ |
| 165 | +static void destroy_fixture(struct aria_fixture *fixture) |
| 166 | +{ |
| 167 | + free(fixture->source->source); |
| 168 | + buffer_free(fixture->source); |
| 169 | + free(fixture->sink->sink); |
| 170 | + buffer_free(fixture->sink); |
| 171 | + fixture->dev->drv->ops.free(fixture->dev); |
| 172 | + free(fixture); |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + * @brief Run aria period by period and check the delayed, scaled output. |
| 177 | + * @param channels Channels count. |
| 178 | + * @param chunk_frames Frames per 1 ms chunk, ibs in frames. |
| 179 | + * @param source_frames Source buffer size in frames. |
| 180 | + * @param sink_frames Sink buffer size in frames. |
| 181 | + * @param first_frames Frames available in the first period. |
| 182 | + * @param attenuation Aria attenuation parameter. |
| 183 | + */ |
| 184 | +static void run_aria(unsigned int channels, size_t chunk_frames, size_t source_frames, |
| 185 | + size_t sink_frames, size_t first_frames, uint32_t attenuation) |
| 186 | +{ |
| 187 | + const size_t frame_bytes = channels * sizeof(int32_t); |
| 188 | + struct aria_fixture *fixture = create_fixture(channels, chunk_frames * frame_bytes, |
| 189 | + source_frames, sink_frames, attenuation); |
| 190 | + struct processing_module *mod = comp_mod(fixture->dev); |
| 191 | + const struct module_interface *ops = fixture->dev->drv->adapter_ops; |
| 192 | + struct audio_stream *source = &fixture->source->stream; |
| 193 | + struct audio_stream *sink = &fixture->sink->stream; |
| 194 | + size_t in_frame = 0; |
| 195 | + size_t out_frame = 0; |
| 196 | + |
| 197 | + for (int period = 0; period < TEST_PERIODS; period++) { |
| 198 | + size_t frames = period ? chunk_frames : first_frames; |
| 199 | + int32_t *w = audio_stream_get_wptr(source); |
| 200 | + int32_t *r; |
| 201 | + size_t avail; |
| 202 | + |
| 203 | + for (size_t f = 0; f < frames; f++) { |
| 204 | + for (unsigned int ch = 0; ch < channels; ch++) { |
| 205 | + *w = test_sample(in_frame + f, ch); |
| 206 | + w = audio_stream_wrap(source, w + 1); |
| 207 | + } |
| 208 | + } |
| 209 | + audio_stream_produce(source, frames * frame_bytes); |
| 210 | + in_frame += frames; |
| 211 | + |
| 212 | + zassert_ok(ops->process(mod, fixture->sources, 1, fixture->sinks, 1)); |
| 213 | + |
| 214 | + avail = audio_stream_get_avail_frames(sink); |
| 215 | + zassert_equal(avail, frames, "period %d: %zu frames out, %zu in", period, avail, |
| 216 | + frames); |
| 217 | + |
| 218 | + r = audio_stream_get_rptr(sink); |
| 219 | + for (size_t f = 0; f < avail; f++, out_frame++) { |
| 220 | + for (unsigned int ch = 0; ch < channels; ch++) { |
| 221 | + int32_t expected = test_sample(out_frame - chunk_frames, ch) |
| 222 | + << attenuation; |
| 223 | + |
| 224 | + if (period >= TEST_SETTLED) { |
| 225 | + zassert_equal(*r, expected, |
| 226 | + "frame %zu ch %u: got %#x expected %#x", |
| 227 | + out_frame, ch, *r, expected); |
| 228 | + } |
| 229 | + r = audio_stream_wrap(sink, r + 1); |
| 230 | + } |
| 231 | + } |
| 232 | + audio_stream_consume(sink, avail * frame_bytes); |
| 233 | + } |
| 234 | + |
| 235 | + destroy_fixture(fixture); |
| 236 | +} |
| 237 | + |
| 238 | +/** |
| 239 | + * @brief Initialize the aria component registry for the suite. |
| 240 | + * @return Suite fixture, unused. |
| 241 | + */ |
| 242 | +static void *aria_suite_setup(void) |
| 243 | +{ |
| 244 | + sys_comp_init(sof_get()); |
| 245 | + sys_comp_module_aria_interface_init(); |
| 246 | + return NULL; |
| 247 | +} |
| 248 | + |
| 249 | +/* |
| 250 | + * A short first period leaves the read position inside the ring, and with a |
| 251 | + * 70 frame sink the sink wraps before the ring in later periods. The ring |
| 252 | + * wrap distance must follow the read pointer across the sink wrap. |
| 253 | + */ |
| 254 | +ZTEST(aria_suite, test_sink_wrap_before_ring_wrap) |
| 255 | +{ |
| 256 | + run_aria(2, 48, 96, 70, 20, 1); |
| 257 | +} |
| 258 | + |
| 259 | +/* |
| 260 | + * 3 channels and a 47 frame chunk give an odd number of samples per chunk. |
| 261 | + * The ring must still hold whole frames, so no frame straddles its end and |
| 262 | + * channels stay in place. |
| 263 | + */ |
| 264 | +ZTEST(aria_suite, test_odd_samples_per_chunk) |
| 265 | +{ |
| 266 | + run_aria(3, 47, 94, 94, 47, 1); |
| 267 | +} |
| 268 | + |
| 269 | +ZTEST_SUITE(aria_suite, NULL, aria_suite_setup, NULL, NULL, NULL); |
0 commit comments