From 3dc6ff20b277e7f6fae3e4ababfd4de0ddeea641 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:16:40 +0000 Subject: [PATCH] codecs: use shared DVD and Blu-ray LPCM decoding --- Cargo.lock | 10 ++-- Cargo.toml | 6 +-- crates/codecs/src/audio.rs | 95 +------------------------------------- 3 files changed, 10 insertions(+), 101 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf45f8c36..78d5f3f2a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1061,10 +1061,10 @@ dependencies = [ [[package]] name = "craft-audio" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0#64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0" +source = "git+https://github.com/storytold/craft-libs?rev=c5749f5d06aa492c685c10cea51cff0279c90a04#c5749f5d06aa492c685c10cea51cff0279c90a04" dependencies = [ "craft-media", - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c5749f5d06aa492c685c10cea51cff0279c90a04)", "log", "rayon", "symphonia 0.6.1", @@ -1130,7 +1130,7 @@ dependencies = [ [[package]] name = "craft-media" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0#64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0" +source = "git+https://github.com/storytold/craft-libs?rev=c5749f5d06aa492c685c10cea51cff0279c90a04#c5749f5d06aa492c685c10cea51cff0279c90a04" dependencies = [ "chrono", "thiserror 2.0.21", @@ -1195,7 +1195,7 @@ dependencies = [ [[package]] name = "craft-video" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0#64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0" +source = "git+https://github.com/storytold/craft-libs?rev=c5749f5d06aa492c685c10cea51cff0279c90a04#c5749f5d06aa492c685c10cea51cff0279c90a04" dependencies = [ "craft-media", "rayon", @@ -1234,7 +1234,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0#64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0" +source = "git+https://github.com/storytold/craft-libs?rev=c5749f5d06aa492c685c10cea51cff0279c90a04#c5749f5d06aa492c685c10cea51cff0279c90a04" dependencies = [ "quick-xml 0.41.0", "serde", diff --git a/Cargo.toml b/Cargo.toml index 24d19f47e..aa9bea942 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,10 +59,10 @@ craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs" craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both # are on one commit, so the types they share (CodecParams, ByteSource) are the same. -craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0", default-features = false } -craft-audio = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0", default-features = false, features = ["aac-decode", "aac-encode", "rayon", "ac3-decode", "mpa-decode", "mp3-decode", "decode-native", "symphonia", "opus-decode", "flac-encode", "mp3-encode"] } +craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "c5749f5d06aa492c685c10cea51cff0279c90a04", default-features = false } +craft-audio = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "c5749f5d06aa492c685c10cea51cff0279c90a04", default-features = false, features = ["aac-decode", "aac-encode", "rayon", "ac3-decode", "mpa-decode", "mp3-decode", "decode-native", "symphonia", "opus-decode", "flac-encode", "mp3-encode"] } # The video codecs (including H.264 encode, APV encode, ProRes and decoders), on the same commit as craft-media. -craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0", features = ["h264-decode", "h264-encode", "hevc-decode", "vp9-decode", "av1-decode", "vp8-decode", "mpeg2", "apv", "prores", "threads"] } +craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "c5749f5d06aa492c685c10cea51cff0279c90a04", features = ["h264-decode", "h264-encode", "hevc-decode", "vp9-decode", "av1-decode", "vp8-decode", "mpeg2", "apv", "prores", "threads"] } # Exact time, EDL / ALE and the interchange report; serde for the project file's time fields. craft-timeline = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", features = ["serde"] } craft-captions = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } diff --git a/crates/codecs/src/audio.rs b/crates/codecs/src/audio.rs index 8bccf4929..8aa9067cc 100644 --- a/crates/codecs/src/audio.rs +++ b/crates/codecs/src/audio.rs @@ -696,99 +696,8 @@ pub fn latm_split(frame: &[u8], last: &mut Option) -> Option<(Vec Option { - if p.len() < 4 { - return None; - } - let channels = *BD_LPCM_CHANNELS.get((p[2] >> 4) as usize).filter(|&&c| c > 0)?; - let sample_rate = match p[2] & 15 { - 1 => 48_000, - 4 => 96_000, - 5 => 192_000, - _ => return None, - }; - let bits = match p[3] >> 6 { - 1 => 16, - 2 => 20, - 3 => 24, - _ => return None, - }; - Some(BlurayLpcm { sample_rate, channels, bits }) -} - -/// Decode one Blu-ray LPCM packet (big-endian; channels padded to an even count; 20-bit -/// samples are stored in 24 bits). -pub fn decode_bluray_lpcm(p: &[u8]) -> Option>> { - let h = bluray_lpcm_header(p)?; - let coded = (h.channels + 1) & !1; - let bps = if h.bits == 16 { 2 } else { 3 }; - let data = &p[4..]; - let n = data.len() / (coded * bps); - let mut out = vec![Vec::with_capacity(n); h.channels]; - for k in 0..n { - for (c, o) in out.iter_mut().enumerate() { - let at = (k * coded + c) * bps; - let v = if bps == 2 { - i16::from_be_bytes([data[at], data[at + 1]]) as f32 / 32768.0 - } else { - (i32::from_be_bytes([data[at], data[at + 1], data[at + 2], 0]) >> 8) as f32 / 8_388_608.0 - }; - o.push(v); - } - } - Some(out) -} - -/// Bytes per two sample frames of DVD LPCM. -pub fn dvd_lpcm_group_bytes(channels: usize, bits: u32) -> usize { - match bits { - 16 => 4 * channels, - 20 => 5 * channels, - _ => 6 * channels, - } -} - -/// Decode DVD-Video LPCM (big-endian). 16-bit samples are interleaved; 20- and 24-bit samples -/// come in groups of two frames: the upper 16 bits of all the group's samples, then their low -/// 4 / 8 bits. -pub fn decode_dvd_lpcm(data: &[u8], channels: usize, bits: u32) -> Vec> { - let ch = channels.max(1); - let group = dvd_lpcm_group_bytes(ch, bits); - let groups = data.len() / group; - let mut out = vec![Vec::with_capacity(groups * 2); ch]; - for g in 0..groups { - let b = &data[g * group..(g + 1) * group]; - for f in 0..2 { - for (c, o) in out.iter_mut().enumerate() { - let k = f * ch + c; - let hi = i16::from_be_bytes([b[2 * k], b[2 * k + 1]]) as i32; - let v = match bits { - 16 => hi as f32 / 32768.0, - 20 => { - let nib = b[4 * ch + k / 2]; - let lo = if k.is_multiple_of(2) { nib >> 4 } else { nib & 15 } as i32; - ((hi << 4) | lo) as f32 / 524_288.0 - } - _ => ((hi << 8) | b[4 * ch + k] as i32) as f32 / 8_388_608.0, - }; - o.push(v); - } - } - } - out -} +/// Blu-ray and DVD LPCM packet decoding from the shared audio library. +pub use craft_audio::lpcm::{BlurayLpcm, bluray_lpcm_header, decode_bluray_lpcm, decode_dvd_lpcm, dvd_lpcm_group_bytes}; /// Decode interleaved PCM bytes into planar f32. pub fn decode_pcm(data: &[u8], cfg: &filmcraft_isobmff::PcmConfig) -> Vec> {