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
//! AV1 Bitstream Conformance Validation.
//!
//! This module provides validators for AV1 bitstream syntax and semantic
//! conformance as specified in the AV1 specification.
//!
//! # Validators
//!
//! - [`SequenceHeaderValidator`] — validates a parsed `SequenceHeader` against spec constraints
//! - [`ObuValidator`] — validates the syntactic structure of a complete AV1 bitstream
//!
//! # Example
//!
//! ```ignore
//! use oximedia_codec::av1::conformance::{ObuValidator, SequenceHeaderValidator};
//!
//! let result = SequenceHeaderValidator::validate(&header);
//! if !result.is_valid {
//!     for error in &result.errors {
//!         eprintln!("Conformance error: {error}");
//!     }
//! }
//! ```

use super::obu::{parse_obu, ObuType};
use super::sequence::SequenceHeader;

// =============================================================================
// ValidationResult
// =============================================================================

/// Result of an AV1 conformance validation pass.
#[derive(Clone, Debug, Default)]
pub struct ValidationResult {
    /// `true` if no errors were found (warnings are allowed).
    pub is_valid: bool,
    /// Hard constraint violations from the AV1 spec.
    pub errors: Vec<String>,
    /// Advisory issues that do not strictly violate the spec but may indicate
    /// non-conformant or unusual configurations.
    pub warnings: Vec<String>,
}

impl ValidationResult {
    /// Create a new, initially-valid result.
    #[must_use]
    fn new() -> Self {
        Self {
            is_valid: true,
            errors: Vec::new(),
            warnings: Vec::new(),
        }
    }

    /// Record a hard error and mark the result as invalid.
    fn error(&mut self, msg: impl Into<String>) {
        self.errors.push(msg.into());
        self.is_valid = false;
    }

    /// Record a warning (does not affect `is_valid`).
    fn warn(&mut self, msg: impl Into<String>) {
        self.warnings.push(msg.into());
    }
}

// =============================================================================
// SequenceHeaderValidator
// =============================================================================

/// Validates a parsed [`SequenceHeader`] against AV1 specification constraints.
///
/// Checks cover:
/// - `seq_profile` range (0–2)
/// - Bit depth validity (8, 10, or 12)
/// - Profile-specific chroma subsampling requirements
/// - `order_hint_bits` range and consistency with `enable_order_hint`
/// - `film_grain_params_present` against profile constraints
pub struct SequenceHeaderValidator;

impl SequenceHeaderValidator {
    /// Validate a [`SequenceHeader`] and return a [`ValidationResult`].
    ///
    /// This is a pure, side-effect-free function.
    #[must_use]
    pub fn validate(header: &SequenceHeader) -> ValidationResult {
        let mut result = ValidationResult::new();

        // ── Profile range ────────────────────────────────────────────────────
        if header.profile > 2 {
            result.error(format!(
                "seq_profile {} is out of range; must be 0, 1, or 2",
                header.profile
            ));
        }

        // ── Bit depth ────────────────────────────────────────────────────────
        let bd = header.color_config.bit_depth;
        if bd != 8 && bd != 10 && bd != 12 {
            result.error(format!("bit_depth {bd} is invalid; must be 8, 10, or 12"));
        }

        // ── Profile-specific color constraints ───────────────────────────────
        match header.profile {
            0 => {
                // Profile 0 (Main): must use 4:2:0 subsampling, no 12-bit
                if !header.color_config.subsampling_x || !header.color_config.subsampling_y {
                    result.error(
                        "profile 0 (Main) requires 4:2:0 subsampling \
                         (subsampling_x and subsampling_y must both be true)"
                            .to_string(),
                    );
                }
                if bd == 12 {
                    result.error("profile 0 (Main) does not allow 12-bit depth".to_string());
                }
            }
            1 => {
                // Profile 1 (High): must use 4:4:4, mono_chrome forbidden
                if header.color_config.mono_chrome {
                    result.error("profile 1 (High) forbids mono_chrome".to_string());
                }
                if header.color_config.subsampling_x || header.color_config.subsampling_y {
                    result.error(
                        "profile 1 (High) requires 4:4:4 \
                         (subsampling_x and subsampling_y must both be false)"
                            .to_string(),
                    );
                }
                if bd == 12 {
                    result.error("profile 1 (High) does not allow 12-bit depth".to_string());
                }
            }
            2 => {
                // Profile 2 (Professional): bit_depth must be 12 if twelve_bit flag is set;
                // we simply verify the stored bit_depth is one of the allowed values
                // (already checked above).  No additional subsampling restriction.
            }
            _ => {
                // Already caught above; no further checks needed.
            }
        }

        // ── order_hint_bits range & consistency ──────────────────────────────
        if header.order_hint_bits > 8 {
            result.error(format!(
                "order_hint_bits {} exceeds maximum of 8",
                header.order_hint_bits
            ));
        }
        if !header.enable_order_hint && header.order_hint_bits != 0 {
            result.warn(format!(
                "enable_order_hint is false but order_hint_bits is {}; \
                 expected 0 when order hints are disabled",
                header.order_hint_bits
            ));
        }

        // ── film_grain_params_present constraints ────────────────────────────
        // Film grain is only meaningful with luma + chroma planes (num_planes == 3).
        // Profile 1 mandates num_planes == 3 (YUV444); profile 0 and 2 may be mono.
        if header.film_grain_params_present && header.color_config.num_planes != 3 {
            result.warn(
                "film_grain_params_present is set but num_planes != 3; \
                 film grain synthesis requires luma and chroma planes"
                    .to_string(),
            );
        }

        // Profile 1 + mono_chrome is already an error above; guard here is
        // redundant but adds clarity for edge-case combinations.
        if header.film_grain_params_present
            && header.profile == 1
            && header.color_config.mono_chrome
        {
            result.warn(
                "film_grain_params_present with profile 1 and mono_chrome \
                 is contradictory (profile 1 forbids mono_chrome)"
                    .to_string(),
            );
        }

        result
    }
}

