oximedia-codec 0.1.7

Video codec implementations for OxiMedia
Documentation
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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
//! VP9 Inter prediction types and structures.
//!
//! This module provides inter-prediction modes, compound prediction types,
//! and context structures for motion-compensated prediction in VP9 decoding.
//!
//! Inter prediction uses motion vectors to reference blocks from previously
//! decoded frames (LAST, GOLDEN, or ALTREF references).

#![forbid(unsafe_code)]
#![allow(dead_code)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::similar_names)]
#![allow(clippy::fn_params_excessive_bools)]
#![allow(clippy::if_not_else)]
#![allow(clippy::bool_to_int_with_if)]

use super::mv::{MotionVector, MvPair, MvRefType, RefPair};
use super::partition::BlockSize;

/// Number of inter prediction modes.
pub const INTER_MODES: usize = 4;

/// Number of compound prediction modes.
pub const COMPOUND_MODES: usize = 8;

/// Number of reference frame types (including intra).
pub const REF_FRAMES: usize = 4;

/// Number of inter reference frames (excluding intra).
pub const INTER_REFS: usize = 3;

/// Maximum number of reference motion vectors.
pub const MAX_REF_MV_CANDIDATES: usize = 8;

/// Maximum number of motion vector references per block.
pub const MAX_MV_REF_CANDIDATES: usize = 2;

/// Inter prediction mode.
///
/// These modes describe how motion vectors are derived for inter blocks.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Hash)]
#[repr(u8)]
pub enum InterMode {
    /// Use the nearest motion vector from spatial neighbors.
    #[default]
    NearestMv = 0,
    /// Use the near motion vector from spatial neighbors.
    NearMv = 1,
    /// Use a zero motion vector.
    ZeroMv = 2,
    /// Use a new motion vector read from the bitstream.
    NewMv = 3,
}

impl InterMode {
    /// All inter modes in order.
    pub const ALL: [InterMode; INTER_MODES] = [
        InterMode::NearestMv,
        InterMode::NearMv,
        InterMode::ZeroMv,
        InterMode::NewMv,
    ];

    /// Converts from u8 value to `InterMode`.
    #[must_use]
    pub const fn from_u8(value: u8) -> Option<Self> {
        match value {
            0 => Some(Self::NearestMv),
            1 => Some(Self::NearMv),
            2 => Some(Self::ZeroMv),
            3 => Some(Self::NewMv),
            _ => None,
        }
    }

    /// Returns true if this mode requires reading a motion vector delta.
    #[must_use]
    pub const fn requires_mv_delta(&self) -> bool {
        matches!(self, Self::NewMv)
    }

    /// Returns true if this mode uses a zero motion vector.
    #[must_use]
    pub const fn is_zero(&self) -> bool {
        matches!(self, Self::ZeroMv)
    }

    /// Returns true if this mode uses reference motion vectors.
    #[must_use]
    pub const fn uses_ref_mv(&self) -> bool {
        matches!(self, Self::NearestMv | Self::NearMv)
    }

    /// Returns the index of this mode.
    #[must_use]
    pub const fn index(&self) -> usize {
        *self as usize
    }
}

impl From<InterMode> for u8 {
    fn from(value: InterMode) -> Self {
        value as u8
    }
}

/// Compound prediction mode.
///
/// These modes are used for compound prediction with two reference frames.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Hash)]
#[repr(u8)]
pub enum CompoundMode {
    /// Both references use NEAREST motion vector.
    #[default]
    NearestNearest = 0,
    /// First reference NEAREST, second NEAR.
    NearestNear = 1,
    /// First reference NEAR, second NEAREST.
    NearNearest = 2,
    /// Both references use NEAR motion vector.
    NearNear = 3,
    /// First reference NEAREST, second NEW.
    NearestNew = 4,
    /// First reference NEW, second NEAREST.
    NewNearest = 5,
    /// First reference NEAR, second NEW.
    NearNew = 6,
    /// First reference NEW, second NEAR.
    NewNear = 7,
}

impl CompoundMode {
    /// All compound modes in order.
    pub const ALL: [CompoundMode; COMPOUND_MODES] = [
        CompoundMode::NearestNearest,
        CompoundMode::NearestNear,
        CompoundMode::NearNearest,
        CompoundMode::NearNear,
        CompoundMode::NearestNew,
        CompoundMode::NewNearest,
        CompoundMode::NearNew,
        CompoundMode::NewNear,
    ];

