|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// |
| 3 | +// Copyright 2025 NXP |
| 4 | +// |
| 5 | +// Author: Daniel Baluta <daniel.baluta@nxp.com> |
| 6 | + |
| 7 | +#include <rtos/panic.h> |
| 8 | +#include <rtos/alloc.h> |
| 9 | +#include <rtos/cache.h> |
| 10 | +#include <rtos/init.h> |
| 11 | +#include <rtos/string.h> |
| 12 | +#include <sof/audio/buffer.h> |
| 13 | +#include <sof/audio/format.h> |
| 14 | +#include <sof/audio/pipeline.h> |
| 15 | +#include <sof/ipc/msg.h> |
| 16 | +#include <sof/lib/uuid.h> |
| 17 | +#include <sof/list.h> |
| 18 | +#include <sof/audio/module_adapter/module/generic.h> |
| 19 | +#include <sof/ut.h> |
| 20 | +#include <sof/trace/trace.h> |
| 21 | +#include <ipc/dai.h> |
| 22 | +#include <user/trace.h> |
| 23 | +#include <errno.h> |
| 24 | +#include <stddef.h> |
| 25 | +#include <stdint.h> |
| 26 | +#include <module/module/interface.h> |
| 27 | +#include <zephyr/logging/log.h> |
| 28 | + |
| 29 | +#include <sof/audio/nxp/eap/eap_lib_defines.h> |
| 30 | +#include <sof/audio/nxp/eap/EAP_Includes/EAP16.h> |
| 31 | +#include <sof/audio/nxp/eap/EAP_Parameter_presets.h> |
| 32 | + |
| 33 | +LOG_MODULE_REGISTER(nxp_eap, CONFIG_SOF_LOG_LEVEL); |
| 34 | +SOF_DEFINE_REG_UUID(nxp_eap); |
| 35 | +DECLARE_TR_CTX(nxp_eap_tr, SOF_UUID(nxp_eap_uuid), LOG_LEVEL_INFO); |
| 36 | + |
| 37 | +#define NXP_EAP_DEFAULT_MAX_BLOCK_SIZE 480 |
| 38 | + |
| 39 | +struct nxp_eap_data { |
| 40 | + LVM_Handle_t instance; |
| 41 | + LVM_MemTab_t mem_tab; |
| 42 | + LVM_InstParams_t inst_params; |
| 43 | + LVM_ControlParams_t ctrl_params; |
| 44 | + int sample_rate; |
| 45 | + int channels; |
| 46 | + int frame_bytes; |
| 47 | + int audio_time_ms; |
| 48 | + uint32_t buffer_bytes; |
| 49 | +}; |
| 50 | + |
| 51 | +struct nxp_eap_preset_params { |
| 52 | + char *name; |
| 53 | + LVM_ControlParams_t *params; |
| 54 | +}; |
| 55 | + |
| 56 | +struct nxp_eap_preset_params nxp_eap_effect_presets[] = { |
| 57 | + { |
| 58 | + .name = "AllEffectsOff", |
| 59 | + .params = (LVM_ControlParams_t *)&ControlParamSet_allEffectOff, |
| 60 | + }, |
| 61 | + { |
| 62 | + .name = "AutoVolumeLeveler", |
| 63 | + .params = (LVM_ControlParams_t *)&ControlParamSet_autoVolumeLeveler, |
| 64 | + }, |
| 65 | + { |
| 66 | + .name = "ConcertSound", |
| 67 | + .params = (LVM_ControlParams_t *)&ControlParamSet_concertSound, |
| 68 | + }, |
| 69 | + { |
| 70 | + .name = "LoudnessMaximiser", |
| 71 | + .params = (LVM_ControlParams_t *)&ControlParamSet_loudnessMaximiser, |
| 72 | + }, |
| 73 | + { |
| 74 | + .name = "MusicEnhancer", |
| 75 | + .params = (LVM_ControlParams_t *)&ControlParamSet_musicEnhancerRmsLimiter, |
| 76 | + }, |
| 77 | + { |
| 78 | + .name = "VoiceEnhancer", |
| 79 | + .params = (LVM_ControlParams_t *)&ControlParamSet_voiceEnhancer, |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +static int nxp_eap_init(struct processing_module *mod) |
| 84 | +{ |
| 85 | + struct comp_dev *dev = mod->dev; |
| 86 | + struct nxp_eap_data *eap; |
| 87 | + LVM_VersionInfo_st info; |
| 88 | + LVM_ReturnStatus_en lvm_ret; |
| 89 | + int ret = 0; |
| 90 | + |
| 91 | + LVM_GetVersionInfo(&info); |
| 92 | + |
| 93 | + tr_info(mod->dev, "NXP EAP library, platform: %s version:%s", |
| 94 | + info.pPlatform, info.pVersionNumber); |
| 95 | + |
| 96 | + eap = rballoc(0, SOF_MEM_CAPS_RAM, sizeof(*eap)); |
| 97 | + if (!eap) { |
| 98 | + comp_err(dev, "nxp_eap_init() failed to allocate module private data"); |
| 99 | + return -ENOMEM; |
| 100 | + } |
| 101 | + |
| 102 | + module_set_private_data(mod, eap); |
| 103 | + |
| 104 | + memcpy(&eap->inst_params, &InstParams_allEffectOff, sizeof(eap->inst_params)); |
| 105 | + |
| 106 | + lvm_ret = LVM_GetMemoryTable(LVM_NULL, &eap->mem_tab, &eap->inst_params); |
| 107 | + if (lvm_ret != LVM_SUCCESS) { |
| 108 | + comp_err(dev, "nxp_eap_init() failet to get memory table %d", lvm_ret); |
| 109 | + return -EINVAL; |
| 110 | + } |
| 111 | + |
| 112 | + /* mark all pBaseAddress with NULL so that would be easier to implement cleanup */ |
| 113 | + for (int i = 0; i < LVM_NR_MEMORY_REGIONS; i++) |
| 114 | + eap->mem_tab.Region[i].pBaseAddress = NULL; |
| 115 | + |
| 116 | + for (int i = 0; i < LVM_NR_MEMORY_REGIONS; i++) { |
| 117 | + eap->mem_tab.Region[i].pBaseAddress = rballoc(0, SOF_MEM_CAPS_RAM, |
| 118 | + eap->mem_tab.Region[i].Size); |
| 119 | + if (!eap->mem_tab.Region[i].pBaseAddress) { |
| 120 | + comp_err(dev, "nxp_eap_init() failed to allocate memory for region %d", i); |
| 121 | + ret = -ENOMEM; |
| 122 | + goto free_mem; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + lvm_ret = LVM_GetInstanceHandle(&eap->instance, &eap->mem_tab, &eap->inst_params); |
| 127 | + if (lvm_ret != LVM_SUCCESS) { |
| 128 | + comp_err(dev, "nxp_eap_init() failed to get instance handle err: %d", ret); |
| 129 | + ret = -EINVAL; |
| 130 | + goto free_mem; |
| 131 | + } |
| 132 | + |
| 133 | + /* default parameters, no effects */ |
| 134 | + memcpy(&eap->ctrl_params, &ControlParamSet_allEffectOff, sizeof(eap->ctrl_params)); |
| 135 | + |
| 136 | + return 0; |
| 137 | + |
| 138 | +free_mem: |
| 139 | + for (int i = 0; i < LVM_NR_MEMORY_REGIONS; i++) { |
| 140 | + if (eap->mem_tab.Region[i].pBaseAddress) { |
| 141 | + rfree(eap->mem_tab.Region[i].pBaseAddress); |
| 142 | + eap->mem_tab.Region[i].pBaseAddress = NULL; |
| 143 | + } |
| 144 | + } |
| 145 | + return ret; |
| 146 | +} |
| 147 | + |
| 148 | +static int nxp_eap_free(struct processing_module *mod) |
| 149 | +{ |
| 150 | + struct comp_dev *dev = mod->dev; |
| 151 | + struct nxp_eap_data *eap = module_get_private_data(mod); |
| 152 | + |
| 153 | + comp_dbg(dev, "nxp_eap_free()"); |
| 154 | + |
| 155 | + for (int i = 0; i < LVM_NR_MEMORY_REGIONS; i++) { |
| 156 | + if (eap->mem_tab.Region[i].pBaseAddress) { |
| 157 | + rfree(eap->mem_tab.Region[i].pBaseAddress); |
| 158 | + eap->mem_tab.Region[i].pBaseAddress = NULL; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + rfree(eap); |
| 163 | + |
| 164 | + return 0; |
| 165 | +} |
| 166 | + |
| 167 | +static int nxp_eap_prepare(struct processing_module *mod, |
| 168 | + struct sof_source **sources, int num_of_sources, |
| 169 | + struct sof_sink **sinks, int num_of_sinks) |
| 170 | +{ |
| 171 | + struct comp_dev *dev = mod->dev; |
| 172 | + struct module_data *md = &mod->priv; |
| 173 | + struct nxp_eap_data *eap = module_get_private_data(mod); |
| 174 | + struct comp_buffer *source = comp_dev_get_first_data_producer(dev); |
| 175 | + const struct audio_stream *stream; |
| 176 | + |
| 177 | + comp_dbg(dev, "nxp_eap_prepare()"); |
| 178 | + |
| 179 | + stream = &source->stream; |
| 180 | + eap->sample_rate = audio_stream_get_rate(stream); |
| 181 | + eap->channels = audio_stream_get_channels(stream); |
| 182 | + eap->frame_bytes = audio_stream_frame_bytes(stream); |
| 183 | + eap->audio_time_ms = 0; |
| 184 | + |
| 185 | + /* total bytes needed to be in the input buffer to be processed |
| 186 | + * by the EAP library |
| 187 | + */ |
| 188 | + eap->buffer_bytes = NXP_EAP_DEFAULT_MAX_BLOCK_SIZE; |
| 189 | + |
| 190 | + md->mpd.in_buff = rballoc_align(0, SOF_MEM_CAPS_RAM, eap->buffer_bytes, 32); |
| 191 | + if (!md->mpd.in_buff) |
| 192 | + return -ENOMEM; |
| 193 | + |
| 194 | + md->mpd.in_buff_size = eap->buffer_bytes; |
| 195 | + |
| 196 | + md->mpd.out_buff = rballoc_align(0, SOF_MEM_CAPS_RAM, eap->buffer_bytes, 32); |
| 197 | + if (!md->mpd.out_buff) { |
| 198 | + rfree(md->mpd.in_buff); |
| 199 | + return -ENOMEM; |
| 200 | + } |
| 201 | + |
| 202 | + md->mpd.out_buff_size = eap->buffer_bytes; |
| 203 | + |
| 204 | + return 0; |
| 205 | +} |
| 206 | + |
| 207 | +static int nxp_eap_reset(struct processing_module *mod) |
| 208 | +{ |
| 209 | + struct comp_dev *dev = mod->dev; |
| 210 | + struct module_data *md = &mod->priv; |
| 211 | + |
| 212 | + comp_dbg(dev, "nxp_eap_reset"); |
| 213 | + |
| 214 | + if (md->mpd.in_buff) { |
| 215 | + rfree(md->mpd.in_buff); |
| 216 | + md->mpd.in_buff = NULL; |
| 217 | + } |
| 218 | + |
| 219 | + if (md->mpd.out_buff) { |
| 220 | + rfree(md->mpd.out_buff); |
| 221 | + md->mpd.out_buff = NULL; |
| 222 | + } |
| 223 | + |
| 224 | + return 0; |
| 225 | +} |
| 226 | + |
| 227 | +static int nxp_eap_process(struct processing_module *mod, |
| 228 | + struct input_stream_buffer *input_buffers, int num_input_buffers, |
| 229 | + struct output_stream_buffer *output_buffers, int num_output_buffers) |
| 230 | +{ |
| 231 | + struct comp_dev *dev = mod->dev; |
| 232 | + struct module_data *eap = &mod->priv; |
| 233 | + struct nxp_eap_data *eap_data = module_get_private_data(mod); |
| 234 | + LVM_INT16 *buffer_table[2]; |
| 235 | + LVM_ReturnStatus_en ret; |
| 236 | + |
| 237 | + comp_dbg(dev, "nxp_eap_process()"); |
| 238 | + |
| 239 | + /* we need to input buffer to be completely full to be able to process it */ |
| 240 | + if (input_buffers[0].size < eap->mpd.in_buff_size) |
| 241 | + return -ENODATA; |
| 242 | + |
| 243 | + memcpy_s(eap->mpd.in_buff, eap->mpd.in_buff_size, |
| 244 | + (uint8_t *)input_buffers[0].data + input_buffers[0].consumed, |
| 245 | + eap->mpd.in_buff_size); |
| 246 | + eap->mpd.avail = eap->mpd.in_buff_size; |
| 247 | + |
| 248 | + buffer_table[0] = eap->mpd.out_buff; |
| 249 | + buffer_table[1] = LVM_NULL; |
| 250 | + |
| 251 | + eap_data->audio_time_ms += eap->mpd.avail / (eap_data->sample_rate / 1000); |
| 252 | + |
| 253 | + ret = LVM_Process(eap_data->instance, (LVM_INT16 *)eap->mpd.in_buff, |
| 254 | + (LVM_INT16 **)buffer_table, eap->mpd.avail / eap_data->frame_bytes, |
| 255 | + eap_data->audio_time_ms); |
| 256 | + if (ret != LVM_SUCCESS) { |
| 257 | + comp_err(dev, "nxp_eap_process() failed with error %d", ret); |
| 258 | + return -EIO; |
| 259 | + } |
| 260 | + |
| 261 | + eap->mpd.produced = eap->mpd.in_buff_size; |
| 262 | + eap->mpd.consumed = eap->mpd.in_buff_size; |
| 263 | + |
| 264 | + input_buffers[0].consumed = eap->mpd.consumed; |
| 265 | + |
| 266 | + /* copy produced samples to output buffer */ |
| 267 | + memcpy_s(output_buffers[0].data, eap->mpd.produced, eap->mpd.out_buff, eap->mpd.produced); |
| 268 | + output_buffers[0].size = eap->mpd.produced; |
| 269 | + |
| 270 | + return 0; |
| 271 | +} |
| 272 | + |
| 273 | +static int nxp_eap_cmd_set_value(struct processing_module *mod, struct sof_ipc_ctrl_data *cdata) |
| 274 | +{ |
| 275 | + int index; |
| 276 | + LVM_ReturnStatus_en ret; |
| 277 | + struct comp_dev *dev = mod->dev; |
| 278 | + struct nxp_eap_data *eap = module_get_private_data(mod); |
| 279 | + |
| 280 | + index = cdata->chanv[0].value; |
| 281 | + |
| 282 | + if (index >= ARRAY_SIZE(nxp_eap_effect_presets)) { |
| 283 | + comp_info(dev, "nxp_eap_cmd_set_value() invalid index (%d), config not changed", |
| 284 | + index); |
| 285 | + } else { |
| 286 | + memcpy(&eap->ctrl_params, nxp_eap_effect_presets[index].params, |
| 287 | + sizeof(eap->ctrl_params)); |
| 288 | + comp_info(dev, "New config set to %s", nxp_eap_effect_presets[index].name); |
| 289 | + } |
| 290 | + |
| 291 | + ret = LVM_SetControlParameters(eap->instance, &eap->ctrl_params); |
| 292 | + if (ret != LVM_SUCCESS) { |
| 293 | + comp_err(dev, "LVM_SetControlParameters failed with error %d", ret); |
| 294 | + return -EIO; |
| 295 | + } |
| 296 | + return 0; |
| 297 | +} |
| 298 | + |
| 299 | +static int nxp_eap_set_config(struct processing_module *mod, uint32_t param_id, |
| 300 | + enum module_cfg_fragment_position pos, uint32_t data_offset_size, |
| 301 | + const uint8_t *fragment, size_t fragment_size, uint8_t *response, |
| 302 | + size_t response_size) |
| 303 | +{ |
| 304 | + struct comp_dev *dev = mod->dev; |
| 305 | + struct sof_ipc_ctrl_data *cdata = (struct sof_ipc_ctrl_data *)fragment; |
| 306 | + |
| 307 | + comp_dbg(dev, "nxp_eap_set_config()"); |
| 308 | + |
| 309 | + if (cdata->cmd != SOF_CTRL_CMD_BINARY) |
| 310 | + return nxp_eap_cmd_set_value(mod, cdata); |
| 311 | + |
| 312 | + comp_err(dev, "nxp_set_config() binary config not supported"); |
| 313 | + return -EINVAL; |
| 314 | +} |
| 315 | + |
| 316 | +static int nxp_eap_get_config(struct processing_module *mod, |
| 317 | + uint32_t param_id, uint32_t *data_offset_size, |
| 318 | + uint8_t *fragment, size_t fragment_size) |
| 319 | +{ |
| 320 | + struct comp_dev *dev = mod->dev; |
| 321 | + |
| 322 | + comp_dbg(dev, "nxp_eap_get_config()"); |
| 323 | + |
| 324 | + return 0; |
| 325 | +} |
| 326 | + |
| 327 | +static const struct module_interface nxp_eap_interface = { |
| 328 | + .init = nxp_eap_init, |
| 329 | + .prepare = nxp_eap_prepare, |
| 330 | + .process_raw_data = nxp_eap_process, |
| 331 | + .set_configuration = nxp_eap_set_config, |
| 332 | + .get_configuration = nxp_eap_get_config, |
| 333 | + .reset = nxp_eap_reset, |
| 334 | + .free = nxp_eap_free, |
| 335 | +}; |
| 336 | + |
| 337 | +DECLARE_MODULE_ADAPTER(nxp_eap_interface, nxp_eap_uuid, nxp_eap_tr); |
| 338 | +SOF_MODULE_INIT(nxp_eap, sys_comp_module_nxp_eap_interface_init); |
0 commit comments