]> git.nihav.org Git - nihav.git/blame - src/codecs/real/rv60dsp.rs
drop unneeded mut for mutable refs
[nihav.git] / src / codecs / real / rv60dsp.rs
CommitLineData
aca89041
KS
1use crate::frame::NAVideoBuffer;
2use crate::codecs::MV;
3use crate::codecs::blockdsp::edge_emu;
52aad9fe
KS
4
5fn clip8(val: i16) -> u8 { val.min(255).max(0) as u8 }
6
7macro_rules! el {
8 ($s: ident, $o: expr) => ( $s[$o] as i16 )
9}
10
11macro_rules! filter {
12 (01; $s: ident, $o: expr, $step: expr) => (
13 clip8((( el!($s, $o - 2 * $step)
14 -5 * el!($s, $o - 1 * $step)
15 +52 * el!($s, $o - 0 * $step)
16 +20 * el!($s, $o + 1 * $step)
17 -5 * el!($s, $o + 2 * $step)
18 + el!($s, $o + 3 * $step) + 32) >> 6) as i16)
19 );
20 (02; $s: ident, $o: expr, $step: expr) => (
21 clip8((( el!($s, $o - 2 * $step)
22 -5 * el!($s, $o - 1 * $step)
23 +20 * el!($s, $o - 0 * $step)
24 +20 * el!($s, $o + 1 * $step)
25 -5 * el!($s, $o + 2 * $step)
26 + el!($s, $o + 3 * $step) + 16) >> 5) as i16)
27 );
28 (03; $s: ident, $o: expr, $step: expr) => (
29 clip8((( el!($s, $o - 2 * $step)
30 -5 * el!($s, $o - 1 * $step)
31 +20 * el!($s, $o - 0 * $step)
32 +52 * el!($s, $o + 1 * $step)
33 -5 * el!($s, $o + 2 * $step)
34 + el!($s, $o + 3 * $step) + 32) >> 6) as i16)
35 );
36}
37
38macro_rules! filter_row {
39 ($d: ident, $do: expr, $s: ident, $so: expr, $step: expr, $size: expr, $mode: expr) => ({
40 match $mode {
41 1 => {
42 for x in 0..$size {
43 $d[$do + x] = filter!(01; $s, $so + x, $step);
44 }
45 },
46 2 => {
47 for x in 0..$size {
48 $d[$do + x] = filter!(02; $s, $so + x, $step);
49 }
50 },
51 3 => {
52 for x in 0..$size {
53 $d[$do + x] = filter!(03; $s, $so + x, $step);
54 }
55 },
56 _ => {},
57 };
58 });
59}
60
61fn luma_mc(dst: &mut [u8], mut didx: usize, dstride: usize, src: &[u8], mut sidx: usize, sstride: usize, w: usize, h: usize, cx: usize, cy: usize) {
62 if (cx == 0) && (cy == 0) {
63 for _ in 0..h {
64 for x in 0..w { dst[didx + x] = src[sidx + x]; }
65 didx += dstride;
66 sidx += sstride;
67 }
68 } else if cy == 0 {
69 for _ in 0..h {
70 filter_row!(dst, didx, src, sidx, 1, w, cx);
71 didx += dstride;
72 sidx += sstride;
73 }
74 } else if cx == 0 {
75 for _ in 0..h {
76 filter_row!(dst, didx, src, sidx, sstride, w, cy);
77 didx += dstride;
78 sidx += sstride;
79 }
80 } else if (cx != 3) || (cy != 3) {
81 let mut tmp: [u8; 70 * 64] = [0; 70 * 64];
82 for y in 0..h+5 {
83 filter_row!(tmp, y * 64, src, sidx - sstride * 2, 1, w, cx);
84 sidx += sstride;
85 }
86 for y in 0..h {
87 filter_row!(dst, didx, tmp, (y + 2) * 64, 64, w, cy);
88 didx += dstride;
89 }
90 } else {
91 for _ in 0..h {
92 for x in 0..w {
93 dst[didx + x] = ((el!(src, sidx + x) + el!(src, sidx + x + 1) +
94 el!(src, sidx + x + sstride) + el!(src, sidx + x + 1 + sstride) + 2) >> 2) as u8;
95 }
96 didx += dstride;
97 sidx += sstride;
98 }
99 }
100}
101
102fn chroma_mc(dst: &mut [u8], mut didx: usize, dstride: usize, src: &[u8], mut sidx: usize, sstride: usize, w: usize, h: usize, x: usize, y: usize) {
103 if (x == 0) && (y == 0) {
104 for _ in 0..h {
105 for x in 0..w { dst[didx + x] = src[sidx + x]; }
106 didx += dstride;
107 sidx += sstride;
108 }
109 return;
110 }
111 if (x > 0) && (y > 0) {
112 let a = ((4 - x) * (4 - y)) as u16;
113 let b = (( x) * (4 - y)) as u16;
114 let c = ((4 - x) * ( y)) as u16;
115 let d = (( x) * ( y)) as u16;
116 for _ in 0..h {
117 for x in 0..w {
118 dst[didx + x] = ((a * (src[sidx + x] as u16)
119 + b * (src[sidx + x + 1] as u16)
120 + c * (src[sidx + x + sstride] as u16)
121 + d * (src[sidx + x + 1 + sstride] as u16) + 8) >> 4) as u8;
122 }
123 didx += dstride;
124 sidx += sstride;
125 }
126 } else {
127 let a = ((4 - x) * (4 - y)) as u16;
128 let e = (( x) * (4 - y) + (4 - x) * ( y)) as u16;
129 let step = if y > 0 { sstride } else { 1 };
130 for _ in 0..h {
131 for x in 0..w {
132 dst[didx + x] = ((a * (src[sidx + x] as u16)
133 + e * (src[sidx + x + step] as u16) + 8) >> 4) as u8;
134 }
135 didx += dstride;
136 sidx += sstride;
137 }
138 }
139}
140
141fn check_pos(x: usize, y: usize, cw: usize, ch: usize, w: usize, h: usize, dx: i16, dy: i16, e0: isize, e1: isize, e2: isize, e3: isize) -> bool {
142 let xn = (x as isize) + (dx as isize);
143 let yn = (y as isize) + (dy as isize);
144
145 (xn - e0 >= 0) && (xn + (cw as isize) + e1 <= (w as isize)) && (yn - e2 >= 0) && (yn + (ch as isize) + e3 <= (h as isize))
146}
147
148macro_rules! diff{
149 ($src: ident, $e1: expr, $e2: expr) => (
150 ($src[$e1] as i16) - ($src[$e2] as i16)
151 )
152}
153macro_rules! strength{
154 ($el: expr, $lim: expr) => (if $el < $lim { 3 } else { 1 })
155}
156fn clip_symm(val: i16, lim: i16) -> i16 { val.max(-lim).min(lim) }
157
158fn filter_luma_edge(dst: &mut [u8], mut offset: usize, step: usize, stride: usize, mode1: u8, mode2: u8, lim1: i16, lim2: i16) {
159 let mut diff_q1q0: [i16; 4] = [0; 4];
160 let mut diff_p1p0: [i16; 4] = [0; 4];
161 for i in 0..4 {
162 let off = offset + i * stride;
163 diff_q1q0[i] = diff!(dst, off - 2 * step, off - step);
164 diff_p1p0[i] = diff!(dst, off + step, off);
165 }
166 let str_p = strength!(diff_q1q0[0] + diff_q1q0[1] + diff_q1q0[2] + diff_q1q0[3], lim2);
167 let str_q = strength!(diff_p1p0[0] + diff_p1p0[1] + diff_p1p0[2] + diff_p1p0[3], lim2);
168 if str_p + str_q > 2 {
169 let msum = ((mode1 + mode2 + str_q + str_p) >> 1) as i16;
170 let (maxprod, weak) = if (str_q == 1) || (str_p == 1) { (512, true) } else { (384, false) };
171 for y in 0..4 {
172 let diff_p0q0 = diff!(dst, offset, offset - step);
173 if (diff_p0q0 != 0) && (lim1 * diff_p0q0.abs() < maxprod) {
174 let diff_q1q2 = diff!(dst, offset - 2 * step, offset - 3 * step);
175 let diff_p1p2 = diff!(dst, offset + step, offset + 2 * step);
176 let delta = if weak {
177 clip_symm((diff_p0q0 + 1) >> 1, msum >> 1)
178 } else {
179 let diff_strg = (diff!(dst, offset - 2 * step, offset + step) + 4 * diff_p0q0 + 4) >> 3;
180 clip_symm(diff_strg, msum)
181 };
182 dst[offset - step] = clip8((dst[offset - step] as i16) + delta);
183 dst[offset] = clip8((dst[offset] as i16) - delta);
184 if (str_q != 1) && (diff_q1q2.abs() <= (lim1 >> 2)) {
185 let diff = (diff_q1q0[y] + diff_q1q2 - delta) >> 1;
186 let delta_q1 = if weak {
187 clip_symm(diff, (mode1 >> 1) as i16)
188 } else {
189 clip_symm(diff, mode1 as i16)
190 };
191 dst[offset - 2 * step] = clip8((dst[offset - 2 * step] as i16) - delta_q1);
192 }
193 if (str_p != 1) && (diff_p1p2.abs() <= (lim1 >> 2)) {
194 let diff = (diff_p1p0[y] + diff_p1p2 + delta) >> 1;
195 let delta_p1 = if weak {
196 clip_symm(diff, (mode2 >> 1) as i16)
197 } else {
198 clip_symm(diff, mode2 as i16)
199 };
200 dst[offset + step] = clip8((dst[offset + step] as i16) - delta_p1);
201 }
202 }
203 offset += stride;
204 }
205 }
206}
207fn filter_chroma_edge(dst: &mut [u8], mut offset: usize, step: usize, stride: usize, mode1: u8, mode2: u8, lim1: i16, lim2: i16) {
208 let diff_q = 4 * diff!(dst, offset - 2 * step, offset - step).abs();
209 let diff_p = 4 * diff!(dst, offset + step, offset ).abs();
210 let str_q = strength!(diff_q, lim2);
211 let str_p = strength!(diff_p, lim2);
212 if str_p + str_q > 2 {
213 let msum = ((mode1 + mode2 + str_q + str_p) >> 1) as i16;
214 let (maxprod, weak) = if (str_q == 1) || (str_p == 1) { (512, true) } else { (384, false) };
215 for _ in 0..2 {
216 let diff_pq = diff!(dst, offset, offset - step);
217 if (diff_pq != 0) && (lim1 * diff_pq.abs() < maxprod) {
218 let delta = if weak {
219 clip_symm((diff_pq + 1) >> 1, msum >> 1)
220 } else {
221 let diff_strg = (diff!(dst, offset - 2 * step, offset + step) + 4 * diff_pq + 4) >> 3;
222 clip_symm(diff_strg, msum)
223 };
224 dst[offset - step] = clip8((dst[offset - step] as i16) + delta);
225 dst[offset] = clip8((dst[offset] as i16) - delta);
226 }
227 offset += stride;
228 }
229 }
230}
231
232pub struct RV60DeblockParams {
233 pub deblock_chroma: bool,
234 pub width: usize,
235 pub height: usize,
236 pub dblkstride: usize,
237}
238
239pub struct RV60DSP {}
240/*pub fn rv6_transform4x4_dc(coeffs: &mut [i16]) {
241 let dc = (((coeffs[0] * 13 + 0x10) >> 5) * 13 + 0x10) >> 5;
242 for el in coeffs.iter_mut().take(16) {
243 *el = dc;
244 }
245}*/
246
247impl RV60DSP {
248 pub fn new() -> Self { Self{} }
249 pub fn transform4x4(&self, blk: &mut [i16]) {
250 let mut tmp: [i32; 4 * 4] = [0; 4 * 4];
251
252 for i in 0..4 {
253 let a = blk[i + 0 * 4] as i32;
254 let b = blk[i + 1 * 4] as i32;
255 let c = blk[i + 2 * 4] as i32;
256 let d = blk[i + 3 * 4] as i32;
257
258 let t0 = 13 * (a + c);
259 let t1 = 13 * (a - c);
260 let t2 = 7 * b - 17 * d;
261 let t3 = 7 * d + 17 * b;
262 tmp[i + 0 * 4] = (t0 + t3 + 0x10) >> 5;
263 tmp[i + 1 * 4] = (t1 + t2 + 0x10) >> 5;
264 tmp[i + 2 * 4] = (t1 - t2 + 0x10) >> 5;
265 tmp[i + 3 * 4] = (t0 - t3 + 0x10) >> 5;
266 }
267 for (dst, src) in blk.chunks_mut(4).zip(tmp.chunks(4)) {
268 let a = src[0];
269 let b = src[1];
270 let c = src[2];
271 let d = src[3];
272
273 let t0 = 13 * (a + c);
274 let t1 = 13 * (a - c);
275 let t2 = 7 * b - 17 * d;
276 let t3 = 7 * d + 17 * b;
277 dst[0] = ((t0 + t3 + 0x10) >> 5) as i16;
278 dst[1] = ((t1 + t2 + 0x10) >> 5) as i16;
279 dst[2] = ((t1 - t2 + 0x10) >> 5) as i16;
280 dst[3] = ((t0 - t3 + 0x10) >> 5) as i16;
281 }
282 }
283 /*pub fn transform8x8_dc(&self, blk: &mut [i16]) {
284 assert!(blk.len() >= 8 * 8);
285 let dc = (((coeffs[0] * 37 + 0x40) >> 7) * 37 + 0x40) >> 7;
286 for el in coeffs.iter_mut().take(8 * 8) {
287 *el = dc;
288 }
289 }*/
290 pub fn transform8x8(&self, blk: &mut [i16]) {
291 assert!(blk.len() >= 8 * 8);
292 let mut tmp: [i32; 8 * 8] = [0; 8 * 8];
293 for i in 0..8 {
294 let s0 = blk[i + 0 * 8] as i32;
295 let s1 = blk[i + 1 * 8] as i32;
296 let s2 = blk[i + 2 * 8] as i32;
297 let s3 = blk[i + 3 * 8] as i32;
298 let s4 = blk[i + 4 * 8] as i32;
299 let s5 = blk[i + 5 * 8] as i32;
300 let s6 = blk[i + 6 * 8] as i32;
301 let s7 = blk[i + 7 * 8] as i32;
302
303 let t0 = 37 * (s0 + s4);
304 let t1 = 37 * (s0 - s4);
305 let t2 = 48 * s2 + 20 * s6;
306 let t3 = 20 * s2 - 48 * s6;
307 let t4 = t0 + t2;
308 let t5 = t0 - t2;
309 let t6 = t1 + t3;
310 let t7 = t1 - t3;
311 let t8 = 51 * s1 + 43 * s3 + 29 * s5 + 10 * s7;
312 let t9 = 43 * s1 - 10 * s3 - 51 * s5 - 29 * s7;
313 let ta = 29 * s1 - 51 * s3 + 10 * s5 + 43 * s7;
314 let tb = 10 * s1 - 29 * s3 + 43 * s5 - 51 * s7;
315 tmp[i + 0 * 8] = (t4 + t8 + 0x40) >> 7;
316 tmp[i + 1 * 8] = (t6 + t9 + 0x40) >> 7;
317 tmp[i + 2 * 8] = (t7 + ta + 0x40) >> 7;
318 tmp[i + 3 * 8] = (t5 + tb + 0x40) >> 7;
319 tmp[i + 4 * 8] = (t5 - tb + 0x40) >> 7;
320 tmp[i + 5 * 8] = (t7 - ta + 0x40) >> 7;
321 tmp[i + 6 * 8] = (t6 - t9 + 0x40) >> 7;
322 tmp[i + 7 * 8] = (t4 - t8 + 0x40) >> 7;
323 }
324 for (dst, src) in blk.chunks_mut(8).zip(tmp.chunks(8)) {
325 let s0 = src[0];
326 let s1 = src[1];
327 let s2 = src[2];
328 let s3 = src[3];
329 let s4 = src[4];
330 let s5 = src[5];
331 let s6 = src[6];
332 let s7 = src[7];
333
334 let t0 = 37 * (s0 + s4);
335 let t1 = 37 * (s0 - s4);
336 let t2 = 48 * s2 + 20 * s6;
337 let t3 = 20 * s2 - 48 * s6;
338 let t4 = t0 + t2;
339 let t5 = t0 - t2;
340 let t6 = t1 + t3;
341 let t7 = t1 - t3;
342 let t8 = 51 * s1 + 43 * s3 + 29 * s5 + 10 * s7;
343 let t9 = 43 * s1 - 10 * s3 - 51 * s5 - 29 * s7;
344 let ta = 29 * s1 - 51 * s3 + 10 * s5 + 43 * s7;
345 let tb = 10 * s1 - 29 * s3 + 43 * s5 - 51 * s7;
346 dst[0] = ((t4 + t8 + 0x40) >> 7) as i16;
347 dst[1] = ((t6 + t9 + 0x40) >> 7) as i16;
348 dst[2] = ((t7 + ta + 0x40) >> 7) as i16;
349 dst[3] = ((t5 + tb + 0x40) >> 7) as i16;
350 dst[4] = ((t5 - tb + 0x40) >> 7) as i16;
351 dst[5] = ((t7 - ta + 0x40) >> 7) as i16;
352 dst[6] = ((t6 - t9 + 0x40) >> 7) as i16;
353 dst[7] = ((t4 - t8 + 0x40) >> 7) as i16;
354 }
355 }
356 /*pub fn transform16x16_dc(&self, blk: &mut [i16; 16 * 16]) {
357 let dc = (((coeffs[0] * 26 + 0x40) >> 7) * 26 + 0x40) >> 7;
358 for el in coeffs.iter_mut() {
359 *el = dc;
360 }
361 }*/
362 #[allow(non_snake_case)]
363 fn transform16(blk: &mut [i16; 16 * 16], off: usize, step: usize) {
364 let src0 = blk[off + 0 * step] as i32;
365 let src1 = blk[off + 1 * step] as i32;
366 let src2 = blk[off + 2 * step] as i32;
367 let src3 = blk[off + 3 * step] as i32;
368 let src4 = blk[off + 4 * step] as i32;
369 let src5 = blk[off + 5 * step] as i32;
370 let src6 = blk[off + 6 * step] as i32;
371 let src7 = blk[off + 7 * step] as i32;
372 let src8 = blk[off + 8 * step] as i32;
373 let src9 = blk[off + 9 * step] as i32;
374 let srcA = blk[off + 10 * step] as i32;
375 let srcB = blk[off + 11 * step] as i32;
376 let srcC = blk[off + 12 * step] as i32;
377 let srcD = blk[off + 13 * step] as i32;
378 let srcE = blk[off + 14 * step] as i32;
379 let srcF = blk[off + 15 * step] as i32;
380 let t0 = 26 * (src0 + src8);
381 let t1 = 26 * (src0 - src8);
382 let t2 = 14 * src4 - 34 * srcC;
383 let t3 = 34 * src4 + 14 * srcC;
384 let t4 = t0 + t3;
385 let t5 = t0 - t3;
386 let t6 = t1 + t2;
387 let t7 = t1 - t2;
388 let tmp00 = 31 * src2 + -7 * src6 + -36 * srcA + -20 * srcE;
389 let tmp01 = 36 * src2 + 31 * src6 + 20 * srcA + 7 * srcE;
390 let tmp02 = 20 * src2 + -36 * src6 + 7 * srcA + 31 * srcE;
391 let tmp03 = 7 * src2 + -20 * src6 + 31 * srcA + -36 * srcE;
392 let tm0 = t4 + tmp01;
393 let tm1 = t4 - tmp01;
394 let tm2 = t5 + tmp03;
395 let tm3 = t5 - tmp03;
396 let tm4 = t6 + tmp00;
397 let tm5 = t6 - tmp00;
398 let tm6 = t7 + tmp02;
399 let tm7 = t7 - tmp02;
400 let tt0 = 37 * src1 + 35 * src3 + 32 * src5 + 28 * src7 + 23 * src9 + 17 * srcB + 11 * srcD + 4 * srcF;
401 let tt1 = 35 * src1 + 23 * src3 + 4 * src5 + -17 * src7 + -32 * src9 + -37 * srcB + -28 * srcD + -11 * srcF;
402 let tt2 = 32 * src1 + 4 * src3 + -28 * src5 + -35 * src7 + -11 * src9 + 23 * srcB + 37 * srcD + 17 * srcF;
403 let tt3 = 28 * src1 + -17 * src3 + -35 * src5 + 4 * src7 + 37 * src9 + 11 * srcB + -32 * srcD + -23 * srcF;
404 let tt4 = 23 * src1 + -32 * src3 + -11 * src5 + 37 * src7 + -4 * src9 + -35 * srcB + 17 * srcD + 28 * srcF;
405 let tt5 = 17 * src1 + -37 * src3 + 23 * src5 + 11 * src7 + -35 * src9 + 28 * srcB + 4 * srcD + -32 * srcF;
406 let tt6 = 11 * src1 + -28 * src3 + 37 * src5 + -32 * src7 + 17 * src9 + 4 * srcB + -23 * srcD + 35 * srcF;
407 let tt7 = 4 * src1 + -11 * src3 + 17 * src5 + -23 * src7 + 28 * src9 + -32 * srcB + 35 * srcD + -37 * srcF;
408 blk[off + 0 * step] = ((tm0 + tt0 + 64) >> 7) as i16;
409 blk[off + 1 * step] = ((tm4 + tt1 + 64) >> 7) as i16;
410 blk[off + 2 * step] = ((tm6 + tt2 + 64) >> 7) as i16;
411 blk[off + 3 * step] = ((tm4 + tt3 + 64) >> 7) as i16;
412 blk[off + 4 * step] = ((tm3 + tt4 + 64) >> 7) as i16;
413 blk[off + 5 * step] = ((tm7 + tt5 + 64) >> 7) as i16;
414 blk[off + 6 * step] = ((tm5 + tt6 + 64) >> 7) as i16;
415 blk[off + 7 * step] = ((tm1 + tt7 + 64) >> 7) as i16;
416 blk[off + 8 * step] = ((tm1 - tt7 + 64) >> 7) as i16;
417 blk[off + 9 * step] = ((tm5 - tt6 + 64) >> 7) as i16;
418 blk[off + 10 * step] = ((tm7 - tt5 + 64) >> 7) as i16;
419 blk[off + 11 * step] = ((tm3 - tt4 + 64) >> 7) as i16;
420 blk[off + 12 * step] = ((tm2 - tt3 + 64) >> 7) as i16;
421 blk[off + 13 * step] = ((tm6 - tt2 + 64) >> 7) as i16;
422 blk[off + 14 * step] = ((tm4 - tt1 + 64) >> 7) as i16;
423 blk[off + 15 * step] = ((tm0 - tt0 + 64) >> 7) as i16;
424 }
425 pub fn transform16x16(&self, blk: &mut [i16; 16 * 16]) {
426 for i in 0..16 {
427 Self::transform16(blk, i, 16);
428 }
429 for i in 0..16 {
430 Self::transform16(blk, i * 16, 1);
431 }
432 }
433
434 pub fn add_block(&self, dst: &mut [u8], mut doff: usize, dstride: usize, blk: &[i16], size: usize) {
435 for y in 0..size {
436 for x in 0..size {
437 dst[doff + x] = clip8((dst[doff + x] as i16) + blk[x + y * size]);
438 }
439 doff += dstride;
440 }
441 }
442 fn avg(&self, dst: &mut [u8], mut didx: usize, dstride: usize,
443 src: &[u8], mut sidx: usize, sstride: usize,
444 w: usize, h: usize) {
445 for _ in 0..h {
446 for x in 0..w {
447 dst[didx + x] = (((dst[didx + x] as u16) + (src[sidx + x] as u16)) >> 1) as u8;
448 }
449 didx += dstride;
450 sidx += sstride;
451 }
452 }
453 pub fn do_avg(&self, frame: &mut NAVideoBuffer<u8>, prev_frame: &NAVideoBuffer<u8>, x: usize, y: usize, w: usize, h: usize) {
454 for comp in 0..3 {
455 let dstride = frame.get_stride(comp);
456 let sstride = prev_frame.get_stride(comp);
457 let doff = if comp == 0 { x + y * dstride } else { frame.get_offset(comp) + (x >> 1) + (y >> 1) * dstride };
458 let soff = prev_frame.get_offset(comp);
459 let mut ddata = frame.get_data_mut();
460 let dst: &mut [u8] = ddata.as_mut_slice();
461 let sdata = prev_frame.get_data();
462 let src: &[u8] = sdata.as_slice();
463
464 if comp == 0 {
465 self.avg(dst, doff, dstride, src, soff, sstride, w, h);
466 } else {
467 self.avg(dst, doff, dstride, src, soff, sstride, w >> 1, h >> 1);
468 }
469 }
470 }
471 pub fn do_mc(&self, frame: &mut NAVideoBuffer<u8>, prev_frame: &NAVideoBuffer<u8>, x: usize, y: usize, w: usize, h: usize, mv: MV, avg: bool) {
472 { // luma
473 let dstride = frame.get_stride(0);
474 let doffset = frame.get_offset(0) + (if !avg { x + y * dstride } else { 0 });
475 let mut data = frame.get_data_mut();
476 let dst: &mut [u8] = data.as_mut_slice();
477
478 let (w_, h_) = prev_frame.get_dimensions(0);
479 let fw = (w_ + 15) & !15;
480 let fh = (h_ + 15) & !15;
481
482 let dx = mv.x >> 2;
483 let cx = (mv.x & 3) as usize;
484 let dy = mv.y >> 2;
485 let cy = (mv.y & 3) as usize;
486
487 if check_pos(x, y, w, h, fw, fh, dx, dy, RV60_EDGE1[cx], RV60_EDGE2[cx], RV60_EDGE1[cy], RV60_EDGE2[cy]) {
488 let sstride = prev_frame.get_stride(0);
489 let mut soffset = prev_frame.get_offset(0) + x + y * sstride;
490 let data = prev_frame.get_data();
491 let src: &[u8] = data.as_slice();
492 soffset = ((soffset as isize) + (dx as isize) + (dy as isize) * (sstride as isize)) as usize;
493 luma_mc(dst, doffset, dstride, src, soffset, sstride, w, h, cx, cy);
494 } else {
495 let mut ebuf: [u8; 70*70] = [0; 70*70];
496 edge_emu(prev_frame, (x as isize) + (dx as isize) - 2, (y as isize) + (dy as isize) - 2, w+5, h+5, &mut ebuf, 70, 0);
497 luma_mc(dst, doffset, dstride, &ebuf, 70*2 + 2, 70, w, h, cx, cy);
498 }
499 }
500 let (w_, h_) = prev_frame.get_dimensions(1);
501 let fw = (w_ + 7) & !7;
502 let fh = (h_ + 7) & !7;
503 let mvx = mv.x / 2;
504 let mvy = mv.y / 2;
505 let dx = mvx >> 2;
506 let cx = (mvx & 3) as usize;
507 let dy = mvy >> 2;
508 let cy = (mvy & 3) as usize;
509 let cw = w >> 1;
510 let ch = h >> 1;
511
512 for comp in 1..3 { // chroma
513 let dstride = frame.get_stride(comp);
514 let doffset = frame.get_offset(comp) + (if !avg { (x >> 1) + (y >> 1) * dstride } else { 0 });
515 let mut data = frame.get_data_mut();
516 let dst: &mut [u8] = data.as_mut_slice();
517 if check_pos(x >> 1, y >> 1, cw, ch, fw, fh, dx, dy, 0, 1, 0, 1) {
518 let sstride = prev_frame.get_stride(comp);
519 let mut soffset = prev_frame.get_offset(comp) + (x >> 1) + (y >> 1) * sstride;
520 let data = prev_frame.get_data();
521 let src: &[u8] = data.as_slice();
522 soffset = ((soffset as isize) + (dx as isize) + (dy as isize) * (sstride as isize)) as usize;
523 chroma_mc(dst, doffset, dstride, src, soffset, sstride, cw, ch, cx, cy);
524 } else {
525 let mut ebuf: [u8; 40*40] = [0; 40*40];
526 edge_emu(prev_frame, ((x >> 1) as isize) + (dx as isize), ((y >> 1) as isize) + (dy as isize), cw+1, ch+1, &mut ebuf, 40, comp);
527 chroma_mc(dst, doffset, dstride, &ebuf, 0, 40, cw, ch, cx, cy);
528 }
529 }
530 }
531 fn deblock_edge4_ver(&self, frame: &mut NAVideoBuffer<u8>, xpos: usize, ypos: usize,
532 dblk_l: u8, dblk_r: u8, deblock_chroma: bool) {
533 let qp_l = dblk_l >> 2;
534 let str_l = dblk_l & 3;
535 let qp_r = dblk_r >> 2;
536 let str_r = dblk_r & 3;
537 let dl_l = &RV60_DEB_LIMITS[qp_l as usize];
538 let dl_r = &RV60_DEB_LIMITS[qp_r as usize];
539 let mode_l = if str_l != 0 { dl_l[(str_l - 1) as usize] } else { 0 };
540 let mode_r = if str_r != 0 { dl_r[(str_r - 1) as usize] } else { 0 };
541 let lim1 = dl_r[2] as i16;
542 let lim2 = (dl_r[3] * 4) as i16;
543 {
544 let stride = frame.get_stride(0);
545 let offset = frame.get_offset(0) + xpos + ypos * stride;
546 let mut data = frame.get_data_mut();
547 let dst: &mut [u8] = data.as_mut_slice();
548 filter_luma_edge(dst, offset, 1, stride, mode_l, mode_r, lim1, lim2);
549 }
550 if ((str_l | str_r) >= 2) && deblock_chroma {
551 for comp in 1..2 {
552 let stride = frame.get_stride(comp);
553 let offset = frame.get_offset(comp) + (xpos >> 1) + (ypos >> 1) * stride;
554 let mut data = frame.get_data_mut();
555 let dst: &mut [u8] = data.as_mut_slice();
556 filter_chroma_edge(dst, offset, 1, stride, mode_l, mode_r, lim1, lim2);
557 }
558 }
559 }
560 fn deblock_edge4_hor(&self, frame: &mut NAVideoBuffer<u8>, xpos: usize, ypos: usize,
561 dblk_t: u8, dblk_d: u8, deblock_chroma: bool) {
562 let qp_t = dblk_t >> 2;
563 let str_t = dblk_t & 3;
564 let qp_d = dblk_d >> 2;
565 let str_d = dblk_d & 3;
566 let dl_t = &RV60_DEB_LIMITS[qp_t as usize];
567 let dl_d = &RV60_DEB_LIMITS[qp_d as usize];
568 let mode_t = if str_t != 0 { dl_t[(str_t - 1) as usize] } else { 0 };
569 let mode_d = if str_d != 0 { dl_d[(str_d - 1) as usize] } else { 0 };
570 let lim1 = dl_d[2] as i16;
571 let lim2 = (dl_d[3] * 4) as i16;
572 {
573 let stride = frame.get_stride(0);
574 let offset = frame.get_offset(0) + xpos + ypos * stride;
575 let mut data = frame.get_data_mut();
576 let dst: &mut [u8] = data.as_mut_slice();
577 filter_luma_edge(dst, offset, stride, 1, mode_t, mode_d, lim1, lim2);
578 }
579 if ((str_t | str_d) >= 2) && deblock_chroma {
580 for comp in 1..2 {
581 let stride = frame.get_stride(comp);
582 let offset = frame.get_offset(comp) + (xpos >> 1) + (ypos >> 1) * stride;
583 let mut data = frame.get_data_mut();
584 let dst: &mut [u8] = data.as_mut_slice();
585 filter_chroma_edge(dst, offset, stride, 1, mode_t, mode_d, lim1, lim2);
586 }
587 }
588 }
589 fn deblock8x8(&self, dparams: &RV60DeblockParams, frame: &mut NAVideoBuffer<u8>,
590 xpos: usize, ypos: usize, top_str: &[u8], left_str: &[u8], dblkpos: usize) {
591 if xpos > 0 {
592 if ypos > 0 {
593 let str_l = left_str[dblkpos - dparams.dblkstride];
594 let str_r = left_str[dblkpos];
595 if (str_l | str_r) != 0 {
596 self.deblock_edge4_ver(frame, xpos, ypos - 4, str_l, str_r, dparams.deblock_chroma);
597 }
598 }
599 {
600 let str_l = left_str[dblkpos];
601 let str_r = left_str[dblkpos + dparams.dblkstride];
602 if (str_l | str_r) != 0 {
603 self.deblock_edge4_ver(frame, xpos, ypos + 0, str_l, str_r, dparams.deblock_chroma);
604 }
605 }
606 if ypos + 4 >= dparams.height {
607 let str_l = left_str[dblkpos + dparams.dblkstride];
608 let str_r = left_str[dblkpos + dparams.dblkstride * 2];
609 if (str_l | str_r) != 0 {
610 self.deblock_edge4_ver(frame, xpos, ypos + 4, str_l, str_r, dparams.deblock_chroma);
611 }
612 }
613 }
614 if ypos > 0 {
615 if xpos > 0 {
616 let str_t = top_str[dblkpos - 1];
617 let str_d = top_str[dblkpos];
618 if (str_t | str_d) != 0 {
619 self.deblock_edge4_hor(frame, xpos - 4, ypos, str_t, str_d, dparams.deblock_chroma);
620 }
621 }
622 {
623 let str_t = top_str[dblkpos];
624 let str_d = top_str[dblkpos + 1];
625 if (str_t | str_d) != 0 {
626 self.deblock_edge4_hor(frame, xpos + 0, ypos, str_t, str_d, dparams.deblock_chroma);
627 }
628 }
629 if xpos + 4 >= dparams.width {
630 let str_t = top_str[dblkpos + 1];
631 let str_d = top_str[dblkpos + 2];
632 if (str_t | str_d) != 0 {
633 self.deblock_edge4_hor(frame, xpos + 4, ypos, str_t, str_d, dparams.deblock_chroma);
634 }
635 }
636 }
637 }
638 pub fn do_deblock(&self, dparams: &RV60DeblockParams, frame: &mut NAVideoBuffer<u8>,
639 xpos: usize, ypos: usize, size: usize, top_str: &[u8], left_str: &[u8], dpos: usize) {
640 for x in 0..(size >> 3) {
641 self.deblock8x8(dparams, frame, xpos + x * 8, ypos,
642 top_str, left_str, dpos + x * 2);
643 }
644 for y in 1..(size >> 3) {
645 self.deblock8x8(dparams, frame, xpos, ypos + y * 8,
646 top_str, left_str, dpos + y * 2 * dparams.dblkstride);
647 }
648 }
649}
650
651const RV60_DEB_LIMITS: [[u8; 4]; 32] = [
652 [ 0, 0, 128, 0 ], [ 0, 0, 128, 0 ], [ 0, 0, 128, 0 ], [ 0, 0, 128, 0 ],
653 [ 0, 0, 128, 0 ], [ 0, 0, 128, 0 ], [ 0, 0, 128, 0 ], [ 0, 0, 128, 0 ],
654 [ 0, 0, 128, 3 ], [ 0, 1, 128, 3 ], [ 0, 1, 122, 3 ], [ 1, 1, 96, 4 ],
655 [ 1, 1, 75, 4 ], [ 1, 1, 59, 4 ], [ 1, 1, 47, 6 ], [ 1, 1, 37, 6 ],
656 [ 1, 1, 29, 6 ], [ 1, 2, 23, 7 ], [ 1, 2, 18, 8 ], [ 1, 2, 15, 8 ],
657 [ 1, 2, 13, 9 ], [ 2, 3, 11, 9 ], [ 2, 3, 10, 10 ], [ 2, 3, 9, 10 ],
658 [ 2, 4, 8, 11 ], [ 3, 4, 7, 11 ], [ 3, 5, 6, 12 ], [ 3, 5, 5, 13 ],
659 [ 3, 5, 4, 14 ], [ 4, 7, 3, 15 ], [ 5, 8, 2, 16 ], [ 5, 9, 1, 17 ]
660];
661
662#[derive(Clone)]
663pub struct IntraPredContext {
664 pub t: [u8; 129], // 0 - TL or 0x80, two block sizes or replicated last val from block0
665 pub l: [u8; 129],
666 pub has_t: bool,
667 pub has_tr: bool,
668 pub has_l: bool,
669 pub has_ld: bool,
670}
671
672impl IntraPredContext {
673 pub fn new() -> Self {
674 Self {
675 t: [0x80; 129], l: [0x80; 129], has_t: false, has_tr: false, has_l: false, has_ld: false,
676 }
677 }
678 pub fn pred_dc(&self, dst: &mut [u8], mut doff: usize, dstride: usize, size: usize, filter: bool) {
679 let dc;
680 if !self.has_t && !self.has_l {
681 dc = 0x80;
682 } else {
683 let mut sum = 0;
684 if self.has_t {
685 for x in 0..size { sum += self.t[x + 1] as u16; }
686 }
687 if self.has_l {
688 for y in 0..size { sum += self.l[y + 1] as u16; }
689 }
690 if self.has_t && self.has_l {
691 dc = ((sum + (size as u16)) / ((size as u16) * 2)) as u8;
692 } else {
693 dc = ((sum + ((size >> 1) as u16)) / (size as u16)) as u8;
694 }
695 }
696 for _ in 0..size {
697 for x in 0..size { dst[doff + x] = dc; }
698 doff += dstride;
699 }
700 if filter && self.has_t && self.has_l {
701 doff -= dstride * size;
702 dst[doff] = (((self.t[1] as u16) + (self.l[1] as u16) + 2 * (dst[doff] as u16) + 2) >> 2) as u8;
703 for x in 1..size {
704 dst[doff + x] = (((self.t[x + 1] as u16) + 3 * (dst[doff + x] as u16) + 2) >> 2) as u8;
705 }
706 for y in 1..size {
707 doff += dstride;
708 dst[doff] = (((self.l[y + 1] as u16) + 3 * (dst[doff] as u16) + 2) >> 2) as u8;
709 }
710 }
711 }
712 pub fn pred_plane(&self, dst: &mut [u8], mut doff: usize, dstride: usize, size: usize) {
713 let lastl = self.l[size + 1] as i32;
714 let lastt = self.t[size + 1] as i32;
715 let mut tmp1: [i32; 64] = [0; 64];
716 let mut tmp2: [i32; 64] = [0; 64];
717 for i in 0..size {
718 tmp1[i] = lastl - (self.t[i + 1] as i32);
719 tmp2[i] = lastt - (self.l[i + 1] as i32);
720 }
721 let shift = match size {
722 4 => 3,
723 8 => 4,
724 16 => 5,
725 32 => 6,
726 _ => 7,
727 };
728 let mut top_ref: [i32; 64] = [0; 64];
729 let mut left_ref:[i32; 64] = [0; 64];
730 for i in 0..size {
731 top_ref [i] = (self.t[i + 1] as i32) << (shift - 1);
732 left_ref[i] = (self.l[i + 1] as i32) << (shift - 1);
733 }
734 for y in 0..size {
735 let add = tmp2[y];
736 let mut sum = left_ref[y] + (size as i32);
737 for x in 0..size {
738 let v = tmp1[x] + top_ref[x];
739 sum += add;
740 top_ref[x] = v;
741 dst[doff + x] = ((sum + v) >> shift) as u8;
742 }
743 doff += dstride;
744 }
745 }
746 fn pred_hor_angle(dst: &mut [u8], doff: usize, dstride: usize, size: usize, weight: i16, src: &[u8]) {
747 let mut sum = 0;
748 for x in 0..size {
749 sum += weight;
750 let off = ((sum >> 5) + 32) as usize;
751 let frac = (sum & 0x1F) as u16;
752 if frac == 0 {
753 for y in 0..size {
754 dst[doff + x + y * dstride] = src[off + y];
755 }
756 } else {
757 for y in 0..size {
758 let a = src[off + y + 0] as u16;
759 let b = src[off + y + 1] as u16;
760 dst[doff + x + y * dstride] = (((32 - frac) * a + frac * b + 0x10) >> 5) as u8;
761 }
762 }
763 }
764 }
765 fn pred_ver_angle(dst: &mut [u8], mut doff: usize, dstride: usize, size: usize, weight: i16, src: &[u8]) {
766 let mut sum = 0;
767 for _ in 0..size {
768 sum += weight;
769 let off = ((sum >> 5) + 32) as usize;
770 let frac = (sum & 0x1F) as u16;
771 if frac == 0 {
772 for x in 0..size {
773 dst[doff + x] = src[off + x];
774 }
775 } else {
776 for x in 0..size {
777 let a = src[off + x + 0] as u16;
778 let b = src[off + x + 1] as u16;
779 dst[doff + x] = (((32 - frac) * a + frac * b + 0x10) >> 5) as u8;
780 }
781 }
782 doff += dstride;
783 }
784 }
785 fn filter_weak(dst: &mut [u8], src: &[u8], size: usize) {
786 dst[0] = src[0];
787 for i in 1..size-1 {
788 dst[i] = (((src[i - 1] as u16) + 2 * (src[i] as u16) + (src[i + 1] as u16) + 2) >> 2) as u8;
789 }
790 dst[size - 1] = src[size - 1];
791 }
792 fn filter_bilin32(dst: &mut [u8], v0: u8, v1: u8, size: usize) {
793 let diff = (v1 as i16) - (v0 as i16);
794 let mut sum = ((v0 as i16) << 5) + (1 << (5 - 1));
795 for i in 0..size {
796 dst[i] = (sum >> 5) as u8;
797 sum += diff;
798 }
799 }
800 pub fn pred_angle(&self, dst: &mut [u8], mut doff: usize, dstride: usize, size: usize, angle: usize, filter: bool) {
801 let mut filtered1: [u8; 96] = [0; 96];
802 let mut filtered2: [u8; 96] = [0; 96];
803 if angle == 0 {
804 self.pred_plane(dst, doff, dstride, size);
805 } else if angle == 1 {
806 self.pred_dc(dst, doff, dstride, size, filter);
807 } else if angle <= 9 {
808 let ang_weight = RV60_IPRED_ANGLE[10 - angle];
809 let add_size = (size * (ang_weight as usize) + 31) >> 5;
810 if size <= 16 {
811 Self::filter_weak(&mut filtered1[32..], &self.l[1..], size + add_size);
812 } else {
813 Self::filter_bilin32(&mut filtered1[32..], self.l[1], self.l[33], 32);
814 Self::filter_bilin32(&mut filtered1[64..], self.l[32], self.l[64], add_size);
815 }
816 Self::pred_hor_angle(dst, doff, dstride, size, ang_weight as i16, &filtered1);
817 } else if angle == 10 {
818 if size <= 16 {
819 Self::filter_weak(&mut filtered1[32..], &self.l[1..], size);
820 } else {
821 Self::filter_bilin32(&mut filtered1[32..], self.l[1], self.l[33], 32);
822 }
823 for y in 0..size {
824 for x in 0..size {
825 dst[doff + x] = filtered1[32 + y];
826 }
827 doff += dstride;
828 }
829 if filter {
830 doff -= dstride * size;
831 let tl = self.t[0] as i16;
832 for x in 0..size {
833 dst[doff + x] = clip8((dst[doff + x] as i16) + (((self.t[x + 1] as i16) - tl) >> 1));
834 }
835 }
836 } else if angle <= 17 {
837 let ang_weight = RV60_IPRED_ANGLE [angle - 10];
838 let inv_angle = RV60_IPRED_INV_ANGLE[angle - 10];
839 let add_size = (size * (ang_weight as usize) + 31) >> 5;
840 if size <= 16 {
841 for i in 0..size+1 {
842 filtered1[32-1 + i] = self.l[i];
843 }
844 for i in 0..size+1 {
845 filtered2[32-1 + i] = self.t[i];
846 }
847 } else {
848 filtered1[32-1] = self.l[0];
849 Self::filter_bilin32(&mut filtered1[32..], self.l[0], self.l[32], 32);
850 filtered2[32-1] = self.t[0];
851 Self::filter_bilin32(&mut filtered2[32..], self.t[0], self.t[32], 32);
852 }
853 if add_size > 1 {
854 let mut sum = 0x80;
855 for i in 1..add_size {
856 sum += inv_angle;
857 let pos = ((sum >> 8) + 32 - 1) as usize;
858 filtered1[32 - 1 - i] = filtered2[pos];
859 }
860 }
861 Self::pred_hor_angle(dst, doff, dstride, size, -(ang_weight as i16), &filtered1);
862 } else if angle <= 25 {
863 let ang_weight = RV60_IPRED_ANGLE[26 - angle];
864 let inv_angle = RV60_IPRED_INV_ANGLE[26 - angle];
865 let add_size = (size * (ang_weight as usize) + 31) >> 5;
866 if size <= 16 {
867 for i in 0..size+1 {
868 filtered1[32-1 + i] = self.t[i];
869 }
870 for i in 0..size+1 {
871 filtered2[32-1 + i] = self.l[i];
872 }
873 } else {
874 filtered1[32-1] = self.t[0];
875 Self::filter_bilin32(&mut filtered1[32..], self.t[0], self.t[32], 32);
876 filtered2[32-1] = self.l[0];
877 Self::filter_bilin32(&mut filtered2[32..], self.l[0], self.l[32], 32);
878 }
879 if add_size > 1 {
880 let mut sum = 0x80;
881 for i in 1..add_size {
882 sum += inv_angle;
883 let pos = ((sum >> 8) + 32 - 1) as usize;
884 filtered1[32 - 1 - i] = filtered2[pos];
885 }
886 }
887 Self::pred_ver_angle(dst, doff, dstride, size, -(ang_weight as i16), &filtered1);
888 } else if angle == 26 {
889 if size <= 16 {
890 Self::filter_weak(&mut filtered1[32..], &self.t[1..], size);
891 } else {
892 Self::filter_bilin32(&mut filtered1[32..], self.t[1], self.t[33], 32);
893 }
894 for _ in 0..size {
895 for x in 0..size {
896 dst[doff + x] = filtered1[32 + x];
897 }
898 doff += dstride;
899 }
900 if filter {
901 doff -= dstride * size;
902 let tl = self.l[0] as i16;
903 for y in 0..size {
904 dst[doff] = clip8((dst[doff] as i16) + (((self.l[y + 1] as i16) - tl) >> 1));
905 doff += dstride;
906 }
907 }
908 } else if angle <= 34 {
909 let ang_weight = RV60_IPRED_ANGLE[angle - 26];
910 let add_size = (size * (ang_weight as usize) + 31) >> 5;
911 if size <= 16 {
912 Self::filter_weak(&mut filtered1[32..], &self.t[1..], size + add_size);
913 } else {
914 Self::filter_bilin32(&mut filtered1[32..], self.t[1], self.t[33], 32);
915 Self::filter_bilin32(&mut filtered1[64..], self.t[32], self.t[64], add_size);
916 }
917 Self::pred_ver_angle(dst, doff, dstride, size, ang_weight as i16, &filtered1);
918 } else {
919 unreachable!();
920 }
921 }
922}
923
924const RV60_IPRED_ANGLE: [u8; 9] = [ 0, 2, 5, 9, 13, 17, 21, 26, 32 ];
925const RV60_IPRED_INV_ANGLE: [i16; 9] = [ 0, 4096, 1638, 910, 630, 482, 390, 315, 256 ];
926const RV60_EDGE1: [isize; 4] = [ 0, 2, 2, 2 ];
927const RV60_EDGE2: [isize; 4] = [ 0, 3, 3, 3 ];
928