From 45ddc4ffa6243c5509ef34c8b5260acdcc90b645 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 12 Nov 2026 22:05:44 +0100 Subject: [PATCH 001/150] mpegts: a repeated PAT or PMT packet is not appended to the section in progress The scan fed every PAT and PMT packet to the section parser, so a packet sent twice (same continuity counter and payload, H.222.0 2.4.3.3) appended its payload to the section being collected. PAT and PMT packets now go through a per-PID repeat guard first, like the elementary stream packets; a packet that signals a discontinuity is never taken for a repeat. The rest of storytold/Filmcraft#1438 is already in craft-media (#993). Co-Authored-By: Claude Sonnet 5.5 --- crates/media/src/mpegts/tests.rs | 17 +++++++++++++++++ crates/media/src/mpegts/ts.rs | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/crates/media/src/mpegts/tests.rs b/crates/media/src/mpegts/tests.rs index 0215680ad..b9cb1af3b 100644 --- a/crates/media/src/mpegts/tests.rs +++ b/crates/media/src/mpegts/tests.rs @@ -791,6 +791,23 @@ fn scrambled_packet_is_skipped_with_a_warning() { assert!(total(&f) < total(&base)); } +#[test] +fn repeated_pat_and_pmt_packets_are_dropped_before_the_section_parser() { + let clean = audio_ts(2); + let first = |pid: u16| { + (0..clean.len() / 188).map(|i| i * 188).find(|&o| (((clean[o + 1] & 0x1F) as u16) << 8 | clean[o + 2] as u16) == pid).expect("packet exists") + }; + let mut dup = clean.clone(); + for pid in [0x100, 0] { + let o = first(pid); + let copy = dup[o..o + 188].to_vec(); + dup.splice(o + 188..o + 188, copy); + } + let f = open(&dup).unwrap(); + assert!(f.warnings.iter().any(|w| w.contains("2 duplicate packet(s) dropped")), "{:?}", f.warnings); + assert!(!f.warnings.iter().any(|w| w.contains("continuity") || w.contains("CRC")), "{:?}", f.warnings); +} + #[test] fn duplicate_packet_is_dropped_but_a_different_payload_with_the_same_counter_is_kept() { let clean = audio_ts(6); diff --git a/crates/media/src/mpegts/ts.rs b/crates/media/src/mpegts/ts.rs index a4c01a986..1d28bb736 100644 --- a/crates/media/src/mpegts/ts.rs +++ b/crates/media/src/mpegts/ts.rs @@ -497,6 +497,7 @@ pub(crate) fn scan(src: &S, packet_size: usize, sync_off let mut warnings: Vec = Vec::new(); let mut pat = Psi::default(); let mut pmt = PmtState::default(); + let mut psi_dup: HashMap = HashMap::new(); let mut es: HashMap = HashMap::new(); let mut order: Vec = Vec::new(); // one timestamp unwrapper for the program: its streams share one clock, so a stream whose @@ -560,6 +561,13 @@ pub(crate) fn scan(src: &S, packet_size: usize, sync_off } continue; } + if pk.pid == 0 || Some(pk.pid) == pmt.pmt_pid { + // a repeated packet would append its payload to the section in progress + if psi_dup.entry(pk.pid).or_default().is_dup(&pk) { + duplicates += 1; + continue; + } + } if pk.pid == 0 { feed_pat(&pk, &mut pat, &mut pmt, &mut crc_errors); continue; From 81bf3feb39a3304a9d9bb30025ad208ffd158602 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 12 Nov 2026 22:09:46 +0100 Subject: [PATCH 002/150] mxf: CinemaDNG in MXF (frame-wrapped DNG pictures), read and write The demuxer identifies CinemaDNG from the CinemaDNG container labels, the registered MXF-GC TIFF/EP container and picture coding labels, a 42h picture element type, or a TIFF header with the DNGVersion tag at the start of the essence. The writer produces OP1a files with frame-wrapped DNG pictures, the provisional container label, element type 42h and an RGBA descriptor (no coding label); OP-Atom is refused because it is clip-wrapped here. Same change as the crates/mxf part of storytold/Filmcraft#1078, ported onto craft-media. Co-Authored-By: Claude Sonnet 5.5 --- crates/media/README.md | 11 +++- crates/media/src/mxf/essence.rs | 81 ++++++++++++++++++++++++++-- crates/media/src/mxf/file.rs | 8 ++- crates/media/src/mxf/mod.rs | 5 +- crates/media/src/mxf/write.rs | 54 ++++++++++++++++--- crates/media/src/mxf/write/labels.rs | 4 ++ crates/media/src/mxf/write/tests.rs | 50 +++++++++++++++++ 7 files changed, 198 insertions(+), 15 deletions(-) diff --git a/crates/media/README.md b/crates/media/README.md index 387ab8cdd..48c930d1b 100644 --- a/crates/media/README.md +++ b/crates/media/README.md @@ -614,6 +614,7 @@ Implemented from the SMPTE documents (editions used): | SMPTE ST 386 | 2004 | D-10 (IMX) mapping: container label, sound element type | | SMPTE ST 2019-4 | 2009 | VC-3 (DNxHD / DNxHR) mapping | | SMPTE RDD 44 | 2017 | Apple ProRes mapping | +| Adobe CinemaDNG Image Data Format Specification | 1.0.0.0 (September 2009), §7 | CinemaDNG in MXF: frame-wrapped DNG picture elements (element type 42h), the essence container labels for CFA / LinearRaw content, RGBA descriptor; the labels are the specification's **provisional** values from Adobe's experimental range (byte 9 = 0Fh), not SMPTE-registered | | SMPTE RP 224 / RP 210 | registers | labels: data definitions, picture coding, essence containers, operational patterns; property labels of the primer pack (writer) | | SMPTE ST 330 | 2011 | basic UMIDs of the written packages | @@ -655,7 +656,10 @@ let pcm = mxf.read_pcm(&file, a, 48_000, 1920)?; // planar f32, sample-exact orders by the bitstream (the AVC picture order counts). - **Codecs.** From the picture essence coding label, then the essence container label, then the essence bytes: AVC / AVC-Intra, VC-3, ProRes (profile from the label), MPEG-2, - MPEG-4 visual, JPEG 2000, DV, uncompressed. + MPEG-4 visual, JPEG 2000, DV, uncompressed, CinemaDNG (the CinemaDNG container labels, the + registered MXF-GC TIFF/EP container and TIFF/EP picture coding labels, a 42h picture element + type, or a TIFF header with the DNGVersion tag at the start of the essence; the DNG pictures + themselves are developed by `craft-raw`). - **Sound.** PCM chunks (frame- or clip-wrapped) with sample counts; `read_pcm` decodes little-endian 8/16/24/32-bit PCM (wave / AES3 descriptors) and ST 331 AES3 elements (D-10). - **Timecode.** The material package timecode component (start frame count, rounded base, @@ -728,7 +732,10 @@ w.finish()?; element types 0x0C / 0x0D), ProRes (RDD 44: coding label profile byte, container `02 1C`), AVC byte stream (ST 381-3: container `02 10 60`, coding label from `profile_idc`; Annex B access units with in-band SPS / PPS — the AVC sub-descriptor is not written), Broadcast Wave PCM - (ST 382: container `02 06 01/02`, element types 0x01 / 0x02). + (ST 382: container `02 06 01/02`, element types 0x01 / 0x02), CinemaDNG (frame-wrapped DNG + datasets, element type 0x42, the provisional container label `0F 0A 0D 0B 02 7F 01/02 00`, an + RGBA descriptor with pixel layout and reference levels of the decoded 16-bit RGB picture and no + coding label; OP-Atom is refused because it is clip-wrapped here and DNG datasets cannot be). - `write_opatom_pcm(&OpAtomPcm, &[i32])` builds a complete in-memory OP-Atom PCM file (AAF media consolidation); `encode_pcm` interleaves planar f32 as little-endian PCM. diff --git a/crates/media/src/mxf/essence.rs b/crates/media/src/mxf/essence.rs index 27f893dc5..2c0d1ef7e 100644 --- a/crates/media/src/mxf/essence.rs +++ b/crates/media/src/mxf/essence.rs @@ -27,6 +27,9 @@ pub enum Codec { Dv, /// Uncompressed pictures (ST 384). Uncompressed, + /// CinemaDNG: one DNG (TIFF) dataset per frame-wrapped picture element (CinemaDNG Image Data + /// Format Specification §7), or TIFF/EP pictures under the registered MXF-GC TIFF/EP labels. + CinemaDng, /// Linear PCM, little-endian (Broadcast Wave and AES3 descriptors, ST 382). Pcm, /// 8-channel AES3 element data (ST 331, D-10 sound). @@ -46,13 +49,14 @@ impl Codec { Codec::Jpeg2000 => "JPEG 2000", Codec::Dv => "DV", Codec::Uncompressed => "Uncompressed", + Codec::CinemaDng => "CinemaDNG", Codec::Pcm => "PCM", Codec::Aes3Element => "AES3 PCM", Codec::Unknown => "unknown", } } /// The crate-wide identifier: VC-3 is [`CodecId::Dnxhd`], PCM and AES3 element data are - /// [`CodecId::Pcm`]; JPEG 2000, DV and uncompressed pictures have no identifier. + /// [`CodecId::Pcm`]; JPEG 2000, DV, uncompressed and CinemaDNG pictures have no identifier. /// /// [`CodecId::Dnxhd`]: crate::CodecId::Dnxhd /// [`CodecId::Pcm`]: crate::CodecId::Pcm @@ -65,15 +69,35 @@ impl Codec { Codec::Mpeg2 => Id::Mpeg2Video, Codec::Mpeg4Visual => Id::Mpeg4Video, Codec::Pcm | Codec::Aes3Element => Id::Pcm, - Codec::Jpeg2000 | Codec::Dv | Codec::Uncompressed | Codec::Unknown => Id::Unknown, + Codec::Jpeg2000 | Codec::Dv | Codec::Uncompressed | Codec::CinemaDng | Codec::Unknown => Id::Unknown, } } /// Every picture is coded independently. pub fn intra_only(&self) -> bool { - matches!(self, Codec::Avc { intra: true } | Codec::Vc3 | Codec::ProRes { .. } | Codec::Jpeg2000 | Codec::Dv | Codec::Uncompressed) + matches!( + self, + Codec::Avc { intra: true } | Codec::Vc3 | Codec::ProRes { .. } | Codec::Jpeg2000 | Codec::Dv | Codec::Uncompressed | Codec::CinemaDng + ) } } +/// The CinemaDNG essence container labels (CinemaDNG Image Data Format Specification 1.0.0.0, +/// §7.2.1): provisional, unregistered labels from Adobe's experimental range (byte 9 = 0F h), +/// `…0F 0A 0D 0B 02 7F cc 00` with content kind `cc` 01 (CFA) or 02 (LinearRaw). +pub const CINEMADNG_CONTAINER_PREFIX: [u8; 6] = [0x0F, 0x0A, 0x0D, 0x0B, 0x02, 0x7F]; + +/// Element type (byte 15 of the picture element key) of a frame-wrapped CinemaDNG picture: the +/// "TIFF" type of ST 331 (CinemaDNG specification §7.1.1.2). +pub const CINEMADNG_ELEMENT_TYPE: u8 = 0x42; + +/// Whether `ul` is a CinemaDNG essence container label (CFA or LinearRaw content), or the +/// registered MXF-GC TIFF/EP container (`…0D 01 03 01 02 14 ww 00`, RP 224). +pub fn is_cinemadng_container(ul: &Ul) -> bool { + ul.is_smpte() + && (ul.item_starts_with(&CINEMADNG_CONTAINER_PREFIX) && matches!(ul.0[14], 0x01 | 0x02) + || ul.item_starts_with(&[0x0D, 0x01, 0x03, 0x01, 0x02, 0x14])) +} + /// Identify a picture codec from the picture essence coding label and the essence container label. pub fn identify_picture(coding: Option
    , container: Option
      , mpeg_descriptor: bool) -> Codec { if let Some(c) = coding.filter(Ul::is_smpte) { @@ -81,6 +105,9 @@ pub fn identify_picture(coding: Option
        , container: Option
          , mpeg_descript // 04 01 02 02 .. : compressed picture coding if b[8..12] == [0x04, 0x01, 0x02, 0x02] { match (b[12], b[13]) { + // TIFF/EP Profile 2 picture coding (RP 224: 03 02 01 01 uncompressed CFA … 04 + // compressed LinearRaw), the coding of DNG pictures + (0x03, 0x02) if b[14] == 0x01 => return Codec::CinemaDng, // 01: MPEG compression (ISO/IEC 13818-2 profiles, 14496-2, 14496-10) (0x01, 0x01..=0x1F) => return Codec::Mpeg2, (0x01, 0x20..=0x2F) => return Codec::Mpeg4Visual, @@ -99,6 +126,9 @@ pub fn identify_picture(coding: Option
            , container: Option
              , mpeg_descript return Codec::Uncompressed; } } + if container.is_some_and(|c| is_cinemadng_container(&c)) { + return Codec::CinemaDng; + } if let Some(c) = container.filter(Ul::is_smpte) && c.item_starts_with(&[0x0D, 0x01, 0x03, 0x01, 0x02]) { @@ -117,8 +147,30 @@ pub fn identify_picture(coding: Option
                , container: Option
                  , mpeg_descript if mpeg_descriptor { Codec::Mpeg2 } else { Codec::Unknown } } +/// DNG evidence in the leading bytes of a TIFF: a TIFF 6.0 §2 header whose first IFD, when it lies +/// within `b`, holds the DNGVersion tag (DNG 1.x §2, tag 50706). A plain TIFF has no such tag. +fn is_dng(b: &[u8]) -> bool { + let le = match b.get(..4) { + Some(h) if h == b"II*\0" => true, + Some(h) if h == b"MM\0*" => false, + _ => return false, + }; + let u16_at = |o: usize| b.get(o..o + 2).map(|v| if le { u16::from_le_bytes([v[0], v[1]]) } else { u16::from_be_bytes([v[0], v[1]]) }); + let Some(ifd) = b.get(4..8).map(|v| if le { u32::from_le_bytes([v[0], v[1], v[2], v[3]]) } else { u32::from_be_bytes([v[0], v[1], v[2], v[3]]) }) + else { + return false; + }; + let Some(count) = usize::try_from(ifd).ok().and_then(|o| u16_at(o).map(|n| (o, n))) else { + return false; + }; + (0..usize::from(count.1)).any(|i| u16_at(count.0 + 2 + i * 12) == Some(50706)) +} + /// Identify a picture codec from the first bytes of an essence element. pub fn sniff_picture(b: &[u8]) -> Option { + if is_dng(b) { + return Some(Codec::CinemaDng); + } if b.len() >= 8 && &b[4..8] == b"icpf" { return Some(Codec::ProRes { profile: None }); } @@ -367,6 +419,20 @@ mod tests { assert_eq!(identify_picture(None, None, false), Codec::Unknown); } + #[test] + fn identifies_cinemadng() { + // CinemaDNG specification §7.2.1: CFA and LinearRaw containers (any registry version byte) + let adobe = |kind: u8| Ul([0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x0F, 0x0A, 0x0D, 0x0B, 0x02, 0x7F, kind, 0x00]); + assert_eq!(identify_picture(None, Some(adobe(1)), false), Codec::CinemaDng); + assert_eq!(identify_picture(None, Some(adobe(2)), false), Codec::CinemaDng); + assert_eq!(identify_picture(None, Some(adobe(3)), false), Codec::Unknown); + // RP 224: MXF-GC frame-wrapped TIFF/EP container, TIFF/EP picture coding + assert_eq!(identify_picture(None, Some(ul(&[0x0D, 1, 3, 1, 2, 0x14, 1])), false), Codec::CinemaDng); + assert_eq!(identify_picture(Some(ul(&[4, 1, 2, 2, 3, 2, 1, 3])), None, false), Codec::CinemaDng); + assert!(Codec::CinemaDng.intra_only()); + assert_eq!(Codec::CinemaDng.name(), "CinemaDNG"); + } + #[test] fn sniffs_essence() { assert_eq!(sniff_picture(&[0, 0, 0, 1, 0x09, 0x10]), Some(Codec::Avc { intra: false })); @@ -374,6 +440,15 @@ mod tests { assert_eq!(sniff_picture(&[0, 0, 2, 0x80, 1]), Some(Codec::Vc3)); assert_eq!(sniff_picture(&[0, 0, 0, 9, b'i', b'c', b'p', b'f']), Some(Codec::ProRes { profile: None })); assert_eq!(sniff_picture(&[1, 2, 3]), None); + // a TIFF header alone is not DNG; an IFD0 with the DNGVersion tag (50706 = 0xC612) is + let tiff_le = |tag: u16| [b"II*\0\x08\0\0\0\x01\0".as_slice(), &tag.to_le_bytes(), &[1, 0, 4, 0, 0, 0, 1, 4, 0, 0], &[0; 8]].concat(); + let tiff_be = |tag: u16| [b"MM\0*\0\0\0\x08\0\x01".as_slice(), &tag.to_be_bytes(), &[0, 1, 0, 0, 0, 4, 1, 4, 0, 0], &[0; 8]].concat(); + assert_eq!(sniff_picture(&tiff_le(0xC612)), Some(Codec::CinemaDng)); + assert_eq!(sniff_picture(&tiff_be(0xC612)), Some(Codec::CinemaDng)); + assert_eq!(sniff_picture(&tiff_le(0x0100)), None); + assert_eq!(sniff_picture(&tiff_be(0x0100)), None); + assert_eq!(sniff_picture(b"II*\0\x08\0\0\0"), None); + assert_eq!(sniff_picture(b"II*\0"), None); } #[test] diff --git a/crates/media/src/mxf/file.rs b/crates/media/src/mxf/file.rs index 15d847c7d..9064bcb83 100644 --- a/crates/media/src/mxf/file.rs +++ b/crates/media/src/mxf/file.rs @@ -910,7 +910,7 @@ fn build_track( } // A wrong or missing label: trust the essence bytes. if let Some(first) = elements.first() { - let head = read_upto(src, first.value_offset, 64)?; + let head = read_upto(src, first.value_offset, 4096)?; if let Some(sniffed) = sniff_picture(&head) { let agree = std::mem::discriminant(&sniffed) == std::mem::discriminant(&codec); if !agree && !matches!(codec, Codec::Avc { intra: true }) { @@ -918,6 +918,12 @@ fn build_track( } } } + // CinemaDNG §7.1.1.2: a "TIFF" picture element type names DNG pictures + if codec == Codec::Unknown + && elements.first().is_some_and(|e| e.key.0[12] == 0x15 && e.key.0[14] == super::essence::CINEMADNG_ELEMENT_TYPE) + { + codec = Codec::CinemaDng; + } t.codec = codec; t.picture = Some(pi); build_pictures(src, &mut t, elements, index, warnings)?; diff --git a/crates/media/src/mxf/mod.rs b/crates/media/src/mxf/mod.rs index ca0e8f918..56de637c7 100644 --- a/crates/media/src/mxf/mod.rs +++ b/crates/media/src/mxf/mod.rs @@ -2,7 +2,8 @@ //! ST 377-1 (file format: KLV, partitions, primer pack, header metadata, index tables, random //! index pack), ST 378 (OP1a), ST 390 (OP-Atom), ST 379-1/2 (generic container), ST 381-1 (MPEG //! video mapping), ST 381-3 (AVC mapping), ST 382 (AES3 / Broadcast Wave audio mapping), ST 331 -//! (AES3 element data, D-10 sound), ST 2019-4 (VC-3 mapping) and RDD 44 (ProRes mapping). +//! (AES3 element data, D-10 sound), ST 2019-4 (VC-3 mapping), RDD 44 (ProRes mapping) and the CinemaDNG +//! specification's MXF mapping (frame-wrapped DNG pictures). //! //! [`open`] walks the file's KLV packets (keys and lengths only; essence payloads are never read //! while opening), parses the partition packs, the primer pack and header metadata of the most @@ -32,7 +33,7 @@ mod meta; pub mod write; pub use crate::source::{ByteSource, ReadSeekSource}; -pub use essence::{Codec, PictureInfo, SoundFormat, SoundInfo, aes3_element_samples, decode_pcm}; +pub use essence::{CINEMADNG_CONTAINER_PREFIX, CINEMADNG_ELEMENT_TYPE, Codec, PictureInfo, SoundFormat, SoundInfo, aes3_element_samples, decode_pcm}; pub use file::{Chunk, EssenceTrack, MxfFile, OperationalPattern, Partition, PartitionKind, Sample, Timecode, TrackKind, Wrapping, open}; pub use index::{IndexEntry, IndexSegment}; pub use klv::{Rational, Ul}; diff --git a/crates/media/src/mxf/write.rs b/crates/media/src/mxf/write.rs index 9fb6bfea2..42abf846f 100644 --- a/crates/media/src/mxf/write.rs +++ b/crates/media/src/mxf/write.rs @@ -107,6 +107,10 @@ pub enum PictureCoding { ProRes { profile: u8 }, /// H.264 / AVC Annex B byte stream; `profile_idc` from the SPS (66, 77, 100, …). Avc { profile_idc: u8, intra: bool }, + /// CinemaDNG: one DNG dataset per picture (CinemaDNG specification §7), frame-wrapped only, + /// with an RGBA descriptor of the decoded 16-bit RGB image. `linear_raw`: the pictures are + /// LinearRaw (PhotometricInterpretation 34892) rather than CFA (32803). + CinemaDng { linear_raw: bool }, } impl PictureCoding { @@ -116,9 +120,10 @@ impl PictureCoding { _ => true, } } - /// Picture essence coding label. - fn coding_label(&self) -> [u8; 16] { - match *self { + /// Picture essence coding label (None: CinemaDNG, which has no registered one, §7.2.2.2). + fn coding_label(&self) -> Option<[u8; 16]> { + Some(match *self { + PictureCoding::CinemaDng { .. } => return None, PictureCoding::Vc3 { cid } => { let code = if (1235..=1234 + 0x7F).contains(&cid) { (cid - 1234) as u8 } else { 0 }; [0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x0A, 0x04, 0x01, 0x02, 0x02, 0x71, code, 0x00, 0x00] @@ -145,7 +150,7 @@ impl PictureCoding { }; [0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x0A, 0x04, 0x01, 0x02, 0x02, 0x01, b13, b14, 0x01] } - } + }) } /// (essence container label, element type) for frame or clip wrapping. fn container(&self, clip: bool) -> ([u8; 16], u8) { @@ -154,6 +159,12 @@ impl PictureCoding { PictureCoding::Vc3 { .. } => (essence_container(0x0A, 0x11, w, 0x00), if clip { 0x0D } else { 0x0C }), PictureCoding::ProRes { .. } => (essence_container(0x0D, 0x1C, w, 0x00), if clip { 0x18 } else { 0x17 }), PictureCoding::Avc { .. } => (essence_container(0x0A, 0x10, 0x60, w), if clip { 0x16 } else { 0x15 }), + // CinemaDNG §7.2.1 (provisional label) and §7.1.1.2 (element type 42h); always frame + // wrapped (`MxfWriter::new` refuses clip wrapping) + PictureCoding::CinemaDng { linear_raw } => ( + [0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x0F, 0x0A, 0x0D, 0x0B, 0x02, 0x7F, if *linear_raw { 0x02 } else { 0x01 }, 0x00], + 0x42, + ), } } } @@ -202,6 +213,8 @@ impl PictureDesc { if matches!(cid, 1235 | 1241 | 1250 | 1256 | 1270 | 1271) { 10 } else { 8 }, if matches!(cid, 1256 | 1270) { (1, 1) } else { (2, 1) }, ), + // §7.2.2.3: the decoded image is described as 16 bits per component + PictureCoding::CinemaDng { .. } => (16, (1, 1)), }; PictureDesc { coding, @@ -589,6 +602,19 @@ impl MxfWriter { return Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid PCM sound description")); } let clip = cfg.pattern == Pattern::OpAtom; + if clip && matches!(cfg.picture.as_ref().map(|p| p.coding), Some(PictureCoding::CinemaDng { .. })) { + // CinemaDNG §7.1.1: a TIFF dataset has no length field, so DNG pictures can't be clip wrapped + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "CinemaDNG pictures are frame wrapped only (this writer's OP-Atom is clip wrapped)", + )); + } + if let Some(p) = &cfg.picture { + // CinemaDNG §7.2.2.3 fixes the decoded image at 16 bits per component + if matches!(p.coding, PictureCoding::CinemaDng { .. }) && p.depth != 16 { + return Err(io::Error::new(io::ErrorKind::InvalidInput, "CinemaDNG pictures are described at 16 bits per component")); + } + } let mut tracks = Vec::new(); let mut id = cfg.first_track_id.max(2); if let Some(p) = &cfg.picture { @@ -985,7 +1011,8 @@ impl MxfWriter { TrackKind::Picture => { // Picture tracks exist only with a picture configuration. let Some(p) = cfg.picture.as_ref() else { continue }; - let mut s = Set::new(0x28, uid); + let rgba = matches!(p.coding, PictureCoding::CinemaDng { .. }); + let mut s = Set::new(if rgba { 0x29 } else { 0x28 }, uid); if multiple { s.u32(0x3006, t.id); } @@ -1020,8 +1047,21 @@ impl MxfWriter { // field dominance: the temporally first field (1 top, 2 bottom) s.u8(0x3212, if p.bottom_field_first { 2 } else { 1 }); } - s.p(0x3201, &p.coding.coding_label()) - .p(0x3210, &tcl) + if let Some(label) = p.coding.coding_label() { + s.p(0x3201, &label); + } + if rgba { + // RGBA descriptor (ST 377-1 G.2.40): pixel layout R, G, B of 16 bits each and + // the component reference levels. CinemaDNG §7.2.2.3 fixes the decoded + // image at 16 bits; `MxfWriter::new` rejects any other `PictureDesc::depth`. + let mut layout = [0u8; 16]; + layout[..6].copy_from_slice(&[b'R', 16, b'G', 16, b'B', 16]); + let max = u32::from(u16::MAX); + s.p(0x3401, &layout).u32(0x3406, max).u32(0x3407, 0); + s.write(&mut out); + continue; + } + s.p(0x3210, &tcl) .p(0x321A, &ce) .p(0x3219, &cp) .u32(0x3301, depth) diff --git a/crates/media/src/mxf/write/labels.rs b/crates/media/src/mxf/write/labels.rs index eb8fbc040..12f62c7fd 100644 --- a/crates/media/src/mxf/write/labels.rs +++ b/crates/media/src/mxf/write/labels.rs @@ -120,6 +120,10 @@ pub const PRIMER: &[(u16, [u8; 16])] = &[ (0x3304, ul(&[0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x05, 0x03, 0x03])), (0x3305, ul(&[0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x05, 0x03, 0x04])), (0x3306, ul(&[0x01, 0x01, 0x01, 0x02, 0x04, 0x01, 0x05, 0x03, 0x05])), + // RGBA picture essence descriptor (ST 377-1; RP 210): PixelLayout, ComponentMaxRef, ComponentMinRef + (0x3401, ul(&[0x01, 0x01, 0x01, 0x02, 0x04, 0x01, 0x05, 0x03, 0x06])), + (0x3406, ul(&[0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x05, 0x03, 0x0B])), + (0x3407, ul(&[0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x05, 0x03, 0x0C])), (0x3D03, ul(&[0x01, 0x01, 0x01, 0x05, 0x04, 0x02, 0x03, 0x01, 0x01, 0x01])), (0x3D02, ul(&[0x01, 0x01, 0x01, 0x04, 0x04, 0x02, 0x03, 0x01, 0x04])), (0x3D07, ul(&[0x01, 0x01, 0x01, 0x05, 0x04, 0x02, 0x01, 0x01, 0x04])), diff --git a/crates/media/src/mxf/write/tests.rs b/crates/media/src/mxf/write/tests.rs index 91574fec7..eac50a0e9 100644 --- a/crates/media/src/mxf/write/tests.rs +++ b/crates/media/src/mxf/write/tests.rs @@ -401,6 +401,56 @@ fn long_pcm_reads_backed_by_the_file_are_not_capped() { assert!(m.read_pcm(&f, 0, 0, frames + (1 << 24)).is_err()); } +/// A TIFF-headed stand-in for a DNG dataset (the demuxer only carries the bytes). +fn dng_like(i: usize) -> Vec { + let mut v = b"II*\0\x08\0\0\0".to_vec(); + v.extend(std::iter::repeat_n(i as u8 ^ 0x33, 300 + 11 * i)); + v +} + +#[test] +fn cinemadng_op1a_reads_back() { + for linear_raw in [false, true] { + let mut cfg = op1a_config(Rational::new(24, 1), PictureCoding::CinemaDng { linear_raw }); + cfg.sound.truncate(1); + let mut w = MxfWriter::new(Cursor::new(Vec::new()), cfg).unwrap(); + for i in 0..6 { + w.push_picture(&dng_like(i), FrameInfo::intra()).unwrap(); + w.push_sound(0, &pcm16(i * 2000, 2000)).unwrap(); + } + let f = w.finish().unwrap().into_inner(); + // CinemaDNG §7.1.1.2: picture element key 06.0E.2B.34.01.02.01.01.0D.01.03.01.15.01.42.01 + let key = [0x06, 0x0E, 0x2B, 0x34, 0x01, 0x02, 0x01, 0x01, 0x0D, 0x01, 0x03, 0x01, 0x15, 0x01, 0x42, 0x01]; + assert_eq!(f.windows(16).filter(|k| *k == key).count(), 6); + // §7.2.1: the essence container label of the content kind + let ec = [0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x0F, 0x0A, 0x0D, 0x0B, 0x02, 0x7F, if linear_raw { 2 } else { 1 }, 0x00]; + assert!(f.windows(16).any(|k| k == ec)); + let m = open(&f).unwrap(); + assert!(m.warnings.is_empty(), "{:?}", m.warnings); + let v = m.track_of_kind(TrackKind::Picture).unwrap(); + let t = &m.tracks[v]; + assert_eq!(t.codec, Codec::CinemaDng); + assert_eq!(t.wrapping, Wrapping::Frame); + assert_eq!(t.samples.len(), 6); + let p = t.picture.as_ref().unwrap(); + assert!(p.rgba, "RGBA picture essence descriptor (§7.2.2)"); + assert_eq!((p.frame_width(), p.frame_height()), (64, 36)); + assert!(p.picture_coding.is_none()); + for i in 0..6 { + assert!(t.samples[i].key); + assert_eq!(m.read_sample(&f, v, i).unwrap(), dng_like(i)); + } + assert_eq!(m.timecode.unwrap().format(), "01:02:30:00"); + } +} + +#[test] +fn cinemadng_is_never_clip_wrapped() { + let mut cfg = WriterConfig::new(Pattern::OpAtom, Rational::new(24, 1), PackageIds::from_seed("t", "T")); + cfg.picture = Some(PictureDesc::new(PictureCoding::CinemaDng { linear_raw: false }, 64, 36)); + assert!(MxfWriter::new(Cursor::new(Vec::new()), cfg).is_err()); +} + /// One `push_sound` of a long clip must not cost more than the clip itself: draining the buffered /// PCM from the front for every edit unit made this quadratic (about 16 s for 10 minutes). #[test] From 756def2bc6211909afcd894fc6c10c55c695d42c Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 12 Nov 2026 23:18:51 +0100 Subject: [PATCH 003/150] mxf: checked end offset for the TIFF u16 read in DNG sniffing Co-Authored-By: Claude Sonnet 5.5 --- crates/media/src/mxf/essence.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/crates/media/src/mxf/essence.rs b/crates/media/src/mxf/essence.rs index 2c0d1ef7e..74818e0e8 100644 --- a/crates/media/src/mxf/essence.rs +++ b/crates/media/src/mxf/essence.rs @@ -147,6 +147,14 @@ pub fn identify_picture(coding: Option
                    , container: Option
                      , mpeg_descript if mpeg_descriptor { Codec::Mpeg2 } else { Codec::Unknown } } +/// Read a TIFF `u16` at offset `o`. The end offset is computed with checked arithmetic, so an +/// untrusted offset near `usize::MAX` (a 32-bit target with IFD offset `0xFFFF_FFFF`) is +/// out of bounds instead of overflowing. +fn tiff_u16(b: &[u8], le: bool, o: usize) -> Option { + let v = b.get(o..o.checked_add(2)?)?; + Some(if le { u16::from_le_bytes([v[0], v[1]]) } else { u16::from_be_bytes([v[0], v[1]]) }) +} + /// DNG evidence in the leading bytes of a TIFF: a TIFF 6.0 §2 header whose first IFD, when it lies /// within `b`, holds the DNGVersion tag (DNG 1.x §2, tag 50706). A plain TIFF has no such tag. fn is_dng(b: &[u8]) -> bool { @@ -155,7 +163,7 @@ fn is_dng(b: &[u8]) -> bool { Some(h) if h == b"MM\0*" => false, _ => return false, }; - let u16_at = |o: usize| b.get(o..o + 2).map(|v| if le { u16::from_le_bytes([v[0], v[1]]) } else { u16::from_be_bytes([v[0], v[1]]) }); + let u16_at = |o: usize| tiff_u16(b, le, o); let Some(ifd) = b.get(4..8).map(|v| if le { u32::from_le_bytes([v[0], v[1], v[2], v[3]]) } else { u32::from_be_bytes([v[0], v[1], v[2], v[3]]) }) else { return false; @@ -419,6 +427,15 @@ mod tests { assert_eq!(identify_picture(None, None, false), Codec::Unknown); } + #[test] + fn tiff_offset_near_usize_max_is_out_of_bounds() { + let b = [0x49, 0x49, 0x2A, 0x00, 0xFF, 0xFF, 0xFF, 0xFF]; + assert_eq!(tiff_u16(&b, true, usize::MAX), None); + assert_eq!(tiff_u16(&b, true, usize::MAX - 1), None); + assert_eq!(tiff_u16(&b, true, 0), Some(0x4949)); + assert!(!is_dng(&b)); + } + #[test] fn identifies_cinemadng() { // CinemaDNG specification §7.2.1: CFA and LinearRaw containers (any registry version byte) From 25f1ac5801dcf687990bdaf1ca2aee0d91934cf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Legrand?= Date: Sat, 14 Nov 2026 10:39:34 +0100 Subject: [PATCH 004/150] test(prc): validate compressed mesh reconstruction --- crates/prc/README.md | 4 +- crates/prc/src/tess/compressed.rs | 2 + crates/prc/src/tess/compressed/independent.rs | 451 ++++++++++++++++++ 3 files changed, 455 insertions(+), 2 deletions(-) create mode 100644 crates/prc/src/tess/compressed/independent.rs diff --git a/crates/prc/README.md b/crates/prc/README.md index 4d40f0962..c76d0af5b 100644 --- a/crates/prc/README.md +++ b/crates/prc/README.md @@ -30,7 +30,7 @@ BitReader: bit, bits, character, unsigned_integer, integer, integer_vbn, double_ - `Tessellation::Mesh` is a `TriangleMesh`: positions, triangles, one triangle range per face, the stored normals and texture coordinates with each triangle's corner indices into them, and per-triangle line-attribute styles. `Wire` is polylines; `Markup` is read past. -- `Tessellation::Compressed` is read to its end so later entities decode; its triangles are not rebuilt yet. +- `Tessellation::Compressed` is read to its end so later entities decode. Recalculated-normal meshes in the accepted 3T form are rebuilt when their arrays fit; stored-normal meshes and unsupported traversals stay `Compressed`. - A face may carry at most 8 texture-coordinate sets; more is `Malformed`. Optimised vertex colours are `Unsupported`. - The product tree: each drawable tessellation placed by its occurrence path's locations and the item's coordinate systems (a 4x4 `Matrix`), coloured by the style inheritance rules, with occurrences' entity references applied @@ -39,6 +39,6 @@ BitReader: bit, bits, character, unsigned_integer, integer, integer_vbn, double_ came from, its visibility and the range of placements it drew. - Occurrence and prototype cycles are `Malformed`; the walk is capped at depth 64, 2^20 visits and `MAX_PLACEMENTS`. - A tree's markups (PMI), views, scene lights and clipping planes are read past, not modelled; so are textures. -- Not yet in the crate: rebuilding compressed meshes, textures. +- Not yet in the crate: stored-normal or one-status compressed mesh reconstruction, textures. - The `Double` code table's rows come from [prc-rs](https://github.com/ralovich/prc-rs) (MIT); its notice is in `src/acof/table.rs`. A few reading choices also follow prc-rs; each is marked `// prc-rs:` in the source. diff --git a/crates/prc/src/tess/compressed.rs b/crates/prc/src/tess/compressed.rs index 53e082b4d..f6fa70317 100644 --- a/crates/prc/src/tess/compressed.rs +++ b/crates/prc/src/tess/compressed.rs @@ -349,5 +349,7 @@ impl Walk { } } +#[cfg(test)] +mod independent; #[cfg(test)] mod tests; diff --git a/crates/prc/src/tess/compressed/independent.rs b/crates/prc/src/tess/compressed/independent.rs new file mode 100644 index 000000000..209337f78 --- /dev/null +++ b/crates/prc/src/tess/compressed/independent.rs @@ -0,0 +1,451 @@ +#![allow( + clippy::cast_possible_truncation, + clippy::cast_precision_loss, + clippy::expect_used, + clippy::float_cmp, + clippy::many_single_char_names, + clippy::panic, + clippy::unwrap_used +)] + +use super::*; + +const S: f64 = std::f64::consts::SQRT_2; + +#[derive(Clone, Copy)] +enum Slot { + Point(V), + #[allow(dead_code)] + Reference(u32), +} + +#[derive(Clone, Copy)] +enum Recipe { + Seed { slots: [Slot; 3], status: i32, face: u32 }, + Apex { edge: (u32, u32), opposite: u32, slot: Slot, status: i32, face: u32 }, +} + +impl Recipe { + fn status(self) -> i32 { + match self { + Self::Seed { status, .. } | Self::Apex { status, .. } => status, + } + } + + fn face(self) -> u32 { + match self { + Self::Seed { face, .. } | Self::Apex { face, .. } => face, + } + } +} + +#[derive(Clone)] +struct Encoded { + tolerance: f64, + origin: V, + points: Vec, + edge_status: Vec, + face_of: Vec, + is_reference: Vec, + references: Vec, + positions: Vec, + triangles: Vec<[u32; 3]>, +} + +impl Encoded { + fn arrays(&self, limit: usize) -> Arrays<'_> { + Arrays { + tolerance: self.tolerance, + origin: self.origin, + points: &self.points, + edge_status: &self.edge_status, + triangles: self.triangles.len(), + is_reference: &self.is_reference, + references: &self.references, + limit, + } + } + + #[allow(dead_code)] + fn faces(&self) -> usize { + self.face_of.iter().max().map_or(0, |&face| face as usize + 1) + } + + fn push_reference(&mut self, vertex: u32) -> u32 { + let index = usize::try_from(vertex).unwrap(); + assert!(index < self.positions.len(), "a recipe reference must name an earlier vertex"); + self.is_reference.push(true); + self.references.push(vertex); + vertex + } + + fn push_position(&mut self, position: V, point: [i64; 3]) -> u32 { + self.points.extend(point); + self.is_reference.push(false); + self.positions.push(position); + u32::try_from(self.positions.len() - 1).unwrap() + } + + fn push_seed_point(&mut self, index: usize, target: V, triangle: [u32; 3]) -> u32 { + let base = match index { + 0 => self.origin, + 1 => self.positions[usize::try_from(triangle[0]).unwrap()], + 2 => { + let first = self.positions[usize::try_from(triangle[0]).unwrap()]; + let second = self.positions[usize::try_from(triangle[1]).unwrap()]; + p_scale(p_add(first, second), 0.5) + } + _ => panic!("a seed has exactly three slots"), + }; + let point = quantize(p_sub(target, base), self.tolerance); + let position = p_add(base, scaled(point, self.tolerance)); + self.push_position(position, point) + } + + fn push_apex_point(&mut self, edge: (u32, u32), opposite: u32, target: V) -> u32 { + let (p, q) = edge; + let (lo, hi) = if p < q { (p, q) } else { (q, p) }; + let lo = self.positions[usize::try_from(lo).unwrap()]; + let hi = self.positions[usize::try_from(hi).unwrap()]; + let opposite = self.positions[usize::try_from(opposite).unwrap()]; + let (origin, x, y, z) = encoder_frame(lo, hi, opposite); + let offset = p_sub(target, origin); + let point = [ + quantize_component(p_dot(offset, x), self.tolerance), + quantize_component(-p_dot(offset, y), self.tolerance), + quantize_component(-p_dot(offset, z), self.tolerance), + ]; + let delta = scaled(point, self.tolerance); + let position = p_sub(p_sub(p_add(origin, p_scale(x, delta[0])), p_scale(y, delta[1])), p_scale(z, delta[2])); + self.push_position(position, point) + } +} + +fn encode(origin: V, tolerance: f64, recipes: &[Recipe]) -> Encoded { + assert!(tolerance.is_finite() && tolerance > 0.0, "a positive finite tolerance"); + let mut edge_status: Vec = recipes.iter().map(|recipe| recipe.status()).collect(); + edge_status.resize(recipes.len().saturating_mul(3), 0); + let mut encoded = Encoded { + tolerance, + origin, + points: Vec::new(), + edge_status, + face_of: Vec::new(), + is_reference: Vec::new(), + references: Vec::new(), + positions: Vec::new(), + triangles: Vec::new(), + }; + + for &recipe in recipes { + encoded.face_of.push(recipe.face()); + match recipe { + Recipe::Seed { slots, .. } => { + let mut triangle = [0; 3]; + for (index, slot) in slots.into_iter().enumerate() { + triangle[index] = match slot { + Slot::Point(target) => encoded.push_seed_point(index, target, triangle), + Slot::Reference(vertex) => encoded.push_reference(vertex), + }; + } + encoded.triangles.push(triangle); + } + Recipe::Apex { edge: (p, q), opposite, slot, .. } => { + let vertex = match slot { + Slot::Point(target) => encoded.push_apex_point((p, q), opposite, target), + Slot::Reference(vertex) => encoded.push_reference(vertex), + }; + encoded.triangles.push([p, q, vertex]); + } + } + } + encoded +} + +fn p_add(a: V, b: V) -> V { + [a[0] + b[0], a[1] + b[1], a[2] + b[2]] +} + +fn p_sub(a: V, b: V) -> V { + [a[0] - b[0], a[1] - b[1], a[2] - b[2]] +} + +fn p_scale(a: V, factor: f64) -> V { + a.map(|component| component * factor) +} + +fn p_dot(a: V, b: V) -> f64 { + a[0] * b[0] + a[1] * b[1] + a[2] * b[2] +} + +fn p_cross(a: V, b: V) -> V { + [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]] +} + +fn quantize_component(value: f64, tolerance: f64) -> i64 { + let quantized = (value / tolerance).round(); + assert!(quantized.is_finite() && quantized >= i64::MIN as f64 && quantized <= i64::MAX as f64); + quantized as i64 +} + +fn quantize(value: V, tolerance: f64) -> [i64; 3] { + value.map(|component| quantize_component(component, tolerance)) +} + +fn scaled(point: [i64; 3], tolerance: f64) -> V { + point.map(|component| component as f64 * tolerance) +} + +fn encoder_length(value: V) -> Option { + let squared = (value[0] * value[0] + value[1] * value[1] + value[2] * value[2]) as f32; + let bits = squared.to_bits(); + let exponent = (bits >> 23) & 0xff; + let seed = if exponent > 127 { f32::from_bits((bits & !(0xff << 23)) | (((exponent - 127) / 2 + 127) << 23)) } else { squared }; + let squared = f64::from(squared); + let mut current = f64::from(seed); + for _ in 0..100 { + let next = 0.5 * (current + squared / current); + if next == current { + return Some(next); + } + current = next; + } + None +} + +fn encoder_unit(value: V) -> Option { + let length = encoder_length(value).filter(|&length| length >= f64::from(f32::EPSILON))?; + Some([value[0] / length, value[1] / length, value[2] / length]) +} + +fn encoder_ortho_z(x: V) -> Option { + let x = encoder_unit(x)?; + encoder_unit(p_cross(x, [0.0, 1.0, 0.0])).or_else(|| encoder_unit(p_cross(x, [1.0, 0.0, 0.0]))) +} + +fn encoder_frame(lo: V, hi: V, opposite: V) -> (V, V, V, V) { + let origin = p_scale(p_add(lo, hi), 0.5); + let difference = p_sub(lo, hi); + let x = encoder_unit(difference).unwrap_or(difference); + let residue = p_cross(p_sub(opposite, origin), x); + let (y, z) = encoder_unit(residue) + .and_then(|z| Some((encoder_unit(p_cross(z, x))?, z))) + .or_else(|| { + let z = encoder_ortho_z(x)?; + let y = encoder_unit(p_cross(z, x))?; + Some(if p_dot(residue, z) < 0.0 { (p_scale(y, -1.0), p_scale(z, -1.0)) } else { (y, z) }) + }) + .unwrap_or_default(); + (origin, x, y, z) +} + +fn assert_positions(actual: &[V], expected: &[V]) { + assert_eq!(actual.len(), expected.len(), "vertex counts differ"); + for (index, (actual, expected)) in actual.iter().zip(expected).enumerate() { + for component in 0..3 { + assert!( + (actual[component] - expected[component]).abs() < 1e-9, + "vertex {index}, component {component}: actual {actual:?}, expected {expected:?}", + ); + } + } +} + +#[allow(dead_code)] +fn assert_within_tolerance(actual: &[V], source: &[V], tolerance: f64) { + assert_eq!(actual.len(), source.len(), "vertex counts differ"); + let limit = tolerance * 3.0_f64.sqrt() / 2.0; + for (index, (actual, source)) in actual.iter().zip(source).enumerate() { + let difference = p_sub(*actual, *source); + let distance = p_dot(difference, difference).sqrt(); + assert!(distance <= limit + 1e-12, "vertex {index}: distance {distance} exceeds {limit}; actual {actual:?}, source {source:?}"); + } +} + +fn triangle_seed(x: f64, status: i32, face: u32) -> Recipe { + Recipe::Seed { slots: [Slot::Point([x, 0.0, 0.0]), Slot::Point([x + 2.0, 0.0, 0.0]), Slot::Point([x, 2.0, 0.0])], status, face } +} + +fn panel(target: V) -> Encoded { + encode( + [0.0; 3], + 0.5, + &[ + triangle_seed(0.0, 3, 0), + Recipe::Apex { edge: (2, 1), opposite: 0, slot: Slot::Point(target), status: 3, face: 0 }, + Recipe::Apex { edge: (2, 3), opposite: 1, slot: Slot::Reference(0), status: 2, face: 0 }, + Recipe::Apex { edge: (0, 3), opposite: 2, slot: Slot::Reference(1), status: 0, face: 0 }, + ], + ) +} + +#[test] +fn independent_seed_and_apex_match_a_hand_computed_stream() { + let encoded = encode( + [10.0, 0.0, 0.0], + 0.5, + &[ + Recipe::Seed { slots: [Slot::Point([10.0, 0.0, 0.0]), Slot::Point([12.0, 0.0, 0.0]), Slot::Point([10.0, 2.0, 0.0])], status: 2, face: 0 }, + Recipe::Apex { edge: (2, 1), opposite: 0, slot: Slot::Point([11.0 + 2.0 * S, 1.0 + S, -1.0]), status: 0, face: 0 }, + ], + ); + + assert_eq!(encoded.points, [0, 0, 0, 4, 0, 0, -2, 4, 0, 2, -6, 2]); + let rebuilt = rebuild(&encoded.arrays(usize::MAX)).unwrap(); + assert_eq!(rebuilt.triangles, [[0, 1, 2], [2, 1, 3]]); + let expected = [[10.0, 0.0, 0.0], [12.0, 0.0, 0.0], [10.0, 2.0, 0.0], [11.0 + 2.0 * S, 1.0 + S, -1.0]]; + assert_positions(&rebuilt.positions, &expected); +} + +#[test] +fn independent_components_and_a_reference_are_rebuilt() { + let encoded = encode( + [0.0; 3], + 0.5, + &[ + triangle_seed(0.0, 2, 0), + Recipe::Apex { edge: (2, 1), opposite: 0, slot: Slot::Reference(0), status: 0, face: 0 }, + triangle_seed(5.0, 0, 1), + ], + ); + + let rebuilt = rebuild(&encoded.arrays(usize::MAX)).unwrap(); + assert_eq!(rebuilt.triangles, [[0, 1, 2], [2, 1, 0], [3, 4, 5]]); + assert_eq!(encoded.references, [0]); + assert_eq!(encoded.face_of, [0, 0, 1]); + assert_eq!(rebuilt.positions.len(), 6); + let expected = [[0.0, 0.0, 0.0], [2.0, 0.0, 0.0], [0.0, 2.0, 0.0], [5.0, 0.0, 0.0], [7.0, 0.0, 0.0], [5.0, 2.0, 0.0]]; + assert_positions(&rebuilt.positions, &expected); +} + +#[test] +fn independent_fold_and_component_retry_rebuild_the_same_panel() { + let folded = [1.0 - 1.0 / S, 1.0 - S, 0.0]; + let unfolded = [1.0 + S, 1.0 + 1.0 / S, 0.0]; + for target in [folded, unfolded] { + let source = [[0.0, 0.0, 0.0], [2.0, 0.0, 0.0], [0.0, 2.0, 0.0], target]; + let encoded = panel(target); + assert_within_tolerance(&encoded.positions, &source, 0.5); + let rebuilt = rebuild(&encoded.arrays(usize::MAX)).unwrap(); + assert_eq!(rebuilt.triangles, [[0, 1, 2], [2, 1, 3], [2, 3, 0], [0, 3, 1]]); + assert_eq!(rebuilt.positions.len(), 4); + assert_within_tolerance(&rebuilt.positions, &source, 0.5); + } +} + +#[test] +fn independent_closed_continuation_uses_the_open_edge() { + let encoded = encode( + [0.0; 3], + 0.5, + &[ + triangle_seed(0.0, 2, 0), + Recipe::Apex { edge: (2, 1), opposite: 0, slot: Slot::Point([0.0, 0.0, 2.0]), status: 2, face: 0 }, + Recipe::Apex { edge: (3, 1), opposite: 2, slot: Slot::Reference(0), status: 2, face: 0 }, + Recipe::Apex { edge: (3, 0), opposite: 1, slot: Slot::Reference(2), status: 0, face: 0 }, + ], + ); + + let source = [[0.0, 0.0, 0.0], [2.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 2.0]]; + assert_within_tolerance(&encoded.positions, &source, 0.5); + let rebuilt = rebuild(&encoded.arrays(usize::MAX)).unwrap(); + assert_eq!(rebuilt.triangles, [[0, 1, 2], [2, 1, 3], [3, 1, 0], [3, 0, 2]]); + assert_within_tolerance(&rebuilt.positions, &source, 0.5); +} + +#[test] +fn independent_non_planar_strip_tracks_the_quantized_working_mesh() { + let source: Vec = (0..16).map(|index| [f64::from(index) * 0.05, f64::from(index % 2) * 2.0, (f64::from(index % 5) - 2.0) * 0.03]).collect(); + let mut recipes = vec![Recipe::Seed { slots: [Slot::Point(source[0]), Slot::Point(source[1]), Slot::Point(source[2])], status: 2, face: 0 }]; + let (mut p, mut q, mut opposite) = (2_u32, 1_u32, 0_u32); + + for index in 1..source.len() - 2 { + let vertex = u32::try_from(index + 2).unwrap(); + let status = if index % 2 == 1 { 1 } else { 2 }; + recipes.push(Recipe::Apex { edge: (p, q), opposite, slot: Slot::Point(source[index + 2]), status, face: 0 }); + if status == 1 { + (p, q, opposite) = (p, vertex, q); + } else { + (p, q, opposite) = (vertex, q, p); + } + } + + let encoded = encode([0.0; 3], 1e-4, &recipes); + assert_within_tolerance(&encoded.positions, &source, 1e-4); + let rebuilt = rebuild(&encoded.arrays(usize::MAX)).unwrap(); + assert_eq!( + rebuilt.triangles, + [ + [0, 1, 2], + [2, 1, 3], + [2, 3, 4], + [4, 3, 5], + [4, 5, 6], + [6, 5, 7], + [6, 7, 8], + [8, 7, 9], + [8, 9, 10], + [10, 9, 11], + [10, 11, 12], + [12, 11, 13], + [12, 13, 14], + [14, 13, 15], + ] + ); + assert_eq!(encoded.points.len(), source.len() * 3); + assert_within_tolerance(&rebuilt.positions, &source, 1e-4); +} + +#[test] +fn independent_near_degenerate_parent_uses_an_orthogonal_frame() { + let encoded = encode( + [0.0; 3], + 1e-8, + &[ + Recipe::Seed { + slots: [Slot::Point([1.0, 1.0 + 1e-8, 0.0]), Slot::Point([2.0, 0.0, 0.0]), Slot::Point([0.0, 2.0, 0.0])], + status: 2, + face: 0, + }, + Recipe::Apex { edge: (2, 1), opposite: 0, slot: Slot::Point([0.3, 0.7, 0.2]), status: 0, face: 0 }, + ], + ); + + let source = [[1.0, 1.0 + 1e-8, 0.0], [2.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.3, 0.7, 0.2]]; + assert_within_tolerance(&encoded.positions, &source, 1e-8); + let rebuilt = rebuild(&encoded.arrays(usize::MAX)).unwrap(); + assert_eq!(rebuilt.triangles, [[0, 1, 2], [2, 1, 3]]); + assert_within_tolerance(&rebuilt.positions, &source, 1e-8); +} + +#[test] +fn independent_rebuild_requires_exact_arrays_and_enough_budget() { + let encoded = encode([0.0; 3], 0.5, &[triangle_seed(0.0, 0, 0)]); + assert!(rebuild(&encoded.arrays(usize::MAX)).is_ok()); + + let mut extra_point = encoded.clone(); + extra_point.points.push(0); + assert_eq!(rebuild(&extra_point.arrays(usize::MAX)).unwrap_err(), "the decoded triangles do not use up the stored arrays",); + + let mut extra_slot = encoded.clone(); + extra_slot.is_reference.push(false); + assert_eq!(rebuild(&extra_slot.arrays(usize::MAX)).unwrap_err(), "the decoded triangles do not use up the stored arrays",); + + let mut extra_reference = encoded.clone(); + extra_reference.references.push(0); + assert_eq!(rebuild(&extra_reference.arrays(usize::MAX)).unwrap_err(), "the decoded triangles do not use up the stored arrays",); + + assert_eq!(rebuild(&encoded.arrays(0)).unwrap_err(), "the rebuild needs more than the decoded-element allowance left",); +} + +#[test] +fn independent_faces_group_walk_order_triangles() { + let encoded = encode([0.0; 3], 0.5, &[triangle_seed(0.0, 0, 1), triangle_seed(5.0, 0, 0)]); + let rebuilt = rebuild(&encoded.arrays(usize::MAX)).unwrap(); + let mesh = super::super::by_face(rebuilt, &encoded.face_of, encoded.faces(), &[]); + + assert_eq!(mesh.faces, [0..1, 1..2]); + assert_eq!(mesh.triangles, [[3, 4, 5], [0, 1, 2]]); + let expected = [[0.0, 0.0, 0.0], [2.0, 0.0, 0.0], [0.0, 2.0, 0.0], [5.0, 0.0, 0.0], [7.0, 0.0, 0.0], [5.0, 2.0, 0.0]]; + assert_positions(&mesh.positions, &expected); +} From a528fa7430314b3a0905a5cdc86be39631bf86f8 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 14 Nov 2026 11:23:04 +0100 Subject: [PATCH 005/150] rtf: keep right-to-left paragraph direction Read \rtlpar/\rtldir and \ltrpar/\ltrdir into the paragraph's bidi flag and write \rtlpar for right-to-left paragraphs. The flow paragraph carries the direction between the document and the RTF reader/writer. Co-Authored-By: Claude Sonnet 5.5 --- crates/flow/src/from_doc.rs | 3 +++ crates/flow/src/to_doc.rs | 3 +++ crates/flow/src/types.rs | 2 ++ crates/rtf/src/doc/export.rs | 4 ++++ crates/rtf/src/doc/state.rs | 3 +++ crates/rtf/src/doc/styles.rs | 1 + crates/rtf/src/doc/tests.rs | 30 ++++++++++++++++++++++++++++++ crates/rtf/src/doc/word.rs | 4 ++++ 8 files changed, 50 insertions(+) diff --git a/crates/flow/src/from_doc.rs b/crates/flow/src/from_doc.rs index 341ded449..ea7f6fc92 100644 --- a/crates/flow/src/from_doc.rs +++ b/crates/flow/src/from_doc.rs @@ -493,6 +493,8 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec { kind, list, align: p.props.align, + // Direction can come from the paragraph's style (`w:bidi` in the style's pPr). + rtl: doc.styles.resolve_para(&p.props).bidi, page_break: p.props.page_break_before.unwrap_or(false), keep_next: p.props.keep_next.unwrap_or(false), inlines: Vec::new(), @@ -536,6 +538,7 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec { kind, list: None, align: p.props.align, + rtl: out.rtl, page_break: true, keep_next: p.props.keep_next.unwrap_or(false), inlines: Vec::new(), diff --git a/crates/flow/src/to_doc.rs b/crates/flow/src/to_doc.rs index c10be85c2..ce29a4448 100644 --- a/crates/flow/src/to_doc.rs +++ b/crates/flow/src/to_doc.rs @@ -159,6 +159,9 @@ fn para_props(p: &Para, list_num: Option) -> ParaProps { out.numbering = Some(NumRef { num, level: li.level.min(8) }); } out.align = p.align; + if p.rtl { + out.bidi = Some(true); + } let sp = p.spacing; out.space_before = sp.before; out.space_after = sp.after; diff --git a/crates/flow/src/types.rs b/crates/flow/src/types.rs index e897778f9..f1c281c62 100644 --- a/crates/flow/src/types.rs +++ b/crates/flow/src/types.rs @@ -676,6 +676,8 @@ pub struct Para { pub kind: Kind, pub list: Option, pub align: Option, + /// The paragraph reads right to left (OOXML `w:bidi`, RTF `\rtlpar`). + pub rtl: bool, pub page_break: bool, /// Keep the paragraph on the same page as the next one (ODT `fo:keep-with-next`). pub keep_next: bool, diff --git a/crates/rtf/src/doc/export.rs b/crates/rtf/src/doc/export.rs index 69fab3d76..8f0f1734b 100644 --- a/crates/rtf/src/doc/export.rs +++ b/crates/rtf/src/doc/export.rs @@ -664,6 +664,10 @@ impl Writer { Some(Align::Distribute) => out.push_str("\\qd"), _ => out.push_str("\\ql"), } + // The paragraph's text direction, as Word writes it (`\rtlpar`; `\ltrpar` is the default). + if p.rtl { + out.push_str("\\rtlpar"); + } if p.page_break { out.push_str("\\pagebb"); } diff --git a/crates/rtf/src/doc/state.rs b/crates/rtf/src/doc/state.rs index 486b94a32..1f5d8d2e6 100644 --- a/crates/rtf/src/doc/state.rs +++ b/crates/rtf/src/doc/state.rs @@ -148,6 +148,8 @@ pub(super) struct St { pub(super) hidden: bool, // Paragraph properties. pub(super) align: Option, + /// `\rtlpar`: the paragraph reads right to left. + pub(super) rtldir: bool, pub(super) style: i32, pub(super) intbl: bool, pub(super) ls: Option, @@ -185,6 +187,7 @@ impl Default for St { uc: 1, hidden: false, align: None, + rtldir: false, style: 0, intbl: false, ls: None, diff --git a/crates/rtf/src/doc/styles.rs b/crates/rtf/src/doc/styles.rs index 014a105b4..dd42f3b87 100644 --- a/crates/rtf/src/doc/styles.rs +++ b/crates/rtf/src/doc/styles.rs @@ -87,6 +87,7 @@ impl Reader { pub(super) fn para_props(&self, st: &St) -> Para { let mut p = Para::new(self.para_kind(st)); p.align = st.align; + p.rtl = st.rtldir; p.page_break = st.pagebb; p.spacing = spacing(st); p.borders = st.borders.any_visible().then_some(st.borders); diff --git a/crates/rtf/src/doc/tests.rs b/crates/rtf/src/doc/tests.rs index 7c1075a86..9274104ab 100644 --- a/crates/rtf/src/doc/tests.rs +++ b/crates/rtf/src/doc/tests.rs @@ -38,6 +38,36 @@ fn basic_reader() { assert!(matches!(&l.inlines[0], Inline::Text(_, f) if f.link.as_deref() == Some("http://a.b"))); } +#[test] +fn rtl_paragraphs_read_and_write_their_direction() { + // Word writes an RTL paragraph as `\rtlpar` (`\ltrpar` for LTR); reading it keeps the + // direction as w:bidi on the document paragraph, and export writes it back. + let rtf = br#"{\rtf1\ansi\pard\rtlpar RTL\par\pard\ltrpar LTR\par\pard plain\par}"#; + let flow = parse(rtf).unwrap(); + let paras: Vec<&Para> = flow.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p) } else { None }).collect(); + assert!(paras[0].rtl, "rtlpar sets the direction"); + assert!(!paras[1].rtl && !paras[2].rtl, "ltrpar and no word: not RTL"); + // \rtldir and \ltrdir, which some writers used for paragraphs, are read too. + let old = parse(br#"{\rtf1\ansi\pard\rtldir RTL\par\pard\ltrdir LTR\par}"#).unwrap(); + let old: Vec<&Para> = old.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p) } else { None }).collect(); + assert!(old[0].rtl && !old[1].rtl, "rtldir and ltrdir are read"); + // \pard resets the direction. + let reset = parse(br#"{\rtf1\ansi\pard\rtlpar A\par\pard B\par}"#).unwrap(); + let reset: Vec<&Para> = reset.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p) } else { None }).collect(); + assert!(reset[0].rtl && !reset[1].rtl, "pard resets the direction"); + // Import keeps it as bidi on the document paragraph. + let bidi = |d: &Document| { + d.body.iter().filter_map(|b| if let Block::Para(p) = &**b { Some(p.props.bidi.unwrap_or(false)) } else { None }).collect::>() + }; + let doc = import(rtf).unwrap(); + assert_eq!(bidi(&doc), [true, false, false]); + // Export writes \rtlpar once, and not the \rtldir. + let out = export(&doc); + assert_eq!(out.matches("\\rtlpar").count(), 1, "{out}"); + assert!(!out.contains("rtldir"), "{out}"); + assert_eq!(bidi(&import(out.as_bytes()).unwrap()), [true, false, false]); +} + #[test] fn stylesheet_names_take_unicode_escapes() { // \u116?=t \u105?=i \u116?=t \u108?=l \u101?=e — how Word writes a non-Latin style name. diff --git a/crates/rtf/src/doc/word.rs b/crates/rtf/src/doc/word.rs index 28360c616..bf163db0c 100644 --- a/crates/rtf/src/doc/word.rs +++ b/crates/rtf/src/doc/word.rs @@ -171,6 +171,10 @@ impl Reader { "qr" => self.st.align = Some(Align::Right), "qj" => self.st.align = Some(Align::Justify), "qd" => self.st.align = Some(Align::Distribute), + // `\rtldir`/`\ltrdir` are not paragraph words (they set a table's or section's + // direction in RTF); some writers used them for paragraphs, so they are read too. + "rtlpar" | "rtldir" => self.st.rtldir = true, + "ltrpar" | "ltrdir" => self.st.rtldir = false, "s" => self.st.style = pv, "outlinelevel" => self.st.outline = Some(pv.clamp(0, 9) as u8), "pagebb" => self.st.pagebb = on, From 12656b3d0fad48bb958e42c50a07a0959966fb27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Legrand?= Date: Sat, 14 Nov 2026 17:20:40 +0100 Subject: [PATCH 006/150] docs(dxf): describe configurable tag limit --- crates/dxf/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/dxf/src/lib.rs b/crates/dxf/src/lib.rs index 4cf3e46e9..040fe3888 100644 --- a/crates/dxf/src/lib.rs +++ b/crates/dxf/src/lib.rs @@ -195,8 +195,9 @@ pub use codepage::decode_code_page; /// # Errors /// /// Returns an error for empty input, an invalid group code, an incomplete group-code/value pair, -/// truncated binary data, or more than [`MAX_TAGS`] group codes. For ASCII input, invalid numeric -/// text becomes `0.0` for real values or `0` for integer values, and an EOF tag is not required. +/// truncated binary data, or more group codes than the limit returned by [`max_tags()`], which is +/// [`MAX_TAGS`] by default. For ASCII input, invalid numeric text becomes `0.0` for real values or +/// `0` for integer values, and an EOF tag is not required. pub fn parse(bytes: &[u8]) -> Result> { parse_with_limit(bytes, max_tags()) } From 99223e1972bbb1682cfa57e7f6ab5d56c4411ca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Legrand?= Date: Sat, 31 Oct 2026 14:17:41 +0100 Subject: [PATCH 007/150] refactor(i18n): satisfy Clippy pedantic lints --- crates/i18n/src/collate.rs | 2 ++ crates/i18n/src/fold.rs | 14 +++++++++++--- crates/i18n/src/format.rs | 13 +++++++++---- crates/i18n/src/lib.rs | 17 ++++++++++++----- crates/i18n/src/number.rs | 11 +++++++++++ crates/i18n/src/region.rs | 4 ++++ crates/i18n/src/scripts.rs | 28 +++++++++++++++++++++++----- crates/i18n/src/size.rs | 8 ++++++++ 8 files changed, 80 insertions(+), 17 deletions(-) diff --git a/crates/i18n/src/collate.rs b/crates/i18n/src/collate.rs index 06d417897..629b3bf75 100644 --- a/crates/i18n/src/collate.rs +++ b/crates/i18n/src/collate.rs @@ -30,6 +30,7 @@ impl Collator { /// Sorting for `locale`, a language code such as `sv`, `pt-br` or `zh-hant` (any case, `-` /// or `_`, glibc forms such as `sv_SE.UTF-8` and `sr_RS@latin`). A BCP 47 collation type /// picks another order where there is one: `zh-hant-u-co-pinyin`, `zh-hans-u-co-stroke`. + #[must_use] pub fn new(locale: &str) -> Collator { let mut prefs: CollatorPreferences = Locale::try_from_str(&bcp47(locale)).unwrap_or(Locale::UNKNOWN).into(); prefs.numeric_ordering = Some(CollationNumericOrdering::True); @@ -46,6 +47,7 @@ impl Collator { /// the collation counts as equal (`Layer 2` and `Layer 02`) is in code point order, so a sort /// doesn't depend on the input order. Without collation data (never with the embedded data) /// this is code point order. + #[must_use] pub fn compare(&self, a: &str, b: &str) -> Ordering { match &self.0 { Some(c) => c.as_borrowed().compare(a, b).then_with(|| a.cmp(b)), diff --git a/crates/i18n/src/fold.rs b/crates/i18n/src/fold.rs index 959c6425a..dd77b6dfd 100644 --- a/crates/i18n/src/fold.rs +++ b/crates/i18n/src/fold.rs @@ -15,7 +15,7 @@ //! `IRMAK` and `ırmak`. Plain `str::to_lowercase` follows no locale and turns `İ` into `i` plus a //! combining dot, which nothing typed would match. //! -//! The Turkish i handling and sigma folding come from PdfCraft's `fold_case` (snipeTR, +//! The Turkish i handling and sigma folding come from `PdfCraft`'s `fold_case` (snipeTR, //! nevicia2010-hub). use std::ops::Range; @@ -34,6 +34,7 @@ pub struct Folder { impl Folder { /// Case- and accent-insensitive folding for `locale`, a language code such as `tr`, `pt-br` /// or `zh-hant` (any case, `-` or `_`). + #[must_use] pub fn new(locale: &str) -> Folder { let language = locale.split(['-', '_', '.', '@']).next().unwrap_or_default(); Folder { turkic: language.eq_ignore_ascii_case("tr") || language.eq_ignore_ascii_case("az"), accents: false } @@ -41,11 +42,13 @@ impl Folder { /// Accents count: `é` no longer matches `e`, and a match can't end on a letter whose accent it /// leaves out. + #[must_use] pub fn keep_accents(self) -> Folder { Folder { accents: true, ..self } } /// `s` folded, with the map back to `s`. + #[must_use] pub fn fold(&self, s: &str) -> Folded { let nfkd = DecomposingNormalizerBorrowed::new_nfkd(); let extend = CodePointSetData::new::(); @@ -75,17 +78,19 @@ impl Folder { } /// `s` folded, without the map: a key to compare or store. + #[must_use] pub fn key(&self, s: &str) -> String { self.fold(s).text } /// Whether `haystack` contains `needle`, both folded. + #[must_use] pub fn contains(&self, haystack: &str, needle: &str) -> bool { self.fold(haystack).find(&self.fold(needle)).is_some() } /// One cluster, already in NFKD. - fn fold_cluster(&self, cluster: &str, out: &mut String) { + fn fold_cluster(self, cluster: &str, out: &mut String) { let mut chars = cluster.chars().peekable(); while let Some(c) = chars.next() { match c { @@ -219,6 +224,7 @@ pub struct Folded { impl Folded { /// The folded text. + #[must_use] pub fn as_str(&self) -> &str { &self.text } @@ -226,6 +232,7 @@ impl Folded { /// The source byte range that folded bytes `range` came from, widened to whole source /// characters with their accents: `ss` in the folding of `Straße` maps back to `ß`, `e` in /// that of `é` to the whole `é`. An empty range maps to an empty one. + #[must_use] pub fn source_range(&self, range: Range) -> Range { // The last cluster starting at or before `at` is the one holding it: clusters that fold to // nothing share their offset with the next one. @@ -241,6 +248,7 @@ impl Folded { /// The first match of `needle` (folded the same way), as a byte range of the source text. /// An empty needle matches at the start. + #[must_use] pub fn find(&self, needle: &Folded) -> Option> { self.matches(needle).next() } @@ -363,10 +371,10 @@ mod tests { #[test] fn matches_map_back_to_the_source() { - let f = Folder::new("en"); fn find<'a>(f: Folder, haystack: &'a str, needle: &str) -> Option<&'a str> { f.fold(haystack).find(&f.fold(needle)).map(|r| &haystack[r]) } + let f = Folder::new("en"); let find = |haystack, needle| find(f, haystack, needle); assert_eq!(find("Große Straße", "strasse"), Some("Straße")); assert_eq!(find("Große Straße", "SS"), Some("ß")); diff --git a/crates/i18n/src/format.rs b/crates/i18n/src/format.rs index 14eb8c102..badfbdfca 100644 --- a/crates/i18n/src/format.rs +++ b/crates/i18n/src/format.rs @@ -4,7 +4,7 @@ use std::fmt; use fixed_decimal::{Decimal, Sign}; -use icu_decimal::{DecimalFormatter, parts}; +use icu_decimal::{DecimalFormatter, options::DecimalFormatterOptions, parts}; use icu_locale_core::Locale; use icu_locale_core::extensions::unicode::{key, value}; use writeable::{Part, PartsWrite, Writeable}; @@ -46,7 +46,8 @@ impl Format { if digits == Digits::Latin { locale.extensions.unicode.keywords.set(key!("nu"), value!("latn")); } - let decimal = data::provider().and_then(|p| DecimalFormatter::try_new_with_buffer_provider(p, (&locale).into(), Default::default()).ok()); + let decimal = data::provider() + .and_then(|p| DecimalFormatter::try_new_with_buffer_provider(p, (&locale).into(), DecimalFormatterOptions::default()).ok()); let mut format = Format { decimal, group: None, point: None, percent: ("", "%"), marks: Marks::default() }; // The locale's own separators and digits, read from a number that shows them all. let probe = format.parts(&Decimal::try_from_str("-1234567.5").unwrap_or_default()); @@ -81,6 +82,7 @@ impl Format { } /// The format for a locale tag (`de-CH`, `ar-EG`), with the locale's own digits. + #[must_use] pub fn from_tag(tag: &str) -> Format { Format::new(&Region::from_tag(tag), Digits::Locale) } @@ -92,6 +94,7 @@ impl Format { /// `value` rounded to `decimals` digits after the decimal separator; infinities and NaN are /// `∞`, `-∞` and `NaN`. + #[must_use] pub fn number(&self, value: f64, decimals: usize) -> String { match to_decimal(value, decimals) { Some(d) => self.write(&d), @@ -101,6 +104,7 @@ impl Format { /// [`Self::number`] without grouping separators, for values such as pixel sizes and years: /// `1920`, `١٩٢٠`, `1234,5`. + #[must_use] pub fn number_ungrouped(&self, value: f64, decimals: usize) -> String { match to_decimal(value, decimals) { Some(d) => { @@ -113,6 +117,7 @@ impl Format { /// `ratio` as a percentage with `decimals` digits after the decimal separator: `0.5` is /// `50%`, `50 %` (French, German), `%50` (Turkish), `٥٠٪؜` (Egyptian Arabic). + #[must_use] pub fn percent(&self, ratio: f64, decimals: usize) -> String { let Some(d) = to_decimal(ratio * 100.0, decimals) else { return format!("{}{}{}", self.percent.0, non_finite(ratio), self.percent.1); @@ -212,7 +217,7 @@ impl PartsWrite for Sink<'_> { _ => return result, }; if slot.is_empty() { - *slot = written.to_owned(); + written.clone_into(slot); } } result @@ -370,7 +375,7 @@ mod tests { for (tag, want) in [("en", "50%"), ("de", "50\u{a0}%"), ("fr", "12,5\u{a0}%"), ("tr", "%50"), ("ar-EG", "٥٠٪\u{61c}"), ("fa", "ÛµÛ°Ùª"), ("hi", "50%")] { - let decimals = if tag == "fr" { 1 } else { 0 }; + let decimals = usize::from(tag == "fr"); let ratio = if tag == "fr" { 0.125 } else { 0.5 }; assert_eq!(Format::from_tag(tag).percent(ratio, decimals), want, "{tag}"); } diff --git a/crates/i18n/src/lib.rs b/crates/i18n/src/lib.rs index 53bbffe9e..ce473d84f 100644 --- a/crates/i18n/src/lib.rs +++ b/crates/i18n/src/lib.rs @@ -113,7 +113,9 @@ impl Locale { rows.entry(e.context).or_default().insert(e.source, e.translation); } } - problems.iter().for_each(|p| log::warn!("translation catalog: {p}; row skipped")); + for p in &problems { + log::warn!("translation catalog: {p}; row skipped"); + } Parsed { rows, problems } }) } @@ -156,6 +158,7 @@ impl I18n { /// to left, say). Locales that can't be used are skipped and come back as problems for the app /// to log. A locale's bad rows are logged (`log::warn!`) when it is first looked up in, and /// [`I18n::check_all`] lists them all. + #[must_use] pub fn new(embedded: &'static Embedded, offered: &[&str]) -> (I18n, Vec) { let mut problems = Vec::new(); let mut locales = vec![bare("en"), bare("pseudo")]; @@ -417,6 +420,7 @@ impl I18n { /// value that already is one isolate keeps it; pass numbers, paths and codes through [`ltr`]. /// Other text gets no marks, and a value's own isolate is taken off. Unknown placeholders stay as written, /// and inserted values are never read as templates, so a file name containing `{n}` stays intact. +#[must_use] pub fn fmt(template: &str, args: &[(&str, &str)]) -> String { fill(template, args, true) } @@ -424,6 +428,7 @@ pub fn fmt(template: &str, args: &[(&str, &str)]) -> String { /// [`fmt`] without isolates, for text that goes where the marks would get in the way: file /// names, logs, terminals, text compared or parsed later. Every isolate mark is taken off, in /// the template and in the values. +#[must_use] pub fn fmt_plain(template: &str, args: &[(&str, &str)]) -> String { strip_isolates(&fill(template, args, false)) } @@ -444,6 +449,7 @@ const ALM: char = '\u{061C}'; /// `value` as a first-strong isolate (FSI…PDI): it takes the direction of its first strong /// letter and can't reorder the text around it. What [`fmt`] does to each value in text with a /// right-to-left letter. +#[must_use] pub fn isolate(value: &str) -> String { wrap(FSI, value) } @@ -452,12 +458,14 @@ pub fn isolate(value: &str) -> String { /// codes, which read left to right in every language. In an RTL first-strong isolate, the /// segments of `/home/مستخدم/file.txt` would run right to left. [`fmt`] takes the isolate off /// again in text with no right-to-left letter. +#[must_use] pub fn ltr(value: &str) -> String { wrap(LRI, value) } /// `text` without isolate marks (LRI, RLI, FSI, PDI): a [`fmt`] or [`I18n::trn`] result made fit /// for a file name, a log or a comparison. +#[must_use] pub fn strip_isolates(text: &str) -> String { text.chars().filter(|c| !matches!(*c, LRI | RLI | FSI | PDI)).collect() } @@ -483,14 +491,13 @@ fn wrap(initiator: char, value: &str) -> String { PDF if open.last() == Some(&false) => { open.pop(); } - PDF => continue, PDI if isolates > 0 => { while open.pop() == Some(false) { out.push(PDF); } isolates -= 1; } - PDI => continue, + PDF | PDI => continue, _ => {} } out.push(c); @@ -713,11 +720,11 @@ fn plural_rules(code: &str) -> Option<(PluralRules, Vec)> { categories.push(c); } } - categories.sort_by_key(rank); + categories.sort_by_key(|c| rank(*c)); Some((rules, categories)) } -fn rank(c: &PluralCategory) -> u8 { +fn rank(c: PluralCategory) -> u8 { match c { PluralCategory::Zero => 0, PluralCategory::One => 1, diff --git a/crates/i18n/src/number.rs b/crates/i18n/src/number.rs index 1120fbe88..211d9dfe0 100644 --- a/crates/i18n/src/number.rs +++ b/crates/i18n/src/number.rs @@ -80,6 +80,12 @@ impl Format { /// /// Percent signs, units and the space before a unit (`50 %`, `12 px`) are not read; strip them /// first. Anything else is an error rather than a guess. + /// + /// # Errors + /// + /// Returns `NumberError::Empty` for an empty input or one reduced to spaces and a sign, + /// `Unexpected` for an invalid character, `MixedDigits` for mixed digit sets, `Separator` + /// for misplaced marks, and `TooLarge` when the parsed value is not finite. pub fn parse(&self, text: &str) -> Result { let marks = self.marks; let text: String = text.chars().filter(|c| !is_bidi_mark(*c)).collect(); @@ -129,6 +135,11 @@ impl Format { /// The number in `text` as `locale` (a language tag, `de-CH`, `ar_EG.UTF-8`) writes it: /// [`Format::parse`] for [`Format::from_tag`]. Build the [`Format`] once to read many numbers. A /// locale CLDR doesn't know reads as CLDR's root does (`1,234.5`). +/// +/// # Errors +/// +/// Returns the same `NumberError` as [`Format::parse`] for invalid digits, characters, or +/// separators, or when the value is not finite. pub fn parse_number(text: &str, locale: &str) -> Result { Format::from_tag(locale).parse(text) } diff --git a/crates/i18n/src/region.rs b/crates/i18n/src/region.rs index fcdffe8af..205444c25 100644 --- a/crates/i18n/src/region.rs +++ b/crates/i18n/src/region.rs @@ -73,6 +73,7 @@ impl Region { /// `nu` stays in the number and time locales (`ar-EG-u-nu-latn`). `C` and `POSIX` are the C /// locale: English, A4, metric, a `.` decimal and no grouping. An empty or unreadable tag is /// `en-US`; a bare `en` is the United States, any other bare language has no territory. + #[must_use] pub fn from_tag(tag: &str) -> Region { match Tag::parse(tag) { Some(tag) => Region::from_parts(&tag), @@ -85,6 +86,7 @@ impl Region { /// The region for an app's saved region setting: `""` or `auto` (any case) is /// [`Region::system`], anything else a locale tag for [`Region::from_tag`]. + #[must_use] pub fn from_setting(setting: &str) -> Region { let setting = setting.trim(); if setting.is_empty() || setting.eq_ignore_ascii_case("auto") { Region::system() } else { Region::from_tag(setting) } @@ -99,6 +101,7 @@ impl Region { /// - Linux and the BSDs: `LC_ALL`, else each of `LC_NUMERIC`, `LC_TIME`, `LC_MEASUREMENT` and /// `LC_PAPER` on its own, else `LANG`. /// - The web and Android: the preferred language, the only locale they expose. + #[must_use] pub fn system() -> Region { static SYSTEM: OnceLock = OnceLock::new(); SYSTEM.get_or_init(read_system).clone() @@ -298,6 +301,7 @@ fn from_apple(identifier: &str, metric: bool, decimal: &str, grouping: &str, hou /// A grouping separator the system sets: none at all when empty (Windows' `sThousand` the user /// cleared), else one character. #[cfg(any(test, windows, target_vendor = "apple"))] +#[expect(clippy::option_option, reason = "The public setting distinguishes following the locale, no grouping, and a custom separator")] fn grouping(s: &str) -> Option> { if s.is_empty() { Some(None) } else { single_char(s).map(Some) } } diff --git a/crates/i18n/src/scripts.rs b/crates/i18n/src/scripts.rs index 1ae0f29bc..e16342e26 100644 --- a/crates/i18n/src/scripts.rs +++ b/crates/i18n/src/scripts.rs @@ -39,6 +39,7 @@ pub enum Script { impl Script { /// The ISO 15924 code, `Latn`. + #[must_use] pub fn code(self) -> &'static str { match self { Script::Arab => "Arab", @@ -75,6 +76,7 @@ impl Script { } /// Whether text in this script runs right to left. + #[must_use] pub fn rtl(self) -> bool { matches!(self, Script::Arab | Script::Hebr | Script::Syrc | Script::Thaa) } @@ -112,8 +114,12 @@ pub enum Han { /// where it is the language's own script. A language not in the table is taken to be written in /// Latin. // ponytail: the languages a Craft app ships or could plausibly ship; add a row with the locale. +#[must_use] pub fn scripts(tag: &str) -> &'static [Script] { - use Script::*; + use Script::{ + Arab, Armn, Beng, Cyrl, Deva, Ethi, Geor, Grek, Gujr, Guru, Hang, Hani, Hebr, Hira, Kana, Khmr, Knda, Laoo, Latn, Mlym, Mymr, Orya, Sinh, + Syrc, Taml, Telu, Thaa, Thai, Tibt, + }; let tag = Subtags::parse(tag); if let Some(s) = tag.script.as_deref().and_then(subtag_scripts) { return s; @@ -154,6 +160,7 @@ pub fn scripts(tag: &str) -> &'static [Script] { } /// The direction of a language tag's text: right to left when its main script is. +#[must_use] pub fn direction(tag: &str) -> Direction { match scripts(tag).first() { Some(s) if s.rtl() => Direction::Rtl, @@ -165,6 +172,7 @@ pub fn direction(tag: &str) -> Direction { /// P3): the contents of isolates (LRI, RLI or FSI up to the matching PDI) are skipped, and the /// search stops at the first paragraph separator. `None` when the paragraph has no strong /// character; P3 then reads it as left to right, and an app may use the interface direction. +#[must_use] pub fn base_direction(text: &str) -> Option { match unicode_bidi::get_base_direction(text) { unicode_bidi::Direction::Ltr => Some(Direction::Ltr), @@ -175,9 +183,10 @@ pub fn base_direction(text: &str) -> Option { /// The forms a language tag's Han characters take: `zh-CN`, `zh-SG`, `zh-Hans` and plain `zh` → /// Simplified; `zh-TW`, `zh-Hant` and the other regions CLDR writes in Traditional characters -/// (`zh-US`, `zh-AU`) → Traditional; `zh-HK`, `zh-MO`, `zh-Hant-HK` and `yue` → HongKong; `ja` → +/// (`zh-US`, `zh-AU`) → Traditional; `zh-HK`, `zh-MO`, `zh-Hant-HK` and `yue` → `HongKong`; `ja` → /// Japanese; `ko` → Korean. `None` for a language not written with Han, which leaves the choice /// to the font stack. +#[must_use] pub fn han(tag: &str) -> Option { let tag = Subtags::parse(tag); let hong_kong = matches!(tag.region.as_deref(), Some("hk" | "mo")); @@ -202,8 +211,14 @@ pub fn han(tag: &str) -> Option { // ponytail: mn-CN → Mong is left out until vertical Mongolian layout is supported. pub(crate) fn region_script(language: &str, region: &str) -> Option<&'static str> { Some(match (language, region) { - ("az", "iq" | "ir") | ("ha", "cm" | "sd") | ("kk", "af" | "cn" | "ir" | "mn") | ("ku", "iq" | "ir" | "lb") => "arab", - ("ky", "cn") | ("ms", "cc") | ("pa", "pk") | ("tg", "pk") | ("uz", "af") => "arab", + ("az", "iq" | "ir") + | ("ha", "cm" | "sd") + | ("kk", "af" | "cn" | "ir" | "mn") + | ("ku", "iq" | "ir" | "lb") + | ("ky", "cn") + | ("ms", "cc") + | ("pa" | "tg", "pk") + | ("uz", "af") => "arab", ("az", "ru") | ("ku", "am" | "az" | "ge" | "tm") | ("ug", "kz" | "mn") | ("uz", "cn") => "cyrl", ("ky", "tr") | ("sr", "me" | "ro" | "tr") => "latn", ("sd", "in") => "deva", @@ -216,7 +231,10 @@ pub(crate) fn region_script(language: &str, region: &str) -> Option<&'static str /// The scripts a script subtag stands for, including the CLDR unions (`jpan`, `kore`). `None` for /// one not in [`Script`], so the language's own scripts apply. fn subtag_scripts(subtag: &str) -> Option<&'static [Script]> { - use Script::*; + use Script::{ + Arab, Armn, Beng, Cyrl, Deva, Ethi, Geor, Grek, Gujr, Guru, Hang, Hani, Hebr, Hira, Kana, Khmr, Knda, Laoo, Latn, Mlym, Mong, Mymr, Orya, + Sinh, Syrc, Taml, Telu, Thaa, Thai, Tibt, + }; Some(match subtag { "hans" | "hant" | "hani" => &[Hani], "jpan" => &[Hani, Hira, Kana], diff --git a/crates/i18n/src/size.rs b/crates/i18n/src/size.rs index 830c09573..b316360a5 100644 --- a/crates/i18n/src/size.rs +++ b/crates/i18n/src/size.rs @@ -33,6 +33,7 @@ impl SizeUnit { /// // `12 MB`, `3.4 MB`, `640 KB`: decimal steps, whole kilobytes, no decimals from 10 MB. /// let style = FileSize::DECIMAL.smallest(SizeUnit::Kilo).largest(SizeUnit::Mega).unit_decimals(SizeUnit::Kilo, 0).whole_from(10); /// ``` +#[must_use] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct FileSize { step: u64, @@ -104,6 +105,10 @@ impl FileSize { } /// The unit `bytes` is written in, the number in it and its digits after the point. + #[expect( + clippy::cast_precision_loss, + reason = "File sizes are displayed as rounded f64 values; exact u64 arithmetic is not required for presentation" + )] fn parts(&self, bytes: u64) -> (SizeUnit, f64, usize) { let (smallest, largest) = (self.smallest.min(self.largest), self.largest); let mut unit = smallest; @@ -137,6 +142,7 @@ impl FileSize { } } + #[expect(clippy::cast_precision_loss, reason = "Unit scales are displayed as f64 values; exact u64 arithmetic is not required for presentation")] fn scale(&self, unit: SizeUnit) -> f64 { (self.step as f64).powi(unit as i32) } @@ -160,6 +166,7 @@ impl FileSize { impl I18n { /// `bytes` as a file size in `lang`, written the way `style` says (`1.5 MB`, `512 bytes`), the /// number as [`I18n::format`] writes it (`1,5 Mo`). + #[must_use] pub fn file_size(&self, lang: Lang, bytes: u64, style: FileSize) -> String { let (unit, n, decimals) = style.parts(bytes); if unit == SizeUnit::Byte && !style.byte_symbol { @@ -171,6 +178,7 @@ impl I18n { /// `value` with `decimals` digits after the decimal separator in the unit template `unit`, an /// English catalog key with the number as `{n}` (`{n} px`, `{n} dB`), the number as /// [`I18n::format`] writes it. Percentages are [`crate::Format::percent`]. + #[must_use] pub fn measure(&self, lang: Lang, value: f64, decimals: usize, unit: &str) -> String { self.unit_text(lang, unit, &self.format().number(value, decimals)) } From 271a2a7296be4b9ba4a0f7b6e13a09fb5377d83f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Legrand?= Date: Sun, 1 Nov 2026 22:08:57 +0100 Subject: [PATCH 008/150] style: remove review churn --- crates/i18n/src/collate.rs | 2 -- crates/i18n/src/fold.rs | 8 -------- crates/i18n/src/format.rs | 4 ---- crates/i18n/src/lib.rs | 6 ------ crates/i18n/src/region.rs | 3 --- crates/i18n/src/scripts.rs | 6 ------ crates/i18n/src/size.rs | 3 --- 7 files changed, 32 deletions(-) diff --git a/crates/i18n/src/collate.rs b/crates/i18n/src/collate.rs index 629b3bf75..06d417897 100644 --- a/crates/i18n/src/collate.rs +++ b/crates/i18n/src/collate.rs @@ -30,7 +30,6 @@ impl Collator { /// Sorting for `locale`, a language code such as `sv`, `pt-br` or `zh-hant` (any case, `-` /// or `_`, glibc forms such as `sv_SE.UTF-8` and `sr_RS@latin`). A BCP 47 collation type /// picks another order where there is one: `zh-hant-u-co-pinyin`, `zh-hans-u-co-stroke`. - #[must_use] pub fn new(locale: &str) -> Collator { let mut prefs: CollatorPreferences = Locale::try_from_str(&bcp47(locale)).unwrap_or(Locale::UNKNOWN).into(); prefs.numeric_ordering = Some(CollationNumericOrdering::True); @@ -47,7 +46,6 @@ impl Collator { /// the collation counts as equal (`Layer 2` and `Layer 02`) is in code point order, so a sort /// doesn't depend on the input order. Without collation data (never with the embedded data) /// this is code point order. - #[must_use] pub fn compare(&self, a: &str, b: &str) -> Ordering { match &self.0 { Some(c) => c.as_borrowed().compare(a, b).then_with(|| a.cmp(b)), diff --git a/crates/i18n/src/fold.rs b/crates/i18n/src/fold.rs index dd77b6dfd..fb420e1e1 100644 --- a/crates/i18n/src/fold.rs +++ b/crates/i18n/src/fold.rs @@ -34,7 +34,6 @@ pub struct Folder { impl Folder { /// Case- and accent-insensitive folding for `locale`, a language code such as `tr`, `pt-br` /// or `zh-hant` (any case, `-` or `_`). - #[must_use] pub fn new(locale: &str) -> Folder { let language = locale.split(['-', '_', '.', '@']).next().unwrap_or_default(); Folder { turkic: language.eq_ignore_ascii_case("tr") || language.eq_ignore_ascii_case("az"), accents: false } @@ -42,13 +41,11 @@ impl Folder { /// Accents count: `é` no longer matches `e`, and a match can't end on a letter whose accent it /// leaves out. - #[must_use] pub fn keep_accents(self) -> Folder { Folder { accents: true, ..self } } /// `s` folded, with the map back to `s`. - #[must_use] pub fn fold(&self, s: &str) -> Folded { let nfkd = DecomposingNormalizerBorrowed::new_nfkd(); let extend = CodePointSetData::new::(); @@ -78,13 +75,11 @@ impl Folder { } /// `s` folded, without the map: a key to compare or store. - #[must_use] pub fn key(&self, s: &str) -> String { self.fold(s).text } /// Whether `haystack` contains `needle`, both folded. - #[must_use] pub fn contains(&self, haystack: &str, needle: &str) -> bool { self.fold(haystack).find(&self.fold(needle)).is_some() } @@ -224,7 +219,6 @@ pub struct Folded { impl Folded { /// The folded text. - #[must_use] pub fn as_str(&self) -> &str { &self.text } @@ -232,7 +226,6 @@ impl Folded { /// The source byte range that folded bytes `range` came from, widened to whole source /// characters with their accents: `ss` in the folding of `Straße` maps back to `ß`, `e` in /// that of `é` to the whole `é`. An empty range maps to an empty one. - #[must_use] pub fn source_range(&self, range: Range) -> Range { // The last cluster starting at or before `at` is the one holding it: clusters that fold to // nothing share their offset with the next one. @@ -248,7 +241,6 @@ impl Folded { /// The first match of `needle` (folded the same way), as a byte range of the source text. /// An empty needle matches at the start. - #[must_use] pub fn find(&self, needle: &Folded) -> Option> { self.matches(needle).next() } diff --git a/crates/i18n/src/format.rs b/crates/i18n/src/format.rs index badfbdfca..b907b469c 100644 --- a/crates/i18n/src/format.rs +++ b/crates/i18n/src/format.rs @@ -82,7 +82,6 @@ impl Format { } /// The format for a locale tag (`de-CH`, `ar-EG`), with the locale's own digits. - #[must_use] pub fn from_tag(tag: &str) -> Format { Format::new(&Region::from_tag(tag), Digits::Locale) } @@ -94,7 +93,6 @@ impl Format { /// `value` rounded to `decimals` digits after the decimal separator; infinities and NaN are /// `∞`, `-∞` and `NaN`. - #[must_use] pub fn number(&self, value: f64, decimals: usize) -> String { match to_decimal(value, decimals) { Some(d) => self.write(&d), @@ -104,7 +102,6 @@ impl Format { /// [`Self::number`] without grouping separators, for values such as pixel sizes and years: /// `1920`, `١٩٢٠`, `1234,5`. - #[must_use] pub fn number_ungrouped(&self, value: f64, decimals: usize) -> String { match to_decimal(value, decimals) { Some(d) => { @@ -117,7 +114,6 @@ impl Format { /// `ratio` as a percentage with `decimals` digits after the decimal separator: `0.5` is /// `50%`, `50 %` (French, German), `%50` (Turkish), `٥٠٪؜` (Egyptian Arabic). - #[must_use] pub fn percent(&self, ratio: f64, decimals: usize) -> String { let Some(d) = to_decimal(ratio * 100.0, decimals) else { return format!("{}{}{}", self.percent.0, non_finite(ratio), self.percent.1); diff --git a/crates/i18n/src/lib.rs b/crates/i18n/src/lib.rs index ce473d84f..24ce578a6 100644 --- a/crates/i18n/src/lib.rs +++ b/crates/i18n/src/lib.rs @@ -158,7 +158,6 @@ impl I18n { /// to left, say). Locales that can't be used are skipped and come back as problems for the app /// to log. A locale's bad rows are logged (`log::warn!`) when it is first looked up in, and /// [`I18n::check_all`] lists them all. - #[must_use] pub fn new(embedded: &'static Embedded, offered: &[&str]) -> (I18n, Vec) { let mut problems = Vec::new(); let mut locales = vec![bare("en"), bare("pseudo")]; @@ -420,7 +419,6 @@ impl I18n { /// value that already is one isolate keeps it; pass numbers, paths and codes through [`ltr`]. /// Other text gets no marks, and a value's own isolate is taken off. Unknown placeholders stay as written, /// and inserted values are never read as templates, so a file name containing `{n}` stays intact. -#[must_use] pub fn fmt(template: &str, args: &[(&str, &str)]) -> String { fill(template, args, true) } @@ -428,7 +426,6 @@ pub fn fmt(template: &str, args: &[(&str, &str)]) -> String { /// [`fmt`] without isolates, for text that goes where the marks would get in the way: file /// names, logs, terminals, text compared or parsed later. Every isolate mark is taken off, in /// the template and in the values. -#[must_use] pub fn fmt_plain(template: &str, args: &[(&str, &str)]) -> String { strip_isolates(&fill(template, args, false)) } @@ -449,7 +446,6 @@ const ALM: char = '\u{061C}'; /// `value` as a first-strong isolate (FSI…PDI): it takes the direction of its first strong /// letter and can't reorder the text around it. What [`fmt`] does to each value in text with a /// right-to-left letter. -#[must_use] pub fn isolate(value: &str) -> String { wrap(FSI, value) } @@ -458,14 +454,12 @@ pub fn isolate(value: &str) -> String { /// codes, which read left to right in every language. In an RTL first-strong isolate, the /// segments of `/home/مستخدم/file.txt` would run right to left. [`fmt`] takes the isolate off /// again in text with no right-to-left letter. -#[must_use] pub fn ltr(value: &str) -> String { wrap(LRI, value) } /// `text` without isolate marks (LRI, RLI, FSI, PDI): a [`fmt`] or [`I18n::trn`] result made fit /// for a file name, a log or a comparison. -#[must_use] pub fn strip_isolates(text: &str) -> String { text.chars().filter(|c| !matches!(*c, LRI | RLI | FSI | PDI)).collect() } diff --git a/crates/i18n/src/region.rs b/crates/i18n/src/region.rs index 205444c25..15874d67f 100644 --- a/crates/i18n/src/region.rs +++ b/crates/i18n/src/region.rs @@ -73,7 +73,6 @@ impl Region { /// `nu` stays in the number and time locales (`ar-EG-u-nu-latn`). `C` and `POSIX` are the C /// locale: English, A4, metric, a `.` decimal and no grouping. An empty or unreadable tag is /// `en-US`; a bare `en` is the United States, any other bare language has no territory. - #[must_use] pub fn from_tag(tag: &str) -> Region { match Tag::parse(tag) { Some(tag) => Region::from_parts(&tag), @@ -86,7 +85,6 @@ impl Region { /// The region for an app's saved region setting: `""` or `auto` (any case) is /// [`Region::system`], anything else a locale tag for [`Region::from_tag`]. - #[must_use] pub fn from_setting(setting: &str) -> Region { let setting = setting.trim(); if setting.is_empty() || setting.eq_ignore_ascii_case("auto") { Region::system() } else { Region::from_tag(setting) } @@ -101,7 +99,6 @@ impl Region { /// - Linux and the BSDs: `LC_ALL`, else each of `LC_NUMERIC`, `LC_TIME`, `LC_MEASUREMENT` and /// `LC_PAPER` on its own, else `LANG`. /// - The web and Android: the preferred language, the only locale they expose. - #[must_use] pub fn system() -> Region { static SYSTEM: OnceLock = OnceLock::new(); SYSTEM.get_or_init(read_system).clone() diff --git a/crates/i18n/src/scripts.rs b/crates/i18n/src/scripts.rs index e16342e26..aa39d8917 100644 --- a/crates/i18n/src/scripts.rs +++ b/crates/i18n/src/scripts.rs @@ -39,7 +39,6 @@ pub enum Script { impl Script { /// The ISO 15924 code, `Latn`. - #[must_use] pub fn code(self) -> &'static str { match self { Script::Arab => "Arab", @@ -76,7 +75,6 @@ impl Script { } /// Whether text in this script runs right to left. - #[must_use] pub fn rtl(self) -> bool { matches!(self, Script::Arab | Script::Hebr | Script::Syrc | Script::Thaa) } @@ -114,7 +112,6 @@ pub enum Han { /// where it is the language's own script. A language not in the table is taken to be written in /// Latin. // ponytail: the languages a Craft app ships or could plausibly ship; add a row with the locale. -#[must_use] pub fn scripts(tag: &str) -> &'static [Script] { use Script::{ Arab, Armn, Beng, Cyrl, Deva, Ethi, Geor, Grek, Gujr, Guru, Hang, Hani, Hebr, Hira, Kana, Khmr, Knda, Laoo, Latn, Mlym, Mymr, Orya, Sinh, @@ -160,7 +157,6 @@ pub fn scripts(tag: &str) -> &'static [Script] { } /// The direction of a language tag's text: right to left when its main script is. -#[must_use] pub fn direction(tag: &str) -> Direction { match scripts(tag).first() { Some(s) if s.rtl() => Direction::Rtl, @@ -172,7 +168,6 @@ pub fn direction(tag: &str) -> Direction { /// P3): the contents of isolates (LRI, RLI or FSI up to the matching PDI) are skipped, and the /// search stops at the first paragraph separator. `None` when the paragraph has no strong /// character; P3 then reads it as left to right, and an app may use the interface direction. -#[must_use] pub fn base_direction(text: &str) -> Option { match unicode_bidi::get_base_direction(text) { unicode_bidi::Direction::Ltr => Some(Direction::Ltr), @@ -186,7 +181,6 @@ pub fn base_direction(text: &str) -> Option { /// (`zh-US`, `zh-AU`) → Traditional; `zh-HK`, `zh-MO`, `zh-Hant-HK` and `yue` → `HongKong`; `ja` → /// Japanese; `ko` → Korean. `None` for a language not written with Han, which leaves the choice /// to the font stack. -#[must_use] pub fn han(tag: &str) -> Option { let tag = Subtags::parse(tag); let hong_kong = matches!(tag.region.as_deref(), Some("hk" | "mo")); diff --git a/crates/i18n/src/size.rs b/crates/i18n/src/size.rs index b316360a5..e0d45d59d 100644 --- a/crates/i18n/src/size.rs +++ b/crates/i18n/src/size.rs @@ -33,7 +33,6 @@ impl SizeUnit { /// // `12 MB`, `3.4 MB`, `640 KB`: decimal steps, whole kilobytes, no decimals from 10 MB. /// let style = FileSize::DECIMAL.smallest(SizeUnit::Kilo).largest(SizeUnit::Mega).unit_decimals(SizeUnit::Kilo, 0).whole_from(10); /// ``` -#[must_use] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct FileSize { step: u64, @@ -166,7 +165,6 @@ impl FileSize { impl I18n { /// `bytes` as a file size in `lang`, written the way `style` says (`1.5 MB`, `512 bytes`), the /// number as [`I18n::format`] writes it (`1,5 Mo`). - #[must_use] pub fn file_size(&self, lang: Lang, bytes: u64, style: FileSize) -> String { let (unit, n, decimals) = style.parts(bytes); if unit == SizeUnit::Byte && !style.byte_symbol { @@ -178,7 +176,6 @@ impl I18n { /// `value` with `decimals` digits after the decimal separator in the unit template `unit`, an /// English catalog key with the number as `{n}` (`{n} px`, `{n} dB`), the number as /// [`I18n::format`] writes it. Percentages are [`crate::Format::percent`]. - #[must_use] pub fn measure(&self, lang: Lang, value: f64, decimals: usize, unit: &str) -> String { self.unit_text(lang, unit, &self.format().number(value, decimals)) } From 1a4e11926ce8fe6851f881537798299bd629dcb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Legrand?= Date: Sat, 14 Nov 2026 17:11:58 +0100 Subject: [PATCH 009/150] refactor(dwg): apply behavior-preserving cleanups --- crates/dwg/src/lib.rs | 79 ++++++++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 24 deletions(-) diff --git a/crates/dwg/src/lib.rs b/crates/dwg/src/lib.rs index 2db7afeae..de42828c7 100644 --- a/crates/dwg/src/lib.rs +++ b/crates/dwg/src/lib.rs @@ -57,6 +57,10 @@ impl Limits { /// A byte count in megabytes for messages ("9.9 MB", "1024 MB"). #[cfg_attr(no_dwg_bridge, allow(dead_code))] +#[expect( + clippy::cast_precision_loss, + reason = "Megabyte formatting is rounded for display; exact integer precision beyond 2^53 bytes is not meaningful." +)] fn mb(n: u64) -> String { let m = n as f64 / f64::from(1u32 << 20); if m >= 100.0 { format!("{m:.0} MB") } else { format!("{m:.1} MB") } @@ -71,12 +75,17 @@ mod proxy_transport; #[cfg(not(no_dwg_bridge))] mod native { + use std::fmt::Write as _; use std::io::{Cursor, Write}; use std::panic::AssertUnwindSafe; use super::{Limits, mb}; /// Read DWG bytes and return an ASCII DXF rendition, within [`Limits::DEFAULT`]. + /// + /// # Errors + /// + /// Returns an error when the DWG cannot be read or post-processed, or its DXF rendition exceeds the default limits. pub fn dwg_to_dxf(bytes: &[u8]) -> Result, String> { dwg_to_dxf_with(bytes, Limits::DEFAULT) } @@ -84,6 +93,10 @@ mod native { /// [`dwg_to_dxf`] with explicit limits. Both are checked before the expensive step they /// guard: the input size before the DWG reader runs, the DXF size while it is written, so a /// runaway conversion stops at the limit instead of filling memory first. + /// + /// # Errors + /// + /// Returns an error when the input exceeds `limits`, decoding fails, or the DXF output exceeds `limits`. pub fn dwg_to_dxf_with(bytes: &[u8], limits: Limits) -> Result, String> { dwg_to_dxf_noted(bytes, limits).map(|(dxf, _)| dxf) } @@ -92,6 +105,10 @@ mod native { /// failsafe mode: an object record it can't decode is skipped instead of failing the whole /// file, and the notes say how many were skipped and of which types, so a partial read never /// passes for a complete one. + /// + /// # Errors + /// + /// Returns an error when the input exceeds `limits`, decoding fails, or the DXF output exceeds `limits`. pub fn dwg_to_dxf_noted(bytes: &[u8], limits: Limits) -> Result<(Vec, Vec), String> { if bytes.len() > limits.max_dwg_bytes { return Err(format!( @@ -114,13 +131,13 @@ mod native { let mut notes = read_notes(&outcome.stats); let mut doc = outcome.document; notes.extend( - guarded(AssertUnwindSafe(|| application_objects(&mut doc))).map_err(|_| "the DWG application object conversion failed".to_string())?, + guarded(AssertUnwindSafe(|| application_objects(&mut doc))).map_err(|()| "the DWG application object conversion failed".to_string())?, ); // After application_objects, so the proxy objects it makes are numbered as the DXF's. notes.extend( - guarded(AssertUnwindSafe(|| super::proxy_transport::prepare(&mut doc))).map_err(|_| "the DWG proxy conversion failed".to_string())?, + guarded(AssertUnwindSafe(|| super::proxy_transport::prepare(&mut doc))).map_err(|()| "the DWG proxy conversion failed".to_string())?, ); - let orphans = guarded(AssertUnwindSafe(|| post_process(&mut doc))).map_err(|_| "the DWG post-processing failed on this file".to_string())?; + let orphans = guarded(AssertUnwindSafe(|| post_process(&mut doc))).map_err(|()| "the DWG post-processing failed on this file".to_string())?; if orphans > 0 { let (s, were) = if orphans == 1 { ("", "was") } else { ("s", "were") }; notes.push(format!("DWG: {orphans} DGN line style component{s} that no line style definition uses {were} left out")); @@ -624,7 +641,7 @@ mod native { // A real table has at most one merge per cell; more ranges than that come from a // damaged file and would only make this rows × cols × ranges. let max_ranges = rows.saturating_mul(cols).min(1 << 16); - for r in t.merged_ranges.iter().take(max_ranges).cloned().collect::>() { + for r in t.merged_ranges.iter().take(max_ranges).copied().collect::>() { // Clamp to the table: the ranges come from the file. let (bottom, right) = (r.bottom_row.min(rows.saturating_sub(1)), r.right_col.min(cols.saturating_sub(1))); if r.top_row > bottom || r.left_col > right || rows == 0 || cols == 0 || (r.top_row, r.left_col) == (bottom, right) { @@ -707,8 +724,8 @@ mod native { } /// Leave out DGN line style components (`LS*COMPONENT`) that no line style definition uses - /// (#1482). A drawing that once came from MicroStation can carry hundreds of thousands of - /// them, orphaned copies piled up by earlier DGN imports and copy-pastes (AutoCAD's PURGE + /// (#1482). A drawing that once came from `MicroStation` can carry hundreds of thousands of + /// them, orphaned copies piled up by earlier DGN imports and copy-pastes (`AutoCAD`'s PURGE /// removes them as orphaned data): in a real 83 MB site plan they were 888,483 of 889,053 /// components and made its DXF rendition 1.2 GB, over [`Limits::DEFAULT`], so the file /// didn't open at all. No definition that was read refers to them (a definition the reader @@ -838,7 +855,7 @@ mod native { for (name, code, value) in unwritten_header_vars(h) { let written = section.windows(name.len() + 2).any(|w| w.starts_with(name.as_bytes()) && w.ends_with(b"\r\n")); if !written && value.parse::().is_ok_and(f64::is_finite) { - add.push_str(&format!(" 9\r\n{name}\r\n{code:>3}\r\n{value}\r\n")); + let _ = write!(add, " 9\r\n{name}\r\n{code:>3}\r\n{value}\r\n"); } } dxf.splice(end..end, add.into_bytes()); @@ -870,14 +887,22 @@ mod native { /// Oldest DWG version written; older DXF input is written as this version. const MIN_DWG_VERSION: acadrust::DxfVersion = acadrust::DxfVersion::AC1018; - /// Convert DXF bytes into a DWG file of the DXF's version, and at least AutoCAD 2004 + /// Convert DXF bytes into a DWG file of the DXF's version, and at least `AutoCAD` 2004 /// (AC1018). + /// + /// # Errors + /// + /// Returns an error when the DXF cannot be decoded or the DWG cannot be written, including oversized input. pub fn dxf_to_dwg(dxf: &[u8]) -> Result, String> { convert(dxf, None, None).map(|(dwg, _)| dwg) } /// Convert DXF bytes into a DWG file of `version`, a code from [`super::VERSIONS`] ("AC1032"), /// whatever version the DXF declares. + /// + /// # Errors + /// + /// Returns an error for an unsupported version, invalid DXF, oversized input, or a DWG write failure. pub fn dxf_to_dwg_as(dxf: &[u8], version: &str) -> Result, String> { dxf_to_dwg_noted(dxf, version).map(|(dwg, _)| dwg) } @@ -1003,7 +1028,7 @@ mod native { /// The current dimension style (`$DIMSTYLE`) by its name: the DXF gives the name, and the /// handle read with it needn't be the style's in the document, in which case the DWG writer - /// fell back to Standard and AutoCAD opened the drawing with Standard current (#1051). + /// fell back to Standard and `AutoCAD` opened the drawing with Standard current (#1051). fn current_dimstyle(doc: &mut acadrust::CadDocument) { let name = doc.header.current_dimstyle_name.clone(); if let Some(h) = doc.dim_styles.iter().find(|ds| ds.name.eq_ignore_ascii_case(&name)).map(|ds| ds.handle) { @@ -1013,7 +1038,7 @@ mod native { /// The current text style (`$TEXTSTYLE`) by its name, the same way as [`current_dimstyle`]: /// the handle read with the DXF needn't be the style's in the document, in which case the DWG - /// writer fell back to Standard and AutoCAD opened the drawing with Standard current (#1650). + /// writer fell back to Standard and `AutoCAD` opened the drawing with Standard current (#1650). fn current_text_style(doc: &mut acadrust::CadDocument) { let name = doc.header.current_text_style_name.clone(); if let Some(h) = doc.text_styles.iter().find(|ts| ts.name.eq_ignore_ascii_case(&name)).map(|ts| ts.handle) { @@ -1203,7 +1228,7 @@ mod native { }; let (classes, source) = guarded(AssertUnwindSafe(|| (dxf_class_names(&data), super::proxy_transport::source_classes(&data)))) .map_err(|_| "the DXF proxy class conversion failed".to_string())?; - let mut doc = std::panic::catch_unwind(move || acadrust::DxfReader::from_reader(Cursor::new(data)).and_then(|r| r.read())) + let mut doc = std::panic::catch_unwind(move || acadrust::DxfReader::from_reader(Cursor::new(data)).and_then(acadrust::DxfReader::read)) .map_err(|_| "the DXF→DWG conversion failed".to_string())? .map_err(|e| format!("DXF: {e}"))?; match version { @@ -1242,14 +1267,14 @@ mod native { current_layer(&mut doc); unwritten })) - .map_err(|_| "the DXF to DWG conversion failed on this file".to_string())?; + .map_err(|()| "the DXF to DWG conversion failed on this file".to_string())?; let dwg = std::panic::catch_unwind(AssertUnwindSafe(|| acadrust::DwgWriter::write_to_vec(&doc))) .map_err(|_| "the DWG writer failed on this drawing".to_string())? .map_err(|e| format!("DWG write: {e}"))?; Ok((dwg, unwritten)) } - /// AutoCAD's entry name for the drawing variables dictionary (the DXF Reference calls it the + /// `AutoCAD`'s entry name for the drawing variables dictionary (the DXF Reference calls it the /// ACDBVARIABLEDICTIONARY; dictionary keys are case-insensitive). const VARIABLE_DICTIONARY: &str = "AcDbVariableDictionary"; @@ -1265,11 +1290,11 @@ mod native { if valid.contains(&value) { value.to_string() } else { default.to_string() } } - /// Add the drawing variables dictionary AutoCAD always writes into the named object + /// Add the drawing variables dictionary `AutoCAD` always writes into the named object /// dictionary, with the variables it keeps there, when the drawing has none (#1813). /// Vectric Aspire 10.5 reads a 2004 (AC1018) DWG without it as empty ("No data was imported /// from the DXF / DWG file"), though it reads the same drawing as DXF — which has no such - /// dictionary either, as CADCraft's and ezdxf's writers don't make one. AutoCAD's AC1032 DWGs + /// dictionary either, as CADCraft's and ezdxf's writers don't make one. `AutoCAD`'s AC1032 DWGs /// read there without it, so newer output is left as it was, and a dictionary the drawing /// does have keeps its own entries and values. fn add_variable_dictionary(doc: &mut acadrust::CadDocument) { @@ -1318,7 +1343,7 @@ mod native { /// Entities of a layout other than the one in `*Paper_Space` are written owned by their block /// (entity mode 0, with its handle). acadrust writes every paper-space entity with mode 1, - /// "paper space", which AutoCAD reads as the `*Paper_Space` block's: everything drawn on a + /// "paper space", which `AutoCAD` reads as the `*Paper_Space` block's: everything drawn on a /// second layout landed on the first one, and the second stayed empty (#1057). fn other_layout_owners(doc: &mut acadrust::CadDocument) { let others: std::collections::HashSet = doc @@ -1541,11 +1566,11 @@ thread_local! { /// Panics when a test asked for `stage` to fail (see [`PANIC_IN`]); does nothing otherwise. #[cfg(not(no_dwg_bridge))] #[allow(clippy::panic)] -fn fail_in_test(_stage: &str) { +fn fail_in_test(stage: &str) { + #[cfg(not(test))] + let _ = stage; #[cfg(test)] - if PANIC_IN.with(|p| p.get()) == Some(_stage) { - panic!("test-forced panic in {_stage}"); - } + assert!(PANIC_IN.with(std::cell::Cell::get) != Some(stage), "test-forced panic in {stage}"); } /// The error for a DWG version this writer can't create. @@ -1623,6 +1648,8 @@ pub fn dxf_to_dwg_kept_noted(_dxf: &[u8], _version: &str, _kept: &str) -> Result #[cfg(test)] mod tests { + use std::fmt::Write as _; + use super::*; /// The DXF of an empty drawing, enough for the bridge to make a small DWG. @@ -1716,9 +1743,9 @@ mod tests { let mut s = String::from(" 0\nSECTION\n 2\nHEADER\n 9\n$ACADVER\n 1\nAC1032\n 0\nENDSEC\n 0\nSECTION\n 2\nENTITIES\n"); for (text, flags, sub, pts) in dims { s += " 0\nDIMENSION\n 8\n0\n100\nAcDbEntity\n100\nAcDbDimension\n 10\n0\n 20\n0\n 30\n0\n"; - s += &format!(" 11\n50\n 21\n-20\n 31\n0\n 70\n{flags}\n 1\n{text}\n 3\nStandard\n100\n{sub}\n"); - for (code, [x, y]) in pts.iter() { - s += &format!("{code:>3}\n{x}\n{:>3}\n{y}\n{:>3}\n0\n", code + 10, code + 20); + let _ = write!(s, " 11\n50\n 21\n-20\n 31\n0\n 70\n{flags}\n 1\n{text}\n 3\nStandard\n100\n{sub}\n"); + for (code, [x, y]) in *pts { + let _ = write!(s, "{code:>3}\n{x}\n{:>3}\n{y}\n{:>3}\n0\n", code + 10, code + 20); } } s + " 0\nENDSEC\n 0\nEOF\n" @@ -1787,13 +1814,17 @@ mod tests { .collect() } - /// Issue #1482: a DWG that once came from MicroStation can carry hundreds of thousands of DGN + /// Issue #1482: a DWG that once came from `MicroStation` can carry hundreds of thousands of DGN /// line style components no definition uses (a real 83 MB site plan: 888,483 of 889,053, a /// 1.2 GB DXF rendition over the limit, so it didn't open). They're left out with a note, and /// their dictionary entries with them; the definition and every component it reaches — /// through compound and point components, around a cycle — are kept. #[cfg(not(no_dwg_bridge))] #[test] + #[expect( + clippy::too_many_lines, + reason = "This integration test builds and verifies one complete DGN component graph; splitting the fixture would obscure reachability." + )] fn orphaned_dgn_line_style_components_are_left_out() { use acadrust::objects::*; let mut doc = acadrust::CadDocument::new(); From 80ed9665e7d27a98cc55e5526a1523327a380784 Mon Sep 17 00:00:00 2001 From: batistalucasv Date: Sat, 14 Nov 2026 15:25:54 -0300 Subject: [PATCH 010/150] craft-docx: preserve w:numberingChange without active list and raise w:original bound --- crates/docx/src/read/story/blocks.rs | 9 +++--- crates/docx/src/write/props.rs | 8 +++-- crates/docx/tests/it/fixtures.rs | 44 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/crates/docx/src/read/story/blocks.rs b/crates/docx/src/read/story/blocks.rs index 38c35a02b..878429454 100644 --- a/crates/docx/src/read/story/blocks.rs +++ b/crates/docx/src/read/story/blocks.rs @@ -249,10 +249,11 @@ impl Reader<'_> { .unwrap_or_default(); para.props.fmt_change = PropChange::boxed(self.revision(RevisionKind::Format, ch), old); } - if para.props.numbering.is_some() - && let Some(ch) = ppr.child("w:numPr").and_then(|n| n.child("w:numberingChange")) - { - let original = ch.attr("w:original").unwrap_or("").chars().take(256).collect(); + if let Some(ch) = ppr.child("w:numPr").and_then(|n| n.child("w:numberingChange")) { + // Multi-level numbering caches (`w:original`) routinely exceed 256 chars while staying + // bounded against hostile attributes. + const MAX_CHANGE_ORIGINAL: usize = 4096; + let original = ch.attr("w:original").unwrap_or("").chars().take(MAX_CHANGE_ORIGINAL).collect(); para.props.num_change = Some(Box::new(NumChange { rev: self.revision(RevisionKind::Format, ch), original })); } if let Some(s) = ppr.child("w:sectPr") { diff --git a/crates/docx/src/write/props.rs b/crates/docx/src/write/props.rs index 0a64fc20a..0b92ce191 100644 --- a/crates/docx/src/write/props.rs +++ b/crates/docx/src/write/props.rs @@ -261,10 +261,12 @@ pub fn ppr_inner(w: &mut W, p: &ParaProps, framed: bool, num_change: Option<&Cha toggle(w, "w:pageBreakBefore", p.page_break_before); write_frame(w, p, framed); toggle(w, "w:widowControl", p.widow_control); - if let Some(nr) = p.numbering { + if p.numbering.is_some() || num_change.is_some() { w.open("w:numPr", &[]); - w.val("w:ilvl", &n(nr.level.min(8) as i64)); - w.val("w:numId", &n(nr.num as i64)); + if let Some(nr) = p.numbering { + w.val("w:ilvl", &n(nr.level.min(8) as i64)); + w.val("w:numId", &n(nr.num as i64)); + } if let Some(a) = num_change { let refs: Vec<(&str, &str)> = a.iter().map(|(k, v)| (*k, v.as_str())).collect(); w.empty("w:numberingChange", &refs); diff --git a/crates/docx/tests/it/fixtures.rs b/crates/docx/tests/it/fixtures.rs index 868e8cbb0..348d6239e 100644 --- a/crates/docx/tests/it/fixtures.rs +++ b/crates/docx/tests/it/fixtures.rs @@ -2197,6 +2197,50 @@ fn formatting_revisions_round_trip() { assert_eq!(again.revisions, d.revisions); } +#[test] +fn numbering_change_without_active_list_roundtrips() { + let body = r#"Removed"#; + let d = read_body(body); + let p = paras(&d); + assert_eq!(p.len(), 1); + assert!(p[0].props.numbering.is_none()); + let nc = p[0].props.num_change.as_deref().expect("numberingChange read back"); + assert_eq!(nc.original, "%1:1:0:."); + + let bytes = craft_docx::write(&d).unwrap(); + let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); + let mut xml = String::new(); + std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); + assert!(xml.contains(r#""#), "{xml}"); + + let again = craft_docx::read(&bytes).unwrap(); + let ap = paras(&again); + assert!(ap[0].props.numbering.is_none()); + let anc = ap[0].props.num_change.as_deref().expect("numberingChange preserved after save"); + assert_eq!(anc.original, "%1:1:0:."); +} + +#[test] +fn numbering_change_original_preserves_long_values_within_bound() { + let long_orig = "%1:1:0:.".repeat(64); // 512 chars, well past the old 256-char cap + assert!(long_orig.len() > 256 && long_orig.len() < 4096); + let over_cap = "x".repeat(5000); + let body = format!( + r#"Long +Bounded"# + ); + let d = read_body(&body); + let p = paras(&d); + assert_eq!(p[0].props.num_change.as_deref().map(|n| n.original.as_str()), Some(long_orig.as_str())); + assert_eq!(p[1].props.num_change.as_deref().map(|n| n.original.chars().count()), Some(4096)); + + let bytes = craft_docx::write(&d).unwrap(); + let again = craft_docx::read(&bytes).unwrap(); + let ap = paras(&again); + assert_eq!(ap[0].props.num_change.as_deref().map(|n| n.original.as_str()), Some(long_orig.as_str())); +} + /// A package with `styles`, a theme (accent1 4472C4, accent2 ED7D31, major font "Major Face") and, /// when given, `settings`. fn themed_docx(body: &str, styles: &str, settings: Option<&str>) -> Document { From 376fa26e4c4349e95363abf3d21bc76e8ee4d86e Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 14 Nov 2026 22:01:59 +0100 Subject: [PATCH 011/150] rtf: read paragraph direction from stylesheet entries A style's \rtlpar/\ltrpar (and \rtldir/\ltrdir) is stored with the style and found through \sbasedon. A paragraph in the style gets it unless it sets its own direction; \pard drops the direct direction and the style's stays. Co-Authored-By: Claude Sonnet 5.5 --- crates/rtf/src/doc/state.rs | 4 ++-- crates/rtf/src/doc/styles.rs | 6 +++++- crates/rtf/src/doc/tests.rs | 12 ++++++++++++ crates/rtf/src/doc/word.rs | 6 ++++-- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/crates/rtf/src/doc/state.rs b/crates/rtf/src/doc/state.rs index 1f5d8d2e6..c27758d05 100644 --- a/crates/rtf/src/doc/state.rs +++ b/crates/rtf/src/doc/state.rs @@ -149,7 +149,7 @@ pub(super) struct St { // Paragraph properties. pub(super) align: Option, /// `\rtlpar`: the paragraph reads right to left. - pub(super) rtldir: bool, + pub(super) rtldir: Option, pub(super) style: i32, pub(super) intbl: bool, pub(super) ls: Option, @@ -187,7 +187,7 @@ impl Default for St { uc: 1, hidden: false, align: None, - rtldir: false, + rtldir: None, style: 0, intbl: false, ls: None, diff --git a/crates/rtf/src/doc/styles.rs b/crates/rtf/src/doc/styles.rs index dd42f3b87..e8eb9904c 100644 --- a/crates/rtf/src/doc/styles.rs +++ b/crates/rtf/src/doc/styles.rs @@ -30,6 +30,7 @@ impl Reader { out.italic = out.italic.or(d.italic); out.size = out.size.or(d.size); out.font = out.font.or(d.font); + out.rtl = out.rtl.or(d.rtl); cur = d.based_on; } out @@ -87,7 +88,8 @@ impl Reader { pub(super) fn para_props(&self, st: &St) -> Para { let mut p = Para::new(self.para_kind(st)); p.align = st.align; - p.rtl = st.rtldir; + // Direct `\rtlpar`/`\ltrpar` wins over the style's direction. + p.rtl = st.rtldir.or_else(|| self.file_style(st.style).rtl).unwrap_or(false); p.page_break = st.pagebb; p.spacing = spacing(st); p.borders = st.borders.any_visible().then_some(st.borders); @@ -136,6 +138,8 @@ pub(super) struct StyleDef { pub(super) size: Option, /// A `\fonttbl` index, named when it's used (the table may follow). pub(super) font: Option, + /// `\rtlpar` (`Some(true)`) or `\ltrpar`: the paragraph direction the style sets. + pub(super) rtl: Option, } /// Clear from `f` the formatting a paragraph of kind `kind` gets from its style, whose diff --git a/crates/rtf/src/doc/tests.rs b/crates/rtf/src/doc/tests.rs index 9274104ab..30fc8127c 100644 --- a/crates/rtf/src/doc/tests.rs +++ b/crates/rtf/src/doc/tests.rs @@ -68,6 +68,18 @@ fn rtl_paragraphs_read_and_write_their_direction() { assert_eq!(bidi(&import(out.as_bytes()).unwrap()), [true, false, false]); } +#[test] +fn rtl_paragraph_styles_carry_their_direction() { + // Word often writes the direction in the style: `{\s1\rtlpar RTL;}` then `\pard\s1`. + let rtf = br#"{\rtf1\ansi{\stylesheet{\s0 Normal;}{\s1\rtlpar RTL;}{\s2\sbasedon1 Child;}{\s3\sbasedon1\ltrpar Back;}} +\pard\s1 A\par\pard\s2 B\par\pard\s3 C\par\pard\s1\ltrpar D\par\pard\s0\rtlpar E\par\pard F\par}"#; + let flow = parse(rtf).unwrap(); + let rtl: Vec = flow.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p.rtl) } else { None }).collect(); + // A: style. B: inherited from the base style. C: the style overrides its base. D: direct + // \ltrpar beats the style. E: direct \rtlpar on Normal. F: \pard drops direct formatting. + assert_eq!(rtl, [true, true, false, false, true, false]); +} + #[test] fn stylesheet_names_take_unicode_escapes() { // \u116?=t \u105?=i \u116?=t \u108?=l \u101?=e — how Word writes a non-Latin style name. diff --git a/crates/rtf/src/doc/word.rs b/crates/rtf/src/doc/word.rs index bf163db0c..0a346ef93 100644 --- a/crates/rtf/src/doc/word.rs +++ b/crates/rtf/src/doc/word.rs @@ -173,8 +173,8 @@ impl Reader { "qd" => self.st.align = Some(Align::Distribute), // `\rtldir`/`\ltrdir` are not paragraph words (they set a table's or section's // direction in RTF); some writers used them for paragraphs, so they are read too. - "rtlpar" | "rtldir" => self.st.rtldir = true, - "ltrpar" | "ltrdir" => self.st.rtldir = false, + "rtlpar" | "rtldir" => self.st.rtldir = Some(true), + "ltrpar" | "ltrdir" => self.st.rtldir = Some(false), "s" => self.st.style = pv, "outlinelevel" => self.st.outline = Some(pv.clamp(0, 9) as u8), "pagebb" => self.st.pagebb = on, @@ -421,6 +421,8 @@ impl Reader { "i" => def.italic = Some(on), "fs" => def.size = (value > 0).then(|| (value as f32 / 2.0).clamp(1.0, 1638.0)), "f" => def.font = Some(value), + "rtlpar" | "rtldir" => def.rtl = Some(true), + "ltrpar" | "ltrdir" => def.rtl = Some(false), _ => {} } true From d869560911f5e14914314ebaddd97b8acfa87805 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 14 Nov 2026 22:01:59 +0100 Subject: [PATCH 012/150] rtf: keep direction words in old paragraph formatting \rtlpar, \ltrpar, \rtldir and \ltrdir are paragraph formatting words, so \oldpprops holds them and a tracked direction change keeps the old direction. Co-Authored-By: Claude Sonnet 5.5 --- crates/rtf/src/doc/styles.rs | 4 ++++ crates/rtf/src/doc/tests.rs | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/crates/rtf/src/doc/styles.rs b/crates/rtf/src/doc/styles.rs index e8eb9904c..6047bf3c4 100644 --- a/crates/rtf/src/doc/styles.rs +++ b/crates/rtf/src/doc/styles.rs @@ -208,6 +208,10 @@ pub(super) fn is_format_word(w: &str) -> bool { | "s" | "outlinelevel" | "pagebb" + | "rtlpar" + | "ltrpar" + | "rtldir" + | "ltrdir" | "intbl" | "ls" | "ilvl" diff --git a/crates/rtf/src/doc/tests.rs b/crates/rtf/src/doc/tests.rs index 30fc8127c..8ba6294ae 100644 --- a/crates/rtf/src/doc/tests.rs +++ b/crates/rtf/src/doc/tests.rs @@ -80,6 +80,20 @@ fn rtl_paragraph_styles_carry_their_direction() { assert_eq!(rtl, [true, true, false, false, true, false]); } +#[test] +fn oldpprops_keep_the_old_paragraph_direction() { + // A tracked change of direction: now LTR, before RTL (`\rtlpar` in the old properties). + let rtf = br"{\rtf1\ansi{\*\revtbl {Unknown;}{Alice;}} +\pard\ltrpar\prauth1\prdate-2014675576{\*\oldpprops \pard \rtlpar}A\par}"; + let flow = parse(rtf).unwrap(); + let Some(FBlock::Para(p)) = flow.blocks.first() else { panic!("no paragraph") }; + assert!(!p.rtl); + assert!(p.fmt_change.as_deref().expect("paragraph change").old.rtl, "old direction is RTL"); + let doc = import(rtf).unwrap(); + let c = doc_paras(&doc)[0].props.fmt_change.as_deref().expect("change"); + assert_eq!(c.old.bidi, Some(true)); +} + #[test] fn stylesheet_names_take_unicode_escapes() { // \u116?=t \u105?=i \u116?=t \u108?=l \u101?=e — how Word writes a non-Latin style name. From 537d61a60f84a92675b2391a6e912067c810d0b9 Mon Sep 17 00:00:00 2001 From: vangel76 <271991477+vangel76@users.noreply.github.com> Date: Sun, 15 Nov 2026 01:05:40 +0100 Subject: [PATCH 013/150] rtf: read \ql and \qr after \rtlpar as start and end The model's paragraph alignment is logical (start/end) in right-to-left paragraphs, but the reader mapped \ql/\qr straight to left/right. For a \rtlpar paragraph that mirrored the alignment compared to Word. Word reads a side word that follows \rtlpar as start/end, and one that comes before it as plain left/right (checked black-box by saving RTF variants to DOCX). So \rtlpar\qr is start (no explicit alignment), \rtlpar\ql is end, and \qr\rtlpar is end. The writer already puts the alignment before \rtlpar, so its output keeps its meaning and is unchanged. Co-Authored-By: Claude Sonnet 5.5 --- crates/rtf/src/doc/tests.rs | 43 +++++++++++++++++++++++++++++++++++++ crates/rtf/src/doc/word.rs | 16 ++++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/crates/rtf/src/doc/tests.rs b/crates/rtf/src/doc/tests.rs index 8ba6294ae..8313ad5b8 100644 --- a/crates/rtf/src/doc/tests.rs +++ b/crates/rtf/src/doc/tests.rs @@ -1886,3 +1886,46 @@ fn compound_borders_round_trip() { let back = import(export(&d).as_bytes()).unwrap(); assert_eq!(tops(&back), want); } + +fn para_aligns(flow: &Flow) -> Vec<(bool, Option)> { + flow.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some((p.rtl, p.align)) } else { None }).collect() +} + +#[test] +fn alignment_words_after_rtlpar_mean_start_and_end() { + // Word reads `\ql`/`\qr` that follow `\rtlpar` as start/end (the model's alignment is + // logical), so `\rtlpar\qr` sits at the right edge and `\rtlpar\ql` at the left edge. + // Words written before `\rtlpar` keep their plain meaning (checked in Word by saving to DOCX: + // `\qr\rtlpar` gives jc=right, i.e. end, in the bidi paragraph; `\ql\rtlpar` gives no jc). + let rtf = br#"{\rtf1\ansi\deff0{\fonttbl{\f0 Arial;}}\pard LTR one.\par \pard\rtlpar\qr A.\par \pard\rtlpar B.\par \pard\ltrpar Back.\par \pard\rtlpar\ql C.\par \pard\rtlpar\qc D.\par \pard\ql\rtlpar E.\par \pard\qr\rtlpar F.\par \pard\rtlpar\ltrpar\qr G.\par}"#; + let got = para_aligns(&parse(rtf).unwrap()); + assert_eq!( + got, + [ + (false, None), + (true, Some(Align::Left)), + (true, None), + (false, None), + (true, Some(Align::Right)), + (true, Some(Align::Center)), + (true, Some(Align::Left)), + (true, Some(Align::Right)), + (false, Some(Align::Right)), + ] + ); +} + +#[test] +fn alignment_survives_an_export_import_round_trip() { + let rtf = br#"{\rtf1\ansi\pard\ql L.\par \pard\qr R.\par \pard\qc C.\par \pard\rtlpar\qr start.\par \pard\rtlpar\ql end.\par \pard\rtlpar\qc mid.\par \pard\rtlpar plain.\par}"#; + let first = para_aligns(&parse(rtf).unwrap()); + let doc = import(rtf).unwrap(); + let out = export(&doc); + let second = para_aligns(&parse(out.as_bytes()).unwrap()); + let norm = |v: Vec<(bool, Option)>| v.into_iter().map(|(r, a)| (r, a.filter(|a| *a != Align::Left))).collect::>(); + assert_eq!(norm(first), norm(second.clone()), "{out}"); + // `\rtlpar\qr` is start (no explicit alignment in the model) and `\rtlpar\ql` is end. + assert_eq!(norm(second)[3..5], [(true, None), (true, Some(Align::Right))], "{out}"); + // The export writes the alignment before the direction, which Word reads as logical start/end. + assert!(out.contains("\\qr\\rtlpar"), "{out}"); +} diff --git a/crates/rtf/src/doc/word.rs b/crates/rtf/src/doc/word.rs index 0a346ef93..2eb86692f 100644 --- a/crates/rtf/src/doc/word.rs +++ b/crates/rtf/src/doc/word.rs @@ -3,6 +3,18 @@ use super::styles::{StyleDef, is_char_format_word, is_format_word}; use super::{Align, Fmt, Reader, VAlign, model}; impl Reader { + /// The logical alignment for a side word (`Left` for `\ql`, `Right` for `\qr`). The model's + /// alignment is logical (start/end), and Word reads a side word that comes after `\rtlpar` as + /// start/end: the sides swap in a paragraph already marked right to left. Before `\rtlpar` + /// the side keeps its plain meaning, which is why the writer puts the alignment first. + fn side_align(&self, side: Align) -> Align { + match (self.st.rtldir, side) { + (Some(true), Align::Left) => Align::Right, + (Some(true), Align::Right) => Align::Left, + _ => side, + } + } + pub(super) fn word(&mut self, w: &str, p: Option, starred: bool, first_in_group: bool) { // Every control word ends a multibyte run: the code page it is read in may change here // (`\f`, `\fcharset`, `\cpg`, `\ansicpg`, `\pard`), and `\par` must see the text before it. @@ -166,9 +178,9 @@ impl Reader { "cf" => self.st.fmt.color = self.colors.get(pv.max(0) as usize).copied().flatten(), "highlight" | "chcbpat" | "cb" => self.st.fmt.background = self.colors.get(pv.max(0) as usize).copied().flatten(), "v" => self.st.hidden = on, - "ql" => self.st.align = Some(Align::Left), + "ql" => self.st.align = Some(self.side_align(Align::Left)), "qc" => self.st.align = Some(Align::Center), - "qr" => self.st.align = Some(Align::Right), + "qr" => self.st.align = Some(self.side_align(Align::Right)), "qj" => self.st.align = Some(Align::Justify), "qd" => self.st.align = Some(Align::Distribute), // `\rtldir`/`\ltrdir` are not paragraph words (they set a table's or section's From 6767c7136297160cfca1460943b87f46a236cd8d Mon Sep 17 00:00:00 2001 From: vangel76 <271991477+vangel76@users.noreply.github.com> Date: Sun, 15 Nov 2026 02:26:31 +0100 Subject: [PATCH 014/150] Re-run CI: the Ubuntu gate timed out Co-Authored-By: Claude Sonnet 5.5 From ff61266d6337abc59285dd3a6369b23ef0b445a0 Mon Sep 17 00:00:00 2001 From: el-j Date: Sun, 15 Nov 2026 12:38:57 +0100 Subject: [PATCH 015/150] craft-audio: QOA (Quite OK Audio) decoder and encoder Adds the qoa module behind the qoa, qoa-decode and qoa-encode features: whole .qoa files, 16-bit, 1 to 8 channels. Streaming files are unsupported. An ffmpeg oracle test checks that ffmpeg decodes our files to exactly the samples our decoder returns. Closes #1541 --- crates/audio/Cargo.toml | 8 + crates/audio/src/lib.rs | 5 +- crates/audio/src/qoa.rs | 411 +++++++++++++++++++++++++++++++ crates/audio/tests/qoa_oracle.rs | 67 +++++ 4 files changed, 490 insertions(+), 1 deletion(-) create mode 100644 crates/audio/src/qoa.rs create mode 100644 crates/audio/tests/qoa_oracle.rs diff --git a/crates/audio/Cargo.toml b/crates/audio/Cargo.toml index c411a0357..645246130 100644 --- a/crates/audio/Cargo.toml +++ b/crates/audio/Cargo.toml @@ -38,6 +38,10 @@ aac-decode = ["craft-media/bitstream"] # The AAC encoder (`aac::Encoder`), and AAC in M4A through `encode` and `FileEncoder`. aac-encode = ["craft-media/bitstream", "craft-media/isobmff"] opus-decode = [] +# QOA (Quite OK Audio) files: decoder and encoder in the `qoa` module, each on its own. +qoa = ["qoa-decode", "qoa-encode"] +qoa-decode = [] +qoa-encode = [] opus-encode = [] ac3-decode = ["craft-media/bitstream"] mpa-decode = ["craft-media/bitstream"] @@ -84,6 +88,10 @@ required-features = ["decode", "lossless-encode", "peaks"] name = "codecs" required-features = ["decode", "encode"] +[[test]] +name = "qoa_oracle" +required-features = ["qoa"] + [[example]] name = "info" required-features = ["decode"] diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index 33dcdee3b..d825b4a78 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -27,7 +27,8 @@ //! - Codec modules for packet-level use, each with its own features: [`aac`] (`aac-decode`, //! `aac-encode`), [`alac`] (`alac-decode`, `alac-encode`), [`opus`] (`opus-decode`, and //! [`opus::encoder`] with `opus-encode`), [`ac3`] (`ac3-decode`), [`mpa`] (`mpa-decode`), [`mp3`] -//! (`mp3-decode`, `mp3-encode`), [`flac`] (`flac-encode`: the packet encoder). +//! (`mp3-decode`, `mp3-encode`), [`flac`] (`flac-encode`: the packet encoder), [`qoa`] +//! (`qoa-decode`, `qoa-encode`: whole QOA files). //! //! The shared types below build with no features. @@ -49,6 +50,8 @@ pub mod opus; mod pcm; #[cfg(feature = "peaks")] pub mod peaks; +#[cfg(any(feature = "qoa-decode", feature = "qoa-encode"))] +pub mod qoa; #[cfg(feature = "decode-native")] mod read; #[cfg(any(feature = "aac-decode", feature = "alac-decode", feature = "opus-decode", feature = "ac3-decode", feature = "mpa-decode"))] diff --git a/crates/audio/src/qoa.rs b/crates/audio/src/qoa.rs new file mode 100644 index 000000000..a04430137 --- /dev/null +++ b/crates/audio/src/qoa.rs @@ -0,0 +1,411 @@ +//! QOA ("Quite OK Audio"): a decoder (feature `qoa-decode`) and an encoder (feature `qoa-encode`) +//! for whole `.qoa` files, 16-bit, 1 to 8 channels. +//! +//! Written from the published QOA specification (qoaformat.org). A file is an 8-byte header (the +//! magic `qoaf` and the number of samples per channel) followed by frames of up to 5120 samples +//! per channel. Each frame starts with its channel count, sample rate, sample count and size, then +//! the state of one 4-tap sign-sign LMS predictor per channel, then 64-bit slices of 20 samples: +//! a 4-bit scale factor and twenty 3-bit quantised residuals, the channels interleaved slice by +//! slice. +//! +//! Streaming files (a sample count of 0 in the header) are not supported. +//! Samples are `i16`, one `Vec` per channel. + +#[cfg(feature = "qoa-decode")] +const MAX_FRAME_SAMPLES: usize = 5120; +const SLICE_SAMPLES: usize = 20; +const LMS_LEN: usize = 4; +const MAGIC: &[u8; 4] = b"qoaf"; +/// The most channels a file may have. +pub const MAX_CHANNELS: usize = 8; + +/// `dequant[scale factor][quantised residual]`: `round(round((sf + 1)^2.75) * [0.75, -0.75, 2.5, +/// -2.5, 4.5, -4.5, 7, -7])`. +const DEQUANT: [[i32; 8]; 16] = [ + [1, -1, 3, -3, 5, -5, 7, -7], + [5, -5, 18, -18, 32, -32, 49, -49], + [16, -16, 53, -53, 95, -95, 147, -147], + [34, -34, 113, -113, 203, -203, 315, -315], + [63, -63, 210, -210, 378, -378, 588, -588], + [104, -104, 345, -345, 621, -621, 966, -966], + [158, -158, 528, -528, 950, -950, 1477, -1477], + [228, -228, 760, -760, 1368, -1368, 2128, -2128], + [316, -316, 1053, -1053, 1895, -1895, 2947, -2947], + [422, -422, 1405, -1405, 2529, -2529, 3934, -3934], + [548, -548, 1828, -1828, 3290, -3290, 5117, -5117], + [696, -696, 2320, -2320, 4176, -4176, 6496, -6496], + [868, -868, 2893, -2893, 5207, -5207, 8099, -8099], + [1064, -1064, 3548, -3548, 6386, -6386, 9933, -9933], + [1286, -1286, 4288, -4288, 7718, -7718, 12005, -12005], + [1536, -1536, 5120, -5120, 9216, -9216, 14336, -14336], +]; + +/// What went wrong reading or writing a QOA file. +#[non_exhaustive] +#[derive(Debug, thiserror::Error, PartialEq, Eq)] +pub enum Error { + /// The input is not a QOA file, or is damaged or truncated. + #[error("malformed QOA: {0}")] + Malformed(&'static str), + /// A valid file this module does not handle (a streaming file). + #[error("unsupported QOA: {0}")] + Unsupported(&'static str), + /// The samples or settings given to the encoder cannot be written as QOA. + #[error("QOA cannot encode: {0}")] + InvalidInput(&'static str), +} + +/// A decoded file. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Audio { + /// Sample rate in Hz (1 to 16 777 215). + pub sample_rate: u32, + /// One `Vec` of samples per channel, all the same length. + pub channels: Vec>, +} + +/// The predictor: the last four samples and four weights. +#[derive(Clone, Copy)] +struct Lms { + history: [i32; LMS_LEN], + weights: [i32; LMS_LEN], +} + +impl Lms { + fn new() -> Self { + Lms { history: [0; LMS_LEN], weights: [0, 0, -(1 << 13), 1 << 14] } + } + + fn predict(&self) -> i32 { + let mut p = 0i32; + for i in 0..LMS_LEN { + p = p.wrapping_add(self.weights[i].wrapping_mul(self.history[i])); + } + p >> 13 + } + + fn update(&mut self, sample: i32, residual: i32) { + let delta = residual >> 4; + for i in 0..LMS_LEN { + self.weights[i] += if self.history[i] < 0 { -delta } else { delta }; + } + self.history.copy_within(1.., 0); + self.history[LMS_LEN - 1] = sample; + } +} + +fn clamp16(v: i32) -> i32 { + v.clamp(-32768, 32767) +} + +#[cfg(feature = "qoa-decode")] +mod decode { + use super::{Audio, DEQUANT, Error, LMS_LEN, Lms, MAGIC, MAX_CHANNELS, MAX_FRAME_SAMPLES, SLICE_SAMPLES, clamp16}; + + fn be16(b: &[u8], i: usize) -> u16 { + u16::from_be_bytes([b[i], b[i + 1]]) + } + + /// Decode a whole `.qoa` file. + /// + /// The declared length is checked against the input before anything is allocated: a slice of + /// 8 bytes holds at most 20 samples, so a file cannot declare more samples than that. + pub fn decode(bytes: &[u8]) -> Result { + if bytes.len() < 8 || &bytes[..4] != MAGIC { + return Err(Error::Malformed("missing the qoaf magic")); + } + let total = u32::from_be_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]) as usize; + if total == 0 { + return Err(Error::Unsupported("streaming files (a sample count of 0)")); + } + if bytes.len() < 16 { + return Err(Error::Malformed("truncated frame header")); + } + let channels = usize::from(bytes[8]); + let sample_rate = u32::from_be_bytes([0, bytes[9], bytes[10], bytes[11]]); + if channels == 0 || channels > MAX_CHANNELS || sample_rate == 0 { + return Err(Error::Malformed("bad channel count or sample rate")); + } + // 8 bytes (20 samples) per channel at the very least, plus the frame headers. + let room = (bytes.len() - 8) / (8 * channels) * SLICE_SAMPLES; + if total > room { + return Err(Error::Malformed("the file is too short for its declared length")); + } + let mut out: Vec> = Vec::new(); + for _ in 0..channels { + let mut v = Vec::new(); + v.try_reserve_exact(total).map_err(|_| Error::Malformed("too many samples"))?; + out.push(v); + } + let mut pos = 8; + let mut done = 0usize; + while done < total { + let head = bytes.get(pos..pos + 8).ok_or(Error::Malformed("truncated frame header"))?; + let rate = u32::from_be_bytes([0, head[1], head[2], head[3]]); + if usize::from(head[0]) != channels || rate != sample_rate { + return Err(Error::Malformed("the channel count or sample rate changes between frames")); + } + let samples = usize::from(be16(head, 4)); + let size = usize::from(be16(head, 6)); + let slices = samples.div_ceil(SLICE_SAMPLES); + if samples == 0 || samples > MAX_FRAME_SAMPLES || samples > total - done { + return Err(Error::Malformed("bad frame sample count")); + } + if size != 8 + 16 * channels + 8 * slices * channels { + return Err(Error::Malformed("frame size does not match its sample count")); + } + let frame = bytes.get(pos..pos + size).ok_or(Error::Malformed("truncated frame"))?; + let mut lms = [Lms::new(); MAX_CHANNELS]; + for (c, state) in lms.iter_mut().take(channels).enumerate() { + let at = 8 + c * 16; + for i in 0..LMS_LEN { + state.history[i] = i32::from(be16(frame, at + 2 * i) as i16); + state.weights[i] = i32::from(be16(frame, at + 8 + 2 * i) as i16); + } + } + let mut at = 8 + 16 * channels; + for s in 0..slices { + let n = (samples - s * SLICE_SAMPLES).min(SLICE_SAMPLES); + for (c, state) in lms.iter_mut().take(channels).enumerate() { + let mut slice = u64::from_be_bytes([ + frame[at], + frame[at + 1], + frame[at + 2], + frame[at + 3], + frame[at + 4], + frame[at + 5], + frame[at + 6], + frame[at + 7], + ]); + at += 8; + let table = &DEQUANT[(slice >> 60) as usize & 15]; + for _ in 0..n { + let dequantised = table[(slice >> 57) as usize & 7]; + slice <<= 3; + let sample = clamp16(state.predict() + dequantised); + out[c].push(sample as i16); + state.update(sample, dequantised); + } + } + } + pos += size; + done += samples; + } + Ok(Audio { sample_rate, channels: out }) + } +} +#[cfg(feature = "qoa-decode")] +pub use decode::decode; + +#[cfg(feature = "qoa-encode")] +mod encode { + use super::{DEQUANT, Error, Lms, MAGIC, MAX_CHANNELS, SLICE_SAMPLES, clamp16}; + + /// The quantised residual (0..8) whose dequantised value is nearest `residual`. + fn quantise(table: &[i32; 8], residual: i32) -> usize { + let mut best = 0; + let mut best_err = i64::MAX; + for (q, &d) in table.iter().enumerate() { + let e = i64::from(residual - d).pow(2); + if e < best_err { + best = q; + best_err = e; + } + } + best + } + + /// Encode one slice of one channel with every scale factor and keep the one with the least + /// squared error. Returns the 64-bit slice; `lms` is advanced to the chosen result. + fn encode_slice(lms: &mut Lms, samples: &[i16]) -> u64 { + let mut best: Option<(i64, u64, Lms)> = None; + for (sf, table) in DEQUANT.iter().enumerate() { + let mut trial = *lms; + let mut slice = sf as u64; + let mut err = 0i64; + for k in 0..SLICE_SAMPLES { + let mut q = 0; + if let Some(&s) = samples.get(k) { + let predicted = trial.predict(); + q = quantise(table, i32::from(s) - predicted); + let sample = clamp16(predicted + table[q]); + err += i64::from(i32::from(s) - sample).pow(2); + trial.update(sample, table[q]); + } + slice = slice << 3 | q as u64; + } + // 4 + 60 bits are in `slice`; the scale factor belongs on top. + let slice = slice & ((1 << 60) - 1) | (sf as u64) << 60; + if best.as_ref().is_none_or(|b| err < b.0) { + best = Some((err, slice, trial)); + } + } + match best { + Some((_, slice, state)) => { + *lms = state; + slice + } + None => 0, + } + } + + /// Encode `channels` (one `Vec` of samples per channel, all the same length) as a `.qoa` file. + pub fn encode(sample_rate: u32, channels: &[Vec]) -> Result, Error> { + let n = channels.len(); + if n == 0 || n > MAX_CHANNELS { + return Err(Error::InvalidInput("QOA stores 1 to 8 channels")); + } + if sample_rate == 0 || sample_rate > 0x00ff_ffff { + return Err(Error::InvalidInput("the sample rate must be 1 to 16777215 Hz")); + } + let total = channels[0].len(); + if channels.iter().any(|c| c.len() != total) { + return Err(Error::InvalidInput("the channels differ in length")); + } + let total32 = u32::try_from(total).map_err(|_| Error::InvalidInput("too many samples"))?; + if total == 0 { + return Err(Error::InvalidInput("QOA cannot store an empty file")); + } + let mut out = Vec::with_capacity(8 + total * n / 2); + out.extend_from_slice(MAGIC); + out.extend_from_slice(&total32.to_be_bytes()); + let mut lms = [Lms::new(); MAX_CHANNELS]; + let mut start = 0; + while start < total { + let len = (total - start).min(5120); + let slices = len.div_ceil(SLICE_SAMPLES); + let size = 8 + 16 * n + 8 * slices * n; + out.push(n as u8); + out.extend_from_slice(&sample_rate.to_be_bytes()[1..]); + out.extend_from_slice(&(len as u16).to_be_bytes()); + out.extend_from_slice(&(size as u16).to_be_bytes()); + // The state a frame starts from is written as 16-bit values, so the predictor + // continues from exactly what a decoder reads. + for state in lms.iter_mut().take(n) { + for v in state.history.iter_mut().chain(state.weights.iter_mut()) { + *v = i32::from(*v as i16); + out.extend_from_slice(&(*v as i16).to_be_bytes()); + } + } + for s in 0..slices { + let from = start + s * SLICE_SAMPLES; + let to = (from + SLICE_SAMPLES).min(start + len); + for (c, state) in lms.iter_mut().take(n).enumerate() { + out.extend_from_slice(&encode_slice(state, &channels[c][from..to]).to_be_bytes()); + } + } + start += len; + } + Ok(out) + } +} +#[cfg(feature = "qoa-encode")] +pub use encode::encode; + +#[cfg(all(test, feature = "qoa-decode", feature = "qoa-encode"))] +mod tests { + use super::*; + + fn tone(frames: usize, ch: usize) -> Vec> { + (0..ch) + .map(|c| (0..frames).map(|i| (((i as f64) * (0.03 + 0.01 * c as f64)).sin() * 12000.0 + ((i * 7919) % 200) as f64) as i16).collect()) + .collect() + } + + fn snr(a: &[i16], b: &[i16]) -> f64 { + let (mut s, mut e) = (0f64, 0f64); + for (&x, &y) in a.iter().zip(b) { + s += f64::from(x).powi(2); + e += (f64::from(x) - f64::from(y)).powi(2); + } + 10.0 * (s / e.max(1.0)).log10() + } + + #[test] + fn dequant_table_matches_its_formula() { + for (sf, row) in DEQUANT.iter().enumerate() { + let scale = ((sf as f64 + 1.0).powf(2.75)).round(); + for (q, base) in [0.75, -0.75, 2.5, -2.5, 4.5, -4.5, 7.0, -7.0].into_iter().enumerate() { + assert_eq!(f64::from(row[q]), (scale * base).round(), "sf {sf} q {q}"); + } + } + } + + #[test] + fn round_trips_with_a_good_signal_to_noise_ratio() { + for (frames, ch) in [(1, 1), (19, 2), (20, 1), (21, 2), (5120, 1), (5121, 2), (12_345, 8)] { + let src = tone(frames, ch); + let bytes = encode(44_100, &src).unwrap(); + let back = decode(&bytes).unwrap(); + assert_eq!((back.sample_rate, back.channels.len(), back.channels[0].len()), (44_100, ch, frames)); + if frames > 1000 { + for (c, (a, b)) in src.iter().zip(&back.channels).enumerate() { + assert!(snr(a, b) > 25.0, "{frames}x{ch} channel {c}"); + } + } + } + } + + #[test] + fn silence_and_full_scale_extremes_survive() { + let silent = vec![vec![0i16; 3000]]; + // No residual value is zero, so silence decodes to a tiny residue, never exactly 0. + let quiet = decode(&encode(8000, &silent).unwrap()).unwrap(); + assert!(quiet.channels[0].iter().all(|&s| s.abs() <= 2), "silence stays within 2 LSB"); + let square: Vec = (0..4000).map(|i| if (i / 50) % 2 == 0 { i16::MAX } else { i16::MIN }).collect(); + let back = decode(&encode(48_000, std::slice::from_ref(&square)).unwrap()).unwrap(); + assert_eq!(back.channels[0].len(), square.len()); + } + + #[test] + fn the_file_has_the_documented_layout() { + let bytes = encode(48_000, &tone(100, 2)).unwrap(); + assert_eq!(&bytes[..4], b"qoaf"); + assert_eq!(u32::from_be_bytes(bytes[4..8].try_into().unwrap()), 100); + assert_eq!(bytes[8], 2); + assert_eq!(&bytes[9..12], &48_000u32.to_be_bytes()[1..]); + assert_eq!(u16::from_be_bytes([bytes[12], bytes[13]]), 100); + // 8 header + 2 channels * 16 state + 5 slices * 2 channels * 8 + assert_eq!(usize::from(u16::from_be_bytes([bytes[14], bytes[15]])), 8 + 32 + 80); + assert_eq!(bytes.len(), 8 + 8 + 32 + 80); + // The first frame starts from the specified predictor state. + assert_eq!(&bytes[16 + 8..16 + 16], &[0, 0, 0, 0, 0xe0, 0, 0x40, 0]); + } + + #[test] + fn invalid_input_to_the_encoder_is_refused() { + assert!(encode(44_100, &[]).is_err()); + assert!(encode(0, &[vec![0; 10]]).is_err()); + assert!(encode(1 << 24, &[vec![0; 10]]).is_err()); + assert!(encode(44_100, &[vec![0; 10], vec![0; 9]]).is_err()); + assert!(encode(44_100, &[vec![]]).is_err()); + assert!(encode(44_100, &vec![vec![0; 10]; 9]).is_err()); + } + + #[test] + fn hostile_input_is_an_error_not_a_panic() { + let good = encode(44_100, &tone(6000, 2)).unwrap(); + for len in 0..good.len().min(600) { + let _ = decode(&good[..len]); + } + assert!(decode(&good[..good.len() - 1]).is_err()); + // A header that declares more samples than the bytes can hold. + let mut huge = good.clone(); + huge[4..8].copy_from_slice(&u32::MAX.to_be_bytes()); + assert!(decode(&huge).is_err()); + // A streaming file, and a frame whose size field lies. + let mut streaming = good.clone(); + streaming[4..8].fill(0); + assert_eq!(decode(&streaming), Err(Error::Unsupported("streaming files (a sample count of 0)"))); + let mut lying = good.clone(); + lying[14] ^= 1; + assert!(decode(&lying).is_err()); + let mut zero_rate = good.clone(); + zero_rate[9..12].fill(0); + assert!(decode(&zero_rate).is_err()); + let mut changed = good; + let second = 8 + usize::from(u16::from_be_bytes([changed[14], changed[15]])); + changed[second] = 1; + assert!(decode(&changed).is_err()); + } +} diff --git a/crates/audio/tests/qoa_oracle.rs b/crates/audio/tests/qoa_oracle.rs new file mode 100644 index 000000000..cf0ed8c37 --- /dev/null +++ b/crates/audio/tests/qoa_oracle.rs @@ -0,0 +1,67 @@ +//! QOA against ffmpeg's decoder as the reference (oracle; skipped when ffmpeg is absent, a failure +//! with `CRAFT_MEDIA_REQUIRE_ORACLES=1`): ffmpeg decodes the files our encoder writes to exactly +//! the samples our decoder returns for them, over several sizes (partial slices, partial frames, +//! several frames), channel counts and rates. + +use craft_audio::qoa::{decode, encode}; +use std::process::Command; + +fn signal(channels: usize, frames: usize) -> Vec> { + let mut seed = 0x2545_F491u32; + (0..channels) + .map(|c| { + (0..frames) + .map(|i| { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + let tone = ((i as f64) * (0.02 + 0.013 * c as f64)).sin() * 9000.0; + // Tones, noise bursts, silence and full-scale peaks. + match (i / 700) % 4 { + 0 => tone as i16, + 1 => (tone + f64::from(seed % 6000) - 3000.0) as i16, + 2 => 0, + _ => { + if i % 2 == 0 { + i16::MAX + } else { + i16::MIN + } + } + } + }) + .collect() + }) + .collect() +} + +#[test] +fn ffmpeg_decodes_our_files_to_the_same_samples() { + let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("qoa"); + for (rate, channels, frames) in + [(44_100u32, 1usize, 1usize), (8_000, 1, 19), (48_000, 2, 5_121), (22_050, 2, 12_345), (96_000, 1, 20_000), (44_100, 4, 6_000)] + { + let src = signal(channels, frames); + let bytes = encode(rate, &src).expect("encode"); + let ours = decode(&bytes).expect("decode"); + let file = dir.join(format!("ours_{rate}_{channels}_{frames}.qoa")); + std::fs::write(&file, &bytes).expect("write fixture"); + let out = Command::new(&ffmpeg) + .args(["-v", "error", "-y", "-i"]) + .arg(&file) + .args(["-f", "s16le", "-acodec", "pcm_s16le", "-"]) + .output() + .expect("run ffmpeg"); + assert!(out.status.success(), "ffmpeg failed on {}: {}", file.display(), String::from_utf8_lossy(&out.stderr)); + let theirs: Vec = out.stdout.as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect(); + let mut interleaved = Vec::with_capacity(frames * channels); + for i in 0..frames { + for c in 0..channels { + interleaved.push(ours.channels[c][i]); + } + } + assert_eq!(theirs.len(), interleaved.len(), "{rate} Hz, {channels} ch, {frames} frames: sample count"); + assert!(theirs == interleaved, "{rate} Hz, {channels} ch, {frames} frames: samples differ"); + } +} From 3ce92b332db8fa1f8d33214d299e88096263c007 Mon Sep 17 00:00:00 2001 From: el-j Date: Sun, 15 Nov 2026 12:53:20 +0100 Subject: [PATCH 016/150] craft-image-codecs: JPEG-LS (ITU-T T.87) reader and writer Adds Format::JpegLs: lossless and near-lossless gray and RGB, 2 to 16 bits. Reads scans that code one component each; writes lossless 8- and 16-bit gray and RGB. Line- and sample-interleaved scans, mapping tables and colour transforms are reported as unsupported. Closes #1558 --- Cargo.lock | 1 + crates/image-codecs/Cargo.toml | 1 + crates/image-codecs/README.md | 1 + crates/image-codecs/src/codecs.rs | 1 + crates/image-codecs/src/codecs/jpegls.rs | 870 ++++++++++++++++++ crates/image-codecs/src/format.rs | 14 +- crates/image-codecs/src/lib.rs | 4 +- crates/image-codecs/tests/it/detect_caps.rs | 16 + crates/image-codecs/tests/it/jpegls_oracle.rs | 103 +++ crates/image-codecs/tests/it/main.rs | 1 + 10 files changed, 1009 insertions(+), 3 deletions(-) create mode 100644 crates/image-codecs/src/codecs/jpegls.rs create mode 100644 crates/image-codecs/tests/it/jpegls_oracle.rs diff --git a/Cargo.lock b/Cargo.lock index 9a600099d..55ab615b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1919,6 +1919,7 @@ dependencies = [ "avif-serialize", "craft-heif", "craft-jpeg", + "craft-media-testkit", "craft-metadata", "exr", "flate2", diff --git a/crates/image-codecs/Cargo.toml b/crates/image-codecs/Cargo.toml index dc920f14b..5281fe2cf 100644 --- a/crates/image-codecs/Cargo.toml +++ b/crates/image-codecs/Cargo.toml @@ -46,6 +46,7 @@ exr = { version = "1.73", default-features = false, features = ["rayon"] } proptest = { workspace = true } image = { version = "0.25", default-features = false, features = ["png", "jpeg", "tiff"] } sha2 = { version = "0.10", default-features = false, features = ["std"] } +craft-media-testkit = { workspace = true } [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] jxl-oxide = { version = "=0.12.6", default-features = false, features = ["rayon"] } diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index 6e9e11617..a67be77c5 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -56,6 +56,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | Cineon | yes | yes | U16 (10-bit code values read and written exactly through U16) | Gray, RGB | no | no | no | no | no | no | no | built in | | DICOM | yes | **no** | U8, U16 | Gray, RGB | no | no | no | no | no | no | n/a | built in | | Amiga IFF (ILBM/PBM) | yes | yes (24-bit ILBM) | U8 | RGB, RGBA | yes (1-bit mask plane on write) | no | no | no | no | no | no | built in | +| JPEG-LS (`.jls`) | yes (any NEAR; one scan per component) | yes (lossless) | U8, U16 | Gray, RGB | no | no | no | no | no | no | no | built in | | AVIF | **no** | only with feature `avif` | U8 | RGB, RGBA | yes | no | no | no | no | no | yes | `image` + `ravif` | | HEIF/HEIC | with feature `heif` | **no** | U8, U16 (10/12-bit decodes to U16) | RGB, RGBA | yes (auxiliary alpha) | yes (`colr` prof) | yes | yes | no | no | n/a | `heic-rs` | diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs index 0288ae22b..af91aa536 100644 --- a/crates/image-codecs/src/codecs.rs +++ b/crates/image-codecs/src/codecs.rs @@ -13,6 +13,7 @@ pub(crate) mod heif; pub(crate) mod iff; pub(crate) mod jp2; pub(crate) mod jpeg; +pub(crate) mod jpegls; pub(crate) mod jxl; pub(crate) mod pcx; pub(crate) mod png; diff --git a/crates/image-codecs/src/codecs/jpegls.rs b/crates/image-codecs/src/codecs/jpegls.rs new file mode 100644 index 000000000..b405e71ed --- /dev/null +++ b/crates/image-codecs/src/codecs/jpegls.rs @@ -0,0 +1,870 @@ +//! JPEG-LS (ITU-T T.87 | ISO/IEC 14495-1, "LOCO-I"): lossless and near-lossless gray and RGB +//! images, 2 to 16 bits per sample. +//! +//! Read: baseline JPEG-LS with the default or `LSE`-preset coding parameters, any `NEAR`, and +//! scans that code one component each (`ILV` 0), which is also how this module writes. Line- and +//! sample-interleaved scans, colour transforms and mapping tables are reported as unsupported. +//! Write: lossless (`NEAR` 0), 8 or 16 bits, gray or RGB, one scan per component. +//! +//! Samples of 9 to 15 bits are scaled to 16 bits on read (the top bits are repeated into the low +//! ones), so a decoded image always spans the full range of its sample type. + +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::format::Format; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::Limits; + +const F: Format = Format::JpegLs; +const SOF55: u8 = 0xF7; +const LSE: u8 = 0xF8; +const RESET_DEFAULT: i32 = 64; +const MIN_C: i32 = -128; +const MAX_C: i32 = 127; +/// `J[RUNindex]`: the log2 of the run length a run-index step codes. +const J: [u32; 32] = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 9, 10, 11, 12, 13, 14, 15]; +/// Context slots: the regular contexts (365 of the 405 slots the index below can reach), then the two +/// run-interruption contexts. +const RI_BASE: usize = 405; +const CONTEXTS: usize = RI_BASE + 2; + +fn bad(reason: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, reason) +} + +/// Whether `b` is a JPEG-LS file: an SOI followed by marker segments up to an SOF55 frame header. +pub(crate) fn looks_like_jpegls(b: &[u8]) -> bool { + if !b.starts_with(&[0xFF, 0xD8]) { + return false; + } + let mut pos = 2; + for _ in 0..64 { + let (Some(&0xFF), Some(&marker)) = (b.get(pos), b.get(pos + 1)) else { return false }; + match marker { + SOF55 => return true, + 0xDA | 0xD9 => return false, + 0xC0..=0xCF | 0xDB..=0xDF => return false, + 0x01 | 0xD0..=0xD7 | 0xFF => pos += if marker == 0xFF { 1 } else { 2 }, + _ => { + let Some(len) = b.get(pos + 2..pos + 4).map(|l| usize::from(u16::from_be_bytes([l[0], l[1]]))) else { return false }; + pos += 2 + len; + } + } + } + false +} + +/// The coding parameters of one scan, derived as T.87 section C.2 specifies. +#[derive(Clone, Copy)] +struct Params { + maxval: i32, + near: i32, + range: i32, + qbpp: u32, + limit: u32, + t1: i32, + t2: i32, + t3: i32, + reset: i32, +} + +fn ceil_log2(v: i32) -> u32 { + let mut n = 0; + while (1i64 << n) < i64::from(v) { + n += 1; + } + n +} + +impl Params { + /// `preset` holds the `LSE` values (`maxval`, `t1`, `t2`, `t3`, `reset`), 0 where absent. + fn new(precision: u32, near: i32, preset: [i32; 5]) -> Result { + let default_max = (1i32 << precision) - 1; + let maxval = if preset[0] == 0 { default_max } else { preset[0] }; + if maxval < 1 || maxval > default_max || near < 0 || near > (maxval / 2).min(255) { + return Err(bad("invalid MAXVAL or NEAR")); + } + let clamp = |v: i32, lo: i32, hi: i32| if v > hi || v < lo { lo } else { v }; + let (d1, d2, d3) = if maxval >= 128 { + let factor = (maxval.min(4095) + 128) / 256; + let t1 = clamp(factor + 2 + 3 * near, near + 1, maxval); + let t2 = clamp(factor * 4 + 3 + 5 * near, t1, maxval); + let t3 = clamp(factor * 17 + 4 + 7 * near, t2, maxval); + (t1, t2, t3) + } else { + let factor = 256 / (maxval + 1); + let t1 = clamp((3 / factor + 3 * near).max(2), near + 1, maxval); + let t2 = clamp((7 / factor + 5 * near).max(3), t1, maxval); + let t3 = clamp((21 / factor + 7 * near).max(4), t2, maxval); + (t1, t2, t3) + }; + let t1 = if preset[1] == 0 { d1 } else { preset[1] }; + let t2 = if preset[2] == 0 { d2 } else { preset[2] }; + let t3 = if preset[3] == 0 { d3 } else { preset[3] }; + let reset = if preset[4] == 0 { RESET_DEFAULT } else { preset[4] }; + if !(near < t1 && t1 <= t2 && t2 <= t3 && t3 <= maxval) || !(3..=255).contains(&reset) { + return Err(bad("invalid threshold or RESET preset")); + } + let range = (maxval + 2 * near) / (2 * near + 1) + 1; + let bpp = ceil_log2(maxval + 1).max(2); + Ok(Params { maxval, near, range, qbpp: ceil_log2(range), limit: 2 * (bpp + bpp.max(8)), t1, t2, t3, reset }) + } +} + +/// Adaptive state of one scan: contexts, bias and the run index. +struct State { + a: [i32; CONTEXTS], + b: [i32; CONTEXTS], + c: [i32; CONTEXTS], + n: [i32; CONTEXTS], + nn: [i32; 2], + run_index: usize, +} + +impl State { + fn new(p: &Params) -> State { + let a0 = ((p.range + 32) / 64).max(2); + State { a: [a0; CONTEXTS], b: [0; CONTEXTS], c: [0; CONTEXTS], n: [1; CONTEXTS], nn: [0; 2], run_index: 0 } + } +} + +fn quantise_gradient(d: i32, p: &Params) -> i32 { + if d <= -p.t3 { + -4 + } else if d <= -p.t2 { + -3 + } else if d <= -p.t1 { + -2 + } else if d < -p.near { + -1 + } else if d <= p.near { + 0 + } else if d < p.t1 { + 1 + } else if d < p.t2 { + 2 + } else if d < p.t3 { + 3 + } else { + 4 + } +} + +fn predict(ra: i32, rb: i32, rc: i32) -> i32 { + if rc >= ra.max(rb) { + ra.min(rb) + } else if rc <= ra.min(rb) { + ra.max(rb) + } else { + ra + rb - rc + } +} + +/// The error quantised for `NEAR` and reduced into the modulo range, as the encoder codes it. +fn reduce(mut e: i32, p: &Params) -> i32 { + if p.near > 0 { + e = if e > 0 { (e + p.near) / (2 * p.near + 1) } else { -(p.near - e) / (2 * p.near + 1) }; + } + if e < 0 { + e += p.range; + } + if e >= (p.range + 1) / 2 { + e -= p.range; + } + e +} + +/// The reconstructed sample for a decoded (sign-adjusted) error. +fn reconstruct(px: i32, sign: i32, e: i32, p: &Params) -> i32 { + let mut r = px + sign * e * (2 * p.near + 1); + if r < -p.near { + r += p.range * (2 * p.near + 1); + } else if r > p.maxval + p.near { + r -= p.range * (2 * p.near + 1); + } + r.clamp(0, p.maxval) +} + +fn golomb_k(n: i32, a: i32) -> u32 { + let mut k = 0; + while (i64::from(n) << k) < i64::from(a) { + k += 1; + } + k +} + +/// Bits of the entropy-coded segment, with T.87's stuffing: after an `0xFF` byte the next byte +/// carries only 7 bits. +struct BitReader<'a> { + data: &'a [u8], + pos: usize, + cur: u32, + left: u32, + prev_ff: bool, +} + +impl<'a> BitReader<'a> { + fn new(data: &'a [u8]) -> Self { + BitReader { data, pos: 0, cur: 0, left: 0, prev_ff: false } + } + + fn bit(&mut self) -> Result { + if self.left == 0 { + let byte = *self.data.get(self.pos).ok_or_else(|| bad("the entropy-coded data ends early"))?; + self.pos += 1; + if self.prev_ff { + if byte & 0x80 != 0 { + return Err(bad("a marker inside entropy-coded data")); + } + self.cur = u32::from(byte); + self.left = 7; + } else { + self.cur = u32::from(byte); + self.left = 8; + } + self.prev_ff = byte == 0xFF; + } + self.left -= 1; + Ok((self.cur >> self.left) & 1) + } + + fn bits(&mut self, n: u32) -> Result { + let mut v = 0; + for _ in 0..n { + v = v << 1 | self.bit()?; + } + Ok(v) + } + + /// A limited-length Golomb code: the mapped error. + fn golomb(&mut self, k: u32, limit: u32, qbpp: u32) -> Result { + let max_zeros = limit.checked_sub(qbpp + 1).ok_or_else(|| bad("invalid LIMIT"))?; + let mut zeros = 0; + while self.bit()? == 0 { + zeros += 1; + if zeros > max_zeros { + return Err(bad("a Golomb code is longer than LIMIT")); + } + } + let v = if zeros < max_zeros { (zeros << k) + self.bits(k)? } else { self.bits(qbpp)? + 1 }; + i32::try_from(v).map_err(|_| bad("mapped error out of range")) + } +} + +/// Decode one component scan of `w` x `h` samples into `out` (row-major). +fn decode_scan(data: &[u8], w: usize, h: usize, p: &Params, out: &mut [u16]) -> Result<(), CodecError> { + let mut r = BitReader::new(data); + let mut st = State::new(p); + let mut prev = vec![0i32; w + 2]; + let mut cur = vec![0i32; w + 2]; + for y in 0..h { + // Ra at the first sample is Rb; the slot at index 0 is Rc there, left over from the line above. + cur[0] = prev[1]; + if y > 0 { + prev[w + 1] = prev[w]; + } + let mut x = 0usize; + while x < w { + let (ra, rb, rc, rd) = (cur[x], prev[x + 1], prev[x], if y == 0 { 0 } else { prev[x + 2] }); + let (q1, q2, q3) = (quantise_gradient(rd - rb, p), quantise_gradient(rb - rc, p), quantise_gradient(rc - ra, p)); + if q1 == 0 && q2 == 0 && q3 == 0 { + x = decode_run(&mut r, &mut st, p, &prev, &mut cur, ra, x, w)?; + continue; + } + let (sign, q1, q2, q3) = if q1 < 0 || (q1 == 0 && (q2 < 0 || (q2 == 0 && q3 < 0))) { (-1, -q1, -q2, -q3) } else { (1, q1, q2, q3) }; + let q = (q1 * 81 + (q2 + 4) * 9 + (q3 + 4)) as usize; + let k = golomb_k(st.n[q], st.a[q]); + let m = r.golomb(k, p.limit, p.qbpp)?; + let swap = p.near == 0 && k == 0 && 2 * st.b[q] <= -st.n[q]; + let e = if swap { + if m & 1 == 1 { (m - 1) >> 1 } else { -(m >> 1) - 1 } + } else if m & 1 == 0 { + m >> 1 + } else { + -((m + 1) >> 1) + }; + let px = (predict(ra, rb, rc) + sign * st.c[q]).clamp(0, p.maxval); + let rx = reconstruct(px, sign, e, p); + update_regular(&mut st, q, e, p); + cur[x + 1] = rx; + x += 1; + } + let row = &mut out[y * w..(y + 1) * w]; + for (o, &v) in row.iter_mut().zip(&cur[1..=w]) { + *o = v as u16; + } + // The line just decoded becomes the line above; its slot 0 is the Rc of the next line's first sample. + std::mem::swap(&mut prev, &mut cur); + } + Ok(()) +} + +fn update_regular(st: &mut State, q: usize, e: i32, p: &Params) { + st.b[q] += e * (2 * p.near + 1); + st.a[q] += e.abs(); + if st.n[q] == p.reset { + st.a[q] >>= 1; + st.b[q] = if st.b[q] >= 0 { st.b[q] >> 1 } else { -((1 - st.b[q]) >> 1) }; + st.n[q] >>= 1; + } + st.n[q] += 1; + if st.b[q] <= -st.n[q] { + if st.c[q] > MIN_C { + st.c[q] -= 1; + } + st.b[q] += st.n[q]; + if st.b[q] <= -st.n[q] { + st.b[q] = -st.n[q] + 1; + } + } else if st.b[q] > 0 { + if st.c[q] < MAX_C { + st.c[q] += 1; + } + st.b[q] -= st.n[q]; + if st.b[q] > 0 { + st.b[q] = 0; + } + } +} + +/// Run-interruption variables update shared by both directions. +fn update_run(st: &mut State, q: usize, e: i32, em: i32, ri_type: i32, p: &Params) { + let i = q - RI_BASE; + if e < 0 { + st.nn[i] += 1; + } + st.a[q] += (em + 1 - ri_type) >> 1; + if st.n[q] == p.reset { + st.a[q] >>= 1; + st.n[q] >>= 1; + st.nn[i] >>= 1; + } + st.n[q] += 1; +} + +/// The run-interruption Golomb parameter and the mapping flag of an error. +fn run_k(st: &State, q: usize, ri_type: i32) -> u32 { + let temp = if ri_type == 1 { st.a[q] + (st.n[q] >> 1) } else { st.a[q] }; + golomb_k(st.n[q], temp) +} + +#[allow(clippy::too_many_arguments)] +fn decode_run( + r: &mut BitReader, + st: &mut State, + p: &Params, + prev: &[i32], + cur: &mut [i32], + ra: i32, + x: usize, + w: usize, +) -> Result { + let remaining = w - x; + let mut run = 0usize; + while r.bit()? == 1 { + let step = 1usize << J[st.run_index]; + let count = step.min(remaining - run); + run += count; + if count == step && st.run_index < 31 { + st.run_index += 1; + } + if run == remaining { + break; + } + } + if run < remaining { + let rest = r.bits(J[st.run_index])? as usize; + run += rest; + if run > remaining { + return Err(bad("a run passes the end of the line")); + } + } + for v in &mut cur[x + 1..x + 1 + run] { + *v = ra; + } + if run == remaining { + return Ok(w); + } + // The sample that ended the run: its own Rb, the sample above it. + let rb = prev[x + run + 1]; + let ri_type = i32::from((ra - rb).abs() <= p.near); + let q = RI_BASE + ri_type as usize; + let k = run_k(st, q, ri_type); + let limit = p.limit - J[st.run_index] - 1; + let em = r.golomb(k, limit, p.qbpp)?; + let tmp = em + ri_type; + let cond = k == 0 && 2 * st.nn[ri_type as usize] < st.n[q]; + let map = tmp & 1; + let mag = (tmp + map) / 2; + let e = if (cond && map == 1) || (!cond && map == 0) { mag } else { -mag }; + let (px, sign) = if ri_type == 1 { (ra, 1) } else { (rb, if ra > rb { -1 } else { 1 }) }; + // The decoder holds the sign-adjusted error; the sign flips the reconstruction. + let rx = reconstruct(px, sign, e, p); + update_run(st, q, e, em, ri_type, p); + if st.run_index > 0 { + st.run_index -= 1; + } + cur[x + run + 1] = rx; + Ok(x + run + 1) +} + +// ---- encoder ---- + +struct BitWriter { + out: Vec, + cur: u32, + used: u32, + cap: u32, +} + +impl BitWriter { + fn new() -> Self { + BitWriter { out: Vec::new(), cur: 0, used: 0, cap: 8 } + } + + fn put(&mut self, value: u32, n: u32) { + for i in (0..n).rev() { + // A run of more than 32 zero bits (a long escape code) has no bits of `value` to shift out. + let bit = if i < 32 { (value >> i) & 1 } else { 0 }; + self.cur = self.cur << 1 | bit; + self.used += 1; + if self.used == self.cap { + let byte = self.cur as u8; + self.out.push(byte); + self.cap = if byte == 0xFF { 7 } else { 8 }; + self.cur = 0; + self.used = 0; + } + } + } + + /// Pad the last byte with zero bits; a closing 0xFF is followed by a zero byte. + fn finish(mut self) -> Vec { + if self.used > 0 { + let pad = self.cap - self.used; + self.put(0, pad); + } + if self.out.last() == Some(&0xFF) { + self.out.push(0); + } + self.out + } + + fn golomb(&mut self, m: i32, k: u32, limit: u32, qbpp: u32) { + let m = m as u32; + let max_zeros = limit - qbpp - 1; + let q = m >> k; + if q < max_zeros { + self.put(0, q); + self.put(1, 1); + self.put(m & ((1 << k) - 1), k); + } else { + self.put(0, max_zeros); + self.put(1, 1); + self.put(m - 1, qbpp); + } + } +} + +fn encode_scan(plane: &[u16], w: usize, h: usize, p: &Params) -> Vec { + let mut wr = BitWriter::new(); + let mut st = State::new(p); + let mut prev = vec![0i32; w + 2]; + let mut cur = vec![0i32; w + 2]; + for y in 0..h { + cur[0] = prev[1]; + if y > 0 { + prev[w + 1] = prev[w]; + } + let line = &plane[y * w..(y + 1) * w]; + let mut x = 0usize; + while x < w { + let (ra, rb, rc, rd) = (cur[x], prev[x + 1], prev[x], if y == 0 { 0 } else { prev[x + 2] }); + let ix = i32::from(line[x]); + let (q1, q2, q3) = (quantise_gradient(rd - rb, p), quantise_gradient(rb - rc, p), quantise_gradient(rc - ra, p)); + if q1 == 0 && q2 == 0 && q3 == 0 { + x = encode_run(&mut wr, &mut st, p, line, &prev, &mut cur, ra, x); + continue; + } + let (sign, q1, q2, q3) = if q1 < 0 || (q1 == 0 && (q2 < 0 || (q2 == 0 && q3 < 0))) { (-1, -q1, -q2, -q3) } else { (1, q1, q2, q3) }; + let q = (q1 * 81 + (q2 + 4) * 9 + (q3 + 4)) as usize; + let px = (predict(ra, rb, rc) + sign * st.c[q]).clamp(0, p.maxval); + let e = reduce(sign * (ix - px), p); + let k = golomb_k(st.n[q], st.a[q]); + let swap = p.near == 0 && k == 0 && 2 * st.b[q] <= -st.n[q]; + let m = if swap { + if e >= 0 { 2 * e + 1 } else { -2 * (e + 1) } + } else if e >= 0 { + 2 * e + } else { + -2 * e - 1 + }; + wr.golomb(m, k, p.limit, p.qbpp); + cur[x + 1] = reconstruct(px, sign, e, p); + update_regular(&mut st, q, e, p); + x += 1; + } + std::mem::swap(&mut prev, &mut cur); + } + wr.finish() +} + +#[allow(clippy::too_many_arguments)] +fn encode_run(wr: &mut BitWriter, st: &mut State, p: &Params, line: &[u16], prev: &[i32], cur: &mut [i32], ra: i32, x: usize) -> usize { + let w = line.len(); + let mut run = 0usize; + while x + run < w && (i32::from(line[x + run]) - ra).abs() <= p.near { + cur[x + run + 1] = ra; + run += 1; + } + let mut count = run; + while count >= 1usize << J[st.run_index] { + wr.put(1, 1); + count -= 1usize << J[st.run_index]; + if st.run_index < 31 { + st.run_index += 1; + } + } + if x + run == w { + if count > 0 { + wr.put(1, 1); + } + return w; + } + wr.put(0, 1); + wr.put(count as u32, J[st.run_index]); + let ix = i32::from(line[x + run]); + let rb = prev[x + run + 1]; + let ri_type = i32::from((ra - rb).abs() <= p.near); + let q = RI_BASE + ri_type as usize; + let (px, sign) = if ri_type == 1 { (ra, 1) } else { (rb, if ra > rb { -1 } else { 1 }) }; + let e = reduce(sign * (ix - px), p); + let k = run_k(st, q, ri_type); + let n_neg = st.nn[ri_type as usize]; + let map = i32::from((k == 0 && e > 0 && 2 * n_neg < st.n[q]) || (e < 0 && 2 * n_neg >= st.n[q]) || (e < 0 && k != 0)); + let em = 2 * e.abs() - ri_type - map; + wr.golomb(em, k, p.limit - J[st.run_index] - 1, p.qbpp); + cur[x + run + 1] = reconstruct(px, sign, e, p); + update_run(st, q, e, em, ri_type, p); + if st.run_index > 0 { + st.run_index -= 1; + } + x + run + 1 +} + +/// Writes a lossless JPEG-LS file: 8 or 16 bits, gray or RGB, one scan per component. +pub(crate) fn encode(src: &Image, plan: Plan) -> Result, CodecError> { + let enc = |msg: String| CodecError::encode(F, msg); + let img = src.converted(plan.layout, plan.sample); + let nc = match img.layout() { + ChannelLayout::Gray => 1, + ChannelLayout::Rgb => 3, + l => return Err(enc(format!("no JPEG-LS layout for {l:?}"))), + }; + let precision: u32 = match img.sample_type() { + SampleType::U8 => 8, + SampleType::U16 => 16, + s => return Err(enc(format!("JPEG-LS stores 8 or 16 bits per sample, not {s:?}"))), + }; + let (w, h) = img.dimensions(); + let (Ok(w16), Ok(h16)) = (u16::try_from(w), u16::try_from(h)) else { + return Err(enc(format!("dimensions exceed 65535x65535: {w}x{h}"))); + }; + let (wu, hu) = (w as usize, h as usize); + let data = img.data(); + if data.len() != wu * hu * nc * (precision as usize / 8) { + return Err(enc("the image has the wrong number of samples".into())); + } + let params = Params::new(precision, 0, [0; 5]).map_err(|_| enc("invalid coding parameters".into()))?; + let mut out = vec![0xFF, 0xD8, 0xFF, SOF55]; + out.extend_from_slice(&((8 + 3 * nc) as u16).to_be_bytes()); + out.push(precision as u8); + out.extend_from_slice(&h16.to_be_bytes()); + out.extend_from_slice(&w16.to_be_bytes()); + out.push(nc as u8); + for c in 0..nc { + out.extend_from_slice(&[c as u8 + 1, 0x11, 0]); + } + let mut plane = vec![0u16; wu * hu]; + for c in 0..nc { + for (i, v) in plane.iter_mut().enumerate() { + let at = i * nc + c; + *v = if precision == 8 { u16::from(data[at]) } else { u16::from_ne_bytes([data[2 * at], data[2 * at + 1]]) }; + } + out.extend_from_slice(&[0xFF, 0xDA, 0, 8, 1, c as u8 + 1, 0, 0, 0, 0]); + out.extend_from_slice(&encode_scan(&plane, wu, hu, ¶ms)); + } + out.extend_from_slice(&[0xFF, 0xD9]); + Ok(out) +} + +struct Frame { + precision: u32, + width: usize, + height: usize, + components: Vec, +} + +fn be16(b: &[u8], i: usize) -> Result { + b.get(i..i + 2).map(|s| usize::from(u16::from_be_bytes([s[0], s[1]]))).ok_or_else(|| bad("truncated header")) +} + +pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { + if !looks_like_jpegls(b) { + return Err(bad("not a JPEG-LS file")); + } + let mut pos = 2; + let mut frame: Option = None; + let mut preset = [0i32; 5]; + let mut planes: Vec> = Vec::new(); + let mut scans_done = 0; + loop { + // Find the next marker. + while b.get(pos) != Some(&0xFF) { + if pos >= b.len() { + return finish(frame, planes, scans_done); + } + pos += 1; + } + while b.get(pos + 1) == Some(&0xFF) { + pos += 1; + } + let Some(&marker) = b.get(pos + 1) else { return finish(frame, planes, scans_done) }; + if marker == 0xD9 { + return finish(frame, planes, scans_done); + } + if marker == 0x00 || (0xD0..=0xD7).contains(&marker) || marker == 0x01 { + pos += 2; + continue; + } + let len = be16(b, pos + 2)?; + let seg = b.get(pos + 4..pos + 2 + len).ok_or_else(|| bad("truncated marker segment"))?; + match marker { + SOF55 => { + if frame.is_some() || seg.len() < 6 { + return Err(bad("bad or repeated frame header")); + } + let (precision, height, width, nf) = (u32::from(seg[0]), be16(seg, 1)?, be16(seg, 3)?, usize::from(seg[5])); + if !(2..=16).contains(&precision) || width == 0 || height == 0 || !matches!(nf, 1 | 3) || seg.len() < 6 + 3 * nf { + return Err(bad("unsupported frame header")); + } + if height == 0 { + return Err(CodecError::unsupported(F, "a frame height set later by a DNL marker")); + } + let layout = if nf == 1 { ChannelLayout::Gray } else { ChannelLayout::Rgb }; + let sample = if precision <= 8 { SampleType::U8 } else { SampleType::U16 }; + limits.check(width as u32, height as u32, layout, sample)?; + let components = (0..nf).map(|i| seg[6 + 3 * i]).collect(); + frame = Some(Frame { precision, width, height, components }); + } + LSE => { + if seg.first() == Some(&1) && seg.len() >= 11 { + for (i, p) in preset.iter_mut().enumerate() { + *p = be16(seg, 1 + 2 * i)? as i32; + } + } else if seg.first() != Some(&1) { + return Err(CodecError::unsupported(F, "mapping tables and colour transforms (LSE)")); + } + } + 0xDA => { + let f = frame.as_ref().ok_or_else(|| bad("a scan before the frame header"))?; + let ns = usize::from(*seg.first().ok_or_else(|| bad("empty scan header"))?); + if seg.len() < 1 + 2 * ns + 3 || ns != 1 { + return Err(CodecError::unsupported(F, "interleaved scans (ILV 1 and 2)")); + } + let (cs, tm, near, ilv) = (seg[1], seg[2], i32::from(seg[3]), seg[4]); + if tm != 0 { + return Err(CodecError::unsupported(F, "mapping tables")); + } + if ilv != 0 && f.components.len() > 1 { + return Err(CodecError::unsupported(F, "interleaved scans (ILV 1 and 2)")); + } + if seg[5] & 0x0f != 0 { + return Err(CodecError::unsupported(F, "a point transform")); + } + let idx = f.components.iter().position(|&c| c == cs).ok_or_else(|| bad("a scan names an unknown component"))?; + if planes.len() <= idx { + planes.resize(f.components.len(), Vec::new()); + } + if !planes[idx].is_empty() { + return Err(bad("a component is coded twice")); + } + let params = Params::new(f.precision, near, preset)?; + let start = pos + 2 + len; + // The scan ends at the next real marker: an 0xFF followed by a byte with its top bit set. + let mut end = start; + while end < b.len() { + if b[end] == 0xFF && b.get(end + 1).is_none_or(|&n| n & 0x80 != 0) { + break; + } + end += 1; + } + let mut samples = Vec::new(); + samples.try_reserve_exact(f.width * f.height).map_err(|_| bad("pixel buffer is too large"))?; + samples.resize(f.width * f.height, 0u16); + decode_scan(&b[start..end], f.width, f.height, ¶ms, &mut samples)?; + planes[idx] = samples; + scans_done += 1; + pos = end; + continue; + } + _ => {} + } + pos += 2 + len; + } +} + +fn finish(frame: Option, planes: Vec>, scans: usize) -> Result { + let f = frame.ok_or_else(|| bad("no frame header"))?; + if scans != f.components.len() || planes.iter().any(Vec::is_empty) || planes.len() != f.components.len() { + return Err(bad("the file ends before every component is coded")); + } + let (w, h, nc) = (f.width, f.height, f.components.len()); + let layout = if nc == 1 { ChannelLayout::Gray } else { ChannelLayout::Rgb }; + let p = f.precision; + let scale = |v: u16| -> u16 { + if p == 16 { + v + } else { + let v = u32::from(v); + ((v << (16 - p)) | (v >> (2 * p).saturating_sub(16).min(15))) as u16 + } + }; + if p <= 8 { + let to8 = |v: u16| -> u8 { if p == 8 { v as u8 } else { (scale(v) >> 8) as u8 } }; + let mut data = Vec::with_capacity(w * h * nc); + for i in 0..w * h { + for plane in &planes { + data.push(to8(plane[i])); + } + } + Image::from_u8(w as u32, h as u32, layout, data) + } else { + let mut data = Vec::with_capacity(w * h * nc); + for i in 0..w * h { + for plane in &planes { + data.push(scale(plane[i])); + } + } + Image::from_u16(w as u32, h as u32, layout, &data) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn noise_u16(n: usize, seed: &mut u32, mask: u32) -> Vec { + (0..n) + .map(|_| { + *seed ^= *seed << 13; + *seed ^= *seed >> 17; + *seed ^= *seed << 5; + (*seed & mask) as u16 + }) + .collect() + } + + fn image(w: u32, h: u32, layout: ChannelLayout, wide: bool, kind: u8) -> Image { + let n = w as usize * h as usize * layout.channels(); + let mut seed = 0x1234_5679u32; + let samples: Vec = match kind { + 0 => noise_u16(n, &mut seed, if wide { 0xFFFF } else { 0xFF }), + 1 => vec![if wide { 40_000 } else { 100 }; n], + _ => (0..n).map(|i| if wide { ((i / 3) * 37 % 65_536) as u16 } else { ((i / 3) % 251) as u16 }).collect(), + }; + if wide { + Image::from_u16(w, h, layout, &samples).unwrap() + } else { + Image::from_u8(w, h, layout, samples.iter().map(|&v| v as u8).collect()).unwrap() + } + } + + fn plan(img: &Image) -> Plan { + Plan { layout: img.layout(), sample: img.sample_type() } + } + + #[test] + fn round_trips_noise_flat_and_smooth_images() { + for layout in [ChannelLayout::Gray, ChannelLayout::Rgb] { + for wide in [false, true] { + for kind in 0..3 { + // Widths around the run-length steps, and single-pixel dimensions. + for (w, h) in [(1, 1), (1, 9), (2, 3), (7, 5), (33, 4), (300, 3), (1025, 2)] { + let img = image(w, h, layout, wide, kind); + let bytes = encode(&img, plan(&img)).unwrap(); + assert!(looks_like_jpegls(&bytes)); + let back = decode(&bytes, &Limits::default()).unwrap_or_else(|e| panic!("{layout:?} wide={wide} kind={kind} {w}x{h}: {e}")); + assert_eq!(back.data(), img.data(), "{layout:?} wide={wide} kind={kind} {w}x{h}"); + } + } + } + } + } + + #[test] + fn default_parameters_follow_the_standard() { + let p = Params::new(8, 0, [0; 5]).unwrap(); + assert_eq!((p.maxval, p.range, p.qbpp, p.limit, p.t1, p.t2, p.t3, p.reset), (255, 256, 8, 32, 3, 7, 21, 64)); + let p = Params::new(16, 0, [0; 5]).unwrap(); + assert_eq!((p.maxval, p.range, p.qbpp, p.limit, p.t1, p.t2, p.t3), (65535, 65536, 16, 64, 18, 67, 276)); + let p = Params::new(8, 3, [0; 5]).unwrap(); + assert_eq!((p.range, p.t1), (38, 12)); // RANGE = floor((255 + 2 * 3) / 7) + 1 + assert!(Params::new(8, 200, [0; 5]).is_err()); + } + + #[test] + fn detection_tells_jpeg_ls_from_jpeg() { + let img = image(4, 4, ChannelLayout::Gray, false, 0); + let ls = encode(&img, plan(&img)).unwrap(); + assert!(looks_like_jpegls(&ls)); + // A baseline JPEG starts SOI, APP0 / DQT, ... SOF0: not JPEG-LS. + assert!(!looks_like_jpegls(&[0xFF, 0xD8, 0xFF, 0xE0, 0, 4, 0, 0, 0xFF, 0xC0, 0, 11])); + assert!(!looks_like_jpegls(&[0xFF, 0xD8, 0xFF, 0xC0, 0, 11])); + // SOF55 after an application segment still is. + assert!(looks_like_jpegls(&[0xFF, 0xD8, 0xFF, 0xE8, 0, 4, 0, 0, 0xFF, 0xF7, 0, 11])); + assert!(!looks_like_jpegls(&[0xFF, 0xD8])); + } + + #[test] + fn interleaved_scans_and_tables_are_unsupported() { + let img = image(8, 4, ChannelLayout::Rgb, false, 2); + let good = encode(&img, plan(&img)).unwrap(); + // The scan header body is Ns, Cs, Tm, NEAR, ILV, point transform, after the marker and length. + let sos = good.windows(2).position(|w| w == [0xFF, 0xDA]).unwrap() + 4; + let mut line = good.clone(); + line[sos + 4] = 1; + assert!(matches!(decode(&line, &Limits::default()), Err(CodecError::Unsupported { .. }))); + let mut mapped = good.clone(); + mapped[sos + 2] = 1; + assert!(matches!(decode(&mapped, &Limits::default()), Err(CodecError::Unsupported { .. }))); + let mut transform = good; + transform[sos + 5] = 1; + assert!(matches!(decode(&transform, &Limits::default()), Err(CodecError::Unsupported { .. }))); + } + + #[test] + fn hostile_input_is_an_error_not_a_panic() { + for (w, h, layout, wide) in [(23, 9, ChannelLayout::Gray, false), (11, 7, ChannelLayout::Rgb, true)] { + let img = image(w, h, layout, wide, 0); + let good = encode(&img, plan(&img)).unwrap(); + for len in 0..good.len() { + let _ = decode(&good[..len], &Limits::default()); + } + // Flip every bit of the entropy-coded data and of the headers, one at a time. + for i in 0..good.len() { + for bit in 0..8 { + let mut c = good.clone(); + c[i] ^= 1 << bit; + let _ = decode(&c, &Limits::default()); + } + } + } + // A huge frame is stopped by the limits or the length check before any allocation. + let img = image(4, 4, ChannelLayout::Gray, false, 0); + let mut huge = encode(&img, plan(&img)).unwrap(); + let sof = huge.windows(2).position(|w| w == [0xFF, 0xF7]).unwrap(); + huge[sof + 5..sof + 9].copy_from_slice(&[0xFF, 0xFF, 0xFF, 0xFF]); + assert!(decode(&huge, &Limits::default()).is_err()); + } +} diff --git a/crates/image-codecs/src/format.rs b/crates/image-codecs/src/format.rs index ecaf945cd..810c1ad48 100644 --- a/crates/image-codecs/src/format.rs +++ b/crates/image-codecs/src/format.rs @@ -44,6 +44,8 @@ pub enum Format { Iff, /// JPEG 2000 (JP2 container or raw J2K codestream): read-only; see [`ASYMMETRIC_EXCEPTIONS`]. Jp2, + /// JPEG-LS (`.jls`): lossless and near-lossless gray and RGB. + JpegLs, } /// What a format can hold **and** what this crate reads/writes for it. @@ -113,7 +115,7 @@ const TIFF_LAYOUTS: &[ChannelLayout] = &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L:: impl Format { /// Every format known to the crate (enabled or not). - pub const ALL: [Format; 24] = [ + pub const ALL: [Format; 25] = [ Format::Png, Format::Jpeg, Format::Jp2, @@ -138,6 +140,7 @@ impl Format { Format::Cineon, Format::Dicom, Format::Iff, + Format::JpegLs, ]; pub fn caps(self) -> FormatCaps { @@ -176,6 +179,7 @@ impl Format { Format::Dicom => "DICOM", Format::Iff => "Amiga IFF", Format::Jp2 => "JPEG 2000", + Format::JpegLs => "JPEG-LS", } } @@ -207,6 +211,7 @@ impl Format { Format::Dicom => &["dcm", "dicom", "dc3"], Format::Iff => &["iff", "lbm", "ilbm"], Format::Jp2 => &["jp2", "j2k", "j2c", "jpf", "jpx"], + Format::JpegLs => &["jls"], } } @@ -236,13 +241,14 @@ impl Format { Format::Dicom => "application/dicom", Format::Iff => "image/x-iff", Format::Jp2 => "image/jp2", + Format::JpegLs => "image/jls", } } /// Maximum encodable dimensions, if the format has a hard limit. pub fn max_dimensions(self) -> Option<(u32, u32)> { match self { - Format::Jpeg | Format::Gif | Format::Iff => Some((65535, 65535)), + Format::Jpeg | Format::Gif | Format::Iff | Format::JpegLs => Some((65535, 65535)), Format::WebP => Some((16384, 16384)), Format::Ico => Some((256, 256)), _ => None, @@ -310,6 +316,7 @@ pub fn caps(format: Format) -> FormatCaps { Format::Cineon => FormatCaps { depths: &[S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Dicom => FormatCaps { read: true, write: false, depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Iff => FormatCaps { depths: &[S::U8], layouts: &[L::Rgb, L::Rgba], alpha: true, ..base }, + Format::JpegLs => FormatCaps { depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Jp2 => FormatCaps { read: true, write: false, layouts: &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA], icc: true, ..base }, } } @@ -321,6 +328,9 @@ pub fn detect(bytes: &[u8]) -> Option { if b.starts_with(b"\x89PNG\r\n\x1a\n") { return Some(Format::Png); } + if crate::codecs::jpegls::looks_like_jpegls(b) { + return Some(Format::JpegLs); + } if b.starts_with(&[0xFF, 0xD8, 0xFF]) { return Some(Format::Jpeg); } diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 21130e392..b60691e83 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -62,7 +62,7 @@ pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_ pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding}; pub use half::f16; -use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; +use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jpegls, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; /// Detect the format and decode with default [`Limits`]. pub fn decode(bytes: &[u8]) -> Result { @@ -101,6 +101,7 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res Format::Cineon => dpx::decode_cineon(bytes, l), Format::Dicom => dicom::decode(bytes, l), Format::Iff => iff::decode(bytes, l), + Format::JpegLs => jpegls::decode(bytes, l), Format::OpenExr => exr::decode(bytes, l), Format::Heif => heif::decode(bytes, l, opts.keep_orientation), Format::Jp2 => jp2::decode(bytes, l), @@ -457,6 +458,7 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result dpx::encode_cineon(image, plan), Format::Dicom => Err(CodecError::unsupported(format, "DICOM is import-only; save as TIFF or PNG instead")), Format::Iff => iff::encode(image, plan), + Format::JpegLs => jpegls::encode(image, plan), Format::OpenExr => exr::encode(image, plan, opts), Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")), Format::Jp2 => Err(CodecError::unsupported(format, "JPEG 2000 is import-only; save as PNG or TIFF instead")), diff --git a/crates/image-codecs/tests/it/detect_caps.rs b/crates/image-codecs/tests/it/detect_caps.rs index 640772c83..390eaa41f 100644 --- a/crates/image-codecs/tests/it/detect_caps.rs +++ b/crates/image-codecs/tests/it/detect_caps.rs @@ -62,6 +62,7 @@ fn default_build_formats_all_symmetric() { Format::Dpx, Format::Cineon, Format::Iff, + Format::JpegLs, ]; for f in expected { assert!(caps(f).read && caps(f).write, "{f:?}"); @@ -218,6 +219,21 @@ fn detect_magic_iff() { assert_eq!(detect(b"FORM\0\0\0\x14AIFF"), None); assert!(decode_as(Format::Iff, b"FORM\0\0\0\x14ILBM").is_err()); } +#[test] +fn detect_magic_jpeg_ls() { + use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode}; + let img = Image::from_u8(5, 3, ChannelLayout::Gray, (0..15).collect()).unwrap(); + let bytes = encode(&img, Format::JpegLs, &EncodeOptions::default()).unwrap(); + assert_eq!(detect(&bytes), Some(Format::JpegLs)); + assert_eq!(from_extension("scan.JLS"), Some(Format::JpegLs)); + assert_eq!(decode(&bytes).unwrap().data(), img.data()); + assert!(!ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::JpegLs)); + assert_eq!(Format::JpegLs.max_dimensions(), Some((65535, 65535))); + // Plain JPEG still is JPEG: SOI, then a baseline frame header. + assert_eq!(detect(&[0xFF, 0xD8, 0xFF, 0xC0, 0, 11]), Some(Format::Jpeg)); + assert!(decode_as(Format::JpegLs, &[0xFF, 0xD8, 0xFF, 0xF7]).is_err()); +} + #[test] fn detect_magic_avif() { assert_eq!(detect(b"\0\0\0\x1cftypavif\0\0\0\0avifmif1miaf"), Some(Format::Avif)); diff --git a/crates/image-codecs/tests/it/jpegls_oracle.rs b/crates/image-codecs/tests/it/jpegls_oracle.rs new file mode 100644 index 000000000..aa98609a1 --- /dev/null +++ b/crates/image-codecs/tests/it/jpegls_oracle.rs @@ -0,0 +1,103 @@ +//! JPEG-LS against ffmpeg as the reference (oracle; skipped when ffmpeg is absent, a failure with +//! `CRAFT_MEDIA_REQUIRE_ORACLES=1`): +//! - files ffmpeg's encoder writes (8- and 16-bit gray: smooth, noisy and flat images, which +//! exercise the regular, run and escape-coded paths) decode to the pixels ffmpeg decodes; +//! - files our encoder writes (gray 8 and 16 bits, RGB 8 bits) decode in ffmpeg to our pixels (ffmpeg +//! cannot decode 16-bit RGB JPEG-LS, so that case is only round-tripped through our own decoder); +//! - ffmpeg writes RGB line-interleaved, which is reported as unsupported. + +use craft_image_codecs::{ChannelLayout, CodecError, EncodeOptions, Format, Image, SampleType, decode, encode}; +use std::path::Path; +use std::process::Command; + +const W: u32 = 66; +const H: u32 = 40; + +fn run(ffmpeg: &Path, args: &[&str]) -> Vec { + let out = Command::new(ffmpeg).args(["-v", "error", "-y"]).args(args).output().expect("run ffmpeg"); + assert!(out.status.success(), "ffmpeg {args:?}: {}", String::from_utf8_lossy(&out.stderr)); + out.stdout +} + +/// `(name, lavfi source)`: smooth, noisy and flat pictures of W x H. +fn sources() -> Vec<(&'static str, String)> { + let s = format!("{W}x{H}"); + vec![ + ("testsrc", format!("testsrc2=size={s}:rate=1")), + ("noise", format!("nullsrc=size={s}:rate=1,geq=lum='random(1)*255':cb=128:cr=128")), + ("flat", format!("color=c=0x336699:size={s}:rate=1")), + ("gradient", format!("gradients=size={s}:rate=1:seed=7")), + ] +} + +#[test] +fn ffmpeg_files_decode_to_ffmpegs_pixels() { + let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("jpegls"); + for (name, src) in sources() { + for (fmt, ext_bits) in [("gray", 8), ("gray16le", 16)] { + let file = dir.join(format!("ffmpeg_{name}_{ext_bits}.jls")); + let f = file.to_str().expect("path"); + run(&ffmpeg, &["-f", "lavfi", "-i", &src, "-frames:v", "1", "-pix_fmt", fmt, f]); + let theirs = run(&ffmpeg, &["-i", f, "-f", "rawvideo", "-pix_fmt", fmt, "-"]); + let ours = decode(&std::fs::read(&file).expect("read")).unwrap_or_else(|e| panic!("{name} {fmt}: {e}")); + assert_eq!((ours.width(), ours.height(), ours.layout()), (W, H, ChannelLayout::Gray), "{name} {fmt}"); + let want_sample = if ext_bits == 8 { SampleType::U8 } else { SampleType::U16 }; + assert_eq!(ours.sample_type(), want_sample); + let ours_bytes: Vec = if ext_bits == 8 { + ours.data().to_vec() + } else { + ours.data().as_chunks::<2>().0.iter().flat_map(|b| u16::from_ne_bytes(*b).to_le_bytes()).collect() + }; + assert!(ours_bytes == theirs, "{name} {fmt}: pixels differ from ffmpeg's decode"); + } + } +} + +#[test] +fn ffmpegs_rgb_is_line_interleaved_and_unsupported() { + let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("jpegls"); + let file = dir.join("ffmpeg_rgb.jls"); + run( + &ffmpeg, + &["-f", "lavfi", "-i", &format!("testsrc2=size={W}x{H}:rate=1"), "-frames:v", "1", "-pix_fmt", "rgb24", file.to_str().expect("path")], + ); + assert!(matches!(decode(&std::fs::read(&file).expect("read")), Err(CodecError::Unsupported { .. }))); +} + +#[test] +fn ffmpeg_decodes_our_files_to_our_pixels() { + let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("jpegls"); + for (name, src) in sources() { + for (layout, fmt8, fmt16) in [(ChannelLayout::Gray, "gray", "gray16le"), (ChannelLayout::Rgb, "rgb24", "rgb48le")] { + for bits in [8, 16] { + let fmt = if bits == 8 { fmt8 } else { fmt16 }; + // Source pixels from ffmpeg, so the test image is realistic. + let raw = run(&ffmpeg, &["-f", "lavfi", "-i", &src, "-frames:v", "1", "-pix_fmt", fmt, "-f", "rawvideo", "-"]); + let img = if bits == 8 { + Image::from_u8(W, H, layout, raw).expect("image") + } else { + let samples: Vec = raw.as_chunks::<2>().0.iter().map(|b| u16::from_le_bytes(*b)).collect(); + Image::from_u16(W, H, layout, &samples).expect("image") + }; + let bytes = encode(&img, Format::JpegLs, &EncodeOptions::default()).expect("encode"); + assert_eq!(decode(&bytes).expect("our own file decodes").data(), img.data(), "{name} {fmt}: round trip"); + let file = dir.join(format!("ours_{name}_{fmt}.jls")); + std::fs::write(&file, &bytes).expect("write"); + // ffmpeg's decoder does not implement 16-bit colour ("Not yet implemented"). + if layout == ChannelLayout::Rgb && bits == 16 { + continue; + } + let theirs = run(&ffmpeg, &["-i", file.to_str().expect("path"), "-f", "rawvideo", "-pix_fmt", fmt, "-"]); + let ours_bytes: Vec = if bits == 8 { + img.data().to_vec() + } else { + img.data().as_chunks::<2>().0.iter().flat_map(|b| u16::from_ne_bytes(*b).to_le_bytes()).collect() + }; + assert!(theirs == ours_bytes, "{name} {fmt}: ffmpeg decodes our file to other pixels"); + } + } + } +} diff --git a/crates/image-codecs/tests/it/main.rs b/crates/image-codecs/tests/it/main.rs index f465a0cbb..4eda74a26 100644 --- a/crates/image-codecs/tests/it/main.rs +++ b/crates/image-codecs/tests/it/main.rs @@ -14,6 +14,7 @@ mod exr_stream; mod fidelity; mod gif_frames; mod heif; +mod jpegls_oracle; mod limits; mod malformed; mod metadata; From 5ab7a6035c0ff409cc743978b17d73f414b58524 Mon Sep 17 00:00:00 2001 From: el-j Date: Sun, 15 Nov 2026 13:06:49 +0100 Subject: [PATCH 017/150] craft-video: FFV1 lossless video, version 3 range-coder decoder and encoder Adds the ffv1 feature (RFC 9043): YCbCr (any 2^n subsampling, gray, optional transparency) and RGB through the JPEG 2000 reversible colour transform, 8 to 16 bits, any slice raster, slice and configuration record CRCs. Not covered yet: the Golomb-Rice coder, versions 0 and 1, inter frames. Part of #1509 --- crates/video/Cargo.toml | 6 + crates/video/src/ffv1/README.md | 52 ++ crates/video/src/ffv1/mod.rs | 929 ++++++++++++++++++++++++++++++++ crates/video/src/ffv1/rc.rs | 229 ++++++++ crates/video/src/lib.rs | 4 + crates/video/tests/ffv1.rs | 304 +++++++++++ 6 files changed, 1524 insertions(+) create mode 100644 crates/video/src/ffv1/README.md create mode 100644 crates/video/src/ffv1/mod.rs create mode 100644 crates/video/src/ffv1/rc.rs create mode 100644 crates/video/tests/ffv1.rs diff --git a/crates/video/Cargo.toml b/crates/video/Cargo.toml index c3ebfd722..dae21e5b1 100644 --- a/crates/video/Cargo.toml +++ b/crates/video/Cargo.toml @@ -34,6 +34,8 @@ mpeg2-encode = ["mpeg2-decode"] # MPEG-4 Part 2 video (ISO/IEC 14496-2, the Xvid/DivX format): Simple profile encoder. mpeg4 = ["mpeg4-encode"] mpeg4-encode = ["craft-media/bitstream"] +# FFV1 lossless video (RFC 9043): version 3, range coder, decoder and encoder. +ffv1 = [] # Apple ProRes (SMPTE RDD 36): decoder and encoder. prores = ["craft-media/bitstream"] # H.264 / AVC: decoder and encoder; `h264-decode` or `h264-encode` for one of them. @@ -143,6 +145,10 @@ required-features = ["mpeg4-encode"] name = "mpeg2" required-features = ["mpeg2-decode"] +[[test]] +name = "ffv1" +required-features = ["ffv1"] + [[test]] name = "prores" required-features = ["prores"] diff --git a/crates/video/src/ffv1/README.md b/crates/video/src/ffv1/README.md new file mode 100644 index 000000000..90312dbe5 --- /dev/null +++ b/crates/video/src/ffv1/README.md @@ -0,0 +1,52 @@ +# FFV1 + +Clean-room FFV1 lossless video codec, written from RFC 9043. Feature `ffv1`. + +## What is supported + +| | Decoder | Encoder | +|---|---|---| +| Version | 3 (the one with a configuration record) | 3 | +| Coder | range coder, default or custom state transition table | range coder, default table | +| Colour | YCbCr (any 2^n chroma subsampling, gray, optional transparency), RGB through the JPEG 2000 reversible colour transform (optional transparency) | the same, RGB up to 15 bits | +| Depth | 8 to 16 bits per sample | 8 to 16 bits (RGB 8 to 15) | +| Slices | any raster whose boundaries are multiples of the chroma subsampling | the same | +| Frames | keyframes | keyframes only (`intra` 1) | +| Error detection | slice and configuration record CRCs, checked | written | + +Not supported, and reported as `Error::Unsupported`: the Golomb-Rice coder (`coder_type` 0), +versions 0 and 1 (parameters inside each keyframe, no configuration record), inter frames +(`keyframe` 0), and slice rasters whose boundaries fall inside a chroma sample. + +## Using it + +The configuration record goes in the container (Matroska `CodecPrivate`, the AVI `strf` +extension, an MP4 `glbl` box); each frame is one packet. + +```text +let enc = Encoder::new(&template_frame, EncoderConfig::default())?; +let config = enc.config_record(); +let packet = enc.encode(&frame)?; + +let dec = Decoder::new(&config, width, height)?; +let frame = dec.decode(&packet)?; +``` + +## Notes on the format + +- The Parameters in the configuration record are range coded with the *default* state transition + table. A custom table (`coder_type` 2) applies to the slices only. +- Each Quantization Table starts from fresh states; a Quantization Table Set is five of them. +- A slice is one range-coded stream: for the first slice the `keyframe` bit, then the slice header, + then the samples. Cb and Cr share one set of context states; transparency has its own. +- The context is the sum of five quantised differences. With the default tables this encoder + writes 15125 contexts; FFmpeg refuses tables that multiply to more than 32768, so the encoder + stays below that although the RFC's limit (on half the product) is looser. +- The encoder ends a stream with the top of its final range, so bytes after it (a CRC in the + configuration record) cannot change what decodes. + +## Verification + +`tests/ffv1.rs`: round trips over every shape, hostile input (every truncation and bit flip), +and ffmpeg as the oracle in both directions (skipped without ffmpeg, a failure with +`CRAFT_MEDIA_REQUIRE_ORACLES=1`). diff --git a/crates/video/src/ffv1/mod.rs b/crates/video/src/ffv1/mod.rs new file mode 100644 index 000000000..4df60f9a1 --- /dev/null +++ b/crates/video/src/ffv1/mod.rs @@ -0,0 +1,929 @@ +//! FFV1 lossless video codec (RFC 9043): version 3, range coder, intra-only, YCbCr and RGB (JPEG 2000 +//! RCT), 8 to 16 bits per sample, any number of slices. +//! +//! - [`Decoder`]: built from the *configuration record* a container stores (Matroska `CodecPrivate`, +//! AVI `strf` extension, MP4 `glbl`) and the frame size; [`Decoder::decode`] turns one packet into a +//! [`Frame`]. Slice CRCs are checked. +//! - [`Encoder`]: [`Encoder::config_record`] gives the bytes for the container, [`Encoder::encode`] +//! one packet per frame; every frame is a keyframe. +//! +//! Not supported (reported as [`Error::Unsupported`]): the Golomb-Rice coder (`coder_type` 0), +//! versions 0 and 1 (no configuration record, parameters in every keyframe), inter frames +//! (`intra` 0 with `keyframe` 0), and slice rasters whose boundaries are not multiples of the +//! chroma subsampling. See `src/ffv1/README.md`. +//! +//! Implemented from RFC 9043. Frames are planar `u16`: for YCbCr, `[Y, Cb, Cr]` (plus an optional +//! transparency plane); for RGB, `[R, G, B]` (plus transparency), all at full size. + +mod rc; + +use crate::budget::{DECODE_MEMORY_BUDGET, PictureShape}; +use rc::{Decoder as Rac, Encoder as RacEnc, Transitions}; + +const CONTEXT_SIZE: usize = 32; +const MAX_CONTEXT_INPUTS: usize = 5; +const MAX_CONTEXTS: u64 = 32768; + +/// Codec errors. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// The data ends early or its sizes do not add up. + #[error("truncated FFV1 data")] + Truncated, + /// The data is not valid FFV1. + #[error("invalid FFV1 data: {0}")] + Invalid(&'static str), + /// Valid FFV1 that this module does not decode. + #[error("unsupported FFV1 feature: {0}")] + Unsupported(&'static str), + /// A slice or the configuration record failed its CRC. + #[error("FFV1 CRC mismatch in {0}")] + Crc(&'static str), + /// The encoder was given a frame or settings it cannot encode. + #[error("invalid encoder input: {0}")] + Input(String), +} + +/// `Result` of this module. +pub type Result = std::result::Result; + +/// The colour space coded (RFC 9043 table 8). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Colorspace { + /// YCbCr, coded plane by plane without a pixel transform. + YCbCr, + /// RGB through the JPEG 2000 reversible colour transform, coded line by line. + Rgb, +} + +/// One frame of planar samples. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Frame { + /// Width in pixels. + pub width: u32, + /// Height in pixels. + pub height: u32, + /// Colour space of the planes. + pub colorspace: Colorspace, + /// Bits per sample, 8 to 16. + pub bit_depth: u8, + /// log2 of the horizontal and vertical chroma subsampling (YCbCr only; 0, 0 is 4:4:4). + pub chroma_shift: (u8, u8), + /// The planes, in the order the module doc gives. + pub planes: Vec>, +} + +impl Frame { + /// Whether the planes are Y, Cb, Cr (or R, G, B) rather than Y (gray) alone, or with transparency. + fn plane_count_ok(&self) -> bool { + matches!(self.planes.len(), 1..=4) + } + + fn dims(&self, plane: usize, chroma_planes: bool) -> (usize, usize) { + plane_dims(self.width as usize, self.height as usize, self.colorspace, self.chroma_shift, plane, chroma_planes) + } +} + +/// `(width, height)` of plane `p` for a full frame (or a slice) of `w` x `h`. +fn plane_dims(w: usize, h: usize, cs: Colorspace, shift: (u8, u8), p: usize, chroma_planes: bool) -> (usize, usize) { + if cs == Colorspace::YCbCr && chroma_planes && (p == 1 || p == 2) { (w.div_ceil(1 << shift.0), h.div_ceil(1 << shift.1)) } else { (w, h) } +} + +/// One Quantization Table Set (section 4.1). +#[derive(Clone)] +struct QuantSet { + tables: Vec<[i32; 256]>, + context_count: usize, +} + +/// The parsed "Parameters" of a stream. +#[derive(Clone)] +struct Params { + coder_type: u32, + transitions: Transitions, + colorspace: Colorspace, + bits: u32, + chroma_planes: bool, + shift: (u8, u8), + extra_plane: bool, + num_h: u32, + num_v: u32, + sets: Vec, + initial_states: Vec>>, + ec: bool, + intra: bool, +} + +impl Params { + fn plane_count(&self) -> usize { + 1 + if self.chroma_planes { 2 } else { 0 } + usize::from(self.extra_plane) + } + + /// Quantization Table Set index slot of plane `p` (section 3.6). + fn qset_slot(&self, p: usize) -> usize { + match p { + 0 => 0, + 1 | 2 if self.chroma_planes => 1, + _ => 2, + } + } +} + +fn crc32(data: &[u8]) -> u32 { + // The IEEE polynomial 0x04C11DB7, initial value 0, no reflection, no final inversion. + let mut crc = 0u32; + for &b in data { + crc ^= u32::from(b) << 24; + for _ in 0..8 { + crc = if crc & 0x8000_0000 != 0 { crc << 1 ^ 0x04C1_1DB7 } else { crc << 1 }; + } + } + crc +} + +// ---- Parameters ---- + +fn read_quant_set(rac: &mut Rac, t: &Transitions) -> Result { + let mut scale: u64 = 1; + let mut tables = Vec::with_capacity(MAX_CONTEXT_INPUTS); + for _ in 0..MAX_CONTEXT_INPUTS { + let mut table = [0i32; 256]; + // Each quantization table starts from fresh states. + let mut state = [128u8; CONTEXT_SIZE]; + let mut v: i64 = 0; + let mut k = 0usize; + while k < 128 { + let len = rac.symbol(&mut state, false, t) as usize + 1; + if k + len > 128 { + return Err(Error::Invalid("quantization table run passes its half")); + } + for _ in 0..len { + table[k] = i32::try_from(scale as i64 * v).map_err(|_| Error::Invalid("quantization table overflows"))?; + k += 1; + } + v += 1; + } + for k in 1..128 { + table[256 - k] = -table[k]; + } + table[128] = -table[127]; + tables.push(table); + scale = scale.checked_mul(2 * v as u64 - 1).filter(|&s| s <= 2 * MAX_CONTEXTS).ok_or(Error::Invalid("too many contexts"))?; + } + let context_count = scale.div_ceil(2); + if context_count > MAX_CONTEXTS { + return Err(Error::Invalid("too many contexts")); + } + Ok(QuantSet { tables, context_count: context_count as usize }) +} + +fn parse_params(rac: &mut Rac) -> Result { + let mut state = [128u8; CONTEXT_SIZE]; + // Until the custom table is known, the default table decodes the fields before it. + let t0 = Transitions::default_table(); + let version = rac.symbol(&mut state, false, &t0); + if version != 3 { + return Err(Error::Unsupported("only FFV1 version 3 has a configuration record; versions 0 and 1 are not supported")); + } + let _micro = rac.symbol(&mut state, false, &t0); + let coder_type = rac.symbol(&mut state, false, &t0) as u32; + let mut delta = [0i32; 256]; + if coder_type > 1 { + for d in delta.iter_mut().skip(1) { + *d = rac.symbol(&mut state, true, &t0); + } + } + let transitions = Transitions::new(&delta); + if coder_type == 0 { + return Err(Error::Unsupported("the Golomb-Rice coder (coder_type 0)")); + } + if coder_type > 2 { + return Err(Error::Invalid("unknown coder_type")); + } + let colorspace = match rac.symbol(&mut state, false, &t0) { + 0 => Colorspace::YCbCr, + 1 => Colorspace::Rgb, + _ => return Err(Error::Unsupported("colorspace_type")), + }; + let bits = rac.symbol(&mut state, false, &t0) as u32; + let bits = if bits == 0 { 8 } else { bits }; + let chroma_planes = rac.bit(&mut state[0], &t0); + let (h, v) = (rac.symbol(&mut state, false, &t0), rac.symbol(&mut state, false, &t0)); + let extra_plane = rac.bit(&mut state[0], &t0); + let num_h = rac.symbol(&mut state, false, &t0) as u32 + 1; + let num_v = rac.symbol(&mut state, false, &t0) as u32 + 1; + let set_count = rac.symbol(&mut state, false, &t0); + if !(8..=16).contains(&bits) || h > 3 || v > 3 || h < 0 || v < 0 || num_h > 1024 || num_v > 1024 || !(1..=8).contains(&set_count) { + return Err(Error::Invalid("parameter out of range")); + } + if colorspace == Colorspace::Rgb && (!chroma_planes || h != 0 || v != 0) { + return Err(Error::Unsupported("RGB with chroma_planes 0 or subsampling")); + } + let mut sets = Vec::new(); + for _ in 0..set_count { + // The Parameters are range coded with the default table; the custom one is for the slices. + sets.push(read_quant_set(rac, &t0)?); + } + let mut initial_states = Vec::new(); + for set in &sets { + if rac.bit(&mut state[0], &t0) { + let mut all = Vec::with_capacity(set.context_count); + let mut prev = [128u8; CONTEXT_SIZE]; + for _ in 0..set.context_count { + let mut cur = [0u8; CONTEXT_SIZE]; + for k in 0..CONTEXT_SIZE { + let d = rac.symbol(&mut state, true, &t0); + cur[k] = (i32::from(prev[k]) + d) as u8; + } + prev = cur; + all.push(cur); + } + initial_states.push(Some(all)); + } else { + initial_states.push(None); + } + } + let ec = rac.symbol(&mut state, false, &t0) != 0; + let intra = rac.symbol(&mut state, false, &t0) != 0; + Ok(Params { + coder_type, + transitions, + colorspace, + bits, + chroma_planes, + shift: (h as u8, v as u8), + extra_plane, + num_h, + num_v, + sets, + initial_states, + ec, + intra, + }) +} + +/// The default quantisation tables of this encoder: 11 levels for the two gradients next to the +/// sample and 5 for the other three, which makes 15125 contexts (FFmpeg refuses more than 32768). +fn default_quant_set() -> QuantSet { + // |difference| thresholds: the first |d| of each level above 0 (levels 0..=5 and 0..=2). + let first_of_level: [&[usize]; MAX_CONTEXT_INPUTS] = [&[1, 2, 4, 8, 16], &[1, 2, 4, 8, 16], &[1, 3], &[1, 3], &[1, 3]]; + let mut scale: i32 = 1; + let mut tables = Vec::new(); + let mut counts = [0usize; MAX_CONTEXT_INPUTS]; + for (j, firsts) in first_of_level.iter().enumerate() { + let mut table = [0i32; 256]; + for (k, slot) in table.iter_mut().take(128).enumerate() { + let level = firsts.iter().filter(|&&f| k >= f).count() as i32; + *slot = scale * level; + } + for k in 1..128 { + table[256 - k] = -table[k]; + } + table[128] = -table[127]; + counts[j] = firsts.len() + 1; + scale *= 2 * counts[j] as i32 - 1; + tables.push(table); + } + QuantSet { tables, context_count: (scale as usize).div_ceil(2) } +} + +/// Run lengths (`len - 1` per step) that describe the first half of a table, as written to the stream. +fn quant_runs(table: &[i32; 256]) -> Vec { + let mut runs = Vec::new(); + let mut k = 0usize; + while k < 128 { + let start = k; + while k < 128 && table[k] == table[start] { + k += 1; + } + runs.push((k - start) as i32 - 1); + } + runs +} + +fn write_params(enc: &mut RacEnc, p: &Params) { + let t = Transitions::default_table(); + let mut state = [128u8; CONTEXT_SIZE]; + enc.symbol(&mut state, 3, false, &t); // version + enc.symbol(&mut state, 4, false, &t); // micro_version: first stable variant + enc.symbol(&mut state, p.coder_type as i32, false, &t); + enc.symbol(&mut state, if p.colorspace == Colorspace::YCbCr { 0 } else { 1 }, false, &t); + enc.symbol(&mut state, p.bits as i32, false, &t); + enc.bit(p.chroma_planes, &mut state[0], &t); + enc.symbol(&mut state, i32::from(p.shift.0), false, &t); + enc.symbol(&mut state, i32::from(p.shift.1), false, &t); + enc.bit(p.extra_plane, &mut state[0], &t); + enc.symbol(&mut state, p.num_h as i32 - 1, false, &t); + enc.symbol(&mut state, p.num_v as i32 - 1, false, &t); + enc.symbol(&mut state, p.sets.len() as i32, false, &t); + for set in &p.sets { + for table in &set.tables { + let mut qstate = [128u8; CONTEXT_SIZE]; + for run in quant_runs(table) { + enc.symbol(&mut qstate, run, false, &t); + } + } + } + for _ in &p.sets { + enc.bit(false, &mut state[0], &t); // states_coded + } + enc.symbol(&mut state, i32::from(p.ec), false, &t); + enc.symbol(&mut state, i32::from(p.intra), false, &t); +} + +// ---- slices ---- + +/// A slice's place in the frame, in pixels. +#[derive(Clone, Copy)] +struct SliceRect { + x: usize, + y: usize, + w: usize, + h: usize, +} + +fn slice_rect(p: &Params, width: usize, height: usize, sx: usize, sy: usize, sw: usize, sh: usize) -> Result { + let (nh, nv) = (p.num_h as usize, p.num_v as usize); + if sx + sw > nh || sy + sh > nv || sw == 0 || sh == 0 { + return Err(Error::Invalid("slice outside the slice raster")); + } + let x0 = sx * width / nh; + let x1 = (sx + sw) * width / nh; + let y0 = sy * height / nv; + let y1 = (sy + sh) * height / nv; + if x1 <= x0 || y1 <= y0 { + return Err(Error::Invalid("empty slice")); + } + Ok(SliceRect { x: x0, y: y0, w: x1 - x0, h: y1 - y0 }) +} + +/// The samples of one plane of a slice with the border of RFC 9043 section 3.1 around them: two +/// columns to the left, one to the right, two rows above. +struct Border { + buf: Vec, + stride: usize, + w: usize, +} + +impl Border { + fn new(w: usize, h: usize) -> Border { + let stride = w + 3; + Border { buf: vec![0; stride * (h + 2)], stride, w } + } + + fn at(&self, x: usize, y: usize) -> usize { + (y + 2) * self.stride + x + 2 + } + + /// Set up the left border of row `y` (the column left of it repeats the row above's first sample). + fn start_row(&mut self, y: usize) { + let row = (y + 2) * self.stride; + self.buf[row] = 0; + self.buf[row + 1] = if y > 0 { self.buf[(y + 1) * self.stride + 2] } else { 0 }; + } + + /// The right border of row `y` repeats its last sample. + fn end_row(&mut self, y: usize) { + let row = (y + 2) * self.stride; + self.buf[row + self.w + 2] = self.buf[row + self.w + 1]; + } + + /// `(context, predictor)` for sample `(x, y)`; `context` is signed (section 3.5). + fn context(&self, x: usize, y: usize, tables: &[[i32; 256]], sixteen_bit_ycbcr: bool) -> (i32, i32) { + let i = self.at(x, y); + let (b, s) = (&self.buf, self.stride); + let (l, ll, t, tl, tr, tt) = (b[i - 1], b[i - 2], b[i - s], b[i - s - 1], b[i - s + 1], b[i - 2 * s]); + let ctx = tables[0][((l - tl) & 255) as usize] + + tables[1][((tl - t) & 255) as usize] + + tables[2][((t - tr) & 255) as usize] + + tables[3][((ll - l) & 255) as usize] + + tables[4][((tt - t) & 255) as usize]; + let (pl, pt, ptl) = if sixteen_bit_ycbcr { + let s16 = |v: i32| if v >= 32768 { v - 65536 } else { v }; + (s16(l), s16(t), s16(tl)) + } else { + (l, t, tl) + }; + (ctx, median(pl, pt, pl + pt - ptl)) + } +} + +fn median(a: i32, b: i32, c: i32) -> i32 { + a.max(b).min(a.min(b).max(c)) +} + +/// The reversible colour transform bits: a plane's samples have `bits + 1` bits. +fn rct_swapped(p: &Params) -> bool { + p.colorspace == Colorspace::Rgb && (9..=15).contains(&p.bits) && !p.extra_plane +} + +fn coded_bits(p: &Params) -> u32 { + if p.colorspace == Colorspace::Rgb { p.bits + 1 } else { p.bits } +} + +/// One plane's samples inside a slice, and which class of states codes them. +struct PlaneCoder { + border: Border, + slot: usize, +} + +/// The adaptive states of one plane class: Y, Cb and Cr together, then transparency (section 3.6). +struct ClassStates { + set: usize, + states: Vec<[u8; CONTEXT_SIZE]>, +} + +fn make_coders(p: &Params, rect: SliceRect, set_idx: &[usize]) -> Result<(Vec, Vec)> { + let mut planes = Vec::new(); + for pl in 0..p.plane_count() { + let (w, h) = plane_dims(rect.w, rect.h, p.colorspace, p.shift, pl, p.chroma_planes); + planes.push(PlaneCoder { border: Border::new(w, h), slot: p.qset_slot(pl) }); + } + let mut classes = Vec::new(); + for slot in 0..=planes.iter().map(|c| c.slot).max().unwrap_or(0) { + let set = *set_idx.get(slot).ok_or(Error::Invalid("missing quant table set index"))?; + let q = p.sets.get(set).ok_or(Error::Invalid("quant table set index out of range"))?; + let states = match p.initial_states.get(set) { + Some(Some(init)) => init.clone(), + _ => vec![[128u8; CONTEXT_SIZE]; q.context_count], + }; + classes.push(ClassStates { set, states }); + } + Ok((planes, classes)) +} + +/// The number of Quantization Table Set indexes in a slice header (section 4.6.5, version 3). +fn set_index_count(p: &Params) -> usize { + 2 + usize::from(p.extra_plane) +} + +fn check_frame_size(p: &Params, w: u32, h: u32) -> Result<()> { + if w == 0 || h == 0 { + return Err(Error::Invalid("empty frame")); + } + let shape = PictureShape { + width: u64::from(w), + height: u64::from(h), + shift_x: u32::from(p.shift.0.min(1)), + shift_y: u32::from(p.shift.1.min(1)), + chroma_planes: if p.chroma_planes { 2 } else { 0 }, + bytes_per_sample: 2, + }; + // The decoded planes and, while a slice decodes, its bordered `i32` copy (2 x the samples at most). + if !shape.fits(5, DECODE_MEMORY_BUDGET) { + return Err(Error::Unsupported("frame size above the memory budget")); + } + if p.shift.0 > 1 || p.shift.1 > 1 { + // Subsampling beyond 2:1 is legal FFV1 (e.g. 4:1:1) but the budget model above is for 2:1. + let extra = (w as u64).checked_mul(h as u64).ok_or(Error::Invalid("frame too large"))?; + if extra > 1 << 28 { + return Err(Error::Unsupported("frame size above the memory budget")); + } + } + Ok(()) +} + +/// A decoder for one stream. +pub struct Decoder { + params: Params, + width: u32, + height: u32, +} + +impl Decoder { + /// Build a decoder from the container's configuration record and the frame size. + pub fn new(config_record: &[u8], width: u32, height: u32) -> Result { + if config_record.len() < 5 { + return Err(Error::Truncated); + } + let (body, _crc) = config_record.split_at(config_record.len() - 4); + if crc32(config_record) != 0 { + return Err(Error::Crc("the configuration record")); + } + let mut rac = Rac::new(body); + let params = parse_params(&mut rac)?; + if rac.consumed() > body.len() + 2 { + return Err(Error::Truncated); + } + check_frame_size(¶ms, width, height)?; + Ok(Decoder { params, width, height }) + } + + /// Decode one frame (a packet from the container). + pub fn decode(&self, packet: &[u8]) -> Result { + let p = &self.params; + let (w, h) = (self.width as usize, self.height as usize); + let planes = p.plane_count(); + let mut frame = Frame { + width: self.width, + height: self.height, + colorspace: p.colorspace, + bit_depth: p.bits as u8, + chroma_shift: if p.chroma_planes { p.shift } else { (0, 0) }, + planes: Vec::with_capacity(planes), + }; + for pl in 0..planes { + let (pw, ph) = plane_dims(w, h, p.colorspace, p.shift, pl, p.chroma_planes); + let mut v = Vec::new(); + v.try_reserve_exact(pw * ph).map_err(|_| Error::Unsupported("frame size above the memory budget"))?; + v.resize(pw * ph, 0u16); + frame.planes.push(v); + } + // Slices are found from the end of the packet through their footers (section 4.9). + let trailer = if p.ec { 8 } else { 3 }; + let slice_count = p.num_h as usize * p.num_v as usize; + let mut end = packet.len(); + let mut spans = Vec::with_capacity(slice_count); + for _ in 0..slice_count { + if end < trailer { + return Err(Error::Truncated); + } + let f = end - trailer; + let size = usize::from(packet[f]) << 16 | usize::from(packet[f + 1]) << 8 | usize::from(packet[f + 2]); + let start = f.checked_sub(size).ok_or(Error::Truncated)?; + spans.push((start, end)); + end = start; + } + spans.reverse(); + for (n, &(start, stop)) in spans.iter().enumerate() { + let slice = &packet[start..stop]; + if p.ec && crc32(slice) != 0 { + return Err(Error::Crc("a slice")); + } + self.decode_slice(slice, n == 0, &mut frame)?; + } + Ok(frame) + } + + fn decode_slice(&self, slice: &[u8], first: bool, frame: &mut Frame) -> Result<()> { + let p = &self.params; + let t = &p.transitions; + let data = &slice[..slice.len() - if p.ec { 8 } else { 3 }]; + let mut rac = Rac::new(data); + // The keyframe flag is the first symbol of the first slice. + // The whole slice, header included, is coded with the stream's state transition table. + let t0 = t; + if first { + let mut ks = 128u8; + if !rac.bit(&mut ks, t0) { + return Err(Error::Unsupported("inter frames (keyframe 0)")); + } + } + let mut hs = [128u8; CONTEXT_SIZE]; + let sx = rac.symbol(&mut hs, false, t0) as usize; + let sy = rac.symbol(&mut hs, false, t0) as usize; + let sw = rac.symbol(&mut hs, false, t0) as usize + 1; + let sh = rac.symbol(&mut hs, false, t0) as usize + 1; + let set_idx: Vec = (0..set_index_count(p)).map(|_| rac.symbol(&mut hs, false, t0).max(0) as usize).collect(); + let _picture_structure = rac.symbol(&mut hs, false, t0); + let _sar = (rac.symbol(&mut hs, false, t0), rac.symbol(&mut hs, false, t0)); + let rect = slice_rect(p, self.width as usize, self.height as usize, sx, sy, sw, sh)?; + let (sx_mask, sy_mask) = ((1usize << p.shift.0) - 1, (1usize << p.shift.1) - 1); + if p.colorspace == Colorspace::YCbCr && p.chroma_planes && (rect.x & sx_mask != 0 || rect.y & sy_mask != 0) { + return Err(Error::Unsupported("slice boundaries that are not multiples of the chroma subsampling")); + } + let (mut coders, mut classes) = make_coders(p, rect, &set_idx)?; + let bits = coded_bits(p); + let mask = (1i32 << bits) - 1; + let sixteen = p.colorspace == Colorspace::YCbCr && p.bits == 16; + let read = |rac: &mut Rac, c: &mut PlaneCoder, cls: &mut ClassStates, x: usize, y: usize| -> i32 { + let q = &p.sets[cls.set]; + let (ctx, pred) = c.border.context(x, y, &q.tables, sixteen); + let (idx, neg) = if ctx < 0 { ((-ctx) as usize, true) } else { (ctx as usize, false) }; + let mut d = rac.symbol(&mut cls.states[idx.min(q.context_count - 1)], true, t); + if neg { + d = -d; + } + let v = (pred + d) & mask; + let at = c.border.at(x, y); + c.border.buf[at] = v; + v + }; + match p.colorspace { + Colorspace::YCbCr => { + for (pl, c) in coders.iter_mut().enumerate() { + let (pw, ph) = plane_dims(rect.w, rect.h, p.colorspace, p.shift, pl, p.chroma_planes); + for y in 0..ph { + c.border.start_row(y); + for x in 0..pw { + read(&mut rac, c, &mut classes[c.slot], x, y); + } + c.border.end_row(y); + } + } + } + Colorspace::Rgb => { + for y in 0..rect.h { + for c in coders.iter_mut() { + c.border.start_row(y); + for x in 0..rect.w { + read(&mut rac, c, &mut classes[c.slot], x, y); + } + c.border.end_row(y); + } + } + } + } + self.store(&coders, rect, frame); + Ok(()) + } + + /// Copy a decoded slice into the frame, undoing the colour transform for RGB. + fn store(&self, coders: &[PlaneCoder], rect: SliceRect, frame: &mut Frame) { + let p = &self.params; + if p.colorspace == Colorspace::YCbCr { + for (pl, c) in coders.iter().enumerate() { + let (pw, ph) = plane_dims(rect.w, rect.h, p.colorspace, p.shift, pl, p.chroma_planes); + let (fw, _) = plane_dims(self.width as usize, self.height as usize, p.colorspace, p.shift, pl, p.chroma_planes); + let (ox, oy) = if p.chroma_planes && (pl == 1 || pl == 2) { (rect.x >> p.shift.0, rect.y >> p.shift.1) } else { (rect.x, rect.y) }; + for y in 0..ph { + for x in 0..pw { + frame.planes[pl][(oy + y) * fw + ox + x] = c.border.buf[c.border.at(x, y)] as u16; + } + } + } + return; + } + let swapped = rct_swapped(p); + let off = 1i32 << p.bits; + let fw = self.width as usize; + for y in 0..rect.h { + for x in 0..rect.w { + let get = |i: usize| coders[i].border.buf[coders[i].border.at(x, y)]; + let (yy, cb, cr) = (get(0), get(1) - off, get(2) - off); + let (r, g, b); + if swapped { + let bb = yy - ((cb + cr) >> 2); + r = cr + bb; + g = cb + bb; + b = bb; + } else { + let gg = yy - ((cb + cr) >> 2); + r = cr + gg; + g = gg; + b = cb + gg; + } + let at = (rect.y + y) * fw + rect.x + x; + frame.planes[0][at] = r as u16; + frame.planes[1][at] = g as u16; + frame.planes[2][at] = b as u16; + if coders.len() > 3 { + frame.planes[3][at] = get(3) as u16; + } + } + } + } +} + +/// Encoder settings. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct EncoderConfig { + /// Slices across the frame. + pub slices_h: u32, + /// Slices down the frame. + pub slices_v: u32, + /// Write a CRC in every slice (error detection). + pub slice_crc: bool, +} + +impl Default for EncoderConfig { + fn default() -> Self { + EncoderConfig { slices_h: 1, slices_v: 1, slice_crc: true } + } +} + +/// An encoder for frames of one fixed format. +pub struct Encoder { + params: Params, + width: u32, + height: u32, +} + +impl Encoder { + /// An encoder for frames shaped like `template` (its samples are not used). + pub fn new(template: &Frame, cfg: EncoderConfig) -> Result { + if !template.plane_count_ok() || template.width == 0 || template.height == 0 { + return Err(Error::Input("a frame has 1 to 4 planes and a nonzero size".into())); + } + if !(8..=16).contains(&template.bit_depth) { + return Err(Error::Input("bit depth must be 8 to 16".into())); + } + if cfg.slices_h == 0 + || cfg.slices_v == 0 + || cfg.slices_h > 1024 + || cfg.slices_v > 1024 + || cfg.slices_h > template.width + || cfg.slices_v > template.height + { + return Err(Error::Input("invalid slice raster".into())); + } + let n = template.planes.len(); + let (chroma_planes, extra_plane) = match (template.colorspace, n) { + (Colorspace::YCbCr, 1) => (false, false), + (Colorspace::YCbCr, 2) => (false, true), + (Colorspace::YCbCr, 3) => (true, false), + (Colorspace::YCbCr, _) => (true, true), + (Colorspace::Rgb, 3) => (true, false), + (Colorspace::Rgb, 4) => (true, true), + (Colorspace::Rgb, _) => return Err(Error::Input("RGB frames have 3 or 4 planes".into())), + }; + let shift = if template.colorspace == Colorspace::YCbCr && chroma_planes { template.chroma_shift } else { (0, 0) }; + if shift.0 > 3 || shift.1 > 3 { + return Err(Error::Input("chroma subsampling above 8:1".into())); + } + if template.colorspace == Colorspace::Rgb && template.bit_depth > 15 { + return Err(Error::Input("RGB above 15 bits needs 17-bit coded samples".into())); + } + let params = Params { + coder_type: 1, + transitions: Transitions::default_table(), + colorspace: template.colorspace, + bits: u32::from(template.bit_depth), + chroma_planes, + shift, + extra_plane, + num_h: cfg.slices_h, + num_v: cfg.slices_v, + sets: vec![default_quant_set()], + initial_states: vec![None], + ec: cfg.slice_crc, + intra: true, + }; + // Every slice boundary must be a multiple of the chroma subsampling, so the decoder accepts it. + for i in 0..=cfg.slices_h as usize { + if params.chroma_planes && (i * template.width as usize / cfg.slices_h as usize) & ((1 << shift.0) - 1) != 0 && i != cfg.slices_h as usize + { + return Err(Error::Input("slice boundaries must be multiples of the chroma subsampling".into())); + } + } + for i in 0..cfg.slices_v as usize { + if params.chroma_planes && (i * template.height as usize / cfg.slices_v as usize) & ((1 << shift.1) - 1) != 0 { + return Err(Error::Input("slice boundaries must be multiples of the chroma subsampling".into())); + } + } + Ok(Encoder { params, width: template.width, height: template.height }) + } + + /// The configuration record for the container (`CodecPrivate` and the like). + pub fn config_record(&self) -> Vec { + let mut enc = RacEnc::new(); + write_params(&mut enc, &self.params); + let mut out = enc.finish(); + let crc = crc32(&out); + out.extend_from_slice(&crc.to_be_bytes()); + out + } + + /// Encode one frame as a keyframe packet. + pub fn encode(&self, frame: &Frame) -> Result> { + let p = &self.params; + if frame.width != self.width || frame.height != self.height || frame.colorspace != p.colorspace || u32::from(frame.bit_depth) != p.bits { + return Err(Error::Input("the frame does not match the encoder's format".into())); + } + if frame.planes.len() != p.plane_count() { + return Err(Error::Input("the frame has the wrong number of planes".into())); + } + let shift = if p.chroma_planes { p.shift } else { (0, 0) }; + if p.colorspace == Colorspace::YCbCr && p.chroma_planes && frame.chroma_shift != shift { + return Err(Error::Input("the chroma subsampling does not match".into())); + } + for (pl, plane) in frame.planes.iter().enumerate() { + let (w, h) = frame.dims(pl, p.chroma_planes); + if plane.len() != w * h { + return Err(Error::Input(format!("plane {pl} has {} samples, expected {}", plane.len(), w * h))); + } + if plane.iter().any(|&v| u32::from(v) >> p.bits != 0) { + return Err(Error::Input(format!("plane {pl} has a sample above {} bits", p.bits))); + } + } + let mut out = Vec::new(); + for sy in 0..p.num_v as usize { + for sx in 0..p.num_h as usize { + let rect = slice_rect(p, self.width as usize, self.height as usize, sx, sy, 1, 1)?; + let mut slice = self.encode_slice(frame, rect, sx, sy, out.is_empty())?; + let size = slice.len(); + slice.extend_from_slice(&(size as u32).to_be_bytes()[1..]); + if p.ec { + slice.push(0); // error_status + let crc = crc32(&slice); + slice.extend_from_slice(&crc.to_be_bytes()); + } + out.extend_from_slice(&slice); + } + } + Ok(out) + } + + fn encode_slice(&self, frame: &Frame, rect: SliceRect, sx: usize, sy: usize, first: bool) -> Result> { + let p = &self.params; + let t = &p.transitions; + let t0 = t; + let mut rac = RacEnc::new(); + if first { + let mut ks = 128u8; + rac.bit(true, &mut ks, t0); + } + let mut hs = [128u8; CONTEXT_SIZE]; + for v in [sx as i32, sy as i32, 0, 0] { + rac.symbol(&mut hs, v, false, t0); + } + let set_idx = vec![0usize; set_index_count(p)]; + for &s in &set_idx { + rac.symbol(&mut hs, s as i32, false, t0); + } + for v in [0, 0, 0] { + rac.symbol(&mut hs, v, false, t0); // picture_structure, sar_num, sar_den + } + let (mut coders, mut classes) = make_coders(p, rect, &set_idx)?; + let bits = coded_bits(p); + let half = 1i32 << (bits - 1); + let mask = (1i32 << bits) - 1; + let sixteen = p.colorspace == Colorspace::YCbCr && p.bits == 16; + // The samples to code, one bordered plane's worth per coder. + let values = self.slice_values(frame, rect); + let write = |rac: &mut RacEnc, c: &mut PlaneCoder, cls: &mut ClassStates, x: usize, y: usize, sample: i32| { + let q = &p.sets[cls.set]; + let (ctx, pred) = c.border.context(x, y, &q.tables, sixteen); + let mut d = ((sample - pred + half) & mask) - half; + let (idx, neg) = if ctx < 0 { ((-ctx) as usize, true) } else { (ctx as usize, false) }; + if neg { + d = -d; + } + rac.symbol(&mut cls.states[idx.min(q.context_count - 1)], d, true, t); + let at = c.border.at(x, y); + c.border.buf[at] = sample; + }; + match p.colorspace { + Colorspace::YCbCr => { + for (pl, c) in coders.iter_mut().enumerate() { + let (pw, ph) = plane_dims(rect.w, rect.h, p.colorspace, p.shift, pl, p.chroma_planes); + for y in 0..ph { + c.border.start_row(y); + for x in 0..pw { + write(&mut rac, c, &mut classes[c.slot], x, y, values[pl][y * pw + x]); + } + c.border.end_row(y); + } + } + } + Colorspace::Rgb => { + for y in 0..rect.h { + for (pl, c) in coders.iter_mut().enumerate() { + c.border.start_row(y); + for x in 0..rect.w { + write(&mut rac, c, &mut classes[c.slot], x, y, values[pl][y * rect.w + x]); + } + c.border.end_row(y); + } + } + } + } + Ok(rac.finish()) + } + + /// The slice's samples per coded plane (after the colour transform for RGB). + fn slice_values(&self, frame: &Frame, rect: SliceRect) -> Vec> { + let p = &self.params; + let fw = self.width as usize; + if p.colorspace == Colorspace::YCbCr { + return (0..p.plane_count()) + .map(|pl| { + let (pw, ph) = plane_dims(rect.w, rect.h, p.colorspace, p.shift, pl, p.chroma_planes); + let (full_w, _) = plane_dims(fw, self.height as usize, p.colorspace, p.shift, pl, p.chroma_planes); + let (ox, oy) = + if p.chroma_planes && (pl == 1 || pl == 2) { (rect.x >> p.shift.0, rect.y >> p.shift.1) } else { (rect.x, rect.y) }; + let mut v = Vec::with_capacity(pw * ph); + for y in 0..ph { + for x in 0..pw { + v.push(i32::from(frame.planes[pl][(oy + y) * full_w + ox + x])); + } + } + v + }) + .collect(); + } + let swapped = rct_swapped(p); + let off = 1i32 << p.bits; + let mut planes = vec![Vec::with_capacity(rect.w * rect.h); p.plane_count()]; + for y in 0..rect.h { + for x in 0..rect.w { + let at = (rect.y + y) * fw + rect.x + x; + let (r, g, b) = (i32::from(frame.planes[0][at]), i32::from(frame.planes[1][at]), i32::from(frame.planes[2][at])); + let (yy, cb, cr) = if swapped { + let (cb, cr) = (g - b, r - b); + (b + ((cb + cr) >> 2), cb, cr) + } else { + let (cb, cr) = (b - g, r - g); + (g + ((cb + cr) >> 2), cb, cr) + }; + planes[0].push(yy); + planes[1].push(cb + off); + planes[2].push(cr + off); + if p.extra_plane { + planes[3].push(i32::from(frame.planes[3][at])); + } + } + } + planes + } +} diff --git a/crates/video/src/ffv1/rc.rs b/crates/video/src/ffv1/rc.rs new file mode 100644 index 000000000..fa2a16ee4 --- /dev/null +++ b/crates/video/src/ffv1/rc.rs @@ -0,0 +1,229 @@ +//! The FFV1 range coder (RFC 9043 section 3.8.1): binary decisions with adaptive 8-bit states, +//! and the signed/unsigned integer coding built on them. + +/// RFC 9043 Figure 24: the default state transition table for a coded 1. +const DEFAULT_ONE_STATE: [u8; 256] = [ + 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, + 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, + 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, + 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, + 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, + 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, + 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, + 0, 0, 0, 0, +]; + +/// The state machine: the next state after a coded 0 or 1. +#[derive(Clone)] +pub(crate) struct Transitions { + one: [u8; 256], + zero: [u8; 256], +} + +impl Transitions { + /// The default table plus `delta` (`state_transition_delta`, indexed 1 to 255). + pub(crate) fn new(delta: &[i32; 256]) -> Transitions { + let mut one = [0u8; 256]; + for (i, o) in one.iter_mut().enumerate() { + *o = (i32::from(DEFAULT_ONE_STATE[i]) + delta[i]) as u8; + } + let mut zero = [0u8; 256]; + for i in 1..256 { + zero[i] = (256 - i32::from(one[256 - i])) as u8; + } + Transitions { one, zero } + } + + pub(crate) fn default_table() -> Transitions { + Transitions::new(&[0; 256]) + } +} + +/// Reads binary decisions from `data`; bytes past its end read as 0 (the "Closed" mode of 3.8.1.1.1). +pub(crate) struct Decoder<'a> { + data: &'a [u8], + pos: usize, + low: u32, + range: u32, +} + +impl<'a> Decoder<'a> { + pub(crate) fn new(data: &'a [u8]) -> Decoder<'a> { + let b0 = u32::from(data.first().copied().unwrap_or(0)); + let b1 = u32::from(data.get(1).copied().unwrap_or(0)); + let mut low = b0 << 8 | b1; + let range = 0xFF00; + if low >= range { + low = range; + } + Decoder { data, pos: 2, low, range } + } + + /// Bytes the coder has read so far. + pub(crate) fn consumed(&self) -> usize { + self.pos + } + + fn refill(&mut self) { + if self.range < 256 { + self.range <<= 8; + self.low <<= 8; + self.low += u32::from(self.data.get(self.pos).copied().unwrap_or(0)); + self.pos += 1; + } + } + + pub(crate) fn bit(&mut self, state: &mut u8, t: &Transitions) -> bool { + let off = (self.range * u32::from(*state)) >> 8; + self.range -= off; + if self.low < self.range { + *state = t.zero[usize::from(*state)]; + self.refill(); + false + } else { + self.low -= self.range; + *state = t.one[usize::from(*state)]; + self.range = off; + self.refill(); + true + } + } + + /// RFC 9043 Figure 21: `get_symbol`. + pub(crate) fn symbol(&mut self, state: &mut [u8; 32], signed: bool, t: &Transitions) -> i32 { + if self.bit(&mut state[0], t) { + return 0; + } + let mut e = 0usize; + while self.bit(&mut state[1 + e.min(9)], t) { + e += 1; + if e > 24 { + // No valid stream codes a magnitude this large; stop instead of spinning on garbage. + return 0; + } + } + let mut a = 1i32; + for i in (0..e).rev() { + a = a * 2 + i32::from(self.bit(&mut state[22 + i.min(9)], t)); + } + if signed && self.bit(&mut state[11 + e.min(10)], t) { -a } else { a } + } +} + +/// Writes binary decisions; the inverse of [`Decoder`]. +pub(crate) struct Encoder { + out: Vec, + low: u32, + range: u32, +} + +impl Encoder { + pub(crate) fn new() -> Encoder { + Encoder { out: Vec::new(), low: 0, range: 0xFF00 } + } + + fn shift(&mut self) { + while self.range < 256 { + // Bits 15..8 of `low` are final once a carry (bit 16) is added to the bytes before them. + let carry = self.low >> 16; + let byte = ((self.low >> 8) & 0xFF) as u8; + if carry != 0 { + for b in self.out.iter_mut().rev() { + let (v, over) = b.overflowing_add(1); + *b = v; + if !over { + break; + } + } + } + self.out.push(byte); + self.low = (self.low & 0xFF) << 8; + self.range <<= 8; + } + } + + pub(crate) fn bit(&mut self, bit: bool, state: &mut u8, t: &Transitions) { + let off = (self.range * u32::from(*state)) >> 8; + if bit { + self.low += self.range - off; + self.range = off; + *state = t.one[usize::from(*state)]; + } else { + self.range -= off; + *state = t.zero[usize::from(*state)]; + } + self.shift(); + } + + pub(crate) fn symbol(&mut self, state: &mut [u8; 32], v: i32, signed: bool, t: &Transitions) { + if v == 0 { + self.bit(true, &mut state[0], t); + return; + } + self.bit(false, &mut state[0], t); + let a = v.unsigned_abs(); + let e = (31 - a.leading_zeros()) as usize; + for i in 0..e { + self.bit(true, &mut state[1 + i.min(9)], t); + } + self.bit(false, &mut state[1 + e.min(9)], t); + for i in (0..e).rev() { + self.bit((a >> i) & 1 == 1, &mut state[22 + i.min(9)], t); + } + if signed { + self.bit(v < 0, &mut state[11 + e.min(10)], t); + } + } + + /// Finish the stream with the two bytes of the *top* of the final range: whatever bytes follow + /// (zeros, or a CRC in a container's configuration record) still decode inside the range. + pub(crate) fn finish(mut self) -> Vec { + self.low += self.range - 1; + let carry = self.low >> 16; + if carry != 0 { + for b in self.out.iter_mut().rev() { + let (v, over) = b.overflowing_add(1); + *b = v; + if !over { + break; + } + } + } + self.out.push(((self.low >> 8) & 0xFF) as u8); + self.out.push((self.low & 0xFF) as u8); + self.out + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn symbols_round_trip_through_the_range_coder() { + let t = Transitions::default_table(); + let values: Vec = (0..4000).map(|i| ((i * 7919) % 2001) - 1000).chain([0, 0, 0, 65535, -65535, 1, -1]).collect(); + let mut enc = Encoder::new(); + let mut st = [128u8; 32]; + for &v in &values { + enc.symbol(&mut st, v, true, &t); + } + let bytes = enc.finish(); + let mut dec = Decoder::new(&bytes); + let mut st = [128u8; 32]; + for &v in &values { + assert_eq!(dec.symbol(&mut st, true, &t), v); + } + } + + #[test] + fn transition_tables_follow_the_rfc() { + let t = Transitions::default_table(); + assert_eq!(t.one[8], 20); + assert_eq!(t.one[247], 248); + // zero_state_i = 256 - one_state_(256 - i) + assert_eq!(t.zero[128], (256 - i32::from(t.one[128])) as u8); + assert_eq!(t.zero[1], (256 - i32::from(t.one[255])) as u8); + } +} diff --git a/crates/video/src/lib.rs b/crates/video/src/lib.rs index 0d8625bbd..8202a93f8 100644 --- a/crates/video/src/lib.rs +++ b/crates/video/src/lib.rs @@ -10,6 +10,7 @@ //! | `vp8` | [`vp8`] | VP8 (RFC 6386): decoder (`vp8-decode`) and key-frame encoder (`vp8-encode`) | //! | `mpeg2` | [`mpeg2`] | MPEG-2 video (ITU-T H.262) and MPEG-1 video: decoder (`mpeg2-decode`) and MPEG-2 I/P/B encoder (`mpeg2-encode`) | //! | `mpeg4` | [`mpeg4`] | MPEG-4 Part 2 video (ISO/IEC 14496-2, Xvid/DivX): Simple profile encoder (`mpeg4-encode`) | +//! | `ffv1` | [`ffv1`] | FFV1 lossless video (RFC 9043): version 3, range coder, decoder and encoder | //! | `prores` | [`prores`] | Apple ProRes (SMPTE RDD 36): decoder and encoder | //! | `h264` | [`h264`] | ITU-T H.264 / AVC: decoder (`h264-decode`) and encoder (`h264-encode`) | //! | `hevc` | [`hevc`] | ITU-T H.265 / HEVC: decoder (`hevc-decode`) and encoder (`hevc-encode`) | @@ -41,6 +42,9 @@ pub mod mpeg2; #[cfg(feature = "mpeg4-encode")] pub mod mpeg4; +#[cfg(feature = "ffv1")] +pub mod ffv1; + #[cfg(feature = "prores")] pub mod prores; diff --git a/crates/video/tests/ffv1.rs b/crates/video/tests/ffv1.rs new file mode 100644 index 000000000..1d5c73c0b --- /dev/null +++ b/crates/video/tests/ffv1.rs @@ -0,0 +1,304 @@ +//! FFV1 tests: round trips, hostile input, and ffmpeg as the reference (oracle; skipped when ffmpeg +//! is absent, a failure with `CRAFT_MEDIA_REQUIRE_ORACLES=1`). +//! +//! - Our packets, in an AVI with our configuration record, decode in ffmpeg to our source pixels. +//! - ffmpeg's FFV1 files (version 3, range coder, several pixel formats and slice counts) decode +//! with our decoder to ffmpeg's pixels. + +use craft_media::avi::{AviWriter, MuxOptions, StreamSpec, VideoSpec}; +use craft_video::ffv1::{Colorspace, Decoder, Encoder, EncoderConfig, Error, Frame}; +use std::path::Path; +use std::process::Command; + +fn noise(seed: &mut u32, mask: u32) -> u16 { + *seed ^= *seed << 13; + *seed ^= *seed >> 17; + *seed ^= *seed << 5; + (*seed & mask) as u16 +} + +/// A frame of smooth gradients, noise bursts and flat areas. +fn frame(w: u32, h: u32, cs: Colorspace, bits: u8, shift: (u8, u8), planes: usize) -> Frame { + let max = (1u32 << bits) - 1; + let mut seed = 0x9E37_79B9u32; + let mut ps = Vec::new(); + for p in 0..planes { + let (pw, ph) = if cs == Colorspace::YCbCr && (p == 1 || p == 2) && planes >= 3 { + ((w as usize).div_ceil(1 << shift.0), (h as usize).div_ceil(1 << shift.1)) + } else { + (w as usize, h as usize) + }; + let mut v = Vec::with_capacity(pw * ph); + for y in 0..ph { + for x in 0..pw { + let s = match (y / 11 + p) % 3 { + 0 => ((x * 5 + y * 3 + p * 40) as u32 * max / 600) % (max + 1), + 1 => noise(&mut seed, max) as u32, + _ => max / 3, + }; + v.push(s as u16); + } + } + ps.push(v); + } + Frame { width: w, height: h, colorspace: cs, bit_depth: bits, chroma_shift: shift, planes: ps } +} + +fn round_trip(f: &Frame, cfg: EncoderConfig) -> Frame { + let enc = Encoder::new(f, cfg).expect("encoder"); + let config = enc.config_record(); + let packet = enc.encode(f).expect("encode"); + let dec = Decoder::new(&config, f.width, f.height).expect("decoder"); + dec.decode(&packet).expect("decode") +} + +#[test] +fn round_trips_every_shape() { + for (cs, bits, shift, planes) in [ + (Colorspace::YCbCr, 8u8, (0u8, 0u8), 1usize), + (Colorspace::YCbCr, 8, (1, 1), 3), + (Colorspace::YCbCr, 8, (1, 0), 3), + (Colorspace::YCbCr, 8, (0, 0), 4), + (Colorspace::YCbCr, 10, (1, 1), 3), + (Colorspace::YCbCr, 12, (0, 0), 3), + (Colorspace::YCbCr, 16, (1, 0), 3), + (Colorspace::YCbCr, 16, (0, 0), 1), + (Colorspace::Rgb, 8, (0, 0), 3), + (Colorspace::Rgb, 8, (0, 0), 4), + (Colorspace::Rgb, 10, (0, 0), 3), + (Colorspace::Rgb, 12, (0, 0), 4), + (Colorspace::Rgb, 15, (0, 0), 3), + ] { + for (w, h) in [(1, 1), (3, 5), (64, 48), (67, 41)] { + let f = frame(w, h, cs, bits, shift, planes); + for crc in [true, false] { + let back = round_trip(&f, EncoderConfig { slice_crc: crc, ..EncoderConfig::default() }); + assert_eq!(back.planes, f.planes, "{cs:?} {bits}-bit {shift:?} {planes} planes {w}x{h} crc={crc}"); + } + } + } +} + +#[test] +fn round_trips_with_several_slices() { + let f = frame(96, 64, Colorspace::YCbCr, 8, (1, 1), 3); + for (sh, sv) in [(2, 1), (1, 2), (3, 2), (4, 4)] { + let back = round_trip(&f, EncoderConfig { slices_h: sh, slices_v: sv, slice_crc: true }); + assert_eq!(back.planes, f.planes, "{sh}x{sv} slices"); + } + let rgb = frame(50, 30, Colorspace::Rgb, 8, (0, 0), 3); + assert_eq!(round_trip(&rgb, EncoderConfig { slices_h: 3, slices_v: 2, slice_crc: true }).planes, rgb.planes); +} + +#[test] +fn encoder_refuses_what_it_cannot_write() { + let f = frame(8, 8, Colorspace::YCbCr, 8, (1, 1), 3); + assert!(Encoder::new(&f, EncoderConfig { slices_h: 0, ..EncoderConfig::default() }).is_err()); + assert!(Encoder::new(&f, EncoderConfig { slices_h: 9, ..EncoderConfig::default() }).is_err()); + let mut bad = f.clone(); + bad.bit_depth = 7; + assert!(Encoder::new(&bad, EncoderConfig::default()).is_err()); + let mut rgb16 = frame(8, 8, Colorspace::Rgb, 8, (0, 0), 3); + rgb16.bit_depth = 16; + assert!(Encoder::new(&rgb16, EncoderConfig::default()).is_err()); + let enc = Encoder::new(&f, EncoderConfig::default()).expect("encoder"); + let mut wrong = f.clone(); + wrong.planes[1].pop(); + assert!(enc.encode(&wrong).is_err()); + let mut over = f; + over.planes[0][0] = 300; + assert!(enc.encode(&over).is_err()); +} + +#[test] +fn hostile_input_is_an_error_not_a_panic() { + let f = frame(24, 18, Colorspace::YCbCr, 8, (1, 1), 3); + let enc = Encoder::new(&f, EncoderConfig { slices_h: 2, slices_v: 1, slice_crc: true }).expect("encoder"); + let config = enc.config_record(); + let packet = enc.encode(&f).expect("encode"); + // Every truncation of the configuration record and of the packet. + for n in 0..config.len() { + let _ = Decoder::new(&config[..n], 24, 18); + } + let dec = Decoder::new(&config, 24, 18).expect("decoder"); + for n in 0..packet.len() { + let _ = dec.decode(&packet[..n]); + } + // Every single-bit flip: the CRCs catch them in a packet, and none panics. + for i in 0..packet.len() { + for bit in 0..8 { + let mut c = packet.clone(); + c[i] ^= 1 << bit; + assert!(dec.decode(&c).is_err(), "bit {bit} of byte {i} went unnoticed"); + } + } + for i in 0..config.len() { + for bit in 0..8 { + let mut c = config.clone(); + c[i] ^= 1 << bit; + assert!(matches!(Decoder::new(&c, 24, 18), Err(Error::Crc(_))), "config bit {bit} of byte {i}"); + } + } + // Without slice CRCs, flips may decode to wrong pixels but must not panic. + let enc = Encoder::new(&f, EncoderConfig { slice_crc: false, ..EncoderConfig::default() }).expect("encoder"); + let (config, packet) = (enc.config_record(), enc.encode(&f).expect("encode")); + let dec = Decoder::new(&config, 24, 18).expect("decoder"); + for i in 0..packet.len() { + for bit in 0..8 { + let mut c = packet.clone(); + c[i] ^= 1 << bit; + let _ = dec.decode(&c); + } + } + // A huge frame size is refused before any allocation. + assert!(Decoder::new(&config, u32::MAX, u32::MAX).is_err()); +} + +// ---- ffmpeg ---- + +fn ffmpeg_run(ff: &Path, args: &[&str]) -> Vec { + let out = Command::new(ff).args(["-v", "error", "-nostdin", "-y"]).args(args).output().expect("run ffmpeg"); + assert!(out.status.success(), "ffmpeg {args:?}: {}", String::from_utf8_lossy(&out.stderr)); + out.stdout +} + +fn raw_bytes(f: &Frame) -> Vec { + let mut out = Vec::new(); + for p in &f.planes { + for &v in p { + if f.bit_depth == 8 { + out.push(v as u8); + } else { + out.extend_from_slice(&v.to_le_bytes()); + } + } + } + out +} + +/// `(ffmpeg pix_fmt, colourspace, bits, chroma shift, planes)`; RGB planes are G, B, R in ffmpeg's gbrp. +type Format = (&'static str, Colorspace, u8, (u8, u8), usize); + +const FORMATS: &[Format] = &[ + ("gray", Colorspace::YCbCr, 8, (0, 0), 1), + ("yuv420p", Colorspace::YCbCr, 8, (1, 1), 3), + ("yuv422p", Colorspace::YCbCr, 8, (1, 0), 3), + ("yuv444p", Colorspace::YCbCr, 8, (0, 0), 3), + ("yuv420p10le", Colorspace::YCbCr, 10, (1, 1), 3), + ("yuv444p12le", Colorspace::YCbCr, 12, (0, 0), 3), + ("gray16le", Colorspace::YCbCr, 16, (0, 0), 1), + ("yuv444p16le", Colorspace::YCbCr, 16, (0, 0), 3), + ("gbrp", Colorspace::Rgb, 8, (0, 0), 3), + ("gbrp10le", Colorspace::Rgb, 10, (0, 0), 3), + ("gbrp12le", Colorspace::Rgb, 12, (0, 0), 3), + ("yuva444p", Colorspace::YCbCr, 8, (0, 0), 4), + ("gbrap", Colorspace::Rgb, 8, (0, 0), 4), + ("gbrap12le", Colorspace::Rgb, 12, (0, 0), 4), +]; + +/// Formats only ffmpeg writes: our encoder stops at 15-bit RGB (the transform needs 17 bits). +const DECODE_ONLY: &[Format] = &[("gbrp16le", Colorspace::Rgb, 16, (0, 0), 3), ("yuv411p", Colorspace::YCbCr, 8, (2, 0), 3)]; + +/// ffmpeg's planar RGB is G, B, R (then alpha); ours is R, G, B (then alpha). +fn gbr_order(planes: &[Vec]) -> Vec> { + let mut v = vec![planes[1].clone(), planes[2].clone(), planes[0].clone()]; + v.extend(planes[3..].iter().cloned()); + v +} + +fn avi_with(frame: &Frame, config: &[u8], packet: &[u8]) -> Vec { + let mut spec = VideoSpec::new(*b"FFV1", frame.width, frame.height, 25, 1); + spec.format.extra = config.to_vec(); + let mut w = AviWriter::new(std::io::Cursor::new(Vec::new()), vec![StreamSpec::Video(spec)], MuxOptions::default()).expect("writer"); + w.write_chunk(0, packet, true).expect("chunk"); + w.finish().expect("finish").into_inner() +} + +#[test] +fn ffmpeg_decodes_our_packets_to_our_pixels() { + let Some(ff) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("ffv1"); + for &(pix_fmt, cs, bits, shift, planes) in FORMATS { + for (slices_h, slices_v) in [(1, 1), (2, 2), (3, 2)] { + let f = frame(96, 48, cs, bits, shift, planes); + let enc = Encoder::new(&f, EncoderConfig { slices_h, slices_v, slice_crc: true }).expect("encoder"); + let avi = avi_with(&f, &enc.config_record(), &enc.encode(&f).expect("encode")); + let path = dir.join(format!("ours_{pix_fmt}_{slices_h}x{slices_v}.avi")); + std::fs::write(&path, avi).expect("write"); + let theirs = ffmpeg_run(&ff, &["-i", path.to_str().expect("path"), "-f", "rawvideo", "-pix_fmt", pix_fmt, "-"]); + // gbrp is planar G, B, R; our RGB planes are R, G, B. + let mut want = f.clone(); + if cs == Colorspace::Rgb { + want.planes = gbr_order(&f.planes); + } + assert!(theirs == raw_bytes(&want), "{pix_fmt} {slices_h}x{slices_v}: ffmpeg's pixels differ from ours"); + } + } +} + +#[test] +fn our_decoder_reads_ffmpegs_files() { + let Some(ff) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("ffv1"); + for &(pix_fmt, cs, bits, shift, planes) in FORMATS.iter().chain(DECODE_ONLY) { + for slices in ["1", "4", "6"] { + for (ctx, extra) in [("0", false), ("1", true)] { + let path = dir.join(format!("ffmpeg_{pix_fmt}_s{slices}_c{ctx}.avi")); + let lavfi = "testsrc2=size=96x48:rate=1"; + ffmpeg_run( + &ff, + &[ + "-f", + "lavfi", + "-i", + lavfi, + "-frames:v", + "1", + "-vf", + &format!("format={pix_fmt}"), + "-c:v", + "ffv1", + "-level", + "3", + "-coder", + "1", + "-context", + ctx, + "-slices", + slices, + "-slicecrc", + "1", + "-g", + "1", + path.to_str().expect("path"), + ], + ); + let _ = extra; + let file = std::fs::read(&path).expect("read"); + let avi = craft_media::avi::open(&file[..]).expect("open avi"); + let stream = &avi.streams[0]; + let config = stream.video.as_ref().expect("video").extra.clone(); + let chunk = stream.chunks[0]; + let packet = file[chunk.offset as usize..chunk.offset as usize + chunk.size as usize].to_vec(); + let dec = Decoder::new(&config, 96, 48).unwrap_or_else(|e| panic!("{pix_fmt} s{slices} c{ctx}: config: {e}")); + let got = dec.decode(&packet).unwrap_or_else(|e| panic!("{pix_fmt} s{slices} c{ctx}: decode: {e}")); + let theirs = ffmpeg_run(&ff, &["-i", path.to_str().expect("path"), "-f", "rawvideo", "-pix_fmt", pix_fmt, "-"]); + let mut want = got.clone(); + if cs == Colorspace::Rgb { + want.planes = gbr_order(&got.planes); + } + assert_eq!((got.colorspace, got.bit_depth, got.chroma_shift, got.planes.len()), (cs, bits, shift, planes), "{pix_fmt}"); + if theirs != raw_bytes(&want) { + let mut off = 0; + for (i, p) in want.planes.iter().enumerate() { + let n = p.len() * if bits == 8 { 1 } else { 2 }; + let same = theirs[off..off + n] == raw_bytes(&Frame { planes: vec![p.clone()], ..want.clone() })[..]; + eprintln!("{pix_fmt} s{slices} c{ctx}: plane {i} {}", if same { "equal" } else { "DIFFERENT" }); + off += n; + } + panic!("{pix_fmt} s{slices} c{ctx}: our pixels differ from ffmpeg's"); + } + } + } + } +} From 51a35cecb7b3d0416f0195032bbcc7f960731688 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 13:27:34 +0100 Subject: [PATCH 018/150] flow: keep the old paragraph direction in the tracked formatting-change snapshot Co-Authored-By: Claude Sonnet 5.5 --- crates/flow/src/from_doc.rs | 5 ++++- crates/rtf/src/doc/tests.rs | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/flow/src/from_doc.rs b/crates/flow/src/from_doc.rs index ea7f6fc92..717283f57 100644 --- a/crates/flow/src/from_doc.rs +++ b/crates/flow/src/from_doc.rs @@ -661,7 +661,10 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec { { (li.list, li.label) = (0, None); } - let old = Para { kind, list, align: c.old.align, page_break: c.old.page_break_before.unwrap_or(false), ..Default::default() }; + // Direction resolved like the current paragraph's: the old properties' own `w:bidi`, else + // the style's. + let rtl = doc.styles.resolve_para(&c.old).bidi; + let old = Para { kind, list, align: c.old.align, rtl, page_break: c.old.page_break_before.unwrap_or(false), ..Default::default() }; Box::new(FmtChange { mark: rev_mark(doc, c.rev), old }) }); } diff --git a/crates/rtf/src/doc/tests.rs b/crates/rtf/src/doc/tests.rs index 8313ad5b8..bf98d190a 100644 --- a/crates/rtf/src/doc/tests.rs +++ b/crates/rtf/src/doc/tests.rs @@ -94,6 +94,30 @@ fn oldpprops_keep_the_old_paragraph_direction() { assert_eq!(c.old.bidi, Some(true)); } +#[test] +fn tracked_rtl_to_ltr_change_from_a_document_keeps_the_old_direction() { + use craft_wordmodel::props::{CharProps, ParaProps, PropChange}; + use craft_wordmodel::{Revision, RevisionKind}; + // Document origin (not RTF): now LTR, before RTL. + let mut p = craft_wordmodel::Paragraph::with_text("A", CharProps::default()); + p.props.bidi = Some(false); + p.props.fmt_change = PropChange::boxed(0, ParaProps { bidi: Some(true), ..Default::default() }); + let doc = doc_with(vec![p], vec![Revision::new(RevisionKind::Format, "Bob", "2025-01-02T08:05:00Z")]); + let flow = model::from_doc_tracked(&doc); + let Some(FBlock::Para(fp)) = flow.blocks.first() else { panic!("no paragraph") }; + assert!(!fp.rtl); + assert!(fp.fmt_change.as_deref().expect("change").old.rtl, "snapshot keeps RTL"); + let rtf = export(&doc); + assert!(rtf.contains("{\\*\\oldpprops"), "{rtf}"); + let back = import(rtf.as_bytes()).unwrap(); + let p = doc_paras(&back)[0]; + assert_ne!(p.props.bidi, Some(true), "current direction is LTR"); + let change = p.props.fmt_change.as_deref().expect("change read back"); + assert_eq!(change.old.bidi, Some(true)); + // Reject restores RTL. + assert_eq!(change.old.formatting().bidi, Some(true)); +} + #[test] fn stylesheet_names_take_unicode_escapes() { // \u116?=t \u105?=i \u116?=t \u108?=l \u101?=e — how Word writes a non-Latin style name. From 425bcbfaba54a72cce2adb854c758809e7f56b2f Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 14:37:01 +0100 Subject: [PATCH 019/150] MXF README: craft-raw decodes the DNG pictures, it does not develop them Co-Authored-By: Claude Opus 5.5 --- crates/media/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/media/README.md b/crates/media/README.md index 7b5e47c0e..6262976bb 100644 --- a/crates/media/README.md +++ b/crates/media/README.md @@ -700,7 +700,7 @@ let pcm = mxf.read_pcm(&file, a, 48_000, 1920)?; // planar f32, sample-exact MPEG-4 visual, JPEG 2000, DV, uncompressed, CinemaDNG (the CinemaDNG container labels, the registered MXF-GC TIFF/EP container and TIFF/EP picture coding labels, a 42h picture element type, or a TIFF header with the DNGVersion tag at the start of the essence; the DNG pictures - themselves are developed by `craft-raw`). + themselves are decoded by `craft-raw`). - **Sound.** PCM chunks (frame- or clip-wrapped) with sample counts; `read_pcm` decodes little-endian 8/16/24/32-bit PCM (wave / AES3 descriptors) and ST 331 AES3 elements (D-10). - **Timecode.** The material package timecode component (start frame count, rounded base, From cdf4a2fa6e23f80e6a8210361a79f6e9600a4056 Mon Sep 17 00:00:00 2001 From: el-j Date: Sun, 15 Nov 2026 15:11:43 +0100 Subject: [PATCH 020/150] craft-audio: native FLAC decoder (flac-decode) Adds flac::decode for whole files and flac::Decoder for single frames: RFC 9639 subframes (constant, verbatim, fixed, LPC), Rice and escaped partitions, wasted bits, all stereo modes, fixed and variable block sizes, 4 to 32 bits, CRC-8 and CRC-16 checked. Not yet wired into craft_audio::decode, which still uses symphonia for FLAC. Part of #1546 --- crates/audio/Cargo.toml | 6 + crates/audio/README.md | 1 + crates/audio/src/flac_decode.rs | 558 ++++++++++++++++++++++++++++++ crates/audio/src/lib.rs | 9 +- crates/audio/tests/flac_decode.rs | 179 ++++++++++ 5 files changed, 751 insertions(+), 2 deletions(-) create mode 100644 crates/audio/src/flac_decode.rs create mode 100644 crates/audio/tests/flac_decode.rs diff --git a/crates/audio/Cargo.toml b/crates/audio/Cargo.toml index c411a0357..a0d1070bd 100644 --- a/crates/audio/Cargo.toml +++ b/crates/audio/Cargo.toml @@ -29,6 +29,8 @@ encode = ["lossless-encode", "aac-encode", "alac-encode", "ac3-encode", "eac3-en lossless-encode = ["pcm-encode", "flac-encode"] # Encode WAV/BWF/RF64 and AIFF/AIFF-C files. pcm-encode = [] +# Decode FLAC files and frames natively (`flac::decode`, `flac::Decoder`). +flac-decode = [] # Encode FLAC files. flac-encode = ["craft-media/bitstream"] # Encode Ogg Vorbis files (desktop only: builds the reference libvorbis from its C sources). @@ -84,6 +86,10 @@ required-features = ["decode", "lossless-encode", "peaks"] name = "codecs" required-features = ["decode", "encode"] +[[test]] +name = "flac_decode" +required-features = ["flac-decode", "flac-encode"] + [[example]] name = "info" required-features = ["decode"] diff --git a/crates/audio/README.md b/crates/audio/README.md index 63251db8d..c336b4b8e 100644 --- a/crates/audio/README.md +++ b/crates/audio/README.md @@ -32,6 +32,7 @@ No default features. The shared types (`AudioBuffer`, `AudioInfo`, `ChannelLayou | `encode` | every encoder, and every format of `encode` and `FileEncoder` | `lossless-encode`, `aac-encode`, `alac-encode`, `ac3-encode`, `eac3-encode`, `mp3-encode`, `opus-encode` | | `lossless-encode` | WAV, AIFF and FLAC writing | `pcm-encode`, `flac-encode` | | `pcm-encode` | `encode`, `FileEncoder` for WAV and AIFF; `wav_float_header`; `pcm_wav_file`, `pcm_wav_header`, `pcm_aiff_file`, `pcm_aiff_header` (minimal files around integer PCM bytes, for essence embedded in AAF / OMF) | none | +| `flac-decode` | `flac::decode` (whole `.flac` files) and `flac::Decoder` (single frames, for callers that demux FLAC themselves); native, CRC-checked, 4 to 32 bits. `decode` still uses symphonia for FLAC files | none | | `flac-encode` | `encode`, `FileEncoder` for FLAC; `flac::Encoder`, the packet encoder for callers that mux FLAC themselves | craft-media `bitstream` | | `aac-decode`, `aac-encode` | `aac::Decoder`, `aac::Encoder`; with `aac-decode`, `aac::latm_config` / `latm_split` for LOAS / LATM frames (AAC in MPEG transport streams); with `aac-encode`, `encode`, `FileEncoder` for AAC in M4A | craft-media `bitstream`; `aac-encode` also `isobmff` | | `alac-decode` | `alac::Decoder` | none | diff --git a/crates/audio/src/flac_decode.rs b/crates/audio/src/flac_decode.rs new file mode 100644 index 000000000..dfb6f5fc1 --- /dev/null +++ b/crates/audio/src/flac_decode.rs @@ -0,0 +1,558 @@ +//! A native FLAC decoder (RFC 9639): [`decode`] for whole `.flac` files and [`Decoder`] for single +//! frames (the packets of FLAC in MP4, Matroska or Ogg). +//! +//! Every frame is checked against its CRC-8 and CRC-16. Subframe types constant, verbatim, fixed +//! and LPC; Rice partitions with 4- and 5-bit parameters and escaped partitions; wasted bits; +//! left/side, side/right and mid/side stereo; fixed and variable block sizes; 4 to 32 bits per +//! sample. The STREAMINFO MD5 is not verified. +//! +//! Samples come back as `i32`, one `Vec` per channel, in the FLAC channel order. + +/// What went wrong decoding FLAC. +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum DecodeError { + /// The data ends early. + #[error("truncated FLAC data")] + Truncated, + /// The data is not valid FLAC. + #[error("invalid FLAC data: {0}")] + Invalid(&'static str), + /// A frame or header failed its CRC. + #[error("FLAC CRC mismatch in {0}")] + Crc(&'static str), + /// Valid FLAC that is not decoded. + #[error("unsupported FLAC feature: {0}")] + Unsupported(&'static str), + /// The stream is longer than the sample limit. + #[error("FLAC stream exceeds the sample limit")] + TooLarge, +} + +/// `Result` of this module. +pub type Result = std::result::Result; + +/// The STREAMINFO metadata block (RFC 9639 section 8.2). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct StreamInfo { + /// Smallest block size in samples. + pub min_block_size: u16, + /// Largest block size in samples. + pub max_block_size: u16, + /// Smallest frame in bytes, 0 if unknown. + pub min_frame_size: u32, + /// Largest frame in bytes, 0 if unknown. + pub max_frame_size: u32, + /// Sample rate in Hz. + pub sample_rate: u32, + /// Channel count, 1 to 8. + pub channels: u8, + /// Bits per sample, 4 to 32. + pub bits_per_sample: u8, + /// Samples per channel in the stream, 0 if unknown. + pub total_samples: u64, + /// MD5 of the unencoded audio, all zero if not set. + pub md5: [u8; 16], +} + +impl StreamInfo { + /// Parse the 34 bytes of a STREAMINFO block body. + pub fn parse(b: &[u8]) -> Result { + let b = b.get(..34).ok_or(DecodeError::Truncated)?; + let packed = u64::from_be_bytes([b[10], b[11], b[12], b[13], b[14], b[15], b[16], b[17]]); + let info = StreamInfo { + min_block_size: u16::from_be_bytes([b[0], b[1]]), + max_block_size: u16::from_be_bytes([b[2], b[3]]), + min_frame_size: u32::from_be_bytes([0, b[4], b[5], b[6]]), + max_frame_size: u32::from_be_bytes([0, b[7], b[8], b[9]]), + sample_rate: (packed >> 44) as u32, + channels: ((packed >> 41) & 7) as u8 + 1, + bits_per_sample: ((packed >> 36) & 31) as u8 + 1, + total_samples: packed & 0xF_FFFF_FFFF, + md5: b[18..34].try_into().map_err(|_| DecodeError::Truncated)?, + }; + if info.bits_per_sample < 4 { + return Err(DecodeError::Invalid("bits per sample below 4")); + } + if info.min_block_size != 0 + && (info.min_block_size < 16 && info.min_block_size != info.max_block_size || info.max_block_size < info.min_block_size) + { + return Err(DecodeError::Invalid("inconsistent block sizes")); + } + Ok(info) + } +} + +/// One decoded frame. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Frame { + /// Sample rate in Hz. + pub sample_rate: u32, + /// Bits per sample. + pub bits_per_sample: u8, + /// `true` when `number` is the first sample's index (variable block size), `false` when it is + /// the frame's index (fixed block size). + pub variable_block_size: bool, + /// The coded number of the frame header. + pub number: u64, + /// Samples per channel in the frame. + pub block_size: usize, + /// One `Vec` per channel, after undoing any stereo decorrelation. + pub channels: Vec>, + /// Bytes the frame took, including the header and CRC footer. + pub size: usize, +} + +// ---- checksums ---- + +fn crc8(data: &[u8]) -> u8 { + let mut crc = 0u8; + for &b in data { + crc ^= b; + for _ in 0..8 { + crc = if crc & 0x80 != 0 { crc << 1 ^ 0x07 } else { crc << 1 }; + } + } + crc +} + +fn crc16(data: &[u8]) -> u16 { + let mut crc = 0u16; + for &b in data { + crc ^= u16::from(b) << 8; + for _ in 0..8 { + crc = if crc & 0x8000 != 0 { crc << 1 ^ 0x8005 } else { crc << 1 }; + } + } + crc +} + +// ---- bits ---- + +struct Bits<'a> { + data: &'a [u8], + /// Position in bits. + pos: usize, +} + +impl<'a> Bits<'a> { + fn new(data: &'a [u8]) -> Self { + Bits { data, pos: 0 } + } + + fn left(&self) -> usize { + self.data.len() * 8 - self.pos + } + + fn bit(&mut self) -> Result { + let byte = *self.data.get(self.pos / 8).ok_or(DecodeError::Truncated)?; + let v = (byte >> (7 - self.pos % 8)) & 1; + self.pos += 1; + Ok(u32::from(v)) + } + + /// `n` bits (0 to 32) as an unsigned number. + fn bits(&mut self, n: u32) -> Result { + if n as usize > self.left() { + return Err(DecodeError::Truncated); + } + let mut v = 0u64; + let mut n = n as usize; + while n > 0 { + let byte = u64::from(self.data[self.pos / 8]); + let avail = 8 - self.pos % 8; + let take = avail.min(n); + v = v << take | (byte >> (avail - take)) & ((1 << take) - 1); + self.pos += take; + n -= take; + } + Ok(v) + } + + /// `n` bits (0 to 33) as a two's complement number. + fn signed(&mut self, n: u32) -> Result { + if n == 0 { + return Ok(0); + } + let v = self.bits(n)? as i64; + Ok(if v >> (n - 1) & 1 == 1 { v - (1 << n) } else { v }) + } + + /// Zero bits up to the next one bit (which is consumed). + fn unary(&mut self) -> Result { + let mut n = 0u64; + loop { + // Skip whole zero bytes when aligned. + if self.pos.is_multiple_of(8) { + while self.data.get(self.pos / 8) == Some(&0) { + self.pos += 8; + n += 8; + } + } + if self.bit()? == 1 { + return Ok(n); + } + n += 1; + } + } + + fn align(&mut self) { + self.pos = self.pos.div_ceil(8) * 8; + } +} + +// ---- frame ---- + +const BLOCK_SIZES: [usize; 16] = [0, 192, 576, 1152, 2304, 4608, 0, 0, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768]; + +/// A frame decoder. `info` supplies what a frame header leaves to the STREAMINFO. +#[derive(Debug, Clone, Copy, Default)] +pub struct Decoder { + info: Option, +} + +impl Decoder { + /// A decoder with the stream's STREAMINFO (needed for frames whose header defers the sample + /// rate or bit depth to it). + pub fn new(info: StreamInfo) -> Decoder { + Decoder { info: Some(info) } + } + + /// A decoder without STREAMINFO: frames that defer to it are an error. + pub fn without_streaminfo() -> Decoder { + Decoder { info: None } + } + + /// Decode the frame at the start of `data`. + pub fn decode_frame(&self, data: &[u8]) -> Result { + let mut r = Bits::new(data); + if r.bits(15)? != 0x7FFC { + return Err(DecodeError::Invalid("missing frame sync code")); + } + let variable = r.bit()? == 1; + let (bs_code, sr_code) = (r.bits(4)? as usize, r.bits(4)? as usize); + let (ch_code, bps_code) = (r.bits(4)? as usize, r.bits(3)? as usize); + if r.bit()? != 0 { + return Err(DecodeError::Invalid("reserved bit set")); + } + let number = coded_number(&mut r)?; + let block_size = match bs_code { + 0 => return Err(DecodeError::Invalid("reserved block size")), + 6 => r.bits(8)? as usize + 1, + 7 => r.bits(16)? as usize + 1, + c => BLOCK_SIZES[c], + }; + let sample_rate = match sr_code { + 0 => self.info.map(|i| i.sample_rate).ok_or(DecodeError::Invalid("sample rate only in STREAMINFO"))?, + 1 => 88_200, + 2 => 176_400, + 3 => 192_000, + 4 => 8_000, + 5 => 16_000, + 6 => 22_050, + 7 => 24_000, + 8 => 32_000, + 9 => 44_100, + 10 => 48_000, + 11 => 96_000, + 12 => r.bits(8)? as u32 * 1000, + 13 => r.bits(16)? as u32, + 14 => r.bits(16)? as u32 * 10, + _ => return Err(DecodeError::Invalid("forbidden sample rate code")), + }; + let header_len = r.pos.div_ceil(8); + let stored_crc = r.bits(8)? as u8; + if crc8(&data[..header_len]) != stored_crc { + return Err(DecodeError::Crc("a frame header")); + } + let bits = match bps_code { + 0 => self.info.map(|i| u32::from(i.bits_per_sample)).ok_or(DecodeError::Invalid("bit depth only in STREAMINFO"))?, + 1 => 8, + 2 => 12, + 4 => 16, + 5 => 20, + 6 => 24, + 7 => 32, + _ => return Err(DecodeError::Invalid("reserved bit depth")), + }; + let (channels, mode) = match ch_code { + 0..=7 => (ch_code + 1, Stereo::Independent), + 8 => (2, Stereo::LeftSide), + 9 => (2, Stereo::SideRight), + 10 => (2, Stereo::MidSide), + _ => return Err(DecodeError::Invalid("reserved channel assignment")), + }; + let mut out: Vec> = Vec::with_capacity(channels); + for c in 0..channels { + let extra = match (mode, c) { + (Stereo::LeftSide, 1) | (Stereo::SideRight, 0) | (Stereo::MidSide, 1) => 1, + _ => 0, + }; + out.push(subframe(&mut r, block_size, bits + extra)?); + } + r.align(); + let size = r.pos / 8; + let crc = r.bits(16)? as u16; + if crc16(&data[..size]) != crc { + return Err(DecodeError::Crc("a frame")); + } + let size = size + 2; + if mode != Stereo::Independent { + let (first, rest) = out.split_at_mut(1); + let (a, b) = (&mut first[0], &mut rest[0]); + for (x, y) in a.iter_mut().zip(b.iter_mut()) { + match mode { + Stereo::LeftSide => *y = x.wrapping_sub(*y), + Stereo::SideRight => *x = x.wrapping_add(*y), + _ => { + let m = x.wrapping_shl(1) | (*y & 1); + let side = *y; + *x = m.wrapping_add(side) >> 1; + *y = m.wrapping_sub(side) >> 1; + } + } + } + } + let mut chans = Vec::with_capacity(channels); + for c in out { + let v: Result> = + c.into_iter().map(|s| i32::try_from(s).map_err(|_| DecodeError::Invalid("sample outside the bit depth"))).collect(); + chans.push(v?); + } + Ok(Frame { sample_rate, bits_per_sample: bits as u8, variable_block_size: variable, number, block_size, channels: chans, size }) + } +} + +#[derive(Clone, Copy, PartialEq, Eq)] +enum Stereo { + Independent, + LeftSide, + SideRight, + MidSide, +} + +/// The UTF-8-like coded number of a frame header (up to 36 bits, 7 bytes). +fn coded_number(r: &mut Bits) -> Result { + let first = r.bits(8)? as u8; + let extra = match first { + 0x00..=0x7F => return Ok(u64::from(first)), + 0xC0..=0xDF => 1, + 0xE0..=0xEF => 2, + 0xF0..=0xF7 => 3, + 0xF8..=0xFB => 4, + 0xFC..=0xFD => 5, + 0xFE => 6, + _ => return Err(DecodeError::Invalid("bad coded number")), + }; + let mut v = if extra == 6 { 0 } else { u64::from(first & (0x3F >> extra)) }; + for _ in 0..extra { + let b = r.bits(8)?; + if b & 0xC0 != 0x80 { + return Err(DecodeError::Invalid("bad coded number")); + } + v = v << 6 | (b & 0x3F); + } + Ok(v) +} + +/// One subframe: `bits` is the frame bit depth (plus 1 for a side channel). +fn subframe(r: &mut Bits, n: usize, bits: u32) -> Result> { + if r.bit()? != 0 { + return Err(DecodeError::Invalid("subframe header padding bit set")); + } + let kind = r.bits(6)? as u32; + let wasted = if r.bit()? == 1 { r.unary()? as u32 + 1 } else { 0 }; + if wasted >= bits { + return Err(DecodeError::Invalid("wasted bits leave no sample bits")); + } + let bps = bits - wasted; + let mut s = vec![0i64; n]; + match kind { + 0 => { + let v = r.signed(bps)?; + s.fill(v); + } + 1 => { + for v in s.iter_mut() { + *v = r.signed(bps)?; + } + } + 8..=12 => { + let order = (kind - 8) as usize; + warmup(r, &mut s, order, bps)?; + residual(r, &mut s, order)?; + for i in order..n { + let at = |k: usize| s[i - k]; + // Wrapping: hostile residuals can overflow; such samples fail the range check later. + let p = match order { + 0 => 0, + 1 => at(1), + 2 => at(1).wrapping_mul(2).wrapping_sub(at(2)), + 3 => at(1).wrapping_mul(3).wrapping_sub(at(2).wrapping_mul(3)).wrapping_add(at(3)), + _ => at(1).wrapping_mul(4).wrapping_sub(at(2).wrapping_mul(6)).wrapping_add(at(3).wrapping_mul(4)).wrapping_sub(at(4)), + }; + s[i] = s[i].wrapping_add(p); + } + } + 32..=63 => { + let order = (kind - 31) as usize; + warmup(r, &mut s, order, bps)?; + let precision = r.bits(4)? as u32 + 1; + if precision == 16 { + return Err(DecodeError::Invalid("forbidden coefficient precision")); + } + let shift = r.signed(5)?; + if shift < 0 { + return Err(DecodeError::Invalid("negative prediction shift")); + } + let mut coefs = [0i64; 32]; + for c in coefs.iter_mut().take(order) { + *c = r.signed(precision)?; + } + residual(r, &mut s, order)?; + for i in order..n { + let mut sum = 0i64; + for (k, &c) in coefs.iter().take(order).enumerate() { + sum = sum.wrapping_add(c.wrapping_mul(s[i - 1 - k])); + } + s[i] = s[i].wrapping_add(sum >> shift); + } + } + _ => return Err(DecodeError::Invalid("reserved subframe type")), + } + if wasted > 0 { + for v in s.iter_mut() { + *v = v.wrapping_shl(wasted); + } + } + Ok(s) +} + +fn warmup(r: &mut Bits, s: &mut [i64], order: usize, bps: u32) -> Result<()> { + if order > s.len() { + return Err(DecodeError::Invalid("predictor order above the block size")); + } + for v in s.iter_mut().take(order) { + *v = r.signed(bps)?; + } + Ok(()) +} + +/// The coded residual (RFC 9639 section 9.2.7), into `s[order..]`. +fn residual(r: &mut Bits, s: &mut [i64], order: usize) -> Result<()> { + let method = r.bits(2)?; + let (param_bits, escape) = match method { + 0 => (4, 15), + 1 => (5, 31), + _ => return Err(DecodeError::Invalid("reserved residual coding method")), + }; + let part_order = r.bits(4)? as u32; + let n = s.len(); + let parts = 1usize << part_order; + if !n.is_multiple_of(parts) { + return Err(DecodeError::Invalid("partition order does not divide the block size")); + } + let per = n >> part_order; + if per < order { + return Err(DecodeError::Invalid("partition shorter than the predictor order")); + } + let mut i = order; + for p in 0..parts { + let count = if p == 0 { per - order } else { per }; + let k = r.bits(param_bits)? as u32; + if k == escape { + let width = r.bits(5)? as u32; + for v in s[i..i + count].iter_mut() { + *v = r.signed(width)?; + } + } else { + for v in s[i..i + count].iter_mut() { + let q = r.unary()?; + if q > (1 << 32) { + return Err(DecodeError::Invalid("Rice quotient too large")); + } + let low = r.bits(k)?; + let folded = q << k | low; + *v = if folded & 1 == 0 { (folded >> 1) as i64 } else { -((folded >> 1) as i64) - 1 }; + } + } + i += count; + } + Ok(()) +} + +// ---- files ---- + +/// A decoded file. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Decoded { + /// The STREAMINFO block. + pub info: StreamInfo, + /// One `Vec` of samples per channel. + pub channels: Vec>, +} + +/// Most sample values (samples x channels) [`decode`] will produce. +pub const DEFAULT_MAX_VALUES: u64 = 1 << 28; + +/// Decode a whole `.flac` file (an ID3v2 tag in front is skipped). +pub fn decode(bytes: &[u8]) -> Result { + decode_with_limit(bytes, DEFAULT_MAX_VALUES) +} + +/// [`decode`] with a limit on the sample values (samples x channels) produced. +pub fn decode_with_limit(bytes: &[u8], max_values: u64) -> Result { + let mut pos = 0usize; + if bytes.starts_with(b"ID3") { + let size = bytes.get(6..10).ok_or(DecodeError::Truncated)?; + if size.iter().any(|&b| b & 0x80 != 0) { + return Err(DecodeError::Invalid("bad ID3v2 size")); + } + pos = 10 + size.iter().fold(0usize, |a, &b| a << 7 | usize::from(b)); + } + if bytes.get(pos..pos + 4) != Some(b"fLaC") { + return Err(DecodeError::Invalid("missing fLaC marker")); + } + pos += 4; + let mut info: Option = None; + loop { + let head = bytes.get(pos..pos + 4).ok_or(DecodeError::Truncated)?; + let last = head[0] & 0x80 != 0; + let kind = head[0] & 0x7F; + let len = usize::from(head[1]) << 16 | usize::from(head[2]) << 8 | usize::from(head[3]); + let body = bytes.get(pos + 4..pos + 4 + len).ok_or(DecodeError::Truncated)?; + match (kind, &info) { + (0, None) => info = Some(StreamInfo::parse(body)?), + (0, Some(_)) => return Err(DecodeError::Invalid("a second STREAMINFO block")), + (_, None) => return Err(DecodeError::Invalid("STREAMINFO is not the first block")), + (127, _) => return Err(DecodeError::Invalid("forbidden metadata block type")), + _ => {} + } + pos += 4 + len; + if last { + break; + } + } + let info = info.ok_or(DecodeError::Invalid("no STREAMINFO"))?; + let dec = Decoder::new(info); + let mut channels: Vec> = vec![Vec::new(); usize::from(info.channels)]; + let mut total = 0u64; + while pos + 2 <= bytes.len() && bytes[pos] == 0xFF && bytes[pos + 1] & 0xFE == 0xF8 { + let frame = dec.decode_frame(&bytes[pos..])?; + if frame.channels.len() != channels.len() { + return Err(DecodeError::Invalid("the channel count changes inside the stream")); + } + total += frame.block_size as u64; + if total.saturating_mul(channels.len() as u64) > max_values { + return Err(DecodeError::TooLarge); + } + for (dst, src) in channels.iter_mut().zip(frame.channels) { + dst.extend_from_slice(&src); + } + pos += frame.size; + } + if info.total_samples != 0 && total != info.total_samples { + return Err(DecodeError::Truncated); + } + Ok(Decoded { info, channels }) +} diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index 33dcdee3b..f14530b26 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -27,7 +27,7 @@ //! - Codec modules for packet-level use, each with its own features: [`aac`] (`aac-decode`, //! `aac-encode`), [`alac`] (`alac-decode`, `alac-encode`), [`opus`] (`opus-decode`, and //! [`opus::encoder`] with `opus-encode`), [`ac3`] (`ac3-decode`), [`mpa`] (`mpa-decode`), [`mp3`] -//! (`mp3-decode`, `mp3-encode`), [`flac`] (`flac-encode`: the packet encoder). +//! (`mp3-decode`, `mp3-encode`), [`flac`] (`flac-decode`: the frame and file decoder; `flac-encode`: the packet encoder). //! //! The shared types below build with no features. @@ -88,10 +88,15 @@ pub use write::{OGG_BITRATES, encode_ogg}; /// assert!(file.len() < 48_000 * 2 * 2 / 2); // well under half of 16-bit PCM /// # Ok::<(), craft_audio::flac::Error>(()) /// ``` -#[cfg(feature = "flac-encode")] +#[cfg(any(feature = "flac-encode", feature = "flac-decode"))] pub mod flac { + #[cfg(feature = "flac-decode")] + pub use crate::flac_decode::{DEFAULT_MAX_VALUES, DecodeError, Decoded, Decoder, Frame, StreamInfo, decode, decode_with_limit}; + #[cfg(feature = "flac-encode")] pub use crate::write::flac::encoder::{Encoder, EncoderConfig, Error, STREAMINFO_OFFSET}; } +#[cfg(feature = "flac-decode")] +mod flac_decode; pub use buffer::AudioBuffer; /// The craft-media this crate is built with, for its containers: using it rather than a separate diff --git a/crates/audio/tests/flac_decode.rs b/crates/audio/tests/flac_decode.rs new file mode 100644 index 000000000..03d6d678e --- /dev/null +++ b/crates/audio/tests/flac_decode.rs @@ -0,0 +1,179 @@ +//! The native FLAC decoder: round trips through this crate's encoder over bit depths, channel +//! counts, block sizes and effort levels; hostile input; and ffmpeg as the reference (oracle; +//! skipped when ffmpeg is absent, a failure with `CRAFT_MEDIA_REQUIRE_ORACLES=1`) for files +//! ffmpeg's encoder writes at every compression level. + +use craft_audio::flac::{DecodeError, Decoder, Encoder, EncoderConfig, STREAMINFO_OFFSET, StreamInfo, decode}; +use std::process::Command; + +fn signal(channels: usize, frames: usize, bits: u32, seed: u32) -> Vec> { + let max = if bits == 32 { i32::MAX as f64 } else { ((1i64 << (bits - 1)) - 1) as f64 }; + let mut s = seed; + (0..channels) + .map(|c| { + (0..frames) + .map(|i| { + s ^= s << 13; + s ^= s >> 17; + s ^= s << 5; + let t = i as f64; + let v = match (i / 3000) % 4 { + 0 => (t * (0.01 + 0.007 * c as f64)).sin() * 0.6 + (t * 0.31).sin() * 0.1, + 1 => (f64::from(s) / f64::from(u32::MAX) - 0.5) * 1.8, + 2 => 0.0, + _ => { + if (i / 40) % 2 == 0 { + 0.99 + } else { + -0.99 + } + } + }; + (v * max).round() as i32 + }) + .collect() + }) + .collect() +} + +fn encode(channels: &[Vec], rate: u32, bits: u8, block: u16, level: u8) -> Vec { + let mut cfg = EncoderConfig::new(rate, channels.len() as u8, bits); + cfg.block_size = block; + cfg.level = level; + let mut enc = Encoder::new(cfg).expect("encoder"); + let mut file = enc.header(&[]); + let refs: Vec<&[i32]> = channels.iter().map(Vec::as_slice).collect(); + file.extend(enc.encode(&refs).expect("encode")); + file.extend(enc.finish()); + let at = STREAMINFO_OFFSET as usize; + file[at..at + 34].copy_from_slice(&enc.streaminfo()); + file +} + +#[test] +fn round_trips_through_the_encoder() { + for (channels, bits) in [(1usize, 8u8), (2, 8), (1, 12), (2, 16), (2, 20), (3, 24), (6, 24), (8, 16), (2, 32), (1, 32)] { + for (block, level) in [(4096u16, 5u8), (16, 0), (1000, 8), (65535, 3), (576, 1)] { + let frames = 9000 + usize::from(block % 97); + let src = signal(channels, frames, u32::from(bits), 0x1234_5679 + u32::from(bits)); + let file = encode(&src, 44_100, bits, block, level); + let got = decode(&file).unwrap_or_else(|e| panic!("{channels}ch {bits}-bit block {block} level {level}: {e}")); + assert_eq!((got.info.channels as usize, got.info.bits_per_sample, got.info.sample_rate), (channels, bits, 44_100)); + assert_eq!(got.channels, src, "{channels}ch {bits}-bit block {block} level {level}"); + } + } +} + +#[test] +fn frames_decode_one_at_a_time() { + let src = signal(2, 10_000, 16, 7); + let file = encode(&src, 48_000, 16, 4096, 5); + let info = StreamInfo::parse(&file[STREAMINFO_OFFSET as usize..]).expect("streaminfo"); + let dec = Decoder::new(info); + // The first frame follows the metadata blocks; find the first position a frame decodes at. + let at = (42..file.len()).find(|&p| dec.decode_frame(&file[p..]).is_ok()).expect("a frame"); + let first = dec.decode_frame(&file[at..]).expect("frame"); + assert_eq!((first.block_size, first.number, first.sample_rate, first.bits_per_sample), (4096, 0, 48_000, 16)); + assert_eq!(first.channels[0], src[0][..4096]); + // Without the STREAMINFO a frame that spells out its own rate and depth still decodes. + let _ = Decoder::without_streaminfo().decode_frame(&file[at..]); +} + +#[test] +fn hostile_input_is_an_error_not_a_panic() { + let src = signal(2, 6000, 16, 11); + let file = encode(&src, 44_100, 16, 1152, 5); + for n in 0..file.len().min(3000) { + let _ = decode(&file[..n]); + } + // A cut file is truncated, not silently shorter. + assert!(decode(&file[..file.len() - 5]).is_err()); + // Every bit flip in the first frames is caught by a CRC or another check, never a panic. + for i in 0..file.len().min(2500) { + for bit in 0..8 { + let mut c = file.clone(); + c[i] ^= 1 << bit; + let _ = decode(&c); + } + } + // A sample limit stops a stream that claims too much. + assert_eq!(craft_audio::flac::decode_with_limit(&file, 1000), Err(DecodeError::TooLarge)); + assert!(decode(b"nope").is_err()); + assert!(decode(b"fLaC").is_err()); +} + +// ---- ffmpeg ---- + +fn ffmpeg_run(ff: &std::path::Path, args: &[&str]) -> Vec { + let out = Command::new(ff).args(["-v", "error", "-nostdin", "-y"]).args(args).output().expect("run ffmpeg"); + assert!(out.status.success(), "ffmpeg {args:?}: {}", String::from_utf8_lossy(&out.stderr)); + out.stdout +} + +#[test] +fn ffmpeg_files_decode_to_ffmpegs_samples() { + let Some(ff) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("flac_decode"); + let tones = "aevalsrc=exprs=0.4*sin(2*PI*441*t)+0.2*sin(2*PI*3100*t)|0.3*sin(2*PI*660*t)*sin(2*PI*3*t):s=44100:d=1.3"; + let noise = "anoisesrc=d=1.3:c=white:r=44100:a=0.5:s=3"; + for (src, name) in [(tones, "tones"), (noise, "noise")] { + for channels in [1usize, 2, 6] { + for (fmt, bytes, bits) in [("s16", 2usize, 16u32), ("s32", 4, 24)] { + for level in [0, 3, 5, 8, 12] { + let file = dir.join(format!("ffmpeg_{name}_{channels}ch_{bits}_l{level}.flac")); + let f = file.to_str().expect("path"); + let (ch, lv) = (channels.to_string(), level.to_string()); + ffmpeg_run(&ff, &["-f", "lavfi", "-i", src, "-ac", &ch, "-sample_fmt", fmt, "-compression_level", &lv, "-c:a", "flac", f]); + let theirs = ffmpeg_run(&ff, &["-i", f, "-f", if bits == 16 { "s16le" } else { "s32le" }, "-"]); + let got = + decode(&std::fs::read(&file).expect("read")).unwrap_or_else(|e| panic!("{name} {channels}ch {bits}-bit level {level}: {e}")); + assert_eq!(got.info.channels as usize, channels); + let frames = got.channels[0].len(); + assert_eq!(theirs.len(), frames * channels * bytes, "{name} {channels}ch {bits} l{level}: length"); + let mut ok = true; + for i in 0..frames { + for c in 0..channels { + let at = (i * channels + c) * bytes; + let want = if bytes == 2 { + i32::from(i16::from_le_bytes([theirs[at], theirs[at + 1]])) + } else { + i32::from_le_bytes([theirs[at], theirs[at + 1], theirs[at + 2], theirs[at + 3]]) + }; + // ffmpeg returns 24-bit audio left-aligned in 32 bits. + let mine = if bits == 24 { got.channels[c][i] << 8 } else { got.channels[c][i] }; + if mine != want { + ok = false; + } + } + } + assert!(ok, "{name} {channels}ch {bits}-bit level {level}: samples differ from ffmpeg's decode"); + } + } + } + } +} + +#[test] +fn ffmpeg_decodes_our_files_to_our_samples() { + let Some(ff) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("flac_decode"); + for (channels, bits) in [(1usize, 16u8), (2, 16), (2, 24), (6, 16)] { + let src = signal(channels, 20_000, u32::from(bits), 99); + let file = dir.join(format!("ours_{channels}ch_{bits}.flac")); + std::fs::write(&file, encode(&src, 44_100, bits, 4096, 5)).expect("write"); + let theirs = ffmpeg_run(&ff, &["-i", file.to_str().expect("path"), "-f", if bits == 16 { "s16le" } else { "s32le" }, "-"]); + let got = decode(&std::fs::read(&file).expect("read")).expect("decode"); + let bytes = if bits == 16 { 2 } else { 4 }; + for i in 0..20_000 { + for c in 0..channels { + let at = (i * channels + c) * bytes; + let want = if bytes == 2 { + i32::from(i16::from_le_bytes([theirs[at], theirs[at + 1]])) + } else { + i32::from_le_bytes([theirs[at], theirs[at + 1], theirs[at + 2], theirs[at + 3]]) >> 8 + }; + assert_eq!(got.channels[c][i], want, "{channels}ch {bits}-bit sample {i} of channel {c}"); + } + } + } +} From 7b4d5f992c98ac8f8bff92505166254bd455ab92 Mon Sep 17 00:00:00 2001 From: el-j Date: Sun, 15 Nov 2026 15:18:25 +0100 Subject: [PATCH 021/150] craft-captions: ASS and SSA subtitles (Format::Ass) Reads the [Events] section of ASS v4+ and SSA v4 files and writes a minimal ASS v4+ file. Keeps line breaks, the Name field as the speaker and the \i, \b, \u override tags; other override tags and vector drawings are dropped. Closes #1550 --- Cargo.lock | 1 + crates/captions/Cargo.toml | 2 + crates/captions/README.md | 1 + crates/captions/src/ass.rs | 239 +++++++++++++++++++++++++++++++ crates/captions/src/lib.rs | 17 ++- crates/captions/tests/it/ass.rs | 113 +++++++++++++++ crates/captions/tests/it/main.rs | 1 + 7 files changed, 371 insertions(+), 3 deletions(-) create mode 100644 crates/captions/src/ass.rs create mode 100644 crates/captions/tests/it/ass.rs diff --git a/Cargo.lock b/Cargo.lock index 8e12a6995..55552f9f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1425,6 +1425,7 @@ version = "0.1.0" name = "craft-captions" version = "0.1.0" dependencies = [ + "craft-media-testkit", "craft-timeline", "craft-xml", "proptest", diff --git a/crates/captions/Cargo.toml b/crates/captions/Cargo.toml index df665336d..cd09f7e16 100644 --- a/crates/captions/Cargo.toml +++ b/crates/captions/Cargo.toml @@ -19,3 +19,5 @@ thiserror = { workspace = true } proptest = { workspace = true } # A second XML reader for the tests: checks of written TTML, and the comparison in tests/it/ttml_xml_views.rs. roxmltree = "0.21" +# ffmpeg as the reference for the ASS tests (tests/it/ass.rs). +craft-media-testkit = { workspace = true } diff --git a/crates/captions/README.md b/crates/captions/README.md index 0676d1d72..7380a3ce4 100644 --- a/crates/captions/README.md +++ b/crates/captions/README.md @@ -55,6 +55,7 @@ lists at most 20 characters per stream. | EBU STL `.stl` | yes | yes | EBU Tech 3264-E: GSI block + 128-byte TTI blocks, `STL25.01` (25 fps) or `STL30.01` (read/written as 29.97 fps, non-drop labels), ISO/IEC 6937 Latin text (accents as diacritic + letter), italics / underline codes as `` / ``, extension blocks for long subtitles, start-of-programme (TCP) subtracted. | | TTML `.ttml` (IMSC1) | yes | yes | Writes the IMSC1 Text profile (TTML2 namespaces) with the document's language as `xml:lang` (read back on import): `

                      ` per cue, `
                      `, styled ``s for `` / `` / ``, `ttm:agent` speakers, exact frame times (`123f`) at the sequence rate. Reads any TTML (TTML1, TTML2, the 2006 `ttaf1` drafts): clock / frame / offset / tick times, `dur`, nested time containers (`body` / `div` begins offset and ends clip their contents; `seq` containers are read as `par` with a warning), timed spans, referenced styles. | | DFXP `.dfxp` | yes | yes | TTML1 with the DFXP presentation profile; clock times in milliseconds (frame-exact after `snap_to_frames`). | +| ASS / SSA `.ass`, `.ssa` | yes | yes (ASS v4+) | The `[Events]` section: `Dialogue:` lines through the file's `Format:` line, `Name` as the speaker, `\N`, `\n`, `\h` and the `\i`, `\b`, `\u` override tags kept (as ``, ``, ``); other override tags and drawing lines are dropped. Times are centiseconds. Written files have one `Default` style. | All cue times are exact `Tick`s. Milliseconds convert exactly (1 ms = 254 016 000 ticks); SCC frames map through `FrameRate::FPS_29_97`. `Document::snap_to_frames` snaps cues to a frame grid and diff --git a/crates/captions/src/ass.rs b/crates/captions/src/ass.rs new file mode 100644 index 000000000..3c907c829 --- /dev/null +++ b/crates/captions/src/ass.rs @@ -0,0 +1,239 @@ +//! Advanced SubStation Alpha and SubStation Alpha (`.ass`, `.ssa`). +//! +//! Reading takes the `[Events]` section: its `Format:` line gives the field order (ASS v4+ and SSA +//! v4 differ in the first field only), each `Dialogue:` line becomes a cue; `Comment:` lines and +//! the other sections are skipped. A cue gets the `Name` field as its speaker. The text keeps what +//! the neutral model can show: `\N` and `\n` become a line break and a space, `\h` a no-break +//! space, and the override tags `\i`, `\b` and `\u` become ``, `` and ``. Every other +//! override tag (colours, fonts, positions, karaoke, animation) is dropped, and a line that draws +//! vector shapes (`\p1`) is skipped with a warning. +//! +//! Writing gives a minimal ASS v4+ file: one `Default` style, one `Dialogue:` line per cue with +//! times to the centisecond. Braces in the text, which ASS reads as override blocks, are written +//! as the full-width `ï½›` and `ï½�`. + +use crate::time::{TICKS_PER_SECOND, Tick}; +use crate::{Cue, Document, Error, Result}; + +const TICKS_PER_CS: i64 = TICKS_PER_SECOND / 100; + +/// `H:MM:SS.cc`, the form SSA and ASS use (any number of fraction digits is read). +fn parse_time(s: &str) -> Option { + let s = s.trim(); + let (hms, frac) = s.split_once('.').unwrap_or((s, "")); + let mut parts = hms.split(':'); + let (h, m, sec) = (parts.next()?, parts.next()?, parts.next()?); + if parts.next().is_some() { + return None; + } + let num = |p: &str| -> Option { (!p.is_empty() && p.len() <= 9 && p.bytes().all(|b| b.is_ascii_digit())).then(|| p.parse().ok()).flatten() }; + let (h, m, sec) = (num(h)?, num(m)?, num(sec)?); + if m >= 60 || sec >= 60 || h > 9999 { + return None; + } + let mut ticks = ((h * 60 + m) * 60 + sec) * TICKS_PER_SECOND; + if !frac.is_empty() { + if frac.len() > 9 || !frac.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + let d: i64 = frac.parse().ok()?; + ticks += (i128::from(d) * i128::from(TICKS_PER_SECOND) / 10i128.pow(frac.len() as u32)) as i64; + } + Some(Tick(ticks)) +} + +fn format_time(t: Tick) -> String { + let cs = (i128::from(t.0.max(0)) + i128::from(TICKS_PER_CS) / 2) / i128::from(TICKS_PER_CS); + let (h, m, s, c) = (cs / 360_000, (cs / 6000) % 60, (cs / 100) % 60, cs % 100); + format!("{h}:{m:02}:{s:02}.{c:02}") +} + +/// The Text field of a Dialogue line as cue text; `None` when the line draws shapes. +fn convert_text(raw: &str) -> Option { + let mut out = String::new(); + // Open ``, ``, `` in the order they were opened. + let mut open: Vec = Vec::new(); + let mut chars = raw.chars().peekable(); + let mut drawing = false; + let mut dropped_drawing = false; + while let Some(c) = chars.next() { + match c { + '{' => { + let mut block = String::new(); + let mut closed = false; + for b in chars.by_ref() { + if b == '}' { + closed = true; + break; + } + block.push(b); + } + if !closed { + // An unterminated block is literal text. + out.push('{'); + out.push_str(&block); + continue; + } + for tag in block.split('\\').skip(1) { + let tag = tag.trim_end(); + let (name, val) = tag.split_at(tag.find(|c: char| !c.is_ascii_alphabetic()).unwrap_or(tag.len())); + match name { + "i" | "b" | "u" => { + let letter = name.chars().next().unwrap_or('i'); + let on = val.trim() != "0"; + if on && !open.contains(&letter) { + open.push(letter); + out.push_str(&format!("<{letter}>")); + } else if !on && open.contains(&letter) { + // Close back to it, reopening what was opened after. + let at = open.iter().position(|&o| o == letter).unwrap_or(0); + let after: Vec = open.split_off(at + 1); + for o in after.iter().rev() { + out.push_str(&format!("")); + } + open.pop(); + out.push_str(&format!("")); + for o in after { + open.push(o); + out.push_str(&format!("<{o}>")); + } + } + } + "p" => drawing = val.trim().parse::().unwrap_or(0) != 0, + _ => {} + } + } + } + '\\' => match chars.peek() { + Some('N') => { + chars.next(); + out.push('\n'); + } + Some('n') => { + chars.next(); + out.push(' '); + } + Some('h') => { + chars.next(); + out.push('\u{a0}'); + } + _ => out.push('\\'), + }, + c => { + if drawing { + dropped_drawing = true; + } else { + out.push(c); + } + } + } + } + if dropped_drawing && out.trim().is_empty() { + return None; + } + while let Some(o) = open.pop() { + out.push_str(&format!("")); + } + Some(out) +} + +pub fn parse(text: &str) -> Result { + let text = text.strip_prefix('\u{feff}').unwrap_or(text); + let mut doc = Document::default(); + let mut in_events = false; + let mut saw_events = false; + // Column of each field in a Dialogue line. + let mut cols: Vec = + ["layer", "start", "end", "style", "name", "marginl", "marginr", "marginv", "effect", "text"].map(String::from).to_vec(); + let mut drawings = 0usize; + for (n, line) in text.lines().enumerate() { + let line = line.trim(); + if line.starts_with('[') && line.ends_with(']') { + in_events = line.eq_ignore_ascii_case("[events]"); + saw_events |= in_events; + continue; + } + if !in_events { + continue; + } + let Some((key, rest)) = line.split_once(':') else { continue }; + let rest = rest.trim_start(); + match key.trim().to_ascii_lowercase().as_str() { + "format" => cols = rest.split(',').map(|f| f.trim().to_ascii_lowercase()).collect(), + "dialogue" => { + // The last column (Text) keeps its commas. + let fields: Vec<&str> = rest.splitn(cols.len(), ',').collect(); + let get = |name: &str| cols.iter().position(|c| c == name).and_then(|i| fields.get(i)).copied(); + let (Some(start), Some(end), Some(raw)) = (get("start").and_then(parse_time), get("end").and_then(parse_time), get("text")) else { + doc.warnings.push(format!("line {}: not a Dialogue line this reader understands", n + 1)); + continue; + }; + let Some(text) = convert_text(raw) else { + drawings += 1; + continue; + }; + let name = get("name").map(str::trim).filter(|s| !s.is_empty()); + doc.cues.push(Cue { start, end, text, speaker: name.map(str::to_string), ..Default::default() }); + } + _ => {} + } + } + if !saw_events { + return Err(Error::NotFormat("ASS")); + } + if drawings > 0 { + doc.warnings.push(format!("{drawings} vector drawing line(s) were skipped")); + } + Ok(doc) +} + +fn escape(text: &str) -> String { + let mut out = String::with_capacity(text.len() + 8); + let mut rest = text; + while !rest.is_empty() { + if let Some(r) = rest.strip_prefix("") { + out.push_str("{\\i1}"); + rest = r; + } else if let Some(r) = rest.strip_prefix("") { + out.push_str("{\\i0}"); + rest = r; + } else if let Some(r) = rest.strip_prefix("") { + out.push_str("{\\b1}"); + rest = r; + } else if let Some(r) = rest.strip_prefix("") { + out.push_str("{\\b0}"); + rest = r; + } else if let Some(r) = rest.strip_prefix("") { + out.push_str("{\\u1}"); + rest = r; + } else if let Some(r) = rest.strip_prefix("") { + out.push_str("{\\u0}"); + rest = r; + } else if let Some(c) = rest.chars().next() { + match c { + '\n' => out.push_str("\\N"), + '{' => out.push('\u{ff5b}'), + '}' => out.push('\u{ff5d}'), + '\u{a0}' => out.push_str("\\h"), + c => out.push(c), + } + rest = &rest[c.len_utf8()..]; + } + } + out +} + +pub fn write(doc: &Document) -> String { + let mut out = String::from( + "[Script Info]\n; Written by craft-captions\nScriptType: v4.00+\nWrapStyle: 0\nScaledBorderAndShadow: yes\nPlayResX: 384\nPlayResY: 288\n\n\ + [V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n\ + Style: Default,Arial,16,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,1,0,2,10,10,10,1\n\n\ + [Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n", + ); + for c in &doc.cues { + // A comma in the Name field would shift the columns; the other fields are fixed text. + let name = c.speaker.as_deref().unwrap_or("").replace([',', '\n'], " "); + out.push_str(&format!("Dialogue: 0,{},{},Default,{},0,0,0,,{}\n", format_time(c.start), format_time(c.end), name, escape(&c.text))); + } + out +} diff --git a/crates/captions/src/lib.rs b/crates/captions/src/lib.rs index 0ba9f2ba5..e8a492722 100644 --- a/crates/captions/src/lib.rs +++ b/crates/captions/src/lib.rs @@ -4,7 +4,7 @@ //! - **Files:** [SubRip](srt) (`.srt`), [WebVTT](vtt) (`.vtt`), [Scenarist SCC](scc) (`.scc`, //! CEA-608 pop-on, roll-up and paint-on on read; pop-on on write), [MacCaption MCC](mcc) //! (`.mcc`, SMPTE ST 334-2 packets with CEA-608 and CEA-708), [EBU STL](stl) (`.stl`, EBU -//! Tech 3264) and [TTML](ttml) (`.ttml` IMSC1 Text profile, `.dfxp` DFXP) parse into a +//! Tech 3264), [ASS/SSA](ass) (`.ass`, `.ssa`) and [TTML](ttml) (`.ttml` IMSC1 Text profile, `.dfxp` DFXP) parse into a //! [`Document`] of [`Cue`]s and write back. Readers are forgiving (BOM, UTF-16, Windows-1252 //! fallback, CRLF/CR, missing SRT indexes, `,` or `.` milliseconds, missing hours, no blank //! line between cues); writers are strict. WebVTT cue identifiers, cue settings, `` @@ -32,6 +32,7 @@ #[doc = include_str!("../README.md")] struct ReadmeExamples; +pub mod ass; pub mod cea608; pub mod mcc; pub mod scc; @@ -69,10 +70,12 @@ pub enum Format { Ttml, /// DFXP (TTML1). Dfxp, + /// Advanced SubStation Alpha (also reads SubStation Alpha). + Ass, } impl Format { - pub const ALL: [Format; 7] = [Format::Srt, Format::WebVtt, Format::Scc, Format::Mcc, Format::Stl, Format::Ttml, Format::Dfxp]; + pub const ALL: [Format; 8] = [Format::Srt, Format::WebVtt, Format::Scc, Format::Mcc, Format::Stl, Format::Ttml, Format::Dfxp, Format::Ass]; pub fn name(self) -> &'static str { match self { Format::Srt => "SubRip (SRT)", @@ -82,6 +85,7 @@ impl Format { Format::Stl => "EBU STL", Format::Ttml => "TTML (IMSC1)", Format::Dfxp => "DFXP (TTML1)", + Format::Ass => "Advanced SubStation Alpha (ASS)", } } pub fn extension(self) -> &'static str { @@ -93,6 +97,7 @@ impl Format { Format::Stl => "stl", Format::Ttml => "ttml", Format::Dfxp => "dfxp", + Format::Ass => "ass", } } pub fn from_name(s: &str) -> Option { @@ -104,6 +109,7 @@ impl Format { "stl" | "ebu-stl" | "ebustl" | "ebu" => Some(Format::Stl), "ttml" | "imsc" | "imsc1" | "xml" => Some(Format::Ttml), "dfxp" => Some(Format::Dfxp), + "ass" | "ssa" => Some(Format::Ass), _ => None, } } @@ -251,6 +257,9 @@ pub fn detect(bytes: &[u8], ext: Option<&str>) -> Option { if head.starts_with("File Format=MacCaption_MCC") { return Some(Format::Mcc); } + if head.starts_with("[Script Info]") { + return Some(Format::Ass); + } let ext = ext.map(|e| e.trim_start_matches('.').to_ascii_lowercase()); if (head.starts_with('<') && (head.contains("") || head.contains(":tt "))) && (head.contains("ns/ttml") || head.contains("ttaf1")) @@ -262,7 +271,7 @@ pub fn detect(bytes: &[u8], ext: Option<&str>) -> Option { match ext.as_deref() { Some("srt") if has_arrow_timing || head.is_empty() => Some(Format::Srt), Some("vtt") if has_arrow_timing => Some(Format::WebVtt), - Some("srt" | "vtt" | "scc" | "mcc" | "stl" | "ttml" | "dfxp" | "xml") => None, + Some("srt" | "vtt" | "scc" | "mcc" | "stl" | "ttml" | "dfxp" | "xml" | "ass" | "ssa") => None, _ if has_arrow_timing => Some(Format::Srt), _ => None, } @@ -296,6 +305,7 @@ fn parse_unbounded(bytes: &[u8], format: Format) -> Result { Format::Scc => scc::parse(&text), Format::Mcc => mcc::parse(&text), Format::Ttml | Format::Dfxp => ttml::parse(&text), + Format::Ass => ass::parse(&text), Format::Stl => stl::parse(bytes), } } @@ -333,6 +343,7 @@ pub fn write(doc: &Document, format: Format, opts: WriteOptions) -> Vec { Format::Stl => stl::write(doc, opts.rate, opts.program), Format::Ttml => ttml::write(doc, ttml::Flavor::Imsc1, opts.rate, doc.language.as_deref().unwrap_or("")).into_bytes(), Format::Dfxp => ttml::write(doc, ttml::Flavor::Dfxp, opts.rate, doc.language.as_deref().unwrap_or("")).into_bytes(), + Format::Ass => ass::write(doc).into_bytes(), } } diff --git a/crates/captions/tests/it/ass.rs b/crates/captions/tests/it/ass.rs new file mode 100644 index 000000000..de5d43167 --- /dev/null +++ b/crates/captions/tests/it/ass.rs @@ -0,0 +1,113 @@ +//! SubStation Alpha / Advanced SubStation Alpha: reading, writing, and ffmpeg as the reference +//! (oracle; skipped when ffmpeg is absent, a failure with `CRAFT_MEDIA_REQUIRE_ORACLES=1`). + +use craft_captions::{Cue, Document, Format, Tick, WriteOptions, detect, parse, write}; +use std::process::Command; + +const T: i64 = craft_captions::TICKS_PER_SECOND; + +fn cs(n: i64) -> Tick { + Tick(n * (T / 100)) +} + +const SAMPLE: &str = "\u{feff}[Script Info]\n; a comment\nTitle: Test\nScriptType: v4.00+\n\n[V4+ Styles]\nFormat: Name, Fontname\nStyle: Default,Arial\n\n[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\nComment: 0,0:00:00.00,0:00:01.00,Default,,0,0,0,,not a cue\nDialogue: 0,0:00:01.50,0:00:03.25,Default,Bob,0,0,0,,Hello, {\\i1}world{\\i0}\\Nsecond {\\b1}line{\\b0}\nDialogue: 0,1:02:03.04,1:02:04.5,Default,,0,0,0,,{\\pos(10,20)\\c&H0000FF&}Red\\hspace\\nsoft\nDialogue: 0,0:00:09.00,0:00:10.00,Default,,0,0,0,,{\\p1}m 0 0 l 100 0 100 100{\\p0}\n"; + +#[test] +fn reads_dialogue_lines_with_commas_overrides_and_line_breaks() { + let doc = parse(SAMPLE.as_bytes(), Format::Ass).unwrap(); + assert_eq!(doc.cues.len(), 2); + assert_eq!(doc.cues[0].start, cs(150)); + assert_eq!(doc.cues[0].end, cs(325)); + assert_eq!(doc.cues[0].text, "Hello, world\nsecond line"); + assert_eq!(doc.cues[0].speaker.as_deref(), Some("Bob")); + // 1:02:03.04, a one-digit fraction, an override block dropped, \h and \n. + assert_eq!(doc.cues[1].start, Tick((3600 + 120 + 3) * T + 4 * (T / 100))); + assert_eq!(doc.cues[1].end, Tick((3600 + 120 + 4) * T + T / 2)); + assert_eq!(doc.cues[1].text, "Red\u{a0}space soft"); + assert!(doc.warnings.iter().any(|w| w.contains("vector drawing")), "{:?}", doc.warnings); +} + +#[test] +fn ssa_v4_files_read_through_their_own_format_line() { + let ssa = "[Script Info]\nScriptType: v4.00\n\n[V4 Styles]\nFormat: Name\n\n[Events]\nFormat: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\nDialogue: Marked=0,0:00:01.00,0:00:02.00,*Default,,0000,0000,0000,,Old style\n"; + let doc = parse(ssa.as_bytes(), Format::Ass).unwrap(); + assert_eq!((doc.cues[0].start, doc.cues[0].end, doc.cues[0].text.as_str()), (cs(100), cs(200), "Old style")); +} + +#[test] +fn write_then_read_keeps_times_text_and_speaker() { + let cues = vec![ + Cue { start: cs(100), end: cs(250), text: "Line one\nline two".into(), speaker: Some("Ann".into()), ..Default::default() }, + Cue { + start: Tick(7200 * T + 99 * (T / 100)), + end: Tick(7203 * T), + text: "bold and under {braces}".into(), + ..Default::default() + }, + ]; + let doc = Document { cues, ..Default::default() }; + let bytes = write(&doc, Format::Ass, WriteOptions::default()); + assert_eq!(detect(&bytes, Some("ass")), Some(Format::Ass)); + let back = parse(&bytes, Format::Ass).unwrap(); + assert_eq!(back.cues[0], doc.cues[0]); + assert_eq!(back.cues[1].start, doc.cues[1].start); + // Braces are written full-width, as ASS reads `{` as the start of an override block. + assert_eq!(back.cues[1].text, "bold and under \u{ff5b}braces\u{ff5d}"); +} + +#[test] +fn files_without_an_events_section_are_not_ass() { + assert!(parse(b"[Script Info]\nTitle: x\n", Format::Ass).is_err()); + assert!(parse(b"hello", Format::Ass).is_err()); + assert_eq!(detect(b"hello", Some("ass")), None); +} + +#[test] +fn bad_lines_are_skipped_with_a_warning() { + let text = "[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\nDialogue: 0,garbage,0:00:02.00,Default,,0,0,0,,x\nDialogue: 0,0:00:01.00,0:00:02.00,Default,,0,0,0,,ok\n"; + let doc = parse(text.as_bytes(), Format::Ass).unwrap(); + assert_eq!(doc.cues.len(), 1); + assert_eq!(doc.warnings.len(), 1); +} + +fn ffmpeg_run(ff: &std::path::Path, args: &[&str]) { + let out = Command::new(ff).args(["-v", "error", "-nostdin", "-y"]).args(args).output().expect("run ffmpeg"); + assert!(out.status.success(), "ffmpeg {args:?}: {}", String::from_utf8_lossy(&out.stderr)); +} + +const SRT: &str = "1\n00:00:01,500 --> 00:00:03,250\nHello, world\nsecond line\n\n2\n00:00:04,000 --> 00:00:05,990\nPlain, with a comma\n\n3\n01:00:00,010 --> 01:00:02,999\nUnder and italic both\n"; + +#[test] +fn we_read_what_ffmpeg_writes() { + let Some(ff) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("captions_ass"); + let (srt, ass) = (dir.join("in.srt"), dir.join("ffmpeg.ass")); + std::fs::write(&srt, SRT).expect("write"); + ffmpeg_run(&ff, &["-i", srt.to_str().expect("path"), ass.to_str().expect("path")]); + let from_ass = parse(&std::fs::read(&ass).expect("read"), Format::Ass).expect("parse ass"); + let from_srt = parse(SRT.as_bytes(), Format::Srt).expect("parse srt"); + assert_eq!(from_ass.cues.len(), from_srt.cues.len()); + for (a, s) in from_ass.cues.iter().zip(&from_srt.cues) { + // ASS times are centiseconds; the SRT ones round to them. + assert_eq!(a.start, cs((s.start.0 + T / 200) / (T / 100)), "start of {:?}", s.text); + assert_eq!(a.end, cs((s.end.0 + T / 200) / (T / 100)), "end of {:?}", s.text); + assert_eq!(a.text, s.text); + } +} + +#[test] +fn ffmpeg_reads_what_we_write() { + let Some(ff) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("captions_ass"); + let doc = parse(SRT.as_bytes(), Format::Srt).expect("parse srt"); + let (ass, srt) = (dir.join("ours.ass"), dir.join("back.srt")); + std::fs::write(&ass, write(&doc, Format::Ass, WriteOptions::default())).expect("write"); + ffmpeg_run(&ff, &["-i", ass.to_str().expect("path"), srt.to_str().expect("path")]); + let theirs = parse(&std::fs::read(&srt).expect("read"), Format::Srt).expect("parse back"); + assert_eq!(theirs.cues.len(), doc.cues.len()); + for (t, d) in theirs.cues.iter().zip(&doc.cues) { + assert_eq!(t.start, cs((d.start.0 + T / 200) / (T / 100))); + assert_eq!(t.end, cs((d.end.0 + T / 200) / (T / 100))); + assert_eq!(t.text, d.text, "text of the cue at {:?}", d.start); + } +} diff --git a/crates/captions/tests/it/main.rs b/crates/captions/tests/it/main.rs index 16f8fcbb6..be03d2855 100644 --- a/crates/captions/tests/it/main.rs +++ b/crates/captions/tests/it/main.rs @@ -1,3 +1,4 @@ +mod ass; mod limits; mod literal_roundtrips; mod mcc_708_timing; From ee20ae63fdc314da17362b0e1dc429fa081a686b Mon Sep 17 00:00:00 2001 From: el-j Date: Sun, 15 Nov 2026 15:24:46 +0100 Subject: [PATCH 022/150] craft-audio: qoa_oracle test without index loops (CI clippy) --- crates/audio/tests/qoa_oracle.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/crates/audio/tests/qoa_oracle.rs b/crates/audio/tests/qoa_oracle.rs index cf0ed8c37..7b5ac2674 100644 --- a/crates/audio/tests/qoa_oracle.rs +++ b/crates/audio/tests/qoa_oracle.rs @@ -55,12 +55,7 @@ fn ffmpeg_decodes_our_files_to_the_same_samples() { .expect("run ffmpeg"); assert!(out.status.success(), "ffmpeg failed on {}: {}", file.display(), String::from_utf8_lossy(&out.stderr)); let theirs: Vec = out.stdout.as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect(); - let mut interleaved = Vec::with_capacity(frames * channels); - for i in 0..frames { - for c in 0..channels { - interleaved.push(ours.channels[c][i]); - } - } + let interleaved: Vec = (0..frames).flat_map(|i| ours.channels.iter().map(move |ch| ch[i])).collect(); assert_eq!(theirs.len(), interleaved.len(), "{rate} Hz, {channels} ch, {frames} frames: sample count"); assert!(theirs == interleaved, "{rate} Hz, {channels} ch, {frames} frames: samples differ"); } From 2d5f2e4a73eb54326b3192120a7a4457ad79b07f Mon Sep 17 00:00:00 2001 From: el-j Date: Sun, 15 Nov 2026 15:25:56 +0100 Subject: [PATCH 023/150] craft-audio: flac_decode tests without index loops (CI clippy) --- crates/audio/tests/flac_decode.rs | 49 ++++++++++++++----------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/crates/audio/tests/flac_decode.rs b/crates/audio/tests/flac_decode.rs index 03d6d678e..c35267b08 100644 --- a/crates/audio/tests/flac_decode.rs +++ b/crates/audio/tests/flac_decode.rs @@ -104,6 +104,21 @@ fn hostile_input_is_an_error_not_a_panic() { // ---- ffmpeg ---- +/// Little-endian 16- or 32-bit samples of raw PCM. +fn pcm_values(raw: &[u8], bytes: usize) -> Vec { + if bytes == 2 { + raw.as_chunks::<2>().0.iter().map(|b| i32::from(i16::from_le_bytes(*b))).collect() + } else { + raw.as_chunks::<4>().0.iter().map(|b| i32::from_le_bytes(*b)).collect() + } +} + +/// Planar channels as interleaved samples. +fn interleave(channels: &[Vec]) -> Vec { + let frames = channels.first().map_or(0, Vec::len); + (0..frames).flat_map(|i| channels.iter().map(move |c| c[i])).collect() +} + fn ffmpeg_run(ff: &std::path::Path, args: &[&str]) -> Vec { let out = Command::new(ff).args(["-v", "error", "-nostdin", "-y"]).args(args).output().expect("run ffmpeg"); assert!(out.status.success(), "ffmpeg {args:?}: {}", String::from_utf8_lossy(&out.stderr)); @@ -130,22 +145,10 @@ fn ffmpeg_files_decode_to_ffmpegs_samples() { assert_eq!(got.info.channels as usize, channels); let frames = got.channels[0].len(); assert_eq!(theirs.len(), frames * channels * bytes, "{name} {channels}ch {bits} l{level}: length"); - let mut ok = true; - for i in 0..frames { - for c in 0..channels { - let at = (i * channels + c) * bytes; - let want = if bytes == 2 { - i32::from(i16::from_le_bytes([theirs[at], theirs[at + 1]])) - } else { - i32::from_le_bytes([theirs[at], theirs[at + 1], theirs[at + 2], theirs[at + 3]]) - }; - // ffmpeg returns 24-bit audio left-aligned in 32 bits. - let mine = if bits == 24 { got.channels[c][i] << 8 } else { got.channels[c][i] }; - if mine != want { - ok = false; - } - } - } + // ffmpeg returns 24-bit audio left-aligned in 32 bits. + let shift = if bits == 24 { 8 } else { 0 }; + let mine: Vec = interleave(&got.channels).into_iter().map(|v| v << shift).collect(); + let ok = mine == pcm_values(&theirs, bytes); assert!(ok, "{name} {channels}ch {bits}-bit level {level}: samples differ from ffmpeg's decode"); } } @@ -164,16 +167,8 @@ fn ffmpeg_decodes_our_files_to_our_samples() { let theirs = ffmpeg_run(&ff, &["-i", file.to_str().expect("path"), "-f", if bits == 16 { "s16le" } else { "s32le" }, "-"]); let got = decode(&std::fs::read(&file).expect("read")).expect("decode"); let bytes = if bits == 16 { 2 } else { 4 }; - for i in 0..20_000 { - for c in 0..channels { - let at = (i * channels + c) * bytes; - let want = if bytes == 2 { - i32::from(i16::from_le_bytes([theirs[at], theirs[at + 1]])) - } else { - i32::from_le_bytes([theirs[at], theirs[at + 1], theirs[at + 2], theirs[at + 3]]) >> 8 - }; - assert_eq!(got.channels[c][i], want, "{channels}ch {bits}-bit sample {i} of channel {c}"); - } - } + let shift = if bytes == 4 { 8 } else { 0 }; + let want: Vec = pcm_values(&theirs, bytes).into_iter().map(|v| v >> shift).collect(); + assert!(interleave(&got.channels) == want, "{channels}ch {bits}-bit: samples differ from ffmpeg's decode"); } } From b850981f835f39859077cf62ff4e76462e76167c Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:29:08 +0100 Subject: [PATCH 024/150] craft-audio: port EffectCraft's later Opus encoder speed-ups The shared encoder was imported from storytold/effectcraft@c618429. EffectCraft's copy has since gained @vangel76's speed-ups (effectcraft #2394, #2398, #2399, #2402, #2403, #2404) and the strict-Clippy CELT stage refactor. Port them so EffectCraft can drop crates/opusenc without slowing WebM export. Packets are byte-identical to effectcraft-opusenc at d6c9ad66 (2100 packets across SILK, hybrid and CELT, mono and stereo, 10-320 kb/s). The shared Error API is unchanged. Refs #778 --- crates/audio/src/opus/encoder/bands.rs | 341 +++++++++++++++--- crates/audio/src/opus/encoder/energy.rs | 25 +- crates/audio/src/opus/encoder/mdct.rs | 95 ++++- crates/audio/src/opus/encoder/mod.rs | 67 ++-- crates/audio/src/opus/encoder/rate.rs | 88 ++--- crates/audio/src/opus/encoder/resample.rs | 31 +- .../audio/src/opus/encoder/silk/analysis.rs | 112 +++++- crates/audio/src/opus/encoder/silk/mod.rs | 168 ++++++++- 8 files changed, 748 insertions(+), 179 deletions(-) diff --git a/crates/audio/src/opus/encoder/bands.rs b/crates/audio/src/opus/encoder/bands.rs index 604aab595..697332eb2 100644 --- a/crates/audio/src/opus/encoder/bands.rs +++ b/crates/audio/src/opus/encoder/bands.rs @@ -8,7 +8,8 @@ use crate::opus::encoder::range::{BITRES, RangeEncoder, ilog}; use crate::opus::encoder::rate::{Mode, QTHETA_OFFSET, get_pulses}; -use crate::opus::encoder::tables::{EBANDS, NB_EBANDS}; +use crate::opus::encoder::tables::EBANDS; +use crate::opus::encoder::{CeltFrame, rate::Allocation}; pub const SPREAD_NORMAL: usize = 2; @@ -55,7 +56,7 @@ fn exp_rotation1_inv(x: &mut [f32], len: usize, stride: usize, c: f32, s: f32) { if len <= stride { return; } - if len >= 2 * stride + 1 { + if len > 2 * stride { for i in 0..=len - 2 * stride - 1 { let x1 = x[i]; let x2 = x[i + stride]; @@ -96,12 +97,24 @@ pub fn exp_rotation_inv(x: &mut [f32], len: usize, k: usize, spread: usize) { } } +/// Relative slack for the pulse-candidate filter in [`pvq_search`]: far above the f32 rounding of +/// the filter score (a few 1e-7), far below the spread of real scores. +const PVQ_SCORE_SLACK: f32 = 1e-5; + /// Greedy PVQ search: the integer vector `y` with `sum |y| = k` closest in direction to `x`. pub fn pvq_search(x: &[f32], k: usize, y: &mut [i32]) { let n = x.len(); y[..n].fill(0); - let abs: Vec = x.iter().map(|v| v.abs()).collect(); - let sum: f32 = abs.iter().sum(); + // `abs`, `dy` (2 y + 1, infinite in the padding) and the per-pass scores `score`, each padded to + // whole 8-lane chunks. + let np = n.next_multiple_of(8); + let mut buf = vec![0f32; 3 * np]; + let (abs, rest) = buf.split_at_mut(np); + let (dy, score) = rest.split_at_mut(np); + for (a, v) in abs.iter_mut().zip(x) { + *a = v.abs(); + } + let sum: f32 = abs[..n].iter().sum(); if !sum.is_finite() || sum <= 1e-15 { y[0] = k as i32; return; @@ -113,30 +126,69 @@ pub fn pvq_search(x: &[f32], k: usize, y: &mut [i32]) { // Pre-project onto the pyramid (rounding down) and place the rest greedily. let r = (k as f32 - 1.0) / sum; for j in 0..n { - let p = (abs[j] * r).floor() as i32; + // Non-negative and at most k, so the truncating cast is the floor. + let p = (abs[j] * r) as i32; y[j] = p; placed += p as usize; xy += abs[j] * p as f32; yy += (p * p) as f32; } } + for j in 0..np { + dy[j] = if j < n { 2.0 * y[j] as f32 + 1.0 } else { f32::INFINITY }; + } while placed < k { + // Vectorisable scores (xy + |x_j|)^2 / (yy + 2 y_j + 1) and their maximum. + for ((sc, &a), &d) in score.iter_mut().zip(abs.iter()).zip(dy.iter()) { + let t = xy + a; + *sc = t * t / (yy + d); + } + let mut lane_max = [0f32; 8]; + for c in score.as_chunks::<8>().0 { + for i in 0..8 { + lane_max[i] = if c[i] > lane_max[i] { c[i] } else { lane_max[i] }; + } + } + let top = lane_max.iter().fold(0f32, |m, &v| if v > m { v } else { m }); + // The exact cross-multiplied test below, in index order with the first maximum winning, only + // matters for entries within rounding distance of the top score: every other entry is beaten + // by the top one whenever it is reached and never beats it afterwards. Running the test on + // those alone therefore picks the same pulse as running it on all of them. + let filter = top > 1e-30 && top.is_finite(); + let lo = top * (1.0 - PVQ_SCORE_SLACK); + // Usually exactly one entry qualifies; its index falls out of a lane-wise count. + let mut count = [0u32; 8]; + let mut index = [0u32; 8]; + for (ci, c) in score.as_chunks::<8>().0.iter().enumerate() { + for i in 0..8 { + let hit = u32::from(c[i] >= lo); + count[i] += hit; + index[i] += hit * (ci * 8 + i) as u32; + } + } let mut best = 0usize; - let mut best_num = -1f32; - let mut best_den = 1f32; - for j in 0..n { - let num = xy + abs[j]; - let num = num * num; - let den = yy + 2.0 * y[j] as f32 + 1.0; - if num * best_den > best_num * den { - best_num = num; - best_den = den; - best = j; + if filter && count.iter().sum::() == 1 { + best = index.iter().sum::() as usize; + } else { + let mut best_num = -1f32; + let mut best_den = 1f32; + for j in 0..n { + if !filter || score[j] >= lo { + let num = xy + abs[j]; + let num = num * num; + let den = yy + 2.0 * y[j] as f32 + 1.0; + if num * best_den > best_num * den { + best_num = num; + best_den = den; + best = j; + } + } } } xy += abs[best]; yy += 2.0 * y[best] as f32 + 1.0; y[best] += 1; + dy[best] = 2.0 * y[best] as f32 + 1.0; placed += 1; } for j in 0..n { @@ -146,25 +198,45 @@ pub fn pvq_search(x: &[f32], k: usize, y: &mut [i32]) { } } -/// Encodes the PVQ codeword `y` (`sum |y| = k`) as the index the RFC 6716 §4.3.4.2 decoder maps -/// back to `y`. -pub fn encode_pulses(y: &[i32], k: usize, enc: &mut RangeEncoder, v: &mut Vec) { - let n = y.len(); - let w = k + 1; - v.clear(); - v.resize((n + 1) * w, 0); +/// Largest band width and pulse count the encoder codes (176 coefficients, `get_pulses(40)` = 128). +const PVQ_MAX_N: usize = 176; +const PVQ_MAX_K: usize = 128; + +/// Row-major `V(n, k)` for `n <= max_n`, `k <= max_k` with row stride `max_k + 1`, saturated at +/// `u32::MAX` (a codebook that large is never coded, see `rate::fits_in32`). +fn build_pvq_table(max_n: usize, max_k: usize) -> Vec { + let w = max_k + 1; + let mut v = vec![0u32; (max_n + 1) * w]; v[0] = 1; - for nn in 1..=n { + for nn in 1..=max_n { v[nn * w] = 1; - for kk in 1..=k { - let a = v[(nn - 1) * w + kk]; - let b = v[nn * w + kk - 1]; - let c = v[(nn - 1) * w + kk - 1]; - v[nn * w + kk] = a.saturating_add(b).saturating_add(c); + for kk in 1..=max_k { + let sum = v[(nn - 1) * w + kk] as u64 + v[nn * w + kk - 1] as u64 + v[(nn - 1) * w + kk - 1] as u64; + v[nn * w + kk] = sum.min(u32::MAX as u64) as u32; } } - let total = v[n * w + k]; - let vv = |nn: usize, kx: usize| v[nn * w + kx]; + v +} + +fn pvq_table() -> &'static [u32] { + static TABLE: std::sync::OnceLock> = std::sync::OnceLock::new(); + TABLE.get_or_init(|| build_pvq_table(PVQ_MAX_N, PVQ_MAX_K)) +} + +/// Encodes the PVQ codeword `y` (`sum |y| = k`) as the index the RFC 6716 §4.3.4.2 decoder maps +/// back to `y`. +pub fn encode_pulses(y: &[i32], k: usize, enc: &mut RangeEncoder) { + let n = y.len(); + // Shapes outside the coded range (never produced by the band layout) get a local table. + let local; + let (v, w) = if n <= PVQ_MAX_N && k <= PVQ_MAX_K { + (pvq_table(), PVQ_MAX_K + 1) + } else { + local = build_pvq_table(n, k); + (&local[..], k + 1) + }; + let row = |nn: usize| &v[nn * w..(nn + 1) * w]; + let total = row(n)[k] as u64; let mut index = 0u64; let mut kk = k; for j in 0..n { @@ -173,10 +245,11 @@ pub fn encode_pulses(y: &[i32], k: usize, enc: &mut RangeEncoder, v: &mut Vec { pub i: usize, pub spread: usize, pub remaining_bits: i32, - pvq_v: Vec, iy: Vec, tmp: Vec, } @@ -217,7 +289,7 @@ impl BandEnc<'_, '_> { let q = qn as u32; let half = q >> 1; let ft = (half + 1) * (half + 1); - let (fl, fs) = if it <= half { (it * (it + 1) >> 1, it + 1) } else { (ft - ((q + 1 - it) * (q + 2 - it) >> 1), q + 1 - it) }; + let (fl, fs) = if it <= half { ((it * (it + 1)) >> 1, it + 1) } else { (ft - (((q + 1 - it) * (q + 2 - it)) >> 1), q + 1 - it) }; self.enc.encode(fl, fl + fs, ft); itheta = ((it * 16384) / q) as i32; } @@ -287,36 +359,29 @@ impl BandEnc<'_, '_> { iy.clear(); iy.resize(n, 0); pvq_search(&tmp, k, &mut iy); - encode_pulses(&iy, k, self.enc, &mut self.pvq_v); + encode_pulses(&iy, k, self.enc); self.iy = iy; self.tmp = tmp; } } } -/// Codes all band shapes (`quant_all_bands`, encoder side, long blocks). `x` holds `c` channels -/// of `n` normalised MDCT coefficients each. -#[allow(clippy::too_many_arguments)] -pub fn quant_all_bands( - m: &Mode, - start: usize, - end: usize, - x: &[f32], - n_total: usize, - c: usize, - pulses: &[i32; NB_EBANDS], - spread: usize, - dual_stereo: bool, - intensity: usize, - total_bits: i32, - mut balance: i32, - enc: &mut RangeEncoder, - lm: usize, - coded_bands: usize, -) { +/// Shape-coding controls, independent of the selected band allocation. +pub struct BandCoding { + pub spread: usize, + pub total_bits: i32, +} + +/// Codes all band shapes (`quant_all_bands`, encoder side, long blocks). `x` holds +/// `frame.channels` channels of `n_total` normalised MDCT coefficients each. +pub fn quant_all_bands(m: &Mode, frame: CeltFrame, x: &[f32], n_total: usize, allocation: &Allocation, coding: BandCoding, enc: &mut RangeEncoder) { + let CeltFrame { start, end, channels: c, lm } = frame; + let Allocation { pulses, dual_stereo, intensity, coded_bands, balance, .. } = allocation; + let (dual_stereo, intensity, coded_bands, mut balance) = (*dual_stereo, *intensity, *coded_bands, *balance); + let BandCoding { spread, total_bits } = coding; let mm = 1usize << lm; debug_assert!(c == 1 || dual_stereo || intensity <= start, "stereo frames are coded as dual stereo"); - let mut ctx = BandEnc { m, enc, i: 0, spread, remaining_bits: 0, pvq_v: Vec::new(), iy: Vec::new(), tmp: Vec::new() }; + let mut ctx = BandEnc { m, enc, i: 0, spread, remaining_bits: 0, iy: Vec::new(), tmp: Vec::new() }; for i in start..end { ctx.i = i; let xo = mm * EBANDS[i] as usize; @@ -362,7 +427,7 @@ mod tests { x[i + stride] = c * x2 + s * x1; x[i] = c * x1 + ms * x2; } - if len >= 2 * stride + 1 { + if len > 2 * stride { for i in (0..=len - 2 * stride - 1).rev() { let x1 = x[i]; let x2 = x[i + stride]; @@ -466,12 +531,11 @@ mod tests { #[test] fn pvq_index_roundtrip_exhaustive() { - let mut v = Vec::new(); for n in 1..6usize { for k in 1..6usize { for y in all_codewords(n, k) { let mut e = RangeEncoder::new(); - encode_pulses(&y, k, &mut e, &mut v); + encode_pulses(&y, k, &mut e); let buf = e.done(16); let mut d = RangeDecoder::new(&buf); assert_eq!(decode_pulses(n, k, &mut d), y, "n={n} k={k}"); @@ -480,6 +544,165 @@ mod tests { } } + /// The per-call table encoder this module used before the shared table. + fn encode_pulses_reference(y: &[i32], k: usize, enc: &mut RangeEncoder) { + let n = y.len(); + let w = k + 1; + let mut v = vec![0u64; (n + 1) * w]; + v[0] = 1; + for nn in 1..=n { + v[nn * w] = 1; + for kk in 1..=k { + let a = v[(nn - 1) * w + kk]; + let b = v[nn * w + kk - 1]; + let c = v[(nn - 1) * w + kk - 1]; + v[nn * w + kk] = a.saturating_add(b).saturating_add(c); + } + } + let total = v[n * w + k]; + let vv = |nn: usize, kx: usize| v[nn * w + kx]; + let mut index = 0u64; + let mut kk = k; + for j in 0..n { + if kk == 0 { + break; + } + let rem = n - j; + let t = y[j].unsigned_abs() as usize; + let mut base: u64 = (0..kk - t).map(|m| vv(rem - 1, m)).sum(); + if y[j] < 0 { + base += (vv(rem - 1, kk) + vv(rem, kk)) / 2; + } + index += base; + kk -= t; + } + if total >= 2 { + enc.uint(index as u32, total as u32); + } + } + + #[test] + fn pvq_table_encode_matches_per_call_table() { + let mut seed = 0x9E37_79B9_7F4A_7C15u64; + let mut next = move |m: u64| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed % m + }; + let mut checked = 0; + for n in (1..=PVQ_MAX_N).step_by(5).chain([PVQ_MAX_N]) { + for k in (1..=PVQ_MAX_K).step_by(3).chain([PVQ_MAX_K]) { + if crate::opus::encoder::rate::pvq_v(n, k) > u32::MAX as u128 { + continue; + } + for _ in 0..4 { + let mut y = vec![0i32; n]; + for _ in 0..k { + let at = next(n as u64) as usize; + y[at] += if next(2) == 0 { 1 } else { -1 }; + } + // Opposite pulses cancel; top up so that sum |y| = k again. + let mut have: usize = y.iter().map(|v| v.unsigned_abs() as usize).sum(); + while have < k { + let at = next(n as u64) as usize; + y[at] += if y[at] < 0 { -1 } else { 1 }; + have += 1; + } + let (mut a, mut b) = (RangeEncoder::new(), RangeEncoder::new()); + encode_pulses(&y, k, &mut a); + encode_pulses_reference(&y, k, &mut b); + assert_eq!(a.done(16), b.done(16), "n={n} k={k}"); + checked += 1; + } + } + } + assert!(checked > 100, "{checked}"); + } + + /// The scalar greedy search the vectorised one replaced. + fn pvq_search_ref(x: &[f32], k: usize, y: &mut [i32]) { + let n = x.len(); + y[..n].fill(0); + let abs: Vec = x.iter().map(|v| v.abs()).collect(); + let sum: f32 = abs.iter().sum(); + if !sum.is_finite() || sum <= 1e-15 { + y[0] = k as i32; + return; + } + let mut placed = 0usize; + let mut xy = 0f32; + let mut yy = 0f32; + if k > n / 2 { + let r = (k as f32 - 1.0) / sum; + for j in 0..n { + let p = (abs[j] * r).floor() as i32; + y[j] = p; + placed += p as usize; + xy += abs[j] * p as f32; + yy += (p * p) as f32; + } + } + while placed < k { + let mut best = 0usize; + let mut best_num = -1f32; + let mut best_den = 1f32; + for j in 0..n { + let num = xy + abs[j]; + let num = num * num; + let den = yy + 2.0 * y[j] as f32 + 1.0; + if num * best_den > best_num * den { + best_num = num; + best_den = den; + best = j; + } + } + xy += abs[best]; + yy += 2.0 * y[best] as f32 + 1.0; + y[best] += 1; + placed += 1; + } + for j in 0..n { + if x[j] < 0.0 { + y[j] = -y[j]; + } + } + } + + #[test] + fn pvq_search_matches_scalar_reference() { + let mut state = 0x9e3779b97f4a7c15u64; + let mut rnd = move || { + state ^= state << 13; + state ^= state >> 7; + state ^= state << 17; + (state >> 40) as f32 / (1u64 << 24) as f32 * 2.0 - 1.0 + }; + let mut a = vec![0i32; 200]; + let mut b = vec![0i32; 200]; + let mut cases = 0; + for round in 0..6000 { + let n = 1 + (round * 37 + (rnd().abs() * 176.0) as usize) % 176; + let k = 1 + (rnd().abs() * 127.0) as usize; + let scale = [1.0f32, 1e-9, 1e6, 3e-3][round % 4]; + let x: Vec = (0..n) + .map(|_| match round % 6 { + 0 => rnd(), + 1 => if rnd() > 0.8 { rnd() } else { 0.0 }, + 2 => (rnd() * 4.0).round() / 4.0, + 3 => if rnd() > 0.0 { 1.0 } else { -1.0 }, + 4 => rnd().powi(5), + _ => if rnd() > 0.95 { 3.0 * rnd() } else { 1e-4 * rnd() }, + } * scale) + .collect(); + pvq_search(&x, k, &mut a); + pvq_search_ref(&x, k, &mut b); + assert_eq!(a[..n], b[..n], "n={n} k={k} round={round}"); + cases += 1; + } + assert_eq!(cases, 6000); + } + #[test] fn pvq_search_finds_good_direction() { let x: Vec = (0..32).map(|i| ((i as f32) * 0.7).sin()).collect(); diff --git a/crates/audio/src/opus/encoder/energy.rs b/crates/audio/src/opus/encoder/energy.rs index 8d4f357d0..fb5cc5a4b 100644 --- a/crates/audio/src/opus/encoder/energy.rs +++ b/crates/audio/src/opus/encoder/energy.rs @@ -4,8 +4,9 @@ //! The quantised energies are updated with exactly the decoder's arithmetic so the prediction //! state never drifts between encoder and decoder. +use crate::opus::encoder::CeltFrame; use crate::opus::encoder::range::RangeEncoder; -use crate::opus::encoder::rate::MAX_FINE_BITS; +use crate::opus::encoder::rate::{Allocation, MAX_FINE_BITS}; use crate::opus::encoder::tables::{BETA_COEF, BETA_INTRA, E_PROB_MODEL, NB_EBANDS, PRED_COEF, SMALL_ENERGY_ICDF}; const LAPLACE_MINP: u32 = 1; @@ -56,18 +57,15 @@ pub fn laplace_encode(enc: &mut RangeEncoder, value: i32, mut fs: u32, decay: u3 /// `quant_coarse_energy` for one prediction mode. `e` holds the target log2 energies (relative /// to `E_MEANS`), `old_e` the quantised energies, updated in place. `budget` is the frame size in /// bits. -#[allow(clippy::too_many_arguments)] pub fn quant_coarse_energy( - start: usize, - end: usize, + frame: CeltFrame, e: &[f32; 2 * NB_EBANDS], old_e: &mut [f32; 2 * NB_EBANDS], intra: bool, enc: &mut RangeEncoder, - c: usize, - lm: usize, budget: i32, ) { + let CeltFrame { start, end, channels: c, lm } = frame; let prob = &E_PROB_MODEL[lm][intra as usize]; let (coef, beta) = if intra { (0.0, BETA_INTRA) } else { (PRED_COEF[lm], BETA_COEF[lm]) }; let mut prev = [0f32; 2]; @@ -112,14 +110,13 @@ pub fn quant_coarse_energy( /// `quant_fine_energy`: raw bits refining each band's quantised energy. pub fn quant_fine_energy( - start: usize, - end: usize, + frame: CeltFrame, e: &[f32; 2 * NB_EBANDS], old_e: &mut [f32; 2 * NB_EBANDS], fine_quant: &[i32; NB_EBANDS], enc: &mut RangeEncoder, - c: usize, ) { + let CeltFrame { start, end, channels: c, .. } = frame; for i in start..end { let fq = fine_quant[i]; if fq <= 0 { @@ -139,18 +136,16 @@ pub fn quant_fine_energy( /// `quant_energy_finalise`: spends the bits left at the end of the frame on one more bit of /// energy resolution per band, in priority order. -#[allow(clippy::too_many_arguments)] pub fn quant_energy_finalise( - start: usize, - end: usize, + frame: CeltFrame, e: &[f32; 2 * NB_EBANDS], old_e: &mut [f32; 2 * NB_EBANDS], - fine_quant: &[i32; NB_EBANDS], - fine_priority: &[i32; NB_EBANDS], + allocation: &Allocation, mut bits_left: i32, enc: &mut RangeEncoder, - c: usize, ) { + let CeltFrame { start, end, channels: c, .. } = frame; + let Allocation { fine_quant, fine_priority, .. } = allocation; for prio in 0..2 { let mut i = start; while i < end && bits_left >= c as i32 { diff --git a/crates/audio/src/opus/encoder/mdct.rs b/crates/audio/src/opus/encoder/mdct.rs index 6a42cce96..fa5624514 100644 --- a/crates/audio/src/opus/encoder/mdct.rs +++ b/crates/audio/src/opus/encoder/mdct.rs @@ -24,6 +24,10 @@ impl Cpx { fn add(self, o: Cpx) -> Cpx { Cpx { re: self.re + o.re, im: self.im + o.im } } + #[inline] + fn sub(self, o: Cpx) -> Cpx { + Cpx { re: self.re - o.re, im: self.im - o.im } + } } /// Mixed-radix (2, 3, 4, 5) forward complex FFT, unscaled (decimation in time, recursive). @@ -71,18 +75,64 @@ impl Fft { self.rec(&mut out[q * m..(q + 1) * m], &inp[q * in_stride..], in_stride * p, m, fi + 1, tw_stride * p); } } - let big = self.n; - let mut t = [Cpx::default(); 5]; - for k in 0..m { - for (q, tq) in t.iter_mut().enumerate().take(p) { - *tq = out[q * m + k].mul(self.twiddle[(q * k * tw_stride) % big]); + // The twiddle index q * k * tw_stride is always below n, so no modulo is needed. + let tw = &self.twiddle; + match p { + 2 => { + for k in 0..m { + let a0 = out[k]; + let a1 = out[m + k].mul(tw[k * tw_stride]); + out[k] = a0.add(a1); + out[m + k] = a0.sub(a1); + } } - for q2 in 0..p { - let mut acc = Cpx::default(); - for (q, tq) in t.iter().enumerate().take(p) { - acc = acc.add(tq.mul(self.twiddle[((q * q2) % p) * (big / p)])); + 4 => { + for k in 0..m { + let t0 = out[k]; + let t1 = out[m + k].mul(tw[k * tw_stride]); + let t2 = out[2 * m + k].mul(tw[2 * k * tw_stride]); + let t3 = out[3 * m + k].mul(tw[3 * k * tw_stride]); + let (s0, d0) = (t0.add(t2), t0.sub(t2)); + let (s1, d1) = (t1.add(t3), t1.sub(t3)); + out[k] = s0.add(s1); + out[2 * m + k] = s0.sub(s1); + // The odd outputs combine d0 with -j * d1 = (d1.im, -d1.re) and its negative. + out[m + k] = Cpx { re: d0.re + d1.im, im: d0.im - d1.re }; + out[3 * m + k] = Cpx { re: d0.re - d1.im, im: d0.im + d1.re }; + } + } + 3 => { + // Imaginary part of exp(-2 pi i / 3) = -sqrt(3) / 2. + let h = tw[self.n / 3].im; + for k in 0..m { + let t0 = out[k]; + let t1 = out[m + k].mul(tw[k * tw_stride]); + let t2 = out[2 * m + k].mul(tw[2 * k * tw_stride]); + let (s, d) = (t1.add(t2), t1.sub(t2)); + let a = Cpx { re: t0.re - 0.5 * s.re, im: t0.im - 0.5 * s.im }; + out[k] = t0.add(s); + // j * h * d = (-h * d.im, h * d.re) + out[m + k] = Cpx { re: a.re - h * d.im, im: a.im + h * d.re }; + out[2 * m + k] = Cpx { re: a.re + h * d.im, im: a.im - h * d.re }; + } + } + _ => { + // Generic DFT butterfly (radix 5, once per transform). + let roots: [Cpx; 5] = std::array::from_fn(|q| if q < p { tw[q * (self.n / p)] } else { Cpx::default() }); + let mut t = [Cpx::default(); 5]; + for k in 0..m { + t[0] = out[k]; + for q in 1..p { + t[q] = out[q * m + k].mul(tw[q * k * tw_stride]); + } + for q2 in 0..p { + let mut acc = t[0]; + for q in 1..p { + acc = acc.add(t[q].mul(roots[(q * q2) % p])); + } + out[q2 * m + k] = acc; + } } - out[q2 * m + k] = acc; } } } @@ -199,6 +249,31 @@ mod tests { .collect() } + #[test] + fn fft_matches_dft() { + // Every radix combination, including the 480-point size of the 20 ms frame. + for &n in &[2usize, 3, 4, 5, 6, 8, 12, 15, 16, 20, 24, 30, 48, 60, 120, 240, 480] { + let fft = Fft::new(n); + let x: Vec = (0..n).map(|i| Cpx { re: ((i * 37 % 101) as f32 - 50.0) / 7.0, im: ((i * 53 % 89) as f32 - 44.0) / 9.0 }).collect(); + let mut y = x.clone(); + fft.process(&mut y, &mut Vec::new()); + for k in 0..n { + let (mut re, mut im) = (0f64, 0f64); + for (j, v) in x.iter().enumerate() { + let ph = -2.0 * std::f64::consts::PI * ((j * k) % n) as f64 / n as f64; + re += v.re as f64 * ph.cos() - v.im as f64 * ph.sin(); + im += v.re as f64 * ph.sin() + v.im as f64 * ph.cos(); + } + assert!( + (y[k].re as f64 - re).abs() < 2e-3 && (y[k].im as f64 - im).abs() < 2e-3, + "n={n} k={k}: ({}, {}) vs ({re}, {im})", + y[k].re, + y[k].im + ); + } + } + } + #[test] fn matches_direct_formula() { for &n in &[240usize, 960] { diff --git a/crates/audio/src/opus/encoder/mod.rs b/crates/audio/src/opus/encoder/mod.rs index cbb0d7984..a635cce40 100644 --- a/crates/audio/src/opus/encoder/mod.rs +++ b/crates/audio/src/opus/encoder/mod.rs @@ -10,8 +10,9 @@ //! above in the same range-coded frame; constant packet size. //! - **CELT** (fullband): MDCT transform coding; constant packet size. //! -//! From EffectCraft's `effectcraft-opusenc` (storytold/effectcraft@c618429). Public items keep their -//! names, so EffectCraft can re-export this module. +//! From EffectCraft's `effectcraft-opusenc` (storytold/effectcraft@c618429), with its later speed-ups +//! and CELT stage refactor up to storytold/effectcraft@d6c9ad66. Public items keep their names, so +//! EffectCraft can re-export this module. //! //! ``` //! # #[cfg(feature = "opus-encode")] { @@ -26,6 +27,7 @@ //! # } //! ``` +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] // The bit-allocation code mirrors the normative integer arithmetic; keep C-like shift expressions. #![allow(clippy::precedence, clippy::int_plus_one)] @@ -40,15 +42,24 @@ mod tables; use std::sync::OnceLock; -use bands::{SPREAD_NORMAL, quant_all_bands}; +use bands::{BandCoding, SPREAD_NORMAL, quant_all_bands}; use energy::{quant_coarse_energy, quant_energy_finalise, quant_fine_energy}; use mdct::Mdct; use range::{BITRES, RangeEncoder}; -use rate::{Mode as CeltMode, compute_allocation}; +use rate::{AllocationInput, Mode as CeltMode, compute_allocation}; use resample::Downsampler; use silk::SilkEncoder; use tables::{E_MEANS, EBANDS, NB_EBANDS, OVERLAP, PREEMPH, SPREAD_ICDF, TF_SELECT_TABLE, TRIM_ICDF}; +/// Band range, channel count and transform size shared by CELT coding stages. +#[derive(Clone, Copy)] +struct CeltFrame { + start: usize, + end: usize, + channels: usize, + lm: usize, +} + /// Samples per channel in one 20 ms frame at 48 kHz. const N: usize = 960; /// LM of a 20 ms frame (8 short blocks). @@ -338,7 +349,7 @@ impl Encoder { Mode::Celt => { let (log_e, silence) = self.celt_analysis(pcm); let len = self.frame_bytes; - packet.extend_from_slice(&self.encode_celt(RangeEncoder::new(), len, 0, NB_EBANDS, &log_e, silence)); + packet.extend_from_slice(&self.encode_celt(RangeEncoder::new(), len, 0..NB_EBANDS, &log_e, silence)); } Mode::Silk => { let x = self.silk_input(pcm); @@ -368,7 +379,7 @@ impl Encoder { } let (log_e, _) = self.celt_analysis(pcm); let end = if self.bandwidth == Bandwidth::SuperWideband { 19 } else { NB_EBANDS }; - packet.extend_from_slice(&self.encode_celt(enc, len, HYBRID_START_BAND, end, &log_e, false)); + packet.extend_from_slice(&self.encode_celt(enc, len, HYBRID_START_BAND..end, &log_e, false)); } } Ok(packet) @@ -430,10 +441,18 @@ impl Encoder { /// Codes one CELT frame (RFC 6716 §4.3, in bitstream order) of `len` bytes into `enc` /// (which already holds the SILK frame in hybrid mode), coding bands `start..end`, and /// finalises the frame. - #[allow(clippy::too_many_arguments)] - fn encode_celt(&mut self, mut enc: RangeEncoder, len: usize, start: usize, end: usize, log_e: &[f32; 2 * NB_EBANDS], silence: bool) -> Vec { + fn encode_celt( + &mut self, + mut enc: RangeEncoder, + len: usize, + bands: std::ops::Range, + log_e: &[f32; 2 * NB_EBANDS], + silence: bool, + ) -> Vec { let m = celt_mode(); let c = self.channels; + let frame = CeltFrame { start: bands.start, end: bands.end, channels: c, lm: LM }; + let CeltFrame { start, end, .. } = frame; if c == 1 { for i in 0..NB_EBANDS { self.old_band_e[i] = self.old_band_e[i].max(self.old_band_e[NB_EBANDS + i]); @@ -462,10 +481,10 @@ impl Encoder { let mut enc_intra = enc.clone(); let mut old_intra = self.old_band_e; enc_intra.bit_logp(true, 3); - quant_coarse_energy(start, end, log_e, &mut old_intra, true, &mut enc_intra, c, LM, total_bits_raw); + quant_coarse_energy(frame, log_e, &mut old_intra, true, &mut enc_intra, total_bits_raw); enc.bit_logp(false, 3); let mut old_inter = self.old_band_e; - quant_coarse_energy(start, end, log_e, &mut old_inter, false, &mut enc, c, LM, total_bits_raw); + quant_coarse_energy(frame, log_e, &mut old_inter, false, &mut enc, total_bits_raw); if enc_intra.tell_frac() < enc.tell_frac() { enc = enc_intra; self.old_band_e = old_intra; @@ -473,14 +492,14 @@ impl Encoder { self.old_band_e = old_inter; } } else { - quant_coarse_energy(start, end, log_e, &mut self.old_band_e, false, &mut enc, c, LM, total_bits_raw); + quant_coarse_energy(frame, log_e, &mut self.old_band_e, false, &mut enc, total_bits_raw); } // TF resolution: no changes. { let mut budget = total_bits_raw; let mut tell = enc.tell(); let mut logp = 4; - let tf_select_rsv = LM > 0 && tell + logp + 1 <= budget; + let tf_select_rsv = LM > 0 && tell + logp < budget; budget -= tf_select_rsv as i32; for _ in start..end { if tell + logp <= budget { @@ -512,27 +531,11 @@ impl Encoder { enc.icdf(alloc_trim as usize, &TRIM_ICDF, 7); } let bits = (total_bits_raw << BITRES) - enc.tell_frac() - 1; - let alloc = compute_allocation(m, start, end, &offsets, &cap, alloc_trim, bits, c, LM, &mut enc); - quant_fine_energy(start, end, log_e, &mut self.old_band_e, &alloc.fine_quant, &mut enc, c); - quant_all_bands( - m, - start, - end, - &self.freq, - N, - c, - &alloc.pulses, - SPREAD_NORMAL, - alloc.dual_stereo, - alloc.intensity, - total_bits_raw << BITRES, - alloc.balance, - &mut enc, - LM, - alloc.coded_bands, - ); + let alloc = compute_allocation(m, frame, AllocationInput { offsets: &offsets, cap: &cap, trim: alloc_trim, bits }, &mut enc); + quant_fine_energy(frame, log_e, &mut self.old_band_e, &alloc.fine_quant, &mut enc); + quant_all_bands(m, frame, &self.freq, N, &alloc, BandCoding { spread: SPREAD_NORMAL, total_bits: total_bits_raw << BITRES }, &mut enc); let bits_left = total_bits_raw - enc.tell(); - quant_energy_finalise(start, end, log_e, &mut self.old_band_e, &alloc.fine_quant, &alloc.fine_priority, bits_left, &mut enc, c); + quant_energy_finalise(frame, log_e, &mut self.old_band_e, &alloc, bits_left, &mut enc); debug_assert!(enc.tell() <= total_bits_raw, "frame overflow: {} > {}", enc.tell(), total_bits_raw); if c == 1 { let (a, b) = self.old_band_e.split_at_mut(NB_EBANDS); diff --git a/crates/audio/src/opus/encoder/rate.rs b/crates/audio/src/opus/encoder/rate.rs index c360f9da7..58349bbd7 100644 --- a/crates/audio/src/opus/encoder/rate.rs +++ b/crates/audio/src/opus/encoder/rate.rs @@ -4,6 +4,7 @@ //! spec-derived `filmcraft-opus` decoder (first-party, MIT OR Apache-2.0) step by step; the only //! difference is that the encoder *writes* the band-skip, intensity and dual-stereo symbols. +use crate::opus::encoder::CeltFrame; use crate::opus::encoder::range::{BITRES, RangeEncoder, ilog}; use crate::opus::encoder::tables::{ALLOC, EBANDS, LOG2_FRAC_TABLE, MAX_LM, NB_EBANDS}; @@ -127,7 +128,7 @@ impl Mode { let mut cap = [0i32; NB_EBANDS]; for (i, cp) in cap.iter_mut().enumerate() { let n = (EBANDS[i + 1] - EBANDS[i]) << lm; - *cp = (self.caps[NB_EBANDS * (2 * lm + c - 1) + i] as i32 + 64) * c as i32 * n >> 2; + *cp = ((self.caps[NB_EBANDS * (2 * lm + c - 1) + i] as i32 + 64) * c as i32 * n) >> 2; } cap } @@ -200,7 +201,7 @@ fn compute_caps(m: &Mode) -> Vec { let mut n0 = EBANDS[j + 1] - EBANDS[j]; let max_bits = if n0 << i == 1 { - c * (1 + MAX_FINE_BITS) << BITRES + (c * (1 + MAX_FINE_BITS)) << BITRES } else { let mut lm0 = 0; if n0 > 2 { @@ -240,7 +241,7 @@ fn compute_caps(m: &Mode) -> Vec { let num = mb + ndof * offset; let den = (ndof - 1) << BITRES; let qb = ((num + (den >> 1)) / den).min(MAX_FINE_BITS); - mb += c * qb << BITRES; + mb += (c * qb) << BITRES; mb }; let w = c * ((EBANDS[j + 1] - EBANDS[j]) << i); @@ -263,21 +264,19 @@ pub struct Allocation { pub fine_priority: [i32; NB_EBANDS], } +/// Per-band limits, boosts and available budget for the allocation search. +pub struct AllocationInput<'a> { + pub offsets: &'a [i32; NB_EBANDS], + pub cap: &'a [i32; NB_EBANDS], + pub trim: i32, + pub bits: i32, +} + /// `clt_compute_allocation`, encoder side. Stereo frames are always coded as dual (L/R) stereo /// with no intensity bands, and no band is skipped voluntarily. -#[allow(clippy::too_many_arguments)] -pub fn compute_allocation( - m: &Mode, - start: usize, - end: usize, - offsets: &[i32; NB_EBANDS], - cap: &[i32; NB_EBANDS], - alloc_trim: i32, - total: i32, - c: usize, - lm: usize, - enc: &mut RangeEncoder, -) -> Allocation { +pub fn compute_allocation(m: &Mode, frame: CeltFrame, input: AllocationInput<'_>, enc: &mut RangeEncoder) -> Allocation { + let CeltFrame { start, end, channels: c, lm } = frame; + let AllocationInput { offsets, cap, trim: alloc_trim, bits: total } = input; let ci = c as i32; let lmi = lm as i32; let mut total = total.max(0); @@ -302,8 +301,8 @@ pub fn compute_allocation( let mut trim_offset = [0i32; NB_EBANDS]; for j in start..end { let w = EBANDS[j + 1] - EBANDS[j]; - thresh[j] = (ci << BITRES).max((3 * w << lmi << BITRES) >> 4); - trim_offset[j] = ci * w * (alloc_trim - 5 - lmi) * (end as i32 - j as i32 - 1) * (1 << (lmi + BITRES as i32)) >> 6; + thresh[j] = (ci << BITRES).max(((3 * w) << lmi << BITRES) >> 4); + trim_offset[j] = (ci * w * (alloc_trim - 5 - lmi) * (end as i32 - j as i32 - 1) * (1 << (lmi + BITRES as i32))) >> 6; if w << lmi == 1 { trim_offset[j] -= ci << BITRES; } @@ -317,7 +316,7 @@ pub fn compute_allocation( let mid = (lo + hi) >> 1; for j in (start..end).rev() { let n = EBANDS[j + 1] - EBANDS[j]; - let mut bitsj = ci * n * (ALLOC[mid as usize][j] as i32) << lmi >> 2; + let mut bitsj = (ci * n * (ALLOC[mid as usize][j] as i32)) << lmi >> 2; if bitsj > 0 { bitsj = (bitsj + trim_offset[j]).max(0); } @@ -342,8 +341,8 @@ pub fn compute_allocation( lo -= 1; for j in start..end { let n = EBANDS[j + 1] - EBANDS[j]; - let mut bits1j = ci * n * (ALLOC[lo as usize][j] as i32) << lmi >> 2; - let mut bits2j = if hi >= nb_alloc { cap[j] } else { ci * n * (ALLOC[hi as usize][j] as i32) << lmi >> 2 }; + let mut bits1j = (ci * n * (ALLOC[lo as usize][j] as i32)) << lmi >> 2; + let mut bits2j = if hi >= nb_alloc { cap[j] } else { (ci * n * (ALLOC[hi as usize][j] as i32)) << lmi >> 2 }; if bits1j > 0 { bits1j = (bits1j + trim_offset[j]).max(0); } @@ -361,27 +360,28 @@ pub fn compute_allocation( bits1[j] = bits1j; bits2[j] = bits2j; } - interp_bits2pulses(m, start, end, skip_start, &bits1, &bits2, &thresh, cap, total, skip_rsv, intensity_rsv, dual_stereo_rsv, c, lm, enc) + let interpolation = + AllocationInterpolation { skip_start, bits1: &bits1, bits2: &bits2, thresh: &thresh, cap, total, skip_rsv, intensity_rsv, dual_stereo_rsv }; + interp_bits2pulses(m, frame, interpolation, enc) } -#[allow(clippy::too_many_arguments)] -fn interp_bits2pulses( - m: &Mode, - start: usize, - end: usize, +/// Bracketing allocation vectors and signalling reserves for the interpolation stage. +struct AllocationInterpolation<'a> { skip_start: usize, - bits1: &[i32; NB_EBANDS], - bits2: &[i32; NB_EBANDS], - thresh: &[i32; NB_EBANDS], - cap: &[i32; NB_EBANDS], - mut total: i32, + bits1: &'a [i32; NB_EBANDS], + bits2: &'a [i32; NB_EBANDS], + thresh: &'a [i32; NB_EBANDS], + cap: &'a [i32; NB_EBANDS], + total: i32, skip_rsv: i32, - mut intensity_rsv: i32, - mut dual_stereo_rsv: i32, - c: usize, - lm: usize, - enc: &mut RangeEncoder, -) -> Allocation { + intensity_rsv: i32, + dual_stereo_rsv: i32, +} + +fn interp_bits2pulses(m: &Mode, frame: CeltFrame, interpolation: AllocationInterpolation<'_>, enc: &mut RangeEncoder) -> Allocation { + let CeltFrame { start, end, channels: c, lm } = frame; + let AllocationInterpolation { skip_start, bits1, bits2, thresh, cap, mut total, skip_rsv, mut intensity_rsv, mut dual_stereo_rsv } = + interpolation; let ci = c as i32; let alloc_floor = ci << BITRES; let stereo = (c > 1) as i32; @@ -393,7 +393,7 @@ fn interp_bits2pulses( let mut psum = 0; let mut done = false; for j in (start..end).rev() { - let tmp = bits1[j] + (mid * bits2[j] >> ALLOC_STEPS); + let tmp = bits1[j] + ((mid * bits2[j]) >> ALLOC_STEPS); if tmp >= thresh[j] || done { done = true; psum += tmp.min(cap[j]); @@ -411,7 +411,7 @@ fn interp_bits2pulses( let mut psum = 0; let mut done = false; for j in (start..end).rev() { - let mut tmp = bits1[j] + (lo * bits2[j] >> ALLOC_STEPS); + let mut tmp = bits1[j] + ((lo * bits2[j]) >> ALLOC_STEPS); if tmp < thresh[j] && !done { tmp = if tmp >= alloc_floor { alloc_floor } else { 0 }; } else { @@ -500,9 +500,9 @@ fn interp_bits2pulses( if n == 2 { offset += (den << BITRES) >> 2; } - if bits[j] + offset < den * 2 << BITRES { + if bits[j] + offset < (den * 2) << BITRES { offset += nclogn >> 2; - } else if bits[j] + offset < den * 3 << BITRES { + } else if bits[j] + offset < (den * 3) << BITRES { offset += nclogn >> 3; } let mut eb = (bits[j] + offset + (den << (BITRES - 1))).max(0); @@ -513,7 +513,7 @@ fn interp_bits2pulses( eb = eb.min(MAX_FINE_BITS); ebits[j] = eb; fine_priority[j] = (eb * (den << BITRES) >= bits[j] + offset) as i32; - bits[j] -= ci * eb << BITRES; + bits[j] -= (ci * eb) << BITRES; } else { excess = (bit - (ci << BITRES)).max(0); bits[j] = bit - excess; @@ -523,7 +523,7 @@ fn interp_bits2pulses( if excess > 0 { let extra_fine = (excess >> (stereo + BITRES as i32)).min(MAX_FINE_BITS - ebits[j]); ebits[j] += extra_fine; - let extra_bits = extra_fine * ci << BITRES; + let extra_bits = (extra_fine * ci) << BITRES; fine_priority[j] = (extra_bits >= excess - balance) as i32; excess -= extra_bits; } diff --git a/crates/audio/src/opus/encoder/resample.rs b/crates/audio/src/opus/encoder/resample.rs index 9045cb86d..1acc9a4e7 100644 --- a/crates/audio/src/opus/encoder/resample.rs +++ b/crates/audio/src/opus/encoder/resample.rs @@ -45,6 +45,25 @@ fn design(fc: f64, beta: f64, delay: usize) -> Vec { h.iter().map(|&v| v as f32).collect() } +/// Dot product with eight independent partial sums, which the compiler can vectorise (a single +/// running sum is bound by the latency of the float addition). The sums are combined pairwise. +#[inline] +fn dot8(a: &[f32], b: &[f32]) -> f32 { + let mut s = [0f32; 8]; + let (ca, ra) = a.as_chunks::<8>(); + let (cb, rb) = b.as_chunks::<8>(); + for (x, y) in ca.iter().zip(cb) { + for l in 0..8 { + s[l] += x[l] * y[l]; + } + } + let mut acc = ((s[0] + s[4]) + (s[1] + s[5])) + ((s[2] + s[6]) + (s[3] + s[7])); + for (x, y) in ra.iter().zip(rb) { + acc += x * y; + } + acc +} + /// Decimates one channel of 48 kHz audio by 3, 4 or 6. #[derive(Clone)] pub struct Downsampler { @@ -77,7 +96,7 @@ impl Downsampler { let t = taps - 1 + self.factor * j; let win = &self.buf[t + 1 - taps..=t]; // h is symmetric, so the window can be taken in forward order. - out.push(win.iter().zip(&self.h).map(|(a, b)| a * b).sum()); + out.push(dot8(win, &self.h)); } let n = self.buf.len(); self.buf.copy_within(n - (taps - 1).., 0); @@ -97,6 +116,16 @@ mod tests { (x.iter().map(|&v| (v as f64).powi(2)).sum::() / x.len() as f64).sqrt() } + #[test] + fn dot8_matches_sequential_sum() { + for len in [0usize, 1, 7, 8, 9, 63, 167, 251] { + let a: Vec = (0..len).map(|i| ((i * 37 % 101) as f32 - 50.0) / 50.0).collect(); + let b: Vec = (0..len).map(|i| ((i * 53 % 89) as f32 - 44.0) / 44.0).collect(); + let exact: f64 = a.iter().zip(&b).map(|(&x, &y)| x as f64 * y as f64).sum(); + assert!((dot8(&a, &b) as f64 - exact).abs() < 1e-4, "len {len}"); + } + } + #[test] fn passes_band_and_rejects_aliases() { for &(fs, pass, stop) in &[(16_000u32, 6_000.0, 8_600.0), (12_000, 4_500.0, 6_500.0), (8_000, 3_000.0, 4_500.0)] { diff --git a/crates/audio/src/opus/encoder/silk/analysis.rs b/crates/audio/src/opus/encoder/silk/analysis.rs index 439331076..d80052046 100644 --- a/crates/audio/src/opus/encoder/silk/analysis.rs +++ b/crates/audio/src/opus/encoder/silk/analysis.rs @@ -69,28 +69,55 @@ pub fn levinson(r: &[f64], order: usize) -> Vec { } /// Evaluates the symmetric polynomial `p` (length `d + 1`, `d` even) as the cosine series -/// `e^{jωd/2} P(e^{jω})`. -fn cos_series(p: &[f64], w: f64) -> f64 { +/// `e^{jωd/2} P(e^{jω})`, with `cos_m(m)` giving `cos(m ω)`. +#[inline] +fn cos_series_with(p: &[f64], cos_m: impl Fn(usize) -> f64) -> f64 { let h = (p.len() - 1) / 2; let mut s = p[h]; for k in 0..h { - s += 2.0 * p[k] * ((h - k) as f64 * w).cos(); + s += 2.0 * p[k] * cos_m(h - k); } s } +fn cos_series(p: &[f64], w: f64) -> f64 { + cos_series_with(p, |m| (m as f64 * w).cos()) +} + +/// Number of grid intervals the root search scans over `[0, Ï€]`. +const GRID: usize = 1024; +/// Largest multiple of ω in the series (half the LPC order, at most 16 / 2). +const MAX_H: usize = 8; +/// Bisection steps per root: the bracket shrinks to 3e-9 rad, far below one Q15 NLSF step (9.6e-5 rad). +const BISECT_STEPS: usize = 20; + +/// `cos(m · Ï€ g / GRID)` for every grid point `g` and `m` in `0..=MAX_H`, computed with the same +/// expression as [`cos_series`], so the table values are bit-identical to calling `cos` there. +fn grid_cos() -> &'static [[f64; MAX_H + 1]] { + static TABLE: std::sync::OnceLock> = std::sync::OnceLock::new(); + TABLE.get_or_init(|| { + (0..=GRID) + .map(|g| { + let w = std::f64::consts::PI * g as f64 / GRID as f64; + std::array::from_fn(|m| (m as f64 * w).cos()) + }) + .collect() + }) +} + fn roots(p: &[f64], out: &mut Vec) { - const GRID: usize = 1024; + let grid = grid_cos(); + let at_grid = |g: usize| cos_series_with(p, |m| grid[g][m]); let mut w0 = 0.0; - let mut f0 = cos_series(p, w0); + let mut f0 = at_grid(0); for g in 1..=GRID { let w1 = std::f64::consts::PI * g as f64 / GRID as f64; - let f1 = cos_series(p, w1); + let f1 = at_grid(g); if f0 == 0.0 { out.push(w0); } else if f0 * f1 < 0.0 { let (mut lo, mut hi, mut flo) = (w0, w1, f0); - for _ in 0..40 { + for _ in 0..BISECT_STEPS { let mid = 0.5 * (lo + hi); let fm = cos_series(p, mid); if fm * flo <= 0.0 { @@ -111,6 +138,9 @@ fn roots(p: &[f64], out: &mut Vec) { /// when the root search fails (unstable or degenerate filter). pub fn a2nlsf(a: &[f64]) -> Option> { let d = a.len(); + if d > 2 * MAX_H { + return None; + } let mut c = vec![0f64; d + 2]; c[0] = 1.0; for k in 0..d { @@ -199,9 +229,20 @@ pub fn pitch_search(x: &[f32], start: usize, len: usize, fs_khz: usize) -> Pitch let score = full[(lag - min_lag) as usize]; // Contour: per-subframe lag offsets around the chosen lag. let cb: &[[i32; 4]] = if fs_khz == 8 { &CB_NB_20 } else { &CB_WB_20 }; + // The contour search revisits the same (subframe, lag) pairs for neighbouring base lags and + // codebook entries, so each correlation is computed once. + let span = (18 * fs - min_lag + 1) as usize; + let memo = std::cell::RefCell::new(vec![f64::NAN; 4 * span]); let sub_score = |k: usize, l: i32| -> f64 { let l = l.clamp(min_lag, 18 * fs); - ncorr(x, start + k * sub..start + (k + 1) * sub, l as usize) + let slot = k * span + (l - min_lag) as usize; + let cached = memo.borrow()[slot]; + if !cached.is_nan() { + return cached; + } + let v = ncorr(x, start + k * sub..start + (k + 1) * sub, l as usize); + memo.borrow_mut()[slot] = v; + v }; let mut best = (f64::MIN, lag, 0usize); for l0 in (lag - 2).max(min_lag)..=(lag + 2).min(max_lag) { @@ -319,4 +360,59 @@ mod tests { } } } + + /// The root search as it was before the grid table: `cos` called for every evaluation. + fn roots_reference(p: &[f64], out: &mut Vec) { + let mut w0 = 0.0; + let mut f0 = cos_series(p, w0); + for g in 1..=GRID { + let w1 = std::f64::consts::PI * g as f64 / GRID as f64; + let f1 = cos_series(p, w1); + if f0 == 0.0 { + out.push(w0); + } else if f0 * f1 < 0.0 { + let (mut lo, mut hi, mut flo) = (w0, w1, f0); + for _ in 0..BISECT_STEPS { + let mid = 0.5 * (lo + hi); + let fm = cos_series(p, mid); + if fm * flo <= 0.0 { + hi = mid; + } else { + lo = mid; + flo = fm; + } + } + out.push(0.5 * (lo + hi)); + } + w0 = w1; + f0 = f1; + } + } + + #[test] + fn grid_table_roots_are_bit_identical_to_direct_cos() { + let mut seed = 7u64; + let mut rnd = move || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + (seed >> 11) as f64 / (1u64 << 53) as f64 * 2.0 - 1.0 + }; + let mut found = 0; + for len in [11usize, 17] { + for _ in 0..300 { + let mut p = vec![0f64; len]; + for k in 0..len / 2 + 1 { + p[k] = rnd(); + p[len - 1 - k] = p[k]; + } + let (mut a, mut b) = (Vec::new(), Vec::new()); + roots(&p, &mut a); + roots_reference(&p, &mut b); + assert_eq!(a.iter().map(|v| v.to_bits()).collect::>(), b.iter().map(|v| v.to_bits()).collect::>()); + found += a.len(); + } + } + assert!(found > 1000, "{found}"); + } } diff --git a/crates/audio/src/opus/encoder/silk/mod.rs b/crates/audio/src/opus/encoder/silk/mod.rs index d82e537e0..0fda4f59a 100644 --- a/crates/audio/src/opus/encoder/silk/mod.rs +++ b/crates/audio/src/opus/encoder/silk/mod.rs @@ -129,6 +129,48 @@ struct Coded { pulses: Vec, } +/// Noise-feedback history (decoded − input), most recent first, as a double-length ring: each +/// sample is stored at `pos` and `pos + MAX_LPC_ORDER`, so `buf[pos..pos + MAX_LPC_ORDER]` is +/// always the contiguous window and a push costs two stores instead of a shift. +struct NfRing { + buf: [f64; 2 * MAX_LPC_ORDER], + /// Index of the most recent sample, always below `MAX_LPC_ORDER`. + pos: usize, +} + +impl NfRing { + fn new(hist: &[f64; MAX_LPC_ORDER]) -> Self { + let mut buf = [0.0; 2 * MAX_LPC_ORDER]; + buf[..MAX_LPC_ORDER].copy_from_slice(hist); + buf[MAX_LPC_ORDER..].copy_from_slice(hist); + NfRing { buf, pos: 0 } + } + + /// The history, most recent first. + fn window(&self) -> &[f64] { + self.buf.get(self.pos..self.pos + MAX_LPC_ORDER).unwrap_or(&[]) + } + + /// Makes `v` the most recent sample and drops the oldest. + fn push(&mut self, v: f64) { + self.pos = self.pos.checked_sub(1).unwrap_or(MAX_LPC_ORDER - 1); + for i in [self.pos, self.pos + MAX_LPC_ORDER] { + if let Some(slot) = self.buf.get_mut(i) { + *slot = v; + } + } + } + + /// The history as a plain array, most recent first. + fn to_array(&self) -> [f64; MAX_LPC_ORDER] { + let mut out = [0.0; MAX_LPC_ORDER]; + for (o, v) in out.iter_mut().zip(self.window()) { + *o = *v; + } + out + } +} + #[derive(Clone)] struct Channel { fs_khz: usize, @@ -237,7 +279,7 @@ impl Channel { let sub = self.sub; let order = self.order; let st = &mut self.dec; - let nf = &mut self.nf; + let mut ring = NfRing::new(&self.nf); let off = if p.signal_type == 0 { offset + INACTIVE_OFFSET } else { offset }; let gamma = nf_gamma(off); let mut nf_c = [0f64; MAX_LPC_ORDER]; @@ -355,7 +397,7 @@ impl Channel { } let lpc_q14 = lpc_pred.saturating_mul(16); // Target excitation (Q14) for this sample. - let fb: f64 = (0..order).map(|j| nf_c[j] * nf[j]).sum(); + let fb: f64 = nf_c.iter().zip(ring.window()).take(order).map(|(c, e)| c * e).sum(); let tgt = (x[n] as f64 + fb).clamp(-32768.0, 32767.0) * inv_g - lpc_q14 as f64 - ltp_q14 as f64; let sgn = if seed_v < 0 { -1.0 } else { 1.0 }; let u = sgn * tgt; @@ -394,11 +436,11 @@ impl Channel { let v = res.saturating_add(lpc_q14); s_lpc[MAX_LPC_ORDER + i] = v; xq[n] = sat16(rshift_round(smulww(v, gain_q10), 8)) as i16; - nf.copy_within(0..MAX_LPC_ORDER - 1, 1); - nf[0] = xq[n] as f64 - x[n] as f64; + ring.push(xq[n] as f64 - x[n] as f64); } s_lpc.copy_within(sub..sub + MAX_LPC_ORDER, 0); } + self.nf = ring.to_array(); st.s_lpc_q14.copy_from_slice(&s_lpc[..MAX_LPC_ORDER]); st.out_buf[..l].copy_from_slice(&xq); (Coded { gain_ix, gains_q16, seed, pulses }, xq) @@ -485,6 +527,14 @@ struct Prep { pred_q13: [i32; 2], } +/// One rate-loop trial: the frame coded at gain offset `offset`. +struct Trial { + offset: f64, + bits: i32, + state: SilkEncoder, + coder: RangeEncoder, +} + /// SILK encoder for one mono or stereo (mid/side) stream. #[derive(Clone)] pub struct SilkEncoder { @@ -635,11 +685,20 @@ impl SilkEncoder { pub fn encode(&mut self, input: &[Vec], target_bits: f64, max_bits: i32, enc: &mut RangeEncoder) { let prep = self.prepare(input); let start_bits = enc.tell(); - let trial = |s: &Self, o: f64| -> i32 { - let mut t = s.clone(); - let mut e = enc.clone(); - t.code(&prep, o, &mut e); - e.tell() - start_bits + // Every trial codes the frame into a clone of the encoder and the range coder. The result + // is kept per offset: the hard-limit check below hits the memo for the chosen offset, and + // the final step adopts that trial instead of coding the frame once more. + let mut trials: Vec = Vec::new(); + let mut trial = |s: &Self, o: f64| -> i32 { + if let Some(t) = trials.iter().find(|t| t.offset == o) { + return t.bits; + } + let mut state = s.clone(); + let mut coder = enc.clone(); + state.code(&prep, o, &mut coder); + let bits = coder.tell() - start_bits; + trials.push(Trial { offset: o, bits, state, coder }); + bits }; // Bisection on the gain offset: the smallest offset (finest quantisation) within budget. let target = target_bits.min(max_bits as f64); @@ -701,8 +760,20 @@ impl SilkEncoder { while trial(self, o) > max_bits && o < 12.0 { o += 1.0; } + match trials.iter().position(|t| t.offset == o) { + Some(i) => { + let t = trials.swap_remove(i); + *self = t.state; + *enc = t.coder; + } + None => self.code(&prep, o, enc), + } + self.end_frame(&prep, o); + } + + /// Advances the input history and the frame counter after frame `prep` was coded at `o`. + fn end_frame(&mut self, prep: &Prep, o: f64) { self.last_offset = o; - self.code(&prep, o, enc); for ch in 0..self.channels { self.ch[ch].push_history(&prep.x[ch]); } @@ -712,3 +783,80 @@ impl SilkEncoder { self.frame += 1; } } + +#[cfg(test)] +mod tests { + use super::*; + + /// Deterministic speech-like test input: two harmonics with a moving pitch, a syllable + /// envelope, noise and short silences, at the internal rate and 16-bit scale. + fn signal(len: usize, fs_khz: usize, seed: u32) -> Vec { + let mut s = seed; + let fs = (fs_khz * 1000) as f64; + (0..len) + .map(|i| { + s = s.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + let noise = (s >> 8) as f64 / (1u32 << 24) as f64 - 0.5; + let t = i as f64 / fs; + let f0 = 140.0 + 40.0 * (t * 1.3).sin(); + let env = ((t * 4.0).sin().max(0.0)).powf(0.7); + let voiced = (std::f64::consts::TAU * f0 * t).sin() + 0.5 * (std::f64::consts::TAU * 2.0 * f0 * t).sin(); + (8000.0 * env * voiced + 600.0 * noise) as f32 + }) + .collect() + } + + #[test] + fn nf_ring_matches_a_shifted_history() { + let mut hist = [0f64; MAX_LPC_ORDER]; + for (i, h) in hist.iter_mut().enumerate() { + *h = i as f64 * 0.5 - 3.0; + } + let mut ring = NfRing::new(&hist); + assert_eq!(ring.window(), &hist[..]); + for n in 0..100 { + let v = (n as f64 * 0.37).sin() * 100.0; + hist.copy_within(0..MAX_LPC_ORDER - 1, 1); + hist[0] = v; + ring.push(v); + assert_eq!(ring.window(), &hist[..], "after push {n}"); + } + assert_eq!(ring.to_array(), hist); + } + + /// The rate loop adopts the memoised trial for the chosen offset. A second encoder that codes + /// every frame afresh at that offset (what the loop did before the memo) must produce the + /// same bytes, frame after frame, so the adopted encoder and range-coder states are exact. + #[test] + fn adopting_the_memoised_trial_equals_coding_the_frame_again() { + for (fs_khz, channels, target_bits, max_bits) in + [(8, 1, 200.0, 1000), (12, 1, 320.0, 1000), (16, 1, 480.0, 1000), (16, 2, 640.0, 1000), (16, 1, 900.0, 400), (8, 2, 120.0, 160)] + { + let mut memo = SilkEncoder::new(fs_khz, channels); + let mut fresh = memo.clone(); + let l = memo.frame_len(); + let frames = 40; + let left = signal(l * frames, fs_khz, 7); + let right: Vec = signal(l * frames + 37, fs_khz, 11)[37..].iter().map(|v| v * 0.6).collect(); + for f in 0..frames { + let mut input = vec![left[f * l..(f + 1) * l].to_vec()]; + if channels == 2 { + input.push(right[f * l..(f + 1) * l].to_vec()); + } + let mut enc = RangeEncoder::new(); + memo.encode(&input, target_bits, max_bits, &mut enc); + let o = memo.last_offset; + let prep = fresh.prepare(&input); + let mut enc_fresh = RangeEncoder::new(); + fresh.code(&prep, o, &mut enc_fresh); + fresh.end_frame(&prep, o); + assert_eq!(enc.tell(), enc_fresh.tell(), "{fs_khz} kHz {channels} ch frame {f}"); + assert_eq!(enc.done(1275), enc_fresh.done(1275), "{fs_khz} kHz {channels} ch frame {f}"); + for c in 0..channels { + assert_eq!(memo.ch[c].nf, fresh.ch[c].nf, "noise feedback, channel {c}, frame {f}"); + assert_eq!(memo.ch[c].dec.s_lpc_q14, fresh.ch[c].dec.s_lpc_q14, "LPC state, channel {c}, frame {f}"); + } + } + } + } +} From 4d594a1130c78c88e9f9baf28891fc7d76162611 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 15 Nov 2026 09:30:30 -0500 Subject: [PATCH 025/150] psd: share editable TySh binary envelope from PhotoCraft Preserve transform safety work and compatibility recovery; adapt to shared array geometry and add regression tests. Co-authored-by: David Mann Co-authored-by: Sean Lavery Co-authored-by: Dylan M. Taylor --- crates/psd/src/text.rs | 3 + crates/psd/src/text/envelope.rs | 142 ++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 crates/psd/src/text/envelope.rs diff --git a/crates/psd/src/text.rs b/crates/psd/src/text.rs index 6ba299bca..3a9e14952 100644 --- a/crates/psd/src/text.rs +++ b/crates/psd/src/text.rs @@ -150,3 +150,6 @@ mod tests { assert_eq!(t.style.size, Some(12.0)); } } + +/// Editable type-tool envelope with PhotoCraft-compatible recovery and geometry limits. +pub mod envelope; diff --git a/crates/psd/src/text/envelope.rs b/crates/psd/src/text/envelope.rs new file mode 100644 index 000000000..68664f7d7 --- /dev/null +++ b/crates/psd/src/text/envelope.rs @@ -0,0 +1,142 @@ +//! Editable PSD Type Tool Object Setting (`TySh`) envelope, extracted from PhotoCraft. +//! App-specific text styles, layout, units and editing policy stay with the consumer. +//! Parsing intentionally retains PhotoCraft's optional-warp and partial-bounds recovery. +//! Existing read-only [`super::TypeLayer`] behavior is unchanged. + +use crate::descriptor::{Descriptor, Id, Value as D, VersionedDescriptor}; + +/// Parsed `TySh` contents. +#[derive(Clone, Debug, PartialEq)] +pub struct TySh { + pub transform: [f64; 6], + pub text: Descriptor, + pub warp: Option, + /// The four trailing big-endian f32 values (left, top, right, bottom) after the warp + /// descriptor. Photoshop writes zeros for unwarped type and the text-space bounds (the + /// `bounds` descriptor's rectangle) for warped type. + pub bounds: [f32; 4], +} + +/// Whether a type transform can reach rendering. Pixel coordinates use i32 in the originating PhotoCraft consumer, so non-finite and unrepresentable affine terms are rejected (#4758). +pub fn transform_is_safe(t: &[f64; 6]) -> bool { + t.iter().all(|v| v.is_finite() && v.abs() <= f64::from(i32::MAX)) +} + +/// Parses a `TySh` block. +pub fn parse_tysh(data: &[u8]) -> Option { + let f = |at: usize| data.get(at..at + 8).map(|b| f64::from_be_bytes(b.try_into().unwrap_or([0; 8]))); + let mut m = [0.0; 6]; + for (i, v) in m.iter_mut().enumerate() { + *v = f(2 + i * 8)?; + } + if !transform_is_safe(&m) { + return None; + } + let (text, used) = VersionedDescriptor::parse_prefix(data.get(52..)?).ok()?; + let rest = data.get(52 + used..).unwrap_or(&[]); + let (warp, wused) = match rest.get(2..).and_then(|r| VersionedDescriptor::parse_prefix(r).ok()) { + Some((w, n)) => (Some(w.descriptor), n + 2), + None => (None, 0), + }; + let tail = rest.get(wused..).unwrap_or(&[]); + let mut bounds = [0f32; 4]; + for (i, b) in bounds.iter_mut().enumerate() { + if let Some(x) = tail.get(i * 4..i * 4 + 4) { + *b = f32::from_be_bytes(x.try_into().unwrap_or([0; 4])); + } + } + Some(TySh { transform: m, text: text.descriptor, warp, bounds }) +} + +/// `tysh` moved by (`dx`, `dy`): only the transform's translation changes. The text, its styles, +/// the warp and the trailing bounds (which are in text space) stay byte for byte, so a moved +/// Photoshop type layer keeps everything Photoshop wrote. `None` when the block is too short. +pub fn translate_tysh(tysh: &[u8], dx: f64, dy: f64) -> Option> { + let mut out = tysh.to_vec(); + // Version (2 bytes), then xx, xy, yx, yy, tx, ty as big-endian f64: tx at 34, ty at 42. + for (at, d) in [(34, dx), (42, dy)] { + let b = out.get_mut(at..at + 8)?; + let v = f64::from_be_bytes(b.try_into().ok()?) + d; + b.copy_from_slice(&v.to_be_bytes()); + } + Some(out) +} + +/// Serializes a `TySh` block. +pub fn write_tysh(t: &TySh) -> Vec { + let mut v = 1u16.to_be_bytes().to_vec(); + for x in t.transform { + v.extend_from_slice(&x.to_be_bytes()); + } + v.extend_from_slice(&50u16.to_be_bytes()); + v.extend(VersionedDescriptor::new(t.text.clone()).to_bytes()); + v.extend_from_slice(&1u16.to_be_bytes()); + v.extend(VersionedDescriptor::new(t.warp.clone().unwrap_or_else(default_warp)).to_bytes()); + for b in t.bounds { + v.extend_from_slice(&b.to_be_bytes()); + } + v +} + +fn default_warp() -> Descriptor { + Descriptor::new("warp") + .with("warpStyle", enumv("warpStyle", "warpNone")) + .with("warpValue", D::Double(0.0)) + .with("warpPerspective", D::Double(0.0)) + .with("warpPerspectiveOther", D::Double(0.0)) + .with("warpRotate", enumv("Ornt", "Hrzn")) +} + +fn enumv(t: &str, v: &str) -> D { + D::Enumerated { type_id: Id::new(t), value: Id::new(v) } +} + +#[cfg(test)] +mod tests { + use super::*; + fn sample() -> TySh { + TySh { + transform: [1.0, 0.0, 0.0, 1.0, 25.0, 30.0], + text: Descriptor::new("TxLr").with("EngineData", D::RawData(b"preserved".to_vec())), + warp: Some(default_warp()), + bounds: [-1.0, 2.0, 30.0, 40.0], + } + } + #[test] + fn roundtrip_preserves_descriptors_transform_and_bounds() { + let t = sample(); + let b = write_tysh(&t); + assert_eq!(parse_tysh(&b), Some(t)); + let shifted = translate_tysh(&b, 7.0, -3.0).unwrap(); + assert_eq!(&shifted[..34], &b[..34]); + assert_eq!(&shifted[50..], &b[50..]); + assert_eq!(parse_tysh(&shifted).unwrap().transform, [1.0, 0.0, 0.0, 1.0, 32.0, 27.0]); + } + #[test] + fn truncations_and_unsafe_transforms_preserve_recovery_contract() { + let mut t = sample(); + let bytes = write_tysh(&t); + let (_, n) = VersionedDescriptor::parse_prefix(&bytes[52..]).unwrap(); + for cut in 0..52 + n { + assert!(parse_tysh(&bytes[..cut]).is_none(), "{cut}"); + } + let short = parse_tysh(&bytes[..52 + n]).unwrap(); + assert!(short.warp.is_none()); + assert_eq!(short.bounds, [0.0; 4]); + for cut in 0..50 { + assert!(translate_tysh(&bytes[..cut], 1.0, 2.0).is_none()); + } + for bad in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, f64::from(i32::MAX) + 1.0] { + for i in 0..6 { + t = sample(); + t.transform[i] = bad; + assert!(parse_tysh(&write_tysh(&t)).is_none()); + } + } + t = sample(); + t.warp = None; + assert!(parse_tysh(&write_tysh(&t)).unwrap().warp.is_some()); + let partial = parse_tysh(&bytes[..bytes.len() - 4]).unwrap(); + assert_eq!(partial.bounds, [-1.0, 2.0, 30.0, 0.0]); + } +} From 1e204c51dff0d49e528637b16d58f95cfa2b283a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 09:33:38 -0500 Subject: [PATCH 026/150] psd: bound TySh serialization and reject unsafe geometry --- crates/psd/src/text/envelope.rs | 186 ++++++++++++++++++++++++++++++-- 1 file changed, 175 insertions(+), 11 deletions(-) diff --git a/crates/psd/src/text/envelope.rs b/crates/psd/src/text/envelope.rs index 68664f7d7..e654ed390 100644 --- a/crates/psd/src/text/envelope.rs +++ b/crates/psd/src/text/envelope.rs @@ -3,7 +3,11 @@ //! Parsing intentionally retains PhotoCraft's optional-warp and partial-bounds recovery. //! Existing read-only [`super::TypeLayer`] behavior is unchanged. -use crate::descriptor::{Descriptor, Id, Value as D, VersionedDescriptor}; +use crate::descriptor::{Class, Descriptor, Id, ReferenceItem, UnicodeString, Value as D, VersionedDescriptor}; +use crate::error::{PsdError, Result}; + +/// Maximum encoded envelope size, bounding hostile input and serialized output. +pub const MAX_BYTES: usize = 8 * 1024 * 1024; /// Parsed `TySh` contents. #[derive(Clone, Debug, PartialEq)] @@ -24,6 +28,9 @@ pub fn transform_is_safe(t: &[f64; 6]) -> bool { /// Parses a `TySh` block. pub fn parse_tysh(data: &[u8]) -> Option { + if data.len() > MAX_BYTES { + return None; + } let f = |at: usize| data.get(at..at + 8).map(|b| f64::from_be_bytes(b.try_into().unwrap_or([0; 8]))); let mut m = [0.0; 6]; for (i, v) in m.iter_mut().enumerate() { @@ -45,6 +52,9 @@ pub fn parse_tysh(data: &[u8]) -> Option { *b = f32::from_be_bytes(x.try_into().unwrap_or([0; 4])); } } + if bounds.iter().any(|x| !x.is_finite()) { + return None; + } Some(TySh { transform: m, text: text.descriptor, warp, bounds }) } @@ -52,7 +62,21 @@ pub fn parse_tysh(data: &[u8]) -> Option { /// the warp and the trailing bounds (which are in text space) stay byte for byte, so a moved /// Photoshop type layer keeps everything Photoshop wrote. `None` when the block is too short. pub fn translate_tysh(tysh: &[u8], dx: f64, dy: f64) -> Option> { - let mut out = tysh.to_vec(); + if tysh.len() > MAX_BYTES || tysh.len() < 50 { + return None; + } + let mut transform = [0.0; 6]; + for (i, x) in transform.iter_mut().enumerate() { + *x = f64::from_be_bytes(tysh.get(2 + i * 8..10 + i * 8)?.try_into().ok()?); + } + transform[4] += dx; + transform[5] += dy; + if !transform_is_safe(&transform) { + return None; + } + let mut out = Vec::new(); + out.try_reserve_exact(tysh.len()).ok()?; + out.extend_from_slice(tysh); // Version (2 bytes), then xx, xy, yx, yy, tx, ty as big-endian f64: tx at 34, ty at 42. for (at, d) in [(34, dx), (42, dy)] { let b = out.get_mut(at..at + 8)?; @@ -63,19 +87,136 @@ pub fn translate_tysh(tysh: &[u8], dx: f64, dy: f64) -> Option> { } /// Serializes a `TySh` block. -pub fn write_tysh(t: &TySh) -> Vec { - let mut v = 1u16.to_be_bytes().to_vec(); +pub fn write_tysh(t: &TySh) -> Result> { + if !transform_is_safe(&t.transform) || t.bounds.iter().any(|x| !x.is_finite()) { + return Err(PsdError::LimitExceeded("TySh geometry")); + } + let default; + let warp = match &t.warp { + Some(w) => w, + None => { + default = default_warp(); + &default + } + }; + let mut budget = Budget(78); // versions, transform and trailing bounds + budget.descriptor(&t.text, 0)?; + budget.descriptor(warp, 0)?; + let mut v = Vec::new(); + v.try_reserve_exact(budget.0).map_err(|_| PsdError::OutOfMemory("TySh output"))?; + v.extend_from_slice(&1u16.to_be_bytes()); for x in t.transform { v.extend_from_slice(&x.to_be_bytes()); } v.extend_from_slice(&50u16.to_be_bytes()); - v.extend(VersionedDescriptor::new(t.text.clone()).to_bytes()); + v.extend_from_slice(&16u32.to_be_bytes()); + t.text.write(&mut v); v.extend_from_slice(&1u16.to_be_bytes()); - v.extend(VersionedDescriptor::new(t.warp.clone().unwrap_or_else(default_warp)).to_bytes()); + v.extend_from_slice(&16u32.to_be_bytes()); + warp.write(&mut v); for b in t.bounds { v.extend_from_slice(&b.to_be_bytes()); } - v + Ok(v) +} + +// Count before cloning or serializing: bound both recursive trees and total output. +struct Budget(usize); +impl Budget { + fn add(&mut self, n: usize) -> Result<()> { + self.0 = self.0.checked_add(n).filter(|n| *n <= MAX_BYTES).ok_or(PsdError::LimitExceeded("TySh bytes"))?; + Ok(()) + } + fn unicode(&mut self, s: &UnicodeString) -> Result<()> { + self.add(4)?; + self.add(s.0.len().checked_mul(2).ok_or(PsdError::LimitExceeded("TySh string"))?) + } + fn id(&mut self, id: &Id) -> Result<()> { + self.add(4)?; + self.add(id.as_bytes().len()) + } + fn class(&mut self, c: &Class) -> Result<()> { + self.unicode(&c.name)?; + self.id(&c.class_id) + } + fn descriptor(&mut self, d: &Descriptor, depth: usize) -> Result<()> { + if depth > 32 { + return Err(PsdError::LimitExceeded("TySh depth")); + } + self.unicode(&d.name)?; + self.id(&d.class_id)?; + self.add(4)?; + for (k, v) in &d.items { + self.id(k)?; + self.value(v, depth + 1)?; + } + Ok(()) + } + fn value(&mut self, v: &D, depth: usize) -> Result<()> { + if depth > 32 { + return Err(PsdError::LimitExceeded("TySh depth")); + } + self.add(4)?; + match v { + D::Descriptor(d) | D::GlobalObject(d) => self.descriptor(d, depth), + D::ObjectArray(o) => { + self.add(4)?; + self.descriptor(&o.body, depth) + } + D::List(xs) => { + self.add(4)?; + for x in xs { + self.value(x, depth + 1)?; + } + Ok(()) + } + D::Reference(xs) => { + self.add(4)?; + for x in xs { + self.add(4)?; + match x { + ReferenceItem::Property { class, key } => { + self.class(class)?; + self.id(key)?; + } + ReferenceItem::Class(c) => self.class(c)?, + ReferenceItem::Enumerated { class, type_id, value } => { + self.class(class)?; + self.id(type_id)?; + self.id(value)?; + } + ReferenceItem::Offset { class, .. } | ReferenceItem::Identifier { class, .. } | ReferenceItem::Index { class, .. } => { + self.class(class)?; + self.add(4)?; + } + ReferenceItem::Name { class, name } => { + self.class(class)?; + self.unicode(name)?; + } + } + } + Ok(()) + } + D::Text(x) => self.unicode(x), + D::Class(c) | D::GlobalClass(c) => self.class(c), + D::Enumerated { type_id, value } => { + self.id(type_id)?; + self.id(value) + } + D::UnitFloats { values, .. } => { + self.add(8)?; + self.add(values.len().checked_mul(8).ok_or(PsdError::LimitExceeded("TySh values"))?) + } + D::RawData(x) | D::Alias(x) | D::Path(x) => { + self.add(4)?; + self.add(x.len()) + } + D::Double(_) | D::LargeInteger(_) => self.add(8), + D::UnitFloat { .. } => self.add(12), + D::Integer(_) => self.add(4), + D::Boolean(_) => self.add(1), + } + } } fn default_warp() -> Descriptor { @@ -105,7 +246,7 @@ mod tests { #[test] fn roundtrip_preserves_descriptors_transform_and_bounds() { let t = sample(); - let b = write_tysh(&t); + let b = write_tysh(&t).unwrap(); assert_eq!(parse_tysh(&b), Some(t)); let shifted = translate_tysh(&b, 7.0, -3.0).unwrap(); assert_eq!(&shifted[..34], &b[..34]); @@ -115,7 +256,7 @@ mod tests { #[test] fn truncations_and_unsafe_transforms_preserve_recovery_contract() { let mut t = sample(); - let bytes = write_tysh(&t); + let bytes = write_tysh(&t).unwrap(); let (_, n) = VersionedDescriptor::parse_prefix(&bytes[52..]).unwrap(); for cut in 0..52 + n { assert!(parse_tysh(&bytes[..cut]).is_none(), "{cut}"); @@ -130,13 +271,36 @@ mod tests { for i in 0..6 { t = sample(); t.transform[i] = bad; - assert!(parse_tysh(&write_tysh(&t)).is_none()); + assert!(write_tysh(&t).is_err()); } } t = sample(); t.warp = None; - assert!(parse_tysh(&write_tysh(&t)).unwrap().warp.is_some()); + assert!(parse_tysh(&write_tysh(&t).unwrap()).unwrap().warp.is_some()); let partial = parse_tysh(&bytes[..bytes.len() - 4]).unwrap(); assert_eq!(partial.bounds, [-1.0, 2.0, 30.0, 0.0]); } + #[test] + fn hostile_output_and_translations_are_refused_before_copying() { + let bytes = write_tysh(&sample()).unwrap(); + for bad in [f64::NAN, f64::INFINITY, f64::MAX] { + assert!(translate_tysh(&bytes, bad, 0.0).is_none()); + } + let mut t = sample(); + t.bounds[0] = f32::NAN; + assert!(write_tysh(&t).is_err()); + t = sample(); + t.text = Descriptor::new("TxLr").with("data", D::RawData(vec![0; MAX_BYTES])); + assert!(write_tysh(&t).is_err()); + let oversized = vec![0; MAX_BYTES + 1]; + assert!(translate_tysh(&oversized, 0.0, 0.0).is_none()); + assert!(parse_tysh(&oversized).is_none()); + let mut nested = D::Integer(1); + for _ in 0..34 { + nested = D::List(vec![nested]); + } + t = sample(); + t.text = Descriptor::new("TxLr").with("nest", nested); + assert!(write_tysh(&t).is_err()); + } } From f483f9d6dd0ec9dcaf7697323cfab0b7f6fdc57b Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 15 Nov 2026 09:39:55 -0500 Subject: [PATCH 027/150] cms: preserve original PDFCraft ICC v2 sRGB generator Extracted unchanged from storytold/pdfcraft e098c5c85, crates/preflight/src/icc.rs; original implementation 4d60af8b1, rename d3db99818. --- crates/cms/src/builtin_v2.rs | 128 +++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 crates/cms/src/builtin_v2.rs diff --git a/crates/cms/src/builtin_v2.rs b/crates/cms/src/builtin_v2.rs new file mode 100644 index 000000000..935dd105c --- /dev/null +++ b/crates/cms/src/builtin_v2.rs @@ -0,0 +1,128 @@ +//! A small sRGB ICC profile (version 2, display class, matrix/TRC), written from the published +//! sRGB primaries (IEC 61966-2-1) adapted to D50, with the sRGB tone curve sampled at 1024 +//! points. Used as the PDF/A output intent's destination profile. + +fn s15f16(v: f64) -> [u8; 4] { + ((v * 65536.0).round() as i32).to_be_bytes() +} + +fn xyz(x: f64, y: f64, z: f64) -> Vec { + let mut t = b"XYZ \0\0\0\0".to_vec(); + t.extend(s15f16(x)); + t.extend(s15f16(y)); + t.extend(s15f16(z)); + t +} + +fn desc(text: &str) -> Vec { + let mut t = b"desc\0\0\0\0".to_vec(); + let ascii: Vec = text.bytes().chain(std::iter::once(0)).collect(); + t.extend((ascii.len() as u32).to_be_bytes()); + t.extend(&ascii); + // No Unicode or ScriptCode descriptions. + t.extend([0u8; 4 + 4]); + t.extend([0u8; 2 + 1 + 67]); + t +} + +fn text(text: &str) -> Vec { + let mut t = b"text\0\0\0\0".to_vec(); + t.extend(text.bytes()); + t.push(0); + t +} + +fn curve() -> Vec { + const N: usize = 1024; + let mut t = b"curv\0\0\0\0".to_vec(); + t.extend((N as u32).to_be_bytes()); + for i in 0..N { + let v = i as f64 / (N - 1) as f64; + let lin = if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) }; + t.extend(((lin * 65535.0).round() as u16).to_be_bytes()); + } + t +} + +/// The profile's bytes. +pub fn srgb() -> Vec { + // sRGB primaries, Bradford-adapted to the D50 profile connection space. + let tags: Vec<(&[u8; 4], Vec)> = vec![ + (b"desc", desc("sRGB (PdfCraft)")), + (b"cprt", text("No copyright, use freely")), + (b"wtpt", xyz(0.9642, 1.0, 0.8249)), + (b"rXYZ", xyz(0.4361, 0.2225, 0.0139)), + (b"gXYZ", xyz(0.3851, 0.7169, 0.0971)), + (b"bXYZ", xyz(0.1431, 0.0606, 0.7141)), + (b"rTRC", curve()), + ]; + // gTRC and bTRC share rTRC's data. + let count = tags.len() + 2; + let table_len = 4 + count * 12; + let mut offset = 128 + table_len; + let mut table = (count as u32).to_be_bytes().to_vec(); + let mut data = Vec::new(); + let mut trc = (0, 0); + for (sig, body) in &tags { + let len = body.len(); + table.extend(*sig); + table.extend((offset as u32).to_be_bytes()); + table.extend((len as u32).to_be_bytes()); + if *sig == b"rTRC" { + trc = (offset, len); + } + data.extend(body); + // Tags start on 4-byte boundaries. + let pad = (4 - len % 4) % 4; + data.extend(std::iter::repeat_n(0u8, pad)); + offset += len + pad; + } + for sig in [b"gTRC", b"bTRC"] { + table.extend(sig); + table.extend((trc.0 as u32).to_be_bytes()); + table.extend((trc.1 as u32).to_be_bytes()); + } + let size = 128 + table.len() + data.len(); + let mut h = Vec::with_capacity(size); + h.extend((size as u32).to_be_bytes()); + h.extend(b"\0\0\0\0"); // preferred CMM + h.extend([2, 0x10, 0, 0]); // version 2.1 + h.extend(b"mntr"); + h.extend(b"RGB "); + h.extend(b"XYZ "); + h.extend([0u8; 12]); // date + h.extend(b"acsp"); + h.extend(b"\0\0\0\0"); // platform + h.extend([0u8; 4]); // flags + h.extend([0u8; 4]); // manufacturer + h.extend([0u8; 4]); // model + h.extend([0u8; 8]); // attributes + h.extend([0, 0, 0, 0]); // perceptual intent + h.extend(s15f16(0.9642)); + h.extend(s15f16(1.0)); + h.extend(s15f16(0.8249)); + h.extend([0u8; 4]); // creator + h.resize(128, 0); + h.extend(table); + h.extend(data); + h +} + +#[cfg(test)] +mod tests { + #[test] + fn profile_is_well_formed() { + let p = super::srgb(); + assert_eq!(u32::from_be_bytes(p[0..4].try_into().unwrap()) as usize, p.len()); + assert_eq!(&p[36..40], b"acsp"); + assert_eq!(&p[12..20], b"mntrRGB "); + let n = u32::from_be_bytes(p[128..132].try_into().unwrap()) as usize; + assert_eq!(n, 9); + for i in 0..n { + let e = 132 + i * 12; + let off = u32::from_be_bytes(p[e + 4..e + 8].try_into().unwrap()) as usize; + let len = u32::from_be_bytes(p[e + 8..e + 12].try_into().unwrap()) as usize; + assert!(off.is_multiple_of(4) && off + len <= p.len(), "tag {i}"); + } + } +} From 7bc5f2a4c8214b97394a5dcf60d01e9f5f5298c3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 09:40:37 -0500 Subject: [PATCH 028/150] cms: expose fixed v2 sRGB profile and verify shared parsing --- crates/cms/src/builtin.rs | 12 ++++++++++++ crates/cms/src/builtin_v2.rs | 17 ++++++++++++++++- crates/cms/src/lib.rs | 1 + 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/crates/cms/src/builtin.rs b/crates/cms/src/builtin.rs index ca7034286..e62faa907 100644 --- a/crates/cms/src/builtin.rs +++ b/crates/cms/src/builtin.rs @@ -450,3 +450,15 @@ mod tests { assert!(cicp_rgb(u16::MAX, u16::MAX, true).is_none()); } } + +/// Generates the compact ICC v2.1 sRGB matrix/TRC profile used by PDFCraft. +/// +/// Unlike [`Builtin::Srgb`]'s v4 profile, this uses a 1024-sample tone curve and +/// v2 `desc`/`text` tags for consumers that require a v2 output intent. The legacy +/// description `sRGB (PdfCraft)` is retained to preserve existing document bytes. +/// The generated profile contains no external assets; callers choose where it is +/// appropriate (including their own PDF/A conformance policy). +/// All sizes are fixed; no input-controlled allocation or recursion is involved. +pub fn srgb_v2() -> Vec { + crate::builtin_v2::srgb() +} diff --git a/crates/cms/src/builtin_v2.rs b/crates/cms/src/builtin_v2.rs index 935dd105c..4a9e2e25b 100644 --- a/crates/cms/src/builtin_v2.rs +++ b/crates/cms/src/builtin_v2.rs @@ -44,7 +44,7 @@ fn curve() -> Vec { t } -/// The profile's bytes. +/// Fixed-size profile bytes; all tag sizes and loop counts are constants. pub fn srgb() -> Vec { // sRGB primaries, Bradford-adapted to the D50 profile connection space. let tags: Vec<(&[u8; 4], Vec)> = vec![ @@ -110,6 +110,21 @@ pub fn srgb() -> Vec { #[cfg(test)] mod tests { + #[test] + fn parses_as_v2_with_shared_rgb_curves() { + let bytes = super::srgb(); + let p = crate::Profile::parse(&bytes).unwrap(); + assert_eq!(p.version, (2, 0x10)); + assert_eq!(p.color_space, crate::ColorSpace::Rgb); + assert_eq!(p.pcs, crate::Pcs::Xyz); + assert_eq!(p.description, "sRGB (PdfCraft)"); + let curves = p.trc.unwrap(); + assert_eq!(curves[0], curves[1]); + assert_eq!(curves[1], curves[2]); + assert!(matches!(&curves[0], crate::Curve::Table(v) if v.len() == 1024 && v[0] == 0.0 && v[1023] == 1.0)); + assert_eq!(bytes, super::srgb()); + } + #[test] fn profile_is_well_formed() { let p = super::srgb(); diff --git a/crates/cms/src/lib.rs b/crates/cms/src/lib.rs index 8d4f99f49..5e8a1e268 100644 --- a/crates/cms/src/lib.rs +++ b/crates/cms/src/lib.rs @@ -20,6 +20,7 @@ #![forbid(unsafe_code)] pub mod builtin; +mod builtin_v2; pub mod clut; pub mod curve; pub mod gamut; From cab96afd08c3b68073340d2162657ccc989bd751 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 09:43:27 -0500 Subject: [PATCH 029/150] cms: keep public v2 helper before test module --- crates/cms/src/builtin.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/cms/src/builtin.rs b/crates/cms/src/builtin.rs index e62faa907..5a8b03228 100644 --- a/crates/cms/src/builtin.rs +++ b/crates/cms/src/builtin.rs @@ -424,6 +424,18 @@ fn shipped_cmyk(bytes: &[u8], b: Builtin) -> Profile { }) } +/// Generates the compact ICC v2.1 sRGB matrix/TRC profile used by PDFCraft. +/// +/// Unlike [`Builtin::Srgb`]'s v4 profile, this uses a 1024-sample tone curve and +/// v2 `desc`/`text` tags for consumers that require a v2 output intent. The legacy +/// description `sRGB (PdfCraft)` is retained to preserve existing document bytes. +/// The generated profile contains no external assets; callers choose where it is +/// appropriate (including their own PDF/A conformance policy). +/// All sizes are fixed; no input-controlled allocation or recursion is involved. +pub fn srgb_v2() -> Vec { + crate::builtin_v2::srgb() +} + #[cfg(test)] mod tests { use super::*; @@ -450,15 +462,3 @@ mod tests { assert!(cicp_rgb(u16::MAX, u16::MAX, true).is_none()); } } - -/// Generates the compact ICC v2.1 sRGB matrix/TRC profile used by PDFCraft. -/// -/// Unlike [`Builtin::Srgb`]'s v4 profile, this uses a 1024-sample tone curve and -/// v2 `desc`/`text` tags for consumers that require a v2 output intent. The legacy -/// description `sRGB (PdfCraft)` is retained to preserve existing document bytes. -/// The generated profile contains no external assets; callers choose where it is -/// appropriate (including their own PDF/A conformance policy). -/// All sizes are fixed; no input-controlled allocation or recursion is involved. -pub fn srgb_v2() -> Vec { - crate::builtin_v2::srgb() -} From 2bb5d0344f03a73833576b57847513ea529d3d2c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 10:10:41 -0500 Subject: [PATCH 030/150] docs(psd): document editable TySh envelope fields --- crates/psd/src/text/envelope.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/psd/src/text/envelope.rs b/crates/psd/src/text/envelope.rs index e654ed390..73345fa83 100644 --- a/crates/psd/src/text/envelope.rs +++ b/crates/psd/src/text/envelope.rs @@ -12,8 +12,11 @@ pub const MAX_BYTES: usize = 8 * 1024 * 1024; /// Parsed `TySh` contents. #[derive(Clone, Debug, PartialEq)] pub struct TySh { + /// Affine transform in PSD order: xx, xy, yx, yy, tx, ty. pub transform: [f64; 6], + /// Text descriptor, including text content and styling data. pub text: Descriptor, + /// Optional warp descriptor retained from the Type Tool block. pub warp: Option, /// The four trailing big-endian f32 values (left, top, right, bottom) after the warp /// descriptor. Photoshop writes zeros for unwarped type and the text-space bounds (the From 373d36ea3f66be15b826c861681147e32314b875 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:18:47 +0000 Subject: [PATCH 031/150] Expose bounded stored image dimensions --- crates/image-codecs/src/allocation.rs | 49 +++++++++++++++++++++++++++ crates/image-codecs/src/lib.rs | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/crates/image-codecs/src/allocation.rs b/crates/image-codecs/src/allocation.rs index fe98122e7..40d55dfc8 100644 --- a/crates/image-codecs/src/allocation.rs +++ b/crates/image-codecs/src/allocation.rs @@ -247,3 +247,52 @@ fn read_le24(bytes: &[u8], at: usize) -> Option { let b = bytes.get(at..at.checked_add(3)?)?; Some(u32::from_le_bytes([b[0], b[1], b[2], 0])) } + +/// Probe the stored dimensions of the first PNG, JPEG, BMP or TIFF image, without decoding +/// pixels or applying orientation. TIFF uses the first directory even if it is a thumbnail. +/// The caller's limits bound both the header decoder and the declared output dimensions. +pub fn probe_stored_size(bytes: &[u8], limits: &crate::Limits) -> Result<(u32, u32), CodecError> { + use std::io::Cursor; + let format = crate::detect(bytes).ok_or(CodecError::UnknownFormat)?; + if !matches!(format, Format::Png | Format::Jpeg | Format::Bmp | Format::Tiff) { + return Err(CodecError::unsupported(format, "stored-size probe is not available for this format")); + } + let mut reader = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().map_err(|e| CodecError::malformed(format, e))?; + let mut image_limits = image::Limits::default(); + image_limits.max_image_width = Some(limits.max_width); + image_limits.max_image_height = Some(limits.max_height); + image_limits.max_alloc = Some(limits.max_alloc); + reader.limits(image_limits); + let (w, h) = reader.into_dimensions().map_err(|e| CodecError::malformed(format, e))?; + limits.check_size(w, h)?; + Ok((w, h)) +} + +#[cfg(test)] +mod stored_size_tests { + #![allow(clippy::unwrap_used)] + use super::*; + use std::io::Cursor; + + #[test] + fn stored_probe_uses_caller_limits_without_decoding() { + let mut bmp = Cursor::new(Vec::new()); + image::write_buffer_with_format(&mut bmp, &vec![0; 300_000 * 3], 1, 300_000, image::ExtendedColorType::Rgb8, image::ImageFormat::Bmp) + .unwrap(); + let bytes = bmp.into_inner(); + let limits = crate::Limits::new(u32::MAX, u32::MAX, 128_000_000, 2_048_000_000); + assert_eq!(probe_stored_size(&bytes, &limits).unwrap(), (1, 300_000)); + assert!(probe_stored_size(&bytes, &crate::Limits::default()).is_err()); + assert!(probe_stored_size(&bytes[..20], &limits).is_err()); + } + + #[test] + fn common_stored_headers_are_probed() { + let limits = crate::Limits::new(100, 100, 10_000, 1 << 20); + for format in [image::ImageFormat::Png, image::ImageFormat::Jpeg, image::ImageFormat::Bmp, image::ImageFormat::Tiff] { + let mut out = Cursor::new(Vec::new()); + image::write_buffer_with_format(&mut out, &[10; 18], 3, 2, image::ExtendedColorType::Rgb8, format).unwrap(); + assert_eq!(probe_stored_size(&out.into_inner(), &limits).unwrap(), (3, 2), "{format:?}"); + } + } +} diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 6516b9ff2..5cd1b9c60 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -31,7 +31,7 @@ pub mod orientation; pub mod resolution; pub mod source_settings; pub mod web; -pub use crate::allocation::probe_allocation_size; +pub use crate::allocation::{probe_allocation_size, probe_stored_size}; pub use crate::cicp::Cicp; pub use crate::cicp::ContentLightLevel; pub use crate::codecs::avif::{HDR_REFERENCE_WHITE_NITS, HdrTransfer, encode_avif_hdr, encode_avif_with_speed}; From ae5020c1ed922cf7976a3a5545f05b07f466be12 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:23:57 +0000 Subject: [PATCH 032/150] Use a BMP size supported by the baseline decoder --- crates/image-codecs/src/allocation.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/image-codecs/src/allocation.rs b/crates/image-codecs/src/allocation.rs index 40d55dfc8..a31684a33 100644 --- a/crates/image-codecs/src/allocation.rs +++ b/crates/image-codecs/src/allocation.rs @@ -277,12 +277,11 @@ mod stored_size_tests { #[test] fn stored_probe_uses_caller_limits_without_decoding() { let mut bmp = Cursor::new(Vec::new()); - image::write_buffer_with_format(&mut bmp, &vec![0; 300_000 * 3], 1, 300_000, image::ExtendedColorType::Rgb8, image::ImageFormat::Bmp) - .unwrap(); + image::write_buffer_with_format(&mut bmp, &vec![0; 60_000 * 3], 1, 60_000, image::ExtendedColorType::Rgb8, image::ImageFormat::Bmp).unwrap(); let bytes = bmp.into_inner(); let limits = crate::Limits::new(u32::MAX, u32::MAX, 128_000_000, 2_048_000_000); - assert_eq!(probe_stored_size(&bytes, &limits).unwrap(), (1, 300_000)); - assert!(probe_stored_size(&bytes, &crate::Limits::default()).is_err()); + assert_eq!(probe_stored_size(&bytes, &limits).unwrap(), (1, 60_000)); + assert!(probe_stored_size(&bytes, &crate::Limits::new(u32::MAX, 50_000, u64::MAX, 2_048_000_000)).is_err()); assert!(probe_stored_size(&bytes[..20], &limits).is_err()); } From 2c9903ca1d892b5446acf3598d8b142f3e64842f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:29:27 +0000 Subject: [PATCH 033/150] Support configurable legacy MCP sessions and envelopes --- crates/jsonrpc/src/lib.rs | 58 ++++++++++++++++++++++++++++++ crates/mcp/src/lib.rs | 74 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 129 insertions(+), 3 deletions(-) diff --git a/crates/jsonrpc/src/lib.rs b/crates/jsonrpc/src/lib.rs index 9f8128d1d..cfb46ef79 100644 --- a/crates/jsonrpc/src/lib.rs +++ b/crates/jsonrpc/src/lib.rs @@ -195,3 +195,61 @@ where let responses: Vec<_> = batch.iter().filter_map(|v| one(v, limits, &mut on_request, &mut on_notification)).collect(); Ok((!responses.is_empty()).then_some(Value::Array(responses))) } +/// Process a permissive legacy JSON-RPC envelope. This mode accepts arbitrary IDs and +/// parameter shapes, ignores response envelopes, and retains legacy diagnostic messages. +/// The caller supplies dispatch and notification callbacks, and should bound transport input. +pub fn process_legacy(value: &Value, mut on_request: F, mut on_notification: N) -> Option +where + F: FnMut(Request<'_>) -> Result, + N: FnMut(Request<'_>), +{ + fn one(value: &Value, on_request: &mut F, on_notification: &mut N) -> Option + where + F: FnMut(Request<'_>) -> Result, + N: FnMut(Request<'_>), + { + let Some(object) = value.as_object() else { return Some(error_response(Value::Null, RpcError::new(-32600, "message must be an object"))) }; + let id = object.get("id"); + let Some(method) = object.get("method").and_then(Value::as_str) else { + if object.contains_key("result") || object.contains_key("error") { + return None; + } + return Some(error_response(id.cloned().unwrap_or(Value::Null), RpcError::new(-32600, "missing `method`"))); + }; + let request = Request { id, method, params: object.get("params") }; + let Some(id) = id else { + on_notification(request); + return None; + }; + Some(match on_request(request) { + Ok(result) => json!({"jsonrpc": "2.0", "id": id, "result": result}), + Err(error) => error_response(id.clone(), error), + }) + } + if let Some(batch) = value.as_array() { + if batch.is_empty() { + return Some(error_response(Value::Null, RpcError::new(-32600, "empty batch"))); + } + let replies: Vec<_> = batch.iter().filter_map(|v| one(v, &mut on_request, &mut on_notification)).collect(); + (!replies.is_empty()).then_some(Value::Array(replies)) + } else { + one(value, &mut on_request, &mut on_notification) + } +} + +#[cfg(test)] +mod legacy_tests { + use super::*; + #[test] + fn preserves_legacy_batch_routing_and_errors() { + let input = json!([{"id": [1], "method":"echo", "params":5}, {"method":"notice"}, {"result":1}, {}, 7]); + let seen = std::cell::Cell::new(false); + let result = process_legacy(&input, |r| Ok(r.params.cloned().unwrap_or(Value::Null)), |_| seen.set(true)).unwrap(); + assert!(seen.get()); + assert_eq!( + result, + json!([{"jsonrpc":"2.0","id":[1],"result":5}, {"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"missing `method`"}}, {"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"message must be an object"}}]) + ); + assert_eq!(process_legacy(&json!([]), |_| Ok(Value::Null), |_| {}).unwrap()["error"]["message"], "empty batch"); + } +} diff --git a/crates/mcp/src/lib.rs b/crates/mcp/src/lib.rs index 96cfdc7ae..6ca888fc0 100644 --- a/crates/mcp/src/lib.rs +++ b/crates/mcp/src/lib.rs @@ -34,11 +34,27 @@ pub struct Session { policy: Policy, state: State, capabilities: Value, + versions: Vec, + permissive_initialize: bool, + server_info: Option, + instructions: Option, + initialized: bool, } impl Session { pub fn new(name: impl Into, version: impl Into, policy: Policy) -> Self { - Self { name: name.into(), version: version.into(), policy, state: State::New, capabilities: json!({"tools": {}}) } + Self { + name: name.into(), + version: version.into(), + policy, + state: State::New, + capabilities: json!({"tools": {}}), + versions: vec![PROTOCOL_VERSION.into()], + permissive_initialize: false, + server_info: None, + instructions: None, + initialized: false, + } } /// Set the server capabilities returned at initialization. The value must be an object. @@ -50,6 +66,27 @@ impl Session { Ok(self) } + /// Configure a permissive legacy server: absent or malformed initialize parameters are + /// accepted, supported client versions are echoed, and unknown versions use the first entry. + pub fn with_legacy_initialize(mut self, versions: &[&str], server_info: Value) -> Self { + if !versions.is_empty() { + self.versions = versions.iter().map(|v| (*v).to_owned()).collect(); + } + self.permissive_initialize = true; + self.server_info = Some(server_info); + self + } + + /// Update connection-specific instructions returned by subsequent initialize requests. + pub fn set_instructions(&mut self, instructions: String) { + self.instructions = Some(instructions); + } + + /// Whether an initialized notification has been observed, including before initialize. + pub fn is_initialized(&self) -> bool { + self.initialized + } + pub fn state(&self) -> State { self.state } @@ -64,6 +101,18 @@ impl Session { if self.policy == Policy::Strict && (in_batch || self.state != State::New) { return Some(Err(RpcError::new(-32600, "Invalid Request"))); } + if self.permissive_initialize { + let asked = request.params.and_then(|p| p.get("protocolVersion")).and_then(Value::as_str); + let version = asked + .filter(|v| self.versions.iter().any(|supported| supported == v)) + .unwrap_or_else(|| self.versions.first().map(String::as_str).unwrap_or(PROTOCOL_VERSION)); + let mut result = json!({"protocolVersion": version, "capabilities": self.capabilities, "serverInfo": self.server_info}); + if let Some(instructions) = &self.instructions { + result["instructions"] = json!(instructions); + } + self.state = State::AwaitingInitialized; + return Some(Ok(result)); + } let Some(params) = request.params.and_then(Value::as_object) else { return Some(Err(invalid_initialize())); }; @@ -89,8 +138,11 @@ impl Session { /// Observe a JSON-RPC notification; notifications never receive replies. pub fn notification(&mut self, request: Request<'_>) { - if request.method == "notifications/initialized" && self.state == State::AwaitingInitialized { - self.state = State::Ready; + if request.method == "notifications/initialized" { + self.initialized = true; + if self.state == State::AwaitingInitialized { + self.state = State::Ready; + } } } } @@ -127,6 +179,22 @@ mod tests { Request { id: None, method, params } } + #[test] + fn legacy_initialization_negotiates_and_accepts_missing_parameters() { + let mut s = Session::new("test", "1", Policy::Compatible).with_legacy_initialize(&["new", "old"], json!({"name":"custom", "title":"Title"})); + s.notification(call("notifications/initialized", None)); + assert!(s.is_initialized()); + s.set_instructions("help".into()); + for (params, expected) in + [(None, "new"), (Some(json!({"protocolVersion":"old"})), "old"), (Some(json!({"protocolVersion":"unknown"})), "new")] + { + let result = s.request(call("initialize", params.as_ref()), false).unwrap().unwrap(); + assert_eq!(result["protocolVersion"], expected); + assert_eq!(result["serverInfo"]["title"], "Title"); + assert_eq!(result["instructions"], "help"); + } + } + #[test] fn compatible_negotiation_preserves_existing_clients() { let mut s = Session::new("test", "1", Policy::Compatible); From 451a390542fbde0519cbb8ade3a3325cf02d97be Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:37:41 +0000 Subject: [PATCH 034/150] Add opt-in legacy Base64 compatibility --- crates/mime/src/base64_compat.rs | 93 ++++++++++++++++++++++++++++++++ crates/mime/src/lib.rs | 1 + 2 files changed, 94 insertions(+) create mode 100644 crates/mime/src/base64_compat.rs diff --git a/crates/mime/src/base64_compat.rs b/crates/mime/src/base64_compat.rs new file mode 100644 index 000000000..452b276f9 --- /dev/null +++ b/crates/mime/src/base64_compat.rs @@ -0,0 +1,93 @@ +//! Compatibility decoding for legacy transports that accepted mixed Base64 alphabets and padding. +//! +//! This is deliberately permissive and must not be used for signatures, credentials or other +//! contexts requiring canonical Base64. Use [`crate::base64`] for strict RFC 4648 decoding. +use crate::base64::DecodeError; + +/// Decode standard or URL-safe Base64, accepting either alphabet even in the same input. +/// Spaces, CR, LF and `=` are ignored wherever they occur; an incomplete final sextet is +/// discarded. Other bytes are errors. Output allocation is bounded by the input length. +pub fn decode(input: impl AsRef<[u8]>) -> Result, DecodeError> { + let input = input.as_ref(); + let mut out = Vec::new(); + let capacity = (input.len() / 4) * 3 + (input.len() % 4) * 3 / 4; + out.try_reserve(capacity).map_err(|_| DecodeError)?; + let (mut acc, mut bits) = (0u32, 0u32); + for c in input { + let v = match c { + b'A'..=b'Z' => c - b'A', + b'a'..=b'z' => c - b'a' + 26, + b'0'..=b'9' => c - b'0' + 52, + b'+' | b'-' => 62, + b'/' | b'_' => 63, + b'=' | b'\n' | b'\r' | b' ' => continue, + _ => return Err(DecodeError), + }; + acc = (acc << 6) | u32::from(v); + bits += 6; + if bits >= 8 { + bits -= 8; + out.push((acc >> bits) as u8); + acc &= (1 << bits) - 1; + } + } + Ok(out) +} +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used)] + use super::*; + #[test] + fn accepts_mixed_alphabets_and_ignores_padding_anywhere() { + assert_eq!(decode("+_8=").unwrap(), [251, 255]); + assert_eq!(decode("Z=m 9\nv\r").unwrap(), b"foo"); + assert_eq!(decode("A").unwrap(), b""); + assert_eq!(decode("AB").unwrap(), [0]); + assert!(decode("Zm9v\t").is_err()); + assert!(decode("Zm9v!").is_err()); + assert!(crate::base64::STANDARD.decode("Z=m9v").is_err()); + } + // Differential against CADCraft's pre-cutover decoder, including malformed legacy inputs. + fn legacy(s: &str) -> Option> { + let mut out = Vec::new(); + let (mut acc, mut bits) = (0u32, 0u32); + for c in s.bytes() { + let value = match c { + b'A'..=b'Z' => c - b'A', + b'a'..=b'z' => c - b'a' + 26, + b'0'..=b'9' => c - b'0' + 52, + b'+' | b'-' => 62, + b'/' | b'_' => 63, + b'=' | b'\n' | b'\r' | b' ' => continue, + _ => return None, + }; + acc = (acc << 6) | u32::from(value); + bits += 6; + if bits >= 8 { + bits -= 8; + out.push((acc >> bits) as u8); + } + } + Some(out) + } + #[test] + fn matches_the_legacy_decoder_on_seeded_mixed_inputs() { + let alphabet = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/-_= \r\n!\t"; + let mut seed = 0x1234_5678u32; + for n in 0..50_000 { + let mut text = String::new(); + for _ in 0..n % 45 { + seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + text.push(char::from(alphabet[(seed as usize) % alphabet.len()])); + } + assert_eq!(decode(&text).ok(), legacy(&text), "{text:?}"); + } + } + + #[test] + fn round_trip_rfc_vectors_with_the_shared_encoder() { + for data in [&b""[..], b"f", b"fo", b"foo", b"foobar", &[0, 255, 127]] { + assert_eq!(decode(crate::base64::STANDARD.encode(data)).unwrap(), data); + } + } +} diff --git a/crates/mime/src/lib.rs b/crates/mime/src/lib.rs index 72a930b8b..d3ff9f257 100644 --- a/crates/mime/src/lib.rs +++ b/crates/mime/src/lib.rs @@ -28,6 +28,7 @@ //! says) is the caller's: this crate has no notion of a mailbox or a document. pub mod base64; +pub mod base64_compat; pub mod charset; #[cfg(feature = "detect")] pub mod detect; From 8e394d43faae421541892945cc56f10e47ee3be7 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:39:42 +0000 Subject: [PATCH 035/150] audio: share bounded range-backed WAV PCM access --- crates/audio/src/lib.rs | 3 + crates/audio/src/wav_range.rs | 218 ++++++++++++++++++++++++++++++++++ 2 files changed, 221 insertions(+) create mode 100644 crates/audio/src/wav_range.rs diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index 33dcdee3b..8681ce6d8 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -66,6 +66,9 @@ mod wav; feature = "vorbis-encode" ))] mod write; + +/// Range-backed PCM WAVE access and a 16-bit PCM writer. +pub mod wav_range; #[cfg(feature = "vorbis-encode")] pub use write::{OGG_BITRATES, encode_ogg}; diff --git a/crates/audio/src/wav_range.rs b/crates/audio/src/wav_range.rs new file mode 100644 index 000000000..1ced4fb78 --- /dev/null +++ b/crates/audio/src/wav_range.rs @@ -0,0 +1,218 @@ +//! Range-backed PCM WAVE reader and canonical 16-bit writer. +//! +//! Reads chunk headers and only the requested sample range; it never loads a whole file. + +use crate::{AudioError, Result}; +use craft_media::ByteSource; + +const MAX_CHUNKS: usize = 1 << 16; +const IXML_MAX: u64 = 1 << 20; +/// Maximum sample data returned by a single range read. +pub const MAX_RANGE_BYTES: u64 = 4 << 20; + +fn le16(b: &[u8], o: usize) -> u16 { + b.get(o..o.saturating_add(2)).and_then(|s| s.try_into().ok()).map_or(0, u16::from_le_bytes) +} +fn le32(b: &[u8], o: usize) -> u32 { + b.get(o..o.saturating_add(4)).and_then(|s| s.try_into().ok()).map_or(0, u32::from_le_bytes) +} +fn read_range(source: &impl ByteSource, off: u64, len: usize) -> Result> { + let mut bytes = vec![0; len]; + source.read_at(off, &mut bytes)?; + Ok(bytes) +} + +/// A PCM WAVE file held by an arbitrary random-access source. +pub struct WavRange { + source: S, + /// Number of complete sample frames. + pub frames: u64, + /// Channel count. + pub channels: u16, + /// Sample rate in Hz. + pub rate: u32, + /// Container bits per sample. + pub bits: u16, + /// Whether samples are floating point. + pub float: bool, + /// BWF time reference in samples, from bext or iXML. + pub time_reference: Option, + data_off: u64, +} + +impl WavRange { + /// Open with caller-selected bounds on channel count and sample rate. + pub fn open(source: S, max_channels: u32, max_rate: u32) -> Result { + let len = source.len(); + let mut pos: u64 = 12; + let mut fmt = None; + let mut data = None; + let mut bext_tr = None; + let mut ixml_tr = None; + let mut ds64_data = None; + for _ in 0..MAX_CHUNKS { + let Some(body) = pos.checked_add(8).filter(|&b| b <= len) else { break }; + let mut hdr = [0u8; 8]; + source.read_at(pos, &mut hdr)?; + let id = &hdr[0..4]; + let mut clen = u64::from(le32(&hdr, 4)); + let rest = len - body; + if id == b"data" { + if clen == 0xFFFF_FFFF { + clen = ds64_data.unwrap_or(rest); + } + clen = clen.min(rest); + } + if clen > rest { + break; + } + match id { + b"fmt " if clen >= 16 => { + let b = read_range(&source, body, clen.min(40) as usize)?; + let mut tag = le16(&b, 0); + if tag == 0xFFFE && clen >= 40 { + tag = le16(&b, 24); + } + fmt = Some((tag, le16(&b, 2), le32(&b, 4), le16(&b, 14))); + } + b"ds64" if clen >= 16 => { + let b = read_range(&source, body + 8, 8)?; + ds64_data = Some(u64::from(le32(&b, 0)) | u64::from(le32(&b, 4)) << 32); + } + b"data" => data = Some((body, clen)), + b"bext" if clen >= 346 => { + let b = read_range(&source, body + 338, 8)?; + bext_tr = Some(u64::from(le32(&b, 0)) | u64::from(le32(&b, 4)) << 32); + } + b"iXML" => { + let b = read_range(&source, body, clen.min(IXML_MAX) as usize)?; + ixml_tr = ixml_time_reference(&b); + } + _ => {} + } + pos = body.saturating_add(clen).saturating_add(clen & 1); + } + let (tag, channels, rate, bits) = fmt.ok_or_else(|| AudioError::Malformed("missing fmt chunk".into()))?; + let (data_off, dlen) = data.ok_or_else(|| AudioError::Malformed("missing data chunk".into()))?; + let float = tag == 3; + if !(tag == 1 || float) + || channels == 0 + || rate == 0 + || u32::from(channels) > max_channels + || rate > max_rate + || !matches!(bits, 8 | 16 | 24 | 32 | 64) + { + return Err(AudioError::Unsupported(format!("WAV format tag {tag}, {bits} bits"))); + } + let frames = dlen / (u64::from(channels) * u64::from(bits / 8)); + Ok(Self { source, frames, channels, rate, bits, float, time_reference: bext_tr.or(ixml_tr), data_off }) + } + /// Bytes in one whole frame. + pub fn frame_bytes(&self) -> usize { + usize::from(self.channels) * usize::from(self.bits / 8) + } + /// Read the inclusive range of frames, capped to prevent unbounded allocation. + pub fn read_frames(&self, first: u64, last: u64) -> Result> { + if first > last || last >= self.frames { + return Err(AudioError::Malformed("WAV frame range outside data".into())); + } + let n = (last - first + 1).saturating_mul(self.frame_bytes() as u64); + if n > MAX_RANGE_BYTES { + return Err(AudioError::TooLarge("WAV frame range".into())); + } + let off = self.data_off.saturating_add(first.saturating_mul(self.frame_bytes() as u64)); + read_range(&self.source, off, n as usize) + } + /// Decode a channel from a buffer returned by `read_frames`. + pub fn sample(&self, block: &[u8], frame: usize, ch: usize) -> f32 { + if ch >= usize::from(self.channels) { + return 0.0; + } + let bps = usize::from(self.bits / 8); + let Some(o) = frame.checked_mul(usize::from(self.channels)).and_then(|v| v.checked_add(ch)).and_then(|v| v.checked_mul(bps)) else { + return 0.0; + }; + let Some(b) = block.get(o..o.saturating_add(bps)) else { return 0.0 }; + match (self.bits, self.float, b) { + (8, _, &[a]) => (a as f32 - 128.0) / 128.0, + (16, _, &[a, b]) => i16::from_le_bytes([a, b]) as f32 / 32768.0, + (24, _, &[a, b, c]) => (i32::from_le_bytes([0, a, b, c]) >> 8) as f32 / 8_388_608.0, + (32, false, &[a, b, c, d]) => i32::from_le_bytes([a, b, c, d]) as f32 / 2_147_483_648.0, + (32, true, &[a, b, c, d]) => f32::from_le_bytes([a, b, c, d]), + (64, true, b) => f64::from_le_bytes(b.try_into().unwrap_or([0; 8])) as f32, + _ => 0.0, + } + } +} + +fn ixml_time_reference(x: &[u8]) -> Option { + let text = String::from_utf8_lossy(x); + let field = |tag: &str| -> Option { + let open = format!("<{tag}>"); + let a = text.find(&open)? + open.len(); + let b = a + text[a..].find('<')?; + text[a..b].trim().parse().ok() + }; + let low = field("BWF_TIME_REFERENCE_LOW")?; + Some(low | field("BWF_TIME_REFERENCE_HIGH").unwrap_or(0) << 32) +} + +/// Write interleaved samples as 16-bit PCM WAVE, using symmetric 32767 scaling. +#[cfg(feature = "pcm-encode")] +pub fn write_wav16(samples: &[f32], channels: u16, rate: u32) -> Vec { + let data_len = samples.len().saturating_mul(2); + let mut v = crate::pcm_wav_header(u32::try_from(data_len).unwrap_or(u32::MAX), channels, rate, 16); + v.reserve(data_len); + for s in samples { + v.extend_from_slice(&((s.clamp(-1.0, 1.0) * 32767.0).round() as i16).to_le_bytes()); + } + v +} + +#[cfg(all(test, feature = "pcm-encode"))] +mod tests { + use super::*; + use std::sync::{Arc, Mutex}; + + struct Source { + bytes: Vec, + reads: Arc>>, + } + impl ByteSource for Source { + fn len(&self) -> u64 { + self.bytes.len() as u64 + } + fn read_at(&self, offset: u64, buf: &mut [u8]) -> std::io::Result<()> { + self.reads.lock().unwrap().push((offset, buf.len())); + self.bytes.read_at(offset, buf) + } + } + #[test] + fn bounded_reads_and_pcm_values() { + let bytes = write_wav16(&[-1.0, -0.5, 0.0, 0.5, 1.0], 1, 48_000); + let reads = Arc::new(Mutex::new(Vec::new())); + let wav = WavRange::open(Source { bytes, reads: reads.clone() }, 64, 384_000).unwrap(); + assert_eq!((wav.frames, wav.channels, wav.rate, wav.bits), (5, 1, 48_000, 16)); + assert!(reads.lock().unwrap().iter().all(|(_, n)| *n <= 40)); + let data = wav.read_frames(1, 3).unwrap(); + assert_eq!(wav.sample(&data, 0, 0), -0.5); + assert_eq!(wav.sample(&data, 2, 0), 0.5); + assert!(wav.read_frames(0, 5).is_err()); + assert!(wav.read_frames(3, 1).is_err()); + assert_eq!(reads.lock().unwrap().last(), Some(&(46, 6))); + } + #[test] + fn ixml_fallback_accepts_flat_fields() { + let base = write_wav16(&[0.0], 1, 48_000); + let mut bytes = base[..36].to_vec(); + let xml = b"51"; + bytes.extend_from_slice(b"iXML"); + bytes.extend_from_slice(&(xml.len() as u32).to_le_bytes()); + bytes.extend_from_slice(xml); + if xml.len() % 2 != 0 { + bytes.push(0); + } + bytes.extend_from_slice(&base[36..]); + assert_eq!(WavRange::open(bytes.as_slice(), 64, 384_000).unwrap().time_reference, Some((1 << 32) + 5)); + } +} From badb7904116d518ad84ff1da5a1d99d3cc5e584f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:40:02 +0000 Subject: [PATCH 036/150] audio: expose WAV signature detection --- crates/audio/src/wav_range.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/audio/src/wav_range.rs b/crates/audio/src/wav_range.rs index 1ced4fb78..d8aea0d54 100644 --- a/crates/audio/src/wav_range.rs +++ b/crates/audio/src/wav_range.rs @@ -5,6 +5,11 @@ use crate::{AudioError, Result}; use craft_media::ByteSource; +/// Whether a prefix identifies a RIFF or RF64 WAVE file. +pub fn sniff(b: &[u8]) -> bool { + b.len() >= 12 && (&b[0..4] == b"RIFF" || &b[0..4] == b"RF64") && &b[8..12] == b"WAVE" +} + const MAX_CHUNKS: usize = 1 << 16; const IXML_MAX: u64 = 1 << 20; /// Maximum sample data returned by a single range read. From ac037cd9413e32fd16e0103628485f7eb011abfa Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:46:32 +0000 Subject: [PATCH 037/150] aaf: expose bounded Bento container access --- crates/aaf/src/omf.rs | 2 +- crates/aaf/src/omf/bento.rs | 25 ++++++++++++++++++------- crates/aaf/tests/bento_api.rs | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 crates/aaf/tests/bento_api.rs diff --git a/crates/aaf/src/omf.rs b/crates/aaf/src/omf.rs index 57e0f756c..bbd2e8571 100644 --- a/crates/aaf/src/omf.rs +++ b/crates/aaf/src/omf.rs @@ -21,7 +21,7 @@ //! transition's name) are not in the specification; they were first written by FilmCraft and keep //! their names so that existing files read back. -pub(crate) mod bento; +pub mod bento; mod read; mod write; diff --git a/crates/aaf/src/omf/bento.rs b/crates/aaf/src/omf/bento.rs index 00a7e2a26..171196fe1 100644 --- a/crates/aaf/src/omf/bento.rs +++ b/crates/aaf/src/omf/bento.rs @@ -35,8 +35,8 @@ use std::collections::{BTreeMap, HashMap}; -pub(crate) const MAGIC: [u8; 8] = [0xA4, b'C', b'M', 0xA5, b'H', b'd', b'r', 0xD7]; -pub(crate) const LABEL_SIZE: usize = 24; +pub const MAGIC: [u8; 8] = [0xA4, b'C', b'M', 0xA5, b'H', b'd', b'r', 0xD7]; +pub const LABEL_SIZE: usize = 24; // standard object ids const TOC_OBJECT: u32 = 1; @@ -82,7 +82,7 @@ const MAX_VALUE_BYTES: usize = 16 << 20; const MAX_NAME_BYTES: usize = 4 << 20; /// Builds a Bento container. -pub(crate) struct Writer { +pub struct Writer { data: Vec, names: HashMap<(u32, String), u32>, next: u32, @@ -90,6 +90,12 @@ pub(crate) struct Writer { entries: Vec<(u32, u32, u32, Vec)>, } +impl Default for Writer { + fn default() -> Self { + Self::new() + } +} + impl Writer { pub fn new() -> Writer { Writer { data: Vec::new(), names: HashMap::new(), next: MIN_USER_ID, entries: Vec::new() } @@ -217,9 +223,9 @@ pub(crate) struct Prop { } /// A parsed Bento container. -pub(crate) struct Container<'a> { +pub struct Container<'a> { data: &'a [u8], - pub objects: BTreeMap>, + pub(crate) objects: BTreeMap>, names: HashMap, by_name: HashMap, } @@ -229,11 +235,16 @@ fn be32(b: &[u8], at: usize) -> Option { } /// Whether `b` ends with a Bento label. -pub(crate) fn sniff(b: &[u8]) -> bool { +pub fn sniff(b: &[u8]) -> bool { b.len() >= LABEL_SIZE && b[b.len() - LABEL_SIZE..b.len() - LABEL_SIZE + 8] == MAGIC } impl<'a> Container<'a> { + /// Object identifiers in ascending order. + pub fn object_ids(&self) -> impl Iterator + '_ { + self.objects.keys().copied() + } + pub fn open(data: &'a [u8]) -> Result, String> { if !sniff(data) { return Err("no Bento label at the end of the file".into()); @@ -324,7 +335,7 @@ impl<'a> Container<'a> { } /// The bytes of a value. - pub fn value(&self, p: &Prop) -> Result, String> { + pub(crate) fn value(&self, p: &Prop) -> Result, String> { let total = p.segs.iter().try_fold(0usize, |total, s| { let len = match s { Seg::Imm(b) => b.len(), diff --git a/crates/aaf/tests/bento_api.rs b/crates/aaf/tests/bento_api.rs new file mode 100644 index 000000000..9df70c661 --- /dev/null +++ b/crates/aaf/tests/bento_api.rs @@ -0,0 +1,17 @@ +use craft_aaf::omf::bento::{Container, Writer, sniff}; + +#[test] +fn external_consumer_reads_named_values_and_references() { + let mut writer = Writer::new(); + let first = writer.new_object(); + let second = writer.new_object(); + writer.set(first, "test:value", "test:type", b"long value"); + writer.set_references(first, &[second]); + let bytes = writer.finish(); + assert!(sniff(&bytes)); + let container = Container::open(&bytes).unwrap(); + assert!(container.object_ids().any(|id| id == first)); + assert_eq!(container.get(first, "test:value").as_deref(), Some(b"long value".as_slice())); + assert_eq!(container.resolve(first, second), second); + assert!(Container::open(&bytes[..bytes.len() - 1]).is_err()); +} From aa398af9dd312523c8a9fcb3b2711b726a40abdd Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:48:01 +0000 Subject: [PATCH 038/150] audio: fix range test lint on workspace gates --- crates/audio/src/wav_range.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/audio/src/wav_range.rs b/crates/audio/src/wav_range.rs index d8aea0d54..33762caf2 100644 --- a/crates/audio/src/wav_range.rs +++ b/crates/audio/src/wav_range.rs @@ -188,7 +188,7 @@ mod tests { self.bytes.len() as u64 } fn read_at(&self, offset: u64, buf: &mut [u8]) -> std::io::Result<()> { - self.reads.lock().unwrap().push((offset, buf.len())); + self.reads.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push((offset, buf.len())); self.bytes.read_at(offset, buf) } } @@ -214,7 +214,7 @@ mod tests { bytes.extend_from_slice(b"iXML"); bytes.extend_from_slice(&(xml.len() as u32).to_le_bytes()); bytes.extend_from_slice(xml); - if xml.len() % 2 != 0 { + if xml.len() & 1 != 0 { bytes.push(0); } bytes.extend_from_slice(&base[36..]); From a88aab72c4620d1956196ec51107c3ca9c241a52 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:58:28 +0000 Subject: [PATCH 039/150] aaf: bound duplicate property detection work --- crates/aaf/src/aaf/hostile_tests.rs | 16 ++++++++++++++++ crates/aaf/src/aaf/store.rs | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/aaf/src/aaf/hostile_tests.rs b/crates/aaf/src/aaf/hostile_tests.rs index 853b0000c..527e50da8 100644 --- a/crates/aaf/src/aaf/hostile_tests.rs +++ b/crates/aaf/src/aaf/hostile_tests.rs @@ -108,3 +108,19 @@ fn objects_nested_too_deeply_are_an_error() { let e = store::read(&chain(250)).unwrap_err(); assert!(e.contains("nested too deeply"), "{e}"); } + +/// Many distinct property IDs still reject a duplicate without quadratic scanning. +#[test] +fn duplicate_after_many_distinct_properties_is_rejected() { + let mut props = vec![0x4C, 0x20]; + props.extend_from_slice(&8193u16.to_le_bytes()); + for id in (0..8192u16).chain(std::iter::once(7)) { + props.extend_from_slice(&id.to_le_bytes()); + props.extend_from_slice(&0x82u16.to_le_bytes()); + props.extend_from_slice(&0u16.to_le_bytes()); + } + let mut writer = Writer::new(Version::V3); + writer.stream(Writer::ROOT, "properties", props).unwrap(); + let e = store::read(&writer.finish()).unwrap_err(); + assert!(e.contains("duplicate property id"), "{e}"); +} diff --git a/crates/aaf/src/aaf/store.rs b/crates/aaf/src/aaf/store.rs index 69b14fcf2..859c0ea77 100644 --- a/crates/aaf/src/aaf/store.rs +++ b/crates/aaf/src/aaf/store.rs @@ -461,11 +461,13 @@ impl R<'_> { if at > props.len() { return Err(bad("truncated property index")); } + // A properties stream can name tens of thousands of ids: do not rescan all earlier ids. + let mut seen = std::collections::HashSet::with_capacity(n); for i in 0..n { let (Some(pid), Some(sf), Some(len)) = (rd_u16(&props, 4 + i * 6), rd_u16(&props, 6 + i * 6), rd_u16(&props, 8 + i * 6)) else { return Err(bad("truncated property index")); }; - if o.props.iter().any(|(previous, _)| *previous == pid) { + if !seen.insert(pid) { return Err(bad("duplicate property id")); } let v = props.get(at..at + len as usize).ok_or_else(|| bad("truncated property value"))?; From 6345459e1297ee85a7ca62b428a440691bd062c1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:59:23 +0000 Subject: [PATCH 040/150] Share MTEXT Unicode and MIF escape decoding --- crates/dxf/src/lib.rs | 1 + crates/dxf/src/mtext.rs | 103 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 crates/dxf/src/mtext.rs diff --git a/crates/dxf/src/lib.rs b/crates/dxf/src/lib.rs index 4cf3e46e9..9c912613b 100644 --- a/crates/dxf/src/lib.rs +++ b/crates/dxf/src/lib.rs @@ -11,6 +11,7 @@ use std::io::Write as _; pub mod model; +pub mod mtext; pub mod paths; mod version; pub use version::Version; diff --git a/crates/dxf/src/mtext.rs b/crates/dxf/src/mtext.rs new file mode 100644 index 000000000..9b113aa6c --- /dev/null +++ b/crates/dxf/src/mtext.rs @@ -0,0 +1,103 @@ +//! MTEXT inline-escape handling independent of drawing and text layout. +//! +//! Unicode and MIF escapes are decoded to literal characters, with markup-significant +//! characters re-escaped so subsequent MTEXT parsers cannot treat them as commands. + +/// Whether an inline command takes an argument terminated by `;`. +pub fn takes_arg(code: char) -> bool { + matches!(code, 'f' | 'F' | 'H' | 'W' | 'Q' | 'T' | 'A' | 'C' | 'c' | 'p') +} + +/// Four hex digits at `chars[at..at + 4]`. +fn hex4(chars: &[char], at: usize) -> Option { + let digits = chars.get(at..at.checked_add(4)?)?; + digits.iter().try_fold(0u32, |acc, c| Some(acc * 16 + c.to_digit(16)?)) +} + +/// Push a decoded character so that the tokenizers read it as text, never as markup. +fn push_literal(out: &mut String, c: char) { + if matches!(c, '\\' | '{' | '}') { + out.push('\\'); + } + out.push(c); +} + +/// Decode `\U+hhhh` (one UTF-16 unit; a surrogate pair gives one character, a lone surrogate +/// U+FFFD) and `\M+nhhhh` (double-byte East Asian characters) to the characters they stand +/// for. Malformed escapes remain unchanged for the caller to interpret. +/// An escaped backslash (`\\U+0041`) is not an escape. +pub fn unescape_codes(s: &str) -> std::borrow::Cow<'_, str> { + if !s.contains("\\U+") && !s.contains("\\M+") { + return std::borrow::Cow::Borrowed(s); + } + let chars: Vec = s.chars().collect(); + let mut out = String::with_capacity(s.len()); + let mut i = 0; + while let Some(&c) = chars.get(i) { + if c != '\\' { + out.push(c); + i += 1; + continue; + } + let (n, plus) = (chars.get(i + 1).copied(), chars.get(i + 2).copied()); + if n == Some('U') && plus == Some('+') { + if let Some(unit) = hex4(&chars, i + 3) { + i += 7; + let low = |at: usize| { + (chars.get(at) == Some(&'\\') && chars.get(at + 1) == Some(&'U') && chars.get(at + 2) == Some(&'+')) + .then(|| hex4(&chars, at + 3)) + .flatten() + .filter(|u| (0xDC00..0xE000).contains(u)) + }; + let ch = if (0xD800..0xDC00).contains(&unit) { + match low(i) { + Some(lo) => { + i += 7; + char::from_u32(0x10000 + ((unit - 0xD800) << 10) + (lo - 0xDC00)) + } + None => None, + } + } else { + char::from_u32(unit) + }; + push_literal(&mut out, ch.unwrap_or('\u{fffd}')); + continue; + } + } else if n == Some('M') && plus == Some('+') && chars.get(i + 3).is_some_and(char::is_ascii_digit) && hex4(&chars, i + 4).is_some() { + push_literal(&mut out, crate::mif_escape_at(&chars, i).unwrap_or('\u{fffd}')); + i += 8; + continue; + } + // Any other backslash pair stays as it is (and an escaped backslash stays one). + out.push('\\'); + i += 1; + if let Some(&next) = chars.get(i) { + out.push(next); + i += 1; + } + } + std::borrow::Cow::Owned(out) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn unicode_and_mif_escapes_remain_literal_markup() { + assert_eq!(unescape_codes(r"a\U+0041\U+D83D\U+DE00"), "aA😀"); + assert_eq!(unescape_codes(r"\M+5BCFE"), "ä»¶"); + assert_eq!(unescape_codes(r"\U+005C\U+007B\U+007D"), r"\\\{\}"); + assert_eq!(unescape_codes(r"\\U+0041"), r"\\U+0041"); + assert_eq!(unescape_codes(r"\U+12"), r"\U+12"); + assert_eq!(unescape_codes(r"\U+D800"), "�"); + } + #[test] + fn parameterized_commands_are_classified_without_relaxing_others() { + for c in "fFHWQTACcp".chars() { + assert!(takes_arg(c)); + } + for c in ['S', 'P', 'Z', '\\'] { + assert!(!takes_arg(c)); + } + } +} From b1d4c67196ee7c6b668428c8d211e7d5171dff88 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:02:52 +0000 Subject: [PATCH 041/150] Add streaming MTEXT lexical tokens --- crates/dxf/src/lib.rs | 1 + crates/dxf/src/mtext_tokens.rs | 132 +++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 crates/dxf/src/mtext_tokens.rs diff --git a/crates/dxf/src/lib.rs b/crates/dxf/src/lib.rs index 9c912613b..7543fb28b 100644 --- a/crates/dxf/src/lib.rs +++ b/crates/dxf/src/lib.rs @@ -12,6 +12,7 @@ use std::io::Write as _; pub mod model; pub mod mtext; +pub mod mtext_tokens; pub mod paths; mod version; pub use version::Version; diff --git a/crates/dxf/src/mtext_tokens.rs b/crates/dxf/src/mtext_tokens.rs new file mode 100644 index 000000000..b07a9c2e0 --- /dev/null +++ b/crates/dxf/src/mtext_tokens.rs @@ -0,0 +1,132 @@ +//! Streaming lexical tokens for MTEXT inline markup. +//! +//! Apply [`crate::mtext::unescape_codes`] first when Unicode/MIF escapes should become literal +//! characters. Tokens borrow their source and scanning allocates no memory. The consumer owns +//! formatting, line layout, paragraph policy and how unknown commands are displayed. + +/// How a stacked-text argument finds its closing semicolon. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum StackMode { + /// The first semicolon ends the stack, as plain-text extraction historically did. + Plain, + /// A backslash quotes the next character, as the structured layout parser does. + Structured, +} +/// One lexical unit. Unknown commands retain their letter so the caller chooses compatibility. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Token<'a> { + Char(char), + OpenGroup, + CloseGroup, + Control(char), + Argument(char, &'a str), + Stack(&'a str), + Unknown(char), +} +/// A bounded-memory walk of UTF-8 MTEXT markup. +pub struct Tokens<'a> { + source: &'a str, + offset: usize, + stack_mode: StackMode, +} +impl<'a> Tokens<'a> { + pub fn new(source: &'a str, stack_mode: StackMode) -> Self { + Self { source, offset: 0, stack_mode } + } + fn next_char(&mut self) -> Option { + let c = self.source.get(self.offset..)?.chars().next()?; + self.offset += c.len_utf8(); + Some(c) + } + fn argument(&mut self, structured: bool) -> &'a str { + let start = self.offset; + let mut end = start; + while let Some(c) = self.next_char() { + if c == ';' { + return self.source.get(start..end).unwrap_or_default(); + } + if structured && c == '\\' { + let _ = self.next_char(); + } + end = self.offset; + } + self.source.get(start..end).unwrap_or_default() + } +} +impl<'a> Iterator for Tokens<'a> { + type Item = Token<'a>; + fn next(&mut self) -> Option { + let c = self.next_char()?; + Some(match c { + '{' => Token::OpenGroup, + '}' => Token::CloseGroup, + '\\' => { + let code = self.next_char()?; + match code { + 'S' => Token::Stack(self.argument(self.stack_mode == StackMode::Structured)), + c if crate::mtext::takes_arg(c) => Token::Argument(c, self.argument(false)), + c @ ('P' | 'N' | '~' | '\\' | '{' | '}' | 'L' | 'l' | 'O' | 'o' | 'K' | 'k') => Token::Control(c), + c => Token::Unknown(c), + } + } + c => Token::Char(c), + }) + } +} +/// Split a structured stack into top, bottom and separator, unquoting escaped characters. +pub fn split_stack(raw: &str) -> (String, String, Option) { + let (mut top, mut bottom, mut separator) = (String::new(), String::new(), None); + let mut chars = raw.chars(); + while let Some(c) = chars.next() { + if c == '\\' { + if let Some(c) = chars.next() { + if separator.is_none() { + top.push(c); + } else { + bottom.push(c); + } + } + } else if separator.is_none() && matches!(c, '/' | '#' | '^') { + separator = Some(c); + } else if separator.is_none() { + top.push(c); + } else { + bottom.push(c); + } + } + (top, bottom, separator) +} +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn tokens_borrow_text_and_preserve_unknown_commands() { + let got: Vec<_> = Tokens::new(r"A\Z\P{\H2x;x}\S1/2;", StackMode::Structured).collect(); + assert_eq!( + got, + [ + Token::Char('A'), + Token::Unknown('Z'), + Token::Control('P'), + Token::OpenGroup, + Token::Argument('H', "2x"), + Token::Char('x'), + Token::CloseGroup, + Token::Stack("1/2") + ] + ); + assert_eq!(split_stack("1/2"), ("1".into(), "2".into(), Some('/'))); + } + #[test] + fn stacked_text_uses_the_requested_terminator_rules() { + let text = r"\Sa\;b/c;X"; + assert_eq!(Tokens::new(text, StackMode::Plain).next(), Some(Token::Stack(r"a\"))); + assert_eq!(Tokens::new(text, StackMode::Structured).next(), Some(Token::Stack(r"a\;b/c"))); + assert_eq!(split_stack(r"a\;b/c"), ("a;b".into(), "c".into(), Some('/'))); + } + #[test] + fn missing_terminator_and_trailing_backslash_are_bounded() { + assert_eq!(Tokens::new("\\H2x", StackMode::Structured).collect::>(), [Token::Argument('H', "2x")]); + assert_eq!(Tokens::new("a\\", StackMode::Structured).collect::>(), [Token::Char('a')]); + } +} From ad9850f18fadac54048f680d1da7f50f379cd124 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 11:05:37 -0500 Subject: [PATCH 042/150] fix(audio): validate FLAC stream precision and complete consumption --- crates/audio/src/flac_decode.rs | 49 +++++++++++++++++++++++++++++-- crates/audio/tests/flac_decode.rs | 37 +++++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/crates/audio/src/flac_decode.rs b/crates/audio/src/flac_decode.rs index dfb6f5fc1..bb56e65c5 100644 --- a/crates/audio/src/flac_decode.rs +++ b/crates/audio/src/flac_decode.rs @@ -225,6 +225,11 @@ impl Decoder { /// Decode the frame at the start of `data`. pub fn decode_frame(&self, data: &[u8]) -> Result { + // StreamInfo has public fields: container callers can construct it without + // parse(), so validate before it can control shifts and sample reads. + if self.info.is_some_and(|i| !(4..=32).contains(&i.bits_per_sample) || !(1..=8).contains(&i.channels) || i.sample_rate == 0) { + return Err(DecodeError::Invalid("invalid decoder STREAMINFO")); + } let mut r = Bits::new(data); if r.bits(15)? != 0x7FFC { return Err(DecodeError::Invalid("missing frame sync code")); @@ -315,8 +320,7 @@ impl Decoder { } let mut chans = Vec::with_capacity(channels); for c in out { - let v: Result> = - c.into_iter().map(|s| i32::try_from(s).map_err(|_| DecodeError::Invalid("sample outside the bit depth"))).collect(); + let v: Result> = c.into_iter().map(|s| checked_sample(s, bits)).collect(); chans.push(v?); } Ok(Frame { sample_rate, bits_per_sample: bits as u8, variable_block_size: variable, number, block_size, channels: chans, size }) @@ -542,6 +546,11 @@ pub fn decode_with_limit(bytes: &[u8], max_values: u64) -> Result { if frame.channels.len() != channels.len() { return Err(DecodeError::Invalid("the channel count changes inside the stream")); } + // One file has one sample rate and depth. Otherwise callers would play + // samples under incompatible STREAMINFO metadata. + if frame.sample_rate != info.sample_rate || frame.bits_per_sample != info.bits_per_sample { + return Err(DecodeError::Invalid("frame format differs from STREAMINFO")); + } total += frame.block_size as u64; if total.saturating_mul(channels.len() as u64) > max_values { return Err(DecodeError::TooLarge); @@ -551,8 +560,44 @@ pub fn decode_with_limit(bytes: &[u8], max_values: u64) -> Result { } pos += frame.size; } + // Unknown total_samples must not turn a corrupt/truncated next frame into + // a successfully decoded shorter recording. + if pos != bytes.len() { + return Err(DecodeError::Invalid("trailing data or missing frame sync")); + } if info.total_samples != 0 && total != info.total_samples { return Err(DecodeError::Truncated); } Ok(Decoded { info, channels }) } + +// Validate the advertised precision, not just the storage type. Malformed +// predictors may produce an i32 that is still invalid for 4–24-bit audio. +fn checked_sample(sample: i64, bits: u32) -> Result { + if !(4..=32).contains(&bits) { + return Err(DecodeError::Invalid("invalid sample bit depth")); + } + let bound = 1i64 << (bits - 1); + if !(-bound..bound).contains(&sample) { + return Err(DecodeError::Invalid("sample outside the bit depth")); + } + i32::try_from(sample).map_err(|_| DecodeError::Invalid("sample outside the bit depth")) +} + +#[cfg(test)] +mod sample_bounds_tests { + use super::checked_sample; + + #[test] + fn samples_must_fit_declared_precision() { + for bits in 4..=32 { + let bound = 1i64 << (bits - 1); + assert_eq!(checked_sample(-bound, bits).map(i64::from), Ok(-bound)); + assert_eq!(checked_sample(bound - 1, bits).map(i64::from), Ok(bound - 1)); + assert!(checked_sample(-bound - 1, bits).is_err()); + assert!(checked_sample(bound, bits).is_err()); + } + assert!(checked_sample(0, 0).is_err()); + assert!(checked_sample(0, 255).is_err()); + } +} diff --git a/crates/audio/tests/flac_decode.rs b/crates/audio/tests/flac_decode.rs index c35267b08..a1895789b 100644 --- a/crates/audio/tests/flac_decode.rs +++ b/crates/audio/tests/flac_decode.rs @@ -172,3 +172,40 @@ fn ffmpeg_decodes_our_files_to_our_samples() { assert!(interleave(&got.channels) == want, "{channels}ch {bits}-bit: samples differ from ffmpeg's decode"); } } + +#[test] +fn rejects_unknown_length_corruption_and_streaminfo_format_mismatch() { + let src = signal(1, 100, 16, 7); + let original = encode(&src, 44_100, 16, 4096, 5); + let at = STREAMINFO_OFFSET as usize; + // STREAMINFO is not CRC-covered. Clear only the 36-bit total sample count. + let mut unknown = original.clone(); + unknown[at + 13] &= 0xf0; + unknown[at + 14..at + 18].fill(0); + assert_eq!(decode(&unknown).unwrap().channels, src); + for tail in [vec![0xff], vec![0x00, 0x00]] { + let mut broken = unknown.clone(); + broken.extend(tail); + assert!(decode(&broken).is_err()); + } + // The encoder spells these in frame headers, independent of STREAMINFO. + let mut wrong_rate = original.clone(); + let mut packed = u64::from_be_bytes(wrong_rate[at + 10..at + 18].try_into().unwrap()); + packed = (packed & ((1u64 << 44) - 1)) | (48_000u64 << 44); + wrong_rate[at + 10..at + 18].copy_from_slice(&packed.to_be_bytes()); + assert!(decode(&wrong_rate).is_err()); + let mut wrong_depth = original; + wrong_depth[at + 12] = (wrong_depth[at + 12] & !1) | 1; + wrong_depth[at + 13] = (wrong_depth[at + 13] & 0x0f) | 0x70; + assert!(decode(&wrong_depth).is_err()); +} + +#[test] +fn caller_constructed_streaminfo_is_validated_before_decoding() { + let file = encode(&signal(1, 100, 16, 9), 44_100, 16, 4096, 5); + let info = StreamInfo::parse(&file[STREAMINFO_OFFSET as usize..]).unwrap(); + for bits in [0, 1, 3, 33, 64, 255] { + let invalid = StreamInfo { bits_per_sample: bits, ..info }; + assert!(matches!(Decoder::new(invalid).decode_frame(&[]), Err(DecodeError::Invalid(_)))); + } +} From d3024d0cd5af26edecc04b0a68689436952bd116 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 11:05:39 -0500 Subject: [PATCH 043/150] fix(captions): preserve literal ASS escapes and normalize newlines --- crates/captions/src/ass.rs | 11 ++++++++--- crates/captions/tests/it/ass.rs | 31 ++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/crates/captions/src/ass.rs b/crates/captions/src/ass.rs index 3c907c829..2d0a1df55 100644 --- a/crates/captions/src/ass.rs +++ b/crates/captions/src/ass.rs @@ -191,7 +191,10 @@ fn escape(text: &str) -> String { let mut out = String::with_capacity(text.len() + 8); let mut rest = text; while !rest.is_empty() { - if let Some(r) = rest.strip_prefix("") { + if let Some(r) = rest.strip_prefix("\r\n") { + out.push_str("\\N"); + rest = r; + } else if let Some(r) = rest.strip_prefix("") { out.push_str("{\\i1}"); rest = r; } else if let Some(r) = rest.strip_prefix("") { @@ -211,7 +214,9 @@ fn escape(text: &str) -> String { rest = r; } else if let Some(c) = rest.chars().next() { match c { - '\n' => out.push_str("\\N"), + '\n' | '\r' => out.push_str("\\N"), + // Empty override block separates a literal slash from N/n/h. + '\\' => out.push_str("\\{}"), '{' => out.push('\u{ff5b}'), '}' => out.push('\u{ff5d}'), '\u{a0}' => out.push_str("\\h"), @@ -232,7 +237,7 @@ pub fn write(doc: &Document) -> String { ); for c in &doc.cues { // A comma in the Name field would shift the columns; the other fields are fixed text. - let name = c.speaker.as_deref().unwrap_or("").replace([',', '\n'], " "); + let name = c.speaker.as_deref().unwrap_or("").replace([',', '\n', '\r'], " "); out.push_str(&format!("Dialogue: 0,{},{},Default,{},0,0,0,,{}\n", format_time(c.start), format_time(c.end), name, escape(&c.text))); } out diff --git a/crates/captions/tests/it/ass.rs b/crates/captions/tests/it/ass.rs index de5d43167..fb6d4e01e 100644 --- a/crates/captions/tests/it/ass.rs +++ b/crates/captions/tests/it/ass.rs @@ -99,7 +99,8 @@ fn we_read_what_ffmpeg_writes() { fn ffmpeg_reads_what_we_write() { let Some(ff) = craft_media_testkit::ffmpeg() else { return }; let dir = craft_media_testkit::fixture_subdir("captions_ass"); - let doc = parse(SRT.as_bytes(), Format::Srt).expect("parse srt"); + let mut doc = parse(SRT.as_bytes(), Format::Srt).expect("parse srt"); + doc.cues.push(Cue { start: cs(400_000), end: cs(400_100), text: r"C:\New\notes\hello and \N \n \h".into(), ..Default::default() }); let (ass, srt) = (dir.join("ours.ass"), dir.join("back.srt")); std::fs::write(&ass, write(&doc, Format::Ass, WriteOptions::default())).expect("write"); ffmpeg_run(&ff, &["-i", ass.to_str().expect("path"), srt.to_str().expect("path")]); @@ -111,3 +112,31 @@ fn ffmpeg_reads_what_we_write() { assert_eq!(t.text, d.text, "text of the cue at {:?}", d.start); } } + +#[test] +fn literal_backslashes_survive_ass_round_trip() { + let text = r"C:\New\notes\hello and \N \n \h"; + let doc = Document { cues: vec![Cue { start: cs(100), end: cs(200), text: text.into(), ..Default::default() }], ..Default::default() }; + let bytes = write(&doc, Format::Ass, WriteOptions::default()); + assert_eq!(parse(&bytes, Format::Ass).unwrap().cues[0].text, text); +} + +#[test] +fn carriage_returns_cannot_create_dialogue_records() { + let doc = Document { + cues: vec![Cue { + start: cs(100), + end: cs(200), + text: "first\rsecond\r\nthird".into(), + speaker: Some("Ann\rOther".into()), + ..Default::default() + }], + ..Default::default() + }; + let bytes = write(&doc, Format::Ass, WriteOptions::default()); + assert!(!bytes.contains(&b'\r')); + let back = parse(&bytes, Format::Ass).unwrap(); + assert_eq!(back.cues.len(), 1); + assert_eq!(back.cues[0].text, "first\nsecond\nthird"); + assert_eq!(back.cues[0].speaker.as_deref(), Some("Ann Other")); +} From f31e878c6e923748aebae75869e9dd88874d05d3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:08:57 +0000 Subject: [PATCH 044/150] exr: share ZIP chunk compression --- crates/exr/src/lib.rs | 3 + crates/exr/src/zlib.rs | 796 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 799 insertions(+) create mode 100644 crates/exr/src/zlib.rs diff --git a/crates/exr/src/lib.rs b/crates/exr/src/lib.rs index baaf3d3f1..f1550f8de 100644 --- a/crates/exr/src/lib.rs +++ b/crates/exr/src/lib.rs @@ -18,6 +18,9 @@ //! - [`stream`]: one flat scan-line part read block by block, for pictures larger than memory. //! - [`cryptomatte_samples`]: a file's Cryptomatte layers and their per-pixel (ID, coverage) ranks. +/// RFC 1950/1951 compressor for ZIP-compressed EXR chunks. +pub mod zlib; + pub mod channels; pub mod cryptomatte; #[cfg(feature = "decode")] diff --git a/crates/exr/src/zlib.rs b/crates/exr/src/zlib.rs new file mode 100644 index 000000000..8959a7e89 --- /dev/null +++ b/crates/exr/src/zlib.rs @@ -0,0 +1,796 @@ +//! A small DEFLATE encoder (RFC 1951) in a zlib wrapper (RFC 1950), written from the RFCs for the +//! ZIP-compressed OpenEXR chunks. +//! +//! Greedy LZ77 over a 32 KB window (hash chains of bounded length), then each block of tokens is +//! written as whichever of stored, fixed-Huffman or dynamic-Huffman is smallest. Huffman code +//! lengths are limited (15 bits, 7 for the code-length code) by halving the symbol frequencies +//! until the tree fits. Every code has at least two symbols, so all codes are complete. The +//! output favours simplicity and correctness over ratio; worst-case expansion is the stored-block +//! overhead (5 bytes per 64 KB). + +use std::cmp::Reverse; +use std::collections::BinaryHeap; + +const WINDOW: usize = 32 * 1024; +const MIN_MATCH: usize = 3; +const MAX_MATCH: usize = 258; +/// Hash chain candidates tried per position. +const MAX_CHAIN: usize = 48; +/// Tokens per block (a new set of Huffman codes after this many). +const BLOCK_TOKENS: usize = 1 << 15; +const MAX_STORED: usize = 65_535; + +const LEN_BASE: [u16; 29] = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258]; +const LEN_EXTRA: [u8; 29] = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0]; +const DIST_BASE: [u16; 30] = + [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577]; +const DIST_EXTRA: [u8; 30] = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13]; +/// Order in which the code-length code's lengths are sent. +const CL_ORDER: [usize; 19] = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]; + +/// `data` compressed as a zlib stream (CMF 0x78: deflate, 32 KB window). `tables` are the match +/// finder's hash tables, kept by the caller across the chunks of a picture; the output does not +/// depend on what they held before. +pub fn zlib(data: &[u8], tables: &mut Tables) -> Vec { + let mut w = Bits { out: Vec::with_capacity(data.len() / 2 + 64), acc: 0, n: 0 }; + w.out.extend_from_slice(&[0x78, 0x9C]); // (0x78 << 8 | 0x9C) % 31 == 0 + deflate(data, &mut w, tables); + w.align(); + w.out.extend_from_slice(&adler32(data).to_be_bytes()); + w.out +} + +/// Adler-32 (RFC 1950 §9); 5552 bytes is the longest run that cannot overflow the sums. +fn adler32(data: &[u8]) -> u32 { + let (mut a, mut b) = (1_u32, 0_u32); + for c in data.chunks(5552) { + for &x in c { + a += u32::from(x); + b += a; + } + a %= 65_521; + b %= 65_521; + } + (b << 16) | a +} + +/// LSB-first bit writer. +struct Bits { + out: Vec, + acc: u64, + n: u32, +} + +impl Bits { + /// The low `n` bits of `v` (n ≤ 32). + fn put(&mut self, v: u32, n: u32) { + let mask = (1_u64 << n) - 1; + self.acc |= (u64::from(v) & mask) << self.n; + self.n += n; + while self.n >= 8 { + self.out.push(self.acc as u8); + self.acc >>= 8; + self.n -= 8; + } + } + + fn align(&mut self) { + if self.n > 0 { + self.out.push(self.acc as u8); + } + self.acc = 0; + self.n = 0; + } +} + +#[derive(Clone, Copy)] +enum Tok { + Lit(u8), + Match { len: u16, dist: u16 }, +} + +/// Index into [`LEN_BASE`] of a match length (3..=258). +fn len_code(len: u16) -> usize { + LEN_BASE.partition_point(|&b| b <= len).saturating_sub(1) +} + +/// Index into [`DIST_BASE`] of a distance (1..=32768). +fn dist_code(dist: u16) -> usize { + DIST_BASE.partition_point(|&b| b <= dist).saturating_sub(1) +} + +/// The match finder's hash heads and chains, reused from one input to the next. +/// +/// A slot holds `base + p + 1` for position `p` of the input that wrote it; anything at or below +/// the current input's `base` was left by an earlier input and reads as empty. So the tables are +/// never cleared between inputs: an OpenEXR ZIPS frame is one input per scan line, and zeroing +/// 256 KiB of tables for each 23 KB UHD line cost more memory traffic than the data itself. +#[derive(Default)] +pub struct Tables { + head: Vec, + prev: Vec, + /// Bytes compressed through these tables since they were last cleared. + base: u32, +} + +/// LZ77 match finder: hash heads and chains over the window. +struct Matcher<'a> { + data: &'a [u8], + head: &'a mut [u32], + prev: &'a mut [u32], + bits: u32, + /// `prev` slots used by this input, minus one (a power of two minus one). + mask: usize, + base: u32, +} + +impl<'a> Matcher<'a> { + /// A matcher over `data` (shorter than `u32::MAX` bytes), or None when it is not. + fn new(data: &'a [u8], t: &'a mut Tables) -> Option { + // hash width and chain span follow the input's size, as they did when every input had + // tables of its own (the output depends on them), so short chunks stay cheap to hash + let span = data.len().clamp(256, WINDOW).next_power_of_two(); + let bits = span.trailing_zeros().clamp(8, 15); + let n = u32::try_from(data.len()).ok().filter(|&n| n < u32::MAX)?; + if t.base.checked_add(n).and_then(|b| b.checked_add(1)).is_none() { + // positions would no longer fit after `base`: start over from empty tables + t.head.fill(0); + t.prev.fill(0); + t.base = 0; + } + // grown slots are 0, which is never above `base`, so they read as empty + if t.head.len() < 1 << bits { + t.head.resize(1 << bits, 0); + } + if t.prev.len() < span { + t.prev.resize(span, 0); + } + let base = t.base; + t.base += n; + let (head, prev) = (t.head.get_mut(..1 << bits)?, t.prev.get_mut(..span)?); + Some(Matcher { data, head, prev, bits, mask: span - 1, base }) + } + + /// Position `p` of this input from a stored slot, or None for an empty or earlier input's slot. + fn pos(&self, slot: u32) -> Option { + slot.checked_sub(self.base)?.checked_sub(1).map(|p| p as usize) + } + + fn hash(&self, p: usize) -> Option { + let b = self.data.get(p..p + MIN_MATCH)?; + let v = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]); + Some((v.wrapping_mul(0x9E37_79B1) >> (32 - self.bits)) as usize) + } + + /// Record position `p` (stored as `p + 1`; 0 = empty). + fn insert(&mut self, p: usize) { + let Some(h) = self.hash(p) else { return }; + let (Some(head), Ok(p32)) = (self.head.get(h).copied(), u32::try_from(p)) else { return }; + if let Some(slot) = self.prev.get_mut(p & self.mask) { + *slot = head; + } + if let Some(slot) = self.head.get_mut(h) { + // `new` reserved `base + len + 1` within u32 + *slot = self.base + p32 + 1; + } + } + + /// The longest earlier match at `p`: (length, distance), length 0 if none. + fn longest(&self, p: usize) -> (usize, usize) { + let Some(h) = self.hash(p) else { return (0, 0) }; + let max = self.data.len().saturating_sub(p).min(MAX_MATCH); + let Some(here) = self.data.get(p..p + max) else { return (0, 0) }; + let (mut best, mut best_dist) = (0, 0); + let mut cand = self.head.get(h).and_then(|&s| self.pos(s)); + for _ in 0..MAX_CHAIN { + let Some(c) = cand else { break }; + if c >= p || p - c > WINDOW { + break; + } + // a candidate that differs at `best` cannot be longer than `best`: skip comparing it + if best == 0 || self.data.get(c + best) == here.get(best) { + let l = self.data.get(c..).unwrap_or_default().iter().zip(here).take_while(|(a, b)| a == b).count(); + if l > best { + (best, best_dist) = (l, p - c); + if l == max { + break; + } + } + } + let Some(next) = self.prev.get(c & self.mask).and_then(|&s| self.pos(s)) else { break }; + // chains only go back; anything else is a stale slot + if next >= c { + break; + } + cand = Some(next); + } + (best, best_dist) + } +} + +/// The deflate blocks of `data`, the last one final. +fn deflate(data: &[u8], w: &mut Bits, tables: &mut Tables) { + if data.is_empty() { + // a final fixed-Huffman block holding only end-of-block (code 0000000) + w.put(1, 1); + w.put(1, 2); + w.put(0, 7); + return; + } + // positions are kept in u32; longer inputs (never an OpenEXR chunk) go out stored + let mut m = Matcher::new(data, tables); + let mut toks = Vec::with_capacity(BLOCK_TOKENS.min(data.len()) + 1); + let (mut start, mut i) = (0, 0); + while i < data.len() { + let (len, dist) = m.as_ref().map_or((0, 0), |m| m.longest(i)); + let step = if len >= MIN_MATCH { + toks.push(Tok::Match { len: len as u16, dist: dist as u16 }); + len + } else { + toks.push(Tok::Lit(data[i])); + 1 + }; + if let Some(m) = m.as_mut() { + for p in i..i + step { + m.insert(p); + } + } + i += step; + if toks.len() >= BLOCK_TOKENS || i == data.len() { + block(w, &toks, &data[start..i], i == data.len()); + toks.clear(); + start = i; + } + } +} + +/// Bits the tokens take with the given code lengths (EOB included). +fn data_bits(lf: &[u32], df: &[u32], ll: &[u8], dl: &[u8]) -> u64 { + let mut n = 0_u64; + for (s, (&f, &l)) in lf.iter().zip(ll).enumerate() { + let extra = s.checked_sub(257).and_then(|k| LEN_EXTRA.get(k)).copied().unwrap_or(0); + n += u64::from(f) * u64::from(l + extra); + } + for (d, (&f, &l)) in df.iter().zip(dl).enumerate() { + n += u64::from(f) * u64::from(l + DIST_EXTRA.get(d).copied().unwrap_or(0)); + } + n +} + +/// One block (split into several stored blocks when stored is smallest and `raw` is long). +fn block(w: &mut Bits, toks: &[Tok], raw: &[u8], last: bool) { + let mut lf = [0_u32; 286]; + let mut df = [0_u32; 30]; + for t in toks { + match *t { + Tok::Lit(b) => lf[b as usize] += 1, + Tok::Match { len, dist } => { + lf[257 + len_code(len)] += 1; + df[dist_code(dist)] += 1; + } + } + } + lf[256] += 1; + + // dynamic codes and their header + let ll = lengths(&lf, 15); + let dl = lengths(&df, 15); + let hlit = ll.iter().rposition(|&l| l > 0).map_or(257, |p| (p + 1).max(257)); + let hdist = dl.iter().rposition(|&l| l > 0).map_or(1, |p| p + 1); + let seq = rle_lengths(&[&ll[..hlit], &dl[..hdist]].concat()); + let mut cf = [0_u32; 19]; + for &(s, _) in &seq { + cf[s as usize] += 1; + } + let cl = lengths(&cf, 7); + let hclen = CL_ORDER.iter().rposition(|&s| cl[s] > 0).map_or(4, |p| (p + 1).max(4)); + let seq_bits: u64 = + seq.iter().map(|&(s, _)| u64::from(cl[s as usize]) + [2_u64, 3, 7].get((s as usize).wrapping_sub(16)).copied().unwrap_or(0)).sum(); + let dyn_bits = 3 + 14 + 3 * hclen as u64 + seq_bits + data_bits(&lf, &df, &ll, &dl); + + let (fll, fdl) = fixed_lengths(); + let fixed_bits = 3 + data_bits(&lf, &df, &fll, &fdl); + // per stored piece: header, at most 7 padding bits, LEN and NLEN + let pieces = raw.len().div_ceil(MAX_STORED).max(1) as u64; + let stored_bits = pieces * (3 + 7 + 32) + 8 * raw.len() as u64; + + if stored_bits <= fixed_bits.min(dyn_bits) { + let n = raw.chunks(MAX_STORED).count(); + for (k, piece) in raw.chunks(MAX_STORED).enumerate() { + w.put(u32::from(last && k + 1 == n), 1); + w.put(0, 2); + w.align(); + let len = piece.len() as u16; + w.out.extend_from_slice(&len.to_le_bytes()); + w.out.extend_from_slice(&(!len).to_le_bytes()); + w.out.extend_from_slice(piece); + } + return; + } + w.put(u32::from(last), 1); + if fixed_bits <= dyn_bits { + w.put(1, 2); + write_tokens(w, toks, &fll, &fdl); + return; + } + w.put(2, 2); + w.put((hlit - 257) as u32, 5); + w.put((hdist - 1) as u32, 5); + w.put((hclen - 4) as u32, 4); + for &s in &CL_ORDER[..hclen] { + w.put(u32::from(cl[s]), 3); + } + let cc = codes(&cl); + for &(s, x) in &seq { + w.put(u32::from(cc[s as usize]), u32::from(cl[s as usize])); + match s { + 16 => w.put(u32::from(x), 2), + 17 => w.put(u32::from(x), 3), + 18 => w.put(u32::from(x), 7), + _ => {} + } + } + write_tokens(w, toks, &ll, &dl); +} + +/// The fixed Huffman code lengths (RFC 1951 §3.2.6). +fn fixed_lengths() -> ([u8; 288], [u8; 30]) { + let mut ll = [8_u8; 288]; + ll[144..256].fill(9); + ll[256..280].fill(7); + (ll, [5; 30]) +} + +fn write_tokens(w: &mut Bits, toks: &[Tok], ll: &[u8], dl: &[u8]) { + let lc = codes(ll); + let dc = codes(dl); + let sym = |w: &mut Bits, s: usize| w.put(u32::from(lc[s]), u32::from(ll[s])); + for t in toks { + match *t { + Tok::Lit(b) => sym(w, b as usize), + Tok::Match { len, dist } => { + let k = len_code(len); + sym(w, 257 + k); + w.put(u32::from(len - LEN_BASE[k]), u32::from(LEN_EXTRA[k])); + let d = dist_code(dist); + w.put(u32::from(dc[d]), u32::from(dl[d])); + w.put(u32::from(dist - DIST_BASE[d]), u32::from(DIST_EXTRA[d])); + } + } + } + sym(w, 256); +} + +/// Canonical Huffman codes for `lens`, bit-reversed for the LSB-first writer. +fn codes(lens: &[u8]) -> Vec { + let mut count = [0_u32; 16]; + for &l in lens { + if (1..16).contains(&l) { + count[l as usize] += 1; + } + } + let mut next = [0_u32; 16]; + let mut code = 0; + for bits in 1..16 { + code = (code + count[bits - 1]) << 1; + next[bits] = code; + } + lens.iter() + .map(|&l| { + if !(1..16).contains(&l) { + return 0; + } + let c = next[l as usize]; + next[l as usize] += 1; + (c.reverse_bits() >> (32 - u32::from(l))) as u16 + }) + .collect() +} + +/// Code lengths for `freq`, none longer than `limit`; at least two symbols always get a code. +fn lengths(freq: &[u32], limit: u8) -> Vec { + let mut f: Vec = freq.iter().map(|&x| u64::from(x)).collect(); + let mut used = f.iter().filter(|&&x| x > 0).count(); + for x in f.iter_mut() { + if used >= 2 { + break; + } + if *x == 0 { + *x = 1; + used += 1; + } + } + // halving converges: once every used frequency is 1 the tree is balanced (depth ≤ 9 here) + for _ in 0..66 { + let l = huffman(&f); + if l.iter().all(|&d| d <= limit) { + return l; + } + for x in f.iter_mut().filter(|x| **x > 0) { + *x = (*x >> 1).max(1); + } + } + huffman(&f) +} + +/// Huffman tree depths of the symbols with non-zero frequency (0 for the rest). +fn huffman(f: &[u64]) -> Vec { + let n = f.len(); + let mut heap: BinaryHeap> = f.iter().enumerate().filter(|(_, w)| **w > 0).map(|(i, &w)| Reverse((w, i))).collect(); + let mut parent = vec![usize::MAX; 2 * n]; + let mut next = n; + while heap.len() > 1 { + let (Some(Reverse((wa, a))), Some(Reverse((wb, b)))) = (heap.pop(), heap.pop()) else { break }; + parent[a] = next; + parent[b] = next; + heap.push(Reverse((wa + wb, next))); + next += 1; + } + // parents always come after their children, so walk down from the root + let mut depth = vec![0_u32; next]; + for i in (0..next).rev() { + if let Some(&p) = parent.get(i).filter(|&&p| p < next) { + depth[i] = depth[p].saturating_add(1); + } + } + depth[..n].iter().map(|&d| d.min(u32::from(u8::MAX)) as u8).collect() +} + +/// Code lengths run-length coded with the code-length alphabet: (symbol, extra-bits value). +fn rle_lengths(lens: &[u8]) -> Vec<(u8, u8)> { + let mut out = Vec::new(); + let mut i = 0; + while i < lens.len() { + let l = lens[i]; + let run = lens[i..].iter().take_while(|&&x| x == l).count(); + let mut r = run; + if l == 0 { + while r >= 11 { + let k = r.min(138); + out.push((18, (k - 11) as u8)); + r -= k; + } + if r >= 3 { + out.push((17, (r - 3) as u8)); + r = 0; + } + } else { + out.push((l, 0)); + r -= 1; + while r >= 3 { + let k = r.min(6); + out.push((16, (k - 3) as u8)); + r -= k; + } + } + out.extend(std::iter::repeat_n((l, 0), r)); + i += run; + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A minimal inflater (RFC 1951), only to check the encoder; returns None on any error. + pub(crate) fn inflate(z: &[u8]) -> Option> { + let (cmf, flg) = (*z.first()?, *z.get(1)?); + if cmf & 0x0F != 8 || (u16::from(cmf) << 8 | u16::from(flg)) % 31 != 0 || flg & 0x20 != 0 { + return None; + } + let mut pos = 16_usize; // bit position + let bit = |pos: &mut usize| -> Option { + let b = (*z.get(*pos / 8)? >> (*pos % 8)) & 1; + *pos += 1; + Some(u32::from(b)) + }; + let bits = |pos: &mut usize, n: u32| -> Option { + let mut v = 0; + for k in 0..n { + v |= bit(pos)? << k; + } + Some(v) + }; + // decode one symbol with canonical lengths (slow but simple) + let decode = |pos: &mut usize, lens: &[u8]| -> Option { + let (mut code, mut first, mut index) = (0_i32, 0_i32, 0_i32); + let mut count = [0_i32; 16]; + for &l in lens { + count[l as usize] += 1; + } + let mut sorted: Vec = (0..lens.len()).filter(|&s| lens[s] > 0).collect(); + sorted.sort_by_key(|&s| (lens[s], s)); + for len in 1..16 { + code |= bit(pos)? as i32; + let c = count[len]; + if code - c < first { + return sorted.get((index + code - first) as usize).copied(); + } + index += c; + first = (first + c) << 1; + code <<= 1; + } + None + }; + let mut out = Vec::new(); + loop { + let last = bit(&mut pos)?; + match bits(&mut pos, 2)? { + 0 => { + pos = pos.div_ceil(8) * 8; + let at = pos / 8; + let len = u16::from_le_bytes([*z.get(at)?, *z.get(at + 1)?]); + let nlen = u16::from_le_bytes([*z.get(at + 2)?, *z.get(at + 3)?]); + if len != !nlen { + return None; + } + out.extend_from_slice(z.get(at + 4..at + 4 + len as usize)?); + pos = (at + 4 + len as usize) * 8; + } + t @ (1 | 2) => { + let (ll, dl) = if t == 1 { + let (a, b) = fixed_lengths(); + (a.to_vec(), b.to_vec()) + } else { + let hlit = bits(&mut pos, 5)? as usize + 257; + let hdist = bits(&mut pos, 5)? as usize + 1; + let hclen = bits(&mut pos, 4)? as usize + 4; + let mut cl = [0_u8; 19]; + for &s in &CL_ORDER[..hclen] { + cl[s] = bits(&mut pos, 3)? as u8; + } + let mut lens = Vec::new(); + while lens.len() < hlit + hdist { + match decode(&mut pos, &cl)? { + s @ 0..=15 => lens.push(s as u8), + 16 => { + let p = *lens.last()?; + lens.extend(std::iter::repeat_n(p, 3 + bits(&mut pos, 2)? as usize)); + } + 17 => lens.extend(std::iter::repeat_n(0, 3 + bits(&mut pos, 3)? as usize)), + _ => lens.extend(std::iter::repeat_n(0, 11 + bits(&mut pos, 7)? as usize)), + } + } + if lens.len() != hlit + hdist { + return None; + } + (lens[..hlit].to_vec(), lens[hlit..].to_vec()) + }; + loop { + let s = decode(&mut pos, &ll)?; + if s < 256 { + out.push(s as u8); + } else if s == 256 { + break; + } else { + let k = s - 257; + let len = *LEN_BASE.get(k)? as usize + bits(&mut pos, u32::from(LEN_EXTRA[k]))? as usize; + let d = decode(&mut pos, &dl)?; + let dist = *DIST_BASE.get(d)? as usize + bits(&mut pos, u32::from(DIST_EXTRA[d]))? as usize; + let from = out.len().checked_sub(dist)?; + for j in 0..len { + out.push(out[from + j]); + } + } + } + } + _ => return None, + } + if last == 1 { + break; + } + } + let at = pos.div_ceil(8); + let sum = u32::from_be_bytes(z.get(at..at + 4)?.try_into().ok()?); + (sum == adler32(&out) && at + 4 == z.len()).then_some(out) + } + + /// A deterministic pseudo-random byte stream. + fn noise(n: usize, seed: u32) -> Vec { + let mut s = seed | 1; + (0..n) + .map(|_| { + s ^= s << 13; + s ^= s >> 17; + s ^= s << 5; + s as u8 + }) + .collect() + } + + #[test] + fn round_trips() { + let mut cases: Vec> = vec![vec![], vec![7], vec![1, 2], vec![0; 1], vec![0; 300_000], b"abcabcabcabcabcabd".to_vec()]; + cases.push(noise(100_000, 9)); // incompressible: stored blocks + cases.push(noise(70_000, 3).iter().map(|b| b % 4).collect()); // few symbols: dynamic + cases.push((0..200_000_u32).map(|i| ((i / 7) % 251) as u8).collect()); // long repeats + // text-like: a small vocabulary repeated at varying distances + let words: [&[u8]; 6] = [b"red ", b"green ", b"blue ", b"alpha ", b"half ", b"linear "]; + cases.push(noise(40_000, 5).iter().flat_map(|b| words[*b as usize % 6].iter().copied()).collect()); + // skewed frequencies: Huffman lengths beyond 15 before limiting + let mut fib = Vec::new(); + let (mut a, mut b) = (1_usize, 1_usize); + for s in 0..25_u8 { + fib.extend(std::iter::repeat_n(s.wrapping_mul(37), a.min(200_000))); + (a, b) = (b, a + b); + } + let r = noise(fib.len() * 4, 11); + for i in (1..fib.len()).rev() { + let j = u32::from_le_bytes(r[i * 4..i * 4 + 4].try_into().unwrap()) as usize % (i + 1); + fib.swap(i, j); + } + cases.push(fib); + for (k, data) in cases.iter().enumerate() { + let z = zlib(data, &mut Tables::default()); + assert_eq!(inflate(&z).as_deref(), Some(data.as_slice()), "case {k}"); + // worst case: stored overhead only + assert!(z.len() <= data.len() + (data.len().div_ceil(BLOCK_TOKENS) + 1) * 5 + 6, "case {k}: {} > {}", z.len(), data.len()); + } + // repetitive data actually compresses + assert!(zlib(&cases[4], &mut Tables::default()).len() < 2_000); + assert!(zlib(&cases[8], &mut Tables::default()).len() < 20_000); + } + + #[test] + fn length_limits_hold() { + // Fibonacci frequencies give the deepest possible Huffman tree + let mut f = vec![0_u32; 286]; + let (mut a, mut b) = (1_u32, 1_u32); + for x in f.iter_mut().take(30) { + *x = a; + (a, b) = (b, a.saturating_add(b)); + } + let l = lengths(&f, 15); + assert!(l.iter().all(|&d| d <= 15)); + // a complete code: Kraft sum exactly 1 + let kraft: f64 = l.iter().filter(|&&d| d > 0).map(|&d| 0.5_f64.powi(i32::from(d))).sum(); + assert!((kraft - 1.0).abs() < 1e-12); + // one used symbol still gets a two-symbol code + let l = lengths(&[0, 5, 0], 7); + assert_eq!(l.iter().filter(|&&d| d == 1).count(), 2); + } + + #[test] + fn adler_matches_rfc_example() { + // the classic test vector for "Wikipedia" + assert_eq!(adler32(b"Wikipedia"), 0x11E6_0398); + assert_eq!(adler32(&[]), 1); + } + + /// The match finder before #4659: tables of its own per input, every chain candidate compared + /// from its first byte. + struct OldMatcher<'a> { + data: &'a [u8], + head: Vec, + prev: Vec, + bits: u32, + } + + impl<'a> OldMatcher<'a> { + /// Tables of its own, sized to `data`. + fn new(data: &'a [u8]) -> Self { + let span = data.len().clamp(256, WINDOW).next_power_of_two(); + let bits = span.trailing_zeros().clamp(8, 15); + OldMatcher { data, head: vec![0; 1 << bits], prev: vec![0; span], bits } + } + + /// The hash of the three bytes at `p`, None near the end. + fn hash(&self, p: usize) -> Option { + let b = self.data.get(p..p + MIN_MATCH)?; + let v = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]); + Some((v.wrapping_mul(0x9E37_79B1) >> (32 - self.bits)) as usize) + } + + /// Record position `p` (stored as `p + 1`; 0 = empty). + fn insert(&mut self, p: usize) { + let Some(h) = self.hash(p) else { return }; + let mask = self.prev.len() - 1; + self.prev[p & mask] = self.head[h]; + self.head[h] = p as u32 + 1; + } + + /// The longest earlier match at `p`: (length, distance), length 0 if none. + fn longest(&self, p: usize) -> (usize, usize) { + let Some(h) = self.hash(p) else { return (0, 0) }; + let max = (self.data.len() - p).min(MAX_MATCH); + let here = &self.data[p..p + max]; + let mask = self.prev.len() - 1; + let (mut best, mut best_dist) = (0, 0); + let mut cand = self.head[h] as usize; + for _ in 0..MAX_CHAIN { + let Some(c) = cand.checked_sub(1) else { break }; + if c >= p || p - c > WINDOW { + break; + } + let l = self.data[c..].iter().zip(here).take_while(|(a, b)| a == b).count(); + if l > best { + (best, best_dist) = (l, p - c); + if l == max { + break; + } + } + let next = self.prev[c & mask] as usize; + if next == 0 || next > c { + break; + } + cand = next; + } + (best, best_dist) + } + } + + /// Inputs shaped like OpenEXR chunks (predicted half-float scan lines, single and 16-line) + /// and the awkward cases: empty, tiny, incompressible, long runs, few symbols. + fn chunk_cases() -> Vec> { + let line: Vec = (0..23_040_u32).map(|i| (128 + ((i / 3) % 7) as i32 - 3 + (i % 5 == 0) as i32) as u8).collect(); + let mut cases = vec![line.clone(), line.iter().rev().copied().collect(), vec![], vec![9], b"abcabcabcabd".to_vec()]; + cases.push(line.iter().cycle().take(16 * 23_040).copied().collect()); // ZIP: 16 lines, longer than the window + cases.push(noise(23_040, 4)); + cases.push(noise(70_000, 3).iter().map(|b| b % 4).collect()); + cases.push(vec![0; 300_000]); + cases.push(line.clone()); + cases.push(noise(300, 7)); + cases + } + + /// The matcher's greedy parse of `data`: what `deflate` codes. + fn parse (usize, usize), G: FnMut(usize)>(data: &[u8], mut longest: F, mut insert: G) -> Vec<(usize, usize)> { + let (mut out, mut i) = (Vec::new(), 0); + while i < data.len() { + let (len, dist) = longest(i); + let step = if len >= MIN_MATCH { len } else { 1 }; + out.push(if len >= MIN_MATCH { (len, dist) } else { (0, 0) }); + for p in i..i + step { + insert(p); + } + i += step; + } + out + } + + /// Shared tables and the candidate skip find exactly the matches the previous matcher found. + #[test] + fn shared_tables_find_the_matches_the_old_per_chunk_tables_found() { + let mut tables = Tables::default(); + for (k, data) in chunk_cases().iter().enumerate() { + let old = std::cell::RefCell::new(OldMatcher::new(data)); + let want = parse(data, |p| old.borrow().longest(p), |p| old.borrow_mut().insert(p)); + let new = std::cell::RefCell::new(Matcher::new(data, &mut tables).expect("fits")); + let got = parse(data, |p| new.borrow().longest(p), |p| new.borrow_mut().insert(p)); + assert!(got == want, "chunk {k}: the parse differs"); + } + } + + /// Shared tables compress to the bytes fresh ones give, also when positions run out and they start over. + #[test] + fn shared_tables_give_the_bytes_of_fresh_ones() { + let mut shared = Tables::default(); + for (k, data) in chunk_cases().iter().enumerate() { + let z = zlib(data, &mut shared); + assert!(z == zlib(data, &mut Tables::default()), "chunk {k}"); + assert_eq!(inflate(&z).as_deref(), Some(data.as_slice()), "chunk {k}"); + } + // positions about to run past u32: the tables start over and the bytes stay the same + for gap in [0_u32, 1, 2, 23_040, 23_041, 1 << 20] { + shared.base = u32::MAX - gap; + for (k, data) in chunk_cases().iter().enumerate() { + assert!(zlib(data, &mut shared) == zlib(data, &mut Tables::default()), "gap {gap}, chunk {k}"); + } + } + } + + /// Later chunks use the same tables instead of allocating new ones. + #[test] + fn the_tables_are_reused_across_chunks() { + let mut tables = Tables::default(); + let line = &chunk_cases()[0]; + zlib(line, &mut tables); + let (head, prev) = (tables.head.as_ptr(), tables.prev.as_ptr()); + for _ in 0..4 { + zlib(line, &mut tables); + assert!(tables.head.as_ptr() == head && tables.prev.as_ptr() == prev, "a chunk allocated new tables"); + } + assert_eq!(tables.base as usize, 5 * line.len()); + } +} From d55335423a9719102b2c2f3dfa2f6cbf05e915d3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:11:32 +0000 Subject: [PATCH 045/150] exr: share scanline RGBA writer --- crates/exr/src/lib.rs | 2 + crates/exr/src/rgba8.rs | 235 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 crates/exr/src/rgba8.rs diff --git a/crates/exr/src/lib.rs b/crates/exr/src/lib.rs index f1550f8de..78afd427e 100644 --- a/crates/exr/src/lib.rs +++ b/crates/exr/src/lib.rs @@ -18,6 +18,8 @@ //! - [`stream`]: one flat scan-line part read block by block, for pictures larger than memory. //! - [`cryptomatte_samples`]: a file's Cryptomatte layers and their per-pixel (ID, coverage) ranks. +/// Scanline OpenEXR writer using a caller-supplied linearization table. +pub mod rgba8; /// RFC 1950/1951 compressor for ZIP-compressed EXR chunks. pub mod zlib; diff --git a/crates/exr/src/rgba8.rs b/crates/exr/src/rgba8.rs new file mode 100644 index 000000000..0b26ab8c9 --- /dev/null +++ b/crates/exr/src/rgba8.rs @@ -0,0 +1,235 @@ +//! Single-part scanline OpenEXR output from RGBA8 pixels. + +/// Scanline chunk compression. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Compression { + /// Uncompressed. + None, + /// OpenEXR RLE. + Rle, + /// zlib per scanline. + Zips, + /// zlib per 16 scanlines. + Zip, +} +impl Compression { + fn code(self) -> u8 { + match self { + Self::None => 0, + Self::Rle => 1, + Self::Zips => 2, + Self::Zip => 3, + } + } + fn lines(self) -> usize { + if self == Self::Zip { 16 } else { 1 } + } +} +/// Why a frame cannot be encoded. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Error { + /// Empty, overflowing or mismatched RGBA dimensions. + FrameSize, + /// Dimensions exceed the OpenEXR signed-size fields. + TooLarge, +} +fn checked_len(rgba: &[u8], w: u32, h: u32) -> Result { + let n = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(4)).ok_or(Error::FrameSize)?; + if w == 0 || h == 0 || rgba.len() != n { + return Err(Error::FrameSize); + } + Ok(n) +} + +/// IEEE 754 binary16 bits of `v`, rounded to nearest even (overflow → infinity, NaN → quiet NaN). +pub fn half(v: f32) -> u16 { + let b = v.to_bits(); + let sign = ((b >> 16) & 0x8000) as u16; + let exp = ((b >> 23) & 0xFF) as i32; + let man = b & 0x7F_FFFF; + if exp == 0xFF { + return sign | 0x7C00 | if man != 0 { 0x200 } else { 0 }; + } + // rounds `m >> shift` to nearest, ties to even (`shift` is 1..=24) + let round = |m: u32, shift: u32| { + let (q, rem, mid) = (m >> shift, m & ((1 << shift) - 1), 1 << (shift - 1)); + if rem > mid || (rem == mid && q & 1 == 1) { q + 1 } else { q } + }; + let e = exp - 127 + 15; + if e >= 0x1F { + sign | 0x7C00 + } else if e <= 0 { + if e < -10 { + return sign; + } + // subnormal half: the 24-bit significand scaled by 2^(e - 14); rounding up may carry + // into the smallest normal, which is the right encoding + sign | round(man | 0x80_0000, (14 - e) as u32) as u16 + } else { + // a carry out of the mantissa bumps the exponent (up to infinity), as it should + sign | round(((e as u32) << 23) | man, 13) as u16 + } +} + +/// An OpenEXR file: half-float scene-linear Rec.709 RGB, or RGBA with `alpha` (premultiplied, as +/// EXR defines alpha; the frame's straight alpha is multiplied in after linearisation). +pub fn write_rgba8(rgba: &[u8], w: u32, h: u32, alpha: bool, compression: Compression, linear: &[f32; 256]) -> Result, Error> { + let n = checked_len(rgba, w, h)?; + let too_large = || Error::TooLarge; + let (Ok(x_max), Ok(y_max)) = (i32::try_from(w - 1), i32::try_from(h - 1)) else { + return Err(too_large()); + }; + // channels in the alphabetical order the file layout requires + let names: &[u8] = if alpha { b"ABGR" } else { b"BGR" }; + let line = (w as usize).checked_mul(names.len() * 2).ok_or_else(|| Error::FrameSize)?; + // a chunk's byte count is a signed 32-bit field + let lines = compression.lines(); + if line.checked_mul(lines).and_then(|c| i32::try_from(c).ok()).is_none() { + return Err(too_large()); + } + + let mut hd = Vec::with_capacity(512); + hd.extend_from_slice(&[0x76, 0x2F, 0x31, 0x01]); // magic number 20000630 + hd.extend_from_slice(&[2, 0, 0, 0]); // version 2, single-part scan-line file, short names + let attr = |hd: &mut Vec, name: &str, ty: &str, value: &[u8]| { + for s in [name, ty] { + hd.extend_from_slice(s.as_bytes()); + hd.push(0); + } + hd.extend_from_slice(&(value.len() as i32).to_le_bytes()); + hd.extend_from_slice(value); + }; + let le = |v: &[f32]| v.iter().flat_map(|f| f.to_le_bytes()).collect::>(); + let mut channels = Vec::new(); + for &c in names { + channels.extend_from_slice(&[c, 0]); + channels.extend_from_slice(&1_i32.to_le_bytes()); // pixel type HALF + channels.extend_from_slice(&[0, 0, 0, 0]); // pLinear 0, reserved + channels.extend_from_slice(&1_i32.to_le_bytes()); // x sampling + channels.extend_from_slice(&1_i32.to_le_bytes()); // y sampling + } + channels.push(0); + let window: Vec = [0, 0, x_max, y_max].iter().flat_map(|v| v.to_le_bytes()).collect(); + attr(&mut hd, "channels", "chlist", &channels); + attr(&mut hd, "chromaticities", "chromaticities", &le(&[0.64, 0.33, 0.30, 0.60, 0.15, 0.06, 0.3127, 0.3290])); + attr(&mut hd, "compression", "compression", &[compression.code()]); + attr(&mut hd, "dataWindow", "box2i", &window); + attr(&mut hd, "displayWindow", "box2i", &window); + attr(&mut hd, "lineOrder", "lineOrder", &[0]); // INCREASING_Y + attr(&mut hd, "pixelAspectRatio", "float", &le(&[1.0])); + attr(&mut hd, "screenWindowCenter", "v2f", &le(&[0.0, 0.0])); + attr(&mut hd, "screenWindowWidth", "float", &le(&[1.0])); + hd.push(0); // end of header + + // offset table (one entry per chunk), then the chunks: first y, data size, data + let chunks = (h as usize).div_ceil(lines); + let table = chunks.checked_mul(8); + let body = (h as usize).checked_mul(line + 8); + let size = table.zip(body).and_then(|(t, b)| hd.len().checked_add(t)?.checked_add(b)).ok_or_else(|| Error::FrameSize)?; + // the uncompressed size: compressed chunks only shrink it + let mut out = Vec::with_capacity(size); + out.extend_from_slice(&hd); + let table_at = out.len(); + out.resize(table_at + chunks * 8, 0); + let lin = linear; + let opaque: [u16; 256] = std::array::from_fn(|i| half(lin[i])); + let row = w as usize * 4; + let wn = w as usize; + let mut px = vec![0_u16; wn * names.len()]; + let mut raw = Vec::with_capacity(line * lines); + // the deflate hash tables, shared by the frame's chunks instead of zeroed for each one + let mut tables = crate::zlib::Tables::default(); + for (k, block) in rgba[..n].chunks(row * lines).enumerate() { + raw.clear(); + for src in block.chunks_exact(row) { + // planar per line: every pixel of the first channel, then the next channel … + for (x, &[r, g, b, a]) in src.as_chunks::<4>().0.iter().enumerate() { + let rgb = [b, g, r]; + if alpha { + let k = f32::from(a) / 255.0; + px[x] = half(k); + for (c, &v) in rgb.iter().enumerate() { + px[(c + 1) * wn + x] = if a == 255 { opaque[v as usize] } else { half(lin[v as usize] * k) }; + } + } else { + for (c, &v) in rgb.iter().enumerate() { + px[c * wn + x] = opaque[v as usize]; + } + } + } + raw.extend(px.iter().flat_map(|v| v.to_le_bytes())); + } + let packed = match compression { + Compression::None => None, + Compression::Rle => Some(rle(&predict(&raw))), + Compression::Zips | Compression::Zip => Some(crate::zlib::zlib(&predict(&raw), &mut tables)), + }; + // a chunk that does not shrink is stored as it is (readers compare the sizes) + let data = packed.as_deref().filter(|p| p.len() < raw.len()).unwrap_or(&raw); + let offset = (out.len() as u64).to_le_bytes(); + out[table_at + k * 8..table_at + k * 8 + 8].copy_from_slice(&offset); + out.extend_from_slice(&((k * lines) as i32).to_le_bytes()); + out.extend_from_slice(&(data.len() as i32).to_le_bytes()); + out.extend_from_slice(data); + } + Ok(out) +} + +/// The OpenEXR RLE / ZIP predictor: bytes split into even and odd halves, then each byte replaced +/// by its difference from the previous one, plus 128. +fn predict(raw: &[u8]) -> Vec { + let half = raw.len().div_ceil(2); + let mut t = vec![0_u8; raw.len()]; + for (i, &b) in raw.iter().enumerate() { + t[if i % 2 == 0 { i / 2 } else { half + i / 2 }] = b; + } + let mut p = t.first().copied().unwrap_or(0); + for v in t.iter_mut().skip(1) { + let d = v.wrapping_sub(p).wrapping_add(128); + p = *v; + *v = d; + } + t +} + +/// OpenEXR run-length coding: a signed count byte, then either `count + 1` copies of one byte +/// (count ≥ 0, runs of 3 to 128) or `-count` literal bytes (1 to 127). +pub fn rle(src: &[u8]) -> Vec { + let mut out = Vec::with_capacity(src.len() + src.len() / 127 + 1); + let mut i = 0; + while i < src.len() { + let b = src[i]; + let run = src[i..].iter().take(128).take_while(|&&x| x == b).count(); + if run >= 3 { + out.extend_from_slice(&[(run - 1) as u8, b]); + i += run; + continue; + } + // literals up to the next run of three (or 127 bytes) + let s = i; + while i < src.len() && i - s < 127 { + if src.get(i..i + 3).is_some_and(|t| t[0] == t[1] && t[1] == t[2]) { + break; + } + i += 1; + } + out.push(((i - s) as u8).wrapping_neg()); + out.extend_from_slice(&src[s..i]); + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn writes_all_scanline_compressions() { + let linear = std::array::from_fn(|i| i as f32 / 255.0); + for c in [Compression::None, Compression::Rle, Compression::Zips, Compression::Zip] { + let bytes = write_rgba8(&[128; 16 * 16 * 4], 16, 16, true, c, &linear).unwrap(); + assert_eq!(&bytes[..4], &[0x76, 0x2f, 0x31, 1]); + assert!(bytes.len() > 100); + } + assert_eq!(write_rgba8(&[], 1, 1, false, Compression::None, &linear), Err(Error::FrameSize)); + } +} From ee44af6a45c2d12c4e63058db08a9f417002636d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:14:00 +0000 Subject: [PATCH 046/150] Share first-directory TIFF inspection --- crates/image-codecs/src/lib.rs | 1 + crates/image-codecs/src/tiff_probe.rs | 68 +++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 crates/image-codecs/src/tiff_probe.rs diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 6516b9ff2..b94eb6606 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -30,6 +30,7 @@ mod options; pub mod orientation; pub mod resolution; pub mod source_settings; +pub mod tiff_probe; pub mod web; pub use crate::allocation::probe_allocation_size; pub use crate::cicp::Cicp; diff --git a/crates/image-codecs/src/tiff_probe.rs b/crates/image-codecs/src/tiff_probe.rs new file mode 100644 index 000000000..1a542e412 --- /dev/null +++ b/crates/image-codecs/src/tiff_probe.rs @@ -0,0 +1,68 @@ +//! First-directory TIFF inspection without decoding pixels. + +/// A header inspection failure, distinguishing caller limits from unsupported decoder layouts. +#[derive(Debug, thiserror::Error)] +pub enum Error { + /// Dimensions exceed the caller's pixel limit. + #[error("TIFF dimensions {width}x{height} exceed the pixel limit")] + TooLarge { + /// Stored width. + width: u32, + /// Stored height. + height: u32, + }, + /// The shared TIFF decoder does not support this layout. + #[error("unsupported TIFF: {0}")] + Unsupported(&'static str), + /// The file's header, directory or image-data bounds are malformed. + #[error("malformed TIFF: {0}")] + Malformed(String), +} + +/// Stored dimensions and orientation of the first directory, after checking the layouts and +/// image-data bounds required by the TIFF decoder. Compressed strips are not decompressed. +pub fn header(bytes: &[u8], max_pixels: u64) -> Result<(u32, u32, u16), Error> { + let (tiff, complete) = craft_tiff::Tiff::parse_with_status(bytes, &Default::default()).map_err(|e| Error::Malformed(e.to_string()))?; + if !complete { + return Err(Error::Malformed("truncated TIFF".into())); + } + let ifd = tiff.ifds.first().ok_or_else(|| Error::Malformed("no IFD".into()))?; + let info = ifd.image().map_err(|e| Error::Malformed(e.to_string()))?; + if info.width == 0 || info.height == 0 { + return Err(Error::Malformed("zero dimension".into())); + } + if u64::from(info.width) * u64::from(info.height) > max_pixels { + return Err(Error::TooLarge { width: info.width, height: info.height }); + } + let bits = info.bits_per_sample.first().copied().unwrap_or(1); + if bits < 8 && info.planar == 2 && info.samples_per_pixel > 1 { + return Err(Error::Unsupported("planar TIFF with samples narrower than a byte")); + } + if !(matches!(info.photometric, 0 | 1 | 2 | 3 | 5) || info.photometric == 6 && info.compression == 7) { + return Err(Error::Unsupported("colour type")); + } + if !matches!(info.compression, 1 | 4 | 5 | 7 | 8 | 32773 | 32946) { + return Err(Error::Unsupported("compression method")); + } + let len = bytes.len() as u64; + if info.offsets.is_empty() + || info.offsets.iter().zip(&info.byte_counts).any(|(&o, &c)| c > 0 && (o >= len || (info.compression == 1 && o.saturating_add(c) > len))) + { + return Err(Error::Malformed("truncated: image data past the end of the file".into())); + } + let orientation = ifd.u16(craft_tiff::tags::ORIENTATION).filter(|o| (1..=8).contains(o)).unwrap_or(1); + Ok((info.width, info.height, orientation)) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn reads_first_directory_and_enforces_limits_and_bounds() { + let image = crate::Image::from_u8(2, 3, crate::ChannelLayout::Rgb, vec![42; 18]).unwrap(); + let bytes = crate::encode(&image, crate::Format::Tiff, &crate::EncodeOptions::default()).unwrap(); + assert_eq!(header(&bytes, 6).unwrap(), (2, 3, 1)); + assert!(matches!(header(&bytes, 5), Err(Error::TooLarge { width: 2, height: 3 }))); + assert!(header(&bytes[..8], 6).is_err()); + } +} From 553c86719ce9d57f01bcfb6442ac68ac58cd65de Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:14:57 +0000 Subject: [PATCH 047/150] Share catalog face metrics and glyph outlines --- Cargo.lock | 1 + crates/font-catalog/Cargo.toml | 1 + crates/font-catalog/src/lib.rs | 2 + crates/font-catalog/src/outline.rs | 133 +++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 crates/font-catalog/src/outline.rs diff --git a/Cargo.lock b/Cargo.lock index 9fb46057c..9424cf3fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1761,6 +1761,7 @@ name = "craft-font-catalog" version = "0.1.0" dependencies = [ "fontique", + "kurbo 0.13.1", "skrifa 0.47.0", "thiserror 2.0.21", "windows-registry", diff --git a/crates/font-catalog/Cargo.toml b/crates/font-catalog/Cargo.toml index a95e1c28f..1f0830a63 100644 --- a/crates/font-catalog/Cargo.toml +++ b/crates/font-catalog/Cargo.toml @@ -15,6 +15,7 @@ default = ["system"] system = ["dep:fontique"] [dependencies] +kurbo = { workspace = true } skrifa = { version = "0.47", default-features = false, features = ["std"] } thiserror = { workspace = true } diff --git a/crates/font-catalog/src/lib.rs b/crates/font-catalog/src/lib.rs index c787d3ece..7da4ca310 100644 --- a/crates/font-catalog/src/lib.rs +++ b/crates/font-catalog/src/lib.rs @@ -23,6 +23,7 @@ mod catalog; mod env; mod fontconfig; +mod outline; mod registry; mod scan; mod suitcase; @@ -31,6 +32,7 @@ pub mod system; pub use catalog::{Catalog, FontData, FontFileError, Limits, Lister, LoadedFace, Match, Watch}; pub use env::{FontEnv, Os, font_dirs, font_store_dir, office_font_dirs}; pub use fontconfig::fontconfig_dirs; +pub use outline::{FontOutline, OutlineError}; pub use scan::{FaceInfo, LocalizedName, Source, is_font_file, style_italic, style_weight, style_width}; #[cfg(test)] diff --git a/crates/font-catalog/src/outline.rs b/crates/font-catalog/src/outline.rs new file mode 100644 index 000000000..cd27e0c53 --- /dev/null +++ b/crates/font-catalog/src/outline.rs @@ -0,0 +1,133 @@ +//! Unhinted glyph outlines and metrics for catalog faces. + +use kurbo::BezPath; +use skrifa::instance::{Location, Size}; +use skrifa::outline::{DrawSettings, OutlinePen}; +use skrifa::{FontRef, GlyphId, MetadataProvider, Tag}; + +use crate::{FontData, LoadedFace}; + +/// A parsed face and its variation location. Coordinates use font design units, with y up. +/// The bytes can come from a catalog or any other source, including a collection face. +pub struct FontOutline<'a> { + font: FontRef<'a>, + location: Location, +} + +/// Why a font or glyph outline could not be read. +#[derive(Debug, thiserror::Error)] +pub enum OutlineError { + /// The face index or font data is invalid. + #[error("invalid font face")] + InvalidFace, + /// The glyph has no outline in this face. + #[error("glyph has no outline")] + MissingGlyph, + /// The glyph's outline is malformed. + #[error("cannot draw glyph outline: {0}")] + Draw(#[from] skrifa::outline::DrawError), +} + +impl<'a> FontOutline<'a> { + /// Parse the given face and select axis values in user units. Unknown axes are ignored and + /// values are clamped to their axis ranges by the font parser. + pub fn new(data: &'a FontData, variations: &[([u8; 4], f32)]) -> Result { + Self::from_bytes(&data.bytes, data.index, variations) + } + + /// Parse borrowed bytes and a collection face index without copying the font. + pub fn from_bytes(bytes: &'a [u8], index: u32, variations: &[([u8; 4], f32)]) -> Result { + let font = FontRef::from_index(bytes, index).map_err(|_| OutlineError::InvalidFace)?; + let location = font.axes().location(variations.iter().map(|(tag, value)| (Tag::new(tag), *value))); + Ok(Self { font, location }) + } + + /// Parse a catalog face, retaining the axis settings of a named instance. + pub fn from_loaded(face: &'a LoadedFace) -> Result { + Self::new(&face.data, &face.info.variations) + } + + /// Font-wide metrics in unscaled design units at this variation location. + pub fn metrics(&self) -> skrifa::metrics::Metrics { + self.font.metrics(Size::unscaled(), &self.location) + } + + /// Resolve a Unicode scalar to a glyph identifier. + pub fn glyph_for(&self, character: char) -> Option { + self.font.charmap().map(character).map(|id| id.to_u32()) + } + + /// Draw a glyph in design units. Empty glyphs such as spaces can produce an empty path. + pub fn outline(&self, glyph: u32) -> Result { + let outline = self.font.outline_glyphs().get(GlyphId::new(glyph)).ok_or(OutlineError::MissingGlyph)?; + let mut pen = PathPen(BezPath::new()); + outline.draw(DrawSettings::unhinted(Size::unscaled(), &self.location), &mut pen)?; + Ok(pen.0) + } +} + +struct PathPen(BezPath); +impl OutlinePen for PathPen { + fn move_to(&mut self, x: f32, y: f32) { + self.0.move_to((f64::from(x), f64::from(y))); + } + fn line_to(&mut self, x: f32, y: f32) { + self.0.line_to((f64::from(x), f64::from(y))); + } + fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) { + self.0.quad_to((f64::from(x1), f64::from(y1)), (f64::from(x), f64::from(y))); + } + fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) { + self.0.curve_to((f64::from(x1), f64::from(y1)), (f64::from(x2), f64::from(y2)), (f64::from(x), f64::from(y))); + } + fn close(&mut self) { + self.0.close_path(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use kurbo::PathEl; + use std::sync::Arc; + + #[test] + fn rejects_invalid_face_and_collection_index() { + let data = FontData { bytes: Arc::from(&b"not a font"[..]), index: 0 }; + assert!(matches!(FontOutline::new(&data, &[]), Err(OutlineError::InvalidFace))); + } + + #[test] + fn pen_preserves_geometry_and_upward_y() { + let mut pen = PathPen(BezPath::new()); + pen.move_to(1.0, 2.0); + pen.line_to(3.0, 4.0); + pen.quad_to(5.0, 6.0, 7.0, 8.0); + pen.curve_to(9.0, 10.0, 11.0, 12.0, 13.0, 14.0); + pen.close(); + assert_eq!( + pen.0.elements(), + &[ + PathEl::MoveTo((1.0, 2.0).into()), + PathEl::LineTo((3.0, 4.0).into()), + PathEl::QuadTo((5.0, 6.0).into(), (7.0, 8.0).into()), + PathEl::CurveTo((9.0, 10.0).into(), (11.0, 12.0).into(), (13.0, 14.0).into()), + PathEl::ClosePath, + ] + ); + } + + #[test] + fn reads_craft_font_when_available() { + let Ok(dir) = std::env::var("CRAFT_FONTS_DIR") else { return }; + let bytes = std::fs::read(std::path::Path::new(&dir).join("fonts/noto-sans-hebrew/NotoSansHebrew-Regular.ttf")).unwrap(); + let data = FontData { bytes: bytes.into(), index: 0 }; + let face = FontOutline::new(&data, &[]).unwrap(); + assert!(face.metrics().units_per_em > 0); + let glyph = face.glyph_for('×�').unwrap(); + assert!(!face.outline(glyph).unwrap().elements().is_empty()); + assert!(matches!(face.outline(u32::MAX), Err(OutlineError::MissingGlyph))); + let invalid_index = FontData { bytes: data.bytes.clone(), index: u32::MAX }; + assert!(matches!(FontOutline::new(&invalid_index, &[]), Err(OutlineError::InvalidFace))); + } +} From fb6cb780061c6744ed6886e7dd1a80ccf449e655 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:15:21 +0000 Subject: [PATCH 048/150] Depend on shared TIFF directory parser --- Cargo.lock | 1 + crates/image-codecs/Cargo.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 9fb46057c..23cdde361 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1927,6 +1927,7 @@ dependencies = [ "craft-heif", "craft-jpeg", "craft-metadata", + "craft-tiff", "craft-video", "crc32fast", "exr", diff --git a/crates/image-codecs/Cargo.toml b/crates/image-codecs/Cargo.toml index df71d9860..f697610a6 100644 --- a/crates/image-codecs/Cargo.toml +++ b/crates/image-codecs/Cargo.toml @@ -32,6 +32,7 @@ zune-core = "0.5" jpeg-encoder = "0.6" craft-jpeg = { workspace = true } craft-metadata = { workspace = true } +craft-tiff = { workspace = true } zune-jpegxl = { version = "=0.5.2", default-features = false, features = ["std"] } craft-heif = { workspace = true, optional = true } ravif = { version = "0.13", default-features = false, optional = true } From b616d590e862d9a2c63d67aaadb59dcae2e6d054 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 15 Nov 2026 11:15:32 -0500 Subject: [PATCH 049/150] Port XML property-list reader from Craft Anything Source: storytold/craft-anything at 8f3b775157d331dd31cfa4da911e26d8856b8c77, crates/formats/src/plist.rs and bytes.rs. Preserve original contributor fixes for document boundaries, UTF-16, numeric references, quoted attributes and line endings. Co-authored-by: Afloat Co-authored-by: BFlat Co-authored-by: Rahul Co-authored-by: RRFZ <247457381+Plastic-time@users.noreply.github.com> Co-authored-by: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> --- crates/plist/src/xml.rs | 432 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 432 insertions(+) create mode 100644 crates/plist/src/xml.rs diff --git a/crates/plist/src/xml.rs b/crates/plist/src/xml.rs new file mode 100644 index 000000000..7b41ecc91 --- /dev/null +++ b/crates/plist/src/xml.rs @@ -0,0 +1,432 @@ +//! XML property-list reader ported from Craft Anything at 8f3b7751. +use crate::{DateValue, FormatError, Value}; +const MAX_NODES: usize = 100_000; +const MAX_DEPTH: usize = 64; +fn decode_base64(text: &str, url_safe: bool) -> Option> { + let mut out = Vec::with_capacity(text.len() / 4 * 3); + let mut acc = 0u32; + let mut bits = 0u32; + let mut digits = 0usize; + let mut padding = 0usize; + for c in text.bytes() { + match c { + b' ' | b'\t' | b'\r' | b'\n' => continue, + b'=' => { + padding += 1; + if padding > 2 { + return None; + } + continue; + } + _ if padding != 0 => return None, + _ => {} + } + let value = match c { + b'A'..=b'Z' => c - b'A', + b'a'..=b'z' => c - b'a' + 26, + b'0'..=b'9' => c - b'0' + 52, + b'+' => 62, + b'/' => 63, + b'-' if url_safe => 62, + b'_' if url_safe => 63, + _ => return None, + }; + acc = (acc << 6) | u32::from(value); + bits += 6; + digits += 1; + if bits >= 8 { + bits -= 8; + out.push((acc >> bits) as u8); + acc &= (1 << bits) - 1; + } + } + let remainder = digits % 4; + if remainder == 1 || (padding != 0 && !matches!((remainder, padding), (2, 2) | (3, 1))) { + return None; + } + Some(out) +} + +fn base64_decode(s: &str) -> Option> { decode_base64(s, false) } +#[derive(Debug, Clone, PartialEq)] +enum Tok { + Open(String), + Close(String), + Empty(String), + Text(String), +} + +fn decode_entities(s: &str) -> String { + if !s.contains('&') { + return s.to_string(); + } + let mut out = String::with_capacity(s.len()); + let mut rest = s; + while let Some(p) = rest.find('&') { + out.push_str(rest.get(..p).unwrap_or("")); + let after = rest.get(p..).unwrap_or(""); + let Some(end) = after.find(';').filter(|&e| e <= 12) else { + out.push('&'); + rest = after.get(1..).unwrap_or(""); + continue; + }; + let ent = after.get(1..end).unwrap_or(""); + let rep = match ent { + "lt" => Some('<'), + "gt" => Some('>'), + "amp" => Some('&'), + "quot" => Some('"'), + "apos" => Some('\''), + e if e.starts_with("#x") || e.starts_with("#X") => { + let d = e.get(2..).unwrap_or(""); + if d.bytes().all(|b| b.is_ascii_hexdigit()) { u32::from_str_radix(d, 16).ok().and_then(char::from_u32) } else { None } + } + e if e.starts_with('#') => { + let d = e.get(1..).unwrap_or(""); + if d.bytes().all(|b| b.is_ascii_digit()) { d.parse::().ok().and_then(char::from_u32) } else { None } + } + _ => None, + }; + match rep { + Some(c) => { + out.push(c); + rest = after.get(end + 1..).unwrap_or(""); + } + None => { + out.push('&'); + rest = after.get(1..).unwrap_or(""); + } + } + } + out.push_str(rest); + out +} + +/// Skip a declaration without interpreting or fetching any entity. A DOCTYPE's +/// internal subset can itself contain declarations, quoted delimiters, comments and PIs. +fn after_declaration(mut rest: &str) -> Result<&str, FormatError> { + let mut quote = None; + let mut brackets = 0usize; + while let Some(ch) = rest.chars().next() { + if quote.is_none() { + if let Some(comment) = rest.strip_prefix("").ok_or_else(|| FormatError::Truncated("comment in declaration".into()))?.1; + continue; + } + if let Some(pi) = rest.strip_prefix("").ok_or_else(|| FormatError::Truncated("processing instruction in declaration".into()))?.1; + continue; + } + } + rest = rest.get(ch.len_utf8()..).unwrap_or(""); + match (quote, ch) { + (Some(q), c) if q == c => quote = None, + (None, '\'' | '"') => quote = Some(ch), + (None, '[') => brackets += 1, + (None, ']') => brackets = brackets.saturating_sub(1), + (None, '>') if brackets == 0 => return Ok(rest), + _ => {} + } + } + Err(FormatError::Truncated("declaration".into())) +} + +fn tokenize(text: &str) -> Result, FormatError> { + let mut toks = Vec::new(); + let mut rest = text; + while !rest.is_empty() { + if toks.len() > MAX_NODES { + return Err(FormatError::TooLarge("too many XML tokens".into())); + } + match rest.find('<') { + None => { + if !rest.is_empty() { + toks.push(Tok::Text(decode_entities(rest))); + } + break; + } + Some(p) => { + let t = rest.get(..p).unwrap_or(""); + if !t.is_empty() { + toks.push(Tok::Text(decode_entities(t))); + } + rest = rest.get(p..).unwrap_or(""); + } + } + if let Some(r) = rest.strip_prefix("").ok_or_else(|| FormatError::Truncated("comment".into()))?; + rest = r.get(e + 3..).unwrap_or(""); + continue; + } + if let Some(r) = rest.strip_prefix("").ok_or_else(|| FormatError::Truncated("CDATA".into()))?; + toks.push(Tok::Text(r.get(..e).unwrap_or("").to_string())); + rest = r.get(e + 3..).unwrap_or(""); + continue; + } + if let Some(r) = rest.strip_prefix("").ok_or_else(|| FormatError::Truncated("processing instruction".into()))?; + rest = r.get(end + 2..).unwrap_or(""); + continue; + } + if let Some(declaration) = rest.strip_prefix(" { + quote = None; + false + } + (None, '\'' | '"') => { + quote = Some(ch); + false + } + (None, '>') => true, + _ => false, + }) + .ok_or_else(|| FormatError::Truncated("tag".into()))?; + let inner = rest.get(1..e).unwrap_or("").trim(); + rest = rest.get(e + 1..).unwrap_or(""); + if let Some(name) = inner.strip_prefix('/') { + toks.push(Tok::Close(name.trim().to_string())); + } else if let Some(body) = inner.strip_suffix('/') { + toks.push(Tok::Empty(body.split_whitespace().next().unwrap_or("").to_string())); + } else { + toks.push(Tok::Open(inner.split_whitespace().next().unwrap_or("").to_string())); + } + } + Ok(toks) +} + +struct Xml { + toks: Vec, + pos: usize, + nodes: usize, +} + +impl Xml { + fn next(&mut self) -> Option { + let t = self.toks.get(self.pos).cloned(); + self.pos += 1; + t + } + + fn peek(&self) -> Option<&Tok> { + self.toks.get(self.pos) + } + + /// Text up to the closing tag `name`. + fn text_until(&mut self, name: &str) -> Result { + let mut s = String::new(); + loop { + match self.next() { + Some(Tok::Text(t)) => s.push_str(&t), + Some(Tok::Close(n)) if n == name => return Ok(s), + Some(other) => return Err(FormatError::Malformed(format!("unexpected {other:?} inside <{name}>"))), + None => return Err(FormatError::Truncated(format!("<{name}> is not closed"))), + } + } + } + + fn skip_space(&mut self) { + while matches!(self.peek(), Some(Tok::Text(t)) if t.bytes().all(|b| matches!(b, b' ' | b'\t' | b'\r' | b'\n'))) { + self.pos += 1; + } + } + + fn value(&mut self, depth: usize) -> Result { + self.nodes += 1; + if depth > MAX_DEPTH || self.nodes > MAX_NODES { + return Err(FormatError::TooLarge("plist nesting or size limit".into())); + } + self.skip_space(); + match self.next() { + Some(Tok::Empty(n)) => Ok(match n.as_str() { + "true" => Value::Bool(true), + "false" => Value::Bool(false), + "dict" => Value::Dict(Vec::new()), + "array" => Value::Array(Vec::new()), + "string" => Value::String(String::new()), + "data" => Value::Data(Vec::new()), + other => return Err(FormatError::Malformed(format!("unexpected <{other}/>"))), + }), + Some(Tok::Open(n)) => match n.as_str() { + "string" => Ok(Value::String(self.text_until("string")?)), + "integer" => { + let t = self.text_until("integer")?; + let t = t.trim(); + let v = if let Some(h) = t.strip_prefix("0x").or_else(|| t.strip_prefix("0X")) { + i128::from_str_radix(h, 16).ok() + } else { + t.parse::().ok() + }; + v.map(Value::Integer).ok_or_else(|| FormatError::Malformed(format!("bad integer {t:?}"))) + } + "real" => { + let t = self.text_until("real")?; + t.trim().parse::().map(Value::Real).map_err(|_| FormatError::Malformed(format!("bad real {t:?}"))) + } + "date" => Ok(Value::Date(DateValue::Text(self.text_until("date")?.trim().to_string()))), + "data" => { + let t = self.text_until("data")?; + base64_decode(&t).map(Value::Data).ok_or_else(|| FormatError::Malformed("bad base64 in ".into())) + } + "true" | "false" => { + if !self.text_until(&n)?.is_empty() { + return Err(FormatError::Malformed(format!("<{n}> must be empty"))); + } + Ok(Value::Bool(n == "true")) + } + "array" => { + let mut out = Vec::new(); + loop { + self.skip_space(); + if matches!(self.peek(), Some(Tok::Close(c)) if c == "array") { + self.pos += 1; + return Ok(Value::Array(out)); + } + if self.peek().is_none() { + return Err(FormatError::Truncated(" is not closed".into())); + } + out.push(self.value(depth + 1)?); + } + } + "dict" => { + let mut out = Vec::new(); + loop { + self.skip_space(); + match self.next() { + Some(Tok::Close(c)) if c == "dict" => return Ok(Value::Dict(out)), + Some(Tok::Open(k)) if k == "key" => { + let key = self.text_until("key")?; + let v = self.value(depth + 1)?; + out.push((key, v)); + } + Some(Tok::Empty(k)) if k == "key" => { + let v = self.value(depth + 1)?; + out.push((String::new(), v)); + } + Some(other) => return Err(FormatError::Malformed(format!("expected in , found {other:?}"))), + None => return Err(FormatError::Truncated(" is not closed".into())), + } + } + } + other => Err(FormatError::Malformed(format!("unknown plist element <{other}>"))), + }, + Some(other) => Err(FormatError::Malformed(format!("expected a value, found {other:?}"))), + None => Err(FormatError::Truncated("expected a value".into())), + } + } +} + +/// Decode a UTF-16 XML property list after its byte-order mark into UTF-8 text. +/// Surrogate pairs join normally; a lone surrogate fails as malformed rather +/// than turning into replacement characters that tokenize into garbage element +/// names. +fn decode_utf16_xml(data: &[u8], big_endian: bool) -> Result { + if !data.len().is_multiple_of(2) { + return Err(FormatError::Truncated("UTF-16 code unit".into())); + } + let units: Vec = data.chunks_exact(2).map(|b| if big_endian { u16::from_be_bytes([b[0], b[1]]) } else { u16::from_le_bytes([b[0], b[1]]) }).collect(); + String::from_utf16(&units).map_err(|_| FormatError::Malformed("invalid UTF-16 in XML plist".into())) +} + +/// Parse an XML property list. +pub fn parse_xml(data: &[u8]) -> Result { + // Apple's Core Foundation reader accepts XML plists in UTF-16 as well as + // UTF-8: it recognises both byte-order marks. `from_utf8_lossy` turns a + // UTF-16 byte stream into NUL-separated text, so the encoding is settled + // here, before tokenizing. + let text = match data { + [0xFF, 0xFE, rest @ ..] => decode_utf16_xml(rest, false)?, + [0xFE, 0xFF, rest @ ..] => decode_utf16_xml(rest, true)?, + _ => std::str::from_utf8(data.strip_prefix(b"\xef\xbb\xbf").unwrap_or(data)) + .map_err(|_| FormatError::Malformed("invalid UTF-8 in XML plist".into()))? + .to_owned(), + }; + // XML normalizes literal line endings before parsing; character references such as + // must still decode to a carriage return afterwards. + let text = if text.contains('\r') { text.replace("\r\n", "\n").replace('\r', "\n") } else { text }; + let toks = tokenize(&text)?; + let mut x = Xml { toks, pos: 0, nodes: 0 }; + x.skip_space(); + let wrapped = matches!(x.peek(), Some(Tok::Open(n)) if n == "plist"); + if wrapped { + x.pos += 1; + } + let value = x.value(0)?; + x.skip_space(); + if wrapped { + if !matches!(x.next(), Some(Tok::Close(n)) if n == "plist") { + return Err(FormatError::Malformed("expected after its value".into())); + } + x.skip_space(); + } + if x.peek().is_some() { + return Err(FormatError::Malformed("unexpected content after the plist value".into())); + } + Ok(value) +} + + +#[cfg(test)] +mod tests { +use super::*; + #[test] + fn xml_has_one_complete_root_with_only_markup_space_around_values() { + for invalid in [ + "", + "", + "", + "", + "junk", + "text", + "junk", + "xgarbage", + "no", + "yes", + "", + ] { + assert!(parse_xml(invalid.as_bytes()).is_err(), "{invalid}"); + } + let xml = b" ?>\n "; + assert_eq!(parse_xml(xml).unwrap().get("").and_then(Value::as_str), Some(" ")); + assert_eq!(parse_xml(b" ").unwrap(), Value::Bool(true)); + } + + #[test] + fn xml_rejects_partial_code_units_and_preserves_the_utf8_bom() { + assert_eq!(parse_xml(b"\xef\xbb\xbfok").unwrap().as_str(), Some("ok")); + assert!(parse_xml(b"\xff").is_err()); + for be in [false, true] { + let mut bytes = if be { vec![0xfe, 0xff] } else { vec![0xff, 0xfe] }; + for unit in "".encode_utf16() { + bytes.extend(if be { unit.to_be_bytes() } else { unit.to_le_bytes() }); + } + bytes.push(0); + assert!(parse_xml(&bytes).is_err()); + } + } + + #[test] + fn a_doctype_internal_subset_is_skipped_as_one_declaration() { + let doc = br#"y"> ?>]>ok"#; + assert_eq!(parse_xml(doc).unwrap().as_str(), Some("ok")); + assert!(parse_xml(b"").is_err()); + } + + #[test] + fn xml_char_refs_require_plain_digits() { + let text = |s: &str| { + let doc = format!("{s}"); + parse_xml(doc.as_bytes()).unwrap().as_str().map(str::to_string) + }; + assert_eq!(text("a&#+65;b&#x+41;").as_deref(), Some("a&#+65;b&#x+41;")); + assert_eq!(text("AA").as_deref(), Some("AA")); + assert_eq!(text("&#;&#x;").as_deref(), Some("&#;&#x;")); + } + +} From 4c4ad5d20f03b2d566bab89678712b6eb9231194 Mon Sep 17 00:00:00 2001 From: gmrmad1 Date: Sun, 15 Nov 2026 11:15:51 -0500 Subject: [PATCH 050/150] Reject nonfinite XML property-list reals Port storytold/craft-anything#1905 at d85e104f093a334961048881bf003351e1ac4992. Retain typed-value tests; app JSON projection remains outside this codec. --- crates/plist/src/xml.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/plist/src/xml.rs b/crates/plist/src/xml.rs index 7b41ecc91..54fd9f895 100644 --- a/crates/plist/src/xml.rs +++ b/crates/plist/src/xml.rs @@ -267,7 +267,7 @@ impl Xml { } "real" => { let t = self.text_until("real")?; - t.trim().parse::().map(Value::Real).map_err(|_| FormatError::Malformed(format!("bad real {t:?}"))) + t.trim().parse::().ok().filter(|v| v.is_finite()).map(Value::Real).ok_or_else(|| FormatError::Malformed(format!("bad real {t:?}"))) } "date" => Ok(Value::Date(DateValue::Text(self.text_until("date")?.trim().to_string()))), "data" => { @@ -429,4 +429,16 @@ use super::*; assert_eq!(text("&#;&#x;").as_deref(), Some("&#;&#x;")); } + #[test] + fn xml_real_must_be_finite() { + for bad in ["NaN", "1e999"] { + let doc = format!("k{bad}"); + assert!(parse_xml(doc.as_bytes()).is_err(), "{bad}"); + } + for (text, want) in [("1.5", 1.5), ("-2", -2.0), ("0", 0.0)] { + let doc = format!("k{text}"); + let v = parse_xml(doc.as_bytes()).unwrap_or_else(|e| panic!("{text} should parse: {e}")); + assert_eq!(v.get("k"), Some(&Value::Real(want)), "{text}"); + } + } } From c714810569eab85751713bea62c5833ae10af9b9 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:15:51 +0000 Subject: [PATCH 051/150] Test outlines at variable font weights --- crates/font-catalog/src/outline.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/font-catalog/src/outline.rs b/crates/font-catalog/src/outline.rs index cd27e0c53..1f550f983 100644 --- a/crates/font-catalog/src/outline.rs +++ b/crates/font-catalog/src/outline.rs @@ -130,4 +130,18 @@ mod tests { let invalid_index = FontData { bytes: data.bytes.clone(), index: u32::MAX }; assert!(matches!(FontOutline::new(&invalid_index, &[]), Err(OutlineError::InvalidFace))); } + + #[test] + fn variation_changes_glyph_geometry() { + let Ok(dir) = std::env::var("CRAFT_FONTS_DIR") else { return }; + let bytes = std::fs::read(std::path::Path::new(&dir).join("fonts/noto-sans-arabic/NotoSansArabic.ttf")).unwrap(); + let data = FontData { bytes: bytes.into(), index: 0 }; + let light = FontOutline::new(&data, &[(*b"wght", 100.0)]).unwrap(); + let bold = FontOutline::new(&data, &[(*b"wght", 900.0)]).unwrap(); + let glyph = light.glyph_for('ب').unwrap(); + let light_path = light.outline(glyph).unwrap(); + let bold_path = bold.outline(glyph).unwrap(); + assert!(!light_path.elements().is_empty()); + assert_ne!(light_path, bold_path); + } } From ab37b756cc86dfc552eeb351b0b407e546ec4d9e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:16:31 +0000 Subject: [PATCH 052/150] exr: fix compressor test helper lint --- crates/exr/src/zlib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/exr/src/zlib.rs b/crates/exr/src/zlib.rs index 8959a7e89..a38bd0b46 100644 --- a/crates/exr/src/zlib.rs +++ b/crates/exr/src/zlib.rs @@ -501,9 +501,8 @@ mod tests { } let mut sorted: Vec = (0..lens.len()).filter(|&s| lens[s] > 0).collect(); sorted.sort_by_key(|&s| (lens[s], s)); - for len in 1..16 { + for &c in count.iter().skip(1) { code |= bit(pos)? as i32; - let c = count[len]; if code - c < first { return sorted.get((index + code - first) as usize).copied(); } From b2edb7ed80d8c8fac639050a3ea02f6e0ecaff30 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:16:31 +0000 Subject: [PATCH 053/150] exr: fix compressor test helper lint --- crates/exr/src/zlib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/exr/src/zlib.rs b/crates/exr/src/zlib.rs index 8959a7e89..a38bd0b46 100644 --- a/crates/exr/src/zlib.rs +++ b/crates/exr/src/zlib.rs @@ -501,9 +501,8 @@ mod tests { } let mut sorted: Vec = (0..lens.len()).filter(|&s| lens[s] > 0).collect(); sorted.sort_by_key(|&s| (lens[s], s)); - for len in 1..16 { + for &c in count.iter().skip(1) { code |= bit(pos)? as i32; - let c = count[len]; if code - c < first { return sorted.get((index + code - first) as usize).copied(); } From c339a9e4715bb0856603de7af897efcb43430f59 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:16:52 +0000 Subject: [PATCH 054/150] Share TIFF decode preflight fields --- crates/image-codecs/src/tiff_probe.rs | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/crates/image-codecs/src/tiff_probe.rs b/crates/image-codecs/src/tiff_probe.rs index 1a542e412..05bd1e4db 100644 --- a/crates/image-codecs/src/tiff_probe.rs +++ b/crates/image-codecs/src/tiff_probe.rs @@ -19,6 +19,42 @@ pub enum Error { Malformed(String), } +/// First-directory values needed to preflight decoding. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct DecodeInfo { + /// Stored width. + pub width: u32, + /// Stored height. + pub height: u32, + /// First sample's bit depth, defaulting to eight. + pub bit_depth: u8, + /// Raw orientation tag, if present. + pub orientation: Option, +} + +/// Parse the first directory and enforce the pixel limit before decoding. +/// Unlike [`header`], this does not impose layout or strip checks ahead of the decoder. +pub fn decode_info(bytes: &[u8], max_pixels: u64) -> Result { + let (tiff, complete) = craft_tiff::Tiff::parse_with_status(bytes, &Default::default()).map_err(|e| Error::Malformed(e.to_string()))?; + if !complete { + return Err(Error::Malformed("truncated TIFF".into())); + } + let ifd = tiff.ifds.first().ok_or_else(|| Error::Malformed("no IFD".into()))?; + let info = ifd.image().map_err(|e| Error::Malformed(e.to_string()))?; + if info.width == 0 || info.height == 0 { + return Err(Error::Malformed("zero dimension".into())); + } + if u64::from(info.width) * u64::from(info.height) > max_pixels { + return Err(Error::TooLarge { width: info.width, height: info.height }); + } + Ok(DecodeInfo { + width: info.width, + height: info.height, + bit_depth: info.bits_per_sample.first().copied().unwrap_or(8) as u8, + orientation: ifd.u16(craft_tiff::tags::ORIENTATION), + }) +} + /// Stored dimensions and orientation of the first directory, after checking the layouts and /// image-data bounds required by the TIFF decoder. Compressed strips are not decompressed. pub fn header(bytes: &[u8], max_pixels: u64) -> Result<(u32, u32, u16), Error> { From b16c5908ae777f81fa33a9e9ad0e22b09b37a383 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:17:27 +0000 Subject: [PATCH 055/150] Cover TIFF decode preflight limits --- crates/image-codecs/src/tiff_probe.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/image-codecs/src/tiff_probe.rs b/crates/image-codecs/src/tiff_probe.rs index 05bd1e4db..6aaf5c21a 100644 --- a/crates/image-codecs/src/tiff_probe.rs +++ b/crates/image-codecs/src/tiff_probe.rs @@ -98,6 +98,9 @@ mod tests { let image = crate::Image::from_u8(2, 3, crate::ChannelLayout::Rgb, vec![42; 18]).unwrap(); let bytes = crate::encode(&image, crate::Format::Tiff, &crate::EncodeOptions::default()).unwrap(); assert_eq!(header(&bytes, 6).unwrap(), (2, 3, 1)); + let info = decode_info(&bytes, 6).unwrap(); + assert_eq!((info.width, info.height, info.bit_depth), (2, 3, 8)); + assert!(matches!(decode_info(&bytes, 5), Err(Error::TooLarge { width: 2, height: 3 }))); assert!(matches!(header(&bytes, 5), Err(Error::TooLarge { width: 2, height: 3 }))); assert!(header(&bytes[..8], 6).is_err()); } From ada2188786fd646443c4fb3326e68e52ce84c90d Mon Sep 17 00:00:00 2001 From: RRFZ <247457381+Plastic-time@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:17:46 -0500 Subject: [PATCH 056/150] Preserve XML quoted-attribute and line-ending regression tests Imported from storytold/craft-anything #1738 and #1726 at 8f3b7751, adapted only to shared parse_xml entry point. --- crates/plist/tests/plist_line_endings.rs | 17 ++++++++++++++ crates/plist/tests/plist_quoted_attributes.rs | 22 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 crates/plist/tests/plist_line_endings.rs create mode 100644 crates/plist/tests/plist_quoted_attributes.rs diff --git a/crates/plist/tests/plist_line_endings.rs b/crates/plist/tests/plist_line_endings.rs new file mode 100644 index 000000000..3f8f82b50 --- /dev/null +++ b/crates/plist/tests/plist_line_endings.rs @@ -0,0 +1,17 @@ +use craft_plist::{self as plist, Value}; + +/// Literal line endings normalize before entities, including keys and CDATA. +#[test] +fn xml_line_endings_follow_xml_rules() { + let xml = "a\rba\r\nb\rc\nd e"; + for encoding in 0..3 { + let bytes = match encoding { + 1 => [vec![0xff, 0xfe], xml.encode_utf16().flat_map(u16::to_le_bytes).collect()].concat(), + 2 => [vec![0xfe, 0xff], xml.encode_utf16().flat_map(u16::to_be_bytes).collect()].concat(), + _ => xml.as_bytes().to_vec(), + }; + let value = plist::parse_xml(&bytes).unwrap(); + assert_eq!(value.get("a\nb").and_then(Value::as_str), Some("a\nb\nc\nd\ref\ng")); + } + assert!(plist::parse_xml(b"a\r\nb").is_err()); +} diff --git a/crates/plist/tests/plist_quoted_attributes.rs b/crates/plist/tests/plist_quoted_attributes.rs new file mode 100644 index 000000000..54cd042ef --- /dev/null +++ b/crates/plist/tests/plist_quoted_attributes.rs @@ -0,0 +1,22 @@ +use craft_plist::{self as plist, Value}; + +/// Quoted delimiters belong to attributes, including on empty elements. +#[test] +fn quoted_tag_delimiters_preserve_values() { + for quote in ['\'', '"'] { + let xml = format!( + "b{quote}>界{quote}>key/{quote}>valueempty/{quote}/>" + ); + let value = plist::parse_xml(xml.as_bytes()).unwrap(); + assert_eq!(value.get("key").and_then(Value::as_str), Some("value")); + assert_eq!(value.get("empty").and_then(Value::as_str), Some("")); + } +} + +/// A quote that never closes must not turn later markup into a value. +#[test] +fn unterminated_attribute_quotes_are_rejected() { + for xml in ["value", " 0, + b'G' => 1, + b'B' => 2, + 0 => break, + _ => continue, + }; + if depths[index] != 0 || pair[1] == 0 { + return 0; + } + depths[index] = pair[1]; + } + if depths[0] != 0 && depths.iter().all(|&depth| depth == depths[0]) { u32::from(depths[0]) } else { 0 } +} + fn picture_info(d: &Set) -> PictureInfo { let u = |t| d.u(t).unwrap_or(0) as u32; + let rgba = d.kind() == Some(st::RGBA_DESCRIPTOR); PictureInfo { stored_width: u(tag::STORED_WIDTH), stored_height: u(tag::STORED_HEIGHT), @@ -581,13 +605,13 @@ fn picture_info(d: &Set) -> PictureInfo { frame_layout: u(tag::FRAME_LAYOUT) as u8, field_dominance: u(tag::FIELD_DOMINANCE) as u8, aspect_ratio: d.rational(tag::ASPECT_RATIO).unwrap_or_default(), - component_depth: u(tag::COMPONENT_DEPTH), + component_depth: if rgba { d.get(tag::PIXEL_LAYOUT).map_or(0, rgba_component_depth) } else { u(tag::COMPONENT_DEPTH) }, horizontal_subsampling: u(tag::HORIZONTAL_SUBSAMPLING), vertical_subsampling: u(tag::VERTICAL_SUBSAMPLING), - black_ref_level: d.u(tag::BLACK_REF_LEVEL).map(|v| v as u32), - white_ref_level: d.u(tag::WHITE_REF_LEVEL).map(|v| v as u32), + black_ref_level: d.u(if rgba { tag::COMPONENT_MIN_REF } else { tag::BLACK_REF_LEVEL }).map(|v| v as u32), + white_ref_level: d.u(if rgba { tag::COMPONENT_MAX_REF } else { tag::WHITE_REF_LEVEL }).map(|v| v as u32), alpha_depth: u(tag::ALPHA_SAMPLE_DEPTH), - rgba: d.kind() == Some(st::RGBA_DESCRIPTOR), + rgba, picture_coding: d.ul(tag::PICTURE_ESSENCE_CODING), transfer_characteristic: d.ul(tag::TRANSFER_CHARACTERISTIC), coding_equations: d.ul(tag::CODING_EQUATIONS), @@ -1343,3 +1367,24 @@ mod fallback_tracks_tests { assert_eq!(tracks.len(), 1); } } + +#[cfg(test)] +mod rgba_layout_tests { + use super::rgba_component_depth; + + #[test] + fn only_complete_uniform_rgb_layouts_have_one_depth() { + let mut layout = [0; 16]; + layout[..8].copy_from_slice(&[b'B', 16, b'G', 16, b'R', 16, b'A', 8]); + assert_eq!(rgba_component_depth(&layout), 16); + assert_eq!(rgba_component_depth(&layout[..15]), 0); + layout[3] = 8; + assert_eq!(rgba_component_depth(&layout), 0); + layout[3] = 16; + layout[4] = b'G'; + assert_eq!(rgba_component_depth(&layout), 0); + layout[4] = 0; + assert_eq!(rgba_component_depth(&layout), 0); + assert_eq!(rgba_component_depth(&[0; 16]), 0); + } +} diff --git a/crates/media/src/mxf/meta.rs b/crates/media/src/mxf/meta.rs index d02afdd71..2b649d099 100644 --- a/crates/media/src/mxf/meta.rs +++ b/crates/media/src/mxf/meta.rs @@ -204,6 +204,8 @@ pub mod tag { pub const WHITE_REF_LEVEL: u16 = 0x3305; pub const ALPHA_SAMPLE_DEPTH: u16 = 0x3309; pub const PIXEL_LAYOUT: u16 = 0x3401; + pub const COMPONENT_MAX_REF: u16 = 0x3406; + pub const COMPONENT_MIN_REF: u16 = 0x3407; pub const AUDIO_SAMPLING_RATE: u16 = 0x3D03; pub const LOCKED: u16 = 0x3D02; pub const CHANNEL_COUNT: u16 = 0x3D07; diff --git a/crates/media/src/mxf/write/tests.rs b/crates/media/src/mxf/write/tests.rs index eac50a0e9..bc60423e9 100644 --- a/crates/media/src/mxf/write/tests.rs +++ b/crates/media/src/mxf/write/tests.rs @@ -434,6 +434,10 @@ fn cinemadng_op1a_reads_back() { assert_eq!(t.samples.len(), 6); let p = t.picture.as_ref().unwrap(); assert!(p.rgba, "RGBA picture essence descriptor (§7.2.2)"); + assert_eq!(p.component_depth, 16); + assert_eq!(p.black_ref_level, Some(0)); + assert_eq!(p.white_ref_level, Some(65_535)); + assert!(p.full_range()); assert_eq!((p.frame_width(), p.frame_height()), (64, 36)); assert!(p.picture_coding.is_none()); for i in 0..6 { From f78b0ac48439354229827c50c25a50fc19c71ea8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 11:18:07 -0500 Subject: [PATCH 058/150] Expose bounded XML plist parsing and cap malformed entity scans --- ATTRIBUTION.md | 2 + crates/plist/Cargo.toml | 2 +- crates/plist/README.md | 12 +++++- crates/plist/src/lib.rs | 6 ++- crates/plist/src/xml.rs | 91 ++++++++++++++++++++++++++++++++++------- 5 files changed, 93 insertions(+), 20 deletions(-) diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index ef3b98f31..43d543062 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -100,3 +100,5 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso ## Binary Apple property lists `crates/plist` is extracted from `storytold/craft-anything` (`crates/formats/src/plist.rs` and checked byte reads), at source main `88d1d9cac852aec1c7fd97886db451e005e49d2a`. MIT OR Apache-2.0; source license texts are retained in the crate. Primary import Git authors preserve Brandon Thomas, BFlat, RRFZ and Afloat. Regression imports preserve BFlat. XML, JSON projection, calendar formatting and app policy were not imported into the public API. + +The XML reader in `crates/plist/src/xml.rs` and its base64 compatibility helper are ported from Craft Anything `8f3b775157d331dd31cfa4da911e26d8856b8c77` (same MIT OR Apache-2.0 source licenses). Brandon Thomas is the primary import author; retained hardening credits include Afloat, BFlat, Rahul, RRFZ and NetterSilver. The finite-real fix preserves gmrmad1 as author from Craft Anything #1905 (`d85e104f`); quoted-attribute/line-ending regressions preserve RRFZ. Bounds/adaptation and bounded entity scanning are subsequent Dylan M. Taylor changes. App-specific JSON projections and keyed-archive interpretation remain outside this crate. diff --git a/crates/plist/Cargo.toml b/crates/plist/Cargo.toml index 7ae95aae0..7ffaf3480 100644 --- a/crates/plist/Cargo.toml +++ b/crates/plist/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "craft-plist" -description = "Bounded cross-platform binary Apple property-list reader" +description = "Bounded cross-platform binary and XML Apple property-list reader" version.workspace = true edition.workspace = true license.workspace = true diff --git a/crates/plist/README.md b/crates/plist/README.md index dc5c52007..5621ef96a 100644 --- a/crates/plist/README.md +++ b/crates/plist/README.md @@ -4,8 +4,16 @@ A cross-platform bounded binary (`bplist00`) reader, extracted from Craft Anythi `parse_binary_with_limits` accepts explicit input, object, expanded-node, nesting and decoded-byte limits. Recursion always has a hard ceiling of 256 even if a caller supplies a larger depth. Counts are checked before container reservation; allocations can return a resource error. Limits are per parse, not a promise of a precise process RSS cap. -This first PR contains reading only. XML parsing/writing and keyed-archive interpretation are not implemented. JSON projection, date formatting, OS file access and preference/locale selection are not part of this crate. Existing updater/Photo streaming `plist` users remain unchanged. +This crate contains reading only. Writing and keyed-archive interpretation are not implemented. JSON projection, date formatting, OS file access and preference/locale selection are not part of this crate. Existing updater/Photo streaming `plist` users remain unchanged. -Initial consumers: PhotoCraft's AppleLanguages binary-reader path; later Craft Anything binary parsing and LightCraft XML presets (after XML support/parity). Photo must retain its 4 MiB input policy and first-language selection. +Initial consumers: PhotoCraft's AppleLanguages binary-reader path; later Craft Anything binary parsing and LightCraft XML presets (after consumer parity). Photo must retain its 4 MiB input policy and first-language selection. Source: `storytold/craft-anything` at `88d1d9cac852aec1c7fd97886db451e005e49d2a`, `crates/formats/src/plist.rs` and checked byte reader. Source license files retained. Imported changes retain Brandon Thomas, BFlat, RRFZ and Afloat as primary Git authors; imported regression tests are BFlat's. No external specification implementation copied. + +## XML reader + +`parse_xml` and `parse_xml_with_limits` now read UTF-8 and BOM-tagged UTF-16 XML into the same `Value` tree. XML input/decoded-text size, tokens, values, nesting and cumulative key/leaf text are bounded. The independent token ceiling is eight times the smaller node/object allowance plus eight; unusually fragmented text can exhaust it before the value limit. This is not a precise RSS limit. The decoded-XML limit also includes markup; `` consumes its encoded text allowance, including whitespace. XML `max_objects` and `max_nodes` both cap values. + +The attributed Craft Anything reader includes its complete-document, declaration-skipping (no external entity fetching), quoted-tag and normalized-line-ending behavior. Nonfinite reals are refused using contributor PR #1905. Unknown or malformed character references retain legacy literal behavior; scanning is bounded before searching for a terminator. Base64 retains that reader's legacy optional-padding/ASCII-whitespace behavior, including unused trailing bits; it is not a signature/key-validation API. No writer, JSON projection, keyed-archive interpretation or app policy is added. + +XML source: Craft Anything `8f3b775157d331dd31cfa4da911e26d8856b8c77`, plus #1905 `d85e104f093a334961048881bf003351e1ac4992`. The import credits Brandon Thomas and the existing hardening contributors; #1905 is authored by gmrmad1 and the quoted-tag/line-ending tests by RRFZ. Consumer cutovers require their own policy/parity tests, especially LightCraft's preset mapping and Craft Anything's projections. diff --git a/crates/plist/src/lib.rs b/crates/plist/src/lib.rs index f3af79ddc..2547e73d8 100644 --- a/crates/plist/src/lib.rs +++ b/crates/plist/src/lib.rs @@ -1,10 +1,12 @@ -//! Bounded binary Apple property lists, independent of operating-system preferences. -//! XML and writing are intentionally not implemented in this first extraction. +//! Bounded binary and XML Apple property lists, independent of operating-system preferences. +//! Writing and application-specific projections remain with callers. //! File discovery, locale selection, bundle validation and application policy stay in callers. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] mod binary; mod bytes; +mod xml; pub use binary::{DateValue, Value, first_string_in_array, parse_binary, parse_binary_with_limits}; +pub use xml::{parse_xml, parse_xml_with_limits}; /// Caller-selected resource limits. Zero values refuse the corresponding resource. #[derive(Clone, Copy, Debug)] diff --git a/crates/plist/src/xml.rs b/crates/plist/src/xml.rs index 54fd9f895..52f5c4586 100644 --- a/crates/plist/src/xml.rs +++ b/crates/plist/src/xml.rs @@ -1,7 +1,5 @@ //! XML property-list reader ported from Craft Anything at 8f3b7751. -use crate::{DateValue, FormatError, Value}; -const MAX_NODES: usize = 100_000; -const MAX_DEPTH: usize = 64; +use crate::{DateValue, FormatError, Limits, Value}; fn decode_base64(text: &str, url_safe: bool) -> Option> { let mut out = Vec::with_capacity(text.len() / 4 * 3); let mut acc = 0u32; @@ -47,7 +45,9 @@ fn decode_base64(text: &str, url_safe: bool) -> Option> { Some(out) } -fn base64_decode(s: &str) -> Option> { decode_base64(s, false) } +fn base64_decode(s: &str) -> Option> { + decode_base64(s, false) +} #[derive(Debug, Clone, PartialEq)] enum Tok { Open(String), @@ -65,7 +65,7 @@ fn decode_entities(s: &str) -> String { while let Some(p) = rest.find('&') { out.push_str(rest.get(..p).unwrap_or("")); let after = rest.get(p..).unwrap_or(""); - let Some(end) = after.find(';').filter(|&e| e <= 12) else { + let Some(end) = after.as_bytes().iter().take(13).position(|b| *b == b';') else { out.push('&'); rest = after.get(1..).unwrap_or(""); continue; @@ -131,11 +131,11 @@ fn after_declaration(mut rest: &str) -> Result<&str, FormatError> { Err(FormatError::Truncated("declaration".into())) } -fn tokenize(text: &str) -> Result, FormatError> { +fn tokenize(text: &str, max_tokens: usize) -> Result, FormatError> { let mut toks = Vec::new(); let mut rest = text; while !rest.is_empty() { - if toks.len() > MAX_NODES { + if toks.len() >= max_tokens { return Err(FormatError::TooLarge("too many XML tokens".into())); } match rest.find('<') { @@ -198,6 +198,9 @@ fn tokenize(text: &str) -> Result, FormatError> { toks.push(Tok::Open(inner.split_whitespace().next().unwrap_or("").to_string())); } } + if toks.len() > max_tokens { + return Err(FormatError::TooLarge("too many XML tokens".into())); + } Ok(toks) } @@ -205,6 +208,8 @@ struct Xml { toks: Vec, pos: usize, nodes: usize, + limits: Limits, + bytes_left: usize, } impl Xml { @@ -223,7 +228,10 @@ impl Xml { let mut s = String::new(); loop { match self.next() { - Some(Tok::Text(t)) => s.push_str(&t), + Some(Tok::Text(t)) => { + self.bytes_left = self.bytes_left.checked_sub(t.len()).ok_or_else(|| FormatError::TooLarge("XML text/data bytes".into()))?; + s.push_str(&t); + } Some(Tok::Close(n)) if n == name => return Ok(s), Some(other) => return Err(FormatError::Malformed(format!("unexpected {other:?} inside <{name}>"))), None => return Err(FormatError::Truncated(format!("<{name}> is not closed"))), @@ -239,7 +247,7 @@ impl Xml { fn value(&mut self, depth: usize) -> Result { self.nodes += 1; - if depth > MAX_DEPTH || self.nodes > MAX_NODES { + if depth > self.limits.max_depth.min(256) || self.nodes > self.limits.max_nodes.min(self.limits.max_objects) { return Err(FormatError::TooLarge("plist nesting or size limit".into())); } self.skip_space(); @@ -267,7 +275,12 @@ impl Xml { } "real" => { let t = self.text_until("real")?; - t.trim().parse::().ok().filter(|v| v.is_finite()).map(Value::Real).ok_or_else(|| FormatError::Malformed(format!("bad real {t:?}"))) + t.trim() + .parse::() + .ok() + .filter(|v| v.is_finite()) + .map(Value::Real) + .ok_or_else(|| FormatError::Malformed(format!("bad real {t:?}"))) } "date" => Ok(Value::Date(DateValue::Text(self.text_until("date")?.trim().to_string()))), "data" => { @@ -330,12 +343,24 @@ fn decode_utf16_xml(data: &[u8], big_endian: bool) -> Result = data.chunks_exact(2).map(|b| if big_endian { u16::from_be_bytes([b[0], b[1]]) } else { u16::from_le_bytes([b[0], b[1]]) }).collect(); + let units: Vec = data.as_chunks::<2>().0.iter().map(|b| if big_endian { u16::from_be_bytes(*b) } else { u16::from_le_bytes(*b) }).collect(); String::from_utf16(&units).map_err(|_| FormatError::Malformed("invalid UTF-16 in XML plist".into())) } /// Parse an XML property list. pub fn parse_xml(data: &[u8]) -> Result { + parse_xml_with_limits(data, Limits::default()) +} + +/// Parse UTF-8 or BOM-tagged UTF-16 XML with explicit resource allowances. +/// Input and decoded XML text are bounded before tokenization; token count, value count, +/// nesting and cumulative leaf/key text are bounded separately. `max_objects` and +/// `max_nodes` both cap values. Nesting never exceeds 256 even with a larger caller limit. +/// Data text consumes its encoded size, conservatively including whitespace. +pub fn parse_xml_with_limits(data: &[u8], limits: Limits) -> Result { + if data.len() > limits.max_input_bytes { + return Err(FormatError::TooLarge("input bytes".into())); + } // Apple's Core Foundation reader accepts XML plists in UTF-16 as well as // UTF-8: it recognises both byte-order marks. `from_utf8_lossy` turns a // UTF-16 byte stream into NUL-separated text, so the encoding is settled @@ -347,11 +372,14 @@ pub fn parse_xml(data: &[u8]) -> Result { .map_err(|_| FormatError::Malformed("invalid UTF-8 in XML plist".into()))? .to_owned(), }; + if text.len() > limits.max_expanded_bytes { + return Err(FormatError::TooLarge("decoded XML bytes".into())); + } // XML normalizes literal line endings before parsing; character references such as // must still decode to a carriage return afterwards. let text = if text.contains('\r') { text.replace("\r\n", "\n").replace('\r', "\n") } else { text }; - let toks = tokenize(&text)?; - let mut x = Xml { toks, pos: 0, nodes: 0 }; + let toks = tokenize(&text, limits.max_nodes.min(limits.max_objects).saturating_mul(8).saturating_add(8))?; + let mut x = Xml { toks, pos: 0, nodes: 0, limits, bytes_left: limits.max_expanded_bytes }; x.skip_space(); let wrapped = matches!(x.peek(), Some(Tok::Open(n)) if n == "plist"); if wrapped { @@ -371,10 +399,9 @@ pub fn parse_xml(data: &[u8]) -> Result { Ok(value) } - #[cfg(test)] mod tests { -use super::*; + use super::*; #[test] fn xml_has_one_complete_root_with_only_markup_space_around_values() { for invalid in [ @@ -441,4 +468,38 @@ use super::*; assert_eq!(v.get("k"), Some(&Value::Real(want)), "{text}"); } } + + #[test] + fn malformed_entity_scan_is_bounded_and_preserves_text() { + for n in [4096, 65536, 262144] { + let text = "&".repeat(n); + assert_eq!(decode_entities(&text), text); + assert_eq!(decode_entities(&(text.clone() + ";")), text + ";"); + } + assert_eq!(decode_entities("é&missing; & A"), "é&missing; & \r A"); + } + #[test] + fn caller_limits_refuse_before_unbounded_tree_growth() { + let limits = Limits { max_input_bytes: 3, ..Limits::default() }; + assert!(matches!(parse_xml_with_limits(b"", limits), Err(FormatError::TooLarge(_)))); + let limits = Limits { max_nodes: 1, ..Limits::default() }; + assert!(matches!(parse_xml_with_limits(b"", limits), Err(FormatError::TooLarge(_)))); + let limits = Limits { max_depth: 0, ..Limits::default() }; + assert!(matches!(parse_xml_with_limits(b"", limits), Err(FormatError::TooLarge(_)))); + let limits = Limits { max_expanded_bytes: 8, ..Limits::default() }; + assert!(matches!(parse_xml_with_limits(b"abc", limits), Err(FormatError::TooLarge(_)))); + assert!(parse_xml_with_limits(b"", Limits::default()).is_ok()); + } + #[test] + fn data_preserves_existing_whitespace_padding_and_alphabet_rules() { + for (s, expected) in [(" Z g == ", Some(vec![102])), ("Zg", Some(vec![102])), ("Zg=", None), ("Zg==junk", None), ("_w==", None)] { + let xml = format!("{s}"); + assert_eq!(parse_xml(xml.as_bytes()).ok(), expected.map(Value::Data)); + } + } + #[test] + fn small_node_allowance_accepts_one_dictionary_entry() { + let limits = Limits { max_nodes: 2, max_objects: 2, ..Limits::default() }; + assert!(parse_xml_with_limits(b"kv", limits).is_ok()); + } } From f53ed0c505008412554322fda6d5edba79d9f860 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 11:18:38 -0500 Subject: [PATCH 059/150] Scope imported XML test assertions to test-only lint policy --- crates/plist/src/xml.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/plist/src/xml.rs b/crates/plist/src/xml.rs index 52f5c4586..c6cc4d022 100644 --- a/crates/plist/src/xml.rs +++ b/crates/plist/src/xml.rs @@ -401,6 +401,7 @@ pub fn parse_xml_with_limits(data: &[u8], limits: Limits) -> Result Date: Sun, 15 Nov 2026 16:18:57 +0000 Subject: [PATCH 060/150] Bound and expose partial glyph outlines --- crates/font-catalog/src/outline.rs | 92 ++++++++++++++++++++++++++---- 1 file changed, 81 insertions(+), 11 deletions(-) diff --git a/crates/font-catalog/src/outline.rs b/crates/font-catalog/src/outline.rs index 1f550f983..10a1ae8ab 100644 --- a/crates/font-catalog/src/outline.rs +++ b/crates/font-catalog/src/outline.rs @@ -26,6 +26,12 @@ pub enum OutlineError { /// The glyph's outline is malformed. #[error("cannot draw glyph outline: {0}")] Draw(#[from] skrifa::outline::DrawError), + /// The glyph emitted more path elements than the safety limit. + #[error("glyph outline exceeds the element limit")] + TooComplex, + /// The outline starts without a move command. + #[error("glyph outline has no initial move")] + InvalidPath, } impl<'a> FontOutline<'a> { @@ -58,30 +64,83 @@ impl<'a> FontOutline<'a> { } /// Draw a glyph in design units. Empty glyphs such as spaces can produce an empty path. + /// A malformed or excessively complex glyph returns an error instead of partial geometry. pub fn outline(&self, glyph: u32) -> Result { + let (path, status) = self.outline_recovering(glyph)?; + status.map(|()| path) + } + + /// Draw a glyph while retaining the bounded prefix produced before a draw error. This is + /// useful for consumers preserving existing rendering of damaged fonts. The inner result + /// reports whether the returned path is complete; the outer error means no glyph was found. + /// Paths stop growing at 100,000 elements, even when the font is untrusted. + pub fn outline_recovering(&self, glyph: u32) -> Result<(BezPath, Result<(), OutlineError>), OutlineError> { let outline = self.font.outline_glyphs().get(GlyphId::new(glyph)).ok_or(OutlineError::MissingGlyph)?; - let mut pen = PathPen(BezPath::new()); - outline.draw(DrawSettings::unhinted(Size::unscaled(), &self.location), &mut pen)?; - Ok(pen.0) + let mut pen = PathPen::new(); + let result = outline.draw(DrawSettings::unhinted(Size::unscaled(), &self.location), &mut pen); + let status = if pen.invalid { + Err(OutlineError::InvalidPath) + } else if pen.truncated { + Err(OutlineError::TooComplex) + } else { + result.map(|_| ()).map_err(OutlineError::Draw) + }; + Ok((pen.path, status)) } } -struct PathPen(BezPath); +const MAX_OUTLINE_ELEMENTS: usize = 100_000; +struct PathPen { + path: BezPath, + truncated: bool, + invalid: bool, +} +impl PathPen { + fn new() -> Self { + Self { path: BezPath::new(), truncated: false, invalid: false } + } + fn has_start(&mut self) -> bool { + if self.path.elements().is_empty() { + self.invalid = true; + false + } else { + true + } + } + fn room(&mut self) -> bool { + if self.path.elements().len() >= MAX_OUTLINE_ELEMENTS { + self.truncated = true; + false + } else { + true + } + } +} impl OutlinePen for PathPen { fn move_to(&mut self, x: f32, y: f32) { - self.0.move_to((f64::from(x), f64::from(y))); + if self.room() { + self.path.move_to((f64::from(x), f64::from(y))); + } } fn line_to(&mut self, x: f32, y: f32) { - self.0.line_to((f64::from(x), f64::from(y))); + if self.has_start() && self.room() { + self.path.line_to((f64::from(x), f64::from(y))); + } } fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) { - self.0.quad_to((f64::from(x1), f64::from(y1)), (f64::from(x), f64::from(y))); + if self.has_start() && self.room() { + self.path.quad_to((f64::from(x1), f64::from(y1)), (f64::from(x), f64::from(y))); + } } fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) { - self.0.curve_to((f64::from(x1), f64::from(y1)), (f64::from(x2), f64::from(y2)), (f64::from(x), f64::from(y))); + if self.has_start() && self.room() { + self.path.curve_to((f64::from(x1), f64::from(y1)), (f64::from(x2), f64::from(y2)), (f64::from(x), f64::from(y))); + } } fn close(&mut self) { - self.0.close_path(); + if self.has_start() && self.room() { + self.path.close_path(); + } } } @@ -99,14 +158,14 @@ mod tests { #[test] fn pen_preserves_geometry_and_upward_y() { - let mut pen = PathPen(BezPath::new()); + let mut pen = PathPen::new(); pen.move_to(1.0, 2.0); pen.line_to(3.0, 4.0); pen.quad_to(5.0, 6.0, 7.0, 8.0); pen.curve_to(9.0, 10.0, 11.0, 12.0, 13.0, 14.0); pen.close(); assert_eq!( - pen.0.elements(), + pen.path.elements(), &[ PathEl::MoveTo((1.0, 2.0).into()), PathEl::LineTo((3.0, 4.0).into()), @@ -117,6 +176,17 @@ mod tests { ); } + #[test] + fn pen_caps_path_growth() { + let mut pen = PathPen::new(); + pen.move_to(0.0, 0.0); + for _ in 0..MAX_OUTLINE_ELEMENTS { + pen.line_to(1.0, 2.0); + } + assert_eq!(pen.path.elements().len(), MAX_OUTLINE_ELEMENTS); + assert!(pen.truncated); + } + #[test] fn reads_craft_font_when_available() { let Ok(dir) = std::env::var("CRAFT_FONTS_DIR") else { return }; From c17840e099d7a4237770bca99efb2693f1692a8d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 11:21:36 -0500 Subject: [PATCH 061/150] Preserve LightCraft zero-padded entities with linear scanning Reuse LightCraft f786b87533e58c6d31c7f9f2897814904448f39a entity_end implementation and regression. XML numeric references are not arbitrarily length limited. --- crates/plist/README.md | 2 +- crates/plist/src/xml.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/plist/README.md b/crates/plist/README.md index 5621ef96a..a7adf6c79 100644 --- a/crates/plist/README.md +++ b/crates/plist/README.md @@ -14,6 +14,6 @@ Source: `storytold/craft-anything` at `88d1d9cac852aec1c7fd97886db451e005e49d2a` `parse_xml` and `parse_xml_with_limits` now read UTF-8 and BOM-tagged UTF-16 XML into the same `Value` tree. XML input/decoded-text size, tokens, values, nesting and cumulative key/leaf text are bounded. The independent token ceiling is eight times the smaller node/object allowance plus eight; unusually fragmented text can exhaust it before the value limit. This is not a precise RSS limit. The decoded-XML limit also includes markup; `` consumes its encoded text allowance, including whitespace. XML `max_objects` and `max_nodes` both cap values. -The attributed Craft Anything reader includes its complete-document, declaration-skipping (no external entity fetching), quoted-tag and normalized-line-ending behavior. Nonfinite reals are refused using contributor PR #1905. Unknown or malformed character references retain legacy literal behavior; scanning is bounded before searching for a terminator. Base64 retains that reader's legacy optional-padding/ASCII-whitespace behavior, including unused trailing bits; it is not a signature/key-validation API. No writer, JSON projection, keyed-archive interpretation or app policy is added. +The attributed Craft Anything reader includes its complete-document, declaration-skipping (no external entity fetching), quoted-tag and normalized-line-ending behavior. Nonfinite reals are refused using contributor PR #1905. Unknown or malformed character references retain legacy literal behavior; scanning stops at the next non-entity byte, preserving LightCraft's valid zero-padded numeric references without suffix rescans. Base64 retains that reader's legacy optional-padding/ASCII-whitespace behavior, including unused trailing bits; it is not a signature/key-validation API. No writer, JSON projection, keyed-archive interpretation or app policy is added. XML source: Craft Anything `8f3b775157d331dd31cfa4da911e26d8856b8c77`, plus #1905 `d85e104f093a334961048881bf003351e1ac4992`. The import credits Brandon Thomas and the existing hardening contributors; #1905 is authored by gmrmad1 and the quoted-tag/line-ending tests by RRFZ. Consumer cutovers require their own policy/parity tests, especially LightCraft's preset mapping and Craft Anything's projections. diff --git a/crates/plist/src/xml.rs b/crates/plist/src/xml.rs index c6cc4d022..be225d3fb 100644 --- a/crates/plist/src/xml.rs +++ b/crates/plist/src/xml.rs @@ -56,6 +56,12 @@ enum Tok { Text(String), } +// Ported from LightCraft f786b875: scan one entity-name run, never the remaining document. +fn entity_end(rest: &str) -> Option { + let n = rest.bytes().skip(1).take_while(|b| b.is_ascii_alphanumeric() || *b == b'#').count(); + (rest.as_bytes().get(n + 1) == Some(&b';')).then_some(n + 1) +} + fn decode_entities(s: &str) -> String { if !s.contains('&') { return s.to_string(); @@ -65,7 +71,7 @@ fn decode_entities(s: &str) -> String { while let Some(p) = rest.find('&') { out.push_str(rest.get(..p).unwrap_or("")); let after = rest.get(p..).unwrap_or(""); - let Some(end) = after.as_bytes().iter().take(13).position(|b| *b == b';') else { + let Some(end) = entity_end(after) else { out.push('&'); rest = after.get(1..).unwrap_or(""); continue; @@ -478,6 +484,7 @@ mod tests { assert_eq!(decode_entities(&(text.clone() + ";")), text + ";"); } assert_eq!(decode_entities("é&missing; & A"), "é&missing; & \r A"); + assert_eq!(decode_entities("Canon A"), "Canon A"); } #[test] fn caller_limits_refuse_before_unbounded_tree_growth() { From 4c17e51335192345d8e508287c26f17fead930ac Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:23:03 +0000 Subject: [PATCH 062/150] exr: fix RGBA writer lint --- crates/exr/src/rgba8.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/exr/src/rgba8.rs b/crates/exr/src/rgba8.rs index 0b26ab8c9..6dc4bde4c 100644 --- a/crates/exr/src/rgba8.rs +++ b/crates/exr/src/rgba8.rs @@ -81,7 +81,7 @@ pub fn write_rgba8(rgba: &[u8], w: u32, h: u32, alpha: bool, compression: Compre }; // channels in the alphabetical order the file layout requires let names: &[u8] = if alpha { b"ABGR" } else { b"BGR" }; - let line = (w as usize).checked_mul(names.len() * 2).ok_or_else(|| Error::FrameSize)?; + let line = (w as usize).checked_mul(names.len() * 2).ok_or(Error::FrameSize)?; // a chunk's byte count is a signed 32-bit field let lines = compression.lines(); if line.checked_mul(lines).and_then(|c| i32::try_from(c).ok()).is_none() { @@ -125,7 +125,7 @@ pub fn write_rgba8(rgba: &[u8], w: u32, h: u32, alpha: bool, compression: Compre let chunks = (h as usize).div_ceil(lines); let table = chunks.checked_mul(8); let body = (h as usize).checked_mul(line + 8); - let size = table.zip(body).and_then(|(t, b)| hd.len().checked_add(t)?.checked_add(b)).ok_or_else(|| Error::FrameSize)?; + let size = table.zip(body).and_then(|(t, b)| hd.len().checked_add(t)?.checked_add(b)).ok_or(Error::FrameSize)?; // the uncompressed size: compressed chunks only shrink it let mut out = Vec::with_capacity(size); out.extend_from_slice(&hd); From 0dab3133a2a58d321ade446f23592e7855b79e78 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 11:24:10 -0500 Subject: [PATCH 063/150] Allow consumers to retain opaque XML date text --- crates/plist/src/lib.rs | 2 +- crates/plist/src/xml.rs | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/crates/plist/src/lib.rs b/crates/plist/src/lib.rs index 2547e73d8..d668de277 100644 --- a/crates/plist/src/lib.rs +++ b/crates/plist/src/lib.rs @@ -6,7 +6,7 @@ mod binary; mod bytes; mod xml; pub use binary::{DateValue, Value, first_string_in_array, parse_binary, parse_binary_with_limits}; -pub use xml::{parse_xml, parse_xml_with_limits}; +pub use xml::{XmlOptions, parse_xml, parse_xml_with_limits, parse_xml_with_options}; /// Caller-selected resource limits. Zero values refuse the corresponding resource. #[derive(Clone, Copy, Debug)] diff --git a/crates/plist/src/xml.rs b/crates/plist/src/xml.rs index be225d3fb..d30b938d0 100644 --- a/crates/plist/src/xml.rs +++ b/crates/plist/src/xml.rs @@ -1,5 +1,18 @@ //! XML property-list reader ported from Craft Anything at 8f3b7751. use crate::{DateValue, FormatError, Limits, Value}; + +/// XML-specific representation policy, separate from resource allowances. +#[derive(Clone, Copy, Debug)] +pub struct XmlOptions { + /// Trim surrounding whitespace in date text, as the original Craft Anything reader does. + /// Disable for consumers that retain dates as opaque strings, such as Luminar presets. + pub trim_date_text: bool, +} +impl Default for XmlOptions { + fn default() -> Self { + Self { trim_date_text: true } + } +} fn decode_base64(text: &str, url_safe: bool) -> Option> { let mut out = Vec::with_capacity(text.len() / 4 * 3); let mut acc = 0u32; @@ -216,6 +229,7 @@ struct Xml { nodes: usize, limits: Limits, bytes_left: usize, + options: XmlOptions, } impl Xml { @@ -288,7 +302,10 @@ impl Xml { .map(Value::Real) .ok_or_else(|| FormatError::Malformed(format!("bad real {t:?}"))) } - "date" => Ok(Value::Date(DateValue::Text(self.text_until("date")?.trim().to_string()))), + "date" => { + let text = self.text_until("date")?; + Ok(Value::Date(DateValue::Text(if self.options.trim_date_text { text.trim().to_string() } else { text }))) + } "data" => { let t = self.text_until("data")?; base64_decode(&t).map(Value::Data).ok_or_else(|| FormatError::Malformed("bad base64 in ".into())) @@ -364,6 +381,11 @@ pub fn parse_xml(data: &[u8]) -> Result { /// `max_nodes` both cap values. Nesting never exceeds 256 even with a larger caller limit. /// Data text consumes its encoded size, conservatively including whitespace. pub fn parse_xml_with_limits(data: &[u8], limits: Limits) -> Result { + parse_xml_with_options(data, limits, XmlOptions::default()) +} + +/// Parse bounded XML using an explicit date-text representation policy. +pub fn parse_xml_with_options(data: &[u8], limits: Limits, options: XmlOptions) -> Result { if data.len() > limits.max_input_bytes { return Err(FormatError::TooLarge("input bytes".into())); } @@ -385,7 +407,7 @@ pub fn parse_xml_with_limits(data: &[u8], limits: Limits) -> Resultkv", limits).is_ok()); } + #[test] + fn callers_can_preserve_opaque_date_whitespace() { + let xml = b" 2026-01-01T00:00:00Z "; + assert_eq!(parse_xml(xml).unwrap(), Value::Date(DateValue::Text("2026-01-01T00:00:00Z".into()))); + assert_eq!( + parse_xml_with_options(xml, Limits::default(), XmlOptions { trim_date_text: false }).unwrap(), + Value::Date(DateValue::Text(" 2026-01-01T00:00:00Z ".into())) + ); + } } From 64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:26:40 +0000 Subject: [PATCH 064/150] audio: share interleaved PCM packet decoding --- crates/audio/src/lib.rs | 3 ++ crates/audio/src/pcm_interleaved.rs | 72 +++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 crates/audio/src/pcm_interleaved.rs diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index 33dcdee3b..2bc36c5a7 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -66,6 +66,9 @@ mod wav; feature = "vorbis-encode" ))] mod write; + +/// Interleaved PCM packet decoding. +pub mod pcm_interleaved; #[cfg(feature = "vorbis-encode")] pub use write::{OGG_BITRATES, encode_ogg}; diff --git a/crates/audio/src/pcm_interleaved.rs b/crates/audio/src/pcm_interleaved.rs new file mode 100644 index 000000000..0fcb7f140 --- /dev/null +++ b/crates/audio/src/pcm_interleaved.rs @@ -0,0 +1,72 @@ +//! Interleaved PCM packet decoding into caller-owned planar channels. + +/// The sample representation in an interleaved PCM packet. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Format { + /// Bits per sample. + pub bits: u16, + /// IEEE floating-point samples. + pub float: bool, + /// Big-endian sample bytes. + pub big_endian: bool, + /// Signed 8-bit integers (wider integer PCM is signed). + pub signed: bool, +} + +/// Append complete interleaved frames to caller-owned planar channels. Empty channel slices and +/// zero-bit formats append nothing. Incomplete frames are ignored; unsupported widths produce +/// silent samples, matching the container adapters that use this API. +pub fn decode_into(data: &[u8], cfg: Format, out: &mut [Vec]) { + let ch = out.len(); + if cfg.bits == 0 || ch == 0 { + return; + } + let bps = usize::from(cfg.bits).div_ceil(8); + let Some(frame_bytes) = bps.checked_mul(ch).filter(|&n| n > 0) else { return }; + let n = data.len() / frame_bytes; + for channel in out.iter_mut() { + channel.reserve(n); + } + // `n` is whole frames within `data`, so every sample slice below is in bounds. + for i in 0..n { + for (c, o) in out.iter_mut().enumerate() { + let off = (i * ch + c) * bps; + let b = &data[off..off + bps]; + let v = match (cfg.bits, cfg.float, cfg.big_endian) { + (32, true, false) => f32::from_le_bytes([b[0], b[1], b[2], b[3]]), + (32, true, true) => f32::from_be_bytes([b[0], b[1], b[2], b[3]]), + (64, true, false) => f64::from_le_bytes(b.try_into().unwrap_or([0; 8])) as f32, + (64, true, true) => f64::from_be_bytes(b.try_into().unwrap_or([0; 8])) as f32, + (8, _, _) => { + if cfg.signed { + b[0] as i8 as f32 / 128.0 + } else { + (b[0] as f32 - 128.0) / 128.0 + } + } + (16, _, false) => i16::from_le_bytes([b[0], b[1]]) as f32 / 32768.0, + (16, _, true) => i16::from_be_bytes([b[0], b[1]]) as f32 / 32768.0, + (24, _, false) => (i32::from_le_bytes([0, b[0], b[1], b[2]]) >> 8) as f32 / 8_388_608.0, + (24, _, true) => (i32::from_be_bytes([b[0], b[1], b[2], 0]) >> 8) as f32 / 8_388_608.0, + (32, false, false) => i32::from_le_bytes([b[0], b[1], b[2], b[3]]) as f32 / 2_147_483_648.0, + (32, false, true) => i32::from_be_bytes([b[0], b[1], b[2], b[3]]) as f32 / 2_147_483_648.0, + _ => 0.0, + }; + o.push(v); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn decodes_endian_and_interleaving_without_touching_partial_frames() { + let mut channels = vec![Vec::new(), Vec::new()]; + let fmt = Format { bits: 16, float: false, big_endian: true, signed: true }; + decode_into(&[0x40, 0, 0xc0, 0, 0xff], fmt, &mut channels); + assert_eq!(channels, [vec![0.5], vec![-0.5]]); + decode_into(&[0; 8], Format { bits: 0, ..fmt }, &mut channels); + assert_eq!(channels, [vec![0.5], vec![-0.5]]); + } +} From 58e2a887e6fe346deff2708513b0c1cd7c2af8c3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:28:01 +0000 Subject: [PATCH 065/150] Compare malformed CFF recovery with direct drawing --- crates/font-catalog/src/outline.rs | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/crates/font-catalog/src/outline.rs b/crates/font-catalog/src/outline.rs index 10a1ae8ab..d1b3aafe4 100644 --- a/crates/font-catalog/src/outline.rs +++ b/crates/font-catalog/src/outline.rs @@ -214,4 +214,38 @@ mod tests { assert!(!light_path.elements().is_empty()); assert_ne!(light_path, bold_path); } + + #[test] + fn malformed_cff_recovers_the_same_prefix_as_direct_drawing() { + use skrifa::raw::ps::cff::CffFontRef; + let Ok(dir) = std::env::var("CRAFT_FONTS_DIR") else { return }; + let mut bytes = std::fs::read(std::path::Path::new(&dir).join("fonts/noto-sans-cjk-sc/NotoSansCJKsc-Regular.otf")).unwrap(); + let (gid, start, len) = { + let f = FontRef::new(&bytes).unwrap(); + let cff = f.table_data(Tag::new(b"CFF ")).unwrap().as_bytes(); + let cff = CffFontRef::new(cff, 0, None).unwrap(); + let (gid, cs) = (0..cff.num_glyphs()) + .find_map(|gid| { + let cs = cff.charstrings().get(gid as usize)?; + (cs.len() >= 8).then_some((gid, cs)) + }) + .unwrap(); + (gid, cs.as_ptr() as usize - bytes.as_ptr() as usize, cs.len()) + }; + assert!(len >= 8); + // A move and line followed by a Type 2 callsubr without an operand. It emits a path + // prefix before the parser reports an error, without changing the font table offsets. + bytes[start..start + 8].copy_from_slice(&[139, 139, 21, 239, 139, 5, 10, 14]); + let f = FontRef::new(&bytes).unwrap(); + let mut old = PathPen::new(); + let result = + f.outline_glyphs().get(GlyphId::new(gid)).unwrap().draw(DrawSettings::unhinted(Size::unscaled(), &Location::default()), &mut old); + assert!(result.is_err()); + assert!(!old.path.elements().is_empty()); + let shared = FontOutline::from_bytes(&bytes, 0, &[]).unwrap(); + let (prefix, status) = shared.outline_recovering(gid).unwrap(); + assert!(matches!(status, Err(OutlineError::Draw(_)))); + assert_eq!(prefix, old.path); + assert!(matches!(shared.outline(gid), Err(OutlineError::Draw(_)))); + } } From b958ba705ff186d65b7baa671748f27c782f6902 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:29:38 +0000 Subject: [PATCH 066/150] audio: expose ASC prefix to ADTS configuration --- crates/audio/src/aac/config.rs | 51 ++++++++++++++++++++++++++++++++++ crates/audio/src/aac/mod.rs | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/crates/audio/src/aac/config.rs b/crates/audio/src/aac/config.rs index 88be1e214..b81f56f3e 100644 --- a/crates/audio/src/aac/config.rs +++ b/crates/audio/src/aac/config.rs @@ -343,6 +343,48 @@ impl AudioSpecificConfig { } } +/// Why an AudioSpecificConfig prefix cannot be written as an ADTS header. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum AdtsConfigError { + /// Fewer than two bytes were supplied. + Short, + /// The prefix names a profile, indexed rate or channel configuration ADTS cannot carry. + Unsupported { + /// Audio object type from the prefix. + object_type: u8, + /// Sampling frequency index from the prefix. + sf_index: u8, + /// Channel configuration from the prefix. + channel_config: u8, + }, +} + +/// ADTS fixed fields taken from the prefix of an AudioSpecificConfig. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct AdtsConfig { + object_type: u8, + sf_index: u8, + channel_config: u8, +} +impl AdtsConfig { + /// Parse only the first two ASC bytes. Unlike a decoder's full ASC parser, this accepts all + /// four object types representable by ADTS and rejects configurations needing a PCE. + pub fn from_asc_prefix(asc: &[u8]) -> std::result::Result { + let (Some(&a), Some(&b)) = (asc.first(), asc.get(1)) else { return Err(AdtsConfigError::Short) }; + let object_type = a >> 3; + let sf_index = ((a & 7) << 1) | (b >> 7); + let channel_config = (b >> 3) & 15; + if !(1..=4).contains(&object_type) || sf_index > 12 || !(1..=7).contains(&channel_config) { + return Err(AdtsConfigError::Unsupported { object_type, sf_index, channel_config }); + } + Ok(Self { object_type, sf_index, channel_config }) + } + /// Write a no-CRC ADTS header with variable-rate buffer fullness for one payload. + pub fn header(self, payload_len: usize) -> Result<[u8; 7]> { + AdtsHeader::write(self.object_type, self.sf_index, self.channel_config, payload_len, 0x7ff) + } +} + /// A parsed ADTS header. #[non_exhaustive] #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -519,6 +561,15 @@ mod tests { assert_eq!(AudioSpecificConfig::parse(&asc.to_bytes().unwrap()).unwrap(), asc); } + #[test] + fn adts_config_preserves_prefix_and_frame_limits() { + let config = AdtsConfig::from_asc_prefix(&[0x12, 0x10]).unwrap(); + assert_eq!(config.header(100).unwrap(), AdtsHeader::write(2, 4, 2, 100, 0x7ff).unwrap()); + assert!(config.header(8185).is_err()); + assert_eq!(AdtsConfig::from_asc_prefix(&[]), Err(AdtsConfigError::Short)); + assert!(matches!(AdtsConfig::from_asc_prefix(&[0x2a, 0x10]), Err(AdtsConfigError::Unsupported { object_type: 5, .. }))); + } + #[test] fn adts_headers_refuse_fields_they_cannot_hold() { for (object_type, sf_index, channel_config) in [(0, 4, 2), (5, 4, 2), (29, 4, 2), (2, 13, 2), (2, 15, 2), (2, 4, 8), (2, 4, 11), (2, 4, 12)] { diff --git a/crates/audio/src/aac/mod.rs b/crates/audio/src/aac/mod.rs index f423a4e22..fb79ae743 100644 --- a/crates/audio/src/aac/mod.rs +++ b/crates/audio/src/aac/mod.rs @@ -60,7 +60,7 @@ mod tns; #[cfg(feature = "aac-decode")] pub(crate) use config::split_adts_upto; -pub use config::{AdtsHeader, AudioSpecificConfig, ElementType, ProgramConfig, config_layout, split_adts}; +pub use config::{AdtsConfig, AdtsConfigError, AdtsHeader, AudioSpecificConfig, ElementType, ProgramConfig, config_layout, split_adts}; #[cfg(feature = "aac-decode")] pub use decoder::Decoder; #[cfg(feature = "aac-encode")] From 25b1cf2b6f4f4fc4942af6b795456df9f4dd0304 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:30:22 +0000 Subject: [PATCH 067/150] audio: expose validated ADTS configuration fields --- crates/audio/src/aac/config.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/audio/src/aac/config.rs b/crates/audio/src/aac/config.rs index b81f56f3e..3d12b0343 100644 --- a/crates/audio/src/aac/config.rs +++ b/crates/audio/src/aac/config.rs @@ -362,9 +362,12 @@ pub enum AdtsConfigError { /// ADTS fixed fields taken from the prefix of an AudioSpecificConfig. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct AdtsConfig { - object_type: u8, - sf_index: u8, - channel_config: u8, + /// Audio object type, 1 through 4. + pub object_type: u8, + /// Indexed sample rate, 0 through 12. + pub sf_index: u8, + /// Channel configuration, 1 through 7. + pub channel_config: u8, } impl AdtsConfig { /// Parse only the first two ASC bytes. Unlike a decoder's full ASC parser, this accepts all From d16ee7926c8403ded5ea3209bc2ab14b1570281a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:36:35 +0000 Subject: [PATCH 068/150] raw: share line-restart lossless JPEG decoder --- crates/raw/src/lib.rs | 3 + crates/raw/src/ljpeg_lines.rs | 543 ++++++++++++++++++++++++++++++++++ 2 files changed, 546 insertions(+) create mode 100644 crates/raw/src/ljpeg_lines.rs diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index 602ec29b4..4fa225364 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -97,6 +97,8 @@ pub mod kelvin; mod kodak; pub mod lens_metadata; pub mod ljpeg; +/// Optimized lossless JPEG decoder for streams with line-aligned restarts. +pub mod ljpeg_lines; #[cfg(feature = "arw")] mod llvc; #[cfg(feature = "mef")] @@ -643,6 +645,7 @@ pub fn orientation(bytes: &[u8]) -> Option { } // A damaged/unsupported pixel IFD must not hide usable orientation metadata from preview // fallback. CR3 keeps IFD0 in its CMT1 box; TIFF-based raws keep it at the file start. + #[cfg(feature = "cr3")] if probe(bytes) == Some(RawFormat::Cr3) { return cr3::orientation(bytes); } diff --git a/crates/raw/src/ljpeg_lines.rs b/crates/raw/src/ljpeg_lines.rs new file mode 100644 index 000000000..e169fd893 --- /dev/null +++ b/crates/raw/src/ljpeg_lines.rs @@ -0,0 +1,543 @@ +//! Lossless JPEG decoding: ITU-T T.81 | ISO/IEC 10918-1 Annex H (process 14, Huffman coded, +//! "SOF3"), the compression DNG calls lossless JPEG (Compression = 7). +//! +//! - Frame header (B.2.2): 2–16-bit precision, 1–4 components, each sampled 1×1. +//! - Huffman tables (B.2.4.2, Annex C) decoded with a 9-bit lookup and the F.2.2.3 code-length +//! walk for longer codes. +//! - Scan header (B.2.3): one scan holding every component, predictor `Ss` 1–7 (Table H.1) and +//! point transform `Al`. +//! - Prediction (H.1.2.1): the first line (and the first line after a restart) predicts from the +//! left neighbour with 2^(P − Pt − 1) for its first sample; later lines predict their first +//! sample from the one above and the rest with the selected predictor. Differences are added +//! modulo 2^16 (H.1.2.2) and the SSSS = 16 category is 32768 with no extra bits. +//! - Restart intervals (DRI, RSTn) that span whole lines. + +/// Why a lossless JPEG stream cannot be decoded. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// A required byte is missing. + #[error("truncated lossless JPEG")] + Truncated, + /// The sample allocation exceeds the caller's bound. + #[error("lossless JPEG is too large")] + TooLarge, + /// Invalid stream structure. + #[error("{0}")] + Malformed(&'static str), + /// A valid variation outside the line-restart decoder's support. + #[error("{0}")] + Unsupported(String), +} +/// Result of the line-restart decoder. +pub type Result = std::result::Result; + +/// A decoded lossless JPEG image. +#[derive(Debug)] +pub struct Decoded { + pub width: usize, + pub height: usize, + pub components: usize, + pub precision: u8, + pub predictor: u8, + /// Samples, interleaved by component, line after line, shifted back up by the point transform. + pub samples: Vec, +} + +const LOOKUP_BITS: u32 = 9; +/// Bits of the combined code + difference lookup. +const FULL_BITS: u32 = 13; +/// A combined lookup entry: bit 8 set, the bits used in 0–7, the difference in 16–31. +const FULL_VALID: u32 = 0x100; + +/// A Huffman table for decoding (T.81 Annex C and F.2.2.3). +#[derive(Clone)] +struct Huffman { + /// For codes of up to LOOKUP_BITS bits: (length, value) indexed by the next LOOKUP_BITS bits; + /// length 0 means "longer code". + fast: Vec<(u8, u8)>, + maxcode: [i32; 17], + valptr: [i32; 17], + mincode: [i32; 17], + values: Vec, + /// For a code and its extra bits that fit in FULL_BITS together (difference categories + /// 0–15): `FULL_VALID | used bits | difference << 16`, indexed by the next FULL_BITS bits; + /// 0 means "decode the slow way". + full: Vec, +} + +impl Huffman { + fn new(bits: &[u8; 16], values: Vec) -> Result { + // C.2: code sizes and codes + let mut sizes = Vec::new(); + for (l, &n) in bits.iter().enumerate() { + for _ in 0..n { + sizes.push(l as u8 + 1); + } + } + if sizes.len() != values.len() || sizes.is_empty() { + return Err(Error::Malformed("lossless JPEG: Huffman table counts")); + } + let mut codes = Vec::with_capacity(sizes.len()); + let mut code: u32 = 0; + let mut si = sizes[0]; + let mut k = 0; + while k < sizes.len() { + while k < sizes.len() && sizes[k] == si { + codes.push(code); + code += 1; + k += 1; + } + // the codes of one length must fit in that length + if code > (1u32 << si) { + return Err(Error::Malformed("lossless JPEG: Huffman table over-subscribed")); + } + code <<= 1; + si += 1; + } + // F.2.2.3 decoder tables + let mut maxcode = [-1i32; 17]; + let mut valptr = [0i32; 17]; + let mut mincode = [0i32; 17]; + let mut j = 0usize; + for l in 1..=16usize { + let n = usize::from(bits[l - 1]); + if n == 0 { + maxcode[l] = -1; + } else { + valptr[l] = j as i32; + mincode[l] = codes[j] as i32; + j += n; + maxcode[l] = codes[j - 1] as i32; + } + } + let mut fast = vec![(0u8, 0u8); 1 << LOOKUP_BITS]; + for (i, (&c, &s)) in codes.iter().zip(&sizes).enumerate() { + let s = u32::from(s); + if s <= LOOKUP_BITS { + let shift = LOOKUP_BITS - s; + let base = (c << shift) as usize; + for f in fast.iter_mut().skip(base).take(1 << shift) { + *f = (s as u8, values[i]); + } + } + } + let mut full = vec![0u32; 1 << FULL_BITS]; + for (&c, (&s, &ssss)) in codes.iter().zip(sizes.iter().zip(&values)) { + let (s, ssss) = (u32::from(s), u32::from(ssss)); + if ssss >= 16 || s + ssss > FULL_BITS { + continue; + } + let total = s + ssss; + for v in 0..1u32 << ssss { + let diff = match ssss { + 0 => 0, + k if v < 1 << (k - 1) => v as i32 - (1 << k) + 1, + _ => v as i32, + }; + let entry = FULL_VALID | total | (u32::from(diff as i16 as u16) << 16); + let shift = FULL_BITS - total; + let base = (((c << ssss) | v) << shift) as usize; + for f in full.iter_mut().skip(base).take(1 << shift) { + *f = entry; + } + } + } + Ok(Huffman { fast, maxcode, valptr, mincode, values, full }) + } +} + +/// Entropy-coded data reader: removes stuffed zero bytes and stops at markers (feeding zeros). +struct Bits<'a> { + data: &'a [u8], + pos: usize, + acc: u64, + n: u32, + /// The marker that ended the data (after 0xFF), if one was met. + marker: Option, +} + +impl<'a> Bits<'a> { + fn new(data: &'a [u8], pos: usize) -> Self { + Bits { data, pos, acc: 0, n: 0, marker: None } + } + + fn fill(&mut self) { + // fast path: eight bytes without a 0xFF (no stuffing, no marker) go in at once + if self.marker.is_none() + && let Some(b) = self.data.get(self.pos..self.pos + 8) + { + let chunk = u64::from_be_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]]); + let inv = !chunk; + let has_ff = inv.wrapping_sub(0x0101_0101_0101_0101) & !inv & 0x8080_8080_8080_8080 != 0; + if !has_ff { + let take = (64 - self.n) / 8; + if take > 0 { + self.acc |= (chunk >> (64 - 8 * take)) << (64 - self.n - 8 * take); + self.pos += take as usize; + self.n += 8 * take; + return; + } + } + } + while self.n <= 56 { + let mut byte = 0u8; + if self.marker.is_none() { + match self.data.get(self.pos) { + Some(&0xFF) => match self.data.get(self.pos + 1) { + Some(&0x00) => { + byte = 0xFF; + self.pos += 2; + } + Some(&m) => self.marker = Some(m), + None => self.marker = Some(0xD9), + }, + Some(&b) => { + byte = b; + self.pos += 1; + } + None => self.marker = Some(0xD9), + } + } + self.acc |= u64::from(byte) << (56 - self.n); + self.n += 8; + } + } + + fn peek(&mut self, k: u32) -> u32 { + if self.n < k { + self.fill(); + } + (self.acc >> (64 - k)) as u32 + } + + fn skip(&mut self, k: u32) { + self.acc <<= k; + self.n -= k; + } + + fn bits(&mut self, k: u32) -> u32 { + if k == 0 { + return 0; + } + let v = self.peek(k); + self.skip(k); + v + } + + /// Decode one Huffman symbol (F.2.2.3). + fn decode(&mut self, h: &Huffman) -> Result { + let p = self.peek(LOOKUP_BITS) as usize; + if let Some(&(len, v)) = h.fast.get(p) + && len > 0 + { + self.skip(u32::from(len)); + return Ok(v); + } + let mut code = self.bits(1) as i32; + for l in 1..=16usize { + if code <= h.maxcode[l] { + let i = h.valptr[l] + code - h.mincode[l]; + return h.values.get(i as usize).copied().ok_or(Error::Malformed("lossless JPEG: bad Huffman code")); + } + code = (code << 1) | self.bits(1) as i32; + } + Err(Error::Malformed("lossless JPEG: bad Huffman code")) + } + + /// Skip to the byte after the next RSTn marker, starting a new bit stream there. + fn restart(&mut self) -> Result<()> { + // the decoder may have looked ahead into fill bits; find the marker in the bytes + let mut p = self.pos; + loop { + match (self.data.get(p), self.data.get(p + 1)) { + (Some(0xFF), Some(&m)) if (0xD0..=0xD7).contains(&m) => { + *self = Bits::new(self.data, p + 2); + return Ok(()); + } + (Some(_), Some(_)) => p += 1, + _ => return Err(Error::Malformed("lossless JPEG: missing restart marker")), + } + } + } +} + +struct Component { + id: u8, + table: usize, +} + +/// Decode a lossless JPEG stream. `max_samples` caps the picture (width × height × components). +pub fn decode(data: &[u8], max_samples: usize) -> Result { + if data.get(0..2) != Some(&[0xFF, 0xD8]) { + return Err(Error::Malformed("lossless JPEG: no SOI marker")); + } + let mut pos = 2usize; + let mut tables: [Option; 4] = [None, None, None, None]; + let mut frame: Option<(u8, usize, usize, Vec)> = None; + let mut restart_interval = 0usize; + loop { + // markers may be preceded by fill bytes 0xFF + while data.get(pos) == Some(&0xFF) && data.get(pos + 1) == Some(&0xFF) { + pos += 1; + } + let (Some(&0xFF), Some(&m)) = (data.get(pos), data.get(pos + 1)) else { + return Err(Error::Malformed("lossless JPEG: expected a marker")); + }; + pos += 2; + if m == 0xD9 { + return Err(Error::Malformed("lossless JPEG: no scan")); + } + if (0xD0..=0xD8).contains(&m) || m == 0x01 { + continue; // markers without a segment + } + let len_b = data.get(pos..pos + 2).ok_or(Error::Truncated)?; + let len = usize::from(u16::from_be_bytes([len_b[0], len_b[1]])); + if len < 2 { + return Err(Error::Malformed("lossless JPEG: segment length")); + } + let seg = data.get(pos + 2..pos + len).ok_or(Error::Truncated)?; + match m { + 0xC3 => { + let [p, y0, y1, x0, x1, nf, rest @ ..] = seg else { + return Err(Error::Malformed("lossless JPEG: short frame header")); + }; + let (precision, nf) = (*p, usize::from(*nf)); + let height = usize::from(u16::from_be_bytes([*y0, *y1])); + let width = usize::from(u16::from_be_bytes([*x0, *x1])); + if !(2..=16).contains(&precision) { + return Err(Error::Unsupported(format!("lossless JPEG precision {precision}"))); + } + if height == 0 { + return Err(Error::Unsupported("lossless JPEG with the height in a DNL marker".into())); + } + if width == 0 || !(1..=4).contains(&nf) || rest.len() < nf * 3 { + return Err(Error::Malformed("lossless JPEG: frame header")); + } + let mut comps = Vec::with_capacity(nf); + for c in rest.as_chunks::<3>().0.iter().take(nf) { + if c[1] != 0x11 { + return Err(Error::Unsupported("subsampled lossless JPEG components".into())); + } + comps.push(Component { id: c[0], table: 0 }); + } + frame = Some((precision, width, height, comps)); + } + 0xC4 => { + let mut s = seg; + while !s.is_empty() { + let [tcth, counts @ ..] = s else { break }; + let bits: [u8; 16] = counts.get(..16).and_then(|b| b.try_into().ok()).ok_or(Error::Truncated)?; + let n: usize = bits.iter().map(|&b| usize::from(b)).sum(); + let values = counts.get(16..16 + n).ok_or(Error::Truncated)?.to_vec(); + let th = usize::from(tcth & 0x0F); + if tcth >> 4 != 0 || th > 3 { + return Err(Error::Malformed("lossless JPEG: Huffman table class or id")); + } + if values.iter().any(|&v| v > 16) { + return Err(Error::Malformed("lossless JPEG: difference category above 16")); + } + tables[th] = Some(Huffman::new(&bits, values)?); + s = counts.get(16 + n..).unwrap_or(&[]); + } + } + 0xDD => { + let r = seg.get(0..2).ok_or(Error::Truncated)?; + restart_interval = usize::from(u16::from_be_bytes([r[0], r[1]])); + } + 0xDA => { + let Some((precision, width, height, mut comps)) = frame.take() else { + return Err(Error::Malformed("lossless JPEG: scan before the frame header")); + }; + let ns = usize::from(*seg.first().ok_or(Error::Truncated)?); + if ns != comps.len() { + return Err(Error::Unsupported("lossless JPEG with more than one scan".into())); + } + for k in 0..ns { + let c = seg.get(1 + 2 * k..3 + 2 * k).ok_or(Error::Truncated)?; + let comp = comps.iter_mut().find(|x| x.id == c[0]).ok_or(Error::Malformed("lossless JPEG: unknown scan component"))?; + comp.table = usize::from(c[1] >> 4); + if comp.table > 3 { + return Err(Error::Malformed("lossless JPEG: Huffman table id")); + } + } + let tail = seg.get(1 + 2 * ns..4 + 2 * ns).ok_or(Error::Truncated)?; + let (predictor, pt) = (tail[0], tail[2] & 0x0F); + if !(1..=7).contains(&predictor) { + return Err(Error::Unsupported(format!("lossless JPEG predictor {predictor}"))); + } + if pt >= precision { + return Err(Error::Malformed("lossless JPEG: point transform")); + } + let n = width.checked_mul(height).and_then(|v| v.checked_mul(comps.len())).ok_or(Error::TooLarge)?; + if n > max_samples { + return Err(Error::TooLarge); + } + let mut hufs = Vec::with_capacity(comps.len()); + for c in &comps { + hufs.push(tables.get(c.table).and_then(Option::as_ref).ok_or(Error::Malformed("lossless JPEG: missing Huffman table"))?); + } + // every sample costs at least one bit of entropy-coded data (a Huffman code is at + // least one bit long), so a frame bigger than the data cannot be real + if n.div_ceil(8) > data.len().saturating_sub(pos + len) { + return Err(Error::Truncated); + } + let params = ScanParams { width, height, precision, predictor, pt, restart: restart_interval }; + let samples = scan(data, pos + len, &hufs, ¶ms)?; + return Ok(Decoded { width, height, components: comps.len(), precision, predictor, samples }); + } + 0xC0..=0xC2 | 0xC5..=0xC7 | 0xC9..=0xCB | 0xCD..=0xCF => { + return Err(Error::Unsupported("lossy (DCT) JPEG in a DNG".into())); + } + _ => {} // APPn, COM, DQT … are skipped + } + pos += len; + } +} + +/// Frame and scan parameters needed to decode one scan. +struct ScanParams { + width: usize, + height: usize, + precision: u8, + predictor: u8, + pt: u8, + /// Restart interval in samples per component (0 = none). + restart: usize, +} + +/// The next difference (F.2.2.1 and H.1.2.2): category and extra bits from the combined table +/// when they fit, else symbol by symbol. +#[inline(always)] +fn difference(bits: &mut Bits, h: &Huffman) -> Result { + let e = h.full.get(bits.peek(FULL_BITS) as usize).copied().unwrap_or(0); + if e & FULL_VALID != 0 { + bits.skip(e & 0xFF); + return Ok(i32::from((e >> 16) as u16 as i16)); + } + Ok(match u32::from(bits.decode(h)?) { + 0 => 0, + 16 => 32768, + s => { + let v = bits.bits(s) as i32; + if v < (1 << (s - 1)) { v - (1 << s) + 1 } else { v } + } + }) +} + +/// Decode the entropy-coded data of one scan (H.1.2). +fn scan(data: &[u8], start: usize, hufs: &[&Huffman], params: &ScanParams) -> Result> { + let ScanParams { width, height, precision, predictor, pt, restart } = *params; + let nc = hufs.len(); + let row_len = width * nc; + if restart != 0 && !restart.is_multiple_of(width) { + return Err(Error::Unsupported("lossless JPEG restart interval that splits a line".into())); + } + let lines_per_interval = if restart == 0 { usize::MAX } else { restart / width }; + let mut out = vec![0u16; row_len * height]; + let mut bits = Bits::new(data, start); + let initial = 1i32 << (precision - pt - 1); + let mut line_in_interval = 0usize; + for y in 0..height { + if line_in_interval == lines_per_interval { + bits.restart()?; + line_in_interval = 0; + } + let (above, cur) = out.split_at_mut(y * row_len); + let cur = cur.get_mut(..row_len).ok_or(Error::TooLarge)?; + let above = if line_in_interval == 0 { None } else { above.get((y - 1) * row_len..) }; + match predictor { + 1 => line::<1>(&mut bits, hufs, cur, above, initial)?, + 2 => line::<2>(&mut bits, hufs, cur, above, initial)?, + 3 => line::<3>(&mut bits, hufs, cur, above, initial)?, + 4 => line::<4>(&mut bits, hufs, cur, above, initial)?, + 5 => line::<5>(&mut bits, hufs, cur, above, initial)?, + 6 => line::<6>(&mut bits, hufs, cur, above, initial)?, + _ => line::<7>(&mut bits, hufs, cur, above, initial)?, + } + line_in_interval += 1; + } + if pt > 0 { + for s in &mut out { + *s <<= pt; + } + } + Ok(out) +} + +/// One line of samples (H.1.2.1): the first line of a restart interval (`above` is `None`) +/// predicts from the left with `initial` for its first sample; other lines predict their first +/// sample from above and the rest with predictor `P`. +#[inline(always)] +fn line(bits: &mut Bits, hufs: &[&Huffman], cur: &mut [u16], above: Option<&[u16]>, initial: i32) -> Result<()> { + let nc = hufs.len(); + let row_len = cur.len(); + let above = match above { + Some(a) => Some(a.get(..row_len).ok_or(Error::TooLarge)?), + None => None, + }; + for (c, h) in hufs.iter().enumerate() { + let d = difference(bits, h)?; + let px = match above { + Some(a) => i32::from(a[c]), + None => initial, + }; + cur[c] = ((px + d) & 0xFFFF) as u16; + } + let mut i = nc; + match above { + None => { + while i < row_len { + for h in hufs { + let d = difference(bits, h)?; + cur[i] = ((i32::from(cur[i - nc]) + d) & 0xFFFF) as u16; + i += 1; + } + } + } + Some(a) => { + while i < row_len { + for h in hufs { + let d = difference(bits, h)?; + let px = predict(P, i32::from(cur[i - nc]), i32::from(a[i]), i32::from(a[i - nc])); + cur[i] = ((px + d) & 0xFFFF) as u16; + i += 1; + } + } + } + } + Ok(()) +} + +/// Table H.1 predictors (`ra` left, `rb` above, `rc` above left). +#[inline(always)] +pub fn predict(p: u8, ra: i32, rb: i32, rc: i32) -> i32 { + match p { + 1 => ra, + 2 => rb, + 3 => rc, + 4 => ra + rb - rc, + 5 => ra + ((rb - rc) >> 1), + 6 => rb + ((ra - rc) >> 1), + _ => (ra + rb) >> 1, + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn agrees_with_full_decoder_for_line_aligned_restarts() { + let pixels: Vec = (0..96).map(|i| ((i * 137) % 4096) as u16).collect(); + for predictor in 1..=7 { + for restart in [0, 12, 24] { + let bytes = crate::ljpeg::encode(&pixels, 12, 8, 1, 12, predictor, restart); + let fast = decode(&bytes, pixels.len()).unwrap(); + let full = crate::ljpeg::decode(&bytes, pixels.len()).unwrap(); + assert_eq!(fast.samples, pixels, "predictor {predictor}, restart {restart}"); + assert_eq!(fast.samples, full.data); + } + } + let bytes = crate::ljpeg::encode(&pixels, 12, 8, 1, 12, 1, 0); + assert_eq!(decode(&bytes, pixels.len() - 1).unwrap_err(), Error::TooLarge); + } +} From 4d51586a45321b101f608276ce7ccbcec3f1fab3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:38:31 +0000 Subject: [PATCH 069/150] Stream glyph outlines into caller-owned pens --- crates/font-catalog/src/lib.rs | 2 +- crates/font-catalog/src/outline.rs | 152 +++++++++++++++++++++++++++-- 2 files changed, 146 insertions(+), 8 deletions(-) diff --git a/crates/font-catalog/src/lib.rs b/crates/font-catalog/src/lib.rs index 7da4ca310..3e0e7376e 100644 --- a/crates/font-catalog/src/lib.rs +++ b/crates/font-catalog/src/lib.rs @@ -32,7 +32,7 @@ pub mod system; pub use catalog::{Catalog, FontData, FontFileError, Limits, Lister, LoadedFace, Match, Watch}; pub use env::{FontEnv, Os, font_dirs, font_store_dir, office_font_dirs}; pub use fontconfig::fontconfig_dirs; -pub use outline::{FontOutline, OutlineError}; +pub use outline::{FontOutline, GlyphPen, OutlineError}; pub use scan::{FaceInfo, LocalizedName, Source, is_font_file, style_italic, style_weight, style_width}; #[cfg(test)] diff --git a/crates/font-catalog/src/outline.rs b/crates/font-catalog/src/outline.rs index d1b3aafe4..c5a79b72e 100644 --- a/crates/font-catalog/src/outline.rs +++ b/crates/font-catalog/src/outline.rs @@ -14,6 +14,40 @@ pub struct FontOutline<'a> { location: Location, } +/// Receives unhinted glyph commands in design units (y up). Implementations own their +/// storage and resource policy; drawing writes directly to the sink without an intermediate path. +pub trait GlyphPen { + /// Start a contour. + fn move_to(&mut self, x: f32, y: f32); + /// Add a straight segment. + fn line_to(&mut self, x: f32, y: f32); + /// Add a quadratic segment. + fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32); + /// Add a cubic segment. + fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32); + /// Close the current contour. + fn close(&mut self); +} + +struct Sink<'a>(&'a mut dyn GlyphPen); +impl OutlinePen for Sink<'_> { + fn move_to(&mut self, x: f32, y: f32) { + self.0.move_to(x, y); + } + fn line_to(&mut self, x: f32, y: f32) { + self.0.line_to(x, y); + } + fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) { + self.0.quad_to(x1, y1, x, y); + } + fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) { + self.0.curve_to(x1, y1, x2, y2, x, y); + } + fn close(&mut self) { + self.0.close(); + } +} + /// Why a font or glyph outline could not be read. #[derive(Debug, thiserror::Error)] pub enum OutlineError { @@ -63,6 +97,14 @@ impl<'a> FontOutline<'a> { self.font.charmap().map(character).map(|id| id.to_u32()) } + /// Stream an unhinted glyph into the caller's sink. On an error, commands already sent + /// remain in the sink. The sink owns any allocation and enforces its own resource limit. + pub fn draw_to(&self, glyph: u32, pen: &mut dyn GlyphPen) -> Result<(), OutlineError> { + let outline = self.font.outline_glyphs().get(GlyphId::new(glyph)).ok_or(OutlineError::MissingGlyph)?; + outline.draw(DrawSettings::unhinted(Size::unscaled(), &self.location), &mut Sink(pen))?; + Ok(()) + } + /// Draw a glyph in design units. Empty glyphs such as spaces can produce an empty path. /// A malformed or excessively complex glyph returns an error instead of partial geometry. pub fn outline(&self, glyph: u32) -> Result { @@ -150,6 +192,24 @@ mod tests { use kurbo::PathEl; use std::sync::Arc; + impl GlyphPen for PathPen { + fn move_to(&mut self, x: f32, y: f32) { + OutlinePen::move_to(self, x, y); + } + fn line_to(&mut self, x: f32, y: f32) { + OutlinePen::line_to(self, x, y); + } + fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) { + OutlinePen::quad_to(self, x1, y1, x, y); + } + fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) { + OutlinePen::curve_to(self, x1, y1, x2, y2, x, y); + } + fn close(&mut self) { + OutlinePen::close(self); + } + } + #[test] fn rejects_invalid_face_and_collection_index() { let data = FontData { bytes: Arc::from(&b"not a font"[..]), index: 0 }; @@ -159,11 +219,11 @@ mod tests { #[test] fn pen_preserves_geometry_and_upward_y() { let mut pen = PathPen::new(); - pen.move_to(1.0, 2.0); - pen.line_to(3.0, 4.0); - pen.quad_to(5.0, 6.0, 7.0, 8.0); - pen.curve_to(9.0, 10.0, 11.0, 12.0, 13.0, 14.0); - pen.close(); + OutlinePen::move_to(&mut pen, 1.0, 2.0); + OutlinePen::line_to(&mut pen, 3.0, 4.0); + OutlinePen::quad_to(&mut pen, 5.0, 6.0, 7.0, 8.0); + OutlinePen::curve_to(&mut pen, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0); + OutlinePen::close(&mut pen); assert_eq!( pen.path.elements(), &[ @@ -179,9 +239,9 @@ mod tests { #[test] fn pen_caps_path_growth() { let mut pen = PathPen::new(); - pen.move_to(0.0, 0.0); + OutlinePen::move_to(&mut pen, 0.0, 0.0); for _ in 0..MAX_OUTLINE_ELEMENTS { - pen.line_to(1.0, 2.0); + OutlinePen::line_to(&mut pen, 1.0, 2.0); } assert_eq!(pen.path.elements().len(), MAX_OUTLINE_ELEMENTS); assert!(pen.truncated); @@ -246,6 +306,84 @@ mod tests { let (prefix, status) = shared.outline_recovering(gid).unwrap(); assert!(matches!(status, Err(OutlineError::Draw(_)))); assert_eq!(prefix, old.path); + let mut streamed = PathPen::new(); + assert!(matches!(shared.draw_to(gid, &mut streamed), Err(OutlineError::Draw(_)))); + assert_eq!(streamed.path, old.path); assert!(matches!(shared.outline(gid), Err(OutlineError::Draw(_)))); } + + #[test] + fn streaming_preserves_a_large_valid_cff_outline() { + let Ok(dir) = std::env::var("CRAFT_FONTS_DIR") else { return }; + let source = std::fs::read(std::path::Path::new(&dir).join("fonts/noto-sans-cjk-sc/NotoSansCJKsc-Regular.otf")).unwrap(); + let source = FontRef::new(&source).unwrap(); + // Minimal CFF with a single generated Type 2 charstring. The glyph has over 100,000 + // valid line segments; no binary fixture is copied into this repository. + let mut program = vec![139, 139, 21]; // rmoveto 0,0 + for _ in 0..100_001 { + program.extend_from_slice(&[140, 6]); + } // hlineto 1 + program.push(14); // endchar + let mut cff = vec![1, 0, 4, 4, 0, 1, 1, 1, 2, b'X', 0, 1, 1, 1, 7, 29, 0, 0, 0, 25, 17, 0, 0, 0, 0]; + cff.extend_from_slice(&[0, 1, 4]); + cff.extend_from_slice(&1u32.to_be_bytes()); + cff.extend_from_slice(&((program.len() as u32) + 1).to_be_bytes()); + cff.extend_from_slice(&program); + let mut builder = write_fonts::FontBuilder::new(); + for tag in [b"head", b"maxp", b"hhea", b"hmtx"] { + builder.add_raw(write_fonts::types::Tag::new(tag), source.table_data(Tag::new(tag)).unwrap().as_bytes().to_vec()); + } + assert!(skrifa::raw::ps::cff::CffFontRef::new(&cff, 0, None).is_ok(), "synthetic CFF parses"); + builder.add_raw(write_fonts::types::Tag::new(b"CFF "), cff); + let bytes = builder.build(); + let shared = FontOutline::from_bytes(&bytes, 0, &[]).unwrap(); + struct Count(usize); + impl GlyphPen for Count { + fn move_to(&mut self, _: f32, _: f32) { + self.0 += 1; + } + fn line_to(&mut self, _: f32, _: f32) { + self.0 += 1; + } + fn quad_to(&mut self, _: f32, _: f32, _: f32, _: f32) { + self.0 += 1; + } + fn curve_to(&mut self, _: f32, _: f32, _: f32, _: f32, _: f32, _: f32) { + self.0 += 1; + } + fn close(&mut self) { + self.0 += 1; + } + } + impl OutlinePen for Count { + fn move_to(&mut self, x: f32, y: f32) { + GlyphPen::move_to(self, x, y); + } + fn line_to(&mut self, x: f32, y: f32) { + GlyphPen::line_to(self, x, y); + } + fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) { + GlyphPen::quad_to(self, x1, y1, x, y); + } + fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) { + GlyphPen::curve_to(self, x1, y1, x2, y2, x, y); + } + fn close(&mut self) { + GlyphPen::close(self); + } + } + let mut old = Count(0); + FontRef::new(&bytes) + .unwrap() + .outline_glyphs() + .get(GlyphId::new(0)) + .unwrap() + .draw(DrawSettings::unhinted(Size::unscaled(), &Location::default()), &mut old) + .unwrap(); + let mut count = Count(0); + shared.draw_to(0, &mut count).unwrap(); + assert_eq!(count.0, old.0); + assert!(count.0 > MAX_OUTLINE_ELEMENTS); + assert!(matches!(shared.outline(0), Err(OutlineError::TooComplex))); + } } From 8b9010ae5356b6588eb22c2e1ca4b4ae12647939 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:39:48 +0000 Subject: [PATCH 070/150] tiff: share bounded lazy classic directory reader --- crates/tiff/src/classic.rs | 421 +++++++++++++++++++++++++++++++++++++ crates/tiff/src/lib.rs | 3 + 2 files changed, 424 insertions(+) create mode 100644 crates/tiff/src/classic.rs diff --git a/crates/tiff/src/classic.rs b/crates/tiff/src/classic.rs new file mode 100644 index 000000000..3408eadcd --- /dev/null +++ b/crates/tiff/src/classic.rs @@ -0,0 +1,421 @@ +//! The TIFF container (TIFF 6.0 §2, as DNG uses it): byte order, image file directories (IFDs), +//! their entries and values, and the SubIFD tree. +//! +//! Every offset and count comes from the file and is checked before use: an entry whose value lies +//! outside the file is an error when it is read, the walk visits at most [`MAX_IFDS`] directories, +//! never the same one twice (cyclic chains end), and goes at most [`MAX_DEPTH`] SubIFD levels deep. + +use std::collections::HashSet; + +/// Why a classic TIFF directory could not be read. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// The input has no supported classic TIFF header. + #[error("not a classic TIFF")] + NotTiff, + /// The directory or a referenced value is invalid. + #[error("{0}")] + Malformed(&'static str), +} +/// Result of the bounded classic TIFF reader. +pub type Result = std::result::Result; + +/// Most IFDs one file may have (main chain and SubIFDs together). +pub const MAX_IFDS: usize = 64; +/// Deepest SubIFD nesting followed. +pub const MAX_DEPTH: usize = 4; +/// Invalid SubIFD edges count too: otherwise a file can make failed offset probes quadratic. +const MAX_IFD_ATTEMPTS: usize = MAX_IFDS * 16; +/// SubIFD references (zero, repeated and invalid ones included) accepted over the whole walk, +/// counted before a list is read: many directories can name the same long SubIFD table. +const MAX_SUB_IFD_REFS: usize = MAX_IFD_ATTEMPTS; + +/// TIFF field types (TIFF 6.0 §2, plus type 13 IFD from TIFF Technical Note 1). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Type { + Byte, + Ascii, + Short, + Long, + Rational, + SByte, + Undefined, + SShort, + SLong, + SRational, + Float, + Double, + Ifd, +} + +impl Type { + fn from_code(c: u16) -> Option { + Some(match c { + 1 => Type::Byte, + 2 => Type::Ascii, + 3 => Type::Short, + 4 => Type::Long, + 5 => Type::Rational, + 6 => Type::SByte, + 7 => Type::Undefined, + 8 => Type::SShort, + 9 => Type::SLong, + 10 => Type::SRational, + 11 => Type::Float, + 12 => Type::Double, + 13 => Type::Ifd, + _ => return None, + }) + } + + /// Bytes per value. + pub fn size(self) -> usize { + match self { + Type::Byte | Type::Ascii | Type::SByte | Type::Undefined => 1, + Type::Short | Type::SShort => 2, + Type::Long | Type::SLong | Type::Float | Type::Ifd => 4, + Type::Rational | Type::SRational | Type::Double => 8, + } + } +} + +/// One directory entry. +#[derive(Clone, Copy, Debug)] +pub struct Entry { + pub tag: u16, + pub ty: Type, + pub count: u32, + /// Where the value bytes start: inside the entry when they fit in four bytes, else the offset + /// the entry holds (not yet checked against the file length). + value_at: u64, +} + +/// One image file directory. +#[derive(Clone, Debug, Default)] +pub struct Ifd { + /// File offset of the directory. + pub offset: u64, + pub entries: Vec, + /// Nesting: 0 for the main chain, 1 for its SubIFDs, … + pub depth: usize, +} + +impl Ifd { + pub fn get(&self, tag: u16) -> Option<&Entry> { + self.entries.iter().find(|e| e.tag == tag) + } +} + +/// A TIFF file in memory. +#[derive(Clone, Copy)] +pub struct Tiff<'a> { + pub data: &'a [u8], + pub big_endian: bool, +} + +impl<'a> Tiff<'a> { + /// Read the header. Classic TIFF only (BigTIFF, version 43, is not used by DNG). + pub fn new(data: &'a [u8]) -> Result<(Tiff<'a>, u64)> { + let big_endian = match data.get(0..4) { + Some(b"II*\0") => false, + Some(b"MM\0*") => true, + _ => return Err(Error::NotTiff), + }; + let t = Tiff { data, big_endian }; + let first = u64::from(t.u32_at(4)?); + Ok((t, first)) + } + + fn bytes_at(&self, off: u64, len: usize) -> Result<&'a [u8]> { + let start = usize::try_from(off).map_err(|_| Error::Malformed("offset outside the file"))?; + let end = start.checked_add(len).ok_or(Error::Malformed("offset outside the file"))?; + self.data.get(start..end).ok_or(Error::Malformed("offset outside the file")) + } + + pub fn u16_of(&self, b: [u8; 2]) -> u16 { + if self.big_endian { u16::from_be_bytes(b) } else { u16::from_le_bytes(b) } + } + + pub fn u32_of(&self, b: [u8; 4]) -> u32 { + if self.big_endian { u32::from_be_bytes(b) } else { u32::from_le_bytes(b) } + } + + fn u16_at(&self, off: u64) -> Result { + let b = self.bytes_at(off, 2)?; + Ok(self.u16_of([b[0], b[1]])) + } + + fn u32_at(&self, off: u64) -> Result { + let b = self.bytes_at(off, 4)?; + Ok(self.u32_of([b[0], b[1], b[2], b[3]])) + } + + /// Read the IFD at `off`: its entries and the offset of the next IFD (0 at the end of a chain). + pub fn read_ifd(&self, off: u64, depth: usize) -> Result<(Ifd, u64)> { + let n = self.u16_at(off)?; + let mut entries = Vec::with_capacity(usize::from(n)); + for i in 0..u64::from(n) { + let at = off + 2 + i * 12; + let e = self.bytes_at(at, 12)?; + let tag = self.u16_of([e[0], e[1]]); + let code = self.u16_of([e[2], e[3]]); + let count = self.u32_of([e[4], e[5], e[6], e[7]]); + // unknown field types are skipped (TIFF 6.0: readers ignore them) + let Some(ty) = Type::from_code(code) else { continue }; + let len = u64::from(count).saturating_mul(ty.size() as u64); + let value_at = if len <= 4 { at + 8 } else { u64::from(self.u32_of([e[8], e[9], e[10], e[11]])) }; + entries.push(Entry { tag, ty, count, value_at }); + } + let next = u64::from(self.u32_at(off + 2 + u64::from(n) * 12)?); + Ok((Ifd { offset: off, entries, depth }, next)) + } + + /// Every IFD of the file: the main chain from `first` and, depth first, the SubIFDs (tag 330) of + /// each. Bounded by [`MAX_IFDS`] and [`MAX_DEPTH`]; a directory reached twice ends that branch. + pub fn all_ifds(&self, first: u64) -> Result> { + let mut out: Vec = Vec::new(); + let mut seen = HashSet::new(); + let mut attempts = 0usize; + let mut sub_refs = 0usize; + let mut stack: Vec<(u64, usize)> = vec![(first, 0)]; + while let Some((off, depth)) = stack.pop() { + let mut next = off; + while next != 0 { + if !seen.insert(next) { + break; + } + attempts += 1; + if attempts > MAX_IFD_ATTEMPTS { + return Err(Error::Malformed("too many IFD offsets")); + } + if out.len() >= MAX_IFDS { + return Err(Error::Malformed("too many IFDs")); + } + let (ifd, after) = match self.read_ifd(next, depth) { + Ok(x) => x, + // a broken directory ends the chain; the first one must be readable + Err(e) if out.is_empty() => return Err(e), + Err(_) => break, + }; + if depth < MAX_DEPTH + && let Some(sub) = ifd.get(tags::SUB_IFDS) + { + sub_refs = sub_refs.saturating_add(usize::try_from(sub.count).unwrap_or(usize::MAX)); + if sub_refs > MAX_SUB_IFD_REFS { + return Err(Error::Malformed("too many SubIFD references")); + } + if let Ok(offs) = self.uints(sub) { + for o in offs.into_iter().rev().filter(|o| *o != 0 && !seen.contains(o)) { + stack.push((o, depth + 1)); + } + } + } + out.push(ifd); + next = after; + } + } + Ok(out) + } + + /// The value bytes of an entry. + pub fn value_bytes(&self, e: &Entry) -> Result<&'a [u8]> { + let len = (e.count as usize).checked_mul(e.ty.size()).ok_or(Error::Malformed("entry too large"))?; + self.bytes_at(e.value_at, len) + } + + /// The `i`-th value of a numeric entry as f64 (rationals divided; a zero denominator is an + /// error). + fn number(&self, e: &Entry, b: &[u8], i: usize) -> Result { + let s = e.ty.size(); + let v = b.get(i * s..i * s + s).ok_or(Error::Malformed("entry too short"))?; + let u16v = |o: usize| self.u16_of([v[o], v[o + 1]]); + let u32v = |o: usize| self.u32_of([v[o], v[o + 1], v[o + 2], v[o + 3]]); + Ok(match e.ty { + Type::Byte | Type::Undefined => f64::from(v[0]), + Type::SByte => f64::from(v[0] as i8), + Type::Short => f64::from(u16v(0)), + Type::SShort => f64::from(u16v(0) as i16), + Type::Long | Type::Ifd => f64::from(u32v(0)), + Type::SLong => f64::from(u32v(0) as i32), + Type::Rational => { + let d = u32v(4); + if d == 0 { + return Err(Error::Malformed("rational with a zero denominator")); + } + f64::from(u32v(0)) / f64::from(d) + } + Type::SRational => { + let d = u32v(4) as i32; + if d == 0 { + return Err(Error::Malformed("rational with a zero denominator")); + } + f64::from(u32v(0) as i32) / f64::from(d) + } + Type::Float => f64::from(f32::from_bits(u32v(0))), + Type::Double => { + let mut a = [0u8; 8]; + a.copy_from_slice(&v[..8]); + if self.big_endian { f64::from_be_bytes(a) } else { f64::from_le_bytes(a) } + } + Type::Ascii => return Err(Error::Malformed("text where a number belongs")), + }) + } + + /// All values of a numeric entry as f64. Non-finite values are an error. + pub fn floats(&self, e: &Entry) -> Result> { + let b = self.value_bytes(e)?; + let mut out = Vec::with_capacity(e.count as usize); + for i in 0..e.count as usize { + let v = self.number(e, b, i)?; + if !v.is_finite() { + return Err(Error::Malformed("non-finite number")); + } + out.push(v); + } + Ok(out) + } + + /// All values of an unsigned integer entry (BYTE, SHORT, LONG, IFD). + pub fn uints(&self, e: &Entry) -> Result> { + if !matches!(e.ty, Type::Byte | Type::Undefined | Type::Short | Type::Long | Type::Ifd) { + return Err(Error::Malformed("expected an unsigned integer field")); + } + let b = self.value_bytes(e)?; + let s = e.ty.size(); + Ok(b.chunks_exact(s) + .map(|v| match s { + 1 => u64::from(v[0]), + 2 => u64::from(self.u16_of([v[0], v[1]])), + _ => u64::from(self.u32_of([v[0], v[1], v[2], v[3]])), + }) + .collect()) + } + + /// The first value of an unsigned integer entry. + pub fn uint(&self, e: &Entry) -> Result { + self.uints(e)?.first().copied().ok_or(Error::Malformed("empty field")) + } + + /// An ASCII entry up to its first NUL, lossily decoded. + pub fn ascii(&self, e: &Entry) -> Result { + let b = self.value_bytes(e)?; + let end = b.iter().position(|&c| c == 0).unwrap_or(b.len()); + Ok(String::from_utf8_lossy(&b[..end]).trim().to_string()) + } +} + +/// Tag numbers (TIFF 6.0, TIFF/EP, DNG 1.7, CinemaDNG). +pub mod tags { + pub const NEW_SUBFILE_TYPE: u16 = 254; + pub const IMAGE_WIDTH: u16 = 256; + pub const IMAGE_LENGTH: u16 = 257; + pub const BITS_PER_SAMPLE: u16 = 258; + pub const COMPRESSION: u16 = 259; + pub const PHOTOMETRIC: u16 = 262; + pub const MAKE: u16 = 271; + pub const MODEL: u16 = 272; + pub const STRIP_OFFSETS: u16 = 273; + pub const SAMPLES_PER_PIXEL: u16 = 277; + pub const ROWS_PER_STRIP: u16 = 278; + pub const STRIP_BYTE_COUNTS: u16 = 279; + pub const PLANAR_CONFIGURATION: u16 = 284; + pub const PREDICTOR: u16 = 317; + pub const TILE_WIDTH: u16 = 322; + pub const TILE_LENGTH: u16 = 323; + pub const TILE_OFFSETS: u16 = 324; + pub const TILE_BYTE_COUNTS: u16 = 325; + pub const SUB_IFDS: u16 = 330; + pub const SAMPLE_FORMAT: u16 = 339; + pub const CFA_REPEAT_PATTERN_DIM: u16 = 33421; + pub const CFA_PATTERN: u16 = 33422; + pub const DNG_VERSION: u16 = 50706; + pub const UNIQUE_CAMERA_MODEL: u16 = 50708; + pub const CFA_PLANE_COLOR: u16 = 50710; + pub const CFA_LAYOUT: u16 = 50711; + pub const LINEARIZATION_TABLE: u16 = 50712; + pub const BLACK_LEVEL_REPEAT_DIM: u16 = 50713; + pub const BLACK_LEVEL: u16 = 50714; + pub const BLACK_LEVEL_DELTA_H: u16 = 50715; + pub const BLACK_LEVEL_DELTA_V: u16 = 50716; + pub const WHITE_LEVEL: u16 = 50717; + pub const DEFAULT_CROP_ORIGIN: u16 = 50719; + pub const DEFAULT_CROP_SIZE: u16 = 50720; + pub const COLOR_MATRIX_1: u16 = 50721; + pub const COLOR_MATRIX_2: u16 = 50722; + pub const CAMERA_CALIBRATION_1: u16 = 50723; + pub const CAMERA_CALIBRATION_2: u16 = 50724; + pub const ANALOG_BALANCE: u16 = 50727; + pub const AS_SHOT_NEUTRAL: u16 = 50728; + pub const AS_SHOT_WHITE_XY: u16 = 50729; + pub const BASELINE_EXPOSURE: u16 = 50730; + pub const CALIBRATION_ILLUMINANT_1: u16 = 50778; + pub const CALIBRATION_ILLUMINANT_2: u16 = 50779; + pub const ACTIVE_AREA: u16 = 50829; + pub const OPCODE_LIST_1: u16 = 51008; + pub const OPCODE_LIST_2: u16 = 51009; + pub const OPCODE_LIST_3: u16 = 51022; + pub const FORWARD_MATRIX_1: u16 = 50964; + pub const FORWARD_MATRIX_2: u16 = 50965; + /// CinemaDNG: SMPTE ST 12-1 time codes, 8 bytes each. + pub const TIME_CODES: u16 = 51043; + /// CinemaDNG: frames per second (SRATIONAL). + pub const FRAME_RATE: u16 = 51044; + /// CinemaDNG: the camera's reel name. + pub const REEL_NAME: u16 = 51081; +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A little-endian TIFF: a chain of `ifds` directories, each with one SubIFDs entry of + /// `count` LONGs, all naming the same table (of zeros, or of `target`). + fn chain(ifds: usize, count: u32, target: u32) -> Vec { + let mut b = b"II*\0".to_vec(); + b.extend_from_slice(&8u32.to_le_bytes()); + let table = 8 + ifds * 18; + for i in 0..ifds { + let next = if i + 1 < ifds { 8 + (i + 1) * 18 } else { 0 }; + b.extend_from_slice(&1u16.to_le_bytes()); + b.extend_from_slice(&tags::SUB_IFDS.to_le_bytes()); + b.extend_from_slice(&4u16.to_le_bytes()); + b.extend_from_slice(&count.to_le_bytes()); + b.extend_from_slice(&(table as u32).to_le_bytes()); + b.extend_from_slice(&(next as u32).to_le_bytes()); + } + for _ in 0..count.max(2) { + b.extend_from_slice(&target.to_le_bytes()); + } + b + } + + #[test] + fn shared_sub_ifd_tables_are_budgeted_before_they_are_queued() { + // 60 directories each naming one 100 000-entry table: 6 million queued edges before + let b = chain(60, 100_000, 0); + let (t, first) = Tiff::new(&b).unwrap(); + assert!(matches!(t.all_ifds(first), Err(Error::Malformed("too many SubIFD references")))); + } + + #[test] + fn matches_general_reader_for_a_simple_ifd_chain() { + let bytes = chain(3, 0, 0); + let (reader, first) = Tiff::new(&bytes).unwrap(); + let classic = reader.all_ifds(first).unwrap(); + let general = crate::Tiff::parse(&bytes).unwrap(); + assert_eq!(classic.len(), general.ifds.len()); + assert!(classic.iter().all(|ifd| ifd.get(tags::SUB_IFDS).is_some())); + } + + #[test] + fn ordinary_sub_ifds_are_still_walked() { + // one directory whose SubIFD is the same directory: visited once + let b = chain(1, 1, 8); + let (t, first) = Tiff::new(&b).unwrap(); + assert_eq!(t.all_ifds(first).unwrap().len(), 1); + // a short chain with a few zero SubIFD offsets + let b = chain(3, 4, 0); + let (t, first) = Tiff::new(&b).unwrap(); + assert_eq!(t.all_ifds(first).unwrap().len(), 3); + } +} diff --git a/crates/tiff/src/lib.rs b/crates/tiff/src/lib.rs index bd095f4f0..0e9d595c7 100644 --- a/crates/tiff/src/lib.rs +++ b/crates/tiff/src/lib.rs @@ -22,6 +22,9 @@ #![forbid(unsafe_code)] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +/// Bounded classic TIFF directory reader with lazy value access. +pub mod classic; + pub mod image; pub mod makernote; pub mod packbits; From c0fd5d610a3dc221e96a0f5329c440d02743966a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 11:40:54 -0500 Subject: [PATCH 071/150] Use fixed-size array chunks for RGBA layout inspection --- crates/media/src/mxf/file.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/media/src/mxf/file.rs b/crates/media/src/mxf/file.rs index 9a0928077..e23471307 100644 --- a/crates/media/src/mxf/file.rs +++ b/crates/media/src/mxf/file.rs @@ -578,7 +578,7 @@ fn rgba_component_depth(layout: &[u8]) -> u32 { return 0; } let mut depths = [0u8; 3]; - for pair in layout.chunks_exact(2) { + for pair in layout.as_chunks::<2>().0 { let index = match pair[0] { b'R' => 0, b'G' => 1, From cbc8279a804c831b57bec0d1eebdcfe39c5781df Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:00:55 +0000 Subject: [PATCH 072/150] Share bounded legacy control envelopes --- crates/jsonrpc/src/control.rs | 50 +++++++++++++++++++++++++++++++++++ crates/jsonrpc/src/lib.rs | 2 ++ 2 files changed, 52 insertions(+) create mode 100644 crates/jsonrpc/src/control.rs diff --git a/crates/jsonrpc/src/control.rs b/crates/jsonrpc/src/control.rs new file mode 100644 index 000000000..f52e294b7 --- /dev/null +++ b/crates/jsonrpc/src/control.rs @@ -0,0 +1,50 @@ +//! Bounded compatibility framing for single-line `{id, method, params}` control requests. +//! This is not JSON-RPC: replies use `{id, ok, result}` or `{id, ok, error}` and every +//! request, including one without an id, receives a reply. Method routing stays with the caller. +use serde_json::{Value, json}; +use std::fmt::Display; + +/// Process one bounded control line. Blank lines yield no response. Missing `params` becomes null; +/// arbitrary JSON ids are echoed and malformed JSON has a null id, matching the legacy protocol. +pub fn process_line(line: &str, max_bytes: usize, dispatch: impl FnOnce(&str, &Value) -> Result) -> Option { + let trimmed = line.trim(); + if trimmed.is_empty() { + return None; + } + if line.len() > max_bytes { + return Some(error(None, "request exceeds byte limit")); + } + let value: Value = match serde_json::from_str(trimmed) { + Ok(v) => v, + Err(e) => return Some(error(None, &format!("bad request envelope: {e}"))), + }; + let id = value.get("id").cloned(); + let Some(method) = value.get("method").and_then(Value::as_str) else { + return Some(error(id, "missing `method`")); + }; + let params = value.get("params").cloned().unwrap_or(Value::Null); + Some(match dispatch(method, ¶ms) { + Ok(result) => json!({"id": id, "ok": true, "result": result}).to_string(), + Err(e) => error(id, &e.to_string()), + }) +} + +fn error(id: Option, message: &str) -> String { + json!({"id": id, "ok": false, "error": message}).to_string() +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn preserves_legacy_responses_and_null_defaults() { + let call = + |method: &str, params: &Value| -> Result { if method == "echo" { Ok(params.clone()) } else { Err("unknown") } }; + assert_eq!(process_line(" ", 20, call), None); + assert_eq!(process_line(r#"{"id":[1],"method":"echo"}"#, 100, call).unwrap(), r#"{"id":[1],"ok":true,"result":null}"#); + assert_eq!(process_line(r#"{"method":"bad"}"#, 100, call).unwrap(), r#"{"error":"unknown","id":null,"ok":false}"#); + assert!(process_line("{", 100, call).unwrap().contains("bad request envelope:")); + assert_eq!(process_line(r#"{"id":3}"#, 100, call).unwrap(), r#"{"error":"missing `method`","id":3,"ok":false}"#); + assert!(process_line("abcdef", 2, call).unwrap().contains("byte limit")); + } +} diff --git a/crates/jsonrpc/src/lib.rs b/crates/jsonrpc/src/lib.rs index 9f8128d1d..a674394a8 100644 --- a/crates/jsonrpc/src/lib.rs +++ b/crates/jsonrpc/src/lib.rs @@ -8,6 +8,8 @@ #![forbid(unsafe_code)] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +pub mod control; + use serde_json::{Value, json}; use std::io::BufRead; From 72da2d5d056fdc1e7565ebe56014375c71c65d3f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:01:28 +0000 Subject: [PATCH 073/150] Enforce control line limit before trimming --- crates/jsonrpc/src/control.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/jsonrpc/src/control.rs b/crates/jsonrpc/src/control.rs index f52e294b7..ed3729329 100644 --- a/crates/jsonrpc/src/control.rs +++ b/crates/jsonrpc/src/control.rs @@ -7,13 +7,13 @@ use std::fmt::Display; /// Process one bounded control line. Blank lines yield no response. Missing `params` becomes null; /// arbitrary JSON ids are echoed and malformed JSON has a null id, matching the legacy protocol. pub fn process_line(line: &str, max_bytes: usize, dispatch: impl FnOnce(&str, &Value) -> Result) -> Option { + if line.len() > max_bytes { + return Some(error(None, "request exceeds byte limit")); + } let trimmed = line.trim(); if trimmed.is_empty() { return None; } - if line.len() > max_bytes { - return Some(error(None, "request exceeds byte limit")); - } let value: Value = match serde_json::from_str(trimmed) { Ok(v) => v, Err(e) => return Some(error(None, &format!("bad request envelope: {e}"))), From d6d4f2bd4cc3578754cb42951b296eef69e4afe1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:01:38 +0000 Subject: [PATCH 074/150] Cover oversized control lines --- crates/jsonrpc/src/control.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/jsonrpc/src/control.rs b/crates/jsonrpc/src/control.rs index ed3729329..3b87e1a97 100644 --- a/crates/jsonrpc/src/control.rs +++ b/crates/jsonrpc/src/control.rs @@ -46,5 +46,11 @@ mod tests { assert!(process_line("{", 100, call).unwrap().contains("bad request envelope:")); assert_eq!(process_line(r#"{"id":3}"#, 100, call).unwrap(), r#"{"error":"missing `method`","id":3,"ok":false}"#); assert!(process_line("abcdef", 2, call).unwrap().contains("byte limit")); + let invoked = std::cell::Cell::new(false); + let _ = process_line(" ", 2, |_, _| -> Result { + invoked.set(true); + Ok(Value::Null) + }); + assert!(!invoked.get()); } } From 8f9a719fb198c9fe3c0a72dfc030313dc8f5c62c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:06:03 +0000 Subject: [PATCH 075/150] Cover idless successful control requests --- crates/jsonrpc/src/control.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/jsonrpc/src/control.rs b/crates/jsonrpc/src/control.rs index 3b87e1a97..c4c3a0389 100644 --- a/crates/jsonrpc/src/control.rs +++ b/crates/jsonrpc/src/control.rs @@ -43,6 +43,7 @@ mod tests { assert_eq!(process_line(" ", 20, call), None); assert_eq!(process_line(r#"{"id":[1],"method":"echo"}"#, 100, call).unwrap(), r#"{"id":[1],"ok":true,"result":null}"#); assert_eq!(process_line(r#"{"method":"bad"}"#, 100, call).unwrap(), r#"{"error":"unknown","id":null,"ok":false}"#); + assert_eq!(process_line(r#"{"method":"echo"}"#, 100, call).unwrap(), r#"{"id":null,"ok":true,"result":null}"#); assert!(process_line("{", 100, call).unwrap().contains("bad request envelope:")); assert_eq!(process_line(r#"{"id":3}"#, 100, call).unwrap(), r#"{"error":"missing `method`","id":3,"ok":false}"#); assert!(process_line("abcdef", 2, call).unwrap().contains("byte limit")); From 86a206d6cd33f6acac9cf3bf9e9bca948becdbf1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:07:46 +0000 Subject: [PATCH 076/150] aaf: exercise default Bento writer API --- crates/aaf/tests/bento_api.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/aaf/tests/bento_api.rs b/crates/aaf/tests/bento_api.rs index 9df70c661..93adc33c1 100644 --- a/crates/aaf/tests/bento_api.rs +++ b/crates/aaf/tests/bento_api.rs @@ -15,3 +15,13 @@ fn external_consumer_reads_named_values_and_references() { assert_eq!(container.resolve(first, second), second); assert!(Container::open(&bytes[..bytes.len() - 1]).is_err()); } + +#[test] +fn default_writer_builds_a_readable_empty_container() -> Result<(), String> { + let bytes = Writer::default().finish(); + let container = Container::open(&bytes)?; + let ids: Vec<_> = container.object_ids().collect(); + assert!(!ids.is_empty()); + assert!(ids.windows(2).all(|pair| pair[0] < pair[1])); + Ok(()) +} From daed8c721229a76fefef48a56052983e390d69d1 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 18:09:15 +0100 Subject: [PATCH 077/150] Fujifilm compressed RAF decodes about 1.2x as fast k from leading_zeros, neighbour reads from row slices of one reused colour-vector buffer, per-quantizer coding parameters computed once, placement through a precomputed map. Output unchanged; adds a differential test against the previous decoder and an optional corpus test. Co-Authored-By: Claude Sonnet 5.5 --- crates/raw/src/rafc.rs | 551 ++++++++++++++++++++++++++++++++--------- 1 file changed, 433 insertions(+), 118 deletions(-) diff --git a/crates/raw/src/rafc.rs b/crates/raw/src/rafc.rs index 2b3aa9a34..8f66ac2fe 100644 --- a/crates/raw/src/rafc.rs +++ b/crates/raw/src/rafc.rs @@ -183,14 +183,15 @@ struct State { } impl State { + /// The smallest `k` with `count << k >= sum`. `count << k0` has the bit length of `sum` (for `k0` the bit-length + /// difference), so it is at most one step short. + #[inline] fn k(self) -> u32 { - let mut k = 0; - while self.count << k < self.sum { - k += 1; - } - k + let k = self.count.leading_zeros().saturating_sub(self.sum.leading_zeros()); + k + u32::from(self.count << k < self.sum) } + #[inline] fn update(&mut self, delta: i32) { self.sum += delta.unsigned_abs(); if self.count == 64 { @@ -202,8 +203,7 @@ impl State { } #[inline] -fn quant(v: i32, near: u32) -> i32 { - let t = [18 + 3 * near, 67 + 5 * near, 276 + 7 * near]; +fn quant(v: i32, near: u32, t: &[u32; 3]) -> i32 { let a = v.unsigned_abs(); let bucket = if a <= near { 0 @@ -232,124 +232,169 @@ fn even_prediction(nw: i32, n: i32, ne: i32, nn: i32) -> i32 { (pair + 2 * n) >> 2 } -#[inline] -fn sample(rows: &[Vec], y: usize, x: usize) -> Result { - rows.get(y).and_then(|r| r.get(x)).copied().ok_or_else(|| corrupt("invalid predictor position")) +/// Residual coding parameters of one precision (`2 * precision + 1` is the quantizer step). +#[derive(Clone, Copy)] +struct Coding { + range: u32, + bits: u32, + limit: u32, + step: i32, } -fn decode_stripe(stripe: &Stripe<'_>, height: usize, bits: u32, xtrans: bool) -> Result> { - let size = if xtrans { BLOCK * 2 / 3 } else { BLOCK / 2 }; - let mut previous = vec![vec![vec![0; size + 2]; 2]; 3]; - let max = (1u32 << bits) - 1; - let range = |near: u32| (max + 2 * near) / (2 * near + 1) + 1; - let seed = |near: u32| State { sum: ((range(near) + 32) / 64).max(2), count: 1 }; - let mut states = [[[seed(0); 41]; 2]; 3]; - // Smooth regions retain finer precision and independent state across block quantizer changes. - let mut fine = [[[[seed(0); 5]; 2]; 3], [[[seed(1); 5]; 2]; 3], [[[seed(2); 5]; 2]; 3]]; - let mut last_near = None; - let mut reader = Bits::new(stripe.bytes); - let n = height.checked_mul(stripe.width).filter(|&n| n <= crate::MAX_SAMPLES).ok_or(RawError::Limit("image too large"))?; - // a capacity hint only: the stripe's declared height is the file's word, so the reservation is capped at a sample - // per bit of the stripe's real bytes (all stripes reserve in parallel; a one-byte stripe must not take 2 GiB) - let mut output = Vec::with_capacity(n.min(stripe.bytes.len().saturating_mul(8))); - for group in 0..height / 6 { - let near = match stripe.quantizers { - Some(q) => u32::from(*q.get(group).ok_or_else(|| corrupt("truncated quantizer table"))?), - None => 0, +impl Coding { + fn new(max: u32, sample_bits: u32, precision: u32) -> Self { + let range = (max + 2 * precision) / (2 * precision + 1) + 1; + let bits = 32 - (range - 1).leading_zeros(); + Self { range, bits, limit: 4 * sample_bits - bits - 1, step: (2 * precision + 1) as i32 } + } +} + +/// Everything that depends only on a six-row group's quantizer, computed when the quantizer changes. +struct Group { + near: u32, + thresholds: [u32; 3], + main: Coding, + /// Precisions 0, 1 and 2 for the low-gradient sites. + fine: [Coding; 3], +} + +impl Group { + fn new(near: u32, max: u32, sample_bits: u32) -> Self { + Self { + near, + thresholds: [18 + 3 * near, 67 + 5 * near, 276 + 7 * near], + main: Coding::new(max, sample_bits, near), + fine: [0, 1, 2].map(|p| Coding::new(max, sample_bits, p)), + } + } +} + +fn seed(max: u32, near: u32) -> State { + let range = (max + 2 * near) / (2 * near + 1) + 1; + State { sum: ((range + 32) / 64).max(2), count: 1 } +} + +/// Colour vectors of one stripe, kept for the rows of a six-row group plus the two rows above it. Red and blue get +/// three rows per group, green six; they are stored one after the other in one buffer of rows `stride` wide. +const ROWS: [usize; 3] = [5, 8, 5]; +const BASE: [usize; 3] = [0, 5, 13]; +const TOTAL_ROWS: usize = 18; + +/// The three lines a sample is predicted from: the two above and the one being decoded. Each line has one edge +/// cell on both sides, which the new line takes from the line above. +struct Lines<'a> { + above2: &'a [i32], + above: &'a [i32], + current: &'a mut [i32], +} + +impl<'a> Lines<'a> { + fn new(plane: &'a mut [i32], y: usize, stride: usize) -> Self { + let (done, rest) = plane.split_at_mut(y * stride); + let done: &[i32] = done; + let current = &mut rest[..stride]; + let above = &done[(y - 1) * stride..]; + let above2 = &done[(y - 2) * stride..(y - 1) * stride]; + current[0] = above[1]; + current[stride - 1] = above[stride - 2]; + Self { above2, above, current } + } +} + +struct Coder<'a> { + reader: Bits<'a>, + states: [[[State; 41]; 2]; 3], + /// Smooth regions retain finer precision and independent state across block quantizer changes. + fine: [[[[State; 5]; 2]; 3]; 3], + max: u32, + xtrans: bool, +} + +impl Coder<'_> { + /// Decodes one sample's residual with the context its gradients select and applies it to the prediction. + #[inline] + fn residual(&mut self, g: &Group, pair: usize, odd: usize, (d1, d2): (i32, i32), predict: i32) -> Result { + let near = g.near; + let contrast = d1.unsigned_abs() + d2.unsigned_abs(); + let precision = match contrast { + 0..=5 => 0, + 6 => near.min(1), + 7 => near.min(2), + _ => near, }; - if last_near != Some(near) { - states = [[[seed(near); 41]; 2]; 3]; - last_near = Some(near); + let (context, state, coding) = if precision < near { + let sign = |v: i32| if v.unsigned_abs() <= precision { 0 } else { v.signum() }; + let context = 3 * sign(d1) + sign(d2); + (context, &mut self.fine[precision as usize][pair % 3][odd][context.unsigned_abs() as usize], g.fine[precision as usize]) + } else { + let context = 9 * quant(d1, near, &g.thresholds) + quant(d2, near, &g.thresholds); + (context, &mut self.states[pair % 3][odd][context.unsigned_abs() as usize], g.main) + }; + let mut delta = self.reader.residual(state.k(), coding.bits, coding.range, coding.limit)?; + state.update(delta); + if context < 0 { + delta = -delta; } - let mut rows = previous; + let mut value = predict + delta * coding.step; + if value < -(precision as i32) { + value += coding.range as i32 * coding.step; + } else if value > (self.max + precision) as i32 { + value -= coding.range as i32 * coding.step; + } + Ok(value.clamp(0, self.max as i32)) + } + + /// One position of the two colour lines of a pair: the even column (`odd == 0`) or the one seven behind it. + #[inline] + fn position(&mut self, g: &Group, pair: usize, odd: usize, pos: usize, lines: &mut [Lines<'_>; 2]) -> Result<()> { + let x = pos + 1; + for (which, l) in lines.iter_mut().enumerate() { + let (nw, north, ne) = (l.above[x - 1], l.above[x], l.above[x + 1]); + let real = !self.xtrans || (MASKS[pair][which] >> (pos % 4)) & 1 != 0; + l.current[x] = if odd == 0 { + let nn = l.above2[x]; + let predict = even_prediction(nw, north, ne, nn); + if real { self.residual(g, pair, 0, (north - nn, nw - north), predict)? } else { predict } + } else { + let (west, east) = (l.current[x - 1], l.current[x + 1]); + let predict = if (nw.min(ne)..=nw.max(ne)).contains(&north) { (west + east) >> 1 } else { (west + east + 2 * north) >> 2 }; + if real { self.residual(g, pair, 1, (north - nw, nw - west), predict)? } else { predict } + }; + } + Ok(()) + } + + /// Decodes the six colour-vector pairs of one group into `buf`. + fn group(&mut self, g: &Group, buf: &mut [i32], size: usize) -> Result<()> { + let stride = size + 2; + let (red, rest) = buf.split_at_mut(ROWS[0] * stride); + let (green, blue) = rest.split_at_mut(ROWS[1] * stride); for pair in 0..6 { - let colours = if pair % 2 == 0 { [0, 1] } else { [1, 2] }; - for &colour in &colours { - let lines = rows.get_mut(colour).ok_or_else(|| corrupt("invalid colour vector"))?; - let mut row = vec![0; size + 2]; - let prev = lines.len() - 1; - let left = sample(lines, prev, 1)?; - let right = sample(lines, prev, size)?; - if let Some(v) = row.first_mut() { - *v = left; - } - if let Some(v) = row.last_mut() { - *v = right; - } - lines.push(row); - } + let mut lines = if pair % 2 == 0 { + [Lines::new(&mut *red, 2 + pair / 2, stride), Lines::new(&mut *green, 2 + pair, stride)] + } else { + [Lines::new(&mut *green, 2 + pair, stride), Lines::new(&mut *blue, 2 + pair / 2, stride)] + }; for even in (0..size + 8).step_by(2) { - for (odd, position) in [(0, Some(even)), (1, even.checked_sub(7))] { - let Some(pos) = position.filter(|&x| x < size) else { continue }; - let x = pos + 1; - for (which, &colour) in colours.iter().enumerate() { - let lines = rows.get_mut(colour).ok_or_else(|| corrupt("invalid colour vector"))?; - let y = lines.len() - 1; - let (nw, north, ne, nn) = - (sample(lines, y - 1, x - 1)?, sample(lines, y - 1, x)?, sample(lines, y - 1, x + 1)?, sample(lines, y - 2, x)?); - let west = sample(lines, y, x - 1)?; - let east = sample(lines, y, x + 1)?; - let (predict, d1, d2) = if odd == 0 { - (even_prediction(nw, north, ne, nn), north - nn, nw - north) - } else { - let p = if (nw.min(ne)..=nw.max(ne)).contains(&north) { (west + east) >> 1 } else { (west + east + 2 * north) >> 2 }; - (p, north - nw, nw - west) - }; - let real = !xtrans || MASKS.get(pair).and_then(|m| m.get(which)).is_some_and(|m| (m >> (pos % 4)) & 1 != 0); - let value = if real { - let contrast = d1.unsigned_abs() + d2.unsigned_abs(); - let precision = match contrast { - 0..=5 => 0, - 6 => near.min(1), - 7 => near.min(2), - _ => near, - }; - let (context, state) = if precision < near { - let sign = |v: i32| if v.unsigned_abs() <= precision { 0 } else { v.signum() }; - let context = 3 * sign(d1) + sign(d2); - let state = fine - .get_mut(precision as usize) - .and_then(|f| f.get_mut(pair % 3)) - .and_then(|s| s.get_mut(odd)) - .and_then(|s| s.get_mut(context.unsigned_abs() as usize)) - .ok_or_else(|| corrupt("invalid fine coding context"))?; - (context, state) - } else { - let context = 9 * quant(d1, near) + quant(d2, near); - let state = states - .get_mut(pair % 3) - .and_then(|s| s.get_mut(odd)) - .and_then(|s| s.get_mut(context.unsigned_abs() as usize)) - .ok_or_else(|| corrupt("invalid coding context"))?; - (context, state) - }; - let coded_range = range(precision); - let coded_bits = 32 - (coded_range - 1).leading_zeros(); - let mut delta = reader.residual(state.k(), coded_bits, coded_range, 4 * bits - coded_bits - 1)?; - state.update(delta); - if context < 0 { - delta = -delta; - } - let step = (2 * precision + 1) as i32; - let mut value = predict + delta * step; - if value < -(precision as i32) { - value += coded_range as i32 * step; - } else if value > (max + precision) as i32 { - value -= coded_range as i32 * step; - } - value.clamp(0, max as i32) - } else { - predict - }; - *lines.get_mut(y).and_then(|r| r.get_mut(x)).ok_or_else(|| corrupt("invalid predictor position"))? = value; - } + if even < size { + self.position(g, pair, 0, even, &mut lines)?; + } + if even >= 7 && even - 7 < size { + self.position(g, pair, 1, even - 7, &mut lines)?; } } } - for y in 0..6 { - for x in 0..stripe.width { + Ok(()) + } +} + +/// For each of the six rows of a group, where each output sample sits in the colour-vector buffer. +fn placement(xtrans: bool, width: usize, stride: usize) -> [Vec; 6] { + std::array::from_fn(|y| { + (0..width) + .map(|x| { let colour = if xtrans { - XTRANS.get(y).and_then(|r| r.get(x % 6)).copied().ok_or_else(|| corrupt("invalid CFA position"))? + XTRANS[y][x % 6] } else { match (y % 2, x % 2) { (0, 0) => 0, @@ -359,10 +404,49 @@ fn decode_stripe(stripe: &Stripe<'_>, height: usize, bits: u32, xtrans: bool) -> }; let row = 2 + if colour == 1 { y } else { y / 2 }; let col = 1 + if xtrans { 2 * (x / 3) + (x % 3).min(1) } else { x / 2 }; - output.push(sample(rows.get(colour).ok_or_else(|| corrupt("invalid colour vector"))?, row, col)? as u16); - } + (BASE[colour] + row) * stride + col + }) + .collect() + }) +} + +fn decode_stripe(stripe: &Stripe<'_>, height: usize, bits: u32, xtrans: bool) -> Result> { + let size = if xtrans { BLOCK * 2 / 3 } else { BLOCK / 2 }; + let stride = size + 2; + let max = (1u32 << bits) - 1; + let mut coder = Coder { + reader: Bits::new(stripe.bytes), + states: [[[seed(max, 0); 41]; 2]; 3], + fine: [[[[seed(max, 0); 5]; 2]; 3], [[[seed(max, 1); 5]; 2]; 3], [[[seed(max, 2); 5]; 2]; 3]], + max, + xtrans, + }; + let mut last_near = None; + let mut group = Group::new(0, max, bits); + let n = height.checked_mul(stripe.width).filter(|&n| n <= crate::MAX_SAMPLES).ok_or(RawError::Limit("image too large"))?; + // a capacity hint only: the stripe's declared height is the file's word, so the reservation is capped at a sample + // per bit of the stripe's real bytes (all stripes reserve in parallel; a one-byte stripe must not take 2 GiB) + let mut output = Vec::with_capacity(n.min(stripe.bytes.len().saturating_mul(8))); + let place = placement(xtrans, stripe.width, stride); + let mut buf = vec![0i32; TOTAL_ROWS * stride]; + for index in 0..height / 6 { + let near = match stripe.quantizers { + Some(q) => u32::from(*q.get(index).ok_or_else(|| corrupt("truncated quantizer table"))?), + None => 0, + }; + if last_near != Some(near) { + coder.states = [[[seed(max, near); 41]; 2]; 3]; + group = Group::new(near, max, bits); + last_near = Some(near); + } + coder.group(&group, &mut buf, size)?; + for row in &place { + output.extend(row.iter().map(|&at| buf[at] as u16)); + } + // the last two rows of each colour are the next group's rows above + for (base, rows) in BASE.iter().zip(ROWS) { + buf.copy_within((base + rows - 2) * stride..(base + rows) * stride, base * stride); } - previous = rows.into_iter().map(|mut r| r.split_off(r.len() - 2)).collect(); } Ok(output) } @@ -590,3 +674,234 @@ mod tests { } } } + +/// The straightforward decoder this module started from (shift loop for `k`, an `Option`/`Result` neighbour read +/// per access, a `Vec` per colour line, one pixel placed at a time), kept as the oracle for the optimised one. +#[cfg(test)] +mod differential { + use super::*; + use sha2::{Digest, Sha256}; + + fn k_by_shifting(state: State) -> u32 { + let mut k = 0; + while state.count << k < state.sum { + k += 1; + } + k + } + + fn quant_by_table(v: i32, near: u32) -> i32 { + let t = [18 + 3 * near, 67 + 5 * near, 276 + 7 * near]; + quant(v, near, &t) + } + + fn sample(rows: &[Vec], y: usize, x: usize) -> Result { + rows.get(y).and_then(|r| r.get(x)).copied().ok_or_else(|| corrupt("invalid predictor position")) + } + + fn reference_stripe(stripe: &Stripe<'_>, height: usize, bits: u32, xtrans: bool) -> Result> { + let size = if xtrans { BLOCK * 2 / 3 } else { BLOCK / 2 }; + let mut previous = vec![vec![vec![0; size + 2]; 2]; 3]; + let max = (1u32 << bits) - 1; + let range = |near: u32| (max + 2 * near) / (2 * near + 1) + 1; + let seed = |near: u32| State { sum: ((range(near) + 32) / 64).max(2), count: 1 }; + let mut states = [[[seed(0); 41]; 2]; 3]; + let mut fine = [[[[seed(0); 5]; 2]; 3], [[[seed(1); 5]; 2]; 3], [[[seed(2); 5]; 2]; 3]]; + let mut last_near = None; + let mut reader = Bits::new(stripe.bytes); + let n = height.checked_mul(stripe.width).filter(|&n| n <= crate::MAX_SAMPLES).ok_or(RawError::Limit("image too large"))?; + let mut output = Vec::with_capacity(n.min(stripe.bytes.len().saturating_mul(8))); + for group in 0..height / 6 { + let near = match stripe.quantizers { + Some(q) => u32::from(*q.get(group).ok_or_else(|| corrupt("truncated quantizer table"))?), + None => 0, + }; + if last_near != Some(near) { + states = [[[seed(near); 41]; 2]; 3]; + last_near = Some(near); + } + let mut rows = previous; + for pair in 0..6 { + let colours = if pair % 2 == 0 { [0, 1] } else { [1, 2] }; + for &colour in &colours { + let lines = rows.get_mut(colour).ok_or_else(|| corrupt("invalid colour vector"))?; + let mut row = vec![0; size + 2]; + let prev = lines.len() - 1; + let left = sample(lines, prev, 1)?; + let right = sample(lines, prev, size)?; + if let Some(v) = row.first_mut() { + *v = left; + } + if let Some(v) = row.last_mut() { + *v = right; + } + lines.push(row); + } + for even in (0..size + 8).step_by(2) { + for (odd, position) in [(0, Some(even)), (1, even.checked_sub(7))] { + let Some(pos) = position.filter(|&x| x < size) else { continue }; + let x = pos + 1; + for (which, &colour) in colours.iter().enumerate() { + let lines = rows.get_mut(colour).ok_or_else(|| corrupt("invalid colour vector"))?; + let y = lines.len() - 1; + let (nw, north, ne, nn) = + (sample(lines, y - 1, x - 1)?, sample(lines, y - 1, x)?, sample(lines, y - 1, x + 1)?, sample(lines, y - 2, x)?); + let west = sample(lines, y, x - 1)?; + let east = sample(lines, y, x + 1)?; + let (predict, d1, d2) = if odd == 0 { + (even_prediction(nw, north, ne, nn), north - nn, nw - north) + } else { + let p = if (nw.min(ne)..=nw.max(ne)).contains(&north) { (west + east) >> 1 } else { (west + east + 2 * north) >> 2 }; + (p, north - nw, nw - west) + }; + let real = !xtrans || MASKS.get(pair).and_then(|m| m.get(which)).is_some_and(|m| (m >> (pos % 4)) & 1 != 0); + let value = if real { + let contrast = d1.unsigned_abs() + d2.unsigned_abs(); + let precision = match contrast { + 0..=5 => 0, + 6 => near.min(1), + 7 => near.min(2), + _ => near, + }; + let (context, state) = if precision < near { + let sign = |v: i32| if v.unsigned_abs() <= precision { 0 } else { v.signum() }; + let context = 3 * sign(d1) + sign(d2); + let state = fine + .get_mut(precision as usize) + .and_then(|f| f.get_mut(pair % 3)) + .and_then(|s| s.get_mut(odd)) + .and_then(|s| s.get_mut(context.unsigned_abs() as usize)) + .ok_or_else(|| corrupt("invalid fine coding context"))?; + (context, state) + } else { + let context = 9 * quant_by_table(d1, near) + quant_by_table(d2, near); + let state = states + .get_mut(pair % 3) + .and_then(|s| s.get_mut(odd)) + .and_then(|s| s.get_mut(context.unsigned_abs() as usize)) + .ok_or_else(|| corrupt("invalid coding context"))?; + (context, state) + }; + let coded_range = range(precision); + let coded_bits = 32 - (coded_range - 1).leading_zeros(); + let mut delta = reader.residual(k_by_shifting(*state), coded_bits, coded_range, 4 * bits - coded_bits - 1)?; + state.update(delta); + if context < 0 { + delta = -delta; + } + let step = (2 * precision + 1) as i32; + let mut value = predict + delta * step; + if value < -(precision as i32) { + value += coded_range as i32 * step; + } else if value > (max + precision) as i32 { + value -= coded_range as i32 * step; + } + value.clamp(0, max as i32) + } else { + predict + }; + *lines.get_mut(y).and_then(|r| r.get_mut(x)).ok_or_else(|| corrupt("invalid predictor position"))? = value; + } + } + } + } + for y in 0..6 { + for x in 0..stripe.width { + let colour = if xtrans { + XTRANS.get(y).and_then(|r| r.get(x % 6)).copied().ok_or_else(|| corrupt("invalid CFA position"))? + } else { + match (y % 2, x % 2) { + (0, 0) => 0, + (1, 1) => 2, + _ => 1, + } + }; + let row = 2 + if colour == 1 { y } else { y / 2 }; + let col = 1 + if xtrans { 2 * (x / 3) + (x % 3).min(1) } else { x / 2 }; + output.push(sample(rows.get(colour).ok_or_else(|| corrupt("invalid colour vector"))?, row, col)? as u16); + } + } + previous = rows.into_iter().map(|mut r| r.split_off(r.len() - 2)).collect(); + } + Ok(output) + } + + /// Deterministic bytes: uniform, or mostly zero (long unary prefixes and escapes). + fn bytes(seed: u64, len: usize, sparse: bool) -> Vec { + let mut x = seed | 1; + (0..len) + .map(|_| { + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + let b = (x >> 24) as u8; + if sparse && x & 0b11 != 0 { 0 } else { b } + }) + .collect() + } + + #[test] + fn k_without_the_shift_loop_matches_it() { + let mut sums: Vec = (0..=4200).collect(); + for i in 0..=30 { + sums.extend([(1u32 << i) - 1, 1 << i, (1 << i) + 1]); + } + for count in 1..=64 { + for &sum in &sums { + let state = State { sum, count }; + assert_eq!(state.k(), k_by_shifting(state), "count {count}, sum {sum}"); + } + } + } + + /// Every variant the stripe decoder handles (lossless and lossy, Bayer and X-Trans, 12/14/16 bits, full and + /// partial stripes, quantizer changes, fine contexts) gives the oracle's samples, or its error, on dense + /// pseudo-random entropy streams that reach all contexts, and on sparse ones that reach the escapes. + #[test] + fn stripe_decoder_matches_the_straightforward_one() { + let quantizers: [Option<&[u8]>; 4] = + [None, Some([0, 1, 2, 3].as_slice()), Some([2, 2, 7, 0, 40, 255].as_slice()), Some([1, 1, 0, 5, 5, 9, 2].as_slice())]; + let mut decoded = 0; + for bits in [12, 14, 16] { + for xtrans in [false, true] { + for q in quantizers { + for (seed, width, sparse) in [(1, BLOCK, false), (2, 301, false), (3, 12, false), (4, BLOCK, true)] { + let data = bytes(seed * 31 + u64::from(bits), 24 << 10, sparse); + let groups = q.map_or(3, <[u8]>::len); + let stripe = Stripe { bytes: &data, quantizers: q, width }; + let want = reference_stripe(&stripe, groups * 6, bits, xtrans).map_err(|e| e.to_string()); + let got = decode_stripe(&stripe, groups * 6, bits, xtrans).map_err(|e| e.to_string()); + assert_eq!(got, want, "bits {bits}, xtrans {xtrans}, quantizers {q:?}, width {width}, sparse {sparse}"); + decoded += usize::from(want.is_ok()); + } + } + } + } + assert!(decoded >= 24, "only {decoded} of 96 streams decoded to the end, the comparison reaches too little"); + } + + /// Optional: the CC0 files of docs/raf-corpus.sha256 in the directory named by `LIGHTCRAFT_RAF_CORPUS` (file + /// names as listed there, e.g. `raf-fuji-xt5-6122.raf`). Each present file's SHA-256 is checked, then the + /// decoded sensor samples are compared with the hash recorded from the decoder before the optimisation. + #[test] + fn optional_cc0_samples_decode_as_recorded() { + let Some(directory) = std::env::var_os("LIGHTCRAFT_RAF_CORPUS") else { return }; + let list = include_str!("../docs/raf-corpus.sha256"); + for line in list.lines().filter(|l| !l.starts_with('#') && !l.trim().is_empty()) { + let mut columns = line.split_whitespace(); + let (Some(digest), Some(path)) = (columns.next(), columns.next()) else { panic!("bad corpus row {line:?}") }; + let name = path.rsplit('/').next().unwrap(); + let Ok(bytes) = std::fs::read(std::path::Path::new(&directory).join(name)) else { continue }; + let sha = Sha256::digest(&bytes).iter().map(|b| format!("{b:02x}")).collect::(); + assert_eq!(sha, digest, "{name}: not the published file"); + let Some(&(_, expected)) = RECORDED.iter().find(|(n, _)| *n == name) else { panic!("{name}: no recorded hash") }; + let image = crate::decode(&bytes).unwrap(); + let crate::RawData::U16(data) = &image.data else { panic!("{name}: not 16-bit samples") }; + let hash = data.iter().flat_map(|v| v.to_le_bytes()).fold(0xcbf29ce484222325u64, |h, b| (h ^ u64::from(b)).wrapping_mul(0x100000001b3)); + assert_eq!(hash, expected, "{name}"); + } + } + + /// FNV-1a over the little-endian decoded samples, recorded from `upstream/main` before the change. + const RECORDED: &[(&str, u64)] = &[]; +} From 4ca26a59968ee7b259801d0ddabe6956531a75d7 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:10:59 +0000 Subject: [PATCH 078/150] timeline: bound FCP7 nested sequence imports --- crates/timeline/src/fcp7.rs | 65 +++++++++++++++++++++++++++----- crates/timeline/tests/it/fcp7.rs | 21 +++++++++++ 2 files changed, 77 insertions(+), 9 deletions(-) diff --git a/crates/timeline/src/fcp7.rs b/crates/timeline/src/fcp7.rs index 41243baf6..82ff410f9 100644 --- a/crates/timeline/src/fcp7.rs +++ b/crates/timeline/src/fcp7.rs @@ -117,6 +117,9 @@ struct PendingLink { refs: Vec, } +const MAX_NESTING_DEPTH: usize = 64; +const MAX_REIMPORTS: usize = 4096; + struct Imp<'a, 'c, 'r> { b: Builder<'c>, opts: &'a ImportOptions<'a>, @@ -126,6 +129,9 @@ struct Imp<'a, 'c, 'r> { file_items: HashMap, seq_items: HashMap, in_progress: HashSet, + depth_hits: usize, + truncated_at: HashMap, + reimports_left: usize, links: Vec, xml_clip_ids: HashMap<(ItemId, String), ClipId>, /// `(sequence key, file key)` pairs where some audio clip item of that file uses `sourcetrack/trackindex` > 1, @@ -177,6 +183,9 @@ pub fn import(text_in: &str, opts: &ImportOptions, report: &mut Report) -> Resul file_items: HashMap::new(), seq_items: HashMap::new(), in_progress: HashSet::new(), + depth_hits: 0, + truncated_at: HashMap::new(), + reimports_left: MAX_REIMPORTS, links: Vec::new(), xml_clip_ids: HashMap::new(), split_files: HashSet::new(), @@ -349,10 +358,27 @@ impl<'a> Imp<'a, '_, '_> { /// Sequence item for a `` element (definition or reference). fn sequence(&mut self, n: Node<'a>, bin: Option) -> Option { let key = node_key(n); + let depth = self.in_progress.len(); + let mut again = None; if let Some(id) = self.seq_items.get(&key) { - return Some(*id); + let shallower = self.truncated_at.get(&key).is_some_and(|d| depth < *d) && !self.in_progress.contains(&key); + if !shallower { + return Some(*id); + } + if self.reimports_left == 0 { + self.report.warn("truncated nested sequence kept incomplete after the re-import limit was reached"); + return Some(*id); + } + self.reimports_left -= 1; + again = Some(*id); } let def = if child(n, "media").is_some() { n } else { *self.seq_defs.get(&key)? }; + if depth >= MAX_NESTING_DEPTH { + self.depth_hits += 1; + self.report.warn("nested sequence is nested too deeply; imported as a gap"); + return None; + } + let hits_before = self.depth_hits; if !self.in_progress.insert(key.clone()) { self.report.warn("recursive nested sequence ignored"); return None; @@ -374,12 +400,18 @@ impl<'a> Imp<'a, '_, '_> { { settings.sample_rate = sr as u32; } - let id = self.b.reserve_sequence(&name, settings.clone(), bin); - if let Some(l) = path_text(def, &["labels", "label2"]).and_then(label_from) - && let Some(it) = self.b.p.item_mut(id) - { - it.label = l; - } + let id = match again { + Some(id) => id, + None => { + let id = self.b.reserve_sequence(&name, settings.clone(), bin); + if let Some(l) = path_text(def, &["labels", "label2"]).and_then(label_from) + && let Some(it) = self.b.p.item_mut(id) + { + it.label = l; + } + id + } + }; self.seq_items.insert(key.clone(), id); let mut seq = empty_sequence(settings); seq.start_timecode = tc.and_then(|t| child_i64(t, "frame")).unwrap_or(0); @@ -403,6 +435,11 @@ impl<'a> Imp<'a, '_, '_> { } self.b.put_sequence(id, seq); self.in_progress.remove(&key); + if self.depth_hits > hits_before { + self.truncated_at.insert(key, depth); + } else { + self.truncated_at.remove(&key); + } Some(id) } @@ -531,9 +568,19 @@ impl<'a> Imp<'a, '_, '_> { } } } else if let Some(s) = child(c, "sequence") { - self.sequence(s, None) + if let Some(id) = self.sequence(s, None) { + return Some(id); + } + self.report.warn(format!("clip \"{name}\" refers to a nested sequence that was left out; imported offline")); + Some(self.offline_media(kind, seq_rate, frame, name)) } else { self.report.warn(format!("clip \"{name}\" has no media reference; imported offline")); + Some(self.offline_media(kind, seq_rate, frame, name)) + } + } + + fn offline_media(&mut self, kind: TrackKind, seq_rate: FrameRate, frame: (u32, u32), name: &str) -> ItemId { + { let spec = MediaSpec { video: (kind == TrackKind::Video).then_some((frame.0, frame.1, seq_rate)), audio: (kind == TrackKind::Audio).then_some((48_000, 2)), @@ -545,7 +592,7 @@ impl<'a> Imp<'a, '_, '_> { if let Some(m) = self.b.p.item_mut(id).and_then(|i| i.as_media_mut()) { m.offline = true; } - Some(id) + id } } diff --git a/crates/timeline/tests/it/fcp7.rs b/crates/timeline/tests/it/fcp7.rs index 00d0704ec..4deb7a3bd 100644 --- a/crates/timeline/tests/it/fcp7.rs +++ b/crates/timeline/tests/it/fcp7.rs @@ -764,3 +764,24 @@ fn a_huge_link_group_does_not_write_quadratic_links() { assert_eq!(text.matches("").count(), 64 * 64); assert!(rep.has_warnings(), "{rep}"); } + +#[test] +fn nested_sequence_chain_is_bounded_and_reimported_at_a_shallower_depth() { + let mut doc = String::from(""); + for i in 0..66 { + let nested = if i < 65 { + format!( + "nest024024", + i + 1 + ) + } else { + String::new() + }; + doc.push_str(&format!("S{i}2424")); + } + doc.push_str(""); + let (imported, report) = fcp7_import(&doc, None); + assert!(report.mentions("nested too deeply")); + let nested = imported.project.items.values().find(|item| item.name == "S63").and_then(|item| imported.project.sequence(item.id)); + assert!(nested.is_some_and(|seq| seq.video_tracks.first().is_some_and(|track| track.items.len() == 1))); +} From 50c3053bbc67b43439b8f2ca578d5e0af18266e1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:12:12 +0000 Subject: [PATCH 079/150] Preserve raster probe format and bound JPEG header input --- crates/image-codecs/src/allocation.rs | 35 ++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/crates/image-codecs/src/allocation.rs b/crates/image-codecs/src/allocation.rs index a31684a33..6ba09f569 100644 --- a/crates/image-codecs/src/allocation.rs +++ b/crates/image-codecs/src/allocation.rs @@ -257,7 +257,19 @@ pub fn probe_stored_size(bytes: &[u8], limits: &crate::Limits) -> Result<(u32, u if !matches!(format, Format::Png | Format::Jpeg | Format::Bmp | Format::Tiff) { return Err(CodecError::unsupported(format, "stored-size probe is not available for this format")); } - let mut reader = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().map_err(|e| CodecError::malformed(format, e))?; + // The JPEG header reader owns a copy of its input before applying image limits. + if format == Format::Jpeg && u64::try_from(bytes.len()).map_or(true, |len| len > limits.max_alloc) { + return Err(CodecError::malformed(format, "JPEG input exceeds the allocation budget")); + } + let image_format = match format { + Format::Png => image::ImageFormat::Png, + Format::Jpeg => image::ImageFormat::Jpeg, + Format::Bmp => image::ImageFormat::Bmp, + Format::Tiff => image::ImageFormat::Tiff, + _ => return Err(CodecError::unsupported(format, "stored-size probe is not available for this format")), + }; + // Preserve detect's BigTIFF selection; image's signature guesser only knows classic TIFF. + let mut reader = image::ImageReader::with_format(Cursor::new(bytes), image_format); let mut image_limits = image::Limits::default(); image_limits.max_image_width = Some(limits.max_width); image_limits.max_image_height = Some(limits.max_height); @@ -285,6 +297,27 @@ mod stored_size_tests { assert!(probe_stored_size(&bytes[..20], &limits).is_err()); } + #[test] + fn bigtiff_stored_dimensions_preserve_detected_format() { + let mut out = Cursor::new(Vec::new()); + tiff::encoder::TiffEncoder::new_big(&mut out).unwrap().write_image::(3, 2, &[10; 18]).unwrap(); + let bytes = out.into_inner(); + assert_eq!(crate::detect(&bytes), Some(Format::Tiff)); + assert_eq!(probe_stored_size(&bytes, &crate::Limits::default()).unwrap(), (3, 2)); + } + + #[test] + fn jpeg_input_copy_is_bounded_by_allocation_budget() { + let mut out = Cursor::new(Vec::new()); + image::write_buffer_with_format(&mut out, &[10; 18], 3, 2, image::ExtendedColorType::Rgb8, image::ImageFormat::Jpeg).unwrap(); + let bytes = out.into_inner(); + let mut limits = crate::Limits::default(); + limits.max_alloc = bytes.len() as u64; + assert_eq!(probe_stored_size(&bytes, &limits).unwrap(), (3, 2)); + limits.max_alloc -= 1; + assert!(probe_stored_size(&bytes, &limits).is_err()); + } + #[test] fn common_stored_headers_are_probed() { let limits = crate::Limits::new(100, 100, 10_000, 1 << 20); From 1d950d5597fd6111b9d4e5f9a78b95b537fb96d4 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 18:15:56 +0100 Subject: [PATCH 080/150] perf(raw): read CRX entropy streams through a 64-bit bit cache One 8-byte load per ~56 bits instead of a checked 5-byte window per read; the Rice prefix and its low bits come from one cached word. Escapes and the end of the stream keep the bit-by-bit path, so results and errors match. Part of #1654 Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/crx.rs | 263 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 229 insertions(+), 34 deletions(-) diff --git a/crates/raw/src/crx.rs b/crates/raw/src/crx.rs index a8382f757..f92040f88 100644 --- a/crates/raw/src/crx.rs +++ b/crates/raw/src/crx.rs @@ -502,17 +502,60 @@ fn be32(src: &[u8], at: usize) -> Result { Ok(u32::from_be_bytes(bytes)) } +/// An MSB-first bit reader over one entropy stream. +/// +/// Up to 64 stream bits are held in `cache`, left-aligned: the top `count` bits are the next unread +/// ones. A refill loads 8 bytes at once and keeps whole bytes only, so the bits below `count` are +/// either zero or the stream's own next bits at their final positions, which a later refill ORs in +/// again unchanged. Reads never trust those bits: they are checked against `count`. struct Bits<'a> { src: &'a [u8], - at: usize, + /// The next byte to load into `cache`. + next: usize, + cache: u64, + count: u32, } -impl Bits<'_> { - /// The next 32 bits, MSB first, when at least that many remain before the end of the stream. - fn peek32(&self) -> Option { - let bytes = self.src.get(self.at / 8..self.at / 8 + 5)?; - let word = u64::from_be_bytes([0, 0, 0, bytes[0], bytes[1], bytes[2], bytes[3], bytes[4]]); - Some((word >> (8 - self.at % 8)) as u32) +impl<'a> Bits<'a> { + fn new(src: &'a [u8]) -> Self { + Self { src, next: 0, cache: 0, count: 0 } + } + + /// The number of bits consumed so far. + fn position(&self) -> usize { + self.next * 8 - self.count as usize + } + + /// Loads whole bytes until more than 56 bits are cached or the stream ends. + fn refill(&mut self) { + if self.count > 56 { + return; + } + if let Some(bytes) = self.src.get(self.next..self.next + 8).and_then(|s| <[u8; 8]>::try_from(s).ok()) { + self.cache |= u64::from_be_bytes(bytes) >> self.count; + let loaded = (63 - self.count) / 8; + self.next += loaded as usize; + self.count += loaded * 8; + return; + } + // The last 7 bytes: one at a time. + while self.count <= 56 { + let Some(&byte) = self.src.get(self.next) else { break }; + self.cache |= u64::from(byte) << (56 - self.count); + self.next += 1; + self.count += 8; + } + } + + /// Drops `count` bits (at most `self.count`, below 64). + fn consume(&mut self, count: u32) { + self.cache <<= count; + self.count -= count; + } + + /// The top `count` cached bits (0 to 32) as a number; `>> 1 >> (63 - count)` is 0 for `count` 0. + fn top(&self, count: u32) -> u32 { + (self.cache >> 1 >> (63 - count)) as u32 } fn take(&mut self, count: u32) -> Result { @@ -522,27 +565,34 @@ impl Bits<'_> { if count == 0 { return Ok(0); } - if let Some(window) = self.peek32() { - self.at += count as usize; - return Ok(window >> (32 - count)); - } - // Near the end of the stream: bit by bit, so truncation is reported exactly. - let mut value = 0; - for _ in 0..count { - let byte = self.src.get(self.at / 8).ok_or_else(|| corrupt("truncated entropy stream"))?; - value = (value << 1) | u32::from((byte >> (7 - self.at % 8)) & 1); - self.at += 1; + if self.count < count { + self.refill(); + if self.count < count { + return Err(corrupt("truncated entropy stream")); + } } + let value = self.top(count); + self.consume(count); Ok(value) } fn rice(&mut self, k: u32) -> Result { - // Common case: the unary prefix and its terminating 1 lie within the next 32 bits. - if let Some(window) = self.peek32().filter(|&w| w != 0) { - let zeros = window.leading_zeros(); - self.at += zeros as usize + 1; - return if k <= 20 { Ok((zeros << k) | self.take(k)?) } else { Err(corrupt("Rice parameter exceeds sample precision")) }; + // Common case: the unary prefix (shorter than the 41-zero escape), its terminating 1 and the k + // low bits are all cached. + if k <= 20 { + let mut zeros = self.cache.leading_zeros(); + if zeros + 1 + k > self.count { + self.refill(); + zeros = self.cache.leading_zeros(); + } + if zeros < 41 && zeros + 1 + k <= self.count { + self.consume(zeros + 1); + let low = self.top(k); + self.consume(k); + return Ok((zeros << k) | low); + } } + // Escapes, k above 20 and the end of the stream: bit by bit, so truncation is reported exactly. let mut zeros = 0; while self.take(1)? == 0 { zeros += 1; @@ -580,11 +630,12 @@ impl Bits<'_> { fn finish(&self) -> Result<()> { let bits = self.src.len().checked_mul(8).ok_or(RawError::Limit("CRX entropy length"))?; - if bits.saturating_sub(self.at) > 7 { + let at = self.position(); + if bits.saturating_sub(at) > 7 { return Err(corrupt("unconsumed entropy data")); } if let Some(&last) = self.src.last() { - let remaining = (bits - self.at) as u32; + let remaining = (bits - at) as u32; if u32::from(last) & ((1 << remaining) - 1) != 0 { return Err(corrupt("nonzero entropy alignment bits")); } @@ -653,7 +704,7 @@ fn lossless_plane(src: &[u8], width: usize, height: usize) -> Result> { current.try_reserve_exact(width).map_err(|_| RawError::Limit("CRX row allocation"))?; current.resize(width, 0i32); let (mut k, mut run_index) = (0u32, 0usize); - let mut bits = Bits { src, at: 0 }; + let mut bits = Bits::new(src); for y in 0..height { let mut x = 0; let mut left = previous.first().copied().ok_or_else(|| corrupt("empty plane row"))?; @@ -716,7 +767,7 @@ fn high_frequency_band(src: &[u8], width: usize, height: usize) -> Result Result { Ok(values) }; let (mut previous, mut current) = (row()?, row()?); - let mut bits = Bits { src, at: 0 }; + let mut bits = Bits::new(src); let mut k = 0; for y in 0..height { let mut left = *previous.first().ok_or_else(|| corrupt("empty QP row"))?; @@ -879,11 +930,11 @@ mod tests { #[test] fn partial_final_run_segment_keeps_its_index() { - let mut bits = Bits { src: &[0xc0], at: 0 }; + let mut bits = Bits::new(&[0xc0]); let mut index = 11; assert_eq!(run(&mut bits, &mut index, 6).unwrap(), 6); assert_eq!(index, 12); - assert_eq!(bits.at, 2); + assert_eq!(bits.position(), 2); } #[test] @@ -1081,14 +1132,14 @@ mod tests { #[test] fn qp_escape_and_average_precede_the_quantization_table() { // 23 zeros, the terminating 1, then the symbol in 8 bits. - let mut bits = Bits { src: &[0, 0, 1, 0x18], at: 0 }; + let mut bits = Bits::new(&[0, 0, 1, 0x18]); assert_eq!(bits.qp_rice(0).unwrap(), 24); - assert_eq!(bits.at, 32); + assert_eq!(bits.position(), 32); // 22 zeros are an ordinary Rice prefix. - let mut bits = Bits { src: &[0, 0, 2, 0], at: 0 }; + let mut bits = Bits::new(&[0, 0, 2, 0]); assert_eq!(bits.qp_rice(0).unwrap(), 22); - assert_eq!(bits.at, 23); - assert!(Bits { src: &[0, 0, 0, 0x80, 0], at: 0 }.qp_rice(0).is_err()); + assert_eq!(bits.position(), 23); + assert!(Bits::new(&[0, 0, 0, 0x80, 0]).qp_rice(0).is_err()); // The map's first symbol is a 32-bit literal (300 = QP 150 from prediction 0). let map = qp_map(&[0, 0, 1, 0x2c, 0x94], 1, 4).unwrap(); assert_eq!(map.data, [150; 4]); @@ -1226,4 +1277,148 @@ mod tests { assert_eq!(hash, expected, "{name}"); } } + + /// The bit reader before the 64-bit cache: a checked 5-byte window per read, bit by bit near the end. + struct ReferenceBits<'a> { + src: &'a [u8], + at: usize, + } + + impl ReferenceBits<'_> { + fn peek32(&self) -> Option { + let bytes = self.src.get(self.at / 8..self.at / 8 + 5)?; + let word = u64::from_be_bytes([0, 0, 0, bytes[0], bytes[1], bytes[2], bytes[3], bytes[4]]); + Some((word >> (8 - self.at % 8)) as u32) + } + + fn take(&mut self, count: u32) -> Result { + if count > 24 { + return Err(corrupt("invalid Rice parameter")); + } + if count == 0 { + return Ok(0); + } + if let Some(window) = self.peek32() { + self.at += count as usize; + return Ok(window >> (32 - count)); + } + let mut value = 0; + for _ in 0..count { + let byte = self.src.get(self.at / 8).ok_or_else(|| corrupt("truncated entropy stream"))?; + value = (value << 1) | u32::from((byte >> (7 - self.at % 8)) & 1); + self.at += 1; + } + Ok(value) + } + + fn rice(&mut self, k: u32) -> Result { + if let Some(window) = self.peek32().filter(|&w| w != 0) { + let zeros = window.leading_zeros(); + self.at += zeros as usize + 1; + return if k <= 20 { Ok((zeros << k) | self.take(k)?) } else { Err(corrupt("Rice parameter exceeds sample precision")) }; + } + let mut zeros = 0; + while self.take(1)? == 0 { + zeros += 1; + if zeros > 41 { + return Err(corrupt("Rice escape prefix exceeds 41 bits")); + } + } + if zeros == 41 { + self.take(21) + } else if k <= 20 { + Ok((zeros << k) | self.take(k)?) + } else { + Err(corrupt("Rice parameter exceeds sample precision")) + } + } + + fn qp_rice(&mut self, k: u32) -> Result { + let mut zeros = 0; + while self.take(1)? == 0 { + zeros += 1; + if zeros > QP_ESCAPE_ZEROS { + return Err(corrupt("QP Rice escape prefix exceeds 23 bits")); + } + } + if zeros == QP_ESCAPE_ZEROS { + self.take(8) + } else if k <= 20 { + Ok((zeros << k) | self.take(k)?) + } else { + Err(corrupt("QP Rice parameter exceeds precision")) + } + } + + fn finish(&self) -> bool { + let bits = self.src.len() * 8; + bits.saturating_sub(self.at) <= 7 && self.src.last().is_none_or(|&last| u32::from(last) & ((1u32 << (bits - self.at)) - 1) == 0) + } + } + + /// A xorshift generator: deterministic pseudo-random test input without a dependency. + fn xorshift(seed: &mut u64) -> u64 { + *seed ^= *seed << 13; + *seed ^= *seed >> 7; + *seed ^= *seed << 17; + *seed + } + + #[test] + fn cached_bit_reader_matches_the_bitwise_reference() { + let mut seed = 0x2545_f491_4f6c_dd1d; + for round in 0..400 { + let length = (xorshift(&mut seed) % 48) as usize; + // Mostly zero and small bytes, so long unary prefixes and both escapes occur. + let stream: Vec = (0..length) + .map(|_| match xorshift(&mut seed) % 4 { + 0 | 1 => 0, + 2 => (xorshift(&mut seed) & 0x0f) as u8, + _ => xorshift(&mut seed) as u8, + }) + .collect(); + let ops: Vec<(u64, u32)> = (0..48).map(|_| (xorshift(&mut seed) % 3, (xorshift(&mut seed) % 26) as u32)).collect(); + // Every truncation of the stream, so reads meet the end at every offset. + for end in 0..=stream.len() { + let src = &stream[..end]; + let (mut cached, mut reference) = (Bits::new(src), ReferenceBits { src, at: 0 }); + let mut complete = true; + for &(op, n) in &ops { + let (a, b) = match op { + 0 => (cached.take(n), reference.take(n)), + 1 => (cached.rice(n), reference.rice(n)), + _ => (cached.qp_rice(n % 22), reference.qp_rice(n % 22)), + }; + assert_eq!(a.as_ref().ok(), b.as_ref().ok(), "round {round}, end {end}, op {op} {n}, stream {stream:02x?}"); + if a.is_err() { + complete = false; + break; + } + assert_eq!(cached.position(), reference.at, "round {round}, end {end}, op {op} {n}"); + } + if complete { + assert_eq!(cached.finish().is_ok(), reference.finish(), "round {round}, end {end}"); + } + } + } + } + + #[test] + fn rice_escape_is_an_error_when_cut_at_any_byte() { + // 41 zeros, the terminating 1 and the 21-bit literal 16400 (+8200): the whole stream is one escape. + let src = [0, 0, 0, 0, 0, 0x40, 0x80, 0x20]; + assert_eq!(lossless_plane(&src, 1, 1).unwrap(), [8200]); + for end in 0..src.len() { + assert!(lossless_plane(&src[..end], 1, 1).is_err(), "prefix {end}"); + } + // The same escape read from the cache, 9 bytes into a longer stream. + let mut long = vec![0xff; 9]; + long.extend(src); + let mut bits = Bits::new(&long); + for _ in 0..72 { + assert_eq!(bits.rice(0).unwrap(), 0); + } + assert_eq!(bits.rice(0).unwrap(), 16400); + bits.finish().unwrap(); + } } From ae11e0ae47c190459d15a56cd02e3baa900ae5c5 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 18:16:45 +0100 Subject: [PATCH 081/150] perf(raw): decode C-RAW subbands in parallel, one quantization step per band or QP cell The ten subbands of a plane are separate entropy streams; the first error in band order is still the one reported. The step is looked up once per band, or once per QP cell for adaptive gains, instead of once per coefficient. Part of #1654 Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/crx.rs | 104 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 89 insertions(+), 15 deletions(-) diff --git a/crates/raw/src/crx.rs b/crates/raw/src/crx.rs index f92040f88..e5db20667 100644 --- a/crates/raw/src/crx.rs +++ b/crates/raw/src/crx.rs @@ -413,24 +413,58 @@ fn wavelet_plane(sample: &[u8], plane: &Plane, width: usize, height: usize, edge for high in high_dimensions.iter().rev() { shapes.extend_from_slice(high); } - let mut bands = Vec::with_capacity(10); - for (i, (&(w, h), band)) in shapes.iter().zip(&plane.bands).enumerate() { - let bytes = band_bytes(sample, band)?; - let mut data = if i == 0 { lossless_plane(bytes, w, h)? } else { coded_high_frequency_band(bytes, w, coded_band_width(i, w, edges), h)? }; - for (position, value) in data.iter_mut().enumerate() { - let step = match band.quant { - Quantization::Uniform(value) => quant_step(value, MAX_UNIFORM_QUANT)?, - Quantization::Adaptive { base, gain } => { - adaptive_step(qp.ok_or_else(|| corrupt("missing QP map"))?, i, position % w, position / w, base, gain)? - } - }; - *value = value.checked_mul(step).ok_or_else(|| corrupt("dequantized coefficient overflow"))?; - } - bands.push(WaveletBand { width: w, height: h, data }); - } + // Each subband is its own entropy stream: decode and dequantize them in parallel. The first error + // in band order is reported, as when they were decoded one after another. + let bands: Vec> = shapes + .par_iter() + .zip(plane.bands.par_iter()) + .enumerate() + .map(|(i, (&(w, h), band))| { + let bytes = band_bytes(sample, band)?; + let mut data = if i == 0 { lossless_plane(bytes, w, h)? } else { coded_high_frequency_band(bytes, w, coded_band_width(i, w, edges), h)? }; + dequantize(&mut data, i, w, &band.quant, qp)?; + Ok(WaveletBand { width: w, height: h, data }) + }) + .collect(); + let bands = bands.into_iter().collect::>>()?; crx_wavelet::reconstruct(&bands, width, height, 3, edges) } +/// Multiplies the coefficients of subband `band` (`width` columns) by their quantization step. +/// +/// The step is constant per subband, except for adaptive quantization with a gain, where it is +/// constant per QP cell: 2 coefficients wide in bands 4-6 and 4 wide in bands 7-9 (see +/// [`adaptive_step`]), so it is looked up once per cell rather than per coefficient. +fn dequantize(data: &mut [i32], band: usize, width: usize, quant: &Quantization, qp: Option<&QpMap>) -> Result<()> { + let (map, base, gain) = match *quant { + Quantization::Uniform(value) => return scale(data, quant_step(value, MAX_UNIFORM_QUANT)?), + Quantization::Adaptive { base, gain } => (qp.ok_or_else(|| corrupt("missing QP map"))?, base, gain), + }; + if gain == 0 { + return scale(data, adaptive_step(map, band, 0, 0, base, gain)?); + } + if width == 0 { + return Err(corrupt("empty subband row")); + } + let cell = if (4..=6).contains(&band) { 2 } else { 4 }; + for (y, row) in data.chunks_exact_mut(width).enumerate() { + for (column, values) in row.chunks_mut(cell).enumerate() { + scale(values, adaptive_step(map, band, column * cell, y, base, gain)?)?; + } + } + Ok(()) +} + +fn scale(values: &mut [i32], step: i32) -> Result<()> { + let mut overflow = false; + for value in values.iter_mut() { + let (product, wrapped) = value.overflowing_mul(step); + *value = product; + overflow |= wrapped; + } + if overflow { Err(corrupt("dequantized coefficient overflow")) } else { Ok(()) } +} + fn adaptive_step(map: &QpMap, band: usize, x: usize, y: usize, base: u32, gain: u32) -> Result { let step = if gain == 0 { u64::from(base) @@ -1421,4 +1455,44 @@ mod tests { assert_eq!(bits.rice(0).unwrap(), 16400); bits.finish().unwrap(); } + + #[test] + fn dequantization_per_cell_matches_a_step_per_coefficient() { + let mut seed = 0x9e37_79b9_7f4a_7c15; + // Odd map heights as on the EOS R7, and band sizes that fit the map and that overrun it by one. + for (map_width, map_height) in [(1, 1), (2, 3), (3, 4), (5, 7)] { + let map = QpMap { width: map_width, height: map_height, data: (0..map_width * map_height).map(|_| 131 + (xorshift(&mut seed) % 45) as i32).collect() }; + for band in 0..10 { + for (width, height) in [(1, 1), (3, 2), (2 * map_width - 1, map_height.div_ceil(2)), (2 * map_width + 1, map_height), (4 * map_width, map_height), (4 * map_width + 1, map_height + 1)] { + for quant in [ + Quantization::Uniform(4 + 4 * band as u8), + Quantization::Adaptive { base: 0, gain: 0 }, + Quantization::Adaptive { base: 3, gain: 0 }, + Quantization::Adaptive { base: 1, gain: 8 }, + Quantization::Adaptive { base: 0, gain: 16 }, + ] { + let data: Vec = (0..width * height).map(|_| (xorshift(&mut seed) % 2001) as i32 - 1000).collect(); + let mut cells = data.clone(); + let cells_result = dequantize(&mut cells, band, width, &quant, Some(&map)); + let mut reference = data; + let reference_result = reference.iter_mut().enumerate().try_for_each(|(position, value)| -> Result<()> { + let step = match quant { + Quantization::Uniform(uniform) => quant_step(uniform, MAX_UNIFORM_QUANT)?, + Quantization::Adaptive { base, gain } => adaptive_step(&map, band, position % width, position / width, base, gain)?, + }; + *value = value.checked_mul(step).ok_or_else(|| corrupt("dequantized coefficient overflow"))?; + Ok(()) + }); + let case = format!("map {map_width}x{map_height}, band {band}, {width}x{height}, {quant:?}"); + assert_eq!(cells_result.is_ok(), reference_result.is_ok(), "{case}"); + if cells_result.is_ok() { + assert_eq!(cells, reference, "{case}"); + } + } + } + } + } + assert!(dequantize(&mut [1 << 30], 1, 1, &Quantization::Uniform(10), None).is_err()); + assert!(dequantize(&mut [1], 4, 1, &Quantization::Adaptive { base: 0, gain: 0 }, None).is_err()); + } } From 40e7af21d63a6311b8f62a66dd0d6db7cd302249 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 18:18:37 +0100 Subject: [PATCH 082/150] Record corpus hashes and cover every quantizer table in the differential test Co-Authored-By: Claude Sonnet 5.5 --- crates/raw/src/rafc.rs | 52 +++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/crates/raw/src/rafc.rs b/crates/raw/src/rafc.rs index 8f66ac2fe..c044323e5 100644 --- a/crates/raw/src/rafc.rs +++ b/crates/raw/src/rafc.rs @@ -826,8 +826,9 @@ mod differential { Ok(output) } - /// Deterministic bytes: uniform, or mostly zero (long unary prefixes and escapes). - fn bytes(seed: u64, len: usize, sparse: bool) -> Vec { + /// Deterministic bytes: uniform (0), mostly zero (1: long unary prefixes and escapes) or one-heavy (2: short + /// prefixes and small residuals, which stay inside the range of every quantizer so the stream decodes to the end). + fn bytes(seed: u64, len: usize, kind: u8) -> Vec { let mut x = seed | 1; (0..len) .map(|_| { @@ -835,7 +836,11 @@ mod differential { x ^= x >> 7; x ^= x << 17; let b = (x >> 24) as u8; - if sparse && x & 0b11 != 0 { 0 } else { b } + match kind { + 1 if x & 0b11 != 0 => 0, + 2 => b | (x >> 40) as u8, + _ => b, + } }) .collect() } @@ -855,29 +860,32 @@ mod differential { } /// Every variant the stripe decoder handles (lossless and lossy, Bayer and X-Trans, 12/14/16 bits, full and - /// partial stripes, quantizer changes, fine contexts) gives the oracle's samples, or its error, on dense - /// pseudo-random entropy streams that reach all contexts, and on sparse ones that reach the escapes. + /// partial stripes, quantizer changes, fine contexts) gives the oracle's samples, or its error, on + /// uniform pseudo-random entropy streams that reach all contexts, one-heavy ones that every quantizer table decodes + /// to the end, and sparse ones that reach the escapes. Each quantizer table must have streams that finish. #[test] fn stripe_decoder_matches_the_straightforward_one() { let quantizers: [Option<&[u8]>; 4] = [None, Some([0, 1, 2, 3].as_slice()), Some([2, 2, 7, 0, 40, 255].as_slice()), Some([1, 1, 0, 5, 5, 9, 2].as_slice())]; - let mut decoded = 0; + // uniform and one-heavy streams that decode to the end, per quantizer table + let mut finished = [0usize; 4]; for bits in [12, 14, 16] { for xtrans in [false, true] { - for q in quantizers { - for (seed, width, sparse) in [(1, BLOCK, false), (2, 301, false), (3, 12, false), (4, BLOCK, true)] { - let data = bytes(seed * 31 + u64::from(bits), 24 << 10, sparse); + for (table, q) in quantizers.into_iter().enumerate() { + for (seed, width, kind) in [(1, BLOCK, 0), (2, 301, 0), (3, 12, 0), (4, BLOCK, 1), (5, BLOCK, 2), (6, 301, 2)] { + let data = bytes(seed * 31 + u64::from(bits), 32 << 10, kind); let groups = q.map_or(3, <[u8]>::len); let stripe = Stripe { bytes: &data, quantizers: q, width }; let want = reference_stripe(&stripe, groups * 6, bits, xtrans).map_err(|e| e.to_string()); let got = decode_stripe(&stripe, groups * 6, bits, xtrans).map_err(|e| e.to_string()); - assert_eq!(got, want, "bits {bits}, xtrans {xtrans}, quantizers {q:?}, width {width}, sparse {sparse}"); - decoded += usize::from(want.is_ok()); + assert_eq!(got, want, "bits {bits}, xtrans {xtrans}, quantizers {q:?}, width {width}, stream kind {kind}"); + finished[table] += usize::from(want.is_ok() && kind != 1); } } } } - assert!(decoded >= 24, "only {decoded} of 96 streams decoded to the end, the comparison reaches too little"); + eprintln!("dense streams decoded to the end per quantizer table (of 30): {finished:?}"); + assert!(finished.iter().all(|&n| n > 0), "some quantizer table never decodes to the end: {finished:?}"); } /// Optional: the CC0 files of docs/raf-corpus.sha256 in the directory named by `LIGHTCRAFT_RAF_CORPUS` (file @@ -903,5 +911,23 @@ mod differential { } /// FNV-1a over the little-endian decoded samples, recorded from `upstream/main` before the change. - const RECORDED: &[(&str, u64)] = &[]; + const RECORDED: &[(&str, u64)] = &[ + ("raf-fuji-gfx100-3773.raf", 0x1195d3ab70da7cb4), + ("raf-fuji-gfx100-3775.raf", 0xb7c7c1a7717ef93c), + ("raf-fuji-gfx100rf-8091.raf", 0x49110f1b80eefe2e), + ("raf-fuji-gfx100s-4495.raf", 0x84ab46122f72796c), + ("raf-fuji-gfx100s-4503.raf", 0x63debf0afde73e2e), + ("raf-fuji-gfx50s-1435.raf", 0xb3e570a6c88455f5), + ("raf-fuji-xe5-8509.raf", 0x6a7d5cde2832d4f3), + ("raf-fuji-xh2-6001.raf", 0x466a5707ea484f9c), + ("raf-fuji-xh2-6002.raf", 0xbbf043676476925a), + ("raf-fuji-xm5-7748.raf", 0xedd17960a117a553), + ("raf-fuji-xt2-865.raf", 0xe020816ec114436f), + ("raf-fuji-xt20-compressed.raf", 0x85014025edc2cebd), + ("raf-fuji-xt4-3914.raf", 0x7c98e527dbe01f23), + ("raf-fuji-xt4-3918.raf", 0xfb4ba0df34c1c904), + ("raf-fuji-xt5-6122.raf", 0x8f76abd57de79a94), + ("raf-fuji-xt5-6123.raf", 0x0b2784853c31278d), + ("raf-fuji-xt50-7807.raf", 0xa8e2be9cac387ebc), + ]; } From 9ea6cf1ec34e41632a9b226b7e537a0611d4f93b Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 18:18:47 +0100 Subject: [PATCH 083/150] perf(raw): decode all CR3 tiles in parallel and interleave them by row pairs Every plane of every tile is one parallel task that ends in sensor samples; the mosaic is then filled per output row pair in parallel, without a modulo, division, bounds check or Result per sample. The first error in tile and plane order is still the one reported, and the sensor buffer is allocated only after every plane has decoded. The CC0 corpus test also pins the four EOS R5 Mark II files and skips files that are not present. Part of #1654 Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/crx.rs | 225 ++++++++++++++++++++++++++++++++---------- 1 file changed, 173 insertions(+), 52 deletions(-) diff --git a/crates/raw/src/crx.rs b/crates/raw/src/crx.rs index e5db20667..2c68409d2 100644 --- a/crates/raw/src/crx.rs +++ b/crates/raw/src/crx.rs @@ -95,60 +95,94 @@ pub(crate) fn decode(sample: &[u8], config: &Cr3Compression) -> Result> let count = image_size(config)?; let tiles = layout(sample, config)?; validate_planes(&tiles, config)?; - // Allocated once the first plane decodes: a tiny crafted file can claim a huge image. - let mut output = Vec::new(); + let qp = tiles.iter().map(|tile| tile_qp(sample, tile)).collect::>>()?; let midpoint = 1i32 << (config.bit_depth - 1); let maximum = (1i32 << config.bit_depth) - 1; - for tile in tiles { - let qp = tile_qp(sample, &tile)?; - // Each plane is its own entropy stream: decode a tile's planes in parallel. - let planes: Vec>> = tile - .planes - .par_iter() - .enumerate() - .map(|(p, plane)| { - if config.levels == 0 { - let band = plane.bands.first().ok_or_else(|| corrupt("missing lossless band"))?; - lossless_plane(band_bytes(sample, band)?, tile.width / 2, tile.height / 2) - } else { - let edges = TileEdges { left: tile.x > 0, right: tile.x + tile.width < width, top: false, bottom: false }; - wavelet_plane(sample, plane, tile.width / 2, tile.height / 2, edges, qp.as_ref()) - } - .map_err(|error| RawError::Corrupt(format!("Canon CRX tile ({}, {}), plane {p}: {error}", tile.x, tile.y))) - }) - .collect(); - for (p, decoded) in planes.into_iter().enumerate() { - let decoded = decoded?; - if output.is_empty() { - output.try_reserve_exact(count).map_err(|_| RawError::Limit("CRX sample allocation"))?; - output.resize(count, 0); - } - for (i, &value) in decoded.iter().enumerate() { - let sensor_value = sensor_sample(value, midpoint, maximum, config.levels != 0)?; - let (x, y) = (tile.x + 2 * (i % (tile.width / 2)) + p % 2, tile.y + 2 * (i / (tile.width / 2)) + p / 2); - let slot = y - .checked_mul(width) - .and_then(|n| n.checked_add(x)) - .and_then(|n| output.get_mut(n)) - .ok_or_else(|| corrupt("tile writes outside image"))?; - *slot = sensor_value; - } - } - } + // Every plane of every tile is its own entropy stream: decode them all in parallel, each straight + // to sensor samples, so a task holds only its own plane as i32. The first error in tile and plane + // order is reported, as when the tiles were decoded one after another. + let jobs: Vec<(&Tile, Option<&QpMap>, usize, &Plane)> = tiles + .iter() + .zip(&qp) + .flat_map(|(tile, qp)| tile.planes.iter().enumerate().map(move |(p, plane)| (tile, qp.as_ref(), p, plane))) + .collect(); + let planes: Vec>> = jobs + .par_iter() + .map(|&(tile, qp, p, plane)| { + let decoded = decode_plane(sample, config, width, tile, plane, qp) + .map_err(|error| RawError::Corrupt(format!("Canon CRX tile ({}, {}), plane {p}: {error}", tile.x, tile.y)))?; + sensor_plane(&decoded, midpoint, maximum, config.levels != 0) + }) + .collect(); + let planes = planes.into_iter().collect::>>()?; + // Allocated once every plane has decoded: a tiny crafted file can claim a huge image. + let mut output = Vec::new(); + output.try_reserve_exact(count).map_err(|_| RawError::Limit("CRX sample allocation"))?; + output.resize(count, 0); + interleave(&mut output, width, &tiles, &planes)?; Ok(output) } -/// A decoded plane value as a sensor sample of the CMP1 bit depth. +fn decode_plane(sample: &[u8], config: &Cr3Compression, width: usize, tile: &Tile, plane: &Plane, qp: Option<&QpMap>) -> Result> { + if config.levels == 0 { + let band = plane.bands.first().ok_or_else(|| corrupt("missing lossless band"))?; + lossless_plane(band_bytes(sample, band)?, tile.width / 2, tile.height / 2) + } else { + let edges = TileEdges { left: tile.x > 0, right: tile.x + tile.width < width, top: false, bottom: false }; + wavelet_plane(sample, plane, tile.width / 2, tile.height / 2, edges, qp) + } +} + +/// Decoded plane values as sensor samples of the CMP1 bit depth. /// /// Lossless planes reproduce the sensor exactly, so a value outside the bit depth is corrupt data. /// Wavelet (C-RAW) planes are quantized: next to clipped highlights the reconstruction overshoots /// the sensor's range by up to a few quantization steps (measured on 17 CC0 files: at most 36 codes /// above 16383 at 14 bits, never below 0), while lossless files of the same bodies clip at exactly -/// 16383. Those samples are clamped to the range the sensor can record. -fn sensor_sample(value: i32, midpoint: i32, maximum: i32, quantized: bool) -> Result { - let sample = value.checked_add(midpoint).ok_or_else(|| corrupt("decoded sample outside its bit depth"))?; - let sample = if quantized { sample.clamp(0, maximum) } else { sample }; - u16::try_from(sample).ok().filter(|_| sample <= maximum).ok_or_else(|| corrupt("decoded sample outside its bit depth")) +/// 16383. Those samples are clamped to the range the sensor can record. The range is checked once, +/// on the plane's extremes; `midpoint` is positive, so only the largest sum can overflow. +fn sensor_plane(values: &[i32], midpoint: i32, maximum: i32, quantized: bool) -> Result> { + let outside = || corrupt("decoded sample outside its bit depth"); + let (Some(&low), Some(&high)) = (values.iter().min(), values.iter().max()) else { return Ok(Vec::new()) }; + let high = high.checked_add(midpoint).ok_or_else(outside)?; + if !quantized && (low.saturating_add(midpoint) < 0 || high > maximum) { + return Err(outside()); + } + let mut samples = Vec::new(); + samples.try_reserve_exact(values.len()).map_err(|_| RawError::Limit("CRX sample allocation"))?; + // Every sum fits (the largest did), and the clamp leaves in-range lossless samples unchanged. + samples.extend(values.iter().map(|&value| (value + midpoint).clamp(0, maximum) as u16)); + Ok(samples) +} + +/// Writes each tile's four planes (`planes`: four per tile, in tile order) into the Bayer mosaic. +/// Plane `p` holds the samples at row parity `p / 2` and column parity `p % 2`. Runs in parallel +/// over pairs of output rows. +fn interleave(output: &mut [u16], width: usize, tiles: &[Tile], planes: &[Vec]) -> Result<()> { + let outside = || corrupt("tile writes outside image"); + if width == 0 || planes.len() != tiles.len() * 4 { + return Err(outside()); + } + output.par_chunks_mut(2 * width).enumerate().try_for_each(|(pair, rows)| -> Result<()> { + let y = 2 * pair; + for (tile, planes) in tiles.iter().zip(planes.chunks_exact(4)).filter(|(tile, _)| (tile.y..tile.y + tile.height).contains(&y)) { + let half = tile.width / 2; + let start = (y - tile.y) / 2 * half; + for (row, sources) in rows.chunks_exact_mut(width).zip(planes.chunks_exact(2)) { + let [even, odd] = sources else { return Err(outside()) }; + let target = row.get_mut(tile.x..tile.x + tile.width).ok_or_else(outside)?; + let even = even.get(start..start + half).ok_or_else(outside)?; + let odd = odd.get(start..start + half).ok_or_else(outside)?; + for ((pixels, &left), &right) in target.chunks_exact_mut(2).zip(even).zip(odd) { + if let [a, b] = pixels { + *a = left; + *b = right; + } + } + } + } + Ok(()) + }) } /// Header-only probing must reject the same unsupported coding variants as full decoding. @@ -1128,14 +1162,22 @@ mod tests { #[test] fn only_quantized_planes_clamp_out_of_range_samples() { - assert_eq!(sensor_sample(8200, 8192, 16383, true).unwrap(), 16383); - assert_eq!(sensor_sample(-8300, 8192, 16383, true).unwrap(), 0); - assert_eq!(sensor_sample(8191, 8192, 16383, false).unwrap(), 16383); - assert_eq!(sensor_sample(-8192, 8192, 16383, false).unwrap(), 0); + assert_eq!(sensor_plane(&[8200], 8192, 16383, true).unwrap(), [16383]); + assert_eq!(sensor_plane(&[-8300], 8192, 16383, true).unwrap(), [0]); + assert_eq!(sensor_plane(&[-8300, 0, 8200], 8192, 16383, true).unwrap(), [0, 8192, 16383]); + assert_eq!(sensor_plane(&[8191], 8192, 16383, false).unwrap(), [16383]); + assert_eq!(sensor_plane(&[-8192], 8192, 16383, false).unwrap(), [0]); + assert_eq!(sensor_plane(&[-8192, 5, 8191], 8192, 16383, false).unwrap(), [0, 8197, 16383]); // Lossless planes reproduce the sensor exactly: outside the bit depth is corrupt data. - assert!(matches!(sensor_sample(8192, 8192, 16383, false), Err(RawError::Corrupt(_)))); - assert!(matches!(sensor_sample(-8193, 8192, 16383, false), Err(RawError::Corrupt(_)))); - assert!(matches!(sensor_sample(i32::MAX, 8192, 16383, true), Err(RawError::Corrupt(_)))); + assert!(matches!(sensor_plane(&[8192], 8192, 16383, false), Err(RawError::Corrupt(_)))); + assert!(matches!(sensor_plane(&[-8193], 8192, 16383, false), Err(RawError::Corrupt(_)))); + assert!(matches!(sensor_plane(&[0, 8192, 0], 8192, 16383, false), Err(RawError::Corrupt(_)))); + assert!(matches!(sensor_plane(&[0, -8193, 0], 8192, 16383, false), Err(RawError::Corrupt(_)))); + assert!(matches!(sensor_plane(&[i32::MAX], 8192, 16383, true), Err(RawError::Corrupt(_)))); + assert!(matches!(sensor_plane(&[0, i32::MAX], 8192, 16383, true), Err(RawError::Corrupt(_)))); + // The lowest i32 cannot overflow below: it is clamped in a quantized plane and corrupt in a lossless one. + assert_eq!(sensor_plane(&[i32::MIN], 8192, 16383, true).unwrap(), [0]); + assert!(matches!(sensor_plane(&[i32::MIN], 8192, 16383, false), Err(RawError::Corrupt(_)))); } #[test] @@ -1288,9 +1330,18 @@ mod tests { ("cr3-canon-rp-craw.cr3", 0xf48df06ca1d67382u64), ("cr3-canon-rp-raw-crop.cr3", 0x46808a9de3b3dcc7u64), ("cr3-canon-rp-craw-crop.cr3", 0x39a60c4efed4e1f4u64), + // EOS R5 Mark II (version 0x200 C-RAW and its lossless pair): pinned to this decoder's + // own output, see docs/cr3.md. + ("cr3-canon-r5m2-raw.cr3", 0xff716fb30248496fu64), + ("cr3-canon-r5m2-craw.cr3", 0xaf32b45357b534e5u64), + ("cr3-canon-r5m2-raw-crop.cr3", 0x31cf630afe868958u64), + ("cr3-canon-r5m2-craw-crop.cr3", 0x4721605b20339332u64), ] { let path = std::path::Path::new(&directory).join(name); - let bytes = std::fs::read(path).unwrap(); + let Ok(bytes) = std::fs::read(&path) else { + eprintln!("skipped {}: not in LIGHTCRAFT_CR3_CORPUS", path.display()); + continue; + }; let container = craft_metadata::cr3::parse_cr3(&bytes).unwrap(); let mut candidates = container .tracks @@ -1495,4 +1546,74 @@ mod tests { assert!(dequantize(&mut [1 << 30], 1, 1, &Quantization::Uniform(10), None).is_err()); assert!(dequantize(&mut [1], 4, 1, &Quantization::Adaptive { base: 0, gain: 0 }, None).is_err()); } + + #[test] + fn interleave_places_every_plane_of_every_tile_like_a_per_sample_scatter() { + // A 2 x 2 tile grid whose right column is narrower, as at the right edge of a real image. + let (width, height) = (12, 8); + let tile = |x, y, width, height| Tile { x, y, width, height, planes: Vec::new(), qp: None }; + let tiles = [tile(0, 0, 8, 4), tile(8, 0, 4, 4), tile(0, 4, 8, 4), tile(8, 4, 4, 4)]; + let planes: Vec> = tiles + .iter() + .enumerate() + .flat_map(|(t, tile)| (0..4).map(move |p| (0..tile.width * tile.height / 4).map(|i| ((t * 4 + p) * 100 + i) as u16).collect())) + .collect(); + let mut expected = vec![0u16; width * height]; + for (t, tile) in tiles.iter().enumerate() { + for p in 0..4 { + for (i, &value) in planes[t * 4 + p].iter().enumerate() { + let (x, y) = (tile.x + 2 * (i % (tile.width / 2)) + p % 2, tile.y + 2 * (i / (tile.width / 2)) + p / 2); + expected[y * width + x] = value; + } + } + } + let mut output = vec![0u16; width * height]; + interleave(&mut output, width, &tiles, &planes).unwrap(); + assert_eq!(output, expected); + // A plane shorter than its tile is an error, not a partial image. + let mut short = planes.clone(); + short[5].pop(); + assert!(interleave(&mut output, width, &tiles, &short).is_err()); + assert!(interleave(&mut output, width, &tiles, &planes[..12]).is_err()); + } + + #[test] + fn lossless_tile_grid_decodes_each_plane_to_its_place() { + // Four 4 x 4 tiles in a 2 x 2 grid (horizontal and vertical tile boundaries). Each 2 x 2 plane + // is flat at value 1, 2 or 3, chosen per tile and plane. Bitstreams per value: row 0 has a + // zero-run flag 0, the Rice-coded first sample and a 0 symbol; row 1 is one run to the row end. + let stream = |value: usize| -> &'static [u8] { + match value { + 1 => &[0x1e], // 0, 001 (k 0), 1 (k 0), 1, 1 + 2 => &[0x06, 0xc0], // 0, 00001 (k 0), 10 (k 1), 1, 1 + _ => &[0x01, 0x98], // 0, 0000001 (k 0), 100 (k 2), 1, 1 + } + }; + let value = |t: usize, p: usize| 1 + (t + p) % 3; + let mut config = coding(); + config.width = 8; + config.height = 8; + config.header_size = 432; + let mut sample = Vec::new(); + let mut data = Vec::new(); + for t in 0..4 { + let sizes: Vec = (0..4).map(|p| stream(value(t, p)).len() as u32).collect(); + marker(&mut sample, 0xff01, sizes.iter().sum(), (t as u32) << 16); + for (p, &size) in sizes.iter().enumerate() { + marker(&mut sample, 0xff02, size, ((p as u32) << 28) | 0x0800_0000); + marker(&mut sample, 0xff03, size, 0x0020_0000); + data.extend_from_slice(stream(value(t, p))); + } + } + assert_eq!(sample.len(), 432); + sample.extend(data); + validate(&sample, &config).unwrap(); + let expected: Vec = + (0..64).map(|i| (i % 8, i / 8)).map(|(x, y)| 8192 + value((y / 4) * 2 + x / 4, (y % 2) * 2 + x % 2) as u16).collect(); + assert_eq!(decode(&sample, &config).unwrap(), expected); + // A corrupt plane in the last tile still fails the whole image. + let mut broken = sample.clone(); + *broken.last_mut().unwrap() ^= 1; + assert!(decode(&broken, &config).is_err()); + } } From 7566f61bad3d9efc6c03ecb021e9caa4099c9420 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 18:20:39 +0100 Subject: [PATCH 084/150] perf(raw): run CRX wavelet synthesis on whole rows, in parallel The vertical lifting combined strided columns one at a time; it now lifts whole rows, per pair of output rows in parallel, with the same integer equations and edge rules (i64 floor shifts equal the div_euclid it replaces). The horizontal pass runs its rows in parallel and drops the Result per sample; overflow is still an error. Differential tests compare both passes with the previous implementation for every size 1..=17 and every edge combination. Part of #1654 Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/crx_wavelet.rs | 272 ++++++++++++++++++++++++++++------ 1 file changed, 226 insertions(+), 46 deletions(-) diff --git a/crates/raw/src/crx_wavelet.rs b/crates/raw/src/crx_wavelet.rs index ac5e51d1d..9e0197670 100644 --- a/crates/raw/src/crx_wavelet.rs +++ b/crates/raw/src/crx_wavelet.rs @@ -6,6 +6,7 @@ //! neighbouring low/high coefficients; only outer image edges are symmetric. use crate::{RawError, Result}; +use rayon::prelude::*; #[derive(Clone, Debug)] pub(crate) struct Band { @@ -18,10 +19,6 @@ fn corrupt() -> RawError { RawError::Corrupt("invalid CRX wavelet subband geometry".into()) } -fn checked_sample(v: i64) -> Result { - i32::try_from(v).map_err(|_| RawError::Corrupt("CRX wavelet coefficient overflow".into())) -} - #[derive(Clone, Copy, Debug, Default)] pub(crate) struct TileEdges { pub left: bool, @@ -34,40 +31,112 @@ fn sizes(length: usize, before: bool, after: bool) -> (usize, usize) { (length.div_ceil(2) + usize::from(after && length.is_multiple_of(2)), length / 2 + usize::from(before) + usize::from(after)) } +/// Whether a lifted value still fits the i32 coefficient range. +fn fits(value: i64) -> bool { + i32::try_from(value).is_ok() +} + +fn overflow() -> RawError { + RawError::Corrupt("CRX wavelet coefficient overflow".into()) +} + fn inverse_line(low: &[i32], high: &[i32], out: &mut [i32], before: bool, after: bool) -> Result<()> { let (low_count, high_count) = sizes(out.len(), before, after); if out.is_empty() || low.len() != low_count || high.len() != high_count { return Err(corrupt()); } - if high.is_empty() { + let Some(&last_high) = high.last() else { let value = low.first().copied().ok_or_else(corrupt)?; *out.first_mut().ok_or_else(corrupt)? = value; return Ok(()); - } + }; // The high-pass storage starts at -1 when there is a preceding tile. // Only absent external neighbours are reflected at the image boundary. - let high_at = |index: usize| -> Result { high.get(index.min(high.len().saturating_sub(1))).copied().ok_or_else(corrupt) }; - let even = |i: usize| -> Result { - let left = high_at((i + usize::from(before)).saturating_sub(1))?; - let right = high_at(i + usize::from(before))?; - let update = (i64::from(left) + i64::from(right) + 2).div_euclid(4); - checked_sample(i64::from(*low.get(i).ok_or_else(corrupt)?) - update) + let shift = usize::from(before); + let high_at = |index: usize| i64::from(high.get(index).copied().unwrap_or(last_high)); + let even = |i: usize, value: i32| i64::from(value) - ((high_at((i + shift).saturating_sub(1)) + high_at(i + shift) + 2) >> 2); + let mut in_range = true; + for (i, (slot, &value)) in out.iter_mut().step_by(2).zip(low).enumerate() { + let lifted = even(i, value); + in_range &= fits(lifted); + *slot = lifted as i32; + } + // With a following tile, the last odd sample's right neighbour is the even sample past the end. + let length = out.len(); + let past_end = if after && length.is_multiple_of(2) { low.get(length / 2).map(|&value| even(length / 2, value)) } else { None }; + in_range &= past_end.is_none_or(fits); + for i in 0..length / 2 { + let left = out.get(2 * i).copied().map_or(0, i64::from); + let right = out.get(2 * i + 2).copied().map(i64::from).or(past_end).unwrap_or(left); + let lifted = high_at(i + shift) + ((left + right) >> 1); + in_range &= fits(lifted); + if let Some(slot) = out.get_mut(2 * i + 1) { + *slot = lifted as i32; + } + } + if in_range { Ok(()) } else { Err(overflow()) } +} + +/// `out = low - floor((left + right + 2) / 4)` per column: one even row of the vertical lifting. +fn even_row(out: &mut [i32], low: &[i32], left: &[i32], right: &[i32]) -> bool { + let mut in_range = true; + for (((slot, &l), &a), &b) in out.iter_mut().zip(low).zip(left).zip(right) { + let lifted = i64::from(l) - ((i64::from(a) + i64::from(b) + 2) >> 2); + in_range &= fits(lifted); + *slot = lifted as i32; + } + in_range +} + +/// `out = high + floor((above + below) / 2)` per column: one odd row of the vertical lifting. +fn odd_row(out: &mut [i32], high: &[i32], above: &[i32], below: &[i32]) -> bool { + let mut in_range = true; + for (((slot, &h), &a), &b) in out.iter_mut().zip(high).zip(above).zip(below) { + let lifted = i64::from(h) + ((i64::from(a) + i64::from(b)) >> 1); + in_range &= fits(lifted); + *slot = lifted as i32; + } + in_range +} + +/// [`inverse_line`] applied to every column of `out`, on whole rows instead of strided columns. +/// +/// `rows` holds the low-pass rows, then the high-pass rows, each `width` wide. Runs in parallel over +/// pairs of output rows: each pair lifts its even row, recomputes the next even row (the following +/// pair's, or the one a following tile supplies past the end) and lifts its odd row from both. +fn inverse_columns(rows: &[i32], out: &mut [i32], width: usize, before: bool, after: bool) -> Result<()> { + let height = out.len().checked_div(width).filter(|&h| h > 0 && h * width == out.len()).ok_or_else(corrupt)?; + let (low_count, high_count) = sizes(height, before, after); + if rows.len() != (low_count + high_count) * width { + return Err(corrupt()); + } + let (low, high) = (rows.get(..low_count * width).ok_or_else(corrupt)?, rows.get(low_count * width..).ok_or_else(corrupt)?); + let low_row = |i: usize| low.get(i * width..(i + 1) * width).ok_or_else(corrupt); + let Some(last_high) = high_count.checked_sub(1) else { + let first = out.get_mut(..width).ok_or_else(corrupt)?; + first.copy_from_slice(low_row(0)?); + return Ok(()); }; - for i in 0..out.len().div_ceil(2) { - *out.get_mut(i * 2).ok_or_else(corrupt)? = even(i)?; - } - for i in 0..out.len() / 2 { - let left = out.get(i * 2).copied().ok_or_else(corrupt)?; - let right = match out.get(i * 2 + 2).copied() { - Some(value) => value, - None if after => even(i + 1)?, - None => left, - }; - let prediction = (i64::from(left) + i64::from(right)).div_euclid(2); - let value = high_at(i + usize::from(before))?; - *out.get_mut(i * 2 + 1).ok_or_else(corrupt)? = checked_sample(i64::from(value) + prediction)?; - } - Ok(()) + let high_row = |i: usize| { + let i = i.min(last_high); + high.get(i * width..(i + 1) * width).ok_or_else(corrupt) + }; + let shift = usize::from(before); + out.par_chunks_mut(2 * width).enumerate().try_for_each_init(Vec::new, |next, (i, pair)| -> Result<()> { + let (even, odd) = pair.split_at_mut(width); + let mut in_range = even_row(even, low_row(i)?, high_row((i + shift).saturating_sub(1))?, high_row(i + shift)?); + if !odd.is_empty() { + let below: &[i32] = if 2 * i + 2 < height || after { + next.resize(width, 0); + in_range &= even_row(next, low_row(i + 1)?, high_row(i + shift)?, high_row(i + shift + 1)?); + next.as_slice() + } else { + &*even + }; + in_range &= odd_row(odd, high_row(i + shift)?, even, below); + } + if in_range { Ok(()) } else { Err(overflow()) } + }) } fn valid_band(band: &Band, width: usize, height: usize) -> bool { @@ -97,32 +166,18 @@ pub(crate) fn inverse_level(ll: &Band, hl: &Band, lh: &Band, hh: &Band, width: u let mut temporary = zeroed(intermediate_count)?; // CRX analysis is vertical then horizontal. Integer rounding means that // synthesis must reverse that order, including for quantized subbands. - for y in 0..lhgt + hhgt { + temporary.par_chunks_mut(width).enumerate().try_for_each(|(y, row)| { let (lo, hi, by) = if y < lhgt { (ll, hl, y) } else { (lh, hh, y - lhgt) }; inverse_line( lo.data.get(by * lw..(by + 1) * lw).ok_or_else(corrupt)?, hi.data.get(by * hw..(by + 1) * hw).ok_or_else(corrupt)?, - temporary.get_mut(y * width..(y + 1) * width).ok_or_else(corrupt)?, + row, edges.left, edges.right, - )?; - } + ) + })?; let mut data = zeroed(count)?; - let mut low = zeroed(lhgt)?; - let mut high = zeroed(hhgt)?; - let mut column = zeroed(height)?; - for x in 0..width { - for (y, value) in low.iter_mut().enumerate() { - *value = temporary.get(y * width + x).copied().ok_or_else(corrupt)?; - } - for (y, value) in high.iter_mut().enumerate() { - *value = temporary.get((y + lhgt) * width + x).copied().ok_or_else(corrupt)?; - } - inverse_line(&low, &high, &mut column, edges.top, edges.bottom)?; - for (y, &value) in column.iter().enumerate() { - *data.get_mut(y * width + x).ok_or_else(corrupt)? = value; - } - } + inverse_columns(&temporary, &mut data, width, edges.top, edges.bottom)?; Ok(Band { width, height, data }) } @@ -335,4 +390,129 @@ mod tests { assert!(reconstruct(&bands, 20, 9, 3, TileEdges::default()).is_err()); } } + + /// The lifting before the row-wise rewrite: a checked closure per sample, as a reference. + fn reference_line(low: &[i32], high: &[i32], out: &mut [i32], before: bool, after: bool) -> Result<()> { + let (low_count, high_count) = sizes(out.len(), before, after); + if out.is_empty() || low.len() != low_count || high.len() != high_count { + return Err(corrupt()); + } + if high.is_empty() { + out[0] = low[0]; + return Ok(()); + } + let checked = |v: i64| i32::try_from(v).map_err(|_| overflow()); + let high_at = |index: usize| high[index.min(high.len() - 1)]; + let even = |i: usize| -> Result { + let left = high_at((i + usize::from(before)).saturating_sub(1)); + let right = high_at(i + usize::from(before)); + checked(i64::from(low[i]) - (i64::from(left) + i64::from(right) + 2).div_euclid(4)) + }; + for i in 0..out.len().div_ceil(2) { + out[i * 2] = even(i)?; + } + for i in 0..out.len() / 2 { + let left = out[i * 2]; + let right = match out.get(i * 2 + 2).copied() { + Some(value) => value, + None if after => even(i + 1)?, + None => left, + }; + out[i * 2 + 1] = checked(i64::from(high_at(i + usize::from(before))) + (i64::from(left) + i64::from(right)).div_euclid(2))?; + } + Ok(()) + } + + /// One level before the row-wise rewrite: horizontal lines, then strided columns. + fn reference_level(bands: &[Band; 4], width: usize, height: usize, edges: TileEdges) -> Result> { + let [ll, hl, lh, hh] = bands; + let (lhgt, hhgt) = sizes(height, edges.top, edges.bottom); + let mut temporary = vec![0; width * (lhgt + hhgt)]; + for y in 0..lhgt + hhgt { + let (lo, hi, by) = if y < lhgt { (ll, hl, y) } else { (lh, hh, y - lhgt) }; + reference_line( + &lo.data[by * lo.width..(by + 1) * lo.width], + &hi.data[by * hi.width..(by + 1) * hi.width], + &mut temporary[y * width..(y + 1) * width], + edges.left, + edges.right, + )?; + } + let mut data = vec![0; width * height]; + let mut column = vec![0; height]; + for x in 0..width { + let low: Vec = (0..lhgt).map(|y| temporary[y * width + x]).collect(); + let high: Vec = (lhgt..lhgt + hhgt).map(|y| temporary[y * width + x]).collect(); + reference_line(&low, &high, &mut column, edges.top, edges.bottom)?; + for (y, &value) in column.iter().enumerate() { + data[y * width + x] = value; + } + } + Ok(data) + } + + fn xorshift(seed: &mut u64) -> u64 { + *seed ^= *seed << 13; + *seed ^= *seed >> 7; + *seed ^= *seed << 17; + *seed + } + + /// Small coefficients, or (with `extreme`) values near the i32 limits so lifting can overflow. + fn coefficients(seed: &mut u64, count: usize, extreme: bool) -> Vec { + (0..count) + .map(|_| { + let r = xorshift(seed); + if extreme && r % 5 == 0 { if r & 32 == 0 { i32::MAX - (r >> 40) as i32 % 4 } else { i32::MIN + (r >> 40) as i32 % 4 } } else { (r % 2001) as i32 - 1000 } + }) + .collect() + } + + const EDGE_CASES: [(bool, bool); 4] = [(false, false), (true, false), (false, true), (true, true)]; + + #[test] + fn line_lifting_matches_the_reference_for_every_length_and_edge() { + let mut seed = 0x0123_4567_89ab_cdef; + for length in 1..=40 { + for (before, after) in EDGE_CASES { + let (low_count, high_count) = sizes(length, before, after); + for extreme in [false, true] { + let low = coefficients(&mut seed, low_count, extreme); + let high = coefficients(&mut seed, high_count, extreme); + let (mut fast, mut reference) = (vec![0; length], vec![0; length]); + let fast_result = inverse_line(&low, &high, &mut fast, before, after); + let reference_result = reference_line(&low, &high, &mut reference, before, after); + let case = format!("length {length}, before {before}, after {after}, extreme {extreme}"); + assert_eq!(fast_result.is_ok(), reference_result.is_ok(), "{case}"); + if fast_result.is_ok() { + assert_eq!(fast, reference, "{case}"); + } + } + } + } + } + + #[test] + fn row_wise_level_matches_column_lifting_for_every_size_and_edge() { + let mut seed = 0xfeed_beef_dead_cafe; + for width in 1..=17 { + for height in 1..=17 { + for (left, right) in EDGE_CASES { + for (top, bottom) in EDGE_CASES { + let edges = TileEdges { left, right, top, bottom }; + let (lw, hw) = sizes(width, left, right); + let (lhgt, hhgt) = sizes(height, top, bottom); + // Overflow cases on the smaller sizes only; they fail early and are cheap. + let extreme = width * height < 40 && xorshift(&mut seed) % 2 == 0; + let mut band = |w, h| Band { width: w, height: h, data: coefficients(&mut seed, w * h, extreme) }; + let bands = [band(lw, lhgt), band(hw, lhgt), band(lw, hhgt), band(hw, hhgt)]; + let fast = inverse_level(&bands[0], &bands[1], &bands[2], &bands[3], width, height, edges); + let reference = reference_level(&bands, width, height, edges); + let case = format!("{width}x{height}, {edges:?}, extreme {extreme}"); + assert_eq!(fast.as_ref().ok().map(|band| &band.data), reference.as_ref().ok(), "{case}"); + } + } + } + } + } } From b3b8521ab4029033a9ffc4ebad53f4901cbdf639 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:24:59 +0000 Subject: [PATCH 085/150] Initialize JPEG budget regression limits directly --- crates/image-codecs/src/allocation.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/image-codecs/src/allocation.rs b/crates/image-codecs/src/allocation.rs index 6ba09f569..df9afd93f 100644 --- a/crates/image-codecs/src/allocation.rs +++ b/crates/image-codecs/src/allocation.rs @@ -311,8 +311,7 @@ mod stored_size_tests { let mut out = Cursor::new(Vec::new()); image::write_buffer_with_format(&mut out, &[10; 18], 3, 2, image::ExtendedColorType::Rgb8, image::ImageFormat::Jpeg).unwrap(); let bytes = out.into_inner(); - let mut limits = crate::Limits::default(); - limits.max_alloc = bytes.len() as u64; + let mut limits = crate::Limits { max_alloc: bytes.len() as u64, ..Default::default() }; assert_eq!(probe_stored_size(&bytes, &limits).unwrap(), (3, 2)); limits.max_alloc -= 1; assert!(probe_stored_size(&bytes, &limits).is_err()); From 308e5c8519261a963ec5f614c04a79c4a4c404a9 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 18:27:31 +0100 Subject: [PATCH 086/150] test(raw): name the plane type in the interleave test Part of #1654 Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/crx.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/raw/src/crx.rs b/crates/raw/src/crx.rs index 2c68409d2..7374d41f6 100644 --- a/crates/raw/src/crx.rs +++ b/crates/raw/src/crx.rs @@ -1556,7 +1556,7 @@ mod tests { let planes: Vec> = tiles .iter() .enumerate() - .flat_map(|(t, tile)| (0..4).map(move |p| (0..tile.width * tile.height / 4).map(|i| ((t * 4 + p) * 100 + i) as u16).collect())) + .flat_map(|(t, tile)| (0..4).map(move |p| (0..tile.width * tile.height / 4).map(|i| ((t * 4 + p) * 100 + i) as u16).collect::>())) .collect(); let mut expected = vec![0u16; width * height]; for (t, tile) in tiles.iter().enumerate() { From c9de09877ba38dba838ade37ba105032357709a9 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 12:29:34 -0500 Subject: [PATCH 087/150] Share bounded BMP coverage inspection (#1610) --- crates/image-codecs/src/bmp_probe.rs | 145 +++++++++++++++++++++++++++ crates/image-codecs/src/lib.rs | 1 + 2 files changed, 146 insertions(+) create mode 100644 crates/image-codecs/src/bmp_probe.rs diff --git a/crates/image-codecs/src/bmp_probe.rs b/crates/image-codecs/src/bmp_probe.rs new file mode 100644 index 000000000..59c78ca1e --- /dev/null +++ b/crates/image-codecs/src/bmp_probe.rs @@ -0,0 +1,145 @@ +//! Header-only BMP dimensions and pixel-data coverage, without allocating a canvas. + +/// Coverage of the declared pixel array. Row byte counts include row padding; RLE coverage +/// counts the pixels drawn by commands and may be smaller than the canvas. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Coverage { + pub pixels: u64, + pub row_bytes: Option<(u64, u64)>, +} + +/// Inspect pixel data for the given dimensions without decoding a bitmap. The row tuple is +/// `(available, needed)`; callers choose their own sparse-canvas and allocation policy. +pub fn coverage(bytes: &[u8], w: u32, h: u32) -> Coverage { + match bmp_layout(bytes) { + BmpLayout::Rle { offset, rle4 } => Coverage { pixels: bmp_rle_pixels(bytes, offset, rle4, w, h), row_bytes: None }, + BmpLayout::Rows { offset, bits } => { + let needed = u64::from(w).saturating_mul(u64::from(bits)).div_ceil(32).saturating_mul(4).saturating_mul(u64::from(h)); + let held = bytes.len().saturating_sub(offset) as u64; + Coverage { pixels: u64::from(w) * u64::from(h), row_bytes: Some((held, needed)) } + } + } +} + +enum BmpLayout { + /// RLE8 or RLE4 commands. + Rle { offset: usize, rle4: bool }, + /// Rows of `bits` per pixel, each padded to 4 bytes. + Rows { offset: usize, bits: u16 }, +} + +/// The layout the decoder reads, from the file header and the bitmap header: a 12-byte core header +/// (bits per pixel at 24, never compressed) or a 40-byte or larger one (bits at 28, compression at +/// 30). Fields the decoder would have refused are read as 1 bit per pixel at offset 0, the most +/// lenient layout. +fn bmp_layout(bytes: &[u8]) -> BmpLayout { + let u16_at = |i: usize| bytes.get(i..i.checked_add(2)?).and_then(|b| b.try_into().ok()).map(u16::from_le_bytes); + let u32_at = |i: usize| bytes.get(i..i.checked_add(4)?).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes); + let offset = u32_at(10).and_then(|o| usize::try_from(o).ok()).unwrap_or(0); + let (bits, compression) = match u32_at(14) { + Some(12) => (u16_at(24), Some(0)), + _ => (u16_at(28), u32_at(30)), + }; + match compression { + Some(1) => BmpLayout::Rle { offset, rle4: false }, + Some(2) => BmpLayout::Rle { offset, rle4: true }, + _ => BmpLayout::Rows { offset, bits: bits.filter(|&b| b > 0).unwrap_or(1) }, + } +} + +/// The pixels an RLE8 or RLE4 BMP's commands draw on a `w` × `h` canvas, read the way the decoder +/// paints them: a run is clipped at the end of its row, an end-of-row code moves to the next row, a +/// delta skips pixels (left blank), and an end-of-bitmap code, a truncated command or the last row +/// ends the picture. Every command takes at least 2 bytes, so the walk is linear in the file. +fn bmp_rle_pixels(bytes: &[u8], offset: usize, rle4: bool, w: u32, h: u32) -> u64 { + let (w, h) = (u64::from(w), u64::from(h)); + let (mut pos, mut x, mut y, mut covered) = (offset, 0u64, 0u64, 0u64); + while y < h { + let (Some(&count), Some(&value)) = (bytes.get(pos), bytes.get(pos.saturating_add(1))) else { + break; + }; + pos = pos.saturating_add(2); + match (count, value) { + // end of row + (0, 0) => (x, y) = (0, y.saturating_add(1)), + // end of bitmap + (0, 1) => break, + // delta: right and down + (0, 2) => { + let (Some(&dx), Some(&dy)) = (bytes.get(pos), bytes.get(pos.saturating_add(1))) else { + break; + }; + pos = pos.saturating_add(2); + (x, y) = (x.saturating_add(u64::from(dx)), y.saturating_add(u64::from(dy))); + } + // absolute: `value` pixels stored one per byte (RLE8) or nibble (RLE4), padded to 2 bytes + (0, n) => { + let len = if rle4 { usize::from(n).div_ceil(2) } else { usize::from(n) }; + pos = pos.saturating_add(len + (len & 1)); + if pos > bytes.len() { + break; + } + covered = covered.saturating_add(u64::from(n).min(w.saturating_sub(x))); + x = x.saturating_add(u64::from(n)); + } + // a run of `count` pixels + (n, _) => { + covered = covered.saturating_add(u64::from(n).min(w.saturating_sub(x))); + x = x.saturating_add(u64::from(n)); + } + } + } + covered +} + +/// Reads BMP `(width, height)` from the DIB header without allocating. +pub fn dimensions(bytes: &[u8]) -> Option<(u32, u32)> { + if bytes.get(0..2) != Some(b"BM") { + return None; + } + let dib = u32::from_le_bytes(bytes.get(14..18)?.try_into().ok()?); + let (w, h) = if dib == 12 { + (u16::from_le_bytes(bytes.get(18..20)?.try_into().ok()?) as u32, u16::from_le_bytes(bytes.get(20..22)?.try_into().ok()?) as u32) + } else { + let w = i32::from_le_bytes(bytes.get(18..22)?.try_into().ok()?); + let h = i32::from_le_bytes(bytes.get(22..26)?.try_into().ok()?); + (u32::try_from(w).ok()?, h.unsigned_abs()) + }; + Some((w, h)) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn bmp(compression: u32, pixels: &[u8]) -> Vec { + let mut b = vec![0; 54]; + b[..2].copy_from_slice(b"BM"); + b[10..14].copy_from_slice(&54u32.to_le_bytes()); + b[14..18].copy_from_slice(&40u32.to_le_bytes()); + b[18..22].copy_from_slice(&4i32.to_le_bytes()); + b[22..26].copy_from_slice(&2i32.to_le_bytes()); + b[28..30].copy_from_slice(&8u16.to_le_bytes()); + b[30..34].copy_from_slice(&compression.to_le_bytes()); + b.extend_from_slice(pixels); + b + } + + #[test] + fn dimensions_and_padded_rows() { + let b = bmp(0, &[0; 7]); + assert_eq!(dimensions(&b), Some((4, 2))); + assert_eq!(coverage(&b, 4, 2), Coverage { pixels: 8, row_bytes: Some((7, 8)) }); + assert_eq!(dimensions(&b[..20]), None); + } + + #[test] + fn rle_counts_clipped_runs_deltas_and_complete_absolute_blocks() { + let b = bmp(1, &[3, 7, 0, 0, 0, 2, 1, 0, 0, 3, 1, 2, 3, 0, 0, 1]); + assert_eq!(coverage(&b, 4, 2).pixels, 6); + let truncated = bmp(1, &[0, 4, 1, 2]); + assert_eq!(coverage(&truncated, 4, 2).pixels, 0); + let nibble = bmp(2, &[0, 3, 0x12, 0x30, 0, 1]); + assert_eq!(coverage(&nibble, 4, 2).pixels, 3); + } +} diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 5a84e6368..adeed0825 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -16,6 +16,7 @@ mod allocation; pub mod apng; +pub mod bmp_probe; pub mod cicp; mod codecs; mod error; From 68181b7d15555d225ce43e00306192367c69d0e0 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 18:31:35 +0100 Subject: [PATCH 088/150] fix(raw): keep tile-then-plane error order for QP maps; doc and test review points A tile's QP map error is reported by its first plane job, so a later tile's corrupt map no longer wins over an earlier tile's plane error. Equivalence tests also compare error texts; the corpus test fails when the folder holds none of the pinned files; docs/cr3.md says when the sensor buffer is allocated. Part of #1654 Co-Authored-By: Claude Opus 5.5 --- crates/raw/docs/cr3.md | 2 +- crates/raw/src/crx.rs | 13 ++++++++++--- crates/raw/src/crx_wavelet.rs | 2 ++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/raw/docs/cr3.md b/crates/raw/docs/cr3.md index 40e375b54..9c17c809c 100644 --- a/crates/raw/docs/cr3.md +++ b/crates/raw/docs/cr3.md @@ -187,7 +187,7 @@ the six verified fixtures are unchanged. Partial-subband and rounded-plane coding, vertical C-RAW tiling and adaptive low-frequency gains remain unsupported (see the variant table above; `cr3_variants.rs` pins each rejection). The decoder bounds image dimensions, sample count, tile count, marker headers and temporary allocations. The full sensor buffer is -allocated only after the first plane decodes, and plane buffers grow row by row, +allocated only after every plane has decoded, and plane buffers grow row by row, so a small crafted file fails before reserving memory for the image it claims. ## Further bodies: one CC0 file each diff --git a/crates/raw/src/crx.rs b/crates/raw/src/crx.rs index 7374d41f6..42a40b30e 100644 --- a/crates/raw/src/crx.rs +++ b/crates/raw/src/crx.rs @@ -95,20 +95,22 @@ pub(crate) fn decode(sample: &[u8], config: &Cr3Compression) -> Result> let count = image_size(config)?; let tiles = layout(sample, config)?; validate_planes(&tiles, config)?; - let qp = tiles.iter().map(|tile| tile_qp(sample, tile)).collect::>>()?; + // A tile's QP map error is reported by its first plane, so errors keep tile, then plane order. + let qp: Vec>> = tiles.iter().map(|tile| tile_qp(sample, tile)).collect(); let midpoint = 1i32 << (config.bit_depth - 1); let maximum = (1i32 << config.bit_depth) - 1; // Every plane of every tile is its own entropy stream: decode them all in parallel, each straight // to sensor samples, so a task holds only its own plane as i32. The first error in tile and plane // order is reported, as when the tiles were decoded one after another. - let jobs: Vec<(&Tile, Option<&QpMap>, usize, &Plane)> = tiles + let jobs: Vec<(&Tile, &Result>, usize, &Plane)> = tiles .iter() .zip(&qp) - .flat_map(|(tile, qp)| tile.planes.iter().enumerate().map(move |(p, plane)| (tile, qp.as_ref(), p, plane))) + .flat_map(|(tile, qp)| tile.planes.iter().enumerate().map(move |(p, plane)| (tile, qp, p, plane))) .collect(); let planes: Vec>> = jobs .par_iter() .map(|&(tile, qp, p, plane)| { + let qp = qp.as_ref().map_err(Clone::clone)?.as_ref(); let decoded = decode_plane(sample, config, width, tile, plane, qp) .map_err(|error| RawError::Corrupt(format!("Canon CRX tile ({}, {}), plane {p}: {error}", tile.x, tile.y)))?; sensor_plane(&decoded, midpoint, maximum, config.levels != 0) @@ -1319,6 +1321,7 @@ mod tests { #[test] fn optional_cc0_samples_match_external_oracle() { let Some(directory) = std::env::var_os("LIGHTCRAFT_CR3_CORPUS") else { return }; + let mut checked = 0; for (name, expected) in [ ("cr3-canon-r100-raw.cr3", 0xcbe5299ab9c52630u64), ("cr3-canon-m50-raw.cr3", 0x62261f0ba81cfcd2u64), @@ -1360,7 +1363,9 @@ mod tests { .flat_map(|value| value.to_le_bytes()) .fold(0xcbf29ce484222325u64, |hash, byte| (hash ^ u64::from(byte)).wrapping_mul(0x100000001b3)); assert_eq!(hash, expected, "{name}"); + checked += 1; } + assert!(checked > 0, "LIGHTCRAFT_CR3_CORPUS holds none of the pinned files"); } /// The bit reader before the 64-bit cache: a checked 5-byte window per read, bit by bit near the end. @@ -1475,6 +1480,7 @@ mod tests { _ => (cached.qp_rice(n % 22), reference.qp_rice(n % 22)), }; assert_eq!(a.as_ref().ok(), b.as_ref().ok(), "round {round}, end {end}, op {op} {n}, stream {stream:02x?}"); + assert_eq!(a.as_ref().err().map(ToString::to_string), b.as_ref().err().map(ToString::to_string), "round {round}, end {end}, op {op} {n}"); if a.is_err() { complete = false; break; @@ -1536,6 +1542,7 @@ mod tests { }); let case = format!("map {map_width}x{map_height}, band {band}, {width}x{height}, {quant:?}"); assert_eq!(cells_result.is_ok(), reference_result.is_ok(), "{case}"); + assert_eq!(cells_result.as_ref().err().map(ToString::to_string), reference_result.as_ref().err().map(ToString::to_string), "{case}"); if cells_result.is_ok() { assert_eq!(cells, reference, "{case}"); } diff --git a/crates/raw/src/crx_wavelet.rs b/crates/raw/src/crx_wavelet.rs index 9e0197670..066aa8d82 100644 --- a/crates/raw/src/crx_wavelet.rs +++ b/crates/raw/src/crx_wavelet.rs @@ -484,6 +484,7 @@ mod tests { let reference_result = reference_line(&low, &high, &mut reference, before, after); let case = format!("length {length}, before {before}, after {after}, extreme {extreme}"); assert_eq!(fast_result.is_ok(), reference_result.is_ok(), "{case}"); + assert_eq!(fast_result.as_ref().err().map(ToString::to_string), reference_result.as_ref().err().map(ToString::to_string), "{case}"); if fast_result.is_ok() { assert_eq!(fast, reference, "{case}"); } @@ -510,6 +511,7 @@ mod tests { let reference = reference_level(&bands, width, height, edges); let case = format!("{width}x{height}, {edges:?}, extreme {extreme}"); assert_eq!(fast.as_ref().ok().map(|band| &band.data), reference.as_ref().ok(), "{case}"); + assert_eq!(fast.as_ref().err().map(ToString::to_string), reference.as_ref().err().map(ToString::to_string), "{case}"); } } } From ee87ab6c9e09ef290b9ba527a2fd40f6f6b02b4f Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 18:33:35 +0100 Subject: [PATCH 089/150] style(raw): rustfmt and clippy for the CR3 decoder changes Part of #1654 Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/crx.rs | 51 ++++++++++++++++++++++------------- crates/raw/src/crx_wavelet.rs | 14 +++++++--- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/crates/raw/src/crx.rs b/crates/raw/src/crx.rs index 42a40b30e..935975224 100644 --- a/crates/raw/src/crx.rs +++ b/crates/raw/src/crx.rs @@ -102,11 +102,8 @@ pub(crate) fn decode(sample: &[u8], config: &Cr3Compression) -> Result> // Every plane of every tile is its own entropy stream: decode them all in parallel, each straight // to sensor samples, so a task holds only its own plane as i32. The first error in tile and plane // order is reported, as when the tiles were decoded one after another. - let jobs: Vec<(&Tile, &Result>, usize, &Plane)> = tiles - .iter() - .zip(&qp) - .flat_map(|(tile, qp)| tile.planes.iter().enumerate().map(move |(p, plane)| (tile, qp, p, plane))) - .collect(); + let jobs: Vec<(&Tile, &Result>, usize, &Plane)> = + tiles.iter().zip(&qp).flat_map(|(tile, qp)| tile.planes.iter().enumerate().map(move |(p, plane)| (tile, qp, p, plane))).collect(); let planes: Vec>> = jobs .par_iter() .map(|&(tile, qp, p, plane)| { @@ -167,19 +164,16 @@ fn interleave(output: &mut [u16], width: usize, tiles: &[Tile], planes: &[Vec Result<()> { let y = 2 * pair; - for (tile, planes) in tiles.iter().zip(planes.chunks_exact(4)).filter(|(tile, _)| (tile.y..tile.y + tile.height).contains(&y)) { + for (tile, planes) in tiles.iter().zip(planes.as_chunks::<4>().0).filter(|(tile, _)| (tile.y..tile.y + tile.height).contains(&y)) { let half = tile.width / 2; let start = (y - tile.y) / 2 * half; - for (row, sources) in rows.chunks_exact_mut(width).zip(planes.chunks_exact(2)) { - let [even, odd] = sources else { return Err(outside()) }; + for (row, [even, odd]) in rows.chunks_exact_mut(width).zip(planes.as_chunks::<2>().0) { let target = row.get_mut(tile.x..tile.x + tile.width).ok_or_else(outside)?; let even = even.get(start..start + half).ok_or_else(outside)?; let odd = odd.get(start..start + half).ok_or_else(outside)?; - for ((pixels, &left), &right) in target.chunks_exact_mut(2).zip(even).zip(odd) { - if let [a, b] = pixels { - *a = left; - *b = right; - } + for ([a, b], (&left, &right)) in target.as_chunks_mut::<2>().0.iter_mut().zip(even.iter().zip(odd)) { + *a = left; + *b = right; } } } @@ -1480,7 +1474,11 @@ mod tests { _ => (cached.qp_rice(n % 22), reference.qp_rice(n % 22)), }; assert_eq!(a.as_ref().ok(), b.as_ref().ok(), "round {round}, end {end}, op {op} {n}, stream {stream:02x?}"); - assert_eq!(a.as_ref().err().map(ToString::to_string), b.as_ref().err().map(ToString::to_string), "round {round}, end {end}, op {op} {n}"); + assert_eq!( + a.as_ref().err().map(ToString::to_string), + b.as_ref().err().map(ToString::to_string), + "round {round}, end {end}, op {op} {n}" + ); if a.is_err() { complete = false; break; @@ -1518,9 +1516,20 @@ mod tests { let mut seed = 0x9e37_79b9_7f4a_7c15; // Odd map heights as on the EOS R7, and band sizes that fit the map and that overrun it by one. for (map_width, map_height) in [(1, 1), (2, 3), (3, 4), (5, 7)] { - let map = QpMap { width: map_width, height: map_height, data: (0..map_width * map_height).map(|_| 131 + (xorshift(&mut seed) % 45) as i32).collect() }; + let map = QpMap { + width: map_width, + height: map_height, + data: (0..map_width * map_height).map(|_| 131 + (xorshift(&mut seed) % 45) as i32).collect(), + }; for band in 0..10 { - for (width, height) in [(1, 1), (3, 2), (2 * map_width - 1, map_height.div_ceil(2)), (2 * map_width + 1, map_height), (4 * map_width, map_height), (4 * map_width + 1, map_height + 1)] { + for (width, height) in [ + (1, 1), + (3, 2), + (2 * map_width - 1, map_height.div_ceil(2)), + (2 * map_width + 1, map_height), + (4 * map_width, map_height), + (4 * map_width + 1, map_height + 1), + ] { for quant in [ Quantization::Uniform(4 + 4 * band as u8), Quantization::Adaptive { base: 0, gain: 0 }, @@ -1542,7 +1551,11 @@ mod tests { }); let case = format!("map {map_width}x{map_height}, band {band}, {width}x{height}, {quant:?}"); assert_eq!(cells_result.is_ok(), reference_result.is_ok(), "{case}"); - assert_eq!(cells_result.as_ref().err().map(ToString::to_string), reference_result.as_ref().err().map(ToString::to_string), "{case}"); + assert_eq!( + cells_result.as_ref().err().map(ToString::to_string), + reference_result.as_ref().err().map(ToString::to_string), + "{case}" + ); if cells_result.is_ok() { assert_eq!(cells, reference, "{case}"); } @@ -1563,7 +1576,9 @@ mod tests { let planes: Vec> = tiles .iter() .enumerate() - .flat_map(|(t, tile)| (0..4).map(move |p| (0..tile.width * tile.height / 4).map(|i| ((t * 4 + p) * 100 + i) as u16).collect::>())) + .flat_map(|(t, tile)| { + (0..4).map(move |p| (0..tile.width * tile.height / 4).map(|i| ((t * 4 + p) * 100 + i) as u16).collect::>()) + }) .collect(); let mut expected = vec![0u16; width * height]; for (t, tile) in tiles.iter().enumerate() { diff --git a/crates/raw/src/crx_wavelet.rs b/crates/raw/src/crx_wavelet.rs index 066aa8d82..190840be7 100644 --- a/crates/raw/src/crx_wavelet.rs +++ b/crates/raw/src/crx_wavelet.rs @@ -463,7 +463,11 @@ mod tests { (0..count) .map(|_| { let r = xorshift(seed); - if extreme && r % 5 == 0 { if r & 32 == 0 { i32::MAX - (r >> 40) as i32 % 4 } else { i32::MIN + (r >> 40) as i32 % 4 } } else { (r % 2001) as i32 - 1000 } + if extreme && r.is_multiple_of(5) { + if r & 32 == 0 { i32::MAX - (r >> 40) as i32 % 4 } else { i32::MIN + (r >> 40) as i32 % 4 } + } else { + (r % 2001) as i32 - 1000 + } }) .collect() } @@ -484,7 +488,11 @@ mod tests { let reference_result = reference_line(&low, &high, &mut reference, before, after); let case = format!("length {length}, before {before}, after {after}, extreme {extreme}"); assert_eq!(fast_result.is_ok(), reference_result.is_ok(), "{case}"); - assert_eq!(fast_result.as_ref().err().map(ToString::to_string), reference_result.as_ref().err().map(ToString::to_string), "{case}"); + assert_eq!( + fast_result.as_ref().err().map(ToString::to_string), + reference_result.as_ref().err().map(ToString::to_string), + "{case}" + ); if fast_result.is_ok() { assert_eq!(fast, reference, "{case}"); } @@ -504,7 +512,7 @@ mod tests { let (lw, hw) = sizes(width, left, right); let (lhgt, hhgt) = sizes(height, top, bottom); // Overflow cases on the smaller sizes only; they fail early and are cheap. - let extreme = width * height < 40 && xorshift(&mut seed) % 2 == 0; + let extreme = width * height < 40 && xorshift(&mut seed).is_multiple_of(2); let mut band = |w, h| Band { width: w, height: h, data: coefficients(&mut seed, w * h, extreme) }; let bands = [band(lw, lhgt), band(hw, lhgt), band(lw, hhgt), band(hw, hhgt)]; let fast = inverse_level(&bands[0], &bands[1], &bands[2], &bands[3], width, height, edges); From 4904d6367a176ac790916bf9a5a8b292e6fffb18 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 18:35:06 +0100 Subject: [PATCH 090/150] test(raw): a corrupt CR3 QP map is reported as a QP error, not a plane error Part of #1654 Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/crx.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/raw/src/crx.rs b/crates/raw/src/crx.rs index 935975224..4d8f53ba8 100644 --- a/crates/raw/src/crx.rs +++ b/crates/raw/src/crx.rs @@ -1310,6 +1310,11 @@ mod tests { for end in 0..sample.len() { assert!(decode(&sample[..end], &config).is_err()); } + // A corrupt QP map (nonzero alignment bits) is reported as such, not as a plane error. + let at = sample.windows(5).position(|bytes| bytes == [0, 0, 1, 0x2c, 0x94]).unwrap() + 4; + sample[at] ^= 1; + let error = decode(&sample, &config).unwrap_err().to_string(); + assert!(error.contains("alignment") && !error.contains("plane"), "{error}"); } #[test] From a31327d5f1d01e0eaf7249cd8ffd7c8fc68a6edf Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:36:53 +0000 Subject: [PATCH 091/150] webp: validate decoder canvas before frame allocation --- crates/webp/src/frames.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/crates/webp/src/frames.rs b/crates/webp/src/frames.rs index e97118482..4b5322d34 100644 --- a/crates/webp/src/frames.rs +++ b/crates/webp/src/frames.rs @@ -102,6 +102,15 @@ pub fn read_frames(bytes: &[u8], limits: &FrameLimits) -> Result limits.max_canvas_bytes { + return Err(FrameError::LimitExceeded(format!("a {width}x{height} canvas is over {} bytes", limits.max_canvas_bytes))); + } + if (width, height) != (info.width, info.height) { + return Err(FrameError::Malformed("conflicting canvas dimensions".into())); + } if !decoder.has_animation() { let mut rgba = vec![0u8; usize::try_from(canvas).map_err(|_| FrameError::LimitExceeded("canvas".into()))?]; let alpha = decoder.color_type().has_alpha(); @@ -244,6 +253,21 @@ mod tests { assert!(read_frames(&bytes, &FrameLimits { max_canvas_bytes: 24, ..FrameLimits::default() }).is_ok()); } + #[test] + fn conflicting_canvas_is_rejected_before_frame_decode() { + let mut bytes = anim(&[(1, 10), (2, 10)]); + // The decoder uses the first VP8X, while the scanner sees the last one. + // A tiny limit makes this an ordering test without allocating a huge frame. + let second = chunk(b"VP8X", &[0x12, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + bytes.splice(30..30, second); + let size = (bytes.len() - 8) as u32; + bytes[4..8].copy_from_slice(&size.to_le_bytes()); + assert!(matches!(read_frames(&bytes, &FrameLimits { max_canvas_bytes: 4, ..FrameLimits::default() }), Err(FrameError::LimitExceeded(_)))); + assert!( + matches!(read_frames(&bytes, &FrameLimits::default()), Err(FrameError::Malformed(message)) if message.contains("conflicting canvas")) + ); + } + #[test] fn damage_and_hostile_input() { let bytes = anim(&[(1, 10), (2, 10), (3, 10)]); From a6493b48f0bc2274234f5ab92ec98f91fffcb5ac Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 12:38:25 -0500 Subject: [PATCH 092/150] Fix JPEG-LS low-precision expansion and scan allocation cap --- crates/image-codecs/src/codecs/jpegls.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/image-codecs/src/codecs/jpegls.rs b/crates/image-codecs/src/codecs/jpegls.rs index b405e71ed..a5dff1015 100644 --- a/crates/image-codecs/src/codecs/jpegls.rs +++ b/crates/image-codecs/src/codecs/jpegls.rs @@ -654,6 +654,8 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { let layout = if nf == 1 { ChannelLayout::Gray } else { ChannelLayout::Rgb }; let sample = if precision <= 8 { SampleType::U8 } else { SampleType::U16 }; limits.check(width as u32, height as u32, layout, sample)?; + // Each scan retains u16 samples even when the final image is 8-bit. + limits.check_bytes(width as u32, height as u32, 2)?; let components = (0..nf).map(|i| seg[6 + 3 * i]).collect(); frame = Some(Frame { precision, width, height, components }); } @@ -731,7 +733,7 @@ fn finish(frame: Option, planes: Vec>, scans: usize) -> Result u8 { if p == 8 { v as u8 } else { (scale(v) >> 8) as u8 } }; + let to8 = |v: u16| -> u8 { if p == 8 { v as u8 } else { (u32::from(v) * 255 / ((1u32 << p) - 1)) as u8 } }; let mut data = Vec::with_capacity(w * h * nc); for i in 0..w * h { for plane in &planes { @@ -784,6 +786,25 @@ mod tests { Plan { layout: img.layout(), sample: img.sample_type() } } + #[test] + fn low_precision_samples_span_the_full_output_range() { + for precision in 2..=7 { + let max = (1u16 << precision) - 1; + let f = Frame { precision, width: usize::from(max) + 1, height: 1, components: vec![1] }; + let image = finish(Some(f), vec![(0..=max).collect()], 1).unwrap(); + let expected: Vec = (0..=max).map(|v| (u32::from(v) * 255 / u32::from(max)) as u8).collect(); + assert_eq!(image.data(), expected); + } + } + + #[test] + fn eight_bit_scan_intermediate_respects_allocation_limit() { + let img = image(8, 8, ChannelLayout::Gray, false, 0); + let bytes = encode(&img, plan(&img)).unwrap(); + assert!(matches!(decode(&bytes, &Limits::new(8, 8, 64, 127)), Err(CodecError::LimitExceeded(_)))); + assert!(decode(&bytes, &Limits::new(8, 8, 64, 128)).is_ok()); + } + #[test] fn round_trips_noise_flat_and_smooth_images() { for layout in [ChannelLayout::Gray, ChannelLayout::Rgb] { From e56b4e3cd4cf60b70823d91756f8270426668833 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 12:55:47 -0500 Subject: [PATCH 093/150] Deduplicate EXIF orientation and ColorSpace through craft-metadata (#1465) Extract the remaining metadata delegation from #1198 after #1331 landed the raster facade changes. Preserve current image-codecs API, pixel transforms, docs and regression tests; omit unrelated resolution reexports and TIFF cleanup. Original-change: 1d52e89405ff9237c53f34584ff97fef8decc611 --- crates/image-codecs/src/exif_color.rs | 62 +------- crates/image-codecs/src/orientation.rs | 211 +------------------------ 2 files changed, 12 insertions(+), 261 deletions(-) diff --git a/crates/image-codecs/src/exif_color.rs b/crates/image-codecs/src/exif_color.rs index 4259bb15c..80928a285 100644 --- a/crates/image-codecs/src/exif_color.rs +++ b/crates/image-codecs/src/exif_color.rs @@ -5,66 +5,18 @@ //! ColorSpace = sRGB as sRGB, unless Preferences › File Handling › Ignore EXIF Profile Tag is on. //! Anything malformed, truncated or missing reads as no tag. -use crate::orientation::{Order, ifd0_entries, tiff_body}; - -/// IFD0's pointer to the Exif sub-IFD. -const TAG_EXIF_IFD: u16 = 0x8769; -/// The ColorSpace tag in the Exif sub-IFD. -const TAG_COLOR_SPACE: u16 = 0xA001; -const TYPE_SHORT: u16 = 3; -const TYPE_LONG: u16 = 4; -/// TIFF type 13: an IFD offset (some writers use it for sub-IFD pointers). -const TYPE_IFD: u16 = 13; -/// Bytes per classic TIFF IFD entry; EXIF is never BigTIFF. -const ENTRY: usize = 12; -/// No IFD can start inside the 8-byte TIFF header. -const HEADER: usize = 8; - -/// The ColorSpace value meaning sRGB. -pub const EXIF_COLOR_SPACE_SRGB: u16 = 1; - -/// The EXIF ColorSpace value of a TIFF-structured block (an EXIF payload with or without the -/// `Exif\0\0` prefix), or `None` when it has none or it is malformed. -pub fn exif_color_space(exif: &[u8]) -> Option { - let b = tiff_body(exif); - let (order, first, count, entry_bytes, big_tiff) = ifd0_entries(b)?; - if big_tiff { - return None; - } - let ptr = find(order, b, first, count, entry_bytes, TAG_EXIF_IFD)?; - if !matches!(order.u16(b, ptr.checked_add(2)?)?, TYPE_LONG | TYPE_IFD) || order.u32(b, ptr.checked_add(4)?)? != 1 { - return None; - } - let ifd = usize::try_from(order.u32(b, ptr.checked_add(8)?)?).ok()?; - if ifd < HEADER { - return None; - } - let n = usize::from(order.u16(b, ifd)?); - let first = ifd.checked_add(2)?; - // The whole table must be present. - if first.checked_add(n.checked_mul(ENTRY)?)? > b.len() { - return None; - } - let e = find(order, b, first, n, ENTRY, TAG_COLOR_SPACE)?; - if order.u32(b, e.checked_add(4)?)? != 1 { - return None; - } - match order.u16(b, e.checked_add(2)?)? { - TYPE_SHORT => order.u16(b, e.checked_add(8)?), - TYPE_LONG => u16::try_from(order.u32(b, e.checked_add(8)?)?).ok(), - _ => None, - } -} - -/// Offset of the first entry tagged `tag` in the IFD whose `count` entries start at `first`. -fn find(order: Order, b: &[u8], first: usize, count: usize, entry_bytes: usize, tag: u16) -> Option { - (0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).find(|&e| order.u16(b, e) == Some(tag)) -} +// Share parsing and malformed-input policy with every craft-metadata caller. +pub use craft_metadata::edit::{EXIF_COLOR_SPACE_SRGB, exif_color_space}; #[cfg(test)] mod tests { use super::*; + const TAG_EXIF_IFD: u16 = 0x8769; + const TAG_COLOR_SPACE: u16 = 0xA001; + const TYPE_SHORT: u16 = 3; + const TYPE_LONG: u16 = 4; + /// A little- or big-endian EXIF block: IFD0 holding only the Exif pointer, then an Exif IFD /// holding `entries` (tag, type, count, value). fn exif(big_endian: bool, entries: &[(u16, u16, u32, u32)]) -> Vec { diff --git a/crates/image-codecs/src/orientation.rs b/crates/image-codecs/src/orientation.rs index f77e6691f..fd5ec9c97 100644 --- a/crates/image-codecs/src/orientation.rs +++ b/crates/image-codecs/src/orientation.rs @@ -20,213 +20,12 @@ use std::borrow::Cow; use crate::error::CodecError; use crate::image::Image; -/// The TIFF/EXIF Orientation tag. -const TAG_ORIENTATION: u16 = 274; -/// Field types accepted for the Orientation tag: SHORT (the spec) and LONG -/// (written by some cameras and tools). -const TYPE_SHORT: u16 = 3; -const TYPE_LONG: u16 = 4; -/// Bytes per IFD entry. -const ENTRY: usize = 12; -/// The TIFF header is 8 bytes, so no IFD can start before it. -const HEADER: usize = 8; -/// BigTIFF uses a 16-byte header, 20-byte entries, and 8-byte offsets. -const BIG_ENTRY: usize = 20; -const BIG_HEADER: usize = 16; +// Keep byte-level EXIF/XMP handling in one implementation so fixes apply to all callers. +pub use craft_metadata::edit::{exif_orientation, upright_exif, upright_xmp}; -/// Byte order of a TIFF structure. -#[derive(Clone, Copy)] -pub(crate) enum Order { - Little, - Big, -} - -impl Order { - pub(crate) fn u16(self, b: &[u8], at: usize) -> Option { - let s: [u8; 2] = b.get(at..at.checked_add(2)?)?.try_into().ok()?; - Some(match self { - Order::Little => u16::from_le_bytes(s), - Order::Big => u16::from_be_bytes(s), - }) - } - - pub(crate) fn u32(self, b: &[u8], at: usize) -> Option { - let s: [u8; 4] = b.get(at..at.checked_add(4)?)?.try_into().ok()?; - Some(match self { - Order::Little => u32::from_le_bytes(s), - Order::Big => u32::from_be_bytes(s), - }) - } - - pub(crate) fn u64(self, b: &[u8], at: usize) -> Option { - let s: [u8; 8] = b.get(at..at.checked_add(8)?)?.try_into().ok()?; - Some(match self { - Order::Little => u64::from_le_bytes(s), - Order::Big => u64::from_be_bytes(s), - }) - } -} - -/// The TIFF payload of an EXIF block (a JPEG-style `Exif\0\0` prefix is skipped). -pub(crate) fn tiff_body(b: &[u8]) -> &[u8] { - b.strip_prefix(b"Exif\0\0").unwrap_or(b) -} - -/// IFD0 of a TIFF structure: byte order, entry layout, and entry offsets. -/// -/// The full table and next-IFD pointer must be present, and IFD0 must start -/// after the corresponding TIFF or BigTIFF header. -pub(crate) fn ifd0_entries(b: &[u8]) -> Option<(Order, usize, usize, usize, bool)> { - let (order, big_tiff) = match b.get(0..4)? { - [b'I', b'I', 42, 0] => (Order::Little, false), - [b'M', b'M', 0, 42] => (Order::Big, false), - [b'I', b'I', 43, 0] => (Order::Little, true), - [b'M', b'M', 0, 43] => (Order::Big, true), - _ => return None, - }; - let (ifd, count_bytes, entry_bytes, next_ifd_bytes, header) = if big_tiff { - if order.u16(b, 4)? != 8 || order.u16(b, 6)? != 0 { - return None; - } - (usize::try_from(order.u64(b, 8)?).ok()?, 8usize, BIG_ENTRY, 8usize, BIG_HEADER) - } else { - (usize::try_from(order.u32(b, 4)?).ok()?, 2usize, ENTRY, 4usize, HEADER) - }; - if ifd < header { - return None; - } - let count = if big_tiff { usize::try_from(order.u64(b, ifd)?).ok()? } else { usize::from(order.u16(b, ifd)?) }; - let first = ifd.checked_add(count_bytes)?; - let next_ifd = first.checked_add(count.checked_mul(entry_bytes)?)?; - if next_ifd.checked_add(next_ifd_bytes)? > b.len() { - return None; - } - // In bounds: every entry ends at or before `next_ifd`. - Some((order, first, count, entry_bytes, big_tiff)) -} - -/// A well-formed Orientation entry's value: where it sits and its field type. -#[derive(Clone, Copy)] -struct Value { - at: usize, - ty: u16, -} - -impl Value { - /// The stored value (a SHORT or LONG, left-justified in the 4-byte field). - fn read(self, order: Order, b: &[u8]) -> Option { - match self.ty { - TYPE_SHORT => order.u16(b, self.at).map(u32::from), - _ => order.u32(b, self.at), - } - } - - /// 1 in this entry's own type, width and byte order. - fn one(self, order: Order) -> Vec { - match (self.ty, order) { - (TYPE_SHORT, Order::Little) => 1u16.to_le_bytes().to_vec(), - (TYPE_SHORT, Order::Big) => 1u16.to_be_bytes().to_vec(), - (_, Order::Little) => 1u32.to_le_bytes().to_vec(), - (_, Order::Big) => 1u32.to_be_bytes().to_vec(), - } - } -} - -/// The value of the entry at `e` if it is a well-formed Orientation entry: -/// exactly one SHORT or LONG. -fn orientation_value(order: Order, b: &[u8], e: usize, big_tiff: bool) -> Option { - let count = if big_tiff { order.u64(b, e.checked_add(4)?)? } else { u64::from(order.u32(b, e.checked_add(4)?)?) }; - if order.u16(b, e)? != TAG_ORIENTATION || count != 1 { - return None; - } - let ty = order.u16(b, e.checked_add(2)?)?; - let value_offset = if big_tiff { 12 } else { 8 }; - matches!(ty, TYPE_SHORT | TYPE_LONG).then_some(Value { at: e.checked_add(value_offset)?, ty }) -} - -/// Locates the Orientation value in IFD0. -/// -/// The **first** Orientation entry wins (like libexif and Photoshop); if that -/// one is malformed (count ≠ 1, or neither SHORT nor LONG) the tag is ignored, -/// even when a later duplicate is well formed. -fn find_entry(b: &[u8]) -> Option<(Order, Value)> { - let (order, first, count, entry_bytes, big_tiff) = ifd0_entries(b)?; - let e = (0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).find(|&e| order.u16(b, e) == Some(TAG_ORIENTATION))?; - Some((order, orientation_value(order, b, e, big_tiff)?)) -} - -/// The orientation (1–8) recorded in a TIFF-structured block: an EXIF payload -/// (with or without the `Exif\0\0` prefix) or a whole TIFF file. Missing, -/// malformed or out-of-range values give 1. -pub fn exif_orientation(exif: &[u8]) -> u16 { - let b = tiff_body(exif); - find_entry(b).and_then(|(order, v)| v.read(order, b)).and_then(|v| u16::try_from(v).ok()).filter(|v| (1..=8).contains(v)).unwrap_or(1) -} - -/// `exif` with its Orientation rewritten to 1, borrowed when there is nothing -/// to change (no well-formed tag, already 1, or unparseable). -/// -/// Only the value bytes of each well-formed Orientation entry change, written -/// as 1 in the entry's existing type and width (SHORT or LONG); every other -/// byte (other tags, their offset-based values, sub-IFDs) is kept as is. -/// Well-formed duplicates are all set to 1, so a reader that lets a later -/// duplicate win cannot rotate the upright pixels either. -pub fn upright_exif(exif: &[u8]) -> Cow<'_, [u8]> { - let body = tiff_body(exif); - let prefix = exif.len() - body.len(); - let Some((order, first, count, entry_bytes, big_tiff)) = ifd0_entries(body) else { - return Cow::Borrowed(exif); - }; - let values: Vec = (0..count) - .filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)) - .filter_map(|e| orientation_value(order, body, e, big_tiff)) - .collect(); - if values.iter().all(|v| v.read(order, body) == Some(1)) { - return Cow::Borrowed(exif); - } - let mut out = exif.to_vec(); - for v in values { - let one = v.one(order); - let slot = prefix.checked_add(v.at).and_then(|at| out.get_mut(at..at.checked_add(one.len())?)); - if let Some(slot) = slot { - slot.copy_from_slice(&one); - } - } - Cow::Owned(out) -} - -/// `xmp` with any `tiff:Orientation` (attribute or element form) set to 1, -/// borrowed when there is nothing to change. -pub fn upright_xmp(xmp: &str) -> Cow<'_, str> { - const KEY: &str = "tiff:Orientation"; - if !xmp.contains(KEY) { - return Cow::Borrowed(xmp); - } - let mut out = String::with_capacity(xmp.len()); - let mut rest = xmp; - let mut changed = false; - while let Some(i) = rest.find(KEY) { - let (head, tail) = rest.split_at(i + KEY.len()); - out.push_str(head); - rest = tail; - // `tiff:Orientation="6"`, `tiff:Orientation='6'` or `6<`. - let open = rest.char_indices().take_while(|&(_, c)| c.is_ascii_whitespace() || matches!(c, '=' | '"' | '\'' | '>')).last(); - let Some((at, c)) = open else { continue }; - if !matches!(c, '"' | '\'' | '>') { - continue; - } - let start = at + 1; - let digits = rest.get(start..).map_or(0, |s| s.bytes().take_while(u8::is_ascii_digit).count()); - if digits == 0 || rest.get(start..start + digits) == Some("1") { - continue; - } - out.push_str(rest.get(..start).unwrap_or_default()); - out.push('1'); - rest = rest.get(start + digits..).unwrap_or_default(); - changed = true; - } - out.push_str(rest); - if changed { Cow::Owned(out) } else { Cow::Borrowed(xmp) } +/// TIFF payload of an EXIF block (a JPEG-style `Exif\0\0` prefix is skipped). +pub(crate) fn tiff_body(bytes: &[u8]) -> &[u8] { + bytes.strip_prefix(b"Exif\0\0").unwrap_or(bytes) } /// Source pixel index for output pixel `(0, y2)` and the step per output column, for From c7642687050cf1ea742117c6ffe7a6388713a75c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:56:52 +0000 Subject: [PATCH 094/150] Allow ZIP archives to own their input --- crates/zip/src/archive.rs | 54 ++++++++++++++++++++++++++++++++++--- crates/zip/tests/archive.rs | 23 ++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/crates/zip/src/archive.rs b/crates/zip/src/archive.rs index 5739e830a..49da63585 100644 --- a/crates/zip/src/archive.rs +++ b/crates/zip/src/archive.rs @@ -2,6 +2,7 @@ use std::borrow::Cow; use std::io::{Cursor, Read}; +use std::sync::Arc; use crate::preflight::{ReadError, read_entry}; use crate::{Budget, Error, check_entry_count}; @@ -21,11 +22,26 @@ pub struct Limits { /// caller that reads through a clone keeps its own count. #[derive(Clone)] pub struct Archive<'a> { - zip: zip::ZipArchive>, + zip: zip::ZipArchive>>, budget: Budget, duplicates: bool, } +#[derive(Clone)] +enum Input<'a> { + Borrowed(&'a [u8]), + Owned(Arc>), +} + +impl AsRef<[u8]> for Input<'_> { + fn as_ref(&self) -> &[u8] { + match self { + Self::Borrowed(bytes) => bytes, + Self::Owned(bytes) => bytes.as_slice(), + } + } +} + const CENTRAL_HEADER: &[u8; 4] = b"PK\x01\x02"; const CENTRAL_HEADER_LEN: usize = 46; @@ -46,15 +62,20 @@ impl<'a> Archive<'a> { /// Indexes `bytes`. The declared entry count is checked against /// [`Limits::max_entries`] before `zip` parses the central directory, and again after. pub fn open(bytes: &'a [u8], limits: Limits) -> Result { - check_entry_count(bytes, limits.max_entries)?; - let zip = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| Error::NotZip(e.to_string()))?; + Self::open_input(Input::Borrowed(bytes), limits) + } + + fn open_input(input: Input<'a>, limits: Limits) -> Result { + check_entry_count(input.as_ref(), limits.max_entries)?; + let bytes = input.clone(); + let zip = zip::ZipArchive::new(Cursor::new(input)).map_err(|e| Error::NotZip(e.to_string()))?; let found = zip.len() as u64; if found > limits.max_entries { return Err(Error::TooManyEntries { declared: found, limit: limits.max_entries }); } // `zip` keeps one entry per name, so a directory with more headers than `zip` has entries // repeats a name. - let duplicates = count_central_headers(bytes, zip.central_directory_start(), zip.len().saturating_add(1)) > zip.len(); + let duplicates = count_central_headers(bytes.as_ref(), zip.central_directory_start(), zip.len().saturating_add(1)) > zip.len(); Ok(Archive { zip, budget: Budget::new("archive", limits.max_total_bytes), duplicates }) } @@ -180,3 +201,28 @@ impl<'a> Archive<'a> { &mut self.budget } } + +impl Archive<'static> { + /// Indexes owned bytes without copying them. Clones share the backing storage. + pub fn open_owned(bytes: Vec, limits: Limits) -> Result { + Self::open_input(Input::Owned(Arc::new(bytes)), limits) + } +} + +#[cfg(test)] +mod ownership_tests { + use super::*; + + #[test] + fn owned_storage_lives_until_last_archive_clone_is_dropped() { + let bytes = crate::Writer::new().finish().unwrap(); + let bytes = Arc::new(bytes); + let weak = Arc::downgrade(&bytes); + let archive = Archive::open_input(Input::Owned(bytes), Limits { max_entries: 1, max_total_bytes: 1 }).unwrap(); + let clone = archive.clone(); + drop(archive); + assert!(weak.upgrade().is_some()); + drop(clone); + assert!(weak.upgrade().is_none()); + } +} diff --git a/crates/zip/tests/archive.rs b/crates/zip/tests/archive.rs index 2c4b84113..d3b135a9c 100644 --- a/crates/zip/tests/archive.rs +++ b/crates/zip/tests/archive.rs @@ -182,3 +182,26 @@ fn a_clone_keeps_its_own_count() { assert_eq!((c.budget().used(), z.budget().used()), (3, 0)); assert!(z.read("a", 10).unwrap().is_some()); } + +#[test] +fn owned_archive_reads_and_clones_with_independent_budgets() { + let bytes = archive(&[("a", b"abc", Compression::Deflated)]); + let mut z = Archive::open_owned(bytes, Limits { max_total_bytes: 3, ..ROOMY }).unwrap(); + let mut clone = z.clone(); + assert_eq!(z.read("a", 3).unwrap().as_deref(), Some(&b"abc"[..])); + assert!(matches!(z.read("a", 3), Err(Error::BudgetSpent { .. }))); + assert_eq!(clone.read("a", 3).unwrap().as_deref(), Some(&b"abc"[..])); + assert_eq!(clone.budget().used(), 3); +} + +#[test] +fn owned_archive_applies_directory_guards() { + let mut bytes = archive(&[("a", b"1", Compression::Stored), ("b", b"2", Compression::Stored)]); + assert!(matches!(Archive::open_owned(bytes.clone(), Limits { max_entries: 1, ..ROOMY }), Err(Error::TooManyEntries { .. }))); + let local = find(&bytes, b"PK\x03\x04", 4); + bytes[local + 30] = b'a'; + let central = find(&bytes, b"PK\x01\x02", find(&bytes, b"PK\x01\x02", 0) + 4); + bytes[central + 46] = b'a'; + assert!(Archive::open_owned(bytes, ROOMY).unwrap().has_duplicate_names()); + assert!(Archive::open_owned(b"invalid".to_vec(), ROOMY).is_err()); +} From 16d838105b07965832bb43132880176f299634d5 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 12:59:08 -0500 Subject: [PATCH 095/150] Write flat PSD from borrowed samples (#1620) * Write flat PSD from borrowed interleaved samples * Cover multiple PSD rows and planes * Document flat PSD inputs and satisfy lint --- crates/psd/src/flat.rs | 151 +++++++++++++++++++++++++++++++++++++++++ crates/psd/src/lib.rs | 1 + 2 files changed, 152 insertions(+) create mode 100644 crates/psd/src/flat.rs diff --git a/crates/psd/src/flat.rs b/crates/psd/src/flat.rs new file mode 100644 index 000000000..58b1dff94 --- /dev/null +++ b/crates/psd/src/flat.rs @@ -0,0 +1,151 @@ +//! Flat PSD export from borrowed interleaved samples, without an image-sized staging buffer. +use crate::{PsdError, Result}; + +/// Display-encoded samples, interleaved by pixel. +#[derive(Clone, Copy)] +pub enum Samples<'a> { + /// Eight-bit samples. + U8(&'a [u8]), + /// Sixteen-bit samples. + U16(&'a [u16]), +} + +/// Optional resources embedded in a flat PSD. +#[derive(Clone, Copy, Default)] +pub struct Metadata<'a> { + /// ICC profile bytes. + pub icc: Option<&'a [u8]>, + /// TIFF-structured EXIF bytes. + pub exif: Option<&'a [u8]>, + /// XMP document text. + pub xmp: Option<&'a str>, + /// Resolution in pixels per inch. + pub ppi: Option, +} + +/// Serialize a flat PSD. Input channels are gray, gray-alpha, RGB or RGBA; alpha is discarded. +/// Eight-bit rows use PackBits; sixteen-bit samples are stored raw and big-endian. The output +/// buffer and one eight-bit row are held at once; there is no image-sized pixel copy. +pub fn write(width: u32, height: u32, channels: u8, samples: Samples<'_>, meta: Metadata<'_>) -> Result> { + if width == 0 || height == 0 || width > 30_000 || height > 30_000 || !(1..=4).contains(&channels) { + return Err(PsdError::invalid("invalid flat PSD dimensions or channels")); + } + let n = (width as usize).checked_mul(height as usize).ok_or(PsdError::LimitExceeded("image dimensions"))?; + let ch = usize::from(channels); + let needed = n.checked_mul(ch).ok_or(PsdError::LimitExceeded("sample count"))?; + let (depth, have) = match samples { + Samples::U8(s) => (8u16, s.len()), + Samples::U16(s) => (16u16, s.len()), + }; + if have < needed { + return Err(PsdError::invalid("sample buffer too short")); + } + let color = if ch <= 2 { 1usize } else { 3 }; + let mut out = Vec::new(); + out.extend_from_slice(b"8BPS"); + out.extend_from_slice(&1u16.to_be_bytes()); + out.extend_from_slice(&[0; 6]); + out.extend_from_slice(&(color as u16).to_be_bytes()); + out.extend_from_slice(&height.to_be_bytes()); + out.extend_from_slice(&width.to_be_bytes()); + out.extend_from_slice(&depth.to_be_bytes()); + out.extend_from_slice(&(if color == 1 { 1u16 } else { 3 }).to_be_bytes()); + out.extend_from_slice(&0u32.to_be_bytes()); + let mut resources = Vec::new(); + if let Some(ppi) = meta.ppi.filter(|p| *p > 0) { + let fixed = (u32::from(ppi) << 16).to_be_bytes(); + let one = 1u16.to_be_bytes(); + resource(&mut resources, 1005, &[&fixed[..], &one, &one, &fixed, &one, &one].concat())?; + } + if let Some(data) = meta.icc { + resource(&mut resources, 1039, data)?; + } + if let Some(data) = meta.exif { + resource(&mut resources, 1058, data)?; + } + if let Some(data) = meta.xmp { + resource(&mut resources, 1060, data.as_bytes())?; + } + out.extend_from_slice(&u32::try_from(resources.len()).map_err(|_| PsdError::LimitExceeded("resource section"))?.to_be_bytes()); + out.extend_from_slice(&resources); + out.extend_from_slice(&0u32.to_be_bytes()); + match samples { + Samples::U8(src) => { + out.extend_from_slice(&1u16.to_be_bytes()); + let rows = color.checked_mul(height as usize).ok_or(PsdError::LimitExceeded("row count"))?; + let table = out.len(); + let table_end = + table.checked_add(rows.checked_mul(2).ok_or(PsdError::LimitExceeded("row table"))?).ok_or(PsdError::LimitExceeded("row table"))?; + out.try_reserve(rows * 2).map_err(|_| PsdError::OutOfMemory("row table"))?; + out.resize(table_end, 0); + let mut row = Vec::new(); + row.try_reserve_exact(width as usize).map_err(|_| PsdError::OutOfMemory("row"))?; + for c in 0..color { + for y in 0..height as usize { + row.clear(); + let start = y * width as usize * ch; + row.extend(src[start + c..start + width as usize * ch].iter().step_by(ch).copied()); + let before = out.len(); + crate::compression::packbits::encode(&row, &mut out); + let count = u16::try_from(out.len() - before).map_err(|_| PsdError::LimitExceeded("PSD row length"))?; + let at = table + (c * height as usize + y) * 2; + out[at..at + 2].copy_from_slice(&count.to_be_bytes()); + } + } + } + Samples::U16(src) => { + out.extend_from_slice(&0u16.to_be_bytes()); + for c in 0..color { + for sample in src.iter().take(needed).skip(c).step_by(ch) { + out.extend_from_slice(&sample.to_be_bytes()); + } + } + } + } + Ok(out) +} + +fn resource(out: &mut Vec, id: u16, data: &[u8]) -> Result<()> { + let len = u32::try_from(data.len()).map_err(|_| PsdError::LimitExceeded("resource length"))?; + out.extend_from_slice(b"8BIM"); + out.extend_from_slice(&id.to_be_bytes()); + out.extend_from_slice(&[0, 0]); + out.extend_from_slice(&len.to_be_bytes()); + out.extend_from_slice(data); + if !data.len().is_multiple_of(2) { + out.push(0); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn writes_rgb_eight_bit_rows_and_preserves_resources() { + let bytes = write( + 2, + 2, + 4, + Samples::U8(&[10, 20, 30, 0, 40, 50, 60, 1, 70, 80, 90, 2, 100, 110, 120, 3]), + Metadata { icc: Some(&[1, 2, 3]), xmp: Some(""), ppi: Some(300), ..Metadata::default() }, + ) + .unwrap(); + let psd = crate::PsdFile::from_bytes(&bytes).unwrap(); + assert_eq!(psd.header.channels, 3); + assert_eq!(psd.decode_merged().unwrap(), [10, 40, 70, 100, 20, 50, 80, 110, 30, 60, 90, 120]); + assert!(psd.resources.iter().any(|r| r.id == 1039 && r.data == [1, 2, 3])); + assert!(psd.resources.iter().any(|r| r.id == 1060 && r.data == b"")); + } + + #[test] + fn writes_raw_sixteen_bit_gray_and_rejects_short_input() { + let bytes = write(2, 1, 2, Samples::U16(&[1, 999, 0xabcd, 0]), Metadata::default()).unwrap(); + let psd = crate::PsdFile::from_bytes(&bytes).unwrap(); + assert_eq!(psd.header.channels, 1); + assert_eq!(psd.decode_merged().unwrap(), [0, 1, 0xab, 0xcd]); + assert!(write(2, 2, 3, Samples::U8(&[0; 11]), Metadata::default()).is_err()); + assert!(write(30_001, 1, 1, Samples::U8(&[]), Metadata::default()).is_err()); + } +} diff --git a/crates/psd/src/lib.rs b/crates/psd/src/lib.rs index acd3c8853..0ddaa0007 100644 --- a/crates/psd/src/lib.rs +++ b/crates/psd/src/lib.rs @@ -71,6 +71,7 @@ pub mod file; pub mod filter_effects; #[cfg(any(test, feature = "testgen"))] pub mod fixtures; +pub mod flat; pub mod float; pub mod grd; pub mod hdr; From 902a722422583d051093679985793fb94b2af26e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 13:02:56 -0500 Subject: [PATCH 096/150] Allow independent XML plist token budgets --- crates/plist/src/lib.rs | 2 +- crates/plist/src/xml.rs | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/plist/src/lib.rs b/crates/plist/src/lib.rs index d668de277..d84980398 100644 --- a/crates/plist/src/lib.rs +++ b/crates/plist/src/lib.rs @@ -6,7 +6,7 @@ mod binary; mod bytes; mod xml; pub use binary::{DateValue, Value, first_string_in_array, parse_binary, parse_binary_with_limits}; -pub use xml::{XmlOptions, parse_xml, parse_xml_with_limits, parse_xml_with_options}; +pub use xml::{XmlOptions, parse_xml, parse_xml_with_limits, parse_xml_with_options, parse_xml_with_token_limit}; /// Caller-selected resource limits. Zero values refuse the corresponding resource. #[derive(Clone, Copy, Debug)] diff --git a/crates/plist/src/xml.rs b/crates/plist/src/xml.rs index d30b938d0..f4eee291d 100644 --- a/crates/plist/src/xml.rs +++ b/crates/plist/src/xml.rs @@ -386,6 +386,14 @@ pub fn parse_xml_with_limits(data: &[u8], limits: Limits) -> Result Result { + let max_tokens = limits.max_nodes.min(limits.max_objects).saturating_mul(8).saturating_add(8); + parse_xml_with_token_limit(data, limits, options, max_tokens) +} + +/// Parse XML with an independent token allowance, without reducing the value allowance. +/// This bounds token-heavy input (for example many CDATA chunks in one string). +/// Zero tokens refuses nonempty XML. Existing convenience APIs retain their default budget. +pub fn parse_xml_with_token_limit(data: &[u8], limits: Limits, options: XmlOptions, max_tokens: usize) -> Result { if data.len() > limits.max_input_bytes { return Err(FormatError::TooLarge("input bytes".into())); } @@ -406,7 +414,7 @@ pub fn parse_xml_with_options(data: &[u8], limits: Limits, options: XmlOptions) // XML normalizes literal line endings before parsing; character references such as // must still decode to a carriage return afterwards. let text = if text.contains('\r') { text.replace("\r\n", "\n").replace('\r', "\n") } else { text }; - let toks = tokenize(&text, limits.max_nodes.min(limits.max_objects).saturating_mul(8).saturating_add(8))?; + let toks = tokenize(&text, max_tokens)?; let mut x = Xml { toks, pos: 0, nodes: 0, limits, bytes_left: limits.max_expanded_bytes, options }; x.skip_space(); let wrapped = matches!(x.peek(), Some(Tok::Open(n)) if n == "plist"); @@ -431,6 +439,15 @@ pub fn parse_xml_with_options(data: &[u8], limits: Limits, options: XmlOptions) mod tests { #![allow(clippy::unwrap_used, clippy::panic)] use super::*; + #[test] + fn explicit_token_budget_is_independent_of_value_budget() { + let limits = Limits { max_nodes: 100, max_objects: 100, ..Limits::default() }; + let xml = b""; + assert!(matches!(parse_xml_with_token_limit(xml, limits, XmlOptions::default(), 3), Err(FormatError::TooLarge(_)))); + assert_eq!(parse_xml_with_token_limit(xml, limits, XmlOptions::default(), 4).unwrap(), Value::String("ab".into())); + assert!(matches!(parse_xml_with_token_limit(b"", limits, XmlOptions::default(), 0), Err(FormatError::TooLarge(_)))); + } + #[test] fn xml_has_one_complete_root_with_only_markup_space_around_values() { for invalid in [ From d181e08d3df8ff1c9b216778d668e6071a42993b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 13:04:18 -0500 Subject: [PATCH 097/150] Read selected merged PSD planes without parsing layers (#1623) * Read selected merged PSD planes without parsing layers * Validate merged PSD payload bounds without decoding * Expose declared dimensions for limit reporting * Cover complete and truncated merged payloads --- crates/psd/src/lib.rs | 1 + crates/psd/src/merged.rs | 251 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 252 insertions(+) create mode 100644 crates/psd/src/merged.rs diff --git a/crates/psd/src/lib.rs b/crates/psd/src/lib.rs index 0ddaa0007..4c3222f46 100644 --- a/crates/psd/src/lib.rs +++ b/crates/psd/src/lib.rs @@ -81,6 +81,7 @@ pub mod image_data; mod io; pub mod layer; mod lazy; +pub mod merged; pub mod metadata; pub mod path; pub mod patterns; diff --git a/crates/psd/src/merged.rs b/crates/psd/src/merged.rs new file mode 100644 index 000000000..467fb8904 --- /dev/null +++ b/crates/psd/src/merged.rs @@ -0,0 +1,251 @@ +//! Borrowed, merged-only PSD reader that skips layer records and decodes selected planes. +use crate::{Head, PsdError, Result}; + +/// Dimensions in the fixed header fields, for reporting a preflight limit error. +/// This does not validate the PSD signature or remaining header. +pub fn declared_dimensions(bytes: &[u8]) -> Option<(u32, u32)> { + let h = bytes.get(14..18)?; + let w = bytes.get(18..22)?; + Some((u32::from_be_bytes([w[0], w[1], w[2], w[3]]), u32::from_be_bytes([h[0], h[1], h[2], h[3]]))) +} + +/// The file head and merged-composite location. Layer records are not parsed or allocated. +pub struct Merged<'a> { + /// Header, color-mode data, and image resources. + pub head: Head, + /// Whether the layer count marks merged transparency. + pub transparency: bool, + /// Merged image compression identifier. + pub compression: u16, + bytes: &'a [u8], + data_offset: usize, +} + +fn take<'a>(bytes: &'a [u8], at: &mut usize, len: usize) -> Result<&'a [u8]> { + let end = at.checked_add(len).ok_or(PsdError::LimitExceeded("offset"))?; + let data = bytes.get(*at..end).ok_or_else(|| PsdError::invalid("truncated"))?; + *at = end; + Ok(data) +} +fn u16_at(bytes: &[u8], at: &mut usize) -> Result { + let b = take(bytes, at, 2)?; + Ok(u16::from_be_bytes([b[0], b[1]])) +} +fn u32_at(bytes: &[u8], at: &mut usize) -> Result { + let b = take(bytes, at, 4)?; + Ok(u32::from_be_bytes([b[0], b[1], b[2], b[3]])) +} +fn u64_at(bytes: &[u8], at: &mut usize) -> Result { + let b = take(bytes, at, 8)?; + Ok(u64::from_be_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]])) +} + +impl<'a> Merged<'a> { + /// Open the merged section with a pixel limit. This skips layer records, preserving the ability + /// to read a composite when its layer data is not understood. No image planes are allocated. + pub fn open(bytes: &'a [u8], max_pixels: u64) -> Result { + // Refuse a declared oversized canvas before copying color-mode data or resources. + if let Some((w, h)) = declared_dimensions(bytes) + && u64::from(w) * u64::from(h) > max_pixels + { + return Err(PsdError::LimitExceeded("pixel count")); + } + let head = Head::from_bytes(bytes)?; + let h = &head.header; + if u64::from(h.width) * u64::from(h.height) > max_pixels { + return Err(PsdError::LimitExceeded("pixel count")); + } + let psb = h.version.is_psb(); + let mut at = usize::try_from(head.layer_section_offset).map_err(|_| PsdError::LimitExceeded("offset"))?; + let len = if psb { u64_at(bytes, &mut at)? } else { u64::from(u32_at(bytes, &mut at)?) }; + let layer = take(bytes, &mut at, usize::try_from(len).map_err(|_| PsdError::LimitExceeded("layer section"))?)?; + let offset = if psb { 8 } else { 4 }; + let transparency = layer.get(offset..offset + 2).is_some_and(|b| i16::from_be_bytes([b[0], b[1]]) < 0); + let compression = u16_at(bytes, &mut at)?; + Ok(Self { head, transparency, compression, bytes, data_offset: at }) + } + + /// Check that all declared raw data or RLE row payloads fit without decoding them. + pub fn validate_all(&self) -> Result<()> { + let h = &self.head.header; + let bpc = match h.depth { + 8 => 1usize, + 16 => 2, + 32 => 4, + _ => return Err(PsdError::Unsupported("merged depth".into())), + }; + let channels = usize::from(h.channels); + let height = h.height as usize; + let mut at = self.data_offset; + match self.compression { + 0 => { + let len = (h.width as usize) + .checked_mul(height) + .and_then(|n| n.checked_mul(bpc)) + .and_then(|n| n.checked_mul(channels)) + .ok_or(PsdError::LimitExceeded("merged data"))?; + take(self.bytes, &mut at, len)?; + } + 1 => { + let rows = channels.checked_mul(height).ok_or(PsdError::LimitExceeded("row count"))?; + let entry = if h.version.is_psb() { 4 } else { 2 }; + let table_len = rows.checked_mul(entry).ok_or(PsdError::LimitExceeded("row table"))?; + let table = take(self.bytes, &mut at, table_len)?; + let mut total = 0usize; + for chunk in table.chunks_exact(entry) { + let len = if entry == 2 { + usize::from(u16::from_be_bytes([chunk[0], chunk[1]])) + } else { + u32::from_be_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]) as usize + }; + total = total.saturating_add(len); + } + take(self.bytes, &mut at, total)?; + } + _ => return Err(PsdError::Unsupported("merged compression".into())), + } + Ok(()) + } + + /// Decode only the first `used` planes, leaving spot channels untouched. Eight-, sixteen-, and + /// thirty-two-bit raw or PackBits samples are returned as planar big-endian bytes. Before any + /// RLE plane allocation, encoded row lengths must fit the input and the PackBits expansion bound. + pub fn read_planes(&self, used: usize) -> Result>> { + let h = &self.head.header; + let channels = usize::from(h.channels); + if used > channels { + return Err(PsdError::invalid("too few channels")); + } + let bpc = match h.depth { + 8 => 1usize, + 16 => 2, + 32 => 4, + _ => return Err(PsdError::Unsupported("merged depth".into())), + }; + let width = h.width as usize; + let height = h.height as usize; + let row = width.checked_mul(bpc).ok_or(PsdError::LimitExceeded("row size"))?; + let plane = row.checked_mul(height).ok_or(PsdError::LimitExceeded("plane size"))?; + let mut at = self.data_offset; + let mut planes = Vec::new(); + planes.try_reserve_exact(used).map_err(|_| PsdError::OutOfMemory("planes"))?; + match self.compression { + 0 => { + for _ in 0..used { + planes.push(take(self.bytes, &mut at, plane)?.to_vec()); + } + } + 1 => { + let entry = if h.version.is_psb() { 4 } else { 2 }; + let table_len = channels.checked_mul(height).and_then(|v| v.checked_mul(entry)).ok_or(PsdError::LimitExceeded("row table"))?; + let table = take(self.bytes, &mut at, table_len)?; + let count = |i: usize| -> usize { + let pos = i.saturating_mul(entry); + match table.get(pos..pos.saturating_add(entry)) { + Some(&[a, b]) => usize::from(u16::from_be_bytes([a, b])), + Some(&[a, b, c, d]) => u32::from_be_bytes([a, b, c, d]) as usize, + _ => 0, + } + }; + let min_row = row.div_ceil(128).saturating_mul(2); + let mut total = 0usize; + for i in 0..used.saturating_mul(height) { + let n = count(i); + if n < min_row { + return Err(PsdError::invalid("RLE row too short for its width")); + } + total = total.saturating_add(n); + } + if total > self.bytes.len().saturating_sub(at) { + return Err(PsdError::invalid("truncated")); + } + for c in 0..used { + let mut decoded = Vec::new(); + decoded.try_reserve_exact(plane).map_err(|_| PsdError::OutOfMemory("plane"))?; + decoded.resize(plane, 0); + for (y, row) in decoded.chunks_exact_mut(row).enumerate() { + let encoded = take(self.bytes, &mut at, count(c * height + y))?; + unpackbits(encoded, row)?; + } + planes.push(decoded); + } + } + _ => return Err(PsdError::Unsupported("merged compression".into())), + } + Ok(planes) + } +} + +/// Decode one PackBits row. Truncated packets leave the pre-zeroed remainder unchanged; packets +/// that overrun the output are rejected. Trailing encoded data after the row is ignored. +pub fn unpackbits(mut src: &[u8], row: &mut [u8]) -> Result<()> { + let mut at = 0; + while at < row.len() { + let Some((&h, rest)) = src.split_first() else { break }; + src = rest; + let h = h as i8; + if h >= 0 { + let n = h as usize + 1; + let lit = src.get(..n).unwrap_or(src); + let dst = row.get_mut(at..at + lit.len()).ok_or_else(|| PsdError::invalid("RLE row overruns its width"))?; + dst.copy_from_slice(lit); + at += lit.len(); + src = src.get(n..).unwrap_or(&[]); + } else if h != -128 { + let n = (1 - h as isize) as usize; + let Some((&b, rest)) = src.split_first() else { break }; + src = rest; + row.get_mut(at..at + n).ok_or_else(|| PsdError::invalid("RLE row overruns its width"))?.fill(b); + at += n; + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn head(channels: u16, compression: u16) -> Vec { + let mut b = b"8BPS\0\x01\0\0\0\0\0\0".to_vec(); + b.extend_from_slice(&channels.to_be_bytes()); + b.extend_from_slice(&2u32.to_be_bytes()); + b.extend_from_slice(&2u32.to_be_bytes()); + b.extend_from_slice(&16u16.to_be_bytes()); + b.extend_from_slice(&1u16.to_be_bytes()); + b.extend_from_slice(&0u32.to_be_bytes()); + b.extend_from_slice(&0u32.to_be_bytes()); + b.extend_from_slice(&0u32.to_be_bytes()); + b.extend_from_slice(&compression.to_be_bytes()); + b + } + + #[test] + fn reads_only_selected_rle_plane_without_extra_channel_data() { + let mut b = head(2, 1); + for _ in 0..4 { + b.extend_from_slice(&2u16.to_be_bytes()); + } + b.extend_from_slice(&[0xfd, 0x80, 0xfd, 0x80]); + let merged = Merged::open(&b, 4).unwrap(); + assert_eq!(merged.read_planes(1).unwrap(), vec![vec![0x80; 8]]); + assert!(merged.read_planes(2).is_err()); + assert!(merged.validate_all().is_err()); + assert!(Merged::open(&b, 3).is_err()); + } + + #[test] + fn skips_invalid_layer_contents_and_checks_rle_packets() { + let mut b = head(1, 0); + let layer_offset = 34; + b.splice(layer_offset..layer_offset + 4, [0, 0, 0, 3, 0xff, 0xfe, 0xfd]); + b.extend_from_slice(&[1; 8]); + let merged = Merged::open(&b, 4).unwrap(); + assert_eq!(merged.read_planes(1).unwrap(), vec![vec![1; 8]]); + assert!(merged.validate_all().is_ok()); + let mut row = [0; 4]; + unpackbits(&[3, 7, 8], &mut row).unwrap(); + assert_eq!(row, [7, 8, 0, 0]); + assert!(unpackbits(&[0x81, 1], &mut row).is_err()); + } +} From ef9b72796c1a84dc2ce68fb36155aa686508746a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:07:37 +0000 Subject: [PATCH 098/150] jpeg: share Adobe APP14 transform inspection --- crates/jpeg/src/app14.rs | 40 ++++++++++++++++++++++++++++++++++++++++ crates/jpeg/src/lib.rs | 3 +++ 2 files changed, 43 insertions(+) create mode 100644 crates/jpeg/src/app14.rs diff --git a/crates/jpeg/src/app14.rs b/crates/jpeg/src/app14.rs new file mode 100644 index 000000000..99ddabac1 --- /dev/null +++ b/crates/jpeg/src/app14.rs @@ -0,0 +1,40 @@ +//! Adobe APP14 transform inspection for JPEG samples. + +/// Read the Adobe APP14 transform byte needed for RGB/CMYK JPEG interpretation. +pub fn adobe_transform(jpeg: &[u8]) -> Option { + let mut at = 2usize; + if jpeg.get(..2) != Some(&[0xFF, 0xD8]) { + return None; + } + while at + 1 < jpeg.len() && jpeg[at] == 0xFF { + let marker = jpeg[at + 1]; + if marker == 0xDA { + return None; + } + let len = usize::from(u16::from_be_bytes([*jpeg.get(at + 2)?, *jpeg.get(at + 3)?])); + if len < 2 { + return None; + } + let end = at.checked_add(2 + len)?; + let segment = jpeg.get(at + 4..end)?; + if marker == 0xEE && segment.get(..5) == Some(b"Adobe") { + return segment.get(11).copied(); + } + at = end; + } + None +} + +#[cfg(test)] +mod tests { + use super::adobe_transform; + #[test] + fn reads_adobe_transform_and_rejects_truncated_segments() { + let mut jpeg = vec![0xff, 0xd8, 0xff, 0xee, 0, 14]; + jpeg.extend_from_slice(b"Adobe\0\x64\0\0\0\0\x02"); + assert_eq!(adobe_transform(&jpeg), Some(2)); + assert_eq!(adobe_transform(&jpeg[..jpeg.len() - 1]), None); + assert_eq!(adobe_transform(&[0xff, 0xd8, 0xff, 0xda]), None); + assert_eq!(adobe_transform(&[0xff, 0xd8, 0xff, 0xee, 0, 1]), None); + } +} diff --git a/crates/jpeg/src/lib.rs b/crates/jpeg/src/lib.rs index 50e1bd625..8d4ff1ca8 100644 --- a/crates/jpeg/src/lib.rs +++ b/crates/jpeg/src/lib.rs @@ -1706,3 +1706,6 @@ mod tests; /// Baseline JPEG encoder (8-bit, Huffman-coded sequential DCT). mod encode; pub use encode::{ChromaSubsampling, EncodeError, encode_baseline}; + +mod app14; +pub use app14::adobe_transform; From 204240ad80c652d6ca2d31d6930aedb8aa4eca2d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:07:43 +0000 Subject: [PATCH 099/150] ps: keep synthesized text spans inside clip scopes --- crates/ps/src/read/font.rs | 11 +++++++++-- crates/ps/src/read/pdf.rs | 6 +++++- crates/ps/src/read/tests.rs | 13 +++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/crates/ps/src/read/font.rs b/crates/ps/src/read/font.rs index 9a1af3fb6..f7b0e5ced 100644 --- a/crates/ps/src/read/font.rs +++ b/crates/ps/src/read/font.rs @@ -567,12 +567,19 @@ fn key_code(code: u32) -> Option { fn show_string(ip: &mut Interp, s: &[u8], draw: bool, per_char: &mut PerGlyph<'_>) -> R<(f64, f64)> { // Whitespace-only shows advance the text position but emit no painted glyphs; avoid bloating // the content stream with an empty semantic marked-content wrapper for them. - let text = ip.gs.font.as_ref().and_then(|font| actual_text(font, s)); + // Type 3 glyph programs can paint and change clipping inside a show. Composite fonts may + // select Type 3 descendants. Do not wrap their arbitrary output in a synthetic text span. + let text = ip + .gs + .font + .as_ref() + .filter(|font| !matches!(font.get_name(b"FontType").and_then(|v| v.num()), Some(0.0 | 3.0))) + .and_then(|font| actual_text(font, s)); if draw && !ip.charpath && let Some(text) = text.filter(|text| text.chars().any(|c| !c.is_whitespace())) { - ip.out.begin_actual_text(&text); + ip.out.begin_actual_text(&text, &ip.gs.clip).map_err(PsErr::Fatal)?; let result = show_glyphs(ip, s, draw, false, per_char); ip.out.end_actual_text(); result diff --git a/crates/ps/src/read/pdf.rs b/crates/ps/src/read/pdf.rs index 0faabe09a..3231dfaa4 100644 --- a/crates/ps/src/read/pdf.rs +++ b/crates/ps/src/read/pdf.rs @@ -153,8 +153,12 @@ impl Out { } /// Preserve the semantic text for consumers that group outlined glyphs. - pub fn begin_actual_text(&mut self, text: &str) { + pub fn begin_actual_text(&mut self, text: &str, clip: &Option>) -> Result<(), String> { + // A clip can outlive the show. Establish it before the marked span so EMC does not + // accidentally close the graphics state that later painting still needs. + self.sync_clip(clip)?; self.c.begin_marked_content_with_properties(Name(b"Span")).properties().actual_text(pdf_writer::TextStr(text)); + Ok(()) } pub fn end_actual_text(&mut self) { diff --git a/crates/ps/src/read/tests.rs b/crates/ps/src/read/tests.rs index 152041659..99c934158 100644 --- a/crates/ps/src/read/tests.rs +++ b/crates/ps/src/read/tests.rs @@ -137,6 +137,7 @@ fn type3_fonts_draw_their_glyphs() { /T3 T3 definefont pop /T3 findfont 20 scalefont setfont 10 20 moveto (AA) show"); // Two glyphs, the second 12 points (600 units at 20 pt) to the right. assert_eq!(c.lines().filter(|l| *l == "f").count(), 2, "{c}"); + assert!(!c.contains("/ActualText"), "Type 3 procedures are not synthetic text spans: {c}"); assert!(c.contains("22 20 m"), "{c}"); } @@ -471,6 +472,18 @@ fn actual_text_uses_the_selected_encoding_and_omits_unknown_names() { assert!(!content.contains("/ActualText"), "{content}"); } +#[test] +fn actual_text_starts_after_persistent_clip() { + let (content, _) = run( + "newpath 0 0 moveto 50 0 lineto 50 50 lineto closepath clip newpath /Helvetica findfont 12 scalefont setfont 10 10 moveto (Hi) show 0 0 100 100 rectfill", + ); + let clip = content.find("\nW\n").unwrap(); + let span = content.find("/ActualText (Hi)").unwrap(); + let end = content.find("EMC").unwrap(); + let restore = content.rfind("\nQ").unwrap(); + assert!(clip < span && span < end && end < restore, "{content}"); +} + #[test] fn text_paint_keeps_completed_glyphs_when_a_later_glyph_hits_the_work_limit() { let ttf = tiny_ttf(); From 8bd1412eaa7c0b58f3f9c401495d35fe12d6d85e Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:07:46 +0100 Subject: [PATCH 100/150] craft-dxf: add VectorCraft's ASCII drawing writer (#1588) * craft-dxf: add VectorCraft's ASCII drawing writer Move the incremental file writer from vectorcraft-cad (crates/cad/src/writer.rs) into craft_dxf::drawing. It writes the HEADER/TABLES/BLOCKS/OBJECTS skeleton around a pre-built ENTITIES section, which is the format API VectorCraft's scene mapper already uses. The existing model::write tag serializer is unchanged. Golden files lock the bytes for empty and one-LINE drawings in R12, 2000 and 2018. Part of #791 / #778. * test(dxf): preserve exact golden bytes on Windows checkout * fix(dxf): preserve BYBLOCK and use valid image dictionary keys * test(dxf): clone images before write in dictionary-key test Drawing::write takes self, so the assertions have to use a clone of the image list. The rest of the test is unchanged. --------- Co-authored-by: Dylan M. Taylor --- .gitattributes | 3 + crates/dxf/src/drawing.rs | 774 +++++++++++++ crates/dxf/src/lib.rs | 4 +- crates/dxf/tests/data/drawing_empty_r12.dxf | 250 ++++ crates/dxf/tests/data/drawing_empty_r2018.dxf | 972 ++++++++++++++++ crates/dxf/tests/data/drawing_line_r12.dxf | 268 +++++ crates/dxf/tests/data/drawing_line_r2000.dxf | 982 ++++++++++++++++ crates/dxf/tests/data/drawing_line_r2018.dxf | 1002 +++++++++++++++++ 8 files changed, 4254 insertions(+), 1 deletion(-) create mode 100644 crates/dxf/src/drawing.rs create mode 100644 crates/dxf/tests/data/drawing_empty_r12.dxf create mode 100644 crates/dxf/tests/data/drawing_empty_r2018.dxf create mode 100644 crates/dxf/tests/data/drawing_line_r12.dxf create mode 100644 crates/dxf/tests/data/drawing_line_r2000.dxf create mode 100644 crates/dxf/tests/data/drawing_line_r2018.dxf diff --git a/.gitattributes b/.gitattributes index a854bbdb9..0c35cf7c5 100644 --- a/.gitattributes +++ b/.gitattributes @@ -14,3 +14,6 @@ crates/font-store/index/sources.json -text # The build fixture verifies these license files are copied byte-for-byte. crates/fonts-build/tests/fixture/craft-fonts/fonts/**/OFL.txt -text + +# DXF drawing goldens assert exact writer bytes, including LF on every platform. +crates/dxf/tests/data/drawing_*.dxf -text diff --git a/crates/dxf/src/drawing.rs b/crates/dxf/src/drawing.rs new file mode 100644 index 000000000..0b52c337c --- /dev/null +++ b/crates/dxf/src/drawing.rs @@ -0,0 +1,774 @@ +//! Incremental ASCII DXF file writer used by VectorCraft. +//! +//! This is the file skeleton around a pre-written `ENTITIES` section: group codes, handles, and +//! the HEADER / TABLES / BLOCKS / OBJECTS sections CAD apps expect. It is a different API from +//! [`crate::model::write`], which serializes a parsed [`crate::model`] drawing as tags. +//! +//! Moved from `vectorcraft-cad` (`crates/cad/src/writer.rs`). The bytes a [`Drawing`] writes must +//! stay the same: VectorCraft's export tests treat this as the format oracle. +//! +//! DXF R13 and later give every object a handle and subclass markers; R2000 and later add layouts +//! and plot styles. + +use std::fmt::Write as _; + +pub use crate::model::write::Format; + +/// An object handle (written in hex). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Handle(pub u32); + +/// The handles of the fixed objects every drawing has. Entities and table entries count up from +/// [`FIRST_FREE`]. +pub mod fixed { + use super::Handle; + pub const LAYER_TABLE: Handle = Handle(0x1); + pub const LTYPE_TABLE: Handle = Handle(0x2); + pub const APPID_TABLE: Handle = Handle(0x3); + pub const DIMSTYLE_TABLE: Handle = Handle(0x4); + pub const STYLE_TABLE: Handle = Handle(0x5); + pub const UCS_TABLE: Handle = Handle(0x6); + pub const VIEW_TABLE: Handle = Handle(0x7); + pub const VPORT_TABLE: Handle = Handle(0x8); + pub const BLOCK_RECORD_TABLE: Handle = Handle(0x9); + pub const ROOT: Handle = Handle(0xA); + pub const GROUPS: Handle = Handle(0xB); + pub const LAYOUTS: Handle = Handle(0xC); + pub const MODEL_RECORD: Handle = Handle(0xD); + pub const PAPER_RECORD: Handle = Handle(0xE); + pub const MODEL_BLOCK: Handle = Handle(0xF); + pub const MODEL_END: Handle = Handle(0x10); + pub const PAPER_BLOCK: Handle = Handle(0x11); + pub const PAPER_END: Handle = Handle(0x12); + pub const MODEL_LAYOUT: Handle = Handle(0x13); + pub const PAPER_LAYOUT: Handle = Handle(0x14); + pub const ACTIVE_VPORT: Handle = Handle(0x15); + pub const ACAD_APPID: Handle = Handle(0x16); + pub const STANDARD_DIMSTYLE: Handle = Handle(0x17); + pub const IMAGE_DICT: Handle = Handle(0x18); + pub const IMAGE_VARS: Handle = Handle(0x19); + pub const PLOT_STYLES: Handle = Handle(0x1A); + pub const PLOT_NORMAL: Handle = Handle(0x1B); + pub const FIRST_FREE: u32 = 0x30; +} + +/// A number as DXF writes it: up to 6 decimals, no trailing zeros, 0 for anything not finite. +pub fn num(v: f64) -> String { + if !v.is_finite() { + return "0".into(); + } + let s = format!("{v:.6}"); + let s = s.trim_end_matches('0').trim_end_matches('.'); + if s == "-0" || s.is_empty() { "0".into() } else { s.into() } +} + +/// Text as a DXF string value: one line, `^` escaped, and before DXF 2007 (which reads UTF-8) +/// every character outside ASCII as `\U+XXXX`. +pub fn text(s: &str, utf8: bool) -> String { + let mut out = String::with_capacity(s.len()); + for c in s.chars() { + match c { + '\r' | '\n' | '\t' => out.push(' '), + c if c.is_control() => {} + '^' => out.push_str("^ "), + c if !utf8 && !c.is_ascii() => match u16::try_from(u32::from(c)) { + Ok(u) => _ = write!(out, "\\U+{u:04X}"), + Err(_) => out.push('?'), + }, + c => out.push(c), + } + } + out +} + +/// Group-code/value pairs in the order they are written. +pub struct Codes { + pub v: Format, + out: String, +} + +impl Codes { + pub fn new(v: Format) -> Self { + Self { v, out: String::new() } + } + + pub fn into_string(self) -> String { + self.out + } + + pub fn append(&mut self, other: &str) { + self.out.push_str(other); + } + + /// A value written as it is (names and keywords the writer itself chose). + pub fn raw(&mut self, code: u16, value: &str) { + _ = writeln!(self.out, "{code:>3}\n{value}"); + } + + /// Text from the document, encoded for the version (see [`text`]). + pub fn str(&mut self, code: u16, value: &str) { + let t = text(value, self.v.utf8()); + self.raw(code, &t); + } + + pub fn int(&mut self, code: u16, value: i64) { + _ = writeln!(self.out, "{code:>3}\n{value}"); + } + + pub fn num(&mut self, code: u16, value: f64) { + _ = writeln!(self.out, "{code:>3}\n{}", num(value)); + } + + pub fn handle(&mut self, code: u16, h: Handle) { + _ = writeln!(self.out, "{code:>3}\n{:X}", h.0); + } + + /// A 2D point at `code` (x), `code + 10` (y). + pub fn xy(&mut self, code: u16, x: f64, y: f64) { + self.num(code, x); + self.num(code + 10, y); + } + + /// A 3D point at `code`, `code + 10`, `code + 20` (z = 0). + pub fn xyz(&mut self, code: u16, x: f64, y: f64) { + self.xy(code, x, y); + self.num(code + 20, 0.0); + } + + /// The extrusion direction of planar entities: the z axis. + pub fn normal(&mut self) { + self.num(210, 0.0); + self.num(220, 0.0); + self.num(230, 1.0); + } + + /// The start of an object: its type, and from R13 its handle, owner and first subclass. + fn object(&mut self, kind: &str, h: Handle, owner: Handle, subclass: &str) { + self.raw(0, kind); + if self.v.handles() { + self.handle(if kind == "DIMSTYLE" { 105 } else { 5 }, h); + self.handle(330, owner); + self.raw(100, subclass); + } + } + + /// A table entry: the shared record subclass, then the table's own (from R13). + fn record(&mut self, kind: &str, h: Handle, table: Handle, subclass: &str) { + self.object(kind, h, table, "AcDbSymbolTableRecord"); + if self.v.handles() { + self.raw(100, subclass); + } + } + + fn section(&mut self, name: &str) { + self.raw(0, "SECTION"); + self.raw(2, name); + } + + fn end_section(&mut self) { + self.raw(0, "ENDSEC"); + } + + fn table(&mut self, name: &str, h: Handle, count: usize) { + self.raw(0, "TABLE"); + self.raw(2, name); + if self.v.handles() { + self.handle(5, h); + self.raw(330, "0"); + self.raw(100, "AcDbSymbolTable"); + } + self.int(70, count as i64); + if name == "DIMSTYLE" && self.v.r2000() { + self.raw(100, "AcDbDimStyleTable"); + } + } + + fn end_table(&mut self) { + self.raw(0, "ENDTAB"); + } +} + +/// How an entity is drawn: its colour index, true colour, lineweight, linetype and transparency. +#[derive(Clone, Debug, Default)] +pub struct EntityStyle { + pub aci: u8, + /// `0xRRGGBB` (DXF 2004 and later). + pub rgb: Option, + /// Hundredths of a millimetre (DXF 2000 and later). + pub lineweight: Option, + pub linetype: Option, + /// Opacity 0–255 (DXF 2004 and later; opaque is left out). + pub alpha: Option, +} + +impl Codes { + /// The start of an entity of `kind` owned by `owner` (model space, or a polyline for its + /// vertices) on `layer` with its style, ending with `subclass` (none when empty). + pub fn entity(&mut self, kind: &str, h: Handle, owner: Handle, layer: &str, style: &EntityStyle, subclass: &str) { + self.object(kind, h, owner, "AcDbEntity"); + self.str(8, layer); + if let Some(lt) = &style.linetype { + self.str(6, lt); + } + self.int(62, i64::from(style.aci)); + if self.v.r2000() + && let Some(lw) = style.lineweight + { + self.int(370, i64::from(lw)); + } + if self.v.true_color() { + if let Some(rgb) = style.rgb { + self.int(420, i64::from(rgb)); + } + if let Some(a) = style.alpha { + self.int(440, 0x0200_0000 | i64::from(a)); + } + } + if self.v.handles() && !subclass.is_empty() { + self.raw(100, subclass); + } + } +} + +/// A DXF layer. +#[derive(Clone, Debug)] +pub struct Layer { + pub name: String, + pub aci: u8, + /// Switched off (a hidden layer). + pub off: bool, + pub locked: bool, + /// Plotted (printable). + pub plot: bool, +} + +/// A dash pattern: dash, gap, dash… lengths in drawing units. +#[derive(Clone, Debug)] +pub struct LineType { + pub name: String, + pub pattern: Vec, +} + +/// A placed image file and the image entities that show it. +#[derive(Clone, Debug)] +pub struct ImageDef { + pub name: String, + pub size: (u32, u32), + pub handle: Handle, + /// `(reactor, image entity)` per image entity. + pub reactors: Vec<(Handle, Handle)>, +} + +/// Everything around the entities, collected while they were written. +pub struct Drawing { + pub version: Format, + pub layers: Vec, + pub linetypes: Vec, + /// Text styles: (style name, font family). + pub styles: Vec<(String, String)>, + pub images: Vec, + /// The entities' group codes. + pub entities: String, + /// `[x0, y0, x1, y1]` of everything written (drawing units). + pub extents: [f64; 4], + /// The export region (drawing units). + pub limits: [f64; 4], + /// `$INSUNITS`. + pub insunits: u8, + pub metric: bool, + /// The next free handle. + pub next: u32, +} + +/// The text style every drawing has. +pub const STANDARD_STYLE: &str = "Standard"; + +impl Drawing { + /// The whole file. + pub fn write(self) -> String { + let v = self.version; + let mut c = Codes::new(v); + // Table entries get the handles after the entities'. + let mut next = self.next; + let mut take = |n: usize| { + let start = next; + next = next.saturating_add(u32::try_from(n).unwrap_or(u32::MAX)); + start + }; + let ltype_base = take(3 + self.linetypes.len()); + let layer_base = take(1 + self.layers.len()); + let style_base = take(1 + self.styles.len()); + self.header(&mut c, next); + if v.handles() { + self.classes(&mut c); + } + c.section("TABLES"); + self.vport(&mut c); + self.ltypes(&mut c, ltype_base); + self.layer_table(&mut c, layer_base); + self.style_table(&mut c, style_base); + if v.handles() { + for (name, h) in [("VIEW", fixed::VIEW_TABLE), ("UCS", fixed::UCS_TABLE)] { + c.table(name, h, 0); + c.end_table(); + } + c.table("APPID", fixed::APPID_TABLE, 1); + c.record("APPID", fixed::ACAD_APPID, fixed::APPID_TABLE, "AcDbRegAppTableRecord"); + c.raw(2, "ACAD"); + c.int(70, 0); + c.end_table(); + c.table("DIMSTYLE", fixed::DIMSTYLE_TABLE, 1); + c.record("DIMSTYLE", fixed::STANDARD_DIMSTYLE, fixed::DIMSTYLE_TABLE, "AcDbDimStyleTableRecord"); + c.raw(2, "Standard"); + c.int(70, 0); + c.end_table(); + c.table("BLOCK_RECORD", fixed::BLOCK_RECORD_TABLE, 2); + for (name, h, layout) in + [("*Model_Space", fixed::MODEL_RECORD, fixed::MODEL_LAYOUT), ("*Paper_Space", fixed::PAPER_RECORD, fixed::PAPER_LAYOUT)] + { + c.record("BLOCK_RECORD", h, fixed::BLOCK_RECORD_TABLE, "AcDbBlockTableRecord"); + c.raw(2, name); + if v.r2000() { + c.handle(340, layout); + c.int(70, 0); + c.int(280, 1); + c.int(281, 0); + } + } + c.end_table(); + } + c.end_section(); + if v.handles() { + c.section("BLOCKS"); + for (name, record, begin, end) in [ + ("*Model_Space", fixed::MODEL_RECORD, fixed::MODEL_BLOCK, fixed::MODEL_END), + ("*Paper_Space", fixed::PAPER_RECORD, fixed::PAPER_BLOCK, fixed::PAPER_END), + ] { + c.object("BLOCK", begin, record, "AcDbEntity"); + c.raw(8, "0"); + c.raw(100, "AcDbBlockBegin"); + c.raw(2, name); + c.int(70, 0); + c.xyz(10, 0.0, 0.0); + c.raw(3, name); + c.raw(1, ""); + c.object("ENDBLK", end, record, "AcDbEntity"); + c.raw(8, "0"); + c.raw(100, "AcDbBlockEnd"); + } + c.end_section(); + } + c.section("ENTITIES"); + c.append(&self.entities); + c.end_section(); + if v.handles() { + self.objects(&mut c); + } + c.raw(0, "EOF"); + c.into_string() + } + + fn header(&self, c: &mut Codes, handseed: u32) { + let v = self.version; + let var = |c: &mut Codes, name: &str| c.raw(9, name); + c.section("HEADER"); + var(c, "$ACADVER"); + c.raw(1, v.acadver()); + var(c, "$DWGCODEPAGE"); + c.raw(3, "ANSI_1252"); + var(c, "$INSBASE"); + c.xyz(10, 0.0, 0.0); + let [x0, y0, x1, y1] = self.extents; + var(c, "$EXTMIN"); + c.xyz(10, x0, y0); + var(c, "$EXTMAX"); + c.xyz(10, x1, y1); + let [lx0, ly0, lx1, ly1] = self.limits; + var(c, "$LIMMIN"); + c.xy(10, lx0, ly0); + var(c, "$LIMMAX"); + c.xy(10, lx1, ly1); + if v.r2000() { + var(c, "$LWDISPLAY"); + c.int(290, 1); + var(c, "$INSUNITS"); + c.int(70, i64::from(self.insunits)); + var(c, "$MEASUREMENT"); + c.int(70, i64::from(self.metric)); + } + if v.handles() { + var(c, "$HANDSEED"); + c.handle(5, Handle(handseed)); + } + c.end_section(); + } + + fn classes(&self, c: &mut Codes) { + let mut classes: Vec<(&str, &str, &str, i64, bool)> = vec![]; + if self.version.r2000() { + classes.extend([ + ("ACDBDICTIONARYWDFLT", "AcDbDictionaryWithDefault", "ObjectDBX Classes", 0, false), + ("ACDBPLACEHOLDER", "AcDbPlaceHolder", "ObjectDBX Classes", 0, false), + ("LAYOUT", "AcDbLayout", "ObjectDBX Classes", 0, false), + ]); + } + if !self.images.is_empty() { + classes.extend([ + ("RASTERVARIABLES", "AcDbRasterVariables", "ISM", 0, false), + ("IMAGE", "AcDbRasterImage", "ISM", 2175, true), + ("IMAGEDEF", "AcDbRasterImageDef", "ISM", 0, false), + ("IMAGEDEF_REACTOR", "AcDbRasterImageDefReactor", "ISM", 1, false), + ]); + } + c.section("CLASSES"); + for (name, cpp, app, flags, entity) in classes { + c.raw(0, "CLASS"); + c.raw(1, name); + c.raw(2, cpp); + c.raw(3, app); + c.int(90, flags); + if self.version.true_color() { + c.int(91, 0); + } + c.int(280, 0); + c.int(281, i64::from(entity)); + } + c.end_section(); + } + + /// The active viewport, zoomed to the drawing's extents. + fn vport(&self, c: &mut Codes) { + let [x0, y0, x1, y1] = self.extents; + c.table("VPORT", fixed::VPORT_TABLE, 1); + c.record("VPORT", fixed::ACTIVE_VPORT, fixed::VPORT_TABLE, "AcDbViewportTableRecord"); + c.raw(2, if self.version.handles() { "*Active" } else { "*ACTIVE" }); + c.int(70, 0); + c.xy(10, 0.0, 0.0); + c.xy(11, 1.0, 1.0); + c.xy(12, (x0 + x1) / 2.0, (y0 + y1) / 2.0); + c.xy(13, 0.0, 0.0); + c.xy(14, 1.0, 1.0); + c.xy(15, 10.0, 10.0); + // The view direction looks down the z axis: (0, 0, 1), each code once. + c.xy(16, 0.0, 0.0); + c.num(36, 1.0); + c.xyz(17, 0.0, 0.0); + let (w, h) = ((x1 - x0).abs(), (y1 - y0).abs()); + // The view's height, with a margin; its width follows from the aspect ratio. + c.num(40, (h.max(w / 1.5) * 1.1).max(1.0)); + c.num(41, 1.5); + c.num(42, 50.0); + c.num(43, 0.0); + c.num(44, 0.0); + c.num(50, 0.0); + c.num(51, 0.0); + for (code, value) in [(71, 0), (72, 1000), (73, 1), (74, 3), (75, 0), (76, 0), (77, 0), (78, 0)] { + c.int(code, value); + } + if self.version.r2000() { + c.int(281, 0); + c.int(65, 0); + c.num(146, 0.0); + } + c.end_table(); + } + + fn ltypes(&self, c: &mut Codes, base: u32) { + c.table("LTYPE", fixed::LTYPE_TABLE, 3 + self.linetypes.len()); + let solid = [("ByBlock", ""), ("ByLayer", ""), ("Continuous", "Solid line")]; + let rows = solid.iter().map(|(n, d)| (*n, *d, &[][..])).chain(self.linetypes.iter().map(|l| (l.name.as_str(), "", l.pattern.as_slice()))); + for (i, (name, desc, pattern)) in rows.enumerate() { + c.record("LTYPE", Handle(base.saturating_add(i as u32)), fixed::LTYPE_TABLE, "AcDbLinetypeTableRecord"); + c.str(2, name); + c.int(70, 0); + c.str(3, if pattern.is_empty() { desc } else { "Dashed" }); + c.int(72, 65); + c.int(73, pattern.len() as i64); + c.num(40, pattern.iter().map(|v| v.abs()).sum()); + for v in pattern { + c.num(49, *v); + if self.version.handles() { + c.int(74, 0); + } + } + } + c.end_table(); + } + + fn layer_table(&self, c: &mut Codes, base: u32) { + let zero = Layer { name: "0".into(), aci: 7, off: false, locked: false, plot: true }; + c.table("LAYER", fixed::LAYER_TABLE, 1 + self.layers.len()); + for (i, l) in std::iter::once(&zero).chain(&self.layers).enumerate() { + c.record("LAYER", Handle(base.saturating_add(i as u32)), fixed::LAYER_TABLE, "AcDbLayerTableRecord"); + c.str(2, &l.name); + c.int(70, if l.locked { 4 } else { 0 }); + // A negative colour switches the layer off. + let aci = i64::from(l.aci.max(1)); + c.int(62, if l.off { -aci } else { aci }); + c.raw(6, "Continuous"); + if self.version.r2000() { + c.int(290, i64::from(l.plot)); + c.int(370, -3); + c.handle(390, fixed::PLOT_NORMAL); + } + } + c.end_table(); + } + + fn style_table(&self, c: &mut Codes, base: u32) { + c.table("STYLE", fixed::STYLE_TABLE, 1 + self.styles.len()); + let standard = (STANDARD_STYLE.to_string(), String::new()); + for (i, (name, family)) in std::iter::once(&standard).chain(&self.styles).enumerate() { + c.record("STYLE", Handle(base.saturating_add(i as u32)), fixed::STYLE_TABLE, "AcDbTextStyleTableRecord"); + c.str(2, name); + c.int(70, 0); + c.num(40, 0.0); + c.num(41, 1.0); + c.num(50, 0.0); + c.int(71, 0); + c.num(42, 2.5); + // The font file; from 2000 the family name too, which is how CAD apps find a TrueType font. + if family.is_empty() { + c.raw(3, "txt"); + } else { + c.str(3, &format!("{family}.ttf")); + } + c.raw(4, ""); + if self.version.r2000() && !family.is_empty() { + c.raw(1001, "ACAD"); + c.str(1000, family); + c.int(1071, 0); + } + } + c.end_table(); + } + + fn objects(&self, c: &mut Codes) { + let v = self.version; + let images = !self.images.is_empty(); + c.section("OBJECTS"); + let dict = |c: &mut Codes, h: Handle, owner: Handle, entries: &[(&str, Handle)]| { + c.object("DICTIONARY", h, owner, "AcDbDictionary"); + c.int(281, 1); + for (name, e) in entries { + c.str(3, name); + c.handle(350, *e); + } + }; + let mut root = vec![("ACAD_GROUP", fixed::GROUPS)]; + if images { + root.extend([("ACAD_IMAGE_DICT", fixed::IMAGE_DICT), ("ACAD_IMAGE_VARS", fixed::IMAGE_VARS)]); + } + if v.r2000() { + root.extend([("ACAD_LAYOUT", fixed::LAYOUTS), ("ACAD_PLOTSTYLENAME", fixed::PLOT_STYLES)]); + } + dict(c, fixed::ROOT, Handle(0), &root); + dict(c, fixed::GROUPS, fixed::ROOT, &[]); + if v.r2000() { + dict(c, fixed::LAYOUTS, fixed::ROOT, &[("Layout1", fixed::PAPER_LAYOUT), ("Model", fixed::MODEL_LAYOUT)]); + c.object("ACDBDICTIONARYWDFLT", fixed::PLOT_STYLES, fixed::ROOT, "AcDbDictionary"); + c.int(281, 1); + c.raw(3, "Normal"); + c.handle(350, fixed::PLOT_NORMAL); + c.raw(100, "AcDbDictionaryWithDefault"); + c.handle(340, fixed::PLOT_NORMAL); + // A placeholder has no data and no subclass marker of its own. + c.raw(0, "ACDBPLACEHOLDER"); + c.handle(5, fixed::PLOT_NORMAL); + c.handle(330, fixed::PLOT_STYLES); + self.layout(c, fixed::MODEL_LAYOUT, "Model", 0, fixed::MODEL_RECORD); + self.layout(c, fixed::PAPER_LAYOUT, "Layout1", 1, fixed::PAPER_RECORD); + } + if images { + c.object("RASTERVARIABLES", fixed::IMAGE_VARS, fixed::ROOT, "AcDbRasterVariables"); + c.int(90, 0); + c.int(70, 0); + c.int(71, 1); + c.int(72, 0); + let keys: Vec = self.images.iter().map(|i| format!("IMAGE_{:X}", i.handle.0)).collect(); + let entries: Vec<(&str, Handle)> = keys.iter().zip(&self.images).map(|(key, image)| (key.as_str(), image.handle)).collect(); + dict(c, fixed::IMAGE_DICT, fixed::ROOT, &entries); + for img in &self.images { + c.raw(0, "IMAGEDEF"); + c.handle(5, img.handle); + c.raw(102, "{ACAD_REACTORS"); + c.handle(330, fixed::IMAGE_DICT); + for (reactor, _) in &img.reactors { + c.handle(330, *reactor); + } + c.raw(102, "}"); + c.handle(330, fixed::IMAGE_DICT); + c.raw(100, "AcDbRasterImageDef"); + c.int(90, 0); + c.str(1, &img.name); + c.xy(10, f64::from(img.size.0), f64::from(img.size.1)); + c.xy(11, 1.0, 1.0); + c.int(280, 1); + c.int(281, 0); + for (reactor, image) in &img.reactors { + c.object("IMAGEDEF_REACTOR", *reactor, *image, "AcDbRasterImageDefReactor"); + c.int(90, 2); + c.handle(330, *image); + } + } + } + c.end_section(); + } + + /// A layout (DXF 2000 and later): its plot settings, then the layout itself. + fn layout(&self, c: &mut Codes, h: Handle, name: &str, tab: i64, record: Handle) { + let [x0, y0, x1, y1] = self.extents; + c.object("LAYOUT", h, fixed::LAYOUTS, "AcDbPlotSettings"); + c.raw(1, ""); + c.raw(2, "none_device"); + c.raw(4, ""); + c.raw(6, ""); + for code in 40..=49 { + c.num(code, 0.0); + } + c.num(140, 0.0); + c.num(141, 0.0); + c.num(142, 1.0); + c.num(143, 1.0); + // Plot flags; 1024 marks the model space layout. + c.int(70, if record == fixed::MODEL_RECORD { 688 | 1024 } else { 688 }); + c.int(72, 1); + c.int(73, 0); + c.int(74, 5); + c.raw(7, ""); + c.int(75, 16); + if self.version.true_color() { + // Shade plot mode (as displayed), resolution level (normal) and custom DPI. + c.int(76, 0); + c.int(77, 2); + c.int(78, 300); + } + c.num(147, 1.0); + // The paper image origin is 148 (x) and 149 (y), not a point at 148 and 158. + c.num(148, 0.0); + c.num(149, 0.0); + c.raw(100, "AcDbLayout"); + c.raw(1, name); + c.int(70, 1); + c.int(71, tab); + let [lx0, ly0, lx1, ly1] = self.limits; + c.xy(10, lx0, ly0); + c.xy(11, lx1, ly1); + c.xyz(12, 0.0, 0.0); + c.xyz(14, x0, y0); + c.xyz(15, x1, y1); + c.num(146, 0.0); + c.xyz(13, 0.0, 0.0); + c.xyz(16, 1.0, 0.0); + c.xyz(17, 0.0, 1.0); + c.int(76, 1); + c.handle(330, record); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn numbers_and_text_are_written_plainly() { + assert_eq!(num(1.5), "1.5"); + assert_eq!(num(-0.0000001), "0"); + assert_eq!(num(2.0), "2"); + assert_eq!(num(f64::NAN), "0"); + assert_eq!(text("a^b\nc", true), "a^ b c"); + assert_eq!(text("Größe 😀", false), "Gr\\U+00F6\\U+00DFe ?"); + assert_eq!(text("Größe", true), "Größe"); + } + + /// A line from (0, 0) to (10, 20) on layer `Layer1`, in the style scene.rs writes entities. + fn line_entities(v: Format) -> String { + let mut e = Codes::new(v); + let style = EntityStyle { aci: 1, rgb: Some(0xFF0000), lineweight: Some(25), linetype: None, alpha: None }; + e.entity("LINE", Handle(fixed::FIRST_FREE), fixed::MODEL_RECORD, "Layer1", &style, "AcDbLine"); + e.xyz(10, 0.0, 0.0); + e.xyz(11, 10.0, 20.0); + e.into_string() + } + + fn sample(v: Format, with_line: bool) -> Drawing { + Drawing { + version: v, + layers: vec![Layer { name: "Layer1".into(), aci: 1, off: false, locked: false, plot: true }], + linetypes: vec![], + styles: vec![], + images: vec![], + entities: if with_line { line_entities(v) } else { String::new() }, + extents: [0.0, 0.0, 200.0, 100.0], + limits: [0.0, 0.0, 200.0, 100.0], + insunits: 4, + metric: true, + next: fixed::FIRST_FREE + if with_line { 1 } else { 0 }, + } + } + + fn assert_golden(name: &str, got: &str) { + let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/data").join(name); + if std::env::var_os("WRITE_DXF_GOLDENS").is_some() { + std::fs::create_dir_all(path.parent().expect("parent")).expect("mkdir"); + std::fs::write(&path, got).expect("write golden"); + return; + } + let expected = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {}: {e}", path.display())); + assert_eq!(got, expected, "bytes changed versus {}", path.display()); + } + + use std::path::PathBuf; + + #[test] + fn empty_and_line_drawings_match_goldens() { + for (name, v, line) in [ + ("drawing_empty_r12.dxf", Format::R12, false), + ("drawing_empty_r2018.dxf", Format::R2018, false), + ("drawing_line_r12.dxf", Format::R12, true), + ("drawing_line_r2000.dxf", Format::R2000, true), + ("drawing_line_r2018.dxf", Format::R2018, true), + ] { + assert_golden(name, &sample(v, line).write()); + } + } + + #[test] + fn entity_preserves_byblock_colour() { + for v in Format::ALL { + let mut c = Codes::new(v); + c.entity("LINE", Handle(fixed::FIRST_FREE), fixed::MODEL_RECORD, "0", &EntityStyle::default(), "AcDbLine"); + assert!(c.into_string().contains(" 62\n0\n"), "{v:?}"); + } + } + + #[test] + fn image_dictionary_keys_are_independent_of_filenames() { + let mut drawing = sample(Format::R2018, false); + for (index, name) in [r"C:\images\scan,1.png", "scan,1.png"].into_iter().enumerate() { + drawing.images.push(ImageDef { name: name.into(), size: (10, 20), handle: Handle(fixed::FIRST_FREE + index as u32), reactors: vec![] }); + } + drawing.next = fixed::FIRST_FREE + 2; + let images = drawing.images.clone(); + let output = drawing.write(); + for image in &images { + assert!(output.contains(&format!(" 3\nIMAGE_{:X}\n350\n{:X}\n", image.handle.0, image.handle.0))); + assert!(output.contains(&format!(" 1\n{}\n", image.name))); + assert!(!output.contains(&format!(" 3\n{}\n", image.name))); + } + } + + #[test] + fn every_format_writes_its_acadver_and_ends_at_eof() { + for v in Format::ALL { + let s = sample(v, true).write(); + assert!(s.contains(&format!(" 1\n{}\n", v.acadver())), "{v:?}"); + assert!(s.ends_with(" 0\nEOF\n"), "{v:?}"); + if v.handles() { + assert!(s.contains(" 0\nSECTION\n 2\nCLASSES\n"), "{v:?}"); + } else { + assert!(!s.contains("\nCLASSES\n"), "{v:?}"); + } + } + } +} diff --git a/crates/dxf/src/lib.rs b/crates/dxf/src/lib.rs index 7543fb28b..5f18e3291 100644 --- a/crates/dxf/src/lib.rs +++ b/crates/dxf/src/lib.rs @@ -3,13 +3,15 @@ //! DXF is a sequence of (group code, value) pairs. This crate parses ASCII and binary DXF into a //! flat list of [`Tag`]s, splits them into sections and entities, and writes tags back. [`model`] //! reads the tags as a drawing (header variables, tables, blocks, entities, objects); mapping -//! that onto an application's own document is left to the caller. Written from Autodesk's +//! that onto an application's own document is left to the caller. [`drawing`] writes an ASCII +//! file around a pre-built `ENTITIES` section (VectorCraft's writer). Written from Autodesk's //! published DXF Reference; depends on no other Craft crate. Moved from CADCraft's `cadcraft-dxf`. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] #![forbid(unsafe_code)] use std::io::Write as _; +pub mod drawing; pub mod model; pub mod mtext; pub mod mtext_tokens; diff --git a/crates/dxf/tests/data/drawing_empty_r12.dxf b/crates/dxf/tests/data/drawing_empty_r12.dxf new file mode 100644 index 000000000..402512d9e --- /dev/null +++ b/crates/dxf/tests/data/drawing_empty_r12.dxf @@ -0,0 +1,250 @@ + 0 +SECTION + 2 +HEADER + 9 +$ACADVER + 1 +AC1009 + 9 +$DWGCODEPAGE + 3 +ANSI_1252 + 9 +$INSBASE + 10 +0 + 20 +0 + 30 +0 + 9 +$EXTMIN + 10 +0 + 20 +0 + 30 +0 + 9 +$EXTMAX + 10 +200 + 20 +100 + 30 +0 + 9 +$LIMMIN + 10 +0 + 20 +0 + 9 +$LIMMAX + 10 +200 + 20 +100 + 0 +ENDSEC + 0 +SECTION + 2 +TABLES + 0 +TABLE + 2 +VPORT + 70 +1 + 0 +VPORT + 2 +*ACTIVE + 70 +0 + 10 +0 + 20 +0 + 11 +1 + 21 +1 + 12 +100 + 22 +50 + 13 +0 + 23 +0 + 14 +1 + 24 +1 + 15 +10 + 25 +10 + 16 +0 + 26 +0 + 36 +1 + 17 +0 + 27 +0 + 37 +0 + 40 +146.666667 + 41 +1.5 + 42 +50 + 43 +0 + 44 +0 + 50 +0 + 51 +0 + 71 +0 + 72 +1000 + 73 +1 + 74 +3 + 75 +0 + 76 +0 + 77 +0 + 78 +0 + 0 +ENDTAB + 0 +TABLE + 2 +LTYPE + 70 +3 + 0 +LTYPE + 2 +ByBlock + 70 +0 + 3 + + 72 +65 + 73 +0 + 40 +0 + 0 +LTYPE + 2 +ByLayer + 70 +0 + 3 + + 72 +65 + 73 +0 + 40 +0 + 0 +LTYPE + 2 +Continuous + 70 +0 + 3 +Solid line + 72 +65 + 73 +0 + 40 +0 + 0 +ENDTAB + 0 +TABLE + 2 +LAYER + 70 +2 + 0 +LAYER + 2 +0 + 70 +0 + 62 +7 + 6 +Continuous + 0 +LAYER + 2 +Layer1 + 70 +0 + 62 +1 + 6 +Continuous + 0 +ENDTAB + 0 +TABLE + 2 +STYLE + 70 +1 + 0 +STYLE + 2 +Standard + 70 +0 + 40 +0 + 41 +1 + 50 +0 + 71 +0 + 42 +2.5 + 3 +txt + 4 + + 0 +ENDTAB + 0 +ENDSEC + 0 +SECTION + 2 +ENTITIES + 0 +ENDSEC + 0 +EOF diff --git a/crates/dxf/tests/data/drawing_empty_r2018.dxf b/crates/dxf/tests/data/drawing_empty_r2018.dxf new file mode 100644 index 000000000..01e9d07b3 --- /dev/null +++ b/crates/dxf/tests/data/drawing_empty_r2018.dxf @@ -0,0 +1,972 @@ + 0 +SECTION + 2 +HEADER + 9 +$ACADVER + 1 +AC1032 + 9 +$DWGCODEPAGE + 3 +ANSI_1252 + 9 +$INSBASE + 10 +0 + 20 +0 + 30 +0 + 9 +$EXTMIN + 10 +0 + 20 +0 + 30 +0 + 9 +$EXTMAX + 10 +200 + 20 +100 + 30 +0 + 9 +$LIMMIN + 10 +0 + 20 +0 + 9 +$LIMMAX + 10 +200 + 20 +100 + 9 +$LWDISPLAY +290 +1 + 9 +$INSUNITS + 70 +4 + 9 +$MEASUREMENT + 70 +1 + 9 +$HANDSEED + 5 +36 + 0 +ENDSEC + 0 +SECTION + 2 +CLASSES + 0 +CLASS + 1 +ACDBDICTIONARYWDFLT + 2 +AcDbDictionaryWithDefault + 3 +ObjectDBX Classes + 90 +0 + 91 +0 +280 +0 +281 +0 + 0 +CLASS + 1 +ACDBPLACEHOLDER + 2 +AcDbPlaceHolder + 3 +ObjectDBX Classes + 90 +0 + 91 +0 +280 +0 +281 +0 + 0 +CLASS + 1 +LAYOUT + 2 +AcDbLayout + 3 +ObjectDBX Classes + 90 +0 + 91 +0 +280 +0 +281 +0 + 0 +ENDSEC + 0 +SECTION + 2 +TABLES + 0 +TABLE + 2 +VPORT + 5 +8 +330 +0 +100 +AcDbSymbolTable + 70 +1 + 0 +VPORT + 5 +15 +330 +8 +100 +AcDbSymbolTableRecord +100 +AcDbViewportTableRecord + 2 +*Active + 70 +0 + 10 +0 + 20 +0 + 11 +1 + 21 +1 + 12 +100 + 22 +50 + 13 +0 + 23 +0 + 14 +1 + 24 +1 + 15 +10 + 25 +10 + 16 +0 + 26 +0 + 36 +1 + 17 +0 + 27 +0 + 37 +0 + 40 +146.666667 + 41 +1.5 + 42 +50 + 43 +0 + 44 +0 + 50 +0 + 51 +0 + 71 +0 + 72 +1000 + 73 +1 + 74 +3 + 75 +0 + 76 +0 + 77 +0 + 78 +0 +281 +0 + 65 +0 +146 +0 + 0 +ENDTAB + 0 +TABLE + 2 +LTYPE + 5 +2 +330 +0 +100 +AcDbSymbolTable + 70 +3 + 0 +LTYPE + 5 +30 +330 +2 +100 +AcDbSymbolTableRecord +100 +AcDbLinetypeTableRecord + 2 +ByBlock + 70 +0 + 3 + + 72 +65 + 73 +0 + 40 +0 + 0 +LTYPE + 5 +31 +330 +2 +100 +AcDbSymbolTableRecord +100 +AcDbLinetypeTableRecord + 2 +ByLayer + 70 +0 + 3 + + 72 +65 + 73 +0 + 40 +0 + 0 +LTYPE + 5 +32 +330 +2 +100 +AcDbSymbolTableRecord +100 +AcDbLinetypeTableRecord + 2 +Continuous + 70 +0 + 3 +Solid line + 72 +65 + 73 +0 + 40 +0 + 0 +ENDTAB + 0 +TABLE + 2 +LAYER + 5 +1 +330 +0 +100 +AcDbSymbolTable + 70 +2 + 0 +LAYER + 5 +33 +330 +1 +100 +AcDbSymbolTableRecord +100 +AcDbLayerTableRecord + 2 +0 + 70 +0 + 62 +7 + 6 +Continuous +290 +1 +370 +-3 +390 +1B + 0 +LAYER + 5 +34 +330 +1 +100 +AcDbSymbolTableRecord +100 +AcDbLayerTableRecord + 2 +Layer1 + 70 +0 + 62 +1 + 6 +Continuous +290 +1 +370 +-3 +390 +1B + 0 +ENDTAB + 0 +TABLE + 2 +STYLE + 5 +5 +330 +0 +100 +AcDbSymbolTable + 70 +1 + 0 +STYLE + 5 +35 +330 +5 +100 +AcDbSymbolTableRecord +100 +AcDbTextStyleTableRecord + 2 +Standard + 70 +0 + 40 +0 + 41 +1 + 50 +0 + 71 +0 + 42 +2.5 + 3 +txt + 4 + + 0 +ENDTAB + 0 +TABLE + 2 +VIEW + 5 +7 +330 +0 +100 +AcDbSymbolTable + 70 +0 + 0 +ENDTAB + 0 +TABLE + 2 +UCS + 5 +6 +330 +0 +100 +AcDbSymbolTable + 70 +0 + 0 +ENDTAB + 0 +TABLE + 2 +APPID + 5 +3 +330 +0 +100 +AcDbSymbolTable + 70 +1 + 0 +APPID + 5 +16 +330 +3 +100 +AcDbSymbolTableRecord +100 +AcDbRegAppTableRecord + 2 +ACAD + 70 +0 + 0 +ENDTAB + 0 +TABLE + 2 +DIMSTYLE + 5 +4 +330 +0 +100 +AcDbSymbolTable + 70 +1 +100 +AcDbDimStyleTable + 0 +DIMSTYLE +105 +17 +330 +4 +100 +AcDbSymbolTableRecord +100 +AcDbDimStyleTableRecord + 2 +Standard + 70 +0 + 0 +ENDTAB + 0 +TABLE + 2 +BLOCK_RECORD + 5 +9 +330 +0 +100 +AcDbSymbolTable + 70 +2 + 0 +BLOCK_RECORD + 5 +D +330 +9 +100 +AcDbSymbolTableRecord +100 +AcDbBlockTableRecord + 2 +*Model_Space +340 +13 + 70 +0 +280 +1 +281 +0 + 0 +BLOCK_RECORD + 5 +E +330 +9 +100 +AcDbSymbolTableRecord +100 +AcDbBlockTableRecord + 2 +*Paper_Space +340 +14 + 70 +0 +280 +1 +281 +0 + 0 +ENDTAB + 0 +ENDSEC + 0 +SECTION + 2 +BLOCKS + 0 +BLOCK + 5 +F +330 +D +100 +AcDbEntity + 8 +0 +100 +AcDbBlockBegin + 2 +*Model_Space + 70 +0 + 10 +0 + 20 +0 + 30 +0 + 3 +*Model_Space + 1 + + 0 +ENDBLK + 5 +10 +330 +D +100 +AcDbEntity + 8 +0 +100 +AcDbBlockEnd + 0 +BLOCK + 5 +11 +330 +E +100 +AcDbEntity + 8 +0 +100 +AcDbBlockBegin + 2 +*Paper_Space + 70 +0 + 10 +0 + 20 +0 + 30 +0 + 3 +*Paper_Space + 1 + + 0 +ENDBLK + 5 +12 +330 +E +100 +AcDbEntity + 8 +0 +100 +AcDbBlockEnd + 0 +ENDSEC + 0 +SECTION + 2 +ENTITIES + 0 +ENDSEC + 0 +SECTION + 2 +OBJECTS + 0 +DICTIONARY + 5 +A +330 +0 +100 +AcDbDictionary +281 +1 + 3 +ACAD_GROUP +350 +B + 3 +ACAD_LAYOUT +350 +C + 3 +ACAD_PLOTSTYLENAME +350 +1A + 0 +DICTIONARY + 5 +B +330 +A +100 +AcDbDictionary +281 +1 + 0 +DICTIONARY + 5 +C +330 +A +100 +AcDbDictionary +281 +1 + 3 +Layout1 +350 +14 + 3 +Model +350 +13 + 0 +ACDBDICTIONARYWDFLT + 5 +1A +330 +A +100 +AcDbDictionary +281 +1 + 3 +Normal +350 +1B +100 +AcDbDictionaryWithDefault +340 +1B + 0 +ACDBPLACEHOLDER + 5 +1B +330 +1A + 0 +LAYOUT + 5 +13 +330 +C +100 +AcDbPlotSettings + 1 + + 2 +none_device + 4 + + 6 + + 40 +0 + 41 +0 + 42 +0 + 43 +0 + 44 +0 + 45 +0 + 46 +0 + 47 +0 + 48 +0 + 49 +0 +140 +0 +141 +0 +142 +1 +143 +1 + 70 +1712 + 72 +1 + 73 +0 + 74 +5 + 7 + + 75 +16 + 76 +0 + 77 +2 + 78 +300 +147 +1 +148 +0 +149 +0 +100 +AcDbLayout + 1 +Model + 70 +1 + 71 +0 + 10 +0 + 20 +0 + 11 +200 + 21 +100 + 12 +0 + 22 +0 + 32 +0 + 14 +0 + 24 +0 + 34 +0 + 15 +200 + 25 +100 + 35 +0 +146 +0 + 13 +0 + 23 +0 + 33 +0 + 16 +1 + 26 +0 + 36 +0 + 17 +0 + 27 +1 + 37 +0 + 76 +1 +330 +D + 0 +LAYOUT + 5 +14 +330 +C +100 +AcDbPlotSettings + 1 + + 2 +none_device + 4 + + 6 + + 40 +0 + 41 +0 + 42 +0 + 43 +0 + 44 +0 + 45 +0 + 46 +0 + 47 +0 + 48 +0 + 49 +0 +140 +0 +141 +0 +142 +1 +143 +1 + 70 +688 + 72 +1 + 73 +0 + 74 +5 + 7 + + 75 +16 + 76 +0 + 77 +2 + 78 +300 +147 +1 +148 +0 +149 +0 +100 +AcDbLayout + 1 +Layout1 + 70 +1 + 71 +1 + 10 +0 + 20 +0 + 11 +200 + 21 +100 + 12 +0 + 22 +0 + 32 +0 + 14 +0 + 24 +0 + 34 +0 + 15 +200 + 25 +100 + 35 +0 +146 +0 + 13 +0 + 23 +0 + 33 +0 + 16 +1 + 26 +0 + 36 +0 + 17 +0 + 27 +1 + 37 +0 + 76 +1 +330 +E + 0 +ENDSEC + 0 +EOF diff --git a/crates/dxf/tests/data/drawing_line_r12.dxf b/crates/dxf/tests/data/drawing_line_r12.dxf new file mode 100644 index 000000000..6810c07cb --- /dev/null +++ b/crates/dxf/tests/data/drawing_line_r12.dxf @@ -0,0 +1,268 @@ + 0 +SECTION + 2 +HEADER + 9 +$ACADVER + 1 +AC1009 + 9 +$DWGCODEPAGE + 3 +ANSI_1252 + 9 +$INSBASE + 10 +0 + 20 +0 + 30 +0 + 9 +$EXTMIN + 10 +0 + 20 +0 + 30 +0 + 9 +$EXTMAX + 10 +200 + 20 +100 + 30 +0 + 9 +$LIMMIN + 10 +0 + 20 +0 + 9 +$LIMMAX + 10 +200 + 20 +100 + 0 +ENDSEC + 0 +SECTION + 2 +TABLES + 0 +TABLE + 2 +VPORT + 70 +1 + 0 +VPORT + 2 +*ACTIVE + 70 +0 + 10 +0 + 20 +0 + 11 +1 + 21 +1 + 12 +100 + 22 +50 + 13 +0 + 23 +0 + 14 +1 + 24 +1 + 15 +10 + 25 +10 + 16 +0 + 26 +0 + 36 +1 + 17 +0 + 27 +0 + 37 +0 + 40 +146.666667 + 41 +1.5 + 42 +50 + 43 +0 + 44 +0 + 50 +0 + 51 +0 + 71 +0 + 72 +1000 + 73 +1 + 74 +3 + 75 +0 + 76 +0 + 77 +0 + 78 +0 + 0 +ENDTAB + 0 +TABLE + 2 +LTYPE + 70 +3 + 0 +LTYPE + 2 +ByBlock + 70 +0 + 3 + + 72 +65 + 73 +0 + 40 +0 + 0 +LTYPE + 2 +ByLayer + 70 +0 + 3 + + 72 +65 + 73 +0 + 40 +0 + 0 +LTYPE + 2 +Continuous + 70 +0 + 3 +Solid line + 72 +65 + 73 +0 + 40 +0 + 0 +ENDTAB + 0 +TABLE + 2 +LAYER + 70 +2 + 0 +LAYER + 2 +0 + 70 +0 + 62 +7 + 6 +Continuous + 0 +LAYER + 2 +Layer1 + 70 +0 + 62 +1 + 6 +Continuous + 0 +ENDTAB + 0 +TABLE + 2 +STYLE + 70 +1 + 0 +STYLE + 2 +Standard + 70 +0 + 40 +0 + 41 +1 + 50 +0 + 71 +0 + 42 +2.5 + 3 +txt + 4 + + 0 +ENDTAB + 0 +ENDSEC + 0 +SECTION + 2 +ENTITIES + 0 +LINE + 8 +Layer1 + 62 +1 + 10 +0 + 20 +0 + 30 +0 + 11 +10 + 21 +20 + 31 +0 + 0 +ENDSEC + 0 +EOF diff --git a/crates/dxf/tests/data/drawing_line_r2000.dxf b/crates/dxf/tests/data/drawing_line_r2000.dxf new file mode 100644 index 000000000..173a9b427 --- /dev/null +++ b/crates/dxf/tests/data/drawing_line_r2000.dxf @@ -0,0 +1,982 @@ + 0 +SECTION + 2 +HEADER + 9 +$ACADVER + 1 +AC1015 + 9 +$DWGCODEPAGE + 3 +ANSI_1252 + 9 +$INSBASE + 10 +0 + 20 +0 + 30 +0 + 9 +$EXTMIN + 10 +0 + 20 +0 + 30 +0 + 9 +$EXTMAX + 10 +200 + 20 +100 + 30 +0 + 9 +$LIMMIN + 10 +0 + 20 +0 + 9 +$LIMMAX + 10 +200 + 20 +100 + 9 +$LWDISPLAY +290 +1 + 9 +$INSUNITS + 70 +4 + 9 +$MEASUREMENT + 70 +1 + 9 +$HANDSEED + 5 +37 + 0 +ENDSEC + 0 +SECTION + 2 +CLASSES + 0 +CLASS + 1 +ACDBDICTIONARYWDFLT + 2 +AcDbDictionaryWithDefault + 3 +ObjectDBX Classes + 90 +0 +280 +0 +281 +0 + 0 +CLASS + 1 +ACDBPLACEHOLDER + 2 +AcDbPlaceHolder + 3 +ObjectDBX Classes + 90 +0 +280 +0 +281 +0 + 0 +CLASS + 1 +LAYOUT + 2 +AcDbLayout + 3 +ObjectDBX Classes + 90 +0 +280 +0 +281 +0 + 0 +ENDSEC + 0 +SECTION + 2 +TABLES + 0 +TABLE + 2 +VPORT + 5 +8 +330 +0 +100 +AcDbSymbolTable + 70 +1 + 0 +VPORT + 5 +15 +330 +8 +100 +AcDbSymbolTableRecord +100 +AcDbViewportTableRecord + 2 +*Active + 70 +0 + 10 +0 + 20 +0 + 11 +1 + 21 +1 + 12 +100 + 22 +50 + 13 +0 + 23 +0 + 14 +1 + 24 +1 + 15 +10 + 25 +10 + 16 +0 + 26 +0 + 36 +1 + 17 +0 + 27 +0 + 37 +0 + 40 +146.666667 + 41 +1.5 + 42 +50 + 43 +0 + 44 +0 + 50 +0 + 51 +0 + 71 +0 + 72 +1000 + 73 +1 + 74 +3 + 75 +0 + 76 +0 + 77 +0 + 78 +0 +281 +0 + 65 +0 +146 +0 + 0 +ENDTAB + 0 +TABLE + 2 +LTYPE + 5 +2 +330 +0 +100 +AcDbSymbolTable + 70 +3 + 0 +LTYPE + 5 +31 +330 +2 +100 +AcDbSymbolTableRecord +100 +AcDbLinetypeTableRecord + 2 +ByBlock + 70 +0 + 3 + + 72 +65 + 73 +0 + 40 +0 + 0 +LTYPE + 5 +32 +330 +2 +100 +AcDbSymbolTableRecord +100 +AcDbLinetypeTableRecord + 2 +ByLayer + 70 +0 + 3 + + 72 +65 + 73 +0 + 40 +0 + 0 +LTYPE + 5 +33 +330 +2 +100 +AcDbSymbolTableRecord +100 +AcDbLinetypeTableRecord + 2 +Continuous + 70 +0 + 3 +Solid line + 72 +65 + 73 +0 + 40 +0 + 0 +ENDTAB + 0 +TABLE + 2 +LAYER + 5 +1 +330 +0 +100 +AcDbSymbolTable + 70 +2 + 0 +LAYER + 5 +34 +330 +1 +100 +AcDbSymbolTableRecord +100 +AcDbLayerTableRecord + 2 +0 + 70 +0 + 62 +7 + 6 +Continuous +290 +1 +370 +-3 +390 +1B + 0 +LAYER + 5 +35 +330 +1 +100 +AcDbSymbolTableRecord +100 +AcDbLayerTableRecord + 2 +Layer1 + 70 +0 + 62 +1 + 6 +Continuous +290 +1 +370 +-3 +390 +1B + 0 +ENDTAB + 0 +TABLE + 2 +STYLE + 5 +5 +330 +0 +100 +AcDbSymbolTable + 70 +1 + 0 +STYLE + 5 +36 +330 +5 +100 +AcDbSymbolTableRecord +100 +AcDbTextStyleTableRecord + 2 +Standard + 70 +0 + 40 +0 + 41 +1 + 50 +0 + 71 +0 + 42 +2.5 + 3 +txt + 4 + + 0 +ENDTAB + 0 +TABLE + 2 +VIEW + 5 +7 +330 +0 +100 +AcDbSymbolTable + 70 +0 + 0 +ENDTAB + 0 +TABLE + 2 +UCS + 5 +6 +330 +0 +100 +AcDbSymbolTable + 70 +0 + 0 +ENDTAB + 0 +TABLE + 2 +APPID + 5 +3 +330 +0 +100 +AcDbSymbolTable + 70 +1 + 0 +APPID + 5 +16 +330 +3 +100 +AcDbSymbolTableRecord +100 +AcDbRegAppTableRecord + 2 +ACAD + 70 +0 + 0 +ENDTAB + 0 +TABLE + 2 +DIMSTYLE + 5 +4 +330 +0 +100 +AcDbSymbolTable + 70 +1 +100 +AcDbDimStyleTable + 0 +DIMSTYLE +105 +17 +330 +4 +100 +AcDbSymbolTableRecord +100 +AcDbDimStyleTableRecord + 2 +Standard + 70 +0 + 0 +ENDTAB + 0 +TABLE + 2 +BLOCK_RECORD + 5 +9 +330 +0 +100 +AcDbSymbolTable + 70 +2 + 0 +BLOCK_RECORD + 5 +D +330 +9 +100 +AcDbSymbolTableRecord +100 +AcDbBlockTableRecord + 2 +*Model_Space +340 +13 + 70 +0 +280 +1 +281 +0 + 0 +BLOCK_RECORD + 5 +E +330 +9 +100 +AcDbSymbolTableRecord +100 +AcDbBlockTableRecord + 2 +*Paper_Space +340 +14 + 70 +0 +280 +1 +281 +0 + 0 +ENDTAB + 0 +ENDSEC + 0 +SECTION + 2 +BLOCKS + 0 +BLOCK + 5 +F +330 +D +100 +AcDbEntity + 8 +0 +100 +AcDbBlockBegin + 2 +*Model_Space + 70 +0 + 10 +0 + 20 +0 + 30 +0 + 3 +*Model_Space + 1 + + 0 +ENDBLK + 5 +10 +330 +D +100 +AcDbEntity + 8 +0 +100 +AcDbBlockEnd + 0 +BLOCK + 5 +11 +330 +E +100 +AcDbEntity + 8 +0 +100 +AcDbBlockBegin + 2 +*Paper_Space + 70 +0 + 10 +0 + 20 +0 + 30 +0 + 3 +*Paper_Space + 1 + + 0 +ENDBLK + 5 +12 +330 +E +100 +AcDbEntity + 8 +0 +100 +AcDbBlockEnd + 0 +ENDSEC + 0 +SECTION + 2 +ENTITIES + 0 +LINE + 5 +30 +330 +D +100 +AcDbEntity + 8 +Layer1 + 62 +1 +370 +25 +100 +AcDbLine + 10 +0 + 20 +0 + 30 +0 + 11 +10 + 21 +20 + 31 +0 + 0 +ENDSEC + 0 +SECTION + 2 +OBJECTS + 0 +DICTIONARY + 5 +A +330 +0 +100 +AcDbDictionary +281 +1 + 3 +ACAD_GROUP +350 +B + 3 +ACAD_LAYOUT +350 +C + 3 +ACAD_PLOTSTYLENAME +350 +1A + 0 +DICTIONARY + 5 +B +330 +A +100 +AcDbDictionary +281 +1 + 0 +DICTIONARY + 5 +C +330 +A +100 +AcDbDictionary +281 +1 + 3 +Layout1 +350 +14 + 3 +Model +350 +13 + 0 +ACDBDICTIONARYWDFLT + 5 +1A +330 +A +100 +AcDbDictionary +281 +1 + 3 +Normal +350 +1B +100 +AcDbDictionaryWithDefault +340 +1B + 0 +ACDBPLACEHOLDER + 5 +1B +330 +1A + 0 +LAYOUT + 5 +13 +330 +C +100 +AcDbPlotSettings + 1 + + 2 +none_device + 4 + + 6 + + 40 +0 + 41 +0 + 42 +0 + 43 +0 + 44 +0 + 45 +0 + 46 +0 + 47 +0 + 48 +0 + 49 +0 +140 +0 +141 +0 +142 +1 +143 +1 + 70 +1712 + 72 +1 + 73 +0 + 74 +5 + 7 + + 75 +16 +147 +1 +148 +0 +149 +0 +100 +AcDbLayout + 1 +Model + 70 +1 + 71 +0 + 10 +0 + 20 +0 + 11 +200 + 21 +100 + 12 +0 + 22 +0 + 32 +0 + 14 +0 + 24 +0 + 34 +0 + 15 +200 + 25 +100 + 35 +0 +146 +0 + 13 +0 + 23 +0 + 33 +0 + 16 +1 + 26 +0 + 36 +0 + 17 +0 + 27 +1 + 37 +0 + 76 +1 +330 +D + 0 +LAYOUT + 5 +14 +330 +C +100 +AcDbPlotSettings + 1 + + 2 +none_device + 4 + + 6 + + 40 +0 + 41 +0 + 42 +0 + 43 +0 + 44 +0 + 45 +0 + 46 +0 + 47 +0 + 48 +0 + 49 +0 +140 +0 +141 +0 +142 +1 +143 +1 + 70 +688 + 72 +1 + 73 +0 + 74 +5 + 7 + + 75 +16 +147 +1 +148 +0 +149 +0 +100 +AcDbLayout + 1 +Layout1 + 70 +1 + 71 +1 + 10 +0 + 20 +0 + 11 +200 + 21 +100 + 12 +0 + 22 +0 + 32 +0 + 14 +0 + 24 +0 + 34 +0 + 15 +200 + 25 +100 + 35 +0 +146 +0 + 13 +0 + 23 +0 + 33 +0 + 16 +1 + 26 +0 + 36 +0 + 17 +0 + 27 +1 + 37 +0 + 76 +1 +330 +E + 0 +ENDSEC + 0 +EOF diff --git a/crates/dxf/tests/data/drawing_line_r2018.dxf b/crates/dxf/tests/data/drawing_line_r2018.dxf new file mode 100644 index 000000000..8318c1c5e --- /dev/null +++ b/crates/dxf/tests/data/drawing_line_r2018.dxf @@ -0,0 +1,1002 @@ + 0 +SECTION + 2 +HEADER + 9 +$ACADVER + 1 +AC1032 + 9 +$DWGCODEPAGE + 3 +ANSI_1252 + 9 +$INSBASE + 10 +0 + 20 +0 + 30 +0 + 9 +$EXTMIN + 10 +0 + 20 +0 + 30 +0 + 9 +$EXTMAX + 10 +200 + 20 +100 + 30 +0 + 9 +$LIMMIN + 10 +0 + 20 +0 + 9 +$LIMMAX + 10 +200 + 20 +100 + 9 +$LWDISPLAY +290 +1 + 9 +$INSUNITS + 70 +4 + 9 +$MEASUREMENT + 70 +1 + 9 +$HANDSEED + 5 +37 + 0 +ENDSEC + 0 +SECTION + 2 +CLASSES + 0 +CLASS + 1 +ACDBDICTIONARYWDFLT + 2 +AcDbDictionaryWithDefault + 3 +ObjectDBX Classes + 90 +0 + 91 +0 +280 +0 +281 +0 + 0 +CLASS + 1 +ACDBPLACEHOLDER + 2 +AcDbPlaceHolder + 3 +ObjectDBX Classes + 90 +0 + 91 +0 +280 +0 +281 +0 + 0 +CLASS + 1 +LAYOUT + 2 +AcDbLayout + 3 +ObjectDBX Classes + 90 +0 + 91 +0 +280 +0 +281 +0 + 0 +ENDSEC + 0 +SECTION + 2 +TABLES + 0 +TABLE + 2 +VPORT + 5 +8 +330 +0 +100 +AcDbSymbolTable + 70 +1 + 0 +VPORT + 5 +15 +330 +8 +100 +AcDbSymbolTableRecord +100 +AcDbViewportTableRecord + 2 +*Active + 70 +0 + 10 +0 + 20 +0 + 11 +1 + 21 +1 + 12 +100 + 22 +50 + 13 +0 + 23 +0 + 14 +1 + 24 +1 + 15 +10 + 25 +10 + 16 +0 + 26 +0 + 36 +1 + 17 +0 + 27 +0 + 37 +0 + 40 +146.666667 + 41 +1.5 + 42 +50 + 43 +0 + 44 +0 + 50 +0 + 51 +0 + 71 +0 + 72 +1000 + 73 +1 + 74 +3 + 75 +0 + 76 +0 + 77 +0 + 78 +0 +281 +0 + 65 +0 +146 +0 + 0 +ENDTAB + 0 +TABLE + 2 +LTYPE + 5 +2 +330 +0 +100 +AcDbSymbolTable + 70 +3 + 0 +LTYPE + 5 +31 +330 +2 +100 +AcDbSymbolTableRecord +100 +AcDbLinetypeTableRecord + 2 +ByBlock + 70 +0 + 3 + + 72 +65 + 73 +0 + 40 +0 + 0 +LTYPE + 5 +32 +330 +2 +100 +AcDbSymbolTableRecord +100 +AcDbLinetypeTableRecord + 2 +ByLayer + 70 +0 + 3 + + 72 +65 + 73 +0 + 40 +0 + 0 +LTYPE + 5 +33 +330 +2 +100 +AcDbSymbolTableRecord +100 +AcDbLinetypeTableRecord + 2 +Continuous + 70 +0 + 3 +Solid line + 72 +65 + 73 +0 + 40 +0 + 0 +ENDTAB + 0 +TABLE + 2 +LAYER + 5 +1 +330 +0 +100 +AcDbSymbolTable + 70 +2 + 0 +LAYER + 5 +34 +330 +1 +100 +AcDbSymbolTableRecord +100 +AcDbLayerTableRecord + 2 +0 + 70 +0 + 62 +7 + 6 +Continuous +290 +1 +370 +-3 +390 +1B + 0 +LAYER + 5 +35 +330 +1 +100 +AcDbSymbolTableRecord +100 +AcDbLayerTableRecord + 2 +Layer1 + 70 +0 + 62 +1 + 6 +Continuous +290 +1 +370 +-3 +390 +1B + 0 +ENDTAB + 0 +TABLE + 2 +STYLE + 5 +5 +330 +0 +100 +AcDbSymbolTable + 70 +1 + 0 +STYLE + 5 +36 +330 +5 +100 +AcDbSymbolTableRecord +100 +AcDbTextStyleTableRecord + 2 +Standard + 70 +0 + 40 +0 + 41 +1 + 50 +0 + 71 +0 + 42 +2.5 + 3 +txt + 4 + + 0 +ENDTAB + 0 +TABLE + 2 +VIEW + 5 +7 +330 +0 +100 +AcDbSymbolTable + 70 +0 + 0 +ENDTAB + 0 +TABLE + 2 +UCS + 5 +6 +330 +0 +100 +AcDbSymbolTable + 70 +0 + 0 +ENDTAB + 0 +TABLE + 2 +APPID + 5 +3 +330 +0 +100 +AcDbSymbolTable + 70 +1 + 0 +APPID + 5 +16 +330 +3 +100 +AcDbSymbolTableRecord +100 +AcDbRegAppTableRecord + 2 +ACAD + 70 +0 + 0 +ENDTAB + 0 +TABLE + 2 +DIMSTYLE + 5 +4 +330 +0 +100 +AcDbSymbolTable + 70 +1 +100 +AcDbDimStyleTable + 0 +DIMSTYLE +105 +17 +330 +4 +100 +AcDbSymbolTableRecord +100 +AcDbDimStyleTableRecord + 2 +Standard + 70 +0 + 0 +ENDTAB + 0 +TABLE + 2 +BLOCK_RECORD + 5 +9 +330 +0 +100 +AcDbSymbolTable + 70 +2 + 0 +BLOCK_RECORD + 5 +D +330 +9 +100 +AcDbSymbolTableRecord +100 +AcDbBlockTableRecord + 2 +*Model_Space +340 +13 + 70 +0 +280 +1 +281 +0 + 0 +BLOCK_RECORD + 5 +E +330 +9 +100 +AcDbSymbolTableRecord +100 +AcDbBlockTableRecord + 2 +*Paper_Space +340 +14 + 70 +0 +280 +1 +281 +0 + 0 +ENDTAB + 0 +ENDSEC + 0 +SECTION + 2 +BLOCKS + 0 +BLOCK + 5 +F +330 +D +100 +AcDbEntity + 8 +0 +100 +AcDbBlockBegin + 2 +*Model_Space + 70 +0 + 10 +0 + 20 +0 + 30 +0 + 3 +*Model_Space + 1 + + 0 +ENDBLK + 5 +10 +330 +D +100 +AcDbEntity + 8 +0 +100 +AcDbBlockEnd + 0 +BLOCK + 5 +11 +330 +E +100 +AcDbEntity + 8 +0 +100 +AcDbBlockBegin + 2 +*Paper_Space + 70 +0 + 10 +0 + 20 +0 + 30 +0 + 3 +*Paper_Space + 1 + + 0 +ENDBLK + 5 +12 +330 +E +100 +AcDbEntity + 8 +0 +100 +AcDbBlockEnd + 0 +ENDSEC + 0 +SECTION + 2 +ENTITIES + 0 +LINE + 5 +30 +330 +D +100 +AcDbEntity + 8 +Layer1 + 62 +1 +370 +25 +420 +16711680 +100 +AcDbLine + 10 +0 + 20 +0 + 30 +0 + 11 +10 + 21 +20 + 31 +0 + 0 +ENDSEC + 0 +SECTION + 2 +OBJECTS + 0 +DICTIONARY + 5 +A +330 +0 +100 +AcDbDictionary +281 +1 + 3 +ACAD_GROUP +350 +B + 3 +ACAD_LAYOUT +350 +C + 3 +ACAD_PLOTSTYLENAME +350 +1A + 0 +DICTIONARY + 5 +B +330 +A +100 +AcDbDictionary +281 +1 + 0 +DICTIONARY + 5 +C +330 +A +100 +AcDbDictionary +281 +1 + 3 +Layout1 +350 +14 + 3 +Model +350 +13 + 0 +ACDBDICTIONARYWDFLT + 5 +1A +330 +A +100 +AcDbDictionary +281 +1 + 3 +Normal +350 +1B +100 +AcDbDictionaryWithDefault +340 +1B + 0 +ACDBPLACEHOLDER + 5 +1B +330 +1A + 0 +LAYOUT + 5 +13 +330 +C +100 +AcDbPlotSettings + 1 + + 2 +none_device + 4 + + 6 + + 40 +0 + 41 +0 + 42 +0 + 43 +0 + 44 +0 + 45 +0 + 46 +0 + 47 +0 + 48 +0 + 49 +0 +140 +0 +141 +0 +142 +1 +143 +1 + 70 +1712 + 72 +1 + 73 +0 + 74 +5 + 7 + + 75 +16 + 76 +0 + 77 +2 + 78 +300 +147 +1 +148 +0 +149 +0 +100 +AcDbLayout + 1 +Model + 70 +1 + 71 +0 + 10 +0 + 20 +0 + 11 +200 + 21 +100 + 12 +0 + 22 +0 + 32 +0 + 14 +0 + 24 +0 + 34 +0 + 15 +200 + 25 +100 + 35 +0 +146 +0 + 13 +0 + 23 +0 + 33 +0 + 16 +1 + 26 +0 + 36 +0 + 17 +0 + 27 +1 + 37 +0 + 76 +1 +330 +D + 0 +LAYOUT + 5 +14 +330 +C +100 +AcDbPlotSettings + 1 + + 2 +none_device + 4 + + 6 + + 40 +0 + 41 +0 + 42 +0 + 43 +0 + 44 +0 + 45 +0 + 46 +0 + 47 +0 + 48 +0 + 49 +0 +140 +0 +141 +0 +142 +1 +143 +1 + 70 +688 + 72 +1 + 73 +0 + 74 +5 + 7 + + 75 +16 + 76 +0 + 77 +2 + 78 +300 +147 +1 +148 +0 +149 +0 +100 +AcDbLayout + 1 +Layout1 + 70 +1 + 71 +1 + 10 +0 + 20 +0 + 11 +200 + 21 +100 + 12 +0 + 22 +0 + 32 +0 + 14 +0 + 24 +0 + 34 +0 + 15 +200 + 25 +100 + 35 +0 +146 +0 + 13 +0 + 23 +0 + 33 +0 + 16 +1 + 26 +0 + 36 +0 + 17 +0 + 27 +1 + 37 +0 + 76 +1 +330 +E + 0 +ENDSEC + 0 +EOF From 1fdb3934b5a8154828dd1646289f84dbd1302b53 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:09:31 +0000 Subject: [PATCH 101/150] ps: check selected glyphs before text labeling --- crates/ps/src/read/font.rs | 66 ++++++++++++++++++++----------------- crates/ps/src/read/tests.rs | 10 ++++++ 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/crates/ps/src/read/font.rs b/crates/ps/src/read/font.rs index f7b0e5ced..510f27863 100644 --- a/crates/ps/src/read/font.rs +++ b/crates/ps/src/read/font.rs @@ -429,7 +429,7 @@ type PerGlyph<'a> = dyn FnMut(&mut Interp, u32, usize) -> R<(f64, f64)> + 'a; /// Show (or, with `draw` false, measure) a string: the total advance in user space. /// `per_char` gives each glyph's extra displacement (or, with `replace`, its whole advance). -fn show_glyphs(ip: &mut Interp, s: &[u8], draw: bool, replace: bool, per_char: &mut PerGlyph<'_>) -> R<(f64, f64)> { +fn show_glyphs(ip: &mut Interp, s: &[u8], draw: bool, replace: bool, per_char: &mut PerGlyph<'_>, text: Option<&str>) -> R<(f64, f64)> { let font = match ip.gs.font.clone() { Some(f) => f, None => return err("invalidfont"), @@ -443,6 +443,12 @@ fn show_glyphs(ip: &mut Interp, s: &[u8], draw: bool, replace: bool, per_char: & let fm = font_matrix(&font); s.iter().map(|c| (u32::from(*c), font.clone(), fm, crate::read::composite::Key2::Code(*c))).collect() }; + // Glyph procedures can issue arbitrary paint and clip operations. The selected jobs, not + // the composite font's family, determine whether this show can have a synthetic wrapper. + let marked = text.filter(|_| !jobs.iter().any(|(_, leaf, _, _)| leaf.get_name(b"FontType").and_then(|v| v.num()) == Some(3.0))); + if let Some(text) = marked { + ip.out.begin_actual_text(text, &ip.gs.clip).map_err(PsErr::Fatal)?; + } let mut total = (0.0, 0.0); let mut text_paths: Vec<(Vec, crate::read::gstate::DevColor)> = Vec::new(); let mut text_segments = 0usize; @@ -547,11 +553,18 @@ fn show_glyphs(ip: &mut Interp, s: &[u8], draw: bool, replace: bool, per_char: & Ok(total) })(); let path_limit = matches!(&result, Err(PsErr::Fatal(message)) if message == "PostScript (path limit)"); - if (result.is_ok() || !path_limit) && !ip.gs.nulldev { - for (path, color) in text_paths { - ip.out.fill(&ip.gs.clip, &path, false, color).map_err(PsErr::Fatal)?; + let paint_result = (|| { + if (result.is_ok() || !path_limit) && !ip.gs.nulldev { + for (path, color) in text_paths { + ip.out.fill(&ip.gs.clip, &path, false, color).map_err(PsErr::Fatal)?; + } } + Ok(()) + })(); + if marked.is_some() { + ip.out.end_actual_text(); } + paint_result?; result } @@ -567,25 +580,9 @@ fn key_code(code: u32) -> Option { fn show_string(ip: &mut Interp, s: &[u8], draw: bool, per_char: &mut PerGlyph<'_>) -> R<(f64, f64)> { // Whitespace-only shows advance the text position but emit no painted glyphs; avoid bloating // the content stream with an empty semantic marked-content wrapper for them. - // Type 3 glyph programs can paint and change clipping inside a show. Composite fonts may - // select Type 3 descendants. Do not wrap their arbitrary output in a synthetic text span. - let text = ip - .gs - .font - .as_ref() - .filter(|font| !matches!(font.get_name(b"FontType").and_then(|v| v.num()), Some(0.0 | 3.0))) - .and_then(|font| actual_text(font, s)); - if draw - && !ip.charpath - && let Some(text) = text.filter(|text| text.chars().any(|c| !c.is_whitespace())) - { - ip.out.begin_actual_text(&text, &ip.gs.clip).map_err(PsErr::Fatal)?; - let result = show_glyphs(ip, s, draw, false, per_char); - ip.out.end_actual_text(); - result - } else { - show_glyphs(ip, s, draw, false, per_char) - } + let text = ip.gs.font.as_ref().and_then(|font| actual_text(font, s)); + let text = text.filter(|text| draw && !ip.charpath && text.chars().any(|c| !c.is_whitespace())); + show_glyphs(ip, s, draw, false, per_char, text.as_deref()) } pub fn show(ip: &mut Interp) -> R { @@ -634,14 +631,21 @@ fn explicit_show(ip: &mut Interp, xs: bool, ys: bool) -> R { return err("nocurrentpoint"); } let step = if xs && ys { 2 } else { 1 }; - show_glyphs(ip, &s, true, true, &mut |_, _, i| { - let d = |k: usize| disp.get(i * step + k).copied().unwrap_or(0.0); - Ok(match (xs, ys) { - (true, true) => (d(0), d(1)), - (true, false) => (d(0), 0.0), - _ => (0.0, d(0)), - }) - }) + show_glyphs( + ip, + &s, + true, + true, + &mut |_, _, i| { + let d = |k: usize| disp.get(i * step + k).copied().unwrap_or(0.0); + Ok(match (xs, ys) { + (true, true) => (d(0), d(1)), + (true, false) => (d(0), 0.0), + _ => (0.0, d(0)), + }) + }, + None, + ) .map(|_| ()) } diff --git a/crates/ps/src/read/tests.rs b/crates/ps/src/read/tests.rs index 99c934158..c694a7177 100644 --- a/crates/ps/src/read/tests.rs +++ b/crates/ps/src/read/tests.rs @@ -472,6 +472,16 @@ fn actual_text_uses_the_selected_encoding_and_omits_unknown_names() { assert!(!content.contains("/ActualText"), "{content}"); } +#[test] +fn composite_show_only_omits_text_for_selected_type3_glyphs() { + let setup = "/T3 8 dict dup begin /FontType 3 def /FontMatrix [0.001 0 0 0.001 0 0] def /FontBBox [0 0 1000 1000] def /Encoding 256 array def 0 1 255 { Encoding exch /.notdef put } for Encoding 65 /A put /BuildChar { pop pop 600 0 0 0 500 500 setcachedevice 0 0 500 500 rectfill } def end def /T3 T3 definefont pop /T0 8 dict dup begin /FontType 0 def /FMapType 2 def /FontMatrix [1 0 0 1 0 0] def /Encoding 256 array def 0 1 255 { Encoding exch /A put } for /FDepVector [ /Helvetica findfont /T3 findfont ] def end /T0 exch definefont pop /T0 findfont 20 scalefont setfont 10 20 moveto "; + let (standard, _) = run(&format!("{setup} <0041> show")); + assert!(standard.contains("/ActualText (AA)"), "{standard}"); + let (mixed, _) = run(&format!("{setup} <00410141> show")); + assert!(!mixed.contains("/ActualText"), "{mixed}"); + assert!(mixed.lines().any(|line| line == "f"), "the Type 3 glyph is still painted: {mixed}"); +} + #[test] fn actual_text_starts_after_persistent_clip() { let (content, _) = run( From f44195aa11d9778ce8f567346d5b6fb848d243f7 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:12:18 +0000 Subject: [PATCH 102/150] Reject damaged later SVGZ members --- crates/svg/src/svgz.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/svg/src/svgz.rs b/crates/svg/src/svgz.rs index de5232612..4cb466111 100644 --- a/crates/svg/src/svgz.rs +++ b/crates/svg/src/svgz.rs @@ -44,13 +44,15 @@ pub fn inflate(bytes: &[u8], limit: usize) -> Result, Error> { let kept = out.len(); let room = u64::try_from(limit.saturating_sub(kept)).unwrap_or(u64::MAX).saturating_add(1); let mut member = flate2::bufread::GzDecoder::new(rest); + // A valid header identifies a member even if its body or trailer is damaged. + let recognized = member.header().is_some(); let read = member.by_ref().take(room).read_to_end(&mut out); if out.len() > limit { return Err(Error::TooLarge(limit)); } match read { Ok(_) => {} - Err(e) if first => return Err(Error::Corrupt(e.to_string())), + Err(e) if first || recognized => return Err(Error::Corrupt(e.to_string())), // Trailing bytes that only start like a member: not part of the file. Err(_) => { out.truncate(kept); @@ -161,6 +163,7 @@ mod tests { let mut two = compress(a.as_bytes()); two.extend(compress(b.as_bytes())); assert_eq!(text(&two, MB).as_deref(), Ok(SVG)); + assert_eq!(text(&two, SVG.len()).as_deref(), Ok(SVG)); // The limit counts every member. assert_eq!(inflate(&two, SVG.len() - 1), Err(Error::TooLarge(SVG.len() - 1))); let mut padded = compress(SVG.as_bytes()); @@ -172,6 +175,26 @@ mod tests { assert_eq!(text(&junk, MB).as_deref(), Ok(SVG)); } + #[test] + fn damaged_later_members_are_errors() { + let mut first = compress(b""); + let second = compress(b""); + + let mut bad_crc = second.clone(); + let crc = bad_crc.len() - 8; + bad_crc[crc] ^= 1; + first.extend_from_slice(&bad_crc); + assert!(matches!(inflate(&first, MB), Err(Error::Corrupt(_)))); + assert!(matches!(inflate(&first, b"".len()), Err(Error::Corrupt(_)))); + + let mut truncated = compress(b""); + truncated.extend_from_slice(&second[..second.len() - 4]); + assert!(matches!(inflate(&truncated, MB), Err(Error::Corrupt(_)))); + let mut truncated_body = compress(b""); + truncated_body.extend_from_slice(&second[..10]); + assert!(matches!(inflate(&truncated_body, MB), Err(Error::Corrupt(_)))); + } + #[test] fn damaged_and_nested_streams_are_errors() { let z = compress(SVG.as_bytes()); From e9c818bcfe03fefaaf08520baf0f8c607840e247 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:12:23 +0000 Subject: [PATCH 103/150] jpeg: share decoded sample RGBA conversion --- crates/jpeg/src/lib.rs | 3 +++ crates/jpeg/src/rgba.rs | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 crates/jpeg/src/rgba.rs diff --git a/crates/jpeg/src/lib.rs b/crates/jpeg/src/lib.rs index 8d4ff1ca8..23434fdd0 100644 --- a/crates/jpeg/src/lib.rs +++ b/crates/jpeg/src/lib.rs @@ -1709,3 +1709,6 @@ pub use encode::{ChromaSubsampling, EncodeError, encode_baseline}; mod app14; pub use app14::adobe_transform; + +mod rgba; +pub use rgba::{UnsupportedComponents, to_rgba8}; diff --git a/crates/jpeg/src/rgba.rs b/crates/jpeg/src/rgba.rs new file mode 100644 index 000000000..495c5141f --- /dev/null +++ b/crates/jpeg/src/rgba.rs @@ -0,0 +1,44 @@ +//! Conversion of decoded 8-bit JPEG samples to opaque RGBA. + +/// The JPEG component count is not supported by the RGBA conversion. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct UnsupportedComponents(pub u8); + +/// Convert grayscale, RGB or CMYK samples to opaque RGBA, ignoring incomplete trailing pixels. +pub fn to_rgba8(samples: &[u8], components: u8) -> Result, UnsupportedComponents> { + let mut rgba = Vec::with_capacity(samples.len().saturating_mul(4)); + match components { + 1 => { + for &gray in samples { + rgba.extend_from_slice(&[gray, gray, gray, 255]); + } + } + 3 => { + for rgb in samples.as_chunks::<3>().0 { + rgba.extend_from_slice(&[rgb[0], rgb[1], rgb[2], 255]); + } + } + 4 => { + for cmyk in samples.as_chunks::<4>().0 { + let (c, m, y, k) = (u16::from(cmyk[0]), u16::from(cmyk[1]), u16::from(cmyk[2]), u16::from(cmyk[3])); + let rgb = [((255 - c) * (255 - k) / 255) as u8, ((255 - m) * (255 - k) / 255) as u8, ((255 - y) * (255 - k) / 255) as u8]; + rgba.extend_from_slice(&[rgb[0], rgb[1], rgb[2], 255]); + } + } + n => return Err(UnsupportedComponents(n)), + } + Ok(rgba) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn gray_rgb_and_cmyk_conversion_matches_integer_rules() -> Result<(), UnsupportedComponents> { + assert_eq!(to_rgba8(&[12], 1)?, [12, 12, 12, 255]); + assert_eq!(to_rgba8(&[1, 2, 3, 4], 3)?, [1, 2, 3, 255]); + assert_eq!(to_rgba8(&[0, 255, 128, 0], 4)?, [255, 0, 127, 255]); + assert_eq!(to_rgba8(&[], 2), Err(UnsupportedComponents(2))); + Ok(()) + } +} From 6d590ce432d9ae725590df72428aeb47256a01fd Mon Sep 17 00:00:00 2001 From: Bram Heerink <1698441+bramheerink@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:17:03 -0500 Subject: [PATCH 104/150] Extract PSD guide resource decoding for shared consumers Adapt PhotoCraft f49564d281bf8892db58ed154767717223aff4f6 to a validated allocation-free iterator; retain caller-owned guide policy. --- crates/psd/src/guides.rs | 103 +++++++++++++++++++++++++++++++++++++++ crates/psd/src/lib.rs | 1 + 2 files changed, 104 insertions(+) create mode 100644 crates/psd/src/guides.rs diff --git a/crates/psd/src/guides.rs b/crates/psd/src/guides.rs new file mode 100644 index 000000000..5f100c480 --- /dev/null +++ b/crates/psd/src/guides.rs @@ -0,0 +1,103 @@ +//! PSD Grid and Guides version 2 (resource 1092) record decoding. +//! +//! Extracted from PhotoCraft `guides_map.rs` at f49564d281bf8892db58ed154767717223aff4f6 +//! by Bram Heerink. Document colour preference and resource-retention policy belong to callers. +//! Unknown direction values retain PhotoCraft's vertical interpretation. Grid cycles, reserved +//! bytes and trailing data are not interpreted. Resource 1097 remains opaque except its count. + +/// Grid and Guides version 2 resource ID. +pub const GUIDES_V2: u16 = 1092; +/// Associated guide identifiers resource ID. +pub const GUIDE_IDS: u16 = 1097; + +/// One version 2 guide record. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct GuideV2 { + /// Signed position in units of 1/32 pixel. + pub location: i32, + /// True for vertical guides (direction byte other than 1). + pub vertical: bool, + /// Stored RGB colour; interpretation of preference colours is app policy. + pub rgb: [u8; 3], +} + +fn be_u32(data: &[u8], at: usize) -> Option { + let b = data.get(at..at.checked_add(4)?)?; + Some(u32::from_be_bytes([b[0], b[1], b[2], b[3]])) +} + +/// Validate the full declared record region before returning an allocation-free iterator. +/// Returns `None` for an unsupported version, short header or truncated records. +/// Record count cannot cause allocation or work beyond the supplied byte slice. +pub fn parse_v2(data: &[u8]) -> Option + '_> { + if be_u32(data, 0)? != 2 { + return None; + } + let count = usize::try_from(be_u32(data, 12)?).ok()?; + let end = 16usize.checked_add(count.checked_mul(9)?)?; + let records = data.get(16..end)?; + Some(records.as_chunks::<9>().0.iter().map(|g| GuideV2 { + location: i32::from_be_bytes([g[0], g[1], g[2], g[3]]), + vertical: g[4] != 1, + rgb: [g[6], g[7], g[8]], + })) +} + +/// Only the declared entry count in resource 1097; this does not validate its GUID payload. +pub fn ids_count(data: &[u8]) -> Option { + be_u32(data, 0) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn fixture() -> Vec { + let mut v = Vec::new(); + for n in [2u32, 576, 576, 2] { + v.extend_from_slice(&n.to_be_bytes()); + } + for (position, direction, rgb) in [(512i32, 0u8, [74, 255, 255]), (-32, 1, [1, 2, 3])] { + v.extend_from_slice(&position.to_be_bytes()); + v.extend_from_slice(&[direction, 0]); + v.extend_from_slice(&rgb); + } + v + } + + #[test] + fn decodes_signed_positions_directions_and_colours() { + let data = fixture(); + let actual = parse_v2(&data).map(Iterator::collect::>); + assert_eq!( + actual, + Some(vec![GuideV2 { location: 512, vertical: true, rgb: [74, 255, 255] }, GuideV2 { location: -32, vertical: false, rgb: [1, 2, 3] },]) + ); + } + + #[test] + fn validates_every_truncation_and_hostile_count_before_iteration() { + let mut data = fixture(); + for n in 0..data.len() { + assert!(parse_v2(&data[..n]).is_none()); + } + data[12..16].copy_from_slice(&u32::MAX.to_be_bytes()); + assert!(parse_v2(&data).is_none()); + data[12..16].copy_from_slice(&0u32.to_be_bytes()); + assert_eq!(parse_v2(&data).map(|v| v.len()), Some(0)); + data[3] = 1; + assert!(parse_v2(&data).is_none()); + } + + #[test] + fn preserves_unknown_direction_reserved_bytes_and_trailing_data_policy() { + let mut data = fixture(); + data[20] = 255; + data[21] = 42; + data.extend_from_slice(&[9; 12]); + assert_eq!(parse_v2(&data).and_then(|mut v| v.next()).map(|g| g.vertical), Some(true)); + assert_eq!(parse_v2(&data).map(|v| v.len()), Some(2)); + assert_eq!(ids_count(&[0, 0, 0, 2, 255]), Some(2)); + assert_eq!(ids_count(&[0, 0, 0]), None); + } +} diff --git a/crates/psd/src/lib.rs b/crates/psd/src/lib.rs index 4c3222f46..27797f1bc 100644 --- a/crates/psd/src/lib.rs +++ b/crates/psd/src/lib.rs @@ -74,6 +74,7 @@ pub mod fixtures; pub mod flat; pub mod float; pub mod grd; +pub mod guides; pub mod hdr; pub mod head; pub mod header; From 689eff839d03313b3d0f694edceadb514287a88d Mon Sep 17 00:00:00 2001 From: Gert Pellin Date: Sun, 15 Nov 2026 13:28:14 -0500 Subject: [PATCH 105/150] Share raw PSD Duotone ink record layout Extract the fixed layout from PhotoCraft 772cf39c09930229e11b920196f486239bbbfab8, incorporating alternate-colour handling from 2d7c62055ee47497f8cb77bbdfd3befa29718f4a. Keep document colour/curve policy out of the codec. Co-authored-by: David Mann --- crates/psd/src/duotone.rs | 159 ++++++++++++++++++++++++++++++++++++++ crates/psd/src/lib.rs | 1 + 2 files changed, 160 insertions(+) create mode 100644 crates/psd/src/duotone.rs diff --git a/crates/psd/src/duotone.rs b/crates/psd/src/duotone.rs new file mode 100644 index 000000000..7b66da951 --- /dev/null +++ b/crates/psd/src/duotone.rs @@ -0,0 +1,159 @@ +//! Raw records in Photoshop's version 1 Duotone Color Mode Data. +//! +//! Layout extracted from PhotoCraft `duotone_map.rs`, originally by Gert Pellin +//! (772cf39c09930229e11b920196f486239bbbfab8), with colour-book integration by David Mann. +//! Colour conversion, curve sampling and fallback/display policy are caller-owned. +//! Parsing accepts the 412-byte record prefix as PhotoCraft did; dot gain and overprints +//! are optional trailing data, not prerequisites for interpreting the inks. + +/// Full block size emitted by Photoshop and PhotoCraft. +pub const BLOCK_LEN: usize = 524; +/// Alternate Duotone Colors image resource. +pub const ALTERNATE_DUOTONE_COLORS: u16 = 1066; +/// Raw colour space and four component words, without colour interpretation. +pub type ColorRecord = [u16; 5]; +/// Sentinel for an absent ink or computed overprint colour. +pub const NO_COLOR: ColorRecord = [0xffff, 0, 0, 0, 0]; + +/// One fixed-size ink slot, including uninterpreted reserved curve word. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct InkRecord { + pub color: ColorRecord, + /// Pascal name bytes, including its length byte; text encoding belongs to the caller. + pub name: [u8; 64], + /// Thirteen signed curve samples and the reserved final word. + pub curve: [i16; 14], +} + +fn word(data: &[u8], at: usize) -> Option { + let bytes = data.get(at..at.checked_add(2)?)?; + Some(u16::from_be_bytes([bytes[0], bytes[1]])) +} + +/// Decode a raw colour record at a byte offset, without guessing unknown colour spaces. +pub fn color_at(data: &[u8], at: usize) -> Option { + let bytes = data.get(at..at.checked_add(10)?)?; + Some(std::array::from_fn(|i| u16::from_be_bytes([bytes[i * 2], bytes[i * 2 + 1]]))) +} + +/// Borrowed, validated view over at most four active ink records. No input-sized allocation. +#[derive(Clone, Copy, Debug)] +pub struct DuotoneView<'a> { + data: &'a [u8], + count: usize, +} + +impl<'a> DuotoneView<'a> { + /// Accept version 1, one to four inks and the full 412-byte record prefix. + pub fn parse(data: &'a [u8]) -> Option { + if data.len() < 412 || word(data, 0)? != 1 { + return None; + } + let count = usize::from(word(data, 2)?); + (1..=4).contains(&count).then_some(Self { data, count }) + } + + pub fn ink_count(self) -> usize { + self.count + } + + /// Copy one small fixed-size record. Inactive/out-of-range slots are not returned. + pub fn ink(self, index: usize) -> Option { + if index >= self.count { + return None; + } + let name_at = 44 + 64 * index; + let name = self.data.get(name_at..name_at + 64)?.try_into().ok()?; + let curve_at = 300 + 28 * index; + let curve_bytes = self.data.get(curve_at..curve_at + 28)?; + Some(InkRecord { + color: color_at(self.data, 4 + 10 * index)?, + name, + curve: std::array::from_fn(|i| i16::from_be_bytes([curve_bytes[i * 2], curve_bytes[i * 2 + 1]])), + }) + } +} + +/// Read one raw alternate colour when the resource version/count permits it. +pub fn alternate_color(data: &[u8], index: usize) -> Option { + if word(data, 0)? != 1 || index >= usize::from(word(data, 2)?) { + return None; + } + color_at(data, 4usize.checked_add(index.checked_mul(10)?)?) +} + +/// Serialize four raw slots and explicit trailing policy into a fixed-size block. +/// `count` must be 1..=4. Reserved bytes and inactive slots are preserved as supplied. +pub fn write_block(count: u16, inks: &[InkRecord; 4], dot_gain: u16, overprints: &[ColorRecord; 11]) -> Option<[u8; BLOCK_LEN]> { + if !(1..=4).contains(&count) { + return None; + } + let mut out = [0u8; BLOCK_LEN]; + out[..2].copy_from_slice(&1u16.to_be_bytes()); + out[2..4].copy_from_slice(&count.to_be_bytes()); + for (i, ink) in inks.iter().enumerate() { + for (j, value) in ink.color.iter().enumerate() { + let at = 4 + 10 * i + 2 * j; + out[at..at + 2].copy_from_slice(&value.to_be_bytes()); + } + out[44 + 64 * i..44 + 64 * (i + 1)].copy_from_slice(&ink.name); + for (j, value) in ink.curve.iter().enumerate() { + let at = 300 + 28 * i + 2 * j; + out[at..at + 2].copy_from_slice(&value.to_be_bytes()); + } + } + out[412..414].copy_from_slice(&dot_gain.to_be_bytes()); + for (i, color) in overprints.iter().enumerate() { + for (j, value) in color.iter().enumerate() { + let at = 414 + 10 * i + 2 * j; + out[at..at + 2].copy_from_slice(&value.to_be_bytes()); + } + } + Some(out) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + + #[test] + fn fixed_layout_roundtrip_preserves_raw_fields() { + let mut ink = InkRecord { color: [7, 1, 2, 3, 65535], name: [0; 64], curve: [-1; 14] }; + ink.name[..4].copy_from_slice(&[3, b'I', b'n', b'k']); + ink.curve[0] = 0; + ink.curve[12] = 1000; + let slots = std::array::from_fn(|_| ink.clone()); + let bytes = write_block(4, &slots, 20, &[NO_COLOR; 11]).unwrap(); + assert_eq!(&bytes[..4], &[0, 1, 0, 4]); + assert_eq!(&bytes[44..48], &[3, b'I', b'n', b'k']); + assert_eq!(&bytes[412..416], &[0, 20, 255, 255]); + let view = DuotoneView::parse(&bytes); + assert_eq!(view.map(DuotoneView::ink_count), Some(4)); + for i in 0..4 { + assert_eq!(view.and_then(|v| v.ink(i)), Some(ink.clone())); + } + assert!(view.and_then(|v| v.ink(usize::MAX)).is_none()); + for n in 0..412 { + assert!(DuotoneView::parse(&bytes[..n]).is_none()); + } + assert!(DuotoneView::parse(&bytes[..412]).is_some()); + assert!(write_block(0, &slots, 20, &[NO_COLOR; 11]).is_none()); + assert!(write_block(5, &slots, 20, &[NO_COLOR; 11]).is_none()); + } + + #[test] + fn rejects_hostile_offsets_versions_counts_and_alternate_truncation() { + let mut bytes = [0u8; BLOCK_LEN]; + bytes[..4].copy_from_slice(&[0, 1, 255, 255]); + assert!(DuotoneView::parse(&bytes).is_none()); + bytes[..4].copy_from_slice(&[0, 2, 0, 1]); + assert!(DuotoneView::parse(&bytes).is_none()); + assert!(color_at(&bytes, usize::MAX).is_none()); + let alt = [0, 1, 0, 1, 0, 7, 0, 1, 0, 2, 0, 3, 0, 4]; + assert_eq!(alternate_color(&alt, 0), Some([7, 1, 2, 3, 4])); + assert!(alternate_color(&alt[..13], 0).is_none()); + assert!(alternate_color(&alt, 1).is_none()); + assert!(alternate_color(&alt, usize::MAX).is_none()); + } +} diff --git a/crates/psd/src/lib.rs b/crates/psd/src/lib.rs index 4c3222f46..dfb23b480 100644 --- a/crates/psd/src/lib.rs +++ b/crates/psd/src/lib.rs @@ -65,6 +65,7 @@ pub mod compression; pub mod csh; pub mod descriptor; pub mod displace_map; +pub mod duotone; pub mod engine_data; pub mod error; pub mod file; From ca365bd0f06542f42a6c5d334dce65b8c5e0d74f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:28:47 +0000 Subject: [PATCH 106/150] Allow FBX pre and post rotations without active flag --- crates/mesh/src/fbx.rs | 19 ++++++++++++++++--- crates/mesh/src/tests/fbx.rs | 13 +++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index acb654513..0494c8e9c 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -638,9 +638,9 @@ fn euler(d: [f64; 3], order: i64) -> Mat4 { } /// A Model's local transform: `T · Roff · Rp · Rpre · R · Rpostâ�»Â¹ · Rpâ�»Â¹ · Soff · Sp · S · Spâ�»Â¹`. -fn model_local(p: &Props) -> Mat4 { +fn model_local(p: &Props, apply_inactive_rotation: bool) -> Mat4 { let order = p.f("RotationOrder").map_or(0, |o| o as i64); - let active = p.f("RotationActive").is_some_and(|a| a != 0.0); + let active = apply_inactive_rotation || p.f("RotationActive").is_some_and(|a| a != 0.0); let (pre, post) = if active { (euler(p.v3("PreRotation", [0.0; 3]), 0), transpose(&euler(p.v3("PostRotation", [0.0; 3]), 0))) } else { (IDENTITY, IDENTITY) }; let rp = p.v3("RotationPivot", [0.0; 3]); @@ -671,6 +671,14 @@ fn geometric(p: &Props) -> Option { Some(chain(&[translate(t), euler(r, 0), scale(s)])) } +/// Optional FBX scene compatibility behavior. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct SceneOptions { + /// Apply pre/post rotations even when `RotationActive` is missing or zero. Some importers + /// retain these values regardless of the flag. + pub apply_inactive_rotation: bool, +} + impl Document { /// The top-level record of this name. pub fn top(&self, name: &str) -> Option<&Record> { @@ -720,6 +728,11 @@ impl Document { /// The scene the records describe. `resolve` is asked for texture image files (see the /// module documentation). pub fn scene(&self, limits: &Limits, resolve: Resolver) -> Result { + self.scene_with_options(limits, resolve, &SceneOptions::default()) + } + + /// Build a scene with explicitly selected compatibility behavior. + pub fn scene_with_options(&self, limits: &Limits, resolve: Resolver, options: &SceneOptions) -> Result { if self.version != 0 && self.version < 6000 { return Err(crate::Error::Unsupported(format!( "FBX {}.{} (only FBX 6.x and 7.x files are read)", @@ -761,7 +774,7 @@ impl Document { node_of.insert(*id, b.scene.nodes.len()); b.scene.nodes.push(Node { name: short_name(o.first_str().unwrap_or("")).to_string(), - transform: model_local(&Props::of(o)), + transform: model_local(&Props::of(o), options.apply_inactive_rotation), ..Node::default() }); } diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index a58ddeb19..9d4eec9c9 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -627,3 +627,16 @@ fn fbx6_model_holds_its_mesh() { assert_eq!(d.unit_scale(), Some(100.0)); } } + +#[test] +fn optional_inactive_pre_rotation_keeps_legacy_placements() { + let text = "FBXHeaderExtension: { FBXVersion: 7400 } Objects: { Model: 1, \"Model::A\", \"Null\" { Properties70: { P: \"PreRotation\", \"\", \"\", \"\", 0,0,90 } } }"; + let doc = document(text.as_bytes()); + let normal = doc.scene(&Limits::default(), &mut no_external).unwrap(); + let options = crate::fbx::SceneOptions { apply_inactive_rotation: true }; + let legacy = doc.scene_with_options(&Limits::default(), &mut no_external, &options).unwrap(); + let p = [1.0, 0.0, 0.0]; + assert_eq!(transform_point(&normal.nodes[0].transform, p), p); + let turned = transform_point(&legacy.nodes[0].transform, p); + assert!((turned[0]).abs() < 1e-12 && (turned[1] - 1.0).abs() < 1e-12); +} From a28cb617653255f5fe46624320b606bf9f692a8d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:33:29 +0000 Subject: [PATCH 107/150] Cover interleaved PCM sample representations --- crates/audio/src/pcm_interleaved.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/crates/audio/src/pcm_interleaved.rs b/crates/audio/src/pcm_interleaved.rs index 0fcb7f140..f08c19202 100644 --- a/crates/audio/src/pcm_interleaved.rs +++ b/crates/audio/src/pcm_interleaved.rs @@ -69,4 +69,28 @@ mod tests { decode_into(&[0; 8], Format { bits: 0, ..fmt }, &mut channels); assert_eq!(channels, [vec![0.5], vec![-0.5]]); } + #[test] + fn decodes_sample_representations_and_ignores_invalid_inputs() { + let base = Format { bits: 8, float: false, big_endian: false, signed: false }; + let cases: &[(Format, &[u8], f32)] = &[ + (base, &[192], 0.5), + (Format { signed: true, ..base }, &[192], -0.5), + (Format { bits: 16, ..base }, &[0, 64], 0.5), + (Format { bits: 24, ..base }, &[0, 0, 64], 0.5), + (Format { bits: 24, big_endian: true, ..base }, &[0xc0, 0, 0], -0.5), + (Format { bits: 32, ..base }, &[0, 0, 0, 64], 0.5), + (Format { bits: 32, big_endian: true, ..base }, &[0xc0, 0, 0, 0], -0.5), + (Format { bits: 32, float: true, ..base }, &0.5_f32.to_le_bytes(), 0.5), + (Format { bits: 32, float: true, big_endian: true, ..base }, &(-0.5_f32).to_be_bytes(), -0.5), + (Format { bits: 64, float: true, ..base }, &0.5_f64.to_le_bytes(), 0.5), + (Format { bits: 64, float: true, big_endian: true, ..base }, &(-0.5_f64).to_be_bytes(), -0.5), + (Format { bits: 12, ..base }, &[0xff, 0xff], 0.0), + ]; + for &(format, bytes, expected) in cases { + let mut out = [vec![1.0]]; + decode_into(bytes, format, &mut out); + assert_eq!(out[0], [1.0, expected], "{format:?}"); + } + decode_into(&[1], base, &mut []); + } } From b9c22554ce0fd2caccdb53b417082a2ff2ef178f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 13:35:18 -0500 Subject: [PATCH 108/150] Make serialized Duotone ink count valid by construction --- crates/psd/src/duotone.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/crates/psd/src/duotone.rs b/crates/psd/src/duotone.rs index 7b66da951..62283f576 100644 --- a/crates/psd/src/duotone.rs +++ b/crates/psd/src/duotone.rs @@ -15,6 +15,16 @@ pub type ColorRecord = [u16; 5]; /// Sentinel for an absent ink or computed overprint colour. pub const NO_COLOR: ColorRecord = [0xffff, 0, 0, 0, 0]; +/// Valid active ink counts for a serialized block. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[repr(u16)] +pub enum InkCount { + One = 1, + Two = 2, + Three = 3, + Four = 4, +} + /// One fixed-size ink slot, including uninterpreted reserved curve word. #[derive(Clone, Debug, PartialEq, Eq)] pub struct InkRecord { @@ -83,14 +93,11 @@ pub fn alternate_color(data: &[u8], index: usize) -> Option { } /// Serialize four raw slots and explicit trailing policy into a fixed-size block. -/// `count` must be 1..=4. Reserved bytes and inactive slots are preserved as supplied. -pub fn write_block(count: u16, inks: &[InkRecord; 4], dot_gain: u16, overprints: &[ColorRecord; 11]) -> Option<[u8; BLOCK_LEN]> { - if !(1..=4).contains(&count) { - return None; - } +/// The typed count cannot be invalid. Reserved bytes and inactive slots are preserved as supplied. +pub fn write_block(count: InkCount, inks: &[InkRecord; 4], dot_gain: u16, overprints: &[ColorRecord; 11]) -> [u8; BLOCK_LEN] { let mut out = [0u8; BLOCK_LEN]; out[..2].copy_from_slice(&1u16.to_be_bytes()); - out[2..4].copy_from_slice(&count.to_be_bytes()); + out[2..4].copy_from_slice(&(count as u16).to_be_bytes()); for (i, ink) in inks.iter().enumerate() { for (j, value) in ink.color.iter().enumerate() { let at = 4 + 10 * i + 2 * j; @@ -109,11 +116,10 @@ pub fn write_block(count: u16, inks: &[InkRecord; 4], dot_gain: u16, overprints: out[at..at + 2].copy_from_slice(&value.to_be_bytes()); } } - Some(out) + out } #[cfg(test)] -#[allow(clippy::unwrap_used)] mod tests { use super::*; @@ -124,7 +130,7 @@ mod tests { ink.curve[0] = 0; ink.curve[12] = 1000; let slots = std::array::from_fn(|_| ink.clone()); - let bytes = write_block(4, &slots, 20, &[NO_COLOR; 11]).unwrap(); + let bytes = write_block(InkCount::Four, &slots, 20, &[NO_COLOR; 11]); assert_eq!(&bytes[..4], &[0, 1, 0, 4]); assert_eq!(&bytes[44..48], &[3, b'I', b'n', b'k']); assert_eq!(&bytes[412..416], &[0, 20, 255, 255]); @@ -138,8 +144,6 @@ mod tests { assert!(DuotoneView::parse(&bytes[..n]).is_none()); } assert!(DuotoneView::parse(&bytes[..412]).is_some()); - assert!(write_block(0, &slots, 20, &[NO_COLOR; 11]).is_none()); - assert!(write_block(5, &slots, 20, &[NO_COLOR; 11]).is_none()); } #[test] From 357dc3fb73a1ecb0696edcb36bf6b2e30234bcdf Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:37:23 +0000 Subject: [PATCH 109/150] Support strict FBX geometry and final polygons --- crates/mesh/src/fbx.rs | 37 ++++++++++++++++++++++++++++++++++-- crates/mesh/src/tests/fbx.rs | 13 ++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index 0494c8e9c..a2e97f59c 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -677,6 +677,8 @@ pub struct SceneOptions { /// Apply pre/post rotations even when `RotationActive` is missing or zero. Some importers /// retain these values regardless of the flag. pub apply_inactive_rotation: bool, + /// Refuse malformed geometry arrays and accept an unterminated final polygon. + pub strict_geometry: bool, } impl Document { @@ -743,6 +745,7 @@ impl Document { let objects = self.top("Objects").ok_or_else(|| malformed(KIND, "no Objects section"))?; let mut b = Build { limits, + strict_geometry: options.strict_geometry, budget: Budget { left: Cell::new(self.left), limit: limits.max_data_bytes }, resolve, by_key: objects.children.iter().filter_map(|o| Some((key(o.props.first())?, o))).collect(), @@ -955,6 +958,7 @@ fn material(name: &str, record: &Record) -> Material { /// A scene under construction. struct Build<'a, 'r> { limits: &'a Limits, + strict_geometry: bool, budget: Budget, resolve: Resolver<'r>, by_key: HashMap, &'a Record>, @@ -1087,6 +1091,32 @@ impl<'a> Build<'a, '_> { fn mesh(&mut self, name: &str, geom: &Record, materials: &[usize]) -> Result> { let limits = self.limits; let (Some(vertices), Some(pvi)) = (geom.child("Vertices"), geom.child("PolygonVertexIndex")) else { return Ok(None) }; + if self.strict_geometry { + let valid = |record: &Record, integers: bool| { + let values = record.child("a").unwrap_or(record); + values.props.iter().all(|p| match p { + Prop::Int(_) | Prop::Ints(_) => true, + Prop::Float(f) => f.is_finite() && (!integers || f.fract() == 0.0), + Prop::Floats(a) => a.iter().all(|f| f.is_finite() && (!integers || f.fract() == 0.0)), + _ => false, + }) + }; + if !valid(vertices, false) || !valid(pvi, true) { + return Err(malformed(KIND, "mesh array contains a non-numeric, non-finite or fractional index value")); + } + for (element, names) in [ + ("LayerElementNormal", &["Normals", "NormalsIndex"][..]), + ("LayerElementUV", &["UV", "UVIndex"][..]), + ("LayerElementMaterial", &["Materials"][..]), + ("LayerElementColor", &["Colors", "ColorIndex"][..]), + ] { + if let Some(layer) = geom.children_named(element).min_by_key(|e| e.props.first().and_then(Prop::as_i64) != Some(0)) { + if names.iter().filter_map(|n| layer.child(n)).any(|r| !valid(r, false)) { + return Err(malformed(KIND, "mesh array contains a non-numeric or non-finite value")); + } + } + } + } let pool = |what: &str, n: usize| { if n > limits.max_vertices { Err(too_large(KIND, format!("more than {} {what}", limits.max_vertices))) } else { Ok(()) } }; @@ -1104,15 +1134,18 @@ impl<'a> Build<'a, '_> { let mut part_of: HashMap, usize> = HashMap::new(); let mut face: Vec = vec![]; let (mut broken, mut poly) = (false, 0usize); - for (pv, raw) in pvi.ints().into_iter().enumerate() { + let polygon_indices = pvi.ints(); + let index_count = polygon_indices.len(); + for (pv, raw) in polygon_indices.into_iter().enumerate() { // The last corner of a polygon is stored as `-index - 1`. - let (v, last) = if raw < 0 { (!raw, true) } else { (raw, false) }; + let (v, last) = if raw < 0 { (!raw, true) } else { (raw, self.strict_geometry && pv + 1 == index_count) }; match u32::try_from(v).ok().filter(|v| (*v as usize) < positions.len()) { Some(p) => face.push(Corner { position: p, normal: Layer::at(normal_layer, pv, p as usize, poly), uv: Layer::at(uv_layer, pv, p as usize, poly), }), + None if self.strict_geometry => return Err(malformed(KIND, format!("polygon vertex index {raw} out of range"))), None => broken = true, } if face.len() > limits.max_face_corners { diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index 9d4eec9c9..7f19c7d52 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -633,10 +633,21 @@ fn optional_inactive_pre_rotation_keeps_legacy_placements() { let text = "FBXHeaderExtension: { FBXVersion: 7400 } Objects: { Model: 1, \"Model::A\", \"Null\" { Properties70: { P: \"PreRotation\", \"\", \"\", \"\", 0,0,90 } } }"; let doc = document(text.as_bytes()); let normal = doc.scene(&Limits::default(), &mut no_external).unwrap(); - let options = crate::fbx::SceneOptions { apply_inactive_rotation: true }; + let options = crate::fbx::SceneOptions { apply_inactive_rotation: true, ..Default::default() }; let legacy = doc.scene_with_options(&Limits::default(), &mut no_external, &options).unwrap(); let p = [1.0, 0.0, 0.0]; assert_eq!(transform_point(&normal.nodes[0].transform, p), p); let turned = transform_point(&legacy.nodes[0].transform, p); assert!((turned[0]).abs() < 1e-12 && (turned[1] - 1.0).abs() < 1e-12); } + +#[test] +fn strict_geometry_rejects_bad_arrays_and_keeps_final_polygon() { + let options = crate::fbx::SceneOptions { strict_geometry: true, ..Default::default() }; + let read_strict = |s: &str| document(s.as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options); + let unterminated = ASCII.replace("a: 0,1,2,-4,0,1,-5", "a: 0,1,2,-4,0,1,4"); + assert_eq!(read_strict(&unterminated).unwrap().meshes[0].triangle_count(), 3); + for bad in ["a: 0,1,2,-4,0,1,200", "a: 0,1,2,-4,0,1,1.5", "a: 0,1,2,-4,0,1,NaN"] { + assert!(read_strict(&ASCII.replace("a: 0,1,2,-4,0,1,-5", bad)).is_err()); + } +} From 81057428d1e251d90415eb15f620d5cb3b866fa1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:41:32 +0000 Subject: [PATCH 110/150] Add shared FilmCraft project envelope and migrations --- Cargo.lock | 9 ++ Cargo.toml | 1 + crates/fcproj-format/Cargo.toml | 16 ++ crates/fcproj-format/src/lib.rs | 260 ++++++++++++++++++++++++++++++++ 4 files changed, 286 insertions(+) create mode 100644 crates/fcproj-format/Cargo.toml create mode 100644 crates/fcproj-format/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index b3325f790..3b008d6eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1671,6 +1671,15 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-fcproj-format" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-fig" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 0c9729e00..9e664024f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,6 +68,7 @@ craft-doc = { path = "crates/doc" } craft-docpath = { path = "crates/docpath" } craft-dpx = { path = "crates/dpx" } craft-dwg = { path = "crates/dwg" } +craft-fcproj-format = { path = "crates/fcproj-format" } craft-ecproj-format = { path = "crates/ecproj-format" } craft-figma-rest = { path = "crates/figma-rest" } craft-exr = { path = "crates/exr" } diff --git a/crates/fcproj-format/Cargo.toml b/crates/fcproj-format/Cargo.toml new file mode 100644 index 000000000..aa210187d --- /dev/null +++ b/crates/fcproj-format/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "craft-fcproj-format" +description = "FilmCraft project envelope and schema migrations" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } diff --git a/crates/fcproj-format/src/lib.rs b/crates/fcproj-format/src/lib.rs new file mode 100644 index 000000000..877b79765 --- /dev/null +++ b/crates/fcproj-format/src/lib.rs @@ -0,0 +1,260 @@ +//! FilmCraft `.fcproj` JSON envelope, version detection and historical migrations. +//! Project and editor-view types are supplied by the caller. +use serde::{Deserialize, Serialize}; +use serde_json::Value; +/// The `format` tag written in every envelope. +pub const FORMAT_ID: &str = "filmcraft.project"; + +/// A migration upgrades a whole document from schema `n` to `n + 1`. +pub type Migration = fn(Value) -> Result; + +/// `MIGRATIONS[i]` upgrades schema `i + 1` to `i + 2`. Append one function per schema bump; never +/// edit a shipped one. +pub const MIGRATIONS: &[Migration] = &[ + v1_to_v2, v2_to_v3, v3_to_v4, v4_to_v5, v5_to_v6, v6_to_v7, v7_to_v8, v8_to_v9, v9_to_v10, v10_to_v11, v11_to_v12, v12_to_v13, v13_to_v14, + v14_to_v15, v15_to_v16, v16_to_v17, +]; + +/// The schema version this build writes (and the newest it reads). +pub const SCHEMA_VERSION: u32 = MIGRATIONS.len() as u32 + 1; + +/// Oldest schema this build can still upgrade. +pub const OLDEST_SCHEMA: u32 = 1; + +#[derive(Debug, thiserror::Error)] +pub enum FormatError { + #[error("not a FilmCraft project: {0}")] + NotAProject(String), + #[error( + "this project was saved by a newer version of FilmCraft (project schema v{found}); this build reads up to v{supported}. Update FilmCraft to open it." + )] + TooNew { found: u32, supported: u32 }, + #[error("project schema v{found} is too old for this build (oldest supported: v{oldest})")] + TooOld { found: u32, oldest: u32 }, + #[error("upgrading project from schema v{from} to v{}: {msg}", from + 1)] + Migration { from: u32, msg: String }, + #[error("project file is damaged: {0}")] + Corrupt(String), +} + +/// Serialized current-schema envelope. +#[derive(Serialize)] +pub struct EnvelopeOut<'a, P, V> { + pub format: &'static str, + pub schema_version: u32, + pub generator: String, + pub project: &'a P, + #[serde(skip_serializing_if = "Option::is_none")] + pub view: Option<&'a V>, +} +/// Parsed envelope. View parsing remains optional to preserve forward compatibility. +#[derive(Deserialize)] +pub struct EnvelopeIn

                      { + #[serde(default)] + pub generator: Option, + pub project: P, + #[serde(default)] + pub view: Option, +} +/// Probe and validate the schema directly from bytes without building a JSON tree. +pub fn detect_bytes(bytes: &[u8]) -> Result { + let probe: Probe = serde_json::from_slice(bytes) + .map_err(|e| if e.is_syntax() || e.is_eof() { FormatError::Corrupt(e.to_string()) } else { FormatError::NotAProject(e.to_string()) })?; + let found = probe_version(&probe)?; + check_supported(found)?; + Ok(found) +} +/// Just enough of a document to know which schema it is. +#[derive(Deserialize)] +struct Probe { + #[serde(default)] + format: Option, + #[serde(default)] + schema_version: Option, + /// Bare (pre-envelope) project: its `version` field. + #[serde(default)] + version: Option, + #[serde(default)] + items: Option, + #[serde(default)] + root: Option, +} + +/// Detect the schema version of a parsed document. +pub fn detect_version(doc: &Value) -> Result { + let probe: Probe = serde_json::from_value(doc.clone()).map_err(|e| FormatError::NotAProject(e.to_string()))?; + probe_version(&probe) +} + +fn probe_version(p: &Probe) -> Result { + if let Some(v) = p.schema_version { + if let Some(f) = &p.format + && f != FORMAT_ID + { + return Err(FormatError::NotAProject(format!("format is `{f}`, expected `{FORMAT_ID}`"))); + } + return Ok(v); + } + // Before the envelope (v1) a file was the bare Project object with `version`. + // Its `version` was 1, or 2 on builds that briefly bumped it when caption tracks were added + // (same shape; the new field loads with its default). Anything higher is from the future. + if p.items.is_some() && p.root.is_some() { + return Ok(match p.version.unwrap_or(1) { + 0..=2 => 1, + v => v, + }); + } + Err(FormatError::NotAProject("no `schema_version` and not a legacy project".into())) +} + +pub fn check_supported(found: u32) -> Result<(), FormatError> { + if found > SCHEMA_VERSION { + return Err(FormatError::TooNew { found, supported: SCHEMA_VERSION }); + } + if found < OLDEST_SCHEMA { + return Err(FormatError::TooOld { found, oldest: OLDEST_SCHEMA }); + } + Ok(()) +} + +/// Run `table` from schema `from` up to `table.len() + 1`, stamping `schema_version` after each step. +pub fn migrate_with(table: &[Migration], mut doc: Value, from: u32) -> Result { + let target = table.len() as u32 + 1; + if from > target { + return Err(FormatError::TooNew { found: from, supported: target }); + } + if from == 0 { + return Err(FormatError::TooOld { found: 0, oldest: 1 }); + } + for v in from..target { + let step = table[(v - 1) as usize]; + doc = step(doc).map_err(|msg| FormatError::Migration { from: v, msg })?; + match doc.as_object_mut() { + Some(o) => { + o.insert("schema_version".into(), Value::from(v + 1)); + } + None => return Err(FormatError::Migration { from: v, msg: "migration produced a non-object document".into() }), + } + } + Ok(doc) +} + +/// v1 → v2: wrap the bare project in the envelope and drop the per-project `version` field. +fn v1_to_v2(doc: Value) -> Result { + let Value::Object(mut project) = doc else { return Err("expected a JSON object".into()) }; + project.remove("version"); + Ok(serde_json::json!({ + "format": FORMAT_ID, + "schema_version": 2, + "generator": "FilmCraft (schema v1)", + "project": Value::Object(project), + })) +} + +/// v2 → v3: graphic clips added; existing data needs no change. +fn v2_to_v3(doc: Value) -> Result { + Ok(doc) +} + +/// v3 → v4: Essential Sound fields added; existing data needs no change. +fn v3_to_v4(doc: Value) -> Result { + Ok(doc) +} + +/// v4 → v5: colour-management fields added; existing data needs no change. +fn v4_to_v5(doc: Value) -> Result { + Ok(doc) +} + +/// v5 → v6: media identity and ingest settings added; existing data needs no change. +fn v5_to_v6(doc: Value) -> Result { + Ok(doc) +} + +/// v6 → v7: effect masks (Bézier mask paths as keyframable parameters); existing data needs no change. +fn v6_to_v7(doc: Value) -> Result { + Ok(doc) +} + +/// v7 → v8: multi-camera source sequences / clips and merged clips added; existing data needs no +/// change. +fn v7_to_v8(doc: Value) -> Result { + Ok(doc) +} + +/// v8 → v9: per-media-item transcripts (`project.transcripts`) added; existing data needs no change. +fn v8_to_v9(doc: Value) -> Result { + Ok(doc) +} + +/// v9 → v10: clip time interpolation, Hold Filters, Field Options and audio source channels; the +/// Modify ▸ Audio Channels map; subclips' Restrict Trims flag. Existing data needs no change. +fn v9_to_v10(doc: Value) -> Result { + Ok(doc) +} + +/// v10 → v11: search bins (`project.search_bins`), Flash Cue markers, Project Settings safe areas, +/// capture format and scratch disks. Existing data needs no change. +fn v10_to_v11(doc: Value) -> Result { + Ok(doc) +} + +/// v11 → v12: graphics templates, rolls / crawls, responsive design and per-character text styles +/// on graphic clips, and source graphics. Existing data needs no change. +fn v11_to_v12(doc: Value) -> Result { + Ok(doc) +} + +/// v12 → v13: container audio streams and per-clip stream selection. `MediaInfo` reads the old +/// `audio` field as stream zero; `TrackItem::audio_stream` defaults to zero. +fn v12_to_v13(doc: Value) -> Result { + Ok(doc) +} + +/// v13 → v14: narrations (Text to Speech). Existing data needs no change. +fn v13_to_v14(doc: Value) -> Result { + Ok(doc) +} + +/// v14 → v15: project notes. Existing data needs no change. +fn v14_to_v15(doc: Value) -> Result { + Ok(doc) +} + +/// v15 → v16: existing colors remain presets; authors and the named custom palette default empty. +fn v15_to_v16(doc: Value) -> Result { + Ok(doc) +} + +/// v16 → v17: marker URLs default empty. Older builds must not silently drop saved URLs. +fn v16_to_v17(doc: Value) -> Result { + Ok(doc) +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn legacy_and_current_envelopes_preserve_values() { + let bare = json!({"version": 2, "items": {}, "root": {}, "extra": [1, 2]}); + assert_eq!(detect_version(&bare).unwrap(), 1); + let migrated = migrate_with(MIGRATIONS, bare, 1).unwrap(); + assert_eq!(migrated["schema_version"], 17); + assert_eq!(migrated["project"]["extra"], json!([1, 2])); + assert_eq!(migrated["generator"], "FilmCraft (schema v1)"); + assert_eq!(detect_bytes(serde_json::to_string(&migrated).unwrap().as_bytes()).unwrap(), 17); + let parsed: EnvelopeIn = serde_json::from_value(migrated).unwrap(); + assert!(parsed.view.is_none()); + assert_eq!(parsed.project["extra"], json!([1, 2])); + } + #[test] + fn rejects_wrong_future_and_damaged_envelopes() { + assert!(matches!(detect_bytes(br#"{"format":"other","schema_version":1}"#), Err(FormatError::NotAProject(_)))); + assert!(matches!(detect_bytes(br#"{"schema_version":18}"#), Err(FormatError::TooNew { found: 18, supported: 17 }))); + assert!(matches!(detect_bytes(br#"{"schema_version":0}"#), Err(FormatError::TooOld { found: 0, oldest: 1 }))); + assert!(matches!(detect_bytes(b"{"), Err(FormatError::Corrupt(_)))); + assert!(matches!(migrate_with(MIGRATIONS, json!([]), 1), Err(FormatError::Migration { from: 1, .. }))); + } +} From f43730f887d01c8962b36be628f5e91ddb9792ef Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:42:29 +0000 Subject: [PATCH 111/150] Exercise outline recovery without external fonts --- crates/font-catalog/src/outline.rs | 86 +++++++++++++++++------------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/crates/font-catalog/src/outline.rs b/crates/font-catalog/src/outline.rs index c5a79b72e..f48b61e4f 100644 --- a/crates/font-catalog/src/outline.rs +++ b/crates/font-catalog/src/outline.rs @@ -236,6 +236,25 @@ mod tests { ); } + #[test] + fn streaming_adapter_forwards_each_command_and_rejects_an_invalid_prefix() { + let mut pen = PathPen::new(); + { + let mut sink = Sink(&mut pen); + OutlinePen::move_to(&mut sink, 1.0, 2.0); + OutlinePen::line_to(&mut sink, 3.0, 4.0); + OutlinePen::quad_to(&mut sink, 5.0, 6.0, 7.0, 8.0); + OutlinePen::curve_to(&mut sink, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0); + OutlinePen::close(&mut sink); + } + assert_eq!(pen.path.elements().len(), 5); + assert_eq!(pen.path.elements()[2], PathEl::QuadTo((5.0, 6.0).into(), (7.0, 8.0).into())); + let mut invalid = PathPen::new(); + OutlinePen::line_to(&mut invalid, 1.0, 2.0); + assert!(invalid.invalid); + assert!(invalid.path.elements().is_empty()); + } + #[test] fn pen_caps_path_growth() { let mut pen = PathPen::new(); @@ -275,27 +294,31 @@ mod tests { assert_ne!(light_path, bold_path); } + // Construct a minimal CFF font so CI can exercise the public API without installed fonts. + fn cff_font(program: &[u8]) -> Vec { + let mut cff = vec![1, 0, 4, 4, 0, 1, 1, 1, 2, b'X', 0, 1, 1, 1, 7, 29, 0, 0, 0, 25, 17, 0, 0, 0, 0]; + cff.extend_from_slice(&[0, 1, 4]); + cff.extend_from_slice(&1u32.to_be_bytes()); + cff.extend_from_slice(&((program.len() as u32) + 1).to_be_bytes()); + cff.extend_from_slice(program); + let mut builder = write_fonts::FontBuilder::new(); + let mut head = vec![0; 54]; + head[18..20].copy_from_slice(&1000u16.to_be_bytes()); + let mut hhea = vec![0; 36]; + hhea[34..36].copy_from_slice(&1u16.to_be_bytes()); + builder.add_raw(write_fonts::types::Tag::new(b"head"), head); + builder.add_raw(write_fonts::types::Tag::new(b"hhea"), hhea); + builder.add_raw(write_fonts::types::Tag::new(b"hmtx"), vec![0, 100, 0, 0]); + builder.add_raw(write_fonts::types::Tag::new(b"maxp"), vec![0, 0, 0x50, 0, 0, 1]); + builder.add_raw(write_fonts::types::Tag::new(b"CFF "), cff); + builder.build() + } + #[test] fn malformed_cff_recovers_the_same_prefix_as_direct_drawing() { - use skrifa::raw::ps::cff::CffFontRef; - let Ok(dir) = std::env::var("CRAFT_FONTS_DIR") else { return }; - let mut bytes = std::fs::read(std::path::Path::new(&dir).join("fonts/noto-sans-cjk-sc/NotoSansCJKsc-Regular.otf")).unwrap(); - let (gid, start, len) = { - let f = FontRef::new(&bytes).unwrap(); - let cff = f.table_data(Tag::new(b"CFF ")).unwrap().as_bytes(); - let cff = CffFontRef::new(cff, 0, None).unwrap(); - let (gid, cs) = (0..cff.num_glyphs()) - .find_map(|gid| { - let cs = cff.charstrings().get(gid as usize)?; - (cs.len() >= 8).then_some((gid, cs)) - }) - .unwrap(); - (gid, cs.as_ptr() as usize - bytes.as_ptr() as usize, cs.len()) - }; - assert!(len >= 8); - // A move and line followed by a Type 2 callsubr without an operand. It emits a path - // prefix before the parser reports an error, without changing the font table offsets. - bytes[start..start + 8].copy_from_slice(&[139, 139, 21, 239, 139, 5, 10, 14]); + // Move and line, then callsubr without an operand: a prefix is emitted before failure. + let bytes = cff_font(&[139, 139, 21, 239, 139, 5, 10, 14]); + let gid = 0; let f = FontRef::new(&bytes).unwrap(); let mut old = PathPen::new(); let result = @@ -303,6 +326,8 @@ mod tests { assert!(result.is_err()); assert!(!old.path.elements().is_empty()); let shared = FontOutline::from_bytes(&bytes, 0, &[]).unwrap(); + assert_eq!(shared.metrics().units_per_em, 1000); + assert_eq!(shared.glyph_for('A'), None); let (prefix, status) = shared.outline_recovering(gid).unwrap(); assert!(matches!(status, Err(OutlineError::Draw(_)))); assert_eq!(prefix, old.path); @@ -314,28 +339,13 @@ mod tests { #[test] fn streaming_preserves_a_large_valid_cff_outline() { - let Ok(dir) = std::env::var("CRAFT_FONTS_DIR") else { return }; - let source = std::fs::read(std::path::Path::new(&dir).join("fonts/noto-sans-cjk-sc/NotoSansCJKsc-Regular.otf")).unwrap(); - let source = FontRef::new(&source).unwrap(); - // Minimal CFF with a single generated Type 2 charstring. The glyph has over 100,000 - // valid line segments; no binary fixture is copied into this repository. - let mut program = vec![139, 139, 21]; // rmoveto 0,0 + // Minimal CFF with more than 100,000 line segments. + let mut program = vec![139, 139, 21]; for _ in 0..100_001 { program.extend_from_slice(&[140, 6]); - } // hlineto 1 - program.push(14); // endchar - let mut cff = vec![1, 0, 4, 4, 0, 1, 1, 1, 2, b'X', 0, 1, 1, 1, 7, 29, 0, 0, 0, 25, 17, 0, 0, 0, 0]; - cff.extend_from_slice(&[0, 1, 4]); - cff.extend_from_slice(&1u32.to_be_bytes()); - cff.extend_from_slice(&((program.len() as u32) + 1).to_be_bytes()); - cff.extend_from_slice(&program); - let mut builder = write_fonts::FontBuilder::new(); - for tag in [b"head", b"maxp", b"hhea", b"hmtx"] { - builder.add_raw(write_fonts::types::Tag::new(tag), source.table_data(Tag::new(tag)).unwrap().as_bytes().to_vec()); } - assert!(skrifa::raw::ps::cff::CffFontRef::new(&cff, 0, None).is_ok(), "synthetic CFF parses"); - builder.add_raw(write_fonts::types::Tag::new(b"CFF "), cff); - let bytes = builder.build(); + program.push(14); + let bytes = cff_font(&program); let shared = FontOutline::from_bytes(&bytes, 0, &[]).unwrap(); struct Count(usize); impl GlyphPen for Count { From 9fd926dc93be7d4723e6eb4f484c7233d91d3e94 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 15 Nov 2026 13:43:03 -0500 Subject: [PATCH 112/150] cms: share EffectCraft SPI1D table parsing Port the SPI1D reader introduced by EffectCraft 492fa6de0a049c22afcc690afa36930d295261f3. Validate the declared table before storing samples and accumulate RGB entries directly to bound retention while preserving accepted format syntax. Co-authored-by: Dylan M. Taylor --- crates/cms/src/lib.rs | 1 + crates/cms/src/spi1d.rs | 59 +++++++++++++++++++++++++++++++++++++++ crates/cms/tests/spi1d.rs | 32 +++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 crates/cms/src/spi1d.rs create mode 100644 crates/cms/tests/spi1d.rs diff --git a/crates/cms/src/lib.rs b/crates/cms/src/lib.rs index 5e8a1e268..6302b042e 100644 --- a/crates/cms/src/lib.rs +++ b/crates/cms/src/lib.rs @@ -31,6 +31,7 @@ pub mod math; pub mod named; pub mod pipeline; pub mod profile; +pub mod spi1d; pub mod synth; pub mod transform; mod write; diff --git a/crates/cms/src/spi1d.rs b/crates/cms/src/spi1d.rs new file mode 100644 index 000000000..ea77bdc88 --- /dev/null +++ b/crates/cms/src/spi1d.rs @@ -0,0 +1,59 @@ +//! Sony Imageworks SPI1D tables, preserving EffectCraft's permissive header syntax. +//! +//! Unknown header lines and trailing text after the closing brace are ignored. Table +//! samples are not clamped or sanitized: callers own their numerical rendering policy. +//! The declared length is validated before storing samples; surplus samples are refused. + +/// Largest accepted table (the existing EffectCraft interpolation limit). +pub const MAX_ENTRIES: usize = 1 << 24; + +/// A one-dimensional RGB lookup table and its common input domain. +#[derive(Clone, Debug, PartialEq)] +pub struct Table { + pub values: Vec<[f32; 3]>, + pub domain: [f32; 2], +} + +/// Parse a SPI1D table with one or three components, expanding monochrome samples to RGB. +/// No allocation is based only on the claimed length. At most `MAX_ENTRIES` RGB entries +/// are retained. Missing final braces remain accepted, as in the source reader. +pub fn parse(text: &str) -> Option { + let (mut from, mut len, mut comps) = ([0.0f32, 1.0], 0usize, 1usize); + let mut values = Vec::new(); + let mut body = false; + let mut sample = [0.0; 3]; + let mut channel = 0; + for line in text.lines().map(str::trim).filter(|l| !l.is_empty()) { + if body { + if line.starts_with('}') { + break; + } + for t in line.split_whitespace() { + if values.len() == len { + return None; + } + sample[channel] = t.parse().ok()?; + channel += 1; + if channel == comps { + values.push(if comps == 1 { [sample[0]; 3] } else { sample }); + channel = 0; + } + } + continue; + } + let mut t = line.split_whitespace(); + match t.next()? { + "{" => { + if !(2..=MAX_ENTRIES).contains(&len) || !matches!(comps, 1 | 3) || from[1] <= from[0] || from.iter().any(|v| !v.is_finite()) { + return None; + } + body = true; + } + "From" => from = [t.next()?.parse().ok()?, t.next()?.parse().ok()?], + "Length" => len = t.next()?.parse().ok()?, + "Components" => comps = t.next()?.parse().ok()?, + _ => {} + } + } + (body && values.len() == len && channel == 0).then_some(Table { values, domain: from }) +} diff --git a/crates/cms/tests/spi1d.rs b/crates/cms/tests/spi1d.rs new file mode 100644 index 000000000..f4e27d051 --- /dev/null +++ b/crates/cms/tests/spi1d.rs @@ -0,0 +1,32 @@ +use craft_cms::spi1d::{MAX_ENTRIES, Table, parse}; + +#[test] +fn monochrome_and_three_component_samples_preserve_domain() { + assert_eq!( + parse("Version 1\nFrom -1 2\nLength 2\nComponents 1\n{\n-2\n3\n}"), + Some(Table { values: vec![[-2.0; 3], [3.0; 3]], domain: [-1.0, 2.0] }) + ); + assert_eq!( + parse("Length 2\nComponents 3\n{\n0 1\n2 3 4 5\n}"), + Some(Table { values: vec![[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]], domain: [0.0, 1.0] }) + ); +} + +#[test] +fn legacy_header_and_eof_tolerance_is_preserved() { + let table = parse("Ignored arbitrary header\nLength 2 extra\n{\n0 1"); + assert!(table.is_some()); + assert_eq!(table, parse("Length 2\n{\n0 1\n} ignored\ninvalid trailing text")); + assert!(parse("Length 2\n{\nNaN inf\n}").is_some(), "sample sanitization belongs to the caller"); +} + +#[test] +fn invalid_headers_partial_and_surplus_tables_are_refused() { + for header in ["Length 1", "Length 2\nComponents 2", "Length 2\nFrom 2 1", "Length 2\nFrom NaN 1"] { + assert!(parse(&format!("{header}\n{{\n0 1\n}}")).is_none()); + } + assert!(parse(&format!("Length {}\n{{\n0 1", MAX_ENTRIES + 1)).is_none()); + assert!(parse("Length 2\nComponents 3\n{\n0 1 2 3 4").is_none()); + assert!(parse("Length 2\n{\n0 1 2").is_none()); + assert!(parse("Length 2\n{\n").is_none()); +} From 818f05566eaa73c27e2fb884555d1eb8e31be646 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:43:10 +0000 Subject: [PATCH 113/150] Allow last FBX parent connection to win --- crates/mesh/src/fbx.rs | 8 +++++++- crates/mesh/src/tests/fbx.rs | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index a2e97f59c..3b37e8e59 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -679,6 +679,8 @@ pub struct SceneOptions { pub apply_inactive_rotation: bool, /// Refuse malformed geometry arrays and accept an unterminated final polygon. pub strict_geometry: bool, + /// If a model names several parents, use the last connection. + pub prefer_last_parent: bool, } impl Document { @@ -788,7 +790,11 @@ impl Document { if let (Some(ci), Some(pi)) = (node_of.get(c), node_of.get(p)) && ci != pi { - parent.entry(*ci).or_insert(*pi); + if options.prefer_last_parent { + parent.insert(*ci, *pi); + } else { + parent.entry(*ci).or_insert(*pi); + } } } for i in 0..b.scene.nodes.len() { diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index 7f19c7d52..c044df5b0 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -651,3 +651,14 @@ fn strict_geometry_rejects_bad_arrays_and_keeps_final_polygon() { assert!(read_strict(&ASCII.replace("a: 0,1,2,-4,0,1,-5", bad)).is_err()); } } + +#[test] +fn scene_option_selects_the_last_parent_connection() { + let text = "FBXHeaderExtension: { FBXVersion: 7400 } Objects: { Model: 1, \"Model::A\", \"Null\" {} Model: 2, \"Model::B\", \"Null\" {} Model: 3, \"Model::Child\", \"Null\" {} } Connections: { C: \"OO\",3,1 C: \"OO\",3,2 }"; + let doc = document(text.as_bytes()); + let options = crate::fbx::SceneOptions { prefer_last_parent: true, ..Default::default() }; + let scene = doc.scene_with_options(&Limits::default(), &mut no_external, &options).unwrap(); + assert!(scene.nodes[1].children.contains(&2)); + let default = doc.scene(&Limits::default(), &mut no_external).unwrap(); + assert!(default.nodes[0].children.contains(&2)); +} From 4ed217f1bc84e0bb438b9bb8358f64ee5b4fe44f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:43:11 +0000 Subject: [PATCH 114/150] Share TIFF first-directory preflight between inspection APIs --- crates/image-codecs/src/tiff_probe.rs | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/crates/image-codecs/src/tiff_probe.rs b/crates/image-codecs/src/tiff_probe.rs index 6aaf5c21a..1c6889c21 100644 --- a/crates/image-codecs/src/tiff_probe.rs +++ b/crates/image-codecs/src/tiff_probe.rs @@ -32,14 +32,12 @@ pub struct DecodeInfo { pub orientation: Option, } -/// Parse the first directory and enforce the pixel limit before decoding. -/// Unlike [`header`], this does not impose layout or strip checks ahead of the decoder. -pub fn decode_info(bytes: &[u8], max_pixels: u64) -> Result { +fn first_directory(bytes: &[u8], max_pixels: u64) -> Result<(craft_tiff::Ifd, craft_tiff::image::ImageInfo), Error> { let (tiff, complete) = craft_tiff::Tiff::parse_with_status(bytes, &Default::default()).map_err(|e| Error::Malformed(e.to_string()))?; if !complete { return Err(Error::Malformed("truncated TIFF".into())); } - let ifd = tiff.ifds.first().ok_or_else(|| Error::Malformed("no IFD".into()))?; + let ifd = tiff.ifds.into_iter().next().ok_or_else(|| Error::Malformed("no IFD".into()))?; let info = ifd.image().map_err(|e| Error::Malformed(e.to_string()))?; if info.width == 0 || info.height == 0 { return Err(Error::Malformed("zero dimension".into())); @@ -47,6 +45,13 @@ pub fn decode_info(bytes: &[u8], max_pixels: u64) -> Result { if u64::from(info.width) * u64::from(info.height) > max_pixels { return Err(Error::TooLarge { width: info.width, height: info.height }); } + Ok((ifd, info)) +} + +/// Parse the first directory and enforce the pixel limit before decoding. +/// Unlike [`header`], this does not impose layout or strip checks ahead of the decoder. +pub fn decode_info(bytes: &[u8], max_pixels: u64) -> Result { + let (ifd, info) = first_directory(bytes, max_pixels)?; Ok(DecodeInfo { width: info.width, height: info.height, @@ -58,18 +63,7 @@ pub fn decode_info(bytes: &[u8], max_pixels: u64) -> Result { /// Stored dimensions and orientation of the first directory, after checking the layouts and /// image-data bounds required by the TIFF decoder. Compressed strips are not decompressed. pub fn header(bytes: &[u8], max_pixels: u64) -> Result<(u32, u32, u16), Error> { - let (tiff, complete) = craft_tiff::Tiff::parse_with_status(bytes, &Default::default()).map_err(|e| Error::Malformed(e.to_string()))?; - if !complete { - return Err(Error::Malformed("truncated TIFF".into())); - } - let ifd = tiff.ifds.first().ok_or_else(|| Error::Malformed("no IFD".into()))?; - let info = ifd.image().map_err(|e| Error::Malformed(e.to_string()))?; - if info.width == 0 || info.height == 0 { - return Err(Error::Malformed("zero dimension".into())); - } - if u64::from(info.width) * u64::from(info.height) > max_pixels { - return Err(Error::TooLarge { width: info.width, height: info.height }); - } + let (ifd, info) = first_directory(bytes, max_pixels)?; let bits = info.bits_per_sample.first().copied().unwrap_or(1); if bits < 8 && info.planar == 2 && info.samples_per_pixel > 1 { return Err(Error::Unsupported("planar TIFF with samples narrower than a byte")); From c07e38090879ffa994f893df9956c6d5b96c049c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:45:37 +0000 Subject: [PATCH 115/150] Share TrueType subset embedding classification --- crates/font-catalog/src/embedding.rs | 85 ++++++++++++++++++++++++++++ crates/font-catalog/src/lib.rs | 2 + 2 files changed, 87 insertions(+) create mode 100644 crates/font-catalog/src/embedding.rs diff --git a/crates/font-catalog/src/embedding.rs b/crates/font-catalog/src/embedding.rs new file mode 100644 index 000000000..42321824e --- /dev/null +++ b/crates/font-catalog/src/embedding.rs @@ -0,0 +1,85 @@ +//! Permission to embed TrueType outline subsets based on the face's OS/2 `fsType`. + +use skrifa::{FontRef, Tag}; + +/// Whether a face can be embedded as a TrueType subset. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum SubsetEmbedding { + /// Installable, editable, or preview-and-print embedding, or no readable `fsType`. + Allowed, + /// Restricted licence embedding. + Restricted, + /// Embedding is allowed only without subsetting. + NoSubsetting, + /// Only bitmap embedding is allowed. + BitmapOnly, + /// The face is invalid or lacks a TrueType outline table. + NotTrueType, +} + +/// Classify a face's TrueType subset embedding permissions. Missing or truncated OS/2 +/// data permits embedding; bitmap-only takes precedence over restricted and no-subsetting. +pub fn subset_embedding(bytes: &[u8], index: u32) -> SubsetEmbedding { + let Ok(face) = FontRef::from_index(bytes, index) else { return SubsetEmbedding::NotTrueType }; + if [b"glyf", b"loca", b"head"].iter().any(|tag| face.table_data(Tag::new(tag)).is_none()) { + return SubsetEmbedding::NotTrueType; + } + let fs_type = face.table_data(Tag::new(b"OS/2")).and_then(|table| { + let data = table.as_bytes(); + Some(u16::from_be_bytes([*data.get(8)?, *data.get(9)?])) + }); + let Some(fs_type) = fs_type else { return SubsetEmbedding::Allowed }; + if fs_type & 0x0200 != 0 { + SubsetEmbedding::BitmapOnly + } else if fs_type & 0x000f == 0x0002 { + SubsetEmbedding::Restricted + } else if fs_type & 0x0100 != 0 { + SubsetEmbedding::NoSubsetting + } else { + SubsetEmbedding::Allowed + } +} + +#[cfg(test)] +mod tests { + use super::*; + use write_fonts::{FontBuilder, types::Tag}; + + fn font(fs_type: Option<&[u8]>, missing: Option<&[u8; 4]>) -> Vec { + let mut builder = FontBuilder::new(); + for tag in [b"glyf", b"loca", b"head"] { + if Some(tag) != missing { + builder.add_raw(Tag::new(tag), vec![0; 4]); + } + } + if let Some(data) = fs_type { + builder.add_raw(Tag::new(b"OS/2"), data.to_vec()); + } + builder.build() + } + + #[test] + fn subset_permissions_and_precedence() { + assert_eq!(subset_embedding(b"junk", 0), SubsetEmbedding::NotTrueType); + for tag in [b"glyf", b"loca", b"head"] { + assert_eq!(subset_embedding(&font(None, Some(tag)), 0), SubsetEmbedding::NotTrueType); + } + assert_eq!(subset_embedding(&font(None, None), 1), SubsetEmbedding::NotTrueType); + assert_eq!(subset_embedding(&font(Some(&[0; 9]), None), 0), SubsetEmbedding::Allowed); + for (bits, expected) in [ + (0x0000u16, SubsetEmbedding::Allowed), + (0x0004, SubsetEmbedding::Allowed), + (0x0008, SubsetEmbedding::Allowed), + (0x0006, SubsetEmbedding::Allowed), + (0x0002, SubsetEmbedding::Restricted), + (0x0100, SubsetEmbedding::NoSubsetting), + (0x0102, SubsetEmbedding::Restricted), + (0x0302, SubsetEmbedding::BitmapOnly), + ] { + let mut os2 = [0; 10]; + os2[8..10].copy_from_slice(&bits.to_be_bytes()); + assert_eq!(subset_embedding(&font(Some(&os2), None), 0), expected, "fsType {bits:#06x}"); + } + assert_eq!(subset_embedding(&font(None, None), 0), SubsetEmbedding::Allowed); + } +} diff --git a/crates/font-catalog/src/lib.rs b/crates/font-catalog/src/lib.rs index c787d3ece..4970217b6 100644 --- a/crates/font-catalog/src/lib.rs +++ b/crates/font-catalog/src/lib.rs @@ -21,6 +21,7 @@ //! ``` mod catalog; +mod embedding; mod env; mod fontconfig; mod registry; @@ -29,6 +30,7 @@ mod suitcase; pub mod system; pub use catalog::{Catalog, FontData, FontFileError, Limits, Lister, LoadedFace, Match, Watch}; +pub use embedding::{SubsetEmbedding, subset_embedding}; pub use env::{FontEnv, Os, font_dirs, font_store_dir, office_font_dirs}; pub use fontconfig::fontconfig_dirs; pub use scan::{FaceInfo, LocalizedName, Source, is_font_file, style_italic, style_weight, style_width}; From 4e1a15cee23f687959b965c0baaa1e1cf9aa2947 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:48:36 +0000 Subject: [PATCH 116/150] Share bounded SFNT face discovery --- crates/font-catalog/src/lib.rs | 2 + crates/font-catalog/src/sfnt.rs | 178 +++++++++++++++++++++++++++++++ crates/font-catalog/src/tests.rs | 17 +++ 3 files changed, 197 insertions(+) create mode 100644 crates/font-catalog/src/sfnt.rs diff --git a/crates/font-catalog/src/lib.rs b/crates/font-catalog/src/lib.rs index c787d3ece..5b78a8933 100644 --- a/crates/font-catalog/src/lib.rs +++ b/crates/font-catalog/src/lib.rs @@ -25,6 +25,8 @@ mod env; mod fontconfig; mod registry; mod scan; +/// Bounded SFNT face metadata discovery over caller-provided random-access readers. +pub mod sfnt; mod suitcase; pub mod system; diff --git a/crates/font-catalog/src/sfnt.rs b/crates/font-catalog/src/sfnt.rs new file mode 100644 index 000000000..83efa64dd --- /dev/null +++ b/crates/font-catalog/src/sfnt.rs @@ -0,0 +1,178 @@ +//! Minimal SFNT (TrueType / OpenType / collection) header reader for font discovery: reads only +//! the table directory, `name` and `OS/2` of each face, so scanning a system font folder does not +//! load whole files. + +use std::io::{Read, Seek, SeekFrom}; + +/// The tables skrifa reads glyph outlines from: TrueType, CFF, CFF2 and VARC. The renderer draws +/// no glyph of a face that has none of them, such as a face with bitmaps only or with its outlines +/// in Apple's `hvgl` table. +const OUTLINE_TABLES: [&[u8; 4]; 4] = [b"glyf", b"CFF ", b"CFF2", b"VARC"]; + +/// Names and style attributes of one face in a font file. +#[derive(Clone, Debug, PartialEq)] +pub struct FaceNames { + pub index: u32, + pub family: String, + pub style: String, + pub full_name: String, + pub weight: u16, + pub italic: bool, +} + +fn be16(b: &[u8], o: usize) -> Option { + Some(u16::from_be_bytes([*b.get(o)?, *b.get(o + 1)?])) +} +fn be32(b: &[u8], o: usize) -> Option { + Some(u32::from_be_bytes([*b.get(o)?, *b.get(o + 1)?, *b.get(o + 2)?, *b.get(o + 3)?])) +} + +fn read_at(r: &mut R, off: u64, len: usize) -> Option> { + if len > 4 << 20 { + return None; + } + r.seek(SeekFrom::Start(off)).ok()?; + let mut v = vec![0u8; len]; + r.read_exact(&mut v).ok()?; + Some(v) +} + +/// Read the names of every face in a font file (a single font or a collection). Faces with none +/// of the `OUTLINE_TABLES` are left out. +pub fn read_faces(r: &mut R) -> Vec { + let Some(head) = read_at(r, 0, 12) else { return Vec::new() }; + let offsets: Vec = if &head[0..4] == b"ttcf" { + let n = be32(&head, 8).unwrap_or(0).min(256) as usize; + let Some(t) = read_at(r, 12, n * 4) else { return Vec::new() }; + (0..n).filter_map(|i| be32(&t, i * 4)).collect() + } else { + vec![0] + }; + offsets.iter().enumerate().filter_map(|(i, &o)| read_face(r, o as u64, i as u32)).collect() +} + +/// Names from in-memory font data. +pub fn read_faces_bytes(data: &[u8]) -> Vec { + read_faces(&mut std::io::Cursor::new(data)) +} + +fn read_face(r: &mut R, off: u64, index: u32) -> Option { + let h = read_at(r, off, 12)?; + let tag = &h[0..4]; + if tag != [0, 1, 0, 0] && tag != b"OTTO" && tag != b"true" { + return None; + } + let n = be16(&h, 4)? as usize; + let dir = read_at(r, off + 12, n * 16)?; + if !dir.as_chunks::<16>().0.iter().any(|rec| OUTLINE_TABLES.iter().any(|t| rec.starts_with(*t))) { + return None; + } + let mut name = None; + let mut os2 = None; + for i in 0..n { + let rec = &dir[i * 16..i * 16 + 16]; + let (o, l) = (be32(rec, 8)? as u64, be32(rec, 12)? as usize); + match &rec[0..4] { + b"name" => name = Some((o, l)), + b"OS/2" => os2 = Some((o, l)), + _ => {} + } + } + let (no, nl) = name?; + let nb = read_at(r, no, nl)?; + let get = |id: u16| name_string(&nb, id); + let family = get(16).or_else(|| get(1))?; + let style = get(17).or_else(|| get(2)).unwrap_or_else(|| "Regular".into()); + let full_name = get(4).unwrap_or_else(|| format!("{family} {style}")); + let (mut weight, mut italic) = (400u16, false); + if let Some((oo, ol)) = os2 + && let Some(ob) = read_at(r, oo, ol.min(78)) + { + weight = be16(&ob, 4).unwrap_or(400); + italic = be16(&ob, 62).is_some_and(|fs| fs & 1 != 0); + } + let sl = style.to_ascii_lowercase(); + italic |= sl.contains("italic") || sl.contains("oblique"); + Some(FaceNames { index, family, style, full_name, weight, italic }) +} + +/// A name-table string by id, preferring Windows Unicode English, then any Unicode, then Mac Roman. +fn name_string(b: &[u8], id: u16) -> Option { + let count = be16(b, 2)? as usize; + let storage = be16(b, 4)? as usize; + let mut best: Option<(u8, String)> = None; + for i in 0..count { + let r = 6 + i * 12; + let (plat, enc, lang, nid, len, off) = + (be16(b, r)?, be16(b, r + 2)?, be16(b, r + 4)?, be16(b, r + 6)?, be16(b, r + 8)? as usize, be16(b, r + 10)? as usize); + if nid != id { + continue; + } + let rank = match plat { + 3 if enc == 1 || enc == 10 => { + if best.as_ref().is_some_and(|(current, _)| *current <= if lang == 0x409 { 0 } else { 1 }) { + continue; + } + if lang == 0x409 { 0 } else { 1 } + } + 0 => { + if best.as_ref().is_some_and(|(current, _)| *current <= 2) { + continue; + } + 2 + } + 1 if enc == 0 => { + if best.as_ref().is_some_and(|(current, _)| *current <= 3) { + continue; + } + 3 + } + _ => continue, + }; + // Only decode a record that can improve the selected name. A hostile name table may + // point thousands of records at the same huge string. + let raw = b.get(storage + off..storage + off + len)?; + let s = match plat { + 3 => { + let u: Vec = raw.as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes([c[0], c[1]])).collect(); + String::from_utf16_lossy(&u) + } + 0 => { + let u: Vec = raw.as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes([c[0], c[1]])).collect(); + String::from_utf16_lossy(&u) + } + 1 => raw.iter().map(|&c| if c < 128 { c as char } else { '?' }).collect(), + _ => return None, + }; + best = Some((rank, s)); + } + best.map(|(_, s)| s).filter(|s| !s.trim().is_empty()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn later_non_improving_name_records_are_not_decoded() { + // The first record is the preferred Windows-English value. The second names an invalid + // range: it must be ignored instead of making the whole name lookup fail. + let mut table = vec![0; 30]; + table[2..4].copy_from_slice(&2u16.to_be_bytes()); + table[4..6].copy_from_slice(&30u16.to_be_bytes()); + let first = 6usize; + table[first..first + 2].copy_from_slice(&3u16.to_be_bytes()); + table[first + 2..first + 4].copy_from_slice(&1u16.to_be_bytes()); + table[first + 4..first + 6].copy_from_slice(&0x409u16.to_be_bytes()); + table[first + 6..first + 8].copy_from_slice(&1u16.to_be_bytes()); + table[first + 8..first + 10].copy_from_slice(&4u16.to_be_bytes()); + let second = first + 12; + table[second..second + 2].copy_from_slice(&3u16.to_be_bytes()); + table[second + 2..second + 4].copy_from_slice(&1u16.to_be_bytes()); + table[second + 4..second + 6].copy_from_slice(&0x409u16.to_be_bytes()); + table[second + 6..second + 8].copy_from_slice(&1u16.to_be_bytes()); + table[second + 8..second + 10].copy_from_slice(&u16::MAX.to_be_bytes()); + table.extend_from_slice(&[0, b'A', 0, b'B']); + assert_eq!(name_string(&table, 1).as_deref(), Some("AB")); + } +} diff --git a/crates/font-catalog/src/tests.rs b/crates/font-catalog/src/tests.rs index 5137a4295..7967cc9c8 100644 --- a/crates/font-catalog/src/tests.rs +++ b/crates/font-catalog/src/tests.rs @@ -568,3 +568,20 @@ fn a_read_is_capped_where_the_file_has_grown() { assert!(matches!(read_capped(&path.with_extension("missing"), 9), Err(FontFileError::Io(_)))); let _ = std::fs::remove_file(&path); } + +#[test] +fn bounded_sfnt_reader_preserves_face_names_and_collection_indices() { + let mut bold = spec("Example", "Bold Italic", 700); + bold.italic = true; + let data = font(&bold); + let faces = crate::sfnt::read_faces_bytes(&data); + assert_eq!(faces.len(), 1); + assert_eq!((&*faces[0].family, &*faces[0].style, faces[0].weight, faces[0].italic), ("Example", "Bold Italic", 700, true)); + let mut hidden = spec("Hidden", "Regular", 400); + hidden.outlines = false; + let combined = collection(&[font(&hidden), data]); + let faces = crate::sfnt::read_faces_bytes(&combined); + assert_eq!(faces.len(), 1); + assert_eq!((faces[0].index, faces[0].family.as_str()), (1, "Example")); + assert!(crate::sfnt::read_faces_bytes(b"invalid").is_empty()); +} From 39b7076b0421ef056d5d57bd1e08cb65d21e0332 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:50:08 +0000 Subject: [PATCH 117/150] Allow object-only FBX hierarchy connections --- crates/mesh/src/fbx.rs | 21 +++++++++++++++++++-- crates/mesh/src/tests/fbx.rs | 10 ++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index 3b37e8e59..8f0b17c3c 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -681,6 +681,8 @@ pub struct SceneOptions { pub strict_geometry: bool, /// If a model names several parents, use the last connection. pub prefer_last_parent: bool, + /// Restrict model hierarchy, geometry and material links to object connections. + pub only_object_connections: bool, } impl Document { @@ -748,10 +750,12 @@ impl Document { let mut b = Build { limits, strict_geometry: options.strict_geometry, + only_object_connections: options.only_object_connections, budget: Budget { left: Cell::new(self.left), limit: limits.max_data_bytes }, resolve, by_key: objects.children.iter().filter_map(|o| Some((key(o.props.first())?, o))).collect(), children: HashMap::new(), + object_children: HashMap::new(), scene: Scene::default(), material_of: HashMap::new(), texture_of: HashMap::new(), @@ -764,8 +768,14 @@ impl Document { let mut links: Vec<(Key, Key)> = vec![]; for e in self.top("Connections").into_iter().flat_map(|c| c.children.iter()).filter(|e| e.name == "C" || e.name == "Connect") { let (Some(child), Some(parent)) = (key(e.props.get(1)), key(e.props.get(2))) else { continue }; - links.push((child, parent)); + let object = e.str_at(0) == Some("OO"); + if !options.only_object_connections || object { + links.push((child, parent)); + } b.children.entry(parent).or_default().push((child, e.str_at(3))); + if options.only_object_connections && object { + b.object_children.entry(parent).or_default().push((child, e.str_at(3))); + } } let models: Vec<(Key, &Record)> = @@ -965,10 +975,12 @@ fn material(name: &str, record: &Record) -> Material { struct Build<'a, 'r> { limits: &'a Limits, strict_geometry: bool, + only_object_connections: bool, budget: Budget, resolve: Resolver<'r>, by_key: HashMap, &'a Record>, children: HashMap, Vec<(Key<'a>, Option<&'a str>)>>, + object_children: HashMap, Vec<(Key<'a>, Option<&'a str>)>>, scene: Scene, material_of: HashMap, usize>, texture_of: HashMap, Option>, @@ -983,7 +995,12 @@ struct Build<'a, 'r> { impl<'a> Build<'a, '_> { /// The objects of `kind` connected under `id`, with the property each connects to. fn children_of(&self, id: Key<'a>, kind: &str) -> Vec<(Key<'a>, Option<&'a str>)> { - let all = self.children.get(&id).map(Vec::as_slice).unwrap_or_default(); + let source = if self.only_object_connections && matches!(kind, "Geometry" | "Material" | "NodeAttribute") { + &self.object_children + } else { + &self.children + }; + let all = source.get(&id).map(Vec::as_slice).unwrap_or_default(); all.iter().filter(|l| self.by_key.get(&l.0).is_some_and(|o| o.name == kind)).copied().collect() } diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index c044df5b0..679cb0124 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -662,3 +662,13 @@ fn scene_option_selects_the_last_parent_connection() { let default = doc.scene(&Limits::default(), &mut no_external).unwrap(); assert!(default.nodes[0].children.contains(&2)); } + +#[test] +fn object_only_hierarchy_ignores_property_connections() { + let text = "FBXHeaderExtension: { FBXVersion: 7400 } Objects: { Model: 1, \"Model::A\", \"Null\" {} Model: 2, \"Model::B\", \"Null\" {} } Connections: { C: \"OP\",2,1,\"SomeProperty\" }"; + let doc = document(text.as_bytes()); + let options = crate::fbx::SceneOptions { only_object_connections: true, ..Default::default() }; + let scene = doc.scene_with_options(&Limits::default(), &mut no_external, &options).unwrap(); + assert_eq!(scene.roots.len(), 2); + assert_eq!(doc.scene(&Limits::default(), &mut no_external).unwrap().roots.len(), 1); +} From e79d9a8d5ff79d3e31c12748ecd45e90ee9d3168 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:51:28 +0000 Subject: [PATCH 118/150] Cover classic TIFF numeric field representations --- crates/tiff/src/classic.rs | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/crates/tiff/src/classic.rs b/crates/tiff/src/classic.rs index 3408eadcd..c1ab6f64a 100644 --- a/crates/tiff/src/classic.rs +++ b/crates/tiff/src/classic.rs @@ -418,4 +418,53 @@ mod tests { let (t, first) = Tiff::new(&b).unwrap(); assert_eq!(t.all_ifds(first).unwrap().len(), 3); } + #[test] + fn numeric_field_types_and_malformed_values() { + fn field(code: u16, payload: &[u8]) -> Vec { + let mut b = b"II*\0".to_vec(); + b.extend_from_slice(&8u32.to_le_bytes()); + b.extend_from_slice(&1u16.to_le_bytes()); + b.extend_from_slice(&256u16.to_le_bytes()); + b.extend_from_slice(&code.to_le_bytes()); + b.extend_from_slice(&1u32.to_le_bytes()); + if payload.len() <= 4 { + let mut inline = [0; 4]; + inline[..payload.len()].copy_from_slice(payload); + b.extend_from_slice(&inline); + } else { + b.extend_from_slice(&26u32.to_le_bytes()); + } + b.extend_from_slice(&0u32.to_le_bytes()); + if payload.len() > 4 { + b.extend_from_slice(payload); + } + b + } + let cases: &[(u16, &[u8], f64)] = &[ + (1, &[7], 7.0), + (6, &[255], -1.0), + (3, &[2, 0], 2.0), + (8, &[255, 255], -1.0), + (4, &[3, 0, 0, 0], 3.0), + (9, &[255, 255, 255, 255], -1.0), + (5, &[3, 0, 0, 0, 2, 0, 0, 0], 1.5), + (10, &[253, 255, 255, 255, 2, 0, 0, 0], -1.5), + (11, &1.5_f32.to_le_bytes(), 1.5), + (12, &1.5_f64.to_le_bytes(), 1.5), + ]; + for &(code, data, expected) in cases { + let bytes = field(code, data); + let (t, at) = Tiff::new(&bytes).unwrap(); + let (ifd, _) = t.read_ifd(at, 0).unwrap(); + assert_eq!(t.floats(&ifd.entries[0]).unwrap(), [expected], "type {code}"); + } + let bytes = field(5, &[1, 0, 0, 0, 0, 0, 0, 0]); + let (t, at) = Tiff::new(&bytes).unwrap(); + let (ifd, _) = t.read_ifd(at, 0).unwrap(); + assert!(t.floats(&ifd.entries[0]).is_err()); + let bytes = field(11, &f32::NAN.to_le_bytes()); + let (t, at) = Tiff::new(&bytes).unwrap(); + let (ifd, _) = t.read_ifd(at, 0).unwrap(); + assert!(t.floats(&ifd.entries[0]).is_err()); + } } From e7c96161ddaf860a9fa7f8fd21f935620e553022 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 13:51:53 -0500 Subject: [PATCH 119/150] Share JPEG marker and MPF index inspection (#1634) * Expose tolerant JPEG marker inspection * Export JPEG inspection API * Factor JPEG inspection fields from shared walker * Share ICC chunk collection between JPEG inspection modes * Cover JPEG MPF and gain-map inspection * Expose MPF index inspection for located payloads * Cover located MPF index parity * Preserve bounded MPF index directory validation --- crates/metadata/src/container.rs | 215 +++++++++++++++++++++++++------ crates/metadata/src/lib.rs | 2 +- crates/metadata/src/mpf.rs | 36 +++++- 3 files changed, 215 insertions(+), 38 deletions(-) diff --git a/crates/metadata/src/container.rs b/crates/metadata/src/container.rs index 842020683..65a47ba05 100644 --- a/crates/metadata/src/container.rs +++ b/crates/metadata/src/container.rs @@ -84,22 +84,83 @@ pub fn embedded(bytes: &[u8]) -> Embedded { const XMP_SIG: &[u8] = b"http://ns.adobe.com/xap/1.0/\0"; const XMP_EXT_SIG: &[u8] = b"http://ns.adobe.com/xmp/extension/\0"; +/// JPEG frame, marker and metadata details collected before the first scan. +#[derive(Debug, Default)] +pub struct JpegInspection { + /// Stored frame width. + pub width: u32, + /// Stored frame height. + pub height: u32, + /// Number of frame components. + pub components: u8, + /// Sample precision. + pub precision: u8, + /// Start-of-frame marker byte. + pub sof: u8, + /// Components in the first scan. + pub first_scan_components: u8, + /// Number of APP1 segments. + pub app1: u32, + /// First EXIF TIFF payload. + pub exif: Option>, + /// Offset of the EXIF TIFF payload. + pub exif_offset: usize, + /// First standard XMP packet. + pub xmp: Option, + /// Reassembled ICC profile. + pub icc: Option>, + /// ICC chunks were malformed or inconsistent. + pub icc_invalid: bool, + /// Adobe APP14 transform byte. + pub adobe_transform: Option, + /// MPF TIFF offset and payload. + pub mpf: Option<(usize, Vec)>, + /// ISO gain-map APP2 payload following its identifier. + pub iso_gainmap: Option>, + valid: bool, +} + +impl JpegInspection { + /// Whether metadata identifies this image as a gain map. + pub fn is_gain_map(&self) -> bool { + self.iso_gainmap.as_ref().is_some_and(|p| p.len() > 4) || self.xmp.as_deref().is_some_and(|x| x.contains("hdrgm:GainMapMax")) + } +} + +/// Inspect markers before the first scan, tolerating garbage between segments and truncated +/// final payloads. Returns `None` for a missing SOI or an invalid segment length. +pub fn inspect_jpeg(b: &[u8]) -> Option { + if !b.starts_with(&[0xff, 0xd8]) { + return None; + } + let (embedded, _, mut inspection) = jpeg_segments_inner(b, false, true); + if !inspection.valid { + return None; + } + inspection.exif = embedded.exif; + inspection.xmp = embedded.xmp.map(|x| x.trim_end_matches('\0').to_string()); + inspection.icc = embedded.icc; + Some(inspection) +} + /// Walk JPEG markers up to SOS and collect APP1 Exif / XMP, APP2 ICC and APP13 IPTC. pub fn jpeg_segments(b: &[u8]) -> Embedded { - jpeg_segments_inner(b, false).0 + jpeg_segments_inner(b, false, false).0 } /// Walk JPEG metadata and return every ordinary XMP APP1 packet in segment order, along with the /// usual [`Embedded`] projection (which retains only the first ordinary packet). pub fn jpeg_segments_with_all_xmp(b: &[u8]) -> (Embedded, Vec) { - jpeg_segments_inner(b, true) + let (embedded, packets, _) = jpeg_segments_inner(b, true, false); + (embedded, packets) } -fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec) { +fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool, inspect: bool) -> (Embedded, Vec, JpegInspection) { let mut out = Embedded::default(); let mut xmp_packets = Vec::new(); + let mut inspection = JpegInspection { valid: true, ..Default::default() }; if !b.starts_with(&[0xff, 0xd8]) { - return (out, xmp_packets); + return (out, xmp_packets, inspection); } let mut icc_parts: [Option<&[u8]>; 255] = [None; 255]; let mut icc_total = None; @@ -110,6 +171,10 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec (Embedded, Vec (Embedded, Vec out.exif = Some(payload.get(6..).unwrap_or(&[]).to_vec()), + 0xe1 if payload.starts_with(if inspect { b"Exif\0\0" } else { b"Exif\0" }) && out.exif.is_none() => { + out.exif = Some(payload.get(6..).unwrap_or(&[]).to_vec()) + } 0xe1 if payload.starts_with(XMP_SIG) && out.xmp.is_none() => { let packet = String::from_utf8_lossy(&payload[XMP_SIG.len()..]).into_owned(); if collect_all_xmp { @@ -145,7 +219,7 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec { xmp_packets.push(String::from_utf8_lossy(&payload[XMP_SIG.len()..]).into_owned()); } - 0xe1 if payload.starts_with(XMP_EXT_SIG) => { + 0xe1 if !inspect && payload.starts_with(XMP_EXT_SIG) => { // GUID (32) + full length (4) + offset (4) + data let p = &payload[XMP_EXT_SIG.len()..]; if p.len() >= 40 { @@ -156,35 +230,9 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec { - if !complete || payload.len() < 14 { - icc_invalid = true; - } else if let Some(header) = payload.get(12..14) { - let (Some(&sequence), Some(&total)) = (header.first(), header.get(1)) else { - icc_invalid = true; - i = end; - continue; - }; - if sequence == 0 || total == 0 || sequence > total || icc_total.is_some_and(|expected| expected != total) { - icc_invalid = true; - } else { - icc_total = Some(total); - if let Some(slot) = icc_parts.get_mut(usize::from(sequence - 1)) { - if slot.is_some() { - icc_invalid = true; - } else if let Some(data) = payload.get(14..) { - *slot = Some(data); - } else { - icc_invalid = true; - } - } else { - icc_invalid = true; - } - } - } else { - icc_invalid = true; - } + collect_icc_part(payload, complete, &mut icc_parts, &mut icc_total, &mut icc_invalid); } - 0xed if payload.starts_with(b"Photoshop 3.0\0") && out.iptc.is_none() => out.iptc = photoshop_iptc(&payload[14..]), + 0xed if !inspect && payload.starts_with(b"Photoshop 3.0\0") && out.iptc.is_none() => out.iptc = photoshop_iptc(&payload[14..]), _ => {} } i = end; @@ -214,8 +262,61 @@ fn jpeg_segments_inner(b: &[u8], collect_all_xmp: bool) -> (Embedded, Vec(payload: &'a [u8], complete: bool, parts: &mut [Option<&'a [u8]>; 255], total: &mut Option, invalid: &mut bool) { + if !complete || payload.len() < 14 { + *invalid = true; + return; + } + let (sequence, count) = (payload[12], payload[13]); + if sequence == 0 || count == 0 || sequence > count || total.is_some_and(|expected| expected != count) { + *invalid = true; + return; + } + *total = Some(count); + let Some(slot) = parts.get_mut(usize::from(sequence - 1)) else { + *invalid = true; + return; + }; + if slot.is_some() { + *invalid = true; + } else { + *slot = payload.get(14..); + } +} + +fn inspect_marker(info: &mut JpegInspection, marker: u8, payload: &[u8], start: usize, needs_exif: bool) { + if (0xc0..=0xcf).contains(&marker) && !matches!(marker, 0xc4 | 0xc8 | 0xcc) && payload.len() >= 6 && info.components == 0 { + info.precision = payload[0]; + info.sof = marker; + info.height = u32::from(u16::from_be_bytes([payload[1], payload[2]])); + info.width = u32::from(u16::from_be_bytes([payload[3], payload[4]])); + info.components = payload[5]; + } + if marker == 0xe1 { + info.app1 = info.app1.saturating_add(1); + } + if marker == 0xe1 && payload.starts_with(b"Exif\0\0") && needs_exif { + info.exif_offset = start + 6; + } + if marker == 0xe2 && payload.starts_with(b"MPF\0") && info.mpf.is_none() { + info.mpf = Some((start + 4, payload[4..].to_vec())); + } + if marker == 0xe2 + && info.iso_gainmap.is_none() + && let Some(data) = payload.strip_prefix(b"urn:iso:std:iso:ts:21496:-1\0") + { + info.iso_gainmap = Some(data.to_vec()); + } + if marker == 0xee && payload.starts_with(b"Adobe") && payload.len() >= 12 { + info.adobe_transform = Some(payload[11]); + } } /// Put the Extended XMP of JPEG `b` into `out`: when the standard packet links one, only that GUID's @@ -865,3 +966,45 @@ mod tests { } } } + +#[cfg(test)] +mod jpeg_inspection_tests { + use super::*; + fn segment(out: &mut Vec, marker: u8, data: &[u8]) { + out.extend_from_slice(&[0xff, marker]); + out.extend_from_slice(&((data.len() as u16) + 2).to_be_bytes()); + out.extend_from_slice(data); + } + #[test] + fn reports_mpf_gainmap_and_adobe_transform() { + let mut b = vec![0xff, 0xd8]; + segment(&mut b, 0xe2, b"MPF\0IIabc"); + segment(&mut b, 0xe2, b"urn:iso:std:iso:ts:21496:-1\0abcde"); + segment(&mut b, 0xee, b"Adobe\0\0\0\0\0\0\x02"); + b.extend_from_slice(&[0xff, 0xd9]); + let info = inspect_jpeg(&b).unwrap(); + let (offset, data) = info.mpf.unwrap(); + assert_eq!(&b[offset..offset + data.len()], data); + assert_eq!(info.iso_gainmap.as_deref(), Some(&b"abcde"[..])); + assert_eq!(info.adobe_transform, Some(2)); + } + + #[test] + fn tolerant_walk_keeps_offsets_frame_and_icc_chunks() { + let mut b = vec![0xff, 0xd8, 0x44]; + segment(&mut b, 0xe1, b"Exif\0\0II"); + segment(&mut b, 0xe2, b"ICC_PROFILE\0\x02\x02B"); + segment(&mut b, 0xe2, b"ICC_PROFILE\0\x01\x02A"); + segment(&mut b, 0xc0, &[8, 0, 2, 0, 3, 3]); + b.extend_from_slice(&[0xff, 0xda, 0, 3, 3]); + let info = inspect_jpeg(&b).unwrap(); + assert_eq!((info.width, info.height, info.components, info.first_scan_components), (3, 2, 3, 3)); + assert_eq!(info.app1, 1); + assert_eq!(&b[info.exif_offset..info.exif_offset + 2], b"II"); + assert_eq!(info.icc.as_deref(), Some(&b"AB"[..])); + assert!(!info.icc_invalid); + // The existing strict API still stops at the garbage byte. + assert!(jpeg_segments(&b).exif.is_none()); + assert!(inspect_jpeg(&[0xff, 0xd8, 0xff, 0xe1, 0, 1]).is_none()); + } +} diff --git a/crates/metadata/src/lib.rs b/crates/metadata/src/lib.rs index ff2c530fa..bf6700455 100644 --- a/crates/metadata/src/lib.rs +++ b/crates/metadata/src/lib.rs @@ -37,7 +37,7 @@ pub mod tags; mod xmp; mod xmp_merge; -pub use container::{Embedded, embedded, jpeg_segments, jpeg_segments_with_all_xmp, png_chunks, webp_chunks}; +pub use container::{Embedded, JpegInspection, embedded, inspect_jpeg, jpeg_segments, jpeg_segments_with_all_xmp, png_chunks, webp_chunks}; pub use datetime::{DatePrecision, DateTime}; pub use exif::{from_tiff, read_exif, strip_exif_header, try_read_exif, try_read_exif_with, write_exif}; pub use focus::FocusPoint; diff --git a/crates/metadata/src/mpf.rs b/crates/metadata/src/mpf.rs index 602c8f90d..9da7dc8c6 100644 --- a/crates/metadata/src/mpf.rs +++ b/crates/metadata/src/mpf.rs @@ -113,7 +113,40 @@ pub fn images(jpeg: &[u8]) -> Vec { }; // offsets count from the byte-order field, which follows the signature let Some(base) = segs.iter().find(|s| jpeg.get(s.1..s.2) == Some(payload)).map(|s| s.1 + SIGNATURE.len()) else { return Vec::new() }; - let Some(tiff) = payload.get(SIGNATURE.len()..).and_then(|t| Tiff::parse(t).ok()) else { return Vec::new() }; + let Some(index) = payload.get(SIGNATURE.len()..) else { return Vec::new() }; + parse_index(index, base) +} + +/// Read an already located MPF TIFF index. `base` is the index's offset in the containing file. +/// Entries are bounded to [`MAX_ENTRIES`] and returned without reading image payloads. +pub fn images_from_index(index: &[u8], base: usize) -> Vec { + // The bounded legacy reader requires a complete classic first IFD, while the generic TIFF + // parser can salvage partial directories. Preserve that refusal for located MPF indices. + let le = match index.get(..2) { + Some(b"II") => true, + Some(b"MM") => false, + _ => return Vec::new(), + }; + let read16 = + |at: usize| index.get(at..at.checked_add(2)?).map(|b| if le { u16::from_le_bytes([b[0], b[1]]) } else { u16::from_be_bytes([b[0], b[1]]) }); + let read32 = |at: usize| { + index + .get(at..at.checked_add(4)?) + .map(|b| if le { u32::from_le_bytes([b[0], b[1], b[2], b[3]]) } else { u32::from_be_bytes([b[0], b[1], b[2], b[3]]) }) + }; + if read16(2) != Some(42) { + return Vec::new(); + } + let Some(at) = read32(4).and_then(|v| usize::try_from(v).ok()) else { return Vec::new() }; + let Some(count) = read16(at).map(usize::from).filter(|n| *n <= 4096) else { return Vec::new() }; + if at.checked_add(2).and_then(|p| count.checked_mul(12).and_then(|n| p.checked_add(n))).is_none_or(|end| end > index.len()) { + return Vec::new(); + } + parse_index(index, base) +} + +fn parse_index(index: &[u8], base: usize) -> Vec { + let Some(tiff) = Tiff::parse(index).ok() else { return Vec::new() }; let Some(list) = tiff.ifds.first().and_then(|i| i.bytes(MP_ENTRY)) else { return Vec::new() }; let u32_at = |rec: &[u8], at: usize| rec.get(at..at + 4).and_then(|s| <[u8; 4]>::try_from(s).ok()).map(|s| tiff.order.u32(s) as usize); let mut out = Vec::new(); @@ -226,6 +259,7 @@ mod tests { let segs = segments(&file).unwrap(); assert_eq!(segs.iter().map(|s| s.0).collect::>(), [0xe0, 0xe1, 0xe2, 0xdb]); let (_, a, e) = segs[2]; + assert_eq!(images_from_index(&file[a + SIGNATURE.len()..e], a + SIGNATURE.len()), found); let mut without = file[..a - 4].to_vec(); without.extend_from_slice(&file[e..found[0].offset]); assert_eq!(without, primary); From ce5f671c221231d000dcdcf5351068de5b45eb1e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:54:08 +0000 Subject: [PATCH 120/150] Fix strict FBX geometry clippy warning --- crates/mesh/src/fbx.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index a2e97f59c..eb468abc9 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -1110,10 +1110,10 @@ impl<'a> Build<'a, '_> { ("LayerElementMaterial", &["Materials"][..]), ("LayerElementColor", &["Colors", "ColorIndex"][..]), ] { - if let Some(layer) = geom.children_named(element).min_by_key(|e| e.props.first().and_then(Prop::as_i64) != Some(0)) { - if names.iter().filter_map(|n| layer.child(n)).any(|r| !valid(r, false)) { - return Err(malformed(KIND, "mesh array contains a non-numeric or non-finite value")); - } + if let Some(layer) = geom.children_named(element).min_by_key(|e| e.props.first().and_then(Prop::as_i64) != Some(0)) + && names.iter().filter_map(|n| layer.child(n)).any(|r| !valid(r, false)) + { + return Err(malformed(KIND, "mesh array contains a non-numeric or non-finite value")); } } } From eeaa77c08162964474fa31e27c990494857e2901 Mon Sep 17 00:00:00 2001 From: chasse20 Date: Sun, 15 Nov 2026 13:54:31 -0500 Subject: [PATCH 121/150] Share bounded DDS float surface decoding Adapt MaterialCraft DDS backend integration from cc052aeaa8c61fbe6975674b6d5e28bb3c597b9d; retain float precision and first-surface behavior, keep transfer policy app-local. --- Cargo.lock | 36 ++++++++++++++++ crates/texture/Cargo.toml | 9 ++++ crates/texture/src/dds.rs | 87 +++++++++++++++++++++++++++++++++++++++ crates/texture/src/lib.rs | 3 ++ 4 files changed, 135 insertions(+) create mode 100644 crates/texture/src/dds.rs diff --git a/Cargo.lock b/Cargo.lock index b3325f790..087416480 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -340,6 +340,12 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "astc-decode" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d6058e6202e20b7fa445692ec4b77ca8eb02508091fcd479ea753d9f60e376b" + [[package]] name = "async-broadcast" version = "0.7.2" @@ -2758,6 +2764,8 @@ dependencies = [ name = "craft-texture" version = "0.1.0" dependencies = [ + "bytemuck", + "dds", "rayon", "thiserror 2.0.21", ] @@ -3295,6 +3303,19 @@ dependencies = [ "generic-array", ] +[[package]] +name = "dds" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e917a58e17b27d2f15626c490754ccced7f46592dc5b5f39a3013b89cba5f068" +dependencies = [ + "astc-decode", + "bitflags 2.13.2", + "glam", + "resize", + "zerocopy", +] + [[package]] name = "der" version = "0.7.10" @@ -4543,6 +4564,12 @@ dependencies = [ "xml-rs", ] +[[package]] +name = "glam" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "151665d9be52f9bb40fc7966565d39666f2d1e69233571b71b87791c7e0528b3" + [[package]] name = "glifo" version = "0.1.1" @@ -8136,6 +8163,15 @@ dependencies = [ "web-sys", ] +[[package]] +name = "resize" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71725ecd5e0197b54fe859055b108688472ab6a358f8fbe5cee4a556b1b5bfea" +dependencies = [ + "rgb", +] + [[package]] name = "resvg" version = "0.45.1" diff --git a/crates/texture/Cargo.toml b/crates/texture/Cargo.toml index 78868dd87..173abb0b0 100644 --- a/crates/texture/Cargo.toml +++ b/crates/texture/Cargo.toml @@ -9,6 +9,7 @@ repository.workspace = true [features] default = [] +dds = ["dep:dds", "dep:bytemuck"] parallel = ["dep:rayon"] [lints] @@ -17,3 +18,11 @@ workspace = true [dependencies] rayon = { workspace = true, optional = true } thiserror = { workspace = true } + +# Same decoder used by MaterialCraft; no parallel backend enabled. +dds = { version = "0.2.0", default-features = false, optional = true } +bytemuck = { workspace = true, optional = true } + +[dev-dependencies] +dds = { version = "0.2.0", default-features = false } +bytemuck = { workspace = true } diff --git a/crates/texture/src/dds.rs b/crates/texture/src/dds.rs new file mode 100644 index 000000000..cbada515e --- /dev/null +++ b/crates/texture/src/dds.rs @@ -0,0 +1,87 @@ +//! First-surface DDS decoding to unmodified RGBA floats. +//! +//! Adapted from MaterialCraft `dds_image.rs` (originally chasse20, cc052aeaa8c61fbe6975674b6d5e28bb3c597b9d). +//! Uses the same `dds` backend and first-surface semantics, including the first cube face or +//! volume slice. This is not a whole-resource decoder. Transfer functions and image policy +//! belong to callers. [`Limits`] bounds the output float plane, not backend scratch buffers. + +use crate::{ImageError, Kind, Limits}; + +#[derive(Debug, thiserror::Error)] +pub enum DecodeError { + #[error(transparent)] + Image(#[from] ImageError), + #[error("DDS decode failed: {0}")] + Decode(String), +} + +/// Unmodified backend output. Alpha and out-of-range float values are not clamped. +#[derive(Debug)] +pub struct Surface { + pub width: u32, + pub height: u32, + pub rgba: Vec, + /// Exactly the backend's `Precision::F32` classification, as used by MaterialCraft. + pub source_precision_is_f32: bool, +} + +/// Decode the first surface after validating dimensions, pixels and output-byte limits. +/// The output reservation is fallible; no second full-sized float plane is created. +pub fn decode_first(bytes: &[u8], limits: &Limits) -> Result { + let error = |e: ::dds::DecodingError| DecodeError::Decode(e.to_string()); + let mut decoder = ::dds::Decoder::new(std::io::Cursor::new(bytes)).map_err(error)?; + let size = decoder.main_size(); + let len = limits.check(size.width, size.height, Kind::Color)?; + let mut rgba = Vec::new(); + rgba.try_reserve_exact(len).map_err(|_| ImageError::OutOfMemory(size.width, size.height))?; + rgba.resize(len, 0.0_f32); + let view = ::dds::ImageViewMut::new(bytemuck::cast_slice_mut(&mut rgba), size, ::dds::ColorFormat::RGBA_F32) + .ok_or_else(|| DecodeError::Decode("invalid DDS output dimensions".into()))?; + decoder.read_surface(view).map_err(error)?; + Ok(Surface { width: size.width, height: size.height, rgba, source_precision_is_f32: decoder.format().precision() == ::dds::Precision::F32 }) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + + fn bytes(format: ::dds::Format, payload: &[u8]) -> Vec { + let mut bytes = Vec::new(); + ::dds::Header::new_image(1, 1, format).write(&mut bytes).unwrap(); + bytes.extend_from_slice(payload); + bytes + } + + #[test] + fn rgba8_keeps_transfer_and_alpha_unmodified() { + let data = bytes(::dds::Format::R8G8B8A8_UNORM, &[128, 64, 255, 32]); + let image = decode_first(&data, &Limits::default()).unwrap(); + assert_eq!((image.width, image.height), (1, 1)); + assert!(!image.source_precision_is_f32); + for (actual, expected) in image.rgba.iter().zip([128.0 / 255.0, 64.0 / 255.0, 1.0, 32.0 / 255.0]) { + assert!((actual - expected).abs() < 1e-6); + } + } + + #[test] + fn float_hdr_and_negative_values_survive() { + let values = [-0.5_f32, 2.0, 0.25, 0.75]; + let payload: Vec = values.iter().flat_map(|v| v.to_le_bytes()).collect(); + let image = decode_first(&bytes(::dds::Format::R32G32B32A32_FLOAT, &payload), &Limits::default()).unwrap(); + assert!(image.source_precision_is_f32); + assert_eq!(image.rgba, values); + } + + #[test] + fn checks_output_budget_and_truncation() { + let data = bytes(::dds::Format::R8G8B8A8_UNORM, &[1, 2, 3, 4]); + let mut limits = Limits::default(); + limits.max_bytes = 15; + assert!(matches!(decode_first(&data, &limits), Err(DecodeError::Image(ImageError::TooLarge(1, 1))))); + limits.max_bytes = 16; + assert!(decode_first(&data, &limits).is_ok()); + assert!(decode_first(&data[..data.len() - 1], &limits).is_err()); + assert!(decode_first(&[], &limits).is_err()); + } +} diff --git a/crates/texture/src/lib.rs b/crates/texture/src/lib.rs index 2aaef3e8b..e33b68d88 100644 --- a/crates/texture/src/lib.rs +++ b/crates/texture/src/lib.rs @@ -29,6 +29,9 @@ #![forbid(unsafe_code)] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#[cfg(any(feature = "dds", test))] +pub mod dds; + use std::sync::Arc; #[cfg(feature = "parallel")] From 279795518713551bc1486a09caa79e2438aebf2f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 13:55:19 -0500 Subject: [PATCH 122/150] Cover compressed half-float and first-face DDS behavior --- crates/texture/src/dds.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/texture/src/dds.rs b/crates/texture/src/dds.rs index cbada515e..6ba5569e8 100644 --- a/crates/texture/src/dds.rs +++ b/crates/texture/src/dds.rs @@ -73,11 +73,34 @@ mod tests { assert_eq!(image.rgba, values); } + #[test] + fn half_float_and_compressed_pixels_preserve_backend_values() { + let half: Vec = [0xb800u16, 0x4000, 0x3400, 0x3a00].iter().flat_map(|v| v.to_le_bytes()).collect(); + let image = decode_first(&bytes(::dds::Format::R16G16B16A16_FLOAT, &half), &Limits::default()).unwrap(); + assert_eq!(image.rgba, [-0.5, 2.0, 0.25, 0.75]); + assert_eq!(image.source_precision_is_f32, ::dds::Format::R16G16B16A16_FLOAT.precision() == ::dds::Precision::F32); + let mut compressed = Vec::new(); + ::dds::Header::new_image(4, 4, ::dds::Format::BC1_UNORM).write(&mut compressed).unwrap(); + compressed.extend_from_slice(&[0, 0xf8, 0, 0, 0, 0, 0, 0]); + let image = decode_first(&compressed, &Limits::default()).unwrap(); + assert!(!image.source_precision_is_f32); + assert!(image.rgba.as_chunks::<4>().0.iter().all(|p| *p == [1.0, 0.0, 0.0, 1.0])); + } + + #[test] + fn cube_keeps_first_surface_only_like_materialcraft() { + let mut data = Vec::new(); + ::dds::Header::new_cube_map(1, 1, ::dds::Format::R8G8B8A8_UNORM).write(&mut data).unwrap(); + data.extend_from_slice(&[255, 0, 0, 255]); + data.extend_from_slice(&[0; 20]); + let image = decode_first(&data, &Limits::default()).unwrap(); + assert_eq!(image.rgba, [1.0, 0.0, 0.0, 1.0]); + } + #[test] fn checks_output_budget_and_truncation() { let data = bytes(::dds::Format::R8G8B8A8_UNORM, &[1, 2, 3, 4]); - let mut limits = Limits::default(); - limits.max_bytes = 15; + let mut limits = Limits { max_bytes: 15, ..Limits::default() }; assert!(matches!(decode_first(&data, &limits), Err(DecodeError::Image(ImageError::TooLarge(1, 1))))); limits.max_bytes = 16; assert!(decode_first(&data, &limits).is_ok()); From 27b1134e23a32cd6fb5aac2ff24ae711ffacbf02 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:57:27 +0000 Subject: [PATCH 123/150] Share Opus FFT and CELT allocation primitives --- crates/audio/src/opus/decode/celt/mdct.rs | 136 +------ crates/audio/src/opus/decode/celt/rate.rs | 346 +---------------- crates/audio/src/opus/decode/celt/tables.rs | 2 + .../audio/src/opus/decode/silk/resampler.rs | 20 +- crates/audio/src/opus/dot.rs | 20 + crates/audio/src/opus/encoder/mdct.rs | 129 +------ crates/audio/src/opus/encoder/mod.rs | 2 +- crates/audio/src/opus/encoder/rate.rs | 332 +--------------- crates/audio/src/opus/encoder/resample.rs | 19 +- crates/audio/src/opus/fft.rs | 130 +++++++ crates/audio/src/opus/mod.rs | 8 +- crates/audio/src/opus/rate_common.rs | 362 ++++++++++++++++++ 12 files changed, 551 insertions(+), 955 deletions(-) create mode 100644 crates/audio/src/opus/dot.rs create mode 100644 crates/audio/src/opus/fft.rs create mode 100644 crates/audio/src/opus/rate_common.rs diff --git a/crates/audio/src/opus/decode/celt/mdct.rs b/crates/audio/src/opus/decode/celt/mdct.rs index b3bfcf0f7..7503d9ebe 100644 --- a/crates/audio/src/opus/decode/celt/mdct.rs +++ b/crates/audio/src/opus/decode/celt/mdct.rs @@ -4,141 +4,7 @@ //! producing the N/2 "folded" time samples; the window overlap region is then unfolded against the //! folded tail of the previous block, which the caller keeps in place in the output buffer. -#[derive(Clone, Copy, Default, Debug)] -pub struct Cpx { - pub re: f32, - pub im: f32, -} - -impl Cpx { - #[inline] - fn mul(self, o: Cpx) -> Cpx { - Cpx { re: self.re * o.re - self.im * o.im, im: self.re * o.im + self.im * o.re } - } - #[inline] - fn add(self, o: Cpx) -> Cpx { - Cpx { re: self.re + o.re, im: self.im + o.im } - } - #[inline] - fn sub(self, o: Cpx) -> Cpx { - Cpx { re: self.re - o.re, im: self.im - o.im } - } -} - -/// Mixed-radix (2, 3, 4, 5) forward complex FFT, unscaled. Radix 2, 3 and 4 use dedicated -/// butterflies; radix 5 uses a generic one. -pub struct Fft { - n: usize, - factors: Vec, - twiddle: Vec, -} - -impl Fft { - pub fn new(n: usize) -> Fft { - let mut factors = Vec::new(); - let mut m = n; - for p in [4usize, 2, 3, 5] { - while m.is_multiple_of(p) { - factors.push(p); - m /= p; - } - } - assert_eq!(m, 1, "unsupported FFT size {n}"); - let twiddle = (0..n) - .map(|k| { - let ph = -2.0 * std::f64::consts::PI * k as f64 / n as f64; - Cpx { re: ph.cos() as f32, im: ph.sin() as f32 } - }) - .collect(); - Fft { n, factors, twiddle } - } - - pub fn process(&self, data: &mut [Cpx], scratch: &mut Vec) { - scratch.clear(); - scratch.extend_from_slice(&data[..self.n]); - self.rec(&mut data[..self.n], scratch, 1, self.n, 0, 1); - } - - fn rec(&self, out: &mut [Cpx], inp: &[Cpx], in_stride: usize, n: usize, fi: usize, tw_stride: usize) { - let p = self.factors[fi]; - let m = n / p; - if m == 1 { - for (i, o) in out.iter_mut().enumerate().take(p) { - *o = inp[i * in_stride]; - } - } else { - for q in 0..p { - self.rec(&mut out[q * m..(q + 1) * m], &inp[q * in_stride..], in_stride * p, m, fi + 1, tw_stride * p); - } - } - let tw = &self.twiddle; - match p { - 2 => { - for k in 0..m { - let a0 = out[k]; - let a1 = out[m + k].mul(tw[k * tw_stride]); - out[k] = a0.add(a1); - out[m + k] = a0.sub(a1); - } - } - 4 => { - for k in 0..m { - let t0 = out[k]; - let t1 = out[m + k].mul(tw[k * tw_stride]); - let t2 = out[2 * m + k].mul(tw[2 * k * tw_stride]); - let t3 = out[3 * m + k].mul(tw[3 * k * tw_stride]); - let s0 = t0.add(t2); - let d0 = t0.sub(t2); - let s1 = t1.add(t3); - let d1 = t1.sub(t3); - out[k] = s0.add(s1); - out[2 * m + k] = s0.sub(s1); - // -j * d1 = (d1.im, -d1.re) - out[m + k] = Cpx { re: d0.re + d1.im, im: d0.im - d1.re }; - out[3 * m + k] = Cpx { re: d0.re - d1.im, im: d0.im + d1.re }; - } - } - 3 => { - // sin(-2*pi/3) = -sqrt(3)/2 - let h = tw[self.n / 3].im; - for k in 0..m { - let t0 = out[k]; - let t1 = out[m + k].mul(tw[k * tw_stride]); - let t2 = out[2 * m + k].mul(tw[2 * k * tw_stride]); - let s = t1.add(t2); - let d = t1.sub(t2); - let a = Cpx { re: t0.re - 0.5 * s.re, im: t0.im - 0.5 * s.im }; - out[k] = t0.add(s); - // j * h * d = (-h * d.im, h * d.re) - out[m + k] = Cpx { re: a.re - h * d.im, im: a.im + h * d.re }; - out[2 * m + k] = Cpx { re: a.re + h * d.im, im: a.im - h * d.re }; - } - } - _ => { - // Radix 5 (at most once per transform): generic butterfly with the five roots of - // unity looked up once. - let mut w5 = [Cpx::default(); 5]; - for (q, w) in w5.iter_mut().enumerate() { - *w = tw[q * (self.n / p)]; - } - let mut t = [Cpx::default(); 5]; - for k in 0..m { - t[0] = out[k]; - for q in 1..p { - t[q] = out[q * m + k].mul(tw[q * k * tw_stride]); - } - for q2 in 0..p { - let mut acc = t[0]; - for q in 1..p { - acc = acc.add(t[q].mul(w5[(q * q2) % p])); - } - out[q2 * m + k] = acc; - } - } - } - } - } -} +use crate::opus::fft::{Cpx, Fft}; pub struct Imdct { /// Full MDCT length (2x the number of coefficients). diff --git a/crates/audio/src/opus/decode/celt/rate.rs b/crates/audio/src/opus/decode/celt/rate.rs index 782d68d5c..29e8566c1 100644 --- a/crates/audio/src/opus/decode/celt/rate.rs +++ b/crates/audio/src/opus/decode/celt/rate.rs @@ -1,272 +1,45 @@ //! Pulse cache and bit allocation (RFC 6716 §4.3.3, §4.3.4.1). -use super::tables::{ALLOC, EBANDS, LOG2_FRAC_TABLE, MAX_LM, NB_EBANDS, OVERLAP}; +use super::tables::{EBANDS, LOG2_FRAC_TABLE, MAX_LM, NB_EBANDS, OVERLAP}; use crate::opus::decode::range::{BITRES, RangeDecoder}; -const MAX_PSEUDO: i32 = 40; -const LOG_MAX_PSEUDO: i32 = 6; +pub use crate::opus::rate_common::get_pulses; +#[cfg(test)] +use crate::opus::rate_common::log2_frac; +#[cfg(test)] +pub use crate::opus::rate_common::pvq_v; pub const MAX_FINE_BITS: i32 = 8; const FINE_OFFSET: i32 = 21; pub const QTHETA_OFFSET: i32 = 4; pub const QTHETA_OFFSET_TWOPHASE: i32 = 16; const ALLOC_STEPS: i32 = 6; -/// Number of pulses represented by pseudo-pulse index `i`. -pub fn get_pulses(i: i32) -> i32 { - if i < 8 { i } else { (8 + (i & 7)) << ((i >> 3) - 1) } -} - -/// Conservative (rounded up) log2 of `val` with `frac` fractional bits. -pub fn log2_frac(mut val: u32, mut frac: u32) -> i32 { - let mut l = crate::opus::decode::range::ilog(val); - if val & val.wrapping_sub(1) != 0 { - if l > 16 { - val = ((val - 1) >> (l - 16)) + 1; - } else { - val <<= 16 - l; - } - let mut l = (l - 1) << frac; - loop { - let b = (val >> 16) as i32; - l += b << frac; - val = (val + b as u32) >> b; - val = (val * val + 0x7FFF) >> 15; - if frac == 0 { - break; - } - frac -= 1; - } - l + (val > 0x8000) as i32 - } else { - l -= 1; - l << frac - } -} - -/// PVQ codebook size V(N, K) as u128 (saturating far above 2^32). -pub fn pvq_v(n: usize, k: usize) -> u128 { - // V(N,K) = V(N-1,K) + V(N,K-1) + V(N-1,K-1); computed row by row. - if k == 0 { - return 1; - } - if n == 0 { - return 0; - } - let mut row = vec![0u128; k + 1]; // V(0, k) - row[0] = 1; - for _ in 0..n { - let mut prev_diag = row[0]; // V(n-1, 0) - // V(n, 0) = 1 - for kk in 1..=k { - let up = row[kk]; // V(n-1, kk) - let v = up.saturating_add(row[kk - 1]).saturating_add(prev_diag); - prev_diag = up; - row[kk] = v.min(u128::MAX >> 8); - } - row[0] = 1; - } - row[k] -} - -fn fits_in32(n: usize, k: i32) -> bool { - pvq_v(n, k as usize) <= u32::MAX as u128 -} - -/// Precomputed per-mode data shared by all CELT decoders. +/// Decoder mode: shared allocation tables and decoder-specific inverse transforms. pub struct Mode { - /// log2 of band width in 1/8 bits. - pub log_n: [i32; NB_EBANDS], - /// Index into `cache_bits` for `(LM+1)*NB_EBANDS + band` (-1 when unused). - pub cache_index: Vec, - /// `cache[0]` = max pseudo-pulses; `cache[k]` = bits(k) - 1 in 1/8 bits. - pub cache_bits: Vec, - /// Per-band maximum allocation: `[(2*LM + C-1)*NB_EBANDS + band]`. - pub caps: Vec, + core: crate::opus::rate_common::Mode, pub window: [f32; OVERLAP], pub mdct: [super::mdct::Imdct; MAX_LM + 1], } - impl Mode { - pub fn new() -> Mode { - let mut log_n = [0i32; NB_EBANDS]; - for (i, l) in log_n.iter_mut().enumerate() { - *l = log2_frac((EBANDS[i + 1] - EBANDS[i]) as u32, BITRES); - } - let (cache_index, cache_bits) = compute_pulse_cache(MAX_LM as i32); - let mut m = Mode { - log_n, - cache_index, - cache_bits, - caps: Vec::new(), + pub fn new() -> Self { + let mut mode = Self { + core: crate::opus::rate_common::Mode::new(), window: [0.0; OVERLAP], mdct: [super::mdct::Imdct::new(1920), super::mdct::Imdct::new(960), super::mdct::Imdct::new(480), super::mdct::Imdct::new(240)], }; - m.caps = compute_caps(&m); for i in 0..OVERLAP { let x = std::f64::consts::FRAC_PI_2 * (i as f64 + 0.5) / OVERLAP as f64; let s = x.sin(); - m.window[i] = (std::f64::consts::FRAC_PI_2 * s * s).sin() as f32; + mode.window[i] = (std::f64::consts::FRAC_PI_2 * s * s).sin() as f32; } - m - } - - #[inline] - pub fn cache(&self, lm: i32, band: usize) -> &[u8] { - let idx = self.cache_index[((lm + 1) as usize) * NB_EBANDS + band]; - &self.cache_bits[idx.max(0) as usize..] - } - - pub fn bits2pulses(&self, band: usize, lm: i32, bits: i32) -> i32 { - let cache = self.cache(lm, band); - let mut lo = 0i32; - let mut hi = cache[0] as i32; - let bits = bits - 1; - for _ in 0..LOG_MAX_PSEUDO { - let mid = (lo + hi + 1) >> 1; - if cache[mid as usize] as i32 >= bits { - hi = mid; - } else { - lo = mid; - } - } - let lo_bits = if lo == 0 { -1 } else { cache[lo as usize] as i32 }; - if bits - lo_bits <= cache[hi as usize] as i32 - bits { lo } else { hi } - } - - pub fn pulses2bits(&self, band: usize, lm: i32, pulses: i32) -> i32 { - if pulses == 0 { 0 } else { self.cache(lm, band)[pulses as usize] as i32 + 1 } - } - - /// Per-band allocation caps for this frame (`init_caps`). - pub fn init_caps(&self, lm: usize, c: usize) -> [i32; NB_EBANDS] { - let mut cap = [0i32; NB_EBANDS]; - for (i, cp) in cap.iter_mut().enumerate() { - let n = (EBANDS[i + 1] - EBANDS[i]) << lm; - *cp = ((self.caps[NB_EBANDS * (2 * lm + c - 1) + i] as i32 + 64) * c as i32 * n) >> 2; - } - cap + mode } } - -fn compute_pulse_cache(lm_max: i32) -> (Vec, Vec) { - let nb = NB_EBANDS; - let rows = (lm_max + 2) as usize; - let mut index = vec![-1i16; nb * rows]; - let mut entries: Vec<(usize, i32, usize)> = Vec::new(); // (N, K, offset) - let mut curr = 0usize; - for i in 0..rows { - for j in 0..nb { - let n = (((EBANDS[j + 1] - EBANDS[j]) << i) >> 1) as usize; - index[i * nb + j] = -1; - // Reuse the entry of an earlier band with the same size. - 'search: for k in 0..=i { - for nn in 0..nb { - if k == i && nn >= j { - break; - } - if n == (((EBANDS[nn + 1] - EBANDS[nn]) << k) >> 1) as usize { - index[i * nb + j] = index[k * nb + nn]; - break 'search; - } - } - } - if index[i * nb + j] == -1 && n != 0 { - let mut k = 0; - while k < MAX_PSEUDO && fits_in32(n, get_pulses(k + 1)) { - k += 1; - } - index[i * nb + j] = curr as i16; - entries.push((n, k, curr)); - curr += (k + 1) as usize; - } - } +impl std::ops::Deref for Mode { + type Target = crate::opus::rate_common::Mode; + fn deref(&self) -> &Self::Target { + &self.core } - let mut bits = vec![0u8; curr]; - for &(n, k, off) in &entries { - let maxk = get_pulses(k) as usize; - let tmp = required_bits(n, maxk, BITRES); - for j in 1..=k { - bits[off + j as usize] = (tmp[get_pulses(j) as usize] - 1) as u8; - } - bits[off] = k as u8; - } - (index, bits) -} - -/// Bits (1/2^frac units, rounded up) needed for each K in `0..=maxk` for dimension `n`. -fn required_bits(n: usize, maxk: usize, frac: u32) -> Vec { - let mut out = vec![0i32; maxk + 1]; - if n == 1 { - for o in out.iter_mut().skip(1) { - *o = 1 << frac; - } - } else { - for (k, o) in out.iter_mut().enumerate().skip(1) { - let v = pvq_v(n, k); - *o = log2_frac(v as u32, frac); - } - } - out -} - -fn compute_caps(m: &Mode) -> Vec { - let nb = NB_EBANDS; - let mut caps = vec![0u8; (MAX_LM + 1) * 2 * nb]; - for i in 0..=MAX_LM as i32 { - for c in 1..=2i32 { - for j in 0..nb { - let mut n0 = EBANDS[j + 1] - EBANDS[j]; - let max_bits = if n0 << i == 1 { - (c * (1 + MAX_FINE_BITS)) << BITRES - } else { - let mut lm0 = 0; - if n0 > 2 { - n0 >>= 1; - lm0 -= 1; - } else if n0 <= 1 { - lm0 = i.min(1); - n0 <<= lm0; - } - let pcache = m.cache(lm0, j); - let mut mb = pcache[pcache[0] as usize] as i32 + 1; - let mut n = n0; - for k in 0..(i - lm0) { - mb <<= 1; - let offset = ((m.log_n[j] + ((lm0 + k) << BITRES)) >> 1) - QTHETA_OFFSET; - let num = 459 * ((2 * n - 1) * offset + mb); - let den = ((2 * n - 1) << 9) - 459; - let qb = ((num + (den >> 1)) / den).min(57); - mb += qb; - n <<= 1; - } - if c == 2 { - mb <<= 1; - let offset = ((m.log_n[j] + (i << BITRES)) >> 1) - if n == 2 { QTHETA_OFFSET_TWOPHASE } else { QTHETA_OFFSET }; - let ndof = 2 * n - 1 - (n == 2) as i32; - let p = if n == 2 { 512 } else { 487 }; - let num = p * (mb + ndof * offset); - let den = (ndof << 9) - p; - let qb = ((num + (den >> 1)) / den).min(if n == 2 { 64 } else { 61 }); - mb += qb; - } - let ndof = c * n + if c == 2 && n > 2 { 1 } else { 0 }; - let mut offset = ((m.log_n[j] + (i << BITRES)) >> 1) - FINE_OFFSET; - if n == 2 { - offset += (1 << BITRES) >> 2; - } - let num = mb + ndof * offset; - let den = (ndof - 1) << BITRES; - let qb = ((num + (den >> 1)) / den).min(MAX_FINE_BITS); - mb += (c * qb) << BITRES; - mb - }; - let w = c * ((EBANDS[j + 1] - EBANDS[j]) << i); - let v = (4 * max_bits / w) - 64; - caps[((i * 2 + c - 1) as usize) * nb + j] = v.clamp(0, 255) as u8; - } - } - } - caps } /// Result of the allocation process. @@ -296,89 +69,8 @@ pub struct AllocationInput<'a, 'd> { pub fn compute_allocation(input: AllocationInput<'_, '_>) -> Allocation { let AllocationInput { mode: m, start, end, offsets, cap, trim: alloc_trim, total, channels: c, lm, decoder: dec } = input; - let ci = c as i32; - let lmi = lm as i32; - let mut total = total.max(0); - let mut skip_start = start; - let skip_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 }; - total -= skip_rsv; - let mut intensity_rsv = 0; - let mut dual_stereo_rsv = 0; - if c == 2 { - intensity_rsv = LOG2_FRAC_TABLE[end - start]; - if intensity_rsv > total { - intensity_rsv = 0; - } else { - total -= intensity_rsv; - dual_stereo_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 }; - total -= dual_stereo_rsv; - } - } - let mut bits1 = [0i32; NB_EBANDS]; - let mut bits2 = [0i32; NB_EBANDS]; - let mut thresh = [0i32; NB_EBANDS]; - let mut trim_offset = [0i32; NB_EBANDS]; - for j in start..end { - let w = EBANDS[j + 1] - EBANDS[j]; - thresh[j] = (ci << BITRES).max(((3 * w) << lmi << BITRES) >> 4); - trim_offset[j] = (ci * w * (alloc_trim - 5 - lmi) * (end as i32 - j as i32 - 1) * (1 << (lmi + BITRES as i32))) >> 6; - if w << lmi == 1 { - trim_offset[j] -= ci << BITRES; - } - } - let nb_alloc = ALLOC.len() as i32; - let mut lo = 1; - let mut hi = nb_alloc - 1; - loop { - let mut done = false; - let mut psum = 0; - let mid = (lo + hi) >> 1; - for j in (start..end).rev() { - let n = EBANDS[j + 1] - EBANDS[j]; - let mut bitsj = (ci * n * (ALLOC[mid as usize][j] as i32)) << lmi >> 2; - if bitsj > 0 { - bitsj = (bitsj + trim_offset[j]).max(0); - } - bitsj += offsets[j]; - if bitsj >= thresh[j] || done { - done = true; - psum += bitsj.min(cap[j]); - } else if bitsj >= ci << BITRES { - psum += ci << BITRES; - } - } - if psum > total { - hi = mid - 1; - } else { - lo = mid + 1; - } - if lo > hi { - break; - } - } - hi = lo; - lo -= 1; - for j in start..end { - let n = EBANDS[j + 1] - EBANDS[j]; - let mut bits1j = (ci * n * (ALLOC[lo as usize][j] as i32)) << lmi >> 2; - let mut bits2j = if hi >= nb_alloc { cap[j] } else { (ci * n * (ALLOC[hi as usize][j] as i32)) << lmi >> 2 }; - if bits1j > 0 { - bits1j = (bits1j + trim_offset[j]).max(0); - } - if bits2j > 0 { - bits2j = (bits2j + trim_offset[j]).max(0); - } - if lo > 0 { - bits1j += offsets[j]; - } - bits2j += offsets[j]; - if offsets[j] > 0 { - skip_start = j; - } - bits2j = (bits2j - bits1j).max(0); - bits1[j] = bits1j; - bits2[j] = bits2j; - } + let crate::opus::rate_common::Bracket { skip_start, bits1, bits2, thresh, total, skip_rsv, intensity_rsv, dual_stereo_rsv } = + crate::opus::rate_common::bracket(start, end, offsets, cap, alloc_trim, total, c, lm); interp_bits2pulses(InterpPulsesInput { mode: m, start, diff --git a/crates/audio/src/opus/decode/celt/tables.rs b/crates/audio/src/opus/decode/celt/tables.rs index f2381f714..b65d05c09 100644 --- a/crates/audio/src/opus/decode/celt/tables.rs +++ b/crates/audio/src/opus/decode/celt/tables.rs @@ -14,6 +14,7 @@ pub const MAX_LM: usize = 3; pub const PREEMPH: f32 = 0.850_006_1; /// Static allocation table (Table 57), indexed `[quality][band]`, in 1/32 bit per MDCT bin. +#[cfg(not(feature = "opus-encode"))] pub const ALLOC: [[u8; NB_EBANDS]; 11] = transpose([ [0, 90, 110, 118, 126, 134, 144, 152, 162, 172, 200], [0, 80, 100, 110, 119, 127, 137, 145, 155, 165, 200], @@ -38,6 +39,7 @@ pub const ALLOC: [[u8; NB_EBANDS]; 11] = transpose([ [0, 0, 0, 0, 0, 1, 1, 1, 1, 20, 104], ]); +#[cfg(not(feature = "opus-encode"))] const fn transpose(t: [[u8; 11]; NB_EBANDS]) -> [[u8; NB_EBANDS]; 11] { let mut out = [[0u8; NB_EBANDS]; 11]; let mut b = 0; diff --git a/crates/audio/src/opus/decode/silk/resampler.rs b/crates/audio/src/opus/decode/silk/resampler.rs index 0671778cc..7d3f803ef 100644 --- a/crates/audio/src/opus/decode/silk/resampler.rs +++ b/crates/audio/src/opus/decode/silk/resampler.rs @@ -4,25 +4,7 @@ //! RFC 6716 Table 54 (and the integer delays used when no rate conversion is needed), so that the //! SILK output lines up with the CELT layer in Hybrid mode. -/// Dot product with eight independent partial sums, so the additions pipeline and the loop -/// vectorises instead of waiting on one 4-cycle add chain. Rounds differently from a -/// sequential sum (the resampler is not normative). -#[inline] -fn dot8(a: &[f32], b: &[f32]) -> f32 { - let mut s = [0f32; 8]; - let (ca, ra) = a.as_chunks::<8>(); - let (cb, rb) = b.as_chunks::<8>(); - for (x, y) in ca.iter().zip(cb) { - for l in 0..8 { - s[l] += x[l] * y[l]; - } - } - let mut acc = ((s[0] + s[4]) + (s[1] + s[5])) + ((s[2] + s[6]) + (s[3] + s[7])); - for (x, y) in ra.iter().zip(rb) { - acc += x * y; - } - acc -} +use crate::opus::dot::dot8; #[derive(Clone)] pub struct Resampler { diff --git a/crates/audio/src/opus/dot.rs b/crates/audio/src/opus/dot.rs new file mode 100644 index 000000000..536209e0a --- /dev/null +++ b/crates/audio/src/opus/dot.rs @@ -0,0 +1,20 @@ +//! Shared fixed-order FIR dot product. + +/// Dot product with eight independent partial sums, which the compiler can vectorise (a single +/// running sum is bound by the latency of the float addition). The sums are combined pairwise. +#[inline] +pub(super) fn dot8(a: &[f32], b: &[f32]) -> f32 { + let mut s = [0f32; 8]; + let (ca, ra) = a.as_chunks::<8>(); + let (cb, rb) = b.as_chunks::<8>(); + for (x, y) in ca.iter().zip(cb) { + for l in 0..8 { + s[l] += x[l] * y[l]; + } + } + let mut acc = ((s[0] + s[4]) + (s[1] + s[5])) + ((s[2] + s[6]) + (s[3] + s[7])); + for (x, y) in ra.iter().zip(rb) { + acc += x * y; + } + acc +} diff --git a/crates/audio/src/opus/encoder/mdct.rs b/crates/audio/src/opus/encoder/mdct.rs index fa5624514..7cac21d93 100644 --- a/crates/audio/src/opus/encoder/mdct.rs +++ b/crates/audio/src/opus/encoder/mdct.rs @@ -9,134 +9,7 @@ use crate::opus::encoder::tables::OVERLAP; -#[derive(Clone, Copy, Default, Debug)] -pub struct Cpx { - pub re: f32, - pub im: f32, -} - -impl Cpx { - #[inline] - fn mul(self, o: Cpx) -> Cpx { - Cpx { re: self.re * o.re - self.im * o.im, im: self.re * o.im + self.im * o.re } - } - #[inline] - fn add(self, o: Cpx) -> Cpx { - Cpx { re: self.re + o.re, im: self.im + o.im } - } - #[inline] - fn sub(self, o: Cpx) -> Cpx { - Cpx { re: self.re - o.re, im: self.im - o.im } - } -} - -/// Mixed-radix (2, 3, 4, 5) forward complex FFT, unscaled (decimation in time, recursive). -pub struct Fft { - n: usize, - factors: Vec, - twiddle: Vec, -} - -impl Fft { - pub fn new(n: usize) -> Fft { - let mut factors = Vec::new(); - let mut m = n; - for p in [4usize, 2, 3, 5] { - while m.is_multiple_of(p) { - factors.push(p); - m /= p; - } - } - assert_eq!(m, 1, "unsupported FFT size {n}"); - let twiddle = (0..n) - .map(|k| { - let ph = -2.0 * std::f64::consts::PI * k as f64 / n as f64; - Cpx { re: ph.cos() as f32, im: ph.sin() as f32 } - }) - .collect(); - Fft { n, factors, twiddle } - } - - pub fn process(&self, data: &mut [Cpx], scratch: &mut Vec) { - scratch.clear(); - scratch.extend_from_slice(&data[..self.n]); - self.rec(&mut data[..self.n], scratch, 1, self.n, 0, 1); - } - - fn rec(&self, out: &mut [Cpx], inp: &[Cpx], in_stride: usize, n: usize, fi: usize, tw_stride: usize) { - let p = self.factors[fi]; - let m = n / p; - if m == 1 { - for (i, o) in out.iter_mut().enumerate().take(p) { - *o = inp[i * in_stride]; - } - } else { - for q in 0..p { - self.rec(&mut out[q * m..(q + 1) * m], &inp[q * in_stride..], in_stride * p, m, fi + 1, tw_stride * p); - } - } - // The twiddle index q * k * tw_stride is always below n, so no modulo is needed. - let tw = &self.twiddle; - match p { - 2 => { - for k in 0..m { - let a0 = out[k]; - let a1 = out[m + k].mul(tw[k * tw_stride]); - out[k] = a0.add(a1); - out[m + k] = a0.sub(a1); - } - } - 4 => { - for k in 0..m { - let t0 = out[k]; - let t1 = out[m + k].mul(tw[k * tw_stride]); - let t2 = out[2 * m + k].mul(tw[2 * k * tw_stride]); - let t3 = out[3 * m + k].mul(tw[3 * k * tw_stride]); - let (s0, d0) = (t0.add(t2), t0.sub(t2)); - let (s1, d1) = (t1.add(t3), t1.sub(t3)); - out[k] = s0.add(s1); - out[2 * m + k] = s0.sub(s1); - // The odd outputs combine d0 with -j * d1 = (d1.im, -d1.re) and its negative. - out[m + k] = Cpx { re: d0.re + d1.im, im: d0.im - d1.re }; - out[3 * m + k] = Cpx { re: d0.re - d1.im, im: d0.im + d1.re }; - } - } - 3 => { - // Imaginary part of exp(-2 pi i / 3) = -sqrt(3) / 2. - let h = tw[self.n / 3].im; - for k in 0..m { - let t0 = out[k]; - let t1 = out[m + k].mul(tw[k * tw_stride]); - let t2 = out[2 * m + k].mul(tw[2 * k * tw_stride]); - let (s, d) = (t1.add(t2), t1.sub(t2)); - let a = Cpx { re: t0.re - 0.5 * s.re, im: t0.im - 0.5 * s.im }; - out[k] = t0.add(s); - // j * h * d = (-h * d.im, h * d.re) - out[m + k] = Cpx { re: a.re - h * d.im, im: a.im + h * d.re }; - out[2 * m + k] = Cpx { re: a.re + h * d.im, im: a.im - h * d.re }; - } - } - _ => { - // Generic DFT butterfly (radix 5, once per transform). - let roots: [Cpx; 5] = std::array::from_fn(|q| if q < p { tw[q * (self.n / p)] } else { Cpx::default() }); - let mut t = [Cpx::default(); 5]; - for k in 0..m { - t[0] = out[k]; - for q in 1..p { - t[q] = out[q * m + k].mul(tw[q * k * tw_stride]); - } - for q2 in 0..p { - let mut acc = t[0]; - for q in 1..p { - acc = acc.add(t[q].mul(roots[(q * q2) % p])); - } - out[q2 * m + k] = acc; - } - } - } - } - } -} +use crate::opus::fft::{Cpx, Fft}; /// Forward MDCT producing `n` coefficients from a block of `n + OVERLAP` samples. pub struct Mdct { diff --git a/crates/audio/src/opus/encoder/mod.rs b/crates/audio/src/opus/encoder/mod.rs index a635cce40..8a3df07f1 100644 --- a/crates/audio/src/opus/encoder/mod.rs +++ b/crates/audio/src/opus/encoder/mod.rs @@ -38,7 +38,7 @@ mod range; mod rate; mod resample; mod silk; -mod tables; +pub(super) mod tables; use std::sync::OnceLock; diff --git a/crates/audio/src/opus/encoder/rate.rs b/crates/audio/src/opus/encoder/rate.rs index 58349bbd7..3eb9575d4 100644 --- a/crates/audio/src/opus/encoder/rate.rs +++ b/crates/audio/src/opus/encoder/rate.rs @@ -5,254 +5,17 @@ //! difference is that the encoder *writes* the band-skip, intensity and dual-stereo symbols. use crate::opus::encoder::CeltFrame; -use crate::opus::encoder::range::{BITRES, RangeEncoder, ilog}; -use crate::opus::encoder::tables::{ALLOC, EBANDS, LOG2_FRAC_TABLE, MAX_LM, NB_EBANDS}; +use crate::opus::encoder::range::{BITRES, RangeEncoder}; +use crate::opus::encoder::tables::{EBANDS, LOG2_FRAC_TABLE, NB_EBANDS}; -const MAX_PSEUDO: i32 = 40; -const LOG_MAX_PSEUDO: i32 = 6; +#[cfg(test)] +pub use crate::opus::rate_common::pvq_v; +pub use crate::opus::rate_common::{Mode, get_pulses}; pub const MAX_FINE_BITS: i32 = 8; const FINE_OFFSET: i32 = 21; pub const QTHETA_OFFSET: i32 = 4; -pub const QTHETA_OFFSET_TWOPHASE: i32 = 16; const ALLOC_STEPS: i32 = 6; -/// Number of pulses represented by pseudo-pulse index `i`. -pub fn get_pulses(i: i32) -> i32 { - if i < 8 { i } else { (8 + (i & 7)) << ((i >> 3) - 1) } -} - -/// Conservative (rounded up) log2 of `val` with `frac` fractional bits. -pub fn log2_frac(mut val: u32, mut frac: u32) -> i32 { - let mut l = ilog(val); - if val & val.wrapping_sub(1) != 0 { - if l > 16 { - val = ((val - 1) >> (l - 16)) + 1; - } else { - val <<= 16 - l; - } - let mut l = (l - 1) << frac; - loop { - let b = (val >> 16) as i32; - l += b << frac; - val = (val + b as u32) >> b; - val = (val * val + 0x7FFF) >> 15; - if frac == 0 { - break; - } - frac -= 1; - } - l + (val > 0x8000) as i32 - } else { - l -= 1; - l << frac - } -} - -/// PVQ codebook size V(N, K) (saturating far above 2^32). -pub fn pvq_v(n: usize, k: usize) -> u128 { - if k == 0 { - return 1; - } - if n == 0 { - return 0; - } - let mut row = vec![0u128; k + 1]; - row[0] = 1; - for _ in 0..n { - let mut prev_diag = row[0]; - for kk in 1..=k { - let up = row[kk]; - let v = up.saturating_add(row[kk - 1]).saturating_add(prev_diag); - prev_diag = up; - row[kk] = v.min(u128::MAX >> 8); - } - row[0] = 1; - } - row[k] -} - -fn fits_in32(n: usize, k: i32) -> bool { - pvq_v(n, k as usize) <= u32::MAX as u128 -} - -/// Precomputed per-mode data. -pub struct Mode { - /// log2 of band width in 1/8 bits. - pub log_n: [i32; NB_EBANDS], - cache_index: Vec, - cache_bits: Vec, - caps: Vec, -} - -impl Mode { - pub fn new() -> Mode { - let mut log_n = [0i32; NB_EBANDS]; - for (i, l) in log_n.iter_mut().enumerate() { - *l = log2_frac((EBANDS[i + 1] - EBANDS[i]) as u32, BITRES); - } - let (cache_index, cache_bits) = compute_pulse_cache(MAX_LM as i32); - let mut m = Mode { log_n, cache_index, cache_bits, caps: Vec::new() }; - m.caps = compute_caps(&m); - m - } - - #[inline] - pub fn cache(&self, lm: i32, band: usize) -> &[u8] { - let idx = self.cache_index[((lm + 1) as usize) * NB_EBANDS + band]; - &self.cache_bits[idx.max(0) as usize..] - } - - pub fn bits2pulses(&self, band: usize, lm: i32, bits: i32) -> i32 { - let cache = self.cache(lm, band); - let mut lo = 0i32; - let mut hi = cache[0] as i32; - let bits = bits - 1; - for _ in 0..LOG_MAX_PSEUDO { - let mid = (lo + hi + 1) >> 1; - if cache[mid as usize] as i32 >= bits { - hi = mid; - } else { - lo = mid; - } - } - let lo_bits = if lo == 0 { -1 } else { cache[lo as usize] as i32 }; - if bits - lo_bits <= cache[hi as usize] as i32 - bits { lo } else { hi } - } - - pub fn pulses2bits(&self, band: usize, lm: i32, pulses: i32) -> i32 { - if pulses == 0 { 0 } else { self.cache(lm, band)[pulses as usize] as i32 + 1 } - } - - /// Per-band allocation caps for this frame (`init_caps`). - pub fn init_caps(&self, lm: usize, c: usize) -> [i32; NB_EBANDS] { - let mut cap = [0i32; NB_EBANDS]; - for (i, cp) in cap.iter_mut().enumerate() { - let n = (EBANDS[i + 1] - EBANDS[i]) << lm; - *cp = ((self.caps[NB_EBANDS * (2 * lm + c - 1) + i] as i32 + 64) * c as i32 * n) >> 2; - } - cap - } -} - -fn compute_pulse_cache(lm_max: i32) -> (Vec, Vec) { - let nb = NB_EBANDS; - let rows = (lm_max + 2) as usize; - let mut index = vec![-1i16; nb * rows]; - let mut entries: Vec<(usize, i32, usize)> = Vec::new(); - let mut curr = 0usize; - for i in 0..rows { - for j in 0..nb { - let n = (((EBANDS[j + 1] - EBANDS[j]) << i) >> 1) as usize; - index[i * nb + j] = -1; - 'search: for k in 0..=i { - for nn in 0..nb { - if k == i && nn >= j { - break; - } - if n == (((EBANDS[nn + 1] - EBANDS[nn]) << k) >> 1) as usize { - index[i * nb + j] = index[k * nb + nn]; - break 'search; - } - } - } - if index[i * nb + j] == -1 && n != 0 { - let mut k = 0; - while k < MAX_PSEUDO && fits_in32(n, get_pulses(k + 1)) { - k += 1; - } - index[i * nb + j] = curr as i16; - entries.push((n, k, curr)); - curr += (k + 1) as usize; - } - } - } - let mut bits = vec![0u8; curr]; - for &(n, k, off) in &entries { - let maxk = get_pulses(k) as usize; - let tmp = required_bits(n, maxk, BITRES); - for j in 1..=k { - bits[off + j as usize] = (tmp[get_pulses(j) as usize] - 1) as u8; - } - bits[off] = k as u8; - } - (index, bits) -} - -fn required_bits(n: usize, maxk: usize, frac: u32) -> Vec { - let mut out = vec![0i32; maxk + 1]; - if n == 1 { - for o in out.iter_mut().skip(1) { - *o = 1 << frac; - } - } else { - for (k, o) in out.iter_mut().enumerate().skip(1) { - *o = log2_frac(pvq_v(n, k) as u32, frac); - } - } - out -} - -fn compute_caps(m: &Mode) -> Vec { - let nb = NB_EBANDS; - let mut caps = vec![0u8; (MAX_LM + 1) * 2 * nb]; - for i in 0..=MAX_LM as i32 { - for c in 1..=2i32 { - for j in 0..nb { - let mut n0 = EBANDS[j + 1] - EBANDS[j]; - - let max_bits = if n0 << i == 1 { - (c * (1 + MAX_FINE_BITS)) << BITRES - } else { - let mut lm0 = 0; - if n0 > 2 { - n0 >>= 1; - lm0 -= 1; - } else if n0 <= 1 { - lm0 = i.min(1); - n0 <<= lm0; - } - let pcache = m.cache(lm0, j); - let mut mb = pcache[pcache[0] as usize] as i32 + 1; - let mut n = n0; - for k in 0..(i - lm0) { - mb <<= 1; - let offset = ((m.log_n[j] + ((lm0 + k) << BITRES)) >> 1) - QTHETA_OFFSET; - let num = 459 * ((2 * n - 1) * offset + mb); - let den = ((2 * n - 1) << 9) - 459; - let qb = ((num + (den >> 1)) / den).min(57); - mb += qb; - n <<= 1; - } - if c == 2 { - mb <<= 1; - let offset = ((m.log_n[j] + (i << BITRES)) >> 1) - if n == 2 { QTHETA_OFFSET_TWOPHASE } else { QTHETA_OFFSET }; - let ndof = 2 * n - 1 - (n == 2) as i32; - let p = if n == 2 { 512 } else { 487 }; - let num = p * (mb + ndof * offset); - let den = (ndof << 9) - p; - let qb = ((num + (den >> 1)) / den).min(if n == 2 { 64 } else { 61 }); - mb += qb; - } - let ndof = c * n + if c == 2 && n > 2 { 1 } else { 0 }; - let mut offset = ((m.log_n[j] + (i << BITRES)) >> 1) - FINE_OFFSET; - if n == 2 { - offset += (1 << BITRES) >> 2; - } - let num = mb + ndof * offset; - let den = (ndof - 1) << BITRES; - let qb = ((num + (den >> 1)) / den).min(MAX_FINE_BITS); - mb += (c * qb) << BITRES; - mb - }; - let w = c * ((EBANDS[j + 1] - EBANDS[j]) << i); - let v = (4 * max_bits / w) - 64; - caps[((i * 2 + c - 1) as usize) * nb + j] = v.clamp(0, 255) as u8; - } - } - } - caps -} - /// Result of the allocation process. pub struct Allocation { pub coded_bands: usize, @@ -277,89 +40,8 @@ pub struct AllocationInput<'a> { pub fn compute_allocation(m: &Mode, frame: CeltFrame, input: AllocationInput<'_>, enc: &mut RangeEncoder) -> Allocation { let CeltFrame { start, end, channels: c, lm } = frame; let AllocationInput { offsets, cap, trim: alloc_trim, bits: total } = input; - let ci = c as i32; - let lmi = lm as i32; - let mut total = total.max(0); - let mut skip_start = start; - let skip_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 }; - total -= skip_rsv; - let mut intensity_rsv = 0; - let mut dual_stereo_rsv = 0; - if c == 2 { - intensity_rsv = LOG2_FRAC_TABLE[end - start]; - if intensity_rsv > total { - intensity_rsv = 0; - } else { - total -= intensity_rsv; - dual_stereo_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 }; - total -= dual_stereo_rsv; - } - } - let mut bits1 = [0i32; NB_EBANDS]; - let mut bits2 = [0i32; NB_EBANDS]; - let mut thresh = [0i32; NB_EBANDS]; - let mut trim_offset = [0i32; NB_EBANDS]; - for j in start..end { - let w = EBANDS[j + 1] - EBANDS[j]; - thresh[j] = (ci << BITRES).max(((3 * w) << lmi << BITRES) >> 4); - trim_offset[j] = (ci * w * (alloc_trim - 5 - lmi) * (end as i32 - j as i32 - 1) * (1 << (lmi + BITRES as i32))) >> 6; - if w << lmi == 1 { - trim_offset[j] -= ci << BITRES; - } - } - let nb_alloc = ALLOC.len() as i32; - let mut lo = 1; - let mut hi = nb_alloc - 1; - loop { - let mut done = false; - let mut psum = 0; - let mid = (lo + hi) >> 1; - for j in (start..end).rev() { - let n = EBANDS[j + 1] - EBANDS[j]; - let mut bitsj = (ci * n * (ALLOC[mid as usize][j] as i32)) << lmi >> 2; - if bitsj > 0 { - bitsj = (bitsj + trim_offset[j]).max(0); - } - bitsj += offsets[j]; - if bitsj >= thresh[j] || done { - done = true; - psum += bitsj.min(cap[j]); - } else if bitsj >= ci << BITRES { - psum += ci << BITRES; - } - } - if psum > total { - hi = mid - 1; - } else { - lo = mid + 1; - } - if lo > hi { - break; - } - } - hi = lo; - lo -= 1; - for j in start..end { - let n = EBANDS[j + 1] - EBANDS[j]; - let mut bits1j = (ci * n * (ALLOC[lo as usize][j] as i32)) << lmi >> 2; - let mut bits2j = if hi >= nb_alloc { cap[j] } else { (ci * n * (ALLOC[hi as usize][j] as i32)) << lmi >> 2 }; - if bits1j > 0 { - bits1j = (bits1j + trim_offset[j]).max(0); - } - if bits2j > 0 { - bits2j = (bits2j + trim_offset[j]).max(0); - } - if lo > 0 { - bits1j += offsets[j]; - } - bits2j += offsets[j]; - if offsets[j] > 0 { - skip_start = j; - } - bits2j = (bits2j - bits1j).max(0); - bits1[j] = bits1j; - bits2[j] = bits2j; - } + let crate::opus::rate_common::Bracket { skip_start, bits1, bits2, thresh, total, skip_rsv, intensity_rsv, dual_stereo_rsv } = + crate::opus::rate_common::bracket(start, end, offsets, cap, alloc_trim, total, c, lm); let interpolation = AllocationInterpolation { skip_start, bits1: &bits1, bits2: &bits2, thresh: &thresh, cap, total, skip_rsv, intensity_rsv, dual_stereo_rsv }; interp_bits2pulses(m, frame, interpolation, enc) diff --git a/crates/audio/src/opus/encoder/resample.rs b/crates/audio/src/opus/encoder/resample.rs index 1acc9a4e7..a72589919 100644 --- a/crates/audio/src/opus/encoder/resample.rs +++ b/crates/audio/src/opus/encoder/resample.rs @@ -45,24 +45,7 @@ fn design(fc: f64, beta: f64, delay: usize) -> Vec { h.iter().map(|&v| v as f32).collect() } -/// Dot product with eight independent partial sums, which the compiler can vectorise (a single -/// running sum is bound by the latency of the float addition). The sums are combined pairwise. -#[inline] -fn dot8(a: &[f32], b: &[f32]) -> f32 { - let mut s = [0f32; 8]; - let (ca, ra) = a.as_chunks::<8>(); - let (cb, rb) = b.as_chunks::<8>(); - for (x, y) in ca.iter().zip(cb) { - for l in 0..8 { - s[l] += x[l] * y[l]; - } - } - let mut acc = ((s[0] + s[4]) + (s[1] + s[5])) + ((s[2] + s[6]) + (s[3] + s[7])); - for (x, y) in ra.iter().zip(rb) { - acc += x * y; - } - acc -} +use crate::opus::dot::dot8; /// Decimates one channel of 48 kHz audio by 3, 4 or 6. #[derive(Clone)] diff --git a/crates/audio/src/opus/fft.rs b/crates/audio/src/opus/fft.rs new file mode 100644 index 000000000..300c78d48 --- /dev/null +++ b/crates/audio/src/opus/fft.rs @@ -0,0 +1,130 @@ +//! Shared CELT forward complex FFT. + +#[derive(Clone, Copy, Default, Debug)] +pub struct Cpx { + pub re: f32, + pub im: f32, +} + +impl Cpx { + #[inline] + pub(crate) fn mul(self, o: Cpx) -> Cpx { + Cpx { re: self.re * o.re - self.im * o.im, im: self.re * o.im + self.im * o.re } + } + #[inline] + pub(crate) fn add(self, o: Cpx) -> Cpx { + Cpx { re: self.re + o.re, im: self.im + o.im } + } + #[inline] + pub(crate) fn sub(self, o: Cpx) -> Cpx { + Cpx { re: self.re - o.re, im: self.im - o.im } + } +} + +/// Mixed-radix (2, 3, 4, 5) forward complex FFT, unscaled (decimation in time, recursive). +pub struct Fft { + pub(crate) n: usize, + pub(crate) factors: Vec, + pub(crate) twiddle: Vec, +} + +impl Fft { + pub fn new(n: usize) -> Fft { + let mut factors = Vec::new(); + let mut m = n; + for p in [4usize, 2, 3, 5] { + while m.is_multiple_of(p) { + factors.push(p); + m /= p; + } + } + assert_eq!(m, 1, "unsupported FFT size {n}"); + let twiddle = (0..n) + .map(|k| { + let ph = -2.0 * std::f64::consts::PI * k as f64 / n as f64; + Cpx { re: ph.cos() as f32, im: ph.sin() as f32 } + }) + .collect(); + Fft { n, factors, twiddle } + } + + pub fn process(&self, data: &mut [Cpx], scratch: &mut Vec) { + scratch.clear(); + scratch.extend_from_slice(&data[..self.n]); + self.rec(&mut data[..self.n], scratch, 1, self.n, 0, 1); + } + + fn rec(&self, out: &mut [Cpx], inp: &[Cpx], in_stride: usize, n: usize, fi: usize, tw_stride: usize) { + let p = self.factors[fi]; + let m = n / p; + if m == 1 { + for (i, o) in out.iter_mut().enumerate().take(p) { + *o = inp[i * in_stride]; + } + } else { + for q in 0..p { + self.rec(&mut out[q * m..(q + 1) * m], &inp[q * in_stride..], in_stride * p, m, fi + 1, tw_stride * p); + } + } + // The twiddle index q * k * tw_stride is always below n, so no modulo is needed. + let tw = &self.twiddle; + match p { + 2 => { + for k in 0..m { + let a0 = out[k]; + let a1 = out[m + k].mul(tw[k * tw_stride]); + out[k] = a0.add(a1); + out[m + k] = a0.sub(a1); + } + } + 4 => { + for k in 0..m { + let t0 = out[k]; + let t1 = out[m + k].mul(tw[k * tw_stride]); + let t2 = out[2 * m + k].mul(tw[2 * k * tw_stride]); + let t3 = out[3 * m + k].mul(tw[3 * k * tw_stride]); + let (s0, d0) = (t0.add(t2), t0.sub(t2)); + let (s1, d1) = (t1.add(t3), t1.sub(t3)); + out[k] = s0.add(s1); + out[2 * m + k] = s0.sub(s1); + // The odd outputs combine d0 with -j * d1 = (d1.im, -d1.re) and its negative. + out[m + k] = Cpx { re: d0.re + d1.im, im: d0.im - d1.re }; + out[3 * m + k] = Cpx { re: d0.re - d1.im, im: d0.im + d1.re }; + } + } + 3 => { + // Imaginary part of exp(-2 pi i / 3) = -sqrt(3) / 2. + let h = tw[self.n / 3].im; + for k in 0..m { + let t0 = out[k]; + let t1 = out[m + k].mul(tw[k * tw_stride]); + let t2 = out[2 * m + k].mul(tw[2 * k * tw_stride]); + let (s, d) = (t1.add(t2), t1.sub(t2)); + let a = Cpx { re: t0.re - 0.5 * s.re, im: t0.im - 0.5 * s.im }; + out[k] = t0.add(s); + // j * h * d = (-h * d.im, h * d.re) + out[m + k] = Cpx { re: a.re - h * d.im, im: a.im + h * d.re }; + out[2 * m + k] = Cpx { re: a.re + h * d.im, im: a.im - h * d.re }; + } + } + _ => { + // Generic DFT butterfly (radix 5, once per transform). + let roots: [Cpx; 5] = std::array::from_fn(|q| if q < p { tw[q * (self.n / p)] } else { Cpx::default() }); + let mut t = [Cpx::default(); 5]; + for k in 0..m { + t[0] = out[k]; + for q in 1..p { + t[q] = out[q * m + k].mul(tw[q * k * tw_stride]); + } + for q2 in 0..p { + let mut acc = t[0]; + for q in 1..p { + acc = acc.add(t[q].mul(roots[(q * q2) % p])); + } + out[q2 * m + k] = acc; + } + } + } + } + } +} diff --git a/crates/audio/src/opus/mod.rs b/crates/audio/src/opus/mod.rs index b67e1ef3d..84e81ec8c 100644 --- a/crates/audio/src/opus/mod.rs +++ b/crates/audio/src/opus/mod.rs @@ -7,14 +7,18 @@ //! 20 ms, mono or stereo). From EffectCraft's `effectcraft-opusenc` (storytold/effectcraft@c618429). //! //! Public items keep their names, so FilmCraft and EffectCraft can re-export this module. The two -//! halves come from separate crates and do not share code yet (each has its own range coder, MDCT -//! and band tables). [`Bandwidth`] is one type for both (`encoder::Bandwidth` is the same type); +//! halves share the FFT, resampling dot product and CELT allocation preparation; range coders and +//! mode-specific MDCT processing remain separate. [`Bandwidth`] is one type for both (`encoder::Bandwidth` is the same type); //! the two `Mode` enums keep their apps' variant names and convert into each other. // Index loops and long argument lists follow the specification's pseudo-code (FilmCraft and // EffectCraft allow these lints workspace-wide). #![allow(clippy::needless_range_loop, clippy::too_many_arguments)] +mod dot; +mod fft; +mod rate_common; + #[cfg(feature = "opus-decode")] mod decode; #[cfg(feature = "opus-encode")] diff --git a/crates/audio/src/opus/rate_common.rs b/crates/audio/src/opus/rate_common.rs new file mode 100644 index 000000000..86b27c90a --- /dev/null +++ b/crates/audio/src/opus/rate_common.rs @@ -0,0 +1,362 @@ +//! CELT pulse cache and allocation caps shared by encoder and decoder. + +#[cfg(not(feature = "opus-encode"))] +use super::decode::celt::tables::{ALLOC, EBANDS, LOG2_FRAC_TABLE, MAX_LM, NB_EBANDS}; +#[cfg(feature = "opus-encode")] +use super::encoder::tables::{ALLOC, EBANDS, LOG2_FRAC_TABLE, MAX_LM, NB_EBANDS}; + +const BITRES: u32 = 3; +const MAX_PSEUDO: i32 = 40; +const LOG_MAX_PSEUDO: i32 = 6; +const MAX_FINE_BITS: i32 = 8; +const FINE_OFFSET: i32 = 21; +const QTHETA_OFFSET: i32 = 4; +const QTHETA_OFFSET_TWOPHASE: i32 = 16; +fn ilog(x: u32) -> i32 { + (32 - x.leading_zeros()) as i32 +} + +/// Number of pulses represented by pseudo-pulse index `i`. +pub fn get_pulses(i: i32) -> i32 { + if i < 8 { i } else { (8 + (i & 7)) << ((i >> 3) - 1) } +} + +/// Conservative (rounded up) log2 of `val` with `frac` fractional bits. +pub fn log2_frac(mut val: u32, mut frac: u32) -> i32 { + let mut l = ilog(val); + if val & val.wrapping_sub(1) != 0 { + if l > 16 { + val = ((val - 1) >> (l - 16)) + 1; + } else { + val <<= 16 - l; + } + let mut l = (l - 1) << frac; + loop { + let b = (val >> 16) as i32; + l += b << frac; + val = (val + b as u32) >> b; + val = (val * val + 0x7FFF) >> 15; + if frac == 0 { + break; + } + frac -= 1; + } + l + (val > 0x8000) as i32 + } else { + l -= 1; + l << frac + } +} + +/// PVQ codebook size V(N, K) (saturating far above 2^32). +pub fn pvq_v(n: usize, k: usize) -> u128 { + if k == 0 { + return 1; + } + if n == 0 { + return 0; + } + let mut row = vec![0u128; k + 1]; + row[0] = 1; + for _ in 0..n { + let mut prev_diag = row[0]; + for kk in 1..=k { + let up = row[kk]; + let v = up.saturating_add(row[kk - 1]).saturating_add(prev_diag); + prev_diag = up; + row[kk] = v.min(u128::MAX >> 8); + } + row[0] = 1; + } + row[k] +} + +fn fits_in32(n: usize, k: i32) -> bool { + pvq_v(n, k as usize) <= u32::MAX as u128 +} + +/// Precomputed per-mode data. +pub struct Mode { + /// log2 of band width in 1/8 bits. + pub log_n: [i32; NB_EBANDS], + pub(crate) cache_index: Vec, + pub(crate) cache_bits: Vec, + pub(crate) caps: Vec, +} + +impl Mode { + pub fn new() -> Mode { + let mut log_n = [0i32; NB_EBANDS]; + for (i, l) in log_n.iter_mut().enumerate() { + *l = log2_frac((EBANDS[i + 1] - EBANDS[i]) as u32, BITRES); + } + let (cache_index, cache_bits) = compute_pulse_cache(MAX_LM as i32); + let mut m = Mode { log_n, cache_index, cache_bits, caps: Vec::new() }; + m.caps = compute_caps(&m); + m + } + + #[inline] + pub fn cache(&self, lm: i32, band: usize) -> &[u8] { + let idx = self.cache_index[((lm + 1) as usize) * NB_EBANDS + band]; + &self.cache_bits[idx.max(0) as usize..] + } + + pub fn bits2pulses(&self, band: usize, lm: i32, bits: i32) -> i32 { + let cache = self.cache(lm, band); + let mut lo = 0i32; + let mut hi = cache[0] as i32; + let bits = bits - 1; + for _ in 0..LOG_MAX_PSEUDO { + let mid = (lo + hi + 1) >> 1; + if cache[mid as usize] as i32 >= bits { + hi = mid; + } else { + lo = mid; + } + } + let lo_bits = if lo == 0 { -1 } else { cache[lo as usize] as i32 }; + if bits - lo_bits <= cache[hi as usize] as i32 - bits { lo } else { hi } + } + + pub fn pulses2bits(&self, band: usize, lm: i32, pulses: i32) -> i32 { + if pulses == 0 { 0 } else { self.cache(lm, band)[pulses as usize] as i32 + 1 } + } + + /// Per-band allocation caps for this frame (`init_caps`). + pub fn init_caps(&self, lm: usize, c: usize) -> [i32; NB_EBANDS] { + let mut cap = [0i32; NB_EBANDS]; + for (i, cp) in cap.iter_mut().enumerate() { + let n = (EBANDS[i + 1] - EBANDS[i]) << lm; + *cp = ((self.caps[NB_EBANDS * (2 * lm + c - 1) + i] as i32 + 64) * c as i32 * n) >> 2; + } + cap + } +} + +fn compute_pulse_cache(lm_max: i32) -> (Vec, Vec) { + let nb = NB_EBANDS; + let rows = (lm_max + 2) as usize; + let mut index = vec![-1i16; nb * rows]; + let mut entries: Vec<(usize, i32, usize)> = Vec::new(); + let mut curr = 0usize; + for i in 0..rows { + for j in 0..nb { + let n = (((EBANDS[j + 1] - EBANDS[j]) << i) >> 1) as usize; + index[i * nb + j] = -1; + 'search: for k in 0..=i { + for nn in 0..nb { + if k == i && nn >= j { + break; + } + if n == (((EBANDS[nn + 1] - EBANDS[nn]) << k) >> 1) as usize { + index[i * nb + j] = index[k * nb + nn]; + break 'search; + } + } + } + if index[i * nb + j] == -1 && n != 0 { + let mut k = 0; + while k < MAX_PSEUDO && fits_in32(n, get_pulses(k + 1)) { + k += 1; + } + index[i * nb + j] = curr as i16; + entries.push((n, k, curr)); + curr += (k + 1) as usize; + } + } + } + let mut bits = vec![0u8; curr]; + for &(n, k, off) in &entries { + let maxk = get_pulses(k) as usize; + let tmp = required_bits(n, maxk, BITRES); + for j in 1..=k { + bits[off + j as usize] = (tmp[get_pulses(j) as usize] - 1) as u8; + } + bits[off] = k as u8; + } + (index, bits) +} + +fn required_bits(n: usize, maxk: usize, frac: u32) -> Vec { + let mut out = vec![0i32; maxk + 1]; + if n == 1 { + for o in out.iter_mut().skip(1) { + *o = 1 << frac; + } + } else { + for (k, o) in out.iter_mut().enumerate().skip(1) { + *o = log2_frac(pvq_v(n, k) as u32, frac); + } + } + out +} + +fn compute_caps(m: &Mode) -> Vec { + let nb = NB_EBANDS; + let mut caps = vec![0u8; (MAX_LM + 1) * 2 * nb]; + for i in 0..=MAX_LM as i32 { + for c in 1..=2i32 { + for j in 0..nb { + let mut n0 = EBANDS[j + 1] - EBANDS[j]; + + let max_bits = if n0 << i == 1 { + (c * (1 + MAX_FINE_BITS)) << BITRES + } else { + let mut lm0 = 0; + if n0 > 2 { + n0 >>= 1; + lm0 -= 1; + } else if n0 <= 1 { + lm0 = i.min(1); + n0 <<= lm0; + } + let pcache = m.cache(lm0, j); + let mut mb = pcache[pcache[0] as usize] as i32 + 1; + let mut n = n0; + for k in 0..(i - lm0) { + mb <<= 1; + let offset = ((m.log_n[j] + ((lm0 + k) << BITRES)) >> 1) - QTHETA_OFFSET; + let num = 459 * ((2 * n - 1) * offset + mb); + let den = ((2 * n - 1) << 9) - 459; + let qb = ((num + (den >> 1)) / den).min(57); + mb += qb; + n <<= 1; + } + if c == 2 { + mb <<= 1; + let offset = ((m.log_n[j] + (i << BITRES)) >> 1) - if n == 2 { QTHETA_OFFSET_TWOPHASE } else { QTHETA_OFFSET }; + let ndof = 2 * n - 1 - (n == 2) as i32; + let p = if n == 2 { 512 } else { 487 }; + let num = p * (mb + ndof * offset); + let den = (ndof << 9) - p; + let qb = ((num + (den >> 1)) / den).min(if n == 2 { 64 } else { 61 }); + mb += qb; + } + let ndof = c * n + if c == 2 && n > 2 { 1 } else { 0 }; + let mut offset = ((m.log_n[j] + (i << BITRES)) >> 1) - FINE_OFFSET; + if n == 2 { + offset += (1 << BITRES) >> 2; + } + let num = mb + ndof * offset; + let den = (ndof - 1) << BITRES; + let qb = ((num + (den >> 1)) / den).min(MAX_FINE_BITS); + mb += (c * qb) << BITRES; + mb + }; + let w = c * ((EBANDS[j + 1] - EBANDS[j]) << i); + let v = (4 * max_bits / w) - 64; + caps[((i * 2 + c - 1) as usize) * nb + j] = v.clamp(0, 255) as u8; + } + } + } + caps +} + +/// Shared allocation search before encoder/decoder signalling diverges. +pub struct Bracket { + pub skip_start: usize, + pub bits1: [i32; NB_EBANDS], + pub bits2: [i32; NB_EBANDS], + pub thresh: [i32; NB_EBANDS], + pub total: i32, + pub skip_rsv: i32, + pub intensity_rsv: i32, + pub dual_stereo_rsv: i32, +} + +pub fn bracket( + start: usize, + end: usize, + offsets: &[i32; NB_EBANDS], + cap: &[i32; NB_EBANDS], + alloc_trim: i32, + total: i32, + c: usize, + lm: usize, +) -> Bracket { + let ci = c as i32; + let lmi = lm as i32; + let mut total = total.max(0); + let mut skip_start = start; + let skip_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 }; + total -= skip_rsv; + let mut intensity_rsv = 0; + let mut dual_stereo_rsv = 0; + if c == 2 { + intensity_rsv = LOG2_FRAC_TABLE[end - start]; + if intensity_rsv > total { + intensity_rsv = 0; + } else { + total -= intensity_rsv; + dual_stereo_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 }; + total -= dual_stereo_rsv; + } + } + let mut bits1 = [0i32; NB_EBANDS]; + let mut bits2 = [0i32; NB_EBANDS]; + let mut thresh = [0i32; NB_EBANDS]; + let mut trim_offset = [0i32; NB_EBANDS]; + for j in start..end { + let w = EBANDS[j + 1] - EBANDS[j]; + thresh[j] = (ci << BITRES).max(((3 * w) << lmi << BITRES) >> 4); + trim_offset[j] = (ci * w * (alloc_trim - 5 - lmi) * (end as i32 - j as i32 - 1) * (1 << (lmi + BITRES as i32))) >> 6; + if w << lmi == 1 { + trim_offset[j] -= ci << BITRES; + } + } + let nb_alloc = ALLOC.len() as i32; + let mut lo = 1; + let mut hi = nb_alloc - 1; + loop { + let mut done = false; + let mut psum = 0; + let mid = (lo + hi) >> 1; + for j in (start..end).rev() { + let n = EBANDS[j + 1] - EBANDS[j]; + let mut bitsj = (ci * n * (ALLOC[mid as usize][j] as i32)) << lmi >> 2; + if bitsj > 0 { + bitsj = (bitsj + trim_offset[j]).max(0); + } + bitsj += offsets[j]; + if bitsj >= thresh[j] || done { + done = true; + psum += bitsj.min(cap[j]); + } else if bitsj >= ci << BITRES { + psum += ci << BITRES; + } + } + if psum > total { + hi = mid - 1; + } else { + lo = mid + 1; + } + if lo > hi { + break; + } + } + hi = lo; + lo -= 1; + for j in start..end { + let n = EBANDS[j + 1] - EBANDS[j]; + let mut bits1j = (ci * n * (ALLOC[lo as usize][j] as i32)) << lmi >> 2; + let mut bits2j = if hi >= nb_alloc { cap[j] } else { (ci * n * (ALLOC[hi as usize][j] as i32)) << lmi >> 2 }; + if bits1j > 0 { + bits1j = (bits1j + trim_offset[j]).max(0); + } + if bits2j > 0 { + bits2j = (bits2j + trim_offset[j]).max(0); + } + if lo > 0 { + bits1j += offsets[j]; + } + bits2j += offsets[j]; + if offsets[j] > 0 { + skip_start = j; + } + bits2j = (bits2j - bits1j).max(0); + bits1[j] = bits1j; + bits2[j] = bits2j; + } + Bracket { skip_start, bits1, bits2, thresh, total, skip_rsv, intensity_rsv, dual_stereo_rsv } +} From cae7c1d8a6fec9a9ee136169918c5dd20ca509a0 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 13:57:49 -0500 Subject: [PATCH 124/150] Share Samsung SEF repair and JPEG EOI inspection (#1640) * Expose tolerant JPEG marker inspection * Export JPEG inspection API * Factor JPEG inspection fields from shared walker * Share ICC chunk collection between JPEG inspection modes * Cover JPEG MPF and gain-map inspection * Expose MPF index inspection for located payloads * Cover located MPF index parity * Preserve bounded MPF index directory validation * Share Samsung SEF repair and JPEG EOI inspection * Cover JPEG scan byte search --- crates/image-codecs/src/codecs/jpeg.rs | 43 +++++++++++++- crates/image-codecs/src/jpeg_sef.rs | 82 ++++++++++++++++++++++++++ crates/image-codecs/src/lib.rs | 1 + 3 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 crates/image-codecs/src/jpeg_sef.rs diff --git a/crates/image-codecs/src/codecs/jpeg.rs b/crates/image-codecs/src/codecs/jpeg.rs index bcd86ad5a..15346ea56 100644 --- a/crates/image-codecs/src/codecs/jpeg.rs +++ b/crates/image-codecs/src/codecs/jpeg.rs @@ -195,6 +195,27 @@ enum DataEnd { /// measured on these, not on the file length, which APP padding can inflate without /// adding image data. fn data_end(b: &[u8]) -> (DataEnd, u64) { + data_end_mode(b, false) +} + +/// Whether the marker walk actually reaches the first EOI, with malformed segment lengths refused. +pub(crate) fn reaches_eoi(b: &[u8]) -> bool { + data_end_mode(b, true).0 == DataEnd::Complete +} + +fn find_ff(b: &[u8]) -> Option { + const ONES: u64 = 0x0101_0101_0101_0101; + let (words, tail) = b.as_chunks::<8>(); + for (i, word) in words.iter().enumerate() { + let x = !u64::from_ne_bytes(*word); + if x.wrapping_sub(ONES) & !x & (ONES << 7) != 0 { + return word.iter().position(|&v| v == 0xff).map(|j| i * 8 + j); + } + } + tail.iter().position(|&v| v == 0xff).map(|j| words.len() * 8 + j) +} + +fn data_end_mode(b: &[u8], strict: bool) -> (DataEnd, u64) { // Where the bytes run out: inside the image data once a scan has held some. let cut = |scan_data: bool, scanned: u64| (if scan_data { DataEnd::Truncated } else { DataEnd::Empty }, scanned); let mut i = 2; // past SOI @@ -207,7 +228,7 @@ fn data_end(b: &[u8]) -> (DataEnd, u64) { // segments after the stray bytes (padding among them) are still recognised. Stray // bytes are not scan data, so they never count (junk would buy budget as padding // did); with no marker left, the image data ends here. - let Some(p) = b.get(i..).and_then(|r| r.iter().position(|&v| v == 0xFF)) else { return cut(scan_data, scanned) }; + let Some(p) = b.get(i..).and_then(find_ff) else { return cut(scan_data, scanned) }; i += p; continue; } @@ -216,15 +237,19 @@ fn data_end(b: &[u8]) -> (DataEnd, u64) { 0xD9 => return (DataEnd::Complete, scanned), 0xFF => i += 1, 0x01 | 0xD0..=0xD8 => i += 2, + 0x00 if strict => i += 2, _ => { let Some(len) = b.get(i + 2..i + 4).and_then(|s| <[u8; 2]>::try_from(s).ok()).map(u16::from_be_bytes) else { return cut(scan_data, scanned); }; + if strict && len < 2 { + return cut(scan_data, scanned); + } i = i.saturating_add(2 + usize::from(len)); if code == 0xDA { let start = i; loop { - let Some(p) = b.get(i..).and_then(|r| r.iter().position(|&v| v == 0xFF)) else { + let Some(p) = b.get(i..).and_then(find_ff) else { // EOF inside this scan: everything from the scan start to the end // of the file is scan data (inside a declared segment it may not // be, so only count what remains). @@ -653,6 +678,20 @@ mod tests { } /// `info` reads the frame from the markers, and which decoder the file goes to. + #[test] + fn ff_search_matches_plain_search() { + let mut b: Vec = (0..300u32).map(|i| (i * 7 % 255) as u8).collect(); + for at in [None, Some(0), Some(7), Some(8), Some(15), Some(203), Some(296), Some(299)] { + if let Some(i) = at { + b[i] = 0xff; + } + for start in [0, 1, 5, 9, 290, 299, 300] { + let s = &b[start..]; + assert_eq!(find_ff(s), s.iter().position(|&v| v == 0xff)); + } + } + } + #[test] fn info_reads_the_frame_and_the_decoder() { let img = Image::from_raw(37, 21, ChannelLayout::Rgb, SampleType::U8, vec![128; 37 * 21 * 3]).unwrap(); diff --git a/crates/image-codecs/src/jpeg_sef.rs b/crates/image-codecs/src/jpeg_sef.rs new file mode 100644 index 000000000..6ae878013 --- /dev/null +++ b/crates/image-codecs/src/jpeg_sef.rs @@ -0,0 +1,82 @@ +//! Validated Samsung SEF trailer repair for JPEGs missing their final EOI marker. +use std::borrow::Cow; + +/// Whether the first JPEG image reaches an EOI through its marker and scan structure. +pub fn reaches_eoi(bytes: &[u8]) -> bool { + crate::codecs::jpeg::reaches_eoi(bytes) +} + +/// Borrow ordinary input unchanged. For a validated Samsung SEF trailer following an open JPEG +/// scan, remove that trailer and append the missing EOI. The owned copy is made only on repair. +pub fn repair(bytes: &[u8]) -> Cow<'_, [u8]> { + let Some(image) = image_end(bytes).and_then(|end| bytes.get(..end)) else { return Cow::Borrowed(bytes) }; + if reaches_eoi(image) { + return Cow::Borrowed(bytes); + } + let mut fixed = Vec::with_capacity(image.len().saturating_add(2)); + fixed.extend_from_slice(image); + fixed.extend_from_slice(&[0xff, 0xd9]); + let in_scan = craft_metadata::inspect_jpeg(&fixed).is_some_and(|m| m.first_scan_components > 0) && reaches_eoi(&fixed); + if in_scan { Cow::Owned(fixed) } else { Cow::Borrowed(bytes) } +} + +fn image_end(b: &[u8]) -> Option { + let le32 = |at: usize| b.get(at..at.checked_add(4)?)?.try_into().ok().map(|v| u32::from_le_bytes(v) as usize); + let tail = b.len().checked_sub(8)?; + if b.get(tail.checked_add(4)?..)? != b"SEFT" { + return None; + } + let dir = tail.checked_sub(le32(tail)?)?; + if b.get(dir..dir.checked_add(4)?)? != b"SEFH" { + return None; + } + let count = le32(dir.checked_add(8)?)?; + let entries = dir.checked_add(12)?; + if count == 0 || count > tail.checked_sub(entries)? / 12 { + return None; + } + let mut start = dir; + for i in 0..count { + let entry = entries.checked_add(i.checked_mul(12)?)?; + let block = dir.checked_sub(le32(entry.checked_add(4)?)?)?; + let len = le32(entry.checked_add(8)?)?; + let kind = |at: usize| b.get(at.checked_add(2)?..at.checked_add(4)?); + if len < 4 || block.checked_add(len)? > dir || kind(block)? != kind(entry)? { + return None; + } + start = start.min(block); + } + Some(start) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn repairs_only_valid_sef_after_an_open_scan() { + let image = [0xff, 0xd8, 0xff, 0xc0, 0, 8, 8, 0, 1, 0, 1, 1, 0xff, 0xda, 0, 3, 1, 0x12, 0xff, 0, 0x34]; + let mut b = image.to_vec(); + let block = [0, 0, 0xe0, 0x08, 1, 2, 3, 4]; + b.extend_from_slice(&block); + b.extend_from_slice(b"SEFH"); + b.extend_from_slice(&106u32.to_le_bytes()); + b.extend_from_slice(&1u32.to_le_bytes()); + b.extend_from_slice(&[0, 0, 0xe0, 0x08]); + b.extend_from_slice(&(block.len() as u32).to_le_bytes()); + b.extend_from_slice(&(block.len() as u32).to_le_bytes()); + b.extend_from_slice(&24u32.to_le_bytes()); + b.extend_from_slice(b"SEFT"); + let mut expected = image.to_vec(); + expected.extend_from_slice(&[0xff, 0xd9]); + assert_eq!(repair(&b).as_ref(), expected); + assert!(matches!(repair(&image), Cow::Borrowed(_))); + b[image.len() + 2] = 0; + assert!(matches!(repair(&b), Cow::Borrowed(_))); + } + #[test] + fn eoi_requires_valid_lengths_and_skips_stuffed_bytes() { + assert!(reaches_eoi(&[0xff, 0xd8, 0xff, 0, 0xff, 0xd9])); + assert!(!reaches_eoi(&[0xff, 0xd8, 0xff, 0xe1, 0, 1, 0xff, 0xd9])); + assert!(!reaches_eoi(&[0xff, 0xd8, 0xff, 0xe1, 0, 4, 0xff, 0xd9])); + } +} diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index adeed0825..361285b2a 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -26,6 +26,7 @@ mod fidelity; mod format; mod image; mod inspection; +pub mod jpeg_sef; mod lab; mod options; pub mod orientation; From 82276297a690d71b116f4e0dbd367e0e914088d6 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:57:52 +0000 Subject: [PATCH 125/150] Share authenticated JSON-lines control protocol --- Cargo.lock | 8 ++ Cargo.toml | 1 + crates/control-protocol/Cargo.toml | 15 +++ crates/control-protocol/src/lib.rs | 144 +++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 crates/control-protocol/Cargo.toml create mode 100644 crates/control-protocol/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index b3325f790..983547cc8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1456,6 +1456,14 @@ dependencies = [ "encoding_rs", ] +[[package]] +name = "craft-control-protocol" +version = "0.1.0" +dependencies = [ + "serde_json", + "subtle", +] + [[package]] name = "craft-crs" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 0c9729e00..4f58c378e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -91,6 +91,7 @@ craft-indd = { path = "crates/indd" } craft-jmap = { path = "crates/jmap" } craft-ical = { path = "crates/ical" } craft-jpeg = { path = "crates/jpeg" } +craft-control-protocol = { path = "crates/control-protocol" } craft-jsonrpc = { path = "crates/jsonrpc" } craft-mcp = { path = "crates/mcp" } craft-jxl = { path = "crates/jxl" } diff --git a/crates/control-protocol/Cargo.toml b/crates/control-protocol/Cargo.toml new file mode 100644 index 000000000..f00bbc1c6 --- /dev/null +++ b/crates/control-protocol/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "craft-control-protocol" +description = "Authenticated bounded JSON-lines control protocol" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +serde_json = { workspace = true } +subtle = "2" diff --git a/crates/control-protocol/src/lib.rs b/crates/control-protocol/src/lib.rs new file mode 100644 index 000000000..393fa625b --- /dev/null +++ b/crates/control-protocol/src/lib.rs @@ -0,0 +1,144 @@ +//! Authenticated JSON-lines request framing and dispatch for local control services. +use serde_json::{Value, json}; +use std::io::{BufRead, BufReader, Read, Write}; +use subtle::ConstantTimeEq; + +/// One parsed line. +enum Line { + Request { + id: Value, + method: String, + params: Value, + }, + Blank, + /// Not a request: reply with this error and close the connection. + Reject(String), +} + +fn parse_line(bytes: &[u8], token: &str) -> Line { + let Ok(text) = std::str::from_utf8(bytes) else { + return Line::Reject("not a control request (invalid UTF-8); closing the connection".into()); + }; + let text = text.trim(); + if text.is_empty() { + return Line::Blank; + } + let msg = match serde_json::from_str::(text) { + Ok(v) => v, + Err(e) => return Line::Reject(format!("bad JSON ({e}); expected one JSON request object per line; closing the connection")), + }; + let Some(obj) = msg.as_object() else { + return Line::Reject("not a control request (expected a JSON object); closing the connection".into()); + }; + let Some(method) = obj.get("method").and_then(Value::as_str) else { + return Line::Reject("not a control request (missing string `method`); closing the connection".into()); + }; + // Authenticate every request before constructing or enqueueing a ControlRequest. A valid + // earlier request must not authorize a later one with a missing or incorrect credential. + let authenticated = obj.get("token").and_then(Value::as_str).is_some_and(|supplied| bool::from(supplied.as_bytes().ct_eq(token.as_bytes()))); + if !authenticated { + return Line::Reject("control authentication failed; closing the connection".into()); + } + Line::Request { + id: obj.get("id").cloned().unwrap_or(Value::Null), + method: method.to_string(), + params: obj.get("params").cloned().unwrap_or(json!({})), + } +} + +#[derive(Debug, PartialEq, Eq)] +pub enum ReadFailure { + Timeout, + Rejected(String), +} + +/// Read one `\n`-terminated line of at most `max` bytes into `buf` (newline stripped). +/// `Ok(false)` at end of stream; `Err` when the line is too long or the read fails. +pub fn read_bounded_line(reader: &mut impl BufRead, buf: &mut Vec, max: usize) -> Result { + buf.clear(); + // `take` bounds the bytes buffered for one line: a peer can't make us allocate more than `max`. + let limit = u64::try_from(max).unwrap_or(u64::MAX).saturating_add(1); + let n = reader.take(limit).read_until(b'\n', buf).map_err(|e| match e.kind() { + std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock => ReadFailure::Timeout, + _ => ReadFailure::Rejected(e.to_string()), + })?; + if n == 0 { + return Ok(false); + } + if buf.last() == Some(&b'\n') { + buf.pop(); + if buf.last() == Some(&b'\r') { + buf.pop(); + } + return Ok(true); + } + if buf.len() > max { + return Err(ReadFailure::Rejected(format!("request line longer than {max} bytes; closing the connection"))); + } + // Last line without a newline, then end of stream. + Ok(true) +} + +/// Serve requests from `read`, answering on `out`, until the peer closes, sends something that is +/// not a request, or `handle` returns `None` (app gone). `handle` gets `(method, params)` and +/// returns the reply object (the request's `id` is added here). +pub fn serve_lines(read: R, mut out: W, token: &str, max_line: usize, mut handle: impl FnMut(String, Value) -> Option) { + let mut reader = BufReader::new(read); + let mut buf = Vec::new(); + loop { + let reject = match read_bounded_line(&mut reader, &mut buf, max_line) { + Ok(false) => return, + // Expiration is not a request: a pooled client must see EOF and reconnect, rather + // than read a stale error reply. A partial line must not be parsed on timeout. + Err(ReadFailure::Timeout) => return, + Err(ReadFailure::Rejected(e)) => e, + Ok(true) => match parse_line(&buf, token) { + Line::Blank => continue, + Line::Reject(e) => e, + Line::Request { id, method, params } => { + let Some(mut r) = handle(method, params) else { return }; + if let Some(o) = r.as_object_mut() { + o.insert("id".into(), id); + } + if writeln!(out, "{r}").and_then(|()| out.flush()).is_err() { + return; + } + continue; + } + }, + }; + let _ = writeln!(out, "{}", json!({"ok": false, "error": reject})).and_then(|()| out.flush()); + return; + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn authenticates_each_request_and_closes_after_failure() { + let input = + b"{\"id\":1,\"method\":\"first\",\"token\":\"secret\"}\n{\"id\":2,\"method\":\"second\"}\n{\"method\":\"third\",\"token\":\"secret\"}\n"; + let mut output = Vec::new(); + let mut calls = Vec::new(); + serve_lines(&input[..], &mut output, "secret", 4096, |method, _| { + calls.push(method); + Some(json!({"ok": true})) + }); + assert_eq!(calls, ["first"]); + let lines: Vec = std::str::from_utf8(&output).unwrap().lines().map(|s| serde_json::from_str(s).unwrap()).collect(); + assert_eq!(lines[0], json!({"ok": true, "id": 1})); + assert_eq!(lines[1]["error"], "control authentication failed; closing the connection"); + } + #[test] + fn bounds_requests_and_preserves_last_unterminated_line() { + let mut buf = Vec::new(); + assert_eq!(read_bounded_line(&mut &b"abc"[..], &mut buf, 3), Ok(true)); + assert_eq!(buf, b"abc"); + assert!(matches!(read_bounded_line(&mut &b"abcd"[..], &mut buf, 3), Err(ReadFailure::Rejected(_)))); + let mut out = Vec::new(); + serve_lines(&b"{broken}\n"[..], &mut out, "secret", 1024, |_, _| panic!("must not dispatch")); + let reply: Value = serde_json::from_slice(&out).unwrap(); + assert!(reply["error"].as_str().unwrap().starts_with("bad JSON (")); + } +} From ebdff8b93136e894606a1c344d545c7325c5cf62 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 13:59:18 -0500 Subject: [PATCH 126/150] Document Duotone public record fields and counts --- crates/psd/src/duotone.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/psd/src/duotone.rs b/crates/psd/src/duotone.rs index 62283f576..6c9ae49e1 100644 --- a/crates/psd/src/duotone.rs +++ b/crates/psd/src/duotone.rs @@ -19,15 +19,20 @@ pub const NO_COLOR: ColorRecord = [0xffff, 0, 0, 0, 0]; #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[repr(u16)] pub enum InkCount { + /// One active ink. One = 1, + /// Two active inks. Two = 2, + /// Three active inks. Three = 3, + /// Four active inks. Four = 4, } /// One fixed-size ink slot, including uninterpreted reserved curve word. #[derive(Clone, Debug, PartialEq, Eq)] pub struct InkRecord { + /// Raw ink colour space and components. pub color: ColorRecord, /// Pascal name bytes, including its length byte; text encoding belongs to the caller. pub name: [u8; 64], @@ -63,6 +68,7 @@ impl<'a> DuotoneView<'a> { (1..=4).contains(&count).then_some(Self { data, count }) } + /// Number of active ink records (one through four). pub fn ink_count(self) -> usize { self.count } From 726ad0f8290bedb6ced60fa06200a6b574f9990d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 13:59:20 -0500 Subject: [PATCH 127/150] Document DDS result fields and errors --- crates/texture/src/dds.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/texture/src/dds.rs b/crates/texture/src/dds.rs index 6ba5569e8..77b053c31 100644 --- a/crates/texture/src/dds.rs +++ b/crates/texture/src/dds.rs @@ -7,10 +7,13 @@ use crate::{ImageError, Kind, Limits}; +/// A malformed DDS/backend failure or rejected output allocation. #[derive(Debug, thiserror::Error)] pub enum DecodeError { + /// Output size or allocation failed shared image limits. #[error(transparent)] Image(#[from] ImageError), + /// The decoder rejected input or output geometry. #[error("DDS decode failed: {0}")] Decode(String), } @@ -18,8 +21,11 @@ pub enum DecodeError { /// Unmodified backend output. Alpha and out-of-range float values are not clamped. #[derive(Debug)] pub struct Surface { + /// Width of the first surface in pixels. pub width: u32, + /// Height of the first surface in pixels. pub height: u32, + /// Row-major RGBA float samples without transfer conversion. pub rgba: Vec, /// Exactly the backend's `Precision::F32` classification, as used by MaterialCraft. pub source_precision_is_f32: bool, From 2e6878d973605c1aba8b9be091d3002474e32e9b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:59:28 +0000 Subject: [PATCH 128/150] Share session token shape validation --- crates/control-protocol/src/lib.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/control-protocol/src/lib.rs b/crates/control-protocol/src/lib.rs index 393fa625b..eb57788fe 100644 --- a/crates/control-protocol/src/lib.rs +++ b/crates/control-protocol/src/lib.rs @@ -3,6 +3,11 @@ use serde_json::{Value, json}; use std::io::{BufRead, BufReader, Read, Write}; use subtle::ConstantTimeEq; +/// Whether a session token has the control protocol's 64 hexadecimal character form. +pub fn valid_token(token: &str) -> bool { + token.len() == 64 && token.bytes().all(|b| b.is_ascii_hexdigit()) +} + /// One parsed line. enum Line { Request { @@ -116,6 +121,12 @@ pub fn serve_lines(read: R, mut out: W, token: &str, max_line mod tests { use super::*; #[test] + fn validates_session_token_shape() { + assert!(valid_token(&"aB09".repeat(16))); + assert!(!valid_token(&"a".repeat(63))); + assert!(!valid_token(&"g".repeat(64))); + } + #[test] fn authenticates_each_request_and_closes_after_failure() { let input = b"{\"id\":1,\"method\":\"first\",\"token\":\"secret\"}\n{\"id\":2,\"method\":\"second\"}\n{\"method\":\"third\",\"token\":\"secret\"}\n"; From edfd24c0048e9afbad35661b5bfdc6e89f65bed2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 19:00:31 +0000 Subject: [PATCH 129/150] Share control client request and reply grammar --- crates/control-protocol/src/lib.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/control-protocol/src/lib.rs b/crates/control-protocol/src/lib.rs index eb57788fe..9d237cb37 100644 --- a/crates/control-protocol/src/lib.rs +++ b/crates/control-protocol/src/lib.rs @@ -8,6 +8,20 @@ pub fn valid_token(token: &str) -> bool { token.len() == 64 && token.bytes().all(|b| b.is_ascii_hexdigit()) } +/// Encode one client request, including its terminating newline. +pub fn request_line(id: u64, token: Option<&str>, method: &str, params: &Value) -> String { + format!("{}\n", json!({"id": id, "token": token, "method": method, "params": params})) +} + +/// Extract the result or application error from a parsed control reply. +pub fn reply_result(reply: &Value) -> Result { + if reply.get("ok").and_then(Value::as_bool) == Some(true) { + Ok(reply.get("result").cloned().unwrap_or(Value::Null)) + } else { + Err(reply.get("error").and_then(Value::as_str).unwrap_or("error").to_owned()) + } +} + /// One parsed line. enum Line { Request { @@ -121,6 +135,13 @@ pub fn serve_lines(read: R, mut out: W, token: &str, max_line mod tests { use super::*; #[test] + fn client_request_and_reply_keep_optional_fields() { + let line = request_line(7, None, "status", &json!({"a": 1})); + assert_eq!(serde_json::from_str::(&line).unwrap(), json!({"id": 7, "token": null, "method": "status", "params": {"a": 1}})); + assert_eq!(reply_result(&json!({"ok": true})).unwrap(), Value::Null); + assert_eq!(reply_result(&json!({"ok": false, "error": "busy"})).unwrap_err(), "busy"); + } + #[test] fn validates_session_token_shape() { assert!(valid_token(&"aB09".repeat(16))); assert!(!valid_token(&"a".repeat(63))); From e2b5907a404726f2ddefa05901f65a054303d552 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 19:05:00 +0000 Subject: [PATCH 130/150] Test control framing failures and backpressure --- crates/control-protocol/src/lib.rs | 58 ++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/crates/control-protocol/src/lib.rs b/crates/control-protocol/src/lib.rs index 9d237cb37..8bdb08d66 100644 --- a/crates/control-protocol/src/lib.rs +++ b/crates/control-protocol/src/lib.rs @@ -1,4 +1,5 @@ //! Authenticated JSON-lines request framing and dispatch for local control services. +#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))] use serde_json::{Value, json}; use std::io::{BufRead, BufReader, Read, Write}; use subtle::ConstantTimeEq; @@ -135,6 +136,63 @@ pub fn serve_lines(read: R, mut out: W, token: &str, max_line mod tests { use super::*; #[test] + fn rejects_bad_request_shapes_without_dispatching() { + for input in [&b"\xff\n"[..], &b"[]\n"[..], &b"{}\n"[..]] { + let mut out = Vec::new(); + serve_lines(input, &mut out, "secret", 1024, |_, _| panic!("must not dispatch")); + let reply: Value = serde_json::from_slice(&out).unwrap(); + assert_eq!(reply["ok"], false); + assert!(reply["error"].as_str().unwrap().contains("closing the connection")); + } + } + #[test] + fn timeout_after_partial_line_never_dispatches() { + struct Partial(bool); + impl Read for Partial { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + if !self.0 { + self.0 = true; + buf[..2].copy_from_slice(b"{\""); + return Ok(2); + } + Err(std::io::ErrorKind::TimedOut.into()) + } + } + let mut out = Vec::new(); + serve_lines(Partial(false), &mut out, "secret", 1024, |_, _| panic!("must not dispatch")); + assert!(out.is_empty()); + } + #[test] + fn line_boundaries_and_closed_dispatch_are_preserved() { + let mut buf = Vec::new(); + assert_eq!(read_bounded_line(&mut &b"abc\r\n"[..], &mut buf, 4), Ok(true)); + assert_eq!(buf, b"abc"); + assert_eq!(read_bounded_line(&mut &b"abcd\n"[..], &mut buf, 4), Ok(true)); + assert_eq!(buf, b"abcd"); + let mut out = Vec::new(); + serve_lines(&b"{\"method\":\"x\",\"token\":\"s\"}\n"[..], &mut out, "s", 1024, |_, _| None); + assert!(out.is_empty()); + } + #[test] + fn output_failure_stops_following_requests() { + struct Closed; + impl Write for Closed { + fn write(&mut self, _: &[u8]) -> std::io::Result { + Err(std::io::ErrorKind::BrokenPipe.into()) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + let input = b"{\"method\":\"x\",\"token\":\"s\"}\n{\"method\":\"y\",\"token\":\"s\"}\n"; + let mut calls = 0; + serve_lines(&input[..], Closed, "s", 1024, |_, _| { + calls += 1; + Some(json!({"ok": true})) + }); + assert_eq!(calls, 1); + } + #[test] fn client_request_and_reply_keep_optional_fields() { let line = request_line(7, None, "status", &json!({"a": 1})); assert_eq!(serde_json::from_str::(&line).unwrap(), json!({"id": 7, "token": null, "method": "status", "params": {"a": 1}})); From b761b5b57f3631c5208e0d8e07b843ab79c79dc0 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 19:13:00 +0000 Subject: [PATCH 131/150] Apply shared format production lint policy --- crates/fcproj-format/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/fcproj-format/src/lib.rs b/crates/fcproj-format/src/lib.rs index 877b79765..4bc37caab 100644 --- a/crates/fcproj-format/src/lib.rs +++ b/crates/fcproj-format/src/lib.rs @@ -1,5 +1,6 @@ //! FilmCraft `.fcproj` JSON envelope, version detection and historical migrations. //! Project and editor-view types are supplied by the caller. +#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))] use serde::{Deserialize, Serialize}; use serde_json::Value; /// The `format` tag written in every envelope. From 40b9b7ef45a0b5d61873aab42d1059c6d507aa58 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 19:13:32 +0000 Subject: [PATCH 132/150] Bound OBJ face and continued-line parsing --- crates/mesh/src/obj.rs | 34 ++++++++++++++++++++++++++-------- crates/mesh/src/tests/obj.rs | 11 +++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/crates/mesh/src/obj.rs b/crates/mesh/src/obj.rs index 092a4e0ff..d1354a66d 100644 --- a/crates/mesh/src/obj.rs +++ b/crates/mesh/src/obj.rs @@ -331,15 +331,26 @@ impl Reader<'_> { Ok(object.parts.get_mut(slot)) } - fn face(&mut self, rest: &[&str]) -> Result<()> { - if rest.len() > self.limits.max_face_corners { - return Err(too_large(KIND, format!("a face of more than {} corners", self.limits.max_face_corners))); + fn face<'a>(&mut self, rest: impl Iterator) -> Result<()> { + let mut corners = Vec::new(); + let mut invalid = false; + for token in rest { + // Check before collecting tokens or corners: one line can hold millions of them. + if corners.len() >= self.limits.max_face_corners { + return Err(too_large(KIND, format!("a face of more than {} corners", self.limits.max_face_corners))); + } + match self.corner(token) { + Some(corner) => corners.push(corner), + None => { + invalid = true; + corners.push(Corner::at(0)); + } + } } - let corners: Option> = rest.iter().map(|t| self.corner(t)).collect(); - let Some(corners) = corners.filter(|c| c.len() >= 3) else { + if invalid || corners.len() < 3 { self.bad_lines += 1; return Ok(()); - }; + } self.triangles = self.triangles.saturating_add(corners.len() - 2); if self.triangles > self.limits.max_triangles { return Err(too_large(KIND, format!("more than {} triangles", self.limits.max_triangles))); @@ -361,7 +372,7 @@ impl Reader<'_> { let Some(key) = it.next() else { return Ok(()) }; let after = line.get(key.len()..).unwrap_or("").trim(); match key { - "v" => self.vertex(&it.collect::>())?, + "v" => self.vertex(&it.take(6).collect::>())?, "vt" => { self.pool_room(self.vt.len(), "texture coordinates")?; // `vt u` alone is a 1D coordinate; OBJ's v axis points up the image. @@ -390,7 +401,7 @@ impl Reader<'_> { } "usemtl" => self.use_material(after)?, "mtllib" => self.library(after, resolve)?, - "f" => self.face(&it.collect::>())?, + "f" => self.face(it)?, _ => {} } Ok(()) @@ -445,6 +456,10 @@ impl Reader<'_> { /// writes; every name asked, and every image a library names, is listed in /// [`Scene::external`]. pub fn read(bytes: &[u8], limits: &Limits, resolve: Resolver) -> Result { + // Lossy UTF-8 conversion can allocate, so bound it before copying invalid input. + if std::str::from_utf8(bytes).is_err() && bytes.len() > limits.max_data_bytes { + return Err(too_large(KIND, "invalid UTF-8 text exceeds the memory limit")); + } let text = String::from_utf8_lossy(bytes); let mut r = Reader { limits, @@ -469,6 +484,9 @@ pub fn read(bytes: &[u8], limits: &Limits, resolve: Resolver) -> Result { let mut joined = String::new(); for raw in text.lines() { let line = raw.split('#').next().unwrap_or(""); + if line.len() > limits.max_data_bytes || joined.len().checked_add(line.len()).is_none_or(|n| n > limits.max_data_bytes) { + return Err(too_large(KIND, "a continued line exceeds the memory limit")); + } if let Some(head) = line.trim_end().strip_suffix('\\') { joined.push_str(head); joined.push(' '); diff --git a/crates/mesh/src/tests/obj.rs b/crates/mesh/src/tests/obj.rs index 9c61773cb..5283012c4 100644 --- a/crates/mesh/src/tests/obj.rs +++ b/crates/mesh/src/tests/obj.rs @@ -341,3 +341,14 @@ fn a_library_named_again_is_read_once() { assert_eq!((s.materials.len(), s.images.len()), (1, 1)); assert_eq!(s.external, ["m.mtl", "red.png"]); } + +#[test] +fn face_tokens_and_continued_lines_are_bounded_before_collection() { + let limits = Limits { max_face_corners: 3, max_data_bytes: 1024, ..Limits::default() }; + let face = format!("v 0 0 0\nf {}\n", "1 ".repeat(100)); + assert!(matches!(read(face.as_bytes(), &limits, &mut no_external), Err(Error::TooLarge(_)))); + let long_line = format!("v {}\\\n{}\n", "1 ".repeat(300), "1 ".repeat(300)); + assert!(matches!(read(long_line.as_bytes(), &limits, &mut no_external), Err(Error::TooLarge(_)))); + let invalid = b"v 0 0 0\nf 1 1 1\n\xff"; + assert!(matches!(read(invalid, &Limits { max_data_bytes: 4, ..Limits::default() }, &mut no_external), Err(Error::TooLarge(_)))); +} From 7d8884c62c601fbaf784c60b38c863e6c398c303 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 19:14:12 +0000 Subject: [PATCH 133/150] Move typed project envelope encode and decode into shared API --- crates/fcproj-format/src/lib.rs | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/crates/fcproj-format/src/lib.rs b/crates/fcproj-format/src/lib.rs index 4bc37caab..98e50cf59 100644 --- a/crates/fcproj-format/src/lib.rs +++ b/crates/fcproj-format/src/lib.rs @@ -57,6 +57,26 @@ pub struct EnvelopeIn

                      { #[serde(default)] pub view: Option, } +/// Serialize the current envelope, with optional editor view and caller-provided generator. +pub fn encode(project: &P, view: Option<&V>, generator: String, pretty: bool) -> Vec { + let envelope = EnvelopeOut { format: FORMAT_ID, schema_version: SCHEMA_VERSION, generator, project, view }; + let result = if pretty { serde_json::to_vec_pretty(&envelope) } else { serde_json::to_vec(&envelope) }; + result.unwrap_or_default() +} + +/// Decode an envelope, migrating older documents while retaining the current-version fast path. +pub fn decode(bytes: &[u8]) -> Result<(EnvelopeIn

                      , u32), FormatError> { + let found = detect_bytes(bytes)?; + if found == SCHEMA_VERSION { + let env = serde_json::from_slice(bytes).map_err(|e| FormatError::Corrupt(e.to_string()))?; + return Ok((env, found)); + } + let doc: Value = serde_json::from_slice(bytes).map_err(|e| FormatError::Corrupt(e.to_string()))?; + let doc = migrate_with(MIGRATIONS, doc, found)?; + let env = serde_json::from_value(doc).map_err(|e| FormatError::Corrupt(format!("after upgrading from schema v{found}: {e}")))?; + Ok((env, found)) +} + /// Probe and validate the schema directly from bytes without building a JSON tree. pub fn detect_bytes(bytes: &[u8]) -> Result { let probe: Probe = serde_json::from_slice(bytes) @@ -251,6 +271,22 @@ mod tests { assert_eq!(parsed.project["extra"], json!([1, 2])); } #[test] + fn typed_encode_and_decode_current_and_legacy() { + let project = json!({"items": {}, "root": {}, "name": "Cut"}); + let view = json!({"active": 3}); + for pretty in [false, true] { + let bytes = encode(&project, Some(&view), "FilmCraft test".into(), pretty); + let (file, version): (EnvelopeIn, _) = decode(&bytes).unwrap(); + assert_eq!(version, SCHEMA_VERSION); + assert_eq!(file.project, project); + assert_eq!(file.view, Some(view.clone())); + } + let old = br#"{"version":1,"items":{},"root":{},"name":"Cut"}"#; + let (file, version): (EnvelopeIn, _) = decode(old).unwrap(); + assert_eq!(version, 1); + assert_eq!(file.project["name"], "Cut"); + } + #[test] fn rejects_wrong_future_and_damaged_envelopes() { assert!(matches!(detect_bytes(br#"{"format":"other","schema_version":1}"#), Err(FormatError::NotAProject(_)))); assert!(matches!(detect_bytes(br#"{"schema_version":18}"#), Err(FormatError::TooNew { found: 18, supported: 17 }))); From 284b0d815b69178cf4eb67321b7cc6c03202553c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 19:15:02 +0000 Subject: [PATCH 134/150] Expose compatible OBJ read options --- crates/mesh/src/obj.rs | 28 +++++++++++++++++++++++++--- crates/mesh/src/tests/obj.rs | 12 ++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/crates/mesh/src/obj.rs b/crates/mesh/src/obj.rs index d1354a66d..0a4cc3b68 100644 --- a/crates/mesh/src/obj.rs +++ b/crates/mesh/src/obj.rs @@ -167,8 +167,20 @@ struct Object { slots: HashMap<(String, Option, u32), usize>, } +/// Options for callers that need strict parsing or original face-run order. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct ReadOptions { + /// Return an error when a vertex or face line cannot be read. + pub strict: bool, + /// Keep separate runs when groups, materials or smoothing change. + pub preserve_runs: bool, + /// Do not resolve or report material libraries; `usemtl` names remain available. + pub ignore_material_libraries: bool, +} + struct Reader<'a> { limits: &'a Limits, + options: ReadOptions, v: Vec<[f64; 3]>, colors: Vec<[f32; 4]>, vt: Vec<[f32; 2]>, @@ -314,7 +326,7 @@ impl Reader<'_> { let Some(object) = self.objects.last_mut() else { return Ok(None) }; let slot = match self.slot { Some(s) => s, - None => match object.slots.get(&(self.group.clone(), self.material, self.smoothing)) { + None => match (!self.options.preserve_runs).then(|| object.slots.get(&(self.group.clone(), self.material, self.smoothing))).flatten() { Some(s) => *s, None => { if self.parts >= self.limits.max_parts { @@ -400,7 +412,7 @@ impl Reader<'_> { self.group = after.to_string(); } "usemtl" => self.use_material(after)?, - "mtllib" => self.library(after, resolve)?, + "mtllib" if !self.options.ignore_material_libraries => self.library(after, resolve)?, "f" => self.face(it)?, _ => {} } @@ -456,6 +468,11 @@ impl Reader<'_> { /// writes; every name asked, and every image a library names, is listed in /// [`Scene::external`]. pub fn read(bytes: &[u8], limits: &Limits, resolve: Resolver) -> Result { + read_with_options(bytes, limits, resolve, &ReadOptions::default()) +} + +/// Read an OBJ using the selected compatibility behavior. +pub fn read_with_options(bytes: &[u8], limits: &Limits, resolve: Resolver, options: &ReadOptions) -> Result { // Lossy UTF-8 conversion can allocate, so bound it before copying invalid input. if std::str::from_utf8(bytes).is_err() && bytes.len() > limits.max_data_bytes { return Err(too_large(KIND, "invalid UTF-8 text exceeds the memory limit")); @@ -463,6 +480,7 @@ pub fn read(bytes: &[u8], limits: &Limits, resolve: Resolver) -> Result { let text = String::from_utf8_lossy(bytes); let mut r = Reader { limits, + options: *options, v: vec![], colors: vec![], vt: vec![], @@ -482,7 +500,8 @@ pub fn read(bytes: &[u8], limits: &Limits, resolve: Resolver) -> Result { }; // A line ending in `\` continues on the next. let mut joined = String::new(); - for raw in text.lines() { + for (i, raw) in text.lines().enumerate() { + let raw = if i == 0 { raw.trim_start_matches('\u{feff}') } else { raw }; let line = raw.split('#').next().unwrap_or(""); if line.len() > limits.max_data_bytes || joined.len().checked_add(line.len()).is_none_or(|n| n > limits.max_data_bytes) { return Err(too_large(KIND, "a continued line exceeds the memory limit")); @@ -501,6 +520,9 @@ pub fn read(bytes: &[u8], limits: &Limits, resolve: Resolver) -> Result { } } r.line(joined.trim(), resolve)?; + if options.strict && r.bad_lines > 0 { + return Err(malformed(KIND, "invalid vertex or face line")); + } r.finish() } diff --git a/crates/mesh/src/tests/obj.rs b/crates/mesh/src/tests/obj.rs index 5283012c4..583fcb4a0 100644 --- a/crates/mesh/src/tests/obj.rs +++ b/crates/mesh/src/tests/obj.rs @@ -352,3 +352,15 @@ fn face_tokens_and_continued_lines_are_bounded_before_collection() { let invalid = b"v 0 0 0\nf 1 1 1\n\xff"; assert!(matches!(read(invalid, &Limits { max_data_bytes: 4, ..Limits::default() }, &mut no_external), Err(Error::TooLarge(_)))); } + +#[test] +fn compatibility_options_keep_runs_and_skip_material_libraries() { + let input = b"\xef\xbb\xbf v 0 0 0\nv 1 0 0\nv 0 1 0\nmtllib missing.mtl\nusemtl Red\nf 1 2 3\nusemtl Blue\nf 1 2 3\nusemtl Red\nf 1 2 3\n"; + let options = crate::obj::ReadOptions { strict: true, preserve_runs: true, ignore_material_libraries: true }; + let scene = crate::obj::read_with_options(input, &Limits::default(), &mut no_external, &options).unwrap(); + let parts = &scene.meshes[0].parts; + assert_eq!(parts.len(), 3); + assert_eq!(parts[0].material, parts[2].material); + assert!(scene.warnings.is_empty()); + assert!(crate::obj::read_with_options(b"v NaN 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 3\n", &Limits::default(), &mut no_external, &options).is_err()); +} From 2ee29d87b59f13de6a629df25b1151922303869a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 19:18:54 +0000 Subject: [PATCH 135/150] Preserve repeated OBJ group declarations --- crates/mesh/src/obj.rs | 6 ++++++ crates/mesh/src/tests/obj.rs | 11 ++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/mesh/src/obj.rs b/crates/mesh/src/obj.rs index 0a4cc3b68..b59f903d5 100644 --- a/crates/mesh/src/obj.rs +++ b/crates/mesh/src/obj.rs @@ -176,6 +176,8 @@ pub struct ReadOptions { pub preserve_runs: bool, /// Do not resolve or report material libraries; `usemtl` names remain available. pub ignore_material_libraries: bool, + /// Treat each group declaration as an object boundary, retaining repeated names. + pub groups_as_objects: bool, } struct Reader<'a> { @@ -407,6 +409,10 @@ impl Reader<'_> { }; } "o" => self.start_object(after.to_string())?, + "g" if self.options.groups_as_objects => { + self.group.clear(); + self.start_object(after.to_string())?; + } "g" => { self.slot = None; self.group = after.to_string(); diff --git a/crates/mesh/src/tests/obj.rs b/crates/mesh/src/tests/obj.rs index 583fcb4a0..ed8a89755 100644 --- a/crates/mesh/src/tests/obj.rs +++ b/crates/mesh/src/tests/obj.rs @@ -356,7 +356,7 @@ fn face_tokens_and_continued_lines_are_bounded_before_collection() { #[test] fn compatibility_options_keep_runs_and_skip_material_libraries() { let input = b"\xef\xbb\xbf v 0 0 0\nv 1 0 0\nv 0 1 0\nmtllib missing.mtl\nusemtl Red\nf 1 2 3\nusemtl Blue\nf 1 2 3\nusemtl Red\nf 1 2 3\n"; - let options = crate::obj::ReadOptions { strict: true, preserve_runs: true, ignore_material_libraries: true }; + let options = crate::obj::ReadOptions { strict: true, preserve_runs: true, ignore_material_libraries: true, ..Default::default() }; let scene = crate::obj::read_with_options(input, &Limits::default(), &mut no_external, &options).unwrap(); let parts = &scene.meshes[0].parts; assert_eq!(parts.len(), 3); @@ -364,3 +364,12 @@ fn compatibility_options_keep_runs_and_skip_material_libraries() { assert!(scene.warnings.is_empty()); assert!(crate::obj::read_with_options(b"v NaN 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 3\n", &Limits::default(), &mut no_external, &options).is_err()); } + +#[test] +fn group_boundaries_keep_repeated_names_separate() { + let input = b"v 0 0 0\nv 1 0 0\nv 0 1 0\ng Same\nusemtl M\nf 1 2 3\ng Same\nf 1 3 2\n"; + let options = crate::obj::ReadOptions { groups_as_objects: true, ..Default::default() }; + let scene = crate::obj::read_with_options(input, &Limits::default(), &mut no_external, &options).unwrap(); + assert_eq!(scene.meshes.len(), 2); + assert!(scene.meshes.iter().all(|m| m.name == "Same")); +} From 316d40376e0e4a8b4991ab428c5e0730688a7219 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 19:19:15 +0000 Subject: [PATCH 136/150] Keep OBJ declaration groups across smoothing changes --- crates/mesh/src/obj.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/mesh/src/obj.rs b/crates/mesh/src/obj.rs index b59f903d5..69428eb25 100644 --- a/crates/mesh/src/obj.rs +++ b/crates/mesh/src/obj.rs @@ -400,6 +400,7 @@ impl Reader<'_> { self.vn.push(n.unwrap_or(NAN3)); self.bad_lines += usize::from(n.is_none()); } + "s" if self.options.groups_as_objects => {} "s" => { self.slot = None; self.smoothing = match it.next() { From 6c09292724ec8dbe32950cb5042cfaa2a614fb6c Mon Sep 17 00:00:00 2001 From: SteamSmith <341182916+SteamSmith@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:27:36 -0500 Subject: [PATCH 137/150] cms: share bounded ICC v2 matrix-profile serialization Extract the TextureCraft writer originally introduced in 59f0ab48508e2dc5a812acd1aee00d5d59a528d8, retaining caller colour and timestamp policy. Preserve the is_multiple_of cleanup from 59a3018. Co-authored-by: bflatastic --- crates/cms/src/lib.rs | 1 + crates/cms/src/write_v2.rs | 195 +++++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 crates/cms/src/write_v2.rs diff --git a/crates/cms/src/lib.rs b/crates/cms/src/lib.rs index 5e8a1e268..abd2b2604 100644 --- a/crates/cms/src/lib.rs +++ b/crates/cms/src/lib.rs @@ -34,6 +34,7 @@ pub mod profile; pub mod synth; pub mod transform; mod write; +pub mod write_v2; pub use builtin::Builtin; pub use clut::Clut; diff --git a/crates/cms/src/write_v2.rs b/crates/cms/src/write_v2.rs new file mode 100644 index 000000000..63933c801 --- /dev/null +++ b/crates/cms/src/write_v2.rs @@ -0,0 +1,195 @@ +//! Bounded ICC v2.1 matrix/TRC display-profile writer, extracted from TextureCraft. +//! Colour-space selection and transfer-curve sampling remain caller policy. + +const D50_XYZ: [f64; 3] = [0.9642, 1.0, 0.8249]; + +/// One common red/green/blue transfer curve. +#[derive(Clone, Copy)] +pub enum ToneCurve<'a> { + /// ICC gamma 1.0 (`curv` with one u8Fixed8 entry). + Linear, + /// Already quantized samples, inclusive endpoints, between 2 and 4096 entries. + Samples(&'a [u16]), +} + +/// Parameters for an ICC v2.1 RGB display profile. +pub struct MatrixProfile<'a> { + /// RGB-to-XYZ matrix already adapted to PCS D50, in row-major order. + pub matrix: [[f64; 3]; 3], + /// Common RGB transfer curve; its storage is shared by all three tags. + pub curve: ToneCurve<'a>, + /// ASCII description, at most 1024 bytes with no embedded NUL. + pub description: &'a str, + /// ASCII copyright text, at most 1024 bytes with no embedded NUL. + pub copyright: &'a str, + /// Raw ICC date fields: year, month, day, hour, minute, second. + pub date: [u16; 6], +} + +fn s15f16(v: f64) -> [u8; 4] { + ((v * 65536.0).round().clamp(f64::from(i32::MIN), f64::from(i32::MAX)) as i32).to_be_bytes() +} + +fn xyz_tag(v: [f64; 3]) -> Vec { + let mut b = b"XYZ \0\0\0\0".to_vec(); + for c in v { + b.extend_from_slice(&s15f16(c)); + } + b +} + +fn text_tag(text: &str) -> Vec { + let mut b = b"text\0\0\0\0".to_vec(); + b.extend_from_slice(text.as_bytes()); + b.push(0); + b +} + +/// `textDescriptionType` with the ASCII description only. +fn desc_tag(text: &str) -> Vec { + let mut b = b"desc\0\0\0\0".to_vec(); + b.extend_from_slice(&(text.len() as u32 + 1).to_be_bytes()); + b.extend_from_slice(text.as_bytes()); + b.push(0); + // No Unicode text and no Macintosh ScriptCode text: empty fields and the 67-byte filler. + b.extend_from_slice(&[0; 4 + 4 + 2 + 1 + 67]); + b +} + +fn curve_tag(curve: ToneCurve<'_>) -> Vec { + let mut b = b"curv\0\0\0\0".to_vec(); + match curve { + ToneCurve::Linear => { + b.extend_from_slice(&1u32.to_be_bytes()); + b.extend_from_slice(&0x0100u16.to_be_bytes()); + } + ToneCurve::Samples(samples) => { + b.extend_from_slice(&(samples.len() as u32).to_be_bytes()); + for sample in samples { + b.extend_from_slice(&sample.to_be_bytes()); + } + } + } + b +} + +/// Write a deterministic ICC v2.1 RGB display profile with one shared RGB TRC. +/// +/// Returns `None` for nonfinite matrix values, descriptions longer than 1024 ASCII +/// bytes or containing NUL, or sampled curves outside 2..=4096 entries. These caps +/// bound every allocation independently of caller input. Matrix values outside the +/// signed 15.16 range saturate, matching the legacy exporter. The caller owns colour +/// adaptation, transfer sampling, names and timestamp policy. +pub fn matrix_profile(params: &MatrixProfile<'_>) -> Option> { + let valid_text = |s: &str| s.len() <= 1024 && s.is_ascii() && !s.contains('\0'); + if !valid_text(params.description) + || !valid_text(params.copyright) + || !params.matrix.iter().flatten().all(|v| v.is_finite()) + || matches!(params.curve, ToneCurve::Samples(s) if !(2..=4096).contains(&s.len())) + { + return None; + } + let m = params.matrix; + let column = |c: usize| [m[0][c], m[1][c], m[2][c]]; + let curve = curve_tag(params.curve); + // The green and blue curves are the red one's entry. + let tags: [(&[u8; 4], Vec); 9] = [ + (b"desc", desc_tag(params.description)), + (b"cprt", text_tag(params.copyright)), + (b"wtpt", xyz_tag(D50_XYZ)), + (b"rXYZ", xyz_tag(column(0))), + (b"gXYZ", xyz_tag(column(1))), + (b"bXYZ", xyz_tag(column(2))), + (b"rTRC", curve.clone()), + (b"gTRC", curve.clone()), + (b"bTRC", curve), + ]; + // Header (128) + tag count (4) + 12 per tag, then the tag data, each tag padded to 4 bytes. + let table_end = 128 + 4 + 12 * tags.len(); + let mut offsets = Vec::new(); + let mut data: Vec = Vec::new(); + for (i, (_, bytes)) in tags.iter().enumerate() { + let shared = i >= 7; + let at = if shared { offsets.get(6).copied().unwrap_or(0) } else { table_end + data.len() }; + offsets.push(at); + if !shared { + data.extend_from_slice(bytes); + while !data.len().is_multiple_of(4) { + data.push(0); + } + } + } + let total = table_end + data.len(); + let mut out = vec![0u8; 128]; + out.get_mut(0..4)?.copy_from_slice(&(total as u32).to_be_bytes()); + out.get_mut(8..12)?.copy_from_slice(&[2, 0x10, 0, 0]); // version 2.1.0 + out.get_mut(12..16)?.copy_from_slice(b"mntr"); + out.get_mut(16..20)?.copy_from_slice(b"RGB "); + out.get_mut(20..24)?.copy_from_slice(b"XYZ "); + // Serialize caller-supplied ICC date fields; no calendar arithmetic. + for (i, value) in params.date.iter().enumerate() { + out.get_mut(24 + i * 2..26 + i * 2)?.copy_from_slice(&value.to_be_bytes()); + } + out.get_mut(36..40)?.copy_from_slice(b"acsp"); + for (k, v) in D50_XYZ.iter().enumerate() { + out.get_mut(68 + 4 * k..72 + 4 * k)?.copy_from_slice(&s15f16(*v)); + } + out.extend_from_slice(&(tags.len() as u32).to_be_bytes()); + for ((sig, bytes), at) in tags.iter().zip(&offsets) { + out.extend_from_slice(*sig); + out.extend_from_slice(&(*at as u32).to_be_bytes()); + out.extend_from_slice(&(bytes.len() as u32).to_be_bytes()); + } + out.extend_from_slice(&data); + Some(out) +} + +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used)] + use super::*; + + fn params() -> MatrixProfile<'static> { + MatrixProfile { + matrix: [[0.4361, 0.3851, 0.1431], [0.2225, 0.7169, 0.0606], [0.0139, 0.0971, 0.7141]], + curve: ToneCurve::Linear, + description: "v2", + copyright: "free", + date: [2026, 1, 1, 0, 0, 0], + } + } + + #[test] + fn valid_profile_parses_and_aliases_curves() { + for curve in [ToneCurve::Linear, ToneCurve::Samples(&[0, 32768, 65535])] { + let bytes = matrix_profile(&MatrixProfile { curve, ..params() }).unwrap(); + let p = crate::Profile::parse(&bytes).unwrap(); + assert_eq!(p.version, (2, 0x10)); + assert_eq!(p.description, "v2"); + assert_eq!(&bytes[24..28], &[7, 234, 0, 1]); + assert_eq!(&bytes[208..216], &bytes[220..228]); + assert_eq!(&bytes[208..216], &bytes[232..240]); + assert_eq!(bytes, matrix_profile(&MatrixProfile { curve, ..params() }).unwrap()); + } + } + + #[test] + fn bounded_inputs_reject_before_serializing() { + let long = "x".repeat(1025); + for description in [long.as_str(), "bad\0name", "non-ascii é"] { + assert!(matrix_profile(&MatrixProfile { description, ..params() }).is_none()); + } + for samples in [&[][..], &[0][..], &vec![0; 4097][..]] { + assert!(matrix_profile(&MatrixProfile { curve: ToneCurve::Samples(samples), ..params() }).is_none()); + } + for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] { + let mut p = params(); + p.matrix[0][0] = value; + assert!(matrix_profile(&p).is_none()); + } + let samples = vec![0; 4096]; + let name = "x".repeat(1024); + let bytes = matrix_profile(&MatrixProfile { description: &name, copyright: &name, curve: ToneCurve::Samples(&samples), ..params() }).unwrap(); + assert!(bytes.len() < 12 * 1024); + } +} From 201601fa2d5dab0f1aa0e6f2581751bac088a495 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 14:31:01 -0500 Subject: [PATCH 138/150] Fix external stream-codecs consumers to use bounded vendored JBIG2 --- crates/stream-codecs/Cargo.toml | 5 +++-- crates/stream-codecs/src/jbig2/decode.rs | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/stream-codecs/Cargo.toml b/crates/stream-codecs/Cargo.toml index 8827f294e..14ce68603 100644 --- a/crates/stream-codecs/Cargo.toml +++ b/crates/stream-codecs/Cargo.toml @@ -15,8 +15,9 @@ workspace = true fax = "0.2.7" # CCITT fax decoding, the decoder the vendored PDF reader uses. MIT OR Apache-2.0. hayro-ccitt = { version = "0.3", default-features = false } -# JBIG2 decoding, the decoder the vendored PDF reader uses (the workspace patches it to `vendor/hayro-jbig2`). -hayro-jbig2 = { version = "=0.3.0", default-features = false, features = ["std"] } +# The bounded JBIG2 API is a local extension: consumers do not inherit workspace patches. +# Keep the vendored dependency explicit so Git consumers retain its work-budget safeguards. +hayro-jbig2 = { path = "../../vendor/hayro-jbig2", version = "=0.3.0", default-features = false, features = ["std"] } thiserror = { workspace = true } [dev-dependencies] diff --git a/crates/stream-codecs/src/jbig2/decode.rs b/crates/stream-codecs/src/jbig2/decode.rs index e5a49d359..0e04bd648 100644 --- a/crates/stream-codecs/src/jbig2/decode.rs +++ b/crates/stream-codecs/src/jbig2/decode.rs @@ -78,6 +78,15 @@ mod tests { use super::*; use crate::{encode_jbig2, encode_jbig2_generic}; + /// A registry fallback lacks the budget APIs and breaks consumers outside this workspace. + #[test] + fn bounded_decoder_dependency_is_explicit_for_external_consumers() { + let manifest = include_str!("../../Cargo.toml"); + let dependency = manifest.lines().find(|line| line.starts_with("hayro-jbig2 =")).unwrap(); + assert!(dependency.contains(r#"path = "../../vendor/hayro-jbig2""#)); + assert!(dependency.contains(r#"version = "=0.3.0""#)); + } + /// [`decode_jbig2_within`] with no limit on the work. fn decode_jbig2(data: &[u8], globals: Option<&[u8]>, columns: u32, rows: u32, max: usize) -> Result, CodecError> { let mut unbounded = u64::MAX; From 5e2bc54a48c9f03e7ce4eca42775ce5cc051f5d6 Mon Sep 17 00:00:00 2001 From: Jason D Date: Sun, 15 Nov 2026 14:38:21 -0500 Subject: [PATCH 139/150] psd: share raw channel display and alternate spot records Port raw framing from PhotoCraft f7f8fdcd89 and 9a390661b5; preserve tolerant import semantics and keep colour policy with consumers. Co-authored-by: Khawar Zamman Wani Co-authored-by: Dylan M. Taylor --- crates/psd/src/channel_resources.rs | 111 ++++++++++++++++++++++++++++ crates/psd/src/lib.rs | 1 + 2 files changed, 112 insertions(+) create mode 100644 crates/psd/src/channel_resources.rs diff --git a/crates/psd/src/channel_resources.rs b/crates/psd/src/channel_resources.rs new file mode 100644 index 000000000..204d36eb0 --- /dev/null +++ b/crates/psd/src/channel_resources.rs @@ -0,0 +1,111 @@ +//! Raw PSD channel DisplayInfo (1077/1007) and Alternate Spot Colors (1067) records. +//! +//! Ported from PhotoCraft channel_map: Jason D's f7f8fdcd89 DisplayInfo framing and +//! Khawar Zamman Wani's 9a390661b5 alternate-spot reader. Colour interpretation, opacity +//! clamping, channel matching and legacy compatibility decisions belong to the caller. +//! Readers borrow their input and ignore a trailing partial record, preserving the +//! original tolerant import behavior. Version words are framing, not validation here. + +/// One 13-byte DisplayInfo record, without document-specific colour interpretation. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct DisplayInfo { + /// PSD colour-space identifier. + pub space: u16, + /// Four raw colour components. + pub components: [u16; 4], + /// Raw opacity (normally 0 through 100). + pub opacity: u16, + /// Raw channel kind (0 selected, 1 masked, 2 spot). + pub kind: u8, +} + +impl DisplayInfo { + /// Encode the version-1 resource's 13-byte record, without clamping fields. + pub fn to_bytes(self) -> [u8; 13] { + let mut out = [0; 13]; + let values = [self.space, self.components[0], self.components[1], self.components[2], self.components[3], self.opacity]; + for (slot, value) in out[..12].as_chunks_mut::<2>().0.iter_mut().zip(values) { + *slot = value.to_be_bytes(); + } + out[12] = self.kind; + out + } +} + +/// Iterate complete DisplayInfo records without allocating. +/// +/// `versioned` selects resource 1077 (skip four version bytes, 13-byte records), +/// otherwise resource 1007 (14-byte records, ignoring each padding byte). A short +/// version header or incomplete final record yields no corresponding record. +pub fn display_info(data: &[u8], versioned: bool) -> impl ExactSizeIterator + '_ { + let (body, size) = if versioned { (data.get(4..).unwrap_or(&[]), 13) } else { (data, 14) }; + body.chunks_exact(size).map(|rec| { + let u = |i: usize| u16::from_be_bytes([rec[i], rec[i + 1]]); + DisplayInfo { space: u(0), components: [u(2), u(4), u(6), u(8)], opacity: u(10), kind: rec[12] } + }) +} + +/// One Alternate Spot Colors record, retaining unknown colour spaces. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct AlternateSpotColor { + /// Channel identifier from resource 1053. + pub id: u32, + /// PSD colour-space identifier. + pub space: u16, + /// Four raw colour components. + pub components: [u16; 4], +} + +/// Iterate available complete 14-byte records up to the resource's declared u16 count. +/// +/// Short headers, absent declared records and trailing partial bytes are tolerated exactly +/// as in the original importer. No allocation is derived from an untrusted count. +pub fn alternate_spot_colors(data: &[u8]) -> impl ExactSizeIterator + '_ { + let count = data.get(2..4).map_or(0, |c| usize::from(u16::from_be_bytes([c[0], c[1]]))); + data.get(4..).unwrap_or_default().as_chunks::<14>().0.iter().take(count).map(|rec| { + let u = |i: usize| u16::from_be_bytes([rec[i], rec[i + 1]]); + AlternateSpotColor { id: u32::from_be_bytes([rec[0], rec[1], rec[2], rec[3]]), space: u(4), components: [u(6), u(8), u(10), u(12)] } + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn display_round_trip_and_truncation() { + let record = DisplayInfo { space: 0x2331, components: [0, 1, 255, u16::MAX], opacity: 500, kind: 255 }; + let mut modern = 99u32.to_be_bytes().to_vec(); + modern.extend(record.to_bytes()); + assert_eq!(display_info(&modern, true).collect::>(), [record]); + for n in 0..modern.len() { + assert_eq!(display_info(&modern[..n], true).len(), 0); + } + modern.extend([1, 2, 3]); + assert_eq!(display_info(&modern, true).len(), 1); + let mut old = record.to_bytes().to_vec(); + old.push(42); + old.extend(record.to_bytes()); + old.push(99); + assert_eq!(display_info(&old, false).collect::>(), [record, record]); + assert_eq!(display_info(&old[..27], false).len(), 1); + } + + #[test] + fn alternate_count_is_bounded_by_available_records() { + let mut bytes = vec![0, 9, 255, 255]; + bytes.extend(123u32.to_be_bytes()); + for value in [7u16, 10000, 0xffff, 4, 5] { + bytes.extend(value.to_be_bytes()); + } + let want = AlternateSpotColor { id: 123, space: 7, components: [10000, 0xffff, 4, 5] }; + assert_eq!(alternate_spot_colors(&bytes).collect::>(), [want]); + for n in 0..bytes.len() { + assert_eq!(alternate_spot_colors(&bytes[..n]).len(), 0); + } + bytes.extend([1, 2, 3]); + assert_eq!(alternate_spot_colors(&bytes).len(), 1); + bytes[2..4].copy_from_slice(&0u16.to_be_bytes()); + assert_eq!(alternate_spot_colors(&bytes).len(), 0); + } +} diff --git a/crates/psd/src/lib.rs b/crates/psd/src/lib.rs index 4c3222f46..0a99ae34c 100644 --- a/crates/psd/src/lib.rs +++ b/crates/psd/src/lib.rs @@ -60,6 +60,7 @@ pub mod asl; pub mod atn; pub mod blend; pub mod builder; +pub mod channel_resources; mod codepage; pub mod compression; pub mod csh; From 8ef4542960214e2822fee15d994e7d58fd1f5122 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 14:39:53 -0500 Subject: [PATCH 140/150] Fix DDS test fixture Header module path --- crates/texture/src/dds.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/texture/src/dds.rs b/crates/texture/src/dds.rs index 77b053c31..6971ad461 100644 --- a/crates/texture/src/dds.rs +++ b/crates/texture/src/dds.rs @@ -54,7 +54,7 @@ mod tests { fn bytes(format: ::dds::Format, payload: &[u8]) -> Vec { let mut bytes = Vec::new(); - ::dds::Header::new_image(1, 1, format).write(&mut bytes).unwrap(); + ::dds::header::Header::new_image(1, 1, format).write(&mut bytes).unwrap(); bytes.extend_from_slice(payload); bytes } @@ -86,7 +86,7 @@ mod tests { assert_eq!(image.rgba, [-0.5, 2.0, 0.25, 0.75]); assert_eq!(image.source_precision_is_f32, ::dds::Format::R16G16B16A16_FLOAT.precision() == ::dds::Precision::F32); let mut compressed = Vec::new(); - ::dds::Header::new_image(4, 4, ::dds::Format::BC1_UNORM).write(&mut compressed).unwrap(); + ::dds::header::Header::new_image(4, 4, ::dds::Format::BC1_UNORM).write(&mut compressed).unwrap(); compressed.extend_from_slice(&[0, 0xf8, 0, 0, 0, 0, 0, 0]); let image = decode_first(&compressed, &Limits::default()).unwrap(); assert!(!image.source_precision_is_f32); @@ -96,7 +96,7 @@ mod tests { #[test] fn cube_keeps_first_surface_only_like_materialcraft() { let mut data = Vec::new(); - ::dds::Header::new_cube_map(1, 1, ::dds::Format::R8G8B8A8_UNORM).write(&mut data).unwrap(); + ::dds::header::Header::new_cube_map(1, 1, ::dds::Format::R8G8B8A8_UNORM).write(&mut data).unwrap(); data.extend_from_slice(&[255, 0, 0, 255]); data.extend_from_slice(&[0; 20]); let image = decode_first(&data, &Limits::default()).unwrap(); From 556d02941ad3b97b1d109e9c08d4477d647deb1f Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 15 Nov 2026 14:49:24 -0500 Subject: [PATCH 141/150] Extract bounded USD Crate LZ4 decoding from TextureCraft Source: storytold/texturecraft bdbb52c9b66b221a55b370105b42f485a76e6fc7. Preserve chunk framing and output cap policy, with fallible allocation and checked offsets. --- Cargo.lock | 14 +++++++ Cargo.toml | 1 + crates/usd/Cargo.toml | 15 +++++++ crates/usd/README.md | 7 ++++ crates/usd/src/lib.rs | 95 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 crates/usd/Cargo.toml create mode 100644 crates/usd/README.md create mode 100644 crates/usd/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index b3325f790..d1f177658 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2855,6 +2855,14 @@ dependencies = [ "zip", ] +[[package]] +name = "craft-usd" +version = "0.1.0" +dependencies = [ + "lz4_flex", + "thiserror 2.0.21", +] + [[package]] name = "craft-vcard" version = "0.1.0" @@ -6054,6 +6062,12 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4050469837a6ff301cd14c1f8f24f88549e6d548f24f64e2148eb0f72cebc51f" +[[package]] +name = "lz4_flex" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "373f5eceeeab7925e0c1098212f2fbc4d416adec9d35051a6ab251e824c1854a" + [[package]] name = "maybe-owned" version = "0.3.4" diff --git a/Cargo.toml b/Cargo.toml index 0c9729e00..db3902687 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ rust-version = "1.95" repository = "https://github.com/storytold/craft-libs" [workspace.dependencies] +craft-usd = { path = "crates/usd" } craft-smime = { path = "crates/smime" } craft-ber = { path = "crates/ber" } craft-proto = { path = "crates/proto" } diff --git a/crates/usd/Cargo.toml b/crates/usd/Cargo.toml new file mode 100644 index 000000000..ac1c64d0c --- /dev/null +++ b/crates/usd/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "craft-usd" +description = "Bounded USD Crate wire primitives, independent of scene and material policy" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +lz4_flex = { version = "0.11", default-features = false, features = ["safe-decode", "safe-encode"] } +thiserror.workspace = true diff --git a/crates/usd/README.md b/crates/usd/README.md new file mode 100644 index 000000000..606b5b250 --- /dev/null +++ b/crates/usd/README.md @@ -0,0 +1,7 @@ +# craft-usd + +Bounded wire primitives for USD Crate, independent of application scene and material models. + +Currently provides Crate-framed LZ4 decompression only: a single unprefixed block or up to 127 length-prefixed blocks, under a caller-provided output-byte cap with fallible output allocation. Trailing bytes after counted chunks retain the original reader's ignored-tail policy. This is not a complete USD reader/writer; scene interpretation, integer packing and export remain outside this first extraction. + +Adapted from TextureCraft `crates/formats/src/mesh_import/usd/codec.rs`, introduced by bflatastic in `bdbb52c9b66b221a55b370105b42f485a76e6fc7`. The LZ4 backend is MIT-licensed `lz4_flex`. diff --git a/crates/usd/src/lib.rs b/crates/usd/src/lib.rs new file mode 100644 index 000000000..943bf1d4d --- /dev/null +++ b/crates/usd/src/lib.rs @@ -0,0 +1,95 @@ +//! Bounded USD Crate wire primitives. Not a complete USD scene reader or writer. +//! +//! The framing decoder is adapted from TextureCraft's `usd/codec.rs`, originally +//! bflatastic, commit bdbb52c9b66b221a55b370105b42f485a76e6fc7. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +/// Failure to read a bounded Crate LZ4 frame. +#[derive(Debug, thiserror::Error)] +pub enum Error { + /// Invalid or incomplete chunk framing. + #[error("invalid USD Crate LZ4 framing: {0}")] + Framing(&'static str), + /// Invalid compressed data or output exceeding the supplied capacity. + #[error("USD Crate LZ4 data: {0}")] + Lz4(#[from] lz4_flex::block::DecompressError), + /// The bounded output allocation could not be reserved. + #[error("cannot allocate USD Crate LZ4 output")] + Allocation, +} + +/// Decode Crate's chunk-count byte and LZ4 blocks to at most `max_output` bytes. +/// +/// A zero count means a single unprefixed block; 1..=127 means that many blocks, +/// each preceded by a signed little-endian 32-bit length. As in the original reader, +/// bytes following the declared chunks are ignored. The output allocation is fallible +/// and capped by both the caller's budget and the input's maximum LZ4 expansion. +/// This bounds the returned plane, not backend internal scratch memory. +pub fn decompress(input: &[u8], max_output: usize) -> Result, Error> { + let (&chunks, mut rest) = input.split_first().ok_or(Error::Framing("empty block"))?; + if chunks > 127 { + return Err(Error::Framing("chunk count exceeds 127")); + } + let capacity = max_output.min(input.len().saturating_mul(256).saturating_add(64)); + let mut output = Vec::new(); + output.try_reserve_exact(capacity).map_err(|_| Error::Allocation)?; + output.resize(capacity, 0); + let written = if chunks == 0 { + lz4_flex::block::decompress_into(rest, &mut output)? + } else { + let mut total = 0usize; + for _ in 0..chunks { + let length = rest.get(..4).and_then(|b| b.try_into().ok()).map(i32::from_le_bytes).ok_or(Error::Framing("missing chunk length"))?; + let length = usize::try_from(length).map_err(|_| Error::Framing("negative chunk length"))?; + let end = 4usize.checked_add(length).ok_or(Error::Framing("chunk length overflow"))?; + let chunk = rest.get(4..end).ok_or(Error::Framing("truncated chunk"))?; + rest = rest.get(end..).unwrap_or_default(); + let tail = output.get_mut(total..).ok_or(Error::Framing("output offset overflow"))?; + let count = lz4_flex::block::decompress_into(chunk, tail)?; + total = total.checked_add(count).ok_or(Error::Framing("output length overflow"))?; + } + total + }; + output.truncate(written); + Ok(output) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + + #[test] + fn single_block_matches_original_wire() { + for n in [0, 1, 15, 16, 255, 4096] { + let raw: Vec = (0..n).map(|i| (i % 251) as u8).collect(); + let mut frame = vec![0]; + frame.extend(lz4_flex::block::compress(&raw)); + assert_eq!(decompress(&frame, raw.len()).unwrap(), raw); + if n > 0 { + assert!(decompress(&frame, n - 1).is_err()); + } + } + } + + #[test] + fn counted_blocks_and_trailing_bytes_keep_existing_policy() { + let mut frame = vec![2]; + for raw in [b"first".as_slice(), b"second".as_slice()] { + let block = lz4_flex::block::compress(raw); + frame.extend((block.len() as i32).to_le_bytes()); + frame.extend(block); + } + frame.extend(b"ignored"); + assert_eq!(decompress(&frame, 11).unwrap(), b"firstsecond"); + assert!(decompress(&frame, 10).is_err()); + } + + #[test] + fn malformed_frames_fail_within_budget() { + for data in [vec![], vec![128], vec![1], vec![1, 255, 255, 255, 255], vec![1, 5, 0, 0, 0, 1]] { + assert!(decompress(&data, 32).is_err()); + } + } +} From 808730f49b9637b7909fe2ae56f9d63fdb100cae Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 14:57:56 -0500 Subject: [PATCH 142/150] ps: preserve reencoded glyph names in fallback fonts --- crates/ps/src/read/font.rs | 67 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/crates/ps/src/read/font.rs b/crates/ps/src/read/font.rs index 9a1af3fb6..af0c6e3c2 100644 --- a/crates/ps/src/read/font.rs +++ b/crates/ps/src/read/font.rs @@ -43,9 +43,27 @@ fn substitute_type42(ip: &mut Interp, font_name: &[u8], bytes: &[u8]) -> R Option { b"oslash" => 'ø', b"oe" => 'Å“', b"germandbls" => 'ß', - _ => return None, + _ => { + return crate::read::encodings::iso_latin1() + .iter() + .enumerate() + .skip(160) + .find(|(_, glyph)| glyph.as_bytes() == name) + .and_then(|(code, _)| char::from_u32(code as u32)); + } }) } @@ -747,5 +772,43 @@ mod tests { assert_eq!(standard_glyph_char(b"quoteleft"), Some('‘')); assert_eq!(standard_glyph_char(b"quoteright"), Some('’')); assert_eq!(standard_glyph_char(b"unmapped.custom"), None); + assert_eq!(standard_glyph_char(b"eacute"), Some('é')); + assert_eq!(standard_glyph_char(b"Adieresis"), Some('Ä')); + assert_eq!(standard_glyph_char(b"space"), Some(' ')); + } + /// A tiny generated SFNT directory with named glyphs, not a copied font asset. + fn named_font() -> Vec { + let maxp = [0, 0, 0x50, 0, 0, 3]; + let mut post = vec![0u8; 32]; + post[..4].copy_from_slice(&[0, 2, 0, 0]); + post.extend_from_slice(&[0, 3, 0, 0, 1, 2, 1, 3]); + for n in [b"eacute".as_slice(), b"custom.accent"] { + post.push(n.len() as u8); + post.extend_from_slice(n); + } + let mut data = vec![0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0]; + for (tag, offset, len) in [(b"maxp", 44u32, maxp.len()), (b"post", 52u32, post.len())] { + data.extend_from_slice(tag); + data.extend_from_slice(&0u32.to_be_bytes()); + data.extend_from_slice(&offset.to_be_bytes()); + data.extend_from_slice(&(len as u32).to_be_bytes()); + } + data.extend_from_slice(&maxp); + data.resize(52, 0); + data.extend_from_slice(&post); + data + } + + #[test] + fn substitute_resolves_reencoded_names_and_charges_budget() -> Result<(), String> { + let bytes = named_font(); + let mut ip = Interp::new(vec![], (0.0, 0.0), Default::default()); + let font = substitute_type42(&mut ip, b"Fallback", &bytes).map_err(|e| format!("{e:?}"))?.ok_or("fixture font rejected")?; + assert_eq!(crate::read::type42::glyph_index(&font, b"eacute"), 1); + assert_eq!(crate::read::type42::glyph_index(&font, b"custom.accent"), 2); + assert_eq!(crate::read::type42::glyph_index(&font, b"unknown"), 0); + let mut limited = Interp::new(vec![], (0.0, 0.0), crate::read::Limits { steps: 0, ..Default::default() }); + assert!(substitute_type42(&mut limited, b"Fallback", &bytes).is_err()); + Ok(()) } } From 9d56699be8f8c04fb61f4710186b7454317afaa5 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 15 Nov 2026 15:08:20 -0500 Subject: [PATCH 143/150] psd: share borrowed Unicode channel-name record framing Extract framing from PhotoCraft 16941f7198da9ae1811a8928f4c256b5cb37c1cb, retaining the overflow protection added by 3b45e8baee7117e53847523b5bc3ae2b337966d4. Allocation-free adaptation leaves text policy with callers. Co-authored-by: Valentin Klein Co-authored-by: Dylan M. Taylor --- crates/psd/src/lib.rs | 1 + crates/psd/src/unicode_names.rs | 54 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 crates/psd/src/unicode_names.rs diff --git a/crates/psd/src/lib.rs b/crates/psd/src/lib.rs index 4c3222f46..5da308a0c 100644 --- a/crates/psd/src/lib.rs +++ b/crates/psd/src/lib.rs @@ -101,6 +101,7 @@ pub mod testgen; pub mod text; pub mod tiff; pub mod tree; +pub mod unicode_names; pub use blend::BlendMode; pub use builder::{GroupSpec, LayerSpec, MaskSpec, PixelData, PsdBuilder}; diff --git a/crates/psd/src/unicode_names.rs b/crates/psd/src/unicode_names.rs new file mode 100644 index 000000000..c4547243b --- /dev/null +++ b/crates/psd/src/unicode_names.rs @@ -0,0 +1,54 @@ +//! Counted UTF-16 name records, as used by PSD Unicode Alpha Names (resource 1045). +//! +//! Adapted from PhotoCraft's channel-name reader. Framing is shared; decoding malformed +//! UTF-16, removing terminators and assigning names to document channels remain caller policy. + +/// Iterate complete records as borrowed big-endian UTF-16 byte slices. +/// +/// A record starts with a big-endian `u32` code-unit count. Empty records are retained. +/// An incomplete count, truncated record or arithmetic overflow ends the list, preserving +/// preceding complete records. No allocation is driven by the declared count, and bytes +/// after a malformed record are not interpreted as new records. +pub fn records(mut data: &[u8]) -> impl Iterator { + std::iter::from_fn(move || { + let count = data.get(..4)?; + let count = u32::from_be_bytes([count[0], count[1], count[2], count[3]]); + let input = data; + // On any malformed length the iterator stays exhausted on subsequent calls. + data = &[]; + let len = usize::try_from(count).ok()?.checked_mul(2)?; + let end = 4usize.checked_add(len)?; + let record = input.get(4..end)?; + data = input.get(end..)?; + Some(record) + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn borrows_empty_and_uninterpreted_code_units() { + let data = [0, 0, 0, 0, 0, 0, 0, 3, 0xd8, 0, 0, 65, 0, 0]; + let found: Vec<_> = records(&data).collect(); + assert_eq!(found, [&data[4..4], &data[8..14]]); + assert_eq!(found[1].as_ptr(), data[8..].as_ptr()); + } + + #[test] + fn partial_tail_and_hostile_count_stop_without_losing_prefix() { + let first = [0, 0, 0, 1, 0, 65]; + for tail in [&[0][..], &[0, 0, 0], &[0, 0, 0, 2, 0, 66], &[255, 255, 255, 255, 0, 66]] { + let mut bytes = first.to_vec(); + bytes.extend(tail); + let mut it = records(&bytes); + assert_eq!(it.next(), Some(&first[4..])); + assert_eq!(it.next(), None); + assert_eq!(it.next(), None); + } + for end in 0..first.len() { + assert_eq!(records(&first[..end]).count(), 0); + } + } +} From 456abf7b7a2e6919b5a16e35fa2bf4e796f88674 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 15 Nov 2026 15:08:22 -0500 Subject: [PATCH 144/150] Extract bounded USD Crate integer delta decoding Retain TextureCraft signed wrapping and delta widths from bdbb52c9b66b221a55b370105b42f485a76e6fc7; use explicit widths, output byte budget, checked code-table admission and fallible reservation. --- crates/usd/README.md | 2 +- crates/usd/src/integers.rs | 127 +++++++++++++++++++++++++++++++++++++ crates/usd/src/lib.rs | 3 + 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 crates/usd/src/integers.rs diff --git a/crates/usd/README.md b/crates/usd/README.md index 606b5b250..9e9635185 100644 --- a/crates/usd/README.md +++ b/crates/usd/README.md @@ -2,6 +2,6 @@ Bounded wire primitives for USD Crate, independent of application scene and material models. -Currently provides Crate-framed LZ4 decompression only: a single unprefixed block or up to 127 length-prefixed blocks, under a caller-provided output-byte cap with fallible output allocation. Trailing bytes after counted chunks retain the original reader's ignored-tail policy. This is not a complete USD reader/writer; scene interpretation, integer packing and export remain outside this first extraction. +Provides bounded integer-delta array decoding and Crate-framed LZ4 decompression: a single unprefixed block or up to 127 length-prefixed blocks, under a caller-provided output-byte cap with fallible output allocation. Trailing bytes after counted chunks retain the original reader's ignored-tail policy. This is not a complete USD reader/writer; scene interpretation, integer packing and export remain outside this extraction. Integer decoding uses explicit 32/64-bit widths, signed wrapping arithmetic, a caller output-byte cap and fallible allocation. Adapted from TextureCraft `crates/formats/src/mesh_import/usd/codec.rs`, introduced by bflatastic in `bdbb52c9b66b221a55b370105b42f485a76e6fc7`. The LZ4 backend is MIT-licensed `lz4_flex`. diff --git a/crates/usd/src/integers.rs b/crates/usd/src/integers.rs new file mode 100644 index 000000000..c50dd0e0f --- /dev/null +++ b/crates/usd/src/integers.rs @@ -0,0 +1,127 @@ +//! USD Crate integer delta decoding, adapted from TextureCraft's bflatastic implementation. + +/// Wire width of each integer and its common delta. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Width { + /// Signed 32-bit wrapping arithmetic, sign-extended to i64 on output. + I32, + /// Signed 64-bit wrapping arithmetic. + I64, +} +impl Width { + fn bytes(self) -> usize { + match self { + Self::I32 => 4, + Self::I64 => 8, + } + } +} + +/// Invalid packed data or a caller output-budget/allocation failure. +#[derive(Debug, thiserror::Error)] +pub enum Error { + /// The declared count exceeds the caller's byte budget or addressable output. + #[error("USD integer array exceeds output budget")] + Limit, + /// A common delta, code table or selected delta is incomplete. + #[error("truncated USD integer array")] + Truncated, + /// The bounded output reservation failed. + #[error("cannot allocate USD integer array")] + Allocation, +} + +fn signed(bytes: &[u8], at: usize, width: usize) -> Option { + let part = bytes.get(at..at.checked_add(width)?)?; + Some(match width { + 1 => i64::from(i8::from_le_bytes(part.try_into().ok()?)), + 2 => i64::from(i16::from_le_bytes(part.try_into().ok()?)), + 4 => i64::from(i32::from_le_bytes(part.try_into().ok()?)), + 8 => i64::from_le_bytes(part.try_into().ok()?), + _ => return None, + }) +} + +/// Decode `count` values, charging eight output bytes per value against `max_output_bytes`. +/// +/// The common delta is followed by a packed two-bit code per value and variable-width +/// exceptional deltas. Arithmetic wraps at the specified wire width. Extra bytes after the +/// values are ignored, matching the original reader. Counts, the code table and the output +/// byte budget are checked before a fallible reservation. A zero count returns an empty vector. +pub fn decode(data: &[u8], count: usize, width: Width, max_output_bytes: usize) -> Result, Error> { + let output_bytes = count.checked_mul(8).filter(|n| *n <= max_output_bytes && *n <= isize::MAX as usize).ok_or(Error::Limit)?; + if output_bytes == 0 { + return Ok(Vec::new()); + } + let size = width.bytes(); + let common = signed(data, 0, size).ok_or(Error::Truncated)?; + let code_bytes = count.div_ceil(4); + let mut at = size.checked_add(code_bytes).ok_or(Error::Truncated)?; + let codes = data.get(size..at).ok_or(Error::Truncated)?; + let mut output = Vec::new(); + output.try_reserve_exact(count).map_err(|_| Error::Allocation)?; + let mut previous = 0i64; + for i in 0..count { + let code = (codes.get(i / 4).copied().ok_or(Error::Truncated)? >> (2 * (i % 4))) & 3; + let delta = if code == 0 { + common + } else { + let bytes = (size >> (3 - usize::from(code))).max(1); + let value = signed(data, at, bytes).ok_or(Error::Truncated)?; + at = at.checked_add(bytes).ok_or(Error::Truncated)?; + value + }; + previous = match width { + Width::I32 => i64::from((previous as i32).wrapping_add(delta as i32)), + Width::I64 => previous.wrapping_add(delta), + }; + output.push(previous); + } + Ok(output) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + + #[test] + fn common_deltas_and_output_budget() { + let mut data = 1i32.to_le_bytes().to_vec(); + data.push(0); + assert_eq!(decode(&data, 4, Width::I32, 32).unwrap(), [1, 2, 3, 4]); + assert!(matches!(decode(&data, 4, Width::I32, 31), Err(Error::Limit))); + assert!(matches!(decode(&data[..4], 4, Width::I32, 32), Err(Error::Truncated))); + assert!(matches!(decode(&[], usize::MAX, Width::I64, usize::MAX), Err(Error::Limit))); + assert!(decode(&[], 0, Width::I64, 0).unwrap().is_empty()); + } + + #[test] + fn narrow_exception_codes_sign_extend() { + for (width, size) in [(Width::I32, 4), (Width::I64, 8)] { + let mut data = vec![0; size]; + data.push(1 | (2 << 2)); + let first_size = (size >> 2).max(1); + let second_size = size >> 1; + data.extend_from_slice(&(-3i64).to_le_bytes()[..first_size]); + data.extend_from_slice(&7i64.to_le_bytes()[..second_size]); + assert_eq!(decode(&data, 2, width, 16).unwrap(), [-3, 4]); + } + } + + #[test] + fn exceptional_deltas_and_wrapping() { + for (width, size) in [(Width::I32, 4), (Width::I64, 8)] { + // Full-width positive maximum, followed by common +1, wraps to signed minimum. + let mut data = 1i64.to_le_bytes()[..size].to_vec(); + data.push(3); + let maximum = if width == Width::I32 { i64::from(i32::MAX) } else { i64::MAX }; + data.extend_from_slice(&maximum.to_le_bytes()[..size]); + let minimum = if width == Width::I32 { i64::from(i32::MIN) } else { i64::MIN }; + assert_eq!(decode(&data, 2, width, 16).unwrap(), [maximum, minimum]); + assert!(decode(&data[..data.len() - 1], 2, width, 16).is_err()); + data.extend(b"ignored tail"); + assert_eq!(decode(&data, 2, width, 16).unwrap(), [maximum, minimum]); + } + } +} diff --git a/crates/usd/src/lib.rs b/crates/usd/src/lib.rs index 943bf1d4d..edaeb89a2 100644 --- a/crates/usd/src/lib.rs +++ b/crates/usd/src/lib.rs @@ -5,6 +5,9 @@ #![forbid(unsafe_code)] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +/// Bounded Crate integer delta arrays. +pub mod integers; + /// Failure to read a bounded Crate LZ4 frame. #[derive(Debug, thiserror::Error)] pub enum Error { From 30596f73c5ef851ba3dad9cf145669f391e2eb0d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:43:10 -0500 Subject: [PATCH 145/150] Merge pull request #1707 from storytold/extract/wav-marker-metadata audio: extract WAV cue, adtl, RF64 and XMP marker metadata --- crates/audio/src/lib.rs | 2 + crates/audio/src/wav_markers.rs | 721 ++++++++++++++++++++++++++++++++ 2 files changed, 723 insertions(+) create mode 100644 crates/audio/src/wav_markers.rs diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index e0660d9c9..be0678303 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -58,6 +58,8 @@ mod read; mod track; #[cfg(any(feature = "decode-native", feature = "pcm-encode"))] mod wav; +/// Bounded WAV/RF64 cue, adtl and XMP marker metadata, without audio decoding. +pub mod wav_markers; #[cfg(any( feature = "pcm-encode", feature = "flac-encode", diff --git a/crates/audio/src/wav_markers.rs b/crates/audio/src/wav_markers.rs new file mode 100644 index 000000000..0bf4e7568 --- /dev/null +++ b/crates/audio/src/wav_markers.rs @@ -0,0 +1,721 @@ +//! Markers a WAV file carries, read from its RIFF chunks. +//! +//! Two encodings are read into separate lists for the caller to map and merge: +//! - **Cue points** (the standard): a `cue ` chunk with one point per marker, named by `labl` (or +//! `note`) sub-chunks of a `LIST` `adtl` chunk, and given a length by `ltxt`. +//! - **XMP** (Adobe's tools): an `_PMX` chunk holding an XMP packet whose `xmpDM:markers` list has +//! one entry per marker (`xmpDM:startTime`, `xmpDM:duration`, `xmpDM:name`), counted in frames +//! of `xmpDM:frameRate` (`f44100` for samples at 44.1 kHz), or with an explicit +//! FrameCount rate suffix (`15f24`, `15f30000s1001`). Durations with a distinct rate +//! are converted to the marker start rate without app-specific time quantization. +//! +//! RF64 files (WAVs over 4 GB) carry a `0xFFFFFFFF` size for a chunk too big for 32 bits and give its real size in a +//! `ds64` chunk; those sizes are resolved so the chunks after the audio are still found. +//! +//! The file is untrusted input: every read is bounds-checked, sizes use checked arithmetic, and +//! the number of markers, of `adtl` sub-chunks looked at and the size of the XMP packet are capped. + +use std::collections::{HashMap, HashSet}; + +/// Marker coordinates before app-specific time quantization. Explicit XMP duration +/// rates are normalized to the start coordinate rate; bare counts retain the legacy +/// track/file-rate interpretation. +#[derive(Clone, Debug, PartialEq)] +pub struct Marker { + /// Start in frames at `rate`. + pub start: f64, + /// Duration in frames at `rate`. + pub duration: f64, + /// Frames per second; consumers decide how invalid or absent rates map to their timeline. + pub rate: f64, + /// Label text. + pub name: String, +} +/// Separate source lists: rounding, deduplication, ordering and labels belong to the caller. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Markers { + /// RIFF cue/adtl markers in file order. + pub cue: Vec, + /// XMP markers in packet order. + pub xmp: Vec, + /// A chunk-inventory or cue-label retention limit omitted metadata. Marker coordinates + /// remain available when only their label was omitted. Existing marker/XMP prefix limits + /// are separate and do not set this flag. + pub limited: bool, +} + +/// Most markers read from one file. +pub const MAX_MARKERS: usize = 10_000; +/// Largest XMP packet read (bytes). +const MAX_XMP: usize = 4 << 20; +/// Most `adtl` sub-chunks (`labl`, `note`, `ltxt`) looked at in one file: a few per cue point is plenty. +const MAX_ADTL_RECORDS: usize = 4 * MAX_MARKERS; +/// Most entries read from an RF64 file's `ds64` size table. +const MAX_DS64_TABLE: usize = 1_024; +/// Maximum retained RIFF chunk descriptors (including empty chunks). +pub const MAX_CHUNKS: usize = 100_000; +/// Maximum UTF-8 bytes in one retained cue label. +pub const MAX_LABEL_BYTES: usize = 64 * 1024; +/// Aggregate cue label bytes, enforced separately for the name map and emitted markers. +pub const MAX_LABEL_TOTAL_BYTES: usize = 4 * 1024 * 1024; + +fn u32_at(b: &[u8], at: usize) -> Option { + let s = b.get(at..at.checked_add(4)?)?; + Some(u32::from_le_bytes(s.try_into().ok()?)) +} + +fn u64_at(b: &[u8], at: usize) -> Option { + let s = b.get(at..at.checked_add(8)?)?; + Some(u64::from_le_bytes(s.try_into().ok()?)) +} + +/// The text of a NUL-terminated label (the rest of the chunk), lossily decoded. +fn label(b: &[u8]) -> Option { + // Look only as far as one label can be retained; never copy an arbitrary chunk body. + let end = b.iter().take(MAX_LABEL_BYTES + 1).position(|&c| c == 0).unwrap_or(b.len()); + if end > MAX_LABEL_BYTES { + return None; + } + let text = String::from_utf8_lossy(b.get(..end)?); + let text = text.trim(); + (text.len() <= MAX_LABEL_BYTES).then(|| text.to_string()) +} + +/// Whether `bytes` start a RIFF/WAVE file. +pub fn is_wav(bytes: &[u8]) -> bool { + matches!(bytes.get(0..4), Some(b"RIFF" | b"RF64")) && bytes.get(8..12) == Some(b"WAVE") +} + +/// The sizes an RF64 file's `ds64` chunk gives for chunks too big for the 32-bit size field. +struct Ds64 { + /// The `data` chunk's size. + data: u64, + /// Other chunks: `(id, size)`. + table: Vec<([u8; 4], u64)>, +} + +impl Ds64 { + /// The real size of chunk `id`, when the file says. + fn size_of(&self, id: &[u8; 4]) -> Option { + if id == b"data" { Some(self.data) } else { self.table.iter().find(|(t, _)| t == id).map(|(_, size)| *size) } + } +} + +/// The `ds64` chunk of an RF64 file (`RF64`, size, `WAVE`, then `ds64`); `None` for any other file or one whose +/// `ds64` doesn't fit. +fn ds64(bytes: &[u8]) -> Option { + if bytes.get(0..4) != Some(b"RF64") || bytes.get(12..16) != Some(b"ds64") { + return None; + } + let size = usize::try_from(u32_at(bytes, 16)?).ok()?; + let body = bytes.get(20..20usize.checked_add(size)?)?; + // riff size (8), data size (8), sample count (8), table length (4), then `(id, size)` entries of 12 bytes + let data = u64_at(body, 8)?; + let entries = usize::try_from(u32_at(body, 24)?).ok()?; + let mut table = Vec::new(); + for i in 0..entries.min(MAX_DS64_TABLE) { + let at = 28usize.checked_add(i.checked_mul(12)?)?; + let id = <[u8; 4]>::try_from(body.get(at..at.checked_add(4)?)?).ok()?; + table.push((id, u64_at(body, at + 4)?)); + } + Some(Ds64 { data, table }) +} + +/// The chunks of a WAVE file as `(id, body)`, stopping at the first one that doesn't fit. +fn chunks<'a>(bytes: &'a [u8], limited: &mut bool) -> Vec<([u8; 4], &'a [u8])> { + let big = ds64(bytes); + let mut out = Vec::new(); + let mut at = 12usize; + while let Some(head) = bytes.get(at..at.saturating_add(8)) { + if out.len() == MAX_CHUNKS { + *limited = true; + break; + } + let (Ok(id), Some(field)) = (<[u8; 4]>::try_from(&head[..4]), u32_at(head, 4)) else { break }; + // In an RF64 file a size field of 0xFFFFFFFF stands for the size `ds64` gives. + let size = match &big { + Some(d) if field == u32::MAX => d.size_of(&id).unwrap_or(u64::from(field)), + _ => u64::from(field), + }; + let start = at.saturating_add(8); + let Some(end) = usize::try_from(size).ok().and_then(|n| start.checked_add(n)) else { break }; + let Some(body) = bytes.get(start..end) else { break }; + out.push((id, body)); + // Chunks are padded to an even size. + at = end.saturating_add(end & 1); + } + out +} + +/// Read bounded cue/adtl and XMP metadata without interpreting application timeline policy. +pub fn read(bytes: &[u8]) -> Markers { + if !is_wav(bytes) { + return Markers::default(); + } + let mut limited = false; + let chunks = chunks(bytes, &mut limited); + let rate = chunks.iter().find(|(id, _)| id == b"fmt ").and_then(|(_, b)| u32_at(b, 4)).map_or(0.0, f64::from); + let cue = cue_markers(&chunks, rate, &mut limited); + let xmp = chunks.iter().find(|(id, _)| id == b"_PMX").map_or_else(Vec::new, |(_, xmp)| xmp_markers(xmp, rate)); + Markers { cue, xmp, limited } +} + +/// Cue points named and sized by the `adtl` list. +fn cue_markers(chunks: &[([u8; 4], &[u8])], rate: f64, limited: &mut bool) -> Vec { + let Some((_, cue)) = chunks.iter().find(|(id, _)| id == b"cue ") else { return Vec::new() }; + let count = (u32_at(cue, 0).unwrap_or(0) as usize).min(MAX_MARKERS); + // The cue points first: `(id, offset in samples)`. + let mut points = Vec::new(); + for i in 0..count { + let Some(point) = cue.get(4usize.saturating_add(i.saturating_mul(24))..) else { break }; + let (Some(id), Some(offset)) = (u32_at(point, 0), u32_at(point, 20)) else { break }; + points.push((id, offset)); + } + // Only the names and lengths of those ids are kept, so a hostile `adtl` list cannot grow them past the cue count, + // and each record is a lookup, not a scan. + let wanted: HashSet = points.iter().map(|(id, _)| *id).collect(); + let mut names: HashMap = HashMap::new(); + let mut lengths: HashMap = HashMap::new(); + let mut name_bytes = 0usize; + let mut records = 0usize; + 'lists: for (_, list) in chunks.iter().filter(|(id, b)| id == b"LIST" && b.get(0..4) == Some(b"adtl")) { + let mut at = 4usize; + while let Some(head) = list.get(at..at.saturating_add(8)) { + let (Ok(id), Some(size)) = (<[u8; 4]>::try_from(&head[..4]), u32_at(head, 4)) else { break }; + let start = at.saturating_add(8); + let Some(end) = start.checked_add(size as usize) else { break }; + let Some(body) = list.get(start..end) else { break }; + records += 1; + if records > MAX_ADTL_RECORDS { + break 'lists; + } + if let Some(cue_id) = u32_at(body, 0).filter(|c| wanted.contains(c)) { + match &id { + // A `labl` is the name; a `note` only when there is none. + b"labl" | b"note" if id == *b"labl" || !names.contains_key(&cue_id) => { + let without_old = name_bytes.saturating_sub(names.get(&cue_id).map_or(0, String::len)); + let text = label(body.get(4..).unwrap_or_default()) + .filter(|text| text.len() <= MAX_LABEL_TOTAL_BYTES.saturating_sub(without_old)) + .unwrap_or_else(|| { + *limited = true; + String::new() + }); + name_bytes = without_old + text.len(); + names.insert(cue_id, text); + } + // The first `ltxt` of a cue point gives its length. + b"ltxt" => { + lengths.entry(cue_id).or_insert_with(|| u32_at(body, 4).unwrap_or(0)); + } + _ => {} + } + } + at = end.saturating_add(end & 1); + } + } + let mut emitted_bytes = 0usize; + points + .into_iter() + .map(|(id, offset)| { + let comment = match names.get(&id) { + Some(text) if text.len() <= MAX_LABEL_TOTAL_BYTES.saturating_sub(emitted_bytes) => { + emitted_bytes += text.len(); + text.clone() + } + Some(_) => { + *limited = true; + String::new() + } + None => String::new(), + }; + let length = lengths.get(&id).copied().unwrap_or(0); + Marker { start: f64::from(offset), duration: f64::from(length), rate, name: comment } + }) + .collect() +} + +/// The text of `name` in an XMP entry, as an attribute (`name="v"`) or an element +/// (`v`). +fn xmp_value(entry: &str, name: &str) -> Option { + // Each entry starts just after ` in_tag = true, + b'>' => in_tag = false, + b'\'' | b'"' if in_tag => { + let quote = bytes[at]; + at += 1; + while at < bytes.len() && bytes[at] != quote { + at += 1; + } + } + _ if in_tag && (at == 0 || xml_space(bytes[at - 1])) && bytes[at..].starts_with(name.as_bytes()) => { + let mut value = at + name.len(); + while value < bytes.len() && xml_space(bytes[value]) { + value += 1; + } + if bytes.get(value) == Some(&b'=') { + value += 1; + while value < bytes.len() && xml_space(bytes[value]) { + value += 1; + } + if let Some("e @ (b'\'' | b'"')) = bytes.get(value) { + let start = value + 1; + let end = bytes.get(start..)?.iter().position(|&b| b == quote)? + start; + return Some(unescape(entry.get(start..end)?)); + } + } + } + _ => {} + } + at += 1; + } + let open = format!("<{name}>"); + let i = entry.find(&open)?; + let rest = entry.get(i + open.len()..)?; + Some(unescape(rest.get(..rest.find('<')?)?)) +} + +fn xml_space(b: u8) -> bool { + matches!(b, b' ' | b'\t' | b'\r' | b'\n') +} + +fn unescape(s: &str) -> String { + let mut out = String::with_capacity(s.len()); + let mut rest = s; + while let Some(i) = rest.find('&') { + out.push_str(rest.get(..i).unwrap_or_default()); + let tail = rest.get(i..).unwrap_or_default(); + let Some(semi) = tail.as_bytes().iter().take(11).position(|b| *b == b';') else { + out.push('&'); + rest = tail.get(1..).unwrap_or_default(); + continue; + }; + let ent = tail.get(1..semi).unwrap_or_default(); + match ent { + "amp" => out.push('&'), + "lt" => out.push('<'), + "gt" => out.push('>'), + "quot" => out.push('"'), + "apos" => out.push('\''), + _ => { + let code = ent + .strip_prefix("#x") + .and_then(|h| u32::from_str_radix(h, 16).ok()) + .or_else(|| ent.strip_prefix('#').and_then(|d| d.parse().ok())); + match code.and_then(char::from_u32) { + Some(c) => out.push(c), + None => out.push_str(tail.get(..=semi).unwrap_or_default()), + } + } + } + rest = tail.get(semi + 1..).unwrap_or_default(); + } + out.push_str(rest); + out.trim().to_string() +} + +/// A frame rate as XMP writes it: `f44100`, or `f30000s1001` (rate over scale). +fn xmp_rate(text: &str) -> Option { + let t = text.trim().strip_prefix('f')?; + let (num, den) = t.split_once('s').unwrap_or((t, "1")); + let (n, d): (f64, f64) = (num.parse().ok()?, den.parse().ok()?); + (n.is_finite() && d.is_finite() && n > 0.0 && d > 0.0 && (n / d).is_finite() && n / d > 0.0).then_some(n / d) +} + +/// Adobe XMP FrameCount: a count optionally followed by `fRATE[sSCALE]`. +/// Bare counts retain the caller's legacy track/file-rate interpretation. +fn xmp_count(text: &str, default_rate: f64) -> Option<(f64, f64)> { + let text = text.trim(); + let (count, rate) = match text.find('f') { + Some(at) => (text.get(..at)?, xmp_rate(text.get(at..)?)?), + None => (text, default_rate), + }; + let count: f64 = count.parse().ok()?; + (count.is_finite() && rate.is_finite() && rate > 0.0).then_some((count, rate)) +} + +/// The markers of an XMP packet's `xmpDM:markers` entries. +fn xmp_markers(packet: &[u8], file_rate: f64) -> Vec { + let text = String::from_utf8_lossy(packet.get(..packet.len().min(MAX_XMP)).unwrap_or_default()); + let mut out = Vec::new(); + // The frame rate belongs to the track an entry follows. + let mut rate = file_rate; + for entry in text.split("= MAX_MARKERS { + break; + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + struct TimedMarker { + time: f64, + duration: f64, + comment: String, + } + fn read(bytes: &[u8]) -> Vec { + let metadata = super::read(bytes); + let time = |frames: f64, rate: f64| if rate > 0.0 && frames.is_finite() { (frames / rate).max(0.0) } else { 0.0 }; + let map = |m: Marker| TimedMarker { time: time(m.start, m.rate), duration: time(m.duration, m.rate), comment: m.name }; + let mut out: Vec<_> = metadata.cue.into_iter().map(map).collect(); + let mut seen: HashSet<_> = out.iter().map(|m| (m.comment.clone(), m.time.to_bits())).collect(); + for m in metadata.xmp.into_iter().map(map) { + if seen.insert((m.comment.clone(), m.time.to_bits())) { + out.push(m); + } + } + out.truncate(MAX_MARKERS); + out.sort_by(|a, b| a.time.total_cmp(&b.time)); + out + } + + fn chunk(id: &[u8; 4], body: &[u8]) -> Vec { + let mut v = id.to_vec(); + v.extend((body.len() as u32).to_le_bytes()); + v.extend(body); + if body.len() % 2 == 1 { + v.push(0); + } + v + } + + fn fmt(rate: u32) -> Vec { + let mut b = Vec::new(); + b.extend(1u16.to_le_bytes()); // PCM + b.extend(1u16.to_le_bytes()); // mono + b.extend(rate.to_le_bytes()); + b.extend((rate * 2).to_le_bytes()); + b.extend(2u16.to_le_bytes()); + b.extend(16u16.to_le_bytes()); + chunk(b"fmt ", &b) + } + + fn wav(chunks: &[Vec]) -> Vec { + let body: Vec = chunks.iter().flatten().copied().collect(); + let mut v = b"RIFF".to_vec(); + v.extend((body.len() as u32 + 4).to_le_bytes()); + v.extend(b"WAVE"); + v.extend(body); + v + } + + fn cue(points: &[(u32, u32)]) -> Vec { + let mut b = (points.len() as u32).to_le_bytes().to_vec(); + for (id, offset) in points { + b.extend(id.to_le_bytes()); + b.extend(0u32.to_le_bytes()); + b.extend(b"data"); + b.extend(0u32.to_le_bytes()); + b.extend(0u32.to_le_bytes()); + b.extend(offset.to_le_bytes()); + } + chunk(b"cue ", &b) + } + + fn labl(id: &[u8; 4], cue_id: u32, text: &str) -> Vec { + let mut b = cue_id.to_le_bytes().to_vec(); + b.extend(text.as_bytes()); + b.push(0); + chunk(id, &b) + } + + fn adtl(subs: &[Vec]) -> Vec { + let mut b = b"adtl".to_vec(); + b.extend(subs.iter().flatten()); + chunk(b"LIST", &b) + } + + #[test] + fn cue_points_are_named_by_the_adtl_list_and_timed_in_samples() { + let file = wav(&[ + fmt(44100), + chunk(b"data", &[0; 8]), + cue(&[(2, 88200), (1, 22050)]), + adtl(&[labl(b"labl", 1, "SEQ01_Prologue"), labl(b"labl", 2, "SEQ02_Vallee_P1_Agriculture")]), + ]); + let m = read(&file); + assert_eq!(m.len(), 2); + assert_eq!((m[0].comment.as_str(), m[0].time), ("SEQ01_Prologue", 0.5)); + assert_eq!((m[1].comment.as_str(), m[1].time), ("SEQ02_Vallee_P1_Agriculture", 2.0)); + } + + #[test] + fn a_note_names_a_cue_only_when_it_has_no_label_and_ltxt_gives_a_length() { + let mut ltxt = 1u32.to_le_bytes().to_vec(); + ltxt.extend(44100u32.to_le_bytes()); + ltxt.extend([0; 16]); + let file = wav(&[ + fmt(44100), + cue(&[(1, 0), (2, 44100)]), + adtl(&[labl(b"note", 1, "from note"), chunk(b"ltxt", <xt), labl(b"labl", 2, "named"), labl(b"note", 2, "ignored")]), + ]); + let m = read(&file); + assert_eq!((m[0].comment.as_str(), m[0].duration), ("from note", 1.0)); + assert_eq!(m[1].comment, "named"); + } + + const XMP_ELEMENTS: &str = r#"MarkersCuef44100441000One & Two13230022050Three"#; + + #[test] + fn xmp_markers_are_read_as_elements_in_frames_of_the_track_rate() { + let file = wav(&[fmt(48000), chunk(b"_PMX", XMP_ELEMENTS.as_bytes())]); + let m = read(&file); + assert_eq!(m.len(), 2); + // 44100 frames at f44100 is one second, whatever the file's own rate. + assert_eq!((m[0].comment.as_str(), m[0].time), ("One & Two", 1.0)); + assert_eq!((m[1].comment.as_str(), m[1].time, m[1].duration), ("Three", 3.0, 0.5)); + } + + #[test] + fn xmp_markers_are_read_as_attributes_and_use_the_file_rate_without_a_track_rate() { + let xmp = r#""#; + let m = read(&wav(&[fmt(48000), chunk(b"_PMX", xmp.as_bytes())])); + assert_eq!((m[0].comment.as_str(), m[0].time), ("Attr é", 0.5)); + } + + #[test] + fn a_marker_in_both_encodings_is_listed_once() { + let xmp = r#""#; + let file = wav(&[fmt(44100), cue(&[(1, 44100)]), adtl(&[labl(b"labl", 1, "Same")]), chunk(b"_PMX", xmp.as_bytes())]); + let names: Vec = read(&file).into_iter().map(|m| m.comment).collect(); + assert_eq!(names, ["Same", "Only XMP"]); + } + + #[test] + fn damaged_or_hostile_files_read_as_far_as_they_go_without_panicking() { + assert!(read(b"").is_empty()); + assert!(read(b"RIFF").is_empty()); + assert!(read(b"not a wav file at all").is_empty()); + let good = wav(&[fmt(44100), cue(&[(1, 44100)]), adtl(&[labl(b"labl", 1, "Kept")])]); + // Every truncation is safe. + for n in 0..good.len() { + let _ = read(&good[..n]); + } + // A cue count far beyond the chunk, and chunk sizes past the end of the file. + let mut huge = 0xFFFF_FFFFu32.to_le_bytes().to_vec(); + huge.extend([0; 24]); + let m = read(&wav(&[fmt(44100), chunk(b"cue ", &huge)])); + assert!(m.len() <= 1); + let mut lying = good.clone(); + lying.extend(b"LIST\xff\xff\xff\xffadtl"); + assert_eq!(read(&lying).len(), 1); + // No sample rate: times are zero, not a division by zero. + let m = read(&wav(&[cue(&[(1, 44100)])])); + assert_eq!(m[0].time, 0.0); + } + + /// A chunk whose size field says `field` (an RF64 file's `0xFFFFFFFF`) while its body is `body`. + fn chunk_with_size_field(id: &[u8; 4], field: u32, body: &[u8]) -> Vec { + let mut v = id.to_vec(); + v.extend(field.to_le_bytes()); + v.extend(body); + if body.len() % 2 == 1 { + v.push(0); + } + v + } + + /// An RF64 `ds64` chunk: the `data` chunk's size and a table of other chunks' sizes. + fn ds64_chunk(data_size: u64, table: &[([u8; 4], u64)]) -> Vec { + let mut b = 0u64.to_le_bytes().to_vec(); // riff size (not used) + b.extend(data_size.to_le_bytes()); + b.extend(0u64.to_le_bytes()); // sample count + b.extend((table.len() as u32).to_le_bytes()); + for (id, size) in table { + b.extend(id); + b.extend(size.to_le_bytes()); + } + chunk(b"ds64", &b) + } + + fn rf64(chunks: &[Vec]) -> Vec { + let body: Vec = chunks.iter().flatten().copied().collect(); + let mut v = b"RF64".to_vec(); + v.extend(u32::MAX.to_le_bytes()); + v.extend(b"WAVE"); + v.extend(body); + v + } + + #[test] + fn rf64_markers_after_a_data_chunk_of_sentinel_size_are_read() { + let audio = [0u8; 8]; + let file = rf64(&[ + ds64_chunk(audio.len() as u64, &[]), + fmt(44100), + chunk_with_size_field(b"data", u32::MAX, &audio), + cue(&[(1, 44100)]), + adtl(&[labl(b"labl", 1, "After the audio")]), + ]); + let m = read(&file); + assert_eq!(m.len(), 1); + assert_eq!((m[0].comment.as_str(), m[0].time), ("After the audio", 1.0)); + } + + #[test] + fn rf64_sizes_of_other_chunks_come_from_the_ds64_table() { + let junk = [7u8; 6]; + let tail = || [cue(&[(1, 8000)]), adtl(&[labl(b"labl", 1, "Found")])].concat(); + let with_entry = rf64(&[ds64_chunk(0, &[(*b"junk", junk.len() as u64)]), fmt(8000), chunk_with_size_field(b"junk", u32::MAX, &junk), tail()]); + assert_eq!(read(&with_entry)[0].comment, "Found"); + // Without the table entry the sentinel is all there is, and what follows is not reached. + let without = rf64(&[ds64_chunk(0, &[]), fmt(8000), chunk_with_size_field(b"junk", u32::MAX, &junk), tail()]); + assert!(read(&without).is_empty()); + } + + #[test] + fn hostile_rf64_size_tables_read_as_far_as_they_go_without_panicking() { + // sizes that overflow when added to an offset + for data in [u64::MAX, u64::MAX - 7, 1 << 63] { + let file = + rf64(&[ds64_chunk(data, &[(*b"junk", u64::MAX)]), fmt(8000), chunk_with_size_field(b"data", u32::MAX, &[0; 4]), cue(&[(1, 0)])]); + assert!(read(&file).is_empty()); + } + // a table that claims far more entries than its chunk holds + let mut liar = ds64_chunk(0, &[]); + liar[8 + 24..8 + 28].copy_from_slice(&u32::MAX.to_le_bytes()); + let _ = read(&rf64(&[liar, fmt(8000), cue(&[(1, 0)])])); + // every truncation of a good RF64 file is safe + let good = rf64(&[ + ds64_chunk(4, &[]), + fmt(44100), + chunk_with_size_field(b"data", u32::MAX, &[0; 4]), + cue(&[(1, 44100)]), + adtl(&[labl(b"labl", 1, "Kept")]), + ]); + assert_eq!(read(&good).len(), 1); + for n in 0..good.len() { + let _ = read(&good[..n]); + } + } + + #[test] + fn a_huge_adtl_list_is_read_in_linear_time_and_only_for_the_cue_points_that_exist() { + // Records for ids that have no cue point are skipped, not stored, and the number looked at is capped. + let junk = |n: u32| (1000..1000 + n).map(|id| labl(b"labl", id, "n")).collect::>(); + let started = std::time::Instant::now(); + // the real label comes first: read + let mut subs = vec![labl(b"labl", 1, "Real")]; + subs.extend(junk(100_000)); + let m = read(&wav(&[fmt(44100), cue(&[(1, 44100)]), adtl(&subs)])); + assert_eq!(m[0].comment, "Real"); + // it comes after more records than are looked at: not read, and the marker is still there + let mut subs = junk(MAX_ADTL_RECORDS as u32 + 10); + subs.push(labl(b"labl", 1, "Too late")); + let m = read(&wav(&[fmt(44100), cue(&[(1, 44100)]), adtl(&subs)])); + assert_eq!((m.len(), m[0].comment.as_str()), (1, "")); + assert!(started.elapsed() < std::time::Duration::from_secs(5), "took {:?}", started.elapsed()); + } + + #[test] + fn metadata_keeps_original_coordinates_and_separate_sources() { + let xmp = br#""#; + let file = wav(&[fmt(44100), cue(&[(1, 1)]), adtl(&[labl(b"labl", 1, "Same")]), chunk(b"_PMX", xmp)]); + let data = super::read(&file); + assert_eq!(data.cue[0].start, 1.0); + assert_eq!(data.cue[0].rate, 44100.0); + assert_eq!(data.xmp[0].start, 1.0); + assert_eq!(data.xmp[0].duration, 2.0); + assert_eq!(data.cue.len(), 1); + assert_eq!(data.xmp.len(), 1); + } + + #[test] + fn repeated_unterminated_entities_preserve_text_with_bounded_lookahead() { + let text = "&".repeat(100_000); + assert_eq!(unescape(&text), text); + assert_eq!(unescape("A A & &unknown;"), "A A & &unknown;"); + } + #[test] + fn repeated_cue_ids_cannot_multiply_label_storage() { + let text = "x".repeat(MAX_LABEL_BYTES); + let points: Vec<_> = (0..MAX_MARKERS).map(|i| (1, i as u32)).collect(); + let file = wav(&[fmt(44100), cue(&points), adtl(&[labl(b"labl", 1, &text)])]); + let metadata = super::read(&file); + assert_eq!(metadata.cue.len(), MAX_MARKERS); + assert_eq!(metadata.cue.iter().map(|m| m.name.len()).sum::(), MAX_LABEL_TOTAL_BYTES); + assert!(metadata.limited); + assert_eq!(metadata.cue[0].name, text); + assert!(metadata.cue[MAX_MARKERS - 1].name.is_empty()); + assert_eq!(metadata.cue[MAX_MARKERS - 1].start, (MAX_MARKERS - 1) as f64); + } + + #[test] + fn cue_name_map_and_single_label_are_bounded() { + let count = MAX_LABEL_TOTAL_BYTES / MAX_LABEL_BYTES + 2; + let text = "x".repeat(MAX_LABEL_BYTES); + let points: Vec<_> = (0..count as u32).map(|i| (i, i)).collect(); + let labels: Vec<_> = (0..count as u32).map(|i| labl(b"labl", i, &text)).collect(); + let metadata = super::read(&wav(&[fmt(44100), cue(&points), adtl(&labels)])); + assert!(metadata.limited); + assert_eq!(metadata.cue.iter().map(|m| m.name.len()).sum::(), MAX_LABEL_TOTAL_BYTES); + let oversized = "x".repeat(MAX_LABEL_BYTES + 1); + let metadata = super::read(&wav(&[fmt(44100), cue(&[(1, 3)]), adtl(&[labl(b"labl", 1, &oversized)])])); + assert!(metadata.limited); + assert_eq!(metadata.cue[0].start, 3.0); + assert!(metadata.cue[0].name.is_empty()); + } + + #[test] + fn empty_chunk_inventory_is_bounded() { + let empty = chunk(b"junk", &[]); + let many = empty.repeat(MAX_CHUNKS + 1); + let metadata = super::read(&wav(&[many, fmt(44100), cue(&[(1, 1)])])); + assert!(metadata.limited); + assert!(metadata.cue.is_empty()); + } + + #[test] + fn entity_lookahead_keeps_previous_semicolon_boundary() { + assert_eq!(unescape("A"), "A"); + assert_eq!(unescape("A"), "A"); + } + #[test] + fn xmp_quotes_spacing_and_attribute_boundaries() { + let packet = br#""#; + let markers = xmp_markers(packet, 48000.0); + assert_eq!(markers.len(), 1); + assert_eq!((markers[0].start, markers[0].duration, markers[0].rate), (24000.0, 12000.0, 48000.0)); + assert_eq!(markers[0].name, "single & quoted"); + let spoof = r#" label=" xmpDM:startTime='7'" otherxmpDM:startTime='8' xmpDM:startTime = '9'/>"#; + assert_eq!(xmp_value(spoof, "xmpDM:startTime").as_deref(), Some("9")); + assert!(xmp_value(" xmpDM:startTime='unterminated", "xmpDM:startTime").is_none()); + } + + #[test] + fn xmp_frame_counts_preserve_explicit_and_mixed_rates() { + let packet = br#""#; + let markers = xmp_markers(packet, 44100.0); + assert_eq!(markers.len(), 3); + assert_eq!((markers[0].start, markers[0].rate), (15.0, 24.0)); + assert!((markers[0].duration / markers[0].rate - 0.5005).abs() < 1e-12); + assert_eq!((markers[1].start, markers[1].duration, markers[1].rate), (48000.0, 24000.0, 48000.0)); + assert_eq!((markers[2].start, markers[2].duration, markers[2].rate), (15.0, 12.0, 24.0)); + for invalid in ["15f0", "15f24s0", "15fNaN", "15finf", "NaN", "1e999", "15f24junk"] { + assert!(xmp_count(invalid, 48000.0).is_none(), "{invalid}"); + } + } +} From da591dc6b2e5091892bab6080d638b6515f93a7c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:21:37 -0500 Subject: [PATCH 146/150] fix(raw): bound LJ92 writer tile padding before allocation --- crates/raw/src/dngwrite.rs | 60 +++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/crates/raw/src/dngwrite.rs b/crates/raw/src/dngwrite.rs index 8953f94ea..ace0c07f6 100644 --- a/crates/raw/src/dngwrite.rs +++ b/crates/raw/src/dngwrite.rs @@ -13,6 +13,7 @@ pub enum DngCompression { Uncompressed, /// Lossless JPEG tiles of `tile × tile` pixels (`tile` even). CFA tiles are encoded as two interleaved /// components of half the tile width (the common DNG layout), linear data with one component per sample. + /// Edges above 2048 (after even alignment) are rejected before padding the tile. Lj92 { tile: u32, }, @@ -35,6 +36,11 @@ const DEFLATE_LEVEL: u8 = 1; /// above the 256 default and every real tile this writer makes. const MAX_DEFLATE_TILE: u32 = 2048; +// Match the existing padded Deflate edge budget. JPEG's 16-bit dimension limit alone would permit +// a 65534²-sample allocation even for a tiny source image. At most four validated channels make +// this bound at most 32 MiB of u16 padding per tile, before encoder scratch/output. +const MAX_LJ92_TILE: usize = MAX_DEFLATE_TILE as usize; + /// `tile` aligned down to a multiple of 16 and clamped to [`MAX_DEFLATE_TILE`]. fn deflate_tile(tile: u32) -> usize { (tile.max(16) & !15).min(MAX_DEFLATE_TILE) as usize @@ -270,7 +276,7 @@ pub fn write_dng(raw: &RawImage, opts: &DngWriteOptions) -> Result> { } DngCompression::Lj92 { tile } => { let tile = (tile.max(2) & !1) as usize; - if tile > 65534 { + if tile > MAX_LJ92_TILE { return Err(RawError::Limit("tile too large")); } ifd.set(t::COMPRESSION, Value::Short(vec![compression::JPEG])); @@ -432,6 +438,58 @@ mod tests { use super::*; use crate::{BlackLevel, OpcodeLists, RawData, RawFormat, RawImage, Rect}; + fn tiny_raw() -> RawImage { + let (w, h) = (8usize, 8usize); + let full = Rect::new(0, 0, w, h); + RawImage { + format: RawFormat::Dng, + width: w, + height: h, + cpp: 1, + data: RawData::U16((0..64).map(|i| i as u16 * 129).collect()), + cfa: None, + bits: 14, + black: BlackLevel::uniform(0.0), + white: vec![16383.0], + active_area: full, + crop: full, + orientation: crate::Orientation::Normal, + color: crate::ColorData::default(), + wb_multipliers: None, + linearized: false, + pixel_aspect: 1.0, + opcodes: OpcodeLists::default(), + camera: Default::default(), + cinema: Default::default(), + lens_metadata: Default::default(), + level_sources: Default::default(), + notices: crate::RawNotices::default(), + } + } + + #[test] + fn oversized_lj92_padding_is_rejected() { + let raw = tiny_raw(); + // The near-boundary case previously completed with an oversized padded tile; keep it first + // so reverting the guard fails without trying the multi-gigabyte cases. + for tile in [2050, 65534, u32::MAX] { + let opts = DngWriteOptions { compression: DngCompression::Lj92 { tile }, ..Default::default() }; + assert!(matches!(write_dng(&raw, &opts), Err(RawError::Limit("tile too large")))); + } + } + + #[test] + fn default_lj92_tiles_preserve_samples() { + let raw = tiny_raw(); + let bytes = write_dng(&raw, &DngWriteOptions::default()).expect("normal tiles write"); + let back = crate::decode(&bytes).expect("normal tiles decode"); + assert_eq!((back.width, back.height), (raw.width, raw.height)); + match (&back.data, &raw.data) { + (RawData::U16(actual), RawData::U16(expected)) => assert_eq!(actual, expected), + _ => panic!("integer DNG changed sample type"), + } + } + /// Deflate tiles of `tile × tile` pad into a square buffer that grows with the square: a /// caller-supplied edge of `u32::MAX` would make the writer pad an 8 × 8 image into billions of /// edge-replicate iterations and a multi-gigabyte buffer. The clamp keeps the bounded shape — same From d1a431c4b51b5fa3eb950c1ec72f7e131c036f43 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:59:29 +0100 Subject: [PATCH 147/150] audio: pure-Rust Vorbis encoder (1-8 channels, quality, tags, wasm) and an Ogg page writer (#1642) * audio: pure-Rust Vorbis encoder, and an Ogg page writer in craft-media Replace the libvorbis wrapper (vorbis_rs, C, desktop only) behind `vorbis-encode` with FilmCraft's clean-room Vorbis I encoder by @JordyUSA (storytold/filmcraft#4481), and move its Ogg page writer into craft-media's `ogg` module. craft-audio: - `vorbis::encoder::{Encoder, EncoderConfig, Error}`: 1-8 channels in Vorbis order, any sample rate, quality -1 to 10 (VBR), streaming packets with granule positions. - `encode_ogg_vorbis` with `OggVorbisOptions` (quality, vendor, tags, serial) writes a whole Ogg Vorbis file with a comment header. - `encode_ogg(buf, kbps)` and `OGG_BITRATES` keep working: the bit rate picks the quality whose nominal rate it is (`quality_for_bitrate`). - `encode` with `EncodeFormat::Ogg` takes 1-8 channel WAV-order layouts and writes them in Vorbis order, and `RateControl::Quality`. - Ogg is built on wasm32 too. - Residue partitions hold whole sample frames (48, 40, 48 or 56 values for 3, 5, 6 and 7 channels): libvorbis decodes each partition from channel 0, so 32-value partitions came out scrambled for those counts. - The first audio packet gets a page of its own, so a short file's first audio page is never also its last (ffmpeg then kept the padding). craft-media: `ogg::PageWriter`, `opus_tags`, `vorbis_comment`. Part of storytold/craft-libs#778. * audio, media: address review on the Vorbis encoder and Ogg writer - Encoder: read cursor into the pending input, compacted once per call, so a large encode() call no longer moves the tail on every block. - Tests: check ffmpeg's libvorbis decoder only when ffmpeg has it. - PageWriter::finish: the end granule replaces the last page's only when a packet completes on it (RFC 7845 4.4); documented and tested. - README: Ogg Opus packet granules are the samples decoded so far. * audio: pick Ogg by extension on wasm32 too The Vorbis encoder is pure Rust now, so encode_format_for("ogg") follows the vorbis-encode feature alone, as EncodeFormat::Ogg.is_built() and ENCODE_EXTENSIONS already do. --- Cargo.lock | 34 -- crates/audio/Cargo.toml | 10 +- crates/audio/README.md | 33 +- crates/audio/src/lib.rs | 34 +- crates/audio/src/vorbis/encoder/book.rs | 287 ++++++++++ crates/audio/src/vorbis/encoder/floor.rs | 207 ++++++++ crates/audio/src/vorbis/encoder/mdct.rs | 134 +++++ crates/audio/src/vorbis/encoder/mod.rs | 494 ++++++++++++++++++ crates/audio/src/vorbis/encoder/tests.rs | 285 ++++++++++ crates/audio/src/vorbis/mod.rs | 4 + crates/audio/src/write/mod.rs | 27 +- crates/audio/src/write/vorbis.rs | 297 ++++++++--- crates/audio/tests/audio/encode_format.rs | 4 +- crates/audio/tests/codecs/file_formats.rs | 2 +- crates/audio/tests/codecs/main.rs | 2 + .../tests/codecs/vorbis_encoder_oracle.rs | 131 +++++ crates/media/README.md | 8 +- crates/media/src/ogg/mod.rs | 10 +- crates/media/src/ogg/write.rs | 206 ++++++++ crates/media/src/ogg/write_tests.rs | 200 +++++++ 20 files changed, 2259 insertions(+), 150 deletions(-) create mode 100644 crates/audio/src/vorbis/encoder/book.rs create mode 100644 crates/audio/src/vorbis/encoder/floor.rs create mode 100644 crates/audio/src/vorbis/encoder/mdct.rs create mode 100644 crates/audio/src/vorbis/encoder/mod.rs create mode 100644 crates/audio/src/vorbis/encoder/tests.rs create mode 100644 crates/audio/src/vorbis/mod.rs create mode 100644 crates/audio/tests/codecs/vorbis_encoder_oracle.rs create mode 100644 crates/media/src/ogg/write.rs create mode 100644 crates/media/src/ogg/write_tests.rs diff --git a/Cargo.lock b/Cargo.lock index b3325f790..65f4523e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -222,16 +222,6 @@ version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" -[[package]] -name = "aotuv_lancer_vorbis_sys" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bc4fd1a61860d2f1198b60bedd30910eaffa978f1ee6214dfb24ac70d589225" -dependencies = [ - "cc", - "ogg_next_sys", -] - [[package]] name = "arbitrary" version = "1.5.0" @@ -1414,7 +1404,6 @@ dependencies = [ "serde_json", "symphonia", "thiserror 2.0.21", - "vorbis_rs", ] [[package]] @@ -6812,15 +6801,6 @@ dependencies = [ "phf 0.11.3", ] -[[package]] -name = "ogg_next_sys" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed2d7a48e247c2bb07e633aefb65a38648ea58c7eedd4e4408a5861721ab049b" -dependencies = [ - "cc", -] - [[package]] name = "oid-registry" version = "0.8.1" @@ -10639,20 +10619,6 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" -[[package]] -name = "vorbis_rs" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49c5da94d280f7a27e8c937e9b73df2da3e23a2583f48471fd8fb4c72f9c1933" -dependencies = [ - "aotuv_lancer_vorbis_sys", - "errno", - "getrandom 0.4.3", - "ogg_next_sys", - "thiserror 2.0.21", - "tinyvec", -] - [[package]] name = "wait-timeout" version = "0.2.1" diff --git a/crates/audio/Cargo.toml b/crates/audio/Cargo.toml index 645246130..859ac20bc 100644 --- a/crates/audio/Cargo.toml +++ b/crates/audio/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "craft-audio" -description = "Audio file I/O and codecs: WAV/AIFF read and write, FLAC encoder, AAC and ALAC decoders and encoders, AC-3/E-AC-3 decoder and encoder, MP3 decoder and encoder, Opus decoder and encoder, MP1/MP2 decoders." +description = "Audio file I/O and codecs: WAV/AIFF read and write, FLAC encoder, AAC and ALAC decoders and encoders, AC-3/E-AC-3 decoder and encoder, MP3 decoder and encoder, Opus decoder and encoder, Vorbis encoder, MP1/MP2 decoders." version.workspace = true edition.workspace = true license.workspace = true @@ -31,8 +31,9 @@ lossless-encode = ["pcm-encode", "flac-encode"] pcm-encode = [] # Encode FLAC files. flac-encode = ["craft-media/bitstream"] -# Encode Ogg Vorbis files (desktop only: builds the reference libvorbis from its C sources). -vorbis-encode = ["dep:vorbis_rs"] +# The pure-Rust Vorbis encoder (`vorbis::encoder`) and Ogg Vorbis files through `encode` and +# `encode_ogg_vorbis`. +vorbis-encode = ["craft-media/ogg"] # Codec modules on their own, for packet-level use. aac-decode = ["craft-media/bitstream"] # The AAC encoder (`aac::Encoder`), and AAC in M4A through `encode` and `FileEncoder`. @@ -72,9 +73,6 @@ rayon = { workspace = true, optional = true } # symphonia keeps its AAC decoder for AAC in CAF, a container craft-media does not read. symphonia = { version = "0.6.1", optional = true, default-features = false, features = ["mp3", "flac", "vorbis", "ogg", "wav", "pcm", "aiff", "isomp4", "aac", "alac", "adpcm", "caf", "mkv"] } -[target.'cfg(not(target_arch = "wasm32"))'.dependencies] -vorbis_rs = { version = "0.5", optional = true } - [dev-dependencies] craft-media-testkit = { workspace = true } proptest = { workspace = true } diff --git a/crates/audio/README.md b/crates/audio/README.md index 63251db8d..a5af4b3f3 100644 --- a/crates/audio/README.md +++ b/crates/audio/README.md @@ -29,7 +29,7 @@ No default features. The shared types (`AudioBuffer`, `AudioInfo`, `ChannelLayou | `decode` | `decode`, `probe`, `Decoder`, `tracks`, `TrackDecoder`; every format in [Format support](#format-support) | `decode-native`, `symphonia` | | `decode-native` | the same, native formats only (others are `Unsupported`); `wav_pcm`, `aiff_pcm` (a file's integer PCM bit for bit, as `RawPcm`) | the six decoders below, craft-media `isobmff`/`matroska`/`ogg`, `log` | | `symphonia` | the fallback for formats with no native decoder; turning it on is the same as `decode` | `decode-native`, symphonia | -| `encode` | every encoder, and every format of `encode` and `FileEncoder` | `lossless-encode`, `aac-encode`, `alac-encode`, `ac3-encode`, `eac3-encode`, `mp3-encode`, `opus-encode` | +| `encode` | every encoder, and every format of `encode` and `FileEncoder` | `lossless-encode`, `aac-encode`, `alac-encode`, `ac3-encode`, `eac3-encode`, `mp3-encode`, `opus-encode`, `vorbis-encode` | | `lossless-encode` | WAV, AIFF and FLAC writing | `pcm-encode`, `flac-encode` | | `pcm-encode` | `encode`, `FileEncoder` for WAV and AIFF; `wav_float_header`; `pcm_wav_file`, `pcm_wav_header`, `pcm_aiff_file`, `pcm_aiff_header` (minimal files around integer PCM bytes, for essence embedded in AAF / OMF) | none | | `flac-encode` | `encode`, `FileEncoder` for FLAC; `flac::Encoder`, the packet encoder for callers that mux FLAC themselves | craft-media `bitstream` | @@ -37,6 +37,7 @@ No default features. The shared types (`AudioBuffer`, `AudioInfo`, `ChannelLayou | `alac-decode` | `alac::Decoder` | none | | `alac-encode` | `alac::Encoder`; `encode`, `FileEncoder` for ALAC in M4A | craft-media `isobmff` | | `opus-decode`, `opus-encode` | `opus::Decoder`/`StreamDecoder`, `opus::Encoder` | none | +| `vorbis-encode` | `vorbis::encoder::Encoder` (pure Rust); `encode_ogg_vorbis`, `encode_ogg` and `encode` for Ogg Vorbis files (whole buffer; not `FileEncoder`) | craft-media `ogg` | | `ac3-decode` | `ac3::Decoder`, `ac3::ProgrammeDecoder` | craft-media `bitstream` | | `ac3-encode`, `eac3-encode` | `ac3::encoder::Encoder`; `encode`, `FileEncoder` for AC-3 and E-AC-3 streams | craft-media `bitstream` | | `mpa-decode` | `mpa::Decoder` | craft-media `bitstream` | @@ -355,8 +356,12 @@ let pre_skip = enc.pre_skip(); // samples the player drops at the start The encoder takes 48 kHz only, and craft-audio has no resampler. -craft-media has no Ogg muxer, so `.opus` files cannot be written yet; Opus goes into -Matroska/WebM or MP4: +Opus goes into Ogg, Matroska/WebM or MP4: + +- **Ogg (`.opus`):** `craft_media::ogg::PageWriter` with `head` and `opus_tags` as the two header + packets, each packet's granule position the samples decoded so far (pre-skip included, as + the input above starts with it), and `finish(Some(pre_skip + length))` to trim the end + (RFC 7845 §4). - **Matroska/WebM:** `head` is the track's `CodecPrivate`, and the pre-skip goes in `TrackSpec::codec_delay_ns` (in nanoseconds at 48 kHz). The Matroska muxer cannot write @@ -368,6 +373,27 @@ Matroska/WebM or MP4: timescale), `segment_duration` the audio's length (movie timescale), `media_rate` `0x0001_0000`. +### Encode Ogg Vorbis + +```rust +use craft_audio::{OggVorbisOptions, encode_ogg_vorbis}; + +// 1–8 channels in Vorbis order (5.1: L C R Ls Rs LFE), any sample rate. +let opts = OggVorbisOptions::new(6.0).with_tag("TITLE", "Take 3").with_tag("ARTIST", "Me"); +let ogg: Vec = encode_ogg_vorbis(&buf, &opts)?; +``` + +The encoder is pure Rust (`vorbis::encoder`, from FilmCraft #4481) and builds for wasm32. Quality +runs from -1 to 10 at a variable bit rate: about 18 kbit/s per channel at -1, 100 at 6 and 140 +at 10 on music. `encode` with `EncodeFormat::Ogg` takes a WAV-order layout and writes it in +Vorbis order; its `RateControl::Bitrate` (64–500 kbit/s) picks the quality whose nominal rate +that is (`quality_for_bitrate`), and `RateControl::Quality` 0.0–1.0 is quality -1 to 10. For +streaming, feed `vorbis::encoder::Encoder` and write its packets with `craft_media::ogg::PageWriter` +(identification, `vorbis_comment`, setup, then the audio with each packet's granule position). + +Not yet: short blocks (transients may pre-echo), channel coupling (stereo costs +about twice mono) and bit-rate management. + ### Waveform overviews ```rust @@ -704,6 +730,7 @@ the full tests with ffmpeg and the vectors required. | MP3 encoder | FilmCraft `filmcraft-mp3enc` [4bec22b](https://github.com/storytold/filmcraft/commit/4bec22bd03bd1d995c9cf72e02a284f96d3d72cd) | | ALAC | Apple's reference implementation [macosforge/alac@c38887c](https://github.com/macosforge/alac/tree/c38887c5c5e64a4b31108733bd79ca9b2496d987/codec), Copyright © 2011 Apple Inc., Apache-2.0; ported to Rust and modified (see the `alac` module docs and [NOTICE](../../NOTICE)) | | Opus encoder | EffectCraft `effectcraft-opusenc` [c618429](https://github.com/storytold/effectcraft/commit/c618429252587525a2a6d4b5bccbe4ec5261e1b9) | +| Vorbis encoder | FilmCraft `filmcraft-vorbis` by @JordyUSA ([storytold/filmcraft#4481](https://github.com/storytold/filmcraft/pull/4481)), with partitions of whole sample frames so libvorbis reads surround | Where apps had overlapping implementations, the one that measured better was kept, with the tests of both: diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index be0678303..8151d4072 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -13,6 +13,8 @@ //! demuxers and [`TrackDecoder`]; everything else (FLAC, Vorbis, CAF, ADPCM, A-law, µ-law) through //! `symphonia`, which is to be replaced by native decoders. Feature `decode-native` is the //! same without symphonia. +//! - [`encode_ogg_vorbis`] and [`vorbis::encoder`] (feature `vorbis-encode`): Ogg Vorbis files +//! and Vorbis packets from a pure-Rust encoder, 1 to 8 channels, quality -1 to 10, with tags. //! - [`TrackDecoder`] (any decoder feature): one track's packets from any demuxer to planar //! `f32` in WAV channel order. //! - [`encode`], [`FileEncoder`]: WAV/BWF/RF64 and AIFF/AIFF-C (feature `pcm-encode`, which also @@ -56,6 +58,8 @@ pub mod qoa; mod read; #[cfg(any(feature = "aac-decode", feature = "alac-decode", feature = "opus-decode", feature = "ac3-decode", feature = "mpa-decode"))] mod track; +#[cfg(feature = "vorbis-encode")] +pub mod vorbis; #[cfg(any(feature = "decode-native", feature = "pcm-encode"))] mod wav; /// Bounded WAV/RF64 cue, adtl and XMP marker metadata, without audio decoding. @@ -75,7 +79,7 @@ mod write; /// Range-backed PCM WAVE access and a 16-bit PCM writer. pub mod wav_range; #[cfg(feature = "vorbis-encode")] -pub use write::{OGG_BITRATES, encode_ogg}; +pub use write::{OGG_BITRATES, OggVorbisOptions, encode_ogg, encode_ogg_vorbis, quality_for_bitrate}; /// The FLAC packet encoder (RFC 9639), for a caller that muxes FLAC itself (into MP4, Matroska or /// its own `.flac` writer): planar integer samples in, frames out, and the STREAMINFO to patch in @@ -278,7 +282,9 @@ pub enum EncodeFormat { Ac3, /// An E-AC-3 stream: up to 7.1 at 32, 44.1 or 48 kHz. Eac3, - /// Ogg Vorbis, whole-buffer encoding only. + /// Ogg Vorbis, 1–8 channels at any sample rate, whole-buffer encoding only. Variable bit + /// rate: a [`RateControl::Bitrate`] picks the quality whose nominal rate it is + /// ([`quality_for_bitrate`]), [`RateControl::Quality`] sets it directly. Ogg, } @@ -379,7 +385,7 @@ impl EncodeFormat { EncodeFormat::Mp3 => cfg!(feature = "mp3-encode"), EncodeFormat::Ac3 => cfg!(feature = "ac3-encode"), EncodeFormat::Eac3 => cfg!(feature = "eac3-encode"), - EncodeFormat::Ogg => cfg!(all(feature = "vorbis-encode", not(target_arch = "wasm32"))), + EncodeFormat::Ogg => cfg!(feature = "vorbis-encode"), } } @@ -400,9 +406,9 @@ impl EncodeFormat { pub fn max_channels(self) -> u16 { match self { EncodeFormat::Wav | EncodeFormat::Aiff => 32_767, - EncodeFormat::Flac | EncodeFormat::Aac | EncodeFormat::Alac | EncodeFormat::Eac3 => 8, + EncodeFormat::Flac | EncodeFormat::Aac | EncodeFormat::Alac | EncodeFormat::Eac3 | EncodeFormat::Ogg => 8, EncodeFormat::Ac3 => 6, - EncodeFormat::Mp3 | EncodeFormat::Ogg => 2, + EncodeFormat::Mp3 => 2, } } @@ -416,7 +422,10 @@ impl EncodeFormat { EncodeFormat::Aac => &[&[0x4], &[0x3], &[0x7], &[0x107], &[0x607], &[0x60F], &[0x70F], &[0xFF]], // Apple's layout tags for 1–8 channels, surrounds as back speakers. EncodeFormat::Alac => &[&[0x4], &[0x3], &[0x7], &[0x107], &[0x37], &[0x3F], &[0x13F], &[0xFF]], - EncodeFormat::Mp3 | EncodeFormat::Ogg => &[&[0x4], &[0x3]], + EncodeFormat::Mp3 => &[&[0x4], &[0x3]], + // The Vorbis I §4.3.9 layouts for 1–8 channels (6.1 with a back centre and sides, 7.1 + // with sides and backs), written in Vorbis order. + EncodeFormat::Ogg => &[&[0x4], &[0x3], &[0x7], &[0x33], &[0x37], &[0x3F], &[0x70F], &[0x63F]], // The coding modes 1/0 to 3/2 with and without an LFE, by channel count. EncodeFormat::Ac3 => &[&[0x4], &[0x3, 0xC], &[0x7, 0x103, 0xB], &[0x107, 0x603, 0xF, 0x10B], &[0x607, 0x10F, 0x60B], &[0x60F]], // The same, and 7.1 with backs and sides. @@ -458,10 +467,10 @@ impl EncodeFormat { } } - /// Whether the format takes [`RateControl::Quality`]: AAC. The other lossy formats are - /// written at a constant bit rate only. + /// Whether the format takes [`RateControl::Quality`]: AAC and Ogg Vorbis. The other lossy + /// formats are written at a constant bit rate only. pub fn supports_quality(self) -> bool { - self == EncodeFormat::Aac + matches!(self, EncodeFormat::Aac | EncodeFormat::Ogg) } /// Whether the format loses audio: AAC, MP3, AC-3 and E-AC-3. These take @@ -480,11 +489,12 @@ pub enum RateControl { /// set of rates (MP3, AC-3) uses the highest one not above it; a rate outside what the /// stream can carry is moved to the nearest one inside. AAC is written at 6 kbit/s or a /// quarter of the sample rate per channel at least: from about half of that down its encoder - /// codes silence. Vorbis treats this as a VBR target (64–500 kbit/s), falling back to - /// a quality preset when that target is unsupported; it does not promise a constant bitrate. + /// codes silence. Vorbis takes 64–500 kbit/s and writes the quality whose nominal rate that is + /// ([`quality_for_bitrate`]); its rate varies with the audio. Bitrate(u32), /// A constant quality from 0.0 (smallest) to 1.0 (best), at whatever bit rate each frame - /// needs. AAC only; values outside the range are clamped, and NaN is an error. + /// needs. AAC and Vorbis (0.0 is Vorbis quality -1, 1.0 quality 10); values outside the range + /// are clamped, and NaN is an error. Quality(f32), } diff --git a/crates/audio/src/vorbis/encoder/book.rs b/crates/audio/src/vorbis/encoder/book.rs new file mode 100644 index 000000000..b392cf27c --- /dev/null +++ b/crates/audio/src/vorbis/encoder/book.rs @@ -0,0 +1,287 @@ +//! Bit packing (Vorbis I §2) and codebooks (§3): Huffman lengths, the codeword assignment the +//! decoder derives from them, VQ lattices (lookup type 1) and the header encoding. + +use super::{Error, Result}; + +/// LSB-first bit packer (Vorbis I §2.1.4): bits fill each byte from its least significant end. +#[derive(Clone, Debug, Default)] +pub struct BitWriter { + buf: Vec, + bits: u64, +} + +impl BitWriter { + pub fn new() -> BitWriter { + BitWriter::default() + } + + /// Writes the low `n` bits of `v` (`n` ≤ 32). + pub fn write(&mut self, v: u32, n: u32) { + let n = n.min(32); + for i in 0..n { + let bit = (v >> i) & 1; + let at = (self.bits / 8) as usize; + if at == self.buf.len() { + self.buf.push(0); + } + if bit != 0 + && let Some(b) = self.buf.get_mut(at) + { + *b |= 1 << (self.bits % 8); + } + self.bits += 1; + } + } + + pub fn flag(&mut self, b: bool) { + self.write(b as u32, 1); + } + + /// Writes a codeword MSB first: the decoder walks the tree from its first bit. + fn codeword(&mut self, word: u32, len: u32) { + for i in (0..len).rev() { + self.write((word >> i) & 1, 1); + } + } + + pub fn bytes(&mut self, data: &[u8]) { + for &b in data { + self.write(u32::from(b), 8); + } + } + + pub fn finish(self) -> Vec { + self.buf + } +} + +/// `ilog` of the specification (§9.2.1): the position of the highest set bit, 0 for 0. +pub fn ilog(v: u32) -> u32 { + 32 - v.leading_zeros() +} + +/// The 32-bit float format of codebook headers (§9.2.2), for values it represents exactly. +pub fn float32_pack(v: f64) -> u32 { + if v == 0.0 || !v.is_finite() { + return 0; + } + let sign = if v < 0.0 { 0x8000_0000u32 } else { 0 }; + let mut m = v.abs(); + let mut exp: i32 = 0; + // a 21-bit mantissa: scale into [2^20, 2^21) + while m >= (1 << 21) as f64 { + m /= 2.0; + exp += 1; + } + while m < (1 << 20) as f64 { + m *= 2.0; + exp -= 1; + } + let mant = (m.round() as u32).min((1 << 21) - 1); + let biased = (exp + 788).clamp(0, 1023) as u32; + sign | (biased << 21) | mant +} + +/// Huffman code lengths (1..=`max_len`) for `weights`, every entry coded. +pub fn huffman_lengths(weights: &[f64], max_len: u32) -> Vec { + let n = weights.len(); + if n <= 1 { + return vec![1; n]; + } + let total: f64 = weights.iter().map(|w| if w.is_finite() { w.max(0.0) } else { 0.0 }).sum::().max(1e-300); + let mut floor = 1e-9; + loop { + let w: Vec = weights.iter().map(|&x| (if x.is_finite() { x.max(0.0) } else { 0.0 }) / total + floor).collect(); + let lens = huffman(&w); + if lens.iter().all(|&l| l <= max_len) || floor > 1.0 { + return lens.into_iter().map(|l| l.clamp(1, max_len)).collect(); + } + floor *= 4.0; + } +} + +fn huffman(w: &[f64]) -> Vec { + // nodes: (weight, children) with leaves first + let n = w.len(); + let mut parent = vec![usize::MAX; 2 * n]; + let mut weight: Vec = w.to_vec(); + let mut alive: Vec = (0..n).collect(); + while alive.len() > 1 { + alive.sort_by(|&a, &b| weight[b].partial_cmp(&weight[a]).unwrap_or(std::cmp::Ordering::Equal).then(b.cmp(&a))); + let (Some(a), Some(b)) = (alive.pop(), alive.pop()) else { break }; + let id = weight.len(); + weight.push(weight[a] + weight[b]); + if parent.len() <= id { + parent.resize(id + 1, usize::MAX); + } + parent[a] = id; + parent[b] = id; + alive.push(id); + } + (0..n) + .map(|i| { + let mut d = 0; + let mut x = i; + while let Some(&p) = parent.get(x) { + if p == usize::MAX { + break; + } + d += 1; + x = p; + } + d + }) + .collect() +} + +/// The codewords the decoder assigns to `lengths` (§3.2.1): in entry order, each entry gets the +/// lowest available codeword of its length (no prefix of an assigned codeword, and no assigned +/// codeword a prefix of it). An error when the lengths do not fit a prefix code. +pub fn assign_codewords(lengths: &[u32]) -> Result> { + // A trie of the assigned codewords: `children[node]` (0 = none), `leaf[node]`. + let mut children: Vec<[usize; 2]> = vec![[0, 0]]; + let mut leaf = vec![false]; + let mut words = Vec::with_capacity(lengths.len()); + for &len in lengths { + if len == 0 || len > 32 { + return Err(Error::Internal("codeword length")); + } + let bits = lowest_free(&children, &leaf, 0, len).ok_or(Error::Internal("over-subscribed codebook"))?; + let mut node = 0usize; + let mut word = 0u32; + for &bit in &bits { + word = (word << 1) | bit as u32; + node = match children[node][bit] { + 0 => { + children.push([0, 0]); + leaf.push(false); + let id = children.len() - 1; + children[node][bit] = id; + id + } + c => c, + }; + } + leaf[node] = true; + words.push(word); + } + Ok(words) +} + +/// The leftmost free path of `depth` bits below `node` (depth-first, 0 before 1). +fn lowest_free(children: &[[usize; 2]], leaf: &[bool], node: usize, depth: u32) -> Option> { + if leaf[node] || depth == 0 { + return None; + } + for bit in 0..2 { + let c = children[node][bit]; + if c == 0 { + let mut p = vec![bit]; + p.resize(depth as usize, 0); + return Some(p); + } + if let Some(mut rest) = lowest_free(children, leaf, c, depth - 1) { + rest.insert(0, bit); + return Some(rest); + } + } + None +} + +/// A codebook: entry lengths and codewords, and for VQ books a lattice (lookup type 1). +#[derive(Clone, Debug)] +pub struct Codebook { + pub dimensions: u32, + pub lengths: Vec, + pub words: Vec, + /// Lattice values per dimension (lookup type 1: `values^dimensions == entries`). + pub lattice: Option, +} + +#[derive(Clone, Copy, Debug)] +pub struct Lattice { + pub minimum: f64, + pub delta: f64, + /// Values per dimension. + pub values: u32, +} + +impl Codebook { + /// A scalar book (no lookup) of `weights.len()` entries. + pub fn scalar(dimensions: u32, weights: &[f64]) -> Result { + let lengths = huffman_lengths(weights, 20); + let words = assign_codewords(&lengths)?; + Ok(Codebook { dimensions, lengths, words, lattice: None }) + } + + /// A lattice VQ book: `values` integers per dimension from `minimum` in steps of `delta`; + /// `weight(v)` is the relative probability of one component `v`. + pub fn lattice(dimensions: u32, values: u32, minimum: i32, delta: i32, weight: impl Fn(i32) -> f64) -> Result { + let entries = values.checked_pow(dimensions).ok_or(Error::Internal("codebook size"))?; + let w: Vec = (0..entries) + .map(|e| { + let mut p = 1.0; + let mut rest = e; + for _ in 0..dimensions { + let t = (rest % values) as i32; + rest /= values; + p *= weight(minimum + t * delta); + } + p + }) + .collect(); + let lengths = huffman_lengths(&w, 20); + let words = assign_codewords(&lengths)?; + Ok(Codebook { dimensions, lengths, words, lattice: Some(Lattice { minimum: f64::from(minimum), delta: f64::from(delta), values }) }) + } + + pub fn entries(&self) -> usize { + self.lengths.len() + } + + /// Writes entry `e`. + pub fn put(&self, w: &mut BitWriter, e: usize) { + if let (Some(&word), Some(&len)) = (self.words.get(e), self.lengths.get(e)) { + w.codeword(word, len); + } + } + + /// The entry of the lattice vector `v` (component 0 is the least significant digit, §3.2.1). + pub fn entry_of(&self, v: &[i32]) -> usize { + let Some(l) = self.lattice else { return 0 }; + let mut e = 0usize; + let mut scale = 1usize; + for &x in v.iter().take(self.dimensions as usize) { + let t = ((f64::from(x) - l.minimum) / l.delta).round().clamp(0.0, f64::from(l.values - 1)) as usize; + e += t * scale; + scale *= l.values as usize; + } + e + } + + /// The header encoding (§3.2.1). + pub fn write_header(&self, w: &mut BitWriter) { + w.write(0x56_4342, 24); + w.write(self.dimensions, 16); + w.write(self.entries() as u32, 24); + w.flag(false); // not ordered + w.flag(false); // not sparse + for &l in &self.lengths { + w.write(l - 1, 5); + } + match self.lattice { + None => w.write(0, 4), + Some(l) => { + w.write(1, 4); + w.write(float32_pack(l.minimum), 32); + w.write(float32_pack(l.delta), 32); + let bits = ilog(l.values - 1).max(1); + w.write(bits - 1, 4); + w.flag(false); // sequence_p + for m in 0..l.values { + w.write(m, bits); + } + } + } + } +} diff --git a/crates/audio/src/vorbis/encoder/floor.rs b/crates/audio/src/vorbis/encoder/floor.rs new file mode 100644 index 000000000..503385bd3 --- /dev/null +++ b/crates/audio/src/vorbis/encoder/floor.rs @@ -0,0 +1,207 @@ +//! Floor type 1 (Vorbis I §7): a piecewise-linear spectral envelope in the decoder's dB scale. +//! +//! The encoder picks a target value at each of the floor's X positions, codes it against the +//! decoder's prediction from its neighbours (step 2 of §7.2.4, inverted), and renders the curve +//! exactly as the decoder will (`render_line`, integer arithmetic), so the residue is computed +//! against the very floor the decoder multiplies it by. + +use super::book::{BitWriter, Codebook, ilog}; + +/// `floor1_multiplier`: 2, so values run 0..128 and step two entries of the dB table. +pub const MULTIPLIER: u32 = 2; +/// The value range for [`MULTIPLIER`] 2. +pub const RANGE: i32 = 128; +const RANGE_BITS: u32 = 10; +/// Points per partition, and partitions. +pub const CLASS_DIM: usize = 4; +pub const PARTITIONS: usize = 8; + +/// Step (in dB) between entries of `floor1_inverse_dB_table`: it runs from 1.0649863e-07 to 1 +/// over 256 entries. +fn db_step() -> f64 { + (1.0f64 / 1.064_986_3e-7).ln() / 255.0 +} + +/// `floor1_inverse_dB_table[i]`. +pub fn inverse_db(i: i32) -> f32 { + (f64::from(i.clamp(0, 255) - 255) * db_step()).exp() as f32 +} + +/// The table index whose amplitude is nearest (in dB) to `a`. +pub fn db_index(a: f32) -> i32 { + if !a.is_finite() || a <= 0.0 { + return 0; + } + ((f64::from(a)).ln() / db_step() + 255.0).round().clamp(0.0, 255.0) as i32 +} + +/// A floor 1 configuration for blocks of `n/2 = 2^RANGE_BITS` bins. +#[derive(Clone, Debug)] +pub struct Floor1 { + /// X positions: 0, 1024, then the partitions' points. + pub xs: Vec, + /// Index of the book coding the point values. + pub book: usize, + /// For each point (in coding order) its low and high neighbours. + neighbours: Vec<(usize, usize)>, + /// Point indices sorted by X. + sorted: Vec, +} + +impl Floor1 { + /// Points spaced about evenly in log frequency over 1..1024. + pub fn new(book: usize) -> Floor1 { + let count = CLASS_DIM * PARTITIONS; + let mut xs = vec![0, 1 << RANGE_BITS]; + let mut last = 0; + for i in 1..=count { + // geometric from 2 to ~1000 bins, at least one bin apart + let x = (2.0f64 * (500.0f64).powf((i - 1) as f64 / (count - 1) as f64)).round() as i32; + let x = x.max(last + 1).min((1 << RANGE_BITS) - 1); + xs.push(x); + last = x; + } + let neighbours = (0..xs.len()) + .map(|i| { + if i < 2 { + return (0, 0); + } + let (mut lo, mut hi) = (0usize, 1usize); + for j in 0..i { + if xs[j] < xs[i] && xs[j] > xs[lo] { + lo = j; + } + if xs[j] > xs[i] && xs[j] < xs[hi] { + hi = j; + } + } + (lo, hi) + }) + .collect(); + let mut sorted: Vec = (0..xs.len()).collect(); + sorted.sort_by_key(|&i| xs[i]); + Floor1 { xs, book, neighbours, sorted } + } + + /// The setup-header encoding (§7.2.2): one class of `CLASS_DIM` points, no subclasses. + pub fn write_header(&self, w: &mut BitWriter) { + w.write(PARTITIONS as u32, 5); + for _ in 0..PARTITIONS { + w.write(0, 4); + } + w.write(CLASS_DIM as u32 - 1, 3); + w.write(0, 2); // no subclasses + w.write(self.book as u32 + 1, 8); + w.write(MULTIPLIER - 1, 2); + w.write(RANGE_BITS, 4); + for &x in &self.xs[2..] { + w.write(x as u32, RANGE_BITS); + } + } + + /// Codes the target values `want` (one per point, 0..RANGE) into `w`; returns the floor curve + /// the decoder renders (`n2` amplitudes). + pub fn encode(&self, w: &mut BitWriter, book: &Codebook, want: &[i32], n2: usize) -> Vec { + let n = self.xs.len(); + let want = |i: usize| want.get(i).copied().unwrap_or(0).clamp(0, RANGE - 1); + let mut fin = vec![0i32; n]; + let mut used = vec![false; n]; + let mut coded = vec![0u32; n]; + fin[0] = want(0); + fin[1] = want(1); + used[0] = true; + used[1] = true; + for i in 2..n { + let (lo, hi) = self.neighbours[i]; + let pred = render_point(self.xs[lo], fin[lo], self.xs[hi], fin[hi], self.xs[i]); + let f = want(i); + let (highroom, lowroom) = (RANGE - pred, pred); + let room = 2 * highroom.min(lowroom); + let val = if f == pred { + 0 + } else { + let d = f - pred; + let inside = if d > 0 { 2 * d } else { -2 * d - 1 }; + if inside < room { + inside + } else if highroom > lowroom { + f - pred + lowroom + } else { + pred - f + highroom - 1 + } + }; + coded[i] = val.clamp(0, RANGE - 1) as u32; + if val != 0 { + used[lo] = true; + used[hi] = true; + used[i] = true; + fin[i] = f; + } else { + fin[i] = pred; + } + } + let bits = ilog(RANGE as u32 - 1); + w.write(fin[0] as u32, bits); + w.write(fin[1] as u32, bits); + for &c in &coded[2..] { + book.put(w, c as usize); + } + self.render(&fin, &used, n2) + } + + /// The decoder's curve (§7.2.4 step 2, `render_line`) as amplitudes. + fn render(&self, fin: &[i32], used: &[bool], n2: usize) -> Vec { + let mut v = vec![0i32; n2]; + let m = MULTIPLIER as i32; + let (mut lx, mut ly) = (0i32, fin[self.sorted[0]] * m); + let (mut hx, mut hy) = (0i32, ly); + for &i in &self.sorted[1..] { + if used[i] { + hy = fin[i] * m; + hx = self.xs[i]; + render_line(lx, ly, hx, hy, &mut v); + lx = hx; + ly = hy; + } + } + if (hx as usize) < n2 { + render_line(hx, hy, n2 as i32, hy, &mut v); + } + v.iter().map(|&i| inverse_db(i)).collect() + } +} + +fn render_point(x0: i32, y0: i32, x1: i32, y1: i32, x: i32) -> i32 { + let dy = y1 - y0; + let adx = (x1 - x0).max(1); + let err = dy.abs() * (x - x0); + let off = err / adx; + if dy < 0 { y0 - off } else { y0 + off } +} + +fn render_line(x0: i32, y0: i32, x1: i32, y1: i32, v: &mut [i32]) { + let dy = y1 - y0; + let adx = x1 - x0; + if adx <= 0 { + return; + } + let base = dy / adx; + let sy = if dy < 0 { base - 1 } else { base + 1 }; + let ady = dy.abs() - base.abs() * adx; + let (mut y, mut err) = (y0, 0); + if let Some(s) = v.get_mut(x0 as usize) { + *s = y; + } + for x in x0 + 1..x1 { + err += ady; + if err >= adx { + err -= adx; + y += sy; + } else { + y += base; + } + if let Some(s) = v.get_mut(x as usize) { + *s = y; + } + } +} diff --git a/crates/audio/src/vorbis/encoder/mdct.rs b/crates/audio/src/vorbis/encoder/mdct.rs new file mode 100644 index 000000000..3802a24d6 --- /dev/null +++ b/crates/audio/src/vorbis/encoder/mdct.rs @@ -0,0 +1,134 @@ +//! Forward MDCT of a windowed block of `n` samples to `n/2` coefficients: +//! `X[k] = Σ x[j] cos(2Ï€/n · (j + 1/2 + n/4)(k + 1/2))`, the transform the Vorbis decoder inverts +//! (Vorbis I §1.3.2, §4.3.9). Computed as a DCT-IV of the folded block through an `n/8`-point +//! complex FFT. + +use std::f64::consts::PI; + +#[derive(Clone, Copy, Default, Debug)] +struct Cpx { + re: f64, + im: f64, +} + +impl Cpx { + fn mul(self, o: Cpx) -> Cpx { + Cpx { re: self.re * o.re - self.im * o.im, im: self.re * o.im + self.im * o.re } + } +} + +/// A forward MDCT for one block size (a power of two, at least 16). +#[derive(Clone, Debug)] +pub struct Mdct { + n: usize, + twiddle: Vec, + post: Vec, + fft_tw: Vec, + rev: Vec, +} + +impl Mdct { + pub fn new(n: usize) -> Mdct { + let m = n / 2; // DCT-IV length + let h = m / 2; // FFT length + let twiddle = (0..h).map(|j| cis(-PI * (4.0 * j as f64 + 1.0) / (4.0 * m as f64))).collect(); + let post = (0..h).map(|k| cis(-PI * k as f64 / m as f64)).collect(); + let fft_tw = (0..h / 2).map(|j| cis(-2.0 * PI * j as f64 / h as f64)).collect(); + let bits = h.trailing_zeros(); + let rev = (0..h).map(|i| if bits == 0 { 0 } else { i.reverse_bits() >> (usize::BITS - bits) }).collect(); + Mdct { n, twiddle, post, fft_tw, rev } + } + + /// `x`: `n` windowed samples; `out`: `n/2` coefficients (scaled by `scale`). + pub fn forward(&self, x: &[f32], out: &mut [f32], scale: f64) { + let n = self.n; + let m = n / 2; + let q = n / 4; + let at = |i: usize| f64::from(x.get(i).copied().unwrap_or(0.0)); + // fold (a, b, c, d) to the DCT-IV input (-c_r - d, a - b_r) + let mut v = vec![0f64; m]; + for (i, s) in v.iter_mut().enumerate() { + *s = if i < q { -at(3 * q - 1 - i) - at(3 * q + i) } else { at(i - q) - at(3 * q - 1 - i) }; + } + // DCT-IV through a half-length complex FFT + let h = m / 2; + let mut z: Vec = (0..h).map(|j| Cpx { re: v[2 * j], im: v[m - 1 - 2 * j] }.mul(self.twiddle[j])).collect(); + self.fft(&mut z); + for (k, c) in z.iter().enumerate() { + let d = c.mul(self.post[k]); + if let Some(o) = out.get_mut(2 * k) { + *o = (d.re * scale) as f32; + } + if let Some(o) = out.get_mut(m - 1 - 2 * k) { + *o = (-d.im * scale) as f32; + } + } + } + + /// In-place radix-2 FFT (`e^{-2Ï€i jk/h}`). + fn fft(&self, a: &mut [Cpx]) { + let h = a.len(); + for i in 0..h { + let j = self.rev[i]; + if i < j { + a.swap(i, j); + } + } + let mut len = 2; + while len <= h { + let step = h / len; + for start in (0..h).step_by(len) { + for k in 0..len / 2 { + let w = self.fft_tw[k * step]; + let u = a[start + k]; + let t = a[start + k + len / 2].mul(w); + a[start + k] = Cpx { re: u.re + t.re, im: u.im + t.im }; + a[start + k + len / 2] = Cpx { re: u.re - t.re, im: u.im - t.im }; + } + } + len *= 2; + } + } +} + +fn cis(t: f64) -> Cpx { + Cpx { re: t.cos(), im: t.sin() } +} + +/// The Vorbis window (§4.3.1) for a block of `n` between blocks of the same size. +pub fn window(n: usize) -> Vec { + (0..n) + .map(|i| { + let s = (PI * (i as f64 + 0.5) / n as f64).sin(); + (PI / 2.0 * s * s).sin() as f32 + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn matches_the_direct_formula() { + for n in [16usize, 64, 256, 2048] { + let x: Vec = (0..n).map(|i| ((i * 7919 % 211) as f32 - 105.0) / 50.0).collect(); + let mut fast = vec![0f32; n / 2]; + Mdct::new(n).forward(&x, &mut fast, 1.0); + for k in [0usize, 1, 2, n / 4, n / 2 - 1] { + let direct: f64 = + (0..n).map(|j| f64::from(x[j]) * (2.0 * PI / n as f64 * (j as f64 + 0.5 + n as f64 / 4.0) * (k as f64 + 0.5)).cos()).sum(); + assert!((direct - f64::from(fast[k])).abs() < 1e-3 * (1.0 + direct.abs()), "n={n} k={k}: {direct} vs {}", fast[k]); + } + } + } + + #[test] + fn the_window_is_power_complementary() { + let w = window(2048); + for i in 0..1024 { + let s = w[i] * w[i] + w[i + 1024] * w[i + 1024]; + assert!((s - 1.0).abs() < 1e-5, "{i}: {s}"); + } + } +} diff --git a/crates/audio/src/vorbis/encoder/mod.rs b/crates/audio/src/vorbis/encoder/mod.rs new file mode 100644 index 000000000..95be88244 --- /dev/null +++ b/crates/audio/src/vorbis/encoder/mod.rs @@ -0,0 +1,494 @@ +//! Pure-Rust Vorbis I encoder, written from the Vorbis I specification (Xiph.Org, 2020-07-04). +//! +//! No `unsafe` and no C code; builds for `wasm32-unknown-unknown`. ffmpeg is used only as an +//! external test oracle. +//! +//! The stream: one mode of 2048-sample blocks; per channel a floor 1 envelope of 34 points; one +//! residue type 2 over the interleaved channels, partitions of 32 values (a multiple of the +//! channel count for 3, 5, 6 and 7 channels) in seven classes (silent, +//! three single-pass lattice books, and two- and three-pass cascades for large values); no channel +//! coupling. Quality (-1 to 10) sets how far the floor sits from the spectral envelope (below it +//! for tonal regions, above it for noise-like ones, from the spectral flatness), and with it the +//! residue's precision: variable bit rate, like other Vorbis encoders. +//! +//! From FilmCraft's `filmcraft-vorbis` by @JordyUSA (storytold/filmcraft#4481). +//! +//! The packets go into Ogg with [`craft_media::ogg::PageWriter`] and a comment header from +//! [`craft_media::ogg::vorbis_comment`]; [`crate::encode_ogg_vorbis`] does all of it for a +//! whole buffer. +//! +//! ``` +//! use craft_audio::vorbis::encoder::{Encoder, EncoderConfig}; +//! +//! let mut enc = Encoder::new(EncoderConfig::new(48_000, 2, 5.0))?; +//! let tone: Vec = (0..4_800).map(|i| (i as f32 * 0.05).sin() * 0.5).collect(); +//! let mut packets = enc.encode(&[&tone, &tone])?; +//! packets.extend(enc.finish()?); +//! // the last granule position is the input's length, so a decoder trims the padding +//! assert_eq!(packets.last().map(|p| p.1), Some(4_800)); +//! assert_eq!(&enc.identification()[1..7], b"vorbis"); +//! # Ok::<(), craft_audio::vorbis::encoder::Error>(()) +//! ``` + +// Index loops follow the specification, as in FilmCraft (which allows this lint workspace-wide). +#![allow(clippy::needless_range_loop)] + +mod book; +mod floor; +mod mdct; + +use book::{BitWriter, Codebook}; +use floor::Floor1; +use mdct::Mdct; + +/// Why an encoder could not be created or input could not be encoded. +#[non_exhaustive] +#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// A setting or the input is outside what the encoder takes. + #[error("invalid argument: {0}")] + InvalidArgument(&'static str), + /// An encoder invariant failed (a bug, reported instead of panicking). + #[error("internal error: {0}")] + Internal(&'static str), +} + +/// The encoder's result type. +pub type Result = std::result::Result; + +/// Block size (samples); the short block size of the header is never used. +pub const BLOCK: usize = 2048; +const HOP: usize = BLOCK / 2; +/// Residue values per partition for `channels` channels: 32, or for 3, 5, 6 and 7 channels a +/// multiple of the channel count (48, 40, 48, 56). libvorbis decodes each partition of an +/// interleaved (type 2) residue as if it started on channel 0, so a partition must hold whole +/// sample frames; the size also divides `channels × 1024` and is a multiple of every book's +/// dimension (4). +fn partition_size(channels: usize) -> usize { + match channels { + 3 | 6 => 48, + 5 => 40, + 7 => 56, + _ => 32, + } +} +const CLASSES: usize = 8; +/// Largest residue magnitude the cascade codes: 7·225 + 7·15 + 7. +const MAX_RESIDUE: i32 = 1687; + +/// The quality a NaN or infinite setting reads as. +pub const DEFAULT_QUALITY: f32 = 6.0; + +/// Encoder settings. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct EncoderConfig { + /// Samples per second, 1 to 768 000. + pub sample_rate: u32, + /// 1 to 8, in Vorbis channel order (§4.3.9). + pub channels: usize, + /// -1 (smallest) to 10 (best); clamped. + pub quality: f32, +} + +impl EncoderConfig { + /// Settings for `channels` channels (in Vorbis order) at `sample_rate` and `quality`. + pub fn new(sample_rate: u32, channels: usize, quality: f32) -> EncoderConfig { + EncoderConfig { sample_rate, channels, quality } + } +} + +/// Book indices in the setup header. +const BOOK_CLASS: usize = 0; +const BOOK_FLOOR: usize = 1; +const BOOK_A1_SPARSE: usize = 2; +const BOOK_A1: usize = 3; +const BOOK_A2: usize = 4; +const BOOK_A4: usize = 5; +const BOOK_F: usize = 6; +const BOOK_M: usize = 7; +const BOOK_C: usize = 8; + +/// Books per class and pass (`None`: nothing coded in that pass). +const CASCADE: [[Option; 3]; CLASSES] = [ + [None, None, None], + [Some(BOOK_A1_SPARSE), None, None], + [Some(BOOK_A1), None, None], + [Some(BOOK_A2), None, None], + [Some(BOOK_A4), None, None], + [Some(BOOK_F), None, None], + [Some(BOOK_M), Some(BOOK_F), None], + [Some(BOOK_C), Some(BOOK_M), Some(BOOK_F)], +]; + +/// A Vorbis encoder for one stream: planar `f32` in, Vorbis packets with their granule +/// positions out. +pub struct Encoder { + cfg: EncoderConfig, + books: Vec, + floor: Floor1, + mdct: Mdct, + window: Vec, + /// Input not yet past a block, per channel; the next block starts at `start`. + pending: Vec>, + /// Read cursor into `pending`: consumed hops are dropped once per call, not per block. + start: usize, + samples_in: u64, + blocks: u64, + finished: bool, + /// Floor offset below the envelope (dB) for tonal and for noise-like regions. + tonal_db: f32, + noise_db: f32, + /// Residue magnitudes below this quantise to 0. + dead_zone: f32, +} + +impl Encoder { + /// An encoder for `cfg`: 1 to 8 channels, a sample rate of 1 to 768 000; the quality is + /// clamped to -1..=10, and NaN or infinite reads as [`DEFAULT_QUALITY`]. + pub fn new(cfg: EncoderConfig) -> Result { + if !(1..=8).contains(&cfg.channels) { + return Err(Error::InvalidArgument("Vorbis encoder: 1 to 8 channels")); + } + if !(1..=768_000).contains(&cfg.sample_rate) { + return Err(Error::InvalidArgument("Vorbis encoder: sample rate")); + } + let quality = if cfg.quality.is_finite() { cfg.quality.clamp(-1.0, 10.0) } else { DEFAULT_QUALITY }; + let cfg = EncoderConfig { quality, ..cfg }; + let books = vec![ + Codebook::scalar(2, &(0..CLASSES * CLASSES).map(|e| class_weight(e / CLASSES) * class_weight(e % CLASSES)).collect::>())?, + Codebook::scalar(1, &(0..floor::RANGE).map(|v| 1.0 / (1.0 + f64::from(v)).powf(1.6)).collect::>())?, + Codebook::lattice(4, 3, -1, 1, |v| if v == 0 { 0.92 } else { 0.04 })?, + Codebook::lattice(4, 3, -1, 1, |v| if v == 0 { 0.5 } else { 0.25 })?, + Codebook::lattice(2, 5, -2, 1, |v| 0.6f64.powi(v.abs()))?, + Codebook::lattice(2, 9, -4, 1, |v| 0.7f64.powi(v.abs()))?, + Codebook::lattice(2, 15, -7, 1, |v| 0.8f64.powi(v.abs()))?, + Codebook::lattice(2, 15, -105, 15, |v| 0.55f64.powi((v / 15).abs()))?, + Codebook::lattice(2, 15, -1575, 225, |v| 0.4f64.powi((v / 225).abs()))?, + ]; + Ok(Encoder { + cfg, + books, + floor: Floor1::new(BOOK_FLOOR), + mdct: Mdct::new(BLOCK), + window: mdct::window(BLOCK), + // the first block is centred on sample 0: the decoder's first output is input sample 0 + pending: vec![vec![0.0; HOP]; cfg.channels], + start: 0, + samples_in: 0, + blocks: 0, + finished: false, + // tones 4 dB (-1) to 30.4 dB (10) under their envelope; noise 14 dB above it (-1) + // to 4.1 dB (10) + tonal_db: 4.0 + 2.4 * (quality + 1.0), + noise_db: -14.0 + 0.9 * (quality + 1.0), + dead_zone: 0.8 - 0.025 * (quality + 1.0), + }) + } + + /// Channels per sample frame. + pub fn channels(&self) -> usize { + self.cfg.channels + } + + /// The quality in use, after clamping. + pub fn quality(&self) -> f32 { + self.cfg.quality + } + + /// Input samples per channel taken so far. + pub fn samples_in(&self) -> u64 { + self.samples_in + } + + /// The identification header packet (§4.2.2): the first packet of the stream. + pub fn identification(&self) -> Vec { + let mut w = BitWriter::new(); + w.write(1, 8); + w.bytes(b"vorbis"); + w.write(0, 32); + w.write(self.cfg.channels as u32, 8); + w.write(self.cfg.sample_rate, 32); + w.write(0, 32); // bit rates: unset (variable) + w.write(0, 32); + w.write(0, 32); + w.write(8, 4); // 256-sample short blocks (unused) + w.write(11, 4); // 2048-sample long blocks + w.flag(true); + w.finish() + } + + /// The setup header packet (§4.2.4): codebooks, floor, residue, mapping and mode. It follows + /// the comment header. + pub fn setup(&self) -> Vec { + let mut w = BitWriter::new(); + w.write(5, 8); + w.bytes(b"vorbis"); + w.write(self.books.len() as u32 - 1, 8); + for b in &self.books { + b.write_header(&mut w); + } + w.write(0, 6); // one time-domain transform placeholder + w.write(0, 16); + w.write(0, 6); // one floor + w.write(1, 16); + self.floor.write_header(&mut w); + w.write(0, 6); // one residue, type 2 + w.write(2, 16); + let end = (self.cfg.channels * HOP) as u32; + w.write(0, 24); + w.write(end, 24); + w.write(partition_size(self.cfg.channels) as u32 - 1, 24); + w.write(CLASSES as u32 - 1, 6); + w.write(BOOK_CLASS as u32, 8); + for c in &CASCADE { + let bits: u32 = c.iter().enumerate().map(|(j, b)| u32::from(b.is_some()) << j).sum(); + w.write(bits & 7, 3); + w.flag(bits > 7); + if bits > 7 { + w.write(bits >> 3, 5); + } + } + for c in &CASCADE { + for b in c.iter().flatten() { + w.write(*b as u32, 8); + } + } + w.write(0, 6); // one mapping, type 0 + w.write(0, 16); + w.flag(false); // one submap + w.flag(false); // no coupling + w.write(0, 2); + w.write(0, 8); // submap 0: time, floor 0, residue 0 + w.write(0, 8); + w.write(0, 8); + w.write(0, 6); // one mode: long blocks, mapping 0 + w.flag(true); + w.write(0, 16); + w.write(0, 16); + w.write(0, 8); + w.flag(true); + w.finish() + } + + /// Encodes planar samples (one slice per channel, all the same length, nominal range ±1); + /// returns the packets completed with their granule positions. NaN or infinite samples, or a + /// channel count or length mismatch, are an error. + pub fn encode(&mut self, planar: &[&[f32]]) -> Result, i64)>> { + if self.finished { + return Err(Error::InvalidArgument("Vorbis encoder: encode after finish")); + } + if planar.len() != self.cfg.channels { + return Err(Error::InvalidArgument("Vorbis encoder: one slice per channel")); + } + let len = planar.first().map_or(0, |p| p.len()); + if planar.iter().any(|p| p.len() != len) { + return Err(Error::InvalidArgument("Vorbis encoder: channels of different lengths")); + } + if planar.iter().any(|p| p.iter().any(|v| !v.is_finite())) { + return Err(Error::InvalidArgument("Vorbis encoder: NaN or infinite sample")); + } + for (b, p) in self.pending.iter_mut().zip(planar) { + b.extend_from_slice(p); + } + self.samples_in += len as u64; + self.drain() + } + + /// Encodes what remains, padded with silence; the last packet's granule position is the + /// input's length, so the decoder trims the padding (§A.2). + pub fn finish(&mut self) -> Result, i64)>> { + if self.finished { + return Ok(Vec::new()); + } + self.finished = true; + // block k ends output at k·HOP: blocks 0..=ceil(len / HOP) + let blocks = self.samples_in.div_ceil(HOP as u64) + 1; + let missing = blocks.saturating_sub(self.blocks) as usize; + self.compact(); + let need = (missing + 1) * HOP; + for b in self.pending.iter_mut() { + if b.len() < need { + b.resize(need, 0.0); + } + } + let mut out = Vec::new(); + for _ in 0..missing { + out.push(self.block()?); + } + Ok(out) + } + + fn drain(&mut self) -> Result, i64)>> { + let mut out = Vec::new(); + while self.pending.first().is_some_and(|b| b.len().saturating_sub(self.start) >= BLOCK) { + out.push(self.block()?); + } + self.compact(); + Ok(out) + } + + /// Drops the hops already encoded from the front of every channel buffer. + fn compact(&mut self) { + for b in self.pending.iter_mut() { + b.drain(..self.start.min(b.len())); + } + self.start = 0; + } + + /// Encodes the block at `start` in `pending` and advances by a hop. + fn block(&mut self) -> Result<(Vec, i64)> { + let ch = self.cfg.channels; + let mut w = BitWriter::new(); + w.write(0, 1); // audio packet; one mode, so no mode number + w.flag(true); // previous and next windows long + w.flag(true); + let mut residue = vec![0i32; ch * HOP]; + let mut any = false; + let mut x = vec![0f32; BLOCK]; + let mut coeffs = vec![0f32; HOP]; + for c in 0..ch { + let src = self.pending.get(c).and_then(|b| b.get(self.start..)).ok_or(Error::Internal("channel buffer"))?; + for (i, v) in x.iter_mut().enumerate() { + *v = src.get(i).copied().unwrap_or(0.0).clamp(-8.0, 8.0) * self.window[i]; + } + self.mdct.forward(&x, &mut coeffs, 2.0 / HOP as f64); + let peak = coeffs.iter().fold(0f32, |m, v| m.max(v.abs())); + // (a NaN peak cannot happen: samples are finite and clamped) + if peak <= 1e-7 { + w.flag(false); // floor unused: the channel is silent + continue; + } + any = true; + w.flag(true); + let want = self.floor_targets(&coeffs); + let curve = self.floor.encode(&mut w, &self.books[BOOK_FLOOR], &want, HOP); + for (k, (&v, &f)) in coeffs.iter().zip(&curve).enumerate() { + let r = if f > 0.0 { v / f } else { 0.0 }; + // a dead zone: values just past half a step are noise peaks, not worth a ±1 + let q = if r.abs() < self.dead_zone { 0.0 } else { r.round() }; + residue[k * ch + c] = if q.is_finite() { (q as i32).clamp(-MAX_RESIDUE, MAX_RESIDUE) } else { 0 }; + } + } + if any { + self.write_residue(&mut w, &residue); + } + let granule = if self.blocks == 0 { + 0 + } else { + let natural = self.blocks.saturating_mul(HOP as u64); + if self.finished { natural.min(self.samples_in) } else { natural } + }; + self.start += HOP; + self.blocks += 1; + Ok((w.finish(), i64::try_from(granule).unwrap_or(i64::MAX))) + } + + /// Floor values at the floor's points: the spectral envelope (power smoothed over a quarter of + /// the frequency, at least 8 bins, on each side) less an offset that grows with the region's tonality, from its + /// spectral flatness: noise needs a few dB of precision, tones much more. + fn floor_targets(&self, coeffs: &[f32]) -> Vec { + let n = coeffs.len(); + let mut cum = vec![0f64; n + 1]; + let mut cum_ln = vec![0f64; n + 1]; + for (i, &v) in coeffs.iter().enumerate() { + let p = f64::from(v) * f64::from(v); + cum[i + 1] = cum[i] + p; + cum_ln[i + 1] = cum_ln[i] + (p + 1e-30).ln(); + } + self.floor + .xs + .iter() + .map(|&x| { + let k = (x as usize).min(n - 1); + let half = (k / 4).max(8); + let (lo, hi) = (k.saturating_sub(half), (k + half + 1).min(n)); + let len = (hi - lo) as f64; + let am = (cum[hi] - cum[lo]) / len; + let gm = ((cum_ln[hi] - cum_ln[lo]) / len).exp(); + // spectral flatness: about -2.5 dB for noise, far below for a tone + let sfm_db = 10.0 * (gm / am.max(1e-30)).max(1e-30).log10(); + let tonality = ((-sfm_db - 2.5) / 20.0).clamp(0.0, 1.0); + let offset = tonality * f64::from(self.tonal_db) + (1.0 - tonality) * f64::from(self.noise_db); + let amp = (am.sqrt() * 10f64.powf(-offset / 20.0)).max(1e-7) as f32; + (floor::db_index(amp) + 1) / floor::MULTIPLIER as i32 + }) + .collect() + } + + /// Residue type 2 (§8.6.2 with the interleaved vector): classes per partition, then the + /// cascade's passes in the decoder's order. + fn write_residue(&self, w: &mut BitWriter, r: &[i32]) { + let partition = partition_size(self.cfg.channels); + let parts = r.len() / partition; + let classes: Vec = r.chunks(partition).map(class_of).collect(); + let per_word = self.books[BOOK_CLASS].dimensions as usize; + for pass in 0..3 { + let mut pc = 0; + while pc < parts { + if pass == 0 { + let mut e = 0usize; + for i in 0..per_word { + e = e * CLASSES + classes.get(pc + i).copied().unwrap_or(0); + } + self.books[BOOK_CLASS].put(w, e); + } + for _ in 0..per_word { + if pc >= parts { + break; + } + let class = classes[pc]; + if let Some(b) = CASCADE[class][pass] { + let book = &self.books[b]; + let dims = book.dimensions as usize; + let part = &r[pc * partition..(pc + 1) * partition]; + for chunk in part.chunks(dims) { + let v: Vec = chunk.iter().map(|&x| pass_value(x, class, pass)).collect(); + book.put(w, book.entry_of(&v)); + } + } + pc += 1; + } + } + } + } +} + +/// Relative frequency of each residue class (for the class book's code lengths). +fn class_weight(c: usize) -> f64 { + [0.3, 0.2, 0.12, 0.12, 0.1, 0.08, 0.06, 0.02].get(c).copied().unwrap_or(0.01) +} + +/// The part of residue value `v` coded in `pass` for `class`: the passes add up to `v`. +fn pass_value(v: i32, class: usize, pass: usize) -> i32 { + let split = |v: i32, step: i32| -> (i32, i32) { + let a = ((f64::from(v) / f64::from(step)).round() as i32).clamp(-7, 7); + (a * step, v - a * step) + }; + match (class, pass) { + (6, 0) => split(v, 15).0, + (6, _) => split(v, 15).1, + (7, 0) => split(v, 225).0, + (7, 1) => split(split(v, 225).1, 15).0, + (7, _) => split(split(v, 225).1, 15).1, + _ => v, + } +} + +/// The residue class of a partition: silent; ±1 with at most four nonzero values, or more; then +/// by the largest magnitude. +fn class_of(p: &[i32]) -> usize { + let m = p.iter().map(|v| v.abs()).max().unwrap_or(0); + match m { + 0 => 0, + 1 if p.iter().filter(|&&v| v != 0).count() <= 4 => 1, + 1 => 2, + 2 => 3, + 3..=4 => 4, + 5..=7 => 5, + 8..=112 => 6, + _ => 7, + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/audio/src/vorbis/encoder/tests.rs b/crates/audio/src/vorbis/encoder/tests.rs new file mode 100644 index 000000000..b1ae87463 --- /dev/null +++ b/crates/audio/src/vorbis/encoder/tests.rs @@ -0,0 +1,285 @@ +//! Unit tests, and ffmpeg (as an external oracle only) decoding the encoder's streams. + +#![allow(clippy::unwrap_used)] + +use std::path::Path; +use std::process::Command; + +use super::book::{BitWriter, assign_codewords, float32_pack}; +use super::{BLOCK, Encoder, EncoderConfig, HOP, partition_size}; + +#[test] +fn codewords_follow_the_specification_example() { + // Vorbis I §3.2.1: lengths 2 4 4 4 4 2 3 3 + let words = assign_codewords(&[2, 4, 4, 4, 4, 2, 3, 3]).unwrap(); + assert_eq!(words, [0b00, 0b0100, 0b0101, 0b0110, 0b0111, 0b10, 0b110, 0b111]); + assert!(assign_codewords(&[1, 1, 1]).is_err(), "over-subscribed"); +} + +#[test] +fn every_book_is_a_complete_prefix_code() { + let e = Encoder::new(EncoderConfig::new(48_000, 2, 5.0)).unwrap(); + for (i, b) in e.books.iter().enumerate() { + let kraft: f64 = b.lengths.iter().map(|&l| 0.5f64.powi(l as i32)).sum(); + assert!((kraft - 1.0).abs() < 1e-9, "book {i}: Kraft sum {kraft}"); + assert!(b.lengths.iter().all(|&l| (1..=20).contains(&l))); + for (a, (&wa, &la)) in b.words.iter().zip(&b.lengths).enumerate() { + for (c, (&wc, &lc)) in b.words.iter().zip(&b.lengths).enumerate() { + if a != c && la <= lc { + assert_ne!(wc >> (lc - la), wa, "book {i}: codeword {a} is a prefix of {c}"); + } + } + } + } +} + +#[test] +fn float32_pack_is_exact_for_lattice_values() { + let unpack = |x: u32| { + let mant = f64::from(x & 0x1F_FFFF); + let exp = ((x >> 21) & 0x3FF) as i32 - 788; + let v = mant * 2f64.powi(exp); + if x & 0x8000_0000 != 0 { -v } else { v } + }; + for v in [1.0, -1.0, -2.0, -4.0, -7.0, -105.0, 15.0, -1575.0, 225.0, 0.5, 3.0] { + assert_eq!(unpack(float32_pack(v)), v); + } + assert_eq!(float32_pack(0.0), 0); +} + +#[test] +fn bits_are_packed_lsb_first() { + let mut w = BitWriter::new(); + w.write(0b1, 1); + w.write(0b10, 2); + w.write(0x1F, 5); + w.write(0xABC, 12); + assert_eq!(w.finish(), [0b1111_1101, 0xBC, 0x0A]); +} + +#[test] +fn hostile_input_is_an_error_not_a_panic() { + assert!(Encoder::new(EncoderConfig::new(48_000, 0, 3.0)).is_err()); + assert!(Encoder::new(EncoderConfig::new(48_000, 9, 3.0)).is_err()); + assert!(Encoder::new(EncoderConfig::new(0, 2, 3.0)).is_err()); + assert_eq!(Encoder::new(EncoderConfig::new(48_000, 1, f32::NAN)).unwrap().cfg.quality, 6.0, "NaN quality uses the default, as the export does"); + let mut e = Encoder::new(EncoderConfig::new(44_100, 2, 3.0)).unwrap(); + assert!(e.encode(&[&[0.0; 4]]).is_err()); + assert!(e.encode(&[&[0.0; 4], &[0.0; 3]]).is_err()); + assert!(e.encode(&[&[f32::NAN; 4], &[0.0; 4]]).is_err()); + let loud = vec![1e30f32; 5000]; + assert!(!e.encode(&[&loud, &loud]).unwrap().is_empty()); + assert!(!e.finish().unwrap().is_empty()); + assert!(e.finish().unwrap().is_empty()); + assert!(e.encode(&[&[0.0; 4], &[0.0; 4]]).is_err()); +} + +#[test] +fn granules_count_the_samples_and_trim_the_end() { + for len in [0usize, 1, 1023, 1024, 1025, 48_000] { + let mut e = Encoder::new(EncoderConfig::new(48_000, 1, 3.0)).unwrap(); + let x = vec![0.1f32; len]; + let mut packets = e.encode(&[&x]).unwrap(); + packets.extend(e.finish().unwrap()); + assert_eq!(packets.len(), len.div_ceil(HOP) + 1, "len {len}"); + // packet k ends output at k·1024 samples; the last one at the input's length + let want: Vec = (0..packets.len()).map(|k| ((k * HOP) as i64).min(len as i64)).collect(); + let got: Vec = packets.iter().map(|p| p.1).collect(); + assert_eq!(got, want, "len {len}"); + } +} + +// --- ffmpeg oracle --- + +/// An Ogg Vorbis file of the three headers and the audio packets, through craft-media's writer. +fn ogg(packets: &[(Vec, i64)]) -> Vec { + let mut w = craft_media::ogg::PageWriter::new(7); + let mut out = Vec::new(); + for (i, (p, g)) in packets.iter().enumerate() { + out.extend(if i < 3 { w.header(p) } else { w.packet(p, *g) }); + } + out.extend(w.finish(None)); + out +} + +fn comment() -> Vec { + craft_media::ogg::vorbis_comment("test", &[]) +} + +/// Encodes `input` (planar) and decodes it with ffmpeg's `decoder`; returns the planar output. +/// ffmpeg's decoders to check: its own `vorbis` always, and `libvorbis` when ffmpeg has it. +fn decoders(ff: &Path) -> Vec<&'static str> { + let list = + Command::new(ff).args(["-hide_banner", "-decoders"]).output().map(|o| String::from_utf8_lossy(&o.stdout).into_owned()).unwrap_or_default(); + if list.lines().any(|l| l.split_whitespace().nth(1) == Some("libvorbis")) { + vec!["vorbis", "libvorbis"] + } else { + eprintln!("SKIPPED (libvorbis decoder): this ffmpeg has no libvorbis; checking its own decoder only"); + vec!["vorbis"] + } +} + +fn round_trip(input: &[Vec], rate: u32, quality: f32, decoder: &str, name: &str) -> Option<(Vec>, usize)> { + let ff = craft_media_testkit::ffmpeg_or_skip("vorbis oracle")?; + let mut e = Encoder::new(EncoderConfig::new(rate, input.len(), quality)).unwrap(); + let mut packets = vec![(e.identification(), 0), (comment(), 0), (e.setup(), 0)]; + let n = input[0].len(); + let mut at = 0; + while at < n { + let end = (at + 4410).min(n); + let s: Vec<&[f32]> = input.iter().map(|c| &c[at..end]).collect(); + packets.extend(e.encode(&s).unwrap()); + at = end; + } + packets.extend(e.finish().unwrap()); + let bytes: usize = packets[3..].iter().map(|p| p.0.len()).sum(); + let dir = craft_media_testkit::fixtures_dir("audio/vorbis-encoder"); + let path = dir.join(format!("{name}.ogg")); + std::fs::write(&path, ogg(&packets)).unwrap(); + let out = Command::new(ff) + .args(["-v", "error", "-xerror", "-c:a", decoder, "-i"]) + .arg(&path) + .args(["-f", "f32le", "-c:a", "pcm_f32le", "-"]) + .output() + .unwrap(); + assert!(out.status.success() && out.stderr.is_empty(), "{name} {decoder}: {}", String::from_utf8_lossy(&out.stderr)); + let pcm: Vec = out.stdout.as_chunks::<4>().0.iter().map(|&b| f32::from_le_bytes(b)).collect(); + let ch = input.len(); + Some(((0..ch).map(|c| pcm.iter().skip(c).step_by(ch).copied().collect()).collect(), bytes)) +} + +fn snr_db(a: &[f32], b: &[f32]) -> f64 { + let (mut s, mut e) = (0f64, 0f64); + for (&x, &y) in a.iter().zip(b) { + s += f64::from(x) * f64::from(x); + e += (f64::from(x) - f64::from(y)).powi(2); + } + 10.0 * (s.max(1e-20) / e.max(1e-20)).log10() +} + +fn music(n: usize, seed: u32) -> Vec { + let mut r = seed; + (0..n) + .map(|i| { + let t = i as f32 / 48_000.0; + r = r.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + let noise = (r >> 8) as f32 / 8_388_608.0 - 1.0; + let chord: f32 = [220.0f32, 277.18, 329.63, 440.0] + .iter() + .enumerate() + .map(|(k, f)| (0.2 / (k + 1) as f32) * (2.0 * std::f32::consts::PI * f * t).sin()) + .sum(); + chord + 0.01 * noise + }) + .collect() +} + +/// Harmonic chords changing every half second, with decaying noise bursts (music-like). +fn chords(n: usize, seed: u32) -> Vec { + let mut r = seed; + let notes = [220.0f32, 277.2, 329.6, 440.0, 349.2, 523.3, 392.0, 659.3]; + (0..n) + .map(|i| { + let t = i as f32 / 48_000.0; + let chord = (t * 2.0) as usize; + let mut s = 0.0; + for k in 0..3 { + let f = notes[(chord * 3 + k * 2) % notes.len()]; + for h in 1..6 { + s += (2.0 * std::f32::consts::PI * f * h as f32 * t).sin() * 0.08 / h as f32; + } + } + r = r.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + let env = (-(t * 2.0).fract() * 3.0).exp(); + s * (0.5 + 0.5 * env) + 0.02 * ((r >> 8) as f32 / 8_388_608.0 - 1.0) * env + }) + .collect() +} + +/// ffmpeg's own Vorbis decoder and libvorbis read every stream exactly as long as the input, +/// with SNR floors about 6 dB (chords 4 dB) under what the encoder measured when written, and bit +/// rates near libvorbis's for the same quality. +/// A signal, and per quality the SNR floor and the bit-rate range (kbit/s). +type Case = (&'static str, Vec>, [(f32, f64, std::ops::Range); 5]); + +#[test] +fn ffmpeg_decodes_every_quality() { + let Some(ff) = craft_media_testkit::ffmpeg_or_skip("vorbis oracle") else { return }; + let n = 48_000 * 3; + let cases: [Case; 2] = [ + ( + "music", + vec![music(n, 1), music(n, 2).iter().map(|v| v * 0.7).collect()], + [(-1.0, 3.0, 15..90), (0.0, 6.5, 20..110), (3.0, 12.0, 60..180), (6.0, 18.5, 120..280), (10.0, 25.5, 180..400)], + ), + ( + "chords", + vec![chords(n, 1), chords(n, 2).iter().map(|v| v * 0.8).collect()], + [(-1.0, 4.5, 20..90), (0.0, 6.0, 25..110), (3.0, 10.0, 70..200), (6.0, 14.0, 130..300), (10.0, 19.0, 180..420)], + ), + ]; + for (name, input, levels) in &cases { + for (q, floor, kbps) in levels { + for decoder in decoders(&ff) { + let Some((out, bytes)) = round_trip(input, 48_000, *q, decoder, &format!("{name}{q}")) else { return }; + assert!(out.iter().all(|c| c.len() == n), "{name} q{q} {decoder}: length {}", out[0].len()); + let snr = snr_db(&input[0][4096..], &out[0][4096..]); + assert!(snr >= *floor, "{name} q{q} {decoder}: {snr:.2} dB < {floor}"); + let rate = bytes * 8 / 3 / 1000; + assert!(kbps.contains(&rate), "{name} q{q}: {rate} kbit/s outside {kbps:?}"); + } + } + } +} + +/// Mono, 44.1 kHz, 3.0, 5.1 and 6.1 streams decode too, with libvorbis as well (which reads each +/// residue partition from channel 0); a silent channel costs one bit per block. +#[test] +fn channel_counts_and_rates() { + let Some(ff) = craft_media_testkit::ffmpeg_or_skip("vorbis oracle") else { return }; + let n = 44_100; + for (rate, ch) in [(44_100u32, 1usize), (22_050, 2), (48_000, 3), (48_000, 6), (48_000, 7)] { + let mut input: Vec> = (0..ch).map(|c| chords(n, c as u32 + 1).iter().map(|v| v * (1.0 - 0.1 * c as f32)).collect()).collect(); + if ch == 6 { + input[3] = vec![0.0; n]; + } + for decoder in decoders(&ff) { + let Some((mut out, _)) = round_trip(&input, rate, 5.0, decoder, &format!("ch{ch}_{rate}_{decoder}")) else { return }; + if decoder == "vorbis" { + // ffmpeg's decoder gives WAV order (5.1: FL FR FC LFE BL BR); the stream is in Vorbis + // order (L C R Ls Rs LFE), which libvorbis passes through + let wav_to_vorbis: &[usize] = match ch { + 3 => &[0, 2, 1], + 6 => &[0, 2, 1, 4, 5, 3], + 7 => &[0, 2, 1, 5, 6, 4, 3], + _ => &[0, 1], + }; + if ch > 2 { + out = wav_to_vorbis.iter().map(|&i| out[i].clone()).collect(); + } + } + assert_eq!(out.len(), ch); + for (c, (a, b)) in input.iter().zip(&out).enumerate() { + assert_eq!(b.len(), n, "{ch} ch {rate} Hz {decoder}: channel {c} length"); + if a.iter().all(|&v| v == 0.0) { + assert!(b.iter().all(|v| v.abs() < 1e-6), "silent channel {c}"); + } else { + let snr = snr_db(&a[4096..], &b[4096..]); + assert!(snr > 8.0, "{ch} ch {rate} Hz {decoder} channel {c}: {snr:.1} dB"); + } + } + } + } +} + +/// Every partition holds whole sample frames (libvorbis decodes each one from channel 0), splits +/// the interleaved residue evenly and fits every book's dimension. +#[test] +fn partitions_hold_whole_frames() { + for ch in 1..=8 { + let p = partition_size(ch); + assert_eq!(p % ch, 0, "{ch} channels"); + assert_eq!((ch * BLOCK / 2) % p, 0, "{ch} channels"); + assert_eq!(p % 4, 0, "{ch} channels"); + } +} diff --git a/crates/audio/src/vorbis/mod.rs b/crates/audio/src/vorbis/mod.rs new file mode 100644 index 000000000..29c423f1d --- /dev/null +++ b/crates/audio/src/vorbis/mod.rs @@ -0,0 +1,4 @@ +//! Vorbis I (feature `vorbis-encode`): the pure-Rust [`encoder`]. Decoding Vorbis goes through +//! [`crate::decode`]. + +pub mod encoder; diff --git a/crates/audio/src/write/mod.rs b/crates/audio/src/write/mod.rs index ce4b90773..a2043d279 100644 --- a/crates/audio/src/write/mod.rs +++ b/crates/audio/src/write/mod.rs @@ -12,7 +12,7 @@ mod pcm; #[cfg(feature = "vorbis-encode")] mod vorbis; #[cfg(feature = "vorbis-encode")] -pub use vorbis::{OGG_BITRATES, encode as encode_ogg}; +pub use vorbis::{OGG_BITRATES, OggVorbisOptions, encode as encode_ogg, encode_ogg_vorbis, quality_for_bitrate}; #[cfg(feature = "pcm-encode")] mod raw; #[cfg(feature = "pcm-encode")] @@ -66,15 +66,22 @@ pub fn encode(buf: &AudioBuffer, opts: &EncodeOptions) -> Result> { EncodeFormat::Aac | EncodeFormat::Alac | EncodeFormat::Mp3 | EncodeFormat::Ac3 | EncodeFormat::Eac3 => coded::encode(buf, opts), #[cfg(feature = "vorbis-encode")] EncodeFormat::Ogg => { - if opts.layout.is_some_and(|layout| !EncodeFormat::Ogg.supports_layout(&layout)) { - return Err(AudioError::Unsupported("Vorbis requires a mono or stereo speaker layout".into())); + let count = u16::try_from(buf.channels.len()).unwrap_or(u16::MAX); + let layout = opts.layout.unwrap_or_else(|| ChannelLayout::default_for(count)); + if layout.channels() != count { + return Err(AudioError::Unsupported(format!("a {}-channel layout for {count} channels", layout.channels()))); } - let kbps = match opts.rate_control { - None => 192, - Some(crate::RateControl::Bitrate(bps)) => bps / 1000, - Some(crate::RateControl::Quality(_)) => return Err(AudioError::Unsupported("Ogg export takes a target bitrate".into())), + let map = vorbis::vorbis_map(&layout)?; + let quality = match opts.rate_control { + None => vorbis::quality_for_bitrate(192, buf.channels.len()), + Some(crate::RateControl::Bitrate(bps)) => { + vorbis::quality_for_bitrate((bps / 1000).clamp(OGG_BITRATES.0, OGG_BITRATES.1), buf.channels.len()) + } + Some(crate::RateControl::Quality(q)) if q.is_nan() => return Err(AudioError::Encode("the Vorbis quality is NaN".into())), + Some(crate::RateControl::Quality(q)) => -1.0 + 11.0 * q.clamp(0.0, 1.0), }; - vorbis::encode(buf, kbps) + let planar: Vec<&[f32]> = map.iter().filter_map(|&i| buf.channels.get(i).map(Vec::as_slice)).collect(); + vorbis::encode_planar(buf.sample_rate, &planar, &OggVorbisOptions::new(quality)) } #[allow(unreachable_patterns)] f => Err(AudioError::Unsupported(format!("writing {f:?}"))), @@ -106,7 +113,7 @@ const fn extensions() -> ([&'static str; 13], usize) { add!(cfg!(feature = "mp3-encode"), "mp3"); add!(cfg!(feature = "ac3-encode"), "ac3"); add!(cfg!(feature = "eac3-encode"), "eac3", "ec3"); - add!(cfg!(all(feature = "vorbis-encode", not(target_arch = "wasm32"))), "ogg"); + add!(cfg!(feature = "vorbis-encode"), "ogg"); (all, n) } @@ -148,7 +155,7 @@ pub fn encode_format_for(name: &str) -> Option { "ac3" => Some(EncodeFormat::Ac3), #[cfg(feature = "eac3-encode")] "eac3" | "ec3" => Some(EncodeFormat::Eac3), - #[cfg(all(feature = "vorbis-encode", not(target_arch = "wasm32")))] + #[cfg(feature = "vorbis-encode")] "ogg" => Some(EncodeFormat::Ogg), _ => None, } diff --git a/crates/audio/src/write/vorbis.rs b/crates/audio/src/write/vorbis.rs index f56d01148..6c19ada77 100644 --- a/crates/audio/src/write/vorbis.rs +++ b/crates/audio/src/write/vorbis.rs @@ -1,83 +1,198 @@ -//! Ogg Vorbis encoding (RFC 5334): the reference libvorbis encoder, built from its C sources by -//! `vorbis_rs`. Stereo or mono, variable bitrate at the requested kbit/s (64–500), with a -//! fallback to a quality preset when the bitrate is not supported for the rate and channel count. +//! Ogg Vorbis files (Vorbis I §A, RFC 5334) from the pure-Rust [`crate::vorbis::encoder`] and +//! craft-media's Ogg page writer: 1 to 8 channels in Vorbis channel order, a quality from -1 to +//! 10 (variable bit rate), and the tags in the comment header. Builds for every target. -use crate::{AudioBuffer, AudioError, Result}; +use craft_media::ogg::{PageWriter, vorbis_comment}; -/// The bitrates an Ogg Vorbis export is asked for, in kbit/s. +use crate::vorbis::encoder::{DEFAULT_QUALITY, Encoder, EncoderConfig, Error as VorbisError}; +use crate::{AudioBuffer, AudioError, ChannelLayout, Result}; + +/// The bit rates [`encode_ogg`](crate::encode_ogg) takes, in kbit/s. Vorbis is variable: the bit rate picks a +/// quality ([`quality_for_bitrate`]), and the file comes out near it for typical music. pub const OGG_BITRATES: (u32, u32) = (64, 500); -/// The block size the encoder processes at a time: small enough that memory stays bounded -/// regardless of file length. -#[cfg(not(target_arch = "wasm32"))] -const BLOCK: usize = 4096; +/// The serial number of the stream when the options do not set one (`LCRF`): one stream per file, +/// so a constant keeps files byte-for-byte reproducible. +const SERIAL: u32 = 0x4C43_5246; -#[cfg(not(target_arch = "wasm32"))] -fn encode_error(e: vorbis_rs::VorbisError) -> AudioError { - AudioError::Encode(e.to_string()) +/// Samples per channel fed to the encoder at a time. +const CHUNK: usize = 4096; + +/// What [`encode_ogg_vorbis`] writes besides the audio. +/// +/// `#[non_exhaustive]`: build it with `OggVorbisOptions::default()` or +/// [`OggVorbisOptions::new`] and the `with_*` methods. +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct OggVorbisOptions { + /// -1 (smallest) to 10 (best); clamped, and NaN or infinite reads as + /// [`DEFAULT_QUALITY`] (6). + pub quality: f32, + /// The comment header's vendor string. + pub vendor: String, + /// `NAME=value` comments (Vorbis I §5), in order. A name that is empty, or holds `=` or a byte + /// outside 0x20–0x7D, is left out. + pub tags: Vec<(String, String)>, + /// The logical stream's serial number. + pub serial: u32, } -/// Encode `buf` as an Ogg Vorbis stream at about `kbps` (variable, as Vorbis is): stereo, or mono -/// for a mono buffer. A bitrate the encoder has no setup for at that rate and channel count falls -/// back to a quality that suits it. `Err` says why there is nothing. -#[cfg(not(target_arch = "wasm32"))] -pub fn encode(buf: &AudioBuffer, kbps: u32) -> Result> { - use std::num::{NonZeroU8, NonZeroU32}; - - let channels = buf.channels.len(); - if !(1..=2).contains(&channels) { - return Err(AudioError::Unsupported("Ogg Vorbis export requires mono or stereo audio".into())); +impl Default for OggVorbisOptions { + fn default() -> Self { + OggVorbisOptions { quality: DEFAULT_QUALITY, vendor: "craft-audio".into(), tags: Vec::new(), serial: SERIAL } } - if buf.channels.iter().any(|c| c.len() != buf.frames() || c.iter().any(|s| !s.is_finite())) { +} + +impl OggVorbisOptions { + /// The defaults at `quality`. + pub fn new(quality: f32) -> Self { + OggVorbisOptions { quality, ..Self::default() } + } + + /// Sets [`quality`](Self::quality). + pub fn with_quality(mut self, quality: f32) -> Self { + self.quality = quality; + self + } + + /// Sets [`vendor`](Self::vendor). + pub fn with_vendor(mut self, vendor: impl Into) -> Self { + self.vendor = vendor.into(); + self + } + + /// Adds a comment ([`tags`](Self::tags)). + pub fn with_tag(mut self, name: impl Into, value: impl Into) -> Self { + self.tags.push((name.into(), value.into())); + self + } + + /// Sets [`serial`](Self::serial). + pub fn with_serial(mut self, serial: u32) -> Self { + self.serial = serial; + self + } +} + +fn encode_error(e: VorbisError) -> AudioError { + AudioError::Encode(format!("Vorbis: {e}")) +} + +/// Encode `buf` as an Ogg Vorbis file with `opts`. The channels are in Vorbis channel order +/// (Vorbis I §4.3.9: L C R for three, L C R Ls Rs LFE for 5.1, L C R Ls Rs Rl Rr LFE for 7.1). +/// The last granule position is the buffer's length, so a decoder returns exactly its frames. +/// +/// ``` +/// use craft_audio::{AudioBuffer, OggVorbisOptions, encode_ogg_vorbis}; +/// +/// let buf = AudioBuffer::new(48_000, 6, 4_800); // 0.1 s of 5.1 silence +/// let opts = OggVorbisOptions::new(4.0).with_tag("TITLE", "Silence"); +/// let ogg = encode_ogg_vorbis(&buf, &opts).unwrap(); +/// assert!(ogg.starts_with(b"OggS")); +/// ``` +pub fn encode_ogg_vorbis(buf: &AudioBuffer, opts: &OggVorbisOptions) -> Result> { + let planar: Vec<&[f32]> = buf.channels.iter().map(Vec::as_slice).collect(); + encode_planar(buf.sample_rate, &planar, opts) +} + +/// [`encode_ogg_vorbis`] over channels already in Vorbis order. +pub(crate) fn encode_planar(sample_rate: u32, planar: &[&[f32]], opts: &OggVorbisOptions) -> Result> { + if !(1..=8).contains(&planar.len()) { + return Err(AudioError::Unsupported(format!("Ogg Vorbis holds 1 to 8 channels, not {}", planar.len()))); + } + let frames = planar.first().map_or(0, |c| c.len()); + if planar.iter().any(|c| c.len() != frames || c.iter().any(|s| !s.is_finite())) { return Err(AudioError::Encode("Vorbis samples must be finite and channels equally long".into())); } - let (Some(rate), Some(count)) = (NonZeroU32::new(buf.sample_rate), NonZeroU8::new(channels as u8)) else { + if sample_rate == 0 { return Err(AudioError::Encode("there is no audio to encode".into())); - }; - let target = NonZeroU32::new(kbps.clamp(OGG_BITRATES.0, OGG_BITRATES.1) * 1000).unwrap_or(NonZeroU32::MIN); - - let encode = |strategy: vorbis_rs::VorbisBitrateManagementStrategy| -> Result> { - let mut encoder = vorbis_rs::VorbisEncoderBuilder::new_with_serial(rate, count, Vec::new(), 0x4C43_5246) - .bitrate_management_strategy(strategy) - .build() - .map_err(encode_error)?; - let frames = buf.frames(); - let mut at = 0usize; - while at < frames { - let end = (at + BLOCK).min(frames); - let block: Vec<&[f32]> = buf.channels.iter().take(channels).map(|side| side.get(at..end).unwrap_or_default()).collect(); - encoder.encode_audio_block(&block).map_err(encode_error)?; - at = end; + } + let mut enc = Encoder::new(EncoderConfig::new(sample_rate, planar.len(), opts.quality)).map_err(encode_error)?; + let mut pages = PageWriter::new(opts.serial); + let tags: Vec<(&str, &str)> = opts.tags.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect(); + let mut out = pages.header(&enc.identification()); + out.extend(pages.header(&vorbis_comment(&opts.vendor, &tags))); + out.extend(pages.header(&enc.setup())); + // The first audio packet (which outputs nothing) gets a page of its own: a stream whose first + // audio page is also its last would leave decoders to choose between start and end trimming + // (Vorbis I §A.2), and ffmpeg then plays the padding. + let mut first = true; + let mut add = |packets: Vec<(Vec, i64)>, out: &mut Vec| { + for (packet, granule) in packets { + out.extend(pages.packet(&packet, granule)); + if std::mem::take(&mut first) { + out.extend(pages.flush()); + } } - encoder.finish().map_err(encode_error) }; + let mut at = 0; + loop { + let end = (at + CHUNK).min(frames); + let chunk: Vec<&[f32]> = planar.iter().map(|c| c.get(at..end).unwrap_or_default()).collect(); + add(enc.encode(&chunk).map_err(encode_error)?, &mut out); + at = end; + if at >= frames { + break; + } + } + add(enc.finish().map_err(encode_error)?, &mut out); + out.extend(pages.finish(None)); + Ok(out) +} - // Convert Rust unwinds to an error. This does not catch native crashes or aborts. - let attempt = |strategy| { - std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| encode(strategy))) - .unwrap_or_else(|_| Err(AudioError::Encode("the Vorbis encoder failed".into()))) - }; - match attempt(vorbis_rs::VorbisBitrateManagementStrategy::Vbr { target_bitrate: target }) { - Ok(bytes) => Ok(bytes), - Err(_) => attempt(vorbis_rs::VorbisBitrateManagementStrategy::QualityVbr { target_quality: 0.6 }) - .map_err(|e| AudioError::Encode(format!("the Vorbis encoder refused the audio: {e}"))), +/// Nominal kbit/s per channel at each whole quality from -1 to 10: this encoder's rate on +/// music-like test signals at 48 kHz (harmonic chords with noise), averaged. +const KBPS_PER_CHANNEL: [f32; 12] = [18.0, 23.0, 31.0, 43.0, 58.0, 75.0, 89.0, 100.0, 111.0, 121.0, 130.0, 140.0]; + +/// The quality whose nominal bit rate is `kbps` kbit/s for `channels` channels (clamped to -1..=10). +/// Vorbis is variable: the bit rate of a file depends on the audio. +pub fn quality_for_bitrate(kbps: u32, channels: usize) -> f32 { + let per = kbps as f32 / channels.max(1) as f32; + let mut q = -1.0; + for pair in KBPS_PER_CHANNEL.windows(2) { + if let [lo, hi] = *pair + && per <= hi + { + return q + ((per - lo) / (hi - lo)).clamp(0.0, 1.0); + } + q += 1.0; + } + 10.0 +} + +/// Encode `buf` as an Ogg Vorbis stream near `kbps` kbit/s ([`OGG_BITRATES`], clamped): the +/// quality [`quality_for_bitrate`] gives, no tags. 1 to 8 channels in Vorbis channel order. +/// `Err` says why there is nothing. +pub fn encode(buf: &AudioBuffer, kbps: u32) -> Result> { + let kbps = kbps.clamp(OGG_BITRATES.0, OGG_BITRATES.1); + encode_ogg_vorbis(buf, &OggVorbisOptions::new(quality_for_bitrate(kbps, buf.channels.len()))) +} + +/// WAV-order speakers ([`crate::EncodeFormat::layout_masks`]) to Vorbis order, per channel count. +const VORBIS_ORDER: [&[usize]; 8] = + [&[0], &[0, 1], &[0, 2, 1], &[0, 1, 2, 3], &[0, 2, 1, 3, 4], &[0, 2, 1, 4, 5, 3], &[0, 2, 1, 5, 6, 4, 3], &[0, 2, 1, 6, 7, 4, 5, 3]]; + +/// For each Vorbis channel, the channel of audio in `layout` it comes from. +pub(crate) fn vorbis_map(layout: &ChannelLayout) -> Result> { + let n = layout.channels(); + let mask = crate::EncodeFormat::Ogg.layout_masks(n).first().copied(); + let order = usize::from(n).checked_sub(1).and_then(|i| VORBIS_ORDER.get(i)).copied(); + match (mask, order) { + (Some(mask), Some(order)) => crate::layout::channel_map(mask, Some(order), layout) + .ok_or_else(|| AudioError::Unsupported(format!("Vorbis has no layout of {n} channels with these speakers"))), + _ => Err(AudioError::Unsupported(format!("Ogg Vorbis holds 1 to 8 channels, not {n}"))), } } -/// Ogg Vorbis encoding is unavailable on WASM; returns an unsupported-format error. -#[cfg(target_arch = "wasm32")] -pub fn encode(_buf: &AudioBuffer, _kbps: u32) -> Result> { - Err(AudioError::Unsupported("Ogg Vorbis files are exported by the desktop app".into())) -} - -#[cfg(all(test, not(target_arch = "wasm32")))] +#[cfg(test)] mod tests { #![allow(clippy::unwrap_used)] - use super::*; + use super::{OGG_BITRATES, OggVorbisOptions, encode, encode_ogg_vorbis, quality_for_bitrate, vorbis_map}; + use crate::{AudioBuffer, AudioError, ChannelLayout}; #[test] fn rejects_unsupported_or_malformed_channels_without_dropping_audio() { - for channels in [0, 3, 6] { + for channels in [0, 9] { assert!(matches!(encode(&AudioBuffer::new(48_000, channels, 16), 192), Err(AudioError::Unsupported(_)))); } let ragged = AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.0; 16], vec![0.0; 15]] }; @@ -85,46 +200,68 @@ mod tests { assert!(encode(&AudioBuffer::new(0, 1, 16), 192).is_err()); let invalid = AudioBuffer { sample_rate: 48_000, channels: vec![vec![f32::NAN]] }; assert!(encode(&invalid, 192).is_err()); + let infinite = AudioBuffer { sample_rate: 48_000, channels: vec![vec![f32::INFINITY]] }; + assert!(encode(&infinite, 192).is_err()); } #[test] - fn generic_and_direct_encoders_write_mono_and_stereo_ogg() { - for channels in [1, 2] { + fn generic_and_direct_encoders_write_one_to_eight_channels() { + for channels in 1..=8 { let audio = AudioBuffer::new(48_000, channels, 512); let opts = crate::EncodeOptions::new(crate::EncodeFormat::Ogg); assert!(opts.check(48_000, channels).is_ok()); assert_eq!(opts.bitrate(48_000, channels).unwrap(), None); let direct = encode(&audio, 192).unwrap(); - let generic = crate::encode(&audio, &opts).unwrap(); assert!(direct.starts_with(b"OggS")); - assert_eq!(direct, generic); + // silence: the channel order changes nothing, so the generic path writes the same file + let generic = crate::encode(&audio, &opts.with_bitrate(192_000)).unwrap(); + assert_eq!(direct, generic, "{channels} channels"); #[cfg(feature = "decode")] { let (info, decoded) = crate::decode(&generic, Some("ogg")).unwrap(); assert_eq!(info.channels, channels); - assert_eq!(decoded.num_channels(), channels); assert_eq!(decoded.frames(), audio.frames()); } } } - #[cfg(feature = "decode")] + #[test] - fn distinct_signals_survive_full_and_partial_blocks() { - for frames in [4096, 4097, 8193] { - let channels: Vec> = [440.0f32, 880.0] - .into_iter() - .map(|hz| (0..frames).map(|i| (std::f32::consts::TAU * hz * i as f32 / 48_000.0).sin() * 0.4).collect()) - .collect(); - let audio = AudioBuffer { sample_rate: 48_000, channels }; - let bytes = encode(&audio, 192).unwrap(); - let (_, decoded) = crate::decode(&bytes, Some("ogg")).unwrap(); - assert_eq!(decoded.frames(), frames); - assert_eq!(decoded.channels.len(), 2); - for (expected, actual) in audio.channels.iter().zip(&decoded.channels) { - let mse = expected.iter().zip(actual).map(|(a, b)| (a - b).powi(2)).sum::() / frames as f32; - // Vorbis is lossy; this tolerates codec error but rejects silence or swapped channels. - assert!(mse.sqrt() < 0.08, "{frames} frames: RMS error {}", mse.sqrt()); - } + fn the_quality_follows_the_bit_rate() { + assert_eq!(quality_for_bitrate(0, 2), -1.0); + assert_eq!(quality_for_bitrate(10_000, 2), 10.0); + assert!((quality_for_bitrate(200, 2) - 6.0).abs() < 1e-6); + assert!((quality_for_bitrate(600, 6) - 6.0).abs() < 1e-6); + let mut last = -2.0; + for kbps in OGG_BITRATES.0..=OGG_BITRATES.1 { + let q = quality_for_bitrate(kbps, 2); + assert!(q >= last && (-1.0..=10.0).contains(&q)); + last = q; } + assert!(quality_for_bitrate(192, 0).is_finite()); + } + + #[test] + fn layouts_map_to_vorbis_order() { + // 5.1 in WAV order (L R C LFE Ls Rs) is written L C R Ls Rs LFE + assert_eq!(vorbis_map(&ChannelLayout::SURROUND_5_1).unwrap(), [0, 2, 1, 4, 5, 3]); + assert_eq!(vorbis_map(&ChannelLayout::SURROUND_5_1_SIDE).unwrap(), [0, 2, 1, 4, 5, 3]); + assert_eq!(vorbis_map(&ChannelLayout::from_mask(0x63F)).unwrap(), [0, 2, 1, 6, 7, 4, 5, 3]); + assert_eq!(vorbis_map(&ChannelLayout::from_mask(0x7)).unwrap(), [0, 2, 1]); + assert_eq!(vorbis_map(&ChannelLayout::STEREO).unwrap(), [0, 1]); + assert!(vorbis_map(&ChannelLayout::from_mask(0xC)).is_err()); + assert!(vorbis_map(&ChannelLayout::unordered(9)).is_err()); + assert!(vorbis_map(&ChannelLayout::unordered(0)).is_err()); + } + + #[test] + fn options_build_and_tags_reach_the_comment_header() { + let opts = OggVorbisOptions::default().with_quality(2.0).with_vendor("v").with_tag("TITLE", "t").with_tag("bad=name", "x").with_serial(9); + assert_eq!((opts.quality, opts.vendor.as_str(), opts.serial, opts.tags.len()), (2.0, "v", 9, 2)); + let ogg = encode_ogg_vorbis(&AudioBuffer::new(44_100, 1, 100), &opts).unwrap(); + let file = craft_media::ogg::open(&ogg).unwrap(); + let s = &file.streams[0]; + assert_eq!((s.serial, s.saw_eos, s.headers.len()), (9, true, 3)); + assert_eq!(s.headers[1], craft_media::ogg::vorbis_comment("v", &[("TITLE", "t")])); + assert_eq!(s.last_granule, Some(100)); } } diff --git a/crates/audio/tests/audio/encode_format.rs b/crates/audio/tests/audio/encode_format.rs index e91f93623..72749df04 100644 --- a/crates/audio/tests/audio/encode_format.rs +++ b/crates/audio/tests/audio/encode_format.rs @@ -20,8 +20,8 @@ fn only_formats_the_encoder_writes_are_named() { } #[test] -fn ogg_names_follow_the_native_feature() { - let built = cfg!(all(feature = "vorbis-encode", not(target_arch = "wasm32"))); +fn ogg_names_follow_the_feature() { + let built = cfg!(feature = "vorbis-encode"); assert_eq!(EncodeFormat::Ogg.is_built(), built); assert_eq!(encode_format_for("ogg"), built.then_some(EncodeFormat::Ogg)); assert_eq!(craft_audio::ENCODE_EXTENSIONS.contains(&"ogg"), built); diff --git a/crates/audio/tests/codecs/file_formats.rs b/crates/audio/tests/codecs/file_formats.rs index 84169f665..82edf72aa 100644 --- a/crates/audio/tests/codecs/file_formats.rs +++ b/crates/audio/tests/codecs/file_formats.rs @@ -527,7 +527,7 @@ fn the_format_queries_agree_with_the_writer() { // The most channels is the most: one more is refused, by the layouts of a format that has // a fixed set and by the writer of one that has not. let most: Vec = EncodeFormat::ALL.iter().map(|f| f.max_channels()).collect(); - assert_eq!(most, [32_767, 32_767, 8, 8, 8, 2, 6, 8, 2]); + assert_eq!(most, [32_767, 32_767, 8, 8, 8, 2, 6, 8, 8]); for &format in EncodeFormat::ALL { let most = format.max_channels(); match format.fixed_layouts(most) { diff --git a/crates/audio/tests/codecs/main.rs b/crates/audio/tests/codecs/main.rs index c1c9e2131..41829be5c 100644 --- a/crates/audio/tests/codecs/main.rs +++ b/crates/audio/tests/codecs/main.rs @@ -20,3 +20,5 @@ mod opus; mod opusenc; mod routing_oracle; mod track_decoder; +#[cfg(feature = "vorbis-encode")] +mod vorbis_encoder_oracle; diff --git a/crates/audio/tests/codecs/vorbis_encoder_oracle.rs b/crates/audio/tests/codecs/vorbis_encoder_oracle.rs new file mode 100644 index 000000000..342f4fd9a --- /dev/null +++ b/crates/audio/tests/codecs/vorbis_encoder_oracle.rs @@ -0,0 +1,131 @@ +//! ffmpeg as an external oracle for the pure-Rust Vorbis encoder: both of its decoders (its own +//! and libvorbis) read every file exactly as long as the input, surround comes out on the right +//! speakers, and the tags reach ffprobe. Skipped when ffmpeg is not installed. + +use std::path::Path; +use std::process::Command; + +use craft_audio::{AudioBuffer, ChannelLayout, EncodeFormat, EncodeOptions, OggVorbisOptions, encode, encode_ogg_vorbis}; + +/// A different tone on every channel, so each output channel can be traced to its input. +fn tones(rate: u32, channels: usize, frames: usize) -> AudioBuffer { + let channels = (0..channels) + .map(|c| { + let f = 220.0 * (c + 1) as f32; + (0..frames).map(|i| (std::f32::consts::TAU * f * i as f32 / rate as f32).sin() * 0.3).collect() + }) + .collect(); + AudioBuffer { sample_rate: rate, channels } +} + +/// Decodes `file` with ffmpeg's `decoder`; planar output in ffmpeg's (WAV) channel order. +fn ffmpeg_decode(ff: &Path, decoder: &str, file: &[u8], channels: usize, tag: &str) -> Vec> { + let path = crate::testkit::fixtures_dir("vorbis-encoder").join(format!("{tag}.ogg")); + std::fs::write(&path, file).unwrap(); + let out = Command::new(ff) + .args(["-v", "error", "-xerror", "-c:a", decoder, "-i"]) + .arg(&path) + .args(["-f", "f32le", "-c:a", "pcm_f32le", "-"]) + .output() + .unwrap(); + assert!(out.status.success() && out.stderr.is_empty(), "{tag} {decoder}: {}", String::from_utf8_lossy(&out.stderr)); + let pcm: Vec = out.stdout.as_chunks::<4>().0.iter().map(|&b| f32::from_le_bytes(b)).collect(); + (0..channels).map(|c| pcm.iter().skip(c).step_by(channels).copied().collect()).collect() +} + +/// ffmpeg's decoders to check: its own `vorbis` always, and `libvorbis` when ffmpeg has it. +fn decoders(ff: &Path) -> Vec<&'static str> { + let list = + Command::new(ff).args(["-hide_banner", "-decoders"]).output().map(|o| String::from_utf8_lossy(&o.stdout).into_owned()).unwrap_or_default(); + if list.lines().any(|l| l.split_whitespace().nth(1) == Some("libvorbis")) { + vec!["vorbis", "libvorbis"] + } else { + eprintln!("SKIPPED (libvorbis decoder): this ffmpeg has no libvorbis; checking its own decoder only"); + vec!["vorbis"] + } +} + +fn correlation(a: &[f32], b: &[f32]) -> f64 { + let (mut ab, mut aa, mut bb) = (0f64, 0f64, 0f64); + for (&x, &y) in a.iter().zip(b) { + ab += f64::from(x) * f64::from(y); + aa += f64::from(x) * f64::from(x); + bb += f64::from(y) * f64::from(y); + } + ab / (aa * bb).sqrt().max(1e-20) +} + +/// Vorbis order (§4.3.9) of the WAV-order layouts the test writes, per channel count. +const VORBIS_ORDER: [&[usize]; 8] = + [&[0], &[0, 1], &[0, 2, 1], &[0, 1, 2, 3], &[0, 2, 1, 3, 4], &[0, 2, 1, 4, 5, 3], &[0, 2, 1, 5, 6, 4, 3], &[0, 2, 1, 6, 7, 4, 5, 3]]; + +/// 1 to 8 channels through `encode` with a WAV-order layout, at exactly the input's length out of +/// both decoders: ffmpeg's own puts every channel back on its WAV speaker, and libvorbis (through +/// ffmpeg, which passes its order through) gives the file's Vorbis order. +#[test] +fn every_channel_count_decodes_on_the_right_speakers() { + let ff = require_ffmpeg!(); + for channels in 1..=8u16 { + let layout = if channels == 7 { ChannelLayout::from_mask(0x70F) } else { ChannelLayout::default_for(channels) }; + let buf = tones(48_000, channels.into(), 24_000 + 77); + let opts = EncodeOptions::new(EncodeFormat::Ogg).with_layout(layout).with_quality(0.6); + let file = encode(&buf, &opts).unwrap(); + for decoder in decoders(&ff) { + let out = ffmpeg_decode(&ff, decoder, &file, channels.into(), &format!("tones{channels}-{decoder}")); + for (j, got) in out.iter().enumerate() { + let c = if decoder == "libvorbis" { VORBIS_ORDER[usize::from(channels) - 1][j] } else { j }; + assert_eq!(got.len(), buf.frames(), "{channels} ch {decoder}: channel {j} length"); + let best = (0..buf.channels.len()).max_by(|&a, &b| { + correlation(&buf.channels[a][2048..], &got[2048..]).total_cmp(&correlation(&buf.channels[b][2048..], &got[2048..])) + }); + assert_eq!(best, Some(c), "{channels} ch {decoder}: output channel {c} carries input {best:?}"); + let r = correlation(&buf.channels[c][2048..], &got[2048..]); + assert!(r > 0.95, "{channels} ch {decoder}: channel {c} correlation {r:.3}"); + } + } + } +} + +/// The comment header's tags, UTF-8 values included, are what ffprobe reports; a name that is not +/// a valid field name is left out. +#[test] +fn tags_reach_ffprobe() { + let Some(probe) = crate::common::tool("ffprobe").or_else(|| crate::common::skip("ffprobe not found")) else { return }; + let opts = OggVorbisOptions::new(3.0) + .with_tag("TITLE", "SmörgÃ¥sbord ✓") + .with_tag("ARTIST", "Craft") + .with_tag("bad=name", "dropped") + .with_vendor("craft-audio test"); + let file = encode_ogg_vorbis(&tones(44_100, 2, 44_100), &opts).unwrap(); + let path = crate::testkit::fixtures_dir("vorbis-encoder").join("tags.ogg"); + std::fs::write(&path, &file).unwrap(); + let out = Command::new(probe).args(["-v", "error", "-of", "json", "-show_streams", "-show_format"]).arg(&path).output().unwrap(); + assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); + let j: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap(); + let s = &j["streams"][0]; + assert_eq!((s["codec_name"].as_str(), s["channels"].as_u64(), s["sample_rate"].as_str()), (Some("vorbis"), Some(2), Some("44100"))); + let tags = &s["tags"]; + assert_eq!((tags["TITLE"].as_str(), tags["ARTIST"].as_str()), (Some("SmörgÃ¥sbord ✓"), Some("Craft"))); + assert!(tags.as_object().unwrap().keys().all(|k| !k.contains("bad")), "{tags}"); + assert_eq!(j["format"]["duration"].as_str().map(|d| d.parse::().unwrap()), Some(1.0)); +} + +/// The lowest and highest qualities and the bit-rate path decode at the exact length, mono at +/// 44.1 kHz and stereo at 22.05 kHz. +#[test] +fn quality_extremes_and_rates_decode_exactly() { + let ff = require_ffmpeg!(); + for (rate, channels, quality) in [(44_100u32, 1usize, -1.0f32), (44_100, 1, 10.0), (22_050, 2, -1.0), (22_050, 2, 10.0), (96_000, 2, 5.0)] { + let buf = tones(rate, channels, rate as usize / 2 + 13); + let file = encode_ogg_vorbis(&buf, &OggVorbisOptions::new(quality)).unwrap(); + for decoder in decoders(&ff) { + let out = ffmpeg_decode(&ff, decoder, &file, channels, &format!("q{quality}-{rate}-{channels}-{decoder}")); + assert!(out.iter().all(|c| c.len() == buf.frames()), "q{quality} {rate} Hz {decoder}: {}", out[0].len()); + let r = correlation(&buf.channels[0][2048..], &out[0][2048..]); + assert!(r > 0.9, "q{quality} {rate} Hz {decoder}: correlation {r:.3}"); + } + } + let low = craft_audio::encode_ogg(&tones(48_000, 2, 48_000), craft_audio::OGG_BITRATES.0).unwrap(); + let high = craft_audio::encode_ogg(&tones(48_000, 2, 48_000), craft_audio::OGG_BITRATES.1).unwrap(); + assert!(low.len() < high.len(), "{} vs {} bytes", low.len(), high.len()); +} diff --git a/crates/media/README.md b/crates/media/README.md index 6262976bb..6663da282 100644 --- a/crates/media/README.md +++ b/crates/media/README.md @@ -447,7 +447,13 @@ page yet. Damage recovery may read bytes again up to twice the file's length plu is read at most about three times over; the packets kept and the header packets of a stream are bounded too, with a warning when a bound is reached. `OpusTiming` gives each Opus packet's start, the pre-skip and the end trimming of the final page. -No muxer. + +The muxer side is `PageWriter`, from FilmCraft's `filmcraft-oggmux` by @JordyUSA +(storytold/filmcraft#4481): it laces the packets of one logical stream into pages (continued +packets, granule positions, BOS/EOS, CRC-32), puts each header packet on pages of its own, closes +audio pages at a byte or packet target, and can replace the final granule position for Ogg Opus +end trimming. `opus_tags` and `vorbis_comment` build the comment headers; tag names that are not +valid field names (Vorbis I §5.2.1) are left out. ## `avi` diff --git a/crates/media/src/ogg/mod.rs b/crates/media/src/ogg/mod.rs index 387c3fc71..d487f9224 100644 --- a/crates/media/src/ogg/mod.rs +++ b/crates/media/src/ogg/mod.rs @@ -1,4 +1,4 @@ -//! Clean-room Ogg demuxer, written from RFC 3533 (the Ogg encapsulation format), RFC 7845 (Ogg +//! Clean-room Ogg demuxer and page writer, written from RFC 3533 (the Ogg encapsulation format), RFC 7845 (Ogg //! Opus) and the Vorbis I specification's Ogg mapping (section A). //! //! [`open`] reads every page (capture pattern, header, lacing values, CRC-32 checked; damaged @@ -12,6 +12,9 @@ //! TOC-derived durations, the pre-skip, and end trimming of the final page, so a player can seek //! to any 48 kHz sample. //! +//! [`PageWriter`] writes the pages of a logical bitstream, and [`opus_tags`] and +//! [`vorbis_comment`] build the comment headers of Ogg Opus and Ogg Vorbis. +//! //! Layer L0: no dependencies beyond `std`, no `unsafe`, builds for `wasm32-unknown-unknown`. //! //! From FilmCraft's `filmcraft-ogg` (storytold/filmcraft@8fda2f2). Public items keep their names, so @@ -25,6 +28,9 @@ use std::fmt; use std::io; pub use crate::source::{ByteSource, ReadSeekSource}; +pub use write::{PAGE_TARGET_BYTES, PAGE_TARGET_PACKETS, PageWriter, opus_tags, vorbis_comment}; + +mod write; /// Errors from reading an Ogg file. #[non_exhaustive] @@ -879,3 +885,5 @@ impl VorbisInfo { #[cfg(test)] mod tests; +#[cfg(test)] +mod write_tests; diff --git a/crates/media/src/ogg/write.rs b/crates/media/src/ogg/write.rs new file mode 100644 index 000000000..ff631c650 --- /dev/null +++ b/crates/media/src/ogg/write.rs @@ -0,0 +1,206 @@ +//! Ogg page writer (RFC 3533) and the comment-header packets of Ogg Opus (RFC 7845 §5.2) and Ogg +//! Vorbis (Vorbis I §5). +//! +//! [`PageWriter`] laces packets into pages of one logical bitstream: lacing values of 255 with a +//! shorter last value per packet, at most 255 per page, continued packets flagged on the next +//! page, the granule position of the last packet completed on each page (-1 on a page where no +//! packet completes), page sequence numbers, the CRC-32, and the beginning- and end-of-stream +//! flags. Header packets are flushed to pages of their own, as both mappings require. +//! +//! From FilmCraft's `filmcraft-oggmux` by @JordyUSA (storytold/filmcraft#4481). + +use super::crc32; + +/// Default size at which an audio page is closed before the next packet is added. +pub const PAGE_TARGET_BYTES: usize = 4096; +/// Default number of packets after which an audio page is closed (one second of 20 ms Opus). +pub const PAGE_TARGET_PACKETS: usize = 50; + +/// Writes the pages of one logical bitstream. +#[derive(Clone, Debug)] +pub struct PageWriter { + serial: u32, + seq: u32, + lacing: Vec, + body: Vec, + /// Granule position of the last packet completed on the pending page. + granule: Option, + /// The pending page starts with the continuation of a packet. + continued: bool, + packets: usize, + bos_written: bool, + finished: bool, + target_bytes: usize, + target_packets: usize, +} + +impl PageWriter { + /// A writer for the logical bitstream with serial number `serial`, closing audio pages at + /// [`PAGE_TARGET_BYTES`] or [`PAGE_TARGET_PACKETS`]. + pub fn new(serial: u32) -> PageWriter { + PageWriter { + serial, + seq: 0, + lacing: Vec::with_capacity(255), + body: Vec::new(), + granule: None, + continued: false, + packets: 0, + bos_written: false, + finished: false, + target_bytes: PAGE_TARGET_BYTES, + target_packets: PAGE_TARGET_PACKETS, + } + } + + /// Closes audio pages at `bytes` of payload or `packets` packets, whichever comes first. + pub fn with_page_target(mut self, bytes: usize, packets: usize) -> PageWriter { + self.target_bytes = bytes.max(1); + self.target_packets = packets.max(1); + self + } + + /// Pages written so far. + pub fn pages(&self) -> u32 { + self.seq + } + + /// A header packet on pages of its own (granule position 0); the first one opens the stream. + pub fn header(&mut self, data: &[u8]) -> Vec { + let mut out = self.flush(); + out.extend(self.add(data, 0)); + out.extend(self.flush()); + out + } + + /// Adds an audio packet that ends at granule position `granule`; returns the pages it + /// completed. The pending page is closed first once it is full enough, so the last packet + /// always stays pending for [`Self::finish`] to mark the end of the stream. + pub fn packet(&mut self, data: &[u8], granule: i64) -> Vec { + let mut out = Vec::new(); + if self.body.len() >= self.target_bytes || self.packets >= self.target_packets { + out = self.flush(); + } + out.extend(self.add(data, granule)); + out + } + + /// Closes the pending page now, if there is one. + pub fn flush(&mut self) -> Vec { + let mut out = Vec::new(); + if !self.lacing.is_empty() { + self.emit(&mut out, false); + } + out + } + + /// Ends the stream: the pending page (or an empty page when nothing is pending) with the + /// end-of-stream flag. `last_granule` replaces the granule position of the final page (Ogg + /// Opus end trimming) when a packet is pending; `None` keeps the last packet's. End trimming + /// applies only to packets completed on the last page (RFC 7845 §4.4), so do not + /// [`Self::flush`] the last packet before finishing. + pub fn finish(&mut self, last_granule: Option) -> Vec { + let mut out = Vec::new(); + if self.finished { + return out; + } + if let Some(g) = last_granule + && !self.lacing.is_empty() + { + self.granule = Some(g); + } + self.emit(&mut out, true); + self.finished = true; + out + } + + fn add(&mut self, data: &[u8], granule: i64) -> Vec { + let mut out = Vec::new(); + let mut rest = data; + let mut started = false; + loop { + if self.lacing.len() == 255 { + // A full page: the next one continues this packet, unless it has not begun yet. + self.emit(&mut out, false); + self.continued = started; + } + let n = rest.len().min(255); + self.lacing.push(n as u8); + started = true; + self.body.extend_from_slice(&rest[..n]); + rest = &rest[n..]; + if n < 255 { + break; + } + } + self.granule = Some(granule); + self.packets += 1; + out + } + + fn emit(&mut self, out: &mut Vec, eos: bool) { + let mut flags = 0u8; + if self.continued { + flags |= 0x01; + } + if !self.bos_written { + flags |= 0x02; + self.bos_written = true; + } + if eos { + flags |= 0x04; + } + let start = out.len(); + out.extend_from_slice(b"OggS"); + out.push(0); + out.push(flags); + out.extend_from_slice(&self.granule.unwrap_or(-1).to_le_bytes()); + out.extend_from_slice(&self.serial.to_le_bytes()); + out.extend_from_slice(&self.seq.to_le_bytes()); + out.extend_from_slice(&[0; 4]); + out.push(self.lacing.len() as u8); + out.extend_from_slice(&self.lacing); + out.extend_from_slice(&self.body); + let crc = crc32(&out[start..]); + out[start + 22..start + 26].copy_from_slice(&crc.to_le_bytes()); + self.seq = self.seq.wrapping_add(1); + self.lacing.clear(); + self.body.clear(); + self.granule = None; + self.continued = false; + self.packets = 0; + } +} + +/// A comment header body: vendor string and `KEY=value` comments, little-endian lengths. +fn comments(magic: &[u8], vendor: &str, tags: &[(&str, &str)]) -> Vec { + let mut v = magic.to_vec(); + let len = |n: usize| u32::try_from(n).unwrap_or(u32::MAX).to_le_bytes(); + v.extend(len(vendor.len())); + v.extend_from_slice(vendor.as_bytes()); + let tags: Vec = tags.iter().filter(|(k, _)| valid_key(k)).map(|(k, val)| format!("{k}={val}")).collect(); + v.extend(len(tags.len())); + for t in &tags { + v.extend(len(t.len())); + v.extend_from_slice(t.as_bytes()); + } + v +} + +/// A comment field name: printable ASCII 0x20–0x7D other than `=` (Vorbis I §5.2.1), not empty. +fn valid_key(k: &str) -> bool { + !k.is_empty() && k.bytes().all(|b| (0x20..=0x7D).contains(&b) && b != b'=') +} + +/// The `OpusTags` packet (RFC 7845 §5.2). Comments whose name is not a valid field name are +/// left out. +pub fn opus_tags(vendor: &str, tags: &[(&str, &str)]) -> Vec { + comments(b"OpusTags", vendor, tags) +} + +/// The Vorbis comment header packet (Vorbis I §5: type 3, `vorbis`, comments, framing bit). +pub fn vorbis_comment(vendor: &str, tags: &[(&str, &str)]) -> Vec { + let mut v = comments(b"\x03vorbis", vendor, tags); + v.push(1); + v +} diff --git a/crates/media/src/ogg/write_tests.rs b/crates/media/src/ogg/write_tests.rs new file mode 100644 index 000000000..eebf5732e --- /dev/null +++ b/crates/media/src/ogg/write_tests.rs @@ -0,0 +1,200 @@ +//! The page writer, read back by [`open`]. + +#![allow(clippy::unwrap_used)] + +use super::{Codec, OpusTiming, PageWriter, crc32, open, opus_tags, vorbis_comment}; + +/// A TOC-only 20 ms CELT packet of `len` bytes whose bytes encode `i` (so packets differ). +fn opus_packet(i: usize, len: usize) -> Vec { + let mut p = vec![0xF8u8]; + p.extend((0..len.saturating_sub(1)).map(|k| (i * 31 + k) as u8)); + p +} + +fn opus_head(pre_skip: u16) -> Vec { + let mut h = b"OpusHead".to_vec(); + h.extend([1, 2]); + h.extend(pre_skip.to_le_bytes()); + h.extend(48_000u32.to_le_bytes()); + h.extend([0, 0, 0]); + h +} + +/// An Ogg Opus file of `n` packets of `sizes(i)` bytes, `samples` playable samples after the pre-skip. +fn write_opus(n: usize, sizes: impl Fn(usize) -> usize, pre_skip: u16, samples: i64) -> (Vec, Vec>) { + let mut w = PageWriter::new(0x1234_5678); + let mut f = w.header(&opus_head(pre_skip)); + f.extend(w.header(&opus_tags("FilmCraft", &[("TITLE", "Test"), ("bad=key", "dropped"), ("", "dropped")]))); + let mut packets = Vec::new(); + for i in 0..n { + let p = opus_packet(i, sizes(i)); + f.extend(w.packet(&p, (i as i64 + 1) * 960)); + packets.push(p); + } + f.extend(w.finish(Some(i64::from(pre_skip) + samples))); + (f, packets) +} + +#[test] +fn written_pages_read_back_packet_for_packet() { + let (f, packets) = write_opus(230, |i| 3 + (i * 37) % 400, 120, 230 * 960 - 120 - 500); + let ogg = open(&f).unwrap(); + assert!(ogg.warnings.is_empty(), "{:?}", ogg.warnings); + assert_eq!(ogg.streams.len(), 1); + let s = &ogg.streams[0]; + assert_eq!((s.serial, s.codec, s.saw_eos), (0x1234_5678, Codec::Opus, true)); + assert_eq!(s.headers.len(), 2); + assert_eq!(s.headers[0], opus_head(120)); + assert_eq!(s.headers[1], opus_tags("FilmCraft", &[("TITLE", "Test")])); + assert_eq!(s.packets.len(), packets.len()); + for (k, p) in packets.iter().enumerate() { + assert_eq!(&ogg.read_packet(&f, 0, k).unwrap(), p, "packet {k}"); + } + assert!(s.packets.last().unwrap().eos); + assert_eq!(s.last_granule, Some(120 + 230 * 960 - 120 - 500)); + // RFC 7845 §4: the end trimming of the final page. + let t = OpusTiming::of(s, 120); + assert_eq!(t.total, 230 * 960 - 120 - 500); + assert_eq!(t.starts[0], -120); + assert!(t.durations.iter().all(|&d| d == 960)); +} + +#[test] +fn headers_have_pages_of_their_own_and_only_the_first_page_opens_the_stream() { + let (f, _) = write_opus(3, |_| 10, 0, 3 * 960); + let mut pos = 0; + let mut flags = Vec::new(); + let mut seqs = Vec::new(); + while pos + 27 <= f.len() { + assert_eq!(&f[pos..pos + 4], b"OggS"); + let mut page = f[pos..].to_vec(); + let nseg = page[26] as usize; + let len = 27 + nseg + page[27..27 + nseg].iter().map(|&l| l as usize).sum::(); + page.truncate(len); + let stored = u32::from_le_bytes(page[22..26].try_into().unwrap()); + page[22..26].fill(0); + assert_eq!(crc32(&page), stored, "page CRC"); + flags.push(f[pos + 5]); + seqs.push(u32::from_le_bytes(f[pos + 18..pos + 22].try_into().unwrap())); + pos += len; + } + assert_eq!(pos, f.len()); + assert_eq!(flags, [0x02, 0x00, 0x04], "BOS head page, tags page, one audio page with EOS"); + assert_eq!(seqs, [0, 1, 2]); +} + +#[test] +fn a_packet_longer_than_a_page_continues() { + let big = 2 * 255 * 255 + 7000; // spans three pages: the middle one completes no packet + let (f, packets) = write_opus(4, |i| if i == 1 { big } else { 50 }, 0, 4 * 960); + let ogg = open(&f).unwrap(); + assert!(ogg.warnings.is_empty()); + for (k, p) in packets.iter().enumerate() { + assert_eq!(&ogg.read_packet(&f, 0, k).unwrap(), p, "packet {k}"); + } + // a page on which no packet completes has granule position -1 + let mut pos = 0; + let mut saw_none = false; + while pos + 27 <= f.len() { + let nseg = f[pos + 26] as usize; + let lacing = &f[pos + 27..pos + 27 + nseg]; + let g = i64::from_le_bytes(f[pos + 6..pos + 14].try_into().unwrap()); + if lacing.iter().all(|&l| l == 255) { + assert_eq!(g, -1); + saw_none = true; + } + pos += 27 + nseg + lacing.iter().map(|&l| l as usize).sum::(); + } + assert!(saw_none); +} + +#[test] +fn page_targets_bound_page_size_and_finish_marks_the_end_once() { + let mut w = PageWriter::new(7).with_page_target(1000, 10); + let mut f = w.header(&opus_head(0)); + f.extend(w.header(&opus_tags("v", &[]))); + for i in 0..95 { + f.extend(w.packet(&opus_packet(i, 160), (i as i64 + 1) * 960)); + } + f.extend(w.finish(None)); + assert!(w.finish(None).is_empty()); + let ogg = open(&f).unwrap(); + let s = &ogg.streams[0]; + assert_eq!(s.packets.len(), 95); + assert_eq!(s.pages, 2 + 95_u32.div_ceil(7), "1000-byte pages hold 7 packets of 160 bytes"); + assert_eq!(s.last_granule, Some(95 * 960)); + // An empty stream still ends. + let mut w = PageWriter::new(1); + let mut f = w.header(&opus_head(0)); + f.extend(w.finish(Some(0))); + assert!(open(&f).unwrap().streams[0].saw_eos); + // A flushed last packet cannot be trimmed: the empty end page keeps its granule. + let mut w = PageWriter::new(1); + let mut f = w.header(&opus_head(312)); + f.extend(w.header(&opus_tags("t", &[]))); + f.extend(w.packet(&[0xFC; 40], 960)); + f.extend(w.flush()); + f.extend(w.finish(Some(500))); + let s = &open(&f).unwrap().streams[0]; + assert!(s.saw_eos); + assert_eq!(s.last_granule, Some(960)); +} + +#[test] +fn vorbis_comment_has_the_framing_bit() { + let c = vorbis_comment("FilmCraft", &[("ARTIST", "x")]); + assert_eq!(&c[..7], b"\x03vorbis"); + assert_eq!(*c.last().unwrap(), 1); + assert_eq!(u32::from_le_bytes(c[7..11].try_into().unwrap()), 9); +} + +/// Packets on and around the lacing boundaries (multiples of 255, where a packet ends with a +/// zero lacing value) and an empty packet read back exactly. +#[test] +fn lacing_boundaries_read_back() { + let sizes = [0usize, 1, 254, 255, 256, 509, 510, 511, 765, 65_024, 65_025, 65_026]; + let mut w = PageWriter::new(3); + let mut f = w.header(&opus_head(0)); + f.extend(w.header(&opus_tags("v", &[]))); + let packets: Vec> = sizes.iter().enumerate().map(|(i, &n)| (0..n).map(|k| (i * 7 + k) as u8).collect()).collect(); + for (i, p) in packets.iter().enumerate() { + f.extend(w.packet(p, (i as i64 + 1) * 960)); + } + f.extend(w.finish(None)); + let ogg = open(&f).unwrap(); + assert!(ogg.warnings.is_empty(), "{:?}", ogg.warnings); + assert_eq!(ogg.streams[0].packets.len(), packets.len()); + for (k, p) in packets.iter().enumerate() { + assert_eq!(ogg.read_packet(&f, 0, k).unwrap(), *p, "packet {k} of {} bytes", p.len()); + } +} + +/// A page exactly full of lacing values when a packet starts: the packet begins on the next page, +/// which must not be flagged as continuing one (the reader would drop its first packet). Review +/// of #4481; reachable with page targets larger than the defaults. +#[test] +fn a_packet_after_an_exactly_full_page_is_not_a_continuation() { + let mut w = PageWriter::new(5).with_page_target(1 << 20, 1000); + let mut f = w.header(&opus_head(0)); + f.extend(w.header(&opus_tags("v", &[]))); + let packets: Vec> = (0..300).map(|i| opus_packet(i, 1)).collect(); + for (i, p) in packets.iter().enumerate() { + f.extend(w.packet(p, (i as i64 + 1) * 960)); + } + f.extend(w.finish(None)); + let ogg = open(&f).unwrap(); + assert!(ogg.warnings.is_empty(), "{:?}", ogg.warnings); + assert_eq!(ogg.streams[0].packets.len(), 300, "every packet is read back"); + for (k, p) in packets.iter().enumerate() { + assert_eq!(&ogg.read_packet(&f, 0, k).unwrap(), p, "packet {k}"); + } + // the second audio page starts with packet 255 and is not a continuation + let mut pos = 0; + let mut flags = Vec::new(); + while pos + 27 <= f.len() { + let nseg = f[pos + 26] as usize; + flags.push(f[pos + 5]); + pos += 27 + nseg + f[pos + 27..pos + 27 + nseg].iter().map(|&l| l as usize).sum::(); + } + assert!(flags.iter().all(|&fl| fl & 0x01 == 0), "no page continues a packet: {flags:?}"); +} From 59b4f1e8c2909fbcacba491b219958f4a976d5f8 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Sun, 15 Nov 2026 23:25:25 +0100 Subject: [PATCH 148/150] image-codecs: decode AVIF pixels with the pure-Rust AV1 decoder (#778) (#1601) * image-codecs: decode AVIF pixels with craft-video's pure-Rust AV1 decoder New optional `avif-decode` feature (native targets) reads still AVIF files: the HEIF/ISOBMFF item graph, grids, clap/irot/imir, alpha auxiliary images (including premultiplied), 8/10/12-bit 4:0:0, 4:2:0, 4:2:2 and 4:4:4, nclx/ICC colour, Exif and XMP, and PQ/HLG to linear F32 through cicp::linearize as the HEIF path does. The container walk follows LightCraft #3017 by @emmertex. Every size is checked against the caller's Limits before the AV1 decoder runs, the decoder's memory budget is capped by max_alloc, and the decode is wrapped in catch_unwind so a decoder panic becomes an error. Fixtures are tiny ffmpeg/libaom files; the 4:2:0 planes match libdav1d bit for bit. Adds a decode_avif fuzz target. Part of storytold/craft-libs#778. * image-codecs, video: address review on the AVIF decoder - clap: width and height are unsigned fractions, the offsets have signed numerators, so 0x80000000/0x40000000 is a width of 2, not -2. - An item with clap, irot or imir more than once (repeated ipma records) is refused before any canvas work, instead of rotating 261120 times. - Matrix coefficients 10 (BT.2020 constant luminance) is refused rather than converted with the non-constant-luminance equations. - An alpha plane whose own transforms differ from the colour's is refused rather than composed in the wrong place; none, or the colour's, still decode. - craft-video: Decoder::set_shown_budget lowers the per-chunk byte cap on shown pictures (never above 1 GiB), and the AVIF decoder sets it to max_alloc, so a payload of many shown pictures is refused before they are built. --- crates/image-codecs/Cargo.toml | 4 + crates/image-codecs/README.md | 27 +- crates/image-codecs/fuzz/Cargo.toml | 9 +- .../fuzz/fuzz_targets/decode_avif.rs | 14 + crates/image-codecs/src/cicp.rs | 2 +- crates/image-codecs/src/codecs.rs | 2 + crates/image-codecs/src/codecs/avif_decode.rs | 1350 +++++++++++++++++ crates/image-codecs/src/format.rs | 13 +- crates/image-codecs/src/lib.rs | 24 +- .../tests/fixtures/avif/README.md | 12 + .../tests/fixtures/avif/quad-420-bt709.avif | Bin 0 -> 369 bytes .../fixtures/avif/quad-420-bt709.dav1d.yuv | 1 + .../fixtures/avif/quad-444-gbr-lossless.avif | Bin 0 -> 345 bytes .../tests/fixtures/avif/quad-source.rgb | 1 + crates/image-codecs/tests/it/detect_caps.rs | 11 +- crates/video/src/av1/decode/decoder.rs | 57 +- 16 files changed, 1488 insertions(+), 39 deletions(-) create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_avif.rs create mode 100644 crates/image-codecs/src/codecs/avif_decode.rs create mode 100644 crates/image-codecs/tests/fixtures/avif/README.md create mode 100644 crates/image-codecs/tests/fixtures/avif/quad-420-bt709.avif create mode 100644 crates/image-codecs/tests/fixtures/avif/quad-420-bt709.dav1d.yuv create mode 100644 crates/image-codecs/tests/fixtures/avif/quad-444-gbr-lossless.avif create mode 100644 crates/image-codecs/tests/fixtures/avif/quad-source.rgb diff --git a/crates/image-codecs/Cargo.toml b/crates/image-codecs/Cargo.toml index a8872c0d3..208ff3ce7 100644 --- a/crates/image-codecs/Cargo.toml +++ b/crates/image-codecs/Cargo.toml @@ -15,6 +15,9 @@ corpus = [] default = [] avif = ["image/avif", "dep:ravif", "dep:rav1e", "dep:avif-serialize"] heif = ["dep:craft-heif"] +# AVIF decode: craft-video's clean-room pure-Rust AV1 decoder. Native targets only (on wasm32 a +# decoder panic aborts instead of becoming an error), so wasm32 builds leave AVIF unreadable. +avif-decode = ["dep:craft-video", "craft-video/av1-decode"] [dependencies] crc32fast = "1.5" @@ -41,6 +44,7 @@ hayro-jpeg2000 = { version = "=0.3.5", default-features = false, features = ["st [target.'cfg(not(target_arch = "wasm32"))'.dependencies] rayon = { workspace = true } +craft-video = { workspace = true, optional = true } exr = { version = "1.73", default-features = false, features = ["rayon"] } [dev-dependencies] diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index aa0027666..a0e1afaa4 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -1,9 +1,10 @@ # craft-image-codecs Codecs for flat raster image formats, written in pure Rust. This crate defines its own `Image` -type and shares its baseline and extended JPEG decoders through `craft-jpeg`, and its optional -HEIF decoder through `craft-heif`. It builds for `wasm32-unknown-unknown` and does no filesystem -I/O: decoders take `&[u8]` and encoders return `Vec`. +type and shares its baseline and extended JPEG decoders through `craft-jpeg`, its optional HEIF +decoder through `craft-heif`, and its optional AVIF decoder through `craft-video`'s AV1 decoder. +It builds for `wasm32-unknown-unknown` and does no filesystem I/O: decoders take `&[u8]` and +encoders return `Vec`. It follows the "Avoiding GIMP's hole" rules in `docs/architecture.md` §1.1: @@ -57,7 +58,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | DICOM | yes | **no** | U8, U16 | Gray, RGB | no | no | no | no | no | no | n/a | built in | | Amiga IFF (ILBM/PBM) | yes | yes (24-bit ILBM) | U8 | RGB, RGBA | yes (1-bit mask plane on write) | no | no | no | no | no | no | built in | | JPEG-LS (`.jls`) | yes (any NEAR; one scan per component) | yes (lossless) | U8, U16 | Gray, RGB | no | no | no | no | no | no | no | built in | -| AVIF | **no** | only with feature `avif` | U8 | RGB, RGBA | yes | no | no | no | no | no | yes | `image` + `ravif` | +| AVIF | with feature `avif-decode` (native only) | only with feature `avif` | U8, U16 (10/12-bit decodes to U16; PQ/HLG to linear F32) | RGB, RGBA | yes (auxiliary alpha) | read (`colr` prof/rICC) | read | read | no | no | yes | `craft-video` AV1 decoder; `image` + `ravif` to write | | HEIF/HEIC | with feature `heif` | **no** | U8, U16 (10/12-bit decodes to U16) | RGB, RGBA | yes (auxiliary alpha) | yes (`colr` prof) | yes | yes | no | no | n/a | `heic-rs` | "Native" means the data is stored and read back without conversion. Anything else is converted by @@ -178,10 +179,20 @@ embedded table (and a PNG's transparent entry) so `photocraft-io` can open them decodes to RGB. Image sequences, overlays and identity derivations, multilayer HEVC and some 4:2:2/4:4:4 streams return `CodecError::Unsupported` or `Malformed`, never wrong pixels. Writing needs an HEVC encoder and every mature one is C, so HEIF is listed in `ASYMMETRIC_EXCEPTIONS`. -* **AVIF.** Encoding uses `ravif`, which is pure Rust. Decoding - needs `dav1d`, which is C. AVIF is therefore read-unsupported, and write support is gated - behind the non-default `avif` feature. In a default build it is neither readable nor writable, - so the symmetric guarantee holds. It is listed in `ASYMMETRIC_EXCEPTIONS`. +* **AVIF.** Encoding uses `ravif`, which is pure Rust, behind the non-default `avif` feature. + Decoding (`codecs::avif_decode`, behind the non-default `avif-decode` feature) reads the HEIF + boxes itself and decodes the AV1 pictures with `craft-video`'s clean-room pure-Rust AV1 decoder: + one `av01` image or a `grid`, 8/10/12-bit, 4:2:0/4:2:2/4:4:4/monochrome, the `nclx` colour (or + the sequence header's) and an ICC profile, an `auxl` alpha plane (`prem` too), the Exif and XMP + items, and `clap`/`irot`/`imir`, which are applied unless `keep_orientation` is set, as for HEIF. + PQ and HLG open as linear light in F32, as HEIF does. Image sequences (animated AVIF), depth and + gain-map auxiliary images and layered progressive images aren't read. It is bounded like the + other decoders: the `Limits` are checked against the declared size before any picture decodes, + against every picture and every output buffer, `max_alloc` caps the AV1 decoder's picture store, + box, item and extent counts are capped, item data can't exceed the file, and a decoder panic is + `CodecError::Malformed`. wasm32 builds leave AVIF unreadable, since a panic there aborts. In a + default build AVIF is neither readable nor writable, so the symmetric guarantee holds; a build + with only one of the two features is listed in `ASYMMETRIC_EXCEPTIONS`. * **Lossy WebP.** Lossless (VP8L, via `image-webp`) is the default. `webp_lossless: false` writes a lossy file with our own clean-room VP8 key-frame encoder (`codecs::vp8`, from RFC 6386; `webp_quality` 0–100 on libwebp's scale sets the quantizer). Alpha travels losslessly diff --git a/crates/image-codecs/fuzz/Cargo.toml b/crates/image-codecs/fuzz/Cargo.toml index 3257a3953..30226c76e 100644 --- a/crates/image-codecs/fuzz/Cargo.toml +++ b/crates/image-codecs/fuzz/Cargo.toml @@ -14,7 +14,7 @@ cargo-fuzz = true [dependencies] libfuzzer-sys = "0.4" -craft-image-codecs = { path = "..", features = ["heif"] } +craft-image-codecs = { path = "..", features = ["heif", "avif-decode"] } # Called directly too: the codec turns its panics into errors (see decode_heif.rs). heic-rs = "=0.1.4" @@ -76,3 +76,10 @@ path = "fuzz_targets/decode_webp.rs" test = false doc = false bench = false + +[[bin]] +name = "decode_avif" +path = "fuzz_targets/decode_avif.rs" +test = false +doc = false +bench = false diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_avif.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_avif.rs new file mode 100644 index 000000000..9a4b8978d --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_avif.rs @@ -0,0 +1,14 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, probe_size, DecodeOptions, Format, Limits}; + +// The AVIF box reader and craft-video's AV1 decoder parse untrusted files: run with `-timeout=10` +// so a hang is reported, not just a panic. The codec turns an AV1 decoder panic into an error, so +// only the box reader's own panics would reach the fuzzer. Both orientation paths are covered. +fuzz_target!(|data: &[u8]| { + let keep_orientation = data.first().is_some_and(|b| b & 1 == 1); + let limits = Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }; + let _ = probe_size(data); + let _ = decode_as_with(Format::Avif, data, &DecodeOptions { limits, keep_orientation }); +}); diff --git a/crates/image-codecs/src/cicp.rs b/crates/image-codecs/src/cicp.rs index 290c82bc3..e6911d1b0 100644 --- a/crates/image-codecs/src/cicp.rs +++ b/crates/image-codecs/src/cicp.rs @@ -99,7 +99,7 @@ pub fn linear_to_pq(linear: f32) -> f32 { /// Relative luminance weights of the RGB primaries `code` (BT.2020's for anything else, the /// space BT.2100 defines HLG in). -fn luma_weights(code: u16) -> [f32; 3] { +pub(crate) fn luma_weights(code: u16) -> [f32; 3] { match code { 1 => [0.2126, 0.7152, 0.0722], 12 => [0.2290, 0.6917, 0.0793], diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs index af91aa536..00b94b7ec 100644 --- a/crates/image-codecs/src/codecs.rs +++ b/crates/image-codecs/src/codecs.rs @@ -1,4 +1,6 @@ pub(crate) mod avif; +#[cfg(all(feature = "avif-decode", not(target_arch = "wasm32")))] +pub(crate) mod avif_decode; pub(crate) mod dds; pub(crate) mod dds_bc; pub(crate) mod deep_exr; diff --git a/crates/image-codecs/src/codecs/avif_decode.rs b/crates/image-codecs/src/codecs/avif_decode.rs new file mode 100644 index 000000000..382f6a474 --- /dev/null +++ b/crates/image-codecs/src/codecs/avif_decode.rs @@ -0,0 +1,1350 @@ +//! AVIF (AV1 Image File Format) decoding, behind the non-default `avif-decode` feature, native +//! targets only. +//! +//! The container is HEIF (ISO/IEC 23008-12) over ISO-BMFF. This module reads the boxes itself +//! (`meta`, `pitm`, `iloc`, `iinf`, `iref`, `iprp`/`ipco`/`ipma`, `idat`), and `craft-video`'s +//! clean-room AV1 decoder (`av1-decode`, pure Rust, no `unsafe`) decodes the AV1 still pictures. +//! The container reader, colour conversion and transforms are adapted from @emmertex's LightCraft +//! AVIF importer (storytold/lightcraft#3017). +//! +//! Supported: the primary item as one `av01` image or a `grid` of them; 8-, 10- and 12-bit; +//! 4:2:0, 4:2:2, 4:4:4 and monochrome; full or limited range; the `nclx` colour description (or the +//! AV1 sequence header's) and an ICC profile (`prof` / `rICC`); an alpha auxiliary image (`auxl`, +//! premultiplied with `prem` too); the primary item's Exif and XMP items. `clap`, `irot` and `imir` +//! are applied unless the caller keeps the stored orientation, as for HEIF. PQ and HLG files +//! open as linear light in 32-bit float, as HEIF does. Not read: image sequences (animated AVIF), +//! depth and gain-map auxiliary images, layered (`lsel`/`a1lx`) progressive images. +//! +//! Bounded for hostile input: box nesting, item, property and extent counts are capped; item data +//! can't add up to more than the file; the [`Limits`] are checked against the declared size +//! before any picture is decoded, against every decoded picture and against each output buffer; +//! the AV1 decoder's picture store and the pictures one chunk may show are capped by `max_alloc` +//! too; repeated transforms are refused; and a panic in the decoder is +//! a malformed-file error. wasm32 builds leave AVIF unreadable: a panic there aborts instead of +//! unwinding. +//! +//! Sources: ISO/IEC 23008-12 (HEIF) and the AV1 Image File Format specification v1.1.0 (box +//! layouts), ITU-T H.273 (colour code points). + +use std::panic::{AssertUnwindSafe, catch_unwind}; + +use craft_video::av1::decode as av1; + +use crate::Format; +use crate::cicp::Cicp; +use crate::error::CodecError; +use crate::image::{ChannelLayout, Image, Metadata, SampleType}; +use crate::options::Limits; + +const F: Format = Format::Avif; +/// Deepest box nesting followed (meta → iprp → ipco → property is 4). +const MAX_DEPTH: u32 = 8; +/// Most boxes, items, properties, associations or extents accepted: far above real files. +const MAX_ITEMS: usize = 1 << 16; +/// The AV1 decoder's own picture-store cap (`craft-video`'s default on 64-bit targets). +const AV1_MEMORY_BUDGET: u64 = 4 << 30; +/// The `auxC` types of an alpha plane (AVIF and the older HEVC-in-HEIF spelling). +const ALPHA_URNS: [&[u8]; 2] = [b"urn:mpeg:mpegB:cicp:systems:auxiliary:alpha", b"urn:mpeg:hevc:2015:auxid:1"]; + +type Result = std::result::Result; + +fn bad(m: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, m) +} + +/// A big-endian reader over a byte slice that never panics. +struct Rd<'a> { + b: &'a [u8], + p: usize, +} + +impl<'a> Rd<'a> { + fn new(b: &'a [u8]) -> Self { + Rd { b, p: 0 } + } + fn take(&mut self, n: usize) -> Result<&'a [u8]> { + let end = self.p.checked_add(n).ok_or_else(|| bad("box overflows"))?; + let s = self.b.get(self.p..end).ok_or_else(|| bad("box truncated"))?; + self.p = end; + Ok(s) + } + fn u8(&mut self) -> Result { + Ok(self.take(1)?.first().copied().unwrap_or(0)) + } + fn u16(&mut self) -> Result { + Ok(self.uint(2)? as u16) + } + fn u32(&mut self) -> Result { + Ok(self.uint(4)? as u32) + } + /// An unsigned big-endian number of `n` (0 to 8) bytes. + fn uint(&mut self, n: usize) -> Result { + if n > 8 { + return Err(bad("field too wide")); + } + Ok(self.take(n)?.iter().fold(0u64, |a, &x| (a << 8) | u64::from(x))) + } + /// A full box's version, its flags skipped. + fn version(&mut self) -> Result { + let v = self.u8()?; + self.take(3)?; + Ok(v) + } + /// An item ID: 16 bits in version 0 boxes, 32 bits otherwise. + fn id(&mut self, wide: bool) -> Result { + if wide { self.u32() } else { self.u16().map(u32::from) } + } + fn cstr(&mut self) -> Result<&'a [u8]> { + let rest = self.rest(); + let n = rest.iter().position(|&c| c == 0).unwrap_or(rest.len()); + let s = self.take(n)?; + if self.p < self.b.len() { + self.p += 1; + } + Ok(s) + } + fn rest(&self) -> &'a [u8] { + self.b.get(self.p..).unwrap_or(&[]) + } +} + +/// The child boxes of `b`: (type, payload). +fn boxes(b: &[u8]) -> Result> { + let mut out = Vec::new(); + let mut p = 0usize; + while p < b.len() { + let mut r = Rd::new(b.get(p..).unwrap_or(&[])); + if r.b.len() < 8 { + break; // trailing padding + } + let size32 = r.u32()?; + let t = r.take(4)?; + let ty = [t[0], t[1], t[2], t[3]]; + let (hdr, size) = match size32 { + 0 => (8usize, r.b.len() as u64), + 1 => (16, r.uint(8)?), + s => (8, u64::from(s)), + }; + let size = usize::try_from(size).map_err(|_| bad("box too large"))?; + if size < hdr || size > r.b.len() { + return Err(bad(format!("box {} has a bad size", String::from_utf8_lossy(&ty)))); + } + out.push((ty, r.b.get(hdr..size).unwrap_or(&[]))); + if out.len() > MAX_ITEMS { + return Err(bad("too many boxes")); + } + p = p.checked_add(size).ok_or_else(|| bad("box overflows"))?; + } + Ok(out) +} + +#[derive(Clone, Debug, Default)] +struct Extent { + offset: u64, + length: u64, +} + +#[derive(Clone, Debug, Default)] +struct Loc { + method: u8, + base: u64, + extents: Vec, +} + +#[derive(Clone, Copy, Debug, PartialEq)] +enum Transform { + /// Clean aperture: width, height, horizontal and vertical offset of the centre. + Clap { w: f64, h: f64, dx: f64, dy: f64 }, + /// Anticlockwise rotation by `n` × 90°. + Rot(u8), + /// Mirror, as HEIF amendment 2, libheif and libavif read it: 0 = top and bottom exchanged, + /// 1 = left and right exchanged. + Mir(u8), +} + +#[derive(Clone, Debug)] +enum Prop { + Av1c(Vec), + Nclx(Cicp), + Icc(Vec), + Ispe(u32, u32), + AuxC(Vec), + T(Transform), + Other, +} + +struct Item { + id: u32, + kind: [u8; 4], + /// `mime` items: the content type. + content_type: Option>, +} + +#[derive(Default)] +struct Container<'a> { + file: &'a [u8], + primary: u32, + items: Vec, + locs: Vec<(u32, Loc)>, + idat: &'a [u8], + props: Vec, + /// item → 1-based property indices, in order. + assoc: Vec<(u32, Vec)>, + /// (type, from, to…) + refs: Vec<([u8; 4], u32, Vec)>, + /// `iloc` extents read so far, across all items (capped at [`MAX_ITEMS`]). + extents: usize, +} + +fn parse(file: &[u8]) -> Result> { + let top = boxes(file)?; + let meta = top.iter().find(|(t, _)| t == b"meta").map(|(_, b)| *b).ok_or_else(|| { + if top.iter().any(|(t, _)| t == b"moov") { + CodecError::unsupported(F, "AVIF image sequences (animated AVIF) aren't read") + } else { + bad("no meta box") + } + })?; + let mut c = Container { file, ..Default::default() }; + let meta = meta.get(4..).ok_or_else(|| bad("meta box truncated"))?; + for (t, b) in boxes(meta)? { + let mut r = Rd::new(b); + match &t { + b"pitm" => { + let v = r.version()?; + c.primary = r.id(v != 0)?; + } + b"iloc" => parse_iloc(&mut r, &mut c)?, + b"iinf" => parse_iinf(&mut r, &mut c)?, + b"iref" => parse_iref(&mut r, &mut c)?, + b"idat" => c.idat = b, + b"iprp" => parse_iprp(b, &mut c, 0)?, + _ => {} + } + } + if c.primary == 0 { + return Err(bad("no primary item")); + } + Ok(c) +} + +fn parse_iinf(r: &mut Rd, c: &mut Container) -> Result<()> { + let v = r.version()?; + let _count = r.id(v != 0)?; + for (t, b) in boxes(r.rest())? { + if &t != b"infe" { + continue; + } + let mut r = Rd::new(b); + let v = r.version()?; + if v < 2 { + continue; + } + let id = r.id(v != 2)?; + r.u16()?; // protection index + let kind = r.take(4)?; + let kind = [kind[0], kind[1], kind[2], kind[3]]; + let _name = r.cstr()?; + let content_type = (&kind == b"mime").then(|| r.cstr().ok().map(<[u8]>::to_vec)).flatten(); + c.items.push(Item { id, kind, content_type }); + if c.items.len() > MAX_ITEMS { + return Err(bad("too many items")); + } + } + Ok(()) +} + +fn parse_iref(r: &mut Rd, c: &mut Container) -> Result<()> { + let v = r.version()?; + for (t, b) in boxes(r.rest())? { + let mut r = Rd::new(b); + let from = r.id(v != 0)?; + let n = r.u16()?; + let mut to = Vec::with_capacity(usize::from(n).min(r.rest().len() / 2 + 1)); + for _ in 0..n { + to.push(r.id(v != 0)?); + } + c.refs.push((t, from, to)); + if c.refs.len() > MAX_ITEMS { + return Err(bad("too many item references")); + } + } + Ok(()) +} + +fn parse_iloc(r: &mut Rd, c: &mut Container) -> Result<()> { + let v = r.version()?; + if v > 2 { + return Err(CodecError::unsupported(F, "iloc version")); + } + let a = r.u8()?; + let b = r.u8()?; + let (off_size, len_size, base_size) = (usize::from(a >> 4), usize::from(a & 15), usize::from(b >> 4)); + let index_size = if v >= 1 { usize::from(b & 15) } else { 0 }; + let count = if v < 2 { u32::from(r.u16()?) } else { r.u32()? }; + for _ in 0..count { + let id = r.id(v >= 2)?; + let method = if v >= 1 { (r.u16()? & 15) as u8 } else { 0 }; + r.u16()?; // data reference index + let base = r.uint(base_size)?; + let n = r.u16()?; + let mut extents = Vec::with_capacity(usize::from(n).min(64)); + for _ in 0..n { + r.uint(index_size)?; + let offset = r.uint(off_size)?; + let length = r.uint(len_size)?; + extents.push(Extent { offset, length }); + // Zero-width offset and length fields read no bytes, so the file size alone doesn't + // bound this. + c.extents += 1; + if c.extents > MAX_ITEMS { + return Err(bad("too many item extents")); + } + } + c.locs.push((id, Loc { method, base, extents })); + if c.locs.len() > MAX_ITEMS { + return Err(bad("too many items")); + } + } + Ok(()) +} + +fn parse_iprp(b: &[u8], c: &mut Container, depth: u32) -> Result<()> { + if depth > MAX_DEPTH { + return Err(bad("boxes nested too deep")); + } + for (t, b) in boxes(b)? { + match &t { + b"ipco" => { + for (t, b) in boxes(b)? { + c.props.push(parse_prop(&t, b)?); + if c.props.len() > MAX_ITEMS { + return Err(bad("too many properties")); + } + } + } + b"ipma" => parse_ipma(b, c)?, + _ => {} + } + } + Ok(()) +} + +fn parse_ipma(b: &[u8], c: &mut Container) -> Result<()> { + let mut r = Rd::new(b); + let v = r.u8()?; + let flags = r.take(3)?; + let wide = flags.get(2).is_some_and(|f| f & 1 != 0); + let n = r.u32()?; + for _ in 0..n { + let id = r.id(v != 0)?; + let k = r.u8()?; + let mut list = Vec::with_capacity(usize::from(k)); + for _ in 0..k { + let idx = if wide { usize::from(r.u16()? & 0x7fff) } else { usize::from(r.u8()? & 0x7f) }; + list.push(idx); + } + c.assoc.push((id, list)); + if c.assoc.len() > MAX_ITEMS { + return Err(bad("too many property associations")); + } + } + Ok(()) +} + +fn parse_prop(t: &[u8; 4], b: &[u8]) -> Result { + let mut r = Rd::new(b); + Ok(match t { + b"av1C" => Prop::Av1c(b.get(4..).unwrap_or(&[]).to_vec()), + b"colr" => match r.take(4)? { + b"nclx" => Prop::Nclx(Cicp { primaries: r.u16()?, transfer: r.u16()?, matrix: r.u16()?, full_range: r.u8()? & 0x80 != 0 }), + b"prof" | b"rICC" => Prop::Icc(r.rest().to_vec()), + _ => Prop::Other, + }, + b"ispe" => { + r.version()?; + Prop::Ispe(r.u32()?, r.u32()?) + } + b"auxC" => { + r.version()?; + Prop::AuxC(r.cstr()?.to_vec()) + } + b"irot" => Prop::T(Transform::Rot(r.u8()? & 3)), + b"imir" => Prop::T(Transform::Mir(r.u8()? & 1)), + b"clap" => { + // Width and height are unsigned fractions; the centre offsets have signed numerators + // (and unsigned denominators), as libavif and libheif read them. + let mut q = |signed: bool| -> Result { + let raw = r.u32()?; + let n = if signed { f64::from(raw as i32) } else { f64::from(raw) }; + let d = r.u32()?; + if d == 0 { + return Err(bad("clap with a zero denominator")); + } + Ok(n / f64::from(d)) + }; + let (w, h, dx, dy) = (q(false)?, q(false)?, q(true)?, q(true)?); + Prop::T(Transform::Clap { w, h, dx, dy }) + } + _ => Prop::Other, + }) +} + +impl Container<'_> { + fn item_type(&self, id: u32) -> Option<[u8; 4]> { + self.items.iter().find(|t| t.id == id).map(|t| t.kind) + } + fn props_of(&self, id: u32) -> impl Iterator { + self.assoc.iter().filter(move |a| a.0 == id).flat_map(|a| a.1.iter()).filter_map(|&i| i.checked_sub(1).and_then(|i| self.props.get(i))) + } + fn data(&self, id: u32) -> Result> { + let loc = self.locs.iter().find(|l| l.0 == id).map(|l| &l.1).ok_or_else(|| bad(format!("item {id} has no location")))?; + let src = match loc.method { + 0 => self.file, + 1 => self.idat, + _ => return Err(CodecError::unsupported(F, "item data by reference to another item")), + }; + let mut out = Vec::new(); + for e in &loc.extents { + let start = loc.base.checked_add(e.offset).ok_or_else(|| bad("item offset overflows"))?; + let start = usize::try_from(start).map_err(|_| bad("item offset out of range"))?; + let end = if e.length == 0 { + src.len() + } else { + usize::try_from(e.length).ok().and_then(|l| start.checked_add(l)).ok_or_else(|| bad("item length overflows"))? + }; + let chunk = src.get(start..end).ok_or_else(|| bad("item data runs past the end of the file"))?; + // Every byte comes from the file, so a real item is never larger than it; repeated or + // overlapping extents that add up to more are refused before they allocate. + if out.len().saturating_add(chunk.len()) > self.file.len() { + return Err(bad("item data is larger than the file")); + } + out.extend_from_slice(chunk); + } + Ok(out) + } + fn refs_from(&self, kind: &[u8; 4], from: u32) -> Vec { + self.refs.iter().filter(|r| &r.0 == kind && r.1 == from).flat_map(|r| r.2.iter().copied()).collect() + } + /// Items with a `kind` reference to `to`. + fn refs_to(&self, kind: &[u8; 4], to: u32) -> impl Iterator { + self.refs.iter().filter(move |r| &r.0 == kind && r.2.contains(&to)).map(|r| r.1) + } + fn ispe(&self, id: u32) -> Option<(u32, u32)> { + self.props_of(id).find_map(|p| if let Prop::Ispe(w, h) = p { Some((*w, *h)) } else { None }) + } + /// The item's transformative properties in association order. HEIF allows each of `clap`, + /// `irot` and `imir` at most once per item; a file that associates one more often (repeated + /// `ipma` entries could otherwise ask for a quarter of a million full-image rotations) is + /// refused before any pixels are touched. + fn transforms(&self, id: u32) -> Result> { + // Three is the most a valid item has, so a fourth is already a repeat. + let ts: Vec = self.props_of(id).filter_map(|p| if let Prop::T(t) = p { Some(*t) } else { None }).take(4).collect(); + for (i, t) in ts.iter().enumerate() { + if ts[..i].iter().any(|u| std::mem::discriminant(u) == std::mem::discriminant(t)) { + return Err(bad("an image item has a transformative property (clap, irot or imir) more than once")); + } + } + Ok(ts) + } + fn nclx(&self, id: u32) -> Option { + self.props_of(id).find_map(|p| if let Prop::Nclx(n) = p { Some(*n) } else { None }) + } + fn icc(&self, id: u32) -> Option> { + self.props_of(id).find_map(|p| if let Prop::Icc(b) = p { Some(b.clone()) } else { None }) + } + /// The primary item's alpha plane: an auxiliary image (`auxl`) whose `auxC` says alpha. + fn alpha_item(&self) -> Option { + self.refs_to(b"auxl", self.primary).find(|&id| { + matches!(self.item_type(id), Some(t) if &t == b"av01" || &t == b"grid") + && self.props_of(id).any(|p| matches!(p, Prop::AuxC(urn) if ALPHA_URNS.contains(&urn.as_slice()))) + }) + } + /// The primary item's colour is premultiplied by its alpha (`prem` from colour to alpha). + fn premultiplied(&self, alpha: u32) -> bool { + self.refs_from(b"prem", self.primary).contains(&alpha) + } + /// The first item of a metadata kind that describes (`cdsc`) the primary item. + fn metadata_item(&self, is: impl Fn(&Item) -> bool) -> Option { + let described: Vec = self.refs_to(b"cdsc", self.primary).collect(); + self.items.iter().filter(|i| is(i)).map(|i| i.id).find(|id| described.contains(id)) + } +} + +/// The size of an image item (`av01` or `grid`) before its transforms. +fn item_size(c: &Container, id: u32) -> Result<(u32, u32)> { + let kind = c.item_type(id).ok_or_else(|| bad("an image item is not described"))?; + let (w, h) = match (c.ispe(id), &kind) { + (_, b"grid") => grid_header(&c.data(id)?)?.2, + (Some(s), b"av01") => s, + (None, b"av01") => return Err(bad("an AV1 image item has no size (ispe)")), + _ => return Err(CodecError::unsupported(F, "the image item is not an AV1 image or a grid")), + }; + if w == 0 || h == 0 { + return Err(bad("zero dimension")); + } + Ok((w, h)) +} + +/// The size after `clap` / `irot`, from a stored size. +fn displayed(mut w: u32, mut h: u32, ts: &[Transform]) -> (u32, u32) { + for t in ts { + match *t { + Transform::Clap { w: cw, h: ch, .. } if cw >= 1.0 && ch >= 1.0 && cw.is_finite() && ch.is_finite() => { + w = (cw.round() as u32).clamp(1, w); + h = (ch.round() as u32).clamp(1, h); + } + Transform::Rot(n) if n % 2 == 1 => std::mem::swap(&mut w, &mut h), + _ => {} + } + } + (w, h) +} + +/// (rows, columns, output size) of a `grid` item. +fn grid_header(d: &[u8]) -> Result<(u32, u32, (u32, u32))> { + let mut r = Rd::new(d); + r.u8()?; // version + let flags = r.u8()?; + let rows = u32::from(r.u8()?) + 1; + let cols = u32::from(r.u8()?) + 1; + let n = if flags & 1 != 0 { 4 } else { 2 }; + let w = r.uint(n)? as u32; + let h = r.uint(n)? as u32; + Ok((rows, cols, (w, h))) +} + +/// The upright size [`decode`] returns (`keep_orientation` off), from the boxes alone. +pub(crate) fn probe(bytes: &[u8], limits: &Limits) -> Result<(u32, u32)> { + let c = parse(bytes)?; + let (w, h) = item_size(&c, c.primary)?; + limits.check_size(w, h)?; + Ok(displayed(w, h, &c.transforms(c.primary)?)) +} + +/// One decoded AV1 picture. +struct Pic { + w: usize, + h: usize, + bits: u8, + ssx: usize, + ssy: usize, + mono: bool, + planes: [Vec; 3], + cicp: Cicp, +} + +fn av1_error(e: av1::Error) -> CodecError { + match e { + av1::Error::Unsupported(w) => CodecError::unsupported(F, format!("AV1: {w}")), + other => bad(format!("AV1: {other}")), + } +} + +/// Decodes the `av01` item `id`, refusing pictures over `limits`. +fn decode_av1(c: &Container, id: u32, limits: &Limits) -> Result { + if c.item_type(id) != Some(*b"av01") { + return Err(CodecError::unsupported(F, "an image tile is not an AV1 image")); + } + // A tile's declared size counts before its picture is decoded too. + if let Some((w, h)) = c.ispe(id) { + limits.check(w, h, ChannelLayout::Rgb, SampleType::U16)?; + } + let data = c.data(id)?; + let config = c.props_of(id).find_map(|p| if let Prop::Av1c(o) = p { Some(o.clone()) } else { None }); + let mut dec = av1::Decoder::new(); + dec.set_memory_budget(limits.max_alloc.min(AV1_MEMORY_BUDGET)); + // Only the first picture is kept, so the pictures a chunk may show are held to the same budget + // before they are built (the decoder allows up to 1 GiB of them otherwise). + dec.set_shown_budget(limits.max_alloc); + let mut pics = catch_unwind(AssertUnwindSafe(|| -> std::result::Result, av1::Error> { + let mut pics = Vec::new(); + // The sequence header normally travels in the item too; the av1C copy covers files + // where it doesn't. + if let Some(cfg) = config.filter(|o| !o.is_empty()) { + pics.extend(dec.decode(&cfg)?); + } + pics.extend(dec.decode(&data)?); + pics.extend(dec.flush_result()?); + Ok(pics) + })) + .map_err(|_| bad("the AV1 decoder failed on this file"))? + .map_err(av1_error)?; + if pics.is_empty() { + return Err(bad("the AV1 data holds no picture")); + } + let p = pics.swap_remove(0); + limits.check(p.width, p.height, ChannelLayout::Rgb, SampleType::U16)?; + let (w, h) = (p.width as usize, p.height as usize); + let (ssx, ssy) = (usize::from(p.subsampling_x.min(1)), usize::from(p.subsampling_y.min(1))); + let need = w.checked_mul(h).ok_or_else(|| bad("picture too large"))?; + let cneed = ((w + ssx) >> ssx) * ((h + ssy) >> ssy); + if need == 0 || p.planes[0].len() < need || (!p.mono_chrome && (p.planes[1].len() < cneed || p.planes[2].len() < cneed)) { + return Err(bad("AV1 picture planes are short")); + } + let cicp = Cicp { + primaries: u16::from(p.color_primaries), + transfer: u16::from(p.transfer_characteristics), + matrix: u16::from(p.matrix_coefficients), + full_range: p.full_range, + }; + Ok(Pic { w, h, bits: p.bit_depth.clamp(8, 16), ssx, ssy, mono: p.mono_chrome, planes: p.planes, cicp }) +} + +/// Kr, Kb of an H.273 matrix code point; `None` = identity (GBR). +fn coefficients(matrix: u16, primaries: u16) -> Result> { + Ok(match matrix { + 0 => None, + 1 => Some((0.2126, 0.0722)), + 4 => Some((0.30, 0.11)), + 7 => Some((0.212, 0.087)), + // 9 is BT.2020 non-constant luminance; 10 (constant luminance) needs other equations and + // is refused below. + 9 => Some((0.2627, 0.0593)), + // Chromaticity-derived non-constant luminance. + 12 => { + let [kr, _, kb] = crate::cicp::luma_weights(primaries); + Some((kr, kb)) + } + // 2 (unspecified: AVIF readers assume BT.601), 5, 6 (BT.601) and reserved values. + 2 | 5 | 6 | 3 | 15.. => Some((0.299, 0.114)), + _ => return Err(CodecError::unsupported(F, format!("matrix coefficients {matrix} (YCgCo, constant luminance or ICtCp)"))), + }) +} + +/// A chroma sample at luma position (x, y), bilinear between chroma sites centred on their +/// luma pairs. +#[inline] +fn chroma_at(plane: &[u16], cw: usize, ch: usize, ssx: usize, ssy: usize, x: usize, y: usize) -> f32 { + let pos = |v: usize, ss: usize, n: usize| -> (usize, usize, f32) { + if ss == 0 { + return (v.min(n - 1), v.min(n - 1), 0.0); + } + let f = ((v as f32 + 0.5) / 2.0 - 0.5).max(0.0); + let i = f as usize; + (i.min(n - 1), (i + 1).min(n - 1), f - i as f32) + }; + let (x0, x1, fx) = pos(x, ssx, cw); + let (y0, y1, fy) = pos(y, ssy, ch); + let s = |xx: usize, yy: usize| f32::from(plane.get(yy * cw + xx).copied().unwrap_or(0)); + let top = s(x0, y0) * (1.0 - fx) + s(x1, y0) * fx; + let bot = s(x0, y1) * (1.0 - fx) + s(x1, y1) * fx; + top * (1.0 - fy) + bot * fy +} + +/// The sample scaling of a picture: luma offset and range, chroma range and midpoint. +struct Levels { + yoff: f32, + yrange: f32, + crange: f32, + mid: f32, +} + +impl Levels { + fn new(bits: u8, full_range: bool) -> Self { + let max = ((1u32 << bits) - 1) as f32; + let scale = f32::from(1u16 << (bits - 8).min(8)); + let (yoff, yrange, crange) = if full_range { (0.0, max, max) } else { (16.0 * scale, 219.0 * scale, 224.0 * scale) }; + Levels { yoff, yrange, crange, mid: f32::from(1u16 << (bits - 1).min(15)) } + } +} + +fn to_u16(v: f32) -> u16 { + (v.clamp(0.0, 1.0) * 65535.0 + 0.5) as u16 +} + +/// Where a decoded picture goes: an interleaved 16-bit canvas of `ch` channels. +struct Canvas { + w: usize, + h: usize, + ch: usize, + data: Vec, +} + +impl Canvas { + fn new(w: u32, h: u32, layout: ChannelLayout, limits: &Limits) -> Result { + limits.check(w, h, layout, SampleType::U16)?; + let (w, h, ch) = (w as usize, h as usize, layout.channels()); + let len = w.checked_mul(h).and_then(|n| n.checked_mul(ch)).ok_or_else(|| bad("image too large"))?; + Ok(Canvas { w, h, ch, data: vec![0; len] }) + } + + /// The picture's YCbCr as non-linear R'G'B' into channels 0..3, its top-left at (x0, y0). + fn put_colour(&mut self, p: &Pic, cicp: Cicp, x0: usize, y0: usize) -> Result<()> { + let lv = Levels::new(p.bits, cicp.full_range); + let k = coefficients(cicp.matrix, cicp.primaries)?; + let (cw, chh) = ((p.w + p.ssx) >> p.ssx, (p.h + p.ssy) >> p.ssy); + for y in 0..p.h.min(self.h.saturating_sub(y0)) { + for x in 0..p.w.min(self.w.saturating_sub(x0)) { + let luma = (f32::from(p.planes[0][y * p.w + x]) - lv.yoff) / lv.yrange; + let (cb, cr) = if p.mono { + (lv.mid, lv.mid) + } else { + (chroma_at(&p.planes[1], cw, chh, p.ssx, p.ssy, x, y), chroma_at(&p.planes[2], cw, chh, p.ssx, p.ssy, x, y)) + }; + let rgb = match k { + // Identity (GBR): Y = G, Cb = B, Cr = R, all three at the luma levels. + None => [(cr - lv.yoff) / lv.yrange, luma, (cb - lv.yoff) / lv.yrange], + Some((kr, kb)) => { + let (cb, cr) = ((cb - lv.mid) / lv.crange, (cr - lv.mid) / lv.crange); + let r = luma + 2.0 * (1.0 - kr) * cr; + let b = luma + 2.0 * (1.0 - kb) * cb; + [r, (luma - kr * r - kb * b) / (1.0 - kr - kb), b] + } + }; + let o = ((y0 + y) * self.w + x0 + x) * self.ch; + for (d, v) in self.data[o..o + 3].iter_mut().zip(rgb) { + *d = to_u16(v); + } + } + } + Ok(()) + } + + /// The picture's luma as alpha into channel 3, its top-left at (x0, y0). + fn put_alpha(&mut self, p: &Pic, x0: usize, y0: usize) { + let lv = Levels::new(p.bits, p.cicp.full_range); + for y in 0..p.h.min(self.h.saturating_sub(y0)) { + for x in 0..p.w.min(self.w.saturating_sub(x0)) { + let a = (f32::from(p.planes[0][y * p.w + x]) - lv.yoff) / lv.yrange; + self.data[((y0 + y) * self.w + x0 + x) * self.ch + 3] = to_u16(a); + } + } + } +} + +/// Decodes the image item `id` (one `av01` or a `grid` of them) into `canvas`, through `put`. +/// Returns the first picture's bit depth and colour description. +fn render( + c: &Container, + id: u32, + limits: &Limits, + canvas: &mut Canvas, + put: &mut dyn FnMut(&mut Canvas, &Pic, usize, usize) -> Result<()>, +) -> Result<(u8, Cicp)> { + if c.item_type(id) != Some(*b"grid") { + let p = decode_av1(c, id, limits)?; + if (p.w, p.h) != (canvas.w, canvas.h) { + return Err(bad("the AV1 picture's size differs from its item's")); + } + put(canvas, &p, 0, 0)?; + return Ok((p.bits, p.cicp)); + } + let (rows, cols, _) = grid_header(&c.data(id)?)?; + let tiles = c.refs_from(b"dimg", id); + if tiles.is_empty() || tiles.len() as u64 != u64::from(rows) * u64::from(cols) { + return Err(bad("grid tile count doesn't match its rows and columns")); + } + let mut first: Option<(usize, usize, u8, Cicp)> = None; + for (i, &t) in tiles.iter().enumerate() { + let p = decode_av1(c, t, limits)?; + let (tw, th, ..) = *first.get_or_insert((p.w, p.h, p.bits, p.cicp)); + if (p.w, p.h) != (tw, th) { + return Err(bad("grid tiles differ in size")); + } + if tw.saturating_mul(cols as usize) < canvas.w || th.saturating_mul(rows as usize) < canvas.h { + return Err(bad("grid tiles don't cover the grid")); + } + put(canvas, &p, (i % cols as usize) * tw, (i / cols as usize) * th)?; + } + first.map(|(_, _, bits, cicp)| (bits, cicp)).ok_or_else(|| bad("grid without tiles")) +} + +/// Applies the container's transforms in order to an interleaved canvas. +fn apply_transforms(mut cv: Canvas, ts: &[Transform]) -> Canvas { + for t in ts { + cv = match *t { + Transform::Clap { w, h, dx, dy } => clap(cv, w, h, dx, dy), + Transform::Rot(n) => (0..n).fold(cv, |cv, _| rotate(cv)), + Transform::Mir(axis) => mirror(cv, axis), + }; + } + cv +} + +fn clap(cv: Canvas, cw: f64, chh: f64, dx: f64, dy: f64) -> Canvas { + if !(cw >= 1.0 && chh >= 1.0 && cw.is_finite() && chh.is_finite() && dx.is_finite() && dy.is_finite()) { + return cv; + } + let (w, h, ch) = (cv.w, cv.h, cv.ch); + let nw = (cw.round() as usize).clamp(1, w); + let nh = (chh.round() as usize).clamp(1, h); + // The centre of the aperture is the image centre plus the offset. + let x0 = ((w as f64 - 1.0) / 2.0 + dx - (nw as f64 - 1.0) / 2.0).round().clamp(0.0, (w - nw) as f64) as usize; + let y0 = ((h as f64 - 1.0) / 2.0 + dy - (nh as f64 - 1.0) / 2.0).round().clamp(0.0, (h - nh) as f64) as usize; + let mut data = Vec::with_capacity(nw * nh * ch); + for y in y0..y0 + nh { + data.extend_from_slice(&cv.data[(y * w + x0) * ch..(y * w + x0 + nw) * ch]); + } + Canvas { w: nw, h: nh, ch, data } +} + +/// 90° anticlockwise: (x, y) → (y, w - 1 - x), the new size h × w. +fn rotate(cv: Canvas) -> Canvas { + let (w, h, ch) = (cv.w, cv.h, cv.ch); + let mut data = vec![0u16; cv.data.len()]; + for y in 0..h { + for x in 0..w { + let d = ((w - 1 - x) * h + y) * ch; + let s = (y * w + x) * ch; + data[d..d + ch].copy_from_slice(&cv.data[s..s + ch]); + } + } + Canvas { w: h, h: w, ch, data } +} + +fn mirror(mut cv: Canvas, axis: u8) -> Canvas { + let (w, h, ch) = (cv.w, cv.h, cv.ch); + if axis == 0 { + for y in 0..h / 2 { + let (top, bottom) = cv.data.split_at_mut((h - 1 - y) * w * ch); + top[y * w * ch..(y + 1) * w * ch].swap_with_slice(&mut bottom[..w * ch]); + } + } else { + for row in cv.data.chunks_exact_mut(w * ch) { + for x in 0..w / 2 { + for c in 0..ch { + row.swap(x * ch + c, (w - 1 - x) * ch + c); + } + } + } + } + cv +} + +/// Straight colour from colour premultiplied by alpha. +fn unpremultiply(cv: &mut Canvas) { + for px in cv.data.as_chunks_mut::<4>().0 { + let a = u32::from(px[3]); + if a == 0 { + continue; + } + for v in &mut px[..3] { + *v = (u32::from(*v) * 65535 / a).min(65535) as u16; + } + } +} + +/// The primary item's Exif (TIFF structure, its offset prefix dropped) and XMP. +fn read_metadata(c: &Container) -> (Option>, Option) { + let exif = c.metadata_item(|i| &i.kind == b"Exif").and_then(|id| c.data(id).ok()).and_then(|d| { + let offset = usize::try_from(u32::from_be_bytes(d.get(..4)?.try_into().ok()?)).ok()?; + let tiff = d.get(4usize.checked_add(offset)?..)?; + (tiff.starts_with(b"II*\0") || tiff.starts_with(b"MM\0*")).then(|| tiff.to_vec()) + }); + let xmp = c + .metadata_item(|i| &i.kind == b"mime" && i.content_type.as_deref() == Some(b"application/rdf+xml")) + .and_then(|id| c.data(id).ok()) + .map(|d| String::from_utf8_lossy(&d).trim_end_matches('\0').to_owned()); + (exif, xmp) +} + +/// Decodes the primary image. `keep_orientation` leaves the container's `clap`, `irot` and +/// `imir` unapplied (the stored pixels), as for HEIF. +pub(crate) fn decode(bytes: &[u8], limits: &Limits, keep_orientation: bool) -> Result { + let c = parse(bytes)?; + let id = c.primary; + let (w, h) = item_size(&c, id)?; + let alpha = c.alpha_item().filter(|&a| item_size(&c, a).is_ok_and(|s| s == (w, h))); + let transforms = c.transforms(id)?; + // The alpha plane is composed in stored orientation and turned with the colour, which is right + // when it has no transforms of its own (what encoders write) or the colour's. Others would put + // transparency in the wrong place, so they are refused rather than ignored. + if let Some(a) = alpha { + let own = c.transforms(a)?; + if !own.is_empty() && own != transforms { + return Err(CodecError::unsupported(F, "an alpha plane with transforms (clap, irot, imir) that differ from the image's")); + } + } + let layout = if alpha.is_some() { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; + // The declared size, before any picture is decoded. + let mut canvas = Canvas::new(w, h, layout, limits)?; + let file_cicp = c.nclx(id); + let mut colour = |cv: &mut Canvas, p: &Pic, x: usize, y: usize| cv.put_colour(p, file_cicp.unwrap_or(p.cicp), x, y); + let (bits, stream_cicp) = render(&c, id, limits, &mut canvas, &mut colour)?; + if let Some(a) = alpha { + render(&c, a, limits, &mut canvas, &mut |cv, p, x, y| { + cv.put_alpha(p, x, y); + Ok(()) + })?; + if c.premultiplied(a) { + unpremultiply(&mut canvas); + } + } + if !keep_orientation { + canvas = apply_transforms(canvas, &transforms); + } + let (w, h) = (canvas.w as u32, canvas.h as u32); + let image = Image::from_u16(w, h, layout, &canvas.data)?; + drop(canvas); + let mut img = if bits > 8 { image } else { image.convert(layout, SampleType::U8) }; + let cicp = file_cicp.unwrap_or(stream_cicp); + img.icc = c.icc(id); + img.cicp = Some(cicp); + let (exif, xmp) = read_metadata(&c); + img.meta = Metadata { + exif: exif.map(|e| if keep_orientation { e } else { crate::orientation::upright_exif(&e).into_owned() }), + xmp: xmp.map(|x| if keep_orientation { x } else { crate::orientation::upright_xmp(&x).into_owned() }), + ..Default::default() + }; + // PQ or HLG: linear light in 32-bit float, as for HEIF. + if cicp.is_hdr() { + limits.check(w, h, layout, SampleType::F32)?; + return crate::cicp::linearize(img, cicp); + } + Ok(img) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::DecodeOptions; + + const QUAD_420: &[u8] = include_bytes!("../../tests/fixtures/avif/quad-420-bt709.avif"); + const QUAD_420_DAV1D: &[u8] = include_bytes!("../../tests/fixtures/avif/quad-420-bt709.dav1d.yuv"); + const QUAD_GBR_LOSSLESS: &[u8] = include_bytes!("../../tests/fixtures/avif/quad-444-gbr-lossless.avif"); + const QUAD_SOURCE: &[u8] = include_bytes!("../../tests/fixtures/avif/quad-source.rgb"); + + fn rgb8(img: &Image) -> Vec { + img.to_rgba8().as_chunks::<4>().0.iter().flat_map(|p| [p[0], p[1], p[2]]).collect() + } + + fn mean_abs_diff(a: &[u8], b: &[u8]) -> f64 { + assert_eq!(a.len(), b.len()); + a.iter().zip(b).map(|(&x, &y)| (f64::from(x) - f64::from(y)).abs()).sum::() / a.len() as f64 + } + + /// A box of type `t` around `body`. + fn bx(t: &[u8; 4], body: &[u8]) -> Vec { + let mut v = ((body.len() + 8) as u32).to_be_bytes().to_vec(); + v.extend_from_slice(t); + v.extend_from_slice(body); + v + } + + /// A full box: version 0, no flags. + fn fbx(t: &[u8; 4], body: &[u8]) -> Vec { + let mut b = vec![0, 0, 0, 0]; + b.extend_from_slice(body); + bx(t, &b) + } + + /// An `infe` (version 2) for item `id` of `kind`. + fn infe(id: u16, kind: &[u8; 4]) -> Vec { + let mut b = vec![2, 0, 0, 0]; + b.extend_from_slice(&id.to_be_bytes()); + b.extend_from_slice(&[0, 0]); + b.extend_from_slice(kind); + b.push(0); + bx(b"infe", &b) + } + + fn ispe(w: u32, h: u32) -> Vec { + let mut b = w.to_be_bytes().to_vec(); + b.extend_from_slice(&h.to_be_bytes()); + fbx(b"ispe", &b) + } + + /// The AV1 data and `av1C` of an encoded file's primary item. + fn av1_of(file: &[u8]) -> (Vec, Vec) { + let c = parse(file).unwrap(); + let cfg = c.props_of(c.primary).find_map(|p| if let Prop::Av1c(o) = p { Some(o.clone()) } else { None }).unwrap(); + (c.data(c.primary).unwrap(), cfg) + } + + /// An AVIF of `items` (id, type, data, property indices) with `props`, `refs` and primary 1. + /// All item data sits in `idat` (construction method 1). + /// An item: ID, type, data and 1-based property indices. + type TestItem<'a> = (u16, &'a [u8; 4], &'a [u8], &'a [u8]); + /// An item reference: type, from, to. + type TestRef<'a> = (&'a [u8; 4], u16, &'a [u16]); + + fn build(items: &[TestItem], props: &[Vec], refs: &[TestRef]) -> Vec { + let mut iinf = vec![0, 0, 0, 0]; + iinf.extend_from_slice(&(items.len() as u16).to_be_bytes()); + let mut iloc = vec![1, 0, 0, 0, 0x44, 0x00]; + iloc.extend_from_slice(&(items.len() as u16).to_be_bytes()); + let mut ipma = vec![0, 0, 0, 0]; + ipma.extend_from_slice(&(items.len() as u32).to_be_bytes()); + let mut idat = Vec::new(); + for &(id, kind, data, assoc) in items { + iinf.extend(infe(id, kind)); + iloc.extend_from_slice(&id.to_be_bytes()); + iloc.extend_from_slice(&[0, 1, 0, 0, 0, 1]); // method 1, data ref 0, one extent + iloc.extend_from_slice(&(idat.len() as u32).to_be_bytes()); + iloc.extend_from_slice(&(data.len() as u32).to_be_bytes()); + idat.extend_from_slice(data); + ipma.extend_from_slice(&id.to_be_bytes()); + ipma.push(assoc.len() as u8); + ipma.extend(assoc.iter().map(|&p| p | 0x80)); + } + let mut iref = vec![0, 0, 0, 0]; + for &(kind, from, to) in refs { + let mut b = from.to_be_bytes().to_vec(); + b.extend_from_slice(&(to.len() as u16).to_be_bytes()); + to.iter().for_each(|t| b.extend_from_slice(&t.to_be_bytes())); + iref.extend(bx(kind, &b)); + } + let mut meta = vec![0, 0, 0, 0]; + meta.extend(fbx(b"hdlr", b"\0\0\0\0pict\0\0\0\0\0\0\0\0\0\0\0\0\0")); + meta.extend(fbx(b"pitm", &[0, 1])); + meta.extend(bx(b"iinf", &iinf)); + meta.extend(bx(b"iloc", &iloc)); + meta.extend(bx(b"iref", &iref)); + meta.extend(bx(b"iprp", &[bx(b"ipco", &props.concat()), bx(b"ipma", &ipma)].concat())); + meta.extend(bx(b"idat", &idat)); + let mut file = bx(b"ftyp", b"avif\0\0\0\0avifmif1miaf"); + file.extend(bx(b"meta", &meta)); + file + } + + #[test] + fn libaom_420_limited_range_matches_dav1d() { + let img = crate::decode(QUAD_420).unwrap(); + assert_eq!((img.width(), img.height(), img.layout(), img.sample_type()), (33, 17, ChannelLayout::Rgb, SampleType::U8)); + assert_eq!(img.cicp.map(|c| (c.matrix, c.full_range)), Some((1, false)), "BT.709 matrix, limited range"); + assert_eq!(crate::probe_size(QUAD_420).unwrap(), (33, 17)); + // The AV1 picture is libdav1d's, sample for sample. + let c = parse(QUAD_420).unwrap(); + let p = decode_av1(&c, c.primary, &Limits::default()).unwrap(); + assert_eq!((p.w, p.h, p.bits, p.ssx, p.ssy, p.mono), (33, 17, 8, 1, 1, false)); + let planes: Vec = p.planes.iter().flatten().map(|&v| v as u8).collect(); + assert_eq!(planes, QUAD_420_DAV1D); + let ours = rgb8(&img); + // The quadrant centres are the source's colours, give or take the lossy coding. + for (x, y) in [(5usize, 4usize), (27, 4), (5, 13), (27, 13)] { + let i = (y * 33 + x) * 3; + let d = mean_abs_diff(&ours[i..i + 3], &QUAD_SOURCE[i..i + 3]); + assert!(d < 12.0, "({x}, {y}): {:?} vs {:?}", &ours[i..i + 3], &QUAD_SOURCE[i..i + 3]); + } + } + + #[test] + fn libaom_lossless_identity_matrix_is_exact() { + let img = crate::decode(QUAD_GBR_LOSSLESS).unwrap(); + assert_eq!((img.width(), img.height(), img.sample_type()), (33, 17, SampleType::U8)); + assert_eq!(img.cicp.map(|c| (c.matrix, c.full_range)), Some((0, true))); + assert_eq!(rgb8(&img), QUAD_SOURCE); + } + + #[test] + fn limits_are_checked_before_decoding() { + let tight = |max_pixels, max_alloc| DecodeOptions { limits: Limits { max_pixels, max_alloc, ..Limits::default() }, ..Default::default() }; + assert!(matches!(crate::decode_with(QUAD_420, &tight(33 * 17 - 1, u64::MAX)), Err(CodecError::LimitExceeded(_)))); + // The 16-bit intermediate counts: 33 × 17 RGB at 2 bytes. + assert!(matches!(crate::decode_with(QUAD_420, &tight(u64::MAX, 33 * 17 * 6 - 1)), Err(CodecError::LimitExceeded(_)))); + assert!(crate::decode_with(QUAD_420, &tight(33 * 17, Limits::default().max_alloc)).is_ok()); + // `max_alloc` is the AV1 decoder's picture-store budget too. + assert!(crate::decode_with(QUAD_420, &tight(33 * 17, 33 * 17 * 6)).is_err()); + // A declared size over the limits fails from the boxes alone. + let (av1, cfg) = av1_of(QUAD_420); + let huge = build(&[(1, b"av01", &av1, &[1, 2])], &[ispe(1 << 20, 1 << 20), bx(b"av1C", &[&[0x81, 0, 0, 0][..], &cfg].concat())], &[]); + assert!(matches!(crate::decode(&huge), Err(CodecError::LimitExceeded(_)))); + assert!(matches!(crate::probe_size(&huge), Err(CodecError::LimitExceeded(_)))); + } + + #[test] + fn a_picture_that_differs_from_its_ispe_is_refused() { + let (av1, cfg) = av1_of(QUAD_420); + let file = build(&[(1, b"av01", &av1, &[1, 2])], &[ispe(32, 17), bx(b"av1C", &[&[0x81, 0, 0, 0][..], &cfg].concat())], &[]); + assert!(matches!(crate::decode(&file), Err(CodecError::Malformed { .. }))); + } + + #[test] + fn a_grid_is_assembled_from_its_tiles_and_cropped() { + // Two copies of the 33 × 17 picture side by side, cropped to 60 × 15 by the grid. + let (av1, cfg) = av1_of(QUAD_GBR_LOSSLESS); + let av1c = bx(b"av1C", &[&[0x81, 0, 0, 0][..], &cfg].concat()); + let grid = [0u8, 0, 0, 1, 0, 60, 0, 15]; + let file = build( + &[(1, b"grid", &grid, &[1]), (2, b"av01", &av1, &[2, 3]), (3, b"av01", &av1, &[2, 3])], + &[ispe(60, 15), ispe(33, 17), av1c, fbx(b"pixi", &[3, 8, 8, 8])], + &[(b"dimg", 1, &[2, 3])], + ); + assert_eq!(crate::probe_size(&file).unwrap(), (60, 15)); + let img = crate::decode(&file).unwrap(); + assert_eq!((img.width(), img.height()), (60, 15)); + let got = rgb8(&img); + for y in 0..15 { + for x in 0..60 { + let s = (y * 33 + x % 33) * 3; + assert_eq!(&got[(y * 60 + x) * 3..][..3], &QUAD_SOURCE[s..s + 3], "({x}, {y})"); + } + } + // Tiles that can't cover the grid are refused. + let wide = [0u8, 0, 0, 1, 0, 70, 0, 15]; + let file = build( + &[(1, b"grid", &wide, &[1]), (2, b"av01", &av1, &[2, 3]), (3, b"av01", &av1, &[2, 3])], + &[ispe(70, 15), ispe(33, 17), bx(b"av1C", &[&[0x81, 0, 0, 0][..], &cfg].concat())], + &[(b"dimg", 1, &[2, 3])], + ); + assert!(matches!(crate::decode(&file), Err(CodecError::Malformed { .. }))); + } + + #[test] + fn container_rotation_and_mirror_are_applied_unless_kept() { + let (av1, cfg) = av1_of(QUAD_GBR_LOSSLESS); + let av1c = bx(b"av1C", &[&[0x81, 0, 0, 0][..], &cfg].concat()); + let file = build(&[(1, b"av01", &av1, &[1, 2, 3, 4])], &[ispe(33, 17), av1c, bx(b"irot", &[1]), bx(b"imir", &[0])], &[]); + assert_eq!(crate::probe_size(&file).unwrap(), (17, 33)); + let img = crate::decode(&file).unwrap(); + assert_eq!((img.width(), img.height()), (17, 33)); + let got = rgb8(&img); + // Rotated 90° anticlockwise, then top and bottom exchanged: a transpose, (x, y) from (y, x). + for y in 0..33 { + for x in 0..17 { + let s = (x * 33 + y) * 3; + assert_eq!(&got[(y * 17 + x) * 3..][..3], &QUAD_SOURCE[s..s + 3], "({x}, {y})"); + } + } + let kept = crate::decode_with(&file, &DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + assert_eq!((kept.width(), kept.height(), rgb8(&kept)), (33, 17, QUAD_SOURCE.to_vec())); + } + + /// A `clap` box: width, height, horizontal and vertical offset, each numerator then denominator. + fn clap_box(f: [u32; 8]) -> Vec { + bx(b"clap", &f.iter().flat_map(|v| v.to_be_bytes()).collect::>()) + } + + #[test] + fn clean_aperture_sizes_are_unsigned() { + let (av1, cfg) = av1_of(QUAD_GBR_LOSSLESS); + let av1c = bx(b"av1C", &[&[0x81, 0, 0, 0][..], &cfg].concat()); + // A 2 × 2 aperture with its width written as 0x80000000 / 0x40000000, centred one pixel + // left of the image centre (a signed offset numerator: -1 / 1). + let clap = clap_box([0x8000_0000, 0x4000_0000, 2, 1, (-1i32) as u32, 1, 0, 1]); + let file = build(&[(1, b"av01", &av1, &[1, 2, 3])], &[ispe(33, 17), av1c, clap], &[]); + assert_eq!(crate::probe_size(&file).unwrap(), (2, 2)); + let img = crate::decode(&file).unwrap(); + assert_eq!((img.width(), img.height()), (2, 2)); + // Centre (16, 8) moved to (15, 8): the 2 × 2 crop starts at (15, 8) (rounded from 14.5, 7.5). + let got = rgb8(&img); + let (x0, y0) = ((32.0f64 / 2.0 - 1.0 - 0.5).round() as usize, (16.0f64 / 2.0 - 0.5).round() as usize); + for y in 0..2 { + for x in 0..2 { + let s = ((y0 + y) * 33 + x0 + x) * 3; + assert_eq!(&got[(y * 2 + x) * 3..][..3], &QUAD_SOURCE[s..s + 3], "({x}, {y})"); + } + } + } + + #[test] + fn repeated_transforms_are_refused() { + let (av1, cfg) = av1_of(QUAD_GBR_LOSSLESS); + let av1c = bx(b"av1C", &[&[0x81, 0, 0, 0][..], &cfg].concat()); + // the same irot associated twice, and through two ipma entries for the item + let twice = build(&[(1, b"av01", &av1, &[1, 2, 3, 3])], &[ispe(33, 17), av1c.clone(), bx(b"irot", &[1])], &[]); + assert!(matches!(crate::probe_size(&twice), Err(CodecError::Malformed { .. }))); + assert!(matches!(crate::decode(&twice), Err(CodecError::Malformed { .. }))); + let mut many = build(&[(1, b"av01", &av1, &[1, 2, 3])], &[ispe(33, 17), av1c.clone(), bx(b"irot", &[1])], &[]); + let c = parse(&many).unwrap(); + assert_eq!(c.transforms(1).unwrap(), vec![Transform::Rot(1)]); + // 1024 more ipma records for item 1, each repeating the irot 255 times + let mut ipma = vec![0, 0, 0, 0]; + ipma.extend_from_slice(&1024u32.to_be_bytes()); + for _ in 0..1024 { + ipma.extend_from_slice(&[0, 1, 255]); + ipma.extend(std::iter::repeat_n(3u8, 255)); + } + many = build(&[(1, b"av01", &av1, &[1, 2])], &[ispe(33, 17), av1c, bx(b"irot", &[1])], &[]); + let mut c = parse(&many).unwrap(); + parse_ipma(&ipma, &mut c).unwrap(); + assert!(matches!(c.transforms(1), Err(CodecError::Malformed { .. }))); + // each kind once is fine, in any order + let ok = build( + &[(1, b"av01", &av1, &[1, 2, 3, 4, 5])], + &[ + ispe(33, 17), + bx(b"av1C", &[&[0x81, 0, 0, 0][..], &cfg].concat()), + clap_box([33, 1, 17, 1, 0, 1, 0, 1]), + bx(b"irot", &[1]), + bx(b"imir", &[0]), + ], + &[], + ); + assert_eq!(crate::probe_size(&ok).unwrap(), (17, 33)); + } + + #[test] + fn alpha_with_transforms_of_its_own_is_refused() { + let (av1, cfg) = av1_of(QUAD_GBR_LOSSLESS); + let av1c = bx(b"av1C", &[&[0x81, 0, 0, 0][..], &cfg].concat()); + let auxc = fbx(b"auxC", b"urn:mpeg:mpegB:cicp:systems:auxiliary:alpha\0"); + let props = [ispe(33, 17), av1c, auxc, bx(b"imir", &[1])]; + let with = |colour: &[u8], alpha: &[u8]| build(&[(1, b"av01", &av1, colour), (2, b"av01", &av1, alpha)], &props, &[(b"auxl", 2, &[1])]); + // no transforms on the alpha plane, or the colour's: composed, then turned together + let plain = crate::decode(&with(&[1, 2], &[1, 2, 3])).unwrap(); + assert_eq!(plain.layout(), ChannelLayout::Rgba); + let both = crate::decode(&with(&[1, 2, 4], &[1, 2, 3, 4])).unwrap(); + let alpha_free = crate::decode(&with(&[1, 2, 4], &[1, 2, 3])).unwrap(); + assert_eq!(both.to_rgba8(), alpha_free.to_rgba8()); + // an alpha-only mirror would put transparency on the wrong side + assert!(matches!(crate::decode(&with(&[1, 2], &[1, 2, 3, 4])), Err(CodecError::Unsupported { .. }))); + } + + #[test] + fn transforms_rotate_mirror_and_crop() { + // 3 × 2, one channel, value = index + let cv = || Canvas { w: 3, h: 2, ch: 1, data: (0..6).collect() }; + let r = apply_transforms(cv(), &[Transform::Rot(1)]); + // anticlockwise: the right column becomes the top row + assert_eq!((r.w, r.h, r.data), (2, 3, vec![2, 5, 1, 4, 0, 3])); + // axis 0 exchanges top and bottom, axis 1 left and right (libheif's and libavif's reading) + assert_eq!(apply_transforms(cv(), &[Transform::Mir(0)]).data, vec![3, 4, 5, 0, 1, 2]); + assert_eq!(apply_transforms(cv(), &[Transform::Mir(1)]).data, vec![2, 1, 0, 5, 4, 3]); + let c = apply_transforms(cv(), &[Transform::Clap { w: 1.0, h: 1.0, dx: 1.0, dy: 0.0 }]); + assert_eq!((c.w, c.h), (1, 1)); + assert!(c.data == [2] || c.data == [5]); + assert_eq!(displayed(3, 2, &[Transform::Rot(3)]), (2, 3)); + // A non-finite or empty aperture is ignored. + assert_eq!(apply_transforms(cv(), &[Transform::Clap { w: f64::NAN, h: 1.0, dx: 0.0, dy: 0.0 }]).data.len(), 6); + } + + #[test] + fn hostile_files_are_errors_not_panics() { + for file in [QUAD_420, QUAD_GBR_LOSSLESS] { + // every truncation + for n in 0..file.len() { + let _ = crate::probe_size(&file[..n]); + let _ = crate::decode(&file[..n]); + } + // byte flips through the container and the AV1 data + for i in 0..file.len() { + for x in [0x01u8, 0x80, 0xff] { + let mut m = file.to_vec(); + m[i] ^= x; + let _ = crate::probe_size(&m); + let _ = crate::decode(&m); + } + } + } + // box sizes that point far outside the file + let mut b = bx(b"ftyp", b"avif\0\0\0\0avif"); + b.extend_from_slice(b"\xff\xff\xff\xffmeta\0\0\0\0"); + assert!(crate::decode(&b).is_err()); + let mut b = bx(b"ftyp", b"avif\0\0\0\0avif"); + b.extend_from_slice(b"\0\0\0\x01meta\xff\xff\xff\xff\xff\xff\xff\xff"); + assert!(crate::probe_size(&b).is_err()); + } + + #[test] + fn an_animated_avif_is_unsupported() { + let mut b = bx(b"ftyp", b"avis\0\0\0\0avis"); + b.extend(bx(b"moov", &[])); + assert!(matches!(crate::decode(&b), Err(CodecError::Unsupported { format: Format::Avif, .. }))); + } + + #[test] + fn zero_width_iloc_extents_are_capped() { + // iloc v2 with zero-byte offset/length/base/index fields: each extent costs no input bytes + let mut iloc = vec![2, 0, 0, 0, 0, 0]; + iloc.extend_from_slice(&3u32.to_be_bytes()); + for id in 1..=3u32 { + iloc.extend_from_slice(&id.to_be_bytes()); + iloc.extend_from_slice(&[0, 0, 0, 0, 0xff, 0xff]); // method, data ref, 65535 extents + } + let mut meta = vec![0, 0, 0, 0]; + meta.extend(bx(b"iloc", &iloc)); + let mut b = bx(b"ftyp", b"avif\0\0\0\0avif"); + b.extend(bx(b"meta", &meta)); + assert!(matches!(crate::decode(&b), Err(CodecError::Malformed { message, .. }) if message.contains("extents"))); + } + + #[test] + fn item_data_larger_than_the_file_is_refused() { + // one item whose 1000 extents each repeat the whole file (length 0 = to the end) + let mut iloc = vec![1, 0, 0, 0, 0x44, 0]; + iloc.extend_from_slice(&1u16.to_be_bytes()); + iloc.extend_from_slice(&[0, 1, 0, 0, 0, 0]); // id 1, method 0, data ref 0 + iloc.extend_from_slice(&1000u16.to_be_bytes()); + for _ in 0..1000 { + iloc.extend_from_slice(&[0; 8]); + } + let mut meta = vec![0, 0, 0, 0]; + meta.extend(fbx(b"pitm", &[0, 1])); + meta.extend(bx(b"iloc", &iloc)); + let mut b = bx(b"ftyp", b"avif\0\0\0\0avif"); + b.extend(bx(b"meta", &meta)); + let c = parse(&b).unwrap(); + assert!(matches!(c.data(1), Err(CodecError::Malformed { message, .. }) if message.contains("larger than the file"))); + } + + #[test] + fn unsupported_matrices_are_errors() { + for m in [8u16, 10, 11, 13, 14] { + assert!(matches!(coefficients(m, 1), Err(CodecError::Unsupported { .. })), "{m}"); + } + assert_eq!(coefficients(0, 1).unwrap(), None); + assert!(coefficients(12, 9).unwrap().is_some()); + } + + #[cfg(feature = "avif")] + mod with_the_encoder { + use super::*; + use crate::codecs::avif::{HdrTransfer, encode_avif_hdr, encode_avif_with_speed}; + + /// A smooth test card with a saturated band. + fn card(w: usize, h: usize, ch: usize) -> Vec { + (0..w * h) + .flat_map(|i| { + let (x, y) = (i % w, i / w); + let px = + [(x * 255 / (w - 1)) as u8, (y * 255 / (h - 1)) as u8, if x < w / 4 { 230 } else { 40 }, (255 - x * 200 / (w - 1)) as u8]; + px.into_iter().take(ch) + }) + .collect() + } + + #[test] + fn ravif_rgb_round_trip_is_10_bit() { + let (w, h) = (48, 32); + let src = Image::from_u8(w as u32, h as u32, ChannelLayout::Rgb, card(w, h, 3)).unwrap(); + let file = encode_avif_with_speed(&src, 95, 10).unwrap(); + let img = crate::decode(&file).unwrap(); + assert_eq!((img.width(), img.height(), img.layout(), img.sample_type()), (48, 32, ChannelLayout::Rgb, SampleType::U16)); + let diff = mean_abs_diff(&rgb8(&img), &card(w, h, 3)); + assert!(diff < 2.5, "mean difference {diff}"); + } + + #[test] + fn ravif_alpha_round_trips() { + let (w, h) = (40, 24); + let src = Image::from_u8(w as u32, h as u32, ChannelLayout::Rgba, card(w, h, 4)).unwrap(); + let file = encode_avif_with_speed(&src, 95, 10).unwrap(); + let img = crate::decode(&file).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Rgba); + let got = img.to_rgba8(); + let want = card(w, h, 4); + let alpha = |v: &[u8]| v.iter().skip(3).step_by(4).copied().collect::>(); + let diff = mean_abs_diff(&alpha(&got), &alpha(&want)); + assert!(diff < 2.0, "alpha mean difference {diff}"); + } + + #[test] + fn exif_is_read_from_its_item() { + let mut src = Image::from_u8(16, 8, ChannelLayout::Rgb, card(16, 8, 3)).unwrap(); + let exif = b"II*\0\x08\0\0\0\0\0\0\0\0\0".to_vec(); + src.meta.exif = Some(exif.clone()); + let file = encode_avif_with_speed(&src, 80, 10).unwrap(); + let img = crate::decode_with(&file, &DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + assert_eq!(img.meta.exif.as_deref(), Some(exif.as_slice())); + } + + #[test] + fn hdr_round_trips_keep_highlights_above_white() { + let (w, h) = (32usize, 16usize); + // linear BT.2020: left half grey 0.18, right half 4 × SDR white + let rgb: Vec = (0..w * h).flat_map(|i| if i % w < w / 2 { [0.18f32; 3] } else { [4.0; 3] }).collect(); + for t in [HdrTransfer::Pq, HdrTransfer::Hlg] { + let file = encode_avif_hdr(w as u32, h as u32, &rgb, 98, 10, None, t).unwrap(); + let img = crate::decode(&file).unwrap(); + assert_eq!(img.sample_type(), SampleType::F32, "{t:?}"); + assert_eq!(img.cicp.map(|c| c.transfer), Some(Cicp::TRANSFER_LINEAR), "{t:?}"); + let v = img.to_f32_samples().unwrap(); + let at = |x: usize| v[(8 * w + x) * 3 + 1]; + assert!((at(4) / 0.18 - 1.0).abs() < 0.08, "{t:?}: grey {}", at(4)); + assert!((at(w - 4) / 4.0 - 1.0).abs() < 0.08, "{t:?}: highlight {}", at(w - 4)); + } + } + } +} diff --git a/crates/image-codecs/src/format.rs b/crates/image-codecs/src/format.rs index 810c1ad48..26696ae2a 100644 --- a/crates/image-codecs/src/format.rs +++ b/crates/image-codecs/src/format.rs @@ -26,7 +26,8 @@ pub enum Format { OpenExr, /// Radiance RGBE. Hdr, - /// Encode-only behind the `avif` feature; see [`ASYMMETRIC_EXCEPTIONS`]. + /// Encoded behind the `avif` feature and decoded behind `avif-decode` (native targets); see + /// [`ASYMMETRIC_EXCEPTIONS`] for a build with only one of them. Avif, /// HEIF/HEIC (HEVC-coded): read-only; see [`ASYMMETRIC_EXCEPTIONS`]. Heif, @@ -91,8 +92,10 @@ pub const ASYMMETRIC_EXCEPTIONS: &[(Format, &str)] = &[ ), ( Format::Avif, - "AVIF encode uses ravif (pure Rust) but decoding requires dav1d (C); read stays unsupported \ - until a pure-Rust AV1 decoder is viable. Only enabled with the non-default `avif` feature.", + "AVIF encode (ravif and rav1e, pure Rust) is behind the non-default `avif` feature and AVIF \ + decode (craft-video's clean-room pure-Rust AV1 decoder) behind the non-default \ + `avif-decode` feature, native targets only (on wasm32 a decoder panic would abort). A build \ + with only one of them reads or writes AVIF, not both.", ), (Format::Jxl, "Lossless JPEG XL export uses the pure-Rust zune-jpegxl encoder; JPEG XL reading is tracked separately and is not yet available."), ( @@ -304,7 +307,9 @@ pub fn caps(format: Format) -> FormatCaps { Format::Qoi => base, Format::OpenExr => FormatCaps { depths: &[S::F16, S::F32], layouts: RGB_GRAY, ..base }, Format::Hdr => FormatCaps { depths: &[S::F32], layouts: &[L::Rgb], alpha: false, lossy: true, ..base }, - Format::Avif => FormatCaps { read: false, write: cfg!(feature = "avif"), lossy: true, ..base }, + Format::Avif => { + FormatCaps { read: cfg!(all(feature = "avif-decode", not(target_arch = "wasm32"))), write: cfg!(feature = "avif"), lossy: true, ..base } + } Format::Heif => { FormatCaps { read: cfg!(feature = "heif"), write: false, depths: &[S::U8, S::U16], icc: true, exif: true, xmp: true, lossy: true, ..base } } diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 361285b2a..48dee65c8 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -110,7 +110,11 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res Format::Heif => heif::decode(bytes, l, opts.keep_orientation), Format::Jp2 => jp2::decode(bytes, l), Format::Jxl => Err(CodecError::unsupported(format, "JPEG XL reading is not yet available; export is supported")), - Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::decode(format, bytes, l), + #[cfg(all(feature = "avif-decode", not(target_arch = "wasm32")))] + Format::Avif => codecs::avif_decode::decode(bytes, l, opts.keep_orientation), + #[cfg(not(all(feature = "avif-decode", not(target_arch = "wasm32"))))] + Format::Avif => via_image::decode(format, bytes, l), + Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr => via_image::decode(format, bytes, l), }?; finish_format(format, bytes, img, opts) } @@ -167,10 +171,11 @@ fn finish_format(format: Format, bytes: &[u8], mut img: Image, opts: &DecodeOpti } // Turn the pixels upright, like Photoshop: a TIFF records it in the decoded page's own // directory, the others in their EXIF block. The metadata is rewritten to Orientation = 1 - // on the way. HEIF keeps it in its container, and its decoder has already applied it. + // on the way. HEIF and AVIF keep it in their container, and their decoders have already + // applied it. let o = match format { Format::Tiff => tiff::orientation(bytes, None), - Format::Heif => 1, + Format::Heif | Format::Avif => 1, Format::Dpx => dpx::orientation(bytes), Format::Cineon => dpx::cineon_orientation(bytes), _ => img.meta.exif.as_deref().map_or(1, exif_orientation), @@ -199,10 +204,11 @@ pub fn decode_png_rows>( } /// The upright pixel size [`decode`] would return, read from the file's header without decoding -/// pixels, for WebP, OpenEXR (flat) and the GIF/BMP/TGA/ICO/QOI/HDR family. It runs the same +/// pixels, for WebP, OpenEXR (flat), AVIF (with `avif-decode`) and the GIF/BMP/TGA/ICO/QOI/HDR family. It runs the same /// header reads and size checks as the decoder (default [`Limits`]) and swaps the size for an -/// EXIF orientation of 5–8, so wherever [`decode`] succeeds the two agree. Other formats (and -/// deep EXR or AVIF) return [`CodecError::Unsupported`]: decode those to learn their size. +/// EXIF orientation of 5–8 (AVIF: its container's `clap` and `irot`), so wherever [`decode`] +/// succeeds the two agree. Other formats (and deep EXR) return [`CodecError::Unsupported`]: decode +/// those to learn their size. pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> { let format = detect(bytes).ok_or(CodecError::UnknownFormat)?; if !caps(format).read { @@ -212,7 +218,11 @@ pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> { let (w, h, exif) = match format { Format::WebP => webp::probe(bytes, l)?, Format::OpenExr => exr::probe(bytes, l).map(|(w, h)| (w, h, None))?, - Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::probe(format, bytes, l)?, + #[cfg(all(feature = "avif-decode", not(target_arch = "wasm32")))] + Format::Avif => codecs::avif_decode::probe(bytes, l).map(|(w, h)| (w, h, None))?, + #[cfg(not(all(feature = "avif-decode", not(target_arch = "wasm32"))))] + Format::Avif => via_image::probe(format, bytes, l)?, + Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr => via_image::probe(format, bytes, l)?, _ => return Err(CodecError::unsupported(format, "no header-only size probe")), }; let o = exif.as_deref().map_or(1, exif_orientation); diff --git a/crates/image-codecs/tests/fixtures/avif/README.md b/crates/image-codecs/tests/fixtures/avif/README.md new file mode 100644 index 000000000..b8b3a8c2a --- /dev/null +++ b/crates/image-codecs/tests/fixtures/avif/README.md @@ -0,0 +1,12 @@ +# AVIF decode fixtures + +Small AVIF files from an independent encoder (libaom through FFmpeg 7.1), for `src/codecs/avif_decode.rs`. + +- `quad-source.rgb`: the 33×17 source, raw RGB24 four-colour quadrants (red, green, blue, near-white). +- `quad-420-bt709.avif`: 8-bit 4:2:0, limited range, BT.709 matrix, odd size (FFmpeg leaves the primaries and transfer unspecified): + `ffmpeg -f rawvideo -pix_fmt rgb24 -s 33x17 -i quad-source.rgb -c:v libaom-av1 -crf 12 -still-picture 1 -pix_fmt yuv420p -colorspace bt709 -color_primaries bt709 -color_trc iec61966-2-1 -color_range tv quad-420-bt709.avif` +- `quad-420-bt709.dav1d.yuv`: that file's AV1 picture as libdav1d decodes it, planar 8-bit 4:2:0 (Y, then Cb, then Cr): + `ffmpeg -c:v libdav1d -i quad-420-bt709.avif -f rawvideo -pix_fmt yuv420p quad-420-bt709.dav1d.yuv` +- `quad-444-gbr-lossless.avif`: lossless 8-bit 4:4:4 with the identity (GBR) matrix, full range: + `ffmpeg -f rawvideo -pix_fmt rgb24 -s 33x17 -i quad-source.rgb -c:v libaom-av1 -crf 0 -still-picture 1 -pix_fmt gbrp -colorspace rgb -color_range pc quad-444-gbr-lossless.avif`. + It decodes to `quad-source.rgb` exactly. diff --git a/crates/image-codecs/tests/fixtures/avif/quad-420-bt709.avif b/crates/image-codecs/tests/fixtures/avif/quad-420-bt709.avif new file mode 100644 index 0000000000000000000000000000000000000000..419196cb178743782cf147ea50d111e2b8642d42 GIT binary patch literal 369 zcmZQzU{FXasVqn=%S>Yc0uY^>nP!-qnV9D5Xy^nK`k9+rk_eIm0{x7XoFWL5fuSHX zxdg@r(E&hyX;G?2VqQv4Dp(a?L1sxVSP3JLl*`P?PX_T^7#J9V5JWOCDgx;MAlAst z%mX_CEF%Tvq%r}8LA=B=14HNhoP4mES(ybz1we7{%!1^6AT5$vT#yP<2m~Or1c6wf zAhRM9B*4tU!2zUr63Yyo8yR>&Lc+=UIYoKNITZ{{KnOIQfk8a8AU6@j00N-HSQwd_ zS%A!l+?2!;E=~zih2|6v4i*6;XO{y&vw`U4k2c-I-AWygkL2ynp7_}AP4*4enEOQm q)z5b1%rN?KQ|H$?;i_Mo{+`I#w8y3Pf9fWWdacYc0uY^>nP!-qnV9D5Xy^zO`k9+rk_eIm0{x7XoFWL5fuSHX zxdg@r(E&hyX;G?2VqQv4Dp(a?L1sxVSP3JLl*`P?PX_T^7#J9V5JWOCDgtQ>AlAst z%mX_CEF%Tvq%r}8LA=B=14HNhoP4mES(ybz1we7{%!1^6AT5$vT#yP<2m~Or1c6wf zAhRM9B*4tU!2zUr63Yyo8x?>o1_t5e{G6h^jRAvYzlgp0#MRG~S8BZJXM2k7n(sn>EClKWl bool { /// copies of one 4K reference (about 6.4 GB) from a few hundred bytes of show_existing_frame headers. const MAX_SHOWN_BYTES_PER_CHUNK: usize = 1 << 30; -fn shown_bytes_within_limit(bytes: usize) -> bool { - bytes <= MAX_SHOWN_BYTES_PER_CHUNK +fn shown_bytes_within_limit(bytes: usize, budget: usize) -> bool { + bytes <= budget.min(MAX_SHOWN_BYTES_PER_CHUNK) } /// Bytes of the `u16` sample planes of a `w` × `h` picture. @@ -313,14 +316,14 @@ fn picture_bytes(w: usize, h: usize, c: &crate::av1::decode::header::ColorConfig } /// Add a shown picture to a chunk's outputs, refusing the chunk (and dropping everything it showed) -/// before the list can grow past [`MAX_SHOWN_PER_CHUNK`]. -fn push_shown(out: &mut Vec<(Output, usize, u32)>, shown_bytes: &mut usize, item: (Output, usize, u32)) -> Result<()> { +/// before the list can grow past [`MAX_SHOWN_PER_CHUNK`] or its pictures past `budget` bytes. +fn push_shown(out: &mut Vec<(Output, usize, u32)>, shown_bytes: &mut usize, budget: usize, item: (Output, usize, u32)) -> Result<()> { if !shown_within_limit(out.len() + 1) { out.clear(); return Err(Error::Invalid("too many pictures shown by one chunk")); } let bytes = shown_bytes.saturating_add(item.0.bytes()); - if !shown_bytes_within_limit(bytes) { + if !shown_bytes_within_limit(bytes, budget) { out.clear(); return Err(Error::Invalid("pictures shown by one chunk are too large")); } @@ -349,6 +352,14 @@ impl Decoder { self.memory_budget = bytes; } + /// Cap the bytes of the pictures one `decode` call may return together, below the built-in + /// 1 GiB (a larger value keeps that). The pictures are refused before they are built, so a + /// caller that keeps only one of them (an AVIF still) can hold a stream to its own memory + /// budget instead of collecting up to 256 pictures first. + pub fn set_shown_budget(&mut self, bytes: u64) { + self.shown_budget = usize::try_from(bytes).unwrap_or(usize::MAX).min(MAX_SHOWN_BYTES_PER_CHUNK); + } + /// A decoder using all available cores (with the `threads` feature). pub fn new() -> Decoder { Decoder::with_threads(default_threads()) @@ -369,6 +380,7 @@ impl Decoder { ref_color: Default::default(), shown_bytes: 0, shown_overflow: false, + shown_budget: MAX_SHOWN_BYTES_PER_CHUNK, lf_deltas: LoopFilterDeltas::defaults(), seg_features: SegmentationFeatures::default(), prev_gm_params: default_gm_params(), @@ -564,20 +576,20 @@ impl Decoder { let mut r = BitReader::new(payload); let shown = self.frame_header(&mut r, temporal_id, spatial_id)?; if let Some(o) = shown { - push_shown(out, &mut self.shown_bytes, (o, tu, spatial_id))?; + push_shown(out, &mut self.shown_bytes, self.shown_budget, (o, tu, spatial_id))?; continue; } if obu_type == OBU_FRAME_T { r.byte_align(); let rest = payload.get(r.byte_pos()..).ok_or(Error::Truncated)?; if let Some(o) = self.tile_group(rest)? { - push_shown(out, &mut self.shown_bytes, (o, tu, spatial_id))?; + push_shown(out, &mut self.shown_bytes, self.shown_budget, (o, tu, spatial_id))?; } } } OBU_TILE_GROUP_T => { if let Some(o) = self.tile_group(payload)? { - push_shown(out, &mut self.shown_bytes, (o, tu, spatial_id))?; + push_shown(out, &mut self.shown_bytes, self.shown_budget, (o, tu, spatial_id))?; } } OBU_METADATA_T | OBU_TILE_LIST_T => {} @@ -726,7 +738,7 @@ impl Decoder { // A picture past the chunk's byte budget is never built (decoding the frame as a reference // still happens, so the reference slots stay as the stream says), and the chunk is refused. let bytes = picture_bytes(job.fh.upscaled_width as usize, job.fh.frame_height as usize, &job.seq.color); - if job.picture.is_some() && !shown_bytes_within_limit(self.shown_bytes.saturating_add(bytes)) { + if job.picture.is_some() && !shown_bytes_within_limit(self.shown_bytes.saturating_add(bytes), self.shown_budget) { job.picture = None; self.shown_overflow = true; self.submit(job); @@ -1307,18 +1319,33 @@ mod shown_limit_tests { #[test] fn push_shown_refuses_a_chunk_past_either_limit_and_drops_what_it_showed() { let (mut out, mut bytes) = (Vec::new(), 0usize); - push_shown(&mut out, &mut bytes, shown(10)).unwrap(); + push_shown(&mut out, &mut bytes, MAX_SHOWN_BYTES_PER_CHUNK, shown(10)).unwrap(); assert_eq!((out.len(), bytes), (1, 10)); // past the byte budget - assert!(push_shown(&mut out, &mut bytes, shown(MAX_SHOWN_BYTES_PER_CHUNK)).is_err()); + assert!(push_shown(&mut out, &mut bytes, MAX_SHOWN_BYTES_PER_CHUNK, shown(MAX_SHOWN_BYTES_PER_CHUNK)).is_err()); assert!(out.is_empty(), "the refused chunk shows nothing"); // past the count limit let (mut out, mut bytes) = (Vec::new(), 0usize); for _ in 0..MAX_SHOWN_PER_CHUNK { - push_shown(&mut out, &mut bytes, shown(0)).unwrap(); + push_shown(&mut out, &mut bytes, MAX_SHOWN_BYTES_PER_CHUNK, shown(0)).unwrap(); } - assert!(push_shown(&mut out, &mut bytes, shown(0)).is_err()); + assert!(push_shown(&mut out, &mut bytes, MAX_SHOWN_BYTES_PER_CHUNK, shown(0)).is_err()); assert!(out.is_empty()); + // past a caller's lower budget + let (mut out, mut bytes) = (Vec::new(), 0usize); + push_shown(&mut out, &mut bytes, 100, shown(60)).unwrap(); + assert!(push_shown(&mut out, &mut bytes, 100, shown(60)).is_err()); + assert!(out.is_empty()); + } + + #[test] + fn a_shown_budget_can_lower_the_limit_but_not_raise_it() { + let mut d = Decoder::with_threads(1); + assert_eq!(d.shown_budget, MAX_SHOWN_BYTES_PER_CHUNK); + d.set_shown_budget(1 << 20); + assert_eq!(d.shown_budget, 1 << 20); + d.set_shown_budget(u64::MAX); + assert_eq!(d.shown_budget, MAX_SHOWN_BYTES_PER_CHUNK); } #[test] @@ -1333,8 +1360,8 @@ mod shown_limit_tests { // 4K 4:2:0, u16 samples: about 25 MB; 256 of them is far past the limit, a few are fine let one = picture_bytes(3840, 2160, &c); assert_eq!(one, (3840 * 2160 + 2 * 1920 * 1080) * 2); - assert!(shown_bytes_within_limit(4 * one)); - assert!(!shown_bytes_within_limit(MAX_SHOWN_PER_CHUNK * one)); + assert!(shown_bytes_within_limit(4 * one, MAX_SHOWN_BYTES_PER_CHUNK)); + assert!(!shown_bytes_within_limit(MAX_SHOWN_PER_CHUNK * one, MAX_SHOWN_BYTES_PER_CHUNK)); let mono = crate::av1::decode::header::ColorConfig { num_planes: 1, ..Default::default() }; assert_eq!(picture_bytes(10, 5, &mono), 100); assert_eq!(picture_bytes(usize::MAX, 2, &c), usize::MAX); From 254e9a409b410dc93c0e06ff66cd1e767893ac32 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Sun, 15 Nov 2026 23:44:31 +0100 Subject: [PATCH 149/150] Add craft-deckcraft-format: the .deckcraft package layer from DeckCraft (#1748) The package layer of DeckCraft's native format, moved from storytold/deckcraft crates/format at 0d5c0b85 (lib.rs, finite.rs): the mimetype and legacy SlideCraft type, sniffing, the presentation.json envelope (generic over the app's presentation), the NaN-safe JSON writer (Finite), the reader that patches null numbers in older files, member names and compression choice, and the bounded, budget-charged member reads with the format's limits. The presentation model, repair, media and chart-part mapping, and outline/RTF stay in DeckCraft. Writes go through craft-zip's deterministic writer; the consumer's golden test shows the package bytes are unchanged. Part of #778. --- Cargo.lock | 11 ++ Cargo.toml | 1 + README.md | 4 + crates/deckcraft-format/Cargo.toml | 18 ++ crates/deckcraft-format/README.md | 25 +++ crates/deckcraft-format/src/finite.rs | 185 +++++++++++++++++++ crates/deckcraft-format/src/lib.rs | 226 +++++++++++++++++++++++ crates/deckcraft-format/src/manifest.rs | 99 ++++++++++ crates/deckcraft-format/src/tests.rs | 235 ++++++++++++++++++++++++ 9 files changed, 804 insertions(+) create mode 100644 crates/deckcraft-format/Cargo.toml create mode 100644 crates/deckcraft-format/README.md create mode 100644 crates/deckcraft-format/src/finite.rs create mode 100644 crates/deckcraft-format/src/lib.rs create mode 100644 crates/deckcraft-format/src/manifest.rs create mode 100644 crates/deckcraft-format/src/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 6d54a86d5..7ed516463 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1480,6 +1480,17 @@ dependencies = [ "proptest", ] +[[package]] +name = "craft-deckcraft-format" +version = "0.1.0" +dependencies = [ + "craft-zip", + "serde", + "serde_json", + "thiserror 2.0.21", + "zip", +] + [[package]] name = "craft-designcraft-format" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index db3902687..48a2edf1d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,7 @@ craft-doc-binary = { path = "crates/doc-binary" } craft-docx = { path = "crates/docx" } craft-document-bundle = { path = "crates/document-bundle" } craft-mcmat = { path = "crates/mcmat" } +craft-deckcraft-format = { path = "crates/deckcraft-format" } craft-designcraft-format = { path = "crates/designcraft-format" } craft-dxf = { path = "crates/dxf" } craft-drawingml = { path = "crates/drawingml" } diff --git a/README.md b/README.md index c6e95bd4f..0b4b9a415 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,10 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit pages and version detection, and written back; and `model`, the drawing above the tags (header variables, tables, blocks, layouts, entities) and its writer (R12 to 2018), merged from CADCraft's and VectorCraft's readers. Bounded on hostile input. Mapping the model onto a document stays in each app. +- [`craft-deckcraft-format`](crates/deckcraft-format/README.md): the DeckCraft `.deckcraft` package: + identity and sniffing, the `presentation.json` envelope with the presentation as a type + parameter, NaN-safe JSON and the reader for older files with `null` numbers, member names and + compression, and bounded, budgeted member reads. The presentation model and its mapping stay in DeckCraft. - [`craft-dwg`](crates/dwg/README.md): bounded byte-level DWG↔DXF conversion with diagnostics, capped output and an optional child-process worker; the `acadrust` dependency remains isolated behind bytes and is unavailable on wasm32 and FreeBSD/aarch64. diff --git a/crates/deckcraft-format/Cargo.toml b/crates/deckcraft-format/Cargo.toml new file mode 100644 index 000000000..d7c6b035f --- /dev/null +++ b/crates/deckcraft-format/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "craft-deckcraft-format" +description = "The DeckCraft .deckcraft package: identity, manifest envelope, NaN-safe JSON, member names and bounded reads" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +craft-zip = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } +zip = { version = "9", default-features = false, features = ["deflate-flate2"] } diff --git a/crates/deckcraft-format/README.md b/crates/deckcraft-format/README.md new file mode 100644 index 000000000..94f5d90e8 --- /dev/null +++ b/crates/deckcraft-format/README.md @@ -0,0 +1,25 @@ +# craft-deckcraft-format + +The DeckCraft `.deckcraft` package. A file is a ZIP that holds, in this order: + +- `mimetype`, stored: `application/vnd.storyteller.deckcraft+zip`. Files from before the + SlideCraft rename carry the legacy type, and `sniff` accepts both. +- `presentation.json`, deflated: `{"format":"deckcraft","version":1,"generator":…,"presentation":…}`. + It is compact JSON, and non-finite numbers are written as `0` (`Finite`). +- `media/` and `chart-parts/` binary members. A member whose content type is already + compressed (JPEG, PNG, video, MP3/MP4 audio, anything `zip`) is stored, and any other member is deflated. + +The presentation is a type parameter: DeckCraft owns the model, decides which media and chart +parts become members, and repairs a loaded deck. This crate owns the bytes: + +- `write(presentation, generator, members)` writes the package with `craft-zip`'s deterministic writer. +- `Package::open_manifest(bytes, max)` reads and checks the manifest. A file with no + `presentation.json` or a different `format` is `NotOurs`. `parse_manifest` reads files written + before `Finite`, where serde_json saved NaN as `null`, as 0 within fixed patch and re-parse budgets. +- `Package::read_max` refuses a member that declares more than its limit and stops one that + inflates past it. `read_charged` charges a shared budget for every byte it inflates, including + the bytes of a read that then fails its CRC. +- `Budget` and the `MAX_*` constants are the format's limits: 2 GiB per member, 4 GiB of media and + chart parts, 100,000 items and a 256 MiB manifest. + +There is no `unsafe` and no panics, and it builds for `wasm32-unknown-unknown`. diff --git a/crates/deckcraft-format/src/finite.rs b/crates/deckcraft-format/src/finite.rs new file mode 100644 index 000000000..834108a33 --- /dev/null +++ b/crates/deckcraft-format/src/finite.rs @@ -0,0 +1,185 @@ +//! A serde serializer adapter that writes non-finite floats as 0. +//! +//! serde_json writes NaN and ±infinity as `null`, which doesn't read back as a number: a single +//! NaN anywhere in the model (a rotation, a crop, a stroke width) made a saved file fail to open +//! as "damaged". [`Finite`] wraps any `Serialize` value and replaces those floats on the way out; +//! everything else passes through unchanged. + +use serde::ser::{self, Serialize, Serializer}; + +/// `T`, serialized with non-finite floats written as 0. +pub struct Finite<'a, T: ?Sized>(pub &'a T); + +impl Serialize for Finite<'_, T> { + fn serialize(&self, s: S) -> Result { + self.0.serialize(Wrap(s)) + } +} + +struct Wrap(S); + +macro_rules! pass { + ($($m:ident($t:ty)),* $(,)?) => { + $(fn $m(self, v: $t) -> Result { self.0.$m(v) })* + }; +} + +impl Serializer for Wrap { + type Ok = S::Ok; + type Error = S::Error; + type SerializeSeq = Wrap; + type SerializeTuple = Wrap; + type SerializeTupleStruct = Wrap; + type SerializeTupleVariant = Wrap; + type SerializeMap = Wrap; + type SerializeStruct = Wrap; + type SerializeStructVariant = Wrap; + + pass!( + serialize_bool(bool), + serialize_i8(i8), + serialize_i16(i16), + serialize_i32(i32), + serialize_i64(i64), + serialize_i128(i128), + serialize_u8(u8), + serialize_u16(u16), + serialize_u32(u32), + serialize_u64(u64), + serialize_u128(u128), + serialize_char(char), + serialize_str(&str), + serialize_bytes(&[u8]), + ); + + fn serialize_f32(self, v: f32) -> Result { + self.0.serialize_f32(if v.is_finite() { v } else { 0.0 }) + } + fn serialize_f64(self, v: f64) -> Result { + self.0.serialize_f64(if v.is_finite() { v } else { 0.0 }) + } + fn serialize_none(self) -> Result { + self.0.serialize_none() + } + fn serialize_some(self, v: &T) -> Result { + self.0.serialize_some(&Finite(v)) + } + fn serialize_unit(self) -> Result { + self.0.serialize_unit() + } + fn serialize_unit_struct(self, name: &'static str) -> Result { + self.0.serialize_unit_struct(name) + } + fn serialize_unit_variant(self, name: &'static str, i: u32, variant: &'static str) -> Result { + self.0.serialize_unit_variant(name, i, variant) + } + fn serialize_newtype_struct(self, name: &'static str, v: &T) -> Result { + self.0.serialize_newtype_struct(name, &Finite(v)) + } + fn serialize_newtype_variant(self, name: &'static str, i: u32, variant: &'static str, v: &T) -> Result { + self.0.serialize_newtype_variant(name, i, variant, &Finite(v)) + } + fn serialize_seq(self, len: Option) -> Result { + self.0.serialize_seq(len).map(Wrap) + } + fn serialize_tuple(self, len: usize) -> Result { + self.0.serialize_tuple(len).map(Wrap) + } + fn serialize_tuple_struct(self, name: &'static str, len: usize) -> Result { + self.0.serialize_tuple_struct(name, len).map(Wrap) + } + fn serialize_tuple_variant(self, name: &'static str, i: u32, variant: &'static str, len: usize) -> Result { + self.0.serialize_tuple_variant(name, i, variant, len).map(Wrap) + } + fn serialize_map(self, len: Option) -> Result { + self.0.serialize_map(len).map(Wrap) + } + fn serialize_struct(self, name: &'static str, len: usize) -> Result { + self.0.serialize_struct(name, len).map(Wrap) + } + fn serialize_struct_variant( + self, + name: &'static str, + i: u32, + variant: &'static str, + len: usize, + ) -> Result { + self.0.serialize_struct_variant(name, i, variant, len).map(Wrap) + } + fn is_human_readable(&self) -> bool { + self.0.is_human_readable() + } +} + +macro_rules! compound { + ($tr:ident, $m:ident) => { + impl ser::$tr for Wrap { + type Ok = S::Ok; + type Error = S::Error; + fn $m(&mut self, v: &T) -> Result<(), S::Error> { + self.0.$m(&Finite(v)) + } + fn end(self) -> Result { + self.0.end() + } + } + }; +} +compound!(SerializeSeq, serialize_element); +compound!(SerializeTuple, serialize_element); +compound!(SerializeTupleStruct, serialize_field); +compound!(SerializeTupleVariant, serialize_field); + +impl ser::SerializeMap for Wrap { + type Ok = S::Ok; + type Error = S::Error; + fn serialize_key(&mut self, k: &T) -> Result<(), S::Error> { + self.0.serialize_key(&Finite(k)) + } + fn serialize_value(&mut self, v: &T) -> Result<(), S::Error> { + self.0.serialize_value(&Finite(v)) + } + fn end(self) -> Result { + self.0.end() + } +} + +macro_rules! fields { + ($tr:ident) => { + impl ser::$tr for Wrap { + type Ok = S::Ok; + type Error = S::Error; + fn serialize_field(&mut self, key: &'static str, v: &T) -> Result<(), S::Error> { + self.0.serialize_field(key, &Finite(v)) + } + fn skip_field(&mut self, key: &'static str) -> Result<(), S::Error> { + self.0.skip_field(key) + } + fn end(self) -> Result { + self.0.end() + } + } + }; +} +fields!(SerializeStruct); +fields!(SerializeStructVariant); + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn non_finite_floats_are_written_as_zero_and_nothing_else_changes() { + #[derive(serde::Serialize)] + struct T { + a: f64, + b: Option, + c: Vec<(f32, f64)>, + d: Option, + e: std::collections::BTreeMap, + } + let t = T { a: f64::NAN, b: Some(f64::INFINITY), c: vec![(f32::NEG_INFINITY, 1.5)], d: None, e: [("k".to_string(), f64::NAN)].into() }; + let s = serde_json::to_string(&Finite(&t)).unwrap_or_default(); + assert_eq!(s, r#"{"a":0.0,"b":0.0,"c":[[0.0,1.5]],"d":null,"e":{"k":0.0}}"#); + } +} diff --git a/crates/deckcraft-format/src/lib.rs b/crates/deckcraft-format/src/lib.rs new file mode 100644 index 000000000..b59363c99 --- /dev/null +++ b/crates/deckcraft-format/src/lib.rs @@ -0,0 +1,226 @@ +//! The native `.deckcraft` package: a ZIP holding a stored `mimetype`, `presentation.json` (the +//! manifest: format name, version, generator and the presentation, minus media bytes), and +//! `media/` and `chart-parts/` binary members. Readers ignore unknown fields, so newer +//! files open in older builds with what those builds understand. +//! +//! DeckCraft owns the presentation model and its mapping: which media and chart parts become +//! members, and how a loaded deck is repaired. This crate owns the bytes: the package identity, +//! the manifest envelope, the NaN-safe JSON writer, the reader that opens files with NaN saved as +//! `null`, member names and compression, and the bounded member reads and their budgets. +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +mod finite; +mod manifest; + +#[cfg(test)] +mod tests; + +use std::io::{Cursor, Read}; + +pub use finite::Finite; +pub use manifest::parse_manifest; + +pub const MIMETYPE: &str = "application/vnd.storyteller.deckcraft+zip"; +/// Files written before the SlideCraft → DeckCraft rename (`.slidecraft`) are still read. +pub const LEGACY_MIMETYPE: &str = concat!("application/vnd.storyteller.", "slide", "craft+zip"); +pub const EXTENSION: &str = "deckcraft"; +/// The manifest's `format`. +pub const FORMAT: &str = "deckcraft"; +/// Format version written into the manifest. +pub const VERSION: u32 = 1; +/// The stored member naming the package type. +pub const MIMETYPE_ENTRY: &str = "mimetype"; +/// The manifest member. +pub const MANIFEST_ENTRY: &str = "presentation.json"; +/// Largest entry we will inflate (guards against zip bombs). +pub const MAX_ENTRY: u64 = 2 << 30; +/// All embedded media and chart sidecars together, inflated (storytold/deckcraft#393). +pub const MAX_TOTAL_MEDIA: u64 = 4 << 30; +/// Media items and chart sidecar parts one file may list. +pub const MAX_MEDIA_ITEMS: usize = 100_000; +/// Largest `presentation.json` we will inflate. The manifest holds no media bytes, so it is far +/// smaller than a media entry may be; inflating and parsing one is what dominates memory. +pub const MAX_MANIFEST: u64 = 256 << 20; + +const MEDIA_PREFIX: &str = "media/"; +const CHART_PARTS_PREFIX: &str = "chart-parts/"; + +/// What one load may inflate. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Budget { + /// Bytes all media and chart members may inflate to together. + pub total: u64, + /// Media items and chart parts the manifest may list. + pub items: usize, + /// Bytes `presentation.json` may inflate to. + pub manifest: u64, +} + +impl Default for Budget { + fn default() -> Self { + Budget { total: MAX_TOTAL_MEDIA, items: MAX_MEDIA_ITEMS, manifest: MAX_MANIFEST } + } +} + +impl Budget { + /// A media budget with the default manifest limit. + pub const fn media(total: u64, items: usize) -> Self { + Budget { total, items, manifest: MAX_MANIFEST } + } +} + +#[derive(Debug, thiserror::Error)] +pub enum FormatError { + #[error("not a DeckCraft file: {0}")] + NotOurs(String), + #[error("damaged file: {0}")] + Damaged(String), + #[error("write failed: {0}")] + Write(String), +} + +pub type Result = std::result::Result; + +/// `presentation.json`: the envelope around the app's presentation. +#[derive(serde::Serialize, serde::Deserialize)] +pub struct Manifest { + pub format: String, + pub version: u32, + pub generator: String, + pub presentation: T, +} + +/// The member holding media item `id`'s bytes. +pub fn media_entry(id: u32) -> String { + format!("{MEDIA_PREFIX}{id}") +} + +/// The member holding the `index`th chart part a save separated out. +pub fn chart_part_entry(index: usize) -> String { + format!("{CHART_PARTS_PREFIX}{index}") +} + +/// Whether `entry` is a name [`chart_part_entry`] gives, exactly: no other member, no path +/// tricks, no leading zeros. +pub fn is_chart_part_entry(entry: &str) -> bool { + entry.strip_prefix(CHART_PARTS_PREFIX).and_then(|s| s.parse::().ok()).is_some_and(|n| entry == chart_part_entry(n)) +} + +/// Whether a member of this content type is stored rather than deflated: it is compressed already. +pub fn is_compressed(content_type: &str) -> bool { + content_type.starts_with("image/jpeg") + || content_type.starts_with("image/png") + || content_type.starts_with("video/") + || content_type.starts_with("audio/mpeg") + || content_type.starts_with("audio/mp4") + || content_type.contains("zip") +} + +/// Does this look like a `.deckcraft` file? +pub fn sniff(bytes: &[u8]) -> bool { + bytes.starts_with(b"PK") && [MIMETYPE, LEGACY_MIMETYPE].iter().any(|m| bytes.windows(m.len()).take(200).any(|w| w == m.as_bytes())) +} + +/// A binary member of a package being written. +#[derive(Clone, Copy, Debug)] +pub struct Member<'a> { + /// Its name, from [`media_entry`] or [`chart_part_entry`]. + pub name: &'a str, + /// Its content type, which decides whether it is stored or deflated. + pub content_type: &'a str, + pub bytes: &'a [u8], +} + +/// The package: `mimetype` (stored), the manifest naming `generator` around `presentation` +/// (deflated, with non-finite numbers written as 0), then `members` in order. +pub fn write(presentation: &T, generator: &str, members: &[Member<'_>]) -> Result> { + use craft_zip::Compression::{Deflated, Stored}; + let w = |e: craft_zip::Error| FormatError::Write(e.to_string()); + let mut z = craft_zip::Writer::new(); + z.add(MIMETYPE_ENTRY, MIMETYPE.as_bytes(), Stored).map_err(w)?; + let m = Manifest { format: FORMAT.to_string(), version: VERSION, generator: generator.to_string(), presentation }; + let json = serde_json::to_vec(&Finite(&m)).map_err(|e| FormatError::Write(e.to_string()))?; + z.add(MANIFEST_ENTRY, &json, Deflated).map_err(w)?; + for member in members { + let compression = if is_compressed(member.content_type) { Stored } else { Deflated }; + z.add(member.name, member.bytes, compression).map_err(w)?; + } + z.finish().map_err(w) +} + +/// A package open for reading. Nothing is inflated until a member is read. +pub struct Package<'a> { + zip: zip::ZipArchive>, +} + +impl<'a> Package<'a> { + /// Index `bytes` as a ZIP. + pub fn open(bytes: &'a [u8]) -> Result { + let zip = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| FormatError::NotOurs(e.to_string()))?; + Ok(Package { zip }) + } + + /// Open `bytes` and read its manifest, inflating at most `max_manifest` bytes of it. Missing + /// `presentation.json` or a different `format` is [`FormatError::NotOurs`]. + pub fn open_manifest(bytes: &'a [u8], max_manifest: u64) -> Result<(Manifest, Self)> { + let mut package = Package::open(bytes)?; + if !package.contains(MANIFEST_ENTRY) { + return Err(FormatError::NotOurs(format!("no {MANIFEST_ENTRY}"))); + } + let json = package.read_max(MANIFEST_ENTRY, max_manifest)?; + let m: Manifest = parse_manifest(&json)?; + if m.format != FORMAT { + return Err(FormatError::NotOurs(m.format)); + } + Ok((m, package)) + } + + /// Whether the package has a member named `name`. + pub fn contains(&self, name: &str) -> bool { + self.zip.index_for_name(name).is_some() + } + + /// The size `name` declares, inflated; `None` when there is no such readable member. + pub fn declared_size(&mut self, name: &str) -> Option { + self.zip.by_name(name).ok().map(|f| f.size()) + } + + /// Read one member within [`MAX_ENTRY`]. + pub fn read(&mut self, name: &str) -> Result> { + self.read_max(name, MAX_ENTRY) + } + + /// Read one member, refusing it before inflating if it declares more than `max` bytes, and + /// stopping at `max` if it inflates further than it declared. + pub fn read_max(&mut self, name: &str, max: u64) -> Result> { + self.read_counted(name, max, &mut 0) + } + + /// [`Package::read_max`] within `left` (and [`MAX_ENTRY`]), charging `left` for every byte + /// inflated, including those inflated before a decode or CRC error, so a member that fails + /// late still spends the shared budget it used. + pub fn read_charged(&mut self, name: &str, left: &mut u64) -> Result> { + let mut spent = 0; + let out = self.read_counted(name, (*left).min(MAX_ENTRY), &mut spent); + *left = left.saturating_sub(spent); + out + } + + /// [`Package::read_max`], adding the bytes inflated to `spent` whether or not the read succeeds. + fn read_counted(&mut self, name: &str, max: u64, spent: &mut u64) -> Result> { + let f = self.zip.by_name(name).map_err(|e| FormatError::Damaged(format!("{name}: {e}")))?; + if f.size() > max { + return Err(FormatError::Damaged(format!("{name} is too large"))); + } + let mut out = Vec::with_capacity(usize::try_from(f.size().min(64 << 20)).unwrap_or(0)); + let mut f = f.take(max.saturating_add(1)); + let read = f.read_to_end(&mut out); + *spent = spent.saturating_add(out.len() as u64); + read.map_err(|e| FormatError::Damaged(format!("{name}: {e}")))?; + if out.len() as u64 > max { + return Err(FormatError::Damaged(format!("{name} is too large"))); + } + Ok(out) + } +} diff --git a/crates/deckcraft-format/src/manifest.rs b/crates/deckcraft-format/src/manifest.rs new file mode 100644 index 000000000..03f017ace --- /dev/null +++ b/crates/deckcraft-format/src/manifest.rs @@ -0,0 +1,99 @@ +//! Reading `presentation.json`, including files written with NaN or infinity saved as `null`. + +use serde::de::DeserializeOwned; + +use crate::{FormatError, Manifest, Result}; + +/// Most `null`s standing in for numbers that [`parse_manifest`] patches in one file, and the +/// candidates it tries for each. +const MAX_NULL_FIXES: usize = 256; +const MAX_TRIES_PER_FIX: usize = 32; +/// Bytes [`parse_manifest`] may re-parse in all while patching, so a hostile file full of `null`s +/// can't keep it busy: past this the file is reported damaged. +const MAX_REPARSE_BYTES: usize = 256 * 1024 * 1024; + +/// Where a parse failed, comparable: later is progress. +fn failure(src: &[u8]) -> std::result::Result, (String, (usize, usize))> { + serde_json::from_slice::>(src).map_err(|e| (e.to_string(), (e.line(), e.column()))) +} + +/// Parse `presentation.json`. serde_json writes a NaN or infinite number as `null`, which then +/// fails to read back as a number ("invalid type: null, expected f64"), so DeckCraft could save +/// a file it couldn't open. Each such `null` is read as 0 instead (the app then brings the +/// value into range); anything else that doesn't parse is damage. +/// +/// serde reports the failing `null` exactly, except inside tagged enums (buffered), where it +/// reports the end of the enclosing object: there the `null`s before that point are tried +/// nearest first, and a patch is kept only if the parse then gets further. (Files saved since +/// [`crate::Finite`] never contain such `null`s; this reads older ones.) +pub fn parse_manifest(json: &[u8]) -> Result> { + // Parse the inflated text as it is: only a file that fails on a `null` number is copied for + // patching (the copy is bounded by `MAX_MANIFEST`). + let mut last = match failure(json) { + Ok(m) => return Ok(m), + Err(f) => f, + }; + if !is_null_number(&last.0) { + return Err(FormatError::Damaged(last.0)); + } + let mut src = json.to_vec(); + let mut budget = MAX_REPARSE_BYTES; + 'fixes: for _ in 0..MAX_NULL_FIXES { + let (msg, pos) = &last; + if !is_null_number(msg) { + break; + } + let end = offset(&src, pos.0, pos.1).unwrap_or(src.len()); + let mut progressed = false; + let candidates: Vec = value_nulls_before(&src, end + 4).take(MAX_TRIES_PER_FIX).collect(); + for at in candidates { + let (Some(head), Some(tail)) = (src.get(..at), src.get(at.saturating_add(4)..)) else { continue }; + let Some(left) = budget.checked_sub(src.len()) else { break 'fixes }; + budget = left; + let mut next = Vec::with_capacity(src.len()); + next.extend_from_slice(head); + next.push(b'0'); + next.extend_from_slice(tail); + match failure(&next) { + Ok(m) => return Ok(m), + // The patch shortened the text by 3 bytes before the error: compare positions + // allowing for that. + Err(f) if f.1 > (pos.0, pos.1.saturating_sub(3)) => { + src = next; + last = f; + progressed = true; + break; + } + Err(_) => {} + } + } + if !progressed { + break; + } + } + Err(FormatError::Damaged(last.0)) +} + +/// Is this parse error a `null` where a number belongs (a NaN or infinity serde_json wrote)? +fn is_null_number(msg: &str) -> bool { + msg.starts_with("invalid type: null, expected f") || msg.starts_with("invalid type: null, expected a number") +} + +/// Byte offset of a 1-based `line`:`column`. +fn offset(src: &[u8], line: usize, column: usize) -> Option { + let mut start = 0; + for _ in 1..line { + start += src.get(start..)?.iter().position(|b| *b == b'\n')? + 1; + } + Some((start + column).min(src.len())) +} + +/// Starts of `null` tokens in value position (after `:`, `[` or `,`) before `end`, nearest first. +fn value_nulls_before(src: &[u8], end: usize) -> impl Iterator + '_ { + let end = end.min(src.len()); + (0..end.saturating_sub(3)).rev().filter(move |&i| { + src.get(i..i + 4) == Some(b"null") + && src[..i].iter().rev().find(|b| !b.is_ascii_whitespace()).is_some_and(|b| matches!(b, b':' | b'[' | b',')) + && src.get(i + 4).is_none_or(|b| !b.is_ascii_alphanumeric()) + }) +} diff --git a/crates/deckcraft-format/src/tests.rs b/crates/deckcraft-format/src/tests.rs new file mode 100644 index 000000000..8090d709a --- /dev/null +++ b/crates/deckcraft-format/src/tests.rs @@ -0,0 +1,235 @@ +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +use serde::{Deserialize, Serialize}; + +use super::*; + +/// A stand-in for an app's presentation: plain numbers, an optional number and a tagged enum +/// (whose `null`s serde reports only at the end of the enclosing object). +#[derive(Debug, Default, PartialEq, Serialize, Deserialize)] +struct Deck { + #[serde(default)] + width: f64, + #[serde(default)] + zoom: Option, + #[serde(default)] + shapes: Vec, +} + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +#[serde(tag = "kind")] +enum Shape { + Box { x: f64, y: f64 }, + Line { len: f64 }, +} + +fn package(members: &[Member<'_>]) -> Vec { + write(&Deck { width: 960.0, ..Default::default() }, "Test 1", members).unwrap() +} + +#[test] +fn a_package_holds_mimetype_then_manifest_then_members_in_order() { + let png = [0x89, b'P', b'N', b'G']; + let svg = b"".repeat(20); + let bytes = package(&[ + Member { name: &media_entry(7), content_type: "image/png", bytes: &png }, + Member { name: &media_entry(8), content_type: "image/svg+xml", bytes: &svg }, + Member { name: &chart_part_entry(0), content_type: "application/zip", bytes: b"PK" }, + ]); + assert!(sniff(&bytes)); + let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes.as_slice())).unwrap(); + assert_eq!(z.len(), 5); + let methods: Vec<_> = (0..z.len()) + .map(|i| { + let f = z.by_index(i).unwrap(); + (f.name().unwrap().into_owned(), f.compression()) + }) + .collect(); + use zip::CompressionMethod::{Deflated, Stored}; + assert_eq!( + methods, + [("mimetype", Stored), ("presentation.json", Deflated), ("media/7", Stored), ("media/8", Deflated), ("chart-parts/0", Stored)] + .map(|(n, m)| (n.to_string(), m)) + ); + // The mimetype is the first, stored member, readable in place. + assert_eq!(craft_zip::stored_mimetype(&bytes), Some(MIMETYPE.as_bytes())); + let (m, mut p) = Package::open_manifest::(&bytes, MAX_MANIFEST).unwrap(); + assert_eq!((m.format.as_str(), m.version, m.generator.as_str(), m.presentation.width), (FORMAT, VERSION, "Test 1", 960.0)); + assert_eq!(p.read(&media_entry(8)).unwrap(), svg); + assert_eq!(p.declared_size(&media_entry(7)), Some(4)); + assert_eq!(p.declared_size("media/99"), None); +} + +#[test] +fn the_manifest_is_compact_json_with_its_fields_in_order() { + let bytes = package(&[]); + let mut p = Package::open(&bytes).unwrap(); + let json = String::from_utf8(p.read(MANIFEST_ENTRY).unwrap()).unwrap(); + assert_eq!(json, r#"{"format":"deckcraft","version":1,"generator":"Test 1","presentation":{"width":960.0,"zoom":null,"shapes":[]}}"#); +} + +#[test] +fn non_finite_numbers_are_written_as_zero_and_read_back() { + let deck = Deck { width: f64::NAN, zoom: Some(f64::INFINITY), shapes: vec![Shape::Box { x: f64::NEG_INFINITY, y: 2.0 }] }; + let bytes = write(&deck, "t", &[]).unwrap(); + let (m, _) = Package::open_manifest::(&bytes, MAX_MANIFEST).unwrap(); + assert_eq!(m.presentation, Deck { width: 0.0, zoom: Some(0.0), shapes: vec![Shape::Box { x: 0.0, y: 2.0 }] }); +} + +/// Files written before [`Finite`] (plain serde_json: NaN as `null`) open too, including a +/// `null` inside a tagged enum, where serde reports only the enclosing object. +#[test] +fn older_manifests_with_null_numbers_parse() { + let cases: [(&str, Deck); 3] = [ + (r#"{"format":"deckcraft","version":1,"generator":"old","presentation":{"width":null}}"#, Deck::default()), + ( + r#"{"format":"deckcraft","version":1,"generator":"old","presentation":{"width":3,"shapes":[{"kind":"Box","x":null,"y":4}]}}"#, + Deck { width: 3.0, shapes: vec![Shape::Box { x: 0.0, y: 4.0 }], ..Default::default() }, + ), + ( + "{\n \"format\": \"deckcraft\",\n \"version\": 1,\n \"generator\": \"t\",\n \"presentation\": {\"shapes\": [{\"kind\": \"Line\", \"len\": null}]}\n}", + Deck { shapes: vec![Shape::Line { len: 0.0 }], ..Default::default() }, + ), + ]; + for (json, want) in cases { + let m = parse_manifest::(json.as_bytes()).unwrap_or_else(|e| panic!("{json}: {e}")); + assert_eq!(m.presentation, want, "{json}"); + } +} + +/// Only `null`s where numbers belong are patched: other damage still fails. +#[test] +fn other_manifest_damage_is_still_reported() { + let ok = br#"{"format": "deckcraft", "version": 1, "generator": "t", "presentation": {}}"#; + assert!(parse_manifest::(ok).is_ok()); + let bad = br#"{"format": null, "version": 1, "generator": "t", "presentation": {}}"#; + assert!(matches!(parse_manifest::(bad), Err(FormatError::Damaged(_)))); + assert!(parse_manifest::(&ok[..ok.len() - 3]).is_err()); +} + +#[test] +fn a_manifest_full_of_nulls_is_refused_within_bounds() { + let shapes = (0..2000).map(|_| r#"{"kind":"Box","x":1,"y":"not a number"}"#).collect::>().join(","); + let json = format!(r#"{{"format":"deckcraft","version":1,"generator":"t","presentation":{{"width":null,"shapes":[{shapes}]}}}}"#); + assert!(matches!(parse_manifest::(json.as_bytes()), Err(FormatError::Damaged(_)))); +} + +#[test] +fn what_is_not_a_package_is_not_ours() { + for bytes in [&b""[..], b"PK\x03\x04garbage", &[0u8; 1000]] { + assert!(matches!(Package::open_manifest::(bytes, MAX_MANIFEST), Err(FormatError::NotOurs(_)))); + } + // A ZIP without the manifest, and one from another app. + let mut z = craft_zip::Writer::new(); + z.add("mimetype", b"application/zip", craft_zip::Compression::Stored).unwrap(); + let no_manifest = z.finish().unwrap(); + let err = Package::open_manifest::(&no_manifest, MAX_MANIFEST).err().unwrap(); + assert_eq!(err.to_string(), "not a DeckCraft file: no presentation.json"); + let mut z = craft_zip::Writer::new(); + z.add(MANIFEST_ENTRY, br#"{"format":"other","version":1,"generator":"x","presentation":{}}"#, craft_zip::Compression::Deflated).unwrap(); + let other = z.finish().unwrap(); + let err = Package::open_manifest::(&other, MAX_MANIFEST).err().unwrap(); + assert_eq!(err.to_string(), "not a DeckCraft file: other"); + // A truncated package is an error, not a panic. + let mut b = package(&[]); + b.truncate(b.len() / 2); + assert!(Package::open_manifest::(&b, MAX_MANIFEST).is_err()); +} + +#[test] +fn legacy_slidecraft_packages_are_recognised() { + let mut z = craft_zip::Writer::new(); + z.add(MIMETYPE_ENTRY, LEGACY_MIMETYPE.as_bytes(), craft_zip::Compression::Stored).unwrap(); + assert!(sniff(&z.finish().unwrap())); + assert!(!sniff(b"PK\x03\x04 application/zip")); + assert!(!sniff(MIMETYPE.as_bytes())); +} + +#[test] +fn an_oversized_manifest_is_refused_before_inflating() { + let bytes = package(&[]); + let size = Package::open(&bytes).unwrap().declared_size(MANIFEST_ENTRY).unwrap(); + assert!(Package::open_manifest::(&bytes, size).is_ok()); + let err = Package::open_manifest::(&bytes, size - 1).err().unwrap(); + assert_eq!(err.to_string(), "damaged file: presentation.json is too large"); +} + +#[test] +fn a_member_that_inflates_past_its_declared_size_stops_at_the_limit() { + // Declared sizes are the file's word: a stored member whose header lies is still capped. + let data = vec![7u8; 100]; + let bytes = package(&[Member { name: &media_entry(1), content_type: "image/png", bytes: &data }]); + let mut p = Package::open(&bytes).unwrap(); + assert_eq!(p.read_max(&media_entry(1), 100).unwrap().len(), 100); + assert!(p.read_max(&media_entry(1), 99).is_err()); + let err = p.read("media/2").unwrap_err(); + assert!(matches!(err, FormatError::Damaged(ref m) if m.starts_with("media/2: ")), "{err}"); +} + +#[test] +fn corrupt_members_are_charged_for_what_they_inflated() { + let a = b"PICTURE-AAAAAAAAAAAA"; + let b = b"PICTURE-BBBBBBBBBBBB"; + let mut bytes = package(&[ + Member { name: &media_entry(1), content_type: "image/png", bytes: a }, + Member { name: &media_entry(2), content_type: "image/png", bytes: b }, + ]); + // Corrupt both stored members, so each fails its CRC only after inflating all its bytes. + for tag in [b"PICTURE-AAAA", b"PICTURE-BBBB"] { + let at = bytes.windows(tag.len()).position(|w| w == tag).unwrap(); + bytes[at] ^= 0xff; + } + let mut p = Package::open(&bytes).unwrap(); + let mut left = 30; + assert!(p.read_charged(&media_entry(1), &mut left).is_err()); + assert_eq!(left, 10, "a failed read still spends what it inflated"); + // The second fits a fresh budget on its own, but not what is left: refused before inflating. + let err = p.read_charged(&media_entry(2), &mut left).unwrap_err(); + assert_eq!(err.to_string(), "damaged file: media/2 is too large"); + assert_eq!(left, 10); +} + +#[test] +fn chart_part_names_are_exact() { + assert_eq!(chart_part_entry(3), "chart-parts/3"); + assert!(is_chart_part_entry("chart-parts/0")); + assert!(is_chart_part_entry("chart-parts/12")); + for not in ["media/1", "chart-parts/../0", "chart-parts/00", "chart-parts/-1", "chart-parts/", "chart-parts/+1", "chart-parts/1 "] { + assert!(!is_chart_part_entry(not), "{not}"); + } +} + +#[test] +fn compressed_content_types_are_stored() { + for stored in [ + "image/jpeg", + "image/png", + "video/mp4", + "audio/mpeg", + "audio/mp4", + "application/zip", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet+zip", + ] { + assert!(is_compressed(stored), "{stored}"); + } + for deflated in ["image/svg+xml", "image/gif", "audio/wav", "application/octet-stream", "text/plain"] { + assert!(!is_compressed(deflated), "{deflated}"); + } +} + +#[test] +fn duplicate_members_are_a_write_error() { + let err = write( + &Deck::default(), + "t", + &[Member { name: "media/1", content_type: "x", bytes: b"a" }, Member { name: "media/1", content_type: "x", bytes: b"b" }], + ) + .unwrap_err(); + assert!(matches!(err, FormatError::Write(_)), "{err}"); +} + +#[test] +fn the_default_budget_is_the_formats_limits() { + assert_eq!(Budget::default(), Budget { total: 4 << 30, items: 100_000, manifest: 256 << 20 }); + assert_eq!(Budget::media(10, 2), Budget { total: 10, items: 2, manifest: MAX_MANIFEST }); +} From 72f4465d027fff411850407b25c81fc71a37f6cd Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:26:49 +0100 Subject: [PATCH 150/150] metadata: cover big-endian, thumbnail and interop paths in the legacy EXIF summary --- crates/metadata/src/compat_orientation.rs | 55 +++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/crates/metadata/src/compat_orientation.rs b/crates/metadata/src/compat_orientation.rs index 22db7b414..9a4b90a99 100644 --- a/crates/metadata/src/compat_orientation.rs +++ b/crates/metadata/src/compat_orientation.rs @@ -179,4 +179,59 @@ mod tests { assert_eq!(summarize(b"II*\0\xff\xff\xff\xff"), ExifSummary::default()); assert_eq!(summarize(&[]), ExifSummary::default()); } + + /// Big-endian IFD: `entries` are (tag, type, count, value-or-offset), value left-justified. + fn be_ifd(out: &mut Vec, entries: &[(u16, u16, u32, [u8; 4])], next: u32) { + out.extend_from_slice(&(entries.len() as u16).to_be_bytes()); + for (tag, typ, count, value) in entries { + out.extend_from_slice(&tag.to_be_bytes()); + out.extend_from_slice(&typ.to_be_bytes()); + out.extend_from_slice(&count.to_be_bytes()); + out.extend_from_slice(value); + } + out.extend_from_slice(&next.to_be_bytes()); + } + + #[test] + fn big_endian_thumbnail_and_adobe_rgb_interop_hint() { + let (ifd1, exif, interop, rational, thumb) = (38u32, 68u32, 98u32, 164u32, 172u32); + let mut b = b"MM\0\x2a\0\0\0\x08".to_vec(); + be_ifd(&mut b, &[(0x0112, 3, 1, [0, 6, 0, 0]), (0x8769, 4, 1, exif.to_be_bytes())], ifd1); + be_ifd(&mut b, &[(0x0201, 4, 1, thumb.to_be_bytes()), (0x0202, 1, 1, [6, 0, 0, 0])], 0); + be_ifd(&mut b, &[(0xA001, 3, 1, [0xFF, 0xFF, 0, 0]), (0xA005, 13, 1, interop.to_be_bytes())], 0); + // Every tag-1 candidate is read; only the ASCII "R03" one matches. + be_ifd( + &mut b, + &[(1, 99, 1, [0; 4]), (1, 5, 1, rational.to_be_bytes()), (1, 4, 1, *b"R98\0"), (1, 3, 1, *b"R9\0\0"), (1, 2, 4, *b"R03\0")], + 0, + ); + assert_eq!(b.len(), rational as usize); + b.extend_from_slice(&[0, 0, 0, 1, 0, 0, 0, 2]); + b.extend_from_slice(&[0xFF, 0xD8, 0xFF, 0xD9, 0, 0]); + let s = summarize(&b); + assert_eq!(s, ExifSummary { orientation: Some(6), thumbnail: Some((thumb as usize, 6)), adobe_rgb_hint: true }); + // A thumbnail that is not a JPEG, or runs past the blob, is ignored. + let mut not_jpeg = b.clone(); + not_jpeg[thumb as usize] = 0; + assert_eq!(summarize(¬_jpeg).thumbnail, None); + assert_eq!(summarize(&b[..b.len() - 1]).thumbnail, None); + // Without ColorSpace 0xFFFF the interop index is not consulted. + let mut srgb = b.clone(); + srgb[(exif + 10) as usize] = 0; + assert!(!summarize(&srgb).adobe_rgb_hint); + } + + #[test] + fn rejects_bad_headers_types_and_oversized_tables() { + assert_eq!(summarize(b"XX\0\x2a\0\0\0\x08"), ExifSummary::default()); + assert_eq!(summarize(b"MM\0\x2b\0\0\0\x08"), ExifSummary::default()); + let mut huge = b"MM\0\x2a\0\0\0\x08".to_vec(); + huge.extend_from_slice(&5000u16.to_be_bytes()); + assert_eq!(summarize(&huge), ExifSummary::default()); + // Orientation stored with an unsupported type (SRATIONAL) is ignored. + let mut bytes = minimal_exif(6); + bytes[12..14].copy_from_slice(&10u16.to_le_bytes()); + assert_eq!(orientation(&bytes), 1); + assert_eq!(orientation(&[b"Exif\0\0".as_slice(), &minimal_exif(3)].concat()), 3); + } }