1 use nihav_core::frame::*;
2 use nihav_core::codecs::blockdsp::edge_emu;
4 fn clip_u8(val: i16) -> u8 {
5 val.max(0).min(255) as u8
8 pub struct IPredContext {
17 pub fn fill(&mut self, src: &[u8], off: usize, stride: usize, tsize: usize, lsize: usize) {
20 self.top[i] = src[off - stride + i];
26 self.top = [0x80; 16];
30 self.left[i] = src[off - 1 + i * stride];
36 self.left = [0x80; 16];
38 if self.has_top && self.has_left {
39 self.tl = src[off - stride - 1];
46 impl Default for IPredContext {
47 fn default() -> Self {
58 const DCT_COEFFS: [i32; 16] = [
59 23170, 23170, 23170, 23170,
60 30274, 12540, -12540, -30274,
61 23170, -23170, -23170, 23170,
62 12540, -30274, 30274, -12540
65 pub fn idct4x4(coeffs: &mut [i16; 16]) {
66 let mut tmp = [0i16; 16];
67 for (src, dst) in coeffs.chunks(4).zip(tmp.chunks_mut(4)) {
68 let s0 = src[0] as i32;
69 let s1 = src[1] as i32;
70 let s2 = src[2] as i32;
71 let s3 = src[3] as i32;
73 let t0 = (s0 + s2).wrapping_mul(23170);
74 let t1 = (s0 - s2).wrapping_mul(23170);
75 let t2 = s1.wrapping_mul(30274) + s3.wrapping_mul(12540);
76 let t3 = s1.wrapping_mul(12540) - s3.wrapping_mul(30274);
78 dst[0] = ((t0 + t2) >> 14) as i16;
79 dst[1] = ((t1 + t3) >> 14) as i16;
80 dst[2] = ((t1 - t3) >> 14) as i16;
81 dst[3] = ((t0 - t2) >> 14) as i16;
84 let s0 = tmp[i + 4 * 0] as i32;
85 let s1 = tmp[i + 4 * 1] as i32;
86 let s2 = tmp[i + 4 * 2] as i32;
87 let s3 = tmp[i + 4 * 3] as i32;
89 let t0 = (s0 + s2).wrapping_mul(23170) + 0x20000;
90 let t1 = (s0 - s2).wrapping_mul(23170) + 0x20000;
91 let t2 = s1.wrapping_mul(30274) + s3.wrapping_mul(12540);
92 let t3 = s1.wrapping_mul(12540) - s3.wrapping_mul(30274);
94 coeffs[i + 0 * 4] = ((t0 + t2) >> 18) as i16;
95 coeffs[i + 1 * 4] = ((t1 + t3) >> 18) as i16;
96 coeffs[i + 2 * 4] = ((t1 - t3) >> 18) as i16;
97 coeffs[i + 3 * 4] = ((t0 - t2) >> 18) as i16;
101 pub fn idct4x4_dc(coeffs: &mut [i16; 16]) {
102 let dc = (((((coeffs[0] as i32) * DCT_COEFFS[0]) >> 14) * DCT_COEFFS[0] + 0x20000) >> 18) as i16;
103 for el in coeffs.iter_mut() {
108 pub fn add_coeffs4x4(dst: &mut [u8], off: usize, stride: usize, coeffs: &[i16; 16]) {
109 let dst = &mut dst[off..];
110 for (out, src) in dst.chunks_mut(stride).zip(coeffs.chunks(4)) {
111 for (oel, iel) in out.iter_mut().take(4).zip(src.iter()) {
112 *oel = clip_u8((*oel as i16) + *iel);
116 pub fn add_coeffs16x1(dst: &mut [u8], off: usize, coeffs: &[i16; 16]) {
117 let dst = &mut dst[off..];
118 for (oel, iel) in dst.iter_mut().take(16).zip(coeffs.iter()) {
119 *oel = clip_u8((*oel as i16) + *iel);
123 pub trait IntraPred {
125 fn ipred_dc(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
127 if !ipred.has_left && !ipred.has_top {
131 let mut dcshift = match Self::SIZE {
136 for el in ipred.left.iter().take(Self::SIZE) {
142 for el in ipred.top.iter().take(Self::SIZE) {
147 dc = ((dcsum + (1 << (dcshift - 1))) >> dcshift) as u8;
149 for _ in 0..Self::SIZE {
150 let out = &mut dst[off..][..Self::SIZE];
151 for el in out.iter_mut() {
157 fn ipred_v(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
158 for _ in 0..Self::SIZE {
159 let out = &mut dst[off..][..Self::SIZE];
160 out.copy_from_slice(&ipred.top[0..Self::SIZE]);
164 fn ipred_h(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
165 for leftel in ipred.left.iter().take(Self::SIZE) {
166 let out = &mut dst[off..][..Self::SIZE];
167 for el in out.iter_mut() {
173 fn ipred_tm(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
174 let tl = ipred.tl as i16;
175 for m in 0..Self::SIZE {
176 for n in 0..Self::SIZE {
177 dst[off + n] = clip_u8((ipred.left[m] as i16) + (ipred.top[n] as i16) - tl);
184 pub struct IPred16x16 {}
185 impl IntraPred for IPred16x16 { const SIZE: usize = 16; }
187 pub struct IPred8x8 {}
188 impl IntraPred for IPred8x8 { const SIZE: usize = 8; }
190 macro_rules! load_pred4 {
191 (topleft; $ipred: expr) => {{
192 let tl = $ipred.tl as u16;
193 let a0 = $ipred.top[0] as u16;
194 let l0 = $ipred.left[0] as u16;
195 ((l0 + tl * 2 + a0 + 2) >> 2) as u8
197 (top; $ipred: expr) => {{
198 let tl = $ipred.tl as u16;
199 let a0 = $ipred.top[0] as u16;
200 let a1 = $ipred.top[1] as u16;
201 let a2 = $ipred.top[2] as u16;
202 let a3 = $ipred.top[3] as u16;
203 let a4 = $ipred.top[4] as u16;
204 let p0 = ((tl + a0 * 2 + a1 + 2) >> 2) as u8;
205 let p1 = ((a0 + a1 * 2 + a2 + 2) >> 2) as u8;
206 let p2 = ((a1 + a2 * 2 + a3 + 2) >> 2) as u8;
207 let p3 = ((a2 + a3 * 2 + a4 + 2) >> 2) as u8;
210 (top8; $ipred: expr) => {{
211 let t3 = $ipred.top[3] as u16;
212 let t4 = $ipred.top[4] as u16;
213 let t5 = $ipred.top[5] as u16;
214 let t6 = $ipred.top[6] as u16;
215 let t7 = $ipred.top[7] as u16;
216 let p4 = ((t3 + t4 * 2 + t5 + 2) >> 2) as u8;
217 let p5 = ((t4 + t5 * 2 + t6 + 2) >> 2) as u8;
218 let p6 = ((t5 + t6 * 2 + t7 + 2) >> 2) as u8;
219 let p7 = ((t6 + t7 * 2 + t7 + 2) >> 2) as u8;
222 (topavg; $ipred: expr) => {{
223 let tl = $ipred.tl as u16;
224 let a0 = $ipred.top[0] as u16;
225 let a1 = $ipred.top[1] as u16;
226 let a2 = $ipred.top[2] as u16;
227 let a3 = $ipred.top[3] as u16;
228 let p0 = ((tl + a0 + 1) >> 1) as u8;
229 let p1 = ((a0 + a1 + 1) >> 1) as u8;
230 let p2 = ((a1 + a2 + 1) >> 1) as u8;
231 let p3 = ((a2 + a3 + 1) >> 1) as u8;
234 (left; $ipred: expr) => {{
235 let tl = $ipred.tl as u16;
236 let l0 = $ipred.left[0] as u16;
237 let l1 = $ipred.left[1] as u16;
238 let l2 = $ipred.left[2] as u16;
239 let l3 = $ipred.left[3] as u16;
240 let l4 = $ipred.left[4] as u16;
241 let p0 = ((tl + l0 * 2 + l1 + 2) >> 2) as u8;
242 let p1 = ((l0 + l1 * 2 + l2 + 2) >> 2) as u8;
243 let p2 = ((l1 + l2 * 2 + l3 + 2) >> 2) as u8;
244 let p3 = ((l2 + l3 * 2 + l4 + 2) >> 2) as u8;
247 (left8; $ipred: expr) => {{
248 let l3 = $ipred.left[3] as u16;
249 let l4 = $ipred.left[4] as u16;
250 let l5 = $ipred.left[5] as u16;
251 let l6 = $ipred.left[6] as u16;
252 let l7 = $ipred.left[7] as u16;
253 let p4 = ((l3 + l4 * 2 + l5 + 2) >> 2) as u8;
254 let p5 = ((l4 + l5 * 2 + l6 + 2) >> 2) as u8;
255 let p6 = ((l5 + l6 * 2 + l7 + 2) >> 2) as u8;
256 let p7 = ((l6 + l7 * 2 + l7 + 2) >> 2) as u8;
259 (leftavg; $ipred: expr) => {{
260 let tl = $ipred.tl as u16;
261 let l0 = $ipred.left[0] as u16;
262 let l1 = $ipred.left[1] as u16;
263 let l2 = $ipred.left[2] as u16;
264 let l3 = $ipred.left[3] as u16;
265 let p0 = ((tl + l0 + 1) >> 1) as u8;
266 let p1 = ((l0 + l1 + 1) >> 1) as u8;
267 let p2 = ((l1 + l2 + 1) >> 1) as u8;
268 let p3 = ((l2 + l3 + 1) >> 1) as u8;
273 pub struct IPred4x4 {}
275 pub fn ipred_dc(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
278 for el in ipred.left.iter().take(4) {
281 for el in ipred.top.iter().take(4) {
284 dc = ((dcsum + (1 << 2)) >> 3) as u8;
286 let out = &mut dst[off..][..4];
287 for el in out.iter_mut() {
293 pub fn ipred_tm(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
294 let tl = ipred.tl as i16;
297 dst[off + n] = clip_u8((ipred.left[m] as i16) + (ipred.top[n] as i16) - tl);
302 pub fn ipred_ve(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
303 let (v0, v1, v2, v3) = load_pred4!(top; ipred);
304 let vert_pred = [v0, v1, v2, v3];
306 let out = &mut dst[off..][..4];
307 out.copy_from_slice(&vert_pred);
311 pub fn ipred_he(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
312 let (p0, p1, p2, _) = load_pred4!(left; ipred);
313 let p3 = (((ipred.left[2] as u16) + (ipred.left[3] as u16) * 3 + 2) >> 2) as u8;
314 let hor_pred = [p0, p1, p2, p3];
317 dst[off + n] = hor_pred[m];
322 pub fn ipred_ld(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
323 let (_, p0, p1, p2) = load_pred4!(top; ipred);
324 let (p3, p4, p5, p6) = load_pred4!(top8; ipred);
326 dst[off + 0] = p0; dst[off + 1] = p1; dst[off + 2] = p2; dst[off + 3] = p3;
328 dst[off + 0] = p1; dst[off + 1] = p2; dst[off + 2] = p3; dst[off + 3] = p4;
330 dst[off + 0] = p2; dst[off + 1] = p3; dst[off + 2] = p4; dst[off + 3] = p5;
332 dst[off + 0] = p3; dst[off + 1] = p4; dst[off + 2] = p5; dst[off + 3] = p6;
334 pub fn ipred_rd(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
335 let tl = load_pred4!(topleft; ipred);
336 let (l0, l1, l2, _) = load_pred4!(left; ipred);
337 let (t0, t1, t2, _) = load_pred4!(top; ipred);
339 dst[off + 0] = tl; dst[off + 1] = t0; dst[off + 2] = t1; dst[off + 3] = t2;
341 dst[off + 0] = l0; dst[off + 1] = tl; dst[off + 2] = t0; dst[off + 3] = t1;
343 dst[off + 0] = l1; dst[off + 1] = l0; dst[off + 2] = tl; dst[off + 3] = t0;
345 dst[off + 0] = l2; dst[off + 1] = l1; dst[off + 2] = l0; dst[off + 3] = tl;
347 pub fn ipred_vr(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
348 let tl = load_pred4!(topleft; ipred);
349 let (l0, l1, _, _) = load_pred4!(left; ipred);
350 let (t0, t1, t2, _) = load_pred4!(top; ipred);
351 let (m0, m1, m2, m3) = load_pred4!(topavg; ipred);
353 dst[off + 0] = m0; dst[off + 1] = m1; dst[off + 2] = m2; dst[off + 3] = m3;
355 dst[off + 0] = tl; dst[off + 1] = t0; dst[off + 2] = t1; dst[off + 3] = t2;
357 dst[off + 0] = l0; dst[off + 1] = m0; dst[off + 2] = m1; dst[off + 3] = m2;
359 dst[off + 0] = l1; dst[off + 1] = tl; dst[off + 2] = t0; dst[off + 3] = t1;
361 pub fn ipred_vl(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
362 let (_, t1, t2, t3) = load_pred4!(top; ipred);
363 let (t4, t5, t6, _) = load_pred4!(top8; ipred);
364 let (_, m1, m2, m3) = load_pred4!(topavg; ipred);
365 let m4 = (((ipred.top[3] as u16) + (ipred.top[4] as u16) + 1) >> 1) as u8;
367 dst[off + 0] = m1; dst[off + 1] = m2; dst[off + 2] = m3; dst[off + 3] = m4;
369 dst[off + 0] = t1; dst[off + 1] = t2; dst[off + 2] = t3; dst[off + 3] = t4;
371 dst[off + 0] = m2; dst[off + 1] = m3; dst[off + 2] = m4; dst[off + 3] = t5;
373 dst[off + 0] = t2; dst[off + 1] = t3; dst[off + 2] = t4; dst[off + 3] = t6;
375 pub fn ipred_hd(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
376 let tl = load_pred4!(topleft; ipred);
377 let (l0, l1, l2, _) = load_pred4!(left; ipred);
378 let (m0, m1, m2, m3) = load_pred4!(leftavg; ipred);
379 let (t0, t1, _, _) = load_pred4!(top; ipred);
381 dst[off + 0] = m0; dst[off + 1] = tl; dst[off + 2] = t0; dst[off + 3] = t1;
383 dst[off + 0] = m1; dst[off + 1] = l0; dst[off + 2] = m0; dst[off + 3] = tl;
385 dst[off + 0] = m2; dst[off + 1] = l1; dst[off + 2] = m1; dst[off + 3] = l0;
387 dst[off + 0] = m3; dst[off + 1] = l2; dst[off + 2] = m2; dst[off + 3] = l1;
389 pub fn ipred_hu(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
390 let (_, m1, m2, m3) = load_pred4!(leftavg; ipred);
391 let (_, l1, l2, _) = load_pred4!(left; ipred);
392 let l3 = (((ipred.left[2] as u16) + (ipred.left[3] as u16) * 3 + 2) >> 2) as u8;
393 let p3 = ipred.left[3];
395 dst[off + 0] = m1; dst[off + 1] = l1; dst[off + 2] = m2; dst[off + 3] = l2;
397 dst[off + 0] = m2; dst[off + 1] = l2; dst[off + 2] = m3; dst[off + 3] = l3;
399 dst[off + 0] = m3; dst[off + 1] = l3; dst[off + 2] = p3; dst[off + 3] = p3;
401 dst[off + 0] = p3; dst[off + 1] = p3; dst[off + 2] = p3; dst[off + 3] = p3;
405 fn delta(p1: i16, p0: i16, q0: i16, q1: i16) -> i16 {
406 (p1 - q1) + 3 * (q0 - p0)
409 pub type LoopFilterFunc = fn(buf: &mut [u8], off: usize, step: usize, stride: usize, len: usize, thr: i16, thr_inner: i16, thr_hev: i16);
411 pub fn simple_loop_filter(buf: &mut [u8], mut off: usize, step: usize, stride: usize, len: usize, thr: i16, _thr_inner: i16, _thr_hev: i16) {
413 let p1 = buf[off - step * 2] as i16;
414 let p0 = buf[off - step * 1] as i16;
415 let q0 = buf[off + step * 0] as i16;
416 let q1 = buf[off + step * 1] as i16;
419 let diff = delta(p1, p0, q0, q1);
420 let diffq0 = (diff.min(127) + 4) >> 3;
421 let diffp0 = diffq0 - if (diff & 7) == 4 { 1 } else { 0 };
422 buf[off - step * 1] = clip_u8(p0 + diffp0);
423 buf[off + step * 0] = clip_u8(q0 - diffq0);
429 fn normal_loop_filter(buf: &mut [u8], mut off: usize, step: usize, stride: usize, len: usize, thr: i16, thr_inner: i16, thr_hev: i16, edge: bool) {
431 let p0 = buf[off - step * 1] as i16;
432 let q0 = buf[off + step * 0] as i16;
434 if dpq.abs() <= thr {
435 let p3 = buf[off - step * 4] as i16;
436 let p2 = buf[off - step * 3] as i16;
437 let p1 = buf[off - step * 2] as i16;
438 let q1 = buf[off + step * 1] as i16;
439 let q2 = buf[off + step * 2] as i16;
440 let q3 = buf[off + step * 3] as i16;
447 if (dp0.abs() <= thr_inner) && (dp1.abs() <= thr_inner) &&
448 (dp2.abs() <= thr_inner) && (dq0.abs() <= thr_inner) &&
449 (dq1.abs() <= thr_inner) && (dq2.abs() <= thr_inner) {
450 let high_edge_variation = (dp0.abs() > thr_hev) || (dq0.abs() > thr_hev);
451 if high_edge_variation {
452 let diff = delta(p1, p0, q0, q1);
453 let diffq0 = (diff.min(127) + 4) >> 3;
454 let diffp0 = diffq0 - if (diff & 7) == 4 { 1 } else { 0 };
455 buf[off - step * 1] = clip_u8(p0 + diffp0);
456 buf[off + step * 0] = clip_u8(q0 - diffq0);
458 let d = delta(p1, p0, q0, q1);
459 let diff0 = (d * 27 + 63) >> 7;
460 buf[off - step * 1] = clip_u8(p0 + diff0);
461 buf[off + step * 0] = clip_u8(q0 - diff0);
462 let diff1 = (d * 18 + 63) >> 7;
463 buf[off - step * 2] = clip_u8(p1 + diff1);
464 buf[off + step * 1] = clip_u8(q1 - diff1);
465 let diff2 = (d * 9 + 63) >> 7;
466 buf[off - step * 3] = clip_u8(p2 + diff2);
467 buf[off + step * 2] = clip_u8(q2 - diff2);
469 let diff = 3 * (q0 - p0);
470 let diffq0 = (diff.min(127) + 4) >> 3;
471 let diffp0 = diffq0 - if (diff & 7) == 4 { 1 } else { 0 };
472 buf[off - step * 1] = clip_u8(p0 + diffp0);
473 buf[off + step * 0] = clip_u8(q0 - diffq0);
474 let diff2 = (diffq0 + 1) >> 1;
475 buf[off - step * 2] = clip_u8(p1 + diff2);
476 buf[off + step * 1] = clip_u8(q1 - diff2);
484 pub fn normal_loop_filter_inner(buf: &mut [u8], off: usize, step: usize, stride: usize, len: usize, thr: i16, thr_inner: i16, thr_hev: i16) {
485 normal_loop_filter(buf, off, step, stride, len, thr, thr_inner, thr_hev, false);
488 pub fn normal_loop_filter_edge(buf: &mut [u8], off: usize, step: usize, stride: usize, len: usize, thr: i16, thr_inner: i16, thr_hev: i16) {
489 normal_loop_filter(buf, off, step, stride, len, thr, thr_inner, thr_hev, true);
492 const VP7_BICUBIC_FILTERS: [[i16; 6]; 8] = [
493 [ 0, 0, 128, 0, 0, 0 ],
494 [ 0, -6, 123, 12, -1, 0 ],
495 [ 2, -11, 108, 36, -8, 1 ],
496 [ 0, -9, 93, 50, -6, 0 ],
497 [ 3, -16, 77, 77, -16, 3 ],
498 [ 0, -6, 50, 93, -9, 0 ],
499 [ 1, -8, 36, 108, -11, 2 ],
500 [ 0, -1, 12, 123, -6, 0 ]
503 macro_rules! interpolate {
504 ($src: expr, $off: expr, $step: expr, $mode: expr) => {{
505 let s0 = $src[$off + 0 * $step] as i32;
506 let s1 = $src[$off + 1 * $step] as i32;
507 let s2 = $src[$off + 2 * $step] as i32;
508 let s3 = $src[$off + 3 * $step] as i32;
509 let s4 = $src[$off + 4 * $step] as i32;
510 let s5 = $src[$off + 5 * $step] as i32;
511 let filt = &VP7_BICUBIC_FILTERS[$mode];
512 let src = [s0, s1, s2, s3, s4, s5];
514 for (s, c) in src.iter().zip(filt.iter()) {
515 val += s * (*c as i32);
517 clip_u8((val >> 7) as i16)
521 const EDGE_PRE: usize = 2;
522 const EDGE_POST: usize = 4;
523 const TMP_STRIDE: usize = 16;
525 fn mc_block_common(dst: &mut [u8], mut doff: usize, dstride: usize, src: &[u8], sstride: usize, size: usize, mx: usize, my: usize) {
526 if (mx == 0) && (my == 0) {
527 let dst = &mut dst[doff..];
528 let src = &src[EDGE_PRE + EDGE_PRE * sstride..];
529 for (out, src) in dst.chunks_mut(dstride).take(size).zip(src.chunks(sstride)) {
530 (&mut out[0..size]).copy_from_slice(&src[0..size]);
533 let src = &src[EDGE_PRE * sstride..];
534 for src in src.chunks(sstride).take(size) {
536 dst[doff + x] = interpolate!(src, x, 1, mx);
541 let src = &src[EDGE_PRE..];
544 dst[doff + x] = interpolate!(src, x + y * sstride, sstride, my);
549 let mut tmp = [0u8; TMP_STRIDE * (16 + EDGE_PRE + EDGE_POST)];
550 for (y, dst) in tmp.chunks_mut(TMP_STRIDE).take(size + EDGE_PRE + EDGE_POST).enumerate() {
552 dst[x] = interpolate!(src, x + y * sstride, 1, mx);
557 dst[doff + x] = interpolate!(tmp, x + y * TMP_STRIDE, TMP_STRIDE, my);
563 fn mc_block(dst: &mut [u8], doff: usize, dstride: usize, xpos: usize, ypos: usize,
564 mvx: i16, mvy: i16, reffrm: NAVideoBufferRef<u8>, plane: usize,
565 mc_buf: &mut [u8], size: usize) {
566 if (mvx == 0) && (mvy == 0) {
567 let dst = &mut dst[doff..];
568 let sstride = reffrm.get_stride(plane);
569 let srcoff = reffrm.get_offset(plane) + xpos + ypos * sstride;
570 let src = &reffrm.get_data();
571 let src = &src[srcoff..];
572 for (out, src) in dst.chunks_mut(dstride).take(size).zip(src.chunks(sstride)) {
573 (&mut out[0..size]).copy_from_slice(&src[0..size]);
577 let (w, h) = reffrm.get_dimensions(plane);
578 let wa = if plane == 0 { ((w + 15) & !15) } else { ((w + 7) & !7) } as isize;
579 let ha = if plane == 0 { ((h + 15) & !15) } else { ((h + 7) & !7) } as isize;
580 let bsize = (size as isize) + (EDGE_PRE as isize) + (EDGE_POST as isize);
581 let ref_x = (xpos as isize) + ((mvx >> 3) as isize) - (EDGE_PRE as isize);
582 let ref_y = (ypos as isize) + ((mvy >> 3) as isize) - (EDGE_PRE as isize);
584 let (src, sstride) = if (ref_x < 0) || (ref_x + bsize > wa) || (ref_y < 0) || (ref_y + bsize > ha) {
585 edge_emu(&reffrm, ref_x, ref_y, bsize as usize, bsize as usize, mc_buf, 32, plane);
586 (mc_buf as &[u8], 32)
588 let off = reffrm.get_offset(plane);
589 let stride = reffrm.get_stride(plane);
590 let data = reffrm.get_data();
591 (&data[off + (ref_x as usize) + (ref_y as usize) * stride..], stride)
593 let mx = (mvx & 7) as usize;
594 let my = (mvy & 7) as usize;
595 mc_block_common(dst, doff, dstride, src, sstride, size, mx, my);
597 pub fn mc_block16x16(dst: &mut [u8], doff: usize, dstride: usize, xpos: usize, ypos: usize,
598 mvx: i16, mvy: i16, src: NAVideoBufferRef<u8>, plane: usize, mc_buf: &mut [u8]) {
599 mc_block(dst, doff, dstride, xpos, ypos, mvx, mvy, src, plane, mc_buf, 16);
601 pub fn mc_block8x8(dst: &mut [u8], doff: usize, dstride: usize, xpos: usize, ypos: usize,
602 mvx: i16, mvy: i16, src: NAVideoBufferRef<u8>, plane: usize, mc_buf: &mut [u8]) {
603 mc_block(dst, doff, dstride, xpos, ypos, mvx, mvy, src, plane, mc_buf, 8);
605 pub fn mc_block4x4(dst: &mut [u8], doff: usize, dstride: usize, xpos: usize, ypos: usize,
606 mvx: i16, mvy: i16, src: NAVideoBufferRef<u8>, plane: usize, mc_buf: &mut [u8]) {
607 mc_block(dst, doff, dstride, xpos, ypos, mvx, mvy, src, plane, mc_buf, 4);
609 pub fn mc_block_special(dst: &mut [u8], doff: usize, dstride: usize, xpos: usize, ypos: usize,
610 mvx: i16, mvy: i16, reffrm: NAVideoBufferRef<u8>, plane: usize,
611 mc_buf: &mut [u8], size: usize, pitch_mode: u8) {
612 const Y_MUL: [isize; 8] = [ 1, 0, 2, 4, 1, 1, 2, 2 ];
613 const Y_OFF: [isize; 8] = [ 0, 4, 0, 0, 1, -1, 1, -1 ];
614 const ILACE_CHROMA: [bool; 8] = [ false, false, true, true, false, false, true, true ]; // mode&2 != 0
616 let pitch_mode = (pitch_mode & 7) as usize;
617 let (xstep, ymul) = if plane == 0 {
618 (Y_OFF[pitch_mode], Y_MUL[pitch_mode])
620 (0, if ILACE_CHROMA[pitch_mode] { 2 } else { 1 })
623 let (w, h) = reffrm.get_dimensions(plane);
624 let wa = if plane == 0 { ((w + 15) & !15) } else { ((w + 7) & !7) } as isize;
625 let ha = if plane == 0 { ((h + 15) & !15) } else { ((h + 7) & !7) } as isize;
626 let start_x = (xpos as isize) - (EDGE_PRE as isize) * xstep;
627 let end_x = (xpos as isize) + ((size + EDGE_POST) as isize) * xstep;
628 let start_y = (ypos as isize) - (EDGE_PRE as isize) * ymul;
629 let end_y = (ypos as isize) + ((size + EDGE_POST) as isize) * ymul;
630 let off = reffrm.get_offset(plane);
631 let stride = reffrm.get_stride(plane);
632 let (src, sstride) = if (start_x >= 0) && (end_x <= wa) && (start_y >= 0) && (end_y <= ha) {
633 let data = reffrm.get_data();
634 (&data[off + (start_x as usize) + (start_y as usize) * stride..],
635 ((stride as isize) + xstep) as usize)
637 let add = (size + EDGE_PRE + EDGE_POST) * (xstep.abs() as usize);
638 let bw = size + EDGE_PRE + EDGE_POST + add;
639 let bh = (end_y - start_y) as usize;
640 let bo = if xstep >= 0 { 0 } else { add };
641 edge_emu(&reffrm, start_x + (bo as isize), start_y, bw, bh, mc_buf, 128, plane);
642 (&mc_buf[bo..], (128 + xstep) as usize)
644 let mx = (mvx & 7) as usize;
645 let my = (mvy & 7) as usize;
647 0 => unimplemented!(),
648 1 => mc_block_common(dst, doff, dstride, src, sstride, size, mx, my),
650 let hsize = size / 2;
653 mc_block_common(dst, doff + x * hsize + y * hsize * dstride, dstride,
654 &src[x * hsize + y * sstride..], sstride * 2, hsize, mx, my);
659 let qsize = size / 4;
662 mc_block_common(dst, doff + x * qsize + y * qsize * dstride, dstride,
663 &src[x * qsize + y * sstride..], sstride * 4, qsize, mx, my);
671 pub fn fade_frame(srcfrm: NAVideoBufferRef<u8>, dstfrm: &mut NASimpleVideoFrame<u8>, alpha: u16, beta: u16) {
672 let mut fade_lut = [0u8; 256];
673 for (i, el) in fade_lut.iter_mut().enumerate() {
675 *el = (y + ((y * beta) >> 8) + alpha).max(0).min(255) as u8;
678 let (w, h) = srcfrm.get_dimensions(0);
679 let (wa, ha) = ((w + 15) & !15, (h + 15) & !15);
680 let soff = srcfrm.get_offset(0);
681 let sstride = srcfrm.get_stride(0);
682 let sdata = srcfrm.get_data();
683 let src = &sdata[soff..];
684 let dstride = dstfrm.stride[0];
685 let dst = &mut dstfrm.data[dstfrm.offset[0]..];
686 for (src, dst) in src.chunks(sstride).zip(dst.chunks_mut(dstride)).take(ha) {
687 for (s, d) in src.iter().zip(dst.iter_mut()).take(wa) {
688 *d = fade_lut[*s as usize];
693 let (w, h) = srcfrm.get_dimensions(plane);
694 let (wa, ha) = ((w + 7) & !7, (h + 7) & !7);
695 let soff = srcfrm.get_offset(plane);
696 let sstride = srcfrm.get_stride(plane);
697 let sdata = srcfrm.get_data();
698 let src = &sdata[soff..];
699 let dstride = dstfrm.stride[plane];
700 let dst = &mut dstfrm.data[dstfrm.offset[plane]..];
701 for (src, dst) in src.chunks(sstride).zip(dst.chunks_mut(dstride)).take(ha) {
702 (&mut dst[0..wa]).copy_from_slice(&src[0..wa]);