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
//! Motion vector prediction for video encoding.
//!
//! This module provides:
//! - Spatial predictors (left, top, top-right, top-left)
//! - Temporal predictors (co-located block from reference)
//! - MVP (Motion Vector Predictor) selection
//! - MV cost calculation using lambda-based RD optimization
//!
//! Good MV prediction reduces the bits needed to encode motion vectors
//! and provides better starting points for motion search.

#![forbid(unsafe_code)]
#![allow(dead_code)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::unused_self)]
#![allow(clippy::redundant_closure_for_method_calls)]
#![allow(clippy::trivially_copy_pass_by_ref)]

use super::types::{BlockSize, MotionVector, MvCost};

/// Maximum number of MV predictors.
pub const MAX_PREDICTORS: usize = 8;

/// Weight for spatial predictors.
pub const SPATIAL_WEIGHT: u32 = 2;

/// Weight for temporal predictors.
pub const TEMPORAL_WEIGHT: u32 = 1;

/// Position of a neighboring block.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NeighborPosition {
    /// Left neighbor.
    Left,
    /// Top neighbor.
    Top,
    /// Top-right neighbor.
    TopRight,
    /// Top-left neighbor.
    TopLeft,
    /// Co-located block in reference frame.
    CoLocated,
    /// Below-left neighbor.
    BelowLeft,
    /// Median of neighbors.
    Median,
}

/// Information about a neighboring block.
#[derive(Clone, Copy, Debug, Default)]
pub struct NeighborInfo {
    /// Motion vector.
    pub mv: MotionVector,
    /// Reference frame index.
    pub ref_idx: i8,
    /// Is this neighbor available?
    pub available: bool,
    /// Is this an inter-predicted block?
    pub is_inter: bool,
}

impl NeighborInfo {
    /// Creates unavailable neighbor info.
    #[must_use]
    pub const fn unavailable() -> Self {
        Self {
            mv: MotionVector::zero(),
            ref_idx: -1,
            available: false,
            is_inter: false,
        }
    }

    /// Creates neighbor info with MV.
    #[must_use]
    pub const fn with_mv(mv: MotionVector, ref_idx: i8) -> Self {
        Self {
            mv,
            ref_idx,
            available: true,
            is_inter: true,
        }
    }

    /// Creates intra neighbor (no MV).
    #[must_use]
    pub const fn intra() -> Self {
        Self {
            mv: MotionVector::zero(),
            ref_idx: -1,
            available: true,
            is_inter: false,
        }
    }
}

/// Context for motion vector prediction.
#[derive(Clone, Debug, Default)]
pub struct MvPredContext {
    /// Left neighbor.
    pub left: NeighborInfo,
    /// Top neighbor.
    pub top: NeighborInfo,
    /// Top-right neighbor.
    pub top_right: NeighborInfo,
    /// Top-left neighbor.
    pub top_left: NeighborInfo,
    /// Co-located block in reference.
    pub co_located: NeighborInfo,
    /// Current reference frame index.
    pub ref_idx: i8,
    /// Block position (x in 4x4 units).
    pub mi_col: usize,
    /// Block position (y in 4x4 units).
    pub mi_row: usize,
    /// Block size.
    pub block_size: BlockSize,
}

impl MvPredContext {
    /// Creates a new prediction context.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            left: NeighborInfo::unavailable(),
            top: NeighborInfo::unavailable(),
            top_right: NeighborInfo::unavailable(),
            top_left: NeighborInfo::unavailable(),
            co_located: NeighborInfo::unavailable(),
            ref_idx: 0,
            mi_col: 0,
            mi_row: 0,
            block_size: BlockSize::Block8x8,
        }
    }

    /// Sets the block position.
    #[must_use]
    pub const fn at_position(mut self, mi_row: usize, mi_col: usize) -> Self {
        self.mi_row = mi_row;
        self.mi_col = mi_col;
        self
    }

    /// Sets the block size.
    #[must_use]
    pub const fn with_size(mut self, size: BlockSize) -> Self {
        self.block_size = size;
        self
    }

    /// Sets the reference frame index.
    #[must_use]
    pub const fn with_ref(mut self, ref_idx: i8) -> Self {
        self.ref_idx = ref_idx;
        self
    }

    /// Sets the left neighbor.
    #[must_use]
    pub const fn with_left(mut self, info: NeighborInfo) -> Self {
        self.left = info;
        self
    }

    /// Sets the top neighbor.
    #[must_use]
    pub const fn with_top(mut self, info: NeighborInfo) -> Self {
        self.top = info;
        self
    }

    /// Sets the top-right neighbor.
    #[must_use]
    pub const fn with_top_right(mut self, info: NeighborInfo) -> Self {
        self.top_right = info;
        self
    }

    /// Sets the top-left neighbor.
    #[must_use]
    pub const fn with_top_left(mut self, info: NeighborInfo) -> Self {
        self.top_left = info;
        self
    }

    /// Sets the co-located block.
    #[must_use]
    pub const fn with_co_located(mut self, info: NeighborInfo) -> Self {
        self.co_located = info;
        self
    }
}

