29
29
}
30
30
#endif
31
31
32
- // Should moved to SDWebImage Core
33
- #include < sys/sysctl.h>
34
- static int computeHostNumLogicalCores (void ) {
35
- uint32_t count;
36
- size_t len = sizeof (count);
37
- sysctlbyname (" hw.logicalcpu" , &count, &len, NULL , 0 );
38
- if (count < 1 ) {
39
- int nm[2 ];
40
- nm[0 ] = CTL_HW;
41
- nm[1 ] = HW_AVAILCPU;
42
- sysctl (nm, 2 , &count, &len, NULL , 0 );
43
- if (count < 1 )
44
- return -1 ;
45
- }
46
- return count;
47
- }
48
-
49
32
static void FreeImageData (void *info, const void *data, size_t size) {
50
33
free ((void *)data);
51
34
}
@@ -386,6 +369,11 @@ - (NSData *)encodedDataWithFrames:(NSArray<SDImageFrame *> *)frames loopCount:(N
386
369
}
387
370
distance = JxlEncoderDistanceFromQuality (compressionQuality * 100.0 );
388
371
}
372
+ // calculate multithread count
373
+ size_t threadCount = [options[SDImageCoderEncodeJXLThreadCount] unsignedIntValue ];
374
+ if (threadCount == 0 ) {
375
+ threadCount = JxlThreadParallelRunnerDefaultNumWorkerThreads ();
376
+ }
389
377
390
378
NSMutableData *output = [NSMutableData data ];
391
379
BOOL success = NO ;
@@ -402,9 +390,14 @@ - (NSData *)encodedDataWithFrames:(NSArray<SDImageFrame *> *)frames loopCount:(N
402
390
}
403
391
JxlEncoderFrameSettings* frame_settings = JxlEncoderFrameSettingsCreate (enc, NULL );
404
392
// setup basic info for whole encoding
405
- JxlEncoderStatus jret = SetupEncoderForPrimaryImage (enc, frame_settings, imageRef, orientation, distance, hasAnimation, loopCount, options);
393
+ void * runner = NULL ;
394
+ if (threadCount > 1 ) {
395
+ runner = JxlThreadParallelRunnerCreate (NULL , threadCount);
396
+ }
397
+ JxlEncoderStatus jret = SetupEncoderForPrimaryImage (enc, frame_settings, runner, imageRef, orientation, distance, hasAnimation, loopCount, options);
406
398
if (jret != JXL_ENC_SUCCESS) {
407
399
JxlEncoderDestroy (enc);
400
+ JxlThreadParallelRunnerDestroy (runner);
408
401
return nil ;
409
402
}
410
403
@@ -413,6 +406,7 @@ - (NSData *)encodedDataWithFrames:(NSArray<SDImageFrame *> *)frames loopCount:(N
413
406
success = [self sd_encodeFrameWithEnc: enc frameSettings: frame_settings frame: imageRef orientation: orientation duration: 0 options: options output: output];
414
407
if (!success) {
415
408
JxlEncoderDestroy (enc);
409
+ JxlThreadParallelRunnerDestroy (runner);
416
410
return nil ;
417
411
}
418
412
// finish input and ready for output
@@ -430,6 +424,7 @@ - (NSData *)encodedDataWithFrames:(NSArray<SDImageFrame *> *)frames loopCount:(N
430
424
// earily break
431
425
if (!success) {
432
426
JxlEncoderDestroy (enc);
427
+ JxlThreadParallelRunnerDestroy (runner);
433
428
return nil ;
434
429
}
435
430
// last frame
@@ -448,6 +443,7 @@ - (NSData *)encodedDataWithFrames:(NSArray<SDImageFrame *> *)frames loopCount:(N
448
443
// destroying the decoder also frees JxlEncoderFrameSettings
449
444
// free(frame_settings);
450
445
JxlEncoderDestroy (enc);
446
+ JxlThreadParallelRunnerDestroy (runner);
451
447
452
448
if (jret != JXL_ENC_SUCCESS) {
453
449
return nil ;
@@ -456,7 +452,7 @@ - (NSData *)encodedDataWithFrames:(NSArray<SDImageFrame *> *)frames loopCount:(N
456
452
}
457
453
458
454
// see: https://github.com/libjxl/libjxl/blob/main/lib/jxl/roundtrip_test.cc#L165
459
- JxlEncoderStatus EncodeWithEncoder (JxlEncoder* enc, NSMutableData *compressed) {
455
+ static JxlEncoderStatus EncodeWithEncoder (JxlEncoder* enc, NSMutableData *compressed) {
460
456
// increase output buffer by 64 bytes once a time
461
457
[compressed increaseLengthBy: 64 ];
462
458
uint8_t * next_out = compressed.mutableBytes ;
@@ -482,7 +478,7 @@ JxlEncoderStatus EncodeWithEncoder(JxlEncoder* enc, NSMutableData *compressed) {
482
478
return JXL_ENC_SUCCESS;
483
479
}
484
480
485
- JxlEncoderStatus SetupEncoderForPrimaryImage (JxlEncoder *enc, JxlEncoderFrameSettings *frame_settings, CGImageRef imageRef, CGImagePropertyOrientation orientation, float distance, BOOL hasAnimation, NSUInteger loopCount, NSDictionary *options) {
481
+ static JxlEncoderStatus SetupEncoderForPrimaryImage (JxlEncoder *enc, JxlEncoderFrameSettings *frame_settings, void * runner , CGImageRef imageRef, CGImagePropertyOrientation orientation, float distance, BOOL hasAnimation, NSUInteger loopCount, NSDictionary *options) {
486
482
// bitmap info from CGImage
487
483
size_t width = CGImageGetWidth (imageRef);
488
484
size_t height = CGImageGetHeight (imageRef);
@@ -625,13 +621,7 @@ JxlEncoderStatus SetupEncoderForPrimaryImage(JxlEncoder *enc, JxlEncoderFrameSet
625
621
return jret;
626
622
}
627
623
628
- /* This needs to be set each time the encoder is reset */
629
- size_t threadCount = [options[SDImageCoderEncodeJXLThreadCount] unsignedIntValue ];
630
- if (threadCount == 0 ) {
631
- threadCount = computeHostNumLogicalCores ();
632
- }
633
- if (threadCount > 1 ) {
634
- void * runner = JxlThreadParallelRunnerCreate (NULL , threadCount);
624
+ if (runner) {
635
625
jret = JxlEncoderSetParallelRunner (enc, JxlThreadParallelRunner, runner);
636
626
}
637
627
0 commit comments