aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-duck/src/codecs/vp7enc/blocks.rs
blob: 227638264cdd8676389664abae7f907a4ea5dd28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
use nihav_core::frame::*;
use nihav_codec_support::codecs::{MV, ZERO_MV};
use nihav_codec_support::data::GenericCache;
use super::super::vp78::{PredMode, MVSplitMode, SubMVRef};
use super::super::vp78data::*;
use super::super::vp78dsp::*;
use super::super::vp7data::*;
use super::super::vp7dsp::*;

#[derive(Clone,Copy)]
pub enum MBType {
    Intra(PredMode, PredMode),
    Intra4x4([PredMode; 16], [u8; 16], PredMode),
    InterNoMV(bool, [u8; 4]),
    InterNearest(bool, [u8; 4]),
    InterNear(bool, [u8; 4]),
    InterMV(bool, [u8; 4], MV),
    InterSplitMV(bool, [u8; 4], MVSplitMode, [SubMVRef; 16], [MV; 16]),
}

impl MBType {
    pub fn is_intra(&self) -> bool {
        matches!(*self, MBType::Intra(_, _) | MBType::Intra4x4(_, _, _))
    }
    pub fn get_last(&self) -> bool {
        match *self {
            MBType::InterNoMV(last, _) |
            MBType::InterNearest(last, _) |
            MBType::InterNear(last, _) |
            MBType::InterMV(last, _, _) |
            MBType::InterSplitMV(last, _, _, _, _) => last,
            _ => false,
        }
    }
}

impl Default for MBType {
    fn default() -> Self { MBType::Intra(PredMode::DCPred, PredMode::DCPred) }
}

pub fn get_block_difference(dst: &mut [i16; 16], src1: &[u8; 16], src2: &[u8; 16]) {
    for (dst, (&src1, &src2)) in dst.iter_mut().zip(src1.iter().zip(src2.iter())) {
        *dst = i16::from(src1) - i16::from(src2);
    }
}
pub fn get_difference_dist(old: &[u8; 16], new: &[u8; 16], diff: &[i16; 16]) -> u32 {
    let mut dist = 0;
    for ((&old, &new), &diff) in old.iter().zip(new.iter()).zip(diff.iter()) {
        let nval = (i16::from(new) + diff).max(0).min(255);
        let oval = i16::from(old);
        dist += (i32::from(nval - oval) * i32::from(nval - oval)) as u32;
    }
    dist
}

pub fn requant_y2_dc(val: &mut i16, q: usize) {
    *val = *val / Y2_DC_QUANTS[q] * Y2_DC_QUANTS[q];
}

pub trait DCTBlock {
    fn has_nz(&self) -> bool;
    fn fdct(&mut self);
    fn idct(&mut self);
    fn requant_y(&mut self, q: usize);
    fn quant(&mut self, q: usize, ctype: usize);
    fn dequant(&mut self, q: usize, ctype: usize);
}

