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
Simplified EXIF code (#12)
* Use break in switch

* Use walrus operator

* Do not add irot and imir flags if orientation is default

* Do not potentially call Exif tobytes() twice

* Simplified code by only setting info["exif"] once

---------

Co-authored-by: Andrew Murray <radarhere@users.noreply.github.com>
  • Loading branch information
radarhere and radarhere authored Dec 15, 2024
commit b585f9e560c9ed07ee0abd5ac064da9bf5551c7c
35 changes: 16 additions & 19 deletions src/PIL/AvifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@

def _get_default_max_threads():
if DEFAULT_MAX_THREADS:
return DEFAULT_MAX_THREADS

Check warning on line 53 in src/PIL/AvifImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/AvifImagePlugin.py#L53

Added line #L53 was not covered by tests
if hasattr(os, "sched_getaffinity"):
return len(os.sched_getaffinity(0))

Check warning on line 55 in src/PIL/AvifImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/AvifImagePlugin.py#L55

Added line #L55 was not covered by tests
else:
return os.cpu_count() or 1

Expand Down Expand Up @@ -96,20 +96,21 @@

if icc:
self.info["icc_profile"] = icc
if exif:
self.info["exif"] = exif
if xmp:
self.info["xmp"] = xmp

if exif_orientation != 1 or exif is not None:
if exif_orientation != 1 or exif:
exif_data = Image.Exif()
orig_orientation = 1
if exif is not None:
if exif:
exif_data.load(exif)
orig_orientation = exif_data.get(ExifTags.Base.Orientation, 1)
if exif_orientation != orig_orientation:
original_orientation = exif_data.get(ExifTags.Base.Orientation, 1)
else:
original_orientation = 1
if exif_orientation != original_orientation:
exif_data[ExifTags.Base.Orientation] = exif_orientation
self.info["exif"] = exif_data.tobytes()
exif = exif_data.tobytes()
if exif:
self.info["exif"] = exif

def seek(self, frame: int) -> None:
if not self._seek_check(frame):
Expand Down Expand Up @@ -180,22 +181,18 @@
autotiling = bool(info.get("autotiling", tile_rows_log2 == tile_cols_log2 == 0))

icc_profile = info.get("icc_profile", im.info.get("icc_profile"))
exif = info.get("exif")
if exif:
exif_orientation = 1
if exif := info.get("exif"):
if isinstance(exif, Image.Exif):
exif_data = exif
exif = exif.tobytes()
else:
exif_data = Image.Exif()
exif_data.load(exif)
exif_orientation = exif_data.pop(ExifTags.Base.Orientation, 0)
if exif_orientation != 0:
if len(exif_data):
exif = exif_data.tobytes()
else:
exif = None
else:
exif_orientation = 0
if ExifTags.Base.Orientation in exif_data:
exif_orientation = exif_data.pop(ExifTags.Base.Orientation)
exif = exif_data.tobytes() if exif_data else b""
elif isinstance(exif, Image.Exif):
exif = exif_data.tobytes()

Check warning on line 195 in src/PIL/AvifImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/AvifImagePlugin.py#L194-L195

Added lines #L194 - L195 were not covered by tests

xmp = info.get("xmp")

Expand Down
26 changes: 8 additions & 18 deletions src/_avif.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@
static PyTypeObject AvifDecoder_Type;

static int
normalize_quantize_value(int qvalue) {

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
if (qvalue < AVIF_QUANTIZER_BEST_QUALITY) {
return AVIF_QUANTIZER_BEST_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 if (qvalue > AVIF_QUANTIZER_WORST_QUALITY) {
return AVIF_QUANTIZER_WORST_QUALITY;

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
} else {
return qvalue;

Check warning on line 50 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L50

Added line #L50 was not covered by tests
}
}

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

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 if (value > 6) {
return 6;

Check warning on line 59 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L59

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

Check warning on line 77 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L76-L77

Added lines #L76 - L77 were not covered by tests
}
}

Expand All @@ -93,21 +93,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 96 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L96

Added line #L96 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 101 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L100-L101

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

Check warning on line 103 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L103

Added line #L103 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 110 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L110

Added line #L110 was not covered by tests
}
if (imir) {
return axis ? 2 // Swap left and right.
Expand All @@ -126,16 +126,6 @@
// Orientation to irot and imir boxes as defined in HEIF ISO/IEC 28002-12:2021
// sections 6.5.10 and 6.5.12.
switch (orientation) {
case 1: // The 0th row is at the visual top of the image, and the 0th column is
// the visual left-hand side.
image->transformFlags = otherFlags;
image->irot.angle = 0; // ignored
#if AVIF_VERSION_MAJOR >= 1
image->imir.axis = 0; // ignored
#else
image->imir.mode = 0; // ignored
#endif
return;
case 2: // The 0th row is at the visual top of the image, and the 0th column is
// the visual right-hand side.
image->transformFlags = otherFlags | AVIF_TRANSFORM_IMIR;
Expand All @@ -145,17 +135,17 @@
#else
image->imir.mode = 1;
#endif
return;
break;
case 3: // The 0th row is at the visual bottom of the image, and the 0th column

Check warning on line 139 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L139

Added line #L139 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 142 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L141-L142

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

Check warning on line 144 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L144

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

Check warning on line 148 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L148

Added line #L148 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 @@ -165,7 +155,7 @@
#else
image->imir.mode = 0;
#endif
return;
break;
case 5: // The 0th row is the visual left-hand side of the image, and the 0th
// column is the visual top.
image->transformFlags =
Expand All @@ -177,17 +167,17 @@
#else
image->imir.mode = 0;
#endif
return;
break;
case 6: // The 0th row is the visual right-hand side of the image, and the 0th

Check warning on line 171 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L171

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

Check warning on line 174 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L173-L174

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

Check warning on line 176 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L176

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

Check warning on line 180 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L180

Added line #L180 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 @@ -199,17 +189,17 @@
#else
image->imir.mode = 0;
#endif
return;
break;
case 8: // The 0th row is the visual left-hand side of the image, and the 0th

Check warning on line 193 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L193

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

Check warning on line 196 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L195-L196

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

Check warning on line 198 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L198

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

Check warning on line 202 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L202

Added line #L202 was not covered by tests
}
}

