oximedia-codec 0.1.6

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
//! Core types for motion estimation.
//!
//! This module provides fundamental types used throughout the motion
//! estimation pipeline, including motion vectors, search ranges, and
//! block matching results.

#![forbid(unsafe_code)]
#![allow(dead_code)]
#![allow(clippy::similar_names)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::trivially_copy_pass_by_ref)]

use std::ops::{Add, Neg, Sub};

/// Maximum motion vector component magnitude (in sub-pixel units).
pub const MV_MAX: i32 = 16383 * 8; // 1/8 pel precision

/// Minimum motion vector component magnitude (in sub-pixel units).
pub const MV_MIN: i32 = -16384 * 8;

/// Default search range in pixels.
pub const DEFAULT_SEARCH_RANGE: i32 = 64;

/// Motion vector precision levels.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Hash)]
#[repr(u8)]
pub enum MvPrecision {
    /// Full pixel precision (integer pel).
    FullPel = 0,
    /// Half pixel precision (1/2 pel).
    HalfPel = 1,
    /// Quarter pixel precision (1/4 pel).
    #[default]
    QuarterPel = 2,
    /// Eighth pixel precision (1/8 pel).
    EighthPel = 3,
}

impl MvPrecision {
    /// Returns the number of fractional bits for this precision.
    #[must_use]
    pub const fn fractional_bits(self) -> u8 {
        match self {
            Self::FullPel => 0,
            Self::HalfPel => 1,
            Self::QuarterPel => 2,
            Self::EighthPel => 3,
        }
    }

    /// Returns the scale factor for sub-pixel units.
    #[must_use]
    pub const fn scale(self) -> i32 {
        1 << self.fractional_bits()
    }

    /// Returns the mask for extracting fractional part.
    #[must_use]
    pub const fn frac_mask(self) -> i32 {
        self.scale() - 1
    }

    /// Converts a value from this precision to another.
    #[must_use]
    pub const fn convert(self, value: i32, target: Self) -> i32 {
        let src_bits = self.fractional_bits() as i32;
        let dst_bits = target.fractional_bits() as i32;
        let shift = dst_bits - src_bits;
        if shift > 0 {
            value << shift
        } else {
            value >> (-shift)
        }
    }
}

/// A motion vector with sub-pixel precision.
///
/// Components are stored in 1/8 pixel (eighth-pel) precision internally.
/// This allows conversion to any lower precision without loss.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Hash)]
pub struct MotionVector {
    /// Horizontal displacement (dx) in 1/8 pixel units.
    pub dx: i32,
    /// Vertical displacement (dy) in 1/8 pixel units.
    pub dy: i32,
}

impl MotionVector {
    /// Creates a zero motion vector.
    #[must_use]
    pub const fn zero() -> Self {
        Self { dx: 0, dy: 0 }
    }

    /// Creates a motion vector with the given components (in 1/8 pel).
    #[must_use]
    pub const fn new(dx: i32, dy: i32) -> Self {
        Self { dx, dy }
    }

    /// Creates a motion vector from full-pixel coordinates.
    #[must_use]
    pub const fn from_full_pel(dx: i32, dy: i32) -> Self {
        Self {
            dx: dx << 3,
            dy: dy << 3,
        }
    }

    /// Creates a motion vector at the given precision.
    #[must_use]
    pub const fn from_precision(dx: i32, dy: i32, precision: MvPrecision) -> Self {
        let shift = 3 - precision.fractional_bits() as i32;
        Self {
            dx: dx << shift,
            dy: dy << shift,
        }
    }

    /// Returns true if this is a zero motion vector.
    #[must_use]
    pub const fn is_zero(&self) -> bool {
        self.dx == 0 && self.dy == 0
    }

    /// Returns the full-pixel horizontal component.
    #[must_use]
    pub const fn full_pel_x(&self) -> i32 {
        self.dx >> 3
    }

    /// Returns the full-pixel vertical component.
    #[must_use]
    pub const fn full_pel_y(&self) -> i32 {
        self.dy >> 3
    }

    /// Returns the fractional horizontal component (0-7).
    #[must_use]
    pub const fn frac_x(&self) -> i32 {
        self.dx & 7
    }

    /// Returns the fractional vertical component (0-7).
    #[must_use]
    pub const fn frac_y(&self) -> i32 {
        self.dy & 7
    }

    /// Returns the half-pel x component (0-1).
    #[must_use]
    pub const fn half_pel_x(&self) -> i32 {
        (self.dx >> 2) & 1
    }