impl DCTBlock for [i16; 16] {
    fn has_nz(&self) -> bool {
        for &el in self.iter() {
            if el != 0 {
                return true;
            }
        }
        false
    }
    #[allow(clippy::erasing_op)]
    #[allow(clippy::identity_op)]
    fn fdct(&mut self) {
        let mut tmp = [0i16; 16];
        for i in 0..4 {
            let s0 = i32::from(self[i + 4 * 0]);
            let s1 = i32::from(self[i + 4 * 1]);
            let s2 = i32::from(self[i + 4 * 2]);
            let s3 = i32::from(self[i + 4 * 3]);

            let t0 = (s0 + s3).wrapping_mul(23170) + 0x2000;
            let t1 = (s1 + s2).wrapping_mul(23170);
            let t2 = s0 - s3;
            let t3 = s1 - s2;
            let t4 = t2.wrapping_mul(30274) + t3.wrapping_mul(12540) + 0x2000;
            let t5 = t2.wrapping_mul(12540) - t3.wrapping_mul(30274) + 0x2000;

            tmp[i + 0 * 4] = ((t0 + t1) >> 14) as i16;
            tmp[i + 1 * 4] = ( t4       >> 14) as i16;
            tmp[i + 2 * 4] = ((t0 - t1) >> 14) as i16;
            tmp[i + 3 * 4] = ( t5       >> 14) as i16;
        }
        for (src, dst) in tmp.chunks(4).zip(self.chunks_mut(4)) {
            let s0 = i32::from(src[0]);
            let s1 = i32::from(src[1]);
            let s2 = i32::from(src[2]);
            let s3 = i32::from(src[3]);

            let t0 = (s0 + s3).wrapping_mul(23170) + 0x8000;
            let t1 = (s1 + s2).wrapping_mul(23170);
            let t2 = s0 - s3;
            let t3 = s1 - s2;
            let t4 = t2.wrapping_mul(30274) + t3.wrapping_mul(12540) + 0x8000;
            let t5 = t2.wrapping_mul(12540) - t3.wrapping_mul(30274) + 0x8000;

            dst[0] = ((t0 + t1) >> 16) as i16;
            dst[1] = ( t4       >> 16) as i16;
            dst[2] = ((t0 - t1) >> 16) as i16;
            dst[3] = ( t5       >> 16) as i16;
        }
    }
    fn idct(&mut self) { idct4x4(self) }
    fn requant_y(&mut self, q: usize) {
        self[0] = self[0] / Y_DC_QUANTS[q] * Y_DC_QUANTS[q];
        for el in self[1..].iter_mut() {
            *el = *el / Y_AC_QUANTS[q] * Y_AC_QUANTS[q];
        }
    }
    fn quant(&mut self, q: usize, ctype: usize) {
        let (q_dc, q_ac) = match ctype {
                0 | 3 => (Y_DC_QUANTS[q],  Y_AC_QUANTS[q]),
                2     => (UV_DC_QUANTS[q], UV_AC_QUANTS[q]),
                _     => (Y2_DC_QUANTS[q], Y2_AC_QUANTS[q]),
            };
        self[0] /= q_dc;
        for el in self[1..].iter_mut() {
            *el /= q_ac;
        }
    }
    fn dequant(&mut self, q: usize, ctype: usize) {
        let (q_dc, q_ac) = match ctype {
                0 | 3 => (Y_DC_QUANTS[q],  Y_AC_QUANTS[q]),
                2     => (UV_DC_QUANTS[q], UV_AC_QUANTS[q]),
                _     => (Y2_DC_QUANTS[q], Y2_AC_QUANTS[q]),
            };
        self[0] *= q_dc;
        for el in self[1..].iter_mut() {
            *el *= q_ac;
        }
    }
}

pub trait IPredBlock16 {
    fn ipred16(&mut self, stride: usize, mode: PredMode, ipred: &IPredContext);
}
pub trait IPredBlock8 {
    fn ipred8 (&mut self, stride: usize, mode: PredMode, ipred: &IPredContext);
}
pub trait IPredBlock4 {
    fn ipred4 (&mut self, stride: usize, mode: PredMode, ipred: &IPredContext);
}

impl IPredBlock16 for [u8; 256] {
    fn ipred16(&mut self, stride: usize, mode: PredMode, ipred: &IPredContext) {
        match mode {
            PredMode::DCPred => IPred16x16::ipred_dc(self, 0, stride, ipred),
            PredMode::HPred  => IPred16x16::ipred_h (self, 0, stride, ipred),
            PredMode::VPred  => IPred16x16::ipred_v (self, 0, stride, ipred),
            PredMode::TMPred => IPred16x16::ipred_tm(self, 0, stride, ipred),
            _ => {},
        }
    }
}
impl IPredBlock8 for [u8; 64] {
    fn ipred8(&mut self, stride: usize, mode: PredMode, ipred: &IPredContext) {
        match mode {
            PredMode::DCPred => IPred8x8::ipred_dc(self, 0, stride, ipred),
            PredMode::HPred  => IPred8x8::ipred_h (self, 0, stride, ipred),
            PredMode::VPred  => IPred8x8::ipred_v (self, 0, stride, ipred),
            PredMode::TMPred => IPred8x8::ipred_tm(self, 0, stride, ipred),
            _ => {},
        }
    }
}
impl IPredBlock4 for &mut [u8] {
    fn ipred4(&mut self, stride: usize, mode: PredMode, ipred: &IPredContext) {
        match mode {
            PredMode::DCPred => IPred4x4::ipred_dc(self, 0, stride, ipred),
            PredMode::HPred  => IPred4x4::ipred_he(self, 0, stride, ipred),
            PredMode::VPred  => IPred4x4::ipred_ve(self, 0, stride, ipred),
            PredMode::TMPred => IPred4x4::ipred_tm(self, 0, stride, ipred),
            PredMode::LDPred => IPred4x4::ipred_ld(self, 0, stride, ipred),
            PredMode::RDPred => IPred4x4::ipred_rd(self, 0, stride, ipred),
            PredMode::VRPred => IPred4x4::ipred_vr(self, 0, stride, ipred),
            PredMode::VLPred => IPred4x4::ipred_vl(self, 0, stride, ipred),
            PredMode::HDPred => IPred4x4::ipred_hd(self, 0, stride, ipred),
            PredMode::HUPred => IPred4x4::ipred_hu(self, 0, stride, ipred),
            _ => {},
        }
    }
}
impl IPredBlock4 for [u8; 16] {
    fn ipred4(&mut self, stride: usize, mode: PredMode, ipred: &IPredContext) {
        (self as &mut [u8]).ipred4(stride, mode, ipred);
    }
}