// =============================================================================
// ObuValidator
// =============================================================================

/// Validates the syntactic structure of an AV1 bitstream at the OBU level.
///
/// Checks include:
/// - Correct first OBU type
/// - `FrameHeader`/`Frame` OBU appearing before any `SequenceHeader`
/// - OBU size fields remaining within data bounds (surfaced from parse errors)
pub struct ObuValidator;

impl ObuValidator {
    /// Validate the OBU-level structure of `data` and return a [`ValidationResult`].
    ///
    /// The validator iterates all OBUs, accumulating errors and warnings
    /// without aborting on the first issue (except for a parse error that
    /// makes forward progress impossible).
    #[must_use]
    pub fn validate_bitstream(data: &[u8]) -> ValidationResult {
        let mut result = ValidationResult::new();

        if data.is_empty() {
            result.warn("bitstream is empty".to_string());
            return result;
        }

        let mut offset = 0usize;
        let mut first_obu = true;
        let mut seen_sequence_header = false;

        while offset < data.len() {
            let remaining = &data[offset..];

            match parse_obu(remaining) {
                Err(e) => {
                    result.error(format!("OBU parse error at byte offset {offset}: {e}"));
                    // Cannot continue safely; stop iteration.
                    break;
                }
                Ok((header, _payload, total_size)) => {
                    // ── First OBU check ──────────────────────────────────
                    if first_obu {
                        match header.obu_type {
                            ObuType::TemporalDelimiter | ObuType::SequenceHeader => {
                                // Conformant start
                            }
                            _ => {
                                result.warn(format!(
                                    "first OBU at offset 0 is {:?}; \
                                     expected TemporalDelimiter or SequenceHeader",
                                    header.obu_type
                                ));
                            }
                        }
                        first_obu = false;
                    }

                    // ── Track SequenceHeader presence ────────────────────
                    if matches!(header.obu_type, ObuType::SequenceHeader) {
                        seen_sequence_header = true;
                    }

                    // ── FrameHeader / Frame before SequenceHeader ─────────
                    if !seen_sequence_header {
                        match header.obu_type {
                            ObuType::FrameHeader
                            | ObuType::Frame
                            | ObuType::RedundantFrameHeader => {
                                result.error(format!(
                                    "{:?} OBU at byte offset {offset} appears \
                                     before any SequenceHeader",
                                    header.obu_type
                                ));
                            }
                            _ => {}
                        }
                    }

                    offset += total_size;
                }
            }
        }

        result
    }

    /// Count the number of successfully parseable OBUs in `data`.
    ///
    /// Stops counting at the first parse error.
    #[must_use]
    pub fn count_obus(data: &[u8]) -> usize {
        let mut count = 0usize;
        let mut offset = 0usize;

        while offset < data.len() {
            match parse_obu(&data[offset..]) {
                Err(_) => break,
                Ok((_header, _payload, total_size)) => {
                    count += 1;
                    offset += total_size;
                }
            }
        }

        count
    }

