Skip to content

Decode the frames before a seek target in the Symphonia decoder - #170

Open
yzhou216 wants to merge 2 commits into
tesselode:mainfrom
yzhou216:seek-preroll
Open

yzhou216 wants to merge 2 commits into
tesselode:mainfrom
yzhou216:seek-preroll

Conversation

@yzhou216

@yzhou216 yzhou216 commented Nov 11, 2026 •

Copy link
Copy Markdown

AAC, MP3, and Vorbis need the frames before a frame to decode it. SymphoniaDecoder::seek jumps straight to the target with format_reader.seek: it never resets the decoder, and never decodes anything before the target. Symphonia leaves both steps to the caller, so the first frame or two after every seek come out wrong.

Kira seeks whenever a streaming sound starts partway into its file, and again each time a loop region wraps. A looping AAC or MP3 sound therefore crackles briefly at every loop point. Against a straight decode of the same file, the worst sample error after a seek was about 0.16 for AAC and MP3. Ogg Vorbis goes wrong differently: the frames after a seek begin before the index that seek reports.

To reproduce, check out the first commit, which adds only the test, and run cargo test --package kira --all-features --lib symphonia. All three new tests fail, for example:

seek(7167) returned 6144, and frame 6144 decoded after it is Frame { left: 0.0, right: 0.0 }, but decoding from the start gives Frame { left: -0.119132124, right: -0.119132124 }

What the fix changes:

  • seek now goes to 2048 frames before the target (more than one AAC packet of 1024 frames or MP3 packet of 1152), calls AudioDecoder::reset, and decodes until a packet reaches the target.
  • That packet's frames from the target onward wait for the next decode call. seek returns the target, or the packet's start if that is later.
  • A packet's first frame index comes from the packet itself (pts + trim_start) rather than a running count, because Ogg Vorbis outputs nothing for the first packet after a reset even though the file counts frames for it.
  • decode and seek share a new private decode_packet. The public API is unchanged.

The test lives in decoder/symphonia/test.rs. It decodes a 0.4 second tone in one pass, then walks backwards from the end in steps of 61 frames, seeking to each, decoding 1024 frames, and comparing them with the one-pass decode within 1e-5. Small AAC, MP3, and Ogg Vorbis versions of the tone come with it (1.7 KB, 1.2 KB, and 3.8 KB); the comment at the top of the test gives the ffmpeg command that made them. A frame decoded without its predecessors is off by 0.07 or more, far outside the tolerance. The AAC test needs the aac and isomp4 features, so it runs only with --all-features; MP3 and Ogg Vorbis run with the defaults.

I ran clippy with all features, and the tests in the four configurations CI uses (none, cpal, mp3, all features), on Rust 1.86 and 1.99, then again on the current nightly after splitting the commits. There's no changelog entry, as I understand you write those at release time.

This touches the same lines of decode as #168; I'm happy to rebase onto whichever lands first.

@tesselode

Copy link
Copy Markdown
Owner

Can you split this into two commits: one with just the new test and one with the fix?

Do you have a citation for this:

Symphonia leaves both to the caller, so the first frame or two after every seek come out wrong.

My reading of the docs is that the Decoder is expected to detect a seek and call reset internally. If it doesn't call reset, we should just add a call to reset and that should be it. We shouldn't have to seek earlier than requested because that's what format readers already do: they seek to the beginning of the packet. So if something really is decoding wrong, it might be a bug in Symphonia, not Kira. But I'm not totally convinced about that because I don't understand the test.

frame 6144 after seeking to 7167 is Frame...

Why are we looking at a frame before the one we seeked to? That doesn't make sense to me.

Seeking to a frame and decoding from there should give the same frames as decoding the whole file from the start.  The test checks this for AAC, MP3, and Ogg Vorbis.  It fails on all three, which the next commit fixes.
AAC, MP3, and Vorbis decode each frame using the frames before it.
`SymphoniaDecoder::seek` sought straight to the target frame, without
resetting the decoder or decoding the frames before the target, so the
first frame or two after a seek came out wrong. Kira seeks where a
streaming sound starts partway into its file and at every wrap of a loop
region, so looping AAC and MP3 sounds had a short burst of noise at each
loop point.

Seek 2048 frames before the target (more than a packet of AAC or MP3),
reset the decoder, and decode packets until one reaches the target.
`decode` returns the frames from the target to the end of that packet
first. The first frame of each packet is taken from the packet, as the
first packet an Ogg Vorbis decoder decodes after a reset has no frames,
though the file counts frames for it.

The test compares the frames decoded after seeks to many indices with a
straight decode of a short tone in AAC, MP3, and Ogg Vorbis. It fails
without the change.
@yzhou216

Copy link
Copy Markdown
Author

I've split it in two.

It's in Symphonia's docs for FormatReader::seek: "After a seek, all Decoders consuming packets from this reader should be reset" and "to seek to an exact frame, a Decoder must decode packets until the requested position is reached." The decoder doesn't do it by itself.

I tried just adding reset. The test fails the same way as on main. I also tried reset plus decoding from the target's packet. AAC is still off by 0.06, MP3 by 0.13, and Ogg Vorbis lands past the target. Only starting earlier passes.

AAC, MP3, and Vorbis overlap each packet with the previous one, so the first packet you decode after a seek comes out wrong. You have to start a packet early and throw away the extra frames. Opus does the same thing (RFC 7845 section 4.6). I don't think it's a Symphonia bug.

About frame 6144: seek(7167) returned 6144, the start of that AAC packet, so 6144 is the first frame we get back. I've changed the message to say so:

seek(7167) returned 6144, and frame 6144 decoded after it is Frame { left: 0.0, right: 0.0 }, but decoding from the start gives Frame { left: -0.119132124, right: -0.119132124 }

This branch has not been deployed

No deployments
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