    /// Converts from u8 value to `CompoundMode`.
    #[must_use]
    pub const fn from_u8(value: u8) -> Option<Self> {
        match value {
            0 => Some(Self::NearestNearest),
            1 => Some(Self::NearestNear),
            2 => Some(Self::NearNearest),
            3 => Some(Self::NearNear),
            4 => Some(Self::NearestNew),
            5 => Some(Self::NewNearest),
            6 => Some(Self::NearNew),
            7 => Some(Self::NewNear),
            _ => None,
        }
    }

    /// Returns true if the first reference requires reading a new motion vector.
    #[must_use]
    pub const fn first_requires_new_mv(&self) -> bool {
        matches!(self, Self::NewNearest | Self::NewNear)
    }

    /// Returns true if the second reference requires reading a new motion vector.
    #[must_use]
    pub const fn second_requires_new_mv(&self) -> bool {
        matches!(self, Self::NearestNew | Self::NearNew)
    }

    /// Returns true if any reference requires a new motion vector.
    #[must_use]
    pub const fn requires_new_mv(&self) -> bool {
        self.first_requires_new_mv() || self.second_requires_new_mv()
    }

    /// Returns the inter mode for the first reference.
    #[must_use]
    pub const fn first_mode(&self) -> InterMode {
        match self {
            Self::NearestNearest | Self::NearestNear | Self::NearestNew => InterMode::NearestMv,
            Self::NearNearest | Self::NearNear | Self::NearNew => InterMode::NearMv,
            Self::NewNearest | Self::NewNear => InterMode::NewMv,
        }
    }

    /// Returns the inter mode for the second reference.
    #[must_use]
    pub const fn second_mode(&self) -> InterMode {
        match self {
            Self::NearestNearest | Self::NearNearest | Self::NewNearest => InterMode::NearestMv,
            Self::NearestNear | Self::NearNear | Self::NewNear => InterMode::NearMv,
            Self::NearestNew | Self::NearNew => InterMode::NewMv,
        }
    }

    /// Returns the index of this mode.
    #[must_use]
    pub const fn index(&self) -> usize {
        *self as usize
    }
}

impl From<CompoundMode> for u8 {
    fn from(value: CompoundMode) -> Self {
        value as u8
    }
}

/// Reference frame type for VP9.
///
/// VP9 supports three inter reference frames and one intra (no reference) type.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Hash)]
#[repr(u8)]
pub enum RefFrameType {
    /// Intra prediction (no reference frame).
    #[default]
    Intra = 0,
    /// Last decoded frame.
    Last = 1,
    /// Golden reference frame.
    Golden = 2,
    /// Alternate reference frame.
    AltRef = 3,
}

impl RefFrameType {
    /// All reference frame types.
    pub const ALL: [RefFrameType; REF_FRAMES] = [
        RefFrameType::Intra,
        RefFrameType::Last,
        RefFrameType::Golden,
        RefFrameType::AltRef,
    ];

    /// Inter reference frame types only.
    pub const INTER_REFS: [RefFrameType; INTER_REFS] = [
        RefFrameType::Last,
        RefFrameType::Golden,
        RefFrameType::AltRef,
    ];

    /// Converts from u8 value to `RefFrameType`.
    #[must_use]
    pub const fn from_u8(value: u8) -> Option<Self> {
        match value {
            0 => Some(Self::Intra),
            1 => Some(Self::Last),
            2 => Some(Self::Golden),
            3 => Some(Self::AltRef),
            _ => None,
        }
    }

    /// Returns true if this is an inter reference type.
    #[must_use]
    pub const fn is_inter(&self) -> bool {
        !matches!(self, Self::Intra)
    }

    /// Returns true if this is an intra reference type.
    #[must_use]
    pub const fn is_intra(&self) -> bool {
        matches!(self, Self::Intra)
    }

    /// Returns the index of this reference type.
    #[must_use]
    pub const fn index(&self) -> usize {
        *self as usize
    }

    /// Returns the inter reference index (0, 1, or 2), or None for intra.
    #[must_use]
    pub const fn inter_index(&self) -> Option<usize> {
        match self {
            Self::Intra => None,
            Self::Last => Some(0),
            Self::Golden => Some(1),
            Self::AltRef => Some(2),
        }
    }