    /// Returns the half-pel y component (0-1).
    #[must_use]
    pub const fn half_pel_y(&self) -> i32 {
        (self.dy >> 2) & 1
    }

    /// Returns the quarter-pel x component (0-3).
    #[must_use]
    pub const fn quarter_pel_x(&self) -> i32 {
        (self.dx >> 1) & 3
    }

    /// Returns the quarter-pel y component (0-3).
    #[must_use]
    pub const fn quarter_pel_y(&self) -> i32 {
        (self.dy >> 1) & 3
    }

    /// Converts to the specified precision (may lose fractional bits).
    #[must_use]
    pub const fn to_precision(&self, precision: MvPrecision) -> Self {
        let shift = 3 - precision.fractional_bits() as i32;
        let mask = !((1 << shift) - 1);
        Self {
            dx: self.dx & mask,
            dy: self.dy & mask,
        }
    }

    /// Rounds to the specified precision.
    #[must_use]
    pub const fn round_to_precision(&self, precision: MvPrecision) -> Self {
        let shift = 3 - precision.fractional_bits() as i32;
        let round = 1 << (shift - 1);
        if shift > 0 {
            Self {
                dx: ((self.dx + round) >> shift) << shift,
                dy: ((self.dy + round) >> shift) << shift,
            }
        } else {
            *self
        }
    }

    /// Clamps the motion vector to valid range.
    #[must_use]
    pub fn clamp(&self) -> Self {
        Self {
            dx: self.dx.clamp(MV_MIN, MV_MAX),
            dy: self.dy.clamp(MV_MIN, MV_MAX),
        }
    }

    /// Clamps to the specified search range (in full pixels).
    #[must_use]
    pub fn clamp_to_range(&self, range: &SearchRange) -> Self {
        Self {
            dx: self.dx.clamp(-range.horizontal << 3, range.horizontal << 3),
            dy: self.dy.clamp(-range.vertical << 3, range.vertical << 3),
        }
    }

    /// Returns the squared magnitude.
    #[must_use]
    pub const fn magnitude_squared(&self) -> i64 {
        (self.dx as i64) * (self.dx as i64) + (self.dy as i64) * (self.dy as i64)
    }

    /// Returns the L1 norm (Manhattan distance).
    #[must_use]
    pub const fn l1_norm(&self) -> i32 {
        self.dx.abs() + self.dy.abs()
    }

    /// Returns the L-infinity norm (Chebyshev distance).
    #[must_use]
    pub fn linf_norm(&self) -> i32 {
        self.dx.abs().max(self.dy.abs())
    }

    /// Scales the motion vector.
    #[must_use]
    #[allow(clippy::cast_possible_truncation)]
    pub fn scale(&self, num: i32, den: i32) -> Self {
        if den == 0 {
            return *self;
        }
        Self {
            dx: ((i64::from(self.dx) * i64::from(num)) / i64::from(den)) as i32,
            dy: ((i64::from(self.dy) * i64::from(num)) / i64::from(den)) as i32,
        }
    }
}

impl Add for MotionVector {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        Self {
            dx: self.dx.saturating_add(other.dx),
            dy: self.dy.saturating_add(other.dy),
        }
    }
}

impl Sub for MotionVector {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        Self {
            dx: self.dx.saturating_sub(other.dx),
            dy: self.dy.saturating_sub(other.dy),
        }
    }
}

impl Neg for MotionVector {
    type Output = Self;

    fn neg(self) -> Self {
        Self {
            dx: self.dx.saturating_neg(),
            dy: self.dy.saturating_neg(),
        }
    }
}

/// Search range for motion estimation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SearchRange {
    /// Horizontal search range in full pixels.
    pub horizontal: i32,
    /// Vertical search range in full pixels.
    pub vertical: i32,
}

impl Default for SearchRange {
    fn default() -> Self {
        Self::new(DEFAULT_SEARCH_RANGE, DEFAULT_SEARCH_RANGE)
    }
}

impl SearchRange {
    /// Creates a new search range.
    #[must_use]
    pub const fn new(horizontal: i32, vertical: i32) -> Self {
        Self {
            horizontal,
            vertical,
        }
    }

    /// Creates a symmetric search range.
    #[must_use]
    pub const fn symmetric(range: i32) -> Self {
        Self::new(range, range)
    }

    /// Returns the total number of search positions.
    #[must_use]
    pub const fn num_positions(&self) -> u64 {
        let w = (2 * self.horizontal + 1) as u64;
        let h = (2 * self.vertical + 1) as u64;
        w * h
    }

