webmedia 0.2.0

Standalone raster image and streaming video decoders for Rust applications
Documentation
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! CELT TF-choice decoding and exact adjustment tables from RFC 6716
//! section 4.3.4.5, Tables 60, 61, 62, and 63. No reference-only table needed.

use super::{celt::Layout, invalid, range::RangeDecoder};
use crate::video::backend::MediaDecodeError;

// [transient][tf_select][LM][choice].
const ADJUSTMENTS: [[[[i8; 2]; 4]; 2]; 2] = [
    [
        [[0, -1], [0, -1], [0, -2], [0, -2]],
        [[0, -1], [0, -2], [0, -3], [0, -3]],
    ],
    [
        [[0, -1], [1, 0], [2, 0], [3, 0]],
        [[0, -1], [1, -1], [1, -1], [1, -1]],
    ],
];

/// Inverse band transform in RFC 6716 section 4.3.4.5, final paragraph.
/// Both input and output use frequency-major, time-block-minor storage.
/// This implements the specified normalized Hadamard math; packet-level
/// interoperability still depends on the missing allocation/split decisions.
#[derive(Clone, Copy, Debug)]
pub struct BandTransform {
    width: usize,
    original_blocks: usize,
    coded_blocks: usize,
    adjustment: i8,
}

impl BandTransform {
    pub fn new(
        layout: Layout,
        band: usize,
        transient: bool,
        adjustment: i8,
    ) -> Result<Self, MediaDecodeError> {
        let original_blocks = layout.blocks(transient)?;
        let width = layout.band(band)?.len();
        let lm = (layout.samples() / 120).trailing_zeros() as usize;
        if !ADJUSTMENTS[usize::from(transient)]
            .iter()
            .any(|table| table[lm].contains(&adjustment))
        {
            return Err(invalid("invalid CELT band TF adjustment"));
        }
        let factor = 1usize << adjustment.unsigned_abs();
        let coded_blocks = if adjustment >= 0 {
            if original_blocks % factor != 0 {
                return Err(invalid("CELT TF exceeds available time blocks"));
            }
            original_blocks / factor
        } else {
            original_blocks * factor
        };
        if width % coded_blocks != 0 {
            // The prose does not define narrow-band omission rules. Do not
            // invent a partial transform when a full level does not fit.
            return Err(MediaDecodeError::Unsupported);
        }
        Ok(Self {
            width,
            original_blocks,
            coded_blocks,
            adjustment,
        })
    }

    /// Spreading precedes the inverse TF transform and uses coded time blocks.
    pub fn coded_blocks(self) -> usize {
        self.coded_blocks
    }

    pub fn inverse(self, vector: &mut [f64]) -> Result<(), MediaDecodeError> {
        if vector.len() != self.width || vector.iter().any(|value| !value.is_finite()) {
            return Err(invalid("invalid CELT inverse TF vector"));
        }
        if self.adjustment == 0 {
            return Ok(());
        }
        let factor = 1usize << self.adjustment.unsigned_abs();
        let gain = (factor as f64).sqrt().recip();
        let mut output = vec![0.0; self.width];
        if self.adjustment > 0 {
            // Undo frequency refinement across the original short MDCTs.
            for frequency in 0..self.width / self.original_blocks {
                for block in 0..self.coded_blocks {
                    let mut group = [0.0; 8];
                    for (k, value) in group[..factor].iter_mut().enumerate() {
                        *value = vector[(frequency * factor + k) * self.coded_blocks + block];
                    }
                    hadamard(&mut group[..factor]);
                    for (k, &value) in group[..factor].iter().enumerate() {
                        output[frequency * self.original_blocks + block * factor + k] =
                            value * gain;
                    }
                }
            }
        } else {
            // Time-refined input is in sequency order, not Sylvester order.
            // H_sequency = P*H, so the inverse is H*P^-1, not P*H.
            for frequency in 0..self.width / self.coded_blocks {
                for block in 0..self.original_blocks {
                    let mut group = [0.0; 8];
                    for time in 0..factor {
                        let natural = sequency_index(time, factor.trailing_zeros());
                        group[natural] =
                            vector[frequency * self.coded_blocks + block * factor + time];
                    }
                    hadamard(&mut group[..factor]);
                    for (k, &value) in group[..factor].iter().enumerate() {
                        output[(frequency * factor + k) * self.original_blocks + block] =
                            value * gain;
                    }
                }
            }
        }
        if output.iter().any(|value| !value.is_finite()) {
            return Err(invalid("CELT inverse TF overflow"));
        }
        vector.copy_from_slice(&output);
        Ok(())
    }
}

fn hadamard(vector: &mut [f64]) {
    let mut half = 1;
    while half < vector.len() {
        for group in vector.chunks_exact_mut(2 * half) {
            for index in 0..half {
                let (a, b) = (group[index], group[index + half]);
                group[index] = a + b;
                group[index + half] = a - b;
            }
        }
        half *= 2;
    }
}

