1use super::NativeDecoder;
25use crate::mp4_demux::{TrackCodec, VideoTrack};
26
27#[cfg(all(feature = "videotoolbox", target_os = "macos"))]
28use crate::videotoolbox_decode::VideoToolboxDecoder;
29#[cfg(all(feature = "nvidia", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64"))]
30use crate::nvidia::NvdecDecoder;
31#[cfg(all(feature = "vaapi", target_os = "linux"))]
32use crate::vaapi::VaapiDecoder;
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36#[non_exhaustive]
37pub enum Backend {
38 VideoToolbox,
41 Nvdec,
44 Vaapi,
47 Native,
49}
50
51impl Backend {
52 pub fn available(self) -> bool {
55 match self {
56 Backend::VideoToolbox => cfg!(all(feature = "videotoolbox", target_os = "macos")),
57 #[cfg(all(feature = "nvidia", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64"))]
58 Backend::Nvdec => crate::nvidia::available(),
59 #[cfg(not(all(feature = "nvidia", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64")))]
60 Backend::Nvdec => false,
61 #[cfg(all(feature = "vaapi", target_os = "linux"))]
62 Backend::Vaapi => crate::vaapi::available(),
63 #[cfg(not(all(feature = "vaapi", target_os = "linux")))]
64 Backend::Vaapi => false,
65 Backend::Native => true,
66 }
67 }
68
69 pub const HARDWARE: [Backend; 3] = [Backend::VideoToolbox, Backend::Nvdec, Backend::Vaapi];
71
72 pub fn preferred() -> Backend {
74 Self::HARDWARE.into_iter().find(|b| b.available()).unwrap_or(Backend::Native)
75 }
76}
77
78enum Inner {
79 #[cfg(all(feature = "videotoolbox", target_os = "macos"))]
80 VideoToolbox(VideoToolboxDecoder),
81 #[cfg(all(feature = "nvidia", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64"))]
82 Nvdec(Box<NvdecDecoder>),
83 #[cfg(all(feature = "vaapi", target_os = "linux"))]
84 Vaapi(Box<VaapiDecoder>),
85 Native(Box<NativeDecoder>),
86}
87
88pub struct Decoder {
90 inner: Inner,
91}
92
93impl Decoder {
94 pub fn new(track: &VideoTrack) -> Result<Self, String> {
97 for b in Backend::HARDWARE.into_iter().filter(|b| b.available()) {
98 if let Ok(d) = Self::with_backend(track, b) {
99 return Ok(d);
100 }
101 }
102 Self::with_backend(track, Backend::Native)
104 }
105
106 pub fn from_config(codec: TrackCodec, config: &[u8], width: u32, height: u32, timescale: u32) -> Result<Self, String> {
110 Self::new(&config_track(codec, config, width, height, timescale))
111 }
112
113 pub fn with_backend(track: &VideoTrack, backend: Backend) -> Result<Self, String> {
116 let inner = match backend {
117 #[cfg(all(feature = "videotoolbox", target_os = "macos"))]
118 Backend::VideoToolbox => Inner::VideoToolbox(VideoToolboxDecoder::new(track)?),
119 #[cfg(not(all(feature = "videotoolbox", target_os = "macos")))]
120 Backend::VideoToolbox => {
121 return Err("VideoToolbox needs macOS and threers' `videotoolbox` feature".into())
122 }
123 #[cfg(all(feature = "nvidia", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64"))]
124 Backend::Nvdec => Inner::Nvdec(Box::new(NvdecDecoder::new(track)?)),
125 #[cfg(not(all(feature = "nvidia", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64")))]
126 Backend::Nvdec => {
127 return Err("NVDEC needs Linux or Windows and the `nvidia` feature".into())
128 }
129 #[cfg(all(feature = "vaapi", target_os = "linux"))]
130 Backend::Vaapi => Inner::Vaapi(Box::new(VaapiDecoder::new(track)?)),
131 #[cfg(not(all(feature = "vaapi", target_os = "linux")))]
132 Backend::Vaapi => return Err("VAAPI needs Linux and the `vaapi` feature".into()),
133 Backend::Native => Inner::Native(Box::new(NativeDecoder::new(track)?)),
134 };
135 Ok(Decoder { inner })
136 }
137
138 pub fn backend(&self) -> Backend {
140 match self.inner {
141 #[cfg(all(feature = "videotoolbox", target_os = "macos"))]
142 Inner::VideoToolbox(_) => Backend::VideoToolbox,
143 #[cfg(all(feature = "nvidia", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64"))]
144 Inner::Nvdec(_) => Backend::Nvdec,
145 #[cfg(all(feature = "vaapi", target_os = "linux"))]
146 Inner::Vaapi(_) => Backend::Vaapi,
147 Inner::Native(_) => Backend::Native,
148 }
149 }
150
151 pub fn size(&self) -> (u32, u32) {
153 match &self.inner {
154 #[cfg(all(feature = "videotoolbox", target_os = "macos"))]
155 Inner::VideoToolbox(d) => d.size(),
156 #[cfg(all(feature = "nvidia", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64"))]
157 Inner::Nvdec(d) => d.size(),
158 #[cfg(all(feature = "vaapi", target_os = "linux"))]
159 Inner::Vaapi(d) => d.size(),
160 Inner::Native(d) => d.size(),
161 }
162 }
163
164 pub fn push(&mut self, sample: &[u8], pts: i64, dts: i64, duration: i64) -> Result<(), String> {
168 match &mut self.inner {
169 #[cfg(all(feature = "videotoolbox", target_os = "macos"))]
170 Inner::VideoToolbox(d) => d.push(sample, pts, dts, duration),
171 #[cfg(all(feature = "nvidia", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64"))]
172 Inner::Nvdec(d) => d.push(sample, pts, dts, duration),
173 #[cfg(all(feature = "vaapi", target_os = "linux"))]
174 Inner::Vaapi(d) => d.push(sample, pts, dts, duration),
175 Inner::Native(d) => d.push(sample, pts, dts, duration),
176 }
177 }
178
179 pub fn take(&mut self, pts: i64) -> Option<Vec<u8>> {
182 match &mut self.inner {
183 #[cfg(all(feature = "videotoolbox", target_os = "macos"))]
184 Inner::VideoToolbox(d) => d.take(pts),
185 #[cfg(all(feature = "nvidia", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64"))]
186 Inner::Nvdec(d) => d.take(pts),
187 #[cfg(all(feature = "vaapi", target_os = "linux"))]
188 Inner::Vaapi(d) => d.take(pts),
189 Inner::Native(d) => d.take(pts),
190 }
191 }
192}
193
194fn config_track(codec: TrackCodec, config: &[u8], width: u32, height: u32, timescale: u32) -> VideoTrack {
195 VideoTrack { codec, width, height, config: config.to_vec(), timescale, samples: Vec::new() }
196}
197
198#[cfg(test)]
199mod tests {
200 use super::*;
201 use crate::h264::{CompressedEncoder, Yuv420Frame};
202
203 #[test]
204 fn decodes_on_every_backend_this_build_has() {
205 let (w, h) = (64u32, 32u32);
206 let rgba: Vec<u8> = (0..w * h).flat_map(|i| [(i % 64 * 4) as u8, 120, 200, 255]).collect();
207 let enc = CompressedEncoder::new(w, h, 20);
208 let sample = enc.encode_sample(&Yuv420Frame::from_rgba(w, h, &rgba));
209 let track = config_track(TrackCodec::H264, &enc.avcc(), w, h, 90_000);
210 for backend in [Backend::VideoToolbox, Backend::Nvdec, Backend::Vaapi, Backend::Native] {
211 let mut dec = match Decoder::with_backend(&track, backend) {
212 Ok(d) => d,
213 Err(e) if backend != Backend::Native => {
216 eprintln!("{backend:?}: {e}");
217 continue;
218 }
219 Err(e) => panic!("the native decoder would not open: {e}"),
220 };
221 assert_eq!(dec.backend(), backend);
222 dec.push(&sample, 0, 0, 3600).unwrap();
223 let bgra = dec.take(0).expect("a picture");
224 assert_eq!(bgra.len(), (w * h * 4) as usize);
225 let px = &bgra[((16 * w + 32) * 4) as usize..][..4];
227 assert!((px[0] as i32 - 200).abs() < 12, "{backend:?}: {px:?}");
228 }
229 let auto = Decoder::from_config(TrackCodec::H264, &enc.avcc(), w, h, 90_000).unwrap();
230 assert!([Backend::preferred(), Backend::Native].contains(&auto.backend()));
232 }
233}