forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

mbaff.rs322 lines, 10.3 KiBHistoryRawDownload
1//! Macroblock-adaptive frame/field decoding (MBAFF): macroblock pair
2//! addressing and the neighbour derivation of clause 6.4.12.2 (Table 6-4).
3//!
4//! Layout. A frame picture with `MbaffFrameFlag` 1 codes macroblock *pairs*:
5//! macroblock address `2 * pair + b` (b = 0 top, 1 bottom) of pair
6//! `(px, pr)`. Every per-macroblock and per-4x4-block array of the
7//! [`Picture`] is indexed by *slot*: the pair's top macroblock lives at slot
8//! row `2 * pr` and its bottom macroblock at `2 * pr + 1`, whether the pair is
9//! a frame pair or a field pair. A macroblock's own blocks are therefore
10//! addressed exactly as in a progressive picture (block coordinates
11//! `(4 * px + i, 4 * slot_y + j)`), and everything that reads *neighbouring*
12//! data goes through [`locate`], the single implementation of Table 6-4.
13//!
14//! Samples are different: they live at their true picture position. A frame
15//! macroblock covers 16 consecutive rows of its pair; a field macroblock
16//! covers every second row of the 32-row pair, starting at row 0 (top) or 1
17//! (bottom), which a stride of two rows and a first-row offset express.
18
19use crate::dpb::Picture;
20use crate::sample::Sample;
21
22/// Where the macroblock being decoded sits in an MBAFF frame.
23#[derive(Clone, Copy, Default, Debug)]
24pub(crate) struct MbPos {
25 /// The picture is an MBAFF frame; every other field is meaningless when
26 /// this is false.
27 pub active: bool,
28 /// Macroblock column.
29 pub px: usize,
30 /// Slot row: `2 * pair_row + is_bottom`.
31 pub slot_y: usize,
32 /// The macroblock belongs to a field macroblock pair.
33 pub field: bool,
34 /// The slice that is decoding it (neighbours must belong to the same one).
35 pub slice_id: u16,
36}
37
38impl MbPos {
39 /// True for the second macroblock of its pair.
40 #[inline]
41 pub(crate) fn bottom(&self) -> bool {
42 self.slot_y & 1 == 1
43 }
44}
45
46/// A neighbouring luma (or chroma) location resolved by Table 6-4.
47#[derive(Clone, Copy, Debug)]
48pub(crate) struct Loc {
49 /// Slot (index into the per-macroblock arrays) of `mbAddrN`.
50 pub slot: usize,
51 /// `xW` and `yW` inside that macroblock.
52 pub xw: i32,
53 pub yw: i32,
54 /// `mbAddrN` is a field macroblock.
55 pub field: bool,
56}
57
58/// Which macroblock of the left pair a frame macroblock at the bottom of its
59/// pair takes as its upper-left neighbour when the left pair is a field pair.
60/// Table 6-4 reads `mbAddrA` (the top field macroblock) with
61/// `yM = (yN + maxH) >> 1`.
62const D_FRAME_BOTTOM_FIELD_LEFT_BOTTOM: bool = true;
63
64/// Clause 6.4.12.2: the macroblock and the position in it that cover the luma
65/// (`maxw = maxh = 16`) or chroma (`8`) location `(xn, yn)` relative to the
66/// current macroblock, or `None` when it is unavailable.
67pub(crate) fn locate<S: Sample>(
68 pic: &Picture<S>,
69 p: &MbPos,
70 xn: i32,
71 yn: i32,
72 maxw: i32,
73 maxh: i32,
74) -> Option<Loc> {
75 let w = pic.mb_w;
76 if xn >= 0 && xn < maxw && yn >= 0 && yn < maxh {
77 return Some(Loc {
78 slot: p.slot_y * w + p.px,
79 xw: xn,
80 yw: yn,
81 field: p.field,
82 });
83 }
84 if yn >= maxh || (xn >= maxw && yn >= 0) {
85 return None;
86 }
87 let pr = (p.slot_y / 2) as i32;
88 let bottom = p.bottom();
89 // The pair at column offset `dx`, row offset `dy`: its top slot and
90 // whether it is a field pair; None when it is outside the picture or in
91 // another slice (clause 6.4.8 availability, per pair in MBAFF frames).
92 let pair = |dx: i32, dy: i32| -> Option<(usize, bool)> {
93 let x = p.px as i32 + dx;
94 let y = pr + dy;
95 if x < 0 || y < 0 || x >= w as i32 {
96 return None;
97 }
98 let top = (2 * y as usize) * w + x as usize;
99 (pic.mb_slice[top] == p.slice_id).then(|| (top, pic.mb_field[top] != 0))
100 };
101 let xw = (xn + maxw).rem_euclid(maxw);
102 let mk = |top: usize, in_bottom: bool, ym: i32| -> Option<Loc> {
103 let slot = if in_bottom { top + w } else { top };
104 Some(Loc {
105 slot,
106 xw,
107 yw: ym.rem_euclid(maxh),
108 field: pic.mb_field[slot] != 0,
109 })
110 };
111 let cur = p.field;
112 if xn < 0 {
113 if yn >= 0 {
114 // A: the left pair.
115 let (t, xf) = pair(-1, 0)?;
116 return match (cur, bottom) {
117 (false, false) => {
118 if !xf {
119 mk(t, false, yn)
120 } else {
121 mk(t, yn % 2 != 0, yn >> 1)
122 }
123 }
124 (false, true) => {
125 if !xf {
126 mk(t, true, yn)
127 } else {
128 mk(t, yn % 2 != 0, (yn + maxh) >> 1)
129 }
130 }
131 (true, false) => {
132 if xf {
133 mk(t, false, yn)
134 } else if yn < maxh / 2 {
135 mk(t, false, yn << 1)
136 } else {
137 mk(t, true, (yn << 1) - maxh)
138 }
139 }
140 (true, true) => {
141 if xf {
142 mk(t, true, yn)
143 } else if yn < maxh / 2 {
144 mk(t, false, (yn << 1) + 1)
145 } else {
146 mk(t, true, (yn << 1) + 1 - maxh)
147 }
148 }
149 };
150 }
151 // D: above-left.
152 return match (cur, bottom) {
153 (false, false) => {
154 let (t, _) = pair(-1, -1)?;
155 mk(t, true, yn)
156 }
157 (false, true) => {
158 let (t, xf) = pair(-1, 0)?;
159 if !xf {
160 mk(t, false, yn)
161 } else {
162 mk(t, D_FRAME_BOTTOM_FIELD_LEFT_BOTTOM, (yn + maxh) >> 1)
163 }
164 }
165 (true, false) => {
166 let (t, xf) = pair(-1, -1)?;
167 if xf {
168 mk(t, false, yn)
169 } else {
170 mk(t, true, 2 * yn)
171 }
172 }
173 (true, true) => {
174 let (t, _) = pair(-1, -1)?;
175 mk(t, true, yn)
176 }
177 };
178 }
179 if xn < maxw {
180 // B: above.
181 return match (cur, bottom) {
182 (false, false) => {
183 let (t, _) = pair(0, -1)?;
184 mk(t, true, yn)
185 }
186 (false, true) => mk((p.slot_y - 1) * w + p.px, false, yn),
187 (true, false) => {
188 let (t, xf) = pair(0, -1)?;
189 if xf {
190 mk(t, false, yn)
191 } else {
192 mk(t, true, 2 * yn)
193 }
194 }
195 (true, true) => {
196 let (t, _) = pair(0, -1)?;
197 mk(t, true, yn)
198 }
199 };
200 }
201 // C: above-right.
202 match (cur, bottom) {
203 (false, false) => {
204 let (t, _) = pair(1, -1)?;
205 mk(t, true, yn)
206 }
207 (false, true) => None,
208 (true, false) => {
209 let (t, xf) = pair(1, -1)?;
210 if xf {
211 mk(t, false, yn)
212 } else {
213 mk(t, true, 2 * yn)
214 }
215 }
216 (true, true) => {
217 let (t, _) = pair(1, -1)?;
218 mk(t, true, yn)
219 }
220 }
221}
222
223/// The 4x4 luma block left of (`left`) or above (`!left`) the block at global
224/// block coordinates `(bx, by)` of the current macroblock, as global block
225/// coordinates plus the neighbouring macroblock's field flag.
226pub(crate) fn nb_blk4<S: Sample>(
227 pic: &Picture<S>,
228 p: &MbPos,
229 bx: usize,
230 by: usize,
231 left: bool,
232) -> Option<(usize, usize, bool)> {
233 let (lx, ly) = (bx - 4 * p.px, by - 4 * p.slot_y);
234 if left {
235 if lx > 0 {
236 return Some((bx - 1, by, p.field));
237 }
238 let l = locate(pic, p, -1, ly as i32 * 4, 16, 16)?;
239 return Some((
240 (l.slot % pic.mb_w) * 4 + (l.xw as usize >> 2),
241 (l.slot / pic.mb_w) * 4 + (l.yw as usize >> 2),
242 l.field,
243 ));
244 }
245 if ly > 0 {
246 return Some((bx, by - 1, p.field));
247 }
248 let l = locate(pic, p, lx as i32 * 4, -1, 16, 16)?;
249 Some((
250 (l.slot % pic.mb_w) * 4 + (l.xw as usize >> 2),
251 (l.slot / pic.mb_w) * 4 + (l.yw as usize >> 2),
252 l.field,
253 ))
254}
255
256/// The same for a chroma 4x4 block of the `(mb_w * 2) x (mb_h * cbh)` grid,
257/// where `cbh` is the number of chroma 4x4 block rows per macroblock (2 for
258/// 4:2:0, 4 for 4:2:2; the chroma macroblock is `8` wide and `4 * cbh` high).
259pub(crate) fn nb_blk_c<S: Sample>(
260 pic: &Picture<S>,
261 p: &MbPos,
262 cx: usize,
263 cy: usize,
264 left: bool,
265) -> Option<(usize, usize)> {
266 let cbh = pic.cmb_h / 4;
267 let maxh = (4 * cbh) as i32;
268 let (lx, ly) = (cx - 2 * p.px, cy - cbh * p.slot_y);
269 if left {
270 if lx > 0 {
271 return Some((cx - 1, cy));
272 }
273 let l = locate(pic, p, -1, ly as i32 * 4, 8, maxh)?;
274 return Some((
275 (l.slot % pic.mb_w) * 2 + (l.xw as usize >> 2),
276 (l.slot / pic.mb_w) * cbh + (l.yw as usize >> 2),
277 ));
278 }
279 if ly > 0 {
280 return Some((cx, cy - 1));
281 }
282 let l = locate(pic, p, lx as i32 * 4, -1, 8, maxh)?;
283 Some((
284 (l.slot % pic.mb_w) * 2 + (l.xw as usize >> 2),
285 (l.slot / pic.mb_w) * cbh + (l.yw as usize >> 2),
286 ))
287}
288
289/// Slot of the macroblock containing the neighbouring location, for the
290/// macroblock-level neighbours A (`(-1, 0)`) and B (`(0, -1)`) of 6.4.9.
291pub(crate) fn mb_a<S: Sample>(pic: &Picture<S>, p: &MbPos) -> Option<usize> {
292 locate(pic, p, -1, 0, 16, 16).map(|l| l.slot)
293}
294
295/// See [`mb_a`].
296pub(crate) fn mb_b<S: Sample>(pic: &Picture<S>, p: &MbPos) -> Option<usize> {
297 locate(pic, p, 0, -1, 16, 16).map(|l| l.slot)
298}
299
300/// 7.4.4 inference of `mb_field_decoding_flag` for a pair that does not code
301/// it: the left pair of the same slice, else the pair above, else frame.
302pub(crate) fn infer_field<S: Sample>(
303 pic: &Picture<S>,
304 px: usize,
305 pair_row: usize,
306 slice_id: u16,
307) -> bool {
308 let w = pic.mb_w;
309 if px > 0 {
310 let t = (2 * pair_row) * w + px - 1;
311 if pic.mb_slice[t] == slice_id {
312 return pic.mb_field[t] != 0;
313 }
314 }
315 if pair_row > 0 {
316 let t = (2 * (pair_row - 1)) * w + px;
317 if pic.mb_slice[t] == slice_id {
318 return pic.mb_field[t] != 0;
319 }
320 }
321 false
322}