aboutsummaryrefslogtreecommitdiff
path: root/lvc/src/bin
diff options
context:
space:
mode:
Diffstat (limited to 'lvc/src/bin')
-rw-r--r--lvc/src/bin/bench.rs28
-rw-r--r--lvc/src/bin/main.rs136
2 files changed, 164 insertions, 0 deletions
diff --git a/lvc/src/bin/bench.rs b/lvc/src/bin/bench.rs
new file mode 100644
index 0000000..37036f6
--- /dev/null
+++ b/lvc/src/bin/bench.rs
@@ -0,0 +1,28 @@
+use lvc::{diff::*, Frame, Ref, View, P2};
+use std::time::Instant;
+
+fn measure(f: impl FnOnce()) {
+ let t1 = Instant::now();
+ f();
+ let t2 = Instant::now();
+ eprintln!("took {:?}", (t2 - t1));
+}
+
+fn main() {
+ let size = P2 { x: 2000, y: 2000 };
+ let f1 = Frame::new(size);
+ let f2 = Frame::new(size);
+ measure(|| {
+ diff([&f1, &f2], View::all(size), Ref::default());
+ });
+}
+
+// #[test]
+// fn bench_fast_diff() {
+// let size = P2 { x: 2000, y: 2000 };
+// let f1 = Frame::new(size);
+// let f2 = Frame::new(size);
+// measure(|| {
+// diff_fast([&f1, &f2], View::all(size), Ref::default());
+// });
+// }
diff --git a/lvc/src/bin/main.rs b/lvc/src/bin/main.rs
new file mode 100644
index 0000000..d5c9bbe
--- /dev/null
+++ b/lvc/src/bin/main.rs
@@ -0,0 +1,136 @@
+use clap::{Parser, Subcommand};
+use lvc::{
+ debug::draw_debug,
+ decode::decode,
+ encode::{encode, EncodeConfig},
+ Block, Frame, Pixel, PixelValue, View, P2,
+};
+use std::{
+ io::{stdin, stdout, BufReader, BufWriter, Read, Write},
+ time::Instant,
+};
+
+#[derive(Parser)]
+#[clap(about, version)]
+struct Args {
+ // Width of the video signal
+ #[arg(short, long)]
+ width: u16,
+ // Height of the video signal
+ #[arg(short, long)]
+ height: u16,
+ #[clap(subcommand)]
+ action: Action,
+}
+
+#[derive(Clone, Subcommand)]
+enum Action {
+ // Compress video
+ Encode {
+ #[arg(short, long, default_value_t = 400)]
+ max_block_size: usize,
+ #[arg(short, long, default_value_t = 10)]
+ iters: usize,
+ #[arg(short, long, default_value_t = 5_000)]
+ threshold: u32,
+ },
+ // Decompress video
+ Decode {
+ #[arg(short, long)]
+ debug: bool,
+ },
+}
+
+fn main() {
+ let args = Args::parse();
+
+ let size = P2 {
+ x: args.width as i32,
+ y: args.height as i32,
+ };
+ match args.action {
+ Action::Decode { debug } => {
+ 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 };
+
+ let mut stdin = BufReader::new(stdin());
+ let mut stdout = BufWriter::new(stdout());
+
+ loop {
+ let b = Block::read(&mut stdin, View::all(size)).unwrap();
+ decode(&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);
+ write_frame(&mut stdout, &debug_frame);
+ } else {
+ write_frame(&mut stdout, &frame);
+ }
+
+ last_frame.pixels.copy_from_slice(&frame.pixels); // TODO use mem::swap
+ frame.pixels.iter_mut().for_each(|e| *e = Pixel::BLACK);
+ }
+ }
+ Action::Encode {
+ max_block_size,
+ threshold,
+ iters,
+ } => {
+ let config = EncodeConfig {
+ threshold,
+ max_block_size,
+ iters,
+ };
+
+ let mut last_frame = Frame::new(size);
+
+ let mut stdin = BufReader::new(stdin());
+ let mut stdout = BufWriter::new(stdout());
+
+ for frame_number in 0.. {
+ let mut frame = read_frame(&mut stdin, size);
+
+ let t = Instant::now();
+ let b: Block = encode(&last_frame, &frame, View::all(size), &config);
+ eprintln!("frame {frame_number} took {:?}", t.elapsed());
+
+ b.write(&mut stdout);
+
+ decode(&last_frame, &mut frame, View::all(size), &b);
+ last_frame = frame;
+ }
+ }
+ }
+}
+
+fn read_frame(inp: &mut impl Read, size: P2) -> Frame {
+ let mut f = Frame::new(size);
+
+ for y in 0..size.y {
+ for x in 0..size.x {
+ let mut cc = [0u8; 3];
+ inp.read_exact(&mut cc).unwrap();
+ f[P2 { x, y }] = Pixel {
+ r: cc[0] as PixelValue,
+ g: cc[1] as PixelValue,
+ b: cc[2] as PixelValue,
+ };
+ }
+ }
+ f
+}
+
+fn write_frame(out: &mut impl Write, frame: &Frame) {
+ for y in 0..frame.size.y {
+ for x in 0..frame.size.x {
+ let p = frame[P2 { x, y }];
+ let mut cc = [0u8; 3];
+ cc[0] = p.r.clamp(0, 255) as u8;
+ cc[1] = p.g.clamp(0, 255) as u8;
+ cc[2] = p.b.clamp(0, 255) as u8;
+ out.write_all(&mut cc).unwrap();
+ }
+ }
+}