1 use nihav_core::frame::*;
2 use nihav_codec_support::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 pub fn add_coeffs4x4(dst: &mut [u8], off: usize, stride: usize, coeffs: &[i16; 16]) {
59 let dst = &mut dst[off..];
60 for (out, src) in dst.chunks_mut(stride).zip(coeffs.chunks(4)) {
61 for (oel, iel) in out.iter_mut().take(4).zip(src.iter()) {
62 *oel = clip_u8(i16::from(*oel) + *iel);
66 pub fn add_coeffs16x1(dst: &mut [u8], off: usize, coeffs: &[i16; 16]) {
67 let dst = &mut dst[off..];
68 for (oel, iel) in dst.iter_mut().take(16).zip(coeffs.iter()) {
69 *oel = clip_u8(i16::from(*oel) + *iel);
75 fn ipred_dc(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
77 if !ipred.has_left && !ipred.has_top {
81 let mut dcshift = match Self::SIZE {
86 for el in ipred.left.iter().take(Self::SIZE) {
87 dcsum += u16::from(*el);
92 for el in ipred.top.iter().take(Self::SIZE) {
93 dcsum += u16::from(*el);
97 dc = ((dcsum + (1 << (dcshift - 1))) >> dcshift) as u8;
99 for _ in 0..Self::SIZE {
100 let out = &mut dst[off..][..Self::SIZE];
101 for el in out.iter_mut() {
107 fn ipred_v(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
108 for _ in 0..Self::SIZE {
109 let out = &mut dst[off..][..Self::SIZE];
110 out.copy_from_slice(&ipred.top[0..Self::SIZE]);
114 fn ipred_h(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
115 for leftel in ipred.left.iter().take(Self::SIZE) {
116 let out = &mut dst[off..][..Self::SIZE];
117 for el in out.iter_mut() {
123 fn ipred_tm(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
124 let tl = i16::from(ipred.tl);
125 for m in 0..Self::SIZE {
126 for n in 0..Self::SIZE {
127 dst[off + n] = clip_u8(i16::from(ipred.left[m]) + i16::from(ipred.top[n]) - tl);
132 fn ipred_const(dst: &mut [u8], off: usize, stride: usize, dc: u8) {
133 for row in dst[off..].chunks_mut(stride).take(Self::SIZE) {
134 for el in row[..Self::SIZE].iter_mut() {
141 pub struct IPred16x16 {}
142 impl IntraPred for IPred16x16 { const SIZE: usize = 16; }
144 pub struct IPred8x8 {}
145 impl IntraPred for IPred8x8 { const SIZE: usize = 8; }
147 macro_rules! load_pred4 {
148 (topleft; $ipred: expr) => {{
149 let tl = u16::from($ipred.tl);
150 let a0 = u16::from($ipred.top[0]);
151 let l0 = u16::from($ipred.left[0]);
152 ((l0 + tl * 2 + a0 + 2) >> 2) as u8
154 (top; $ipred: expr) => {{
155 let tl = u16::from($ipred.tl);
156 let a0 = u16::from($ipred.top[0]);
157 let a1 = u16::from($ipred.top[1]);
158 let a2 = u16::from($ipred.top[2]);
159 let a3 = u16::from($ipred.top[3]);
160 let a4 = u16::from($ipred.top[4]);
161 let p0 = ((tl + a0 * 2 + a1 + 2) >> 2) as u8;
162 let p1 = ((a0 + a1 * 2 + a2 + 2) >> 2) as u8;
163 let p2 = ((a1 + a2 * 2 + a3 + 2) >> 2) as u8;
164 let p3 = ((a2 + a3 * 2 + a4 + 2) >> 2) as u8;
167 (top8; $ipred: expr) => {{
168 let t3 = u16::from($ipred.top[3]);
169 let t4 = u16::from($ipred.top[4]);
170 let t5 = u16::from($ipred.top[5]);
171 let t6 = u16::from($ipred.top[6]);
172 let t7 = u16::from($ipred.top[7]);
173 let p4 = ((t3 + t4 * 2 + t5 + 2) >> 2) as u8;
174 let p5 = ((t4 + t5 * 2 + t6 + 2) >> 2) as u8;
175 let p6 = ((t5 + t6 * 2 + t7 + 2) >> 2) as u8;
176 let p7 = ((t6 + t7 * 2 + t7 + 2) >> 2) as u8;
179 (topavg; $ipred: expr) => {{
180 let tl = u16::from($ipred.tl);
181 let a0 = u16::from($ipred.top[0]);
182 let a1 = u16::from($ipred.top[1]);
183 let a2 = u16::from($ipred.top[2]);
184 let a3 = u16::from($ipred.top[3]);
185 let p0 = ((tl + a0 + 1) >> 1) as u8;
186 let p1 = ((a0 + a1 + 1) >> 1) as u8;
187 let p2 = ((a1 + a2 + 1) >> 1) as u8;
188 let p3 = ((a2 + a3 + 1) >> 1) as u8;
191 (left; $ipred: expr) => {{
192 let tl = u16::from($ipred.tl);
193 let l0 = u16::from($ipred.left[0]);
194 let l1 = u16::from($ipred.left[1]);
195 let l2 = u16::from($ipred.left[2]);
196 let l3 = u16::from($ipred.left[3]);
197 let l4 = u16::from($ipred.left[4]);
198 let p0 = ((tl + l0 * 2 + l1 + 2) >> 2) as u8;
199 let p1 = ((l0 + l1 * 2 + l2 + 2) >> 2) as u8;
200 let p2 = ((l1 + l2 * 2 + l3 + 2) >> 2) as u8;
201 let p3 = ((l2 + l3 * 2 + l4 + 2) >> 2) as u8;
204 (left8; $ipred: expr) => {{
205 let l3 = u16::from($ipred.left[3]);
206 let l4 = u16::from($ipred.left[4]);
207 let l5 = u16::from($ipred.left[5]);
208 let l6 = u16::from($ipred.left[6]);
209 let l7 = u16::from($ipred.left[7]);
210 let p4 = ((l3 + l4 * 2 + l5 + 2) >> 2) as u8;
211 let p5 = ((l4 + l5 * 2 + l6 + 2) >> 2) as u8;
212 let p6 = ((l5 + l6 * 2 + l7 + 2) >> 2) as u8;
213 let p7 = ((l6 + l7 * 2 + l7 + 2) >> 2) as u8;
216 (leftavg; $ipred: expr) => {{
217 let tl = u16::from($ipred.tl);
218 let l0 = u16::from($ipred.left[0]);
219 let l1 = u16::from($ipred.left[1]);
220 let l2 = u16::from($ipred.left[2]);
221 let l3 = u16::from($ipred.left[3]);
222 let p0 = ((tl + l0 + 1) >> 1) as u8;
223 let p1 = ((l0 + l1 + 1) >> 1) as u8;
224 let p2 = ((l1 + l2 + 1) >> 1) as u8;
225 let p3 = ((l2 + l3 + 1) >> 1) as u8;
230 pub struct IPred4x4 {}
232 pub fn ipred_dc(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
234 for el in ipred.left.iter().take(4) {
235 dcsum += u16::from(*el);
237 for el in ipred.top.iter().take(4) {
238 dcsum += u16::from(*el);
240 let dc = ((dcsum + (1 << 2)) >> 3) as u8;
242 let out = &mut dst[off..][..4];
243 for el in out.iter_mut() {
249 pub fn ipred_tm(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
250 let tl = i16::from(ipred.tl);
253 dst[off + n] = clip_u8(i16::from(ipred.left[m]) + i16::from(ipred.top[n]) - tl);
258 pub fn ipred_ve(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
259 let (v0, v1, v2, v3) = load_pred4!(top; ipred);
260 let vert_pred = [v0, v1, v2, v3];
262 let out = &mut dst[off..][..4];
263 out.copy_from_slice(&vert_pred);
267 pub fn ipred_he(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
268 let (p0, p1, p2, _) = load_pred4!(left; ipred);
269 let p3 = ((u16::from(ipred.left[2]) + u16::from(ipred.left[3]) * 3 + 2) >> 2) as u8;
270 let hor_pred = [p0, p1, p2, p3];
273 dst[off + n] = hor_pred[m];
278 pub fn ipred_ld(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
279 let (_, p0, p1, p2) = load_pred4!(top; ipred);
280 let (p3, p4, p5, p6) = load_pred4!(top8; ipred);
282 dst[off + 0] = p0; dst[off + 1] = p1; dst[off + 2] = p2; dst[off + 3] = p3;
284 dst[off + 0] = p1; dst[off + 1] = p2; dst[off + 2] = p3; dst[off + 3] = p4;
286 dst[off + 0] = p2; dst[off + 1] = p3; dst[off + 2] = p4; dst[off + 3] = p5;
288 dst[off + 0] = p3; dst[off + 1] = p4; dst[off + 2] = p5; dst[off + 3] = p6;
290 pub fn ipred_rd(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
291 let tl = load_pred4!(topleft; ipred);
292 let (l0, l1, l2, _) = load_pred4!(left; ipred);
293 let (t0, t1, t2, _) = load_pred4!(top; ipred);
295 dst[off + 0] = tl; dst[off + 1] = t0; dst[off + 2] = t1; dst[off + 3] = t2;
297 dst[off + 0] = l0; dst[off + 1] = tl; dst[off + 2] = t0; dst[off + 3] = t1;
299 dst[off + 0] = l1; dst[off + 1] = l0; dst[off + 2] = tl; dst[off + 3] = t0;
301 dst[off + 0] = l2; dst[off + 1] = l1; dst[off + 2] = l0; dst[off + 3] = tl;
303 pub fn ipred_vr(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
304 let tl = load_pred4!(topleft; ipred);
305 let (l0, l1, _, _) = load_pred4!(left; ipred);
306 let (t0, t1, t2, _) = load_pred4!(top; ipred);
307 let (m0, m1, m2, m3) = load_pred4!(topavg; ipred);
309 dst[off + 0] = m0; dst[off + 1] = m1; dst[off + 2] = m2; dst[off + 3] = m3;
311 dst[off + 0] = tl; dst[off + 1] = t0; dst[off + 2] = t1; dst[off + 3] = t2;
313 dst[off + 0] = l0; dst[off + 1] = m0; dst[off + 2] = m1; dst[off + 3] = m2;
315 dst[off + 0] = l1; dst[off + 1] = tl; dst[off + 2] = t0; dst[off + 3] = t1;
317 pub fn ipred_vl(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
318 let (_, t1, t2, t3) = load_pred4!(top; ipred);
319 let (t4, t5, t6, _) = load_pred4!(top8; ipred);
320 let (_, m1, m2, m3) = load_pred4!(topavg; ipred);
321 let m4 = ((u16::from(ipred.top[3]) + u16::from(ipred.top[4]) + 1) >> 1) as u8;
323 dst[off + 0] = m1; dst[off + 1] = m2; dst[off + 2] = m3; dst[off + 3] = m4;
325 dst[off + 0] = t1; dst[off + 1] = t2; dst[off + 2] = t3; dst[off + 3] = t4;
327 dst[off + 0] = m2; dst[off + 1] = m3; dst[off + 2] = m4; dst[off + 3] = t5;
329 dst[off + 0] = t2; dst[off + 1] = t3; dst[off + 2] = t4; dst[off + 3] = t6;
331 pub fn ipred_hd(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
332 let tl = load_pred4!(topleft; ipred);
333 let (l0, l1, l2, _) = load_pred4!(left; ipred);
334 let (m0, m1, m2, m3) = load_pred4!(leftavg; ipred);
335 let (t0, t1, _, _) = load_pred4!(top; ipred);
337 dst[off + 0] = m0; dst[off + 1] = tl; dst[off + 2] = t0; dst[off + 3] = t1;
339 dst[off + 0] = m1; dst[off + 1] = l0; dst[off + 2] = m0; dst[off + 3] = tl;
341 dst[off + 0] = m2; dst[off + 1] = l1; dst[off + 2] = m1; dst[off + 3] = l0;
343 dst[off + 0] = m3; dst[off + 1] = l2; dst[off + 2] = m2; dst[off + 3] = l1;
345 pub fn ipred_hu(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) {
346 let (_, m1, m2, m3) = load_pred4!(leftavg; ipred);
347 let (_, l1, l2, _) = load_pred4!(left; ipred);
348 let l3 = ((u16::from(ipred.left[2]) + u16::from(ipred.left[3]) * 3 + 2) >> 2) as u8;
349 let p3 = ipred.left[3];
351 dst[off + 0] = m1; dst[off + 1] = l1; dst[off + 2] = m2; dst[off + 3] = l2;
353 dst[off + 0] = m2; dst[off + 1] = l2; dst[off + 2] = m3; dst[off + 3] = l3;
355 dst[off + 0] = m3; dst[off + 1] = l3; dst[off + 2] = p3; dst[off + 3] = p3;
357 dst[off + 0] = p3; dst[off + 1] = p3; dst[off + 2] = p3; dst[off + 3] = p3;
361 const VP7_BICUBIC_FILTERS: [[i16; 6]; 8] = [
362 [ 0, 0, 128, 0, 0, 0 ],
363 [ 0, -6, 123, 12, -1, 0 ],
364 [ 2, -11, 108, 36, -8, 1 ],
365 [ 0, -9, 93, 50, -6, 0 ],
366 [ 3, -16, 77, 77, -16, 3 ],
367 [ 0, -6, 50, 93, -9, 0 ],
368 [ 1, -8, 36, 108, -11, 2 ],
369 [ 0, -1, 12, 123, -6, 0 ]
372 macro_rules! interpolate {
373 ($src: expr, $off: expr, $step: expr, $mode: expr) => {{
374 let s0 = i32::from($src[$off + 0 * $step]);
375 let s1 = i32::from($src[$off + 1 * $step]);
376 let s2 = i32::from($src[$off + 2 * $step]);
377 let s3 = i32::from($src[$off + 3 * $step]);
378 let s4 = i32::from($src[$off + 4 * $step]);
379 let s5 = i32::from($src[$off + 5 * $step]);
380 let filt = &VP7_BICUBIC_FILTERS[$mode];
381 let src = [s0, s1, s2, s3, s4, s5];
383 for (s, c) in src.iter().zip(filt.iter()) {
384 val += s * i32::from(*c);
386 clip_u8((val >> 7) as i16)
390 const EDGE_PRE: usize = 2;
391 const EDGE_POST: usize = 4;
392 const TMP_STRIDE: usize = 16;
394 fn mc_block_common(dst: &mut [u8], mut doff: usize, dstride: usize, src: &[u8], sstride: usize, size: usize, mx: usize, my: usize) {
395 if (mx == 0) && (my == 0) {
396 let dst = &mut dst[doff..];
397 let src = &src[EDGE_PRE + EDGE_PRE * sstride..];
398 for (out, src) in dst.chunks_mut(dstride).take(size).zip(src.chunks(sstride)) {
399 (&mut out[0..size]).copy_from_slice(&src[0..size]);
402 let src = &src[EDGE_PRE * sstride..];
403 for src in src.chunks(sstride).take(size) {
405 dst[doff + x] = interpolate!(src, x, 1, mx);
410 let src = &src[EDGE_PRE..];
413 dst[doff + x] = interpolate!(src, x + y * sstride, sstride, my);
418 let mut tmp = [0u8; TMP_STRIDE * (16 + EDGE_PRE + EDGE_POST)];
419 for (y, dst) in tmp.chunks_mut(TMP_STRIDE).take(size + EDGE_PRE + EDGE_POST).enumerate() {
421 dst[x] = interpolate!(src, x + y * sstride, 1, mx);
426 dst[doff + x] = interpolate!(tmp, x + y * TMP_STRIDE, TMP_STRIDE, my);
432 fn mc_block(dst: &mut [u8], doff: usize, dstride: usize, xpos: usize, ypos: usize,
433 mvx: i16, mvy: i16, reffrm: NAVideoBufferRef<u8>, plane: usize,
434 mc_buf: &mut [u8], size: usize) {
435 if (mvx == 0) && (mvy == 0) {
436 let dst = &mut dst[doff..];
437 let sstride = reffrm.get_stride(plane);
438 let srcoff = reffrm.get_offset(plane) + xpos + ypos * sstride;
439 let src = &reffrm.get_data();
440 let src = &src[srcoff..];
441 for (out, src) in dst.chunks_mut(dstride).take(size).zip(src.chunks(sstride)) {
442 (&mut out[0..size]).copy_from_slice(&src[0..size]);
446 let (w, h) = reffrm.get_dimensions(plane);
447 let wa = if plane == 0 { (w + 15) & !15 } else { (w + 7) & !7 } as isize;
448 let ha = if plane == 0 { (h + 15) & !15 } else { (h + 7) & !7 } as isize;
449 let bsize = (size as isize) + (EDGE_PRE as isize) + (EDGE_POST as isize);
450 let ref_x = (xpos as isize) + ((mvx >> 3) as isize) - (EDGE_PRE as isize);
451 let ref_y = (ypos as isize) + ((mvy >> 3) as isize) - (EDGE_PRE as isize);
453 let (src, sstride) = if (ref_x < 0) || (ref_x + bsize > wa) || (ref_y < 0) || (ref_y + bsize > ha) {
454 edge_emu(&reffrm, ref_x, ref_y, bsize as usize, bsize as usize, mc_buf, 32, plane, 4);
455 (mc_buf as &[u8], 32)
457 let off = reffrm.get_offset(plane);
458 let stride = reffrm.get_stride(plane);
459 let data = reffrm.get_data();
460 (&data[off + (ref_x as usize) + (ref_y as usize) * stride..], stride)
462 let mx = (mvx & 7) as usize;
463 let my = (mvy & 7) as usize;
464 mc_block_common(dst, doff, dstride, src, sstride, size, mx, my);
466 pub fn mc_block16x16(dst: &mut [u8], doff: usize, dstride: usize, xpos: usize, ypos: usize,
467 mvx: i16, mvy: i16, src: NAVideoBufferRef<u8>, plane: usize, mc_buf: &mut [u8]) {
468 mc_block(dst, doff, dstride, xpos, ypos, mvx, mvy, src, plane, mc_buf, 16);
470 pub fn mc_block8x8(dst: &mut [u8], doff: usize, dstride: usize, xpos: usize, ypos: usize,
471 mvx: i16, mvy: i16, src: NAVideoBufferRef<u8>, plane: usize, mc_buf: &mut [u8]) {
472 mc_block(dst, doff, dstride, xpos, ypos, mvx, mvy, src, plane, mc_buf, 8);
474 pub fn mc_block4x4(dst: &mut [u8], doff: usize, dstride: usize, xpos: usize, ypos: usize,
475 mvx: i16, mvy: i16, src: NAVideoBufferRef<u8>, plane: usize, mc_buf: &mut [u8]) {
476 mc_block(dst, doff, dstride, xpos, ypos, mvx, mvy, src, plane, mc_buf, 4);
478 pub fn mc_block_special(dst: &mut [u8], doff: usize, dstride: usize, xpos: usize, ypos: usize,
479 mvx: i16, mvy: i16, reffrm: NAVideoBufferRef<u8>, plane: usize,
480 mc_buf: &mut [u8], size: usize, pitch_mode: u8) {
481 const Y_MUL: [isize; 8] = [ 1, 0, 2, 4, 1, 1, 2, 2 ];
482 const Y_OFF: [isize; 8] = [ 0, 4, 0, 0, 1, -1, 1, -1 ];
483 const ILACE_CHROMA: [bool; 8] = [ false, false, true, true, false, false, true, true ]; // mode&2 != 0
485 let pitch_mode = (pitch_mode & 7) as usize;
486 let (xstep, ymul) = if plane == 0 {
487 (Y_OFF[pitch_mode], Y_MUL[pitch_mode])
489 (0, if ILACE_CHROMA[pitch_mode] { 2 } else { 1 })
492 let (w, h) = reffrm.get_dimensions(plane);
493 let wa = if plane == 0 { (w + 15) & !15 } else { (w + 7) & !7 } as isize;
494 let ha = if plane == 0 { (h + 15) & !15 } else { (h + 7) & !7 } as isize;
495 let mut start_x = (xpos as isize) + ((mvx >> 3) as isize) - (EDGE_PRE as isize);
496 let mut end_x = (xpos as isize) + ((mvx >> 3) as isize) + ((size + EDGE_POST) as isize);
498 start_x -= (size + EDGE_POST) as isize;
499 } else if xstep > 0 {
500 end_x += (size as isize) * xstep;
502 let mut start_y = (ypos as isize) + ((mvy >> 3) as isize) - (EDGE_PRE as isize) * ymul;
503 let mut end_y = (ypos as isize) + ((mvy >> 3) as isize) + ((size + EDGE_POST) as isize) * ymul;
505 start_y -= EDGE_PRE as isize;
506 end_y += (EDGE_POST + 1) as isize;
508 let off = reffrm.get_offset(plane);
509 let stride = reffrm.get_stride(plane);
510 let (src, sstride) = if (start_x >= 0) && (end_x <= wa) && (start_y >= 0) && (end_y <= ha) {
511 let data = reffrm.get_data();
512 (&data[off + (start_x as usize) + (start_y as usize) * stride..],
513 ((stride as isize) + xstep) as usize)
515 let add = (size + EDGE_PRE + EDGE_POST) * (xstep.abs() as usize);
516 let bw = size + EDGE_PRE + EDGE_POST + add;
517 let bh = (end_y - start_y) as usize;
518 let bo = if xstep >= 0 { 0 } else { add };
519 edge_emu(&reffrm, start_x + (bo as isize), start_y, bw, bh, mc_buf, 128, plane, 0);
520 (&mc_buf[bo..], (128 + xstep) as usize)
522 let mx = (mvx & 7) as usize;
523 let my = (mvy & 7) as usize;
525 0 => unimplemented!(),
526 1 => mc_block_common(dst, doff, dstride, src, sstride, size, mx, my),
528 let hsize = size / 2;
531 mc_block_common(dst, doff + x * hsize + y * hsize * dstride, dstride,
532 &src[x * hsize + y * sstride..], sstride * 2, hsize, mx, my);
537 let qsize = size / 4;
540 mc_block_common(dst, doff + x * qsize + y * qsize * dstride, dstride,
541 &src[x * qsize + y * sstride..], sstride * 4, qsize, mx, my);