-
Notifications
You must be signed in to change notification settings - Fork 252
/
tiler.rs
660 lines (567 loc) · 21.6 KB
/
tiler.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// 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::util::*;
use std::marker::PhantomData;
pub const MAX_TILE_WIDTH: usize = 4096;
pub const MAX_TILE_AREA: usize = 4096 * 2304;
pub const MAX_TILE_COLS: usize = 64;
pub const MAX_TILE_ROWS: usize = 64;
/// Tiling information
///
/// This stores everything necessary to split a frame into tiles, and write
/// headers fields into the bitstream.
///
/// The method tile_iter_mut() actually provides tiled views of FrameState
/// and FrameBlocks.
#[derive(Debug, Clone, Copy)]
pub struct TilingInfo {
pub frame_width: usize,
pub frame_height: usize,
pub tile_width_sb: usize,
pub tile_height_sb: usize,
pub cols: usize, // number of columns of tiles within the whole frame
pub rows: usize, // number of rows of tiles within the whole frame
pub tile_cols_log2: usize,
pub tile_rows_log2: usize,
pub min_tile_cols_log2: usize,
pub max_tile_cols_log2: usize,
pub min_tile_rows_log2: usize,
pub max_tile_rows_log2: usize,
pub sb_size_log2: usize,
}
impl TilingInfo {
pub fn new(
sb_size_log2: usize,
frame_width: usize,
frame_height: usize,
tile_cols_log2: usize,
tile_rows_log2: usize,
) -> Self {
// <https://aomediacodec.github.io/av1-spec/#tile-info-syntax>
// Frame::new() aligns to the next multiple of 8
let frame_width = frame_width.align_power_of_two(3);
let frame_height = frame_height.align_power_of_two(3);
let frame_width_sb =
frame_width.align_power_of_two_and_shift(sb_size_log2);
let frame_height_sb =
frame_height.align_power_of_two_and_shift(sb_size_log2);
let sb_cols = frame_width.align_power_of_two_and_shift(sb_size_log2);
let sb_rows = frame_height.align_power_of_two_and_shift(sb_size_log2);
let max_tile_width_sb = MAX_TILE_WIDTH >> sb_size_log2;
let max_tile_area_sb = MAX_TILE_AREA >> (2 * sb_size_log2);
let min_tile_cols_log2 = Self::tile_log2(max_tile_width_sb, sb_cols);
let max_tile_cols_log2 = Self::tile_log2(1, sb_cols.min(MAX_TILE_COLS));
let max_tile_rows_log2 = Self::tile_log2(1, sb_rows.min(MAX_TILE_ROWS));
let min_tiles_log2 = min_tile_cols_log2
.max(Self::tile_log2(max_tile_area_sb, sb_cols * sb_rows));
let tile_cols_log2 =
tile_cols_log2.max(min_tile_cols_log2).min(max_tile_cols_log2);
let tile_width_sb = sb_cols.align_power_of_two_and_shift(tile_cols_log2);
let min_tile_rows_log2 = if min_tiles_log2 > tile_cols_log2 {
min_tiles_log2 - tile_cols_log2
} else {
0
};
let tile_rows_log2 =
tile_rows_log2.max(min_tile_rows_log2).min(max_tile_rows_log2);
let tile_height_sb = sb_rows.align_power_of_two_and_shift(tile_rows_log2);
let cols = (frame_width_sb + tile_width_sb - 1) / tile_width_sb;
let rows = (frame_height_sb + tile_height_sb - 1) / tile_height_sb;
Self {
frame_width,
frame_height,
tile_width_sb,
tile_height_sb,
cols,
rows,
tile_cols_log2,
tile_rows_log2,
min_tile_cols_log2,
max_tile_cols_log2,
min_tile_rows_log2,
max_tile_rows_log2,
sb_size_log2,
}
}
/// Return the smallest value for `k` such that `blkSize << k` is greater than
/// or equal to `target`.
///
/// <https://aomediacodec.github.io/av1-spec/#tile-size-calculation-function>
fn tile_log2(blk_size: usize, target: usize) -> usize {
let mut k = 0;
while (blk_size << k) < target {
k += 1;
}
k
}
#[inline(always)]
pub fn tile_count(&self) -> usize {
self.cols * self.rows
}
/// Split frame-level structures into tiles
///
/// Provide mutable tiled views of frame-level structures.
pub fn tile_iter_mut<'a, 'b, T: Pixel>(
&self,
fs: &'a mut FrameState<T>,
fb: &'b mut FrameBlocks,
) -> TileContextIterMut<'a, 'b, T> {
TileContextIterMut { ti: *self, fs, fb, next: 0, phantom: PhantomData }
}
}
/// Container for all tiled views
pub struct TileContextMut<'a, 'b, T: Pixel> {
pub ts: TileStateMut<'a, T>,
pub tb: TileBlocksMut<'b>,
}
/// Iterator over tiled views
pub struct TileContextIterMut<'a, 'b, T: Pixel> {
ti: TilingInfo,
fs: *mut FrameState<T>,
fb: *mut FrameBlocks,
next: usize,
phantom: PhantomData<(&'a mut FrameState<T>, &'b mut FrameBlocks)>,
}
impl<'a, 'b, T: Pixel> Iterator for TileContextIterMut<'a, 'b, T> {
type Item = TileContextMut<'a, 'b, T>;
fn next(&mut self) -> Option<Self::Item> {
if self.next < self.ti.rows * self.ti.cols {
let tile_col = self.next % self.ti.cols;
let tile_row = self.next / self.ti.cols;
let ctx = TileContextMut {
ts: {
let fs = unsafe { &mut *self.fs };
let sbo = SuperBlockOffset {
x: tile_col * self.ti.tile_width_sb,
y: tile_row * self.ti.tile_height_sb,
};
let x = sbo.x << self.ti.sb_size_log2;
let y = sbo.y << self.ti.sb_size_log2;
let tile_width = self.ti.tile_width_sb << self.ti.sb_size_log2;
let tile_height = self.ti.tile_height_sb << self.ti.sb_size_log2;
let width = tile_width.min(self.ti.frame_width - x);
let height = tile_height.min(self.ti.frame_height - y);
TileStateMut::new(fs, sbo, self.ti.sb_size_log2, width, height)
},
tb: {
let fb = unsafe { &mut *self.fb };
let tile_width_mi =
self.ti.tile_width_sb << (self.ti.sb_size_log2 - MI_SIZE_LOG2);
let tile_height_mi =
self.ti.tile_height_sb << (self.ti.sb_size_log2 - MI_SIZE_LOG2);
let x = tile_col * tile_width_mi;
let y = tile_row * tile_height_mi;
let cols = tile_width_mi.min(fb.cols - x);
let rows = tile_height_mi.min(fb.rows - y);
TileBlocksMut::new(fb, x, y, cols, rows)
},
};
self.next += 1;
Some(ctx)
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.ti.cols * self.ti.rows - self.next;
(remaining, Some(remaining))
}
}
impl<T: Pixel> ExactSizeIterator for TileContextIterMut<'_, '_, T> {}
#[cfg(test)]
pub mod test {
use super::*;
use crate::api::*;
use crate::lrf::*;
use crate::partition::*;
#[test]
fn test_tiling_info_from_tile_count() {
let sb_size_log2 = 6;
let (width, height) = (160, 144);
let ti = TilingInfo::new(sb_size_log2, width, height, 0, 0);
assert_eq!(1, ti.cols);
assert_eq!(1, ti.rows);
assert_eq!(3, ti.tile_width_sb);
assert_eq!(3, ti.tile_height_sb);
let ti = TilingInfo::new(sb_size_log2, width, height, 1, 1);
assert_eq!(2, ti.cols);
assert_eq!(2, ti.rows);
assert_eq!(2, ti.tile_width_sb);
assert_eq!(2, ti.tile_height_sb);
let ti = TilingInfo::new(sb_size_log2, width, height, 2, 2);
assert_eq!(3, ti.cols);
assert_eq!(3, ti.rows);
assert_eq!(1, ti.tile_width_sb);
assert_eq!(1, ti.tile_height_sb);
// cannot split more than superblocks
let ti = TilingInfo::new(sb_size_log2, width, height, 10, 8);
assert_eq!(3, ti.cols);
assert_eq!(3, ti.rows);
assert_eq!(1, ti.tile_width_sb);
assert_eq!(1, ti.tile_height_sb);
let ti = TilingInfo::new(sb_size_log2, 1024, 1024, 0, 0);
assert_eq!(1, ti.cols);
assert_eq!(1, ti.rows);
assert_eq!(16, ti.tile_width_sb);
assert_eq!(16, ti.tile_height_sb);
}
fn create_frame_invariants(
width: usize,
height: usize,
chroma_sampling: ChromaSampling,
) -> FrameInvariants<u16> {
// FrameInvariants aligns to the next multiple of 8, so using other values could make tests confusing
assert!(width & 7 == 0);
assert!(height & 7 == 0);
let config = EncoderConfig {
width,
height,
bit_depth: 8,
chroma_sampling,
..Default::default()
};
let sequence = Sequence::new(&config);
FrameInvariants::new(config, sequence)
}
#[test]
fn test_tile_iter_len() {
let fi = create_frame_invariants(160, 144, ChromaSampling::Cs420);
let mut fs = FrameState::new(&fi);
// frame size 160x144, 40x36 in 4x4-blocks
let mut fb = FrameBlocks::new(fi.w_in_b, fi.h_in_b);
{
// 2x2 tiles
let ti = TilingInfo::new(fi.sb_size_log2(), fi.width, fi.height, 1, 1);
let mut iter = ti.tile_iter_mut(&mut fs, &mut fb);
assert_eq!(4, iter.len());
assert!(iter.next().is_some());
assert_eq!(3, iter.len());
assert!(iter.next().is_some());
assert_eq!(2, iter.len());
assert!(iter.next().is_some());
assert_eq!(1, iter.len());
assert!(iter.next().is_some());
assert_eq!(0, iter.len());
assert!(iter.next().is_none());
}
{
// 4x4 tiles requested, will actually get 3x3 tiles
let ti = TilingInfo::new(fi.sb_size_log2(), fi.width, fi.height, 2, 2);
let mut iter = ti.tile_iter_mut(&mut fs, &mut fb);
assert_eq!(9, iter.len());
assert!(iter.next().is_some());
assert_eq!(8, iter.len());
assert!(iter.next().is_some());
assert_eq!(7, iter.len());
assert!(iter.next().is_some());
assert_eq!(6, iter.len());
assert!(iter.next().is_some());
assert_eq!(5, iter.len());
assert!(iter.next().is_some());
assert_eq!(4, iter.len());
assert!(iter.next().is_some());
assert_eq!(3, iter.len());
assert!(iter.next().is_some());
assert_eq!(2, iter.len());
assert!(iter.next().is_some());
assert_eq!(1, iter.len());
assert!(iter.next().is_some());
assert_eq!(0, iter.len());
assert!(iter.next().is_none());
}
}
#[inline]
fn rect<T: Pixel>(
region: &PlaneRegionMut<'_, T>,
) -> (isize, isize, usize, usize) {
let &Rect { x, y, width, height } = region.rect();
(x, y, width, height)
}
#[test]
fn test_tile_area() {
let fi = create_frame_invariants(160, 144, ChromaSampling::Cs420);
let mut fs = FrameState::new(&fi);
let mut fb = FrameBlocks::new(fi.w_in_b, fi.h_in_b);
// 4x4 tiles requested, will actually get 3x3 tiles
let ti = TilingInfo::new(fi.sb_size_log2(), fi.width, fi.height, 2, 2);
let iter = ti.tile_iter_mut(&mut fs, &mut fb);
let tile_states = iter.map(|ctx| ctx.ts).collect::<Vec<_>>();
// the frame must be split into 9 tiles:
//
// luma (Y) chroma (U) chroma (V)
// 64x64 64x64 32x64 32x32 32x32 16x32 32x32 32x32 16x32
// 64x64 64x64 32x64 32x32 32x32 16x32 32x32 32x32 16x32
// 64x16 64x16 32x16 32x 8 32x 8 16x 8 32x 8 32x 8 16x 8
assert_eq!(9, tile_states.len());
let tile = &tile_states[0].rec; // the top-left tile
assert_eq!((0, 0, 64, 64), rect(&tile.planes[0]));
assert_eq!((0, 0, 32, 32), rect(&tile.planes[1]));
assert_eq!((0, 0, 32, 32), rect(&tile.planes[2]));
let tile = &tile_states[1].rec; // the top-middle tile
assert_eq!((64, 0, 64, 64), rect(&tile.planes[0]));
assert_eq!((32, 0, 32, 32), rect(&tile.planes[1]));
assert_eq!((32, 0, 32, 32), rect(&tile.planes[2]));
let tile = &tile_states[2].rec; // the top-right tile
assert_eq!((128, 0, 32, 64), rect(&tile.planes[0]));
assert_eq!((64, 0, 16, 32), rect(&tile.planes[1]));
assert_eq!((64, 0, 16, 32), rect(&tile.planes[2]));
let tile = &tile_states[3].rec; // the middle-left tile
assert_eq!((0, 64, 64, 64), rect(&tile.planes[0]));
assert_eq!((0, 32, 32, 32), rect(&tile.planes[1]));
assert_eq!((0, 32, 32, 32), rect(&tile.planes[2]));
let tile = &tile_states[4].rec; // the center tile
assert_eq!((64, 64, 64, 64), rect(&tile.planes[0]));
assert_eq!((32, 32, 32, 32), rect(&tile.planes[1]));
assert_eq!((32, 32, 32, 32), rect(&tile.planes[2]));
let tile = &tile_states[5].rec; // the middle-right tile
assert_eq!((128, 64, 32, 64), rect(&tile.planes[0]));
assert_eq!((64, 32, 16, 32), rect(&tile.planes[1]));
assert_eq!((64, 32, 16, 32), rect(&tile.planes[2]));
let tile = &tile_states[6].rec; // the bottom-left tile
assert_eq!((0, 128, 64, 16), rect(&tile.planes[0]));
assert_eq!((0, 64, 32, 8), rect(&tile.planes[1]));
assert_eq!((0, 64, 32, 8), rect(&tile.planes[2]));
let tile = &tile_states[7].rec; // the bottom-middle tile
assert_eq!((64, 128, 64, 16), rect(&tile.planes[0]));
assert_eq!((32, 64, 32, 8), rect(&tile.planes[1]));
assert_eq!((32, 64, 32, 8), rect(&tile.planes[2]));
let tile = &tile_states[8].rec; // the bottom-right tile
assert_eq!((128, 128, 32, 16), rect(&tile.planes[0]));
assert_eq!((64, 64, 16, 8), rect(&tile.planes[1]));
assert_eq!((64, 64, 16, 8), rect(&tile.planes[2]));
}
#[inline]
fn b_area(region: &TileBlocksMut<'_>) -> (usize, usize, usize, usize) {
(region.x(), region.y(), region.cols(), region.rows())
}
#[test]
fn test_tile_blocks_area() {
let fi = create_frame_invariants(160, 144, ChromaSampling::Cs420);
let mut fs = FrameState::new(&fi);
let mut fb = FrameBlocks::new(fi.w_in_b, fi.h_in_b);
// 4x4 tiles requested, will actually get 3x3 tiles
let ti = TilingInfo::new(fi.sb_size_log2(), fi.width, fi.height, 2, 2);
let iter = ti.tile_iter_mut(&mut fs, &mut fb);
let tbs = iter.map(|ctx| ctx.tb).collect::<Vec<_>>();
// the FrameBlocks must be split into 9 TileBlocks:
//
// 16x16 16x16 8x16
// 16x16 16x16 8x16
// 16x 4 16x4 8x 4
assert_eq!(9, tbs.len());
assert_eq!((0, 0, 16, 16), b_area(&tbs[0]));
assert_eq!((16, 0, 16, 16), b_area(&tbs[1]));
assert_eq!((32, 0, 8, 16), b_area(&tbs[2]));
assert_eq!((0, 16, 16, 16), b_area(&tbs[3]));
assert_eq!((16, 16, 16, 16), b_area(&tbs[4]));
assert_eq!((32, 16, 8, 16), b_area(&tbs[5]));
assert_eq!((0, 32, 16, 4), b_area(&tbs[6]));
assert_eq!((16, 32, 16, 4), b_area(&tbs[7]));
assert_eq!((32, 32, 8, 4), b_area(&tbs[8]));
}
#[test]
fn test_tile_write() {
let fi = create_frame_invariants(160, 144, ChromaSampling::Cs420);
let mut fs = FrameState::new(&fi);
let mut fb = FrameBlocks::new(fi.w_in_b, fi.h_in_b);
{
// 4x4 tiles requested, will actually get 3x3 tiles
let ti = TilingInfo::new(fi.sb_size_log2(), fi.width, fi.height, 2, 2);
let iter = ti.tile_iter_mut(&mut fs, &mut fb);
let mut tile_states = iter.map(|ctx| ctx.ts).collect::<Vec<_>>();
{
// row 12 of Y-plane of the top-left tile
let tile_plane = &mut tile_states[0].rec.planes[0];
let row = &mut tile_plane[12];
assert_eq!(64, row.len());
&mut row[35..41].copy_from_slice(&[4, 42, 12, 18, 15, 31]);
}
{
// row 8 of U-plane of the middle-right tile
let tile_plane = &mut tile_states[5].rec.planes[1];
let row = &mut tile_plane[8];
assert_eq!(16, row.len());
&mut row[..4].copy_from_slice(&[14, 121, 1, 3]);
}
{
// row 1 of V-plane of the bottom-middle tile
let tile_plane = &mut tile_states[7].rec.planes[2];
let row = &mut tile_plane[1];
assert_eq!(32, row.len());
&mut row[11..16].copy_from_slice(&[6, 5, 2, 11, 8]);
}
}
// check that writes on tiles correctly affected the underlying frame
let plane = &fs.rec.planes[0];
let y = plane.cfg.yorigin + 12;
let x = plane.cfg.xorigin + 35;
let idx = y * plane.cfg.stride + x;
assert_eq!(&[4, 42, 12, 18, 15, 31], &plane.data[idx..idx + 6]);
let plane = &fs.rec.planes[1];
let offset = (64, 32); // middle-right tile, chroma plane
let y = plane.cfg.yorigin + offset.1 + 8;
let x = plane.cfg.xorigin + offset.0;
let idx = y * plane.cfg.stride + x;
assert_eq!(&[14, 121, 1, 3], &plane.data[idx..idx + 4]);
let plane = &fs.rec.planes[2];
let offset = (32, 64); // bottom-middle tile, chroma plane
let y = plane.cfg.yorigin + offset.1 + 1;
let x = plane.cfg.xorigin + offset.0 + 11;
let idx = y * plane.cfg.stride + x;
assert_eq!(&[6, 5, 2, 11, 8], &plane.data[idx..idx + 5]);
}
#[test]
fn test_tile_restoration_edges() {
let fi = create_frame_invariants(64, 80, ChromaSampling::Cs420);
let mut fs = FrameState::new(&fi);
let mut fb = FrameBlocks::new(fi.w_in_b, fi.h_in_b);
let ti = TilingInfo::new(fi.sb_size_log2(), fi.width, fi.height, 2, 2);
let iter = ti.tile_iter_mut(&mut fs, &mut fb);
let mut tile_states = iter.map(|ctx| ctx.ts).collect::<Vec<_>>();
assert_eq!(tile_states.len(), 2);
{
let trs = &mut tile_states[0].restoration;
let units = &trs.planes[0].units;
assert_eq!(units.x(), 0);
assert_eq!(units.y(), 0);
assert_eq!(units.cols(), 1);
assert_eq!(units.rows(), 1);
}
{
let trs = &mut tile_states[1].restoration;
let units = &trs.planes[0].units;
assert_eq!(units.x(), 0);
assert_eq!(units.y(), 1);
// no units, the tile is too small (less than 1/2 super-block)
assert_eq!(units.cols() * units.rows(), 0);
}
}
#[test]
fn test_tile_restoration_write() {
let fi = create_frame_invariants(256, 256, ChromaSampling::Cs420);
let mut fs = FrameState::new(&fi);
let mut fb = FrameBlocks::new(fi.w_in_b, fi.h_in_b);
{
// 2x2 tiles, each one containing 2×2 restoration units (1 super-block per restoration unit)
let ti = TilingInfo::new(fi.sb_size_log2(), fi.width, fi.height, 1, 1);
let iter = ti.tile_iter_mut(&mut fs, &mut fb);
let mut tile_states = iter.map(|ctx| ctx.ts).collect::<Vec<_>>();
{
// unit (1, 0) of Y-plane of the top-left tile
let units = &mut tile_states[0].restoration.planes[0].units;
units[0][1].filter =
RestorationFilter::Wiener { coeffs: [[1, 2, 3], [4, 5, 6]] };
}
{
// unit (0, 1) of U-plane of the bottom-right tile
let units = &mut tile_states[3].restoration.planes[1].units;
units[1][0].filter =
RestorationFilter::Sgrproj { set: 42, xqd: [10, 20] };
}
{
// unit (1, 1) of V-plane of the bottom-left tile
let units = &mut tile_states[2].restoration.planes[2].units;
units[1][1].filter =
RestorationFilter::Sgrproj { set: 5, xqd: [1, 2] };
}
}
// check that writes on tiles correctly affected the underlying restoration units
let units = &mut fs.restoration.planes[0].units;
assert_eq!(
units[0][1].filter,
RestorationFilter::Wiener { coeffs: [[1, 2, 3], [4, 5, 6]] }
);
let units = &mut fs.restoration.planes[1].units;
assert_eq!(
units[3][2].filter,
RestorationFilter::Sgrproj { set: 42, xqd: [10, 20] }
);
let units = &mut fs.restoration.planes[2].units;
assert_eq!(
units[3][1].filter,
RestorationFilter::Sgrproj { set: 5, xqd: [1, 2] }
);
}
#[test]
fn test_tile_motion_vectors_write() {
let fi = create_frame_invariants(160, 144, ChromaSampling::Cs420);
let mut fs = FrameState::new(&fi);
let mut fb = FrameBlocks::new(fi.w_in_b, fi.h_in_b);
{
// 4x4 tiles requested, will actually get 3x3 tiles
let ti = TilingInfo::new(fi.sb_size_log2(), fi.width, fi.height, 2, 2);
let iter = ti.tile_iter_mut(&mut fs, &mut fb);
let mut tile_states = iter.map(|ctx| ctx.ts).collect::<Vec<_>>();
{
// block (8, 5) of the top-left tile (of the first ref frame)
let mvs = &mut tile_states[0].mvs[0];
mvs[5][8] = MotionVector { col: 42, row: 38 };
println!("{:?}", mvs[5][8]);
}
{
// block (4, 2) of the middle-right tile (of ref frame 2)
let mvs = &mut tile_states[5].mvs[2];
mvs[2][3] = MotionVector { col: 2, row: 14 };
}
}
// check that writes on tiled views affected the underlying motion vectors
let mvs = &fs.frame_mvs[0];
assert_eq!(MotionVector { col: 42, row: 38 }, mvs[5][8]);
let mvs = &fs.frame_mvs[2];
let mix = (128 >> MI_SIZE_LOG2) + 3;
let miy = (64 >> MI_SIZE_LOG2) + 2;
assert_eq!(MotionVector { col: 2, row: 14 }, mvs[miy][mix]);
}
#[test]
fn test_tile_blocks_write() {
let fi = create_frame_invariants(160, 144, ChromaSampling::Cs420);
let mut fs = FrameState::new(&fi);
let mut fb = FrameBlocks::new(fi.w_in_b, fi.h_in_b);
{
// 4x4 tiles requested, will actually get 3x3 tiles
let ti = TilingInfo::new(fi.sb_size_log2(), fi.width, fi.height, 2, 2);
let iter = ti.tile_iter_mut(&mut fs, &mut fb);
let mut tbs = iter.map(|ctx| ctx.tb).collect::<Vec<_>>();
{
// top-left tile
let tb = &mut tbs[0];
// block (4, 3)
tb[3][4].n4_w = 42;
// block (8, 5)
tb[5][8].segmentation_idx = 14;
}
{
// middle-right tile
let tb = &mut tbs[5];
// block (0, 1)
tb[1][0].n4_h = 11;
// block (7, 5)
tb[5][7].cdef_index = 3;
}
{
// bottom-middle tile
let tb = &mut tbs[7];
// block (3, 2)
tb[2][3].mode = PredictionMode::PAETH_PRED;
// block (1, 1)
tb[1][1].n4_w = 8;
}
}
// check that writes on tiles correctly affected the underlying blocks
assert_eq!(42, fb[3][4].n4_w);
assert_eq!(14, fb[5][8].segmentation_idx);
assert_eq!(11, fb[17][32].n4_h);
assert_eq!(3, fb[21][39].cdef_index);
assert_eq!(PredictionMode::PAETH_PRED, fb[34][19].mode);
assert_eq!(8, fb[33][17].n4_w);
}
}