diff --git a/src/audio/module_adapter/module/cadence_ipc4.c b/src/audio/module_adapter/module/cadence_ipc4.c index 4b2a53477d02..0068826ae569 100644 --- a/src/audio/module_adapter/module/cadence_ipc4.c +++ b/src/audio/module_adapter/module/cadence_ipc4.c @@ -147,7 +147,6 @@ static int cadence_configure_aac_dec_params(struct processing_module *mod) const struct snd_codec *codec_params; int bitstream_format; int sample_rate; - int to_stereo; int word_size; int ret; @@ -219,17 +218,19 @@ static int cadence_configure_aac_dec_params(struct processing_module *mod) } comp_warn(dev, "applied external sample rate return code: %#x", ret); } + } - to_stereo = codec_params->ch_out == 2; - API_CALL(cd, XA_API_CMD_SET_CONFIG_PARAM, - XA_AACDEC_CONFIG_PARAM_TO_STEREO, &to_stereo, ret); - if (ret != LIB_NO_ERROR) { - if (LIB_IS_FATAL_ERROR(ret)) { - comp_err(dev, "failed to apply stereo output: error: %#x", ret); - return ret; - } - comp_warn(dev, "applied stereo output return code: %#x", ret); - } + /* + * The stereo AAC library always produces 2-channel interleaved output, + * mono streams are duplicated to both channels by default (TO_STEREO). + * For a mono path keep only channel 0 of the decoder output. + */ + if (cd->base_cfg.audio_fmt.channels_count == 1) { + cd->mono_sample_bytes = word_size == 16 ? sizeof(int16_t) : sizeof(int32_t); + } else if (cd->base_cfg.audio_fmt.channels_count != 2) { + comp_err(dev, "Unsupported number of channels: %u", + cd->base_cfg.audio_fmt.channels_count); + return -EINVAL; } return 0; @@ -644,6 +645,34 @@ static bool cadence_raw_aac_input_is_padding(struct processing_module *mod) return true; } +/** Compact the stereo decoder output in place to channel 0 for a mono path. */ +static void cadence_stereo_to_mono(struct processing_module *mod) +{ + struct cadence_codec_data *cd = module_get_private_data(mod); + struct module_data *codec = &mod->priv; + uint32_t frames; + uint32_t i; + + if (!cd->mono_sample_bytes || !codec->mpd.produced) + return; + + frames = codec->mpd.produced / (2 * cd->mono_sample_bytes); + + if (cd->mono_sample_bytes == sizeof(int16_t)) { + int16_t *buf = codec->mpd.out_buff; + + for (i = 0; i < frames; i++) + buf[i] = buf[2 * i]; + } else { + int32_t *buf = codec->mpd.out_buff; + + for (i = 0; i < frames; i++) + buf[i] = buf[2 * i]; + } + + codec->mpd.produced = frames * cd->mono_sample_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) { @@ -722,6 +751,8 @@ static int cadence_codec_process(struct processing_module *mod, struct sof_sourc audio_buffer_set_eos(sof_audio_buffer_from_sink(sinks[0])); } + cadence_stereo_to_mono(mod); + /* do not proceed if not enough free space left */ if (out_space < codec->mpd.produced) { source_release_data(sources[0], 0); diff --git a/src/include/sof/audio/module_adapter/module/cadence.h b/src/include/sof/audio/module_adapter/module/cadence.h index 24c3beab77a2..2f5376c295f5 100644 --- a/src/include/sof/audio/module_adapter/module/cadence.h +++ b/src/include/sof/audio/module_adapter/module/cadence.h @@ -70,6 +70,7 @@ struct cadence_codec_data { uint32_t api_id; struct module_config setup_cfg; uint32_t flac_max_blocksize; /* FLAC: decoder output block size (samples) */ + uint32_t mono_sample_bytes; /* AAC: keep only channel 0 of the stereo output */ }; enum cadence_api_id {