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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//! Vorbis I setup header and audio mode/window selection, from sections 4, 6-8.

use super::entropy::{Codebook, PacketBits};
use crate::video::backend::MediaDecodeError;

fn invalid(message: &str) -> MediaDecodeError {
    MediaDecodeError::InvalidData(message.into())
}

fn field(bits: &mut PacketBits<'_>, width: u8) -> Result<usize, MediaDecodeError> {
    bits.read(width)
        .map(|value| value as usize)
        .ok_or_else(|| invalid("truncated Vorbis setup or packet header"))
}

fn reference(bits: &mut PacketBits<'_>, count: usize) -> Result<usize, MediaDecodeError> {
    let index = field(bits, 8)?;
    if index >= count {
        return Err(invalid("invalid Vorbis setup reference"));
    }
    Ok(index)
}

pub(super) fn read_codebooks(
    bits: &mut PacketBits<'_>,
    entry_budget: &mut usize,
    lookup_budget: &mut usize,
) -> Result<Vec<Codebook>, MediaDecodeError> {
    let count = field(bits, 8)? + 1;
    let mut books = Vec::with_capacity(count);
    for _ in 0..count {
        books.push(Codebook::parse(bits, entry_budget, lookup_budget)?);
    }
    Ok(books)
}

#[derive(Clone, Debug)]
pub struct FloorZero {
    pub order: usize,
    pub rate: usize,
    pub bark_map_size: usize,
    pub amplitude_bits: u8,
    pub amplitude_offset: usize,
    pub books: Vec<usize>,
}

#[derive(Clone, Debug)]
pub struct FloorClass {
    pub dimensions: usize,
    pub subclasses: u8,
    pub masterbook: Option<usize>,
    pub books: Vec<Option<usize>>,
}

#[derive(Clone, Debug)]
pub struct FloorOne {
    pub partitions: Vec<usize>,
    pub classes: Vec<FloorClass>,
    pub multiplier: usize,
    pub x: Vec<usize>,
    pub neighbors: Vec<(usize, usize)>,
    pub sorted_points: Vec<usize>,
}

#[derive(Clone, Debug)]
pub enum Floor {
    Zero(FloorZero),
    One(FloorOne),
}

#[derive(Clone, Debug)]
pub struct Residue {
    pub kind: u8,
    pub begin: usize,
    pub end: usize,
    pub partition_size: usize,
    pub classbook: usize,
    pub books: Vec<[Option<usize>; 8]>,
}

#[derive(Clone, Debug)]
pub struct Submap {
    pub floor: usize,
    pub residue: usize,
}

#[derive(Clone, Debug)]
pub struct Mapping {
    pub coupling: Vec<(usize, usize)>,
    pub mux: Vec<usize>,
    pub submaps: Vec<Submap>,
}

#[derive(Clone, Debug)]
pub struct Mode {
    pub long: bool,
    pub mapping: usize,
}

#[derive(Clone, Debug)]
pub struct Setup {
    pub codebooks: Vec<Codebook>,
    pub floors: Vec<Floor>,
    pub residues: Vec<Residue>,
    pub mappings: Vec<Mapping>,
    pub modes: Vec<Mode>,
}

