| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use ec_core::error::{Error, Result}; |
| 7 | use ec_h265_syntax::Pps; |
| 8 | |
| 9 | |
| 10 | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 11 | pub struct TileScan { |
| 12 | |
| 13 | pub rs_to_ts: Vec<usize>, |
| 14 | |
| 15 | pub ts_to_rs: Vec<usize>, |
| 16 | |
| 17 | pub tile_of_rs: Vec<u16>, |
| 18 | } |
| 19 | |
| 20 | impl TileScan { |
| 21 | |
| 22 | pub fn single(cols: usize, rows: usize) -> TileScan { |
| 23 | let total = cols * rows; |
| 24 | TileScan { |
| 25 | rs_to_ts: (0..total).collect(), |
| 26 | ts_to_rs: (0..total).collect(), |
| 27 | tile_of_rs: vec![0; total], |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | |
| 32 | pub fn from_pps(pps: &Pps, cols: usize, rows: usize) -> Result<TileScan> { |
| 33 | if !pps.tiles_enabled { |
| 34 | return Ok(TileScan::single(cols, rows)); |
| 35 | } |
| 36 | let n_cols = pps.num_tile_columns_minus1 as usize + 1; |
| 37 | let n_rows = pps.num_tile_rows_minus1 as usize + 1; |
| 38 | if n_cols > cols || n_rows > rows { |
| 39 | return Err(Error::corrupt(format!( |
| 40 | "HEVC PPS: {n_cols}x{n_rows} tiles in a {cols}x{rows} CTB picture" |
| 41 | ))); |
| 42 | } |
| 43 | let col_widths = if pps.uniform_spacing { |
| 44 | uniform(cols, n_cols) |
| 45 | } else { |
| 46 | explicit(cols, &pps.column_width_minus1, n_cols)? |
| 47 | }; |
| 48 | let row_heights = if pps.uniform_spacing { |
| 49 | uniform(rows, n_rows) |
| 50 | } else { |
| 51 | explicit(rows, &pps.row_height_minus1, n_rows)? |
| 52 | }; |
| 53 | Self::new(cols, rows, &col_widths, &row_heights) |
| 54 | } |
| 55 | |
| 56 | |
| 57 | pub fn new( |
| 58 | cols: usize, |
| 59 | rows: usize, |
| 60 | col_widths: &[usize], |
| 61 | row_heights: &[usize], |
| 62 | ) -> Result<TileScan> { |
| 63 | let total = cols * rows; |
| 64 | let mut ts_to_rs = Vec::with_capacity(total); |
| 65 | let mut tile_of_rs = vec![0u16; total]; |
| 66 | let mut tile: u16 = 0; |
| 67 | let mut y0 = 0; |
| 68 | for &h in row_heights { |
| 69 | let mut x0 = 0; |
| 70 | for &w in col_widths { |
| 71 | for y in y0..y0 + h { |
| 72 | for x in x0..x0 + w { |
| 73 | let rs = y * cols + x; |
| 74 | ts_to_rs.push(rs); |
| 75 | if let Some(id) = tile_of_rs.get_mut(rs) { |
| 76 | *id = tile; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | x0 += w; |
| 81 | tile = tile.saturating_add(1); |
| 82 | } |
| 83 | y0 += h; |
| 84 | } |
| 85 | if ts_to_rs.len() != total || sizes_mismatch(col_widths, cols, row_heights, rows) { |
| 86 | return Err(Error::corrupt( |
| 87 | "HEVC PPS: tile sizes do not cover the picture".to_string(), |
| 88 | )); |
| 89 | } |
| 90 | let mut rs_to_ts = vec![0usize; total]; |
| 91 | for (ts, &rs) in ts_to_rs.iter().enumerate() { |
| 92 | if let Some(slot) = rs_to_ts.get_mut(rs) { |
| 93 | *slot = ts; |
| 94 | } |
| 95 | } |
| 96 | Ok(TileScan { |
| 97 | rs_to_ts, |
| 98 | ts_to_rs, |
| 99 | tile_of_rs, |
| 100 | }) |
| 101 | } |
| 102 | |
| 103 | |
| 104 | pub fn is_tile_start(&self, ts: usize) -> bool { |
| 105 | ts == 0 || self.tile_at_ts(ts) != self.tile_at_ts(ts - 1) |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | pub fn is_row_start(&self, rs: usize, cols: usize) -> bool { |
| 111 | rs.is_multiple_of(cols) || self.tile_of_rs.get(rs) != self.tile_of_rs.get(rs - 1) |
| 112 | } |
| 113 | |
| 114 | |
| 115 | fn tile_at_ts(&self, ts: usize) -> Option<u16> { |
| 116 | self.ts_to_rs |
| 117 | .get(ts) |
| 118 | .and_then(|&rs| self.tile_of_rs.get(rs).copied()) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | fn uniform(total: usize, n: usize) -> Vec<usize> { |
| 124 | (0..n) |
| 125 | .map(|i| ((i + 1) * total) / n - (i * total) / n) |
| 126 | .collect() |
| 127 | } |
| 128 | |
| 129 | |
| 130 | fn explicit(total: usize, minus1: &[u32], n: usize) -> Result<Vec<usize>> { |
| 131 | let mut sizes = Vec::with_capacity(n); |
| 132 | let mut used = 0usize; |
| 133 | for i in 0..n - 1 { |
| 134 | let v = minus1.get(i).map_or(0, |&v| v as usize) + 1; |
| 135 | used += v; |
| 136 | sizes.push(v); |
| 137 | } |
| 138 | if used >= total { |
| 139 | return Err(Error::corrupt( |
| 140 | "HEVC PPS: explicit tile sizes leave no room for the last tile".to_string(), |
| 141 | )); |
| 142 | } |
| 143 | sizes.push(total - used); |
| 144 | Ok(sizes) |
| 145 | } |
| 146 | |
| 147 | |
| 148 | fn sizes_mismatch(cols_w: &[usize], cols: usize, rows_h: &[usize], rows: usize) -> bool { |
| 149 | cols_w.iter().sum::<usize>() != cols || rows_h.iter().sum::<usize>() != rows |
| 150 | } |
| 151 | |
| 152 | #[cfg(test)] |
| 153 | mod tests { |
| 154 | use super::*; |
| 155 | |
| 156 | #[test] |
| 157 | fn uniform_split_matches_6_3() { |
| 158 | |
| 159 | assert_eq!(uniform(10, 3), vec![3, 3, 4]); |
| 160 | } |
| 161 | |
| 162 | #[test] |
| 163 | fn two_by_two_tiles_scan_tile_by_tile() { |
| 164 | |
| 165 | let t = TileScan::new(4, 2, &[2, 2], &[2]).expect("valid tiles"); |
| 166 | assert_eq!(t.ts_to_rs, vec![0, 1, 4, 5, 2, 3, 6, 7]); |
| 167 | assert_eq!(t.tile_of_rs, vec![0, 0, 1, 1, 0, 0, 1, 1]); |
| 168 | assert_eq!(t.rs_to_ts[4], 2); |
| 169 | assert!(t.is_tile_start(0)); |
| 170 | |
| 171 | assert!(t.is_tile_start(4)); |
| 172 | assert!(!t.is_tile_start(1)); |
| 173 | assert!(!t.is_tile_start(2)); |
| 174 | } |
| 175 | |
| 176 | #[test] |
| 177 | fn single_tile_is_raster() { |
| 178 | let t = TileScan::single(3, 2); |
| 179 | assert_eq!(t.ts_to_rs, (0..6).collect::<Vec<_>>()); |
| 180 | assert!(t.is_row_start(3, 3)); |
| 181 | assert!(!t.is_row_start(4, 3)); |
| 182 | } |
| 183 | } |