Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

M9.3: HEVC (Main/Main 10) wired into codecs — MP4/MOV hvc1/hev1 import and playback

Brandon Thomas345b4162cf57
diff --git a/crates/codecs/Cargo.toml b/crates/codecs/Cargo.toml
index 67f6ff007..5af91c946 100644
--- a/crates/codecs/Cargo.toml
+++ b/crates/codecs/Cargo.toml
@@ -17,6 +17,7 @@ filmcraft-media = { workspace = true }
filmcraft-isobmff = { workspace = true }
filmcraft-bitstream = { workspace = true }
filmcraft-h264 = { workspace = true }
+filmcraft-hevc = { workspace = true }
filmcraft-prores = { workspace = true }
filmcraft-aac = { workspace = true }
thiserror = { workspace = true }
diff --git a/crates/codecs/src/lib.rs b/crates/codecs/src/lib.rs
index b0f5e43e9..92d0d8463 100644
--- a/crates/codecs/src/lib.rs
+++ b/crates/codecs/src/lib.rs
@@ -43,7 +43,7 @@ impl From<CodecError> for filmcraft_media::MediaError {
fn factories() -> &'static RwLock<Vec<VideoDecoderFactory>> {
static F: std::sync::OnceLock<RwLock<Vec<VideoDecoderFactory>>> = std::sync::OnceLock::new();
- F.get_or_init(|| RwLock::new(vec![video::h264_factory, video::prores_factory, video::mjpeg_factory]))
+ F.get_or_init(|| RwLock::new(vec![video::h264_factory, video::hevc_factory, video::prores_factory, video::mjpeg_factory]))
}
/// Register a video decoder factory (tried before previously registered ones).
diff --git a/crates/codecs/src/video.rs b/crates/codecs/src/video.rs
index 66e5d8bcf..76ee775cf 100644
--- a/crates/codecs/src/video.rs
+++ b/crates/codecs/src/video.rs
@@ -125,6 +125,93 @@ pub fn h264_factory(e: &SampleEntry) -> Option<Result<Box<dyn VideoDecoder>>> {
}
}
+/// Our pure-Rust HEVC decoder (Main / Main 10, frame-threaded).
+pub struct HevcDecoder {
+ hvcc: Vec<u8>,
+ dec: filmcraft_hevc::Decoder,
+}
+
+impl HevcDecoder {
+ pub fn new(hvcc: Vec<u8>) -> Result<Self> {
+ let dec = filmcraft_hevc::Decoder::from_hvcc(&hvcc).map_err(|e| CodecError::Decode(e.to_string()))?;
+ Ok(Self { hvcc, dec })
+ }
+ fn convert(p: filmcraft_hevc::Picture) -> DecodedFrame {
+ use filmcraft_hevc::Plane;
+ use std::sync::Arc;
+ fn tight<T: Copy>(src: &[T], stride: usize, w: usize, h: usize) -> Vec<T> {
+ if stride == w && src.len() >= w * h {
+ return src[..w * h].to_vec();
+ }
+ let mut out = Vec::with_capacity(w * h);
+ for y in 0..h {
+ out.extend_from_slice(&src[y * stride..y * stride + w]);
+ }
+ out
+ }
+ let (w, h) = (p.width as usize, p.height as usize);
+ let (cw, ch) = (p.chroma_width as usize, p.chroma_height as usize);
+ let data = match (&p.y, &p.u, &p.v) {
+ (Plane::U8(y), Plane::U8(u), Plane::U8(v)) => filmcraft_frame::PixelData::Yuv8 {
+ planes: [Arc::new(tight(y, p.y_stride, w, h)), Arc::new(tight(u, p.uv_stride, cw, ch)), Arc::new(tight(v, p.uv_stride, cw, ch))],
+ chroma: filmcraft_frame::Chroma::C420,
+ alpha: None,
+ },
+ _ => {
+ let wide = |pl: &Plane| -> Vec<u16> { (0..pl.len()).map(|i| pl.get(i)).collect() };
+ filmcraft_frame::PixelData::Yuv16 {
+ planes: [
+ Arc::new(tight(&wide(&p.y), p.y_stride, w, h)),
+ Arc::new(tight(&wide(&p.u), p.uv_stride, cw, ch)),
+ Arc::new(tight(&wide(&p.v), p.uv_stride, cw, ch)),
+ ],
+ chroma: filmcraft_frame::Chroma::C420,
+ bits: p.bit_depth,
+ alpha: None,
+ }
+ }
+ };
+ let mut color = filmcraft_color::ColorInfo { matrix: filmcraft_frame::default_matrix(p.width, p.height), ..filmcraft_color::ColorInfo::REC709 };
+ if let Some(m) = filmcraft_color::Matrix::from_code(p.color.matrix) {
+ color.matrix = m;
+ }
+ if let Some(t) = filmcraft_color::Transfer::from_code(p.color.transfer) {
+ color.transfer = t;
+ }
+ if p.color.full_range {
+ color.range = filmcraft_color::Range::Full;
+ }
+ let par = if p.sar.0 > 0 && p.sar.1 > 0 { (p.sar.0 as u32, p.sar.1 as u32) } else { (1, 1) };
+ let frame = VideoFrame { width: p.width, height: p.height, data, color, par, pts: filmcraft_time::Tick::ZERO };
+ DecodedFrame { pts: p.pts, frame }
+ }
+}
+
+impl VideoDecoder for HevcDecoder {
+ fn decode(&mut self, sample: &[u8], pts: i64) -> Result<Vec<DecodedFrame>> {
+ let pics = self.dec.decode(sample, pts).map_err(|e| CodecError::Decode(e.to_string()))?;
+ Ok(pics.into_iter().map(Self::convert).collect())
+ }
+ fn flush(&mut self) -> Vec<DecodedFrame> {
+ self.dec.flush().into_iter().map(Self::convert).collect()
+ }
+ fn reset(&mut self) {
+ if let Ok(d) = filmcraft_hevc::Decoder::from_hvcc(&self.hvcc) {
+ self.dec = d;
+ }
+ }
+ fn name(&self) -> &str {
+ "FilmCraft HEVC"
+ }
+}
+
+pub fn hevc_factory(e: &SampleEntry) -> Option<Result<Box<dyn VideoDecoder>>> {
+ match &e.codec {
+ CodecConfig::Hevc(c) => Some(HevcDecoder::new(c.to_bytes()).map(|d| Box::new(d) as Box<dyn VideoDecoder>)),
+ _ => None,
+ }
+}
+
/// Our ProRes decoder (every frame is intra; slices decode in parallel).
pub struct ProResDecoder;