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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#![deny(missing_docs)]
use crate::frame::*;
use crate::serialize::{Deserialize, Serialize};
use crate::stats::EncoderStats;
use crate::util::Pixel;
use std::any::Any;
use std::fmt;
use std::sync::Arc;
use thiserror::*;
#[derive(Debug)]
pub struct Opaque(Box<dyn Any + Send + Sync>);
impl Opaque {
pub fn new<T: Any + Send + Sync>(t: T) -> Self {
Opaque(Box::new(t) as Box<dyn Any + Send + Sync>)
}
pub fn downcast<T: Any + Send + Sync>(self) -> Result<Box<T>, Opaque> {
if self.0.is::<T>() {
unsafe {
let raw: *mut (dyn Any + Send + Sync) = Box::into_raw(self.0);
Ok(Box::from_raw(raw as *mut T))
}
} else {
Err(self)
}
}
}
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct Rational {
pub num: u64,
pub den: u64,
}
impl Rational {
pub const fn new(num: u64, den: u64) -> Self {
Rational { num, den }
}
pub const fn from_reciprocal(reciprocal: Self) -> Self {
Rational { num: reciprocal.den, den: reciprocal.num }
}
pub fn as_f64(self) -> f64 {
self.num as f64 / self.den as f64
}
}
#[cfg(feature = "serialize")]
impl serde::Serialize for Rational {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
(self.num, self.den).serialize(serializer)
}
}
#[cfg(feature = "serialize")]
impl<'a> serde::Deserialize<'a> for Rational {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'a>,
{
let (num, den) = serde::Deserialize::deserialize(deserializer)?;
Ok(Rational::new(num, den))
}
}
#[allow(dead_code, non_camel_case_types)]
#[derive(Debug, Eq, PartialEq, Clone, Copy, Serialize, Deserialize)]
#[repr(C)]
pub enum FrameType {
KEY,
INTER,
INTRA_ONLY,
SWITCH,
}
impl FrameType {
#[inline]
pub fn has_inter(self) -> bool {
self == FrameType::INTER || self == FrameType::SWITCH
}
#[inline]
pub fn all_intra(self) -> bool {
self == FrameType::KEY || self == FrameType::INTRA_ONLY
}
}
impl fmt::Display for FrameType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::FrameType::*;
match self {
KEY => write!(f, "Key frame"),
INTER => write!(f, "Inter frame"),
INTRA_ONLY => write!(f, "Intra only frame"),
SWITCH => write!(f, "Switching frame"),
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Error)]
pub enum EncoderStatus {
#[error("need more data")]
NeedMoreData,
#[error("enough data")]
EnoughData,
#[error("limit reached")]
LimitReached,
#[error("encoded")]
Encoded,
#[error("failure")]
Failure,
#[error("not ready")]
NotReady,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Packet<T: Pixel> {
pub data: Vec<u8>,
#[cfg_attr(feature = "serialize", serde(skip))]
pub rec: Option<Arc<Frame<T>>>,
#[cfg_attr(feature = "serialize", serde(skip))]
pub source: Option<Arc<Frame<T>>>,
pub input_frameno: u64,
pub frame_type: FrameType,
pub qp: u8,
pub enc_stats: EncoderStats,
#[cfg_attr(feature = "serialize", serde(skip))]
pub opaque: Option<Opaque>,
}
impl<T: Pixel> PartialEq for Packet<T> {
fn eq(&self, other: &Self) -> bool {
self.data == other.data
&& self.input_frameno == other.input_frameno
&& self.frame_type == other.frame_type
&& self.qp == other.qp
}
}
impl<T: Pixel> fmt::Display for Packet<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Frame {} - {} - {} bytes",
self.input_frameno,
self.frame_type,
self.data.len()
)
}
}
pub trait IntoFrame<T: Pixel> {
fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>);
}
impl<T: Pixel> IntoFrame<T> for Option<Arc<Frame<T>>> {
fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>) {
(self, None)
}
}
impl<T: Pixel> IntoFrame<T> for Arc<Frame<T>> {
fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>) {
(Some(self), None)
}
}
impl<T: Pixel> IntoFrame<T> for (Arc<Frame<T>>, FrameParameters) {
fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>) {
(Some(self.0), Some(self.1))
}
}
impl<T: Pixel> IntoFrame<T> for (Arc<Frame<T>>, Option<FrameParameters>) {
fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>) {
(Some(self.0), self.1)
}
}
impl<T: Pixel> IntoFrame<T> for Frame<T> {
fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>) {
(Some(Arc::new(self)), None)
}
}
impl<T: Pixel> IntoFrame<T> for (Frame<T>, FrameParameters) {
fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>) {
(Some(Arc::new(self.0)), Some(self.1))
}
}
impl<T: Pixel> IntoFrame<T> for (Frame<T>, Option<FrameParameters>) {
fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>) {
(Some(Arc::new(self.0)), self.1)
}
}