forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

spec_tables.rs145 lines, 5.0 KiBHistoryRawDownload
1//! The constant tables in `tables_data.rs` against the numbers printed in
2//! ETSI TS 102 114 V1.6.1 (2019-08), committed in `spec_ts102114_tables.txt`
3//! (written by `tools/gen_dts_tables.py`, with the clause of every table).
4//!
5//! The conversions from printed number to Rust value are repeated here
6//! independently of the generator.
7
8use ec_dts::tables_data::*;
9use std::collections::HashMap;
10
11const SPEC: &str = include_str!("spec_ts102114_tables.txt");
12
13struct Spec {
14 tables: HashMap<String, Vec<String>>,
15 huff: HashMap<String, Vec<(i32, u32, u64)>>,
16}
17
18fn load() -> Spec {
19 let mut s = Spec {
20 tables: HashMap::new(),
21 huff: HashMap::new(),
22 };
23 for line in SPEC.lines().filter(|l| !l.starts_with('#') && !l.is_empty()) {
24 let parts: Vec<&str> = line.splitn(3, " | ").collect();
25 let head: Vec<&str> = parts[0].split(' ').collect();
26 match head[0] {
27 "table" => {
28 s.tables.insert(
29 head[1].to_string(),
30 parts[2].split(' ').map(str::to_string).collect(),
31 );
32 }
33 "huff" => {
34 let e = parts[2]
35 .split(' ')
36 .map(|t| {
37 let v: Vec<&str> = t.split(':').collect();
38 (v[0].parse().unwrap(), v[1].parse().unwrap(), v[2].parse().unwrap())
39 })
40 .collect();
41 s.huff.insert(head[1].to_string(), e);
42 }
43 other => panic!("unknown record {other}"),
44 }
45 }
46 s
47}
48
49/// Integers with "invalid" read as 0 and zero padding to `n`.
50fn ints(v: &[String], n: usize) -> Vec<u32> {
51 let mut o: Vec<u32> = v
52 .iter()
53 .map(|x| if x == "invalid" { 0 } else { x.parse().unwrap() })
54 .collect();
55 o.resize(n, 0);
56 o
57}
58
59#[test]
60fn integer_tables_match_spec() {
61 let s = load();
62 assert_eq!(SCALE_FACTOR_QUANT6.to_vec(), ints(&s.tables["SCALE_FACTOR_QUANT6"], 64)); // D.1.1
63 assert_eq!(SCALE_FACTOR_QUANT7.to_vec(), ints(&s.tables["SCALE_FACTOR_QUANT7"], 128)); // D.1.2
64 assert_eq!(LOSSY_QUANT.to_vec(), ints(&s.tables["LOSSY_QUANT"], 32)); // D.2.1
65 assert_eq!(LOSSLESS_QUANT.to_vec(), ints(&s.tables["LOSSLESS_QUANT"], 32)); // D.2.2
66 // Table 5-26: ABITS 0 is 1 here (spec 0), 27..31 invalid.
67 let mut ql = ints(&s.tables["QUANT_LEVELS"], 32);
68 ql[0] = 1;
69 assert_eq!(QUANT_LEVELS.to_vec(), ql);
70 // Table 5-27: adjustment values x 2^22.
71 let adj: Vec<u32> = s.tables["SCALE_FACTOR_ADJ"]
72 .iter()
73 .map(|x| (x.parse::<f64>().unwrap() * 4194304.0) as u32)
74 .collect();
75 assert_eq!(SCALE_FACTOR_ADJ.to_vec(), adj);
76 // D.11: DmixTable preceded by a 0 entry.
77 let mut dm = vec![0u32];
78 dm.extend(ints(&s.tables["DMIXTABLE"], 241));
79 assert_eq!(DMIXTABLE.iter().map(|&x| x as u32).collect::<Vec<_>>(), dm);
80}
81
82#[test]
83fn float_and_fir_tables_match_spec() {
84 let s = load();
85 // D.9: round-half-up(coef * 2^29), cosine modulation sign per 32 taps.
86 let fir = &s.tables["FIR_64"];
87 assert_eq!(fir.len(), 1024);
88 for (i, t) in fir.iter().enumerate() {
89 let v = (t.parse::<f64>().unwrap() * 536870912.0 + 0.5).floor() as i32;
90 let v = if (i / 32) % 4 == 0 { v } else { -v };
91 assert_eq!(FIR_64[i], v, "FIR_64[{i}]");
92 }
93 // D.8, 128x column, polyphase order.
94 let lfe = &s.tables["LFE_FIR_128"];
95 assert_eq!(lfe.len(), 512);
96 for (k, got) in LFE_FIR_128.iter().enumerate() {
97 let (q, p) = (k / 4, k % 4);
98 let idx = [q, 128 + q, 255 - q, 127 - q][p];
99 let want: f32 = lfe[idx].parse().unwrap();
100 assert_eq!(got.to_bits(), want.to_bits(), "LFE_FIR_128[{k}]");
101 }
102}
103
104#[test]
105fn huffman_books_match_spec() {
106 let s = load();
107 let mut order: Vec<(String, i32)> = Vec::new(); // (table, symbol = level + offset)
108 for (n, groups, off) in [
109 (3, "A", 1),
110 (5, "ABC", 2),
111 (7, "ABC", 3),
112 (9, "ABC", 4),
113 (13, "ABC", 6),
114 (17, "ABCDEFG", 8),
115 (25, "ABCDEFG", 12),
116 (33, "ABCDEFG", 16),
117 (65, "ABCDEFG", 32),
118 (129, "ABCDEFG", 64),
119 ] {
120 for g in groups.chars() {
121 order.push((format!("{g}{n}"), off));
122 }
123 }
124 for g in "ABCDE".chars() {
125 order.push((format!("{g}12"), -1)); // level column is ABITS = symbol + 1
126 }
127 for g in "ABCDE".chars() {
128 order.push((format!("S{g}129"), 64));
129 }
130 for g in "ABCD".chars() {
131 order.push((format!("{g}4"), 0));
132 }
133 assert_eq!(s.huff.len(), order.len());
134 let mut got: Vec<(u8, u8)> = Vec::new();
135 for (name, off) in &order {
136 let mut e = s.huff[name].clone();
137 // a prefix code with no gaps
138 let kraft: u64 = e.iter().map(|&(_, l, _)| 1u64 << (32 - l)).sum();
139 assert_eq!(kraft, 1u64 << 32, "{name} is not a complete prefix code");
140 e.sort_by_key(|&(_, l, c)| c << (32 - l));
141 got.extend(e.iter().map(|&(lv, l, _)| ((lv + off) as u8, l as u8)));
142 }
143 assert_eq!(HUFF_SRC.len(), got.len());
144 assert_eq!(HUFF_SRC.to_vec(), got);
145}