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