1 use nihav_core::frame::*;
2 use nihav_codec_support::codecs::{MV, ZERO_MV};
3 use nihav_codec_support::data::GenericCache;
4 use super::super::vp78::{PredMode, MVSplitMode, SubMVRef};
5 use super::super::vp78data::*;
6 use super::super::vp78dsp::*;
7 use super::super::vp7data::*;
8 use super::super::vp7dsp::*;
12 Intra(PredMode, PredMode),
13 Intra4x4([PredMode; 16], [u8; 16], PredMode),
14 InterNoMV(bool, [u8; 4]),
15 InterNearest(bool, [u8; 4]),
16 InterNear(bool, [u8; 4]),
17 InterMV(bool, [u8; 4], MV),
18 InterSplitMV(bool, [u8; 4], MVSplitMode, [SubMVRef; 16], [MV; 16]),
22 pub fn is_intra(&self) -> bool {
25 MBType::Intra4x4(_, _, _) => true,
29 pub fn get_last(&self) -> bool {
31 MBType::InterNoMV(last, _) |
32 MBType::InterNearest(last, _) |
33 MBType::InterNear(last, _) |
34 MBType::InterMV(last, _, _) |
35 MBType::InterSplitMV(last, _, _, _, _) => last,
41 impl Default for MBType {
42 fn default() -> Self { MBType::Intra(PredMode::DCPred, PredMode::DCPred) }
45 pub fn get_block_difference(dst: &mut [i16; 16], src1: &[u8; 16], src2: &[u8; 16]) {
46 for (dst, (&src1, &src2)) in dst.iter_mut().zip(src1.iter().zip(src2.iter())) {
47 *dst = i16::from(src1) - i16::from(src2);
50 pub fn get_difference_dist(old: &[u8; 16], new: &[u8; 16], diff: &[i16; 16]) -> u32 {
52 for ((&old, &new), &diff) in old.iter().zip(new.iter()).zip(diff.iter()) {
53 let nval = (i16::from(new) + diff).max(0).min(255);
54 let oval = i16::from(old);
55 dist += (i32::from(nval - oval) * i32::from(nval - oval)) as u32;
60 pub fn requant_y2_dc(val: &mut i16, q: usize) {
61 *val = *val / Y2_DC_QUANTS[q] * Y2_DC_QUANTS[q];
65 fn has_nz(&self) -> bool;
68 fn requant_y(&mut self, q: usize);
69 fn quant(&mut self, q: usize, ctype: usize);
70 fn dequant(&mut self, q: usize, ctype: usize);
73 impl DCTBlock for [i16; 16] {
74 fn has_nz(&self) -> bool {
75 for &el in self.iter() {
82 #[allow(clippy::erasing_op)]
83 #[allow(clippy::identity_op)]
85 let mut tmp = [0i16; 16];
87 let s0 = i32::from(self[i + 4 * 0]);
88 let s1 = i32::from(self[i + 4 * 1]);
89 let s2 = i32::from(self[i + 4 * 2]);
90 let s3 = i32::from(self[i + 4 * 3]);
92 let t0 = (s0 + s3).wrapping_mul(23170) + 0x2000;
93 let t1 = (s1 + s2).wrapping_mul(23170);
96 let t4 = t2.wrapping_mul(30274) + t3.wrapping_mul(12540) + 0x2000;
97 let t5 = t2.wrapping_mul(12540) - t3.wrapping_mul(30274) + 0x2000;
99 tmp[i + 0 * 4] = ((t0 + t1) >> 14) as i16;
100 tmp[i + 1 * 4] = ( t4 >> 14) as i16;
101 tmp[i + 2 * 4] = ((t0 - t1) >> 14) as i16;
102 tmp[i + 3 * 4] = ( t5 >> 14) as i16;
104 for (src, dst) in tmp.chunks(4).zip(self.chunks_mut(4)) {
105 let s0 = i32::from(src[0]);
106 let s1 = i32::from(src[1]);
107 let s2 = i32::from(src[2]);
108 let s3 = i32::from(src[3]);
110 let t0 = (s0 + s3).wrapping_mul(23170) + 0x8000;
111 let t1 = (s1 + s2).wrapping_mul(23170);
114 let t4 = t2.wrapping_mul(30274) + t3.wrapping_mul(12540) + 0x8000;
115 let t5 = t2.wrapping_mul(12540) - t3.wrapping_mul(30274) + 0x8000;
117 dst[0] = ((t0 + t1) >> 16) as i16;
118 dst[1] = ( t4 >> 16) as i16;
119 dst[2] = ((t0 - t1) >> 16) as i16;
120 dst[3] = ( t5 >> 16) as i16;
123 fn idct(&mut self) { idct4x4(self) }
124 fn requant_y(&mut self, q: usize) {
125 self[0] = self[0] / Y_DC_QUANTS[q] * Y_DC_QUANTS[q];
126 for el in self[1..].iter_mut() {
127 *el = *el / Y_AC_QUANTS[q] * Y_AC_QUANTS[q];
130 fn quant(&mut self, q: usize, ctype: usize) {
131 let (q_dc, q_ac) = match ctype {
132 0 | 3 => (Y_DC_QUANTS[q], Y_AC_QUANTS[q]),
133 2 => (UV_DC_QUANTS[q], UV_AC_QUANTS[q]),
134 _ => (Y2_DC_QUANTS[q], Y2_AC_QUANTS[q]),
137 for el in self[1..].iter_mut() {
141 fn dequant(&mut self, q: usize, ctype: usize) {
142 let (q_dc, q_ac) = match ctype {
143 0 | 3 => (Y_DC_QUANTS[q], Y_AC_QUANTS[q]),
144 2 => (UV_DC_QUANTS[q], UV_AC_QUANTS[q]),
145 _ => (Y2_DC_QUANTS[q], Y2_AC_QUANTS[q]),
148 for el in self[1..].iter_mut() {
154 pub trait IPredBlock16 {
155 fn ipred16(&mut self, stride: usize, mode: PredMode, ipred: &IPredContext);
157 pub trait IPredBlock8 {
158 fn ipred8 (&mut self, stride: usize, mode: PredMode, ipred: &IPredContext);
160 pub trait IPredBlock4 {
161 fn ipred4 (&mut self, stride: usize, mode: PredMode, ipred: &IPredContext);
164 impl IPredBlock16 for [u8; 256] {
165 fn ipred16(&mut self, stride: usize, mode: PredMode, ipred: &IPredContext) {
167 PredMode::DCPred => IPred16x16::ipred_dc(self, 0, stride, ipred),
168 PredMode::HPred => IPred16x16::ipred_h (self, 0, stride, ipred),
169 PredMode::VPred => IPred16x16::ipred_v (self, 0, stride, ipred),
170 PredMode::TMPred => IPred16x16::ipred_tm(self, 0, stride, ipred),
175 impl IPredBlock8 for [u8; 64] {
176 fn ipred8(&mut self, stride: usize, mode: PredMode, ipred: &IPredContext) {
178 PredMode::DCPred => IPred8x8::ipred_dc(self, 0, stride, ipred),
179 PredMode::HPred => IPred8x8::ipred_h (self, 0, stride, ipred),
180 PredMode::VPred => IPred8x8::ipred_v (self, 0, stride, ipred),
181 PredMode::TMPred => IPred8x8::ipred_tm(self, 0, stride, ipred),
186 impl IPredBlock4 for &mut [u8] {
187 fn ipred4(&mut self, stride: usize, mode: PredMode, ipred: &IPredContext) {
189 PredMode::DCPred => IPred4x4::ipred_dc(self, 0, stride, ipred),
190 PredMode::HPred => IPred4x4::ipred_he(self, 0, stride, ipred),
191 PredMode::VPred => IPred4x4::ipred_ve(self, 0, stride, ipred),
192 PredMode::TMPred => IPred4x4::ipred_tm(self, 0, stride, ipred),
193 PredMode::LDPred => IPred4x4::ipred_ld(self, 0, stride, ipred),
194 PredMode::RDPred => IPred4x4::ipred_rd(self, 0, stride, ipred),
195 PredMode::VRPred => IPred4x4::ipred_vr(self, 0, stride, ipred),
196 PredMode::VLPred => IPred4x4::ipred_vl(self, 0, stride, ipred),
197 PredMode::HDPred => IPred4x4::ipred_hd(self, 0, stride, ipred),
198 PredMode::HUPred => IPred4x4::ipred_hu(self, 0, stride, ipred),
203 impl IPredBlock4 for [u8; 16] {
204 fn ipred4(&mut self, stride: usize, mode: PredMode, ipred: &IPredContext) {
205 (self as &mut [u8]).ipred4(stride, mode, ipred);
209 pub struct LumaIterator<'a> {
214 impl<'a> Iterator for LumaIterator<'a> {
215 type Item = [u8; 16];
216 fn next(&mut self) -> Option<Self::Item> {
218 let mut blk = [0; 16];
219 let off = (self.blkno & 3) * 4 + (self.blkno >> 2) * 16 * 4;
220 for (dst, src) in blk.chunks_exact_mut(4).zip(self.luma[off..].chunks(16)) {
221 dst.copy_from_slice(&src[..4]);
231 pub struct ChromaIterator<'a> {
232 chroma: &'a [u8; 64],
236 impl<'a> Iterator for ChromaIterator<'a> {
237 type Item = [u8; 16];
238 fn next(&mut self) -> Option<Self::Item> {
240 let mut blk = [0; 16];
241 let off = (self.blkno & 1) * 4 + (self.blkno >> 1) * 8 * 4;
242 for (dst, src) in blk.chunks_exact_mut(4).zip(self.chroma[off..].chunks(8)) {
243 dst.copy_from_slice(&src[..4]);
253 pub struct SrcBlock {
255 pub chroma: [[u8; 64]; 2],
258 impl Default for SrcBlock {
259 fn default() -> Self {
260 unsafe { std::mem::zeroed() }
265 pub fn new() -> Self { Self::default() }
266 pub fn is_flat(&self) -> bool {
267 let y0 = self.luma[0];
268 for &el in self.luma[1..].iter() {
275 pub fn apply_ipred_luma(&self, mode: PredMode, ipred: &IPredContext, dst: &mut Residue) {
276 let mut tmp = [0; 256];
277 (&mut tmp).ipred16(16, mode, ipred);
278 dst.set_luma_from_diff(&self.luma, &tmp);
280 pub fn fill_ipred_luma(&mut self, mode: PredMode, ipred: &IPredContext) {
281 self.luma.ipred16(16, mode, ipred);
283 pub fn apply_ipred_chroma(&self, mode: PredMode, ipred_u: &IPredContext, ipred_v: &IPredContext, dst: &mut Residue) {
284 let mut tmp = [[0u8; 64]; 2];
285 tmp[0].ipred8(8, mode, ipred_u);
286 tmp[1].ipred8(8, mode, ipred_v);
287 dst.set_chroma_from_diff(&self.chroma, &tmp);
289 pub fn fill_ipred_chroma(&mut self, mode: PredMode, ipred_u: &IPredContext, ipred_v: &IPredContext) {
290 self.chroma[0].ipred8(8, mode, ipred_u);
291 self.chroma[1].ipred8(8, mode, ipred_v);
294 pub fn luma_blocks(&self) -> LumaIterator {
295 LumaIterator{ luma: &self.luma, blkno: 0 }
297 pub fn chroma_blocks(&self, plane: usize) -> ChromaIterator {
298 ChromaIterator{ chroma: &self.chroma[plane], blkno: 0 }
304 pub luma: [[i16; 16]; 16],
306 pub chroma: [[[i16; 16]; 4]; 2],
311 impl Default for Residue {
312 fn default() -> Self {
313 unsafe { std::mem::zeroed() }
318 pub fn new() -> Self { Self::default() }
319 pub fn reset(&mut self) {
323 pub fn add_residue(&mut self, dst: &mut SrcBlock) {
327 for (dst, src) in dst.luma.chunks_mut(16 * 4).zip(self.luma.chunks(4)) {
328 for (x, blk) in src.iter().enumerate() {
329 for (drow, srow) in dst[x * 4..].chunks_mut(16).zip(blk.chunks(4)) {
330 for (del, &sel) in drow.iter_mut().zip(srow.iter()) {
331 *del = (i16::from(*del) + sel).max(0).min(255) as u8;
336 for (dchroma, schroma) in dst.chroma.iter_mut().zip(self.chroma.iter()) {
337 for (dst, src) in dchroma.chunks_mut(8 * 4).zip(schroma.chunks(2)) {
338 for (x, blk) in src.iter().enumerate() {
339 for (drow, srow) in dst[x * 4..].chunks_mut(8).zip(blk.chunks(4)) {
340 for (del, &sel) in drow.iter_mut().zip(srow.iter()) {
341 *del = (i16::from(*del) + sel).max(0).min(255) as u8;
348 pub fn add_residue_chroma(&mut self, dst: &mut SrcBlock) {
349 let q = self.q as usize;
350 for (dchroma, schroma) in dst.chroma.iter_mut().zip(self.chroma.iter_mut()) {
351 for (dst, src) in dchroma.chunks_mut(8 * 4).zip(schroma.chunks_mut(2)) {
352 for (x, blk) in src.iter_mut().enumerate() {
353 blk[0] *= UV_DC_QUANTS[q];
354 for el in blk[1..].iter_mut() {
356 *el *= UV_AC_QUANTS[q];
360 for (drow, srow) in dst[x * 4..].chunks_mut(8).zip(blk.chunks(4)) {
361 for (del, &sel) in drow.iter_mut().zip(srow.iter()) {
362 *del = (i16::from(*del) + sel).max(0).min(255) as u8;
369 pub fn set_luma_from_diff(&mut self, blk1: &[u8; 256], blk2: &[u8; 256]) {
370 for (dst, (src1, src2)) in self.luma.chunks_mut(4).zip(blk1.chunks(16 * 4).zip(blk2.chunks(16 * 4))) {
371 for (x, blk) in dst.iter_mut().enumerate() {
372 for (dst, (row1, row2)) in blk.chunks_mut(4).zip(src1[x * 4..].chunks(16).zip(src2[x * 4..].chunks(16))) {
373 for (dst, (&a, &b)) in dst.iter_mut().zip(row1.iter().zip(row2.iter())) {
374 *dst = i16::from(a) - i16::from(b);
380 pub fn set_chroma_from_diff(&mut self, blk1: &[[u8; 64]; 2], blk2: &[[u8; 64]; 2]) {
381 for (chroma, (src1, src2)) in self.chroma.iter_mut().zip(blk1.iter().zip(blk2.iter())) {
382 for (dst, (src1, src2)) in chroma.chunks_mut(2).zip(src1.chunks(8 * 4).zip(src2.chunks(8 * 4))) {
383 for (x, blk) in dst.iter_mut().enumerate() {
384 for (dst, (row1, row2)) in blk.chunks_mut(4).zip(src1[x * 4..].chunks(8).zip(src2[x * 4..].chunks(8))) {
385 for (dst, (&a, &b)) in dst.iter_mut().zip(row1.iter().zip(row2.iter())) {
386 *dst = i16::from(a) - i16::from(b);
393 pub fn fdct(&mut self) {
397 pub fn fdct_luma(&mut self) {
398 for blk in self.luma.iter_mut() {
402 pub fn fdct_chroma(&mut self) {
403 for chroma in self.chroma.iter_mut() {
404 for blk in chroma.iter_mut() {
409 pub fn fdct_dc_block(&mut self) {
410 for (dc, blk) in self.dcs.iter_mut().zip(self.luma.iter_mut()) {
417 pub fn idct(&mut self) {
421 pub fn idct_luma(&mut self) {
424 for (&dc, blk) in self.dcs.iter().zip(self.luma.iter_mut()) {
428 for blk in self.luma.iter_mut() {
432 pub fn idct_chroma(&mut self) {
433 for chroma in self.chroma.iter_mut() {
434 for blk in chroma.iter_mut() {
439 pub fn quant(&mut self, q: usize) {
441 self.quant_chroma(q);
444 pub fn quant_luma(&mut self, q: usize) {
446 self.dcs[0] /= Y2_DC_QUANTS[q];
447 for el in self.dcs[1..].iter_mut() {
449 *el /= Y2_AC_QUANTS[q];
453 for blk in self.luma.iter_mut() {
454 blk[0] /= Y_DC_QUANTS[q];
455 for el in blk[1..].iter_mut() {
457 *el /= Y_AC_QUANTS[q];
463 pub fn quant_chroma(&mut self, q: usize) {
464 for chroma in self.chroma.iter_mut() {
465 for blk in chroma.iter_mut() {
466 blk[0] /= UV_DC_QUANTS[q];
467 for el in blk[1..].iter_mut() {
469 *el /= UV_AC_QUANTS[q];
476 pub fn dequant(&mut self) {
478 self.dequant_chroma();
480 pub fn dequant_luma(&mut self) {
481 let q = self.q as usize;
483 self.dcs[0] *= Y2_DC_QUANTS[q];
484 for el in self.dcs[1..].iter_mut() {
486 *el *= Y2_AC_QUANTS[q];
490 for blk in self.luma.iter_mut() {
491 blk[0] *= Y_DC_QUANTS[q];
492 for el in blk[1..].iter_mut() {
494 *el *= Y_AC_QUANTS[q];
499 pub fn dequant_chroma(&mut self) {
500 let q = self.q as usize;
501 for chroma in self.chroma.iter_mut() {
502 for blk in chroma.iter_mut() {
503 blk[0] *= UV_DC_QUANTS[q];
504 for el in blk[1..].iter_mut() {
506 *el *= UV_AC_QUANTS[q];
514 pub fn load_blocks(src: &NAVideoBuffer<u8>, sblocks: &mut Vec<SrcBlock>) {
515 let data = src.get_data();
516 let y = &data[src.get_offset(0)..];
517 let u = &data[src.get_offset(1)..];
518 let v = &data[src.get_offset(2)..];
519 let ystride = src.get_stride(0);
520 let ustride = src.get_stride(1);
521 let vstride = src.get_stride(2);
522 let (width, height) = src.get_dimensions(0);
525 for (ystrip, (ustrip, vstrip)) in y.chunks(ystride * 16).take((height + 15) / 16).zip(u.chunks(ustride * 8).zip(v.chunks(vstride * 8))) {
526 for x in (0..width).step_by(16) {
527 let mut sblk = SrcBlock::default();
529 for (dst, src) in sblk.luma.chunks_mut(16).zip(ystrip[x..].chunks(ystride)) {
530 dst.copy_from_slice(&src[..16]);
532 for (dst, src) in sblk.chroma[0].chunks_mut(8).zip(ustrip[x / 2..].chunks(ustride)) {
533 dst.copy_from_slice(&src[..8]);
535 for (dst, src) in sblk.chroma[1].chunks_mut(8).zip(vstrip[x / 2..].chunks(vstride)) {
536 dst.copy_from_slice(&src[..8]);
543 pub struct YModePred {
544 pub cache: GenericCache<PredMode>,
548 fn resize(&mut self, mb_w: usize) {
549 self.cache = GenericCache::new(4, mb_w * 4 + 1, PredMode::DCPred);
551 pub fn set_mode(&mut self, mb_x: usize, mode: PredMode) {
552 for row in self.cache.data[self.cache.xpos + mb_x * 4..].chunks_mut(self.cache.stride).take(4) {
553 for el in row[..4].iter_mut() {
554 *el = mode.to_b_mode();
558 pub fn set_modes4x4(&mut self, mb_x: usize, imodes: &[PredMode; 16], ctx: &mut [u8; 16]) {
559 let mut off = self.cache.xpos + mb_x * 4;
562 let top_idx = self.cache.data[off + x - self.cache.stride].to_b_index();
563 let left_idx = self.cache.data[off + x - 1].to_b_index();
564 self.cache.data[off + x] = imodes[x + y * 4];
565 ctx[x + y * 4] = ((top_idx * 10) + left_idx) as u8;
567 off += self.cache.stride;
572 impl Default for YModePred {
573 fn default() -> Self {
575 cache: GenericCache::new(0, 0, PredMode::DCPred)
581 pub struct BlockPCtx {
583 pub nz_y_top: [bool; 4],
584 pub nz_y_left: [bool; 4],
585 pub nz_c_top: [[bool; 2]; 2],
586 pub nz_c_left: [[bool; 2]; 2],
590 pub struct PredContext {
594 pub top_line_y: Vec<u8>,
595 pub top_line_u: Vec<u8>,
596 pub top_line_v: Vec<u8>,
601 pub left_y: [u8; 16],
602 pub left_u: [u8; 16],
603 pub left_v: [u8; 16],
605 pub dc_last: [i16; 2],
606 pub dc_count: [usize; 2],
607 dc_last_saved: [i16; 2],
608 dc_count_saved: [usize; 2],
609 pub nz_y2_top: Vec<bool>,
610 pub nz_y2_left: bool,
611 pub nz_y_top: Vec<bool>,
612 pub nz_y_left: [bool; 4],
613 pub nz_c_top: [Vec<bool>; 2],
614 pub nz_c_left: [[bool; 2]; 2],
616 pub ymodes: YModePred,
619 pub mv_stride: usize,
624 pub fn new() -> Self { Self::default() }
625 pub fn resize(&mut self, mb_w: usize, mb_h: usize) {
629 self.top_line_y.resize(mb_w * 16 + 1, 0);
630 self.top_line_u.resize(mb_w * 8 + 1, 0);
631 self.top_line_v.resize(mb_w * 8 + 1, 0);
633 self.nz_y2_top.resize(mb_w, false);
634 self.nz_y_top.resize(mb_w * 4, false);
635 self.nz_c_top[0].resize(mb_w * 2, false);
636 self.nz_c_top[1].resize(mb_w * 2, false);
638 self.ymodes.resize(mb_w);
640 self.mv_stride = mb_w * 4;
641 self.mvs.resize(self.mv_stride * mb_h * 4, ZERO_MV);
644 pub fn reset(&mut self) {
645 for el in self.top_line_y.iter_mut() { *el = 0x80; }
646 for el in self.top_line_u.iter_mut() { *el = 0x80; }
647 for el in self.top_line_v.iter_mut() { *el = 0x80; }
648 self.left_y = [0x80; 16];
649 self.left_u = [0x80; 16];
650 self.left_v = [0x80; 16];
655 for el in self.nz_y_top.iter_mut() { *el = false; }
656 self.nz_y_left = [false; 4];
657 for el in self.nz_y2_top.iter_mut() { *el = false; }
658 self.nz_y2_left = false;
659 for el in self.nz_c_top[0].iter_mut() { *el = false; }
660 for el in self.nz_c_top[1].iter_mut() { *el = false; }
661 self.nz_c_left = [[false; 2]; 2];
663 self.ymodes.cache.reset();
665 for mv in self.mvs.iter_mut() { *mv = ZERO_MV; }
667 pub fn reset_intra(&mut self) {
668 self.dc_last = [0; 2];
669 self.dc_count = [0; 2];
670 self.dc_last_saved = [0; 2];
671 self.dc_count_saved = [0; 2];
673 pub fn save_dc_pred(&mut self) {
674 self.dc_last_saved = self.dc_last;
675 self.dc_count_saved = self.dc_count;
678 pub fn restore_dc_pred(&mut self) {
679 self.dc_last = self.dc_last_saved;
680 self.dc_count = self.dc_count_saved;
682 pub fn update_mb_row(&mut self) {
683 self.left_y = [0x80; 16];
684 self.left_u = [0x80; 16];
685 self.left_v = [0x80; 16];
689 self.ymodes.cache.update_row();
691 pub fn update_mb(&mut self, sblk: &SrcBlock, mb_x: usize) {
692 for (dst, src) in self.left_y.iter_mut().zip(sblk.luma.chunks_exact(16)) {
695 self.tl_y = self.top_line_y[mb_x * 16 + 16];
696 self.top_line_y[mb_x * 16 + 1..][..16].copy_from_slice(&sblk.luma[15 * 16..]);
698 for (dst, src) in self.left_u.iter_mut().zip(sblk.chroma[0].chunks_exact(8)) {
701 self.tl_u = self.top_line_u[mb_x * 8 + 8];
702 self.top_line_u[mb_x * 8 + 1..][..8].copy_from_slice(&sblk.chroma[0][7 * 8..]);
704 for (dst, src) in self.left_v.iter_mut().zip(sblk.chroma[1].chunks_exact(8)) {
707 self.tl_v = self.top_line_v[mb_x * 8 + 8];
708 self.top_line_v[mb_x * 8 + 1..][..8].copy_from_slice(&sblk.chroma[1][7 * 8..]);
710 pub fn fill_ipred(&mut self, plane: usize, mb_x: usize, ipred: &mut IPredContext) {
714 ipred.top.copy_from_slice(&self.top_line_y[mb_x * 16 + 1..][..16]);
715 ipred.tl = self.tl_y;
717 ipred.left.copy_from_slice(&self.left_y);
718 ipred.has_left = mb_x > 0;
722 ipred.top[..8].copy_from_slice(&self.top_line_u[mb_x * 8 + 1..][..8]);
723 ipred.tl = self.tl_u;
725 ipred.left.copy_from_slice(&self.left_u);
726 ipred.has_left = mb_x > 0;
730 ipred.top[..8].copy_from_slice(&self.top_line_v[mb_x * 8 + 1..][..8]);
731 ipred.tl = self.tl_v;
733 ipred.left.copy_from_slice(&self.left_v);
734 ipred.has_left = mb_x > 0;
738 pub fn get_ipred_tr(&self, mb_x: usize) -> [u8; 4] {
739 if mb_x < self.mb_w - 1 {
741 tr.copy_from_slice(&self.top_line_y[mb_x * 16 + 1 + 16..][..4]);
747 pub fn fill_pctx(&self, mb_x: usize, pctx: &mut BlockPCtx) {
748 pctx.nz_y2 = (self.nz_y2_left as u8) + (self.nz_y2_top[mb_x] as u8);
749 pctx.nz_y_left = self.nz_y_left;
750 pctx.nz_y_top.copy_from_slice(&self.nz_y_top[mb_x * 4..][..4]);
751 pctx.nz_c_left = self.nz_c_left;
752 pctx.nz_c_top = [[self.nz_c_top[0][mb_x * 2], self.nz_c_top[0][mb_x * 2 + 1]],
753 [self.nz_c_top[1][mb_x * 2], self.nz_c_top[1][mb_x * 2 + 1]]];
755 pub fn set_nz(&mut self, mb_x: usize, blk: &Residue) {
757 let has_nz = blk.dcs.has_nz();
758 self.nz_y2_left = has_nz;
759 self.nz_y2_top[mb_x] = has_nz;
761 for (y, blk_row) in blk.luma.chunks(4).enumerate() {
762 for (x, blk) in blk_row.iter().enumerate() {
763 let has_nz = blk.has_nz();
764 self.nz_y_left[y] = has_nz;
765 self.nz_y_top[mb_x * 4 + x] = has_nz;
768 for (c, chroma) in blk.chroma.iter().enumerate() {
769 for (y, blk_row) in chroma.chunks(2).enumerate() {
770 for (x, blk) in blk_row.iter().enumerate() {
771 let has_nz = blk.has_nz();
772 self.nz_c_left[c][y] = has_nz;
773 self.nz_c_top[c][mb_x * 2 + x] = has_nz;
779 pub fn get_y2_dc_pred(&self, last: bool) -> i16 {
780 let ref_id = !last as usize;
781 if self.dc_count[ref_id] > 3 {
787 pub fn predict_y2_dc(&mut self, dc: &mut i16, last: bool) {
788 let ref_id = !last as usize;
789 let pdc = self.dc_last[ref_id];
792 if self.dc_count[ref_id] > 3 {
796 if (pdc == 0) || (orig_dc == 0) || ((pdc ^ orig_dc) < 0) {
797 self.dc_count[ref_id] = 0;
798 } else if pdc == orig_dc {
799 self.dc_count[ref_id] += 1;
801 self.dc_last[ref_id] = orig_dc;
804 pub fn fill_mv(&mut self, mb_x: usize, mb_y: usize, mv: MV) {
805 let mut iidx = mb_x * 4 + mb_y * 4 * self.mv_stride;
808 self.mvs[iidx + x] = mv;
810 iidx += self.mb_w * 4;
813 pub fn find_mv_pred(&self, mb_x: usize, mb_y: usize) -> ([u8; 4], MV, MV, MV) {
814 let mut nearest_mv = ZERO_MV;
815 let mut near_mv = ZERO_MV;
817 let mut ct: [u8; 4] = [0; 4];
819 let start = if self.version == 0 { 1 } else { 0 };
820 let mvwrap = (self.mb_w as isize) + 1;
821 for (yoff, xoff, weight, blk_no) in CAND_POS.iter() {
822 let cx = (mb_x as isize) + (*xoff as isize);
823 let cy = (mb_y as isize) + (*yoff as isize);
824 let mvpos = cx + cy * mvwrap;
825 if (mvpos < start) || ((mvpos % mvwrap) == (mvwrap - 1)) {
829 let cx = (mvpos % mvwrap) as usize;
830 let cy = (mvpos / mvwrap) as usize;
831 let bx = (*blk_no as usize) & 3;
832 let by = (*blk_no as usize) >> 2;
833 let blk_pos = cx * 4 + bx + (cy * 4 + by) * self.mv_stride;
834 let mv = self.mvs[blk_pos];
840 if (nearest_mv == ZERO_MV) || (nearest_mv == mv) {
843 } else if near_mv == ZERO_MV {
847 idx = if mv == near_mv { 2 } else { 3 };
851 let pred_mv = if ct[1] > ct[2] {
852 if ct[1] >= ct[0] { nearest_mv } else { ZERO_MV }
854 if ct[2] >= ct[0] { near_mv } else { ZERO_MV }
857 let mvprobs = [INTER_MODE_PROBS[ct[0] as usize][0],
858 INTER_MODE_PROBS[ct[1] as usize][1],
859 INTER_MODE_PROBS[ct[2] as usize][2],
860 INTER_MODE_PROBS[ct[2] as usize][3]];
862 (mvprobs, nearest_mv, near_mv, pred_mv)