oximedia-codec 0.1.4

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
//! Deblocking filter for reducing blocking artifacts.
//!
//! The deblocking filter is applied at block boundaries to reduce
//! artifacts caused by block-based transform coding. It uses adaptive
//! filtering based on boundary strength (bS) calculations.

#![forbid(unsafe_code)]
#![allow(clippy::unreadable_literal)]
#![allow(clippy::items_after_statements)]
#![allow(clippy::unnecessary_wraps)]
#![allow(clippy::struct_excessive_bools)]
#![allow(clippy::identity_op)]
#![allow(clippy::range_plus_one)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::useless_conversion)]
#![allow(clippy::redundant_closure_for_method_calls)]
#![allow(clippy::single_match_else)]
#![allow(dead_code)]
#![allow(clippy::doc_markdown)]
#![allow(clippy::unused_self)]
#![allow(clippy::trivially_copy_pass_by_ref)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::similar_names)]
#![allow(clippy::many_single_char_names)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::cast_lossless)]

use super::pipeline::FrameContext;
use super::{FrameBuffer, PlaneBuffer, ReconstructResult};

// =============================================================================
// Constants
// =============================================================================

/// Maximum boundary strength.
pub const MAX_BOUNDARY_STRENGTH: u8 = 4;

/// Minimum block size for deblocking.
pub const MIN_DEBLOCK_SIZE: usize = 4;

/// Filter tap count for normal filter.
pub const NORMAL_FILTER_TAPS: usize = 4;

/// Filter tap count for strong filter.
pub const STRONG_FILTER_TAPS: usize = 8;

// =============================================================================
// Filter Strength
// =============================================================================

/// Boundary strength (bS) for deblocking.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct FilterStrength {
    /// Boundary strength value (0-4).
    pub bs: u8,
    /// Alpha threshold parameter.
    pub alpha: u8,
    /// Beta threshold parameter.
    pub beta: u8,
    /// tc0 clipping parameter.
    pub tc0: u8,
}

impl FilterStrength {
    /// Create a new filter strength.
    #[must_use]
    pub const fn new(bs: u8) -> Self {
        Self {
            bs,
            alpha: 0,
            beta: 0,
            tc0: 0,
        }
    }

    /// Create from quantization parameter.
    #[must_use]
    pub fn from_qp(qp: u8, bs: u8) -> Self {
        let idx = qp.min(51) as usize;

        // Alpha table (indexed by QP)
        const ALPHA: [u8; 52] = [
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 5, 6, 7, 8, 9, 10, 12, 13, 15,
            17, 20, 22, 25, 28, 32, 36, 40, 45, 50, 56, 63, 71, 80, 90, 101, 113, 127, 144, 162,
            182, 203, 226, 255, 255,
        ];

        // Beta table (indexed by QP)
        const BETA: [u8; 52] = [
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 6, 6, 7,
            7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
        ];

        // tc0 table (indexed by QP and bS)
        const TC0: [[u8; 3]; 52] = [
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 0],
            [0, 0, 1],
            [0, 0, 1],
            [0, 0, 1],
            [0, 0, 1],
            [0, 1, 1],
            [0, 1, 1],
            [1, 1, 1],
            [1, 1, 1],
            [1, 1, 1],
            [1, 1, 1],
            [1, 1, 2],
            [1, 1, 2],
            [1, 1, 2],
            [1, 1, 2],
            [1, 2, 3],
            [1, 2, 3],
            [2, 2, 3],
            [2, 2, 4],
            [2, 3, 4],
            [2, 3, 4],
            [3, 3, 5],
            [3, 4, 6],
            [3, 4, 6],
            [4, 5, 7],
            [4, 5, 8],
            [4, 6, 9],
            [5, 7, 10],
            [6, 8, 11],
            [6, 8, 13],
            [7, 10, 14],
            [8, 11, 16],
            [9, 12, 18],
            [10, 13, 20],
            [11, 15, 23],
            [13, 17, 25],
        ];

        let tc0 = if bs > 0 && bs < 4 {
            TC0[idx][(bs - 1) as usize]
        } else {
            0
        };

