devices/virtio/video/encoder/backend/
mod.rs

1// Copyright 2020 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#[cfg(feature = "ffmpeg")]
6pub mod ffmpeg;
7#[cfg(feature = "libvda")]
8pub mod vda;
9
10use base::AsRawDescriptor;
11
12use super::EncoderCapabilities;
13use super::EncoderEvent;
14use super::InputBufferId;
15use super::OutputBufferId;
16use super::SessionConfig;
17use crate::virtio::video::error::VideoResult;
18use crate::virtio::video::format::Bitrate;
19use crate::virtio::video::resource::GuestResource;
20use crate::virtio::video::resource::GuestResourceHandle;
21
22pub trait EncoderSession {
23    /// Encodes the frame provided by `resource`.
24    /// `force_keyframe` forces the frame to be encoded as a keyframe.
25    /// When the buffer has been successfully processed, a `ProcessedInputBuffer` event will
26    /// be readable from the event pipe, with the same `InputBufferId` as returned by this
27    /// function.
28    /// When the corresponding encoded data is ready, `ProcessedOutputBuffer` events will be
29    /// readable from the event pipe, with the same timestamp as provided `timestamp`.
30    fn encode(
31        &mut self,
32        resource: GuestResource,
33        timestamp: u64,
34        force_keyframe: bool,
35    ) -> VideoResult<InputBufferId>;
36
37    /// Provides an output `resource` to store encoded output, where `offset` and `size` define the
38    /// region of memory to use.
39    /// When the buffer has been filled with encoded output, a `ProcessedOutputBuffer` event will be
40    /// readable from the event pipe, with the same `OutputBufferId` as returned by this function.
41    fn use_output_buffer(
42        &mut self,
43        resource: GuestResourceHandle,
44        offset: u32,
45        size: u32,
46    ) -> VideoResult<OutputBufferId>;
47
48    /// Requests the encoder to flush. When completed, an `EncoderEvent::FlushResponse` event will
49    /// be readable from the event pipe.
50    fn flush(&mut self) -> VideoResult<()>;
51
52    /// Requests the encoder to use new encoding parameters provided by `bitrate` and `framerate`.
53    fn request_encoding_params_change(
54        &mut self,
55        bitrate: Bitrate,
56        framerate: u32,
57    ) -> VideoResult<()>;
58
59    /// Returns the event pipe on which the availability of events will be signaled. Note that the
60    /// returned value is borrowed and only valid as long as the session is alive.
61    fn event_pipe(&self) -> &dyn AsRawDescriptor;
62
63    /// Performs a blocking read for an encoder event. This function should only be called when
64    /// the file descriptor returned by `event_pipe` is readable.
65    fn read_event(&mut self) -> VideoResult<EncoderEvent>;
66}
67
68pub trait Encoder {
69    type Session: EncoderSession;
70
71    fn query_capabilities(&self) -> VideoResult<EncoderCapabilities>;
72    fn start_session(&mut self, config: SessionConfig) -> VideoResult<Self::Session>;
73    fn stop_session(&mut self, session: Self::Session) -> VideoResult<()>;
74}