aboutsummaryrefslogtreecommitdiff
path: root/lvc/src/main.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2023-03-08 21:26:35 +0100
committermetamuffin <metamuffin@disroot.org>2023-03-08 21:26:35 +0100
commit292519649c4244adb6672488efe7c2e906726c58 (patch)
tree41e2cd62d53b47afa7995d91a326ef2e361029c7 /lvc/src/main.rs
parent5b3c03bc0cfcf89e76953dde13ed58a39b5d1dd0 (diff)
downloadvideo-codec-experiments-292519649c4244adb6672488efe7c2e906726c58.tar
video-codec-experiments-292519649c4244adb6672488efe7c2e906726c58.tar.bz2
video-codec-experiments-292519649c4244adb6672488efe7c2e906726c58.tar.zst
about to implement huff
Diffstat (limited to 'lvc/src/main.rs')
-rw-r--r--lvc/src/main.rs135
1 files changed, 0 insertions, 135 deletions
diff --git a/lvc/src/main.rs b/lvc/src/main.rs
deleted file mode 100644
index 9d8a6cb..0000000
--- a/lvc/src/main.rs
+++ /dev/null
@@ -1,135 +0,0 @@
-use bincode::config::standard;
-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 = bincode::decode_from_std_read(&mut stdin, standard()).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);
- }
- }
- 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());
-
- bincode::encode_into_std_write(&b, &mut stdout, standard()).unwrap();
-
- 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 as u8;
- cc[1] = p.g as u8;
- cc[2] = p.b as u8;
- out.write_all(&mut cc).unwrap();
- }
- }
-}