pub struct LumaIterator<'a> {
    luma:   &'a [u8; 256],
    blkno:  usize,
}

impl<'a> Iterator for LumaIterator<'a> {
    type Item = [u8; 16];
    fn next(&mut self) -> Option<Self::Item> {
        if self.blkno < 16 {
            let mut blk = [0; 16];
            let off = (self.blkno & 3) * 4 + (self.blkno >> 2) * 16 * 4;
            for (dst, src) in blk.chunks_exact_mut(4).zip(self.luma[off..].chunks(16)) {
                dst.copy_from_slice(&src[..4]);
            }
            self.blkno += 1;
            Some(blk)
        } else {
            None
        }
    }
}

pub struct ChromaIterator<'a> {
    chroma: &'a [u8; 64],
    blkno:  usize,
}

impl<'a> Iterator for ChromaIterator<'a> {
    type Item = [u8; 16];
    fn next(&mut self) -> Option<Self::Item> {
        if self.blkno < 4 {
            let mut blk = [0; 16];
            let off = (self.blkno & 1) * 4 + (self.blkno >> 1) * 8 * 4;
            for (dst, src) in blk.chunks_exact_mut(4).zip(self.chroma[off..].chunks(8)) {
                dst.copy_from_slice(&src[..4]);
            }
            self.blkno += 1;
            Some(blk)
        } else {
            None
        }
    }
}

pub struct SrcBlock {
    pub luma:   [u8; 256],
    pub chroma: [[u8; 64]; 2],
}

impl Default for SrcBlock {
    fn default() -> Self {
        unsafe { std::mem::zeroed() }
    }
}

impl SrcBlock {
    pub fn new() -> Self { Self::default() }
    pub fn is_flat(&self) -> bool {
        let y0 = self.luma[0];
        for &el in self.luma[1..].iter() {
            if el != y0 {
                return false;
            }
        }
        true
    }
    pub fn apply_ipred_luma(&self, mode: PredMode, ipred: &IPredContext, dst: &mut Residue) {
        let mut tmp = [0; 256];
        (&mut tmp).ipred16(16, mode, ipred);
        dst.set_luma_from_diff(&self.luma, &tmp);
    }
    pub fn fill_ipred_luma(&mut self, mode: PredMode, ipred: &IPredContext) {
        self.luma.ipred16(16, mode, ipred);
    }
    pub fn apply_ipred_chroma(&self, mode: PredMode, ipred_u: &IPredContext, ipred_v: &IPredContext, dst: &mut Residue) {
        let mut tmp = [[0u8; 64]; 2];
        tmp[0].ipred8(8, mode, ipred_u);
        tmp[1].ipred8(8, mode, ipred_v);
        dst.set_chroma_from_diff(&self.chroma, &tmp);
    }
    pub fn fill_ipred_chroma(&mut self, mode: PredMode, ipred_u: &IPredContext, ipred_v: &IPredContext) {
        self.chroma[0].ipred8(8, mode, ipred_u);
        self.chroma[1].ipred8(8, mode, ipred_v);
    }

    pub fn luma_blocks(&self) -> LumaIterator {
        LumaIterator{ luma: &self.luma, blkno: 0 }
    }
    pub fn chroma_blocks(&self, plane: usize) -> ChromaIterator {
        ChromaIterator{ chroma: &self.chroma[plane], blkno: 0 }
    }
}

#[derive(Clone)]
pub struct Residue {
    pub luma:   [[i16; 16]; 16],
    pub dcs:    [i16; 16],
    pub chroma: [[[i16; 16]; 4]; 2],
    pub has_dc: bool,
    pub q:      u8,
}

