A third encode seat, and it writes HEVC
Browse files at this commit76787eb296bc137104c607ab8bce21df670157d1Parent: 75da480- feat
- a third encode seat, and it writes HEVC
1 changed file +133 -25
- crates/engine-hw/src/lib.rs +133 -25
crates/engine-hw/src/lib.rsmodified+133 -25
| @@ -1,5 +1,5 @@ | |||
| 1 | -//! VA-API decode (H.264, HEVC, VP9 and AV1) and encode (H.264, and AV1 where | 1 | +//! VA-API decode (H.264, HEVC, VP9 and AV1) and encode (H.264 and HEVC, and AV1 |
| 2 | -//! the GPU has an entrypoint for it), shipped as a | 2 | +//! where the GPU has an entrypoint for it), shipped as a |
| 3 | //! `dlopen`-able plugin so the main binary never gets a DT_NEEDED on | 3 | //! `dlopen`-able plugin so the main binary never gets a DT_NEEDED on |
| 4 | //! libva/gbm/drm. Every entry point is | 4 | //! libva/gbm/drm. Every entry point is |
| 5 | //! `extern "C"`, catches unwinds and reports failure as a null pointer or a | 5 | //! `extern "C"`, catches unwinds and reports failure as a null pointer or a |
| @@ -20,6 +20,7 @@ use cros_codecs::backend::vaapi::decoder::VaapiBackend; | |||
| 20 | use cros_codecs::backend::vaapi::encoder::VaapiBackend as VaapiEncBackend; | 20 | use cros_codecs::backend::vaapi::encoder::VaapiBackend as VaapiEncBackend; |
| 21 | use cros_codecs::codec::av1::parser::Profile as Av1Profile; | 21 | use cros_codecs::codec::av1::parser::Profile as Av1Profile; |
| 22 | use cros_codecs::codec::h264::parser::{Level, Profile as H264Profile}; | 22 | use cros_codecs::codec::h264::parser::{Level, Profile as H264Profile}; |
| 23 | +use cros_codecs::codec::h265::parser::{Level as H265Level, Profile as H265Profile}; | ||
| 23 | use cros_codecs::decoder::stateless::av1::Av1; | 24 | use cros_codecs::decoder::stateless::av1::Av1; |
| 24 | use cros_codecs::decoder::stateless::h264::H264; | 25 | use cros_codecs::decoder::stateless::h264::H264; |
| 25 | use cros_codecs::decoder::stateless::h265::H265; | 26 | use cros_codecs::decoder::stateless::h265::H265; |
| @@ -28,8 +29,10 @@ use cros_codecs::decoder::stateless::{DecodeError, StatelessDecoder, StatelessVi | |||
| 28 | use cros_codecs::decoder::{BlockingMode, DecodedHandle, DecoderEvent, StreamInfo}; | 29 | use cros_codecs::decoder::{BlockingMode, DecodedHandle, DecoderEvent, StreamInfo}; |
| 29 | use cros_codecs::encoder::av1::EncoderConfig as Av1Config; | 30 | use cros_codecs::encoder::av1::EncoderConfig as Av1Config; |
| 30 | use cros_codecs::encoder::h264::EncoderConfig as H264Config; | 31 | use cros_codecs::encoder::h264::EncoderConfig as H264Config; |
| 32 | +use cros_codecs::encoder::h265::EncoderConfig as H265Config; | ||
| 31 | use cros_codecs::encoder::stateless::av1::StatelessEncoder as Av1StatelessEncoder; | 33 | use cros_codecs::encoder::stateless::av1::StatelessEncoder as Av1StatelessEncoder; |
| 32 | use cros_codecs::encoder::stateless::h264::StatelessEncoder; | 34 | use cros_codecs::encoder::stateless::h264::StatelessEncoder; |
| 35 | +use cros_codecs::encoder::stateless::h265::StatelessEncoder as H265StatelessEncoder; | ||
| 33 | use cros_codecs::encoder::{ | 36 | use cros_codecs::encoder::{ |
| 34 | FrameMetadata, PredictionStructure, RateControl, Tunings, VideoEncoder, | 37 | FrameMetadata, PredictionStructure, RateControl, Tunings, VideoEncoder, |
| 35 | }; | 38 | }; |
| @@ -98,6 +101,10 @@ type Av1Encoder = Av1StatelessEncoder< | |||
| 98 | GenericDmaVideoFrame, | 101 | GenericDmaVideoFrame, |
| 99 | VaapiEncBackend<GenericDmaVideoFrame, Surface<GenericDmaVideoFrame>>, | 102 | VaapiEncBackend<GenericDmaVideoFrame, Surface<GenericDmaVideoFrame>>, |
| 100 | >; | 103 | >; |
| 104 | +type HevcEncoder = H265StatelessEncoder< | ||
| 105 | + GenericDmaVideoFrame, | ||
| 106 | + VaapiEncBackend<GenericDmaVideoFrame, Surface<GenericDmaVideoFrame>>, | ||
| 107 | +>; | ||
| 101 | 108 | ||
| 102 | /// Ceiling on decode/drain iterations that make no progress. VA-API in blocking | 109 | /// Ceiling on decode/drain iterations that make no progress. VA-API in blocking |
| 103 | /// mode should always either hand back an event or accept input; this only | 110 | /// mode should always either hand back an event or accept input; this only |
| @@ -608,8 +615,8 @@ pub unsafe extern "C" fn vh_close(session: *mut c_void) { | |||
| 608 | 615 | ||
| 609 | /// What each codec needs of the driver before this plugin can claim it: the | 616 | /// What each codec needs of the driver before this plugin can claim it: the |
| 610 | /// 8-bit decode profiles, the 10-bit ones, and the profile the *encoder* here | 617 | /// 8-bit decode profiles, the 10-bit ones, and the profile the *encoder* here |
| 611 | -/// would open -- `None` for the two this plugin has no encoder for at all, so a | 618 | +/// would open -- `None` for the one this plugin has no encoder for at all, so a |
| 612 | -/// GPU with an HEVC or VP9 encode entrypoint is never reported as one edith can | 619 | +/// GPU with a VP9 encode entrypoint is never reported as one edith can |
| 613 | /// reach. The encode profiles are the very ones [`EncSession::open_codec`] | 620 | /// reach. The encode profiles are the very ones [`EncSession::open_codec`] |
| 614 | /// asks for, which is what keeps this a description of the plugin rather than a | 621 | /// asks for, which is what keeps this a description of the plugin rather than a |
| 615 | /// second opinion about it. | 622 | /// second opinion about it. |
| @@ -628,7 +635,11 @@ const CAP_TABLE: [(u32, &[VAProfile::Type], &[VAProfile::Type], Option<VAProfile | |||
| 628 | CAP_HEVC, | 635 | CAP_HEVC, |
| 629 | &[VAProfile::VAProfileHEVCMain], | 636 | &[VAProfile::VAProfileHEVCMain], |
| 630 | &[VAProfile::VAProfileHEVCMain10], | 637 | &[VAProfile::VAProfileHEVCMain10], |
| 631 | - None, | 638 | + // 8-bit Main only: the encoder here opens Main whatever the source was, |
| 639 | + // so a Main 10 entrypoint is a *decode* claim on this line and nothing | ||
| 640 | + // more. Main 10 encode would be a second profile here and a 10-bit input | ||
| 641 | + // buffer in `EncSession`, neither of which exists yet. | ||
| 642 | + Some(VAProfile::VAProfileHEVCMain), | ||
| 632 | ), | 643 | ), |
| 633 | ( | 644 | ( |
| 634 | CAP_VP9, | 645 | CAP_VP9, |
| @@ -762,13 +773,15 @@ fn fix_slice_nal_headers(au: &mut [u8]) { | |||
| 762 | /// point polls it before returning, so the GPU is finished with the buffer | 773 | /// point polls it before returning, so the GPU is finished with the buffer |
| 763 | /// before the next picture is written into it. | 774 | /// before the next picture is written into it. |
| 764 | struct EncSession { | 775 | struct EncSession { |
| 765 | - /// Boxed because the two codecs are two types and everything past `encode` | 776 | + /// Boxed because the three codecs are three types and everything past |
| 766 | - /// is the same trait: one session type serves both, which is what keeps the | 777 | + /// `encode` is the same trait: one session type serves them all, which is |
| 767 | - /// C ABI at one new symbol (the open) instead of four. | 778 | + /// what keeps the C ABI at one new symbol per codec (the open) instead of |
| 779 | + /// four. | ||
| 768 | encoder: Box<dyn VideoEncoder<GenericDmaVideoFrame>>, | 780 | encoder: Box<dyn VideoEncoder<GenericDmaVideoFrame>>, |
| 769 | - /// H.264 only: the byte the driver leaves at zero is a *NAL* header, and an | 781 | + /// Which codec the open picked, kept because one thing past it still |
| 770 | - /// AV1 temporal unit has no such thing. | 782 | + /// differs: the H.264 driver leaves a *NAL* header byte at zero, and |
| 771 | - h264: bool, | 783 | + /// neither an HEVC access unit nor an AV1 temporal unit has that one. |
| 784 | + codec: EncCodec, | ||
| 772 | /// Owns the DRM node the buffer object below was allocated from. | 785 | /// Owns the DRM node the buffer object below was allocated from. |
| 773 | _gbm: gbm::Device<std::fs::File>, | 786 | _gbm: gbm::Device<std::fs::File>, |
| 774 | frame: GenericDmaVideoFrame, | 787 | frame: GenericDmaVideoFrame, |
| @@ -786,9 +799,18 @@ struct EncSession { | |||
| 786 | out: Vec<u8>, | 799 | out: Vec<u8>, |
| 787 | } | 800 | } |
| 788 | 801 | ||
| 802 | +/// Which codec an [`EncSession`] was opened for. Decided once, at the open, the | ||
| 803 | +/// way the H.264 seat's dimensions are -- everything after it is one trait. | ||
| 804 | +#[derive(Clone, Copy, PartialEq, Eq)] | ||
| 805 | +enum EncCodec { | ||
| 806 | + H264, | ||
| 807 | + Hevc, | ||
| 808 | + Av1, | ||
| 809 | +} | ||
| 810 | + | ||
| 789 | impl EncSession { | 811 | impl EncSession { |
| 790 | fn open(width: u32, height: u32, fps_num: u32, fps_den: u32, bitrate: u64) -> Option<Self> { | 812 | fn open(width: u32, height: u32, fps_num: u32, fps_den: u32, bitrate: u64) -> Option<Self> { |
| 791 | - Self::open_codec(width, height, fps_num, fps_den, bitrate, false) | 813 | + Self::open_codec(width, height, fps_num, fps_den, bitrate, EncCodec::H264) |
| 792 | } | 814 | } |
| 793 | 815 | ||
| 794 | fn open_codec( | 816 | fn open_codec( |
| @@ -797,13 +819,21 @@ impl EncSession { | |||
| 797 | fps_num: u32, | 819 | fps_num: u32, |
| 798 | fps_den: u32, | 820 | fps_den: u32, |
| 799 | bitrate: u64, | 821 | bitrate: u64, |
| 800 | - av1: bool, | 822 | + codec: EncCodec, |
| 801 | ) -> Option<Self> { | 823 | ) -> Option<Self> { |
| 802 | // NV12 chroma is half resolution, so odd dimensions have no packing; | 824 | // NV12 chroma is half resolution, so odd dimensions have no packing; |
| 803 | // and radeonsi refuses encode contexts below 64x64 (measured). | 825 | // and radeonsi refuses encode contexts below 64x64 (measured). |
| 804 | if width < 64 || height < 64 || width % 2 != 0 || height % 2 != 0 { | 826 | if width < 64 || height < 64 || width % 2 != 0 || height % 2 != 0 { |
| 805 | return None; | 827 | return None; |
| 806 | } | 828 | } |
| 829 | + // The HEVC entrypoint's own floor, which is a different number: the same | ||
| 830 | + // driver that takes a 128x128 H.264 context answers `VaError(19)` to an | ||
| 831 | + // HEVC one below 384x384 (measured 2026-08-13 on radeonsi). Said here so | ||
| 832 | + // a small export falls back to software at the open rather than through | ||
| 833 | + // a driver refusal, and so the C ABI never claims a seat it has not got. | ||
| 834 | + if codec == EncCodec::Hevc && (width < 384 || height < 384) { | ||
| 835 | + return None; | ||
| 836 | + } | ||
| 807 | if fps_num == 0 || fps_den == 0 || bitrate == 0 { | 837 | if fps_num == 0 || fps_den == 0 || bitrate == 0 { |
| 808 | return None; | 838 | return None; |
| 809 | } | 839 | } |
| @@ -813,9 +843,10 @@ impl EncSession { | |||
| 813 | height: align16(height), | 843 | height: align16(height), |
| 814 | }; | 844 | }; |
| 815 | let framerate = ((fps_num as f64 / fps_den as f64).round() as i64).clamp(1, 240) as u32; | 845 | let framerate = ((fps_num as f64 / fps_den as f64).round() as i64).clamp(1, 240) as u32; |
| 816 | - let profile = match av1 { | 846 | + let profile = match codec { |
| 817 | - true => VAProfile::VAProfileAV1Profile0, | 847 | + EncCodec::Av1 => VAProfile::VAProfileAV1Profile0, |
| 818 | - false => VAProfile::VAProfileH264Main, | 848 | + EncCodec::Hevc => VAProfile::VAProfileHEVCMain, |
| 849 | + EncCodec::H264 => VAProfile::VAProfileH264Main, | ||
| 819 | }; | 850 | }; |
| 820 | // The driver's own answer to "can you encode this at all": a GPU with no | 851 | // The driver's own answer to "can you encode this at all": a GPU with no |
| 821 | // AV1 encode entrypoint is what makes the caller fall back to `rav1e`, | 852 | // AV1 encode entrypoint is what makes the caller fall back to `rav1e`, |
| @@ -833,8 +864,8 @@ impl EncSession { | |||
| 833 | let pred_structure = PredictionStructure::LowDelay { | 864 | let pred_structure = PredictionStructure::LowDelay { |
| 834 | limit: (framerate * 2) as u16, | 865 | limit: (framerate * 2) as u16, |
| 835 | }; | 866 | }; |
| 836 | - let encoder: Box<dyn VideoEncoder<GenericDmaVideoFrame>> = match av1 { | 867 | + let encoder: Box<dyn VideoEncoder<GenericDmaVideoFrame>> = match codec { |
| 837 | - true => { | 868 | + EncCodec::Av1 => { |
| 838 | // Constant quality, because that is the only rate control the | 869 | // Constant quality, because that is the only rate control the |
| 839 | // vendored AV1 backend takes (`stateless/av1/vaapi.rs` refuses | 870 | // vendored AV1 backend takes (`stateless/av1/vaapi.rs` refuses |
| 840 | // anything else outright); 128 of 255 is its own reference | 871 | // anything else outright); 128 of 255 is its own reference |
| @@ -867,7 +898,50 @@ impl EncSession { | |||
| 867 | .ok()?, | 898 | .ok()?, |
| 868 | ) | 899 | ) |
| 869 | } | 900 | } |
| 870 | - false => { | 901 | + EncCodec::Hevc => { |
| 902 | + // **Every picture an IDR**, which is the one place this seat | ||
| 903 | + // does not mirror the H.264 one. A GOP's P slices come out of | ||
| 904 | + // this driver undecodable: the slice segment headers are the | ||
| 905 | + // *driver's* to write (as they are for H.264) and radeonsi | ||
| 906 | + // writes a constant picture order count into every one of them, | ||
| 907 | + // so a decoder sees frame after frame claiming to be the same | ||
| 908 | + // output picture. Measured 2026-08-13: with the default GOP, | ||
| 909 | + // only the leading IDR of each group decodes. | ||
| 910 | + // | ||
| 911 | + // So the hardware seat is intra-only exactly as the software one | ||
| 912 | + // is (`export::Enc::open_hevc`), and an HEVC export is an | ||
| 913 | + // intraframe master either way -- the same file, coded by the | ||
| 914 | + // GPU instead of by twelve cores. | ||
| 915 | + // | ||
| 916 | + // corner-cut: inter HEVC on this GPU needs the slice segment | ||
| 917 | + // header written here rather than by the driver | ||
| 918 | + // (`VAEncPackedHeaderSlice`), which is a change in the vendored | ||
| 919 | + // backend; upgrade path is that, or a driver that writes a | ||
| 920 | + // correct POC. | ||
| 921 | + let config = H265Config { | ||
| 922 | + resolution: Resolution { width, height }, | ||
| 923 | + profile: H265Profile::Main, | ||
| 924 | + level: H265Level::L4, | ||
| 925 | + pred_structure: PredictionStructure::LowDelay { limit: 1 }, | ||
| 926 | + initial_tunings: Tunings { | ||
| 927 | + rate_control: RateControl::ConstantBitrate(bitrate), | ||
| 928 | + framerate, | ||
| 929 | + ..Default::default() | ||
| 930 | + }, | ||
| 931 | + }; | ||
| 932 | + Box::new( | ||
| 933 | + HevcEncoder::new_vaapi( | ||
| 934 | + display, | ||
| 935 | + config, | ||
| 936 | + Fourcc::from(b"NV12"), | ||
| 937 | + coded, | ||
| 938 | + low_power, | ||
| 939 | + BlockingMode::Blocking, | ||
| 940 | + ) | ||
| 941 | + .ok()?, | ||
| 942 | + ) | ||
| 943 | + } | ||
| 944 | + EncCodec::H264 => { | ||
| 871 | let config = H264Config { | 945 | let config = H264Config { |
| 872 | resolution: Resolution { width, height }, | 946 | resolution: Resolution { width, height }, |
| 873 | profile: H264Profile::Main, | 947 | profile: H264Profile::Main, |
| @@ -898,7 +972,7 @@ impl EncSession { | |||
| 898 | let layout = nv12_layout(coded, frame.get_plane_pitch()[0], false); | 972 | let layout = nv12_layout(coded, frame.get_plane_pitch()[0], false); |
| 899 | Some(Self { | 973 | Some(Self { |
| 900 | encoder, | 974 | encoder, |
| 901 | - h264: !av1, | 975 | + codec, |
| 902 | _gbm: gbm, | 976 | _gbm: gbm, |
| 903 | frame, | 977 | frame, |
| 904 | layout, | 978 | layout, |
| @@ -981,7 +1055,7 @@ impl EncSession { | |||
| 981 | fn collect(&mut self) -> Result<(), String> { | 1055 | fn collect(&mut self) -> Result<(), String> { |
| 982 | while let Some(coded) = self.encoder.poll().map_err(|e| e.to_string())? { | 1056 | while let Some(coded) = self.encoder.poll().map_err(|e| e.to_string())? { |
| 983 | let mut au = coded.bitstream; | 1057 | let mut au = coded.bitstream; |
| 984 | - if self.h264 { | 1058 | + if self.codec == EncCodec::H264 { |
| 985 | fix_slice_nal_headers(&mut au); | 1059 | fix_slice_nal_headers(&mut au); |
| 986 | } | 1060 | } |
| 987 | self.ready.push_back(au); | 1061 | self.ready.push_back(au); |
| @@ -1044,7 +1118,37 @@ pub extern "C" fn vh_enc_av1_open( | |||
| 1044 | bitrate: u64, | 1118 | bitrate: u64, |
| 1045 | ) -> *mut c_void { | 1119 | ) -> *mut c_void { |
| 1046 | catch_unwind(AssertUnwindSafe(|| { | 1120 | catch_unwind(AssertUnwindSafe(|| { |
| 1047 | - match EncSession::open_codec(width, height, fps_num, fps_den, bitrate, true) { | 1121 | + match EncSession::open_codec(width, height, fps_num, fps_den, bitrate, EncCodec::Av1) { |
| 1122 | + Some(session) => Box::into_raw(Box::new(session)) as *mut c_void, | ||
| 1123 | + None => std::ptr::null_mut(), | ||
| 1124 | + } | ||
| 1125 | + })) | ||
| 1126 | + .unwrap_or(std::ptr::null_mut()) | ||
| 1127 | +} | ||
| 1128 | + | ||
| 1129 | +/// The same, coding HEVC: null unless this GPU has an HEVC encode entrypoint and | ||
| 1130 | +/// the picture is at least 384x384 ([`EncSession::open_codec`] states that floor | ||
| 1131 | +/// and where it was measured), and the caller then encodes HEVC in software | ||
| 1132 | +/// instead. | ||
| 1133 | +/// | ||
| 1134 | +/// **Intra-only**: every picture it hands back is an IDR carrying its own VPS, | ||
| 1135 | +/// SPS and PPS, which is what makes the file decodable at all on this driver -- | ||
| 1136 | +/// see the `EncCodec::Hevc` arm of [`EncSession::open_codec`]. `bitrate` is the | ||
| 1137 | +/// constant bitrate the GPU codes at, as the H.264 seat's is. | ||
| 1138 | +/// | ||
| 1139 | +/// The session it returns is fed, drained and closed through `vh_enc_frame`, | ||
| 1140 | +/// `vh_enc_drain` and `vh_enc_close` exactly as an H.264 one is; what comes back | ||
| 1141 | +/// out of them is Annex-B HEVC access units. | ||
| 1142 | +#[unsafe(no_mangle)] | ||
| 1143 | +pub extern "C" fn vh_enc_hevc_open( | ||
| 1144 | + width: u32, | ||
| 1145 | + height: u32, | ||
| 1146 | + fps_num: u32, | ||
| 1147 | + fps_den: u32, | ||
| 1148 | + bitrate: u64, | ||
| 1149 | +) -> *mut c_void { | ||
| 1150 | + catch_unwind(AssertUnwindSafe(|| { | ||
| 1151 | + match EncSession::open_codec(width, height, fps_num, fps_den, bitrate, EncCodec::Hevc) { | ||
| 1048 | Some(session) => Box::into_raw(Box::new(session)) as *mut c_void, | 1152 | Some(session) => Box::into_raw(Box::new(session)) as *mut c_void, |
| 1049 | None => std::ptr::null_mut(), | 1153 | None => std::ptr::null_mut(), |
| 1050 | } | 1154 | } |
| @@ -1160,9 +1264,13 @@ mod tests { | |||
| 1160 | return; | 1264 | return; |
| 1161 | }; | 1265 | }; |
| 1162 | eprintln!("{caps:?}"); | 1266 | eprintln!("{caps:?}"); |
| 1163 | - // Encode is this plugin's two seats and no others, whatever entrypoints | 1267 | + // Encode is this plugin's three seats and no others, whatever |
| 1164 | - // the driver has: an HEVC or VP9 EncSlice is not an encoder here. | 1268 | + // entrypoints the driver has: a VP9 EncSlice is not an encoder here. |
| 1165 | - assert_eq!(caps.encode & !(CAP_H264 | CAP_AV1), 0, "{caps:?}"); | 1269 | + assert_eq!( |
| 1270 | + caps.encode & !(CAP_H264 | CAP_HEVC | CAP_AV1), | ||
| 1271 | + 0, | ||
| 1272 | + "{caps:?}" | ||
| 1273 | + ); | ||
| 1166 | // A 10-bit claim is a decode claim -- same profile family, read back | 1274 | // A 10-bit claim is a decode claim -- same profile family, read back |
| 1167 | // through P010 -- and there is no 10-bit encode path at all. | 1275 | // through P010 -- and there is no 10-bit encode path at all. |
| 1168 | assert_eq!(caps.decode_10bit & !caps.decode, 0, "{caps:?}"); | 1276 | assert_eq!(caps.decode_10bit & !caps.decode, 0, "{caps:?}"); |