#! Contains FLAC streams made by a small encoder of the tests' own, of every #! kind of subframe, channel assignment, and residual, and the frames each #! decodes to, which, FLAC being lossless, are the samples it was made of. import std::math; import malunal::audio; # Bits written highest first. struct flac_bits { var bytes: vector; var byte: u32; var held: u32; } def flac_bits::new(): flac_bits { return flac_bits{ bytes = vector::::new() }; } def flac_bits::put(&mut this, value: u64, count: u32): void { var k := count; while k > 0 { k--; this.byte = (this.byte << 1) | ((value >> k) & 1) as u32; this.held++; if this.held == 8 { this.bytes.append(this.byte as u8); this.byte = 0; this.held = 0; } } } def flac_bits::signed(&mut this, value: i64, count: u32): void { val masked := count >= 64 ? value as u64 : (value as u64) & (((1 as u64) << count) - 1); this.put(masked, count); } def flac_bits::align(&mut this): void { while this.held != 0 { this.put(0, 1); } } # How a channel of a frame is coded. enum coding: u8 { constant, verbatim, fixed, lpc } # A frame to make: its first frame of the stream, its block, how its channels # are assigned, and how each subframe is coded, of what order, of how many # wasted bits, residual partitions, and whether those are raw. struct frame_plan { var first: u64; var block: u64; var assignment: u32; var kind: coding; var order: u32; var wasted: u32; var partitions: u32; # Their order. var escape: bool; } # The coefficients of the tests' linear prediction, of 12 bits shifted by 9, # by its order. def lpc_coefficients(order: u32): vector { val all: i64[6] = [870, -410, 140, -90, 40, -20]; var made := vector::::new(); for j in 0..order as u64 { made.append(all[j]); } return made; } # The prediction of a sample by those before it. def predicted(s: &vector, i: u64, kind: coding, order: u32): i64 { if kind == coding::fixed { return match order { 0 => 0, 1 => s[i - 1], 2 => 2 * s[i - 1] - s[i - 2], 3 => 3 * s[i - 1] - 3 * s[i - 2] + s[i - 3], else => 4 * s[i - 1] - 6 * s[i - 2] + 4 * s[i - 3] - s[i - 4], }; } val c := lpc_coefficients(order); var sum: i64 = 0; for j in 0..order as u64 { sum += c[j] * s[i - 1 - j]; } return sum >> 9; } # Writes a subframe of samples, of so many bits a sample. def put_subframe(w: &mut flac_bits, samples: &vector, bits: u32, plan: &frame_plan): void { val block := samples.length; var kind := plan.kind; var code: u32 = 1; if kind == coding::constant { code = 0; } else if kind == coding::fixed { code = 8 + plan.order; } else if kind == coding::lpc { code = 31 + plan.order; } w.put(0, 1); w.put(code, 6); # Wasted bits: the samples are all multiples of their power of two. var shifted := vector::::new(); for i in 0..block { shifted.append(samples[i] >> plan.wasted); } if plan.wasted > 0 { w.put(1, 1); w.put(0, plan.wasted - 1); w.put(1, 1); } else { w.put(0, 1); } val width := bits - plan.wasted; if kind == coding::constant { w.signed(shifted[0], width); return; } if kind == coding::verbatim { for i in 0..block { w.signed(shifted[i], width); } return; } val order := plan.order as u64; for i in 0..order { w.signed(shifted[i], width); } if kind == coding::lpc { w.put(11, 4); w.signed(9, 5); val c := lpc_coefficients(plan.order); for j in 0..order { w.signed(c[j], 12); } } var residual := vector::::new(); for i in order..block { residual.append(shifted[i] - predicted(&shifted, i, kind, plan.order)); } # The residual: partitions of Rice codes of a parameter each, or raw. w.put(1, 2); w.put(plan.partitions, 4); val count: u64 = (1 as u64) << plan.partitions; val each := block / count; var at: u64 = 0; for p in 0..count { val length := p == 0 ? each - order : each; var largest: u64 = 0; for i in 0..length { largest = math::max(largest, math::abs(residual[at + i]) as u64); } if plan.escape { var raw: u32 = 1; while ((1 as u64) << (raw - 1)) <= largest { raw++; } w.put(31, 5); w.put(raw, 5); for i in 0..length { w.signed(residual[at + i], raw); } } else { var k: u32 = 0; while ((1 as u64) << (k + 1)) <= largest && k < 20 { k++; } w.put(k, 5); for i in 0..length { val v := residual[at + i]; val folded: u64 = v >= 0 ? (v as u64) << 1 : ((-v) as u64 << 1) - 1; val high := folded >> k; w.put(0, high as u32); w.put(1, 1); w.put(folded, k); } } at += length; } } # The CRC-8 and CRC-16 of FLAC's frames. def test_crc8(bytes: span): u32 { var crc: u32 = 0; for i in 0..bytes.length { crc = crc ^ bytes[i] as u32; for _ in 0..8 { crc = (crc & 0x80) != 0 ? ((crc << 1) ^ 0x07) & 0xFF : (crc << 1) & 0xFF; } } return crc; } def test_crc16(bytes: span): u32 { var crc: u32 = 0; for i in 0..bytes.length { crc = crc ^ ((bytes[i] as u32) << 8); for _ in 0..8 { crc = (crc & 0x8000) != 0 ? ((crc << 1) ^ 0x8005) & 0xFFFF : (crc << 1) & 0xFFFF; } } return crc; } # Writes a frame of each channel's samples of the stream, from its first. def put_frame(out: &mut vector, channels: &vector>, bits: u32, plan: &frame_plan, variable: bool, number: u64): void { var w := flac_bits::new(); w.put(0xFFF8 | (variable ? 1u : 0u), 16); w.put(7, 4); # its block size, 16 bits, after the number w.put(0, 4); # its rate, the stream's w.put(plan.assignment, 4); w.put(bits == 16 ? 4u : bits == 24 ? 6u : 0u, 3); w.put(0, 1); # The number, as UTF-8 codes it. if number < 0x80 { w.put(number, 8); } else if number < 0x800 { w.put(0xC0 | (number >> 6), 8); w.put(0x80 | (number & 0x3F), 8); } else if number < 0x10000 { w.put(0xE0 | (number >> 12), 8); w.put(0x80 | ((number >> 6) & 0x3F), 8); w.put(0x80 | (number & 0x3F), 8); } else { w.put(0xF0 | (number >> 18), 8); w.put(0x80 | ((number >> 12) & 0x3F), 8); w.put(0x80 | ((number >> 6) & 0x3F), 8); w.put(0x80 | (number & 0x3F), 8); } w.put(plan.block - 1, 16); w.put(test_crc8(w.bytes.span) as u64, 8); # Each channel, of the frame's samples, a side channel made of two. var parts := vector::>::new(); for c in 0..channels.length { var part := vector::::new(); for i in 0..plan.block { part.append(channels[c][plan.first + i]); } parts.append(move(part)); } if plan.assignment >= 8 { var a := vector::::new(); var b := vector::::new(); for i in 0..plan.block { val l := parts[0][i]; val r := parts[1][i]; if plan.assignment == 8 { a.append(l); b.append(l - r); } else if plan.assignment == 9 { a.append(l - r); b.append(r); } else { a.append((l + r) >> 1); b.append(l - r); } } parts[0] = move(a); parts[1] = move(b); } for c in 0..parts.length { val side := (plan.assignment == 8 && c == 1) || (plan.assignment == 9 && c == 0) || (plan.assignment == 10 && c == 1); put_subframe(&mut w, &parts[c], side ? bits + 1 : bits, plan); } w.align(); val crc := test_crc16(w.bytes.span); w.put(crc as u64, 16); out.extend(w.bytes.span); } # A FLAC stream made by the tests: its bytes, and the frames it decodes to. struct flac_stream { var bytes: vector; var expected: vector; var channels: vector>; } # The samples of a channel: two sines and noise, of so many bits, or a run all # the same where `flat` says, as a constant subframe codes. def flac_samples(channel: u64, count: u64, bits: u32, wasted: u32): vector { var made := vector::::new(); var noise: u64 = 0x9E3779B97F4A7C15 + channel; val peak := ((1 as i64) << (bits - 2)) as f64; for i in 0..count { noise = math::wrapping_add(math::wrapping_mul(noise, 6364136223846793005), 1442695040888963407); val wobble := ((noise >> 40) & 0xFFF) as f64 / 4096.0 - 0.5; val x := i as f64; var v := math::round(peak * (0.6 * math::sin(x * (0.01 + 0.003 * channel as f64)) + 0.3 * math::sin(x * 0.17) + 0.05 * wobble)) as i64; v = (v >> wasted) << wasted; made.append(v); } return made; } # Makes a FLAC stream of channels of samples, coded as the plans say, its # blocks fixed in size, numbered by frame, or varying, numbered by sample; with # a seek table where `table` says, and comments where `comments` gives them. def made_flac(channels: u32, bits: u32, plans: span, variable: bool, table: bool, comments: span): flac_stream { var total: u64 = 0; var most: u64 = 0; var least: u64 = 0xFFFF; for &plan in plans { total = math::max(total, plan.first + plan.block); most = math::max(most, plan.block); least = math::min(least, plan.block); } var samples := vector::>::new(); for c in 0..channels as u64 { samples.append(flac_samples(c, total, bits, plans[0].wasted)); # A block coded constant is all its first sample. for &plan in plans { continue if plan.kind != coding::constant; for i in plan.first..plan.first + plan.block { samples[c][i] = samples[c][plan.first]; } } } var out := vector::::new(); for &letter in ['f', 'L', 'a', 'C'] { out.append(letter as u8); } # STREAMINFO, then the seek table and comments where asked, the last marked. var info := flac_bits::new(); info.put(variable ? least : most, 16); info.put(most, 16); info.put(0, 24); info.put(0, 24); info.put(48000, 20); info.put((channels - 1) as u64, 3); info.put((bits - 1) as u64, 5); info.put(total, 36); info.put(0, 64); info.put(0, 64); val blocks := 1 + (table ? 1u : 0u) + (comments.length > 0 ? 1u : 0u); out.append((blocks == 1 ? 0x80 : 0) as u8); out.append(0); out.append(0); out.append(34); out.extend(info.bytes.span); # The frames, written first, so the seek table knows where each is. var frames := vector::::new(); var starts := vector::::new(); for i in 0..plans.length { starts.append(frames.length); put_frame(&mut frames, &samples, bits, &plans[i], variable, variable ? plans[i].first : i); } if table { var points := flac_bits::new(); for i in 0..plans.length { continue if i % 3 != 0; points.put(plans[i].first, 64); points.put(starts[i], 64); points.put(plans[i].block, 16); } points.put(0xFFFFFFFFFFFFFFFF, 64); # a placeholder, of none points.put(0, 64); points.put(0, 16); val last := comments.length == 0; out.append((last ? 0x83 : 3) as u8); out.append((points.bytes.length >> 16) as u8); out.append((points.bytes.length >> 8) as u8); out.append(points.bytes.length as u8); out.extend(points.bytes.span); } if comments.length > 0 { var block := vector::::new(); put_le(&mut block, 4); for &letter in ['t', 'e', 's', 't'] { block.append(letter as u8); } put_le(&mut block, comments.length as u32); for &each in comments { put_le(&mut block, each.length as u32); for i in 0..each.length { block.append(each[i] as u8); } } out.append(0x84); out.append((block.length >> 16) as u8); out.append((block.length >> 8) as u8); out.append(block.length as u8); out.extend(block.span); } out.extend(frames.span); # What it decodes to: the samples, as floats. var expected := vector::::new(); val scale := 1.0 / ((1 as u64) << (bits - 1)) as f64; for i in 0..total { for c in 0..channels as u64 { expected.append((samples[c][i] as f64 * scale) as f32); } } return flac_stream{ bytes = move(out), expected = move(expected), channels = move(samples) }; } def put_le(out: &mut vector, value: u32): void { out.append(value as u8); out.append((value >> 8) as u8); out.append((value >> 16) as u8); out.append((value >> 24) as u8); } # Plans of every kind of subframe and channel assignment in turn, a block each # of so many frames. def every_kind(count: u64, block: u64, channels: u32): vector { val kinds: coding[4] = [coding::constant, coding::verbatim, coding::fixed, coding::lpc]; var made := vector::::new(); for i in 0..count { var plan := frame_plan{ first = i * block, block = block, kind = kinds[i % 4], partitions = (i % 3) as u32, escape = i % 5 == 4 }; plan.order = plan.kind == coding::fixed ? ((i / 4) % 5) as u32 : plan.kind == coding::lpc ? 1 + ((i / 4) % 6) as u32 : 0u; val stereo: u32[4] = [1, 8, 9, 10]; plan.assignment = channels == 2 ? stereo[i % 4] : channels - 1; made.append(plan); } return made; }