Files
psysonic/src-tauri/patches/symphonia-format-isomp4/src/atoms/tkhd.rs
T
Psychotoxical a4c400cc69 fix(audio): correctly parse iTunSMPB gapless info from older iTunes M4A files
M4A files store the iTunSMPB value behind a 16-byte binary 'data' atom
header. The previous parser took bytes directly after the ASCII key,
landing in the binary header and producing a corrupted parts array
(total_valid=348 instead of ~13M samples), which caused take_duration
to exhaust the source in ~8ms — silent no-playback.

Fix: search for the " 00000000 " sentinel within 128 bytes of the key
to locate the actual value string, falling back to the old offset for
ID3v2/MP3 files where no binary header exists.

Also patches symphonia-format-isomp4 (local crate under patches/) to:
- Tolerate SL descriptor predefined=0x01 (older iTunes M4A)
- Convert descriptor.unwrap() panic to a proper decode error
- Gracefully skip malformed trak atoms (e.g. MJPEG cover-art streams)
  instead of failing the entire probe

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-07 18:54:10 +02:00

90 lines
2.7 KiB
Rust

// Symphonia
// Copyright (c) 2019-2022 The Project Symphonia Developers.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use symphonia_core::errors::{decode_error, Result};
use symphonia_core::io::ReadBytes;
use crate::atoms::{Atom, AtomHeader};
use crate::fp::FpU8;
/// Track header atom.
#[allow(dead_code)]
#[derive(Debug)]
pub struct TkhdAtom {
/// Atom header.
header: AtomHeader,
/// Track header flags.
pub flags: u32,
/// Creation time.
pub ctime: u64,
/// Modification time.
pub mtime: u64,
/// Track identifier.
pub id: u32,
/// Track duration in the timescale units specified in the movie header. This value is equal to
/// the sum of the durations of all the track's edits.
pub duration: u64,
/// Layer.
pub layer: u16,
/// Grouping identifier.
pub alternate_group: u16,
/// Preferred volume for track playback.
pub volume: FpU8,
}
impl Atom for TkhdAtom {
fn header(&self) -> AtomHeader {
self.header
}
fn read<B: ReadBytes>(reader: &mut B, header: AtomHeader) -> Result<Self> {
let (version, flags) = AtomHeader::read_extra(reader)?;
let mut tkhd = TkhdAtom {
header,
flags,
ctime: 0,
mtime: 0,
id: 0,
duration: 0,
layer: 0,
alternate_group: 0,
volume: Default::default(),
};
// Version 0 uses 32-bit time values, verion 1 used 64-bit values.
match version {
0 => {
tkhd.ctime = u64::from(reader.read_be_u32()?);
tkhd.mtime = u64::from(reader.read_be_u32()?);
tkhd.id = reader.read_be_u32()?;
let _ = reader.read_be_u32()?; // Reserved
tkhd.duration = u64::from(reader.read_be_u32()?);
}
1 => {
tkhd.ctime = reader.read_be_u64()?;
tkhd.mtime = reader.read_be_u64()?;
tkhd.id = reader.read_be_u32()?;
let _ = reader.read_be_u32()?; // Reserved
tkhd.duration = reader.read_be_u64()?;
}
_ => return decode_error("isomp4: invalid tkhd version"),
}
// Reserved
let _ = reader.read_be_u64()?;
tkhd.layer = reader.read_be_u16()?;
tkhd.alternate_group = reader.read_be_u16()?;
tkhd.volume = FpU8::parse_raw(reader.read_be_u16()?);
// The remainder of the header is only useful for video tracks.
Ok(tkhd)
}
}