forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

residue.rs142 lines, 5.0 KiBHistoryRawDownload
1//! Residue decode (§8): the spectrum that rides on the floor.
2//!
3//! All three residue types share one loop — partitions classified in groups,
4//! then up to eight refinement passes over those classes — and differ only in
5//! how a partition's values are laid out. Type 2 differs one level up: it codes
6//! every channel of a submap as one interleaved vector, which is what makes
7//! coupled stereo cheap.
8
9use crate::bits::Bits;
10use crate::codebook::Codebook;
11use crate::setup::ResidueConfig;
12
13/// Decode one residue into `vectors`, which the caller has zeroed.
14///
15/// For types 0 and 1 there is one vector per channel of the submap; for type 2
16/// the caller passes a single interleaved vector `channels * n` long. Channels
17/// flagged in `skip` are not coded at all (an unused floor, or a coupling
18/// partner already covered).
19pub fn decode(
20 config: &ResidueConfig,
21 codebooks: &[Codebook],
22 bits: &mut Bits,
23 vectors: &mut [Vec<f32>],
24 skip: &[bool],
25) {
26 let Some(size) = vectors.first().map(Vec::len) else {
27 return;
28 };
29 if skip.iter().all(|&s| s) {
30 return;
31 }
32 let begin = config.begin.min(size);
33 let end = config.end.min(size);
34 let to_read = end.saturating_sub(begin);
35 let partitions = to_read / config.partition_size;
36 if partitions == 0 {
37 return;
38 }
39 let classbook = &codebooks[config.classbook];
40 let per_word = classbook.dimensions.max(1);
41 let classes = config.classifications as u32;
42 let mut classification = vec![vec![0u8; partitions + per_word]; vectors.len()];
43
44 for pass in 0..8usize {
45 let mut partition = 0usize;
46 while partition < partitions {
47 if pass == 0 {
48 for (channel, row) in classification.iter_mut().enumerate() {
49 if skip[channel] {
50 continue;
51 }
52 let Some(mut word) = classbook.decode_scalar(bits) else {
53 return;
54 };
55 // The class word packs `per_word` classes, most recent
56 // partition in the least significant digit.
57 for i in (0..per_word).rev() {
58 row[partition + i] = (word % classes) as u8;
59 word /= classes;
60 }
61 }
62 }
63 for _ in 0..per_word {
64 if partition >= partitions {
65 break;
66 }
67 for (channel, vector) in vectors.iter_mut().enumerate() {
68 if skip[channel] {
69 continue;
70 }
71 let class = usize::from(classification[channel][partition]);
72 let book = config.books[class][pass];
73 if book < 0 {
74 continue;
75 }
76 let offset = begin + partition * config.partition_size;
77 let slice = &mut vector[offset..offset + config.partition_size];
78 if !decode_partition(config.kind, &codebooks[book as usize], bits, slice) {
79 return;
80 }
81 }
82 partition += 1;
83 }
84 }
85 }
86}
87
88/// One partition's worth of values, added onto what earlier passes left.
89fn decode_partition(kind: u8, book: &Codebook, bits: &mut Bits, out: &mut [f32]) -> bool {
90 if !book.has_values() {
91 return false;
92 }
93 let dimensions = book.dimensions;
94 match kind {
95 // Type 0 lays a codeword's values out strided, so one codeword covers
96 // the whole partition at `step` spacing.
97 0 => {
98 let step = out.len() / dimensions.max(1);
99 for i in 0..step {
100 let Some(values) = book.decode_vector(bits) else {
101 return false;
102 };
103 for (j, &value) in values.iter().enumerate() {
104 if let Some(slot) = out.get_mut(i + j * step) {
105 *slot += value;
106 }
107 }
108 }
109 }
110 // Types 1 and 2 lay them out contiguously.
111 _ => {
112 let mut i = 0usize;
113 while i < out.len() {
114 let Some(values) = book.decode_vector(bits) else {
115 return false;
116 };
117 for &value in values {
118 if i >= out.len() {
119 break;
120 }
121 out[i] += value;
122 i += 1;
123 }
124 if dimensions == 0 {
125 return false;
126 }
127 }
128 }
129 }
130 true
131}
132
133/// Spread a type-2 interleaved vector back over its channels.
134pub fn deinterleave(interleaved: &[f32], channels: usize, out: &mut [Vec<f32>]) {
135 for (i, &value) in interleaved.iter().enumerate() {
136 let channel = i % channels;
137 let position = i / channels;
138 if let Some(slot) = out[channel].get_mut(position) {
139 *slot = value;
140 }
141 }
142}