use super::params::TileInfo;
#[inline]
pub fn uniform_spacing(total: u32, n: u32) -> Vec<u32> {
(0..n)
.map(|i| (i + 1) * total / n - i * total / n)
.collect()
}
#[inline]
pub fn plane_shifts(c_idx: u8, chroma_array_type: u8) -> (u8, u8) {
if c_idx == 0 {
return (0, 0);
}
match chroma_array_type {
1 => (1, 1),
2 => (1, 0),
_ => (0, 0),
}
}
#[derive(Debug, Clone)]
pub struct TileGrid {
pub ctb_width: u32,
pub ctb_height: u32,
pub col_bd: Vec<u32>,
pub row_bd: Vec<u32>,
pub ctb_addr_ts_to_rs: Vec<u32>,
pub ctb_addr_rs_to_ts: Vec<u32>,
}
impl TileGrid {
pub fn single(ctb_width: u32, ctb_height: u32) -> Self {
let n = (ctb_width * ctb_height) as usize;
let identity: Vec<u32> = (0..n as u32).collect();
Self {
ctb_width,
ctb_height,
col_bd: vec![0, ctb_width],
row_bd: vec![0, ctb_height],
ctb_addr_ts_to_rs: identity.clone(),
ctb_addr_rs_to_ts: identity,
}
}
pub fn from_tile_info(ctb_width: u32, ctb_height: u32, ti: &TileInfo) -> Self {
let num_cols = ti.num_tile_columns_minus1 as u32 + 1;
let num_rows = ti.num_tile_rows_minus1 as u32 + 1;
let column_width: Vec<u32> = if ti.uniform_spacing_flag {
uniform_spacing(ctb_width, num_cols)
} else {
let mut w: Vec<u32> = ti.column_widths.iter().map(|&v| v as u32).collect();
let sum: u32 = w.iter().sum();
w.push(ctb_width - sum);
w
};
let row_height: Vec<u32> = if ti.uniform_spacing_flag {
uniform_spacing(ctb_height, num_rows)
} else {
let mut h: Vec<u32> = ti.row_heights.iter().map(|&v| v as u32).collect();
let sum: u32 = h.iter().sum();
h.push(ctb_height - sum);
h
};
Self::from_sizes(ctb_width, ctb_height, &column_width, &row_height)
}
pub fn uniform(ctb_width: u32, ctb_height: u32, num_cols: u32, num_rows: u32) -> Self {
let column_width = uniform_spacing(ctb_width, num_cols);
let row_height = uniform_spacing(ctb_height, num_rows);
Self::from_sizes(ctb_width, ctb_height, &column_width, &row_height)
}
pub fn from_sizes(
ctb_width: u32,
ctb_height: u32,
column_width: &[u32],
row_height: &[u32],
) -> Self {
let num_cols = column_width.len() as u32;
let num_rows = row_height.len() as u32;
let mut col_bd = Vec::with_capacity(num_cols as usize + 1);
col_bd.push(0);
for &w in column_width {
col_bd.push(col_bd.last().unwrap() + w);
}
let mut row_bd = Vec::with_capacity(num_rows as usize + 1);
row_bd.push(0);
for &h in row_height {
row_bd.push(row_bd.last().unwrap() + h);
}
let area = (ctb_width * ctb_height) as usize;
let mut ctb_addr_rs_to_ts = vec![0u32; area];
let mut ctb_addr_ts_to_rs = vec![0u32; area];
for ctb_addr_rs in 0..area as u32 {
let tb_x = ctb_addr_rs % ctb_width;
let tb_y = ctb_addr_rs / ctb_width;
let tile_x = (0..num_cols)
.find(|&i| tb_x < col_bd[i as usize + 1])
.unwrap();
let tile_y = (0..num_rows)
.find(|&i| tb_y < row_bd[i as usize + 1])
.unwrap();
let mut val = 0u32;
for i in 0..tile_x {
val += row_height[tile_y as usize] * column_width[i as usize];
}
for i in 0..tile_y {
val += ctb_width * row_height[i as usize];
}
val += (tb_y - row_bd[tile_y as usize]) * column_width[tile_x as usize] + tb_x
- col_bd[tile_x as usize];
ctb_addr_rs_to_ts[ctb_addr_rs as usize] = val;
ctb_addr_ts_to_rs[val as usize] = ctb_addr_rs;
}
Self {
ctb_width,
ctb_height,
col_bd,
row_bd,
ctb_addr_ts_to_rs,
ctb_addr_rs_to_ts,
}
}
#[inline]
pub fn num_cols(&self) -> u32 {
self.col_bd.len() as u32 - 1
}
#[inline]
pub fn num_rows(&self) -> u32 {
self.row_bd.len() as u32 - 1
}
#[inline]
pub fn num_tiles(&self) -> u32 {
self.num_cols() * self.num_rows()
}
#[inline]
pub fn is_single(&self) -> bool {
self.num_tiles() == 1
}
#[inline]
pub fn tile_col_of_ctb(&self, x_ctb: u32) -> u32 {
(0..self.num_cols())
.find(|&i| x_ctb < self.col_bd[i as usize + 1])
.unwrap_or(self.num_cols() - 1)
}
#[inline]
pub fn tile_row_of_ctb(&self, y_ctb: u32) -> u32 {
(0..self.num_rows())
.find(|&i| y_ctb < self.row_bd[i as usize + 1])
.unwrap_or(self.num_rows() - 1)
}
#[inline]
pub fn tile_id_of_ctb(&self, x_ctb: u32, y_ctb: u32) -> u32 {
self.tile_row_of_ctb(y_ctb) * self.num_cols() + self.tile_col_of_ctb(x_ctb)
}
#[inline]
pub fn same_tile_ctb(&self, ax_ctb: u32, ay_ctb: u32, bx_ctb: u32, by_ctb: u32) -> bool {
self.tile_col_of_ctb(ax_ctb) == self.tile_col_of_ctb(bx_ctb)
&& self.tile_row_of_ctb(ay_ctb) == self.tile_row_of_ctb(by_ctb)
}
#[inline]
pub fn same_tile_px(&self, ax: u32, ay: u32, bx: u32, by: u32, ctb_log2: u8) -> bool {
if self.is_single() {
return true;
}
self.same_tile_ctb(
ax >> ctb_log2,
ay >> ctb_log2,
bx >> ctb_log2,
by >> ctb_log2,
)
}
#[inline]
pub fn tile_plane_bounds(
&self,
px: u32,
py: u32,
ctb_log2: u8,
shift_x: u8,
shift_y: u8,
) -> (u32, u32, u32, u32) {
let xc = (px << shift_x) >> ctb_log2;
let yc = (py << shift_y) >> ctb_log2;
let col = self.tile_col_of_ctb(xc) as usize;
let row = self.tile_row_of_ctb(yc) as usize;
let cl = ctb_log2 - shift_x;
let rl = ctb_log2 - shift_y;
(
self.col_bd[col] << cl,
self.row_bd[row] << rl,
self.col_bd[col + 1] << cl,
self.row_bd[row + 1] << rl,
)
}
pub fn tile_ctb_bounds(&self, tile_idx: u32) -> (u32, u32, u32, u32) {
let col = tile_idx % self.num_cols();
let row = tile_idx / self.num_cols();
(
self.col_bd[col as usize],
self.row_bd[row as usize],
self.col_bd[col as usize + 1],
self.row_bd[row as usize + 1],
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn single_tile_is_identity() {
let g = TileGrid::single(5, 4);
assert_eq!(g.num_tiles(), 1);
for rs in 0..20u32 {
assert_eq!(g.ctb_addr_rs_to_ts[rs as usize], rs);
assert_eq!(g.ctb_addr_ts_to_rs[rs as usize], rs);
}
}
#[test]
fn uniform_2x2_scan_and_membership() {
let g = TileGrid::from_sizes(4, 4, &[2, 2], &[2, 2]);
assert_eq!(g.num_tiles(), 4);
assert_eq!(g.ctb_addr_rs_to_ts[0], 0); assert_eq!(g.ctb_addr_rs_to_ts[1], 1); assert_eq!(g.ctb_addr_rs_to_ts[4], 2); assert_eq!(g.ctb_addr_rs_to_ts[5], 3); assert_eq!(g.ctb_addr_rs_to_ts[2], 4);
for rs in 0..16u32 {
let ts = g.ctb_addr_rs_to_ts[rs as usize];
assert_eq!(g.ctb_addr_ts_to_rs[ts as usize], rs);
}
assert!(!g.same_tile_ctb(1, 0, 2, 0));
assert!(g.same_tile_ctb(0, 0, 1, 1));
assert_eq!(g.tile_ctb_bounds(3), (2, 2, 4, 4));
}
#[test]
fn uniform_spacing_uneven() {
let ti = TileInfo {
num_tile_columns_minus1: 1,
num_tile_rows_minus1: 0,
uniform_spacing_flag: true,
column_widths: vec![],
row_heights: vec![],
loop_filter_across_tiles_enabled_flag: true,
};
let g = TileGrid::from_tile_info(5, 1, &ti);
assert_eq!(g.col_bd, vec![0, 2, 5]);
}
}