-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathstateful.rs
More file actions
169 lines (141 loc) · 3.94 KB
/
Copy pathstateful.rs
File metadata and controls
169 lines (141 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use thiserror::Error;
use zerocopy::AsBytes;
use crate::decoder::DecodedHandle;
use crate::decoder::StreamInfo;
use crate::DecodedFormat;
use crate::Resolution;
use std::sync::Arc;
pub enum DecodeError {
InvalidState,
InvalidData,
}
pub enum ConfigError {
InvalidState,
NotSupported,
InvalidConfig,
}
/// Error returned by stateful backend methods.
#[derive(Error, Debug)]
pub enum StatefulBackendError {
#[error("not enough resources to proceed with the operation now")]
OutOfResources,
#[error("this format is not supported")]
UnsupportedFormat,
#[error(transparent)]
Other(#[from] anyhow::Error),
}
#[derive(Copy, Clone, Debug)]
pub enum EncodedFormat {
AV1,
H264,
H265,
VP8,
VP9,
}
/// Common trait shared by all stateful video decoder backends, providing codec-independent
/// methods.
pub trait StatefulDecoderBackend<Codec: StatefulCodec> {
/// The type that the backend returns as a result of a decode operation.
/// This will usually be some backend-specific type with a resource and a
/// resource pool so that said buffer can be reused for another decode
/// operation when it goes out of scope.
type Handle: DecodedHandle;
/// Returns the current decoding parameters, as parsed from the stream.
fn stream_info(&self) -> Option<&StreamInfo>;
}
pub trait StatefulCodec {
/// State that needs to be kept during a decoding operation, typed by backend.
type DecoderState<B: StatefulDecoderBackend<Self>>;
}
#[derive(Copy, Clone, Debug)]
pub enum ColorGamut {
Bt709,
Bt470bg,
Smpte170m,
}
#[derive(Copy, Clone, Debug)]
pub enum ColorTransfer {
Bt709,
Smpte170m,
Iec61966_2_1,
}
#[derive(Copy, Clone, Debug)]
pub enum ColorMatrix {
Rgb,
Bt709,
Bt470bg,
Smpte170m,
}
#[derive(Copy, Clone, Debug)]
pub struct VideoColorSpace {
primaries: ColorGamut,
transfer: ColorTransfer,
matrix: ColorMatrix,
}
#[derive(Clone, Debug)]
pub struct EncodedVideoChunk {
data: Arc<Vec<u8>>,
}
pub struct VideoFrame {
format: DecodedFormat,
coded_resolution: Resolution,
display_resolution: Resolution,
duration: usize,
timestamp: usize,
color_space: VideoColorSpace,
buffer: Option<Box<dyn AsBytes>>,
}
pub enum StatefulDecoderEvent {
/// The next frame has been decoded.
FrameReady(VideoFrame),
/// The format of the stream has changed and action is required. A VideoFrame
/// with no buffer is returned.
FormatChanged(VideoFrame),
}
pub trait StatefulVideoDecoder<M> {
/// Add a new chunk of video to decoding queue, this is a non-blocking method
fn decode(&mut self, chunk: EncodedVideoChunk) -> Result<(), DecodeError>;
/// Return information about the currently decoded stream
fn stream_info(&self) -> Option<&StreamInfo>;
/// Close the decoder and prevent it from future use
fn close(&mut self);
/// Finish processing all pending decode requests
fn flush(&mut self) -> Result<(), DecodeError>;
/// Returns the next event, if there is any pending.
fn next_event(&mut self) -> Option<StatefulDecoderEvent>;
}
#[derive(Copy, Clone, Debug, Default)]
pub enum StatefulDecoderState {
#[default]
Unconfigured,
Configured,
Closed,
}
pub struct StatefulDecoder<C, B>
where
C: StatefulCodec,
B: StatefulDecoderBackend<C>,
{
decoding_state: StatefulDecoderState,
/// The backend used for hardware acceleration.
backend: B,
/// Codec-specific state.
codec_state: C::DecoderState<B>,
}
impl<C, B> StatefulDecoder<C, B>
where
C: StatefulCodec,
B: StatefulDecoderBackend<C>,
C::DecoderState<B>: Default,
{
pub fn new(backend: B) -> Self {
Self {
backend,
decoding_state: Default::default(),
codec_state: Default::default(),
}
}
}