    /// Converts from `MvRefType`.
    #[must_use]
    pub const fn from_mv_ref_type(mv_ref: MvRefType) -> Self {
        match mv_ref {
            MvRefType::Intra => Self::Intra,
            MvRefType::Last => Self::Last,
            MvRefType::Golden => Self::Golden,
            MvRefType::AltRef => Self::AltRef,
        }
    }

    /// Converts to `MvRefType`.
    #[must_use]
    pub const fn to_mv_ref_type(&self) -> MvRefType {
        match self {
            Self::Intra => MvRefType::Intra,
            Self::Last => MvRefType::Last,
            Self::Golden => MvRefType::Golden,
            Self::AltRef => MvRefType::AltRef,
        }
    }
}

impl From<RefFrameType> for u8 {
    fn from(value: RefFrameType) -> Self {
        value as u8
    }
}

impl From<MvRefType> for RefFrameType {
    fn from(value: MvRefType) -> Self {
        Self::from_mv_ref_type(value)
    }
}

/// Prediction mode for a block.
///
/// This enum encapsulates both single and compound inter prediction modes.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum PredictionMode {
    /// Intra prediction (no reference frames).
    #[default]
    Intra,
    /// Single reference inter prediction.
    SingleRef {
        /// The inter prediction mode.
        mode: InterMode,
        /// The reference frame type.
        ref_frame: RefFrameType,
        /// The motion vector.
        mv: MotionVector,
    },
    /// Compound (two reference) inter prediction.
    Compound {
        /// The compound prediction mode.
        mode: CompoundMode,
        /// The reference frame pair.
        ref_frames: RefPair,
        /// The motion vector pair.
        mvs: MvPair,
    },
}

impl PredictionMode {
    /// Creates a new single reference prediction mode.
    #[must_use]
    pub const fn single(mode: InterMode, ref_frame: RefFrameType, mv: MotionVector) -> Self {
        Self::SingleRef {
            mode,
            ref_frame,
            mv,
        }
    }

    /// Creates a new compound prediction mode.
    #[must_use]
    pub const fn compound(mode: CompoundMode, ref_frames: RefPair, mvs: MvPair) -> Self {
        Self::Compound {
            mode,
            ref_frames,
            mvs,
        }
    }

    /// Returns true if this is an intra prediction mode.
    #[must_use]
    pub const fn is_intra(&self) -> bool {
        matches!(self, Self::Intra)
    }

    /// Returns true if this is a single reference inter prediction mode.
    #[must_use]
    pub const fn is_single_ref(&self) -> bool {
        matches!(self, Self::SingleRef { .. })
    }

    /// Returns true if this is a compound (two reference) prediction mode.
    #[must_use]
    pub const fn is_compound(&self) -> bool {
        matches!(self, Self::Compound { .. })
    }

    /// Returns true if this is any inter prediction mode.
    #[must_use]
    pub const fn is_inter(&self) -> bool {
        !self.is_intra()
    }

    /// Returns the first motion vector, if any.
    #[must_use]
    pub const fn mv0(&self) -> Option<MotionVector> {
        match self {
            Self::Intra => None,
            Self::SingleRef { mv, .. } => Some(*mv),
            Self::Compound { mvs, .. } => Some(mvs.mv0),
        }
    }

    /// Returns the second motion vector for compound prediction, if any.
    #[must_use]
    pub const fn mv1(&self) -> Option<MotionVector> {
        match self {
            Self::Intra | Self::SingleRef { .. } => None,
            Self::Compound { mvs, .. } => Some(mvs.mv1),
        }
    }

    /// Returns the first reference frame type, if any.
    #[must_use]
    pub const fn ref0(&self) -> Option<RefFrameType> {
        match self {
            Self::Intra => None,
            Self::SingleRef { ref_frame, .. } => Some(*ref_frame),
            Self::Compound { ref_frames, .. } => {
                Some(RefFrameType::from_mv_ref_type(ref_frames.ref0))
            }
        }
    }

    /// Returns the second reference frame type for compound prediction, if any.
    #[must_use]
    pub const fn ref1(&self) -> Option<RefFrameType> {
        match self {
            Self::Intra | Self::SingleRef { .. } => None,
            Self::Compound { ref_frames, .. } => {
                Some(RefFrameType::from_mv_ref_type(ref_frames.ref1))
            }
        }
    }
}