impl Floor {
    fn parse(bits: &mut PacketBits<'_>, books: &[Codebook]) -> Result<Self, MediaDecodeError> {
        match field(bits, 16)? {
            0 => {
                let order = field(bits, 8)?;
                let rate = field(bits, 16)?;
                let bark_map_size = field(bits, 16)?;
                let amplitude_bits = field(bits, 6)? as u8;
                let amplitude_offset = field(bits, 8)?;
                let count = field(bits, 4)? + 1;
                let mut floor_books = Vec::with_capacity(count);
                for _ in 0..count {
                    let book = reference(bits, books.len())?;
                    if !books[book].has_lookup() {
                        return Err(invalid("Vorbis floor zero requires vector books"));
                    }
                    floor_books.push(book);
                }
                if order == 0 || rate == 0 || bark_map_size == 0 {
                    return Err(invalid("invalid Vorbis floor zero parameters"));
                }
                Ok(Self::Zero(FloorZero {
                    order,
                    rate,
                    bark_map_size,
                    amplitude_bits,
                    amplitude_offset,
                    books: floor_books,
                }))
            }
            1 => {
                let count = field(bits, 5)?;
                let mut partitions = Vec::with_capacity(count);
                for _ in 0..count {
                    partitions.push(field(bits, 4)?);
                }
                let class_count = partitions.iter().max().map_or(0, |&maximum| maximum + 1);
                let mut classes = Vec::with_capacity(class_count);
                for _ in 0..class_count {
                    let dimensions = field(bits, 3)? + 1;
                    let subclasses = field(bits, 2)? as u8;
                    let masterbook = if subclasses != 0 {
                        Some(reference(bits, books.len())?)
                    } else {
                        None
                    };
                    let mut subclass_books = Vec::with_capacity(1 << subclasses);
                    for _ in 0..1 << subclasses {
                        let encoded = field(bits, 8)?;
                        if encoded > books.len() {
                            return Err(invalid("invalid Vorbis floor subclass book"));
                        }
                        subclass_books.push(encoded.checked_sub(1));
                    }
                    classes.push(FloorClass {
                        dimensions,
                        subclasses,
                        masterbook,
                        books: subclass_books,
                    });
                }
                let multiplier = field(bits, 2)? + 1;
                let range_bits = field(bits, 4)? as u8;
                let mut x = vec![0, 1 << range_bits];
                for &class in &partitions {
                    if x.len() + classes[class].dimensions > 65 {
                        return Err(invalid("Vorbis floor one has more than 65 points"));
                    }
                    for _ in 0..classes[class].dimensions {
                        let value = field(bits, range_bits)?;
                        if x.contains(&value) {
                            return Err(invalid("duplicate Vorbis floor point"));
                        }
                        x.push(value);
                    }
                }
                let mut neighbors = vec![(0, 0); 2];
                for index in 2..x.len() {
                    let low = (0..index)
                        .filter(|&prior| x[prior] < x[index])
                        .max_by_key(|&prior| x[prior])
                        .unwrap();
                    let high = (0..index)
                        .filter(|&prior| x[prior] > x[index])
                        .min_by_key(|&prior| x[prior])
                        .unwrap();
                    neighbors.push((low, high));
                }
                let mut sorted_points: Vec<_> = (0..x.len()).collect();
                sorted_points.sort_unstable_by_key(|&index| x[index]);
                Ok(Self::One(FloorOne {
                    partitions,
                    classes,
                    multiplier,
                    x,
                    neighbors,
                    sorted_points,
                }))
            }
            _ => Err(invalid("reserved Vorbis floor type")),
        }
    }
}

impl Residue {
    fn parse(bits: &mut PacketBits<'_>, codebooks: &[Codebook]) -> Result<Self, MediaDecodeError> {
        let kind = field(bits, 16)?;
        if kind > 2 {
            return Err(invalid("reserved Vorbis residue type"));
        }
        let begin = field(bits, 24)?;
        let end = field(bits, 24)?;
        let partition_size = field(bits, 24)? + 1;
        let classifications = field(bits, 6)? + 1;
        let classbook = reference(bits, codebooks.len())?;
        let mut combinations = 1usize;
        for _ in 0..codebooks[classbook].dimensions {
            combinations = combinations
                .checked_mul(classifications)
                .filter(|&count| count <= codebooks[classbook].entries)
                .ok_or_else(|| invalid("Vorbis residue classification book is too small"))?;
        }
        if begin > end {
            return Err(invalid("reversed Vorbis residue range"));
        }
        let mut cascades = Vec::with_capacity(classifications);
        for _ in 0..classifications {
            let low = field(bits, 3)?;
            let high = if field(bits, 1)? != 0 {
                field(bits, 5)?
            } else {
                0
            };
            cascades.push(low | (high << 3));
        }
        let mut books = Vec::with_capacity(classifications);
        for cascade in cascades {
            let mut passes = [None; 8];
            for (pass, book) in passes.iter_mut().enumerate() {
                if cascade & (1 << pass) != 0 {
                    let index = reference(bits, codebooks.len())?;
                    if !codebooks[index].has_lookup() {
                        return Err(invalid("Vorbis residue requires vector books"));
                    }
                    *book = Some(index);
                }
            }
            books.push(passes);
        }
        Ok(Self {
            kind: kind as u8,
            begin,
            end,
            partition_size,
            classbook,
            books,
        })
    }
}

