use std::{
collections::VecDeque,
io::Cursor,
sync::{Arc, Condvar, Mutex, MutexGuard},
time::Duration,
};
use image::{AnimationDecoder, ImageFormat, RgbaImage, metadata::LoopCount};
pub const MAX_FRAMES: usize = 600;
pub const MAX_CACHED_BYTES: usize = 48 << 20;
pub const MAX_STREAMING_BYTES: usize = 128 << 20;
pub const MAX_STREAMS: usize = 4;
const MAX_DIMENSION: u32 = 8192;
const AHEAD: usize = 2;
const IDLE: Duration = Duration::from_secs(2);
pub fn format(bytes: &[u8]) -> Option<ImageFormat> {
if bytes.starts_with(b"GIF8") {
Some(ImageFormat::Gif)
} else if bytes.len() >= 12 && bytes.starts_with(b"RIFF") && &bytes[8..12] == b"WEBP" {
Some(ImageFormat::WebP)
} else {
None
}
}
pub fn frame_delay(delay: image::Delay) -> u32 {
let (numer, denom) = delay.numer_denom_ms();
match numer / denom.max(1) {
ms if ms >= 20 => ms,
_ => 100,
}
}
pub fn scaled(size: (u32, u32), longest: u32) -> (u32, u32) {
let (w, h) = size;
let side = w.max(h);
let limit = match longest {
0 => MAX_DIMENSION,
l => l.min(MAX_DIMENSION),
};
if side <= limit {
return size;
}
let f = limit as f64 / side as f64;
(
((w as f64 * f).round() as u32).max(1),
((h as f64 * f).round() as u32).max(1),
)
}
pub fn shrunk(rgba: RgbaImage, longest: u32) -> RgbaImage {
let size = rgba.dimensions();
match scaled(size, longest) {
s if s == size => rgba,
(w, h) => image::imageops::thumbnail(&rgba, w, h),
}
}
pub fn working_bytes(natural: (u32, u32), longest: u32) -> Option<usize> {
let (w, h) = natural;
let (tw, th) = scaled(natural, longest);
let full = (w as usize).checked_mul(h as usize)?.checked_mul(12)?;
let held = (tw as usize)
.checked_mul(th as usize)?
.checked_mul(4 * (AHEAD + 2))?;
let bytes = full.checked_add(held)?;
(bytes <= MAX_STREAMING_BYTES).then_some(bytes)
}
pub struct Decoded<F> {
pub first: F,
pub natural: (u32, u32),
pub animation: Option<Animation<F>>,
}
pub struct Animation<F> {
pub delays: Vec<u32>,
pub plays: u32,
frames: Store<F>,
}
enum Store<F> {
Cached(Vec<F>),
Streamed(Stream<F>),
}
impl<F: Clone + Send + 'static> Animation<F> {
pub fn len(&self) -> usize {
self.delays.len()
}
pub fn is_empty(&self) -> bool {
self.delays.is_empty()
}
pub fn frame(&self, index: usize) -> F {
match &self.frames {
Store::Cached(frames) => frames[index.min(frames.len() - 1)].clone(),
Store::Streamed(stream) => stream.frame(index % self.delays.len()),
}
}
pub fn cached(&self) -> &[F] {
match &self.frames {
Store::Cached(frames) => frames,
Store::Streamed(_) => &[],
}
}
pub fn streams(&self) -> bool {
matches!(self.frames, Store::Streamed(_))
}
pub fn held_frames(&self) -> usize {
match &self.frames {
Store::Cached(frames) => frames.len(),
Store::Streamed(_) => AHEAD + 2,
}
}
pub fn other_bytes(&self) -> usize {
match &self.frames {
Store::Cached(_) => 0,
Store::Streamed(stream) => stream.shared.source.bytes.len() + stream.shared.working,
}
}
}
pub fn decode<F: Clone + Send + 'static>(
bytes: &[u8],
format: ImageFormat,
longest: u32,
stream: bool,
convert: fn(RgbaImage) -> F,
) -> Result<Option<Decoded<F>>, String> {
let Some((frames, plays)) = open(bytes, format)? else {
return Ok(None);
};
let (mut kept, mut delays, mut total) = (Vec::new(), Vec::new(), 0usize);
let mut natural = None;
let mut over = false;
for frame in frames.take(MAX_FRAMES) {
let frame = match frame {
Ok(frame) => frame,
Err(e) if !delays.is_empty() => {
log::debug!("image: animation cut short: {e}");
break;
}
Err(e) => return Err(e.to_string()),
};
delays.push(frame_delay(frame.delay()));
let buffer = frame.into_buffer();
natural.get_or_insert(buffer.dimensions());
let (w, h) = scaled(buffer.dimensions(), longest);
total += w as usize * h as usize * 4;
if total > MAX_CACHED_BYTES && !over {
over = true;
kept.truncate(1);
}
if !over || kept.is_empty() {
kept.push(convert(shrunk(buffer, longest)));
}
}
let natural = natural.ok_or("no frames")?;
let first = kept[0].clone();
if delays.len() < 2 {
return Ok(Some(Decoded {
first,
natural,
animation: None,
}));
}
let frames = if !over {
Store::Cached(kept)
} else if let Some(working) = working_bytes(natural, longest).filter(|_| stream) {
Store::Streamed(Stream::new(
Source {
bytes: Arc::from(bytes),
format,
longest,
len: delays.len(),
convert,
},
working,
first.clone(),
))
} else {
let side = scaled(natural, longest);
let side = side.0.max(side.1) as f64;
let fit = (side * (MAX_CACHED_BYTES as f64 / total as f64).sqrt()).floor() as u32;
log::debug!("image: animation over {MAX_CACHED_BYTES} bytes: decoding at {fit}");
return decode(bytes, format, fit.max(1), false, convert);
};
Ok(Some(Decoded {
first,
natural,
animation: Some(Animation {
delays,
plays,
frames,
}),
}))
}
fn open(bytes: &[u8], format: ImageFormat) -> Result<Option<(image::Frames<'_>, u32)>, String> {
let cursor = Cursor::new(bytes);
let error = |e: image::ImageError| e.to_string();
let (frames, loops) = match format {
ImageFormat::Gif => {
let decoder = image::codecs::gif::GifDecoder::new(cursor).map_err(error)?;
let loops = decoder.loop_count();
(decoder.into_frames(), loops)
}
ImageFormat::WebP => {
let decoder = image::codecs::webp::WebPDecoder::new(cursor).map_err(error)?;
if !decoder.has_animation() {
return Ok(None);
}
let loops = decoder.loop_count();
(decoder.into_frames(), loops)
}
_ => return Err("not an animation format".into()),
};
let plays = match loops {
LoopCount::Infinite => 0,
LoopCount::Finite(n) if format == ImageFormat::Gif => n.get().saturating_add(1),
LoopCount::Finite(n) => n.get(),
};
Ok(Some((frames, plays)))
}
struct Source<F> {
bytes: Arc<[u8]>,
format: ImageFormat,
longest: u32,
len: usize,
convert: fn(RgbaImage) -> F,
}
struct Stream<F> {
shared: Arc<Shared<F>>,
}
struct Shared<F> {
source: Source<F>,
working: usize,
state: Mutex<State<F>>,
wake: Condvar,
}
struct State<F> {
wanted: usize,
ready: VecDeque<(usize, F)>,
shown: (usize, F),
running: bool,
asked: bool,
failed: bool,
dropped: bool,
}
fn ahead(from: usize, to: usize, len: usize) -> usize {
(to + len - from) % len
}
impl<F> State<F> {
fn next(&self, len: usize) -> usize {
match self.ready.back() {
Some(&(last, _)) => (last + 1) % len,
None if self.shown.0 == self.wanted => (self.wanted + 1) % len,
None => self.wanted,
}
}
}
impl<F: Clone + Send + 'static> Stream<F> {
fn new(source: Source<F>, working: usize, first: F) -> Stream<F> {
Stream {
shared: Arc::new(Shared {
source,
working,
state: Mutex::new(State {
wanted: 0,
ready: VecDeque::new(),
shown: (0, first),
running: false,
asked: false,
failed: false,
dropped: false,
}),
wake: Condvar::new(),
}),
}
}
fn frame(&self, index: usize) -> F {
let shared = &self.shared;
let len = shared.source.len;
let mut s = shared.lock();
s.wanted = index;
s.asked = true;
while let Some(&(at, _)) = s.ready.front() {
if at == index {
s.shown = s.ready.pop_front().expect("front");
break;
}
if ahead(index, at, len) <= AHEAD {
break;
}
s.ready.pop_front();
}
if !s.running && !s.failed && reserve(shared.working) {
s.running = true;
let worker = shared.clone();
let spawned = std::thread::Builder::new()
.name("nsh-animation".into())
.spawn(move || run(Running(worker)));
if spawned.is_err() {
s.running = false;
release(shared.working);
}
}
shared.wake.notify_one();
s.shown.1.clone()
}
}
impl<F> Drop for Stream<F> {
fn drop(&mut self) {
self.shared.lock().dropped = true;
self.shared.wake.notify_one();
}
}
impl<F> Shared<F> {
fn lock(&self) -> MutexGuard<'_, State<F>> {
self.state.lock().unwrap_or_else(|e| e.into_inner())
}
}
struct Running<F>(Arc<Shared<F>>);
impl<F> Drop for Running<F> {
fn drop(&mut self) {
self.0.lock().running = false;
release(self.0.working);
}
}
fn run<F: Clone + Send + 'static>(running: Running<F>) {
let shared = &running.0;
let source = &shared.source;
let mut decoder: Option<(image::Frames<'static>, usize)> = None;
loop {
let next = {
let mut s = shared.lock();
loop {
if s.dropped {
return;
}
let next = s.next(source.len);
if ahead(s.wanted, next, source.len) <= AHEAD
&& s.ready.len() <= AHEAD
&& s.ready.iter().all(|&(at, _)| at != next)
&& s.shown.0 != next
{
break next;
}
s.asked = false;
let (guard, wait) = shared
.wake
.wait_timeout(s, IDLE)
.unwrap_or_else(|e| e.into_inner());
s = guard;
if wait.timed_out() && !s.asked {
return;
}
}
};
let frame = match read(&mut decoder, source, next) {
Ok(frame) => frame,
Err(e) => {
log::debug!("image: animation stream ended: {e}");
shared.lock().failed = true;
return;
}
};
let mut s = shared.lock();
if s.next(source.len) == next && ahead(s.wanted, next, source.len) <= AHEAD {
s.ready.push_back((next, frame));
}
}
}
fn read<F>(
decoder: &mut Option<(image::Frames<'static>, usize)>,
source: &Source<F>,
index: usize,
) -> Result<F, String> {
if decoder.as_ref().is_none_or(|&(_, at)| at > index) {
let bytes = OwnedBytes(source.bytes.clone());
let frames = match source.format {
ImageFormat::Gif => image::codecs::gif::GifDecoder::new(Cursor::new(bytes))
.map_err(|e| e.to_string())?
.into_frames(),
_ => image::codecs::webp::WebPDecoder::new(Cursor::new(bytes))
.map_err(|e| e.to_string())?
.into_frames(),
};
*decoder = Some((frames, 0));
}
let (frames, at) = decoder.as_mut().expect("opened");
loop {
let frame = frames
.next()
.ok_or("the animation ended early")?
.map_err(|e| e.to_string())?;
*at += 1;
if *at > index {
return Ok((source.convert)(shrunk(
frame.into_buffer(),
source.longest,
)));
}
}
}
struct OwnedBytes(Arc<[u8]>);
impl AsRef<[u8]> for OwnedBytes {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
static USAGE: Mutex<(usize, usize)> = Mutex::new((0, 0));
fn reserve(bytes: usize) -> bool {
let mut usage = USAGE.lock().unwrap_or_else(|e| e.into_inner());
if usage.1 >= MAX_STREAMS || usage.0 + bytes > MAX_STREAMING_BYTES {
return false;
}
usage.0 += bytes;
usage.1 += 1;
true
}
fn release(bytes: usize) {
let mut usage = USAGE.lock().unwrap_or_else(|e| e.into_inner());
usage.0 -= bytes;
usage.1 -= 1;
}
#[cfg(test)]
mod tests {
use std::time::Instant;
use super::*;
fn gif(side: u32, count: u8) -> Vec<u8> {
let mut bytes = Vec::new();
{
let mut encoder = image::codecs::gif::GifEncoder::new(&mut bytes);
for i in 0..count {
let pixels = RgbaImage::from_pixel(side, side, image::Rgba([i, 0, 0, 255]));
encoder
.encode_frame(image::Frame::from_parts(
pixels,
0,
0,
image::Delay::from_numer_denom_ms(100, 1),
))
.unwrap();
}
}
bytes
}
fn rgba(image: RgbaImage) -> Arc<RgbaImage> {
Arc::new(image)
}
fn frame_when_ready(animation: &Animation<Arc<RgbaImage>>, index: usize) -> Arc<RgbaImage> {
let deadline = Instant::now() + Duration::from_secs(20);
loop {
let frame = animation.frame(index);
if frame.get_pixel(0, 0)[0] == index as u8 {
return frame;
}
assert!(Instant::now() < deadline, "frame {index} never decoded");
std::thread::sleep(Duration::from_millis(5));
}
}
#[test]
fn times_frames() {
assert_eq!(frame_delay(image::Delay::from_numer_denom_ms(70, 1)), 70);
assert_eq!(frame_delay(image::Delay::from_numer_denom_ms(0, 1)), 100);
assert_eq!(frame_delay(image::Delay::from_numer_denom_ms(10, 1)), 100);
assert_eq!(frame_delay(image::Delay::from_numer_denom_ms(41, 2)), 20);
}
#[test]
fn sniffs_animated_formats() {
assert_eq!(format(b"GIF89a..."), Some(ImageFormat::Gif));
assert_eq!(format(b"RIFF\0\0\0\0WEBPVP8X"), Some(ImageFormat::WebP));
assert_eq!(format(b"\x89PNG\r\n"), None);
assert_eq!(format(b"RIFF"), None);
}
#[test]
fn small_animations_keep_their_frames() {
let decoded = decode(&gif(16, 3), ImageFormat::Gif, 0, true, rgba)
.unwrap()
.unwrap();
let animation = decoded.animation.unwrap();
assert!(!animation.streams());
assert_eq!((animation.len(), animation.held_frames()), (3, 3));
assert_eq!(animation.delays, [100, 100, 100]);
assert_eq!(animation.frame(2).get_pixel(0, 0)[0], 2);
}
#[test]
fn large_animations_stream_at_the_size_shown() {
let decoded = decode(&gif(1024, 14), ImageFormat::Gif, 1024, true, rgba)
.unwrap()
.unwrap();
assert_eq!(decoded.first.dimensions(), (1024, 1024));
let animation = decoded.animation.unwrap();
assert!(animation.streams());
assert_eq!(animation.len(), 14);
for index in (0..14).chain(0..2) {
let frame = frame_when_ready(&animation, index);
assert_eq!(frame.dimensions(), (1024, 1024));
}
}
#[test]
fn without_streaming_large_animations_decode_as_small_as_they_fit() {
let decoded = decode(&gif(1024, 14), ImageFormat::Gif, 1024, false, rgba)
.unwrap()
.unwrap();
let animation = decoded.animation.unwrap();
assert!(!animation.streams());
assert_eq!(animation.len(), 14);
let side = animation.frame(0).width();
assert!(side < 1024 && 14 * side as usize * side as usize * 4 <= MAX_CACHED_BYTES);
}
#[test]
fn streamed_frames_match_decoded_ones() {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../examples/gallery/bounce.gif");
let bytes = std::fs::read(path).unwrap();
let cached = decode(&bytes, ImageFormat::Gif, 32, true, rgba)
.unwrap()
.unwrap()
.animation
.unwrap();
let stream = Stream::new(
Source {
bytes: Arc::from(&bytes[..]),
format: ImageFormat::Gif,
longest: 32,
len: cached.len(),
convert: rgba,
},
0,
cached.frame(0),
);
let mut decoder = None;
for index in (0..cached.len()).chain([0, 2, 1]) {
let frame = read(&mut decoder, &stream.shared.source, index).unwrap();
assert_eq!(*frame, *cached.frame(index), "frame {index}");
}
}
#[test]
fn streams_share_their_working_memory() {
let mut taken = 0;
while reserve(1) {
taken += 1;
}
assert!(taken <= MAX_STREAMS);
for _ in 0..taken {
release(1);
}
assert!(working_bytes((1600, 920), 1200).is_some());
assert!(working_bytes((8192, 8192), 8192).is_none());
}
}