impl Default for Residue {
    fn default() -> Self {
        unsafe { std::mem::zeroed() }
    }
}

impl Residue {
    pub fn new() -> Self { Self::default() }
    pub fn reset(&mut self) {
        self.has_dc = false;
        self.q = 242;
    }
    pub fn add_residue(&mut self, dst: &mut SrcBlock) {
        self.dequant();
        self.idct();

        for (dst, src) in dst.luma.chunks_mut(16 * 4).zip(self.luma.chunks(4)) {
            for (x, blk) in src.iter().enumerate() {
                for (drow, srow) in dst[x * 4..].chunks_mut(16).zip(blk.chunks(4)) {
                    for (del, &sel) in drow.iter_mut().zip(srow.iter()) {
                        *del = (i16::from(*del) + sel).max(0).min(255) as u8;
                    }
                }
            }
        }
        for (dchroma, schroma) in dst.chroma.iter_mut().zip(self.chroma.iter()) {
            for (dst, src) in dchroma.chunks_mut(8 * 4).zip(schroma.chunks(2)) {
                for (x, blk) in src.iter().enumerate() {
                    for (drow, srow) in dst[x * 4..].chunks_mut(8).zip(blk.chunks(4)) {
                        for (del, &sel) in drow.iter_mut().zip(srow.iter()) {
                            *del = (i16::from(*del) + sel).max(0).min(255) as u8;
                        }
                    }
                }
            }
        }
    }
    pub fn add_residue_chroma(&mut self, dst: &mut SrcBlock) {
        let q = self.q as usize;
        for (dchroma, schroma) in dst.chroma.iter_mut().zip(self.chroma.iter_mut()) {
            for (dst, src) in dchroma.chunks_mut(8 * 4).zip(schroma.chunks_mut(2)) {
                for (x, blk) in src.iter_mut().enumerate() {
                    blk[0] *= UV_DC_QUANTS[q];
                    for el in blk[1..].iter_mut() {
                        if *el != 0 {
                            *el *= UV_AC_QUANTS[q];
                        }
                    }
                    blk.idct();
                    for (drow, srow) in dst[x * 4..].chunks_mut(8).zip(blk.chunks(4)) {
                        for (del, &sel) in drow.iter_mut().zip(srow.iter()) {
                            *del = (i16::from(*del) + sel).max(0).min(255) as u8;
                        }
                    }
                }
            }
        }
    }
    pub fn set_luma_from_diff(&mut self, blk1: &[u8; 256], blk2: &[u8; 256]) {
        for (dst, (src1, src2)) in self.luma.chunks_mut(4).zip(blk1.chunks(16 * 4).zip(blk2.chunks(16 * 4))) {
            for (x, blk) in dst.iter_mut().enumerate() {
                for (dst, (row1, row2)) in blk.chunks_mut(4).zip(src1[x * 4..].chunks(16).zip(src2[x * 4..].chunks(16))) {
                    for (dst, (&a, &b)) in dst.iter_mut().zip(row1.iter().zip(row2.iter())) {
                        *dst = i16::from(a) - i16::from(b);
                    }
                }
            }
        }
    }
    pub fn set_chroma_from_diff(&mut self, blk1: &[[u8; 64]; 2], blk2: &[[u8; 64]; 2]) {
        for (chroma, (src1, src2)) in self.chroma.iter_mut().zip(blk1.iter().zip(blk2.iter())) {
            for (dst, (src1, src2)) in chroma.chunks_mut(2).zip(src1.chunks(8 * 4).zip(src2.chunks(8 * 4))) {
                for (x, blk) in dst.iter_mut().enumerate() {
                    for (dst, (row1, row2)) in blk.chunks_mut(4).zip(src1[x * 4..].chunks(8).zip(src2[x * 4..].chunks(8))) {
                        for (dst, (&a, &b)) in dst.iter_mut().zip(row1.iter().zip(row2.iter())) {
                            *dst = i16::from(a) - i16::from(b);
                        }
                    }
                }
            }
        }
    }
    pub fn fdct(&mut self) {
        self.fdct_luma();
        self.fdct_chroma();
    }
    pub fn fdct_luma(&mut self) {
        for blk in self.luma.iter_mut() {
            blk.fdct();
        }
    }
    pub fn fdct_chroma(&mut self) {
        for chroma in self.chroma.iter_mut() {
            for blk in chroma.iter_mut() {
                blk.fdct();
            }
        }
    }
    pub fn fdct_dc_block(&mut self) {
        for (dc, blk) in self.dcs.iter_mut().zip(self.luma.iter_mut()) {
            *dc = blk[0];
            blk[0] = 0;
        }
        self.dcs.fdct();
        self.has_dc = true;
    }
    pub fn idct(&mut self) {
        self.idct_luma();
        self.idct_chroma();
    }
    pub fn idct_luma(&mut self) {
        if self.has_dc {
            self.dcs.idct();
            for (&dc, blk) in self.dcs.iter().zip(self.luma.iter_mut()) {
                blk[0] = dc;
            }
        }
        for blk in self.luma.iter_mut() {
            blk.idct();
        }
    }
    pub fn idct_chroma(&mut self) {
        for chroma in self.chroma.iter_mut() {
            for blk in chroma.iter_mut() {
                blk.idct();
            }
        }
    }
    pub fn quant(&mut self, q: usize) {
        self.quant_luma(q);
        self.quant_chroma(q);
        self.q = q as u8;
    }
    pub fn quant_luma(&mut self, q: usize) {
        if self.has_dc {
            self.dcs[0] /= Y2_DC_QUANTS[q];
            for el in self.dcs[1..].iter_mut() {
                if *el != 0 {
                    *el /= Y2_AC_QUANTS[q];
                }
            }
        }
        for blk in self.luma.iter_mut() {
            blk[0] /= Y_DC_QUANTS[q];
            for el in blk[1..].iter_mut() {
                if *el != 0 {
                    *el /= Y_AC_QUANTS[q];
                }
            }
        }
        self.q = q as u8;
    }
    pub fn quant_chroma(&mut self, q: usize) {
        for chroma in self.chroma.iter_mut() {
            for blk in chroma.iter_mut() {
                blk[0] /= UV_DC_QUANTS[q];
                for el in blk[1..].iter_mut() {
                    if *el != 0 {
                        *el /= UV_AC_QUANTS[q];
                    }
                }
            }
        }
        self.q = q as u8;
    }
    pub fn dequant(&mut self) {
        self.dequant_luma();
        self.dequant_chroma();
    }
    pub fn dequant_luma(&mut self) {
        let q = self.q as usize;
        if self.has_dc {
            self.dcs[0] *= Y2_DC_QUANTS[q];
            for el in self.dcs[1..].iter_mut() {
                if *el != 0 {
                    *el *= Y2_AC_QUANTS[q];
                }
            }
        }
        for blk in self.luma.iter_mut() {
            blk[0] *= Y_DC_QUANTS[q];
            for el in blk[1..].iter_mut() {
                if *el != 0 {
                    *el *= Y_AC_QUANTS[q];
                }
            }
        }
    }
    pub fn dequant_chroma(&mut self) {
        let q = self.q as usize;
        for chroma in self.chroma.iter_mut() {
            for blk in chroma.iter_mut() {
                blk[0] *= UV_DC_QUANTS[q];
                for el in blk[1..].iter_mut() {
                    if *el != 0 {
                        *el *= UV_AC_QUANTS[q];
                    }
                }
            }
        }
    }
}

