108 lines
2.8 KiB
Plaintext
108 lines
2.8 KiB
Plaintext
#! Contains how the tests write what they encode: bits as Vorbis packs them,
|
|||
|
|
#! and Ogg's pages, each laced and checked by its CRC, worked out bit by bit as
|
||
|
|
#! RFC 3533 defines it, apart from how the decoder works it out.
|
||
|
|
|
||
|
|
# Bits written as Vorbis packs them: each number's lowest bit first.
|
||
|
|
struct bit_writer {
|
||
|
|
var bytes: vector<u8>;
|
||
|
|
var held: u32;
|
||
|
|
var count: u32;
|
||
|
|
}
|
||
|
|
|
||
|
|
def bit_writer::new(): bit_writer {
|
||
|
|
return bit_writer{ bytes = vector::<u8>::new() };
|
||
|
|
}
|
||
|
|
|
||
|
|
def bit_writer::put(&mut this, value: u32, width: u32): void {
|
||
|
|
for i in 0..width {
|
||
|
|
this.held |= ((value >> i) & 1) << this.count;
|
||
|
|
this.count++;
|
||
|
|
if this.count == 8 {
|
||
|
|
this.bytes.append(this.held as u8);
|
||
|
|
this.held = 0;
|
||
|
|
this.count = 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# A codeword, its first bit highest, as a codebook's are read.
|
||
|
|
def bit_writer::code(&mut this, codeword: u32, length: u32): void {
|
||
|
|
for i in 0..length {
|
||
|
|
this.put((codeword >> (length - 1 - i)) & 1, 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
def bit_writer::text(&mut this, text: strview): void {
|
||
|
|
for i in 0..text.length {
|
||
|
|
this.put(text[i] as u32, 8);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
def bit_writer::finish(&mut this): vector<u8> {
|
||
|
|
if this.count > 0 {
|
||
|
|
this.bytes.append(this.held as u8);
|
||
|
|
}
|
||
|
|
return move(this.bytes);
|
||
|
|
}
|
||
|
|
|
||
|
|
# A packet's lacing: 255 for each whole 255 bytes, then what's left.
|
||
|
|
def lace(into: &mut vector<u8>, length: u64): void {
|
||
|
|
var left := length;
|
||
|
|
while left >= 255 {
|
||
|
|
into.append(255);
|
||
|
|
left -= 255;
|
||
|
|
}
|
||
|
|
into.append(left as u8);
|
||
|
|
}
|
||
|
|
|
||
|
|
# A page of up to two packets.
|
||
|
|
def page_of(out: &mut vector<u8>, flags: u8, granule: u64, sequence: u32, one: span<u8>, two: span<u8>): void {
|
||
|
|
var lacing := vector::<u8>::new();
|
||
|
|
var body := vector::<u8>::new();
|
||
|
|
lace(&mut lacing, one.length);
|
||
|
|
body.extend(one);
|
||
|
|
if two.length > 0 {
|
||
|
|
lace(&mut lacing, two.length);
|
||
|
|
body.extend(two);
|
||
|
|
}
|
||
|
|
page_laced(out, flags, granule, sequence, lacing.span, body.span);
|
||
|
|
}
|
||
|
|
|
||
|
|
# A page, its CRC worked out bit by bit, as RFC 3533 defines it.
|
||
|
|
def page_laced(out: &mut vector<u8>, flags: u8, granule: u64, sequence: u32, lacing: span<u8>, body: span<u8>): void {
|
||
|
|
val start := out.length;
|
||
|
|
for c in "OggS" {
|
||
|
|
out.append(c as u8);
|
||
|
|
}
|
||
|
|
out.append(0);
|
||
|
|
out.append(flags);
|
||
|
|
for i in 0u..8u {
|
||
|
|
out.append((granule >> (i * 8) as u32) as u8);
|
||
|
|
}
|
||
|
|
put_u32(out, 0x51A7);
|
||
|
|
put_u32(out, sequence);
|
||
|
|
put_u32(out, 0);
|
||
|
|
out.append(lacing.length as u8);
|
||
|
|
out.extend(lacing);
|
||
|
|
out.extend(body);
|
||
|
|
var crc: u32 = 0;
|
||
|
|
for i in start..out.length {
|
||
|
|
crc ^= (out[i] as u32) << 24;
|
||
|
|
for _ in 0..8 {
|
||
|
|
val high := (crc & 0x80000000) != 0;
|
||
|
|
crc = (crc & 0x7FFFFFFF) << 1;
|
||
|
|
if high {
|
||
|
|
crc ^= 0x04C11DB7;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for i in 0u..4u {
|
||
|
|
out[start + 22 + i] = (crc >> (i * 8) as u32) as u8;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
def put_u32(out: &mut vector<u8>, value: u32): void {
|
||
|
|
for i in 0u..4u {
|
||
|
|
out.append((value >> (i * 8) as u32) as u8);
|
||
|
|
}
|
||
|
|
}
|