use opus_rs::modes::default_mode;
use opus_rs::range_coder::RangeCoder;
use opus_rs::rate::{BITRES, clt_compute_allocation, pulses2bits};
#[test]
fn test_rate_allocation_constraints() {
let mode = default_mode();
let nb_ebands = mode.nb_ebands;
let offsets = vec![0; nb_ebands];
let cap = vec![100; nb_ebands];
let mut pulses = vec![0; nb_ebands];
let mut fine_quant = vec![0; nb_ebands];
let mut fine_priority = vec![0; nb_ebands];
let mut intensity = 0;
let mut dual_stereo = 0;
let mut balance = 0;
let total_bits_bytes = 40; let total_bits = (total_bits_bytes * 8) << BITRES;
let mut rc = RangeCoder::new_encoder(100);
let used_bits = clt_compute_allocation(
mode,
0,
nb_ebands,
&offsets,
&cap,
5, &mut intensity,
&mut dual_stereo,
total_bits,
&mut balance,
&mut pulses,
&mut fine_quant,
&mut fine_priority,
1, 3, &mut rc,
true, 0, nb_ebands as i32,
);
println!("Used allocation bits: {} / {}", used_bits, total_bits);
assert!(used_bits <= total_bits, "Over-allocation!");
for i in 0..nb_ebands {
assert!(pulses[i] >= 0, "Negative pulses in band {}", i);
let pulse_count = opus_rs::rate::bits2pulses(mode, i, 3, pulses[i]);
let _bits_calc = pulses2bits(mode, i, 3, pulse_count);
}
assert!(
used_bits > 0,
"Allocation used 0 bits with plenty available"
);
}