pub fn load_blocks(src: &NAVideoBuffer<u8>, sblocks: &mut Vec<SrcBlock>) {
    let data = src.get_data();
    let y = &data[src.get_offset(0)..];
    let u = &data[src.get_offset(1)..];
    let v = &data[src.get_offset(2)..];
    let ystride = src.get_stride(0);
    let ustride = src.get_stride(1);
    let vstride = src.get_stride(2);
    let (width, height) = src.get_dimensions(0);

    sblocks.clear();
    for (ystrip, (ustrip, vstrip)) in y.chunks(ystride * 16).take((height + 15) / 16).zip(u.chunks(ustride * 8).zip(v.chunks(vstride * 8))) {
        for x in (0..width).step_by(16) {
            let mut sblk = SrcBlock::default();

            for (dst, src) in sblk.luma.chunks_mut(16).zip(ystrip[x..].chunks(ystride)) {
                dst.copy_from_slice(&src[..16]);
            }
            for (dst, src) in sblk.chroma[0].chunks_mut(8).zip(ustrip[x / 2..].chunks(ustride)) {
                dst.copy_from_slice(&src[..8]);
            }
            for (dst, src) in sblk.chroma[1].chunks_mut(8).zip(vstrip[x / 2..].chunks(vstride)) {
                dst.copy_from_slice(&src[..8]);
            }
            sblocks.push(sblk);
        }
    }
}

