Skip to main content

hang/catalog/video/
color.rs

1use serde::{Deserialize, Serialize};
2
3/// The colour a rendition's pixels carry, so a renderer can set up its pipeline without reparsing the bitstream.
4///
5/// The first four fields are ITU-T H.273 (ISO/IEC 23091-2) code points, the numbering H.264 and
6/// H.265 VUI, AV1, VP9, and the ISOBMFF `colr` box all share. Absent means the source left it
7/// unspecified, never a signalled default.
8///
9/// Marked `#[non_exhaustive]`: start from [`Color::default`] and assign the fields you know.
10#[serde_with::serde_as]
11#[serde_with::skip_serializing_none]
12#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
13#[serde(default, rename_all = "camelCase")]
14#[non_exhaustive]
15pub struct Color {
16	/// The chromaticities of the source primaries, as H.273 `ColourPrimaries` (1 is BT.709, 9 is BT.2020).
17	#[serde_as(as = "crate::catalog::NotNull")]
18	pub primaries: Option<u8>,
19
20	/// The transfer function, as H.273 `TransferCharacteristics` (1 is BT.709, 16 is PQ, 18 is HLG).
21	#[serde_as(as = "crate::catalog::NotNull")]
22	pub transfer: Option<u8>,
23
24	/// The matrix between RGB and Y'CbCr, as H.273 `MatrixCoefficients` (0 is RGB, 1 is BT.709, 9 is BT.2020 NCL).
25	#[serde_as(as = "crate::catalog::NotNull")]
26	pub matrix: Option<u8>,
27
28	/// The numeric range of the samples.
29	#[serde_as(as = "crate::catalog::NotNull")]
30	pub range: Option<ColorRange>,
31
32	/// The display the content was mastered on, as SMPTE ST 2086.
33	#[serde_as(as = "crate::catalog::NotNull")]
34	pub mastering: Option<Mastering>,
35
36	/// The content's light levels, as CTA-861.3.
37	#[serde_as(as = "crate::catalog::NotNull")]
38	pub content_light: Option<ContentLight>,
39}
40
41/// The numeric range of the samples, as H.273's `VideoFullRangeFlag`.
42#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)]
43#[serde(rename_all = "lowercase")]
44pub enum ColorRange {
45	/// Studio swing: 8-bit luma spans 16..235 and chroma 16..240.
46	Limited,
47	/// Full swing: every 8-bit code is used.
48	Full,
49}
50
51/// The colour volume of a mastering display, as SMPTE ST 2086.
52#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
53#[serde(rename_all = "camelCase")]
54pub struct Mastering {
55	/// The red primary.
56	pub red: Chromaticity,
57	/// The green primary.
58	pub green: Chromaticity,
59	/// The blue primary.
60	pub blue: Chromaticity,
61	/// The white point.
62	pub white: Chromaticity,
63	/// The peak luminance in candelas per square metre.
64	pub max_luminance: f64,
65	/// The minimum luminance in candelas per square metre.
66	pub min_luminance: f64,
67}
68
69/// A CIE 1931 xy chromaticity coordinate.
70#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
71pub struct Chromaticity {
72	/// The x coordinate.
73	pub x: f64,
74	/// The y coordinate.
75	pub y: f64,
76}
77
78/// Content light levels in candelas per square metre, as CTA-861.3. Zero means unknown.
79#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
80#[serde(rename_all = "camelCase")]
81pub struct ContentLight {
82	/// The brightest pixel in the content (MaxCLL).
83	pub max_cll: u16,
84	/// The brightest frame average in the content (MaxFALL).
85	pub max_fall: u16,
86}
87
88#[cfg(test)]
89mod test {
90	use super::*;
91
92	#[test]
93	fn hdr10_round_trips() {
94		let color = Color {
95			primaries: Some(9),
96			transfer: Some(16),
97			matrix: Some(9),
98			range: Some(ColorRange::Limited),
99			mastering: Some(Mastering {
100				red: Chromaticity { x: 0.708, y: 0.292 },
101				green: Chromaticity { x: 0.17, y: 0.797 },
102				blue: Chromaticity { x: 0.131, y: 0.046 },
103				white: Chromaticity { x: 0.3127, y: 0.329 },
104				max_luminance: 1000.0,
105				min_luminance: 0.005,
106			}),
107			content_light: Some(ContentLight {
108				max_cll: 1000,
109				max_fall: 400,
110			}),
111		};
112
113		let encoded = serde_json::to_value(&color).unwrap();
114		assert_eq!(
115			encoded,
116			serde_json::json!({
117				"primaries": 9,
118				"transfer": 16,
119				"matrix": 9,
120				"range": "limited",
121				"mastering": {
122					"red": { "x": 0.708, "y": 0.292 },
123					"green": { "x": 0.17, "y": 0.797 },
124					"blue": { "x": 0.131, "y": 0.046 },
125					"white": { "x": 0.3127, "y": 0.329 },
126					"maxLuminance": 1000.0,
127					"minLuminance": 0.005,
128				},
129				"contentLight": { "maxCll": 1000, "maxFall": 400 },
130			})
131		);
132		assert_eq!(serde_json::from_value::<Color>(encoded).unwrap(), color);
133	}
134
135	#[test]
136	fn unspecified_fields_are_omitted() {
137		let color = Color {
138			matrix: Some(1),
139			..Default::default()
140		};
141		assert_eq!(
142			serde_json::to_value(&color).unwrap(),
143			serde_json::json!({ "matrix": 1 })
144		);
145	}
146}