diff options
author | metamuffin <metamuffin@disroot.org> | 2023-11-15 15:14:12 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-11-15 15:14:12 +0100 |
commit | 7df3589b72ba1c0b4eb8998c9d83ad033b6ddb57 (patch) | |
tree | 25ea5b2dea3b6352406018b52aeaad1aba415076 /tweak/src | |
parent | bcd52f79931a4d37883618ed142f2260cd8c8b39 (diff) | |
download | video-codec-experiments-7df3589b72ba1c0b4eb8998c9d83ad033b6ddb57.tar video-codec-experiments-7df3589b72ba1c0b4eb8998c9d83ad033b6ddb57.tar.bz2 video-codec-experiments-7df3589b72ba1c0b4eb8998c9d83ad033b6ddb57.tar.zst |
a
Diffstat (limited to 'tweak/src')
-rw-r--r-- | tweak/src/main.rs | 163 |
1 files changed, 123 insertions, 40 deletions
diff --git a/tweak/src/main.rs b/tweak/src/main.rs index a46d561..b2144a3 100644 --- a/tweak/src/main.rs +++ b/tweak/src/main.rs @@ -1,6 +1,7 @@ +use rayon::join; use std::{ io::{stdin, stdout, Read, Write}, - ops::Index, + time::Instant, }; const WIDTH: usize = 1920; @@ -8,42 +9,115 @@ const HEIGHT: usize = 1080; fn main() { let mut oframe = Frame::new(); + let mut dframe = Frame::new(); + let mut out = Vec::<u8>::new(); + let do_debug = false; loop { + let timer = Instant::now(); let iframe = Frame::read(stdin()); - // let diff = oframe.average_area_diff(&iframe, Area::root()); - // eprintln!("{diff:?}"); + let t = encode(&oframe, &iframe, Area::root()); + decode(&mut oframe, Area::root(), &t); - oframe.apply_area_diff(Area::root(), diff); + write(&mut out, &t); - oframe.write(stdout()); - // oframe = iframe; + if do_debug { + dframe.0.copy_from_slice(&oframe.0); + debug(&mut dframe, Area::root(), &t); + dframe.write(stdout()); + } else { + oframe.write(stdout()); + } + + eprintln!( + "compression={:.01} time={:?}", + (WIDTH * HEIGHT * 3) as f64 / out.len() as f64, + timer.elapsed() + ); + out.clear(); } } -pub fn decode(f: &mut Frame, area: Area, tree: DiffTree) { - oframe.apply_area_diff(Area::root(), diff); - -} +pub fn write(w: &mut Vec<u8>, t: &DiffTree) { + match t { + DiffTree::Split([a, b]) => { + w.push(0); + write(w, a); + write(w, b); + } + DiffTree::Diff(d) => w.extend([1, d.0[0] as u8, d.0[1] as u8, d.0[2] as u8]), + } +} -pub enum DiffTree { - Split([Box<DiffTree>; 2]), - Diff(PixelDiff), +pub fn decode(f: &mut Frame, area: Area, tree: &DiffTree) { + match tree { + DiffTree::Split([ta, tb]) => { + let (aa, ab) = area.split(); + decode(f, aa, ta); + decode(f, ab, tb); + } + DiffTree::Diff(diff) => { + f.apply_area_diff(area, *diff); + } + } +} +pub fn debug(f: &mut Frame, area: Area, tree: &DiffTree) { + match tree { + DiffTree::Split([ta, tb]) => { + let (aa, ab) = area.split(); + decode(f, aa, ta); + decode(f, ab, tb); + } + DiffTree::Diff(_diff) => { + let Area { x1, y1, x2, y2 } = area; + for x in x1..x2 { + for y in y1..y2 { + let o = (x + y * WIDTH) * 3; + f.0[o + 0] = 255; + f.0[o + 1] = 0; + f.0[o + 2] = 255; + } + } + } + } } -pub fn stuff(a: &Frame, b: &Frame, area: Area) -> DiffTree { +pub fn encode(a: &Frame, b: &Frame, area: Area) -> DiffTree { if area.area() == 1 { DiffTree::Diff(Frame::diff_pixel(a, b, area.x1, area.y1)) } else { let (aa, ba) = area.split(); - let (at, bt) = (stuff(a, b, aa), stuff(a, b, ba)); + let (at, bt) = join(|| encode(a, b, aa), || encode(a, b, ba)); + + match (&at, &bt) { + (DiffTree::Diff(ad), DiffTree::Diff(bd)) => { + let d_r = ad.0[0].abs_diff(bd.0[0]); + let d_g = ad.0[1].abs_diff(bd.0[1]); + let d_b = ad.0[2].abs_diff(bd.0[2]); + + let visdiff = (d_r as usize + d_g as usize + d_b as usize) * aa.area(); + if visdiff < 100 { + return DiffTree::Diff(PixelDiff([ + ((ad.0[0] as i16 + bd.0[0] as i16) / 2) as i8, + ((ad.0[1] as i16 + bd.0[1] as i16) / 2) as i8, + ((ad.0[2] as i16 + bd.0[2] as i16) / 2) as i8, + ])); + } + } + _ => (), + } DiffTree::Split([Box::new(at), Box::new(bt)]) } } +pub enum DiffTree { + Split([Box<DiffTree>; 2]), + Diff(PixelDiff), +} + #[derive(Debug)] pub struct Frame(Vec<u8>); -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub struct PixelDiff([i8; 3]); #[derive(Debug, Clone, Copy)] pub struct Area { @@ -53,6 +127,15 @@ pub struct Area { y2: usize, } +#[inline] +fn diff_clamp(x: u8, y: u8) -> i8 { + if y >= x { + (y - x).min(127) as i8 + } else { + -((x - y).min(128) as i8) + } +} + impl Frame { pub fn new() -> Self { Self(vec![0u8; WIDTH * HEIGHT * 3]) @@ -69,9 +152,9 @@ impl Frame { pub fn diff_pixel(a: &Frame, b: &Frame, x: usize, y: usize) -> PixelDiff { let o = (x + y * WIDTH) * 3; PixelDiff([ - (a.0[o + 0] as i16 - b.0[o + 0] as i16).clamp(i8::MIN as i16, i8::MAX as i16) as i8, - (a.0[o + 1] as i16 - b.0[o + 1] as i16).clamp(i8::MIN as i16, i8::MAX as i16) as i8, - (a.0[o + 2] as i16 - b.0[o + 2] as i16).clamp(i8::MIN as i16, i8::MAX as i16) as i8, + diff_clamp(a.0[o + 0], b.0[o + 0]), + diff_clamp(a.0[o + 1], b.0[o + 1]), + diff_clamp(a.0[o + 2], b.0[o + 2]), ]) } @@ -85,27 +168,27 @@ impl Frame { } } } - pub fn average_area_diff( - &self, - other: &Frame, - area @ Area { x1, y1, x2, y2 }: Area, - ) -> PixelDiff { - let (mut r, mut g, mut b) = (0i32, 0i32, 0i32); - for x in x1..x2 { - for y in y1..y2 { - let o = (x + y * WIDTH) * 3; - r += other.0[o + 0] as i32 - self.0[o + 0] as i32; - g += other.0[o + 1] as i32 - self.0[o + 1] as i32; - b += other.0[o + 2] as i32 - self.0[o + 2] as i32; - } - } - let a = area.area() as i32; - PixelDiff([ - (r / a).clamp(i8::MIN as i32, i8::MAX as i32) as i8, - (g / a).clamp(i8::MIN as i32, i8::MAX as i32) as i8, - (b / a).clamp(i8::MIN as i32, i8::MAX as i32) as i8, - ]) - } + // pub fn average_area_diff( + // &self, + // other: &Frame, + // area @ Area { x1, y1, x2, y2 }: Area, + // ) -> PixelDiff { + // let (mut r, mut g, mut b) = (0i32, 0i32, 0i32); + // for x in x1..x2 { + // for y in y1..y2 { + // let o = (x + y * WIDTH) * 3; + // r += other.0[o + 0] as i32 - self.0[o + 0] as i32; + // g += other.0[o + 1] as i32 - self.0[o + 1] as i32; + // b += other.0[o + 2] as i32 - self.0[o + 2] as i32; + // } + // } + // let a = area.area() as i32; + // PixelDiff([ + // (r / a).clamp(i8::MIN as i32, i8::MAX as i32) as i8, + // (g / a).clamp(i8::MIN as i32, i8::MAX as i32) as i8, + // (b / a).clamp(i8::MIN as i32, i8::MAX as i32) as i8, + // ]) + // } } impl Area { pub fn area(&self) -> usize { |