edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·9b67700·Tahinli·3 days ago·4021 commits
| 1 | use vk_video_ffi::{Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref}; |
| 2 | |
| 3 | #[cfg(windows)] |
| 4 | use crate::D3d11Texture; |
| 5 | #[cfg(target_os = "linux")] |
| 6 | use crate::DmaBuf; |
| 7 | use crate::history::History; |
| 8 | use crate::{CodedFrame, Colour, Error, Flight, Gpu, Meta, RateControl, last_frame}; |
| 9 | |
| 10 | const TEMPORAL_DELIMITER: [u8; 2] = [0x12, 0x00]; |
| 11 | |
| 12 | /// Encoder settings. |
| 13 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 14 | pub struct Av1EncoderConfig { |
| 15 | /// Width in luma samples, multiple of 8. |
| 16 | pub width: u32, |
| 17 | /// Height in luma samples, multiple of 8. |
| 18 | pub height: u32, |
| 19 | /// Frame rate as numerator and denominator. |
| 20 | pub fps: (u32, u32), |
| 21 | /// Frames from one KEY to the next, at least 1. |
| 22 | pub gop_size: u32, |
| 23 | /// Rate control. |
| 24 | pub rate: RateControl, |
| 25 | /// Luma and chroma bit depth: 8, or 10 for Main 10 (`encode_16`). |
| 26 | pub bit_depth: u8, |
| 27 | /// `seq_level_idx`, clamped to the driver maximum. |
| 28 | pub level: u8, |
| 29 | /// Colour config of the sequence header, or none. |
| 30 | pub colour: Option<Colour>, |
| 31 | /// Speed/quality trade-off as in ec-hw's `EncoderConfig::quality`: 0 for the default level, 1 for the driver's best, each step up one level faster. |
| 32 | pub quality: u32, |
| 33 | } |
| 34 | |
| 35 | /// AV1 encoder: order hint, DPB slot and LAST reference bookkeeping over [`Av1EncodeSession`]. |
| 36 | pub struct Av1Encoder { |
| 37 | session: Av1EncodeSession, |
| 38 | sequence_header: Vec<u8>, |
| 39 | width: usize, |
| 40 | height: usize, |
| 41 | gop_size: u32, |
| 42 | position: u32, |
| 43 | order_hint: u8, |
| 44 | history: History<u8>, |
| 45 | uv: Vec<u8>, |
| 46 | uv16: Vec<u16>, |
| 47 | flight: Flight, |
| 48 | } |
| 49 | |
| 50 | impl Av1Encoder { |
| 51 | /// Opens an encoder session on `gpu`. |
| 52 | pub fn new(gpu: &Gpu, cfg: Av1EncoderConfig) -> Result<Self, Error> { |
| 53 | if cfg.gop_size == 0 { |
| 54 | return Err(Error::Invalid("gop_size is zero".into())); |
| 55 | } |
| 56 | let session = Av1EncodeSession::new( |
| 57 | gpu, |
| 58 | &Av1EncodeConfig { |
| 59 | width: cfg.width, |
| 60 | height: cfg.height, |
| 61 | fps: cfg.fps, |
| 62 | rate: cfg.rate, |
| 63 | bit_depth: cfg.bit_depth, |
| 64 | level: cfg.level, |
| 65 | colour: cfg.colour, |
| 66 | quality: cfg.quality, |
| 67 | }, |
| 68 | )?; |
| 69 | let sequence_header = session.sequence_header(); |
| 70 | Ok(Av1Encoder { |
| 71 | session, |
| 72 | sequence_header, |
| 73 | width: cfg.width as usize, |
| 74 | height: cfg.height as usize, |
| 75 | gop_size: cfg.gop_size, |
| 76 | position: 0, |
| 77 | order_hint: 0, |
| 78 | history: History::new(), |
| 79 | uv: vec![0; cfg.width as usize * cfg.height as usize / 2], |
| 80 | uv16: vec![0; cfg.width as usize * cfg.height as usize / 2], |
| 81 | flight: Flight::default(), |
| 82 | }) |
| 83 | } |
| 84 | |
| 85 | /// Submits one I420 frame without waiting; returns the oldest temporal unit once a second one is queued. |
| 86 | pub fn submit( |
| 87 | &mut self, |
| 88 | y: &[u8], |
| 89 | u: &[u8], |
| 90 | v: &[u8], |
| 91 | force_keyframe: bool, |
| 92 | ) -> Result<Option<CodedFrame>, Error> { |
| 93 | let chroma = self.width * self.height / 4; |
| 94 | if y.len() != self.width * self.height || u.len() != chroma || v.len() != chroma { |
| 95 | return Err(Error::Invalid("I420 plane size".into())); |
| 96 | } |
| 97 | for (i, pair) in self.uv.chunks_exact_mut(2).enumerate() { |
| 98 | pair[0] = u[i]; |
| 99 | pair[1] = v[i]; |
| 100 | } |
| 101 | let pic = self.next_picture(force_keyframe); |
| 102 | let out = self.session.submit(y, &self.uv, &pic)?; |
| 103 | Ok(out.map(|bytes| self.coded(bytes))) |
| 104 | } |
| 105 | |
| 106 | /// Encodes one I420 frame as a temporal unit; `force_keyframe` makes it a KEY frame and restarts the GOP. |
| 107 | pub fn encode( |
| 108 | &mut self, |
| 109 | y: &[u8], |
| 110 | u: &[u8], |
| 111 | v: &[u8], |
| 112 | force_keyframe: bool, |
| 113 | ) -> Result<CodedFrame, Error> { |
| 114 | let first = self.submit(y, u, v, force_keyframe)?; |
| 115 | let rest = self.flush()?; |
| 116 | last_frame(first, rest) |
| 117 | } |
| 118 | |
| 119 | /// Submits one 10-bit I420 frame of a 10-bit encoder without waiting. |
| 120 | pub fn submit_16( |
| 121 | &mut self, |
| 122 | y: &[u16], |
| 123 | u: &[u16], |
| 124 | v: &[u16], |
| 125 | force_keyframe: bool, |
| 126 | ) -> Result<Option<CodedFrame>, Error> { |
| 127 | let chroma = self.width * self.height / 4; |
| 128 | if y.len() != self.width * self.height || u.len() != chroma || v.len() != chroma { |
| 129 | return Err(Error::Invalid("I420 plane size".into())); |
| 130 | } |
| 131 | for (i, pair) in self.uv16.chunks_exact_mut(2).enumerate() { |
| 132 | pair[0] = u[i]; |
| 133 | pair[1] = v[i]; |
| 134 | } |
| 135 | let pic = self.next_picture(force_keyframe); |
| 136 | let out = self.session.submit_16(y, &self.uv16, &pic)?; |
| 137 | Ok(out.map(|bytes| self.coded(bytes))) |
| 138 | } |
| 139 | |
| 140 | /// Encodes one 10-bit I420 frame of a 10-bit encoder; `force_keyframe` makes it a KEY frame and restarts the GOP. |
| 141 | pub fn encode_16( |
| 142 | &mut self, |
| 143 | y: &[u16], |
| 144 | u: &[u16], |
| 145 | v: &[u16], |
| 146 | force_keyframe: bool, |
| 147 | ) -> Result<CodedFrame, Error> { |
| 148 | let first = self.submit_16(y, u, v, force_keyframe)?; |
| 149 | let rest = self.flush()?; |
| 150 | last_frame(first, rest) |
| 151 | } |
| 152 | |
| 153 | /// Waits for every submitted frame and returns them in order. |
| 154 | pub fn flush(&mut self) -> Result<Vec<CodedFrame>, Error> { |
| 155 | let bytes = self.session.flush()?; |
| 156 | Ok(bytes.into_iter().map(|b| self.coded(b)).collect()) |
| 157 | } |
| 158 | |
| 159 | /// Changes bitrate or frame rate; takes effect on the next frame. |
| 160 | pub fn set_rate(&mut self, rate: RateControl, fps: (u32, u32)) -> Result<(), Error> { |
| 161 | Ok(self.session.set_rate(rate, fps)?) |
| 162 | } |
| 163 | |
| 164 | /// Submits one packed RGB frame (src_width x src_height, BGRx if `bgr`, else RGBx; row stride in bytes) without waiting. |
| 165 | pub fn submit_rgb( |
| 166 | &mut self, |
| 167 | rgb: &[u8], |
| 168 | src_width: u32, |
| 169 | src_height: u32, |
| 170 | stride_bytes: u32, |
| 171 | bgr: bool, |
| 172 | force_keyframe: bool, |
| 173 | ) -> Result<Option<CodedFrame>, Error> { |
| 174 | let pic = self.next_picture(force_keyframe); |
| 175 | let out = self |
| 176 | .session |
| 177 | .submit_rgb(rgb, src_width, src_height, stride_bytes, bgr, &pic)?; |
| 178 | Ok(out.map(|bytes| self.coded(bytes))) |
| 179 | } |
| 180 | |
| 181 | /// Encodes one packed RGB frame (src_width x src_height, BGRx if `bgr`, else RGBx; row stride in bytes), box-downscaled to the encoder size on the GPU. |
| 182 | pub fn encode_rgb( |
| 183 | &mut self, |
| 184 | rgb: &[u8], |
| 185 | src_width: u32, |
| 186 | src_height: u32, |
| 187 | stride_bytes: u32, |
| 188 | bgr: bool, |
| 189 | force_keyframe: bool, |
| 190 | ) -> Result<CodedFrame, Error> { |
| 191 | let first = self.submit_rgb(rgb, src_width, src_height, stride_bytes, bgr, force_keyframe)?; |
| 192 | let rest = self.flush()?; |
| 193 | last_frame(first, rest) |
| 194 | } |
| 195 | |
| 196 | /// Submits one shared D3D11 texture of the source size without waiting. |
| 197 | #[cfg(windows)] |
| 198 | pub fn submit_d3d11( |
| 199 | &mut self, |
| 200 | src: &D3d11Texture<'_>, |
| 201 | force_keyframe: bool, |
| 202 | ) -> Result<Option<CodedFrame>, Error> { |
| 203 | let pic = self.next_picture(force_keyframe); |
| 204 | let out = self.session.submit_d3d11(src, &pic)?; |
| 205 | Ok(out.map(|bytes| self.coded(bytes))) |
| 206 | } |
| 207 | |
| 208 | /// Encodes one shared D3D11 texture of the source size (BGRA if `bgr`, else RGBA), downscaled to the encoder size on the GPU. |
| 209 | #[cfg(windows)] |
| 210 | pub fn encode_d3d11( |
| 211 | &mut self, |
| 212 | src: &D3d11Texture<'_>, |
| 213 | force_keyframe: bool, |
| 214 | ) -> Result<CodedFrame, Error> { |
| 215 | let first = self.submit_d3d11(src, force_keyframe)?; |
| 216 | let rest = self.flush()?; |
| 217 | last_frame(first, rest) |
| 218 | } |
| 219 | |
| 220 | /// Submits one DMA-BUF frame of the source size without waiting. |
| 221 | #[cfg(target_os = "linux")] |
| 222 | pub fn submit_dmabuf( |
| 223 | &mut self, |
| 224 | src: &DmaBuf<'_>, |
| 225 | force_keyframe: bool, |
| 226 | ) -> Result<Option<CodedFrame>, Error> { |
| 227 | let pic = self.next_picture(force_keyframe); |
| 228 | let out = self.session.submit_dmabuf(src, &pic)?; |
| 229 | Ok(out.map(|bytes| self.coded(bytes))) |
| 230 | } |
| 231 | |
| 232 | /// Encodes one DMA-BUF frame of the source size (BGRx if `bgr`, else RGBx), box-downscaled to the encoder size on the GPU. |
| 233 | #[cfg(target_os = "linux")] |
| 234 | pub fn encode_dmabuf( |
| 235 | &mut self, |
| 236 | src: &DmaBuf<'_>, |
| 237 | force_keyframe: bool, |
| 238 | ) -> Result<CodedFrame, Error> { |
| 239 | let first = self.submit_dmabuf(src, force_keyframe)?; |
| 240 | let rest = self.flush()?; |
| 241 | last_frame(first, rest) |
| 242 | } |
| 243 | |
| 244 | fn next_picture(&mut self, force_keyframe: bool) -> Av1Picture { |
| 245 | let step = self |
| 246 | .history |
| 247 | .next(force_keyframe || self.position >= self.gop_size); |
| 248 | self.order_hint = if step.key { |
| 249 | 0 |
| 250 | } else { |
| 251 | self.order_hint.wrapping_add(1) |
| 252 | }; |
| 253 | self.history.store(&step, self.order_hint); |
| 254 | self.position = if step.key { 1 } else { self.position + 1 }; |
| 255 | self.flight.push(Meta { |
| 256 | keyframe: step.key, |
| 257 | recovery: step.recovery, |
| 258 | id: step.id, |
| 259 | }); |
| 260 | Av1Picture { |
| 261 | key: step.key, |
| 262 | order_hint: self.order_hint, |
| 263 | slot: step.slot, |
| 264 | reference: step.reference.map(|(slot, order_hint)| Av1Ref { slot, order_hint }), |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /// The receiver decoded every frame up to `id` ([`CodedFrame::id`]); a recovery frame may reference one while it stays in the DPB. |
| 269 | pub fn ack(&mut self, id: u64) { |
| 270 | self.history.ack(id); |
| 271 | } |
| 272 | |
| 273 | /// Make the next frame reference the newest acknowledged frame still held, or a KEY frame when none is. |
| 274 | pub fn recover(&mut self) { |
| 275 | self.history.arm(); |
| 276 | } |
| 277 | |
| 278 | fn coded(&mut self, bytes: Vec<u8>) -> CodedFrame { |
| 279 | let meta = self.flight.pop(); |
| 280 | let mut data = TEMPORAL_DELIMITER.to_vec(); |
| 281 | if meta.keyframe { |
| 282 | data.extend_from_slice(&self.sequence_header); |
| 283 | } |
| 284 | data.extend_from_slice(&bytes); |
| 285 | CodedFrame { |
| 286 | data, |
| 287 | keyframe: meta.keyframe, |
| 288 | recovery: meta.recovery, |
| 289 | id: meta.id, |
| 290 | } |
| 291 | } |
| 292 | } |