        Self {
            bs,
            alpha: ALPHA[idx],
            beta: BETA[idx],
            tc0,
        }
    }

    /// Check if filtering should be applied.
    #[must_use]
    pub const fn should_filter(&self) -> bool {
        self.bs > 0
    }

    /// Check if this is a strong filter (bS = 4).
    #[must_use]
    pub const fn is_strong(&self) -> bool {
        self.bs >= 4
    }

    /// Get the tc parameter for filtering.
    #[must_use]
    pub const fn tc(&self) -> i16 {
        if self.bs >= 4 {
            0 // Not used for strong filter
        } else {
            self.tc0 as i16
        }
    }
}

// =============================================================================
// Deblock Parameters
// =============================================================================

/// Parameters for deblocking filter.
#[derive(Clone, Debug, Default)]
pub struct DeblockParams {
    /// Deblocking filter disabled.
    pub disable_deblock: bool,
    /// Alpha offset (slice level).
    pub alpha_offset: i8,
    /// Beta offset (slice level).
    pub beta_offset: i8,
    /// Quantization parameter.
    pub qp: u8,
}

impl DeblockParams {
    /// Create new deblock parameters.
    #[must_use]
    pub fn new(qp: u8) -> Self {
        Self {
            qp,
            ..Default::default()
        }
    }

    /// Set alpha offset.
    #[must_use]
    pub const fn with_alpha_offset(mut self, offset: i8) -> Self {
        self.alpha_offset = offset;
        self
    }

    /// Set beta offset.
    #[must_use]
    pub const fn with_beta_offset(mut self, offset: i8) -> Self {
        self.beta_offset = offset;
        self
    }

    /// Get effective QP for alpha calculation.
    #[must_use]
    pub fn effective_qp_alpha(&self) -> u8 {
        ((self.qp as i16) + i16::from(self.alpha_offset)).clamp(0, 51) as u8
    }

    /// Get effective QP for beta calculation.
    #[must_use]
    pub fn effective_qp_beta(&self) -> u8 {
        ((self.qp as i16) + i16::from(self.beta_offset)).clamp(0, 51) as u8
    }

    /// Create filter strength for an edge.
    #[must_use]
    pub fn create_strength(&self, bs: u8) -> FilterStrength {
        FilterStrength::from_qp(self.qp, bs)
    }
}

// =============================================================================
// Boundary Strength Calculator
// =============================================================================

/// Block information for boundary strength calculation.
#[derive(Clone, Copy, Debug, Default)]
pub struct BlockInfo {
    /// Block is intra-coded.
    pub is_intra: bool,
    /// Has non-zero coefficients.
    pub has_coeffs: bool,
    /// Reference frame index.
    pub ref_frame: u8,
    /// Motion vector x component (in quarter-pel).
    pub mv_x: i16,
    /// Motion vector y component (in quarter-pel).
    pub mv_y: i16,
}

impl BlockInfo {
    /// Create info for an intra block.
    #[must_use]
    pub const fn intra() -> Self {
        Self {
            is_intra: true,
            has_coeffs: true,
            ref_frame: 0,
            mv_x: 0,
            mv_y: 0,
        }
    }

    /// Create info for an inter block.
    #[must_use]
    pub const fn inter(ref_frame: u8, mv_x: i16, mv_y: i16, has_coeffs: bool) -> Self {
        Self {
            is_intra: false,
            has_coeffs,
            ref_frame,
            mv_x,
            mv_y,
        }
    }
}

/// Calculate boundary strength between two adjacent blocks.
#[must_use]
pub fn calculate_boundary_strength(p: &BlockInfo, q: &BlockInfo) -> u8 {
    // If either block is intra, bS = 4
    if p.is_intra || q.is_intra {
        return 4;
    }

    // If either block has non-zero coefficients, bS = 2
    if p.has_coeffs || q.has_coeffs {
        return 2;
    }

    // Check reference frame difference
    if p.ref_frame != q.ref_frame {
        return 1;
    }

    // Check motion vector difference (threshold is 4 quarter-pels = 1 full pel)
    if (p.mv_x - q.mv_x).abs() >= 4 || (p.mv_y - q.mv_y).abs() >= 4 {
        return 1;
    }

    // No filtering needed
    0
}

// =============================================================================
// Deblocking Filter
// =============================================================================

/// Deblocking filter implementation.
#[derive(Debug)]
pub struct DeblockFilter {
    /// Filter parameters.
    params: DeblockParams,
    /// Block size.
    block_size: usize,
}

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

