From a713143ef9c1187c37004043b1d3322d773f9ea0 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 6 Dec 2022 22:16:50 +0100 Subject: webm + variable amount of threads --- evc/scripts/gen | 4 ++-- evc/src/bin/encode.rs | 3 ++- evc/src/codec/encode.rs | 19 ++++++++++++++----- evc/src/threading.rs | 4 ++-- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/evc/scripts/gen b/evc/scripts/gen index 1cf4493..620c69f 100755 --- a/evc/scripts/gen +++ b/evc/scripts/gen @@ -5,6 +5,6 @@ set t $argv[3] ffmpeg -hide_banner -i $argv[4] -to {$t} -vf scale={$w}x{$h},fps=30,format=rgb24 -f rawvideo pipe:1 | LOG=info cargo run --release --bin encode -- -W {$w} -H {$h} > samples/encoded LOG=info cargo run --release --bin decode -- < samples/encoded | - ffmpeg -hide_banner -y -framerate 30 -video_size {$w}x{$h} -pixel_format rgb24 -f rawvideo -i pipe:0 samples/decoded.mp4 + ffmpeg -hide_banner -y -framerate 30 -video_size {$w}x{$h} -pixel_format rgb24 -f rawvideo -i pipe:0 samples/decoded.webm LOG=info cargo run --release --bin decode -- --debug < samples/encoded | - ffmpeg -hide_banner -y -framerate 30 -video_size {$w}x{$h} -pixel_format rgb24 -f rawvideo -i pipe:0 samples/decoded-debug.mp4 + ffmpeg -hide_banner -y -framerate 30 -video_size {$w}x{$h} -pixel_format rgb24 -f rawvideo -i pipe:0 samples/decoded-debug.webm diff --git a/evc/src/bin/encode.rs b/evc/src/bin/encode.rs index fb5a18b..4857a80 100644 --- a/evc/src/bin/encode.rs +++ b/evc/src/bin/encode.rs @@ -38,7 +38,8 @@ fn main() -> anyhow::Result<()> { translate: !args.no_translation, ref_thres: 2.0, max_diff_size: 10_000, - min_block_size: 12, + min_block_size: 4, + max_threads: 12, }; let size = Vec2 { diff --git a/evc/src/codec/encode.rs b/evc/src/codec/encode.rs index 13df17d..dbefdb4 100644 --- a/evc/src/codec/encode.rs +++ b/evc/src/codec/encode.rs @@ -8,6 +8,7 @@ pub struct EncodeConfig { pub ref_thres: f64, pub max_diff_size: isize, pub min_block_size: isize, + pub max_threads: usize, // pub importance_k: f64, // pub importance_scale: f64, } @@ -40,7 +41,7 @@ pub fn encode_block(view: View<&Frame>, prev: View<&Frame>, config: &EncodeConfi if diff < (config.ref_thres) { Block::Reference { translation } } else { - if view.size.x < config.min_block_size { + if view.size.x < config.min_block_size || view.size.y < config.min_block_size { Block::Literal(view.pixels()) } else { let [av, bv] = @@ -48,10 +49,18 @@ pub fn encode_block(view: View<&Frame>, prev: View<&Frame>, config: &EncodeConfi 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), - ); + + // only bother to do multithreading, when the block is big. + let (a, b) = if view.size.x > 64 { + both_par( + || encode_block(av, ap, config), + || encode_block(bv, bp, config), + config.max_threads, + ) + } else { + (encode_block(av, ap, config), encode_block(bv, bp, config)) + }; + if a.is_literal() && b.is_literal() { Block::Literal(view.pixels()) } else { diff --git a/evc/src/threading.rs b/evc/src/threading.rs index 012b5bf..3291172 100644 --- a/evc/src/threading.rs +++ b/evc/src/threading.rs @@ -5,13 +5,13 @@ use std::{ static THREADS_RUNNING: AtomicUsize = AtomicUsize::new(0); -pub fn both_par(f1: F1, f2: F2) -> (O1, O2) +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) < 12 { + if THREADS_RUNNING.load(Ordering::Relaxed) < max_threads { THREADS_RUNNING.fetch_add(1, Ordering::Relaxed); let o1h = thread::spawn(move || f1()); -- cgit v1.2.3-70-g09d2