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 {
23 matches!(*self, MBType::Intra(_, _) | MBType::Intra4x4(_, _, _))
25 pub fn get_last(&self) -> bool {
27 MBType::InterNoMV(last, _) |
28 MBType::InterNearest(last, _) |
29 MBType::InterNear(last, _) |
30 MBType::InterMV(last, _, _) |
31 MBType::InterSplitMV(last, _, _, _, _) => last,
37 impl Default for MBType {
38 fn default() -> Self { MBType::Intra(PredMode::DCPred, PredMode::DCPred) }
41 pub fn get_block_difference(dst: &mut [i16; 16], src1: &[u8; 16], src2: &[u8; 16]) {
42 for (dst, (&src1, &src2)) in dst.iter_mut().zip(src1.iter().zip(src2.iter())) {
43 *dst = i16::from(src1) - i16::from(src2);
46 pub fn get_difference_dist(old: &[u8; 16], new: &[u8; 16], diff: &[i16; 16]) -> u32 {
48 for ((&old, &new), &diff) in old.iter().zip(new.iter()).zip(diff.iter()) {
49 let nval = (i16::from(new) + diff).max(0).min(255);
50 let oval = i16::from(old);
51 dist += (i32::from(nval - oval) * i32::from(nval - oval)) as u32;
56 pub fn requant_y2_dc(val: &mut i16, q: usize) {
57 *val = *val / Y2_DC_QUANTS[q] * Y2_DC_QUANTS[q];
61 fn has_nz(&self) -> bool;
64 fn requant_y(&mut self, q: usize);
65 fn quant(&mut self, q: usize, ctype: usize);
66 fn dequant(&mut self, q: usize, ctype: usize);
69 impl DCTBlock for [i16; 16] {
70 fn has_nz(&self) -> bool {
71 for &el in self.iter() {
78 #[allow(clippy::erasing_op)]
79 #[allow(clippy::identity_op)]
81 let mut tmp = [0i16; 16];
83 let s0 = i32::from(self[i + 4 * 0]);
84 let s1 = i32::from(self[i + 4 * 1]);
85 let s2 = i32::from(self[i + 4 * 2]);
86 let s3 = i32::from(self[i + 4 * 3]);
88 let t0 = (s0 + s3).wrapping_mul(23170) + 0x2000;
89 let t1 = (s1 + s2).wrapping_mul(23170);
92 let t4 = t2.wrapping_mul(30274) + t3.wrapping_mul(12540) + 0x2000;
93 let t5 = t2.wrapping_mul(12540) - t3.wrapping_mul(30274) + 0x2000;
95 tmp[i + 0 * 4] = ((t0 + t1) >> 14) as i16;
96 tmp[i + 1 * 4] = ( t4 >> 14) as i16;
97 tmp[i + 2 * 4] = ((t0 - t1) >> 14) as i16;
98 tmp[i + 3 * 4] = ( t5 >> 14) as i16;
100 for (src, dst) in tmp.chunks(4).zip(self.chunks_mut(4)) {
101 let s0 = i32::from(src[0]);
102 let s1 = i32::from(src[1]);
103 let s2 = i32::from(src[2]);
104 let s3 = i32::from(src[3]);
106 let t0 = (s0 + s3).wrapping_mul(23170) + 0x8000;
107 let t1 = (s1 + s2).wrapping_mul(23170);
110 let t4 = t2.wrapping_mul(30274) + t3.wrapping_mul(12540) + 0x8000;
111 let t5 = t2.wrapping_mul(12540) - t3.wrapping_mul(30274) + 0x8000;
113 dst[0] = ((t0 + t1) >> 16) as i16;
114 dst[1] = ( t4 >> 16) as i16;
115 dst[2] = ((t0 - t1) >> 16) as i16;
116 dst[3] = ( t5 >> 16) as i16;
119 fn idct(&mut self) { idct4x4(self) }
120 fn requant_y(&mut self, q: usize) {
121 self[0] = self[0] / Y_DC_QUANTS[q] * Y_DC_QUANTS[q];
122 for el in self[1..].iter_mut() {
123 *el = *el / Y_AC_QUANTS[q] * Y_AC_QUANTS[q];
126 fn quant(&mut self, q: usize, ctype: usize) {
127 let (q_dc, q_ac) = match ctype {
128 0 | 3 => (Y_DC_QUANTS[q], Y_AC_QUANTS[q]),
129 2 => (UV_DC_QUANTS[q], UV_AC_QUANTS[q]),
130 _ => (Y2_DC_QUANTS[q], Y2_AC_QUANTS[q]),
133 for el in self[1..].iter_mut() {
137 fn dequant(&mut self, q: usize, ctype: usize) {
138 let (q_dc, q_ac) = match ctype {
139 0 | 3 => (Y_DC_QUANTS[q], Y_AC_QUANTS[q]),
140 2 => (UV_DC_QUANTS[q], UV_AC_QUANTS[q]),
141 _ => (Y2_DC_QUANTS[q], Y2_AC_QUANTS[q]),
144 for el in self[1..].iter_mut() {
150 pub trait IPredBlock16 {
151 fn ipred16(&mut self, stride: usize, mode: PredMode, ipred: &IPredContext);
153 pub trait IPredBlock8 {
154 fn ipred8 (&mut self, stride: usize, mode: PredMode, ipred: &IPredContext);
156 pub trait IPredBlock4 {
157 fn ipred4 (&mut self, stride: usize, mode: PredMode, ipred: &IPredContext);
160 impl IPredBlock16 for [u8; 256] {
161 fn ipred16(&mut self, stride: usize, mode: PredMode, ipred: &IPredContext) {
163 PredMode::DCPred => IPred16x16::ipred_dc(self, 0, stride, ipred),
164 PredMode::HPred => IPred16x16::ipred_h (self, 0, stride, ipred),
165 PredMode::VPred => IPred16x16::ipred_v (self, 0, stride, ipred),
166 PredMode::TMPred => IPred16x16::ipred_tm(self, 0, stride, ipred),
171 impl IPredBlock8 for [u8; 64] {
172 fn ipred8(&mut self, stride: usize, mode: PredMode, ipred: &IPredContext) {
174 PredMode::DCPred => IPred8x8::ipred_dc(self, 0, stride, ipred),
175 PredMode::HPred => IPred8x8::ipred_h (self, 0, stride, ipred),
176 PredMode::VPred => IPred8x8::ipred_v (self, 0, stride, ipred),
177 PredMode::TMPred => IPred8x8::ipred_tm(self, 0, stride, ipred),
182 impl IPredBlock4 for &mut [u8] {
183 fn ipred4(&mut self, stride: usize, mode: PredMode, ipred: &IPredContext) {
185 PredMode::DCPred => IPred4x4::ipred_dc(self, 0, stride, ipred),
186 PredMode::HPred => IPred4x4::ipred_he(self, 0, stride, ipred),
187 PredMode::VPred => IPred4x4::ipred_ve(self, 0, stride, ipred),
188 PredMode::TMPred => IPred4x4::ipred_tm(self, 0, stride, ipred),
189 PredMode::LDPred => IPred4x4::ipred_ld(self, 0, stride, ipred),
190 PredMode::RDPred => IPred4x4::ipred_rd(self, 0, stride, ipred),
191 PredMode::VRPred => IPred4x4::ipred_vr(self, 0, stride, ipred),
192 PredMode::VLPred => IPred4x4::ipred_vl(self, 0, stride, ipred),
193 PredMode::HDPred => IPred4x4::ipred_hd(self, 0, stride, ipred),
194 PredMode::HUPred => IPred4x4::ipred_hu(self, 0, stride, ipred),
199 impl IPredBlock4 for [u8; 16] {
200 fn ipred4(&mut self, stride: usize, mode: PredMode, ipred: &IPredContext) {
201 (self as &mut [u8]).ipred4(stride, mode, ipred);
205 pub struct LumaIterator<'a> {
210 impl<'a> Iterator for LumaIterator<'a> {
211 type Item = [u8; 16];
212 fn next(&mut self) -> Option<Self::Item> {
214 let mut blk = [0; 16];
215 let off = (self.blkno & 3) * 4 + (self.blkno >> 2) * 16 * 4;
216 for (dst, src) in blk.chunks_exact_mut(4).zip(self.luma[off..].chunks(16)) {
217 dst.copy_from_slice(&src[..4]);
227 pub struct ChromaIterator<'a> {
228 chroma: &'a [u8; 64],
232 impl<'a> Iterator for ChromaIterator<'a> {
233 type Item = [u8; 16];
234 fn next(&mut self) -> Option<Self::Item> {
236 let mut blk = [0; 16];
237 let off = (self.blkno & 1) * 4 + (self.blkno >> 1) * 8 * 4;
238 for (dst, src) in blk.chunks_exact_mut(4).zip(self.chroma[off..].chunks(8)) {
239 dst.copy_from_slice(&src[..4]);
249 pub struct SrcBlock {
251 pub chroma: [[u8; 64]; 2],
254 impl Default for SrcBlock {
255 fn default() -> Self {
256 unsafe { std::mem::zeroed() }
261 pub fn new() -> Self { Self::default() }
262 pub fn is_flat(&self) -> bool {
263 let y0 = self.luma[0];
264 for &el in self.luma[1..].iter() {
271 pub fn apply_ipred_luma(&self, mode: PredMode, ipred: &IPredContext, dst: &mut Residue) {
272 let mut tmp = [0; 256];
273 tmp.ipred16(16, mode, ipred);
274 dst.set_luma_from_diff(&self.luma, &tmp);
276 pub fn fill_ipred_luma(&mut self, mode: PredMode, ipred: &IPredContext) {
277 self.luma.ipred16(16, mode, ipred);
279 pub fn apply_ipred_chroma(&self, mode: PredMode, ipred_u: &IPredContext, ipred_v: &IPredContext, dst: &mut Residue) {
280 let mut tmp = [[0u8; 64]; 2];
281 tmp[0].ipred8(8, mode, ipred_u);
282 tmp[1].ipred8(8, mode, ipred_v);
283 dst.set_chroma_from_diff(&self.chroma, &tmp);
285 pub fn fill_ipred_chroma(&mut self, mode: PredMode, ipred_u: &IPredContext, ipred_v: &IPredContext) {
286 self.chroma[0].ipred8(8, mode, ipred_u);
287 self.chroma[1].ipred8(8, mode, ipred_v);
290 pub fn luma_blocks(&self) -> LumaIterator {
291 LumaIterator{ luma: &self.luma, blkno: 0 }
293 pub fn chroma_blocks(&self, plane: usize) -> ChromaIterator {
294 ChromaIterator{ chroma: &self.chroma[plane], blkno: 0 }
300 pub luma: [[i16; 16]; 16],
302 pub chroma: [[[i16; 16]; 4]; 2],
307 impl Default for Residue {
308 fn default() -> Self {
309 unsafe { std::mem::zeroed() }
314 pub fn new() -> Self { Self::default() }
315 pub fn reset(&mut self) {
319 pub fn add_residue(&mut self, dst: &mut SrcBlock) {
323 for (dst, src) in dst.luma.chunks_mut(16 * 4).zip(self.luma.chunks(4)) {
324 for (x, blk) in src.iter().enumerate() {
325 for (drow, srow) in dst[x * 4..].chunks_mut(16).zip(blk.chunks(4)) {
326 for (del, &sel) in drow.iter_mut().zip(srow.iter()) {
327 *del = (i16::from(*del) + sel).max(0).min(255) as u8;
332 for (dchroma, schroma) in dst.chroma.iter_mut().zip(self.chroma.iter()) {
333 for (dst, src) in dchroma.chunks_mut(8 * 4).zip(schroma.chunks(2)) {
334 for (x, blk) in src.iter().enumerate() {
335 for (drow, srow) in dst[x * 4..].chunks_mut(8).zip(blk.chunks(4)) {
336 for (del, &sel) in drow.iter_mut().zip(srow.iter()) {
337 *del = (i16::from(*del) + sel).max(0).min(255) as u8;
344 pub fn add_residue_chroma(&mut self, dst: &mut SrcBlock) {
345 let q = self.q as usize;
346 for (dchroma, schroma) in dst.chroma.iter_mut().zip(self.chroma.iter_mut()) {
347 for (dst, src) in dchroma.chunks_mut(8 * 4).zip(schroma.chunks_mut(2)) {
348 for (x, blk) in src.iter_mut().enumerate() {
349 blk[0] *= UV_DC_QUANTS[q];
350 for el in blk[1..].iter_mut() {
352 *el *= UV_AC_QUANTS[q];
356 for (drow, srow) in dst[x * 4..].chunks_mut(8).zip(blk.chunks(4)) {
357 for (del, &sel) in drow.iter_mut().zip(srow.iter()) {
358 *del = (i16::from(*del) + sel).max(0).min(255) as u8;
365 pub fn set_luma_from_diff(&mut self, blk1: &[u8; 256], blk2: &[u8; 256]) {
366 for (dst, (src1, src2)) in self.luma.chunks_mut(4).zip(blk1.chunks(16 * 4).zip(blk2.chunks(16 * 4))) {
367 for (x, blk) in dst.iter_mut().enumerate() {
368 for (dst, (row1, row2)) in blk.chunks_mut(4).zip(src1[x * 4..].chunks(16).zip(src2[x * 4..].chunks(16))) {
369 for (dst, (&a, &b)) in dst.iter_mut().zip(row1.iter().zip(row2.iter())) {
370 *dst = i16::from(a) - i16::from(b);
376 pub fn set_chroma_from_diff(&mut self, blk1: &[[u8; 64]; 2], blk2: &[[u8; 64]; 2]) {
377 for (chroma, (src1, src2)) in self.chroma.iter_mut().zip(blk1.iter().zip(blk2.iter())) {
378 for (dst, (src1, src2)) in chroma.chunks_mut(2).zip(src1.chunks(8 * 4).zip(src2.chunks(8 * 4))) {
379 for (x, blk) in dst.iter_mut().enumerate() {
380 for (dst, (row1, row2)) in blk.chunks_mut(4).zip(src1[x * 4..].chunks(8).zip(src2[x * 4..].chunks(8))) {
381 for (dst, (&a, &b)) in dst.iter_mut().zip(row1.iter().zip(row2.iter())) {
382 *dst = i16::from(a) - i16::from(b);
389 pub fn fdct(&mut self) {
393 pub fn fdct_luma(&mut self) {
394 for blk in self.luma.iter_mut() {
398 pub fn fdct_chroma(&mut self) {
399 for chroma in self.chroma.iter_mut() {
400 for blk in chroma.iter_mut() {
405 pub fn fdct_dc_block(&mut self) {
406 for (dc, blk) in self.dcs.iter_mut().zip(self.luma.iter_mut()) {
413 pub fn idct(&mut self) {
417 pub fn idct_luma(&mut self) {
420 for (&dc, blk) in self.dcs.iter().zip(self.luma.iter_mut()) {
424 for blk in self.luma.iter_mut() {
428 pub fn idct_chroma(&mut self) {
429 for chroma in self.chroma.iter_mut() {
430 for blk in chroma.iter_mut() {
435 pub fn quant(&mut self, q: usize) {
437 self.quant_chroma(q);
440 pub fn quant_luma(&mut self, q: usize) {
442 self.dcs[0] /= Y2_DC_QUANTS[q];
443 for el in self.dcs[1..].iter_mut() {
445 *el /= Y2_AC_QUANTS[q];
449 for blk in self.luma.iter_mut() {
450 blk[0] /= Y_DC_QUANTS[q];
451 for el in blk[1..].iter_mut() {
453 *el /= Y_AC_QUANTS[q];
459 pub fn quant_chroma(&mut self, q: usize) {
460 for chroma in self.chroma.iter_mut() {
461 for blk in chroma.iter_mut() {
462 blk[0] /= UV_DC_QUANTS[q];
463 for el in blk[1..].iter_mut() {
465 *el /= UV_AC_QUANTS[q];
472 pub fn dequant(&mut self) {
474 self.dequant_chroma();
476 pub fn dequant_luma(&mut self) {
477 let q = self.q as usize;
479 self.dcs[0] *= Y2_DC_QUANTS[q];
480 for el in self.dcs[1..].iter_mut() {
482 *el *= Y2_AC_QUANTS[q];
486 for blk in self.luma.iter_mut() {
487 blk[0] *= Y_DC_QUANTS[q];
488 for el in blk[1..].iter_mut() {
490 *el *= Y_AC_QUANTS[q];
495 pub fn dequant_chroma(&mut self) {
496 let q = self.q as usize;
497 for chroma in self.chroma.iter_mut() {
498 for blk in chroma.iter_mut() {
499 blk[0] *= UV_DC_QUANTS[q];
500 for el in blk[1..].iter_mut() {
502 *el *= UV_AC_QUANTS[q];
510 pub fn load_blocks(src: &NAVideoBuffer<u8>, sblocks: &mut Vec<SrcBlock>) {
511 let data = src.get_data();
512 let y = &data[src.get_offset(0)..];
513 let u = &data[src.get_offset(1)..];
514 let v = &data[src.get_offset(2)..];
515 let ystride = src.get_stride(0);
516 let ustride = src.get_stride(1);
517 let vstride = src.get_stride(2);
518 let (width, height) = src.get_dimensions(0);
521 for (ystrip, (ustrip, vstrip)) in y.chunks(ystride * 16).take((height + 15) / 16).zip(u.chunks(ustride * 8).zip(v.chunks(vstride * 8))) {
522 for x in (0..width).step_by(16) {
523 let mut sblk = SrcBlock::default();
525 for (dst, src) in sblk.luma.chunks_mut(16).zip(ystrip[x..].chunks(ystride)) {
526 dst.copy_from_slice(&src[..16]);
528 for (dst, src) in sblk.chroma[0].chunks_mut(8).zip(ustrip[x / 2..].chunks(ustride)) {
529 dst.copy_from_slice(&src[..8]);
531 for (dst, src) in sblk.chroma[1].chunks_mut(8).zip(vstrip[x / 2..].chunks(vstride)) {
532 dst.copy_from_slice(&src[..8]);
539 pub struct YModePred {
540 pub cache: GenericCache<PredMode>,
544 fn resize(&mut self, mb_w: usize) {
545 self.cache = GenericCache::new(4, mb_w * 4 + 1, PredMode::DCPred);
547 pub fn set_mode(&mut self, mb_x: usize, mode: PredMode) {
548 for row in self.cache.data[self.cache.xpos + mb_x * 4..].chunks_mut(self.cache.stride).take(4) {
549 for el in row[..4].iter_mut() {
550 *el = mode.to_b_mode();
554 pub fn set_modes4x4(&mut self, mb_x: usize, imodes: &[PredMode; 16], ctx: &mut [u8; 16]) {
555 let mut off = self.cache.xpos + mb_x * 4;
558 let top_idx = self.cache.data[off + x - self.cache.stride].to_b_index();
559 let left_idx = self.cache.data[off + x - 1].to_b_index();
560 self.cache.data[off + x] = imodes[x + y * 4];
561 ctx[x + y * 4] = ((top_idx * 10) + left_idx) as u8;
563 off += self.cache.stride;
568 impl Default for YModePred {
569 fn default() -> Self {
571 cache: GenericCache::new(0, 0, PredMode::DCPred)
577 pub struct BlockPCtx {
579 pub nz_y_top: [bool; 4],
580 pub nz_y_left: [bool; 4],
581 pub nz_c_top: [[bool; 2]; 2],
582 pub nz_c_left: [[bool; 2]; 2],
586 pub struct PredContext {
590 pub top_line_y: Vec<u8>,
591 pub top_line_u: Vec<u8>,
592 pub top_line_v: Vec<u8>,
597 pub left_y: [u8; 16],
598 pub left_u: [u8; 16],
599 pub left_v: [u8; 16],
601 pub dc_last: [i16; 2],
602 pub dc_count: [usize; 2],
603 dc_last_saved: [i16; 2],
604 dc_count_saved: [usize; 2],
605 pub nz_y2_top: Vec<bool>,
606 pub nz_y2_left: bool,
607 pub nz_y_top: Vec<bool>,
608 pub nz_y_left: [bool; 4],
609 pub nz_c_top: [Vec<bool>; 2],
610 pub nz_c_left: [[bool; 2]; 2],
612 pub ymodes: YModePred,
615 pub mv_stride: usize,
620 pub fn new() -> Self { Self::default() }
621 pub fn resize(&mut self, mb_w: usize, mb_h: usize) {
625 self.top_line_y.resize(mb_w * 16 + 1, 0);
626 self.top_line_u.resize(mb_w * 8 + 1, 0);
627 self.top_line_v.resize(mb_w * 8 + 1, 0);
629 self.nz_y2_top.resize(mb_w, false);
630 self.nz_y_top.resize(mb_w * 4, false);
631 self.nz_c_top[0].resize(mb_w * 2, false);
632 self.nz_c_top[1].resize(mb_w * 2, false);
634 self.ymodes.resize(mb_w);
636 self.mv_stride = mb_w * 4;
637 self.mvs.resize(self.mv_stride * mb_h * 4, ZERO_MV);
640 pub fn reset(&mut self) {
641 for el in self.top_line_y.iter_mut() { *el = 0x80; }
642 for el in self.top_line_u.iter_mut() { *el = 0x80; }
643 for el in self.top_line_v.iter_mut() { *el = 0x80; }
644 self.left_y = [0x80; 16];
645 self.left_u = [0x80; 16];
646 self.left_v = [0x80; 16];
651 for el in self.nz_y_top.iter_mut() { *el = false; }
652 self.nz_y_left = [false; 4];
653 for el in self.nz_y2_top.iter_mut() { *el = false; }
654 self.nz_y2_left = false;
655 for el in self.nz_c_top[0].iter_mut() { *el = false; }
656 for el in self.nz_c_top[1].iter_mut() { *el = false; }
657 self.nz_c_left = [[false; 2]; 2];
659 self.ymodes.cache.reset();
661 for mv in self.mvs.iter_mut() { *mv = ZERO_MV; }
663 pub fn reset_intra(&mut self) {
664 self.dc_last = [0; 2];
665 self.dc_count = [0; 2];
666 self.dc_last_saved = [0; 2];
667 self.dc_count_saved = [0; 2];
669 pub fn save_dc_pred(&mut self) {
670 self.dc_last_saved = self.dc_last;
671 self.dc_count_saved = self.dc_count;
674 pub fn restore_dc_pred(&mut self) {
675 self.dc_last = self.dc_last_saved;
676 self.dc_count = self.dc_count_saved;
678 pub fn update_mb_row(&mut self) {
679 self.left_y = [0x80; 16];
680 self.left_u = [0x80; 16];
681 self.left_v = [0x80; 16];
685 self.ymodes.cache.update_row();
687 pub fn update_mb(&mut self, sblk: &SrcBlock, mb_x: usize) {
688 for (dst, src) in self.left_y.iter_mut().zip(sblk.luma.chunks_exact(16)) {
691 self.tl_y = self.top_line_y[mb_x * 16 + 16];
692 self.top_line_y[mb_x * 16 + 1..][..16].copy_from_slice(&sblk.luma[15 * 16..]);
694 for (dst, src) in self.left_u.iter_mut().zip(sblk.chroma[0].chunks_exact(8)) {
697 self.tl_u = self.top_line_u[mb_x * 8 + 8];
698 self.top_line_u[mb_x * 8 + 1..][..8].copy_from_slice(&sblk.chroma[0][7 * 8..]);
700 for (dst, src) in self.left_v.iter_mut().zip(sblk.chroma[1].chunks_exact(8)) {
703 self.tl_v = self.top_line_v[mb_x * 8 + 8];
704 self.top_line_v[mb_x * 8 + 1..][..8].copy_from_slice(&sblk.chroma[1][7 * 8..]);
706 pub fn fill_ipred(&mut self, plane: usize, mb_x: usize, ipred: &mut IPredContext) {
710 ipred.top.copy_from_slice(&self.top_line_y[mb_x * 16 + 1..][..16]);
711 ipred.tl = self.tl_y;
713 ipred.left.copy_from_slice(&self.left_y);
714 ipred.has_left = mb_x > 0;
718 ipred.top[..8].copy_from_slice(&self.top_line_u[mb_x * 8 + 1..][..8]);
719 ipred.tl = self.tl_u;
721 ipred.left.copy_from_slice(&self.left_u);
722 ipred.has_left = mb_x > 0;
726 ipred.top[..8].copy_from_slice(&self.top_line_v[mb_x * 8 + 1..][..8]);
727 ipred.tl = self.tl_v;
729 ipred.left.copy_from_slice(&self.left_v);
730 ipred.has_left = mb_x > 0;
734 pub fn get_ipred_tr(&self, mb_x: usize) -> [u8; 4] {
735 if mb_x < self.mb_w - 1 {
737 tr.copy_from_slice(&self.top_line_y[mb_x * 16 + 1 + 16..][..4]);
743 pub fn fill_pctx(&self, mb_x: usize, pctx: &mut BlockPCtx) {
744 pctx.nz_y2 = (self.nz_y2_left as u8) + (self.nz_y2_top[mb_x] as u8);
745 pctx.nz_y_left = self.nz_y_left;
746 pctx.nz_y_top.copy_from_slice(&self.nz_y_top[mb_x * 4..][..4]);
747 pctx.nz_c_left = self.nz_c_left;
748 pctx.nz_c_top = [[self.nz_c_top[0][mb_x * 2], self.nz_c_top[0][mb_x * 2 + 1]],
749 [self.nz_c_top[1][mb_x * 2], self.nz_c_top[1][mb_x * 2 + 1]]];
751 pub fn set_nz(&mut self, mb_x: usize, blk: &Residue) {
753 let has_nz = blk.dcs.has_nz();
754 self.nz_y2_left = has_nz;
755 self.nz_y2_top[mb_x] = has_nz;
757 for (y, blk_row) in blk.luma.chunks(4).enumerate() {
758 for (x, blk) in blk_row.iter().enumerate() {
759 let has_nz = blk.has_nz();
760 self.nz_y_left[y] = has_nz;
761 self.nz_y_top[mb_x * 4 + x] = has_nz;
764 for (c, chroma) in blk.chroma.iter().enumerate() {
765 for (y, blk_row) in chroma.chunks(2).enumerate() {
766 for (x, blk) in blk_row.iter().enumerate() {
767 let has_nz = blk.has_nz();
768 self.nz_c_left[c][y] = has_nz;
769 self.nz_c_top[c][mb_x * 2 + x] = has_nz;
775 pub fn get_y2_dc_pred(&self, last: bool) -> i16 {
776 let ref_id = !last as usize;
777 if self.dc_count[ref_id] > 3 {
783 pub fn predict_y2_dc(&mut self, dc: &mut i16, last: bool) {
784 let ref_id = !last as usize;
785 let pdc = self.dc_last[ref_id];
788 if self.dc_count[ref_id] > 3 {
792 if (pdc == 0) || (orig_dc == 0) || ((pdc ^ orig_dc) < 0) {
793 self.dc_count[ref_id] = 0;
794 } else if pdc == orig_dc {
795 self.dc_count[ref_id] += 1;
797 self.dc_last[ref_id] = orig_dc;
800 pub fn fill_mv(&mut self, mb_x: usize, mb_y: usize, mv: MV) {
801 let mut iidx = mb_x * 4 + mb_y * 4 * self.mv_stride;
804 self.mvs[iidx + x] = mv;
806 iidx += self.mb_w * 4;
809 pub fn find_mv_pred(&self, mb_x: usize, mb_y: usize) -> ([u8; 4], MV, MV, MV) {
810 let mut nearest_mv = ZERO_MV;
811 let mut near_mv = ZERO_MV;
813 let mut ct: [u8; 4] = [0; 4];
815 let start = if self.version == 0 { 1 } else { 0 };
816 let mvwrap = (self.mb_w as isize) + 1;
817 for (yoff, xoff, weight, blk_no) in CAND_POS.iter() {
818 let cx = (mb_x as isize) + (*xoff as isize);
819 let cy = (mb_y as isize) + (*yoff as isize);
820 let mvpos = cx + cy * mvwrap;
821 if (mvpos < start) || ((mvpos % mvwrap) == (mvwrap - 1)) {
825 let cx = (mvpos % mvwrap) as usize;
826 let cy = (mvpos / mvwrap) as usize;
827 let bx = (*blk_no as usize) & 3;
828 let by = (*blk_no as usize) >> 2;
829 let blk_pos = cx * 4 + bx + (cy * 4 + by) * self.mv_stride;
830 let mv = self.mvs[blk_pos];
836 if (nearest_mv == ZERO_MV) || (nearest_mv == mv) {
839 } else if near_mv == ZERO_MV {
843 idx = if mv == near_mv { 2 } else { 3 };
847 let pred_mv = if ct[1] > ct[2] {
848 if ct[1] >= ct[0] { nearest_mv } else { ZERO_MV }
850 if ct[2] >= ct[0] { near_mv } else { ZERO_MV }
853 let mvprobs = [INTER_MODE_PROBS[ct[0] as usize][0],
854 INTER_MODE_PROBS[ct[1] as usize][1],
855 INTER_MODE_PROBS[ct[2] as usize][2],
856 INTER_MODE_PROBS[ct[2] as usize][3]];
858 (mvprobs, nearest_mv, near_mv, pred_mv)