    /// Checks if a position is within the search range.
    #[must_use]
    pub const fn contains(&self, dx: i32, dy: i32) -> bool {
        dx >= -self.horizontal
            && dx <= self.horizontal
            && dy >= -self.vertical
            && dy <= self.vertical
    }

    /// Returns a scaled search range.
    #[must_use]
    pub const fn scale(&self, factor: i32) -> Self {
        Self {
            horizontal: self.horizontal * factor,
            vertical: self.vertical * factor,
        }
    }

    /// Returns a reduced search range (for refinement).
    #[must_use]
    pub const fn reduce(&self, factor: i32) -> Self {
        if factor == 0 {
            *self
        } else {
            Self {
                horizontal: self.horizontal / factor,
                vertical: self.vertical / factor,
            }
        }
    }
}

/// Result of a block matching operation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BlockMatch {
    /// Motion vector.
    pub mv: MotionVector,
    /// Sum of Absolute Differences (distortion).
    pub sad: u32,
    /// Rate-distortion cost (if computed).
    pub cost: u32,
}

impl Default for BlockMatch {
    fn default() -> Self {
        Self::worst()
    }
}

impl BlockMatch {
    /// Creates a new block match result.
    #[must_use]
    pub const fn new(mv: MotionVector, sad: u32, cost: u32) -> Self {
        Self { mv, sad, cost }
    }

    /// Creates a zero motion vector match.
    #[must_use]
    pub const fn zero_mv(sad: u32) -> Self {
        Self {
            mv: MotionVector::zero(),
            sad,
            cost: sad,
        }
    }

    /// Creates the worst possible match (for initialization).
    #[must_use]
    pub const fn worst() -> Self {
        Self {
            mv: MotionVector::zero(),
            sad: u32::MAX,
            cost: u32::MAX,
        }
    }

    /// Returns true if this match is better than another.
    #[must_use]
    pub const fn is_better_than(&self, other: &Self) -> bool {
        self.cost < other.cost
    }

    /// Updates with a better match if found.
    pub fn update_if_better(&mut self, other: &Self) {
        if other.is_better_than(self) {
            *self = *other;
        }
    }
}

/// Motion vector cost calculator for rate-distortion optimization.
#[derive(Clone, Copy, Debug)]
pub struct MvCost {
    /// Lambda for rate-distortion tradeoff.
    pub lambda: f32,
    /// Weight for MV bits.
    pub mv_weight: f32,
    /// Reference motion vector for differential coding.
    pub ref_mv: MotionVector,
}

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

impl MvCost {
    /// Creates a new MV cost calculator.
    #[must_use]
    pub const fn new(lambda: f32) -> Self {
        Self {
            lambda,
            mv_weight: 1.0,
            ref_mv: MotionVector::zero(),
        }
    }

    /// Creates with a reference motion vector.
    #[must_use]
    pub const fn with_ref_mv(lambda: f32, ref_mv: MotionVector) -> Self {
        Self {
            lambda,
            mv_weight: 1.0,
            ref_mv,
        }
    }

    /// Estimates the bit cost of a motion vector.
    #[must_use]
    pub fn estimate_bits(&self, mv: &MotionVector) -> f32 {
        let diff = *mv - self.ref_mv;
        let dx_bits = Self::component_bits(diff.dx);
        let dy_bits = Self::component_bits(diff.dy);
        (dx_bits + dy_bits) * self.mv_weight
    }

    /// Estimates bits for a single component.
    #[must_use]
    fn component_bits(value: i32) -> f32 {
        if value == 0 {
            return 1.0;
        }
        let abs_val = value.unsigned_abs();
        // Approximate: 2 * log2(abs) + constant overhead
        let log2_approx = 32 - abs_val.leading_zeros();
        (2 * log2_approx + 2) as f32
    }

    /// Calculates the rate-distortion cost.
    #[must_use]
    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    pub fn rd_cost(&self, mv: &MotionVector, sad: u32) -> u32 {
        let bits = self.estimate_bits(mv);
        let rate_cost = (bits * self.lambda) as u32;
        sad.saturating_add(rate_cost)
    }

    /// Updates the reference motion vector.
    pub fn set_ref_mv(&mut self, ref_mv: MotionVector) {
        self.ref_mv = ref_mv;
    }
}