pub struct YModePred {
    pub cache:  GenericCache<PredMode>,
}

impl YModePred {
    fn resize(&mut self, mb_w: usize) {
        self.cache = GenericCache::new(4, mb_w * 4 + 1, PredMode::DCPred);
    }
    pub fn set_mode(&mut self, mb_x: usize, mode: PredMode) {
        for row in self.cache.data[self.cache.xpos + mb_x * 4..].chunks_mut(self.cache.stride).take(4) {
            for el in row[..4].iter_mut() {
                *el = mode.to_b_mode();
            }
        }
    }
    pub fn set_modes4x4(&mut self, mb_x: usize, imodes: &[PredMode; 16], ctx: &mut [u8; 16]) {
        let mut off = self.cache.xpos + mb_x * 4;
        for y in 0..4 {
            for x in 0..4 {
                let top_idx = self.cache.data[off + x - self.cache.stride].to_b_index();
                let left_idx = self.cache.data[off + x - 1].to_b_index();
                self.cache.data[off + x] = imodes[x + y * 4];
                ctx[x + y * 4] = ((top_idx * 10) + left_idx) as u8;
            }
            off += self.cache.stride;
        }
    }
}

impl Default for YModePred {
    fn default() -> Self {
        Self {
            cache: GenericCache::new(0, 0, PredMode::DCPred)
        }
    }
}

#[derive(Default)]
pub struct BlockPCtx {
    pub nz_y2:      u8,
    pub nz_y_top:   [bool; 4],
    pub nz_y_left:  [bool; 4],
    pub nz_c_top:   [[bool; 2]; 2],
    pub nz_c_left:  [[bool; 2]; 2],
}

#[derive(Default)]
pub struct PredContext {
    pub mb_w:       usize,
    pub mb_h:       usize,

    pub top_line_y: Vec<u8>,
    pub top_line_u: Vec<u8>,
    pub top_line_v: Vec<u8>,
    pub tl_y:       u8,
    pub tl_u:       u8,
    pub tl_v:       u8,

    pub left_y:     [u8; 16],
    pub left_u:     [u8; 16],
    pub left_v:     [u8; 16],

    pub dc_last:    [i16; 2],
    pub dc_count:   [usize; 2],
    dc_last_saved:  [i16; 2],
    dc_count_saved: [usize; 2],
    pub nz_y2_top:  Vec<bool>,
    pub nz_y2_left: bool,
    pub nz_y_top:   Vec<bool>,
    pub nz_y_left:  [bool; 4],
    pub nz_c_top:   [Vec<bool>; 2],
    pub nz_c_left:  [[bool; 2]; 2],

    pub ymodes:     YModePred,

    pub mvs:        Vec<MV>,
    pub mv_stride:  usize,
    pub version:    u8,
}

impl PredContext {
    pub fn new() -> Self { Self::default() }
    pub fn resize(&mut self, mb_w: usize, mb_h: usize) {
        self.mb_w = mb_w;
        self.mb_h = mb_h;

        self.top_line_y.resize(mb_w * 16 + 1, 0);
        self.top_line_u.resize(mb_w * 8 + 1, 0);
        self.top_line_v.resize(mb_w * 8 + 1, 0);

        self.nz_y2_top.resize(mb_w, false);
        self.nz_y_top.resize(mb_w * 4, false);
        self.nz_c_top[0].resize(mb_w * 2, false);
        self.nz_c_top[1].resize(mb_w * 2, false);

        self.ymodes.resize(mb_w);

        self.mv_stride = mb_w * 4;
        self.mvs.resize(self.mv_stride * mb_h * 4, ZERO_MV);
    }

