From 10e806fcc74fb3119fbcb9adffb983ae531185c6 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 01:26:32 +0100 Subject: [PATCH] xma: start the frame after a continuation at the next packet that has frames A frame that ends in a packet where no frame begins (the tail of a frame that crossed packets, then padding) left the next frame pointing into that padding, which ended the stream early. Now the next frame is found by following each packet's skip to the next packet with frames. (cherry picked from commit ba42db643c15c1b7fb17ba40464dddda36c1db60) --- crates/viport-formats/src/xma.rs | 47 +++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/crates/viport-formats/src/xma.rs b/crates/viport-formats/src/xma.rs index 2e2d9b4c..05f2f816 100644 --- a/crates/viport-formats/src/xma.rs +++ b/crates/viport-formats/src/xma.rs @@ -210,10 +210,20 @@ fn next_frame(data: &[u8], start: usize, end: usize) -> Option { } let last = number + 1 >= header.frames; let mut end = end; - if end.is_multiple_of(PACKET_BITS) || (last && end / PACKET_BITS == index) { - // The next frame begins in the next packet of this stream. - let next = (end - 1) / PACKET_BITS + 1 + packet(data, (end - 1) / PACKET_BITS)?.skip as usize; - let header = packet(data, next)?; + // A frame that ends in a packet where no frame begins (the tail of a frame + // that crossed packets, then padding) is the last one before the next + // packet that has frames. + let ends_in_packet_without_frames = + end / PACKET_BITS != index && !end.is_multiple_of(PACKET_BITS) && packet(data, end / PACKET_BITS)?.frames == 0; + if end.is_multiple_of(PACKET_BITS) || (last && end / PACKET_BITS == index) || ends_in_packet_without_frames { + // The next frame begins in the next packet of this stream that has one; + // a packet with none (a frame spans all of it) holds no start. + let mut next = (end - 1) / PACKET_BITS + 1 + packet(data, (end - 1) / PACKET_BITS)?.skip as usize; + let mut header = packet(data, next)?; + while header.frames == 0 { + next += 1 + header.skip as usize; + header = packet(data, next)?; + } end = next * PACKET_BITS + HEADER_BITS + header.first_frame_bit as usize; } (end / 8 < data.len()).then_some(end) @@ -1518,6 +1528,35 @@ mod tests { assert_eq!(next, 32 + NEEDED); } + /// A frame that crosses a packet where no frame begins (a full + /// continuation) and ends in another such packet, followed by padding: the + /// next frame is the first one of the next packet that has frames, not the + /// padding the frame ended in. + #[test] + fn the_frame_after_continuation_packets_starts_in_the_next_packet_with_frames() { + let mut data = vec![0u8; 4 * PACKET_BYTES]; + let header = |frames: u32, first: u32| (frames << 26) | (first << 11); + put_bits(&mut data, 0, 32, header(2, 0)); + // Packet 1 is all of one frame; packet 2 holds only its last 64 bits. + put_bits(&mut data, PACKET_BITS, 32, header(0, 0x7fff)); + put_bits(&mut data, 2 * PACKET_BITS, 32, header(0, 64)); + put_bits(&mut data, 3 * PACKET_BITS, 32, header(1, 0)); + // A first frame of 16000 bits, then one from the end of its payload + // through packets 1 and 2. + let second = HEADER_BITS + 16000; + put_bits(&mut data, HEADER_BITS, LENGTH_BITS, 16000); + let tail = PACKET_BITS - second; + let bits = tail + (PACKET_BITS - HEADER_BITS) + 64; + put_bits(&mut data, second, LENGTH_BITS, bits as u32); + put_bits(&mut data, 3 * PACKET_BITS + HEADER_BITS, LENGTH_BITS, 100); + let first = read_frame(&data, HEADER_BITS).unwrap(); + assert_eq!(first.next, Some(second)); + let frame = read_frame(&data, second).unwrap(); + assert_eq!(frame.bits, bits); + assert_eq!(frame.next, Some(3 * PACKET_BITS + HEADER_BITS)); + assert_eq!(read_frame(&data, 3 * PACKET_BITS + HEADER_BITS).unwrap().next, None); + } + /// The direct sum `imdct_half` stands for (FFmpeg's naive inverse MDCT: /// the standard formula, negated). fn imdct_reference(input: &[f32]) -> Vec {