687 lines
25 KiB
Plaintext
687 lines
25 KiB
Plaintext
#! Contains `decoder`, which reads an Ogg Opus stream's frames as it's asked
|
|||
|
|
#! for them, so a long one plays without being held decoded, and `decode`, which
|
||
|
|
#! reads one whole.
|
||
|
|
#!
|
||
|
|
#! Opus is decoded as RFC 6716 has it and libopus's float build decodes it,
|
||
|
|
#! whose decoder this is a port of: each packet's frames are SILK's, of speech,
|
||
|
|
#! CELT's, of music, or hybrid, of both, SILK's below 8 kHz and CELT's above,
|
||
|
|
#! and a stream moves between them as it goes, with CELT's redundant frames, or
|
||
|
|
#! a crossfade, smoothing each change. Everything is decoded at 48 kHz, SILK
|
||
|
|
#! resampled up from its own rate. A stream of one or two channels, of channel
|
||
|
|
#! mapping family 0, is read; streams of more aren't. Its pre-skip is cut from
|
||
|
|
#! its start and its end cut where its last page's granule position says, as
|
||
|
|
#! RFC 7845 has it, its output gain applied, and its comments read for where
|
||
|
|
#! it loops, as `audio::loop_of_comments` reads them. A stream is sought by
|
||
|
|
#! halving its pages, then decoding from half a second before where it's
|
||
|
|
#! sought, more than the 80 ms Opus asks, so its decoder settles to within a
|
||
|
|
#! few ten-thousandths of decoding straight through, if not exactly.
|
||
|
|
#!
|
||
|
|
#! A lost or empty frame is decoded as silence, rather than concealed as
|
||
|
|
#! libopus does.
|
||
|
|
|
||
|
|
# libopus, which this decoder is a port of, is:
|
||
|
|
#
|
||
|
|
# Copyright 2001-2023 Xiph.Org, Skype Limited, Octasic,
|
||
|
|
# Jean-Marc Valin, Timothy B. Terriberry,
|
||
|
|
# CSIRO, Gregory Maxwell, Mark Borgerding,
|
||
|
|
# Erik de Castro Lopo, Mozilla, Amazon
|
||
|
|
#
|
||
|
|
# Redistribution and use in source and binary forms, with or without
|
||
|
|
# modification, are permitted provided that the following conditions
|
||
|
|
# are met:
|
||
|
|
#
|
||
|
|
# - Redistributions of source code must retain the above copyright
|
||
|
|
# notice, this list of conditions and the following disclaimer.
|
||
|
|
#
|
||
|
|
# - Redistributions in binary form must reproduce the above copyright
|
||
|
|
# notice, this list of conditions and the following disclaimer in the
|
||
|
|
# documentation and/or other materials provided with the distribution.
|
||
|
|
#
|
||
|
|
# - Neither the name of Internet Society, IETF or IETF Trust, nor the
|
||
|
|
# names of specific contributors, may be used to endorse or promote
|
||
|
|
# products derived from this software without specific prior written
|
||
|
|
# permission.
|
||
|
|
#
|
||
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||
|
|
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||
|
|
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||
|
|
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
||
|
|
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||
|
|
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||
|
|
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||
|
|
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||
|
|
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||
|
|
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||
|
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
|
|
||
|
|
import std::math;
|
||
|
|
import malunal::audio::ogg;
|
||
|
|
module malunal::audio::opus;
|
||
|
|
|
||
|
|
|
||
|
|
# Each frame's mode.
|
||
|
|
const mode_silk: i32 = 1000;
|
||
|
|
const mode_hybrid: i32 = 1001;
|
||
|
|
const mode_celt: i32 = 1002;
|
||
|
|
|
||
|
|
# Each frame's bandwidth: narrow, medium, wide, super-wide, and full.
|
||
|
|
const band_nb: i32 = 1101;
|
||
|
|
const band_mb: i32 = 1102;
|
||
|
|
const band_wb: i32 = 1103;
|
||
|
|
const band_swb: i32 = 1104;
|
||
|
|
const band_fb: i32 = 1105;
|
||
|
|
|
||
|
|
# The frames of 20, 10, 5, and 2.5 ms, at 48 kHz.
|
||
|
|
const f20: i32 = 960;
|
||
|
|
const f10: i32 = 480;
|
||
|
|
const f5: i32 = 240;
|
||
|
|
const f2_5: i32 = 120;
|
||
|
|
|
||
|
|
# The frames decoded before a frame sought, so the decoder settles: half a
|
||
|
|
# second, as CELT's energies, predicted from the frame before, halve their
|
||
|
|
# error a frame of 20 ms, and those of 2.5 ms a tenth.
|
||
|
|
const preroll: u64 = 24000;
|
||
|
|
|
||
|
|
# The most frames a packet decodes to: 120 ms.
|
||
|
|
const most_packet_frames: u64 = 5760;
|
||
|
|
|
||
|
|
# What decoding frames keeps between them: libopus's `OpusDecoder`.
|
||
|
|
struct frames_state {
|
||
|
|
var channels: i32;
|
||
|
|
var celt: celt_decoder;
|
||
|
|
var silk: silk_decoder;
|
||
|
|
var stream_channels: i32; # Of the packet being decoded.
|
||
|
|
var mode: i32;
|
||
|
|
var prev_mode: i32; # 0 before any frame.
|
||
|
|
var bandwidth: i32;
|
||
|
|
var frame_size: i32;
|
||
|
|
var prev_redundancy: bool;
|
||
|
|
var gain: f32; # The stream's output gain, as a multiple.
|
||
|
|
var silk_pcm: vector<i16>;
|
||
|
|
var transition: vector<f32>;
|
||
|
|
var redundant: vector<f32>;
|
||
|
|
}
|
||
|
|
|
||
|
|
def frames_state::new(channels: i32, gain_q8: i32): frames_state {
|
||
|
|
var made := frames_state{
|
||
|
|
channels = channels, celt = celt_decoder::new(channels), silk = silk_decoder::new(),
|
||
|
|
silk_pcm = vector::<i16>::new(), transition = vector::<f32>::new(), redundant = vector::<f32>::new(), gain = 1,
|
||
|
|
};
|
||
|
|
if gain_q8 != 0 {
|
||
|
|
made.gain = math::exp(0.6931471805599453094 * (6.48814081e-4 as f32 * gain_q8 as f32) as f64) as f32;
|
||
|
|
}
|
||
|
|
made.silk_pcm.resize(most_packet_frames * 2);
|
||
|
|
made.transition.resize((f5 * 2) as u64);
|
||
|
|
made.redundant.resize((f5 * 2) as u64);
|
||
|
|
made.reset();
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Forgets every frame before.
|
||
|
|
def frames_state::reset(&mut this): void {
|
||
|
|
this.celt.reset();
|
||
|
|
this.silk.reset();
|
||
|
|
this.stream_channels = this.channels;
|
||
|
|
this.mode = 0;
|
||
|
|
this.prev_mode = 0;
|
||
|
|
this.bandwidth = 0;
|
||
|
|
this.frame_size = 120;
|
||
|
|
this.prev_redundancy = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The frames of a packet, as its first byte, its table of contents, says.
|
||
|
|
def samples_per_frame(toc: u8): i32 {
|
||
|
|
if (toc & 0x80) != 0 {
|
||
|
|
return (48000 << ((toc >> 3) & 3) as u32) / 400;
|
||
|
|
}
|
||
|
|
if (toc & 0x60) == 0x60 {
|
||
|
|
return (toc & 0x08) != 0 ? 960 : 480;
|
||
|
|
}
|
||
|
|
val size := ((toc >> 3) & 3) as i32;
|
||
|
|
return size == 3 ? 2880 : (48000 << size as u32) / 100;
|
||
|
|
}
|
||
|
|
|
||
|
|
def mode_of(toc: u8): i32 {
|
||
|
|
return (toc & 0x80) != 0 ? mode_celt : (toc & 0x60) == 0x60 ? mode_hybrid : mode_silk;
|
||
|
|
}
|
||
|
|
|
||
|
|
def bandwidth_of(toc: u8): i32 {
|
||
|
|
if (toc & 0x80) != 0 {
|
||
|
|
val made := band_mb + ((toc >> 5) & 3) as i32;
|
||
|
|
return made == band_mb ? band_nb : made;
|
||
|
|
}
|
||
|
|
if (toc & 0x60) == 0x60 {
|
||
|
|
return (toc & 0x10) != 0 ? band_fb : band_swb;
|
||
|
|
}
|
||
|
|
return band_nb + ((toc >> 5) & 3) as i32;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A frame's size as a packet codes it: in one byte, or two.
|
||
|
|
def frame_size_at(data: span<u8>, at: u64, sizes: &mut i32[48], index: u64): i32 {
|
||
|
|
return -1 if at >= data.length;
|
||
|
|
if data[at] < 252 {
|
||
|
|
sizes[index] = data[at] as i32;
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
return -1 if at + 1 >= data.length;
|
||
|
|
sizes[index] = 4 * data[at + 1] as i32 + data[at] as i32;
|
||
|
|
return 2;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Decodes a packet's frames.
|
||
|
|
# @returns How many frames it gave, or -1 where it's malformed.
|
||
|
|
def frames_state::decode_packet(&mut this, data: span<u8>, pcm: slice<f32>): i32 {
|
||
|
|
return -1 if data.length == 0;
|
||
|
|
val toc := data[0];
|
||
|
|
val frame_size := samples_per_frame(toc);
|
||
|
|
var sizes: i32[48];
|
||
|
|
var count: i32 = 1;
|
||
|
|
var at: u64 = 1;
|
||
|
|
var len := data.length as i32 - 1;
|
||
|
|
var last_size := len;
|
||
|
|
match toc & 3 {
|
||
|
|
0 => {}
|
||
|
|
1 => {
|
||
|
|
count = 2;
|
||
|
|
return -1 if (len & 1) != 0;
|
||
|
|
last_size = len / 2;
|
||
|
|
sizes[0] = last_size;
|
||
|
|
}
|
||
|
|
2 => {
|
||
|
|
count = 2;
|
||
|
|
val bytes := frame_size_at(data, at, &mut sizes, 0);
|
||
|
|
return -1 if bytes < 0;
|
||
|
|
len -= bytes;
|
||
|
|
return -1 if sizes[0] > len;
|
||
|
|
at += bytes as u64;
|
||
|
|
last_size = len - sizes[0];
|
||
|
|
}
|
||
|
|
else => {
|
||
|
|
return -1 if len < 1;
|
||
|
|
val ch := data[at];
|
||
|
|
at++;
|
||
|
|
count = (ch & 0x3F) as i32;
|
||
|
|
return -1 if count <= 0 || frame_size * count > 5760;
|
||
|
|
len--;
|
||
|
|
if (ch & 0x40) != 0 {
|
||
|
|
# Padding.
|
||
|
|
while true {
|
||
|
|
return -1 if len <= 0;
|
||
|
|
val p := data[at] as i32;
|
||
|
|
at++;
|
||
|
|
len--;
|
||
|
|
val tmp := p == 255 ? 254 : p;
|
||
|
|
len -= tmp;
|
||
|
|
break if p != 255;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return -1 if len < 0;
|
||
|
|
if (ch & 0x80) != 0 {
|
||
|
|
# Of frames of their own sizes.
|
||
|
|
last_size = len;
|
||
|
|
for i in 0..count - 1 {
|
||
|
|
val bytes := frame_size_at(data, at, &mut sizes, i as u64);
|
||
|
|
return -1 if bytes < 0;
|
||
|
|
len -= bytes;
|
||
|
|
return -1 if sizes[i as u64] > len;
|
||
|
|
at += bytes as u64;
|
||
|
|
last_size -= bytes + sizes[i as u64];
|
||
|
|
}
|
||
|
|
return -1 if last_size < 0;
|
||
|
|
} else {
|
||
|
|
last_size = len / count;
|
||
|
|
return -1 if last_size * count != len;
|
||
|
|
for i in 0..count - 1 {
|
||
|
|
sizes[i as u64] = last_size;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return -1 if last_size > 1275;
|
||
|
|
sizes[(count - 1) as u64] = last_size;
|
||
|
|
|
||
|
|
this.mode = mode_of(toc);
|
||
|
|
this.bandwidth = bandwidth_of(toc);
|
||
|
|
this.frame_size = frame_size;
|
||
|
|
this.stream_channels = (toc & 4) != 0 ? 2 : 1;
|
||
|
|
var made: i32 = 0;
|
||
|
|
for i in 0..count {
|
||
|
|
val size := sizes[i as u64] as u64;
|
||
|
|
val frame := data[at:size];
|
||
|
|
val got := this.decode_frame(frame, true, slice::<f32>::over(pcm.data + (made * this.channels) as u64, (frame_size * this.channels) as u64), frame_size);
|
||
|
|
return -1 if got < 0;
|
||
|
|
at += size;
|
||
|
|
made += got;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A crossfade from one signal to another over 2.5 ms, by the square of CELT's window.
|
||
|
|
def smooth_fade(from: slice<f32>, to: slice<f32>, out: slice<f32>, channels: i32): void {
|
||
|
|
for c in 0..channels {
|
||
|
|
for i in 0..f2_5 {
|
||
|
|
val w := window120[i as u64] * window120[i as u64];
|
||
|
|
val at := (i * channels + c) as u64;
|
||
|
|
out[at] = w * to[at] + (1 as f32 - w) * from[at];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Decodes a frame, or, where it's lost, conceals one: libopus's
|
||
|
|
# `opus_decode_frame`.
|
||
|
|
# @param given The frame, or nothing where `present` is false.
|
||
|
|
# @param pcm Where its frames go, `channels` floats each.
|
||
|
|
# @param wanted How many frames it may give.
|
||
|
|
# @returns How many it gave, or -1 where it's malformed.
|
||
|
|
def frames_state::decode_frame(&mut this, given: span<u8>, given_present: bool, pcm: slice<f32>, wanted: i32): i32 {
|
||
|
|
var frame_size := math::min(wanted, 2880);
|
||
|
|
var len := given.length as i32;
|
||
|
|
var present := given_present;
|
||
|
|
if len <= 1 {
|
||
|
|
present = false;
|
||
|
|
frame_size = math::min(frame_size, this.frame_size);
|
||
|
|
}
|
||
|
|
var audiosize := frame_size;
|
||
|
|
var mode := this.prev_redundancy ? mode_celt : this.prev_mode;
|
||
|
|
var bandwidth: i32 = 0;
|
||
|
|
var rd := range_decoder::of(given);
|
||
|
|
if present {
|
||
|
|
audiosize = this.frame_size;
|
||
|
|
mode = this.mode;
|
||
|
|
bandwidth = this.bandwidth;
|
||
|
|
} else {
|
||
|
|
if mode == 0 {
|
||
|
|
for i in 0..(audiosize * this.channels) as u64 {
|
||
|
|
pcm[i] = 0;
|
||
|
|
}
|
||
|
|
return audiosize;
|
||
|
|
}
|
||
|
|
# Concealed 20 ms at most at a time.
|
||
|
|
if audiosize > f20 {
|
||
|
|
var done: i32 = 0;
|
||
|
|
while done < audiosize {
|
||
|
|
val got := this.decode_frame(span::<u8>::blank(), false, sub(pcm, (done * this.channels) as u64, ((audiosize - done) * this.channels) as u64), math::min(audiosize - done, f20));
|
||
|
|
return got if got < 0;
|
||
|
|
done += got;
|
||
|
|
}
|
||
|
|
return frame_size;
|
||
|
|
}
|
||
|
|
if audiosize < f20 {
|
||
|
|
if audiosize > f10 {
|
||
|
|
audiosize = f10;
|
||
|
|
} else if mode != mode_silk && audiosize > f5 && audiosize < f10 {
|
||
|
|
audiosize = f5;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
val channels := this.channels;
|
||
|
|
|
||
|
|
var transition := false;
|
||
|
|
if present && this.prev_mode > 0 && ((mode == mode_celt && this.prev_mode != mode_celt && !this.prev_redundancy) || (mode != mode_celt && this.prev_mode == mode_celt)) {
|
||
|
|
transition = true;
|
||
|
|
}
|
||
|
|
val transition_pcm := slice::<f32>::over(this.transition.data, (f5 * channels) as u64);
|
||
|
|
if transition && mode == mode_celt {
|
||
|
|
_ = this.decode_frame(span::<u8>::blank(), false, transition_pcm, math::min(f5, audiosize));
|
||
|
|
}
|
||
|
|
return -1 if audiosize > frame_size;
|
||
|
|
frame_size = audiosize;
|
||
|
|
|
||
|
|
# SILK's frames.
|
||
|
|
if mode != mode_celt {
|
||
|
|
if this.prev_mode == mode_celt {
|
||
|
|
this.silk.reset();
|
||
|
|
}
|
||
|
|
val payload_ms := math::max(10, 1000 * audiosize / 48000);
|
||
|
|
val internal_rate: i32 = mode == mode_silk && bandwidth == band_nb ? 8000 : mode == mode_silk && bandwidth == band_mb ? 12000 : 16000;
|
||
|
|
var decoded: i32 = 0;
|
||
|
|
while decoded < frame_size {
|
||
|
|
val out := slice::<i16>::over(this.silk_pcm.data + (decoded * channels) as u64, ((frame_size - decoded) * channels) as u64);
|
||
|
|
var got := this.silk.decode(&mut rd, !present, decoded == 0, this.stream_channels, internal_rate, payload_ms, channels, out);
|
||
|
|
if got < 0 {
|
||
|
|
return -1 if present;
|
||
|
|
# Concealment failing isn't fatal.
|
||
|
|
got = frame_size;
|
||
|
|
for i in 0..(frame_size * channels) as u64 {
|
||
|
|
out[i] = 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
decoded += got;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
var start_band: i32 = 0;
|
||
|
|
var redundancy := false;
|
||
|
|
var celt_to_silk := false;
|
||
|
|
var redundancy_bytes: i32 = 0;
|
||
|
|
if mode != mode_celt && present && rd.tell() + 17 + (mode == mode_hybrid ? 20 : 0) <= 8 * len {
|
||
|
|
# Whether there's a redundant frame of CELT's, of the band to 8 kHz.
|
||
|
|
redundancy = mode == mode_hybrid ? rd.bit_logp(12) : true;
|
||
|
|
if redundancy {
|
||
|
|
celt_to_silk = rd.bit_logp(1);
|
||
|
|
redundancy_bytes = mode == mode_hybrid ? rd.uint(256) as i32 + 2 : len - ((rd.tell() + 7) >> 3);
|
||
|
|
len -= redundancy_bytes;
|
||
|
|
if len * 8 < rd.tell() {
|
||
|
|
len = 0;
|
||
|
|
redundancy_bytes = 0;
|
||
|
|
redundancy = false;
|
||
|
|
}
|
||
|
|
rd.storage -= redundancy_bytes as u32;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if mode != mode_celt {
|
||
|
|
start_band = 17;
|
||
|
|
}
|
||
|
|
if redundancy {
|
||
|
|
transition = false;
|
||
|
|
}
|
||
|
|
if transition && mode != mode_celt {
|
||
|
|
_ = this.decode_frame(span::<u8>::blank(), false, transition_pcm, math::min(f5, audiosize));
|
||
|
|
}
|
||
|
|
|
||
|
|
if bandwidth != 0 {
|
||
|
|
this.celt.end = bandwidth == band_nb ? 13 : bandwidth == band_mb || bandwidth == band_wb ? 17 : bandwidth == band_swb ? 19 : 21;
|
||
|
|
}
|
||
|
|
this.celt.stream_channels = this.stream_channels;
|
||
|
|
val redundant := slice::<f32>::over(this.redundant.data, (f5 * channels) as u64);
|
||
|
|
if redundancy && celt_to_silk {
|
||
|
|
this.celt.start = 0;
|
||
|
|
var own := range_decoder::of(given[len as u64:redundancy_bytes as u64]);
|
||
|
|
_ = this.celt.decode(&mut own, redundancy_bytes, redundant, f5);
|
||
|
|
}
|
||
|
|
this.celt.start = start_band;
|
||
|
|
|
||
|
|
if mode != mode_silk {
|
||
|
|
val celt_frame_size := math::min(f20, frame_size);
|
||
|
|
if mode != this.prev_mode && this.prev_mode > 0 && !this.prev_redundancy {
|
||
|
|
this.celt.reset();
|
||
|
|
}
|
||
|
|
_ = this.celt.decode(&mut rd, present ? len : 0, pcm, celt_frame_size);
|
||
|
|
} else {
|
||
|
|
for i in 0..(frame_size * channels) as u64 {
|
||
|
|
pcm[i] = 0;
|
||
|
|
}
|
||
|
|
# Leaving hybrid, CELT fades out by a frame of silence.
|
||
|
|
if this.prev_mode == mode_hybrid && !(redundancy && celt_to_silk && this.prev_redundancy) {
|
||
|
|
this.celt.start = 0;
|
||
|
|
val silence: u8[2] = [0xFF, 0xFF];
|
||
|
|
var quiet := range_decoder::of(silence[0:2]);
|
||
|
|
_ = this.celt.decode(&mut quiet, 2, pcm, f2_5);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if mode != mode_celt {
|
||
|
|
for i in 0..(frame_size * channels) as u64 {
|
||
|
|
pcm[i] = pcm[i] + (1 as f32 / 32768) * this.silk_pcm[i] as f32;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# CELT's redundant frame at a move from SILK to it.
|
||
|
|
if redundancy && !celt_to_silk {
|
||
|
|
this.celt.reset();
|
||
|
|
this.celt.start = 0;
|
||
|
|
var own := range_decoder::of(given[len as u64:redundancy_bytes as u64]);
|
||
|
|
_ = this.celt.decode(&mut own, redundancy_bytes, redundant, f5);
|
||
|
|
val end := sub(pcm, ((frame_size - f2_5) * channels) as u64, (f2_5 * channels) as u64);
|
||
|
|
smooth_fade(end, sub(redundant, (f2_5 * channels) as u64, (f2_5 * channels) as u64), end, channels);
|
||
|
|
}
|
||
|
|
# And at a move from it to SILK.
|
||
|
|
if redundancy && celt_to_silk && (this.prev_mode != mode_silk || this.prev_redundancy) {
|
||
|
|
for i in 0..(f2_5 * channels) as u64 {
|
||
|
|
pcm[i] = redundant[i];
|
||
|
|
}
|
||
|
|
val rest := sub(pcm, (f2_5 * channels) as u64, (f2_5 * channels) as u64);
|
||
|
|
smooth_fade(sub(redundant, (f2_5 * channels) as u64, (f2_5 * channels) as u64), rest, rest, channels);
|
||
|
|
}
|
||
|
|
if transition {
|
||
|
|
if audiosize >= f5 {
|
||
|
|
for i in 0..(f2_5 * channels) as u64 {
|
||
|
|
pcm[i] = transition_pcm[i];
|
||
|
|
}
|
||
|
|
val rest := sub(pcm, (f2_5 * channels) as u64, (f2_5 * channels) as u64);
|
||
|
|
smooth_fade(sub(transition_pcm, (f2_5 * channels) as u64, (f2_5 * channels) as u64), rest, rest, channels);
|
||
|
|
} else {
|
||
|
|
smooth_fade(transition_pcm, pcm, pcm, channels);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if this.gain != 1 {
|
||
|
|
for i in 0..(frame_size * channels) as u64 {
|
||
|
|
pcm[i] = pcm[i] * this.gain;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
this.prev_mode = mode;
|
||
|
|
this.prev_redundancy = redundancy && !celt_to_silk;
|
||
|
|
return audiosize;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Reads an Ogg Opus stream's frames as it's asked for them, at 48 kHz. Its
|
||
|
|
#! bytes must stay where they are while it reads them.
|
||
|
|
pub struct decoder {
|
||
|
|
var stream: packets;
|
||
|
|
var audio_at: u64; # Where the first page of audio begins.
|
||
|
|
var heard: u32; # The channels: one, or two.
|
||
|
|
var pre_skip: u64; # The frames decoded first, then cut.
|
||
|
|
var state: frames_state;
|
||
|
|
var made: vector<f32>; # The frames a packet gave.
|
||
|
|
var pending: vector<f32>; # Frames decoded and not yet read, a frame's together.
|
||
|
|
var taken: u64; # How many floats of `pending` were read.
|
||
|
|
var timed: bool; # Whether a granule position was met, so frames are where it says.
|
||
|
|
var clock: u64; # The granule position of the end of what was decoded.
|
||
|
|
var total: u64; # The granule position of the stream's end.
|
||
|
|
var from: u64; # The granule position a seek asked for, before which what's decoded is cut.
|
||
|
|
var looped: loop_range; # Where its comments say it loops.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Opens an Ogg Opus stream, reading its headers.
|
||
|
|
#! @param bytes The file's bytes, which must stay where they are while the
|
||
|
|
#! decoder reads them.
|
||
|
|
#! @returns The decoder, or why the stream can't be read: `unrecognized` where
|
||
|
|
#! it's no Ogg file or has no Opus stream, `malformed` where a header
|
||
|
|
#! or page is broken, and `unsupported` where it's of a later
|
||
|
|
#! version, or more than two channels.
|
||
|
|
pub def decoder::open(bytes: span<u8>): result<decoder, audio_error> {
|
||
|
|
# The first stream whose first page begins with Opus's identification.
|
||
|
|
var at: u64 = 0;
|
||
|
|
var serial: u32 = 0;
|
||
|
|
var found := false;
|
||
|
|
while !found {
|
||
|
|
return audio_error::unrecognized if at >= bytes.length;
|
||
|
|
val first := ogg::page_at(bytes, at) else (e) { return e; };
|
||
|
|
return audio_error::unrecognized if (first.flags & ogg::first_page) == 0;
|
||
|
|
if is_opus_head(bytes[first.body:first.length]) {
|
||
|
|
serial = first.serial;
|
||
|
|
found = true;
|
||
|
|
} else {
|
||
|
|
at = first.body + first.length;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
var stream := ogg::packets::of(bytes, serial, at);
|
||
|
|
var next := ogg::packet{};
|
||
|
|
var had := stream.next(&mut next) else (e) { return e; };
|
||
|
|
return audio_error::malformed if !had || next.data.length < 19;
|
||
|
|
val head := next.data;
|
||
|
|
return audio_error::unsupported if (head[8] & 0xF0) != 0;
|
||
|
|
val channels := head[9] as u32;
|
||
|
|
return audio_error::malformed if channels == 0;
|
||
|
|
return audio_error::unsupported if channels > 2 || head[18] != 0;
|
||
|
|
val pre_skip := head[10] as u64 | (head[11] as u64) << 8;
|
||
|
|
var gain := head[16] as i32 | (head[17] as i32) << 8;
|
||
|
|
if gain >= 32768 {
|
||
|
|
gain -= 65536;
|
||
|
|
}
|
||
|
|
|
||
|
|
had = stream.next(&mut next) else (e) { return e; };
|
||
|
|
return audio_error::malformed if !had || next.data.length < 16 || !has_magic(next.data, "OpusTags");
|
||
|
|
val looped := audio::loop_of_comments(next.data[8:next.data.length - 8]);
|
||
|
|
# Audio begins on a page of its own.
|
||
|
|
return audio_error::malformed if stream.segment != stream.current.segments;
|
||
|
|
|
||
|
|
var made := decoder{
|
||
|
|
stream = move(&mut stream), audio_at = 0, heard = channels, pre_skip = pre_skip,
|
||
|
|
state = frames_state::new(channels as i32, gain), made = vector::<f32>::new(), pending = vector::<f32>::new(),
|
||
|
|
looped = looped,
|
||
|
|
};
|
||
|
|
made.audio_at = made.stream.at;
|
||
|
|
made.made.resize(most_packet_frames * 2);
|
||
|
|
made.total = ogg::last_granule(bytes, serial);
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
def has_magic(data: span<u8>, magic: strview): bool {
|
||
|
|
return false if data.length < magic.length;
|
||
|
|
for i in 0..magic.length {
|
||
|
|
return false if data[i] != magic[i];
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Whether a packet is Opus's identification header.
|
||
|
|
def is_opus_head(data: span<u8>): bool {
|
||
|
|
return data.length >= 19 && has_magic(data, "OpusHead");
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many channels the frames read have: one, or two.
|
||
|
|
pub val decoder::channels: u32 {
|
||
|
|
get => this.heard;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Frames a second: 48000, at which Opus is always decoded.
|
||
|
|
pub val decoder::rate: u32 {
|
||
|
|
get => 48000;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many frames the stream has, as its last page says, less its pre-skip,
|
||
|
|
#! or 0 where it says nothing.
|
||
|
|
pub val decoder::frames: u64 {
|
||
|
|
get => this.total > this.pre_skip ? this.total - this.pre_skip : 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Where the stream says a voice playing it loops, as its comments say.
|
||
|
|
pub val decoder::loop_points: loop_range {
|
||
|
|
get => this.looped;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Reads frames.
|
||
|
|
#! @param into Where they go, a frame's `channels` floats together.
|
||
|
|
#! @returns How many frames were read: fewer than fit only once the stream
|
||
|
|
#! ends; or why the stream couldn't be read.
|
||
|
|
pub def decoder::read(&mut this, into: slice<f32>): result<u64, audio_error> {
|
||
|
|
val width := this.heard as u64;
|
||
|
|
val wanted := into.length / width;
|
||
|
|
val cut := math::max(this.from, this.pre_skip);
|
||
|
|
var given: u64 = 0;
|
||
|
|
while given < wanted {
|
||
|
|
val held := (this.pending.length - this.taken) / width;
|
||
|
|
# What the pre-skip, or a seek, passes, once it's known where it is.
|
||
|
|
if held > 0 && this.timed && this.clock - held < cut {
|
||
|
|
val before := cut - (this.clock - held);
|
||
|
|
this.taken += (before < held ? before : held) * width;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if held > 0 && (this.timed || this.stream.done) {
|
||
|
|
val count := held < wanted - given ? held : wanted - given;
|
||
|
|
for i in 0..count * width {
|
||
|
|
into[given * width + i] = this.pending[this.taken + i];
|
||
|
|
}
|
||
|
|
this.taken += count * width;
|
||
|
|
given += count;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if this.taken == this.pending.length {
|
||
|
|
this.pending.clear();
|
||
|
|
this.taken = 0;
|
||
|
|
}
|
||
|
|
var next := ogg::packet{};
|
||
|
|
val had := this.stream.next(&mut next) else (e) { return e; };
|
||
|
|
if !had {
|
||
|
|
# The stream ended before any page said where its frames are.
|
||
|
|
this.timed = true;
|
||
|
|
break if this.pending.length == this.taken;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
val count := this.state.decode_packet(next.data, slice::<f32>::over(this.made.data, this.made.length));
|
||
|
|
return audio_error::malformed if count < 0;
|
||
|
|
this.pending.extend(span::<f32>::over(this.made.data, count as u64 * width));
|
||
|
|
this.time(count as u64, next.granule, next.last);
|
||
|
|
}
|
||
|
|
return given;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Reads the stream again from its first frame.
|
||
|
|
pub def decoder::rewind(&mut this): void {
|
||
|
|
this.stream.rewind(this.audio_at);
|
||
|
|
this.state.reset();
|
||
|
|
this.pending.clear();
|
||
|
|
this.taken = 0;
|
||
|
|
this.timed = false;
|
||
|
|
this.clock = 0;
|
||
|
|
this.from = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Reads on from a frame of the stream: from the page halving its pages finds
|
||
|
|
#! half a second or more before it, the frames between decoded and left out,
|
||
|
|
#! so the decoder has settled by the frame, to within a few ten-thousandths of
|
||
|
|
#! what decoding straight through gives.
|
||
|
|
#! @param frame The frame, past the end where the stream ends there.
|
||
|
|
#! @returns Nothing, or why the stream couldn't be read.
|
||
|
|
pub def decoder::seek(&mut this, frame: u64): result<void, audio_error> {
|
||
|
|
this.rewind();
|
||
|
|
this.from = frame + this.pre_skip;
|
||
|
|
return if this.from <= preroll;
|
||
|
|
this.stream.rewind(ogg::resume_at(this.stream.bytes, this.stream.serial, this.audio_at, this.from - preroll));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Keeps the frames a packet gave where the granule positions say: those before
|
||
|
|
# the first page's position cut from the start, and those past the last page's
|
||
|
|
# from the end.
|
||
|
|
def decoder::time(&mut this, count: u64, granule: u64, last: bool): void {
|
||
|
|
val width := this.heard as u64;
|
||
|
|
if !this.timed {
|
||
|
|
return if granule == ogg::no_granule;
|
||
|
|
val decoded := (this.pending.length - this.taken) / width;
|
||
|
|
this.timed = true;
|
||
|
|
this.clock = granule;
|
||
|
|
if decoded > granule {
|
||
|
|
val extra := (decoded - granule) * width;
|
||
|
|
if last {
|
||
|
|
this.pending.resize(this.pending.length - extra);
|
||
|
|
} else {
|
||
|
|
this.taken += extra;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.clock += count;
|
||
|
|
if last && granule != ogg::no_granule && this.clock > granule {
|
||
|
|
val held := (this.pending.length - this.taken) / width;
|
||
|
|
val extra := this.clock - granule < held ? this.clock - granule : held;
|
||
|
|
this.pending.resize(this.pending.length - extra * width);
|
||
|
|
this.clock = granule;
|
||
|
|
} else if granule != ogg::no_granule {
|
||
|
|
this.clock = granule;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Reads an Ogg Opus file whole.
|
||
|
|
#! @param bytes The file's bytes.
|
||
|
|
#! @returns The sound, at 48 kHz, or why it couldn't be read: as
|
||
|
|
#! `decoder::open` and `decoder::read` say, and `too_large` where it
|
||
|
|
#! has more frames than `max_frames`.
|
||
|
|
pub def decode(bytes: span<u8>): result<sound, audio_error> {
|
||
|
|
var reading := decoder::open(bytes) else (e) { return e; };
|
||
|
|
return audio_error::too_large if reading.frames > max_frames;
|
||
|
|
val width := reading.channels as u64;
|
||
|
|
var samples := vector::<f32>::new();
|
||
|
|
samples.reserve(reading.frames * width);
|
||
|
|
var chunk := vector::<f32>::new();
|
||
|
|
chunk.resize(4096 * width);
|
||
|
|
while true {
|
||
|
|
val count := reading.read(chunk.slice) else (e) { return e; };
|
||
|
|
break if count == 0;
|
||
|
|
samples.extend(span::<f32>::over(chunk.data, count * width));
|
||
|
|
return audio_error::too_large if samples.length / width > max_frames;
|
||
|
|
}
|
||
|
|
return sound{ rate = 48000, channels = reading.channels, samples = move(samples), loop_points = reading.loop_points };
|
||
|
|
}
|