forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB8dbd683Tahinli3 months ago856 commits

sei.rs150 lines, 5.9 KiBHistoryRawDownload
1//! SEI messages: mastering display, content light level, decoded picture hash.
2//!
3//! Three messages, and each one is here because something downstream reads it.
4//! The two HDR messages are what a tone map finds when the container lost its
5//! metadata (see [`ec_core::color::hevc_sei_light`], which reads exactly these
6//! payloads back). The decoded picture hash is the encoder's own oracle: an MD5
7//! of the reconstructed picture, which any conformant decoder must reproduce, so
8//! a mismatch is a *bit-exactness* failure report rather than a mystery.
9
10use crate::md5::Md5;
11use crate::nal::{NalHeader, NalUnitType};
12use crate::ps::rbsp_trailing_bits;
13use ec_core::bitio::BitWriter;
14use ec_core::color::ContentLight;
15
16/// `payloadType` values written here.
17const PAYLOAD_MASTERING_DISPLAY: u32 = 137;
18const PAYLOAD_CONTENT_LIGHT_LEVEL: u32 = 144;
19const PAYLOAD_DECODED_PICTURE_HASH: u32 = 132;
20
21/// Build one SEI RBSP holding `messages`, each a `(payloadType, payload)` pair.
22///
23/// The `ff` byte run for sizes over 254 is written the way the spec spells it,
24/// even though nothing here comes close: a hash message is 49 bytes.
25fn sei_rbsp(messages: &[(u32, Vec<u8>)]) -> Vec<u8> {
26 let mut w = BitWriter::with_capacity(64);
27 for (payload_type, payload) in messages {
28 let mut left = *payload_type;
29 while left >= 255 {
30 w.write_bits(0xff, 8);
31 left -= 255;
32 }
33 w.write_bits(left, 8);
34 let mut size = payload.len() as u32;
35 while size >= 255 {
36 w.write_bits(0xff, 8);
37 size -= 255;
38 }
39 w.write_bits(size, 8);
40 w.write_bytes(payload);
41 }
42 rbsp_trailing_bits(&mut w);
43 w.into_bytes()
44}
45
46/// The prefix SEI RBSP describing HDR mastering metadata, or `None` when there
47/// is nothing to say.
48///
49/// Both messages are written whole or not at all: a `mastering_display_colour_volume`
50/// with a zero luminance pair is what the oracle writes when it knows nothing, and a
51/// tone map that believes it maps the film to black.
52pub fn hdr_metadata_rbsp(light: ContentLight) -> Option<Vec<u8>> {
53 let mut messages = Vec::new();
54 if let (Some(max), Some(min)) = (light.mastering_max, light.mastering_min) {
55 let mut payload = Vec::with_capacity(24);
56 // The chromaticities are not modelled by this family (the tone map wants
57 // the luminance pair), so BT.2020's are written: they are what an HDR
58 // grade is approved on, and a decoder that reads them gets a true answer
59 // rather than zeros.
60 const BT2020_PRIMARIES: [(u16, u16); 3] = [(8500, 39_850), (6550, 2300), (35_400, 14_600)];
61 const D65: (u16, u16) = (15_635, 16_450);
62 for (x, y) in BT2020_PRIMARIES {
63 payload.extend_from_slice(&x.to_be_bytes());
64 payload.extend_from_slice(&y.to_be_bytes());
65 }
66 payload.extend_from_slice(&D65.0.to_be_bytes());
67 payload.extend_from_slice(&D65.1.to_be_bytes());
68 payload.extend_from_slice(&((max * 10_000.0) as u32).to_be_bytes());
69 payload.extend_from_slice(&((min * 10_000.0) as u32).to_be_bytes());
70 messages.push((PAYLOAD_MASTERING_DISPLAY, payload));
71 }
72 if let (Some(cll), Some(fall)) = (light.max_cll, light.max_fall) {
73 let mut payload = Vec::with_capacity(4);
74 payload.extend_from_slice(&(cll as u16).to_be_bytes());
75 payload.extend_from_slice(&(fall as u16).to_be_bytes());
76 messages.push((PAYLOAD_CONTENT_LIGHT_LEVEL, payload));
77 }
78 if messages.is_empty() {
79 None
80 } else {
81 Some(sei_rbsp(&messages))
82 }
83}
84
85/// The suffix SEI RBSP carrying one MD5 per colour plane (D.2.20).
86///
87/// `planes` are the *reconstructed* planes in coding order, each as
88/// `(samples, stride, width, height)` over the whole coded picture — the
89/// conformance window is not applied, because the hash is defined over
90/// `pic_width_in_luma_samples`.
91pub fn decoded_picture_hash_rbsp(planes: &[(&[u8], usize, usize, usize)]) -> Vec<u8> {
92 let mut payload = Vec::with_capacity(1 + 16 * planes.len());
93 payload.push(0); // hash_type = MD5
94 for &(data, stride, width, height) in planes {
95 let mut md5 = Md5::new();
96 for y in 0..height {
97 md5.update(&data[y * stride..y * stride + width]);
98 }
99 payload.extend_from_slice(&md5.finish());
100 }
101 sei_rbsp(&[(PAYLOAD_DECODED_PICTURE_HASH, payload)])
102}
103
104/// The NAL header a prefix SEI travels under.
105pub fn prefix_sei_header() -> NalHeader {
106 NalHeader::new(NalUnitType::PrefixSei)
107}
108
109/// The NAL header a suffix SEI (the picture hash) travels under.
110pub fn suffix_sei_header() -> NalHeader {
111 NalHeader::new(NalUnitType::SuffixSei)
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 #[test]
119 fn hdr_sei_reads_back_through_ec_core() {
120 let light = ContentLight {
121 max_cll: Some(1000.0),
122 max_fall: Some(400.0),
123 mastering_max: Some(1000.0),
124 mastering_min: Some(0.005),
125 };
126 let rbsp = hdr_metadata_rbsp(light).unwrap();
127 let mut annex_b = Vec::new();
128 crate::nal::write_annex_b(&mut annex_b, prefix_sei_header(), &rbsp, true);
129 // ec-core's reader is the consumer this exists for.
130 let read = ec_core::color::hevc_sei_light(&annex_b);
131 assert_eq!(read.max_cll, Some(1000.0));
132 assert_eq!(read.max_fall, Some(400.0));
133 assert_eq!(read.mastering_max, Some(1000.0));
134 assert_eq!(read.mastering_min, Some(0.005));
135 assert!(hdr_metadata_rbsp(ContentLight::default()).is_none());
136 }
137
138 #[test]
139 fn picture_hash_is_md5_of_each_plane() {
140 let luma = vec![7u8; 16 * 16];
141 let rbsp = decoded_picture_hash_rbsp(&[(&luma, 16, 16, 16)]);
142 // payloadType 132, payloadSize 17, hash_type 0, then the digest.
143 assert_eq!(rbsp[0], 132);
144 assert_eq!(rbsp[1], 17);
145 assert_eq!(rbsp[2], 0);
146 let mut md5 = Md5::new();
147 md5.update(&luma);
148 assert_eq!(&rbsp[3..19], &md5.finish()[..]);
149 }
150}