Skip to main content

webp_rust/decoder/
tree.rs

1use crate::decoder::vp8::Vp8BoolDecoder;
2use crate::decoder::vp8i::{
3    B_DC_PRED, B_HD_PRED, B_HE_PRED, B_HU_PRED, B_LD_PRED, B_PRED, B_RD_PRED, B_TM_PRED, B_VE_PRED,
4    B_VL_PRED, B_VR_PRED, DC_PRED, H_PRED, NUM_BANDS, NUM_BMODES, NUM_CTX, NUM_PROBAS, NUM_TYPES,
5    TM_PRED, V_PRED,
6};
7use crate::decoder::DecoderError;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub struct ProbabilityUpdateSummary {
11    pub coefficient_updates: usize,
12    pub use_skip_probability: bool,
13    pub skip_probability: Option<u8>,
14}
15
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct ProbabilityTables {
18    pub coefficients: [[[[u8; NUM_PROBAS]; NUM_CTX]; NUM_BANDS]; NUM_TYPES],
19    pub summary: ProbabilityUpdateSummary,
20}
21
22const BANDS: [usize; 17] = [0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 0];
23
24impl ProbabilityTables {
25    pub fn coeff_probs(
26        &self,
27        coeff_type: usize,
28        coeff_index: usize,
29        ctx: usize,
30    ) -> &[u8; NUM_PROBAS] {
31        &self.coefficients[coeff_type][BANDS[coeff_index]][ctx]
32    }
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub struct MacroBlockHeader {
37    pub segment: u8,
38    pub skip: bool,
39    pub is_i4x4: bool,
40    pub luma_mode: u8,
41    pub sub_modes: [u8; 16],
42    pub uv_mode: u8,
43}
44
45pub(crate) const Y_MODES_INTRA4: [i8; 18] = [
46    -(B_DC_PRED as i8),
47    1,
48    -(B_TM_PRED as i8),
49    2,
50    -(B_VE_PRED as i8),
51    3,
52    4,
53    6,
54    -(B_HE_PRED as i8),
55    5,
56    -(B_RD_PRED as i8),
57    -(B_VR_PRED as i8),
58    -(B_LD_PRED as i8),
59    7,
60    -(B_VL_PRED as i8),
61    8,
62    -(B_HD_PRED as i8),
63    -(B_HU_PRED as i8),
64];
65
66pub(crate) const BMODES_PROBA: [[[u8; NUM_BMODES - 1]; NUM_BMODES]; NUM_BMODES] = [
67    [
68        [231, 120, 48, 89, 115, 113, 120, 152, 112],
69        [152, 179, 64, 126, 170, 118, 46, 70, 95],
70        [175, 69, 143, 80, 85, 82, 72, 155, 103],
71        [56, 58, 10, 171, 218, 189, 17, 13, 152],
72        [114, 26, 17, 163, 44, 195, 21, 10, 173],
73        [121, 24, 80, 195, 26, 62, 44, 64, 85],
74        [144, 71, 10, 38, 171, 213, 144, 34, 26],
75        [170, 46, 55, 19, 136, 160, 33, 206, 71],
76        [63, 20, 8, 114, 114, 208, 12, 9, 226],
77        [81, 40, 11, 96, 182, 84, 29, 16, 36],
78    ],
79    [
80        [134, 183, 89, 137, 98, 101, 106, 165, 148],
81        [72, 187, 100, 130, 157, 111, 32, 75, 80],
82        [66, 102, 167, 99, 74, 62, 40, 234, 128],
83        [41, 53, 9, 178, 241, 141, 26, 8, 107],
84        [74, 43, 26, 146, 73, 166, 49, 23, 157],
85        [65, 38, 105, 160, 51, 52, 31, 115, 128],
86        [104, 79, 12, 27, 217, 255, 87, 17, 7],
87        [87, 68, 71, 44, 114, 51, 15, 186, 23],
88        [47, 41, 14, 110, 182, 183, 21, 17, 194],
89        [66, 45, 25, 102, 197, 189, 23, 18, 22],
90    ],
91    [
92        [88, 88, 147, 150, 42, 46, 45, 196, 205],
93        [43, 97, 183, 117, 85, 38, 35, 179, 61],
94        [39, 53, 200, 87, 26, 21, 43, 232, 171],
95        [56, 34, 51, 104, 114, 102, 29, 93, 77],
96        [39, 28, 85, 171, 58, 165, 90, 98, 64],
97        [34, 22, 116, 206, 23, 34, 43, 166, 73],
98        [107, 54, 32, 26, 51, 1, 81, 43, 31],
99        [68, 25, 106, 22, 64, 171, 36, 225, 114],
100        [34, 19, 21, 102, 132, 188, 16, 76, 124],
101        [62, 18, 78, 95, 85, 57, 50, 48, 51],
102    ],
103    [
104        [193, 101, 35, 159, 215, 111, 89, 46, 111],
105        [60, 148, 31, 172, 219, 228, 21, 18, 111],
106        [112, 113, 77, 85, 179, 255, 38, 120, 114],
107        [40, 42, 1, 196, 245, 209, 10, 25, 109],
108        [88, 43, 29, 140, 166, 213, 37, 43, 154],
109        [61, 63, 30, 155, 67, 45, 68, 1, 209],
110        [100, 80, 8, 43, 154, 1, 51, 26, 71],
111        [142, 78, 78, 16, 255, 128, 34, 197, 171],
112        [41, 40, 5, 102, 211, 183, 4, 1, 221],
113        [51, 50, 17, 168, 209, 192, 23, 25, 82],
114    ],
115    [
116        [138, 31, 36, 171, 27, 166, 38, 44, 229],
117        [67, 87, 58, 169, 82, 115, 26, 59, 179],
118        [63, 59, 90, 180, 59, 166, 93, 73, 154],
119        [40, 40, 21, 116, 143, 209, 34, 39, 175],
120        [47, 15, 16, 183, 34, 223, 49, 45, 183],
121        [46, 17, 33, 183, 6, 98, 15, 32, 183],
122        [57, 46, 22, 24, 128, 1, 54, 17, 37],
123        [65, 32, 73, 115, 28, 128, 23, 128, 205],
124        [40, 3, 9, 115, 51, 192, 18, 6, 223],
125        [87, 37, 9, 115, 59, 77, 64, 21, 47],
126    ],
127    [
128        [104, 55, 44, 218, 9, 54, 53, 130, 226],
129        [64, 90, 70, 205, 40, 41, 23, 26, 57],
130        [54, 57, 112, 184, 5, 41, 38, 166, 213],
131        [30, 34, 26, 133, 152, 116, 10, 32, 134],
132        [39, 19, 53, 221, 26, 114, 32, 73, 255],
133        [31, 9, 65, 234, 2, 15, 1, 118, 73],
134        [75, 32, 12, 51, 192, 255, 160, 43, 51],
135        [88, 31, 35, 67, 102, 85, 55, 186, 85],
136        [56, 21, 23, 111, 59, 205, 45, 37, 192],
137        [55, 38, 70, 124, 73, 102, 1, 34, 98],
138    ],
139    [
140        [125, 98, 42, 88, 104, 85, 117, 175, 82],
141        [95, 84, 53, 89, 128, 100, 113, 101, 45],
142        [75, 79, 123, 47, 51, 128, 81, 171, 1],
143        [57, 17, 5, 71, 102, 57, 53, 41, 49],
144        [38, 33, 13, 121, 57, 73, 26, 1, 85],
145        [41, 10, 67, 138, 77, 110, 90, 47, 114],
146        [115, 21, 2, 10, 102, 255, 166, 23, 6],
147        [101, 29, 16, 10, 85, 128, 101, 196, 26],
148        [57, 18, 10, 102, 102, 213, 34, 20, 43],
149        [117, 20, 15, 36, 163, 128, 68, 1, 26],
150    ],
151    [
152        [102, 61, 71, 37, 34, 53, 31, 243, 192],
153        [69, 60, 71, 38, 73, 119, 28, 222, 37],
154        [68, 45, 128, 34, 1, 47, 11, 245, 171],
155        [62, 17, 19, 70, 146, 85, 55, 62, 70],
156        [37, 43, 37, 154, 100, 163, 85, 160, 1],
157        [63, 9, 92, 136, 28, 64, 32, 201, 85],
158        [75, 15, 9, 9, 64, 255, 184, 119, 16],
159        [86, 6, 28, 5, 64, 255, 25, 248, 1],
160        [56, 8, 17, 132, 137, 255, 55, 116, 128],
161        [58, 15, 20, 82, 135, 57, 26, 121, 40],
162    ],
163    [
164        [164, 50, 31, 137, 154, 133, 25, 35, 218],
165        [51, 103, 44, 131, 131, 123, 31, 6, 158],
166        [86, 40, 64, 135, 148, 224, 45, 183, 128],
167        [22, 26, 17, 131, 240, 154, 14, 1, 209],
168        [45, 16, 21, 91, 64, 222, 7, 1, 197],
169        [56, 21, 39, 155, 60, 138, 23, 102, 213],
170        [83, 12, 13, 54, 192, 255, 68, 47, 28],
171        [85, 26, 85, 85, 128, 128, 32, 146, 171],
172        [18, 11, 7, 63, 144, 171, 4, 4, 246],
173        [35, 27, 10, 146, 174, 171, 12, 26, 128],
174    ],
175    [
176        [190, 80, 35, 99, 180, 80, 126, 54, 45],
177        [85, 126, 47, 87, 176, 51, 41, 20, 32],
178        [101, 75, 128, 139, 118, 146, 116, 128, 85],
179        [56, 41, 15, 176, 236, 85, 37, 9, 62],
180        [71, 30, 17, 119, 118, 255, 17, 18, 138],
181        [101, 38, 60, 138, 55, 70, 43, 26, 142],
182        [146, 36, 19, 30, 171, 255, 97, 27, 20],
183        [138, 45, 61, 62, 219, 1, 81, 188, 64],
184        [32, 41, 20, 117, 151, 142, 20, 21, 163],
185        [112, 19, 12, 61, 195, 128, 48, 4, 24],
186    ],
187];
188
189pub(crate) const COEFFS_PROBA0: [[[[u8; NUM_PROBAS]; NUM_CTX]; NUM_BANDS]; NUM_TYPES] = [
190    [
191        [
192            [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
193            [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
194            [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
195        ],
196        [
197            [253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128],
198            [189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128],
199            [106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128],
200        ],
201        [
202            [1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128],
203            [181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128],
204            [78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128],
205        ],
206        [
207            [1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128],
208            [184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128],
209            [77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128],
210        ],
211        [
212            [1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128],
213            [170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128],
214            [37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128],
215        ],
216        [
217            [1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128],
218            [207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128],
219            [102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128],
220        ],
221        [
222            [1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128],
223            [177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128],
224            [80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128],
225        ],
226        [
227            [1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
228            [246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
229            [255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
230        ],
231    ],
232    [
233        [
234            [198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62],
235            [131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1],
236            [68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128],
237        ],
238        [
239            [1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128],
240            [184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128],
241            [81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128],
242        ],
243        [
244            [1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128],
245            [99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128],
246            [23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128],
247        ],
248        [
249            [1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128],
250            [109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128],
251            [44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128],
252        ],
253        [
254            [1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128],
255            [94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128],
256            [22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128],
257        ],
258        [
259            [1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128],
260            [124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128],
261            [35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128],
262        ],
263        [
264            [1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128],
265            [121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128],
266            [45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128],
267        ],
268        [
269            [1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128],
270            [203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128],
271            [137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128],
272        ],
273    ],
274    [
275        [
276            [253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128],
277            [175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128],
278            [73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128],
279        ],
280        [
281            [1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128],
282            [239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128],
283            [155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128],
284        ],
285        [
286            [1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128],
287            [201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128],
288            [69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128],
289        ],
290        [
291            [1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128],
292            [223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128],
293            [141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128],
294        ],
295        [
296            [1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128],
297            [190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128],
298            [149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
299        ],
300        [
301            [1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128],
302            [247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128],
303            [240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128],
304        ],
305        [
306            [1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128],
307            [213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128],
308            [55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128],
309        ],
310        [
311            [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
312            [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
313            [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],
314        ],
315    ],
316    [
317        [
318            [202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255],
319            [126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128],
320            [61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128],
321        ],
322        [
323            [1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128],
324            [166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128],
325            [39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128],
326        ],
327        [
328            [1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128],
329            [124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128],
330            [24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128],
331        ],
332        [
333            [1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128],
334            [149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128],
335            [28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128],
336        ],
337        [
338            [1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128],
339            [123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128],
340            [20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128],
341        ],
342        [
343            [1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128],
344            [168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128],
345            [47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128],
346        ],
347        [
348            [1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128],
349            [141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128],
350            [42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128],
351        ],
352        [
353            [1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
354            [244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
355            [238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128],
356        ],
357    ],
358];
359
360fn parse_i4x4_mode(br: &mut Vp8BoolDecoder<'_>, top_mode: u8, left_mode: u8) -> u8 {
361    let prob = &BMODES_PROBA[top_mode as usize][left_mode as usize];
362    let mut node = Y_MODES_INTRA4[br.get_bit(prob[0]) as usize];
363    while node > 0 {
364        node = Y_MODES_INTRA4[2 * node as usize + br.get_bit(prob[node as usize]) as usize];
365    }
366    (-node) as u8
367}
368
369pub fn parse_intra_mode_row(
370    br: &mut Vp8BoolDecoder<'_>,
371    macroblock_width: usize,
372    update_segment_map: bool,
373    segment_probs: &[u8; 3],
374    use_skip_probability: bool,
375    skip_probability: u8,
376    top_modes: &mut [u8],
377    left_modes: &mut [u8; 4],
378) -> Result<Vec<MacroBlockHeader>, DecoderError> {
379    if top_modes.len() != macroblock_width * 4 {
380        return Err(DecoderError::InvalidParam("top mode cache length"));
381    }
382
383    let mut row = Vec::with_capacity(macroblock_width);
384    for mb_x in 0..macroblock_width {
385        let top = &mut top_modes[mb_x * 4..mb_x * 4 + 4];
386        let segment = if update_segment_map {
387            if br.get_bit(segment_probs[0]) == 0 {
388                br.get_bit(segment_probs[1]) as u8
389            } else {
390                br.get_bit(segment_probs[2]) as u8 + 2
391            }
392        } else {
393            0
394        };
395        let skip = use_skip_probability && br.get_bit(skip_probability) == 1;
396        let is_i4x4 = br.get_bit(145) == 0;
397
398        let mut header = MacroBlockHeader {
399            segment,
400            skip,
401            is_i4x4,
402            luma_mode: B_PRED,
403            sub_modes: [B_DC_PRED; 16],
404            uv_mode: DC_PRED,
405        };
406
407        if !is_i4x4 {
408            let ymode = if br.get_bit(156) == 1 {
409                if br.get_bit(128) == 1 {
410                    TM_PRED
411                } else {
412                    H_PRED
413                }
414            } else if br.get_bit(163) == 1 {
415                V_PRED
416            } else {
417                DC_PRED
418            };
419            header.luma_mode = ymode;
420            top.fill(ymode);
421            left_modes.fill(ymode);
422        } else {
423            let mut modes = [B_DC_PRED; 16];
424            for y in 0..4 {
425                let mut ymode = left_modes[y];
426                for x in 0..4 {
427                    ymode = parse_i4x4_mode(br, top[x], ymode);
428                    top[x] = ymode;
429                    modes[y * 4 + x] = ymode;
430                }
431                left_modes[y] = ymode;
432            }
433            header.sub_modes = modes;
434        }
435
436        header.uv_mode = if br.get_bit(142) == 0 {
437            DC_PRED
438        } else if br.get_bit(114) == 0 {
439            V_PRED
440        } else if br.get_bit(183) == 1 {
441            TM_PRED
442        } else {
443            H_PRED
444        };
445
446        row.push(header);
447    }
448
449    if br.eof() {
450        return Err(DecoderError::Bitstream("cannot parse intra mode row"));
451    }
452
453    Ok(row)
454}
455
456pub(crate) const COEFFS_UPDATE_PROBA: [[[[u8; NUM_PROBAS]; NUM_CTX]; NUM_BANDS]; NUM_TYPES] = [
457    [
458        [
459            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
460            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
461            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
462        ],
463        [
464            [176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255],
465            [223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255],
466            [249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255],
467        ],
468        [
469            [255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255],
470            [234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
471            [253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
472        ],
473        [
474            [255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255],
475            [239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
476            [254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
477        ],
478        [
479            [255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255],
480            [251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
481            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
482        ],
483        [
484            [255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
485            [251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
486            [254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
487        ],
488        [
489            [255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255],
490            [250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255],
491            [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
492        ],
493        [
494            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
495            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
496            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
497        ],
498    ],
499    [
500        [
501            [217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
502            [225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255],
503            [234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255],
504        ],
505        [
506            [255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
507            [223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
508            [238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255],
509        ],
510        [
511            [255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255],
512            [249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
513            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
514        ],
515        [
516            [255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255],
517            [247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
518            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
519        ],
520        [
521            [255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
522            [252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
523            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
524        ],
525        [
526            [255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
527            [253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
528            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
529        ],
530        [
531            [255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255],
532            [250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
533            [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
534        ],
535        [
536            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
537            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
538            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
539        ],
540    ],
541    [
542        [
543            [186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255],
544            [234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255],
545            [251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255],
546        ],
547        [
548            [255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
549            [236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
550            [251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255],
551        ],
552        [
553            [255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
554            [254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
555            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
556        ],
557        [
558            [255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
559            [254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
560            [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
561        ],
562        [
563            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
564            [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
565            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
566        ],
567        [
568            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
569            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
570            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
571        ],
572        [
573            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
574            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
575            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
576        ],
577        [
578            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
579            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
580            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
581        ],
582    ],
583    [
584        [
585            [248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
586            [250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255],
587            [248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255],
588        ],
589        [
590            [255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255],
591            [246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255],
592            [252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255],
593        ],
594        [
595            [255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255],
596            [248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255],
597            [253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255],
598        ],
599        [
600            [255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255],
601            [245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255],
602            [253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
603        ],
604        [
605            [255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255],
606            [252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
607            [255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
608        ],
609        [
610            [255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255],
611            [249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
612            [255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
613        ],
614        [
615            [255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255],
616            [250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
617            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
618        ],
619        [
620            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
621            [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
622            [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
623        ],
624    ],
625];
626
627pub fn parse_probability_tables(
628    br: &mut Vp8BoolDecoder<'_>,
629) -> Result<ProbabilityTables, DecoderError> {
630    let mut coefficient_updates = 0usize;
631    let mut coefficients = COEFFS_PROBA0;
632    for (type_index, tables) in COEFFS_UPDATE_PROBA.into_iter().enumerate() {
633        for (band_index, bands) in tables.into_iter().enumerate() {
634            for (ctx_index, contexts) in bands.into_iter().enumerate() {
635                for (prob_index, prob) in contexts.into_iter().enumerate() {
636                    if br.get_bit(prob) == 1 {
637                        coefficients[type_index][band_index][ctx_index][prob_index] =
638                            br.get_value(8) as u8;
639                        coefficient_updates += 1;
640                    }
641                }
642            }
643        }
644    }
645
646    let use_skip_probability = br.get() == 1;
647    let skip_probability = if use_skip_probability {
648        Some(br.get_value(8) as u8)
649    } else {
650        None
651    };
652
653    if br.eof() {
654        return Err(DecoderError::Bitstream(
655            "cannot parse coefficient probability updates",
656        ));
657    }
658
659    Ok(ProbabilityTables {
660        coefficients,
661        summary: ProbabilityUpdateSummary {
662            coefficient_updates,
663            use_skip_probability,
664            skip_probability,
665        },
666    })
667}
668
669pub fn parse_probability_updates(
670    br: &mut Vp8BoolDecoder<'_>,
671) -> Result<ProbabilityUpdateSummary, DecoderError> {
672    parse_probability_tables(br).map(|tables| tables.summary)
673}