Skip to content

Add AVIF plugin (decoder + encoder using libavif) #5201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 64 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
3878b58
Add AVIF plugin (using libavif)
fdintino Jan 3, 2021
e2add24
Added type hints (#2)
radarhere Oct 16, 2024
e5494a2
Fix PLAT envvar in cibuildwheel container
fdintino Oct 16, 2024
8b8bbba
Update Tests/check_avif_leaks.py
fdintino Oct 18, 2024
58ef692
Simplified code (#3)
radarhere Oct 19, 2024
d6a0a15
Merge branch 'main' into libavif-plugin
radarhere Nov 8, 2024
50b993a
Set default max threads in Python (#4)
radarhere Nov 11, 2024
671e3c8
Removed unused upsampling setting (#5)
radarhere Nov 12, 2024
658cdf3
Merge branch 'main' into libavif-plugin
radarhere Nov 18, 2024
7225cb9
Merge branch 'main' into libavif-plugin
radarhere Nov 24, 2024
3730bf2
Merge branch 'main' into libavif-plugin
radarhere Nov 30, 2024
c40bcbf
Improved error handling
radarhere Dec 2, 2024
d76ae2f
Do not ignore SyntaxError when saving EXIF data (#8)
radarhere Dec 7, 2024
9ad8311
Allow libavif to install rav1e, except on manylinux2014 and aarch64 (#7)
radarhere Dec 7, 2024
de4c6c1
Removed ld64 flag (#6)
radarhere Dec 7, 2024
524d802
fix: set exif orientation from irot/imir when decoding AVIF
fdintino Dec 9, 2024
7b73d77
Merge branch 'main' into libavif-plugin
radarhere Dec 13, 2024
a56acd8
Removed unittest mock (#10)
radarhere Dec 13, 2024
f5dc957
Use cmds_cmake (#9)
radarhere Dec 13, 2024
8d77678
chore(docs): update quality and speed with correct defaults
fdintino Dec 13, 2024
4eaa6b7
Merge branch 'main' into libavif-plugin
radarhere Dec 14, 2024
bdb24f9
Removed `_avif.HAVE_AVIF` and `_avif.VERSION` (#11)
radarhere Dec 15, 2024
ddc8e7e
Use "rav1e" if available as default ("auto") avif encoder
fdintino Dec 15, 2024
b585f9e
Simplified EXIF code (#12)
radarhere Dec 15, 2024
da2e18d
Revert "Use "rav1e" if available as default ("auto") avif encoder"
fdintino Dec 17, 2024
9b6e575
Merge branch 'main' into libavif-plugin
radarhere Dec 18, 2024
3a9a3ab
Reduced epsilons (#13)
radarhere Dec 24, 2024
9328932
Removed avifEncOptions (#14)
radarhere Jan 3, 2025
be02830
Merge branch 'main' into libavif-plugin
radarhere Jan 3, 2025
4135664
Merge branch 'main' into libavif-plugin
radarhere Jan 4, 2025
29c158d
Merge branch 'main' into libavif-plugin
radarhere Jan 8, 2025
4c63ea6
Fixed series of tuples as advanced argument (#15)
radarhere Jan 15, 2025
ce6bf21
Merge branch 'main' into libavif-plugin
radarhere Jan 17, 2025
4b29af4
Skip building libavif on 32-bit Windows (#16)
radarhere Jan 21, 2025
38f0d10
Merge branch 'main' into libavif-plugin
radarhere Jan 25, 2025
1410d23
Removed qmin and qmax (#17)
radarhere Jan 25, 2025
6cbad27
Merge branch 'main' into libavif-plugin
radarhere Feb 1, 2025
19ba2dd
Use rgb.rowBytes in overflow check (#18)
radarhere Feb 3, 2025
4508f37
Use aom LICENSE instead of PATENTS (#19)
radarhere Feb 3, 2025
7de1212
Merge branch 'main' into libavif-plugin
radarhere Feb 7, 2025
e1509ee
Removed memset and ignoreAlpha (#20)
radarhere Feb 9, 2025
0590f08
Handle avifDecoderCreate and avifEncoderCreate errors (#21)
radarhere Feb 12, 2025
5761b44
Merge branch 'main' into libavif-plugin
radarhere Feb 14, 2025
38b9941
Sort formats alphabetically
radarhere Feb 15, 2025
10dfa63
Simplified code
radarhere Feb 15, 2025
9abfdbc
Merge branch 'main' into libavif-plugin
radarhere Mar 3, 2025
d80ac3c
Use default PyTypeObject values
radarhere Feb 8, 2025
b4eec64
Merge branch 'main' into libavif-plugin
radarhere Mar 17, 2025
d552087
Updated libavif to 1.2.1 (#26)
radarhere Mar 17, 2025
79f7339
Updated wording
radarhere Mar 19, 2025
46f4508
Added version comments
radarhere Mar 19, 2025
9bebf37
Sort alphabetically
radarhere Mar 19, 2025
5da2113
Simplified code
radarhere Mar 19, 2025
0732554
Merge branch 'main' into libavif-plugin
radarhere Mar 21, 2025
9ea5e3d
Remove support for libavif < 1.0.0
radarhere Mar 21, 2025
fca6df2
Added get_codec_version() (#29)
radarhere Mar 21, 2025
024a894
Merge branch 'radarhere-avif_1' into libavif-plugin
fdintino Mar 21, 2025
2ba9356
Update rust (#33)
radarhere Mar 25, 2025
fdc68e6
Merge branch 'main' into libavif-plugin
radarhere Mar 31, 2025
9e63868
Continue to build libyuv on macOS
radarhere Mar 31, 2025
eff2680
Added release notes
radarhere Mar 31, 2025
fb096e1
Derive some avif test images from existing Pillow test images
fdintino Mar 31, 2025
1276543
Updated size
radarhere Mar 31, 2025
c8d0408
Continue to build libyuv on Linux
radarhere Mar 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert "Use "rav1e" if available as default ("auto") avif encoder"
This reverts commit ddc8e7e.
  • Loading branch information
fdintino committed Dec 17, 2024
commit da2e18de3ebf429b1f4a8ad8e0f7134692961f99
2 changes: 1 addition & 1 deletion docs/handbook/image-file-formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options:
YUV range, either "full" or "limited". Defaults to "full"

**codec**
AV1 codec to use for encoding. Specific values are "rav1e", "aom", and
AV1 codec to use for encoding. Specific values are "aom", "rav1e", and
"svt", presuming the chosen codec is available. Defaults to "auto", which
will choose the first available codec in the order of the preceding list.

Expand Down
10 changes: 1 addition & 9 deletions src/_avif.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <Python.h>
#include "avif/avif.h"

static int have_rav1e = 0;

typedef struct {
avifPixelFormat subsampling;
int qmin;
Expand Down Expand Up @@ -41,22 +39,22 @@
static PyTypeObject AvifDecoder_Type;

static int
normalize_quantize_value(int qvalue) {

Check warning on line 42 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L42

Added line #L42 was not covered by tests
if (qvalue < AVIF_QUANTIZER_BEST_QUALITY) {
return AVIF_QUANTIZER_BEST_QUALITY;

Check warning on line 44 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L44

Added line #L44 was not covered by tests
} else if (qvalue > AVIF_QUANTIZER_WORST_QUALITY) {
return AVIF_QUANTIZER_WORST_QUALITY;

Check warning on line 46 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L46

Added line #L46 was not covered by tests
} else {
return qvalue;

Check warning on line 48 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L48

Added line #L48 was not covered by tests
}
}

static int
normalize_tiles_log2(int value) {
if (value < 0) {
return 0;

Check warning on line 55 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L55

Added line #L55 was not covered by tests
} else if (value > 6) {
return 6;

Check warning on line 57 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L57

Added line #L57 was not covered by tests
} else {
return value;
}
Expand All @@ -73,8 +71,8 @@
case AVIF_RESULT_TRUNCATED_DATA:
case AVIF_RESULT_NO_CONTENT:
return PyExc_SyntaxError;
default:
return PyExc_RuntimeError;

Check warning on line 75 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L74-L75

Added lines #L74 - L75 were not covered by tests
}
}

Expand All @@ -93,21 +91,21 @@
return axis ? 7 // 90 degrees anti-clockwise then swap left and right.
: 5; // 90 degrees anti-clockwise then swap top and bottom.
}
return 6; // 90 degrees anti-clockwise.

Check warning on line 94 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L94

Added line #L94 was not covered by tests
}
if (irot && angle == 2) {
if (imir) {
return axis ? 4 // 180 degrees anti-clockwise then swap left and right.
: 2; // 180 degrees anti-clockwise then swap top and bottom.

Check warning on line 99 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L98-L99

Added lines #L98 - L99 were not covered by tests
}
return 3; // 180 degrees anti-clockwise.

Check warning on line 101 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L101

Added line #L101 was not covered by tests
}
if (irot && angle == 3) {
if (imir) {
return axis ? 5 // 270 degrees anti-clockwise then swap left and right.
: 7; // 270 degrees anti-clockwise then swap top and bottom.
}
return 8; // 270 degrees anti-clockwise.

Check warning on line 108 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L108

Added line #L108 was not covered by tests
}
if (imir) {
return axis ? 2 // Swap left and right.
Expand Down Expand Up @@ -136,16 +134,16 @@
image->imir.mode = 1;
#endif
break;
case 3: // The 0th row is at the visual bottom of the image, and the 0th column

Check warning on line 137 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L137

Added line #L137 was not covered by tests
// is the visual right-hand side.
image->transformFlags = otherFlags | AVIF_TRANSFORM_IROT;
image->irot.angle = 2;

Check warning on line 140 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L139-L140

Added lines #L139 - L140 were not covered by tests
#if AVIF_VERSION_MAJOR >= 1
image->imir.axis = 0; // ignored

Check warning on line 142 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L142

Added line #L142 was not covered by tests
#else
image->imir.mode = 0; // ignored
#endif
break;

Check warning on line 146 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L146

Added line #L146 was not covered by tests
case 4: // The 0th row is at the visual bottom of the image, and the 0th column
// is the visual left-hand side.
image->transformFlags = otherFlags | AVIF_TRANSFORM_IMIR;
Expand All @@ -168,16 +166,16 @@
image->imir.mode = 0;
#endif
break;
case 6: // The 0th row is the visual right-hand side of the image, and the 0th

Check warning on line 169 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L169

Added line #L169 was not covered by tests
// column is the visual top.
image->transformFlags = otherFlags | AVIF_TRANSFORM_IROT;
image->irot.angle = 3;

Check warning on line 172 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L171-L172

Added lines #L171 - L172 were not covered by tests
#if AVIF_VERSION_MAJOR >= 1
image->imir.axis = 0; // ignored

Check warning on line 174 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L174

Added line #L174 was not covered by tests
#else
image->imir.mode = 0; // ignored
#endif
break;

Check warning on line 178 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L178

Added line #L178 was not covered by tests
case 7: // The 0th row is the visual right-hand side of the image, and the 0th
// column is the visual bottom.
image->transformFlags =
Expand All @@ -190,16 +188,16 @@
image->imir.mode = 0;
#endif
break;
case 8: // The 0th row is the visual left-hand side of the image, and the 0th

Check warning on line 191 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L191

Added line #L191 was not covered by tests
// column is the visual bottom.
image->transformFlags = otherFlags | AVIF_TRANSFORM_IROT;
image->irot.angle = 1;

Check warning on line 194 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L193-L194

Added lines #L193 - L194 were not covered by tests
#if AVIF_VERSION_MAJOR >= 1
image->imir.axis = 0; // ignored

Check warning on line 196 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L196

Added line #L196 was not covered by tests
#else
image->imir.mode = 0; // ignored
#endif
break;

Check warning on line 200 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L200

Added line #L200 was not covered by tests
}
}

Expand Down Expand Up @@ -239,34 +237,34 @@
PyObject *keyval, *py_key, *py_val;
char *key, *val;
if (!PyTuple_Check(opts)) {
PyErr_SetString(PyExc_ValueError, "Invalid advanced codec options");
return 1;

Check warning on line 241 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L240-L241

Added lines #L240 - L241 were not covered by tests
}
size = PyTuple_GET_SIZE(opts);

for (i = 0; i < size; i++) {
keyval = PyTuple_GetItem(opts, i);
if (!PyTuple_Check(keyval) || PyTuple_GET_SIZE(keyval) != 2) {
PyErr_SetString(PyExc_ValueError, "Invalid advanced codec options");
return 1;

Check warning on line 249 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L248-L249

Added lines #L248 - L249 were not covered by tests
}
py_key = PyTuple_GetItem(keyval, 0);
py_val = PyTuple_GetItem(keyval, 1);
if (!PyBytes_Check(py_key) || !PyBytes_Check(py_val)) {
PyErr_SetString(PyExc_ValueError, "Invalid advanced codec options");
return 1;

Check warning on line 255 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L254-L255

Added lines #L254 - L255 were not covered by tests
}
key = PyBytes_AsString(py_key);
val = PyBytes_AsString(py_val);

avifResult result = avifEncoderSetCodecSpecificOption(encoder, key, val);
if (result != AVIF_RESULT_OK) {
PyErr_Format(
exc_type_for_avif_result(result),

Check warning on line 263 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L262-L263

Added lines #L262 - L263 were not covered by tests
"Setting advanced codec options failed: %s",
avifResultToString(result)

Check warning on line 265 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L265

Added line #L265 was not covered by tests
);
return 1;

Check warning on line 267 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L267

Added line #L267 was not covered by tests
}
}
return 0;
Expand Down Expand Up @@ -348,24 +346,20 @@
enc_options.qmax = normalize_quantize_value(100 - quality);
#endif
} else {
enc_options.qmin = normalize_quantize_value(qmin);
enc_options.qmax = normalize_quantize_value(qmax);

Check warning on line 350 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L349-L350

Added lines #L349 - L350 were not covered by tests
}
enc_options.quality = quality;

if (speed < AVIF_SPEED_SLOWEST) {
speed = AVIF_SPEED_SLOWEST;

Check warning on line 355 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L355

Added line #L355 was not covered by tests
} else if (speed > AVIF_SPEED_FASTEST) {
speed = AVIF_SPEED_FASTEST;

Check warning on line 357 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L357

Added line #L357 was not covered by tests
}
enc_options.speed = speed;

if (strcmp(codec, "auto") == 0) {
if (have_rav1e) {
enc_options.codec = AVIF_CODEC_CHOICE_RAV1E;
} else {
enc_options.codec = AVIF_CODEC_CHOICE_AUTO;
}
enc_options.codec = AVIF_CODEC_CHOICE_AUTO;
} else {
enc_options.codec = avifCodecChoiceFromName(codec);
}
Expand All @@ -381,8 +375,8 @@

// Validate canvas dimensions
if (width <= 0 || height <= 0) {
PyErr_SetString(PyExc_ValueError, "invalid canvas dimensions");
return NULL;

Check warning on line 379 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L378-L379

Added lines #L378 - L379 were not covered by tests
}

enc_options.tile_rows_log2 = normalize_tiles_log2(tile_rows_log2);
Expand Down Expand Up @@ -412,9 +406,9 @@
encoder->maxThreads = is_aom_encode && max_threads > 64 ? 64 : max_threads;
#if AVIF_VERSION >= 1000000
if (enc_options.qmin != -1 && enc_options.qmax != -1) {
encoder->minQuantizer = enc_options.qmin;
encoder->maxQuantizer = enc_options.qmax;
} else {

Check warning on line 411 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L409-L411

Added lines #L409 - L411 were not covered by tests
encoder->quality = enc_options.quality;
}
#else
Expand All @@ -434,7 +428,7 @@
if (advanced != Py_None) {
#if AVIF_VERSION >= 80200
if (_add_codec_specific_options(encoder, advanced)) {
return NULL;

Check warning on line 431 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L431

Added line #L431 was not covered by tests
}
#else
PyErr_SetString(
Expand Down Expand Up @@ -471,12 +465,12 @@
PyBytes_GET_SIZE(icc_bytes)
);
if (result != AVIF_RESULT_OK) {
PyErr_Format(
exc_type_for_avif_result(result),

Check warning on line 469 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L468-L469

Added lines #L468 - L469 were not covered by tests
"Setting ICC profile failed: %s",
avifResultToString(result)

Check warning on line 471 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L471

Added line #L471 was not covered by tests
);
return NULL;

Check warning on line 473 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L473

Added line #L473 was not covered by tests
}
} else {
image->colorPrimaries = AVIF_COLOR_PRIMARIES_BT709;
Expand All @@ -484,21 +478,21 @@
}

if (PyBytes_GET_SIZE(exif_bytes)) {
self->exif_bytes = exif_bytes;
Py_INCREF(exif_bytes);

Check warning on line 482 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L481-L482

Added lines #L481 - L482 were not covered by tests

result = avifImageSetMetadataExif(
image,
(uint8_t *)PyBytes_AS_STRING(exif_bytes),
PyBytes_GET_SIZE(exif_bytes)

Check warning on line 487 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L484-L487

Added lines #L484 - L487 were not covered by tests
);
if (result != AVIF_RESULT_OK) {
PyErr_Format(
exc_type_for_avif_result(result),

Check warning on line 491 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L490-L491

Added lines #L490 - L491 were not covered by tests
"Setting EXIF data failed: %s",
avifResultToString(result)

Check warning on line 493 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L493

Added line #L493 was not covered by tests
);
return NULL;

Check warning on line 495 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L495

Added line #L495 was not covered by tests
}
}
if (PyBytes_GET_SIZE(xmp_bytes)) {
Expand All @@ -511,12 +505,12 @@
PyBytes_GET_SIZE(xmp_bytes)
);
if (result != AVIF_RESULT_OK) {
PyErr_Format(
exc_type_for_avif_result(result),

Check warning on line 509 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L508-L509

Added lines #L508 - L509 were not covered by tests
"Setting XMP data failed: %s",
avifResultToString(result)

Check warning on line 511 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L511

Added line #L511 was not covered by tests
);
return NULL;

Check warning on line 513 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L513

Added line #L513 was not covered by tests
}
}
if (exif_orientation > 1) {
Expand All @@ -528,8 +522,8 @@

return (PyObject *)self;
}
PyErr_SetString(PyExc_RuntimeError, "could not create encoder object");
return NULL;

Check warning on line 526 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L525-L526

Added lines #L525 - L526 were not covered by tests
}

PyObject *
Expand Down Expand Up @@ -576,7 +570,7 @@
&mode,
&is_single_frame
)) {
return NULL;

Check warning on line 573 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L573

Added line #L573 was not covered by tests
}

is_first_frame = (self->frame_index == -1);
Expand Down Expand Up @@ -626,25 +620,25 @@

result = avifRGBImageAllocatePixels(&rgb);
if (result != AVIF_RESULT_OK) {
PyErr_Format(
exc_type_for_avif_result(result),

Check warning on line 624 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L623-L624

Added lines #L623 - L624 were not covered by tests
"Pixel allocation failed: %s",
avifResultToString(result)

Check warning on line 626 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L626

Added line #L626 was not covered by tests
);
return NULL;

Check warning on line 628 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L628

Added line #L628 was not covered by tests
}

if (rgb.rowBytes * rgb.height != size) {
PyErr_Format(
PyExc_RuntimeError,

Check warning on line 633 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L632-L633

Added lines #L632 - L633 were not covered by tests
"rgb data is incorrect size: %u * %u (%u) != %u",
rgb.rowBytes,
rgb.height,
rgb.rowBytes * rgb.height,
size

Check warning on line 638 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L635-L638

Added lines #L635 - L638 were not covered by tests
);
ret = NULL;
goto end;

Check warning on line 641 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L640-L641

Added lines #L640 - L641 were not covered by tests
}

// rgb.pixels is safe for writes
Expand All @@ -654,13 +648,13 @@
Py_END_ALLOW_THREADS

if (result != AVIF_RESULT_OK) {
PyErr_Format(
exc_type_for_avif_result(result),

Check warning on line 652 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L651-L652

Added lines #L651 - L652 were not covered by tests
"Conversion to YUV failed: %s",
avifResultToString(result)

Check warning on line 654 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L654

Added line #L654 was not covered by tests
);
ret = NULL;
goto end;

Check warning on line 657 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L656-L657

Added lines #L656 - L657 were not covered by tests
}

uint32_t addImageFlags = AVIF_ADD_IMAGE_FLAG_NONE;
Expand Down Expand Up @@ -708,13 +702,13 @@
Py_END_ALLOW_THREADS

if (result != AVIF_RESULT_OK) {
PyErr_Format(
exc_type_for_avif_result(result),

Check warning on line 706 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L705-L706

Added lines #L705 - L706 were not covered by tests
"Failed to finish encoding: %s",
avifResultToString(result)

Check warning on line 708 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L708

Added line #L708 was not covered by tests
);
avifRWDataFree(&raw);
return NULL;

Check warning on line 711 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L710-L711

Added lines #L710 - L711 were not covered by tests
}

ret = PyBytes_FromStringAndSize((char *)raw.data, raw.size);
Expand Down Expand Up @@ -748,8 +742,8 @@

self = PyObject_New(AvifDecoderObject, &AvifDecoder_Type);
if (!self) {
PyErr_SetString(PyExc_RuntimeError, "could not create decoder object");
return NULL;

Check warning on line 746 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L745-L746

Added lines #L745 - L746 were not covered by tests
}
self->decoder = NULL;

Expand All @@ -776,15 +770,15 @@
PyBytes_GET_SIZE(self->data)
);
if (result != AVIF_RESULT_OK) {
PyErr_Format(
exc_type_for_avif_result(result),

Check warning on line 774 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L773-L774

Added lines #L773 - L774 were not covered by tests
"Setting IO memory failed: %s",
avifResultToString(result)

Check warning on line 776 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L776

Added line #L776 was not covered by tests
);
avifDecoderDestroy(self->decoder);
self->decoder = NULL;
Py_DECREF(self);
return NULL;

Check warning on line 781 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L778-L781

Added lines #L778 - L781 were not covered by tests
}

result = avifDecoderParse(self->decoder);
Expand Down Expand Up @@ -875,18 +869,18 @@
decoder = self->decoder;

if (!PyArg_ParseTuple(args, "I", &frame_index)) {
return NULL;

Check warning on line 872 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L872

Added line #L872 was not covered by tests
}

result = avifDecoderNthImage(decoder, frame_index);
if (result != AVIF_RESULT_OK) {
PyErr_Format(
exc_type_for_avif_result(result),

Check warning on line 878 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L877-L878

Added lines #L877 - L878 were not covered by tests
"Failed to decode frame %u: %s",
decoder->imageIndex + 1,
avifResultToString(result)

Check warning on line 881 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L880-L881

Added lines #L880 - L881 were not covered by tests
);
return NULL;

Check warning on line 883 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L883

Added line #L883 was not covered by tests
}

image = decoder->image;
Expand All @@ -906,31 +900,31 @@
row_bytes = rgb.width * avifRGBImagePixelSize(&rgb);

if (rgb.height > PY_SSIZE_T_MAX / row_bytes) {
PyErr_SetString(PyExc_MemoryError, "Integer overflow in pixel size");
return NULL;

Check warning on line 904 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L903-L904

Added lines #L903 - L904 were not covered by tests
}

result = avifRGBImageAllocatePixels(&rgb);
if (result != AVIF_RESULT_OK) {
PyErr_Format(
exc_type_for_avif_result(result),

Check warning on line 910 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L909-L910

Added lines #L909 - L910 were not covered by tests
"Pixel allocation failed: %s",
avifResultToString(result)

Check warning on line 912 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L912

Added line #L912 was not covered by tests
);
return NULL;

Check warning on line 914 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L914

Added line #L914 was not covered by tests
}

Py_BEGIN_ALLOW_THREADS result = avifImageYUVToRGB(image, &rgb);
Py_END_ALLOW_THREADS

if (result != AVIF_RESULT_OK) {
PyErr_Format(
exc_type_for_avif_result(result),

Check warning on line 922 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L921-L922

Added lines #L921 - L922 were not covered by tests
"Conversion from YUV failed: %s",
avifResultToString(result)

Check warning on line 924 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L924

Added line #L924 was not covered by tests
);
avifRGBImageFreePixels(&rgb);
return NULL;

Check warning on line 927 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L926-L927

Added lines #L926 - L927 were not covered by tests
}

size = rgb.rowBytes * rgb.height;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this guaranteed to not overflow, even in the face of invalid input?

Copy link
Contributor Author

@fdintino fdintino Jan 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

libavif currently restricts images to a maximum of 2^28 pixels. If the dimensions are larger than 16384x16384 then the function that sets decoder->image->width and decoder->image->height fails. So I suppose that a 4-channel 16384x16384 8-bit image could overflow on a 32-bit platform. I'm not certain because the codecs used by libavif have their own overflow limit checks. For instance, dav1d enforces a maximum of 2^26 pixels on 32-bit systems. Should I add a check against PY_SSIZE_T_MAX to be sure? (edit: answering my own question and adding this check)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added here

Pillow/src/_avif.c

Lines 619 to 622 in b84a8e0

if (rgb.height > PY_SSIZE_T_MAX / row_bytes) {
PyErr_SetString(PyExc_MemoryError, "Integer overflow in pixel size");
return NULL;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, I'm the one who will get a CVE on this if there's a problem, and I'd like really clear guidelines about what the assumptions are for sizes of things and where they come from for dangerous operations like memset, malloc, and pointer reads/writes. This isn't so much for now, but a couple years down the line, things need to be clear. This will be fuzzed, this will be run under valgrind, so hopefully there won't be problems.

I've basically had to reverse engineer how SgiRleDecode works over the last month or so, and I'd like to be preventing that sort of experience in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does raising a MemoryError if rgb.height > PY_SSIZE_T_MAX / row_bytes (as I have in the latest PR push) suffice to address that concern?

Expand Down Expand Up @@ -1003,7 +997,7 @@
static int
setup_module(PyObject *m) {
if (PyType_Ready(&AvifDecoder_Type) < 0 || PyType_Ready(&AvifEncoder_Type) < 0) {
return -1;

Check warning on line 1000 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L1000

Added line #L1000 was not covered by tests
}

PyObject *d = PyModule_GetDict(m);
Expand All @@ -1011,8 +1005,6 @@
PyDict_SetItemString(d, "libavif_version", v ? v : Py_None);
Py_XDECREF(v);

have_rav1e = _codec_available("rav1e", AVIF_CODEC_FLAG_CAN_ENCODE);

return 0;
}

Expand All @@ -1029,8 +1021,8 @@

m = PyModule_Create(&module_def);
if (setup_module(m) < 0) {
Py_DECREF(m);
return NULL;

Check warning on line 1025 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L1024-L1025

Added lines #L1024 - L1025 were not covered by tests
}

return m;
Expand Down
Loading