oximedia-core 0.1.3

Core types and traits 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
//! HDR (High Dynamic Range) metadata and conversion support.
//!
//! This module provides types and utilities for handling HDR video metadata,
//! including mastering display color volume (MDCV), content light level (CLL),
//! transfer characteristics, and color primaries.
//!
//! # Overview
//!
//! HDR video uses wider color gamuts and higher dynamic range than standard SDR
//! (Standard Dynamic Range) video. This module supports:
//!
//! - **Metadata Types:**
//!   - Mastering Display Color Volume (MDCV/SMPTE 2086)
//!   - Content Light Level (CLL) - `MaxCLL` and `MaxFALL`
//!   - HDR10+ dynamic metadata (structure only)
#![allow(clippy::match_same_arms)]
//!   - Dolby Vision metadata (read-only)
//!   - HLG (Hybrid Log-Gamma) parameters
//!
//! - **Transfer Functions:**
//!   - ST.2084 (PQ - Perceptual Quantizer) for HDR10
//!   - HLG (ARIB STD-B67) for broadcast HDR
//!   - BT.709 for SDR
//!   - BT.2020 (HDR container)
//!   - sRGB
//!
//! - **Color Primaries:**
//!   - BT.709 (Rec.709) - HD/SDR standard
//!   - BT.2020 (Rec.2020) - UHD/HDR standard
//!   - DCI-P3 - Digital cinema
//!   - Display P3 - Apple displays
//!   - Custom primaries
//!
//! # Example
//!
//! ```
//! use oximedia_core::hdr::{HdrMetadata, MasteringDisplayColorVolume, ContentLightLevel};
//! use oximedia_core::hdr::{TransferCharacteristic, ColorPrimaries};
//!
//! // Create HDR10 metadata
//! let mdcv = MasteringDisplayColorVolume {
//!     display_primaries: ColorPrimaries::BT2020.primaries(),
//!     white_point: ColorPrimaries::BT2020.white_point(),
//!     max_luminance: 1000.0,
//!     min_luminance: 0.005,
//! };
//!
//! let cll = ContentLightLevel {
//!     max_cll: 1000,
//!     max_fall: 400,
//! };
//!
//! let metadata = HdrMetadata {
//!     mdcv: Some(mdcv),
//!     cll: Some(cll),
//!     transfer: TransferCharacteristic::Pq,
//!     primaries: ColorPrimaries::BT2020,
//! };
//!
//! // Check if content is HDR
//! assert!(metadata.is_hdr());
//! ```

#![forbid(unsafe_code)]

pub mod convert;
pub mod metadata;
pub mod parser;
pub mod primaries;
pub mod transfer;

pub use convert::{
    ColorGamutMapper, GamutMappingMode, HdrToSdrConverter, PqToHlgConverter, ToneMappingMode,
};
pub use metadata::{
    ContentLightLevel, DolbyVisionMetadata, Hdr10PlusMetadata, HlgParameters,
    MasteringDisplayColorVolume,
};
pub use parser::{Av1ColorConfig, HevcSeiParser, MatroskaColorElements, Vp9ColorConfig};
pub use primaries::{ColorPrimaries, Primaries, WhitePoint};
pub use transfer::TransferCharacteristic;

/// HDR metadata container.
///
/// This structure aggregates all HDR-related metadata for a video stream,
/// including mastering display information, content light levels, and
/// color space parameters.
///
/// # Examples
///
/// ```
/// use oximedia_core::hdr::{HdrMetadata, TransferCharacteristic, ColorPrimaries};
///
/// let metadata = HdrMetadata::default();
/// assert!(!metadata.is_hdr());
///
/// let hdr10 = HdrMetadata::hdr10(1000.0, 0.005, 1000, 400);
/// assert!(hdr10.is_hdr());
/// ```
#[derive(Clone, Debug, Default, PartialEq)]
pub struct HdrMetadata {
    /// Mastering display color volume (SMPTE 2086).
    pub mdcv: Option<MasteringDisplayColorVolume>,
    /// Content light level information.
    pub cll: Option<ContentLightLevel>,
    /// HDR10+ dynamic metadata.
    pub hdr10_plus: Option<Hdr10PlusMetadata>,
    /// Dolby Vision metadata (read-only).
    pub dolby_vision: Option<DolbyVisionMetadata>,
    /// HLG parameters.
    pub hlg: Option<HlgParameters>,
    /// Transfer characteristic (EOTF).
    pub transfer: TransferCharacteristic,
    /// Color primaries.
    pub primaries: ColorPrimaries,
}

