|
|
@@ -149,10 +149,20 @@ fn next_frame(data: &[u8], start: usize, end: usize) -> Option<usize> {
|
|
|
}
|
|
|
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)
|
|
|
@@ -1047,6 +1057,35 @@ mod tests {
|
|
|
assert_eq!(frame.next, None);
|
|
|
}
|
|
|
|
|
|
+ /// 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<f32> {
|