impl DeblockFilter {
    /// Create a new deblocking filter.
    #[must_use]
    pub fn new() -> Self {
        Self {
            params: DeblockParams::default(),
            block_size: 8,
        }
    }

    /// Create with specific parameters.
    #[must_use]
    pub fn with_params(params: DeblockParams) -> Self {
        Self {
            params,
            block_size: 8,
        }
    }

    /// Set filter parameters.
    pub fn set_params(&mut self, params: DeblockParams) {
        self.params = params;
    }

    /// Get current parameters.
    #[must_use]
    pub fn params(&self) -> &DeblockParams {
        &self.params
    }

    /// Apply deblocking filter to a frame.
    ///
    /// # Errors
    ///
    /// Returns error if filtering fails.
    pub fn apply(
        &mut self,
        frame: &mut FrameBuffer,
        _context: &FrameContext,
    ) -> ReconstructResult<()> {
        if self.params.disable_deblock {
            return Ok(());
        }

        let bd = frame.bit_depth();

        // Filter Y plane
        self.filter_plane(frame.y_plane_mut(), bd, false)?;

        // Filter chroma planes (use default bS = 1 for simplicity)
        if let Some(u) = frame.u_plane_mut() {
            self.filter_plane(u, bd, true)?;
        }
        if let Some(v) = frame.v_plane_mut() {
            self.filter_plane(v, bd, true)?;
        }

        Ok(())
    }

    /// Filter a single plane.
    fn filter_plane(
        &self,
        plane: &mut PlaneBuffer,
        bd: u8,
        is_chroma: bool,
    ) -> ReconstructResult<()> {
        let width = plane.width() as usize;
        let height = plane.height() as usize;
        let block_size = if is_chroma { 4 } else { self.block_size };

        // Use a default boundary strength for demonstration
        // In a full implementation, this would come from block info
        let strength = self.params.create_strength(2);

        // Filter vertical edges
        for by in 0..(height / block_size) {
            for bx in 1..(width / block_size) {
                let x = (bx * block_size) as u32;
                let y = (by * block_size) as u32;
                self.filter_edge_vertical(plane, x, y, block_size, &strength, bd);
            }
        }

        // Filter horizontal edges
        for by in 1..(height / block_size) {
            for bx in 0..(width / block_size) {
                let x = (bx * block_size) as u32;
                let y = (by * block_size) as u32;
                self.filter_edge_horizontal(plane, x, y, block_size, &strength, bd);
            }
        }

        Ok(())
    }

    /// Filter a vertical edge.
    fn filter_edge_vertical(
        &self,
        plane: &mut PlaneBuffer,
        x: u32,
        y: u32,
        length: usize,
        strength: &FilterStrength,
        bd: u8,
    ) {
        if !strength.should_filter() {
            return;
        }

        for i in 0..length {
            let py = y + i as u32;

            // Get samples
            let p2 = plane.get(x.saturating_sub(3), py);
            let p1 = plane.get(x.saturating_sub(2), py);
            let p0 = plane.get(x.saturating_sub(1), py);
            let q0 = plane.get(x, py);
            let q1 = plane.get(x + 1, py);
            let q2 = plane.get(x + 2, py);

            // Check filter condition
            if !self.should_filter_edge(p0, p1, q0, q1, strength) {
                continue;
            }

            // Apply filter
            let (new_p0, new_p1, new_q0, new_q1) = if strength.is_strong() {
                self.strong_filter(p0, p1, p2, q0, q1, q2, bd)
            } else {
                self.normal_filter(p0, p1, q0, q1, strength, bd)
            };

            // Write back
            plane.set(x.saturating_sub(1), py, new_p0);
            plane.set(x.saturating_sub(2), py, new_p1);
            plane.set(x, py, new_q0);
            plane.set(x + 1, py, new_q1);
        }
    }