/// Inter prediction context.
///
/// This structure holds context information needed for inter prediction
/// of a block, including reference frames and motion vectors.
#[derive(Clone, Debug, Default)]
pub struct InterPredContext {
    /// Block size for this context.
    pub block_size: BlockSize,
    /// Block row position in 4x4 units.
    pub mi_row: usize,
    /// Block column position in 4x4 units.
    pub mi_col: usize,
    /// The prediction mode.
    pub mode: PredictionMode,
    /// Reference frame sign bias (for motion vector sign adjustment).
    pub ref_sign_bias: [bool; REF_FRAMES],
    /// Whether the block is compound prediction.
    pub is_compound: bool,
    /// Reference motion vector candidates for first reference.
    pub ref_mv_candidates_0: [MotionVector; MAX_MV_REF_CANDIDATES],
    /// Reference motion vector candidates for second reference (compound).
    pub ref_mv_candidates_1: [MotionVector; MAX_MV_REF_CANDIDATES],
    /// Number of valid candidates for first reference.
    pub ref_mv_count_0: usize,
    /// Number of valid candidates for second reference.
    pub ref_mv_count_1: usize,
}

impl InterPredContext {
    /// Creates a new inter prediction context.
    #[must_use]
    pub const fn new(block_size: BlockSize, mi_row: usize, mi_col: usize) -> Self {
        Self {
            block_size,
            mi_row,
            mi_col,
            mode: PredictionMode::Intra,
            ref_sign_bias: [false; REF_FRAMES],
            is_compound: false,
            ref_mv_candidates_0: [MotionVector::zero(); MAX_MV_REF_CANDIDATES],
            ref_mv_candidates_1: [MotionVector::zero(); MAX_MV_REF_CANDIDATES],
            ref_mv_count_0: 0,
            ref_mv_count_1: 0,
        }
    }

    /// Sets the prediction mode.
    pub fn set_mode(&mut self, mode: PredictionMode) {
        self.mode = mode;
        self.is_compound = mode.is_compound();
    }

    /// Sets the reference sign bias.
    pub fn set_sign_bias(&mut self, ref_type: RefFrameType, bias: bool) {
        self.ref_sign_bias[ref_type.index()] = bias;
    }

    /// Returns the sign bias for a reference frame.
    #[must_use]
    pub const fn sign_bias(&self, ref_type: RefFrameType) -> bool {
        self.ref_sign_bias[ref_type.index()]
    }

    /// Adds a reference motion vector candidate for the first reference.
    pub fn add_mv_candidate_0(&mut self, mv: MotionVector) {
        if self.ref_mv_count_0 < MAX_MV_REF_CANDIDATES {
            self.ref_mv_candidates_0[self.ref_mv_count_0] = mv;
            self.ref_mv_count_0 += 1;
        }
    }

    /// Adds a reference motion vector candidate for the second reference.
    pub fn add_mv_candidate_1(&mut self, mv: MotionVector) {
        if self.ref_mv_count_1 < MAX_MV_REF_CANDIDATES {
            self.ref_mv_candidates_1[self.ref_mv_count_1] = mv;
            self.ref_mv_count_1 += 1;
        }
    }

    /// Returns the nearest motion vector for the first reference.
    #[must_use]
    pub const fn nearest_mv_0(&self) -> MotionVector {
        if self.ref_mv_count_0 > 0 {
            self.ref_mv_candidates_0[0]
        } else {
            MotionVector::zero()
        }
    }

    /// Returns the near motion vector for the first reference.
    #[must_use]
    pub const fn near_mv_0(&self) -> MotionVector {
        if self.ref_mv_count_0 > 1 {
            self.ref_mv_candidates_0[1]
        } else {
            MotionVector::zero()
        }
    }

    /// Returns the nearest motion vector for the second reference.
    #[must_use]
    pub const fn nearest_mv_1(&self) -> MotionVector {
        if self.ref_mv_count_1 > 0 {
            self.ref_mv_candidates_1[0]
        } else {
            MotionVector::zero()
        }
    }

    /// Returns the near motion vector for the second reference.
    #[must_use]
    pub const fn near_mv_1(&self) -> MotionVector {
        if self.ref_mv_count_1 > 1 {
            self.ref_mv_candidates_1[1]
        } else {
            MotionVector::zero()
        }
    }