impl HdrMetadata {
    /// Creates a new HDR metadata container with default values.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::hdr::HdrMetadata;
    ///
    /// let metadata = HdrMetadata::new();
    /// assert!(!metadata.is_hdr());
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates HDR10 metadata with mastering display and content light level.
    ///
    /// # Arguments
    ///
    /// * `max_luminance` - Maximum display luminance in nits (cd/m²)
    /// * `min_luminance` - Minimum display luminance in nits (cd/m²)
    /// * `max_cll` - Maximum content light level in nits
    /// * `max_fall` - Maximum frame-average light level in nits
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::hdr::HdrMetadata;
    ///
    /// // Typical HDR10 content mastered at 1000 nits
    /// let metadata = HdrMetadata::hdr10(1000.0, 0.005, 1000, 400);
    /// assert!(metadata.is_hdr());
    /// assert!(metadata.is_hdr10());
    /// ```
    #[must_use]
    pub fn hdr10(max_luminance: f64, min_luminance: f64, max_cll: u16, max_fall: u16) -> Self {
        Self {
            mdcv: Some(MasteringDisplayColorVolume {
                display_primaries: ColorPrimaries::BT2020.primaries(),
                white_point: ColorPrimaries::BT2020.white_point(),
                max_luminance,
                min_luminance,
            }),
            cll: Some(ContentLightLevel { max_cll, max_fall }),
            hdr10_plus: None,
            dolby_vision: None,
            hlg: None,
            transfer: TransferCharacteristic::Pq,
            primaries: ColorPrimaries::BT2020,
        }
    }

    /// Creates HLG metadata.
    ///
    /// # Arguments
    ///
    /// * `application_version` - HLG application version
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::hdr::HdrMetadata;
    ///
    /// let metadata = HdrMetadata::hlg(0);
    /// assert!(metadata.is_hdr());
    /// assert!(metadata.is_hlg());
    /// ```
    #[must_use]
    pub fn hlg(application_version: u8) -> Self {
        Self {
            mdcv: None,
            cll: None,
            hdr10_plus: None,
            dolby_vision: None,
            hlg: Some(HlgParameters {
                application_version,
            }),
            transfer: TransferCharacteristic::Hlg,
            primaries: ColorPrimaries::BT2020,
        }
    }

    /// Returns true if this metadata represents HDR content.
    ///
    /// Content is considered HDR if it uses PQ or HLG transfer characteristics,
    /// or if it has mastering display metadata.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::hdr::{HdrMetadata, TransferCharacteristic};
    ///
    /// let mut metadata = HdrMetadata::new();
    /// assert!(!metadata.is_hdr());
    ///
    /// metadata.transfer = TransferCharacteristic::Pq;
    /// assert!(metadata.is_hdr());
    /// ```
    #[must_use]
    pub fn is_hdr(&self) -> bool {
        matches!(
            self.transfer,
            TransferCharacteristic::Pq | TransferCharacteristic::Hlg
        ) || self.mdcv.is_some()
    }

    /// Returns true if this is HDR10 content.
    ///
    /// HDR10 uses PQ transfer function and BT.2020 primaries.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::hdr::HdrMetadata;
    ///
    /// let metadata = HdrMetadata::hdr10(1000.0, 0.005, 1000, 400);
    /// assert!(metadata.is_hdr10());
    /// ```
    #[must_use]
    pub fn is_hdr10(&self) -> bool {
        self.transfer == TransferCharacteristic::Pq
            && self.primaries == ColorPrimaries::BT2020
            && self.mdcv.is_some()
    }

    /// Returns true if this is HDR10+ content.
    ///
    /// HDR10+ extends HDR10 with dynamic metadata.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::hdr::HdrMetadata;
    ///
    /// let mut metadata = HdrMetadata::hdr10(1000.0, 0.005, 1000, 400);
    /// assert!(!metadata.is_hdr10_plus());
    /// ```
    #[must_use]
    pub fn is_hdr10_plus(&self) -> bool {
        self.is_hdr10() && self.hdr10_plus.is_some()
    }

    /// Returns true if this is HLG content.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::hdr::HdrMetadata;
    ///
    /// let metadata = HdrMetadata::hlg(0);
    /// assert!(metadata.is_hlg());
    /// ```
    #[must_use]
    pub fn is_hlg(&self) -> bool {
        self.transfer == TransferCharacteristic::Hlg
    }