Expand Down Expand Up @@ -249,34 +239,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 243 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L242-L243

Added lines #L242 - L243 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 251 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L250-L251

Added lines #L250 - L251 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 257 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L256-L257

Added lines #L256 - L257 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 265 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L264-L265

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

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 1;

Check warning on line 269 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L269

Added line #L269 was not covered by tests
}
}
return 0;
Expand Down Expand Up @@ -358,15 +348,15 @@
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 352 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L351-L352

Added lines #L351 - L352 were not covered by tests
}
enc_options.quality = quality;

if (speed < AVIF_SPEED_SLOWEST) {
speed = AVIF_SPEED_SLOWEST;

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
} else if (speed > AVIF_SPEED_FASTEST) {
speed = AVIF_SPEED_FASTEST;

Check warning on line 359 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L359

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

Expand All @@ -391,8 +381,8 @@

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

Check warning on line 385 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L384-L385

Added lines #L384 - L385 were not covered by tests
}

enc_options.tile_rows_log2 = normalize_tiles_log2(tile_rows_log2);
Expand Down Expand Up @@ -422,9 +412,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 417 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L415-L417

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

Check warning on line 437 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L437

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

Check warning on line 475 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L474-L475

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

Check warning on line 477 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L477

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

Check warning on line 479 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L479

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

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

Check warning on line 488 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L487-L488

Added lines #L487 - L488 were not covered by tests

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

Check warning on line 493 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L490-L493

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

Check warning on line 497 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L496-L497

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

Check warning on line 499 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L499

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

Check warning on line 501 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L501

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

Check warning on line 515 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L514-L515

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

Check warning on line 517 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L517

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

Check warning on line 519 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L519

Added line #L519 was not covered by tests
}
}
if (exif_orientation > 0) {
if (exif_orientation > 1) {
exif_orientation_to_irot_imir(image, exif_orientation);
}

Expand All @@ -538,8 +528,8 @@

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

Check warning on line 532 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L531-L532

Added lines #L531 - L532 were not covered by tests
}

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

Check warning on line 579 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L579

Added line #L579 was not covered by tests
}

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

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

Check warning on line 630 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L629-L630

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

Check warning on line 632 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L632

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

Check warning on line 634 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L634

Added line #L634 was not covered by tests
}

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

Check warning on line 639 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L638-L639

Added lines #L638 - L639 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 644 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L641-L644

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

Check warning on line 647 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L646-L647

Added lines #L646 - L647 were not covered by tests
}

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

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

Check warning on line 658 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L657-L658

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

Check warning on line 660 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L660

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

Check warning on line 663 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L662-L663

Added lines #L662 - L663 were not covered by tests
}

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

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

Check warning on line 712 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L711-L712

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

Check warning on line 714 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L714

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

Check warning on line 717 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L716-L717

Added lines #L716 - L717 were not covered by tests
}

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

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

Check warning on line 752 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L751-L752

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

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

Check warning on line 780 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L779-L780

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

Check warning on line 782 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L782

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

Check warning on line 787 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L784-L787

Added lines #L784 - L787 were not covered by tests
}

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

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

Check warning on line 878 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L878

Added line #L878 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 884 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L883-L884

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

Check warning on line 887 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L886-L887

Added lines #L886 - L887 were not covered by tests
);
return NULL;

Check warning on line 889 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L889

Added line #L889 was not covered by tests
}

image = decoder->image;
Expand All @@ -916,31 +906,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 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
}

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

Check warning on line 916 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L915-L916

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

Check warning on line 918 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L918

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

Check warning on line 920 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L920

Added line #L920 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 928 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L927-L928

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

Check warning on line 930 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L930

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

Check warning on line 933 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L932-L933

Added lines #L932 - L933 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 @@ -1013,7 +1003,7 @@
static int
setup_module(PyObject *m) {
if (PyType_Ready(&AvifDecoder_Type) < 0 || PyType_Ready(&AvifEncoder_Type) < 0) {
return -1;

Check warning on line 1006 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L1006

Added line #L1006 was not covered by tests
}

PyObject *d = PyModule_GetDict(m);
Expand All @@ -1039,8 +1029,8 @@

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

Check warning on line 1033 in src/_avif.c

View check run for this annotation

Codecov / codecov/patch

src/_avif.c#L1032-L1033

Added lines #L1032 - L1033 were not covered by tests
}

return m;
Expand Down
Loading