xref: /aosp_15_r20/external/crosvm/devices/src/virtio/video/encoder/backend/mod.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
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")]
6 pub mod ffmpeg;
7 #[cfg(feature = "libvda")]
8 pub mod vda;
9 
10 use base::AsRawDescriptor;
11 
12 use super::EncoderCapabilities;
13 use super::EncoderEvent;
14 use super::InputBufferId;
15 use super::OutputBufferId;
16 use super::SessionConfig;
17 use crate::virtio::video::error::VideoResult;
18 use crate::virtio::video::format::Bitrate;
19 use crate::virtio::video::resource::GuestResource;
20 use crate::virtio::video::resource::GuestResourceHandle;
21 
22 pub 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`.
encode( &mut self, resource: GuestResource, timestamp: u64, force_keyframe: bool, ) -> VideoResult<InputBufferId>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.
use_output_buffer( &mut self, resource: GuestResourceHandle, offset: u32, size: u32, ) -> VideoResult<OutputBufferId>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.
flush(&mut self) -> VideoResult<()>50     fn flush(&mut self) -> VideoResult<()>;
51 
52     /// Requests the encoder to use new encoding parameters provided by `bitrate` and `framerate`.
request_encoding_params_change( &mut self, bitrate: Bitrate, framerate: u32, ) -> VideoResult<()>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.
event_pipe(&self) -> &dyn AsRawDescriptor61     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.
read_event(&mut self) -> VideoResult<EncoderEvent>65     fn read_event(&mut self) -> VideoResult<EncoderEvent>;
66 }
67 
68 pub trait Encoder {
69     type Session: EncoderSession;
70 
query_capabilities(&self) -> VideoResult<EncoderCapabilities>71     fn query_capabilities(&self) -> VideoResult<EncoderCapabilities>;
start_session(&mut self, config: SessionConfig) -> VideoResult<Self::Session>72     fn start_session(&mut self, config: SessionConfig) -> VideoResult<Self::Session>;
stop_session(&mut self, session: Self::Session) -> VideoResult<()>73     fn stop_session(&mut self, session: Self::Session) -> VideoResult<()>;
74 }
75