Skip to content

ASoC: SOF: ipc4-compress: Fix the end of stream handling on drain - #6079

Open
ujfalusi wants to merge 2 commits into
thesofproject:topic/sof-devfrom
ujfalusi:peter/sof/pr/compr-drain-clear
Open

ujfalusi wants to merge 2 commits into
thesofproject:topic/sof-devfrom
ujfalusi:peter/sof/pr/compr-drain-clear

Conversation

@ujfalusi

@ujfalusi ujfalusi commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

The host DMA of a compressed playback stream is free running around the
ring buffer, so after the last valid byte the firmware keeps reading
whatever is left in the buffer until the decoder detects the end of the
stream. As long as the stream fits into the buffer the data after the end
is zero, but once the buffer has wrapped it is stale data from the
previous lap, and the decoders try to decode it. This shows up as:

  • RAW AAC (M4A): fatal decoder error on the stale data, the drain never
    completes and the application hangs
  • ADTS AAC: stale or corrupt frames can end the stream early or decode
    old data

The two patches:

  1. Clear the free part of the buffer on drain, from the application write
    position up to the DSP read position. The end of the stream is then
    always followed by zeros, as for streams that never wrapped the buffer.
    This needs no firmware change and fixes the RAW AAC hang with existing
    firmware.
  2. 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, as a
    struct module_param record with 0xffff in the upper 16 bits of the
    id. A decoder that supports it can feed the codec with the exact amount
    of remaining data and finish at the real end of the stream, instead of
    guessing from what follows it. Firmware without support skips the
    record (no IPC error); the zeroing from patch 1 remains the fallback.

Firmware support for the stream end control in the Cadence module:
thesofproject/sof#11611

Tested on PTL (SoundWire, compress playback) with the existing firmware
and with the firmware supporting the stream end:

  • AAC: RAW (M4A) and ADTS, mono and stereo, streams that fit into the
    buffer and that wrap it (90 s, 285 s)
  • MP3, FLAC and PCM (WAV) streams of 3 s to 285 s
    All streams play to their real end and the drain completes.

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 <peter.ujfalusi@linux.intel.com>
Copilot AI balanced review requested due to automatic review settings December 3, 2026 13:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The drain path uses a stale playback count and can permanently skip clearing freed buffer space, allowing wrapped streams to decode stale data or hang during draining.

1 open issue
What changed in this PR

This PR aims to prevent stale audio data from disrupting end-of-stream detection in SOF IPC4 compressed playback.

Changes:

  • Adds a helper to zero unused ring-buffer space, including wrapped regions.
  • Calls the helper when a playback stream drains.
File Description
sound/​soc/​sof/​ipc4-compress.c Adds ring-buffer clearing during drain to remove stale playback data.

Review effort: Balanced

Comment on lines +579 to +581
pending = crtd->total_bytes_available - crtd->total_bytes_transferred;
if (pending >= crtd->buffer_size)
return;
Copilot AI balanced review requested due to automatic review settings December 3, 2026 15:46
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 <peter.ujfalusi@linux.intel.com>
@ujfalusi ujfalusi changed the title ASoC: SOF: ipc4-compress: Clear the free part of the buffer on drain ASoC: SOF: ipc4-compress: Fix the end of stream handling on drain Dec 3, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Drain relies on stale transfer counters and can skip clearing freed buffer space, allowing stale data to be decoded or preventing drain completion on firmware that ignores stream-end control.

2 open issues

Review effort: Balanced

Comment on lines +584 to +586
pending = crtd->total_bytes_available - crtd->total_bytes_transferred;
if (pending >= crtd->buffer_size)
return;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants