Skip to content

Add AVIF to wheels using only aomenc and dav1d AVIF codecs for reduced size #8858

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

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8b4e66e
fix(avif): use only aomenc and dav1d to reduce wheel size
fdintino Apr 1, 2025
a2cbce7
Include comment about CONFIG_AV1_DECODER=0
fdintino Apr 2, 2025
84485e1
s/AVIF/AV1/
fdintino Apr 2, 2025
ab55e54
remove ENABLE_NASM and CMAKE_MODULE_PATH libavif cmake args
fdintino Apr 2, 2025
8a6e5af
Remove rust installation via rustup, which is no longer needed
fdintino Apr 3, 2025
deb1c9d
Build libavif with -Os and with LTO enabled
fdintino Apr 4, 2025
6d7f396
Do not install perl (#35)
radarhere Apr 6, 2025
7aca46c
Do not disable CMAKE_MACOSX_RPATH (#36)
radarhere Apr 6, 2025
8bc14e2
Update .github/workflows/wheels-dependencies.sh
fdintino Apr 7, 2025
621bcd5
Removed CMAKE_INTERPROCEDURAL_OPTIMIZATION (#37)
radarhere Apr 8, 2025
1c40ea5
Re-add CMAKE_INTERPROCEDURAL_OPTIMIZATION=ON on windows
fdintino Apr 8, 2025
331354a
only disable CMAKE_INTERPROCEDURAL_OPTIMIZATION on macOS
fdintino Apr 8, 2025
8cac43f
Use elif
radarhere Apr 8, 2025
ce01f19
Merge branch 'main' into chore/remove-rav1e
radarhere Apr 13, 2025
8ab93db
Disable AOM high-bit-depth support
fdintino Apr 17, 2025
a005e28
Use CMAKE_BUILD_TYPE=MinSizeRel on aarch64 manylinux2014
fdintino Apr 17, 2025
6c918a0
use alternative mirror for nasm, since nasm.us is down
fdintino Apr 23, 2025
5573242
Further reduce size (LTO on macOS, more aggressive symbol stripping)
fdintino Apr 23, 2025
8a174f5
Revert "use alternative mirror for nasm, since nasm.us is down"
fdintino Apr 24, 2025
f40eed2
Merge branch 'main' into chore/remove-rav1e
radarhere May 1, 2025
8905480
Set CMAKE_POLICY_VERSION_MINIMUM=3.9 to allow IPO with gcc
fdintino May 1, 2025
f20ce67
bump libavif to 1.3.0
fdintino May 12, 2025
5a8313c
Remove -DCMAKE_POLICY_VERSION_MINIMUM=3.5 from build_prepare
fdintino May 14, 2025
a3cba6a
Merge branch 'main' into chore/remove-rav1e
radarhere May 23, 2025
01daa3d
Merge branch 'main' into chore/remove-rav1e
radarhere May 27, 2025
3ffce33
Merge branch 'main' into chore/remove-rav1e
radarhere Jun 14, 2025
7607474
Simplified code
radarhere Jun 14, 2025
2222237
Include in Windows x86 wheels (#38)
radarhere Jun 20, 2025
1cabcba
Merge branch 'main' into chore/remove-rav1e
radarhere Jun 20, 2025
c993fbc
Windows arm64 wheels do not have libavif
radarhere Jun 22, 2025
7f44ca6
Removed trailing space
radarhere Jun 23, 2025
124c1a5
Updated capitalisation
radarhere Jun 24, 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
55 changes: 55 additions & 0 deletions .github/workflows/wheels-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ LIBWEBP_VERSION=1.5.0
BZIP2_VERSION=1.0.8
LIBXCB_VERSION=1.17.0
BROTLI_VERSION=1.1.0
LIBAVIF_VERSION=1.3.0

function build_pkg_config {
if [ -e pkg-config-stamp ]; then return; fi
Expand Down Expand Up @@ -98,6 +99,59 @@ function build_harfbuzz {
touch harfbuzz-stamp
}

function build_libavif {
if [ -e libavif-stamp ]; then return; fi

python3 -m pip install meson ninja

if [[ "$PLAT" == "x86_64" ]] || [ -n "$SANITIZER" ]; then
build_simple nasm 2.16.03 https://www.nasm.us/pub/nasm/releasebuilds/2.16.03
fi

local build_type=MinSizeRel
local lto=ON

local libavif_cmake_flags

if [ -n "$IS_MACOS" ]; then
lto=OFF
libavif_cmake_flags=(
-DCMAKE_C_FLAGS_MINSIZEREL="-Oz -DNDEBUG -flto" \
-DCMAKE_CXX_FLAGS_MINSIZEREL="-Oz -DNDEBUG -flto" \
-DCMAKE_SHARED_LINKER_FLAGS_INIT="-Wl,-S,-x,-dead_strip_dylibs" \
)
else
if [[ "$MB_ML_VER" == 2014 ]] && [[ "$PLAT" == "x86_64" ]]; then
build_type=Release
fi
libavif_cmake_flags=(-DCMAKE_SHARED_LINKER_FLAGS_INIT="-Wl,--strip-all,-z,relro,-z,now")
fi

local out_dir=$(fetch_unpack https://github.com/AOMediaCodec/libavif/archive/refs/tags/v$LIBAVIF_VERSION.tar.gz libavif-$LIBAVIF_VERSION.tar.gz)
# CONFIG_AV1_HIGHBITDEPTH=0 is a flag for libaom (included as a subproject
# of libavif) that disables support for encoding high bit depth images.
(cd $out_dir \
&& cmake \
-DCMAKE_INSTALL_PREFIX=$BUILD_PREFIX \
-DCMAKE_INSTALL_LIBDIR=$BUILD_PREFIX/lib \
-DCMAKE_INSTALL_NAME_DIR=$BUILD_PREFIX/lib \
-DBUILD_SHARED_LIBS=ON \

Choose a reason for hiding this comment

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

Did you mean to change BUILD_SHARED_LIBS to ON? It is still OFF in winbuild/build_prepare.py.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did. Using the linker to generate the shared library file allows it to remove unused objects. There is a mechanism for including shared library dependencies of a python extension inside a wheel file on macOS and linux (delocate and auditwheel, respectively) but there is no such standard mechanism for doing the same on windows. So the windows library has to be built statically.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The savings are modest, about 0.5 MB, but there isn't any cost to switching to a shared library.

Copy link
Member

Choose a reason for hiding this comment

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

When you say 0.5mb, I presume that's per wheel. Which wheel(s) were you using to check that?

-DAVIF_LIBSHARPYUV=LOCAL \
-DAVIF_LIBYUV=LOCAL \
-DAVIF_CODEC_AOM=LOCAL \
-DCONFIG_AV1_HIGHBITDEPTH=0 \
-DAVIF_CODEC_AOM_DECODE=OFF \
Copy link

@wantehchang wantehchang Apr 7, 2025

Choose a reason for hiding this comment

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

Frankie: Is it possible to build with the libaom and dav1d packages in Linux distributions (i.e., -DAVIF_CODEC_AOM=SYSTEM -DAVIF_CODEC_DAV1D=LOCAL)? That will allow the Pillow Library to share the libaom and dav1d shared libraries with other applications on the system.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The shared libraries linked from the pillow extension are all included in the distributed binary, so that people installing Pillow don't need to install the libraries themselves. This is why libjpeg-turbo, libpng, libtiff, and other libraries are included in the wheel files on PyPI. Nothing would be gained by using libaom from the linux distribution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If someone were to build Pillow from source, they could use libavif from the distro, or build libavif against system libraries. The binary wheels are tagged for python version, system architecture, and ABI compatibility (musl and two different versions of glibc). They are intended to be installed in any compatible environment without any other dependencies.

-DAVIF_CODEC_DAV1D=LOCAL \
-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=$lto \
-DCMAKE_C_VISIBILITY_PRESET=hidden \
-DCMAKE_CXX_VISIBILITY_PRESET=hidden \
-DCMAKE_BUILD_TYPE=$build_type \
"${libavif_cmake_flags[@]}" \
. \
&& make install)
touch libavif-stamp
}

function build {
build_xz
if [ -z "$IS_ALPINE" ] && [ -z "$SANITIZER" ] && [ -z "$IS_MACOS" ]; then
Expand Down Expand Up @@ -132,6 +186,7 @@ function build {
build_tiff
fi

build_libavif
build_libpng
build_lcms2
build_openjpeg
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ jobs:
# Install extra test images
xcopy /S /Y Tests\test-images\* Tests\images

& python.exe winbuild\build_prepare.py -v --no-imagequant --no-avif --architecture=${{ matrix.cibw_arch }}
& python.exe winbuild\build_prepare.py -v --no-imagequant --architecture=${{ matrix.cibw_arch }}
shell: pwsh

- name: Build wheels
Expand Down
6 changes: 5 additions & 1 deletion Tests/check_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def test_wheel_modules() -> None:
expected_modules = {"pil", "tkinter", "freetype2", "littlecms2", "webp"}
expected_modules = {"pil", "tkinter", "freetype2", "littlecms2", "webp", "avif"}

if sys.platform == "win32":
# tkinter is not available in cibuildwheel installed CPython on Windows
Expand All @@ -20,6 +20,10 @@ def test_wheel_modules() -> None:
except ImportError:
expected_modules.remove("tkinter")

# libavif is not available on Windows for ARM64 architectures
if platform.machine() == "ARM64":
expected_modules.remove("avif")

assert set(features.get_supported_modules()) == expected_modules


Expand Down
6 changes: 6 additions & 0 deletions docs/releasenotes/11.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ Pillow only supports libavif 1.0.0 or later. In order to prevent errors when bui
from source, if a user happens to have an earlier libavif on their system, Pillow will
now ignore it.

AVIF support in wheels
^^^^^^^^^^^^^^^^^^^^^^

Support for reading and writing AVIF images is now included in Pillow's wheels, except
for Windows ARM64. libaom is available as an encoder and dav1d as a decoder.

Python 3.14 beta
^^^^^^^^^^^^^^^^

Expand Down
26 changes: 26 additions & 0 deletions wheels/dependency_licenses/AOM.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2016, Alliance for Open Media. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
23 changes: 23 additions & 0 deletions wheels/dependency_licenses/DAV1D.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Copyright © 2018-2019, VideoLAN and dav1d authors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Loading
Loading