fn sequency_index(index: usize, bits: u32) -> usize {
    let gray = index ^ (index >> 1);
    gray.reverse_bits() >> (usize::BITS - bits)
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TfResolution {
    pub selected: bool,
    pub choices: Vec<bool>,
    pub adjustments: Vec<i8>,
}

impl TfResolution {
    /// Invoke after coarse energy, before spread and allocation. A conservative
    /// whole-sequence budget guard intentionally rejects small-budget cases
    /// until their precise omission/reservation rules are independently known.
    /// Failure does not consume entropy.
    pub fn decode(
        decoder: &mut RangeDecoder<'_>,
        layout: Layout,
        transient: bool,
    ) -> Result<Self, MediaDecodeError> {
        layout.blocks(transient)?;
        let lm = (layout.samples() / 120).trailing_zeros() as usize;
        let bands = layout.bands().len();
        let first_logp = if transient { 2 } else { 4 };
        let later_logp = if transient { 4 } else { 5 };
        let selection_possible = ADJUSTMENTS[usize::from(transient)][0][lm]
            != ADJUSTMENTS[usize::from(transient)][1][lm];
        let maximum_bits = first_logp + (bands - 1) * later_logp + usize::from(selection_possible);
        if decoder.tell().saturating_add(maximum_bits as u64) > decoder.frame_bytes() as u64 * 8 {
            return Err(MediaDecodeError::Unsupported);
        }
        let mut cursor = decoder.clone();
        let mut choices = Vec::with_capacity(bands);
        let mut choice = false;
        for index in 0..bands {
            choice ^= cursor.bit(if index == 0 {
                first_logp as u8
            } else {
                later_logp as u8
            })?;
            choices.push(choice);
        }
        let selection_matters = choices.iter().any(|&choice| {
            ADJUSTMENTS[usize::from(transient)][0][lm][usize::from(choice)]
                != ADJUSTMENTS[usize::from(transient)][1][lm][usize::from(choice)]
        });
        let selected = selection_matters && cursor.bit(1)?;
        let adjustments = choices
            .iter()
            .map(|&choice| {
                ADJUSTMENTS[usize::from(transient)][usize::from(selected)][lm][usize::from(choice)]
            })
            .collect();
        *decoder = cursor;
        Ok(Self {
            selected,
            choices,
            adjustments,
        })
    }

    pub fn adjustment(
        lm: u8,
        transient: bool,
        selected: bool,
        choice: bool,
    ) -> Result<i8, MediaDecodeError> {
        if lm > 3 {
            return Err(invalid("invalid CELT TF frame duration"));
        }
        Ok(
            ADJUSTMENTS[usize::from(transient)][usize::from(selected)][usize::from(lm)]
                [usize::from(choice)],
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sequency_rows_have_exactly_the_requested_number_of_sign_changes() {
        for bits in 1..=3 {
            let size = 1 << bits;
            for row in 0..size {
                let natural = sequency_index(row, bits);
                let signs: Vec<_> = (0..size)
                    .map(|column| (natural & column).count_ones() & 1)
                    .collect();
                assert_eq!(
                    signs.windows(2).filter(|pair| pair[0] != pair[1]).count(),
                    row
                );
            }
        }
    }

    #[test]
    fn inverse_tf_matches_direct_hadamard_matrix_and_preserves_energy() {
        let layout = Layout::from_configuration(31).unwrap().unwrap();
        for (transient, adjustment) in [(true, 1), (true, 3), (true, -1), (false, -2), (false, -3)]
        {
            let transform = BandTransform::new(layout, 20, transient, adjustment).unwrap();
            let input: Vec<_> = (0..transform.width)
                .map(|i| (i % 17) as f64 - 8.0)
                .collect();
            let mut actual = input.clone();
            transform.inverse(&mut actual).unwrap();
            let factor = 1usize << adjustment.unsigned_abs();
            let mut expected = vec![0.0; actual.len()];
            let scale = (factor as f64).sqrt().recip();
            if adjustment > 0 {
                for frequency in 0..actual.len() / transform.original_blocks {
                    for block in 0..transform.coded_blocks {
                        for time in 0..factor {
                            let value = (0..factor)
                                .map(|column| {
                                    let sign = if (time & column).count_ones() & 1 == 0 {
                                        1.0
                                    } else {
                                        -1.0
                                    };
                                    sign * input[(frequency * factor + column)
                                        * transform.coded_blocks
                                        + block]
                                })
                                .sum::<f64>();
                            expected
                                [frequency * transform.original_blocks + block * factor + time] =
                                scale * value;
                        }
                    }
                }
            } else {
                // Derive the sequency permutation by sorting Sylvester rows
                // by sign changes, independent of the Gray-code implementation.
                let mut rows: Vec<_> = (0..factor).collect();
                rows.sort_by_key(|&row| {
                    (0..factor - 1)
                        .filter(|&i| (row & i).count_ones() & 1 != (row & (i + 1)).count_ones() & 1)
                        .count()
                });
                for frequency in 0..actual.len() / transform.coded_blocks {
                    for block in 0..transform.original_blocks {
                        for bin in 0..factor {
                            let value = rows
                                .iter()
                                .enumerate()
                                .map(|(time, &row)| {
                                    let sign = if (row & bin).count_ones() & 1 == 0 {
                                        1.0
                                    } else {
                                        -1.0
                                    };
                                    sign * input
                                        [frequency * transform.coded_blocks + block * factor + time]
                                })
                                .sum::<f64>();
                            expected
                                [(frequency * factor + bin) * transform.original_blocks + block] =
                                scale * value;
                        }
                    }
                }
            }
            for (&actual, &expected) in actual.iter().zip(&expected) {
                assert!((actual - expected).abs() < 1e-12);
            }
            let before = input.iter().map(|v| v * v).sum::<f64>();
            let after = actual.iter().map(|v| v * v).sum::<f64>();
            assert!((before - after).abs() < 1e-9);
        }
    }

    #[test]
    fn inverse_tf_rejects_invalid_shapes_without_modifying_output() {
        let layout = Layout::from_configuration(31).unwrap().unwrap();
        assert!(BandTransform::new(layout, 0, false, 1).is_err());
        assert!(BandTransform::new(layout, 0, true, -1).is_err());
        assert!(BandTransform::new(layout, 0, true, i8::MIN).is_err());
        let transform = BandTransform::new(layout, 0, true, 1).unwrap();
        let mut wrong_size = [1.0; 7];
        assert!(transform.inverse(&mut wrong_size).is_err());
        assert_eq!(wrong_size, [1.0; 7]);
        let mut overflow = [f64::MAX; 8];
        assert!(transform.inverse(&mut overflow).is_err());
        assert_eq!(overflow, [f64::MAX; 8]);
    }

    #[test]
    fn recovered_tables_have_all_normative_adjustments() {
        for lm in 0..=3 {
            assert_eq!(
                TfResolution::adjustment(lm, false, false, false).unwrap(),
                0
            );
            assert_eq!(TfResolution::adjustment(lm, false, true, false).unwrap(), 0);
            assert_eq!(
                TfResolution::adjustment(lm, false, false, true).unwrap(),
                [-1, -1, -2, -2][lm as usize]
            );
            assert_eq!(
                TfResolution::adjustment(lm, false, true, true).unwrap(),
                [-1, -2, -3, -3][lm as usize]
            );
            assert_eq!(
                TfResolution::adjustment(lm, true, false, false).unwrap(),
                [0, 1, 2, 3][lm as usize]
            );
            assert_eq!(
                TfResolution::adjustment(lm, true, true, false).unwrap(),
                [0, 1, 1, 1][lm as usize]
            );
            assert_eq!(
                TfResolution::adjustment(lm, true, false, true).unwrap(),
                [-1, 0, 0, 0][lm as usize]
            );
            assert_eq!(TfResolution::adjustment(lm, true, true, true).unwrap(), -1);
        }
    }

    #[test]
    fn tf_flags_follow_relative_choice_and_conditional_select() {
        for config in 16..=31 {
            let layout = Layout::from_configuration(config).unwrap().unwrap();
            for transient in [false, true] {
                if layout.blocks(transient).is_err() {
                    continue;
                }
                for seed in 0..=255u32 {
                    let bytes: Vec<_> = (0..128).map(|i| (seed * 73 + i * 137) as u8).collect();
                    let mut actual = RangeDecoder::new(&bytes);
                    let mut expected = actual.clone();
                    let decoded = TfResolution::decode(&mut actual, layout, transient).unwrap();
                    let mut choice = false;
                    let mut matters = false;
                    let lm = (layout.samples() / 120).trailing_zeros() as u8;
                    for (index, &decoded_choice) in decoded.choices.iter().enumerate() {
                        let logp = if index == 0 {
                            if transient { 2 } else { 4 }
                        } else {
                            if transient { 4 } else { 5 }
                        };
                        choice ^= expected.symbol(&[((1u32 << logp) - 1) as u16, 1]).unwrap() != 0;
                        assert_eq!(decoded_choice, choice);
                        matters |= TfResolution::adjustment(lm, transient, false, choice).unwrap()
                            != TfResolution::adjustment(lm, transient, true, choice).unwrap();
                    }
                    let selected = matters && expected.symbol(&[1, 1]).unwrap() != 0;
                    assert_eq!(decoded.selected, selected);
                    assert_eq!(actual.tell_fractional(), expected.tell_fractional());
                }
            }
        }
        let mut truncated = RangeDecoder::new(&[0]);
        assert!(matches!(
            TfResolution::decode(
                &mut truncated,
                Layout::from_configuration(31).unwrap().unwrap(),
                false
            ),
            Err(MediaDecodeError::Unsupported)
        ));
        assert_eq!(truncated.tell(), 1);
    }
}