use std::cell::Cell;
use crate::mq::{Contexts, Ctx, Mq, Raw};
pub(crate) const STYLE_BYPASS: u8 = 1;
pub(crate) const STYLE_RESET: u8 = 2;
pub(crate) const STYLE_TERMALL: u8 = 4;
pub(crate) const STYLE_CAUSAL: u8 = 8;
pub(crate) const STYLE_SEGSYM: u8 = 32;
pub(crate) const STYLE_HT: u8 = 64;
pub(crate) const MAX_PLANES: u32 = 30;
const SIG_ALL: u32 = (1 << 18) - 1;
const EACH: u32 = 0o1111;
const PI: u32 = 19;
const PI_ALL: u32 = EACH << PI;
const MU: u32 = 20;
const NEIGHBOURS: u32 = 0x1EF;
static ZC: [[Ctx; 512]; 3] = {
let mut t = [[Ctx::C0; 512]; 3];
let mut v = 0;
while v < 512 {
let h = ((v >> 3) & 1) + ((v >> 5) & 1);
let w = ((v >> 1) & 1) + ((v >> 7) & 1);
let d = (v & 1) + ((v >> 2) & 1) + ((v >> 6) & 1) + ((v >> 8) & 1);
t[0][v] = Ctx::new(zc_lh(h, w, d) as usize);
t[1][v] = Ctx::new(zc_lh(w, h, d) as usize);
t[2][v] = Ctx::new(zc_hh(h + w, d) as usize);
v += 1;
}
t
};
const fn zc_lh(h: usize, v: usize, d: usize) -> u8 {
match (h, v, d) {
(2, _, _) => 8,
(1, 1 | 2, _) => 7,
(1, _, 0) => 5,
(1, _, _) => 6,
(_, 2, _) => 4,
(_, 1, _) => 3,
(_, _, 0) => 0,
(_, _, 1) => 1,
_ => 2,
}
}
const fn zc_hh(hv: usize, d: usize) -> u8 {
match (d, hv) {
(0, 0) => 0,
(0, 1) => 1,
(0, _) => 2,
(1, 0) => 3,
(1, 1) => 4,
(1, _) => 5,
(2, 0) => 6,
(2, _) => 7,
_ => 8,
}
}
static SC: [(Ctx, u8); 256] = {
let mut t = [(Ctx::C0, 0); 256];
let mut v = 0;
while v < 256 {
let mut s = [0i32; 4];
let mut i = 0;
while i < 4 {
if (v >> (2 * i + 1)) & 1 != 0 {
s[i] = 1 - 2 * ((v as i32 >> (2 * i)) & 1);
}
i += 1;
}
let [up, left, right, down] = s;
let h = clamp1(left + right);
let w = clamp1(up + down);
let (flip, h, w) = if h < 0 || (h == 0 && w < 0) {
(1, -h, -w)
} else {
(0, h, w)
};
let ctx = if h == 0 { 9 + w } else { 12 + w };
t[v] = (Ctx::new(ctx as usize), flip);
v += 1;
}
t
};
const fn clamp1(v: i32) -> i32 {
if v > 1 {
1
} else if v < -1 {
-1
} else {
v
}
}
trait Source: Copy {
fn decision(&mut self, cx: &mut Contexts, ctx: Ctx) -> u32;
fn sign(&mut self, cx: &mut Contexts, sc: (Ctx, u8)) -> u32;
}
impl Source for Mq<'_> {
#[inline(always)]
fn decision(&mut self, cx: &mut Contexts, ctx: Ctx) -> u32 {
self.decode(cx, ctx)
}
#[inline(always)]
fn sign(&mut self, cx: &mut Contexts, sc: (Ctx, u8)) -> u32 {
self.decode(cx, sc.0) ^ u32::from(sc.1)
}
}
impl Source for Raw<'_> {
#[inline(always)]
fn decision(&mut self, _: &mut Contexts, _: Ctx) -> u32 {
self.bit()
}
#[inline(always)]
fn sign(&mut self, _: &mut Contexts, _: (Ctx, u8)) -> u32 {
self.bit()
}
}
pub(crate) struct Segment {
pub passes: u32,
pub start: usize,
pub end: usize,
}
#[derive(Default)]
pub(crate) struct T1 {
pub data: Vec<u32>,
flags: Vec<u32>,
w: usize,
h: usize,
causal: bool,
}
struct Place<'a> {
up: &'a [Cell<u32>; 3],
cur: &'a [Cell<u32>; 3],
down: &'a [Cell<u32>; 3],
samples: &'a mut [u32; 4],
causal: bool,
}
#[inline(always)]
fn set(word: &Cell<u32>, bits: u32) {
word.set(word.get() | bits);
}
impl Place<'_> {
#[inline(always)]
fn sign_entry(&self, f: u32, k: u32) -> (Ctx, u8) {
let down = if k == 3 {
(f >> 25) & 0x40
} else {
(f >> (18 + 3 * k)) & 0x40
};
let idx = ((f >> (3 * k)) & 0xAA)
| ((f >> (18 + 3 * k)) & 1)
| ((self.cur[0].get() >> (19 + 3 * k)) & 4)
| ((self.cur[2].get() >> (17 + 3 * k)) & 0x10)
| down;
SC[(idx & 255) as usize]
}
#[inline(always)]
fn newly<S: Source>(
&mut self,
src: &mut S,
cx: &mut Contexts,
f: &mut u32,
k: u32,
value: u32,
) {
let s = src.sign(cx, self.sign_entry(*f, k));
self.samples[k as usize & 3] = value | (s << 31);
*f |= (0x10 << (3 * k)) | (s << (21 + 3 * k));
set(&self.cur[0], 0x20 << (3 * k));
set(&self.cur[2], 0x08 << (3 * k));
if k == 0 && !self.causal {
set(&self.up[0], 1 << 17);
set(&self.up[1], (1 << 16) | (s << 31));
set(&self.up[2], 1 << 15);
}
if k == 3 {
set(&self.down[0], 1 << 2);
set(&self.down[1], (1 << 1) | (s << 18));
set(&self.down[2], 1);
}
}
#[inline(always)]
fn significance<S: Source>(
&mut self,
src: &mut S,
cx: &mut Contexts,
bp: u32,
zc: &[Ctx; 512],
h: u32,
) {
let mut f = self.cur[1].get();
if f & SIG_ALL == 0 {
return;
}
let side = f | (f >> 2);
let row = side | (f >> 1);
let fresh = !(f >> 4) & EACH;
let mut m = (row | (side >> 3) | (row >> 6)) & fresh;
if m == 0 {
return;
}
let value = 3 << bp;
macro_rules! at {
($k:expr) => {
if m & (1 << (3 * $k)) != 0 {
let nb = (f >> (3 * $k)) & 0x1FF;
if src.decision(cx, zc[nb as usize]) != 0 {
self.newly(src, cx, &mut f, $k, value);
m |= (8 << (3 * $k)) & fresh;
}
f |= 1 << (PI + 3 * $k);
}
};
}
at!(0);
if h > 1 {
at!(1);
}
if h > 2 {
at!(2);
}
if h > 3 {
at!(3);
}
let _ = m;
self.cur[1].set(f);
}
#[inline(always)]
fn refinement<S: Source>(&mut self, src: &mut S, cx: &mut Contexts, bp: u32) {
let mut f = self.cur[1].get();
let m = (f >> 4) & !(f >> PI) & EACH;
if m == 0 {
return;
}
let half = 1u32 << bp;
macro_rules! at {
($k:expr) => {
if m & (1 << (3 * $k)) != 0 {
let ctx = if (f >> (MU + 3 * $k)) & 1 != 0 {
Ctx::C16
} else if (f >> (3 * $k)) & NEIGHBOURS != 0 {
Ctx::C15
} else {
Ctx::C14
};
let bit = src.decision(cx, ctx);
let v = &mut self.samples[$k as usize];
*v = v
.wrapping_add(half)
.wrapping_sub((2 * half) & bit.wrapping_sub(1));
f |= 1 << (MU + 3 * $k);
}
};
}
at!(0);
at!(1);
at!(2);
at!(3);
self.cur[1].set(f);
}
#[inline(always)]
fn cleanup(&mut self, src: &mut Mq, cx: &mut Contexts, bp: u32, zc: &[Ctx; 512], h: u32) {
let mut f = self.cur[1].get();
let value = 3 << bp;
let m = if f & (SIG_ALL | PI_ALL) == 0 && h == 4 {
if src.decode(cx, Ctx::Run) == 0 {
return;
}
let mut k = src.decode(cx, Ctx::Uni) << 1;
k |= src.decode(cx, Ctx::Uni);
self.newly(src, cx, &mut f, k, value);
(0o1110 << (3 * k)) & EACH
} else {
!((f >> 4) | (f >> PI)) & EACH
};
if m != 0 {
macro_rules! at {
($k:expr) => {
if m & (1 << (3 * $k)) != 0 {
let nb = (f >> (3 * $k)) & 0x1FF;
if src.decode(cx, zc[nb as usize]) != 0 {
self.newly(src, cx, &mut f, $k, value);
}
}
};
}
at!(0);
if h > 1 {
at!(1);
}
if h > 2 {
at!(2);
}
if h > 3 {
at!(3);
}
}
self.cur[1].set(f & !PI_ALL);
}
}
impl T1 {
#[allow(clippy::too_many_arguments)]
pub fn decode(
&mut self,
w: usize,
h: usize,
planes: u32,
class: usize,
style: u8,
bytes: &[u8],
segments: &[Segment],
) {
self.w = w;
self.h = h;
self.causal = style & STYLE_CAUSAL != 0;
self.data.clear();
self.data.resize(w * h.next_multiple_of(4), 0);
self.flags.clear();
self.flags.resize((w + 2) * (h.div_ceil(4) + 2), 0);
let zc = &ZC[class];
let mut cx = Contexts::new();
let mut pass = 0u32;
let total = planes * 3 - 2;
for seg in segments {
let bytes = &bytes[seg.start..seg.end];
let mut mq = Mq::new(bytes);
let mut raw = Raw::new(bytes);
for _ in 0..seg.passes {
if pass >= total {
return;
}
let bp = planes - 1 - pass.div_ceil(3);
let is_raw = style & STYLE_BYPASS != 0 && pass >= 10;
match (pass + 2) % 3 {
0 if is_raw => self.significance(&mut raw, &mut cx, bp, zc),
0 => self.significance(&mut mq, &mut cx, bp, zc),
1 if is_raw => self.refinement(&mut raw, &mut cx, bp),
1 => self.refinement(&mut mq, &mut cx, bp),
_ => {
self.cleanup(&mut mq, &mut cx, bp, zc);
if style & STYLE_SEGSYM != 0 {
for _ in 0..4 {
mq.decode(&mut cx, Ctx::Uni);
}
}
}
}
if style & STYLE_RESET != 0 {
cx = Contexts::new();
}
pass += 1;
}
}
}
#[inline(always)]
fn columns(&mut self, mut body: impl FnMut(Place, u32)) {
let fw = self.w + 2;
let causal = self.causal;
let flags = Cell::from_mut(&mut self.flags[..]).as_slice_of_cells();
for (i, stripe) in self.data.chunks_exact_mut(4 * self.w).enumerate() {
let [up, cur, down] = [0, 1, 2].map(|j| flags[(i + j) * fw..][..fw].windows(3));
let rows = self.h - 4 * i;
let places = stripe.chunks_exact_mut(4).zip(cur).zip(up).zip(down);
let places = places.map(|(((samples, cur), up), down)| Place {
up: three(up),
cur: three(cur),
down: three(down),
samples: match samples.first_chunk_mut() {
Some(s) => s,
None => unreachable!("chunks of four"),
},
causal,
});
if rows >= 4 {
places.for_each(|p| body(p, 4));
} else {
places.for_each(|p| body(p, rows as u32));
}
}
}
fn significance<S: Source>(
&mut self,
src: &mut S,
cx: &mut Contexts,
bp: u32,
zc: &[Ctx; 512],
) {
let mut s = *src;
self.columns(
#[inline(always)]
|mut p, h| p.significance(&mut s, cx, bp, zc, h),
);
*src = s;
}
fn refinement<S: Source>(&mut self, src: &mut S, cx: &mut Contexts, bp: u32) {
let mut s = *src;
self.columns(
#[inline(always)]
|mut p, _| p.refinement(&mut s, cx, bp),
);
*src = s;
}
fn cleanup(&mut self, src: &mut Mq, cx: &mut Contexts, bp: u32, zc: &[Ctx; 512]) {
let mut s = *src;
self.columns(
#[inline(always)]
|mut p, h| p.cleanup(&mut s, cx, bp, zc, h),
);
*src = s;
}
}
#[inline(always)]
fn three(window: &[Cell<u32>]) -> &[Cell<u32>; 3] {
match window.first_chunk() {
Some(w) => w,
None => unreachable!("windows of three"),
}
}