Skip to content

Commit 7861ff0

Browse files
authored
Merge pull request #58 from SDWebImage/feature/codec_choice
Added the option for avifCodecChoice to control which codec (aom/dav1d/rav1e/svt-av1) to use
2 parents 079994d + 584ce52 commit 7861ff0

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

SDWebImageAVIFCoder/Classes/Public/SDImageAVIFCoder.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

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

16+
/// A `avifCodecChoice` enum which specify the custom codec for AVIF decoding, defaults to 0 (`AVIF_CODEC_CHOICE_AUTO`)
17+
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderAVIFDecodeCodecChoice;
18+
19+
/// A `avifCodecChoice` enum which specify the custom codec for AVIF encoding, defaults to 0 (`AVIF_CODEC_CHOICE_AUTO`)
20+
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderAVIFEncodeCodecChoice;
21+
1622
/// Supports AVIF static image and AVIFS animated image
1723
@interface SDImageAVIFCoder : NSObject <SDAnimatedImageCoder>
1824

SDWebImageAVIFCoder/Classes/SDImageAVIFCoder.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
#endif
6262
#endif
6363

64+
SDImageCoderOption _Nonnull const SDImageCoderAVIFDecodeCodecChoice = @"avifDecodeCodecChoice";
65+
SDImageCoderOption _Nonnull const SDImageCoderAVIFEncodeCodecChoice = @"avifEncodeCodecChoice";
66+
6467
@implementation SDImageAVIFCoder {
6568
avifDecoder *_decoder;
6669
NSData *_imageData;
@@ -122,9 +125,17 @@ - (UIImage *)decodedImageWithData:(NSData *)data options:(SDImageCoderOptions *)
122125
preserveAspectRatio = preserveAspectRatioValue.boolValue;
123126
}
124127

128+
avifCodecChoice codecChoice = AVIF_CODEC_CHOICE_AUTO;
129+
NSNumber *codecChoiceValue = options[SDImageCoderAVIFDecodeCodecChoice];
130+
if (codecChoiceValue != nil) {
131+
codecChoice = [codecChoiceValue intValue];
132+
}
133+
125134
// Decode it
126135
avifDecoder * decoder = avifDecoderCreate();
127136
avifDecoderSetIOMemory(decoder, data.bytes, data.length);
137+
decoder->maxThreads = 2;
138+
decoder->codecChoice = codecChoice;
128139
// Disable strict mode to keep some AVIF image compatible
129140
decoder->strictFlags = AVIF_STRICT_DISABLED;
130141
avifResult decodeResult = avifDecoderParse(decoder);
@@ -270,6 +281,12 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm
270281
return nil;
271282
}
272283

284+
avifCodecChoice codecChoice = AVIF_CODEC_CHOICE_AUTO;
285+
NSNumber *codecChoiceValue = options[SDImageCoderAVIFEncodeCodecChoice];
286+
if (codecChoiceValue != nil) {
287+
codecChoice = [codecChoiceValue intValue];
288+
}
289+
273290
avifPixelFormat avifFormat = AVIF_PIXEL_FORMAT_YUV444;
274291

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

301318
avifRWData raw = AVIF_DATA_EMPTY;
302319
avifEncoder *encoder = avifEncoderCreate();
320+
encoder->codecChoice = codecChoice;
303321
encoder->minQuantizer = rescaledQuality;
304322
encoder->maxQuantizer = rescaledQuality;
305323
encoder->minQuantizerAlpha = rescaledQuality;
@@ -324,8 +342,15 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm
324342
- (instancetype)initWithAnimatedImageData:(NSData *)data options:(SDImageCoderOptions *)options {
325343
self = [super init];
326344
if (self) {
345+
avifCodecChoice codecChoice = AVIF_CODEC_CHOICE_AUTO;
346+
NSNumber *codecChoiceValue = options[SDImageCoderAVIFDecodeCodecChoice];
347+
if (codecChoiceValue != nil) {
348+
codecChoice = [codecChoiceValue intValue];
349+
}
327350
avifDecoder *decoder = avifDecoderCreate();
328351
avifDecoderSetIOMemory(decoder, data.bytes, data.length);
352+
decoder->maxThreads = 2;
353+
decoder->codecChoice = codecChoice;
329354
// Disable strict mode to keep some AVIF image compatible
330355
decoder->strictFlags = AVIF_STRICT_DISABLED;
331356
avifResult decodeResult = avifDecoderParse(decoder);

0 commit comments

Comments
 (0)