Initial Commit
This commit is contained in:
64 files changed
+14785
No files matched your search
@@ -0,0 +1,186 @@
|
||||
#! Contains the tests of Ogg Opus: files of the tests' own, `tests/files`,
|
||||
#! tones libopus encoded through FFmpeg of sines, of CELT in stereo, of SILK of
|
||||
#! one channel at 16 kHz, and of one channel moving from hybrid frames to
|
||||
#! SILK's to CELT's, each decoded near the sines it was made of; streams made
|
||||
#! again of their packets, a page each, sought by halving their pages, and
|
||||
#! played streamed as they play decoded; their heads' gain and their comments'
|
||||
#! loop points read; and what isn't read refused.
|
||||
#!
|
||||
#! RFC 8251's test vectors were checked apart from these: each of the 12,
|
||||
#! decoded in stereo, within one step of 16 bits of libopus's own output, those
|
||||
#! of SILK alone exactly; decoded in one channel, 63 dB or nearer to libopus's
|
||||
#! mix of it.
|
||||
|
||||
import std::math;
|
||||
import std::testing;
|
||||
import malunal::audio;
|
||||
import malunal::audio::ogg;
|
||||
|
||||
# The packets of the first stream of an Ogg file.
|
||||
def packets_of(bytes: span<u8>): vector<vector<u8>> {
|
||||
var made := vector::<vector<u8>>::new();
|
||||
val first := ogg::page_at(bytes, 0) else (_) { return made; };
|
||||
var stream := ogg::packets::of(bytes, first.serial, 0);
|
||||
var next := ogg::packet{};
|
||||
while true {
|
||||
val had := stream.next(&mut next) else (_) { break; };
|
||||
break if !had;
|
||||
var copy := vector::<u8>::new();
|
||||
copy.extend(next.data);
|
||||
made.append(move(copy));
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# An Opus head, of 48 kHz.
|
||||
def opus_head(version: u8, channels: u8, pre_skip: u32, gain: i32, family: u8): vector<u8> {
|
||||
var made := vector::<u8>::new();
|
||||
for c in "OpusHead" {
|
||||
made.append(c as u8);
|
||||
}
|
||||
made.append(version);
|
||||
made.append(channels);
|
||||
made.append((pre_skip & 0xFF) as u8);
|
||||
made.append((pre_skip >> 8) as u8);
|
||||
put_u32(&mut made, 48000);
|
||||
val bits := (gain < 0 ? gain + 65536 : gain) as u32;
|
||||
made.append((bits & 0xFF) as u8);
|
||||
made.append((bits >> 8) as u8);
|
||||
made.append(family);
|
||||
return made;
|
||||
}
|
||||
|
||||
# Opus comments.
|
||||
def opus_tags(comments: span<strview>): vector<u8> {
|
||||
var made := vector::<u8>::new();
|
||||
for c in "OpusTags" {
|
||||
made.append(c as u8);
|
||||
}
|
||||
put_u32(&mut made, 5);
|
||||
for c in "tests" {
|
||||
made.append(c as u8);
|
||||
}
|
||||
put_u32(&mut made, comments.length as u32);
|
||||
for comment in comments {
|
||||
put_u32(&mut made, comment.length as u32);
|
||||
for c in comment {
|
||||
made.append(c as u8);
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# An Ogg Opus file of a head, comments, and packets of audio, a page each,
|
||||
# their granule positions counted on but the last's, which is `end`.
|
||||
def opus_file(head: span<u8>, tags: span<u8>, audio: span<vector<u8>>, end: u64): vector<u8> {
|
||||
var made := vector::<u8>::new();
|
||||
val none: u8[1] = [0];
|
||||
page_of(&mut made, 2, 0, 0, head, none[0:0]);
|
||||
page_of(&mut made, 0, 0, 1, tags, none[0:0]);
|
||||
var granule: u64 = 0;
|
||||
for i in 0..audio.length {
|
||||
val last := i + 1 == audio.length;
|
||||
granule += 960;
|
||||
page_of(&mut made, last ? 4 : 0, last ? end : granule, (i + 2) as u32, audio[i].span, none[0:0]);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
@test def decodes_opus_to_the_tone_it_was_made_of(t: &mut tester): void {
|
||||
val stereo := test_file("tone.opus");
|
||||
val found := audio::format_of(stereo.span) else { t.expects(false); return; };
|
||||
t.expects(found == format::opus);
|
||||
val read := audio::decode(stereo.span) else (e) { t.expects(false); return; };
|
||||
t.expects(read.rate == 48000 && read.channels == 2 && read.frames == 72000);
|
||||
val both: f64[2] = [440, 660];
|
||||
t.expects(tone_error(&read, both[:]) < 0.005);
|
||||
|
||||
# SILK's, and one moving between SILK's, hybrid, and CELT's frames.
|
||||
val one: f64[1] = [330];
|
||||
for name in ["tone16.opus", "tone_mono.opus"] {
|
||||
val bytes := test_file(name);
|
||||
val low := audio::decode(bytes.span) else (e) { t.expects(false); return; };
|
||||
t.expects(low.rate == 48000 && low.channels == 1 && low.frames == 48000);
|
||||
t.expects(tone_error(&low, one[:]) < 0.04);
|
||||
}
|
||||
}
|
||||
|
||||
@test def seeks_opus_by_its_pages(t: &mut tester): void {
|
||||
val original := test_file("tone.opus");
|
||||
val whole := audio::decode(original.span) else (e) { t.expects(false); return; };
|
||||
val packets := packets_of(original.span);
|
||||
t.expects(packets.length == 78);
|
||||
val bytes := opus_file(packets[0].span, packets[1].span, packets.span[2:76], 72000 + 312);
|
||||
val again := audio::decode(bytes.span) else (e) { t.expects(false); return; };
|
||||
t.expects(again.frames == 72000 && apart(again.samples.span, whole.samples.span) == 0);
|
||||
|
||||
# From half a second before, which settles near the tone decoded through.
|
||||
var reading := audio::opus::decoder::open(bytes.span) else (e) { t.expects(false); return; };
|
||||
var piece := vector::<f32>::new();
|
||||
piece.resize(2000 * 2);
|
||||
val targets: u64[6] = [0, 1, 1000, 30000, 71999, 72000];
|
||||
for &target in targets {
|
||||
t.expects(reading.seek(target) is value(_));
|
||||
val count := reading.read(piece.slice) else (e) { t.expects(false); return; };
|
||||
val wanted: u64 = 72000 - target < 2000 ? 72000 - target : 2000;
|
||||
t.expects(count == wanted);
|
||||
t.expects(apart(piece.span[0:count * 2], whole.samples.span[target * 2:count * 2]) < 1e-3);
|
||||
}
|
||||
# A stream plays as the sound decoded whole.
|
||||
var m := mixer::new(48000);
|
||||
val streamed := m.add_streamed(bytes.span) else (e) { t.expects(false); return; };
|
||||
val decoded := m.add(audio::decode(bytes.span) else (e) { t.expects(false); return; });
|
||||
_ = m.play(streamed);
|
||||
val from_stream := mixed(&mut m, 72010);
|
||||
_ = m.play(decoded);
|
||||
val from_sound := mixed(&mut m, 72010);
|
||||
t.expects(apart(from_stream.span, from_sound.span) < 1e-6);
|
||||
}
|
||||
|
||||
@test def reads_opus_gain_and_loop_points(t: &mut tester): void {
|
||||
val original := test_file("tone.opus");
|
||||
val whole := audio::decode(original.span) else (e) { t.expects(false); return; };
|
||||
val packets := packets_of(original.span);
|
||||
# Louder by 6 dB, in Q8.
|
||||
val head := opus_head(1, 2, 312, 6 * 256, 0);
|
||||
val comments: strview[2] = ["LOOPSTART=4800", "LOOPLENGTH=9600"];
|
||||
val tags := opus_tags(comments[:]);
|
||||
val bytes := opus_file(head.span, tags.span, packets.span[2:76], 72000 + 312);
|
||||
val louder := audio::decode(bytes.span) else (e) { t.expects(false); return; };
|
||||
t.expects(louder.loop_points.start == 4800 && louder.loop_points.end == 14400);
|
||||
val gain := math::pow(10.0, 6.0 / 20);
|
||||
var worst: f64 = 0;
|
||||
for i in 0..whole.samples.length {
|
||||
worst = math::max(worst, math::abs(louder.samples[i] as f64 - whole.samples[i] as f64 * gain));
|
||||
}
|
||||
t.expects(worst < 1e-5);
|
||||
}
|
||||
|
||||
@test def refuses_what_isnt_opus(t: &mut tester): void {
|
||||
val original := test_file("tone.opus");
|
||||
val packets := packets_of(original.span);
|
||||
val tags := opus_tags(no_comments());
|
||||
# More than two channels, a mapping family other than 0, and a later version.
|
||||
val heads: u8[9] = [1, 3, 0, 1, 2, 1, 16, 2, 0];
|
||||
for k in 0..3 {
|
||||
val head := opus_head(heads[k * 3], heads[k * 3 + 1], 312, 0, heads[k * 3 + 2]);
|
||||
val bytes := opus_file(head.span, tags.span, packets.span[2:76], 72312);
|
||||
t.expects(fails(audio::decode(bytes.span), audio_error::unsupported));
|
||||
}
|
||||
# A packet of frames of no count.
|
||||
var broken := vector::<vector<u8>>::new();
|
||||
for i in 2u..10u {
|
||||
var copy := vector::<u8>::new();
|
||||
copy.extend(packets[i].span);
|
||||
broken.append(move(copy));
|
||||
}
|
||||
broken[3].clear();
|
||||
broken[3].append(0xFB);
|
||||
broken[3].append(0x00);
|
||||
val bad := opus_file(packets[0].span, tags.span, broken.span, 8 * 960);
|
||||
t.expects(fails(audio::decode(bad.span), audio_error::malformed));
|
||||
# Cut short, partway through its last page.
|
||||
var cut := test_file("tone.opus");
|
||||
cut.truncate();
|
||||
t.expects(fails(audio::decode(cut.span), audio_error::malformed));
|
||||
}
|
||||
Reference in new issue
Block a user