Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-30 16:12:42 -05:00
commit f8f6ef453d
36 files changed
+4431

No files matched your search

+196
View File
@@ -0,0 +1,196 @@
#! Contains Ogg Vorbis streams made by a small encoder of the tests' own, and the
#! frames each should decode to, as `spec.sakura` works them out.
#!
#! A stream's setup has a book of two classes and a book of 16 vectors of two
#! dimensions, a floor of type 1 whose posts are both 255, a curve of 1, so each
#! spectrum is its residue, partitions of 8 values, each silent or coded, and
#! blocks of 64 and 256 samples. Two channels are coupled, and coded by a
#! residue of type 2; one is coded by a residue of type 1.
import std::math;
import malunal::audio;
# What a stream made by the tests holds.
struct made_stream {
var bytes: vector<u8>;
var expected: vector<f32>; # The frames it decodes to, a frame's channels together.
var channels: u32;
}
# The blocks a stream is made of: long or short, in order.
const pattern: bool[9] = [true, true, false, false, true, false, true, true, false];
# A coded partition's entries, and whether a partition is coded.
def entry_at(packet: u64, partition: u64, j: u64): u32 {
return ((packet * 7 + partition * 5 + j * 3) % 16) as u32;
}
def coded(packet: u64, partition: u64): bool {
return (packet + partition) % 3 != 0;
}
# The values a vector entry stands for: a grid of four from -1.5, twice over.
def entry_value(entry: u32, dimension: u32): f32 {
val index := dimension == 0 ? entry % 4 : (entry / 4) % 4;
return -1.5 + index as f32;
}
# Makes a stream of one or two channels, `trimmed` frames cut from its end by
# its last page's granule position.
# @param floor_zero Whether its floor says it's of type 0, which isn't read.
def made_vorbis(channels: u32, trimmed: u64, floor_zero: bool = false): made_stream {
val sizes: u64[2] = [64, 256];
val count := 9u;
# The headers.
var id := bit_writer::new();
id.put(1, 8);
id.text("vorbis");
id.put(0, 32);
id.put(channels, 8);
id.put(48000, 32);
id.put(0, 32);
id.put(0, 32);
id.put(0, 32);
id.put(6 | (8 << 4), 8);
id.put(1, 8);
var comment := bit_writer::new();
comment.put(3, 8);
comment.text("vorbis");
comment.put(0, 32);
comment.put(0, 32);
comment.put(1, 8);
var setup := bit_writer::new();
setup.put(5, 8);
setup.text("vorbis");
setup.put(1, 8); # two books
setup.put(0x564342, 24); # book 0: the classes
setup.put(1, 16);
setup.put(2, 24);
setup.put(0, 2);
setup.put(0, 5);
setup.put(0, 5);
setup.put(0, 4);
setup.put(0x564342, 24); # book 1: the vectors
setup.put(2, 16);
setup.put(16, 24);
setup.put(0, 2);
for _ in 0..16 {
setup.put(3, 5);
}
setup.put(1, 4);
setup.put(0x80000000 | (787 << 21) | 3, 32); # minimum -1.5
setup.put((788 << 21) | 1, 32); # delta 1
setup.put(1, 4);
setup.put(0, 1);
for v in 0u..4u {
setup.put(v as u32, 2);
}
setup.put(0, 6); # one time transform
setup.put(0, 16);
setup.put(0, 6); # one floor
setup.put(floor_zero ? 0 : 1, 16);
setup.put(0, 5);
setup.put(0, 2);
setup.put(7, 4);
setup.put(0, 6); # one residue
setup.put(channels == 2 ? 2 : 1, 16);
setup.put(0, 24);
setup.put(channels * 128, 24);
setup.put(7, 24);
setup.put(1, 6);
setup.put(0, 8);
setup.put(0, 4);
setup.put(1, 4);
setup.put(1, 8);
setup.put(0, 6); # one mapping
setup.put(0, 16);
setup.put(0, 1);
if channels == 2 {
setup.put(1, 1);
setup.put(0, 8);
setup.put(0, 1);
setup.put(1, 1);
} else {
setup.put(0, 1);
}
setup.put(0, 2);
setup.put(0, 24);
setup.put(1, 6); # two modes
for long in 0u..2u {
setup.put(long as u32, 1);
setup.put(0, 32);
setup.put(0, 8);
}
setup.put(1, 1);
# The packets, and the residue each codes.
var packets := vector::<vector<u8>>::new();
var residues := vector::<vector<f32>>::new();
for k in 0..count {
val long := pattern[k];
val half := sizes[long ? 1 : 0] / 2;
var w := bit_writer::new();
w.put(0, 1);
w.put(long ? 1 : 0, 1);
if long {
w.put(k == 0 || pattern[k - 1] ? 1 : 0, 1);
w.put(k + 1 == count || pattern[k + 1] ? 1 : 0, 1);
}
for _ in 0..channels {
w.put(1, 1);
w.put(255, 8);
w.put(255, 8);
}
val size := half * channels as u64;
var residue := vector::<f32>::new();
residue.resize(size);
for p in 0..size / 8 {
val on := coded(k, p);
w.code(on ? 1 : 0, 1);
continue if !on;
for j in 0u..4u {
val e := entry_at(k, p, j);
w.code(e, 4);
residue[p * 8 + j * 2] = entry_value(e, 0);
residue[p * 8 + j * 2 + 1] = entry_value(e, 1);
}
}
packets.append(w.finish());
residues.append(move(residue));
}
var expected := defined_frames(channels, trimmed, &residues);
# The pages: the identification alone, the comment and setup, then the audio
# three packets to a page, each page's granule position the frames decoded
# once its last packet is.
var bytes := vector::<u8>::new();
var sequence: u32 = 0;
val ident := id.finish();
page_of(&mut bytes, 2, 0, sequence, ident.view(), span::<u8>::empty());
sequence++;
val comments := comment.finish();
val setups := setup.finish();
page_of(&mut bytes, 0, 0, sequence, comments.view(), setups.view());
sequence++;
var decoded: u64 = 0;
var k: u64 = 0;
while k < count {
val end := math::min(k + 3, count);
var body := vector::<u8>::new();
var lacing := vector::<u8>::new();
for j in k..end {
if j > 0 {
decoded += sizes[pattern[j - 1] ? 1 : 0] / 4 + sizes[pattern[j] ? 1 : 0] / 4;
}
lace(&mut lacing, packets[j].length);
body.extend(packets[j].view());
}
val last := end == count;
page_laced(&mut bytes, last ? 4 : 0, last ? decoded - trimmed : decoded, sequence, lacing.view(), body.view());
sequence++;
k = end;
}
return made_stream{ bytes = move(bytes), expected = move(expected), channels = channels };
}