forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

tables.rs629 lines, 16.7 KiBHistoryRawDownload
1//! VLC and constant tables (ISO/IEC 13818-2 Annex B, clause 7).
2
3/// Table B.14 / B.15 rows: (run, level, code, length without the sign bit).
4pub(crate) const B14: [(u8, u8, u16, u8); 111] = [
5 (0, 1, 3, 2),
6 (0, 2, 4, 4),
7 (0, 3, 5, 5),
8 (0, 4, 6, 7),
9 (0, 5, 38, 8),
10 (0, 6, 33, 8),
11 (0, 7, 10, 10),
12 (0, 8, 29, 12),
13 (0, 9, 24, 12),
14 (0, 10, 19, 12),
15 (0, 11, 16, 12),
16 (0, 12, 26, 13),
17 (0, 13, 25, 13),
18 (0, 14, 24, 13),
19 (0, 15, 23, 13),
20 (0, 16, 31, 14),
21 (0, 17, 30, 14),
22 (0, 18, 29, 14),
23 (0, 19, 28, 14),
24 (0, 20, 27, 14),
25 (0, 21, 26, 14),
26 (0, 22, 25, 14),
27 (0, 23, 24, 14),
28 (0, 24, 23, 14),
29 (0, 25, 22, 14),
30 (0, 26, 21, 14),
31 (0, 27, 20, 14),
32 (0, 28, 19, 14),
33 (0, 29, 18, 14),
34 (0, 30, 17, 14),
35 (0, 31, 16, 14),
36 (0, 32, 24, 15),
37 (0, 33, 23, 15),
38 (0, 34, 22, 15),
39 (0, 35, 21, 15),
40 (0, 36, 20, 15),
41 (0, 37, 19, 15),
42 (0, 38, 18, 15),
43 (0, 39, 17, 15),
44 (0, 40, 16, 15),
45 (1, 1, 3, 3),
46 (1, 2, 6, 6),
47 (1, 3, 37, 8),
48 (1, 4, 12, 10),
49 (1, 5, 27, 12),
50 (1, 6, 22, 13),
51 (1, 7, 21, 13),
52 (1, 8, 31, 15),
53 (1, 9, 30, 15),
54 (1, 10, 29, 15),
55 (1, 11, 28, 15),
56 (1, 12, 27, 15),
57 (1, 13, 26, 15),
58 (1, 14, 25, 15),
59 (1, 15, 19, 16),
60 (1, 16, 18, 16),
61 (1, 17, 17, 16),
62 (1, 18, 16, 16),
63 (2, 1, 5, 4),
64 (2, 2, 4, 7),
65 (2, 3, 11, 10),
66 (2, 4, 20, 12),
67 (2, 5, 20, 13),
68 (3, 1, 7, 5),
69 (3, 2, 36, 8),
70 (3, 3, 28, 12),
71 (3, 4, 19, 13),
72 (4, 1, 6, 5),
73 (4, 2, 15, 10),
74 (4, 3, 18, 12),
75 (5, 1, 7, 6),
76 (5, 2, 9, 10),
77 (5, 3, 18, 13),
78 (6, 1, 5, 6),
79 (6, 2, 30, 12),
80 (6, 3, 20, 16),
81 (7, 1, 4, 6),
82 (7, 2, 21, 12),
83 (8, 1, 7, 7),
84 (8, 2, 17, 12),
85 (9, 1, 5, 7),
86 (9, 2, 17, 13),
87 (10, 1, 39, 8),
88 (10, 2, 16, 13),
89 (11, 1, 35, 8),
90 (11, 2, 26, 16),
91 (12, 1, 34, 8),
92 (12, 2, 25, 16),
93 (13, 1, 32, 8),
94 (13, 2, 24, 16),
95 (14, 1, 14, 10),
96 (14, 2, 23, 16),
97 (15, 1, 13, 10),
98 (15, 2, 22, 16),
99 (16, 1, 8, 10),
100 (16, 2, 21, 16),
101 (17, 1, 31, 12),
102 (18, 1, 26, 12),
103 (19, 1, 25, 12),
104 (20, 1, 23, 12),
105 (21, 1, 22, 12),
106 (22, 1, 31, 13),
107 (23, 1, 30, 13),
108 (24, 1, 29, 13),
109 (25, 1, 28, 13),
110 (26, 1, 27, 13),
111 (27, 1, 31, 16),
112 (28, 1, 30, 16),
113 (29, 1, 29, 16),
114 (30, 1, 28, 16),
115 (31, 1, 27, 16),
116];
117// B14 esc=(1, 6) eob=(2, 2)
118pub(crate) const B15: [(u8, u8, u16, u8); 111] = [
119 (0, 1, 2, 2),
120 (0, 2, 6, 3),
121 (0, 3, 7, 4),
122 (0, 4, 28, 5),
123 (0, 5, 29, 5),
124 (0, 6, 5, 6),
125 (0, 7, 4, 6),
126 (0, 8, 123, 7),
127 (0, 9, 124, 7),
128 (0, 10, 35, 8),
129 (0, 11, 34, 8),
130 (0, 12, 250, 8),
131 (0, 13, 251, 8),
132 (0, 14, 254, 8),
133 (0, 15, 255, 8),
134 (0, 16, 31, 14),
135 (0, 17, 30, 14),
136 (0, 18, 29, 14),
137 (0, 19, 28, 14),
138 (0, 20, 27, 14),
139 (0, 21, 26, 14),
140 (0, 22, 25, 14),
141 (0, 23, 24, 14),
142 (0, 24, 23, 14),
143 (0, 25, 22, 14),
144 (0, 26, 21, 14),
145 (0, 27, 20, 14),
146 (0, 28, 19, 14),
147 (0, 29, 18, 14),
148 (0, 30, 17, 14),
149 (0, 31, 16, 14),
150 (0, 32, 24, 15),
151 (0, 33, 23, 15),
152 (0, 34, 22, 15),
153 (0, 35, 21, 15),
154 (0, 36, 20, 15),
155 (0, 37, 19, 15),
156 (0, 38, 18, 15),
157 (0, 39, 17, 15),
158 (0, 40, 16, 15),
159 (1, 1, 2, 3),
160 (1, 2, 6, 5),
161 (1, 3, 121, 7),
162 (1, 4, 39, 8),
163 (1, 5, 32, 8),
164 (1, 6, 22, 13),
165 (1, 7, 21, 13),
166 (1, 8, 31, 15),
167 (1, 9, 30, 15),
168 (1, 10, 29, 15),
169 (1, 11, 28, 15),
170 (1, 12, 27, 15),
171 (1, 13, 26, 15),
172 (1, 14, 25, 15),
173 (1, 15, 19, 16),
174 (1, 16, 18, 16),
175 (1, 17, 17, 16),
176 (1, 18, 16, 16),
177 (2, 1, 5, 5),
178 (2, 2, 7, 7),
179 (2, 3, 252, 8),
180 (2, 4, 12, 10),
181 (2, 5, 20, 13),
182 (3, 1, 7, 5),
183 (3, 2, 38, 8),
184 (3, 3, 28, 12),
185 (3, 4, 19, 13),
186 (4, 1, 6, 6),
187 (4, 2, 253, 8),
188 (4, 3, 18, 12),
189 (5, 1, 7, 6),
190 (5, 2, 4, 9),
191 (5, 3, 18, 13),
192 (6, 1, 6, 7),
193 (6, 2, 30, 12),
194 (6, 3, 20, 16),
195 (7, 1, 4, 7),
196 (7, 2, 21, 12),
197 (8, 1, 5, 7),
198 (8, 2, 17, 12),
199 (9, 1, 120, 7),
200 (9, 2, 17, 13),
201 (10, 1, 122, 7),
202 (10, 2, 16, 13),
203 (11, 1, 33, 8),
204 (11, 2, 26, 16),
205 (12, 1, 37, 8),
206 (12, 2, 25, 16),
207 (13, 1, 36, 8),
208 (13, 2, 24, 16),
209 (14, 1, 5, 9),
210 (14, 2, 23, 16),
211 (15, 1, 7, 9),
212 (15, 2, 22, 16),
213 (16, 1, 13, 10),
214 (16, 2, 21, 16),
215 (17, 1, 31, 12),
216 (18, 1, 26, 12),
217 (19, 1, 25, 12),
218 (20, 1, 23, 12),
219 (21, 1, 22, 12),
220 (22, 1, 31, 13),
221 (23, 1, 30, 13),
222 (24, 1, 29, 13),
223 (25, 1, 28, 13),
224 (26, 1, 27, 13),
225 (27, 1, 31, 16),
226 (28, 1, 30, 16),
227 (29, 1, 29, 16),
228 (30, 1, 28, 16),
229 (31, 1, 27, 16),
230];
231// B15 esc=(1, 6) eob=(6, 4)
232
233/// Escape code of tables B.14 and B.15 (6 bits).
234pub(crate) const ESCAPE: (u16, u8) = (1, 6);
235/// End-of-block code of table B.14.
236pub(crate) const EOB_B14: (u16, u8) = (2, 2);
237/// End-of-block code of table B.15.
238pub(crate) const EOB_B15: (u16, u8) = (6, 4);
239
240/// Macroblock type flags (tables B.2 to B.4).
241pub(crate) const MB_QUANT: u16 = 1;
242pub(crate) const MB_FWD: u16 = 2;
243pub(crate) const MB_BWD: u16 = 4;
244pub(crate) const MB_PAT: u16 = 8;
245pub(crate) const MB_INTRA: u16 = 16;
246
247/// Table B.2: macroblock_type in I pictures, (code, length, flags).
248pub(crate) const MBTYPE_I: &[(u16, u8, u16)] = &[(0b1, 1, MB_INTRA), (0b01, 2, MB_INTRA | MB_QUANT)];
249/// Table B.3: macroblock_type in P pictures.
250pub(crate) const MBTYPE_P: &[(u16, u8, u16)] = &[
251 (0b1, 1, MB_FWD | MB_PAT),
252 (0b01, 2, MB_PAT),
253 (0b001, 3, MB_FWD),
254 (0b00011, 5, MB_INTRA),
255 (0b00010, 5, MB_FWD | MB_PAT | MB_QUANT),
256 (0b00001, 5, MB_PAT | MB_QUANT),
257 (0b000001, 6, MB_INTRA | MB_QUANT),
258];
259/// Table B.4: macroblock_type in B pictures.
260pub(crate) const MBTYPE_B: &[(u16, u8, u16)] = &[
261 (0b10, 2, MB_FWD | MB_BWD),
262 (0b11, 2, MB_FWD | MB_BWD | MB_PAT),
263 (0b010, 3, MB_BWD),
264 (0b011, 3, MB_BWD | MB_PAT),
265 (0b0010, 4, MB_FWD),
266 (0b0011, 4, MB_FWD | MB_PAT),
267 (0b00011, 5, MB_INTRA),
268 (0b00010, 5, MB_FWD | MB_BWD | MB_PAT | MB_QUANT),
269 (0b000011, 6, MB_FWD | MB_PAT | MB_QUANT),
270 (0b000010, 6, MB_BWD | MB_PAT | MB_QUANT),
271 (0b000001, 6, MB_INTRA | MB_QUANT),
272];
273
274/// Table B.1: macroblock_address_increment, (code, length, value 1..=33).
275pub(crate) const MBA: &[(u16, u8, u16)] = &[
276 (0b1, 1, 1),
277 (0b011, 3, 2),
278 (0b010, 3, 3),
279 (0b0011, 4, 4),
280 (0b0010, 4, 5),
281 (0b00011, 5, 6),
282 (0b00010, 5, 7),
283 (0b0000111, 7, 8),
284 (0b0000110, 7, 9),
285 (0b00001011, 8, 10),
286 (0b00001010, 8, 11),
287 (0b00001001, 8, 12),
288 (0b00001000, 8, 13),
289 (0b00000111, 8, 14),
290 (0b00000110, 8, 15),
291 (0b0000010111, 10, 16),
292 (0b0000010110, 10, 17),
293 (0b0000010101, 10, 18),
294 (0b0000010100, 10, 19),
295 (0b0000010011, 10, 20),
296 (0b0000010010, 10, 21),
297 (0b00000100011, 11, 22),
298 (0b00000100010, 11, 23),
299 (0b00000100001, 11, 24),
300 (0b00000100000, 11, 25),
301 (0b00000011111, 11, 26),
302 (0b00000011110, 11, 27),
303 (0b00000011101, 11, 28),
304 (0b00000011100, 11, 29),
305 (0b00000011011, 11, 30),
306 (0b00000011010, 11, 31),
307 (0b00000011001, 11, 32),
308 (0b00000011000, 11, 33),
309 // macroblock_escape (adds 33) and the MPEG-1 stuffing code (adds nothing).
310 (0b00000001000, 11, 34),
311 (0b00000001111, 11, 35),
312];
313
314/// Table B.9: coded_block_pattern, (code, length, cbp).
315pub(crate) const CBP: &[(u16, u8, u16)] = &[
316 (0b000000001, 9, 0),
317 (0b01011, 5, 1),
318 (0b01001, 5, 2),
319 (0b001101, 6, 3),
320 (0b1101, 4, 4),
321 (0b0010111, 7, 5),
322 (0b0010011, 7, 6),
323 (0b00011111, 8, 7),
324 (0b1100, 4, 8),
325 (0b0010110, 7, 9),
326 (0b0010010, 7, 10),
327 (0b00011110, 8, 11),
328 (0b10011, 5, 12),
329 (0b00011011, 8, 13),
330 (0b00010111, 8, 14),
331 (0b00010011, 8, 15),
332 (0b1011, 4, 16),
333 (0b0010101, 7, 17),
334 (0b0010001, 7, 18),
335 (0b00011101, 8, 19),
336 (0b10001, 5, 20),
337 (0b00011001, 8, 21),
338 (0b00010101, 8, 22),
339 (0b00010001, 8, 23),
340 (0b001111, 6, 24),
341 (0b00001111, 8, 25),
342 (0b00001101, 8, 26),
343 (0b000000011, 9, 27),
344 (0b01111, 5, 28),
345 (0b00001011, 8, 29),
346 (0b00000111, 8, 30),
347 (0b000000111, 9, 31),
348 (0b1010, 4, 32),
349 (0b0010100, 7, 33),
350 (0b0010000, 7, 34),
351 (0b00011100, 8, 35),
352 (0b001110, 6, 36),
353 (0b00001110, 8, 37),
354 (0b00001100, 8, 38),
355 (0b000000010, 9, 39),
356 (0b10000, 5, 40),
357 (0b00011000, 8, 41),
358 (0b00010100, 8, 42),
359 (0b00010000, 8, 43),
360 (0b01110, 5, 44),
361 (0b00001010, 8, 45),
362 (0b00000110, 8, 46),
363 (0b000000110, 9, 47),
364 (0b10010, 5, 48),
365 (0b00011010, 8, 49),
366 (0b00010110, 8, 50),
367 (0b00010010, 8, 51),
368 (0b01101, 5, 52),
369 (0b00001001, 8, 53),
370 (0b00000101, 8, 54),
371 (0b000000101, 9, 55),
372 (0b01100, 5, 56),
373 (0b00001000, 8, 57),
374 (0b00000100, 8, 58),
375 (0b000000100, 9, 59),
376 (0b111, 3, 60),
377 (0b01010, 5, 61),
378 (0b01000, 5, 62),
379 (0b001100, 6, 63),
380];
381
382/// Table B.10: motion_code magnitude 0..=16, (code, length) without the sign bit.
383pub(crate) const MOTION: [(u16, u8); 17] = [
384 (0b1, 1),
385 (0b01, 2),
386 (0b001, 3),
387 (0b0001, 4),
388 (0b000011, 6),
389 (0b0000101, 7),
390 (0b0000100, 7),
391 (0b0000011, 7),
392 (0b000001011, 9),
393 (0b000001010, 9),
394 (0b000001001, 9),
395 (0b0000010001, 10),
396 (0b0000010000, 10),
397 (0b0000001111, 10),
398 (0b0000001110, 10),
399 (0b0000001101, 10),
400 (0b0000001100, 10),
401];
402
403/// Table B.12: dct_dc_size_luminance, (code, length) for sizes 0..=11.
404pub(crate) const DC_LUMA: [(u16, u8); 12] = [
405 (0b100, 3),
406 (0b00, 2),
407 (0b01, 2),
408 (0b101, 3),
409 (0b110, 3),
410 (0b1110, 4),
411 (0b11110, 5),
412 (0b111110, 6),
413 (0b1111110, 7),
414 (0b11111110, 8),
415 (0b111111110, 9),
416 (0b111111111, 9),
417];
418/// Table B.13: dct_dc_size_chrominance.
419pub(crate) const DC_CHROMA: [(u16, u8); 12] = [
420 (0b00, 2),
421 (0b01, 2),
422 (0b10, 2),
423 (0b110, 3),
424 (0b1110, 4),
425 (0b11110, 5),
426 (0b111110, 6),
427 (0b1111110, 7),
428 (0b11111110, 8),
429 (0b111111110, 9),
430 (0b1111111110, 10),
431 (0b1111111111, 10),
432];
433
434/// Classic zig-zag scan, scan position to raster index.
435pub(crate) const ZIGZAG: [u8; 64] = [
436 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20,
437 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59,
438 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
439];
440/// Alternate scan (Figure 7-3), scan position to raster index.
441pub(crate) const ALT_SCAN: [u8; 64] = [
442 0, 8, 16, 24, 1, 9, 2, 10, 17, 25, 32, 40, 48, 56, 57, 49, 41, 33, 26, 18, 3, 11, 4, 12, 19,
443 27, 34, 42, 50, 58, 35, 43, 51, 59, 20, 28, 5, 13, 6, 14, 21, 29, 36, 44, 52, 60, 37, 45, 53,
444 61, 22, 30, 7, 15, 23, 31, 38, 46, 54, 62, 39, 47, 55, 63,
445];
446
447/// Default intra quantiser matrix in zig-zag order (6.3.11).
448pub(crate) const DEFAULT_INTRA: [u8; 64] = [
449 8, 16, 16, 19, 16, 19, 22, 22, 22, 22, 22, 22, 26, 24, 26, 27, 27, 27, 26, 26, 26, 26, 27, 27,
450 27, 29, 29, 29, 34, 34, 34, 29, 29, 29, 27, 27, 29, 29, 32, 32, 34, 34, 37, 38, 37, 35, 35, 34,
451 35, 38, 38, 40, 40, 40, 48, 48, 46, 46, 56, 56, 58, 69, 69, 83,
452];
453
454/// Non-linear quantiser_scale (q_scale_type = 1), index quantiser_scale_code.
455pub(crate) const NONLINEAR_QSCALE: [u8; 32] = [
456 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64,
457 72, 80, 88, 96, 104, 112,
458];
459
460/// A prefix-code lookup table indexed by the next `bits` bits of the stream.
461pub(crate) struct Vlc {
462 /// `len << 16 | value`; len 0 marks an invalid prefix.
463 pub(crate) table: Vec<u32>,
464 pub(crate) bits: u32,
465}
466
467impl Vlc {
468 /// Build from `(code, length, value)` rows.
469 pub(crate) fn build(rows: &[(u32, u32, u32)]) -> Vlc {
470 let bits = rows.iter().map(|r| r.1).max().unwrap_or(1);
471 let mut table = vec![0u32; 1 << bits];
472 for &(code, len, value) in rows {
473 let lo = (code << (bits - len)) as usize;
474 let hi = lo + (1usize << (bits - len));
475 for slot in &mut table[lo..hi] {
476 *slot = (len << 16) | value;
477 }
478 }
479 Vlc { table, bits }
480 }
481
482 fn small(rows: &[(u16, u8, u16)]) -> Vlc {
483 let rows: Vec<_> = rows
484 .iter()
485 .map(|&(c, l, v)| (c as u32, l as u32, v as u32))
486 .collect();
487 Vlc::build(&rows)
488 }
489
490 fn pairs(rows: &[(u16, u8)]) -> Vlc {
491 let rows: Vec<_> = rows
492 .iter()
493 .enumerate()
494 .map(|(i, &(c, l))| (c as u32, l as u32, i as u32))
495 .collect();
496 Vlc::build(&rows)
497 }
498
499 fn coef(rows: &[(u8, u8, u16, u8); 111], esc: (u16, u8), eob: (u16, u8)) -> Vlc {
500 // The sign bit follows the code and is read separately.
501 let mut r: Vec<(u32, u32, u32)> = rows
502 .iter()
503 .map(|&(run, level, code, len)| {
504 (code as u32, len as u32, ((run as u32) << 8) | level as u32)
505 })
506 .collect();
507 r.push((esc.0 as u32, esc.1 as u32, COEF_ESC));
508 r.push((eob.0 as u32, eob.1 as u32, COEF_EOB));
509 Vlc::build(&r)
510 }
511}
512
513/// Table values for end-of-block and escape.
514pub(crate) const COEF_EOB: u32 = 0xFFFF;
515pub(crate) const COEF_ESC: u32 = 0xFFFE;
516
517/// All lookup tables, built once.
518pub(crate) struct Tables {
519 pub(crate) mba: Vlc,
520 pub(crate) cbp: Vlc,
521 pub(crate) mbtype: [Vlc; 3],
522 pub(crate) motion: Vlc,
523 pub(crate) dc_luma: Vlc,
524 pub(crate) dc_chroma: Vlc,
525 pub(crate) coef0: Vlc,
526 pub(crate) coef1: Vlc,
527}
528
529pub(crate) fn tables() -> &'static Tables {
530 static T: std::sync::OnceLock<Tables> = std::sync::OnceLock::new();
531 T.get_or_init(|| Tables {
532 mba: Vlc::small(MBA),
533 cbp: Vlc::small(CBP),
534 mbtype: [
535 Vlc::small(MBTYPE_I),
536 Vlc::small(MBTYPE_P),
537 Vlc::small(MBTYPE_B),
538 ],
539 motion: Vlc::pairs(&MOTION),
540 dc_luma: Vlc::pairs(&DC_LUMA),
541 dc_chroma: Vlc::pairs(&DC_CHROMA),
542 coef0: Vlc::coef(&B14, ESCAPE, EOB_B14),
543 coef1: Vlc::coef(&B15, ESCAPE, EOB_B15),
544 })
545}
546
547#[cfg(test)]
548mod tests {
549 use super::*;
550
551 fn prefix_free(rows: &[(u32, u32)]) {
552 for (i, a) in rows.iter().enumerate() {
553 for (j, b) in rows.iter().enumerate() {
554 if i != j && b.1 >= a.1 {
555 assert!(
556 (b.0 >> (b.1 - a.1)) != a.0,
557 "code {:b}/{} is a prefix of {:b}/{}",
558 a.0,
559 a.1,
560 b.0,
561 b.1
562 );
563 }
564 }
565 }
566 }
567
568 fn check(rows: &[(u32, u32)], expect_full: bool) {
569 prefix_free(rows);
570 let kraft: f64 = rows.iter().map(|r| 2f64.powi(-(r.1 as i32))).sum();
571 assert!(kraft <= 1.0 + 1e-12, "kraft {kraft}");
572 if expect_full {
573 assert!((kraft - 1.0).abs() < 1e-12, "kraft {kraft}");
574 }
575 }
576
577 #[test]
578 fn all_codes_are_prefix_free() {
579 let t = |r: &[(u16, u8, u16)]| -> Vec<(u32, u32)> {
580 r.iter().map(|x| (x.0 as u32, x.1 as u32)).collect()
581 };
582 check(&t(MBA), false);
583 check(&t(CBP), false);
584 check(&t(MBTYPE_I), false);
585 check(&t(MBTYPE_P), false);
586 check(&t(MBTYPE_B), false);
587 let p = |r: &[(u16, u8)]| -> Vec<(u32, u32)> {
588 r.iter().map(|x| (x.0 as u32, x.1 as u32)).collect()
589 };
590 check(&p(&DC_LUMA), false);
591 check(&p(&DC_CHROMA), false);
592 let mut m = p(&MOTION);
593 // motion codes are followed by a sign bit except zero
594 m.truncate(17);
595 check(&m, false);
596 let mut c14: Vec<(u32, u32)> = B14.iter().map(|x| (x.2 as u32, x.3 as u32)).collect();
597 c14.push((ESCAPE.0 as u32, ESCAPE.1 as u32));
598 c14.push((EOB_B14.0 as u32, EOB_B14.1 as u32));
599 check(&c14, false);
600 let mut c15: Vec<(u32, u32)> = B15.iter().map(|x| (x.2 as u32, x.3 as u32)).collect();
601 c15.push((ESCAPE.0 as u32, ESCAPE.1 as u32));
602 c15.push((EOB_B15.0 as u32, EOB_B15.1 as u32));
603 check(&c15, false);
604 }
605
606 #[test]
607 fn scans_are_permutations() {
608 for s in [&ZIGZAG, &ALT_SCAN] {
609 let mut seen = [false; 64];
610 for &x in s.iter() {
611 assert!(!seen[x as usize]);
612 seen[x as usize] = true;
613 }
614 }
615 }
616
617 #[test]
618 fn mba_and_cbp_are_complete_enough() {
619 // Table B.9 has 64 entries (cbp 0 included), B.1 has 33 + escape + stuffing.
620 assert_eq!(CBP.len(), 64);
621 let mut seen = [false; 64];
622 for r in CBP {
623 assert!(!seen[r.2 as usize]);
624 seen[r.2 as usize] = true;
625 }
626 let kraft: f64 = CBP.iter().map(|r| 2f64.powi(-(r.1 as i32))).sum();
627 assert!(kraft > 0.99, "cbp kraft {kraft}");
628 }
629}