dsi_bitstream/codes/rice.rs
1/*
2 * SPDX-FileCopyrightText: 2023 Sebastiano Vigna
3 *
4 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
5 */
6
7//! Rice codes.
8//!
9//! Rice codes (AKA Golomb−Rice codes) are a form of approximated [Golomb
10//! codes](crate::codes::golomb) in which the parameter *b* is a power of
11//! two. This restriction makes the code less precise in modeling data with a
12//! geometric distribution, but encoding and decoding can be performed without
13//! any integer arithmetic, and thus much more quickly.
14//!
15//! The implied distribution of a Rice code is [the same as that of a Golomb
16//! code with the same parameter](crate::codes::golomb).
17//!
18//! For natural numbers distributed with a geometric distribution with base *p*,
19//! the base-2 logarithm of the optimal *b* is [⌈log₂(ln((√5 + 1)/2) / ln(1 -
20//! *p*))⌉](log2_b).
21//!
22//! The supported range is [0 . . 2⁶⁴) for log₂(*b*) in [0 . . 64), but writing
23//! 2⁶⁴ – 1 when *b* = 1 requires writing the unary code for 2⁶⁴ – 1, which
24//! might not be possible depending on the [`BitWrite`](crate::traits::BitWrite)
25//! implementation (and would require writing 2⁶⁴ bits anyway).
26//!
27//! # References
28//!
29//! Robert F. Rice, “[Some practical universal noiseless coding
30//! techniques](https://ntrs.nasa.gov/api/citations/19790014634/downloads/19790014634.pdf)”.
31//! Jet Propulsion Laboratory, Pasadena, CA, Tech. Rep. JPL-79-22, JPL-83-17,
32//! and JPL-91-3, March 1979.
33//!
34//! Aaron Kiely. “[Selecting the Golomb parameter in Rice
35//! coding](https://tda.jpl.nasa.gov/progress_report/42-159/159E.pdf)”.
36//! Interplanetary Network Progress report 42-159, Jet Propulsion Laboratory,
37//! 2004.
38
39use crate::traits::*;
40
41/// Returns the length of the Rice code for `n` with parameter `log2_b`.
42#[must_use]
43#[inline(always)]
44pub fn len_rice(n: u64, log2_b: usize) -> usize {
45 debug_assert!(log2_b < 64);
46 (n >> log2_b) as usize + 1 + log2_b
47}
48
49/// Returns the optimal value of log₂*b* for a geometric distribution of base
50/// *p*, that is, ⌈log₂(ln((√5 + 1)/2) / ln(1 - *p*))⌉
51pub fn log2_b(p: f64) -> usize {
52 ((-((5f64.sqrt() + 1.0) / 2.0).ln() / (-p).ln_1p()).log2()).ceil() as usize
53}
54
55/// Trait for reading Rice codes.
56pub trait RiceRead<E: Endianness>: BitRead<E> {
57 #[inline(always)]
58 fn read_rice(&mut self, log2_b: usize) -> Result<u64, Self::Error> {
59 debug_assert!(log2_b < 64);
60 Ok((self.read_unary()? << log2_b) + self.read_bits(log2_b)?)
61 }
62}
63
64/// Trait for writing Rice codes.
65pub trait RiceWrite<E: Endianness>: BitWrite<E> {
66 #[inline(always)]
67 fn write_rice(&mut self, n: u64, log2_b: usize) -> Result<usize, Self::Error> {
68 debug_assert!(log2_b < 64);
69
70 let mut written_bits = self.write_unary(n >> log2_b)?;
71 #[cfg(feature = "checks")]
72 {
73 // Clean up n in case checks are enabled
74 let n = n & (1_u128 << log2_b).wrapping_sub(1) as u64;
75 written_bits += self.write_bits(n, log2_b)?;
76 }
77 #[cfg(not(feature = "checks"))]
78 {
79 written_bits += self.write_bits(n, log2_b)?;
80 }
81 Ok(written_bits)
82 }
83}
84
85impl<E: Endianness, B: BitRead<E>> RiceRead<E> for B {}
86impl<E: Endianness, B: BitWrite<E>> RiceWrite<E> for B {}
87
88#[cfg(test)]
89#[test]
90fn test_log2_b() {
91 use crate::prelude::golomb::b;
92
93 let mut p = 1.0;
94 for _ in 0..100 {
95 p *= 0.9;
96 let golomb = b(p);
97 if golomb & -(golomb as i64) as u64 == golomb {
98 assert_eq!(golomb, 1 << log2_b(p));
99 }
100 }
101}