EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust
M13.4: av1enc key and inter frames, bit-exact with libdav1d
Brandon Thomas4130172bbe1f
diff --git a/crates/av1enc/README.md b/crates/av1enc/README.mdnew file mode 100644index 000000000..f07567666--- /dev/null+++ b/crates/av1enc/README.md@@ -0,0 +1,67 @@+# effectcraft-av1enc++Clean-room, pure-Rust AV1 encoder for EffectCraft's MP4 / WebM export (layer L0, `std` only,+no `unsafe`, builds for `wasm32-unknown-unknown`).++Output is decoded **bit-exactly** by libdav1d (`ffmpeg -c:v libdav1d`, checked in+`tests/ffmpeg_oracle.rs` when ffmpeg is installed) and by FilmCraft's spec-derived+`filmcraft-av1` decoder (`tests/roundtrip.rs`): every decoded frame equals the encoder's own+reconstruction, so prediction never drifts.++## What is implemented++- **Stream**: Main profile (`seq_profile` 0), 4:2:0, 8 or 10 bit, BT.709 colour description,+ limited or full range. Sequence header with one operating point; `seq_level_idx` given or the+ lowest Annex A level whose MaxPicSize / MaxH/VSize / MaxDisplayRate (and, in bitrate mode, Main+ tier MaxBitrate) fit. Order hints, film grain, superres, CDEF, loop restoration, intra edge+ filter, filter intra, inter-intra, masked / distance compound, warped motion and dual filter+ are disabled in the sequence header.+- **Packets**: one temporal unit per frame as stored in MP4 / Matroska samples: OBUs with size+ fields, no temporal delimiter; key frames start with the sequence header OBU, followed by one+ `OBU_FRAME` (frame header + tile group). Every frame is shown, in input order.+ `Encoder::av1c()` returns the `AV1CodecConfigurationRecord` (also the WebM `CodecPrivate`).+- **Frames**: a `KEY_FRAME` every `keyint` frames, `INTER_FRAME`s otherwise. Inter frames use+ slot 0 (the previous reconstruction) for all seven references and refresh slot 0 only, so+ `LAST_FRAME` is always the previous frame. `primary_ref_frame = PRIMARY_REF_NONE` (default+ CDFs every frame), symbol-level CDF adaptation on, frame-end CDF update off. One tile, loop+ filter level 0, `TX_MODE_LARGEST`, `reduced_tx_set`, no segmentation, no delta q / lf.+- **Tiles**: the multi-symbol arithmetic encoder is the exact inverse of the spec's symbol+ decoder (8.2), including the exit process padding; CDF adaptation as in 8.2.6.+- **Partitions**: 64x64 superblocks, `PARTITION_NONE` / `PARTITION_SPLIT` down to 8x8 chosen by+ a rate-distortion cost (SSE + λ·estimated bits); blocks crossing the frame edge are split as+ the syntax requires. 64x64 blocks are only used as inter skip blocks.+- **Intra**: DC, V, H, Paeth, smooth, smooth-V, smooth-H for luma and chroma (chosen by SAD),+ with the spec's edge preparation and above-right / below-left availability.+- **Inter**: single reference (`LAST_FRAME`), `NEWMV` / `NEARESTMV` / `NEARMV` / `GLOBALMV`+ chosen from the spec's motion vector prediction stack (7.10.2, mirrored exactly), full-sample+ motion search (candidates + diamond search), the normative 8-tap `EIGHTTAP` prediction for+ luma and (half-sample) chroma, skip blocks, per-block intra / inter choice.+- **Residual**: one transform per plane and block (4x4 ... 32x32): `DCT_DCT` for luma, the+ mode-dependent `DCT` / `ADST` combination for intra chroma; dead-zone quantisation with the+ spec's `Dc_Qlookup` / `Ac_Qlookup`; the normative dequantisation and inverse transforms in the+ reconstruction loop; full coefficient coding (all_zero, tx type, eob_pt, eob_extra,+ coeff_base_eob, coeff_base, coeff_br, dc_sign, Golomb) with the exact contexts.+- **Rate control**: `ConstantQ(base_q_idx)`; `Bitrate { kbps }` adapts `base_q_idx` per frame+ towards the average target (key frames get a lower quantizer and a larger share; the first+ key frame is searched in a few passes).++## Limits++- One tile: width and height up to 4096 and at most 2304 superblocks (4096x2304 samples).+- No in-loop filters (deblocking, CDEF, loop restoration), so compression at low rates is below+ mature encoders; no sub-sample motion search, no compound prediction, no 4xN blocks.+- 4:2:0 only (Main profile), no 12 bit.++## Specification and provenance++Implemented from the **AV1 Bitstream & Decoding Process Specification, version 1.0.0 with+Errata 1** (AOMediaCodec, 2019; the edition FilmCraft's decoder follows) and the **AV1 Codec+ISO Media File Format Binding** (v1.2.0) for `av1C`.++The constant tables (default CDFs, scan orders, quantizer lookup tables, interpolation filter+taps, smooth weights, block / transform size tables, ...) and the decoder-side processes the+encoder must reproduce exactly (inverse transforms, intra and inter prediction rounding, context+derivations, motion vector prediction) are mirrored from FilmCraft's first-party, spec-derived+`filmcraft-av1` decoder (MIT OR Apache-2.0), which is also the round-trip oracle. No source of+libaom, dav1d, rav1e, SVT-AV1, ffmpeg or any other third-party encoder or decoder was consulted;+ffmpeg / libdav1d is used only as an external test oracle.diff --git a/crates/av1enc/src/bitw.rs b/crates/av1enc/src/bitw.rsnew file mode 100644index 000000000..e4c8a1e69--- /dev/null+++ b/crates/av1enc/src/bitw.rs@@ -0,0 +1,87 @@+//! MSB-first bit writer for the OBU headers (spec 4.10 `f(n)` descriptors) and `leb128()`.++#[derive(Default)]+pub(crate) struct BitWriter {+ pub buf: Vec<u8>,+ nbits: usize,+}++impl BitWriter {+ pub fn new() -> Self {+ BitWriter::default()+ }++ /// `f(n)`: writes the low `n` bits of `v`, most significant first.+ pub fn f(&mut self, n: u32, v: u32) {+ for i in (0..n).rev() {+ self.bit((v >> i) & 1 != 0);+ }+ }++ pub fn bit(&mut self, b: bool) {+ if self.nbits.is_multiple_of(8) {+ self.buf.push(0);+ }+ if b {+ let last = self.buf.len() - 1;+ self.buf[last] |= 0x80 >> (self.nbits % 8);+ }+ self.nbits += 1;+ }++ /// `byte_alignment()`: zero bits up to the next byte boundary.+ pub fn byte_align(&mut self) {+ while !self.nbits.is_multiple_of(8) {+ self.bit(false);+ }+ }++ /// `trailing_bits()`: a one bit followed by zero bits up to the byte boundary.+ pub fn trailing_bits(&mut self) {+ self.bit(true);+ self.byte_align();+ }++ pub fn into_bytes(self) -> Vec<u8> {+ self.buf+ }+}++/// `leb128()` encoding of an OBU size.+pub(crate) fn leb128(mut v: usize, out: &mut Vec<u8>) {+ loop {+ let mut b = (v & 0x7f) as u8;+ v >>= 7;+ if v != 0 {+ b |= 0x80;+ }+ out.push(b);+ if v == 0 {+ break;+ }+ }+}++/// An OBU with `obu_has_size_field` = 1 and no extension header.+pub(crate) fn obu(obu_type: u8, payload: &[u8], out: &mut Vec<u8>) {+ out.push((obu_type << 3) | 0x02);+ leb128(payload.len(), out);+ out.extend_from_slice(payload);+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn bits_and_leb128() {+ let mut w = BitWriter::new();+ w.f(3, 0b101);+ w.f(6, 0b110011);+ w.trailing_bits();+ assert_eq!(w.into_bytes(), vec![0b1011_1001, 0b1100_0000]);+ let mut v = Vec::new();+ leb128(300, &mut v);+ assert_eq!(v, vec![0xAC, 0x02]);+ }+}diff --git a/crates/av1enc/src/cdf.rs b/crates/av1enc/src/cdf.rsnew file mode 100644index 000000000..8540e60a8--- /dev/null+++ b/crates/av1enc/src/cdf.rs@@ -0,0 +1,97 @@+//! The adaptive CDFs this encoder codes with (the subset of spec 6.8.2 init_non_coeff_cdfs /+//! init_coeff_cdfs that its syntax uses). Layout as in filmcraft-av1: each innermost array+//! holds N cumulative values (the last is 32768) plus the adaptation counter.++use crate::tables::*;++#[derive(Clone)]+pub(crate) struct Cdfs {+ pub y_mode: [[u16; 14]; 4],+ pub uv_mode_cfl_not_allowed: [[u16; 14]; 13],+ pub uv_mode_cfl_allowed: [[u16; 15]; 13],+ pub angle_delta: [[u16; 8]; 8],+ pub partition_w8: [[u16; 5]; 4],+ pub partition_w16: [[u16; 11]; 4],+ pub partition_w32: [[u16; 11]; 4],+ pub partition_w64: [[u16; 11]; 4],+ pub skip: [[u16; 3]; 3],+ pub is_inter: [[u16; 3]; 4],+ pub single_ref: [[[u16; 3]; 6]; 3],+ pub new_mv: [[u16; 3]; 6],+ pub zero_mv: [[u16; 3]; 2],+ pub ref_mv: [[u16; 3]; 6],+ pub drl_mode: [[u16; 3]; 3],+ pub mv_joint: [u16; 5],+ pub mv_class: [[u16; 12]; 2],+ pub mv_class0_bit: [[u16; 3]; 2],+ pub mv_fr: [[u16; 5]; 2],+ pub mv_class0_fr: [[[u16; 5]; 2]; 2],+ pub mv_sign: [[u16; 3]; 2],+ pub mv_bit: [[[u16; 3]; 10]; 2],+ pub intra_tx_type_set2: [[[u16; 6]; 13]; 3],+ pub inter_tx_type_set3: [[u16; 3]; 4],+ pub txb_skip: [[[u16; 3]; 13]; 5],+ pub eob_pt_16: [[[u16; 6]; 2]; 2],+ pub eob_pt_32: [[[u16; 7]; 2]; 2],+ pub eob_pt_64: [[[u16; 8]; 2]; 2],+ pub eob_pt_128: [[[u16; 9]; 2]; 2],+ pub eob_pt_256: [[[u16; 10]; 2]; 2],+ pub eob_pt_512: [[u16; 11]; 2],+ pub eob_pt_1024: [[u16; 12]; 2],+ pub eob_extra: [[[[u16; 3]; 9]; 2]; 5],+ pub dc_sign: [[[u16; 3]; 3]; 2],+ pub coeff_base_eob: [[[[u16; 4]; 4]; 2]; 5],+ pub coeff_base: [[[[u16; 5]; 42]; 2]; 5],+ pub coeff_br: [[[[u16; 5]; 21]; 2]; 5],+}++impl Cdfs {+ /// init_non_coeff_cdfs( ) followed by init_coeff_cdfs( ) for `base_q_idx`.+ pub fn new(base_q_idx: u32) -> Box<Cdfs> {+ let idx = match base_q_idx {+ 0..=20 => 0,+ 21..=60 => 1,+ 61..=120 => 2,+ _ => 3,+ };+ Box::new(Cdfs {+ y_mode: DEFAULT_Y_MODE_CDF,+ uv_mode_cfl_not_allowed: DEFAULT_UV_MODE_CFL_NOT_ALLOWED_CDF,+ uv_mode_cfl_allowed: DEFAULT_UV_MODE_CFL_ALLOWED_CDF,+ angle_delta: DEFAULT_ANGLE_DELTA_CDF,+ partition_w8: DEFAULT_PARTITION_W8_CDF,+ partition_w16: DEFAULT_PARTITION_W16_CDF,+ partition_w32: DEFAULT_PARTITION_W32_CDF,+ partition_w64: DEFAULT_PARTITION_W64_CDF,+ skip: DEFAULT_SKIP_CDF,+ is_inter: DEFAULT_IS_INTER_CDF,+ single_ref: DEFAULT_SINGLE_REF_CDF,+ new_mv: DEFAULT_NEW_MV_CDF,+ zero_mv: DEFAULT_ZERO_MV_CDF,+ ref_mv: DEFAULT_REF_MV_CDF,+ drl_mode: DEFAULT_DRL_MODE_CDF,+ mv_joint: DEFAULT_MV_JOINT_CDF,+ mv_class: DEFAULT_MV_CLASS_CDF,+ mv_class0_bit: [DEFAULT_MV_CLASS0_BIT_CDF; 2],+ mv_fr: DEFAULT_MV_FR_CDF,+ mv_class0_fr: DEFAULT_MV_CLASS0_FR_CDF,+ mv_sign: [DEFAULT_MV_SIGN_CDF; 2],+ mv_bit: [DEFAULT_MV_BIT_CDF; 2],+ intra_tx_type_set2: DEFAULT_INTRA_TX_TYPE_SET2_CDF,+ inter_tx_type_set3: DEFAULT_INTER_TX_TYPE_SET3_CDF,+ txb_skip: DEFAULT_TXB_SKIP_CDF[idx],+ eob_pt_16: DEFAULT_EOB_PT_16_CDF[idx],+ eob_pt_32: DEFAULT_EOB_PT_32_CDF[idx],+ eob_pt_64: DEFAULT_EOB_PT_64_CDF[idx],+ eob_pt_128: DEFAULT_EOB_PT_128_CDF[idx],+ eob_pt_256: DEFAULT_EOB_PT_256_CDF[idx],+ eob_pt_512: DEFAULT_EOB_PT_512_CDF[idx],+ eob_pt_1024: DEFAULT_EOB_PT_1024_CDF[idx],+ eob_extra: DEFAULT_EOB_EXTRA_CDF[idx],+ dc_sign: DEFAULT_DC_SIGN_CDF[idx],+ coeff_base_eob: DEFAULT_COEFF_BASE_EOB_CDF[idx],+ coeff_base: DEFAULT_COEFF_BASE_CDF[idx],+ coeff_br: DEFAULT_COEFF_BR_CDF[idx],+ })+ }+}diff --git a/crates/av1enc/src/decide.rs b/crates/av1enc/src/decide.rsnew file mode 100644index 000000000..e1697c319--- /dev/null+++ b/crates/av1enc/src/decide.rs@@ -0,0 +1,677 @@+//! Mode decision and reconstruction (not normative): partitions, intra modes, motion vectors,+//! transform coefficients. Every prediction and reconstruction step uses the normative+//! processes, so the reconstruction built here is exactly what a decoder produces; the syntax+//! is written afterwards by `writer.rs`.++use crate::pred::*;+use crate::tables::*;+use crate::transform::{forward_2d, inverse_2d};++/// Decisions for one coded block.+#[derive(Clone)]+pub(crate) struct Leaf {+ pub bsize: usize,+ pub is_inter: bool,+ pub y_mode: usize,+ pub uv_mode: usize,+ pub tx_type_y: usize,+ /// Motion vector [row, col] in 1/8 luma samples (inter blocks).+ pub mv: [i32; 2],+ pub skip: bool,+ /// Quantised coefficients of the single transform block of each plane (raster order).+ pub coefs: [Vec<i32>; 3],+}++pub(crate) enum Node {+ Out,+ Leaf(Box<Leaf>),+ Split(Box<[Node; 4]>),+}++/// A reconstructed frame kept as a reference.+pub(crate) struct RefFrame {+ pub planes: [Plane; 3],+ /// Motion vectors chosen for the frame (per 4x4), used as search candidates.+ pub mvs: Vec<[i32; 2]>,+}++pub(crate) struct FrameGeom {+ pub width: usize,+ pub height: usize,+ pub mi_cols: usize,+ pub mi_rows: usize,+ pub sb_cols: usize,+ pub sb_rows: usize,+}++impl FrameGeom {+ pub fn new(width: usize, height: usize) -> FrameGeom {+ let mi_cols = 2 * width.div_ceil(8);+ let mi_rows = 2 * height.div_ceil(8);+ FrameGeom { width, height, mi_cols, mi_rows, sb_cols: mi_cols.div_ceil(16), sb_rows: mi_rows.div_ceil(16) }+ }+ /// Padded plane size (whole superblocks).+ pub fn padded(&self, plane: usize) -> (usize, usize) {+ let s = (plane > 0) as usize;+ ((self.sb_cols * 64) >> s, (self.sb_rows * 64) >> s)+ }+ /// Visible plane size.+ pub fn visible(&self, plane: usize) -> (usize, usize) {+ if plane == 0 { (self.width, self.height) } else { (self.width.div_ceil(2), self.height.div_ceil(2)) }+ }+}++pub(crate) fn tx_size_for(log2: u32) -> usize {+ match log2 {+ 2 => TX_4X4,+ 3 => TX_8X8,+ 4 => TX_16X16,+ 5 => TX_32X32,+ _ => TX_64X64,+ }+}++/// get_tx_set( ) for the square sizes used here with reduced_tx_set = 1.+pub(crate) fn tx_set(tx_sz: usize, is_inter: bool) -> usize {+ let sqr_up = TX_SIZE_SQR_UP[tx_sz] as usize;+ if sqr_up > TX_32X32 {+ return TX_SET_DCTONLY;+ }+ if is_inter {+ TX_SET_INTER_3+ } else if sqr_up == TX_32X32 {+ TX_SET_DCTONLY+ } else {+ TX_SET_INTRA_2+ }+}++pub(crate) fn tx_in_set(set: usize, is_inter: bool, tx_type: usize) -> bool {+ if is_inter { TX_TYPE_IN_SET_INTER[set][tx_type] == 1 } else { TX_TYPE_IN_SET_INTRA[set][tx_type] == 1 }+}++/// The chroma transform type of an intra block (compute_tx_type for plane > 0).+pub(crate) fn intra_uv_tx_type(uv_mode: usize, uv_tx: usize) -> usize {+ let t = MODE_TO_TXFM[uv_mode] as usize;+ if tx_in_set(tx_set(uv_tx, false), false, t) { t } else { DCT_DCT }+}++struct TxResult {+ levels: Vec<i32>,+ nonzero: bool,+ dist: f64,+ bits: f64,+}++/// Saved encoder state of a block area (reconstruction, BlockDecoded flags).+struct Snapshot {+ planes: [Vec<u16>; 3],+ decoded: Box<[[[bool; 35]; 35]; 3]>,+}++pub(crate) struct Decider<'a> {+ pub g: &'a FrameGeom,+ pub bd: u32,+ pub intra_only: bool,+ pub src: &'a [Plane; 3],+ pub rec: [Plane; 3],+ pub refr: Option<&'a RefFrame>,+ decoded: Box<[[[bool; 35]; 35]; 3]>,+ /// Decision motion vectors per 4x4 (search candidates).+ pub mvs: Vec<[i32; 2]>,+ dc_q: f32,+ ac_q: f32,+ dc_qi: i64,+ ac_qi: i64,+ lambda: f64,+ lambda_sad: f64,+ sb_r: usize,+ sb_c: usize,+}++const MODE_BITS_INTRA: f64 = 3.0;++impl<'a> Decider<'a> {+ pub fn new(g: &'a FrameGeom, bd: u32, qidx: u8, intra_only: bool, src: &'a [Plane; 3], refr: Option<&'a RefFrame>) -> Self {+ let bdi = ((bd - 8) >> 1) as usize;+ let dc_qi = DC_QLOOKUP[bdi][qidx as usize] as i64;+ let ac_qi = AC_QLOOKUP[bdi][qidx as usize] as i64;+ // Lagrangian multiplier in squared-sample units of the coded bit depth.+ let step = ac_qi as f64 / 8.0;+ let lambda = 0.22 * step * step;+ let rec = std::array::from_fn(|p| {+ let (w, h) = g.padded(p);+ Plane::new(w, h)+ });+ Decider {+ g,+ bd,+ intra_only,+ src,+ rec,+ refr,+ decoded: Box::new([[[false; 35]; 35]; 3]),+ mvs: vec![[0, 0]; g.mi_cols * g.mi_rows],+ dc_q: dc_qi as f32,+ ac_q: ac_qi as f32,+ dc_qi,+ ac_qi,+ lambda,+ lambda_sad: lambda.sqrt(),+ sb_r: 0,+ sb_c: 0,+ }+ }++ /// Decides one 64x64 superblock (reconstruction is written to `rec`).+ pub fn superblock(&mut self, r: usize, c: usize) -> Node {+ self.sb_r = r;+ self.sb_c = c;+ // clear_block_decoded_flags( r, c, sbSize4 )+ for plane in 0..3 {+ let s = (plane > 0) as usize;+ let sb_w4 = (self.g.mi_cols as isize - c as isize) >> s;+ let sb_h4 = (self.g.mi_rows as isize - r as isize) >> s;+ let bd = &mut self.decoded[plane];+ for y in -1..=((16 >> s) as isize) {+ for x in -1..=((16 >> s) as isize) {+ let v = if y < 0 && x < sb_w4 { true } else { x < 0 && y < sb_h4 };+ bd[(y + 1) as usize][(x + 1) as usize] = v;+ }+ }+ bd[(16 >> s) + 1][0] = false;+ }+ self.partition(r, c, BLOCK_64X64, None).0+ }++ fn decoded_at(&self, plane: usize, y: isize, x: isize) -> bool {+ let (yy, xx) = (y + 1, x + 1);+ if yy < 0 || xx < 0 || yy > 34 || xx > 34 {+ return false;+ }+ self.decoded[plane][yy as usize][xx as usize]+ }++ /// Marks the transform block of `plane` covering the block at (r, c) as decoded.+ fn mark_decoded(&mut self, plane: usize, r: usize, c: usize, n4: usize) {+ let s = (plane > 0) as usize;+ let by = (r & 15) >> s;+ let bx = (c & 15) >> s;+ let step = (n4 >> s).max(1);+ for i in 0..step {+ for j in 0..step {+ if by + i + 1 < 35 && bx + j + 1 < 35 {+ self.decoded[plane][by + i + 1][bx + j + 1] = true;+ }+ }+ }+ }++ fn snapshot(&self, r: usize, c: usize, n4: usize) -> Snapshot {+ let planes = std::array::from_fn(|p| {+ let s = (p > 0) as usize;+ let (x0, y0, n) = ((c * 4) >> s, (r * 4) >> s, (n4 * 4) >> s);+ let pl = &self.rec[p];+ let mut v = Vec::with_capacity(n * n);+ for y in y0..y0 + n {+ v.extend_from_slice(&pl.data[y * pl.stride + x0..y * pl.stride + x0 + n]);+ }+ v+ });+ Snapshot { planes, decoded: self.decoded.clone() }+ }++ fn restore(&mut self, snap: &Snapshot, r: usize, c: usize, n4: usize) {+ for p in 0..3 {+ let s = (p > 0) as usize;+ let (x0, y0, n) = ((c * 4) >> s, (r * 4) >> s, (n4 * 4) >> s);+ let pl = &mut self.rec[p];+ for (i, y) in (y0..y0 + n).enumerate() {+ pl.data[y * pl.stride + x0..y * pl.stride + x0 + n].copy_from_slice(&snap.planes[p][i * n..i * n + n]);+ }+ }+ self.decoded.clone_from(&snap.decoded);+ }++ fn set_mvs(&mut self, r: usize, c: usize, n4: usize, mv: [i32; 2]) {+ for y in r..(r + n4).min(self.g.mi_rows) {+ for x in c..(c + n4).min(self.g.mi_cols) {+ self.mvs[y * self.g.mi_cols + x] = mv;+ }+ }+ }++ fn partition(&mut self, r: usize, c: usize, bsize: usize, parent_mv: Option<[i32; 2]>) -> (Node, f64) {+ let g = self.g;+ if r >= g.mi_rows || c >= g.mi_cols {+ return (Node::Out, 0.0);+ }+ let n4 = NUM_4X4_BLOCKS_WIDE[bsize] as usize;+ let half = n4 >> 1;+ let has_rows = r + half < g.mi_rows;+ let has_cols = c + half < g.mi_cols;+ let can_none = has_rows && has_cols && (bsize != BLOCK_64X64 || !self.intra_only);+ let can_split = bsize > BLOCK_8X8;+ let sub = PARTITION_SUBSIZE[PARTITION_SPLIT][bsize] as usize;+ if !can_none {+ return self.split(r, c, sub, half, parent_mv);+ }+ let before = self.snapshot(r, c, n4);+ let (leaf, cost_none) = self.leaf(r, c, bsize, parent_mv);+ let cost_none = cost_none + self.lambda * 1.0;+ if !can_split {+ return (Node::Leaf(Box::new(leaf)), cost_none);+ }+ // a cheap skip block rarely gains from splitting+ if leaf.skip && cost_none < self.lambda * 8.0 {+ return (Node::Leaf(Box::new(leaf)), cost_none);+ }+ let after_none = self.snapshot(r, c, n4);+ self.restore(&before, r, c, n4);+ let pm = if leaf.is_inter { Some(leaf.mv) } else { parent_mv };+ let (node, cost_split) = self.split(r, c, sub, half, pm);+ let cost_split = cost_split + self.lambda * 2.0;+ if cost_split < cost_none {+ (node, cost_split)+ } else {+ self.restore(&after_none, r, c, n4);+ self.set_mvs(r, c, n4, if leaf.is_inter { leaf.mv } else { [0, 0] });+ (Node::Leaf(Box::new(leaf)), cost_none)+ }+ }++ fn split(&mut self, r: usize, c: usize, sub: usize, half: usize, pm: Option<[i32; 2]>) -> (Node, f64) {+ let (n0, c0) = self.partition(r, c, sub, pm);+ let (n1, c1) = self.partition(r, c + half, sub, pm);+ let (n2, c2) = self.partition(r + half, c, sub, pm);+ let (n3, c3) = self.partition(r + half, c + half, sub, pm);+ (Node::Split(Box::new([n0, n1, n2, n3])), c0 + c1 + c2 + c3)+ }++ /// Best coding of the block as a single (NONE partition) block.+ fn leaf(&mut self, r: usize, c: usize, bsize: usize, parent_mv: Option<[i32; 2]>) -> (Leaf, f64) {+ let n4 = NUM_4X4_BLOCKS_WIDE[bsize] as usize;+ if self.intra_only {+ let res = self.intra_leaf(r, c, bsize);+ self.set_mvs(r, c, n4, [0, 0]);+ return res;+ }+ let before = self.snapshot(r, c, n4);+ let (inter, cost_inter) = self.inter_leaf(r, c, bsize, parent_mv);+ if bsize == BLOCK_64X64 || inter.skip {+ self.set_mvs(r, c, n4, inter.mv);+ return (inter, cost_inter);+ }+ let after_inter = self.snapshot(r, c, n4);+ self.restore(&before, r, c, n4);+ let (intra, cost_intra) = self.intra_leaf(r, c, bsize);+ if cost_intra < cost_inter {+ self.set_mvs(r, c, n4, [0, 0]);+ (intra, cost_intra)+ } else {+ self.restore(&after_inter, r, c, n4);+ self.set_mvs(r, c, n4, inter.mv);+ (inter, cost_inter)+ }+ }++ fn intra_leaf(&mut self, r: usize, c: usize, bsize: usize) -> (Leaf, f64) {+ let n4 = NUM_4X4_BLOCKS_WIDE[bsize] as usize;+ let log2 = (n4 * 4).trailing_zeros();+ let (sbr, sbc) = ((r & 15) as isize, (c & 15) as isize);+ let bd = self.bd;+ let mut pred = vec![0u16; 32 * 32];+ let mut best_pred = vec![0u16; 32 * 32];+ let n = 1usize << log2;+ // luma+ let e = IntraEdge {+ x: c * 4,+ y: r * 4,+ have_left: c > 0,+ have_above: r > 0,+ have_above_right: self.decoded_at(0, sbr - 1, sbc + n4 as isize),+ have_below_left: self.decoded_at(0, sbr + n4 as isize, sbc - 1),+ log2,+ max_x: (self.g.mi_cols * 4) as i32 - 1,+ max_y: (self.g.mi_rows * 4) as i32 - 1,+ bit_depth: bd,+ };+ let ed = edges(&self.rec[0], &e);+ let mut best = (f64::MAX, DC_PRED);+ for &mode in INTRA_MODES_USED.iter() {+ predict_intra(&ed, &e, mode, &mut pred);+ let sad = self.sad(0, c * 4, r * 4, n, &pred[..n * n]) as f64;+ let cost = sad + self.lambda_sad * if mode == DC_PRED { 1.0 } else { MODE_BITS_INTRA };+ if cost < best.0 {+ best = (cost, mode);+ best_pred[..n * n].copy_from_slice(&pred[..n * n]);+ }+ }+ let y_mode = best.1;+ let tx_y = tx_size_for(log2);+ let ty = self.code_tx(0, c * 4, r * 4, log2, DCT_DCT, &best_pred[..n * n], true);+ self.mark_decoded(0, r, c, n4);+ // chroma (one mode for both planes)+ let cn = n >> 1;+ let clog2 = log2 - 1;+ let (csbr, csbc) = (sbr >> 1, sbc >> 1);+ let ce: [IntraEdge; 2] = std::array::from_fn(|i| IntraEdge {+ x: c * 2,+ y: r * 2,+ have_left: c > 0,+ have_above: r > 0,+ have_above_right: self.decoded_at(1 + i, csbr - 1, csbc + (n4 >> 1) as isize),+ have_below_left: self.decoded_at(1 + i, csbr + (n4 >> 1) as isize, csbc - 1),+ log2: clog2,+ max_x: (self.g.mi_cols * 2) as i32 - 1,+ max_y: (self.g.mi_rows * 2) as i32 - 1,+ bit_depth: bd,+ });+ let ced = [edges(&self.rec[1], &ce[0]), edges(&self.rec[2], &ce[1])];+ let mut best_uv = (f64::MAX, DC_PRED);+ for &mode in INTRA_MODES_USED.iter() {+ let mut sad = 0u64;+ for i in 0..2 {+ predict_intra(&ced[i], &ce[i], mode, &mut pred);+ sad += self.sad(1 + i, c * 2, r * 2, cn, &pred[..cn * cn]);+ }+ let cost = sad as f64 + self.lambda_sad * if mode == DC_PRED { 1.0 } else { 2.5 };+ if cost < best_uv.0 {+ best_uv = (cost, mode);+ }+ }+ let uv_mode = best_uv.1;+ let uv_tx = tx_size_for(clog2);+ let uv_type = intra_uv_tx_type(uv_mode, uv_tx);+ let mut tuv = Vec::with_capacity(2);+ for i in 0..2 {+ predict_intra(&ced[i], &ce[i], uv_mode, &mut pred);+ tuv.push(self.code_tx(1 + i, c * 2, r * 2, clog2, uv_type, &pred[..cn * cn], true));+ self.mark_decoded(1 + i, r, c, n4);+ }+ let _ = tx_y;+ let skip = !ty.nonzero && !tuv[0].nonzero && !tuv[1].nonzero;+ let dist = ty.dist + tuv[0].dist + tuv[1].dist;+ let bits = if skip { 1.0 } else { ty.bits + tuv[0].bits + tuv[1].bits + 1.0 } + MODE_BITS_INTRA + 2.0;+ let [u, v]: [TxResult; 2] = tuv.try_into().ok().expect("two chroma planes");+ let leaf = Leaf { bsize, is_inter: false, y_mode, uv_mode, tx_type_y: DCT_DCT, mv: [0, 0], skip, coefs: [ty.levels, u.levels, v.levels] };+ (leaf, dist + self.lambda * bits)+ }++ fn inter_leaf(&mut self, r: usize, c: usize, bsize: usize, parent_mv: Option<[i32; 2]>) -> (Leaf, f64) {+ let n4 = NUM_4X4_BLOCKS_WIDE[bsize] as usize;+ let n = n4 * 4;+ let log2 = n.trailing_zeros();+ let (mv, mv_bits) = self.motion_search(r, c, n, parent_mv);+ let refr = self.refr.expect("reference frame");+ let mut pred = vec![0u16; n * n];+ let mut tx = Vec::with_capacity(3);+ let mut dist = 0.0;+ for p in 0..3 {+ let s = (p > 0) as usize;+ let (vw, vh) = self.g.visible(p);+ let pn = n >> s;+ let (x, y) = ((c * 4) >> s, (r * 4) >> s);+ predict_inter(&refr.planes[p], vw as i32 - 1, vh as i32 - 1, x, y, pn, pn, mv, s, self.bd, &mut pred[..pn * pn]);+ if bsize == BLOCK_64X64 {+ // coded as a skip block: the prediction is the reconstruction+ dist += self.sse(p, x, y, pn, &pred[..pn * pn]);+ self.put(p, x, y, pn, &pred[..pn * pn]);+ } else {+ let t = self.code_tx(p, x, y, log2 - s as u32, DCT_DCT, &pred[..pn * pn], false);+ dist += t.dist;+ tx.push(t);+ }+ self.mark_decoded(p, r, c, n4);+ }+ let mode_bits = if mv == [0, 0] { 1.5 } else { 2.0 + mv_bits };+ if bsize == BLOCK_64X64 {+ let leaf = Leaf { bsize, is_inter: true, y_mode: NEWMV, uv_mode: DC_PRED, tx_type_y: DCT_DCT, mv, skip: true, coefs: Default::default() };+ return (leaf, dist + self.lambda * (mode_bits + 1.0));+ }+ let skip = tx.iter().all(|t| !t.nonzero);+ let bits = mode_bits + if skip { 1.0 } else { 1.0 + tx.iter().map(|t| t.bits).sum::<f64>() };+ let [y, u, v]: [TxResult; 3] = tx.try_into().ok().expect("three planes");+ let leaf = Leaf { bsize, is_inter: true, y_mode: NEWMV, uv_mode: DC_PRED, tx_type_y: DCT_DCT, mv, skip, coefs: [y.levels, u.levels, v.levels] };+ (leaf, dist + self.lambda * bits)+ }++ /// Full-sample motion search: candidate vectors then a shrinking diamond search.+ /// Returns the vector and an estimate of its coding cost in bits.+ fn motion_search(&self, r: usize, c: usize, n: usize, parent_mv: Option<[i32; 2]>) -> ([i32; 2], f64) {+ let g = self.g;+ let refr = self.refr.expect("reference frame");+ let mi = |rr: usize, cc: usize| self.mvs[rr * g.mi_cols + cc];+ let mut pred_mv = [0, 0];+ let mut cands: Vec<[i32; 2]> = vec![[0, 0]];+ if c > 0 {+ pred_mv = mi(r, c - 1);+ cands.push(pred_mv);+ }+ if r > 0 {+ cands.push(mi(r - 1, c));+ let cr = c + n / 4;+ if cr < g.mi_cols {+ cands.push(mi(r - 1, cr));+ }+ }+ if let Some(p) = parent_mv {+ cands.push(p);+ }+ cands.push(refr.mvs[r * g.mi_cols + c]);+ let (x0, y0) = ((c * 4) as i32, (r * 4) as i32);+ let (w, h) = (g.width as i32, g.height as i32);+ // keep the referenced block within 64 samples of the picture+ let lim_x = (-x0 - n as i32 - 64, w - x0 + 64);+ let lim_y = (-y0 - n as i32 - 64, h - y0 + 64);+ let valid = |m: [i32; 2]| {+ let (dy, dx) = (m[0] >> 3, m[1] >> 3);+ dx >= lim_x.0 && dx <= lim_x.1 && dy >= lim_y.0 && dy <= lim_y.1+ };+ let mv_bits = |m: [i32; 2]| -> f64 {+ let comp = |d: i32| if d == 0 { 0.5 } else { 2.0 + 2.0 * ((d.abs() / 8) as f64 + 1.0).log2() };+ comp(m[0] - pred_mv[0]) + comp(m[1] - pred_mv[1])+ };+ let cost =+ |m: [i32; 2]| -> f64 { self.sad_ref(&refr.planes[0], x0 + (m[1] >> 3), y0 + (m[0] >> 3), c * 4, r * 4, n) as f64 + self.lambda_sad * mv_bits(m) };+ let mut best = ([0, 0], f64::MAX);+ for m in cands {+ let m = [m[0] & !7, m[1] & !7];+ if !valid(m) {+ continue;+ }+ let cst = cost(m);+ if cst < best.1 {+ best = (m, cst);+ }+ }+ let mut step = 16 * 8;+ while step >= 8 {+ let mut moved = true;+ let mut iters = 0;+ while moved && iters < 16 {+ moved = false;+ iters += 1;+ let center = best.0;+ for d in [[-step, 0], [step, 0], [0, -step], [0, step]] {+ let m = [center[0] + d[0], center[1] + d[1]];+ if !valid(m) {+ continue;+ }+ let cst = cost(m);+ if cst < best.1 {+ best = (m, cst);+ moved = true;+ }+ }+ }+ step >>= 1;+ }+ (best.0, mv_bits(best.0))+ }++ /// SAD of the source block at (x, y) (luma) against the reference block at (rx, ry).+ fn sad_ref(&self, refp: &Plane, rx: i32, ry: i32, x: usize, y: usize, n: usize) -> u64 {+ let (vw, vh) = (self.g.width, self.g.height);+ let w = n.min(vw.saturating_sub(x));+ let h = n.min(vh.saturating_sub(y));+ let src = &self.src[0];+ let (lx, ly) = (vw as i32 - 1, vh as i32 - 1);+ let mut sad = 0u64;+ let inside = rx >= 0 && ry >= 0 && rx + w as i32 - 1 <= lx && ry + h as i32 - 1 <= ly;+ for i in 0..h {+ let s = &src.row(y + i)[x..x + w];+ let rr = refp.row((ry + i as i32).clamp(0, ly) as usize);+ if inside {+ let rrow = &rr[rx as usize..rx as usize + w];+ sad += s.iter().zip(rrow).map(|(&a, &b)| (a as i32 - b as i32).unsigned_abs() as u64).sum::<u64>();+ } else {+ for j in 0..w {+ let b = rr[(rx + j as i32).clamp(0, lx) as usize];+ sad += (s[j] as i32 - b as i32).unsigned_abs() as u64;+ }+ }+ }+ sad+ }++ /// SAD of a prediction against the source over the visible part of the block.+ fn sad(&self, p: usize, x: usize, y: usize, n: usize, pred: &[u16]) -> u64 {+ let (vw, vh) = self.g.visible(p);+ let w = n.min(vw.saturating_sub(x));+ let h = n.min(vh.saturating_sub(y));+ let src = &self.src[p];+ let mut s = 0u64;+ for i in 0..h {+ let row = &src.row(y + i)[x..x + w];+ s += row.iter().zip(&pred[i * n..i * n + w]).map(|(&a, &b)| (a as i32 - b as i32).unsigned_abs() as u64).sum::<u64>();+ }+ s+ }++ fn sse(&self, p: usize, x: usize, y: usize, n: usize, blk: &[u16]) -> f64 {+ let (vw, vh) = self.g.visible(p);+ let w = n.min(vw.saturating_sub(x));+ let h = n.min(vh.saturating_sub(y));+ let src = &self.src[p];+ let mut s = 0u64;+ for i in 0..h {+ let row = &src.row(y + i)[x..x + w];+ s += row+ .iter()+ .zip(&blk[i * n..i * n + w])+ .map(|(&a, &b)| {+ let d = a as i64 - b as i64;+ (d * d) as u64+ })+ .sum::<u64>();+ }+ s as f64+ }++ fn put(&mut self, p: usize, x: usize, y: usize, n: usize, blk: &[u16]) {+ let pl = &mut self.rec[p];+ for i in 0..n {+ let o = (y + i) * pl.stride + x;+ pl.data[o..o + n].copy_from_slice(&blk[i * n..i * n + n]);+ }+ }++ /// Transform, quantise and reconstruct one square transform block of `plane` at (x, y)+ /// over the prediction `pred`; the reconstruction is written to `rec`.+ fn code_tx(&mut self, p: usize, x: usize, y: usize, log2: u32, tx_type: usize, pred: &[u16], intra: bool) -> TxResult {+ let n = 1usize << log2;+ let tx_sz = tx_size_for(log2);+ let bd = self.bd;+ let src = &self.src[p];+ let mut res = vec![0i32; n * n];+ for i in 0..n {+ let row = &src.row(y + i)[x..x + n];+ for j in 0..n {+ res[i * n + j] = row[j] as i32 - pred[i * n + j] as i32;+ }+ }+ let mut coef = vec![0f32; n * n];+ forward_2d(&res, &mut coef, n, tx_type);+ // dead-zone quantiser: rounding offset below one half+ let rnd = if intra { 0.40f32 } else { 0.33f32 };+ let mut levels = vec![0i32; n * n];+ let mut nonzero = false;+ for (k, (&cf, l)) in coef.iter().zip(levels.iter_mut()).enumerate() {+ let q = if k == 0 { self.dc_q } else { self.ac_q };+ let a = (cf.abs() / q + rnd).floor() as i32;+ if a != 0 {+ *l = if cf < 0.0 { -a.min(1 << 16) } else { a.min(1 << 16) };+ nonzero = true;+ }+ }+ let zero_dist = self.sse(p, x, y, n, pred);+ if !nonzero {+ self.put(p, x, y, n, pred);+ return TxResult { levels, nonzero, dist: zero_dist, bits: 1.0 };+ }+ let recon = self.reconstruct(&levels, tx_sz, tx_type, pred, n, bd);+ let dist = self.sse(p, x, y, n, &recon);+ let bits = estimate_bits(&levels, tx_sz);+ if dist + self.lambda * bits >= zero_dist + self.lambda {+ levels.iter_mut().for_each(|l| *l = 0);+ self.put(p, x, y, n, pred);+ return TxResult { levels, nonzero: false, dist: zero_dist, bits: 1.0 };+ }+ self.put(p, x, y, n, &recon);+ TxResult { levels, nonzero: true, dist, bits }+ }++ /// Dequantisation (7.12.3) and the inverse transform, added to the prediction.+ fn reconstruct(&self, levels: &[i32], tx_sz: usize, tx_type: usize, pred: &[u16], n: usize, bd: u32) -> Vec<u16> {+ let denom: i64 = if tx_sz == TX_32X32 { 2 } else { 1 };+ let lim = 1i64 << (7 + bd);+ let mut deq = vec![0i32; n * n];+ for (k, (&l, d)) in levels.iter().zip(deq.iter_mut()).enumerate() {+ if l == 0 {+ continue;+ }+ let q = if k == 0 { self.dc_qi } else { self.ac_qi };+ let dq = l as i64 * q;+ let mag = (dq.abs() & 0xFF_FFFF) / denom;+ *d = (if dq < 0 { -mag } else { mag }).clamp(-lim, lim - 1) as i32;+ }+ let mut resid = vec![0i32; n * n];+ inverse_2d(&deq, &mut resid, tx_sz, tx_type, bd);+ let max = (1i32 << bd) - 1;+ pred.iter().zip(&resid).map(|(&p, &r)| (p as i32 + r).clamp(0, max) as u16).collect()+ }+}++/// Rough coefficient coding cost in bits.+fn estimate_bits(levels: &[i32], tx_sz: usize) -> f64 {+ let scan = scan_for(tx_sz);+ let mut eob = 0;+ for (i, &pos) in scan.iter().enumerate() {+ if levels[pos as usize] != 0 {+ eob = i + 1;+ }+ }+ if eob == 0 {+ return 1.0;+ }+ let mut bits = 3.0 + (eob as f64).log2();+ for &pos in &scan[..eob] {+ let a = levels[pos as usize].unsigned_abs();+ bits += match a {+ 0 => 0.7,+ 1 => 2.5,+ _ => 3.5 + 1.6 * (a as f64).log2(),+ };+ }+ bits+}++pub(crate) fn scan_for(tx_sz: usize) -> &'static [u16] {+ match tx_sz {+ TX_4X4 => &DEFAULT_SCAN_4X4,+ TX_8X8 => &DEFAULT_SCAN_8X8,+ TX_16X16 => &DEFAULT_SCAN_16X16,+ _ => &DEFAULT_SCAN_32X32,+ }+}diff --git a/crates/av1enc/src/lib.rs b/crates/av1enc/src/lib.rsindex 7b2ee3bcf..9a0889caa 100644--- a/crates/av1enc/src/lib.rs+++ b/crates/av1enc/src/lib.rs@@ -1,7 +1,27 @@-//! Clean-room pure-Rust AV1 encoder — API skeleton (implementation in progress).+//! Clean-room pure-Rust AV1 encoder (Main profile, 4:2:0, 8 and 10 bit), implemented from the+//! AV1 Bitstream & Decoding Process Specification v1.0.0 with Errata 1.+//!+//! Key frames and single-reference inter frames (LAST_FRAME = the previous frame), 64x64+//! superblocks split down to 8x8 by rate-distortion decisions, intra modes DC / V / H / Paeth /+//! smooth, full-sample motion search with the spec's motion vector prediction, DCT / ADST+//! transforms up to 32x32, one tile per frame, in-loop filters off. See the crate README.//!//! The public API below is the contract `crates/export` codes against; keep it stable.+mod bitw;+mod cdf;+mod decide;+mod pred;+mod symbol;+mod tables;+mod transform;+mod writer;++use bitw::{BitWriter, obu};+use decide::{Decider, FrameGeom, RefFrame};+use pred::Plane;+use writer::TileWriter;+/// Rate control.#[derive(Clone, Copy, Debug, PartialEq, Eq)]pub enum RateControl {@@ -68,9 +88,63 @@ impl std::fmt::Display for Error {impl std::error::Error for Error {}+const OBU_SEQUENCE_HEADER: u8 = 1;+const OBU_FRAME: u8 = 6;++/// Annex A level limits: (seq_level_idx, MaxPicSize, MaxHSize, MaxVSize, MaxDisplayRate,+/// MaxBitrate (Main tier, Mbit/s)).+const LEVELS: [(u8, u64, u32, u32, u64, u64); 14] = [+ (0, 147_456, 2048, 1152, 4_423_680, 1_500_000),+ (1, 278_784, 2816, 1584, 8_363_520, 3_000_000),+ (4, 665_856, 4352, 2448, 19_975_680, 6_000_000),+ (5, 1_065_024, 5504, 3096, 31_950_720, 10_000_000),+ (8, 2_359_296, 6144, 3456, 70_778_880, 12_000_000),+ (9, 2_359_296, 6144, 3456, 141_557_760, 20_000_000),+ (12, 8_912_896, 8192, 4352, 267_386_880, 30_000_000),+ (13, 8_912_896, 8192, 4352, 534_773_760, 40_000_000),+ (14, 8_912_896, 8192, 4352, 1_069_547_520, 60_000_000),+ (15, 8_912_896, 8192, 4352, 1_069_547_520, 60_000_000),+ (16, 35_651_584, 16384, 8704, 1_069_547_520, 60_000_000),+ (17, 35_651_584, 16384, 8704, 2_139_095_040, 100_000_000),+ (18, 35_651_584, 16384, 8704, 4_278_190_080, 160_000_000),+ (19, 35_651_584, 16384, 8704, 4_278_190_080, 160_000_000),+];++/// The lowest level whose limits fit the stream (31 = no level limits when none does).+fn pick_level(cfg: &EncoderConfig) -> u8 {+ let pic = cfg.width as u64 * cfg.height as u64;+ let rate = pic * cfg.fps_num as u64 / cfg.fps_den.max(1) as u64;+ let bitrate = match cfg.rate {+ RateControl::Bitrate { kbps } => kbps as u64 * 1000,+ RateControl::ConstantQ(_) => 0,+ };+ for &(idx, max_pic, max_h, max_v, max_rate, max_br) in LEVELS.iter() {+ if pic <= max_pic && cfg.width <= max_h && cfg.height <= max_v && rate <= max_rate && bitrate <= max_br {+ return idx;+ }+ }+ 31+}++/// Frame-level rate control state for `RateControl::Bitrate`.+struct RateState {+ /// Bits per frame at the target rate.+ frame_bits: f64,+ /// Quantizer index for inter frames (continuous).+ q: f64,+ /// Accumulated (spent - budget) bits.+ debt: f64,+}+/// AV1 encoder. Frames come out in input order (no hidden/reordered frames).pub struct Encoder {cfg: EncoderConfig,+ geom: FrameGeom,+ level: u8,+ seq_obu: Vec<u8>,+ frame_num: u64,+ refr: Option<RefFrame>,+ rc: Option<RateState>,}impl Encoder {@@ -78,21 +152,302 @@ impl Encoder {if cfg.width == 0 || cfg.height == 0 {return Err(Error::InvalidConfig("empty frame".into()));}- Ok(Encoder { cfg })+ if cfg.width > 4096 || cfg.height > 4096 {+ return Err(Error::InvalidConfig(format!("{}x{} exceeds the single-tile limit (4096 samples per side)", cfg.width, cfg.height)));+ }+ if cfg.bit_depth != 8 && cfg.bit_depth != 10 {+ return Err(Error::InvalidConfig(format!("bit depth {} (8 or 10 supported)", cfg.bit_depth)));+ }+ if cfg.fps_num == 0 || cfg.fps_den == 0 {+ return Err(Error::InvalidConfig("frame rate".into()));+ }+ let geom = FrameGeom::new(cfg.width as usize, cfg.height as usize);+ if geom.sb_cols * geom.sb_rows > 2304 {+ return Err(Error::InvalidConfig(format!("{}x{} exceeds the single-tile area limit (MAX_TILE_AREA)", cfg.width, cfg.height)));+ }+ if let RateControl::Bitrate { kbps } = cfg.rate+ && kbps == 0+ {+ return Err(Error::InvalidConfig("zero bitrate".into()));+ }+ let level = cfg.level_idx.unwrap_or_else(|| pick_level(&cfg));+ if level > 31 {+ return Err(Error::InvalidConfig(format!("seq_level_idx {level}")));+ }+ let rc = match cfg.rate {+ RateControl::ConstantQ(_) => None,+ RateControl::Bitrate { kbps } => {+ let frame_bits = kbps as f64 * 1000.0 * cfg.fps_den as f64 / cfg.fps_num as f64;+ // initial guess from the bits per sample+ let bpp = frame_bits / (cfg.width as f64 * cfg.height as f64);+ let q = (110.0 - 40.0 * (bpp / 0.1).log2()).clamp(20.0, 230.0);+ Some(RateState { frame_bits, q, debt: 0.0 })+ }+ };+ let mut enc = Encoder { cfg, geom, level, seq_obu: Vec::new(), frame_num: 0, refr: None, rc };+ let payload = enc.sequence_header_payload();+ obu(OBU_SEQUENCE_HEADER, &payload, &mut enc.seq_obu);+ Ok(enc)}+pub fn config(&self) -> &EncoderConfig {&self.cfg}+/// The sequence header OBU (with size field).pub fn sequence_header_obu(&self) -> Vec<u8> {- vec![]+ self.seq_obu.clone()}+/// AV1CodecConfigurationRecord (`av1C` payload, also the WebM CodecPrivate).pub fn av1c(&self) -> Vec<u8> {- vec![]+ let high = (self.cfg.bit_depth > 8) as u8;+ let mut v = vec![+ 0x81, // marker (1), version (1)+ self.level & 0x1f, // seq_profile 0, seq_level_idx_0+ (high << 6) | (1 << 3) | (1 << 2), // tier 0, high_bitdepth, twelve_bit 0, mono 0, ss_x 1, ss_y 1, csp 0+ 0, // no initial_presentation_delay+ ];+ v.extend_from_slice(&self.seq_obu);+ v+ }++ /// sequence_header_obu( ) payload (5.5) including trailing bits.+ fn sequence_header_payload(&self) -> Vec<u8> {+ let c = &self.cfg;+ let mut w = BitWriter::new();+ w.f(3, 0); // seq_profile+ w.f(1, 0); // still_picture+ w.f(1, 0); // reduced_still_picture_header+ w.f(1, 0); // timing_info_present_flag+ w.f(1, 0); // initial_display_delay_present_flag+ w.f(5, 0); // operating_points_cnt_minus_1+ w.f(12, 0); // operating_point_idc[ 0 ]+ w.f(5, self.level as u32); // seq_level_idx[ 0 ]+ if self.level > 7 {+ w.f(1, 0); // seq_tier[ 0 ]+ }+ let wbits = 32 - (c.width - 1).max(1).leading_zeros();+ let hbits = 32 - (c.height - 1).max(1).leading_zeros();+ w.f(4, wbits - 1);+ w.f(4, hbits - 1);+ w.f(wbits, c.width - 1);+ w.f(hbits, c.height - 1);+ w.f(1, 0); // frame_id_numbers_present_flag+ w.f(1, 0); // use_128x128_superblock+ w.f(1, 0); // enable_filter_intra+ w.f(1, 0); // enable_intra_edge_filter+ w.f(1, 0); // enable_interintra_compound+ w.f(1, 0); // enable_masked_compound+ w.f(1, 0); // enable_warped_motion+ w.f(1, 0); // enable_dual_filter+ w.f(1, 0); // enable_order_hint+ w.f(1, 0); // seq_choose_screen_content_tools+ w.f(1, 0); // seq_force_screen_content_tools+ w.f(1, 0); // enable_superres+ w.f(1, 0); // enable_cdef+ w.f(1, 0); // enable_restoration+ // color_config( )+ w.f(1, (c.bit_depth > 8) as u32); // high_bitdepth+ w.f(1, 0); // mono_chrome+ w.f(1, 1); // color_description_present_flag+ w.f(8, 1); // color_primaries: BT.709+ w.f(8, 1); // transfer_characteristics: BT.709+ w.f(8, 1); // matrix_coefficients: BT.709+ w.f(1, c.full_range as u32); // color_range+ w.f(2, 0); // chroma_sample_position: unknown+ w.f(1, 0); // separate_uv_delta_q+ w.f(1, 0); // film_grain_params_present+ w.trailing_bits();+ w.into_bytes()+ }++ /// uncompressed_header( ) (5.9) for a shown KEY_FRAME or an INTER_FRAME referencing slot 0.+ fn frame_header(&self, w: &mut BitWriter, key: bool, qidx: u8) {+ w.f(1, 0); // show_existing_frame+ w.f(2, if key { 0 } else { 1 }); // frame_type+ w.f(1, 1); // show_frame+ if !key {+ w.f(1, 0); // error_resilient_mode+ }+ w.f(1, 0); // disable_cdf_update+ w.f(1, 0); // frame_size_override_flag+ if !key {+ w.f(3, 7); // primary_ref_frame = PRIMARY_REF_NONE+ w.f(8, 1); // refresh_frame_flags: slot 0 becomes LAST for the next frame+ for _ in 0..7 {+ w.f(3, 0); // ref_frame_idx[ i ]+ }+ w.f(1, 0); // render_and_frame_size_different+ w.f(1, 0); // allow_high_precision_mv+ w.f(1, 0); // is_filter_switchable+ w.f(2, 0); // interpolation_filter = EIGHTTAP+ w.f(1, 0); // is_motion_mode_switchable+ } else {+ w.f(1, 0); // render_and_frame_size_different+ }+ w.f(1, 1); // disable_frame_end_update_cdf+ // tile_info( ): uniform spacing, one tile+ let g = &self.geom;+ let tile_log2 = |blk: usize, target: usize| {+ let mut k = 0;+ while (blk << k) < target {+ k += 1;+ }+ k+ };+ w.f(1, 1); // uniform_tile_spacing_flag+ if tile_log2(1, g.sb_cols.min(64)) > 0 {+ w.f(1, 0); // increment_tile_cols_log2+ }+ if tile_log2(1, g.sb_rows.min(64)) > 0 {+ w.f(1, 0); // increment_tile_rows_log2+ }+ // quantization_params( )+ w.f(8, qidx as u32);+ w.f(1, 0); // DeltaQYDc+ w.f(1, 0); // DeltaQUDc+ w.f(1, 0); // DeltaQUAc+ w.f(1, 0); // using_qmatrix+ w.f(1, 0); // segmentation_enabled+ w.f(1, 0); // delta_q_present (base_q_idx > 0)+ // loop_filter_params( )+ w.f(6, 0);+ w.f(6, 0);+ w.f(3, 0); // loop_filter_sharpness+ w.f(1, 0); // loop_filter_delta_enabled+ w.f(1, 0); // tx_mode_select (TX_MODE_LARGEST)+ if !key {+ w.f(1, 0); // reference_select+ }+ w.f(1, 1); // reduced_tx_set+ if !key {+ for _ in 0..7 {+ w.f(1, 0); // is_global+ }+ }+ }++ /// Copies a picture into superblock-padded planes (edge replication).+ fn load_source(&self, frame: &Frame) -> [Plane; 3] {+ let maxv = (1u16 << self.cfg.bit_depth) - 1;+ std::array::from_fn(|p| {+ let (vw, vh) = self.geom.visible(p);+ let (pw, ph) = self.geom.padded(p);+ let (data, stride) = match p {+ 0 => (frame.y, frame.y_stride),+ 1 => (frame.u, frame.uv_stride),+ _ => (frame.v, frame.uv_stride),+ };+ assert!(stride >= vw && data.len() >= stride * (vh - 1) + vw, "plane {p} is too small for {vw}x{vh}");+ let mut pl = Plane::new(pw, ph);+ for y in 0..ph {+ let sy = y.min(vh - 1);+ let src = &data[sy * stride..sy * stride + vw];+ let row = &mut pl.data[y * pw..y * pw + pw];+ for (x, d) in row.iter_mut().enumerate() {+ *d = src[x.min(vw - 1)].min(maxv);+ }+ }+ pl+ })}++ /// Codes one frame at `qidx`; returns the frame OBU and the reconstruction.+ fn encode_frame(&self, src: &[Plane; 3], key: bool, qidx: u8) -> (Vec<u8>, RefFrame) {+ let g = &self.geom;+ let refr = if key { None } else { self.refr.as_ref() };+ let mut dec = Decider::new(g, self.cfg.bit_depth as u32, qidx, key, src, refr);+ let mut tw = TileWriter::new(g, key, qidx);+ for sbr in 0..g.sb_rows {+ tw.start_sb_row();+ for sbc in 0..g.sb_cols {+ let node = dec.superblock(sbr * 16, sbc * 16);+ tw.superblock(sbr * 16, sbc * 16, &node);+ }+ }+ let tile = tw.finish();+ let mut w = BitWriter::new();+ self.frame_header(&mut w, key, qidx);+ w.byte_align();+ // tile_group_obu( ): a single tile, no tile_start_and_end_present_flag+ let mut payload = w.into_bytes();+ payload.extend_from_slice(&tile);+ let mut out = Vec::with_capacity(payload.len() + 8);+ obu(OBU_FRAME, &payload, &mut out);+ let rf = RefFrame { planes: dec.rec, mvs: dec.mvs };+ (out, rf)+ }+/// Encodes one frame.- pub fn encode(&mut self, _frame: &Frame) -> Packet {- Packet { data: vec![], keyframe: true }+ pub fn encode(&mut self, frame: &Frame) -> Packet {+ let key = self.frame_num.is_multiple_of(self.cfg.keyint.max(1) as u64);+ let src = self.load_source(frame);+ let (frame_obu, rf) = match self.cfg.rate {+ RateControl::ConstantQ(q) => self.encode_frame(&src, key, q.max(1)),+ RateControl::Bitrate { .. } => self.encode_rate_controlled(&src, key),+ };+ self.refr = Some(rf);+ self.frame_num += 1;+ let mut data = Vec::with_capacity(frame_obu.len() + self.seq_obu.len());+ if key {+ data.extend_from_slice(&self.seq_obu);+ }+ data.extend_from_slice(&frame_obu);+ Packet { data, keyframe: key }+ }++ fn encode_rate_controlled(&mut self, src: &[Plane; 3], key: bool) -> (Vec<u8>, RefFrame) {+ let first = self.frame_num == 0;+ let rc = self.rc.as_ref().expect("rate control state");+ let (frame_bits, debt, q_inter) = (rc.frame_bits, rc.debt, rc.q);+ // key frames get a lower quantizer and a larger share of the budget+ let key_boost = 1.0 + (self.cfg.keyint.clamp(1, 60) as f64 - 1.0).sqrt() * 0.75;+ let budget = if key { frame_bits * key_boost } else { frame_bits };+ // pay back a fraction of the accumulated deviation+ let target = (budget - debt * 0.25).max(budget * 0.25);+ let mut q = if key { q_inter - 24.0 } else { q_inter };+ let mut best: Option<(Vec<u8>, RefFrame, f64)> = None;+ let passes = if first { 4 } else { 1 };+ for pass in 0..passes {+ let qi = q.round().clamp(1.0, 255.0) as u8;+ let (obu, rf) = self.encode_frame(src, key, qi);+ let bits = obu.len() as f64 * 8.0;+ let err = (bits / target).log2();+ let better = best.as_ref().is_none_or(|b| err.abs() < (b.2 / target).log2().abs());+ if better {+ best = Some((obu, rf, bits));+ }+ if pass + 1 < passes {+ q = (q + 30.0 * err).clamp(1.0, 255.0);+ }+ }+ let (obu, rf, bits) = best.expect("encoded");+ let rc = self.rc.as_mut().expect("rate control state");+ rc.debt += bits - budget;+ // adapt the inter quantizer: about 30 index steps per doubling of the rate+ let err = (bits / target).log2().clamp(-1.0, 1.0);+ if first && key {+ rc.q = (q + 24.0).clamp(1.0, 255.0);+ } else {+ rc.q = (rc.q + 12.0 * err).clamp(1.0, 255.0);+ }+ (obu, rf)+ }++ /// The reconstruction of the last encoded frame (visible area, planes Y, U, V), equal to+ /// what a decoder outputs. For tests.+ #[doc(hidden)]+ pub fn last_reconstruction(&self) -> Option<[Vec<u16>; 3]> {+ let rf = self.refr.as_ref()?;+ Some(std::array::from_fn(|p| {+ let (vw, vh) = self.geom.visible(p);+ let pl = &rf.planes[p];+ let mut v = Vec::with_capacity(vw * vh);+ for y in 0..vh {+ v.extend_from_slice(&pl.row(y)[..vw]);+ }+ v+ }))}}diff --git a/crates/av1enc/src/pred.rs b/crates/av1enc/src/pred.rsnew file mode 100644index 000000000..ab7e0d0da--- /dev/null+++ b/crates/av1enc/src/pred.rs@@ -0,0 +1,262 @@+//! Prediction processes, mirrored from the spec (7.11.2 intra prediction for the non-directional+//! modes plus V_PRED / H_PRED without edge filtering, 7.11.3 block inter prediction without+//! reference scaling) so that the encoder predicts exactly like a decoder.++use crate::tables::*;+use crate::transform::round2;++/// A plane buffer (padded to whole superblocks).+#[derive(Clone)]+pub(crate) struct Plane {+ pub data: Vec<u16>,+ pub stride: usize,+}++impl Plane {+ pub fn new(w: usize, h: usize) -> Plane {+ Plane { data: vec![0; w * h], stride: w }+ }+ #[inline(always)]+ pub fn at(&self, x: usize, y: usize) -> u16 {+ self.data[y * self.stride + x]+ }+ #[inline(always)]+ pub fn row(&self, y: usize) -> &[u16] {+ &self.data[y * self.stride..(y + 1) * self.stride]+ }+}++/// The intra modes this encoder uses (values are the spec's y / uv mode numbers).+pub(crate) const INTRA_MODES_USED: [usize; 7] = [DC_PRED, V_PRED, H_PRED, PAETH_PRED, SMOOTH_PRED, SMOOTH_V_PRED, SMOOTH_H_PRED];++/// Inputs of the intra prediction process.+pub(crate) struct IntraEdge {+ pub x: usize,+ pub y: usize,+ pub have_left: bool,+ pub have_above: bool,+ pub have_above_right: bool,+ pub have_below_left: bool,+ pub log2: u32,+ /// ((MiCols * MI_SIZE) >> subX) - 1 and the same for rows.+ pub max_x: i32,+ pub max_y: i32,+ pub bit_depth: u32,+}++/// Edge samples: `above[0..2n]`, `left[0..2n]` and the top-left corner.+pub(crate) struct Edges {+ above: [i32; 64],+ left: [i32; 64],+ corner: i32,+}++pub(crate) fn edges(pl: &Plane, e: &IntraEdge) -> Edges {+ let w = 1usize << e.log2;+ let h = w;+ let bd = e.bit_depth;+ let (x, y) = (e.x, e.y);+ let n = w + h;+ let mut above = [0i32; 64];+ let mut left = [0i32; 64];+ if !e.have_above && e.have_left {+ let v = pl.at(x - 1, y) as i32;+ above[..n].iter_mut().for_each(|a| *a = v);+ } else if !e.have_above && !e.have_left {+ let v = (1 << (bd - 1)) - 1;+ above[..n].iter_mut().for_each(|a| *a = v);+ } else {+ let above_limit = e.max_x.min(x as i32 + if e.have_above_right { 2 * w as i32 } else { w as i32 } - 1);+ let row = pl.row(y - 1);+ for (i, a) in above[..n].iter_mut().enumerate() {+ *a = row[above_limit.min(x as i32 + i as i32) as usize] as i32;+ }+ }+ if !e.have_left && e.have_above {+ let v = pl.at(x, y - 1) as i32;+ left[..n].iter_mut().for_each(|a| *a = v);+ } else if !e.have_left && !e.have_above {+ let v = (1 << (bd - 1)) + 1;+ left[..n].iter_mut().for_each(|a| *a = v);+ } else {+ let left_limit = e.max_y.min(y as i32 + if e.have_below_left { 2 * h as i32 } else { h as i32 } - 1);+ for (i, l) in left[..n].iter_mut().enumerate() {+ *l = pl.at(x - 1, left_limit.min(y as i32 + i as i32) as usize) as i32;+ }+ }+ let corner = if e.have_above && e.have_left {+ pl.at(x - 1, y - 1) as i32+ } else if e.have_above {+ pl.at(x, y - 1) as i32+ } else if e.have_left {+ pl.at(x - 1, y) as i32+ } else {+ 1 << (bd - 1)+ };+ Edges { above, left, corner }+}++fn sm_weights(log2: u32) -> &'static [u8] {+ match log2 {+ 2 => &SM_WEIGHTS_TX_4X4,+ 3 => &SM_WEIGHTS_TX_8X8,+ 4 => &SM_WEIGHTS_TX_16X16,+ _ => &SM_WEIGHTS_TX_32X32,+ }+}++/// Predicts a square `2^log2` block with `mode` into `pred` (row stride = block width).+pub(crate) fn predict_intra(ed: &Edges, e: &IntraEdge, mode: usize, pred: &mut [u16]) {+ let log2 = e.log2;+ let w = 1usize << log2;+ let h = w;+ let bd = e.bit_depth;+ let (above, left) = (&ed.above, &ed.left);+ match mode {+ DC_PRED => {+ let v = if e.have_left && e.have_above {+ let sum: i32 = left[..h].iter().sum::<i32>() + above[..w].iter().sum::<i32>() + ((w + h) >> 1) as i32;+ sum / (w + h) as i32+ } else if e.have_left {+ (left[..h].iter().sum::<i32>() + (h >> 1) as i32) >> log2+ } else if e.have_above {+ (above[..w].iter().sum::<i32>() + (w >> 1) as i32) >> log2+ } else {+ 1 << (bd - 1)+ };+ let v = v.clamp(0, (1 << bd) - 1) as u16;+ pred[..w * h].iter_mut().for_each(|p| *p = v);+ }+ V_PRED => {+ for i in 0..h {+ for j in 0..w {+ pred[i * w + j] = above[j] as u16;+ }+ }+ }+ H_PRED => {+ for i in 0..h {+ pred[i * w..i * w + w].iter_mut().for_each(|p| *p = left[i] as u16);+ }+ }+ PAETH_PRED => {+ let tl = ed.corner;+ for i in 0..h {+ for j in 0..w {+ let a = above[j];+ let l = left[i];+ let base = a + l - tl;+ let p_left = (base - l).abs();+ let p_top = (base - a).abs();+ let p_tl = (base - tl).abs();+ pred[i * w + j] = if p_left <= p_top && p_left <= p_tl {+ l as u16+ } else if p_top <= p_tl {+ a as u16+ } else {+ tl as u16+ };+ }+ }+ }+ _ => {+ let wx = sm_weights(log2);+ let wy = wx;+ for i in 0..h {+ for j in 0..w {+ let v = if mode == SMOOTH_PRED {+ let s = wy[i] as i32 * above[j] + (256 - wy[i] as i32) * left[h - 1] + wx[j] as i32 * left[i] + (256 - wx[j] as i32) * above[w - 1];+ round2(s, 9)+ } else if mode == SMOOTH_V_PRED {+ round2(wy[i] as i32 * above[j] + (256 - wy[i] as i32) * left[h - 1], 8)+ } else {+ round2(wx[j] as i32 * left[i] + (256 - wx[j] as i32) * above[w - 1], 8)+ };+ pred[i * w + j] = v as u16;+ }+ }+ }+ }+}++/// Block inter prediction (7.11.3.3 motion vector scaling without scaling, 7.11.3.4 block+/// inter prediction with the EIGHTTAP filter, single reference so InterRound1 = 11 and the+/// final rounding is the identity) of the `w x h` block at plane position (x, y) displaced by+/// `mv` (1/8 luma sample units, [row, col]). `last_x` / `last_y` are the largest sample+/// coordinates of the reference plane; the result goes to `pred` (row stride `w`).+pub(crate) fn predict_inter(+ refp: &Plane,+ last_x: i32,+ last_y: i32,+ x: usize,+ y: usize,+ w: usize,+ h: usize,+ mv: [i32; 2],+ ss: usize,+ bit_depth: u32,+ pred: &mut [u16],+) {+ // positions in 1/16 sample units ((startX >> 6) & 15 is the filter phase)+ let px = ((x as i32) << 4) + ((2 * mv[1]) >> ss);+ let py = ((y as i32) << 4) + ((2 * mv[0]) >> ss);+ let (fx, fy) = ((px & 15) as usize, (py & 15) as usize);+ let (ix, iy) = (px >> 4, py >> 4);+ let max = (1i32 << bit_depth) - 1;+ if fx == 0 && fy == 0 {+ // the filters' phase-0 kernel is a unit impulse: a clamped copy+ for r in 0..h {+ let ry = (iy + r as i32).clamp(0, last_y) as usize;+ let row = refp.row(ry);+ let out = &mut pred[r * w..r * w + w];+ let x0 = ix;+ if x0 >= 0 && x0 + w as i32 - 1 <= last_x {+ out.copy_from_slice(&row[x0 as usize..x0 as usize + w]);+ } else {+ for (c, o) in out.iter_mut().enumerate() {+ *o = row[(x0 + c as i32).clamp(0, last_x) as usize];+ }+ }+ }+ return;+ }+ let filt_h = if w <= 4 { 4 } else { EIGHTTAP };+ let filt_v = if h <= 4 { 4 } else { EIGHTTAP };+ let kh = &SUBPEL_FILTERS[filt_h][fx];+ let kv = &SUBPEL_FILTERS[filt_v][fy];+ let inter_h = h + 7;+ let mut inter = vec![0i32; inter_h * w];+ for r in 0..inter_h {+ let ry = (iy + r as i32 - 3).clamp(0, last_y) as usize;+ let row = refp.row(ry);+ for c in 0..w {+ let bx = ix + c as i32 - 3;+ let mut s = 0i32;+ for t in 0..8 {+ s += kh[t] as i32 * row[(bx + t as i32).clamp(0, last_x) as usize] as i32;+ }+ inter[r * w + c] = round2(s, 3);+ }+ }+ for r in 0..h {+ for c in 0..w {+ let mut s = 0i32;+ for t in 0..8 {+ s += kv[t] as i32 * inter[(r + t) * w + c];+ }+ pred[r * w + c] = round2(s, 11).clamp(0, max) as u16;+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn phase_zero_filters_are_impulses() {+ for f in SUBPEL_FILTERS.iter() {+ assert_eq!(f[0], [0, 0, 0, 128, 0, 0, 0, 0]);+ }+ }+}diff --git a/crates/av1enc/src/symbol.rs b/crates/av1enc/src/symbol.rsnew file mode 100644index 000000000..579a51e20--- /dev/null+++ b/crates/av1enc/src/symbol.rs@@ -0,0 +1,267 @@+//! Multi-symbol arithmetic encoder: the exact inverse of the symbol decoder of spec 8.2.+//!+//! The decoder keeps `SymbolValue`, the bitwise complement of a 15-bit window into the tile data+//! minus the lower bounds of the symbols decoded so far, and selects symbol `k` when+//! `cur(k) <= SymbolValue < cur(k - 1)` with+//! `cur(k) = ((SymbolRange >> 8) * ((32768 - cdf[k]) >> 6) >> 1) + 4 * (N - k - 1)` and+//! `cur(-1) = SymbolRange`. The encoder therefore runs an ordinary carry-propagating range+//! coder over the complemented bit stream: coding symbol `k` adds `cur(k)` to `low` and sets the+//! range to `cur(k - 1) - cur(k)`; both are renormalised exactly like the decoder's range.+//!+//! At the end (spec 8.2.4 exit process) the decoder has consumed `S` renormalisation bits plus a+//! 15-bit window, and the bits from position `S` on must be a one followed by zeros. In the+//! complemented domain that window is `0b011_1111_1111_1111`, so the encoder picks the `S`-bit+//! prefix `D` with `D * 2^15 + 2^14 - 1` inside `[low, low + range)`, i.e.+//! `D = (low + 2^14) >> 15`, and writes `!D`, a one bit and zero padding.++pub(crate) struct SymbolWriter {+ /// Low end of the interval, holding `15 + cnt` bits not yet moved to `out` (plus carries).+ low: u64,+ rng: u32,+ cnt: u32,+ out: Vec<u8>,+ pub disable_update: bool,+}++impl SymbolWriter {+ pub fn new(disable_update: bool) -> Self {+ SymbolWriter { low: 0, rng: 1 << 15, cnt: 0, out: Vec::with_capacity(1 << 16), disable_update }+ }++ /// Adds `k` to the bytes already output (carry propagation).+ fn carry(&mut self, mut k: u64) {+ let mut i = self.out.len();+ while k != 0 {+ i -= 1;+ let t = self.out[i] as u64 + k;+ self.out[i] = t as u8;+ k = t >> 8;+ }+ }++ #[inline(always)]+ fn encode(&mut self, cdf: &[u16], n: usize, s: usize) {+ let r8 = self.rng >> 8;+ let cur = |k: usize| -> u32 { ((r8 * ((32768 - cdf[k] as u32) >> 6)) >> 1) + 4 * (n - k - 1) as u32 };+ let hi = if s == 0 { self.rng } else { cur(s - 1) };+ let lo = cur(s);+ debug_assert!(hi > lo, "symbol {s} has an empty interval");+ self.low += lo as u64;+ self.rng = hi - lo;+ let d = 15 - (31 - self.rng.leading_zeros());+ if d == 0 {+ return;+ }+ self.rng <<= d;+ self.low <<= d;+ self.cnt += d;+ while self.cnt >= 8 {+ let sh = 15 + self.cnt - 8;+ let c = self.low >> sh;+ self.low &= (1u64 << sh) - 1;+ self.cnt -= 8;+ self.carry(c >> 8);+ self.out.push((c & 0xff) as u8);+ }+ }++ /// Codes `s` with the adaptive CDF `cdf` (N values plus the adaptation counter).+ #[inline]+ pub fn symbol(&mut self, s: usize, cdf: &mut [u16]) {+ let n = cdf.len() - 1;+ debug_assert!(s < n);+ self.encode(cdf, n, s);+ if !self.disable_update {+ update_cdf(cdf, n, s);+ }+ }++ /// Codes `s` with a CDF that is not adapted.+ pub fn symbol_fixed(&mut self, s: usize, cdf: &[u16]) {+ let n = cdf.len() - 1;+ self.encode(cdf, n, s);+ }++ /// `read_bool()` counterpart.+ pub fn bool(&mut self, b: bool) {+ self.encode(&[1 << 14, 1 << 15, 0], 2, b as usize);+ }++ /// `L(n)` counterpart (most significant bit first).+ #[cfg(test)]+ pub fn literal(&mut self, n: u32, v: u32) {+ for i in (0..n).rev() {+ self.bool((v >> i) & 1 != 0);+ }+ }++ /// Finishes the tile: returns its bytes (with the trailing one bit and zero padding).+ pub fn finish(mut self) -> Vec<u8> {+ let low = self.low + (1 << 14);+ let mut tail = low >> 15;+ self.carry(tail >> self.cnt);+ tail &= (1u64 << self.cnt) - 1;+ let mut bytes: Vec<u8> = self.out.iter().map(|b| !b).collect();+ let n = self.cnt + 1; // 1..=8 bits: !tail then the trailing one bit+ let v = (((!tail) & ((1u64 << self.cnt) - 1)) << 1) | 1;+ bytes.push((v << (8 - n)) as u8);+ bytes+ }+}++/// CDF adaptation (spec 8.2.6, as in the decoder).+#[inline(always)]+pub(crate) fn update_cdf(cdf: &mut [u16], n: usize, symbol: usize) {+ let count = cdf[n];+ let rate = 3 + (count > 15) as u32 + (count > 31) as u32 + (31 - (n as u32).leading_zeros()).min(2);+ let mut tmp = 0u32;+ for i in 0..n - 1 {+ if i == symbol {+ tmp = 1 << 15;+ }+ let c = cdf[i] as u32;+ if tmp < c {+ cdf[i] = (c - ((c - tmp) >> rate)) as u16;+ } else {+ cdf[i] = (c + ((tmp - c) >> rate)) as u16;+ }+ }+ cdf[n] += (count < 32) as u16;+}++#[cfg(test)]+mod tests {+ use super::*;++ /// The spec's symbol decoder (8.2.2 - 8.2.4), for checking the encoder.+ struct Dec<'a> {+ data: &'a [u8],+ pos: usize,+ value: u32,+ range: u32,+ max_bits: i64,+ }++ impl<'a> Dec<'a> {+ fn new(data: &'a [u8]) -> Self {+ let mut d = Dec { data, pos: 0, value: 0, range: 1 << 15, max_bits: 8 * data.len() as i64 - 15 };+ let nb = (data.len() * 8).min(15) as u32;+ let buf = d.bits(nb);+ d.value = ((1 << 15) - 1) ^ (buf << (15 - nb));+ d+ }+ fn bits(&mut self, n: u32) -> u32 {+ let mut x = 0;+ for _ in 0..n {+ let b = if self.pos < self.data.len() * 8 { (self.data[self.pos >> 3] >> (7 - (self.pos & 7))) & 1 } else { 0 };+ self.pos += 1;+ x = (x << 1) | b as u32;+ }+ x+ }+ fn symbol(&mut self, cdf: &mut [u16], adapt: bool) -> usize {+ let n = cdf.len() - 1;+ let mut cur = self.range;+ let mut s = 0;+ let mut prev;+ loop {+ prev = cur;+ let f = (1u32 << 15) - cdf[s] as u32;+ cur = (((self.range >> 8) * (f >> 6)) >> 1) + 4 * (n - s - 1) as u32;+ if self.value >= cur {+ break;+ }+ s += 1;+ }+ self.range = prev - cur;+ self.value -= cur;+ let b = 15 - (31 - self.range.leading_zeros());+ if b > 0 {+ self.range <<= b;+ let nb = (b as i64).min(self.max_bits.max(0)) as u32;+ let nd = self.bits(nb) << (b - nb);+ self.value = nd ^ (((self.value + 1) << b) - 1);+ self.max_bits -= b as i64;+ }+ if adapt {+ update_cdf(cdf, n, s);+ }+ s+ }+ /// Position of the trailing one bit (8.2.4).+ fn trailing_position(&self) -> i64 {+ let consumed = (self.pos as i64).min(self.data.len() as i64 * 8);+ consumed - (15i64).min(self.max_bits + 15)+ }+ }++ #[test]+ fn bools_only() {+ for len in 1..200 {+ let bits: Vec<bool> = (0..len).map(|i| (i * 7 + len) % 3 == 0).collect();+ let mut w = SymbolWriter::new(false);+ for &b in &bits {+ w.bool(b);+ }+ let data = w.finish();+ let mut d = Dec::new(&data);+ for (i, &b) in bits.iter().enumerate() {+ assert_eq!(d.symbol(&mut [1 << 14, 1 << 15, 0], false) == 1, b, "len {len} bit {i} data {data:?}");+ }+ }+ }++ #[test]+ fn round_trip_random_symbols() {+ let mut seed = 0x1234_5678u32;+ let mut rnd = || {+ seed ^= seed << 13;+ seed ^= seed >> 17;+ seed ^= seed << 5;+ seed+ };+ for trial in 0..300 {+ let count = 1 + (rnd() % 3000) as usize;+ let mut cdfs_e = vec![[4096u16, 11264, 19328, 32768, 0]; 4];+ let mut cdfs_d = cdfs_e.clone();+ let mut syms = Vec::new();+ let mut w = SymbolWriter::new(false);+ for _ in 0..count {+ let kind = rnd() % 3;+ let ctx = (rnd() % 4) as usize;+ // skewed symbols so that the range also gets small+ let s = if rnd() % 4 == 0 { (rnd() % 4) as usize } else { 0 };+ match kind {+ 0 => w.symbol(s, &mut cdfs_e[ctx]),+ 1 => w.bool(s & 1 == 1),+ _ => w.literal(5, s as u32 * 7),+ }+ syms.push((kind, ctx, s));+ }+ let data = w.finish();+ let mut d = Dec::new(&data);+ for (idx, &(kind, ctx, s)) in syms.iter().enumerate() {+ let got = match kind {+ 0 => d.symbol(&mut cdfs_d[ctx], true),+ 1 => d.symbol(&mut [1 << 14, 1 << 15, 0], false),+ _ => {+ let mut v = 0;+ for _ in 0..5 {+ v = (v << 1) | d.symbol(&mut [1 << 14, 1 << 15, 0], false);+ }+ v / 7+ }+ };+ let want = if kind == 1 { s & 1 } else { s };+ assert_eq!(got, want, "trial {trial} idx {idx} kind {kind} of {}", syms.len());+ }+ let tp = d.trailing_position();+ assert!(tp >= 0 && (tp as usize) < data.len() * 8, "trial {trial}: trailing bit position {tp}");+ let tp = tp as usize;+ assert_eq!((data[tp >> 3] >> (7 - (tp & 7))) & 1, 1, "trial {trial}: trailing one bit");+ for p in tp + 1..data.len() * 8 {+ assert_eq!((data[p >> 3] >> (7 - (p & 7))) & 1, 0, "trial {trial}: padding");+ }+ }+ }+}diff --git a/crates/av1enc/src/tables.rs b/crates/av1enc/src/tables.rsnew file mode 100644index 000000000..3e333a519--- /dev/null+++ b/crates/av1enc/src/tables.rs@@ -0,0 +1,4805 @@+// Tables mirrored from filmcraft-av1's spec_tables.rs, which extracts them from the AV1 Bitstream &+// Decoding Process Specification v1.0.0 with Errata 1. Only the tables this encoder uses are kept.+#![allow(dead_code, clippy::unreadable_literal)]++pub const ADST_ADST: usize = 3;+pub const ADST_DCT: usize = 1;+pub const ADST_FLIPADST: usize = 7;+pub const AFFINE: usize = 3;+pub const ALTREF2_FRAME: usize = 6;+pub const ALTREF_FRAME: usize = 7;+pub const ANGLE_STEP: usize = 3;+pub const BIDIR_COMP_REFERENCE: usize = 1;+pub const BILINEAR: usize = 3;+pub const BLOCK_128X128: usize = 15;+pub const BLOCK_128X64: usize = 14;+pub const BLOCK_16X16: usize = 6;+pub const BLOCK_16X32: usize = 7;+pub const BLOCK_16X4: usize = 17;+pub const BLOCK_16X64: usize = 20;+pub const BLOCK_16X8: usize = 5;+pub const BLOCK_32X16: usize = 8;+pub const BLOCK_32X32: usize = 9;+pub const BLOCK_32X64: usize = 10;+pub const BLOCK_32X8: usize = 19;+pub const BLOCK_4X16: usize = 16;+pub const BLOCK_4X4: usize = 0;+pub const BLOCK_4X8: usize = 1;+pub const BLOCK_64X128: usize = 13;+pub const BLOCK_64X16: usize = 21;+pub const BLOCK_64X32: usize = 11;+pub const BLOCK_64X64: usize = 12;+pub const BLOCK_8X16: usize = 4;+pub const BLOCK_8X32: usize = 18;+pub const BLOCK_8X4: usize = 2;+pub const BLOCK_8X8: usize = 3;+pub const BLOCK_INVALID: usize = 22;+pub const BLOCK_SIZES: usize = 22;+pub const BLOCK_SIZE_GROUPS: usize = 4;+pub const BR_CDF_SIZE: usize = 4;+pub const BUFFER_POOL_MAX_SIZE: usize = 10;+pub const BWDREF_FRAME: usize = 5;+pub const BWD_REFS: usize = 3;+pub const CFL_ALPHABET_SIZE: usize = 16;+pub const CFL_ALPHA_CONTEXTS: usize = 6;+pub const CFL_JOINT_SIGNS: usize = 8;+pub const CFL_SIGN_NEG: usize = 1;+pub const CFL_SIGN_POS: usize = 2;+pub const CFL_SIGN_ZERO: usize = 0;+pub const CLASS0_SIZE: usize = 2;+pub const COEFF_BASE_RANGE: usize = 12;+pub const COEFF_CDF_Q_CTXS: usize = 4;+pub const COMPOUND_AVERAGE: usize = 2;+pub const COMPOUND_DIFFWTD: usize = 1;+pub const COMPOUND_DISTANCE: usize = 4;+pub const COMPOUND_IDX_CONTEXTS: usize = 6;+pub const COMPOUND_INTRA: usize = 3;+pub const COMPOUND_MODES: usize = 8;+pub const COMPOUND_MODE_CONTEXTS: usize = 8;+pub const COMPOUND_REFERENCE: usize = 1;+pub const COMPOUND_TYPES: usize = 2;+pub const COMPOUND_WEDGE: usize = 0;+pub const COMP_GROUP_IDX_CONTEXTS: usize = 6;+pub const COMP_INTER_CONTEXTS: usize = 5;+pub const COMP_NEWMV_CTXS: usize = 5;+pub const COMP_REF_TYPE_CONTEXTS: usize = 5;+pub const CP_BT_2020: usize = 9;+pub const CP_BT_470_B_G: usize = 5;+pub const CP_BT_470_M: usize = 4;+pub const CP_BT_601: usize = 6;+pub const CP_BT_709: usize = 1;+pub const CP_EBU_3213: usize = 22;+pub const CP_GENERIC_FILM: usize = 8;+pub const CP_SMPTE_240: usize = 7;+pub const CP_SMPTE_431: usize = 11;+pub const CP_SMPTE_432: usize = 12;+pub const CP_UNSPECIFIED: usize = 2;+pub const CP_XYZ: usize = 10;+pub const CSP_COLOCATED: usize = 2;+pub const CSP_RESERVED: usize = 3;+pub const CSP_UNKNOWN: usize = 0;+pub const CSP_VERTICAL: usize = 1;+pub const D113_PRED: usize = 5;+pub const D135_PRED: usize = 4;+pub const D157_PRED: usize = 6;+pub const D203_PRED: usize = 7;+pub const D45_PRED: usize = 3;+pub const D67_PRED: usize = 8;+pub const DCT_ADST: usize = 2;+pub const DCT_DCT: usize = 0;+pub const DCT_FLIPADST: usize = 5;+pub const DC_PRED: usize = 0;+pub const DC_SIGN_CONTEXTS: usize = 3;+pub const DELTA_LF_SMALL: usize = 3;+pub const DELTA_Q_SMALL: usize = 3;+pub const DIRECTIONAL_MODES: usize = 8;+pub const DIV_LUT_BITS: usize = 8;+pub const DIV_LUT_NUM: usize = 257;+pub const DIV_LUT_PREC_BITS: usize = 14;+pub const DRL_MODE_CONTEXTS: usize = 3;+pub const EC_MIN_PROB: usize = 4;+pub const EC_PROB_SHIFT: usize = 6;+pub const EIGHTTAP: usize = 0;+pub const EIGHTTAP_SHARP: usize = 2;+pub const EIGHTTAP_SMOOTH: usize = 1;+pub const EOB_COEF_CONTEXTS: usize = 9;+pub const FILTER_BITS: usize = 7;+pub const FILTER_D157_PRED: usize = 3;+pub const FILTER_DC_PRED: usize = 0;+pub const FILTER_H_PRED: usize = 2;+pub const FILTER_PAETH_PRED: usize = 4;+pub const FILTER_V_PRED: usize = 1;+pub const FLIPADST_ADST: usize = 8;+pub const FLIPADST_DCT: usize = 4;+pub const FLIPADST_FLIPADST: usize = 6;+pub const FRAME_LF_COUNT: usize = 4;+pub const FWD_REFS: usize = 4;+pub const GLOBALMV: usize = 16;+pub const GLOBAL_GLOBALMV: usize = 24;+pub const GM_ABS_ALPHA_BITS: usize = 12;+pub const GM_ABS_TRANS_BITS: usize = 12;+pub const GM_ABS_TRANS_ONLY_BITS: usize = 9;+pub const GM_ALPHA_PREC_BITS: usize = 15;+pub const GM_TRANS_ONLY_PREC_BITS: usize = 3;+pub const GM_TRANS_PREC_BITS: usize = 6;+pub const GOLDEN_FRAME: usize = 4;+pub const H_ADST: usize = 13;+pub const H_DCT: usize = 11;+pub const H_FLIPADST: usize = 15;+pub const H_PRED: usize = 2;+pub const IDENTITY: usize = 0;+pub const IDTX: usize = 9;+pub const II_DC_PRED: usize = 0;+pub const II_H_PRED: usize = 2;+pub const II_SMOOTH_PRED: usize = 3;+pub const II_V_PRED: usize = 1;+pub const INTERINTRA_MODES: usize = 4;+pub const INTERP_FILTERS: usize = 3;+pub const INTERP_FILTER_CONTEXTS: usize = 16;+pub const INTER_FRAME: usize = 1;+pub const INTRABC_DELAY_PIXELS: usize = 256;+pub const INTRABC_DELAY_SB64: usize = 4;+pub const INTRA_EDGE_KERNELS: usize = 3;+pub const INTRA_EDGE_TAPS: usize = 5;+pub const INTRA_FILTER_MODES: usize = 5;+pub const INTRA_FILTER_SCALE_BITS: usize = 4;+pub const INTRA_FRAME: usize = 0;+pub const INTRA_MODES: usize = 13;+pub const INTRA_MODE_CONTEXTS: usize = 5;+pub const INTRA_ONLY_FRAME: usize = 2;+pub const IS_INTER_CONTEXTS: usize = 4;+pub const KEY_FRAME: usize = 0;+pub const LAST2_FRAME: usize = 2;+pub const LAST3_FRAME: usize = 3;+pub const LAST_FRAME: usize = 1;+pub const LEAST_SQUARES_SAMPLES_MAX: usize = 8;+pub const LEVEL_CONTEXTS: usize = 21;+pub const LOCALWARP: usize = 2;+pub const LS_MV_MAX: usize = 256;+pub const MASK_MASTER_SIZE: usize = 64;+pub const MAX_ANGLE_DELTA: usize = 3;+pub const MAX_FRAME_DISTANCE: usize = 31;+pub const MAX_LOOP_FILTER: usize = 63;+pub const MAX_OFFSET_HEIGHT: usize = 0;+pub const MAX_OFFSET_WIDTH: usize = 8;+pub const MAX_REF_MV_STACK_SIZE: usize = 8;+pub const MAX_SB_SIZE: usize = 128;+pub const MAX_SEGMENTS: usize = 8;+pub const MAX_TILE_AREA: usize = 9437184;+pub const MAX_TILE_COLS: usize = 64;+pub const MAX_TILE_ROWS: usize = 64;+pub const MAX_TILE_WIDTH: usize = 4096;+pub const MAX_TX_DEPTH: usize = 2;+pub const MAX_VARTX_DEPTH: usize = 2;+pub const MB_MODE_COUNT: usize = 17;+pub const MC_BT_2020_CL: usize = 10;+pub const MC_BT_2020_NCL: usize = 9;+pub const MC_BT_470_B_G: usize = 5;+pub const MC_BT_601: usize = 6;+pub const MC_BT_709: usize = 1;+pub const MC_CHROMAT_CL: usize = 13;+pub const MC_CHROMAT_NCL: usize = 12;+pub const MC_FCC: usize = 4;+pub const MC_ICTCP: usize = 14;+pub const MC_IDENTITY: usize = 0;+pub const MC_RESERVED_3: usize = 3;+pub const MC_SMPTE_2085: usize = 11;+pub const MC_SMPTE_240: usize = 7;+pub const MC_SMPTE_YCGCO: usize = 8;+pub const MC_UNSPECIFIED: usize = 2;+pub const METADATA_TYPE_HDR_CLL: usize = 1;+pub const METADATA_TYPE_HDR_MDCV: usize = 2;+pub const METADATA_TYPE_ITUT_T35: usize = 4;+pub const METADATA_TYPE_SCALABILITY: usize = 3;+pub const METADATA_TYPE_TIMECODE: usize = 5;+pub const MFMV_STACK_SIZE: usize = 3;+pub const MI_SIZE: usize = 4;+pub const MI_SIZE_LOG2: usize = 2;+pub const MOTION_MODES: usize = 3;+pub const MV_BORDER: usize = 128;+pub const MV_CLASSES: usize = 11;+pub const MV_CLASS_0: usize = 0;+pub const MV_CLASS_1: usize = 1;+pub const MV_CLASS_10: usize = 10;+pub const MV_CLASS_2: usize = 2;+pub const MV_CLASS_3: usize = 3;+pub const MV_CLASS_4: usize = 4;+pub const MV_CLASS_5: usize = 5;+pub const MV_CLASS_6: usize = 6;+pub const MV_CLASS_7: usize = 7;+pub const MV_CLASS_8: usize = 8;+pub const MV_CLASS_9: usize = 9;+pub const MV_CONTEXTS: usize = 2;+pub const MV_INTRABC_CONTEXT: usize = 1;+pub const MV_JOINTS: usize = 4;+pub const MV_JOINT_HNZVNZ: usize = 3;+pub const MV_JOINT_HNZVZ: usize = 1;+pub const MV_JOINT_HZVNZ: usize = 2;+pub const MV_JOINT_ZERO: usize = 0;+pub const MV_OFFSET_BITS: usize = 10;+pub const NEARESTMV: usize = 14;+pub const NEAREST_NEARESTMV: usize = 18;+pub const NEAREST_NEWMV: usize = 20;+pub const NEARMV: usize = 15;+pub const NEAR_NEARMV: usize = 19;+pub const NEAR_NEWMV: usize = 22;+pub const NEWMV: usize = 17;+pub const NEW_MV_CONTEXTS: usize = 6;+pub const NEW_NEARESTMV: usize = 21;+pub const NEW_NEARMV: usize = 23;+pub const NEW_NEWMV: usize = 25;+pub const NUM_BASE_LEVELS: usize = 2;+pub const NUM_REF_FRAMES: usize = 8;+pub const OBMC: usize = 1;+pub const OBU_FRAME: usize = 6;+pub const OBU_FRAME_HEADER: usize = 3;+pub const OBU_METADATA: usize = 5;+pub const OBU_PADDING: usize = 15;+pub const OBU_REDUNDANT_FRAME_HEADER: usize = 7;+pub const OBU_SEQUENCE_HEADER: usize = 1;+pub const OBU_TEMPORAL_DELIMITER: usize = 2;+pub const OBU_TILE_GROUP: usize = 4;+pub const OBU_TILE_LIST: usize = 8;+pub const ONLY_4X4: usize = 0;+pub const PAETH_PRED: usize = 12;+pub const PALETTE_BLOCK_SIZE_CONTEXTS: usize = 7;+pub const PALETTE_COLORS: usize = 8;+pub const PALETTE_COLOR_CONTEXTS: usize = 5;+pub const PALETTE_MAX_COLOR_CONTEXT_HASH: usize = 8;+pub const PALETTE_NUM_NEIGHBORS: usize = 3;+pub const PALETTE_SIZES: usize = 7;+pub const PALETTE_UV_MODE_CONTEXTS: usize = 2;+pub const PALETTE_Y_MODE_CONTEXTS: usize = 3;+pub const PARTITION_CONTEXTS: usize = 4;+pub const PARTITION_HORZ: usize = 1;+pub const PARTITION_HORZ_4: usize = 8;+pub const PARTITION_HORZ_A: usize = 4;+pub const PARTITION_HORZ_B: usize = 5;+pub const PARTITION_NONE: usize = 0;+pub const PARTITION_SPLIT: usize = 3;+pub const PARTITION_VERT: usize = 2;+pub const PARTITION_VERT_4: usize = 9;+pub const PARTITION_VERT_A: usize = 6;+pub const PARTITION_VERT_B: usize = 7;+pub const PLANE_TYPES: usize = 2;+pub const PRIMARY_REF_NONE: usize = 7;+pub const QM_TOTAL_SIZE: usize = 3344;+pub const REFMVS_LIMIT: usize = 4095;+pub const REFS_PER_FRAME: usize = 7;+pub const REF_CAT_LEVEL: usize = 640;+pub const REF_CONTEXTS: usize = 3;+pub const REF_MV_CONTEXTS: usize = 6;+pub const REF_SCALE_SHIFT: usize = 14;+pub const RESTORATION_TILESIZE_MAX: usize = 256;+pub const RESTORE_NONE: usize = 0;+pub const RESTORE_SGRPROJ: usize = 2;+pub const RESTORE_SWITCHABLE: usize = 3;+pub const RESTORE_WIENER: usize = 1;+pub const ROTZOOM: usize = 2;+pub const SCALABILITY_L1T2: usize = 0;+pub const SCALABILITY_L1T3: usize = 1;+pub const SCALABILITY_L2T1: usize = 2;+pub const SCALABILITY_L2T2: usize = 3;+pub const SCALABILITY_L2T3: usize = 4;+pub const SCALABILITY_L3T1: usize = 15;+pub const SCALABILITY_L3T2: usize = 16;+pub const SCALABILITY_L3T2_KEY: usize = 21;+pub const SCALABILITY_L3T2_KEY_SHIFT: usize = 25;+pub const SCALABILITY_L3T3: usize = 17;+pub const SCALABILITY_L3T3_KEY: usize = 22;+pub const SCALABILITY_L3T3_KEY_SHIFT: usize = 26;+pub const SCALABILITY_L4T5_KEY: usize = 23;+pub const SCALABILITY_L4T5_KEY_SHIFT: usize = 27;+pub const SCALABILITY_L4T7_KEY: usize = 24;+pub const SCALABILITY_L4T7_KEY_SHIFT: usize = 28;+pub const SCALABILITY_S2T1: usize = 5;+pub const SCALABILITY_S2T2: usize = 6;+pub const SCALABILITY_S2T3: usize = 7;+pub const SCALABILITY_S3T1: usize = 18;+pub const SCALABILITY_S3T2: usize = 19;+pub const SCALABILITY_S3T3: usize = 20;+pub const SCALABILITY_SS: usize = 14;+pub const SCALE_SUBPEL_BITS: usize = 10;+pub const SEGMENT_ID_CONTEXTS: usize = 3;+pub const SEGMENT_ID_PREDICTED_CONTEXTS: usize = 3;+pub const SEG_LVL_ALT_LF_Y_V: usize = 1;+pub const SEG_LVL_ALT_Q: usize = 0;+pub const SEG_LVL_GLOBALMV: usize = 7;+pub const SEG_LVL_MAX: usize = 8;+pub const SEG_LVL_REF_FRAME: usize = 5;+pub const SEG_LVL_SKIP: usize = 6;+pub const SELECT_INTEGER_MV: usize = 2;+pub const SELECT_SCREEN_CONTENT_TOOLS: usize = 2;+pub const SGRPROJ_MTABLE_BITS: usize = 20;+pub const SGRPROJ_PARAMS_BITS: usize = 4;+pub const SGRPROJ_PRJ_BITS: usize = 7;+pub const SGRPROJ_PRJ_SUBEXP_K: usize = 4;+pub const SGRPROJ_RECIP_BITS: usize = 12;+pub const SGRPROJ_RST_BITS: usize = 4;+pub const SGRPROJ_SGR_BITS: usize = 8;+pub const SIG_COEF_CONTEXTS: usize = 42;+pub const SIG_COEF_CONTEXTS_2D: usize = 26;+pub const SIG_COEF_CONTEXTS_EOB: usize = 4;+pub const SIG_REF_DIFF_OFFSET_NUM: usize = 5;+pub const SIMPLE: usize = 0;+pub const SINGLE_REFERENCE: usize = 0;+pub const SINGLE_REFS: usize = 7;+pub const SINPI_1_9: usize = 1321;+pub const SINPI_2_9: usize = 2482;+pub const SINPI_3_9: usize = 3344;+pub const SINPI_4_9: usize = 3803;+pub const SKIP_CONTEXTS: usize = 3;+pub const SKIP_MODE_CONTEXTS: usize = 3;+pub const SMOOTH_H_PRED: usize = 11;+pub const SMOOTH_PRED: usize = 9;+pub const SMOOTH_V_PRED: usize = 10;+pub const SUBPEL_BITS: usize = 4;+pub const SUBPEL_MASK: usize = 15;+pub const SUPERRES_DENOM_BITS: usize = 3;+pub const SUPERRES_DENOM_MIN: usize = 9;+pub const SUPERRES_EXTRA_BITS: usize = 8;+pub const SUPERRES_FILTER_BITS: usize = 6;+pub const SUPERRES_FILTER_OFFSET: usize = 3;+pub const SUPERRES_FILTER_SHIFTS: usize = 64;+pub const SUPERRES_FILTER_TAPS: usize = 8;+pub const SUPERRES_NUM: usize = 8;+pub const SUPERRES_SCALE_BITS: usize = 14;+pub const SUPERRES_SCALE_MASK: usize = 16383;+pub const SWITCHABLE: usize = 4;+pub const SWITCH_FRAME: usize = 3;+pub const TC_BT_1361: usize = 12;+pub const TC_BT_2020_10_BIT: usize = 14;+pub const TC_BT_2020_12_BIT: usize = 15;+pub const TC_BT_470_B_G: usize = 5;+pub const TC_BT_470_M: usize = 4;+pub const TC_BT_601: usize = 6;+pub const TC_BT_709: usize = 1;+pub const TC_HLG: usize = 18;+pub const TC_IEC_61966: usize = 11;+pub const TC_LINEAR: usize = 8;+pub const TC_LOG_100: usize = 9;+pub const TC_LOG_100_SQRT10: usize = 10;+pub const TC_RESERVED_0: usize = 0;+pub const TC_RESERVED_3: usize = 3;+pub const TC_SMPTE_2084: usize = 16;+pub const TC_SMPTE_240: usize = 7;+pub const TC_SMPTE_428: usize = 17;+pub const TC_SRGB: usize = 13;+pub const TC_UNSPECIFIED: usize = 2;+pub const TOTAL_REFS_PER_FRAME: usize = 8;+pub const TRANSLATION: usize = 1;+pub const TXB_SKIP_CONTEXTS: usize = 13;+pub const TXFM_PARTITION_CONTEXTS: usize = 21;+pub const TX_16X16: usize = 2;+pub const TX_16X32: usize = 9;+pub const TX_16X4: usize = 14;+pub const TX_16X64: usize = 17;+pub const TX_16X8: usize = 8;+pub const TX_32X16: usize = 10;+pub const TX_32X32: usize = 3;+pub const TX_32X64: usize = 11;+pub const TX_32X8: usize = 16;+pub const TX_4X16: usize = 13;+pub const TX_4X4: usize = 0;+pub const TX_4X8: usize = 5;+pub const TX_64X16: usize = 18;+pub const TX_64X32: usize = 12;+pub const TX_64X64: usize = 4;+pub const TX_8X16: usize = 7;+pub const TX_8X32: usize = 15;+pub const TX_8X4: usize = 6;+pub const TX_8X8: usize = 1;+pub const TX_CLASS_2D: usize = 0;+pub const TX_CLASS_HORIZ: usize = 1;+pub const TX_CLASS_VERT: usize = 2;+pub const TX_MODES: usize = 3;+pub const TX_MODE_LARGEST: usize = 1;+pub const TX_MODE_SELECT: usize = 2;+pub const TX_SET_DCTONLY: usize = 0;+pub const TX_SET_INTER_1: usize = 1;+pub const TX_SET_INTER_2: usize = 2;+pub const TX_SET_INTER_3: usize = 3;+pub const TX_SET_INTRA_1: usize = 1;+pub const TX_SET_INTRA_2: usize = 2;+pub const TX_SET_TYPES_INTER: usize = 4;+pub const TX_SET_TYPES_INTRA: usize = 3;+pub const TX_SIZES: usize = 5;+pub const TX_SIZES_ALL: usize = 19;+pub const TX_SIZE_CONTEXTS: usize = 3;+pub const TX_TYPES: usize = 16;+pub const UNIDIR_COMP_REFERENCE: usize = 0;+pub const UNIDIR_COMP_REFS: usize = 4;+pub const UNIFORM_45: usize = 0;+pub const UNIFORM_45_INV: usize = 1;+pub const UV_CFL_PRED: usize = 13;+pub const UV_INTRA_MODES_CFL_ALLOWED: usize = 14;+pub const UV_INTRA_MODES_CFL_NOT_ALLOWED: usize = 13;+pub const V_ADST: usize = 12;+pub const V_DCT: usize = 10;+pub const V_FLIPADST: usize = 14;+pub const V_PRED: usize = 1;+pub const WARPEDDIFF_PREC_BITS: usize = 10;+pub const WARPEDMODEL_NONDIAGAFFINE_CLAMP: usize = 8192;+pub const WARPEDMODEL_PREC_BITS: usize = 16;+pub const WARPEDMODEL_TRANS_CLAMP: usize = 8388608;+pub const WARPEDPIXEL_PREC_SHIFTS: usize = 64;+pub const WARP_PARAM_REDUCE_BITS: usize = 6;+pub const WEDGE_HORIZONTAL: usize = 0;+pub const WEDGE_OBLIQUE117: usize = 4;+pub const WEDGE_OBLIQUE153: usize = 5;+pub const WEDGE_OBLIQUE27: usize = 2;+pub const WEDGE_OBLIQUE63: usize = 3;+pub const WEDGE_TYPES: usize = 16;+pub const WEDGE_VERTICAL: usize = 1;+pub const WIENER_COEFFS: usize = 3;+pub const ZERO_MV_CONTEXTS: usize = 2;++pub static AC_QLOOKUP: [[u16; 256]; 3] = [+ [+ 4, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,+ 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,+ 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128,+ 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 155, 158, 161, 164, 167, 170, 173, 176, 179, 182, 185, 188, 191, 194, 197, 200, 203, 207,+ 211, 215, 219, 223, 227, 231, 235, 239, 243, 247, 251, 255, 260, 265, 270, 275, 280, 285, 290, 295, 300, 305, 311, 317, 323, 329, 335, 341, 347, 353,+ 359, 366, 373, 380, 387, 394, 401, 408, 416, 424, 432, 440, 448, 456, 465, 474, 483, 492, 501, 510, 520, 530, 540, 550, 560, 571, 582, 593, 604, 615,+ 627, 639, 651, 663, 676, 689, 702, 715, 729, 743, 757, 771, 786, 801, 816, 832, 848, 864, 881, 898, 915, 933, 951, 969, 988, 1007, 1026, 1046, 1066,+ 1087, 1108, 1129, 1151, 1173, 1196, 1219, 1243, 1267, 1292, 1317, 1343, 1369, 1396, 1423, 1451, 1479, 1508, 1537, 1567, 1597, 1628, 1660, 1692, 1725,+ 1759, 1793, 1828,+ ],+ [+ 4, 9, 11, 13, 16, 18, 21, 24, 27, 30, 33, 37, 40, 44, 48, 51, 55, 59, 63, 67, 71, 75, 79, 83, 88, 92, 96, 100, 105, 109, 114, 118, 122, 127, 131, 136,+ 140, 145, 149, 154, 158, 163, 168, 172, 177, 181, 186, 190, 195, 199, 204, 208, 213, 217, 222, 226, 231, 235, 240, 244, 249, 253, 258, 262, 267, 271,+ 275, 280, 284, 289, 293, 297, 302, 306, 311, 315, 319, 324, 328, 332, 337, 341, 345, 349, 354, 358, 362, 367, 371, 375, 379, 384, 388, 392, 396, 401,+ 409, 417, 425, 433, 441, 449, 458, 466, 474, 482, 490, 498, 506, 514, 523, 531, 539, 547, 555, 563, 571, 579, 588, 596, 604, 616, 628, 640, 652, 664,+ 676, 688, 700, 713, 725, 737, 749, 761, 773, 785, 797, 809, 825, 841, 857, 873, 889, 905, 922, 938, 954, 970, 986, 1002, 1018, 1038, 1058, 1078, 1098,+ 1118, 1138, 1158, 1178, 1198, 1218, 1242, 1266, 1290, 1314, 1338, 1362, 1386, 1411, 1435, 1463, 1491, 1519, 1547, 1575, 1603, 1631, 1663, 1695, 1727,+ 1759, 1791, 1823, 1859, 1895, 1931, 1967, 2003, 2039, 2079, 2119, 2159, 2199, 2239, 2283, 2327, 2371, 2415, 2459, 2507, 2555, 2603, 2651, 2703, 2755,+ 2807, 2859, 2915, 2971, 3027, 3083, 3143, 3203, 3263, 3327, 3391, 3455, 3523, 3591, 3659, 3731, 3803, 3876, 3952, 4028, 4104, 4184, 4264, 4348, 4432,+ 4516, 4604, 4692, 4784, 4876, 4972, 5068, 5168, 5268, 5372, 5476, 5584, 5692, 5804, 5916, 6032, 6148, 6268, 6388, 6512, 6640, 6768, 6900, 7036, 7172,+ 7312,+ ],+ [+ 4, 13, 19, 27, 35, 44, 54, 64, 75, 87, 99, 112, 126, 139, 154, 168, 183, 199, 214, 230, 247, 263, 280, 297, 314, 331, 349, 366, 384, 402, 420, 438,+ 456, 475, 493, 511, 530, 548, 567, 586, 604, 623, 642, 660, 679, 698, 716, 735, 753, 772, 791, 809, 828, 846, 865, 884, 902, 920, 939, 957, 976, 994,+ 1012, 1030, 1049, 1067, 1085, 1103, 1121, 1139, 1157, 1175, 1193, 1211, 1229, 1246, 1264, 1282, 1299, 1317, 1335, 1352, 1370, 1387, 1405, 1422, 1440,+ 1457, 1474, 1491, 1509, 1526, 1543, 1560, 1577, 1595, 1627, 1660, 1693, 1725, 1758, 1791, 1824, 1856, 1889, 1922, 1954, 1987, 2020, 2052, 2085, 2118,+ 2150, 2183, 2216, 2248, 2281, 2313, 2346, 2378, 2411, 2459, 2508, 2556, 2605, 2653, 2701, 2750, 2798, 2847, 2895, 2943, 2992, 3040, 3088, 3137, 3185,+ 3234, 3298, 3362, 3426, 3491, 3555, 3619, 3684, 3748, 3812, 3876, 3941, 4005, 4069, 4149, 4230, 4310, 4390, 4470, 4550, 4631, 4711, 4791, 4871, 4967,+ 5064, 5160, 5256, 5352, 5448, 5544, 5641, 5737, 5849, 5961, 6073, 6185, 6297, 6410, 6522, 6650, 6778, 6906, 7034, 7162, 7290, 7435, 7579, 7723, 7867,+ 8011, 8155, 8315, 8475, 8635, 8795, 8956, 9132, 9308, 9484, 9660, 9836, 10028, 10220, 10412, 10604, 10812, 11020, 11228, 11437, 11661, 11885, 12109,+ 12333, 12573, 12813, 13053, 13309, 13565, 13821, 14093, 14365, 14637, 14925, 15213, 15502, 15806, 16110, 16414, 16734, 17054, 17390, 17726, 18062,+ 18414, 18766, 19134, 19502, 19886, 20270, 20670, 21070, 21486, 21902, 22334, 22766, 23214, 23662, 24126, 24590, 25070, 25551, 26047, 26559, 27071,+ 27599, 28143, 28687, 29247,+ ],+];++pub static ADJUSTED_TX_SIZE: [u8; 19] = [0, 1, 2, 3, 3, 5, 6, 7, 8, 9, 10, 3, 3, 13, 14, 15, 16, 9, 10];++pub static COEFF_BASE_CTX_OFFSET: [[[u8; 5]; 5]; 19] = [+ [[0, 1, 6, 6, 0], [1, 6, 6, 21, 0], [6, 6, 21, 21, 0], [6, 21, 21, 21, 0], [0, 0, 0, 0, 0]],+ [[0, 1, 6, 6, 21], [1, 6, 6, 21, 21], [6, 6, 21, 21, 21], [6, 21, 21, 21, 21], [21, 21, 21, 21, 21]],+ [[0, 1, 6, 6, 21], [1, 6, 6, 21, 21], [6, 6, 21, 21, 21], [6, 21, 21, 21, 21], [21, 21, 21, 21, 21]],+ [[0, 1, 6, 6, 21], [1, 6, 6, 21, 21], [6, 6, 21, 21, 21], [6, 21, 21, 21, 21], [21, 21, 21, 21, 21]],+ [[0, 1, 6, 6, 21], [1, 6, 6, 21, 21], [6, 6, 21, 21, 21], [6, 21, 21, 21, 21], [21, 21, 21, 21, 21]],+ [[0, 11, 11, 11, 0], [11, 11, 11, 11, 0], [6, 6, 21, 21, 0], [6, 21, 21, 21, 0], [21, 21, 21, 21, 0]],+ [[0, 16, 6, 6, 21], [16, 16, 6, 21, 21], [16, 16, 21, 21, 21], [16, 16, 21, 21, 21], [0, 0, 0, 0, 0]],+ [[0, 11, 11, 11, 11], [11, 11, 11, 11, 11], [6, 6, 21, 21, 21], [6, 21, 21, 21, 21], [21, 21, 21, 21, 21]],+ [[0, 16, 6, 6, 21], [16, 16, 6, 21, 21], [16, 16, 21, 21, 21], [16, 16, 21, 21, 21], [16, 16, 21, 21, 21]],+ [[0, 11, 11, 11, 11], [11, 11, 11, 11, 11], [6, 6, 21, 21, 21], [6, 21, 21, 21, 21], [21, 21, 21, 21, 21]],+ [[0, 16, 6, 6, 21], [16, 16, 6, 21, 21], [16, 16, 21, 21, 21], [16, 16, 21, 21, 21], [16, 16, 21, 21, 21]],+ [[0, 11, 11, 11, 11], [11, 11, 11, 11, 11], [6, 6, 21, 21, 21], [6, 21, 21, 21, 21], [21, 21, 21, 21, 21]],+ [[0, 16, 6, 6, 21], [16, 16, 6, 21, 21], [16, 16, 21, 21, 21], [16, 16, 21, 21, 21], [16, 16, 21, 21, 21]],+ [[0, 11, 11, 11, 0], [11, 11, 11, 11, 0], [6, 6, 21, 21, 0], [6, 21, 21, 21, 0], [21, 21, 21, 21, 0]],+ [[0, 16, 6, 6, 21], [16, 16, 6, 21, 21], [16, 16, 21, 21, 21], [16, 16, 21, 21, 21], [0, 0, 0, 0, 0]],+ [[0, 11, 11, 11, 11], [11, 11, 11, 11, 11], [6, 6, 21, 21, 21], [6, 21, 21, 21, 21], [21, 21, 21, 21, 21]],+ [[0, 16, 6, 6, 21], [16, 16, 6, 21, 21], [16, 16, 21, 21, 21], [16, 16, 21, 21, 21], [16, 16, 21, 21, 21]],+ [[0, 11, 11, 11, 11], [11, 11, 11, 11, 11], [6, 6, 21, 21, 21], [6, 21, 21, 21, 21], [21, 21, 21, 21, 21]],+ [[0, 16, 6, 6, 21], [16, 16, 6, 21, 21], [16, 16, 21, 21, 21], [16, 16, 21, 21, 21], [16, 16, 21, 21, 21]],+];++pub static COEFF_BASE_POS_CTX_OFFSET: [u8; 3] = [26, 31, 36];++pub static COS128_LOOKUP: [u16; 65] = [+ 4096, 4095, 4091, 4085, 4076, 4065, 4052, 4036, 4017, 3996, 3973, 3948, 3920, 3889, 3857, 3822, 3784, 3745, 3703, 3659, 3612, 3564, 3513, 3461, 3406, 3349,+ 3290, 3229, 3166, 3102, 3035, 2967, 2896, 2824, 2751, 2675, 2598, 2520, 2440, 2359, 2276, 2191, 2106, 2019, 1931, 1842, 1751, 1660, 1567, 1474, 1380, 1285,+ 1189, 1092, 995, 897, 799, 700, 601, 501, 401, 301, 201, 101, 0,+];++pub static DC_QLOOKUP: [[u16; 256]; 3] = [+ [+ 4, 8, 8, 9, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 23, 24, 25, 26, 26, 27, 28, 29, 30, 31, 32, 32, 33, 34, 35, 36, 37, 38, 38, 39,+ 40, 41, 42, 43, 43, 44, 45, 46, 47, 48, 48, 49, 50, 51, 52, 53, 53, 54, 55, 56, 57, 57, 58, 59, 60, 61, 62, 62, 63, 64, 65, 66, 66, 67, 68, 69, 70, 70,+ 71, 72, 73, 74, 74, 75, 76, 77, 78, 78, 79, 80, 81, 81, 82, 83, 84, 85, 85, 87, 88, 90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 108, 110, 111,+ 113, 114, 116, 117, 118, 120, 121, 123, 125, 127, 129, 131, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 161, 164, 166, 169, 172,+ 174, 177, 180, 182, 185, 187, 190, 192, 195, 199, 202, 205, 208, 211, 214, 217, 220, 223, 226, 230, 233, 237, 240, 243, 247, 250, 253, 257, 261, 265,+ 269, 272, 276, 280, 284, 288, 292, 296, 300, 304, 309, 313, 317, 322, 326, 330, 335, 340, 344, 349, 354, 359, 364, 369, 374, 379, 384, 389, 395, 400,+ 406, 411, 417, 423, 429, 435, 441, 447, 454, 461, 467, 475, 482, 489, 497, 505, 513, 522, 530, 539, 549, 559, 569, 579, 590, 602, 614, 626, 640, 654,+ 668, 684, 700, 717, 736, 755, 775, 796, 819, 843, 869, 896, 925, 955, 988, 1022, 1058, 1098, 1139, 1184, 1232, 1282, 1336,+ ],+ [+ 4, 9, 10, 13, 15, 17, 20, 22, 25, 28, 31, 34, 37, 40, 43, 47, 50, 53, 57, 60, 64, 68, 71, 75, 78, 82, 86, 90, 93, 97, 101, 105, 109, 113, 116, 120,+ 124, 128, 132, 136, 140, 143, 147, 151, 155, 159, 163, 166, 170, 174, 178, 182, 185, 189, 193, 197, 200, 204, 208, 212, 215, 219, 223, 226, 230, 233,+ 237, 241, 244, 248, 251, 255, 259, 262, 266, 269, 273, 276, 280, 283, 287, 290, 293, 297, 300, 304, 307, 310, 314, 317, 321, 324, 327, 331, 334, 337,+ 343, 350, 356, 362, 369, 375, 381, 387, 394, 400, 406, 412, 418, 424, 430, 436, 442, 448, 454, 460, 466, 472, 478, 484, 490, 499, 507, 516, 525, 533,+ 542, 550, 559, 567, 576, 584, 592, 601, 609, 617, 625, 634, 644, 655, 666, 676, 687, 698, 708, 718, 729, 739, 749, 759, 770, 782, 795, 807, 819, 831,+ 844, 856, 868, 880, 891, 906, 920, 933, 947, 961, 975, 988, 1001, 1015, 1030, 1045, 1061, 1076, 1090, 1105, 1120, 1137, 1153, 1170, 1186, 1202, 1218,+ 1236, 1253, 1271, 1288, 1306, 1323, 1342, 1361, 1379, 1398, 1416, 1436, 1456, 1476, 1496, 1516, 1537, 1559, 1580, 1601, 1624, 1647, 1670, 1692, 1717,+ 1741, 1766, 1791, 1817, 1844, 1871, 1900, 1929, 1958, 1990, 2021, 2054, 2088, 2123, 2159, 2197, 2236, 2276, 2319, 2363, 2410, 2458, 2508, 2561, 2616,+ 2675, 2737, 2802, 2871, 2944, 3020, 3102, 3188, 3280, 3375, 3478, 3586, 3702, 3823, 3953, 4089, 4236, 4394, 4559, 4737, 4929, 5130, 5347,+ ],+ [+ 4, 12, 18, 25, 33, 41, 50, 60, 70, 80, 91, 103, 115, 127, 140, 153, 166, 180, 194, 208, 222, 237, 251, 266, 281, 296, 312, 327, 343, 358, 374, 390,+ 405, 421, 437, 453, 469, 484, 500, 516, 532, 548, 564, 580, 596, 611, 627, 643, 659, 674, 690, 706, 721, 737, 752, 768, 783, 798, 814, 829, 844, 859,+ 874, 889, 904, 919, 934, 949, 964, 978, 993, 1008, 1022, 1037, 1051, 1065, 1080, 1094, 1108, 1122, 1136, 1151, 1165, 1179, 1192, 1206, 1220, 1234,+ 1248, 1261, 1275, 1288, 1302, 1315, 1329, 1342, 1368, 1393, 1419, 1444, 1469, 1494, 1519, 1544, 1569, 1594, 1618, 1643, 1668, 1692, 1717, 1741, 1765,+ 1789, 1814, 1838, 1862, 1885, 1909, 1933, 1957, 1992, 2027, 2061, 2096, 2130, 2165, 2199, 2233, 2267, 2300, 2334, 2367, 2400, 2434, 2467, 2499, 2532,+ 2575, 2618, 2661, 2704, 2746, 2788, 2830, 2872, 2913, 2954, 2995, 3036, 3076, 3127, 3177, 3226, 3275, 3324, 3373, 3421, 3469, 3517, 3565, 3621, 3677,+ 3733, 3788, 3843, 3897, 3951, 4005, 4058, 4119, 4181, 4241, 4301, 4361, 4420, 4479, 4546, 4612, 4677, 4742, 4807, 4871, 4942, 5013, 5083, 5153, 5222,+ 5291, 5367, 5442, 5517, 5591, 5665, 5745, 5825, 5905, 5984, 6063, 6149, 6234, 6319, 6404, 6495, 6587, 6678, 6769, 6867, 6966, 7064, 7163, 7269, 7376,+ 7483, 7599, 7715, 7832, 7958, 8085, 8214, 8352, 8492, 8635, 8788, 8945, 9104, 9275, 9450, 9639, 9832, 10031, 10245, 10465, 10702, 10946, 11210, 11482,+ 11776, 12081, 12409, 12750, 13118, 13501, 13913, 14343, 14807, 15290, 15812, 16356, 16943, 17575, 18237, 18949, 19718, 20521, 21387,+ ],+];++pub static DEFAULT_ANGLE_DELTA_CDF: [[u16; 8]; 8] = [+ [2180, 5032, 7567, 22776, 26989, 30217, 32768, 0],+ [2301, 5608, 8801, 23487, 26974, 30330, 32768, 0],+ [3780, 11018, 13699, 19354, 23083, 31286, 32768, 0],+ [4581, 11226, 15147, 17138, 21834, 28397, 32768, 0],+ [1737, 10927, 14509, 19588, 22745, 28823, 32768, 0],+ [2664, 10176, 12485, 17650, 21600, 30495, 32768, 0],+ [2240, 11096, 15453, 20341, 22561, 28917, 32768, 0],+ [3605, 10428, 12459, 17676, 21244, 30655, 32768, 0],+];++pub static DEFAULT_COEFF_BASE_CDF: [[[[[u16; 5]; 42]; 2]; 5]; 4] = [+ [+ [+ [+ [4034, 8930, 12727, 32768, 0],+ [18082, 29741, 31877, 32768, 0],+ [12596, 26124, 30493, 32768, 0],+ [9446, 21118, 27005, 32768, 0],+ [6308, 15141, 21279, 32768, 0],+ [2463, 6357, 9783, 32768, 0],+ [20667, 30546, 31929, 32768, 0],+ [13043, 26123, 30134, 32768, 0],+ [8151, 18757, 24778, 32768, 0],+ [5255, 12839, 18632, 32768, 0],+ [2820, 7206, 11161, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [15736, 27553, 30604, 32768, 0],+ [11210, 23794, 28787, 32768, 0],+ [5947, 13874, 19701, 32768, 0],+ [4215, 9323, 13891, 32768, 0],+ [2833, 6462, 10059, 32768, 0],+ [19605, 30393, 31582, 32768, 0],+ [13523, 26252, 30248, 32768, 0],+ [8446, 18622, 24512, 32768, 0],+ [3818, 10343, 15974, 32768, 0],+ [1481, 4117, 6796, 32768, 0],+ [22649, 31302, 32190, 32768, 0],+ [14829, 27127, 30449, 32768, 0],+ [8313, 17702, 23304, 32768, 0],+ [3022, 8301, 12786, 32768, 0],+ [1536, 4412, 7184, 32768, 0],+ [22354, 29774, 31372, 32768, 0],+ [14723, 25472, 29214, 32768, 0],+ [6673, 13745, 18662, 32768, 0],+ [2068, 5766, 9322, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [6302, 16444, 21761, 32768, 0],+ [23040, 31538, 32475, 32768, 0],+ [15196, 28452, 31496, 32768, 0],+ [10020, 22946, 28514, 32768, 0],+ [6533, 16862, 23501, 32768, 0],+ [3538, 9816, 15076, 32768, 0],+ [24444, 31875, 32525, 32768, 0],+ [15881, 28924, 31635, 32768, 0],+ [9922, 22873, 28466, 32768, 0],+ [6527, 16966, 23691, 32768, 0],+ [4114, 11303, 17220, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [20201, 30770, 32209, 32768, 0],+ [14754, 28071, 31258, 32768, 0],+ [8378, 20186, 26517, 32768, 0],+ [5916, 15299, 21978, 32768, 0],+ [4268, 11583, 17901, 32768, 0],+ [24361, 32025, 32581, 32768, 0],+ [18673, 30105, 31943, 32768, 0],+ [10196, 22244, 27576, 32768, 0],+ [5495, 14349, 20417, 32768, 0],+ [2676, 7415, 11498, 32768, 0],+ [24678, 31958, 32585, 32768, 0],+ [18629, 29906, 31831, 32768, 0],+ [9364, 20724, 26315, 32768, 0],+ [4641, 12318, 18094, 32768, 0],+ [2758, 7387, 11579, 32768, 0],+ [25433, 31842, 32469, 32768, 0],+ [18795, 29289, 31411, 32768, 0],+ [7644, 17584, 23592, 32768, 0],+ [3408, 9014, 15047, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [4536, 10072, 14001, 32768, 0],+ [25459, 31416, 32206, 32768, 0],+ [16605, 28048, 30818, 32768, 0],+ [11008, 22857, 27719, 32768, 0],+ [6915, 16268, 22315, 32768, 0],+ [2625, 6812, 10537, 32768, 0],+ [24257, 31788, 32499, 32768, 0],+ [16880, 29454, 31879, 32768, 0],+ [11958, 25054, 29778, 32768, 0],+ [7916, 18718, 25084, 32768, 0],+ [3383, 8777, 13446, 32768, 0],+ [22720, 31603, 32393, 32768, 0],+ [14960, 28125, 31335, 32768, 0],+ [9731, 22210, 27928, 32768, 0],+ [6304, 15832, 22277, 32768, 0],+ [2910, 7818, 12166, 32768, 0],+ [20375, 30627, 32131, 32768, 0],+ [13904, 27284, 30887, 32768, 0],+ [9368, 21558, 27144, 32768, 0],+ [5937, 14966, 21119, 32768, 0],+ [2667, 7225, 11319, 32768, 0],+ [23970, 31470, 32378, 32768, 0],+ [17173, 29734, 32018, 32768, 0],+ [12795, 25441, 29965, 32768, 0],+ [8981, 19680, 25893, 32768, 0],+ [4728, 11372, 16902, 32768, 0],+ [24287, 31797, 32439, 32768, 0],+ [16703, 29145, 31696, 32768, 0],+ [10833, 23554, 28725, 32768, 0],+ [6468, 16566, 23057, 32768, 0],+ [2415, 6562, 10278, 32768, 0],+ [26610, 32395, 32659, 32768, 0],+ [18590, 30498, 32117, 32768, 0],+ [12420, 25756, 29950, 32768, 0],+ [7639, 18746, 24710, 32768, 0],+ [3001, 8086, 12347, 32768, 0],+ [25076, 32064, 32580, 32768, 0],+ [17946, 30128, 32028, 32768, 0],+ [12024, 24985, 29378, 32768, 0],+ [7517, 18390, 24304, 32768, 0],+ [3243, 8781, 13331, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [6037, 16771, 21957, 32768, 0],+ [24774, 31704, 32426, 32768, 0],+ [16830, 28589, 31056, 32768, 0],+ [10602, 22828, 27760, 32768, 0],+ [6733, 16829, 23071, 32768, 0],+ [3250, 8914, 13556, 32768, 0],+ [25582, 32220, 32668, 32768, 0],+ [18659, 30342, 32223, 32768, 0],+ [12546, 26149, 30515, 32768, 0],+ [8420, 20451, 26801, 32768, 0],+ [4636, 12420, 18344, 32768, 0],+ [27581, 32362, 32639, 32768, 0],+ [18987, 30083, 31978, 32768, 0],+ [11327, 24248, 29084, 32768, 0],+ [7264, 17719, 24120, 32768, 0],+ [3995, 10768, 16169, 32768, 0],+ [25893, 31831, 32487, 32768, 0],+ [16577, 28587, 31379, 32768, 0],+ [10189, 22748, 28182, 32768, 0],+ [6832, 17094, 23556, 32768, 0],+ [3708, 10110, 15334, 32768, 0],+ [25904, 32282, 32656, 32768, 0],+ [19721, 30792, 32276, 32768, 0],+ [12819, 26243, 30411, 32768, 0],+ [8572, 20614, 26891, 32768, 0],+ [5364, 14059, 20467, 32768, 0],+ [26580, 32438, 32677, 32768, 0],+ [20852, 31225, 32340, 32768, 0],+ [12435, 25700, 29967, 32768, 0],+ [8691, 20825, 26976, 32768, 0],+ [4446, 12209, 17269, 32768, 0],+ [27350, 32429, 32696, 32768, 0],+ [21372, 30977, 32272, 32768, 0],+ [12673, 25270, 29853, 32768, 0],+ [9208, 20925, 26640, 32768, 0],+ [5018, 13351, 18732, 32768, 0],+ [27351, 32479, 32713, 32768, 0],+ [21398, 31209, 32387, 32768, 0],+ [12162, 25047, 29842, 32768, 0],+ [7896, 18691, 25319, 32768, 0],+ [4670, 12882, 18881, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [5487, 10460, 13708, 32768, 0],+ [21597, 28303, 30674, 32768, 0],+ [11037, 21953, 26476, 32768, 0],+ [8147, 17962, 22952, 32768, 0],+ [5242, 13061, 18532, 32768, 0],+ [1889, 5208, 8182, 32768, 0],+ [26774, 32133, 32590, 32768, 0],+ [17844, 29564, 31767, 32768, 0],+ [11690, 24438, 29171, 32768, 0],+ [7542, 18215, 24459, 32768, 0],+ [2993, 8050, 12319, 32768, 0],+ [28023, 32328, 32591, 32768, 0],+ [18651, 30126, 31954, 32768, 0],+ [12164, 25146, 29589, 32768, 0],+ [7762, 18530, 24771, 32768, 0],+ [3492, 9183, 13920, 32768, 0],+ [27591, 32008, 32491, 32768, 0],+ [17149, 28853, 31510, 32768, 0],+ [11485, 24003, 28860, 32768, 0],+ [7697, 18086, 24210, 32768, 0],+ [3075, 7999, 12218, 32768, 0],+ [28268, 32482, 32654, 32768, 0],+ [19631, 31051, 32404, 32768, 0],+ [13860, 27260, 31020, 32768, 0],+ [9605, 21613, 27594, 32768, 0],+ [4876, 12162, 17908, 32768, 0],+ [27248, 32316, 32576, 32768, 0],+ [18955, 30457, 32075, 32768, 0],+ [11824, 23997, 28795, 32768, 0],+ [7346, 18196, 24647, 32768, 0],+ [3403, 9247, 14111, 32768, 0],+ [29711, 32655, 32735, 32768, 0],+ [21169, 31394, 32417, 32768, 0],+ [13487, 27198, 30957, 32768, 0],+ [8828, 21683, 27614, 32768, 0],+ [4270, 11451, 17038, 32768, 0],+ [28708, 32578, 32731, 32768, 0],+ [20120, 31241, 32482, 32768, 0],+ [13692, 27550, 31321, 32768, 0],+ [9418, 22514, 28439, 32768, 0],+ [4999, 13283, 19462, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [5673, 14302, 19711, 32768, 0],+ [26251, 30701, 31834, 32768, 0],+ [12782, 23783, 27803, 32768, 0],+ [9127, 20657, 25808, 32768, 0],+ [6368, 16208, 21462, 32768, 0],+ [2465, 7177, 10822, 32768, 0],+ [29961, 32563, 32719, 32768, 0],+ [18318, 29891, 31949, 32768, 0],+ [11361, 24514, 29357, 32768, 0],+ [7900, 19603, 25607, 32768, 0],+ [4002, 10590, 15546, 32768, 0],+ [29637, 32310, 32595, 32768, 0],+ [18296, 29913, 31809, 32768, 0],+ [10144, 21515, 26871, 32768, 0],+ [5358, 14322, 20394, 32768, 0],+ [3067, 8362, 13346, 32768, 0],+ [28652, 32470, 32676, 32768, 0],+ [17538, 30771, 32209, 32768, 0],+ [13924, 26882, 30494, 32768, 0],+ [10496, 22837, 27869, 32768, 0],+ [7236, 16396, 21621, 32768, 0],+ [30743, 32687, 32746, 32768, 0],+ [23006, 31676, 32489, 32768, 0],+ [14494, 27828, 31120, 32768, 0],+ [10174, 22801, 28352, 32768, 0],+ [6242, 15281, 21043, 32768, 0],+ [25817, 32243, 32720, 32768, 0],+ [18618, 31367, 32325, 32768, 0],+ [13997, 28318, 31878, 32768, 0],+ [12255, 26534, 31383, 32768, 0],+ [9561, 21588, 28450, 32768, 0],+ [28188, 32635, 32724, 32768, 0],+ [22060, 32365, 32728, 32768, 0],+ [18102, 30690, 32528, 32768, 0],+ [14196, 28864, 31999, 32768, 0],+ [12262, 25792, 30865, 32768, 0],+ [24176, 32109, 32628, 32768, 0],+ [18280, 29681, 31963, 32768, 0],+ [10205, 23703, 29664, 32768, 0],+ [7889, 20025, 27676, 32768, 0],+ [6060, 16743, 23970, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [5141, 7096, 8260, 32768, 0],+ [27186, 29022, 29789, 32768, 0],+ [6668, 12568, 15682, 32768, 0],+ [2172, 6181, 8638, 32768, 0],+ [1126, 3379, 4531, 32768, 0],+ [443, 1361, 2254, 32768, 0],+ [26083, 31153, 32436, 32768, 0],+ [13486, 24603, 28483, 32768, 0],+ [6508, 14840, 19910, 32768, 0],+ [3386, 8800, 13286, 32768, 0],+ [1530, 4322, 7054, 32768, 0],+ [29639, 32080, 32548, 32768, 0],+ [15897, 27552, 30290, 32768, 0],+ [8588, 20047, 25383, 32768, 0],+ [4889, 13339, 19269, 32768, 0],+ [2240, 6871, 10498, 32768, 0],+ [28165, 32197, 32517, 32768, 0],+ [20735, 30427, 31568, 32768, 0],+ [14325, 24671, 27692, 32768, 0],+ [5119, 12554, 17805, 32768, 0],+ [1810, 5441, 8261, 32768, 0],+ [31212, 32724, 32748, 32768, 0],+ [23352, 31766, 32545, 32768, 0],+ [14669, 27570, 31059, 32768, 0],+ [8492, 20894, 27272, 32768, 0],+ [3644, 10194, 15204, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [2461, 7013, 9371, 32768, 0],+ [24749, 29600, 30986, 32768, 0],+ [9466, 19037, 22417, 32768, 0],+ [3584, 9280, 14400, 32768, 0],+ [1505, 3929, 5433, 32768, 0],+ [677, 1500, 2736, 32768, 0],+ [23987, 30702, 32117, 32768, 0],+ [13554, 24571, 29263, 32768, 0],+ [6211, 14556, 21155, 32768, 0],+ [3135, 10972, 15625, 32768, 0],+ [2435, 7127, 11427, 32768, 0],+ [31300, 32532, 32550, 32768, 0],+ [14757, 30365, 31954, 32768, 0],+ [4405, 11612, 18553, 32768, 0],+ [580, 4132, 7322, 32768, 0],+ [1695, 10169, 14124, 32768, 0],+ [30008, 32282, 32591, 32768, 0],+ [19244, 30108, 31748, 32768, 0],+ [11180, 24158, 29555, 32768, 0],+ [5650, 14972, 19209, 32768, 0],+ [2114, 5109, 8456, 32768, 0],+ [31856, 32716, 32748, 32768, 0],+ [23012, 31664, 32572, 32768, 0],+ [13694, 26656, 30636, 32768, 0],+ [8142, 19508, 26093, 32768, 0],+ [4253, 10955, 16724, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [601, 983, 1311, 32768, 0],+ [18725, 23406, 28087, 32768, 0],+ [5461, 8192, 10923, 32768, 0],+ [3781, 15124, 21425, 32768, 0],+ [2587, 7761, 12072, 32768, 0],+ [106, 458, 810, 32768, 0],+ [22282, 29710, 31894, 32768, 0],+ [8508, 20926, 25984, 32768, 0],+ [3726, 12713, 18083, 32768, 0],+ [1620, 7112, 10893, 32768, 0],+ [729, 2236, 3495, 32768, 0],+ [30163, 32474, 32684, 32768, 0],+ [18304, 30464, 32000, 32768, 0],+ [11443, 26526, 29647, 32768, 0],+ [6007, 15292, 21299, 32768, 0],+ [2234, 6703, 8937, 32768, 0],+ [30954, 32177, 32571, 32768, 0],+ [17363, 29562, 31076, 32768, 0],+ [9686, 22464, 27410, 32768, 0],+ [8192, 16384, 21390, 32768, 0],+ [1755, 8046, 11264, 32768, 0],+ [31168, 32734, 32748, 32768, 0],+ [22486, 31441, 32471, 32768, 0],+ [12833, 25627, 29738, 32768, 0],+ [6980, 17379, 23122, 32768, 0],+ [3111, 8887, 13479, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ ],+ [+ [+ [+ [6041, 11854, 15927, 32768, 0],+ [20326, 30905, 32251, 32768, 0],+ [14164, 26831, 30725, 32768, 0],+ [9760, 20647, 26585, 32768, 0],+ [6416, 14953, 21219, 32768, 0],+ [2966, 7151, 10891, 32768, 0],+ [23567, 31374, 32254, 32768, 0],+ [14978, 27416, 30946, 32768, 0],+ [9434, 20225, 26254, 32768, 0],+ [6658, 14558, 20535, 32768, 0],+ [3916, 8677, 12989, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [18088, 29545, 31587, 32768, 0],+ [13062, 25843, 30073, 32768, 0],+ [8940, 16827, 22251, 32768, 0],+ [7654, 13220, 17973, 32768, 0],+ [5733, 10316, 14456, 32768, 0],+ [22879, 31388, 32114, 32768, 0],+ [15215, 27993, 30955, 32768, 0],+ [9397, 19445, 24978, 32768, 0],+ [3442, 9813, 15344, 32768, 0],+ [1368, 3936, 6532, 32768, 0],+ [25494, 32033, 32406, 32768, 0],+ [16772, 27963, 30718, 32768, 0],+ [9419, 18165, 23260, 32768, 0],+ [2677, 7501, 11797, 32768, 0],+ [1516, 4344, 7170, 32768, 0],+ [26556, 31454, 32101, 32768, 0],+ [17128, 27035, 30108, 32768, 0],+ [8324, 15344, 20249, 32768, 0],+ [1903, 5696, 9469, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [8455, 19003, 24368, 32768, 0],+ [23563, 32021, 32604, 32768, 0],+ [16237, 29446, 31935, 32768, 0],+ [10724, 23999, 29358, 32768, 0],+ [6725, 17528, 24416, 32768, 0],+ [3927, 10927, 16825, 32768, 0],+ [26313, 32288, 32634, 32768, 0],+ [17430, 30095, 32095, 32768, 0],+ [11116, 24606, 29679, 32768, 0],+ [7195, 18384, 25269, 32768, 0],+ [4726, 12852, 19315, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [22822, 31648, 32483, 32768, 0],+ [16724, 29633, 31929, 32768, 0],+ [10261, 23033, 28725, 32768, 0],+ [7029, 17840, 24528, 32768, 0],+ [4867, 13886, 21502, 32768, 0],+ [25298, 31892, 32491, 32768, 0],+ [17809, 29330, 31512, 32768, 0],+ [9668, 21329, 26579, 32768, 0],+ [4774, 12956, 18976, 32768, 0],+ [2322, 7030, 11540, 32768, 0],+ [25472, 31920, 32543, 32768, 0],+ [17957, 29387, 31632, 32768, 0],+ [9196, 20593, 26400, 32768, 0],+ [4680, 12705, 19202, 32768, 0],+ [2917, 8456, 13436, 32768, 0],+ [26471, 32059, 32574, 32768, 0],+ [18458, 29783, 31909, 32768, 0],+ [8400, 19464, 25956, 32768, 0],+ [3812, 10973, 17206, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [6779, 13743, 17678, 32768, 0],+ [24806, 31797, 32457, 32768, 0],+ [17616, 29047, 31372, 32768, 0],+ [11063, 23175, 28003, 32768, 0],+ [6521, 16110, 22324, 32768, 0],+ [2764, 7504, 11654, 32768, 0],+ [25266, 32367, 32637, 32768, 0],+ [19054, 30553, 32175, 32768, 0],+ [12139, 25212, 29807, 32768, 0],+ [7311, 18162, 24704, 32768, 0],+ [3397, 9164, 14074, 32768, 0],+ [25988, 32208, 32522, 32768, 0],+ [16253, 28912, 31526, 32768, 0],+ [9151, 21387, 27372, 32768, 0],+ [5688, 14915, 21496, 32768, 0],+ [2717, 7627, 12004, 32768, 0],+ [23144, 31855, 32443, 32768, 0],+ [16070, 28491, 31325, 32768, 0],+ [8702, 20467, 26517, 32768, 0],+ [5243, 13956, 20367, 32768, 0],+ [2621, 7335, 11567, 32768, 0],+ [26636, 32340, 32630, 32768, 0],+ [19990, 31050, 32341, 32768, 0],+ [13243, 26105, 30315, 32768, 0],+ [8588, 19521, 25918, 32768, 0],+ [4717, 11585, 17304, 32768, 0],+ [25844, 32292, 32582, 32768, 0],+ [19090, 30635, 32097, 32768, 0],+ [11963, 24546, 28939, 32768, 0],+ [6218, 16087, 22354, 32768, 0],+ [2340, 6608, 10426, 32768, 0],+ [28046, 32576, 32694, 32768, 0],+ [21178, 31313, 32296, 32768, 0],+ [13486, 26184, 29870, 32768, 0],+ [7149, 17871, 23723, 32768, 0],+ [2833, 7958, 12259, 32768, 0],+ [27710, 32528, 32686, 32768, 0],+ [20674, 31076, 32268, 32768, 0],+ [12413, 24955, 29243, 32768, 0],+ [6676, 16927, 23097, 32768, 0],+ [2966, 8333, 12919, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [8639, 19339, 24429, 32768, 0],+ [24404, 31837, 32525, 32768, 0],+ [16997, 29425, 31784, 32768, 0],+ [11253, 24234, 29149, 32768, 0],+ [6751, 17394, 24028, 32768, 0],+ [3490, 9830, 15191, 32768, 0],+ [26283, 32471, 32714, 32768, 0],+ [19599, 31168, 32442, 32768, 0],+ [13146, 26954, 30893, 32768, 0],+ [8214, 20588, 26890, 32768, 0],+ [4699, 13081, 19300, 32768, 0],+ [28212, 32458, 32669, 32768, 0],+ [18594, 30316, 32100, 32768, 0],+ [11219, 24408, 29234, 32768, 0],+ [6865, 17656, 24149, 32768, 0],+ [3678, 10362, 16006, 32768, 0],+ [25825, 32136, 32616, 32768, 0],+ [17313, 29853, 32021, 32768, 0],+ [11197, 24471, 29472, 32768, 0],+ [6947, 17781, 24405, 32768, 0],+ [3768, 10660, 16261, 32768, 0],+ [27352, 32500, 32706, 32768, 0],+ [20850, 31468, 32469, 32768, 0],+ [14021, 27707, 31133, 32768, 0],+ [8964, 21748, 27838, 32768, 0],+ [5437, 14665, 21187, 32768, 0],+ [26304, 32492, 32698, 32768, 0],+ [20409, 31380, 32385, 32768, 0],+ [13682, 27222, 30632, 32768, 0],+ [8974, 21236, 26685, 32768, 0],+ [4234, 11665, 16934, 32768, 0],+ [26273, 32357, 32711, 32768, 0],+ [20672, 31242, 32441, 32768, 0],+ [14172, 27254, 30902, 32768, 0],+ [9870, 21898, 27275, 32768, 0],+ [5164, 13506, 19270, 32768, 0],+ [26725, 32459, 32728, 32768, 0],+ [20991, 31442, 32527, 32768, 0],+ [13071, 26434, 30811, 32768, 0],+ [8184, 20090, 26742, 32768, 0],+ [4803, 13255, 19895, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [7555, 14942, 18501, 32768, 0],+ [24410, 31178, 32287, 32768, 0],+ [14394, 26738, 30253, 32768, 0],+ [8413, 19554, 25195, 32768, 0],+ [4766, 12924, 18785, 32768, 0],+ [2029, 5806, 9207, 32768, 0],+ [26776, 32364, 32663, 32768, 0],+ [18732, 29967, 31931, 32768, 0],+ [11005, 23786, 28852, 32768, 0],+ [6466, 16909, 23510, 32768, 0],+ [3044, 8638, 13419, 32768, 0],+ [29208, 32582, 32704, 32768, 0],+ [20068, 30857, 32208, 32768, 0],+ [12003, 25085, 29595, 32768, 0],+ [6947, 17750, 24189, 32768, 0],+ [3245, 9103, 14007, 32768, 0],+ [27359, 32465, 32669, 32768, 0],+ [19421, 30614, 32174, 32768, 0],+ [11915, 25010, 29579, 32768, 0],+ [6950, 17676, 24074, 32768, 0],+ [3007, 8473, 13096, 32768, 0],+ [29002, 32676, 32735, 32768, 0],+ [22102, 31849, 32576, 32768, 0],+ [14408, 28009, 31405, 32768, 0],+ [9027, 21679, 27931, 32768, 0],+ [4694, 12678, 18748, 32768, 0],+ [28216, 32528, 32682, 32768, 0],+ [20849, 31264, 32318, 32768, 0],+ [12756, 25815, 29751, 32768, 0],+ [7565, 18801, 24923, 32768, 0],+ [3509, 9533, 14477, 32768, 0],+ [30133, 32687, 32739, 32768, 0],+ [23063, 31910, 32515, 32768, 0],+ [14588, 28051, 31132, 32768, 0],+ [9085, 21649, 27457, 32768, 0],+ [4261, 11654, 17264, 32768, 0],+ [29518, 32691, 32748, 32768, 0],+ [22451, 31959, 32613, 32768, 0],+ [14864, 28722, 31700, 32768, 0],+ [9695, 22964, 28716, 32768, 0],+ [4932, 13358, 19502, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [6465, 16958, 21688, 32768, 0],+ [25199, 31514, 32360, 32768, 0],+ [14774, 27149, 30607, 32768, 0],+ [9257, 21438, 26972, 32768, 0],+ [5723, 15183, 21882, 32768, 0],+ [3150, 8879, 13731, 32768, 0],+ [26989, 32262, 32682, 32768, 0],+ [17396, 29937, 32085, 32768, 0],+ [11387, 24901, 29784, 32768, 0],+ [7289, 18821, 25548, 32768, 0],+ [3734, 10577, 16086, 32768, 0],+ [29728, 32501, 32695, 32768, 0],+ [17431, 29701, 31903, 32768, 0],+ [9921, 22826, 28300, 32768, 0],+ [5896, 15434, 22068, 32768, 0],+ [3430, 9646, 14757, 32768, 0],+ [28614, 32511, 32705, 32768, 0],+ [19364, 30638, 32263, 32768, 0],+ [13129, 26254, 30402, 32768, 0],+ [8754, 20484, 26440, 32768, 0],+ [4378, 11607, 17110, 32768, 0],+ [30292, 32671, 32744, 32768, 0],+ [21780, 31603, 32501, 32768, 0],+ [14314, 27829, 31291, 32768, 0],+ [9611, 22327, 28263, 32768, 0],+ [4890, 13087, 19065, 32768, 0],+ [25862, 32567, 32733, 32768, 0],+ [20794, 32050, 32567, 32768, 0],+ [17243, 30625, 32254, 32768, 0],+ [13283, 27628, 31474, 32768, 0],+ [9669, 22532, 28918, 32768, 0],+ [27435, 32697, 32748, 32768, 0],+ [24922, 32390, 32714, 32768, 0],+ [21449, 31504, 32536, 32768, 0],+ [16392, 29729, 31832, 32768, 0],+ [11692, 24884, 29076, 32768, 0],+ [24193, 32290, 32735, 32768, 0],+ [18909, 31104, 32563, 32768, 0],+ [12236, 26841, 31403, 32768, 0],+ [8171, 21840, 29082, 32768, 0],+ [7224, 17280, 25275, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [3078, 6839, 9890, 32768, 0],+ [13837, 20450, 24479, 32768, 0],+ [5914, 14222, 19328, 32768, 0],+ [3866, 10267, 14762, 32768, 0],+ [2612, 7208, 11042, 32768, 0],+ [1067, 2991, 4776, 32768, 0],+ [25817, 31646, 32529, 32768, 0],+ [13708, 26338, 30385, 32768, 0],+ [7328, 18585, 24870, 32768, 0],+ [4691, 13080, 19276, 32768, 0],+ [1825, 5253, 8352, 32768, 0],+ [29386, 32315, 32624, 32768, 0],+ [17160, 29001, 31360, 32768, 0],+ [9602, 21862, 27396, 32768, 0],+ [5915, 15772, 22148, 32768, 0],+ [2786, 7779, 12047, 32768, 0],+ [29246, 32450, 32663, 32768, 0],+ [18696, 29929, 31818, 32768, 0],+ [10510, 23369, 28560, 32768, 0],+ [6229, 16499, 23125, 32768, 0],+ [2608, 7448, 11705, 32768, 0],+ [30753, 32710, 32748, 32768, 0],+ [21638, 31487, 32503, 32768, 0],+ [12937, 26854, 30870, 32768, 0],+ [8182, 20596, 26970, 32768, 0],+ [3637, 10269, 15497, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [5244, 12150, 16906, 32768, 0],+ [20486, 26858, 29701, 32768, 0],+ [7756, 18317, 23735, 32768, 0],+ [3452, 9256, 13146, 32768, 0],+ [2020, 5206, 8229, 32768, 0],+ [1801, 4993, 7903, 32768, 0],+ [27051, 31858, 32531, 32768, 0],+ [15988, 27531, 30619, 32768, 0],+ [9188, 21484, 26719, 32768, 0],+ [6273, 17186, 23800, 32768, 0],+ [3108, 9355, 14764, 32768, 0],+ [31076, 32520, 32680, 32768, 0],+ [18119, 30037, 31850, 32768, 0],+ [10244, 22969, 27472, 32768, 0],+ [4692, 14077, 19273, 32768, 0],+ [3694, 11677, 17556, 32768, 0],+ [30060, 32581, 32720, 32768, 0],+ [21011, 30775, 32120, 32768, 0],+ [11931, 24820, 29289, 32768, 0],+ [7119, 17662, 24356, 32768, 0],+ [3833, 10706, 16304, 32768, 0],+ [31954, 32731, 32748, 32768, 0],+ [23913, 31724, 32489, 32768, 0],+ [15520, 28060, 31286, 32768, 0],+ [11517, 23008, 28571, 32768, 0],+ [6193, 14508, 20629, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [1035, 2807, 4156, 32768, 0],+ [13162, 18138, 20939, 32768, 0],+ [2696, 6633, 8755, 32768, 0],+ [1373, 4161, 6853, 32768, 0],+ [1099, 2746, 4716, 32768, 0],+ [340, 1021, 1599, 32768, 0],+ [22826, 30419, 32135, 32768, 0],+ [10395, 21762, 26942, 32768, 0],+ [4726, 12407, 17361, 32768, 0],+ [2447, 7080, 10593, 32768, 0],+ [1227, 3717, 6011, 32768, 0],+ [28156, 31424, 31934, 32768, 0],+ [16915, 27754, 30373, 32768, 0],+ [9148, 20990, 26431, 32768, 0],+ [5950, 15515, 21148, 32768, 0],+ [2492, 7327, 11526, 32768, 0],+ [30602, 32477, 32670, 32768, 0],+ [20026, 29955, 31568, 32768, 0],+ [11220, 23628, 28105, 32768, 0],+ [6652, 17019, 22973, 32768, 0],+ [3064, 8536, 13043, 32768, 0],+ [31769, 32724, 32748, 32768, 0],+ [22230, 30887, 32373, 32768, 0],+ [12234, 25079, 29731, 32768, 0],+ [7326, 18816, 25353, 32768, 0],+ [3933, 10907, 16616, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ ],+ [+ [+ [+ [8896, 16227, 20630, 32768, 0],+ [23629, 31782, 32527, 32768, 0],+ [15173, 27755, 31321, 32768, 0],+ [10158, 21233, 27382, 32768, 0],+ [6420, 14857, 21558, 32768, 0],+ [3269, 8155, 12646, 32768, 0],+ [24835, 32009, 32496, 32768, 0],+ [16509, 28421, 31579, 32768, 0],+ [10957, 21514, 27418, 32768, 0],+ [7881, 15930, 22096, 32768, 0],+ [5388, 10960, 15918, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [20745, 30773, 32093, 32768, 0],+ [15200, 27221, 30861, 32768, 0],+ [13032, 20873, 25667, 32768, 0],+ [12285, 18663, 23494, 32768, 0],+ [11563, 17481, 21489, 32768, 0],+ [26260, 31982, 32320, 32768, 0],+ [15397, 28083, 31100, 32768, 0],+ [9742, 19217, 24824, 32768, 0],+ [3261, 9629, 15362, 32768, 0],+ [1480, 4322, 7499, 32768, 0],+ [27599, 32256, 32460, 32768, 0],+ [16857, 27659, 30774, 32768, 0],+ [9551, 18290, 23748, 32768, 0],+ [3052, 8933, 14103, 32768, 0],+ [2021, 5910, 9787, 32768, 0],+ [29005, 32015, 32392, 32768, 0],+ [17677, 27694, 30863, 32768, 0],+ [9204, 17356, 23219, 32768, 0],+ [2403, 7516, 12814, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [10808, 22056, 26896, 32768, 0],+ [25739, 32313, 32676, 32768, 0],+ [17288, 30203, 32221, 32768, 0],+ [11359, 24878, 29896, 32768, 0],+ [6949, 17767, 24893, 32768, 0],+ [4287, 11796, 18071, 32768, 0],+ [27880, 32521, 32705, 32768, 0],+ [19038, 31004, 32414, 32768, 0],+ [12564, 26345, 30768, 32768, 0],+ [8269, 19947, 26779, 32768, 0],+ [5674, 14657, 21674, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [25742, 32319, 32671, 32768, 0],+ [19557, 31164, 32454, 32768, 0],+ [13381, 26381, 30755, 32768, 0],+ [10101, 21466, 26722, 32768, 0],+ [9209, 19650, 26825, 32768, 0],+ [27107, 31917, 32432, 32768, 0],+ [18056, 28893, 31203, 32768, 0],+ [10200, 21434, 26764, 32768, 0],+ [4660, 12913, 19502, 32768, 0],+ [2368, 6930, 12504, 32768, 0],+ [26960, 32158, 32613, 32768, 0],+ [18628, 30005, 32031, 32768, 0],+ [10233, 22442, 28232, 32768, 0],+ [5471, 14630, 21516, 32768, 0],+ [3235, 10767, 17109, 32768, 0],+ [27696, 32440, 32692, 32768, 0],+ [20032, 31167, 32438, 32768, 0],+ [8700, 21341, 28442, 32768, 0],+ [5662, 14831, 21795, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [9704, 17294, 21132, 32768, 0],+ [26762, 32278, 32633, 32768, 0],+ [18382, 29620, 31819, 32768, 0],+ [10891, 23475, 28723, 32768, 0],+ [6358, 16583, 23309, 32768, 0],+ [3248, 9118, 14141, 32768, 0],+ [27204, 32573, 32699, 32768, 0],+ [19818, 30824, 32329, 32768, 0],+ [11772, 25120, 30041, 32768, 0],+ [6995, 18033, 25039, 32768, 0],+ [3752, 10442, 16098, 32768, 0],+ [27222, 32256, 32559, 32768, 0],+ [15356, 28399, 31475, 32768, 0],+ [8821, 20635, 27057, 32768, 0],+ [5511, 14404, 21239, 32768, 0],+ [2935, 8222, 13051, 32768, 0],+ [24875, 32120, 32529, 32768, 0],+ [15233, 28265, 31445, 32768, 0],+ [8605, 20570, 26932, 32768, 0],+ [5431, 14413, 21196, 32768, 0],+ [2994, 8341, 13223, 32768, 0],+ [28201, 32604, 32700, 32768, 0],+ [21041, 31446, 32456, 32768, 0],+ [13221, 26213, 30475, 32768, 0],+ [8255, 19385, 26037, 32768, 0],+ [4930, 12585, 18830, 32768, 0],+ [28768, 32448, 32627, 32768, 0],+ [19705, 30561, 32021, 32768, 0],+ [11572, 23589, 28220, 32768, 0],+ [5532, 15034, 21446, 32768, 0],+ [2460, 7150, 11456, 32768, 0],+ [29874, 32619, 32699, 32768, 0],+ [21621, 31071, 32201, 32768, 0],+ [12511, 24747, 28992, 32768, 0],+ [6281, 16395, 22748, 32768, 0],+ [3246, 9278, 14497, 32768, 0],+ [29715, 32625, 32712, 32768, 0],+ [20958, 31011, 32283, 32768, 0],+ [11233, 23671, 28806, 32768, 0],+ [6012, 16128, 22868, 32768, 0],+ [3427, 9851, 15414, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [11016, 22111, 26794, 32768, 0],+ [25946, 32357, 32677, 32768, 0],+ [17890, 30452, 32252, 32768, 0],+ [11678, 25142, 29816, 32768, 0],+ [6720, 17534, 24584, 32768, 0],+ [4230, 11665, 17820, 32768, 0],+ [28400, 32623, 32747, 32768, 0],+ [21164, 31668, 32575, 32768, 0],+ [13572, 27388, 31182, 32768, 0],+ [8234, 20750, 27358, 32768, 0],+ [5065, 14055, 20897, 32768, 0],+ [28981, 32547, 32705, 32768, 0],+ [18681, 30543, 32239, 32768, 0],+ [10919, 24075, 29286, 32768, 0],+ [6431, 17199, 24077, 32768, 0],+ [3819, 10464, 16618, 32768, 0],+ [26870, 32467, 32693, 32768, 0],+ [19041, 30831, 32347, 32768, 0],+ [11794, 25211, 30016, 32768, 0],+ [6888, 18019, 24970, 32768, 0],+ [4370, 12363, 18992, 32768, 0],+ [29578, 32670, 32744, 32768, 0],+ [23159, 32007, 32613, 32768, 0],+ [15315, 28669, 31676, 32768, 0],+ [9298, 22607, 28782, 32768, 0],+ [6144, 15913, 22968, 32768, 0],+ [28110, 32499, 32669, 32768, 0],+ [21574, 30937, 32015, 32768, 0],+ [12759, 24818, 28727, 32768, 0],+ [6545, 16761, 23042, 32768, 0],+ [3649, 10597, 16833, 32768, 0],+ [28163, 32552, 32728, 32768, 0],+ [22101, 31469, 32464, 32768, 0],+ [13160, 25472, 30143, 32768, 0],+ [7303, 18684, 25468, 32768, 0],+ [5241, 13975, 20955, 32768, 0],+ [28400, 32631, 32744, 32768, 0],+ [22104, 31793, 32603, 32768, 0],+ [13557, 26571, 30846, 32768, 0],+ [7749, 19861, 26675, 32768, 0],+ [4873, 14030, 21234, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [9800, 17635, 21073, 32768, 0],+ [26153, 31885, 32527, 32768, 0],+ [15038, 27852, 31006, 32768, 0],+ [8718, 20564, 26486, 32768, 0],+ [5128, 14076, 20514, 32768, 0],+ [2636, 7566, 11925, 32768, 0],+ [27551, 32504, 32701, 32768, 0],+ [18310, 30054, 32100, 32768, 0],+ [10211, 23420, 29082, 32768, 0],+ [6222, 16876, 23916, 32768, 0],+ [3462, 9954, 15498, 32768, 0],+ [29991, 32633, 32721, 32768, 0],+ [19883, 30751, 32201, 32768, 0],+ [11141, 24184, 29285, 32768, 0],+ [6420, 16940, 23774, 32768, 0],+ [3392, 9753, 15118, 32768, 0],+ [28465, 32616, 32712, 32768, 0],+ [19850, 30702, 32244, 32768, 0],+ [10983, 24024, 29223, 32768, 0],+ [6294, 16770, 23582, 32768, 0],+ [3244, 9283, 14509, 32768, 0],+ [30023, 32717, 32748, 32768, 0],+ [22940, 32032, 32626, 32768, 0],+ [14282, 27928, 31473, 32768, 0],+ [8562, 21327, 27914, 32768, 0],+ [4846, 13393, 19919, 32768, 0],+ [29981, 32590, 32695, 32768, 0],+ [20465, 30963, 32166, 32768, 0],+ [11479, 23579, 28195, 32768, 0],+ [5916, 15648, 22073, 32768, 0],+ [3031, 8605, 13398, 32768, 0],+ [31146, 32691, 32739, 32768, 0],+ [23106, 31724, 32444, 32768, 0],+ [13783, 26738, 30439, 32768, 0],+ [7852, 19468, 25807, 32768, 0],+ [3860, 11124, 16853, 32768, 0],+ [31014, 32724, 32748, 32768, 0],+ [23629, 32109, 32628, 32768, 0],+ [14747, 28115, 31403, 32768, 0],+ [8545, 21242, 27478, 32768, 0],+ [4574, 12781, 19067, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [9185, 19694, 24688, 32768, 0],+ [26081, 31985, 32621, 32768, 0],+ [16015, 29000, 31787, 32768, 0],+ [10542, 23690, 29206, 32768, 0],+ [6732, 17945, 24677, 32768, 0],+ [3916, 11039, 16722, 32768, 0],+ [28224, 32566, 32744, 32768, 0],+ [19100, 31138, 32485, 32768, 0],+ [12528, 26620, 30879, 32768, 0],+ [7741, 20277, 26885, 32768, 0],+ [4566, 12845, 18990, 32768, 0],+ [29933, 32593, 32718, 32768, 0],+ [17670, 30333, 32155, 32768, 0],+ [10385, 23600, 28909, 32768, 0],+ [6243, 16236, 22407, 32768, 0],+ [3976, 10389, 16017, 32768, 0],+ [28377, 32561, 32738, 32768, 0],+ [19366, 31175, 32482, 32768, 0],+ [13327, 27175, 31094, 32768, 0],+ [8258, 20769, 27143, 32768, 0],+ [4703, 13198, 19527, 32768, 0],+ [31086, 32706, 32748, 32768, 0],+ [22853, 31902, 32583, 32768, 0],+ [14759, 28186, 31419, 32768, 0],+ [9284, 22382, 28348, 32768, 0],+ [5585, 15192, 21868, 32768, 0],+ [28291, 32652, 32746, 32768, 0],+ [19849, 32107, 32571, 32768, 0],+ [14834, 26818, 29214, 32768, 0],+ [10306, 22594, 28672, 32768, 0],+ [6615, 17384, 23384, 32768, 0],+ [28947, 32604, 32745, 32768, 0],+ [25625, 32289, 32646, 32768, 0],+ [18758, 28672, 31403, 32768, 0],+ [10017, 23430, 28523, 32768, 0],+ [6862, 15269, 22131, 32768, 0],+ [23933, 32509, 32739, 32768, 0],+ [19927, 31495, 32631, 32768, 0],+ [11903, 26023, 30621, 32768, 0],+ [7026, 20094, 27252, 32768, 0],+ [5998, 18106, 24437, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [4456, 11274, 15533, 32768, 0],+ [21219, 29079, 31616, 32768, 0],+ [11173, 23774, 28567, 32768, 0],+ [7282, 18293, 24263, 32768, 0],+ [4890, 13286, 19115, 32768, 0],+ [1890, 5508, 8659, 32768, 0],+ [26651, 32136, 32647, 32768, 0],+ [14630, 28254, 31455, 32768, 0],+ [8716, 21287, 27395, 32768, 0],+ [5615, 15331, 22008, 32768, 0],+ [2675, 7700, 12150, 32768, 0],+ [29954, 32526, 32690, 32768, 0],+ [16126, 28982, 31633, 32768, 0],+ [9030, 21361, 27352, 32768, 0],+ [5411, 14793, 21271, 32768, 0],+ [2943, 8422, 13163, 32768, 0],+ [29539, 32601, 32730, 32768, 0],+ [18125, 30385, 32201, 32768, 0],+ [10422, 24090, 29468, 32768, 0],+ [6468, 17487, 24438, 32768, 0],+ [2970, 8653, 13531, 32768, 0],+ [30912, 32715, 32748, 32768, 0],+ [20666, 31373, 32497, 32768, 0],+ [12509, 26640, 30917, 32768, 0],+ [8058, 20629, 27290, 32768, 0],+ [4231, 12006, 18052, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [10202, 20633, 25484, 32768, 0],+ [27336, 31445, 32352, 32768, 0],+ [12420, 24384, 28552, 32768, 0],+ [7648, 18115, 23856, 32768, 0],+ [5662, 14341, 19902, 32768, 0],+ [3611, 10328, 15390, 32768, 0],+ [30945, 32616, 32736, 32768, 0],+ [18682, 30505, 32253, 32768, 0],+ [11513, 25336, 30203, 32768, 0],+ [7449, 19452, 26148, 32768, 0],+ [4482, 13051, 18886, 32768, 0],+ [32022, 32690, 32747, 32768, 0],+ [18578, 30501, 32146, 32768, 0],+ [11249, 23368, 28631, 32768, 0],+ [5645, 16958, 22158, 32768, 0],+ [5009, 11444, 16637, 32768, 0],+ [31357, 32710, 32748, 32768, 0],+ [21552, 31494, 32504, 32768, 0],+ [13891, 27677, 31340, 32768, 0],+ [9051, 22098, 28172, 32768, 0],+ [5190, 13377, 19486, 32768, 0],+ [32364, 32740, 32748, 32768, 0],+ [24839, 31907, 32551, 32768, 0],+ [17160, 28779, 31696, 32768, 0],+ [12452, 24137, 29602, 32768, 0],+ [6165, 15389, 22477, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [2575, 7281, 11077, 32768, 0],+ [14002, 20866, 25402, 32768, 0],+ [6343, 15056, 19658, 32768, 0],+ [4474, 11858, 17041, 32768, 0],+ [2865, 8299, 12534, 32768, 0],+ [1344, 3949, 6391, 32768, 0],+ [24720, 31239, 32459, 32768, 0],+ [12585, 25356, 29968, 32768, 0],+ [7181, 18246, 24444, 32768, 0],+ [5025, 13667, 19885, 32768, 0],+ [2521, 7304, 11605, 32768, 0],+ [29908, 32252, 32584, 32768, 0],+ [17421, 29156, 31575, 32768, 0],+ [9889, 22188, 27782, 32768, 0],+ [5878, 15647, 22123, 32768, 0],+ [2814, 8665, 13323, 32768, 0],+ [30183, 32568, 32713, 32768, 0],+ [18528, 30195, 32049, 32768, 0],+ [10982, 24606, 29657, 32768, 0],+ [6957, 18165, 25231, 32768, 0],+ [3508, 10118, 15468, 32768, 0],+ [31761, 32736, 32748, 32768, 0],+ [21041, 31328, 32546, 32768, 0],+ [12568, 26732, 31166, 32768, 0],+ [8052, 20720, 27733, 32768, 0],+ [4336, 12192, 18396, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ ],+ [+ [+ [+ [7062, 16472, 22319, 32768, 0],+ [24538, 32261, 32674, 32768, 0],+ [13675, 28041, 31779, 32768, 0],+ [8590, 20674, 27631, 32768, 0],+ [5685, 14675, 22013, 32768, 0],+ [3655, 9898, 15731, 32768, 0],+ [26493, 32418, 32658, 32768, 0],+ [16376, 29342, 32090, 32768, 0],+ [10594, 22649, 28970, 32768, 0],+ [8176, 17170, 24303, 32768, 0],+ [5605, 12694, 19139, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [23888, 31902, 32542, 32768, 0],+ [18612, 29687, 31987, 32768, 0],+ [16245, 24852, 29249, 32768, 0],+ [15765, 22608, 27559, 32768, 0],+ [19895, 24699, 27510, 32768, 0],+ [28401, 32212, 32457, 32768, 0],+ [15274, 27825, 30980, 32768, 0],+ [9364, 18128, 24332, 32768, 0],+ [2283, 8193, 15082, 32768, 0],+ [1228, 3972, 7881, 32768, 0],+ [29455, 32469, 32620, 32768, 0],+ [17981, 28245, 31388, 32768, 0],+ [10921, 20098, 26240, 32768, 0],+ [3743, 11829, 18657, 32768, 0],+ [2374, 9593, 15715, 32768, 0],+ [31068, 32466, 32635, 32768, 0],+ [20321, 29572, 31971, 32768, 0],+ [10771, 20255, 27119, 32768, 0],+ [2795, 10410, 17361, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [9320, 22102, 27840, 32768, 0],+ [27057, 32464, 32724, 32768, 0],+ [16331, 30268, 32309, 32768, 0],+ [10319, 23935, 29720, 32768, 0],+ [6189, 16448, 24106, 32768, 0],+ [3589, 10884, 18808, 32768, 0],+ [29026, 32624, 32748, 32768, 0],+ [19226, 31507, 32587, 32768, 0],+ [12692, 26921, 31203, 32768, 0],+ [7049, 19532, 27635, 32768, 0],+ [7727, 15669, 23252, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [28056, 32625, 32748, 32768, 0],+ [22383, 32075, 32669, 32768, 0],+ [15417, 27098, 31749, 32768, 0],+ [18127, 26493, 27190, 32768, 0],+ [5461, 16384, 21845, 32768, 0],+ [27982, 32091, 32584, 32768, 0],+ [19045, 29868, 31972, 32768, 0],+ [10397, 22266, 27932, 32768, 0],+ [5990, 13697, 21500, 32768, 0],+ [1792, 6912, 15104, 32768, 0],+ [28198, 32501, 32718, 32768, 0],+ [21534, 31521, 32569, 32768, 0],+ [11109, 25217, 30017, 32768, 0],+ [5671, 15124, 26151, 32768, 0],+ [4681, 14043, 18725, 32768, 0],+ [28688, 32580, 32741, 32768, 0],+ [22576, 32079, 32661, 32768, 0],+ [10627, 22141, 28340, 32768, 0],+ [9362, 14043, 28087, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [7754, 16948, 22142, 32768, 0],+ [25670, 32330, 32691, 32768, 0],+ [15663, 29225, 31994, 32768, 0],+ [9878, 23288, 29158, 32768, 0],+ [6419, 17088, 24336, 32768, 0],+ [3859, 11003, 17039, 32768, 0],+ [27562, 32595, 32725, 32768, 0],+ [17575, 30588, 32399, 32768, 0],+ [10819, 24838, 30309, 32768, 0],+ [7124, 18686, 25916, 32768, 0],+ [4479, 12688, 19340, 32768, 0],+ [28385, 32476, 32673, 32768, 0],+ [15306, 29005, 31938, 32768, 0],+ [8937, 21615, 28322, 32768, 0],+ [5982, 15603, 22786, 32768, 0],+ [3620, 10267, 16136, 32768, 0],+ [27280, 32464, 32667, 32768, 0],+ [15607, 29160, 32004, 32768, 0],+ [9091, 22135, 28740, 32768, 0],+ [6232, 16632, 24020, 32768, 0],+ [4047, 11377, 17672, 32768, 0],+ [29220, 32630, 32718, 32768, 0],+ [19650, 31220, 32462, 32768, 0],+ [13050, 26312, 30827, 32768, 0],+ [9228, 20870, 27468, 32768, 0],+ [6146, 15149, 21971, 32768, 0],+ [30169, 32481, 32623, 32768, 0],+ [17212, 29311, 31554, 32768, 0],+ [9911, 21311, 26882, 32768, 0],+ [4487, 13314, 20372, 32768, 0],+ [2570, 7772, 12889, 32768, 0],+ [30924, 32613, 32708, 32768, 0],+ [19490, 30206, 32107, 32768, 0],+ [11232, 23998, 29276, 32768, 0],+ [6769, 17955, 25035, 32768, 0],+ [4398, 12623, 19214, 32768, 0],+ [30609, 32627, 32722, 32768, 0],+ [19370, 30582, 32287, 32768, 0],+ [10457, 23619, 29409, 32768, 0],+ [6443, 17637, 24834, 32768, 0],+ [4645, 13236, 20106, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [8626, 20271, 26216, 32768, 0],+ [26707, 32406, 32711, 32768, 0],+ [16999, 30329, 32286, 32768, 0],+ [11445, 25123, 30286, 32768, 0],+ [6411, 18828, 25601, 32768, 0],+ [6801, 12458, 20248, 32768, 0],+ [29918, 32682, 32748, 32768, 0],+ [20649, 31739, 32618, 32768, 0],+ [12879, 27773, 31581, 32768, 0],+ [7896, 21751, 28244, 32768, 0],+ [5260, 14870, 23698, 32768, 0],+ [29252, 32593, 32731, 32768, 0],+ [17072, 30460, 32294, 32768, 0],+ [10653, 24143, 29365, 32768, 0],+ [6536, 17490, 23983, 32768, 0],+ [4929, 13170, 20085, 32768, 0],+ [28137, 32518, 32715, 32768, 0],+ [18171, 30784, 32407, 32768, 0],+ [11437, 25436, 30459, 32768, 0],+ [7252, 18534, 26176, 32768, 0],+ [4126, 13353, 20978, 32768, 0],+ [31162, 32726, 32748, 32768, 0],+ [23017, 32222, 32701, 32768, 0],+ [15629, 29233, 32046, 32768, 0],+ [9387, 22621, 29480, 32768, 0],+ [6922, 17616, 25010, 32768, 0],+ [28838, 32265, 32614, 32768, 0],+ [19701, 30206, 31920, 32768, 0],+ [11214, 22410, 27933, 32768, 0],+ [5320, 14177, 23034, 32768, 0],+ [5049, 12881, 17827, 32768, 0],+ [27484, 32471, 32734, 32768, 0],+ [21076, 31526, 32561, 32768, 0],+ [12707, 26303, 31211, 32768, 0],+ [8169, 21722, 28219, 32768, 0],+ [6045, 19406, 27042, 32768, 0],+ [27753, 32572, 32745, 32768, 0],+ [20832, 31878, 32653, 32768, 0],+ [13250, 27356, 31674, 32768, 0],+ [7718, 21508, 29858, 32768, 0],+ [7209, 18350, 25559, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [7876, 16901, 21741, 32768, 0],+ [24001, 31898, 32625, 32768, 0],+ [14529, 27959, 31451, 32768, 0],+ [8273, 20818, 27258, 32768, 0],+ [5278, 14673, 21510, 32768, 0],+ [2983, 8843, 14039, 32768, 0],+ [28016, 32574, 32732, 32768, 0],+ [17471, 30306, 32301, 32768, 0],+ [10224, 24063, 29728, 32768, 0],+ [6602, 17954, 25052, 32768, 0],+ [4002, 11585, 17759, 32768, 0],+ [30190, 32634, 32739, 32768, 0],+ [17497, 30282, 32270, 32768, 0],+ [10229, 23729, 29538, 32768, 0],+ [6344, 17211, 24440, 32768, 0],+ [3849, 11189, 17108, 32768, 0],+ [28570, 32583, 32726, 32768, 0],+ [17521, 30161, 32238, 32768, 0],+ [10153, 23565, 29378, 32768, 0],+ [6455, 17341, 24443, 32768, 0],+ [3907, 11042, 17024, 32768, 0],+ [30689, 32715, 32748, 32768, 0],+ [21546, 31840, 32610, 32768, 0],+ [13547, 27581, 31459, 32768, 0],+ [8912, 21757, 28309, 32768, 0],+ [5548, 15080, 22046, 32768, 0],+ [30783, 32540, 32685, 32768, 0],+ [17540, 29528, 31668, 32768, 0],+ [10160, 21468, 26783, 32768, 0],+ [4724, 13393, 20054, 32768, 0],+ [2702, 8174, 13102, 32768, 0],+ [31648, 32686, 32742, 32768, 0],+ [20954, 31094, 32337, 32768, 0],+ [12420, 25698, 30179, 32768, 0],+ [7304, 19320, 26248, 32768, 0],+ [4366, 12261, 18864, 32768, 0],+ [31581, 32723, 32748, 32768, 0],+ [21373, 31586, 32525, 32768, 0],+ [12744, 26625, 30885, 32768, 0],+ [7431, 20322, 26950, 32768, 0],+ [4692, 13323, 20111, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [7833, 18369, 24095, 32768, 0],+ [26650, 32273, 32702, 32768, 0],+ [16371, 29961, 32191, 32768, 0],+ [11055, 24082, 29629, 32768, 0],+ [6892, 18644, 25400, 32768, 0],+ [5006, 13057, 19240, 32768, 0],+ [29834, 32666, 32748, 32768, 0],+ [19577, 31335, 32570, 32768, 0],+ [12253, 26509, 31122, 32768, 0],+ [7991, 20772, 27711, 32768, 0],+ [5677, 15910, 23059, 32768, 0],+ [30109, 32532, 32720, 32768, 0],+ [16747, 30166, 32252, 32768, 0],+ [10134, 23542, 29184, 32768, 0],+ [5791, 16176, 23556, 32768, 0],+ [4362, 10414, 17284, 32768, 0],+ [29492, 32626, 32748, 32768, 0],+ [19894, 31402, 32525, 32768, 0],+ [12942, 27071, 30869, 32768, 0],+ [8346, 21216, 27405, 32768, 0],+ [6572, 17087, 23859, 32768, 0],+ [32035, 32735, 32748, 32768, 0],+ [22957, 31838, 32618, 32768, 0],+ [14724, 28572, 31772, 32768, 0],+ [10364, 23999, 29553, 32768, 0],+ [7004, 18433, 25655, 32768, 0],+ [27528, 32277, 32681, 32768, 0],+ [16959, 31171, 32096, 32768, 0],+ [10486, 23593, 27962, 32768, 0],+ [8192, 16384, 23211, 32768, 0],+ [8937, 17873, 20852, 32768, 0],+ [27715, 32002, 32615, 32768, 0],+ [15073, 29491, 31676, 32768, 0],+ [11264, 24576, 28672, 32768, 0],+ [2341, 18725, 23406, 32768, 0],+ [7282, 18204, 25486, 32768, 0],+ [28547, 32213, 32657, 32768, 0],+ [20788, 29773, 32239, 32768, 0],+ [6780, 21469, 30508, 32768, 0],+ [5958, 14895, 23831, 32768, 0],+ [16384, 21845, 27307, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [5992, 14304, 19765, 32768, 0],+ [22612, 31238, 32456, 32768, 0],+ [13456, 27162, 31087, 32768, 0],+ [8001, 20062, 26504, 32768, 0],+ [5168, 14105, 20764, 32768, 0],+ [2632, 7771, 12385, 32768, 0],+ [27034, 32344, 32709, 32768, 0],+ [15850, 29415, 31997, 32768, 0],+ [9494, 22776, 28841, 32768, 0],+ [6151, 16830, 23969, 32768, 0],+ [3461, 10039, 15722, 32768, 0],+ [30134, 32569, 32731, 32768, 0],+ [15638, 29422, 31945, 32768, 0],+ [9150, 21865, 28218, 32768, 0],+ [5647, 15719, 22676, 32768, 0],+ [3402, 9772, 15477, 32768, 0],+ [28530, 32586, 32735, 32768, 0],+ [17139, 30298, 32292, 32768, 0],+ [10200, 24039, 29685, 32768, 0],+ [6419, 17674, 24786, 32768, 0],+ [3544, 10225, 15824, 32768, 0],+ [31333, 32726, 32748, 32768, 0],+ [20618, 31487, 32544, 32768, 0],+ [12901, 27217, 31232, 32768, 0],+ [8624, 21734, 28171, 32768, 0],+ [5104, 14191, 20748, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [11206, 21090, 26561, 32768, 0],+ [28759, 32279, 32671, 32768, 0],+ [14171, 27952, 31569, 32768, 0],+ [9743, 22907, 29141, 32768, 0],+ [6871, 17886, 24868, 32768, 0],+ [4960, 13152, 19315, 32768, 0],+ [31077, 32661, 32748, 32768, 0],+ [19400, 31195, 32515, 32768, 0],+ [12752, 26858, 31040, 32768, 0],+ [8370, 22098, 28591, 32768, 0],+ [5457, 15373, 22298, 32768, 0],+ [31697, 32706, 32748, 32768, 0],+ [17860, 30657, 32333, 32768, 0],+ [12510, 24812, 29261, 32768, 0],+ [6180, 19124, 24722, 32768, 0],+ [5041, 13548, 17959, 32768, 0],+ [31552, 32716, 32748, 32768, 0],+ [21908, 31769, 32623, 32768, 0],+ [14470, 28201, 31565, 32768, 0],+ [9493, 22982, 28608, 32768, 0],+ [6858, 17240, 24137, 32768, 0],+ [32543, 32752, 32756, 32768, 0],+ [24286, 32097, 32666, 32768, 0],+ [15958, 29217, 32024, 32768, 0],+ [10207, 24234, 29958, 32768, 0],+ [6929, 18305, 25652, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ [+ [+ [4137, 10847, 15682, 32768, 0],+ [17824, 27001, 30058, 32768, 0],+ [10204, 22796, 28291, 32768, 0],+ [6076, 15935, 22125, 32768, 0],+ [3852, 10937, 16816, 32768, 0],+ [2252, 6324, 10131, 32768, 0],+ [25840, 32016, 32662, 32768, 0],+ [15109, 28268, 31531, 32768, 0],+ [9385, 22231, 28340, 32768, 0],+ [6082, 16672, 23479, 32768, 0],+ [3318, 9427, 14681, 32768, 0],+ [30594, 32574, 32718, 32768, 0],+ [16836, 29552, 31859, 32768, 0],+ [9556, 22542, 28356, 32768, 0],+ [6305, 16725, 23540, 32768, 0],+ [3376, 9895, 15184, 32768, 0],+ [29383, 32617, 32745, 32768, 0],+ [18891, 30809, 32401, 32768, 0],+ [11688, 25942, 30687, 32768, 0],+ [7468, 19469, 26651, 32768, 0],+ [3909, 11358, 17012, 32768, 0],+ [31564, 32736, 32748, 32768, 0],+ [20906, 31611, 32600, 32768, 0],+ [13191, 27621, 31537, 32768, 0],+ [8768, 22029, 28676, 32768, 0],+ [5079, 14109, 20906, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ ],+];++pub static DEFAULT_COEFF_BASE_EOB_CDF: [[[[[u16; 4]; 4]; 2]; 5]; 4] = [+ [+ [+ [[17837, 29055, 32768, 0], [29600, 31446, 32768, 0], [30844, 31878, 32768, 0], [24926, 28948, 32768, 0]],+ [[21365, 30026, 32768, 0], [30512, 32423, 32768, 0], [31658, 32621, 32768, 0], [29630, 31881, 32768, 0]],+ ],+ [+ [[5717, 26477, 32768, 0], [30491, 31703, 32768, 0], [31550, 32158, 32768, 0], [29648, 31491, 32768, 0]],+ [[12608, 27820, 32768, 0], [30680, 32225, 32768, 0], [30809, 32335, 32768, 0], [31299, 32423, 32768, 0]],+ ],+ [+ [[1786, 12612, 32768, 0], [30663, 31625, 32768, 0], [32339, 32468, 32768, 0], [31148, 31833, 32768, 0]],+ [[18857, 23865, 32768, 0], [31428, 32428, 32768, 0], [31744, 32373, 32768, 0], [31775, 32526, 32768, 0]],+ ],+ [+ [[1787, 2532, 32768, 0], [30832, 31662, 32768, 0], [31824, 32682, 32768, 0], [32133, 32569, 32768, 0]],+ [[13751, 22235, 32768, 0], [32089, 32409, 32768, 0], [27084, 27920, 32768, 0], [29291, 32594, 32768, 0]],+ ],+ [+ [[1725, 3449, 32768, 0], [31102, 31935, 32768, 0], [32457, 32613, 32768, 0], [32412, 32649, 32768, 0]],+ [[10923, 21845, 32768, 0], [10923, 21845, 32768, 0], [10923, 21845, 32768, 0], [10923, 21845, 32768, 0]],+ ],+ ],+ [+ [+ [[17560, 29888, 32768, 0], [29671, 31549, 32768, 0], [31007, 32056, 32768, 0], [27286, 30006, 32768, 0]],+ [[26594, 31212, 32768, 0], [31208, 32582, 32768, 0], [31835, 32637, 32768, 0], [30595, 32206, 32768, 0]],+ ],+ [+ [[15239, 29932, 32768, 0], [31315, 32095, 32768, 0], [32130, 32434, 32768, 0], [30864, 31996, 32768, 0]],+ [[26279, 30968, 32768, 0], [31142, 32495, 32768, 0], [31713, 32540, 32768, 0], [31929, 32594, 32768, 0]],+ ],+ [+ [[2644, 25198, 32768, 0], [32038, 32451, 32768, 0], [32639, 32695, 32768, 0], [32166, 32518, 32768, 0]],+ [[17187, 27668, 32768, 0], [31714, 32550, 32768, 0], [32283, 32678, 32768, 0], [31930, 32563, 32768, 0]],+ ],+ [+ [[1044, 2257, 32768, 0], [30755, 31923, 32768, 0], [32208, 32693, 32768, 0], [32244, 32615, 32768, 0]],+ [[21317, 26207, 32768, 0], [29133, 30868, 32768, 0], [29311, 31231, 32768, 0], [29657, 31087, 32768, 0]],+ ],+ [+ [[478, 1834, 32768, 0], [31005, 31987, 32768, 0], [32317, 32724, 32768, 0], [30865, 32648, 32768, 0]],+ [[10923, 21845, 32768, 0], [10923, 21845, 32768, 0], [10923, 21845, 32768, 0], [10923, 21845, 32768, 0]],+ ],+ ],+ [+ [+ [[20092, 30774, 32768, 0], [30695, 32020, 32768, 0], [31131, 32103, 32768, 0], [28666, 30870, 32768, 0]],+ [[27258, 31095, 32768, 0], [31804, 32623, 32768, 0], [31763, 32528, 32768, 0], [31438, 32506, 32768, 0]],+ ],+ [+ [[18049, 30489, 32768, 0], [31706, 32286, 32768, 0], [32163, 32473, 32768, 0], [31550, 32184, 32768, 0]],+ [[27116, 30842, 32768, 0], [31971, 32598, 32768, 0], [32088, 32576, 32768, 0], [32067, 32664, 32768, 0]],+ ],+ [+ [[12854, 29093, 32768, 0], [32272, 32558, 32768, 0], [32667, 32729, 32768, 0], [32306, 32585, 32768, 0]],+ [[25476, 30366, 32768, 0], [32169, 32687, 32768, 0], [32479, 32689, 32768, 0], [31673, 32634, 32768, 0]],+ ],+ [+ [[2809, 19301, 32768, 0], [32205, 32622, 32768, 0], [32338, 32730, 32768, 0], [31786, 32616, 32768, 0]],+ [[22737, 29105, 32768, 0], [30810, 32362, 32768, 0], [30014, 32627, 32768, 0], [30528, 32574, 32768, 0]],+ ],+ [+ [[935, 3382, 32768, 0], [30789, 31909, 32768, 0], [32466, 32756, 32768, 0], [30860, 32513, 32768, 0]],+ [[10923, 21845, 32768, 0], [10923, 21845, 32768, 0], [10923, 21845, 32768, 0], [10923, 21845, 32768, 0]],+ ],+ ],+ [+ [+ [[22497, 31198, 32768, 0], [31715, 32495, 32768, 0], [31606, 32337, 32768, 0], [30388, 31990, 32768, 0]],+ [[27877, 31584, 32768, 0], [32170, 32728, 32768, 0], [32155, 32688, 32768, 0], [32219, 32702, 32768, 0]],+ ],+ [+ [[21457, 31043, 32768, 0], [31951, 32483, 32768, 0], [32153, 32562, 32768, 0], [31473, 32215, 32768, 0]],+ [[27558, 31151, 32768, 0], [32020, 32640, 32768, 0], [32097, 32575, 32768, 0], [32242, 32719, 32768, 0]],+ ],+ [+ [[19980, 30591, 32768, 0], [32219, 32597, 32768, 0], [32581, 32706, 32768, 0], [31803, 32287, 32768, 0]],+ [[26473, 30507, 32768, 0], [32431, 32723, 32768, 0], [32196, 32611, 32768, 0], [31588, 32528, 32768, 0]],+ ],+ [+ [[24647, 30463, 32768, 0], [32412, 32695, 32768, 0], [32468, 32720, 32768, 0], [31269, 32523, 32768, 0]],+ [[28482, 31505, 32768, 0], [32152, 32701, 32768, 0], [31732, 32598, 32768, 0], [31767, 32712, 32768, 0]],+ ],+ [+ [[12358, 24977, 32768, 0], [31331, 32385, 32768, 0], [32634, 32756, 32768, 0], [30411, 32548, 32768, 0]],+ [[10923, 21845, 32768, 0], [10923, 21845, 32768, 0], [10923, 21845, 32768, 0], [10923, 21845, 32768, 0]],+ ],+ ],+];++pub static DEFAULT_COEFF_BR_CDF: [[[[[u16; 5]; 21]; 2]; 5]; 4] = [+ [+ [+ [+ [14298, 20718, 24174, 32768, 0],+ [12536, 19601, 23789, 32768, 0],+ [8712, 15051, 19503, 32768, 0],+ [6170, 11327, 15434, 32768, 0],+ [4742, 8926, 12538, 32768, 0],+ [3803, 7317, 10546, 32768, 0],+ [1696, 3317, 4871, 32768, 0],+ [14392, 19951, 22756, 32768, 0],+ [15978, 23218, 26818, 32768, 0],+ [12187, 19474, 23889, 32768, 0],+ [9176, 15640, 20259, 32768, 0],+ [7068, 12655, 17028, 32768, 0],+ [5656, 10442, 14472, 32768, 0],+ [2580, 4992, 7244, 32768, 0],+ [12136, 18049, 21426, 32768, 0],+ [13784, 20721, 24481, 32768, 0],+ [10836, 17621, 21900, 32768, 0],+ [8372, 14444, 18847, 32768, 0],+ [6523, 11779, 16000, 32768, 0],+ [5337, 9898, 13760, 32768, 0],+ [3034, 5860, 8462, 32768, 0],+ ],+ [+ [15967, 22905, 26286, 32768, 0],+ [13534, 20654, 24579, 32768, 0],+ [9504, 16092, 20535, 32768, 0],+ [6975, 12568, 16903, 32768, 0],+ [5364, 10091, 14020, 32768, 0],+ [4357, 8370, 11857, 32768, 0],+ [2506, 4934, 7218, 32768, 0],+ [23032, 28815, 30936, 32768, 0],+ [19540, 26704, 29719, 32768, 0],+ [15158, 22969, 27097, 32768, 0],+ [11408, 18865, 23650, 32768, 0],+ [8885, 15448, 20250, 32768, 0],+ [7108, 12853, 17416, 32768, 0],+ [4231, 8041, 11480, 32768, 0],+ [19823, 26490, 29156, 32768, 0],+ [18890, 25929, 28932, 32768, 0],+ [15660, 23491, 27433, 32768, 0],+ [12147, 19776, 24488, 32768, 0],+ [9728, 16774, 21649, 32768, 0],+ [7919, 14277, 19066, 32768, 0],+ [5440, 10170, 14185, 32768, 0],+ ],+ ],+ [+ [+ [14406, 20862, 24414, 32768, 0],+ [11824, 18907, 23109, 32768, 0],+ [8257, 14393, 18803, 32768, 0],+ [5860, 10747, 14778, 32768, 0],+ [4475, 8486, 11984, 32768, 0],+ [3606, 6954, 10043, 32768, 0],+ [1736, 3410, 5048, 32768, 0],+ [14430, 20046, 22882, 32768, 0],+ [15593, 22899, 26709, 32768, 0],+ [12102, 19368, 23811, 32768, 0],+ [9059, 15584, 20262, 32768, 0],+ [6999, 12603, 17048, 32768, 0],+ [5684, 10497, 14553, 32768, 0],+ [2822, 5438, 7862, 32768, 0],+ [15785, 21585, 24359, 32768, 0],+ [18347, 25229, 28266, 32768, 0],+ [14974, 22487, 26389, 32768, 0],+ [11423, 18681, 23271, 32768, 0],+ [8863, 15350, 20008, 32768, 0],+ [7153, 12852, 17278, 32768, 0],+ [3707, 7036, 9982, 32768, 0],+ ],+ [+ [15460, 21696, 25469, 32768, 0],+ [12170, 19249, 23191, 32768, 0],+ [8723, 15027, 19332, 32768, 0],+ [6428, 11704, 15874, 32768, 0],+ [4922, 9292, 13052, 32768, 0],+ [4139, 7695, 11010, 32768, 0],+ [2291, 4508, 6598, 32768, 0],+ [19856, 26920, 29828, 32768, 0],+ [17923, 25289, 28792, 32768, 0],+ [14278, 21968, 26297, 32768, 0],+ [10910, 18136, 22950, 32768, 0],+ [8423, 14815, 19627, 32768, 0],+ [6771, 12283, 16774, 32768, 0],+ [4074, 7750, 11081, 32768, 0],+ [19852, 26074, 28672, 32768, 0],+ [19371, 26110, 28989, 32768, 0],+ [16265, 23873, 27663, 32768, 0],+ [12758, 20378, 24952, 32768, 0],+ [10095, 17098, 21961, 32768, 0],+ [8250, 14628, 19451, 32768, 0],+ [5205, 9745, 13622, 32768, 0],+ ],+ ],+ [+ [+ [10563, 16233, 19763, 32768, 0],+ [9794, 16022, 19804, 32768, 0],+ [6750, 11945, 15759, 32768, 0],+ [4963, 9186, 12752, 32768, 0],+ [3845, 7435, 10627, 32768, 0],+ [3051, 6085, 8834, 32768, 0],+ [1311, 2596, 3830, 32768, 0],+ [11246, 16404, 19689, 32768, 0],+ [12315, 18911, 22731, 32768, 0],+ [10557, 17095, 21289, 32768, 0],+ [8136, 14006, 18249, 32768, 0],+ [6348, 11474, 15565, 32768, 0],+ [5196, 9655, 13400, 32768, 0],+ [2349, 4526, 6587, 32768, 0],+ [13337, 18730, 21569, 32768, 0],+ [19306, 26071, 28882, 32768, 0],+ [15952, 23540, 27254, 32768, 0],+ [12409, 19934, 24430, 32768, 0],+ [9760, 16706, 21389, 32768, 0],+ [8004, 14220, 18818, 32768, 0],+ [4138, 7794, 10961, 32768, 0],+ ],+ [+ [10870, 16684, 20949, 32768, 0],+ [9664, 15230, 18680, 32768, 0],+ [6886, 12109, 15408, 32768, 0],+ [4825, 8900, 12305, 32768, 0],+ [3630, 7162, 10314, 32768, 0],+ [3036, 6429, 9387, 32768, 0],+ [1671, 3296, 4940, 32768, 0],+ [13819, 19159, 23026, 32768, 0],+ [11984, 19108, 23120, 32768, 0],+ [10690, 17210, 21663, 32768, 0],+ [7984, 14154, 18333, 32768, 0],+ [6868, 12294, 16124, 32768, 0],+ [5274, 8994, 12868, 32768, 0],+ [2988, 5771, 8424, 32768, 0],+ [19736, 26647, 29141, 32768, 0],+ [18933, 26070, 28984, 32768, 0],+ [15779, 23048, 27200, 32768, 0],+ [12638, 20061, 24532, 32768, 0],+ [10692, 17545, 22220, 32768, 0],+ [9217, 15251, 20054, 32768, 0],+ [5078, 9284, 12594, 32768, 0],+ ],+ ],+ [+ [+ [2331, 3662, 5244, 32768, 0],+ [2891, 4771, 6145, 32768, 0],+ [4598, 7623, 9729, 32768, 0],+ [3520, 6845, 9199, 32768, 0],+ [3417, 6119, 9324, 32768, 0],+ [2601, 5412, 7385, 32768, 0],+ [600, 1173, 1744, 32768, 0],+ [7672, 13286, 17469, 32768, 0],+ [4232, 7792, 10793, 32768, 0],+ [2915, 5317, 7397, 32768, 0],+ [2318, 4356, 6152, 32768, 0],+ [2127, 4000, 5554, 32768, 0],+ [1850, 3478, 5275, 32768, 0],+ [977, 1933, 2843, 32768, 0],+ [18280, 24387, 27989, 32768, 0],+ [15852, 22671, 26185, 32768, 0],+ [13845, 20951, 24789, 32768, 0],+ [11055, 17966, 22129, 32768, 0],+ [9138, 15422, 19801, 32768, 0],+ [7454, 13145, 17456, 32768, 0],+ [3370, 6393, 9013, 32768, 0],+ ],+ [+ [5842, 9229, 10838, 32768, 0],+ [2313, 3491, 4276, 32768, 0],+ [2998, 6104, 7496, 32768, 0],+ [2420, 7447, 9868, 32768, 0],+ [3034, 8495, 10923, 32768, 0],+ [4076, 8937, 10975, 32768, 0],+ [1086, 2370, 3299, 32768, 0],+ [9714, 17254, 20444, 32768, 0],+ [8543, 13698, 17123, 32768, 0],+ [4918, 9007, 11910, 32768, 0],+ [4129, 7532, 10553, 32768, 0],+ [2364, 5533, 8058, 32768, 0],+ [1834, 3546, 5563, 32768, 0],+ [1473, 2908, 4133, 32768, 0],+ [15405, 21193, 25619, 32768, 0],+ [15691, 21952, 26561, 32768, 0],+ [12962, 19194, 24165, 32768, 0],+ [10272, 17855, 22129, 32768, 0],+ [8588, 15270, 20718, 32768, 0],+ [8682, 14669, 19500, 32768, 0],+ [4870, 9636, 13205, 32768, 0],+ ],+ ],+ [+ [+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ ],+ [+ [+ [+ [14995, 21341, 24749, 32768, 0],+ [13158, 20289, 24601, 32768, 0],+ [8941, 15326, 19876, 32768, 0],+ [6297, 11541, 15807, 32768, 0],+ [4817, 9029, 12776, 32768, 0],+ [3731, 7273, 10627, 32768, 0],+ [1847, 3617, 5354, 32768, 0],+ [14472, 19659, 22343, 32768, 0],+ [16806, 24162, 27533, 32768, 0],+ [12900, 20404, 24713, 32768, 0],+ [9411, 16112, 20797, 32768, 0],+ [7056, 12697, 17148, 32768, 0],+ [5544, 10339, 14460, 32768, 0],+ [2954, 5704, 8319, 32768, 0],+ [12464, 18071, 21354, 32768, 0],+ [15482, 22528, 26034, 32768, 0],+ [12070, 19269, 23624, 32768, 0],+ [8953, 15406, 20106, 32768, 0],+ [7027, 12730, 17220, 32768, 0],+ [5887, 10913, 15140, 32768, 0],+ [3793, 7278, 10447, 32768, 0],+ ],+ [+ [15571, 22232, 25749, 32768, 0],+ [14506, 21575, 25374, 32768, 0],+ [10189, 17089, 21569, 32768, 0],+ [7316, 13301, 17915, 32768, 0],+ [5783, 10912, 15190, 32768, 0],+ [4760, 9155, 13088, 32768, 0],+ [2993, 5966, 8774, 32768, 0],+ [23424, 28903, 30778, 32768, 0],+ [20775, 27666, 30290, 32768, 0],+ [16474, 24410, 28299, 32768, 0],+ [12471, 20180, 24987, 32768, 0],+ [9410, 16487, 21439, 32768, 0],+ [7536, 13614, 18529, 32768, 0],+ [5048, 9586, 13549, 32768, 0],+ [21090, 27290, 29756, 32768, 0],+ [20796, 27402, 30026, 32768, 0],+ [17819, 25485, 28969, 32768, 0],+ [13860, 21909, 26462, 32768, 0],+ [11002, 18494, 23529, 32768, 0],+ [8953, 15929, 20897, 32768, 0],+ [6448, 11918, 16454, 32768, 0],+ ],+ ],+ [+ [+ [15999, 22208, 25449, 32768, 0],+ [13050, 19988, 24122, 32768, 0],+ [8594, 14864, 19378, 32768, 0],+ [6033, 11079, 15238, 32768, 0],+ [4554, 8683, 12347, 32768, 0],+ [3672, 7139, 10337, 32768, 0],+ [1900, 3771, 5576, 32768, 0],+ [15788, 21340, 23949, 32768, 0],+ [16825, 24235, 27758, 32768, 0],+ [12873, 20402, 24810, 32768, 0],+ [9590, 16363, 21094, 32768, 0],+ [7352, 13209, 17733, 32768, 0],+ [5960, 10989, 15184, 32768, 0],+ [3232, 6234, 9007, 32768, 0],+ [15761, 20716, 23224, 32768, 0],+ [19318, 25989, 28759, 32768, 0],+ [15529, 23094, 26929, 32768, 0],+ [11662, 18989, 23641, 32768, 0],+ [8955, 15568, 20366, 32768, 0],+ [7281, 13106, 17708, 32768, 0],+ [4248, 8059, 11440, 32768, 0],+ ],+ [+ [14899, 21217, 24503, 32768, 0],+ [13519, 20283, 24047, 32768, 0],+ [9429, 15966, 20365, 32768, 0],+ [6700, 12355, 16652, 32768, 0],+ [5088, 9704, 13716, 32768, 0],+ [4243, 8154, 11731, 32768, 0],+ [2702, 5364, 7861, 32768, 0],+ [22745, 28388, 30454, 32768, 0],+ [20235, 27146, 29922, 32768, 0],+ [15896, 23715, 27637, 32768, 0],+ [11840, 19350, 24131, 32768, 0],+ [9122, 15932, 20880, 32768, 0],+ [7488, 13581, 18362, 32768, 0],+ [5114, 9568, 13370, 32768, 0],+ [20845, 26553, 28932, 32768, 0],+ [20981, 27372, 29884, 32768, 0],+ [17781, 25335, 28785, 32768, 0],+ [13760, 21708, 26297, 32768, 0],+ [10975, 18415, 23365, 32768, 0],+ [9045, 15789, 20686, 32768, 0],+ [6130, 11199, 15423, 32768, 0],+ ],+ ],+ [+ [+ [13549, 19724, 23158, 32768, 0],+ [11844, 18382, 22246, 32768, 0],+ [7919, 13619, 17773, 32768, 0],+ [5486, 10143, 13946, 32768, 0],+ [4166, 7983, 11324, 32768, 0],+ [3364, 6506, 9427, 32768, 0],+ [1598, 3160, 4674, 32768, 0],+ [15281, 20979, 23781, 32768, 0],+ [14939, 22119, 25952, 32768, 0],+ [11363, 18407, 22812, 32768, 0],+ [8609, 14857, 19370, 32768, 0],+ [6737, 12184, 16480, 32768, 0],+ [5506, 10263, 14262, 32768, 0],+ [2990, 5786, 8380, 32768, 0],+ [20249, 25253, 27417, 32768, 0],+ [21070, 27518, 30001, 32768, 0],+ [16854, 24469, 28074, 32768, 0],+ [12864, 20486, 25000, 32768, 0],+ [9962, 16978, 21778, 32768, 0],+ [8074, 14338, 19048, 32768, 0],+ [4494, 8479, 11906, 32768, 0],+ ],+ [+ [13960, 19617, 22829, 32768, 0],+ [11150, 17341, 21228, 32768, 0],+ [7150, 12964, 17190, 32768, 0],+ [5331, 10002, 13867, 32768, 0],+ [4167, 7744, 11057, 32768, 0],+ [3480, 6629, 9646, 32768, 0],+ [1883, 3784, 5686, 32768, 0],+ [18752, 25660, 28912, 32768, 0],+ [16968, 24586, 28030, 32768, 0],+ [13520, 21055, 25313, 32768, 0],+ [10453, 17626, 22280, 32768, 0],+ [8386, 14505, 19116, 32768, 0],+ [6742, 12595, 17008, 32768, 0],+ [4273, 8140, 11499, 32768, 0],+ [22120, 27827, 30233, 32768, 0],+ [20563, 27358, 29895, 32768, 0],+ [17076, 24644, 28153, 32768, 0],+ [13362, 20942, 25309, 32768, 0],+ [10794, 17965, 22695, 32768, 0],+ [9014, 15652, 20319, 32768, 0],+ [5708, 10512, 14497, 32768, 0],+ ],+ ],+ [+ [+ [5705, 10930, 15725, 32768, 0],+ [7946, 12765, 16115, 32768, 0],+ [6801, 12123, 16226, 32768, 0],+ [5462, 10135, 14200, 32768, 0],+ [4189, 8011, 11507, 32768, 0],+ [3191, 6229, 9408, 32768, 0],+ [1057, 2137, 3212, 32768, 0],+ [10018, 17067, 21491, 32768, 0],+ [7380, 12582, 16453, 32768, 0],+ [6068, 10845, 14339, 32768, 0],+ [5098, 9198, 12555, 32768, 0],+ [4312, 8010, 11119, 32768, 0],+ [3700, 6966, 9781, 32768, 0],+ [1693, 3326, 4887, 32768, 0],+ [18757, 24930, 27774, 32768, 0],+ [17648, 24596, 27817, 32768, 0],+ [14707, 22052, 26026, 32768, 0],+ [11720, 18852, 23292, 32768, 0],+ [9357, 15952, 20525, 32768, 0],+ [7810, 13753, 18210, 32768, 0],+ [3879, 7333, 10328, 32768, 0],+ ],+ [+ [8278, 13242, 15922, 32768, 0],+ [10547, 15867, 18919, 32768, 0],+ [9106, 15842, 20609, 32768, 0],+ [6833, 13007, 17218, 32768, 0],+ [4811, 9712, 13923, 32768, 0],+ [3985, 7352, 11128, 32768, 0],+ [1688, 3458, 5262, 32768, 0],+ [12951, 21861, 26510, 32768, 0],+ [9788, 16044, 20276, 32768, 0],+ [6309, 11244, 14870, 32768, 0],+ [5183, 9349, 12566, 32768, 0],+ [4389, 8229, 11492, 32768, 0],+ [3633, 6945, 10620, 32768, 0],+ [3600, 6847, 9907, 32768, 0],+ [21748, 28137, 30255, 32768, 0],+ [19436, 26581, 29560, 32768, 0],+ [16359, 24201, 27953, 32768, 0],+ [13961, 21693, 25871, 32768, 0],+ [11544, 18686, 23322, 32768, 0],+ [9372, 16462, 20952, 32768, 0],+ [6138, 11210, 15390, 32768, 0],+ ],+ ],+ [+ [+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ ],+ [+ [+ [+ [16138, 22223, 25509, 32768, 0],+ [15347, 22430, 26332, 32768, 0],+ [9614, 16736, 21332, 32768, 0],+ [6600, 12275, 16907, 32768, 0],+ [4811, 9424, 13547, 32768, 0],+ [3748, 7809, 11420, 32768, 0],+ [2254, 4587, 6890, 32768, 0],+ [15196, 20284, 23177, 32768, 0],+ [18317, 25469, 28451, 32768, 0],+ [13918, 21651, 25842, 32768, 0],+ [10052, 17150, 21995, 32768, 0],+ [7499, 13630, 18587, 32768, 0],+ [6158, 11417, 16003, 32768, 0],+ [4014, 7785, 11252, 32768, 0],+ [15048, 21067, 24384, 32768, 0],+ [18202, 25346, 28553, 32768, 0],+ [14302, 22019, 26356, 32768, 0],+ [10839, 18139, 23166, 32768, 0],+ [8715, 15744, 20806, 32768, 0],+ [7536, 13576, 18544, 32768, 0],+ [5413, 10335, 14498, 32768, 0],+ ],+ [+ [17394, 24501, 27895, 32768, 0],+ [15889, 23420, 27185, 32768, 0],+ [11561, 19133, 23870, 32768, 0],+ [8285, 14812, 19844, 32768, 0],+ [6496, 12043, 16550, 32768, 0],+ [4771, 9574, 13677, 32768, 0],+ [3603, 6830, 10144, 32768, 0],+ [21656, 27704, 30200, 32768, 0],+ [21324, 27915, 30511, 32768, 0],+ [17327, 25336, 28997, 32768, 0],+ [13417, 21381, 26033, 32768, 0],+ [10132, 17425, 22338, 32768, 0],+ [8580, 15016, 19633, 32768, 0],+ [5694, 11477, 16411, 32768, 0],+ [24116, 29780, 31450, 32768, 0],+ [23853, 29695, 31591, 32768, 0],+ [20085, 27614, 30428, 32768, 0],+ [15326, 24335, 28575, 32768, 0],+ [11814, 19472, 24810, 32768, 0],+ [10221, 18611, 24767, 32768, 0],+ [7689, 14558, 20321, 32768, 0],+ ],+ ],+ [+ [+ [16214, 22380, 25770, 32768, 0],+ [14213, 21304, 25295, 32768, 0],+ [9213, 15823, 20455, 32768, 0],+ [6395, 11758, 16139, 32768, 0],+ [4779, 9187, 13066, 32768, 0],+ [3821, 7501, 10953, 32768, 0],+ [2293, 4567, 6795, 32768, 0],+ [15859, 21283, 23820, 32768, 0],+ [18404, 25602, 28726, 32768, 0],+ [14325, 21980, 26206, 32768, 0],+ [10669, 17937, 22720, 32768, 0],+ [8297, 14642, 19447, 32768, 0],+ [6746, 12389, 16893, 32768, 0],+ [4324, 8251, 11770, 32768, 0],+ [16532, 21631, 24475, 32768, 0],+ [20667, 27150, 29668, 32768, 0],+ [16728, 24510, 28175, 32768, 0],+ [12861, 20645, 25332, 32768, 0],+ [10076, 17361, 22417, 32768, 0],+ [8395, 14940, 19963, 32768, 0],+ [5731, 10683, 14912, 32768, 0],+ ],+ [+ [14433, 21155, 24938, 32768, 0],+ [14658, 21716, 25545, 32768, 0],+ [9923, 16824, 21557, 32768, 0],+ [6982, 13052, 17721, 32768, 0],+ [5419, 10503, 15050, 32768, 0],+ [4852, 9162, 13014, 32768, 0],+ [3271, 6395, 9630, 32768, 0],+ [22210, 27833, 30109, 32768, 0],+ [20750, 27368, 29821, 32768, 0],+ [16894, 24828, 28573, 32768, 0],+ [13247, 21276, 25757, 32768, 0],+ [10038, 17265, 22563, 32768, 0],+ [8587, 14947, 20327, 32768, 0],+ [5645, 11371, 15252, 32768, 0],+ [22027, 27526, 29714, 32768, 0],+ [23098, 29146, 31221, 32768, 0],+ [19886, 27341, 30272, 32768, 0],+ [15609, 23747, 28046, 32768, 0],+ [11993, 20065, 24939, 32768, 0],+ [9637, 18267, 23671, 32768, 0],+ [7625, 13801, 19144, 32768, 0],+ ],+ ],+ [+ [+ [14438, 20798, 24089, 32768, 0],+ [12621, 19203, 23097, 32768, 0],+ [8177, 14125, 18402, 32768, 0],+ [5674, 10501, 14456, 32768, 0],+ [4236, 8239, 11733, 32768, 0],+ [3447, 6750, 9806, 32768, 0],+ [1986, 3950, 5864, 32768, 0],+ [16208, 22099, 24930, 32768, 0],+ [16537, 24025, 27585, 32768, 0],+ [12780, 20381, 24867, 32768, 0],+ [9767, 16612, 21416, 32768, 0],+ [7686, 13738, 18398, 32768, 0],+ [6333, 11614, 15964, 32768, 0],+ [3941, 7571, 10836, 32768, 0],+ [22819, 27422, 29202, 32768, 0],+ [22224, 28514, 30721, 32768, 0],+ [17660, 25433, 28913, 32768, 0],+ [13574, 21482, 26002, 32768, 0],+ [10629, 17977, 22938, 32768, 0],+ [8612, 15298, 20265, 32768, 0],+ [5607, 10491, 14596, 32768, 0],+ ],+ [+ [13569, 19800, 23206, 32768, 0],+ [13128, 19924, 23869, 32768, 0],+ [8329, 14841, 19403, 32768, 0],+ [6130, 10976, 15057, 32768, 0],+ [4682, 8839, 12518, 32768, 0],+ [3656, 7409, 10588, 32768, 0],+ [2577, 5099, 7412, 32768, 0],+ [22427, 28684, 30585, 32768, 0],+ [20913, 27750, 30139, 32768, 0],+ [15840, 24109, 27834, 32768, 0],+ [12308, 20029, 24569, 32768, 0],+ [10216, 16785, 21458, 32768, 0],+ [8309, 14203, 19113, 32768, 0],+ [6043, 11168, 15307, 32768, 0],+ [23166, 28901, 30998, 32768, 0],+ [21899, 28405, 30751, 32768, 0],+ [18413, 26091, 29443, 32768, 0],+ [15233, 23114, 27352, 32768, 0],+ [12683, 20472, 25288, 32768, 0],+ [10702, 18259, 23409, 32768, 0],+ [8125, 14464, 19226, 32768, 0],+ ],+ ],+ [+ [+ [9040, 14786, 18360, 32768, 0],+ [9979, 15718, 19415, 32768, 0],+ [7913, 13918, 18311, 32768, 0],+ [5859, 10889, 15184, 32768, 0],+ [4593, 8677, 12510, 32768, 0],+ [3820, 7396, 10791, 32768, 0],+ [1730, 3471, 5192, 32768, 0],+ [11803, 18365, 22709, 32768, 0],+ [11419, 18058, 22225, 32768, 0],+ [9418, 15774, 20243, 32768, 0],+ [7539, 13325, 17657, 32768, 0],+ [6233, 11317, 15384, 32768, 0],+ [5137, 9656, 13545, 32768, 0],+ [2977, 5774, 8349, 32768, 0],+ [21207, 27246, 29640, 32768, 0],+ [19547, 26578, 29497, 32768, 0],+ [16169, 23871, 27690, 32768, 0],+ [12820, 20458, 25018, 32768, 0],+ [10224, 17332, 22214, 32768, 0],+ [8526, 15048, 19884, 32768, 0],+ [5037, 9410, 13118, 32768, 0],+ ],+ [+ [12339, 17329, 20140, 32768, 0],+ [13505, 19895, 23225, 32768, 0],+ [9847, 16944, 21564, 32768, 0],+ [7280, 13256, 18348, 32768, 0],+ [4712, 10009, 14454, 32768, 0],+ [4361, 7914, 12477, 32768, 0],+ [2870, 5628, 7995, 32768, 0],+ [20061, 25504, 28526, 32768, 0],+ [15235, 22878, 26145, 32768, 0],+ [12985, 19958, 24155, 32768, 0],+ [9782, 16641, 21403, 32768, 0],+ [9456, 16360, 20760, 32768, 0],+ [6855, 12940, 18557, 32768, 0],+ [5661, 10564, 15002, 32768, 0],+ [25656, 30602, 31894, 32768, 0],+ [22570, 29107, 31092, 32768, 0],+ [18917, 26423, 29541, 32768, 0],+ [15940, 23649, 27754, 32768, 0],+ [12803, 20581, 25219, 32768, 0],+ [11082, 18695, 23376, 32768, 0],+ [7939, 14373, 19005, 32768, 0],+ ],+ ],+ [+ [+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ ],+ [+ [+ [+ [18315, 24289, 27551, 32768, 0],+ [16854, 24068, 27835, 32768, 0],+ [10140, 17927, 23173, 32768, 0],+ [6722, 12982, 18267, 32768, 0],+ [4661, 9826, 14706, 32768, 0],+ [3832, 8165, 12294, 32768, 0],+ [2795, 6098, 9245, 32768, 0],+ [17145, 23326, 26672, 32768, 0],+ [20733, 27680, 30308, 32768, 0],+ [16032, 24461, 28546, 32768, 0],+ [11653, 20093, 25081, 32768, 0],+ [9290, 16429, 22086, 32768, 0],+ [7796, 14598, 19982, 32768, 0],+ [6502, 12378, 17441, 32768, 0],+ [21681, 27732, 30320, 32768, 0],+ [22389, 29044, 31261, 32768, 0],+ [19027, 26731, 30087, 32768, 0],+ [14739, 23755, 28624, 32768, 0],+ [11358, 20778, 25511, 32768, 0],+ [10995, 18073, 24190, 32768, 0],+ [9162, 14990, 20617, 32768, 0],+ ],+ [+ [21425, 27952, 30388, 32768, 0],+ [18062, 25838, 29034, 32768, 0],+ [11956, 19881, 24808, 32768, 0],+ [7718, 15000, 20980, 32768, 0],+ [5702, 11254, 16143, 32768, 0],+ [4898, 9088, 16864, 32768, 0],+ [3679, 6776, 11907, 32768, 0],+ [23294, 30160, 31663, 32768, 0],+ [24397, 29896, 31836, 32768, 0],+ [19245, 27128, 30593, 32768, 0],+ [13202, 19825, 26404, 32768, 0],+ [11578, 19297, 23957, 32768, 0],+ [8073, 13297, 21370, 32768, 0],+ [5461, 10923, 19745, 32768, 0],+ [27367, 30521, 31934, 32768, 0],+ [24904, 30671, 31940, 32768, 0],+ [23075, 28460, 31299, 32768, 0],+ [14400, 23658, 30417, 32768, 0],+ [13885, 23882, 28325, 32768, 0],+ [14746, 22938, 27853, 32768, 0],+ [5461, 16384, 27307, 32768, 0],+ ],+ ],+ [+ [+ [18274, 24813, 27890, 32768, 0],+ [15537, 23149, 27003, 32768, 0],+ [9449, 16740, 21827, 32768, 0],+ [6700, 12498, 17261, 32768, 0],+ [4988, 9866, 14198, 32768, 0],+ [4236, 8147, 11902, 32768, 0],+ [2867, 5860, 8654, 32768, 0],+ [17124, 23171, 26101, 32768, 0],+ [20396, 27477, 30148, 32768, 0],+ [16573, 24629, 28492, 32768, 0],+ [12749, 20846, 25674, 32768, 0],+ [10233, 17878, 22818, 32768, 0],+ [8525, 15332, 20363, 32768, 0],+ [6283, 11632, 16255, 32768, 0],+ [20466, 26511, 29286, 32768, 0],+ [23059, 29174, 31191, 32768, 0],+ [19481, 27263, 30241, 32768, 0],+ [15458, 23631, 28137, 32768, 0],+ [12416, 20608, 25693, 32768, 0],+ [10261, 18011, 23261, 32768, 0],+ [8016, 14655, 19666, 32768, 0],+ ],+ [+ [17616, 24586, 28112, 32768, 0],+ [15809, 23299, 27155, 32768, 0],+ [10767, 18890, 23793, 32768, 0],+ [7727, 14255, 18865, 32768, 0],+ [6129, 11926, 16882, 32768, 0],+ [4482, 9704, 14861, 32768, 0],+ [3277, 7452, 11522, 32768, 0],+ [22956, 28551, 30730, 32768, 0],+ [22724, 28937, 30961, 32768, 0],+ [18467, 26324, 29580, 32768, 0],+ [13234, 20713, 25649, 32768, 0],+ [11181, 17592, 22481, 32768, 0],+ [8291, 18358, 24576, 32768, 0],+ [7568, 11881, 14984, 32768, 0],+ [24948, 29001, 31147, 32768, 0],+ [25674, 30619, 32151, 32768, 0],+ [20841, 26793, 29603, 32768, 0],+ [14669, 24356, 28666, 32768, 0],+ [11334, 23593, 28219, 32768, 0],+ [8922, 14762, 22873, 32768, 0],+ [8301, 13544, 20535, 32768, 0],+ ],+ ],+ [+ [+ [17113, 23733, 27081, 32768, 0],+ [14139, 21406, 25452, 32768, 0],+ [8552, 15002, 19776, 32768, 0],+ [5871, 11120, 15378, 32768, 0],+ [4455, 8616, 12253, 32768, 0],+ [3469, 6910, 10386, 32768, 0],+ [2255, 4553, 6782, 32768, 0],+ [18224, 24376, 27053, 32768, 0],+ [19290, 26710, 29614, 32768, 0],+ [14936, 22991, 27184, 32768, 0],+ [11238, 18951, 23762, 32768, 0],+ [8786, 15617, 20588, 32768, 0],+ [7317, 13228, 18003, 32768, 0],+ [5101, 9512, 13493, 32768, 0],+ [22639, 28222, 30210, 32768, 0],+ [23216, 29331, 31307, 32768, 0],+ [19075, 26762, 29895, 32768, 0],+ [15014, 23113, 27457, 32768, 0],+ [11938, 19857, 24752, 32768, 0],+ [9942, 17280, 22282, 32768, 0],+ [7167, 13144, 17752, 32768, 0],+ ],+ [+ [15820, 22738, 26488, 32768, 0],+ [13530, 20885, 25216, 32768, 0],+ [8395, 15530, 20452, 32768, 0],+ [6574, 12321, 16380, 32768, 0],+ [5353, 10419, 14568, 32768, 0],+ [4613, 8446, 12381, 32768, 0],+ [3440, 7158, 9903, 32768, 0],+ [24247, 29051, 31224, 32768, 0],+ [22118, 28058, 30369, 32768, 0],+ [16498, 24768, 28389, 32768, 0],+ [12920, 21175, 26137, 32768, 0],+ [10730, 18619, 25352, 32768, 0],+ [10187, 16279, 22791, 32768, 0],+ [9310, 14631, 22127, 32768, 0],+ [24970, 30558, 32057, 32768, 0],+ [24801, 29942, 31698, 32768, 0],+ [22432, 28453, 30855, 32768, 0],+ [19054, 25680, 29580, 32768, 0],+ [14392, 23036, 28109, 32768, 0],+ [12495, 20947, 26650, 32768, 0],+ [12442, 20326, 26214, 32768, 0],+ ],+ ],+ [+ [+ [12162, 18785, 22648, 32768, 0],+ [12749, 19697, 23806, 32768, 0],+ [8580, 15297, 20346, 32768, 0],+ [6169, 11749, 16543, 32768, 0],+ [4836, 9391, 13448, 32768, 0],+ [3821, 7711, 11613, 32768, 0],+ [2228, 4601, 7070, 32768, 0],+ [16319, 24725, 28280, 32768, 0],+ [15698, 23277, 27168, 32768, 0],+ [12726, 20368, 25047, 32768, 0],+ [9912, 17015, 21976, 32768, 0],+ [7888, 14220, 19179, 32768, 0],+ [6777, 12284, 17018, 32768, 0],+ [4492, 8590, 12252, 32768, 0],+ [23249, 28904, 30947, 32768, 0],+ [21050, 27908, 30512, 32768, 0],+ [17440, 25340, 28949, 32768, 0],+ [14059, 22018, 26541, 32768, 0],+ [11288, 18903, 23898, 32768, 0],+ [9411, 16342, 21428, 32768, 0],+ [6278, 11588, 15944, 32768, 0],+ ],+ [+ [13981, 20067, 23226, 32768, 0],+ [16922, 23580, 26783, 32768, 0],+ [11005, 19039, 24487, 32768, 0],+ [7389, 14218, 19798, 32768, 0],+ [5598, 11505, 17206, 32768, 0],+ [6090, 11213, 15659, 32768, 0],+ [3820, 7371, 10119, 32768, 0],+ [21082, 26925, 29675, 32768, 0],+ [21262, 28627, 31128, 32768, 0],+ [18392, 26454, 30437, 32768, 0],+ [14870, 22910, 27096, 32768, 0],+ [12620, 19484, 24908, 32768, 0],+ [9290, 16553, 22802, 32768, 0],+ [6668, 14288, 20004, 32768, 0],+ [27704, 31055, 31949, 32768, 0],+ [24709, 29978, 31788, 32768, 0],+ [21668, 29264, 31657, 32768, 0],+ [18295, 26968, 30074, 32768, 0],+ [16399, 24422, 29313, 32768, 0],+ [14347, 23026, 28104, 32768, 0],+ [12370, 19806, 24477, 32768, 0],+ ],+ ],+ [+ [+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ [+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ [8192, 16384, 24576, 32768, 0],+ ],+ ],+ ],+];++pub static DEFAULT_DC_SIGN_CDF: [[[[u16; 3]; 3]; 2]; 4] = [+ [[[16000, 32768, 0], [13056, 32768, 0], [18816, 32768, 0]], [[15232, 32768, 0], [12928, 32768, 0], [17280, 32768, 0]]],+ [[[16000, 32768, 0], [13056, 32768, 0], [18816, 32768, 0]], [[15232, 32768, 0], [12928, 32768, 0], [17280, 32768, 0]]],+ [[[16000, 32768, 0], [13056, 32768, 0], [18816, 32768, 0]], [[15232, 32768, 0], [12928, 32768, 0], [17280, 32768, 0]]],+ [[[16000, 32768, 0], [13056, 32768, 0], [18816, 32768, 0]], [[15232, 32768, 0], [12928, 32768, 0], [17280, 32768, 0]]],+];++pub static DEFAULT_DRL_MODE_CDF: [[u16; 3]; 3] = [[13104, 32768, 0], [24560, 32768, 0], [18945, 32768, 0]];++pub static DEFAULT_EOB_EXTRA_CDF: [[[[[u16; 3]; 9]; 2]; 5]; 4] = [+ [+ [+ [+ [16961, 32768, 0],+ [17223, 32768, 0],+ [7621, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [19069, 32768, 0],+ [22525, 32768, 0],+ [13377, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ [+ [+ [20401, 32768, 0],+ [17025, 32768, 0],+ [12845, 32768, 0],+ [12873, 32768, 0],+ [14094, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [20681, 32768, 0],+ [20701, 32768, 0],+ [15250, 32768, 0],+ [15017, 32768, 0],+ [14928, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ [+ [+ [23905, 32768, 0],+ [17194, 32768, 0],+ [16170, 32768, 0],+ [17695, 32768, 0],+ [13826, 32768, 0],+ [15810, 32768, 0],+ [12036, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [23959, 32768, 0],+ [20799, 32768, 0],+ [19021, 32768, 0],+ [16203, 32768, 0],+ [17886, 32768, 0],+ [14144, 32768, 0],+ [12010, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ [+ [+ [27399, 32768, 0],+ [16327, 32768, 0],+ [18071, 32768, 0],+ [19584, 32768, 0],+ [20721, 32768, 0],+ [18432, 32768, 0],+ [19560, 32768, 0],+ [10150, 32768, 0],+ [8805, 32768, 0],+ ],+ [+ [24932, 32768, 0],+ [20833, 32768, 0],+ [12027, 32768, 0],+ [16670, 32768, 0],+ [19914, 32768, 0],+ [15106, 32768, 0],+ [17662, 32768, 0],+ [13783, 32768, 0],+ [28756, 32768, 0],+ ],+ ],+ [+ [+ [23406, 32768, 0],+ [21845, 32768, 0],+ [18432, 32768, 0],+ [16384, 32768, 0],+ [17096, 32768, 0],+ [12561, 32768, 0],+ [17320, 32768, 0],+ [22395, 32768, 0],+ [21370, 32768, 0],+ ],+ [+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ ],+ [+ [+ [+ [17471, 32768, 0],+ [20223, 32768, 0],+ [11357, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [20335, 32768, 0],+ [21667, 32768, 0],+ [14818, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ [+ [+ [20430, 32768, 0],+ [20662, 32768, 0],+ [15367, 32768, 0],+ [16970, 32768, 0],+ [14657, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [22117, 32768, 0],+ [22028, 32768, 0],+ [18650, 32768, 0],+ [16042, 32768, 0],+ [15885, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ [+ [+ [22409, 32768, 0],+ [21012, 32768, 0],+ [15650, 32768, 0],+ [17395, 32768, 0],+ [15469, 32768, 0],+ [20205, 32768, 0],+ [19511, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [24220, 32768, 0],+ [22480, 32768, 0],+ [17737, 32768, 0],+ [18916, 32768, 0],+ [19268, 32768, 0],+ [18412, 32768, 0],+ [18844, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ [+ [+ [25991, 32768, 0],+ [20314, 32768, 0],+ [17731, 32768, 0],+ [19678, 32768, 0],+ [18649, 32768, 0],+ [17307, 32768, 0],+ [21798, 32768, 0],+ [17549, 32768, 0],+ [15630, 32768, 0],+ ],+ [+ [26585, 32768, 0],+ [21469, 32768, 0],+ [20432, 32768, 0],+ [17735, 32768, 0],+ [19280, 32768, 0],+ [15235, 32768, 0],+ [20297, 32768, 0],+ [22471, 32768, 0],+ [28997, 32768, 0],+ ],+ ],+ [+ [+ [26605, 32768, 0],+ [11304, 32768, 0],+ [16726, 32768, 0],+ [16560, 32768, 0],+ [20866, 32768, 0],+ [23524, 32768, 0],+ [19878, 32768, 0],+ [13469, 32768, 0],+ [23084, 32768, 0],+ ],+ [+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ ],+ [+ [+ [+ [18983, 32768, 0],+ [20512, 32768, 0],+ [14885, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [20090, 32768, 0],+ [19444, 32768, 0],+ [17286, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ [+ [+ [19139, 32768, 0],+ [21487, 32768, 0],+ [18959, 32768, 0],+ [20910, 32768, 0],+ [19089, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [20536, 32768, 0],+ [20664, 32768, 0],+ [20625, 32768, 0],+ [19123, 32768, 0],+ [14862, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ [+ [+ [19833, 32768, 0],+ [21502, 32768, 0],+ [17485, 32768, 0],+ [20267, 32768, 0],+ [18353, 32768, 0],+ [23329, 32768, 0],+ [21478, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [22041, 32768, 0],+ [23434, 32768, 0],+ [20001, 32768, 0],+ [20554, 32768, 0],+ [20951, 32768, 0],+ [20145, 32768, 0],+ [15562, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ [+ [+ [23312, 32768, 0],+ [21607, 32768, 0],+ [16526, 32768, 0],+ [18957, 32768, 0],+ [18034, 32768, 0],+ [18934, 32768, 0],+ [24247, 32768, 0],+ [16921, 32768, 0],+ [17080, 32768, 0],+ ],+ [+ [26579, 32768, 0],+ [24910, 32768, 0],+ [18637, 32768, 0],+ [19800, 32768, 0],+ [20388, 32768, 0],+ [9887, 32768, 0],+ [15642, 32768, 0],+ [30198, 32768, 0],+ [24721, 32768, 0],+ ],+ ],+ [+ [+ [26998, 32768, 0],+ [16737, 32768, 0],+ [17838, 32768, 0],+ [18922, 32768, 0],+ [19515, 32768, 0],+ [18636, 32768, 0],+ [17333, 32768, 0],+ [15776, 32768, 0],+ [22658, 32768, 0],+ ],+ [+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ ],+ [+ [+ [+ [20177, 32768, 0],+ [20789, 32768, 0],+ [20262, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [21416, 32768, 0],+ [20855, 32768, 0],+ [23410, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ [+ [+ [20238, 32768, 0],+ [21057, 32768, 0],+ [19159, 32768, 0],+ [22337, 32768, 0],+ [20159, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [20125, 32768, 0],+ [20559, 32768, 0],+ [21707, 32768, 0],+ [22296, 32768, 0],+ [17333, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ [+ [+ [19941, 32768, 0],+ [20527, 32768, 0],+ [21470, 32768, 0],+ [22487, 32768, 0],+ [19558, 32768, 0],+ [22354, 32768, 0],+ [20331, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [22752, 32768, 0],+ [25006, 32768, 0],+ [22075, 32768, 0],+ [21576, 32768, 0],+ [17740, 32768, 0],+ [21690, 32768, 0],+ [19211, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ [+ [+ [21442, 32768, 0],+ [22358, 32768, 0],+ [18503, 32768, 0],+ [20291, 32768, 0],+ [19945, 32768, 0],+ [21294, 32768, 0],+ [21178, 32768, 0],+ [19400, 32768, 0],+ [10556, 32768, 0],+ ],+ [+ [24648, 32768, 0],+ [24949, 32768, 0],+ [20708, 32768, 0],+ [23905, 32768, 0],+ [20501, 32768, 0],+ [9558, 32768, 0],+ [9423, 32768, 0],+ [30365, 32768, 0],+ [19253, 32768, 0],+ ],+ ],+ [+ [+ [26064, 32768, 0],+ [22098, 32768, 0],+ [19613, 32768, 0],+ [20525, 32768, 0],+ [17595, 32768, 0],+ [16618, 32768, 0],+ [20497, 32768, 0],+ [18989, 32768, 0],+ [15513, 32768, 0],+ ],+ [+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ ],+];++pub static DEFAULT_EOB_PT_1024_CDF: [[[u16; 12]; 2]; 4] = [+ [[393, 421, 751, 1623, 3160, 6352, 13345, 18047, 22571, 25830, 32768, 0], [1865, 1988, 2930, 4242, 10533, 16538, 21354, 27255, 28546, 31784, 32768, 0]],+ [[696, 948, 3145, 5702, 9706, 13217, 17851, 21856, 25692, 28034, 32768, 0], [2672, 3591, 9330, 17084, 22725, 24284, 26527, 28027, 28377, 30876, 32768, 0]],+ [+ [2784, 3831, 7041, 10521, 14847, 18844, 23155, 26682, 29229, 31045, 32768, 0],+ [9577, 12466, 17739, 20750, 22061, 23215, 24601, 25483, 25843, 32056, 32768, 0],+ ],+ [+ [6698, 8334, 11961, 15762, 20186, 23862, 27434, 29326, 31082, 32050, 32768, 0],+ [20569, 22426, 25569, 26859, 28053, 28913, 29486, 29724, 29807, 32570, 32768, 0],+ ],+];++pub static DEFAULT_EOB_PT_128_CDF: [[[[u16; 9]; 2]; 2]; 4] = [+ [+ [[219, 482, 1140, 2091, 3680, 6028, 12586, 32768, 0], [371, 699, 1254, 4830, 9479, 12562, 17497, 32768, 0]],+ [[5245, 7456, 12880, 15852, 20033, 23932, 27608, 32768, 0], [2054, 3472, 5869, 14232, 18242, 20590, 26752, 32768, 0]],+ ],+ [+ [[685, 933, 1488, 2714, 4766, 8562, 19254, 32768, 0], [217, 352, 618, 2303, 5261, 9969, 17472, 32768, 0]],+ [[8045, 11200, 15497, 19595, 23948, 27408, 30938, 32768, 0], [2310, 4160, 7471, 14997, 17931, 20768, 30240, 32768, 0]],+ ],+ [+ [[1366, 1738, 2527, 5016, 9355, 15797, 24643, 32768, 0], [354, 558, 944, 2760, 7287, 14037, 21779, 32768, 0]],+ [[13627, 16246, 20173, 24429, 27948, 30415, 31863, 32768, 0], [6275, 9889, 14769, 23164, 27988, 30493, 32272, 32768, 0]],+ ],+ [+ [[3472, 4885, 7489, 12481, 18517, 24536, 29635, 32768, 0], [886, 1731, 3271, 8469, 15569, 22126, 28383, 32768, 0]],+ [[24313, 26062, 28385, 30107, 31217, 31898, 32345, 32768, 0], [9165, 13282, 21150, 30286, 31894, 32571, 32712, 32768, 0]],+ ],+];++pub static DEFAULT_EOB_PT_16_CDF: [[[[u16; 6]; 2]; 2]; 4] = [+ [[[840, 1039, 1980, 4895, 32768, 0], [370, 671, 1883, 4471, 32768, 0]], [[3247, 4950, 9688, 14563, 32768, 0], [1904, 3354, 7763, 14647, 32768, 0]]],+ [[[2125, 2551, 5165, 8946, 32768, 0], [513, 765, 1859, 6339, 32768, 0]], [[7637, 9498, 14259, 19108, 32768, 0], [2497, 4096, 8866, 16993, 32768, 0]]],+ [[[4016, 4897, 8881, 14968, 32768, 0], [716, 1105, 2646, 10056, 32768, 0]], [[11139, 13270, 18241, 23566, 32768, 0], [3192, 5032, 10297, 19755, 32768, 0]]],+ [+ [[6708, 8958, 14746, 22133, 32768, 0], [1222, 2074, 4783, 15410, 32768, 0]],+ [[19575, 21766, 26044, 29709, 32768, 0], [7297, 10767, 19273, 28194, 32768, 0]],+ ],+];++pub static DEFAULT_EOB_PT_256_CDF: [[[[u16; 10]; 2]; 2]; 4] = [+ [+ [[310, 584, 1887, 3589, 6168, 8611, 11352, 15652, 32768, 0], [998, 1850, 2998, 5604, 17341, 19888, 22899, 25583, 32768, 0]],+ [[2520, 3240, 5952, 8870, 12577, 17558, 19954, 24168, 32768, 0], [2203, 4130, 7435, 10739, 20652, 23681, 25609, 27261, 32768, 0]],+ ],+ [+ [[1448, 2109, 4151, 6263, 9329, 13260, 17944, 23300, 32768, 0], [399, 1019, 1749, 3038, 10444, 15546, 22739, 27294, 32768, 0]],+ [[6402, 8148, 12623, 15072, 18728, 22847, 26447, 29377, 32768, 0], [1674, 3252, 5734, 10159, 22397, 23802, 24821, 30940, 32768, 0]],+ ],+ [+ [[3089, 3920, 6038, 9460, 14266, 19881, 25766, 29176, 32768, 0], [1084, 2358, 3488, 5122, 11483, 18103, 26023, 29799, 32768, 0]],+ [[11514, 13794, 17480, 20754, 24361, 27378, 29492, 31277, 32768, 0], [6571, 9610, 15516, 21826, 29092, 30829, 31842, 32708, 32768, 0]],+ ],+ [+ [[5348, 7113, 11820, 15924, 22106, 26777, 30334, 31757, 32768, 0], [2453, 4474, 6307, 8777, 16474, 22975, 29000, 31547, 32768, 0]],+ [[23110, 24597, 27140, 28894, 30167, 30927, 31392, 32094, 32768, 0], [9998, 17661, 25178, 28097, 31308, 32038, 32403, 32695, 32768, 0]],+ ],+];++pub static DEFAULT_EOB_PT_32_CDF: [[[[u16; 7]; 2]; 2]; 4] = [+ [+ [[400, 520, 977, 2102, 6542, 32768, 0], [210, 405, 1315, 3326, 7537, 32768, 0]],+ [[2636, 4273, 7588, 11794, 20401, 32768, 0], [1786, 3179, 6902, 11357, 19054, 32768, 0]],+ ],+ [+ [[989, 1249, 2019, 4151, 10785, 32768, 0], [313, 441, 1099, 2917, 8562, 32768, 0]],+ [[8394, 10352, 13932, 18855, 26014, 32768, 0], [2578, 4124, 8181, 13670, 24234, 32768, 0]],+ ],+ [+ [[2515, 3003, 4452, 8162, 16041, 32768, 0], [574, 821, 1836, 5089, 13128, 32768, 0]],+ [[13468, 16303, 20361, 25105, 29281, 32768, 0], [3542, 5502, 10415, 16760, 25644, 32768, 0]],+ ],+ [+ [[4617, 5709, 8446, 13584, 23135, 32768, 0], [1156, 1702, 3675, 9274, 20539, 32768, 0]],+ [[22086, 24282, 27010, 29770, 31743, 32768, 0], [7699, 10897, 20891, 26926, 31628, 32768, 0]],+ ],+];++pub static DEFAULT_EOB_PT_512_CDF: [[[u16; 11]; 2]; 4] = [+ [[641, 983, 3707, 5430, 10234, 14958, 18788, 23412, 26061, 32768, 0], [5095, 6446, 9996, 13354, 16017, 17986, 20919, 26129, 29140, 32768, 0]],+ [[1230, 2278, 5035, 7776, 11871, 15346, 19590, 24584, 28749, 32768, 0], [7265, 9979, 15819, 19250, 21780, 23846, 26478, 28396, 31811, 32768, 0]],+ [[2624, 3936, 6480, 9686, 13979, 17726, 23267, 28410, 31078, 32768, 0], [12015, 14769, 19588, 22052, 24222, 25812, 27300, 29219, 32114, 32768, 0]],+ [[5927, 7809, 10923, 14597, 19439, 24135, 28456, 31142, 32060, 32768, 0], [21093, 23043, 25742, 27658, 29097, 29716, 30073, 30820, 31956, 32768, 0]],+];++pub static DEFAULT_EOB_PT_64_CDF: [[[[u16; 8]; 2]; 2]; 4] = [+ [+ [[329, 498, 1101, 1784, 3265, 7758, 32768, 0], [335, 730, 1459, 5494, 8755, 12997, 32768, 0]],+ [[3505, 5304, 10086, 13814, 17684, 23370, 32768, 0], [1563, 2700, 4876, 10911, 14706, 22480, 32768, 0]],+ ],+ [+ [[1260, 1446, 2253, 3712, 6652, 13369, 32768, 0], [401, 605, 1029, 2563, 5845, 12626, 32768, 0]],+ [[8609, 10612, 14624, 18714, 22614, 29024, 32768, 0], [1923, 3127, 5867, 9703, 14277, 27100, 32768, 0]],+ ],+ [+ [[2374, 2772, 4583, 7276, 12288, 19706, 32768, 0], [497, 810, 1315, 3000, 7004, 15641, 32768, 0]],+ [[15050, 17126, 21410, 24886, 28156, 30726, 32768, 0], [4034, 6290, 10235, 14982, 21214, 28491, 32768, 0]],+ ],+ [+ [[6307, 7541, 12060, 16358, 22553, 27865, 32768, 0], [1289, 2320, 3971, 7926, 14153, 24291, 32768, 0]],+ [[24212, 25708, 28268, 30035, 31307, 32049, 32768, 0], [8726, 12378, 19409, 26450, 30038, 32462, 32768, 0]],+ ],+];++pub static DEFAULT_INTER_TX_TYPE_SET3_CDF: [[u16; 3]; 4] = [[16384, 32768, 0], [4167, 32768, 0], [1998, 32768, 0], [748, 32768, 0]];++pub static DEFAULT_INTRA_FRAME_Y_MODE_CDF: [[[u16; 14]; 5]; 5] = [+ [+ [15588, 17027, 19338, 20218, 20682, 21110, 21825, 23244, 24189, 28165, 29093, 30466, 32768, 0],+ [12016, 18066, 19516, 20303, 20719, 21444, 21888, 23032, 24434, 28658, 30172, 31409, 32768, 0],+ [10052, 10771, 22296, 22788, 23055, 23239, 24133, 25620, 26160, 29336, 29929, 31567, 32768, 0],+ [14091, 15406, 16442, 18808, 19136, 19546, 19998, 22096, 24746, 29585, 30958, 32462, 32768, 0],+ [12122, 13265, 15603, 16501, 18609, 20033, 22391, 25583, 26437, 30261, 31073, 32475, 32768, 0],+ ],+ [+ [10023, 19585, 20848, 21440, 21832, 22760, 23089, 24023, 25381, 29014, 30482, 31436, 32768, 0],+ [5983, 24099, 24560, 24886, 25066, 25795, 25913, 26423, 27610, 29905, 31276, 31794, 32768, 0],+ [7444, 12781, 20177, 20728, 21077, 21607, 22170, 23405, 24469, 27915, 29090, 30492, 32768, 0],+ [8537, 14689, 15432, 17087, 17408, 18172, 18408, 19825, 24649, 29153, 31096, 32210, 32768, 0],+ [7543, 14231, 15496, 16195, 17905, 20717, 21984, 24516, 26001, 29675, 30981, 31994, 32768, 0],+ ],+ [+ [12613, 13591, 21383, 22004, 22312, 22577, 23401, 25055, 25729, 29538, 30305, 32077, 32768, 0],+ [9687, 13470, 18506, 19230, 19604, 20147, 20695, 22062, 23219, 27743, 29211, 30907, 32768, 0],+ [6183, 6505, 26024, 26252, 26366, 26434, 27082, 28354, 28555, 30467, 30794, 32086, 32768, 0],+ [10718, 11734, 14954, 17224, 17565, 17924, 18561, 21523, 23878, 28975, 30287, 32252, 32768, 0],+ [9194, 9858, 16501, 17263, 18424, 19171, 21563, 25961, 26561, 30072, 30737, 32463, 32768, 0],+ ],+ [+ [12602, 14399, 15488, 18381, 18778, 19315, 19724, 21419, 25060, 29696, 30917, 32409, 32768, 0],+ [8203, 13821, 14524, 17105, 17439, 18131, 18404, 19468, 25225, 29485, 31158, 32342, 32768, 0],+ [8451, 9731, 15004, 17643, 18012, 18425, 19070, 21538, 24605, 29118, 30078, 32018, 32768, 0],+ [7714, 9048, 9516, 16667, 16817, 16994, 17153, 18767, 26743, 30389, 31536, 32528, 32768, 0],+ [8843, 10280, 11496, 15317, 16652, 17943, 19108, 22718, 25769, 29953, 30983, 32485, 32768, 0],+ ],+ [+ [12578, 13671, 15979, 16834, 19075, 20913, 22989, 25449, 26219, 30214, 31150, 32477, 32768, 0],+ [9563, 13626, 15080, 15892, 17756, 20863, 22207, 24236, 25380, 29653, 31143, 32277, 32768, 0],+ [8356, 8901, 17616, 18256, 19350, 20106, 22598, 25947, 26466, 29900, 30523, 32261, 32768, 0],+ [10835, 11815, 13124, 16042, 17018, 18039, 18947, 22753, 24615, 29489, 30883, 32482, 32768, 0],+ [7618, 8288, 9859, 10509, 15386, 18657, 22903, 28776, 29180, 31355, 31802, 32593, 32768, 0],+ ],+];++pub static DEFAULT_INTRA_TX_TYPE_SET2_CDF: [[[u16; 6]; 13]; 3] = [+ [+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ ],+ [+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ [6554, 13107, 19661, 26214, 32768, 0],+ ],+ [+ [1127, 12814, 22772, 27483, 32768, 0],+ [145, 6761, 11980, 26667, 32768, 0],+ [362, 5887, 11678, 16725, 32768, 0],+ [385, 15213, 18587, 30693, 32768, 0],+ [25, 2914, 23134, 27903, 32768, 0],+ [60, 4470, 11749, 23991, 32768, 0],+ [37, 3332, 14511, 21448, 32768, 0],+ [157, 6320, 13036, 17439, 32768, 0],+ [119, 6719, 12906, 29396, 32768, 0],+ [47, 5537, 12576, 21499, 32768, 0],+ [269, 6076, 11258, 23115, 32768, 0],+ [83, 5615, 12001, 17228, 32768, 0],+ [1968, 5556, 12023, 18547, 32768, 0],+ ],+];++pub static DEFAULT_IS_INTER_CDF: [[u16; 3]; 4] = [[806, 32768, 0], [16662, 32768, 0], [20186, 32768, 0], [26538, 32768, 0]];++pub static DEFAULT_MV_BIT_CDF: [[u16; 3]; 10] = [+ [17408, 32768, 0],+ [17920, 32768, 0],+ [18944, 32768, 0],+ [20480, 32768, 0],+ [22528, 32768, 0],+ [24576, 32768, 0],+ [28672, 32768, 0],+ [29952, 32768, 0],+ [29952, 32768, 0],+ [30720, 32768, 0],+];++pub static DEFAULT_MV_CLASS0_BIT_CDF: [u16; 3] = [27648, 32768, 0];++pub static DEFAULT_MV_CLASS0_FR_CDF: [[[u16; 5]; 2]; 2] =+ [[[16384, 24576, 26624, 32768, 0], [12288, 21248, 24128, 32768, 0]], [[16384, 24576, 26624, 32768, 0], [12288, 21248, 24128, 32768, 0]]];++pub static DEFAULT_MV_CLASS0_HP_CDF: [u16; 3] = [20480, 32768, 0];++pub static DEFAULT_MV_CLASS_CDF: [[u16; 12]; 2] = [+ [28672, 30976, 31858, 32320, 32551, 32656, 32740, 32757, 32762, 32767, 32768, 0],+ [28672, 30976, 31858, 32320, 32551, 32656, 32740, 32757, 32762, 32767, 32768, 0],+];++pub static DEFAULT_MV_FR_CDF: [[u16; 5]; 2] = [[8192, 17408, 21248, 32768, 0], [8192, 17408, 21248, 32768, 0]];++pub static DEFAULT_MV_HP_CDF: [u16; 3] = [16384, 32768, 0];++pub static DEFAULT_MV_JOINT_CDF: [u16; 5] = [4096, 11264, 19328, 32768, 0];++pub static DEFAULT_MV_SIGN_CDF: [u16; 3] = [16384, 32768, 0];++pub static DEFAULT_NEW_MV_CDF: [[u16; 3]; 6] = [[24035, 32768, 0], [16630, 32768, 0], [15339, 32768, 0], [8386, 32768, 0], [12222, 32768, 0], [4676, 32768, 0]];++pub static DEFAULT_PARTITION_W16_CDF: [[u16; 11]; 4] = [+ [15597, 20929, 24571, 26706, 27664, 28821, 29601, 30571, 31902, 32768, 0],+ [7925, 11043, 16785, 22470, 23971, 25043, 26651, 28701, 29834, 32768, 0],+ [5414, 13269, 15111, 20488, 22360, 24500, 25537, 26336, 32117, 32768, 0],+ [2662, 6362, 8614, 20860, 23053, 24778, 26436, 27829, 31171, 32768, 0],+];++pub static DEFAULT_PARTITION_W32_CDF: [[u16; 11]; 4] = [+ [18462, 20920, 23124, 27647, 28227, 29049, 29519, 30178, 31544, 32768, 0],+ [7689, 9060, 12056, 24992, 25660, 26182, 26951, 28041, 29052, 32768, 0],+ [6015, 9009, 10062, 24544, 25409, 26545, 27071, 27526, 32047, 32768, 0],+ [1394, 2208, 2796, 28614, 29061, 29466, 29840, 30185, 31899, 32768, 0],+];++pub static DEFAULT_PARTITION_W64_CDF: [[u16; 11]; 4] = [+ [20137, 21547, 23078, 29566, 29837, 30261, 30524, 30892, 31724, 32768, 0],+ [6732, 7490, 9497, 27944, 28250, 28515, 28969, 29630, 30104, 32768, 0],+ [5945, 7663, 8348, 28683, 29117, 29749, 30064, 30298, 32238, 32768, 0],+ [870, 1212, 1487, 31198, 31394, 31574, 31743, 31881, 32332, 32768, 0],+];++pub static DEFAULT_PARTITION_W8_CDF: [[u16; 5]; 4] =+ [[19132, 25510, 30392, 32768, 0], [13928, 19855, 28540, 32768, 0], [12522, 23679, 28629, 32768, 0], [9896, 18783, 25853, 32768, 0]];++pub static DEFAULT_REF_MV_CDF: [[u16; 3]; 6] =+ [[23974, 32768, 0], [24188, 32768, 0], [17848, 32768, 0], [28622, 32768, 0], [24312, 32768, 0], [19923, 32768, 0]];++pub static DEFAULT_SCAN_16X16: [u16; 256] = [+ 0, 1, 16, 32, 17, 2, 3, 18, 33, 48, 64, 49, 34, 19, 4, 5, 20, 35, 50, 65, 80, 96, 81, 66, 51, 36, 21, 6, 7, 22, 37, 52, 67, 82, 97, 112, 128, 113, 98, 83,+ 68, 53, 38, 23, 8, 9, 24, 39, 54, 69, 84, 99, 114, 129, 144, 160, 145, 130, 115, 100, 85, 70, 55, 40, 25, 10, 11, 26, 41, 56, 71, 86, 101, 116, 131, 146,+ 161, 176, 192, 177, 162, 147, 132, 117, 102, 87, 72, 57, 42, 27, 12, 13, 28, 43, 58, 73, 88, 103, 118, 133, 148, 163, 178, 193, 208, 224, 209, 194, 179,+ 164, 149, 134, 119, 104, 89, 74, 59, 44, 29, 14, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 241, 226, 211, 196, 181, 166,+ 151, 136, 121, 106, 91, 76, 61, 46, 31, 47, 62, 77, 92, 107, 122, 137, 152, 167, 182, 197, 212, 227, 242, 243, 228, 213, 198, 183, 168, 153, 138, 123, 108,+ 93, 78, 63, 79, 94, 109, 124, 139, 154, 169, 184, 199, 214, 229, 244, 245, 230, 215, 200, 185, 170, 155, 140, 125, 110, 95, 111, 126, 141, 156, 171, 186,+ 201, 216, 231, 246, 247, 232, 217, 202, 187, 172, 157, 142, 127, 143, 158, 173, 188, 203, 218, 233, 248, 249, 234, 219, 204, 189, 174, 159, 175, 190, 205,+ 220, 235, 250, 251, 236, 221, 206, 191, 207, 222, 237, 252, 253, 238, 223, 239, 254, 255,+];++pub static DEFAULT_SCAN_32X32: [u16; 1024] = [+ 0, 1, 32, 64, 33, 2, 3, 34, 65, 96, 128, 97, 66, 35, 4, 5, 36, 67, 98, 129, 160, 192, 161, 130, 99, 68, 37, 6, 7, 38, 69, 100, 131, 162, 193, 224, 256,+ 225, 194, 163, 132, 101, 70, 39, 8, 9, 40, 71, 102, 133, 164, 195, 226, 257, 288, 320, 289, 258, 227, 196, 165, 134, 103, 72, 41, 10, 11, 42, 73, 104, 135,+ 166, 197, 228, 259, 290, 321, 352, 384, 353, 322, 291, 260, 229, 198, 167, 136, 105, 74, 43, 12, 13, 44, 75, 106, 137, 168, 199, 230, 261, 292, 323, 354,+ 385, 416, 448, 417, 386, 355, 324, 293, 262, 231, 200, 169, 138, 107, 76, 45, 14, 15, 46, 77, 108, 139, 170, 201, 232, 263, 294, 325, 356, 387, 418, 449,+ 480, 512, 481, 450, 419, 388, 357, 326, 295, 264, 233, 202, 171, 140, 109, 78, 47, 16, 17, 48, 79, 110, 141, 172, 203, 234, 265, 296, 327, 358, 389, 420,+ 451, 482, 513, 544, 576, 545, 514, 483, 452, 421, 390, 359, 328, 297, 266, 235, 204, 173, 142, 111, 80, 49, 18, 19, 50, 81, 112, 143, 174, 205, 236, 267,+ 298, 329, 360, 391, 422, 453, 484, 515, 546, 577, 608, 640, 609, 578, 547, 516, 485, 454, 423, 392, 361, 330, 299, 268, 237, 206, 175, 144, 113, 82, 51,+ 20, 21, 52, 83, 114, 145, 176, 207, 238, 269, 300, 331, 362, 393, 424, 455, 486, 517, 548, 579, 610, 641, 672, 704, 673, 642, 611, 580, 549, 518, 487, 456,+ 425, 394, 363, 332, 301, 270, 239, 208, 177, 146, 115, 84, 53, 22, 23, 54, 85, 116, 147, 178, 209, 240, 271, 302, 333, 364, 395, 426, 457, 488, 519, 550,+ 581, 612, 643, 674, 705, 736, 768, 737, 706, 675, 644, 613, 582, 551, 520, 489, 458, 427, 396, 365, 334, 303, 272, 241, 210, 179, 148, 117, 86, 55, 24, 25,+ 56, 87, 118, 149, 180, 211, 242, 273, 304, 335, 366, 397, 428, 459, 490, 521, 552, 583, 614, 645, 676, 707, 738, 769, 800, 832, 801, 770, 739, 708, 677,+ 646, 615, 584, 553, 522, 491, 460, 429, 398, 367, 336, 305, 274, 243, 212, 181, 150, 119, 88, 57, 26, 27, 58, 89, 120, 151, 182, 213, 244, 275, 306, 337,+ 368, 399, 430, 461, 492, 523, 554, 585, 616, 647, 678, 709, 740, 771, 802, 833, 864, 896, 865, 834, 803, 772, 741, 710, 679, 648, 617, 586, 555, 524, 493,+ 462, 431, 400, 369, 338, 307, 276, 245, 214, 183, 152, 121, 90, 59, 28, 29, 60, 91, 122, 153, 184, 215, 246, 277, 308, 339, 370, 401, 432, 463, 494, 525,+ 556, 587, 618, 649, 680, 711, 742, 773, 804, 835, 866, 897, 928, 960, 929, 898, 867, 836, 805, 774, 743, 712, 681, 650, 619, 588, 557, 526, 495, 464, 433,+ 402, 371, 340, 309, 278, 247, 216, 185, 154, 123, 92, 61, 30, 31, 62, 93, 124, 155, 186, 217, 248, 279, 310, 341, 372, 403, 434, 465, 496, 527, 558, 589,+ 620, 651, 682, 713, 744, 775, 806, 837, 868, 899, 930, 961, 992, 993, 962, 931, 900, 869, 838, 807, 776, 745, 714, 683, 652, 621, 590, 559, 528, 497, 466,+ 435, 404, 373, 342, 311, 280, 249, 218, 187, 156, 125, 94, 63, 95, 126, 157, 188, 219, 250, 281, 312, 343, 374, 405, 436, 467, 498, 529, 560, 591, 622,+ 653, 684, 715, 746, 777, 808, 839, 870, 901, 932, 963, 994, 995, 964, 933, 902, 871, 840, 809, 778, 747, 716, 685, 654, 623, 592, 561, 530, 499, 468, 437,+ 406, 375, 344, 313, 282, 251, 220, 189, 158, 127, 159, 190, 221, 252, 283, 314, 345, 376, 407, 438, 469, 500, 531, 562, 593, 624, 655, 686, 717, 748, 779,+ 810, 841, 872, 903, 934, 965, 996, 997, 966, 935, 904, 873, 842, 811, 780, 749, 718, 687, 656, 625, 594, 563, 532, 501, 470, 439, 408, 377, 346, 315, 284,+ 253, 222, 191, 223, 254, 285, 316, 347, 378, 409, 440, 471, 502, 533, 564, 595, 626, 657, 688, 719, 750, 781, 812, 843, 874, 905, 936, 967, 998, 999, 968,+ 937, 906, 875, 844, 813, 782, 751, 720, 689, 658, 627, 596, 565, 534, 503, 472, 441, 410, 379, 348, 317, 286, 255, 287, 318, 349, 380, 411, 442, 473, 504,+ 535, 566, 597, 628, 659, 690, 721, 752, 783, 814, 845, 876, 907, 938, 969, 1000, 1001, 970, 939, 908, 877, 846, 815, 784, 753, 722, 691, 660, 629, 598,+ 567, 536, 505, 474, 443, 412, 381, 350, 319, 351, 382, 413, 444, 475, 506, 537, 568, 599, 630, 661, 692, 723, 754, 785, 816, 847, 878, 909, 940, 971, 1002,+ 1003, 972, 941, 910, 879, 848, 817, 786, 755, 724, 693, 662, 631, 600, 569, 538, 507, 476, 445, 414, 383, 415, 446, 477, 508, 539, 570, 601, 632, 663, 694,+ 725, 756, 787, 818, 849, 880, 911, 942, 973, 1004, 1005, 974, 943, 912, 881, 850, 819, 788, 757, 726, 695, 664, 633, 602, 571, 540, 509, 478, 447, 479,+ 510, 541, 572, 603, 634, 665, 696, 727, 758, 789, 820, 851, 882, 913, 944, 975, 1006, 1007, 976, 945, 914, 883, 852, 821, 790, 759, 728, 697, 666, 635,+ 604, 573, 542, 511, 543, 574, 605, 636, 667, 698, 729, 760, 791, 822, 853, 884, 915, 946, 977, 1008, 1009, 978, 947, 916, 885, 854, 823, 792, 761, 730,+ 699, 668, 637, 606, 575, 607, 638, 669, 700, 731, 762, 793, 824, 855, 886, 917, 948, 979, 1010, 1011, 980, 949, 918, 887, 856, 825, 794, 763, 732, 701,+ 670, 639, 671, 702, 733, 764, 795, 826, 857, 888, 919, 950, 981, 1012, 1013, 982, 951, 920, 889, 858, 827, 796, 765, 734, 703, 735, 766, 797, 828, 859,+ 890, 921, 952, 983, 1014, 1015, 984, 953, 922, 891, 860, 829, 798, 767, 799, 830, 861, 892, 923, 954, 985, 1016, 1017, 986, 955, 924, 893, 862, 831, 863,+ 894, 925, 956, 987, 1018, 1019, 988, 957, 926, 895, 927, 958, 989, 1020, 1021, 990, 959, 991, 1022, 1023,+];++pub static DEFAULT_SCAN_4X4: [u16; 16] = [0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15];++pub static DEFAULT_SCAN_8X8: [u16; 64] = [+ 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29,+ 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,+];++pub static DEFAULT_SINGLE_REF_CDF: [[[u16; 3]; 6]; 3] = [+ [[4897, 32768, 0], [1555, 32768, 0], [4236, 32768, 0], [8650, 32768, 0], [904, 32768, 0], [1444, 32768, 0]],+ [[16973, 32768, 0], [16751, 32768, 0], [19647, 32768, 0], [24773, 32768, 0], [11014, 32768, 0], [15087, 32768, 0]],+ [[29744, 32768, 0], [30279, 32768, 0], [31194, 32768, 0], [31895, 32768, 0], [26875, 32768, 0], [30304, 32768, 0]],+];++pub static DEFAULT_SKIP_CDF: [[u16; 3]; 3] = [[31671, 32768, 0], [16515, 32768, 0], [4576, 32768, 0]];++pub static DEFAULT_TXB_SKIP_CDF: [[[[u16; 3]; 13]; 5]; 4] = [+ [+ [+ [31849, 32768, 0],+ [5892, 32768, 0],+ [12112, 32768, 0],+ [21935, 32768, 0],+ [20289, 32768, 0],+ [27473, 32768, 0],+ [32487, 32768, 0],+ [7654, 32768, 0],+ [19473, 32768, 0],+ [29984, 32768, 0],+ [9961, 32768, 0],+ [30242, 32768, 0],+ [32117, 32768, 0],+ ],+ [+ [31548, 32768, 0],+ [1549, 32768, 0],+ [10130, 32768, 0],+ [16656, 32768, 0],+ [18591, 32768, 0],+ [26308, 32768, 0],+ [32537, 32768, 0],+ [5403, 32768, 0],+ [18096, 32768, 0],+ [30003, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [29957, 32768, 0],+ [5391, 32768, 0],+ [18039, 32768, 0],+ [23566, 32768, 0],+ [22431, 32768, 0],+ [25822, 32768, 0],+ [32197, 32768, 0],+ [3778, 32768, 0],+ [15336, 32768, 0],+ [28981, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [17920, 32768, 0],+ [1818, 32768, 0],+ [7282, 32768, 0],+ [25273, 32768, 0],+ [10923, 32768, 0],+ [31554, 32768, 0],+ [32624, 32768, 0],+ [1366, 32768, 0],+ [15628, 32768, 0],+ [30462, 32768, 0],+ [146, 32768, 0],+ [5132, 32768, 0],+ [31657, 32768, 0],+ ],+ [+ [6308, 32768, 0],+ [117, 32768, 0],+ [1638, 32768, 0],+ [2161, 32768, 0],+ [16384, 32768, 0],+ [10923, 32768, 0],+ [30247, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ [+ [+ [30371, 32768, 0],+ [7570, 32768, 0],+ [13155, 32768, 0],+ [20751, 32768, 0],+ [20969, 32768, 0],+ [27067, 32768, 0],+ [32013, 32768, 0],+ [5495, 32768, 0],+ [17942, 32768, 0],+ [28280, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [31782, 32768, 0],+ [1836, 32768, 0],+ [10689, 32768, 0],+ [17604, 32768, 0],+ [21622, 32768, 0],+ [27518, 32768, 0],+ [32399, 32768, 0],+ [4419, 32768, 0],+ [16294, 32768, 0],+ [28345, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [31901, 32768, 0],+ [10311, 32768, 0],+ [18047, 32768, 0],+ [24806, 32768, 0],+ [23288, 32768, 0],+ [27914, 32768, 0],+ [32296, 32768, 0],+ [4215, 32768, 0],+ [15756, 32768, 0],+ [28341, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [26726, 32768, 0],+ [1045, 32768, 0],+ [11703, 32768, 0],+ [20590, 32768, 0],+ [18554, 32768, 0],+ [25970, 32768, 0],+ [31938, 32768, 0],+ [5583, 32768, 0],+ [21313, 32768, 0],+ [29390, 32768, 0],+ [641, 32768, 0],+ [22265, 32768, 0],+ [31452, 32768, 0],+ ],+ [+ [26584, 32768, 0],+ [188, 32768, 0],+ [8847, 32768, 0],+ [24519, 32768, 0],+ [22938, 32768, 0],+ [30583, 32768, 0],+ [32608, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ [+ [+ [29614, 32768, 0],+ [9068, 32768, 0],+ [12924, 32768, 0],+ [19538, 32768, 0],+ [17737, 32768, 0],+ [24619, 32768, 0],+ [30642, 32768, 0],+ [4119, 32768, 0],+ [16026, 32768, 0],+ [25657, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [31957, 32768, 0],+ [3230, 32768, 0],+ [11153, 32768, 0],+ [18123, 32768, 0],+ [20143, 32768, 0],+ [26536, 32768, 0],+ [31986, 32768, 0],+ [3050, 32768, 0],+ [14603, 32768, 0],+ [25155, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [32363, 32768, 0],+ [10692, 32768, 0],+ [19090, 32768, 0],+ [24357, 32768, 0],+ [24442, 32768, 0],+ [28312, 32768, 0],+ [32169, 32768, 0],+ [3648, 32768, 0],+ [15690, 32768, 0],+ [26815, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [30669, 32768, 0],+ [3832, 32768, 0],+ [11663, 32768, 0],+ [18889, 32768, 0],+ [19782, 32768, 0],+ [23313, 32768, 0],+ [31330, 32768, 0],+ [5124, 32768, 0],+ [18719, 32768, 0],+ [28468, 32768, 0],+ [3082, 32768, 0],+ [20982, 32768, 0],+ [29443, 32768, 0],+ ],+ [+ [28573, 32768, 0],+ [3183, 32768, 0],+ [17802, 32768, 0],+ [25977, 32768, 0],+ [26677, 32768, 0],+ [27832, 32768, 0],+ [32387, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+ [+ [+ [26887, 32768, 0],+ [6729, 32768, 0],+ [10361, 32768, 0],+ [17442, 32768, 0],+ [15045, 32768, 0],+ [22478, 32768, 0],+ [29072, 32768, 0],+ [2713, 32768, 0],+ [11861, 32768, 0],+ [20773, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [31903, 32768, 0],+ [2044, 32768, 0],+ [7528, 32768, 0],+ [14618, 32768, 0],+ [16182, 32768, 0],+ [24168, 32768, 0],+ [31037, 32768, 0],+ [2786, 32768, 0],+ [11194, 32768, 0],+ [20155, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [32510, 32768, 0],+ [8430, 32768, 0],+ [17318, 32768, 0],+ [24154, 32768, 0],+ [23674, 32768, 0],+ [28789, 32768, 0],+ [32139, 32768, 0],+ [3440, 32768, 0],+ [13117, 32768, 0],+ [22702, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ [+ [31671, 32768, 0],+ [2056, 32768, 0],+ [11746, 32768, 0],+ [16852, 32768, 0],+ [18635, 32768, 0],+ [24715, 32768, 0],+ [31484, 32768, 0],+ [4656, 32768, 0],+ [16074, 32768, 0],+ [24704, 32768, 0],+ [1806, 32768, 0],+ [14645, 32768, 0],+ [25336, 32768, 0],+ ],+ [+ [31539, 32768, 0],+ [8433, 32768, 0],+ [20576, 32768, 0],+ [27904, 32768, 0],+ [27852, 32768, 0],+ [30026, 32768, 0],+ [32441, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ [16384, 32768, 0],+ ],+ ],+];++pub static DEFAULT_UV_MODE_CFL_ALLOWED_CDF: [[u16; 15]; 13] = [+ [10407, 11208, 12900, 13181, 13823, 14175, 14899, 15656, 15986, 20086, 20995, 22455, 24212, 32768, 0],+ [4532, 19780, 20057, 20215, 20428, 21071, 21199, 21451, 22099, 24228, 24693, 27032, 29472, 32768, 0],+ [5273, 5379, 20177, 20270, 20385, 20439, 20949, 21695, 21774, 23138, 24256, 24703, 26679, 32768, 0],+ [6740, 7167, 7662, 14152, 14536, 14785, 15034, 16741, 18371, 21520, 22206, 23389, 24182, 32768, 0],+ [4987, 5368, 5928, 6068, 19114, 20315, 21857, 22253, 22411, 24911, 25380, 26027, 26376, 32768, 0],+ [5370, 6889, 7247, 7393, 9498, 21114, 21402, 21753, 21981, 24780, 25386, 26517, 27176, 32768, 0],+ [4816, 4961, 7204, 7326, 8765, 8930, 20169, 20682, 20803, 23188, 23763, 24455, 24940, 32768, 0],+ [6608, 6740, 8529, 9049, 9257, 9356, 9735, 18827, 19059, 22336, 23204, 23964, 24793, 32768, 0],+ [5998, 7419, 7781, 8933, 9255, 9549, 9753, 10417, 18898, 22494, 23139, 24764, 25989, 32768, 0],+ [10660, 11298, 12550, 12957, 13322, 13624, 14040, 15004, 15534, 20714, 21789, 23443, 24861, 32768, 0],+ [10522, 11530, 12552, 12963, 13378, 13779, 14245, 15235, 15902, 20102, 22696, 23774, 25838, 32768, 0],+ [10099, 10691, 12639, 13049, 13386, 13665, 14125, 15163, 15636, 19676, 20474, 23519, 25208, 32768, 0],+ [3144, 5087, 7382, 7504, 7593, 7690, 7801, 8064, 8232, 9248, 9875, 10521, 29048, 32768, 0],+];++pub static DEFAULT_UV_MODE_CFL_NOT_ALLOWED_CDF: [[u16; 14]; 13] = [+ [22631, 24152, 25378, 25661, 25986, 26520, 27055, 27923, 28244, 30059, 30941, 31961, 32768, 0],+ [9513, 26881, 26973, 27046, 27118, 27664, 27739, 27824, 28359, 29505, 29800, 31796, 32768, 0],+ [9845, 9915, 28663, 28704, 28757, 28780, 29198, 29822, 29854, 30764, 31777, 32029, 32768, 0],+ [13639, 13897, 14171, 25331, 25606, 25727, 25953, 27148, 28577, 30612, 31355, 32493, 32768, 0],+ [9764, 9835, 9930, 9954, 25386, 27053, 27958, 28148, 28243, 31101, 31744, 32363, 32768, 0],+ [11825, 13589, 13677, 13720, 15048, 29213, 29301, 29458, 29711, 31161, 31441, 32550, 32768, 0],+ [14175, 14399, 16608, 16821, 17718, 17775, 28551, 30200, 30245, 31837, 32342, 32667, 32768, 0],+ [12885, 13038, 14978, 15590, 15673, 15748, 16176, 29128, 29267, 30643, 31961, 32461, 32768, 0],+ [12026, 13661, 13874, 15305, 15490, 15726, 15995, 16273, 28443, 30388, 30767, 32416, 32768, 0],+ [19052, 19840, 20579, 20916, 21150, 21467, 21885, 22719, 23174, 28861, 30379, 32175, 32768, 0],+ [18627, 19649, 20974, 21219, 21492, 21816, 22199, 23119, 23527, 27053, 31397, 32148, 32768, 0],+ [17026, 19004, 19997, 20339, 20586, 21103, 21349, 21907, 22482, 25896, 26541, 31819, 32768, 0],+ [12124, 13759, 14959, 14992, 15007, 15051, 15078, 15166, 15255, 15753, 16039, 16606, 32768, 0],+];++pub static DEFAULT_Y_MODE_CDF: [[u16; 14]; 4] = [+ [22801, 23489, 24293, 24756, 25601, 26123, 26606, 27418, 27945, 29228, 29685, 30349, 32768, 0],+ [18673, 19845, 22631, 23318, 23950, 24649, 25527, 27364, 28152, 29701, 29984, 30852, 32768, 0],+ [19770, 20979, 23396, 23939, 24241, 24654, 25136, 27073, 27830, 29360, 29730, 30659, 32768, 0],+ [20155, 21301, 22838, 23178, 23261, 23533, 23703, 24804, 25352, 26575, 27016, 28049, 32768, 0],+];++pub static DEFAULT_ZERO_MV_CDF: [[u16; 3]; 2] = [[2175, 32768, 0], [1054, 32768, 0]];++pub static INTRA_MODE_CONTEXT: [u8; 13] = [0, 1, 2, 3, 4, 4, 4, 4, 3, 0, 1, 2, 0];++pub static MAG_REF_OFFSET_WITH_TX_CLASS: [[[u8; 2]; 3]; 3] = [[[0, 1], [1, 0], [1, 1]], [[0, 1], [1, 0], [0, 2]], [[0, 1], [1, 0], [2, 0]]];++pub static MAX_TX_SIZE_RECT: [u8; 22] = [0, 5, 6, 1, 7, 8, 2, 9, 10, 3, 11, 12, 4, 4, 4, 4, 13, 14, 15, 16, 17, 18];++pub static MI_HEIGHT_LOG2: [u8; 22] = [0, 1, 0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 2, 0, 3, 1, 4, 2];++pub static MI_WIDTH_LOG2: [u8; 22] = [0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 0, 2, 1, 3, 2, 4];++pub static MODE_TO_TXFM: [u8; 14] = [0, 1, 2, 0, 3, 1, 2, 2, 1, 3, 1, 2, 3, 0];++pub static NUM_4X4_BLOCKS_HIGH: [u8; 22] = [1, 2, 1, 2, 4, 2, 4, 8, 4, 8, 16, 8, 16, 32, 16, 32, 4, 1, 8, 2, 16, 4];++pub static NUM_4X4_BLOCKS_WIDE: [u8; 22] = [1, 1, 2, 2, 2, 4, 4, 4, 8, 8, 8, 16, 16, 16, 32, 32, 1, 4, 2, 8, 4, 16];++pub static PARTITION_SUBSIZE: [[u8; 22]; 10] = [+ [0, 22, 22, 3, 22, 22, 6, 22, 22, 9, 22, 22, 12, 22, 22, 15, 22, 22, 22, 22, 22, 22],+ [22, 22, 22, 2, 22, 22, 5, 22, 22, 8, 22, 22, 11, 22, 22, 14, 22, 22, 22, 22, 22, 22],+ [22, 22, 22, 1, 22, 22, 4, 22, 22, 7, 22, 22, 10, 22, 22, 13, 22, 22, 22, 22, 22, 22],+ [22, 22, 22, 0, 22, 22, 3, 22, 22, 6, 22, 22, 9, 22, 22, 12, 22, 22, 22, 22, 22, 22],+ [22, 22, 22, 2, 22, 22, 5, 22, 22, 8, 22, 22, 11, 22, 22, 14, 22, 22, 22, 22, 22, 22],+ [22, 22, 22, 2, 22, 22, 5, 22, 22, 8, 22, 22, 11, 22, 22, 14, 22, 22, 22, 22, 22, 22],+ [22, 22, 22, 1, 22, 22, 4, 22, 22, 7, 22, 22, 10, 22, 22, 13, 22, 22, 22, 22, 22, 22],+ [22, 22, 22, 1, 22, 22, 4, 22, 22, 7, 22, 22, 10, 22, 22, 13, 22, 22, 22, 22, 22, 22],+ [22, 22, 22, 22, 22, 22, 17, 22, 22, 19, 22, 22, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22],+ [22, 22, 22, 22, 22, 22, 16, 22, 22, 18, 22, 22, 20, 22, 22, 22, 22, 22, 22, 22, 22, 22],+];++pub static SIG_REF_DIFF_OFFSET: [[[u8; 2]; 5]; 3] =+ [[[0, 1], [1, 0], [1, 1], [0, 2], [2, 0]], [[0, 1], [1, 0], [0, 2], [0, 3], [0, 4]], [[0, 1], [1, 0], [2, 0], [3, 0], [4, 0]]];++pub static SIZE_GROUP: [u8; 22] = [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 0, 0, 1, 1, 2, 2];++pub static SM_WEIGHTS_TX_16X16: [u8; 16] = [255, 225, 196, 170, 145, 123, 102, 84, 68, 54, 43, 33, 26, 20, 17, 16];++pub static SM_WEIGHTS_TX_32X32: [u8; 32] =+ [255, 240, 225, 210, 196, 182, 169, 157, 145, 133, 122, 111, 101, 92, 83, 74, 66, 59, 52, 45, 39, 34, 29, 25, 21, 17, 14, 12, 10, 9, 8, 8];++pub static SM_WEIGHTS_TX_4X4: [u8; 4] = [255, 149, 85, 64];++pub static SM_WEIGHTS_TX_8X8: [u8; 8] = [255, 197, 146, 105, 73, 50, 37, 32];++pub static SUBPEL_FILTERS: [[[i16; 8]; 16]; 6] = [+ [+ [0, 0, 0, 128, 0, 0, 0, 0],+ [0, 2, -6, 126, 8, -2, 0, 0],+ [0, 2, -10, 122, 18, -4, 0, 0],+ [0, 2, -12, 116, 28, -8, 2, 0],+ [0, 2, -14, 110, 38, -10, 2, 0],+ [0, 2, -14, 102, 48, -12, 2, 0],+ [0, 2, -16, 94, 58, -12, 2, 0],+ [0, 2, -14, 84, 66, -12, 2, 0],+ [0, 2, -14, 76, 76, -14, 2, 0],+ [0, 2, -12, 66, 84, -14, 2, 0],+ [0, 2, -12, 58, 94, -16, 2, 0],+ [0, 2, -12, 48, 102, -14, 2, 0],+ [0, 2, -10, 38, 110, -14, 2, 0],+ [0, 2, -8, 28, 116, -12, 2, 0],+ [0, 0, -4, 18, 122, -10, 2, 0],+ [0, 0, -2, 8, 126, -6, 2, 0],+ ],+ [+ [0, 0, 0, 128, 0, 0, 0, 0],+ [0, 2, 28, 62, 34, 2, 0, 0],+ [0, 0, 26, 62, 36, 4, 0, 0],+ [0, 0, 22, 62, 40, 4, 0, 0],+ [0, 0, 20, 60, 42, 6, 0, 0],+ [0, 0, 18, 58, 44, 8, 0, 0],+ [0, 0, 16, 56, 46, 10, 0, 0],+ [0, -2, 16, 54, 48, 12, 0, 0],+ [0, -2, 14, 52, 52, 14, -2, 0],+ [0, 0, 12, 48, 54, 16, -2, 0],+ [0, 0, 10, 46, 56, 16, 0, 0],+ [0, 0, 8, 44, 58, 18, 0, 0],+ [0, 0, 6, 42, 60, 20, 0, 0],+ [0, 0, 4, 40, 62, 22, 0, 0],+ [0, 0, 4, 36, 62, 26, 0, 0],+ [0, 0, 2, 34, 62, 28, 2, 0],+ ],+ [+ [0, 0, 0, 128, 0, 0, 0, 0],+ [-2, 2, -6, 126, 8, -2, 2, 0],+ [-2, 6, -12, 124, 16, -6, 4, -2],+ [-2, 8, -18, 120, 26, -10, 6, -2],+ [-4, 10, -22, 116, 38, -14, 6, -2],+ [-4, 10, -22, 108, 48, -18, 8, -2],+ [-4, 10, -24, 100, 60, -20, 8, -2],+ [-4, 10, -24, 90, 70, -22, 10, -2],+ [-4, 12, -24, 80, 80, -24, 12, -4],+ [-2, 10, -22, 70, 90, -24, 10, -4],+ [-2, 8, -20, 60, 100, -24, 10, -4],+ [-2, 8, -18, 48, 108, -22, 10, -4],+ [-2, 6, -14, 38, 116, -22, 10, -4],+ [-2, 6, -10, 26, 120, -18, 8, -2],+ [-2, 4, -6, 16, 124, -12, 6, -2],+ [0, 2, -2, 8, 126, -6, 2, -2],+ ],+ [+ [0, 0, 0, 128, 0, 0, 0, 0],+ [0, 0, 0, 120, 8, 0, 0, 0],+ [0, 0, 0, 112, 16, 0, 0, 0],+ [0, 0, 0, 104, 24, 0, 0, 0],+ [0, 0, 0, 96, 32, 0, 0, 0],+ [0, 0, 0, 88, 40, 0, 0, 0],+ [0, 0, 0, 80, 48, 0, 0, 0],+ [0, 0, 0, 72, 56, 0, 0, 0],+ [0, 0, 0, 64, 64, 0, 0, 0],+ [0, 0, 0, 56, 72, 0, 0, 0],+ [0, 0, 0, 48, 80, 0, 0, 0],+ [0, 0, 0, 40, 88, 0, 0, 0],+ [0, 0, 0, 32, 96, 0, 0, 0],+ [0, 0, 0, 24, 104, 0, 0, 0],+ [0, 0, 0, 16, 112, 0, 0, 0],+ [0, 0, 0, 8, 120, 0, 0, 0],+ ],+ [+ [0, 0, 0, 128, 0, 0, 0, 0],+ [0, 0, -4, 126, 8, -2, 0, 0],+ [0, 0, -8, 122, 18, -4, 0, 0],+ [0, 0, -10, 116, 28, -6, 0, 0],+ [0, 0, -12, 110, 38, -8, 0, 0],+ [0, 0, -12, 102, 48, -10, 0, 0],+ [0, 0, -14, 94, 58, -10, 0, 0],+ [0, 0, -12, 84, 66, -10, 0, 0],+ [0, 0, -12, 76, 76, -12, 0, 0],+ [0, 0, -10, 66, 84, -12, 0, 0],+ [0, 0, -10, 58, 94, -14, 0, 0],+ [0, 0, -10, 48, 102, -12, 0, 0],+ [0, 0, -8, 38, 110, -12, 0, 0],+ [0, 0, -6, 28, 116, -10, 0, 0],+ [0, 0, -4, 18, 122, -8, 0, 0],+ [0, 0, -2, 8, 126, -4, 0, 0],+ ],+ [+ [0, 0, 0, 128, 0, 0, 0, 0],+ [0, 0, 30, 62, 34, 2, 0, 0],+ [0, 0, 26, 62, 36, 4, 0, 0],+ [0, 0, 22, 62, 40, 4, 0, 0],+ [0, 0, 20, 60, 42, 6, 0, 0],+ [0, 0, 18, 58, 44, 8, 0, 0],+ [0, 0, 16, 56, 46, 10, 0, 0],+ [0, 0, 14, 54, 48, 12, 0, 0],+ [0, 0, 12, 52, 52, 12, 0, 0],+ [0, 0, 12, 48, 54, 14, 0, 0],+ [0, 0, 10, 46, 56, 16, 0, 0],+ [0, 0, 8, 44, 58, 18, 0, 0],+ [0, 0, 6, 42, 60, 20, 0, 0],+ [0, 0, 4, 40, 62, 22, 0, 0],+ [0, 0, 4, 36, 62, 26, 0, 0],+ [0, 0, 2, 34, 62, 30, 0, 0],+ ],+];++pub static SUBSAMPLED_SIZE: [[[u8; 2]; 2]; 22] = [+ [[0, 0], [0, 0]],+ [[1, 0], [22, 0]],+ [[2, 22], [0, 0]],+ [[3, 2], [1, 0]],+ [[4, 3], [22, 1]],+ [[5, 22], [3, 2]],+ [[6, 5], [4, 3]],+ [[7, 6], [22, 4]],+ [[8, 22], [6, 5]],+ [[9, 8], [7, 6]],+ [[10, 9], [22, 7]],+ [[11, 22], [9, 8]],+ [[12, 11], [10, 9]],+ [[13, 12], [22, 10]],+ [[14, 22], [12, 11]],+ [[15, 14], [13, 12]],+ [[16, 1], [22, 1]],+ [[17, 22], [2, 2]],+ [[18, 4], [22, 16]],+ [[19, 22], [5, 17]],+ [[20, 7], [22, 18]],+ [[21, 22], [8, 19]],+];++pub static TRANSFORM_ROW_SHIFT: [u8; 19] = [0, 1, 2, 2, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2];++pub static TX_HEIGHT: [u8; 19] = [4, 8, 16, 32, 64, 8, 4, 16, 8, 32, 16, 64, 32, 16, 4, 32, 8, 64, 16];++pub static TX_HEIGHT_LOG2: [u8; 19] = [2, 3, 4, 5, 6, 3, 2, 4, 3, 5, 4, 6, 5, 4, 2, 5, 3, 6, 4];++pub static TX_SIZE_SQR: [u8; 19] = [0, 1, 2, 3, 4, 0, 0, 1, 1, 2, 2, 3, 3, 0, 0, 1, 1, 2, 2];++pub static TX_SIZE_SQR_UP: [u8; 19] = [0, 1, 2, 3, 4, 1, 1, 2, 2, 3, 3, 4, 4, 2, 2, 3, 3, 4, 4];++pub static TX_TYPE_IN_SET_INTER: [[u8; 16]; 4] = [+ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],+ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],+ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],+ [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],+];++pub static TX_TYPE_IN_SET_INTRA: [[u8; 16]; 3] =+ [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]];++pub static TX_TYPE_INTER_INV_SET3: [u8; 2] = [9, 0];++pub static TX_TYPE_INTRA_INV_SET2: [u8; 5] = [9, 0, 3, 1, 2];++pub static TX_WIDTH: [u8; 19] = [4, 8, 16, 32, 64, 4, 8, 8, 16, 16, 32, 32, 64, 4, 16, 8, 32, 16, 64];++pub static TX_WIDTH_LOG2: [u8; 19] = [2, 3, 4, 5, 6, 2, 3, 3, 4, 4, 5, 5, 6, 2, 4, 3, 5, 4, 6];diff --git a/crates/av1enc/src/transform.rs b/crates/av1enc/src/transform.rsnew file mode 100644index 000000000..a2b2d6575--- /dev/null+++ b/crates/av1enc/src/transform.rs@@ -0,0 +1,454 @@+//! Transforms. The inverse transforms are the normative ones of spec 7.13 (mirrored from+//! filmcraft-av1, so that the encoder reconstructs exactly like a decoder); the forward+//! transforms are floating-point matrices scaled to match them (not normative).++use std::sync::OnceLock;++use crate::tables::*;++#[inline(always)]+fn round2_64(x: i64, n: u32) -> i32 {+ if n == 0 { x as i32 } else { ((x + (1i64 << (n - 1))) >> n) as i32 }+}++#[inline(always)]+pub(crate) fn round2(x: i32, n: u32) -> i32 {+ if n == 0 { x } else { (x + (1 << (n - 1))) >> n }+}++#[inline(always)]+fn cos128(angle: i32) -> i64 {+ let a = (angle & 255) as usize;+ (match a {+ 0..=64 => COS128_LOOKUP[a] as i32,+ 65..=128 => -(COS128_LOOKUP[128 - a] as i32),+ 129..=192 => -(COS128_LOOKUP[a - 128] as i32),+ _ => COS128_LOOKUP[256 - a] as i32,+ }) as i64+}++#[inline(always)]+fn sin128(angle: i32) -> i64 {+ cos128(angle - 64)+}++fn brev(num_bits: u32, x: usize) -> usize {+ let mut t = 0;+ for i in 0..num_bits {+ let bit = (x >> i) & 1;+ t += bit << (num_bits - 1 - i);+ }+ t+}++#[inline(always)]+fn bf(t: &mut [i32], a: usize, b: usize, angle: i32, flip: bool) {+ let x = t[a] as i64 * cos128(angle) - t[b] as i64 * sin128(angle);+ let y = t[a] as i64 * sin128(angle) + t[b] as i64 * cos128(angle);+ t[a] = round2_64(x, 12);+ t[b] = round2_64(y, 12);+ if flip {+ t.swap(a, b);+ }+}++#[inline(always)]+fn hd(t: &mut [i32], a: usize, b: usize, flip: bool, r: u32) {+ let (a, b) = if flip { (b, a) } else { (a, b) };+ let x = t[a];+ let y = t[b];+ let lo = -(1i32 << (r - 1));+ let hi = (1i32 << (r - 1)) - 1;+ t[a] = (x + y).clamp(lo, hi);+ t[b] = (x - y).clamp(lo, hi);+}++/// Inverse DCT of length 2^n, n = 2..=5 (7.13.2.3), in place.+fn inverse_dct(t: &mut [i32], n: u32, r: u32) {+ let len = 1usize << n;+ let mut copy = [0i32; 32];+ copy[..len].copy_from_slice(&t[..len]);+ for i in 0..len {+ t[i] = copy[brev(n, i)];+ }+ if n >= 5 {+ for i in 0..8 {+ bf(t, 16 + i, 31 - i, 6 + ((brev(3, 7 - i) as i32) << 3), false);+ }+ }+ if n >= 4 {+ for i in 0..4 {+ bf(t, 8 + i, 15 - i, 12 + ((brev(2, 3 - i) as i32) << 4), false);+ }+ }+ if n >= 5 {+ for i in 0..8 {+ hd(t, 16 + 2 * i, 17 + 2 * i, i & 1 == 1, r);+ }+ }+ if n >= 3 {+ for i in 0..2 {+ bf(t, 4 + i, 7 - i, 56 - 32 * i as i32, false);+ }+ }+ if n >= 4 {+ for i in 0..4 {+ hd(t, 8 + 2 * i, 9 + 2 * i, i & 1 == 1, r);+ }+ }+ if n >= 5 {+ for i in 0..2 {+ for j in 0..2 {+ bf(t, 30 - 4 * i - j, 17 + 4 * i + j, 24 + ((j as i32) << 6) + (((1 - i) as i32) << 5), true);+ }+ }+ }+ for i in 0..2 {+ bf(t, 2 * i, 2 * i + 1, 32 + 16 * i as i32, i == 0);+ }+ if n >= 3 {+ for i in 0..2 {+ hd(t, 4 + 2 * i, 5 + 2 * i, i == 1, r);+ }+ }+ if n >= 4 {+ for i in 0..2 {+ bf(t, 14 - i, 9 + i, 48 + 64 * i as i32, true);+ }+ }+ if n >= 5 {+ for i in 0..4 {+ for j in 0..2 {+ hd(t, 16 + 4 * i + j, 19 + 4 * i - j, i & 1 == 1, r);+ }+ }+ }+ for i in 0..2 {+ hd(t, i, 3 - i, false, r);+ }+ if n >= 3 {+ bf(t, 6, 5, 32, true);+ }+ if n >= 4 {+ for i in 0..2 {+ for j in 0..2 {+ hd(t, 8 + 4 * i + j, 11 + 4 * i - j, i == 1, r);+ }+ }+ }+ if n >= 5 {+ for i in 0..4 {+ bf(t, 29 - i, 18 + i, 48 + (i as i32 >> 1) * 64, true);+ }+ }+ if n >= 3 {+ for i in 0..4 {+ hd(t, i, 7 - i, false, r);+ }+ }+ if n >= 4 {+ for i in 0..2 {+ bf(t, 13 - i, 10 + i, 32, true);+ }+ }+ if n >= 5 {+ for i in 0..2 {+ for j in 0..4 {+ hd(t, 16 + i * 8 + j, 23 + i * 8 - j, i == 1, r);+ }+ }+ }+ if n >= 4 {+ for i in 0..8 {+ hd(t, i, 15 - i, false, r);+ }+ }+ if n >= 5 {+ for i in 0..4 {+ bf(t, 27 - i, 20 + i, 32, true);+ }+ }+ if n >= 5 {+ for i in 0..16 {+ hd(t, i, 31 - i, false, r);+ }+ }+}++const SINPI_1_9: i64 = 1321;+const SINPI_2_9: i64 = 2482;+const SINPI_3_9: i64 = 3344;+const SINPI_4_9: i64 = 3803;++fn inverse_adst4(t: &mut [i32]) {+ let (t0, t1, t2, t3) = (t[0] as i64, t[1] as i64, t[2] as i64, t[3] as i64);+ let mut s = [SINPI_1_9 * t0, SINPI_2_9 * t0, SINPI_3_9 * t1, SINPI_4_9 * t2, SINPI_1_9 * t2, SINPI_2_9 * t3, SINPI_4_9 * t3];+ let a7 = t0 - t2;+ let b7 = a7 + t3;+ s[0] += s[3];+ s[1] -= s[4];+ s[3] = s[2];+ s[2] = SINPI_3_9 * b7;+ s[0] += s[5];+ s[1] -= s[6];+ let x = [s[0] + s[3], s[1] + s[3], s[2], s[0] + s[1] - s[3]];+ for i in 0..4 {+ t[i] = round2_64(x[i], 12);+ }+}++fn adst_in_permute(t: &mut [i32], n: u32) {+ let n0 = 1usize << n;+ let mut copy = [0i32; 16];+ copy[..n0].copy_from_slice(&t[..n0]);+ for i in 0..n0 {+ let idx = if i & 1 == 1 { i - 1 } else { n0 - i - 1 };+ t[i] = copy[idx];+ }+}++fn adst_out_permute(t: &mut [i32], n: u32) {+ let n0 = 1usize << n;+ let mut copy = [0i32; 16];+ copy[..n0].copy_from_slice(&t[..n0]);+ for i in 0..n0 {+ let a = (i >> 3) & 1;+ let b = ((i >> 2) & 1) ^ ((i >> 3) & 1);+ let c = ((i >> 1) & 1) ^ ((i >> 2) & 1);+ let d = (i & 1) ^ ((i >> 1) & 1);+ let idx = ((d << 3) | (c << 2) | (b << 1) | a) >> (4 - n);+ t[i] = if i & 1 == 1 { -copy[idx] } else { copy[idx] };+ }+}++fn inverse_adst8(t: &mut [i32], r: u32) {+ adst_in_permute(t, 3);+ for i in 0..4 {+ bf(t, 2 * i, 2 * i + 1, 60 - 16 * i as i32, true);+ }+ for i in 0..4 {+ hd(t, i, 4 + i, false, r);+ }+ for i in 0..2 {+ bf(t, 4 + 3 * i, 5 + i, 48 - 32 * i as i32, true);+ }+ for i in 0..2 {+ for j in 0..2 {+ hd(t, 4 * j + i, 2 + 4 * j + i, false, r);+ }+ }+ for i in 0..2 {+ bf(t, 2 + 4 * i, 3 + 4 * i, 32, true);+ }+ adst_out_permute(t, 3);+}++fn inverse_adst16(t: &mut [i32], r: u32) {+ adst_in_permute(t, 4);+ for i in 0..8 {+ bf(t, 2 * i, 2 * i + 1, 62 - 8 * i as i32, true);+ }+ for i in 0..8 {+ hd(t, i, 8 + i, false, r);+ }+ for i in 0..2 {+ bf(t, 8 + 2 * i, 9 + 2 * i, 56 - 32 * i as i32, true);+ bf(t, 13 + 2 * i, 12 + 2 * i, 8 + 32 * i as i32, true);+ }+ for i in 0..4 {+ for j in 0..2 {+ hd(t, 8 * j + i, 4 + 8 * j + i, false, r);+ }+ }+ for i in 0..2 {+ for j in 0..2 {+ bf(t, 4 + 8 * j + 3 * i, 5 + 8 * j + i, 48 - 32 * i as i32, true);+ }+ }+ for i in 0..2 {+ for j in 0..4 {+ hd(t, 4 * j + i, 2 + 4 * j + i, false, r);+ }+ }+ for i in 0..4 {+ bf(t, 2 + 4 * i, 3 + 4 * i, 32, true);+ }+ adst_out_permute(t, 4);+}++/// 1-D transform kind.+#[derive(Clone, Copy, PartialEq, Eq, Debug)]+pub(crate) enum Kind {+ Dct,+ Adst,+}++/// (vertical / column kind, horizontal / row kind) of a transform type.+pub(crate) fn kinds(tx_type: usize) -> (Kind, Kind) {+ match tx_type {+ DCT_DCT => (Kind::Dct, Kind::Dct),+ ADST_DCT => (Kind::Adst, Kind::Dct),+ DCT_ADST => (Kind::Dct, Kind::Adst),+ ADST_ADST => (Kind::Adst, Kind::Adst),+ _ => unreachable!("unsupported transform type {tx_type}"),+ }+}++fn inverse_1d(t: &mut [i32], kind: Kind, n: u32, r: u32) {+ match kind {+ Kind::Dct => inverse_dct(t, n, r),+ Kind::Adst => match n {+ 2 => inverse_adst4(t),+ 3 => inverse_adst8(t, r),+ _ => inverse_adst16(t, r),+ },+ }+}++/// 2-D inverse transform (7.13.3) of a square `n x n` block (n = 4..=32) of dequantised+/// coefficients (row stride `n`) into `residual` (row stride `n`).+pub(crate) fn inverse_2d(dequant: &[i32], residual: &mut [i32], tx_sz: usize, tx_type: usize, bit_depth: u32) {+ let log2 = TX_WIDTH_LOG2[tx_sz] as u32;+ let n = 1usize << log2;+ let row_shift = TRANSFORM_ROW_SHIFT[tx_sz] as u32;+ let row_clamp = bit_depth + 8;+ let col_clamp = (bit_depth + 6).max(16);+ let (ck, rk) = kinds(tx_type);+ let lo = -(1i32 << (col_clamp - 1));+ let hi = (1i32 << (col_clamp - 1)) - 1;+ let mut t = [0i32; 32];+ for i in 0..n {+ let row = &dequant[i * n..i * n + n];+ if row.iter().all(|&v| v == 0) {+ residual[i * n..i * n + n].iter_mut().for_each(|v| *v = 0);+ continue;+ }+ t[..n].copy_from_slice(row);+ inverse_1d(&mut t, rk, log2, row_clamp);+ for j in 0..n {+ residual[i * n + j] = round2(t[j], row_shift).clamp(lo, hi);+ }+ }+ for j in 0..n {+ for i in 0..n {+ t[i] = residual[i * n + j];+ }+ inverse_1d(&mut t, ck, log2, col_clamp);+ for i in 0..n {+ residual[i * n + j] = round2(t[i], 4);+ }+ }+}++/// Orthonormal forward basis matrices `[k][i]` (row k = basis function k), sizes 4..=32.+struct Bases {+ dct: [Vec<f32>; 4],+ adst: [Vec<f32>; 3],+}++fn bases() -> &'static Bases {+ static B: OnceLock<Bases> = OnceLock::new();+ B.get_or_init(|| {+ let pi = std::f64::consts::PI;+ let dct = std::array::from_fn(|l| {+ let n = 4usize << l;+ let mut m = vec![0f32; n * n];+ for k in 0..n {+ let c = if k == 0 { std::f64::consts::FRAC_1_SQRT_2 } else { 1.0 };+ for i in 0..n {+ m[k * n + i] = (c * (2.0 / n as f64).sqrt() * (pi * (2 * i + 1) as f64 * k as f64 / (2 * n) as f64).cos()) as f32;+ }+ }+ m+ });+ let adst = std::array::from_fn(|l| {+ let n = 4usize << l;+ let mut m = vec![0f32; n * n];+ for k in 0..n {+ for i in 0..n {+ m[k * n + i] = if n == 4 {+ // the AV1 4-point ADST (sin(pi (i + 1)(2k + 1) / 9) basis)+ (2.0 / 3.0 * (pi * ((i + 1) * (2 * k + 1)) as f64 / 9.0).sin()) as f32+ } else {+ ((2.0 / n as f64).sqrt() * (pi * ((2 * i + 1) * (2 * k + 1)) as f64 / (4 * n) as f64).sin()) as f32+ };+ }+ }+ m+ });+ Bases { dct, adst }+ })+}++fn basis(kind: Kind, n: usize) -> &'static [f32] {+ let l = n.trailing_zeros() as usize - 2;+ match kind {+ Kind::Dct => &bases().dct[l],+ Kind::Adst => &bases().adst[l],+ }+}++/// Forward 2-D transform of the `n x n` residual (row stride `n`) into orthonormal-domain+/// coefficients scaled by 8 (so that `level = out / q` with the spec's dequantiser).+pub(crate) fn forward_2d(residual: &[i32], out: &mut [f32], n: usize, tx_type: usize) {+ let (ck, rk) = kinds(tx_type);+ let bc = basis(ck, n);+ let br = basis(rk, n);+ // tmp[i][k] = sum_j residual[i][j] * br[k][j] (horizontal)+ let mut tmp = vec![0f32; n * n];+ for i in 0..n {+ let row = &residual[i * n..i * n + n];+ for k in 0..n {+ let b = &br[k * n..k * n + n];+ let mut s = 0f32;+ for j in 0..n {+ s += row[j] as f32 * b[j];+ }+ tmp[i * n + k] = s;+ }+ }+ // out[k][j] = 8 * sum_i bc[k][i] * tmp[i][j] (vertical)+ for k in 0..n {+ let b = &bc[k * n..k * n + n];+ for j in 0..n {+ let mut s = 0f32;+ for i in 0..n {+ s += b[i] * tmp[i * n + j];+ }+ out[k * n + j] = 8.0 * s;+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ /// forward (float) then the normative inverse reproduces the residual: the forward basis+ /// and scale match the decoder's inverse transforms for every supported size and type.+ #[test]+ fn forward_matches_inverse() {+ let mut seed = 99u32;+ let mut rnd = || {+ seed = seed.wrapping_mul(1_103_515_245).wrapping_add(12345);+ ((seed >> 16) % 201) as i32 - 100+ };+ for (tx, n) in [(TX_4X4, 4), (TX_8X8, 8), (TX_16X16, 16), (TX_32X32, 32)] {+ let denom = if tx == TX_32X32 { 2.0 } else { 1.0 };+ for tx_type in [DCT_DCT, ADST_DCT, DCT_ADST, ADST_ADST] {+ if n == 32 && tx_type != DCT_DCT {+ continue;+ }+ let res: Vec<i32> = (0..n * n).map(|_| rnd()).collect();+ let mut coef = vec![0f32; n * n];+ forward_2d(&res, &mut coef, n, tx_type);+ // q = 8 / denom keeps full precision: dequant = level * q / denom = coef+ let deq: Vec<i32> = coef.iter().map(|&c| (c / denom).round() as i32).collect();+ let mut out = vec![0i32; n * n];+ inverse_2d(&deq, &mut out, tx, tx_type, 8);+ let err: i32 = res.iter().zip(&out).map(|(a, b)| (a - b).abs()).max().unwrap();+ assert!(err <= 1, "tx {tx} type {tx_type}: max error {err}");+ }+ }+ }+}diff --git a/crates/av1enc/src/writer.rs b/crates/av1enc/src/writer.rsnew file mode 100644index 000000000..729a0887f--- /dev/null+++ b/crates/av1enc/src/writer.rs@@ -0,0 +1,981 @@+//! Tile syntax writer (spec 5.11 with the 8.3 CDF selection). It keeps the same per-4x4 mode+//! info, coefficient contexts and adaptive CDFs as a decoder and writes the decisions taken by+//! `decide.rs` as symbols. The context derivations and the motion vector prediction process+//! (7.10.2) mirror the decoder exactly, since symbols and coded motion vector differences+//! depend on them.++use crate::cdf::Cdfs;+use crate::decide::{FrameGeom, Leaf, Node, intra_uv_tx_type, scan_for, tx_in_set, tx_set, tx_size_for};+use crate::symbol::SymbolWriter;+use crate::tables::*;++type MvP = [i32; 2];++/// Mode info per 4x4 (MiSizes, YModes, RefFrames, Mvs, ...).+struct Mi {+ cols: usize,+ mi_size: Vec<u8>,+ y_mode: Vec<u8>,+ ref_frame: Vec<[i8; 2]>,+ mv: Vec<MvP>,+ skip: Vec<bool>,+ is_inter: Vec<bool>,+ tx_type: Vec<u8>,+ written: Vec<bool>,+}++impl Mi {+ #[inline(always)]+ fn idx(&self, r: usize, c: usize) -> usize {+ r * self.cols + c+ }+}++/// Output of find_mv_stack.+#[derive(Default)]+struct MvStack {+ ref_stack_mv: [MvP; MAX_REF_MV_STACK_SIZE + 1],+ weight_stack: [u32; MAX_REF_MV_STACK_SIZE + 1],+ num_mv_found: usize,+ new_mv_count: usize,+ found_match: bool,+ close_matches: usize,+ total_matches: usize,+ new_mv_context: usize,+ ref_mv_context: usize,+ zero_mv_context: usize,+ drl_ctx_stack: [usize; MAX_REF_MV_STACK_SIZE + 1],+}++pub(crate) struct TileWriter<'a> {+ g: &'a FrameGeom,+ intra_only: bool,+ qidx: u8,+ sw: SymbolWriter,+ cdf: Box<Cdfs>,+ kf_y_mode: [[[u16; 14]; 5]; 5],+ mi: Mi,+ above_level: [Vec<u8>; 3],+ above_dc: [Vec<u8>; 3],+ left_level: [Vec<u8>; 3],+ left_dc: [Vec<u8>; 3],+ // current block+ r: usize,+ c: usize,+ bsize: usize,+ avail_u: bool,+ avail_l: bool,+ mvs: MvStack,+}++fn block_size_px(bs: usize) -> usize {+ 4 * NUM_4X4_BLOCKS_WIDE[bs] as usize+}++fn is_directional(mode: usize) -> bool {+ (V_PRED..=D67_PRED).contains(&mode)+}++impl<'a> TileWriter<'a> {+ pub fn new(g: &'a FrameGeom, intra_only: bool, qidx: u8) -> Self {+ let n = g.mi_cols * g.mi_rows;+ TileWriter {+ g,+ intra_only,+ qidx,+ sw: SymbolWriter::new(false),+ cdf: Cdfs::new(qidx as u32),+ kf_y_mode: DEFAULT_INTRA_FRAME_Y_MODE_CDF,+ mi: Mi {+ cols: g.mi_cols,+ mi_size: vec![0; n],+ y_mode: vec![0; n],+ ref_frame: vec![[0, -1]; n],+ mv: vec![[0, 0]; n],+ skip: vec![false; n],+ is_inter: vec![false; n],+ tx_type: vec![0; n],+ written: vec![false; n],+ },+ above_level: std::array::from_fn(|_| vec![0; g.mi_cols + 64]),+ above_dc: std::array::from_fn(|_| vec![0; g.mi_cols + 64]),+ left_level: std::array::from_fn(|_| vec![0; g.mi_rows + 64]),+ left_dc: std::array::from_fn(|_| vec![0; g.mi_rows + 64]),+ r: 0,+ c: 0,+ bsize: 0,+ avail_u: false,+ avail_l: false,+ mvs: MvStack::default(),+ }+ }++ pub fn finish(self) -> Vec<u8> {+ self.sw.finish()+ }++ /// Start of a superblock row (clear_left_context).+ pub fn start_sb_row(&mut self) {+ for p in 0..3 {+ self.left_level[p].iter_mut().for_each(|v| *v = 0);+ self.left_dc[p].iter_mut().for_each(|v| *v = 0);+ }+ }++ pub fn superblock(&mut self, r: usize, c: usize, node: &Node) {+ self.partition(r, c, BLOCK_64X64, node);+ }++ fn partition(&mut self, r: usize, c: usize, bsize: usize, node: &Node) {+ let (mi_rows, mi_cols) = (self.g.mi_rows, self.g.mi_cols);+ if r >= mi_rows || c >= mi_cols {+ return;+ }+ let avail_u = r > 0;+ let avail_l = c > 0;+ let n4 = NUM_4X4_BLOCKS_WIDE[bsize] as usize;+ let half = n4 >> 1;+ let has_rows = r + half < mi_rows;+ let has_cols = c + half < mi_cols;+ let split = matches!(node, Node::Split(_));+ let bsl = MI_WIDTH_LOG2[bsize] as usize;+ let above = avail_u && (MI_WIDTH_LOG2[self.mi.mi_size[self.mi.idx(r - 1, c)] as usize] as usize) < bsl;+ let left = avail_l && (MI_HEIGHT_LOG2[self.mi.mi_size[self.mi.idx(r, c - 1)] as usize] as usize) < bsl;+ let ctx = left as usize * 2 + above as usize;+ let partition = if split { PARTITION_SPLIT } else { PARTITION_NONE };+ if has_rows && has_cols {+ let cdf: &mut [u16] = match bsl {+ 1 => &mut self.cdf.partition_w8[ctx],+ 2 => &mut self.cdf.partition_w16[ctx],+ 3 => &mut self.cdf.partition_w32[ctx],+ _ => &mut self.cdf.partition_w64[ctx],+ };+ self.sw.symbol(partition, cdf);+ } else if has_cols || has_rows {+ assert!(split, "blocks crossing the frame edge are split");+ let pcdf: Vec<u16> = match bsl {+ 2 => self.cdf.partition_w16[ctx].to_vec(),+ 3 => self.cdf.partition_w32[ctx].to_vec(),+ _ => self.cdf.partition_w64[ctx].to_vec(),+ };+ let pr = |k: usize| pcdf[k] as i32 - if k == 0 { 0 } else { pcdf[k - 1] as i32 };+ let psum = if has_cols {+ // split_or_horz+ pr(PARTITION_VERT) + pr(PARTITION_SPLIT) + pr(PARTITION_HORZ_A) + pr(PARTITION_VERT_A) + pr(PARTITION_VERT_B) + pr(PARTITION_VERT_4)+ } else {+ // split_or_vert+ pr(PARTITION_HORZ) + pr(PARTITION_SPLIT) + pr(PARTITION_HORZ_A) + pr(PARTITION_HORZ_B) + pr(PARTITION_VERT_A) + pr(PARTITION_HORZ_4)+ };+ let cdf = [((1i32 << 15) - psum) as u16, 1 << 15, 0];+ self.sw.symbol_fixed(1, &cdf);+ } else {+ assert!(split);+ }+ match node {+ Node::Split(ch) => {+ let sub = PARTITION_SUBSIZE[PARTITION_SPLIT][bsize] as usize;+ self.partition(r, c, sub, &ch[0]);+ self.partition(r, c + half, sub, &ch[1]);+ self.partition(r + half, c, sub, &ch[2]);+ self.partition(r + half, c + half, sub, &ch[3]);+ }+ Node::Leaf(leaf) => {+ debug_assert_eq!(leaf.bsize, bsize);+ self.block(r, c, leaf);+ }+ Node::Out => unreachable!("decided block inside the frame"),+ }+ }++ fn block(&mut self, r: usize, c: usize, b: &Leaf) {+ self.r = r;+ self.c = c;+ self.bsize = b.bsize;+ self.avail_u = r > 0;+ self.avail_l = c > 0;+ let bsize = b.bsize;+ let n4 = NUM_4X4_BLOCKS_WIDE[bsize] as usize;+ // skip+ let mut ctx = 0;+ if self.avail_u {+ ctx += self.mi.skip[self.mi.idx(r - 1, c)] as usize;+ }+ if self.avail_l {+ ctx += self.mi.skip[self.mi.idx(r, c - 1)] as usize;+ }+ let mut y_mode = b.y_mode;+ if self.intra_only {+ self.sw.symbol(b.skip as usize, &mut self.cdf.skip[ctx]);+ let above = if self.avail_u { self.mi.y_mode[self.mi.idx(r - 1, c)] as usize } else { DC_PRED };+ let left = if self.avail_l { self.mi.y_mode[self.mi.idx(r, c - 1)] as usize } else { DC_PRED };+ let actx = INTRA_MODE_CONTEXT[above] as usize;+ let lctx = INTRA_MODE_CONTEXT[left] as usize;+ self.sw.symbol(b.y_mode, &mut self.kf_y_mode[actx][lctx]);+ self.intra_angle_and_uv(b);+ } else {+ self.sw.symbol(b.skip as usize, &mut self.cdf.skip[ctx]);+ // is_inter+ let left = if self.avail_l { self.mi.ref_frame[self.mi.idx(r, c - 1)] } else { [INTRA_FRAME as i8, -1] };+ let above = if self.avail_u { self.mi.ref_frame[self.mi.idx(r - 1, c)] } else { [INTRA_FRAME as i8, -1] };+ let left_intra = left[0] <= INTRA_FRAME as i8;+ let above_intra = above[0] <= INTRA_FRAME as i8;+ let ctx = if self.avail_u && self.avail_l {+ if left_intra && above_intra { 3 } else { (left_intra || above_intra) as usize }+ } else if self.avail_u || self.avail_l {+ 2 * (if self.avail_u { above_intra } else { left_intra }) as usize+ } else {+ 0+ };+ self.sw.symbol(b.is_inter as usize, &mut self.cdf.is_inter[ctx]);+ if b.is_inter {+ y_mode = self.inter_block_mode_info(b, left, above);+ } else {+ let ctx = SIZE_GROUP[bsize] as usize;+ self.sw.symbol(b.y_mode, &mut self.cdf.y_mode[ctx]);+ self.intra_angle_and_uv(b);+ }+ }+ // mode info stored before the residual+ let (rows, cols) = (n4.min(self.g.mi_rows - r), n4.min(self.g.mi_cols - c));+ for y in 0..rows {+ for x in 0..cols {+ let i = self.mi.idx(r + y, c + x);+ self.mi.y_mode[i] = y_mode as u8;+ self.mi.ref_frame[i] = if b.is_inter { [LAST_FRAME as i8, -1] } else { [INTRA_FRAME as i8, -1] };+ self.mi.written[i] = true;+ if b.is_inter {+ self.mi.mv[i] = b.mv;+ }+ }+ }+ if b.skip {+ // reset_block_context( )+ for plane in 0..3 {+ let s = (plane > 0) as usize;+ for i in (c >> s)..((c + n4) >> s) {+ self.above_level[plane][i] = 0;+ self.above_dc[plane][i] = 0;+ }+ for i in (r >> s)..((r + n4) >> s) {+ self.left_level[plane][i] = 0;+ self.left_dc[plane][i] = 0;+ }+ }+ } else {+ let log2 = (n4 * 4).trailing_zeros();+ assert!(log2 <= 5, "64x64 blocks are coded as skip blocks");+ for plane in 0..3 {+ let s = (plane > 0) as u32;+ self.coeffs(plane, (c * 4) >> s, (r * 4) >> s, tx_size_for(log2 - s), b, &b.coefs[plane]);+ }+ }+ for y in 0..rows {+ for x in 0..cols {+ let i = self.mi.idx(r + y, c + x);+ self.mi.is_inter[i] = b.is_inter;+ self.mi.skip[i] = b.skip;+ self.mi.mi_size[i] = bsize as u8;+ }+ }+ }++ fn intra_angle_and_uv(&mut self, b: &Leaf) {+ if self.bsize >= BLOCK_8X8 && is_directional(b.y_mode) {+ self.sw.symbol(MAX_ANGLE_DELTA, &mut self.cdf.angle_delta[b.y_mode - V_PRED]);+ }+ let bs = block_size_px(self.bsize);+ if bs <= 32 {+ self.sw.symbol(b.uv_mode, &mut self.cdf.uv_mode_cfl_allowed[b.y_mode]);+ } else {+ self.sw.symbol(b.uv_mode, &mut self.cdf.uv_mode_cfl_not_allowed[b.y_mode]);+ }+ if self.bsize >= BLOCK_8X8 && is_directional(b.uv_mode) {+ self.sw.symbol(MAX_ANGLE_DELTA, &mut self.cdf.angle_delta[b.uv_mode - V_PRED]);+ }+ }++ /// read_ref_frames (LAST_FRAME), find_mv_stack, the inter mode, DRL and motion vector.+ /// Returns the inter mode (YMode).+ fn inter_block_mode_info(&mut self, b: &Leaf, left: [i8; 2], above: [i8; 2]) -> usize {+ let count = |ft: i8| -> usize {+ let mut n = 0;+ if self.avail_u {+ n += (above[0] == ft) as usize + (above[1] == ft) as usize;+ }+ if self.avail_l {+ n += (left[0] == ft) as usize + (left[1] == ft) as usize;+ }+ n+ };+ let cmp = |a: usize, b: usize| match a.cmp(&b) {+ std::cmp::Ordering::Less => 0,+ std::cmp::Ordering::Equal => 1,+ std::cmp::Ordering::Greater => 2,+ };+ let (last, last2, last3, gold) = (count(LAST_FRAME as i8), count(LAST2_FRAME as i8), count(LAST3_FRAME as i8), count(GOLDEN_FRAME as i8));+ let (bwd, alt2, alt) = (count(BWDREF_FRAME as i8), count(ALTREF2_FRAME as i8), count(ALTREF_FRAME as i8));+ let ctx_p1 = cmp(last + last2 + last3 + gold, bwd + alt2 + alt);+ let ctx_comp_ref = cmp(last + last2, last3 + gold);+ let ctx_comp_ref_p1 = cmp(last, last2);+ self.sw.symbol(0, &mut self.cdf.single_ref[ctx_p1][0]);+ self.sw.symbol(0, &mut self.cdf.single_ref[ctx_comp_ref][2]);+ self.sw.symbol(0, &mut self.cdf.single_ref[ctx_comp_ref_p1][3]);+ self.find_mv_stack();+ let mv = b.mv;+ let s = &self.mvs;+ let mode = if mv == [0, 0] {+ GLOBALMV+ } else if mv == s.ref_stack_mv[0] {+ NEARESTMV+ } else if mv == s.ref_stack_mv[1] {+ NEARMV+ } else {+ NEWMV+ };+ let (nctx, zctx, rctx) = (s.new_mv_context, s.zero_mv_context, s.ref_mv_context);+ self.sw.symbol((mode != NEWMV) as usize, &mut self.cdf.new_mv[nctx]);+ if mode != NEWMV {+ self.sw.symbol((mode != GLOBALMV) as usize, &mut self.cdf.zero_mv[zctx]);+ if mode != GLOBALMV {+ self.sw.symbol((mode == NEARMV) as usize, &mut self.cdf.ref_mv[rctx]);+ }+ }+ // DRL: ref_mv_idx 0 for NEWMV, 1 for NEARMV+ if mode == NEWMV {+ if self.mvs.num_mv_found > 1 {+ let ctx = self.mvs.drl_ctx_stack[0];+ self.sw.symbol(0, &mut self.cdf.drl_mode[ctx]);+ }+ } else if mode == NEARMV && self.mvs.num_mv_found > 2 {+ let ctx = self.mvs.drl_ctx_stack[1];+ self.sw.symbol(0, &mut self.cdf.drl_mode[ctx]);+ }+ if mode == NEWMV {+ let pred = self.mvs.ref_stack_mv[0];+ self.write_mv([mv[0] - pred[0], mv[1] - pred[1]]);+ }+ mode+ }++ fn write_mv(&mut self, diff: MvP) {+ let joint = ((diff[0] != 0) as usize) << 1 | (diff[1] != 0) as usize;+ // MV_JOINT_ZERO, MV_JOINT_HNZVZ (col only), MV_JOINT_HZVNZ (row only), MV_JOINT_HNZVNZ+ self.sw.symbol(joint, &mut self.cdf.mv_joint);+ for (comp, &d) in diff.iter().enumerate() {+ if d != 0 {+ self.write_mv_component(comp, d);+ }+ }+ }++ fn write_mv_component(&mut self, comp: usize, v: i32) {+ debug_assert!(v % 2 == 0, "quarter-sample precision");+ self.sw.symbol((v < 0) as usize, &mut self.cdf.mv_sign[comp]);+ let m = v.unsigned_abs() - 1; // odd: the implied hp bit is 1+ let fr = ((m >> 1) & 3) as usize;+ let int = m >> 3;+ if int < CLASS0_SIZE as u32 {+ self.sw.symbol(MV_CLASS_0, &mut self.cdf.mv_class[comp]);+ self.sw.symbol(int as usize, &mut self.cdf.mv_class0_bit[comp]);+ self.sw.symbol(fr, &mut self.cdf.mv_class0_fr[comp][int as usize]);+ } else {+ let class = 31 - int.leading_zeros();+ let d = int - (1 << class);+ self.sw.symbol(class as usize, &mut self.cdf.mv_class[comp]);+ for i in 0..class as usize {+ self.sw.symbol(((d >> i) & 1) as usize, &mut self.cdf.mv_bit[comp][i]);+ }+ self.sw.symbol(fr, &mut self.cdf.mv_fr[comp]);+ }+ }++ // ---- coefficients ----------------------------------------------------------------------++ fn coeffs(&mut self, plane: usize, start_x: usize, start_y: usize, tx_sz: usize, b: &Leaf, levels: &[i32]) {+ let x4 = start_x >> 2;+ let y4 = start_y >> 2;+ let w4 = TX_WIDTH[tx_sz] as usize >> 2;+ let h4 = TX_HEIGHT[tx_sz] as usize >> 2;+ let tx_sz_ctx = (TX_SIZE_SQR[tx_sz] as usize + TX_SIZE_SQR_UP[tx_sz] as usize + 1) >> 1;+ let ptype = (plane > 0) as usize;+ let scan = scan_for(tx_sz);+ let mut eob = 0;+ for (i, &pos) in scan.iter().enumerate() {+ if levels[pos as usize] != 0 {+ eob = i + 1;+ }+ }+ let ctx = self.all_zero_ctx(plane, tx_sz, x4, y4, w4, h4);+ self.sw.symbol((eob == 0) as usize, &mut self.cdf.txb_skip[tx_sz_ctx][ctx]);+ let mut cul_level = 0u32;+ let mut dc_category = 0u8;+ if eob == 0 {+ if plane == 0 {+ self.set_tx_types(x4, y4, tx_sz, DCT_DCT);+ }+ } else {+ if plane == 0 {+ // transform_type( )+ let set = tx_set(tx_sz, b.is_inter);+ let t = b.tx_type_y;+ if set > 0 && self.qidx > 0 {+ let sqr = TX_SIZE_SQR[tx_sz] as usize;+ if b.is_inter {+ let s = TX_TYPE_INTER_INV_SET3.iter().position(|&v| v as usize == t).expect("inter tx type in set");+ self.sw.symbol(s, &mut self.cdf.inter_tx_type_set3[sqr]);+ } else {+ let s = TX_TYPE_INTRA_INV_SET2.iter().position(|&v| v as usize == t).expect("intra tx type in set");+ self.sw.symbol(s, &mut self.cdf.intra_tx_type_set2[sqr][b.y_mode]);+ }+ } else {+ assert_eq!(t, DCT_DCT);+ }+ self.set_tx_types(x4, y4, tx_sz, t);+ }+ let plane_tx_type = self.compute_tx_type(plane, tx_sz, x4, y4, b);+ debug_assert!(plane_tx_type == DCT_DCT || matches!(plane_tx_type, ADST_DCT | DCT_ADST | ADST_ADST));+ let eob_multisize = (TX_WIDTH_LOG2[tx_sz] as usize).min(5) + (TX_HEIGHT_LOG2[tx_sz] as usize).min(5) - 4;+ let eob_pt = if eob < 2 { eob } else { (32 - ((eob - 1) as u32).leading_zeros()) as usize + 1 };+ let ectx = 0; // TX_CLASS_2D+ let s = eob_pt - 1;+ match eob_multisize {+ 0 => self.sw.symbol(s, &mut self.cdf.eob_pt_16[ptype][ectx]),+ 1 => self.sw.symbol(s, &mut self.cdf.eob_pt_32[ptype][ectx]),+ 2 => self.sw.symbol(s, &mut self.cdf.eob_pt_64[ptype][ectx]),+ 3 => self.sw.symbol(s, &mut self.cdf.eob_pt_128[ptype][ectx]),+ 4 => self.sw.symbol(s, &mut self.cdf.eob_pt_256[ptype][ectx]),+ 5 => self.sw.symbol(s, &mut self.cdf.eob_pt_512[ptype]),+ _ => self.sw.symbol(s, &mut self.cdf.eob_pt_1024[ptype]),+ }+ if eob_pt >= 3 {+ let eob_shift = eob_pt - 3;+ let offset = eob - ((1 << (eob_pt - 2)) + 1);+ self.sw.symbol((offset >> eob_shift) & 1, &mut self.cdf.eob_extra[tx_sz_ctx][ptype][eob_pt - 3]);+ for i in 1..eob_pt - 2 {+ let sh = eob_pt - 2 - 1 - i;+ self.sw.bool((offset >> sh) & 1 == 1);+ }+ }+ let bwl = TX_WIDTH_LOG2[tx_sz] as usize;+ let height = TX_HEIGHT[tx_sz] as usize;+ let width = 1usize << bwl;+ // Quant[] as the decoder sees it while parsing: the levels coded so far (capped)+ let mut quant = vec![0i32; width * height];+ for c in (0..eob).rev() {+ let pos = scan[c] as usize;+ let a = levels[pos].unsigned_abs().min(15) as i32;+ if c == eob - 1 {+ let ctx = coeff_base_eob_ctx(c, bwl, height);+ self.sw.symbol((a.min(3) - 1) as usize, &mut self.cdf.coeff_base_eob[tx_sz_ctx][ptype][ctx]);+ } else {+ let ctx = coeff_base_ctx(&quant, tx_sz, bwl, width, height, pos);+ self.sw.symbol(a.min(3) as usize, &mut self.cdf.coeff_base[tx_sz_ctx][ptype][ctx]);+ }+ if a > NUM_BASE_LEVELS as i32 {+ let ctx = coeff_br_ctx(&quant, bwl, height, pos);+ let mut rem = a - 3;+ for _ in 0..(COEFF_BASE_RANGE / (BR_CDF_SIZE - 1)) {+ let br = rem.min(3);+ self.sw.symbol(br as usize, &mut self.cdf.coeff_br[tx_sz_ctx.min(TX_32X32)][ptype][ctx]);+ rem -= br;+ if br < 3 {+ break;+ }+ }+ }+ quant[pos] = a;+ }+ for c in 0..eob {+ let pos = scan[c] as usize;+ let v = levels[pos];+ if v != 0 {+ if c == 0 {+ let ctx = self.dc_sign_ctx(plane, x4, y4, w4, h4);+ self.sw.symbol((v < 0) as usize, &mut self.cdf.dc_sign[ptype][ctx]);+ } else {+ self.sw.bool(v < 0);+ }+ }+ let a = v.unsigned_abs();+ if a > (NUM_BASE_LEVELS + COEFF_BASE_RANGE) as u32 {+ // Golomb code of a - 15+ let x = a - 15 + 1;+ let length = 32 - x.leading_zeros();+ for _ in 0..length - 1 {+ self.sw.bool(false);+ }+ self.sw.bool(true);+ for i in (0..length - 1).rev() {+ self.sw.bool((x >> i) & 1 == 1);+ }+ }+ if pos == 0 && a > 0 {+ dc_category = if v < 0 { 1 } else { 2 };+ }+ cul_level += a & 0xFFFFF;+ }+ cul_level = cul_level.min(63);+ }+ for i in 0..w4 {+ if x4 + i < self.above_level[plane].len() {+ self.above_level[plane][x4 + i] = cul_level as u8;+ self.above_dc[plane][x4 + i] = dc_category;+ }+ }+ for i in 0..h4 {+ if y4 + i < self.left_level[plane].len() {+ self.left_level[plane][y4 + i] = cul_level as u8;+ self.left_dc[plane][y4 + i] = dc_category;+ }+ }+ }++ fn set_tx_types(&mut self, x4: usize, y4: usize, tx_sz: usize, t: usize) {+ for i in 0..(TX_WIDTH[tx_sz] as usize >> 2) {+ for j in 0..(TX_HEIGHT[tx_sz] as usize >> 2) {+ if y4 + j < self.g.mi_rows && x4 + i < self.g.mi_cols {+ let k = self.mi.idx(y4 + j, x4 + i);+ self.mi.tx_type[k] = t as u8;+ }+ }+ }+ }++ fn compute_tx_type(&self, plane: usize, tx_sz: usize, x4: usize, y4: usize, b: &Leaf) -> usize {+ if TX_SIZE_SQR_UP[tx_sz] as usize > TX_32X32 {+ return DCT_DCT;+ }+ if plane == 0 {+ return self.mi.tx_type[self.mi.idx(y4, x4)] as usize;+ }+ if b.is_inter {+ let set = tx_set(tx_sz, true);+ let xx = self.c.max(x4 << 1);+ let yy = self.r.max(y4 << 1);+ let t = self.mi.tx_type[self.mi.idx(yy, xx)] as usize;+ return if tx_in_set(set, true, t) { t } else { DCT_DCT };+ }+ intra_uv_tx_type(b.uv_mode, tx_sz)+ }++ fn all_zero_ctx(&self, plane: usize, tx_sz: usize, x4: usize, y4: usize, w4: usize, h4: usize) -> usize {+ let s = (plane > 0) as usize;+ let max_x4 = self.g.mi_cols >> s;+ let max_y4 = self.g.mi_rows >> s;+ let w = TX_WIDTH[tx_sz] as usize;+ let h = TX_HEIGHT[tx_sz] as usize;+ let bsize = SUBSAMPLED_SIZE[self.bsize][s][s] as usize;+ let bw = block_size_px(bsize);+ let bh = 4 * NUM_4X4_BLOCKS_HIGH[bsize] as usize;+ let al = &self.above_level[plane];+ let ll = &self.left_level[plane];+ if plane == 0 {+ let mut top = 0u32;+ let mut left = 0u32;+ for k in 0..w4 {+ if x4 + k < max_x4 {+ top = top.max(al[x4 + k] as u32);+ }+ }+ for k in 0..h4 {+ if y4 + k < max_y4 {+ left = left.max(ll[y4 + k] as u32);+ }+ }+ top = top.min(255);+ left = left.min(255);+ if bw == w && bh == h {+ 0+ } else if top == 0 && left == 0 {+ 1+ } else if top == 0 || left == 0 {+ 2 + (top.max(left) > 3) as usize+ } else if top.max(left) <= 3 {+ 4+ } else if top.min(left) <= 3 {+ 5+ } else {+ 6+ }+ } else {+ let ad = &self.above_dc[plane];+ let ld = &self.left_dc[plane];+ let mut above = 0u8;+ let mut left = 0u8;+ for i in 0..w4 {+ if x4 + i < max_x4 {+ above |= al[x4 + i] | ad[x4 + i];+ }+ }+ for i in 0..h4 {+ if y4 + i < max_y4 {+ left |= ll[y4 + i] | ld[y4 + i];+ }+ }+ let mut ctx = (above != 0) as usize + (left != 0) as usize + 7;+ if bw * bh > w * h {+ ctx += 3;+ }+ ctx+ }+ }++ fn dc_sign_ctx(&self, plane: usize, x4: usize, y4: usize, w4: usize, h4: usize) -> usize {+ let s = (plane > 0) as usize;+ let max_x4 = self.g.mi_cols >> s;+ let max_y4 = self.g.mi_rows >> s;+ let mut dc_sign = 0i32;+ for k in 0..w4 {+ if x4 + k < max_x4 {+ match self.above_dc[plane][x4 + k] {+ 1 => dc_sign -= 1,+ 2 => dc_sign += 1,+ _ => {}+ }+ }+ }+ for k in 0..h4 {+ if y4 + k < max_y4 {+ match self.left_dc[plane][y4 + k] {+ 1 => dc_sign -= 1,+ 2 => dc_sign += 1,+ _ => {}+ }+ }+ }+ match dc_sign.signum() {+ -1 => 1,+ 1 => 2,+ _ => 0,+ }+ }++ // ---- motion vector prediction (7.10.2), single reference LAST_FRAME --------------------++ fn inside(&self, r: isize, c: isize) -> bool {+ c >= 0 && c < self.g.mi_cols as isize && r >= 0 && r < self.g.mi_rows as isize+ }++ fn find_mv_stack(&mut self) {+ let n4 = NUM_4X4_BLOCKS_WIDE[self.bsize] as isize;+ let (bw4, bh4) = (n4, n4);+ self.mvs = MvStack::default();+ self.scan_row(-1);+ let mut found_above = self.mvs.found_match;+ self.mvs.found_match = false;+ self.scan_col(-1);+ let mut found_left = self.mvs.found_match;+ self.mvs.found_match = false;+ if bw4.max(bh4) <= 16 {+ self.scan_point(-1, bw4);+ }+ if self.mvs.found_match {+ found_above = true;+ }+ self.mvs.close_matches = found_above as usize + found_left as usize;+ let num_nearest = self.mvs.num_mv_found;+ let num_new = self.mvs.new_mv_count;+ for idx in 0..num_nearest {+ self.mvs.weight_stack[idx] += REF_CAT_LEVEL as u32;+ }+ self.mvs.zero_mv_context = 0;+ // use_ref_frame_mvs = 0: no temporal candidates+ self.scan_point(-1, -1);+ if self.mvs.found_match {+ found_above = true;+ }+ self.mvs.found_match = false;+ self.scan_row(-3);+ if self.mvs.found_match {+ found_above = true;+ }+ self.mvs.found_match = false;+ self.scan_col(-3);+ if self.mvs.found_match {+ found_left = true;+ }+ self.mvs.found_match = false;+ if bh4 > 1 {+ self.scan_row(-5);+ }+ if self.mvs.found_match {+ found_above = true;+ }+ self.mvs.found_match = false;+ if bw4 > 1 {+ self.scan_col(-5);+ }+ if self.mvs.found_match {+ found_left = true;+ }+ self.mvs.total_matches = found_above as usize + found_left as usize;+ let n = self.mvs.num_mv_found;+ self.sort_stack(0, num_nearest);+ self.sort_stack(num_nearest, n);+ if self.mvs.num_mv_found < 2 {+ self.extra_search();+ }+ self.context_and_clamping(num_new);+ }++ fn scan_row(&mut self, delta_row: isize) {+ let bw4 = NUM_4X4_BLOCKS_WIDE[self.bsize] as isize;+ let (mi_row, mi_col) = (self.r as isize, self.c as isize);+ let end4 = bw4.min(self.g.mi_cols as isize - mi_col).min(16);+ let mut delta_col = 0;+ let use_step16 = bw4 >= 16;+ let mut delta_row = delta_row;+ if delta_row.abs() > 1 {+ delta_row += mi_row & 1;+ delta_col = 1 - (mi_col & 1);+ }+ let mut i = 0;+ while i < end4 {+ let mv_row = mi_row + delta_row;+ let mv_col = mi_col + delta_col + i;+ if !self.inside(mv_row, mv_col) {+ break;+ }+ let k = self.mi.idx(mv_row as usize, mv_col as usize);+ let mut len = bw4.min(NUM_4X4_BLOCKS_WIDE[self.mi.mi_size[k] as usize] as isize);+ if delta_row.abs() > 1 {+ len = len.max(2);+ }+ if use_step16 {+ len = len.max(4);+ }+ self.add_ref_mv_candidate(mv_row as usize, mv_col as usize, len as u32 * 2);+ i += len;+ }+ }++ fn scan_col(&mut self, delta_col: isize) {+ let bh4 = NUM_4X4_BLOCKS_HIGH[self.bsize] as isize;+ let (mi_row, mi_col) = (self.r as isize, self.c as isize);+ let end4 = bh4.min(self.g.mi_rows as isize - mi_row).min(16);+ let mut delta_row = 0;+ let use_step16 = bh4 >= 16;+ let mut delta_col = delta_col;+ if delta_col.abs() > 1 {+ delta_row = 1 - (mi_row & 1);+ delta_col += mi_col & 1;+ }+ let mut i = 0;+ while i < end4 {+ let mv_row = mi_row + delta_row + i;+ let mv_col = mi_col + delta_col;+ if !self.inside(mv_row, mv_col) {+ break;+ }+ let k = self.mi.idx(mv_row as usize, mv_col as usize);+ let mut len = bh4.min(NUM_4X4_BLOCKS_HIGH[self.mi.mi_size[k] as usize] as isize);+ if delta_col.abs() > 1 {+ len = len.max(2);+ }+ if use_step16 {+ len = len.max(4);+ }+ self.add_ref_mv_candidate(mv_row as usize, mv_col as usize, len as u32 * 2);+ i += len;+ }+ }++ fn scan_point(&mut self, delta_row: isize, delta_col: isize) {+ let mv_row = self.r as isize + delta_row;+ let mv_col = self.c as isize + delta_col;+ if self.inside(mv_row, mv_col) && self.mi.written[self.mi.idx(mv_row as usize, mv_col as usize)] {+ self.add_ref_mv_candidate(mv_row as usize, mv_col as usize, 4);+ }+ }++ fn add_ref_mv_candidate(&mut self, mv_row: usize, mv_col: usize, weight: u32) {+ let i = self.mi.idx(mv_row, mv_col);+ if !self.mi.is_inter[i] {+ return;+ }+ let rf = self.mi.ref_frame[i];+ for cand_list in 0..2 {+ if rf[cand_list] == LAST_FRAME as i8 {+ self.search_stack(i, cand_list, weight);+ }+ }+ }++ fn search_stack(&mut self, i: usize, cand_list: usize, weight: u32) {+ debug_assert_eq!(cand_list, 0);+ let cand_mode = self.mi.y_mode[i] as usize;+ // gm_type is IDENTITY: the stored motion vector is used+ let mut cand = self.mi.mv[i];+ lower_mv_precision(&mut cand);+ if cand_mode == NEWMV {+ self.mvs.new_mv_count += 1;+ }+ self.mvs.found_match = true;+ let n = self.mvs.num_mv_found;+ if let Some(idx) = (0..n).find(|&k| self.mvs.ref_stack_mv[k] == cand) {+ self.mvs.weight_stack[idx] += weight;+ } else if n < MAX_REF_MV_STACK_SIZE {+ self.mvs.ref_stack_mv[n] = cand;+ self.mvs.weight_stack[n] = weight;+ self.mvs.num_mv_found += 1;+ }+ }++ fn sort_stack(&mut self, start: usize, end: usize) {+ let mut end = end;+ let s = &mut self.mvs;+ while end > start {+ let mut new_end = start;+ for idx in start + 1..end {+ if s.weight_stack[idx - 1] < s.weight_stack[idx] {+ s.weight_stack.swap(idx - 1, idx);+ s.ref_stack_mv.swap(idx - 1, idx);+ new_end = idx;+ }+ }+ end = new_end;+ }+ }++ fn extra_search(&mut self) {+ let (mi_row, mi_col) = (self.r as isize, self.c as isize);+ let n4 = NUM_4X4_BLOCKS_WIDE[self.bsize] as isize;+ let w4 = n4.min(16).min(self.g.mi_cols as isize - mi_col);+ let h4 = n4.min(16).min(self.g.mi_rows as isize - mi_row);+ let num4x4 = w4.min(h4);+ for pass in 0..2 {+ let mut idx = 0;+ while idx < num4x4 && self.mvs.num_mv_found < 2 {+ let (mv_row, mv_col) = if pass == 0 { (mi_row - 1, mi_col + idx) } else { (mi_row + idx, mi_col - 1) };+ if !self.inside(mv_row, mv_col) {+ break;+ }+ let k = self.mi.idx(mv_row as usize, mv_col as usize);+ // add_extra_mv_candidate( ) (no sign bias: a single forward reference)+ let rfs = self.mi.ref_frame[k];+ for &cand_ref in rfs.iter().take(2) {+ if cand_ref > INTRA_FRAME as i8 {+ let cand = self.mi.mv[k];+ let n = self.mvs.num_mv_found;+ if !(0..n).any(|j| self.mvs.ref_stack_mv[j] == cand) {+ self.mvs.ref_stack_mv[n] = cand;+ self.mvs.weight_stack[n] = 2;+ self.mvs.num_mv_found += 1;+ }+ }+ }+ let sz = self.mi.mi_size[k] as usize;+ idx += if pass == 0 { NUM_4X4_BLOCKS_WIDE[sz] } else { NUM_4X4_BLOCKS_HIGH[sz] } as isize;+ }+ }+ for idx in self.mvs.num_mv_found..2 {+ self.mvs.ref_stack_mv[idx] = [0, 0]; // GlobalMvs[ 0 ]+ }+ }++ fn context_and_clamping(&mut self, num_new: usize) {+ let bs = block_size_px(self.bsize) as i32;+ let n = self.mvs.num_mv_found;+ for idx in 0..n {+ let mut z = 0;+ if idx + 1 < n {+ let w0 = self.mvs.weight_stack[idx];+ let w1 = self.mvs.weight_stack[idx + 1];+ if w0 >= REF_CAT_LEVEL as u32 {+ if w1 < REF_CAT_LEVEL as u32 {+ z = 1;+ }+ } else {+ z = 2;+ }+ }+ self.mvs.drl_ctx_stack[idx] = z;+ }+ let n4 = NUM_4X4_BLOCKS_WIDE[self.bsize] as i32;+ for idx in 0..n {+ let mut mv = self.mvs.ref_stack_mv[idx];+ let border_r = MV_BORDER as i32 + bs * 8;+ let to_top = -((self.r as i32 * 4) * 8);+ let to_bottom = ((self.g.mi_rows as i32 - n4 - self.r as i32) * 4) * 8;+ mv[0] = mv[0].clamp(to_top - border_r, to_bottom + border_r);+ let to_left = -((self.c as i32 * 4) * 8);+ let to_right = ((self.g.mi_cols as i32 - n4 - self.c as i32) * 4) * 8;+ mv[1] = mv[1].clamp(to_left - border_r, to_right + border_r);+ self.mvs.ref_stack_mv[idx] = mv;+ }+ let (close, total) = (self.mvs.close_matches, self.mvs.total_matches);+ if close == 0 {+ self.mvs.new_mv_context = total.min(1);+ self.mvs.ref_mv_context = total;+ } else if close == 1 {+ self.mvs.new_mv_context = 3 - num_new.min(1);+ self.mvs.ref_mv_context = 2 + total;+ } else {+ self.mvs.new_mv_context = 5 - num_new.min(1);+ self.mvs.ref_mv_context = 5;+ }+ }+}++/// lower_mv_precision( ) with allow_high_precision_mv = 0 and force_integer_mv = 0.+fn lower_mv_precision(mv: &mut MvP) {+ for c in mv.iter_mut() {+ if *c & 1 != 0 {+ *c += if *c > 0 { -1 } else { 1 };+ }+ }+}++fn coeff_base_eob_ctx(c: usize, bwl: usize, height: usize) -> usize {+ if c == 0 {+ 0+ } else if c <= (height << bwl) / 8 {+ 1+ } else if c <= (height << bwl) / 4 {+ 2+ } else {+ 3+ }+}++/// Context of coeff_base for TX_CLASS_2D (8.3.2).+fn coeff_base_ctx(quant: &[i32], tx_sz: usize, bwl: usize, width: usize, height: usize, pos: usize) -> usize {+ let row = pos >> bwl;+ let col = pos - (row << bwl);+ let mut mag = 0i32;+ for off in SIG_REF_DIFF_OFFSET[TX_CLASS_2D].iter() {+ let ref_row = row + off[0] as usize;+ let ref_col = col + off[1] as usize;+ if ref_row < height && ref_col < width {+ mag += quant[(ref_row << bwl) + ref_col].abs().min(3);+ }+ }+ let ctx = ((mag + 1) >> 1).min(4) as usize;+ if row == 0 && col == 0 {+ return 0;+ }+ ctx + COEFF_BASE_CTX_OFFSET[tx_sz][row.min(4)][col.min(4)] as usize+}++/// Context of coeff_br for TX_CLASS_2D.+fn coeff_br_ctx(quant: &[i32], bwl: usize, txh: usize, pos: usize) -> usize {+ let txw = 1usize << bwl;+ let row = pos >> bwl;+ let col = pos - (row << bwl);+ let mut mag = 0i32;+ for off in MAG_REF_OFFSET_WITH_TX_CLASS[TX_CLASS_2D].iter() {+ let ref_row = row + off[0] as usize;+ let ref_col = col + off[1] as usize;+ if ref_row < txh && ref_col < txw {+ mag += quant[ref_row * txw + ref_col].min((COEFF_BASE_RANGE + NUM_BASE_LEVELS + 1) as i32);+ }+ }+ let mag = ((mag + 1) >> 1).min(6) as usize;+ if pos == 0 {+ mag+ } else if row < 2 && col < 2 {+ mag + 7+ } else {+ mag + 14+ }+}diff --git a/crates/av1enc/tests/common/mod.rs b/crates/av1enc/tests/common/mod.rsnew file mode 100644index 000000000..9f191cd0d--- /dev/null+++ b/crates/av1enc/tests/common/mod.rs@@ -0,0 +1,114 @@+#![allow(dead_code)]++use effectcraft_av1enc::{Encoder, EncoderConfig, Frame, Packet};++/// A synthetic 4:2:0 picture.+pub struct Pic {+ pub w: usize,+ pub h: usize,+ pub y: Vec<u16>,+ pub u: Vec<u16>,+ pub v: Vec<u16>,+}++impl Pic {+ pub fn frame(&self) -> Frame<'_> {+ Frame { y: &self.y, u: &self.u, v: &self.v, y_stride: self.w, uv_stride: self.w.div_ceil(2) }+ }+}++/// Smooth gradients with a soft blob, moved by (dx, dy) samples per frame index `t`, plus a+/// textured rectangle that moves differently.+pub fn synth(w: usize, h: usize, bd: u32, t: usize) -> Pic {+ let max = ((1u32 << bd) - 1) as f64;+ let (cw, ch) = (w.div_ceil(2), h.div_ceil(2));+ let sample = |x: f64, y: f64| -> f64 {+ let fx = x - 2.0 * t as f64;+ let fy = y - 1.0 * t as f64;+ let base = 0.25 + 0.5 * (fx / 97.0).sin() * (fy / 61.0).cos() * 0.5 + 0.2 * ((fx + fy) / (w + h) as f64);+ let d = ((fx - w as f64 / 2.0).powi(2) + (fy - h as f64 / 2.0).powi(2)).sqrt();+ let blob = 0.3 * (-d * d / (2.0 * (w.min(h) as f64 / 5.0).powi(2))).exp();+ // a checkered patch moving right by 4 per frame+ let px = x - 4.0 * t as f64 - w as f64 / 4.0;+ let py = y - h as f64 / 3.0;+ let patch =+ if (0.0..24.0).contains(&px) && (0.0..16.0).contains(&py) { if ((px as i32 / 4) + (py as i32 / 4)) % 2 == 0 { 0.25 } else { -0.25 } } else { 0.0 };+ (base + blob + patch).clamp(0.0, 1.0)+ };+ let mut y = vec![0u16; w * h];+ for j in 0..h {+ for i in 0..w {+ y[j * w + i] = (sample(i as f64, j as f64) * max).round() as u16;+ }+ }+ let mut u = vec![0u16; cw * ch];+ let mut v = vec![0u16; cw * ch];+ for j in 0..ch {+ for i in 0..cw {+ let s = sample(2.0 * i as f64, 2.0 * j as f64);+ u[j * cw + i] = ((0.5 + 0.3 * (s - 0.5) + 0.1 * (i as f64 / 13.0).sin()) * max).round() as u16;+ v[j * cw + i] = ((0.5 - 0.25 * (s - 0.5) + 0.1 * (j as f64 / 7.0).cos()) * max).round() as u16;+ }+ }+ Pic { w, h, y, u, v }+}++/// Deterministic noise picture (hard case for prediction).+pub fn noise(w: usize, h: usize, bd: u32, seed: u32) -> Pic {+ let mut s = seed.wrapping_mul(2_654_435_761).max(1);+ let mut rnd = || {+ s ^= s << 13;+ s ^= s >> 17;+ s ^= s << 5;+ s+ };+ let mask = (1u32 << bd) - 1;+ let (cw, ch) = (w.div_ceil(2), h.div_ceil(2));+ Pic {+ w,+ h,+ y: (0..w * h).map(|_| (rnd() & mask) as u16).collect(),+ u: (0..cw * ch).map(|_| (rnd() & mask) as u16).collect(),+ v: (0..cw * ch).map(|_| (rnd() & mask) as u16).collect(),+ }+}++pub fn psnr(a: &[u16], b: &[u16], bd: u32) -> f64 {+ let max = ((1u32 << bd) - 1) as f64;+ let mse = a.iter().zip(b).map(|(&x, &y)| (x as f64 - y as f64).powi(2)).sum::<f64>() / a.len() as f64;+ if mse == 0.0 { 99.0 } else { 10.0 * (max * max / mse).log10() }+}++pub struct Run {+ pub packets: Vec<Packet>,+ pub recons: Vec<[Vec<u16>; 3]>,+ pub psnr_y: Vec<f64>,+}++/// Encodes `pics`, checks every packet decodes (filmcraft-av1) to exactly the encoder's+/// reconstruction and returns packets, reconstructions and luma PSNRs.+pub fn encode_and_check(cfg: EncoderConfig, pics: &[Pic]) -> Run {+ let bd = cfg.bit_depth as u32;+ let mut enc = Encoder::new(cfg).expect("config");+ let mut dec = filmcraft_av1::Decoder::new();+ let mut run = Run { packets: Vec::new(), recons: Vec::new(), psnr_y: Vec::new() };+ for (i, p) in pics.iter().enumerate() {+ let pkt = enc.encode(&p.frame());+ let rec = enc.last_reconstruction().expect("reconstruction");+ let out = dec.decode(&pkt.data).unwrap_or_else(|e| panic!("frame {i}: decode error {e:?}"));+ assert_eq!(out.len(), 1, "frame {i}: one picture per packet");+ let d = &out[0];+ assert_eq!((d.width as usize, d.height as usize), (p.w, p.h));+ for pl in 0..3 {+ if d.planes[pl] != rec[pl] {+ let first = d.planes[pl].iter().zip(&rec[pl]).position(|(a, b)| a != b).unwrap_or(usize::MAX);+ let pw = if pl == 0 { p.w } else { p.w.div_ceil(2) };+ panic!("frame {i} plane {pl}: decoder output differs from the reconstruction (first at x {} y {})", first % pw, first / pw);+ }+ }+ run.psnr_y.push(psnr(&p.y, &rec[0], bd));+ run.recons.push(rec);+ run.packets.push(pkt);+ }+ run+}diff --git a/crates/av1enc/tests/ffmpeg_oracle.rs b/crates/av1enc/tests/ffmpeg_oracle.rsnew file mode 100644index 000000000..231ed7bcd--- /dev/null+++ b/crates/av1enc/tests/ffmpeg_oracle.rs@@ -0,0 +1,138 @@+//! External oracle: streams decoded by ffmpeg's libdav1d must equal the encoder's reconstruction+//! bit for bit. Skipped when `ffmpeg` (with libdav1d) is not installed. ffmpeg's native `av1`+//! decoder only drives hardware decoders, so it is not used.++mod common;++use std::process::Command;++use common::*;+use effectcraft_av1enc::{Encoder, EncoderConfig, Packet, RateControl};++fn have_libdav1d() -> bool {+ let Ok(out) = Command::new("ffmpeg").args(["-hide_banner", "-decoders"]).output() else {+ eprintln!("ffmpeg not found: skipping the external oracle");+ return false;+ };+ let ok = out.status.success() && String::from_utf8_lossy(&out.stdout).contains("libdav1d");+ if !ok {+ eprintln!("ffmpeg without libdav1d: skipping the external oracle");+ }+ ok+}++/// IVF container with one temporal unit (temporal delimiter + packet) per frame.+fn ivf(w: usize, h: usize, packets: &[Packet]) -> Vec<u8> {+ let mut v = Vec::new();+ v.extend_from_slice(b"DKIF");+ v.extend_from_slice(&0u16.to_le_bytes());+ v.extend_from_slice(&32u16.to_le_bytes());+ v.extend_from_slice(b"AV01");+ v.extend_from_slice(&(w as u16).to_le_bytes());+ v.extend_from_slice(&(h as u16).to_le_bytes());+ v.extend_from_slice(&30u32.to_le_bytes());+ v.extend_from_slice(&1u32.to_le_bytes());+ v.extend_from_slice(&(packets.len() as u32).to_le_bytes());+ v.extend_from_slice(&0u32.to_le_bytes());+ for (i, p) in packets.iter().enumerate() {+ v.extend_from_slice(&((p.data.len() + 2) as u32).to_le_bytes());+ v.extend_from_slice(&(i as u64).to_le_bytes());+ v.extend_from_slice(&[0x12, 0x00]); // OBU_TEMPORAL_DELIMITER+ v.extend_from_slice(&p.data);+ }+ v+}++/// Encodes `pics`, decodes the IVF stream with `ffmpeg -c:v libdav1d` and compares every+/// decoded frame with the encoder's reconstruction.+fn check(name: &str, cfg: EncoderConfig, pics: &[Pic]) {+ let (w, h, bd) = (cfg.width as usize, cfg.height as usize, cfg.bit_depth);+ let mut enc = Encoder::new(cfg).expect("config");+ let mut packets = Vec::new();+ let mut recons = Vec::new();+ for p in pics {+ packets.push(enc.encode(&p.frame()));+ recons.push(enc.last_reconstruction().expect("reconstruction"));+ }+ let dir = std::env::temp_dir().join(format!("effectcraft-av1enc-{}-{name}", std::process::id()));+ std::fs::create_dir_all(&dir).expect("temp dir");+ let input = dir.join("in.ivf");+ let output = dir.join("out.yuv");+ std::fs::write(&input, ivf(w, h, &packets)).expect("write ivf");+ let pix_fmt = if bd == 8 { "yuv420p" } else { "yuv420p10le" };+ let res = Command::new("ffmpeg")+ .args(["-v", "error", "-y", "-c:v", "libdav1d", "-i"])+ .arg(&input)+ .args(["-f", "rawvideo", "-pix_fmt", pix_fmt])+ .arg(&output)+ .output()+ .expect("run ffmpeg");+ let stderr = String::from_utf8_lossy(&res.stderr);+ assert!(res.status.success() && stderr.trim().is_empty(), "{name}: ffmpeg -c:v libdav1d failed: {stderr}");+ let raw = std::fs::read(&output).expect("decoded yuv");+ let _ = std::fs::remove_dir_all(&dir);+ let samples: Vec<u16> =+ if bd == 8 { raw.iter().map(|&b| b as u16).collect() } else { raw.chunks_exact(2).map(|c| u16::from_le_bytes([c[0], c[1]])).collect() };+ let (cw, ch) = (w.div_ceil(2), h.div_ceil(2));+ let fsize = w * h + 2 * cw * ch;+ assert_eq!(samples.len(), fsize * pics.len(), "{name}: decoded frame count");+ for (i, rec) in recons.iter().enumerate() {+ let f = &samples[i * fsize..(i + 1) * fsize];+ let planes = [&f[..w * h], &f[w * h..w * h + cw * ch], &f[w * h + cw * ch..]];+ for p in 0..3 {+ if planes[p] != &rec[p][..] {+ let pw = if p == 0 { w } else { cw };+ let k = planes[p].iter().zip(&rec[p]).position(|(a, b)| a != b).unwrap_or(0);+ panic!("{name}: frame {i} plane {p} differs from the reconstruction at ({}, {})", k % pw, k / pw);+ }+ }+ }+ eprintln!("{name}: libdav1d bit-exact over {} frames ({} bytes)", pics.len(), packets.iter().map(|p| p.data.len()).sum::<usize>());+}++fn cfg(w: usize, h: usize, bd: u8, rate: RateControl, keyint: u32) -> EncoderConfig {+ let mut c = EncoderConfig::new(w as u32, h as u32, 30, 1);+ c.bit_depth = bd;+ c.rate = rate;+ c.keyint = keyint;+ c+}++#[test]+fn libdav1d_decodes_bit_exact() {+ if !have_libdav1d() {+ return;+ }+ // a moving pattern with inter frames+ let pics: Vec<Pic> = (0..8).map(|t| synth(176, 144, 8, t)).collect();+ check("inter8", cfg(176, 144, 8, RateControl::ConstantQ(90), 5), &pics);+ // all-intra at a fine quantizer (large coefficients, Golomb codes)+ let pics: Vec<Pic> = (0..2).map(|t| noise(64, 48, 8, t as u32 + 3)).collect();+ check("noise-intra", cfg(64, 48, 8, RateControl::ConstantQ(4), 1), &pics);+ // odd sizes, 8 and 10 bit+ let pics: Vec<Pic> = (0..4).map(|t| synth(33, 17, 8, t)).collect();+ check("odd8", cfg(33, 17, 8, RateControl::ConstantQ(60), 3), &pics);+ let pics: Vec<Pic> = (0..5).map(|t| synth(101, 75, 10, t)).collect();+ check("odd10", cfg(101, 75, 10, RateControl::ConstantQ(110), 4), &pics);+ // a noisy moving picture, bitrate mode+ let pics: Vec<Pic> = (0..6).map(|t| noisy(128, 80, t)).collect();+ check("bitrate", cfg(128, 80, 8, RateControl::Bitrate { kbps: 300 }, 10), &pics);+}++#[test]+fn libdav1d_decodes_1080p() {+ if !have_libdav1d() {+ return;+ }+ let pics: Vec<Pic> = (0..3).map(|t| noisy(1920, 1080, t)).collect();+ check("1080p", cfg(1920, 1080, 8, RateControl::ConstantQ(120), 30), &pics);+}++fn noisy(w: usize, h: usize, t: usize) -> Pic {+ let mut p = synth(w, h, 8, t);+ let n = noise(w, h, 8, 7 + t as u32);+ for (a, b) in p.y.iter_mut().zip(&n.y) {+ *a = (*a as i32 + (*b as i32 - 128) / 16).clamp(0, 255) as u16;+ }+ p+}diff --git a/crates/av1enc/tests/roundtrip.rs b/crates/av1enc/tests/roundtrip.rsnew file mode 100644index 000000000..1e777e509--- /dev/null+++ b/crates/av1enc/tests/roundtrip.rs@@ -0,0 +1,92 @@+//! Round trips through FilmCraft's spec-derived AV1 decoder: decoded frames must equal the+//! encoder's own reconstruction bit for bit.++mod common;++use common::*;+use effectcraft_av1enc::{EncoderConfig, RateControl};++fn cfg(w: u32, h: u32, bd: u8, q: u8, keyint: u32) -> EncoderConfig {+ let mut c = EncoderConfig::new(w, h, 30, 1);+ c.bit_depth = bd;+ c.rate = RateControl::ConstantQ(q);+ c.keyint = keyint;+ c+}++#[test]+fn intra_8bit_smooth_high_quality() {+ let pics = vec![synth(128, 96, 8, 0)];+ let run = encode_and_check(cfg(128, 96, 8, 40, 1), &pics);+ eprintln!("intra 8-bit q40: {} bytes, PSNR-Y {:.2} dB", run.packets[0].data.len(), run.psnr_y[0]);+ assert!(run.packets[0].keyframe);+ assert!(run.psnr_y[0] > 35.0, "PSNR {:.2}", run.psnr_y[0]);+}++#[test]+fn odd_sizes_all_intra() {+ for (w, h) in [(33, 17), (1, 1), (7, 9), (65, 63), (100, 30)] {+ let pics: Vec<Pic> = (0..2).map(|t| synth(w, h, 8, t)).collect();+ let run = encode_and_check(cfg(w as u32, h as u32, 8, 90, 1), &pics);+ assert!(run.packets.iter().all(|p| p.keyframe));+ }+}++#[test]+fn noise_low_q_exact() {+ let pics = vec![noise(72, 40, 8, 1)];+ let run = encode_and_check(cfg(72, 40, 8, 1, 1), &pics);+ assert!(run.psnr_y[0] > 40.0, "PSNR {:.2}", run.psnr_y[0]);+}++#[test]+fn inter_moving_pattern() {+ let pics: Vec<Pic> = (0..6).map(|t| synth(160, 112, 8, t)).collect();+ let run = encode_and_check(cfg(160, 112, 8, 80, 30), &pics);+ let sizes: Vec<usize> = run.packets.iter().map(|p| p.data.len()).collect();+ eprintln!("inter sizes {sizes:?} psnr {:?}", run.psnr_y);+ assert!(run.packets[0].keyframe && run.packets[1..].iter().all(|p| !p.keyframe));+ let inter_avg = sizes[1..].iter().sum::<usize>() as f64 / (sizes.len() - 1) as f64;+ assert!(inter_avg < sizes[0] as f64 * 0.5, "inter frames {inter_avg} vs key {}", sizes[0]);+ assert!(run.psnr_y.iter().all(|&p| p > 33.0));+}++#[test]+fn ten_bit_inter() {+ let pics: Vec<Pic> = (0..4).map(|t| synth(96, 72, 10, t)).collect();+ let run = encode_and_check(cfg(96, 72, 10, 60, 3), &pics);+ eprintln!("10-bit psnr {:?}", run.psnr_y);+ assert!(run.psnr_y.iter().all(|&p| p > 35.0));+}++#[test]+fn odd_size_inter() {+ let pics: Vec<Pic> = (0..5).map(|t| synth(57, 43, 8, t)).collect();+ encode_and_check(cfg(57, 43, 8, 120, 10), &pics);+ let pics: Vec<Pic> = (0..3).map(|t| synth(33, 17, 10, t)).collect();+ encode_and_check(cfg(33, 17, 10, 120, 10), &pics);+}++#[test]+fn bitrate_mode_tracks_target() {+ let n = 12;+ let pics: Vec<Pic> = (0..n).map(|t| synth(192, 128, 8, t)).collect();+ let mut c = cfg(192, 128, 8, 0, 12);+ c.rate = RateControl::Bitrate { kbps: 400 };+ let run = encode_and_check(c, &pics);+ let bits: usize = run.packets.iter().map(|p| p.data.len() * 8).sum();+ let kbps = bits as f64 / (n as f64 / 30.0) / 1000.0;+ eprintln!("bitrate mode: {kbps:.1} kbit/s for a 400 kbit/s target, psnr {:?}", run.psnr_y);+ assert!(kbps > 400.0 / 2.0 && kbps < 400.0 * 2.0, "{kbps} kbit/s");+}++#[test]+fn headers() {+ let enc = effectcraft_av1enc::Encoder::new(cfg(1920, 1080, 10, 100, 60)).unwrap();+ let c = enc.av1c();+ assert_eq!(c[0], 0x81);+ assert_eq!(c[1] & 0x1f, 8, "1080p30 is level 4.0");+ assert_eq!(c[2], 0b0100_1100);+ assert_eq!(&c[4..], &enc.sequence_header_obu()[..]);+ assert_eq!(enc.sequence_header_obu()[0], 0x0A);+}