7
7
typedef struct {
8
8
PyObject_HEAD avifEncoder * encoder ;
9
9
avifImage * image ;
10
- int frame_index ;
10
+ int first_frame ;
11
11
} AvifEncoderObject ;
12
12
13
13
static PyTypeObject AvifEncoder_Type ;
@@ -16,7 +16,6 @@ static PyTypeObject AvifEncoder_Type;
16
16
typedef struct {
17
17
PyObject_HEAD avifDecoder * decoder ;
18
18
Py_buffer buffer ;
19
- char * mode ;
20
19
} AvifDecoderObject ;
21
20
22
21
static PyTypeObject AvifDecoder_Type ;
@@ -154,7 +153,7 @@ exif_orientation_to_irot_imir(avifImage *image, int orientation) {
154
153
}
155
154
156
155
static int
157
- _codec_available (const char * name , uint32_t flags ) {
156
+ _codec_available (const char * name , avifCodecFlags flags ) {
158
157
avifCodecChoice codec = avifCodecChoiceFromName (name );
159
158
if (codec == AVIF_CODEC_CHOICE_AUTO ) {
160
159
return 0 ;
@@ -252,7 +251,7 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
252
251
253
252
if (!PyArg_ParseTuple (
254
253
args ,
255
- "IIsiiissiiOOy *y*iy*O" ,
254
+ "(II)siiissiiOOy *y*iy*O" ,
256
255
& width ,
257
256
& height ,
258
257
& subsampling ,
@@ -372,7 +371,7 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
372
371
avifEncoderDestroy (encoder );
373
372
return NULL ;
374
373
}
375
- self -> frame_index = - 1 ;
374
+ self -> first_frame = 1 ;
376
375
377
376
avifResult result ;
378
377
if (icc_buffer .len ) {
@@ -466,10 +465,9 @@ _encoder_add(AvifEncoderObject *self, PyObject *args) {
466
465
unsigned int width ;
467
466
unsigned int height ;
468
467
char * mode ;
469
- PyObject * is_single_frame = NULL ;
468
+ unsigned int is_single_frame ;
470
469
PyObject * ret = Py_None ;
471
470
472
- int is_first_frame ;
473
471
avifRGBImage rgb ;
474
472
avifResult result ;
475
473
@@ -479,7 +477,7 @@ _encoder_add(AvifEncoderObject *self, PyObject *args) {
479
477
480
478
if (!PyArg_ParseTuple (
481
479
args ,
482
- "z#IIIsO " ,
480
+ "y#I(II)sp " ,
483
481
(char * * )& rgb_bytes ,
484
482
& size ,
485
483
& duration ,
@@ -491,8 +489,6 @@ _encoder_add(AvifEncoderObject *self, PyObject *args) {
491
489
return NULL ;
492
490
}
493
491
494
- is_first_frame = self -> frame_index == -1 ;
495
-
496
492
if (image -> width != width || image -> height != height ) {
497
493
PyErr_Format (
498
494
PyExc_ValueError ,
@@ -505,7 +501,7 @@ _encoder_add(AvifEncoderObject *self, PyObject *args) {
505
501
return NULL ;
506
502
}
507
503
508
- if (is_first_frame ) {
504
+ if (self -> first_frame ) {
509
505
// If we don't have an image populated with yuv planes, this is the first frame
510
506
frame = image ;
511
507
} else {
@@ -577,7 +573,7 @@ _encoder_add(AvifEncoderObject *self, PyObject *args) {
577
573
}
578
574
579
575
uint32_t addImageFlags = AVIF_ADD_IMAGE_FLAG_NONE ;
580
- if (PyObject_IsTrue ( is_single_frame ) ) {
576
+ if (is_single_frame ) {
581
577
addImageFlags |= AVIF_ADD_IMAGE_FLAG_SINGLE ;
582
578
}
583
579
@@ -597,12 +593,12 @@ _encoder_add(AvifEncoderObject *self, PyObject *args) {
597
593
598
594
end :
599
595
avifRGBImageFreePixels (& rgb );
600
- if (!is_first_frame ) {
596
+ if (!self -> first_frame ) {
601
597
avifImageDestroy (frame );
602
598
}
603
599
604
600
if (ret == Py_None ) {
605
- self -> frame_index ++ ;
601
+ self -> first_frame = 0 ;
606
602
Py_RETURN_NONE ;
607
603
} else {
608
604
return ret ;
@@ -708,12 +704,6 @@ AvifDecoderNew(PyObject *self_, PyObject *args) {
708
704
return NULL ;
709
705
}
710
706
711
- if (decoder -> alphaPresent ) {
712
- self -> mode = "RGBA" ;
713
- } else {
714
- self -> mode = "RGB" ;
715
- }
716
-
717
707
self -> decoder = decoder ;
718
708
self -> buffer = buffer ;
719
709
@@ -757,7 +747,7 @@ _decoder_get_info(AvifDecoderObject *self) {
757
747
image -> width ,
758
748
image -> height ,
759
749
decoder -> imageCount ,
760
- self -> mode ,
750
+ decoder -> alphaPresent == AVIF_TRUE ? "RGBA" : "RGB" ,
761
751
NULL == icc ? Py_None : icc ,
762
752
NULL == exif ? Py_None : exif ,
763
753
irot_imir_to_exif_orientation (image ),
@@ -793,25 +783,19 @@ _decoder_get_frame(AvifDecoderObject *self, PyObject *args) {
793
783
PyErr_Format (
794
784
exc_type_for_avif_result (result ),
795
785
"Failed to decode frame %u: %s" ,
796
- decoder -> imageIndex + 1 ,
786
+ frame_index ,
797
787
avifResultToString (result )
798
788
);
799
789
return NULL ;
800
790
}
801
791
802
792
image = decoder -> image ;
803
793
804
- memset (& rgb , 0 , sizeof (rgb ));
805
794
avifRGBImageSetDefaults (& rgb , image );
806
795
807
796
rgb .depth = 8 ;
808
-
809
- if (decoder -> alphaPresent ) {
810
- rgb .format = AVIF_RGB_FORMAT_RGBA ;
811
- } else {
812
- rgb .format = AVIF_RGB_FORMAT_RGB ;
813
- rgb .ignoreAlpha = AVIF_TRUE ;
814
- }
797
+ rgb .format =
798
+ decoder -> alphaPresent == AVIF_TRUE ? AVIF_RGB_FORMAT_RGBA : AVIF_RGB_FORMAT_RGB ;
815
799
816
800
result = avifRGBImageAllocatePixels (& rgb );
817
801
if (result != AVIF_RESULT_OK ) {
@@ -940,5 +924,9 @@ PyInit__avif(void) {
940
924
return NULL ;
941
925
}
942
926
927
+ #ifdef Py_GIL_DISABLED
928
+ PyUnstable_Module_SetGIL (m , Py_MOD_GIL_NOT_USED );
929
+ #endif
930
+
943
931
return m ;
944
932
}
0 commit comments