1#![deny(missing_docs)]
19#![doc = include_str!("../README.md")]
20use rusttorch_core::{Device, Tensor};
21use rusttorch_data::{MemoryFootprint, PinMemory};
22use serde::{Deserialize, Serialize};
23#[derive(Debug, thiserror::Error)]
25#[non_exhaustive]
26pub enum CodecError {
27 #[error(transparent)]
29 Io(#[from] std::io::Error),
30 #[error("{0}")]
32 Invalid(String),
33 #[error(transparent)]
35 Limit(#[from] rusttorch_core::RustTorchError),
36 #[error(transparent)]
38 Tensor(#[from] tch::TchError),
39 #[cfg(any(feature = "system", feature = "vcpkg"))]
41 #[error(transparent)]
42 Native(#[from] rsmpeg::error::RsmpegError),
43}
44pub type Result<T> = std::result::Result<T, CodecError>;
46#[derive(Clone, Copy, Debug, Serialize, PartialEq, Eq)]
48pub struct TimeBase {
49 numerator: u32,
50 denominator: u32,
51}
52impl TimeBase {
53 pub fn new(numerator: u32, denominator: u32) -> Result<Self> {
55 if numerator == 0 || denominator == 0 {
56 return Err(CodecError::Invalid(
57 "time base requires positive numerator and denominator".into(),
58 ));
59 }
60 Ok(Self {
61 numerator,
62 denominator,
63 })
64 }
65 pub const fn numerator(self) -> u32 {
67 self.numerator
68 }
69 pub const fn denominator(self) -> u32 {
71 self.denominator
72 }
73 pub fn seconds(self, ticks: i64) -> f64 {
75 ticks as f64 * self.numerator as f64 / self.denominator as f64
76 }
77 pub fn rescale(self, ticks: i64, target: Self) -> Result<i64> {
79 let value = (ticks as i128) * (self.numerator as i128) * (target.denominator as i128)
80 / ((self.denominator as i128) * (target.numerator as i128));
81 i64::try_from(value)
82 .map_err(|_| CodecError::Invalid("rescaled timestamp overflows i64".into()))
83 }
84}
85impl<'de> Deserialize<'de> for TimeBase {
86 fn deserialize<D: serde::Deserializer<'de>>(d: D) -> std::result::Result<Self, D::Error> {
87 #[derive(Deserialize)]
88 struct Wire {
89 numerator: u32,
90 denominator: u32,
91 }
92 let w = Wire::deserialize(d)?;
93 Self::new(w.numerator, w.denominator).map_err(serde::de::Error::custom)
94 }
95}
96#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
98pub struct Timestamp {
99 pub ticks: i64,
101 pub time_base: TimeBase,
103}
104impl Timestamp {
105 pub fn seconds(self) -> f64 {
107 self.time_base.seconds(self.ticks)
108 }
109}
110#[derive(Clone, Copy, Debug, PartialEq, Eq)]
112pub enum StreamKind {
113 Video,
115 Audio,
117}
118#[derive(Debug)]
120pub struct VideoFrame {
121 pub pixels: Tensor,
123 pub timestamp: Option<Timestamp>,
125}
126#[derive(Debug)]
128pub struct AudioFrame {
129 pub samples: Tensor,
131 pub sample_rate: u32,
133 pub timestamp: Option<Timestamp>,
135}
136#[derive(Debug)]
138pub enum MediaFrame {
139 Video(VideoFrame),
141 Audio(AudioFrame),
143}
144impl MediaFrame {
145 pub fn timestamp(&self) -> Option<Timestamp> {
147 match self {
148 Self::Video(v) => v.timestamp,
149 Self::Audio(a) => a.timestamp,
150 }
151 }
152}
153impl MemoryFootprint for VideoFrame {
154 fn resident_bytes(&self) -> usize {
155 self.pixels.resident_bytes()
156 }
157}
158impl MemoryFootprint for AudioFrame {
159 fn resident_bytes(&self) -> usize {
160 self.samples.resident_bytes()
161 }
162}
163impl MemoryFootprint for MediaFrame {
164 fn resident_bytes(&self) -> usize {
165 match self {
166 Self::Video(v) => v.resident_bytes(),
167 Self::Audio(a) => a.resident_bytes(),
168 }
169 }
170}
171impl PinMemory for VideoFrame {
172 fn pin_memory(mut self, d: Device) -> rusttorch_core::Result<Self> {
173 self.pixels = self.pixels.pin_memory(d)?;
174 Ok(self)
175 }
176}
177impl PinMemory for AudioFrame {
178 fn pin_memory(mut self, d: Device) -> rusttorch_core::Result<Self> {
179 self.samples = self.samples.pin_memory(d)?;
180 Ok(self)
181 }
182}
183impl PinMemory for MediaFrame {
184 fn pin_memory(self, d: Device) -> rusttorch_core::Result<Self> {
185 match self {
186 Self::Video(v) => Ok(Self::Video(v.pin_memory(d)?)),
187 Self::Audio(a) => Ok(Self::Audio(a.pin_memory(d)?)),
188 }
189 }
190}
191#[derive(Clone, Debug)]
193pub struct StreamInfo {
194 pub index: usize,
196 pub kind: StreamKind,
198 pub time_base: TimeBase,
200 pub duration: Option<i64>,
202 pub decoder: String,
204}
205#[derive(Clone, Debug)]
207pub struct CodecCapabilities {
208 pub version: String,
210 pub configuration: String,
212 pub license: String,
214 pub cpu_decode: bool,
216}
217#[cfg(any(feature = "system", feature = "vcpkg"))]
218mod native;
219#[cfg(any(feature = "system", feature = "vcpkg"))]
220pub use native::{MediaDecoder, capabilities};