From 85d20c4f4cf3656fbf7c27b6b8bbf9536e3ae04d Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 7 Dec 2022 17:16:40 +0100 Subject: refactor, matrix math --- evc/src/bin/encode.rs | 6 +-- evc/src/block.rs | 8 ++-- evc/src/codec/encode.rs | 18 +++++---- evc/src/debug.rs | 4 +- evc/src/frame.rs | 46 +++++++++++++++------- evc/src/header.rs | 4 +- evc/src/helpers/matrix.rs | 30 +++++++++++++++ evc/src/helpers/mod.rs | 3 ++ evc/src/helpers/threading.rs | 26 +++++++++++++ evc/src/helpers/vector.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++ evc/src/lib.rs | 4 +- evc/src/pixel.rs | 8 ++++ evc/src/refsampler.rs | 14 +++++++ evc/src/ser.rs | 2 +- evc/src/threading.rs | 26 ------------- evc/src/vec2.rs | 82 --------------------------------------- evc/src/view.rs | 32 +++++++-------- 17 files changed, 247 insertions(+), 158 deletions(-) create mode 100644 evc/src/helpers/matrix.rs create mode 100644 evc/src/helpers/mod.rs create mode 100644 evc/src/helpers/threading.rs create mode 100644 evc/src/helpers/vector.rs create mode 100644 evc/src/refsampler.rs delete mode 100644 evc/src/threading.rs delete mode 100644 evc/src/vec2.rs (limited to 'evc/src') diff --git a/evc/src/bin/encode.rs b/evc/src/bin/encode.rs index 4857a80..611347f 100644 --- a/evc/src/bin/encode.rs +++ b/evc/src/bin/encode.rs @@ -7,8 +7,8 @@ use evc::{ }, frame::Frame, header::Header, + helpers::vector::Vec2, ser::Sink, - vec2::Vec2, }; use log::info; use std::io::{BufReader, BufWriter}; @@ -36,9 +36,9 @@ fn main() -> anyhow::Result<()> { let config = EncodeConfig { translate: !args.no_translation, - ref_thres: 2.0, + ref_thres: 50.0, max_diff_size: 10_000, - min_block_size: 4, + min_block_size: 8, max_threads: 12, }; diff --git a/evc/src/block.rs b/evc/src/block.rs index 929793d..dd23207 100644 --- a/evc/src/block.rs +++ b/evc/src/block.rs @@ -3,14 +3,14 @@ use anyhow::bail; use crate::{ pixel::Pixel, ser::{Ser, Sink, Source}, - vec2::{Small, Vec2}, + helpers::vector::{Small, Vec2}, }; #[derive(Clone, Debug)] pub enum Block { Literal(Vec), Split(Box<[Block; 2]>), - Reference { translation: Vec2 }, + Reference { translation: Vec2 }, } impl Block { @@ -33,7 +33,7 @@ impl Block { Ok(()) } - pub fn read(source: &mut impl std::io::Read, size: Vec2) -> anyhow::Result { + pub fn read(source: &mut impl std::io::Read, size: Vec2) -> anyhow::Result { Ok(match source.get::()? { 0 => Block::Literal(source.get()?), 1 => Block::Split(Box::new({ @@ -54,7 +54,7 @@ impl Block { [a, b] })), 2 => Block::Reference { - translation: source.get::>()?.0, + translation: source.get::>>()?.0, }, x => bail!("corrupt block type ({})", x), }) diff --git a/evc/src/codec/encode.rs b/evc/src/codec/encode.rs index dbefdb4..ce5d71b 100644 --- a/evc/src/codec/encode.rs +++ b/evc/src/codec/encode.rs @@ -1,5 +1,6 @@ use crate::{ - block::Block, frame::Frame, pixel::Pixel, threading::both_par, vec2::Vec2, view::View, + block::Block, frame::Frame, helpers::threading::both_par, helpers::vector::Vec2, pixel::Pixel, + view::View, }; #[derive(Debug, Clone)] @@ -17,15 +18,15 @@ pub fn encode_block(view: View<&Frame>, prev: View<&Frame>, config: &EncodeConfi // let importance = importance(&view); let (diff, translation) = if view.area() > config.max_diff_size { - (f64::INFINITY, Vec2::ZERO) + (f64::INFINITY, Vec2::::ZERO) } else if config.translate { let mut best_diff = f64::INFINITY; - let mut best_translation = Vec2::ZERO; + let mut best_translation = Vec2::::ZERO; const OFFSETS: &[isize] = &[-64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64]; for x in OFFSETS { for y in OFFSETS { let translation = Vec2 { x: *x, y: *y }; - let diff = View::diff(&view, &prev.offset(translation)) / view.area() as f64; + let diff = View::diff(&view, &prev.offset(translation)); // / view.area() as f64; if diff < best_diff { best_translation = translation; best_diff = diff; @@ -34,7 +35,10 @@ pub fn encode_block(view: View<&Frame>, prev: View<&Frame>, config: &EncodeConfi } (best_diff, best_translation) } else { - (View::diff(&view, &prev) / view.area() as f64, Vec2::ZERO) + ( + View::diff(&view, &prev) / view.area() as f64, + Vec2::::ZERO, + ) }; // config.importance_k) // / (config.importance_k + importance * config.importance_scale) @@ -76,10 +80,10 @@ pub fn importance(view: &View<&Frame>) -> f64 { for y in 0..view.size.y { let p = Vec2 { x, y }; if x > 0 { - acc += Pixel::distance(view[p], view[p + Vec2::LEFT]); + acc += Pixel::distance(view[p], view[p + Vec2::::LEFT]); } if y > 0 { - acc += Pixel::distance(view[p], view[p + Vec2::UP]); + acc += Pixel::distance(view[p], view[p + Vec2::::UP]); } } } diff --git a/evc/src/debug.rs b/evc/src/debug.rs index 0858fe4..d2102ba 100644 --- a/evc/src/debug.rs +++ b/evc/src/debug.rs @@ -1,4 +1,4 @@ -use crate::{frame::Frame, pixel::Pixel, vec2::Vec2, view::View}; +use crate::{frame::Frame, helpers::vector::Vec2, pixel::Pixel, view::View}; impl View<&mut Frame> { pub fn draw_box(&mut self, color: Pixel) { @@ -16,7 +16,7 @@ impl View<&mut Frame> { } impl Frame { - pub fn draw_line(&mut self, start: Vec2, end: Vec2, color: Pixel) { + pub fn draw_line(&mut self, start: Vec2, end: Vec2, color: Pixel) { let (sx, sy) = (start.x as f32, start.y as f32); let (ex, ey) = (end.x as f32, end.y as f32); let (dx, dy) = (ex - sx, ey - sy); diff --git a/evc/src/frame.rs b/evc/src/frame.rs index 3c40a3d..024b240 100644 --- a/evc/src/frame.rs +++ b/evc/src/frame.rs @@ -1,25 +1,25 @@ use crate::{ + helpers::vector::Vec2, pixel::Pixel, ser::{Sink, Source}, - vec2::Vec2, view::View, }; use std::ops::{Index, IndexMut}; #[derive(Debug, Clone)] pub struct Frame { - pub size: Vec2, + pub size: Vec2, buffer: Vec, } impl Frame { - pub fn new(size: Vec2) -> Self { + pub fn new(size: Vec2) -> Self { Self { size, buffer: (0..size.x * size.y).map(|_| Pixel::default()).collect(), } } - pub fn read(source: &mut impl Source, size: Vec2) -> anyhow::Result { + pub fn read(source: &mut impl Source, size: Vec2) -> anyhow::Result { let mut frame = Frame::new(size); for y in 0..size.y { for x in 0..size.x { @@ -38,28 +38,32 @@ impl Frame { Ok(()) } pub fn view<'a>(&'a self) -> View<&'a Frame> { - View::new(self, Vec2::ZERO, self.size) + View::new(self, Vec2::::ZERO, self.size) } pub fn view_mut<'a>(&'a mut self) -> View<&'a mut Frame> { - View::new(self, Vec2::ZERO, self.size) + View::new(self, Vec2::::ZERO, self.size) } - pub fn view_area<'a>(&'a self, offset: Vec2, size: Vec2) -> View<&'a Frame> { + pub fn view_area<'a>(&'a self, offset: Vec2, size: Vec2) -> View<&'a Frame> { View::new(self, offset, size) } - pub fn view_area_mut<'a>(&'a mut self, offset: Vec2, size: Vec2) -> View<&'a mut Frame> { + pub fn view_area_mut<'a>( + &'a mut self, + offset: Vec2, + size: Vec2, + ) -> View<&'a mut Frame> { View::new(self, offset, size) } - pub fn set(&mut self, pos: Vec2, color: Pixel) { + pub fn set(&mut self, pos: Vec2, color: Pixel) { if pos.x >= 0 && pos.y >= 0 && pos.x < self.size.x && pos.y < self.size.y { self[pos] = color } } } -impl Index for Frame { +impl Index> for Frame { type Output = Pixel; #[inline] - fn index(&self, Vec2 { x, y }: Vec2) -> &Self::Output { + fn index(&self, Vec2 { x, y }: Vec2) -> &Self::Output { if x >= 0 && y >= 0 && x < self.size.x && y < self.size.y { &self.buffer[(x + y * self.size.x) as usize] } else { @@ -67,13 +71,29 @@ impl Index for Frame { } } } -impl IndexMut for Frame { +impl IndexMut> for Frame { #[inline] - fn index_mut(&mut self, Vec2 { x, y }: Vec2) -> &mut Self::Output { + fn index_mut(&mut self, Vec2 { x, y }: Vec2) -> &mut Self::Output { &mut self.buffer[(x + y * self.size.x) as usize] } } +impl Frame { + #[inline] + pub fn sample(&self, p: Vec2) -> Pixel { + let fx = p.x.floor() as isize; + let fy = p.y.floor() as isize; + let cx = p.x.ceil() as isize; + let cy = p.y.ceil() as isize; + + // TODO dont loose accuracy here + Pixel::average( + Pixel::average(self[Vec2 { x: fx, y: fy }], self[Vec2 { x: fx, y: cy }]), + Pixel::average(self[Vec2 { x: cx, y: fx }], self[Vec2 { x: cx, y: fy }]), + ) + } +} + impl Index<(isize, isize)> for Frame { type Output = Pixel; #[inline] diff --git a/evc/src/header.rs b/evc/src/header.rs index a613627..4c9118e 100644 --- a/evc/src/header.rs +++ b/evc/src/header.rs @@ -1,11 +1,11 @@ use crate::{ + helpers::vector::Vec2, ser::{Ser, Sink, Source}, - vec2::Vec2, }; #[derive(Debug, Clone, PartialEq, Copy)] pub struct Header { - pub resolution: Vec2, + pub resolution: Vec2, pub frame_count: usize, } diff --git a/evc/src/helpers/matrix.rs b/evc/src/helpers/matrix.rs new file mode 100644 index 0000000..74d2ae4 --- /dev/null +++ b/evc/src/helpers/matrix.rs @@ -0,0 +1,30 @@ +use crate::helpers::vector::Vec2; + +pub struct Mat2 { + a: T, + b: T, + c: T, + d: T, +} + +impl + std::ops::Add + Copy> Mat2 { + pub fn transform(&self, v: Vec2) -> Vec2 { + Vec2 { + x: self.a * v.x + self.b * v.x, + y: self.c * v.y + self.d * v.y, + } + } +} + +impl + std::ops::Add + Copy> std::ops::Mul for Mat2 { + type Output = Mat2; + fn mul(self, rhs: Mat2) -> Mat2 { + let (x, y) = (self, rhs); + Mat2 { + a: x.a * y.a + x.b * y.c, + b: x.a * y.b + x.b * y.d, + c: x.c * y.a + x.d * y.c, + d: x.c * y.b + x.d * y.d, + } + } +} diff --git a/evc/src/helpers/mod.rs b/evc/src/helpers/mod.rs new file mode 100644 index 0000000..c5eed9d --- /dev/null +++ b/evc/src/helpers/mod.rs @@ -0,0 +1,3 @@ +pub mod vector; +pub mod threading; +pub mod matrix; \ No newline at end of file diff --git a/evc/src/helpers/threading.rs b/evc/src/helpers/threading.rs new file mode 100644 index 0000000..3291172 --- /dev/null +++ b/evc/src/helpers/threading.rs @@ -0,0 +1,26 @@ +use std::{ + sync::atomic::{AtomicUsize, Ordering}, + thread, +}; + +static THREADS_RUNNING: AtomicUsize = AtomicUsize::new(0); + +pub fn both_par(f1: F1, f2: F2, max_threads: usize) -> (O1, O2) +where + F1: FnOnce() -> O1 + Send + 'static, + O1: Send + 'static, + F2: FnOnce() -> O2, +{ + if THREADS_RUNNING.load(Ordering::Relaxed) < max_threads { + THREADS_RUNNING.fetch_add(1, Ordering::Relaxed); + + let o1h = thread::spawn(move || f1()); + let o2 = f2(); + let o1 = o1h.join().unwrap(); + + THREADS_RUNNING.fetch_sub(1, Ordering::Relaxed); + (o1, o2) + } else { + (f1(), f2()) + } +} diff --git a/evc/src/helpers/vector.rs b/evc/src/helpers/vector.rs new file mode 100644 index 0000000..9e7369e --- /dev/null +++ b/evc/src/helpers/vector.rs @@ -0,0 +1,92 @@ +use crate::ser::{Ser, Sink, Source}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Vec2 { + pub x: T, + pub y: T, +} + +impl Vec2 { + pub const ZERO: Vec2 = Vec2 { x: 0, y: 0 }; + pub const UP: Vec2 = Vec2 { x: 0, y: -1 }; + pub const LEFT: Vec2 = Vec2 { x: -1, y: 0 }; +} +impl Vec2 { + pub const ZERO: Vec2 = Vec2 { x: 0.0, y: 0.0 }; + pub const UP: Vec2 = Vec2 { x: 0.0, y: -1.0 }; + pub const LEFT: Vec2 = Vec2 { x: -1.0, y: 0.0 }; +} + +impl + Copy> Vec2 { + pub fn downscale(&self, f: T) -> Self { + Self { + x: self.x / f, + y: self.y / f, + } + } +} + +impl Ser for Vec2 { + fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { + sink.put((self.x, self.y)) + } + + fn read(source: &mut impl std::io::Read) -> anyhow::Result { + let (x, y) = source.get()?; + Ok(Vec2 { x, y }) + } +} + +pub struct Small(pub T); +impl Ser for Small> { + fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { + sink.put((self.0.x as i8, self.0.y as i8)) + } + + fn read(source: &mut impl std::io::Read) -> anyhow::Result { + let (x, y): (i8, i8) = source.get()?; + Ok(Small(Vec2 { + x: x as isize, + y: y as isize, + })) + } +} + +impl std::ops::Add for Vec2 { + type Output = Vec2; + #[inline] + fn add(self, rhs: Self) -> Self::Output { + Vec2 { + x: self.x + rhs.x, + y: self.y + rhs.y, + } + } +} +impl std::ops::Sub for Vec2 { + type Output = Vec2; + #[inline] + fn sub(self, rhs: Self) -> Self::Output { + Vec2 { + x: self.x - rhs.x, + y: self.y - rhs.y, + } + } +} +impl std::ops::Mul for Vec2 { + type Output = Vec2; + #[inline] + fn mul(self, rhs: Self) -> Self::Output { + Vec2 { + x: self.x * rhs.x, + y: self.y * rhs.y, + } + } +} + +impl From<(T, T)> for Vec2 { + #[inline] + fn from((x, y): (T, T)) -> Self { + Vec2 { x, y } + } +} + diff --git a/evc/src/lib.rs b/evc/src/lib.rs index e8eb51d..ffacf59 100644 --- a/evc/src/lib.rs +++ b/evc/src/lib.rs @@ -11,5 +11,5 @@ pub mod header; pub mod pixel; pub mod ser; pub mod view; -pub mod vec2; -pub mod threading; +pub mod refsampler; +pub mod helpers; diff --git a/evc/src/pixel.rs b/evc/src/pixel.rs index 4fb6772..121cb95 100644 --- a/evc/src/pixel.rs +++ b/evc/src/pixel.rs @@ -29,6 +29,14 @@ impl Pixel { ); SQRT[rd + gd + bd] } + #[inline] + pub fn average(a: Pixel, b: Pixel) -> Pixel { + Pixel { + r: (a.r >> 2) + (b.r >> 2), + g: (a.g >> 2) + (b.g >> 2), + b: (a.b >> 2) + (b.b >> 2), + } + } } const SQRT: [usize; 256 * 3] = gen_sqrt_lookup(); diff --git a/evc/src/refsampler.rs b/evc/src/refsampler.rs new file mode 100644 index 0000000..95e123b --- /dev/null +++ b/evc/src/refsampler.rs @@ -0,0 +1,14 @@ +use crate::{helpers::{matrix::Mat2, vector::Vec2}, frame::Frame}; + +pub struct Sampler { + translation: Vec2, + transform: Mat2, + + value_scale: f32, +} + +impl Sampler { + pub fn sample(&self, frame: &Frame, p: Vec2) { + + } +} diff --git a/evc/src/ser.rs b/evc/src/ser.rs index 98d6706..d05e42e 100644 --- a/evc/src/ser.rs +++ b/evc/src/ser.rs @@ -194,8 +194,8 @@ impl Ser for f64 { mod test { use super::{Ser, Sink}; use crate::header::Header; + use crate::helpers::vector::Vec2; use crate::ser::Source; - use crate::vec2::Vec2; use std::fmt::Debug; use std::io::Cursor; diff --git a/evc/src/threading.rs b/evc/src/threading.rs deleted file mode 100644 index 3291172..0000000 --- a/evc/src/threading.rs +++ /dev/null @@ -1,26 +0,0 @@ -use std::{ - sync::atomic::{AtomicUsize, Ordering}, - thread, -}; - -static THREADS_RUNNING: AtomicUsize = AtomicUsize::new(0); - -pub fn both_par(f1: F1, f2: F2, max_threads: usize) -> (O1, O2) -where - F1: FnOnce() -> O1 + Send + 'static, - O1: Send + 'static, - F2: FnOnce() -> O2, -{ - if THREADS_RUNNING.load(Ordering::Relaxed) < max_threads { - THREADS_RUNNING.fetch_add(1, Ordering::Relaxed); - - let o1h = thread::spawn(move || f1()); - let o2 = f2(); - let o1 = o1h.join().unwrap(); - - THREADS_RUNNING.fetch_sub(1, Ordering::Relaxed); - (o1, o2) - } else { - (f1(), f2()) - } -} diff --git a/evc/src/vec2.rs b/evc/src/vec2.rs deleted file mode 100644 index 5713d02..0000000 --- a/evc/src/vec2.rs +++ /dev/null @@ -1,82 +0,0 @@ -use crate::ser::{Ser, Sink, Source}; - -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] -pub struct Vec2 { - pub x: isize, - pub y: isize, -} -impl Vec2 { - pub const ZERO: Vec2 = Vec2 { x: 0, y: 0 }; - pub const UP: Vec2 = Vec2 { x: 0, y: -1 }; - pub const LEFT: Vec2 = Vec2 { x: -1, y: 0 }; - pub fn downscale(&self, f: isize) -> Self { - Self { - x: self.x / f, - y: self.y / f, - } - } -} - -impl Ser for Vec2 { - fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { - sink.put((self.x, self.y)) - } - - fn read(source: &mut impl std::io::Read) -> anyhow::Result { - let (x, y) = source.get()?; - Ok(Vec2 { x, y }) - } -} - -pub struct Small(pub T); -impl Ser for Small { - fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { - sink.put((self.0.x as i8, self.0.y as i8)) - } - - fn read(source: &mut impl std::io::Read) -> anyhow::Result { - let (x, y): (i8, i8) = source.get()?; - Ok(Small(Vec2 { - x: x as isize, - y: y as isize, - })) - } -} - -impl std::ops::Add for Vec2 { - type Output = Vec2; - #[inline] - fn add(self, rhs: Self) -> Self::Output { - Vec2 { - x: self.x + rhs.x, - y: self.y + rhs.y, - } - } -} -impl std::ops::Sub for Vec2 { - type Output = Vec2; - #[inline] - fn sub(self, rhs: Self) -> Self::Output { - Vec2 { - x: self.x - rhs.x, - y: self.y - rhs.y, - } - } -} -impl std::ops::Mul for Vec2 { - type Output = Vec2; - #[inline] - fn mul(self, rhs: Self) -> Self::Output { - Vec2 { - x: self.x * rhs.x, - y: self.y * rhs.y, - } - } -} - -impl From<(isize, isize)> for Vec2 { - #[inline] - fn from((x, y): (isize, isize)) -> Self { - Vec2 { x, y } - } -} diff --git a/evc/src/view.rs b/evc/src/view.rs index 36cdf42..5596637 100644 --- a/evc/src/view.rs +++ b/evc/src/view.rs @@ -1,14 +1,14 @@ -use crate::{frame::Frame, pixel::Pixel, vec2::Vec2}; +use crate::{frame::Frame, helpers::vector::Vec2, pixel::Pixel}; use std::ops::{Index, IndexMut}; pub struct View { pub frame: T, - pub offset: Vec2, - pub size: Vec2, + pub offset: Vec2, + pub size: Vec2, } impl View { - pub fn new(frame: T, offset: Vec2, size: Vec2) -> Self { + pub fn new(frame: T, offset: Vec2, size: Vec2) -> Self { Self { frame, offset, @@ -18,7 +18,7 @@ impl View { pub fn area(&self) -> isize { self.size.x * self.size.y } - pub fn center(&self) -> Vec2 { + pub fn center(&self) -> Vec2 { self.offset + self.size.downscale(2) } } @@ -71,7 +71,7 @@ impl View<&mut T> { } } impl View { - pub fn offset(&self, offset: Vec2) -> Self { + pub fn offset(&self, offset: Vec2) -> Self { Self { frame: self.frame, offset: self.offset + offset, @@ -124,7 +124,7 @@ impl View { ] } } -impl> View<&T> { +impl, Output = Pixel>> View<&T> { pub fn diff(va: &Self, vb: &Self) -> f64 { assert_eq!(va.size, vb.size); let mut acc = 0; @@ -164,42 +164,42 @@ impl View<&mut Frame> { } } -impl> Index for View<&T> { +impl, Output = Pixel>> Index> for View<&T> { type Output = Pixel; #[inline] - fn index(&self, p: Vec2) -> &Self::Output { + fn index(&self, p: Vec2) -> &Self::Output { &self.frame[self.offset + p] } } -impl> Index for View<&mut T> { +impl, Output = Pixel>> Index> for View<&mut T> { type Output = Pixel; #[inline] - fn index(&self, p: Vec2) -> &Self::Output { + fn index(&self, p: Vec2) -> &Self::Output { &self.frame[self.offset + p] } } -impl> IndexMut for View<&mut T> { +impl, Output = Pixel>> IndexMut> for View<&mut T> { #[inline] - fn index_mut(&mut self, p: Vec2) -> &mut Self::Output { + fn index_mut(&mut self, p: Vec2) -> &mut Self::Output { &mut self.frame[self.offset + p] } } -impl> Index<(isize, isize)> for View<&T> { +impl, Output = Pixel>> Index<(isize, isize)> for View<&T> { type Output = Pixel; #[inline] fn index(&self, (x, y): (isize, isize)) -> &Self::Output { &self[Vec2 { x, y }] } } -impl> Index<(isize, isize)> for View<&mut T> { +impl, Output = Pixel>> Index<(isize, isize)> for View<&mut T> { type Output = Pixel; #[inline] fn index(&self, (x, y): (isize, isize)) -> &Self::Output { &self[Vec2 { x, y }] } } -impl> IndexMut<(isize, isize)> for View<&mut T> { +impl, Output = Pixel>> IndexMut<(isize, isize)> for View<&mut T> { #[inline] fn index_mut(&mut self, (x, y): (isize, isize)) -> &mut Self::Output { &mut self[Vec2 { x, y }] -- cgit v1.2.3-70-g09d2