impl Mapping {
    fn parse(
        bits: &mut PacketBits<'_>,
        channels: usize,
        floors: usize,
        residues: usize,
    ) -> Result<Self, MediaDecodeError> {
        if field(bits, 16)? != 0 {
            return Err(invalid("reserved Vorbis mapping type"));
        }
        let count = if field(bits, 1)? != 0 {
            field(bits, 4)? + 1
        } else {
            1
        };
        let mut coupling = Vec::new();
        if field(bits, 1)? != 0 {
            let steps = field(bits, 8)? + 1;
            let width = (usize::BITS - (channels - 1).leading_zeros()) as u8;
            for _ in 0..steps {
                let magnitude = field(bits, width)?;
                let angle = field(bits, width)?;
                if magnitude == angle || magnitude >= channels || angle >= channels {
                    return Err(invalid("invalid Vorbis coupled channels"));
                }
                coupling.push((magnitude, angle));
            }
        }
        if field(bits, 2)? != 0 {
            return Err(invalid("nonzero Vorbis mapping reserved bits"));
        }
        let mut mux = vec![0; channels];
        if count > 1 {
            for channel in &mut mux {
                *channel = field(bits, 4)?;
                if *channel >= count {
                    return Err(invalid("invalid Vorbis channel mux"));
                }
            }
        }
        let mut submaps = Vec::with_capacity(count);
        for _ in 0..count {
            field(bits, 8)?;
            submaps.push(Submap {
                floor: reference(bits, floors)?,
                residue: reference(bits, residues)?,
            });
        }
        Ok(Self {
            coupling,
            mux,
            submaps,
        })
    }
}

impl Setup {
    pub fn parse(
        bytes: &[u8],
        channels: u8,
        mut entry_budget: usize,
        mut lookup_budget: usize,
    ) -> Result<Self, MediaDecodeError> {
        if channels == 0 || !bytes.starts_with(b"\x05vorbis") {
            return Err(invalid("invalid Vorbis setup signature or channels"));
        }
        let mut bits = PacketBits::new(&bytes[7..]);
        let codebooks = read_codebooks(&mut bits, &mut entry_budget, &mut lookup_budget)?;
        let time_count = field(&mut bits, 6)? + 1;
        for _ in 0..time_count {
            if field(&mut bits, 16)? != 0 {
                return Err(invalid("nonzero Vorbis time transform"));
            }
        }
        let floor_count = field(&mut bits, 6)? + 1;
        let mut floors = Vec::with_capacity(floor_count);
        for _ in 0..floor_count {
            floors.push(Floor::parse(&mut bits, &codebooks)?);
        }
        let residue_count = field(&mut bits, 6)? + 1;
        let mut residues = Vec::with_capacity(residue_count);
        for _ in 0..residue_count {
            residues.push(Residue::parse(&mut bits, &codebooks)?);
        }
        let mapping_count = field(&mut bits, 6)? + 1;
        let mut mappings = Vec::with_capacity(mapping_count);
        for _ in 0..mapping_count {
            mappings.push(Mapping::parse(
                &mut bits,
                usize::from(channels),
                floors.len(),
                residues.len(),
            )?);
        }
        let mode_count = field(&mut bits, 6)? + 1;
        let mut modes = Vec::with_capacity(mode_count);
        for _ in 0..mode_count {
            let long = field(&mut bits, 1)? != 0;
            if field(&mut bits, 16)? != 0 || field(&mut bits, 16)? != 0 {
                return Err(invalid("reserved Vorbis mode window or transform"));
            }
            modes.push(Mode {
                long,
                mapping: reference(&mut bits, mappings.len())?,
            });
        }
        if field(&mut bits, 1)? != 1 {
            return Err(invalid("missing Vorbis setup framing bit"));
        }
        Ok(Self {
            codebooks,
            floors,
            residues,
            mappings,
            modes,
        })
    }

