1 //! An implementation of the VP8 Video Codec
2 //!
3 //! This module contains a partial implementation of the
4 //! VP8 video format as defined in RFC-6386.
5 //!
6 //! It decodes Keyframes only sans Loop Filtering.
7 //! VP8 is the underpinning of the WebP image format
8 //!
9 //! # Related Links
10 //! * [rfc-6386](http://tools.ietf.org/html/rfc6386) - The VP8 Data Format and Decoding Guide
11 //! * [VP8.pdf](http://static.googleusercontent.com/media/research.google.com/en//pubs/archive/37073.pdf) - An overview of
12 //! of the VP8 format
13 //!
14
15 use byteorder::{LittleEndian, ReadBytesExt};
16 use std::default::Default;
17 use std::cmp;
18 use std::io::Read;
19
20 use super::transform;
21 use crate::{ImageError, ImageResult};
22
23 use crate::math::utils::clamp;
24
25 const MAX_SEGMENTS: usize = 4;
26 const NUM_DCT_TOKENS: usize = 12;
27
28 // Prediction modes
29 const DC_PRED: i8 = 0;
30 const V_PRED: i8 = 1;
31 const H_PRED: i8 = 2;
32 const TM_PRED: i8 = 3;
33 const B_PRED: i8 = 4;
34
35 const B_DC_PRED: i8 = 0;
36 const B_TM_PRED: i8 = 1;
37 const B_VE_PRED: i8 = 2;
38 const B_HE_PRED: i8 = 3;
39 const B_LD_PRED: i8 = 4;
40 const B_RD_PRED: i8 = 5;
41 const B_VR_PRED: i8 = 6;
42 const B_VL_PRED: i8 = 7;
43 const B_HD_PRED: i8 = 8;
44 const B_HU_PRED: i8 = 9;
45
46 // Prediction mode enum
47 #[repr(i8)]
48 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
49 enum LumaMode {
50 /// Predict DC using row above and column to the left.
51 DC = DC_PRED,
52
53 /// Predict rows using row above.
54 V = V_PRED,
55
56 /// Predict columns using column to the left.
57 H = H_PRED,
58
59 /// Propagate second differences.
60 TM = TM_PRED,
61
62 /// Each Y subblock is independently predicted.
63 B = B_PRED,
64 }
65
66 #[repr(i8)]
67 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
68 enum ChromaMode {
69 /// Predict DC using row above and column to the left.
70 DC = DC_PRED,
71
72 /// Predict rows using row above.
73 V = V_PRED,
74
75 /// Predict columns using column to the left.
76 H = H_PRED,
77
78 /// Propagate second differences.
79 TM = TM_PRED,
80 }
81
82 #[repr(i8)]
83 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
84 enum IntraMode {
85 DC = B_DC_PRED,
86 TM = B_TM_PRED,
87 VE = B_VE_PRED,
88 HE = B_HE_PRED,
89 LD = B_LD_PRED,
90 RD = B_RD_PRED,
91 VR = B_VR_PRED,
92 VL = B_VL_PRED,
93 HD = B_HD_PRED,
94 HU = B_HU_PRED,
95 }
96
97 type Prob = u8;
98
99 static SEGMENT_ID_TREE: [i8; 6] = [2, 4, -0, -1, -2, -3];
100
101 // Section 11.2
102 // Tree for determining the keyframe luma intra prediction modes:
103 static KEYFRAME_YMODE_TREE: [i8; 8] = [-B_PRED, 2, 4, 6, -DC_PRED, -V_PRED, -H_PRED, -TM_PRED];
104
105 // Default probabilities for decoding the keyframe luma modes
106 static KEYFRAME_YMODE_PROBS: [Prob; 4] = [145, 156, 163, 128];
107
108 // Tree for determining the keyframe B_PRED mode:
109 static KEYFRAME_BPRED_MODE_TREE: [i8; 18] = [
110 -B_DC_PRED, 2, -B_TM_PRED, 4, -B_VE_PRED, 6, 8, 12, -B_HE_PRED, 10, -B_RD_PRED, -B_VR_PRED,
111 -B_LD_PRED, 14, -B_VL_PRED, 16, -B_HD_PRED, -B_HU_PRED,
112 ];
113
114 // Probabilities for the BPRED_MODE_TREE
115 static KEYFRAME_BPRED_MODE_PROBS: [[[u8; 9]; 10]; 10] = [
116 [
117 [231, 120, 48, 89, 115, 113, 120, 152, 112],
118 [152, 179, 64, 126, 170, 118, 46, 70, 95],
119 [175, 69, 143, 80, 85, 82, 72, 155, 103],
120 [56, 58, 10, 171, 218, 189, 17, 13, 152],
121 [144, 71, 10, 38, 171, 213, 144, 34, 26],
122 [114, 26, 17, 163, 44, 195, 21, 10, 173],
123 [121, 24, 80, 195, 26, 62, 44, 64, 85],
124 [170, 46, 55, 19, 136, 160, 33, 206, 71],
125 [63, 20, 8, 114, 114, 208, 12, 9, 226],
126 [81, 40, 11, 96, 182, 84, 29, 16, 36],
127 ],
128 [
129 [134, 183, 89, 137, 98, 101, 106, 165, 148],
130 [72, 187, 100, 130, 157, 111, 32, 75, 80],
131 [66, 102, 167, 99, 74, 62, 40, 234, 128],
132 [41, 53, 9, 178, 241, 141, 26, 8, 107],
133 [104, 79, 12, 27, 217, 255, 87, 17, 7],
134 [74, 43, 26, 146, 73, 166, 49, 23, 157],
135 [65, 38, 105, 160, 51, 52, 31, 115, 128],
136 [87, 68, 71, 44, 114, 51, 15, 186, 23],
137 [47, 41, 14, 110, 182, 183, 21, 17, 194],
138 [66, 45, 25, 102, 197, 189, 23, 18, 22],
139 ],
140 [
141 [88, 88, 147, 150, 42, 46, 45, 196, 205],
142 [43, 97, 183, 117, 85, 38, 35, 179, 61],
143 [39, 53, 200, 87, 26, 21, 43, 232, 171],
144 [56, 34, 51, 104, 114, 102, 29, 93, 77],
145 [107, 54, 32, 26, 51, 1, 81, 43, 31],
146 [39, 28, 85, 171, 58, 165, 90, 98, 64],
147 [34, 22, 116, 206, 23, 34, 43, 166, 73],
148 [68, 25, 106, 22, 64, 171, 36, 225, 114],
149 [34, 19, 21, 102, 132, 188, 16, 76, 124],
150 [62, 18, 78, 95, 85, 57, 50, 48, 51],
151 ],
152 [
153 [193, 101, 35, 159, 215, 111, 89, 46, 111],
154 [60, 148, 31, 172, 219, 228, 21, 18, 111],
155 [112, 113, 77, 85, 179, 255, 38, 120, 114],
156 [40, 42, 1, 196, 245, 209, 10, 25, 109],
157 [100, 80, 8, 43, 154, 1, 51, 26, 71],
158 [88, 43, 29, 140, 166, 213, 37, 43, 154],
159 [61, 63, 30, 155, 67, 45, 68, 1, 209],
160 [142, 78, 78, 16, 255, 128, 34, 197, 171],
161 [41, 40, 5, 102, 211, 183, 4, 1, 221],
162 [51, 50, 17, 168, 209, 192, 23, 25, 82],
163 ],
164 [
165 [125, 98, 42, 88, 104, 85, 117, 175, 82],
166 [95, 84, 53, 89, 128, 100, 113, 101, 45],
167 [75, 79, 123, 47, 51, 128, 81, 171, 1],
168 [57, 17, 5, 71, 102, 57, 53, 41, 49],
169 [115, 21, 2, 10, 102, 255, 166, 23, 6],
170 [38, 33, 13, 121, 57, 73, 26, 1, 85],
171 [41, 10, 67, 138, 77, 110, 90, 47, 114],
172 [101, 29, 16, 10, 85, 128, 101, 196, 26],
173 [57, 18, 10, 102, 102, 213, 34, 20, 43],
174 [117, 20, 15, 36, 163, 128, 68, 1, 26],
175 ],
176 [
177 [138, 31, 36, 171, 27, 166, 38, 44, 229],
178 [67, 87, 58, 169, 82, 115, 26, 59, 179],
179 [63, 59, 90, 180, 59, 166, 93, 73, 154],
180 [40, 40, 21, 116, 143, 209, 34, 39, 175],
181 [57, 46, 22, 24, 128, 1, 54, 17, 37],
182 [47, 15, 16, 183, 34, 223, 49, 45, 183],
183 [46, 17, 33, 183, 6, 98, 15, 32, 183],
184 [65, 32, 73, 115, 28, 128, 23, 128, 205],
185 [40, 3, 9, 115, 51, 192, 18, 6, 223],
186 [87, 37, 9, 115, 59, 77, 64, 21, 47],
187 ],
188 [
189 [104, 55, 44, 218, 9, 54, 53, 130, 226],
190 [64, 90, 70, 205, 40, 41, 23, 26, 57],
191 [54, 57, 112, 184, 5, 41, 38, 166, 213],
192 [30, 34, 26, 133, 152, 116, 10, 32, 134],
193 [75, 32, 12, 51, 192, 255, 160, 43, 51],
194 [39, 19, 53, 221, 26, 114, 32, 73, 255],
195 [31, 9, 65, 234, 2, 15, 1, 118, 73],
196 [88, 31, 35, 67, 102, 85, 55, 186, 85],
197 [56, 21, 23, 111, 59, 205, 45, 37, 192],
198 [55, 38, 70, 124, 73, 102, 1, 34, 98],
199 ],
200 [
201 [102, 61, 71, 37, 34, 53, 31, 243, 192],
202 [69, 60, 71, 38, 73, 119, 28, 222, 37],
203 [68, 45, 128, 34, 1, 47, 11, 245, 171],
204 [62, 17, 19, 70, 146, 85, 55, 62, 70],
205 [75, 15, 9, 9, 64, 255, 184, 119, 16],
206 [37, 43, 37, 154, 100, 163, 85, 160, 1],
207 [63, 9, 92, 136, 28, 64, 32, 201, 85],
208 [86, 6, 28, 5, 64, 255, 25, 248, 1],
209 [56, 8, 17, 132, 137, 255, 55, 116, 128],
210 [58, 15, 20, 82, 135, 57, 26, 121, 40],
211 ],
212 [
213 [164, 50, 31, 137, 154, 133, 25, 35, 218],
214 [51, 103, 44, 131, 131, 123, 31, 6, 158],
215 [86, 40, 64, 135, 148, 224, 45, 183, 128],
216 [22, 26, 17, 131, 240, 154, 14, 1, 209],
217 [83, 12, 13, 54, 192, 255, 68, 47, 28],
218 [45, 16, 21, 91, 64, 222, 7, 1, 197],
219 [56, 21, 39, 155, 60, 138, 23, 102, 213],
220 [85, 26, 85, 85, 128, 128, 32, 146, 171],
221 [18, 11, 7, 63, 144, 171, 4, 4, 246],
222 [35, 27, 10, 146, 174, 171, 12, 26, 128],
223 ],
224 [
225 [190, 80, 35, 99, 180, 80, 126, 54, 45],
226 [85, 126, 47, 87, 176, 51, 41, 20, 32],
227 [101, 75, 128, 139, 118, 146, 116, 128, 85],
228 [56, 41, 15, 176, 236, 85, 37, 9, 62],
229 [146, 36, 19, 30, 171, 255, 97, 27, 20],
230 [71, 30, 17, 119, 118, 255, 17, 18, 138],
231 [101, 38, 60, 138, 55, 70, 43, 26, 142],
232 [138, 45, 61, 62, 219, 1, 81, 188, 64],
233 [32, 41, 20, 117, 151, 142, 20, 21, 163],
234 [112, 19, 12, 61, 195, 128, 48, 4, 24],
235 ],
236 ];
237
238 // Section 11.4 Tree for determining macroblock the chroma mode
239 static KEYFRAME_UV_MODE_TREE: [i8; 6] = [-DC_PRED, 2, -V_PRED, 4, -H_PRED, -TM_PRED];
240
241 // Probabilities for determining macroblock mode
242 static KEYFRAME_UV_MODE_PROBS: [Prob; 3] = [142, 114, 183];
243
244 // Section 13.4
245 type TokenProbTables = [[[[Prob; NUM_DCT_TOKENS - 1]; 3]; 8]; 4];
246
247 // Probabilities that a token's probability will be updated
248 static COEFF_UPDATE_PROBS: TokenProbTables = [
249 [
250 [
251 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
252 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
253 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
254 ],
255 [
256 [176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255],
257 [223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255],
258 [249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255],
259 ],
260 [
261 [255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255],
262 [234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
263 [253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
264 ],
265 [
266 [255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255],
267 [239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
268 [254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
269 ],
270 [
271 [255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255],
272 [251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
273 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
274 ],
275 [
276 [255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
277 [251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
278 [254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
279 ],
280 [
281 [255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255],
282 [250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255],
283 [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
284 ],
285 [
286 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
287 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
288 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
289 ],
290 ],
291 [
292 [
293 [217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
294 [225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255],
295 [234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255],
296 ],
297 [
298 [255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
299 [223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
300 [238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255],
301 ],
302 [
303 [255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255],
304 [249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
305 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
306 ],
307 [
308 [255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255],
309 [247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
310 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
311 ],
312 [
313 [255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
314 [252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
315 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
316 ],
317 [
318 [255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
319 [253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
320 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
321 ],
322 [
323 [255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255],
324 [250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
325 [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
326 ],
327 [
328 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
329 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
330 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
331 ],
332 ],
333 [
334 [
335 [186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255],
336 [234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255],
337 [251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255],
338 ],
339 [
340 [255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
341 [236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
342 [251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255],
343 ],
344 [
345 [255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
346 [254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
347 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
348 ],
349 [
350 [255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
351 [254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
352 [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
353 ],
354 [
355 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
356 [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
357 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
358 ],
359 [
360 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
361 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
362 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
363 ],
364 [
365 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
366 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
367 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
368 ],
369 [
370 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
371 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
372 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
373 ],
374 ],
375 [
376 [
377 [248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
378 [250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255],
379 [248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255],
380 ],
381 [
382 [255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255],
383 [246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255],
384 [252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255],
385 ],
386 [
387 [255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255],
388 [248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255],
389 [253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255],
390 ],
391 [
392 [255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255],
393 [245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255],
394 [253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
395 ],
396 [
397 [255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255],
398 [252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
399 [255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
400 ],
401 [
402 [255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255],
403 [249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
404 [255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
405 ],
406 [
407 [255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255],
408 [250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
409 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
410 ],
411 [
412 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
413 [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
414 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
415 ],
416 ],
417 ];
418
419 // Section 13.5
420 // Default Probabilities for tokens
421 static COEFF_PROBS: TokenProbTables = [
422 [
423 [
424 [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
425 [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
426 [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
427 ],
428 [
429 [253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128],
430 [189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128],
431 [106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128],
432 ],
433 [
434 [1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128],
435 [181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128],
436 [78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128],
437 ],
438 [
439 [1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128],
440 [184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128],
441 [77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128],
442 ],
443 [
444 [1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128],
445 [170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128],
446 [37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128],
447 ],
448 [
449 [1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128],
450 [207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128],
451 [102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128],
452 ],
453 [
454 [1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128],
455 [177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128],
456 [80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128],
457 ],
458 [
459 [1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
460 [246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
461 [255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
462 ],
463 ],
464 [
465 [
466 [198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62],
467 [131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1],
468 [68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128],
469 ],
470 [
471 [1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128],
472 [184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128],
473 [81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128],
474 ],
475 [
476 [1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128],
477 [99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128],
478 [23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128],
479 ],
480 [
481 [1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128],
482 [109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128],
483 [44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128],
484 ],
485 [
486 [1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128],
487 [94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128],
488 [22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128],
489 ],
490 [
491 [1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128],
492 [124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128],
493 [35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128],
494 ],
495 [
496 [1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128],
497 [121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128],
498 [45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128],
499 ],
500 [
501 [1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128],
502 [203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128],
503 [137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128],
504 ],
505 ],
506 [
507 [
508 [253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128],
509 [175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128],
510 [73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128],
511 ],
512 [
513 [1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128],
514 [239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128],
515 [155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128],
516 ],
517 [
518 [1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128],
519 [201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128],
520 [69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128],
521 ],
522 [
523 [1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128],
524 [223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128],
525 [141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128],
526 ],
527 [
528 [1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128],
529 [190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128],
530 [149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
531 ],
532 [
533 [1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128],
534 [247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128],
535 [240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128],
536 ],
537 [
538 [1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128],
539 [213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128],
540 [55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128],
541 ],
542 [
543 [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
544 [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
545 [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
546 ],
547 ],
548 [
549 [
550 [202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255],
551 [126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128],
552 [61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128],
553 ],
554 [
555 [1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128],
556 [166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128],
557 [39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128],
558 ],
559 [
560 [1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128],
561 [124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128],
562 [24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128],
563 ],
564 [
565 [1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128],
566 [149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128],
567 [28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128],
568 ],
569 [
570 [1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128],
571 [123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128],
572 [20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128],
573 ],
574 [
575 [1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128],
576 [168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128],
577 [47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128],
578 ],
579 [
580 [1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128],
581 [141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128],
582 [42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128],
583 ],
584 [
585 [1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
586 [244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
587 [238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
588 ],
589 ],
590 ];
591
592 // DCT Tokens
593 const DCT_0: i8 = 0;
594 const DCT_1: i8 = 1;
595 const DCT_2: i8 = 2;
596 const DCT_3: i8 = 3;
597 const DCT_4: i8 = 4;
598 const DCT_CAT1: i8 = 5;
599 const DCT_CAT2: i8 = 6;
600 const DCT_CAT3: i8 = 7;
601 const DCT_CAT4: i8 = 8;
602 const DCT_CAT5: i8 = 9;
603 const DCT_CAT6: i8 = 10;
604 const DCT_EOB: i8 = 11;
605
606 static DCT_TOKEN_TREE: [i8; 22] = [
607 -DCT_EOB, 2, -DCT_0, 4, -DCT_1, 6, 8, 12, -DCT_2, 10, -DCT_3, -DCT_4, 14, 16, -DCT_CAT1,
608 -DCT_CAT2, 18, 20, -DCT_CAT3, -DCT_CAT4, -DCT_CAT5, -DCT_CAT6,
609 ];
610
611 static PROB_DCT_CAT: [[Prob; 12]; 6] = [
612 [159, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
613 [165, 145, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
614 [173, 148, 140, 0, 0, 0, 0, 0, 0, 0, 0, 0],
615 [176, 155, 140, 135, 0, 0, 0, 0, 0, 0, 0, 0],
616 [180, 157, 141, 134, 130, 0, 0, 0, 0, 0, 0, 0],
617 [254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0],
618 ];
619
620 static DCT_CAT_BASE: [u8; 6] = [5, 7, 11, 19, 35, 67];
621 static COEFF_BANDS: [u8; 16] = [0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7];
622
623 #[rustfmt::skip]
624 static DC_QUANT: [i16; 128] = [
625 4, 5, 6, 7, 8, 9, 10, 10,
626 11, 12, 13, 14, 15, 16, 17, 17,
627 18, 19, 20, 20, 21, 21, 22, 22,
628 23, 23, 24, 25, 25, 26, 27, 28,
629 29, 30, 31, 32, 33, 34, 35, 36,
630 37, 37, 38, 39, 40, 41, 42, 43,
631 44, 45, 46, 46, 47, 48, 49, 50,
632 51, 52, 53, 54, 55, 56, 57, 58,
633 59, 60, 61, 62, 63, 64, 65, 66,
634 67, 68, 69, 70, 71, 72, 73, 74,
635 75, 76, 76, 77, 78, 79, 80, 81,
636 82, 83, 84, 85, 86, 87, 88, 89,
637 91, 93, 95, 96, 98, 100, 101, 102,
638 104, 106, 108, 110, 112, 114, 116, 118,
639 122, 124, 126, 128, 130, 132, 134, 136,
640 138, 140, 143, 145, 148, 151, 154, 157,
641 ];
642
643 #[rustfmt::skip]
644 static AC_QUANT: [i16; 128] = [
645 4, 5, 6, 7, 8, 9, 10, 11,
646 12, 13, 14, 15, 16, 17, 18, 19,
647 20, 21, 22, 23, 24, 25, 26, 27,
648 28, 29, 30, 31, 32, 33, 34, 35,
649 36, 37, 38, 39, 40, 41, 42, 43,
650 44, 45, 46, 47, 48, 49, 50, 51,
651 52, 53, 54, 55, 56, 57, 58, 60,
652 62, 64, 66, 68, 70, 72, 74, 76,
653 78, 80, 82, 84, 86, 88, 90, 92,
654 94, 96, 98, 100, 102, 104, 106, 108,
655 110, 112, 114, 116, 119, 122, 125, 128,
656 131, 134, 137, 140, 143, 146, 149, 152,
657 155, 158, 161, 164, 167, 170, 173, 177,
658 181, 185, 189, 193, 197, 201, 205, 209,
659 213, 217, 221, 225, 229, 234, 239, 245,
660 249, 254, 259, 264, 269, 274, 279, 284,
661 ];
662
663 static ZIGZAG: [u8; 16] = [0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15];
664
665 struct BoolReader {
666 buf: Vec<u8>,
667 index: usize,
668
669 range: u32,
670 value: u32,
671 bit_count: u8,
672 }
673
674 impl BoolReader {
new() -> BoolReader675 pub(crate) fn new() -> BoolReader {
676 BoolReader {
677 buf: Vec::new(),
678 range: 0,
679 value: 0,
680 bit_count: 0,
681 index: 0,
682 }
683 }
684
init(&mut self, buf: Vec<u8>) -> ImageResult<()>685 pub(crate) fn init(&mut self, buf: Vec<u8>) -> ImageResult<()> {
686 if buf.len() < 2 {
687 return Err(ImageError::FormatError(
688 "Expected at least 2 bytes of decoder initialization data".into()));
689 }
690
691 self.buf = buf;
692 // Direct access safe, since length has just been validated.
693 self.value = (u32::from(self.buf[0]) << 8) | u32::from(self.buf[1]);
694 self.index = 2;
695 self.range = 255;
696 self.bit_count = 0;
697
698 Ok(())
699 }
700
read_bool(&mut self, probability: u8) -> bool701 pub(crate) fn read_bool(&mut self, probability: u8) -> bool {
702 let split = 1 + (((self.range - 1) * u32::from(probability)) >> 8);
703 let bigsplit = split << 8;
704
705 let retval = if self.value >= bigsplit {
706 self.range -= split;
707 self.value -= bigsplit;
708 true
709 } else {
710 self.range = split;
711 false
712 };
713
714 while self.range < 128 {
715 self.value <<= 1;
716 self.range <<= 1;
717 self.bit_count += 1;
718
719 if self.bit_count == 8 {
720 self.bit_count = 0;
721
722 // If no more bits are available, just don't do anything.
723 // This strategy is suggested in the reference implementation of RFC6386 (p.135)
724 if self.index < self.buf.len() {
725 self.value |= u32::from(self.buf[self.index]);
726 self.index += 1;
727 }
728 }
729 }
730
731 retval
732 }
733
read_literal(&mut self, n: u8) -> u8734 pub(crate) fn read_literal(&mut self, n: u8) -> u8 {
735 let mut v = 0u8;
736 let mut n = n;
737
738 while n != 0 {
739 v = (v << 1) + self.read_bool(128u8) as u8;
740 n -= 1;
741 }
742
743 v
744 }
745
read_magnitude_and_sign(&mut self, n: u8) -> i32746 pub(crate) fn read_magnitude_and_sign(&mut self, n: u8) -> i32 {
747 let magnitude = self.read_literal(n);
748 let sign = self.read_literal(1);
749
750 if sign == 1 {
751 -i32::from(magnitude)
752 } else {
753 i32::from(magnitude)
754 }
755 }
756
read_with_tree(&mut self, tree: &[i8], probs: &[Prob], start: isize) -> i8757 pub(crate) fn read_with_tree(&mut self, tree: &[i8], probs: &[Prob], start: isize) -> i8 {
758 let mut index = start;
759
760 loop {
761 let a = self.read_bool(probs[index as usize >> 1]);
762 let b = index + a as isize;
763 index = tree[b as usize] as isize;
764
765 if index <= 0 {
766 break;
767 }
768 }
769
770 -index as i8
771 }
772
read_flag(&mut self) -> bool773 pub(crate) fn read_flag(&mut self) -> bool {
774 0 != self.read_literal(1)
775 }
776 }
777
778 #[derive(Default, Clone, Copy)]
779 struct MacroBlock {
780 bpred: [IntraMode; 16],
781 complexity: [u8; 9],
782 luma_mode: LumaMode,
783 chroma_mode: ChromaMode,
784 segmentid: u8,
785 }
786
787 /// A Representation of the last decoded video frame
788 #[derive(Default, Debug, Clone)]
789 pub struct Frame {
790 /// The width of the luma plane
791 pub width: u16,
792
793 /// The height of the luma plane
794 pub height: u16,
795
796 /// The luma plane of the frame
797 pub ybuf: Vec<u8>,
798
799 /// Indicates whether this frame is a keyframe
800 pub keyframe: bool,
801
802 version: u8,
803
804 /// Indicates whether this frame is intended for display
805 pub for_display: bool,
806
807 // Section 9.2
808 /// The pixel type of the frame as defined by Section 9.2
809 /// of the VP8 Specification
810 pub pixel_type: u8,
811
812 // Section 9.4 and 15
813 filter: u8,
814 filter_level: u8,
815 sharpness_level: u8,
816 }
817
818 #[derive(Clone, Copy, Default)]
819 struct Segment {
820 ydc: i16,
821 yac: i16,
822
823 y2dc: i16,
824 y2ac: i16,
825
826 uvdc: i16,
827 uvac: i16,
828
829 delta_values: bool,
830
831 quantizer_level: i8,
832 loopfilter_level: i8,
833 }
834
835 /// VP8 Decoder
836 ///
837 /// Only decodes keyframes
838 pub struct Vp8Decoder<R> {
839 r: R,
840 b: BoolReader,
841
842 mbwidth: u16,
843 mbheight: u16,
844
845 frame: Frame,
846
847 segments_enabled: bool,
848 segments_update_map: bool,
849 segment: [Segment; MAX_SEGMENTS],
850
851 partitions: [BoolReader; 8],
852 num_partitions: u8,
853
854 segment_tree_probs: [Prob; 3],
855 token_probs: Box<TokenProbTables>,
856
857 // Section 9.10
858 prob_intra: Prob,
859
860 // Section 9.11
861 prob_skip_false: Option<Prob>,
862
863 top: Vec<MacroBlock>,
864 left: MacroBlock,
865
866 top_border: Vec<u8>,
867 left_border: Vec<u8>,
868 }
869
870 impl<R: Read> Vp8Decoder<R> {
871 /// Create a new decoder.
872 /// The reader must present a raw vp8 bitstream to the decoder
new(r: R) -> Vp8Decoder<R>873 pub fn new(r: R) -> Vp8Decoder<R> {
874 let f = Frame::default();
875 let s = Segment::default();
876 let m = MacroBlock::default();
877
878 Vp8Decoder {
879 r,
880 b: BoolReader::new(),
881
882 mbwidth: 0,
883 mbheight: 0,
884
885 frame: f,
886 segments_enabled: false,
887 segments_update_map: false,
888 segment: [s; MAX_SEGMENTS],
889
890 partitions: [
891 BoolReader::new(),
892 BoolReader::new(),
893 BoolReader::new(),
894 BoolReader::new(),
895 BoolReader::new(),
896 BoolReader::new(),
897 BoolReader::new(),
898 BoolReader::new(),
899 ],
900
901 num_partitions: 1,
902
903 segment_tree_probs: [255u8; 3],
904 token_probs: Box::new(COEFF_PROBS),
905
906 // Section 9.10
907 prob_intra: 0u8,
908
909 // Section 9.11
910 prob_skip_false: None,
911
912 top: Vec::new(),
913 left: m,
914
915 top_border: Vec::new(),
916 left_border: Vec::new(),
917 }
918 }
919
update_token_probabilities(&mut self)920 fn update_token_probabilities(&mut self) {
921 for (i, is) in COEFF_UPDATE_PROBS.iter().enumerate() {
922 for (j, js) in is.iter().enumerate() {
923 for (k, ks) in js.iter().enumerate() {
924 for (t, prob) in ks.iter().enumerate().take(NUM_DCT_TOKENS - 1) {
925 if self.b.read_bool(*prob) {
926 let v = self.b.read_literal(8);
927 self.token_probs[i][j][k][t] = v;
928 }
929 }
930 }
931 }
932 }
933 }
934
init_partitions(&mut self, n: usize) -> ImageResult<()>935 fn init_partitions(&mut self, n: usize) -> ImageResult<()> {
936 if n > 1 {
937 let mut sizes = vec![0; 3 * n - 3];
938 self.r.read_exact(sizes.as_mut_slice())?;
939
940 for (i, s) in sizes.chunks(3).enumerate() {
941 let size = {s}.read_u24::<LittleEndian>()
942 .expect("Reading from &[u8] can't fail and the chunk is complete");
943
944 let mut buf = vec![0; size as usize];
945 self.r.read_exact(buf.as_mut_slice())?;
946
947 self.partitions[i].init(buf)?;
948 }
949 }
950
951 let mut buf = Vec::new();
952 self.r.read_to_end(&mut buf)?;
953 self.partitions[n - 1].init(buf)?;
954
955 Ok(())
956 }
957
read_quantization_indices(&mut self)958 fn read_quantization_indices(&mut self) {
959 fn dc_quant(index: i32) -> i16 {
960 DC_QUANT[clamp(index, 0, 127) as usize]
961 }
962
963 fn ac_quant(index: i32) -> i16 {
964 AC_QUANT[clamp(index, 0, 127) as usize]
965 }
966
967 let yac_abs = self.b.read_literal(7);
968 let ydc_delta = if self.b.read_flag() {
969 self.b.read_magnitude_and_sign(4)
970 } else {
971 0
972 };
973
974 let y2dc_delta = if self.b.read_flag() {
975 self.b.read_magnitude_and_sign(4)
976 } else {
977 0
978 };
979
980 let y2ac_delta = if self.b.read_flag() {
981 self.b.read_magnitude_and_sign(4)
982 } else {
983 0
984 };
985
986 let uvdc_delta = if self.b.read_flag() {
987 self.b.read_magnitude_and_sign(4)
988 } else {
989 0
990 };
991
992 let uvac_delta = if self.b.read_flag() {
993 self.b.read_magnitude_and_sign(4)
994 } else {
995 0
996 };
997
998 let n = if self.segments_enabled {
999 MAX_SEGMENTS
1000 } else {
1001 1
1002 };
1003 for i in 0usize..n {
1004 let base = i32::from(if !self.segment[i].delta_values {
1005 i16::from(self.segment[i].quantizer_level)
1006 } else {
1007 i16::from(self.segment[i].quantizer_level) + i16::from(yac_abs)
1008 });
1009
1010 self.segment[i].ydc = dc_quant(base + ydc_delta);
1011 self.segment[i].yac = ac_quant(base);
1012
1013 self.segment[i].y2dc = dc_quant(base + y2dc_delta) * 2;
1014 // The intermediate result (max`284*155`) can be larger than the `i16` range.
1015 self.segment[i].y2ac = (i32::from(ac_quant(base + y2ac_delta)) * 155 / 100) as i16;
1016
1017 self.segment[i].uvdc = dc_quant(base + uvdc_delta);
1018 self.segment[i].uvac = ac_quant(base + uvac_delta);
1019
1020 if self.segment[i].y2ac < 8 {
1021 self.segment[i].y2ac = 8;
1022 }
1023
1024 if self.segment[i].uvdc > 132 {
1025 self.segment[i].uvdc = 132;
1026 }
1027 }
1028 }
1029
read_loop_filter_adjustments(&mut self)1030 fn read_loop_filter_adjustments(&mut self) {
1031 if self.b.read_flag() {
1032 for _i in 0usize..4 {
1033 let ref_frame_delta_update_flag = self.b.read_flag();
1034
1035 let _delta = if ref_frame_delta_update_flag {
1036 self.b.read_magnitude_and_sign(6)
1037 } else {
1038 0i32
1039 };
1040 }
1041
1042 for _i in 0usize..4 {
1043 let mb_mode_delta_update_flag = self.b.read_flag();
1044
1045 let _delta = if mb_mode_delta_update_flag {
1046 self.b.read_magnitude_and_sign(6)
1047 } else {
1048 0i32
1049 };
1050 }
1051 }
1052 }
1053
read_segment_updates(&mut self)1054 fn read_segment_updates(&mut self) {
1055 // Section 9.3
1056 self.segments_update_map = self.b.read_flag();
1057 let update_segment_feature_data = self.b.read_flag();
1058
1059 if update_segment_feature_data {
1060 let segment_feature_mode = self.b.read_flag();
1061
1062 for i in 0usize..MAX_SEGMENTS {
1063 self.segment[i].delta_values = !segment_feature_mode;
1064 }
1065
1066 for i in 0usize..MAX_SEGMENTS {
1067 let update = self.b.read_flag();
1068
1069 self.segment[i].quantizer_level = if update {
1070 self.b.read_magnitude_and_sign(7)
1071 } else {
1072 0i32
1073 } as i8;
1074 }
1075
1076 for i in 0usize..MAX_SEGMENTS {
1077 let update = self.b.read_flag();
1078
1079 self.segment[i].loopfilter_level = if update {
1080 self.b.read_magnitude_and_sign(6)
1081 } else {
1082 0i32
1083 } as i8;
1084 }
1085 }
1086
1087 if self.segments_update_map {
1088 for i in 0usize..3 {
1089 let update = self.b.read_flag();
1090
1091 self.segment_tree_probs[i] = if update { self.b.read_literal(8) } else { 255 };
1092 }
1093 }
1094 }
1095
read_frame_header(&mut self) -> ImageResult<()>1096 fn read_frame_header(&mut self) -> ImageResult<()> {
1097 let tag = self.r.read_u24::<LittleEndian>()?;
1098
1099 self.frame.keyframe = tag & 1 == 0;
1100 self.frame.version = ((tag >> 1) & 7) as u8;
1101 self.frame.for_display = (tag >> 4) & 1 != 0;
1102
1103 let first_partition_size = tag >> 5;
1104
1105 if self.frame.keyframe {
1106 let mut tag = [0u8; 3];
1107 self.r.read_exact(&mut tag)?;
1108
1109 if tag != [0x9d, 0x01, 0x2a] {
1110 return Err(ImageError::FormatError(
1111 format!("Invalid magic bytes {:?} for vp8", tag)))
1112 }
1113
1114 let w = self.r.read_u16::<LittleEndian>()?;
1115 let h = self.r.read_u16::<LittleEndian>()?;
1116
1117 self.frame.width = w & 0x3FFF;
1118 self.frame.height = h & 0x3FFF;
1119
1120 self.top = init_top_macroblocks(self.frame.width as usize);
1121 // Almost always the first macro block, except when non exists (i.e. `width == 0`)
1122 self.left = self.top.get(0).cloned()
1123 .unwrap_or_else(MacroBlock::default);
1124
1125 self.mbwidth = (self.frame.width + 15) / 16;
1126 self.mbheight = (self.frame.height + 15) / 16;
1127
1128 self.frame.ybuf = vec![0u8; self.frame.width as usize * self.frame.height as usize];
1129
1130 self.top_border = vec![127u8; self.frame.width as usize + 4 + 16];
1131 self.left_border = vec![129u8; 1 + 16];
1132 }
1133
1134 let mut buf = vec![0; first_partition_size as usize];
1135 self.r.read_exact(&mut buf)?;
1136
1137 // initialise binary decoder
1138 self.b.init(buf)?;
1139
1140 if self.frame.keyframe {
1141 let color_space = self.b.read_literal(1);
1142 self.frame.pixel_type = self.b.read_literal(1);
1143
1144 if color_space != 0 {
1145 return Err(ImageError::FormatError(
1146 "Only YUV color space is specified.".to_string()))
1147 }
1148 }
1149
1150 self.segments_enabled = self.b.read_flag();
1151 if self.segments_enabled {
1152 self.read_segment_updates();
1153 }
1154
1155 self.frame.filter = self.b.read_literal(1);
1156 self.frame.filter_level = self.b.read_literal(6);
1157 self.frame.sharpness_level = self.b.read_literal(3);
1158
1159 let lf_adjust_enable = self.b.read_flag();
1160 if lf_adjust_enable {
1161 self.read_loop_filter_adjustments();
1162 }
1163
1164 self.num_partitions = (1usize << self.b.read_literal(2) as usize) as u8;
1165 let num_partitions = self.num_partitions as usize;
1166 self.init_partitions(num_partitions)?;
1167
1168 self.read_quantization_indices();
1169
1170 if !self.frame.keyframe {
1171 // 9.7 refresh golden frame and altref frame
1172 return Err(ImageError::UnsupportedError(
1173 "Frames that are not keyframes are not supported".into()))
1174 // FIXME: support this?
1175 } else {
1176 // Refresh entropy probs ?????
1177 let _ = self.b.read_literal(1);
1178 }
1179
1180 self.update_token_probabilities();
1181
1182 let mb_no_skip_coeff = self.b.read_literal(1);
1183 self.prob_skip_false = if mb_no_skip_coeff == 1 {
1184 Some(self.b.read_literal(8))
1185 } else {
1186 None
1187 };
1188
1189 if !self.frame.keyframe {
1190 // 9.10 remaining frame data
1191 self.prob_intra = 0;
1192
1193 return Err(ImageError::UnsupportedError(
1194 "Frames that are not keyframes are not supported".into()))
1195 // FIXME: support this?
1196 } else {
1197 // Reset motion vectors
1198 }
1199
1200 Ok(())
1201 }
1202
read_macroblock_header(&mut self, mbx: usize) -> ImageResult<(bool, MacroBlock)>1203 fn read_macroblock_header(&mut self, mbx: usize) -> ImageResult<(bool, MacroBlock)> {
1204 let mut mb = MacroBlock::default();
1205
1206 mb.segmentid = if self.segments_enabled && self.segments_update_map {
1207 self.b
1208 .read_with_tree(&SEGMENT_ID_TREE, &self.segment_tree_probs, 0) as u8
1209 } else {
1210 0
1211 };
1212
1213 let skip_coeff = if self.prob_skip_false.is_some() {
1214 self.b.read_bool(*self.prob_skip_false.as_ref().unwrap())
1215 } else {
1216 false
1217 };
1218
1219 let inter_predicted = if !self.frame.keyframe {
1220 self.b.read_bool(self.prob_intra)
1221 } else {
1222 false
1223 };
1224
1225 if inter_predicted {
1226 return Err(ImageError::UnsupportedError(
1227 "VP8 inter prediction is not implemented yet".into()));
1228 }
1229
1230 if self.frame.keyframe {
1231 // intra prediction
1232 let luma = self.b
1233 .read_with_tree(&KEYFRAME_YMODE_TREE, &KEYFRAME_YMODE_PROBS, 0);
1234 mb.luma_mode = LumaMode::from_i8(luma)
1235 .ok_or_else(|| ImageError::FormatError(
1236 format!("Invalid luma prediction mode {}", luma))
1237 )?;
1238
1239 match mb.luma_mode.into_intra() {
1240 // `LumaMode::B` - This is predicted individually
1241 None => {
1242 for y in 0usize..4 {
1243 for x in 0usize..4 {
1244 let top = self.top[mbx].bpred[12 + x];
1245 let left = self.left.bpred[y];
1246 let intra = self.b.read_with_tree(
1247 &KEYFRAME_BPRED_MODE_TREE,
1248 &KEYFRAME_BPRED_MODE_PROBS[top as usize][left as usize],
1249 0,
1250 );
1251 let bmode = IntraMode::from_i8(intra)
1252 .ok_or_else(|| ImageError::FormatError(
1253 format!("Invalid intra prediction mode {}", intra))
1254 )?;
1255 mb.bpred[x + y * 4] = bmode;
1256
1257 self.top[mbx].bpred[12 + x] = bmode;
1258 self.left.bpred[y] = bmode;
1259 }
1260 }
1261 },
1262 Some(mode) => {
1263 for i in 0usize..4 {
1264 mb.bpred[12 + i] = mode;
1265 self.left.bpred[i] = mode;
1266 }
1267 }
1268 }
1269
1270 let chroma = self.b
1271 .read_with_tree(&KEYFRAME_UV_MODE_TREE, &KEYFRAME_UV_MODE_PROBS, 0);
1272 mb.chroma_mode = ChromaMode::from_i8(chroma)
1273 .ok_or_else(|| ImageError::FormatError(
1274 format!("Invalid chroma prediction mode {}", chroma))
1275 )?;
1276 }
1277
1278 self.top[mbx].chroma_mode = mb.chroma_mode;
1279 self.top[mbx].luma_mode = mb.luma_mode;
1280 self.top[mbx].bpred = mb.bpred;
1281
1282 Ok((skip_coeff, mb))
1283 }
1284
intra_predict(&mut self, mbx: usize, mby: usize, mb: &MacroBlock, resdata: &[i32])1285 fn intra_predict(&mut self, mbx: usize, mby: usize, mb: &MacroBlock, resdata: &[i32]) {
1286 let stride = 1usize + 16 + 4;
1287 let w = self.frame.width as usize;
1288 let mw = self.mbwidth as usize;
1289 let mut ws = create_border(mbx, mby, mw, &self.top_border, &self.left_border);
1290
1291 match mb.luma_mode {
1292 LumaMode::V => predict_vpred(&mut ws, 16, 1, 1, stride),
1293 LumaMode::H => predict_hpred(&mut ws, 16, 1, 1, stride),
1294 LumaMode::TM => predict_tmpred(&mut ws, 16, 1, 1, stride),
1295 LumaMode::DC => predict_dcpred(&mut ws, 16, stride, mby != 0, mbx != 0),
1296 LumaMode::B => predict_4x4(&mut ws, stride, &mb.bpred, resdata),
1297 }
1298
1299 if mb.luma_mode != LumaMode::B {
1300 for y in 0usize..4 {
1301 for x in 0usize..4 {
1302 let i = x + y * 4;
1303 let rb = &resdata[i * 16..i * 16 + 16];
1304 let y0 = 1 + y * 4;
1305 let x0 = 1 + x * 4;
1306
1307 add_residue(&mut ws, rb, y0, x0, stride);
1308 }
1309 }
1310 }
1311
1312 self.left_border[0] = ws[16];
1313
1314 for i in 0usize..16 {
1315 self.top_border[mbx * 16 + i] = ws[16 * stride + 1 + i];
1316 self.left_border[i + 1] = ws[(i + 1) * stride + 16];
1317 }
1318
1319 // Length is the remainder to the border, but maximally the current chunk.
1320 let ylength = cmp::min(self.frame.height as usize - mby*16, 16);
1321 let xlength = cmp::min(self.frame.width as usize - mbx*16, 16);
1322
1323 for y in 0usize..ylength {
1324 for x in 0usize..xlength {
1325 self.frame.ybuf[(mby * 16 + y) * w + mbx * 16 + x] = ws[(1 + y) * stride + 1 + x];
1326 }
1327 }
1328 }
1329
read_coefficients( &mut self, block: &mut [i32], p: usize, plane: usize, complexity: usize, dcq: i16, acq: i16, ) -> bool1330 fn read_coefficients(
1331 &mut self,
1332 block: &mut [i32],
1333 p: usize,
1334 plane: usize,
1335 complexity: usize,
1336 dcq: i16,
1337 acq: i16,
1338 ) -> bool {
1339 let first = if plane == 0 { 1usize } else { 0usize };
1340 let probs = &self.token_probs[plane];
1341 let tree = &DCT_TOKEN_TREE;
1342
1343 let mut complexity = complexity;
1344 let mut has_coefficients = false;
1345 let mut skip = false;
1346
1347 for i in first..16usize {
1348 let table = &probs[COEFF_BANDS[i] as usize][complexity];
1349
1350 let token = if !skip {
1351 self.partitions[p].read_with_tree(tree, table, 0)
1352 } else {
1353 self.partitions[p].read_with_tree(tree, table, 2)
1354 };
1355
1356 let mut abs_value = i32::from(match token {
1357 DCT_EOB => break,
1358
1359 DCT_0 => {
1360 skip = true;
1361 has_coefficients = true;
1362 complexity = 0;
1363 continue;
1364 }
1365
1366 literal @ DCT_1..=DCT_4 => i16::from(literal),
1367
1368 category @ DCT_CAT1..=DCT_CAT6 => {
1369 let t = PROB_DCT_CAT[(category - DCT_CAT1) as usize];
1370
1371 let mut extra = 0i16;
1372 let mut j = 0;
1373
1374 while t[j] > 0 {
1375 extra = extra + extra + self.partitions[p].read_bool(t[j]) as i16;
1376 j += 1;
1377 }
1378
1379 i16::from(DCT_CAT_BASE[(category - DCT_CAT1) as usize]) + extra
1380 }
1381
1382 c => panic!(format!("unknown token: {}", c)),
1383 });
1384
1385 skip = false;
1386
1387 complexity = if abs_value == 0 {
1388 0
1389 } else if abs_value == 1 {
1390 1
1391 } else {
1392 2
1393 };
1394
1395 if self.partitions[p].read_bool(128) {
1396 abs_value = -abs_value;
1397 }
1398
1399 block[ZIGZAG[i] as usize] =
1400 abs_value * i32::from(if ZIGZAG[i] > 0 { acq } else { dcq });
1401
1402 has_coefficients = true;
1403 }
1404
1405 has_coefficients
1406 }
1407
read_residual_data(&mut self, mb: &MacroBlock, mbx: usize, p: usize) -> [i32; 384]1408 fn read_residual_data(&mut self, mb: &MacroBlock, mbx: usize, p: usize) -> [i32; 384] {
1409 let sindex = mb.segmentid as usize;
1410 let mut blocks = [0i32; 384];
1411 let mut plane = if mb.luma_mode == LumaMode::B { 3 } else { 1 };
1412
1413 if plane == 1 {
1414 let complexity = self.top[mbx].complexity[0] + self.left.complexity[0];
1415 let mut block = [0i32; 16];
1416 let dcq = self.segment[sindex].y2dc;
1417 let acq = self.segment[sindex].y2ac;
1418 let n = self.read_coefficients(&mut block, p, plane, complexity as usize, dcq, acq);
1419
1420 self.left.complexity[0] = if n { 1 } else { 0 };
1421 self.top[mbx].complexity[0] = if n { 1 } else { 0 };
1422
1423 transform::iwht4x4(&mut block);
1424
1425 for k in 0usize..16 {
1426 blocks[16 * k] = block[k];
1427 }
1428
1429 plane = 0;
1430 }
1431
1432 for y in 0usize..4 {
1433 let mut left = self.left.complexity[y + 1];
1434 for x in 0usize..4 {
1435 let i = x + y * 4;
1436 let block = &mut blocks[i * 16..i * 16 + 16];
1437
1438 let complexity = self.top[mbx].complexity[x + 1] + left;
1439 let dcq = self.segment[sindex].ydc;
1440 let acq = self.segment[sindex].yac;
1441
1442 let n = self.read_coefficients(block, p, plane, complexity as usize, dcq, acq);
1443
1444 if block[0] != 0 || n {
1445 transform::idct4x4(block);
1446 }
1447
1448 left = if n { 1 } else { 0 };
1449 self.top[mbx].complexity[x + 1] = if n { 1 } else { 0 };
1450 }
1451
1452 self.left.complexity[y + 1] = left;
1453 }
1454
1455 plane = 2;
1456
1457 for &j in &[5usize, 7usize] {
1458 for y in 0usize..2 {
1459 let mut left = self.left.complexity[y + j];
1460
1461 for x in 0usize..2 {
1462 let i = x + y * 2 + if j == 5 { 16 } else { 20 };
1463 let block = &mut blocks[i * 16..i * 16 + 16];
1464
1465 let complexity = self.top[mbx].complexity[x + j] + left;
1466 let dcq = self.segment[sindex].uvdc;
1467 let acq = self.segment[sindex].uvac;
1468
1469 let n = self.read_coefficients(block, p, plane, complexity as usize, dcq, acq);
1470 if block[0] != 0 || n {
1471 transform::idct4x4(block);
1472 }
1473
1474 left = if n { 1 } else { 0 };
1475 self.top[mbx].complexity[x + j] = if n { 1 } else { 0 };
1476 }
1477
1478 self.left.complexity[y + j] = left;
1479 }
1480 }
1481
1482 blocks
1483 }
1484
1485 /// Decodes the current frame and returns a reference to it
decode_frame(&mut self) -> ImageResult<&Frame>1486 pub fn decode_frame(&mut self) -> ImageResult<&Frame> {
1487 self.read_frame_header()?;
1488
1489 for mby in 0..self.mbheight as usize {
1490 let p = mby % self.num_partitions as usize;
1491 self.left = MacroBlock::default();
1492
1493 for mbx in 0..self.mbwidth as usize {
1494 let (skip, mb) = self.read_macroblock_header(mbx)?;
1495 let blocks = if !skip {
1496 self.read_residual_data(&mb, mbx, p)
1497 } else {
1498 if mb.luma_mode != LumaMode::B {
1499 self.left.complexity[0] = 0;
1500 self.top[mbx].complexity[0] = 0;
1501 }
1502
1503 for i in 1usize..9 {
1504 self.left.complexity[i] = 0;
1505 self.top[mbx].complexity[i] = 0;
1506 }
1507
1508 [0i32; 384]
1509 };
1510
1511 self.intra_predict(mbx, mby, &mb, &blocks);
1512 }
1513
1514 self.left_border = vec![129u8; 1 + 16];
1515 }
1516
1517 Ok(&self.frame)
1518 }
1519 }
1520
1521 impl LumaMode {
from_i8(val: i8) -> Option<Self>1522 fn from_i8(val: i8) -> Option<Self> {
1523 Some(match val {
1524 DC_PRED => LumaMode::DC,
1525 V_PRED => LumaMode::V,
1526 H_PRED => LumaMode::H,
1527 TM_PRED => LumaMode::TM,
1528 B_PRED => LumaMode::B,
1529 _ => return None,
1530 })
1531 }
1532
into_intra(self) -> Option<IntraMode>1533 fn into_intra(self) -> Option<IntraMode> {
1534 Some(match self {
1535 LumaMode::DC => IntraMode::DC,
1536 LumaMode::V => IntraMode::VE,
1537 LumaMode::H => IntraMode::HE,
1538 LumaMode::TM => IntraMode::TM,
1539 LumaMode::B => return None,
1540 })
1541 }
1542 }
1543
1544 impl Default for LumaMode {
default() -> Self1545 fn default() -> Self {
1546 LumaMode::DC
1547 }
1548 }
1549
1550 impl ChromaMode {
from_i8(val: i8) -> Option<Self>1551 fn from_i8(val: i8) -> Option<Self> {
1552 Some(match val {
1553 DC_PRED => ChromaMode::DC,
1554 V_PRED => ChromaMode::V,
1555 H_PRED => ChromaMode::H,
1556 TM_PRED => ChromaMode::TM,
1557 _ => return None,
1558 })
1559 }
1560 }
1561
1562 impl Default for ChromaMode {
default() -> Self1563 fn default() -> Self {
1564 ChromaMode::DC
1565 }
1566 }
1567
1568 impl IntraMode {
from_i8(val: i8) -> Option<Self>1569 fn from_i8(val: i8) -> Option<Self> {
1570 Some(match val {
1571 B_DC_PRED => IntraMode::DC,
1572 B_TM_PRED => IntraMode::TM,
1573 B_VE_PRED => IntraMode::VE,
1574 B_HE_PRED => IntraMode::HE,
1575 B_LD_PRED => IntraMode::LD,
1576 B_RD_PRED => IntraMode::RD,
1577 B_VR_PRED => IntraMode::VR,
1578 B_VL_PRED => IntraMode::VL,
1579 B_HD_PRED => IntraMode::HD,
1580 B_HU_PRED => IntraMode::HU,
1581 _ => return None,
1582 })
1583 }
1584 }
1585
1586 impl Default for IntraMode {
default() -> Self1587 fn default() -> Self {
1588 IntraMode::DC
1589 }
1590 }
1591
init_top_macroblocks(width: usize) -> Vec<MacroBlock>1592 fn init_top_macroblocks(width: usize) -> Vec<MacroBlock> {
1593 let mb_width = (width + 15) / 16;
1594
1595 let mb = MacroBlock {
1596 // Section 11.3 #3
1597 bpred: [IntraMode::DC; 16],
1598 luma_mode: LumaMode::DC,
1599 .. MacroBlock::default()
1600 };
1601
1602 vec![mb; mb_width]
1603 }
1604
create_border(mbx: usize, mby: usize, mbw: usize, top: &[u8], left: &[u8]) -> [u8; 357]1605 fn create_border(mbx: usize, mby: usize, mbw: usize, top: &[u8], left: &[u8]) -> [u8; 357] {
1606 let stride = 1usize + 16 + 4;
1607 let mut ws = [0u8; (1 + 16) * (1 + 16 + 4)];
1608
1609 // A
1610 {
1611 let above = &mut ws[1..stride];
1612 if mby == 0 {
1613 for above in above.iter_mut() {
1614 *above = 127;
1615 }
1616 } else {
1617 for i in 0usize..16 {
1618 above[i] = top[mbx * 16 + i];
1619 }
1620
1621 if mbx == mbw - 1 {
1622 for above in above.iter_mut().skip(16) {
1623 *above = top[mbx * 16 + 15];
1624 }
1625 } else {
1626 for i in 16usize..above.len() {
1627 above[i] = top[mbx * 16 + i];
1628 }
1629 }
1630 }
1631 }
1632
1633 for i in 17usize..stride {
1634 ws[4 * stride + i] = ws[i];
1635 ws[8 * stride + i] = ws[i];
1636 ws[12 * stride + i] = ws[i];
1637 }
1638
1639 // L
1640 if mbx == 0 {
1641 for i in 0usize..16 {
1642 ws[(i + 1) * stride] = 129;
1643 }
1644 } else {
1645 for i in 0usize..16 {
1646 ws[(i + 1) * stride] = left[i + 1];
1647 }
1648 }
1649
1650 // P
1651 ws[0] = if mby == 0 {
1652 127
1653 } else if mbx == 0 {
1654 129
1655 } else {
1656 left[0]
1657 };
1658
1659 ws
1660 }
1661
avg3(left: u8, this: u8, right: u8) -> u81662 fn avg3(left: u8, this: u8, right: u8) -> u8 {
1663 let avg = (u16::from(left) + 2 * u16::from(this) + u16::from(right) + 2) >> 2;
1664 avg as u8
1665 }
1666
avg2(this: u8, right: u8) -> u81667 fn avg2(this: u8, right: u8) -> u8 {
1668 let avg = (u16::from(this) + u16::from(right) + 1) >> 1;
1669 avg as u8
1670 }
1671
add_residue(pblock: &mut [u8], rblock: &[i32], y0: usize, x0: usize, stride: usize)1672 fn add_residue(pblock: &mut [u8], rblock: &[i32], y0: usize, x0: usize, stride: usize) {
1673 for y in 0usize..4 {
1674 for x in 0usize..4 {
1675 let a = rblock[x + y * 4];
1676 let b = pblock[(y0 + y) * stride + x0 + x];
1677 let c = clamp(a + i32::from(b), 0, 255) as u8;
1678 pblock[(y0 + y) * stride + x0 + x] = c;
1679 }
1680 }
1681 }
1682
predict_4x4(ws: &mut [u8], stride: usize, modes: &[IntraMode], resdata: &[i32])1683 fn predict_4x4(ws: &mut [u8], stride: usize, modes: &[IntraMode], resdata: &[i32]) {
1684 for sby in 0usize..4 {
1685 for sbx in 0usize..4 {
1686 let i = sbx + sby * 4;
1687 let y0 = sby * 4 + 1;
1688 let x0 = sbx * 4 + 1;
1689 let rb = &resdata[i * 16..i * 16 + 16];
1690
1691 match modes[i] {
1692 IntraMode::TM => predict_tmpred(ws, 4, x0, y0, stride),
1693 IntraMode::VE => predict_bvepred(ws, x0, y0, stride),
1694 IntraMode::HE => predict_bhepred(ws, x0, y0, stride),
1695 IntraMode::DC => predict_bdcpred(ws, x0, y0, stride),
1696 IntraMode::LD => predict_bldpred(ws, x0, y0, stride),
1697 IntraMode::RD => predict_brdpred(ws, x0, y0, stride),
1698 IntraMode::VR => predict_bvrpred(ws, x0, y0, stride),
1699 IntraMode::VL => predict_bvlpred(ws, x0, y0, stride),
1700 IntraMode::HD => predict_bhdpred(ws, x0, y0, stride),
1701 IntraMode::HU => predict_bhupred(ws, x0, y0, stride),
1702 }
1703
1704 add_residue(ws, rb, y0, x0, stride);
1705 }
1706 }
1707 }
1708
predict_vpred(a: &mut [u8], size: usize, x0: usize, y0: usize, stride: usize)1709 fn predict_vpred(a: &mut [u8], size: usize, x0: usize, y0: usize, stride: usize) {
1710 for y in 0usize..size {
1711 for x in 0usize..size {
1712 a[(x + x0) + stride * (y + y0)] = a[(x + x0) + stride * (y0 + y - 1)];
1713 }
1714 }
1715 }
1716
predict_hpred(a: &mut [u8], size: usize, x0: usize, y0: usize, stride: usize)1717 fn predict_hpred(a: &mut [u8], size: usize, x0: usize, y0: usize, stride: usize) {
1718 for y in 0usize..size {
1719 for x in 0usize..size {
1720 a[(x + x0) + stride * (y + y0)] = a[(x + x0 - 1) + stride * (y0 + y)];
1721 }
1722 }
1723 }
1724
predict_dcpred(a: &mut [u8], size: usize, stride: usize, above: bool, left: bool)1725 fn predict_dcpred(a: &mut [u8], size: usize, stride: usize, above: bool, left: bool) {
1726 let mut sum = 0;
1727 let mut shf = if size == 8 { 2 } else { 3 };
1728
1729 if left {
1730 for y in 0usize..size {
1731 sum += u32::from(a[(y + 1) * stride]);
1732 }
1733
1734 shf += 1;
1735 }
1736
1737 if above {
1738 for x in 0usize..size {
1739 sum += u32::from(a[x + 1]);
1740 }
1741
1742 shf += 1;
1743 }
1744
1745 let dcval = if !left && !above {
1746 128
1747 } else {
1748 (sum + (1 << (shf - 1))) >> shf
1749 };
1750
1751 for y in 0usize..size {
1752 for x in 0usize..size {
1753 a[(x + 1) + stride * (y + 1)] = dcval as u8;
1754 }
1755 }
1756 }
1757
predict_tmpred(a: &mut [u8], size: usize, x0: usize, y0: usize, stride: usize)1758 fn predict_tmpred(a: &mut [u8], size: usize, x0: usize, y0: usize, stride: usize) {
1759 for y in 0usize..size {
1760 for x in 0usize..size {
1761 let pred = i32::from(a[(y0 + y) * stride + x0 - 1])
1762 + i32::from(a[(y0 - 1) * stride + x0 + x])
1763 - i32::from(a[(y0 - 1) * stride + x0 - 1]);
1764
1765 a[(x + x0) + stride * (y + y0)] = clamp(pred, 0, 255) as u8;
1766 }
1767 }
1768 }
1769
predict_bdcpred(a: &mut [u8], x0: usize, y0: usize, stride: usize)1770 fn predict_bdcpred(a: &mut [u8], x0: usize, y0: usize, stride: usize) {
1771 let mut v = 4;
1772 for i in 0usize..4 {
1773 v += u32::from(a[(y0 + i) * stride + x0 - 1]) + u32::from(a[(y0 - 1) * stride + x0 + i]);
1774 }
1775
1776 v >>= 3;
1777 for y in 0usize..4 {
1778 for x in 0usize..4 {
1779 a[x + x0 + stride * (y + y0)] = v as u8;
1780 }
1781 }
1782 }
1783
topleft_pixel(a: &[u8], x0: usize, y0: usize, stride: usize) -> u81784 fn topleft_pixel(a: &[u8], x0: usize, y0: usize, stride: usize) -> u8 {
1785 a[(y0 - 1) * stride + x0 - 1]
1786 }
1787
top_pixels(a: &[u8], x0: usize, y0: usize, stride: usize) -> (u8, u8, u8, u8, u8, u8, u8, u8)1788 fn top_pixels(a: &[u8], x0: usize, y0: usize, stride: usize) -> (u8, u8, u8, u8, u8, u8, u8, u8) {
1789 let a0 = a[(y0 - 1) * stride + x0];
1790 let a1 = a[(y0 - 1) * stride + x0 + 1];
1791 let a2 = a[(y0 - 1) * stride + x0 + 2];
1792 let a3 = a[(y0 - 1) * stride + x0 + 3];
1793 let a4 = a[(y0 - 1) * stride + x0 + 4];
1794 let a5 = a[(y0 - 1) * stride + x0 + 5];
1795 let a6 = a[(y0 - 1) * stride + x0 + 6];
1796 let a7 = a[(y0 - 1) * stride + x0 + 7];
1797
1798 (a0, a1, a2, a3, a4, a5, a6, a7)
1799 }
1800
left_pixels(a: &[u8], x0: usize, y0: usize, stride: usize) -> (u8, u8, u8, u8)1801 fn left_pixels(a: &[u8], x0: usize, y0: usize, stride: usize) -> (u8, u8, u8, u8) {
1802 let l0 = a[y0 * stride + x0 - 1];
1803 let l1 = a[(y0 + 1) * stride + x0 - 1];
1804 let l2 = a[(y0 + 2) * stride + x0 - 1];
1805 let l3 = a[(y0 + 3) * stride + x0 - 1];
1806
1807 (l0, l1, l2, l3)
1808 }
1809
edge_pixels( a: &[u8], x0: usize, y0: usize, stride: usize, ) -> (u8, u8, u8, u8, u8, u8, u8, u8, u8)1810 fn edge_pixels(
1811 a: &[u8],
1812 x0: usize,
1813 y0: usize,
1814 stride: usize,
1815 ) -> (u8, u8, u8, u8, u8, u8, u8, u8, u8) {
1816 let e8 = a[(y0 - 1) * stride + x0 + 3];
1817 let e7 = a[(y0 - 1) * stride + x0 + 2];
1818 let e6 = a[(y0 - 1) * stride + x0 + 1];
1819 let e5 = a[(y0 - 1) * stride + x0];
1820 let e4 = a[(y0 - 1) * stride + x0 - 1];
1821 let e3 = a[y0 * stride + x0 - 1];
1822 let e2 = a[(y0 + 1) * stride + x0 - 1];
1823 let e1 = a[(y0 + 2) * stride + x0 - 1];
1824 let e0 = a[(y0 + 3) * stride + x0 - 1];
1825
1826 (e0, e1, e2, e3, e4, e5, e6, e7, e8)
1827 }
1828
predict_bvepred(a: &mut [u8], x0: usize, y0: usize, stride: usize)1829 fn predict_bvepred(a: &mut [u8], x0: usize, y0: usize, stride: usize) {
1830 let p = topleft_pixel(a, x0, y0, stride);
1831 let (a0, a1, a2, a3, a4, _, _, _) = top_pixels(a, x0, y0, stride);
1832
1833 a[y0 * stride + x0] = avg3(p, a0, a1);
1834 a[(y0 + 1) * stride + x0] = avg3(p, a0, a1);
1835 a[(y0 + 2) * stride + x0] = avg3(p, a0, a1);
1836 a[(y0 + 3) * stride + x0] = avg3(p, a0, a1);
1837
1838 a[y0 * stride + x0 + 1] = avg3(a0, a1, a2);
1839 a[(y0 + 1) * stride + x0 + 1] = avg3(a0, a1, a2);
1840 a[(y0 + 2) * stride + x0 + 1] = avg3(a0, a1, a2);
1841 a[(y0 + 3) * stride + x0 + 1] = avg3(a0, a1, a2);
1842
1843 a[y0 * stride + x0 + 2] = avg3(a1, a2, a3);
1844 a[(y0 + 1) * stride + x0 + 2] = avg3(a1, a2, a3);
1845 a[(y0 + 2) * stride + x0 + 2] = avg3(a1, a2, a3);
1846 a[(y0 + 3) * stride + x0 + 2] = avg3(a1, a2, a3);
1847
1848 a[y0 * stride + x0 + 3] = avg3(a2, a3, a4);
1849 a[(y0 + 1) * stride + x0 + 3] = avg3(a2, a3, a4);
1850 a[(y0 + 2) * stride + x0 + 3] = avg3(a2, a3, a4);
1851 a[(y0 + 3) * stride + x0 + 3] = avg3(a2, a3, a4);
1852 }
1853
predict_bhepred(a: &mut [u8], x0: usize, y0: usize, stride: usize)1854 fn predict_bhepred(a: &mut [u8], x0: usize, y0: usize, stride: usize) {
1855 let p = topleft_pixel(a, x0, y0, stride);
1856 let (l0, l1, l2, l3) = left_pixels(a, x0, y0, stride);
1857
1858 a[y0 * stride + x0] = avg3(p, l0, l1);
1859 a[y0 * stride + x0 + 1] = avg3(p, l0, l1);
1860 a[y0 * stride + x0 + 2] = avg3(p, l0, l1);
1861 a[y0 * stride + x0 + 3] = avg3(p, l0, l1);
1862
1863 a[(y0 + 1) * stride + x0] = avg3(l0, l1, l2);
1864 a[(y0 + 1) * stride + x0 + 1] = avg3(l0, l1, l2);
1865 a[(y0 + 1) * stride + x0 + 2] = avg3(l0, l1, l2);
1866 a[(y0 + 1) * stride + x0 + 3] = avg3(l0, l1, l2);
1867
1868 a[(y0 + 2) * stride + x0] = avg3(l1, l2, l3);
1869 a[(y0 + 2) * stride + x0 + 1] = avg3(l1, l2, l3);
1870 a[(y0 + 2) * stride + x0 + 2] = avg3(l1, l2, l3);
1871 a[(y0 + 2) * stride + x0 + 3] = avg3(l1, l2, l3);
1872
1873 a[(y0 + 3) * stride + x0] = avg3(l2, l3, l3);
1874 a[(y0 + 3) * stride + x0 + 1] = avg3(l2, l3, l3);
1875 a[(y0 + 3) * stride + x0 + 2] = avg3(l2, l3, l3);
1876 a[(y0 + 3) * stride + x0 + 3] = avg3(l2, l3, l3);
1877 }
1878
predict_bldpred(a: &mut [u8], x0: usize, y0: usize, stride: usize)1879 fn predict_bldpred(a: &mut [u8], x0: usize, y0: usize, stride: usize) {
1880 let (a0, a1, a2, a3, a4, a5, a6, a7) = top_pixels(a, x0, y0, stride);
1881
1882 a[y0 * stride + x0] = avg3(a0, a1, a2);
1883 a[y0 * stride + x0 + 1] = avg3(a1, a2, a3);
1884 a[(y0 + 1) * stride + x0] = avg3(a1, a2, a3);
1885 a[y0 * stride + x0 + 2] = avg3(a2, a3, a4);
1886 a[(y0 + 1) * stride + x0 + 1] = avg3(a2, a3, a4);
1887 a[(y0 + 2) * stride + x0] = avg3(a2, a3, a4);
1888 a[y0 * stride + x0 + 3] = avg3(a3, a4, a5);
1889 a[(y0 + 1) * stride + x0 + 2] = avg3(a3, a4, a5);
1890 a[(y0 + 2) * stride + x0 + 1] = avg3(a3, a4, a5);
1891 a[(y0 + 3) * stride + x0] = avg3(a3, a4, a5);
1892 a[(y0 + 1) * stride + x0 + 3] = avg3(a4, a5, a6);
1893 a[(y0 + 2) * stride + x0 + 2] = avg3(a4, a5, a6);
1894 a[(y0 + 3) * stride + x0 + 1] = avg3(a4, a5, a6);
1895 a[(y0 + 2) * stride + x0 + 3] = avg3(a5, a6, a7);
1896 a[(y0 + 3) * stride + x0 + 2] = avg3(a5, a6, a7);
1897 a[(y0 + 3) * stride + x0 + 3] = avg3(a6, a7, a7);
1898 }
1899
predict_brdpred(a: &mut [u8], x0: usize, y0: usize, stride: usize)1900 fn predict_brdpred(a: &mut [u8], x0: usize, y0: usize, stride: usize) {
1901 let (e0, e1, e2, e3, e4, e5, e6, e7, e8) = edge_pixels(a, x0, y0, stride);
1902
1903 a[(y0 + 3) * stride + x0] = avg3(e0, e1, e2);
1904 a[(y0 + 3) * stride + x0 + 1] = avg3(e1, e2, e3);
1905 a[(y0 + 2) * stride + x0] = avg3(e1, e2, e3);
1906 a[(y0 + 3) * stride + x0 + 2] = avg3(e2, e3, e4);
1907 a[(y0 + 2) * stride + x0 + 1] = avg3(e2, e3, e4);
1908 a[(y0 + 1) * stride + x0] = avg3(e2, e3, e4);
1909 a[(y0 + 3) * stride + x0 + 3] = avg3(e3, e4, e5);
1910 a[(y0 + 2) * stride + x0 + 2] = avg3(e3, e4, e5);
1911 a[(y0 + 1) * stride + x0 + 1] = avg3(e3, e4, e5);
1912 a[y0 * stride + x0] = avg3(e3, e4, e5);
1913 a[(y0 + 2) * stride + x0 + 3] = avg3(e4, e5, e6);
1914 a[(y0 + 1) * stride + x0 + 2] = avg3(e4, e5, e6);
1915 a[y0 * stride + x0 + 1] = avg3(e4, e5, e6);
1916 a[(y0 + 1) * stride + x0 + 3] = avg3(e5, e6, e7);
1917 a[y0 * stride + x0 + 2] = avg3(e5, e6, e7);
1918 a[y0 * stride + x0 + 3] = avg3(e6, e7, e8);
1919 }
1920
predict_bvrpred(a: &mut [u8], x0: usize, y0: usize, stride: usize)1921 fn predict_bvrpred(a: &mut [u8], x0: usize, y0: usize, stride: usize) {
1922 let (_, e1, e2, e3, e4, e5, e6, e7, e8) = edge_pixels(a, x0, y0, stride);
1923
1924 a[(y0 + 3) * stride + x0] = avg3(e1, e2, e3);
1925 a[(y0 + 2) * stride + x0] = avg3(e2, e3, e4);
1926 a[(y0 + 3) * stride + x0 + 1] = avg3(e3, e4, e5);
1927 a[(y0 + 1) * stride + x0] = avg3(e3, e4, e5);
1928 a[(y0 + 2) * stride + x0 + 1] = avg2(e4, e5);
1929 a[y0 * stride + x0] = avg2(e4, e5);
1930 a[(y0 + 3) * stride + x0 + 2] = avg3(e4, e5, e6);
1931 a[(y0 + 1) * stride + x0 + 1] = avg3(e4, e5, e6);
1932 a[(y0 + 2) * stride + x0 + 2] = avg2(e5, e6);
1933 a[y0 * stride + x0 + 1] = avg2(e5, e6);
1934 a[(y0 + 3) * stride + x0 + 3] = avg3(e5, e6, e7);
1935 a[(y0 + 1) * stride + x0 + 2] = avg3(e5, e6, e7);
1936 a[(y0 + 2) * stride + x0 + 3] = avg2(e6, e7);
1937 a[y0 * stride + x0 + 2] = avg2(e6, e7);
1938 a[(y0 + 1) * stride + x0 + 3] = avg3(e6, e7, e8);
1939 a[y0 * stride + x0 + 3] = avg2(e7, e8);
1940 }
1941
predict_bvlpred(a: &mut [u8], x0: usize, y0: usize, stride: usize)1942 fn predict_bvlpred(a: &mut [u8], x0: usize, y0: usize, stride: usize) {
1943 let (a0, a1, a2, a3, a4, a5, a6, a7) = top_pixels(a, x0, y0, stride);
1944
1945 a[y0 * stride + x0] = avg2(a0, a1);
1946 a[(y0 + 1) * stride + x0] = avg3(a0, a1, a2);
1947 a[(y0 + 2) * stride + x0] = avg2(a1, a2);
1948 a[y0 * stride + x0 + 1] = avg2(a1, a2);
1949 a[(y0 + 1) * stride + x0 + 1] = avg3(a1, a2, a3);
1950 a[(y0 + 3) * stride + x0] = avg3(a1, a2, a3);
1951 a[(y0 + 2) * stride + x0 + 1] = avg2(a2, a3);
1952 a[y0 * stride + x0 + 2] = avg2(a2, a3);
1953 a[(y0 + 3) * stride + x0 + 1] = avg3(a2, a3, a4);
1954 a[(y0 + 1) * stride + x0 + 2] = avg3(a2, a3, a4);
1955 a[(y0 + 2) * stride + x0 + 2] = avg2(a3, a4);
1956 a[y0 * stride + x0 + 3] = avg2(a3, a4);
1957 a[(y0 + 3) * stride + x0 + 2] = avg3(a3, a4, a5);
1958 a[(y0 + 1) * stride + x0 + 3] = avg3(a3, a4, a5);
1959 a[(y0 + 2) * stride + x0 + 3] = avg3(a4, a5, a6);
1960 a[(y0 + 3) * stride + x0 + 3] = avg3(a5, a6, a7);
1961 }
1962
predict_bhdpred(a: &mut [u8], x0: usize, y0: usize, stride: usize)1963 fn predict_bhdpred(a: &mut [u8], x0: usize, y0: usize, stride: usize) {
1964 let (e0, e1, e2, e3, e4, e5, e6, e7, _) = edge_pixels(a, x0, y0, stride);
1965
1966 a[(y0 + 3) * stride + x0] = avg2(e0, e1);
1967 a[(y0 + 3) * stride + x0 + 1] = avg3(e0, e1, e2);
1968 a[(y0 + 2) * stride + x0] = avg2(e1, e2);
1969 a[(y0 + 3) * stride + x0 + 2] = avg2(e1, e2);
1970 a[(y0 + 2) * stride + x0 + 1] = avg3(e1, e2, e3);
1971 a[(y0 + 3) * stride + x0 + 3] = avg3(e1, e2, e3);
1972 a[(y0 + 2) * stride + x0 + 2] = avg2(e2, e3);
1973 a[(y0 + 1) * stride + x0] = avg2(e2, e3);
1974 a[(y0 + 2) * stride + x0 + 3] = avg3(e2, e3, e4);
1975 a[(y0 + 1) * stride + x0 + 1] = avg3(e2, e3, e4);
1976 a[(y0 + 1) * stride + x0 + 2] = avg2(e3, e4);
1977 a[y0 * stride + x0] = avg2(e3, e4);
1978 a[(y0 + 1) * stride + x0 + 3] = avg3(e3, e4, e5);
1979 a[y0 * stride + x0 + 1] = avg3(e3, e4, e5);
1980 a[y0 * stride + x0 + 2] = avg3(e4, e5, e6);
1981 a[y0 * stride + x0 + 3] = avg3(e5, e6, e7);
1982 }
1983
predict_bhupred(a: &mut [u8], x0: usize, y0: usize, stride: usize)1984 fn predict_bhupred(a: &mut [u8], x0: usize, y0: usize, stride: usize) {
1985 let (l0, l1, l2, l3) = left_pixels(a, x0, y0, stride);
1986
1987 a[y0 * stride + x0] = avg2(l0, l1);
1988 a[y0 * stride + x0 + 1] = avg3(l0, l1, l2);
1989 a[y0 * stride + x0 + 2] = avg2(l1, l2);
1990 a[(y0 + 1) * stride + x0] = avg2(l1, l2);
1991 a[y0 * stride + x0 + 3] = avg3(l1, l2, l3);
1992 a[(y0 + 1) * stride + x0 + 1] = avg3(l1, l2, l3);
1993 a[(y0 + 1) * stride + x0 + 2] = avg2(l2, l3);
1994 a[(y0 + 2) * stride + x0] = avg2(l2, l3);
1995 a[(y0 + 1) * stride + x0 + 3] = avg3(l2, l3, l3);
1996 a[(y0 + 2) * stride + x0 + 1] = avg3(l2, l3, l3);
1997 a[(y0 + 2) * stride + x0 + 2] = l3;
1998 a[(y0 + 2) * stride + x0 + 3] = l3;
1999 a[(y0 + 3) * stride + x0] = l3;
2000 a[(y0 + 3) * stride + x0 + 1] = l3;
2001 a[(y0 + 3) * stride + x0 + 2] = l3;
2002 a[(y0 + 3) * stride + x0 + 3] = l3;
2003 }
2004