use super::NativeDecoder;
use crate::codec::mp4_demux::{TrackCodec, VideoTrack};
#[cfg(all(feature = "videotoolbox", target_os = "macos"))]
use crate::videotoolbox_decode::VideoToolboxDecoder;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Backend {
VideoToolbox,
Native,
}
impl Backend {
pub fn available(self) -> bool {
match self {
Backend::VideoToolbox => cfg!(all(feature = "videotoolbox", target_os = "macos")),
Backend::Native => true,
}
}
pub fn preferred() -> Backend {
if Backend::VideoToolbox.available() {
Backend::VideoToolbox
} else {
Backend::Native
}
}
}
enum Inner {
#[cfg(all(feature = "videotoolbox", target_os = "macos"))]
VideoToolbox(VideoToolboxDecoder),
Native(Box<NativeDecoder>),
}
pub struct Decoder {
inner: Inner,
}
impl Decoder {
pub fn new(track: &VideoTrack) -> Result<Self, String> {
let preferred = Backend::preferred();
if preferred != Backend::Native {
if let Ok(d) = Self::with_backend(track, preferred) {
return Ok(d);
}
}
Self::with_backend(track, Backend::Native)
}
pub fn from_config(codec: TrackCodec, config: &[u8], width: u32, height: u32, timescale: u32) -> Result<Self, String> {
Self::new(&config_track(codec, config, width, height, timescale))
}
pub fn with_backend(track: &VideoTrack, backend: Backend) -> Result<Self, String> {
let inner = match backend {
#[cfg(all(feature = "videotoolbox", target_os = "macos"))]
Backend::VideoToolbox => Inner::VideoToolbox(VideoToolboxDecoder::new(track)?),
#[cfg(not(all(feature = "videotoolbox", target_os = "macos")))]
Backend::VideoToolbox => {
return Err("VideoToolbox needs macOS and threers' `videotoolbox` feature".into())
}
Backend::Native => Inner::Native(Box::new(NativeDecoder::new(track)?)),
};
Ok(Decoder { inner })
}
pub fn backend(&self) -> Backend {
match self.inner {
#[cfg(all(feature = "videotoolbox", target_os = "macos"))]
Inner::VideoToolbox(_) => Backend::VideoToolbox,
Inner::Native(_) => Backend::Native,
}
}
pub fn size(&self) -> (u32, u32) {
match &self.inner {
#[cfg(all(feature = "videotoolbox", target_os = "macos"))]
Inner::VideoToolbox(d) => d.size(),
Inner::Native(d) => d.size(),
}
}
pub fn push(&mut self, sample: &[u8], pts: i64, dts: i64, duration: i64) -> Result<(), String> {
match &mut self.inner {
#[cfg(all(feature = "videotoolbox", target_os = "macos"))]
Inner::VideoToolbox(d) => d.push(sample, pts, dts, duration),
Inner::Native(d) => d.push(sample, pts, dts, duration),
}
}
pub fn take(&mut self, pts: i64) -> Option<Vec<u8>> {
match &mut self.inner {
#[cfg(all(feature = "videotoolbox", target_os = "macos"))]
Inner::VideoToolbox(d) => d.take(pts),
Inner::Native(d) => d.take(pts),
}
}
}
fn config_track(codec: TrackCodec, config: &[u8], width: u32, height: u32, timescale: u32) -> VideoTrack {
VideoTrack { codec, width, height, config: config.to_vec(), timescale, samples: Vec::new() }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::codec::h264::{CompressedEncoder, Yuv420Frame};
#[test]
fn decodes_on_every_backend_this_build_has() {
let (w, h) = (64u32, 32u32);
let rgba: Vec<u8> = (0..w * h).flat_map(|i| [(i % 64 * 4) as u8, 120, 200, 255]).collect();
let enc = CompressedEncoder::new(w, h, 20);
let sample = enc.encode_sample(&Yuv420Frame::from_rgba(w, h, &rgba));
let track = config_track(TrackCodec::H264, &enc.avcc(), w, h, 90_000);
for backend in [Backend::VideoToolbox, Backend::Native] {
let Ok(mut dec) = Decoder::with_backend(&track, backend) else {
assert!(!backend.available(), "{backend:?} is available but would not open");
continue;
};
assert_eq!(dec.backend(), backend);
dec.push(&sample, 0, 0, 3600).unwrap();
let bgra = dec.take(0).expect("a picture");
assert_eq!(bgra.len(), (w * h * 4) as usize);
let px = &bgra[((16 * w + 32) * 4) as usize..][..4];
assert!((px[0] as i32 - 200).abs() < 12, "{backend:?}: {px:?}");
}
let auto = Decoder::from_config(TrackCodec::H264, &enc.avcc(), w, h, 90_000).unwrap();
assert_eq!(auto.backend(), Backend::preferred());
}
}