    /// Returns None for non-audio packets, which must be ignored.
    pub fn packet_header(
        &self,
        bits: &mut PacketBits<'_>,
    ) -> Result<Option<PacketHeader>, MediaDecodeError> {
        if field(bits, 1)? != 0 {
            return Ok(None);
        }
        if self.modes.is_empty() {
            return Err(invalid("Vorbis setup has no modes"));
        }
        let width = (usize::BITS - (self.modes.len() - 1).leading_zeros()) as u8;
        let index = field(bits, width)?;
        let mode = self
            .modes
            .get(index)
            .ok_or_else(|| invalid("invalid Vorbis audio mode"))?;
        let (previous_long, next_long) = if mode.long {
            (field(bits, 1)? != 0, field(bits, 1)? != 0)
        } else {
            (false, false)
        };
        Ok(Some(PacketHeader {
            mode: index,
            long: mode.long,
            previous_long,
            next_long,
        }))
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PacketHeader {
    pub mode: usize,
    pub long: bool,
    pub previous_long: bool,
    pub next_long: bool,
}

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

    fn packet(fields: &[(u32, u8)]) -> Vec<u8> {
        let mut bytes = vec![
            0;
            fields
                .iter()
                .map(|&(_, width)| usize::from(width))
                .sum::<usize>()
                .div_ceil(8)
        ];
        let mut position = 0;
        for &(value, width) in fields {
            for bit in 0..width {
                bytes[position / 8] |= ((value >> bit) as u8 & 1) << (position & 7);
                position += 1;
            }
        }
        bytes
    }

    fn minimal_setup() -> Vec<(u32, u8)> {
        vec![
            (0, 8), // One scalar codebook, one length-one entry.
            (0x564342, 24),
            (1, 16),
            (1, 24),
            (0, 1),
            (0, 1),
            (0, 5),
            (0, 4),
            (0, 6),
            (0, 16), // Time transform.
            (0, 6),
            (1, 16),
            (0, 5),
            (0, 2),
            (6, 4), // Floor one, no partitions.
            (0, 6),
            (0, 16),
            (0, 24),
            (32, 24),
            (7, 24),
            (0, 6),
            (0, 8),
            (0, 3),
            (0, 1),
            (0, 6),
            (0, 16),
            (0, 1),
            (0, 1),
            (0, 2),
            (0, 8),
            (0, 8),
            (0, 8),
            (1, 6), // Two modes, short then long.
            (0, 1),
            (0, 16),
            (0, 16),
            (0, 8),
            (1, 1),
            (0, 16),
            (0, 16),
            (0, 8),
            (1, 1),
        ]
    }

    fn header(fields: &[(u32, u8)]) -> Vec<u8> {
        let mut bytes = b"\x05vorbis".to_vec();
        bytes.extend(packet(fields));
        bytes
    }

    #[test]
    fn complete_setup_and_audio_window_flags() {
        let bytes = header(&minimal_setup());
        let setup = Setup::parse(&bytes, 2, 1, 0).unwrap();
        assert_eq!(setup.codebooks.len(), 1);
        assert_eq!(setup.floors.len(), 1);
        assert_eq!(setup.residues[0].partition_size, 8);
        assert_eq!(setup.mappings[0].mux, [0, 0]);
        for previous in [false, true] {
            for next in [false, true] {
                let bytes = packet(&[
                    (0, 1),
                    (1, 1),
                    (u32::from(previous), 1),
                    (u32::from(next), 1),
                ]);
                let header = setup
                    .packet_header(&mut PacketBits::new(&bytes))
                    .unwrap()
                    .unwrap();
                assert_eq!(
                    header,
                    PacketHeader {
                        mode: 1,
                        long: true,
                        previous_long: previous,
                        next_long: next
                    }
                );
            }
        }
        assert!(
            setup
                .packet_header(&mut PacketBits::new(&[1]))
                .unwrap()
                .is_none()
        );
        assert!(setup.packet_header(&mut PacketBits::new(&[])).is_err());
        for length in 0..bytes.len() {
            assert!(Setup::parse(&bytes[..length], 2, 1, 0).is_err());
        }
        assert!(Setup::parse(&bytes, 0, 1, 0).is_err());
        assert!(Setup::parse(&bytes, 2, 0, 0).is_err());
    }

    #[test]
    fn reserved_fields_references_and_framing_are_rejected() {
        let fields = minimal_setup();
        for (index, value) in [
            (9, 1),
            (11, 2),
            (16, 3),
            (21, 1),
            (25, 1),
            (28, 1),
            (30, 1),
            (31, 1),
            (34, 1),
            (35, 1),
            (36, 1),
            (38, 1),
            (39, 1),
            (40, 1),
            (41, 0),
        ] {
            let mut changed = fields.clone();
            changed[index].0 = value;
            assert!(
                Setup::parse(&header(&changed), 2, 1, 0).is_err(),
                "field {index}"
            );
        }
    }

    #[test]
    fn floor_points_and_coupling_channels_are_checked() {
        let book = Codebook::parse(
            &mut PacketBits::new(&packet(&[
                (0x564342, 24),
                (1, 16),
                (1, 24),
                (0, 1),
                (0, 1),
                (0, 5),
                (0, 4),
            ])),
            &mut 1,
            &mut 0,
        )
        .unwrap();
        let books = [book];
        let prefix = [
            (1, 16),
            (1, 5),
            (0, 4),
            (0, 3),
            (0, 2),
            (0, 8),
            (0, 2),
            (6, 4),
        ];
        for point in [0, 32] {
            let mut fields = prefix.to_vec();
            fields.push((point, 6));
            let bytes = packet(&fields);
            assert_eq!(
                Floor::parse(&mut PacketBits::new(&bytes), &books).is_ok(),
                point == 32
            );
        }
        for angle in [0, 1, 3] {
            let bytes = packet(&[
                (0, 16),
                (0, 1),
                (1, 1),
                (0, 8),
                (0, 2),
                (angle, 2),
                (0, 2),
                (0, 8),
                (0, 8),
                (0, 8),
            ]);
            assert_eq!(
                Mapping::parse(&mut PacketBits::new(&bytes), 3, 1, 1).is_ok(),
                angle == 1
            );
        }
    }
}