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
//! HDR format enumeration and passthrough configuration.
//!
//! This module provides a higher-level, format-centric API for HDR metadata
//! handling that complements the detailed [`super::hdr_passthrough`] module.
//!
//! Where `hdr_passthrough` operates at the level of individual transfer
//! functions and colour-primaries codes, this module exposes named HDR
//! *format* presets (HDR10, HDR10+, HLG, Dolby Vision) and a simple
//! configuration struct for selecting the passthrough behaviour.
use serde::{Deserialize, Serialize};
// ─── HdrFormat ────────────────────────────────────────────────────────────────
/// Named HDR format / signal type.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum HdrFormat {
/// HDR10: PQ/ST2084 transfer, BT.2020 primaries, static SMPTE ST 2086
/// mastering-display and CTA-861.3 MaxCLL/MaxFALL SEI.
Hdr10,
/// HDR10+: HDR10 baseline plus per-scene dynamic SMPTE ST 2094-40 SEI.
Hdr10Plus,
/// HLG BT.2100: Hybrid Log-Gamma, backward-compatible with SDR displays.
HlgBt2100,
/// Dolby Vision single or dual layer with RPU.
DolbyVision {
/// Dolby Vision profile number (4, 5, 7, 8, or 9).
profile: u8,
/// Dolby Vision level (1–13).
level: u8,
},
/// SDR — no HDR metadata.
None,
}
impl HdrFormat {
/// Short descriptive name for the format.
#[must_use]
pub fn name(&self) -> &str {
match self {
Self::Hdr10 => "HDR10",
Self::Hdr10Plus => "HDR10+",
Self::HlgBt2100 => "HLG BT.2100",
Self::DolbyVision { .. } => "Dolby Vision",
Self::None => "SDR",
}
}
/// Returns `true` when this format carries per-frame/per-scene dynamic
/// metadata (HDR10+ and Dolby Vision require this).
#[must_use]
pub fn requires_dynamic_metadata(&self) -> bool {
matches!(self, Self::Hdr10Plus | Self::DolbyVision { .. })
}
/// IANA / FFmpeg colour-primaries string for the format.
#[must_use]
pub fn color_primaries(&self) -> &str {
match self {
Self::Hdr10 | Self::Hdr10Plus | Self::HlgBt2100 | Self::DolbyVision { .. } => "bt2020",
Self::None => "bt709",
}
}
/// IANA / FFmpeg transfer-characteristics string for the format.
#[must_use]
pub fn transfer_characteristics(&self) -> &str {
match self {
Self::Hdr10 | Self::Hdr10Plus | Self::DolbyVision { .. } => "smpte2084",
Self::HlgBt2100 => "arib-std-b67",
Self::None => "bt709",
}
}
/// Returns `true` when this format is any HDR variant (non-SDR).
#[must_use]
pub fn is_hdr_format(&self) -> bool {
!matches!(self, Self::None)
}
}
// ─── MasterDisplayMetadata ────────────────────────────────────────────────────
/// SMPTE ST 2086 mastering-display colour-volume descriptor (simplified).
///
/// Chromaticity coordinates use the CIE xy system (range 0–1).
/// Luminance values are in candelas per square metre (cd/m²).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MasterDisplayMetadata {
/// Red primary CIE xy chromaticity.
pub primaries_r: (f32, f32),
/// Green primary CIE xy chromaticity.
pub primaries_g: (f32, f32),
/// Blue primary CIE xy chromaticity.
pub primaries_b: (f32, f32),
/// White point CIE xy chromaticity.
pub white_point: (f32, f32),
/// Maximum mastering-display luminance in cd/m².
pub max_luminance_nits: f32,
/// Minimum mastering-display luminance in cd/m².
pub min_luminance_nits: f32,
}
impl MasterDisplayMetadata {
/// Standard HDR10 mastering display — Rec.2020 primaries, D65 white
/// point, 1000 cd/m² peak (P3-D65 reference monitor approximation).
#[must_use]
pub fn rec2020_p3d65() -> Self {
Self {
primaries_r: (0.680, 0.320),
primaries_g: (0.265, 0.690),
primaries_b: (0.150, 0.060),
white_point: (0.3127, 0.3290),
max_luminance_nits: 1000.0,
min_luminance_nits: 0.005,
}
}
/// Validates this mastering-display descriptor.
///
/// # Errors
///
/// Returns an error string when:
/// - Any chromaticity value is outside `[0, 1]`.
/// - `min_luminance_nits >= max_luminance_nits`.
/// - `max_luminance_nits <= 0`.
pub fn validate(&self) -> Result<(), String> {
let check_chroma = |name: &str, (x, y): (f32, f32)| -> Result<(), String> {
if !(0.0..=1.0).contains(&x) {
return Err(format!("{name}.x={x} is outside [0, 1]"));
}
if !(0.0..=1.0).contains(&y) {
return Err(format!("{name}.y={y} is outside [0, 1]"));
}
Ok(())
};
check_chroma("primaries_r", self.primaries_r)?;
check_chroma("primaries_g", self.primaries_g)?;
check_chroma("primaries_b", self.primaries_b)?;
check_chroma("white_point", self.white_point)?;
if self.max_luminance_nits <= 0.0 {
return Err(format!(
"max_luminance_nits={} must be > 0",
self.max_luminance_nits
));
}
if self.min_luminance_nits < 0.0 || self.min_luminance_nits >= self.max_luminance_nits {
return Err(format!(
"min_luminance_nits={} must be in [0, {})",
self.min_luminance_nits, self.max_luminance_nits
));
}
Ok(())
}
}
// ─── HdrMetadataBundle ───────────────────────────────────────────────────────
/// Bundled HDR format descriptor and associated static metadata.
///
/// Use this together with [`HdrPassthroughConfig`] to describe the HDR
/// characteristics of a source stream and specify how they should be handled
/// during transcoding.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HdrMetadataBundle {
/// The HDR format / signal type.
pub format: HdrFormat,
/// Maximum Content Light Level (MaxCLL) in nits, if known.
pub max_cll_nits: Option<u16>,
/// Maximum Frame-Average Light Level (MaxFALL) in nits, if known.
pub max_fall_nits: Option<u16>,
/// SMPTE ST 2086 mastering-display descriptor, if available.
pub master_display: Option<MasterDisplayMetadata>,
}
impl HdrMetadataBundle {
/// Constructs an HDR10 bundle with MaxCLL and MaxFALL values.
#[must_use]
pub fn hdr10(max_cll: u16, max_fall: u16) -> Self {
Self {
format: HdrFormat::Hdr10,
max_cll_nits: Some(max_cll),
max_fall_nits: Some(max_fall),
master_display: None,
}
}
/// Constructs a minimal HLG bundle (no static metadata required).
#[must_use]
pub fn hlg() -> Self {
Self {
format: HdrFormat::HlgBt2100,
max_cll_nits: None,
max_fall_nits: None,
master_display: None,
}
}
/// Constructs an SDR bundle.
#[must_use]
pub fn sdr() -> Self {
Self {
format: HdrFormat::None,
max_cll_nits: None,
max_fall_nits: None,
master_display: None,
}
}
/// Returns `true` when the source format carries an HDR signal.
#[must_use]
pub fn is_hdr(&self) -> bool {
self.format.is_hdr_format()
}
}
// ─── HdrPassthroughConfig ────────────────────────────────────────────────────
/// Passthrough behaviour selection.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum HdrPassthroughMode {
/// Copy all HDR metadata from source to output unchanged.
Passthrough,
/// Convert the HDR signal to a different named format.
///
/// Pixel-level tone-mapping must be handled by the frame pipeline.
Convert(HdrFormat),
/// Tone-map the signal down to SDR (BT.709 / BT.1886).
ToSdr,
/// Strip all HDR metadata and signal a plain SDR output.
Strip,
}
/// Top-level configuration for HDR metadata handling in the transcode pipeline.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HdrPassthroughConfig {
/// How to handle HDR metadata.
pub mode: HdrPassthroughMode,
/// Format to use when the source is SDR but the mode is `Convert`.
pub fallback_format: HdrFormat,
}
impl HdrPassthroughConfig {
/// Pass HDR metadata through unchanged.
#[must_use]
pub fn passthrough() -> Self {
Self {
mode: HdrPassthroughMode::Passthrough,
fallback_format: HdrFormat::None,
}
}
/// Tone-map the output to SDR.
#[must_use]
pub fn to_sdr() -> Self {
Self {
mode: HdrPassthroughMode::ToSdr,
fallback_format: HdrFormat::None,
}
}
/// Convert HDR to the specified target format.
#[must_use]
pub fn convert_to(target: HdrFormat) -> Self {
Self {
mode: HdrPassthroughMode::Convert(target),
fallback_format: HdrFormat::None,
}
}
}
// ─── Compatibility check ──────────────────────────────────────────────────────
/// Returns `true` when `source` and `target` are the same HDR format and no
/// conversion is required.
///
/// Note: `DolbyVision` formats are only compatible when both the profile
/// *and* the level match.
#[must_use]
pub fn are_compatible(source: &HdrFormat, target: &HdrFormat) -> bool {
source == target
}
// ─── FFmpeg-style filter options ─────────────────────────────────────────────
/// Generates a list of FFmpeg-style key=value option pairs for the
/// `zscale` / `colorspace` filter graph that corresponds to the requested
/// passthrough configuration and source metadata.
///
/// The caller is responsible for wiring these into the filter graph string.
/// An empty vector is returned when no metadata manipulation is required
/// (i.e. [`HdrPassthroughMode::Passthrough`] mode with an SDR source).
#[must_use]
pub fn hdr_filter_options(
config: &HdrPassthroughConfig,
source_meta: &HdrMetadataBundle,
) -> Vec<(String, String)> {
match &config.mode {
HdrPassthroughMode::Passthrough => {
// Only inject flags for HDR sources to avoid touching SDR metadata.
if source_meta.is_hdr() {
vec![
(
"color_primaries".to_string(),
source_meta.format.color_primaries().to_string(),
),
(
"color_trc".to_string(),
source_meta.format.transfer_characteristics().to_string(),
),
(
"colorspace".to_string(),
source_meta.format.color_primaries().to_string(),
),
]
} else {
vec![]
}
}
HdrPassthroughMode::Strip => {
vec![
("color_primaries".to_string(), "bt709".to_string()),
("color_trc".to_string(), "bt709".to_string()),
("colorspace".to_string(), "bt709".to_string()),
]
}
HdrPassthroughMode::ToSdr => {
let src_trc = source_meta.format.transfer_characteristics().to_string();
vec![
("color_primaries".to_string(), "bt709".to_string()),
("color_trc".to_string(), "bt709".to_string()),
("colorspace".to_string(), "bt709".to_string()),
("transfer_in".to_string(), src_trc),
("transfer_out".to_string(), "bt709".to_string()),
]
}
HdrPassthroughMode::Convert(target) => {
let mut opts = vec![
(
"color_primaries".to_string(),
target.color_primaries().to_string(),
),
(
"color_trc".to_string(),
target.transfer_characteristics().to_string(),
),
(
"colorspace".to_string(),
target.color_primaries().to_string(),
),
(
"transfer_in".to_string(),
source_meta.format.transfer_characteristics().to_string(),
),
(
"transfer_out".to_string(),
target.transfer_characteristics().to_string(),
),
];
if let Some(cll) = source_meta.max_cll_nits {
opts.push(("max_cll".to_string(), cll.to_string()));
}
if let Some(fall) = source_meta.max_fall_nits {
opts.push(("max_fall".to_string(), fall.to_string()));
}
opts
}
}
}
// ─── Tests ────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hdr_format_names() {
assert_eq!(HdrFormat::Hdr10.name(), "HDR10");
assert_eq!(HdrFormat::Hdr10Plus.name(), "HDR10+");
assert_eq!(HdrFormat::HlgBt2100.name(), "HLG BT.2100");
assert_eq!(
HdrFormat::DolbyVision {
profile: 8,
level: 6
}
.name(),
"Dolby Vision"
);
assert_eq!(HdrFormat::None.name(), "SDR");
}
#[test]
fn test_requires_dynamic_metadata() {
assert!(!HdrFormat::Hdr10.requires_dynamic_metadata());
assert!(HdrFormat::Hdr10Plus.requires_dynamic_metadata());
assert!(!HdrFormat::HlgBt2100.requires_dynamic_metadata());
assert!(HdrFormat::DolbyVision {
profile: 5,
level: 1
}
.requires_dynamic_metadata());
assert!(!HdrFormat::None.requires_dynamic_metadata());
}
#[test]
fn test_hdr_metadata_bundle_is_hdr() {
assert!(HdrMetadataBundle::hdr10(1000, 400).is_hdr());
assert!(HdrMetadataBundle::hlg().is_hdr());
assert!(!HdrMetadataBundle::sdr().is_hdr());
}
#[test]
fn test_hdr10_constructor() {
let bundle = HdrMetadataBundle::hdr10(1000, 400);
assert_eq!(bundle.format, HdrFormat::Hdr10);
assert_eq!(bundle.max_cll_nits, Some(1000));
assert_eq!(bundle.max_fall_nits, Some(400));
}
#[test]
fn test_are_compatible_same_format_is_true() {
assert!(are_compatible(&HdrFormat::Hdr10, &HdrFormat::Hdr10));
assert!(are_compatible(&HdrFormat::None, &HdrFormat::None));
assert!(are_compatible(&HdrFormat::HlgBt2100, &HdrFormat::HlgBt2100));
}
#[test]
fn test_are_compatible_different_format_is_false() {
assert!(!are_compatible(&HdrFormat::Hdr10, &HdrFormat::HlgBt2100));
assert!(!are_compatible(&HdrFormat::Hdr10Plus, &HdrFormat::Hdr10));
assert!(!are_compatible(&HdrFormat::None, &HdrFormat::Hdr10));
}
#[test]
fn test_to_sdr_passthrough_mode_is_false() {
let config = HdrPassthroughConfig::to_sdr();
assert_ne!(config.mode, HdrPassthroughMode::Passthrough);
}
#[test]
fn test_hdr_filter_options_non_empty_for_convert_mode() {
let config = HdrPassthroughConfig::convert_to(HdrFormat::HlgBt2100);
let source = HdrMetadataBundle::hdr10(1000, 400);
let opts = hdr_filter_options(&config, &source);
assert!(!opts.is_empty(), "Convert mode must produce filter options");
// Verify transfer_in/transfer_out are present
let has_transfer_in = opts.iter().any(|(k, _)| k == "transfer_in");
let has_transfer_out = opts.iter().any(|(k, _)| k == "transfer_out");
assert!(has_transfer_in, "Expected transfer_in key");
assert!(has_transfer_out, "Expected transfer_out key");
}
#[test]
fn test_hdr_filter_options_empty_for_sdr_passthrough() {
let config = HdrPassthroughConfig::passthrough();
let source = HdrMetadataBundle::sdr();
let opts = hdr_filter_options(&config, &source);
assert!(
opts.is_empty(),
"SDR passthrough should produce no filter options"
);
}
#[test]
fn test_master_display_metadata_rec2020_p3d65_validates() {
let md = MasterDisplayMetadata::rec2020_p3d65();
assert!(md.validate().is_ok());
}
#[test]
fn test_master_display_metadata_bad_luminance_order() {
let mut md = MasterDisplayMetadata::rec2020_p3d65();
md.min_luminance_nits = md.max_luminance_nits + 1.0;
assert!(md.validate().is_err());
}
#[test]
fn test_master_display_metadata_bad_chromaticity() {
let mut md = MasterDisplayMetadata::rec2020_p3d65();
md.primaries_r = (1.5, 0.3); // x out of range
assert!(md.validate().is_err());
}
#[test]
fn test_color_primaries_and_trc_strings() {
assert_eq!(HdrFormat::Hdr10.color_primaries(), "bt2020");
assert_eq!(HdrFormat::Hdr10.transfer_characteristics(), "smpte2084");
assert_eq!(HdrFormat::HlgBt2100.transfer_characteristics(), "arib-std-b67");
assert_eq!(HdrFormat::None.color_primaries(), "bt709");
}
}