1#![deny(missing_docs)]
10
11use crate::frame::*;
12use crate::serialize::{Deserialize, Serialize};
13use crate::stats::EncoderStats;
14use crate::util::Pixel;
15
16use std::any::Any;
17use std::fmt;
18use std::sync::Arc;
19
20use thiserror::*;
21
22#[derive(Debug)]
24pub struct Opaque(Box<dyn Any + Send + Sync>);
25
26impl Opaque {
27 pub fn new<T: Any + Send + Sync>(t: T) -> Self {
29 Opaque(Box::new(t) as Box<dyn Any + Send + Sync>)
30 }
31
32 pub fn downcast<T: Any + Send + Sync>(self) -> Result<Box<T>, Opaque> {
38 if self.0.is::<T>() {
39 unsafe {
41 let raw: *mut (dyn Any + Send + Sync) = Box::into_raw(self.0);
42 Ok(Box::from_raw(raw as *mut T))
43 }
44 } else {
45 Err(self)
46 }
47 }
48}
49
50#[derive(Clone, Copy, Debug)]
53#[repr(C)]
54pub struct Rational {
55 pub num: u64,
57 pub den: u64,
59}
60
61impl Rational {
62 pub const fn new(num: u64, den: u64) -> Self {
64 Rational { num, den }
65 }
66
67 pub const fn from_reciprocal(reciprocal: Self) -> Self {
69 Rational { num: reciprocal.den, den: reciprocal.num }
70 }
71
72 pub fn as_f64(self) -> f64 {
74 self.num as f64 / self.den as f64
75 }
76}
77
78#[cfg(feature = "serialize")]
79impl serde::Serialize for Rational {
80 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
81 where
82 S: serde::Serializer,
83 {
84 (self.num, self.den).serialize(serializer)
85 }
86}
87
88#[cfg(feature = "serialize")]
89impl<'a> serde::Deserialize<'a> for Rational {
90 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
91 where
92 D: serde::Deserializer<'a>,
93 {
94 let (num, den) = serde::Deserialize::deserialize(deserializer)?;
95
96 Ok(Rational::new(num, den))
97 }
98}
99
100#[allow(dead_code, non_camel_case_types)]
102#[derive(Debug, Eq, PartialEq, Clone, Copy, Serialize, Deserialize)]
103#[repr(C)]
104pub enum FrameType {
105 KEY,
107 INTER,
109 INTRA_ONLY,
111 SWITCH,
113}
114
115impl FrameType {
116 #[inline]
118 pub fn has_inter(self) -> bool {
119 self == FrameType::INTER || self == FrameType::SWITCH
120 }
121 #[inline]
123 pub fn all_intra(self) -> bool {
124 self == FrameType::KEY || self == FrameType::INTRA_ONLY
125 }
126}
127
128impl fmt::Display for FrameType {
129 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130 use self::FrameType::*;
131 match self {
132 KEY => write!(f, "Key frame"),
133 INTER => write!(f, "Inter frame"),
134 INTRA_ONLY => write!(f, "Intra only frame"),
135 SWITCH => write!(f, "Switching frame"),
136 }
137 }
138}
139
140#[derive(Clone, Debug, Default)]
142pub struct T35 {
143 pub country_code: u8,
145 pub country_code_extension_byte: u8,
147 pub data: Box<[u8]>,
149}
150
151#[derive(Clone, Copy, Debug, Eq, PartialEq, Error)]
155pub enum EncoderStatus {
156 #[error("need more data")]
163 NeedMoreData,
164 #[error("enough data")]
171 EnoughData,
172 #[error("limit reached")]
179 LimitReached,
180 #[error("encoded")]
182 Encoded,
183 #[error("failure")]
185 Failure,
186 #[error("not ready")]
193 NotReady,
194}
195
196#[derive(Debug, Serialize, Deserialize)]
201pub struct Packet<T: Pixel> {
202 pub data: Vec<u8>,
204 #[cfg_attr(feature = "serialize", serde(skip))]
206 pub rec: Option<Arc<Frame<T>>>,
207 #[cfg_attr(feature = "serialize", serde(skip))]
209 pub source: Option<Arc<Frame<T>>>,
210 pub input_frameno: u64,
216 pub frame_type: FrameType,
218 pub qp: u8,
220 pub enc_stats: EncoderStats,
222 #[cfg_attr(feature = "serialize", serde(skip))]
224 pub opaque: Option<Opaque>,
225}
226
227impl<T: Pixel> PartialEq for Packet<T> {
228 fn eq(&self, other: &Self) -> bool {
229 self.data == other.data
230 && self.input_frameno == other.input_frameno
231 && self.frame_type == other.frame_type
232 && self.qp == other.qp
233 }
234}
235
236impl<T: Pixel> fmt::Display for Packet<T> {
237 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
238 write!(
239 f,
240 "Frame {} - {} - {} bytes",
241 self.input_frameno,
242 self.frame_type,
243 self.data.len()
244 )
245 }
246}
247
248pub trait IntoFrame<T: Pixel> {
257 fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>);
259}
260
261impl<T: Pixel> IntoFrame<T> for Option<Arc<Frame<T>>> {
262 fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>) {
263 (self, None)
264 }
265}
266
267impl<T: Pixel> IntoFrame<T> for Arc<Frame<T>> {
268 fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>) {
269 (Some(self), None)
270 }
271}
272
273impl<T: Pixel> IntoFrame<T> for (Arc<Frame<T>>, FrameParameters) {
274 fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>) {
275 (Some(self.0), Some(self.1))
276 }
277}
278
279impl<T: Pixel> IntoFrame<T> for (Arc<Frame<T>>, Option<FrameParameters>) {
280 fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>) {
281 (Some(self.0), self.1)
282 }
283}
284
285impl<T: Pixel> IntoFrame<T> for Frame<T> {
286 fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>) {
287 (Some(Arc::new(self)), None)
288 }
289}
290
291impl<T: Pixel> IntoFrame<T> for (Frame<T>, FrameParameters) {
292 fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>) {
293 (Some(Arc::new(self.0)), Some(self.1))
294 }
295}
296
297impl<T: Pixel> IntoFrame<T> for (Frame<T>, Option<FrameParameters>) {
298 fn into(self) -> (Option<Arc<Frame<T>>>, Option<FrameParameters>) {
299 (Some(Arc::new(self.0)), self.1)
300 }
301}