/// MV predictor candidate.
#[derive(Clone, Copy, Debug)]
pub struct MvCandidate {
    /// Predicted motion vector.
    pub mv: MotionVector,
    /// Weight/priority of this candidate.
    pub weight: u32,
    /// Source position.
    pub source: NeighborPosition,
}

impl MvCandidate {
    /// Creates a new MV candidate.
    #[must_use]
    pub const fn new(mv: MotionVector, weight: u32, source: NeighborPosition) -> Self {
        Self { mv, weight, source }
    }

    /// Creates a zero MV candidate.
    #[must_use]
    pub const fn zero() -> Self {
        Self {
            mv: MotionVector::zero(),
            weight: 0,
            source: NeighborPosition::Median,
        }
    }
}

/// Motion vector predictor list.
#[derive(Clone, Debug)]
pub struct MvPredictorList {
    /// Candidate predictors.
    candidates: [MvCandidate; MAX_PREDICTORS],
    /// Number of valid candidates.
    count: usize,
}

impl Default for MvPredictorList {
    fn default() -> Self {
        Self::new()
    }
}

impl MvPredictorList {
    /// Creates a new empty predictor list.
    #[must_use]
    pub fn new() -> Self {
        Self {
            candidates: [MvCandidate::zero(); MAX_PREDICTORS],
            count: 0,
        }
    }

    /// Adds a candidate to the list.
    pub fn add(&mut self, candidate: MvCandidate) {
        if self.count < MAX_PREDICTORS {
            // Check for duplicates
            for i in 0..self.count {
                if self.candidates[i].mv == candidate.mv {
                    // Update weight if higher
                    if candidate.weight > self.candidates[i].weight {
                        self.candidates[i].weight = candidate.weight;
                    }
                    return;
                }
            }
            self.candidates[self.count] = candidate;
            self.count += 1;
        }
    }

    /// Adds a candidate from neighbor info.
    pub fn add_from_neighbor(&mut self, info: &NeighborInfo, source: NeighborPosition) {
        if info.available && info.is_inter {
            let weight = match source {
                NeighborPosition::Left | NeighborPosition::Top | NeighborPosition::TopRight => {
                    SPATIAL_WEIGHT
                }
                NeighborPosition::CoLocated => TEMPORAL_WEIGHT,
                _ => 1,
            };
            self.add(MvCandidate::new(info.mv, weight, source));
        }
    }

    /// Sorts candidates by weight (descending).
    pub fn sort_by_weight(&mut self) {
        // Simple insertion sort for small array
        for i in 1..self.count {
            let key = self.candidates[i];
            let mut j = i;
            while j > 0 && self.candidates[j - 1].weight < key.weight {
                self.candidates[j] = self.candidates[j - 1];
                j -= 1;
            }
            self.candidates[j] = key;
        }
    }

    /// Returns the number of candidates.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.count
    }

    /// Returns true if empty.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.count == 0
    }

    /// Gets a candidate by index.
    #[must_use]
    pub const fn get(&self, index: usize) -> Option<&MvCandidate> {
        if index < self.count {
            Some(&self.candidates[index])
        } else {
            None
        }
    }

    /// Returns the best (first) predictor.
    #[must_use]
    pub fn best(&self) -> MotionVector {
        if self.count > 0 {
            self.candidates[0].mv
        } else {
            MotionVector::zero()
        }
    }

    /// Returns all predictors as a slice.
    #[must_use]
    pub fn as_slice(&self) -> &[MvCandidate] {
        &self.candidates[..self.count]
    }

    /// Extracts motion vectors only.
    pub fn motion_vectors(&self) -> Vec<MotionVector> {
        self.candidates[..self.count].iter().map(|c| c.mv).collect()
    }
}

