diff --git a/Cargo.lock b/Cargo.lock index bc4116ce61..c857cd6966 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1438,9 +1438,10 @@ dependencies = [ [[package]] name = "craft-video" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=d6cd7836d364177661b0ae17f48a0805e83a5f32#d6cd7836d364177661b0ae17f48a0805e83a5f32" +source = "git+https://github.com/storytold/craft-libs?rev=045d3865126d3408503c54fea71b08f33262fddf#045d3865126d3408503c54fea71b08f33262fddf" dependencies = [ "craft-media", + "rav1e", "thiserror 2.0.21", ] diff --git a/Cargo.toml b/Cargo.toml index 01239cf5b4..49e4b26bb9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ jixel = "=0.3.4" # Photo metadata: EXIF, IPTC-IIM, XMP packets and GPX track logs (craft-libs#949) craft-metadata = { git = "https://github.com/storytold/craft-libs", rev = "f0f4758c01581d79577b9bf5046c06f4e77b1f18" } craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "d6cd7836d364177661b0ae17f48a0805e83a5f32" } -craft-video = { git = "https://github.com/storytold/craft-libs", rev = "d6cd7836d364177661b0ae17f48a0805e83a5f32", features = ["av1"] } +craft-video = { git = "https://github.com/storytold/craft-libs", rev = "045d3865126d3408503c54fea71b08f33262fddf", features = ["av1", "av1-large-encode"] } craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "f0f4758c01581d79577b9bf5046c06f4e77b1f18" } # craft-update: app-specific pretend versions apply to startup and manual checks. craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] } diff --git a/crates/codecs/src/video.rs b/crates/codecs/src/video.rs index 27223d96f3..17459f3417 100644 --- a/crates/codecs/src/video.rs +++ b/crates/codecs/src/video.rs @@ -831,12 +831,18 @@ pub fn quantizer(quality: u8) -> usize { /// the samples as they come out (in order: no frame reordering). pub struct Av1Encoder { #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] - enc: craft_video::av1::Encoder, + enc: Av1Backend, width: usize, height: usize, has_sample: bool, } +#[cfg(all(feature = "avif", not(target_arch = "wasm32")))] +enum Av1Backend { + Single(craft_video::av1::Encoder), + Tiled(craft_video::av1::LargeEncoder), +} + impl Av1Encoder { pub fn new(c: &VideoConfig) -> Result { let (w, h) = ((c.width & !1) as usize, (c.height & !1) as usize); @@ -850,7 +856,13 @@ impl Av1Encoder { let mut ec = EncoderConfig::new(w as u32, h as u32, c.fps, 1); ec.rate = RateControl::ConstantQ(quantizer(c.quality).clamp(1, 255) as u8); ec.keyint = 120; - let enc = Encoder::new(ec).map_err(|e| bad(format!("AV1: {e}")))?; + let sb_cols = w.div_ceil(64); + let sb_rows = h.div_ceil(64); + let enc = if w > 4096 || h > 4096 || sb_cols * sb_rows > 2304 { + Av1Backend::Tiled(craft_video::av1::LargeEncoder::new(&ec).map_err(|e| bad(format!("AV1: {e}")))?) + } else { + Av1Backend::Single(Encoder::new(ec).map_err(|e| bad(format!("AV1: {e}")))?) + }; Ok(Av1Encoder { enc, width: w, height: h, has_sample: false }) } #[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))] @@ -869,7 +881,13 @@ impl Av1Encoder { pub fn config_record(&self) -> Option> { #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] { - self.has_sample.then(|| self.enc.av1c()) + if !self.has_sample { + return None; + } + match &self.enc { + Av1Backend::Single(enc) => Some(enc.av1c()), + Av1Backend::Tiled(enc) => enc.av1c(), + } } #[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))] { @@ -889,7 +907,11 @@ impl Av1Encoder { let u16_: Vec = u.into_iter().map(u16::from).collect(); let v16: Vec = v.into_iter().map(u16::from).collect(); let frame = craft_video::av1::Frame { y: &y16, u: &u16_, v: &v16, y_stride: self.width, uv_stride: self.width / 2 }; - let pkt = self.enc.encode_with_key(&frame, key).map_err(|e| bad(format!("AV1: {e}")))?; + let pkt = match &mut self.enc { + Av1Backend::Single(enc) => enc.encode_with_key(&frame, key), + Av1Backend::Tiled(enc) => enc.encode_with_key(&frame, key), + } + .map_err(|e| bad(format!("AV1: {e}")))?; self.has_sample = true; Ok(vec![Sample { data: pkt.data, key: pkt.keyframe, duration: periods.max(1) }]) }