    /// Filter a horizontal edge.
    fn filter_edge_horizontal(
        &self,
        plane: &mut PlaneBuffer,
        x: u32,
        y: u32,
        length: usize,
        strength: &FilterStrength,
        bd: u8,
    ) {
        if !strength.should_filter() {
            return;
        }

        for i in 0..length {
            let px = x + i as u32;

            // Get samples
            let p2 = plane.get(px, y.saturating_sub(3));
            let p1 = plane.get(px, y.saturating_sub(2));
            let p0 = plane.get(px, y.saturating_sub(1));
            let q0 = plane.get(px, y);
            let q1 = plane.get(px, y + 1);
            let q2 = plane.get(px, y + 2);

            // Check filter condition
            if !self.should_filter_edge(p0, p1, q0, q1, strength) {
                continue;
            }

            // Apply filter
            let (new_p0, new_p1, new_q0, new_q1) = if strength.is_strong() {
                self.strong_filter(p0, p1, p2, q0, q1, q2, bd)
            } else {
                self.normal_filter(p0, p1, q0, q1, strength, bd)
            };

            // Write back
            plane.set(px, y.saturating_sub(1), new_p0);
            plane.set(px, y.saturating_sub(2), new_p1);
            plane.set(px, y, new_q0);
            plane.set(px, y + 1, new_q1);
        }
    }

    /// Check if edge should be filtered.
    fn should_filter_edge(
        &self,
        p0: i16,
        p1: i16,
        q0: i16,
        q1: i16,
        strength: &FilterStrength,
    ) -> bool {
        let alpha = i16::from(strength.alpha);
        let beta = i16::from(strength.beta);

        // Check thresholds
        (p0 - q0).abs() < alpha && (p1 - p0).abs() < beta && (q1 - q0).abs() < beta
    }

    /// Apply normal (4-tap) filter.
    fn normal_filter(
        &self,
        p0: i16,
        p1: i16,
        q0: i16,
        q1: i16,
        strength: &FilterStrength,
        bd: u8,
    ) -> (i16, i16, i16, i16) {
        let max_val = (1i16 << bd) - 1;
        let tc = strength.tc();

        // Compute delta
        let delta0 = ((((q0 - p0) << 2) + (p1 - q1) + 4) >> 3).clamp(-tc, tc);

        let new_p0 = (p0 + delta0).clamp(0, max_val);
        let new_q0 = (q0 - delta0).clamp(0, max_val);

        // P1 and Q1 filtering (only if tc0 > 0)
        let (new_p1, new_q1) = if tc > 0 {
            let delta_p1 = ((p2_avg(p0, p1) - p1 + delta0) >> 1).clamp(-tc, tc);
            let delta_q1 = ((p2_avg(q0, q1) - q1 - delta0) >> 1).clamp(-tc, tc);
            (
                (p1 + delta_p1).clamp(0, max_val),
                (q1 + delta_q1).clamp(0, max_val),
            )
        } else {
            (p1, q1)
        };

        (new_p0, new_p1, new_q0, new_q1)
    }

    /// Apply strong (8-tap) filter.
    fn strong_filter(
        &self,
        p0: i16,
        p1: i16,
        p2: i16,
        q0: i16,
        q1: i16,
        q2: i16,
        bd: u8,
    ) -> (i16, i16, i16, i16) {
        let max_val = (1i16 << bd) - 1;

        // Strong filter uses weighted average
        let new_p0 = ((p2 + 2 * p1 + 2 * p0 + 2 * q0 + q1 + 4) >> 3).clamp(0, max_val);
        let new_p1 = ((p2 + p1 + p0 + q0 + 2) >> 2).clamp(0, max_val);
        let new_q0 = ((p1 + 2 * p0 + 2 * q0 + 2 * q1 + q2 + 4) >> 3).clamp(0, max_val);
        let new_q1 = ((p0 + q0 + q1 + q2 + 2) >> 2).clamp(0, max_val);

        (new_p0, new_p1, new_q0, new_q1)
    }
}

