aboutsummaryrefslogtreecommitdiff
path: root/test2/src/main.rs
blob: f14520206a39d4e7383ee5b7814cacbe41293411 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
pub mod decode;
pub mod encode;
pub mod huffman;

use decode::Dec;
use encode::Enc;
use framework::bitstream_filter_main;
use glam::IVec2;
use std::env::args;

pub const BLOCK_SIZE: i32 = 8;

fn main() -> Result<(), std::io::Error> {
    match args().nth(1).unwrap().as_str() {
        "enc" => bitstream_filter_main::<Enc>(),
        "dec" => bitstream_filter_main::<Dec>(),
        _ => panic!("unknown mode"),
    }
}

#[derive(Clone)]
struct Frame {
    res: IVec2,
    data: Vec<u8>,
}

impl Frame {
    fn new(res: IVec2) -> Self {
        Frame {
            res,
            data: vec![127; (res.x * res.y + res.x * res.y / 2) as usize],
        }
    }
    fn export_block(&self, off: IVec2, buf: &mut Vec<u8>) {
        // Luma
        for y in 0..BLOCK_SIZE {
            let y_off = off.x + (y + off.y) * self.res.x;
            buf.extend(&self.data[y_off as usize..(y_off + BLOCK_SIZE) as usize]);
        }
        // Chroma
        let uvplane_off = self.res.x * self.res.y;
        for y in 0..BLOCK_SIZE / 2 {
            let y_off = uvplane_off + (off.x & !1) + (y + off.y / 2) * self.res.x;
            buf.extend(&self.data[y_off as usize..(y_off + BLOCK_SIZE) as usize]);
        }
    }
    #[allow(unused)]
    fn import_block(&mut self, off: IVec2, buf: &[u8]) -> usize {
        let mut p = 0;
        // Luma
        for y in 0..BLOCK_SIZE {
            let y_off = off.x + (y + off.y) * self.res.x;
            self.data[y_off as usize..(y_off + BLOCK_SIZE) as usize]
                .copy_from_slice(&buf[p..p + BLOCK_SIZE as usize]);
            p += BLOCK_SIZE as usize;
        }
        // Chroma
        let uvplane_off = self.res.x * self.res.y;
        for y in 0..BLOCK_SIZE / 2 {
            let y_off = uvplane_off + (off.x & !1) + (y + off.y / 2) * self.res.x;
            self.data[y_off as usize..(y_off + BLOCK_SIZE) as usize]
                .copy_from_slice(&buf[p..p + BLOCK_SIZE as usize]);
            p += BLOCK_SIZE as usize;
        }
        p
    }
    fn import_block_diff(&mut self, off: IVec2, buf: &[u8]) -> usize {
        // Luma
        for y in 0..BLOCK_SIZE {
            let y_off = off.x + (y + off.y) * self.res.x;
            let i = y * BLOCK_SIZE;
            for x in 0..BLOCK_SIZE {
                self.data[(y_off + x) as usize] += buf[(i + x) as usize] as u8;
            }
        }
        // Chroma
        let uvplane_off = self.res.x * self.res.y;
        for y in 0..BLOCK_SIZE / 2 {
            let y_off = uvplane_off + (off.x & !1) + (y + off.y / 2) * self.res.x;
            let i = BLOCK_SIZE * BLOCK_SIZE + y * BLOCK_SIZE;
            for x in 0..BLOCK_SIZE {
                self.data[(y_off + x) as usize] += buf[(i + x) as usize] as u8;
            }
        }
        (BLOCK_SIZE * BLOCK_SIZE + BLOCK_SIZE * BLOCK_SIZE / 2) as usize
    }
    fn copy_block(aframe: &Frame, bframe: &mut Frame, aoff: IVec2, boff: IVec2) {
        assert_eq!(aframe.res, bframe.res);
        let res = aframe.res;

        // Luma
        for y in 0..BLOCK_SIZE {
            let ay_off = aoff.x + (y + aoff.y) * res.x;
            let by_off = boff.x + (y + boff.y) * res.x;

            bframe.data[by_off as usize..(by_off + BLOCK_SIZE) as usize]
                .copy_from_slice(&aframe.data[ay_off as usize..(ay_off + BLOCK_SIZE) as usize]);
        }

        // Chroma
        let uvplane_off = res.x * res.y;
        for y in 0..BLOCK_SIZE / 2 {
            let ay_off = uvplane_off + (aoff.x & !1) + (y + aoff.y / 2) * res.x;
            let by_off = uvplane_off + (boff.x & !1) + (y + boff.y / 2) * res.x;

            bframe.data[by_off as usize..(by_off + BLOCK_SIZE) as usize]
                .copy_from_slice(&aframe.data[ay_off as usize..(ay_off + BLOCK_SIZE) as usize]);
        }
    }
    fn compare_block(aframe: &Frame, bframe: &Frame, aoff: IVec2, boff: IVec2) -> u32 {
        assert_eq!(aframe.res, bframe.res);
        let res = aframe.res;
        let mut diff = 0;

        // Luma
        for y in 0..BLOCK_SIZE {
            let ay_off = aoff.x + (y + aoff.y) * res.x;
            let by_off = boff.x + (y + boff.y) * res.x;

            for x in 0..BLOCK_SIZE {
                diff += aframe.data[(ay_off + x) as usize]
                    .abs_diff(bframe.data[(by_off + x) as usize]) as u32
            }
        }

        // Chroma
        let uvplane_off = res.x * res.y;
        for y in 0..BLOCK_SIZE / 2 {
            let ay_off = uvplane_off + (aoff.x & !1) + (y + aoff.y / 2) * res.x;
            let by_off = uvplane_off + (boff.x & !1) + (y + boff.y / 2) * res.x;

            for x in 0..BLOCK_SIZE {
                diff += aframe.data[(ay_off + x) as usize]
                    .abs_diff(bframe.data[(by_off + x) as usize]) as u32;
            }
        }

        diff
    }
}