codecs: use shared DVD and Blu-ray LPCM decoding

This commit is contained in:
Dylan M. Taylor
2026-11-15 18:16:40 +00:00
parent efc74da96f
commit 3dc6ff20b2
3 changed files with 10 additions and 101 deletions
Generated
+5 -5
View File
@@ -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",
+3 -3
View File
@@ -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" }
+2 -93
View File
@@ -696,99 +696,8 @@ pub fn latm_split(frame: &[u8], last: &mut Option<LatmConfig>) -> Option<(Vec<u8
Some((cfg.asc, payload))
}
/// Blu-ray / AVCHD LPCM packet header (4 bytes before the samples).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BlurayLpcm {
pub sample_rate: u32,
pub channels: usize,
pub bits: u32,
}
/// Channels of each channel_assignment code (mono, stereo, 3/0, 2/1, 3/1, 2/2, 3/2, 3/2+LFE,
/// 3/4, 3/4+LFE).
const BD_LPCM_CHANNELS: [usize; 12] = [0, 1, 0, 2, 3, 3, 4, 4, 5, 6, 7, 8];
pub fn bluray_lpcm_header(p: &[u8]) -> Option<BlurayLpcm> {
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<Vec<Vec<f32>>> {
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<Vec<f32>> {
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<Vec<f32>> {