FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#402: WebM transparency (VP9 alpha in BlockAdditions)
liangforstudy4f8dcdf5877e
diff --git a/crates/codecs/src/lib.rs b/crates/codecs/src/lib.rsindex a31da3a7e..f7de9e3d2 100644--- a/crates/codecs/src/lib.rs+++ b/crates/codecs/src/lib.rs@@ -30,6 +30,7 @@ pub mod mxf;pub mod ogg;mod stream_color;pub mod video;+mod webm_alpha;use std::sync::{Arc, RwLock};diff --git a/crates/codecs/src/mkv.rs b/crates/codecs/src/mkv.rsindex cef204d9d..2efb5b537 100644--- a/crates/codecs/src/mkv.rs+++ b/crates/codecs/src/mkv.rs@@ -58,6 +58,8 @@ pub struct MkvSource {/// Video codec as an ISO-BMFF sample entry (for the decoder factories).ventry: Option<SampleEntry>,video: GopCache,+ /// The alpha layer of a VP9 track with `AlphaMode` 1 (WebM transparency, #402).+ alpha: Option<Arc<crate::webm_alpha::AlphaLayer>>,/// The playable audio tracks in file order: `info.audio_streams[k]` describes `audios[k]`.audios: Vec<MkvAudio>,}@@ -380,7 +382,12 @@ impl MkvSource {Some(MkvAudio { track: i, state, starts, preroll })}).collect();- Ok(Self { info, bytes, file, vtrack, ventry, video: GopCache::new(explicit_color).with_rotation(rotation), audios })+ // WebM transparency: VP9 alpha in each block's BlockAdditional (ID 1)+ let alpha = vtrack.and_then(|i| file.tracks.get(i)).filter(|t| {+ matches!(t.codec, Codec::Vp9 { .. }) && t.video.as_ref().is_some_and(|v| v.alpha_mode != 0) && t.samples.iter().any(|s| s.addition.is_some())+ });+ let alpha = alpha.map(|t| Arc::new(crate::webm_alpha::AlphaLayer::new(bytes.clone(), t.samples.iter().map(|s| (s.pts, s.addition)))));+ Ok(Self { info, bytes, file, vtrack, ventry, video: GopCache::new(explicit_color).with_rotation(rotation), alpha, audios })}fn read(&self, track: usize, i: usize) -> crate::Result<Vec<u8>> {@@ -486,9 +493,15 @@ impl VideoSamples for MkvVideo<'_> {self.src.read(self.track, i)}fn make_decoder(&self) -> crate::Result<Box<dyn VideoDecoder>> {- match &self.src.ventry {- Some(e) => make_video_decoder(e),- None => Err(CodecError::Unsupported(format!("no decoder for {} video", codec_label(&self.src.file.tracks[self.track].codec)))),+ let Some(e) = &self.src.ventry else {+ return Err(CodecError::Unsupported(format!("no decoder for {} video", codec_label(&self.src.file.tracks[self.track].codec))));+ };+ let color = make_video_decoder(e)?;+ let Some(layer) = &self.src.alpha else { return Ok(color) };+ // the alpha layer always goes through our own decoder (it is luma only)+ match crate::software_video_decoder(e) {+ Ok(alpha) => Ok(Box::new(crate::webm_alpha::AlphaDecoder::new(color, alpha, layer.clone()))),+ Err(_) => Ok(color),}}}diff --git a/crates/codecs/src/tests.rs b/crates/codecs/src/tests.rsindex 3f1f5047d..751b18fdf 100644--- a/crates/codecs/src/tests.rs+++ b/crates/codecs/src/tests.rs@@ -387,6 +387,68 @@ fn mkv_vp9_422_seeks_bit_exact() {check_vp9_seeks("noise_vp9_422.mkv", "Matroska", "yuv422p", &["-profile:v", "1", "-deadline", "realtime", "-speed", "8"], 30);}+/// #402: a WebM whose VP9 track carries an alpha layer (`AlphaMode` 1, a second VP9 stream in+/// each block's `BlockAdditional`) decodes with that alpha. Frames requested out of order (seeks+/// across key frames, then forward) match libvpx's decode exactly, colour and alpha.+#[test]+fn vp9_webm_alpha_layer_matches_libvpx() {+ let (w, h, frames) = (176usize, 144usize, 30usize);+ // the alpha changes every frame, so a picture paired with the wrong alpha frame shows+ let Some(path) = fixture_path(+ "alpha_vp9.webm",+ &[+ "-f",+ "lavfi",+ "-i",+ "testsrc2=s=176x144:r=25,format=yuva420p,geq=lum='lum(X,Y)':cb='cb(X,Y)':cr='cr(X,Y)':a='mod(X*2+N*7\\,256)'",+ "-frames:v",+ "30",+ "-c:v",+ "libvpx-vp9",+ "-pix_fmt",+ "yuva420p",+ "-g",+ "10",+ "-deadline",+ "realtime",+ "-speed",+ "8",+ "-b:v",+ "600k",+ ],+ ) else {+ return;+ };+ // the reference: ffmpeg's libvpx decoder (its native VP9 decoder ignores the alpha layer)+ let src_path = path.to_string_lossy().into_owned();+ let Some(reference) =+ fixture("alpha_vp9.webm.yuva420p.yuv", &["-c:v", "libvpx-vp9", "-i", &src_path, "-fps_mode", "passthrough", "-f", "rawvideo", "-pix_fmt", "yuva420p"])+ else {+ return;+ };+ let (luma, chroma) = (w * h, (w / 2) * (h / 2));+ let frame_len = luma * 2 + chroma * 2;+ if reference.len() != frame_len * frames {+ eprintln!("libvpx reference has {} bytes, not {}; skipping", reference.len(), frame_len * frames);+ return;+ }+ let bytes: Arc<[u8]> = std::fs::read(&path).unwrap().into();+ let src = crate::open_bytes("alpha_vp9.webm", bytes).unwrap();+ let info = src.info().clone();+ assert_eq!(info.container, "WebM");+ assert!(info.video.as_ref().unwrap().has_alpha);+ let rate = info.frame_rate();+ let order: Vec<usize> = [29, 3, 17, 12, 0, 25, 21, 9].into_iter().chain(10..frames).collect();+ for k in order {+ let f = src.video_frame(FrameRequest::full(rate.tick_of(k as i64))).unwrap();+ let r = &reference[k * frame_len..(k + 1) * frame_len];+ let filmcraft_frame::PixelData::Yuv8 { planes, alpha, .. } = &f.data else { panic!("frame {k}: not 8-bit YUV: {}", f.format_label()) };+ assert_eq!(planes[0].as_slice(), &r[..luma], "frame {k}: luma");+ let alpha = alpha.as_ref().unwrap_or_else(|| panic!("frame {k}: no alpha plane"));+ assert_eq!(alpha.as_slice(), &r[luma + 2 * chroma..], "frame {k}: alpha");+ }+}+/// Every sample flagged as sync (as in an MP4 without `stss`): the GOP cache must still start/// decoding at a real VP9 key frame.#[test]diff --git a/crates/codecs/src/webm_alpha.rs b/crates/codecs/src/webm_alpha.rsnew file mode 100644index 000000000..e602486e7--- /dev/null+++ b/crates/codecs/src/webm_alpha.rs@@ -0,0 +1,292 @@+//! WebM alpha (#402): a VP9 track with `AlphaMode` 1 carries, in each block's `BlockAdditional`+//! with `BlockAddID` 1, a second VP9 stream coded independently of the colour one. Its luma plane,+//! full range, is the frame's alpha (the WebM convention, as libvpx-based decoders read it).+//!+//! [`AlphaDecoder`] wraps the colour decoder: every sample it decodes, it also decodes that+//! sample's alpha frame with a second (software) VP9 decoder and attaches the luma plane to the+//! colour picture with the same timestamp, holding a colour picture back until its alpha is out. The alpha stream has key frames of its own, which need+//! not coincide with the colour ones: after a seek (or any other break in the order samples are+//! fed), the alpha decoder restarts from the nearest alpha key frame at or before the sample.+//!+//! Alpha is best effort: a missing, damaged or undecodable alpha frame leaves that picture opaque,+//! never fails the colour decode.++use std::collections::{HashMap, VecDeque};+use std::sync::Arc;++use filmcraft_frame::{PixelData, VideoFrame};++use crate::video::{DecodedFrame, VideoDecoder};++/// How far back the alpha decoder looks for a key frame when it resynchronises.+const MAX_RESYNC: usize = 600;+/// Decoded alpha pictures waiting for their colour picture, and colour pictures waiting for their+/// alpha (VP9 pictures come out in order, so a few are plenty; past that a picture goes out opaque+/// rather than stall).+const MAX_PENDING: usize = 8;++/// Where each sample's alpha frame is stored.+pub(crate) struct AlphaLayer {+ src: crate::Src,+ /// Per sample, in file (decode) order.+ additions: Vec<Option<(u64, u32)>>,+ pts: Vec<i64>,+ /// First sample with each pts.+ by_pts: HashMap<i64, usize>,+}++impl AlphaLayer {+ /// `samples`: (pts, addition) of every sample of the track, in file order.+ pub(crate) fn new(src: crate::Src, samples: impl Iterator<Item = (i64, Option<(u64, u32)>)>) -> Self {+ let (mut additions, mut all_pts) = (Vec::new(), Vec::new());+ let mut by_pts = HashMap::new();+ for (i, (pts, add)) in samples.enumerate() {+ additions.push(add);+ all_pts.push(pts);+ by_pts.entry(pts).or_insert(i);+ }+ Self { src, additions, pts: all_pts, by_pts }+ }++ fn read(&self, i: usize) -> Option<Vec<u8>> {+ let (offset, size) = (*self.additions.get(i)?)?;+ let end = offset.checked_add(size as u64)?;+ if end > self.src.0.len() {+ return None;+ }+ let mut data = vec![0; size as usize];+ self.src.0.read_at(offset, &mut data).ok()?;+ Some(data)+ }+}++/// A colour decoder plus the decoder of its alpha layer.+///+/// Both decoders may hand pictures back later than the sample that made them (frame threading),+/// and not necessarily after the same call. A colour picture is therefore held until its alpha+/// picture has come out, or until it is certain that it won't: the alpha decoder has moved past+/// it (pictures come out in order), it failed, or the sample has no alpha frame.+pub(crate) struct AlphaDecoder {+ color: Box<dyn VideoDecoder>,+ alpha: Box<dyn VideoDecoder>,+ layer: Arc<AlphaLayer>,+ /// Sample the alpha decoder expects next; `None` after a reset or a failure.+ next: Option<usize>,+ /// Decoded alpha pictures not yet paired, by pts.+ pending: VecDeque<(i64, VideoFrame)>,+ /// Latest pts the alpha decoder has produced since the last reset.+ alpha_max: Option<i64>,+ /// Colour pictures waiting for their alpha, in output order.+ held: VecDeque<DecodedFrame>,+}++impl AlphaDecoder {+ pub(crate) fn new(color: Box<dyn VideoDecoder>, alpha: Box<dyn VideoDecoder>, layer: Arc<AlphaLayer>) -> Self {+ Self { color, alpha, layer, next: None, pending: VecDeque::new(), alpha_max: None, held: VecDeque::new() }+ }++ fn keep(&mut self, frames: Vec<DecodedFrame>) {+ for f in frames {+ self.alpha_max = Some(self.alpha_max.map_or(f.pts, |m| m.max(f.pts)));+ self.pending.push_back((f.pts, f.frame));+ }+ while self.pending.len() > MAX_PENDING {+ self.pending.pop_front();+ }+ }++ /// Decode the alpha frame of the sample with `pts`, first catching up from an alpha key frame+ /// when the sample doesn't follow the previous one.+ fn feed_alpha(&mut self, pts: i64) {+ let Some(&i) = self.layer.by_pts.get(&pts) else {+ self.next = None;+ return;+ };+ if self.next != Some(i) {+ self.alpha.reset();+ self.pending.clear();+ self.alpha_max = None;+ let is_key = |k: usize| self.layer.read(k).is_some_and(|d| filmcraft_vp9::is_keyframe(&d));+ let start = (i.saturating_sub(MAX_RESYNC)..=i).rev().find(|&k| is_key(k));+ if let Some(start) = start {+ for k in start..i {+ let (Some(d), Some(&p)) = (self.layer.read(k), self.layer.pts.get(k)) else { continue };+ match self.alpha.decode(&d, p) {+ Ok(frames) => self.keep(frames),+ Err(_) => break,+ }+ }+ }+ }+ self.next = Some(i + 1);+ let Some(d) = self.layer.read(i) else { return };+ match self.alpha.decode(&d, pts) {+ Ok(frames) => self.keep(frames),+ Err(e) => {+ log::debug!("WebM alpha frame at pts {pts}: {e}");+ self.next = None;+ }+ }+ }++ /// Whether the alpha picture for `pts` can still come out of the alpha decoder.+ fn alpha_may_come(&self, pts: i64) -> bool {+ let has_frame = self.layer.by_pts.get(&pts).is_some_and(|&i| self.layer.additions.get(i).is_some_and(|a| a.is_some()));+ has_frame && self.next.is_some() && self.alpha_max.is_none_or(|m| m < pts)+ }++ /// Release held colour pictures, in order, as far as their alpha is settled. `all`: release+ /// every one (flush), with or without alpha.+ fn release(&mut self, all: bool) -> Vec<DecodedFrame> {+ let mut out = Vec::new();+ while let Some(f) = self.held.front() {+ let pts = f.pts;+ let found = self.pending.iter().position(|(p, _)| *p == pts);+ if found.is_none() && !all && self.held.len() <= MAX_PENDING && self.alpha_may_come(pts) {+ break;+ }+ let Some(mut f) = self.held.pop_front() else { break };+ if let Some(k) = found+ && let Some((_, a)) = self.pending.remove(k)+ {+ set_alpha(&mut f.frame, &a);+ }+ out.push(f);+ }+ out+ }+}++impl VideoDecoder for AlphaDecoder {+ fn decode(&mut self, sample: &[u8], pts: i64) -> crate::Result<Vec<DecodedFrame>> {+ let out = self.color.decode(sample, pts)?;+ self.held.extend(out);+ self.feed_alpha(pts);+ Ok(self.release(false))+ }+ fn flush(&mut self) -> Vec<DecodedFrame> {+ let out = self.color.flush();+ self.held.extend(out);+ let rest = self.alpha.flush();+ self.keep(rest);+ self.release(true)+ }+ fn reset(&mut self) {+ self.color.reset();+ self.alpha.reset();+ self.next = None;+ self.pending.clear();+ self.alpha_max = None;+ self.held.clear();+ }+ fn name(&self) -> &str {+ self.color.name()+ }+ fn intra_only(&self) -> bool {+ self.color.intra_only()+ }+ fn is_random_access(&self, sample: &[u8]) -> Option<bool> {+ self.color.is_random_access(sample)+ }+ fn is_disposable(&self, sample: &[u8]) -> bool {+ self.color.is_disposable(sample)+ }+ fn set_draft(&mut self, on: bool) {+ self.color.set_draft(on);+ self.alpha.set_draft(on);+ }+ fn thread_limited(&self) -> bool {+ self.color.thread_limited()+ }+}++/// The luma plane of `a` (an alpha picture) as `frame`'s alpha, converted to `frame`'s bit depth.+/// Nothing happens when the sizes differ or either picture has no usable planes.+pub(crate) fn set_alpha(frame: &mut VideoFrame, a: &VideoFrame) {+ if (frame.width, frame.height) != (a.width, a.height) {+ return;+ }+ let n = frame.width as usize * frame.height as usize;+ // alpha codes as 16-bit with their depth, so every pairing converts through one path+ let (codes, abits): (Vec<u16>, u32) = match &a.data {+ PixelData::Yuv8 { planes, .. } => (planes[0].iter().map(|&v| v as u16).collect(), 8),+ PixelData::Yuv16 { planes, bits, .. } => (planes[0].to_vec(), (*bits).clamp(1, 16)),+ _ => return,+ };+ if codes.len() != n {+ return;+ }+ let rescale = |v: u16, to: u32| -> u32 {+ let (from_max, to_max) = ((1u32 << abits) - 1, (1u32 << to.clamp(1, 16)) - 1);+ (v as u32 * to_max + from_max / 2) / from_max+ };+ match &mut frame.data {+ PixelData::Yuv8 { alpha, .. } => *alpha = Some(Arc::new(codes.iter().map(|&v| rescale(v, 8) as u8).collect())),+ PixelData::Yuv16 { alpha, bits, .. } => {+ let to = *bits;+ *alpha = Some(Arc::new(codes.iter().map(|&v| rescale(v, to) as u16).collect()));+ }+ PixelData::Rgba8(rgba) => {+ let px = Arc::make_mut(rgba);+ for (p, &v) in px.chunks_exact_mut(4).zip(&codes) {+ if let Some(slot) = p.get_mut(3) {+ *slot = rescale(v, 8) as u8;+ }+ }+ }+ PixelData::RgbaF32(_) => {}+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use filmcraft_frame::Chroma;++ fn yuv8(w: u32, h: u32, luma: Vec<u8>) -> VideoFrame {+ let n = (w * h) as usize;+ let c = n / 4;+ let data = PixelData::Yuv8 { planes: [Arc::new(luma), Arc::new(vec![128; c]), Arc::new(vec![128; c])], chroma: Chroma::C420, alpha: None };+ VideoFrame { width: w, height: h, data, color: Default::default(), par: (1, 1), pts: filmcraft_time::Tick::ZERO }+ }++ fn yuv16(w: u32, h: u32, bits: u32) -> VideoFrame {+ let n = (w * h) as usize;+ let data =+ PixelData::Yuv16 { planes: [Arc::new(vec![0; n]), Arc::new(vec![0; n / 4]), Arc::new(vec![0; n / 4])], chroma: Chroma::C420, bits, alpha: None };+ VideoFrame { width: w, height: h, data, color: Default::default(), par: (1, 1), pts: filmcraft_time::Tick::ZERO }+ }++ #[test]+ fn alpha_luma_becomes_the_alpha_plane_at_the_frame_depth() {+ let a = yuv8(2, 2, vec![0, 128, 255, 64]);+ // 8-bit picture: the luma as is+ let mut f = yuv8(2, 2, vec![9; 4]);+ set_alpha(&mut f, &a);+ let PixelData::Yuv8 { alpha, .. } = &f.data else { panic!() };+ assert_eq!(alpha.as_deref(), Some(&vec![0, 128, 255, 64]));+ // 10-bit picture: full range kept (255 → 1023)+ let mut f = yuv16(2, 2, 10);+ set_alpha(&mut f, &a);+ let PixelData::Yuv16 { alpha, .. } = &f.data else { panic!() };+ assert_eq!(alpha.as_deref(), Some(&vec![0, 514, 1023, 257]));+ // RGBA picture (VP9 RGB profiles): the A channel+ let mut f = VideoFrame::rgba8(2, 2, vec![7; 16]);+ set_alpha(&mut f, &a);+ let PixelData::Rgba8(px) = &f.data else { panic!() };+ assert_eq!(px.chunks(4).map(|p| p[3]).collect::<Vec<_>>(), [0, 128, 255, 64]);+ }++ #[test]+ fn an_alpha_picture_of_another_size_is_ignored() {+ let mut f = yuv8(4, 2, vec![9; 8]);+ set_alpha(&mut f, &yuv8(2, 2, vec![0; 4]));+ let PixelData::Yuv8 { alpha, .. } = &f.data else { panic!() };+ assert!(alpha.is_none());+ // a luma plane shorter than the picture (damaged) is ignored too+ let mut f = yuv8(2, 2, vec![9; 4]);+ set_alpha(&mut f, &yuv8(2, 2, vec![0; 3]));+ let PixelData::Yuv8 { alpha, .. } = &f.data else { panic!() };+ assert!(alpha.is_none());+ }+}diff --git a/crates/matroska/src/demux.rs b/crates/matroska/src/demux.rsindex 9344059cd..4278a1b83 100644--- a/crates/matroska/src/demux.rs+++ b/crates/matroska/src/demux.rs@@ -301,6 +301,9 @@ struct BlockRef {discardable: bool,invisible: bool,discard_padding: Option<i64>,+ /// (offset, size) of the `BlockAdditional` with `BlockAddID` 1 (WebM: the VP8/VP9 alpha+ /// layer), kept for unlaced blocks only.+ addition: Option<(u64, u32)>,}#[derive(Clone, Copy, Debug)]@@ -318,6 +321,7 @@ struct FrameRef {discard_padding: Option<i64>,block_offset: u64,lace: u16,+ addition: Option<(u64, u32)>,}fn track_lookup(tracks: &[Track]) -> impl Fn(u64) -> Option<usize> + '_ {@@ -354,6 +358,36 @@ fn parse_block<S: ByteSource + ?Sized>(Ok(Some((track, rel, flags, frames)))}+/// The `BlockAdditional` with `BlockAddID` 1 (the default ID) in a `BlockAdditions` element:+/// (offset, size) of its data. Other IDs, empty additions and damaged children are skipped.+fn block_addition<S: ByteSource + ?Sized>(io: &mut Io<'_, S>, start: u64, end: u64) -> Result<Option<(u64, u32)>> {+ let mut pos = start;+ while pos < end {+ let Some(more) = io.header(pos, end)? else { break };+ let me = more.end(end);+ if more.id == BLOCK_MORE {+ let (mut id, mut data) = (1u64, None);+ let mut p = more.data_start();+ while p < me {+ let Some(c) = io.header(p, me)? else { break };+ let ce = c.end(me);+ let len = ce.saturating_sub(c.data_start());+ match c.id {+ BLOCK_ADD_ID => id = ebml::uint(io.peek(c.data_start(), len.min(8) as usize)?),+ BLOCK_ADDITIONAL => data = u32::try_from(len).ok().filter(|&n| n > 0).map(|n| (c.data_start(), n)),+ _ => {}+ }+ p = ce;+ }+ if id == 1 && data.is_some() {+ return Ok(data);+ }+ }+ pos = me;+ }+ Ok(None)+}+/// Parse one SimpleBlock or BlockGroup element.fn parse_block_elem<S: ByteSource + ?Sized>(io: &mut Io<'_, S>,@@ -376,17 +410,19 @@ fn parse_block_elem<S: ByteSource + ?Sized>(discardable: flags & 0x01 != 0,invisible: flags & 0x08 != 0,discard_padding: None,+ addition: None,}))}BLOCK_GROUP => {let mut pos = ds;let mut block = None;- let (mut dur, mut has_ref, mut pad) = (None, false, None);+ let (mut dur, mut has_ref, mut pad, mut addition) = (None, false, None, None);while pos < end {let Some(c) = io.header(pos, end)? else { break };let ce = c.end(end);match c.id {BLOCK => block = parse_block(io, c.data_start(), ce - c.data_start(), lookup)?,+ BLOCK_ADDITIONS => addition = block_addition(io, c.data_start(), ce)?,BLOCK_DURATION => dur = Some(ebml::uint(io.peek(c.data_start(), (ce - c.data_start()) as usize)?)),REFERENCE_BLOCK => has_ref = true,DISCARD_PADDING => pad = Some(ebml::int(io.peek(c.data_start(), (ce - c.data_start()) as usize)?)),@@ -395,6 +431,8 @@ fn parse_block_elem<S: ByteSource + ?Sized>(pos = ce;}let Some((track, rel, flags, frames)) = block else { return Ok(None) };+ // with lacing, an addition belongs to the block, not to one of its frames+ let addition = addition.filter(|_| frames.len() == 1);Ok(Some(BlockRef {track,block_offset: h.pos,@@ -405,6 +443,7 @@ fn parse_block_elem<S: ByteSource + ?Sized>(discardable: false,invisible: flags & 0x08 != 0,discard_padding: pad,+ addition,}))}_ => Ok(None),@@ -450,6 +489,7 @@ fn expand(b: &BlockRef, t: &Track, scale: u64, skip: u16, out: &mut impl Extend<discard_padding: b.discard_padding,block_offset: b.block_offset,lace: i as u16,+ addition: b.addition,}}));}@@ -780,6 +820,7 @@ fn add_blocks(file: &mut MkvFile, cluster: u32, blocks: &[BlockRef]) {cluster,block_offset: f.block_offset,lace: f.lace,+ addition: f.addition,});}}@@ -838,6 +879,20 @@ impl MkvFile {Ok(data)}+ /// The `BlockAdditional` (ID 1) of sample `index` of `track`, `None` when the block has none+ /// (see [`Sample::addition`]). Stored as is: header stripping applies to frames only.+ pub fn read_sample_addition<S: ByteSource + ?Sized>(&self, src: &S, track: usize, index: usize) -> Result<Option<Vec<u8>>> {+ let t = self.tracks.get(track).ok_or(Error::NoSuchTrack(track))?;+ let s = t.samples.get(index).ok_or(Error::NoSuchSample(index))?;+ let Some((offset, size)) = s.addition else { return Ok(None) };+ if offset.checked_add(size as u64).is_none_or(|end| end > src.len()) {+ return Err(Error::Invalid("block addition past end of file".into()));+ }+ let mut data = vec![0; size as usize];+ src.read_at(offset, &mut data)?;+ Ok(Some(data))+ }+/// Keyframe sample of `track` with the greatest pts ≤ `time_ns` (indexed files only).pub fn keyframe_before(&self, track: usize, time_ns: i64) -> Option<usize> {let t = self.tracks.get(track)?;diff --git a/crates/matroska/src/ids.rs b/crates/matroska/src/ids.rsindex 6334f4517..3b7e025c4 100644--- a/crates/matroska/src/ids.rs+++ b/crates/matroska/src/ids.rs@@ -55,6 +55,9 @@ pub const ENCRYPTED_BLOCK: u32 = 0xAF;pub const SILENT_TRACKS: u32 = 0x5854;pub const BLOCK: u32 = 0xA1;pub const BLOCK_ADDITIONS: u32 = 0x75A1;+pub const BLOCK_MORE: u32 = 0xA6;+pub const BLOCK_ADD_ID: u32 = 0xEE;+pub const BLOCK_ADDITIONAL: u32 = 0xA5;pub const BLOCK_DURATION: u32 = 0x9B;pub const REFERENCE_PRIORITY: u32 = 0xFA;pub const REFERENCE_BLOCK: u32 = 0xFB;diff --git a/crates/matroska/src/track.rs b/crates/matroska/src/track.rsindex af8af356c..1288895fd 100644--- a/crates/matroska/src/track.rs+++ b/crates/matroska/src/track.rs@@ -191,6 +191,10 @@ pub struct Sample {pub block_offset: u64,/// Index of this frame within a laced block.pub lace: u16,+ /// (offset, size) of the block's `BlockAdditional` with `BlockAddID` 1, when it has one and+ /// is not laced. In WebM this is the alpha layer of a VP8/VP9 track with `AlphaMode` 1: a+ /// second, independently coded stream whose luma plane is the frame's alpha.+ pub addition: Option<(u64, u32)>,}/// A Matroska track (`TrackEntry`) plus, once indexed, its sample table.