Files
audio/audio/vorbis/codebooks.sakura
T
2026-10-30 16:12:42 -05:00

225 lines
7.3 KiB
Plaintext

#! Contains codebooks: the Huffman codes of a setup's entries, and the vectors
#! some entries stand for, as Vorbis I's third section reads them.
#!
#! Each used entry is given the lowest codeword of its length not yet taken,
#! in order, as the spec says. A code of up to `fast_bits` bits is decoded by one
#! look in a table, and a longer one by walking a tree from its first bit. A
#! book whose lengths would need more codewords than there are is `malformed`;
#! one needing fewer is read, its unused codewords decoding to nothing.
import std::math;
module malunal::audio::vorbis;
# The bits a codebook's table decodes at once.
const fast_bits: u32 = 10;
# What a table's place holds, besides an entry: the first bits of a longer code,
# and of none.
const longer: i32 = -1;
const no_code: i32 = -2;
# A codebook.
struct codebook {
var dimensions: u32;
var entries: u32;
var lengths: vector<u8>; # Each entry's codeword's length, 0 where it's unused.
var fast: vector<i32>; # By the next `fast_bits` bits: the entry, `longer`, or `no_code`.
var tree: vector<i32>; # Pairs, a node's children by the next bit: a node, -(entry + 1), or 0 for none.
var lookup: u32; # 0 where its entries stand for no vectors.
var values: vector<f32>; # Each entry's vector, `dimensions` floats.
}
# Reads a codebook from a setup header.
def read_codebook(b: &mut bits): result<codebook, audio_error> {
return audio_error::malformed if b.read(24) != 0x564342;
var book := codebook{
lengths = vector::<u8>::new(), fast = vector::<i32>::new(), tree = vector::<i32>::new(),
values = vector::<f32>::new(),
};
book.dimensions = b.read(16);
book.entries = b.read(24);
return audio_error::malformed if book.dimensions == 0 || b.past;
val entries := book.entries as u64;
book.lengths.resize(entries);
if b.read(1) == 0 {
val sparse := b.read(1) != 0;
for e in 0..entries {
if !sparse || b.read(1) != 0 {
book.lengths[e] = (b.read(5) + 1) as u8;
}
}
} else {
var current: u32 = 0;
var length := b.read(5) + 1;
while current < book.entries {
val count := b.read(ilog(book.entries - current));
return audio_error::malformed if current + count > book.entries || length > 32 || b.past;
for e in current..current + count {
book.lengths[e as u64] = length as u8;
}
current += count;
length++;
}
}
book.lookup = b.read(4);
if book.lookup == 1 || book.lookup == 2 {
val minimum := b.float();
val delta := b.float();
val width := b.read(4) + 1;
val sequence := b.read(1) != 0;
val dims := book.dimensions as u64;
return audio_error::unsupported if entries * dims > 0x400000;
val count := book.lookup == 1 ? lookup1_values(book.entries, book.dimensions) : entries * dims;
var multiplicands := vector::<u32>::new();
multiplicands.resize(count);
for i in 0..count {
multiplicands[i] = b.read(width);
}
return audio_error::malformed if b.past || count == 0;
book.values.resize(entries * dims);
for e in 0..entries {
var last: f32 = 0;
var divisor: u64 = 1;
for j in 0..dims {
val offset := book.lookup == 1 ? (e / divisor) % count : e * dims + j;
val value := multiplicands[offset] as f32 * delta + minimum + last;
if sequence {
last = value;
}
book.values[e * dims + j] = value;
if book.lookup == 1 {
divisor *= count;
}
}
}
} else if book.lookup != 0 {
return audio_error::malformed;
}
return audio_error::malformed if b.past;
book.assign() else (e) { return e; };
return book;
}
# The number of values a lookup of type 1 holds: the most whose power of the
# dimensions is no more than the entries.
def lookup1_values(entries: u32, dimensions: u32): u64 {
var root := math::floor(math::pow(entries as f64, 1.0 / dimensions as f64)) as u64;
while power_at_most(root + 1, dimensions, entries as u64) {
root++;
}
while root > 0 && !power_at_most(root, dimensions, entries as u64) {
root--;
}
return root;
}
# Whether a number to a power is no more than a limit.
def power_at_most(base: u64, power: u32, limit: u64): bool {
var made: u64 = 1;
for _ in 0..power {
made *= base;
return false if made > limit;
}
return true;
}
# Gives each used entry its codeword, as the spec says, into the table and the
# tree. Codewords are kept as their bits come, first lowest, so the table looks
# them up by the bits as read.
def codebook::assign(&mut this): result<void, audio_error> {
this.fast.resize(1 << fast_bits as u64, no_code);
this.tree.append(0);
this.tree.append(0);
var available: u64[33];
var first := true;
for e in 0..this.entries as u64 {
val length := this.lengths[e] as u32;
continue if length == 0;
if first {
first = false;
this.add(e as i32, 0, length) else (x) { return x; };
for i in 1..length + 1 {
available[i as u64] = (1 as u64) << (32 - i);
}
continue;
}
var level := length;
while level > 0 && available[level as u64] == 0 {
level--;
}
return audio_error::malformed if level == 0;
val code := available[level as u64];
available[level as u64] = 0;
this.add(e as i32, code, length) else (x) { return x; };
var deeper := length;
while deeper > level {
available[deeper as u64] = code + ((1 as u64) << (32 - deeper));
deeper--;
}
}
return;
}
# Adds an entry's codeword, given with its first bit highest of 32.
def codebook::add(&mut this, entry: i32, code: u64, length: u32): result<void, audio_error> {
val reversed := reverse_bits(code as u32);
if length <= fast_bits {
val one: u32 = 1;
for fill in 0..(one << (fast_bits - length)) {
this.fast[(reversed | (fill << length)) as u64] = entry;
}
} else {
val one: u32 = 1;
this.fast[(reversed & ((one << fast_bits) - 1)) as u64] = longer;
}
var node: u64 = 0;
for i in 0..length {
val slot := node * 2 + ((reversed >> i) & 1) as u64;
if i == length - 1 {
return audio_error::malformed if this.tree[slot] != 0;
this.tree[slot] = -(entry + 1);
} else {
return audio_error::malformed if this.tree[slot] < 0;
if this.tree[slot] == 0 {
this.tree[slot] = (this.tree.length / 2) as i32;
this.tree.append(0);
this.tree.append(0);
}
node = this.tree[slot] as u64;
}
}
return;
}
# A 32-bit number's bits the other way around.
def reverse_bits(value: u32): u32 {
var v := value;
v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1);
v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2);
v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4);
v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8);
return (v >> 16) | ((v & 0xFFFF) << 16);
}
# Reads an entry's codeword.
# @returns The entry, or -1 where the packet ended or the bits are no codeword.
def codebook::decode(&this, b: &mut bits): i32 {
val found := this.fast[b.peek(fast_bits) as u64];
if found >= 0 {
return b.skip(this.lengths[found as u64] as u32) ? found : -1;
}
return -1 if found == no_code;
var node: u64 = 0;
while true {
val bit := b.read(1);
return -1 if b.past;
val child := this.tree[node * 2 + bit as u64];
return -(child + 1) if child < 0;
return -1 if child == 0;
node = child as u64;
}
return -1;
}