aboutsummaryrefslogtreecommitdiff
path: root/lvc/codec
diff options
context:
space:
mode:
Diffstat (limited to 'lvc/codec')
-rw-r--r--lvc/codec/Cargo.toml7
-rw-r--r--lvc/codec/src/decode.rs56
-rw-r--r--lvc/codec/src/encode.rs3
-rw-r--r--lvc/codec/src/impls.rs13
-rw-r--r--lvc/codec/src/lib.rs5
5 files changed, 61 insertions, 23 deletions
diff --git a/lvc/codec/Cargo.toml b/lvc/codec/Cargo.toml
index 67189bb..0c52ad2 100644
--- a/lvc/codec/Cargo.toml
+++ b/lvc/codec/Cargo.toml
@@ -4,4 +4,9 @@ version = "0.1.0"
edition = "2021"
[dependencies]
-rayon = "1.7.0"
+rayon = {version="1.7.0",optional = true}
+
+[features]
+default = ["parallel"]
+parallel = ["dep:rayon"]
+
diff --git a/lvc/codec/src/decode.rs b/lvc/codec/src/decode.rs
index 4830156..616b7af 100644
--- a/lvc/codec/src/decode.rs
+++ b/lvc/codec/src/decode.rs
@@ -1,38 +1,54 @@
-use crate::{debug::draw_debug, huff::read_huff, split::split, Block, Frame, Pixel, View, P2};
-use rayon::join;
+use crate::{
+ debug::draw_debug, huff::read_huff, impls::join, split::split, Block, Frame, View, P2,
+};
use std::io::{BufReader, BufWriter, Read, Result, Write};
pub fn decode(size: P2, debug: bool, input: impl Read, output: impl Write) -> Result<()> {
let mut input = BufReader::new(input);
let mut output = BufWriter::new(output);
+ let mut d = Decoder::new(size);
+ let mut f = Frame::new(size);
+ loop {
+ d.decode_frame(&mut input, &mut f, debug)?;
+ Frame::write(&mut output, &f)?;
+ }
+}
- let mut frame = Frame::new(size);
- let mut last_frame = Frame::new(size);
- let mut debug_frame = if debug { Some(Frame::new(size)) } else { None };
+pub struct Decoder {
+ last_frame: Frame,
+ size: P2,
+}
- let huff = true;
- loop {
+impl Decoder {
+ pub fn new(size: P2) -> Self {
+ Self {
+ size,
+ last_frame: Frame::new(size),
+ }
+ }
+ pub fn decode_frame(
+ &mut self,
+ mut input: impl Read,
+ output: &mut Frame,
+ debug: bool,
+ ) -> Result<()> {
+ let huff = true;
let b = if huff {
let mut buf = vec![];
read_huff(&mut input, &mut buf)?;
+ eprintln!("{}", buf.len());
let mut buf = std::io::Cursor::new(&mut buf);
- Block::read(&mut buf, View::all(size))?
+ Block::read(&mut buf, View::all(self.size))?
} else {
- Block::read(&mut input, View::all(size))?
+ Block::read(&mut input, View::all(self.size))?
};
- decode_block(&last_frame, &mut frame, View::all(size), &b);
-
- if let Some(debug_frame) = &mut debug_frame {
- debug_frame.pixels.copy_from_slice(&frame.pixels);
- draw_debug(debug_frame, View::all(size), &b);
- Frame::write(&mut output, &debug_frame)?;
- } else {
- Frame::write(&mut output, &frame)?;
+ decode_block(&self.last_frame, output, View::all(self.size), &b);
+ self.last_frame.pixels.copy_from_slice(&output.pixels); // TODO use mem::swap
+ if debug {
+ draw_debug(output, View::all(self.size), &b);
}
-
- last_frame.pixels.copy_from_slice(&frame.pixels); // TODO use mem::swap
- frame.pixels.iter_mut().for_each(|e| *e = Pixel::BLACK);
+ Ok(())
}
}
diff --git a/lvc/codec/src/encode.rs b/lvc/codec/src/encode.rs
index d612daf..564b0b3 100644
--- a/lvc/codec/src/encode.rs
+++ b/lvc/codec/src/encode.rs
@@ -1,5 +1,6 @@
use crate::diff::{diff, pixel_diff};
use crate::huff::write_huff;
+use crate::impls::join;
use crate::split::split;
use crate::{decode::decode_block, Block, Frame, Pixel, Ref, View, P2};
use std::io::{BufReader, BufWriter, Read, Write};
@@ -77,7 +78,7 @@ pub fn encode_block(last_frame: &Frame, frame: &Frame, view: View, config: &Enco
|| (view_area > 64 && attention(frame, view) > config.attention_split)
{
let [av, bv] = split(view);
- let (ab, bb) = rayon::join(
+ let (ab, bb) = join(
|| Box::new(encode_block(last_frame, frame, av, config)),
|| Box::new(encode_block(last_frame, frame, bv, config)),
);
diff --git a/lvc/codec/src/impls.rs b/lvc/codec/src/impls.rs
index 098db39..b4cc119 100644
--- a/lvc/codec/src/impls.rs
+++ b/lvc/codec/src/impls.rs
@@ -1,6 +1,19 @@
use crate::{Frame, Pixel, Ref, View, P2};
use std::ops::{Add, AddAssign, Index, IndexMut, Sub};
+#[cfg(feature = "parallel")]
+pub use rayon::join;
+#[cfg(not(feature = "parallel"))]
+pub fn join<A, B, RA, RB>(oper_a: A, oper_b: B) -> (RA, RB)
+where
+ A: FnOnce() -> RA + Send,
+ B: FnOnce() -> RB + Send,
+ RA: Send,
+ RB: Send,
+{
+ (oper_a(), oper_b())
+}
+
impl Frame {
pub fn export(&self, view: View) -> Vec<Pixel> {
let mut o = vec![];
diff --git a/lvc/codec/src/lib.rs b/lvc/codec/src/lib.rs
index 5cd83b9..c764211 100644
--- a/lvc/codec/src/lib.rs
+++ b/lvc/codec/src/lib.rs
@@ -6,14 +6,17 @@ pub mod debug;
pub mod decode;
pub mod diff;
pub mod encode;
+pub mod frameio;
pub mod huff;
pub mod impls;
pub mod serialize;
pub mod split;
-pub mod frameio;
pub type PixelValue = i16;
+pub use decode::{decode, Decoder};
+pub use encode::encode;
+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Pixel {
pub r: PixelValue,