-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathtile.rs
161 lines (146 loc) · 4.19 KB
/
tile.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 2019, The rav1e contributors. All rights reserved
//
// This source code is subject to the terms of the BSD 2 Clause License and
// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
// was not distributed with this source code in the LICENSE file, you can
// obtain it at www.aomedia.org/license/software. If the Alliance for Open
// Media Patent License 1.0 was not distributed with this source code in the
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
use super::*;
use crate::context::*;
use crate::encoder::*;
use crate::plane::*;
use crate::util::*;
/// Rectangle of a tile, in pixels
///
/// This is similar to Rect, but with unsigned (x, y) for convenience.
#[derive(Debug, Clone, Copy)]
pub struct TileRect {
pub x: usize,
pub y: usize,
pub width: usize,
pub height: usize,
}
impl TileRect {
#[inline(always)]
pub fn decimated(&self, xdec: usize, ydec: usize) -> Self {
Self {
x: self.x >> xdec,
y: self.y >> ydec,
width: self.width >> xdec,
height: self.height >> ydec,
}
}
#[inline(always)]
pub fn to_frame_plane_offset(&self, tile_po: PlaneOffset) -> PlaneOffset {
PlaneOffset {
x: self.x as isize + tile_po.x,
y: self.y as isize + tile_po.y,
}
}
#[inline(always)]
pub fn to_frame_block_offset(
&self,
tile_bo: BlockOffset,
xdec: usize,
ydec: usize,
) -> BlockOffset {
debug_assert!(self.x as usize % (MI_SIZE >> xdec) == 0);
debug_assert!(self.y as usize % (MI_SIZE >> ydec) == 0);
let bx = self.x >> (MI_SIZE_LOG2 - xdec);
let by = self.y >> (MI_SIZE_LOG2 - ydec);
BlockOffset {
x: bx + tile_bo.x,
y: by + tile_bo.y,
}
}
#[inline(always)]
pub fn to_frame_super_block_offset(
&self,
tile_sbo: SuperBlockOffset,
sb_size_log2: usize,
xdec: usize,
ydec: usize,
) -> SuperBlockOffset {
debug_assert!(sb_size_log2 == 6 || sb_size_log2 == 7);
debug_assert!(self.x as usize % (1 << (sb_size_log2 - xdec)) == 0);
debug_assert!(self.y as usize % (1 << (sb_size_log2 - ydec)) == 0);
let sbx = self.x as usize >> (sb_size_log2 - xdec);
let sby = self.y as usize >> (sb_size_log2 - ydec);
SuperBlockOffset {
x: sbx + tile_sbo.x,
y: sby + tile_sbo.y,
}
}
}
impl From<TileRect> for Rect {
#[inline(always)]
fn from(tile_rect: TileRect) -> Rect {
Rect {
x: tile_rect.x as isize,
y: tile_rect.y as isize,
width: tile_rect.width,
height: tile_rect.height,
}
}
}
/// Tiled view of a frame
#[derive(Debug)]
pub struct Tile<'a, T: Pixel> {
pub planes: [PlaneRegion<'a, T>; PLANES],
}
/// Mutable tiled view of a frame
#[derive(Debug)]
pub struct TileMut<'a, T: Pixel> {
pub planes: [PlaneRegionMut<'a, T>; PLANES],
}
// common impl for Tile and TileMut
macro_rules! tile_common {
// $name: Tile or TileMut
// $pr_type: PlaneRegion or PlaneRegionMut
// $iter: iter or iter_mut
//opt_mut: nothing or mut
($name:ident, $pr_type:ident, $iter:ident $(,$opt_mut:tt)?) => {
impl<'a, T: Pixel> $name<'a, T> {
#[inline(always)]
pub fn new(
frame: &'a $($opt_mut)? Frame<T>,
luma_rect: TileRect,
) -> Self {
let mut planes_iter = frame.planes.$iter();
Self {
planes: [
{
let plane = planes_iter.next().unwrap();
$pr_type::new(plane, luma_rect.into())
},
{
let plane = planes_iter.next().unwrap();
let rect = luma_rect.decimated(plane.cfg.xdec, plane.cfg.ydec);
$pr_type::new(plane, rect.into())
},
{
let plane = planes_iter.next().unwrap();
let rect = luma_rect.decimated(plane.cfg.xdec, plane.cfg.ydec);
$pr_type::new(plane, rect.into())
},
],
}
}
}
}
}
tile_common!(Tile, PlaneRegion, iter);
tile_common!(TileMut, PlaneRegionMut, iter_mut, mut);
impl<'a, T: Pixel> TileMut<'a, T> {
#[inline(always)]
pub fn as_const(&self) -> Tile<'_, T> {
Tile {
planes: [
self.planes[0].as_const(),
self.planes[1].as_const(),
self.planes[2].as_const(),
],
}
}
}