The bitstream says which matrix it is, and a grade happens where the preview grades it
Browse files at this commit86c11d8e110bdf2cdf3776309e1bca679b7f674aParent: cc667e6- fix
- the bitstream says which matrix it is, and a grade happens where the preview grades it
5 changed files +255 -14
- crates/engine/src/export.rs +73 -9
- crates/engine/tests/export_color.rs +130 -2
- vendor/oxideav-h265/src/encoder/inter.rs +3 -1
- vendor/oxideav-h265/src/encoder/intra.rs +46 -1
- vendor/oxideav-h265/src/encoder/pyramid.rs +3 -1
crates/engine/src/export.rsmodified+73 -9
| @@ -64,7 +64,7 @@ use flacenc::error::Verify; | |||
| 64 | use oxideav_core::Muxer as _; | 64 | use oxideav_core::Muxer as _; |
| 65 | 65 | ||
| 66 | use crate::audio::{AudioMeta, AudioSession}; | 66 | use crate::audio::{AudioMeta, AudioSession}; |
| 67 | -use crate::colorspace::ColorDescription; | 67 | +use crate::colorspace::{ColorDescription, Matrix}; |
| 68 | use crate::demux::{Demuxer, VideoMeta}; | 68 | use crate::demux::{Demuxer, VideoMeta}; |
| 69 | use crate::hw::{HwEncoder, HwSession}; | 69 | use crate::hw::{HwEncoder, HwSession}; |
| 70 | use crate::mux::{AudioParams, Av1Params, HevcParams, MkvMuxer, Mp4Muxer, SubParams, VideoParams}; | 70 | use crate::mux::{AudioParams, Av1Params, HevcParams, MkvMuxer, Mp4Muxer, SubParams, VideoParams}; |
| @@ -1006,10 +1006,12 @@ fn run( | |||
| 1006 | None => (None, Rate::REAL_TIME, 0, None), | 1006 | None => (None, Rate::REAL_TIME, 0, None), |
| 1007 | }; | 1007 | }; |
| 1008 | // Mixed spaces on one timeline: a clip coded against another matrix than | 1008 | // Mixed spaces on one timeline: a clip coded against another matrix than |
| 1009 | - // the one this file declares is rewritten into it, before the grade, | 1009 | + // the one this file declares is rewritten into it, *after* the grade -- |
| 1010 | - // whose numbers the user set against what the canvas showed. Same-space | 1010 | + // playback grades in the source's own space and converts for the screen |
| 1011 | - // clips -- the whole of an ordinary project -- take `None` and not a | 1011 | + // ([`crate::decode`]), so a grade applied to remapped samples is a |
| 1012 | - // byte is touched. | 1012 | + // different picture than the one the canvas showed. Same-space clips -- |
| 1013 | + // the whole of an ordinary project -- take `None` and not a byte is | ||
| 1014 | + // touched. | ||
| 1013 | // | 1015 | // |
| 1014 | // ponytail: an HDR source is composited with the matrix treatment alone | 1016 | // ponytail: an HDR source is composited with the matrix treatment alone |
| 1015 | // and still looks washed out; the tonemap lands here, ahead of this, and | 1017 | // and still looks washed out; the tonemap lands here, ahead of this, and |
| @@ -1100,12 +1102,15 @@ fn run( | |||
| 1100 | gu.extend_from_slice(u); | 1102 | gu.extend_from_slice(u); |
| 1101 | gv.clear(); | 1103 | gv.clear(); |
| 1102 | gv.extend_from_slice(v); | 1104 | gv.extend_from_slice(v); |
| 1103 | - if let Some((from, to)) = remap { | 1105 | + // Grade in the source's space, then remap the graded |
| 1104 | - crate::colorspace::remap(from, to, gy, gu, gv, width as usize); | 1106 | + // result: the order playback's `Decoded::frame` renders in, |
| 1105 | - } | 1107 | + // and the only order in which preview and export agree. |
| 1106 | if let Some(params) = params { | 1108 | if let Some(params) = params { |
| 1107 | crate::color::apply_yuv(¶ms, gy, gu, gv); | 1109 | crate::color::apply_yuv(¶ms, gy, gu, gv); |
| 1108 | } | 1110 | } |
| 1111 | + if let Some((from, to)) = remap { | ||
| 1112 | + crate::colorspace::remap(from, to, gy, gu, gv, width as usize); | ||
| 1113 | + } | ||
| 1109 | (&gy[..], &gu[..], &gv[..]) | 1114 | (&gy[..], &gu[..], &gv[..]) |
| 1110 | } | 1115 | } |
| 1111 | }; | 1116 | }; |
| @@ -1827,6 +1832,11 @@ struct HevcEnc { | |||
| 1827 | crop: (usize, usize), | 1832 | crop: (usize, usize), |
| 1828 | qp: i32, | 1833 | qp: i32, |
| 1829 | lanes: usize, | 1834 | lanes: usize, |
| 1835 | + /// What the SPS says the samples mean. A decoder reads the bitstream before | ||
| 1836 | + /// it reads the container, so an HEVC stream without this renders BT.601 in | ||
| 1837 | + /// libavcodec however the file is tagged -- the container's `Colour` element | ||
| 1838 | + /// and `colr` box are not enough on their own. | ||
| 1839 | + signal: oxideav_h265::vui::VideoSignalType, | ||
| 1830 | } | 1840 | } |
| 1831 | 1841 | ||
| 1832 | impl Enc { | 1842 | impl Enc { |
| @@ -1929,6 +1939,25 @@ impl Enc { | |||
| 1929 | // progress the whole way, which is what makes that bearable rather than | 1939 | // progress the whole way, which is what makes that bearable rather than |
| 1930 | // hidden. | 1940 | // hidden. |
| 1931 | cfg.speed_settings = rav1e::prelude::SpeedSettings::from_preset(AV1_SPEED); | 1941 | cfg.speed_settings = rav1e::prelude::SpeedSettings::from_preset(AV1_SPEED); |
| 1942 | + // What the sequence header says the samples mean, for the reason | ||
| 1943 | + // [`video_signal_type`] gives: a container tag is the second answer a | ||
| 1944 | + // decoder looks at, not the first. Written as rav1e's own enums rather | ||
| 1945 | + // than the H.273 numbers -- the crate has no `from` for the code points, | ||
| 1946 | + // and the output rule only ever picks one of these two spaces. | ||
| 1947 | + let out = ColorDescription::output(meta.height); | ||
| 1948 | + cfg.pixel_range = rav1e::prelude::PixelRange::Limited; | ||
| 1949 | + cfg.color_description = Some(match out.matrix { | ||
| 1950 | + Matrix::Bt709 => rav1e::prelude::ColorDescription { | ||
| 1951 | + color_primaries: rav1e::prelude::ColorPrimaries::BT709, | ||
| 1952 | + transfer_characteristics: rav1e::prelude::TransferCharacteristics::BT709, | ||
| 1953 | + matrix_coefficients: rav1e::prelude::MatrixCoefficients::BT709, | ||
| 1954 | + }, | ||
| 1955 | + _ => rav1e::prelude::ColorDescription { | ||
| 1956 | + color_primaries: rav1e::prelude::ColorPrimaries::BT601, | ||
| 1957 | + transfer_characteristics: rav1e::prelude::TransferCharacteristics::BT601, | ||
| 1958 | + matrix_coefficients: rav1e::prelude::MatrixCoefficients::BT601, | ||
| 1959 | + }, | ||
| 1960 | + }); | ||
| 1932 | let config = rav1e::Config::new() | 1961 | let config = rav1e::Config::new() |
| 1933 | .with_encoder_config(cfg) | 1962 | .with_encoder_config(cfg) |
| 1934 | .with_threads(std::thread::available_parallelism().map_or(1, |n| n.get())); | 1963 | .with_threads(std::thread::available_parallelism().map_or(1, |n| n.get())); |
| @@ -1987,6 +2016,7 @@ impl Enc { | |||
| 1987 | crop: (width - meta.width as usize, height - meta.height as usize), | 2016 | crop: (width - meta.width as usize, height - meta.height as usize), |
| 1988 | qp: hevc_qp(meta, settings), | 2017 | qp: hevc_qp(meta, settings), |
| 1989 | lanes: lanes.min(HEVC_LANES), | 2018 | lanes: lanes.min(HEVC_LANES), |
| 2019 | + signal: video_signal_type(meta.height), | ||
| 1990 | })) | 2020 | })) |
| 1991 | } | 2021 | } |
| 1992 | 2022 | ||
| @@ -2122,6 +2152,7 @@ impl HevcEnc { | |||
| 2122 | /// as an error rather than unwinding the export thread. | 2152 | /// as an error rather than unwinding the export thread. |
| 2123 | fn code_batch(&mut self) -> crate::Result<()> { | 2153 | fn code_batch(&mut self) -> crate::Result<()> { |
| 2124 | let (width, height, qp, crop) = (self.width, self.height, self.qp, self.crop); | 2154 | let (width, height, qp, crop) = (self.width, self.height, self.qp, self.crop); |
| 2155 | + let signal = self.signal; | ||
| 2125 | let coded: Vec<crate::Result<Vec<u8>>> = std::thread::scope(|scope| { | 2156 | let coded: Vec<crate::Result<Vec<u8>>> = std::thread::scope(|scope| { |
| 2126 | let lanes: Vec<_> = self | 2157 | let lanes: Vec<_> = self |
| 2127 | .pending | 2158 | .pending |
| @@ -2129,7 +2160,15 @@ impl HevcEnc { | |||
| 2129 | .map(|(y, cb, cr)| { | 2160 | .map(|(y, cb, cr)| { |
| 2130 | scope.spawn(move || { | 2161 | scope.spawn(move || { |
| 2131 | oxideav_h265::encoder::intra::encode_idr_intra_au_cropped( | 2162 | oxideav_h265::encoder::intra::encode_idr_intra_au_cropped( |
| 2132 | - y, cb, cr, width, height, qp, crop.0, crop.1, | 2163 | + y, |
| 2164 | + cb, | ||
| 2165 | + cr, | ||
| 2166 | + width, | ||
| 2167 | + height, | ||
| 2168 | + qp, | ||
| 2169 | + crop.0, | ||
| 2170 | + crop.1, | ||
| 2171 | + Some(signal), | ||
| 2133 | ) | 2172 | ) |
| 2134 | .map(|coded| coded.au) | 2173 | .map(|coded| coded.au) |
| 2135 | .map_err(|e| crate::Error::from(format!("HEVC intra encode: {e:?}"))) | 2174 | .map_err(|e| crate::Error::from(format!("HEVC intra encode: {e:?}"))) |
| @@ -2212,6 +2251,31 @@ fn hevc_qp(meta: &VideoMeta, settings: &ExportSettings) -> i32 { | |||
| 2212 | (qp.round() as i32).clamp(HEVC_QP_MIN, HEVC_QP_MAX) | 2251 | (qp.round() as i32).clamp(HEVC_QP_MIN, HEVC_QP_MAX) |
| 2213 | } | 2252 | } |
| 2214 | 2253 | ||
| 2254 | +/// What the *bitstream* says the samples mean, in the words a sequence header | ||
| 2255 | +/// spells them -- the same H.273 code points [`crate::mux`] writes into the | ||
| 2256 | +/// container, from the same [`ColorDescription::output`] rule, so the two halves | ||
| 2257 | +/// of a file cannot drift apart. | ||
| 2258 | +/// | ||
| 2259 | +/// This exists because a container tag is not enough: libavcodec takes the | ||
| 2260 | +/// bitstream's answer first, and a stream that says nothing is "unspecified", | ||
| 2261 | +/// which renders BT.601 -- a visible shift on a 709 export that a player reads | ||
| 2262 | +/// from the wrong matrix. Every export is limited range, which is what | ||
| 2263 | +/// `video_full_range_flag = false` says here. | ||
| 2264 | +fn video_signal_type(height: u32) -> oxideav_h265::vui::VideoSignalType { | ||
| 2265 | + let (primaries, transfer, matrix) = ColorDescription::output(height).codes(); | ||
| 2266 | + oxideav_h265::vui::VideoSignalType { | ||
| 2267 | + // "Unspecified", which is what every encoder here writes: the field is | ||
| 2268 | + // the analogue system a picture came off, and none of these did. | ||
| 2269 | + video_format: 5, | ||
| 2270 | + video_full_range_flag: false, | ||
| 2271 | + colour_description: Some(oxideav_h265::vui::ColourDescription { | ||
| 2272 | + colour_primaries: primaries as u8, | ||
| 2273 | + transfer_characteristics: transfer as u8, | ||
| 2274 | + matrix_coeffs: matrix as u8, | ||
| 2275 | + }), | ||
| 2276 | + } | ||
| 2277 | +} | ||
| 2278 | + | ||
| 2215 | /// Every temporal unit `rav1e` has finished, moved into the queue in display | 2279 | /// Every temporal unit `rav1e` has finished, moved into the queue in display |
| 2216 | /// order. `NeedMoreData` (send another picture) and `LimitReached` (the flush is | 2280 | /// order. `NeedMoreData` (send another picture) and `LimitReached` (the flush is |
| 2217 | /// through) are the two ways it says there is nothing more; `Encoded` means a | 2281 | /// through) are the two ways it says there is nothing more; `Encoded` means a |
crates/engine/tests/export_color.rsmodified+130 -2
| @@ -6,10 +6,13 @@ | |||
| 6 | //! only this engine agrees with is not a tag: | 6 | //! only this engine agrees with is not a tag: |
| 7 | //! | 7 | //! |
| 8 | //! 1. every export declares a space (BT.709 at 720 lines and up, BT.601 below), | 8 | //! 1. every export declares a space (BT.709 at 720 lines and up, BT.601 below), |
| 9 | -//! in the `Colour` element of a Matroska and the `colr` box of an mp4, and | 9 | +//! in the `Colour` element of a Matroska and the `colr` box of an mp4, *and* |
| 10 | +//! in the coded bitstream, which is the answer a decoder takes first; | ||
| 10 | //! 2. a clip coded against the *other* matrix is rewritten into the file's, so | 11 | //! 2. a clip coded against the *other* matrix is rewritten into the file's, so |
| 11 | //! reading the export by its own tags gives back the picture that went in -- | 12 | //! reading the export by its own tags gives back the picture that went in -- |
| 12 | -//! a reconcile, not a second conversion. | 13 | +//! a reconcile, not a second conversion; and |
| 14 | +//! 3. a graded clip comes out of the export the way the preview showed it, | ||
| 15 | +//! which is what makes a grade a decision and not a guess. | ||
| 13 | //! | 16 | //! |
| 14 | //! ```text | 17 | //! ```text |
| 15 | //! cargo test -p engine --release --test export_color | 18 | //! cargo test -p engine --release --test export_color |
| @@ -92,6 +95,52 @@ fn probed(path: &Path) -> Option<Vec<String>> { | |||
| 92 | ) | 95 | ) |
| 93 | } | 96 | } |
| 94 | 97 | ||
| 98 | +/// What ffprobe says the first *coded frame* is, which is the stream's own | ||
| 99 | +/// answer and not the container's: libavcodec fills a frame's colour in from the | ||
| 100 | +/// bitstream, so a codec that signalled nothing reads back "unknown" here even | ||
| 101 | +/// where the file is tagged. `None` when ffprobe is not installed. | ||
| 102 | +fn probed_frame(path: &Path) -> Option<String> { | ||
| 103 | + let out = Command::new("ffprobe") | ||
| 104 | + .args([ | ||
| 105 | + "-v", | ||
| 106 | + "error", | ||
| 107 | + "-select_streams", | ||
| 108 | + "v:0", | ||
| 109 | + "-show_frames", | ||
| 110 | + "-read_intervals", | ||
| 111 | + "%+#1", | ||
| 112 | + "-show_entries", | ||
| 113 | + "frame=color_space", | ||
| 114 | + "-of", | ||
| 115 | + "default=nw=1:nk=1", | ||
| 116 | + ]) | ||
| 117 | + .arg(path) | ||
| 118 | + .output() | ||
| 119 | + .ok()?; | ||
| 120 | + Some( | ||
| 121 | + String::from_utf8_lossy(&out.stdout) | ||
| 122 | + .lines() | ||
| 123 | + .next() | ||
| 124 | + .unwrap_or_default() | ||
| 125 | + .trim() | ||
| 126 | + .to_string(), | ||
| 127 | + ) | ||
| 128 | +} | ||
| 129 | + | ||
| 130 | +/// The session's first frame as BGRA, through this engine's own decode path -- | ||
| 131 | +/// the very path the preview draws with, so a preview frame and a re-imported | ||
| 132 | +/// export frame are two measurements of one picture and nothing else. | ||
| 133 | +fn frame0_bgra(session: &mut PlaybackSession) -> Vec<u8> { | ||
| 134 | + let deadline = Instant::now() + Duration::from_secs(180); | ||
| 135 | + loop { | ||
| 136 | + if let Some(frame) = session.try_frame() { | ||
| 137 | + return frame.bgra; | ||
| 138 | + } | ||
| 139 | + assert!(Instant::now() < deadline, "no frame in 180 s"); | ||
| 140 | + std::thread::sleep(Duration::from_millis(5)); | ||
| 141 | + } | ||
| 142 | +} | ||
| 143 | + | ||
| 95 | /// Frame 0 of `path` as planar 4:2:0 bytes, straight off the decoder with no | 144 | /// Frame 0 of `path` as planar 4:2:0 bytes, straight off the decoder with no |
| 96 | /// colour conversion at all -- the samples as they are in the file, which is | 145 | /// colour conversion at all -- the samples as they are in the file, which is |
| 97 | /// what a remap is measured on. `None` when ffmpeg is not installed. | 146 | /// what a remap is measured on. `None` when ffmpeg is not installed. |
| @@ -230,3 +279,82 @@ fn a_same_space_clip_is_left_alone() { | |||
| 230 | ); | 279 | ); |
| 231 | std::fs::remove_file(&out).unwrap(); | 280 | std::fs::remove_file(&out).unwrap(); |
| 232 | } | 281 | } |
| 282 | + | ||
| 283 | +/// The tag a *decoder* reads, on every format there is. A container's colour | ||
| 284 | +/// element is the second answer libavcodec looks at and the bitstream's is the | ||
| 285 | +/// first, so a stream that signals nothing is rendered BT.601 whatever the file | ||
| 286 | +/// says about itself -- which is a visible shift on a 709 export and was | ||
| 287 | +/// measured as one (9.78 mean codes off the right render against 2.27 off the | ||
| 288 | +/// wrong one, HEVC before its SPS carried a VUI). | ||
| 289 | +/// | ||
| 290 | +/// All five video formats, because the gap was per-codec and a container-level | ||
| 291 | +/// check could not see it: the mp4 and Matroska tags were right the whole time. | ||
| 292 | +#[test] | ||
| 293 | +fn every_format_signals_its_space_in_the_bitstream() { | ||
| 294 | + if probed_frame(&asset("test_bt601.mp4")).is_none() { | ||
| 295 | + eprintln!("no ffprobe: skipping the frame-level colour check"); | ||
| 296 | + return; | ||
| 297 | + } | ||
| 298 | + for (format, ext, name) in [ | ||
| 299 | + (Format::Mp4, "mp4", "h264-mp4"), | ||
| 300 | + (Format::Hevc, "mkv", "hevc-mkv"), | ||
| 301 | + (Format::HevcMp4, "mp4", "hevc-mp4"), | ||
| 302 | + (Format::Av1, "mkv", "av1-mkv"), | ||
| 303 | + (Format::Av1Mp4, "mp4", "av1-mp4"), | ||
| 304 | + ] { | ||
| 305 | + let session = two_frames(&asset("test_bt601.mp4")); | ||
| 306 | + let out = out_path(&format!("frametag_{name}"), ext); | ||
| 307 | + export(&session, &out, format); | ||
| 308 | + let got = probed_frame(&out).expect("ffprobe is installed"); | ||
| 309 | + eprintln!("{name}: frame colour_space {got}"); | ||
| 310 | + assert_eq!(got, "bt709", "{name}: what a decoder makes of the frame"); | ||
| 311 | + std::fs::remove_file(&out).unwrap(); | ||
| 312 | + } | ||
| 313 | +} | ||
| 314 | + | ||
| 315 | +/// Preview and export are the same picture, on the one path where they used to | ||
| 316 | +/// differ: a graded clip whose own space is not the file's. Playback grades in | ||
| 317 | +/// the source's space and converts for the screen, so an export that remapped | ||
| 318 | +/// *first* graded different numbers and came out 21.04 mean codes away from what | ||
| 319 | +/// the canvas had shown -- a saturation the user set against one picture, | ||
| 320 | +/// applied to another. | ||
| 321 | +/// | ||
| 322 | +/// Measured through this engine's own decode of its own export, because that is | ||
| 323 | +/// the comparison the invariant is about; the ungraded control is what says the | ||
| 324 | +/// number belongs to the grade and not to the encoder. | ||
| 325 | +#[test] | ||
| 326 | +fn a_graded_clip_exports_the_picture_the_preview_showed() { | ||
| 327 | + for (grade, label, ceiling) in [ | ||
| 328 | + ( | ||
| 329 | + Some(engine::color::ColorParams { | ||
| 330 | + brightness: 0.0, | ||
| 331 | + contrast: 1.0, | ||
| 332 | + saturation: 0.0, | ||
| 333 | + tint: 0.0, | ||
| 334 | + }), | ||
| 335 | + "saturation 0", | ||
| 336 | + 2.0, | ||
| 337 | + ), | ||
| 338 | + (None, "ungraded control", 2.0), | ||
| 339 | + ] { | ||
| 340 | + let mut session = two_frames(&asset("test_bt601.mp4")); | ||
| 341 | + assert!( | ||
| 342 | + session.set_color(engine::project::Lane::V1, 0, grade), | ||
| 343 | + "{label}: the grade went on the clip" | ||
| 344 | + ); | ||
| 345 | + let preview = frame0_bgra(&mut session); | ||
| 346 | + let out = out_path("preview_vs_export", "mp4"); | ||
| 347 | + export(&session, &out, Format::Mp4); | ||
| 348 | + | ||
| 349 | + let mut reopened = PlaybackSession::open(&*out).expect("reopen the export"); | ||
| 350 | + let exported = frame0_bgra(&mut reopened); | ||
| 351 | + let diff = mean_abs_diff(&preview, &exported); | ||
| 352 | + eprintln!("{label}: preview vs export {diff:.2} mean codes"); | ||
| 353 | + assert!( | ||
| 354 | + diff <= ceiling, | ||
| 355 | + "{label}: the export is {diff:.2} codes from what the preview showed" | ||
| 356 | + ); | ||
| 357 | + drop(reopened); | ||
| 358 | + std::fs::remove_file(&out).unwrap(); | ||
| 359 | + } | ||
| 360 | +} | ||
vendor/oxideav-h265/src/encoder/inter.rsmodified+3 -1
| @@ -364,7 +364,9 @@ impl LowDelayPEncoder { | |||
| 364 | max_num_reorder_pics: 0, | 364 | max_num_reorder_pics: 0, |
| 365 | min_cb_log2: if self.amp { 3 } else { 4 }, | 365 | min_cb_log2: if self.amp { 3 } else { 4 }, |
| 366 | amp: self.amp, | 366 | amp: self.amp, |
| 367 | - conf_win: (0, 0), // edith patch: no crop on the inter paths | 367 | + // edith patch: no crop and no VUI on the inter paths |
| 368 | + conf_win: (0, 0), | ||
| 369 | + signal: None, | ||
| 368 | }, | 370 | }, |
| 369 | &self.filters, | 371 | &self.filters, |
| 370 | )?; | 372 | )?; |
vendor/oxideav-h265/src/encoder/intra.rsmodified+46 -1
| @@ -54,6 +54,8 @@ use crate::encoder::loopfilter::{ | |||
| 54 | encode_sao_ctb, filter_frame, CtbShape, FilterInput, LoopFilterCfg, | 54 | encode_sao_ctb, filter_frame, CtbShape, FilterInput, LoopFilterCfg, |
| 55 | }; | 55 | }; |
| 56 | use crate::encoder::nal::{annexb, nal_unit}; | 56 | use crate::encoder::nal::{annexb, nal_unit}; |
| 57 | +// --- edith patch: the SPS's colour signalling reuses the parser's types --- | ||
| 58 | +use crate::vui::VideoSignalType; | ||
| 57 | use crate::encoder::pcm::{level_idc_for, write_pps_lf, write_ptl, write_vps, write_vps_cfg}; | 59 | use crate::encoder::pcm::{level_idc_for, write_pps_lf, write_ptl, write_vps, write_vps_cfg}; |
| 58 | use crate::encoder::residual::encode_residual_coding; | 60 | use crate::encoder::residual::encode_residual_coding; |
| 59 | use crate::intra_mode_field::{IntraModeField, Neighbour}; | 61 | use crate::intra_mode_field::{IntraModeField, Neighbour}; |
| @@ -157,6 +159,12 @@ pub(crate) struct SpsCfg { | |||
| 157 | /// `conformance_window_flag = 0`, which is what upstream always | 159 | /// `conformance_window_flag = 0`, which is what upstream always |
| 158 | /// wrote. | 160 | /// wrote. |
| 159 | pub conf_win: (u32, u32), | 161 | pub conf_win: (u32, u32), |
| 162 | + /// §E.2.1 `video_signal_type_present_flag` block, written into a | ||
| 163 | + /// VUI of its own. `None` writes `vui_parameters_present_flag = 0`, | ||
| 164 | + /// which is what upstream always wrote — and which makes a decoder | ||
| 165 | + /// infer "unspecified" and render the picture BT.601 whatever the | ||
| 166 | + /// container said. | ||
| 167 | + pub signal: Option<VideoSignalType>, | ||
| 160 | // --- end edith patch --- | 168 | // --- end edith patch --- |
| 161 | } | 169 | } |
| 162 | 170 | ||
| @@ -170,6 +178,7 @@ impl SpsCfg { | |||
| 170 | min_cb_log2: CTB_LOG2, | 178 | min_cb_log2: CTB_LOG2, |
| 171 | amp: false, | 179 | amp: false, |
| 172 | conf_win: (0, 0), | 180 | conf_win: (0, 0), |
| 181 | + signal: None, | ||
| 173 | } | 182 | } |
| 174 | } | 183 | } |
| 175 | } | 184 | } |
| @@ -233,7 +242,37 @@ pub(crate) fn write_sps_cfg( | |||
| 233 | w.put_bit(0); // long_term_ref_pics_present_flag | 242 | w.put_bit(0); // long_term_ref_pics_present_flag |
| 234 | w.put_bit(0); // sps_temporal_mvp_enabled_flag | 243 | w.put_bit(0); // sps_temporal_mvp_enabled_flag |
| 235 | w.put_bit(0); // strong_intra_smoothing_enabled_flag | 244 | w.put_bit(0); // strong_intra_smoothing_enabled_flag |
| 236 | - w.put_bit(0); // vui_parameters_present_flag | 245 | + // --- edith patch: §E.2.1 VUI, colour signalling only (upstream |
| 246 | + // always wrote 0). Everything else in the VUI stays absent, which | ||
| 247 | + // §E.3.1 infers to exactly the values upstream's silence did. | ||
| 248 | + match cfg.signal { | ||
| 249 | + None => w.put_bit(0), // vui_parameters_present_flag | ||
| 250 | + Some(signal) => { | ||
| 251 | + w.put_bit(1); // vui_parameters_present_flag | ||
| 252 | + w.put_bit(0); // aspect_ratio_info_present_flag | ||
| 253 | + w.put_bit(0); // overscan_info_present_flag | ||
| 254 | + w.put_bit(1); // video_signal_type_present_flag | ||
| 255 | + w.put_bits(u32::from(signal.video_format), 3); | ||
| 256 | + w.put_bit(u8::from(signal.video_full_range_flag)); | ||
| 257 | + match signal.colour_description { | ||
| 258 | + None => w.put_bit(0), // colour_description_present_flag | ||
| 259 | + Some(colour) => { | ||
| 260 | + w.put_bit(1); // colour_description_present_flag | ||
| 261 | + w.put_bits(u32::from(colour.colour_primaries), 8); | ||
| 262 | + w.put_bits(u32::from(colour.transfer_characteristics), 8); | ||
| 263 | + w.put_bits(u32::from(colour.matrix_coeffs), 8); | ||
| 264 | + } | ||
| 265 | + } | ||
| 266 | + w.put_bit(0); // chroma_loc_info_present_flag | ||
| 267 | + w.put_bit(0); // neutral_chroma_indication_flag | ||
| 268 | + w.put_bit(0); // field_seq_flag | ||
| 269 | + w.put_bit(0); // frame_field_info_present_flag | ||
| 270 | + w.put_bit(0); // default_display_window_flag | ||
| 271 | + w.put_bit(0); // vui_timing_info_present_flag | ||
| 272 | + w.put_bit(0); // bitstream_restriction_flag | ||
| 273 | + } | ||
| 274 | + } | ||
| 275 | + // --- end edith patch --- | ||
| 237 | w.put_bit(0); // sps_extension_present_flag | 276 | w.put_bit(0); // sps_extension_present_flag |
| 238 | w.rbsp_trailing_bits(); | 277 | w.rbsp_trailing_bits(); |
| 239 | w.finish() | 278 | w.finish() |
| @@ -524,6 +563,10 @@ pub fn encode_idr_intra_au( | |||
| 524 | /// size. Both crops must be even (4:2:0 addresses the window in chroma | 563 | /// size. Both crops must be even (4:2:0 addresses the window in chroma |
| 525 | /// units) and smaller than 16. | 564 | /// units) and smaller than 16. |
| 526 | /// | 565 | /// |
| 566 | +/// `signal` is the §E.2.1 colour signalling the SPS carries: a decoder | ||
| 567 | +/// reads the bitstream before it reads the container, so `None` renders | ||
| 568 | +/// as "unspecified" — BT.601 in libavcodec — however the file is tagged. | ||
| 569 | +/// | ||
| 527 | /// # Errors | 570 | /// # Errors |
| 528 | /// [`IntraEncodeError`] as [`encode_idr_intra_au`], plus | 571 | /// [`IntraEncodeError`] as [`encode_idr_intra_au`], plus |
| 529 | /// [`IntraEncodeError::BadDimensions`] for an odd or oversized crop. | 572 | /// [`IntraEncodeError::BadDimensions`] for an odd or oversized crop. |
| @@ -536,6 +579,7 @@ pub fn encode_idr_intra_au_cropped( | |||
| 536 | qp: i32, | 579 | qp: i32, |
| 537 | crop_right: usize, | 580 | crop_right: usize, |
| 538 | crop_bottom: usize, | 581 | crop_bottom: usize, |
| 582 | + signal: Option<VideoSignalType>, | ||
| 539 | ) -> Result<IntraEncodedAu, IntraEncodeError> { | 583 | ) -> Result<IntraEncodedAu, IntraEncodeError> { |
| 540 | if crop_right % 2 != 0 || crop_bottom % 2 != 0 || crop_right >= CTB || crop_bottom >= CTB { | 584 | if crop_right % 2 != 0 || crop_bottom % 2 != 0 || crop_right >= CTB || crop_bottom >= CTB { |
| 541 | return Err(IntraEncodeError::BadDimensions { | 585 | return Err(IntraEncodeError::BadDimensions { |
| @@ -545,6 +589,7 @@ pub fn encode_idr_intra_au_cropped( | |||
| 545 | } | 589 | } |
| 546 | let mut cfg = SpsCfg::legacy(1); | 590 | let mut cfg = SpsCfg::legacy(1); |
| 547 | cfg.conf_win = ((crop_right / 2) as u32, (crop_bottom / 2) as u32); | 591 | cfg.conf_win = ((crop_right / 2) as u32, (crop_bottom / 2) as u32); |
| 592 | + cfg.signal = signal; | ||
| 548 | encode_idr_intra_au_full(y, cb, cr, width, height, qp, &cfg, &LoopFilterCfg::off()) | 593 | encode_idr_intra_au_full(y, cb, cr, width, height, qp, &cfg, &LoopFilterCfg::off()) |
| 549 | } | 594 | } |
| 550 | // --- end edith patch --- | 595 | // --- end edith patch --- |
vendor/oxideav-h265/src/encoder/pyramid.rsmodified+3 -1
| @@ -230,7 +230,9 @@ impl PyramidEncoder { | |||
| 230 | max_num_reorder_pics: self.depth(), | 230 | max_num_reorder_pics: self.depth(), |
| 231 | min_cb_log2: if self.amp { 3 } else { 4 }, | 231 | min_cb_log2: if self.amp { 3 } else { 4 }, |
| 232 | amp: self.amp, | 232 | amp: self.amp, |
| 233 | - conf_win: (0, 0), // edith patch: no crop on the inter paths | 233 | + // edith patch: no crop and no VUI on the inter paths |
| 234 | + conf_win: (0, 0), | ||
| 235 | + signal: None, | ||
| 234 | } | 236 | } |
| 235 | } | 237 | } |
| 236 | 238 | ||