From ce819906a58fc8ce0482ed159f11c4d1e7fa2d3b Mon Sep 17 00:00:00 2001 From: Zach Shallbetter Date: Tue, 13 Oct 2026 12:28:12 -0700 Subject: [PATCH 0001/1159] craft-cms: public constructors for matrix/TRC and gray profiles (closes #61) - Add Curve::gamma, Curve::parametric, and Curve::from_table constructors with finite and range validation. - Add Profile::matrix_shaper accepting PCS D50-adapted matrices and populating chad for non-D50 white points. - Add Profile::from_source_matrix adapting unadapted source primaries to PCS D50 via Bradford transform. - Add Profile::gray constructor for monochrome display profiles. - Validate curve samples remain within [0.0, 1.0] and verify colorant matrix invertibility post-quantization. - Re-export math::Mat3 from the crate root. - Add independent ICC oracle validation tests against moxcms verifying both matrix conventions produce exact color transforms (diff <= 1). --- crates/cms/src/curve.rs | 31 +++++++ crates/cms/src/lib.rs | 3 +- crates/cms/src/profile.rs | 113 ++++++++++++++++++++++++++ crates/cms/tests/constructors.rs | 135 +++++++++++++++++++++++++++++++ crates/cms/tests/fogra39.rs | 9 ++- 5 files changed, 288 insertions(+), 3 deletions(-) create mode 100644 crates/cms/tests/constructors.rs diff --git a/crates/cms/src/curve.rs b/crates/cms/src/curve.rs index f50f2d663..9e2795cd8 100644 --- a/crates/cms/src/curve.rs +++ b/crates/cms/src/curve.rs @@ -26,6 +26,37 @@ pub fn srgb_trc() -> Curve { } impl Curve { + /// Creates a gamma curve `y = x^g`. Returns `Identity` if `g == 1.0` (within 1e-6). + pub fn gamma(g: f64) -> Result { + if !g.is_finite() || g <= 0.0 { + return Err(crate::CmsError::InvalidArgument("gamma must be positive and finite".into())); + } + if (g - 1.0).abs() < 1e-6 { Ok(Curve::Identity) } else { Ok(Curve::Gamma(g)) } + } + + /// Creates an ICC parametric curve (types 0 to 4). + pub fn parametric(kind: u16, p: [f64; 7]) -> Result { + if kind > 4 { + return Err(crate::CmsError::InvalidArgument(format!("parametric curve kind {kind} must be <= 4"))); + } + let count = Self::param_count(kind); + if !p.iter().take(count).all(|v| v.is_finite()) { + return Err(crate::CmsError::InvalidArgument("parametric curve parameters must be finite".into())); + } + Ok(Curve::Parametric { kind, p }) + } + + /// Creates a sampled lookup table curve over evenly spaced inputs in `[0, 1]`. + pub fn from_table(samples: Vec) -> Result { + if samples.len() < 2 { + return Err(crate::CmsError::InvalidArgument("sampled curve must contain at least 2 entries".into())); + } + if !samples.iter().all(|s| s.is_finite() && (0.0..=1.0).contains(s)) { + return Err(crate::CmsError::InvalidArgument("sampled curve values must be finite and within [0, 1]".into())); + } + Ok(Curve::Table(samples)) + } + /// Number of parameters stored for a parametric function type. pub fn param_count(kind: u16) -> usize { match kind { diff --git a/crates/cms/src/lib.rs b/crates/cms/src/lib.rs index 613d0db6b..31dc6b1ab 100644 --- a/crates/cms/src/lib.rs +++ b/crates/cms/src/lib.rs @@ -13,7 +13,7 @@ //! * [`Lut3d`] samples a display transform into a 3D texture for the GPU canvas. //! //! Implemented from the public ICC.1:2010 (v4.3) and ICC.1:2001-04 (v2) specifications, -//! CIE 15, and Adobe's published black point compensation paper. +//! CIE 15, and the ISO 18619 black point compensation standard. #![forbid(unsafe_code)] pub mod builtin; @@ -34,6 +34,7 @@ pub use clut::Clut; pub use curve::Curve; pub use gamut::GamutCheck; pub use lut3d::Lut3d; +pub use math::Mat3; pub use profile::{ColorSpace, Pcs, Profile, ProfileClass}; pub use transform::{SampleKind, Transform, TransformOptions, black_point, cached}; diff --git a/crates/cms/src/profile.rs b/crates/cms/src/profile.rs index ef8cbcee6..9c428179d 100644 --- a/crates/cms/src/profile.rs +++ b/crates/cms/src/profile.rs @@ -182,6 +182,119 @@ pub struct Profile { } impl Profile { + /// Builds a matrix/TRC display profile from an already PCS D50-adapted colorant matrix, + /// a white point, and per-channel tone reproduction curves. + /// + /// # Matrix Coordinate Space + /// In accordance with ICC.1:2010 §9.2.35–37, the colorant matrix tags (`rXYZ`, `gXYZ`, `bXYZ`) + /// stored in an ICC profile must be chromatically adapted to the D50 Profile Connection Space (PCS). + /// `pcs_matrix` must therefore be expressed relative to D50 (e.g. as read directly from ICC profile tags). + /// + /// If `white` is not D50, the profile's `chad` tag is populated with the Bradford chromatic adaptation + /// matrix mapping `white` to D50. + pub fn matrix_shaper(white: [f64; 3], pcs_matrix: Mat3, trc: [Curve; 3]) -> Result { + if !white.iter().all(|&c| c.is_finite() && c > 0.0) { + return Err(CmsError::InvalidArgument("white point must be positive and finite".into())); + } + if !pcs_matrix.iter().flatten().all(|&v| v.is_finite()) { + return Err(CmsError::InvalidArgument("matrix elements must be finite".into())); + } + if math::invert(&pcs_matrix).is_none() { + return Err(CmsError::InvalidArgument("colorant matrix must be invertible".into())); + } + + let d50 = math::d50_quantized(); + let is_d50 = (white[0] - math::D50[0]).abs() < 1e-4 && (white[1] - math::D50[1]).abs() < 1e-4 && (white[2] - math::D50[2]).abs() < 1e-4; + + let chad = if is_d50 { None } else { Some(math::bradford(white, math::D50)) }; + + // Quantize and normalize to ensure (1, 1, 1) maps exactly to PCS D50 + let mut q = pcs_matrix.map(|row| row.map(math::s15f16_round)); + for (i, row) in q.iter_mut().enumerate() { + let err = d50[i] - row.iter().sum::(); + let k = (0..3).max_by(|a, b| row[*a].abs().total_cmp(&row[*b].abs())).unwrap_or(0); + row[k] = math::s15f16_round(row[k] + err); + } + if math::invert(&q).is_none() { + return Err(CmsError::InvalidArgument("colorant matrix became singular after quantization/normalization".into())); + } + let chad_quantized = chad.map(|a| a.map(|row| row.map(math::s15f16_round))); + + Ok(Profile { + version: (4, 0x30), + class: ProfileClass::Display, + color_space: ColorSpace::Rgb, + pcs: Pcs::Xyz, + rendering_intent: Intent::RelativeColorimetric, + description: "Parametric Matrix/TRC".into(), + copyright: "".into(), + white_point: if is_d50 { d50 } else { white }, + black_point: None, + chad: chad_quantized, + matrix: Some(q), + trc: Some(trc), + gray_trc: None, + a2b: [None, None, None], + b2a: [None, None, None], + bytes: None, + hash: Default::default(), + }) + } + + /// Builds a matrix/TRC display profile from unadapted source primaries relative to `white`. + /// + /// The unadapted `source_matrix` (whose columns sum to `white`) is chromatically adapted to PCS D50 + /// using the Bradford transform before storage in the profile's matrix tags. + pub fn from_source_matrix(white: [f64; 3], source_matrix: Mat3, trc: [Curve; 3]) -> Result { + if !white.iter().all(|&c| c.is_finite() && c > 0.0) { + return Err(CmsError::InvalidArgument("white point must be positive and finite".into())); + } + if !source_matrix.iter().flatten().all(|&v| v.is_finite()) { + return Err(CmsError::InvalidArgument("matrix elements must be finite".into())); + } + if math::invert(&source_matrix).is_none() { + return Err(CmsError::InvalidArgument("colorant matrix must be invertible".into())); + } + + let is_d50 = (white[0] - math::D50[0]).abs() < 1e-4 && (white[1] - math::D50[1]).abs() < 1e-4 && (white[2] - math::D50[2]).abs() < 1e-4; + let pcs_matrix = if is_d50 { + source_matrix + } else { + let adapt = math::bradford(white, math::D50); + math::mul(&adapt, &source_matrix) + }; + + Self::matrix_shaper(white, pcs_matrix, trc) + } + + /// Builds a monochrome profile from a white point and tone reproduction curve. + pub fn gray(white: [f64; 3], trc: Curve) -> Result { + if !white.iter().all(|&c| c.is_finite() && c > 0.0) { + return Err(CmsError::InvalidArgument("white point must be positive and finite".into())); + } + let is_d50 = (white[0] - math::D50[0]).abs() < 1e-4 && (white[1] - math::D50[1]).abs() < 1e-4 && (white[2] - math::D50[2]).abs() < 1e-4; + + Ok(Profile { + version: (4, 0x30), + class: ProfileClass::Display, + color_space: ColorSpace::Gray, + pcs: Pcs::Xyz, + rendering_intent: Intent::RelativeColorimetric, + description: "Parametric Gray".into(), + copyright: "".into(), + white_point: if is_d50 { math::d50_quantized() } else { white }, + black_point: None, + chad: None, + matrix: None, + trc: None, + gray_trc: Some(trc), + a2b: [None, None, None], + b2a: [None, None, None], + bytes: None, + hash: Default::default(), + }) + } + /// Parses ICC profile bytes. pub fn parse(bytes: &[u8]) -> Result { let mut p = parse_profile(bytes)?; diff --git a/crates/cms/tests/constructors.rs b/crates/cms/tests/constructors.rs new file mode 100644 index 000000000..eecd8883e --- /dev/null +++ b/crates/cms/tests/constructors.rs @@ -0,0 +1,135 @@ +use craft_cms::{Builtin, Curve, Intent, Mat3, Profile, Transform}; + +#[test] +fn test_curve_constructors() { + // Gamma + let g22 = Curve::gamma(2.2).expect("valid gamma"); + assert_eq!(g22, Curve::Gamma(2.2)); + + let g10 = Curve::gamma(1.0).expect("gamma 1.0 is identity"); + assert_eq!(g10, Curve::Identity); + + let g_near = Curve::gamma(1.0 + 1e-7).expect("near-identity gamma normalized"); + assert_eq!(g_near, Curve::Identity); + + assert!(Curve::gamma(0.0).is_err()); + assert!(Curve::gamma(-2.2).is_err()); + assert!(Curve::gamma(f64::NAN).is_err()); + assert!(Curve::gamma(f64::INFINITY).is_err()); + + // Parametric + let p0 = Curve::parametric(0, [2.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]).expect("valid parametric type 0"); + assert_eq!(p0, Curve::Parametric { kind: 0, p: [2.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }); + + assert!(Curve::parametric(5, [0.0; 7]).is_err()); + assert!(Curve::parametric(0, [f64::NAN, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]).is_err()); + + // Sampled table + let table = Curve::from_table(vec![0.0, 0.5, 1.0]).expect("valid sampled table"); + assert_eq!(table, Curve::Table(vec![0.0, 0.5, 1.0])); + + assert!(Curve::from_table(vec![0.5]).is_err()); + assert!(Curve::from_table(vec![0.0, f32::NAN]).is_err()); + assert!(Curve::from_table(vec![0.0, -0.1, 1.0]).is_err()); + assert!(Curve::from_table(vec![0.0, 2.0]).is_err()); + assert!(Curve::from_table(vec![0.0, 1.0001]).is_err()); +} + +#[test] +fn test_profile_constructors() { + let srgb = Builtin::Srgb.profile(); + let white_d65 = [0.95047, 1.0, 1.08883]; + + // 1. Matrix shaper with already PCS D50-adapted matrix (e.g. read from ICC tags) + let pcs_d50_matrix: Mat3 = [[0.4360747, 0.3850649, 0.1430804], [0.2225045, 0.7168786, 0.0606169], [0.0139322, 0.0971045, 0.7141733]]; + let trc = [craft_cms::curve::srgb_trc(), craft_cms::curve::srgb_trc(), craft_cms::curve::srgb_trc()]; + let custom_from_pcs = Profile::matrix_shaper(white_d65, pcs_d50_matrix, trc.clone()).expect("matrix shaper from PCS D50"); + assert_eq!(custom_from_pcs.color_space, craft_cms::ColorSpace::Rgb); + assert!(custom_from_pcs.same_colors(srgb), "custom profile from PCS D50 matrix should match reference sRGB"); + + // 2. Matrix shaper from unadapted D65 source primaries (undergoes single Bradford adaptation) + let source_d65_matrix: Mat3 = [[0.4124564, 0.3575761, 0.1804375], [0.2126729, 0.7151522, 0.0721750], [0.0193339, 0.1191920, 0.9503041]]; + let custom_from_source = Profile::from_source_matrix(white_d65, source_d65_matrix, trc).expect("matrix shaper from source"); + assert!(custom_from_source.same_colors(srgb), "custom profile adapted from source primaries should match reference sRGB"); + + // Evaluate transform + let xform = Transform::new(&custom_from_pcs, srgb, Intent::RelativeColorimetric, true).expect("transform from custom RGB to sRGB"); + let mut out = [0.0f32; 3]; + xform.eval(&[0.5, 0.5, 0.5], &mut out).expect("eval"); + assert!((out[0] - 0.5).abs() < 1e-2); + assert!((out[1] - 0.5).abs() < 1e-2); + assert!((out[2] - 0.5).abs() < 1e-2); + + // Invalid matrix shaper inputs + assert!(Profile::matrix_shaper([0.0, 1.0, 1.0], pcs_d50_matrix, [Curve::Identity, Curve::Identity, Curve::Identity]).is_err()); + assert!(Profile::matrix_shaper([f64::NAN, 1.0, 1.0], pcs_d50_matrix, [Curve::Identity, Curve::Identity, Curve::Identity]).is_err()); + let singular: Mat3 = [[0.0; 3]; 3]; + assert!(Profile::matrix_shaper(white_d65, singular, [Curve::Identity, Curve::Identity, Curve::Identity]).is_err()); + + // Near-degenerate matrix that is invertible before quantization but becomes singular after + let d50 = [0.9642, 1.0, 0.8249]; + let near_degenerate: Mat3 = [[d50[0] - 0.000001, 0.000001, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, d50[2]]]; + assert!(Profile::matrix_shaper(d50, near_degenerate, [Curve::Identity, Curve::Identity, Curve::Identity]).is_err()); + + // Gray profile + let gray = Profile::gray(white_d65, Curve::gamma(2.2).expect("curve")).expect("gray profile"); + assert_eq!(gray.color_space, craft_cms::ColorSpace::Gray); + + let gray_to_srgb = Transform::new(&gray, srgb, Intent::RelativeColorimetric, true).expect("transform from gray to sRGB"); + let mut srgb_out = [0.0f32; 3]; + gray_to_srgb.eval(&[0.5], &mut srgb_out).expect("eval"); + assert!((srgb_out[0] - srgb_out[1]).abs() < 1e-3); + assert!((srgb_out[1] - srgb_out[2]).abs() < 1e-3); +} + +#[test] +fn test_matrix_constructors_against_independent_moxcms_oracle() { + use moxcms::{ColorProfile, Layout, TransformOptions as MoxOpts}; + + let srgb = Builtin::Srgb.profile(); + let white_d65 = [0.95047, 1.0, 1.08883]; + + // 1. PCS D50-adapted matrix profile + let pcs_d50_matrix: Mat3 = [[0.4360747, 0.3850649, 0.1430804], [0.2225045, 0.7168786, 0.0606169], [0.0139322, 0.0971045, 0.7141733]]; + let trc = [craft_cms::curve::srgb_trc(), craft_cms::curve::srgb_trc(), craft_cms::curve::srgb_trc()]; + let custom_from_pcs = Profile::matrix_shaper(white_d65, pcs_d50_matrix, trc.clone()).expect("matrix shaper from PCS D50"); + + // 2. Unadapted source matrix profile + let source_d65_matrix: Mat3 = [[0.4124564, 0.3575761, 0.1804375], [0.2126729, 0.7151522, 0.0721750], [0.0193339, 0.1191920, 0.9503041]]; + let custom_from_source = Profile::from_source_matrix(white_d65, source_d65_matrix, trc).expect("matrix shaper from source"); + + // Convert both to standard ICC bytes and parse with independent oracle moxcms + let mox_from_pcs = ColorProfile::new_from_slice(&custom_from_pcs.to_bytes()).expect("moxcms parse custom PCS profile"); + let mox_from_source = ColorProfile::new_from_slice(&custom_from_source.to_bytes()).expect("moxcms parse custom source profile"); + let mox_dst_srgb = ColorProfile::new_from_slice(&srgb.to_bytes()).expect("moxcms parse builtin sRGB"); + + let test_pixels: Vec = (0..512u32).flat_map(|i| [(i * 17 % 256) as u8, (i * 53 % 256) as u8, (i * 97 % 256) as u8]).collect(); + + // Verify 1: moxcms transforming custom_from_pcs -> builtin sRGB produces identity (diff <= 1) + let mox_xform_pcs = mox_from_pcs.create_transform_8bit(Layout::Rgb, &mox_dst_srgb, Layout::Rgb, MoxOpts::default()).unwrap(); + let mut mox_out_pcs = vec![0u8; test_pixels.len()]; + mox_xform_pcs.transform(&test_pixels, &mut mox_out_pcs).unwrap(); + let max_diff_pcs = test_pixels.iter().zip(&mox_out_pcs).map(|(a, b)| (*a as i32 - *b as i32).abs()).max().unwrap(); + assert!(max_diff_pcs <= 1, "moxcms PCS-adapted profile -> sRGB max diff was {max_diff_pcs}"); + + // Verify 2: moxcms transforming custom_from_source -> builtin sRGB produces identity (diff <= 1) + let mox_xform_src = mox_from_source.create_transform_8bit(Layout::Rgb, &mox_dst_srgb, Layout::Rgb, MoxOpts::default()).unwrap(); + let mut mox_out_src = vec![0u8; test_pixels.len()]; + mox_xform_src.transform(&test_pixels, &mut mox_out_src).unwrap(); + let max_diff_src = test_pixels.iter().zip(&mox_out_src).map(|(a, b)| (*a as i32 - *b as i32).abs()).max().unwrap(); + assert!(max_diff_src <= 1, "moxcms source-adapted profile -> sRGB max diff was {max_diff_src}"); + + // Verify 3: craft_cms and moxcms agree when converting from custom_from_source to DisplayP3 + let p3 = Builtin::DisplayP3.profile(); + let mox_dst_p3 = ColorProfile::new_from_slice(&p3.to_bytes()).expect("moxcms parse Display P3"); + let mox_xform_p3 = mox_from_source.create_transform_8bit(Layout::Rgb, &mox_dst_p3, Layout::Rgb, MoxOpts::default()).unwrap(); + let mut mox_p3_out = vec![0u8; test_pixels.len()]; + mox_xform_p3.transform(&test_pixels, &mut mox_p3_out).unwrap(); + + let ours_xform_p3 = Transform::new(&custom_from_source, p3, Intent::RelativeColorimetric, false).unwrap(); + let mut ours_p3_out = vec![0u8; test_pixels.len()]; + ours_xform_p3.convert_u8(&test_pixels, 3, &mut ours_p3_out, 3, false).unwrap(); + + let max_p3_diff = ours_p3_out.iter().zip(&mox_p3_out).map(|(a, b)| (*a as i32 - *b as i32).abs()).max().unwrap(); + assert!(max_p3_diff <= 1, "craft_cms vs moxcms custom source profile -> P3 max diff was {max_p3_diff}"); +} diff --git a/crates/cms/tests/fogra39.rs b/crates/cms/tests/fogra39.rs index f7fcdd0ca..f958ffc51 100644 --- a/crates/cms/tests/fogra39.rs +++ b/crates/cms/tests/fogra39.rs @@ -545,8 +545,13 @@ fn stand_in_separations_respect_the_ink_limit() { let t = Transform::new(Builtin::Srgb.profile(), cmyk, intent, true).unwrap(); let mut out = vec![0.0f32; N * N * N * 4]; t.convert_f32(&grid, 3, &mut out, 4, false).unwrap(); - let (i, max) = - out.chunks_exact(4).map(|c| c.iter().sum::()).enumerate().fold((0, 0.0f32), |m, (i, s)| if s > m.1 { (i, s) } else { m }); + let (i, max) = out + .as_chunks::<4>() + .0 + .iter() + .map(|c| c.iter().sum::()) + .enumerate() + .fold((0, 0.0f32), |m, (i, s)| if s > m.1 { (i, s) } else { m }); eprintln!("{name}, {intent:?}: max total ink {:.2} % at sRGB {:?}", max * 100.0, &grid[i * 3..i * 3 + 3]); assert!(max <= LIMIT, "{name}, {intent:?}: total ink {max} at sRGB {:?}", &grid[i * 3..i * 3 + 3]); From 27bb1ebf065a58fc415ac9eb05c04a834d3cbb38 Mon Sep 17 00:00:00 2001 From: totoz64 <47389794+totoz64@users.noreply.github.com> Date: Tue, 3 Nov 2026 11:19:25 +0100 Subject: [PATCH 0002/1159] CP: Adapt French proofing to shared Language packs Keep French locales in Pack(PackLocale), reusing the shared dictionary cache, affix reader and suggestion engine. Add French elisions, bounded accent and split tuning, conservative typography and Quebec punctuation rules. Validate displayed suggestion casing before OCONV and retain the original eligible form when an explicit target is excluded. Preserve historical text fixtures with three documented pre-existing upstream chain differences. Ported from WordCraft #3185; no dictionary or hyphenation data is bundled. --- crates/proof/README.md | 41 ++ crates/proof/src/affix.rs | 66 ++ crates/proof/src/affix_suggest.rs | 30 + crates/proof/src/fr/grammar.rs | 665 ++++++++++++++++++ crates/proof/src/fr/mod.rs | 181 +++++ crates/proof/src/fr/suggest.rs | 81 +++ crates/proof/src/fr/test_dict.rs | 234 ++++++ crates/proof/src/hunspell.rs | 17 +- crates/proof/src/lib.rs | 5 + crates/proof/src/pack_lang.rs | 8 +- crates/proof/tests/affix_compatibility.rs | 92 +++ .../fixtures/affix_compatibility.compact | 7 + .../fixtures/affix_compatibility.expected | 497 +++++++++++++ .../fixtures/affix_compatibility_cases.rs | 61 ++ crates/proof/tests/french_dictionary.rs | 107 +++ crates/proof/tests/french_packs.rs | 124 ++++ 16 files changed, 2210 insertions(+), 6 deletions(-) create mode 100644 crates/proof/src/fr/grammar.rs create mode 100644 crates/proof/src/fr/mod.rs create mode 100644 crates/proof/src/fr/suggest.rs create mode 100644 crates/proof/src/fr/test_dict.rs create mode 100644 crates/proof/tests/affix_compatibility.rs create mode 100644 crates/proof/tests/fixtures/affix_compatibility.compact create mode 100644 crates/proof/tests/fixtures/affix_compatibility.expected create mode 100644 crates/proof/tests/fixtures/affix_compatibility_cases.rs create mode 100644 crates/proof/tests/french_dictionary.rs create mode 100644 crates/proof/tests/french_packs.rs diff --git a/crates/proof/README.md b/crates/proof/README.md index 970670143..b4b4b5ba8 100644 --- a/crates/proof/README.md +++ b/crates/proof/README.md @@ -16,6 +16,7 @@ craft-proof = { git = "https://github.com/storytold/craft-libs", rev = " | English (en-US, en-CA, en-AU, en-GB and the British-spelling Englishes such as en-NZ) | SCOWL 2026 Hunspell dictionary per English + `en-extra.txt`, dialect policy (`en-variants.txt`); a dictionaries-folder English first; without SCOWL the installed Hunspell English, else the built-in Moby list ([below](#english-is-always-checked)) | yes | Moby list, our patterns, heuristic | | Polish, Portuguese, Italian | the installed Hunspell dictionary | yes | TeX patterns (embedded, or a Language pack) | | Croatian, Turkish, Russian, Czech, Indonesian, Spanish (es, es-MX, es-419, es-UY) | the installed Hunspell dictionary (Russian also `ru-extra.txt`) | yes | Turkish and Russian rules; English fallback otherwise | +| French (fr-FR, fr-CA and other French pack locales) | the installed Language pack or Hunspell dictionary; dictionary-driven elisions and accent-oriented suggestions | conservative typography and basic checks, with Quebec punctuation spacing in fr-CA | the Language pack (generic fr fallback) | | German | — | — | TeX patterns (embedded, or a Language pack) | | Bulgarian | — | — | syllable rules | @@ -41,6 +42,12 @@ application data (`%LOCALAPPDATA%\WordCraft\dictionaries` and `%APPDATA%\WordCra `~/Library/Application Support/WordCraft/dictionaries`, `$XDG_DATA_HOME/wordcraft/dictionaries`), then the usual Hunspell and LibreOffice locations. None on the web. +French remains a `ProofLang::Pack` locale: use its full `tag()` for settings and issue identity. +The shared pack loader keeps regional dictionary overrides, fallback to `fr`, reloads and its +memory budget; no separate French dictionary slot or embedded French patterns are added. +Only French packs receive the French spelling tuning and typography; other pack languages +keep their existing behavior. French agreement and conjugation are outside these checks. + State is process-wide: `add_word`, `ignore_word`, `set_dictionaries_folder`, `load_user_dictionary`/`save_user_dictionary`. The crate reads no document model; callers pass plain text and language spans. @@ -221,6 +228,40 @@ CI: `.github/workflows/proof.yml`. storytold/craft-libs#30/#31 by brzuchal (Michał Marcin Brzuchalski) started the shared crate. +### French adaptation validation + +The French rules and original synthetic fixtures are adapted from +[totoz64’s WordCraft contribution #3185](https://github.com/storytold/wordcraft/pull/3185). +They use the shared affix reader and Language pack cache; WCAFF1 remains deliberately rejected. +The historical text fixtures retain their original expected data. Three observations for +Indonesian `bermainkanlah` are explicitly updated in the compatibility test because the +shared reader already supports that prefix plus two suffixes. This is a finite regression +check, not a universal compatibility claim. + +The ordinary tests cover French packs, regional overrides and reloads, options, user words, +NFC/NFD lookup with original ranges, suggestions and pack hyphenation. The opt-in real French +dictionary gate fails when its input is missing; it never silently skips its assertions: + +```sh +WORDCRAFT_DICTIONARIES=/absolute/isolated/french-dictionary cargo test -p craft-proof --test french_dictionary -- --ignored +``` + +For a reproducible real-dictionary run, use Dicollecte 7.7 from LibreOffice/dictionaries +revision `67032ee957631908098a72edf48dc81cac27e369`: +[fr.aff](https://raw.githubusercontent.com/LibreOffice/dictionaries/67032ee957631908098a72edf48dc81cac27e369/fr_FR/dictionaries/fr.aff) and +[fr.dic](https://raw.githubusercontent.com/LibreOffice/dictionaries/67032ee957631908098a72edf48dc81cac27e369/fr_FR/dictionaries/fr.dic). +Verify SHA-256 before invoking the gate (the test checks location and behavior, not hashes): + +| File | SHA-256 | +|---|---| +| fr.aff | `c176610cd5dc4846806a65ddd029f422d87978bf58f224aa44222662a16a2de5` | +| fr.dic | `b78a868e31dd6e373b6c3217969afb898a9acde828a5e7ef97308da42218c88c` | + +Use a dedicated directory containing only the intended `fr.aff`/`fr.dic` pair (or explicit +regional pairs for both regions). The gate requires more than 50,000 dictionary entries and +checks the actual source folder, spelling, accent/elision suggestions, regional dispatch, +combining-mark ranges and hyphenated compounds. No real dictionary is committed here. + ## Owners WordCraft maintainers: echelon, dylanmtaylor, aiongg. diff --git a/crates/proof/src/affix.rs b/crates/proof/src/affix.rs index 0df68665b..32de07659 100644 --- a/crates/proof/src/affix.rs +++ b/crates/proof/src/affix.rs @@ -2212,6 +2212,37 @@ impl AffixDict { self.check_except(w, self.excluded(true)) } + /// Validate a collected suggestion in its displayed case, before OCONV. Only identity, + /// first-letter and uppercase variants are allowed (including uppercase that keeps sharp s). + /// An exact target excluded from suggestions cannot be rescued by a lowercase fallback. + /// This preserves the checker's homonym, compound and CHECKSHARPS rules without scanning + /// stems or changing which words the spelling checker accepts. + pub(crate) fn can_suggest_cased(&self, original: &str, changed: &str) -> bool { + if original.len() > MAX_INPUT_BYTES || changed.len() > MAX_INPUT_BYTES { + return false; + } + if changed != original + && changed != capitalize(original) + && changed != original.to_uppercase() + && changed != original.split('ß').map(str::to_uppercase).collect::>().join("ß") + { + return false; + } + let target = self.normalize(changed); + if self.is_forbidden(&target) { + return false; + } + // The exact simple-word path includes affixes; asking with no exclusions detects a + // target whose only derivations carry NOSUGGEST, SUBSTANDARD or FORBIDDENWORD. Spend + // the same shared suggestion budget as the normal checker, and abstain if it runs out. + match self.simple_word_budgeted(&target, Excluded::default(), true) { + None => return false, + Some(true) if !self.simple_word(&target, self.excluded(true)) => return false, + _ => {} + } + self.can_suggest(changed) + } + /// Whether `w` is a word only through a `KEEPCASE` stem, so it is correct in this letter case /// alone (`km` from `km/K`, which `Km` and `KM` are not). pub(crate) fn case_locked(&self, w: &str) -> bool { @@ -3298,6 +3329,41 @@ pub(crate) mod tests { assert!(d.check("ASSASSASSASSASS") && !d.check(&"SS".repeat(60))); } + #[test] + fn displayed_casing_rejects_exact_exclusions_before_case_fallback() { + for flag in ["N", "F", "S"] { + let aff = "SET UTF-8\nNOSUGGEST N\nFORBIDDENWORD F\nSUBSTANDARD S\nSFX A Y 1\nSFX A 0 s .\nSFX B Y 1\nSFX B 0 S .\n"; + let dic = format!("5\nnormal/A\nNORMAL/{flag}B\nmixCase\npour\nkm/K\n"); + let d = AffixDict::from_hunspell(&format!("{aff}KEEPCASE K\n"), &dic, &[]).unwrap(); + for (original, changed) in [("normal", "NORMAL"), ("normals", "NORMALS")] { + assert!(!d.can_suggest_cased(changed, changed), "{flag}: {changed}"); + assert!(!d.can_suggest_cased(original, changed), "{flag}: {original} → {changed}"); + } + assert!(d.can_suggest_cased("normal", "Normal")); + assert!(d.can_suggest_cased("mixCase", "MIXCASE")); + assert!(d.can_suggest_cased("km", "km") && !d.can_suggest_cased("km", "KM")); + assert!(!d.can_suggest_cased("normal", "pour"), "no arbitrary replacement"); + let back = AffixDict::from_bytes(&d.to_bytes().unwrap()).unwrap(); + assert!(!back.can_suggest_cased("normal", "NORMAL")); + } + } + + #[test] + fn displayed_casing_uses_normalization_and_native_homonym_policy() { + let aff = "SET UTF-8\nNOSUGGEST N\nSUBSTANDARD S\nIGNORE ()\nICONV 1\nICONV Q NORMAL\n"; + for flag in ["N", "S"] { + let d = AffixDict::from_hunspell(aff, &format!("3\nnormal\nNORMAL/{flag}\nNORMAL\n"), &[]).unwrap(); + // Native homonyms carry exclusions: a second entry does not erase NOSUGGEST. + assert!(!d.contains_suggestible("NORMAL")); + assert!(!d.can_suggest_cased("(normal)", "(NORMAL)")); + assert!(!d.can_suggest_cased("Q", "Q")); + } + let plain = AffixDict::from_hunspell(aff, "3\nnormal\nNORMAL\nNORMAL\n", &[]).unwrap(); + assert!(plain.can_suggest_cased("normal", "NORMAL")); + assert!(plain.can_suggest_cased("Q", "Q")); + assert!(!plain.can_suggest_cased(&"x".repeat(MAX_INPUT_BYTES + 1), "NORMAL")); + } + #[test] fn oconv_converts_suggestions_only() { // Answers as Hunspell 1.7 gives them on the same files: `kbt` → `k’t`. diff --git a/crates/proof/src/affix_suggest.rs b/crates/proof/src/affix_suggest.rs index 3b498542c..68336de16 100644 --- a/crates/proof/src/affix_suggest.rs +++ b/crates/proof/src/affix_suggest.rs @@ -426,6 +426,18 @@ pub(crate) fn suggest_in(d: &AffixDict, word: &str, max: usize, t: &Tuning) -> V _ => vec![text.clone()], }; for t in recased { + let eligible = |candidate: &str| { + let original: Vec<_> = text.split(' ').collect(); + let changed: Vec<_> = candidate.split(' ').collect(); + original.len() == changed.len() && original.iter().zip(changed).all(|(a, b)| d.can_suggest_cased(a, b)) + }; + let t = if eligible(&t) { + t + } else if eligible(&text) { + text.clone() + } else { + continue; + }; // Shown as the dictionary's OCONV table writes it (`'` → `’` in French). let t = d.output(&t).into_owned(); if t != word && !out.contains(&t) { @@ -763,6 +775,24 @@ mod tests { assert_eq!(suggest_in(&d, "Piesx", 4, &Tuning::DEFAULT).first().map(String::as_str), Some("Pies")); } + #[test] + fn recasing_keeps_valid_original_when_exact_target_is_excluded() { + for flag in ["N", "F", "S"] { + let aff = "SET UTF-8\nNOSUGGEST N\nFORBIDDENWORD F\nSUBSTANDARD S\nTRY normal\n"; + let d = AffixDict::from_hunspell(aff, &format!("2\nnormal\nNORMAL/{flag}\n"), &[]).unwrap(); + let got = suggest_in(&d, "NORMALL", 4, &Tuning::DEFAULT); + assert!(got.iter().any(|w| w == "normal"), "{flag}: {got:?}"); + assert!(!got.iter().any(|w| w == "NORMAL"), "{flag}: {got:?}"); + let split = AffixDict::from_hunspell(aff, &format!("3\nnormal\nNORMAL/{flag}\npour\n"), &[]).unwrap(); + let got = suggest_in(&split, "NORMALPOUR", 4, &Tuning::DEFAULT); + assert!(got.iter().any(|w| w == "normal pour"), "{flag}: {got:?}"); + assert!(!got.iter().any(|w| w == "NORMAL POUR"), "{flag}: {got:?}"); + } + let d = AffixDict::from_hunspell("NOSUGGEST N\n", "3\nnormal\nNORMAL/N\npour\n", &[]).unwrap(); + assert!(!d.can_suggest_cased("normal", "NORMAL")); + assert!(!d.can_suggest_cased("normal", "pour")); + } + /// A language's tables come after its dictionary's, a TRY alphabet stands in for a missing TRY /// line, and a split rule decides which missing spaces are offered. #[test] diff --git a/crates/proof/src/fr/grammar.rs b/crates/proof/src/fr/grammar.rs new file mode 100644 index 000000000..1d5449b1f --- /dev/null +++ b/crates/proof/src/fr/grammar.rs @@ -0,0 +1,665 @@ +//! French grammar and typography checks: cheap, rule-based and tuned to stay quiet when unsure +//! (a missed mistake costs less than a wrong mark on correct text). Messages are in French. +//! +//! - **Space before `;` `!` `?` `:`**: French typography puts a non-breaking space there — a +//! narrow one (U+202F) before `;` `!` `?`, a full one (U+00A0) before `:`. Only a missing +//! space is flagged (`Bonjour!`): an ordinary one stays quiet until typing turns it into a +//! non-breaking one (no French AutoFormat yet). Quebec French (`fr-CA`) has no space before +//! `;` `!` `?`, only before `:`. Addresses (`https://…`) and times (`10:30`) are left alone. +//! - **Space inside `« »`**: a missing one after `«` or before `»` (`«oui»`) → U+00A0. +//! - **Straight double quotes** around a phrase (`"bonjour"`) → `« bonjour »`; inch marks +//! (`27"`) and unbalanced quotes are left alone. +//! - **Repeated word** (`le le`, `que que`), except repetitions French makes on purpose +//! (`nous nous lavons`, `vous vous trompez`, `si si`, interjections) and a proper noun after +//! the same lower-case word (`de De…`). +//! - **Capital letter at the start of a sentence** after `.` `!` `?`, knowing French +//! abbreviations (`etc.`, `M.`, `p.`, `cf.`), initials, numbers, ellipses, addresses, +//! dialogue (`« Tu viens ? » demanda-t-elle`), inversions (`Quoi ? dit-il`) and exclamations +//! after an interjection (`Ah ! que c'est beau`). +//! - **Elision** (`de avoir` → `d’avoir`, `je ai` → `j’ai`, `le homme` → `l’homme`) for `le`, +//! `la`, `de`, `je`, `me`, `te`, `se`, `ne`, `que` before a vowel. Before an `h` only for words +//! known to have a mute `h`, so aspirated ones (`le hibou`, `la hache`, `le héros`) stay quiet; +//! never before a proper noun, `onze`, `oui`, `y…` words (`le yaourt`) or `la une`. +//! +//! No agreement or conjugation rule, and no English rule (such as `a`/`an`), applies to French +//! text. + +use crate::{Issue, IssueKind, ProofLang, words}; + +/// Non-breaking space, before `:` and inside `« »`. +const NBSP: char = '\u{A0}'; +/// Narrow non-breaking space, before `;` `!` `?`. +const NNBSP: char = '\u{202F}'; + +/// Words French repeats on purpose: pronominal verbs (`nous nous lavons`), emphasis, +/// interjections, onomatopoeia. +const ALLOWED_REPEATS: &[&str] = &[ + "nous", "vous", "si", "oui", "non", "très", "bon", "vite", "là", "ah", "oh", "eh", "ha", "hé", "ho", "hi", "bla", "chut", "toc", "pan", "bravo", + "merci", "allez", "encore", "beaucoup", "fort", "tchin", "bye", "couci", "tam", "miam", "glou", +]; + +/// Abbreviations written with a final period after which a sentence goes on in lower case +/// (lower case, without the period). +const ABBREVIATIONS: &[&str] = &[ + "etc", "cf", "ex", "env", "av", "apr", "mm", "mme", "mmes", "mlle", "mlles", "mgr", "dr", "pr", "me", "st", "ste", "sts", "pp", "vol", "chap", + "fig", "éd", "coll", "trad", "réf", "tél", "art", "al", "op", "cit", "ibid", "id", "resp", "min", "max", "sq", "sqq", "suiv", "hab", "janv", + "févr", "avr", "juil", "sept", "oct", "nov", "déc", "bd", "boul", "cie", "éts", "vve", "nb", "ps", "no", "nos", "vs", "qqch", "qqn", "ca", + "approx", "prof", "dir", "sté", "dépt", "adj", "adv", "fém", "masc", "sing", "plur", "à-d", "ms", "mss", "éds", "trim", "hôp", +]; + +/// Words after which `!` or `?` doesn't end the sentence (`Ah ! que c'est beau`, `Eh bien ? +/// et alors`). +const INTERJECTIONS: &[&str] = &[ + "ah", "oh", "eh", "hé", "hein", "hélas", "bah", "ô", "aïe", "ouf", "oups", "quoi", "comment", "ha", "ho", "bon", "tiens", "allons", "zut", + "chut", "bravo", "hourra", "dame", "bien", "là", "oui", "non", "pardon", "voyons", +]; + +/// Pronouns ending an inversion (`dit-il`, `demanda-t-elle`): after one, a `!` or `?` doesn't +/// end the sentence (`Quoi ? dit-il`). +const INVERTED: &[&str] = &["-il", "-elle", "-on", "-ils", "-elles", "-je", "-tu", "-nous", "-vous"]; + +/// Speech verbs of a dialogue tag: after one, a `!` or `?` doesn't end the sentence (`— Tu +/// viens ? demanda le voisin`). +const SPEECH_VERBS: &[&str] = &[ + "dit", + "demanda", + "répondit", + "cria", + "murmura", + "ajouta", + "reprit", + "fit", + "lança", + "souffla", + "répliqua", + "interrogea", + "hurla", + "poursuivit", + "continua", + "déclara", + "expliqua", + "protesta", + "soupira", + "chuchota", + "insista", + "s'écria", + "s’écria", + "s'exclama", + "s’exclama", + "objecta", + "conclut", + "dis", + "dit-on", +]; + +/// The words that elide before a vowel, and their elided form. +const ELIDING: &[(&str, &str)] = + &[("le", "l"), ("la", "l"), ("de", "d"), ("je", "j"), ("me", "m"), ("te", "t"), ("se", "s"), ("ne", "n"), ("que", "qu")]; + +/// Words starting with a vowel that don't take an elision (`le onze`, `le oui`, `de ouate`). +const NO_ELISION: &[&str] = &["onze", "onzième", "onzièmes", "oui", "ouistiti", "ouistitis", "ouate", "ouates", "uhlan", "uhlans"]; + +/// Words with a mute `h`, which take an elision (`l’homme`, `d’habitude`, `j’habite`). Only these +/// are flagged: an `h` word outside the list may be aspirated (`le hibou`, `la hache`, `le +/// héros`), and staying quiet is the safe side. +const MUTE_H: &[&str] = &[ + "homme", + "hommes", + "heure", + "heures", + "histoire", + "histoires", + "hiver", + "hivers", + "hôtel", + "hôtels", + "herbe", + "herbes", + "habitude", + "habitudes", + "habitant", + "habitants", + "habit", + "habits", + "hôpital", + "hôpitaux", + "humain", + "humains", + "humanité", + "honneur", + "honneurs", + "horizon", + "horizons", + "huile", + "huiles", + "hypothèse", + "hypothèses", + "harmonie", + "héritage", + "héroïne", + "héroïnes", + "hésitation", + "hommage", + "hommages", + "horaire", + "horaires", + "horreur", + "horreurs", + "humeur", + "humour", + "hygiène", + "hymne", + "habiter", + "hériter", + "hésiter", + "habiller", + "honorer", + "habite", + "habites", + "habitent", + "habitais", + "habitait", + "hésite", + "hésites", + "hésitent", + "hésitais", + "hésitait", + "habille", + "habillent", + "honore", + "hier", + "hôte", + "hôtes", + "hôtesse", + "humidité", + "haleine", + "hebdomadaire", + "hectare", + "hectares", + "hélicoptère", + "hémisphère", + "heureux", + "heureuse", + "hippopotame", + "hirondelle", + "honnête", + "honnêteté", + "huître", + "huîtres", + "humble", + "humilité", + "hydrogène", + "hypocrite", + "horloge", + "hôtellerie", + "hérédité", + "historien", + "historique", + "humanitaire", + "hormone", + "hormones", + "hospitalité", +]; + +fn issue(start: usize, end: usize, message: String, suggestions: Vec) -> Issue { + Issue { start, end, kind: IssueKind::Grammar, message, suggestions, lang: ProofLang::from_tag(Some("fr-FR")) } +} + +/// Is `c` a space that doesn't break (or a thin space some writers use instead)? +fn is_fixed_space(c: char) -> bool { + matches!(c, '\u{A0}' | '\u{202F}' | '\u{2007}' | '\u{2009}' | '\u{2060}') +} + +/// The byte range of the whitespace-delimited token around byte `i` of `text`. +fn token_bounds(text: &str, i: usize) -> (usize, usize) { + let start = text.get(..i).and_then(|t| t.char_indices().rev().find(|(_, c)| c.is_whitespace())).map_or(0, |(k, c)| k + c.len_utf8()); + let end = text.get(i..).and_then(|t| t.find(char::is_whitespace)).map_or(text.len(), |k| i + k); + (start, end) +} + +/// The whitespace-delimited token around byte `i` of `text`. +fn token_at(text: &str, i: usize) -> &str { + let (start, end) = token_bounds(text, i); + text.get(start..end).unwrap_or("") +} + +/// Is `token` a web or e-mail address, a path or an HTML entity, where punctuation isn't prose? +fn is_address(token: &str) -> bool { + token.contains("://") || token.contains('@') || token.starts_with("www.") || token.contains('&') || token.contains('\\') +} + +/// `«` + text + `»` in a message, with French spacing. +fn quoted(s: &str) -> String { + format!("«{NBSP}{s}{NBSP}»") +} + +/// `;` `!` `?` `:` without the non-breaking space French puts before them. +/// Quebec typography (`quebec`) puts no space before `;` `!` `?`, only before `:`. +fn high_punctuation(text: &str, quebec: bool, v: &mut Vec) { + let mut token: Option<(usize, usize, bool)> = None; + for (i, p) in text.char_indices() { + if !matches!(p, ';' | ':' | '!' | '?') || (quebec && p != ':') { + continue; + } + let before = text.get(..i).and_then(|t| t.chars().next_back()); + let after = text.get(i + p.len_utf8()..).and_then(|t| t.chars().next()); + // One look per token, so a long run of marks stays linear. + let address = match token { + Some((start, end, address)) if start <= i && i < end => address, + _ => { + let (start, end) = token_bounds(text, i); + let address = is_address(text.get(start..end).unwrap_or("")); + token = Some((start, end, address)); + address + } + }; + if address { + continue; + } + let (space, kind) = if p == ':' { (NBSP, "insécable") } else { (NNBSP, "fine insécable") }; + let message = format!("Espace {kind} attendue avant {}", quoted(&p.to_string())); + let fix = format!("{space}{p}"); + // Only a missing space is flagged: an ordinary one stays quiet until typing turns it into + // a non-breaking one by itself (French AutoFormat). + if before.is_some_and(char::is_alphabetic) { + // `10:30`, `a:b`, `C:` and `x;y` aren't prose: the mark must end the word. + let ends = match p { + ':' => after.is_none_or(char::is_whitespace), + _ => after.is_none_or(|a| a.is_whitespace() || matches!(a, '!' | '?' | '»' | '"' | '”' | ')')), + }; + if ends { + v.push(issue(i, i + p.len_utf8(), message, vec![fix])); + } + } + } +} + +/// `«` and `»` without a non-breaking space inside. +fn guillemets(text: &str, v: &mut Vec) { + for (i, c) in text.char_indices() { + let len = c.len_utf8(); + match c { + '«' => { + let mut next = text.get(i + len..).unwrap_or("").chars(); + let message = format!("Espace insécable attendue après {}", quoted("«")); + if next.next().is_some_and(char::is_alphanumeric) { + v.push(issue(i, i + len, message, vec![format!("«{NBSP}")])); + } + } + '»' => { + let prev = text.get(..i).unwrap_or("").chars().next_back(); + let message = format!("Espace insécable attendue avant {}", quoted("»")); + if prev.is_some_and(|n| n.is_alphanumeric() || matches!(n, '.' | '!' | '?' | '…')) { + v.push(issue(i, i + len, message, vec![format!("{NBSP}»")])); + } + } + _ => {} + } + } +} + +/// `"…"` around a phrase, where French uses `« … »`. +fn straight_quotes(text: &str, v: &mut Vec) { + let quotes: Vec = text.match_indices('"').map(|(i, _)| i).collect(); + // An odd count leaves no telling which quote opens and which closes. + if !quotes.len().is_multiple_of(2) { + return; + } + for pair in quotes.chunks(2) { + let [a, b] = pair else { continue }; + let (Some(inner), Some(before), Some(after)) = (text.get(a + 1..*b), text.get(..*a), text.get(b + 1..)) else { continue }; + let opens = before.chars().next_back().is_none_or(|c| c.is_whitespace() || matches!(c, '(' | '[' | '—' | '–')); + let closes = after.chars().next().is_none_or(|c| c.is_whitespace() || matches!(c, '.' | ',' | ';' | ':' | '!' | '?' | ')' | ']' | '…')); + let phrase = inner.trim(); + if !opens || !closes || !phrase.chars().any(char::is_alphabetic) || phrase.chars().count() > 120 || phrase.contains('\n') { + continue; + } + v.push(issue(*a, b + 1, format!("Guillemets français attendus{NBSP}: {}", quoted(phrase)), vec![quoted(phrase)])); + } +} + +fn repeated_words(text: &str, ws: &[(usize, usize)], v: &mut Vec) { + for pair in ws.windows(2) { + let [(a0, b0), (a1, b1)] = pair else { continue }; + let (Some(w0), Some(w1), Some(gap)) = (text.get(*a0..*b0), text.get(*a1..*b1), text.get(*b0..*a1)) else { continue }; + let lower = w0.to_lowercase(); + let starts_upper = |w: &str| w.chars().next().is_some_and(char::is_uppercase); + if !gap.is_empty() + && gap.chars().all(char::is_whitespace) + && w0.chars().all(char::is_alphabetic) + && lower == w1.to_lowercase() + && !ALLOWED_REPEATS.contains(&lower.as_str()) + // `de De…`, `Bora Bora`: a capitalized second word is a name. + && !starts_upper(w1) + { + v.push(issue(*a0, *b1, format!("Mot répété{NBSP}: {}", quoted(w1)), vec![w0.to_string()])); + } + } +} + +/// Is `prev`, before a period, an abbreviation, an initial, a number or a Roman numeral (after +/// which a lower-case word doesn't start a sentence)? +fn abbreviation_or_number(prev: &str) -> bool { + prev.chars().count() <= 1 + || prev.chars().any(|c| c.is_ascii_digit()) + || prev.chars().all(|c| matches!(c, 'I' | 'V' | 'X' | 'L' | 'C' | 'D' | 'M')) + || ABBREVIATIONS.contains(&prev.to_lowercase().as_str()) +} + +/// Does `gap`, between the words `prev` and `next`, end a sentence: a `.`, `!` or `?` (French +/// puts a space before the last two), then a space, with only quotes and brackets around? Not +/// an ellipsis, not after an abbreviation; for `!` and `?`, not after an interjection, not before +/// a closing quote (dialogue goes on: `« Tu viens ? » demanda-t-elle`), not before an inversion +/// or a speech verb (`Quoi ? dit-il`, `— Viens ! cria-t-on`), and never in a `dialogue` line (one +/// opening with a dash). +fn ends_sentence(gap: &str, prev: &str, next: &str, dialogue: bool) -> bool { + let closing = ['"', '”', '’', '\'', ')', '»', ']']; + let t = gap.trim_start_matches(|c: char| c.is_whitespace() || is_fixed_space(c) || closing.contains(&c)); + let Some(end) = t.chars().next() else { return false }; + if !matches!(end, '.' | '!' | '?') || t.starts_with("..") { + return false; + } + if end == '.' && abbreviation_or_number(prev) { + return false; + } + let exclamation = end != '.'; + let lower_next = next.to_lowercase(); + if exclamation + && (dialogue + || INTERJECTIONS.contains(&prev.to_lowercase().as_str()) + || INVERTED.iter().any(|p| lower_next.ends_with(p)) + || SPEECH_VERBS.contains(&lower_next.as_str())) + { + return false; + } + let rest = t.trim_start_matches(['.', '!', '?']); + let after_marks = rest.trim_start_matches(is_fixed_space); + if after_marks.starts_with(closing) { + if exclamation { + return false; + } + return ends_sentence_after(after_marks.trim_start_matches(|c: char| closing.contains(&c) || is_fixed_space(c))); + } + ends_sentence_after(rest) +} + +/// Is `rest` (after the final mark) a space, then nothing but opening quotes and brackets? +fn ends_sentence_after(rest: &str) -> bool { + rest.starts_with(char::is_whitespace) + && rest.trim_start().chars().all(|c| c.is_whitespace() || is_fixed_space(c) || matches!(c, '"' | '“' | '«' | '(' | '[' | '—' | '–')) +} + +fn sentence_capitals(text: &str, ws: &[(usize, usize)], v: &mut Vec) { + let dialogue = text.trim_start().starts_with(['—', '–']); + for pair in ws.windows(2) { + let [(a0, b0), (a1, b1)] = pair else { continue }; + let (Some(prev), Some(gap), Some(w)) = (text.get(*a0..*b0), text.get(*b0..*a1), text.get(*a1..*b1)) else { continue }; + let word_like = w.chars().all(|c| c.is_alphabetic() || matches!(c, '\'' | '’' | '-')); + if word_like && w.chars().next().is_some_and(char::is_lowercase) && ends_sentence(gap, prev, w, dialogue) && !is_address(token_at(text, *a1)) + { + let fixed: String = w.chars().next().map(|f| f.to_uppercase().chain(w.chars().skip(1)).collect()).unwrap_or_default(); + v.push(issue(*a1, *b1, "Majuscule attendue en début de phrase".into(), vec![fixed])); + } + } +} + +/// Does `w` (lower case) start with a sound that makes `le`, `de`, `que`… elide? +fn takes_elision(w: &str) -> bool { + let Some(first) = w.chars().next() else { return false }; + if NO_ELISION.contains(&w) { + return false; + } + match first { + 'a' | 'à' | 'â' | 'ä' | 'e' | 'é' | 'è' | 'ê' | 'ë' | 'i' | 'î' | 'ï' | 'o' | 'ô' | 'ö' | 'u' | 'ù' | 'û' | 'ü' | 'œ' | 'æ' => { + true + } + 'h' => MUTE_H.contains(&w), + // Only `y` itself (`j’y vais`): `le yaourt`, `de yoga`. + 'y' => w == "y", + _ => false, + } +} + +fn elisions(text: &str, ws: &[(usize, usize)], v: &mut Vec) { + for pair in ws.windows(2) { + let [(a0, b0), (a1, b1)] = pair else { continue }; + let (Some(w0), Some(w1), Some(gap)) = (text.get(*a0..*b0), text.get(*a1..*b1), text.get(*b0..*a1)) else { continue }; + if gap.is_empty() || !gap.chars().all(|c| c == ' ') { + continue; + } + let lower0 = w0.to_lowercase(); + let Some((_, elided)) = ELIDING.iter().find(|(full, _)| *full == lower0) else { continue }; + // `le`/`Le` only, not `LE`; never before a proper noun (`de Orléans`) or a capital. + let capital = w0.chars().next().is_some_and(char::is_uppercase); + if w0.chars().skip(1).any(char::is_uppercase) || !w1.chars().next().is_some_and(char::is_lowercase) { + continue; + } + if !w1.chars().all(|c| c.is_alphabetic() || c == '-') { + continue; + } + let lower1 = w1.to_lowercase(); + // `le un`, `la une` (the number, the front page); a single letter only where it is a word + // the elision is certain for (`n’a`, `qu’a`, `j’y`). + if matches!(lower0.as_str(), "le" | "la") && matches!(lower1.as_str(), "un" | "une") { + continue; + } + let certain = (lower1 == "y" && matches!(lower0.as_str(), "je" | "me" | "te" | "se" | "ne")) + || (lower1 == "a" && matches!(lower0.as_str(), "ne" | "que")); + if lower1.chars().count() == 1 && !certain { + continue; + } + if !takes_elision(&lower1) { + continue; + } + let head = if capital { crate::affix::capitalize(elided) } else { (*elided).to_string() }; + let fixed = format!("{head}’{w1}"); + v.push(issue(*a0, *b1, format!("Élision{NBSP}: {}", quoted(&fixed)), vec![fixed])); + } +} + +/// Grammar issues in a paragraph of French text (France's typography), in order. +pub fn check(text: &str) -> Vec { + check_in(text, ProofLang::from_tag(Some("fr-FR"))) +} + +/// Grammar issues in a French Language pack locale, with Quebec typography in `fr-CA`. +/// Other locales have no French issues; the original locale is retained on every issue. +pub fn check_in(text: &str, lang: ProofLang) -> Vec { + let Some(locale) = lang.pack_locale().filter(|l| l.primary() == "fr") else { return Vec::new() }; + let ws: Vec<(usize, usize)> = words(text); + let mut v = Vec::new(); + high_punctuation(text, crate::lang::region(locale.as_str()).is_some_and(|r| r.eq_ignore_ascii_case("CA")), &mut v); + guillemets(text, &mut v); + straight_quotes(text, &mut v); + repeated_words(text, &ws, &mut v); + sentence_capitals(text, &ws, &mut v); + elisions(text, &ws, &mut v); + for i in &mut v { + i.lang = lang; + } + v.sort_by_key(|i| (i.start, i.end)); + v +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The issues of `text` as (flagged text, first suggestion). + fn found(text: &str) -> Vec<(String, String)> { + check(text) + .iter() + .map(|i| (text.get(i.start..i.end).unwrap_or("?").to_string(), i.suggestions.first().cloned().unwrap_or_default())) + .collect() + } + + fn quiet(text: &str) { + let v = check(text); + assert!(v.is_empty(), "{text:?} → {v:?}"); + } + + fn one(text: &str, flagged: &str, fix: &str) { + assert_eq!(found(text), [(flagged.to_string(), fix.to_string())], "{text:?}"); + } + + #[test] + fn missing_space_before_high_punctuation() { + one("Bonjour!", "!", "\u{202F}!"); + one("Tu viens?", "?", "\u{202F}?"); + one("Il pleut; il vente.", ";", "\u{202F};"); + one("Note: rien.", ":", "\u{A0}:"); + // `?!`: only the first mark needs the space. + one("Quoi?!", "?", "\u{202F}?"); + assert!(check("Bonjour!").iter().all(|i| i.lang == ProofLang::from_tag(Some("fr-FR")) && i.message.contains("fine insécable"))); + assert!(check("Note: rien.").iter().all(|i| i.message.contains("insécable") && !i.message.contains("fine"))); + } + + #[test] + fn an_ordinary_space_stays_quiet_until_french_autoformat() { + // Typing can't produce a non-breaking space yet, so an ordinary one is accepted. + quiet("Vraiment ? Bonjour ! Il pleut ; note : rien."); + quiet("Il dit « oui » hier."); + } + + #[test] + fn quebec_has_no_space_before_semicolon_exclamation_and_question() { + let fr_ca = |t: &str| check_in(t, ProofLang::from_tag(Some("fr-CA"))); + assert!(fr_ca("Bonjour! Tu viens? Il pleut; il vente.").is_empty(), "{:?}", fr_ca("Bonjour! Tu viens?")); + // The colon and the guillemets still want their space in Quebec. + let v = fr_ca("Note: «oui»."); + assert_eq!(v.len(), 3, "{v:?}"); + assert!(v.iter().all(|i| i.lang == ProofLang::from_tag(Some("fr-CA"))), "{v:?}"); + assert_eq!(check_in("Bonjour!", ProofLang::from_tag(Some("fr-FR"))).len(), 1, "France still wants the space"); + } + + #[test] + fn high_punctuation_stays_quiet() { + quiet("Bonjour\u{202F}! Tu viens\u{202F}? Il pleut\u{202F}; note\u{A0}: rien."); + quiet("Bonjour\u{A0}! Quoi\u{202F}?! Ah\u{2009}!"); + quiet("Il est 10:30."); + quiet("Voir https://exemple.org:8080/a?b=c pour la suite."); + quiet("Écrire à contact@exemple.org!"); + quiet("Super :) et ;-) aussi :D"); + quiet("a:b et x;y"); + quiet("Le fichier C:\\dossier est là."); + } + + #[test] + fn missing_space_inside_guillemets() { + assert_eq!(found("Il dit «oui» hier."), [("«".to_string(), "«\u{A0}".to_string()), ("»".to_string(), "\u{A0}»".to_string())]); + quiet("Il dit «\u{A0}oui\u{A0}» hier."); + quiet("Il dit «\u{202F}oui\u{202F}» hier."); + } + + #[test] + fn straight_quotes_become_guillemets() { + one("Il a dit \"bonjour\" hier.", "\"bonjour\"", "«\u{A0}bonjour\u{A0}»"); + one("\"Bonne idée\", dit-elle.", "\"Bonne idée\"", "«\u{A0}Bonne idée\u{A0}»"); + // Inch marks and unbalanced quotes are left alone. + quiet("Un écran de 27\" et un de 32\" ici."); + quiet("Il a dit \"bonjour hier."); + } + + #[test] + fn repeated_words_are_flagged() { + one("Le le chat dort.", "Le le", "Le"); + one("Je pense que que tu viens.", "que que", "que"); + one("Il mange la la pomme.", "la la", "la"); + } + + #[test] + fn deliberate_repetitions_stay_quiet() { + quiet("Nous nous lavons et vous vous trompez."); + quiet("Si si, très très bien."); + quiet("Ah ah, bla bla."); + quiet("Je crois que qu’il viendra."); + quiet("Le livre de De Gaulle."); + quiet("Nous partons à Bora Bora."); + quiet("Ils ont dit bye bye. Tchin tchin\u{202F}!"); + } + + #[test] + fn sentence_capitals_are_flagged() { + one("Il pleut. il fait froid.", "il", "Il"); + one("Il pleut\u{202F}! il fait froid.", "il", "Il"); + one("Tu viens\u{202F}? on part.", "on", "On"); + one("Il pleut. l’orage arrive.", "l’orage", "L’orage"); + one("Il pleut. «\u{A0}tout va bien\u{A0}», dit-il.", "tout", "Tout"); + } + + #[test] + fn sentence_capitals_stay_quiet() { + quiet("Voir p. 12 et cf. la note, etc. puis la suite."); + quiet("M. le maire est là."); + quiet("Il attend… puis part. Il attend... puis part."); + quiet("«\u{A0}Tu viens\u{202F}?\u{A0}» demanda-t-elle."); + quiet("Ah\u{202F}! que c’est beau."); + quiet("Quoi\u{202F}? dit-il."); + quiet("Tu viens\u{202F}? demanda le voisin."); + quiet("— Tu viens\u{202F}? reprends-tu, inquiet."); + quiet("– Viens\u{202F}! on part."); + quiet("Voir www.exemple.fr pour la suite."); + quiet("En 2026. ensuite rien."); + quiet("il commence en minuscule."); + } + + #[test] + fn missing_elisions_are_flagged() { + one("Il refuse de avoir peur.", "de avoir", "d’avoir"); + one("Je ai faim.", "Je ai", "J’ai"); + one("Voici le homme.", "le homme", "l’homme"); + one("Je sais que il viendra.", "que il", "qu’il"); + one("Le arbre est grand.", "Le arbre", "L’arbre"); + one("Il ne a pas faim.", "ne a", "n’a"); + one("Bon, je y vais.", "je y", "j’y"); + one("Il se est levé.", "se est", "s’est"); + one("Voici la école.", "la école", "l’école"); + one("Il me aime.", "me aime", "m’aime"); + } + + #[test] + fn aspirated_h_and_other_exceptions_stay_quiet() { + for w in [ + "hibou", + "héros", + "haricot", + "hasard", + "haut", + "hall", + "hamac", + "hangar", + "hockey", + "homard", + "huit", + "hérisson", + "hors-jeu", + "onze", + "oui", + "yaourt", + ] { + quiet(&format!("Voici le {w}.")); + } + // hiérarchie has an aspirated h, despite the mute h in hiéroglyphe: + // https://www.academie-francaise.fr/le-hieroglyphe-pour-lhieroglyphe + for w in ["honte", "hache", "harpe", "hausse", "hiérarchie"] { + quiet(&format!("Voici la {w}.")); + } + quiet("Une question de hiérarchie."); + quiet("Le onze de France. À la une du journal."); + quiet("Il vient de Orléans."); + quiet("De a à z, le a et le b."); + quiet("Le y et le x sont des variables. La dérivée de y."); + quiet("Je mange de la viande."); + } + + #[test] + fn hostile_input_never_panics() { + for t in ["", " ", "!", " !", "«", "»", " »", "« ", "\"", "\"\"", ". a", "de", "de ", "\u{202F}?", "é!é", "« » « »", ":", " :", "a :)"] + { + let _ = check(t); + } + // A long run of marks in one token: linear, not one token scan per mark. + let run = format!("mot{}", "?".repeat(200_000)); + assert_eq!(check(&run).len(), 1); + } +} diff --git a/crates/proof/src/fr/mod.rs b/crates/proof/src/fr/mod.rs new file mode 100644 index 000000000..541fcac0d --- /dev/null +++ b/crates/proof/src/fr/mod.rs @@ -0,0 +1,181 @@ +//! French proofing through the shared Language pack cache. +//! +//! A French locale remains [`ProofLang::Pack`]: dictionary selection, installation, reloads, +//! memory limits and hyphenation use the same paths as other packs. This module adds French +//! elisions, accent-oriented suggestions and conservative typography, including Quebec spacing. +//! No dictionary or hyphenation data is bundled here. + +pub mod grammar; +mod suggest; +#[cfg(test)] +#[allow(clippy::unwrap_used)] +pub(crate) mod test_dict; + +use crate::affix::AffixDict; +use crate::{Options, ProofLang}; + +pub use suggest::suggest_in; + +/// Words after whose elision a user-dictionary entry is accepted. Ordinary words and their +/// elisions are always decided by the dictionary (`l'arbre`, but not `l'arbres`). +const ELISIONS: &[&str] = &["l", "d", "j", "m", "t", "s", "n", "c", "qu", "jusqu", "lorsqu", "puisqu", "quoiqu", "presqu", "quelqu"]; + +/// Check French spelling in the installed France/generic pack, with the default options. +/// Regional callers should use [`crate::is_correct_in`] with their full locale. +pub fn is_correct(word: &str) -> bool { + crate::is_correct_in(word, ProofLang::from_tag(Some("fr-FR"))) +} + +/// French suggestions from the installed France/generic pack, best first, at most `max`. +/// Regional callers should use [`crate::suggest_in`] with their full locale. +pub fn suggest(word: &str, max: usize) -> Vec { + crate::suggest_in(word, max, ProofLang::from_tag(Some("fr-FR"))) +} + +/// Check against a supplied dictionary, without changing any installed pack or cache. +pub fn is_correct_with(d: &AffixDict, word: &str) -> bool { + check(d, &crate::composed(word), &Options::default()) +} + +pub(crate) fn check(d: &AffixDict, word: &str, opts: &Options) -> bool { + let Some(w) = crate::spelling::word_to_check(d, word, opts) else { return true }; + // A regional dictionary may store the curly apostrophe without ICONV. Honor that entry + // before the French ASCII-apostrophe fallback. + if known(d, w) { + return true; + } + let w = w.replace('’', "'"); + if known(d, &w) { + return true; + } + if let Some((head, tail)) = w.split_once('\'') + && ELISIONS.contains(&head.to_lowercase().as_str()) + && !tail.is_empty() + && !tail.contains('\'') + && crate::in_user_dictionary(&tail.to_lowercase()) + { + return true; + } + w.contains('-') && w.split('-').all(|p| !p.is_empty() && (p.chars().count() <= 1 || known(d, p))) +} + +fn known(d: &AffixDict, word: &str) -> bool { + d.check(word) || crate::in_user_dictionary(&word.to_lowercase()) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + + #[test] + fn original_french_words_and_elisions() { + let d = test_dict::dictionary(); + for word in [ + "maison", + "maisons", + "école", + "écoles", + "grand", + "grande", + "grands", + "grandes", + "aimer", + "aime", + "aimons", + "aiment", + "suis", + "est", + "sommes", + "sont", + "a", + "avons", + "ont", + "Paris", + "Montréal", + "œuvre", + "sœur", + "aujourd'hui", + "aujourd’hui", + "peut-être", + "arc-en-ciel", + "km", + "Maison", + "MAISON", + "SNCF", + "2026", + "très", + "rivière", + "l'arbre", + "l’arbre", + "L'arbre", + "d'enfants", + "l'homme", + "qu'elle", + "qu’il", + "j'aime", + "j’ai", + "d'avoir", + "l'école", + "jusqu'à", + "maison-école", + "est-il", + "a-t-il", + ] { + assert!(is_correct_with(&d, word), "{word}"); + } + for word in [ + "maisonn", + "ecole", + "arbrre", + "beaucoups", + "aimmer", + "soeur", + "Km", + "grandd", + "aim", + "l'hasard", + "l'chat", + "l'arbres", + "l'arbrre", + "qu'maison", + "maison-ecole", + "maison--école", + ] { + assert!(!is_correct_with(&d, word), "{word}"); + } + assert!(is_correct_with(&d, "e\u{301}cole")); + assert!(!is_correct_with(&d, "maison\u{338}"), "a trailing mark is part of the word"); + } + + #[test] + fn explicit_curly_apostrophe_without_iconv() { + let d = AffixDict::from_hunspell("SET UTF-8\n", "1\naujourd’hui\n", &[]).unwrap(); + assert!(is_correct_with(&d, "aujourd’hui")); + assert!(!is_correct_with(&d, "aujourd’huui")); + } + + #[test] + fn user_word_after_an_elision() { + let d = test_dict::dictionary(); + let word = "zxqmotadaptfr"; + assert!(!is_correct_with(&d, word) && !is_correct_with(&d, &format!("l'{word}"))); + crate::add_word(word); + assert!(is_correct_with(&d, word) && is_correct_with(&d, &format!("l'{word}")) && is_correct_with(&d, &format!("d’{word}"))); + assert!(!is_correct_with(&d, &format!("chat'{word}"))); + crate::remove_word(word); + } + + #[test] + fn options_and_missing_dictionary() { + let d = test_dict::dictionary(); + let defaults = Options::default(); + let strict = Options { ignore_uppercase: false, ignore_numbers: false, ..defaults }; + assert!(check(&d, "SNCF", &defaults) && check(&d, "mp3x", &defaults)); + assert!(!check(&d, "SNCF", &strict) && !check(&d, "mp3x", &strict)); + assert!(check(&d, "MAISON", &strict) && check(&d, "2026", &strict)); + for word in ["école", "l'arbre", "xqzv", "SNCF", ""] { + assert!(check(&AffixDict::default(), word, &strict)); + } + } +} diff --git a/crates/proof/src/fr/suggest.rs b/crates/proof/src/fr/suggest.rs new file mode 100644 index 000000000..86477d33f --- /dev/null +++ b/crates/proof/src/fr/suggest.rs @@ -0,0 +1,81 @@ +//! French tuning of the shared, bounded affix suggestion engine. + +use crate::affix::AffixDict; +use crate::affix_suggest::{Split, Tuning}; + +/// Short words often run into the next one (`et la`, `de Paris`, `pour moi`). +const LEADING_WORDS: &[&str] = &[ + "à", "a", "au", "aux", "de", "du", "des", "le", "la", "les", "un", "une", "et", "ou", "où", "en", "y", "ce", "se", "ne", "je", "tu", "il", "on", + "nous", "vous", "ils", "elle", "par", "pour", "sur", "sous", "dans", "avec", "sans", "chez", "vers", "entre", "très", "plus", "moins", "bien", + "pas", "que", "qui", "mais", "donc", "car", "si", "son", "sa", "ses", "mon", "ma", "mes", "ton", "ta", "tes", "est", "tout", "tous", "toute", + "comme", "quand", "puis", "encore", "déjà", "jamais", "toujours", +]; +/// Short words often run into the one before (`dis-moi` without the hyphen, `là-bas`, or a +/// dropped space: `pourmoi` → `pour moi`). +const TRAILING_WORDS: &[&str] = + &["moi", "toi", "lui", "elle", "nous", "vous", "eux", "le", "la", "les", "en", "y", "là", "ci", "il", "on", "ils", "elles", "est", "et"]; + +const TUNING: Tuning = Tuning { + split: Split::Near { leading: LEADING_WORDS, trailing: TRAILING_WORDS }, + alphabet: "esaitnrulodcpmévqfbghjàxèêyçôîâûùœ", + ..Tuning::DEFAULT +}; + +/// French suggestions from a supplied dictionary; keeps the writer's apostrophe and case. +/// Dictionary OCONV remains active for every other output conversion. +pub fn suggest_in(d: &AffixDict, word: &str, max: usize) -> Vec { + let word = crate::composed(word); + let curly = word.contains('’'); + let apostrophe = if curly { '’' } else { '\'' }; + let mut out = Vec::new(); + for text in crate::affix_suggest::suggest_in(d, &word, max, &TUNING) { + let styled: String = text.chars().map(|c| if c == '\'' || c == '’' { apostrophe } else { c }).collect(); + // A pack may spell only one apostrophe and have no ICONV. Keep its valid output + // rather than turn it back into the spelling mistake the caller is correcting. + let text = if styled.split(' ').all(|p| d.can_suggest_cased(p, p)) { styled } else { text }; + if text != word && !out.contains(&text) { + out.push(text); + } + } + out +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + + #[test] + fn french_accents_confusions_splits_and_elisions() { + let d = super::super::test_dict::dictionary(); + for (typo, expected) in [ + ("ecole", "école"), + ("riviere", "rivière"), + ("soeur", "sœur"), + ("maisonn", "maison"), + ("pourmoi", "pour moi"), + ("l'arbrre", "l'arbre"), + ("l’arbrre", "l’arbre"), + ] { + let got = suggest_in(&d, typo, 8); + assert!(got.iter().any(|s| s == expected), "{typo}: {got:?}"); + } + assert!(suggest_in(&d, "e\u{301}cole", 4).iter().all(|s| s != "école")); + assert!(suggest_in(&d, "ecole", 0).is_empty()); + assert!(suggest_in(&d, &"a".repeat(41), 4).is_empty()); + } + + #[test] + fn curly_dictionary_without_iconv_remains_suggestible() { + let d = AffixDict::from_hunspell("SET UTF-8\n", "1\naujourd’hui\n", &[]).unwrap(); + assert!(suggest_in(&d, "aujourd’huui", 4).iter().any(|s| s == "aujourd’hui")); + assert!(suggest_in(&d, "aujourd'hui", 4).iter().any(|s| s == "aujourd’hui")); + } + + #[test] + fn output_conversion_preserves_typed_apostrophe() { + let d = AffixDict::from_hunspell("SET UTF-8\nICONV 1\nICONV ’ '\nOCONV 1\nOCONV ' ’\n", "1\nl'arbre\n", &[]).unwrap(); + assert!(suggest_in(&d, "l'arbrre", 4).iter().any(|s| s == "l'arbre")); + assert!(suggest_in(&d, "l’arbrre", 4).iter().any(|s| s == "l’arbre")); + } +} diff --git a/crates/proof/src/fr/test_dict.rs b/crates/proof/src/fr/test_dict.rs new file mode 100644 index 000000000..23637254c --- /dev/null +++ b/crates/proof/src/fr/test_dict.rs @@ -0,0 +1,234 @@ +//! A tiny synthetic French Hunspell dictionary for tests (written for these tests; not taken +//! from any real dictionary). Real dictionaries are installed on the system, never bundled. +//! +//! It exercises what the Dicollecte French dictionary uses: two-character flags (`FLAG long`), +//! stems that need an affix (`NEEDAFFIX`, with an empty suffix for the singular), elisions as +//! prefixes licensed by a suffix's continuation (`l'arbre` but not `l'arbres`), irregular verbs +//! through `FULLSTRIP` (`être` → `suis`), `KEEPCASE` (`km`), `NOSUGGEST` (`bah`), `ICONV` (`’` +//! read as `'`) and the +//! suggestion tables (`TRY`, `MAP` for accents, `REP` for common confusions). + +pub const AFF: &str = "SET UTF-8 +FLAG long +NEEDAFFIX () +KEEPCASE || +NOSUGGEST -- +FULLSTRIP +ICONV 1 +ICONV ’ ' +TRY esaitnrulodcpmévqfbghjàxèêyçôîâûùœ +MAP 6 +MAP aàâä +MAP eéèêë +MAP iîï +MAP oôö +MAP uùûü +MAP cç +REP 3 +REP oe œ +REP ph f +REP f ph +PFX L' Y 1 +PFX L' 0 l' [aàâeéèêiîoôuœh] +PFX D' Y 1 +PFX D' 0 d' [aàâeéèêiîoôuœh] +PFX Q' Y 1 +PFX Q' 0 qu' [aàâeéèêiîoôuœh] +PFX J' Y 1 +PFX J' 0 j' [aàâeéèêiîoôuœhy] +SFX S. Y 2 +SFX S. 0 0/L'D'Q' [^sxz] +SFX S. 0 s/D'Q' [^sxz] +SFX S= Y 2 +SFX S= 0 0 [^sxz] +SFX S= 0 s [^sxz] +SFX F. Y 4 +SFX F. 0 0/L'D'Q' . +SFX F. 0 e/L'D'Q' . +SFX F. 0 s/D'Q' . +SFX F. 0 es/D'Q' . +SFX Er Y 6 +SFX Er er er/D' er +SFX Er er e/J' er +SFX Er er es er +SFX Er er ons er +SFX Er er ez er +SFX Er er ent er +SFX Et N 7 +SFX Et être être être +SFX Et être suis être +SFX Et être es être +SFX Et être est être +SFX Et être sommes être +SFX Et être êtes être +SFX Et être sont être +SFX Av N 7 +SFX Av avoir avoir/D' avoir +SFX Av avoir ai/J' avoir +SFX Av avoir as avoir +SFX Av avoir a avoir +SFX Av avoir avons avoir +SFX Av avoir avez avoir +SFX Av avoir ont/Q' avoir +"; + +/// Stems (the count line first, as in Hunspell files). +pub const DIC: &str = "150 +maison/S.() +arbre/S.() +enfant/S.() +ami/S.() +amie/S.() +homme/S.() +école/S.() +café/S.() +chat/S.() +rivière/S.() +ville/S.() +heure/S.() +histoire/S.() +sœur/S.() +œuvre/S.() +photo/S.() +téléphone/S.() +année/S.() +vie/S.() +livre/S.() +table/S.() +porte/S.() +femme/S.() +garçon/S.() +fille/S.() +père/S.() +mère/S.() +langue/S.() +mot/S.() +texte/S.() +phrase/S.() +exemple/S.() +jour/S.() +nuit/S.() +monde/S.() +hasard/S=() +haricot/S=() +grand/F.() +petit/F.() +noir/F.() +aimer/Er() +parler/Er() +chanter/Er() +arriver/Er() +être/Et() +avoir/Av() +il/Q' +ils/Q' +elle/Q' +elles/Q' +on/Q'L' +un/D'Q'L' +une/D'Q'L' +en/Q'J' +y/J' +à/Q' +jusqu'à +aujourd'hui/D' +peut-être +arc-en-ciel +km/|| +bah/-- +Paris +France +Montréal +le +la +les +de +des +du +et +à +je +tu +nous +vous +que +qui +ne +pas +se +me +te +ce +ça +avec +pour +dans +sur +sous +très +près +beaucoup +si +oui +non +mais +ou +où +dit +demanda +quand +comme +plus +bien +tout +toujours +jamais +ici +là +alors +donc +aussi +encore +déjà +mon +ma +mes +son +sa +ses +notre +votre +leur +cette +ces +au +aux +par +entre +vers +chez +sans +moi +toi +lui +rien +chose +temps +fois +eau +voici +voilà +bonjour +merci +beau +belle +la +etc +M +p +"; + +/// The original synthetic fixture, parsed independently of the installed pack cache. +pub fn dictionary() -> crate::affix::AffixDict { + crate::affix::AffixDict::from_hunspell(AFF, DIC, &[]).unwrap() +} diff --git a/crates/proof/src/hunspell.rs b/crates/proof/src/hunspell.rs index 966ded72f..948d0e25b 100644 --- a/crates/proof/src/hunspell.rs +++ b/crates/proof/src/hunspell.rs @@ -754,9 +754,13 @@ mod tests { /// spec's default `~/.local/share` applies, and with no home there is no folder. #[test] fn xdg_data_home_follows_the_spec() { - let home = Path::new("/home/a"); - assert_eq!(xdg_data_home(Some("/data".into()), Some(home)), Some(PathBuf::from("/data"))); - assert_eq!(xdg_data_home(Some("/data".into()), None), Some(PathBuf::from("/data"))); + // On Windows a rooted path without a drive is not absolute. Keep the same + // absolute/relative contract on each platform instead of changing the loader. + let (home, data) = if cfg!(windows) { (r"C:\Users\a", r"C:\data") } else { ("/home/a", "/data") }; + let (home, data) = (Path::new(home), Path::new(data)); + assert!(data.is_absolute()); + assert_eq!(xdg_data_home(Some(data.as_os_str().into()), Some(home)), Some(data.to_path_buf())); + assert_eq!(xdg_data_home(Some(data.as_os_str().into()), None), Some(data.to_path_buf())); for xdg in [None, Some(""), Some("relative/data")] { assert_eq!(xdg_data_home(xdg.map(Into::into), Some(home)), Some(home.join(".local/share")), "{xdg:?}"); assert_eq!(xdg_data_home(xdg.map(Into::into), None), None, "{xdg:?}"); @@ -1010,7 +1014,12 @@ mod tests { assert!(load(&aff, &big).unwrap_err().contains("too large")); // A directory, and on Unix an endless device, are not dictionary files. (No clock: a // reader that read `/dev/zero` would never return, so the error itself is the proof.) - assert!(load(&aff, &dir).unwrap_err().contains("not a regular file")); + // Windows rejects directories in File::open, before the regular-file guard. The + // rejection is required everywhere; its diagnostic is platform-dependent. + let directory_error = load(&aff, &dir).unwrap_err(); + assert!(directory_error.contains(&dir.display().to_string()), "{directory_error}"); + #[cfg(unix)] + assert!(directory_error.contains("not a regular file"), "{directory_error}"); #[cfg(unix)] assert!(load(&aff, Path::new("/dev/zero")).unwrap_err().contains("not a regular file")); // Turkish reads through the same reader. diff --git a/crates/proof/src/lib.rs b/crates/proof/src/lib.rs index 3df6cd017..f0f33d007 100644 --- a/crates/proof/src/lib.rs +++ b/crates/proof/src/lib.rs @@ -42,6 +42,9 @@ //! existing English fallback. //! - **German** ([`ProofLang::De`]): hyphenation by `hyph-utf8`'s German patterns (reformed //! spelling, MIT); no spelling or grammar marks yet. +//! - **French** (a [`ProofLang::Pack`] locale; module [`fr`]): the pack dictionary with French +//! elisions and accent-oriented suggestions, plus conservative typography and basic checks. +//! France and Canada retain their regional tags; Quebec uses its own punctuation spacing. //! - **Any other language** ([`ProofLang::Pack`], module [`packs`]): spelling and suggestions //! from its Hunspell dictionary (an installed Language pack, the dictionaries folder or the //! system's) with default tuning, hyphenation from its pack, no grammar rules; without a @@ -73,6 +76,7 @@ pub mod dict; pub mod en_variants; mod english; pub mod es; +pub mod fr; pub mod hr; pub mod hunspell; pub mod hyphen; @@ -976,6 +980,7 @@ fn grammar_in(text: &str, lang: ProofLang, capital_first: bool) -> Vec { match lang { ProofLang::En => grammar_en(text, capital_first), ProofLang::EnGb | ProofLang::EnAu | ProofLang::EnCa => grammar_en(text, capital_first).into_iter().map(|i| Issue { lang, ..i }).collect(), + ProofLang::Pack(locale) if locale.primary() == "fr" => fr::grammar::check_in(text, lang), // `pt-pt` shares Portuguese's rules, its issues in its own language. _ => language(lang.built_in()) .map_or_else(Vec::new, |l| (l.grammar)(text, lang, capital_first).into_iter().map(|i| Issue { lang, ..i }).collect()), diff --git a/crates/proof/src/pack_lang.rs b/crates/proof/src/pack_lang.rs index 7b4d19315..8b6cea902 100644 --- a/crates/proof/src/pack_lang.rs +++ b/crates/proof/src/pack_lang.rs @@ -1,6 +1,7 @@ //! Proofing for any language by its locale ([`crate::ProofLang::Pack`]): its Hunspell dictionary (the //! dictionaries folder, then an installed Language pack, then the system's), read by the shared -//! affix engine, with default suggestion tuning and no grammar rules. +//! affix engine. Most use default suggestion tuning and no grammar rules; French locales add +//! the spelling tuning and conservative typography in [`crate::fr`] without a separate cache. //! //! The loaded dictionaries share a memory budget ([`MAX_LOADED_BYTES`], estimated from their //! files); the one used least recently goes first, but never one used during the current @@ -320,6 +321,9 @@ pub(crate) fn is_correct(word: &str, locale: PackLocale, opts: &crate::Options) if other_script(w, &scripts) { return true; } + if locale.primary() == "fr" { + return crate::fr::check(&d, w, opts); + } let known = |w: &str| d.check(w) || crate::in_user_dictionary(&w.to_lowercase()); if w.contains('-') && !known(w) { return w.split('-').all(|p| p.chars().count() <= 1 || known(p)); @@ -333,7 +337,7 @@ pub(crate) fn suggest(word: &str, max: usize, locale: PackLocale) -> Vec if d.is_empty() { return Vec::new(); } - crate::affix_suggest::suggest_in(&d, word, max, &Tuning::DEFAULT) + if locale.primary() == "fr" { crate::fr::suggest_in(&d, word, max) } else { crate::affix_suggest::suggest_in(&d, word, max, &Tuning::DEFAULT) } } /// The pack locales whose hyphenation patterns serve a [`crate::ProofLang::Pack`] language: the locale, diff --git a/crates/proof/tests/affix_compatibility.rs b/crates/proof/tests/affix_compatibility.rs new file mode 100644 index 000000000..6d09e5f90 --- /dev/null +++ b/crates/proof/tests/affix_compatibility.rs @@ -0,0 +1,92 @@ +//! Frozen public output from the pre-French shared affix engine at upstream +//! e6c5a701ddff5be16e266028168c70658aae39d9, using original synthetic fixtures. +//! Do not regenerate expectations from the new implementation: they establish the baseline. +//! Legacy WCAFF1 bytes remain frozen, but are explicitly refused by the WCAFF2 engine. + +use craft_proof::affix::{AffixDict, capitalize}; +use std::fmt::Write; + +#[path = "fixtures/affix_compatibility_cases.rs"] +mod fixtures; + +fn snapshot(case: &fixtures::Case, dict: &AffixDict) -> String { + let mut out = format!("{}\nlen={} rules={}\n", case.name, dict.len(), dict.rule_count()); + writeln!(out, "try={:?} key={:?} map={:?} rep={:?} comments={:?}", dict.try_chars(), dict.key(), dict.map(), dict.rep(), dict.comments()) + .unwrap(); + let mut stems: Vec<_> = dict.stems().collect(); + stems.sort_unstable(); + for stem in &stems { + let mut forms = dict.forms(stem); + forms.sort_unstable(); + forms.dedup(); + writeln!(out, "forms {stem:?}: {forms:?}").unwrap(); + } + let mut probes: Vec = case.probes.iter().map(|word| (*word).to_string()).collect(); + for stem in stems { + probes.extend(dict.forms(stem)); + } + let originals = probes.clone(); + for word in originals { + probes.extend([word.to_uppercase(), word.to_lowercase(), capitalize(&word)]); + } + probes.sort_unstable(); + probes.dedup(); + for word in probes { + writeln!( + out, + "{word:?}: contains={} check={} suggest={} raw_suggest={}", + dict.contains(&word), + dict.check(&word), + dict.can_suggest(&word), + dict.contains_suggestible(&word) + ) + .unwrap(); + } + out +} + +#[test] +fn non_french_affix_outputs_match_pre_french_baseline() { + let expected = include_str!("fixtures/affix_compatibility.expected"); + let mut actual = String::new(); + for case in fixtures::cases() { + let dict = AffixDict::from_hunspell(&case.aff, &case.dic, &["original compatibility fixture"]).unwrap(); + let result = snapshot(&case, &dict); + let restored = AffixDict::from_bytes(&dict.to_bytes().unwrap()).unwrap(); + assert_eq!(snapshot(&case, &restored), result, "current compact round trip: {}", case.name); + actual.push_str(&result); + } + // craft-libs 55332dd predates this adaptation and supports a prefix plus a two-suffix + // chain (native test affix::tests::a_prefix_with_a_suffix_chain). The old Indonesian + // fixture's bermain + kan + lah therefore became a word, in these three cases only. + // Keep the frozen historical file untouched and assert every explicit transition once. + let mut expected = expected.to_string(); + let mut transitions = 0; + for word in ["BERMAINKANLAH", "Bermainkanlah", "bermainkanlah"] { + let before = format!("{word:?}: contains=false check=false suggest=false raw_suggest=false\n"); + let after = if word == "bermainkanlah" { + format!("{word:?}: contains=true check=true suggest=true raw_suggest=true\n") + } else { + format!("{word:?}: contains=false check=true suggest=true raw_suggest=false\n") + }; + assert_eq!(expected.matches(&before).count(), 1, "one historical observable: {word}"); + expected = expected.replacen(&before, &after, 1); + transitions += 1; + } + assert_eq!(transitions, 3, "the documented upstream exception has exactly three observables"); + assert_eq!(actual, expected, "shared engine output changed beyond the explicit upstream chain exception"); +} + +#[test] +fn legacy_compact_data_is_refused_without_reinterpreting_homonyms() { + let encoded = include_str!("fixtures/affix_compatibility.compact"); + let cases = fixtures::cases(); + let lines: Vec<_> = encoded.lines().collect(); + assert_eq!(lines.len(), cases.len()); + for (case, hex) in cases.iter().zip(lines) { + assert_eq!(hex.len() % 2, 0); + let bytes: Vec<_> = + hex.as_bytes().as_chunks::<2>().0.iter().map(|pair| u8::from_str_radix(std::str::from_utf8(pair).unwrap(), 16).unwrap()).collect(); + assert!(AffixDict::from_bytes(&bytes).is_err(), "WCAFF1 must be regenerated as WCAFF2: {}", case.name); + } +} diff --git a/crates/proof/tests/fixtures/affix_compatibility.compact b/crates/proof/tests/fixtures/affix_compatibility.compact new file mode 100644 index 000000000..5c8c627de --- /dev/null +++ b/crates/proof/tests/fixtures/affix_compatibility.compact @@ -0,0 +1,7 @@ +4d8bc10a825014446b7bbf62a07dd62744e5ae120b2ca2e02aafb8bceac54b295d067e91db76ea7f2516d16666ce30138c47ae3ba41e8c95a35cf884c89caf1c4b282789531ce4112756d1cadf809591a47e6a4bb39107aef2d64d5924e44f3dd42fd8ac4d09ca823c778d101b0c7011853e2d1be696536cf7e9ee57280dcd8dfe2dd4f97b88306faa2a0753b0f0274bea046c6f19dfd989a8a34dec70485d6b3e7c157553da617a03 +1d8c410e82301045bb9e534ce2deea11a8d8c48d10304196058b990418534aa25ec7852770e9aa17b3b27c79ffbf6a9768bd8515b2a32b8da6c796879bf1d4504ffe811dddfdec2c9c8a1a8d259ec33bbcc2077456a8439aee8f5556a418be90eb33e658e3069dc535941193888c462ae485d5a2a768ffa71244c32379968902d19a290e4174e6699dcc41d0308f17962aa67f +1dc7410ac2301005d0ace7141fdc1b3d42aa64a9a15d5497d392ca40eac818416f6fe8eaf1c65388f1483ba8c9439e5c30ebfae22a9314a93f2cf2ad1fcb94e20d09771c60823d0dada15521d06d1d2e6064308dd7fe3c90eb75657233bfd97789dcc2967db3c864ea03fd01 +1d8a3d0a02311406b77ea7f8c0dee8114437651453ac220a51a33cc88fc408e62c9e451bf55e1bb6199861baf94cca298d10135f39188753f43793f9c88e73c1859ff9912cade4060a5b4c102cc6a4abea410b7687b21f425b0f83df0b86bae57aa1a9f1ce9cbf6fa1a8b9e714bd50b51551f1ffd860444b3d +1d8c310e82401444b1dd53fcc45ef408282cb11008ab414b346a3641310413ed0cad95072034f450488c46f10a7f6fe4aeddcc9bcc0b4606a503d28538e15bbe0f2358c5bb4398f2258f787a860d3fa5c7644d268607d8881ba1ae3f1c9ba6e504ae6f0296c471d9ccb62d3605cc8947e780052ca00ff8c1067a842952fd89b8caae6e8c6878c716df3a56a4f3c01abfe222ed998ea5222f6c552e64d64426e7a794d53ae612fc00 +2dc84b0e8230100050d6738a49dc8b1e0114589800a11864398522c3a768a951120f2f212edf2b4e5e181e618793e13b6b1ab09ac60759963cb05db0e18f7d1905795622c9aa56dc6bb804253edfcad8e54b73dd4016a45877d8419c886b140522c718d2f08629967840a90cee41acf636f7a45dff3ffe3603b5ab8b243b0b702455e47ae08cc4daf552700cb534f37a31fc00 +35c7b98e82600045e1a9790a92e967dc97d20df71515b51345455114c1ed6d2616c6c2cad8d8fe2f36e67269cec9a7e5328a1296be65db31e7e6666cc9137bbd1dbba66e5aa67b9667e6c9f51c435295813c961b72e8d31f4887746a024da82934a50cc8a066d08c9a43736a012d281332a925b4a456d08ab2208b5a436b6a036d281bb2a92db4a576d08e722087da437bca855cca833cea001da82374a44ed0893a4367ea025da80c94a1b25096ca41392a0fe5a90254a01448a18a50912a4125aa0c95a90a54a1aa5095aa4135aa0ed5a906d4a09a50936a412daa0db5a90ed4a15448a5ba5097ea413daa0ff5290dd2a80134a086d0901a41232a0485a83014a62250848a42512a06c5a83814a71250824a42492a05a5a83494a6c41ff8197df77d0ffcf0fd08fcf4fd0cfcf6fd0e7cf37d0b7cf57d0dfcf2fdfa586b76f2aaf4657bae63ff8a97f475301cddfe0d8523d1583c914ca533d95cbea0144be54ab5566f345bed8edaedf5b5c17034d6275363365f98cb95b5ded8db9db377bdc3f174be883f71153771170ff1142ff196fe01 diff --git a/crates/proof/tests/fixtures/affix_compatibility.expected b/crates/proof/tests/fixtures/affix_compatibility.expected new file mode 100644 index 000000000..8fe3a9929 --- /dev/null +++ b/crates/proof/tests/fixtures/affix_compatibility.expected @@ -0,0 +1,497 @@ +pl: conditional strip, prefix cross products, diacritics +len=4 rules=5 +try="aeoiułkr" key="" map=[['a', 'ą'], ['o', 'ó', 'u']] rep=[("ż", "rz"), ("u", "ó")] comments=["original compatibility fixture"] +forms "Warszawa": ["Warszawa", "Warszawą"] +forms "kot": ["kot", "kotem", "koty", "niekot", "niekotem", "niekoty"] +forms "krowa": ["krowa", "krową"] +forms "piesek": ["piesek", "piesekem", "pieseky", "pieska"] +"": contains=false check=false suggest=false raw_suggest=false +"KOT": contains=false check=true suggest=true raw_suggest=false +"KOTEM": contains=false check=true suggest=true raw_suggest=false +"KOTY": contains=false check=true suggest=true raw_suggest=false +"KOTĄ": contains=false check=false suggest=false raw_suggest=false +"KROWA": contains=false check=true suggest=true raw_suggest=false +"KROWY": contains=false check=false suggest=false raw_suggest=false +"KROWĄ": contains=false check=true suggest=true raw_suggest=false +"Kot": contains=false check=true suggest=true raw_suggest=false +"Kotem": contains=false check=true suggest=true raw_suggest=false +"Koty": contains=false check=true suggest=true raw_suggest=false +"Kotą": contains=false check=false suggest=false raw_suggest=false +"Krowa": contains=false check=true suggest=true raw_suggest=false +"Krowy": contains=false check=false suggest=false raw_suggest=false +"Krową": contains=false check=true suggest=true raw_suggest=false +"NIE": contains=false check=false suggest=false raw_suggest=false +"NIEKOT": contains=false check=true suggest=true raw_suggest=false +"NIEKOTEM": contains=false check=true suggest=true raw_suggest=false +"NIEKOTY": contains=false check=true suggest=true raw_suggest=false +"NIEPIESKA": contains=false check=false suggest=false raw_suggest=false +"Nie": contains=false check=false suggest=false raw_suggest=false +"Niekot": contains=false check=true suggest=true raw_suggest=false +"Niekotem": contains=false check=true suggest=true raw_suggest=false +"Niekoty": contains=false check=true suggest=true raw_suggest=false +"Niepieska": contains=false check=false suggest=false raw_suggest=false +"PIESEK": contains=false check=true suggest=true raw_suggest=false +"PIESEKA": contains=false check=false suggest=false raw_suggest=false +"PIESEKEM": contains=false check=true suggest=true raw_suggest=false +"PIESEKY": contains=false check=true suggest=true raw_suggest=false +"PIESKA": contains=false check=true suggest=true raw_suggest=false +"Piesek": contains=false check=true suggest=true raw_suggest=false +"Pieseka": contains=false check=false suggest=false raw_suggest=false +"Piesekem": contains=false check=true suggest=true raw_suggest=false +"Pieseky": contains=false check=true suggest=true raw_suggest=false +"Pieska": contains=false check=true suggest=true raw_suggest=false +"WARSZAWA": contains=false check=true suggest=true raw_suggest=false +"WARSZAWĄ": contains=false check=true suggest=true raw_suggest=false +"Warszawa": contains=true check=true suggest=true raw_suggest=true +"Warszawą": contains=true check=true suggest=true raw_suggest=true +"kot": contains=true check=true suggest=true raw_suggest=true +"kotem": contains=true check=true suggest=true raw_suggest=true +"koty": contains=true check=true suggest=true raw_suggest=true +"kotą": contains=false check=false suggest=false raw_suggest=false +"krowa": contains=true check=true suggest=true raw_suggest=true +"krowy": contains=false check=false suggest=false raw_suggest=false +"krową": contains=true check=true suggest=true raw_suggest=true +"nie": contains=false check=false suggest=false raw_suggest=false +"niekot": contains=true check=true suggest=true raw_suggest=true +"niekotem": contains=true check=true suggest=true raw_suggest=true +"niekoty": contains=true check=true suggest=true raw_suggest=true +"niepieska": contains=false check=false suggest=false raw_suggest=false +"piesek": contains=true check=true suggest=true raw_suggest=true +"pieseka": contains=false check=false suggest=false raw_suggest=false +"piesekem": contains=true check=true suggest=true raw_suggest=true +"pieseky": contains=true check=true suggest=true raw_suggest=true +"pieska": contains=true check=true suggest=true raw_suggest=true +"warszawa": contains=false check=false suggest=false raw_suggest=false +"warszawą": contains=false check=false suggest=false raw_suggest=false +pt: UTF-8 flags, forbidden words, two suffixes +len=4 rules=3 +try="aeiouçãõ" key="" map=[] rep=[] comments=["original compatibility fixture"] +forms "bonito": ["bonita", "bonitas", "bonito", "bonitos"] +forms "casa": ["casa", "casas"] +forms "fazer": ["fazer", "refazer"] +forms "imundo": [] +"BONITA": contains=false check=true suggest=true raw_suggest=false +"BONITAS": contains=false check=true suggest=true raw_suggest=false +"BONITASS": contains=false check=false suggest=false raw_suggest=false +"BONITO": contains=false check=true suggest=true raw_suggest=false +"BONITOS": contains=false check=true suggest=true raw_suggest=false +"Bonita": contains=false check=true suggest=true raw_suggest=false +"Bonitas": contains=false check=true suggest=true raw_suggest=false +"Bonitass": contains=false check=false suggest=false raw_suggest=false +"Bonito": contains=false check=true suggest=true raw_suggest=false +"Bonitos": contains=false check=true suggest=true raw_suggest=false +"CASA": contains=false check=true suggest=true raw_suggest=false +"CASAA": contains=false check=false suggest=false raw_suggest=false +"CASAS": contains=false check=true suggest=true raw_suggest=false +"Casa": contains=false check=true suggest=true raw_suggest=false +"Casaa": contains=false check=false suggest=false raw_suggest=false +"Casas": contains=false check=true suggest=true raw_suggest=false +"FAZER": contains=false check=true suggest=true raw_suggest=false +"Fazer": contains=false check=true suggest=true raw_suggest=false +"IMUNDO": contains=false check=false suggest=false raw_suggest=false +"IMUNDOS": contains=false check=false suggest=false raw_suggest=false +"Imundo": contains=false check=false suggest=false raw_suggest=false +"Imundos": contains=false check=false suggest=false raw_suggest=false +"RECASAS": contains=false check=false suggest=false raw_suggest=false +"REFAZER": contains=false check=true suggest=true raw_suggest=false +"Recasas": contains=false check=false suggest=false raw_suggest=false +"Refazer": contains=false check=true suggest=true raw_suggest=false +"bonita": contains=true check=true suggest=true raw_suggest=true +"bonitas": contains=true check=true suggest=true raw_suggest=true +"bonitass": contains=false check=false suggest=false raw_suggest=false +"bonito": contains=true check=true suggest=true raw_suggest=true +"bonitos": contains=true check=true suggest=true raw_suggest=true +"casa": contains=true check=true suggest=true raw_suggest=true +"casaa": contains=false check=false suggest=false raw_suggest=false +"casas": contains=true check=true suggest=true raw_suggest=true +"fazer": contains=true check=true suggest=true raw_suggest=true +"imundo": contains=false check=false suggest=false raw_suggest=false +"imundos": contains=false check=false suggest=false raw_suggest=false +"recasas": contains=false check=false suggest=false raw_suggest=false +"refazer": contains=true check=true suggest=true raw_suggest=true +it: stripping, non-crossing suffix, proper-name casing +len=4 rules=3 +try="" key="" map=[] rep=[] comments=["original compatibility fixture"] +forms "Roma": ["Roma"] +forms "casa": ["casa", "case", "ricasa"] +forms "fare": ["fare", "rifare"] +forms "libro": ["libri", "libro"] +"": contains=false check=false suggest=false raw_suggest=false +"CASA": contains=false check=true suggest=true raw_suggest=false +"CASE": contains=false check=true suggest=true raw_suggest=false +"Casa": contains=false check=true suggest=true raw_suggest=false +"Case": contains=false check=true suggest=true raw_suggest=false +"FARE": contains=false check=true suggest=true raw_suggest=false +"Fare": contains=false check=true suggest=true raw_suggest=false +"LIBRI": contains=false check=true suggest=true raw_suggest=false +"LIBRO": contains=false check=true suggest=true raw_suggest=false +"LIBROS": contains=false check=false suggest=false raw_suggest=false +"Libri": contains=false check=true suggest=true raw_suggest=false +"Libro": contains=false check=true suggest=true raw_suggest=false +"Libros": contains=false check=false suggest=false raw_suggest=false +"R ILIBRI": contains=false check=false suggest=false raw_suggest=false +"R ilibri": contains=false check=false suggest=false raw_suggest=false +"RICAS A": contains=false check=false suggest=false raw_suggest=false +"RICASA": contains=false check=true suggest=true raw_suggest=false +"RICASE": contains=false check=false suggest=false raw_suggest=false +"RIFARE": contains=false check=true suggest=true raw_suggest=false +"ROMA": contains=false check=true suggest=true raw_suggest=false +"Ricas a": contains=false check=false suggest=false raw_suggest=false +"Ricasa": contains=false check=true suggest=true raw_suggest=false +"Ricase": contains=false check=false suggest=false raw_suggest=false +"Rifare": contains=false check=true suggest=true raw_suggest=false +"Roma": contains=true check=true suggest=true raw_suggest=true +"casa": contains=true check=true suggest=true raw_suggest=true +"case": contains=true check=true suggest=true raw_suggest=true +"fare": contains=true check=true suggest=true raw_suggest=true +"libri": contains=true check=true suggest=true raw_suggest=true +"libro": contains=true check=true suggest=true raw_suggest=true +"libros": contains=false check=false suggest=false raw_suggest=false +"r ilibri": contains=false check=false suggest=false raw_suggest=false +"ricas a": contains=false check=false suggest=false raw_suggest=false +"ricasa": contains=true check=true suggest=true raw_suggest=true +"ricase": contains=false check=false suggest=false raw_suggest=false +"rifare": contains=true check=true suggest=true raw_suggest=true +"roma": contains=false check=false suggest=false raw_suggest=false +cs: negated conditions and shared Unicode stems +len=4 rules=3 +try="" key="" map=[] rep=[] comments=["original compatibility fixture"] +forms "mladý": ["mladý", "nemladý"] +forms "strom": ["nestrom", "nestromy", "strom", "stromy"] +forms "y": ["y"] +forms "žena": ["žena", "ženě"] +"MLADÝ": contains=false check=true suggest=true raw_suggest=false +"MLADÝY": contains=false check=false suggest=false raw_suggest=false +"Mladý": contains=false check=true suggest=true raw_suggest=false +"Mladýy": contains=false check=false suggest=false raw_suggest=false +"NEMLADÝ": contains=false check=true suggest=true raw_suggest=false +"NESTROM": contains=false check=true suggest=true raw_suggest=false +"NESTROMY": contains=false check=true suggest=true raw_suggest=false +"NEŽENĚ": contains=false check=false suggest=false raw_suggest=false +"Nemladý": contains=false check=true suggest=true raw_suggest=false +"Nestrom": contains=false check=true suggest=true raw_suggest=false +"Nestromy": contains=false check=true suggest=true raw_suggest=false +"Neženě": contains=false check=false suggest=false raw_suggest=false +"STROM": contains=false check=true suggest=true raw_suggest=false +"STROMY": contains=false check=true suggest=true raw_suggest=false +"Strom": contains=false check=true suggest=true raw_suggest=false +"Stromy": contains=false check=true suggest=true raw_suggest=false +"Y": contains=false check=true suggest=true raw_suggest=false +"YY": contains=false check=false suggest=false raw_suggest=false +"Yy": contains=false check=false suggest=false raw_suggest=false +"mladý": contains=true check=true suggest=true raw_suggest=true +"mladýy": contains=false check=false suggest=false raw_suggest=false +"nemladý": contains=true check=true suggest=true raw_suggest=true +"nestrom": contains=true check=true suggest=true raw_suggest=true +"nestromy": contains=true check=true suggest=true raw_suggest=true +"neženě": contains=false check=false suggest=false raw_suggest=false +"strom": contains=true check=true suggest=true raw_suggest=true +"stromy": contains=true check=true suggest=true raw_suggest=true +"y": contains=true check=true suggest=true raw_suggest=true +"yy": contains=false check=false suggest=false raw_suggest=false +"ŽENA": contains=false check=true suggest=true raw_suggest=false +"ŽENY": contains=false check=false suggest=false raw_suggest=false +"ŽENĚ": contains=false check=true suggest=true raw_suggest=false +"Žena": contains=false check=true suggest=true raw_suggest=false +"Ženy": contains=false check=false suggest=false raw_suggest=false +"Ženě": contains=false check=true suggest=true raw_suggest=false +"žena": contains=true check=true suggest=true raw_suggest=true +"ženy": contains=false check=false suggest=false raw_suggest=false +"ženě": contains=true check=true suggest=true raw_suggest=true +ru: Cyrillic flags and suggestions exclusion +len=4 rules=2 +try="" key="" map=[['е', 'ё']] rep=[] comments=["original compatibility fixture"] +forms "дом": ["дом", "домы"] +forms "запрет": [] +forms "кот": ["кот", "коты", "некот", "некоты"] +forms "тайна": ["тайна", "тайнаы"] +"ДОМ": contains=false check=true suggest=true raw_suggest=false +"ДОМЫ": contains=false check=true suggest=true raw_suggest=false +"Дом": contains=false check=true suggest=true raw_suggest=false +"Домы": contains=false check=true suggest=true raw_suggest=false +"ЗАПРЕТ": contains=false check=false suggest=false raw_suggest=false +"ЗАПРЕТЫ": contains=false check=false suggest=false raw_suggest=false +"Запрет": contains=false check=false suggest=false raw_suggest=false +"Запреты": contains=false check=false suggest=false raw_suggest=false +"КОТ": contains=false check=true suggest=true raw_suggest=false +"КОТЫ": contains=false check=true suggest=true raw_suggest=false +"КОт": contains=false check=false suggest=false raw_suggest=false +"Кот": contains=false check=true suggest=true raw_suggest=false +"Коты": contains=false check=true suggest=true raw_suggest=false +"НЕДОМЫ": contains=false check=false suggest=false raw_suggest=false +"НЕКОТ": contains=false check=true suggest=true raw_suggest=false +"НЕКОТЫ": contains=false check=true suggest=true raw_suggest=false +"Недомы": contains=false check=false suggest=false raw_suggest=false +"Некот": contains=false check=true suggest=true raw_suggest=false +"Некоты": contains=false check=true suggest=true raw_suggest=false +"ТАЙНА": contains=false check=true suggest=false raw_suggest=false +"ТАЙНАЫ": contains=false check=true suggest=false raw_suggest=false +"Тайна": contains=false check=true suggest=false raw_suggest=false +"Тайнаы": contains=false check=true suggest=false raw_suggest=false +"дом": contains=true check=true suggest=true raw_suggest=true +"домы": contains=true check=true suggest=true raw_suggest=true +"запрет": contains=false check=false suggest=false raw_suggest=false +"запреты": contains=false check=false suggest=false raw_suggest=false +"кОт": contains=false check=false suggest=false raw_suggest=false +"кот": contains=true check=true suggest=true raw_suggest=true +"коты": contains=true check=true suggest=true raw_suggest=true +"недомы": contains=false check=false suggest=false raw_suggest=false +"некот": contains=true check=true suggest=true raw_suggest=true +"некоты": contains=true check=true suggest=true raw_suggest=true +"тайна": contains=true check=true suggest=false raw_suggest=false +"тайнаы": contains=true check=true suggest=false raw_suggest=false +id: continuation and NOSUGGEST through derivations +len=3 rules=3 +try="abcdeikn" key="qwerty|asdf" map=[] rep=[("dj", "j")] comments=["original compatibility fixture"] +forms "baca": ["baca", "bacakan", "bacakanlah"] +forms "main": ["bermain", "bermainkan", "main", "mainkan", "mainkanlah"] +forms "rahasia": ["rahasia", "rahasiakan", "rahasiakanlah"] +"BACA": contains=false check=true suggest=true raw_suggest=false +"BACAKAN": contains=false check=true suggest=true raw_suggest=false +"BACAKANLAH": contains=false check=true suggest=true raw_suggest=false +"BERBACA": contains=false check=false suggest=false raw_suggest=false +"BERMAIN": contains=false check=true suggest=true raw_suggest=false +"BERMAINKAN": contains=false check=true suggest=true raw_suggest=false +"BERMAINKANLAH": contains=false check=false suggest=false raw_suggest=false +"Baca": contains=false check=true suggest=true raw_suggest=false +"Bacakan": contains=false check=true suggest=true raw_suggest=false +"Bacakanlah": contains=false check=true suggest=true raw_suggest=false +"Berbaca": contains=false check=false suggest=false raw_suggest=false +"Bermain": contains=false check=true suggest=true raw_suggest=false +"Bermainkan": contains=false check=true suggest=true raw_suggest=false +"Bermainkanlah": contains=false check=false suggest=false raw_suggest=false +"MAIN": contains=false check=true suggest=true raw_suggest=false +"MAINKAN": contains=false check=true suggest=true raw_suggest=false +"MAINKANLAH": contains=false check=true suggest=true raw_suggest=false +"MAINKANLAHLAH": contains=false check=false suggest=false raw_suggest=false +"MAINLAH": contains=false check=false suggest=false raw_suggest=false +"Main": contains=false check=true suggest=true raw_suggest=false +"Mainkan": contains=false check=true suggest=true raw_suggest=false +"Mainkanlah": contains=false check=true suggest=true raw_suggest=false +"Mainkanlahlah": contains=false check=false suggest=false raw_suggest=false +"Mainlah": contains=false check=false suggest=false raw_suggest=false +"RAHASIA": contains=false check=true suggest=false raw_suggest=false +"RAHASIAKAN": contains=false check=true suggest=false raw_suggest=false +"RAHASIAKANLAH": contains=false check=true suggest=false raw_suggest=false +"Rahasia": contains=false check=true suggest=false raw_suggest=false +"Rahasiakan": contains=false check=true suggest=false raw_suggest=false +"Rahasiakanlah": contains=false check=true suggest=false raw_suggest=false +"baca": contains=true check=true suggest=true raw_suggest=true +"bacakan": contains=true check=true suggest=true raw_suggest=true +"bacakanlah": contains=true check=true suggest=true raw_suggest=true +"berbaca": contains=false check=false suggest=false raw_suggest=false +"bermain": contains=true check=true suggest=true raw_suggest=true +"bermainkan": contains=true check=true suggest=true raw_suggest=true +"bermainkanlah": contains=false check=false suggest=false raw_suggest=false +"main": contains=true check=true suggest=true raw_suggest=true +"mainkan": contains=true check=true suggest=true raw_suggest=true +"mainkanlah": contains=true check=true suggest=true raw_suggest=true +"mainkanlahlah": contains=false check=false suggest=false raw_suggest=false +"mainlah": contains=false check=false suggest=false raw_suggest=false +"rahasia": contains=true check=true suggest=false raw_suggest=false +"rahasiakan": contains=true check=true suggest=false raw_suggest=false +"rahasiakanlah": contains=true check=true suggest=false raw_suggest=false +pt-sized: 70 distinct flags across the old u128 boundary +len=2 rules=70 +try="" key="" map=[] rep=[] comments=["original compatibility fixture"] +forms "outro": ["outro", "outroõ"] +forms "verbo": ["verbo", "verbo1", "verbo2", "verbo3", "verbo4", "verbo5", "verbo6", "verbo7", "verbo8", "verbo9", "verboA", "verboB", "verboC", "verboD", "verboE", "verboF", "verboG", "verboH", "verboI", "verboJ", "verboK", "verboL", "verboM", "verboN", "verboO", "verboP", "verboQ", "verboR", "verboS", "verboT", "verboU", "verboV", "verboW", "verboX", "verboY", "verboZ", "verboa", "verbob", "verboc", "verbod", "verboe", "verbof", "verbog", "verboh", "verboi", "verboj", "verbok", "verbol", "verbom", "verbon", "verboo", "verbop", "verboq", "verbor", "verbos", "verbot", "verbou", "verbov", "verbow", "verbox", "verboy", "verboz", "verboá", "verboã", "verboç", "verboé", "verboí", "verboó", "verboõ", "verboú"] +"OUTRO": contains=false check=true suggest=true raw_suggest=false +"OUTROA": contains=false check=false suggest=false raw_suggest=false +"OUTROÕ": contains=false check=true suggest=true raw_suggest=false +"Outro": contains=false check=true suggest=true raw_suggest=false +"Outroa": contains=false check=false suggest=false raw_suggest=false +"Outroõ": contains=false check=true suggest=true raw_suggest=false +"VERBO": contains=false check=true suggest=true raw_suggest=false +"VERBO0": contains=false check=false suggest=false raw_suggest=false +"VERBO1": contains=false check=true suggest=true raw_suggest=false +"VERBO2": contains=false check=true suggest=true raw_suggest=false +"VERBO3": contains=false check=true suggest=true raw_suggest=false +"VERBO4": contains=false check=true suggest=true raw_suggest=false +"VERBO5": contains=false check=true suggest=true raw_suggest=false +"VERBO6": contains=false check=true suggest=true raw_suggest=false +"VERBO7": contains=false check=true suggest=true raw_suggest=false +"VERBO8": contains=false check=true suggest=true raw_suggest=false +"VERBO9": contains=false check=true suggest=true raw_suggest=false +"VERBOA": contains=false check=true suggest=true raw_suggest=false +"VERBOB": contains=false check=true suggest=true raw_suggest=false +"VERBOC": contains=false check=true suggest=true raw_suggest=false +"VERBOD": contains=false check=true suggest=true raw_suggest=false +"VERBOE": contains=false check=true suggest=true raw_suggest=false +"VERBOF": contains=false check=true suggest=true raw_suggest=false +"VERBOG": contains=false check=true suggest=true raw_suggest=false +"VERBOH": contains=false check=true suggest=true raw_suggest=false +"VERBOI": contains=false check=true suggest=true raw_suggest=false +"VERBOJ": contains=false check=true suggest=true raw_suggest=false +"VERBOK": contains=false check=true suggest=true raw_suggest=false +"VERBOL": contains=false check=true suggest=true raw_suggest=false +"VERBOM": contains=false check=true suggest=true raw_suggest=false +"VERBON": contains=false check=true suggest=true raw_suggest=false +"VERBOO": contains=false check=true suggest=true raw_suggest=false +"VERBOP": contains=false check=true suggest=true raw_suggest=false +"VERBOQ": contains=false check=true suggest=true raw_suggest=false +"VERBOR": contains=false check=true suggest=true raw_suggest=false +"VERBOS": contains=false check=true suggest=true raw_suggest=false +"VERBOSS": contains=false check=false suggest=false raw_suggest=false +"VERBOT": contains=false check=true suggest=true raw_suggest=false +"VERBOU": contains=false check=true suggest=true raw_suggest=false +"VERBOV": contains=false check=true suggest=true raw_suggest=false +"VERBOW": contains=false check=true suggest=true raw_suggest=false +"VERBOX": contains=false check=true suggest=true raw_suggest=false +"VERBOY": contains=false check=true suggest=true raw_suggest=false +"VERBOZ": contains=false check=true suggest=true raw_suggest=false +"VERBOÁ": contains=false check=true suggest=true raw_suggest=false +"VERBOÃ": contains=false check=true suggest=true raw_suggest=false +"VERBOÇ": contains=false check=true suggest=true raw_suggest=false +"VERBOÉ": contains=false check=true suggest=true raw_suggest=false +"VERBOÍ": contains=false check=true suggest=true raw_suggest=false +"VERBOÓ": contains=false check=true suggest=true raw_suggest=false +"VERBOÕ": contains=false check=true suggest=true raw_suggest=false +"VERBOÚ": contains=false check=true suggest=true raw_suggest=false +"Verbo": contains=false check=true suggest=true raw_suggest=false +"Verbo0": contains=false check=false suggest=false raw_suggest=false +"Verbo1": contains=false check=true suggest=true raw_suggest=false +"Verbo2": contains=false check=true suggest=true raw_suggest=false +"Verbo3": contains=false check=true suggest=true raw_suggest=false +"Verbo4": contains=false check=true suggest=true raw_suggest=false +"Verbo5": contains=false check=true suggest=true raw_suggest=false +"Verbo6": contains=false check=true suggest=true raw_suggest=false +"Verbo7": contains=false check=true suggest=true raw_suggest=false +"Verbo8": contains=false check=true suggest=true raw_suggest=false +"Verbo9": contains=false check=true suggest=true raw_suggest=false +"VerboA": contains=false check=false suggest=false raw_suggest=false +"VerboB": contains=false check=false suggest=false raw_suggest=false +"VerboC": contains=false check=false suggest=false raw_suggest=false +"VerboD": contains=false check=false suggest=false raw_suggest=false +"VerboE": contains=false check=false suggest=false raw_suggest=false +"VerboF": contains=false check=false suggest=false raw_suggest=false +"VerboG": contains=false check=false suggest=false raw_suggest=false +"VerboH": contains=false check=false suggest=false raw_suggest=false +"VerboI": contains=false check=false suggest=false raw_suggest=false +"VerboJ": contains=false check=false suggest=false raw_suggest=false +"VerboK": contains=false check=false suggest=false raw_suggest=false +"VerboL": contains=false check=false suggest=false raw_suggest=false +"VerboM": contains=false check=false suggest=false raw_suggest=false +"VerboN": contains=false check=false suggest=false raw_suggest=false +"VerboO": contains=false check=false suggest=false raw_suggest=false +"VerboP": contains=false check=false suggest=false raw_suggest=false +"VerboQ": contains=false check=false suggest=false raw_suggest=false +"VerboR": contains=false check=false suggest=false raw_suggest=false +"VerboS": contains=false check=false suggest=false raw_suggest=false +"VerboT": contains=false check=false suggest=false raw_suggest=false +"VerboU": contains=false check=false suggest=false raw_suggest=false +"VerboV": contains=false check=false suggest=false raw_suggest=false +"VerboW": contains=false check=false suggest=false raw_suggest=false +"VerboX": contains=false check=false suggest=false raw_suggest=false +"VerboY": contains=false check=false suggest=false raw_suggest=false +"VerboZ": contains=false check=false suggest=false raw_suggest=false +"Verboa": contains=false check=true suggest=true raw_suggest=false +"Verbob": contains=false check=true suggest=true raw_suggest=false +"Verboc": contains=false check=true suggest=true raw_suggest=false +"Verbod": contains=false check=true suggest=true raw_suggest=false +"Verboe": contains=false check=true suggest=true raw_suggest=false +"Verbof": contains=false check=true suggest=true raw_suggest=false +"Verbog": contains=false check=true suggest=true raw_suggest=false +"Verboh": contains=false check=true suggest=true raw_suggest=false +"Verboi": contains=false check=true suggest=true raw_suggest=false +"Verboj": contains=false check=true suggest=true raw_suggest=false +"Verbok": contains=false check=true suggest=true raw_suggest=false +"Verbol": contains=false check=true suggest=true raw_suggest=false +"Verbom": contains=false check=true suggest=true raw_suggest=false +"Verbon": contains=false check=true suggest=true raw_suggest=false +"Verboo": contains=false check=true suggest=true raw_suggest=false +"Verbop": contains=false check=true suggest=true raw_suggest=false +"Verboq": contains=false check=true suggest=true raw_suggest=false +"Verbor": contains=false check=true suggest=true raw_suggest=false +"Verbos": contains=false check=true suggest=true raw_suggest=false +"Verbot": contains=false check=true suggest=true raw_suggest=false +"Verbou": contains=false check=true suggest=true raw_suggest=false +"Verbov": contains=false check=true suggest=true raw_suggest=false +"Verbow": contains=false check=true suggest=true raw_suggest=false +"Verbox": contains=false check=true suggest=true raw_suggest=false +"Verboy": contains=false check=true suggest=true raw_suggest=false +"Verboz": contains=false check=true suggest=true raw_suggest=false +"Verboß": contains=false check=false suggest=false raw_suggest=false +"Verboá": contains=false check=true suggest=true raw_suggest=false +"Verboã": contains=false check=true suggest=true raw_suggest=false +"Verboç": contains=false check=true suggest=true raw_suggest=false +"Verboé": contains=false check=true suggest=true raw_suggest=false +"Verboí": contains=false check=true suggest=true raw_suggest=false +"Verboó": contains=false check=true suggest=true raw_suggest=false +"Verboõ": contains=false check=true suggest=true raw_suggest=false +"Verboú": contains=false check=true suggest=true raw_suggest=false +"outro": contains=true check=true suggest=true raw_suggest=true +"outroa": contains=false check=false suggest=false raw_suggest=false +"outroõ": contains=true check=true suggest=true raw_suggest=true +"verbo": contains=true check=true suggest=true raw_suggest=true +"verbo0": contains=false check=false suggest=false raw_suggest=false +"verbo1": contains=true check=true suggest=true raw_suggest=true +"verbo2": contains=true check=true suggest=true raw_suggest=true +"verbo3": contains=true check=true suggest=true raw_suggest=true +"verbo4": contains=true check=true suggest=true raw_suggest=true +"verbo5": contains=true check=true suggest=true raw_suggest=true +"verbo6": contains=true check=true suggest=true raw_suggest=true +"verbo7": contains=true check=true suggest=true raw_suggest=true +"verbo8": contains=true check=true suggest=true raw_suggest=true +"verbo9": contains=true check=true suggest=true raw_suggest=true +"verboA": contains=true check=true suggest=true raw_suggest=true +"verboB": contains=true check=true suggest=true raw_suggest=true +"verboC": contains=true check=true suggest=true raw_suggest=true +"verboD": contains=true check=true suggest=true raw_suggest=true +"verboE": contains=true check=true suggest=true raw_suggest=true +"verboF": contains=true check=true suggest=true raw_suggest=true +"verboG": contains=true check=true suggest=true raw_suggest=true +"verboH": contains=true check=true suggest=true raw_suggest=true +"verboI": contains=true check=true suggest=true raw_suggest=true +"verboJ": contains=true check=true suggest=true raw_suggest=true +"verboK": contains=true check=true suggest=true raw_suggest=true +"verboL": contains=true check=true suggest=true raw_suggest=true +"verboM": contains=true check=true suggest=true raw_suggest=true +"verboN": contains=true check=true suggest=true raw_suggest=true +"verboO": contains=true check=true suggest=true raw_suggest=true +"verboP": contains=true check=true suggest=true raw_suggest=true +"verboQ": contains=true check=true suggest=true raw_suggest=true +"verboR": contains=true check=true suggest=true raw_suggest=true +"verboS": contains=true check=true suggest=true raw_suggest=true +"verboT": contains=true check=true suggest=true raw_suggest=true +"verboU": contains=true check=true suggest=true raw_suggest=true +"verboV": contains=true check=true suggest=true raw_suggest=true +"verboW": contains=true check=true suggest=true raw_suggest=true +"verboX": contains=true check=true suggest=true raw_suggest=true +"verboY": contains=true check=true suggest=true raw_suggest=true +"verboZ": contains=true check=true suggest=true raw_suggest=true +"verboa": contains=true check=true suggest=true raw_suggest=true +"verbob": contains=true check=true suggest=true raw_suggest=true +"verboc": contains=true check=true suggest=true raw_suggest=true +"verbod": contains=true check=true suggest=true raw_suggest=true +"verboe": contains=true check=true suggest=true raw_suggest=true +"verbof": contains=true check=true suggest=true raw_suggest=true +"verbog": contains=true check=true suggest=true raw_suggest=true +"verboh": contains=true check=true suggest=true raw_suggest=true +"verboi": contains=true check=true suggest=true raw_suggest=true +"verboj": contains=true check=true suggest=true raw_suggest=true +"verbok": contains=true check=true suggest=true raw_suggest=true +"verbol": contains=true check=true suggest=true raw_suggest=true +"verbom": contains=true check=true suggest=true raw_suggest=true +"verbon": contains=true check=true suggest=true raw_suggest=true +"verboo": contains=true check=true suggest=true raw_suggest=true +"verbop": contains=true check=true suggest=true raw_suggest=true +"verboq": contains=true check=true suggest=true raw_suggest=true +"verbor": contains=true check=true suggest=true raw_suggest=true +"verbos": contains=true check=true suggest=true raw_suggest=true +"verbot": contains=true check=true suggest=true raw_suggest=true +"verbou": contains=true check=true suggest=true raw_suggest=true +"verbov": contains=true check=true suggest=true raw_suggest=true +"verbow": contains=true check=true suggest=true raw_suggest=true +"verbox": contains=true check=true suggest=true raw_suggest=true +"verboy": contains=true check=true suggest=true raw_suggest=true +"verboz": contains=true check=true suggest=true raw_suggest=true +"verboß": contains=false check=false suggest=false raw_suggest=false +"verboá": contains=true check=true suggest=true raw_suggest=true +"verboã": contains=true check=true suggest=true raw_suggest=true +"verboç": contains=true check=true suggest=true raw_suggest=true +"verboé": contains=true check=true suggest=true raw_suggest=true +"verboí": contains=true check=true suggest=true raw_suggest=true +"verboó": contains=true check=true suggest=true raw_suggest=true +"verboõ": contains=true check=true suggest=true raw_suggest=true +"verboú": contains=true check=true suggest=true raw_suggest=true diff --git a/crates/proof/tests/fixtures/affix_compatibility_cases.rs b/crates/proof/tests/fixtures/affix_compatibility_cases.rs new file mode 100644 index 000000000..d0c2973b4 --- /dev/null +++ b/crates/proof/tests/fixtures/affix_compatibility_cases.rs @@ -0,0 +1,61 @@ +// Original, deliberately small fixtures for behavior supported before French proofing. +// These are not extracts from installed dictionaries and do not claim full language coverage. +pub struct Case { + pub name: &'static str, + pub aff: String, + pub dic: String, + pub probes: Vec<&'static str>, +} + +pub fn cases() -> Vec { + let mut out = vec![ + Case { + name: "pl: conditional strip, prefix cross products, diacritics", + aff: "SET UTF-8\nTRY aeoiułkr\nPFX b Y 1\nPFX b 0 nie .\nSFX a Y 3\nSFX a 0 y [^y]\nSFX a ek ka ek\nSFX a 0 em .\nSFX c N 1\nSFX c a ą a\nMAP 2\nMAP aą\nMAP oóu\nREP 2\nREP ż rz\nREP u ó\n".into(), + dic: "4\nkot/ab\npiesek/a\nkrowa/c\nWarszawa/c\n".into(), + probes: vec!["kot", "koty", "kotem", "niekoty", "pieska", "pieseka", "niepieska", "krową", "krowy", "Warszawą", "warszawą", "kotą", "nie", ""], + }, + Case { + name: "pt: UTF-8 flags, forbidden words, two suffixes", + aff: "SET UTF-8\nFLAG UTF-8\nFORBIDDENWORD ý\nTRY aeiouçãõ\nSFX A Y 1\nSFX A o a/B o\nSFX B Y 1\nSFX B 0 s .\nPFX P Y 1\nPFX P 0 re .\n".into(), + dic: "4\nbonito/AB\ncasa/B\nfazer/P\nimundo/ýB\n".into(), + probes: vec!["bonito", "bonita", "bonitos", "bonitas", "bonitass", "casa", "casas", "casaa", "refazer", "recasas", "imundo", "imundos", "Imundo", "IMUNDO"], + }, + Case { + name: "it: stripping, non-crossing suffix, proper-name casing", + aff: "SET UTF-8\nSFX A Y 1\nSFX A o i o\nSFX B N 1\nSFX B a e a\nPFX P Y 1\nPFX P 0 ri .\n".into(), + dic: "4\nlibro/A\ncasa/BP\nfare/P\nRoma\n".into(), + probes: vec!["libri", "libros", "case", "ricas a", "ricasa", "ricase", "rifare", "r ilibri", "Roma", "roma", "ROMA", ""], + }, + Case { + name: "cs: negated conditions and shared Unicode stems", + aff: "SET UTF-8\nPFX N Y 1\nPFX N 0 ne .\nSFX S Y 1\nSFX S 0 y [^y]\nSFX E N 1\nSFX E a ě a\n".into(), + dic: "4\nstrom/NS\nžena/E\nmladý/N\ny/S\n".into(), + probes: vec!["stromy", "nestromy", "nestrom", "ženě", "ženy", "neženě", "nemladý", "mladýy", "yy", "Ženě", "STROMY"], + }, + Case { + name: "ru: Cyrillic flags and suggestions exclusion", + aff: "SET UTF-8\nFLAG UTF-8\nNOSUGGEST Н\nFORBIDDENWORD Ф\nSFX Я Y 1\nSFX Я 0 ы .\nPFX П Y 1\nPFX П 0 не .\nMAP 1\nMAP её\n".into(), + dic: "4\nкот/ЯП\nдом/Я\nтайна/НЯ\nзапрет/ФЯ\n".into(), + probes: vec!["кот", "коты", "некоты", "домы", "недомы", "тайна", "тайнаы", "ТАЙНАЫ", "запрет", "запреты", "КОТЫ", "кОт"], + }, + Case { + name: "id: continuation and NOSUGGEST through derivations", + aff: "SET UTF-8\nNOSUGGEST N\nKEY qwerty|asdf\nTRY abcdeikn\nREP 1\nREP dj j\nPFX P Y 1\nPFX P 0 ber .\nSFX A Y 1\nSFX A 0 kan/B .\nSFX B Y 1\nSFX B 0 lah .\n".into(), + dic: "3\nmain/PA\nbaca/A\nrahasia/NA\n".into(), + probes: vec!["main", "bermain", "mainkan", "mainkanlah", "bermainkanlah", "mainlah", "mainkanlahlah", "bacakanlah", "berbaca", "rahasia", "rahasiakan", "RAHASIAKANLAH"], + }, + ]; + let flags: Vec = ('a'..='z').chain('A'..='Z').chain('0'..='9').chain(['á', 'é', 'í', 'ó', 'ú', 'ç', 'ã', 'õ']).collect(); + let mut aff = String::from("SET UTF-8\nFLAG UTF-8\n"); + for flag in &flags { + aff.push_str(&format!("SFX {flag} N 1\nSFX {flag} 0 {flag} .\n")); + } + out.push(Case { + name: "pt-sized: 70 distinct flags across the old u128 boundary", + aff, + dic: format!("2\nverbo/{}\noutro/õ\n", flags.iter().collect::()), + probes: vec!["verboa", "verboz", "verboA", "verboZ", "verbo0", "verbo9", "verboç", "verboõ", "verboß", "outroõ", "outroa"], + }); + out +} diff --git a/crates/proof/tests/french_dictionary.rs b/crates/proof/tests/french_dictionary.rs new file mode 100644 index 000000000..5612a0e61 --- /dev/null +++ b/crates/proof/tests/french_dictionary.rs @@ -0,0 +1,107 @@ +//! Explicit manual real-dictionary gate. Verify the pinned runtime inputs as documented in +//! crates/proof/README.md before running with `--ignored`. Missing data is a failure, +//! never a silently successful test that checked nothing. + +#![cfg(not(target_arch = "wasm32"))] +#![allow(clippy::unwrap_used, clippy::expect_used)] + +use craft_proof::{IssueKind, Options, ProofLang, fr}; + +#[test] +#[ignore = "requires a real French dictionary in WORDCRAFT_DICTIONARIES"] +fn real_french_dictionary_spelling_suggestions_and_dispatch() { + let directory = std::env::var_os("WORDCRAFT_DICTIONARIES").expect("set WORDCRAFT_DICTIONARIES to the test dictionary folder"); + craft_proof::set_dictionaries_folder(Some(std::path::PathBuf::from(&directory))); + let french = ProofLang::from_tag(Some("fr-FR")); + let installed = craft_proof::pack_dictionary_file(french).expect("the real French dictionary must be installed"); + assert_eq!(installed.parent(), Some(std::path::Path::new(&directory)), "do not silently use another system dictionary"); + let aff = installed.with_extension("aff"); + let dictionary = craft_proof::hunspell::load(&aff, &installed).expect("the real French dictionary loads"); + assert!(dictionary.len() > 50_000, "expected Dicollecte, not the synthetic fixture: {}", dictionary.len()); + for word in [ + "maison", + "maisons", + "Paris", + "mangeons", + "finissaient", + "étaient", + "l'arbre", + "l’arbre", + "qu'elles", + "école", + "œuvre", + "aujourd'hui", + "chevaux", + "heureuse", + "va-t-il", + ] { + assert!(fr::is_correct(word), "correct French word: {word}"); + } + for (typo, expected) in [("maisonn", "maison"), ("ecole", "école"), ("l’ecole", "l’école"), ("documant", "document")] { + assert!(!fr::is_correct(typo), "misspelling: {typo}"); + let suggestions = fr::suggest(typo, 8); + assert!(suggestions.iter().any(|s| s == expected), "{typo} -> {suggestions:?}, expected {expected}"); + } + let text = "Une maisonn près de l’école."; + for lang in [ProofLang::from_tag(Some("fr-FR")), ProofLang::from_tag(Some("fr-CA"))] { + let issues = craft_proof::check_text(text, &[(0..text.len(), lang)]); + assert_eq!(issues.len(), 1, "{lang:?}: {issues:?}"); + let issue = &issues[0]; + assert_eq!(&text[issue.start..issue.end], "maisonn"); + assert_eq!(issue.lang, lang); + assert_eq!(issue.message, "Faute d’orthographe possible"); + } + check_combining_marks_and_compounds(&dictionary); + assert!(craft_proof::dictionary_load_error(ProofLang::from_tag(Some("fr-FR"))).is_none()); +} + +fn check_combining_marks_and_compounds(dictionary: &craft_proof::affix::AffixDict) { + let spelling = |text: &str, spans: &[(std::ops::Range, ProofLang)]| { + craft_proof::check_text_with(text, spans, &Options::default(), false, false) + .into_iter() + .filter(|i| i.kind == IssueKind::Spelling) + .map(|i| (i.start, i.end, i.lang)) + .collect::>() + }; + let mut failures = Vec::new(); + for lang in [ProofLang::from_tag(Some("fr-FR")), ProofLang::from_tag(Some("fr-CA"))] { + for text in + ["école", "e\u{301}cole", "l’école", "l’e\u{301}cole", "café", "cafe\u{301}", "porte-monnaie", "va-t-il", "a-t-il", "maison-école"] + { + assert!( + fr::is_correct_with(dictionary, text) || ["va-t-il", "a-t-il", "maison-école"].contains(&text), + "real dictionary recognizes {text:?}" + ); + let issues = spelling(text, &[(0..text.len(), lang)]); + if !issues.is_empty() { + failures.push(format!("correct {lang:?} {text:?}: {issues:?}")); + } + } + for text in ["porte--monnaie", "va--t-il", "maison--école", "e\u{301}colllle", "cafffe\u{301}"] { + assert!(!fr::is_correct_with(dictionary, text), "the explicit dictionary entry must not already accept {text:?}"); + let issues = spelling(text, &[(0..text.len(), lang)]); + let expected = vec![(0, text.len(), lang)]; + if issues != expected { + failures.push(format!("typo {lang:?} {text:?}: {issues:?}, wanted {expected:?}")); + } + } + let text = "e\u{301}cole cafe\u{301} xyzxyz porte--monnaie"; + let foreign = text.find("xyzxyz").unwrap(); + let compound = text.find("porte").unwrap(); + let spans = [(0..foreign, lang), (foreign..compound, ProofLang::Other), (compound..text.len(), lang)]; + let issues = spelling(text, &spans); + if issues != [(compound, text.len(), lang)] { + failures.push(format!("mixed {lang:?}: {issues:?}")); + } + assert!(spelling(text, &[(0..text.len(), ProofLang::Other)]).is_empty()); + // An English word remains in its own dictionary next to the original French bytes. + let text = "e\u{301}cole hello cafe\u{301}"; + let en = text.find("hello").unwrap(); + let spans = [(0..en, lang), (en..en + 5, ProofLang::En), (en + 5..text.len(), lang)]; + let issues = spelling(text, &spans); + if !issues.is_empty() { + failures.push(format!("English boundary {lang:?}: {issues:?}")); + } + } + assert!(failures.is_empty(), "{}", failures.join("\n")); +} diff --git a/crates/proof/tests/french_packs.rs b/crates/proof/tests/french_packs.rs new file mode 100644 index 000000000..64ad46748 --- /dev/null +++ b/crates/proof/tests/french_packs.rs @@ -0,0 +1,124 @@ +//! French uses ordinary Language packs and keeps each regional locale at the public API. +#![allow(clippy::unwrap_used, clippy::expect_used)] + +use craft_proof::affix; +use craft_proof::{IssueKind, Options, ProofLang, check_text_with, is_correct_in_with, suggest_in}; +use std::path::{Path, PathBuf}; + +#[path = "../src/fr/test_dict.rs"] +mod fixture; + +struct Tools(PathBuf); +impl Drop for Tools { + fn drop(&mut self) { + craft_proof::set_dictionaries_folder(None); + craft_proof::packs::set_packs_dir(None); + craft_proof::remove_word("zxqrechargefr"); + let _ = std::fs::remove_dir_all(&self.0); + } +} + +fn pair(dir: &Path, name: &str, aff: &str, dic: &str) { + std::fs::create_dir_all(dir).unwrap(); + std::fs::write(dir.join(format!("{name}.aff")), aff).unwrap(); + std::fs::write(dir.join(format!("{name}.dic")), dic).unwrap(); +} + +#[test] +fn french_packs_regions_reload_and_original_ranges() { + assert!(!fixture::dictionary().is_empty()); + let root = std::env::temp_dir().join(format!("craft-fr-packs-{}", std::process::id())); + let _tools = Tools(root.clone()); + let pack = root.join("packs/fr"); + pair(&pack, "spelling", fixture::AFF, fixture::DIC); + std::fs::write(pack.join("pack.toml"), "locale = 'fr'\nname = 'Français'\nlicence = 'MIT'\n").unwrap(); + std::fs::write(pack.join("hyphenation.pat"), "ma1ison\n").unwrap(); + let folder = root.join("dictionaries"); + std::fs::create_dir_all(&folder).unwrap(); + craft_proof::packs::set_packs_dir(Some(root.join("packs"))); + craft_proof::set_dictionaries_folder(Some(folder.clone())); + let defaults = Options::default(); + let strict = Options { ignore_uppercase: false, ignore_numbers: false, ..defaults }; + check_locales(defaults, strict); + check_typography(strict); + let fr = ProofLang::from_tag(Some("fr-FR")); + let ca = ProofLang::from_tag(Some("fr-CA")); + check_user_word(fr, ca, strict); + check_overrides(folder, fr, ca, strict); +} + +fn check_locales(defaults: Options, strict: Options) { + for tag in ["fr", "fr-FR", "fr-CA", "fr-BE", "fr-CA-u-nu-latn", "fr-u-ca-gregory"] { + let lang = ProofLang::from_tag(Some(tag)); + assert!(matches!(lang, ProofLang::Pack(_)), "{tag}"); + assert!(craft_proof::has_dictionary(Some(tag)), "{tag}"); + assert!(craft_proof::pack_dictionary_file(lang).unwrap().ends_with("fr/spelling.dic"), "{tag}"); + for word in ["école", "e\u{301}cole", "l'arbre", "l’arbre", "maison-école"] { + assert!(is_correct_in_with(word, lang, &strict), "{tag}: {word}"); + } + assert!(!is_correct_in_with("maison--école", lang, &strict)); + assert!(!is_correct_in_with("l'arbres", lang, &strict)); + assert!(is_correct_in_with("SNCF", lang, &defaults)); + assert!(!is_correct_in_with("SNCF", lang, &strict)); + assert!(is_correct_in_with("mp3x", lang, &defaults)); + assert!(!is_correct_in_with("mp3x", lang, &strict)); + assert!(suggest_in("ecole", 6, lang).iter().any(|s| s == "école")); + let text = "🙂 maison\u{338} école"; + let issues = check_text_with(text, &[(0..text.len(), lang)], &strict, false, false); + assert_eq!(issues.len(), 1, "{tag}: {issues:?}"); + assert_eq!(&text[issues[0].start..issues[0].end], "maison\u{338}"); + assert_eq!(issues[0].lang, lang); + assert_eq!(issues[0].kind, IssueKind::Spelling); + assert_eq!(issues[0].message, "Faute d’orthographe possible"); + } +} + +fn check_typography(strict: Options) { + let fr = ProofLang::from_tag(Some("fr-FR")); + let ca = ProofLang::from_tag(Some("fr-CA")); + for lang in [fr, ca] { + assert_eq!(craft_proof::hyphen::hyphen_points_in("maison", &craft_proof::hyphen::Limits::default(), lang), [2]); + } + for tag in ["fr-FR", "fr-BE", "fr-u-ca-gregory", "fr-FR-u-rg-cazzzz"] { + let issues = craft_proof::check_grammar_in("Bonjour!", ProofLang::from_tag(Some(tag))); + assert_eq!(issues.len(), 1, "{tag}: {issues:?}"); + assert_eq!(issues[0].suggestions, ["\u{202f}!"]); + } + assert!(craft_proof::check_grammar_in("Bonjour!", ca).is_empty()); + assert!(craft_proof::check_grammar_in("Bonjour!", ProofLang::from_tag(Some("fr-Latn-CA"))).is_empty()); + assert!(craft_proof::check_grammar_in("Bonjour!", ProofLang::from_tag(Some("nl-NL"))).is_empty()); + assert!(craft_proof::fr::grammar::check_in("Bonjour!", ProofLang::En).is_empty()); + let text = "Bonjour! Salut!"; + let issues = check_text_with(text, &[(0..8, fr), (8..text.len(), ca)], &strict, true, false); + let spacing: Vec<_> = issues.iter().filter(|i| i.kind == IssueKind::Grammar && i.message.contains("fine insécable")).collect(); + assert_eq!(spacing.len(), 1, "{issues:?}"); + assert_eq!(spacing[0].lang, fr); +} + +fn check_user_word(fr: ProofLang, ca: ProofLang, strict: Options) { + craft_proof::add_word("zxqrechargefr"); + for lang in [fr, ca] { + assert!(is_correct_in_with("l’zxqrechargefr", lang, &strict)); + } + craft_proof::remove_word("zxqrechargefr"); + assert!(!is_correct_in_with("l’zxqrechargefr", ca, &strict)); +} + +fn check_overrides(folder: PathBuf, fr: ProofLang, ca: ProofLang, strict: Options) { + // Regional overrides keep the generic pack fallback and the shared reload generation. + pair(&folder, "fr_FR", "SET UTF-8\n", "1\nhexagonefixture\n"); + pair(&folder, "fr_CA", "SET UTF-8\n", "1\nquebecfixture\n"); + let generation = craft_proof::dictionary_generation(); + craft_proof::set_dictionaries_folder(Some(folder.clone())); + assert!(craft_proof::dictionary_generation() > generation); + assert!(is_correct_in_with("hexagonefixture", fr, &strict)); + assert!(!is_correct_in_with("hexagonefixture", ca, &strict)); + assert!(is_correct_in_with("quebecfixture", ca, &strict)); + assert!(!is_correct_in_with("quebecfixture", fr, &strict)); + assert!(craft_proof::pack_dictionary_file(ca).unwrap().ends_with("fr_CA.dic")); + std::fs::remove_file(folder.join("fr_CA.dic")).unwrap(); + std::fs::remove_file(folder.join("fr_CA.aff")).unwrap(); + craft_proof::set_dictionaries_folder(Some(folder)); + assert!(is_correct_in_with("école", ca, &strict), "removed regional override falls back to fr pack"); + assert!(craft_proof::pack_dictionary_file(ca).unwrap().ends_with("fr/spelling.dic")); +} From 3c5ffadd3b3fb4bcbf657a245ad9c480d2b6e304 Mon Sep 17 00:00:00 2001 From: totoz64 <47389794+totoz64@users.noreply.github.com> Date: Tue, 3 Nov 2026 11:53:24 +0100 Subject: [PATCH 0003/1159] Proofing: retain exact apostrophes in French compounds Check each original compound component before its ASCII-apostrophe alias. Add a curly-only dictionary regression, prove legacy fixture headers before rejecting WCAFF1, and clarify French-specific Pack grammar in the README. Preserve the existing one-letter component policy. --- crates/proof/README.md | 13 ++++++++----- crates/proof/src/fr/mod.rs | 14 +++++++++++++- crates/proof/tests/affix_compatibility.rs | 2 ++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/crates/proof/README.md b/crates/proof/README.md index b4b4b5ba8..4cc4b2ca4 100644 --- a/crates/proof/README.md +++ b/crates/proof/README.md @@ -89,19 +89,22 @@ pub fn load_errors() -> Vec<(String, String)>; // + (locale or folder, pub fn embedded_locales() -> &'static [&'static str]; // + locales with embedded upstream data, not our supplements ("en-us", "en-gb", "en-ca", "en-au", "pl", "pt", "it", "de" by default; the four English ones with CRAFT_PROOF_EMBED=en) // craft_proof (crate root) -pub enum ProofLang { …, Pack(PackLocale), Other } // + Pack: any language without tools of its own (nl, fr, sr-Latn…) +pub enum ProofLang { …, Pack(PackLocale), Other } // + Pack: a language without a dedicated built-in variant (nl, fr, sr-Latn…) pub struct PackLocale; // + Copy; PackLocale::new(tag) -> Option, .as_str() "nl-nl", .primary() "nl", Display impl ProofLang { pub fn pack_locale(self) -> Option; } // + pub fn pack_dictionary_file(lang: ProofLang) -> Option; // + the .dic a Pack language reads now; None = unchecked ``` -`ProofLang::from_tag` now gives `Pack(locale)` for every valid language tag it has no tools for +`ProofLang::from_tag` now gives `Pack(locale)` for every valid language tag without a dedicated built-in variant (`nl-NL` → `Pack("nl-nl")`, `fr-FR`, `ja-JP`, `sr-Latn`…); `Other` is left for `x-none`, `und`, `zxx`, `mul`, `mis`, `i-…` and malformed tags. A `Pack` language is spell-checked and given suggestions from its Hunspell dictionary (dictionaries folder → installed pack → system Hunspell) -through the shared affix engine with default tuning, and hyphenated from its pack's -`hyphenation.pat`; it has no grammar rules and the generic English spelling message. Without a -dictionary or pack it behaves exactly as `Other` did (no marks, English hyphenation fallback). +through the shared affix engine with default tuning (French tuning for `fr` packs), and +hyphenated from its pack's `hyphenation.pat`. Most pack languages use no dedicated grammar +rules; regional variants can reuse a built-in language's rules. French packs use the French +spelling message and the conservative French grammar and typography described above. Without a dictionary or pack, +spelling has no marks and hyphenation falls back to English; French grammar and typography +still apply independently of a spelling dictionary. `has_dictionary(tag)` / `ProofLang::is_proofed()` say whether a `Pack` language has a dictionary now (read on first use, cached). Loaded `Pack` dictionaries share a 256 MB budget (estimated from their files); the least recently used goes first, but never one used in the paragraph being checked, and languages with no dictionary take no room. Downstream `match`es on `ProofLang` need a `Pack(_)` arm; `code()` stays `&'static str` and is `""` for it, as for `Other`: anything that keys, saves or reports a language (settings, caches, JSON/MCP output) uses `tag()`, which is `code()` for the built-ins and the locale (`nl`, `fr-fr`) for a pack language. diff --git a/crates/proof/src/fr/mod.rs b/crates/proof/src/fr/mod.rs index 541fcac0d..c67c3ffc0 100644 --- a/crates/proof/src/fr/mod.rs +++ b/crates/proof/src/fr/mod.rs @@ -44,6 +44,7 @@ pub(crate) fn check(d: &AffixDict, word: &str, opts: &Options) -> bool { if known(d, w) { return true; } + let original = w; let w = w.replace('’', "'"); if known(d, &w) { return true; @@ -56,7 +57,7 @@ pub(crate) fn check(d: &AffixDict, word: &str, opts: &Options) -> bool { { return true; } - w.contains('-') && w.split('-').all(|p| !p.is_empty() && (p.chars().count() <= 1 || known(d, p))) + original.contains('-') && original.split('-').all(|p| !p.is_empty() && (p.chars().count() <= 1 || known(d, p) || known(d, &p.replace('’', "'")))) } fn known(d: &AffixDict, word: &str) -> bool { @@ -148,6 +149,17 @@ mod tests { assert!(!is_correct_with(&d, "maison\u{338}"), "a trailing mark is part of the word"); } + #[test] + fn curly_apostrophes_in_compounds_keep_exact_dictionary_parts() { + let d = AffixDict::from_hunspell("SET UTF-8\n", "4\naujourd’hui\nécole\ncafé\nl'école\n", &[]).unwrap(); + for word in ["aujourd’hui-école", "aujourd’hui-e\u{301}cole", "café-aujourd’hui", "l’école-aujourd’hui"] { + assert!(is_correct_with(&d, word), "{word}"); + } + for word in ["aujourd’hui-écolle", "aujourd’hui--école", "aujourd'hui-école"] { + assert!(!is_correct_with(&d, word), "{word}"); + } + } + #[test] fn explicit_curly_apostrophe_without_iconv() { let d = AffixDict::from_hunspell("SET UTF-8\n", "1\naujourd’hui\n", &[]).unwrap(); diff --git a/crates/proof/tests/affix_compatibility.rs b/crates/proof/tests/affix_compatibility.rs index 6d09e5f90..c18d9fd03 100644 --- a/crates/proof/tests/affix_compatibility.rs +++ b/crates/proof/tests/affix_compatibility.rs @@ -87,6 +87,8 @@ fn legacy_compact_data_is_refused_without_reinterpreting_homonyms() { assert_eq!(hex.len() % 2, 0); let bytes: Vec<_> = hex.as_bytes().as_chunks::<2>().0.iter().map(|pair| u8::from_str_radix(std::str::from_utf8(pair).unwrap(), 16).unwrap()).collect(); + let raw = miniz_oxide::inflate::decompress_to_vec(&bytes).unwrap(); + assert!(raw.starts_with(b"WCAFF1\n"), "fixture must contain WCAFF1 data: {}", case.name); assert!(AffixDict::from_bytes(&bytes).is_err(), "WCAFF1 must be regenerated as WCAFF2: {}", case.name); } } From 870bad52e20a62e0260c90d5d18c5643181377b7 Mon Sep 17 00:00:00 2001 From: totoz64 <47389794+totoz64@users.noreply.github.com> Date: Wed, 4 Nov 2026 17:43:22 +0100 Subject: [PATCH 0004/1159] M15: Preserve accepted suggestions after the shared collection budget runs out Collector::keep has already validated the stored text. Avoid rechecking identity candidates against an exhausted SuggestSteps budget; validate only changed case and fall back to the accepted original if that validation cannot succeed. Keep the same time/work caps, exclusion rules, parser and ranking. An integrated regression uses the real REP/TRY/affix pipeline and normal budget: it loses the accepted affixed suggestion on parent b5 and retains it with this change, for lower and upper input. No injected state or relaxed limit. Exact-source fmt and strict proof Clippy passed; full local/hosted validation is still being collected. Addresses CodeRabbit4470300723. --- crates/proof/src/affix_suggest.rs | 40 +++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/crates/proof/src/affix_suggest.rs b/crates/proof/src/affix_suggest.rs index 07194c548..b3f07e43c 100644 --- a/crates/proof/src/affix_suggest.rs +++ b/crates/proof/src/affix_suggest.rs @@ -452,13 +452,9 @@ pub(crate) fn suggest_in(d: &AffixDict, word: &str, max: usize, t: &Tuning) -> V let changed: Vec<_> = candidate.split(' ').collect(); original.len() == changed.len() && original.iter().zip(changed).all(|(a, b)| d.can_suggest_cased(a, b)) }; - let t = if eligible(&t) { - t - } else if eligible(&text) { - text.clone() - } else { - continue; - }; + // Collector::keep already accepted text. Collection may have exhausted the shared + // affix budget, so recheck only a changed case and otherwise retain that accepted form. + let t = if t == text || eligible(&t) { t } else { text.clone() }; // Shown as the dictionary's OCONV table writes it (`'` → `’` in French). let t = d.output(&t).into_owned(); if t != word && !out.contains(&t) { @@ -904,4 +900,34 @@ mod tests { assert!(suggest_in(&d, "neer", 4, &english).contains(&"ne'er".to_string())); assert_eq!(suggest_in(&d, "neer", 4, &Tuning::DEFAULT).first().map(String::as_str), Some("ne'er"), "an elision"); } + /// A REP candidate is admitted before costly TRY checks consume the normal shared budget. + /// Finalization must retain that accepted affixed word, including a conservative case fallback. + #[test] + fn accepted_affixed_suggestions_survive_exhausted_collection_budget() { + let input = format!("{}x{}", "a".repeat(20), "a".repeat(19)); + let mut aff = String::from("SET UTF-8\nFLAG num\nTRY "); + aff.extend(('a'..='z').chain((0x100..0x166).filter_map(char::from_u32))); + aff.push_str(&format!("\nREP 1\nREP {input} kots\n")); + // The bounded adversarial rule shape of the existing shared-budget regression. + for f in 1..=1000u32 { + let c = char::from_u32(0x4e00 + f).unwrap(); + aff.push_str(&format!("PFX {f} Y 1\nPFX {f} {c} 0 .\n")); + } + let conts: Vec = (0..286).map(|k| (5000 + k * 7).to_string()).collect(); + for len in 1..=40 { + for k in 0..50 { + let f = 5000 + (len - 1) * 50 + k; + aff.push_str(&format!("SFX {f} Y 1\nSFX {f} 0 {}/{} .\n", "a".repeat(len), conts.join(","))); + } + } + aff.push_str("SFX 9000 Y 1\nSFX 9000 0 s .\n"); + let d = AffixDict::from_hunspell(&aff, "3\nb/1,5000\nab/2\nkot/9000\n", &[]).unwrap(); + assert!(!d.stems().any(|s| s == "kots") && d.can_suggest("kots")); + for word in [input.clone(), input.to_uppercase()] { + let got = suggest_in(&d, &word, 1, &Tuning::DEFAULT); + assert_eq!(got.len(), 1, "accepted affixed candidate lost: {word}: {got:?}"); + assert!(got[0].eq_ignore_ascii_case("kots"), "{word}: {got:?}"); + assert!(d.can_suggest(&got[0])); + } + } } From 55a16b7a724cd7ab3f9873a415fbd6fbcd8b3c96 Mon Sep 17 00:00:00 2001 From: totoz64 <47389794+totoz64@users.noreply.github.com> Date: Wed, 4 Nov 2026 17:56:56 +0100 Subject: [PATCH 0005/1159] M15: Validate exact suggestion eligibility before admitting candidates Collector::keep now uses the exact-case eligibility guard before insertion, preserving NOSUGGEST, SUBSTANDARD, FORBIDDENWORD and KEEPCASE rules. Finalization can safely retain the admitted original after the collection budget is exhausted, and still validates actual case variants. No fresh budget, parser, ranking or historical fixture change. This completes CodeRabbit4470300723 without the provisional870 exclusion regression. On this exact source: workspace fmt and strict proof Clippy passed; 458 unit tests passed with52 ignored, compatibility2 and French Pack integration1 passed, and the real pinned Dicollecte test passed. The integrated normal-budget regression and all15 suggestion/casing/exclusion tests passed together in the fresh complete execution. Hosted validation of this new SHA is still required. --- crates/proof/src/affix_suggest.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/proof/src/affix_suggest.rs b/crates/proof/src/affix_suggest.rs index b3f07e43c..4d8bfea59 100644 --- a/crates/proof/src/affix_suggest.rs +++ b/crates/proof/src/affix_suggest.rs @@ -248,7 +248,8 @@ impl Collector<'_> { /// Keep a suggestion known to be correct, unless the dictionary says never to offer it. fn keep(&mut self, text: String, cost: u32) { - if !text.split(' ').all(|part| self.d.can_suggest(part)) { + // Reject exact excluded spellings at admission, before later candidates spend the budget. + if !text.split(' ').all(|part| self.d.can_suggest_cased(part, part)) { return; } self.seq += 1; From 75710cfd20a11fecf1a192db43485c61a5f0c59b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 12 Nov 2026 07:18:59 -0500 Subject: [PATCH 0006/1159] ci: lock workflow actions through an actions.lock lockfile Mutable action tags are a supply-chain risk: a moved tag runs different code without a diff. Every workflow action is now pinned through .github/workflows/actions.lock, and CI verifies the lockfile so an out-of-sync or tampered one fails instead of running. --- .github/workflows/actions.lock | 170 +++++++++++++++++++++++ .github/workflows/ai-core.yml | 6 +- .github/workflows/atomic.yml | 6 +- .github/workflows/audio.yml | 32 ++--- .github/workflows/crw.yml | 26 ++-- .github/workflows/dialogs.yml | 6 +- .github/workflows/doc.yml | 6 +- .github/workflows/docpath.yml | 6 +- .github/workflows/ecproj-format.yml | 6 +- .github/workflows/font-store.yml | 6 +- .github/workflows/formats.yml | 8 +- .github/workflows/i18n.yml | 6 +- .github/workflows/jpeg.yml | 8 +- .github/workflows/keymap.yml | 28 ++-- .github/workflows/media.yml | 16 +-- .github/workflows/pcraft-format.yml | 6 +- .github/workflows/pdf.yml | 40 +++--- .github/workflows/print.yml | 6 +- .github/workflows/proof.yml | 10 +- .github/workflows/ui.yml | 16 +-- .github/workflows/update.yml | 6 +- .github/workflows/vectorcraft-format.yml | 6 +- .github/workflows/video.yml | 16 +-- .github/workflows/web-api.yml | 8 +- .github/workflows/web-host.yml | 10 +- .github/workflows/window.yml | 6 +- sonar-project.properties | 10 +- 27 files changed, 327 insertions(+), 149 deletions(-) create mode 100644 .github/workflows/actions.lock diff --git a/.github/workflows/actions.lock b/.github/workflows/actions.lock new file mode 100644 index 000000000..0d6c3b8f9 --- /dev/null +++ b/.github/workflows/actions.lock @@ -0,0 +1,170 @@ +# This file is machine-generated by `gh actions-lock`. +# Do not edit by hand; run `gh actions-lock` to update. +# Docs: https://gh.io/actions-lockfile +version: 'v0.0.3' +workflows: + '.github/workflows/ai-core.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/atomic.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/audio.yml': + - 'actions/cache@v6.1.0' + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/crw.yml': + - 'actions/cache@v6.1.0' + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/dialogs.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/doc.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/docpath.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/ecproj-format.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/font-store.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/formats.yml': + - 'actions/checkout@v7.0.1' + - 'dtolnay/rust-toolchain@stable' + - 'step-security/harden-runner@v2.22.4' + - 'swatinem/rust-cache@v2.9.3' + '.github/workflows/house-rules.yml': [] + '.github/workflows/i18n.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/jpeg.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/keymap.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/media.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/pcraft-format.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/pdf.yml': + - 'actions/checkout@v7.0.1' + - 'actions/setup-python@v7.1.0' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + - 'step-security/taiki-e-install-action@v2.87.32' + '.github/workflows/print.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/proof.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/sonarcloud.yml': [] + '.github/workflows/ui.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/update.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/vectorcraft-format.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/video.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/web-api.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/taiki-e-install-action@v2.87.32' + '.github/workflows/web-host.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + - 'step-security/taiki-e-install-action@v2.87.32' + '.github/workflows/window.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' +dependencies: + 'actions/cache@v6.1.0': + ref: 'v6.1.0' + commit: 'sha1-55cc8345863c7cc4c66a329aec7e433d2d1c52a9' + owner_id: 44036562 + repo_id: 215566462 + 'actions/checkout@v7.0.1': + ref: 'v7.0.1' + commit: 'sha1-3d3c42e5aac5ba805825da76410c181273ba90b1' + owner_id: 44036562 + repo_id: 197814629 + 'actions/setup-python@v7.1.0': + ref: 'v7.1.0' + commit: 'sha1-6d4fd22b20465ccc0f7354476588e8df7c9ca0e0' + owner_id: 44036562 + repo_id: 192625525 + 'dtolnay/rust-toolchain@stable': + ref: 'stable' + commit: 'sha1-b725eda525362e7cf34af9a7243cac1e3f01a7ec' + owner_id: 1940490 + repo_id: 260749683 + 'step-security/dtolnay-rust-toolchain@v1.0.0': + ref: 'v1.0.0' + commit: 'sha1-8dd607977d06adf4ed49b190381e74c17a9dd25f' + owner_id: 88700172 + repo_id: 1249865941 + 'step-security/harden-runner@v2.22.4': + ref: 'v2.22.4' + commit: 'sha1-1d88384fd173af6ecb012dfd0264a306b711241a' + owner_id: 88700172 + repo_id: 422287306 + 'step-security/rust-cache@v2.9.2': + ref: 'v2.9.2' + commit: 'sha1-8871c8ba063416da7c32246932ea6ae08700453e' + owner_id: 88700172 + repo_id: 822636713 + 'step-security/taiki-e-install-action@v2.87.32': + ref: 'v2.87.32' + commit: 'sha1-76747d22a7567cc4438546a84f11bdf18e670ab8' + owner_id: 88700172 + repo_id: 1382753260 + 'swatinem/rust-cache@v2.9.3': + ref: 'v2.9.3' + commit: 'sha1-d1527c8f67ef9af357290475d4af4b0fab8f12e0' + owner_id: 580492 + repo_id: 298565987 diff --git a/.github/workflows/ai-core.yml b/.github/workflows/ai-core.yml index cc8d738ac..5471bf1ec 100644 --- a/.github/workflows/ai-core.yml +++ b/.github/workflows/ai-core.yml @@ -28,11 +28,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/atomic.yml b/.github/workflows/atomic.yml index 1471832d2..24f59bc2b 100644 --- a/.github/workflows/atomic.yml +++ b/.github/workflows/atomic.yml @@ -28,11 +28,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/audio.yml b/.github/workflows/audio.yml index 0104f848e..3944d3591 100644 --- a/.github/workflows/audio.yml +++ b/.github/workflows/audio.yml @@ -25,18 +25,18 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-audio-lint save-if: ${{ github.ref == 'refs/heads/main' }} @@ -69,15 +69,15 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false # The workspace's rust-version. - run: rustup toolchain install 1.95.0 --profile minimal - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-audio-msrv save-if: ${{ github.ref == 'refs/heads/main' }} @@ -88,16 +88,16 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-audio-features save-if: ${{ github.ref == 'refs/heads/main' }} @@ -113,20 +113,20 @@ jobs: runs-on: ubuntu-26.04 steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-audio-test save-if: ${{ github.ref == 'refs/heads/main' }} - - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + - uses: actions/cache@v6.1.0 with: path: ${{ runner.temp }}/media-fixtures/opus/vectors # The archive's SHA-256 (scripts/fetch-opus-vectors.sh checks it). diff --git a/.github/workflows/crw.yml b/.github/workflows/crw.yml index 8fec98661..54be7ba3b 100644 --- a/.github/workflows/crw.yml +++ b/.github/workflows/crw.yml @@ -29,17 +29,17 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-crw-${{ matrix.os }} save-if: ${{ github.ref == 'refs/heads/main' }} @@ -53,17 +53,17 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-crw-wasm save-if: ${{ github.ref == 'refs/heads/main' }} @@ -75,21 +75,21 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-crw-samples save-if: ${{ github.ref == 'refs/heads/main' }} - name: Cache the CC0 sample files - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + uses: actions/cache@v6.1.0 with: path: ${{ runner.temp }}/crw-samples key: crw-samples-${{ hashFiles('crates/crw/tests/samples.json') }} diff --git a/.github/workflows/dialogs.yml b/.github/workflows/dialogs.yml index 97a931aec..c8d43d725 100644 --- a/.github/workflows/dialogs.yml +++ b/.github/workflows/dialogs.yml @@ -23,15 +23,15 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: # The token isn't needed after checkout; cargo runs build scripts and proc macros from # dependencies, so don't leave it in .git/config for that third-party code to read. persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: clippy diff --git a/.github/workflows/doc.yml b/.github/workflows/doc.yml index a0c619c1e..6caa4f0ee 100644 --- a/.github/workflows/doc.yml +++ b/.github/workflows/doc.yml @@ -28,13 +28,13 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/docpath.yml b/.github/workflows/docpath.yml index 571b65eca..e0115f6f5 100644 --- a/.github/workflows/docpath.yml +++ b/.github/workflows/docpath.yml @@ -28,11 +28,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/ecproj-format.yml b/.github/workflows/ecproj-format.yml index 16bab05c7..24a3350af 100644 --- a/.github/workflows/ecproj-format.yml +++ b/.github/workflows/ecproj-format.yml @@ -28,13 +28,13 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/font-store.yml b/.github/workflows/font-store.yml index f246f621a..0578f18ec 100644 --- a/.github/workflows/font-store.yml +++ b/.github/workflows/font-store.yml @@ -28,11 +28,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/formats.yml b/.github/workflows/formats.yml index e19902e8c..84d2c0d7e 100644 --- a/.github/workflows/formats.yml +++ b/.github/workflows/formats.yml @@ -72,17 +72,17 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + - uses: dtolnay/rust-toolchain@stable with: components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + - uses: Swatinem/rust-cache@v2.9.3 with: key: ${{ matrix.crate }} save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/i18n.yml b/.github/workflows/i18n.yml index 5f717aeff..7e6903188 100644 --- a/.github/workflows/i18n.yml +++ b/.github/workflows/i18n.yml @@ -28,11 +28,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/jpeg.yml b/.github/workflows/jpeg.yml index 900baf678..21db0ab79 100644 --- a/.github/workflows/jpeg.yml +++ b/.github/workflows/jpeg.yml @@ -25,18 +25,18 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-jpeg save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/keymap.yml b/.github/workflows/keymap.yml index aba535265..41fcc0502 100644 --- a/.github/workflows/keymap.yml +++ b/.github/workflows/keymap.yml @@ -27,24 +27,24 @@ jobs: CRAFT_FONTS_DIR: ${{ github.workspace }}/.test-inputs/craft-fonts steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Checkout test fonts - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + uses: actions/checkout@v7.0.1 with: repository: storytold/craft-fonts ref: b076417b5880bd4154bd22f00695b1d28c6bea83 path: .test-inputs/craft-fonts persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-keymap-linux save-if: ${{ github.ref == 'refs/heads/main' }} @@ -71,23 +71,23 @@ jobs: CRAFT_FONTS_DIR: ${{ github.workspace }}/.test-inputs/craft-fonts steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Checkout test fonts - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + uses: actions/checkout@v7.0.1 with: repository: storytold/craft-fonts ref: b076417b5880bd4154bd22f00695b1d28c6bea83 path: .test-inputs/craft-fonts persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-keymap-${{ matrix.os }} save-if: ${{ github.ref == 'refs/heads/main' }} @@ -98,17 +98,17 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-keymap-wasm save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/media.yml b/.github/workflows/media.yml index cb3912d54..4814e35ca 100644 --- a/.github/workflows/media.yml +++ b/.github/workflows/media.yml @@ -25,18 +25,18 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-media-lint save-if: ${{ github.ref == 'refs/heads/main' }} @@ -56,16 +56,16 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-media-test save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/pcraft-format.yml b/.github/workflows/pcraft-format.yml index 177bb4593..9c66b5a10 100644 --- a/.github/workflows/pcraft-format.yml +++ b/.github/workflows/pcraft-format.yml @@ -28,13 +28,13 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" components: rustfmt, clippy diff --git a/.github/workflows/pdf.yml b/.github/workflows/pdf.yml index c72ac7f72..25cf71d4e 100644 --- a/.github/workflows/pdf.yml +++ b/.github/workflows/pdf.yml @@ -31,19 +31,19 @@ jobs: RUSTFLAGS: --cfg craft_pdf_unicode_fixtures steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-pdf save-if: ${{ github.ref == 'refs/heads/main' }} - - uses: actions/setup-python@v7 + - uses: actions/setup-python@v7.1.0 with: python-version: "3.12" - name: Provision licensed PDF test fonts @@ -61,14 +61,14 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-pdf-backends save-if: ${{ github.ref == 'refs/heads/main' }} @@ -81,19 +81,19 @@ jobs: CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-pdf-wasm save-if: ${{ github.ref == 'refs/heads/main' }} - - uses: actions/setup-python@v7 + - uses: actions/setup-python@v7.1.0 with: python-version: "3.12" - name: Provision licensed PDF test fonts @@ -111,7 +111,7 @@ jobs: - name: Install the matching WASM test runner # A prebuilt, checksum-verified binary instead of building it from source on every run. # A version the pinned action does not list yet falls back to `cargo install --locked`. - uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32 + uses: step-security/taiki-e-install-action@v2.87.32 with: tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }} fallback: cargo-install @@ -122,19 +122,19 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: clippy - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-plate-test save-if: ${{ github.ref == 'refs/heads/main' }} - - uses: actions/setup-python@v7 + - uses: actions/setup-python@v7.1.0 with: python-version: "3.12" - name: Provision licensed PDF test fonts diff --git a/.github/workflows/print.yml b/.github/workflows/print.yml index 3838e6c42..8e2108e09 100644 --- a/.github/workflows/print.yml +++ b/.github/workflows/print.yml @@ -24,11 +24,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/proof.yml b/.github/workflows/proof.yml index 0035b1c04..10365a230 100644 --- a/.github/workflows/proof.yml +++ b/.github/workflows/proof.yml @@ -25,27 +25,27 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false # The Language data (craft-i18n `language-data/`), embedded by crates/proof/build.rs. # Pinned to storytold/craft-i18n#114's head (#109's SCOWL dictionaries + judgment/judgement, # CP2.1); TODO(CP2.1): move to #114's merge commit once it merges. - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: repository: storytold/craft-i18n ref: c239f934ace90b7939afaceb23b472ceeafd6b8a path: craft-i18n persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-proof save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/ui.yml b/.github/workflows/ui.yml index 0892f5a2c..f15ef6ddc 100644 --- a/.github/workflows/ui.yml +++ b/.github/workflows/ui.yml @@ -30,21 +30,21 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 - name: Install software Vulkan renderer if: runner.os == 'Linux' run: | sudo apt-get update sudo apt-get install -y mesa-vulkan-drivers libvulkan1 echo 'WGPU_BACKEND=vulkan' >> "$GITHUB_ENV" - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-ui save-if: ${{ github.ref == 'refs/heads/main' }} @@ -59,15 +59,15 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-ui-wasm save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/update.yml b/.github/workflows/update.yml index f9c8e1498..9a7139026 100644 --- a/.github/workflows/update.yml +++ b/.github/workflows/update.yml @@ -28,15 +28,15 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: # The token isn't needed after checkout; cargo runs build scripts and proc macros from # dependencies, so don't leave it in .git/config for that third-party code to read. persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/vectorcraft-format.yml b/.github/workflows/vectorcraft-format.yml index b2b20463a..74144f30f 100644 --- a/.github/workflows/vectorcraft-format.yml +++ b/.github/workflows/vectorcraft-format.yml @@ -28,13 +28,13 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" components: rustfmt, clippy diff --git a/.github/workflows/video.yml b/.github/workflows/video.yml index ec1c16eda..fa667bdfc 100644 --- a/.github/workflows/video.yml +++ b/.github/workflows/video.yml @@ -22,18 +22,18 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-video-lint - run: cargo fmt -p craft-video --check @@ -60,16 +60,16 @@ jobs: name: Video tests (${{ matrix.os }}) steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-video-${{ matrix.os }} - name: Install ffmpeg (Linux) diff --git a/.github/workflows/web-api.yml b/.github/workflows/web-api.yml index 2460fb569..4bf1a30fe 100644 --- a/.github/workflows/web-api.yml +++ b/.github/workflows/web-api.yml @@ -26,13 +26,13 @@ jobs: CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy @@ -46,7 +46,7 @@ jobs: - name: Install the matching WASM test runner # A prebuilt, checksum-verified binary instead of building it from source on every run. # A version the pinned action does not list yet falls back to `cargo install --locked`. - uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32 + uses: step-security/taiki-e-install-action@v2.87.32 with: tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }} fallback: cargo-install diff --git a/.github/workflows/web-host.yml b/.github/workflows/web-host.yml index b780ee76b..76e6701e5 100644 --- a/.github/workflows/web-host.yml +++ b/.github/workflows/web-host.yml @@ -27,18 +27,18 @@ jobs: CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-web-host save-if: ${{ github.ref == 'refs/heads/main' }} @@ -55,7 +55,7 @@ jobs: - name: Install the matching WASM test runner # A prebuilt, checksum-verified binary instead of building it from source on every run. # A version the pinned action does not list yet falls back to `cargo install --locked`. - uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32 + uses: step-security/taiki-e-install-action@v2.87.32 with: tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }} fallback: cargo-install diff --git a/.github/workflows/window.yml b/.github/workflows/window.yml index b07bce384..f14b24038 100644 --- a/.github/workflows/window.yml +++ b/.github/workflows/window.yml @@ -28,15 +28,15 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: # The token isn't needed after checkout; cargo runs build scripts and proc macros from # dependencies, so don't leave it in .git/config for that third-party code to read. persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/sonar-project.properties b/sonar-project.properties index d08c71698..516e4d5cc 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -15,6 +15,14 @@ sonar.cpd.exclusions=**/*tables.rs # S4790 (weak hash): the ICC specification defines the profile ID as the MD5 of the profile, so # no other algorithm produces an ID other software accepts. It identifies, it does not protect. -sonar.issue.ignore.multicriteria=icc_profile_id +sonar.issue.ignore.multicriteria=icc_profile_id,gha-lockfile sonar.issue.ignore.multicriteria.icc_profile_id.ruleKey=rust:S4790 sonar.issue.ignore.multicriteria.icc_profile_id.resourceKey=crates/cms/src/write.rs + +# githubactions:S7637 asks for every action to be pinned to a commit SHA. A workflow covered by +# .github/workflows/actions.lock runs the commit the lockfile records, so its tag refs are pinned +# and the lockfile covers the actions those actions use too. Sonar does not read lockfiles yet, so +# the rule flags the tag refs anyway: +# https://community.sonarsource.com/t/githubactions-s7637-false-positives-for-actions-pinned-through-native-actions-lock/190814 +sonar.issue.ignore.multicriteria.gha-lockfile.ruleKey=githubactions:S7637 +sonar.issue.ignore.multicriteria.gha-lockfile.resourceKey=.github/workflows/** From 5ba5b82fad19ac0baa1058e0cb1535af656a34a8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 12 Nov 2026 07:22:55 -0500 Subject: [PATCH 0007/1159] ci: verify the actions lockfile in CI Fails the build when .github/workflows/actions.lock stops matching the workflows, or when a locked pin no longer verifies upstream, so a stale or tampered lockfile cannot go unnoticed between dependency updates. --- .github/workflows/actions.lock | 3 +++ .github/workflows/lockfile.yml | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 .github/workflows/lockfile.yml diff --git a/.github/workflows/actions.lock b/.github/workflows/actions.lock index 0d6c3b8f9..c2e897943 100644 --- a/.github/workflows/actions.lock +++ b/.github/workflows/actions.lock @@ -63,6 +63,9 @@ workflows: - 'step-security/dtolnay-rust-toolchain@v1.0.0' - 'step-security/harden-runner@v2.22.4' - 'step-security/rust-cache@v2.9.2' + '.github/workflows/lockfile.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/harden-runner@v2.22.4' '.github/workflows/media.yml': - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' diff --git a/.github/workflows/lockfile.yml b/.github/workflows/lockfile.yml new file mode 100644 index 000000000..e630c9a8f --- /dev/null +++ b/.github/workflows/lockfile.yml @@ -0,0 +1,34 @@ +name: Lockfile + +# Fails when .github/workflows/actions.lock is out of sync with the workflows, or when a locked pin +# no longer verifies upstream, so a moved tag cannot slip a different commit into a run. +# Docs: https://gh.io/actions-lockfile +on: + pull_request: + push: + branches: [main] + workflow_dispatch: + +permissions: + contents: read + +env: + GH_TOKEN: ${{ github.token }} + +jobs: + lockfile: + name: Lockfile is in sync + runs-on: ubuntu-24.04 + timeout-minutes: 10 + steps: + - name: Harden runner + uses: step-security/harden-runner@v2.22.4 + with: + egress-policy: audit + - uses: actions/checkout@v7.0.1 + with: + persist-credentials: false + - name: Install gh-actions-lock + run: gh extension install github/gh-actions-lock --pin v0.4.1 + - name: Verify the lockfile + run: gh actions-lock --verify From 88864de95f194ea7b81273a241868fcb94930153 Mon Sep 17 00:00:00 2001 From: David Mann Date: Thu, 12 Nov 2026 16:52:07 +0100 Subject: [PATCH 0008/1159] fix(flags): the review findings on craft-flags (#1119) - text handed to Flags::from_parts gets the same size limit as a file - the file is read once, at most one byte past the limit - a file named by CRAFT_FLAGS_FILE that does not exist is reported - set() holds a lock file beside the flags file from read to write, so two programs changing flags at once keep both changes - set() keeps a comment after the flag it changes - set() refuses to write a file larger than the reader accepts Refs #1118 --- crates/flags/src/lib.rs | 7 ++++- crates/flags/src/store.rs | 65 +++++++++++++++++++++++++++++---------- crates/flags/src/tests.rs | 59 +++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 17 deletions(-) diff --git a/crates/flags/src/lib.rs b/crates/flags/src/lib.rs index c65e9218f..8552bab2c 100644 --- a/crates/flags/src/lib.rs +++ b/crates/flags/src/lib.rs @@ -38,7 +38,7 @@ use std::collections::BTreeMap; use std::path::PathBuf; #[cfg(not(target_arch = "wasm32"))] -mod store; +pub(crate) mod store; #[cfg(not(target_arch = "wasm32"))] pub use store::{FlagError, load, set}; @@ -135,6 +135,11 @@ impl Flags { } fn read_file(&mut self, text: &str) { + // The same limit as a file read from disk, also for text handed in directly. + if text.len() as u64 > MAX_FILE_BYTES { + self.problems.push(format!("the flags file is larger than {MAX_FILE_BYTES} bytes, so every flag is at its default")); + return; + } let doc = match text.parse::() { Ok(d) => d, Err(e) => { diff --git a/crates/flags/src/store.rs b/crates/flags/src/store.rs index a601d8731..6f422aa09 100644 --- a/crates/flags/src/store.rs +++ b/crates/flags/src/store.rs @@ -1,9 +1,10 @@ //! Reading and changing the flags file (desktop only). use std::fmt; -use std::path::Path; +use std::io::Read; +use std::path::{Path, PathBuf}; -use crate::{ENV, Flags, MAX_FILE_BYTES, default_path, valid_id}; +use crate::{ENV, FILE_ENV, Flags, MAX_FILE_BYTES, default_path, valid_id}; /// Why a flag could not be changed. #[derive(Debug)] @@ -32,32 +33,47 @@ impl std::error::Error for FlagError {} /// The file's text, `None` when it does not exist. A file that isn't a regular file, is too big /// or isn't UTF-8 is an error. fn read_text(path: &Path) -> Result, FlagError> { - let meta = match std::fs::metadata(path) { - Ok(m) => m, + let file = match std::fs::File::open(path) { + Ok(f) => f, Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), Err(e) => return Err(FlagError::Io(e)), }; + let meta = file.metadata().map_err(FlagError::Io)?; if !meta.is_file() { return Err(FlagError::Unreadable(format!("{} is not a regular file", path.display()))); } - if meta.len() > MAX_FILE_BYTES { + // Read at most one byte past the limit from the file opened: one that grew or was replaced + // after a size check can't bring more. + let mut bytes = Vec::new(); + file.take(MAX_FILE_BYTES + 1).read_to_end(&mut bytes).map_err(FlagError::Io)?; + if bytes.len() as u64 > MAX_FILE_BYTES { return Err(FlagError::Unreadable(format!("{} is larger than {MAX_FILE_BYTES} bytes", path.display()))); } - let bytes = std::fs::read(path).map_err(FlagError::Io)?; String::from_utf8(bytes).map(Some).map_err(|_| FlagError::Unreadable(format!("{} is not UTF-8 text", path.display()))) } /// The flags of this run: [`default_path`] and `CRAFT_FLAGS`. Never fails; what could not be /// read is in [`Flags::problems`]. pub fn load() -> Flags { - let env = std::env::var(ENV).ok(); - let Some(path) = default_path() else { - return Flags::from_parts(None, env.as_deref()); + let named = std::env::var_os(FILE_ENV).is_some_and(|p| !p.is_empty()); + load_from(default_path(), named, std::env::var(ENV).ok().as_deref()) +} + +/// [`load`] from `path`; `named` when `CRAFT_FLAGS_FILE` chose it, so a missing file is worth a +/// word (the usual file not existing yet is not). +pub(crate) fn load_from(path: Option, named: bool, env: Option<&str>) -> Flags { + let Some(path) = path else { + return Flags::from_parts(None, env); }; match read_text(&path) { - Ok(text) => Flags::from_parts(text.as_deref(), env.as_deref()), + Ok(None) if named => { + let mut flags = Flags::from_parts(None, env); + flags.problems.insert(0, format!("{}, named by {FILE_ENV}, does not exist; every flag is at its default", path.display())); + flags + } + Ok(text) => Flags::from_parts(text.as_deref(), env), Err(e) => { - let mut flags = Flags::from_parts(None, env.as_deref()); + let mut flags = Flags::from_parts(None, env); flags.problems.insert(0, format!("{}: {e}; every flag is at its default", path.display())); flags } @@ -65,11 +81,21 @@ pub fn load() -> Flags { } /// Turn the flag `id` on or off in the file at `path` (made if missing). Everything else in the -/// file, comments included, is kept; the write is atomic. +/// file, comments included, is kept; the write is atomic. Two programs changing flags at once +/// (the launcher and an app) take turns, so neither change is lost. pub fn set(path: &Path, id: &str, on: bool) -> Result<(), FlagError> { if !valid_id(id) { return Err(FlagError::BadId(id.to_string())); } + if let Some(dir) = path.parent() { + std::fs::create_dir_all(dir).map_err(FlagError::Io)?; + } + // A lock file beside the flags file, which itself is replaced on every write. Released when + // `_lock` is dropped (and by the system if the process ends). + let mut lock_path = path.as_os_str().to_owned(); + lock_path.push(".lock"); + let _lock = std::fs::OpenOptions::new().create(true).truncate(false).write(true).open(PathBuf::from(lock_path)).map_err(FlagError::Io)?; + _lock.lock().map_err(FlagError::Io)?; let mut doc = match read_text(path)? { Some(text) => text.parse::().map_err(|e| FlagError::Unreadable(e.to_string()))?, None => toml_edit::DocumentMut::new(), @@ -78,9 +104,16 @@ pub fn set(path: &Path, id: &str, on: bool) -> Result<(), FlagError> { let Some(table) = item.as_table_like_mut() else { return Err(FlagError::Unreadable("`flags` is not a table".into())); }; - table.insert(id, toml_edit::value(on)); - if let Some(dir) = path.parent() { - std::fs::create_dir_all(dir).map_err(FlagError::Io)?; + // Keep what surrounds the old value, such as a comment after it. + let mut value = toml_edit::Value::from(on); + if let Some(old) = table.get(id).and_then(toml_edit::Item::as_value) { + *value.decor_mut() = old.decor().clone(); } - craft_atomic::atomic_write(path, doc.to_string().as_bytes()).map_err(FlagError::Io) + table.insert(id, toml_edit::Item::Value(value)); + let text = doc.to_string(); + // What can't be read back is not written: the file stays as it was. + if text.len() as u64 > MAX_FILE_BYTES { + return Err(FlagError::Unreadable(format!("the flags file would grow past {MAX_FILE_BYTES} bytes"))); + } + craft_atomic::atomic_write(path, text.as_bytes()).map_err(FlagError::Io) } diff --git a/crates/flags/src/tests.rs b/crates/flags/src/tests.rs index 3f4bef667..92f690037 100644 --- a/crates/flags/src/tests.rs +++ b/crates/flags/src/tests.rs @@ -48,6 +48,15 @@ fn bad_entries_are_reported_and_left_out_never_fatal() { assert!(!Flags::from_parts(Some("flags = 3"), None).problems.is_empty()); } +#[test] +fn oversized_text_is_not_read() { + let mut text = String::from("[flags]\nAI-MCP = true\n"); + text.push_str(&"#".repeat(MAX_FILE_BYTES as usize)); + let flags = Flags::from_parts(Some(&text), None); + assert!(!flags.enabled(&AI_MCP)); + assert!(flags.problems.iter().any(|p| p.contains("larger than"))); +} + #[test] fn many_entries_are_capped() { let mut text = String::from("[flags]\n"); @@ -79,6 +88,7 @@ fn ids_are_checked() { #[cfg(not(target_arch = "wasm32"))] mod file { use super::super::*; + use crate::store; #[test] fn set_creates_the_file_and_keeps_what_else_is_in_it() { @@ -98,6 +108,55 @@ mod file { assert!(flags.enabled(&AI_MODELS)); } + #[test] + fn set_keeps_the_comment_after_a_flag() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join(FILE_NAME); + std::fs::write(&path, "[flags]\nAI-MCP = true # on for testing\n").unwrap(); + set(&path, "AI-MCP", false).unwrap(); + let text = std::fs::read_to_string(&path).unwrap(); + assert!(text.contains("AI-MCP = false # on for testing"), "{text}"); + } + + #[test] + fn set_refuses_to_write_what_could_not_be_read_back() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join(FILE_NAME); + let mut text = String::from("[flags]\n"); + while (text.len() as u64) < MAX_FILE_BYTES - 4 { + text.push_str("# padding\n"); + } + std::fs::write(&path, &text).unwrap(); + assert!(matches!(set(&path, "AI-MODELS", true), Err(FlagError::Unreadable(_)))); + assert_eq!(std::fs::read_to_string(&path).unwrap(), text, "left as it was"); + } + + #[test] + fn a_missing_file_is_mentioned_only_when_named() { + let dir = tempfile::tempdir().unwrap(); + let missing = dir.path().join("nope.toml"); + let named = store::load_from(Some(missing.clone()), true, None); + assert!(named.problems.iter().any(|p| p.contains("does not exist")), "{:?}", named.problems); + assert!(store::load_from(Some(missing), false, None).problems.is_empty(), "the usual file not existing yet is normal"); + } + + #[test] + fn two_writers_keep_both_changes() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join(FILE_NAME); + let ids: Vec = (0..8).map(|i| format!("F{i}")).collect(); + std::thread::scope(|s| { + for id in &ids { + let path = &path; + s.spawn(move || set(path, id, true).unwrap()); + } + }); + let flags = Flags::from_parts(Some(&std::fs::read_to_string(&path).unwrap()), None); + for id in &ids { + assert!(flags.enabled_id(id), "{id} lost"); + } + } + #[test] fn set_never_overwrites_a_file_it_cannot_read() { let dir = tempfile::tempdir().unwrap(); From 4a3bb60b828feaae94de67e00db3c25040f19ec7 Mon Sep 17 00:00:00 2001 From: David Mann Date: Thu, 12 Nov 2026 17:07:23 +0100 Subject: [PATCH 0009/1159] test(flags): the oversized-write fixture is readable, so the test reaches the output-size check The padding loop made the fixture 65,538 bytes, past the read limit, so set() refused while reading and the test passed without the output-size check. The fixture is now exactly MAX_FILE_BYTES - 4 bytes; with the check removed the test fails. --- crates/flags/src/tests.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/flags/src/tests.rs b/crates/flags/src/tests.rs index 92f690037..66c00855b 100644 --- a/crates/flags/src/tests.rs +++ b/crates/flags/src/tests.rs @@ -122,12 +122,15 @@ mod file { fn set_refuses_to_write_what_could_not_be_read_back() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join(FILE_NAME); - let mut text = String::from("[flags]\n"); - while (text.len() as u64) < MAX_FILE_BYTES - 4 { - text.push_str("# padding\n"); - } + // Readable (just under the limit), so the refusal comes from the size of what `set` would + // write, not from reading. + let mut text = String::from("[flags]\n# "); + let pad = MAX_FILE_BYTES as usize - 4 - text.len(); + text.push_str(&"x".repeat(pad)); + assert_eq!(text.len() as u64, MAX_FILE_BYTES - 4); std::fs::write(&path, &text).unwrap(); - assert!(matches!(set(&path, "AI-MODELS", true), Err(FlagError::Unreadable(_)))); + assert!(Flags::from_parts(Some(&text), None).problems.is_empty(), "the fixture itself reads"); + assert!(matches!(set(&path, "AI-MODELS", true), Err(FlagError::Unreadable(e)) if e.contains("would grow"))); assert_eq!(std::fs::read_to_string(&path).unwrap(), text, "left as it was"); } From 62e09222f1499e0c0e71ef5a1c3208fe127ab5b0 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 12 Nov 2026 21:16:46 +0100 Subject: [PATCH 0010/1159] craft-video: AV1 decoding about 1.7x as fast with many threads Port of storytold/Filmcraft#4542 (the code moved here). Frames start as soon as the reference rows they read are done (row wavefront across frames), parsing is split from reconstruction, and there is one frame worker per thread. Output is bit-identical; single-thread speed is unchanged. Co-Authored-By: Claude Sonnet 5.5 --- crates/video/src/av1/decode/README.md | 54 +- crates/video/src/av1/decode/cdef.rs | 30 +- crates/video/src/av1/decode/decoder.rs | 1154 +++++++++++++++-- crates/video/src/av1/decode/frame.rs | 166 +++ crates/video/src/av1/decode/inter.rs | 29 +- crates/video/src/av1/decode/postfilter.rs | 160 ++- crates/video/src/av1/decode/restoration.rs | 14 +- crates/video/src/av1/decode/state.rs | 29 +- crates/video/src/av1/decode/tile.rs | 314 ++++- crates/video/src/av1/decode/tile/coeffs.rs | 52 + crates/video/src/av1/decode/tile/predict.rs | 23 +- crates/video/tests/av1_decode/fuzz_hostile.rs | 70 +- crates/video/tests/av1_decode/main.rs | 1 + crates/video/tests/av1_decode/wavefront.rs | 93 ++ 14 files changed, 1992 insertions(+), 197 deletions(-) create mode 100644 crates/video/tests/av1_decode/wavefront.rs diff --git a/crates/video/src/av1/decode/README.md b/crates/video/src/av1/decode/README.md index 1518df92c..e1b8e4732 100644 --- a/crates/video/src/av1/decode/README.md +++ b/crates/video/src/av1/decode/README.md @@ -73,11 +73,17 @@ with libdav1d: **bit-exact** on all 6 fixtures. `Decoder::new()` uses all cores (`Decoder::with_threads(1)` decodes on the calling thread; on wasm32, or without the default `threads` feature, everything runs on the calling thread). Headers are parsed in order; each frame is -then decoded by a frame worker once the reference frames it reads are finished, so independent -frames (hierarchical GOP layers) decode concurrently; tiles of a frame decode in parallel into -private buffers that are merged afterwards; the in-loop filters (deblocking, CDEF, loop -restoration) each run in parallel bands of 64 rows (`postfilter.rs` explains why the bands are -independent). Results never depend on scheduling (the oracle tests compare 1 thread, many threads +then decoded by a frame worker. With worker threads it first reads the symbols of all its tiles, +which needs only the parse results of its references (CDFs, segment ids, saved motion vectors), +publishes its own, and then reconstructs from the recorded blocks. Frames with several tiles +read the symbols of their tiles in parallel (one after another with intra block copy). A frame reconstructs superblock row by superblock +row (that row of every tile), each row waiting only for the reference rows its motion vectors +reach (row wavefront), and the in-loop filters (deblocking, CDEF, loop restoration) follow a few +64-row bands behind and publish each final band for the frames predicting from it +(`decoder.rs` and `postfilter.rs` give the dependency rules). Super-resolution frames wait for +their whole references and filter the whole frame. There is one frame worker per thread (at most +16), and the frames in flight are bounded by their size. Independent frames (hierarchical GOP +layers) decode concurrently. Results never depend on scheduling (the oracle tests compare 1 thread, many threads and libdav1d). With frame threads, pictures may be returned by a later `decode_pts` call or by `flush`; each carries the `pts` of the temporal unit that showed it. @@ -130,6 +136,44 @@ motion vectors nor CDFs reach another frame — skip deblocking, CDEF and loop r (super-resolution still runs, it sets the output size) and are flagged `Picture::draft`. In SVT-AV1's hierarchical GOPs that is the top layer, half of the frames. +Frame threading on a Ryzen 7800X3D (8 cores / 16 threads, release `examples/av1_av1dec`, median of 7 +alternating runs against Filmcraft `6909e461`, 2026-11-08), wall time for the whole file: + +| Fixture | Threads | Before | After | Speed-up | +|---|---|---|---|---| +| `perf_1080p_gop` (60 frames) | 1 | 69.2 fps | 69.1 fps | 1.00x | +| `perf_1080p_gop` | 16 | 159.6 fps | 227.3 fps | 1.42x | +| `perf_2160p_gop` (30 frames) | 1 | 17.9 fps | 17.8 fps | 1.00x | +| `perf_2160p_gop` | 16 | 40.2 fps | 57.7 fps | 1.43x | + +Peak RSS at 16 threads goes from 278 to 365 MB (1080p) and from 1031 to 1217 MB (2160p, +18%): +every frame in flight holds its parse record and its final samples are kept in 64-row bands. + +With one frame worker per thread, the row wavefront for frames with several tiles and parse +waits limited to what parsing reads (same machine, median of 5 alternating runs, 2026-11-08): + +| Fixture | Threads | Before (`6909e461`) | After | Speed-up | Peak RSS before / after | +|---|---|---|---|---|---| +| `perf_1080p_gop` | 4 | 95.7 fps | 185.2 fps | 1.94x | 148 / 236 MB | +| `perf_1080p_gop` | 16 | 155.0 fps | 259.7 fps | 1.68x | 274 / 560 MB | +| `perf_2160p_gop` | 4 | 23.6 fps | 47.1 fps | 2.00x | 512 / 798 MB | +| `perf_2160p_gop` | 16 | 39.9 fps | 65.8 fps | 1.65x | 994 / 1353 MB | +| `perf_1080p_gop_tiles` (4x2 tiles) | 16 | 186.3 fps | 237.2 fps | 1.27x | 328 / 617 MB | +| SVT-AV1 2160p, 4x2 tiles (30 frames) | 16 | 57.1 fps | 70.8 fps | 1.24x | 1197 / 1502 MB | + +Known limitations of the frame threading (the output is the same in every case, only the +overlap is smaller): + +- A frame whose parse record would pass 32 bytes per luma sample of a tile (pathological streams: + tiny blocks, large palettes, every coefficient coded), or that cannot reserve its record from + the decoder-wide budget of 1 GiB shared by the frames in flight, is decoded again in one pass. +- A superblock row using local warped motion, global motion beyond translation or a scaled + reference waits for the whole reference instead of the rows its motion vectors reach. +- Super-resolution frames do not use the row wavefront: they wait for their whole references + and filter the whole frame. +- The tile columns of a superblock row are reconstructed one after another on the frame's worker + (their symbols are still read in parallel). + ## Limits Frames beyond the AV1 level 6.x limits (Annex A: at most 16384 wide, 8704 high and 35 651 584 luma samples diff --git a/crates/video/src/av1/decode/cdef.rs b/crates/video/src/av1/decode/cdef.rs index edd119292..05eb0a2a7 100644 --- a/crates/video/src/av1/decode/cdef.rs +++ b/crates/video/src/av1/decode/cdef.rs @@ -10,19 +10,33 @@ use crate::av1::decode::state::FrameState; /// equals CurrFrame). Bands of 64 luma rows (whole 64x64 filter blocks) are filtered in /// parallel: every block reads only CurrFrame and writes only its own samples. pub(crate) fn apply(fs: &FrameState, pool: &Pool) -> Option { + if !active(fs) { + return None; + } + let mut out = fs.cur.clone(); + let ranges = bands(pool, fs.fh.mi_rows as usize * 4); + filter_bands(fs, pool, out.row_bands(&ranges), &ranges); + Some(out) +} + +/// Whether CDEF filters any block of the frame (it reads only parse results). +pub(crate) fn active(fs: &FrameState) -> bool { let fh = &fs.fh; if fh.coded_lossless || fh.allow_intrabc || !fs.seq.enable_cdef { - return None; + return false; } let (mi_rows, mi_cols) = (fh.mi_rows as usize, fh.mi_cols as usize); let fb_rows = mi_rows.div_ceil(16); let fb_cols = mi_cols.div_ceil(16); - if !(0..fb_rows).any(|fr| (0..fb_cols).any(|fc| fb_skip_mask(fs, fr * 16, fc * 16).is_some())) { - return None; - } - let mut out = fs.cur.clone(); - let ranges = bands(pool, mi_rows * 4); - let mut planes = out.row_bands(&ranges); + (0..fb_rows).any(|fr| (0..fb_cols).any(|fc| fb_skip_mask(fs, fr * 16, fc * 16).is_some())) +} + +/// Filter the bands `planes` covering luma rows `ranges` (whole 64x64 filter block rows), which +/// hold CurrFrame; reads CurrFrame up to 2 rows beyond each band. +pub(crate) fn filter_bands(fs: &FrameState, pool: &Pool, mut planes: Vec<[RowBand; 3]>, ranges: &[(usize, usize)]) { + let (mi_rows, mi_cols) = (fs.fh.mi_rows as usize, fs.fh.mi_cols as usize); + let fb_rows = mi_rows.div_ceil(16); + let fb_cols = mi_cols.div_ceil(16); pool.map_mut(&mut planes, |b, band| { let (start, end) = ranges[b]; let mut buf = [0i16; BUF_LEN]; @@ -34,8 +48,6 @@ pub(crate) fn apply(fs: &FrameState, pool: &Pool) -> Option { } } }); - drop(planes); - Some(out) } /// The filtered 8x8 blocks of the 64x64 filter block at (r, c): bit `(i << 3) | j` is set for diff --git a/crates/video/src/av1/decode/decoder.rs b/crates/video/src/av1/decode/decoder.rs index 3fb1f1326..dc9caf74b 100644 --- a/crates/video/src/av1/decode/decoder.rs +++ b/crates/video/src/av1/decode/decoder.rs @@ -2,31 +2,37 @@ //! (7.20), show-existing-frame and output (7.18). //! //! Threading: headers are parsed in order on the calling thread; each frame then becomes a job -//! that waits for the reference frames it uses and decodes on a frame worker thread, so frames -//! that do not depend on each other decode concurrently. Within a frame, tiles and the -//! post-filter row bands run on a shared worker pool. Every result depends only on finished -//! reference frames, so the output is identical whatever the scheduling. Shown pictures are -//! returned in order; with frame threads they can come out of a later `decode` call (or -//! `flush`), each carrying the `pts` of the temporal unit that showed it. +//! on a frame worker thread. A job first reads the symbols of its tiles (parse), which needs +//! only the parse results of its references (CDFs, segment ids, saved motion vectors), and +//! publishes its own; it then reconstructs. A frame is reconstructed superblock row by +//! superblock row (that row of every tile), each row waiting only for the reference rows its +//! motion vectors reach, with the in-loop filters following a few bands behind and every final +//! band published for the frames that predict from it (row wavefront); super-resolution frames +//! wait for their whole references. So a frame can parse while its references are still being reconstructed, and reconstruct +//! while their bottom is still being filtered. Within a frame, tiles and the post-filter row +//! bands run on a shared worker pool; waits happen only on frame worker threads. Every result +//! depends only on final reference data, so the output is identical whatever the scheduling. +//! Shown pictures are returned in order; with frame threads they can come out of a later +//! `decode` call (or `flush`), each carrying the `pts` of the temporal unit that showed it. use std::collections::VecDeque; use std::sync::{Arc, Condvar, Mutex}; use crate::av1::decode::bits::{BitReader, leb128}; use crate::av1::decode::cdf::Cdfs; -use crate::av1::decode::frame::FrameBuf; +use crate::av1::decode::frame::{BandFrame, FrameBuf, PlaneRef}; use crate::av1::decode::header::{ ColorConfig, FilmGrainParams, FrameHeader, HeaderState, LoopFilterDeltas, RefInfo, SegmentationFeatures, SequenceHeader, default_gm_params, }; use crate::av1::decode::par::Pool; use crate::av1::decode::spec_tables::{ - ALTREF_FRAME, ALTREF2_FRAME, BWDREF_FRAME, DEFAULT_INTRA_FRAME_Y_MODE_CDF, DIV_MULT, GOLDEN_FRAME, INTRA_FRAME, INTRA_ONLY_FRAME, KEY_FRAME, - LAST_FRAME, LAST2_FRAME, MAX_FRAME_DISTANCE, MAX_OFFSET_HEIGHT, MAX_OFFSET_WIDTH, MFMV_STACK_SIZE, NUM_REF_FRAMES, PRIMARY_REF_NONE, - REFMVS_LIMIT, REFS_PER_FRAME, + ALTREF_FRAME, ALTREF2_FRAME, BWDREF_FRAME, DEFAULT_INTRA_FRAME_Y_MODE_CDF, DIV_MULT, GLOBAL_GLOBALMV, GLOBALMV, GOLDEN_FRAME, INTRA_FRAME, + INTRA_ONLY_FRAME, KEY_FRAME, LAST_FRAME, LAST2_FRAME, LOCALWARP, MAX_FRAME_DISTANCE, MAX_OFFSET_HEIGHT, MAX_OFFSET_WIDTH, MFMV_STACK_SIZE, + NUM_REF_FRAMES, PRIMARY_REF_NONE, REFMVS_LIMIT, REFS_PER_FRAME, TRANSLATION, }; use crate::av1::decode::state::{FrameShared, FrameState, TileState}; use crate::av1::decode::stats::{DecodeStats, Stage, Timer}; -use crate::av1::decode::tile::TileDecoder; +use crate::av1::decode::tile::{Pass, TileDecoder, TileRecord}; use crate::av1::decode::{Error, Picture, Result}; /// Largest accepted frame: the AV1 level 6.x limits (Annex A: MaxHSize 16384, MaxVSize 8704, MaxPicSize @@ -49,22 +55,156 @@ const OBU_FRAME_T: u32 = 6; const OBU_REDUNDANT_FRAME_HEADER_T: u32 = 7; const OBU_TILE_LIST_T: u32 = 8; -/// A stored reference frame (FrameStore plus the saved per-frame state). +/// The samples of a stored reference frame (FrameStore), after every in-loop filter: a whole +/// buffer (decoding on one thread) or the bands published while decoding (with worker threads). pub(crate) struct RefFrame { pub buf: FrameBuf, - pub cdfs: Box, + pub bands: Option>, + pub num_planes: usize, pub bit_depth: u8, pub subsampling_x: u8, pub subsampling_y: u8, + pub color: (u8, u8, u8, bool), +} + +impl RefFrame { + pub fn plane(&self, p: usize) -> PlaneRef<'_> { + match &self.bands { + Some(b) => PlaneRef::Bands(&b.planes[p]), + None => PlaneRef::Whole(&self.buf.planes[p]), + } + } +} + +/// The saved per-frame state that later frames read while parsing (CDFs, segment ids and the +/// motion vectors for motion field estimation). It depends only on the frame's symbols, so it +/// is published as soon as the tiles are decoded, before the in-loop filters run. +pub(crate) struct Parsed { + pub cdfs: Box, pub segment_ids: Vec, pub mi_cols: usize, pub mi_rows: usize, - pub color: (u8, u8, u8, bool), - /// SavedRefFrames / SavedMvs (MfRefFrames / MfMvs of 7.19), mi units. + /// SavedRefFrames / SavedMvs (MfRefFrames / MfMvs of 7.19) at the odd row and column of + /// every 8x8 block, the only positions the projection reads. pub saved_ref_frames: Vec, pub saved_mvs: Vec<[i32; 2]>, } +/// How many luma rows of a frame are final (every in-loop filter applied), or that the frame +/// failed. Rows only ever grow; a failure wakes every waiter with an error. +pub(crate) struct Progress { + state: Mutex, + cv: Condvar, +} + +#[derive(Clone, Copy, Default)] +struct ProgressState { + rows: usize, + failed: bool, +} + +impl Progress { + fn new() -> Progress { + Progress { state: Mutex::new(ProgressState::default()), cv: Condvar::new() } + } + + /// Rows [0, rows) are final. A smaller value than already published is ignored. + pub fn publish(&self, rows: usize) { + #[cfg(test)] + tests::jitter(); + let mut g = self.state.lock().unwrap_or_else(|e| e.into_inner()); + if rows > g.rows { + g.rows = rows; + self.cv.notify_all(); + } + } + + /// The frame will publish no more rows. + pub fn fail(&self) { + let mut g = self.state.lock().unwrap_or_else(|e| e.into_inner()); + g.failed = true; + self.cv.notify_all(); + } + + /// Rows published so far. + #[cfg(test)] + fn rows(&self) -> usize { + self.state.lock().map(|g| g.rows).unwrap_or(0) + } + + /// Blocks until rows [0, rows) are final; an error when the frame failed first. + pub fn wait(&self, rows: usize) -> Result<()> { + #[cfg(test)] + tests::jitter(); + let mut g = self.state.lock().unwrap_or_else(|e| e.into_inner()); + loop { + if g.rows >= rows { + return Ok(()); + } + if g.failed { + return Err(Error::Invalid("reference frame failed to decode")); + } + g = self.cv.wait(g).unwrap_or_else(|e| e.into_inner()); + } + } +} + +/// Everything a decoding frame publishes for the frames that reference it and for the output: +/// its parse results, its final rows (bands, then the row progress) and the finished frame. The +/// parse results and the frame are set exactly once and the progress ends complete or failed, +/// also when decoding fails, so no waiter blocks forever. +pub(crate) struct FrameSlot { + parsed: Slot>, + pub progress: Progress, + /// The final samples, published band by band before the progress covers them (with + /// worker threads). + pub bands: Arc, + frame: Slot>, +} + +impl FrameSlot { + fn new(bands: BandFrame) -> Arc { + Arc::new(FrameSlot { parsed: Slot::new_plain(), progress: Progress::new(), bands: Arc::new(bands), frame: Slot::new_plain() }) + } + + /// The finished frame. + fn wait(&self) -> Result> { + self.frame.wait() + } + + fn is_set(&self) -> bool { + self.frame.is_set() + } + + /// The frame's parse results (available before its samples are final). + fn wait_parsed(&self) -> Result> { + self.parsed.wait() + } + + /// Publish the parse results (ignored when already set). + fn set_parsed(&self, v: Result>) { + self.parsed.set_once(v); + } + + /// Publish the finished frame: every row is final. On an error, the parse results (when + /// not yet published) and the progress fail too. + fn finish(&self, v: Result>, rows: usize) { + match &v { + Ok(_) => {} + Err(e) => { + self.parsed.set_once(Err(e.clone())); + } + } + let ok = v.is_ok(); + self.frame.set(v); + if ok { + self.progress.publish(rows); + } else { + self.progress.fail(); + } + } +} + /// A value produced by a decoding job, waited for by later jobs and by the output. pub(crate) struct Slot { value: Mutex>>, @@ -73,7 +213,11 @@ pub(crate) struct Slot { impl Slot { fn new() -> Arc> { - Arc::new(Slot { value: Mutex::new(None), cv: Condvar::new() }) + Arc::new(Slot::new_plain()) + } + + fn new_plain() -> Slot { + Slot { value: Mutex::new(None), cv: Condvar::new() } } fn set(&self, v: Result) { @@ -82,6 +226,15 @@ impl Slot { self.cv.notify_all(); } + /// [`Slot::set`] unless a value is already there. + fn set_once(&self, v: Result) { + let mut g = self.value.lock().unwrap_or_else(|e| e.into_inner()); + if g.is_none() { + *g = Some(v); + self.cv.notify_all(); + } + } + fn is_set(&self) -> bool { self.value.lock().map(|g| g.is_some()).unwrap_or(true) } @@ -108,8 +261,6 @@ impl Slot { } } -type FrameSlot = Slot>; - /// Everything needed to decode one frame, captured when its header is parsed. struct FrameJob { seq: SequenceHeader, @@ -130,6 +281,15 @@ struct FrameJob { draft: bool, #[cfg(test)] force_panic: bool, + /// Panic in the row wavefront at the first superblock row from this one after which rows + /// are published (tests). + #[cfg(test)] + panic_at_row: Option, + /// Overrides [`RECORD_BYTES_PER_SAMPLE`] (tests). + #[cfg(test)] + record_limit: Option, + /// The decoder's parse record budget. + record_budget: Arc, } /// A shown picture in output order. @@ -251,6 +411,8 @@ pub struct Decoder { draft: bool, stats: Arc>, pool: Pool, + /// Parse record bytes all frames in flight may hold together. + record_budget: Arc, #[cfg(all(feature = "threads", not(target_arch = "wasm32")))] workers: Option, /// Frames submitted to the workers and not known to be finished (oldest first). @@ -265,6 +427,14 @@ pub struct Decoder { /// Make every frame job panic (tests of the worker panic handling). #[cfg(test)] force_panic: bool, + /// (frame number, superblock row): that frame panics in the row wavefront after that row, + /// once it has published rows that later frames may already be using (tests). + #[cfg(test)] + panic_at_row: Option<(usize, usize)>, + #[cfg(test)] + record_limit: Option, + #[cfg(test)] + frame_number: usize, } fn default_threads() -> usize { @@ -278,10 +448,25 @@ fn default_threads() -> usize { } } -/// Frames decoded concurrently with `threads` threads (each in-flight 1080p frame holds a few -/// tens of MB of state). +/// Frame worker threads with `threads` threads. A frame worker spends most of its time waiting +/// for the parse results and rows of its references, so there is one per thread (at least two, +/// at most [`MAX_FRAME_THREADS`]); the memory is bounded by `in_flight_limit` instead. fn frame_threads(threads: usize) -> usize { - if threads <= 1 { 1 } else { threads.div_ceil(2).clamp(2, 8) } + if threads <= 1 { 1 } else { threads.clamp(2, MAX_FRAME_THREADS) } +} + +const MAX_FRAME_THREADS: usize = 16; + +/// Luma samples of the frames in flight (each in-flight frame holds its state, parse record and +/// samples, roughly 10 bytes per luma sample): ten 2160p frames. +#[cfg(all(feature = "threads", not(target_arch = "wasm32")))] +const IN_FLIGHT_SAMPLES: usize = 10 * 3840 * 2160; + +/// Frames submitted to the frame workers that may be unfinished: two more than the workers, +/// fewer for large frames (at least four). +#[cfg(all(feature = "threads", not(target_arch = "wasm32")))] +fn in_flight_limit(frame_threads: usize, luma_samples: usize) -> usize { + (frame_threads + 2).min((IN_FLIGHT_SAMPLES / luma_samples.max(1)).max(4)) } impl Default for Decoder { @@ -379,6 +564,7 @@ impl Decoder { draft: false, stats, pool, + record_budget: Arc::new(crate::av1::decode::tile::RecordBudget::new(RECORD_BUDGET_BYTES)), #[cfg(all(feature = "threads", not(target_arch = "wasm32")))] workers, in_flight: VecDeque::new(), @@ -388,6 +574,12 @@ impl Decoder { pts: 0, #[cfg(test)] force_panic: false, + #[cfg(test)] + panic_at_row: None, + #[cfg(test)] + record_limit: None, + #[cfg(test)] + frame_number: 0, } } @@ -655,6 +847,13 @@ impl Decoder { let picture = fh.show_frame.then(Slot::new); let draft = self.draft && fh.show_frame && fh.refresh_frame_flags == 0; let tile_count = fh.tile_info.cols.checked_mul(fh.tile_info.rows).ok_or(Error::Invalid("tile count overflows"))?; + let slot = FrameSlot::new(BandFrame::new( + fh.upscaled_width as usize, + fh.frame_height as usize, + seq.color.num_planes, + seq.color.subsampling_x as usize, + seq.color.subsampling_y as usize, + )); self.pending = Some(Box::new(FrameJob { seq, fh, @@ -662,14 +861,23 @@ impl Decoder { ref_info: self.refs.clone(), tiles: Vec::new(), tile_covered: vec![false; tile_count], - slot: Slot::new(), + slot, picture, pts: self.pts, apply_film_grain: self.apply_film_grain, draft, #[cfg(test)] force_panic: self.force_panic, + #[cfg(test)] + panic_at_row: self.panic_at_row.filter(|&(f, _)| f == self.frame_number).map(|(_, r)| r), + #[cfg(test)] + record_limit: self.record_limit, + record_budget: self.record_budget.clone(), })); + #[cfg(test)] + { + self.frame_number += 1; + } Ok(None) } @@ -767,6 +975,8 @@ impl Decoder { .clone() .map(|p| Output::Decoded(p, picture_bytes(job.fh.upscaled_width as usize, job.fh.frame_height as usize, &job.seq.color))); #[cfg(all(feature = "threads", not(target_arch = "wasm32")))] + let fh_samples = (fh.upscaled_width as usize).max(fh.frame_width as usize) * fh.frame_height as usize; + #[cfg(all(feature = "threads", not(target_arch = "wasm32")))] if let Some(w) = &self.workers && let Some(tx) = &w.tx { @@ -775,7 +985,8 @@ impl Decoder { Ok(()) => { self.in_flight.push_back(slot); // Bound the frames in flight (memory): wait for the oldest. - while self.in_flight.len() > self.max_in_flight { + let limit = in_flight_limit(w.handles.len(), fh_samples); + while self.in_flight.len() > limit { let Some(s) = self.in_flight.pop_front() else { break }; let _ = s.wait(); } @@ -796,18 +1007,19 @@ impl Decoder { fn run_job(job: FrameJob, pool: &Pool, stats: &Mutex) { let mut st = DecodeStats::default(); let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| decode_frame(&job, pool, &mut st))); + let rows = job.fh.frame_height as usize; match res.unwrap_or(Err(Error::Invalid("decoder panicked"))) { Ok((rf, pic)) => { if let Some(ps) = &job.picture { ps.set(pic.ok_or(Error::Invalid("shown frame without picture"))); } - job.slot.set(Ok(rf)); + job.slot.finish(Ok(rf), rows); } Err(e) => { if let Some(ps) = &job.picture { ps.set(Err(e.clone())); } - job.slot.set(Err(e)); + job.slot.finish(Err(e), rows); } } if let Ok(mut s) = stats.lock() { @@ -832,71 +1044,191 @@ fn decode_frame(job: &FrameJob, pool: &Pool, stats: &mut DecodeStats) -> Result< if fh.primary_ref_frame != PRIMARY_REF_NONE { used[fh.ref_frame_idx[fh.primary_ref_frame]] = true; } - let mut refs: [Option>; NUM_REF_FRAMES] = Default::default(); - for i in 0..NUM_REF_FRAMES { - if used[i] { - let slot = job.refs[i].as_ref().ok_or(Error::Invalid("missing reference frame"))?; - refs[i] = Some(slot.wait()?); + // Prediction reads the reference planes with this frame's chroma format. + if !fh.frame_is_intra { + for i in 0..NUM_REF_FRAMES { + let Some(slot) = job.refs[i].as_ref().filter(|_| used[i]) else { continue }; + let b = &slot.bands; + let c = &seq.color; + if b.num_planes != c.num_planes || b.subsampling_x != c.subsampling_x as usize || b.subsampling_y != c.subsampling_y as usize { + return Err(Error::Invalid("reference frame has another chroma format")); + } } } - let t = Timer::start(); - let mut fs = FrameState::new(seq, fh); - fs.refs = refs; - fs.ref_info = job.ref_info.clone(); + // Parse dependencies first, then the samples. Parsing reads the CDFs and segment ids of + // the primary reference frame, and the saved motion vectors of the inter references only + // with use_ref_frame_mvs; the parse results of the other references are not waited for + // (their failures still fail this frame through the waits for their samples below). + let mut parse_deps = [false; NUM_REF_FRAMES]; if fh.primary_ref_frame != PRIMARY_REF_NONE { - // load_previous_segment_ids( ) - let (mi_cols, mi_rows) = (fs.mi.cols, fs.mi.rows); - let sh = &mut fs.sh; - if let Some(rf) = &sh.refs[fh.ref_frame_idx[fh.primary_ref_frame]] - && fh.seg.enabled - && rf.mi_cols == mi_cols - && rf.mi_rows == mi_rows - { - sh.prev_segment_ids.copy_from_slice(&rf.segment_ids); - } + parse_deps[fh.ref_frame_idx[fh.primary_ref_frame]] = true; } if fh.use_ref_frame_mvs { - motion_field_estimation(&mut fs); + for i in 0..REFS_PER_FRAME { + parse_deps[fh.ref_frame_idx[i]] = true; + } } - let mut cdfs = if fh.primary_ref_frame == PRIMARY_REF_NONE { - Cdfs::new(fh.quant.base_q_idx) - } else { - let rf = fs.refs[fh.ref_frame_idx[fh.primary_ref_frame]].as_ref().ok_or(Error::Invalid("primary reference frame missing"))?; - let mut c = rf.cdfs.clone(); - c.clear_counts(); - c + let mut parsed: [Option>; NUM_REF_FRAMES] = Default::default(); + for i in 0..NUM_REF_FRAMES { + if parse_deps[i] { + let slot = job.refs[i].as_ref().ok_or(Error::Invalid("missing reference frame"))?; + parsed[i] = Some(slot.wait_parsed()?); + } + } + // The samples of the references predicted from (inter frames only). + let wait_samples = || -> Result<[Option>; NUM_REF_FRAMES]> { + let mut refs: [Option>; NUM_REF_FRAMES] = Default::default(); + if !fh.frame_is_intra { + for i in 0..NUM_REF_FRAMES { + if used[i] { + let slot = job.refs[i].as_ref().ok_or(Error::Invalid("missing reference frame"))?; + slot.progress.wait(job.ref_info[i].frame_height as usize)?; + refs[i] = Some(slot.wait()?); + } + } + } + Ok(refs) }; + // With worker threads, the symbols of every tile are read first, so the parse results + // (which the next frames need before they can read their own symbols) are published + // before reconstruction, and parsing does not wait for the reference samples; on one + // thread each block is reconstructed as it is read. + let split = pool.is_parallel(); + // The frame state before tile decoding (with the reference samples when `with_refs`). + let setup = |with_refs: bool| -> Result<(FrameState, Box)> { + let mut fs = FrameState::new(seq, fh); + if with_refs { + fs.refs = wait_samples()?; + } + fs.parsed = parsed.clone(); + fs.ref_info = job.ref_info.clone(); + if fh.primary_ref_frame != PRIMARY_REF_NONE { + // load_previous_segment_ids( ) + let (mi_cols, mi_rows) = (fs.mi.cols, fs.mi.rows); + let sh = &mut fs.sh; + if let Some(rf) = &sh.parsed[fh.ref_frame_idx[fh.primary_ref_frame]] + && fh.seg.enabled + && rf.mi_cols == mi_cols + && rf.mi_rows == mi_rows + { + sh.prev_segment_ids.copy_from_slice(&rf.segment_ids); + } + } + if fh.use_ref_frame_mvs { + motion_field_estimation(&mut fs); + } + let cdfs = if fh.primary_ref_frame == PRIMARY_REF_NONE { + Cdfs::new(fh.quant.base_q_idx) + } else { + let rf = fs.parsed[fh.ref_frame_idx[fh.primary_ref_frame]].as_ref().ok_or(Error::Invalid("primary reference frame missing"))?; + let mut c = rf.cdfs.clone(); + c.clear_counts(); + c + }; + Ok((fs, cdfs)) + }; + let t = Timer::start(); + let (mut fs, mut cdfs) = setup(!split)?; stats.add(Stage::Setup, t.secs()); let t = Timer::start(); - let saved = decode_tiles(pool, &mut fs, &cdfs, &job.tiles)?; + // Time spent waiting for reference samples is not decoding time. + let mut waited = 0.0; + #[cfg(not(test))] + let record_limit = RECORD_BYTES_PER_SAMPLE; + #[cfg(test)] + let record_limit = job.record_limit.unwrap_or(RECORD_BYTES_PER_SAMPLE); + let mut tiles = parse_tiles(pool, &mut fs, &cdfs, &job.tiles, if split { Pass::Parse } else { Pass::Full }, record_limit, &job.record_budget)?; + // A frame whose record would take too much memory (pathological streams) is decoded again + // in one pass, as on one thread. + let one_pass = split && tiles.records.iter().any(|(_, r)| r.over_limit()); + if one_pass { + // The fallback starts again from the tile data: release the records first. + drop(tiles); + let wait = Timer::start(); + (fs, cdfs) = setup(true)?; + waited = wait.secs(); + tiles = parse_tiles(pool, &mut fs, &cdfs, &job.tiles, Pass::Full, record_limit, &job.record_budget)?; + } stats.tiles += job.tiles.len() as u64; - stats.add(Stage::Tiles, t.secs()); if !fh.disable_frame_end_update_cdf - && let Some(s) = saved + && let Some(s) = tiles.saved.take() { cdfs = s; } - crate::av1::decode::postfilter::apply(&mut fs, stats, pool, job.draft); + if split && !one_pass { + if tiles.whole { + if let Some(state) = tiles.states.pop() { + fs.put_tile_state(state); + job.slot.set_parsed(Ok(Arc::new(parse_results(&fs, cdfs)))); + tiles.states.push(fs.take_tile_state()); + } + } else { + for state in &tiles.states { + fs.merge_tile_modes(state); + } + job.slot.set_parsed(Ok(Arc::new(parse_results(&fs, cdfs)))); + } + if !fh.use_superres { + if !tiles.whole { + // The tiles were parsed into private buffers, merged into the frame above: + // reconstruct them in the frame's own buffers. + tiles.states.clear(); + tiles.states.push(fs.take_tile_state()); + tiles.whole = true; + } + // Reconstruct superblock row by superblock row (every tile of the row), each + // waiting only for the reference rows it reads, with the in-loop filters following + // behind. + waited = reconstruct_wavefront(job, pool, &mut fs, &mut tiles, stats)?; + } else { + let wait = Timer::start(); + fs.refs = wait_samples()?; + waited = wait.secs(); + reconstruct_tiles(pool, &fs.sh, &mut tiles)?; + tiles.finish(&mut fs); + let f = Timer::start(); + if fh.use_superres { + crate::av1::decode::postfilter::apply(&mut fs, stats, pool, job.draft); + publish_bands(&job.slot, &fs.cur, 0, usize::MAX, fh.frame_height as usize); + } else { + let mut pipe = crate::av1::decode::postfilter::Pipeline::new(&fs, job.draft); + let done = pipe.advance(&mut fs, stats, pool, usize::MAX); + publish_final(&job.slot, done, fh.frame_height as usize); + } + waited += f.secs(); + } + } else { + tiles.finish(&mut fs); + job.slot.set_parsed(Ok(Arc::new(parse_results(&fs, cdfs)))); + } + stats.add(Stage::Tiles, t.secs() - waited); + if !split || one_pass { + crate::av1::decode::postfilter::apply(&mut fs, stats, pool, job.draft); + } + if one_pass { + publish_bands(&job.slot, &fs.cur, 0, usize::MAX, fh.frame_height as usize); + } + if split { + // A frame using a reference only for its parse results (an intra frame's CDFs) still + // fails with it, as it does on one thread. + for i in 0..NUM_REF_FRAMES { + if used[i] { + job.refs[i].as_ref().ok_or(Error::Invalid("missing reference frame"))?.wait()?; + } + } + } stats.draft_frames += job.draft as u64; let t = Timer::start(); stats.frames += 1; - if fh.seg.enabled && !fh.seg.update_map { - let prev = std::mem::take(&mut fs.prev_segment_ids); - fs.mi.segment_id.copy_from_slice(&prev); - } - let (mf_refs, mf_mvs) = motion_vector_storage(&fs); let rf = Arc::new(RefFrame { - buf: std::mem::take(&mut fs.cur), - cdfs, + // With worker threads the samples are in the published bands. + buf: if split { FrameBuf::default() } else { std::mem::take(&mut fs.cur) }, + bands: split.then(|| job.slot.bands.clone()), + num_planes: seq.color.num_planes, bit_depth: seq.color.bit_depth, subsampling_x: seq.color.subsampling_x, subsampling_y: seq.color.subsampling_y, - segment_ids: std::mem::take(&mut fs.mi.segment_id), - mi_cols: fs.mi.cols, - mi_rows: fs.mi.rows, color: (seq.color.color_primaries, seq.color.transfer_characteristics, seq.color.matrix_coefficients, seq.color.color_range), - saved_ref_frames: mf_refs, - saved_mvs: mf_mvs, }); // job.picture is None for a shown frame refused for its size (see tile_group) let pic = job.picture.is_some().then(|| { @@ -908,16 +1240,256 @@ fn decode_frame(job: &FrameJob, pool: &Pool, stats: &mut DecodeStats) -> Result< Ok((rf, pic)) } -/// Decode the tiles of a frame (tile number, data), in parallel when several; returns the final -/// CDFs of tile context_update_tile_id. -fn decode_tiles(pool: &Pool, fs: &mut FrameState, cdfs: &Cdfs, tiles: &[(usize, Vec)]) -> Result>> { +/// Publish bands [from, to) copied from the final samples `src`, and the row progress they make +/// final. +fn publish_bands(slot: &FrameSlot, src: &FrameBuf, from: usize, to: usize, height: usize) { + let to = to.min(slot.bands.bands()); + for b in from..to { + slot.bands.publish(b, src); + } + slot.progress.publish((to << crate::av1::decode::frame::BAND_LOG2).min(height)); +} + +/// Publish the bands that became final (in order, following those published before) and the +/// row progress they make final. +fn publish_final(slot: &FrameSlot, bands: crate::av1::decode::postfilter::FinalBands, height: usize) { + let mut end = None; + for (b, planes) in bands { + slot.bands.publish_owned(b, planes); + end = Some(b + 1); + } + if let Some(end) = end { + slot.progress.publish((end << crate::av1::decode::frame::BAND_LOG2).min(height)); + } +} + +/// Extra luma rows waited for below what a motion vector reaches: the 8-tap filters read 4 +/// rows below a block, chroma positions round, and the vertical sub-sample phase adds one. +const ROW_WAIT_MARGIN: usize = 16; + +/// The rows of each reference slot that reconstructing the blocks `blocks` (a superblock row +/// and the row above it, whose motion overlapped block motion compensation can reuse) reads, +/// for blocks ending at luma row `bottom`: their reach plus a margin, or everything +/// (`usize::MAX`) for warped motion and scaled references, whose reach is not bounded by the +/// motion vector. +fn rows_needed( + fh: &FrameHeader, + ref_info: &[RefInfo; NUM_REF_FRAMES], + blocks: impl Iterator, + bottom: usize, +) -> [usize; NUM_REF_FRAMES] { + let mut need = [0usize; NUM_REF_FRAMES]; + let mut used = [false; NUM_REF_FRAMES]; + let mut warp = false; + let global = (LAST_FRAME..=ALTREF_FRAME).any(|r| fh.gm_type[r] as usize > TRANSLATION); + for b in blocks { + if !b.inter { + continue; + } + if b.motion_mode as usize == LOCALWARP || (global && (b.y_mode == GLOBALMV || b.y_mode == GLOBAL_GLOBALMV)) { + warp = true; + } + let bottom = bottom.max((b.mi_row + b.bh4) * 4); + for list in 0..2 { + let rf = b.ref_frame[list]; + if rf <= INTRA_FRAME as i8 { + continue; + } + let Some(&i) = fh.ref_frame_idx.get(rf as usize - LAST_FRAME) else { continue }; + used[i] = true; + let ri = &ref_info[i]; + if ri.upscaled_width != fh.frame_width || ri.frame_height != fh.frame_height { + need[i] = usize::MAX; + continue; + } + let down = (b.mv[list][0].max(0) as usize).div_ceil(8); + need[i] = need[i].max(bottom.saturating_add(down).saturating_add(ROW_WAIT_MARGIN)); + } + } + if warp { + for i in 0..NUM_REF_FRAMES { + if used[i] { + need[i] = usize::MAX; + } + } + } + need +} + +/// Reconstructed luma rows between two runs of the in-loop filter pipeline in +/// [`reconstruct_wavefront`] (finer steps publish rows sooner, coarser ones filter more bands in +/// parallel). +const FILTER_STEP_ROWS: usize = 128; + +/// Reconstruct a frame superblock row by superblock row (row wavefront): before each row, wait +/// only for the reference rows it reads; then reconstruct that row in every tile it crosses +/// (tiles share no prediction context, so the order of the tiles within a row does not matter) +/// and run the in-loop filters on what that allows, publishing the bands that became final. +/// `tiles` holds the whole frame's buffers. Returns the seconds spent waiting for references +/// and filtering (not tile decoding). +fn reconstruct_wavefront(job: &FrameJob, pool: &Pool, fs: &mut FrameState, tiles: &mut FrameTiles, stats: &mut DecodeStats) -> Result { + let fh = &job.fh; + let mut other = 0.0; + let ti = fh.tile_info.clone(); + let Some(state) = tiles.states.pop() else { + return Err(Error::Invalid("no tile to reconstruct")); + }; + // The record of every tile, by tile number. + let mut recs: Vec> = (0..ti.rows * ti.cols).map(|_| None).collect(); + for (num, rec) in std::mem::take(&mut tiles.records) { + *recs.get_mut(num).ok_or(Error::Invalid("tile number out of range"))? = Some(rec); + } + let mut used = [false; NUM_REF_FRAMES]; + if !fh.frame_is_intra { + for i in 0..REFS_PER_FRAME { + used[fh.ref_frame_idx[i]] = true; + } + } + for i in 0..NUM_REF_FRAMES { + if used[i] { + let slot = job.refs[i].as_ref().ok_or(Error::Invalid("missing reference frame"))?; + fs.ref_rows[i] = Some(slot.bands.clone()); + } + } + fs.put_tile_state(state); + let mut pipe = crate::av1::decode::postfilter::Pipeline::new(fs, job.draft); + let sb4 = if job.seq.use_128x128_superblock { 32 } else { 16 }; + let mi_rows = fh.mi_rows as usize; + let sb_rows = mi_rows.div_ceil(sb4); + let mut bufs = (crate::av1::decode::tile::Buffers::new(), Cdfs::new(fh.quant.base_q_idx)); + let mut filtered = 0; + let mut tile_row = 0; + for row in 0..sb_rows { + // Tile rows start on superblock rows. + while tile_row + 1 < ti.rows && row * sb4 >= ti.mi_row_starts[tile_row + 1] as usize { + tile_row += 1; + } + let local = (row * sb4).saturating_sub(ti.mi_row_starts[tile_row] as usize) / sb4; + let w = Timer::start(); + if !fh.frame_is_intra { + let mut need = [0usize; NUM_REF_FRAMES]; + for col in 0..ti.cols { + let Some(rec) = &recs[tile_row * ti.cols + col] else { continue }; + let width = (ti.mi_col_starts[col + 1] as usize).min(fh.mi_cols as usize).saturating_sub(ti.mi_col_starts[col] as usize); + let n = rows_needed(fh, &job.ref_info, rec.row_blocks(width.div_ceil(sb4), local.saturating_sub(1), local + 1), (row + 1) * sb4 * 4); + for i in 0..NUM_REF_FRAMES { + need[i] = need[i].max(n[i]); + } + } + for i in 0..NUM_REF_FRAMES { + if used[i] && need[i] > 0 { + let slot = job.refs[i].as_ref().ok_or(Error::Invalid("missing reference frame"))?; + slot.progress.wait(need[i].min(job.ref_info[i].frame_height as usize))?; + } + } + } + other += w.secs(); + let mut t = fs.take_tile_state(); + let mut res = Ok(()); + for col in 0..ti.cols { + let Some(rec) = recs[tile_row * ti.cols + col].as_mut() else { continue }; + let (b, cdf) = bufs; + let mut td = TileDecoder::with_buffers(&fs.sh, &mut t, &[], cdf, tile_row, col, b); + td.pass = Pass::Reconstruct; + td.record = std::mem::take(rec); + res = td.reconstruct_sb_rows(1); + *rec = std::mem::take(&mut td.record); + bufs = td.into_buffers(); + if res.is_err() { + break; + } + } + fs.put_tile_state(t); + res?; + let f = Timer::start(); + let recon_rows = ((row + 1) * sb4 * 4).min(mi_rows * 4); + // Filter in steps of a few bands, so the bands of a step run in parallel. + if recon_rows >= filtered + FILTER_STEP_ROWS || recon_rows == mi_rows * 4 { + filtered = recon_rows; + let done = pipe.advance(fs, stats, pool, recon_rows); + publish_final(&job.slot, done, fh.frame_height as usize); + } + other += f.secs(); + #[cfg(test)] + assert!( + !(job.panic_at_row.is_some_and(|r| row >= r) && job.slot.progress.rows() > 0), + "forced panic in the row wavefront after publishing rows" + ); + } + // A reference that failed after publishing the rows used here still fails this frame, as + // it does when frames are decoded one after another. + for i in 0..NUM_REF_FRAMES { + if used[i] { + let slot = job.refs[i].as_ref().ok_or(Error::Invalid("missing reference frame"))?; + slot.wait()?; + } + } + Ok(other) +} + +/// Most bytes a tile's parse record may take per luma sample of the tile. Typical streams need +/// a fraction of this; past it (pathological streams: tiny blocks, large palettes, every +/// coefficient coded) the frame is decoded in one pass instead, so memory stays bounded. +const RECORD_BYTES_PER_SAMPLE: usize = 32; + +/// Most parse record bytes all frames of a decoder hold at once. Past it, the frames that +/// cannot reserve more are decoded in one pass, so the frames in flight together stay bounded +/// too (typical streams need a few bytes per luma sample: under 100 MB for a 2160p frame). +const RECORD_BUDGET_BYTES: usize = 1 << 30; + +/// The tiles of a frame between parsing and reconstruction. +struct FrameTiles { + /// One state per tile (decoded in parallel into private buffers), or one for the whole + /// frame (tiles one after another). + states: Vec, + /// Per tile: (tile number, record of [`Pass::Parse`]). + records: Vec<(usize, TileRecord)>, + /// The final CDFs of tile context_update_tile_id. + saved: Option>, + /// `states` holds the frame's own buffers (taken with [`FrameState::take_tile_state`]). + whole: bool, +} + +impl FrameTiles { + /// Move the tile buffers (mode info, filter parameters and, after reconstruction, the + /// samples) into the frame. + fn finish(self, fs: &mut FrameState) { + if self.whole { + if let Some(t) = self.states.into_iter().next() { + fs.put_tile_state(t); + } + } else { + for t in &self.states { + fs.merge_tile_state(t); + } + } + } +} + +/// Run `pass` (Full or Parse) over the tiles of a frame. +/// `record_limit`: most record bytes per luma sample of a tile (see [`RECORD_BYTES_PER_SAMPLE`]). +/// `budget`: the decoder-wide budget the records are reserved from. +fn parse_tiles( + pool: &Pool, + fs: &mut FrameState, + cdfs: &Cdfs, + tiles: &[(usize, Vec)], + pass: Pass, + record_limit: usize, + budget: &Arc, +) -> Result { let ti = fs.fh.tile_info.clone(); let ctx_tile = ti.context_update_tile_id as usize; - let run = |sh: &FrameShared, t: &mut TileState, num: usize, data: &[u8]| -> Result>> { + let run = |sh: &FrameShared, t: &mut TileState, num: usize, data: &[u8]| -> Result<(Option>, TileRecord)> { t.intra_frame_y_mode_cdf = DEFAULT_INTRA_FRAME_Y_MODE_CDF; let mut td = TileDecoder::new(sh, t, data, Box::new(cdfs.clone()), num / ti.cols, num % ti.cols); + td.pass = pass; + let (rows, cols) = (td.mi_row_end.saturating_sub(td.mi_row_start), td.mi_col_end.saturating_sub(td.mi_col_start)); + td.record.limit = rows.saturating_mul(cols).saturating_mul(16 * record_limit); + td.record.budget = Some(budget.clone()); td.decode_tile()?; - Ok((num == ctx_tile).then_some(td.cdf)) + let rec = std::mem::take(&mut td.record); + Ok(((num == ctx_tile).then_some(td.cdf), rec)) }; // Intra block copy reads the current frame through the tile's own buffer: keep whole-frame // buffers (tiles one after another) for it. @@ -933,42 +1505,75 @@ fn decode_tiles(pool: &Pool, fs: &mut FrameState, cdfs: &Cdfs, tiles: &[(usize, .collect(); let sh = &fs.sh; let results = pool.map_mut(&mut states, |i, t| run(sh, t, tiles[i].0, &tiles[i].1)); - for t in &states { - fs.merge_tile_state(t); - } let mut saved = None; - for r in results { - if let Some(c) = r? { - saved = Some(c); + let mut records = Vec::with_capacity(tiles.len()); + for (r, (num, _)) in results.into_iter().zip(tiles) { + let (c, rec) = r?; + if c.is_some() { + saved = c; } + records.push((*num, rec)); } - return Ok(saved); + return Ok(FrameTiles { states, records, saved, whole: false }); } let mut t = fs.take_tile_state(); let mut saved = None; + let mut records = Vec::with_capacity(tiles.len()); let mut res = Ok(()); for (num, data) in tiles { match run(&fs.sh, &mut t, *num, data) { - Ok(Some(c)) => saved = Some(c), - Ok(None) => {} + Ok((c, rec)) => { + if c.is_some() { + saved = c; + } + records.push((*num, rec)); + } Err(e) => { res = Err(e); break; } } } - fs.put_tile_state(t); - res.map(|_| saved) + if let Err(e) = res { + fs.put_tile_state(t); + return Err(e); + } + Ok(FrameTiles { states: vec![t], records, saved, whole: true }) +} + +/// Reconstruct parsed tiles (in parallel when each has its own state). +fn reconstruct_tiles(pool: &Pool, fs: &FrameShared, tiles: &mut FrameTiles) -> Result<()> { + let ti = fs.fh.tile_info.clone(); + let run = |t: &mut TileState, num: usize, rec: TileRecord| -> Result<()> { + let mut td = TileDecoder::new(fs, t, &[], Cdfs::new(fs.fh.quant.base_q_idx), num / ti.cols, num % ti.cols); + td.pass = Pass::Reconstruct; + td.record = rec; + td.decode_tile() + }; + let records = std::mem::take(&mut tiles.records); + if !tiles.whole { + let mut items: Vec<(&mut TileState, Option<(usize, TileRecord)>)> = tiles.states.iter_mut().zip(records.into_iter().map(Some)).collect(); + let results = pool.map_mut(&mut items, |_, (t, rec)| match rec.take() { + Some((num, rec)) => run(t, num, rec), + None => Ok(()), + }); + return results.into_iter().collect(); + } + let Some(t) = tiles.states.first_mut() else { return Ok(()) }; + for (num, rec) in records { + run(t, num, rec)?; + } + Ok(()) } fn output_picture(seq: &SequenceHeader, rf: &RefFrame, w: usize, h: usize, grain: &FilmGrainParams, apply_film_grain: bool, pts: i64) -> Picture { let ssx = rf.subsampling_x as usize; let ssy = rf.subsampling_y as usize; - let num_planes = rf.buf.num_planes; + let num_planes = rf.num_planes; let mut planes: [Vec; 3] = Default::default(); for p in 0..num_planes { let (pw, ph) = if p == 0 { (w, h) } else { ((w + ssx) >> ssx, (h + ssy) >> ssy) }; - let src = &rf.buf.planes[p]; + let src = rf.plane(p); let mut v = Vec::with_capacity(pw * ph); for y in 0..ph { v.extend_from_slice(&src.row(y)[..pw]); @@ -996,25 +1601,40 @@ fn output_picture(seq: &SequenceHeader, rf: &RefFrame, w: usize, h: usize, grain pic } -/// Motion field motion vector storage process (7.19): (MfRefFrames, MfMvs). +/// What later frames read from this one while parsing: the final CDFs, the segment ids (the +/// previous frame's when the map is not updated) and the stored motion vectors (7.19). The +/// in-loop filters read the decoded segment ids, so the frame's own map is left alone. +fn parse_results(fs: &FrameState, cdfs: Box) -> Parsed { + let fh = &fs.fh; + let segment_ids = if fh.seg.enabled && !fh.seg.update_map { fs.prev_segment_ids.clone() } else { fs.mi.segment_id.clone() }; + let (saved_ref_frames, saved_mvs) = motion_vector_storage(fs); + Parsed { cdfs, segment_ids, mi_cols: fs.mi.cols, mi_rows: fs.mi.rows, saved_ref_frames, saved_mvs } +} + +/// Motion field motion vector storage process (7.19): (MfRefFrames, MfMvs), kept only where +/// the projection process (7.9.2) reads them, the odd row and column of every 8x8 block +/// (index `y8 * (MiCols >> 1) + x8`). fn motion_vector_storage(fs: &FrameState) -> (Vec, Vec<[i32; 2]>) { let mi = &fs.mi; - let n = mi.rows * mi.cols; - let mut refs = vec![-1i8; n]; - let mut mvs = vec![[0i32; 2]; n]; let fh = &fs.fh; - for i in 0..n { - for list in 0..2 { - let r = mi.ref_frame[i][list]; - if r > INTRA_FRAME as i8 { - let ref_idx = fh.ref_frame_idx[r as usize - LAST_FRAME]; - let dist = crate::av1::decode::header::relative_dist(&fs.seq, fs.ref_info[ref_idx].order_hint, fh.order_hint); - if dist < 0 { - let m = mi.mv[i][list]; - let (row, col) = (m.row as i32, m.col as i32); - if row.abs() <= REFMVS_LIMIT as i32 && col.abs() <= REFMVS_LIMIT as i32 { - refs[i] = r; - mvs[i] = [row, col]; + let (mi_cols, w8, h8) = (fh.mi_cols as usize, fh.mi_cols as usize >> 1, fh.mi_rows as usize >> 1); + let mut refs = vec![-1i8; w8 * h8]; + let mut mvs = vec![[0i32; 2]; w8 * h8]; + for y8 in 0..h8 { + for x8 in 0..w8 { + let (i, o) = ((2 * y8 + 1) * mi_cols + 2 * x8 + 1, y8 * w8 + x8); + for list in 0..2 { + let r = mi.ref_frame[i][list]; + if r > INTRA_FRAME as i8 { + let ref_idx = fh.ref_frame_idx[r as usize - LAST_FRAME]; + let dist = crate::av1::decode::header::relative_dist(&fs.seq, fs.ref_info[ref_idx].order_hint, fh.order_hint); + if dist < 0 { + let m = mi.mv[i][list]; + let (row, col) = (m.row as i32, m.col as i32); + if row.abs() <= REFMVS_LIMIT as i32 && col.abs() <= REFMVS_LIMIT as i32 { + refs[o] = r; + mvs[o] = [row, col]; + } } } } @@ -1072,7 +1692,7 @@ fn project_pos(v8: i32, delta: i32, dst_sign: i32, max8: i32, max_off8: i32) -> /// Projection process (7.9.2). fn project(fs: &mut FrameState, src: usize, dst_sign: i32) -> bool { - let FrameShared { seq, fh, refs, ref_info, motion_field, .. } = &mut fs.sh; + let FrameShared { seq, fh, parsed, ref_info, motion_field, .. } = &mut fs.sh; let src_idx = fh.ref_frame_idx[src - LAST_FRAME]; let w8 = fh.mi_cols as i32 >> 1; let h8 = fh.mi_rows as i32 >> 1; @@ -1080,20 +1700,18 @@ fn project(fs: &mut FrameState, src: usize, dst_sign: i32) -> bool { if ri.mi_rows != fh.mi_rows || ri.mi_cols != fh.mi_cols || ri.frame_type == INTRA_ONLY_FRAME as u8 || ri.frame_type == KEY_FRAME as u8 { return false; } - let Some(rf) = &refs[src_idx] else { return false }; + let Some(rf) = &parsed[src_idx] else { return false }; let rd = |a: u32, b: u32| crate::av1::decode::header::relative_dist(seq, a, b); - let mi_cols = fh.mi_cols as usize; for y8 in 0..h8 { for x8 in 0..w8 { - let row = (2 * y8 + 1) as usize; - let col = (2 * x8 + 1) as usize; - let src_ref = rf.saved_ref_frames[row * mi_cols + col]; + let at = (y8 * w8 + x8) as usize; + let src_ref = rf.saved_ref_frames[at]; if src_ref > INTRA_FRAME as i8 { let ref_to_cur = rd(fh.order_hints[src], fh.order_hint); let ref_offset = rd(fh.order_hints[src], ri.saved_order_hints[src_ref as usize]); let pos_valid = ref_to_cur.abs() <= MAX_FRAME_DISTANCE as i32 && ref_offset.abs() <= MAX_FRAME_DISTANCE as i32 && ref_offset > 0; if pos_valid { - let mv = rf.saved_mvs[row * mi_cols + col]; + let mv = rf.saved_mvs[at]; let proj = get_mv_projection(mv, ref_to_cur * dst_sign, ref_offset); let py = project_pos(y8, proj[0], dst_sign, h8, MAX_OFFSET_HEIGHT as i32); let px = project_pos(x8, proj[1], dst_sign, w8, MAX_OFFSET_WIDTH as i32); @@ -1125,13 +1743,18 @@ mod tests { ref_info: Default::default(), tiles: Vec::new(), tile_covered: vec![false; 3], - slot: Slot::new(), + slot: FrameSlot::new(BandFrame::new(0, 0, 1, 0, 0)), picture: None, pts: 0, apply_film_grain: false, draft: false, #[cfg(test)] force_panic: false, + #[cfg(test)] + panic_at_row: None, + #[cfg(test)] + record_limit: None, + record_budget: decoder.record_budget.clone(), })); decoder } @@ -1278,6 +1901,317 @@ mod tests { assert!(frame_size_allowed(1, 1)); } + /// Random short sleeps in row progress publishing and waiting, to shake out scheduling + /// orders a quiet machine never produces (enabled by [`JitterGuard`]). + static JITTER: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + + pub(super) fn jitter() { + use std::cell::Cell; + use std::sync::atomic::Ordering; + if JITTER.load(Ordering::Relaxed) == 0 { + return; + } + static SEED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0x2545_f491_4f6c_dd1d); + thread_local!(static RNG: Cell = Cell::new(SEED.fetch_add(0x9e37_79b9_7f4a_7c15, Ordering::Relaxed) | 1)); + let r = RNG.with(|c| { + let mut x = c.get(); + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + c.set(x); + x + }); + match r % 4 { + 0 => std::thread::yield_now(), + 1 => std::thread::sleep(std::time::Duration::from_micros(r % 300)), + _ => {} + } + } + + struct JitterGuard; + impl JitterGuard { + fn on() -> JitterGuard { + JITTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + JitterGuard + } + } + impl Drop for JitterGuard { + fn drop(&mut self) { + JITTER.fetch_sub(1, std::sync::atomic::Ordering::SeqCst); + } + } + + /// Inter test streams made with ffmpeg / SVT-AV1 (None, reported, without them): several + /// superblock rows and hierarchical references; odd size; super-resolution; 2x2 tiles. + const STREAMS: [(&str, &str, &str); 4] = [ + ("wavefront_unit_352x288", "testsrc2=size=352x288:rate=25", "-preset 8 -crf 35"), + ("wavefront_unit_odd", "testsrc2=size=203x117:rate=25,noise=alls=15:allf=t", "-preset 8 -crf 40"), + ("wavefront_unit_superres", "testsrc2=size=416x240:rate=25", "-preset 8 -crf 35 -svtav1-params superres-mode=1:superres-denom=12"), + ("wavefront_unit_tiles", "mandelbrot=size=650x370:rate=25", "-preset 8 -crf 32 -svtav1-params tile-columns=1:tile-rows=1"), + ]; + + fn inter_stream((name, src, args): (&str, &str, &str)) -> Option>> { + let ff = craft_media_testkit::ffmpeg_or_skip("av1 wavefront")?; + if !craft_media_testkit::ffmpeg_has_encoder(&ff, "libsvtav1") { + craft_media_testkit::skip_test("av1 wavefront", "this ffmpeg has no libsvtav1 encoder"); + return None; + } + let out = craft_media_testkit::fixtures_dir("av1").join(format!("{name}.ivf")); + let path = craft_media_testkit::fixtures::generate(&out, |tmp| { + std::process::Command::new(&ff) + .args([ + "-hide_banner", + "-loglevel", + "error", + "-y", + "-f", + "lavfi", + "-i", + src, + "-frames:v", + "16", + "-pix_fmt", + "yuv420p", + "-c:v", + "libsvtav1", + ]) + .args(args.split_whitespace()) + .args(["-f", "ivf"]) + .arg(tmp) + .stdin(std::process::Stdio::null()) + .status() + .is_ok_and(|s| s.success()) + }); + let path = path.unwrap_or_else(|| panic!("ffmpeg has libsvtav1 but could not encode {name}")); + let d = std::fs::read(&path).unwrap_or_else(|e| panic!("read {}: {e}", path.display())); + let mut pos = usize::from(u16::from_le_bytes([d[6], d[7]])); + let mut out = Vec::new(); + while let Some(h) = d.get(pos..pos + 12) { + let n = u32::from_le_bytes([h[0], h[1], h[2], h[3]]) as usize; + pos += 12; + out.push(d[pos..pos + n].to_vec()); + pos += n; + } + assert_eq!(out.len(), 16, "{name}: temporal units"); + Some(out) + } + + /// Decode `tus` with `dec`: (pictures, errors). + fn decode_tus(mut dec: Decoder, tus: &[Vec]) -> (Vec, usize) { + let (mut pics, mut errors) = (Vec::new(), 0); + for (i, tu) in tus.iter().enumerate() { + match dec.decode_pts(tu, i as i64) { + Ok(p) => pics.extend(p), + Err(_) => errors += 1, + } + } + match dec.flush_result() { + Ok(p) => pics.extend(p), + Err(_) => errors += 1, + } + (pics, errors) + } + + #[test] + fn jittered_wavefront_gives_the_single_thread_pictures() { + for stream in STREAMS { + let Some(tus) = inter_stream(stream) else { return }; + let name = stream.0; + let (want, errors) = decode_tus(Decoder::with_threads(1), &tus); + assert_eq!(errors, 0, "{name}"); + let _jitter = JitterGuard::on(); + for threads in [2, 3, 5, 8] { + for round in 0..2 { + let tus = tus.clone(); + let (got, errors) = within_a_minute(move || decode_tus(Decoder::with_threads(threads), &tus)); + assert_eq!(errors, 0, "{name}: threads {threads} round {round}"); + assert!(got == want, "{name}: threads {threads} round {round}: pictures differ"); + } + } + } + } + + #[test] + fn frames_over_the_record_limit_decode_in_one_pass() { + for stream in [STREAMS[0], STREAMS[3]] { + let Some(tus) = inter_stream(stream) else { return }; + let name = stream.0; + let (want, _) = decode_tus(Decoder::with_threads(1), &tus); + for (threads, limit) in [(3, 0), (8, 0), (8, 1)] { + let tus = tus.clone(); + let (got, errors) = within_a_minute(move || { + let mut dec = Decoder::with_threads(threads); + dec.record_limit = Some(limit); + decode_tus(dec, &tus) + }); + assert_eq!(errors, 0, "{name}: threads {threads}, limit {limit}"); + assert!(got == want, "{name}: threads {threads}, limit {limit}: pictures differ"); + } + } + } + + #[test] + fn the_parse_records_of_all_frames_stay_within_the_decoder_budget() { + for stream in [STREAMS[0], STREAMS[3]] { + let Some(tus) = inter_stream(stream) else { return }; + let name = stream.0; + let (want, _) = decode_tus(Decoder::with_threads(1), &tus); + // Room for about one frame's records: the other frames in flight decode in one pass. + for (threads, budget) in [(3, 0), (8, 3 << 20), (16, 1 << 20)] { + let tus = tus.clone(); + let b = Arc::new(crate::av1::decode::tile::RecordBudget::new(budget)); + let mut dec = Decoder::with_threads(threads); + dec.record_budget = b.clone(); + let (got, errors) = within_a_minute(move || decode_tus(dec, &tus)); + assert_eq!(errors, 0, "{name}: threads {threads}, budget {budget}"); + assert!(got == want, "{name}: threads {threads}, budget {budget}: pictures differ"); + assert!(b.peak.load(std::sync::atomic::Ordering::Acquire) <= budget, "{name}: threads {threads}: over the budget"); + assert_eq!(b.used(), 0, "{name}: threads {threads}: records not released"); + } + } + } + + #[cfg(all(feature = "threads", not(target_arch = "wasm32")))] + #[test] + fn a_reference_panicking_after_publishing_rows_fails_without_a_hang() { + let Some(tus) = inter_stream(STREAMS[0]) else { return }; + let _jitter = JitterGuard::on(); + // Frame 1 is the first inter frame; later frames predict from it. + for threads in [2, 3, 8] { + for frame in [0, 1, 2] { + let tus = tus.clone(); + let (pics, errors) = within_a_minute(move || { + let mut dec = Decoder::with_threads(threads); + dec.panic_at_row = Some((frame, 1)); + decode_tus(dec, &tus) + }); + assert!(errors > 0, "threads {threads}, frame {frame}: the panic was not reported ({} pictures)", pics.len()); + } + } + } + + #[test] + fn dropping_a_busy_decoder_does_not_hang() { + let Some(tus) = inter_stream(STREAMS[0]) else { return }; + for threads in [2, 3, 8, 16] { + for cut in [1, 3, tus.len() / 2] { + let tus = tus.clone(); + within_a_minute(move || { + let mut dec = Decoder::with_threads(threads); + for (i, tu) in tus.iter().take(cut).enumerate() { + let _ = dec.decode_pts(tu, i as i64); + } + drop(dec); + }); + } + } + } + + #[test] + fn a_flush_mid_stream_keeps_the_pictures() { + let Some(tus) = inter_stream(STREAMS[0]) else { return }; + let (want, _) = decode_tus(Decoder::with_threads(1), &tus); + for threads in [2, 8] { + for cut in [2, 5, tus.len() / 2] { + let tus = tus.clone(); + let got = within_a_minute(move || { + let mut dec = Decoder::with_threads(threads); + let mut pics = Vec::new(); + for (i, tu) in tus.iter().enumerate() { + if i == cut { + pics.extend(dec.flush_result().expect("flush")); + } + pics.extend(dec.decode_pts(tu, i as i64).expect("decode")); + } + pics.extend(dec.flush_result().expect("flush")); + pics + }); + assert!(got == want, "threads {threads}, flush before unit {cut}"); + } + } + } + + #[test] + fn progress_wakes_every_waiter_with_the_rows_or_the_failure() { + let p = Arc::new(Progress::new()); + let waiters: Vec<_> = (1..=8) + .map(|k| { + let p = p.clone(); + std::thread::spawn(move || p.wait(k * 10)) + }) + .collect(); + p.publish(40); + p.publish(20); // never goes back + assert_eq!(p.wait(40), Ok(())); + p.fail(); + let res: Vec<_> = waiters.into_iter().map(|w| w.join().expect("waiter")).collect(); + assert!(res[..4].iter().all(|r| r.is_ok()), "{res:?}"); + assert!(res[4..].iter().all(|r| r.is_err()), "{res:?}"); + assert_eq!(p.wait(40), Ok(()), "rows published before a failure stay readable"); + } + + #[test] + fn a_failed_frame_keeps_its_published_parse_results() { + let slot = FrameSlot::new(BandFrame::new(16, 16, 1, 0, 0)); + let parsed = Parsed { cdfs: Cdfs::new(0), segment_ids: vec![], mi_cols: 4, mi_rows: 4, saved_ref_frames: vec![], saved_mvs: vec![] }; + slot.set_parsed(Ok(Arc::new(parsed))); + slot.finish(Err(Error::Invalid("x")), 16); + assert!(slot.wait_parsed().is_ok()); + assert!(slot.wait().is_err()); + assert!(slot.progress.wait(1).is_err()); + // A frame failing before its parse results: every waiter gets the error. + let slot = FrameSlot::new(BandFrame::new(16, 16, 1, 0, 0)); + slot.finish(Err(Error::Invalid("y")), 16); + assert!(slot.wait_parsed().is_err()); + } + + fn motion(mi_row: usize, bh4: usize, ref_frame: [i8; 2], mv: [[i32; 2]; 2]) -> crate::av1::decode::tile::BlockMotion { + crate::av1::decode::tile::BlockMotion { + mi_row, + bh4, + inter: true, + ref_frame, + mv, + motion_mode: 0, + y_mode: crate::av1::decode::spec_tables::NEARESTMV, + } + } + + #[test] + fn rows_needed_follow_the_motion_vectors() { + let mut fh = FrameHeader { frame_width: 64, upscaled_width: 64, frame_height: 256, ..Default::default() }; + fh.ref_frame_idx = [0, 1, 2, 3, 4, 5, 6]; + let mut ri: [RefInfo; NUM_REF_FRAMES] = Default::default(); + for r in ri.iter_mut() { + (r.upscaled_width, r.frame_height) = (64, 256); + } + let none = [[0; 2]; 2]; + // 41/8 rows down rounds up to 6; upward motion needs only the block's own rows. + let need = rows_needed(&fh, &ri, [motion(0, 16, [1, -1], [[41, 0], [0, 0]]), motion(0, 16, [2, 3], [[-80, 0], [7, 0]])].into_iter(), 64); + assert_eq!(need[0], 64 + 6 + ROW_WAIT_MARGIN); + assert_eq!(need[1], 64 + ROW_WAIT_MARGIN); + assert_eq!(need[2], 64 + 1 + ROW_WAIT_MARGIN); + assert_eq!(need[3], 0); + // Intra blocks and intra block copy read no reference. + let intra = crate::av1::decode::tile::BlockMotion { inter: false, ..motion(0, 16, [1, -1], none) }; + assert_eq!(rows_needed(&fh, &ri, [intra].into_iter(), 64), [0; NUM_REF_FRAMES]); + // Blocks reaching below the superblock row count from their own bottom. + assert_eq!(rows_needed(&fh, &ri, [motion(12, 8, [1, -1], none)].into_iter(), 64)[0], 80 + ROW_WAIT_MARGIN); + // Local warp: the whole of every reference the rows use. + let warp = + crate::av1::decode::tile::BlockMotion { motion_mode: crate::av1::decode::spec_tables::LOCALWARP as u8, ..motion(0, 16, [1, -1], none) }; + let need = rows_needed(&fh, &ri, [warp, motion(0, 16, [2, -1], none)].into_iter(), 64); + assert_eq!((need[0], need[1], need[2]), (usize::MAX, usize::MAX, 0)); + // Global motion beyond translation for GLOBALMV blocks. + fh.gm_type[3] = (TRANSLATION + 1) as u8; + let global = crate::av1::decode::tile::BlockMotion { y_mode: GLOBALMV, ..motion(0, 16, [1, -1], none) }; + assert_eq!(rows_needed(&fh, &ri, [global].into_iter(), 64)[0], usize::MAX); + fh.gm_type[3] = 0; + // A scaled reference. + ri[0].frame_height = 128; + assert_eq!(rows_needed(&fh, &ri, [motion(0, 16, [1, -1], none)].into_iter(), 64)[0], usize::MAX); + } + #[test] fn frames_beyond_the_limits_are_refused() { assert!(!frame_size_allowed(16384, 2177)); // one row over MaxPicSize diff --git a/crates/video/src/av1/decode/frame.rs b/crates/video/src/av1/decode/frame.rs index d0432e2a8..e28ca89e9 100644 --- a/crates/video/src/av1/decode/frame.rs +++ b/crates/video/src/av1/decode/frame.rs @@ -197,6 +197,50 @@ impl FrameBuf { } } + /// Copy luma rows [y0, y1) (chroma rows scaled down; clipped to the stored rows) of `src`, a + /// frame of the same geometry. + pub fn copy_rows_from(&mut self, src: &FrameBuf, y0: usize, y1: usize) { + for p in 0..self.num_planes { + let sy = if p == 0 { 0 } else { self.subsampling_y }; + let (d, s) = (&mut self.planes[p], &src.planes[p]); + if d.stride != s.stride { + continue; + } + let a = (y0 >> sy).min(d.rows).min(s.rows) * d.stride; + let b = (y1 >> sy).min(d.rows).min(s.rows) * d.stride; + if let (Some(dst), Some(src)) = (d.data.get_mut(a..b), s.data.get(a..b)) { + dst.copy_from_slice(src); + } + } + } + + /// Copies of luma rows [y0, y1) (chroma rows scaled down; clipped to the stored rows), one + /// plane region each. + pub fn rows_copy(&self, y0: usize, y1: usize) -> [Plane; 3] { + std::array::from_fn(|p| { + if p >= self.num_planes { + return Plane::default(); + } + let sy = if p == 0 { 0 } else { self.subsampling_y }; + let s = &self.planes[p]; + let (a, b) = ((y0 >> sy).min(s.rows), (y1 >> sy).min(s.rows)); + let mut out = Plane::region(0, a, s.stride, b - a); + if let Some(src) = s.data.get(a * s.stride..b * s.stride) { + out.data.copy_from_slice(src); + } + out + }) + } + + /// A frame of this geometry (samples not copied: zero). + pub fn same_geometry(&self) -> FrameBuf { + let planes = std::array::from_fn(|p| { + let pl = &self.planes[p]; + Plane { data: zeroed(pl.data.len()), stride: pl.stride, rows: pl.rows, ox: pl.ox, oy: pl.oy } + }); + FrameBuf { planes, ..*self } + } + /// [`Plane::row_bands`] of all three planes for ranges of luma rows (chroma rows scaled /// down): element `i` holds the bands of range `i`. pub fn row_bands(&mut self, luma: &[(usize, usize)]) -> Vec<[RowBand<'_>; 3]> { @@ -208,6 +252,128 @@ impl FrameBuf { } } +/// Luma rows per published band of a [`BandFrame`] (log2). +pub const BAND_LOG2: u32 = 6; + +/// One plane of a frame's final samples, published band by band while the rest of the frame +/// is still being decoded (see [`BandFrame`]). A band is written once and then only read. +pub struct BandPlane { + bands: Vec>, + stride: usize, + rows: usize, + log2: u32, + /// Returned for rows of a band that is not published: zeros instead of a panic. Readers wait + /// for the row progress first and references of another chroma format are refused, so + /// this only happens through a scheduling bug. + missing: Box<[u16]>, +} + +impl BandPlane { + fn new(stride: usize, rows: usize, log2: u32) -> BandPlane { + let n = rows.div_ceil(1 << log2); + BandPlane { bands: (0..n).map(|_| std::sync::OnceLock::new()).collect(), stride, rows, log2, missing: vec![0; stride].into_boxed_slice() } + } + + /// Row `y` (`stride` samples) of a published band. + #[inline(always)] + pub fn row(&self, y: usize) -> &[u16] { + let o = (y & ((1 << self.log2) - 1)) * self.stride; + match self.bands.get(y >> self.log2).and_then(|b| b.get()).and_then(|b| b.data.get(o..o + self.stride)) { + Some(r) => r, + None => { + debug_assert!(false, "row {y} read before its band was published"); + &self.missing + } + } + } + + /// Publish band `band` copied from the rows of the whole plane `src` (same geometry); a + /// band is published once. + fn publish(&self, band: usize, src: &Plane) { + let y0 = band << self.log2; + let y1 = ((band + 1) << self.log2).min(self.rows); + let mut p = Plane::region(0, y0, self.stride, y1.saturating_sub(y0)); + let w = self.stride.min(src.stride); + for (y, out) in (y0..y1).zip(p.data.chunks_exact_mut(self.stride.max(1))) { + if let Some(r) = src.data.get(y * src.stride..y * src.stride + w) { + out[..w].copy_from_slice(r); + } + } + self.publish_owned(band, p); + } + + /// Publish band `band` as `p`, whose rows start at the band's first row (stride `stride`; + /// rows past the band are ignored). + fn publish_owned(&self, band: usize, p: Plane) { + if let Some(slot) = self.bands.get(band) + && p.stride == self.stride + { + let _ = slot.set(p); + } + } +} + +/// A frame's final samples (every in-loop filter applied) in bands of 64 luma rows, so later +/// frames can predict from the top of a reference while its bottom is still being decoded. +/// Each band is published once, before the frame's row progress says it is final. +pub struct BandFrame { + pub planes: [BandPlane; 3], + pub num_planes: usize, + pub subsampling_x: usize, + pub subsampling_y: usize, +} + +impl BandFrame { + /// Bands for the visible rows of a `width` x `height` frame stored like [`FrameBuf::new`]. + pub fn new(width: usize, height: usize, num_planes: usize, subsampling_x: usize, subsampling_y: usize) -> BandFrame { + let aw = width.div_ceil(128) * 128 + 160; + let planes = std::array::from_fn(|p| { + if p >= num_planes { + return BandPlane::new(0, 0, BAND_LOG2); + } + let (sx, sy) = if p == 0 { (0, 0) } else { (subsampling_x, subsampling_y) }; + BandPlane::new(aw >> sx, (height + sy) >> sy, BAND_LOG2 - sy as u32) + }); + BandFrame { planes, num_planes, subsampling_x, subsampling_y } + } + + /// Number of bands (of 64 luma rows). + pub fn bands(&self) -> usize { + self.planes[0].bands.len() + } + + /// Publish luma band `band` (and the chroma rows it covers) copied from `src`. + pub fn publish(&self, band: usize, src: &FrameBuf) { + for p in 0..self.num_planes { + self.planes[p].publish(band, &src.planes[p]); + } + } + + /// Publish luma band `band` from per-plane buffers starting at the band's first row. + pub fn publish_owned(&self, band: usize, planes: [Plane; 3]) { + for (p, pl) in planes.into_iter().enumerate().take(self.num_planes) { + self.planes[p].publish_owned(band, pl); + } + } +} + +/// A plane to predict from: a whole stored plane or the published bands of a reference. +#[derive(Clone, Copy)] +pub enum PlaneRef<'a> { + Whole(&'a Plane), + Bands(&'a BandPlane), +} + +impl<'a> PlaneRef<'a> { + #[inline(always)] + pub fn row(&self, y: usize) -> &'a [u16] { + match *self { + PlaneRef::Whole(p) => p.row(y), + PlaneRef::Bands(b) => b.row(y), + } + } +} + /// A motion vector (row, col) in 1/8 sample units. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct Mv { diff --git a/crates/video/src/av1/decode/inter.rs b/crates/video/src/av1/decode/inter.rs index 8b40717a3..8d80e96b0 100644 --- a/crates/video/src/av1/decode/inter.rs +++ b/crates/video/src/av1/decode/inter.rs @@ -3,7 +3,7 @@ use std::sync::OnceLock; -use crate::av1::decode::frame::Plane; +use crate::av1::decode::frame::{Plane, PlaneRef}; use crate::av1::decode::mvpred::round2_signed64; use crate::av1::decode::spec_tables::{ BLOCK_8X8, BLOCK_SIZES, COMPOUND_AVERAGE, COMPOUND_DIFFWTD, COMPOUND_DISTANCE, COMPOUND_INTRA, COMPOUND_WEDGE, DIV_LUT, DIV_LUT_BITS, @@ -227,12 +227,9 @@ impl TileDecoder<'_, '_> { (start_x as i32, start_y as i32, step_x as i32, step_y as i32) } - fn ref_plane(&self, ref_idx: i32, plane: usize) -> &Plane { + fn ref_plane(&self, ref_idx: i32, plane: usize) -> PlaneRef<'_> { // A missing reference (rejected earlier for valid streams) predicts from the current frame. - match usize::try_from(ref_idx).ok().and_then(|i| self.fs.refs.get(i)).and_then(|r| r.as_ref()) { - Some(r) => &r.buf.planes[plane], - None => &self.t.cur.planes[plane], - } + self.fs.ref_samples(ref_idx, plane).unwrap_or(PlaneRef::Whole(&self.t.cur.planes[plane])) } /// Block inter prediction process (7.11.3.4) into `pred` (stride PRED_STRIDE); `tmp` holds @@ -512,12 +509,12 @@ impl TileDecoder<'_, '_> { return false; } let fs = self.fs; - let Some(r) = usize::try_from(ref_idx).ok().and_then(|i| fs.refs.get(i)).and_then(|r| r.as_ref()) else { + let Some(refp) = fs.ref_samples(ref_idx, plane) else { return false; }; let dims = self.ref_plane_dims(ref_idx, plane); let max = ((1u32 << fs.bit_depth) - 1) as u16; - copy_fullpel(&r.buf.planes[plane], dims, McBlock { x: sx, y: sy, w, h }, &mut self.t.cur.planes[plane], (x, y), max); + copy_fullpel(refp, dims, McBlock { x: sx, y: sy, w, h }, &mut self.t.cur.planes[plane], (x, y), max); true } @@ -763,7 +760,7 @@ impl TileDecoder<'_, '_> { /// Unscaled single-reference prediction at a whole-sample position: both filter phases are the /// identity and Round2(128 * Round2(128 * v, InterRound0), InterRound1) == v for every bit /// depth, so the prediction is the (clamped) reference sample. -fn copy_fullpel(refp: &Plane, (last_x, last_y): (i32, i32), src_blk: McBlock, dst: &mut Plane, (dx, dy): (usize, usize), max: u16) { +fn copy_fullpel(refp: PlaneRef, (last_x, last_y): (i32, i32), src_blk: McBlock, dst: &mut Plane, (dx, dy): (usize, usize), max: u16) { let McBlock { x, y, w, h } = src_blk; let x0 = x >> 10; let y0 = y >> 10; @@ -831,7 +828,7 @@ fn vpass(rows: &[i32], vf: &[i16; 8], out: &mut [i32], half: i32 /// Block inter prediction without reference scaling: every column / row uses the same filter /// phase, so the filters run over whole rows (bit-identical to the per-sample process). fn mc_unscaled( - refp: &Plane, + refp: PlaneRef, (last_x, last_y): (i32, i32), blk: McBlock, (hf, vf): (&[i16; 8], &[i16; 8]), @@ -982,9 +979,17 @@ mod tests { [(0, 0), (-5, -3), (7, 9), (last_x - w as i32 / 2, last_y - h as i32 / 2), (last_x + 3, last_y + 2), (-(w as i32) - 4, 3)] { let (x, y) = ((px << 10) | 32, (py << 10) | 32); - mc_unscaled(&refp, (last_x, last_y), McBlock { x, y, w, h }, (&IDENTITY_TAPS, &IDENTITY_TAPS), rnd, &mut pred, &mut tmp); + mc_unscaled( + PlaneRef::Whole(&refp), + (last_x, last_y), + McBlock { x, y, w, h }, + (&IDENTITY_TAPS, &IDENTITY_TAPS), + rnd, + &mut pred, + &mut tmp, + ); let mut dst = Plane::new(w + 3, h); - copy_fullpel(&refp, (last_x, last_y), McBlock { x, y, w, h }, &mut dst, (3, 0), max); + copy_fullpel(PlaneRef::Whole(&refp), (last_x, last_y), McBlock { x, y, w, h }, &mut dst, (3, 0), max); for i in 0..h { for j in 0..w { let want = spec_phase0(&refp, (last_x, last_y), (px, py), (j, i), (w * 3 + h) % SUBPEL_FILTERS.len(), rnd) diff --git a/crates/video/src/av1/decode/postfilter.rs b/crates/video/src/av1/decode/postfilter.rs index 6304fdfb6..aa9c2e3ea 100644 --- a/crates/video/src/av1/decode/postfilter.rs +++ b/crates/video/src/av1/decode/postfilter.rs @@ -1,7 +1,7 @@ //! In-loop post filters run by decode_frame_wrapup: loop filter (7.14), CDEF (7.15), //! super-resolution upscaling (7.16) and loop restoration (7.17). -use crate::av1::decode::frame::RowBand; +use crate::av1::decode::frame::{FrameBuf, Plane, RowBand}; use crate::av1::decode::spec_tables::{ GLOBAL_GLOBALMV, GLOBALMV, INTRA_FRAME, MAX_LOOP_FILTER, NEARESTMV, NUM_4X4_BLOCKS_HIGH, NUM_4X4_BLOCKS_WIDE, SEG_LVL_ALT_LF_Y_V, TX_HEIGHT, TX_WIDTH, @@ -48,6 +48,121 @@ pub(crate) fn apply(fs: &mut FrameState, stats: &mut DecodeStats, pool: &crate:: stats.add(Stage::Restoration, t.lap()); } +/// The in-loop filters run band by band behind reconstruction (row wavefront): each stage of a +/// band starts once the stage before has finished the band below it, which bounds what the +/// stage reads beyond the band (the loop filter changes up to 7 rows above a band and intra +/// prediction of the next superblock row reads the unfiltered row above it; CDEF reads 2 rows +/// and loop restoration 3 rows beyond a band). Bands are 64 luma rows. The result is the same +/// as running each filter over the whole frame. Super-resolution frames use [`apply`]. +/// +/// The last active stage writes each band into its own buffers, which are handed out as the +/// band's final samples without a copy. +pub(crate) struct Pipeline { + bands: Vec<(usize, usize)>, + /// Luma rows of the coded frame (mi_rows * 4). + rows: usize, + lf_on: bool, + cdef_on: bool, + lr_on: bool, + /// Bands finished by each stage. + lf: usize, + cdef: usize, + lr: usize, + pending: [Vec; 3], + /// CdefFrame when loop restoration reads it. + cdef_out: Option, +} + +/// Final bands from [`Pipeline::advance`]: (band, its samples starting at the band's first row). +pub(crate) type FinalBands = Vec<(usize, [Plane; 3])>; + +impl Pipeline { + pub fn new(fs: &FrameState, draft: bool) -> Pipeline { + let rows = fs.fh.mi_rows as usize * 4; + let n = rows.div_ceil(BAND_ROWS).max(1); + let bands = (0..n).map(|b| (b * BAND_ROWS, if b + 1 == n { usize::MAX } else { (b + 1) * BAND_ROWS })).collect(); + let lvl = fs.fh.lf.level; + let cdef_on = !draft && crate::av1::decode::cdef::active(fs); + let lr_on = fs.fh.lr.uses_lr && !draft; + let cdef_out = (cdef_on && lr_on).then(|| fs.cur.same_geometry()); + Pipeline { + bands, + rows, + lf_on: (lvl[0] != 0 || lvl[1] != 0) && !draft, + cdef_on, + lr_on, + lf: 0, + cdef: 0, + lr: 0, + pending: Default::default(), + cdef_out, + } + } + + /// Filter what luma rows [0, `recon_rows`) of reconstructed samples allow (everything once + /// they cover the frame); returns the bands whose samples became final. + pub fn advance(&mut self, fs: &mut FrameState, stats: &mut DecodeStats, pool: &crate::av1::decode::par::Pool, recon_rows: usize) -> FinalBands { + let n = self.bands.len(); + let all = recon_rows >= self.rows; + let mut out = FinalBands::new(); + let mut t = Timer::start(); + // The loop filter of a band waits for the band below it to be reconstructed. + let lf_to = if all { n } else { (recon_rows / BAND_ROWS).saturating_sub(1) }; + if lf_to > self.lf { + if self.lf_on { + loop_filter_bands(fs, pool, &self.bands, self.lf..lf_to, &mut self.pending); + } + self.lf = lf_to; + } + stats.add(Stage::LoopFilter, t.lap()); + let cdef_to = if all { n } else { self.lf.saturating_sub(1) }; + if cdef_to > self.cdef { + let r = &self.bands[self.cdef..cdef_to]; + if self.cdef_on { + if let Some(cdef) = &mut self.cdef_out { + cdef.copy_rows_from(&fs.cur, r[0].0, r[r.len() - 1].1); + crate::av1::decode::cdef::filter_bands(fs, pool, cdef.row_bands(r), r); + } else { + let mut own: Vec<[Plane; 3]> = r.iter().map(|&(a, b)| fs.cur.rows_copy(a, b)).collect(); + crate::av1::decode::cdef::filter_bands(fs, pool, own_bands(&mut own), r); + out.extend((self.cdef..cdef_to).zip(own)); + } + } else if !self.lr_on { + // The loop filter is the last stage: the band is final once the band below it + // is filtered. + out.extend((self.cdef..cdef_to).zip(r.iter().map(|&(a, b)| fs.cur.rows_copy(a, b)))); + } + self.cdef = cdef_to; + } + stats.add(Stage::Cdef, t.lap()); + let lr_to = if all { n } else { self.cdef.saturating_sub(1) }; + if self.lr_on && lr_to > self.lr { + let r = &self.bands[self.lr..lr_to]; + let cdef = self.cdef_out.as_ref().unwrap_or(&fs.cur); + let mut own: Vec<[Plane; 3]> = r.iter().map(|&(a, b)| cdef.rows_copy(a, b)).collect(); + crate::av1::decode::restoration::restore_bands(fs, &fs.cur, cdef, own_bands(&mut own), pool, r); + out.extend((self.lr..lr_to).zip(own)); + self.lr = lr_to; + } + stats.add(Stage::Restoration, t.lap()); + out + } +} + +/// Row bands over per-band buffers made by [`FrameBuf::rows_copy`]. +fn own_bands(own: &mut [[Plane; 3]]) -> Vec<[RowBand<'_>; 3]> { + fn band(p: &mut Plane) -> RowBand<'_> { + let (stride, y0) = (p.stride.max(1), p.oy); + RowBand { data: p.data.as_mut_slice(), stride, y0 } + } + own.iter_mut() + .map(|planes| { + let [a, b, c] = planes; + [band(a), band(b), band(c)] + }) + .collect() +} + // --------------------------------------------------------------------------------------------- // Loop filter (7.14) @@ -69,23 +184,40 @@ const LF_MARGIN: usize = 8; /// written back; the band above never touches them. fn loop_filter(fs: &mut FrameState, pool: &crate::av1::decode::par::Pool) { let luma = bands(pool, fs.fh.mi_rows as usize * 4); + loop_filter_bands(fs, pool, &luma, 0..luma.len(), &mut Default::default()); +} + +/// The loop filter on bands `range` of `all` (the frame's bands). The bands above the range +/// are filtered already: `pending` holds the unfiltered rows above the first band of the range +/// (empty for band 0), saved before the band above was filtered, and is set to those of the +/// band after the range. +fn loop_filter_bands( + fs: &mut FrameState, + pool: &crate::av1::decode::par::Pool, + all: &[(usize, usize)], + range: std::ops::Range, + pending: &mut [Vec; 3], +) { + let Some(luma) = all.get(range.clone()) else { return }; let planes: Vec = (0..fs.num_planes).filter(|&p| p == 0 || fs.fh.lf.level[1 + p] != 0).collect(); let mut cur = std::mem::take(&mut fs.cur); let f: &FrameState = fs; - // Unfiltered copies of the rows above each band but the first. - let margins: Vec<[Vec; 3]> = luma - .iter() - .map(|&(start, _)| { - std::array::from_fn(|p| { - let sub_y = if p == 0 { 0 } else { f.ssy }; - let pl = &cur.planes[p]; - let (y0, y1) = ((start.saturating_sub(LF_MARGIN)) >> sub_y, start >> sub_y); - let rows = pl.data.get(y0 * pl.stride..y1 * pl.stride); - if start == 0 || !planes.contains(&p) { Vec::new() } else { rows.map(<[u16]>::to_vec).unwrap_or_default() } - }) + // Unfiltered copies of the rows above a band (none above the first). + let margin = |cur: &crate::av1::decode::frame::FrameBuf, start: usize| -> [Vec; 3] { + std::array::from_fn(|p| { + let sub_y = if p == 0 { 0 } else { f.ssy }; + let pl = &cur.planes[p]; + let (y0, y1) = ((start.saturating_sub(LF_MARGIN)) >> sub_y, start >> sub_y); + let rows = pl.data.get(y0 * pl.stride..y1 * pl.stride); + if start == 0 || !planes.contains(&p) { Vec::new() } else { rows.map(<[u16]>::to_vec).unwrap_or_default() } }) - .collect(); - let mut bands: Vec<([RowBand; 3], [Vec; 3])> = cur.row_bands(&luma).into_iter().zip(margins).collect(); + }; + let margins: Vec<[Vec; 3]> = + luma.iter().enumerate().map(|(i, &(start, _))| if i == 0 { std::mem::take(pending) } else { margin(&cur, start) }).collect(); + if let Some(&(next, _)) = all.get(range.end) { + *pending = margin(&cur, next); + } + let mut bands: Vec<([RowBand; 3], [Vec; 3])> = cur.row_bands(luma).into_iter().zip(margins).collect(); let changed = pool.map_mut(&mut bands, |b, (band, margin)| { let mut changed: [Vec<(usize, u16)>; 3] = Default::default(); for &p in &planes { diff --git a/crates/video/src/av1/decode/restoration.rs b/crates/video/src/av1/decode/restoration.rs index efc5a751f..d252f7b06 100644 --- a/crates/video/src/av1/decode/restoration.rs +++ b/crates/video/src/av1/decode/restoration.rs @@ -70,10 +70,16 @@ struct LrPlane<'a> { /// Bands of 64 luma rows are restored in parallel: every unit reads only the two input frames /// and writes only its own samples. pub(crate) fn loop_restoration(fs: &FrameState, cur: &FrameBuf, cdef: &FrameBuf, pool: &Pool) -> FrameBuf { - let fh = &fs.fh; let mut lr = cdef.clone(); - let ranges = bands(pool, fh.frame_height as usize); - let mut planes = lr.row_bands(&ranges); + let ranges = bands(pool, fs.fh.frame_height as usize); + restore_bands(fs, cur, cdef, lr.row_bands(&ranges), pool, &ranges); + lr +} + +/// Restore the bands `planes` covering luma rows `ranges`, which hold UpscaledCdefFrame; reads +/// both inputs up to 3 rows beyond each band. +pub(crate) fn restore_bands(fs: &FrameState, cur: &FrameBuf, cdef: &FrameBuf, mut planes: Vec<[RowBand; 3]>, pool: &Pool, ranges: &[(usize, usize)]) { + let fh = &fs.fh; pool.map_mut(&mut planes, |b, band| { let (start, end) = ranges[b]; let mut scratch = Scratch::default(); @@ -100,8 +106,6 @@ pub(crate) fn loop_restoration(fs: &FrameState, cur: &FrameBuf, cdef: &FrameBuf, p.restore(out, start >> sub_y, end >> sub_y, &mut scratch); } }); - drop(planes); - lr } /// Row buffers reused across stripes. diff --git a/crates/video/src/av1/decode/state.rs b/crates/video/src/av1/decode/state.rs index df2e2d682..e0e5a8522 100644 --- a/crates/video/src/av1/decode/state.rs +++ b/crates/video/src/av1/decode/state.rs @@ -6,8 +6,8 @@ //! with several, each tile decodes into private buffers covering its area (tiles never read //! outside themselves), which are then copied into the frame. -use crate::av1::decode::decoder::RefFrame; -use crate::av1::decode::frame::{FrameBuf, MiInfo}; +use crate::av1::decode::decoder::{Parsed, RefFrame}; +use crate::av1::decode::frame::{BandFrame, FrameBuf, MiInfo, PlaneRef}; use crate::av1::decode::header::{FrameHeader, RefInfo, SequenceHeader}; use crate::av1::decode::spec_tables::{DEFAULT_INTRA_FRAME_Y_MODE_CDF, NUM_REF_FRAMES, RESTORE_NONE, SUBSAMPLED_SIZE}; use std::ops::{Deref, DerefMut}; @@ -26,6 +26,11 @@ pub(crate) struct FrameShared { pub cdef_cols: usize, /// Reference slots (FrameStore and saved state) used by this frame. pub refs: [Option>; NUM_REF_FRAMES], + /// Parse results (CDFs, segment ids, saved motion vectors) of the same slots. + pub parsed: [Option>; NUM_REF_FRAMES], + /// The published bands of references that are still being decoded (row wavefront): read + /// only below the row progress that was waited for. + pub ref_rows: [Option>; NUM_REF_FRAMES], pub ref_info: [RefInfo; NUM_REF_FRAMES], /// MotionFieldMvs[ ref ][ y8 * w8 + x8 ] (7.9); empty when unused. pub motion_field: [Vec<[i32; 2]>; 8], @@ -34,6 +39,16 @@ pub(crate) struct FrameShared { } impl FrameShared { + /// The samples of reference slot `ref_idx` to predict from (None for a missing reference). + #[inline] + pub fn ref_samples(&self, ref_idx: i32, plane: usize) -> Option> { + let i = usize::try_from(ref_idx).ok()?; + if let Some(b) = self.ref_rows.get(i)?.as_ref() { + return b.planes.get(plane).map(PlaneRef::Bands); + } + self.refs.get(i)?.as_ref().map(|r| r.plane(plane)) + } + pub fn plane_residual_size(&self, subsize: usize, plane: usize) -> usize { let sx = if plane > 0 { self.ssx } else { 0 }; let sy = if plane > 0 { self.ssy } else { 0 }; @@ -210,6 +225,8 @@ impl FrameState { lr_unit_cols, cdef_cols, refs: Default::default(), + parsed: Default::default(), + ref_rows: Default::default(), ref_info: Default::default(), motion_field: Default::default(), prev_segment_ids: vec![0; mi_cols * mi_rows], @@ -276,6 +293,14 @@ impl FrameState { pub fn merge_tile_state(&mut self, t: &TileState) { for p in 0..self.num_planes { t.cur.planes[p].copy_into(&mut self.cur.planes[p]); + } + self.merge_tile_modes(t); + } + + /// Copy a tile's private buffers except the samples (after parsing, before reconstruction) + /// into the frame. + pub fn merge_tile_modes(&mut self, t: &TileState) { + for p in 0..self.num_planes { t.lf_tx_size[p].copy_into(&mut self.lf_tx_size[p]); for &u in &t.lr_written[p] { self.lr.lr_type[p][u] = t.lr.lr_type[p][u]; diff --git a/crates/video/src/av1/decode/tile.rs b/crates/video/src/av1/decode/tile.rs index 16d59ed3a..2572f9d21 100644 --- a/crates/video/src/av1/decode/tile.rs +++ b/crates/video/src/av1/decode/tile.rs @@ -94,6 +94,191 @@ pub(crate) struct Block { pub local_warp_params: [i32; 6], } +/// What a tile decoder does with the blocks it visits. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub(crate) enum Pass { + /// Read the symbols and reconstruct each block right away (single-threaded decoding). + Full, + /// Read the symbols only, recording what reconstruction needs in a [`TileRecord`]. + Parse, + /// Reconstruct from a [`TileRecord`], reading no symbols. + Reconstruct, +} + +/// The per-block values reconstruction needs beyond the mode info (which is final after +/// parsing): the block variables as parsed, its quantizer index and the warp samples / palette +/// color maps when used. +#[derive(Clone)] +pub(crate) struct BlockRecord { + b: Block, + current_q_index: i32, + warp_samples: Option>, + color_maps: Option, Vec)>>, +} + +/// A parsed tile: its blocks in decoding order, how many of them each superblock holds and the +/// coefficients of every coded transform block as a stream of +/// `eob, tx_type, count, (position, value) * count` (only non-zero values). +#[derive(Default)] +pub(crate) struct TileRecord { + blocks: Vec, + sb_blocks: Vec, + coeffs: Vec, + /// Read positions while reconstructing. + next_block: usize, + next_coeff: usize, + next_sb: usize, + next_sb_row: usize, + /// Bytes recorded and the most allowed. + pub bytes: usize, + pub limit: usize, + /// The decoder-wide budget the recorded bytes are reserved from, and how much is reserved. + pub budget: Option>, + reserved: usize, +} + +impl Drop for TileRecord { + fn drop(&mut self) { + if let Some(b) = &self.budget { + b.release(self.reserved); + } + } +} + +/// The parse record bytes all frames of a decoder may hold at once (frames in flight decode in +/// one pass once it is used up). +pub(crate) struct RecordBudget { + used: std::sync::atomic::AtomicUsize, + max: usize, + /// Most bytes ever reserved at once (tests). + #[cfg(test)] + pub peak: std::sync::atomic::AtomicUsize, +} + +/// Bytes reserved from a [`RecordBudget`] at a time. +const RESERVE_STEP: usize = 1 << 20; + +impl RecordBudget { + pub fn new(max: usize) -> RecordBudget { + RecordBudget { + used: Default::default(), + max, + #[cfg(test)] + peak: Default::default(), + } + } + + /// Reserve `n` bytes; false (nothing reserved) if that would go over the budget. + fn reserve(&self, n: usize) -> bool { + use std::sync::atomic::Ordering; + let ok = self.used.try_update(Ordering::AcqRel, Ordering::Acquire, |u| u.checked_add(n).filter(|&v| v <= self.max)).is_ok(); + #[cfg(test)] + if ok { + self.peak.fetch_max(self.used.load(Ordering::Acquire), Ordering::AcqRel); + } + ok + } + + fn release(&self, n: usize) { + self.used.fetch_sub(n, std::sync::atomic::Ordering::AcqRel); + } + + /// Bytes reserved now (tests). + #[cfg(test)] + pub fn used(&self) -> usize { + self.used.load(std::sync::atomic::Ordering::Acquire) + } +} + +/// The motion of a recorded block (what a reference row wait depends on). +pub(crate) struct BlockMotion { + pub mi_row: usize, + pub bh4: usize, + /// Inter prediction from reference frames (not intra block copy). + pub inter: bool, + pub ref_frame: [i8; 2], + /// Mv[ list ] (row, col) in 1/8 samples. + pub mv: [[i32; 2]; 2], + pub motion_mode: u8, + pub y_mode: usize, +} + +impl TileRecord { + /// Whether recording stopped at [`TileRecord::limit`] (the record is unusable). + pub fn over_limit(&self) -> bool { + self.bytes > self.limit + } + + /// Account for `n` more recorded bytes; false once over the limit (nothing more is kept). + fn grow(&mut self, n: usize) -> bool { + self.bytes = self.bytes.saturating_add(n); + if self.bytes > self.limit { + return false; + } + if self.bytes > self.reserved + && let Some(b) = &self.budget + { + let step = (self.bytes - self.reserved).max(RESERVE_STEP); + if !b.reserve(step) { + // The decoder-wide budget is used up: this frame is decoded in one pass. + self.limit = 0; + return false; + } + self.reserved += step; + } + true + } + /// The recorded blocks of superblock rows [first, end), for a tile `sbs_per_row` superblocks wide. + pub fn row_blocks(&self, sbs_per_row: usize, first: usize, end: usize) -> impl Iterator + '_ { + let start: usize = self.sb_blocks.iter().take(first * sbs_per_row).map(|&n| n as usize).sum(); + let count: usize = self.sb_blocks.iter().skip(first * sbs_per_row).take((end - first) * sbs_per_row).map(|&n| n as usize).sum(); + self.blocks.iter().skip(start).take(count).map(|r| BlockMotion { + mi_row: r.b.mi_row, + bh4: r.b.bh4, + inter: r.b.is_inter && !r.b.use_intrabc, + ref_frame: r.b.ref_frame, + mv: r.b.mv_i, + motion_mode: r.b.motion_mode, + y_mode: r.b.y_mode, + }) + } + + fn next_coeff(&mut self) -> Result { + let v = *self.coeffs.get(self.next_coeff).ok_or(crate::av1::decode::Error::Invalid("coefficient record exhausted"))?; + self.next_coeff += 1; + Ok(v) + } +} + +/// The work buffers of a [`TileDecoder`] (`quant` all zero between transform blocks). +pub(crate) struct Buffers { + quant: Vec, + dequant: Vec, + residual: Vec, + color_map_y: Vec, + color_map_uv: Vec, + pred_buf: [Vec; 2], + mask: Vec, + mc_tmp: Vec, + levels: Vec, +} + +impl Buffers { + pub fn new() -> Buffers { + Buffers { + quant: vec![0; 1024], + dequant: vec![0; 64 * 64], + residual: vec![0; 64 * 64], + color_map_y: vec![0; 64 * 64], + color_map_uv: vec![0; 64 * 64], + pred_buf: [vec![0; 128 * 128], vec![0; 128 * 128]], + mask: vec![0; 128 * 128], + mc_tmp: vec![0; (2 * 128 + 8) * 128], + levels: vec![0; (32 + TX_PAD) * (32 + TX_PAD)], + } + } +} + /// Decoder for one tile. pub(crate) struct TileDecoder<'a, 'f> { pub fs: &'f FrameShared, @@ -128,6 +313,9 @@ pub(crate) struct TileDecoder<'a, 'f> { pub mc_tmp: Vec, /// Coefficient levels of the current transform block, padded (see TX_PAD). levels: Vec, + pub pass: Pass, + /// Written by [`Pass::Parse`], read by [`Pass::Reconstruct`]. + pub record: TileRecord, } const SB_MAX: usize = 32; // superblock size in 4x4 units (128 / 4) @@ -141,6 +329,21 @@ fn block_height(bs: usize) -> usize { impl<'a, 'f> TileDecoder<'a, 'f> { pub fn new(fs: &'f FrameShared, t: &'f mut TileState, data: &'a [u8], cdf: Box, tile_row: usize, tile_col: usize) -> Self { + Self::with_buffers(fs, t, data, cdf, tile_row, tile_col, Buffers::new()) + } + + /// [`TileDecoder::new`] reusing the work buffers of an earlier decoder of the same frame + /// (see [`TileDecoder::into_buffers`]). + pub fn with_buffers( + fs: &'f FrameShared, + t: &'f mut TileState, + data: &'a [u8], + cdf: Box, + tile_row: usize, + tile_col: usize, + bufs: Buffers, + ) -> Self { + let Buffers { quant, dequant, residual, color_map_y, color_map_uv, pred_buf, mask, mc_tmp, levels } = bufs; let ti = &fs.fh.tile_info; let (rs, re) = (ti.mi_row_starts[tile_row] as usize, ti.mi_row_starts[tile_row + 1] as usize); let (cs, ce) = (ti.mi_col_starts[tile_col] as usize, ti.mi_col_starts[tile_col + 1] as usize); @@ -164,19 +367,27 @@ impl<'a, 'f> TileDecoder<'a, 'f> { b: Block::default(), mvs: Default::default(), nb: Default::default(), - quant: vec![0; 1024], - dequant: vec![0; 64 * 64], - residual: vec![0; 64 * 64], - color_map_y: vec![0; 64 * 64], - color_map_uv: vec![0; 64 * 64], + quant, + dequant, + residual, + color_map_y, + color_map_uv, plane_tx_type: 0, - pred_buf: [vec![0; 128 * 128], vec![0; 128 * 128]], - mask: vec![0; 128 * 128], - mc_tmp: vec![0; (2 * 128 + 8) * 128], - levels: vec![0; (32 + TX_PAD) * (32 + TX_PAD)], + pred_buf, + mask, + mc_tmp, + levels, + pass: Pass::Full, + record: TileRecord::default(), } } + /// The work buffers and CDFs, for [`TileDecoder::with_buffers`]. + pub fn into_buffers(self) -> (Buffers, Box) { + let TileDecoder { quant, dequant, residual, color_map_y, color_map_uv, pred_buf, mask, mc_tmp, levels, cdf, .. } = self; + (Buffers { quant, dequant, residual, color_map_y, color_map_uv, pred_buf, mask, mc_tmp, levels }, cdf) + } + fn seq(&self) -> &SequenceHeader { &self.fs.seq } @@ -189,8 +400,11 @@ impl<'a, 'f> TileDecoder<'a, 'f> { c >= self.mi_col_start as isize && c < self.mi_col_end as isize && r >= self.mi_row_start as isize && r < self.mi_row_end as isize } - /// decode_tile( ) + /// decode_tile( ), or its reconstruction from [`TileDecoder::record`] in [`Pass::Reconstruct`]. pub fn decode_tile(&mut self) -> Result<()> { + if self.pass == Pass::Reconstruct { + return self.reconstruct_tile(); + } // clear_above_context( ) let t = &mut *self.t; for p in 0..3 { @@ -223,7 +437,11 @@ impl<'a, 'f> TileDecoder<'a, 'f> { self.clear_cdef(r, c); self.clear_block_decoded_flags(r, c, sb4); self.read_lr(r, c, sb_size); + let before = self.record.blocks.len(); self.decode_partition(r, c, sb_size)?; + if self.pass == Pass::Parse { + self.record.sb_blocks.push((self.record.blocks.len() - before) as u32); + } c += sb4; } r += sb4; @@ -231,6 +449,76 @@ impl<'a, 'f> TileDecoder<'a, 'f> { Ok(()) } + /// The reconstruction half of decode_tile( ): the superblocks in the same order, each block + /// predicted and reconstructed from its record. + fn reconstruct_tile(&mut self) -> Result<()> { + self.reconstruct_sb_rows(usize::MAX) + } + + /// Reconstruct the next `rows` superblock rows of the tile (the record keeps the position). + pub fn reconstruct_sb_rows(&mut self, rows: usize) -> Result<()> { + let sb4 = if self.seq().use_128x128_superblock { 32 } else { 16 }; + let mut r = self.mi_row_start + self.record.next_sb_row * sb4; + let mut done = 0; + while r < self.mi_row_end && done < rows { + let mut c = self.mi_col_start; + while c < self.mi_col_end { + self.clear_block_decoded_flags(r, c, sb4); + let n = *self.record.sb_blocks.get(self.record.next_sb).ok_or(crate::av1::decode::Error::Invalid("block record exhausted"))?; + self.record.next_sb += 1; + for _ in 0..n { + self.reconstruct_block()?; + } + c += sb4; + } + r += sb4; + done += 1; + self.record.next_sb_row += 1; + } + Ok(()) + } + + fn reconstruct_block(&mut self) -> Result<()> { + let rec = self.record.blocks.get(self.record.next_block).ok_or(crate::av1::decode::Error::Invalid("block record exhausted"))?; + self.record.next_block += 1; + self.b = rec.b.clone(); + self.current_q_index = rec.current_q_index; + if let Some(w) = &rec.warp_samples { + self.mvs.num_samples = w.0; + self.mvs.cand_list = w.1; + } + if let Some(maps) = &rec.color_maps { + if let Some(d) = self.color_map_y.get_mut(..maps.0.len()) { + d.copy_from_slice(&maps.0); + } + if let Some(d) = self.color_map_uv.get_mut(..maps.1.len()) { + d.copy_from_slice(&maps.1); + } + } + self.compute_prediction()?; + self.residual() + } + + /// Records the block for [`Pass::Reconstruct`] (its variables as parsed). + fn record_block(&mut self) { + let b = &self.b; + // The rows of the color maps (stride 64) the block's palette prediction reads. + let map_rows = |n: usize, h: usize| if n > 0 { (h + 2).min(64) * 64 } else { 0 }; + let rows_y = map_rows(b.palette_size_y, block_height(b.mi_size)); + let rows_uv = map_rows(b.palette_size_uv, block_height(b.mi_size) >> self.fs.ssy); + let size = std::mem::size_of::() + + rows_y + + rows_uv + + if b.motion_mode as usize == crate::av1::decode::spec_tables::LOCALWARP { 160 } else { 0 }; + if !self.record.grow(size) { + return; + } + let warp_samples = + (b.motion_mode as usize == crate::av1::decode::spec_tables::LOCALWARP).then(|| Box::new((self.mvs.num_samples, self.mvs.cand_list))); + let color_maps = (rows_y + rows_uv > 0).then(|| Box::new((self.color_map_y[..rows_y].to_vec(), self.color_map_uv[..rows_uv].to_vec()))); + self.record.blocks.push(BlockRecord { b: b.clone(), current_q_index: self.current_q_index, warp_samples, color_maps }); + } + fn clear_cdef(&mut self, r: usize, c: usize) { let t = &mut *self.t; t.set_cdef_idx(r, c, -1); @@ -461,7 +749,11 @@ impl<'a, 'f> TileDecoder<'a, 'f> { } } } - self.compute_prediction()?; + if self.pass == Pass::Parse { + self.record_block(); + } else { + self.compute_prediction()?; + } self.residual()?; let b = &self.b; let delta_lf = self.delta_lf; diff --git a/crates/video/src/av1/decode/tile/coeffs.rs b/crates/video/src/av1/decode/tile/coeffs.rs index 6a704f8ac..df358e68d 100644 --- a/crates/video/src/av1/decode/tile/coeffs.rs +++ b/crates/video/src/av1/decode/tile/coeffs.rs @@ -459,6 +459,58 @@ impl<'a, 'f> TileDecoder<'a, 'f> { } } + /// Moves the coefficients `coeffs` left in `quant` into the record (leaving `quant` zero, as + /// reconstruction does). + pub(super) fn record_coeffs(&mut self, eob: usize, tx_sz: usize) { + let scan = self.get_scan(tx_sz); + let scan = scan.get(..eob).unwrap_or(scan); + if !self.record.grow(4 * (3 + 2 * scan.len())) { + // Over the limit: the record is dropped (the frame is decoded again in one pass); + // `quant` must still be left zero. + for &pos in scan { + if let Some(q) = self.quant.get_mut(pos as usize) { + *q = 0; + } + } + return; + } + let rec = &mut self.record.coeffs; + rec.push(eob as i32); + if eob == 0 { + return; + } + rec.push(self.plane_tx_type as i32); + let count_at = rec.len(); + rec.push(0); + let mut count = 0; + for &pos in scan { + let v = std::mem::take(&mut self.quant[pos as usize]); + if v != 0 { + rec.push(pos as i32); + rec.push(v); + count += 1; + } + } + rec[count_at] = count; + } + + /// The next transform block's coefficients from the record into `quant` (and its type); + /// returns its eob. + pub(super) fn replay_coeffs(&mut self) -> crate::av1::decode::Result { + let eob = self.record.next_coeff()?; + if eob <= 0 { + return Ok(0); + } + self.plane_tx_type = usize::try_from(self.record.next_coeff()?).map_err(|_| crate::av1::decode::Error::Invalid("coefficient record"))?; + let count = self.record.next_coeff()?; + for _ in 0..count { + let pos = self.record.next_coeff()? as usize; + let v = self.record.next_coeff()?; + *self.quant.get_mut(pos).ok_or(crate::av1::decode::Error::Invalid("coefficient record"))? = v; + } + Ok(eob as usize) + } + /// Reconstruct process (7.12.3). pub(super) fn reconstruct(&mut self, plane: usize, x: usize, y: usize, tx_sz: usize, dc_only: bool) { // dqDenom (7.12.3) is 1, 2 or 4 and the dividend is non-negative. diff --git a/crates/video/src/av1/decode/tile/predict.rs b/crates/video/src/av1/decode/tile/predict.rs index b1a53f399..244be62c6 100644 --- a/crates/video/src/av1/decode/tile/predict.rs +++ b/crates/video/src/av1/decode/tile/predict.rs @@ -213,7 +213,8 @@ impl<'a, 'f> TileDecoder<'a, 'f> { if start_x >= max_x || start_y >= max_y { return Ok(()); } - if !self.b.is_inter { + let parse_only = self.pass == super::Pass::Parse; + if !self.b.is_inter && !parse_only { let palette = if plane == 0 { self.b.palette_size_y } else { self.b.palette_size_uv }; if palette > 0 { self.predict_palette(plane, start_x, start_y, x, y, tx_sz); @@ -266,9 +267,23 @@ impl<'a, 'f> TileDecoder<'a, 'f> { } } if !self.b.skip { - let eob = self.coeffs(plane, start_x, start_y, tx_sz); - if eob > 0 { - self.reconstruct(plane, start_x, start_y, tx_sz, eob == 1); + match self.pass { + super::Pass::Full => { + let eob = self.coeffs(plane, start_x, start_y, tx_sz); + if eob > 0 { + self.reconstruct(plane, start_x, start_y, tx_sz, eob == 1); + } + } + super::Pass::Parse => { + let eob = self.coeffs(plane, start_x, start_y, tx_sz); + self.record_coeffs(eob, tx_sz); + } + super::Pass::Reconstruct => { + let eob = self.replay_coeffs()?; + if eob > 0 { + self.reconstruct(plane, start_x, start_y, tx_sz, eob == 1); + } + } } } for i in 0..step_y { diff --git a/crates/video/tests/av1_decode/fuzz_hostile.rs b/crates/video/tests/av1_decode/fuzz_hostile.rs index 05dbbe126..31cb66073 100644 --- a/crates/video/tests/av1_decode/fuzz_hostile.rs +++ b/crates/video/tests/av1_decode/fuzz_hostile.rs @@ -1,7 +1,6 @@ //! Hostile-input robustness: truncation, header-region sweeps and random mutation of small //! ffmpeg-generated AV1 streams (frame by frame) must never panic or run away. Skips without ffmpeg. -use std::panic::{AssertUnwindSafe, catch_unwind}; use std::process::{Command, Stdio}; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -20,8 +19,15 @@ impl Rng { } } -/// (name, encoder args, pix_fmt) -const SEEDS: &[(&str, &str, &str)] = &[("av1_fz_default", "-c:v libsvtav1 -g 4", "yuv420p"), ("av1_fz_10bit", "-c:v libsvtav1 -g 6", "yuv420p10le")]; +// Seeds are at least 64x64: SVT-AV1 before 3.0 (the Ubuntu runners have 2.3) refuses a source under 64 lines. +/// (name, encoder args, pix_fmt, size, decoder thread counts) +const SEEDS: &[(&str, &str, &str, &str, &[usize])] = &[ + ("av1_fz_default", "-c:v libsvtav1 -g 4", "yuv420p", "64x64", &[1, 3]), + ("av1_fz_10bit", "-c:v libsvtav1 -g 6", "yuv420p10le", "64x64", &[1, 3]), + // Several superblock rows, so corrupt references meet frames already predicting from their + // top rows (row wavefront with frame threads). + ("av1_fz_rows", "-c:v libsvtav1 -g 6", "yuv420p", "64x136", &[1, 8]), +]; fn ivf_frames(data: &[u8]) -> Vec> { let mut out = Vec::new(); @@ -37,14 +43,13 @@ fn ivf_frames(data: &[u8]) -> Vec> { out } -// Seeds are 64x64: SVT-AV1 before 3.0 (the Ubuntu runners have 2.3) refuses a source under 64 lines. -fn make(ff: &std::path::Path, name: &str, args: &str, pix: &str) -> Option>> { +fn make(ff: &std::path::Path, name: &str, args: &str, pix: &str, size: &str) -> Option>> { let out = craft_media_testkit::fixtures_dir("av1").join(format!("{name}.ivf")); let p = craft_media_testkit::fixtures::generate(&out, |tmp| { let a: Vec<&str> = args.split_whitespace().collect(); matches!( Command::new(ff) - .args(["-hide_banner", "-loglevel", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=64x64:rate=25", "-frames:v", "6", "-pix_fmt", pix]) + .args(["-hide_banner", "-loglevel", "error", "-y", "-f", "lavfi", "-i", &format!("testsrc2=size={size}:rate=25"), "-frames:v", "6", "-pix_fmt", pix]) .args(a) .args(["-f", "ivf"]) .arg(tmp) @@ -69,20 +74,29 @@ fn count_panics() { }); } -fn run_all(frames: &[Vec], label: &str) { +fn run_all(frames: &[Vec], threads: &[usize], label: &str) { craft_media_testkit::set_progress(label); count_panics(); let before = PANICS.load(Ordering::SeqCst); - let res = catch_unwind(AssertUnwindSafe(|| { - for threads in [1, 3] { - let mut dec = Decoder::with_threads(threads); - for (i, f) in frames.iter().enumerate() { - let _ = dec.decode_pts(f, i as i64); - } - let _ = dec.flush_result(); - } - })); - assert!(res.is_ok() && PANICS.load(Ordering::SeqCst) == before, "panic: {label}"); + // Every thread count decodes the stream at the same time, each on a thread of its own. + let ok = std::thread::scope(|s| { + let runs: Vec<_> = threads + .iter() + .map(|&threads| { + s.spawn(move || { + let mut dec = Decoder::with_threads(threads); + for (i, f) in frames.iter().enumerate() { + let _ = dec.decode_pts(f, i as i64); + } + let _ = dec.flush_result(); + }) + }) + .collect(); + // Join every run (no short cut), then check them all. + let joined: Vec = runs.into_iter().map(|r| r.join().is_ok()).collect(); + joined.into_iter().all(|ok| ok) + }); + assert!(ok && PANICS.load(Ordering::SeqCst) == before, "panic: {label}"); } /// Random-mutation rounds per seed (raise with FILMCRAFT_FUZZ_ITERS for a long hunt). @@ -95,17 +109,23 @@ const DEADLINE_SECS: u64 = 600; #[test] fn hostile_streams_never_panic() { - craft_media_testkit::with_deadline(DEADLINE_SECS, sweep); + craft_media_testkit::with_deadline(DEADLINE_SECS, || sweep(&SEEDS[..2])); } -fn sweep() { +/// The seed with several superblock rows, in a test of its own so it runs beside the others. +#[test] +fn hostile_streams_with_superblock_rows_never_panic() { + craft_media_testkit::with_deadline(DEADLINE_SECS, || sweep(&SEEDS[2..])); +} + +fn sweep(seeds: &[(&str, &str, &str, &str, &[usize])]) { let Some(ff) = craft_media_testkit::ffmpeg_or_skip("AV1 fuzz") else { return }; - for &(name, args, pix) in SEEDS { + for &(name, args, pix, size, threads) in seeds { if !craft_media_testkit::ffmpeg_has_encoder(&ff, "libsvtav1") { craft_media_testkit::skip_test("AV1 fuzz", &format!("this ffmpeg has no libsvtav1 encoder (seed {name})")); continue; } - let Some(seed) = make(&ff, name, args, pix) else { + let Some(seed) = make(&ff, name, args, pix, size) else { panic!("ffmpeg has libsvtav1 but could not encode the {name} seed"); }; if seed.is_empty() { @@ -118,7 +138,7 @@ fn sweep() { for n in (0..len).step_by((len / 200).max(1)) { let mut d = seed.clone(); d[fi].truncate(n); - run_all(&d, &format!("{name} frame {fi} truncate {n}")); + run_all(&d, threads, &format!("{name} frame {fi} truncate {n}")); } } // header region of every frame: every byte to a few values, every bit @@ -127,12 +147,12 @@ fn sweep() { for v in [0u8, 0xff, 0x80, 0x01, 0x7f] { let mut d = seed.clone(); d[fi][p] = v; - run_all(&d, &format!("{name} frame {fi} byte {p}={v:#x}")); + run_all(&d, threads, &format!("{name} frame {fi} byte {p}={v:#x}")); } for b in 0..8 { let mut d = seed.clone(); d[fi][p] ^= 1 << b; - run_all(&d, &format!("{name} frame {fi} flip {p}.{b}")); + run_all(&d, threads, &format!("{name} frame {fi} flip {p}.{b}")); } } } @@ -151,7 +171,7 @@ fn sweep() { d[fi][p] = rng.next() as u8; } } - run_all(&d, &format!("{name} random {i}")); + run_all(&d, threads, &format!("{name} random {i}")); } } } diff --git a/crates/video/tests/av1_decode/main.rs b/crates/video/tests/av1_decode/main.rs index 50c3939f6..76a5adfcf 100644 --- a/crates/video/tests/av1_decode/main.rs +++ b/crates/video/tests/av1_decode/main.rs @@ -9,3 +9,4 @@ mod hostile_dims; mod oracle_inter; mod oracle_intra; mod perf; +mod wavefront; diff --git a/crates/video/tests/av1_decode/wavefront.rs b/crates/video/tests/av1_decode/wavefront.rs new file mode 100644 index 000000000..725b02544 --- /dev/null +++ b/crates/video/tests/av1_decode/wavefront.rs @@ -0,0 +1,93 @@ +//! Stress tests of the frame threading (parse / reconstruct split and the row wavefront across +//! frames): every thread count gives the single-thread pictures, run after run, and dropping +//! or flushing a busy decoder never hangs. A hang fails the test through the deadline. + +use crate::common; +use common::*; +use craft_video::av1::decode::{Decoder, Picture}; + +/// A GOP with many superblock rows (the wavefront has rows to overlap), hierarchical +/// references and motion reaching across rows. +fn spec() -> Spec { + Spec::new("wavefront_640x480", "testsrc2=s=640x480:r=25,rotate=a=t*0.5:c=black", 24, "yuv420p", &["-preset", "8", "-crf", "38"]) +} + +/// Seconds a test may take before it counts as a hang (override: FILMCRAFT_HOSTILE_TIMEOUT_SECS). +const DEADLINE_SECS: u64 = 300; + +fn decode(tus: &[Vec], threads: usize) -> Vec { + let mut dec = Decoder::with_threads(threads); + let mut pics = Vec::new(); + for (i, tu) in tus.iter().enumerate() { + pics.extend(dec.decode_pts(tu, i as i64).unwrap_or_else(|e| panic!("threads {threads}, unit {i}: {e}"))); + } + pics.extend(dec.flush_result().unwrap_or_else(|e| panic!("threads {threads}, flush: {e}"))); + pics +} + +#[test] +fn every_thread_count_gives_the_single_thread_pictures_repeatedly() { + let Some(ff) = ffmpeg() else { return }; + let tus = ivf_frames(&make(&ff, &spec())); + craft_media_testkit::with_deadline(DEADLINE_SECS, move || { + let want = decode(&tus, 1); + assert_eq!(want.len(), 24); + for threads in [2, 3, 4, 5, 8, 16] { + for round in 0..4 { + assert!(decode(&tus, threads) == want, "threads {threads}, round {round}: pictures differ"); + } + } + }); +} + +/// 128x128 superblocks in two tile columns (libaom), with motion field estimation: the +/// wavefront over the superblock rows of every tile column. +fn spec_sb128_tiles() -> Spec { + let enc = ["-cpu-used", "8", "-crf", "40", "-tile-columns", "1", "-lag-in-frames", "16", "-aom-params", "sb-size=128:enable-ref-frame-mvs=1"]; + Spec::new("wavefront_sb128_tiles", "testsrc2=s=640x400:r=25,rotate=a=t*0.5:c=black", 16, "yuv420p", &enc).encoder("libaom-av1") +} + +#[test] +fn superblocks_of_128_in_tile_columns_give_the_single_thread_pictures() { + let Some(ff) = ffmpeg_libaom() else { return }; + let tus = ivf_frames(&make(&ff, &spec_sb128_tiles())); + craft_media_testkit::with_deadline(DEADLINE_SECS, move || { + let want = decode(&tus, 1); + assert_eq!(want.len(), 16); + for threads in [2, 3, 4, 8, 16] { + for round in 0..2 { + assert!(decode(&tus, threads) == want, "threads {threads}, round {round}: pictures differ"); + } + } + }); +} + +#[test] +fn dropping_or_flushing_a_busy_decoder_never_hangs() { + let Some(ff) = ffmpeg() else { return }; + let tus = ivf_frames(&make(&ff, &spec())); + craft_media_testkit::with_deadline(DEADLINE_SECS, move || { + let want = decode(&tus, 1); + for threads in [2, 3, 8, 16] { + for cut in [1, 2, 7, 12, 23] { + // Dropped with frames in flight (no flush). + let mut dec = Decoder::with_threads(threads); + for (i, tu) in tus.iter().take(cut).enumerate() { + dec.decode_pts(tu, i as i64).expect("decode"); + } + drop(dec); + // Flushed mid-stream (as before a seek), then continued: the same pictures. + let mut dec = Decoder::with_threads(threads); + let mut pics = Vec::new(); + for (i, tu) in tus.iter().enumerate() { + if i == cut { + pics.extend(dec.flush_result().expect("flush")); + } + pics.extend(dec.decode_pts(tu, i as i64).expect("decode")); + } + pics.extend(dec.flush_result().expect("flush")); + assert!(pics == want, "threads {threads}, flush before unit {cut}: pictures differ"); + } + } + }); +} From c25999c8a48ab1065f4191d992dc6b916f59a4b8 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 12 Nov 2026 21:51:12 +0100 Subject: [PATCH 0011/1159] hevc decode: dequantise while decoding residuals, table-driven sig_coeff contexts Each coefficient is scaled (8.6.3) as it is decoded and stored in a list of (index, value) instead of a separate pass over the bounding box. Only the coefficient bounding box is cleared before the list is scattered into it. DC-only blocks add the inverse-DC constant straight to the prediction. sig_coeff_flag ctxInc comes from a per-sub-block table, last_sb / last_pos from inverse scan tables. Merge candidates use the fixed five-entry list instead of a Vec. Same change as storytold/Filmcraft#3188 (residual part), ported onto craft-video. Co-Authored-By: Claude Sonnet 5.5 --- crates/video/src/hevc/decode/mvpred.rs | 33 ++- crates/video/src/hevc/decode/slicedec.rs | 32 ++- .../hevc/decode/slicedec/transform_tree.rs | 194 +++++++++--------- crates/video/src/hevc/decode/tables.rs | 79 ++++++- 4 files changed, 224 insertions(+), 114 deletions(-) diff --git a/crates/video/src/hevc/decode/mvpred.rs b/crates/video/src/hevc/decode/mvpred.rs index 962bbaa8c..9abbe33aa 100644 --- a/crates/video/src/hevc/decode/mvpred.rs +++ b/crates/video/src/hevc/decode/mvpred.rs @@ -152,38 +152,35 @@ impl<'a, S: crate::hevc::decode::picture::Sample> SliceDecoder<'a, S> { col.ref_idx[1] = 0; col_avail = true; } - let mut cands: Vec = list[..n].to_vec(); if col_avail { - cands.push(col); + list[n] = col; + n += 1; } - if merge_idx < cands.len() { - return self.finish_merge(cands[merge_idx], w0, h0); + if merge_idx < n { + return self.finish_merge(list[merge_idx], w0, h0); } // combined bi-predictive candidates (8.5.3.2.4) - let num_orig = cands.len(); + let num_orig = n; if self.sh.is_b() && num_orig > 1 && num_orig < max { let mut comb = 0; - while comb < num_orig * (num_orig - 1) && cands.len() < max { + while comb < num_orig * (num_orig - 1) && n < max { let (i0, i1) = COMB[comb]; - let (c0, c1) = (cands[i0], cands[i1]); + let (c0, c1) = (list[i0], list[i1]); if c0.pred(0) && c1.pred(1) && (self.ref_pic(0, c0.ref_idx[0]).poc != self.ref_pic(1, c1.ref_idx[1]).poc || c0.mv[0] != c1.mv[1]) { - cands.push(MvField { mv: [c0.mv[0], c1.mv[1]], ref_idx: [c0.ref_idx[0], c1.ref_idx[1]] }); + list[n] = MvField { mv: [c0.mv[0], c1.mv[1]], ref_idx: [c0.ref_idx[0], c1.ref_idx[1]] }; + n += 1; } comb += 1; } } - if merge_idx < cands.len() { - return self.finish_merge(cands[merge_idx], w0, h0); + if merge_idx < n { + return self.finish_merge(list[merge_idx], w0, h0); } - // zero candidates (8.5.3.2.5) + // zero candidates (8.5.3.2.5): candidate merge_idx is the (merge_idx - n)-th one let num_ref = if self.sh.is_b() { self.sh.num_ref_idx[0].min(self.sh.num_ref_idx[1]) } else { self.sh.num_ref_idx[0] } as usize; - let mut zero_idx = 0; - while cands.len() <= merge_idx { - let r = if zero_idx < num_ref { zero_idx as i8 } else { 0 }; - cands.push(MvField { mv: [[0; 2]; 2], ref_idx: [r, if self.sh.is_b() { r } else { -1 }] }); - zero_idx += 1; - } - self.finish_merge(cands[merge_idx], w0, h0) + let zero_idx = merge_idx - n; + let r = if zero_idx < num_ref { zero_idx as i8 } else { 0 }; + self.finish_merge(MvField { mv: [[0; 2]; 2], ref_idx: [r, if self.sh.is_b() { r } else { -1 }] }, w0, h0) } fn finish_merge(&self, mut f: MvField, w: i32, h: i32) -> MvField { diff --git a/crates/video/src/hevc/decode/slicedec.rs b/crates/video/src/hevc/decode/slicedec.rs index 102b74719..8ebaeafb1 100644 --- a/crates/video/src/hevc/decode/slicedec.rs +++ b/crates/video/src/hevc/decode/slicedec.rs @@ -16,7 +16,7 @@ use crate::hevc::decode::spec_tables::{ INTRA_CHROMA, LAST_X_PREFIX, LAST_Y_PREFIX, MERGE_FLAG, MERGE_IDX, MVP_FLAG, PART_MODE, PRED_MODE, PREV_INTRA_LUMA, REF_IDX, RQT_ROOT_CBF, SAO_MERGE, SAO_TYPE, SIG_COEFF, SPLIT_CU, SPLIT_TRANSFORM, TRANSFORM_SKIP, }; -use crate::hevc::decode::tables::{CTX_IDX_MAP, INTRA_CHROMA_MODE_422, LEVEL_SCALE, SCAN_4, qpc, scan_order}; +use crate::hevc::decode::tables::{INTRA_CHROMA_MODE_422, LEVEL_SCALE, SCAN_4, SIG_CTX, qpc, scan_order, scan_pos}; use crate::hevc::decode::transform; use std::sync::Arc; @@ -340,6 +340,8 @@ pub struct SliceDecoder<'a, S> { pub cu_part: PartMode, // scratch pub coeffs: Vec, + /// Scaled non-zero coefficients of the current transform block: (index in `coeffs`, value). + pub nz: Vec<(u16, i32)>, pub pred0: Vec, pub pred1: Vec, pub mc: crate::hevc::decode::inter::McScratch, @@ -461,6 +463,7 @@ impl<'a, S: Sample> SliceDecoder<'a, S> { intra_split: false, cu_part: PartMode::P2Nx2N, coeffs: vec![0; 32 * 32], + nz: vec![(0, 0); 32 * 32], pred0: vec![0; 64 * 64], pred1: vec![0; 64 * 64], mc: Default::default(), @@ -632,3 +635,30 @@ impl<'a, S: Sample> SliceDecoder<'a, S> { Ok(()) } } + +#[cfg(test)] +mod tests { + use crate::hevc::decode::transform; + + /// `reconstruct` clears only the coefficient bounding box, so the inverse transform must not + /// read the stale values left outside it by the previous block. + #[test] + fn inverse_transform_reads_only_the_coefficient_box() { + for &(n, dst) in &[(4usize, true), (4, false), (8, false), (16, false), (32, false)] { + for (max_x, max_y) in [(0, 1), (1, 0), (2, 3), (n / 2, 1), (n - 1, 0), (n - 1, n - 1)] { + let (mut clean, mut stale) = (vec![0i32; n * n], vec![0i32; n * n]); + for (i, (c, s)) in clean.iter_mut().zip(stale.iter_mut()).enumerate() { + let (x, y) = (i % n, i / n); + *s = 12345 - i as i32; + if x <= max_x && y <= max_y { + *c = (i as i32 * 37) % 201 - 100; + *s = *c; + } + } + transform::inverse_transform(&mut clean, n, dst, max_x, max_y, 12); + transform::inverse_transform(&mut stale, n, dst, max_x, max_y, 12); + assert_eq!(clean, stale, "n {n} dst {dst} box ({max_x}, {max_y})"); + } + } + } +} diff --git a/crates/video/src/hevc/decode/slicedec/transform_tree.rs b/crates/video/src/hevc/decode/slicedec/transform_tree.rs index 8512c341a..aa099a957 100644 --- a/crates/video/src/hevc/decode/slicedec/transform_tree.rs +++ b/crates/video/src/hevc/decode/slicedec/transform_tree.rs @@ -1,7 +1,7 @@ use super::{ - CBF_CHROMA, CBF_LUMA, CODED_SUB_BLOCK, CTX_IDX_MAP, CU_QP_DELTA_ABS, ChromaFormat, E_TU_H, E_TU_V, F_CBF, F_INTRA, GT1, GT2, LAST_X_PREFIX, - LAST_Y_PREFIX, LEVEL_SCALE, PartMode, Result, SCAN_4, SIG_COEFF, SPLIT_TRANSFORM, Sample, SliceDecoder, TRANSFORM_SKIP, ensure, for_runs, intra, - scan_order, transform, + CBF_CHROMA, CBF_LUMA, CODED_SUB_BLOCK, CU_QP_DELTA_ABS, ChromaFormat, E_TU_H, E_TU_V, F_CBF, F_INTRA, GT1, GT2, LAST_X_PREFIX, LAST_Y_PREFIX, + LEVEL_SCALE, PartMode, Result, SCAN_4, SIG_COEFF, SIG_CTX, SPLIT_TRANSFORM, Sample, SliceDecoder, TRANSFORM_SKIP, ensure, for_runs, intra, + scan_order, scan_pos, transform, }; /// Significant scan positions of one sub-block, in decoding (descending) order. @@ -44,54 +44,26 @@ struct GreaterFlags { g1ctx: u32, } -/// sigCtx increment of sig_coeff_flag (9.3.4.2.5) for the position `(xp, yp)` inside the sub-block. -fn sig_ctx_inc(p: &SigMapParams, (xp, yp): (usize, usize)) -> usize { - let (xc, yc) = (p.xs * 4 + xp, p.ys * 4 + yp); +/// sigCtx increments (9.3.4.2.5) of the 16 scan positions of one sub-block. +fn sig_ctx_table(p: &SigMapParams) -> [u8; 16] { if p.log2 == 2 { - return CTX_IDX_MAP[(yc << 2) + xc] as usize; + return SIG_CTX[p.scan_idx][4]; } - if xc + yc == 0 { - return 0; - } - let mut s = match p.prev_csbf { - 0 => { - if xp + yp == 0 { - 2 - } else if xp + yp < 3 { - 1 - } else { - 0 - } - } - 1 => { - if yp == 0 { - 2 - } else if yp == 1 { - 1 - } else { - 0 - } - } - 2 => { - if xp == 0 { - 2 - } else if xp == 1 { - 1 - } else { - 0 - } - } - _ => 2, - }; - if p.c == 0 { - if p.xs + p.ys > 0 { - s += 3; - } - s += if p.log2 == 3 { if p.scan_idx == 0 { 9 } else { 15 } } else { 21 }; + let off = if p.c == 0 { + (if p.xs + p.ys > 0 { 3 } else { 0 }) + if p.log2 == 3 { if p.scan_idx == 0 { 9 } else { 15 } } else { 21 } + } else if p.log2 == 3 { + 9 } else { - s += if p.log2 == 3 { 9 } else { 12 }; + 12 + }; + let mut t = SIG_CTX[p.scan_idx][p.prev_csbf as usize]; + for v in t.iter_mut() { + *v += off; } - s + if p.xs + p.ys == 0 { + t[0] = 0; + } + t } /// A node of the transform tree: the block at (`x0`, `y0`) of size 2^`log2`, its parent's origin @@ -333,14 +305,13 @@ impl<'a, S: Sample> SliceDecoder<'a, S> { /// sig_coeff_flag decoding of one coded sub-block, appending the significant scan positions /// (descending) to `list`. fn read_sig_map(&mut self, p: &SigMapParams, list: &mut SigList) { - let pos_scan = &SCAN_4[p.scan_idx]; let c_off_sig = if p.c == 0 { 0 } else { 27 }; + let ctx_tab = sig_ctx_table(p); let mut infer_dc = p.infer_dc; let mut np = p.start; while np >= 0 { - let (xp, yp) = (pos_scan[np as usize].0 as usize, pos_scan[np as usize].1 as usize); if np > 0 || !infer_dc { - let sig_ctx = sig_ctx_inc(p, (xp, yp)); + let sig_ctx = ctx_tab[np as usize] as usize; if self.c.decode_decision(SIG_COEFF + c_off_sig + sig_ctx) == 1 { list.push(np as u8); infer_dc = false; @@ -438,9 +409,23 @@ impl<'a, S: Sample> SliceDecoder<'a, S> { let sb_scan = scan_order(log2sb, scan_idx); let pos_scan = &SCAN_4[scan_idx]; let sb_w = 1usize << log2sb; - let last_sb = sb_scan.iter().position(|&(x, y)| x as u32 == lx >> 2 && y as u32 == ly >> 2).unwrap_or(0); - let last_pos = pos_scan.iter().position(|&(x, y)| x as u32 == lx & 3 && y as u32 == ly & 3).unwrap_or(0); - self.coeffs[..n * n].fill(0); + let last_sb = scan_pos(log2sb, scan_idx, lx >> 2, ly >> 2); + let last_pos = scan_pos(2, scan_idx, lx & 3, ly & 3); + // scaling (8.6.3), applied as each coefficient is decoded + let bd = if c == 0 { self.pic.bd_y } else { self.pic.bd_c }; + let bd_shift = bd + log2 - 5; + let round = 1i64 << (bd_shift - 1); + let scale = (LEVEL_SCALE[(qp % 6) as usize] as i64) << (qp / 6); + let matrix = match &self.pic.scaling { + Some(s) if !bypass && !(ts && n > 4) => { + let size_id = (log2 - 2) as usize; + let matrix_id = if self.cu_intra { c } else { 3 + c }; + Some((s.clone(), size_id * 6 + matrix_id)) + } + _ => None, + }; + let m: Option<&[u8]> = matrix.as_ref().map(|(s, i)| &s[*i][..]); + let mut nnz = 0; let mut csbf = [0u8; 64]; let mut prev_g1: Option = None; let sdh = self.pps.sign_data_hiding && !bypass; @@ -485,12 +470,18 @@ impl<'a, S: Sample> SliceDecoder<'a, S> { for k in 0..list.n { let np = list.pos[k] as usize; let (xc, yc) = (xs * 4 + pos_scan[np].0 as usize, ys * 4 + pos_scan[np].1 as usize); - self.coeffs[yc * n + xc] = values[k]; + let mut v = values[k]; + if !bypass { + let f = m.map_or(16, |m| m[yc * n + xc] as i64); + v = ((v as i64 * f * scale + round) >> bd_shift).clamp(-32768, 32767) as i32; + } + self.nz[nnz] = ((yc * n + xc) as u16, v); + nnz += 1; max_x = max_x.max(xc); max_y = max_y.max(yc); } } - self.reconstruct((x0, y0), log2, c, qp, ts, (max_x, max_y)) + self.reconstruct((x0, y0), log2, c, ts, nnz, (max_x, max_y)) } fn coeff_abs_level_remaining(&mut self, rice: u32) -> Result { @@ -508,55 +499,70 @@ impl<'a, S: Sample> SliceDecoder<'a, S> { } } - /// Scaling, transform and addition to the prediction. - fn reconstruct(&mut self, (x0, y0): (i32, i32), log2: u32, c: usize, qp: i32, ts: bool, (max_x, max_y): (usize, usize)) -> Result<()> { + /// Transform of the scaled coefficients `nz[..nnz]` and addition to the prediction. + fn reconstruct(&mut self, (x0, y0): (i32, i32), log2: u32, c: usize, ts: bool, nnz: usize, (max_x, max_y): (usize, usize)) -> Result<()> { let n = 1usize << log2; let bd = if c == 0 { self.pic.bd_y } else { self.pic.bd_c }; - let coeffs = &mut self.coeffs[..n * n]; - let mut dc_only = false; - if !self.cu_transquant_bypass { - // scaling (8.6.3) - let bd_shift = bd + log2 - 5; - let round = 1i64 << (bd_shift - 1); - let scale = (LEVEL_SCALE[(qp % 6) as usize] as i64) << (qp / 6); - let m: Option<&[u8]> = match &self.pic.scaling { - Some(s) if !(ts && n > 4) => { - let size_id = (log2 - 2) as usize; - let matrix_id = if self.cu_intra { c } else { 3 + c }; - Some(&s[size_id * 6 + matrix_id]) - } - _ => None, - }; - for y in 0..=max_y { - for x in 0..=max_x { - let v = coeffs[y * n + x]; - if v != 0 { - let f = m.map_or(16, |m| m[y * n + x] as i64); - coeffs[y * n + x] = ((v as i64 * f * scale + round) >> bd_shift).clamp(-32768, 32767) as i32; - } - } + let bypass = self.cu_transquant_bypass; + let dst = self.cu_intra && c == 0 && n == 4; + let (stride, max) = (if c == 0 { self.pic.width } else { self.pic.cwidth }, (1i32 << bd) - 1); + let off = y0 as usize * stride + x0 as usize; + let plane = &mut self.pic.planes[c]; + let bd_shift = 20 - bd; + if !bypass && !ts && max_x == 0 && max_y == 0 && !dst { + let v = transform::inverse_dc(self.nz[0].1, bd_shift); + match n { + 4 => add_dc::(plane, off, stride, v, max), + 8 => add_dc::(plane, off, stride, v, max), + 16 => add_dc::(plane, off, stride, v, max), + _ => add_dc::(plane, off, stride, v, max), } - let bd_shift = 20 - bd; + return Ok(()); + } + // the inverse transform reads only the bounding box of the coefficients + let coeffs = &mut self.coeffs[..n * n]; + if bypass || ts { + coeffs.fill(0); + } else { + for row in coeffs.chunks_exact_mut(n).take(max_y + 1) { + row[..=max_x].fill(0); + } + } + for &(p, v) in &self.nz[..nnz] { + coeffs[p as usize] = v; + } + if !bypass { if ts { transform::transform_skip(coeffs, n, bd_shift); - } else if max_x == 0 && max_y == 0 && !(self.cu_intra && c == 0 && n == 4) { - let v = transform::inverse_dc(coeffs[0], bd_shift); - coeffs.fill(v); - dc_only = true; } else { - let dst = self.cu_intra && c == 0 && n == 4; transform::inverse_transform(coeffs, n, dst, max_x, max_y, bd_shift); } } - let _ = dc_only; - let (stride, max) = (if c == 0 { self.pic.width } else { self.pic.cwidth }, (1i32 << bd) - 1); - let plane = &mut self.pic.planes[c]; - for y in 0..n { - let row = &mut plane[(y0 as usize + y) * stride + x0 as usize..][..n]; - for (x, s) in row.iter_mut().enumerate() { - *s = S::from_i32((s.to_i32() + coeffs[y * n + x]).clamp(0, max)); - } + match n { + 4 => add_residual::(plane, off, stride, coeffs, max), + 8 => add_residual::(plane, off, stride, coeffs, max), + 16 => add_residual::(plane, off, stride, coeffs, max), + _ => add_residual::(plane, off, stride, coeffs, max), } Ok(()) } } + +#[inline(always)] +fn add_residual(plane: &mut [S], off: usize, stride: usize, res: &[i32], max: i32) { + for (y, r) in res.as_chunks::().0.iter().take(N).enumerate() { + let row = &mut plane[off + y * stride..][..N]; + for (s, &d) in row.iter_mut().zip(r) { + *s = S::from_i32((s.to_i32() + d).max(0).min(max)); + } + } +} + +#[inline(always)] +fn add_dc(plane: &mut [S], off: usize, stride: usize, v: i32, max: i32) { + for y in 0..N { + for s in &mut plane[off + y * stride..][..N] { + *s = S::from_i32((s.to_i32() + v).max(0).min(max)); + } + } +} diff --git a/crates/video/src/hevc/decode/tables.rs b/crates/video/src/hevc/decode/tables.rs index 7e20bd99e..1c9c39c44 100644 --- a/crates/video/src/hevc/decode/tables.rs +++ b/crates/video/src/hevc/decode/tables.rs @@ -51,6 +51,72 @@ pub fn scan_order(log2: u32, scan_idx: usize) -> &'static [(u8, u8)] { } } +/// Inverse of a scan of an `N x N` block: scan position at `[y * N + x]`. +const fn inverse(scan: [(u8, u8); L], n: usize) -> [u8; L] { + let mut out = [0u8; L]; + let mut i = 0; + while i < L { + out[scan[i].1 as usize * n + scan[i].0 as usize] = i as u8; + i += 1; + } + out +} + +static INV_SCAN_1: [[u8; 1]; 3] = [[0], [0], [0]]; +static INV_SCAN_2: [[u8; 4]; 3] = [inverse(diag::<4>(2), 2), inverse(hv::<4>(2, false), 2), inverse(hv::<4>(2, true), 2)]; +static INV_SCAN_4: [[u8; 16]; 3] = [inverse(diag::<16>(4), 4), inverse(hv::<16>(4, false), 4), inverse(hv::<16>(4, true), 4)]; +static INV_SCAN_8: [[u8; 64]; 3] = [inverse(diag::<64>(8), 8), inverse(hv::<64>(8, false), 8), inverse(hv::<64>(8, true), 8)]; + +/// Position of (x, y) in ScanOrder[log2BlockSize][scanIdx] (log2BlockSize 0..=3). +#[inline] +pub fn scan_pos(log2: u32, scan_idx: usize, x: u32, y: u32) -> usize { + let i = ((y << log2) + x) as usize; + (match log2 { + 0 => INV_SCAN_1[scan_idx][i], + 1 => INV_SCAN_2[scan_idx][i], + 2 => INV_SCAN_4[scan_idx][i], + _ => INV_SCAN_8[scan_idx][i], + }) as usize +} + +/// sig_coeff_flag ctxInc (9.3.4.2.5) by scan position n inside a 4x4 sub-block, `[scanIdx][k][n]`: +/// k = prevCsbf (0..=3) gives sigCtx of blocks larger than 4x4 before the position and size +/// offsets; k = 4 gives ctxIdxMap of 4x4 blocks. +pub static SIG_CTX: [[[u8; 16]; 5]; 3] = [sig_ctx(diag::<16>(4)), sig_ctx(hv::<16>(4, false)), sig_ctx(hv::<16>(4, true))]; + +const fn sig_ctx(scan: [(u8, u8); 16]) -> [[u8; 16]; 5] { + let mut out = [[0u8; 16]; 5]; + let mut n = 0; + while n < 16 { + let (x, y) = (scan[n].0, scan[n].1); + out[0][n] = if x + y == 0 { + 2 + } else if x + y < 3 { + 1 + } else { + 0 + }; + out[1][n] = if y == 0 { + 2 + } else if y == 1 { + 1 + } else { + 0 + }; + out[2][n] = if x == 0 { + 2 + } else if x == 1 { + 1 + } else { + 0 + }; + out[3][n] = 2; + out[4][n] = CTX_IDX_MAP[(y as usize) * 4 + x as usize]; + n += 1; + } + out +} + /// Up-right diagonal scan for log2 size 2 or 3. pub fn scan_diag(log2: u32) -> &'static [(u8, u8)] { scan_order(log2, 0) @@ -81,7 +147,7 @@ pub fn qpc(qpi: i32, chroma_array_type_1: bool) -> i32 { } /// Table 9-50 ctxIdxMap for 4x4 sig_coeff_flag contexts. -pub static CTX_IDX_MAP: [u8; 16] = [0, 1, 4, 5, 2, 3, 4, 5, 6, 6, 8, 8, 7, 7, 8, 8]; +pub const CTX_IDX_MAP: [u8; 16] = [0, 1, 4, 5, 2, 3, 4, 5, 6, 6, 8, 8, 7, 7, 8, 8]; /// levelScale (8-309). pub static LEVEL_SCALE: [i32; 6] = [40, 45, 51, 57, 64, 72]; @@ -137,6 +203,17 @@ mod tests { assert_eq!(SCAN_4[1][1], (1, 0)); } + #[test] + fn scan_pos_inverts_scan_order() { + for log2 in 0..4 { + for scan_idx in 0..3 { + for (i, &(x, y)) in scan_order(log2, scan_idx).iter().enumerate() { + assert_eq!(scan_pos(log2, scan_idx, x as u32, y as u32), i); + } + } + } + } + #[test] fn dct_matrix_properties() { // rows are nearly orthogonal with norm ~ 64^2 * 32 From 45ddc4ffa6243c5509ef34c8b5260acdcc90b645 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 12 Nov 2026 22:05:44 +0100 Subject: [PATCH 0012/1159] 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 0013/1159] 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