ec-opus: SILK packet loss concealment (port of libopus silk/PLC.c: conceal, update, glue frames, BWE after loss, voiced->unvoiced transition); Decoder::conceal, decode_fec without LBRR and DTX frames now use it for SILK/hybrid; SilkDecoder::decode takes SilkFrameMode {Normal, Lbrr, Lost}
Browse files at this commit86fb0f3d0c47e6aff7ce60ab0d4653cbde35f00aParent: 90e21f26 changed files +507 -74
- crates/ec-opus/src/lib.rs +81 -22
- crates/ec-opus/src/silk.rs +412 -42
- crates/ec-opus/src/silk_enc_write.rs +3 -3
- crates/ec-opus/src/stack_budget.rs +5 -5
- crates/ec-opus/tests/conformance.rs +1 -1
- crates/ec-opus/tests/plc.rs +5 -1
crates/ec-opus/src/lib.rsmodified+81 -22
| @@ -21,12 +21,14 @@ | ||
| 21 | 21 | //! - **Output is `f32`, interleaved, at the rate you asked for.** 48, 24, 16, |
| 22 | 22 | //! 12 and 8 kHz are supported; SILK's internal rate is resampled up (by the |
| 23 | 23 | //! normative resampler in [`silk`]) and CELT decimates on the way out. |
| 24 | -//! - **Packet loss concealment is CELT-only.** [`Decoder::conceal`] (or an | |
| 25 | -//! empty packet to [`Decoder::decode_float`]) extends the last CELT frame: | |
| 24 | +//! - **Packet loss concealment.** [`Decoder::conceal`] (or an empty packet to | |
| 25 | +//! [`Decoder::decode_float`]) extends the last frame: a CELT frame gets | |
| 26 | 26 | //! pitch-periodic LPC extension for the first losses, then decaying |
| 27 | -//! band-shaped noise. After a SILK or hybrid frame it yields silence, as does | |
| 28 | -//! a one-byte (DTX) frame inside a packet. [`Decoder::decode_fec`] recovers | |
| 29 | -//! a lost SILK or hybrid frame from the in-band FEC of the next packet. | |
| 27 | +//! band-shaped noise; a SILK or hybrid frame is concealed by the SILK PLC (a | |
| 28 | +//! port of libopus `silk/PLC.c`, without comfort noise). A one-byte (DTX) | |
| 29 | +//! frame inside a packet is concealed the same way. [`Decoder::decode_fec`] | |
| 30 | +//! recovers a lost SILK or hybrid frame from the in-band FEC of the next | |
| 31 | +//! packet. | |
| 30 | 32 | //! |
| 31 | 33 | //! The crate also carries the family's Opus *encoder* — [`Encoder`] for one |
| 32 | 34 | //! stream, [`MultistreamEncoder`] for surround — CELT-only, fullband, mono |
| @@ -83,7 +85,7 @@ pub use multistream::MultistreamDecoder; | ||
| 83 | 85 | pub use multistream_enc::MultistreamEncoder; |
| 84 | 86 | pub use packet::{Bandwidth, Mode, Packet, Toc}; |
| 85 | 87 | pub use range::{RangeDecoder, RangeEncoder}; |
| 86 | -pub use silk::{SilkDecIndices, SilkDecoder}; | |
| 88 | +pub use silk::{SilkDecIndices, SilkDecoder, SilkFrameMode}; | |
| 87 | 89 | pub use silk_enc_write::SilkFrameDiag; |
| 88 | 90 | pub use silk_enc_write::{SilkEncoder, SilkStereoEncoder}; |
| 89 | 91 | |
| @@ -101,7 +103,7 @@ pub struct Decoder { | ||
| 101 | 103 | channels: usize, |
| 102 | 104 | downsample: usize, |
| 103 | 105 | celt: CeltDecoder, |
| 104 | - /// Boxed because a `SilkDecoder` is 6304 bytes, 83% of what a `Decoder` | |
| 106 | + /// Boxed because a `SilkDecoder` is 6496 bytes, 83% of what a `Decoder` | |
| 105 | 107 | /// costs a caller taking one BY VALUE from `Decoder::new`. It is the |
| 106 | 108 | /// decoder's largest field and its only large one; `celt` is 1448. |
| 107 | 109 | /// `Stack`: crate::stack_budget. |
| @@ -111,6 +113,10 @@ pub struct Decoder { | ||
| 111 | 113 | /// Per-frame scratch, allocated once: the SILK layer's output and the |
| 112 | 114 | /// redundancy frame. |
| 113 | 115 | silk_pcm: Vec<i16>, |
| 116 | + /// The internal SILK rate and channel count of the last SILK frame, which | |
| 117 | + /// the SILK concealment reuses. | |
| 118 | + silk_rate: u32, | |
| 119 | + silk_channels: usize, | |
| 114 | 120 | redundant: Vec<f32>, |
| 115 | 121 | prev_mode: Option<Mode>, |
| 116 | 122 | prev_redundancy: bool, |
| @@ -171,6 +177,8 @@ impl Decoder { | ||
| 171 | 177 | silk: Box::new(SilkDecoder::new(sample_rate, channels)), |
| 172 | 178 | celt_window: celt::overlap_window(), |
| 173 | 179 | silk_pcm: vec![0; 5760 * channels], |
| 180 | + silk_rate: 16000, | |
| 181 | + silk_channels: 1, | |
| 174 | 182 | redundant: vec![0.0; 240 * channels], |
| 175 | 183 | prev_mode: None, |
| 176 | 184 | prev_redundancy: false, |
| @@ -233,8 +241,9 @@ impl Decoder { | ||
| 233 | 241 | |
| 234 | 242 | /// Conceals `frame_size` lost samples per channel (a multiple of 2.5 ms, |
| 235 | 243 | /// at most 120 ms), the equivalent of the reference's `decode(NULL)`. |
| 236 | - /// Consecutive calls decay towards background noise; the next real | |
| 237 | - /// packet cross-fades in through the MDCT overlap. | |
| 244 | + /// CELT output decays towards background noise and SILK output is | |
| 245 | + /// extrapolated by the SILK PLC; the next real packet cross-fades in | |
| 246 | + /// through the MDCT overlap. | |
| 238 | 247 | pub fn conceal(&mut self, frame_size: usize, out: &mut [f32]) -> Result<usize> { |
| 239 | 248 | let total_48k = frame_size * self.downsample; |
| 240 | 249 | if total_48k == 0 || !total_48k.is_multiple_of(120) || total_48k > 5760 { |
| @@ -252,17 +261,22 @@ impl Decoder { | ||
| 252 | 261 | let mut done_48k = 0usize; |
| 253 | 262 | while done_48k < total_48k { |
| 254 | 263 | let left = total_48k - done_48k; |
| 255 | - let chunk = [960, 480, 240, 120] | |
| 256 | - .into_iter() | |
| 257 | - .find(|&c| c <= left) | |
| 258 | - .unwrap_or(120); | |
| 264 | + let sizes: &[usize] = match self.prev_mode { | |
| 265 | + Some(Mode::Silk | Mode::Hybrid) => &[960, 480], | |
| 266 | + _ => &[960, 480, 240, 120], | |
| 267 | + }; | |
| 268 | + let chunk = sizes.iter().copied().find(|&c| c <= left).unwrap_or(120); | |
| 259 | 269 | let a = done_48k / self.downsample * self.channels; |
| 260 | 270 | let b = (done_48k + chunk) / self.downsample * self.channels; |
| 261 | 271 | let dst = &mut out[a..b]; |
| 262 | - if self.prev_mode == Some(Mode::Celt) { | |
| 263 | - self.celt.conceal(dst, chunk)?; | |
| 264 | - } else { | |
| 265 | - dst.fill(0.0); | |
| 272 | + match self.prev_mode { | |
| 273 | + Some(Mode::Celt) => { | |
| 274 | + self.celt.conceal(dst, chunk)?; | |
| 275 | + } | |
| 276 | + Some(Mode::Silk | Mode::Hybrid) if chunk >= 480 => { | |
| 277 | + self.conceal_silk(dst, chunk)?; | |
| 278 | + } | |
| 279 | + _ => dst.fill(0.0), | |
| 266 | 280 | } |
| 267 | 281 | done_48k += chunk; |
| 268 | 282 | } |
| @@ -362,10 +376,11 @@ impl Decoder { | ||
| 362 | 376 | let mode = toc.mode(); |
| 363 | 377 | let stream_channels = toc.channels(); |
| 364 | 378 | if data.len() <= 1 { |
| 365 | - // DTX or a dropped frame. There is no PLC here (the container | |
| 366 | - // layer feeds this decoder whole packets), so the frame is silent. | |
| 367 | - out.fill(0.0); | |
| 368 | - self.prev_mode = None; | |
| 379 | + // DTX or a dropped frame: conceal it like a lost one. | |
| 380 | + let n = out.len() / self.channels; | |
| 381 | + if self.conceal(n, out).is_err() { | |
| 382 | + out.fill(0.0); | |
| 383 | + } | |
| 369 | 384 | return Ok(0); |
| 370 | 385 | } |
| 371 | 386 | let mut dec = RangeDecoder::new(data); |
| @@ -391,7 +406,14 @@ impl Decoder { | ||
| 391 | 406 | _ => 16000, |
| 392 | 407 | } |
| 393 | 408 | }; |
| 409 | + self.silk_rate = internal_rate; | |
| 410 | + self.silk_channels = stream_channels; | |
| 394 | 411 | let payload_ms = (frame_48k / 48).max(10); |
| 412 | + let mode = if fec { | |
| 413 | + SilkFrameMode::Lbrr | |
| 414 | + } else { | |
| 415 | + SilkFrameMode::Normal | |
| 416 | + }; | |
| 395 | 417 | let mut done = 0usize; |
| 396 | 418 | let mut first = true; |
| 397 | 419 | while done < frame_out { |
| @@ -402,7 +424,7 @@ impl Decoder { | ||
| 402 | 424 | internal_rate, |
| 403 | 425 | stream_channels, |
| 404 | 426 | first, |
| 405 | - fec, | |
| 427 | + mode, | |
| 406 | 428 | )?; |
| 407 | 429 | if n == 0 { |
| 408 | 430 | break; |
| @@ -553,6 +575,43 @@ impl Decoder { | ||
| 553 | 575 | } |
| 554 | 576 | } |
| 555 | 577 | |
| 578 | +impl Decoder { | |
| 579 | + /// Conceals `dst` (one chunk of 10 or 20 ms) with the SILK PLC, on top of | |
| 580 | + /// the CELT concealment in hybrid mode. | |
| 581 | + fn conceal_silk(&mut self, dst: &mut [f32], chunk: usize) -> Result<()> { | |
| 582 | + if self.prev_mode == Some(Mode::Hybrid) { | |
| 583 | + self.celt.conceal(dst, chunk)?; | |
| 584 | + } else { | |
| 585 | + dst.fill(0.0); | |
| 586 | + } | |
| 587 | + let mut pcm = core::mem::take(&mut self.silk_pcm); | |
| 588 | + let samples = chunk / self.downsample; | |
| 589 | + pcm.clear(); | |
| 590 | + pcm.resize(samples * self.channels, 0); | |
| 591 | + let mut done = 0usize; | |
| 592 | + while done < samples { | |
| 593 | + let n = self.silk.decode( | |
| 594 | + &mut RangeDecoder::new(&[]), | |
| 595 | + &mut pcm[done * self.channels..], | |
| 596 | + chunk / 48, | |
| 597 | + self.silk_rate, | |
| 598 | + self.silk_channels, | |
| 599 | + true, | |
| 600 | + SilkFrameMode::Lost, | |
| 601 | + )?; | |
| 602 | + if n == 0 { | |
| 603 | + break; | |
| 604 | + } | |
| 605 | + done += n; | |
| 606 | + } | |
| 607 | + for (o, s) in dst.iter_mut().zip(pcm.iter()) { | |
| 608 | + *o += *s as f32 * (1.0 / 32768.0); | |
| 609 | + } | |
| 610 | + self.silk_pcm = pcm; | |
| 611 | + Ok(()) | |
| 612 | + } | |
| 613 | +} | |
| 614 | + | |
| 556 | 615 | /// Cross-fades `b` over `a` across `overlap` samples using the CELT window, |
| 557 | 616 | /// the shape the reference uses for redundancy and mode transitions. |
| 558 | 617 | fn smooth_fade( |
crates/ec-opus/src/silk.rsmodified+412 -42
| @@ -12,10 +12,9 @@ | ||
| 12 | 12 | //! written as fixed-point pseudo-code for the same reason). The tables are the |
| 13 | 13 | //! ones the RFC tabulates in Section 4.2, in the layout the reference uses. |
| 14 | 14 | //! |
| 15 | -//! LBRR (in-band FEC) frames can be decoded through the `lbrr` flag of | |
| 16 | -//! [`SilkDecoder::decode`], but SILK packet loss concealment is not implemented: | |
| 17 | -//! a lost or empty frame yields silence rather than an extrapolation. | |
| 18 | -//! Everything else in the layer is complete. | |
| 15 | +//! LBRR (in-band FEC) frames are decoded through [`SilkFrameMode::Lbrr`], and | |
| 16 | +//! lost frames are concealed by the packet loss concealment of the reference | |
| 17 | +//! (`silk/PLC.c`), selected with [`SilkFrameMode::Lost`]. | |
| 19 | 18 | |
| 20 | 19 | // SILK is transliterated fixed-point arithmetic: the loops below index parallel |
| 21 | 20 | // arrays by position because the reference's index arithmetic is the |
| @@ -54,6 +53,21 @@ pub(crate) const PE_MAX_LAG_MS: i32 = 18; | ||
| 54 | 53 | const STEREO_INTERP_LEN_MS: usize = 8; |
| 55 | 54 | const TYPE_VOICED: i32 = 2; |
| 56 | 55 | const MAX_LPC_STABILIZE_ITERATIONS: usize = 16; |
| 56 | +const BWE_COEF_Q16: i32 = 64881; | |
| 57 | +const BWE_AFTER_LOSS_Q16: i32 = 63570; | |
| 58 | +const V_PITCH_GAIN_START_MIN_Q14: i32 = 11469; | |
| 59 | +const V_PITCH_GAIN_START_MAX_Q14: i32 = 15565; | |
| 60 | +const RAND_BUF_SIZE: usize = 128; | |
| 61 | +const RAND_BUF_MASK: i32 = 127; | |
| 62 | +const LOG2_INV_LPC_GAIN_HIGH_THRES: i32 = 3; | |
| 63 | +const LOG2_INV_LPC_GAIN_LOW_THRES: i32 = 8; | |
| 64 | +const PITCH_DRIFT_FAC_Q16: i32 = 655; | |
| 65 | +const MAX_PITCH_LAG_MS: i32 = 18; | |
| 66 | +const HARM_ATT_Q15: [i32; NB_ATT] = [32440, 31130]; | |
| 67 | +const PLC_RAND_ATTENUATE_V_Q15: [i32; NB_ATT] = [31130, 26214]; | |
| 68 | +const PLC_RAND_ATTENUATE_UV_Q15: [i32; NB_ATT] = [32440, 29491]; | |
| 69 | +const NB_ATT: usize = 2; | |
| 70 | +const MAX_LTP_MEM: usize = 20 * 16; | |
| 57 | 71 | |
| 58 | 72 | /// Gain quantiser constants (`gain_quant.c`). |
| 59 | 73 | const GAIN_OFFSET: i32 = (MIN_QGAIN_DB * 128) / 6 + 16 * 128; |
| @@ -68,6 +82,27 @@ enum CondCoding { | ||
| 68 | 82 | Conditionally, |
| 69 | 83 | } |
| 70 | 84 | |
| 85 | +/// What `SilkDecoder::decode` reads for a frame. | |
| 86 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 87 | +pub enum SilkFrameMode { | |
| 88 | + /// Regular frames, read from the range decoder. | |
| 89 | + Normal, | |
| 90 | + /// The packet's LBRR (in-band FEC) frames, where coded. | |
| 91 | + Lbrr, | |
| 92 | + /// No data for this frame: conceal it (PLC). | |
| 93 | + Lost, | |
| 94 | +} | |
| 95 | + | |
| 96 | +impl SilkFrameMode { | |
| 97 | + fn coded(self, lbrr_flag: bool) -> bool { | |
| 98 | + match self { | |
| 99 | + SilkFrameMode::Normal => true, | |
| 100 | + SilkFrameMode::Lbrr => lbrr_flag, | |
| 101 | + SilkFrameMode::Lost => false, | |
| 102 | + } | |
| 103 | + } | |
| 104 | +} | |
| 105 | + | |
| 71 | 106 | // --------------------------------------------------------------------------- |
| 72 | 107 | // Fixed-point helpers. These are the reference's macros; they are written out |
| 73 | 108 | // because SILK's output is defined by them bit for bit. |
| @@ -305,6 +340,24 @@ impl Default for FrameCtrl { | ||
| 305 | 340 | } |
| 306 | 341 | } |
| 307 | 342 | |
| 343 | +/// Packet loss concealment state (`silk_PLC_struct`). | |
| 344 | +#[derive(Clone, Debug, Default)] | |
| 345 | +struct PlcState { | |
| 346 | + pitch_l_q8: i32, | |
| 347 | + ltp_coef_q14: [i16; LTP_ORDER], | |
| 348 | + prev_lpc_q12: [i16; MAX_LPC_ORDER], | |
| 349 | + last_frame_lost: bool, | |
| 350 | + rand_seed: i32, | |
| 351 | + rand_scale_q14: i16, | |
| 352 | + conc_energy: i32, | |
| 353 | + conc_energy_shift: i32, | |
| 354 | + prev_ltp_scale_q14: i16, | |
| 355 | + prev_gain_q16: [i32; 2], | |
| 356 | + fs_khz: usize, | |
| 357 | + nb_subfr: usize, | |
| 358 | + subfr_length: usize, | |
| 359 | +} | |
| 360 | + | |
| 308 | 361 | /// One SILK channel's inter-frame state. |
| 309 | 362 | #[derive(Clone, Debug)] |
| 310 | 363 | struct ChannelState { |
| @@ -335,6 +388,8 @@ struct ChannelState { | ||
| 335 | 388 | lbrr_flag: bool, |
| 336 | 389 | lbrr_flags: [bool; 3], |
| 337 | 390 | resampler: Resampler, |
| 391 | + plc: PlcState, | |
| 392 | + loss_cnt: i32, | |
| 338 | 393 | } |
| 339 | 394 | |
| 340 | 395 | impl ChannelState { |
| @@ -365,6 +420,8 @@ impl ChannelState { | ||
| 365 | 420 | lbrr_flag: false, |
| 366 | 421 | lbrr_flags: [false; 3], |
| 367 | 422 | resampler: Resampler::default(), |
| 423 | + plc: PlcState::default(), | |
| 424 | + loss_cnt: 0, | |
| 368 | 425 | } |
| 369 | 426 | } |
| 370 | 427 | |
| @@ -508,8 +565,8 @@ impl SilkDecoder { | ||
| 508 | 565 | /// `internal_rate` the SILK sample rate the TOC implies (8000, 12000 or |
| 509 | 566 | /// 16000 Hz), and `channels_internal` how many channels the packet codes. |
| 510 | 567 | /// Output is interleaved `i16` at the API rate, `channels_api` channels. |
| 511 | - /// With `lbrr` set, decodes the packet's LBRR (in-band FEC) frames instead | |
| 512 | - /// of the regular ones. | |
| 568 | + /// `mode` selects what is read: regular frames, the LBRR (in-band FEC) | |
| 569 | + /// frames, or nothing at all, in which case the frame is concealed. | |
| 513 | 570 | #[allow(clippy::too_many_arguments)] |
| 514 | 571 | pub fn decode( |
| 515 | 572 | &mut self, |
| @@ -519,8 +576,9 @@ impl SilkDecoder { | ||
| 519 | 576 | internal_rate: u32, |
| 520 | 577 | channels_internal: usize, |
| 521 | 578 | new_packet: bool, |
| 522 | - lbrr: bool, | |
| 579 | + mode: SilkFrameMode, | |
| 523 | 580 | ) -> Result<usize> { |
| 581 | + let lbrr = mode == SilkFrameMode::Lbrr; | |
| 524 | 582 | let fs_khz = (internal_rate / 1000) as usize; |
| 525 | 583 | if new_packet { |
| 526 | 584 | for ch in self.channels.iter_mut() { |
| @@ -558,7 +616,7 @@ impl SilkDecoder { | ||
| 558 | 616 | let mut decode_only_middle = false; |
| 559 | 617 | |
| 560 | 618 | // Header bits: per-frame VAD flags and the LBRR flag (Section 4.2.3). |
| 561 | - if self.channels[0].frames_decoded == 0 { | |
| 619 | + if mode != SilkFrameMode::Lost && self.channels[0].frames_decoded == 0 { | |
| 562 | 620 | for n in 0..channels_internal { |
| 563 | 621 | for i in 0..self.channels[n].frames_per_packet { |
| 564 | 622 | self.channels[n].vad_flags[i] = dec.dec_bit_logp(1); |
| @@ -619,7 +677,7 @@ impl SilkDecoder { | ||
| 619 | 677 | // Stereo prediction weights for this frame. |
| 620 | 678 | if channels_internal == 2 { |
| 621 | 679 | let frame = self.channels[0].frames_decoded; |
| 622 | - if !lbrr || self.channels[0].lbrr_flags[frame] { | |
| 680 | + if mode.coded(self.channels[0].lbrr_flags[frame]) { | |
| 623 | 681 | stereo_decode_pred(dec, &mut ms_pred_q13); |
| 624 | 682 | let side_coded = if lbrr { |
| 625 | 683 | self.channels[1].lbrr_flags[frame] |
| @@ -663,13 +721,9 @@ impl SilkDecoder { | ||
| 663 | 721 | } else { |
| 664 | 722 | CondCoding::Conditionally |
| 665 | 723 | }; |
| 666 | - if !lbrr || self.channels[n].lbrr_flags[self.channels[n].frames_decoded] { | |
| 667 | - n_samples_dec = | |
| 668 | - decode_frame(&mut self.channels[n], dec, &mut buf[2..], cond, lbrr); | |
| 669 | - } else { | |
| 670 | - n_samples_dec = self.channels[n].frame_length; | |
| 671 | - buf[2..2 + n_samples_dec].fill(0); | |
| 672 | - } | |
| 724 | + let coded = mode.coded(self.channels[n].lbrr_flags[self.channels[n].frames_decoded]); | |
| 725 | + n_samples_dec = | |
| 726 | + decode_frame(&mut self.channels[n], dec, &mut buf[2..], cond, lbrr, coded); | |
| 673 | 727 | } else { |
| 674 | 728 | buf[2..2 + n_samples_dec].fill(0); |
| 675 | 729 | } |
| @@ -692,7 +746,13 @@ impl SilkDecoder { | ||
| 692 | 746 | self.stereo.s_mid[0] = mid[n_samples_dec]; |
| 693 | 747 | self.stereo.s_mid[1] = mid[n_samples_dec + 1]; |
| 694 | 748 | } |
| 695 | - self.prev_decode_only_middle = decode_only_middle; | |
| 749 | + if mode == SilkFrameMode::Lost { | |
| 750 | + for ch in self.channels.iter_mut().take(channels_internal) { | |
| 751 | + ch.last_gain_index = 10; | |
| 752 | + } | |
| 753 | + } else { | |
| 754 | + self.prev_decode_only_middle = decode_only_middle; | |
| 755 | + } | |
| 696 | 756 | |
| 697 | 757 | let out_len = n_samples_dec * self.api_rate as usize / (self.channels[0].fs_khz * 1000); |
| 698 | 758 | let mut tmp = core::mem::take(&mut self.resample_tmp); |
| @@ -1314,6 +1374,11 @@ fn decode_parameters(ch: &mut ChannelState, cond: CondCoding) -> FrameCtrl { | ||
| 1314 | 1374 | ctrl.pred_coef_q12[0] = ctrl.pred_coef_q12[1]; |
| 1315 | 1375 | } |
| 1316 | 1376 | ch.prev_nlsf_q15[..ch.lpc_order].copy_from_slice(&nlsf_q15[..ch.lpc_order]); |
| 1377 | + if ch.loss_cnt != 0 { | |
| 1378 | + let order = ch.lpc_order; | |
| 1379 | + bwexpander(&mut ctrl.pred_coef_q12[0][..order], BWE_AFTER_LOSS_Q16); | |
| 1380 | + bwexpander(&mut ctrl.pred_coef_q12[1][..order], BWE_AFTER_LOSS_Q16); | |
| 1381 | + } | |
| 1317 | 1382 | |
| 1318 | 1383 | if ch.indices.signal_type == TYPE_VOICED { |
| 1319 | 1384 | ctrl.pitch_l = decode_pitch( |
| @@ -1338,6 +1403,289 @@ fn decode_parameters(ch: &mut ChannelState, cond: CondCoding) -> FrameCtrl { | ||
| 1338 | 1403 | ctrl |
| 1339 | 1404 | } |
| 1340 | 1405 | |
| 1406 | +/// `silk_bwexpander`: moves the LPC poles towards the origin. | |
| 1407 | +fn bwexpander(ar: &mut [i16], mut chirp_q16: i32) { | |
| 1408 | + let chirp_minus_one = chirp_q16 - 65536; | |
| 1409 | + let d = ar.len(); | |
| 1410 | + for coef in ar.iter_mut().take(d - 1) { | |
| 1411 | + *coef = rshift_round(chirp_q16.wrapping_mul(*coef as i32), 16) as i16; | |
| 1412 | + chirp_q16 += rshift_round(chirp_q16.wrapping_mul(chirp_minus_one), 16); | |
| 1413 | + } | |
| 1414 | + ar[d - 1] = rshift_round(chirp_q16.wrapping_mul(ar[d - 1] as i32), 16) as i16; | |
| 1415 | +} | |
| 1416 | + | |
| 1417 | +/// `silk_sum_sqr_shift`: energy of `x` scaled down to fit, and the shift used. | |
| 1418 | +fn sum_sqr_shift(x: &[i16]) -> (i32, i32) { | |
| 1419 | + let len = x.len(); | |
| 1420 | + let sq = |i: usize| (x[i] as i32).wrapping_mul(x[i] as i32) as u32; | |
| 1421 | + let energy = |shft: i32| { | |
| 1422 | + let mut nrg = 0u32; | |
| 1423 | + let mut i = 0; | |
| 1424 | + while i + 1 < len { | |
| 1425 | + nrg = nrg.wrapping_add((sq(i).wrapping_add(sq(i + 1))) >> shft); | |
| 1426 | + i += 2; | |
| 1427 | + } | |
| 1428 | + if i < len { | |
| 1429 | + nrg = nrg.wrapping_add(sq(i) >> shft); | |
| 1430 | + } | |
| 1431 | + nrg | |
| 1432 | + }; | |
| 1433 | + let mut shft = 31 - clz32(len as i32); | |
| 1434 | + let first = (len as u32).wrapping_add(energy(shft)); | |
| 1435 | + shft = (shft + 3 - clz32(first as i32)).max(0); | |
| 1436 | + let nrg = energy(shft); | |
| 1437 | + (nrg as i32, shft) | |
| 1438 | +} | |
| 1439 | + | |
| 1440 | +/// `silk_PLC_energy`: energies of the last two subframes' excitation. | |
| 1441 | +fn plc_energy( | |
| 1442 | + exc_q14: &[i32], | |
| 1443 | + prev_gain_q10: [i32; 2], | |
| 1444 | + subfr_length: usize, | |
| 1445 | + nb_subfr: usize, | |
| 1446 | +) -> (i32, i32, i32, i32) { | |
| 1447 | + let mut exc_buf = [0i16; 2 * 80]; | |
| 1448 | + for k in 0..2 { | |
| 1449 | + for i in 0..subfr_length { | |
| 1450 | + let x = exc_q14[i + (k + nb_subfr - 2) * subfr_length]; | |
| 1451 | + exc_buf[k * subfr_length + i] = | |
| 1452 | + sat16(smulww(x, prev_gain_q10[k]) >> 8); | |
| 1453 | + } | |
| 1454 | + } | |
| 1455 | + let (e1, s1) = sum_sqr_shift(&exc_buf[..subfr_length]); | |
| 1456 | + let (e2, s2) = sum_sqr_shift(&exc_buf[subfr_length..2 * subfr_length]); | |
| 1457 | + (e1, s1, e2, s2) | |
| 1458 | +} | |
| 1459 | + | |
| 1460 | +impl ChannelState { | |
| 1461 | + /// `silk_PLC_Reset`. | |
| 1462 | + fn plc_reset(&mut self) { | |
| 1463 | + self.plc.pitch_l_q8 = (self.frame_length as i32) << 7; | |
| 1464 | + self.plc.prev_gain_q16 = [1 << 16; 2]; | |
| 1465 | + self.plc.subfr_length = 20; | |
| 1466 | + self.plc.nb_subfr = 2; | |
| 1467 | + } | |
| 1468 | + | |
| 1469 | + /// `silk_PLC`: conceals a lost frame, or tracks the state of a coded one. | |
| 1470 | + fn plc_frame(&mut self, ctrl: &mut FrameCtrl, out: &mut [i16], lost: bool) { | |
| 1471 | + if self.fs_khz != self.plc.fs_khz { | |
| 1472 | + self.plc_reset(); | |
| 1473 | + self.plc.fs_khz = self.fs_khz; | |
| 1474 | + } | |
| 1475 | + if lost { | |
| 1476 | + self.plc_conceal(ctrl, out); | |
| 1477 | + self.loss_cnt += 1; | |
| 1478 | + } else { | |
| 1479 | + self.plc_update(ctrl); | |
| 1480 | + } | |
| 1481 | + } | |
| 1482 | + | |
| 1483 | + /// `silk_PLC_update`: remembers the pitch, LPC and gains of a coded frame. | |
| 1484 | + fn plc_update(&mut self, ctrl: &FrameCtrl) { | |
| 1485 | + let nb = self.nb_subfr; | |
| 1486 | + self.prev_signal_type = self.indices.signal_type; | |
| 1487 | + let mut ltp_gain_q14 = 0i32; | |
| 1488 | + if self.indices.signal_type == TYPE_VOICED { | |
| 1489 | + let mut j = 0usize; | |
| 1490 | + while ((j * self.subfr_length) as i32) < ctrl.pitch_l[nb - 1] { | |
| 1491 | + if j == nb { | |
| 1492 | + break; | |
| 1493 | + } | |
| 1494 | + let k = nb - 1 - j; | |
| 1495 | + let coefs = &ctrl.ltp_coef_q14[k * LTP_ORDER..(k + 1) * LTP_ORDER]; | |
| 1496 | + let temp: i32 = coefs.iter().map(|&c| c as i32).sum(); | |
| 1497 | + if temp > ltp_gain_q14 { | |
| 1498 | + ltp_gain_q14 = temp; | |
| 1499 | + self.plc.ltp_coef_q14.copy_from_slice(coefs); | |
| 1500 | + self.plc.pitch_l_q8 = ctrl.pitch_l[k] << 8; | |
| 1501 | + } | |
| 1502 | + j += 1; | |
| 1503 | + } | |
| 1504 | + self.plc.ltp_coef_q14 = [0; LTP_ORDER]; | |
| 1505 | + self.plc.ltp_coef_q14[LTP_ORDER / 2] = ltp_gain_q14 as i16; | |
| 1506 | + if ltp_gain_q14 < V_PITCH_GAIN_START_MIN_Q14 { | |
| 1507 | + let tmp = V_PITCH_GAIN_START_MIN_Q14 << 10; | |
| 1508 | + let scale_q10 = tmp / ltp_gain_q14.max(1); | |
| 1509 | + for c in self.plc.ltp_coef_q14.iter_mut() { | |
| 1510 | + *c = (smulbb(*c as i32, scale_q10) >> 10) as i16; | |
| 1511 | + } | |
| 1512 | + } else if ltp_gain_q14 > V_PITCH_GAIN_START_MAX_Q14 { | |
| 1513 | + let tmp = V_PITCH_GAIN_START_MAX_Q14 << 14; | |
| 1514 | + let scale_q14 = tmp / ltp_gain_q14.max(1); | |
| 1515 | + for c in self.plc.ltp_coef_q14.iter_mut() { | |
| 1516 | + *c = (smulbb(*c as i32, scale_q14) >> 14) as i16; | |
| 1517 | + } | |
| 1518 | + } | |
| 1519 | + } else { | |
| 1520 | + self.plc.pitch_l_q8 = smulbb(self.fs_khz as i32, MAX_PITCH_LAG_MS) << 8; | |
| 1521 | + self.plc.ltp_coef_q14 = [0; LTP_ORDER]; | |
| 1522 | + } | |
| 1523 | + let order = self.lpc_order; | |
| 1524 | + self.plc.prev_lpc_q12[..order].copy_from_slice(&ctrl.pred_coef_q12[1][..order]); | |
| 1525 | + self.plc.prev_ltp_scale_q14 = ctrl.ltp_scale_q14 as i16; | |
| 1526 | + self.plc.prev_gain_q16 = [ctrl.gains_q16[nb - 2], ctrl.gains_q16[nb - 1]]; | |
| 1527 | + self.plc.subfr_length = self.subfr_length; | |
| 1528 | + self.plc.nb_subfr = nb; | |
| 1529 | + } | |
| 1530 | + | |
| 1531 | + /// `silk_PLC_conceal`: synthesises one lost frame from the saved state. | |
| 1532 | + fn plc_conceal(&mut self, ctrl: &mut FrameCtrl, out: &mut [i16]) { | |
| 1533 | + let order = self.lpc_order; | |
| 1534 | + let ltp_mem = self.ltp_mem_length; | |
| 1535 | + let prev_gain_q10 = [ | |
| 1536 | + self.plc.prev_gain_q16[0] >> 6, | |
| 1537 | + self.plc.prev_gain_q16[1] >> 6, | |
| 1538 | + ]; | |
| 1539 | + if self.first_frame_after_reset { | |
| 1540 | + self.plc.prev_lpc_q12 = [0; MAX_LPC_ORDER]; | |
| 1541 | + } | |
| 1542 | + let (energy1, shift1, energy2, shift2) = | |
| 1543 | + plc_energy(&self.exc_q14, prev_gain_q10, self.subfr_length, self.nb_subfr); | |
| 1544 | + let rand_base = if (energy1 >> shift2) < (energy2 >> shift1) { | |
| 1545 | + ((self.plc.nb_subfr - 1) * self.plc.subfr_length).saturating_sub(RAND_BUF_SIZE) | |
| 1546 | + } else { | |
| 1547 | + (self.plc.nb_subfr * self.plc.subfr_length).saturating_sub(RAND_BUF_SIZE) | |
| 1548 | + }; | |
| 1549 | + | |
| 1550 | + let mut b_q14 = self.plc.ltp_coef_q14; | |
| 1551 | + let mut rand_scale_q14 = self.plc.rand_scale_q14 as i32; | |
| 1552 | + let att = self.loss_cnt.min(NB_ATT as i32 - 1) as usize; | |
| 1553 | + let harm_gain_q15 = HARM_ATT_Q15[att]; | |
| 1554 | + let mut rand_gain_q15 = if self.prev_signal_type == TYPE_VOICED { | |
| 1555 | + PLC_RAND_ATTENUATE_V_Q15[att] | |
| 1556 | + } else { | |
| 1557 | + PLC_RAND_ATTENUATE_UV_Q15[att] | |
| 1558 | + }; | |
| 1559 | + | |
| 1560 | + bwexpander(&mut self.plc.prev_lpc_q12[..order], BWE_COEF_Q16); | |
| 1561 | + let a_q12 = self.plc.prev_lpc_q12; | |
| 1562 | + | |
| 1563 | + if self.loss_cnt == 0 { | |
| 1564 | + rand_scale_q14 = 1 << 14; | |
| 1565 | + if self.prev_signal_type == TYPE_VOICED { | |
| 1566 | + for &b in b_q14.iter() { | |
| 1567 | + rand_scale_q14 -= b as i32; | |
| 1568 | + } | |
| 1569 | + rand_scale_q14 = rand_scale_q14.max(3277); | |
| 1570 | + rand_scale_q14 = | |
| 1571 | + smulbb(rand_scale_q14, self.plc.prev_ltp_scale_q14 as i32) >> 14; | |
| 1572 | + } else { | |
| 1573 | + let inv_gain_q30 = lpc_inverse_pred_gain(&self.plc.prev_lpc_q12[..order]); | |
| 1574 | + let mut down_scale_q30 = | |
| 1575 | + ((1 << 30) >> LOG2_INV_LPC_GAIN_HIGH_THRES).min(inv_gain_q30); | |
| 1576 | + down_scale_q30 = ((1 << 30) >> LOG2_INV_LPC_GAIN_LOW_THRES).max(down_scale_q30); | |
| 1577 | + down_scale_q30 <<= LOG2_INV_LPC_GAIN_HIGH_THRES; | |
| 1578 | + rand_gain_q15 = smulwb(down_scale_q30, rand_gain_q15) >> 14; | |
| 1579 | + } | |
| 1580 | + } | |
| 1581 | + | |
| 1582 | + let mut rand_seed = self.plc.rand_seed; | |
| 1583 | + let mut lag = rshift_round(self.plc.pitch_l_q8, 8); | |
| 1584 | + let idx = (ltp_mem as i32 - lag - order as i32 - (LTP_ORDER / 2) as i32) as usize; | |
| 1585 | + let mut s_ltp = [0i16; MAX_LTP_MEM]; | |
| 1586 | + lpc_analysis_filter( | |
| 1587 | + &mut s_ltp[idx..ltp_mem], | |
| 1588 | + &self.out_buf[idx..ltp_mem], | |
| 1589 | + &a_q12, | |
| 1590 | + ltp_mem - idx, | |
| 1591 | + order, | |
| 1592 | + ); | |
| 1593 | + let inv_gain_q30 = inverse32_var_q(self.plc.prev_gain_q16[1], 46).min(i32::MAX >> 1); | |
| 1594 | + let mut s_ltp_q14 = [0i32; MAX_LTP_MEM + MAX_FRAME_LENGTH]; | |
| 1595 | + for i in idx + order..ltp_mem { | |
| 1596 | + s_ltp_q14[i] = smulwb(inv_gain_q30, s_ltp[i] as i32); | |
| 1597 | + } | |
| 1598 | + | |
| 1599 | + let mut sltp_buf_idx = ltp_mem; | |
| 1600 | + for _ in 0..self.nb_subfr { | |
| 1601 | + let pred0 = sltp_buf_idx + LTP_ORDER / 2 - lag as usize; | |
| 1602 | + for i in 0..self.subfr_length { | |
| 1603 | + let pred = pred0 + i; | |
| 1604 | + let mut ltp_pred_q12 = 2i32; | |
| 1605 | + for j in 0..LTP_ORDER { | |
| 1606 | + ltp_pred_q12 = smlawb(ltp_pred_q12, s_ltp_q14[pred - j], b_q14[j] as i32); | |
| 1607 | + } | |
| 1608 | + rand_seed = silk_rand(rand_seed); | |
| 1609 | + let ridx = ((rand_seed >> 25) & RAND_BUF_MASK) as usize; | |
| 1610 | + s_ltp_q14[sltp_buf_idx] = smlawb( | |
| 1611 | + ltp_pred_q12, | |
| 1612 | + self.exc_q14[rand_base + ridx], | |
| 1613 | + rand_scale_q14, | |
| 1614 | + ) << 2; | |
| 1615 | + sltp_buf_idx += 1; | |
| 1616 | + } | |
| 1617 | + for b in b_q14.iter_mut() { | |
| 1618 | + *b = (smulbb(harm_gain_q15, *b as i32) >> 15) as i16; | |
| 1619 | + } | |
| 1620 | + rand_scale_q14 = smulbb(rand_scale_q14, rand_gain_q15) >> 15; | |
| 1621 | + self.plc.pitch_l_q8 = | |
| 1622 | + smlawb(self.plc.pitch_l_q8, self.plc.pitch_l_q8, PITCH_DRIFT_FAC_Q16); | |
| 1623 | + self.plc.pitch_l_q8 = self | |
| 1624 | + .plc | |
| 1625 | + .pitch_l_q8 | |
| 1626 | + .min(smulbb(MAX_PITCH_LAG_MS, self.fs_khz as i32) << 8); | |
| 1627 | + lag = rshift_round(self.plc.pitch_l_q8, 8); | |
| 1628 | + } | |
| 1629 | + | |
| 1630 | + let base = ltp_mem - MAX_LPC_ORDER; | |
| 1631 | + s_ltp_q14[base..base + MAX_LPC_ORDER].copy_from_slice(&self.s_lpc_q14); | |
| 1632 | + for i in 0..self.frame_length { | |
| 1633 | + let s = base + MAX_LPC_ORDER + i; | |
| 1634 | + let mut lpc_pred_q10 = (order >> 1) as i32; | |
| 1635 | + for j in 0..order { | |
| 1636 | + lpc_pred_q10 = smlawb(lpc_pred_q10, s_ltp_q14[s - j - 1], a_q12[j] as i32); | |
| 1637 | + } | |
| 1638 | + s_ltp_q14[s] = s_ltp_q14[s].saturating_add(lshift_sat32(lpc_pred_q10, 4)); | |
| 1639 | + out[i] = sat16(rshift_round(smulww(s_ltp_q14[s], prev_gain_q10[1]), 8)); | |
| 1640 | + } | |
| 1641 | + let end = base + self.frame_length; | |
| 1642 | + self.s_lpc_q14 | |
| 1643 | + .copy_from_slice(&s_ltp_q14[end..end + MAX_LPC_ORDER]); | |
| 1644 | + | |
| 1645 | + self.plc.rand_seed = rand_seed; | |
| 1646 | + self.plc.rand_scale_q14 = rand_scale_q14 as i16; | |
| 1647 | + self.plc.ltp_coef_q14 = b_q14; | |
| 1648 | + ctrl.pitch_l = [lag; MAX_NB_SUBFR]; | |
| 1649 | + } | |
| 1650 | + | |
| 1651 | + /// `silk_PLC_glue_frames`: smooths the start of the first frame after a loss. | |
| 1652 | + fn plc_glue_frames(&mut self, frame: &mut [i16]) { | |
| 1653 | + let length = frame.len() as i32; | |
| 1654 | + if self.loss_cnt != 0 { | |
| 1655 | + let (e, s) = sum_sqr_shift(frame); | |
| 1656 | + self.plc.conc_energy = e; | |
| 1657 | + self.plc.conc_energy_shift = s; | |
| 1658 | + self.plc.last_frame_lost = true; | |
| 1659 | + return; | |
| 1660 | + } | |
| 1661 | + if self.plc.last_frame_lost { | |
| 1662 | + let (mut energy, energy_shift) = sum_sqr_shift(frame); | |
| 1663 | + if energy_shift > self.plc.conc_energy_shift { | |
| 1664 | + self.plc.conc_energy >>= energy_shift - self.plc.conc_energy_shift; | |
| 1665 | + } else if energy_shift < self.plc.conc_energy_shift { | |
| 1666 | + energy >>= self.plc.conc_energy_shift - energy_shift; | |
| 1667 | + } | |
| 1668 | + if energy > self.plc.conc_energy { | |
| 1669 | + let lz = clz32(self.plc.conc_energy) - 1; | |
| 1670 | + self.plc.conc_energy <<= lz; | |
| 1671 | + energy >>= (24 - lz).max(0); | |
| 1672 | + let frac_q24 = self.plc.conc_energy / energy.max(1); | |
| 1673 | + let mut gain_q16 = sqrt_approx(frac_q24) << 4; | |
| 1674 | + let mut slope_q16 = ((1 << 16) - gain_q16) / length; | |
| 1675 | + slope_q16 <<= 2; | |
| 1676 | + for v in frame.iter_mut() { | |
| 1677 | + *v = smulwb(gain_q16, *v as i32) as i16; | |
| 1678 | + gain_q16 += slope_q16; | |
| 1679 | + if gain_q16 > (1 << 16) { | |
| 1680 | + break; | |
| 1681 | + } | |
| 1682 | + } | |
| 1683 | + } | |
| 1684 | + } | |
| 1685 | + self.plc.last_frame_lost = false; | |
| 1686 | + } | |
| 1687 | +} | |
| 1688 | + | |
| 1341 | 1689 | /// `silk_LPC_analysis_filter`: the whitening filter used to rebuild the LTP |
| 1342 | 1690 | /// state from previously decoded output. |
| 1343 | 1691 | fn lpc_analysis_filter(out: &mut [i16], input: &[i16], b_q12: &[i16], len: usize, d: usize) { |
| @@ -1353,7 +1701,7 @@ fn lpc_analysis_filter(out: &mut [i16], input: &[i16], b_q12: &[i16], len: usize | ||
| 1353 | 1701 | } |
| 1354 | 1702 | |
| 1355 | 1703 | /// `silk_decode_core` (Section 4.2.7.9): excitation to output samples. |
| 1356 | -fn decode_core(ch: &mut ChannelState, ctrl: &FrameCtrl, xq: &mut [i16], pulses: &[i32]) { | |
| 1704 | +fn decode_core(ch: &mut ChannelState, ctrl: &mut FrameCtrl, xq: &mut [i16], pulses: &[i32]) { | |
| 1357 | 1705 | let offset_q10 = QUANTIZATION_OFFSETS_Q10[ch.indices.signal_type as usize >> 1] |
| 1358 | 1706 | [ch.indices.quant_offset_type as usize] as i32; |
| 1359 | 1707 | let nlsf_interpolated = ch.indices.nlsf_interp_coef_q2 < 4; |
| @@ -1382,6 +1730,19 @@ fn decode_core(ch: &mut ChannelState, ctrl: &FrameCtrl, xq: &mut [i16], pulses: | ||
| 1382 | 1730 | let mut res_q14 = [0i32; 5 * 16]; |
| 1383 | 1731 | |
| 1384 | 1732 | for k in 0..ch.nb_subfr { |
| 1733 | + let mut signal_type = ch.indices.signal_type; | |
| 1734 | + // Avoid an abrupt switch from voiced PLC to unvoiced normal decoding. | |
| 1735 | + if ch.loss_cnt != 0 | |
| 1736 | + && ch.prev_signal_type == TYPE_VOICED | |
| 1737 | + && ch.indices.signal_type != TYPE_VOICED | |
| 1738 | + && k < MAX_NB_SUBFR / 2 | |
| 1739 | + { | |
| 1740 | + let b = &mut ctrl.ltp_coef_q14[k * LTP_ORDER..(k + 1) * LTP_ORDER]; | |
| 1741 | + b.fill(0); | |
| 1742 | + b[LTP_ORDER / 2] = 4096; | |
| 1743 | + signal_type = TYPE_VOICED; | |
| 1744 | + ctrl.pitch_l[k] = ch.lag_prev; | |
| 1745 | + } | |
| 1385 | 1746 | let a_q12 = &ctrl.pred_coef_q12[k >> 1]; |
| 1386 | 1747 | let b_q14 = &ctrl.ltp_coef_q14[k * LTP_ORDER..k * LTP_ORDER + LTP_ORDER]; |
| 1387 | 1748 | let gain_q10 = ctrl.gains_q16[k] >> 6; |
| @@ -1397,7 +1758,7 @@ fn decode_core(ch: &mut ChannelState, ctrl: &FrameCtrl, xq: &mut [i16], pulses: | ||
| 1397 | 1758 | }; |
| 1398 | 1759 | ch.prev_gain_q16 = ctrl.gains_q16[k]; |
| 1399 | 1760 | |
| 1400 | - if ch.indices.signal_type == TYPE_VOICED { | |
| 1761 | + if signal_type == TYPE_VOICED { | |
| 1401 | 1762 | lag = ctrl.pitch_l[k] as usize; |
| 1402 | 1763 | if k == 0 || (k == 2 && nlsf_interpolated) { |
| 1403 | 1764 | // Rebuild the LTP history by whitening past output. |
| @@ -1431,7 +1792,7 @@ fn decode_core(ch: &mut ChannelState, ctrl: &FrameCtrl, xq: &mut [i16], pulses: | ||
| 1431 | 1792 | |
| 1432 | 1793 | // Long-term prediction. |
| 1433 | 1794 | let exc_base = k * ch.subfr_length; |
| 1434 | - let pres_q14: &[i32] = if ch.indices.signal_type == TYPE_VOICED { | |
| 1795 | + let pres_q14: &[i32] = if signal_type == TYPE_VOICED { | |
| 1435 | 1796 | let pred_base = sltp_buf_idx - lag + LTP_ORDER / 2; |
| 1436 | 1797 | for i in 0..ch.subfr_length { |
| 1437 | 1798 | let pred_idx = pred_base + i; |
| @@ -1476,32 +1837,41 @@ fn decode_frame( | ||
| 1476 | 1837 | out: &mut [i16], |
| 1477 | 1838 | cond: CondCoding, |
| 1478 | 1839 | lbrr: bool, |
| 1840 | + coded: bool, | |
| 1479 | 1841 | ) -> usize { |
| 1480 | - let frame_index = ch.frames_decoded; | |
| 1481 | - decode_indices(ch, dec, frame_index, lbrr, cond); | |
| 1482 | - let mut pulses = [0i32; MAX_FRAME_LENGTH + SHELL_LEN]; | |
| 1483 | - decode_pulses( | |
| 1484 | - dec, | |
| 1485 | - &mut pulses, | |
| 1486 | - ch.frame_length, | |
| 1487 | - ch.indices.signal_type, | |
| 1488 | - ch.indices.quant_offset_type, | |
| 1489 | - ); | |
| 1490 | - let mut ctrl = decode_parameters(ch, cond); | |
| 1491 | - if cond == CondCoding::IndependentlyNoLtpScaling { | |
| 1492 | - ctrl.ltp_scale_q14 = LTPSCALES_TABLE_Q14[0] as i32; | |
| 1842 | + let mut ctrl = FrameCtrl::default(); | |
| 1843 | + if coded { | |
| 1844 | + let frame_index = ch.frames_decoded; | |
| 1845 | + decode_indices(ch, dec, frame_index, lbrr, cond); | |
| 1846 | + let mut pulses = [0i32; MAX_FRAME_LENGTH + SHELL_LEN]; | |
| 1847 | + decode_pulses( | |
| 1848 | + dec, | |
| 1849 | + &mut pulses, | |
| 1850 | + ch.frame_length, | |
| 1851 | + ch.indices.signal_type, | |
| 1852 | + ch.indices.quant_offset_type, | |
| 1853 | + ); | |
| 1854 | + ctrl = decode_parameters(ch, cond); | |
| 1855 | + if cond == CondCoding::IndependentlyNoLtpScaling { | |
| 1856 | + ctrl.ltp_scale_q14 = LTPSCALES_TABLE_Q14[0] as i32; | |
| 1857 | + } | |
| 1858 | + decode_core(ch, &mut ctrl, out, &pulses); | |
| 1859 | + ch.plc_frame(&mut ctrl, out, false); | |
| 1860 | + ch.loss_cnt = 0; | |
| 1861 | + ch.prev_signal_type = ch.indices.signal_type; | |
| 1862 | + ch.first_frame_after_reset = false; | |
| 1863 | + } else { | |
| 1864 | + ch.plc_frame(&mut ctrl, out, true); | |
| 1493 | 1865 | } |
| 1494 | - decode_core(ch, &ctrl, out, &pulses); | |
| 1495 | - ch.prev_signal_type = ch.indices.signal_type; | |
| 1496 | - ch.first_frame_after_reset = false; | |
| 1497 | - ch.lag_prev = ctrl.pitch_l[ch.nb_subfr - 1]; | |
| 1498 | 1866 | |
| 1499 | 1867 | // Slide the synthesis history. |
| 1500 | - let mv_len = ch.ltp_mem_length - ch.frame_length; | |
| 1501 | - ch.out_buf | |
| 1502 | - .copy_within(ch.frame_length..ch.frame_length + mv_len, 0); | |
| 1503 | - ch.out_buf[mv_len..mv_len + ch.frame_length].copy_from_slice(&out[..ch.frame_length]); | |
| 1504 | - ch.frame_length | |
| 1868 | + let fl = ch.frame_length; | |
| 1869 | + let mv_len = ch.ltp_mem_length - fl; | |
| 1870 | + ch.out_buf.copy_within(fl..fl + mv_len, 0); | |
| 1871 | + ch.out_buf[mv_len..mv_len + fl].copy_from_slice(&out[..fl]); | |
| 1872 | + ch.plc_glue_frames(&mut out[..fl]); | |
| 1873 | + ch.lag_prev = ctrl.pitch_l[ch.nb_subfr - 1]; | |
| 1874 | + fl | |
| 1505 | 1875 | } |
| 1506 | 1876 | |
| 1507 | 1877 | /// `silk_stereo_decode_pred` (Section 4.2.7.1). |
crates/ec-opus/src/silk_enc_write.rsmodified+3 -3
| @@ -375,7 +375,7 @@ impl SilkEncoder { | ||
| 375 | 375 | fs as u32 * 1000, |
| 376 | 376 | 1, |
| 377 | 377 | first, |
| 378 | - false, | |
| 378 | + crate::silk::SilkFrameMode::Normal, | |
| 379 | 379 | )?; |
| 380 | 380 | if n == 0 { |
| 381 | 381 | break; |
| @@ -1309,7 +1309,7 @@ impl SilkStereoEncoder { | ||
| 1309 | 1309 | fs as u32 * 1000, |
| 1310 | 1310 | 2, |
| 1311 | 1311 | true, |
| 1312 | - false, | |
| 1312 | + crate::silk::SilkFrameMode::Normal, | |
| 1313 | 1313 | )?; |
| 1314 | 1314 | debug_assert_eq!(n, frame_ms * fs); |
| 1315 | 1315 | self.mirror_range = dec.range(); |
| @@ -1627,7 +1627,7 @@ mod tests { | ||
| 1627 | 1627 | if wb { 16000 } else { 8000 }, |
| 1628 | 1628 | 1, |
| 1629 | 1629 | true, |
| 1630 | - false, | |
| 1630 | + crate::silk::SilkFrameMode::Normal, | |
| 1631 | 1631 | ) |
| 1632 | 1632 | .unwrap(); |
| 1633 | 1633 | assert_eq!(n, if wb { 320 } else { 160 }); |
crates/ec-opus/src/stack_budget.rsmodified+5 -5
| @@ -114,7 +114,7 @@ pub(crate) const BY_VALUE_TYPES: &[(&str, Option<usize>, bool, &str)] = &[ | ||
| 114 | 114 | // -- lib.rs ----------------------------------------------------------- |
| 115 | 115 | ( |
| 116 | 116 | "Decoder", |
| 117 | - Some(1568), | |
| 117 | + Some(1576), | |
| 118 | 118 | true, |
| 119 | 119 | "Decoder::new -> Result<Decoder>. Was 7576, 92.5% of the budget, \ |
| 120 | 120 | because a 6304-byte SilkDecoder sat inline; that field is now a Box", |
| @@ -218,10 +218,10 @@ pub(crate) const BY_VALUE_TYPES: &[(&str, Option<usize>, bool, &str)] = &[ | ||
| 218 | 218 | // -- silk.rs ---------------------------------------------------------- |
| 219 | 219 | ( |
| 220 | 220 | "SilkDecoder", |
| 221 | - Some(6304), | |
| 221 | + Some(6496), | |
| 222 | 222 | true, |
| 223 | 223 | "silk::SilkDecoder::new -> Self, and it is `pub use`d at the crate \ |
| 224 | - root, so 6304 bytes is a caller's by-value cost TODAY even though \ | |
| 224 | + root, so 6496 bytes is a caller's by-value cost TODAY even though \ | |
| 225 | 225 | Decoder::new moves it straight into a Box. At 77% of the budget it \ |
| 226 | 226 | is the LARGEST budgeted row in the table, and the one that says how \ |
| 227 | 227 | little headroom the budget would have left if it had stayed inline", |
| @@ -235,7 +235,7 @@ pub(crate) const BY_VALUE_TYPES: &[(&str, Option<usize>, bool, &str)] = &[ | ||
| 235 | 235 | // -- silk_enc_write.rs ------------------------------------------------ |
| 236 | 236 | ( |
| 237 | 237 | "SilkEncoder", |
| 238 | - Some(6688), | |
| 238 | + Some(6880), | |
| 239 | 239 | false, |
| 240 | 240 | "silk_enc_write::SilkEncoder::new -> Self. `pub`, `pub use`d at the \ |
| 241 | 241 | crate root, and returned by value -- but every field that holds one \ |
| @@ -243,7 +243,7 @@ pub(crate) const BY_VALUE_TYPES: &[(&str, Option<usize>, bool, &str)] = &[ | ||
| 243 | 243 | ), |
| 244 | 244 | ( |
| 245 | 245 | "SilkStereoEncoder", |
| 246 | - Some(19856), | |
| 246 | + Some(20432), | |
| 247 | 247 | false, |
| 248 | 248 | "silk_enc_write::SilkStereoEncoder::new -> Self. The largest single \ |
| 249 | 249 | type in the crate at 2.42x the budget, and the reason three of the \ |
crates/ec-opus/tests/conformance.rsmodified+1 -1
| @@ -4513,7 +4513,7 @@ fn collect_indices(packets: &[Vec<u8>]) -> Vec<(ec_opus::SilkDecIndices, usize)> | ||
| 4513 | 4513 | continue; |
| 4514 | 4514 | } |
| 4515 | 4515 | let mut dec = ec_opus::RangeDecoder::new(frame_data); |
| 4516 | - let _ = silk.decode(&mut dec, &mut silk_pcm, payload_ms, internal_rate, 1, first, false); | |
| 4516 | + let _ = silk.decode(&mut dec, &mut silk_pcm, payload_ms, internal_rate, 1, first, ec_opus::SilkFrameMode::Normal); | |
| 4517 | 4517 | let ix = silk.last_indices(); |
| 4518 | 4518 | let bytes = frame_data.len(); |
| 4519 | 4519 | results.push((ix, bytes)); |
crates/ec-opus/tests/plc.rsmodified+5 -1
| @@ -103,6 +103,8 @@ fn inband_fec_recovers_a_lost_frame() { | ||
| 103 | 103 | a.extend_from_slice(&buf[..k]); |
| 104 | 104 | let k = if i == 20 { |
| 105 | 105 | lossy.decode_fec(&packets[21], FRAME, &mut buf).unwrap() |
| 106 | + } else if i == 30 { | |
| 107 | + lossy.conceal(FRAME, &mut buf).unwrap() | |
| 106 | 108 | } else { |
| 107 | 109 | lossy.decode_float(p, &mut buf).unwrap() |
| 108 | 110 | }; |
| @@ -111,7 +113,9 @@ fn inband_fec_recovers_a_lost_frame() { | ||
| 111 | 113 | let f = |i: usize| i * FRAME..(i + 1) * FRAME; |
| 112 | 114 | let c20 = corr(&a[f(20)], &b[f(20)]); |
| 113 | 115 | let c22 = corr(&a[f(22)], &b[f(22)]); |
| 114 | - eprintln!("fec c20 {c20:.3} c22 {c22:.3}"); | |
| 116 | + let c30 = corr(&a[f(30)], &b[f(30)]); | |
| 117 | + eprintln!("fec c20 {c20:.3} c22 {c22:.3} plc c30 {c30:.3}"); | |
| 118 | + assert!(c30 > 0.3); | |
| 115 | 119 | assert!(c20 > 0.7); |
| 116 | 120 | assert!(c22 > 0.95); |
| 117 | 121 | } |