-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathmc.rs
480 lines (446 loc) · 14.6 KB
/
mc.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
// Copyright (c) 2019-2022, 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.
cfg_if::cfg_if! {
if #[cfg(nasm_x86_64)] {
pub use crate::asm::x86::mc::*;
} else if #[cfg(asm_neon)] {
pub use crate::asm::aarch64::mc::*;
} else {
pub use self::rust::*;
}
}
use crate::cpu_features::CpuFeatureLevel;
use crate::frame::*;
use crate::tiling::*;
use crate::util::*;
use simd_helpers::cold_for_target_arch;
use std::ops;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct MotionVector {
pub row: i16,
pub col: i16,
}
impl MotionVector {
#[inline]
pub const fn quantize_to_fullpel(self) -> Self {
Self { row: (self.row / 8) * 8, col: (self.col / 8) * 8 }
}
#[inline]
pub const fn is_zero(self) -> bool {
self.row == 0 && self.col == 0
}
#[inline]
pub const fn is_valid(self) -> bool {
use crate::context::{MV_LOW, MV_UPP};
((MV_LOW as i16) < self.row && self.row < (MV_UPP as i16))
&& ((MV_LOW as i16) < self.col && self.col < (MV_UPP as i16))
}
}
impl ops::Mul<i16> for MotionVector {
type Output = MotionVector;
#[inline]
fn mul(self, rhs: i16) -> MotionVector {
MotionVector { row: self.row * rhs, col: self.col * rhs }
}
}
impl ops::Mul<u16> for MotionVector {
type Output = MotionVector;
#[inline]
fn mul(self, rhs: u16) -> MotionVector {
MotionVector { row: self.row * rhs as i16, col: self.col * rhs as i16 }
}
}
impl ops::Shr<u8> for MotionVector {
type Output = MotionVector;
#[inline]
fn shr(self, rhs: u8) -> MotionVector {
MotionVector { row: self.row >> rhs, col: self.col >> rhs }
}
}
impl ops::Shl<u8> for MotionVector {
type Output = MotionVector;
#[inline]
fn shl(self, rhs: u8) -> MotionVector {
MotionVector { row: self.row << rhs, col: self.col << rhs }
}
}
impl ops::Add<MotionVector> for MotionVector {
type Output = MotionVector;
#[inline]
fn add(self, rhs: MotionVector) -> MotionVector {
MotionVector { row: self.row + rhs.row, col: self.col + rhs.col }
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd)]
#[allow(unused)]
pub enum FilterMode {
REGULAR = 0,
SMOOTH = 1,
SHARP = 2,
BILINEAR = 3,
SWITCHABLE = 4,
}
pub const SUBPEL_FILTER_SIZE: usize = 8;
const SUBPEL_FILTERS: [[[i32; SUBPEL_FILTER_SIZE]; 16]; 6] = [
[
[0, 0, 0, 128, 0, 0, 0, 0],
[0, 2, -6, 126, 8, -2, 0, 0],
[0, 2, -10, 122, 18, -4, 0, 0],
[0, 2, -12, 116, 28, -8, 2, 0],
[0, 2, -14, 110, 38, -10, 2, 0],
[0, 2, -14, 102, 48, -12, 2, 0],
[0, 2, -16, 94, 58, -12, 2, 0],
[0, 2, -14, 84, 66, -12, 2, 0],
[0, 2, -14, 76, 76, -14, 2, 0],
[0, 2, -12, 66, 84, -14, 2, 0],
[0, 2, -12, 58, 94, -16, 2, 0],
[0, 2, -12, 48, 102, -14, 2, 0],
[0, 2, -10, 38, 110, -14, 2, 0],
[0, 2, -8, 28, 116, -12, 2, 0],
[0, 0, -4, 18, 122, -10, 2, 0],
[0, 0, -2, 8, 126, -6, 2, 0],
],
[
[0, 0, 0, 128, 0, 0, 0, 0],
[0, 2, 28, 62, 34, 2, 0, 0],
[0, 0, 26, 62, 36, 4, 0, 0],
[0, 0, 22, 62, 40, 4, 0, 0],
[0, 0, 20, 60, 42, 6, 0, 0],
[0, 0, 18, 58, 44, 8, 0, 0],
[0, 0, 16, 56, 46, 10, 0, 0],
[0, -2, 16, 54, 48, 12, 0, 0],
[0, -2, 14, 52, 52, 14, -2, 0],
[0, 0, 12, 48, 54, 16, -2, 0],
[0, 0, 10, 46, 56, 16, 0, 0],
[0, 0, 8, 44, 58, 18, 0, 0],
[0, 0, 6, 42, 60, 20, 0, 0],
[0, 0, 4, 40, 62, 22, 0, 0],
[0, 0, 4, 36, 62, 26, 0, 0],
[0, 0, 2, 34, 62, 28, 2, 0],
],
[
[0, 0, 0, 128, 0, 0, 0, 0],
[-2, 2, -6, 126, 8, -2, 2, 0],
[-2, 6, -12, 124, 16, -6, 4, -2],
[-2, 8, -18, 120, 26, -10, 6, -2],
[-4, 10, -22, 116, 38, -14, 6, -2],
[-4, 10, -22, 108, 48, -18, 8, -2],
[-4, 10, -24, 100, 60, -20, 8, -2],
[-4, 10, -24, 90, 70, -22, 10, -2],
[-4, 12, -24, 80, 80, -24, 12, -4],
[-2, 10, -22, 70, 90, -24, 10, -4],
[-2, 8, -20, 60, 100, -24, 10, -4],
[-2, 8, -18, 48, 108, -22, 10, -4],
[-2, 6, -14, 38, 116, -22, 10, -4],
[-2, 6, -10, 26, 120, -18, 8, -2],
[-2, 4, -6, 16, 124, -12, 6, -2],
[0, 2, -2, 8, 126, -6, 2, -2],
],
[
[0, 0, 0, 128, 0, 0, 0, 0],
[0, 0, 0, 120, 8, 0, 0, 0],
[0, 0, 0, 112, 16, 0, 0, 0],
[0, 0, 0, 104, 24, 0, 0, 0],
[0, 0, 0, 96, 32, 0, 0, 0],
[0, 0, 0, 88, 40, 0, 0, 0],
[0, 0, 0, 80, 48, 0, 0, 0],
[0, 0, 0, 72, 56, 0, 0, 0],
[0, 0, 0, 64, 64, 0, 0, 0],
[0, 0, 0, 56, 72, 0, 0, 0],
[0, 0, 0, 48, 80, 0, 0, 0],
[0, 0, 0, 40, 88, 0, 0, 0],
[0, 0, 0, 32, 96, 0, 0, 0],
[0, 0, 0, 24, 104, 0, 0, 0],
[0, 0, 0, 16, 112, 0, 0, 0],
[0, 0, 0, 8, 120, 0, 0, 0],
],
[
[0, 0, 0, 128, 0, 0, 0, 0],
[0, 0, -4, 126, 8, -2, 0, 0],
[0, 0, -8, 122, 18, -4, 0, 0],
[0, 0, -10, 116, 28, -6, 0, 0],
[0, 0, -12, 110, 38, -8, 0, 0],
[0, 0, -12, 102, 48, -10, 0, 0],
[0, 0, -14, 94, 58, -10, 0, 0],
[0, 0, -12, 84, 66, -10, 0, 0],
[0, 0, -12, 76, 76, -12, 0, 0],
[0, 0, -10, 66, 84, -12, 0, 0],
[0, 0, -10, 58, 94, -14, 0, 0],
[0, 0, -10, 48, 102, -12, 0, 0],
[0, 0, -8, 38, 110, -12, 0, 0],
[0, 0, -6, 28, 116, -10, 0, 0],
[0, 0, -4, 18, 122, -8, 0, 0],
[0, 0, -2, 8, 126, -4, 0, 0],
],
[
[0, 0, 0, 128, 0, 0, 0, 0],
[0, 0, 30, 62, 34, 2, 0, 0],
[0, 0, 26, 62, 36, 4, 0, 0],
[0, 0, 22, 62, 40, 4, 0, 0],
[0, 0, 20, 60, 42, 6, 0, 0],
[0, 0, 18, 58, 44, 8, 0, 0],
[0, 0, 16, 56, 46, 10, 0, 0],
[0, 0, 14, 54, 48, 12, 0, 0],
[0, 0, 12, 52, 52, 12, 0, 0],
[0, 0, 12, 48, 54, 14, 0, 0],
[0, 0, 10, 46, 56, 16, 0, 0],
[0, 0, 8, 44, 58, 18, 0, 0],
[0, 0, 6, 42, 60, 20, 0, 0],
[0, 0, 4, 40, 62, 22, 0, 0],
[0, 0, 4, 36, 62, 26, 0, 0],
[0, 0, 2, 34, 62, 30, 0, 0],
],
];
pub(crate) mod rust {
use super::*;
use num_traits::*;
unsafe fn run_filter<T: AsPrimitive<i32>>(
src: *const T, stride: usize, filter: [i32; 8],
) -> i32 {
filter
.iter()
.enumerate()
.map(|(i, f)| {
let p = src.add(i * stride);
f * (*p).as_()
})
.sum::<i32>()
}
fn get_filter(
mode: FilterMode, frac: i32, length: usize,
) -> [i32; SUBPEL_FILTER_SIZE] {
let filter_idx = if mode == FilterMode::BILINEAR || length > 4 {
mode as usize
} else {
(mode as usize).min(1) + 4
};
SUBPEL_FILTERS[filter_idx][frac as usize]
}
#[cold_for_target_arch("x86_64")]
pub fn put_8tap<T: Pixel>(
dst: &mut PlaneRegionMut<'_, T>, src: PlaneSlice<'_, T>, width: usize,
height: usize, col_frac: i32, row_frac: i32, mode_x: FilterMode,
mode_y: FilterMode, bit_depth: usize, _cpu: CpuFeatureLevel,
) {
// The assembly only supports even heights and valid uncropped widths
assert_eq!(height & 1, 0);
assert!(width.is_power_of_two() && (2..=128).contains(&width));
let ref_stride = src.plane.cfg.stride;
let y_filter = get_filter(mode_y, row_frac, height);
let x_filter = get_filter(mode_x, col_frac, width);
let max_sample_val = (1 << bit_depth) - 1;
let intermediate_bits = 4 - if bit_depth == 12 { 2 } else { 0 };
match (col_frac, row_frac) {
(0, 0) => {
for r in 0..height {
let src_slice = &src[r];
let dst_slice = &mut dst[r];
dst_slice[..width].copy_from_slice(&src_slice[..width]);
}
}
(0, _) => {
let offset_slice = src.go_up(3);
for r in 0..height {
let src_slice = &offset_slice[r];
let dst_slice = &mut dst[r];
for c in 0..width {
dst_slice[c] = T::cast_from(
round_shift(
// SAFETY: We pass this a raw pointer, but it's created from a
// checked slice, so we are safe.
unsafe {
run_filter(src_slice[c..].as_ptr(), ref_stride, y_filter)
},
7,
)
.clamp(0, max_sample_val),
);
}
}
}
(_, 0) => {
let offset_slice = src.go_left(3);
for r in 0..height {
let src_slice = &offset_slice[r];
let dst_slice = &mut dst[r];
for c in 0..width {
dst_slice[c] = T::cast_from(
round_shift(
round_shift(
// SAFETY: We pass this a raw pointer, but it's created from a
// checked slice, so we are safe.
unsafe { run_filter(src_slice[c..].as_ptr(), 1, x_filter) },
7 - intermediate_bits,
),
intermediate_bits,
)
.clamp(0, max_sample_val),
);
}
}
}
(_, _) => {
let mut intermediate: [i16; 8 * (128 + 7)] = [0; 8 * (128 + 7)];
let offset_slice = src.go_left(3).go_up(3);
for cg in (0..width).step_by(8) {
for r in 0..height + 7 {
let src_slice = &offset_slice[r];
for c in cg..(cg + 8).min(width) {
intermediate[8 * r + (c - cg)] = round_shift(
// SAFETY: We pass this a raw pointer, but it's created from a
// checked slice, so we are safe.
unsafe { run_filter(src_slice[c..].as_ptr(), 1, x_filter) },
7 - intermediate_bits,
) as i16;
}
}
for r in 0..height {
let dst_slice = &mut dst[r];
for c in cg..(cg + 8).min(width) {
dst_slice[c] = T::cast_from(
round_shift(
// SAFETY: We pass this a raw pointer, but it's created from a
// checked slice, so we are safe.
unsafe {
run_filter(
intermediate[8 * r + c - cg..].as_ptr(),
8,
y_filter,
)
},
7 + intermediate_bits,
)
.clamp(0, max_sample_val),
);
}
}
}
}
}
}
// HBD output interval is [-20588, 36956] (10-bit), [-20602, 36983] (12-bit)
// Subtract PREP_BIAS to ensure result fits in i16 and matches dav1d assembly
const PREP_BIAS: i32 = 8192;
#[cold_for_target_arch("x86_64")]
pub fn prep_8tap<T: Pixel>(
tmp: &mut [i16], src: PlaneSlice<'_, T>, width: usize, height: usize,
col_frac: i32, row_frac: i32, mode_x: FilterMode, mode_y: FilterMode,
bit_depth: usize, _cpu: CpuFeatureLevel,
) {
// The assembly only supports even heights and valid uncropped widths
assert_eq!(height & 1, 0);
assert!(width.is_power_of_two() && (2..=128).contains(&width));
let ref_stride = src.plane.cfg.stride;
let y_filter = get_filter(mode_y, row_frac, height);
let x_filter = get_filter(mode_x, col_frac, width);
let intermediate_bits = 4 - if bit_depth == 12 { 2 } else { 0 };
let prep_bias = if bit_depth == 8 { 0 } else { PREP_BIAS };
match (col_frac, row_frac) {
(0, 0) => {
for r in 0..height {
let src_slice = &src[r];
for c in 0..width {
tmp[r * width + c] = (i16::cast_from(src_slice[c])
<< intermediate_bits)
- prep_bias as i16;
}
}
}
(0, _) => {
let offset_slice = src.go_up(3);
for r in 0..height {
let src_slice = &offset_slice[r];
for c in 0..width {
tmp[r * width + c] = (round_shift(
// SAFETY: We pass this a raw pointer, but it's created from a
// checked slice, so we are safe.
unsafe {
run_filter(src_slice[c..].as_ptr(), ref_stride, y_filter)
},
7 - intermediate_bits,
) - prep_bias) as i16;
}
}
}
(_, 0) => {
let offset_slice = src.go_left(3);
for r in 0..height {
let src_slice = &offset_slice[r];
for c in 0..width {
tmp[r * width + c] = (round_shift(
// SAFETY: We pass this a raw pointer, but it's created from a
// checked slice, so we are safe.
unsafe { run_filter(src_slice[c..].as_ptr(), 1, x_filter) },
7 - intermediate_bits,
) - prep_bias) as i16;
}
}
}
(_, _) => {
let mut intermediate: [i16; 8 * (128 + 7)] = [0; 8 * (128 + 7)];
let offset_slice = src.go_left(3).go_up(3);
for cg in (0..width).step_by(8) {
for r in 0..height + 7 {
let src_slice = &offset_slice[r];
for c in cg..(cg + 8).min(width) {
intermediate[8 * r + (c - cg)] = round_shift(
// SAFETY: We pass this a raw pointer, but it's created from a
// checked slice, so we are safe.
unsafe { run_filter(src_slice[c..].as_ptr(), 1, x_filter) },
7 - intermediate_bits,
) as i16;
}
}
for r in 0..height {
for c in cg..(cg + 8).min(width) {
tmp[r * width + c] = (round_shift(
// SAFETY: We pass this a raw pointer, but it's created from a
// checked slice, so we are safe.
unsafe {
run_filter(
intermediate[8 * r + c - cg..].as_ptr(),
8,
y_filter,
)
},
7,
) - prep_bias) as i16;
}
}
}
}
}
}
#[cold_for_target_arch("x86_64")]
pub fn mc_avg<T: Pixel>(
dst: &mut PlaneRegionMut<'_, T>, tmp1: &[i16], tmp2: &[i16], width: usize,
height: usize, bit_depth: usize, _cpu: CpuFeatureLevel,
) {
// The assembly only supports even heights and valid uncropped widths
assert_eq!(height & 1, 0);
assert!(width.is_power_of_two() && (2..=128).contains(&width));
let max_sample_val = (1 << bit_depth) - 1;
let intermediate_bits = 4 - if bit_depth == 12 { 2 } else { 0 };
let prep_bias = if bit_depth == 8 { 0 } else { PREP_BIAS * 2 };
for r in 0..height {
let dst_slice = &mut dst[r];
for c in 0..width {
dst_slice[c] = T::cast_from(
round_shift(
tmp1[r * width + c] as i32
+ tmp2[r * width + c] as i32
+ prep_bias,
intermediate_bits + 1,
)
.clamp(0, max_sample_val),
);
}
}
}
}