/// Helper for p2 average calculation.
#[inline]
fn p2_avg(p0: i16, p1: i16) -> i16 {
    (p0 + p1 + 1) >> 1
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_filter_strength_new() {
        let strength = FilterStrength::new(2);
        assert_eq!(strength.bs, 2);
        assert!(strength.should_filter());
        assert!(!strength.is_strong());

        let strength_zero = FilterStrength::new(0);
        assert!(!strength_zero.should_filter());
    }

    #[test]
    fn test_filter_strength_from_qp() {
        let strength = FilterStrength::from_qp(26, 2);
        assert_eq!(strength.bs, 2);
        assert!(strength.alpha > 0);
        assert!(strength.beta > 0);

        let strength_strong = FilterStrength::from_qp(26, 4);
        assert!(strength_strong.is_strong());
    }

    #[test]
    fn test_deblock_params() {
        let params = DeblockParams::new(26)
            .with_alpha_offset(2)
            .with_beta_offset(-2);

        assert_eq!(params.qp, 26);
        assert_eq!(params.effective_qp_alpha(), 28);
        assert_eq!(params.effective_qp_beta(), 24);
    }

    #[test]
    fn test_block_info() {
        let intra = BlockInfo::intra();
        assert!(intra.is_intra);

        let inter = BlockInfo::inter(1, 10, 20, true);
        assert!(!inter.is_intra);
        assert_eq!(inter.ref_frame, 1);
        assert_eq!(inter.mv_x, 10);
    }

    #[test]
    fn test_boundary_strength_calculation() {
        let intra = BlockInfo::intra();
        let inter = BlockInfo::inter(0, 0, 0, false);

        // Intra edge
        assert_eq!(calculate_boundary_strength(&intra, &inter), 4);

        // Two inter blocks with different refs
        let inter2 = BlockInfo::inter(1, 0, 0, false);
        assert_eq!(calculate_boundary_strength(&inter, &inter2), 1);

        // Two inter blocks with same ref but different MV
        let inter3 = BlockInfo::inter(0, 10, 0, false);
        assert_eq!(calculate_boundary_strength(&inter, &inter3), 1);

        // Two identical inter blocks
        assert_eq!(calculate_boundary_strength(&inter, &inter), 0);
    }

    #[test]
    fn test_boundary_strength_with_coeffs() {
        let inter_coeffs = BlockInfo::inter(0, 0, 0, true);
        let inter_no_coeffs = BlockInfo::inter(0, 0, 0, false);

        assert_eq!(
            calculate_boundary_strength(&inter_coeffs, &inter_no_coeffs),
            2
        );
    }

    #[test]
    fn test_deblock_filter_creation() {
        let filter = DeblockFilter::new();
        assert!(!filter.params().disable_deblock);
    }

    #[test]
    fn test_deblock_filter_with_params() {
        let params = DeblockParams::new(26);
        let filter = DeblockFilter::with_params(params);
        assert_eq!(filter.params().qp, 26);
    }

    #[test]
    fn test_deblock_filter_apply_disabled() {
        let mut frame = FrameBuffer::new(64, 64, 8, ChromaSubsampling::Cs420);
        let context = FrameContext::new(64, 64);

        let mut params = DeblockParams::new(26);
        params.disable_deblock = true;
        let mut filter = DeblockFilter::with_params(params);

        let result = filter.apply(&mut frame, &context);
        assert!(result.is_ok());
    }

    #[test]
    fn test_deblock_filter_apply() {
        let mut frame = FrameBuffer::new(64, 64, 8, ChromaSubsampling::Cs420);

        // Create an artificial edge
        for y in 0..64 {
            for x in 0..8 {
                frame.y_plane_mut().set(x, y as u32, 100);
            }
            for x in 8..64 {
                frame.y_plane_mut().set(x as u32, y as u32, 150);
            }
        }

        let context = FrameContext::new(64, 64);
        let params = DeblockParams::new(26);
        let mut filter = DeblockFilter::with_params(params);

        let result = filter.apply(&mut frame, &context);
        assert!(result.is_ok());
    }

    #[test]
    fn test_strong_filter() {
        let filter = DeblockFilter::new();

        let (new_p0, _new_p1, new_q0, _new_q1) =
            filter.strong_filter(100, 95, 90, 150, 155, 160, 8);

        // After strong filtering, edge should be smoother
        assert!((new_p0 - new_q0).abs() < (100 - 150i16).abs());
        assert!(new_p0 >= 0 && new_p0 <= 255);
        assert!(new_q0 >= 0 && new_q0 <= 255);
    }

    #[test]
    fn test_normal_filter() {
        let filter = DeblockFilter::new();
        let strength = FilterStrength::from_qp(26, 2);

        let (new_p0, _new_p1, new_q0, _new_q1) =
            filter.normal_filter(100, 95, 150, 155, &strength, 8);

        // After normal filtering, values should be closer
        assert!((new_p0 - new_q0).abs() <= (100 - 150i16).abs());
    }

    #[test]
    fn test_constants() {
        assert_eq!(MAX_BOUNDARY_STRENGTH, 4);
        assert_eq!(MIN_DEBLOCK_SIZE, 4);
        assert_eq!(NORMAL_FILTER_TAPS, 4);
        assert_eq!(STRONG_FILTER_TAPS, 8);
    }
}