diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index e2901225e..abf80f814 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -23,5 +23,5 @@ jobs: # The codec and format code behind features that are off by default. Without these the # coverage build never compiles it, and new code there reads as untested. cargo-features: >- - craft-video/dnx,craft-video/vp8,craft-video/prores,craft-video/h264,craft-video/hevc,craft-video/vp9,craft-video/av1,craft-video/threads,craft-audio/decode,craft-audio/encode,craft-audio/peaks,craft-audio/rayon,craft-audio/serde,craft-media/matroska,craft-media/ogg,craft-media/bitstream,craft-media/serde,craft-mesh/stl,craft-mesh/obj,craft-mesh/ply,craft-mesh/gltf,craft-metafile/write,craft-image-codecs/sgi,craft-image-codecs/sunraster + craft-video/dnx,craft-video/vp8,craft-video/prores,craft-video/h264,craft-video/hevc,craft-video/vp9,craft-video/av1,craft-video/threads,craft-audio/decode,craft-audio/encode,craft-audio/peaks,craft-audio/rayon,craft-audio/serde,craft-media/matroska,craft-media/ogg,craft-media/bitstream,craft-media/serde,craft-mesh/stl,craft-mesh/obj,craft-mesh/ply,craft-mesh/gltf,craft-metafile/write,craft-image-codecs/sgi,craft-image-codecs/sunraster,craft-jsonrpc/async-lines secrets: inherit diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index f653e9bba..32211cf89 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -79,6 +79,8 @@ mod write; /// PCM WAVE and AIFF wrappers for interchange essence. #[cfg(feature = "pcm-encode")] pub mod essence_pcm; +/// Streaming WAVE and AIFF headers and sample packing. +pub mod pcm_export; /// Interleaved PCM packet decoding. pub mod pcm_interleaved; /// Range-backed PCM WAVE access and a 16-bit PCM writer. diff --git a/crates/audio/src/pcm_export.rs b/crates/audio/src/pcm_export.rs new file mode 100644 index 000000000..618ba281d --- /dev/null +++ b/crates/audio/src/pcm_export.rs @@ -0,0 +1,287 @@ +//! Audio-only files: WAV (RIFF, little-endian PCM; RF64 past 4 GiB) and AIFF (big-endian PCM), +//! 16, 24 or 32-bit integer or 32-bit float ([`SampleFormat`]), for streaming export. +//! Float WAV is `WAVE_FORMAT_IEEE_FLOAT` with a `fact` chunk; float AIFF is AIFF-C (`fl32`). +//! +//! The header is written first from the known length (`header`), then the samples in chunks +//! (`encode`) and the pad byte (`pad`), so an export streams to disk instead of building the +//! file in memory. + +/// PCM representation in an exported file. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum SampleFormat { + /// 16-bit signed integer. + Int16, + /// 24-bit signed integer. + Int24, + /// 32-bit signed integer. + Int32, + /// 32-bit IEEE float. + Float32, +} + +impl SampleFormat { + /// Bits per sample. + pub fn bits(self) -> u16 { + match self { + Self::Int16 => 16, + Self::Int24 => 24, + Self::Int32 | Self::Float32 => 32, + } + } + /// Bytes per sample. + pub fn bytes(self) -> usize { + usize::from(self.bits() / 8) + } + /// Whether this is a floating point format. + pub fn is_float(self) -> bool { + self == Self::Float32 + } + /// Clip and quantize one sample with symmetric integer full scale; NaN becomes silence. + pub fn quantise(self, s: f32) -> i32 { + let s = s.clamp(-1.0, 1.0); + match self { + Self::Int16 => (s * 32_767.0).round() as i32, + Self::Int24 => (s * 8_388_607.0).round() as i32, + Self::Int32 => (f64::from(s) * 2_147_483_647.0).round() as i32, + Self::Float32 => 0, + } + } + /// Sanitize a floating sample: NaN becomes silence, infinity becomes the largest finite value. + pub fn float(s: f32) -> f32 { + if s.is_nan() { 0.0 } else { s.clamp(f32::MIN, f32::MAX) } + } +} + +/// The container of an audio-only export. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PcmContainer { + /// RIFF/WAVE or RF64. + Wav, + /// AIFF or AIFF-C. + Aiff, +} + +/// Bytes of sample data for `frames` frames, or None when that does not fit 64 bits. +fn data_len(frames: u64, channels: u16, format: SampleFormat) -> Option { + frames.checked_mul(u64::from(channels))?.checked_mul(format.bytes() as u64) +} + +/// The pad byte after an odd-sized data chunk (RIFF and AIFF chunks are word-aligned). +pub fn pad(frames: u64, channels: u16, format: SampleFormat) -> &'static [u8] { + if data_len(frames, channels, format).is_some_and(|n| n & 1 == 1) { &[0] } else { &[] } +} + +/// `KSDATAFORMAT_SUBTYPE_PCM` / `_IEEE_FLOAT` after their first two bytes (the format tag). +const SUBTYPE_TAIL: [u8; 14] = [0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71]; + +/// AIFF-C format version 1 (`FVER`). +const AIFC_VERSION_1: u32 = 0xA280_5140; + +/// The AIFF-C compression name of `fl32`, as a Pascal string of even length. +const FL32_NAME: &[u8] = b"\x1532-bit floating point"; + +/// Everything before the samples of a file of `frames` frames. A WAV whose RIFF size would not +/// fit 32 bits (4 GiB: 24-bit 5.1 at 96 kHz after about 41 minutes) is written as RF64 (EBU Tech +/// 3306), with the 64-bit sizes in a `ds64` chunk. AIFF has no 64-bit form, so a longer AIFF is +/// an error naming WAV instead. +pub fn header(container: PcmContainer, channels: u16, sample_rate: u32, format: SampleFormat, frames: u64) -> Result, String> { + let too_long = || "the audio is too long to write".to_string(); + if channels == 0 { + return Err("audio needs at least one channel".into()); + } + if sample_rate == 0 { + return Err("audio needs a sample rate".into()); + } + let bits = format.bits(); + let bps = bits / 8; + let float = format.is_float(); + let data = data_len(frames, channels, format).ok_or_else(too_long)?; + let block_align = channels.checked_mul(bps).ok_or("too many audio channels")?; + match container { + PcmContainer::Wav => { + let byte_rate = + u32::try_from(u64::from(sample_rate) * u64::from(block_align)).map_err(|_| "the audio sample rate is too high".to_string())?; + let ext = channels > 2; + // plain PCM has no cbSize; IEEE float has an empty one + let fmt_len: u32 = if ext { + 40 + } else if float { + 18 + } else { + 16 + }; + // every format but integer PCM has a `fact` chunk (the frame count; RF64: in `ds64`) + let fact_len: u64 = if float { 12 } else { 0 }; + let padded = data.checked_add(data & 1).ok_or_else(too_long)?; + // "WAVE" + fmt chunk + fact chunk + data chunk header + samples + let riff = (4 + 8 + u64::from(fmt_len) + fact_len + 8).checked_add(padded).ok_or_else(too_long)?; + let rf64 = riff > u64::from(u32::MAX); + let mut v = Vec::with_capacity(100); + if rf64 { + const DS64: u64 = 8 + 28; + v.extend_from_slice(b"RF64"); + v.extend_from_slice(&u32::MAX.to_le_bytes()); + v.extend_from_slice(b"WAVEds64"); + v.extend_from_slice(&28u32.to_le_bytes()); + v.extend_from_slice(&riff.checked_add(DS64).ok_or_else(too_long)?.to_le_bytes()); + v.extend_from_slice(&data.to_le_bytes()); + v.extend_from_slice(&frames.to_le_bytes()); + v.extend_from_slice(&0u32.to_le_bytes()); // no table entries + } else { + v.extend_from_slice(b"RIFF"); + v.extend_from_slice(&(riff as u32).to_le_bytes()); + v.extend_from_slice(b"WAVE"); + } + let tag: u16 = if float { 3 } else { 1 }; + v.extend_from_slice(b"fmt "); + v.extend_from_slice(&fmt_len.to_le_bytes()); + v.extend_from_slice(&(if ext { 0xFFFE } else { tag }).to_le_bytes()); + v.extend_from_slice(&channels.to_le_bytes()); + v.extend_from_slice(&sample_rate.to_le_bytes()); + v.extend_from_slice(&byte_rate.to_le_bytes()); + v.extend_from_slice(&block_align.to_le_bytes()); + v.extend_from_slice(&bits.to_le_bytes()); + if ext { + // cbSize, valid bits, channel mask, KSDATAFORMAT_SUBTYPE_PCM / _IEEE_FLOAT + v.extend_from_slice(&22u16.to_le_bytes()); + v.extend_from_slice(&bits.to_le_bytes()); + let mask: u32 = if channels == 6 { 0x3F } else { 0 }; + v.extend_from_slice(&mask.to_le_bytes()); + v.extend_from_slice(&tag.to_le_bytes()); + v.extend_from_slice(&SUBTYPE_TAIL); + } else if float { + v.extend_from_slice(&0u16.to_le_bytes()); + } + if float { + v.extend_from_slice(b"fact"); + v.extend_from_slice(&4u32.to_le_bytes()); + v.extend_from_slice(&(if rf64 { u32::MAX } else { u32::try_from(frames).unwrap_or(u32::MAX) }).to_le_bytes()); + } + v.extend_from_slice(b"data"); + v.extend_from_slice(&(if rf64 { u32::MAX } else { data as u32 }).to_le_bytes()); + Ok(v) + } + PcmContainer::Aiff => { + let aiff_too_long = || "AIFF holds at most 4 GB of audio: export WAV, which has no such limit".to_string(); + let frames32 = u32::try_from(frames).map_err(|_| aiff_too_long())?; + // SSND: offset + block size + samples + let ssnd = u32::try_from(data.checked_add(8).ok_or_else(aiff_too_long)?).map_err(|_| aiff_too_long())?; + // floats need AIFF-C: a version chunk, and the compression type and name in COMM + let comm: u32 = if float { 18 + 4 + FL32_NAME.len() as u32 } else { 18 }; + let fver: u32 = if float { 8 + 4 } else { 0 }; + let form = (4u32 + fver + (8 + comm)) + .checked_add(8) + .and_then(|n| n.checked_add(ssnd)) + .and_then(|n| n.checked_add(ssnd & 1)) + .ok_or_else(aiff_too_long)?; + let mut v = Vec::with_capacity(90); + v.extend_from_slice(b"FORM"); + v.extend_from_slice(&form.to_be_bytes()); + if float { + v.extend_from_slice(b"AIFCFVER"); + v.extend_from_slice(&4u32.to_be_bytes()); + v.extend_from_slice(&AIFC_VERSION_1.to_be_bytes()); + } else { + v.extend_from_slice(b"AIFF"); + } + v.extend_from_slice(b"COMM"); + v.extend_from_slice(&comm.to_be_bytes()); + v.extend_from_slice(&channels.to_be_bytes()); + v.extend_from_slice(&frames32.to_be_bytes()); + v.extend_from_slice(&bits.to_be_bytes()); + v.extend_from_slice(&extended(sample_rate)); + if float { + v.extend_from_slice(b"fl32"); + v.extend_from_slice(FL32_NAME); + } + v.extend_from_slice(b"SSND"); + v.extend_from_slice(&ssnd.to_be_bytes()); + v.extend_from_slice(&0u32.to_be_bytes()); // offset + v.extend_from_slice(&0u32.to_be_bytes()); // block size + Ok(v) + } + } +} + +/// Append interleaved samples in `format` (little-endian for WAV, big-endian for AIFF). +pub fn encode(container: PcmContainer, interleaved: &[f32], format: SampleFormat, out: &mut Vec) { + let bps = format.bytes(); + out.reserve(interleaved.len().saturating_mul(bps)); + for &s in interleaved { + let q = if format.is_float() { SampleFormat::float(s).to_bits() as i32 } else { format.quantise(s) }; + match container { + PcmContainer::Wav => out.extend_from_slice(q.to_le_bytes().get(..bps).unwrap_or_default()), + PcmContainer::Aiff => out.extend_from_slice(q.to_be_bytes().get(4 - bps..).unwrap_or_default()), + } + } +} + +/// A whole file in memory (short audio; exports stream with [`header`] and [`encode`]). +fn write_whole(container: PcmContainer, interleaved: &[f32], channels: u16, sample_rate: u32, format: SampleFormat) -> Result, String> { + let per_frame = usize::from(channels.max(1)); + if !interleaved.len().is_multiple_of(per_frame) { + return Err("the samples do not make whole frames".into()); + } + let frames = (interleaved.len() / per_frame) as u64; + let mut v = header(container, channels, sample_rate, format, frames)?; + encode(container, interleaved, format, &mut v); + v.extend_from_slice(pad(frames, channels, format)); + Ok(v) +} + +/// A RIFF/WAVE file of interleaved samples. More than two channels are written as +/// `WAVE_FORMAT_EXTENSIBLE` with the speaker mask of the layout (6 channels: L, R, C, LFE, Ls, Rs +/// = `0x3F`). +pub fn write_wav(interleaved: &[f32], channels: u16, sample_rate: u32, format: SampleFormat) -> Result, String> { + write_whole(PcmContainer::Wav, interleaved, channels, sample_rate, format) +} + +/// IEEE 754 80-bit extended encoding of a sample rate (AIFF `COMM`). +pub fn extended(rate: u32) -> [u8; 10] { + let mut out = [0u8; 10]; + if rate == 0 { + return out; + } + let shift = rate.leading_zeros(); + let mant = (rate as u64) << (32 + shift); + let exp = 16383 + 31 - shift as u16; + out[..2].copy_from_slice(&exp.to_be_bytes()); + out[2..].copy_from_slice(&mant.to_be_bytes()); + out +} + +/// An AIFF file (`FORM`/`AIFF`, `COMM` + `SSND`; AIFF-C for floats) of interleaved samples. +pub fn write_aiff(interleaved: &[f32], channels: u16, sample_rate: u32, format: SampleFormat) -> Result, String> { + write_whole(PcmContainer::Aiff, interleaved, channels, sample_rate, format) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn streaming_formats_preserve_samples_and_bounds() { + let samples = [1.0, -1.0, f32::NAN]; + let wav = write_wav(&samples, 1, 48_000, SampleFormat::Int24).unwrap(); + assert_eq!(&wav[..4], b"RIFF"); + assert_eq!(&wav[44..53], &[255, 255, 127, 1, 0, 128, 0, 0, 0]); + let h = header(PcmContainer::Wav, 6, 96_000, SampleFormat::Int24, 96_000 * 3600).unwrap(); + assert_eq!(&h[..4], b"RF64"); + assert!(header(PcmContainer::Aiff, 2, 48_000, SampleFormat::Int24, u64::MAX).is_err()); + let aiff = write_aiff(&[0.25, -3.0], 1, 44_100, SampleFormat::Float32).unwrap(); + assert_eq!(&aiff[8..12], b"AIFC"); + assert_eq!(&aiff[aiff.len() - 8..], [0.25f32.to_be_bytes(), (-3.0f32).to_be_bytes()].concat()); + } + + #[test] + fn zero_channels_rates_and_partial_frames_are_refused() { + for container in [PcmContainer::Wav, PcmContainer::Aiff] { + assert!(header(container, 0, 48_000, SampleFormat::Int16, 1).is_err()); + assert!(header(container, 1, 0, SampleFormat::Int16, 1).is_err()); + assert!(header(container, 2, 48_000, SampleFormat::Int16, 0).is_ok(), "zero frames stay valid"); + } + assert!(write_wav(&[0.5], 0, 48_000, SampleFormat::Int16).is_err()); + assert!(write_wav(&[0.1, 0.2, 0.3], 2, 48_000, SampleFormat::Int16).is_err()); + assert!(write_aiff(&[0.1, 0.2, 0.3], 2, 48_000, SampleFormat::Int16).is_err()); + assert!(write_aiff(&[0.1, 0.2], 2, 48_000, SampleFormat::Int16).is_ok()); + } +} diff --git a/crates/video/src/lib.rs b/crates/video/src/lib.rs index 8202a93f8..17ca960bb 100644 --- a/crates/video/src/lib.rs +++ b/crates/video/src/lib.rs @@ -59,3 +59,6 @@ pub mod vp9; #[cfg(any(feature = "av1-decode", feature = "av1-encode"))] pub mod av1; + +/// Uncompressed AVI and QuickTime pixel layouts. +pub mod raw_video; diff --git a/crates/video/src/raw_video.rs b/crates/video/src/raw_video.rs new file mode 100644 index 000000000..413a09158 --- /dev/null +++ b/crates/video/src/raw_video.rs @@ -0,0 +1,302 @@ +//! Packed and planar uncompressed AVI and QuickTime sample decoding. + +/// A malformed uncompressed frame or an unsupported image size. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Error { + UnsupportedDimensions, + Truncated, +} +/// Decoded top-down RGBA pixels or planar YUV samples. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum RawPixels { + Rgba(Vec), + Yuv420([Vec; 3]), + Yuv422([Vec; 3]), +} + +/// Uncompressed video layouts (AVI `BI_RGB` and YUV FourCCs). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum RawFormat { + /// 24-bit B, G, R; rows padded to 4 bytes. + Bgr24, + /// 32-bit B, G, R, X. + Bgrx32, + /// Packed 4:2:2: Y0 U Y1 V (`YUY2`, `YUYV`). + Yuy2, + /// Packed 4:2:2: U Y0 V Y1 (`UYVY`, `2vuy`). + Uyvy, + /// Planar 4:2:0: Y, U, V (`I420`, `IYUV`). + I420, + /// Planar 4:2:0: Y, V, U (`YV12`). + Yv12, + /// Y plane, interleaved U V at half resolution (`NV12`). + Nv12, + /// Luma only (`Y800`, `GREY`, `Y8 `). + Gray8, + /// 24-bit R, G, B; rows padded to an even byte count (QuickTime `raw ` at depth 24). The + /// last row's pad byte may be missing. + Rgb24, + /// 10-bit 4:2:2 Y'CbCr, six pixels in four little-endian words, rows padded to 128 bytes + /// (QuickTime `v210`); shown at 8 bits. + V210, +} + +impl RawFormat { + /// The layout of an AVI video stream: `BI_RGB` with its bit count, or a YUV FourCC. + /// `BI_BITFIELDS` needs its colour masks, so it goes through [`Self::from_avi_bitfields`]. + pub fn from_avi(compression: [u8; 4], bit_count: u16) -> Option { + if u32::from_le_bytes(compression) == 0 { + return match bit_count { + 24 => Some(RawFormat::Bgr24), + 32 => Some(RawFormat::Bgrx32), + _ => None, + }; + } + let mut upper = compression; + upper.make_ascii_uppercase(); + Some(match &upper { + b"YUY2" | b"YUYV" | b"YUNV" | b"V422" => RawFormat::Yuy2, + b"UYVY" | b"2VUY" | b"UYNV" | b"HDYC" => RawFormat::Uyvy, + b"I420" | b"IYUV" => RawFormat::I420, + b"YV12" => RawFormat::Yv12, + b"NV12" => RawFormat::Nv12, + b"Y800" | b"GREY" | b"GRAY" | b"Y8 " => RawFormat::Gray8, + _ => return None, + }) + } + + /// The layout of an AVI `BI_BITFIELDS` stream: only 32-bit with the masks that make it B, G, R, X + /// (R `0x00ff_0000`, G `0x0000_ff00`, B `0x0000_00ff`). Other masks are a different byte order. + pub fn from_avi_bitfields(bit_count: u16, [r, g, b]: [u32; 3]) -> Option { + (bit_count == 32 && (r, g, b) == (0x00ff_0000, 0x0000_ff00, 0x0000_00ff)).then_some(RawFormat::Bgrx32) + } + + /// The layout of a QuickTime / MP4 video sample entry (`raw ` RGB at depth 24, `2vuy`, `v210`). + pub fn from_mov(fourcc: [u8; 4], depth: u16) -> Option { + match (&fourcc, depth) { + (b"raw ", 24) => Some(RawFormat::Rgb24), + (b"2vuy", _) => Some(RawFormat::Uyvy), + (b"v210", _) => Some(RawFormat::V210), + _ => None, + } + } + + pub fn label(self) -> &'static str { + match self { + RawFormat::Bgr24 | RawFormat::Rgb24 => "Uncompressed RGB 24-bit", + RawFormat::Bgrx32 => "Uncompressed RGB 32-bit", + RawFormat::Yuy2 | RawFormat::Uyvy => "Uncompressed YUV 4:2:2", + RawFormat::I420 | RawFormat::Yv12 | RawFormat::Nv12 => "Uncompressed YUV 4:2:0", + RawFormat::Gray8 => "Uncompressed greyscale", + RawFormat::V210 => "Uncompressed YUV 4:2:2 10-bit", + } + } +} + +pub fn decode(format: RawFormat, width: u32, height: u32, bottom_up: bool, s: &[u8]) -> Result { + if width == 0 || height == 0 || width > 16_384 || height > 16_384 { + return Err(Error::UnsupportedDimensions); + } + let (w, h) = (width as usize, height as usize); + let short = || Error::Truncated; + let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); + Ok(match format { + RawFormat::Bgr24 | RawFormat::Bgrx32 => { + let bpp = if format == RawFormat::Bgr24 { 3 } else { 4 }; + let stride = (w * bpp).div_ceil(4) * 4; + if s.len() < stride * h { + return Err(short()); + } + let mut rgba = Vec::with_capacity(w * h * 4); + for row in 0..h { + let src = if bottom_up { h - 1 - row } else { row }; + for px in s.get(src * stride..src * stride + w * bpp).unwrap_or_default().chunks_exact(bpp) { + rgba.extend_from_slice(&[px[2], px[1], px[0], 255]); + } + } + RawPixels::Rgba(rgba) + } + RawFormat::Rgb24 => { + // QuickTime starts each scanline on an even byte, so odd widths carry one pad byte + let (row_len, stride) = (w * 3, (w * 3).next_multiple_of(2)); + if h > 0 && s.len() < stride * (h - 1) + row_len { + return Err(short()); + } + let mut rgba = Vec::with_capacity(w * h * 4); + for r in 0..h { + let row = s.get(r * stride..r * stride + row_len).ok_or_else(short)?; + for px in row.as_chunks::<3>().0 { + rgba.extend_from_slice(&[px[0], px[1], px[2], 255]); + } + } + RawPixels::Rgba(rgba) + } + RawFormat::V210 => { + // 6 pixels per 16 bytes; each row padded to a multiple of 128 bytes (48 pixels) + let stride = w.div_ceil(48) * 128; + if s.len() < stride * h { + return Err(short()); + } + let (mut y, mut u, mut v) = (Vec::with_capacity(w * h), Vec::with_capacity(cw * h), Vec::with_capacity(cw * h)); + for row in s.chunks_exact(stride).take(h) { + // the row's 10-bit samples in order: Cb Y Cr Y Cb Y Cr Y …, kept at 8 bits + let mut comps = row.as_chunks::<4>().0.iter().flat_map(|b| { + let word = u32::from_le_bytes(*b); + [word & 0x3ff, (word >> 10) & 0x3ff, (word >> 20) & 0x3ff].map(|c| (c >> 2) as u8) + }); + for x in 0..cw { + let (Some(cb), Some(y0), Some(cr), Some(y1)) = (comps.next(), comps.next(), comps.next(), comps.next()) else { break }; + u.push(cb); + v.push(cr); + y.push(y0); + if 2 * x + 1 < w { + y.push(y1); + } + } + } + RawPixels::Yuv422([y, u, v]) + } + RawFormat::Yuy2 | RawFormat::Uyvy => { + let stride = cw * 4; + if s.len() < stride * h { + return Err(short()); + } + let (yo, uo, vo, y2) = if format == RawFormat::Yuy2 { (0, 1, 3, 2) } else { (1, 0, 2, 3) }; + let (mut y, mut u, mut v) = (Vec::with_capacity(w * h), Vec::with_capacity(cw * h), Vec::with_capacity(cw * h)); + for row in s.chunks_exact(stride).take(h) { + for (k, q) in row.as_chunks::<4>().0.iter().enumerate() { + y.push(q[yo]); + if 2 * k + 1 < w { + y.push(q[y2]); + } + u.push(q[uo]); + v.push(q[vo]); + } + } + RawPixels::Yuv422([y, u, v]) + } + RawFormat::I420 | RawFormat::Yv12 | RawFormat::Nv12 | RawFormat::Gray8 => { + let luma = w * h; + let need = if format == RawFormat::Gray8 { luma } else { luma + 2 * cw * ch }; + if s.len() < need { + return Err(short()); + } + let y = s.get(..luma).unwrap_or_default().to_vec(); + let (u, v) = match format { + RawFormat::Gray8 => (vec![128; cw * ch], vec![128; cw * ch]), + RawFormat::Nv12 => s.get(luma..luma + 2 * cw * ch).unwrap_or_default().as_chunks::<2>().0.iter().map(|p| (p[0], p[1])).unzip(), + _ => { + let a = s.get(luma..luma + cw * ch).unwrap_or_default().to_vec(); + let b = s.get(luma + cw * ch..luma + 2 * cw * ch).unwrap_or_default().to_vec(); + if format == RawFormat::Yv12 { (b, a) } else { (a, b) } + } + }; + RawPixels::Yuv420([y, u, v]) + } + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn avi_and_mov_layouts_and_bottom_up_rows() -> Result<(), Error> { + assert_eq!(RawFormat::from_avi(*b"yuyv", 16), Some(RawFormat::Yuy2)); + assert_eq!(RawFormat::from_avi([0; 4], 24), Some(RawFormat::Bgr24)); + assert_eq!(RawFormat::from_mov(*b"v210", 0), Some(RawFormat::V210)); + let pixels = decode(RawFormat::Bgr24, 1, 2, true, &[1, 2, 3, 0, 4, 5, 6, 0])?; + assert_eq!(pixels, RawPixels::Rgba(vec![6, 5, 4, 255, 3, 2, 1, 255])); + Ok(()) + } + + #[test] + fn odd_planar_and_packed_frames_preserve_chroma() -> Result<(), Error> { + let yuy = decode(RawFormat::Yuy2, 3, 1, false, &[10, 20, 11, 30, 12, 21, 99, 31])?; + assert_eq!(yuy, RawPixels::Yuv422([vec![10, 11, 12], vec![20, 21], vec![30, 31]])); + let nv = decode(RawFormat::Nv12, 3, 1, false, &[1, 2, 3, 10, 20, 11, 21])?; + assert_eq!(nv, RawPixels::Yuv420([vec![1, 2, 3], vec![10, 11], vec![20, 21]])); + Ok(()) + } + + #[test] + fn truncated_frames_and_hostile_dimensions_are_rejected() { + for format in [ + RawFormat::Bgr24, + RawFormat::Bgrx32, + RawFormat::Rgb24, + RawFormat::V210, + RawFormat::Yuy2, + RawFormat::Uyvy, + RawFormat::I420, + RawFormat::Yv12, + RawFormat::Nv12, + RawFormat::Gray8, + ] { + assert_eq!(decode(format, 3, 2, false, &[]), Err(Error::Truncated)); + } + assert_eq!(decode(RawFormat::Gray8, u32::MAX, 1, false, &[]), Err(Error::UnsupportedDimensions)); + } + + #[test] + fn every_fourcc_and_label_is_mapped() { + for (cc, want) in [ + (*b"UYVY", RawFormat::Uyvy), + (*b"hdyc", RawFormat::Uyvy), + (*b"I420", RawFormat::I420), + (*b"iyuv", RawFormat::I420), + (*b"YV12", RawFormat::Yv12), + (*b"nv12", RawFormat::Nv12), + (*b"Y800", RawFormat::Gray8), + (*b"grey", RawFormat::Gray8), + (*b"Y8 ", RawFormat::Gray8), + ] { + assert_eq!(RawFormat::from_avi(cc, 0), Some(want)); + } + assert_eq!(RawFormat::from_avi(3u32.to_le_bytes(), 32), None); + assert_eq!(RawFormat::from_avi_bitfields(32, [0x00ff_0000, 0x0000_ff00, 0x0000_00ff]), Some(RawFormat::Bgrx32)); + assert_eq!(RawFormat::from_avi_bitfields(32, [0x0000_00ff, 0x0000_ff00, 0x00ff_0000]), None); + assert_eq!(RawFormat::from_avi_bitfields(24, [0x00ff_0000, 0x0000_ff00, 0x0000_00ff]), None); + assert_eq!(RawFormat::from_avi([0; 4], 16), None); + assert_eq!(RawFormat::from_avi(*b"H264", 0), None); + assert_eq!(RawFormat::from_mov(*b"raw ", 24), Some(RawFormat::Rgb24)); + assert_eq!(RawFormat::from_mov(*b"raw ", 32), None); + assert_eq!(RawFormat::from_mov(*b"2vuy", 0), Some(RawFormat::Uyvy)); + let labels = [ + (RawFormat::Bgr24, "Uncompressed RGB 24-bit"), + (RawFormat::Rgb24, "Uncompressed RGB 24-bit"), + (RawFormat::Bgrx32, "Uncompressed RGB 32-bit"), + (RawFormat::Yuy2, "Uncompressed YUV 4:2:2"), + (RawFormat::Nv12, "Uncompressed YUV 4:2:0"), + (RawFormat::Gray8, "Uncompressed greyscale"), + (RawFormat::V210, "Uncompressed YUV 4:2:2 10-bit"), + ]; + for (format, label) in labels { + assert_eq!(format.label(), label); + } + } + + #[test] + fn rgb_v210_and_planar_layouts_decode() -> Result<(), Error> { + assert_eq!(decode(RawFormat::Rgb24, 1, 1, false, &[1, 2, 3])?, RawPixels::Rgba(vec![1, 2, 3, 255])); + // odd width: each row carries one pad byte + assert_eq!(decode(RawFormat::Rgb24, 1, 2, false, &[1, 2, 3, 0, 4, 5, 6, 0])?, RawPixels::Rgba(vec![1, 2, 3, 255, 4, 5, 6, 255])); + // the last row's pad byte may be missing, but earlier rows are still padded + assert_eq!(decode(RawFormat::Rgb24, 1, 2, false, &[1, 2, 3, 0, 4, 5, 6])?, RawPixels::Rgba(vec![1, 2, 3, 255, 4, 5, 6, 255])); + assert!(decode(RawFormat::Rgb24, 1, 2, false, &[1, 2, 3, 4, 5, 6]).is_err(), "unpadded rows are too short"); + assert_eq!(decode(RawFormat::Rgb24, 2, 1, false, &[1, 2, 3, 4, 5, 6])?, RawPixels::Rgba(vec![1, 2, 3, 255, 4, 5, 6, 255])); + assert_eq!(decode(RawFormat::Bgrx32, 1, 1, false, &[1, 2, 3, 9])?, RawPixels::Rgba(vec![3, 2, 1, 255])); + // v210: Cb, Y0, Cr in the first word, Y1 in the low bits of the second; rows are 128 bytes. + let mut row = vec![0u8; 128]; + row[..4].copy_from_slice(&(0x100u32 | (0x200 << 10) | (0x300 << 20)).to_le_bytes()); + row[4..8].copy_from_slice(&0x0FCu32.to_le_bytes()); + assert_eq!(decode(RawFormat::V210, 2, 1, false, &row)?, RawPixels::Yuv422([vec![128, 63], vec![64], vec![192]])); + assert_eq!(decode(RawFormat::V210, 1, 1, false, &row)?, RawPixels::Yuv422([vec![128], vec![64], vec![192]])); + let planar = [1, 2, 3, 4, 10, 20]; + assert_eq!(decode(RawFormat::I420, 2, 2, false, &planar)?, RawPixels::Yuv420([vec![1, 2, 3, 4], vec![10], vec![20]])); + assert_eq!(decode(RawFormat::Yv12, 2, 2, false, &planar)?, RawPixels::Yuv420([vec![1, 2, 3, 4], vec![20], vec![10]])); + assert_eq!(decode(RawFormat::Gray8, 2, 2, false, &planar[..4])?, RawPixels::Yuv420([vec![1, 2, 3, 4], vec![128], vec![128]])); + assert_eq!(decode(RawFormat::Uyvy, 2, 1, false, &[20, 10, 30, 11])?, RawPixels::Yuv422([vec![10, 11], vec![20], vec![30]])); + Ok(()) + } +}