Files
audio/audio/vorbis/setup.sakura
T
2026-11-11 11:49:04 -06:00

253 lines
8.4 KiB
Plaintext

#! Contains a Vorbis stream's three headers read: the identification header's
#! channels, rate, and block sizes; the comment header, checked and passed over;
#! and the setup header's codebooks, floors, residues, mappings, and modes, each
#! checked against what it names, so decoding a packet never reaches past them.
#!
#! Floor 0, which no encoder has written since before Vorbis 1.0, is
#! `unsupported`.
module malunal::audio::vorbis;
# The most X positions a floor of type 1 has: two, and 31 partitions of a class
# of eight.
const most_posts: u64 = 250;
# A floor of type 1: the X positions of its curve's posts, and the books their
# Y values are read with.
struct floor_setup {
var partitions: u32;
var classes: u8[31]; # Each partition's class.
var dimensions: u8[16]; # How many posts each class reads.
var subclasses: u8[16]; # How many bits of its master book's entry choose each post's book.
var masterbooks: u8[16];
var subbooks: i16[128]; # A class's books, `class * 8 + choice`, or -1 for a post read as 0.
var multiplier: u32;
var posts: u32;
var xs: u32[250];
var order: u8[250]; # The posts in order of their X.
var low: u8[250]; # Each post's neighbor before it, of those read before it, nearest by X.
var high: u8[250]; # And after it.
}
# A mapping: the channels coupled, and the floor and residue of each submap.
struct mapping_setup {
var submaps: u32;
var steps: u32;
var magnitudes: u8[256];
var angles: u8[256];
var mux: u8[256]; # Each channel's submap.
var floors: u8[16];
var residues: u8[16];
}
struct mode_setup {
var long: bool;
var mapping: u32;
}
# What a stream's headers say.
struct setup {
var channels: u32;
var rate: u32;
var sizes: u32[2]; # The short and long block sizes.
var books: vector<codebook>;
var floors: vector<floor_setup>;
var residues: vector<residue_setup>;
var mappings: vector<mapping_setup>;
var modes: vector<mode_setup>;
}
def setup::new(): setup {
return setup{
books = vector::<codebook>::new(), floors = vector::<floor_setup>::new(),
residues = vector::<residue_setup>::new(), mappings = vector::<mapping_setup>::new(),
modes = vector::<mode_setup>::new(),
};
}
# Whether a packet is a header of a type: its type byte, then `vorbis`.
def is_header(data: span<u8>, kind: u8): bool {
return data.length >= 7 && data[0] == kind && data[1] == 'v' && data[2] == 'o' && data[3] == 'r'
&& data[4] == 'b' && data[5] == 'i' && data[6] == 's';
}
def setup::read_identification(&mut this, data: span<u8>): result<void, audio_error> {
return audio_error::unrecognized if !is_header(data, 1);
return audio_error::malformed if data.length < 30;
return audio_error::unsupported if u32_at(data, 7) != 0;
this.channels = data[11] as u32;
this.rate = u32_at(data, 12);
val one: u32 = 1;
this.sizes[0] = one << (data[28] & 15) as u32;
this.sizes[1] = one << (data[28] >> 4) as u32;
return audio_error::malformed if this.channels == 0 || this.rate == 0 || (data[29] & 1) == 0;
return audio_error::malformed if this.sizes[0] < 64 || this.sizes[1] > 8192 || this.sizes[0] > this.sizes[1];
return audio_error::unsupported if this.channels > 8;
return;
}
def setup::read_setup(&mut this, data: span<u8>): result<void, audio_error> {
return audio_error::malformed if !is_header(data, 5);
var b := bits::of(data[7:data.length - 7]);
val books := b.read(8) + 1;
for _ in 0..books {
var book := read_codebook(&mut b) else (e) { return e; };
this.books.append(move(book));
}
val transforms := b.read(6) + 1;
for _ in 0..transforms {
return audio_error::malformed if b.read(16) != 0;
}
val floors := b.read(6) + 1;
for _ in 0..floors {
val kind := b.read(16);
return audio_error::unsupported if kind == 0;
return audio_error::malformed if kind != 1;
val floor := this.read_floor(&mut b) else (e) { return e; };
this.floors.append(floor);
}
val residues := b.read(6) + 1;
for _ in 0..residues {
val residue := this.read_residue(&mut b) else (e) { return e; };
this.residues.append(residue);
}
val mappings := b.read(6) + 1;
for _ in 0..mappings {
val mapping := this.read_mapping(&mut b) else (e) { return e; };
this.mappings.append(mapping);
}
val modes := b.read(6) + 1;
for _ in 0..modes {
var mode := mode_setup{ long = b.read(1) != 0 };
return audio_error::malformed if b.read(16) != 0 || b.read(16) != 0;
mode.mapping = b.read(8);
return audio_error::malformed if mode.mapping >= this.mappings.length as u32;
this.modes.append(mode);
}
return audio_error::malformed if b.read(1) != 1 || b.past;
return;
}
def setup::read_floor(&mut this, b: &mut bits): result<floor_setup, audio_error> {
var floor: floor_setup;
floor.partitions = b.read(5);
var classes: u32 = 0;
for p in 0..floor.partitions {
val class := b.read(4);
floor.classes[p as u64] = class as u8;
classes = class + 1 > classes ? class + 1 : classes;
}
for c in 0..classes {
val at := c as u64;
floor.dimensions[at] = (b.read(3) + 1) as u8;
floor.subclasses[at] = b.read(2) as u8;
if floor.subclasses[at] != 0 {
floor.masterbooks[at] = b.read(8) as u8;
return audio_error::malformed if floor.masterbooks[at] as u64 >= this.books.length;
}
val one: u32 = 1;
for j in 0..(one << floor.subclasses[at] as u32) {
val book := b.read(8) as i32 - 1;
return audio_error::malformed if book >= this.books.length as i32;
floor.subbooks[at * 8 + j as u64] = book as i16;
}
}
floor.multiplier = b.read(2) + 1;
val range := b.read(4);
val one: u32 = 1;
floor.xs[0] = 0;
floor.xs[1] = one << range;
floor.posts = 2;
for p in 0..floor.partitions {
val class := floor.classes[p as u64] as u64;
for _ in 0..floor.dimensions[class] {
return audio_error::malformed if floor.posts as u64 >= most_posts;
floor.xs[floor.posts as u64] = b.read(range);
floor.posts++;
}
}
return audio_error::malformed if b.past;
# The posts in order of X, which must differ, and each one's neighbors.
val count := floor.posts as u64;
for i in 0..count {
floor.order[i] = i as u8;
}
for i in 1..count {
var j := i;
while j > 0 && floor.xs[floor.order[j - 1] as u64] > floor.xs[floor.order[j] as u64] {
val held := floor.order[j];
floor.order[j] = floor.order[j - 1];
floor.order[j - 1] = held;
j--;
}
}
for i in 1..count {
return audio_error::malformed if floor.xs[floor.order[i] as u64] == floor.xs[floor.order[i - 1] as u64];
}
for i in 2..count {
var low: u64 = 0;
var high: u64 = 1;
for j in 0..i {
val x := floor.xs[j];
if x < floor.xs[i] && x > floor.xs[low] {
low = j;
}
if x > floor.xs[i] && x < floor.xs[high] {
high = j;
}
}
floor.low[i] = low as u8;
floor.high[i] = high as u8;
}
return floor;
}
def setup::read_mapping(&mut this, b: &mut bits): result<mapping_setup, audio_error> {
return audio_error::malformed if b.read(16) != 0;
var mapping: mapping_setup;
mapping.submaps = b.read(1) != 0 ? b.read(4) + 1 : 1;
if b.read(1) != 0 {
mapping.steps = b.read(8) + 1;
val width := ilog(this.channels - 1);
for s in 0..mapping.steps {
val magnitude := b.read(width);
val angle := b.read(width);
return audio_error::malformed if magnitude == angle || magnitude >= this.channels || angle >= this.channels;
mapping.magnitudes[s as u64] = magnitude as u8;
mapping.angles[s as u64] = angle as u8;
}
}
return audio_error::malformed if b.read(2) != 0;
if mapping.submaps > 1 {
for c in 0..this.channels {
val submap := b.read(4);
return audio_error::malformed if submap >= mapping.submaps;
mapping.mux[c as u64] = submap as u8;
}
}
for s in 0..mapping.submaps {
_ = b.read(8);
val floor := b.read(8);
val residue := b.read(8);
return audio_error::malformed if floor as u64 >= this.floors.length || residue as u64 >= this.residues.length;
mapping.floors[s as u64] = floor as u8;
mapping.residues[s as u64] = residue as u8;
}
return audio_error::malformed if b.past;
return mapping;
}
# A little-endian u32 at a place.
def u32_at(bytes: span<u8>, at: u64): u32 {
return (bytes[at] as u32) | ((bytes[at + 1] as u32) << 8) | ((bytes[at + 2] as u32) << 16) | ((bytes[at + 3] as u32) << 24);
}