AOMediaCodec / libavif

libavif - Library for encoding and decoding .avif files
Other
1.57k stars 202 forks source link

Does libavif supports the decoding level image size scaling ? #1484

Open dreampiggy opened 1 year ago

dreampiggy commented 1 year ago

I search the header of avif.h and could not find anything related to this

Seems libwebp contains something like this:

// Decoding options
struct WebPDecoderOptions {
  int use_scaling;                    // if true, scaling is applied _afterward_
  int scaled_width, scaled_height;    // final resolution
};

Which allows easy to use when we want a thumbnail for huge image...Like my demo code

if (scaledSize.width != 0 && scaledSize.height != 0) {
    config.options.use_scaling = 1;
    config.options.scaled_width = scaledSize.width;
    config.options.scaled_height = scaledSize.height;
}

// Decode the WebP image data into
if (WebPDecode(webpData.bytes, webpData.size, &config) != VP8_STATUS_OK) {
    return nil;
}

Which can return the smaller scaled avifImage and its yuvPlanes

And also, seems the avifDecoderSource.AVIF_DECODER_SOURCE_THUMBNAIL_ITEM shows unimplemented 😂 Even if the AVIF contains embed thumbnail, I can not get that one for quickly display for thumbnail.


My personal use case: Speed up the render of tons of AVIF thumbnails on macOS Finder using https://github.com/dreampiggy/AVIFQuickLook

vigneshvg commented 1 year ago

As of now, there is no way to scale the image at the decoder level (i.e.) the whole image will always be decoded.

libavif has a function that you can use to scale the image after it is decoded. Please look into avifImageScale. It is not part of the public API though.

If this solution is of interest to you, we can consider making the avifImageScale function publicly available in avif.h

maryla-uc commented 1 year ago

avifImageScale is also used in Skia which uses internal.h to do that, even though it's supposed to be internal... It might be good to make the function public.

vigneshvg commented 1 year ago

avifImageScale is also used in Skia which uses internal.h to do that, even though it's supposed to be internal... It might be good to make the function public.

yeah the blame on that one is on me. when the skia wrapper was written, the scaling code was only available when libavif was built with libyuv. so we couldn't make that public as it would depend on libyuv existing (#937). so i went ahead and used the internal header in skia (since skia always builds libavif with libyuv).

but now we have scaling support natively in libavif without the need for libyuv ( thanks to #1534 ). so the libavif scale functions can be made public. i will revive #937 and update the skia code as well.