forge
Explore
Log in

Don't you dare go Hollow

A Colour element with no Range child states no range

Browse files at this commit

18d0fc185729f066311efa790ec63e0d6e892898Ahmet Kaan GümüşParent: db36daa
  • fix
    • a Colour element with no Range child states no range
Show full message
fix(matroska): a Colour element with no Range child states no range

Same class as ec-mp4 2a3816e, in the crate that commit named as carrying it.
`ColorInfo` has to answer `full_range` whether or not the file wrote a `Range`
child, so a `Colour` element without one -- which is most of them -- read as a
*declaration* of limited range. A host resolving colour in tiers then took that
fabricated claim as the container tier and shadowed a bitstream VUI that said
full: a full-range picture displayed as limited, which is a visibly washed-out
one.

`MatroskaDemuxer::color_tags(stream)` hands over what the element really stated
-- the Matroska `Range` coding straight into `Tags::from_codes`, where 0 is
"nothing said" -- and `VideoParameters::color` is unchanged for every file that
carries a `Range`. The unit test holds all three cases.
crates/ec-matroska/src/demux.rsmodified+92 -7
@@ -7,7 +7,7 @@ use std::ops::Range;
7 use ec_core::{7 use ec_core::{
8 AudioParameters, Buf, ChannelLayout, CodecId, CodecParameters, ColorInfo, ContentLight,8 AudioParameters, Buf, ChannelLayout, CodecId, CodecParameters, ColorInfo, ContentLight,
9 Demuxer, Error, MediaParameters, MediaType, Packet, PacketFlags, Result, Rounding, SeekMode,9 Demuxer, Error, MediaParameters, MediaType, Packet, PacketFlags, Result, Rounding, SeekMode,
10- StreamInfo, TimeBase, Timestamp, VideoParameters,10+ StreamInfo, TimeBase, Timestamp, VideoParameters, color::Tags,
11 };11 };
12 12
13 use crate::ebml::{self, Elements};13 use crate::ebml::{self, Elements};
@@ -82,6 +82,9 @@ struct Track {
82 codec_id: String,82 codec_id: String,
83 /// `TrackName` — "Japanese 5.1", "Signs" — empty where the file has none.83 /// `TrackName` — "Japanese 5.1", "Signs" — empty where the file has none.
84 name: String,84 name: String,
85+ /// What this track's `Colour` element *stated*, tier by tier — empty for a
86+ /// track that carries none and for one that is not a picture.
87+ color_tags: Tags,
85 /// What `TrackType` said, for the tracks with no `stream` of their own:88 /// What `TrackType` said, for the tracks with no `stream` of their own:
86 /// "MPEG-2 *video* nothing here decodes" is an answer, "no video track" is89 /// "MPEG-2 *video* nothing here decodes" is an answer, "no video track" is
87 /// not.90 /// not.
@@ -420,6 +423,21 @@ impl<R: Read + Seek> MatroskaDemuxer<R> {
420 .collect()423 .collect()
421 }424 }
422 425
426+ /// What `stream`'s `Colour` element **stated** about its colour, field by
427+ /// field: [`None`] wherever the element said nothing.
428+ ///
429+ /// Not the same answer as [`VideoParameters::color`], and the difference is
430+ /// the reason this exists. A [`ColorInfo`] has to fill in a `full_range`
431+ /// flag whether or not the file carried one, so a `Colour` element with no
432+ /// `Range` child — which is most of them — reads there as a *declaration*
433+ /// of limited range and shadows a bitstream VUI that said full: a
434+ /// full-range picture displayed as limited, which is a washed-out one.
435+ /// [`Tags`] can say nothing, so that is what an absent `Range` comes back
436+ /// as.
437+ pub fn color_tags(&self, stream: u32) -> Tags {
438+ self.track(stream).map_or(Tags::default(), |t| t.color_tags)
439+ }
440+
423 /// Whether this track's frames arrive as the encoder wrote them — no441 /// Whether this track's frames arrive as the encoder wrote them — no
424 /// `ContentEncodings` over them at all.442 /// `ContentEncodings` over them at all.
425 ///443 ///
@@ -1072,6 +1090,9 @@ fn parse_tracks(
1072 let mut name = String::new();1090 let mut name = String::new();
1073 let (mut default_duration, mut unpack) = (None, Unpack::None);1091 let (mut default_duration, mut unpack) = (None, Unpack::None);
1074 let (mut width, mut height, mut color) = (0u32, 0u32, ColorInfo::default());1092 let (mut width, mut height, mut color) = (0u32, 0u32, ColorInfo::default());
1093+ // The `Range` element's own code -- 0 for an element that never stated
1094+ // one, which is not the same claim as "limited" and must not become it.
1095+ let mut range_code = 0u64;
1075 let mut display = (0u64, 0u64);1096 let mut display = (0u64, 0u64);
1076 let mut light = ContentLight::default();1097 let mut light = ContentLight::default();
1077 let (mut rate, mut channels, mut bits) = (0f64, 0u64, 0u64);1098 let (mut rate, mut channels, mut bits) = (0f64, 0u64, 0u64);
@@ -1107,7 +1128,9 @@ fn parse_tracks(
1107 // mean, in the same H.273 code points the bitstream1128 // mean, in the same H.273 code points the bitstream
1108 // uses. An element the file leaves out stays1129 // uses. An element the file leaves out stays
1109 // "unspecified" and falls to the tier below.1130 // "unspecified" and falls to the tier below.
1110- ebml::COLOUR => parse_colour(buf, leaf, &mut color, &mut light),1131+ ebml::COLOUR => {
1132+ parse_colour(buf, leaf, &mut color, &mut light, &mut range_code)
1133+ }
1111 _ => {}1134 _ => {}
1112 }1135 }
1113 }1136 }
@@ -1194,6 +1217,11 @@ fn parse_tracks(
1194 stream,1217 stream,
1195 codec_id: codec,1218 codec_id: codec,
1196 name,1219 name,
1220+ color_tags: Tags::from_codes(
1221+ u64::from(color.matrix),
1222+ u64::from(color.transfer),
1223+ range_code,
1224+ ),
1197 media,1225 media,
1198 unpack,1226 unpack,
1199 default_duration: step,1227 default_duration: step,
@@ -1202,16 +1230,28 @@ fn parse_tracks(
1202 (streams, tracks)1230 (streams, tracks)
1203 }1231 }
1204 1232
1205-fn parse_colour(buf: &[u8], range: Range<usize>, color: &mut ColorInfo, light: &mut ContentLight) {1233+fn parse_colour(
1206- let start = range.start;1234+ buf: &[u8],
1207- for (id, child) in Elements::new(&buf[range]) {1235+ at: Range<usize>,
1236+ color: &mut ColorInfo,
1237+ light: &mut ContentLight,
1238+ range: &mut u64,
1239+) {
1240+ let start = at.start;
1241+ for (id, child) in Elements::new(&buf[at]) {
1208 let child = start + child.start..start + child.end;1242 let child = start + child.start..start + child.end;
1209 match id {1243 match id {
1210 ebml::MATRIX_COEFFICIENTS => color.matrix = ebml::uint_of(&buf[child]) as u8,1244 ebml::MATRIX_COEFFICIENTS => color.matrix = ebml::uint_of(&buf[child]) as u8,
1211 ebml::TRANSFER_CHARACTERISTICS => color.transfer = ebml::uint_of(&buf[child]) as u8,1245 ebml::TRANSFER_CHARACTERISTICS => color.transfer = ebml::uint_of(&buf[child]) as u8,
1212 ebml::PRIMARIES => color.primaries = ebml::uint_of(&buf[child]) as u8,1246 ebml::PRIMARIES => color.primaries = ebml::uint_of(&buf[child]) as u8,
1213- // Matroska says the range as a code, not a flag: 1 limited, 2 full.1247+ // Matroska says the range as a code, not a flag: 0 unspecified,
1214- ebml::RANGE => color.full_range = ebml::uint_of(&buf[child]) == 2,1248+ // 1 limited, 2 full. The code is kept as well as the flag: an
1249+ // element that stated nothing is not an element that stated
1250+ // "limited" ([`MatroskaDemuxer::color_tags`]).
1251+ ebml::RANGE => {
1252+ *range = ebml::uint_of(&buf[child]);
1253+ color.full_range = *range == 2;
1254+ }
1215 ebml::MAX_CLL => light.max_cll = nits(ebml::uint_of(&buf[child]) as f64),1255 ebml::MAX_CLL => light.max_cll = nits(ebml::uint_of(&buf[child]) as f64),
1216 ebml::MAX_FALL => light.max_fall = nits(ebml::uint_of(&buf[child]) as f64),1256 ebml::MAX_FALL => light.max_fall = nits(ebml::uint_of(&buf[child]) as f64),
1217 ebml::MASTERING_METADATA => {1257 ebml::MASTERING_METADATA => {
@@ -1576,6 +1616,51 @@ mod tests {
1576 assert_eq!(language_of("", "fil"), "fil");1616 assert_eq!(language_of("", "fil"), "fil");
1577 }1617 }
1578 1618
1619+ /// **An absent `Range` states nothing**, and must not be read as a
1620+ /// declaration of limited range.
1621+ ///
1622+ /// [`ColorInfo`] has to answer `full_range` either way, so the tier a host
1623+ /// resolves colour with comes off [`MatroskaDemuxer::color_tags`] instead:
1624+ /// there an untagged track says nothing and lets the bitstream's own VUI
1625+ /// answer, where reading the `ColorInfo` would shadow a full-range stream
1626+ /// and display it washed out. Same shape as ec-mp4's `nclc` box.
1627+ #[test]
1628+ fn a_colour_element_with_no_range_child_declares_no_range() {
1629+ let colour = |range: Option<u64>| {
1630+ let mut tags = Vec::new();
1631+ uint(&mut tags, ebml::MATRIX_COEFFICIENTS, 1);
1632+ uint(&mut tags, ebml::TRANSFER_CHARACTERISTICS, 1);
1633+ uint(&mut tags, ebml::PRIMARIES, 1);
1634+ if let Some(range) = range {
1635+ uint(&mut tags, ebml::RANGE, range);
1636+ }
1637+ let mut video = Vec::new();
1638+ uint(&mut video, ebml::PIXEL_WIDTH, 64);
1639+ uint(&mut video, ebml::PIXEL_HEIGHT, 64);
1640+ elem(&mut video, ebml::COLOUR, &tags);
1641+ let mut entry = track(1, 1, "V_AV1", None);
1642+ elem(&mut entry, ebml::VIDEO, &video);
1643+ let mut tracks = Vec::new();
1644+ elem(&mut tracks, ebml::TRACK_ENTRY, &entry);
1645+ let (streams, parsed) =
1646+ parse_tracks(&tracks, 1_000_000, TimeBase::new(1, 1_000), None);
1647+ let info = streams[0].params.video().expect("a picture").clone();
1648+ (info, parsed[0].color_tags)
1649+ };
1650+
1651+ let (info, tags) = colour(None);
1652+ assert_eq!(
1653+ tags.full_range, None,
1654+ "a Colour element with no Range child states no range"
1655+ );
1656+ assert!(!info.color.full_range, "the ColorInfo still has to answer one");
1657+ assert_eq!(tags.matrix, Some(ec_core::color::Matrix::Bt709));
1658+
1659+ assert_eq!(colour(Some(1)).1.full_range, Some(false), "1 is limited");
1660+ assert_eq!(colour(Some(2)).1.full_range, Some(true), "2 is full");
1661+ assert!(colour(Some(2)).0.color.full_range);
1662+ }
1663+
1579 #[test]1664 #[test]
1580 fn codec_ids_cover_the_families_the_family_carries() {1665 fn codec_ids_cover_the_families_the_family_carries() {
1581 assert_eq!(codec_of("V_MPEGH/ISO/HEVC", 0), Some(CodecId::H265));1666 assert_eq!(codec_of("V_MPEGH/ISO/HEVC", 0), Some(CodecId::H265));