/// Block size for motion estimation.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Hash)]
#[repr(u8)]
pub enum BlockSize {
    /// 4x4 block.
    Block4x4 = 0,
    /// 4x8 block.
    Block4x8 = 1,
    /// 8x4 block.
    Block8x4 = 2,
    /// 8x8 block.
    #[default]
    Block8x8 = 3,
    /// 8x16 block.
    Block8x16 = 4,
    /// 16x8 block.
    Block16x8 = 5,
    /// 16x16 block.
    Block16x16 = 6,
    /// 16x32 block.
    Block16x32 = 7,
    /// 32x16 block.
    Block32x16 = 8,
    /// 32x32 block.
    Block32x32 = 9,
    /// 32x64 block.
    Block32x64 = 10,
    /// 64x32 block.
    Block64x32 = 11,
    /// 64x64 block.
    Block64x64 = 12,
    /// 64x128 block.
    Block64x128 = 13,
    /// 128x64 block.
    Block128x64 = 14,
    /// 128x128 block.
    Block128x128 = 15,
}

impl BlockSize {
    /// Returns the width in pixels.
    #[must_use]
    pub const fn width(&self) -> usize {
        match self {
            Self::Block4x4 | Self::Block4x8 => 4,
            Self::Block8x4 | Self::Block8x8 | Self::Block8x16 => 8,
            Self::Block16x8 | Self::Block16x16 | Self::Block16x32 => 16,
            Self::Block32x16 | Self::Block32x32 | Self::Block32x64 => 32,
            Self::Block64x32 | Self::Block64x64 | Self::Block64x128 => 64,
            Self::Block128x64 | Self::Block128x128 => 128,
        }
    }

    /// Returns the height in pixels.
    #[must_use]
    pub const fn height(&self) -> usize {
        match self {
            Self::Block4x4 | Self::Block8x4 => 4,
            Self::Block4x8 | Self::Block8x8 | Self::Block16x8 => 8,
            Self::Block8x16 | Self::Block16x16 | Self::Block32x16 => 16,
            Self::Block16x32 | Self::Block32x32 | Self::Block64x32 => 32,
            Self::Block32x64 | Self::Block64x64 | Self::Block128x64 => 64,
            Self::Block64x128 | Self::Block128x128 => 128,
        }
    }

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

    /// Returns true if the block is square.
    #[must_use]
    pub const fn is_square(&self) -> bool {
        matches!(
            self,
            Self::Block4x4
                | Self::Block8x8
                | Self::Block16x16
                | Self::Block32x32
                | Self::Block64x64
                | Self::Block128x128
        )
    }

    /// Returns the log2 of width.
    #[must_use]
    pub const fn width_log2(&self) -> u8 {
        match self.width() {
            4 => 2,
            8 => 3,
            16 => 4,
            32 => 5,
            64 => 6,
            128 => 7,
            _ => 0,
        }
    }