    /// Return the byte offset of the first `SequenceHeader` OBU in `data`,
    /// or `None` if no such OBU is found.
    #[must_use]
    pub fn find_sequence_header(data: &[u8]) -> Option<usize> {
        let mut offset = 0usize;

        while offset < data.len() {
            match parse_obu(&data[offset..]) {
                Err(_) => break,
                Ok((header, _payload, total_size)) => {
                    if matches!(header.obu_type, ObuType::SequenceHeader) {
                        return Some(offset);
                    }
                    offset += total_size;
                }
            }
        }

        None
    }
}

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

#[cfg(test)]
mod tests {
    use super::super::film_grain::{FilmGrainParams, ScalingPoint};
    use super::super::sequence::{ColorConfig, SequenceHeader};
    use super::{ObuValidator, SequenceHeaderValidator};

    fn valid_profile0_header() -> SequenceHeader {
        SequenceHeader {
            profile: 0,
            still_picture: false,
            reduced_still_picture_header: false,
            max_frame_width_minus_1: 1919,
            max_frame_height_minus_1: 1079,
            enable_order_hint: true,
            order_hint_bits: 7,
            enable_superres: false,
            enable_cdef: true,
            enable_restoration: true,
            color_config: ColorConfig {
                bit_depth: 8,
                mono_chrome: false,
                num_planes: 3,
                color_primaries: 1,
                transfer_characteristics: 1,
                matrix_coefficients: 1,
                color_range: false,
                subsampling_x: true,
                subsampling_y: true,
                separate_uv_delta_q: false,
            },
            film_grain_params_present: false,
        }
    }

    #[test]
    fn test_sequence_header_validator_valid_profile0() {
        let header = valid_profile0_header();
        let result = SequenceHeaderValidator::validate(&header);
        assert!(
            result.is_valid,
            "expected valid profile-0 header; errors: {:?}",
            result.errors
        );
        assert!(result.errors.is_empty());
    }

    #[test]
    fn test_sequence_header_validator_profile1_requires_444() {
        // Profile 1 requires subsampling_x == false && subsampling_y == false.
        // Using subsampling_x: true violates the 4:4:4 requirement.
        let header = SequenceHeader {
            profile: 1,
            color_config: ColorConfig {
                bit_depth: 8,
                mono_chrome: false,
                num_planes: 3,
                color_primaries: 1,
                transfer_characteristics: 1,
                matrix_coefficients: 1,
                color_range: false,
                subsampling_x: true, // wrong for profile 1
                subsampling_y: false,
                separate_uv_delta_q: false,
            },
            ..valid_profile0_header()
        };
        let result = SequenceHeaderValidator::validate(&header);
        assert!(
            !result.is_valid,
            "expected validation failure for profile 1 with 4:2:2 subsampling"
        );
        assert!(
            !result.errors.is_empty(),
            "expected at least one error for profile 1 subsampling violation"
        );
    }

    #[test]
    fn test_sequence_header_validator_invalid_bit_depth() {
        let header = SequenceHeader {
            profile: 0,
            color_config: ColorConfig {
                bit_depth: 7, // invalid
                ..valid_profile0_header().color_config
            },
            ..valid_profile0_header()
        };
        let result = SequenceHeaderValidator::validate(&header);
        assert!(!result.is_valid, "bit_depth=7 should be invalid");
        assert!(result.errors.iter().any(|e| e.contains("bit_depth")));
    }

    #[test]
    fn test_sequence_header_validator_order_hint_bits_too_large() {
        let header = SequenceHeader {
            enable_order_hint: true,
            order_hint_bits: 9, // exceeds max of 8
            ..valid_profile0_header()
        };
        let result = SequenceHeaderValidator::validate(&header);
        assert!(!result.is_valid, "order_hint_bits=9 should be invalid");
        assert!(result.errors.iter().any(|e| e.contains("order_hint_bits")));
    }

    #[test]
    fn test_sequence_header_validator_order_hint_disabled_nonzero_bits_warns() {
        let header = SequenceHeader {
            enable_order_hint: false,
            order_hint_bits: 4, // non-zero but order hint disabled
            ..valid_profile0_header()
        };
        let result = SequenceHeaderValidator::validate(&header);
        // Should still be valid (warning only)
        assert!(
            result.is_valid,
            "non-zero order_hint_bits with enable_order_hint=false should only warn"
        );
        assert!(
            !result.warnings.is_empty(),
            "expected a warning for inconsistent order_hint_bits"
        );
    }

