use super::header::write_modular_transforms;
use super::lz77::ConstantTail;
use super::ma_learning::leaf_offsets;
use super::ma_sampling::MaRefPlane;
use super::ma_streams::{LZ_DEEP_MATCHER_MAX_RATIO_PALETTE, LZ_DEEP_MATCHER_MAX_RATIO_RCT};
use super::palette::write_palette_transform;
use super::tree::{TREE_CTX_OFFSET, emit_learned_tree};
use super::{GroupLayout, palette};
use crate::bit_writer::BitWriter;
use crate::coder_scratch::LZ77_MAX_CONTEXTS;
use crate::encode_image::AlphaPlane;
use crate::entropy::{Token, pack_signed};
use crate::image::Image3Si;
use crate::ma_tree::{LearnedTree, MA_REF_CHANNELS, MaNode, MaSamples};
const MA_TARGET_SAMPLES: usize = 1 << 21;
const MA_FULL_SAMPLE_LIMIT: usize = 1 << 22;
const MA_FINAL_TARGET_SAMPLES: usize = 1 << 23;
const MA_LITE_TARGET_SAMPLES: usize = 1 << 19;
const MA_LITE_FULL_SAMPLE_LIMIT: usize = 1 << 19;
const MA_FINAL_FULL_SAMPLE_LIMIT: usize = 1 << 24;
pub(super) const MA_FULL_SAMPLING: (usize, usize) = (MA_TARGET_SAMPLES, MA_FULL_SAMPLE_LIMIT);
pub(super) const MA_LITE_SAMPLING: (usize, usize) =
(MA_LITE_TARGET_SAMPLES, MA_LITE_FULL_SAMPLE_LIMIT);
pub(super) const MA_DENSE_SAMPLING: (usize, usize) =
(MA_FINAL_TARGET_SAMPLES, MA_FINAL_FULL_SAMPLE_LIMIT);
pub(super) const DENSE_FINAL_MIN_BITS_PER_VALUE: f64 = 1.0;
pub(super) const MA_COARSE_TARGET_SAMPLES: usize = 1 << 16;
pub(super) const MA_COARSE_MAX_LEAVES: usize = 32;
pub(super) const MA_DEEPEN_MIN_SAVINGS: f64 = 0.02;
pub(crate) const MA_SPLIT_COST_BITS: f32 = 100.0;
pub(super) const MA_FINAL_SPLIT_COST_BITS: f32 = 50.0;
pub(super) const MA_MAX_LEAVES: usize = LZ77_MAX_CONTEXTS - 1;
pub(crate) const MA_MIN_NODE_SAMPLES: usize = 128;
pub(super) const MA_PRUNE_SPLIT_COST_BITS: f32 = 20.0;
pub(super) const MA_PRUNE_MIN_NODE_SAMPLES: usize = 16;
pub(super) const MA_PRUNE_LEAF_BITS: f32 = 40.0;
pub(super) const MA_SIDE_PREDS: usize = 4;
pub(super) const MA_WIDE_SIDE_PREDS: usize = 7;
pub(super) const MA_LITE_MAX_LEAVES: usize = 1024;
pub(super) const MA_LITE_CANDIDATES: usize = 16;
pub(super) struct CoarseLearn {
pub(super) samples: MaSamples,
pub(super) stride: usize,
pub(super) max_candidates: usize,
pub(super) leaf_offsets: bool,
pub(super) candidate: LearnedCandidate,
pub(super) deepen: bool,
}
pub(super) struct LearnedCandidate {
pub(super) tree: LearnedTree,
pub(super) tree_tokens: Vec<Token>,
pub(super) leaf_ctx: Vec<u32>,
pub(super) leaf_offset: Vec<i32>,
pub(super) num_ctx: u32,
pub(super) est_real: f64,
pub(super) flat_real: f64,
pub(super) estimated_savings: f64,
pub(super) constant_ctx: Option<u32>,
}
impl LearnedCandidate {
pub(super) fn with_constant_channel(&self, chan_id: u32, value: i32) -> LearnedCandidate {
assert!(
chan_id >= 1,
"a constant channel follows the color channels"
);
assert!(self.constant_ctx.is_none(), "one constant channel per tree");
let mut nodes = Vec::with_capacity(self.tree.nodes.len() + 2);
nodes.push(MaNode::Split {
prop: 0,
val: chan_id as i32 - 1,
gt: 1,
le: 2,
});
nodes.push(MaNode::Leaf {
pred: 0,
offset: value,
});
nodes.extend(self.tree.nodes.iter().map(|node| match *node {
MaNode::Split { prop, val, gt, le } => MaNode::Split {
prop,
val,
gt: gt + 2,
le: le + 2,
},
leaf @ MaNode::Leaf { .. } => leaf,
}));
let tree = LearnedTree {
nodes,
est_bits: self.tree.est_bits,
flat_bits: self.tree.flat_bits,
};
let (mut tree_tokens, leaf_ctx, num_ctx) = emit_learned_tree(&tree);
debug_assert_eq!(num_ctx, self.num_ctx + 1);
debug_assert_eq!(leaf_ctx[1], 0);
let mut leaf_offset = Vec::with_capacity(num_ctx as usize);
leaf_offset.push(value);
leaf_offset.extend_from_slice(&self.leaf_offset);
for (token, &offset) in tree_tokens
.iter_mut()
.filter(|token| token.context == TREE_CTX_OFFSET)
.zip(&leaf_offset)
{
token.value = pack_signed(offset);
}
LearnedCandidate {
tree,
tree_tokens,
leaf_ctx,
leaf_offset,
num_ctx,
est_real: self.est_real,
flat_real: self.flat_real,
estimated_savings: self.estimated_savings,
constant_ctx: Some(0),
}
}
pub(super) fn constant_tail(&self, w: usize, h: usize) -> ConstantTail {
ConstantTail {
context: self.constant_ctx.expect("constant channel leaf"),
count: w * h,
}
}
pub(super) fn set_leaf_offsets(&mut self, samples: &MaSamples, step: usize) {
self.leaf_offset = crate::ma_tree::with_ma_properties!(&samples.props, |rows| {
leaf_offsets(
&self.tree,
&self.leaf_ctx,
self.num_ctx,
rows.iter().zip(&samples.tok).step_by(step),
)
});
for (token, &offset) in self
.tree_tokens
.iter_mut()
.filter(|token| token.context == TREE_CTX_OFFSET)
.zip(&self.leaf_offset)
{
token.value = pack_signed(offset);
}
}
}
pub(super) struct MaChannel<'a> {
pub(super) w: usize,
pub(super) h: usize,
pub(super) meta: bool,
pub(super) pixels: MaPixels<'a>,
pub(super) constant: Option<i32>,
}
pub(super) enum MaPixels<'a> {
U8(&'a [u8]),
U16(&'a [u16]),
I32(&'a [i32]),
}
macro_rules! with_ma_pixels {
($channel:expr, |$pixels:ident| $body:expr) => {
match &$channel.pixels {
crate::lossless::ma_source::MaPixels::U8($pixels) => $body,
crate::lossless::ma_source::MaPixels::U16($pixels) => $body,
crate::lossless::ma_source::MaPixels::I32($pixels) => $body,
}
};
}
pub(crate) const MA_PHOTO_CANDIDATES: usize = 32;
pub(super) const MA_PALETTE_CANDIDATES: usize = 16;
#[derive(Clone, Copy)]
pub(super) enum MaTransform {
Rct(u32),
Palette { num_c: u32, nb_colors: u32 },
}
pub(super) struct MaSource<'a> {
pub(super) channels: Vec<MaChannel<'a>>,
pub(super) transform: MaTransform,
pub(super) group_channels: Vec<Option<MaGroupChannels<'a>>>,
}
pub(super) struct MaGroupChannels<'a> {
pub(super) channels: Vec<MaChannel<'a>>,
pub(super) nb_colors: u32,
pub(super) num_c: u32,
}
const MODULAR_NUM_QUANT_TABLES: usize = 17;
pub(crate) fn ac_stream_id(num_dc_groups: usize, group_index: usize) -> i32 {
(1 + 3 * num_dc_groups + MODULAR_NUM_QUANT_TABLES + group_index) as i32
}
pub(super) struct MaPlacement {
pub(super) channel: usize,
pub(super) chan_id: u32,
pub(super) global: bool,
}
impl MaSource<'_> {
pub(super) fn max_leaves(&self) -> usize {
MA_MAX_LEAVES - usize::from(self.channels.iter().any(|ch| ch.constant.is_some()))
}
pub(super) fn max_candidates(&self) -> usize {
match self.transform {
MaTransform::Palette { .. } => MA_PALETTE_CANDIDATES,
MaTransform::Rct(_) => MA_PHOTO_CANDIDATES,
}
}
pub(super) fn deep_lz_max_ratio(&self) -> f64 {
match self.transform {
MaTransform::Rct(_) if self.group_channels.iter().all(Option::is_none) => {
LZ_DEEP_MATCHER_MAX_RATIO_RCT
}
_ => LZ_DEEP_MATCHER_MAX_RATIO_PALETTE,
}
}
pub(super) fn write_transforms(&self, w: &mut BitWriter) {
match self.transform {
MaTransform::Rct(rct_type) => {
write_modular_transforms(self.channels.len(), rct_type, w)
}
MaTransform::Palette { num_c, nb_colors } => {
write_palette_transform(num_c, nb_colors, w)
}
}
}
pub(super) fn placement(&self, layout: GroupLayout, single_group: bool) -> Vec<MaPlacement> {
let gdim = layout.dim();
let mut global_id = 0u32;
let mut group_id = 0u32;
self.channels
.iter()
.enumerate()
.map(|(channel, ch)| {
let global = single_group || ch.meta || (ch.w <= gdim && ch.h <= gdim);
let chan_id = if single_group {
channel as u32
} else if global {
let id = global_id;
global_id += 1;
id
} else {
let id = group_id;
group_id += 1;
id
};
MaPlacement {
channel,
chan_id,
global,
}
})
.collect()
}
pub(super) fn total_values(&self) -> usize {
self.channels
.iter()
.filter(|c| c.constant.is_none())
.map(|c| c.w * c.h)
.sum()
}
pub(super) fn constant_placement<'p>(
&self,
placement: &'p [MaPlacement],
) -> Option<(&'p MaPlacement, i32)> {
let mut found: Option<(&MaPlacement, i32)> = None;
for p in placement {
if let Some(value) = self.channels[p.channel].constant {
assert!(found.is_none(), "one constant channel per source");
found = Some((p, value));
}
}
let (p, value) = found?;
assert!(
placement
.iter()
.filter(|q| q.global == p.global)
.all(|q| q.chan_id <= p.chan_id),
"the constant channel is the last of its stream"
);
Some((p, value))
}
pub(super) fn group_override(&self, group_index: usize) -> Option<&MaGroupChannels<'_>> {
self.group_channels
.get(group_index)
.and_then(Option::as_ref)
}
pub(super) fn channel_at(
&self,
group_override: Option<usize>,
channel: usize,
) -> &MaChannel<'_> {
match group_override {
Some(group_index) => {
&self
.group_override(group_index)
.expect("override group")
.channels[channel]
}
None => &self.channels[channel],
}
}
pub(super) fn ref_planes(
&self,
placement: &[MaPlacement],
p: &MaPlacement,
x0: usize,
y0: usize,
) -> Vec<MaRefPlane<'_>> {
let ch = &self.channels[p.channel];
placement[..p.channel]
.iter()
.rev()
.filter(|q| q.global == p.global)
.filter_map(|q| {
let other = &self.channels[q.channel];
match other.pixels {
crate::lossless::ma_source::MaPixels::I32(pixels)
if other.w == ch.w && other.h == ch.h =>
{
Some(MaRefPlane {
pixels,
stride: other.w,
x0,
y0,
})
}
_ => None,
}
})
.take(MA_REF_CHANNELS)
.collect()
}
}
pub(super) fn mixed_ma_source<'a>(
ycocg: &'a Image3Si,
alpha: Option<&'a AlphaPlane>,
xsize: usize,
palettes: &'a [Option<palette::LocalPaletteGroup>],
alpha_constant: Option<i32>,
) -> MaSource<'a> {
let mut source = rgb_ma_source(ycocg, alpha, xsize, 6, alpha_constant, 3);
source.group_channels = palettes
.iter()
.map(|palette| {
let palette = palette.as_ref()?;
let num_c = palette.palette.len() / palette.nb_colors;
Some(MaGroupChannels {
channels: vec![
MaChannel {
w: palette.nb_colors,
h: num_c,
meta: true,
pixels: crate::lossless::ma_source::MaPixels::I32(&palette.palette),
constant: None,
},
MaChannel {
w: palette.w,
h: palette.h,
meta: false,
pixels: crate::lossless::ma_source::MaPixels::U16(&palette.indices),
constant: None,
},
],
nb_colors: palette.nb_colors as u32,
num_c: num_c as u32,
})
})
.collect();
source
}
pub(super) fn rgb_ma_source<'a>(
linear: &'a Image3Si,
alpha: Option<&'a AlphaPlane>,
xsize: usize,
rct_type: u32,
alpha_constant: Option<i32>,
num_color: usize,
) -> MaSource<'a> {
let ysize = linear.ysize();
let mut channels: Vec<MaChannel<'a>> = (0..num_color)
.map(|c| {
let pd = linear.plane_data(c);
MaChannel {
w: xsize,
h: ysize,
meta: false,
pixels: crate::lossless::ma_source::MaPixels::I32(pd),
constant: None,
}
})
.collect();
if let Some(a) = alpha {
channels.push(MaChannel {
w: xsize,
h: ysize,
meta: false,
pixels: match a {
AlphaPlane::U8(pixels) => crate::lossless::ma_source::MaPixels::U8(pixels),
AlphaPlane::U16 { data, .. } => crate::lossless::ma_source::MaPixels::U16(data),
AlphaPlane::F32(pixels) => crate::lossless::ma_source::MaPixels::I32(pixels),
},
constant: alpha_constant,
});
}
MaSource {
channels,
transform: MaTransform::Rct(rct_type),
group_channels: Vec::new(),
}
}
pub(super) const PATCH_MIN_COVERAGE: f64 = 0.05;
pub(super) const PALETTE_LAYOUT_MARGIN: f64 = 1.04;
pub(super) const RANK_MIN_VALUES: usize = 1 << 19;
pub(super) const RGB_SINGLE_LAYOUT_MIN_VALUES: usize = 1 << 20;
pub(super) fn source_values_of(xsize: usize, ysize: usize, nb_chans: usize) -> usize {
xsize * ysize * nb_chans
}
pub(super) const PER_GROUP_MARGIN: f64 = 1.0;
pub(super) use with_ma_pixels;