Skip to content

Commit

Permalink
Merge pull request #58 from SDWebImage/feature/codec_choice
Browse files Browse the repository at this point in the history
Added the option for avifCodecChoice to control which codec (aom/dav1d/rav1e/svt-av1) to use
  • Loading branch information
dreampiggy authored Sep 28, 2023
2 parents 079994d + 584ce52 commit 7861ff0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions SDWebImageAVIFCoder/Classes/Public/SDImageAVIFCoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

static const SDImageFormat SDImageFormatAVIF = 15; // AV1-codec based HEIF

/// A `avifCodecChoice` enum which specify the custom codec for AVIF decoding, defaults to 0 (`AVIF_CODEC_CHOICE_AUTO`)
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderAVIFDecodeCodecChoice;

/// A `avifCodecChoice` enum which specify the custom codec for AVIF encoding, defaults to 0 (`AVIF_CODEC_CHOICE_AUTO`)
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderAVIFEncodeCodecChoice;

/// Supports AVIF static image and AVIFS animated image
@interface SDImageAVIFCoder : NSObject <SDAnimatedImageCoder>

Expand Down
25 changes: 25 additions & 0 deletions SDWebImageAVIFCoder/Classes/SDImageAVIFCoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
#endif
#endif

SDImageCoderOption _Nonnull const SDImageCoderAVIFDecodeCodecChoice = @"avifDecodeCodecChoice";
SDImageCoderOption _Nonnull const SDImageCoderAVIFEncodeCodecChoice = @"avifEncodeCodecChoice";

@implementation SDImageAVIFCoder {
avifDecoder *_decoder;
NSData *_imageData;
Expand Down Expand Up @@ -122,9 +125,17 @@ - (UIImage *)decodedImageWithData:(NSData *)data options:(SDImageCoderOptions *)
preserveAspectRatio = preserveAspectRatioValue.boolValue;
}

avifCodecChoice codecChoice = AVIF_CODEC_CHOICE_AUTO;
NSNumber *codecChoiceValue = options[SDImageCoderAVIFDecodeCodecChoice];
if (codecChoiceValue != nil) {
codecChoice = [codecChoiceValue intValue];
}

// Decode it
avifDecoder * decoder = avifDecoderCreate();
avifDecoderSetIOMemory(decoder, data.bytes, data.length);
decoder->maxThreads = 2;
decoder->codecChoice = codecChoice;
// Disable strict mode to keep some AVIF image compatible
decoder->strictFlags = AVIF_STRICT_DISABLED;
avifResult decodeResult = avifDecoderParse(decoder);
Expand Down Expand Up @@ -270,6 +281,12 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm
return nil;
}

avifCodecChoice codecChoice = AVIF_CODEC_CHOICE_AUTO;
NSNumber *codecChoiceValue = options[SDImageCoderAVIFEncodeCodecChoice];
if (codecChoiceValue != nil) {
codecChoice = [codecChoiceValue intValue];
}

avifPixelFormat avifFormat = AVIF_PIXEL_FORMAT_YUV444;

avifImage *avif = avifImageCreate((int)width, (int)height, 8, avifFormat);
Expand Down Expand Up @@ -300,6 +317,7 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm

avifRWData raw = AVIF_DATA_EMPTY;
avifEncoder *encoder = avifEncoderCreate();
encoder->codecChoice = codecChoice;
encoder->minQuantizer = rescaledQuality;
encoder->maxQuantizer = rescaledQuality;
encoder->minQuantizerAlpha = rescaledQuality;
Expand All @@ -324,8 +342,15 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm
- (instancetype)initWithAnimatedImageData:(NSData *)data options:(SDImageCoderOptions *)options {
self = [super init];
if (self) {
avifCodecChoice codecChoice = AVIF_CODEC_CHOICE_AUTO;
NSNumber *codecChoiceValue = options[SDImageCoderAVIFDecodeCodecChoice];
if (codecChoiceValue != nil) {
codecChoice = [codecChoiceValue intValue];
}
avifDecoder *decoder = avifDecoderCreate();
avifDecoderSetIOMemory(decoder, data.bytes, data.length);
decoder->maxThreads = 2;
decoder->codecChoice = codecChoice;
// Disable strict mode to keep some AVIF image compatible
decoder->strictFlags = AVIF_STRICT_DISABLED;
avifResult decodeResult = avifDecoderParse(decoder);
Expand Down

0 comments on commit 7861ff0

Please sign in to comment.