    pub fn reset(&mut self) {
        for el in self.top_line_y.iter_mut() { *el = 0x80; }
        for el in self.top_line_u.iter_mut() { *el = 0x80; }
        for el in self.top_line_v.iter_mut() { *el = 0x80; }
        self.left_y = [0x80; 16];
        self.left_u = [0x80; 16];
        self.left_v = [0x80; 16];
        self.tl_y = 0x80;
        self.tl_u = 0x80;
        self.tl_v = 0x80;

        for el in self.nz_y_top.iter_mut() { *el = false; }
        self.nz_y_left = [false; 4];
        for el in self.nz_y2_top.iter_mut() { *el = false; }
        self.nz_y2_left = false;
        for el in self.nz_c_top[0].iter_mut() { *el = false; }
        for el in self.nz_c_top[1].iter_mut() { *el = false; }
        self.nz_c_left = [[false; 2]; 2];

        self.ymodes.cache.reset();

        for mv in self.mvs.iter_mut() { *mv = ZERO_MV; }
    }
    pub fn reset_intra(&mut self) {
        self.dc_last  = [0; 2];
        self.dc_count = [0; 2];
        self.dc_last_saved  = [0; 2];
        self.dc_count_saved = [0; 2];
    }
    pub fn save_dc_pred(&mut self) {
        self.dc_last_saved  = self.dc_last;
        self.dc_count_saved = self.dc_count;
    }
    #[allow(dead_code)]
    pub fn restore_dc_pred(&mut self) {
        self.dc_last  = self.dc_last_saved;
        self.dc_count = self.dc_count_saved;
    }
    pub fn update_mb_row(&mut self) {
        self.left_y = [0x80; 16];
        self.left_u = [0x80; 16];
        self.left_v = [0x80; 16];
        self.tl_y = 0x80;
        self.tl_u = 0x80;
        self.tl_v = 0x80;
        self.ymodes.cache.update_row();
    }
    pub fn update_mb(&mut self, sblk: &SrcBlock, mb_x: usize) {
        for (dst, src) in self.left_y.iter_mut().zip(sblk.luma.chunks_exact(16)) {
            *dst = src[15];
        }
        self.tl_y = self.top_line_y[mb_x * 16 + 16];
        self.top_line_y[mb_x * 16 + 1..][..16].copy_from_slice(&sblk.luma[15 * 16..]);

        for (dst, src) in self.left_u.iter_mut().zip(sblk.chroma[0].chunks_exact(8)) {
            *dst = src[7];
        }
        self.tl_u = self.top_line_u[mb_x * 8 + 8];
        self.top_line_u[mb_x * 8 + 1..][..8].copy_from_slice(&sblk.chroma[0][7 * 8..]);

        for (dst, src) in self.left_v.iter_mut().zip(sblk.chroma[1].chunks_exact(8)) {
            *dst = src[7];
        }
        self.tl_v = self.top_line_v[mb_x * 8 + 8];
        self.top_line_v[mb_x * 8 + 1..][..8].copy_from_slice(&sblk.chroma[1][7 * 8..]);
    }
    pub fn fill_ipred(&mut self, plane: usize, mb_x: usize, ipred: &mut IPredContext) {
        match plane {
            0 => {
                if ipred.has_top {
                    ipred.top.copy_from_slice(&self.top_line_y[mb_x * 16 + 1..][..16]);
                    ipred.tl = self.tl_y;
                }
                ipred.left.copy_from_slice(&self.left_y);
                ipred.has_left = mb_x > 0;
            },
            1 => {
                if ipred.has_top {
                    ipred.top[..8].copy_from_slice(&self.top_line_u[mb_x * 8 + 1..][..8]);
                    ipred.tl = self.tl_u;
                }
                ipred.left.copy_from_slice(&self.left_u);
                ipred.has_left = mb_x > 0;
            },
            _ => {
                if ipred.has_top {
                    ipred.top[..8].copy_from_slice(&self.top_line_v[mb_x * 8 + 1..][..8]);
                    ipred.tl = self.tl_v;
                }
                ipred.left.copy_from_slice(&self.left_v);
                ipred.has_left = mb_x > 0;
            },
        }
    }
    pub fn get_ipred_tr(&self, mb_x: usize) -> [u8; 4] {
        if mb_x < self.mb_w - 1 {
            let mut tr = [0; 4];
            tr.copy_from_slice(&self.top_line_y[mb_x * 16 + 1 + 16..][..4]);
            tr
        } else {
            [0x80; 4]
        }
    }
    pub fn fill_pctx(&self, mb_x: usize, pctx: &mut BlockPCtx) {
        pctx.nz_y2     = (self.nz_y2_left as u8) + (self.nz_y2_top[mb_x] as u8);
        pctx.nz_y_left = self.nz_y_left;
        pctx.nz_y_top.copy_from_slice(&self.nz_y_top[mb_x * 4..][..4]);
        pctx.nz_c_left = self.nz_c_left;
        pctx.nz_c_top  = [[self.nz_c_top[0][mb_x * 2], self.nz_c_top[0][mb_x * 2 + 1]],
                          [self.nz_c_top[1][mb_x * 2], self.nz_c_top[1][mb_x * 2 + 1]]];
    }
    pub fn set_nz(&mut self, mb_x: usize, blk: &Residue) {
        if blk.has_dc {
            let has_nz = blk.dcs.has_nz();
            self.nz_y2_left = has_nz;
            self.nz_y2_top[mb_x] = has_nz;
        }
        for (y, blk_row) in blk.luma.chunks(4).enumerate() {
            for (x, blk) in blk_row.iter().enumerate() {
                let has_nz = blk.has_nz();
                self.nz_y_left[y] = has_nz;
                self.nz_y_top[mb_x * 4 + x] = has_nz;
            }
        }
        for (c, chroma) in blk.chroma.iter().enumerate() {
            for (y, blk_row) in chroma.chunks(2).enumerate() {
                for (x, blk) in blk_row.iter().enumerate() {
                    let has_nz = blk.has_nz();
                    self.nz_c_left[c][y] = has_nz;
                    self.nz_c_top[c][mb_x * 2 + x] = has_nz;
                }
            }
        }
    }

