diff --git a/src/audio/module_adapter/module/cadence.c b/src/audio/module_adapter/module/cadence.c index 9ef61e48b0a0..c3aad8f79737 100644 --- a/src/audio/module_adapter/module/cadence.c +++ b/src/audio/module_adapter/module/cadence.c @@ -547,6 +547,7 @@ int cadence_codec_process_data(struct processing_module *mod, struct cadence_codec_data *cd = module_get_private_data(mod); struct module_data *codec = &mod->priv; struct comp_dev *dev = mod->dev; + bool input_over; uint32_t done = 0; int ret; @@ -560,8 +561,14 @@ int cadence_codec_process_data(struct processing_module *mod, return 0; } - if (dev->expect_eos) { - /* Signal that the stream is expected to end anytime soon */ + if (cd->stream_end) + /* The last byte of the stream is in the input buffer */ + input_over = cd->stream_pos + codec->mpd.avail >= cd->stream_end; + else + /* The stream is expected to end anytime soon */ + input_over = dev->expect_eos; + + if (input_over) { API_CALL(cd, XA_API_CMD_INPUT_OVER, 0, NULL, ret); if (ret != LIB_NO_ERROR) { if (api_error_code) @@ -616,12 +623,19 @@ int cadence_codec_process_data(struct processing_module *mod, } if (dev->expect_eos) { - /* - * AAC decoder cannot signal DONE, check if it stopped - * producing data when EOS is expected - */ - if (cd->api_id == CADENCE_CODEC_AAC_DEC_ID && !codec->mpd.produced) + if (cd->stream_end) { + /* All input of the stream is consumed and no more output */ + if (!codec->mpd.produced && + cd->stream_pos + codec->mpd.consumed >= cd->stream_end) + done = true; + } else if (cd->api_id == CADENCE_CODEC_AAC_DEC_ID && !codec->mpd.produced) { + /* + * AAC decoder cannot signal DONE, without the stream end + * check if it stopped producing data when EOS is + * expected + */ done = true; + } if (done) codec->mpd.eos_reached = true; diff --git a/src/audio/module_adapter/module/cadence_ipc4.c b/src/audio/module_adapter/module/cadence_ipc4.c index 4b2a53477d02..ca6712771b28 100644 --- a/src/audio/module_adapter/module/cadence_ipc4.c +++ b/src/audio/module_adapter/module/cadence_ipc4.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -644,9 +645,44 @@ static bool cadence_raw_aac_input_is_padding(struct processing_module *mod) return true; } +/** + * Number of input bytes to pass to the codec in one call. + * + * Without the stream end a full input buffer is used. With the stream end + * known only the remaining bytes of the stream are passed, the last buffer of + * the stream can be partial and nothing is passed once the whole stream is + * consumed. + */ +static size_t cadence_input_size(struct processing_module *mod) +{ + struct cadence_codec_data *cd = module_get_private_data(mod); + struct module_data *codec = &mod->priv; + uint64_t remaining; + + if (!cd->stream_end) + return codec->mpd.in_buff_size; + + if (cd->stream_pos >= cd->stream_end) + return 0; + + remaining = cd->stream_end - cd->stream_pos; + + return MIN(remaining, (uint64_t)codec->mpd.in_buff_size); +} + +static void cadence_release_input(struct processing_module *mod, struct sof_source *source, + size_t bytes) +{ + struct cadence_codec_data *cd = module_get_private_data(mod); + + source_release_data(source, bytes); + cd->stream_pos += bytes; +} + static int cadence_codec_process(struct processing_module *mod, struct sof_source **sources, int num_of_sources, struct sof_sink **sinks, int num_of_sinks) { + struct cadence_codec_data *cd = module_get_private_data(mod); struct comp_dev *dev = mod->dev; struct module_data *codec = &mod->priv; size_t in_size = source_get_data_available(sources[0]); @@ -655,27 +691,34 @@ static int cadence_codec_process(struct processing_module *mod, struct sof_sourc void *sink_buffer_start, *sink_ptr; size_t src_bytes, sink_bytes; uint32_t remaining = in_size; + size_t input_size; int ret; + input_size = cadence_input_size(mod); + if (!codec->mpd.init_done) { + if (!input_size || remaining < input_size) + return -ENODATA; + /* Acquire data from the source buffer */ - ret = source_get_data(sources[0], codec->mpd.in_buff_size, &src_ptr, + ret = source_get_data(sources[0], input_size, &src_ptr, (const void **)&source_buffer_start, &src_bytes); if (ret) { comp_err(dev, "cannot get data from source buffer"); return ret; } - cadence_copy_data_from_buffer(codec->mpd.in_buff, src_ptr, codec->mpd.in_buff_size, + cadence_copy_data_from_buffer(codec->mpd.in_buff, src_ptr, input_size, src_bytes, source_buffer_start); - codec->mpd.avail = codec->mpd.in_buff_size; + codec->mpd.avail = input_size; ret = cadence_codec_init_process(mod); if (ret) return ret; remaining -= codec->mpd.consumed; - source_release_data(sources[0], codec->mpd.consumed); + cadence_release_input(mod, sources[0], codec->mpd.consumed); + input_size = cadence_input_size(mod); /* * The codec may need more input to complete the initialization @@ -690,18 +733,25 @@ static int cadence_codec_process(struct processing_module *mod, struct sof_sourc codec->mpd.consumed = 0; - /* Proceed only if we have enough data to fill the module buffer completely */ - if (remaining < codec->mpd.in_buff_size) + /* Proceed only if we have enough data to fill the module buffer */ + if (remaining < input_size) return -ENODATA; - /* Acquire data from the source buffer */ - ret = source_get_data(sources[0], codec->mpd.in_buff_size, &src_ptr, &source_buffer_start, - &src_bytes); + if (input_size) { + /* Acquire data from the source buffer */ + ret = source_get_data(sources[0], input_size, &src_ptr, &source_buffer_start, + &src_bytes); + if (ret) { + comp_err(dev, "cannot get data from source buffer"); + return ret; + } + + cadence_copy_data_from_buffer(codec->mpd.in_buff, src_ptr, input_size, + src_bytes, source_buffer_start); + } - cadence_copy_data_from_buffer(codec->mpd.in_buff, src_ptr, codec->mpd.in_buff_size, - src_bytes, source_buffer_start); - codec->mpd.avail = codec->mpd.in_buff_size; - if (cadence_raw_aac_input_is_padding(mod)) + codec->mpd.avail = input_size; + if (!cd->stream_end && cadence_raw_aac_input_is_padding(mod)) codec->mpd.avail = 0; comp_dbg(dev, "cadence_codec_process() start"); @@ -713,8 +763,6 @@ static int cadence_codec_process(struct processing_module *mod, struct sof_sourc } if (codec->mpd.eos_reached && !codec->mpd.eos_notification_sent) { - struct cadence_codec_data *cd = module_get_private_data(mod); - ipc_msg_send(cd->msg, NULL, false); codec->mpd.eos_notification_sent = true; @@ -739,7 +787,7 @@ static int cadence_codec_process(struct processing_module *mod, struct sof_sourc cadence_copy_data_to_buffer(sink_ptr, codec->mpd.produced, sink_bytes, sink_buffer_start, codec->mpd.out_buff); - source_release_data(sources[0], codec->mpd.consumed); + cadence_release_input(mod, sources[0], codec->mpd.consumed); sink_commit_buffer(sinks[0], codec->mpd.produced); /* reset produced and consumed */ @@ -751,11 +799,87 @@ static int cadence_codec_process(struct processing_module *mod, struct sof_sourc return 0; } +/** + * Handle a stream control parameter blob. + * + * Return: 1 if the blob was handled, 0 if it does not contain stream + * controls, negative error code on malformed blob. + */ +static int cadence_handle_compr_ctrl(struct processing_module *mod, const uint8_t *data, + size_t size) +{ + struct cadence_codec_data *cd = module_get_private_data(mod); + const struct module_param *param = (const struct module_param *)data; + struct comp_dev *dev = mod->dev; + + if (size < sizeof(*param) || !sof_compr_ctrl_is_ctrl(param->id)) + return 0; + + while (size >= sizeof(*param)) { + param = (const struct module_param *)data; + + if (param->size < sizeof(*param) || param->size > size) { + comp_err(dev, "invalid stream control size %u, %zu bytes left", + param->size, size); + return -EINVAL; + } + + if (!sof_compr_ctrl_is_ctrl(param->id)) { + comp_warn(dev, "skipping non stream control %#x", param->id); + } else { + switch (sof_compr_ctrl_get_ctrl(param->id)) { + case SOF_COMPR_CTRL_STREAM_END: + if (param->size < sizeof(*param) + sizeof(cd->stream_end)) { + comp_err(dev, "invalid stream end size %u", param->size); + return -EINVAL; + } + + memcpy_s(&cd->stream_end, sizeof(cd->stream_end), param->data, + sizeof(cd->stream_end)); + comp_info(dev, "stream end %u, position %u", + (uint32_t)cd->stream_end, + (uint32_t)cd->stream_pos); + break; + default: + comp_warn(dev, "unknown stream control %#x", param->id); + break; + } + } + + data += param->size; + size -= param->size; + } + + return 1; +} + +static int cadence_ipc4_set_configuration(struct processing_module *mod, uint32_t config_id, + enum module_cfg_fragment_position pos, + uint32_t data_offset_size, const uint8_t *fragment, + size_t fragment_size, uint8_t *response, + size_t response_size) +{ + int ret; + + /* Stream controls are sent in a single fragment and are not stored */ + if (pos == MODULE_CFG_FRAGMENT_SINGLE) { + ret = cadence_handle_compr_ctrl(mod, fragment, fragment_size); + if (ret) + return ret < 0 ? ret : 0; + } + + return cadence_codec_set_configuration(mod, config_id, pos, data_offset_size, fragment, + fragment_size, response, response_size); +} + static int cadence_codec_reset(struct processing_module *mod) { + struct cadence_codec_data *cd = module_get_private_data(mod); struct module_data *codec = &mod->priv; codec->mpd.init_done = 0; + cd->stream_pos = 0; + cd->stream_end = 0; return 0; } @@ -766,7 +890,7 @@ static bool cadence_is_ready_to_process(struct processing_module *mod, { struct module_data *codec = &mod->priv; - if (source_get_data_available(sources[0]) < codec->mpd.in_buff_size || + if (source_get_data_available(sources[0]) < cadence_input_size(mod) || sink_get_free_size(sinks[0]) < codec->mpd.out_buff_size) return false; @@ -786,7 +910,7 @@ static const struct module_interface cadence_codec_interface = { .init = cadence_codec_init, .prepare = cadence_codec_prepare, .process = cadence_codec_process, - .set_configuration = cadence_codec_set_configuration, + .set_configuration = cadence_ipc4_set_configuration, .reset = cadence_codec_reset, .free = ipc4_cadence_codec_free, .is_ready_to_process = cadence_is_ready_to_process, diff --git a/src/include/sof/audio/module_adapter/module/cadence.h b/src/include/sof/audio/module_adapter/module/cadence.h index 24c3beab77a2..4cdbf83b1039 100644 --- a/src/include/sof/audio/module_adapter/module/cadence.h +++ b/src/include/sof/audio/module_adapter/module/cadence.h @@ -70,6 +70,8 @@ struct cadence_codec_data { uint32_t api_id; struct module_config setup_cfg; uint32_t flac_max_blocksize; /* FLAC: decoder output block size (samples) */ + uint64_t stream_pos; /* byte offset of the next input byte in the stream */ + uint64_t stream_end; /* byte offset of the end of the stream, 0 if not known */ }; enum cadence_api_id { diff --git a/src/include/sof/audio/module_adapter/module/compress_ctrl.h b/src/include/sof/audio/module_adapter/module/compress_ctrl.h new file mode 100644 index 000000000000..7acfabb2378f --- /dev/null +++ b/src/include/sof/audio/module_adapter/module/compress_ctrl.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2026 Intel Corporation. + */ + +#ifndef __SOF_AUDIO_MODULE_ADAPTER_MODULE_COMPRESS_CTRL_H__ +#define __SOF_AUDIO_MODULE_ADAPTER_MODULE_COMPRESS_CTRL_H__ + +#include +#include + +/* + * Stream control parameters for compress modules. + * + * The host sends the parameters as struct module_param records. Stream control + * parameters have SOF_COMPR_CTRL_ID in the upper 16 bits of the id and the + * control in the lower 16 bits. No codec specific parameter uses this id, so + * modules without support for these controls skip them. + */ +#define SOF_COMPR_CTRL_ID 0xffff + +/* Absolute byte offset of the end of the stream (stream size), payload: uint64_t */ +#define SOF_COMPR_CTRL_STREAM_END 1 + +#define SOF_COMPR_CTRL_PARAM_ID(ctrl) ((SOF_COMPR_CTRL_ID << 16) | (ctrl)) + +static inline bool sof_compr_ctrl_is_ctrl(uint32_t param_id) +{ + return (param_id >> 16) == SOF_COMPR_CTRL_ID; +} + +static inline uint16_t sof_compr_ctrl_get_ctrl(uint32_t param_id) +{ + return param_id & 0xffff; +} + +#endif /* __SOF_AUDIO_MODULE_ADAPTER_MODULE_COMPRESS_CTRL_H__ */