Repository navigation
Decode AVIF through shared image codecs #4312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+357
−19
Merged
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
6e93286
Decode AVIF through shared image codecs
dylanmtaylor fd903de
Include JPEG encoder distribution notices
dylanmtaylor daed3a3
Exercise AVIF HDR decoding through public API
dylanmtaylor a47ba5b
Preserve AVIF source bit depth
dylanmtaylor ad442f3
Test AVIF alpha through public entrypoints
dylanmtaylor 7d6f6fa
Update AVIF round trip for supported decode
dylanmtaylor 55e9889
Document native AVIF decode support
dylanmtaylor 797b3ef
Clarify import feature gates
dylanmtaylor 99c47f7
Keep RAW fixtures valid under shared container and strip bounds
dylanmtaylor 4225ae3
Merge main without regressing AVIF shared dependency alignment
dylanmtaylor dea4c9d
Include sample-to-chunk mappings in CR3 fixtures
dylanmtaylor e4c89d7
Find Similar test: a photo read from memory isn't a missing read (#3823)
Bruno-Alumn 3455e4f
Find Similar test: every photo is measured after the resumed search
Bruno-Alumn e44fab5
Integrate main JPEG cutover while retaining AVIF source-depth pin
dylanmtaylor dfe879a
Bound AVIF padded picture storage and preserve integer source flag
dylanmtaylor c6e5789
Merge main and pin shared crates to craft-libs#1679 merge
FredApps 3ef50ea
Size the AVIF picture-store budget for the stored canvas, not the cla…
FredApps 4d1001c
Attribute the AVIF lossless decode fixture
FredApps 111f31f
Keep the AVIF clap test helpers free of unwrap for the codecs lint gate
FredApps d8cc1eb
Merge remote-tracking branch 'origin/main' into t4312
FredApps efc47ac
Merge remote-tracking branch 'origin/main' into t4312
FredApps 1c86e96
Merge remote-tracking branch 'origin/main' into t4312
FredApps 70d26b6
Merge remote-tracking branch 'origin/main' into HEAD
dylanmtaylor ac118ad
lightcraft: keep the newer craft-cms pin after merging main
dylanmtaylor 1b6b4b4
Merge remote-tracking branch 'origin/main' into HEAD
dylanmtaylor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| //! AVIF container and pixel decoding via shared image codecs. | ||
| use crate::{DecodeOptions, Decoded, Error, Format, Result}; | ||
|
|
||
| const F: Format = Format::Avif; | ||
|
|
||
| #[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))] | ||
| pub(crate) fn header(_: &[u8]) -> Result<(u32, u32, u16)> { | ||
| Err(Error::Unsupported(F, "AVIF decoding is not included in this build")) | ||
| } | ||
| #[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))] | ||
| pub(crate) fn decode(_: &[u8], _: &DecodeOptions) -> Result<Decoded> { | ||
| Err(Error::Unsupported(F, "AVIF decoding is not included in this build")) | ||
| } | ||
|
|
||
| #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] | ||
| fn err(e: craft_image_codecs::CodecError) -> Error { | ||
| match e { | ||
| craft_image_codecs::CodecError::LimitExceeded(_) => Error::TooLarge(0, 0), | ||
| craft_image_codecs::CodecError::Unsupported { .. } => Error::Unsupported(F, "unsupported AVIF"), | ||
| other => Error::Malformed(F, other.to_string()), | ||
| } | ||
| } | ||
| #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] | ||
| pub(crate) fn header(bytes: &[u8]) -> Result<(u32, u32, u16)> { | ||
| let (w, h) = craft_image_codecs::probe_size(bytes).map_err(err)?; | ||
| Ok((w, h, 1)) | ||
| } | ||
| #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] | ||
| pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> { | ||
| use crate::convert::{Buf, Meta, Model, Raw, check_size, finish}; | ||
| use crate::space::SourceSpace; | ||
| let (w, h, _) = header(bytes)?; | ||
| check_size(F, w as u64, h as u64, opts)?; | ||
| let options = craft_image_codecs::DecodeOptions { | ||
| limits: craft_image_codecs::Limits { | ||
| max_width: u32::MAX, | ||
| max_height: u32::MAX, | ||
| max_pixels: opts.max_pixels, | ||
| max_alloc: opts.max_pixels.saturating_mul(32), | ||
| }, | ||
| keep_orientation: false, | ||
| }; | ||
| let (img, bit_depth) = craft_image_codecs::decode_avif_with_depth(bytes, &options).map_err(err)?; | ||
| let (w, h) = (img.width(), img.height()); | ||
| check_size(F, w as u64, h as u64, opts)?; | ||
| let buf = match img.sample_type() { | ||
| craft_image_codecs::SampleType::U8 => Buf::U8(img.as_u8().ok_or_else(|| Error::Malformed(F, "expected 8-bit samples".into()))?.to_vec()), | ||
| craft_image_codecs::SampleType::U16 => Buf::U16(img.to_u16_samples().ok_or_else(|| Error::Malformed(F, "expected 16-bit samples".into()))?), | ||
| craft_image_codecs::SampleType::F32 => Buf::F32(img.to_f32_samples().ok_or_else(|| Error::Malformed(F, "expected float samples".into()))?), | ||
| _ => return Err(Error::Unsupported(F, "unsupported AVIF sample type")), | ||
| }; | ||
| let hint = img.cicp.and_then(|c| SourceSpace::from_cicp(c.primaries, c.transfer)); | ||
| let raw = Raw { width: w as usize, height: h as usize, model: Model::Rgb, alpha: img.layout().has_alpha(), premultiplied: false, buf, bit_depth }; | ||
| let meta = Meta { icc: img.icc, exif: img.meta.exif, xmp: img.meta.xmp, orientation: Some(1), hint, ..Default::default() }; | ||
| finish(F, raw, meta, (w, h), opts) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| #[cfg(all(test, feature = "avif", not(target_arch = "wasm32")))] | ||
| mod tests { | ||
| use super::*; | ||
| const FIXTURE: &[u8] = include_bytes!("../tests/fixtures/quad-444-gbr-lossless.avif"); | ||
| #[test] | ||
| fn production_entrypoints_decode_avif_and_enforce_pixel_budget() { | ||
| assert!(F.can_decode()); | ||
| assert_eq!(crate::sniff(FIXTURE), Some(F)); | ||
| let header = crate::read_header(FIXTURE).unwrap(); | ||
| assert_eq!((header.width, header.height, header.orientation), (33, 17, 1)); | ||
| let decoded = crate::decode(FIXTURE, DecodeOptions::default()).unwrap(); | ||
| assert_eq!((decoded.source_width, decoded.source_height), (33, 17)); | ||
| assert_eq!(decoded.bit_depth, 8); | ||
| let opts = DecodeOptions { max_pixels: 100, ..DecodeOptions::default() }; | ||
| assert!(matches!(crate::decode(FIXTURE, opts), Err(Error::TooLarge(..)))); | ||
| } | ||
| #[test] | ||
| fn hdr_avif_round_trips_highlights_through_public_decoder() { | ||
| let pixels = vec![2.0f32; 16 * 16 * 3]; | ||
| for transfer in [crate::encode::HdrTransfer::Pq, crate::encode::HdrTransfer::Hlg] { | ||
| let bytes = crate::encode::encode_avif_hdr(16, 16, &pixels, 95, 10, &crate::encode::EncodeMeta::default(), transfer).unwrap(); | ||
| let decoded = crate::decode(&bytes, DecodeOptions::default()).unwrap(); | ||
| assert_eq!(decoded.bit_depth, 10); | ||
| assert!(decoded.image.data.iter().any(|pixel| pixel[0] > 1.0), "HDR highlights were lost"); | ||
| } | ||
| } | ||
| #[test] | ||
| fn avif_alpha_survives_public_round_trip() { | ||
| let pixels = [200u8, 100, 50, 96].repeat(16 * 16); | ||
| let image = crate::encode::EncodeImage::new(16, 16, 4, crate::encode::Samples::U8(&pixels)); | ||
| let bytes = crate::encode::encode_avif(&image, 95, 10, &crate::encode::EncodeMeta::default()).unwrap(); | ||
| let decoded = crate::decode(&bytes, DecodeOptions::default()).unwrap(); | ||
| let alpha = decoded.alpha.expect("alpha plane"); | ||
| assert!((alpha.data[0] - 96.0 / 255.0).abs() < 0.04); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # AVIF decode fixtures | ||
|
|
||
| Small AVIF files from an independent encoder (libaom through FFmpeg 7.1), for `src/codecs/avif_decode.rs`. | ||
|
|
||
| - `quad-source.rgb`: the 33×17 source, raw RGB24 four-colour quadrants (red, green, blue, near-white). | ||
| - `quad-420-bt709.avif`: 8-bit 4:2:0, limited range, BT.709 matrix, odd size (FFmpeg leaves the primaries and transfer unspecified): | ||
| `ffmpeg -f rawvideo -pix_fmt rgb24 -s 33x17 -i quad-source.rgb -c:v libaom-av1 -crf 12 -still-picture 1 -pix_fmt yuv420p -colorspace bt709 -color_primaries bt709 -color_trc iec61966-2-1 -color_range tv quad-420-bt709.avif` | ||
| - `quad-420-bt709.dav1d.yuv`: that file's AV1 picture as libdav1d decodes it, planar 8-bit 4:2:0 (Y, then Cb, then Cr): | ||
| `ffmpeg -c:v libdav1d -i quad-420-bt709.avif -f rawvideo -pix_fmt yuv420p quad-420-bt709.dav1d.yuv` | ||
| - `quad-444-gbr-lossless.avif`: lossless 8-bit 4:4:4 with the identity (GBR) matrix, full range: | ||
| `ffmpeg -f rawvideo -pix_fmt rgb24 -s 33x17 -i quad-source.rgb -c:v libaom-av1 -crf 0 -still-picture 1 -pix_fmt gbrp -colorspace rgb -color_range pc quad-444-gbr-lossless.avif`. | ||
| It decodes to `quad-source.rgb` exactly. |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.