    /// Clears all motion vector candidates.
    pub fn clear_candidates(&mut self) {
        self.ref_mv_count_0 = 0;
        self.ref_mv_count_1 = 0;
        self.ref_mv_candidates_0 = [MotionVector::zero(); MAX_MV_REF_CANDIDATES];
        self.ref_mv_candidates_1 = [MotionVector::zero(); MAX_MV_REF_CANDIDATES];
    }

    /// Returns the pixel x coordinate.
    #[must_use]
    pub const fn pixel_x(&self) -> usize {
        self.mi_col * 4
    }

    /// Returns the pixel y coordinate.
    #[must_use]
    pub const fn pixel_y(&self) -> usize {
        self.mi_row * 4
    }

    /// Returns the block width in pixels.
    #[must_use]
    pub const fn width(&self) -> usize {
        self.block_size.width()
    }

    /// Returns the block height in pixels.
    #[must_use]
    pub const fn height(&self) -> usize {
        self.block_size.height()
    }
}

/// Inter mode context for probability selection.
#[derive(Clone, Copy, Debug, Default)]
pub struct InterModeContext {
    /// Context for inter mode selection.
    pub mode_context: u8,
    /// Context for new motion vector mode.
    pub new_mv_context: u8,
    /// Context for zero motion vector mode.
    pub zero_mv_context: u8,
    /// Context for reference motion vector mode.
    pub ref_mv_context: u8,
}

impl InterModeContext {
    /// Creates a new inter mode context.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            mode_context: 0,
            new_mv_context: 0,
            zero_mv_context: 0,
            ref_mv_context: 0,
        }
    }

    /// Creates context from neighbor counts.
    #[must_use]
    pub fn from_neighbors(
        same_ref_count: u8,
        diff_ref_count: u8,
        new_mv_count: u8,
        zero_mv_count: u8,
    ) -> Self {
        // Context calculation based on VP9 specification
        let mode_context = match (same_ref_count, diff_ref_count) {
            (0, 0) => 0,
            (1, 0) | (0, 1) => 1,
            (1, 1) => 2,
            (2, 0) | (0, 2) => 3,
            (2, 1) | (1, 2) => 4,
            _ => 5,
        };

        Self {
            mode_context,
            new_mv_context: new_mv_count.min(2),
            zero_mv_context: zero_mv_count.min(2),
            ref_mv_context: same_ref_count.min(2),
        }
    }

    /// Returns the combined mode context index.
    #[must_use]
    pub const fn mode_index(&self) -> usize {
        self.mode_context as usize
    }
}

/// Reference frame context for probability selection.
#[derive(Clone, Copy, Debug, Default)]
pub struct RefFrameContext {
    /// Context for single vs compound reference.
    pub comp_mode_context: u8,
    /// Context for LAST vs GOLDEN reference.
    pub single_ref_context_0: u8,
    /// Context for GOLDEN vs ALTREF reference.
    pub single_ref_context_1: u8,
    /// Context for compound reference selection.
    pub comp_ref_context: u8,
}

