h263-based codecs: use proper motion compensation
[nihav.git] / nihav-codec-support / src / codecs / h263 / data.rs
CommitLineData
b4d5b851 1use nihav_core::io::codebook::CodebookDescReader;
cf64af13
KS
2
3#[allow(dead_code)]
4pub const H263_SCALES: &[u8] = &[
5 0, 1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15 ];
6
de919937
KS
7#[allow(dead_code)]
8pub const H263_DC_SCALES: &[u8] = &[
9 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62 ];
10
ef3209c7
KS
11pub const H263_SCAN_H: &[usize] = &[
12 0, 1, 2, 3, 8, 9, 16, 17,
13 10, 11, 4, 5, 6, 7, 15, 14,
14 13, 12, 19, 18, 24, 25, 32, 33,
15 26, 27, 20, 21, 22, 23, 28, 29,
16 30, 31, 34, 35, 40, 41, 48, 49,
17 42, 43, 36, 37, 38, 39, 44, 45,
18 46, 47, 50, 51, 56, 57, 58, 59,
19 52, 53, 54, 55, 60, 61, 62, 63
20];
21
22pub const H263_SCAN_V: &[usize] = &[
23 0, 8, 16, 24, 1, 9, 2, 10,
24 17, 25, 32, 40, 48, 56, 57, 49,
25 41, 33, 26, 18, 3, 11, 4, 12,
26 19, 27, 34, 42, 50, 58, 35, 43,
27 51, 59, 20, 28, 5, 13, 6, 14,
28 21, 29, 36, 44, 52, 60, 37, 45,
29 53, 61, 22, 30, 7, 15, 23, 31,
30 38, 46, 54, 62, 39, 47, 55, 63
31];
32
cf64af13
KS
33pub const H263_SIZES: &[(usize, usize)] = &[
34 (0, 0), (128, 96), (176, 144), (352, 288), (704, 576), (1408, 1152)
35];
36
37pub const H263_INTRA_MCBPC: &[(u8, u8)] = &[
38 (1, 1), (1, 3), (2, 3), (3, 3), (1, 4), (1, 6), (2, 6), (3, 6), (1, 9)
39];
40
41pub const H263_INTER_MCBPC: &[(u8, u8)] = &[
42 (1, 1), (3, 4), (2, 4), (5, 6), (3, 5), (4, 8), (3, 8), (3, 7),
43 (3, 3), (7, 7), (6, 7), (5, 9), (4, 6), (4, 9), (3, 9), (2, 9),
44 (2, 3), (5, 7), (4, 7), (5, 8), (1, 9), (0, 0), (0, 0), (0, 0),
45 (2, 11), (12, 13), (14, 13), (15, 13)
46];
47
48pub const H263_CBPY: &[(u8, u8)] = &[
49 ( 3, 4), ( 5, 5), ( 4, 5), ( 9, 4), ( 3, 5), ( 7, 4), ( 2, 6), (11, 4),
50 ( 2, 5), ( 3, 6), ( 5, 4), (10, 4), ( 4, 4), ( 8, 4), ( 6, 4), ( 3, 2)
51];
52
53pub const H263_MV: &[(u8, u8)] = &[
54 ( 1, 1), ( 1, 2), ( 1, 3), ( 1, 4), ( 3, 6), ( 5, 7), ( 4, 7), ( 3, 7),
55 (11, 9), (10, 9), ( 9, 9), (17, 10), (16, 10), (15, 10), (14, 10), (13, 10),
56 (12, 10), (11, 10), (10, 10), ( 9, 10), ( 8, 10), ( 7, 10), ( 6, 10), ( 5, 10),
57 ( 4, 10), ( 7, 11), ( 6, 11), ( 5, 11), ( 4, 11), ( 3, 11), ( 2, 11), ( 3, 12),
58 ( 2, 12)
59];
60
271cfde5
KS
61pub const H263_MBTYPE_B: &[(u8, u8)] = &[
62 (1, 1), (3, 3), (1, 5), (4, 4), (5, 4), (6, 6), (2, 4), (3, 4),
63 (7, 6), (4, 6), (5, 6), (1, 6), (1, 7), (1, 8), (1, 10)
64];
65
401b2b60
KS
66pub const H263_CHROMA_ROUND: [i16; 16] = [ 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1 ];
67
271cfde5
KS
68// 0x1 - direct, 0x2 - has quant, 0x4 - has CBP, 0x8 - has dquant, 0x10 - has fwd, 0x20 - has bwd, 0x40 - intra
69
70pub const H263_MBB_CAP_CODED: u8 = 0x2;
71pub const H263_MBB_CAP_DQUANT: u8 = 0x4;
72pub const H263_MBB_CAP_FORWARD: u8 = 0x10;
73pub const H263_MBB_CAP_BACKWARD: u8 = 0x20;
74pub const H263_MBB_CAP_INTRA: u8 = 0x80;
75
76pub const H263_MBTYPE_B_CAPS: &[u8] = &[
77 0x00, 0x02, 0x06, // skipped, direct, direct+dq
78 0x10, 0x12, 0x16, // forward, coded forward, forward+dq
79 0x20, 0x22, 0x26, // backward, coded backward, backward+dq
80 0x30, 0x32, 0x36, // bidir, coded bidir, bidir+dq
81 0x82, 0x86 // intra, intra+dq
82];
83
84pub const H263_CBPC_B: &[(u8, u8)] = &[
85 (0, 1), (2, 2), (7, 3), (6, 3)
86];
87
cf64af13
KS
88pub const H263_DQUANT_TAB: &[i8] = &[-1, -2, 1, 2];
89
43c8b55e
KS
90pub const H263_MODIFIED_QUANT: [[u8; 32]; 2] = [
91 [
92 0, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10, 11, 12, 13,
93 14, 15, 16, 17, 18, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28
94 ], [
95 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17,
96 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 29, 30, 31, 31, 31, 26
97 ]
98];
99
100pub const H263_CHROMA_QUANT: [u8; 32] = [
101 0, 1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 9, 10, 10, 11, 11,
102 12, 12, 12, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15
103];
104
cf64af13
KS
105pub struct H263ShortCodeReader { tab: &'static [(u8, u8)] }
106
107impl H263ShortCodeReader {
e243ceb4 108 pub fn new(tab: &'static [(u8, u8)]) -> Self { H263ShortCodeReader { tab } }
cf64af13
KS
109}
110
111impl CodebookDescReader<u8> for H263ShortCodeReader {
112 fn bits(&mut self, idx: usize) -> u8 { let (_, bits) = self.tab[idx]; bits }
113 fn code(&mut self, idx: usize) -> u32 { let (code, _) = self.tab[idx]; code as u32 }
114 fn sym (&mut self, idx: usize) -> u8 { idx as u8 }
115 fn len(&mut self) -> usize { self.tab.len() }
116}
117
118#[derive(Clone,Copy)]
119pub struct H263RLSym { run: u8, level: i8 }
120impl H263RLSym {
fdb4b2fb
KS
121 pub fn get_run(self) -> u8 { self.run }
122 pub fn is_last(self) -> bool { self.level < 0 }
123 pub fn is_escape(self) -> bool { (self.run == 0) && (self.level == 0) }
124 pub fn get_level(self) -> i16 { if self.level < 0 { -self.level as i16 } else { self.level as i16 } }
cf64af13
KS
125}
126
127pub struct H263RLCodeDesc { code: u8, bits: u8, sym: H263RLSym }
128
129macro_rules! rlcodes{
130 ($(($c:expr, $b:expr, $r:expr, $l:expr)),*) => {
131 &[$(H263RLCodeDesc{ code: $c, bits: $b, sym: H263RLSym{ run: $r, level: $l }}),*]
132 }
133}
134
135pub const H263_RL_CODES: &[H263RLCodeDesc] = rlcodes!(
136 (0x02, 2, 0, 1), (0x0F, 4, 0, 2), (0x15, 6, 0, 3), (0x17, 7, 0, 4),
137 (0x1F, 8, 0, 5), (0x25, 9, 0, 6), (0x24, 9, 0, 7), (0x21, 10, 0, 8),
138 (0x20, 10, 0, 9), (0x07, 11, 0, 10), (0x06, 11, 0, 11), (0x20, 11, 0, 12),
139 (0x06, 3, 1, 1), (0x14, 6, 1, 2), (0x1E, 8, 1, 3), (0x0F, 10, 1, 4),
140 (0x21, 11, 1, 5), (0x50, 12, 1, 6), (0x0E, 4, 2, 1), (0x1D, 8, 2, 2),
141 (0x0E, 10, 2, 3), (0x51, 12, 2, 4), (0x0D, 5, 3, 1), (0x23, 9, 3, 2),
142 (0x0D, 10, 3, 3), (0x0C, 5, 4, 1), (0x22, 9, 4, 2), (0x52, 12, 4, 3),
143 (0x0B, 5, 5, 1), (0x0C, 10, 5, 2), (0x53, 12, 5, 3), (0x13, 6, 6, 1),
144 (0x0B, 10, 6, 2), (0x54, 12, 6, 3), (0x12, 6, 7, 1), (0x0A, 10, 7, 2),
145 (0x11, 6, 8, 1), (0x09, 10, 8, 2), (0x10, 6, 9, 1), (0x08, 10, 9, 2),
146 (0x16, 7, 10, 1), (0x55, 12, 10, 2), (0x15, 7, 11, 1), (0x14, 7, 12, 1),
147 (0x1C, 8, 13, 1), (0x1B, 8, 14, 1), (0x21, 9, 15, 1), (0x20, 9, 16, 1),
148 (0x1F, 9, 17, 1), (0x1E, 9, 18, 1), (0x1D, 9, 19, 1), (0x1C, 9, 20, 1),
149 (0x1B, 9, 21, 1), (0x1A, 9, 22, 1), (0x22, 11, 23, 1), (0x23, 11, 24, 1),
150 (0x56, 12, 25, 1), (0x57, 12, 26, 1), (0x07, 4, 0, -1), (0x19, 9, 0, -2),
151 (0x05, 11, 0, -3), (0x0F, 6, 1, -1), (0x04, 11, 1, -2), (0x0E, 6, 2, -1),
152 (0x0D, 6, 3, -1), (0x0C, 6, 4, -1), (0x13, 7, 5, -1), (0x12, 7, 6, -1),
153 (0x11, 7, 7, -1), (0x10, 7, 8, -1), (0x1A, 8, 9, -1), (0x19, 8, 10, -1),
154 (0x18, 8, 11, -1), (0x17, 8, 12, -1), (0x16, 8, 13, -1), (0x15, 8, 14, -1),
155 (0x14, 8, 15, -1), (0x13, 8, 16, -1), (0x18, 9, 17, -1), (0x17, 9, 18, -1),
156 (0x16, 9, 19, -1), (0x15, 9, 20, -1), (0x14, 9, 21, -1), (0x13, 9, 22, -1),
157 (0x12, 9, 23, -1), (0x11, 9, 24, -1), (0x07, 10, 25, -1), (0x06, 10, 26, -1),
158 (0x05, 10, 27, -1), (0x04, 10, 28, -1), (0x24, 11, 29, -1), (0x25, 11, 30, -1),
159 (0x26, 11, 31, -1), (0x27, 11, 32, -1), (0x58, 12, 33, -1), (0x59, 12, 34, -1),
160 (0x5A, 12, 35, -1), (0x5B, 12, 36, -1), (0x5C, 12, 37, -1), (0x5D, 12, 38, -1),
161 (0x5E, 12, 39, -1), (0x5F, 12, 40, -1), (0x03, 7, 0, 0)
162);
163
164pub const H263_RL_CODES_AIC: &[H263RLCodeDesc] = rlcodes!(
165 (0x02, 2, 0, 1), (0x06, 3, 0, 2), (0x0E, 4, 0, 3), (0x0C, 5, 0, 4),
166 (0x0D, 5, 0, 5), (0x10, 6, 0, 6), (0x11, 6, 0, 7), (0x12, 6, 0, 8),
167 (0x16, 7, 0, 9), (0x1B, 8, 0, 10), (0x20, 9, 0, 11), (0x21, 9, 0, 12),
168 (0x1A, 9, 0, 13), (0x1B, 9, 0, 14), (0x1C, 9, 0, 15), (0x1D, 9, 0, 16),
169 (0x1E, 9, 0, 17), (0x1F, 9, 0, 18), (0x23, 11, 0, 19), (0x22, 11, 0, 20),
170 (0x57, 12, 0, 21), (0x56, 12, 0, 22), (0x55, 12, 0, 23), (0x54, 12, 0, 24),
171 (0x53, 12, 0, 25), (0x0F, 4, 1, 1), (0x14, 6, 1, 2), (0x14, 7, 1, 3),
172 (0x1E, 8, 1, 4), (0x0F, 10, 1, 5), (0x21, 11, 1, 6), (0x50, 12, 1, 7),
173 (0x0B, 5, 2, 1), (0x15, 7, 2, 2), (0x0E, 10, 2, 3), (0x09, 10, 2, 4),
174 (0x15, 6, 3, 1), (0x1D, 8, 3, 2), (0x0D, 10, 3, 3), (0x51, 12, 3, 4),
175 (0x13, 6, 4, 1), (0x23, 9, 4, 2), (0x07, 11, 4, 3), (0x17, 7, 5, 1),
176 (0x22, 9, 5, 2), (0x52, 12, 5, 3), (0x1C, 8, 6, 1), (0x0C, 10, 6, 2),
177 (0x1F, 8, 7, 1), (0x0B, 10, 7, 2), (0x25, 9, 8, 1), (0x0A, 10, 8, 2),
178 (0x24, 9, 9, 1), (0x06, 11, 9, 2), (0x21, 10, 10, 1), (0x20, 10, 11, 1),
179 (0x08, 10, 12, 1), (0x20, 11, 13, 1), (0x07, 4, 0, -1), (0x0C, 6, 0, -2),
180 (0x10, 7, 0, -3), (0x13, 8, 0, -4), (0x11, 9, 0, -5), (0x12, 9, 0, -6),
181 (0x04, 10, 0, -7), (0x27, 11, 0, -8), (0x26, 11, 0, -9), (0x5F, 12, 0,-10),
182 (0x0F, 6, 1, -1), (0x13, 9, 1, -2), (0x05, 10, 1, -3), (0x25, 11, 1, -4),
183 (0x0E, 6, 2, -1), (0x14, 9, 2, -2), (0x24, 11, 2, -3), (0x0D, 6, 3, -1),
184 (0x06, 10, 3, -2), (0x5E, 12, 3, -3), (0x11, 7, 4, -1), (0x07, 10, 4, -2),
185 (0x13, 7, 5, -1), (0x5D, 12, 5, -2), (0x12, 7, 6, -1), (0x5C, 12, 6, -2),
186 (0x14, 8, 7, -1), (0x5B, 12, 7, -2), (0x15, 8, 8, -1), (0x1A, 8, 9, -1),
187 (0x19, 8, 10, -1), (0x18, 8, 11, -1), (0x17, 8, 12, -1), (0x16, 8, 13, -1),
188 (0x19, 9, 14, -1), (0x15, 9, 15, -1), (0x16, 9, 16, -1), (0x18, 9, 17, -1),
189 (0x17, 9, 18, -1), (0x04, 11, 19, -1), (0x05, 11, 20, -1), (0x58, 12, 21, -1),
190 (0x59, 12, 22, -1), (0x5A, 12, 23, -1), (0x03, 7, 0, 0)
191);
192
193pub struct H263RLCodeReader { tab: &'static [H263RLCodeDesc] }
194
195impl H263RLCodeReader {
e243ceb4 196 pub fn new(tab: &'static [H263RLCodeDesc]) -> Self { H263RLCodeReader { tab } }
cf64af13
KS
197}
198
199impl CodebookDescReader<H263RLSym> for H263RLCodeReader {
200 fn bits(&mut self, idx: usize) -> u8 { self.tab[idx].bits }
201 fn code(&mut self, idx: usize) -> u32 { self.tab[idx].code as u32 }
202 fn sym (&mut self, idx: usize) -> H263RLSym { self.tab[idx].sym }
203 fn len(&mut self) -> usize { self.tab.len() }
204}
205