| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::collections::VecDeque; |
| 6 | |
| 7 | use thiserror::Error; |
| 8 | |
| 9 | use crate::encoder::CodedBitstreamBuffer; |
| 10 | use crate::encoder::EncodeError; |
| 11 | use crate::encoder::EncodeResult; |
| 12 | use crate::encoder::FrameMetadata; |
| 13 | use crate::encoder::Tunings; |
| 14 | use crate::encoder::VideoEncoder; |
| 15 | use crate::BlockingMode; |
| 16 | |
| 17 | #[cfg(feature = "vaapi")] |
| 18 | pub mod av1; |
| 19 | #[cfg(feature = "vaapi")] |
| 20 | pub mod h264; |
| 21 | #[cfg(feature = "vaapi")] |
| 22 | pub mod h265; |
| 23 | #[cfg(feature = "vaapi")] |
| 24 | pub(crate) mod predictor; |
| 25 | #[cfg(feature = "vaapi")] |
| 26 | pub mod vp9; |
| 27 | |
| 28 | #[derive(Error, Debug)] |
| 29 | pub enum StatelessBackendError { |
| 30 | #[error("unsupported profile")] |
| 31 | UnsupportedProfile, |
| 32 | #[error("unsupported format")] |
| 33 | UnsupportedFormat, |
| 34 | #[error("not enough resources to proceed with the operation now")] |
| 35 | OutOfResources, |
| 36 | #[error(transparent)] |
| 37 | Other(#[from] anyhow::Error), |
| 38 | } |
| 39 | |
| 40 | pub type StatelessBackendResult<T> = Result<T, StatelessBackendError>; |
| 41 | |
| 42 | |
| 43 | pub trait BackendPromise { |
| 44 | type Output; |
| 45 | |
| 46 | |
| 47 | fn sync(self) -> StatelessBackendResult<Self::Output>; |
| 48 | |
| 49 | |
| 50 | fn is_ready(&self) -> bool; |
| 51 | } |
| 52 | |
| 53 | pub struct ReadyPromise<T>(T); |
| 54 | |
| 55 | impl<T> From<T> for ReadyPromise<T> { |
| 56 | fn from(value: T) -> Self { |
| 57 | ReadyPromise(value) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | impl<T> BackendPromise for ReadyPromise<T> { |
| 62 | type Output = T; |
| 63 | |
| 64 | fn sync(self) -> StatelessBackendResult<Self::Output> { |
| 65 | Ok(self.0) |
| 66 | } |
| 67 | |
| 68 | fn is_ready(&self) -> bool { |
| 69 | true |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | |
| 74 | |
| 75 | pub struct BitstreamPromise<P> |
| 76 | where |
| 77 | P: BackendPromise<Output = Vec<u8>>, |
| 78 | { |
| 79 | |
| 80 | bitstream: P, |
| 81 | |
| 82 | |
| 83 | meta: FrameMetadata, |
| 84 | } |
| 85 | |
| 86 | impl<P> BackendPromise for BitstreamPromise<P> |
| 87 | where |
| 88 | P: BackendPromise<Output = Vec<u8>>, |
| 89 | { |
| 90 | type Output = CodedBitstreamBuffer; |
| 91 | |
| 92 | fn is_ready(&self) -> bool { |
| 93 | self.bitstream.is_ready() |
| 94 | } |
| 95 | |
| 96 | fn sync(self) -> StatelessBackendResult<Self::Output> { |
| 97 | let coded_data = self.bitstream.sync()?; |
| 98 | |
| 99 | log::trace!("synced bitstream size={}", coded_data.len()); |
| 100 | |
| 101 | Ok(CodedBitstreamBuffer::new(self.meta, coded_data)) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | |
| 106 | |
| 107 | pub(crate) struct OutputQueue<O> |
| 108 | where |
| 109 | O: BackendPromise, |
| 110 | { |
| 111 | |
| 112 | blocking: BlockingMode, |
| 113 | |
| 114 | |
| 115 | promises: VecDeque<O>, |
| 116 | } |
| 117 | |
| 118 | impl<O> OutputQueue<O> |
| 119 | where |
| 120 | O: BackendPromise, |
| 121 | { |
| 122 | #[allow(dead_code)] |
| 123 | pub(crate) fn new(blocking: BlockingMode) -> Self { |
| 124 | Self { blocking, promises: Default::default() } |
| 125 | } |
| 126 | |
| 127 | |
| 128 | #[allow(dead_code)] |
| 129 | pub(crate) fn add_promise(&mut self, pending: O) { |
| 130 | self.promises.push_back(pending); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | pub(crate) fn poll(&mut self, mode: BlockingMode) -> StatelessBackendResult<Option<O::Output>> { |
| 137 | let block = self.blocking == BlockingMode::Blocking || mode == BlockingMode::Blocking; |
| 138 | |
| 139 | match self.promises.pop_front() { |
| 140 | Some(o) if block || o.is_ready() => Ok(Some(o.sync()?)), |
| 141 | Some(o) => { |
| 142 | self.promises.push_front(o); |
| 143 | Ok(None) |
| 144 | } |
| 145 | None => Ok(None), |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | |
| 150 | pub(crate) fn is_empty(&self) -> bool { |
| 151 | self.promises.is_empty() |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | pub(super) trait Predictor<Picture, Reference, Request> { |
| 161 | |
| 162 | |
| 163 | |
| 164 | fn new_frame( |
| 165 | &mut self, |
| 166 | backend_pic: Picture, |
| 167 | meta: FrameMetadata, |
| 168 | ) -> EncodeResult<Vec<Request>>; |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | fn reconstructed(&mut self, recon: Reference) -> EncodeResult<Vec<Request>>; |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | fn tune(&mut self, tunings: Tunings) -> EncodeResult<()>; |
| 180 | |
| 181 | |
| 182 | fn drain(&mut self) -> EncodeResult<Vec<Request>>; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | pub trait StatelessVideoEncoderBackend<Codec>: Sized |
| 187 | where |
| 188 | Codec: StatelessCodec<Self>, |
| 189 | { |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | type Picture: 'static; |
| 196 | |
| 197 | |
| 198 | type Reconstructed: 'static; |
| 199 | |
| 200 | |
| 201 | |
| 202 | type CodedPromise: BackendPromise<Output = Vec<u8>>; |
| 203 | |
| 204 | |
| 205 | |
| 206 | type ReconPromise: BackendPromise<Output = Self::Reconstructed>; |
| 207 | } |
| 208 | |
| 209 | pub trait StatelessEncoderBackendImport<Handle, Picture> { |
| 210 | |
| 211 | fn import_picture( |
| 212 | &mut self, |
| 213 | metadata: &FrameMetadata, |
| 214 | handle: Handle, |
| 215 | ) -> StatelessBackendResult<Picture>; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | pub trait StatelessCodec<Backend>: Sized |
| 220 | where |
| 221 | Backend: StatelessVideoEncoderBackend<Self>, |
| 222 | { |
| 223 | |
| 224 | |
| 225 | type Reference; |
| 226 | |
| 227 | |
| 228 | type Request; |
| 229 | |
| 230 | |
| 231 | |
| 232 | type CodedPromise: BackendPromise<Output = CodedBitstreamBuffer>; |
| 233 | |
| 234 | |
| 235 | |
| 236 | type ReferencePromise: BackendPromise<Output = Self::Reference>; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | type Picture<C, B> = <B as StatelessVideoEncoderBackend<C>>::Picture; |
| 241 | |
| 242 | type Reference<C, B> = <C as StatelessCodec<B>>::Reference; |
| 243 | |
| 244 | type Request<C, B> = <C as StatelessCodec<B>>::Request; |
| 245 | |
| 246 | type CodedPromise<C, B> = <C as StatelessCodec<B>>::CodedPromise; |
| 247 | |
| 248 | type ReferencePromise<C, B> = <C as StatelessCodec<B>>::ReferencePromise; |
| 249 | |
| 250 | type BoxPredictor<C, B> = Box<dyn Predictor<Picture<C, B>, Reference<C, B>, Request<C, B>>>; |
| 251 | |
| 252 | pub struct StatelessEncoder<Codec, Handle, Backend> |
| 253 | where |
| 254 | Backend: StatelessVideoEncoderBackend<Codec>, |
| 255 | Codec: StatelessCodec<Backend>, |
| 256 | { |
| 257 | |
| 258 | output_queue: OutputQueue<CodedPromise<Codec, Backend>>, |
| 259 | |
| 260 | |
| 261 | recon_queue: OutputQueue<ReferencePromise<Codec, Backend>>, |
| 262 | |
| 263 | |
| 264 | predictor: BoxPredictor<Codec, Backend>, |
| 265 | |
| 266 | |
| 267 | coded_queue: VecDeque<CodedBitstreamBuffer>, |
| 268 | |
| 269 | |
| 270 | predictor_frame_count: usize, |
| 271 | |
| 272 | |
| 273 | backend: Backend, |
| 274 | |
| 275 | _phantom: std::marker::PhantomData<Handle>, |
| 276 | } |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | pub trait StatelessEncoderExecute<Codec, Handle, Backend> |
| 284 | where |
| 285 | Backend: StatelessVideoEncoderBackend<Codec>, |
| 286 | Codec: StatelessCodec<Backend>, |
| 287 | { |
| 288 | fn execute(&mut self, request: Request<Codec, Backend>) -> EncodeResult<()>; |
| 289 | } |
| 290 | |
| 291 | impl<Codec, Handle, Backend> StatelessEncoder<Codec, Handle, Backend> |
| 292 | where |
| 293 | Codec: StatelessCodec<Backend>, |
| 294 | Backend: StatelessVideoEncoderBackend<Codec>, |
| 295 | Self: StatelessEncoderExecute<Codec, Handle, Backend>, |
| 296 | { |
| 297 | #[allow(dead_code)] |
| 298 | fn new( |
| 299 | backend: Backend, |
| 300 | mode: BlockingMode, |
| 301 | predictor: BoxPredictor<Codec, Backend>, |
| 302 | ) -> EncodeResult<Self> { |
| 303 | Ok(Self { |
| 304 | backend, |
| 305 | predictor, |
| 306 | predictor_frame_count: 0, |
| 307 | coded_queue: Default::default(), |
| 308 | output_queue: OutputQueue::new(mode), |
| 309 | recon_queue: OutputQueue::new(mode), |
| 310 | _phantom: Default::default(), |
| 311 | }) |
| 312 | } |
| 313 | |
| 314 | fn poll_pending(&mut self, mode: BlockingMode) -> EncodeResult<()> { |
| 315 | |
| 316 | while let Some(coded) = self.output_queue.poll(mode)? { |
| 317 | self.coded_queue.push_back(coded); |
| 318 | } |
| 319 | |
| 320 | while let Some(recon) = self.recon_queue.poll(mode)? { |
| 321 | let requests = self.predictor.reconstructed(recon)?; |
| 322 | if requests.is_empty() { |
| 323 | |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | for request in requests { |
| 328 | self.execute(request)?; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | Ok(()) |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | impl<Codec, Handle, Backend> VideoEncoder<Handle> for StatelessEncoder<Codec, Handle, Backend> |
| 337 | where |
| 338 | Codec: StatelessCodec<Backend>, |
| 339 | Backend: StatelessVideoEncoderBackend<Codec>, |
| 340 | Backend: StatelessEncoderBackendImport<Handle, Backend::Picture>, |
| 341 | Self: StatelessEncoderExecute<Codec, Handle, Backend>, |
| 342 | { |
| 343 | fn tune(&mut self, tunings: Tunings) -> EncodeResult<()> { |
| 344 | self.predictor.tune(tunings) |
| 345 | } |
| 346 | |
| 347 | fn encode(&mut self, metadata: FrameMetadata, handle: Handle) -> EncodeResult<()> { |
| 348 | log::trace!("encode: timestamp={} layout={:?}", metadata.timestamp, metadata.layout); |
| 349 | |
| 350 | |
| 351 | let backend_pic = self.backend.import_picture(&metadata, handle)?; |
| 352 | |
| 353 | |
| 354 | self.predictor_frame_count += 1; |
| 355 | |
| 356 | |
| 357 | let requests = self.predictor.new_frame(backend_pic, metadata)?; |
| 358 | for request in requests { |
| 359 | self.execute(request)?; |
| 360 | } |
| 361 | |
| 362 | Ok(()) |
| 363 | } |
| 364 | |
| 365 | fn drain(&mut self) -> EncodeResult<()> { |
| 366 | log::trace!("currently predictor holds {}", self.predictor_frame_count); |
| 367 | |
| 368 | |
| 369 | while self.predictor_frame_count > 0 || !self.recon_queue.is_empty() { |
| 370 | if self.output_queue.is_empty() && self.recon_queue.is_empty() { |
| 371 | |
| 372 | |
| 373 | let requests = self.predictor.drain()?; |
| 374 | if requests.is_empty() { |
| 375 | log::error!("failed to drain predictor, no request was returned"); |
| 376 | return Err(EncodeError::InvalidInternalState); |
| 377 | } |
| 378 | |
| 379 | for request in requests { |
| 380 | self.execute(request)?; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | self.poll_pending(BlockingMode::Blocking)?; |
| 385 | } |
| 386 | |
| 387 | |
| 388 | while !self.output_queue.is_empty() { |
| 389 | self.poll_pending(BlockingMode::Blocking)?; |
| 390 | } |
| 391 | |
| 392 | Ok(()) |
| 393 | } |
| 394 | |
| 395 | fn poll(&mut self) -> EncodeResult<Option<CodedBitstreamBuffer>> { |
| 396 | |
| 397 | self.poll_pending(BlockingMode::NonBlocking)?; |
| 398 | Ok(self.coded_queue.pop_front()) |
| 399 | } |
| 400 | } |