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