mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
* craft-audio: core types, WAV/AIFF read and write, peaks, symphonia The new crate without any codec module: what every later codec plugs into. - Shared types: `AudioBuffer`, `AudioInfo`, `ChannelLayout`/`Speaker`, `AudioError`, `EncodeOptions` (built with `new` and `with_*`), `EncodeFormat`, `BitDepth`, `BwfInfo`, `SampleFormat`. - WAV/BWF/RF64 and AIFF/AIFF-C reading and writing from SoundCraft's `soundcraft-audio-io`, kept over EffectCraft's and FilmCraft's smaller writers (only it has BWF, dither, any speaker mask and streaming without a known length). `encode` and `FileEncoder` behind `pcm-encode`; `lossless-encode` and `encode` turn it on. - `decode`, `probe`, `tracks` and the streaming, seekable `Decoder` (feature `decode-native`). The native path reads WAV, AIFF, and PCM in MP4/MOV and Matroska through craft-media's demuxers, packet by packet, with edit lists and `CodecDelay`/`DiscardPadding` applied. `TrackDecoder` decodes PCM; each codec PR adds its codec to it. - symphonia 0.6 (MPL-2.0, used unmodified) behind the `symphonia` feature, part of `decode`: the fallback for every format with no native decoder yet. Its size fields are checked before it reads, and its panics are caught where they unwind. - `peaks`: the min/max/RMS pyramid for waveform drawing. Workspace: craft-media is a workspace dependency without default features, so an encode-only craft-audio does not build the MP4 demuxer, and `symphonia-bundle-flac` builds without overflow checks in dev profiles (its 32-bit LPC restore wraps on valid input). CI: `audio.yml`, with clippy per feature, rustdoc, WASM, MSRV, and the tests with ffmpeg oracles. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * craft-audio: review fixes (CodeRabbit on #441) - Reading: AIFF that declares frames without an SSND chunk is Malformed; a Matroska PCM track's length counts from its first frame; seek targets saturate; an empty FLAC has one track; a failed header read is an Io error; deinterleave allocates fallibly; concealed silence is capped like a decoded packet, and symphonia's uses the damaged packet's own duration when known. - Peaks sum squares in f64, so loud float samples give a finite RMS. - Writing: .aifc maps to AIFF; BWF text is cut at a character boundary; a WAV byte rate past 32 bits is an error; the FileEncoder docs say nothing may follow the sink's position. - README: the Limits list says which codecs go through symphonia and which MP4 edit lists apply. - Tests for each, alias and dither coverage, and a FLAC case in the routing oracle. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * craft-audio: review fixes, second round (CodeRabbit on #441) - Every header read is checked: `Native::open`, `native::tracks` and the no-symphonia error read it with `head_checked`. craft-media demuxer errors that are I/O failures (not a file ending early) are returned as `AudioError::Io` instead of sending the file to the other path. - A Matroska track's presented length runs from its first frame plus `CodecDelay` (unclamped, in timestamp ticks with the delay rounded to one) to its end. - symphonia's concealment allocates fallibly, and a damaged first packet is concealed when the demuxer gives its duration. `deinterleave` reserves its channel list fallibly too. - `TrackDecoder::conceal` documents its cap at the decode limit. - Tests: an I/O failure past the first 64 KiB, codec delay with the first block at -20, 0 and 50 ms (checked with `probe`), a seek back after an overshoot, and the loud-sample RMS value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * craft-audio: TrackDecoder::conceal returns a Result Concealment allocates fallibly and fails with TooLarge when the silence would pass the 2^30-sample decode limit, instead of returning less than the packet's length. The native path, the README, the packets example and the doc example pass the error on. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * craft-audio: review fixes, third round (CodeRabbit on #441) - Concealed silence is held to the decode limit inside pcm::silence, so symphonia's path checks frames × channels too, and symphonia's decoded audio is copied into fallibly allocated planes. - TrackDecoder::reset forgets the last packet's length. - Without symphonia, a damaged MP4, Matroska or Ogg file (truncated, invalid, no moov) is Malformed instead of Unsupported; a feature the demuxer does not read still names the build. - Tests: an MP4 with no moov in a native-only build, conceal after a reset and past the limit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * craft-audio: review fixes, fourth round (CodeRabbit on #441) - A file the native demuxers find damaged is Malformed with symphonia too, when symphonia calls it unsupported; before, an ftyp-only MP4 was Unsupported in symphonia builds. - symphonia's decoded blocks are held to the 2^30-sample decode limit before allocating. - Symph::seek forgets the last packet's length, so a lost packet after a seek is not concealed with the length of one before it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * craft-audio: review fixes, fifth round (CodeRabbit on #441) - The damaged-file test covers Matroska as well as MP4, and fails when either branch of native::damage is removed. - native::damage leaves out Ogg: ogg::open reports no damage (it skips damaged pages and calls a file with none it can read not Ogg), and symphonia reports damaged Ogg as malformed itself. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * craft-audio: run the ffmpeg oracle tests on ubuntu-26.04 ubuntu-latest's apt ships ffmpeg 6.1.1, which writes MP2 and E-AC-3 in Matroska with ends a few milliseconds early, so the routing tests on the later codec PRs cannot pass on it. ubuntu-26.04 ships ffmpeg 8.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
157 lines
6.0 KiB
Rust
157 lines
6.0 KiB
Rust
//! Byte-level PCM helpers shared by the WAV, AIFF and FLAC readers: bounded readers and sample
|
|
//! decoding to `f32`.
|
|
|
|
use crate::{AudioError, Result};
|
|
|
|
/// Maximum number of decoded `f32` samples (all channels) we will produce: 4 GiB of output.
|
|
pub(crate) const MAX_DECODE_SAMPLES: u64 = (4u64 << 30) / 4;
|
|
|
|
/// Little-endian `u16` at `pos`.
|
|
#[cfg(feature = "decode-native")]
|
|
pub(crate) fn le_u16(b: &[u8], pos: usize) -> Option<u16> {
|
|
let s = b.get(pos..pos.checked_add(2)?)?;
|
|
Some(u16::from_le_bytes([*s.first()?, *s.get(1)?]))
|
|
}
|
|
|
|
/// Little-endian `u32` at `pos`.
|
|
#[cfg(feature = "decode-native")]
|
|
pub(crate) fn le_u32(b: &[u8], pos: usize) -> Option<u32> {
|
|
let s: [u8; 4] = b.get(pos..pos.checked_add(4)?)?.try_into().ok()?;
|
|
Some(u32::from_le_bytes(s))
|
|
}
|
|
|
|
/// Little-endian `u64` at `pos`.
|
|
#[cfg(feature = "decode-native")]
|
|
pub(crate) fn le_u64(b: &[u8], pos: usize) -> Option<u64> {
|
|
let s: [u8; 8] = b.get(pos..pos.checked_add(8)?)?.try_into().ok()?;
|
|
Some(u64::from_le_bytes(s))
|
|
}
|
|
|
|
/// Big-endian `u16` at `pos`.
|
|
#[cfg(feature = "decode-native")]
|
|
pub(crate) fn be_u16(b: &[u8], pos: usize) -> Option<u16> {
|
|
let s: [u8; 2] = b.get(pos..pos.checked_add(2)?)?.try_into().ok()?;
|
|
Some(u16::from_be_bytes(s))
|
|
}
|
|
|
|
/// Big-endian `u32` at `pos`.
|
|
#[cfg(feature = "decode-native")]
|
|
pub(crate) fn be_u32(b: &[u8], pos: usize) -> Option<u32> {
|
|
let s: [u8; 4] = b.get(pos..pos.checked_add(4)?)?.try_into().ok()?;
|
|
Some(u32::from_be_bytes(s))
|
|
}
|
|
|
|
/// Big-endian `u64` at `pos`.
|
|
#[cfg(feature = "symphonia")]
|
|
#[cfg(feature = "decode-native")]
|
|
pub(crate) fn be_u64(b: &[u8], pos: usize) -> Option<u64> {
|
|
let s: [u8; 8] = b.get(pos..pos.checked_add(8)?)?.try_into().ok()?;
|
|
Some(u64::from_be_bytes(s))
|
|
}
|
|
|
|
/// Four-byte tag at `pos`.
|
|
#[cfg(feature = "decode-native")]
|
|
pub(crate) fn tag(b: &[u8], pos: usize) -> Option<[u8; 4]> {
|
|
b.get(pos..pos.checked_add(4)?)?.try_into().ok()
|
|
}
|
|
|
|
/// How the samples in a PCM byte stream are encoded.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub(crate) enum PcmKind {
|
|
/// Signed (or, for 1-byte WAV, unsigned) integers occupying `bytes` bytes each, left-justified.
|
|
Int { bytes: usize, big_endian: bool, unsigned: bool },
|
|
/// IEEE float, 4 or 8 bytes.
|
|
Float { bytes: usize, big_endian: bool },
|
|
}
|
|
|
|
impl PcmKind {
|
|
pub(crate) fn bytes(self) -> usize {
|
|
match self {
|
|
PcmKind::Int { bytes, .. } | PcmKind::Float { bytes, .. } => bytes,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Decode one sample from exactly `kind.bytes()` bytes. Returns 0 for a short slice.
|
|
fn decode_sample(s: &[u8], kind: PcmKind) -> f32 {
|
|
match kind {
|
|
PcmKind::Int { bytes, big_endian, unsigned } => {
|
|
if bytes == 0 || bytes > 4 || s.len() < bytes {
|
|
return 0.0;
|
|
}
|
|
let mut acc: u32 = 0;
|
|
for i in 0..bytes {
|
|
let idx = if big_endian { i } else { bytes - 1 - i };
|
|
let mut byte = s.get(idx).copied().unwrap_or(0);
|
|
if unsigned && i == 0 {
|
|
byte ^= 0x80;
|
|
}
|
|
acc = (acc << 8) | u32::from(byte);
|
|
}
|
|
let shifted = acc << (32 - 8 * bytes as u32);
|
|
(f64::from(shifted as i32) / 2_147_483_648.0) as f32
|
|
}
|
|
PcmKind::Float { bytes, big_endian } => {
|
|
let v = match bytes {
|
|
4 => {
|
|
let Some(a) = s.get(..4).and_then(|x| <[u8; 4]>::try_from(x).ok()) else { return 0.0 };
|
|
if big_endian { f32::from_be_bytes(a) } else { f32::from_le_bytes(a) }
|
|
}
|
|
8 => {
|
|
let Some(a) = s.get(..8).and_then(|x| <[u8; 8]>::try_from(x).ok()) else { return 0.0 };
|
|
(if big_endian { f64::from_be_bytes(a) } else { f64::from_le_bytes(a) }) as f32
|
|
}
|
|
_ => 0.0,
|
|
};
|
|
if v.is_finite() { v } else { 0.0 }
|
|
}
|
|
}
|
|
}
|
|
|
|
/// `channels` empty channels with room for `frames` samples each, allocated fallibly.
|
|
pub(crate) fn planar(channels: usize, frames: usize) -> Result<Vec<Vec<f32>>> {
|
|
let too_large = || AudioError::TooLarge("cannot allocate decoded audio".into());
|
|
let mut out = Vec::new();
|
|
out.try_reserve_exact(channels).map_err(|_| too_large())?;
|
|
for _ in 0..channels {
|
|
let mut ch = Vec::new();
|
|
ch.try_reserve_exact(frames).map_err(|_| too_large())?;
|
|
out.push(ch);
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
/// `channels` channels of `frames` samples of silence, allocated fallibly and held to the decode
|
|
/// limit like decoded audio.
|
|
pub(crate) fn silence(channels: usize, frames: usize) -> Result<Vec<Vec<f32>>> {
|
|
let total = (frames as u64).saturating_mul(channels as u64);
|
|
if total > MAX_DECODE_SAMPLES {
|
|
return Err(AudioError::TooLarge(format!("{total} samples of concealment exceed the {MAX_DECODE_SAMPLES}-sample decode limit")));
|
|
}
|
|
let mut out = planar(channels, frames)?;
|
|
for ch in &mut out {
|
|
ch.resize(frames, 0.0);
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
/// Deinterleave `frames` frames of `channels`-channel PCM from `data` into planar `f32`.
|
|
pub(crate) fn deinterleave(data: &[u8], kind: PcmKind, channels: usize, frames: u64) -> Result<Vec<Vec<f32>>> {
|
|
let bytes = kind.bytes();
|
|
let block = bytes.checked_mul(channels).filter(|&b| b > 0).ok_or_else(|| AudioError::Malformed("zero-sized sample frame".into()))?;
|
|
let available = (data.len() / block) as u64;
|
|
let frames = frames.min(available);
|
|
let total = frames.checked_mul(channels as u64).ok_or_else(|| AudioError::TooLarge("sample count overflows".into()))?;
|
|
if total > MAX_DECODE_SAMPLES {
|
|
return Err(AudioError::TooLarge(format!("{total} samples exceeds the {MAX_DECODE_SAMPLES}-sample decode limit")));
|
|
}
|
|
let frames = usize::try_from(frames).map_err(|_| AudioError::TooLarge("frame count exceeds address space".into()))?;
|
|
let mut out = planar(channels, frames)?;
|
|
for frame in data.chunks_exact(block).take(frames) {
|
|
for (ch, s) in out.iter_mut().zip(frame.chunks_exact(bytes)) {
|
|
ch.push(decode_sample(s, kind));
|
|
}
|
|
}
|
|
Ok(out)
|
|
}
|