    pub fn get_y2_dc_pred(&self, last: bool) -> i16 {
        let ref_id = !last as usize;
        if self.dc_count[ref_id] > 3 {
            self.dc_last[ref_id]
        } else {
            0
        }
    }
    pub fn predict_y2_dc(&mut self, dc: &mut i16, last: bool) {
        let ref_id = !last as usize;
        let pdc = self.dc_last[ref_id];
        let orig_dc = *dc;

        if self.dc_count[ref_id] > 3 {
            *dc -= pdc;
        }

        if (pdc == 0) || (orig_dc == 0) || ((pdc ^ orig_dc) < 0) {
            self.dc_count[ref_id] = 0;
        } else if pdc == orig_dc {
            self.dc_count[ref_id] += 1;
        }
        self.dc_last[ref_id] = orig_dc;
    }

    pub fn fill_mv(&mut self, mb_x: usize, mb_y: usize, mv: MV) {
        let mut iidx = mb_x * 4 + mb_y * 4 * self.mv_stride;
        for _ in 0..4 {
            for x in 0..4 {
                self.mvs[iidx + x] = mv;
            }
            iidx += self.mb_w * 4;
        }
    }
    pub fn find_mv_pred(&self, mb_x: usize, mb_y: usize) -> ([u8; 4], MV, MV, MV) {
        let mut nearest_mv = ZERO_MV;
        let mut near_mv = ZERO_MV;

        let mut ct: [u8; 4] = [0; 4];

        let start = if self.version == 0 { 1 } else { 0 };
        let mvwrap = (self.mb_w as isize) + 1;
        for (yoff, xoff, weight, blk_no) in CAND_POS.iter() {
            let cx = (mb_x as isize) + (*xoff as isize);
            let cy = (mb_y as isize) + (*yoff as isize);
            let mvpos = cx + cy * mvwrap;
            if (mvpos < start) || ((mvpos % mvwrap) == (mvwrap - 1)) {
                ct[0] += weight;
                continue;
            }
            let cx = (mvpos % mvwrap) as usize;
            let cy = (mvpos / mvwrap) as usize;
            let bx = (*blk_no as usize) & 3;
            let by = (*blk_no as usize) >> 2;
            let blk_pos = cx * 4 + bx + (cy * 4 + by) * self.mv_stride;
            let mv = self.mvs[blk_pos];
            if mv == ZERO_MV {
                ct[0] += weight;
                continue;
            }
            let idx;
            if (nearest_mv == ZERO_MV) || (nearest_mv == mv) {
                nearest_mv = mv;
                idx = 1;
            } else if near_mv == ZERO_MV {
                near_mv = mv;
                idx = 2;
            } else {
                idx = if mv == near_mv { 2 } else { 3 };
            }
            ct[idx] += weight;
        }
        let pred_mv = if ct[1] > ct[2] {
                if ct[1] >= ct[0] { nearest_mv } else { ZERO_MV }
            } else {
                if ct[2] >= ct[0] { near_mv } else { ZERO_MV }
            };

        let mvprobs = [INTER_MODE_PROBS[ct[0] as usize][0],
                       INTER_MODE_PROBS[ct[1] as usize][1],
                       INTER_MODE_PROBS[ct[2] as usize][2],
                       INTER_MODE_PROBS[ct[2] as usize][3]];

        (mvprobs, nearest_mv, near_mv, pred_mv)
    }
}