    /// Returns true if this is Dolby Vision content.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::hdr::HdrMetadata;
    ///
    /// let metadata = HdrMetadata::new();
    /// assert!(!metadata.is_dolby_vision());
    /// ```
    #[must_use]
    pub fn is_dolby_vision(&self) -> bool {
        self.dolby_vision.is_some()
    }

    /// Estimates the peak luminance of the content in nits.
    ///
    /// Returns the most appropriate luminance value from available metadata,
    /// falling back to standard defaults if no metadata is present.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::hdr::HdrMetadata;
    ///
    /// let metadata = HdrMetadata::hdr10(1000.0, 0.005, 1200, 400);
    /// assert!((metadata.estimate_peak_luminance() - 1200.0).abs() < 0.01);
    /// ```
    #[must_use]
    pub fn estimate_peak_luminance(&self) -> f64 {
        // Prefer CLL (actual content) over mastering display
        if let Some(cll) = &self.cll {
            f64::from(cll.max_cll)
        } else if let Some(mdcv) = &self.mdcv {
            mdcv.max_luminance
        } else {
            match self.transfer {
                TransferCharacteristic::Pq => 1000.0,  // HDR10 nominal
                TransferCharacteristic::Hlg => 1000.0, // HLG nominal
                _ => 100.0,                            // SDR nominal
            }
        }
    }

    /// Estimates the minimum luminance of the content in nits.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::hdr::HdrMetadata;
    ///
    /// let metadata = HdrMetadata::hdr10(1000.0, 0.005, 1000, 400);
    /// assert!((metadata.estimate_min_luminance() - 0.005).abs() < 0.0001);
    /// ```
    #[must_use]
    pub fn estimate_min_luminance(&self) -> f64 {
        if let Some(mdcv) = &self.mdcv {
            mdcv.min_luminance
        } else {
            match self.transfer {
                TransferCharacteristic::Pq | TransferCharacteristic::Hlg => 0.005,
                _ => 0.1,
            }
        }
    }

    /// Returns a human-readable description of the HDR format.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::hdr::HdrMetadata;
    ///
    /// let metadata = HdrMetadata::hdr10(1000.0, 0.005, 1000, 400);
    /// assert_eq!(metadata.format_name(), "HDR10");
    ///
    /// let hlg = HdrMetadata::hlg(0);
    /// assert_eq!(hlg.format_name(), "HLG");
    /// ```
    #[must_use]
    pub fn format_name(&self) -> &str {
        if self.is_dolby_vision() {
            "Dolby Vision"
        } else if self.is_hdr10_plus() {
            "HDR10+"
        } else if self.is_hdr10() {
            "HDR10"
        } else if self.is_hlg() {
            "HLG"
        } else if self.transfer == TransferCharacteristic::Pq {
            "PQ (ST.2084)"
        } else {
            "SDR"
        }
    }
}

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

    #[test]
    fn test_default_metadata() {
        let metadata = HdrMetadata::default();
        assert!(!metadata.is_hdr());
        assert!(!metadata.is_hdr10());
        assert!(!metadata.is_hlg());
    }

    #[test]
    fn test_hdr10_metadata() {
        let metadata = HdrMetadata::hdr10(1000.0, 0.005, 1000, 400);
        assert!(metadata.is_hdr());
        assert!(metadata.is_hdr10());
        assert!(!metadata.is_hdr10_plus());
        assert!(!metadata.is_hlg());
        assert_eq!(metadata.format_name(), "HDR10");
    }

    #[test]
    fn test_hlg_metadata() {
        let metadata = HdrMetadata::hlg(0);
        assert!(metadata.is_hdr());
        assert!(metadata.is_hlg());
        assert!(!metadata.is_hdr10());
        assert_eq!(metadata.format_name(), "HLG");
    }

    #[test]
    fn test_peak_luminance_estimation() {
        let metadata = HdrMetadata::hdr10(1000.0, 0.005, 1200, 400);
        assert!((metadata.estimate_peak_luminance() - 1200.0).abs() < 0.01);

        let metadata = HdrMetadata::new();
        assert!((metadata.estimate_peak_luminance() - 100.0).abs() < 0.01);
    }

    #[test]
    fn test_min_luminance_estimation() {
        let metadata = HdrMetadata::hdr10(1000.0, 0.005, 1000, 400);
        assert!((metadata.estimate_min_luminance() - 0.005).abs() < 0.0001);
    }
}