use crate::{BLOCK_SIZE, Frame}; use framework::BitstreamFilter; use glam::{IVec2, ivec2}; pub struct Enc { res: IVec2, last: Frame, } impl BitstreamFilter for Enc { const INPUT_CODEC_ID: &str = "V_UNCOMPRESSED"; const OUTPUT_CODEC_ID: &str = "V_VCETEST2"; fn new(width: u32, height: u32) -> Self { let res = ivec2(width as i32, height as i32); Self { res, last: Frame::new(res), } } fn process_block(&mut self, a: Vec) -> Vec { let mut frame = Frame { data: a, res: self.res, }; let mut out = Vec::new(); let mut savblock = Vec::new(); let mut refblock = Vec::new(); for by in 0..frame.res.y / BLOCK_SIZE { for bx in 0..frame.res.x / BLOCK_SIZE { let boff = ivec2(bx * BLOCK_SIZE, by * BLOCK_SIZE); let mut best_d = Frame::compare_block(&frame, &self.last, boff, boff); let mut best_off = boff; for granularity in [8, 4, 2, 1] { for dir in [ ivec2(1, 0), ivec2(1, 1), ivec2(0, 1), ivec2(-1, 1), ivec2(-1, 0), ivec2(-1, -1), ivec2(0, -1), ivec2(1, -1), ] { let roff = boff + dir * granularity; if roff.x >= 0 && roff.y >= 0 && roff.x + BLOCK_SIZE < frame.res.x && roff.y + BLOCK_SIZE < frame.res.y { let d = Frame::compare_block(&frame, &self.last, boff, roff); if d < best_d { best_d = d; best_off = roff; } } } } savblock.clear(); frame.export_block(boff, &mut savblock); Frame::copy_block(&self.last, &mut frame, best_off, boff); refblock.clear(); frame.export_block(boff, &mut refblock); let reloff = best_off - boff; out.push((reloff.x + 127) as u8); out.push((reloff.y + 127) as u8); for i in 0..(BLOCK_SIZE * BLOCK_SIZE + BLOCK_SIZE * BLOCK_SIZE / 2) { let mut d = savblock[i as usize] - refblock[i as usize]; let neg = d > 127; if neg { d = (-(d as i8)) as u8; } d &= 0xff << (6 - d.leading_zeros()); // only keep 3 msb if neg { d = (-(d as i8)) as u8; } out.push(d) } frame.import_block_diff( boff, &out[out.len() - (BLOCK_SIZE * BLOCK_SIZE + BLOCK_SIZE * BLOCK_SIZE / 2) as usize..], ); } } self.last = frame.clone(); out } }