From c5e48a737e1a592a6d0e5e359820c19bda84f2be Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 3 Dec 2026 14:30:21 +0200 Subject: [PATCH 1/2] ASoC: SOF: ipc4-compress: Clear the free part of the buffer on drain The host DMA of a compressed playback stream is free running around the ring buffer, the firmware reads past the last valid byte until the decoder detects the end of the stream. When the stream fits into the buffer the data after the end is zero and the decoders can detect the end of stream reliably, but after the first wrap the free part of the buffer contains stale data from the previous lap, which the decoders try to decode. This can cause the stream to never end (RAW AAC) or to decode stale data. Clear the free part of the buffer on drain, from the application write position up to the DSP read position, so the end of the stream is always followed by zeros, like for streams that never wrapped the buffer. Signed-off-by: Peter Ujfalusi --- sound/soc/sof/ipc4-compress.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/sound/soc/sof/ipc4-compress.c b/sound/soc/sof/ipc4-compress.c index 97a4ec7519055e..53262629b8d4b6 100644 --- a/sound/soc/sof/ipc4-compress.c +++ b/sound/soc/sof/ipc4-compress.c @@ -559,6 +559,39 @@ static int sof_ipc4_compr_get_params(struct snd_soc_component *component, return 0; } +/* + * On DRAIN, zero the free part of the playback ring (from the application + * write position up to the DSP read position). The host DMA free-runs around + * the ring, so after the last valid byte the firmware reads whatever is left in + * the buffer: after the first wrap this is stale data from the previous lap, + * which decoders try to decode. With the area zeroed the end of the stream is + * followed by zeros, as it is for streams that never wrapped the ring. + */ +static void sof_ipc4_compr_drain_clear(struct snd_compr_stream *cstream) +{ + struct snd_compr_runtime *crtd = cstream->runtime; + u64 pending, offset, n; + u64 free_bytes; + + if (cstream->direction != SND_COMPRESS_PLAYBACK || !crtd->dma_area) + return; + + pending = crtd->total_bytes_available - crtd->total_bytes_transferred; + if (pending >= crtd->buffer_size) + return; + + free_bytes = crtd->buffer_size - pending; + div64_u64_rem(crtd->total_bytes_available, crtd->buffer_size, &offset); + n = crtd->buffer_size - offset; + + if (free_bytes <= n) { + memset(crtd->dma_area + offset, 0, free_bytes); + } else { + memset(crtd->dma_area + offset, 0, n); + memset(crtd->dma_area, 0, free_bytes - n); + } +} + static int sof_ipc4_compr_trigger(struct snd_soc_component *component, struct snd_compr_stream *cstream, int cmd) { @@ -601,6 +634,7 @@ static int sof_ipc4_compr_trigger(struct snd_soc_component *component, break; case SND_COMPR_TRIGGER_DRAIN: spcm->pending_stop[dir] = true; + sof_ipc4_compr_drain_clear(cstream); break; case SND_COMPR_TRIGGER_PARTIAL_DRAIN: case SND_COMPR_TRIGGER_NEXT_TRACK: From 86bf838019219b8b59c0af6df4105be146a62a01 Mon Sep 17 00:00:00 2001 From: Peter Ujfalusi Date: Thu, 3 Dec 2026 14:43:47 +0200 Subject: [PATCH 2/2] ASoC: SOF: ipc4-compress: Send the stream end to the decoder on drain The firmware cannot tell where the valid data ends in a compressed playback stream since the host DMA is free running around the buffer. The decoders rely on what follows the last valid byte, which is not reliable, for example the Cadence AAC decoder needs a full input buffer and cannot signal the end of stream on its own. Send the end of the stream (the absolute byte offset of the end, which is the size of the stream) to the decoder module on drain, so it can feed the codec with the exact amount of remaining data and finish decoding at the real end of the stream. The parameter is sent as a struct module_param record with 0xffff in the upper 16 bits of the id, which no codec specific parameter uses, so firmware without support for it skips the parameter. Signed-off-by: Peter Ujfalusi --- sound/soc/sof/ipc4-compress.c | 93 +++++++++++++++++++++++++++++++---- 1 file changed, 83 insertions(+), 10 deletions(-) diff --git a/sound/soc/sof/ipc4-compress.c b/sound/soc/sof/ipc4-compress.c index 53262629b8d4b6..a4cbe7a22986fb 100644 --- a/sound/soc/sof/ipc4-compress.c +++ b/sound/soc/sof/ipc4-compress.c @@ -28,8 +28,8 @@ struct sof_ipc4_compr_init_data { u32 dir; } __packed __aligned(4); -static struct sof_ipc4_process * -sof_ipc4_compr_get_module(struct snd_sof_pcm *spcm, int dir) +static struct snd_sof_widget * +sof_ipc4_compr_get_swidget(struct snd_sof_pcm *spcm, int dir) { int id = dir ? snd_soc_dapm_encoder : snd_soc_dapm_decoder; struct snd_sof_pcm_stream *sps = &spcm->stream[dir]; @@ -40,16 +40,21 @@ sof_ipc4_compr_get_module(struct snd_sof_pcm *spcm, int dir) for_each_dapm_widgets(sps->list, i, widget) { struct snd_sof_widget *swidget = widget->dobj.private; - if (!swidget) - continue; - - if (swidget->widget->id == id) - return swidget->private; + if (swidget && swidget->widget->id == id) + return swidget; } return NULL; } +static struct sof_ipc4_process * +sof_ipc4_compr_get_module(struct snd_sof_pcm *spcm, int dir) +{ + struct snd_sof_widget *swidget = sof_ipc4_compr_get_swidget(spcm, dir); + + return swidget ? swidget->private : NULL; +} + static u32 sof_ipc4_compr_calc_min_fragment_size(struct snd_sof_pcm_stream *sps) { u32 host_buffer_estimate; @@ -592,6 +597,74 @@ static void sof_ipc4_compr_drain_clear(struct snd_compr_stream *cstream) } } +/* + * Stream control parameters for compress modules. + * + * The parameters are sent as struct module_param records. Stream control + * parameters have SOF_IPC4_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 a firmware without support for these controls skips them. + */ +#define SOF_IPC4_COMPR_CTRL_ID 0xffff + +/* Absolute byte offset of the end of the stream (stream size), payload: u64 */ +#define SOF_IPC4_COMPR_CTRL_STREAM_END 1 + +#define SOF_IPC4_COMPR_CTRL_PARAM_ID(ctrl) ((SOF_IPC4_COMPR_CTRL_ID << 16) | (ctrl)) + +struct sof_ipc4_compr_ctrl_u64 { + u32 id; + u32 size; /* size of the whole record */ + u64 value; +} __packed __aligned(4); + +/* + * Tell the decoder the total number of bytes in the stream so it can + * feed the codec with the exact amount of remaining data and finish decoding + * at the real end of the stream instead of relying on what follows it in the + * buffer. + */ +static void sof_ipc4_compr_drain(struct snd_sof_dev *sdev, + struct snd_sof_pcm *spcm, + struct snd_compr_stream *cstream) +{ + const struct sof_ipc_ops *iops = sdev->ipc->ops; + struct sof_ipc4_compr_ctrl_u64 param; + int dir = cstream->direction; + struct sof_ipc4_fw_module *fw_module; + struct snd_sof_widget *swidget; + struct sof_ipc4_msg msg = {{ 0 }}; + int ret; + + if (dir != SND_COMPRESS_PLAYBACK) + return; + + swidget = sof_ipc4_compr_get_swidget(spcm, dir); + if (!swidget || !swidget->module_info) + return; + + fw_module = swidget->module_info; + + param.id = SOF_IPC4_COMPR_CTRL_PARAM_ID(SOF_IPC4_COMPR_CTRL_STREAM_END); + param.size = sizeof(param); + param.value = cstream->runtime->total_bytes_available; + + msg.primary = fw_module->man4_module_entry.id; + msg.primary |= SOF_IPC4_MOD_INSTANCE(swidget->instance_id); + msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); + msg.primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); + msg.data_ptr = ¶m; + msg.data_size = sizeof(param); + + sof_ipc4_compr_drain_clear(cstream); + + ret = iops->set_get_data(sdev, &msg, msg.data_size, true); + if (ret < 0) + spcm_err(spcm, dir, "failed to send stream end: %d\n", ret); + else + spcm_dbg(spcm, dir, "stream end: %llu\n", param.value); +} + static int sof_ipc4_compr_trigger(struct snd_soc_component *component, struct snd_compr_stream *cstream, int cmd) { @@ -609,6 +682,8 @@ static int sof_ipc4_compr_trigger(struct snd_soc_component *component, return -EINVAL; } + spcm_dbg(spcm, dir, "Entry: trigger (cmd: %d)\n", cmd); + switch (cmd) { case SNDRV_PCM_TRIGGER_START: /* @@ -634,7 +709,7 @@ static int sof_ipc4_compr_trigger(struct snd_soc_component *component, break; case SND_COMPR_TRIGGER_DRAIN: spcm->pending_stop[dir] = true; - sof_ipc4_compr_drain_clear(cstream); + sof_ipc4_compr_drain(sdev, spcm, cstream); break; case SND_COMPR_TRIGGER_PARTIAL_DRAIN: case SND_COMPR_TRIGGER_NEXT_TRACK: @@ -646,8 +721,6 @@ static int sof_ipc4_compr_trigger(struct snd_soc_component *component, return 0; } - spcm_dbg(spcm, dir, "Entry: trigger (cmd: %d)\n", cmd); - ret = pcm_ops->trigger(component, NULL, spcm, cmd, dir); if (ret < 0) { spcm_err(spcm, dir, "pcm_ops->trigger failed for cmd %d\n", cmd);