aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-duck/src/codecs/vpenc/coder.rs
blob: 60b1fd3e2c0e593f429b971f3f8d18087b16afed (plain) (blame)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use nihav_core::io::byteio::*;
use nihav_core::codecs::{EncoderResult, EncoderError};
use super::models::*;

pub struct EncSeq {
    pub bit:    bool,
    pub idx:    u8,
}

pub struct TokenSeq<T: PartialEq> {
    pub val:    T,
    pub seq:    &'static [EncSeq],
}

#[macro_export]
macro_rules! bit_entry {
    (T; $idx:expr) => {EncSeq {bit: true,  idx: $idx }};
    (F; $idx:expr) => {EncSeq {bit: false, idx: $idx }};
}

#[macro_export]
macro_rules! bit_seq {
    ($val: expr; $( $bit:tt),* ; $( $idx:expr),* ) => {
        TokenSeq {
            val: $val,
            seq:
                &[
                $(
                    bit_entry!($bit; $idx),
                )*
                ]
        }
    };
}

pub struct BoolEncoder<'a, 'b> {
    bw:     &'a mut ByteWriter<'b>,
    val:    u32,
    range:  u32,
    bits:   u8,
    saved:  u8,
    run:    usize,
}

impl<'a, 'b> BoolEncoder<'a, 'b> {
    pub fn new(bw: &'a mut ByteWriter<'b>) -> Self {
        Self {
            bw,
            val:    0,
            range:  255,
            bits:   0,
            saved:  0,
            run:    0,
        }
    }
    pub fn put_bool(&mut self, bit: bool, prob: u8) -> EncoderResult<()> {
        let split = 1 + (((self.range - 1) * u32::from(prob)) >> 8);
        if bit {
            self.range -= split;
            self.val   += split;
        } else {
            self.range = split;
        }

        if self.range < 128 {
            self.renorm()?;
        }
        Ok(())
    }
    fn flush_run(&mut self, overflow: bool) -> EncoderResult<()> {
        if self.run > 0 {
            self.bw.write_byte(self.saved + (overflow as u8))?;
            if !overflow {
                for _ in 1..self.run {
                    self.bw.write_byte(0xFF)?;
                }
            } else {
                for _ in 1..self.run {
                    self.bw.write_byte(0)?;
                }
            }
            self.run = 0;
        }
        Ok(())
    }
    fn renorm(&mut self) -> EncoderResult<()> {
        let bits = (self.range.leading_zeros() & 7) as u8;
        self.range <<= bits;
        if self.bits + bits < 23 {
            self.bits += bits;
            self.val <<= bits;
        } else {
            for _ in 0..bits {
                if (self.bits == 23) && ((self.val >> 31) != 0) {
                    self.flush_run(true)?;
                }
                self.val <<= 1;
                self.bits += 1;
                if self.bits == 24 {
                    let tbyte = (self.val >> 24) as u8;
                    let nbyte = (self.val >> 16) as u8;
                    if tbyte < 0xFF {
                        self.flush_run(false)?;
                        if nbyte < 0xFE {
                            self.bw.write_byte(tbyte)?;
                        } else {
                            self.saved = tbyte;
                            self.run = 1;
                        }
                    } else {
                        self.run += 1;
                    }
                    self.val &= 0xFFFFFF;
                    self.bits -= 8;
                }
            }
        }
        Ok(())
    }
    pub fn flush(mut self) -> EncoderResult<()> {
        self.flush_run(false)?;
        self.val <<= 24 - self.bits;
        self.bw.write_u32be(self.val)?;
        Ok(())
    }

    pub fn put_bits(&mut self, val: u32, len: u8) -> EncoderResult<()> {
        let mut mask = 1 << (len - 1);
        while mask != 0 {
            self.put_bool((val & mask) != 0, 128)?;
            mask >>= 1;
        }
        Ok(())
    }
    pub fn put_probability(&mut self, prob: u8) -> EncoderResult<()> {
        self.put_bits(u32::from(prob >> 1), 7)
    }
    pub fn encode_probability(&mut self, new: u8, old: u8, prob: u8) -> EncoderResult<()> {
        self.put_bool(new != old, prob)?;
        if new != old {
            self.put_probability(new)?;
        }
        Ok(())
    }
    pub fn write_el<T: PartialEq>(&mut self, el: T, tree: &[TokenSeq<T>], probs: &[u8]) -> EncoderResult<()> {
        for entry in tree.iter() {
            if entry.val == el {
                for seq in entry.seq.iter() {
                    self.put_bool(seq.bit, probs[seq.idx as usize])?;
                }
                return Ok(());
            }
        }
        Err(EncoderError::Bug)
    }
}

pub struct Estimator {}

#[allow(dead_code)]
impl Estimator {
    pub fn new() -> Self { Self{} }
    pub fn write_el<T: PartialEq>(&self, el: T, tree: &[TokenSeq<T>], probs: &mut [ProbCounter]) {
        for entry in tree.iter() {
            if entry.val == el {
                for seq in entry.seq.iter() {
                    probs[seq.idx as usize].add(seq.bit);
                }
                return;
            }
        }
    }
    pub fn est_nits(bit: bool, prob: u8) -> u32 {
        if !bit {
            u32::from(PROB_BITS[prob as usize])
        } else {
            u32::from(PROB_BITS[256 - (prob as usize)])
        }
    }
    pub fn nits_to_bits(nits: u32) -> u32 { (nits + 7) >> 3 }
}