ruopus 0.1.2

A pure-Rust implementation of the Opus audio codec (RFC 6716). No FFI; unsafe confined to documented SIMD kernels.
Documentation
//! Rate control for the SILK encoder (RFC 6716 ยง5.2).
//!
//! [`control_snr`] maps a target bitrate to the coding SNR (`SNR_dB_Q7`) that
//! drives the noise-shaping gain control and the gain/lambda tuning. The
//! per-bandwidth tables were obtained by the reference encoder by measuring
//! the achieved bitrate across SNR settings (entries are spaced at 400 bps
//! and scaled by 21 into Q7); the first ~4 kb/s of entries are all zero and
//! omitted from the tables.

/// Target-rate-to-SNR table for the 8 kHz internal rate (Q7, scaled by 21).
const TARGET_RATE_NB_21: [u8; 107] = [
    0, 15, 39, 52, 61, 68, 74, 79, 84, 88, 92, 95, 99, 102, 105, 108, 111, 114, 117, 119, 122, 124, 126, 129, 131, 133,
    135, 137, 139, 142, 143, 145, 147, 149, 151, 153, 155, 157, 158, 160, 162, 163, 165, 167, 168, 170, 171, 173, 174,
    176, 177, 179, 180, 182, 183, 185, 186, 187, 189, 190, 192, 193, 194, 196, 197, 199, 200, 201, 203, 204, 205, 207,
    208, 209, 211, 212, 213, 215, 216, 217, 219, 220, 221, 223, 224, 225, 227, 228, 230, 231, 232, 234, 235, 236, 238,
    239, 241, 242, 243, 245, 246, 248, 249, 250, 252, 253, 255,
];

/// Target-rate-to-SNR table for the 12 kHz internal rate (Q7, scaled by 21).
const TARGET_RATE_MB_21: [u8; 155] = [
    0, 0, 28, 43, 52, 59, 65, 70, 74, 78, 81, 85, 87, 90, 93, 95, 98, 100, 102, 105, 107, 109, 111, 113, 115, 116, 118,
    120, 122, 123, 125, 127, 128, 130, 131, 133, 134, 136, 137, 138, 140, 141, 143, 144, 145, 147, 148, 149, 151, 152,
    153, 154, 156, 157, 158, 159, 160, 162, 163, 164, 165, 166, 167, 168, 169, 171, 172, 173, 174, 175, 176, 177, 178,
    179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200,
    201, 202, 203, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 214, 215, 216, 217, 218, 219, 220, 221,
    222, 223, 224, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 236, 237, 238, 239, 240, 241, 242,
    243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
];

/// Target-rate-to-SNR table for the 16 kHz internal rate (Q7, scaled by 21).
const TARGET_RATE_WB_21: [u8; 191] = [
    0, 0, 0, 8, 29, 41, 49, 56, 62, 66, 70, 74, 77, 80, 83, 86, 88, 91, 93, 95, 97, 99, 101, 103, 105, 107, 108, 110,
    112, 113, 115, 116, 118, 119, 121, 122, 123, 125, 126, 127, 129, 130, 131, 132, 134, 135, 136, 137, 138, 140, 141,
    142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 156, 157, 158, 159, 159, 160, 161, 162, 163, 164,
    165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 177, 178, 179, 180, 181, 181, 182, 183, 184,
    185, 185, 186, 187, 188, 189, 189, 190, 191, 192, 192, 193, 194, 195, 195, 196, 197, 198, 198, 199, 200, 200, 201,
    202, 203, 203, 204, 205, 206, 206, 207, 208, 209, 209, 210, 211, 211, 212, 213, 214, 214, 215, 216, 216, 217, 218,
    219, 219, 220, 221, 221, 222, 223, 224, 224, 225, 226, 226, 227, 228, 229, 229, 230, 231, 232, 232, 233, 234, 234,
    235, 236, 237, 237, 238, 239, 240, 240, 241, 242, 243, 243, 244, 245, 246, 246, 247, 248, 249, 249, 250, 251, 252,
    253, 255,
];

/// The coding SNR in Q7 for `target_rate_bps` at the given internal rate and
/// subframe count.
#[must_use]
pub(crate) fn control_snr(fs_khz: i32, nb_subfr: usize, target_rate_bps: i32) -> i32 {
    let mut target = target_rate_bps;
    if nb_subfr == 2 {
        target -= 2000 + fs_khz / 16;
    }
    let table: &[u8] = match fs_khz {
        8 => &TARGET_RATE_NB_21,
        12 => &TARGET_RATE_MB_21,
        _ => &TARGET_RATE_WB_21,
    };
    let id = ((target + 200) / 400 - 10).min(table.len() as i32 - 1);
    if id <= 0 { 0 } else { i32::from(table[id as usize]) * 21 }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Bit-exact pins against the reference.
    #[test]
    fn control_snr_matches_reference_pins() {
        // (fs_khz, nb_subfr, rate_bps, expected SNR_dB_Q7).
        let pins = [
            (16, 4, 8000, 1470),
            (16, 4, 16000, 2415),
            (16, 4, 24000, 2961),
            (16, 4, 32000, 3381),
            (16, 4, 40000, 3759),
            (12, 4, 8000, 1701),
            (12, 4, 24000, 3213),
            (12, 4, 40000, 4095),
            (8, 4, 8000, 1932),
            (8, 4, 24000, 3717),
            (8, 4, 40000, 4872),
            (8, 2, 8000, 1428),
            (8, 2, 32000, 4179),
            (16, 2, 8000, 861),
            (16, 2, 16000, 2247),
            (16, 2, 40000, 3675),
        ];
        for (fs, nb, rate, exp) in pins {
            assert_eq!(control_snr(fs, nb, rate), exp, "fs={fs} nb={nb} rate={rate}");
        }
    }
}