/// Spatial MV predictor calculator.
#[derive(Clone, Copy, Debug, Default)]
pub struct SpatialPredictor;

impl SpatialPredictor {
    /// Creates a new spatial predictor.
    #[must_use]
    pub const fn new() -> Self {
        Self
    }

    /// Gets the left neighbor MV.
    #[must_use]
    pub fn get_left(ctx: &MvPredContext) -> Option<MotionVector> {
        if ctx.left.available && ctx.left.is_inter {
            Some(ctx.left.mv)
        } else {
            None
        }
    }

    /// Gets the top neighbor MV.
    #[must_use]
    pub fn get_top(ctx: &MvPredContext) -> Option<MotionVector> {
        if ctx.top.available && ctx.top.is_inter {
            Some(ctx.top.mv)
        } else {
            None
        }
    }

    /// Gets the top-right neighbor MV.
    #[must_use]
    pub fn get_top_right(ctx: &MvPredContext) -> Option<MotionVector> {
        if ctx.top_right.available && ctx.top_right.is_inter {
            Some(ctx.top_right.mv)
        } else {
            None
        }
    }

    /// Gets the top-left neighbor MV.
    #[must_use]
    pub fn get_top_left(ctx: &MvPredContext) -> Option<MotionVector> {
        if ctx.top_left.available && ctx.top_left.is_inter {
            Some(ctx.top_left.mv)
        } else {
            None
        }
    }

    /// Calculates the median predictor from three MVs.
    #[must_use]
    pub fn median(a: MotionVector, b: MotionVector, c: MotionVector) -> MotionVector {
        // Component-wise median
        let dx = Self::median_of_3(a.dx, b.dx, c.dx);
        let dy = Self::median_of_3(a.dy, b.dy, c.dy);
        MotionVector::new(dx, dy)
    }

    /// Median of three values.
    #[must_use]
    fn median_of_3(a: i32, b: i32, c: i32) -> i32 {
        a.max(b.min(c)).min(b.max(c))
    }

    /// Calculates the median MVP from spatial neighbors.
    #[must_use]
    pub fn calculate_median(ctx: &MvPredContext) -> MotionVector {
        let left = Self::get_left(ctx).unwrap_or_else(MotionVector::zero);
        let top = Self::get_top(ctx).unwrap_or_else(MotionVector::zero);
        let top_right = Self::get_top_right(ctx)
            .or_else(|| Self::get_top_left(ctx))
            .unwrap_or_else(MotionVector::zero);

        Self::median(left, top, top_right)
    }

    /// Builds spatial predictor list.
    pub fn build_predictors(ctx: &MvPredContext, list: &mut MvPredictorList) {
        list.add_from_neighbor(&ctx.left, NeighborPosition::Left);
        list.add_from_neighbor(&ctx.top, NeighborPosition::Top);
        list.add_from_neighbor(&ctx.top_right, NeighborPosition::TopRight);
        list.add_from_neighbor(&ctx.top_left, NeighborPosition::TopLeft);

        // Add median if we have multiple neighbors
        let mut neighbor_count = 0;
        if ctx.left.available && ctx.left.is_inter {
            neighbor_count += 1;
        }
        if ctx.top.available && ctx.top.is_inter {
            neighbor_count += 1;
        }
        if ctx.top_right.available && ctx.top_right.is_inter {
            neighbor_count += 1;
        }

        if neighbor_count >= 2 {
            let median = Self::calculate_median(ctx);
            list.add(MvCandidate::new(
                median,
                SPATIAL_WEIGHT + 1,
                NeighborPosition::Median,
            ));
        }
    }
}

/// Temporal MV predictor calculator.
#[derive(Clone, Copy, Debug, Default)]
pub struct TemporalPredictor;

impl TemporalPredictor {
    /// Creates a new temporal predictor.
    #[must_use]
    pub const fn new() -> Self {
        Self
    }

    /// Gets the co-located MV from reference frame.
    #[must_use]
    pub fn get_co_located(ctx: &MvPredContext) -> Option<MotionVector> {
        if ctx.co_located.available && ctx.co_located.is_inter {
            Some(ctx.co_located.mv)
        } else {
            None
        }
    }

