use std::io::Cursor;
use alpha2_media::{
CancelToken, ChromaLocation, DecodeError, DecodeEvent, DecodeRequest, DecodeResult,
DecodedVideoFrame, Decoder, Orientation, PixelFormat, Plane, Rect, SampleAspectRatio,
StreamInfo, StreamKind, frame_size_supported,
};
use alpha2_time::MediaTime;
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum StillFormat {
Png,
Jpeg,
WebP,
}
impl StillFormat {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Png => "PNG",
Self::Jpeg => "JPEG",
Self::WebP => "WebP",
}
}
}
#[must_use]
pub fn still_format(head: &[u8]) -> Option<StillFormat> {
if head.starts_with(&[0xff, 0xd8, 0xff]) {
Some(StillFormat::Jpeg)
} else if head.starts_with(b"\x89PNG\r\n\x1a\n") {
Some(StillFormat::Png)
} else if head.len() >= 12 && head.starts_with(b"RIFF") && &head[8..12] == b"WEBP" {
Some(StillFormat::WebP)
} else {
None
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct StillInfo {
pub format: StillFormat,
pub width: u32,
pub height: u32,
pub orientation: Orientation,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DecodedStill {
pub info: StillInfo,
pub rgba: Vec<u8>,
}
impl DecodedStill {
#[must_use]
pub fn into_video_frame(self) -> DecodedVideoFrame {
let StillInfo {
width,
height,
orientation,
..
} = self.info;
DecodedVideoFrame {
pts: MediaTime::ZERO,
dts: None,
duration: None,
visible_rect: Rect {
x: 0,
y: 0,
width,
height,
},
sample_aspect_ratio: SampleAspectRatio::default(),
orientation,
pixel_format: PixelFormat::Rgba8,
chroma_location: ChromaLocation::Unspecified,
color: None,
planes: vec![Plane {
bytes: self.rgba.into(),
stride: width * 4,
width,
height,
}],
}
}
}
pub fn probe_still(bytes: &[u8]) -> Result<StillInfo, DecodeError> {
let format = still_format(bytes).ok_or_else(not_a_still)?;
let (width, height) = contained(format, || match format {
StillFormat::Png => Ok(png_decoder(bytes)
.read_header_info()
.map_err(|error| png_error(&error))?
.size()),
StillFormat::Jpeg => {
let mut decoder = jpeg_decoder(bytes);
decoder.decode_headers().map_err(jpeg_error)?;
jpeg_size(&decoder)
}
StillFormat::WebP => Ok(webp_decoder(bytes)?.dimensions()),
})?;
check_size(format, width, height)?;
Ok(StillInfo {
format,
width,
height,
orientation: orientation(format, bytes),
})
}
pub fn decode_still(bytes: &[u8]) -> Result<DecodedStill, DecodeError> {
let format = still_format(bytes).ok_or_else(not_a_still)?;
let (width, height, rgba) = contained(format, || match format {
StillFormat::Png => decode_png(bytes),
StillFormat::Jpeg => decode_jpeg(bytes),
StillFormat::WebP => decode_webp(bytes),
})?;
Ok(DecodedStill {
info: StillInfo {
format,
width,
height,
orientation: orientation(format, bytes),
},
rgba,
})
}
fn contained<T>(
format: StillFormat,
decode: impl FnOnce() -> Result<T, DecodeError>,
) -> Result<T, DecodeError> {
std::panic::catch_unwind(std::panic::AssertUnwindSafe(decode)).unwrap_or_else(|payload| {
Err(DecodeError::Adapter(format!(
"{}: the decoder panicked: {}",
format.as_str(),
crate::panic_text(&*payload)
)))
})
}
fn not_a_still() -> DecodeError {
DecodeError::Unsupported("not a PNG, JPEG or WebP picture".into())
}
fn check_size(format: StillFormat, width: u32, height: u32) -> Result<(), DecodeError> {
if width == 0 || height == 0 {
return Err(DecodeError::Adapter(format!(
"{}: a {width}x{height} picture is empty",
format.as_str()
)));
}
if frame_size_supported(width, height) {
Ok(())
} else {
Err(DecodeError::Unsupported(format!(
"{}: a {width}x{height} picture is larger than {} pixels or {} on a side",
format.as_str(),
alpha2_media::MAX_FRAME_PIXELS,
alpha2_media::MAX_FRAME_DIMENSION
)))
}
}
fn rgba_buffer(width: u32, height: u32) -> Vec<u8> {
vec![0; width as usize * height as usize * 4]
}
fn expand_in_place(
buffer: &mut [u8],
pixels: usize,
source_size: usize,
to_rgba: impl Fn(&[u8]) -> [u8; 4],
) {
let mut convert = |index: usize| {
let mut source = [0; 8];
let start = index * source_size;
source[..source_size].copy_from_slice(&buffer[start..start + source_size]);
buffer[index * 4..index * 4 + 4].copy_from_slice(&to_rgba(&source[..source_size]));
};
if source_size >= 4 {
(0..pixels).for_each(&mut convert);
} else {
(0..pixels).rev().for_each(&mut convert);
}
}
fn png_decoder(bytes: &[u8]) -> png::Decoder<Cursor<&[u8]>> {
let mut decoder = png::Decoder::new(Cursor::new(bytes));
decoder.set_ignore_text_chunk(true);
decoder.set_ignore_iccp_chunk(true);
decoder
}
fn png_error(error: &png::DecodingError) -> DecodeError {
DecodeError::Adapter(format!("PNG: {error}"))
}
fn decode_png(bytes: &[u8]) -> Result<(u32, u32, Vec<u8>), DecodeError> {
let mut decoder = png_decoder(bytes);
let (width, height) = decoder
.read_header_info()
.map_err(|error| png_error(&error))?
.size();
check_size(StillFormat::Png, width, height)?;
decoder.set_transformations(png::Transformations::EXPAND);
let mut reader = decoder.read_info().map_err(|error| png_error(&error))?;
let (color, depth) = reader.output_color_type();
let channels = color.samples();
let sample_size = if depth == png::BitDepth::Sixteen {
2
} else {
1
};
let pixels = width as usize * height as usize;
let source_size = channels * sample_size;
let mut buffer = vec![0; pixels * source_size.max(4)];
let written = reader
.output_buffer_size()
.and_then(|written| buffer.get_mut(..written))
.ok_or_else(|| DecodeError::Adapter("PNG: the decoded size disagrees".into()))?;
let frame = reader
.next_frame(written)
.map_err(|error| png_error(&error))?;
if (frame.width, frame.height) != (width, height) {
return Err(DecodeError::Adapter(format!(
"PNG: a {}x{} picture in a {width}x{height} canvas",
frame.width, frame.height
)));
}
let wide = |sample: &[u8]| round_16_to_8(u16::from_be_bytes([sample[0], sample[1]]));
match (channels, sample_size) {
(4, 1) => {}
(3, 1) => expand_in_place(&mut buffer, pixels, 3, |p| [p[0], p[1], p[2], 255]),
(2, 1) => expand_in_place(&mut buffer, pixels, 2, |p| [p[0], p[0], p[0], p[1]]),
(1, 1) => expand_in_place(&mut buffer, pixels, 1, |p| [p[0], p[0], p[0], 255]),
(4, 2) => expand_in_place(&mut buffer, pixels, 8, |p| {
[wide(&p[0..]), wide(&p[2..]), wide(&p[4..]), wide(&p[6..])]
}),
(3, 2) => expand_in_place(&mut buffer, pixels, 6, |p| {
[wide(&p[0..]), wide(&p[2..]), wide(&p[4..]), 255]
}),
(2, 2) => expand_in_place(&mut buffer, pixels, 4, |p| {
let gray = wide(p);
[gray, gray, gray, wide(&p[2..])]
}),
(1, 2) => expand_in_place(&mut buffer, pixels, 2, |p| {
let gray = wide(p);
[gray, gray, gray, 255]
}),
_ => {
return Err(DecodeError::Unsupported(format!(
"PNG: {color:?} at {} bits after expansion",
depth as u8
)));
}
}
buffer.truncate(pixels * 4);
if source_size > 4 {
buffer.shrink_to_fit();
}
Ok((width, height, buffer))
}
fn round_16_to_8(value: u16) -> u8 {
u8::try_from((u32::from(value) + 128) >> 8).unwrap_or(u8::MAX)
}
fn jpeg_decoder(bytes: &[u8]) -> zune_jpeg::JpegDecoder<zune_core::bytestream::ZCursor<&[u8]>> {
let options = zune_core::options::DecoderOptions::default()
.set_max_width(usize::from(u16::MAX))
.set_max_height(usize::from(u16::MAX))
.jpeg_set_out_colorspace(zune_core::colorspace::ColorSpace::RGB);
zune_jpeg::JpegDecoder::new_with_options(zune_core::bytestream::ZCursor::new(bytes), options)
}
fn jpeg_error(error: zune_jpeg::errors::DecodeErrors) -> DecodeError {
match error {
zune_jpeg::errors::DecodeErrors::Unsupported(scheme) => {
DecodeError::Unsupported(format!("JPEG: {scheme:?}"))
}
error => DecodeError::Adapter(format!("JPEG: {error}")),
}
}
fn jpeg_size<T: zune_core::bytestream::ZByteReaderTrait>(
decoder: &zune_jpeg::JpegDecoder<T>,
) -> Result<(u32, u32), DecodeError> {
let malformed = || DecodeError::Adapter("JPEG: no frame header".into());
let (width, height) = decoder.dimensions().ok_or_else(malformed)?;
let side = |value: usize| u32::try_from(value).map_err(|_| malformed());
Ok((side(width)?, side(height)?))
}
fn decode_jpeg(bytes: &[u8]) -> Result<(u32, u32, Vec<u8>), DecodeError> {
let mut decoder = jpeg_decoder(bytes);
decoder.decode_headers().map_err(jpeg_error)?;
let (width, height) = jpeg_size(&decoder)?;
check_size(StillFormat::Jpeg, width, height)?;
let mut rgba = rgba_buffer(width, height);
let pixels = width as usize * height as usize;
decoder
.decode_into(&mut rgba[..pixels * 3])
.map_err(jpeg_error)?;
expand_in_place(&mut rgba, pixels, 3, |p| [p[0], p[1], p[2], 255]);
Ok((width, height, rgba))
}
fn webp_error(error: image_webp::DecodingError) -> DecodeError {
match error {
image_webp::DecodingError::UnsupportedFeature(feature) => {
DecodeError::Unsupported(format!("WebP: {feature}"))
}
error => DecodeError::Adapter(format!("WebP: {error}")),
}
}
fn webp_decoder(bytes: &[u8]) -> Result<image_webp::WebPDecoder<Cursor<&[u8]>>, DecodeError> {
let decoder = image_webp::WebPDecoder::new(Cursor::new(bytes)).map_err(webp_error)?;
if decoder.is_animated() {
return Err(DecodeError::Unsupported(
"WebP: an animated picture is not a still".into(),
));
}
Ok(decoder)
}
fn decode_webp(bytes: &[u8]) -> Result<(u32, u32, Vec<u8>), DecodeError> {
let mut decoder = webp_decoder(bytes)?;
let (width, height) = decoder.dimensions();
check_size(StillFormat::WebP, width, height)?;
let mut rgba = rgba_buffer(width, height);
let pixels = width as usize * height as usize;
if decoder.has_alpha() {
decoder.read_image(&mut rgba).map_err(webp_error)?;
} else {
decoder
.read_image(&mut rgba[..pixels * 3])
.map_err(webp_error)?;
expand_in_place(&mut rgba, pixels, 3, |p| [p[0], p[1], p[2], 255]);
}
Ok((width, height, rgba))
}
fn orientation(format: StillFormat, bytes: &[u8]) -> Orientation {
let exif = match format {
StillFormat::Jpeg => jpeg_exif(bytes),
StillFormat::Png => png_exif(bytes),
StillFormat::WebP => webp_exif(bytes),
};
exif.and_then(exif_orientation)
.and_then(Orientation::from_exif)
.unwrap_or(Orientation::UPRIGHT)
}
fn jpeg_exif(bytes: &[u8]) -> Option<&[u8]> {
let mut at = 2;
loop {
while *bytes.get(at)? == 0xff && bytes.get(at + 1) == Some(&0xff) {
at += 1;
}
if *bytes.get(at)? != 0xff {
return None;
}
let marker = *bytes.get(at + 1)?;
at += 2;
match marker {
0xda | 0xd9 => return None,
0x01 | 0xd0..=0xd7 => continue,
_ => {}
}
let length = usize::from(u16::from_be_bytes([*bytes.get(at)?, *bytes.get(at + 1)?]));
let segment = bytes.get(at + 2..at.checked_add(length)?)?;
if marker == 0xe1
&& let Some(tiff) = segment.strip_prefix(b"Exif\0\0")
{
return Some(tiff);
}
at += length;
}
}
fn png_exif(bytes: &[u8]) -> Option<&[u8]> {
let mut at = 8;
loop {
let length =
usize::try_from(u32::from_be_bytes(bytes.get(at..at + 4)?.try_into().ok()?)).ok()?;
let kind = bytes.get(at + 4..at + 8)?;
let data = bytes.get(at + 8..(at + 8).checked_add(length)?)?;
match kind {
b"eXIf" => return Some(data),
b"IEND" => return None,
_ => at += 12 + length,
}
}
}
fn webp_exif(bytes: &[u8]) -> Option<&[u8]> {
let mut at = 12;
loop {
let kind = bytes.get(at..at + 4)?;
let length = usize::try_from(u32::from_le_bytes(
bytes.get(at + 4..at + 8)?.try_into().ok()?,
))
.ok()?;
let data = bytes.get(at + 8..(at + 8).checked_add(length)?)?;
if kind == b"EXIF" {
return Some(data);
}
at += 8 + length + (length & 1);
}
}
fn exif_orientation(tiff: &[u8]) -> Option<u16> {
let big_endian = match tiff.get(..4)? {
b"II*\0" => false,
b"MM\0*" => true,
_ => return None,
};
let u16_at = |at: usize| {
let bytes = tiff.get(at..at.checked_add(2)?)?.try_into().ok()?;
Some(if big_endian {
u16::from_be_bytes(bytes)
} else {
u16::from_le_bytes(bytes)
})
};
let u32_at = |at: usize| {
let bytes = tiff.get(at..at.checked_add(4)?)?.try_into().ok()?;
Some(if big_endian {
u32::from_be_bytes(bytes)
} else {
u32::from_le_bytes(bytes)
})
};
let ifd = usize::try_from(u32_at(4)?).ok()?;
let entries = usize::from(u16_at(ifd)?);
(0..entries).find_map(|index| {
let entry = ifd.checked_add(2 + index * 12)?;
if u16_at(entry)? != 0x0112 || u32_at(entry + 4)? != 1 {
return None;
}
match u16_at(entry + 2)? {
3 => u16_at(entry + 8),
4 => u16::try_from(u32_at(entry + 8)?).ok(),
_ => None,
}
})
}
#[derive(Debug)]
pub struct StillStream {
frame: DecodedVideoFrame,
info: [StreamInfo; 1],
given: bool,
}
impl StillStream {
pub fn open(path: &std::path::Path) -> Result<Self, DecodeError> {
let bytes = std::fs::read(path).map_err(|error| DecodeError::Adapter(error.to_string()))?;
let still = decode_still(&bytes)?;
let info = StreamInfo {
index: 0,
kind: StreamKind::Video,
codec: still.info.format.as_str().to_lowercase(),
duration: None,
dimensions: Some((still.info.width, still.info.height)),
frame_rate: None,
sample_rate: None,
channels: None,
orientation: still.info.orientation,
sample_aspect_ratio: SampleAspectRatio::default(),
color: None,
frame_bytes: None,
alpha: None,
language: None,
title: None,
};
Ok(Self {
frame: still.into_video_frame(),
info: [info],
given: false,
})
}
}
impl Decoder for StillStream {
fn streams(&self) -> &[StreamInfo] {
&self.info
}
fn flush(&mut self) -> Result<(), DecodeError> {
self.given = false;
Ok(())
}
fn seek(
&mut self,
request: DecodeRequest,
cancel: &CancelToken,
) -> Result<DecodeResult, DecodeError> {
if cancel.is_cancelled() {
return Err(DecodeError::Cancelled);
}
self.given = true;
Ok(DecodeResult {
request,
event: DecodeEvent::Video(self.frame.clone()),
})
}
fn decode_next(&mut self, cancel: &CancelToken) -> Result<DecodeEvent, DecodeError> {
if cancel.is_cancelled() {
return Err(DecodeError::Cancelled);
}
if std::mem::replace(&mut self.given, true) {
return Ok(DecodeEvent::EndOfStream);
}
Ok(DecodeEvent::Video(self.frame.clone()))
}
}
#[cfg(test)]
#[path = "stills_tests.rs"]
mod tests;