use super::block::BlockChoice;
use super::cdf::Cdfs;
use super::coeffs::Scans;
use super::context::{Contexts, Decoded};
use super::delta_q;
use super::entropy::{SymbolEncoder, SymbolSink};
use super::plane::Plane;
use super::quant::Quantizer;
use super::syntax::Square;
use super::transform::Bases;
const PARTITION_NONE: usize = 0;
const PARTITION_SPLIT: usize = 3;
#[derive(Debug)]
pub(crate) struct Frame {
pub(crate) source: Vec<Plane>,
pub(crate) recon: Vec<Plane>,
pub(crate) ss: (u32, u32),
pub(crate) mi: (usize, usize),
pub(crate) quantizer: Quantizer,
pub(crate) delta_q_res: Option<u32>,
pub(crate) bases: Bases,
pub(crate) scans: Scans,
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct TileBounds {
pub(crate) rows: (usize, usize),
pub(crate) cols: (usize, usize),
}
#[derive(Clone, Debug)]
pub(crate) enum Partition {
Whole(BlockChoice),
Split(Box<[Partition; 4]>),
Outside,
}
fn unsplit_probability(cdf: &[u16; 10], splits: [usize; 6]) -> u16 {
let at = |i: usize| if i == 9 { 32768 } else { u32::from(cdf[i]) };
let psum: u32 = splits.into_iter().map(|p| at(p) - at(p - 1)).sum();
(32768 - psum) as u16
}
#[derive(Debug)]
pub(crate) struct TileCoder<'a> {
pub(crate) frame: &'a mut Frame,
pub(crate) contexts: &'a mut Contexts,
pub(crate) cdfs: Cdfs,
pub(crate) bounds: TileBounds,
pub(crate) decoded: Decoded,
pub(crate) quantizer: Quantizer,
pub(crate) current_qindex: u8,
pub(crate) pending_delta: Option<(u8, i32)>,
}
pub(crate) fn encode_tile(
frame: &mut Frame,
contexts: &mut Contexts,
bounds: TileBounds,
) -> Vec<u8> {
let base = frame.quantizer;
let cdfs = Cdfs::new(base.qindex());
let decoded = Decoded::new(frame.recon.len(), frame.ss.0);
let mut tile = TileCoder {
frame,
contexts,
cdfs,
bounds,
decoded,
quantizer: base,
current_qindex: base.qindex(),
pending_delta: None,
};
let columns = tile.contexts.above.blocks.len();
tile.contexts.above.clear_levels(&(0..columns));
let mut encoder = SymbolEncoder::new();
let superblock = Square::S64.units();
for r in (bounds.rows.0..bounds.rows.1).step_by(superblock) {
tile.contexts.left.clear_levels(&(r..r + superblock));
for c in (bounds.cols.0..bounds.cols.1).step_by(superblock) {
let delta = tile.frame.delta_q_res.map(|res| {
let target = delta_q::target(&tile.frame.source[0], (c * 4, r * 4), base.qindex());
delta_q::adjust(res, tile.current_qindex, target)
});
tile.quantizer = delta.map_or(base, |(qindex, _)| base.at(qindex));
let end = (bounds.rows.1, bounds.cols.1);
tile.decoded.clear((r, c), end, tile.frame.ss.0);
let saved = tile.contexts.save(r, c, superblock);
let (_, tree) = tile.search_partition((r, c), Square::S64);
tile.contexts.restore(&saved);
tile.decoded.clear((r, c), end, tile.frame.ss.0);
tile.pending_delta = delta;
tile.code_partition(&mut encoder, (r, c), Square::S64, &tree);
}
}
encoder.finish()
}
impl TileCoder<'_> {
pub(crate) fn above_available(&self, r: usize) -> bool {
r > self.bounds.rows.0
}
pub(crate) fn left_available(&self, c: usize) -> bool {
c > self.bounds.cols.0
}
pub(crate) fn has_rows_cols(&self, (r, c): (usize, usize), size: Square) -> (bool, bool) {
let half = size.units() / 2;
(r + half < self.frame.mi.1, c + half < self.frame.mi.0)
}
pub(crate) fn write_partition<S: SymbolSink>(
&mut self,
sink: &mut S,
(r, c): (usize, usize),
size: Square,
split: bool,
) {
let (has_rows, has_cols) = self.has_rows_cols((r, c), size);
let bsl = size.units_log2();
let above =
self.above_available(r) && u32::from(self.contexts.above.blocks[c].size_log2) < bsl;
let left =
self.left_available(c) && u32::from(self.contexts.left.blocks[r].size_log2) < bsl;
let ctx = usize::from(left) * 2 + usize::from(above);
let symbol = if split {
PARTITION_SPLIT
} else {
PARTITION_NONE
};
if size == Square::S8 {
sink.symbol(&mut self.cdfs.partition_8[ctx], symbol);
return;
}
let cdf = &mut self.cdfs.partition[bsl as usize - 2][ctx];
match (has_rows, has_cols) {
(true, true) => sink.symbol(cdf, symbol),
(false, true) => sink.bool_with(split, unsplit_probability(cdf, [2, 3, 4, 6, 7, 9])),
(true, false) => sink.bool_with(split, unsplit_probability(cdf, [1, 3, 4, 5, 6, 8])),
(false, false) => {}
}
}
fn code_partition<S: SymbolSink>(
&mut self,
sink: &mut S,
(r, c): (usize, usize),
size: Square,
tree: &Partition,
) {
match tree {
Partition::Outside => {}
Partition::Whole(choice) => {
self.write_partition(sink, (r, c), size, false);
self.code_block(sink, (r, c), size, choice);
}
Partition::Split(quarters) => {
self.write_partition(sink, (r, c), size, true);
let half = size.units() / 2;
for (i, quarter) in quarters.iter().enumerate() {
let at = (r + half * (i / 2), c + half * (i % 2));
self.code_partition(sink, at, size.half(), quarter);
}
}
}
}
}