aboutsummaryrefslogtreecommitdiff
path: root/transcoder/src
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2023-10-28 23:13:15 +0200
committermetamuffin <metamuffin@disroot.org>2023-10-28 23:13:15 +0200
commit524230fd8378b699e98012c32e64773b2efba53f (patch)
tree15d8abdbb5bce93432c60225683818c1078460a2 /transcoder/src
parent421d2843a64ba3f94e773b4ea50238bbf94c742e (diff)
downloadjellything-524230fd8378b699e98012c32e64773b2efba53f.tar
jellything-524230fd8378b699e98012c32e64773b2efba53f.tar.bz2
jellything-524230fd8378b699e98012c32e64773b2efba53f.tar.zst
"avif decoding"
Diffstat (limited to 'transcoder/src')
-rw-r--r--transcoder/src/image.rs39
1 files changed, 30 insertions, 9 deletions
diff --git a/transcoder/src/image.rs b/transcoder/src/image.rs
index 3cd9986..7f52ff3 100644
--- a/transcoder/src/image.rs
+++ b/transcoder/src/image.rs
@@ -1,11 +1,14 @@
use crate::LOCAL_TRANSCODING_TASKS;
use anyhow::Context;
-use image::{imageops::FilterType, ImageFormat};
+use image::imageops::FilterType;
use jellybase::{cache::async_cache_file, AssetLocationExt};
use jellycommon::AssetLocation;
use log::{debug, info};
use rgb::FromSlice;
-use std::{fs::File, io::BufReader};
+use std::{
+ fs::File,
+ io::{BufReader, Read, Seek, SeekFrom},
+};
use tokio::io::AsyncWriteExt;
pub async fn transcode(
@@ -27,13 +30,31 @@ pub async fn transcode(
info!("encoding {asset:?} (speed={speed}, quality={quality}, width={width})");
let encoded = tokio::task::spawn_blocking(move || {
let original_path = asset.path();
- // TODO shouldn't be neccessary with guessed format.
- let file = BufReader::new(File::open(&original_path).context("opening source")?);
- let mut reader = image::io::Reader::new(file);
- reader.set_format(ImageFormat::Avif);
- let reader = reader.with_guessed_format().context("guessing format")?;
- debug!("guessed format (or fallback): {:?}", reader.format());
- let original = reader.decode().context("decoding image")?.to_rgba8();
+ let mut file =
+ BufReader::new(File::open(&original_path).context("opening source")?);
+
+ // TODO: use better image library that supports AVIF
+ let is_avif = {
+ let mut magic = [0u8; 12];
+ file.read_exact(&mut magic).context("reading magic")?;
+ file.seek(SeekFrom::Start(0))
+ .context("seeking back to start")?;
+ // TODO: magic experimentally found and probably not working in all cases but fine as long as our avif enc uses that
+ magic
+ == [
+ 0x00, 0x00, 0x00, 0x18, 0x66, 0x74, 0x79, 0x70, 0x61, 0x76, 0x69, 0x66,
+ ]
+ };
+ let original = if is_avif {
+ let mut buf = Vec::new();
+ file.read_to_end(&mut buf).context("reading image")?;
+ libavif_image::read(&buf).unwrap().to_rgba8()
+ } else {
+ let reader = image::io::Reader::new(file);
+ let reader = reader.with_guessed_format().context("guessing format")?;
+ debug!("guessed format (or fallback): {:?}", reader.format());
+ reader.decode().context("decoding image")?.to_rgba8()
+ };
let image = image::imageops::resize(
&original,
width as u32,