diff options
Diffstat (limited to 'evc/src')
-rw-r--r-- | evc/src/bin/encode.rs | 6 | ||||
-rw-r--r-- | evc/src/block.rs | 8 | ||||
-rw-r--r-- | evc/src/codec/encode.rs | 18 | ||||
-rw-r--r-- | evc/src/debug.rs | 4 | ||||
-rw-r--r-- | evc/src/frame.rs | 46 | ||||
-rw-r--r-- | evc/src/header.rs | 4 | ||||
-rw-r--r-- | evc/src/helpers/matrix.rs | 30 | ||||
-rw-r--r-- | evc/src/helpers/mod.rs | 3 | ||||
-rw-r--r-- | evc/src/helpers/threading.rs (renamed from evc/src/threading.rs) | 0 | ||||
-rw-r--r-- | evc/src/helpers/vector.rs (renamed from evc/src/vec2.rs) | 46 | ||||
-rw-r--r-- | evc/src/lib.rs | 4 | ||||
-rw-r--r-- | evc/src/pixel.rs | 8 | ||||
-rw-r--r-- | evc/src/refsampler.rs | 14 | ||||
-rw-r--r-- | evc/src/ser.rs | 2 | ||||
-rw-r--r-- | evc/src/view.rs | 32 |
15 files changed, 157 insertions, 68 deletions
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<Pixel>), Split(Box<[Block; 2]>), - Reference { translation: Vec2 }, + Reference { translation: Vec2<isize> }, } impl Block { @@ -33,7 +33,7 @@ impl Block { Ok(()) } - pub fn read(source: &mut impl std::io::Read, size: Vec2) -> anyhow::Result<Self> { + pub fn read(source: &mut impl std::io::Read, size: Vec2<isize>) -> anyhow::Result<Self> { Ok(match source.get::<u8>()? { 0 => Block::Literal(source.get()?), 1 => Block::Split(Box::new({ @@ -54,7 +54,7 @@ impl Block { [a, b] })), 2 => Block::Reference { - translation: source.get::<Small<Vec2>>()?.0, + translation: source.get::<Small<Vec2<isize>>>()?.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::<isize>::ZERO) } else if config.translate { let mut best_diff = f64::INFINITY; - let mut best_translation = Vec2::ZERO; + let mut best_translation = Vec2::<isize>::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::<isize>::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::<isize>::LEFT]); } if y > 0 { - acc += Pixel::distance(view[p], view[p + Vec2::UP]); + acc += Pixel::distance(view[p], view[p + Vec2::<isize>::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<isize>, end: Vec2<isize>, 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<isize>, buffer: Vec<Pixel>, } impl Frame { - pub fn new(size: Vec2) -> Self { + pub fn new(size: Vec2<isize>) -> Self { Self { size, buffer: (0..size.x * size.y).map(|_| Pixel::default()).collect(), } } - pub fn read(source: &mut impl Source, size: Vec2) -> anyhow::Result<Self> { + pub fn read(source: &mut impl Source, size: Vec2<isize>) -> anyhow::Result<Self> { 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::<isize>::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::<isize>::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<isize>, size: Vec2<isize>) -> 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<isize>, + size: Vec2<isize>, + ) -> 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<isize>, color: Pixel) { if pos.x >= 0 && pos.y >= 0 && pos.x < self.size.x && pos.y < self.size.y { self[pos] = color } } } -impl Index<Vec2> for Frame { +impl Index<Vec2<isize>> for Frame { type Output = Pixel; #[inline] - fn index(&self, Vec2 { x, y }: Vec2) -> &Self::Output { + fn index(&self, Vec2 { x, y }: Vec2<isize>) -> &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<Vec2> for Frame { } } } -impl IndexMut<Vec2> for Frame { +impl IndexMut<Vec2<isize>> for Frame { #[inline] - fn index_mut(&mut self, Vec2 { x, y }: Vec2) -> &mut Self::Output { + fn index_mut(&mut self, Vec2 { x, y }: Vec2<isize>) -> &mut Self::Output { &mut self.buffer[(x + y * self.size.x) as usize] } } +impl Frame { + #[inline] + pub fn sample(&self, p: Vec2<f32>) -> 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<isize>, 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<T> { + a: T, + b: T, + c: T, + d: T, +} + +impl<T: std::ops::Mul<Output = T> + std::ops::Add<Output = T> + Copy> Mat2<T> { + pub fn transform(&self, v: Vec2<T>) -> Vec2<T> { + Vec2 { + x: self.a * v.x + self.b * v.x, + y: self.c * v.y + self.d * v.y, + } + } +} + +impl<T: std::ops::Mul<Output = T> + std::ops::Add<Output = T> + Copy> std::ops::Mul for Mat2<T> { + type Output = Mat2<T>; + fn mul(self, rhs: Mat2<T>) -> Mat2<T> { + 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/threading.rs b/evc/src/helpers/threading.rs index 3291172..3291172 100644 --- a/evc/src/threading.rs +++ b/evc/src/helpers/threading.rs diff --git a/evc/src/vec2.rs b/evc/src/helpers/vector.rs index 5713d02..9e7369e 100644 --- a/evc/src/vec2.rs +++ b/evc/src/helpers/vector.rs @@ -1,15 +1,24 @@ use crate::ser::{Ser, Sink, Source}; #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] -pub struct Vec2 { - pub x: isize, - pub y: isize, +pub struct Vec2<T> { + 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 }; - pub fn downscale(&self, f: isize) -> Self { + +impl Vec2<isize> { + pub const ZERO: Vec2<isize> = Vec2 { x: 0, y: 0 }; + pub const UP: Vec2<isize> = Vec2 { x: 0, y: -1 }; + pub const LEFT: Vec2<isize> = Vec2 { x: -1, y: 0 }; +} +impl Vec2<f32> { + pub const ZERO: Vec2<f32> = Vec2 { x: 0.0, y: 0.0 }; + pub const UP: Vec2<f32> = Vec2 { x: 0.0, y: -1.0 }; + pub const LEFT: Vec2<f32> = Vec2 { x: -1.0, y: 0.0 }; +} + +impl<T: std::ops::Div<Output = T> + Copy> Vec2<T> { + pub fn downscale(&self, f: T) -> Self { Self { x: self.x / f, y: self.y / f, @@ -17,7 +26,7 @@ impl Vec2 { } } -impl Ser for Vec2 { +impl Ser for Vec2<isize> { fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { sink.put((self.x, self.y)) } @@ -29,7 +38,7 @@ impl Ser for Vec2 { } pub struct Small<T>(pub T); -impl Ser for Small<Vec2> { +impl Ser for Small<Vec2<isize>> { fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { sink.put((self.0.x as i8, self.0.y as i8)) } @@ -43,8 +52,8 @@ impl Ser for Small<Vec2> { } } -impl std::ops::Add for Vec2 { - type Output = Vec2; +impl<T: std::ops::Add> std::ops::Add for Vec2<T> { + type Output = Vec2<T::Output>; #[inline] fn add(self, rhs: Self) -> Self::Output { Vec2 { @@ -53,8 +62,8 @@ impl std::ops::Add for Vec2 { } } } -impl std::ops::Sub for Vec2 { - type Output = Vec2; +impl<T: std::ops::Sub> std::ops::Sub for Vec2<T> { + type Output = Vec2<T::Output>; #[inline] fn sub(self, rhs: Self) -> Self::Output { Vec2 { @@ -63,8 +72,8 @@ impl std::ops::Sub for Vec2 { } } } -impl std::ops::Mul for Vec2 { - type Output = Vec2; +impl<T: std::ops::Mul> std::ops::Mul for Vec2<T> { + type Output = Vec2<T::Output>; #[inline] fn mul(self, rhs: Self) -> Self::Output { Vec2 { @@ -74,9 +83,10 @@ impl std::ops::Mul for Vec2 { } } -impl From<(isize, isize)> for Vec2 { +impl<T> From<(T, T)> for Vec2<T> { #[inline] - fn from((x, y): (isize, isize)) -> Self { + 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<f32>, + transform: Mat2<f32>, + + value_scale: f32, +} + +impl Sampler { + pub fn sample(&self, frame: &Frame, p: Vec2<f32>) { + + } +} 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/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<T> { pub frame: T, - pub offset: Vec2, - pub size: Vec2, + pub offset: Vec2<isize>, + pub size: Vec2<isize>, } impl<T> View<T> { - pub fn new(frame: T, offset: Vec2, size: Vec2) -> Self { + pub fn new(frame: T, offset: Vec2<isize>, size: Vec2<isize>) -> Self { Self { frame, offset, @@ -18,7 +18,7 @@ impl<T> View<T> { pub fn area(&self) -> isize { self.size.x * self.size.y } - pub fn center(&self) -> Vec2 { + pub fn center(&self) -> Vec2<isize> { self.offset + self.size.downscale(2) } } @@ -71,7 +71,7 @@ impl<T> View<&mut T> { } } impl<T: Copy> View<T> { - pub fn offset(&self, offset: Vec2) -> Self { + pub fn offset(&self, offset: Vec2<isize>) -> Self { Self { frame: self.frame, offset: self.offset + offset, @@ -124,7 +124,7 @@ impl<T: Copy> View<T> { ] } } -impl<T: Index<Vec2, Output = Pixel>> View<&T> { +impl<T: Index<Vec2<isize>, 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<T: Index<Vec2, Output = Pixel>> Index<Vec2> for View<&T> { +impl<T: Index<Vec2<isize>, Output = Pixel>> Index<Vec2<isize>> for View<&T> { type Output = Pixel; #[inline] - fn index(&self, p: Vec2) -> &Self::Output { + fn index(&self, p: Vec2<isize>) -> &Self::Output { &self.frame[self.offset + p] } } -impl<T: Index<Vec2, Output = Pixel>> Index<Vec2> for View<&mut T> { +impl<T: Index<Vec2<isize>, Output = Pixel>> Index<Vec2<isize>> for View<&mut T> { type Output = Pixel; #[inline] - fn index(&self, p: Vec2) -> &Self::Output { + fn index(&self, p: Vec2<isize>) -> &Self::Output { &self.frame[self.offset + p] } } -impl<T: IndexMut<Vec2, Output = Pixel>> IndexMut<Vec2> for View<&mut T> { +impl<T: IndexMut<Vec2<isize>, Output = Pixel>> IndexMut<Vec2<isize>> for View<&mut T> { #[inline] - fn index_mut(&mut self, p: Vec2) -> &mut Self::Output { + fn index_mut(&mut self, p: Vec2<isize>) -> &mut Self::Output { &mut self.frame[self.offset + p] } } -impl<T: Index<Vec2, Output = Pixel>> Index<(isize, isize)> for View<&T> { +impl<T: Index<Vec2<isize>, 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<T: Index<Vec2, Output = Pixel>> Index<(isize, isize)> for View<&mut T> { +impl<T: Index<Vec2<isize>, 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<T: IndexMut<Vec2, Output = Pixel>> IndexMut<(isize, isize)> for View<&mut T> { +impl<T: IndexMut<Vec2<isize>, 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 }] |