Coverage Report

Created: 2025-11-11 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rav1e-0.8.1/src/util/cdf.rs
Line
Count
Source
1
// Copyright (c) 2017-2021, The rav1e contributors. All rights reserved
2
//
3
// This source code is subject to the terms of the BSD 2 Clause License and
4
// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
5
// was not distributed with this source code in the LICENSE file, you can
6
// obtain it at www.aomedia.org/license/software. If the Alliance for Open
7
// Media Patent License 1.0 was not distributed with this source code in the
8
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
9
10
0
pub const fn cdf<const VARS: usize, const CDF_LEN: usize>(
11
0
  vars: [u16; VARS],
12
0
) -> [u16; CDF_LEN] {
13
  // Ensure that at least one zero is kept at the end
14
0
  assert!(CDF_LEN > VARS);
15
16
0
  let mut out = [0; CDF_LEN];
17
0
  let mut i = 0;
18
0
  while i < vars.len() {
19
0
    assert!(vars[i] <= 32768);
20
0
    out[i] = 32768 - vars[i];
21
0
    i += 1;
22
  }
23
24
0
  out
25
0
}
26
27
0
pub const fn cdf_2d<
28
0
  const VARS: usize,
29
0
  const CDF_LEN: usize,
30
0
  const N_2D: usize,
31
0
>(
32
0
  vars: [[u16; VARS]; N_2D],
33
0
) -> [[u16; CDF_LEN]; N_2D] {
34
0
  let mut out = [[0u16; CDF_LEN]; N_2D];
35
0
  let mut c = 0;
36
0
  while c < vars.len() {
37
0
    out[c] = cdf(vars[c]);
38
0
    c += 1;
39
0
  }
40
41
0
  out
42
0
}
43
44
0
pub const fn cdf_3d<
45
0
  const VARS: usize,
46
0
  const CDF_LEN: usize,
47
0
  const N_2D: usize,
48
0
  const N_3D: usize,
49
0
>(
50
0
  vars: [[[u16; VARS]; N_2D]; N_3D],
51
0
) -> [[[u16; CDF_LEN]; N_2D]; N_3D] {
52
0
  let mut out = [[[0u16; CDF_LEN]; N_2D]; N_3D];
53
0
  let mut c = 0;
54
0
  while c < vars.len() {
55
0
    out[c] = cdf_2d(vars[c]);
56
0
    c += 1;
57
0
  }
58
59
0
  out
60
0
}
61
62
0
pub const fn cdf_4d<
63
0
  const VARS: usize,
64
0
  const CDF_LEN: usize,
65
0
  const N_2D: usize,
66
0
  const N_3D: usize,
67
0
  const N_4D: usize,
68
0
>(
69
0
  vars: [[[[u16; VARS]; N_2D]; N_3D]; N_4D],
70
0
) -> [[[[u16; CDF_LEN]; N_2D]; N_3D]; N_4D] {
71
0
  let mut out = [[[[0u16; CDF_LEN]; N_2D]; N_3D]; N_4D];
72
0
  let mut c = 0;
73
0
  while c < vars.len() {
74
0
    out[c] = cdf_3d(vars[c]);
75
0
    c += 1;
76
0
  }
77
78
0
  out
79
0
}
80
81
0
pub const fn cdf_5d<
82
0
  const VARS: usize,
83
0
  const CDF_LEN: usize,
84
0
  const N_2D: usize,
85
0
  const N_3D: usize,
86
0
  const N_4D: usize,
87
0
  const N_5D: usize,
88
0
>(
89
0
  vars: [[[[[u16; VARS]; N_2D]; N_3D]; N_4D]; N_5D],
90
0
) -> [[[[[u16; CDF_LEN]; N_2D]; N_3D]; N_4D]; N_5D] {
91
0
  let mut out = [[[[[0u16; CDF_LEN]; N_2D]; N_3D]; N_4D]; N_5D];
92
0
  let mut c = 0;
93
0
  while c < vars.len() {
94
0
    out[c] = cdf_4d(vars[c]);
95
0
    c += 1;
96
0
  }
97
98
0
  out
99
0
}
100
101
#[cfg(test)]
102
mod test {
103
  use super::*;
104
105
  #[test]
106
  fn cdf_len_ok() {
107
    let _: [u16; 5] = cdf([]);
108
    let _: [u16; 5] = cdf([1]);
109
    let _: [u16; 5] = cdf([1, 2, 3, 4]);
110
  }
111
112
  #[test]
113
  #[should_panic]
114
  fn cdf_len_panics() {
115
    let _: [u16; 5] = cdf([1, 2, 3, 4, 5]);
116
  }
117
118
  #[test]
119
  #[should_panic]
120
  fn cdf_val_panics() {
121
    let _: [u16; 5] = cdf([40000]);
122
  }
123
124
  #[test]
125
  fn cdf_vals_ok() {
126
    let cdf: [u16; 5] = cdf([2000, 10000, 32768, 0]);
127
    assert_eq!(cdf, [30768, 22768, 0, 32768, 0]);
128
  }
129
130
  #[test]
131
  fn cdf_5d_ok() {
132
    let cdf: [[[[[u16; 4]; 2]; 1]; 1]; 1] =
133
      cdf_5d([[[[[1000, 2000], [3000, 4000]]]]]);
134
    assert_eq!(cdf, [[[[[31768, 30768, 0, 0], [29768, 28768, 0, 0],]]]])
135
  }
136
}