    /// Scales MV for different temporal distances.
    ///
    /// If the co-located block references a frame at distance `src_dist`,
    /// and we want to predict for target at distance `dst_dist`,
    /// scale the MV proportionally.
    #[must_use]
    #[allow(clippy::cast_possible_truncation)]
    pub fn scale_mv(mv: MotionVector, src_dist: i32, dst_dist: i32) -> MotionVector {
        if src_dist == 0 || src_dist == dst_dist {
            return mv;
        }

        let scale_x = (i64::from(mv.dx) * i64::from(dst_dist)) / i64::from(src_dist);
        let scale_y = (i64::from(mv.dy) * i64::from(dst_dist)) / i64::from(src_dist);

        MotionVector::new(scale_x as i32, scale_y as i32)
    }

    /// Builds temporal predictor.
    pub fn build_predictors(ctx: &MvPredContext, list: &mut MvPredictorList) {
        list.add_from_neighbor(&ctx.co_located, NeighborPosition::CoLocated);
    }
}

/// Combined MV predictor that uses both spatial and temporal information.
#[derive(Clone, Debug, Default)]
pub struct MvPredictor {
    /// Spatial predictor.
    spatial: SpatialPredictor,
    /// Temporal predictor.
    temporal: TemporalPredictor,
    /// Predictor list.
    predictors: MvPredictorList,
}

impl MvPredictor {
    /// Creates a new MV predictor.
    #[must_use]
    pub fn new() -> Self {
        Self {
            spatial: SpatialPredictor::new(),
            temporal: TemporalPredictor::new(),
            predictors: MvPredictorList::new(),
        }
    }

    /// Builds all predictors from context.
    pub fn build(&mut self, ctx: &MvPredContext) {
        self.predictors = MvPredictorList::new();

        // Always add zero MV as fallback
        self.predictors.add(MvCandidate::new(
            MotionVector::zero(),
            1,
            NeighborPosition::Median,
        ));

        // Add spatial predictors
        SpatialPredictor::build_predictors(ctx, &mut self.predictors);

        // Add temporal predictors
        TemporalPredictor::build_predictors(ctx, &mut self.predictors);

        // Sort by weight
        self.predictors.sort_by_weight();
    }

    /// Returns the best MVP.
    #[must_use]
    pub fn best_mvp(&self) -> MotionVector {
        self.predictors.best()
    }

    /// Returns all predictors.
    #[must_use]
    pub fn all_predictors(&self) -> &MvPredictorList {
        &self.predictors
    }

    /// Returns predictors as motion vectors.
    pub fn motion_vectors(&self) -> Vec<MotionVector> {
        self.predictors.motion_vectors()
    }
}

/// MV cost calculator for rate-distortion optimization.
#[derive(Clone, Debug)]
pub struct MvCostCalculator {
    /// Lambda for RD tradeoff.
    lambda: f32,
    /// MV cost tables (optional precomputed).
    cost_table: Option<Vec<u32>>,
}

impl Default for MvCostCalculator {
    fn default() -> Self {
        Self::new(1.0)
    }
}

impl MvCostCalculator {
    /// Creates a new cost calculator.
    #[must_use]
    pub const fn new(lambda: f32) -> Self {
        Self {
            lambda,
            cost_table: None,
        }
    }

    /// Builds cost table for fast lookup.
    pub fn build_cost_table(&mut self, max_mv: i32) {
        let size = (2 * max_mv + 1) as usize;
        let mut table = vec![0u32; size];

        for i in 0..size {
            let mv_component = i as i32 - max_mv;
            table[i] = self.component_cost(mv_component);
        }

        self.cost_table = Some(table);
    }

    /// Calculates cost for a single MV component.
    #[must_use]
    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    fn component_cost(&self, value: i32) -> u32 {
        if value == 0 {
            return (self.lambda * 1.0) as u32;
        }

        let abs_val = value.unsigned_abs();
        // Approximate bit cost: log2(abs) * 2 + constant
        let log2 = 32 - abs_val.leading_zeros();
        let bits = log2 * 2 + 2;
        ((bits as f32) * self.lambda) as u32
    }

    /// Calculates the bit cost of an MV differential.
    #[must_use]
    pub fn cost(&self, mv: &MotionVector, mvp: &MotionVector) -> u32 {
        let diff = *mv - *mvp;
        self.component_cost(diff.dx) + self.component_cost(diff.dy)
    }

