diff options
author | metamuffin <metamuffin@disroot.org> | 2022-12-06 21:46:19 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2022-12-06 21:46:19 +0100 |
commit | 39cb075c7f58e78899be43ca9ad4d65837f53a26 (patch) | |
tree | 61f11a221ea2d60b8dc92c1af736c0fb3c9aad2d | |
parent | 437e092985e5633eee50874c337ccbdd0b76ff1e (diff) | |
download | video-codec-experiments-39cb075c7f58e78899be43ca9ad4d65837f53a26.tar video-codec-experiments-39cb075c7f58e78899be43ca9ad4d65837f53a26.tar.bz2 video-codec-experiments-39cb075c7f58e78899be43ca9ad4d65837f53a26.tar.zst |
unsafe multithreading
-rw-r--r-- | evc/spec.md | 1 | ||||
-rw-r--r-- | evc/src/bin/decode.rs | 2 | ||||
-rw-r--r-- | evc/src/bin/encode.rs | 2 | ||||
-rw-r--r-- | evc/src/codec/encode.rs | 41 | ||||
-rw-r--r-- | evc/src/lib.rs | 1 | ||||
-rw-r--r-- | evc/src/threading.rs | 26 | ||||
-rw-r--r-- | evc/src/vec2.rs | 2 |
7 files changed, 67 insertions, 8 deletions
diff --git a/evc/spec.md b/evc/spec.md index a6994e0..da05da1 100644 --- a/evc/spec.md +++ b/evc/spec.md @@ -14,6 +14,7 @@ - sub-blocks: _`[block; 2]` (see above)_ - **Reference-Block** (reuses previous frame in some way) - translation: _`i8, i8`_ + <!-- - factor: _`i8`_ (represents multiplication with $1.1^n$) --> ### Todo diff --git a/evc/src/bin/decode.rs b/evc/src/bin/decode.rs index f2f4c65..b15467f 100644 --- a/evc/src/bin/decode.rs +++ b/evc/src/bin/decode.rs @@ -62,7 +62,7 @@ fn draw_debug(block: &Block, mut target: View<&mut Frame>) { target.draw_box(Pixel::BLUE); target .frame - .draw_line(target.center(), target.center() + *translation, Pixel::BLUE) + .draw_line(target.center(), target.center() + *translation, Pixel::RED) } } } diff --git a/evc/src/bin/encode.rs b/evc/src/bin/encode.rs index 79ec934..fb5a18b 100644 --- a/evc/src/bin/encode.rs +++ b/evc/src/bin/encode.rs @@ -36,7 +36,7 @@ fn main() -> anyhow::Result<()> { let config = EncodeConfig { translate: !args.no_translation, - ref_thres: args.ref_thres, + ref_thres: 2.0, max_diff_size: 10_000, min_block_size: 12, }; diff --git a/evc/src/codec/encode.rs b/evc/src/codec/encode.rs index 7322fea..13df17d 100644 --- a/evc/src/codec/encode.rs +++ b/evc/src/codec/encode.rs @@ -1,4 +1,6 @@ -use crate::{block::Block, frame::Frame, vec2::Vec2, view::View}; +use crate::{ + block::Block, frame::Frame, pixel::Pixel, threading::both_par, vec2::Vec2, view::View, +}; #[derive(Debug, Clone)] pub struct EncodeConfig { @@ -6,9 +8,13 @@ pub struct EncodeConfig { pub ref_thres: f64, pub max_diff_size: isize, pub min_block_size: isize, + // pub importance_k: f64, + // pub importance_scale: f64, } pub fn encode_block(view: View<&Frame>, prev: View<&Frame>, config: &EncodeConfig) -> Block { + // let importance = importance(&view); + let (diff, translation) = if view.area() > config.max_diff_size { (f64::INFINITY, Vec2::ZERO) } else if config.translate { @@ -29,16 +35,23 @@ pub fn encode_block(view: View<&Frame>, prev: View<&Frame>, config: &EncodeConfi } else { (View::diff(&view, &prev) / view.area() as f64, Vec2::ZERO) }; - if diff < config.ref_thres { + // config.importance_k) + // / (config.importance_k + importance * config.importance_scale) + if diff < (config.ref_thres) { Block::Reference { translation } } else { if view.size.x < config.min_block_size { Block::Literal(view.pixels()) } else { - let [av, bv] = view.split(); - let [ap, bp] = prev.split(); - let a = encode_block(av, ap, config); - let b = encode_block(bv, bp, config); + let [av, bv] = + unsafe { std::mem::transmute::<_, [View<&'static Frame>; 2]>(view.split()) }; + let [ap, bp] = + unsafe { std::mem::transmute::<_, [View<&'static Frame>; 2]>(prev.split()) }; + let config = unsafe { std::mem::transmute::<_, &'static EncodeConfig>(config) }; + let (a, b) = both_par( + || encode_block(av, ap, config), + || encode_block(bv, bp, config), + ); if a.is_literal() && b.is_literal() { Block::Literal(view.pixels()) } else { @@ -47,3 +60,19 @@ pub fn encode_block(view: View<&Frame>, prev: View<&Frame>, config: &EncodeConfi } } } + +pub fn importance(view: &View<&Frame>) -> f64 { + let mut acc = 0; + for x in 0..view.size.x { + for y in 0..view.size.y { + let p = Vec2 { x, y }; + if x > 0 { + acc += Pixel::distance(view[p], view[p + Vec2::LEFT]); + } + if y > 0 { + acc += Pixel::distance(view[p], view[p + Vec2::UP]); + } + } + } + (acc / view.area() as usize) as f64 +} diff --git a/evc/src/lib.rs b/evc/src/lib.rs index 22866e1..e8eb51d 100644 --- a/evc/src/lib.rs +++ b/evc/src/lib.rs @@ -12,3 +12,4 @@ pub mod pixel; pub mod ser; pub mod view; pub mod vec2; +pub mod threading; diff --git a/evc/src/threading.rs b/evc/src/threading.rs new file mode 100644 index 0000000..012b5bf --- /dev/null +++ b/evc/src/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, F2, O1, O2>(f1: F1, f2: F2) -> (O1, O2) +where + F1: FnOnce() -> O1 + Send + 'static, + O1: Send + 'static, + F2: FnOnce() -> O2, +{ + if THREADS_RUNNING.load(Ordering::Relaxed) < 12 { + 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 index 8b01fdb..5713d02 100644 --- a/evc/src/vec2.rs +++ b/evc/src/vec2.rs @@ -7,6 +7,8 @@ pub struct Vec2 { } 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, |