    #[test]
    fn test_sequence_header_validator_profile0_rejects_12bit() {
        let header = SequenceHeader {
            profile: 0,
            color_config: ColorConfig {
                bit_depth: 12,
                ..valid_profile0_header().color_config
            },
            ..valid_profile0_header()
        };
        let result = SequenceHeaderValidator::validate(&header);
        assert!(!result.is_valid, "profile 0 should reject 12-bit depth");
    }

    #[test]
    fn test_obu_validator_count_obus_empty() {
        assert_eq!(ObuValidator::count_obus(&[]), 0);
    }

    #[test]
    fn test_obu_validator_find_sequence_header_none() {
        assert_eq!(ObuValidator::find_sequence_header(&[]), None);
    }

    #[test]
    fn test_obu_validator_minimal_valid_bitstream() {
        // TemporalDelimiter: type=2, no extension, has_size=true → byte = (2<<3)|(1<<1) = 0x12
        //                    followed by LEB128 size=0 → 0x00
        // SequenceHeader:    type=1, no extension, has_size=true → byte = (1<<3)|(1<<1) = 0x0A
        //                    followed by LEB128 size=0 → 0x00
        let bitstream: &[u8] = &[0x12, 0x00, 0x0A, 0x00];

        let count = ObuValidator::count_obus(bitstream);
        assert_eq!(count, 2, "expected 2 OBUs in minimal bitstream");

        let seq_offset = ObuValidator::find_sequence_header(bitstream);
        assert_eq!(
            seq_offset,
            Some(2),
            "SequenceHeader should be at byte offset 2"
        );
    }

    #[test]
    fn test_obu_validator_validate_bitstream_empty_warns() {
        let result = ObuValidator::validate_bitstream(&[]);
        // Empty bitstream → warning only, not an error
        assert!(
            result.is_valid,
            "empty bitstream should yield is_valid=true with warnings"
        );
        assert!(
            !result.warnings.is_empty(),
            "empty bitstream should produce at least one warning"
        );
    }

    #[test]
    fn test_obu_validator_validate_bitstream_minimal() {
        let bitstream: &[u8] = &[0x12, 0x00, 0x0A, 0x00];
        let result = ObuValidator::validate_bitstream(bitstream);
        assert!(
            result.is_valid,
            "minimal TD+SH bitstream should be valid; errors: {:?}",
            result.errors
        );
    }

    #[test]
    fn test_obu_validator_validate_bitstream_frame_before_sequence_header() {
        // Frame OBU (type=6) with has_size=true and payload size 0, no prior SequenceHeader.
        // header byte = (6<<3)|(1<<1) = 0x32
        let bitstream: &[u8] = &[0x32, 0x00];
        let result = ObuValidator::validate_bitstream(bitstream);
        assert!(
            !result.is_valid,
            "Frame OBU before SequenceHeader should produce an error"
        );
        assert!(
            result
                .errors
                .iter()
                .any(|e| e.contains("before any SequenceHeader")),
            "error should mention SequenceHeader ordering; got: {:?}",
            result.errors
        );
    }

    #[test]
    fn test_av1_film_grain_params_serialize_deserialize() {
        let mut params = FilmGrainParams::new();
        params.apply_grain = true;
        params.grain_seed = 0xDEAD;
        params.update_grain = true;
        params.film_grain_params_present = true;
        params.num_y_points = 2;
        params.y_points[0] = ScalingPoint::new(0, 64);
        params.y_points[1] = ScalingPoint::new(128, 128);

        // Clone acts as serialise→deserialise round-trip for in-memory representation.
        let cloned = params.clone();

        assert_eq!(params.grain_seed, cloned.grain_seed);
        assert_eq!(params.num_y_points, cloned.num_y_points);
        assert_eq!(params.y_points[0], cloned.y_points[0]);
        assert_eq!(params.y_points[1], cloned.y_points[1]);
        assert!(params.apply_grain);
        assert!(params.update_grain);
        assert!(params.film_grain_params_present);

        // Validate the params are internally consistent.
        assert!(
            params.validate(),
            "constructed FilmGrainParams should pass validate()"
        );
    }

    #[test]
    fn test_av1_film_grain_params_default_valid() {
        let params = FilmGrainParams::default();
        assert!(params.validate(), "default FilmGrainParams should be valid");
    }

    #[test]
    fn test_av1_film_grain_params_invalid_ar_coeff_lag() {
        let mut params = FilmGrainParams::new();
        params.ar_coeff_lag = 4; // max is MAX_AR_LAG=3
        assert!(!params.validate(), "ar_coeff_lag=4 should fail validate()");
    }
}