61
61
#endif
62
62
#endif
63
63
64
+ SDImageCoderOption _Nonnull const SDImageCoderAVIFDecodeCodecChoice = @" avifDecodeCodecChoice" ;
65
+ SDImageCoderOption _Nonnull const SDImageCoderAVIFEncodeCodecChoice = @" avifEncodeCodecChoice" ;
66
+
64
67
@implementation SDImageAVIFCoder {
65
68
avifDecoder *_decoder;
66
69
NSData *_imageData;
@@ -122,9 +125,17 @@ - (UIImage *)decodedImageWithData:(NSData *)data options:(SDImageCoderOptions *)
122
125
preserveAspectRatio = preserveAspectRatioValue.boolValue ;
123
126
}
124
127
128
+ avifCodecChoice codecChoice = AVIF_CODEC_CHOICE_AUTO;
129
+ NSNumber *codecChoiceValue = options[SDImageCoderAVIFDecodeCodecChoice];
130
+ if (codecChoiceValue != nil ) {
131
+ codecChoice = [codecChoiceValue intValue ];
132
+ }
133
+
125
134
// Decode it
126
135
avifDecoder * decoder = avifDecoderCreate ();
127
136
avifDecoderSetIOMemory (decoder, data.bytes , data.length );
137
+ decoder->maxThreads = 2 ;
138
+ decoder->codecChoice = codecChoice;
128
139
// Disable strict mode to keep some AVIF image compatible
129
140
decoder->strictFlags = AVIF_STRICT_DISABLED;
130
141
avifResult decodeResult = avifDecoderParse (decoder);
@@ -270,6 +281,12 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm
270
281
return nil ;
271
282
}
272
283
284
+ avifCodecChoice codecChoice = AVIF_CODEC_CHOICE_AUTO;
285
+ NSNumber *codecChoiceValue = options[SDImageCoderAVIFEncodeCodecChoice];
286
+ if (codecChoiceValue != nil ) {
287
+ codecChoice = [codecChoiceValue intValue ];
288
+ }
289
+
273
290
avifPixelFormat avifFormat = AVIF_PIXEL_FORMAT_YUV444;
274
291
275
292
avifImage *avif = avifImageCreate ((int )width, (int )height, 8 , avifFormat);
@@ -300,6 +317,7 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm
300
317
301
318
avifRWData raw = AVIF_DATA_EMPTY;
302
319
avifEncoder *encoder = avifEncoderCreate ();
320
+ encoder->codecChoice = codecChoice;
303
321
encoder->minQuantizer = rescaledQuality;
304
322
encoder->maxQuantizer = rescaledQuality;
305
323
encoder->minQuantizerAlpha = rescaledQuality;
@@ -324,8 +342,15 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm
324
342
- (instancetype )initWithAnimatedImageData : (NSData *)data options : (SDImageCoderOptions *)options {
325
343
self = [super init ];
326
344
if (self) {
345
+ avifCodecChoice codecChoice = AVIF_CODEC_CHOICE_AUTO;
346
+ NSNumber *codecChoiceValue = options[SDImageCoderAVIFDecodeCodecChoice];
347
+ if (codecChoiceValue != nil ) {
348
+ codecChoice = [codecChoiceValue intValue ];
349
+ }
327
350
avifDecoder *decoder = avifDecoderCreate ();
328
351
avifDecoderSetIOMemory (decoder, data.bytes , data.length );
352
+ decoder->maxThreads = 2 ;
353
+ decoder->codecChoice = codecChoice;
329
354
// Disable strict mode to keep some AVIF image compatible
330
355
decoder->strictFlags = AVIF_STRICT_DISABLED;
331
356
avifResult decodeResult = avifDecoderParse (decoder);
0 commit comments