The hardware HEVC seat says what its samples mean
Browse files at this commit9fdee6f6ff1ad95333d6e5d479f22c2f6919d2d1Parent: eff035e- fix
- the hardware HEVC seat says what its samples mean
6 changed files +116 -13
- crates/engine-hw/src/lib.rs +41 -3
- crates/engine/src/caps.rs +2 -1
- crates/engine/src/export.rs +12 -3
- crates/engine/src/hw.rs +31 -3
- vendor/cros-codecs/src/encoder/h265.rs +15 -0
- vendor/cros-codecs/src/encoder/stateless/h265/predictor.rs +15 -3
crates/engine-hw/src/lib.rsmodified+41 -3
| @@ -30,6 +30,7 @@ use cros_codecs::decoder::stateless::{DecodeError, StatelessDecoder, StatelessVi | |||
| 30 | use cros_codecs::decoder::{BlockingMode, DecodedHandle, DecoderEvent, StreamInfo}; | 30 | use cros_codecs::decoder::{BlockingMode, DecodedHandle, DecoderEvent, StreamInfo}; |
| 31 | use cros_codecs::encoder::av1::EncoderConfig as Av1Config; | 31 | use cros_codecs::encoder::av1::EncoderConfig as Av1Config; |
| 32 | use cros_codecs::encoder::h264::EncoderConfig as H264Config; | 32 | use cros_codecs::encoder::h264::EncoderConfig as H264Config; |
| 33 | +use cros_codecs::encoder::h265::ColourDescription as H265Colour; | ||
| 33 | use cros_codecs::encoder::h265::EncoderConfig as H265Config; | 34 | use cros_codecs::encoder::h265::EncoderConfig as H265Config; |
| 34 | use cros_codecs::encoder::stateless::av1::StatelessEncoder as Av1StatelessEncoder; | 35 | use cros_codecs::encoder::stateless::av1::StatelessEncoder as Av1StatelessEncoder; |
| 35 | use cros_codecs::encoder::stateless::h264::StatelessEncoder; | 36 | use cros_codecs::encoder::stateless::h264::StatelessEncoder; |
| @@ -1154,7 +1155,7 @@ impl EncSession { | |||
| 1154 | bitrate: u64, | 1155 | bitrate: u64, |
| 1155 | codec: EncCodec, | 1156 | codec: EncCodec, |
| 1156 | ) -> Option<Self> { | 1157 | ) -> Option<Self> { |
| 1157 | - Self::open_seat(width, height, fps_num, fps_den, bitrate, codec, false) | 1158 | + Self::open_seat(width, height, fps_num, fps_den, bitrate, codec, false, None) |
| 1158 | } | 1159 | } |
| 1159 | 1160 | ||
| 1160 | #[allow(clippy::too_many_arguments)] | 1161 | #[allow(clippy::too_many_arguments)] |
| @@ -1166,6 +1167,7 @@ impl EncSession { | |||
| 1166 | bitrate: u64, | 1167 | bitrate: u64, |
| 1167 | codec: EncCodec, | 1168 | codec: EncCodec, |
| 1168 | intra_only: bool, | 1169 | intra_only: bool, |
| 1170 | + colour: Option<H265Colour>, | ||
| 1169 | ) -> Option<Self> { | 1171 | ) -> Option<Self> { |
| 1170 | // NV12 chroma is half resolution, so odd dimensions have no packing; | 1172 | // NV12 chroma is half resolution, so odd dimensions have no packing; |
| 1171 | // and radeonsi refuses encode contexts below 64x64 (measured). | 1173 | // and radeonsi refuses encode contexts below 64x64 (measured). |
| @@ -1295,6 +1297,14 @@ impl EncSession { | |||
| 1295 | framerate, | 1297 | framerate, |
| 1296 | ..Default::default() | 1298 | ..Default::default() |
| 1297 | }, | 1299 | }, |
| 1300 | + // What the SPS tells a decoder the samples mean. Without it | ||
| 1301 | + // libavcodec reads this stream's colour as "unspecified" and | ||
| 1302 | + // renders it BT.601 whatever the container says -- the HEVC | ||
| 1303 | + // decoder takes the bitstream's answer and overwrites the | ||
| 1304 | + // container's with it, so an untagged 709 export is a | ||
| 1305 | + // visible shift (`engine::export::video_signal_type` writes | ||
| 1306 | + // the same three code points on the software seat). | ||
| 1307 | + colour, | ||
| 1298 | }; | 1308 | }; |
| 1299 | Box::new( | 1309 | Box::new( |
| 1300 | HevcEncoder::new_vaapi( | 1310 | HevcEncoder::new_vaapi( |
| @@ -1625,6 +1635,7 @@ pub extern "C" fn vh_enc_open_intra( | |||
| 1625 | bitrate, | 1635 | bitrate, |
| 1626 | EncCodec::H264, | 1636 | EncCodec::H264, |
| 1627 | true, | 1637 | true, |
| 1638 | + None, | ||
| 1628 | ) { | 1639 | ) { |
| 1629 | Some(session) => Box::into_raw(Box::new(session)) as *mut c_void, | 1640 | Some(session) => Box::into_raw(Box::new(session)) as *mut c_void, |
| 1630 | None => std::ptr::null_mut(), | 1641 | None => std::ptr::null_mut(), |
| @@ -1672,16 +1683,43 @@ pub extern "C" fn vh_enc_av1_open( | |||
| 1672 | /// The session it returns is fed, drained and closed through `vh_enc_frame`, | 1683 | /// The session it returns is fed, drained and closed through `vh_enc_frame`, |
| 1673 | /// `vh_enc_drain` and `vh_enc_close` exactly as an H.264 one is; what comes back | 1684 | /// `vh_enc_drain` and `vh_enc_close` exactly as an H.264 one is; what comes back |
| 1674 | /// out of them is Annex-B HEVC access units. | 1685 | /// out of them is Annex-B HEVC access units. |
| 1686 | +/// | ||
| 1687 | +/// The last four arguments are what the SPS says the samples mean -- the three | ||
| 1688 | +/// H.273 code points and the range flag -- and they are part of the *symbol* | ||
| 1689 | +/// rather than a later setter because the parameter sets are built when the | ||
| 1690 | +/// sequence starts. A seat that signalled nothing is read back as "unspecified" | ||
| 1691 | +/// and rendered BT.601, the container's own tag notwithstanding, which is why | ||
| 1692 | +/// this replaced the colourless `vh_enc_hevc_open`: an engine older than this | ||
| 1693 | +/// symbol finds none and takes the software intra seat, which has always | ||
| 1694 | +/// written the VUI. | ||
| 1675 | #[unsafe(no_mangle)] | 1695 | #[unsafe(no_mangle)] |
| 1676 | -pub extern "C" fn vh_enc_hevc_open( | 1696 | +pub extern "C" fn vh_enc_hevc_open_colour( |
| 1677 | width: u32, | 1697 | width: u32, |
| 1678 | height: u32, | 1698 | height: u32, |
| 1679 | fps_num: u32, | 1699 | fps_num: u32, |
| 1680 | fps_den: u32, | 1700 | fps_den: u32, |
| 1681 | bitrate: u64, | 1701 | bitrate: u64, |
| 1702 | + colour_primaries: u32, | ||
| 1703 | + transfer_characteristics: u32, | ||
| 1704 | + matrix_coeffs: u32, | ||
| 1705 | + full_range: i32, | ||
| 1682 | ) -> *mut c_void { | 1706 | ) -> *mut c_void { |
| 1683 | catch_unwind(AssertUnwindSafe(|| { | 1707 | catch_unwind(AssertUnwindSafe(|| { |
| 1684 | - match EncSession::open_codec(width, height, fps_num, fps_den, bitrate, EncCodec::Hevc) { | 1708 | + match EncSession::open_seat( |
| 1709 | + width, | ||
| 1710 | + height, | ||
| 1711 | + fps_num, | ||
| 1712 | + fps_den, | ||
| 1713 | + bitrate, | ||
| 1714 | + EncCodec::Hevc, | ||
| 1715 | + false, | ||
| 1716 | + Some(H265Colour { | ||
| 1717 | + colour_primaries, | ||
| 1718 | + transfer_characteristics, | ||
| 1719 | + matrix_coeffs, | ||
| 1720 | + full_range: full_range != 0, | ||
| 1721 | + }), | ||
| 1722 | + ) { | ||
| 1685 | Some(session) => Box::into_raw(Box::new(session)) as *mut c_void, | 1723 | Some(session) => Box::into_raw(Box::new(session)) as *mut c_void, |
| 1686 | None => std::ptr::null_mut(), | 1724 | None => std::ptr::null_mut(), |
| 1687 | } | 1725 | } |
crates/engine/src/caps.rsmodified+2 -1
| @@ -106,7 +106,8 @@ mod tests { | |||
| 106 | assert!(hw_line(Some(VhCaps::default())).starts_with("none —")); | 106 | assert!(hw_line(Some(VhCaps::default())).starts_with("none —")); |
| 107 | // This project's own GPU as `vainfo` lists it: H.264, HEVC and AV1 both | 107 | // This project's own GPU as `vainfo` lists it: H.264, HEVC and AV1 both |
| 108 | // ways, VP9 decode only, and the 10-bit profiles on three of the four. | 108 | // ways, VP9 decode only, and the 10-bit profiles on three of the four. |
| 109 | - // HEVC is an encode seat here as of the plugin's `vh_enc_hevc_open` -- | 109 | + // HEVC is an encode seat here as of the plugin's |
| 110 | + // `vh_enc_hevc_open_colour` -- | ||
| 110 | // the 10-bit note beside it stays a *decode* claim, that seat coding | 111 | // the 10-bit note beside it stays a *decode* claim, that seat coding |
| 111 | // 8-bit Main whatever the source was. | 112 | // 8-bit Main whatever the source was. |
| 112 | let radeonsi = VhCaps { | 113 | let radeonsi = VhCaps { |
crates/engine/src/export.rsmodified+12 -3
| @@ -549,9 +549,18 @@ fn hw_seat(meta: &VideoMeta, settings: &ExportSettings) -> Option<HwEncoder> { | |||
| 549 | // (`hevc_export::a_1080p_hardware_export_leaves_a_decoder_with_nothing_to_say`). | 549 | // (`hevc_export::a_1080p_hardware_export_leaves_a_decoder_with_nothing_to_say`). |
| 550 | // `None` here is the plugin's own "no" -- no entrypoint, or a picture | 550 | // `None` here is the plugin's own "no" -- no entrypoint, or a picture |
| 551 | // under the driver's 384x384 floor -- and the software seat takes it. | 551 | // under the driver's 384x384 floor -- and the software seat takes it. |
| 552 | - format if format.is_hevc() => { | 552 | + // The colour goes in at the open, from the same [`ColorDescription::output`] |
| 553 | - HwEncoder::open_hevc(meta.width, meta.height, fps_num, fps_den, bitrate) | 553 | + // rule the software seat's VUI and both containers' tags come from, so |
| 554 | - } | 554 | + // the file cannot say one thing in its bitstream and another in its |
| 555 | + // header. | ||
| 556 | + format if format.is_hevc() => HwEncoder::open_hevc( | ||
| 557 | + meta.width, | ||
| 558 | + meta.height, | ||
| 559 | + fps_num, | ||
| 560 | + fps_den, | ||
| 561 | + bitrate, | ||
| 562 | + ColorDescription::output(meta.height), | ||
| 563 | + ), | ||
| 555 | // Opt-in only, for the reason [`Enc::open_av1`] states in full. Both | 564 | // Opt-in only, for the reason [`Enc::open_av1`] states in full. Both |
| 556 | // AV1 formats sit on it: the container is not the encoder's business. | 565 | // AV1 formats sit on it: the container is not the encoder's business. |
| 557 | format if format.is_av1() && !forced("VE_HW_AV1") => None, | 566 | format if format.is_av1() && !forced("VE_HW_AV1") => None, |
crates/engine/src/hw.rsmodified+31 -3
| @@ -22,6 +22,8 @@ use std::sync::OnceLock; | |||
| 22 | 22 | ||
| 23 | use libloading::Library; | 23 | use libloading::Library; |
| 24 | 24 | ||
| 25 | +use crate::colorspace::ColorDescription; | ||
| 26 | + | ||
| 25 | const LIB_NAME: &str = "libengine_hw.so"; | 27 | const LIB_NAME: &str = "libengine_hw.so"; |
| 26 | 28 | ||
| 27 | /// Stream metadata, C layout. Mirrors [`crate::VideoMeta`]. | 29 | /// Stream metadata, C layout. Mirrors [`crate::VideoMeta`]. |
| @@ -443,7 +445,11 @@ struct EncPlugin { | |||
| 443 | open_av1: Option<extern "C" fn(u32, u32, u32, u32, u64) -> *mut c_void>, | 445 | open_av1: Option<extern "C" fn(u32, u32, u32, u32, u64) -> *mut c_void>, |
| 444 | /// The HEVC seat, missing from an older plugin for the same reason and at | 446 | /// The HEVC seat, missing from an older plugin for the same reason and at |
| 445 | /// the same cost: an HEVC export then runs on the software intra encoder. | 447 | /// the same cost: an HEVC export then runs on the software intra encoder. |
| 446 | - open_hevc: Option<extern "C" fn(u32, u32, u32, u32, u64) -> *mut c_void>, | 448 | + /// Four arguments more than the others: what the SPS says the samples mean |
| 449 | + /// ([`HwEncoder::open_hevc`]), which is why the symbol is named apart from | ||
| 450 | + /// the colourless `vh_enc_hevc_open` it replaced rather than sharing its | ||
| 451 | + /// name with another signature. | ||
| 452 | + open_hevc: Option<extern "C" fn(u32, u32, u32, u32, u64, u32, u32, u32, i32) -> *mut c_void>, | ||
| 447 | /// The **intra-only** H.264 seat, missing from an older plugin for the same | 453 | /// The **intra-only** H.264 seat, missing from an older plugin for the same |
| 448 | /// reason and at the same cost: a proxy then runs on the software encoder, | 454 | /// reason and at the same cost: a proxy then runs on the software encoder, |
| 449 | /// which codes one frame a GOP and so writes the same kind of file. | 455 | /// which codes one frame a GOP and so writes the same kind of file. |
| @@ -480,7 +486,7 @@ fn load_enc() -> Option<EncPlugin> { | |||
| 480 | Some(EncPlugin { | 486 | Some(EncPlugin { |
| 481 | open: *lib.get(b"vh_enc_open\0").ok()?, | 487 | open: *lib.get(b"vh_enc_open\0").ok()?, |
| 482 | open_av1: lib.get(b"vh_enc_av1_open\0").ok().map(|s| *s), | 488 | open_av1: lib.get(b"vh_enc_av1_open\0").ok().map(|s| *s), |
| 483 | - open_hevc: lib.get(b"vh_enc_hevc_open\0").ok().map(|s| *s), | 489 | + open_hevc: lib.get(b"vh_enc_hevc_open_colour\0").ok().map(|s| *s), |
| 484 | open_intra: lib.get(b"vh_enc_open_intra\0").ok().map(|s| *s), | 490 | open_intra: lib.get(b"vh_enc_open_intra\0").ok().map(|s| *s), |
| 485 | frame: *lib.get(b"vh_enc_frame\0").ok()?, | 491 | frame: *lib.get(b"vh_enc_frame\0").ok()?, |
| 486 | dma_geometry: lib.get(b"vh_enc_dma_geometry\0").ok().map(|s| *s), | 492 | dma_geometry: lib.get(b"vh_enc_dma_geometry\0").ok().map(|s| *s), |
| @@ -575,16 +581,38 @@ impl HwEncoder { | |||
| 575 | /// too old to export the symbol, on a GPU with no HEVC encode entrypoint, | 581 | /// too old to export the symbol, on a GPU with no HEVC encode entrypoint, |
| 576 | /// and below 384x384 -- the driver's own floor, stated in the plugin. The | 582 | /// and below 384x384 -- the driver's own floor, stated in the plugin. The |
| 577 | /// caller then takes the software intra encoder without a word about it. | 583 | /// caller then takes the software intra encoder without a word about it. |
| 584 | + /// | ||
| 585 | + /// `colour` is what the SPS will say the samples mean, and it is taken at | ||
| 586 | + /// the open because the parameter sets are written when the sequence starts. | ||
| 587 | + /// libavcodec's HEVC decoder overwrites a container's colour with the | ||
| 588 | + /// bitstream's, so a seat that signalled nothing hands a player | ||
| 589 | + /// "unspecified" -- BT.601 -- however the file is tagged. | ||
| 578 | pub fn open_hevc( | 590 | pub fn open_hevc( |
| 579 | width: u32, | 591 | width: u32, |
| 580 | height: u32, | 592 | height: u32, |
| 581 | fps_num: u32, | 593 | fps_num: u32, |
| 582 | fps_den: u32, | 594 | fps_den: u32, |
| 583 | bitrate: u64, | 595 | bitrate: u64, |
| 596 | + colour: ColorDescription, | ||
| 584 | ) -> Option<Self> { | 597 | ) -> Option<Self> { |
| 585 | let plugin = enc_plugin()?; | 598 | let plugin = enc_plugin()?; |
| 586 | let open = plugin.open_hevc?; | 599 | let open = plugin.open_hevc?; |
| 587 | - Self::opened(plugin, open, width, height, fps_num, fps_den, bitrate) | 600 | + let (primaries, transfer, matrix) = colour.codes(); |
| 601 | + let handle = open( | ||
| 602 | + width, | ||
| 603 | + height, | ||
| 604 | + fps_num, | ||
| 605 | + fps_den, | ||
| 606 | + bitrate, | ||
| 607 | + u32::from(primaries), | ||
| 608 | + u32::from(transfer), | ||
| 609 | + u32::from(matrix), | ||
| 610 | + i32::from(colour.full_range), | ||
| 611 | + ); | ||
| 612 | + if handle.is_null() { | ||
| 613 | + return None; | ||
| 614 | + } | ||
| 615 | + Some(Self { plugin, handle }) | ||
| 588 | } | 616 | } |
| 589 | 617 | ||
| 590 | /// The null check both opens share, which is the plugin's whole way of | 618 | /// The null check both opens share, which is the plugin's whole way of |
vendor/cros-codecs/src/encoder/h265.rsmodified+15 -0
| @@ -10,6 +10,17 @@ use crate::Resolution; | |||
| 10 | 10 | ||
| 11 | pub struct H265; | 11 | pub struct H265; |
| 12 | 12 | ||
| 13 | +/// What the SPS says the coded samples mean, as H.273 code points, and whether | ||
| 14 | +/// the luma codes cover the full range. Written into the VUI, which is the | ||
| 15 | +/// answer a decoder takes before it looks at any container tag. | ||
| 16 | +#[derive(Clone, Copy)] | ||
| 17 | +pub struct ColourDescription { | ||
| 18 | + pub colour_primaries: u32, | ||
| 19 | + pub transfer_characteristics: u32, | ||
| 20 | + pub matrix_coeffs: u32, | ||
| 21 | + pub full_range: bool, | ||
| 22 | +} | ||
| 23 | + | ||
| 13 | #[derive(Clone)] | 24 | #[derive(Clone)] |
| 14 | pub struct EncoderConfig { | 25 | pub struct EncoderConfig { |
| 15 | pub resolution: Resolution, | 26 | pub resolution: Resolution, |
| @@ -18,6 +29,9 @@ pub struct EncoderConfig { | |||
| 18 | pub pred_structure: PredictionStructure, | 29 | pub pred_structure: PredictionStructure, |
| 19 | /// Initial tunings values | 30 | /// Initial tunings values |
| 20 | pub initial_tunings: Tunings, | 31 | pub initial_tunings: Tunings, |
| 32 | + /// What the VUI declares about the samples. `None` writes no video signal | ||
| 33 | + /// type at all, and a stream that says nothing is read as "unspecified". | ||
| 34 | + pub colour: Option<ColourDescription>, | ||
| 21 | } | 35 | } |
| 22 | 36 | ||
| 23 | impl Default for EncoderConfig { | 37 | impl Default for EncoderConfig { |
| @@ -29,6 +43,7 @@ impl Default for EncoderConfig { | |||
| 29 | level: Level::L4, | 43 | level: Level::L4, |
| 30 | pred_structure: PredictionStructure::LowDelay { limit: 2048 }, | 44 | pred_structure: PredictionStructure::LowDelay { limit: 2048 }, |
| 31 | initial_tunings: Default::default(), | 45 | initial_tunings: Default::default(), |
| 46 | + colour: None, | ||
| 32 | } | 47 | } |
| 33 | } | 48 | } |
| 34 | } | 49 | } |
vendor/cros-codecs/src/encoder/stateless/h265/predictor.rsmodified+15 -3
| @@ -131,7 +131,7 @@ impl<Picture, Reference> LowDelayH265<Picture, Reference> { | |||
| 131 | 131 | ||
| 132 | // H.265 Table 6-1, the encoder only supports 4:2:0 subsampling. Must be set before | 132 | // H.265 Table 6-1, the encoder only supports 4:2:0 subsampling. Must be set before |
| 133 | // `resolution()`, which derives the conformance window from it. | 133 | // `resolution()`, which derives the conformance window from it. |
| 134 | - let sps = SpsBuilder::new(Rc::clone(&vps)) | 134 | + let mut sps = SpsBuilder::new(Rc::clone(&vps)) |
| 135 | .seq_parameter_set_id(0) | 135 | .seq_parameter_set_id(0) |
| 136 | .chroma_format_idc(1) | 136 | .chroma_format_idc(1) |
| 137 | .resolution(coded_width, coded_height) | 137 | .resolution(coded_width, coded_height) |
| @@ -150,8 +150,20 @@ impl<Picture, Reference> LowDelayH265<Picture, Reference> { | |||
| 150 | .max_latency_increase_plus1(0) | 150 | .max_latency_increase_plus1(0) |
| 151 | .short_term_ref_pic_set(short_term_ref_pic_set) | 151 | .short_term_ref_pic_set(short_term_ref_pic_set) |
| 152 | .long_term_ref_pics_present_flag(false) | 152 | .long_term_ref_pics_present_flag(false) |
| 153 | - .timing_info(1, self.tunings.framerate, false) | 153 | + .timing_info(1, self.tunings.framerate, false); |
| 154 | - .build(); | 154 | + |
| 155 | + // What the samples mean, where the caller declared it. `video_format` is | ||
| 156 | + // 5, "unspecified": the field is the analogue system a picture came off | ||
| 157 | + // and an encoded timeline came off none. | ||
| 158 | + if let Some(colour) = config.colour { | ||
| 159 | + sps = sps.video_signal_type(5, colour.full_range).colour_description( | ||
| 160 | + colour.colour_primaries, | ||
| 161 | + colour.transfer_characteristics, | ||
| 162 | + colour.matrix_coeffs, | ||
| 163 | + ); | ||
| 164 | + } | ||
| 165 | + | ||
| 166 | + let sps = sps.build(); | ||
| 155 | 167 | ||
| 156 | let min_qp = self.tunings.min_quality.max(MIN_QP as u32); | 168 | let min_qp = self.tunings.min_quality.max(MIN_QP as u32); |
| 157 | let max_qp = self.tunings.max_quality.min(MAX_QP as u32); | 169 | let max_qp = self.tunings.max_quality.min(MAX_QP as u32); |