impl RefFrameContext {
    /// Creates a new reference frame context.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            comp_mode_context: 0,
            single_ref_context_0: 0,
            single_ref_context_1: 0,
            comp_ref_context: 0,
        }
    }

    /// Creates context from above and left neighbors.
    #[must_use]
    #[allow(clippy::too_many_arguments)]
    pub fn from_neighbors(
        above_ref: Option<RefFrameType>,
        left_ref: Option<RefFrameType>,
        above_compound: bool,
        left_compound: bool,
        above_intra: bool,
        left_intra: bool,
        has_above: bool,
        has_left: bool,
    ) -> Self {
        let mut ctx = Self::new();

        // Compound mode context
        if has_above && has_left {
            if above_compound && left_compound {
                ctx.comp_mode_context = 4;
            } else if above_compound || left_compound {
                ctx.comp_mode_context = 2;
            } else if above_intra || left_intra {
                ctx.comp_mode_context = 1;
            } else {
                ctx.comp_mode_context = 0;
            }
        } else if has_above {
            ctx.comp_mode_context = if above_compound { 3 } else { 0 };
        } else if has_left {
            ctx.comp_mode_context = if left_compound { 3 } else { 0 };
        }

        // Single reference context
        let above_is_last = matches!(above_ref, Some(RefFrameType::Last));
        let left_is_last = matches!(left_ref, Some(RefFrameType::Last));
        let above_is_golden = matches!(above_ref, Some(RefFrameType::Golden));
        let left_is_golden = matches!(left_ref, Some(RefFrameType::Golden));

        if has_above && has_left {
            if above_intra && left_intra {
                ctx.single_ref_context_0 = 2;
                ctx.single_ref_context_1 = 2;
            } else if above_intra || left_intra {
                let ref_frame = if above_intra { left_ref } else { above_ref };
                let is_last = matches!(ref_frame, Some(RefFrameType::Last));
                let is_golden = matches!(ref_frame, Some(RefFrameType::Golden));
                ctx.single_ref_context_0 = if is_last { 2 } else { 3 };
                ctx.single_ref_context_1 = if is_golden { 2 } else { 3 };
            } else {
                ctx.single_ref_context_0 = if above_is_last && left_is_last {
                    3
                } else if above_is_last || left_is_last {
                    1
                } else {
                    0
                };
                ctx.single_ref_context_1 = if above_is_golden && left_is_golden {
                    3
                } else if above_is_golden || left_is_golden {
                    1
                } else {
                    0
                };
            }
        } else if has_above {
            ctx.single_ref_context_0 = if above_intra {
                2
            } else if above_is_last {
                3
            } else {
                0
            };
            ctx.single_ref_context_1 = if above_intra {
                2
            } else if above_is_golden {
                3
            } else {
                0
            };
        } else if has_left {
            ctx.single_ref_context_0 = if left_intra {
                2
            } else if left_is_last {
                3
            } else {
                0
            };
            ctx.single_ref_context_1 = if left_intra {
                2
            } else if left_is_golden {
                3
            } else {
                0
            };
        } else {
            ctx.single_ref_context_0 = 2;
            ctx.single_ref_context_1 = 2;
        }

        ctx
    }
}

/// Scaling factors for reference frame upscaling/downscaling.
#[derive(Clone, Copy, Debug, Default)]
pub struct ScalingFactors {
    /// Horizontal scaling factor (fixed-point 14-bit fraction).
    pub x_scale: i32,
    /// Vertical scaling factor (fixed-point 14-bit fraction).
    pub y_scale: i32,
    /// True if scaling is needed.
    pub is_scaled: bool,
}

impl ScalingFactors {
    /// Fixed-point scale factor for 1:1 (no scaling).
    pub const SCALE_ONE: i32 = 1 << 14;

    /// Creates scaling factors with no scaling.
    #[must_use]
    pub const fn identity() -> Self {
        Self {
            x_scale: Self::SCALE_ONE,
            y_scale: Self::SCALE_ONE,
            is_scaled: false,
        }
    }

    /// Creates scaling factors from source and destination dimensions.
    #[must_use]
    pub fn from_dimensions(
        src_width: u32,
        src_height: u32,
        dst_width: u32,
        dst_height: u32,
    ) -> Self {
        let x_scale = ((i64::from(src_width) << 14) / i64::from(dst_width)) as i32;
        let y_scale = ((i64::from(src_height) << 14) / i64::from(dst_height)) as i32;
        let is_scaled = x_scale != Self::SCALE_ONE || y_scale != Self::SCALE_ONE;

        Self {
            x_scale,
            y_scale,
            is_scaled,
        }
    }

    /// Scales a horizontal position.
    #[must_use]
    pub const fn scale_x(&self, x: i32) -> i32 {
        if self.is_scaled {
            (x * self.x_scale) >> 14
        } else {
            x
        }
    }

    /// Scales a vertical position.
    #[must_use]
    pub const fn scale_y(&self, y: i32) -> i32 {
        if self.is_scaled {
            (y * self.y_scale) >> 14
        } else {
            y
        }
    }

