//! Calibration for the two derived entropy-decode inputs — [`skip_mask`] and //! [`rvs_additive`] — against the ISO reference on real `test256.bin` data, for BOTH //! the luma and chroma planes. //! //! These close the last fixture dependency: with them, every input the mANS `sgm` //! decoder needs is computable from the codestream plus normative assets. The test //! therefore also checks the composition — that the derived RVS, fed through //! [`sigma_index`], reproduces the distribution indices the reference entropy coder //! actually consumed. //! //! env: WAI_JPEGAI_DERIVE_FIXTURE = derive_fixture.json //! { y|uv: { channels, h, w, i_s, gain_per_ch, grfs, //! rvs_expected, sigma_expected, mask_expected } } #![cfg(all(feature = "neural_int", feature = "legacy_int"))] use wai::jpegai_pipeline::{rvs_additive, sigma_index, skip_mask}; #[test] fn derived_inputs_match_reference() { let path = match std::env::var("WAI_JPEGAI_DERIVE_FIXTURE") { Ok(p) if std::path::Path::new(&p).exists() => p, _ => { eprintln!("skip: set WAI_JPEGAI_DERIVE_FIXTURE"); return; } }; let v: serde_json::Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap(); for plane in ["y", "uv"] { let o = &v[plane]; let g = |k: &str| o[k].as_u64().unwrap() as usize; let (channels, h, w) = (g("channels"), g("h"), g("w")); let i64v = |k: &str| o[k].as_array().unwrap().iter().map(|x| x.as_i64().unwrap()).collect::>(); let boolv = |k: &str| o[k].as_array().unwrap().iter().map(|x| x.as_bool().unwrap()).collect::>(); let i_s = i64v("i_s"); let gain_per_ch = i64v("gain_per_ch"); let grfs = boolv("grfs"); let rvs_expected = i64v("rvs_expected"); let mask_expected = boolv("mask_expected"); let sigma_expected: Vec = o["sigma_expected"].as_array().unwrap() .iter().map(|x| x.as_u64().unwrap() as u8).collect(); // 1. RVS additive let rvs = rvs_additive(&i_s, &gain_per_ch, &grfs, channels, h, w); let bad = rvs.iter().zip(&rvs_expected).filter(|(a, b)| a != b).count(); eprintln!("[{plane}] rvs_additive: {}/{} exact", rvs.len() - bad, rvs.len()); assert_eq!(bad, 0, "{plane}: RVS additive diverged from the reference"); // 2. skip mask — note it uses the scale WITHOUT the RVS additive let mask = skip_mask(&i_s, &gain_per_ch, channels); let bad = mask.iter().zip(&mask_expected).filter(|(a, b)| a != b).count(); eprintln!("[{plane}] skip_mask: {}/{} exact ({} coded)", mask.len() - bad, mask.len(), mask.iter().filter(|&&b| b).count()); assert_eq!(bad, 0, "{plane}: skip mask diverged from the reference"); // 3. composition — derived RVS must reproduce the distribution indices the // reference entropy coder consumed let sigma = sigma_index(&i_s, &gain_per_ch, &rvs, channels); let bad = sigma.iter().zip(&sigma_expected).filter(|(a, b)| a != b).count(); eprintln!("[{plane}] sigma_index: {}/{} exact (from derived rvs)", sigma.len() - bad, sigma.len()); assert_eq!(bad, 0, "{plane}: sigma indices diverged when driven by derived RVS"); } }