    /// Calculates full RD cost (distortion + rate).
    #[must_use]
    pub fn rd_cost(&self, mv: &MotionVector, mvp: &MotionVector, distortion: u32) -> u32 {
        distortion.saturating_add(self.cost(mv, mvp))
    }

    /// Creates an MvCost instance from this calculator.
    #[must_use]
    pub fn to_mv_cost(&self, mvp: MotionVector) -> MvCost {
        MvCost::with_ref_mv(self.lambda, mvp)
    }
}

/// MVP selection modes.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum MvpMode {
    /// Use spatial median.
    #[default]
    Median,
    /// Use left neighbor.
    Left,
    /// Use top neighbor.
    Top,
    /// Use co-located temporal.
    Temporal,
    /// Use zero MV.
    Zero,
}

impl MvpMode {
    /// Gets the MVP for this mode.
    #[must_use]
    pub fn get_mvp(&self, ctx: &MvPredContext) -> MotionVector {
        match self {
            Self::Median => SpatialPredictor::calculate_median(ctx),
            Self::Left => SpatialPredictor::get_left(ctx).unwrap_or_else(MotionVector::zero),
            Self::Top => SpatialPredictor::get_top(ctx).unwrap_or_else(MotionVector::zero),
            Self::Temporal => {
                TemporalPredictor::get_co_located(ctx).unwrap_or_else(MotionVector::zero)
            }
            Self::Zero => MotionVector::zero(),
        }
    }
}

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

    #[test]
    fn test_neighbor_info_unavailable() {
        let info = NeighborInfo::unavailable();
        assert!(!info.available);
        assert!(!info.is_inter);
    }

    #[test]
    fn test_neighbor_info_with_mv() {
        let mv = MotionVector::new(10, 20);
        let info = NeighborInfo::with_mv(mv, 0);
        assert!(info.available);
        assert!(info.is_inter);
        assert_eq!(info.mv.dx, 10);
        assert_eq!(info.mv.dy, 20);
    }

    #[test]
    fn test_mv_pred_context_builder() {
        let left = NeighborInfo::with_mv(MotionVector::new(5, 5), 0);
        let ctx = MvPredContext::new()
            .at_position(10, 20)
            .with_size(BlockSize::Block16x16)
            .with_ref(0)
            .with_left(left);

        assert_eq!(ctx.mi_row, 10);
        assert_eq!(ctx.mi_col, 20);
        assert_eq!(ctx.block_size, BlockSize::Block16x16);
        assert!(ctx.left.available);
    }

    #[test]
    fn test_mv_candidate() {
        let mv = MotionVector::new(10, 20);
        let candidate = MvCandidate::new(mv, 5, NeighborPosition::Left);

        assert_eq!(candidate.mv.dx, 10);
        assert_eq!(candidate.weight, 5);
        assert_eq!(candidate.source, NeighborPosition::Left);
    }

    #[test]
    fn test_mv_predictor_list_add() {
        let mut list = MvPredictorList::new();

        list.add(MvCandidate::new(
            MotionVector::new(10, 20),
            2,
            NeighborPosition::Left,
        ));
        list.add(MvCandidate::new(
            MotionVector::new(30, 40),
            3,
            NeighborPosition::Top,
        ));

        assert_eq!(list.len(), 2);
        assert_eq!(list.get(0).expect("get should return value").mv.dx, 10);
        assert_eq!(list.get(1).expect("get should return value").mv.dx, 30);
    }

    #[test]
    fn test_mv_predictor_list_dedup() {
        let mut list = MvPredictorList::new();

        list.add(MvCandidate::new(
            MotionVector::new(10, 20),
            2,
            NeighborPosition::Left,
        ));
        list.add(MvCandidate::new(
            MotionVector::new(10, 20),
            3,
            NeighborPosition::Top,
        ));

        // Should merge duplicates
        assert_eq!(list.len(), 1);
        assert_eq!(list.get(0).expect("get should return value").weight, 3); // Higher weight kept
    }

    #[test]
    fn test_mv_predictor_list_sort() {
        let mut list = MvPredictorList::new();

        list.add(MvCandidate::new(
            MotionVector::new(10, 20),
            1,
            NeighborPosition::Left,
        ));
        list.add(MvCandidate::new(
            MotionVector::new(30, 40),
            3,
            NeighborPosition::Top,
        ));
        list.add(MvCandidate::new(
            MotionVector::new(50, 60),
            2,
            NeighborPosition::TopRight,
        ));

        list.sort_by_weight();

        assert_eq!(list.get(0).expect("get should return value").weight, 3);
        assert_eq!(list.get(1).expect("get should return value").weight, 2);
        assert_eq!(list.get(2).expect("get should return value").weight, 1);
    }

    #[test]
    fn test_spatial_predictor_median() {
        let a = MotionVector::new(10, 30);
        let b = MotionVector::new(20, 20);
        let c = MotionVector::new(30, 10);

        let median = SpatialPredictor::median(a, b, c);

        assert_eq!(median.dx, 20); // Median of 10, 20, 30
        assert_eq!(median.dy, 20); // Median of 30, 20, 10
    }

    #[test]
    fn test_spatial_predictor_build() {
        let ctx = MvPredContext::new()
            .with_left(NeighborInfo::with_mv(MotionVector::new(10, 10), 0))
            .with_top(NeighborInfo::with_mv(MotionVector::new(20, 20), 0));

        let mut list = MvPredictorList::new();
        SpatialPredictor::build_predictors(&ctx, &mut list);

        assert!(list.len() >= 2);
    }

    #[test]
    fn test_temporal_predictor_scale() {
        let mv = MotionVector::new(100, 200);

        // Same distance - no change
        let same = TemporalPredictor::scale_mv(mv, 1, 1);
        assert_eq!(same.dx, 100);
        assert_eq!(same.dy, 200);

        // Double distance
        let doubled = TemporalPredictor::scale_mv(mv, 1, 2);
        assert_eq!(doubled.dx, 200);
        assert_eq!(doubled.dy, 400);

        // Half distance
        let halved = TemporalPredictor::scale_mv(mv, 2, 1);
        assert_eq!(halved.dx, 50);
        assert_eq!(halved.dy, 100);
    }

    #[test]
    fn test_mv_predictor_build() {
        let ctx = MvPredContext::new()
            .with_left(NeighborInfo::with_mv(MotionVector::new(10, 10), 0))
            .with_top(NeighborInfo::with_mv(MotionVector::new(20, 20), 0))
            .with_co_located(NeighborInfo::with_mv(MotionVector::new(15, 15), 0));

        let mut predictor = MvPredictor::new();
        predictor.build(&ctx);

        // Should have multiple predictors
        assert!(predictor.all_predictors().len() >= 3);

        // Best MVP should be calculated
        let mvp = predictor.best_mvp();
        assert!(mvp.dx != 0 || mvp.dy != 0 || predictor.all_predictors().len() == 1);
    }

    #[test]
    fn test_mv_cost_calculator() {
        let calc = MvCostCalculator::new(1.0);
        let mv = MotionVector::new(16, 16);
        let mvp = MotionVector::zero();

        let cost = calc.cost(&mv, &mvp);
        assert!(cost > 0);

        // Same MV as predictor should have lower cost
        let same_cost = calc.cost(&mvp, &mvp);
        assert!(same_cost < cost);
    }

    #[test]
    fn test_mv_cost_rd() {
        let calc = MvCostCalculator::new(1.0);
        let mv = MotionVector::new(16, 16);
        let mvp = MotionVector::zero();

        let rd = calc.rd_cost(&mv, &mvp, 100);
        assert!(rd > 100); // Should include MV cost
    }

    #[test]
    fn test_mvp_mode() {
        let ctx =
            MvPredContext::new().with_left(NeighborInfo::with_mv(MotionVector::new(10, 10), 0));

        assert_eq!(MvpMode::Left.get_mvp(&ctx).dx, 10);
        assert_eq!(MvpMode::Zero.get_mvp(&ctx).dx, 0);
    }

    #[test]
    fn test_motion_vectors_extraction() {
        let mut list = MvPredictorList::new();
        list.add(MvCandidate::new(
            MotionVector::new(10, 20),
            2,
            NeighborPosition::Left,
        ));
        list.add(MvCandidate::new(
            MotionVector::new(30, 40),
            1,
            NeighborPosition::Top,
        ));

        let mvs = list.motion_vectors();
        assert_eq!(mvs.len(), 2);
        assert_eq!(mvs[0].dx, 10);
        assert_eq!(mvs[1].dx, 30);
    }
}