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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//! Standalone CELT frame decoding types and scaffolding.
//!
//! CELT (Constrained Energy Lapped Transform) is the music/wideband codec
//! used within Opus. This module provides lightweight frame-level types for
//! energy bookkeeping and the MDCT-IV inverse transform, independent of the
//! full Opus decoder pipeline found in `crate::opus::celt`.
use std::f32::consts::PI;
/// CELT band-edge positions in MDCT bins for a 48 kHz, 20 ms (960-sample) frame.
///
/// There are 21 bands bounded by 22 edges. The first edge is bin 0 and the
/// last is bin 100 (equivalent to 10 kHz at 48 kHz).
pub const CELT_BANDS: [usize; 22] = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100,
];
/// Number of CELT frequency bands.
const NUM_BANDS: usize = 21;
/// Configuration for a CELT frame decoder.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CeltFrameConfig {
/// Frame size in samples (120, 240, 480, 960, or 1920).
pub frame_size: usize,
/// Number of audio channels (1 or 2).
pub channels: u8,
/// First band index (for hybrid Opus mode).
pub start_band: u8,
/// One-past-last band index.
pub end_band: u8,
}
impl Default for CeltFrameConfig {
/// Default configuration: 960-sample stereo frame covering all 21 bands.
fn default() -> Self {
Self {
frame_size: 960,
channels: 2,
start_band: 0,
end_band: NUM_BANDS as u8,
}
}
}
impl CeltFrameConfig {
/// Sets the frame size and returns `self` for chaining.
pub fn with_frame_size(mut self, frame_size: usize) -> Self {
self.frame_size = frame_size;
self
}
/// Sets the channel count and returns `self` for chaining.
pub fn with_channels(mut self, channels: u8) -> Self {
self.channels = channels;
self
}
/// Sets the start band and returns `self` for chaining.
pub fn with_start_band(mut self, start_band: u8) -> Self {
self.start_band = start_band;
self
}
/// Sets the end band and returns `self` for chaining.
pub fn with_end_band(mut self, end_band: u8) -> Self {
self.end_band = end_band;
self
}
}
/// Per-band log-domain energy for a CELT frame.
///
/// All 21 CELT bands are tracked. Energy values are in the log domain
/// (natural logarithm of linear energy) as used internally by CELT.
#[derive(Debug, Clone)]
pub struct CeltEnergy {
/// Per-band energy values (log domain), indexed 0..21.
pub bands: [f32; NUM_BANDS],
}
impl CeltEnergy {
/// Creates a `CeltEnergy` with all bands initialised to zero.
pub fn new() -> Self {
Self {
bands: [0.0f32; NUM_BANDS],
}
}
/// Returns the energy for the given band index.
///
/// Returns `0.0` for out-of-range indices.
pub fn energy(&self, band: usize) -> f32 {
if band < NUM_BANDS {
self.bands[band]
} else {
0.0
}
}
/// Sets the energy for the given band index.
///
/// Out-of-range indices are silently ignored.
pub fn set_energy(&mut self, band: usize, val: f32) {
if band < NUM_BANDS {
self.bands[band] = val;
}
}
}
/// A decoded CELT frame.
#[derive(Debug, Clone)]
pub struct CeltFrame {
/// Frame configuration.
pub config: CeltFrameConfig,
/// Per-band energy decoded from the bitstream.
pub energy: CeltEnergy,
/// Bitmask of collapsed (zeroed) bands.
pub collapsed_mask: u32,
/// Decoded output samples.
pub samples: Vec<f32>,
}
impl CeltFrame {
/// Creates a new zeroed `CeltFrame` for the given configuration.
pub fn new(config: CeltFrameConfig) -> Self {
let sample_count = config.frame_size;
Self {
config,
energy: CeltEnergy::new(),
collapsed_mask: 0,
samples: vec![0.0f32; sample_count],
}
}
/// Returns the number of samples in this frame (per channel).
pub fn sample_count(&self) -> usize {
self.config.frame_size
}
}
/// CELT frame decoder scaffold.
///
/// Provides energy decoding and the MDCT-IV inverse transform. Full
/// entropy-coded CELT decoding (PVQ, fine energy, band prediction …) is
/// provided by `crate::opus::celt`; this type is intentionally lightweight
/// and intended for testing and scaffolding.
#[derive(Debug)]
pub struct CeltDecoder {
/// Frame configuration.
pub config: CeltFrameConfig,
/// Per-band energy state carried across frames.
pub prev_energy: CeltEnergy,
}
impl CeltDecoder {
/// Creates a new `CeltDecoder` for the given configuration.
pub fn new(config: CeltFrameConfig) -> Self {
Self {
config,
prev_energy: CeltEnergy::new(),
}
}
/// Parses energy values from the first bytes of `data` and returns a
/// `CeltFrame` with zeroed samples.
///
/// Each active band contributes one byte to the energy encoding: the byte
/// is interpreted as a signed i8 and scaled by 1/16 to produce a
/// log-domain energy value. Bands beyond the end of `data` default to
/// `0.0`.
///
/// Full PVQ coefficient decoding is not performed; this method is a
/// scaffold.
pub fn decode_frame(&mut self, data: &[u8]) -> Result<CeltFrame, String> {
let mut frame = CeltFrame::new(self.config.clone());
let start = self.config.start_band as usize;
let end = self.config.end_band as usize;
for band in start..end {
let band_idx = band - start;
let energy = if band_idx < data.len() {
let raw = data[band_idx] as i8;
raw as f32 / 16.0
} else {
0.0
};
// Delta from previous frame (simple inter-frame prediction).
let predicted = self.prev_energy.energy(band);
let new_energy = predicted + energy;
frame.energy.set_energy(band, new_energy);
self.prev_energy.set_energy(band, new_energy);
}
Ok(frame)
}
/// Computes the Type-IV MDCT inverse transform (IMDCT-IV).
///
/// Given `N = coeffs.len()` spectral coefficients `X[k]`, the output
/// time-domain samples are:
///
/// ```text
/// x[n] = sqrt(2/N) * sum_{k=0}^{N-1} X[k] * cos(Ï€/N * (n + 0.5 + N/2) * (k + 0.5))
/// ```
///
/// for `n = 0 .. N-1`.
///
/// Returns an empty vector if `coeffs` is empty.
pub fn apply_mdct_inverse(&self, coeffs: &[f32]) -> Vec<f32> {
let n = coeffs.len();
if n == 0 {
return Vec::new();
}
let scale = (2.0f32 / n as f32).sqrt();
let mut output = vec![0.0f32; n];
for (idx, out) in output.iter_mut().enumerate() {
let n_f = n as f32;
let nn = idx as f32 + 0.5 + n_f / 2.0;
let mut acc = 0.0f32;
for (k, &coeff) in coeffs.iter().enumerate() {
let kk = k as f32 + 0.5;
acc += coeff * (PI / n_f * nn * kk).cos();
}
*out = scale * acc;
}
output
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_celt_bands_has_22_elements() {
assert_eq!(CELT_BANDS.len(), 22, "21 bands require 22 edges");
}
#[test]
fn test_celt_bands_starts_at_zero() {
assert_eq!(CELT_BANDS[0], 0);
}
#[test]
fn test_celt_bands_monotonically_increasing() {
for i in 1..CELT_BANDS.len() {
assert!(
CELT_BANDS[i] > CELT_BANDS[i - 1],
"CELT_BANDS[{}] = {} should be > CELT_BANDS[{}] = {}",
i,
CELT_BANDS[i],
i - 1,
CELT_BANDS[i - 1]
);
}
}
#[test]
fn test_celt_frame_config_default() {
let cfg = CeltFrameConfig::default();
assert_eq!(cfg.frame_size, 960);
assert_eq!(cfg.channels, 2);
assert_eq!(cfg.start_band, 0);
assert_eq!(cfg.end_band, 21);
}
#[test]
fn test_celt_frame_config_builder() {
let cfg = CeltFrameConfig::default()
.with_frame_size(480)
.with_channels(1)
.with_start_band(2)
.with_end_band(18);
assert_eq!(cfg.frame_size, 480);
assert_eq!(cfg.channels, 1);
assert_eq!(cfg.start_band, 2);
assert_eq!(cfg.end_band, 18);
}
#[test]
fn test_celt_energy_new_all_zero() {
let energy = CeltEnergy::new();
for band in 0..NUM_BANDS {
assert_eq!(energy.energy(band), 0.0);
}
}
#[test]
fn test_celt_energy_set_and_get() {
let mut energy = CeltEnergy::new();
energy.set_energy(5, 3.14);
assert!((energy.energy(5) - 3.14).abs() < 1e-6);
}
#[test]
fn test_celt_energy_out_of_range() {
let mut energy = CeltEnergy::new();
// Out-of-range set should not panic.
energy.set_energy(100, 99.0);
// Out-of-range get should return 0.
assert_eq!(energy.energy(100), 0.0);
}
#[test]
fn test_celt_frame_sample_count() {
let cfg = CeltFrameConfig::default().with_frame_size(480);
let frame = CeltFrame::new(cfg);
assert_eq!(frame.sample_count(), 480);
}
#[test]
fn test_celt_frame_sample_count_960() {
let cfg = CeltFrameConfig::default();
let frame = CeltFrame::new(cfg);
assert_eq!(frame.sample_count(), 960);
}
#[test]
fn test_celt_decoder_new() {
let cfg = CeltFrameConfig::default();
let dec = CeltDecoder::new(cfg.clone());
assert_eq!(dec.config, cfg);
}
#[test]
fn test_celt_decoder_decode_frame_returns_correct_size() {
let cfg = CeltFrameConfig::default().with_frame_size(480);
let mut dec = CeltDecoder::new(cfg);
let data = vec![0u8; 21];
let frame = dec.decode_frame(&data).expect("should succeed");
assert_eq!(frame.sample_count(), 480);
}
#[test]
fn test_celt_decoder_decode_frame_zero_data_zero_energy() {
let cfg = CeltFrameConfig::default();
let mut dec = CeltDecoder::new(cfg);
let data = vec![0u8; 21];
let frame = dec.decode_frame(&data).expect("should succeed");
for band in 0..NUM_BANDS {
assert_eq!(frame.energy.energy(band), 0.0);
}
}
#[test]
fn test_celt_decoder_apply_mdct_inverse_all_zero_input() {
let cfg = CeltFrameConfig::default();
let dec = CeltDecoder::new(cfg);
// All-zero coefficients must produce all-zero output.
let coeffs = vec![0.0f32; 16];
let output = dec.apply_mdct_inverse(&coeffs);
assert_eq!(output.len(), 16);
for &sample in &output {
assert!(sample.abs() < 1e-6, "expected zero, got {}", sample);
}
}
#[test]
fn test_celt_decoder_apply_mdct_inverse_empty_input() {
let cfg = CeltFrameConfig::default();
let dec = CeltDecoder::new(cfg);
let output = dec.apply_mdct_inverse(&[]);
assert!(output.is_empty());
}
#[test]
fn test_celt_decoder_apply_mdct_inverse_nonzero() {
let cfg = CeltFrameConfig::default();
let dec = CeltDecoder::new(cfg);
// Single non-zero DC coefficient should yield non-zero output.
let mut coeffs = vec![0.0f32; 8];
coeffs[0] = 1.0;
let output = dec.apply_mdct_inverse(&coeffs);
assert_eq!(output.len(), 8);
let any_nonzero = output.iter().any(|&s| s.abs() > 1e-6);
assert!(any_nonzero, "IMDCT of non-zero input must not be all-zero");
}
}