]> git.nihav.org Git - nihav.git/blame - nihav-realmedia/src/codecs/rv60dsp.rs
realmedia: remove unneeded mut
[nihav.git] / nihav-realmedia / src / codecs / rv60dsp.rs
CommitLineData
cd830591 1use nihav_core::frame::{NAVideoBuffer, NASimpleVideoFrame};
5641dccf
KS
2use nihav_core::codecs::MV;
3use nihav_core::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 }
cd830591 453 pub fn do_avg(&self, frame: &mut NASimpleVideoFrame<u8>, prev_frame: &NAVideoBuffer<u8>, x: usize, y: usize, w: usize, h: usize) {
52aad9fe 454 for comp in 0..3 {
cd830591 455 let dstride = frame.stride[comp];
52aad9fe 456 let sstride = prev_frame.get_stride(comp);
cd830591 457 let doff = if comp == 0 { x + y * dstride } else { frame.offset[comp] + (x >> 1) + (y >> 1) * dstride };
52aad9fe 458 let soff = prev_frame.get_offset(comp);
cd830591 459 let dst = &mut frame.data;
52aad9fe
KS
460 let sdata = prev_frame.get_data();
461 let src: &[u8] = sdata.as_slice();
462
463 if comp == 0 {
464 self.avg(dst, doff, dstride, src, soff, sstride, w, h);
465 } else {
466 self.avg(dst, doff, dstride, src, soff, sstride, w >> 1, h >> 1);
467 }
468 }
469 }
cd830591 470 pub fn do_mc(&self, frame: &mut NASimpleVideoFrame<u8>, prev_frame: &NAVideoBuffer<u8>, x: usize, y: usize, w: usize, h: usize, mv: MV, avg: bool) {
52aad9fe 471 { // luma
cd830591
KS
472 let dstride = frame.stride[0];
473 let doffset = frame.offset[0] + (if !avg { x + y * dstride } else { 0 });
474 let dst = &mut frame.data;
52aad9fe
KS
475
476 let (w_, h_) = prev_frame.get_dimensions(0);
477 let fw = (w_ + 15) & !15;
478 let fh = (h_ + 15) & !15;
479
480 let dx = mv.x >> 2;
481 let cx = (mv.x & 3) as usize;
482 let dy = mv.y >> 2;
483 let cy = (mv.y & 3) as usize;
484
485 if check_pos(x, y, w, h, fw, fh, dx, dy, RV60_EDGE1[cx], RV60_EDGE2[cx], RV60_EDGE1[cy], RV60_EDGE2[cy]) {
486 let sstride = prev_frame.get_stride(0);
487 let mut soffset = prev_frame.get_offset(0) + x + y * sstride;
488 let data = prev_frame.get_data();
489 let src: &[u8] = data.as_slice();
490 soffset = ((soffset as isize) + (dx as isize) + (dy as isize) * (sstride as isize)) as usize;
491 luma_mc(dst, doffset, dstride, src, soffset, sstride, w, h, cx, cy);
492 } else {
493 let mut ebuf: [u8; 70*70] = [0; 70*70];
494 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);
495 luma_mc(dst, doffset, dstride, &ebuf, 70*2 + 2, 70, w, h, cx, cy);
496 }
497 }
498 let (w_, h_) = prev_frame.get_dimensions(1);
499 let fw = (w_ + 7) & !7;
500 let fh = (h_ + 7) & !7;
501 let mvx = mv.x / 2;
502 let mvy = mv.y / 2;
503 let dx = mvx >> 2;
504 let cx = (mvx & 3) as usize;
505 let dy = mvy >> 2;
506 let cy = (mvy & 3) as usize;
507 let cw = w >> 1;
508 let ch = h >> 1;
509
510 for comp in 1..3 { // chroma
cd830591
KS
511 let dstride = frame.stride[comp];
512 let doffset = frame.offset[comp] + (if !avg { (x >> 1) + (y >> 1) * dstride } else { 0 });
52aad9fe
KS
513 if check_pos(x >> 1, y >> 1, cw, ch, fw, fh, dx, dy, 0, 1, 0, 1) {
514 let sstride = prev_frame.get_stride(comp);
515 let mut soffset = prev_frame.get_offset(comp) + (x >> 1) + (y >> 1) * sstride;
516 let data = prev_frame.get_data();
517 let src: &[u8] = data.as_slice();
518 soffset = ((soffset as isize) + (dx as isize) + (dy as isize) * (sstride as isize)) as usize;
cd830591 519 chroma_mc(frame.data, doffset, dstride, src, soffset, sstride, cw, ch, cx, cy);
52aad9fe
KS
520 } else {
521 let mut ebuf: [u8; 40*40] = [0; 40*40];
522 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);
cd830591 523 chroma_mc(frame.data, doffset, dstride, &ebuf, 0, 40, cw, ch, cx, cy);
52aad9fe
KS
524 }
525 }
526 }
cd830591 527 fn deblock_edge4_ver(&self, frame: &mut NASimpleVideoFrame<u8>, xpos: usize, ypos: usize,
52aad9fe
KS
528 dblk_l: u8, dblk_r: u8, deblock_chroma: bool) {
529 let qp_l = dblk_l >> 2;
530 let str_l = dblk_l & 3;
531 let qp_r = dblk_r >> 2;
532 let str_r = dblk_r & 3;
533 let dl_l = &RV60_DEB_LIMITS[qp_l as usize];
534 let dl_r = &RV60_DEB_LIMITS[qp_r as usize];
535 let mode_l = if str_l != 0 { dl_l[(str_l - 1) as usize] } else { 0 };
536 let mode_r = if str_r != 0 { dl_r[(str_r - 1) as usize] } else { 0 };
537 let lim1 = dl_r[2] as i16;
538 let lim2 = (dl_r[3] * 4) as i16;
539 {
cd830591
KS
540 let stride = frame.stride[0];
541 let offset = frame.offset[0] + xpos + ypos * stride;
542 filter_luma_edge(frame.data, offset, 1, stride, mode_l, mode_r, lim1, lim2);
52aad9fe
KS
543 }
544 if ((str_l | str_r) >= 2) && deblock_chroma {
545 for comp in 1..2 {
cd830591
KS
546 let stride = frame.stride[comp];
547 let offset = frame.offset[comp] + (xpos >> 1) + (ypos >> 1) * stride;
548 filter_chroma_edge(frame.data, offset, 1, stride, mode_l, mode_r, lim1, lim2);
52aad9fe
KS
549 }
550 }
551 }
cd830591 552 fn deblock_edge4_hor(&self, frame: &mut NASimpleVideoFrame<u8>, xpos: usize, ypos: usize,
52aad9fe
KS
553 dblk_t: u8, dblk_d: u8, deblock_chroma: bool) {
554 let qp_t = dblk_t >> 2;
555 let str_t = dblk_t & 3;
556 let qp_d = dblk_d >> 2;
557 let str_d = dblk_d & 3;
558 let dl_t = &RV60_DEB_LIMITS[qp_t as usize];
559 let dl_d = &RV60_DEB_LIMITS[qp_d as usize];
560 let mode_t = if str_t != 0 { dl_t[(str_t - 1) as usize] } else { 0 };
561 let mode_d = if str_d != 0 { dl_d[(str_d - 1) as usize] } else { 0 };
562 let lim1 = dl_d[2] as i16;
563 let lim2 = (dl_d[3] * 4) as i16;
564 {
cd830591
KS
565 let stride = frame.stride[0];
566 let offset = frame.offset[0] + xpos + ypos * stride;
567 filter_luma_edge(frame.data, offset, stride, 1, mode_t, mode_d, lim1, lim2);
52aad9fe
KS
568 }
569 if ((str_t | str_d) >= 2) && deblock_chroma {
570 for comp in 1..2 {
cd830591
KS
571 let stride = frame.stride[comp];
572 let offset = frame.offset[comp] + (xpos >> 1) + (ypos >> 1) * stride;
573 filter_chroma_edge(frame.data, offset, stride, 1, mode_t, mode_d, lim1, lim2);
52aad9fe
KS
574 }
575 }
576 }
cd830591 577 fn deblock8x8(&self, dparams: &RV60DeblockParams, frame: &mut NASimpleVideoFrame<u8>,
52aad9fe
KS
578 xpos: usize, ypos: usize, top_str: &[u8], left_str: &[u8], dblkpos: usize) {
579 if xpos > 0 {
580 if ypos > 0 {
581 let str_l = left_str[dblkpos - dparams.dblkstride];
582 let str_r = left_str[dblkpos];
583 if (str_l | str_r) != 0 {
584 self.deblock_edge4_ver(frame, xpos, ypos - 4, str_l, str_r, dparams.deblock_chroma);
585 }
586 }
587 {
588 let str_l = left_str[dblkpos];
589 let str_r = left_str[dblkpos + dparams.dblkstride];
590 if (str_l | str_r) != 0 {
591 self.deblock_edge4_ver(frame, xpos, ypos + 0, str_l, str_r, dparams.deblock_chroma);
592 }
593 }
594 if ypos + 4 >= dparams.height {
595 let str_l = left_str[dblkpos + dparams.dblkstride];
596 let str_r = left_str[dblkpos + dparams.dblkstride * 2];
597 if (str_l | str_r) != 0 {
598 self.deblock_edge4_ver(frame, xpos, ypos + 4, str_l, str_r, dparams.deblock_chroma);
599 }
600 }
601 }
602 if ypos > 0 {
603 if xpos > 0 {
604 let str_t = top_str[dblkpos - 1];
605 let str_d = top_str[dblkpos];
606 if (str_t | str_d) != 0 {
607 self.deblock_edge4_hor(frame, xpos - 4, ypos, str_t, str_d, dparams.deblock_chroma);
608 }
609 }
610 {
611 let str_t = top_str[dblkpos];
612 let str_d = top_str[dblkpos + 1];
613 if (str_t | str_d) != 0 {
614 self.deblock_edge4_hor(frame, xpos + 0, ypos, str_t, str_d, dparams.deblock_chroma);
615 }
616 }
617 if xpos + 4 >= dparams.width {
618 let str_t = top_str[dblkpos + 1];
619 let str_d = top_str[dblkpos + 2];
620 if (str_t | str_d) != 0 {
621 self.deblock_edge4_hor(frame, xpos + 4, ypos, str_t, str_d, dparams.deblock_chroma);
622 }
623 }
624 }
625 }
cd830591 626 pub fn do_deblock(&self, dparams: &RV60DeblockParams, frame: &mut NASimpleVideoFrame<u8>,
52aad9fe
KS
627 xpos: usize, ypos: usize, size: usize, top_str: &[u8], left_str: &[u8], dpos: usize) {
628 for x in 0..(size >> 3) {
629 self.deblock8x8(dparams, frame, xpos + x * 8, ypos,
630 top_str, left_str, dpos + x * 2);
631 }
632 for y in 1..(size >> 3) {
633 self.deblock8x8(dparams, frame, xpos, ypos + y * 8,
634 top_str, left_str, dpos + y * 2 * dparams.dblkstride);
635 }
636 }
637}
638
639const RV60_DEB_LIMITS: [[u8; 4]; 32] = [
640 [ 0, 0, 128, 0 ], [ 0, 0, 128, 0 ], [ 0, 0, 128, 0 ], [ 0, 0, 128, 0 ],
641 [ 0, 0, 128, 0 ], [ 0, 0, 128, 0 ], [ 0, 0, 128, 0 ], [ 0, 0, 128, 0 ],
642 [ 0, 0, 128, 3 ], [ 0, 1, 128, 3 ], [ 0, 1, 122, 3 ], [ 1, 1, 96, 4 ],
643 [ 1, 1, 75, 4 ], [ 1, 1, 59, 4 ], [ 1, 1, 47, 6 ], [ 1, 1, 37, 6 ],
644 [ 1, 1, 29, 6 ], [ 1, 2, 23, 7 ], [ 1, 2, 18, 8 ], [ 1, 2, 15, 8 ],
645 [ 1, 2, 13, 9 ], [ 2, 3, 11, 9 ], [ 2, 3, 10, 10 ], [ 2, 3, 9, 10 ],
646 [ 2, 4, 8, 11 ], [ 3, 4, 7, 11 ], [ 3, 5, 6, 12 ], [ 3, 5, 5, 13 ],
647 [ 3, 5, 4, 14 ], [ 4, 7, 3, 15 ], [ 5, 8, 2, 16 ], [ 5, 9, 1, 17 ]
648];
649
650#[derive(Clone)]
651pub struct IntraPredContext {
652 pub t: [u8; 129], // 0 - TL or 0x80, two block sizes or replicated last val from block0
653 pub l: [u8; 129],
654 pub has_t: bool,
655 pub has_tr: bool,
656 pub has_l: bool,
657 pub has_ld: bool,
658}
659
660impl IntraPredContext {
661 pub fn new() -> Self {
662 Self {
663 t: [0x80; 129], l: [0x80; 129], has_t: false, has_tr: false, has_l: false, has_ld: false,
664 }
665 }
666 pub fn pred_dc(&self, dst: &mut [u8], mut doff: usize, dstride: usize, size: usize, filter: bool) {
667 let dc;
668 if !self.has_t && !self.has_l {
669 dc = 0x80;
670 } else {
671 let mut sum = 0;
672 if self.has_t {
673 for x in 0..size { sum += self.t[x + 1] as u16; }
674 }
675 if self.has_l {
676 for y in 0..size { sum += self.l[y + 1] as u16; }
677 }
678 if self.has_t && self.has_l {
679 dc = ((sum + (size as u16)) / ((size as u16) * 2)) as u8;
680 } else {
681 dc = ((sum + ((size >> 1) as u16)) / (size as u16)) as u8;
682 }
683 }
684 for _ in 0..size {
685 for x in 0..size { dst[doff + x] = dc; }
686 doff += dstride;
687 }
688 if filter && self.has_t && self.has_l {
689 doff -= dstride * size;
690 dst[doff] = (((self.t[1] as u16) + (self.l[1] as u16) + 2 * (dst[doff] as u16) + 2) >> 2) as u8;
691 for x in 1..size {
692 dst[doff + x] = (((self.t[x + 1] as u16) + 3 * (dst[doff + x] as u16) + 2) >> 2) as u8;
693 }
694 for y in 1..size {
695 doff += dstride;
696 dst[doff] = (((self.l[y + 1] as u16) + 3 * (dst[doff] as u16) + 2) >> 2) as u8;
697 }
698 }
699 }
700 pub fn pred_plane(&self, dst: &mut [u8], mut doff: usize, dstride: usize, size: usize) {
701 let lastl = self.l[size + 1] as i32;
702 let lastt = self.t[size + 1] as i32;
703 let mut tmp1: [i32; 64] = [0; 64];
704 let mut tmp2: [i32; 64] = [0; 64];
705 for i in 0..size {
706 tmp1[i] = lastl - (self.t[i + 1] as i32);
707 tmp2[i] = lastt - (self.l[i + 1] as i32);
708 }
709 let shift = match size {
710 4 => 3,
711 8 => 4,
712 16 => 5,
713 32 => 6,
714 _ => 7,
715 };
716 let mut top_ref: [i32; 64] = [0; 64];
717 let mut left_ref:[i32; 64] = [0; 64];
718 for i in 0..size {
719 top_ref [i] = (self.t[i + 1] as i32) << (shift - 1);
720 left_ref[i] = (self.l[i + 1] as i32) << (shift - 1);
721 }
722 for y in 0..size {
723 let add = tmp2[y];
724 let mut sum = left_ref[y] + (size as i32);
725 for x in 0..size {
726 let v = tmp1[x] + top_ref[x];
727 sum += add;
728 top_ref[x] = v;
729 dst[doff + x] = ((sum + v) >> shift) as u8;
730 }
731 doff += dstride;
732 }
733 }
734 fn pred_hor_angle(dst: &mut [u8], doff: usize, dstride: usize, size: usize, weight: i16, src: &[u8]) {
735 let mut sum = 0;
736 for x in 0..size {
737 sum += weight;
738 let off = ((sum >> 5) + 32) as usize;
739 let frac = (sum & 0x1F) as u16;
740 if frac == 0 {
741 for y in 0..size {
742 dst[doff + x + y * dstride] = src[off + y];
743 }
744 } else {
745 for y in 0..size {
746 let a = src[off + y + 0] as u16;
747 let b = src[off + y + 1] as u16;
748 dst[doff + x + y * dstride] = (((32 - frac) * a + frac * b + 0x10) >> 5) as u8;
749 }
750 }
751 }
752 }
753 fn pred_ver_angle(dst: &mut [u8], mut doff: usize, dstride: usize, size: usize, weight: i16, src: &[u8]) {
754 let mut sum = 0;
755 for _ in 0..size {
756 sum += weight;
757 let off = ((sum >> 5) + 32) as usize;
758 let frac = (sum & 0x1F) as u16;
759 if frac == 0 {
760 for x in 0..size {
761 dst[doff + x] = src[off + x];
762 }
763 } else {
764 for x in 0..size {
765 let a = src[off + x + 0] as u16;
766 let b = src[off + x + 1] as u16;
767 dst[doff + x] = (((32 - frac) * a + frac * b + 0x10) >> 5) as u8;
768 }
769 }
770 doff += dstride;
771 }
772 }
773 fn filter_weak(dst: &mut [u8], src: &[u8], size: usize) {
774 dst[0] = src[0];
775 for i in 1..size-1 {
776 dst[i] = (((src[i - 1] as u16) + 2 * (src[i] as u16) + (src[i + 1] as u16) + 2) >> 2) as u8;
777 }
778 dst[size - 1] = src[size - 1];
779 }
780 fn filter_bilin32(dst: &mut [u8], v0: u8, v1: u8, size: usize) {
781 let diff = (v1 as i16) - (v0 as i16);
782 let mut sum = ((v0 as i16) << 5) + (1 << (5 - 1));
783 for i in 0..size {
784 dst[i] = (sum >> 5) as u8;
785 sum += diff;
786 }
787 }
788 pub fn pred_angle(&self, dst: &mut [u8], mut doff: usize, dstride: usize, size: usize, angle: usize, filter: bool) {
789 let mut filtered1: [u8; 96] = [0; 96];
790 let mut filtered2: [u8; 96] = [0; 96];
791 if angle == 0 {
792 self.pred_plane(dst, doff, dstride, size);
793 } else if angle == 1 {
794 self.pred_dc(dst, doff, dstride, size, filter);
795 } else if angle <= 9 {
796 let ang_weight = RV60_IPRED_ANGLE[10 - angle];
797 let add_size = (size * (ang_weight as usize) + 31) >> 5;
798 if size <= 16 {
799 Self::filter_weak(&mut filtered1[32..], &self.l[1..], size + add_size);
800 } else {
801 Self::filter_bilin32(&mut filtered1[32..], self.l[1], self.l[33], 32);
802 Self::filter_bilin32(&mut filtered1[64..], self.l[32], self.l[64], add_size);
803 }
804 Self::pred_hor_angle(dst, doff, dstride, size, ang_weight as i16, &filtered1);
805 } else if angle == 10 {
806 if size <= 16 {
807 Self::filter_weak(&mut filtered1[32..], &self.l[1..], size);
808 } else {
809 Self::filter_bilin32(&mut filtered1[32..], self.l[1], self.l[33], 32);
810 }
811 for y in 0..size {
812 for x in 0..size {
813 dst[doff + x] = filtered1[32 + y];
814 }
815 doff += dstride;
816 }
817 if filter {
818 doff -= dstride * size;
819 let tl = self.t[0] as i16;
820 for x in 0..size {
821 dst[doff + x] = clip8((dst[doff + x] as i16) + (((self.t[x + 1] as i16) - tl) >> 1));
822 }
823 }
824 } else if angle <= 17 {
825 let ang_weight = RV60_IPRED_ANGLE [angle - 10];
826 let inv_angle = RV60_IPRED_INV_ANGLE[angle - 10];
827 let add_size = (size * (ang_weight as usize) + 31) >> 5;
828 if size <= 16 {
e07387c7 829 for i in 0..=size {
52aad9fe
KS
830 filtered1[32-1 + i] = self.l[i];
831 }
e07387c7 832 for i in 0..=size {
52aad9fe
KS
833 filtered2[32-1 + i] = self.t[i];
834 }
835 } else {
836 filtered1[32-1] = self.l[0];
837 Self::filter_bilin32(&mut filtered1[32..], self.l[0], self.l[32], 32);
838 filtered2[32-1] = self.t[0];
839 Self::filter_bilin32(&mut filtered2[32..], self.t[0], self.t[32], 32);
840 }
841 if add_size > 1 {
842 let mut sum = 0x80;
843 for i in 1..add_size {
844 sum += inv_angle;
845 let pos = ((sum >> 8) + 32 - 1) as usize;
846 filtered1[32 - 1 - i] = filtered2[pos];
847 }
848 }
849 Self::pred_hor_angle(dst, doff, dstride, size, -(ang_weight as i16), &filtered1);
850 } else if angle <= 25 {
851 let ang_weight = RV60_IPRED_ANGLE[26 - angle];
852 let inv_angle = RV60_IPRED_INV_ANGLE[26 - angle];
853 let add_size = (size * (ang_weight as usize) + 31) >> 5;
854 if size <= 16 {
e07387c7 855 for i in 0..=size {
52aad9fe
KS
856 filtered1[32-1 + i] = self.t[i];
857 }
e07387c7 858 for i in 0..=size {
52aad9fe
KS
859 filtered2[32-1 + i] = self.l[i];
860 }
861 } else {
862 filtered1[32-1] = self.t[0];
863 Self::filter_bilin32(&mut filtered1[32..], self.t[0], self.t[32], 32);
864 filtered2[32-1] = self.l[0];
865 Self::filter_bilin32(&mut filtered2[32..], self.l[0], self.l[32], 32);
866 }
867 if add_size > 1 {
868 let mut sum = 0x80;
869 for i in 1..add_size {
870 sum += inv_angle;
871 let pos = ((sum >> 8) + 32 - 1) as usize;
872 filtered1[32 - 1 - i] = filtered2[pos];
873 }
874 }
875 Self::pred_ver_angle(dst, doff, dstride, size, -(ang_weight as i16), &filtered1);
876 } else if angle == 26 {
877 if size <= 16 {
878 Self::filter_weak(&mut filtered1[32..], &self.t[1..], size);
879 } else {
880 Self::filter_bilin32(&mut filtered1[32..], self.t[1], self.t[33], 32);
881 }
882 for _ in 0..size {
883 for x in 0..size {
884 dst[doff + x] = filtered1[32 + x];
885 }
886 doff += dstride;
887 }
888 if filter {
889 doff -= dstride * size;
890 let tl = self.l[0] as i16;
891 for y in 0..size {
892 dst[doff] = clip8((dst[doff] as i16) + (((self.l[y + 1] as i16) - tl) >> 1));
893 doff += dstride;
894 }
895 }
896 } else if angle <= 34 {
897 let ang_weight = RV60_IPRED_ANGLE[angle - 26];
898 let add_size = (size * (ang_weight as usize) + 31) >> 5;
899 if size <= 16 {
900 Self::filter_weak(&mut filtered1[32..], &self.t[1..], size + add_size);
901 } else {
902 Self::filter_bilin32(&mut filtered1[32..], self.t[1], self.t[33], 32);
903 Self::filter_bilin32(&mut filtered1[64..], self.t[32], self.t[64], add_size);
904 }
905 Self::pred_ver_angle(dst, doff, dstride, size, ang_weight as i16, &filtered1);
906 } else {
907 unreachable!();
908 }
909 }
910}
911
912const RV60_IPRED_ANGLE: [u8; 9] = [ 0, 2, 5, 9, 13, 17, 21, 26, 32 ];
913const RV60_IPRED_INV_ANGLE: [i16; 9] = [ 0, 4096, 1638, 910, 630, 482, 390, 315, 256 ];
914const RV60_EDGE1: [isize; 4] = [ 0, 2, 2, 2 ];
915const RV60_EDGE2: [isize; 4] = [ 0, 3, 3, 3 ];
916