forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBe12d32fTahinli6 days ago3982 commits

h263data.rs912 lines, 26.2 KiBHistoryRawDownload
1// Generated by tools/gen_h263_tables.py from the text of ITU-T Rec. H.263 (01/2005)
2// (https://www.itu.int/rec/T-REC-H.263). Do not edit by hand.
3//
4// Every table in this file is transcribed from the Recommendation; the clause or table
5// number is given in each doc comment. A `[code, bits]` pair holds a variable length
6// code as its integer value and its length in bits (the sign bit of TCOEF / MVD
7// codes is not included).
8
9/// MCBPC for I pictures, code value by spec index (Table 7/H.263; index 8 is stuffing).
10pub const H263_INTRA_MCBPC_CODE: [u8; 9] = [1, 1, 2, 3, 1, 1, 2, 3, 1];
11
12/// MCBPC for I pictures, code length (Table 7/H.263).
13pub const H263_INTRA_MCBPC_BITS: [u8; 9] = [1, 3, 3, 3, 4, 6, 6, 6, 9];
14
15/// MCBPC for P pictures, code value. Slot = 4 * group + CBPC with group = INTRA + 2 * Q + 4 * four-vectors flags of the MB type (types 0, 3, 1, 4, 2 -> groups 0..4, type 5 -> group 6), slot 20 = stuffing, slots 21-23 unused (Table 8/H.263).
16pub const H263_INTER_MCBPC_CODE: [u8; 28] = [
17 1, 3, 2, 5, 3, 4, 3, 3, 3, 7, 6, 5, 4, 4, 3, 2, 2, 5, 4, 5, 1, 0, 0, 0, 2, 12, 14, 15,
18];
19
20/// MCBPC for P pictures, code length (Table 8/H.263); 0 marks an unused slot.
21pub const H263_INTER_MCBPC_BITS: [u8; 28] = [
22 1, 4, 4, 6, 5, 8, 8, 7, 3, 7, 7, 9, 6, 9, 9, 9, 3, 7, 7, 8, 9, 0, 0, 0, 11, 13, 13, 13,
23];
24
25/// CBPC for B- and EP-pictures, indexed by the coded block pattern (Table O.4/H.263).
26pub const CBPC_B_TAB: [[u8; 2]; 4] = [[0, 1], [2, 2], [7, 3], [6, 3]];
27
28/// CBPY, indexed by the INTRA pattern value (12,34) read as four bits (Table 12/H.263); the INTER code is that of the complemented pattern.
29pub const H263_CBPY_TAB: [[u8; 2]; 16] = [
30 [3, 4],
31 [5, 5],
32 [4, 5],
33 [9, 4],
34 [3, 5],
35 [7, 4],
36 [2, 6],
37 [11, 4],
38 [2, 5],
39 [3, 6],
40 [5, 4],
41 [10, 4],
42 [4, 4],
43 [8, 4],
44 [6, 4],
45 [3, 2],
46];
47
48/// MVD, indexed by the absolute vector difference in half samples 0..32 (Table 14/H.263); the sign bit follows the code, +-16 share one code.
49pub const MVTAB: [[u8; 2]; 33] = [
50 [1, 1],
51 [1, 2],
52 [1, 3],
53 [1, 4],
54 [3, 6],
55 [5, 7],
56 [4, 7],
57 [3, 7],
58 [11, 9],
59 [10, 9],
60 [9, 9],
61 [17, 10],
62 [16, 10],
63 [15, 10],
64 [14, 10],
65 [13, 10],
66 [12, 10],
67 [11, 10],
68 [10, 10],
69 [9, 10],
70 [8, 10],
71 [7, 10],
72 [6, 10],
73 [5, 10],
74 [4, 10],
75 [7, 11],
76 [6, 11],
77 [5, 11],
78 [4, 11],
79 [3, 11],
80 [2, 11],
81 [3, 12],
82 [2, 12],
83];
84
85/// TCOEF VLC without the sign bit, 102 (LAST, RUN, |LEVEL|) codes sorted by LAST, RUN, |LEVEL| followed by ESCAPE (Table 16/H.263).
86pub const INTER_VLC: [[u16; 2]; 103] = [
87 [2, 2],
88 [15, 4],
89 [21, 6],
90 [23, 7],
91 [31, 8],
92 [37, 9],
93 [36, 9],
94 [33, 10],
95 [32, 10],
96 [7, 11],
97 [6, 11],
98 [32, 11],
99 [6, 3],
100 [20, 6],
101 [30, 8],
102 [15, 10],
103 [33, 11],
104 [80, 12],
105 [14, 4],
106 [29, 8],
107 [14, 10],
108 [81, 12],
109 [13, 5],
110 [35, 9],
111 [13, 10],
112 [12, 5],
113 [34, 9],
114 [82, 12],
115 [11, 5],
116 [12, 10],
117 [83, 12],
118 [19, 6],
119 [11, 10],
120 [84, 12],
121 [18, 6],
122 [10, 10],
123 [17, 6],
124 [9, 10],
125 [16, 6],
126 [8, 10],
127 [22, 7],
128 [85, 12],
129 [21, 7],
130 [20, 7],
131 [28, 8],
132 [27, 8],
133 [33, 9],
134 [32, 9],
135 [31, 9],
136 [30, 9],
137 [29, 9],
138 [28, 9],
139 [27, 9],
140 [26, 9],
141 [34, 11],
142 [35, 11],
143 [86, 12],
144 [87, 12],
145 [7, 4],
146 [25, 9],
147 [5, 11],
148 [15, 6],
149 [4, 11],
150 [14, 6],
151 [13, 6],
152 [12, 6],
153 [19, 7],
154 [18, 7],
155 [17, 7],
156 [16, 7],
157 [26, 8],
158 [25, 8],
159 [24, 8],
160 [23, 8],
161 [22, 8],
162 [21, 8],
163 [20, 8],
164 [19, 8],
165 [24, 9],
166 [23, 9],
167 [22, 9],
168 [21, 9],
169 [20, 9],
170 [19, 9],
171 [18, 9],
172 [17, 9],
173 [7, 10],
174 [6, 10],
175 [5, 10],
176 [4, 10],
177 [36, 11],
178 [37, 11],
179 [38, 11],
180 [39, 11],
181 [88, 12],
182 [89, 12],
183 [90, 12],
184 [91, 12],
185 [92, 12],
186 [93, 12],
187 [94, 12],
188 [95, 12],
189 [3, 7],
190];
191
192/// |LEVEL| of the entries of INTER_VLC (Table 16/H.263).
193pub const INTER_LEVEL: [i8; 102] = [
194 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 1, 2, 3,
195 1, 2, 3, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 1, 2,
196 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
197 1, 1, 1, 1, 1, 1, 1,
198];
199
200/// RUN of the entries of INTER_VLC (Table 16/H.263).
201pub const INTER_RUN: [i8; 102] = [
202 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6,
203 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
204 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
205 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
206];
207
208/// INTRA TCOEF VLC for Advanced INTRA Coding, entries sorted by LAST, RUN, |LEVEL|, then ESCAPE (Table I.2/H.263).
209pub const INTRA_VLC_AIC: [[u16; 2]; 103] = [
210 [2, 2],
211 [6, 3],
212 [14, 4],
213 [12, 5],
214 [13, 5],
215 [16, 6],
216 [17, 6],
217 [18, 6],
218 [22, 7],
219 [27, 8],
220 [32, 9],
221 [33, 9],
222 [26, 9],
223 [27, 9],
224 [28, 9],
225 [29, 9],
226 [30, 9],
227 [31, 9],
228 [35, 11],
229 [34, 11],
230 [87, 12],
231 [86, 12],
232 [85, 12],
233 [84, 12],
234 [83, 12],
235 [15, 4],
236 [20, 6],
237 [20, 7],
238 [30, 8],
239 [15, 10],
240 [33, 11],
241 [80, 12],
242 [11, 5],
243 [21, 7],
244 [14, 10],
245 [9, 10],
246 [21, 6],
247 [29, 8],
248 [13, 10],
249 [81, 12],
250 [19, 6],
251 [35, 9],
252 [7, 11],
253 [23, 7],
254 [34, 9],
255 [82, 12],
256 [28, 8],
257 [12, 10],
258 [31, 8],
259 [11, 10],
260 [37, 9],
261 [10, 10],
262 [36, 9],
263 [6, 11],
264 [33, 10],
265 [32, 10],
266 [8, 10],
267 [32, 11],
268 [7, 4],
269 [12, 6],
270 [16, 7],
271 [19, 8],
272 [17, 9],
273 [18, 9],
274 [4, 10],
275 [39, 11],
276 [38, 11],
277 [95, 12],
278 [15, 6],
279 [19, 9],
280 [5, 10],
281 [37, 11],
282 [14, 6],
283 [20, 9],
284 [36, 11],
285 [13, 6],
286 [6, 10],
287 [94, 12],
288 [17, 7],
289 [7, 10],
290 [19, 7],
291 [93, 12],
292 [18, 7],
293 [92, 12],
294 [20, 8],
295 [91, 12],
296 [21, 8],
297 [26, 8],
298 [25, 8],
299 [24, 8],
300 [23, 8],
301 [22, 8],
302 [25, 9],
303 [21, 9],
304 [22, 9],
305 [24, 9],
306 [23, 9],
307 [4, 11],
308 [5, 11],
309 [88, 12],
310 [89, 12],
311 [90, 12],
312 [3, 7],
313];
314
315/// RUN of the entries of INTRA_VLC_AIC (Table I.2/H.263).
316pub const INTRA_RUN_AIC: [i8; 102] = [
317 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
318 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 11, 12, 13, 0, 0, 0, 0,
319 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 9, 10, 11, 12, 13,
320 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
321];
322
323/// |LEVEL| of the entries of INTRA_VLC_AIC (Table I.2/H.263).
324pub const INTRA_LEVEL_AIC: [i8; 102] = [
325 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 1,
326 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1,
327 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1,
328 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
329];
330
331/// Advanced INTRA Coding reconstruction scale 2 * QUANT (Annex I.3: RecC = 2 * QUANT * LEVEL, applied to INTRADC too).
332pub const AIC_DC_SCALE_TABLE: [u8; 32] = [
333 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48,
334 50, 52, 54, 56, 58, 60, 62,
335];
336
337/// Modified Quantization: new QUANT for DQUANT = 10 (row 0) and 11 (row 1) by prior QUANT (Table T.1/H.263); slot 0 is unused.
338pub const MODIFIED_QUANT_TAB: [[u8; 32]; 2] = [
339 [
340 0, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 18, 19, 20, 21, 22,
341 23, 24, 25, 26, 27, 28,
342 ],
343 [
344 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 25, 26, 27,
345 28, 29, 30, 31, 31, 31, 26,
346 ],
347];
348
349/// QUANT_C for chrominance in Modified Quantization mode (Table T.2/H.263); slot 0 is unused.
350pub const H263_CHROMA_QSCALE_TABLE: [u8; 32] = [
351 0, 1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 14, 14,
352 15, 15, 15, 15, 15,
353];
354
355/// Largest macroblock address per standard picture format, sub-QCIF to 2048x1152 (Table K.2/H.263).
356pub const MBA_MAX: [u16; 6] = [47, 98, 395, 1583, 6335, 9215];
357
358/// MBA field width in bits for the same formats (Table K.2/H.263); the last slot repeats the 14 bits of the largest format.
359pub const MBA_LENGTH: [u8; 7] = [6, 7, 9, 11, 13, 14, 14];
360
361/// Zigzag scan: raster position (row * 8 + column) of each scan index (Figure 14/H.263).
362pub const ZIGZAG_DIRECT: [u8; 64] = [
363 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,
364 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59,
365 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
366];
367
368/// Alternate-Horizontal scan (Figure I.2-a/H.263).
369pub const ALTERNATE_HORIZONTAL_SCAN: [u8; 64] = [
370 0, 1, 2, 3, 8, 9, 16, 17, 10, 11, 4, 5, 6, 7, 15, 14, 13, 12, 19, 18, 24, 25, 32, 33, 26, 27,
371 20, 21, 22, 23, 28, 29, 30, 31, 34, 35, 40, 41, 48, 49, 42, 43, 36, 37, 38, 39, 44, 45, 46, 47,
372 50, 51, 56, 57, 58, 59, 52, 53, 54, 55, 60, 61, 62, 63,
373];
374
375/// Alternate-Vertical scan (Figure I.2-b/H.263, as in H.262).
376pub const ALTERNATE_VERTICAL_SCAN: [u8; 64] = [
377 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,
378 27, 34, 42, 50, 58, 35, 43, 51, 59, 20, 28, 5, 13, 6, 14, 21, 29, 36, 44, 52, 60, 37, 45, 53,
379 61, 22, 30, 7, 15, 23, 31, 38, 46, 54, 62, 39, 47, 55, 63,
380];
381
382/// Source format dimensions by PTYPE source-format code 0-7 (width, height; Table 2/H.263 and 5.1.3); 0, 6 and 7 are not standard formats.
383pub const H263_FORMAT: [[u16; 2]; 8] = [
384 [0, 0],
385 [128, 96],
386 [176, 144],
387 [352, 288],
388 [704, 576],
389 [1408, 1152],
390 [0, 0],
391 [0, 0],
392];
393
394/// Pixel aspect ratio (width, height) by PAR code (Table 5/H.263); forbidden, reserved and extended codes give (0, 1).
395pub const H263_PIXEL_ASPECT: [[u8; 2]; 16] = [
396 [0, 1],
397 [1, 1],
398 [12, 11],
399 [10, 11],
400 [16, 11],
401 [40, 33],
402 [0, 1],
403 [0, 1],
404 [0, 1],
405 [0, 1],
406 [0, 1],
407 [0, 1],
408 [0, 1],
409 [0, 1],
410 [0, 1],
411 [0, 1],
412];
413
414// Number of entries (without ESCAPE) and number of LAST = 0 entries of the TCOEF tables.
415pub const H263_RL_INTER_N: usize = 102;
416pub const H263_RL_INTER_LAST: usize = 58;
417pub const RL_INTRA_AIC_N: usize = 102;
418pub const RL_INTRA_AIC_LAST: usize = 58;
419
420#[cfg(test)]
421mod spec_tests {
422 //! The rows below are the printed codes of the Recommendation; the tests rebuild
423 //! the packed tables from them and compare.
424 use super::*;
425
426 fn val(c: &str) -> (u32, u32) {
427 (u32::from_str_radix(c, 2).unwrap(), c.len() as u32)
428 }
429
430 /// (MB type, CBPC, code) of Table 7/H.263, indices 0-8.
431 const T7: [(&str, &str, &str); 9] = [
432 ("3", "00", "1"),
433 ("3", "01", "001"),
434 ("3", "10", "010"),
435 ("3", "11", "011"),
436 ("4", "00", "0001"),
437 ("4", "01", "000001"),
438 ("4", "10", "000010"),
439 ("4", "11", "000011"),
440 ("Stuffing", "-", "000000001"),
441 ];
442 /// (MB type, CBPC, code) of Table 8/H.263, indices 0-24.
443 const T8: [(&str, &str, &str); 25] = [
444 ("0", "00", "1"),
445 ("0", "01", "0011"),
446 ("0", "10", "0010"),
447 ("0", "11", "000101"),
448 ("1", "00", "011"),
449 ("1", "01", "0000111"),
450 ("1", "10", "0000110"),
451 ("1", "11", "000000101"),
452 ("2", "00", "010"),
453 ("2", "01", "0000101"),
454 ("2", "10", "0000100"),
455 ("2", "11", "00000101"),
456 ("3", "00", "00011"),
457 ("3", "01", "00000100"),
458 ("3", "10", "00000011"),
459 ("3", "11", "0000011"),
460 ("4", "00", "000100"),
461 ("4", "01", "000000100"),
462 ("4", "10", "000000011"),
463 ("4", "11", "000000010"),
464 ("Stuffing", "-", "000000001"),
465 ("5", "00", "00000000010"),
466 ("5", "01", "0000000001100"),
467 ("5", "10", "0000000001110"),
468 ("5", "11", "0000000001111"),
469 ];
470 /// Table 12/H.263 codes in index order (index = INTRA pattern).
471 const T12: [&str; 16] = [
472 "0011", "00101", "00100", "1001", "00011", "0111", "000010", "1011", "00010", "000011",
473 "0101", "1010", "0100", "1000", "0110", "11",
474 ];
475 /// Table 14/H.263 codes for positive vectors 0, 0.5, ... 15.5 without sign bit, and -16 last.
476 const T14: [&str; 33] = [
477 "1",
478 "01",
479 "001",
480 "0001",
481 "000011",
482 "0000101",
483 "0000100",
484 "0000011",
485 "000001011",
486 "000001010",
487 "000001001",
488 "0000010001",
489 "0000010000",
490 "0000001111",
491 "0000001110",
492 "0000001101",
493 "0000001100",
494 "0000001011",
495 "0000001010",
496 "0000001001",
497 "0000001000",
498 "0000000111",
499 "0000000110",
500 "0000000101",
501 "0000000100",
502 "00000000111",
503 "00000000110",
504 "00000000101",
505 "00000000100",
506 "00000000011",
507 "00000000010",
508 "000000000011",
509 "000000000010",
510 ];
511 /// Table 16/H.263 rows: (LAST, RUN, |LEVEL|, code without sign) by index; ESCAPE code below.
512 const T16: [(u8, i8, i8, &str); 102] = [
513 (0, 0, 1, "10"),
514 (0, 0, 2, "1111"),
515 (0, 0, 3, "010101"),
516 (0, 0, 4, "0010111"),
517 (0, 0, 5, "00011111"),
518 (0, 0, 6, "000100101"),
519 (0, 0, 7, "000100100"),
520 (0, 0, 8, "0000100001"),
521 (0, 0, 9, "0000100000"),
522 (0, 0, 10, "00000000111"),
523 (0, 0, 11, "00000000110"),
524 (0, 0, 12, "00000100000"),
525 (0, 1, 1, "110"),
526 (0, 1, 2, "010100"),
527 (0, 1, 3, "00011110"),
528 (0, 1, 4, "0000001111"),
529 (0, 1, 5, "00000100001"),
530 (0, 1, 6, "000001010000"),
531 (0, 2, 1, "1110"),
532 (0, 2, 2, "00011101"),
533 (0, 2, 3, "0000001110"),
534 (0, 2, 4, "000001010001"),
535 (0, 3, 1, "01101"),
536 (0, 3, 2, "000100011"),
537 (0, 3, 3, "0000001101"),
538 (0, 4, 1, "01100"),
539 (0, 4, 2, "000100010"),
540 (0, 4, 3, "000001010010"),
541 (0, 5, 1, "01011"),
542 (0, 5, 2, "0000001100"),
543 (0, 5, 3, "000001010011"),
544 (0, 6, 1, "010011"),
545 (0, 6, 2, "0000001011"),
546 (0, 6, 3, "000001010100"),
547 (0, 7, 1, "010010"),
548 (0, 7, 2, "0000001010"),
549 (0, 8, 1, "010001"),
550 (0, 8, 2, "0000001001"),
551 (0, 9, 1, "010000"),
552 (0, 9, 2, "0000001000"),
553 (0, 10, 1, "0010110"),
554 (0, 10, 2, "000001010101"),
555 (0, 11, 1, "0010101"),
556 (0, 12, 1, "0010100"),
557 (0, 13, 1, "00011100"),
558 (0, 14, 1, "00011011"),
559 (0, 15, 1, "000100001"),
560 (0, 16, 1, "000100000"),
561 (0, 17, 1, "000011111"),
562 (0, 18, 1, "000011110"),
563 (0, 19, 1, "000011101"),
564 (0, 20, 1, "000011100"),
565 (0, 21, 1, "000011011"),
566 (0, 22, 1, "000011010"),
567 (0, 23, 1, "00000100010"),
568 (0, 24, 1, "00000100011"),
569 (0, 25, 1, "000001010110"),
570 (0, 26, 1, "000001010111"),
571 (1, 0, 1, "0111"),
572 (1, 0, 2, "000011001"),
573 (1, 0, 3, "00000000101"),
574 (1, 1, 1, "001111"),
575 (1, 1, 2, "00000000100"),
576 (1, 2, 1, "001110"),
577 (1, 3, 1, "001101"),
578 (1, 4, 1, "001100"),
579 (1, 5, 1, "0010011"),
580 (1, 6, 1, "0010010"),
581 (1, 7, 1, "0010001"),
582 (1, 8, 1, "0010000"),
583 (1, 9, 1, "00011010"),
584 (1, 10, 1, "00011001"),
585 (1, 11, 1, "00011000"),
586 (1, 12, 1, "00010111"),
587 (1, 13, 1, "00010110"),
588 (1, 14, 1, "00010101"),
589 (1, 15, 1, "00010100"),
590 (1, 16, 1, "00010011"),
591 (1, 17, 1, "000011000"),
592 (1, 18, 1, "000010111"),
593 (1, 19, 1, "000010110"),
594 (1, 20, 1, "000010101"),
595 (1, 21, 1, "000010100"),
596 (1, 22, 1, "000010011"),
597 (1, 23, 1, "000010010"),
598 (1, 24, 1, "000010001"),
599 (1, 25, 1, "0000000111"),
600 (1, 26, 1, "0000000110"),
601 (1, 27, 1, "0000000101"),
602 (1, 28, 1, "0000000100"),
603 (1, 29, 1, "00000100100"),
604 (1, 30, 1, "00000100101"),
605 (1, 31, 1, "00000100110"),
606 (1, 32, 1, "00000100111"),
607 (1, 33, 1, "000001011000"),
608 (1, 34, 1, "000001011001"),
609 (1, 35, 1, "000001011010"),
610 (1, 36, 1, "000001011011"),
611 (1, 37, 1, "000001011100"),
612 (1, 38, 1, "000001011101"),
613 (1, 39, 1, "000001011110"),
614 (1, 40, 1, "000001011111"),
615 ];
616 const T16_ESC: &str = "0000011";
617 /// Table I.2/H.263 rows in index order.
618 const TI2: [(u8, i8, i8, &str); 102] = [
619 (0, 0, 1, "10"),
620 (0, 1, 1, "1111"),
621 (0, 3, 1, "010101"),
622 (0, 5, 1, "0010111"),
623 (0, 7, 1, "00011111"),
624 (0, 8, 1, "000100101"),
625 (0, 9, 1, "000100100"),
626 (0, 10, 1, "0000100001"),
627 (0, 11, 1, "0000100000"),
628 (0, 4, 3, "00000000111"),
629 (0, 9, 2, "00000000110"),
630 (0, 13, 1, "00000100000"),
631 (0, 0, 2, "110"),
632 (0, 1, 2, "010100"),
633 (0, 1, 4, "00011110"),
634 (0, 1, 5, "0000001111"),
635 (0, 1, 6, "00000100001"),
636 (0, 1, 7, "000001010000"),
637 (0, 0, 3, "1110"),
638 (0, 3, 2, "00011101"),
639 (0, 2, 3, "0000001110"),
640 (0, 3, 4, "000001010001"),
641 (0, 0, 5, "01101"),
642 (0, 4, 2, "000100011"),
643 (0, 3, 3, "0000001101"),
644 (0, 0, 4, "01100"),
645 (0, 5, 2, "000100010"),
646 (0, 5, 3, "000001010010"),
647 (0, 2, 1, "01011"),
648 (0, 6, 2, "0000001100"),
649 (0, 0, 25, "000001010011"),
650 (0, 4, 1, "010011"),
651 (0, 7, 2, "0000001011"),
652 (0, 0, 24, "000001010100"),
653 (0, 0, 8, "010010"),
654 (0, 8, 2, "0000001010"),
655 (0, 0, 7, "010001"),
656 (0, 2, 4, "0000001001"),
657 (0, 0, 6, "010000"),
658 (0, 12, 1, "0000001000"),
659 (0, 0, 9, "0010110"),
660 (0, 0, 23, "000001010101"),
661 (0, 2, 2, "0010101"),
662 (0, 1, 3, "0010100"),
663 (0, 6, 1, "00011100"),
664 (0, 0, 10, "00011011"),
665 (0, 0, 12, "000100001"),
666 (0, 0, 11, "000100000"),
667 (0, 0, 18, "000011111"),
668 (0, 0, 17, "000011110"),
669 (0, 0, 16, "000011101"),
670 (0, 0, 15, "000011100"),
671 (0, 0, 14, "000011011"),
672 (0, 0, 13, "000011010"),
673 (0, 0, 20, "00000100010"),
674 (0, 0, 19, "00000100011"),
675 (0, 0, 22, "000001010110"),
676 (0, 0, 21, "000001010111"),
677 (1, 0, 1, "0111"),
678 (1, 14, 1, "000011001"),
679 (1, 20, 1, "00000000101"),
680 (1, 1, 1, "001111"),
681 (1, 19, 1, "00000000100"),
682 (1, 2, 1, "001110"),
683 (1, 3, 1, "001101"),
684 (1, 0, 2, "001100"),
685 (1, 5, 1, "0010011"),
686 (1, 6, 1, "0010010"),
687 (1, 4, 1, "0010001"),
688 (1, 0, 3, "0010000"),
689 (1, 9, 1, "00011010"),
690 (1, 10, 1, "00011001"),
691 (1, 11, 1, "00011000"),
692 (1, 12, 1, "00010111"),
693 (1, 13, 1, "00010110"),
694 (1, 8, 1, "00010101"),
695 (1, 7, 1, "00010100"),
696 (1, 0, 4, "00010011"),
697 (1, 17, 1, "000011000"),
698 (1, 18, 1, "000010111"),
699 (1, 16, 1, "000010110"),
700 (1, 15, 1, "000010101"),
701 (1, 2, 2, "000010100"),
702 (1, 1, 2, "000010011"),
703 (1, 0, 6, "000010010"),
704 (1, 0, 5, "000010001"),
705 (1, 4, 2, "0000000111"),
706 (1, 3, 2, "0000000110"),
707 (1, 1, 3, "0000000101"),
708 (1, 0, 7, "0000000100"),
709 (1, 2, 3, "00000100100"),
710 (1, 1, 4, "00000100101"),
711 (1, 0, 9, "00000100110"),
712 (1, 0, 8, "00000100111"),
713 (1, 21, 1, "000001011000"),
714 (1, 22, 1, "000001011001"),
715 (1, 23, 1, "000001011010"),
716 (1, 7, 2, "000001011011"),
717 (1, 6, 2, "000001011100"),
718 (1, 5, 2, "000001011101"),
719 (1, 3, 3, "000001011110"),
720 (1, 0, 10, "000001011111"),
721 ];
722
723 #[test]
724 fn mcbpc_matches_tables_7_and_8() {
725 for (i, r) in T7.iter().enumerate() {
726 let (c, n) = val(r.2);
727 assert_eq!(
728 (
729 H263_INTRA_MCBPC_CODE[i] as u32,
730 H263_INTRA_MCBPC_BITS[i] as u32
731 ),
732 (c, n)
733 );
734 }
735 for r in T8.iter() {
736 // group = flags of the MB type (INTRA 1, +Q 2, 4V 4); stuffing is group 5.
737 let group = match r.0 {
738 "Stuffing" => 5,
739 t => [0, 2, 4, 1, 3, 6][t.parse::<usize>().unwrap()],
740 };
741 let slot = 4 * group
742 + if r.0 == "Stuffing" {
743 0
744 } else {
745 usize::from_str_radix(r.1, 2).unwrap()
746 };
747 let (c, n) = val(r.2);
748 assert_eq!(
749 (
750 H263_INTER_MCBPC_CODE[slot] as u32,
751 H263_INTER_MCBPC_BITS[slot] as u32
752 ),
753 (c, n)
754 );
755 }
756 for slot in 21..24 {
757 assert_eq!(H263_INTER_MCBPC_BITS[slot], 0);
758 }
759 }
760
761 #[test]
762 fn cbpy_and_mvd_match_tables_12_and_14() {
763 for (i, c) in T12.iter().enumerate() {
764 let (v, n) = val(c);
765 assert_eq!(
766 (H263_CBPY_TAB[i][0] as u32, H263_CBPY_TAB[i][1] as u32),
767 (v, n)
768 );
769 }
770 for (i, c) in T14.iter().enumerate() {
771 let (v, n) = val(c);
772 assert_eq!((MVTAB[i][0] as u32, MVTAB[i][1] as u32), (v, n));
773 }
774 }
775
776 fn check_rl(
777 rows: &[(u8, i8, i8, &str); 102],
778 vlc: &[[u16; 2]],
779 run: &[i8],
780 level: &[i8],
781 last: usize,
782 ) {
783 let mut sorted: Vec<_> = rows.to_vec();
784 sorted.sort_by_key(|r| (r.0, r.1, r.2));
785 assert_eq!(sorted.iter().filter(|r| r.0 == 0).count(), last);
786 for (i, r) in sorted.iter().enumerate() {
787 let (v, n) = val(r.3);
788 assert_eq!((vlc[i][0] as u32, vlc[i][1] as u32), (v, n), "entry {i}");
789 assert_eq!((run[i], level[i]), (r.1, r.2), "entry {i}");
790 }
791 let (v, n) = val(T16_ESC);
792 assert_eq!((vlc[102][0] as u32, vlc[102][1] as u32), (v, n));
793 }
794
795 #[test]
796 fn tcoef_matches_tables_16_and_i2() {
797 check_rl(
798 &T16,
799 &INTER_VLC,
800 &INTER_RUN,
801 &INTER_LEVEL,
802 H263_RL_INTER_LAST,
803 );
804 check_rl(
805 &TI2,
806 &INTRA_VLC_AIC,
807 &INTRA_RUN_AIC,
808 &INTRA_LEVEL_AIC,
809 RL_INTRA_AIC_LAST,
810 );
811 // Table I.2 re-uses the codewords of Table 16 (I.3).
812 let mut a: Vec<_> = T16.iter().map(|r| r.3).collect();
813 let mut b: Vec<_> = TI2.iter().map(|r| r.3).collect();
814 a.sort();
815 b.sort();
816 assert_eq!(a, b);
817 }
818
819 #[test]
820 fn small_tables_match_the_text() {
821 // Table O.4: 0, 10, 111, 110 for CBPC pattern 00, 01, 10, 11.
822 assert_eq!(CBPC_B_TAB, [[0, 1], [2, 2], [7, 3], [6, 3]]);
823 // Annex I.3: RecC = 2 * QUANT * LEVEL.
824 for q in 0..32 {
825 assert_eq!(AIC_DC_SCALE_TABLE[q] as usize, 2 * q);
826 }
827 // Table T.2.
828 for q in 1..32usize {
829 let c = match q {
830 1..=6 => q,
831 7..=9 => q - 1,
832 10..=11 => 9,
833 12..=13 => 10,
834 14..=15 => 11,
835 16..=18 => 12,
836 19..=21 => 13,
837 22..=26 => 14,
838 _ => 15,
839 };
840 assert_eq!(H263_CHROMA_QSCALE_TABLE[q] as usize, c, "QUANT {q}");
841 }
842 // Table T.1: change of QUANT for DQUANT = 10 / 11.
843 for q in 1..32i32 {
844 let (d10, d11) = match q {
845 1 => (2, 1),
846 2..=10 => (-1, 1),
847 11..=20 => (-2, 2),
848 21..=28 => (-3, 3),
849 29 => (-3, 2),
850 30 => (-3, 1),
851 _ => (-3, -5),
852 };
853 assert_eq!(MODIFIED_QUANT_TAB[0][q as usize] as i32, q + d10);
854 assert_eq!(MODIFIED_QUANT_TAB[1][q as usize] as i32, q + d11);
855 }
856 // Table K.2.
857 assert_eq!(MBA_MAX, [47, 98, 395, 1583, 6335, 9215]);
858 assert_eq!(MBA_LENGTH, [6, 7, 9, 11, 13, 14, 14]);
859 // Table 2 (formats) and Table 5 (PAR).
860 assert_eq!(H263_FORMAT[1], [128, 96]);
861 assert_eq!(H263_FORMAT[5], [1408, 1152]);
862 assert_eq!(
863 &H263_PIXEL_ASPECT[1..6],
864 &[[1, 1], [12, 11], [10, 11], [16, 11], [40, 33]]
865 );
866 }
867
868 #[test]
869 fn scans_match_figures() {
870 // Figure 14: first rows of the printed grid (scan positions, 1-based).
871 let fig14: [[u8; 8]; 8] = [
872 [1, 2, 6, 7, 15, 16, 28, 29],
873 [3, 5, 8, 14, 17, 27, 30, 43],
874 [4, 9, 13, 18, 26, 31, 42, 44],
875 [10, 12, 19, 25, 32, 41, 45, 54],
876 [11, 20, 24, 33, 40, 46, 53, 55],
877 [21, 23, 34, 39, 47, 52, 56, 61],
878 [22, 35, 38, 48, 51, 57, 60, 62],
879 [36, 37, 49, 50, 58, 59, 63, 64],
880 ];
881 for (i, &p) in ZIGZAG_DIRECT.iter().enumerate() {
882 assert_eq!(fig14[p as usize / 8][p as usize % 8] as usize, i + 1);
883 }
884 // Figure I.2-a and I.2-b.
885 let a: [[u8; 8]; 8] = [
886 [1, 2, 3, 4, 11, 12, 13, 14],
887 [5, 6, 9, 10, 18, 17, 16, 15],
888 [7, 8, 20, 19, 27, 28, 29, 30],
889 [21, 22, 25, 26, 31, 32, 33, 34],
890 [23, 24, 35, 36, 43, 44, 45, 46],
891 [37, 38, 41, 42, 47, 48, 49, 50],
892 [39, 40, 51, 52, 57, 58, 59, 60],
893 [53, 54, 55, 56, 61, 62, 63, 64],
894 ];
895 let b: [[u8; 8]; 8] = [
896 [1, 5, 7, 21, 23, 37, 39, 53],
897 [2, 6, 8, 22, 24, 38, 40, 54],
898 [3, 9, 20, 25, 35, 41, 51, 55],
899 [4, 10, 19, 26, 36, 42, 52, 56],
900 [11, 18, 27, 31, 43, 47, 57, 61],
901 [12, 17, 28, 32, 44, 48, 58, 62],
902 [13, 16, 29, 33, 45, 49, 59, 63],
903 [14, 15, 30, 34, 46, 50, 60, 64],
904 ];
905 for (i, &p) in ALTERNATE_HORIZONTAL_SCAN.iter().enumerate() {
906 assert_eq!(a[p as usize / 8][p as usize % 8] as usize, i + 1);
907 }
908 for (i, &p) in ALTERNATE_VERTICAL_SCAN.iter().enumerate() {
909 assert_eq!(b[p as usize / 8][p as usize % 8] as usize, i + 1);
910 }
911 }
912}