    /// Returns the log2 of height.
    #[must_use]
    pub const fn height_log2(&self) -> u8 {
        match self.height() {
            4 => 2,
            8 => 3,
            16 => 4,
            32 => 5,
            64 => 6,
            128 => 7,
            _ => 0,
        }
    }
}

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

    #[test]
    fn test_mv_precision() {
        assert_eq!(MvPrecision::FullPel.fractional_bits(), 0);
        assert_eq!(MvPrecision::HalfPel.fractional_bits(), 1);
        assert_eq!(MvPrecision::QuarterPel.fractional_bits(), 2);
        assert_eq!(MvPrecision::EighthPel.fractional_bits(), 3);

        assert_eq!(MvPrecision::FullPel.scale(), 1);
        assert_eq!(MvPrecision::QuarterPel.scale(), 4);
        assert_eq!(MvPrecision::EighthPel.scale(), 8);
    }

    #[test]
    fn test_mv_precision_convert() {
        // Full pel to quarter pel
        assert_eq!(MvPrecision::FullPel.convert(2, MvPrecision::QuarterPel), 8);
        // Quarter pel to full pel
        assert_eq!(MvPrecision::QuarterPel.convert(8, MvPrecision::FullPel), 2);
    }

    #[test]
    fn test_motion_vector_creation() {
        let mv = MotionVector::new(16, -24);
        assert_eq!(mv.dx, 16);
        assert_eq!(mv.dy, -24);

        let mv_fp = MotionVector::from_full_pel(2, -3);
        assert_eq!(mv_fp.dx, 16);
        assert_eq!(mv_fp.dy, -24);
    }

    #[test]
    fn test_motion_vector_components() {
        let mv = MotionVector::new(27, -19); // 3.375, -2.375 in full pixels

        assert_eq!(mv.full_pel_x(), 3);
        assert_eq!(mv.full_pel_y(), -3); // -19 >> 3 = -3
        assert_eq!(mv.frac_x(), 3);
        assert_eq!(mv.frac_y(), -19 & 7);
    }

    #[test]
    fn test_motion_vector_zero() {
        let mv = MotionVector::zero();
        assert!(mv.is_zero());
        assert_eq!(mv.magnitude_squared(), 0);
    }

    #[test]
    fn test_motion_vector_arithmetic() {
        let mv1 = MotionVector::new(10, 20);
        let mv2 = MotionVector::new(5, -10);

        let sum = mv1 + mv2;
        assert_eq!(sum.dx, 15);
        assert_eq!(sum.dy, 10);

        let diff = mv1 - mv2;
        assert_eq!(diff.dx, 5);
        assert_eq!(diff.dy, 30);

        let neg = -mv1;
        assert_eq!(neg.dx, -10);
        assert_eq!(neg.dy, -20);
    }

    #[test]
    fn test_motion_vector_magnitude() {
        let mv = MotionVector::new(3, 4);
        assert_eq!(mv.magnitude_squared(), 25);
        assert_eq!(mv.l1_norm(), 7);
        assert_eq!(mv.linf_norm(), 4);
    }

    #[test]
    fn test_motion_vector_precision_conversion() {
        let mv = MotionVector::new(27, 19); // 3 + 3/8, 2 + 3/8

        let qpel = mv.to_precision(MvPrecision::QuarterPel);
        assert_eq!(qpel.dx & 1, 0); // Should be even
        assert_eq!(qpel.dy & 1, 0);

        let fpel = mv.to_precision(MvPrecision::FullPel);
        assert_eq!(fpel.dx & 7, 0); // Should be multiple of 8
        assert_eq!(fpel.dy & 7, 0);
    }

    #[test]
    fn test_search_range() {
        let range = SearchRange::symmetric(32);
        assert_eq!(range.horizontal, 32);
        assert_eq!(range.vertical, 32);

        assert!(range.contains(0, 0));
        assert!(range.contains(32, 32));
        assert!(range.contains(-32, -32));
        assert!(!range.contains(33, 0));
    }

    #[test]
    fn test_search_range_positions() {
        let range = SearchRange::symmetric(2);
        // (-2..2) x (-2..2) = 5 x 5 = 25 positions
        assert_eq!(range.num_positions(), 25);
    }

    #[test]
    fn test_block_match() {
        let best = BlockMatch::new(MotionVector::new(8, 16), 100, 120);
        let worst = BlockMatch::worst();

        assert!(best.is_better_than(&worst));
        assert!(!worst.is_better_than(&best));
    }

    #[test]
    fn test_block_match_update() {
        let mut current = BlockMatch::worst();
        let better = BlockMatch::new(MotionVector::new(8, 16), 100, 120);

        current.update_if_better(&better);
        assert_eq!(current.sad, 100);
    }

    #[test]
    fn test_mv_cost() {
        let cost = MvCost::new(1.0);
        let mv = MotionVector::new(16, 16);

        let bits = cost.estimate_bits(&mv);
        assert!(bits > 0.0);

        let rd = cost.rd_cost(&mv, 100);
        assert!(rd >= 100);
    }

    #[test]
    fn test_mv_cost_with_ref() {
        let ref_mv = MotionVector::new(16, 16);
        let cost = MvCost::with_ref_mv(1.0, ref_mv);

        // Same MV as reference should have low cost
        let same_bits = cost.estimate_bits(&ref_mv);

        // Different MV should have higher cost
        let diff_mv = MotionVector::new(32, 32);
        let diff_bits = cost.estimate_bits(&diff_mv);

        assert!(same_bits < diff_bits);
    }

    #[test]
    fn test_block_size() {
        assert_eq!(BlockSize::Block8x8.width(), 8);
        assert_eq!(BlockSize::Block8x8.height(), 8);
        assert_eq!(BlockSize::Block8x8.num_pixels(), 64);
        assert!(BlockSize::Block8x8.is_square());

        assert_eq!(BlockSize::Block16x8.width(), 16);
        assert_eq!(BlockSize::Block16x8.height(), 8);
        assert!(!BlockSize::Block16x8.is_square());
    }

    #[test]
    fn test_block_size_log2() {
        assert_eq!(BlockSize::Block4x4.width_log2(), 2);
        assert_eq!(BlockSize::Block8x8.width_log2(), 3);
        assert_eq!(BlockSize::Block16x16.width_log2(), 4);
        assert_eq!(BlockSize::Block64x64.width_log2(), 6);
    }
}