    /// Scales a motion vector.
    #[must_use]
    #[allow(clippy::cast_possible_truncation)]
    pub fn scale_mv(&self, mv: MotionVector) -> MotionVector {
        if self.is_scaled {
            MotionVector::new(
                ((i32::from(mv.row) * self.y_scale) >> 14) as i16,
                ((i32::from(mv.col) * self.x_scale) >> 14) as i16,
            )
        } else {
            mv
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_inter_mode() {
        assert_eq!(InterMode::NearestMv.index(), 0);
        assert_eq!(InterMode::NearMv.index(), 1);
        assert_eq!(InterMode::ZeroMv.index(), 2);
        assert_eq!(InterMode::NewMv.index(), 3);

        assert!(InterMode::NewMv.requires_mv_delta());
        assert!(!InterMode::ZeroMv.requires_mv_delta());

        assert!(InterMode::ZeroMv.is_zero());
        assert!(!InterMode::NearestMv.is_zero());

        assert!(InterMode::NearestMv.uses_ref_mv());
        assert!(InterMode::NearMv.uses_ref_mv());
        assert!(!InterMode::NewMv.uses_ref_mv());
    }

    #[test]
    fn test_inter_mode_from_u8() {
        assert_eq!(InterMode::from_u8(0), Some(InterMode::NearestMv));
        assert_eq!(InterMode::from_u8(3), Some(InterMode::NewMv));
        assert_eq!(InterMode::from_u8(4), None);
    }

    #[test]
    fn test_compound_mode() {
        assert!(CompoundMode::NewNearest.first_requires_new_mv());
        assert!(!CompoundMode::NewNearest.second_requires_new_mv());
        assert!(CompoundMode::NearestNew.second_requires_new_mv());
        assert!(!CompoundMode::NearestNew.first_requires_new_mv());

        assert_eq!(
            CompoundMode::NearestNearest.first_mode(),
            InterMode::NearestMv
        );
        assert_eq!(
            CompoundMode::NearestNearest.second_mode(),
            InterMode::NearestMv
        );
        assert_eq!(CompoundMode::NewNear.first_mode(), InterMode::NewMv);
        assert_eq!(CompoundMode::NewNear.second_mode(), InterMode::NearMv);
    }

    #[test]
    fn test_compound_mode_from_u8() {
        assert_eq!(CompoundMode::from_u8(0), Some(CompoundMode::NearestNearest));
        assert_eq!(CompoundMode::from_u8(7), Some(CompoundMode::NewNear));
        assert_eq!(CompoundMode::from_u8(8), None);
    }

    #[test]
    fn test_ref_frame_type() {
        assert!(RefFrameType::Intra.is_intra());
        assert!(!RefFrameType::Intra.is_inter());
        assert!(RefFrameType::Last.is_inter());
        assert!(RefFrameType::Golden.is_inter());
        assert!(RefFrameType::AltRef.is_inter());

        assert_eq!(RefFrameType::Intra.index(), 0);
        assert_eq!(RefFrameType::Last.index(), 1);
        assert_eq!(RefFrameType::Golden.index(), 2);
        assert_eq!(RefFrameType::AltRef.index(), 3);

        assert_eq!(RefFrameType::Intra.inter_index(), None);
        assert_eq!(RefFrameType::Last.inter_index(), Some(0));
        assert_eq!(RefFrameType::Golden.inter_index(), Some(1));
        assert_eq!(RefFrameType::AltRef.inter_index(), Some(2));
    }

    #[test]
    fn test_ref_frame_type_conversion() {
        assert_eq!(
            RefFrameType::from_mv_ref_type(MvRefType::Last),
            RefFrameType::Last
        );
        assert_eq!(RefFrameType::Last.to_mv_ref_type(), MvRefType::Last);

        let ref_type: RefFrameType = MvRefType::Golden.into();
        assert_eq!(ref_type, RefFrameType::Golden);
    }

    #[test]
    fn test_prediction_mode_intra() {
        let mode = PredictionMode::Intra;
        assert!(mode.is_intra());
        assert!(!mode.is_inter());
        assert!(!mode.is_single_ref());
        assert!(!mode.is_compound());
        assert!(mode.mv0().is_none());
        assert!(mode.ref0().is_none());
    }

    #[test]
    fn test_prediction_mode_single_ref() {
        let mv = MotionVector::new(10, 20);
        let mode = PredictionMode::single(InterMode::NearestMv, RefFrameType::Last, mv);

        assert!(!mode.is_intra());
        assert!(mode.is_inter());
        assert!(mode.is_single_ref());
        assert!(!mode.is_compound());
        assert_eq!(mode.mv0(), Some(mv));
        assert!(mode.mv1().is_none());
        assert_eq!(mode.ref0(), Some(RefFrameType::Last));
        assert!(mode.ref1().is_none());
    }

    #[test]
    fn test_prediction_mode_compound() {
        let mvs = MvPair::new(MotionVector::new(10, 20), MotionVector::new(30, 40));
        let ref_frames = RefPair::compound(MvRefType::Last, MvRefType::Golden);
        let mode = PredictionMode::compound(CompoundMode::NearestNearest, ref_frames, mvs);

        assert!(!mode.is_intra());
        assert!(mode.is_inter());
        assert!(!mode.is_single_ref());
        assert!(mode.is_compound());
        assert_eq!(mode.mv0(), Some(mvs.mv0));
        assert_eq!(mode.mv1(), Some(mvs.mv1));
        assert_eq!(mode.ref0(), Some(RefFrameType::Last));
        assert_eq!(mode.ref1(), Some(RefFrameType::Golden));
    }

    #[test]
    fn test_inter_pred_context() {
        let mut ctx = InterPredContext::new(BlockSize::Block16x16, 4, 8);

        assert_eq!(ctx.block_size, BlockSize::Block16x16);
        assert_eq!(ctx.mi_row, 4);
        assert_eq!(ctx.mi_col, 8);
        assert_eq!(ctx.pixel_x(), 32);
        assert_eq!(ctx.pixel_y(), 16);
        assert_eq!(ctx.width(), 16);
        assert_eq!(ctx.height(), 16);

        ctx.add_mv_candidate_0(MotionVector::new(10, 20));
        ctx.add_mv_candidate_0(MotionVector::new(30, 40));

        assert_eq!(ctx.ref_mv_count_0, 2);
        assert_eq!(ctx.nearest_mv_0(), MotionVector::new(10, 20));
        assert_eq!(ctx.near_mv_0(), MotionVector::new(30, 40));

        ctx.clear_candidates();
        assert_eq!(ctx.ref_mv_count_0, 0);
        assert_eq!(ctx.nearest_mv_0(), MotionVector::zero());
    }

    #[test]
    fn test_inter_pred_context_sign_bias() {
        let mut ctx = InterPredContext::new(BlockSize::Block8x8, 0, 0);

        ctx.set_sign_bias(RefFrameType::Golden, true);
        ctx.set_sign_bias(RefFrameType::AltRef, true);

        assert!(!ctx.sign_bias(RefFrameType::Intra));
        assert!(!ctx.sign_bias(RefFrameType::Last));
        assert!(ctx.sign_bias(RefFrameType::Golden));
        assert!(ctx.sign_bias(RefFrameType::AltRef));
    }

    #[test]
    fn test_inter_mode_context() {
        let ctx = InterModeContext::from_neighbors(2, 0, 1, 0);
        assert_eq!(ctx.mode_context, 3);
        assert_eq!(ctx.new_mv_context, 1);
        assert_eq!(ctx.zero_mv_context, 0);
        assert_eq!(ctx.ref_mv_context, 2);
    }

    #[test]
    fn test_ref_frame_context() {
        let ctx = RefFrameContext::from_neighbors(
            Some(RefFrameType::Last),
            Some(RefFrameType::Last),
            false,
            false,
            false,
            false,
            true,
            true,
        );

        assert_eq!(ctx.comp_mode_context, 0);
        assert_eq!(ctx.single_ref_context_0, 3);
    }

    #[test]
    fn test_scaling_factors_identity() {
        let sf = ScalingFactors::identity();
        assert!(!sf.is_scaled);
        assert_eq!(sf.x_scale, ScalingFactors::SCALE_ONE);
        assert_eq!(sf.y_scale, ScalingFactors::SCALE_ONE);
    }

    #[test]
    fn test_scaling_factors_scaled() {
        let sf = ScalingFactors::from_dimensions(1920, 1080, 960, 540);
        assert!(sf.is_scaled);

        // 2:1 scaling
        assert_eq!(sf.scale_x(100), 200);
        assert_eq!(sf.scale_y(100), 200);
    }

    #[test]
    fn test_scaling_factors_mv() {
        let sf = ScalingFactors::from_dimensions(1920, 1080, 960, 540);
        let mv = MotionVector::new(10, 20);
        let scaled = sf.scale_mv(mv);

        assert_eq!(scaled.row, 20);
        assert_eq!(scaled.col, 40);
    }

    #[test]
    fn test_scaling_factors_no_scale() {
        let sf = ScalingFactors::from_dimensions(1920, 1080, 1920, 1080);
        assert!(!sf.is_scaled);
    }
}