@@ -20,8 +20,6 @@ typedef struct {
20
20
avifRange range ;
21
21
} avifEncOptions ;
22
22
23
- static int max_threads = 1 ;
24
-
25
23
// Encoder type
26
24
typedef struct {
27
25
PyObject_HEAD
@@ -45,6 +43,53 @@ typedef struct {
45
43
46
44
static PyTypeObject AvifDecoder_Type ;
47
45
46
+ static int max_threads = 0 ;
47
+
48
+ static void
49
+ init_max_threads (void ) {
50
+ PyObject * os = NULL ;
51
+ PyObject * n = NULL ;
52
+ long num_cpus ;
53
+
54
+ os = PyImport_ImportModule ("os" );
55
+ if (os == NULL ) {
56
+ goto error ;
57
+ }
58
+
59
+ if (PyObject_HasAttrString (os , "sched_getaffinity" )) {
60
+ n = PyObject_CallMethod (os , "sched_getaffinity" , "i" , 0 );
61
+ if (n == NULL ) {
62
+ goto error ;
63
+ }
64
+ num_cpus = PySet_Size (n );
65
+ } else {
66
+ n = PyObject_CallMethod (os , "cpu_count" , NULL );
67
+ if (n == NULL ) {
68
+ goto error ;
69
+ }
70
+ num_cpus = PyLong_AsLong (n );
71
+ }
72
+
73
+ if (num_cpus < 1 ) {
74
+ goto error ;
75
+ }
76
+
77
+ max_threads = (int )num_cpus ;
78
+
79
+ done :
80
+ Py_XDECREF (os );
81
+ Py_XDECREF (n );
82
+ return ;
83
+
84
+ error :
85
+ if (PyErr_Occurred ()) {
86
+ PyErr_Clear ();
87
+ }
88
+ PyErr_WarnEx (
89
+ PyExc_RuntimeWarning , "could not get cpu count: using max_threads=1" , 1 );
90
+ goto done ;
91
+ }
92
+
48
93
static int
49
94
normalize_quantize_value (int qvalue ) {
50
95
if (qvalue < AVIF_QUANTIZER_BEST_QUALITY ) {
@@ -206,6 +251,11 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
206
251
self -> xmp_bytes = NULL ;
207
252
208
253
encoder = avifEncoderCreate ();
254
+
255
+ if (max_threads == 0 ) {
256
+ init_max_threads ();
257
+ }
258
+
209
259
encoder -> maxThreads = max_threads ;
210
260
encoder -> minQuantizer = enc_options .qmin ;
211
261
encoder -> maxQuantizer = enc_options .qmax ;
@@ -506,6 +556,9 @@ AvifDecoderNew(PyObject *self_, PyObject *args) {
506
556
507
557
self -> decoder = avifDecoderCreate ();
508
558
#if AVIF_VERSION >= 80400
559
+ if (max_threads == 0 ) {
560
+ init_max_threads ();
561
+ }
509
562
self -> decoder -> maxThreads = max_threads ;
510
563
#endif
511
564
self -> decoder -> codecChoice = codec ;
@@ -742,57 +795,10 @@ setup_module(PyObject *m) {
742
795
return 0 ;
743
796
}
744
797
745
- static void
746
- init_max_threads (void ) {
747
- PyObject * os = NULL ;
748
- PyObject * n = NULL ;
749
- long num_cpus ;
750
-
751
- os = PyImport_ImportModule ("os" );
752
- if (os == NULL ) {
753
- goto error ;
754
- }
755
-
756
- if (PyObject_HasAttrString (os , "sched_getaffinity" )) {
757
- n = PyObject_CallMethod (os , "sched_getaffinity" , "i" , 0 );
758
- if (n == NULL ) {
759
- goto error ;
760
- }
761
- num_cpus = PySet_Size (n );
762
- } else {
763
- n = PyObject_CallMethod (os , "cpu_count" , NULL );
764
- if (n == NULL ) {
765
- goto error ;
766
- }
767
- num_cpus = PyLong_AsLong (n );
768
- }
769
-
770
- if (num_cpus < 1 ) {
771
- goto error ;
772
- }
773
-
774
- max_threads = (int )num_cpus ;
775
-
776
- done :
777
- Py_XDECREF (os );
778
- Py_XDECREF (n );
779
- return ;
780
-
781
- error :
782
- if (PyErr_Occurred ()) {
783
- PyErr_Clear ();
784
- }
785
- PyErr_WarnEx (
786
- PyExc_RuntimeWarning , "could not get cpu count: using max_threads=1" , 1 );
787
- goto done ;
788
- }
789
-
790
798
PyMODINIT_FUNC
791
799
PyInit__avif (void ) {
792
800
PyObject * m ;
793
801
794
- init_max_threads ();
795
-
796
802
static PyModuleDef module_def = {
797
803
PyModuleDef_HEAD_INIT ,
798
804
.m_name = "_avif" ,
0 commit comments