use core::fmt;
use std::fmt::Write as _;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Phase {
Foundation,
Analysis,
Coding,
Controller,
Quality,
Roof,
}
impl Phase {
#[must_use]
pub fn name(self) -> &'static str {
match self {
Self::Foundation => "Foundation",
Self::Analysis => "Analysis",
Self::Coding => "Coding",
Self::Controller => "Controller",
Self::Quality => "Quality",
Self::Roof => "Roof",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Class {
Gen,
Tbl,
Alg,
Glue,
Done,
}
impl Class {
#[must_use]
pub fn tag(self) -> &'static str {
match self {
Self::Gen => "[GEN]",
Self::Tbl => "[TBL]",
Self::Alg => "[ALG]",
Self::Glue => "[GLUE]",
Self::Done => "[done]",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Verify {
Reuse,
RoundTrip,
Tdac,
Reference,
External,
Metric,
}
impl Verify {
#[must_use]
pub fn name(self) -> &'static str {
match self {
Self::Reuse => "reuse",
Self::RoundTrip => "round-trip",
Self::Tdac => "tdac",
Self::Reference => "reference",
Self::External => "external",
Self::Metric => "metric",
}
}
#[must_use]
pub fn is_conformance(self) -> bool {
!matches!(self, Self::Metric)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Status {
Todo,
Stub,
Impl,
Verified,
}
impl Status {
#[must_use]
pub fn symbol(self) -> &'static str {
match self {
Self::Todo => "·",
Self::Stub => "◐",
Self::Impl => "●",
Self::Verified => "✓",
}
}
#[must_use]
pub fn name(self) -> &'static str {
match self {
Self::Todo => "todo",
Self::Stub => "stub",
Self::Impl => "impl",
Self::Verified => "verified",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Accel {
Scalar,
Hybrid,
Simd,
}
impl Accel {
#[must_use]
pub fn tag(self) -> &'static str {
match self {
Self::Scalar => "safe",
Self::Hybrid => "hybrid",
Self::Simd => "SIMD",
}
}
}
#[must_use]
pub fn accel(id: &str) -> Accel {
match id {
"L1" | "L2" | "Q2" => Accel::Simd,
"C2" | "Q6" | "R1" => Accel::Hybrid,
_ => Accel::Scalar,
}
}
#[derive(Debug, Clone, Copy)]
pub struct Brick {
pub id: &'static str,
pub phase: Phase,
pub class: Class,
pub verify: Verify,
pub status: Status,
pub name: &'static str,
}
impl fmt::Display for Brick {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} {:<3} {:<10} {:<6} {:<10} {:<6} {}",
self.status.symbol(),
self.id,
self.phase.name(),
self.class.tag(),
self.verify.name(),
accel(self.id).tag(),
self.name,
)
}
}
pub static BRICKS: &[Brick] = &[
b(
"N1",
Phase::Foundation,
Class::Done,
Verify::Reuse,
Status::Verified,
"Scalefactor-band offsets (reused from decode tables)",
),
b(
"N2",
Phase::Foundation,
Class::Done,
Verify::Reuse,
Status::Verified,
"scalefac_compress → (slen1,slen2); PRETAB (reused)",
),
b(
"N3",
Phase::Foundation,
Class::Done,
Verify::Reuse,
Status::Verified,
"Huffman codebooks (reused; value→code lookup in B1)",
),
b(
"N4",
Phase::Foundation,
Class::Gen,
Verify::Reference,
Status::Verified,
"Forward quantizer power law x^(3/4) / x^(4/3) (round-trips on the lattice)",
),
b(
"N5",
Phase::Foundation,
Class::Gen,
Verify::Tdac,
Status::Verified,
"Analysis polyphase window C[512] (realised in L1, TDAC-verified)",
),
b(
"N6",
Phase::Foundation,
Class::Gen,
Verify::Tdac,
Status::Verified,
"Analysis cosine matrix M[32][64] (realised in L1, TDAC-verified)",
),
b(
"N7",
Phase::Foundation,
Class::Gen,
Verify::Tdac,
Status::Verified,
"Forward-MDCT cosine basis + 4 window shapes (realised in L2)",
),
b(
"N8",
Phase::Foundation,
Class::Tbl,
Verify::Reuse,
Status::Verified,
"linbits per Huffman table (reused from decode PAIR_TABLES)",
),
b(
"N9",
Phase::Foundation,
Class::Gen,
Verify::RoundTrip,
Status::Verified,
"Region-boundary ↔ (region0/1_count) maps",
),
b(
"L1",
Phase::Analysis,
Class::Alg,
Verify::Tdac,
Status::Verified,
"Analysis filterbank: 18 passes, fold 512→64, cosine matrix",
),
b(
"L2",
Phase::Analysis,
Class::Alg,
Verify::Tdac,
Status::Verified,
"Forward MDCT (36-pt long / 3×12-pt short) + overlap",
),
b(
"L3",
Phase::Analysis,
Class::Alg,
Verify::Tdac,
Status::Verified,
"End-to-end analysis round-trip (PCM→freq→PCM identity)",
),
b(
"B1",
Phase::Coding,
Class::Alg,
Verify::RoundTrip,
Status::Verified,
"Huffman encode-table builder (invert codebooks)",
),
b(
"B2",
Phase::Coding,
Class::Alg,
Verify::RoundTrip,
Status::Verified,
"estimate_bits(region, table) — inner-loop cost oracle",
),
b(
"B3",
Phase::Coding,
Class::Alg,
Verify::RoundTrip,
Status::Verified,
"Huffman spectrum encode (big_values + count1 + linbits)",
),
b(
"B4",
Phase::Coding,
Class::Alg,
Verify::RoundTrip,
Status::Verified,
"Region + table selection (minimise B2 cost)",
),
b(
"B5",
Phase::Coding,
Class::Alg,
Verify::RoundTrip,
Status::Verified,
"Side-info serializer (inverse of decode/sideinfo)",
),
b(
"B6",
Phase::Coding,
Class::Alg,
Verify::RoundTrip,
Status::Verified,
"Scalefactor serializer (band-major, scfsi reuse)",
),
b(
"B7",
Phase::Coding,
Class::Glue,
Verify::RoundTrip,
Status::Verified,
"Frame assembly (reservoir-free; decodes via Mp3Decoder)",
),
b(
"B8",
Phase::Coding,
Class::Alg,
Verify::RoundTrip,
Status::Verified,
"Encoder bit reservoir (stream assembler, borrows + caps at 511)",
),
b(
"C1",
Phase::Controller,
Class::Alg,
Verify::Metric,
Status::Verified,
"Trivial psymodel: always-long, flat threshold",
),
b(
"C2",
Phase::Controller,
Class::Alg,
Verify::Metric,
Status::Verified,
"Rate-only quantizer (binary-search global_gain, reject clipping)",
),
b(
"C3",
Phase::Controller,
Class::Glue,
Verify::External,
Status::Verified,
"Encoder::send_frame / receive_packet plumbing (mono)",
),
b(
"C4",
Phase::Controller,
Class::Alg,
Verify::External,
Status::Verified,
"Pipeline gate: 81 dB round-trip, FFmpeg-decoded ✓",
),
b(
"Q1",
Phase::Quality,
Class::Gen,
Verify::Reference,
Status::Verified,
"Psymodel constants from formulas (Terhardt ATH, Zwicker Bark, Schroeder)",
),
b(
"Q2",
Phase::Quality,
Class::Alg,
Verify::Reference,
Status::Verified,
"FFT front-end (in-house radix-2; energy per SFB)",
),
b(
"Q3",
Phase::Quality,
Class::Alg,
Verify::Metric,
Status::Verified,
"Masking threshold + SMR per scalefactor band (spread + ATH)",
),
b(
"Q4",
Phase::Quality,
Class::Alg,
Verify::Metric,
Status::Verified,
"Perceptual entropy → bit demand (reservoir budgeting)",
),
b(
"Q5",
Phase::Quality,
Class::Alg,
Verify::External,
Status::Verified,
"Block switching: short-block coding path + attack FSM (transients→short)",
),
b(
"Q6",
Phase::Quality,
Class::Alg,
Verify::Metric,
Status::Verified,
"Outer distortion loop (shapes noise: 7.4→0.2 dB peak NMR)",
),
b(
"R1",
Phase::Roof,
Class::Alg,
Verify::RoundTrip,
Status::Verified,
"Stereo: independent L/R + mid/side joint stereo (per-frame)",
),
b(
"R2",
Phase::Roof,
Class::Alg,
Verify::External,
Status::Verified,
"Bitrate modes: CBR + VBR (quality target → per-frame bitrate, Xing tag)",
),
b(
"R3",
Phase::Roof,
Class::Tbl,
Verify::External,
Status::Verified,
"Xing/Info header (CBR frame count; FFmpeg reads exact duration)",
),
b(
"R4",
Phase::Roof,
Class::Alg,
Verify::External,
Status::Verified,
"Conformance corpus (8 signals: our-decoder floors + FFmpeg clean)",
),
b(
"R5",
Phase::Roof,
Class::Tbl,
Verify::External,
Status::Verified,
"MPEG-2/2.5 LSF: V2 sfb tables + framing (6 rates, FFmpeg-validated)",
),
];
const fn b(
id: &'static str,
phase: Phase,
class: Class,
verify: Verify,
status: Status,
name: &'static str,
) -> Brick {
Brick {
id,
phase,
class,
verify,
status,
name,
}
}
#[must_use]
pub fn by_id(id: &str) -> Option<&'static Brick> {
BRICKS.iter().find(|b| b.id.eq_ignore_ascii_case(id))
}
#[must_use]
pub fn counts() -> [usize; 4] {
let mut c = [0usize; 4];
for brick in BRICKS {
c[match brick.status {
Status::Todo => 0,
Status::Stub => 1,
Status::Impl => 2,
Status::Verified => 3,
}] += 1;
}
c
}
#[must_use]
pub fn next_unbuilt() -> Option<&'static Brick> {
BRICKS
.iter()
.find(|b| matches!(b.status, Status::Todo | Status::Stub))
}
#[must_use]
pub fn table() -> String {
let mut s = String::new();
let mut last = None;
for brick in BRICKS {
if last != Some(brick.phase) {
let _ = write!(s, "\n── {} ──\n", brick.phase.name());
last = Some(brick.phase);
}
let _ = writeln!(s, "{brick}");
}
let [todo, stub, imp, ver] = counts();
let _ = write!(
s,
"\n{} bricks — {ver} verified ✓ · {imp} impl ● · {stub} stub ◐ · {todo} todo ·\n",
BRICKS.len()
);
let simd = BRICKS.iter().filter(|b| accel(b.id) == Accel::Simd).count();
let hybrid = BRICKS
.iter()
.filter(|b| accel(b.id) == Accel::Hybrid)
.count();
let _ = writeln!(
s,
"accel — {simd} SIMD · {hybrid} hybrid · {} safe scalar Rust",
BRICKS.len() - simd - hybrid
);
s
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ids_are_unique() {
for (i, a) in BRICKS.iter().enumerate() {
for b in &BRICKS[i + 1..] {
assert_ne!(a.id, b.id, "duplicate brick id {}", a.id);
}
}
}
#[test]
fn counts_sum_to_total() {
assert_eq!(counts().iter().sum::<usize>(), BRICKS.len());
}
#[test]
fn every_phase_is_present() {
for p in [
Phase::Foundation,
Phase::Analysis,
Phase::Coding,
Phase::Controller,
Phase::Quality,
Phase::Roof,
] {
assert!(
BRICKS.iter().any(|b| b.phase == p),
"no bricks for {}",
p.name()
);
}
}
#[test]
fn next_unbuilt_is_first_incomplete() {
assert_eq!(next_unbuilt().map(|b| b.id), None);
}
#[test]
fn hot_kernels_are_simd_control_stays_scalar() {
for id in ["L1", "L2", "Q2"] {
assert_eq!(accel(id), Accel::Simd, "{id} should be SIMD");
}
for id in ["B5", "B7", "C3", "R3"] {
assert_eq!(accel(id), Accel::Scalar, "{id} should be scalar");
}
}
}