diff --git a/.ci/install.sh b/.ci/install.sh index 4748feb3d49..e26be66d7c7 100755 --- a/.ci/install.sh +++ b/.ci/install.sh @@ -23,7 +23,8 @@ if [[ $(uname) != CYGWIN* ]]; then sudo apt-get -qq install libfreetype6-dev liblcms2-dev python3-tk\ ghostscript libffi-dev libjpeg-turbo-progs libopenjp2-7-dev\ cmake meson imagemagick libharfbuzz-dev libfribidi-dev\ - sway wl-clipboard libopenblas-dev + sway wl-clipboard libopenblas-dev\ + ninja-build build-essential nasm fi python3 -m pip install --upgrade pip @@ -55,8 +56,14 @@ if [[ $(uname) != CYGWIN* ]]; then # raqm pushd depends && ./install_raqm.sh && popd + # libavif + pushd depends && ./install_libavif.sh && popd + # extra test images pushd depends && ./install_extra_test_images.sh && popd else + # libavif + cd depends && ./install_libavif.sh && cd .. + cd depends && ./install_extra_test_images.sh && cd .. fi diff --git a/.github/workflows/macos-install.sh b/.github/workflows/macos-install.sh index a20838a1507..374bbdc7edf 100755 --- a/.github/workflows/macos-install.sh +++ b/.github/workflows/macos-install.sh @@ -2,7 +2,7 @@ set -e -brew install libtiff libjpeg openjpeg libimagequant webp little-cms2 freetype libraqm +brew install libtiff libjpeg openjpeg libimagequant webp little-cms2 freetype openblas libraqm dav1d aom rav1e export PKG_CONFIG_PATH="/usr/local/opt/openblas/lib/pkgconfig" PYTHONOPTIMIZE=0 python3 -m pip install cffi @@ -16,5 +16,8 @@ python3 -m pip install pyroma python3 -m pip install numpy +# libavif +pushd depends && ./install_libavif.sh && popd + # extra test images pushd depends && ./install_extra_test_images.sh && popd diff --git a/.github/workflows/test-cygwin.yml b/.github/workflows/test-cygwin.yml index 5071e5bd442..1cbeff39a99 100644 --- a/.github/workflows/test-cygwin.yml +++ b/.github/workflows/test-cygwin.yml @@ -47,6 +47,7 @@ jobs: ghostscript ImageMagick jpeg + libaom-devel libfreetype-devel libimagequant-devel libjpeg-devel @@ -58,8 +59,11 @@ jobs: libwebp-devel libxcb-devel libxcb-xinerama0 + cmake make + sudo netpbm + patch perl python3${{ matrix.python-minor-version }}-cffi python3${{ matrix.python-minor-version }}-cython diff --git a/.github/workflows/test-mingw.yml b/.github/workflows/test-mingw.yml index 08dfb9a2da0..8b657019ea7 100644 --- a/.github/workflows/test-mingw.yml +++ b/.github/workflows/test-mingw.yml @@ -50,6 +50,7 @@ jobs: mingw-w64-x86_64-libimagequant \ mingw-w64-x86_64-libjpeg-turbo \ mingw-w64-x86_64-libraqm \ + mingw-w64-x86_64-libavif \ mingw-w64-x86_64-libtiff \ mingw-w64-x86_64-libwebp \ mingw-w64-x86_64-openjpeg2 \ diff --git a/.github/workflows/test-windows.yml b/.github/workflows/test-windows.yml index ae3cc6127da..2213c03769e 100644 --- a/.github/workflows/test-windows.yml +++ b/.github/workflows/test-windows.yml @@ -66,6 +66,8 @@ jobs: 7z x winbuild\depends\nasm-2.16.01-win64.zip "-o$env:RUNNER_WORKSPACE\" echo "$env:RUNNER_WORKSPACE\nasm-2.16.01" >> $env:GITHUB_PATH + python -m pip install meson + choco install ghostscript --version=10.0.0.20230317 echo "C:\Program Files\gs\gs10.00.0\bin" >> $env:GITHUB_PATH @@ -149,6 +151,10 @@ jobs: if: steps.build-cache.outputs.cache-hit != 'true' run: "& winbuild\\build\\build_dep_fribidi.cmd" + - name: Build dependencies / libavif + if: steps.build-cache.outputs.cache-hit != 'true' + run: "& winbuild\\build\\build_dep_libavif.cmd" + # trim ~150MB for each job - name: Optimize build cache if: steps.build-cache.outputs.cache-hit != 'true' diff --git a/Tests/check_avif_leaks.py b/Tests/check_avif_leaks.py new file mode 100644 index 00000000000..57818efcbee --- /dev/null +++ b/Tests/check_avif_leaks.py @@ -0,0 +1,42 @@ +from io import BytesIO + +import pytest + +from PIL import Image + +from .helper import is_win32, skip_unless_feature + +# Limits for testing the leak +mem_limit = 1024 * 1048576 +stack_size = 8 * 1048576 +iterations = int((mem_limit / stack_size) * 2) +test_file = "Tests/images/avif/hopper.avif" + +pytestmark = [ + pytest.mark.skipif(is_win32(), reason="requires Unix or macOS"), + skip_unless_feature("avif"), +] + + +def test_leak_load(): + from resource import RLIMIT_AS, RLIMIT_STACK, setrlimit + + setrlimit(RLIMIT_STACK, (stack_size, stack_size)) + setrlimit(RLIMIT_AS, (mem_limit, mem_limit)) + for _ in range(iterations): + with Image.open(test_file) as im: + im.load() + + +def test_leak_save(): + from resource import RLIMIT_AS, RLIMIT_STACK, setrlimit + + setrlimit(RLIMIT_STACK, (stack_size, stack_size)) + setrlimit(RLIMIT_AS, (mem_limit, mem_limit)) + for _ in range(iterations): + with Image.open(test_file) as im: + im.load() + test_output = BytesIO() + im.save(test_output, "AVIF") + test_output.seek(0) + test_output.read() diff --git a/Tests/helper.py b/Tests/helper.py index 69246bfcf45..6b18c74c62c 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -2,6 +2,7 @@ Helper functions. """ +import gc import logging import os import shutil @@ -218,6 +219,7 @@ def _test_leak(self, core): start_mem = self._get_mem_usage() for cycle in range(self.iterations): core() + gc.collect() mem = self._get_mem_usage() - start_mem msg = f"memory usage limit exceeded in iteration {cycle}" assert mem < self.mem_limit, msg diff --git a/Tests/images/avif/exif.avif b/Tests/images/avif/exif.avif new file mode 100644 index 00000000000..07964487f3c Binary files /dev/null and b/Tests/images/avif/exif.avif differ diff --git a/Tests/images/avif/hopper.avif b/Tests/images/avif/hopper.avif new file mode 100644 index 00000000000..87e4394f059 Binary files /dev/null and b/Tests/images/avif/hopper.avif differ diff --git a/Tests/images/avif/hopper_avif_write.png b/Tests/images/avif/hopper_avif_write.png new file mode 100644 index 00000000000..a47a0562bbc Binary files /dev/null and b/Tests/images/avif/hopper_avif_write.png differ diff --git a/Tests/images/avif/icc_profile.avif b/Tests/images/avif/icc_profile.avif new file mode 100644 index 00000000000..658cfec176e Binary files /dev/null and b/Tests/images/avif/icc_profile.avif differ diff --git a/Tests/images/avif/icc_profile_none.avif b/Tests/images/avif/icc_profile_none.avif new file mode 100644 index 00000000000..c73e70a3a52 Binary files /dev/null and b/Tests/images/avif/icc_profile_none.avif differ diff --git a/Tests/images/avif/rgba10.heif b/Tests/images/avif/rgba10.heif new file mode 100644 index 00000000000..8429a8b01ea Binary files /dev/null and b/Tests/images/avif/rgba10.heif differ diff --git a/Tests/images/avif/star.avifs b/Tests/images/avif/star.avifs new file mode 100644 index 00000000000..bb9dfa5c33d Binary files /dev/null and b/Tests/images/avif/star.avifs differ diff --git a/Tests/images/avif/star.gif b/Tests/images/avif/star.gif new file mode 100644 index 00000000000..52076cafdd8 Binary files /dev/null and b/Tests/images/avif/star.gif differ diff --git a/Tests/images/avif/star.png b/Tests/images/avif/star.png new file mode 100644 index 00000000000..468dcde005d Binary files /dev/null and b/Tests/images/avif/star.png differ diff --git a/Tests/images/avif/star180.png b/Tests/images/avif/star180.png new file mode 100644 index 00000000000..2c5f5222115 Binary files /dev/null and b/Tests/images/avif/star180.png differ diff --git a/Tests/images/avif/star270.png b/Tests/images/avif/star270.png new file mode 100644 index 00000000000..8812b9bdeb8 Binary files /dev/null and b/Tests/images/avif/star270.png differ diff --git a/Tests/images/avif/star90.png b/Tests/images/avif/star90.png new file mode 100644 index 00000000000..93526260bab Binary files /dev/null and b/Tests/images/avif/star90.png differ diff --git a/Tests/images/avif/transparency.avif b/Tests/images/avif/transparency.avif new file mode 100644 index 00000000000..f808357fc7c Binary files /dev/null and b/Tests/images/avif/transparency.avif differ diff --git a/Tests/images/avif/xmp_tags_orientation.avif b/Tests/images/avif/xmp_tags_orientation.avif new file mode 100644 index 00000000000..41faa60501b Binary files /dev/null and b/Tests/images/avif/xmp_tags_orientation.avif differ diff --git a/Tests/test_file_avif.py b/Tests/test_file_avif.py new file mode 100644 index 00000000000..070d862a495 --- /dev/null +++ b/Tests/test_file_avif.py @@ -0,0 +1,782 @@ +import os +import re +import xml.etree.ElementTree +from contextlib import contextmanager +from io import BytesIO +from struct import unpack +from unittest import mock + +import pytest + +from PIL import AvifImagePlugin, Image, UnidentifiedImageError, features + +from .helper import ( + PillowLeakTestCase, + assert_image, + assert_image_similar, + assert_image_similar_tofile, + hopper, + skip_unless_feature, +) + +try: + from PIL import _avif +except ImportError: + _avif = None + + +TEST_AVIF_FILE = "Tests/images/avif/hopper.avif" + + +def assert_xmp_orientation(xmp, expected): + assert isinstance(xmp, bytes) + root = xml.etree.ElementTree.fromstring(xmp) + orientation = None + for elem in root.iter(): + if elem.tag.endswith("}Description"): + orientation = elem.attrib.get("{http://ns.adobe.com/tiff/1.0/}Orientation") + if orientation: + orientation = int(orientation) + break + assert orientation == expected + + +def roundtrip(im, **options): + out = BytesIO() + im.save(out, "AVIF", **options) + out.seek(0) + return Image.open(out) + + +def skip_unless_avif_decoder(codec_name): + reason = f"{codec_name} decode not available" + return pytest.mark.skipif( + not _avif or not _avif.decoder_codec_available(codec_name), reason=reason + ) + + +def skip_unless_avif_encoder(codec_name): + reason = f"{codec_name} encode not available" + return pytest.mark.skipif( + not _avif or not _avif.encoder_codec_available(codec_name), reason=reason + ) + + +def is_docker_qemu(): + try: + init_proc_exe = os.readlink("/proc/1/exe") + except: # noqa: E722 + return False + else: + return "qemu" in init_proc_exe + + +def skip_unless_avif_version_gte(version): + if not _avif: + reason = "AVIF unavailable" + should_skip = True + else: + version_str = ".".join([str(v) for v in version]) + reason = "%s < %s" % (_avif.libavif_version, version_str) + should_skip = _avif.VERSION < version + return pytest.mark.skipif(should_skip, reason=reason) + + +def has_alpha_premultiplied(im_bytes): + stream = BytesIO(im_bytes) + length = len(im_bytes) + while stream.tell() < length: + start = stream.tell() + size, boxtype = unpack(">L4s", stream.read(8)) + if not all(0x20 <= c <= 0x7E for c in boxtype): + # Not ascii + return False + if size == 1: # 64bit size + (size,) = unpack(">Q", stream.read(8)) + end = start + size + version, _ = unpack(">B3s", stream.read(4)) + if boxtype in (b"ftyp", b"hdlr", b"pitm", b"iloc", b"iinf"): + # Skip these boxes + stream.seek(end) + continue + elif boxtype == b"meta": + # Container box possibly including iref prem, continue to parse boxes + # inside it + continue + elif boxtype == b"iref": + while stream.tell() < end: + _, iref_type = unpack(">L4s", stream.read(8)) + version, _ = unpack(">B3s", stream.read(4)) + if iref_type == b"prem": + return True + stream.read(2 if version == 0 else 4) + else: + return False + return False + + +class TestUnsupportedAvif: + def test_unsupported(self): + if features.check("avif"): + AvifImagePlugin.SUPPORTED = False + + try: + file_path = "Tests/images/avif/hopper.avif" + pytest.warns( + UserWarning, + lambda: pytest.raises(UnidentifiedImageError, Image.open, file_path), + ) + finally: + AvifImagePlugin.SUPPORTED = features.check("avif") + + +@skip_unless_feature("avif") +class TestFileAvif: + def test_version(self): + _avif.AvifCodecVersions() + assert re.search(r"\d+\.\d+\.\d+$", features.version_module("avif")) + + def test_read(self): + """ + Can we read an AVIF file without error? + Does it have the bits we expect? + """ + + with Image.open("Tests/images/avif/hopper.avif") as image: + assert image.mode == "RGB" + assert image.size == (128, 128) + assert image.format == "AVIF" + assert image.get_format_mimetype() == "image/avif" + image.load() + image.getdata() + + # generated with: + # avifdec hopper.avif hopper_avif_write.png + assert_image_similar_tofile( + image, "Tests/images/avif/hopper_avif_write.png", 12.0 + ) + + def _roundtrip(self, tmp_path, mode, epsilon, args={}): + temp_file = str(tmp_path / "temp.avif") + + hopper(mode).save(temp_file, **args) + with Image.open(temp_file) as image: + assert image.mode == "RGB" + assert image.size == (128, 128) + assert image.format == "AVIF" + image.load() + image.getdata() + + if mode == "RGB": + # avifdec hopper.avif avif/hopper_avif_write.png + assert_image_similar_tofile( + image, "Tests/images/avif/hopper_avif_write.png", 12.0 + ) + + # This test asserts that the images are similar. If the average pixel + # difference between the two images is less than the epsilon value, + # then we're going to accept that it's a reasonable lossy version of + # the image. + target = hopper(mode) + if mode != "RGB": + target = target.convert("RGB") + assert_image_similar(image, target, epsilon) + + def test_write_rgb(self, tmp_path): + """ + Can we write a RGB mode file to avif without error? + Does it have the bits we expect? + """ + + self._roundtrip(tmp_path, "RGB", 12.5) + + def test_AvifEncoder_with_invalid_args(self): + """ + Calling encoder functions with no arguments should result in an error. + """ + with pytest.raises(TypeError): + _avif.AvifEncoder() + + def test_AvifDecoder_with_invalid_args(self): + """ + Calling decoder functions with no arguments should result in an error. + """ + with pytest.raises(TypeError): + _avif.AvifDecoder() + + def test_no_resource_warning(self, tmp_path): + with Image.open(TEST_AVIF_FILE) as image: + temp_file = str(tmp_path / "temp.avif") + pytest.warns(None, image.save, temp_file) + + @pytest.mark.parametrize("major_brand", [b"avif", b"avis", b"mif1", b"msf1"]) + def test_accept_ftyp_brands(self, major_brand): + data = b"\x00\x00\x00\x1cftyp%s\x00\x00\x00\x00" % major_brand + assert AvifImagePlugin._accept(data) is True + + def test_file_pointer_could_be_reused(self): + with open(TEST_AVIF_FILE, "rb") as blob: + with Image.open(blob) as im: + im.load() + with Image.open(blob) as im: + im.load() + + def test_background_from_gif(self, tmp_path): + with Image.open("Tests/images/chi.gif") as im: + original_value = im.convert("RGB").getpixel((1, 1)) + + # Save as AVIF + out_avif = str(tmp_path / "temp.avif") + im.save(out_avif, save_all=True) + + # Save as GIF + out_gif = str(tmp_path / "temp.gif") + with Image.open(out_avif) as im: + im.save(out_gif) + + with Image.open(out_gif) as reread: + reread_value = reread.convert("RGB").getpixel((1, 1)) + difference = sum( + [abs(original_value[i] - reread_value[i]) for i in range(0, 3)] + ) + assert difference < 5 + + def test_save_single_frame(self, tmp_path): + temp_file = str(tmp_path / "temp.avif") + with Image.open("Tests/images/chi.gif") as im: + im.save(temp_file) + with Image.open(temp_file) as im: + assert im.n_frames == 1 + + def test_invalid_file(self): + invalid_file = "Tests/images/flower.jpg" + + with pytest.raises(SyntaxError): + AvifImagePlugin.AvifImageFile(invalid_file) + + def test_load_transparent_rgb(self): + test_file = "Tests/images/avif/transparency.avif" + with Image.open(test_file) as im: + assert_image(im, "RGBA", (64, 64)) + + # image has 876 transparent pixels + assert im.getchannel("A").getcolors()[0][0] == 876 + + def test_save_transparent(self, tmp_path): + im = Image.new("RGBA", (10, 10), (0, 0, 0, 0)) + assert im.getcolors() == [(100, (0, 0, 0, 0))] + + test_file = str(tmp_path / "temp.avif") + im.save(test_file) + + # check if saved image contains same transparency + with Image.open(test_file) as im: + assert_image(im, "RGBA", (10, 10)) + assert im.getcolors() == [(100, (0, 0, 0, 0))] + + def test_save_icc_profile(self): + with Image.open("Tests/images/avif/icc_profile_none.avif") as im: + assert im.info.get("icc_profile") is None + + with Image.open("Tests/images/avif/icc_profile.avif") as with_icc: + expected_icc = with_icc.info.get("icc_profile") + assert expected_icc is not None + + im = roundtrip(im, icc_profile=expected_icc) + assert im.info["icc_profile"] == expected_icc + + def test_discard_icc_profile(self): + with Image.open("Tests/images/avif/icc_profile.avif") as im: + im = roundtrip(im, icc_profile=None) + assert "icc_profile" not in im.info + + def test_roundtrip_icc_profile(self): + with Image.open("Tests/images/avif/icc_profile.avif") as im: + expected_icc = im.info["icc_profile"] + + im = roundtrip(im) + assert im.info["icc_profile"] == expected_icc + + def test_roundtrip_no_icc_profile(self): + with Image.open("Tests/images/avif/icc_profile_none.avif") as im: + assert im.info.get("icc_profile") is None + + im = roundtrip(im) + assert "icc_profile" not in im.info + + def test_exif(self): + # With an EXIF chunk + with Image.open("Tests/images/avif/exif.avif") as im: + exif = im.getexif() + assert exif[274] == 1 + + def test_exif_save(self, tmp_path): + with Image.open("Tests/images/avif/exif.avif") as im: + test_file = str(tmp_path / "temp.avif") + im.save(test_file) + + with Image.open(test_file) as reloaded: + exif = reloaded.getexif() + assert exif[274] == 1 + + def test_exif_obj_argument(self, tmp_path): + exif = Image.Exif() + exif[274] = 1 + exif_data = exif.tobytes() + with Image.open(TEST_AVIF_FILE) as im: + test_file = str(tmp_path / "temp.avif") + im.save(test_file, exif=exif) + + with Image.open(test_file) as reloaded: + assert reloaded.info["exif"] == exif_data + + def test_exif_bytes_argument(self, tmp_path): + exif = Image.Exif() + exif[274] = 1 + exif_data = exif.tobytes() + with Image.open(TEST_AVIF_FILE) as im: + test_file = str(tmp_path / "temp.avif") + im.save(test_file, exif=exif_data) + + with Image.open(test_file) as reloaded: + assert reloaded.info["exif"] == exif_data + + def test_exif_invalid(self, tmp_path): + with Image.open(TEST_AVIF_FILE) as im: + test_file = str(tmp_path / "temp.avif") + with pytest.raises(ValueError): + im.save(test_file, exif=b"invalid") + + def test_xmp(self): + with Image.open("Tests/images/avif/xmp_tags_orientation.avif") as im: + xmp = im.info.get("xmp") + assert_xmp_orientation(xmp, 3) + + def test_xmp_save(self, tmp_path): + with Image.open("Tests/images/avif/xmp_tags_orientation.avif") as im: + test_file = str(tmp_path / "temp.avif") + im.save(test_file) + + with Image.open(test_file) as reloaded: + xmp = reloaded.info.get("xmp") + assert_xmp_orientation(xmp, 3) + + def test_xmp_save_from_png(self, tmp_path): + with Image.open("Tests/images/xmp_tags_orientation.png") as im: + test_file = str(tmp_path / "temp.avif") + im.save(test_file) + + with Image.open(test_file) as reloaded: + xmp = reloaded.info.get("xmp") + assert_xmp_orientation(xmp, 3) + + def test_xmp_save_argument(self, tmp_path): + xmp_arg = "\n".join( + [ + '', + '', + ' ', + ' ', + " ", + "", + '', + ] + ) + with Image.open("Tests/images/avif/hopper.avif") as im: + test_file = str(tmp_path / "temp.avif") + im.save(test_file, xmp=xmp_arg) + + with Image.open(test_file) as reloaded: + xmp = reloaded.info.get("xmp") + assert_xmp_orientation(xmp, 1) + + def test_tell(self): + with Image.open(TEST_AVIF_FILE) as im: + assert im.tell() == 0 + + def test_seek(self): + with Image.open(TEST_AVIF_FILE) as im: + im.seek(0) + + with pytest.raises(EOFError): + im.seek(1) + + @pytest.mark.parametrize("subsampling", ["4:4:4", "4:2:2", "4:0:0"]) + def test_encoder_subsampling(self, tmp_path, subsampling): + with Image.open(TEST_AVIF_FILE) as im: + test_file = str(tmp_path / "temp.avif") + im.save(test_file, subsampling=subsampling) + + def test_encoder_subsampling_invalid(self, tmp_path): + with Image.open(TEST_AVIF_FILE) as im: + test_file = str(tmp_path / "temp.avif") + with pytest.raises(ValueError): + im.save(test_file, subsampling="foo") + + def test_encoder_range(self, tmp_path): + with Image.open(TEST_AVIF_FILE) as im: + test_file = str(tmp_path / "temp.avif") + im.save(test_file, range="limited") + + def test_encoder_range_invalid(self, tmp_path): + with Image.open(TEST_AVIF_FILE) as im: + test_file = str(tmp_path / "temp.avif") + with pytest.raises(ValueError): + im.save(test_file, range="foo") + + @skip_unless_avif_encoder("aom") + @skip_unless_feature("avif") + def test_encoder_codec_param(self, tmp_path): + with Image.open(TEST_AVIF_FILE) as im: + test_file = str(tmp_path / "temp.avif") + im.save(test_file, codec="aom") + + def test_encoder_codec_invalid(self, tmp_path): + with Image.open(TEST_AVIF_FILE) as im: + test_file = str(tmp_path / "temp.avif") + with pytest.raises(ValueError): + im.save(test_file, codec="foo") + + @skip_unless_avif_decoder("dav1d") + @skip_unless_feature("avif") + def test_encoder_codec_cannot_encode(self, tmp_path): + with Image.open(TEST_AVIF_FILE) as im: + test_file = str(tmp_path / "temp.avif") + with pytest.raises(ValueError): + im.save(test_file, codec="dav1d") + + @skip_unless_avif_encoder("aom") + @skip_unless_avif_version_gte((0, 8, 2)) + @skip_unless_feature("avif") + def test_encoder_advanced_codec_options(self): + with Image.open(TEST_AVIF_FILE) as im: + ctrl_buf = BytesIO() + im.save(ctrl_buf, "AVIF", codec="aom") + test_buf = BytesIO() + im.save( + test_buf, + "AVIF", + codec="aom", + advanced={ + "aq-mode": "1", + "enable-chroma-deltaq": "1", + }, + ) + assert ctrl_buf.getvalue() != test_buf.getvalue() + + @skip_unless_avif_encoder("aom") + @skip_unless_avif_version_gte((0, 8, 2)) + @skip_unless_feature("avif") + @pytest.mark.parametrize("val", [{"foo": "bar"}, 1234]) + def test_encoder_advanced_codec_options_invalid(self, tmp_path, val): + with Image.open(TEST_AVIF_FILE) as im: + test_file = str(tmp_path / "temp.avif") + with pytest.raises(ValueError): + im.save(test_file, codec="aom", advanced=val) + + @skip_unless_avif_decoder("aom") + @skip_unless_feature("avif") + def test_decoder_codec_param(self): + AvifImagePlugin.DECODE_CODEC_CHOICE = "aom" + try: + with Image.open(TEST_AVIF_FILE) as im: + assert im.size == (128, 128) + finally: + AvifImagePlugin.DECODE_CODEC_CHOICE = "auto" + + @skip_unless_avif_encoder("rav1e") + @skip_unless_feature("avif") + def test_decoder_codec_cannot_decode(self, tmp_path): + AvifImagePlugin.DECODE_CODEC_CHOICE = "rav1e" + try: + with pytest.raises(ValueError): + with Image.open(TEST_AVIF_FILE): + pass + finally: + AvifImagePlugin.DECODE_CODEC_CHOICE = "auto" + + def test_decoder_codec_invalid(self): + AvifImagePlugin.DECODE_CODEC_CHOICE = "foo" + try: + with pytest.raises(ValueError): + with Image.open(TEST_AVIF_FILE): + pass + finally: + AvifImagePlugin.DECODE_CODEC_CHOICE = "auto" + + @skip_unless_avif_encoder("aom") + @skip_unless_feature("avif") + def test_encoder_codec_available(self): + assert _avif.encoder_codec_available("aom") is True + + def test_encoder_codec_available_bad_params(self): + with pytest.raises(TypeError): + _avif.encoder_codec_available() + + @skip_unless_avif_decoder("dav1d") + @skip_unless_feature("avif") + def test_encoder_codec_available_cannot_decode(self): + assert _avif.encoder_codec_available("dav1d") is False + + def test_encoder_codec_available_invalid(self): + assert _avif.encoder_codec_available("foo") is False + + @pytest.mark.parametrize( + "quality,expected_qminmax", + [ + [0, (63, 63)], + [100, (0, 0)], + [90, (0, 10)], + [None, (0, 25)], # default + [50, (14, 50)], + ], + ) + def test_encoder_quality_qmin_qmax_map(self, tmp_path, quality, expected_qminmax): + MockEncoder = mock.Mock(wraps=_avif.AvifEncoder) + with mock.patch.object(_avif, "AvifEncoder", new=MockEncoder) as mock_encoder: + with Image.open("Tests/images/avif/hopper.avif") as im: + test_file = str(tmp_path / "temp.avif") + if quality is None: + im.save(test_file) + else: + im.save(test_file, quality=quality) + assert mock_encoder.call_args[0][3:5] == expected_qminmax + + def test_encoder_quality_valueerror(self, tmp_path): + with Image.open("Tests/images/avif/hopper.avif") as im: + test_file = str(tmp_path / "temp.avif") + with pytest.raises(ValueError): + im.save(test_file, quality="invalid") + + @skip_unless_avif_decoder("aom") + @skip_unless_feature("avif") + def test_decoder_codec_available(self): + assert _avif.decoder_codec_available("aom") is True + + def test_decoder_codec_available_bad_params(self): + with pytest.raises(TypeError): + _avif.decoder_codec_available() + + @skip_unless_avif_encoder("rav1e") + @skip_unless_feature("avif") + def test_decoder_codec_available_cannot_decode(self): + assert _avif.decoder_codec_available("rav1e") is False + + def test_decoder_codec_available_invalid(self): + assert _avif.decoder_codec_available("foo") is False + + @pytest.mark.parametrize("upsampling", ["fastest", "best", "nearest", "bilinear"]) + def test_decoder_upsampling(self, upsampling): + AvifImagePlugin.CHROMA_UPSAMPLING = upsampling + try: + with Image.open(TEST_AVIF_FILE): + pass + finally: + AvifImagePlugin.CHROMA_UPSAMPLING = "auto" + + def test_decoder_upsampling_invalid(self): + AvifImagePlugin.CHROMA_UPSAMPLING = "foo" + try: + with pytest.raises(ValueError): + with Image.open(TEST_AVIF_FILE): + pass + finally: + AvifImagePlugin.CHROMA_UPSAMPLING = "auto" + + +@skip_unless_feature("avif") +class TestAvifAnimation: + @contextmanager + def star_frames(self): + with Image.open("Tests/images/avif/star.png") as f1: + with Image.open("Tests/images/avif/star90.png") as f2: + with Image.open("Tests/images/avif/star180.png") as f3: + with Image.open("Tests/images/avif/star270.png") as f4: + yield [f1, f2, f3, f4] + + def test_n_frames(self): + """ + Ensure that AVIF format sets n_frames and is_animated attributes + correctly. + """ + + with Image.open("Tests/images/avif/hopper.avif") as im: + assert im.n_frames == 1 + assert not im.is_animated + + with Image.open("Tests/images/avif/star.avifs") as im: + assert im.n_frames == 5 + assert im.is_animated + + def test_write_animation_L(self, tmp_path): + """ + Convert an animated GIF to animated AVIF, then compare the frame + count, and first and last frames to ensure they're visually similar. + """ + + with Image.open("Tests/images/avif/star.gif") as orig: + assert orig.n_frames > 1 + + temp_file = str(tmp_path / "temp.avif") + orig.save(temp_file, save_all=True) + with Image.open(temp_file) as im: + assert im.n_frames == orig.n_frames + + # Compare first and second-to-last frames to the original animated GIF + orig.load() + im.load() + assert_image_similar(im.convert("RGB"), orig.convert("RGB"), 25.0) + orig.seek(orig.n_frames - 2) + im.seek(im.n_frames - 2) + orig.load() + im.load() + assert_image_similar(im.convert("RGB"), orig.convert("RGB"), 25.0) + + def test_write_animation_RGB(self, tmp_path): + """ + Write an animated AVIF from RGB frames, and ensure the frames + are visually similar to the originals. + """ + + def check(temp_file): + with Image.open(temp_file) as im: + assert im.n_frames == 4 + + # Compare first frame to original + im.load() + assert_image_similar(im, frame1.convert("RGBA"), 25.0) + + # Compare second frame to original + im.seek(1) + im.load() + assert_image_similar(im, frame2.convert("RGBA"), 25.0) + + with self.star_frames() as frames: + frame1 = frames[0] + frame2 = frames[1] + temp_file1 = str(tmp_path / "temp.avif") + frames[0].copy().save(temp_file1, save_all=True, append_images=frames[1:]) + check(temp_file1) + + # Tests appending using a generator + def imGenerator(ims): + yield from ims + + temp_file2 = str(tmp_path / "temp_generator.avif") + frames[0].copy().save( + temp_file2, + save_all=True, + append_images=imGenerator(frames[1:]), + ) + check(temp_file2) + + def test_sequence_dimension_mismatch_check(self, tmp_path): + temp_file = str(tmp_path / "temp.avif") + frame1 = Image.new("RGB", (100, 100)) + frame2 = Image.new("RGB", (150, 150)) + with pytest.raises(ValueError): + frame1.save(temp_file, save_all=True, append_images=[frame2], duration=100) + + def test_heif_raises_unidentified_image_error(self): + with pytest.raises(UnidentifiedImageError): + with Image.open("Tests/images/avif/rgba10.heif"): + pass + + @skip_unless_avif_version_gte((0, 9, 0)) + @pytest.mark.parametrize("alpha_premultipled", [False, True]) + def test_alpha_premultiplied_true(self, alpha_premultipled): + im = Image.new("RGBA", (10, 10), (0, 0, 0, 0)) + im_buf = BytesIO() + im.save(im_buf, "AVIF", alpha_premultiplied=alpha_premultipled) + im_bytes = im_buf.getvalue() + assert has_alpha_premultiplied(im_bytes) is alpha_premultipled + + def test_timestamp_and_duration(self, tmp_path): + """ + Try passing a list of durations, and make sure the encoded + timestamps and durations are correct. + """ + + durations = [1, 10, 20, 30, 40] + temp_file = str(tmp_path / "temp.avif") + with self.star_frames() as frames: + frames[0].save( + temp_file, + save_all=True, + append_images=(frames[1:] + [frames[0]]), + duration=durations, + ) + + with Image.open(temp_file) as im: + assert im.n_frames == 5 + assert im.is_animated + + # Check that timestamps and durations match original values specified + ts = 0 + for frame in range(im.n_frames): + im.seek(frame) + im.load() + assert im.info["duration"] == durations[frame] + assert im.info["timestamp"] == ts + ts += durations[frame] + + def test_seeking(self, tmp_path): + """ + Create an animated AVIF file, and then try seeking through frames in + reverse-order, verifying the timestamps and durations are correct. + """ + + dur = 33 + temp_file = str(tmp_path / "temp.avif") + with self.star_frames() as frames: + frames[0].save( + temp_file, + save_all=True, + append_images=(frames[1:] + [frames[0]]), + duration=dur, + ) + + with Image.open(temp_file) as im: + assert im.n_frames == 5 + assert im.is_animated + + # Traverse frames in reverse, checking timestamps and durations + ts = dur * (im.n_frames - 1) + for frame in reversed(range(im.n_frames)): + im.seek(frame) + im.load() + assert im.info["duration"] == dur + assert im.info["timestamp"] == ts + ts -= dur + + def test_seek_errors(self): + with Image.open("Tests/images/avif/star.avifs") as im: + with pytest.raises(EOFError): + im.seek(-1) + + with pytest.raises(EOFError): + im.seek(42) + + +MAX_THREADS = os.cpu_count() + + +@skip_unless_feature("avif") +class TestAvifLeaks(PillowLeakTestCase): + mem_limit = MAX_THREADS * 3 * 1024 + iterations = 100 + + @pytest.mark.skipif( + is_docker_qemu(), reason="Skipping on cross-architecture containers" + ) + def test_leak_load(self): + with open(TEST_AVIF_FILE, "rb") as f: + im_data = f.read() + + def core(): + with Image.open(BytesIO(im_data)) as im: + im.load() + + self._test_leak(core) diff --git a/depends/install_libavif.sh b/depends/install_libavif.sh new file mode 100755 index 00000000000..1e414668804 --- /dev/null +++ b/depends/install_libavif.sh @@ -0,0 +1,83 @@ +#!/usr/bin/env bash +set -eo pipefail + +LIBAVIF_VERSION=${LIBAVIF_VERSION:-1.0.1} + +LIBAVIF_CMAKE_FLAGS=() + +if uname -s | grep -q Darwin; then + PREFIX=/usr/local +else + PREFIX=/usr +fi + +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" + +PKGCONFIG=${PKGCONFIG:-pkg-config} + +export CFLAGS="-fPIC -O3 $CFLAGS" +export CXXFLAGS="-fPIC -O3 $CXXFLAGS" + +mkdir -p libavif-$LIBAVIF_VERSION +curl -sLo - \ + https://github.com/AOMediaCodec/libavif/archive/v$LIBAVIF_VERSION.tar.gz \ + | tar --strip-components=1 -C libavif-$LIBAVIF_VERSION -zxf - +pushd libavif-$LIBAVIF_VERSION + +if [ "$LIBAVIF_VERSION" == "1.0.1" ]; then + patch -p1 < "${SCRIPT_DIR}/libavif-1.0.1-local-static.patch" +fi + +HAS_DECODER=0 +HAS_ENCODER=0 + +if $PKGCONFIG --exists dav1d; then + LIBAVIF_CMAKE_FLAGS+=(-DAVIF_CODEC_DAV1D=ON) + HAS_DECODER=1 +fi + +if $PKGCONFIG --exists rav1e; then + LIBAVIF_CMAKE_FLAGS+=(-DAVIF_CODEC_RAV1E=ON) + HAS_ENCODER=1 +fi + +if $PKGCONFIG --exists SvtAv1Enc; then + LIBAVIF_CMAKE_FLAGS+=(-DAVIF_CODEC_SVT=ON) + HAS_ENCODER=1 +fi + +if $PKGCONFIG --exists libgav1; then + LIBAVIF_CMAKE_FLAGS+=(-DAVIF_CODEC_LIBGAV1=ON) + HAS_DECODER=1 +fi + +if $PKGCONFIG --exists aom; then + LIBAVIF_CMAKE_FLAGS+=(-DAVIF_CODEC_AOM=ON) + HAS_ENCODER=1 + HAS_DECODER=1 +fi + +if [ "$HAS_ENCODER" != 1 ] || [ "$HAS_DECODER" != 1 ]; then + pushd ext > /dev/null + bash aom.cmd + popd > /dev/null + LIBAVIF_CMAKE_FLAGS+=(-DAVIF_CODEC_AOM=ON -DAVIF_LOCAL_AOM=ON) +fi + +if uname -s | grep -q Darwin; then + # Prevent cmake from using @rpath in install id, so that delocate can + # find and bundle the libavif dylib + LIBAVIF_CMAKE_FLAGS+=("-DCMAKE_INSTALL_NAME_DIR=$PREFIX/lib" -DCMAKE_MACOSX_RPATH=OFF) +fi + +mkdir build +pushd build +cmake .. \ + -DCMAKE_INSTALL_PREFIX=$PREFIX \ + -DCMAKE_BUILD_TYPE=Release \ + "${LIBAVIF_CMAKE_FLAGS[@]}" +make +sudo make install +popd + +popd diff --git a/depends/libavif-1.0.1-local-static.patch b/depends/libavif-1.0.1-local-static.patch new file mode 100644 index 00000000000..e7faeed0f8a --- /dev/null +++ b/depends/libavif-1.0.1-local-static.patch @@ -0,0 +1,148 @@ +From f8f4ed7ecec80a596f60a4a7e1392c09cedbf7ed Mon Sep 17 00:00:00 2001 +From: Frankie Dintino +Date: Tue, 12 Sep 2023 05:47:43 -0400 +Subject: [PATCH] ci: link shared library build against static local + +--- + CMakeLists.txt | 33 +++++++++++++-------------------- + ext/libyuv.cmd | 2 +- + 2 files changed, 14 insertions(+), 21 deletions(-) + +diff --git a/CMakeLists.txt b/CMakeLists.txt +index 1f0cde1..521560e 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -78,10 +78,10 @@ endif() + + if(BUILD_SHARED_LIBS) + set(AVIF_LIBRARY_PREFIX "${CMAKE_SHARED_LIBRARY_PREFIX}") +- set(AVIF_LIBRARY_SUFFIX "${CMAKE_SHARED_LIBRARY_SUFFIX}") + else() + set(AVIF_LIBRARY_PREFIX "${CMAKE_STATIC_LIBRARY_PREFIX}") +- set(AVIF_LIBRARY_SUFFIX "${CMAKE_STATIC_LIBRARY_SUFFIX}") ++ # This is needed to get shared libraries (e.g. pixbufloader-avif) to compile against a static libavif. ++ set(CMAKE_POSITION_INDEPENDENT_CODE ON) + endif() + + set(AVIF_PLATFORM_DEFINITIONS) +@@ -112,7 +112,7 @@ if(AVIF_LOCAL_ZLIBPNG) + set(PREV_ANDROID ${ANDROID}) + set(ANDROID TRUE) + set(PNG_BUILD_ZLIB "${CMAKE_CURRENT_SOURCE_DIR}/ext/zlib" CACHE STRING "" FORCE) +- set(PNG_SHARED ${BUILD_SHARED_LIBS} CACHE BOOL "") ++ set(PNG_SHARED OFF CACHE BOOL "") + set(PNG_TESTS OFF CACHE BOOL "") + add_subdirectory(ext/libpng) + set(PNG_PNG_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ext/libpng") +@@ -135,7 +135,7 @@ if(AVIF_LOCAL_JPEG) + endif() + option(AVIF_LOCAL_LIBYUV "Build libyuv by providing your own copy inside the ext subdir." OFF) + if(AVIF_LOCAL_LIBYUV) +- set(LIB_FILENAME "${CMAKE_CURRENT_SOURCE_DIR}/ext/libyuv/build/${AVIF_LIBRARY_PREFIX}yuv${AVIF_LIBRARY_SUFFIX}") ++ set(LIB_FILENAME "${CMAKE_CURRENT_SOURCE_DIR}/ext/libyuv/build/${AVIF_LIBRARY_PREFIX}yuv${CMAKE_STATIC_LIBRARY_SUFFIX}") + if(NOT EXISTS "${LIB_FILENAME}") + message(FATAL_ERROR "libavif(AVIF_LOCAL_LIBYUV): ${LIB_FILENAME} is missing, bailing out") + endif() +@@ -146,13 +146,6 @@ if(AVIF_LOCAL_LIBYUV) + set(LIBYUV_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ext/libyuv/include" PARENT_SCOPE) + set(LIBYUV_LIBRARY ${LIB_FILENAME} PARENT_SCOPE) + endif() +- if(BUILD_SHARED_LIBS) +- # Fix "libyuv.so: undefined reference to `jpeg_read_raw_data'" errors. +- if(NOT AVIF_LOCAL_JPEG) +- find_package(JPEG REQUIRED) +- endif() +- set(LIBYUV_LIBRARY ${LIBYUV_LIBRARY} ${JPEG_LIBRARY}) +- endif() + set(libyuv_FOUND TRUE) + message(STATUS "libavif: local libyuv found; libyuv-based fast paths enabled.") + else(AVIF_LOCAL_LIBYUV) +@@ -184,7 +177,7 @@ if(libyuv_FOUND) + endif(libyuv_FOUND) + option(AVIF_LOCAL_LIBSHARPYUV "Build libsharpyuv by providing your own copy inside the ext subdir." OFF) + if(AVIF_LOCAL_LIBSHARPYUV) +- set(LIB_FILENAME "${CMAKE_CURRENT_SOURCE_DIR}/ext/libwebp/build/libsharpyuv${AVIF_LIBRARY_SUFFIX}") ++ set(LIB_FILENAME "${CMAKE_CURRENT_SOURCE_DIR}/ext/libwebp/build/libsharpyuv${CMAKE_STATIC_LIBRARY_SUFFIX}") + if(NOT EXISTS "${LIB_FILENAME}") + message(FATAL_ERROR "libavif(AVIF_LOCAL_LIBSHARPYUV): ${LIB_FILENAME} is missing, bailing out") + endif() +@@ -309,16 +302,16 @@ if(AVIF_CODEC_DAV1D) + if(DEFINED ANDROID_ABI) + set(AVIF_DAV1D_BUILD_DIR "${AVIF_DAV1D_BUILD_DIR}/${ANDROID_ABI}") + endif() +- set(LIB_FILENAME "${AVIF_DAV1D_BUILD_DIR}/src/libdav1d${AVIF_LIBRARY_SUFFIX}") ++ set(LIB_FILENAME "${AVIF_DAV1D_BUILD_DIR}/src/libdav1d${CMAKE_STATIC_LIBRARY_SUFFIX}") + if(NOT EXISTS "${LIB_FILENAME}") +- if("${AVIF_LIBRARY_SUFFIX}" STREQUAL ".a") ++ if("${CMAKE_STATIC_LIBRARY_SUFFIX}" STREQUAL ".a") + message(FATAL_ERROR "libavif: ${LIB_FILENAME} is missing, bailing out") + else() + # On windows, meson will produce a libdav1d.a instead of the expected libdav1d.dll/.lib. + # See https://github.com/mesonbuild/meson/issues/8153. + set(LIB_FILENAME "${CMAKE_CURRENT_SOURCE_DIR}/ext/dav1d/build/src/libdav1d.a") + if(NOT EXISTS "${LIB_FILENAME}") +- message(FATAL_ERROR "libavif: ${LIB_FILENAME} (or libdav1d${AVIF_LIBRARY_SUFFIX}) is missing, bailing out") ++ message(FATAL_ERROR "libavif: ${LIB_FILENAME} (or libdav1d${CMAKE_STATIC_LIBRARY_SUFFIX}) is missing, bailing out") + endif() + endif() + endif() +@@ -353,7 +346,7 @@ if(AVIF_CODEC_LIBGAV1) + if(DEFINED ANDROID_ABI) + set(AVIF_LIBGAV1_BUILD_DIR "${AVIF_LIBGAV1_BUILD_DIR}/${ANDROID_ABI}") + endif() +- set(LIB_FILENAME "${AVIF_LIBGAV1_BUILD_DIR}/libgav1${AVIF_LIBRARY_SUFFIX}") ++ set(LIB_FILENAME "${AVIF_LIBGAV1_BUILD_DIR}/libgav1${CMAKE_STATIC_LIBRARY_SUFFIX}") + if(NOT EXISTS "${LIB_FILENAME}") + message(FATAL_ERROR "libavif: ${LIB_FILENAME} is missing, bailing out") + endif() +@@ -378,7 +371,7 @@ if(AVIF_CODEC_RAV1E) + + if(AVIF_LOCAL_RAV1E) + set(LIB_FILENAME +- "${CMAKE_CURRENT_SOURCE_DIR}/ext/rav1e/build.libavif/usr/lib/${AVIF_LIBRARY_PREFIX}rav1e${AVIF_LIBRARY_SUFFIX}" ++ "${CMAKE_CURRENT_SOURCE_DIR}/ext/rav1e/build.libavif/usr/lib/${AVIF_LIBRARY_PREFIX}rav1e${CMAKE_STATIC_LIBRARY_SUFFIX}" + ) + if(NOT EXISTS "${LIB_FILENAME}") + message(FATAL_ERROR "libavif: compiled rav1e library is missing (in ext/rav1e/build.libavif/usr/lib), bailing out") +@@ -411,7 +404,7 @@ if(AVIF_CODEC_SVT) + + if(AVIF_LOCAL_SVT) + set(LIB_FILENAME +- "${CMAKE_CURRENT_SOURCE_DIR}/ext/SVT-AV1/Bin/Release/${AVIF_LIBRARY_PREFIX}SvtAv1Enc${AVIF_LIBRARY_SUFFIX}" ++ "${CMAKE_CURRENT_SOURCE_DIR}/ext/SVT-AV1/Bin/Release/${AVIF_LIBRARY_PREFIX}SvtAv1Enc${CMAKE_STATIC_LIBRARY_SUFFIX}" + ) + if(NOT EXISTS "${LIB_FILENAME}") + message(FATAL_ERROR "libavif: compiled svt library is missing (in ext/SVT-AV1/Bin/Release), bailing out") +@@ -450,7 +443,7 @@ if(AVIF_CODEC_AOM) + endif() + set(AVIF_SRCS ${AVIF_SRCS} src/codec_aom.c) + if(AVIF_LOCAL_AOM) +- set(LIB_FILENAME "${CMAKE_CURRENT_SOURCE_DIR}/ext/aom/build.libavif/${AVIF_LIBRARY_PREFIX}aom${AVIF_LIBRARY_SUFFIX}") ++ set(LIB_FILENAME "${CMAKE_CURRENT_SOURCE_DIR}/ext/aom/build.libavif/${AVIF_LIBRARY_PREFIX}aom${CMAKE_STATIC_LIBRARY_SUFFIX}") + if(NOT EXISTS "${LIB_FILENAME}") + message(FATAL_ERROR "libavif: ${LIB_FILENAME} is missing, bailing out") + endif() +@@ -482,7 +475,7 @@ if(AVIF_CODEC_AVM) + set(AVIF_SRCS ${AVIF_SRCS} src/codec_avm.c) + if(AVIF_LOCAL_AVM) + # Building the avm repository generates files such as "libaom.a" because it is a fork of aom. +- set(LIB_FILENAME "${CMAKE_CURRENT_SOURCE_DIR}/ext/avm/build.libavif/${AVIF_LIBRARY_PREFIX}aom${AVIF_LIBRARY_SUFFIX}") ++ set(LIB_FILENAME "${CMAKE_CURRENT_SOURCE_DIR}/ext/avm/build.libavif/${AVIF_LIBRARY_PREFIX}aom${CMAKE_STATIC_LIBRARY_SUFFIX}") + if(NOT EXISTS "${LIB_FILENAME}") + message(FATAL_ERROR "libavif: ${LIB_FILENAME} (from avm) is missing, bailing out") + endif() +diff --git a/ext/libyuv.cmd b/ext/libyuv.cmd +index c959777..1186156 100755 +--- a/ext/libyuv.cmd ++++ b/ext/libyuv.cmd +@@ -22,6 +22,6 @@ git checkout 464c51a0 + mkdir build + cd build + +-cmake -G Ninja -DCMAKE_BUILD_TYPE=Release .. ++cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBUILD_SHARED_LIBS=OFF .. + ninja yuv + cd ../.. +-- +2.30.0 + diff --git a/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index 2a42bdacba7..98f30ba3679 100644 --- a/docs/handbook/image-file-formats.rst +++ b/docs/handbook/image-file-formats.rst @@ -203,7 +203,7 @@ following options are available:: **append_images** A list of images to append as additional frames. Each of the images in the list can be single or multiframe images. - This is currently supported for GIF, PDF, PNG, TIFF, and WebP. + This is currently supported for GIF, PDF, PNG, TIFF, WebP, and AVIF. It is also supported for ICO and ICNS. If images are passed in of relevant sizes, they will be used instead of scaling down the main image. @@ -1220,6 +1220,79 @@ XBM Pillow reads and writes X bitmap files (mode ``1``). +AVIF +^^^^ + +Pillow reads and writes AVIF files, including AVIF sequence images. Currently, +it is only possible to save 8-bit AVIF images, and all AVIF images are decoded +as 8-bit RGB(A). + +The :py:meth:`~PIL.Image.Image.save` method supports the following options: + +**quality** + Integer, 1-100, Defaults to 90. 0 gives the smallest size and poorest + quality, 100 the largest and best quality. The value of this setting + controls the ``qmin`` and ``qmax`` encoder options. + +**qmin** / **qmax** + Integer, 0-63. The quality of images created by an AVIF encoder are + controlled by minimum and maximum quantizer values. The higher these + values are, the worse the quality. + +**subsampling** + If present, sets the subsampling for the encoder. Defaults to ``"4:2:0``". + Options include: + + * ``"4:0:0"`` + * ``"4:2:0"`` + * ``"4:2:2"`` + * ``"4:4:4"`` + +**speed** + Quality/speed trade-off (0=slower-better, 10=fastest). Defaults to 8. + +**range** + YUV range, either "full" or "limited." Defaults to "full" + +**codec** + AV1 codec to use for encoding. Possible values are "aom", "rav1e", and + "svt", depending on what codecs were compiled with libavif. Defaults to + "auto", which will choose the first available codec in the order of the + preceding list. + +**tile_rows** / **tile_cols** + For tile encoding, the (log 2) number of tile rows and columns to use. + Valid values are 0-6, default 0. + +**alpha_premultiplied** + Encode the image with premultiplied alpha, defaults ``False`` + +**icc_profile** + The ICC Profile to include in the saved file. + +**exif** + The exif data to include in the saved file. + +**xmp** + The XMP data to include in the saved file. + +Saving sequences +~~~~~~~~~~~~~~~~~ + +When calling :py:meth:`~PIL.Image.Image.save` to write an AVIF file, by default +only the first frame of a multiframe image will be saved. If the ``save_all`` +argument is present and true, then all frames will be saved, and the following +options will also be available. + +**append_images** + A list of images to append as additional frames. Each of the + images in the list can be single or multiframe images. + +**duration** + The display duration of each frame, in milliseconds. Pass a single + integer for a constant duration, or a list or tuple to set the + duration for each frame separately. + Read-only formats ----------------- diff --git a/docs/installation.rst b/docs/installation.rst index ca78b29989d..bc38ec08823 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -203,6 +203,15 @@ Many of Pillow's features require external libraries: See `Build Options`_ to see how to build this version. * Previous versions of Pillow (5.0.0 to 8.1.2) linked libraqm dynamically at runtime. +* **libavif** provides support for the AVIF format. + + * Pillow requires libavif version **0.8.0** or greater, which is when + AVIF image sequence support was added. + * libavif is merely an API that wraps AVIF codecs. If you are compiling + libavif from source, you will also need to install both an AVIF encoder + and decoder, such as rav1e and dav1d, or libaom, which both encodes and + decodes AVIF images. + * **libxcb** provides X11 screengrab support. .. tab:: Linux @@ -233,6 +242,12 @@ Many of Pillow's features require external libraries: To install libraqm, ``sudo apt-get install meson`` and then see ``depends/install_raqm.sh``. + Build prerequisites for libavif on Ubuntu are installed with:: + + sudo apt-get install cmake ninja-build nasm + + Then see ``depends/install_libavif.sh`` to build and install libavif. + Prerequisites are installed on recent **Red Hat**, **CentOS** or **Fedora** with:: sudo dnf install libtiff-devel libjpeg-devel openjpeg2-devel zlib-devel \ @@ -272,6 +287,12 @@ Many of Pillow's features require external libraries: Then see ``depends/install_raqm_cmake.sh`` to install libraqm. + To install libavif on macOS use Homebrew to install its build dependencies:: + + brew install aom dav1d rav1e + + Then see ``depends/install_libavif.sh`` to install libavif. + .. tab:: Windows We recommend you use prebuilt wheels from PyPI. @@ -309,7 +330,8 @@ Many of Pillow's features require external libraries: mingw-w64-x86_64-libwebp \ mingw-w64-x86_64-openjpeg2 \ mingw-w64-x86_64-libimagequant \ - mingw-w64-x86_64-libraqm + mingw-w64-x86_64-libraqm \ + mingw-w64-x86_64-libavif https://www.msys2.org/docs/python/ states that setuptools >= 60 does not work with MSYS2. To workaround this, before installing Pillow you must run:: @@ -324,11 +346,11 @@ Many of Pillow's features require external libraries: sudo pkg install python3 - Prerequisites are installed on **FreeBSD 10 or 11** with:: + Prerequisites are installed on **FreeBSD 11 or 12** with:: - sudo pkg install jpeg-turbo tiff webp lcms2 freetype2 openjpeg harfbuzz fribidi libxcb + sudo pkg install jpeg-turbo tiff webp lcms2 freetype2 openjpeg harfbuzz fribidi libxcb libavif - Then see ``depends/install_raqm_cmake.sh`` to install libraqm. + See ``depends/install_raqm_cmake.sh`` to install libraqm. .. tab:: Android diff --git a/docs/reference/features.rst b/docs/reference/features.rst index c6619306186..3f7f73de0f8 100644 --- a/docs/reference/features.rst +++ b/docs/reference/features.rst @@ -21,6 +21,7 @@ Support for the following modules can be checked: * ``freetype2``: FreeType font support via :py:func:`PIL.ImageFont.truetype`. * ``littlecms2``: LittleCMS 2 support via :py:mod:`PIL.ImageCms`. * ``webp``: WebP image support. +* ``avif``: AVIF image support. .. autofunction:: PIL.features.check_module .. autofunction:: PIL.features.version_module diff --git a/docs/reference/plugins.rst b/docs/reference/plugins.rst index fcf4514a84d..af5ac7c5f2e 100644 --- a/docs/reference/plugins.rst +++ b/docs/reference/plugins.rst @@ -1,6 +1,14 @@ Plugin reference ================ +:mod:`~PIL.AvifImagePlugin` Module +---------------------------------- + +.. automodule:: PIL.AvifImagePlugin + :members: + :undoc-members: + :show-inheritance: + :mod:`~PIL.BmpImagePlugin` Module --------------------------------- diff --git a/setup.py b/setup.py index 93516671670..19856ed259e 100755 --- a/setup.py +++ b/setup.py @@ -295,6 +295,7 @@ class feature: "jpeg2000", "imagequant", "xcb", + "avif", ] required = {"jpeg", "zlib"} @@ -805,6 +806,12 @@ def build_extensions(self): if _find_library_file(self, "xcb"): feature.xcb = "xcb" + if feature.want("avif"): + _dbg("Looking for avif") + if _find_include_file(self, "avif/avif.h"): + if _find_library_file(self, "avif"): + feature.avif = "avif" + for f in feature: if not getattr(feature, f) and feature.require(f): if f in ("jpeg", "zlib"): @@ -898,6 +905,13 @@ def build_extensions(self): else: self._remove_extension("PIL._webp") + if feature.avif: + libs = [feature.avif] + defs = [] + self._update_extension("PIL._avif", libs, defs) + else: + self._remove_extension("PIL._avif") + tk_libs = ["psapi"] if sys.platform in ("win32", "cygwin") else [] self._update_extension("PIL._imagingtk", tk_libs) @@ -937,6 +951,7 @@ def summary_report(self, feature): (feature.webp, "WEBP"), (feature.webpmux, "WEBPMUX"), (feature.xcb, "XCB (X protocol)"), + (feature.avif, "LIBAVIF"), ] all = 1 @@ -979,6 +994,7 @@ def debug_build(): Extension("PIL._imagingft", ["src/_imagingft.c"]), Extension("PIL._imagingcms", ["src/_imagingcms.c"]), Extension("PIL._webp", ["src/_webp.c"]), + Extension("PIL._avif", ["src/_avif.c"]), Extension("PIL._imagingtk", ["src/_imagingtk.c", "src/Tk/tkImaging.c"]), Extension("PIL._imagingmath", ["src/_imagingmath.c"]), Extension("PIL._imagingmorph", ["src/_imagingmorph.c"]), diff --git a/src/PIL/AvifImagePlugin.py b/src/PIL/AvifImagePlugin.py new file mode 100644 index 00000000000..6a110282786 --- /dev/null +++ b/src/PIL/AvifImagePlugin.py @@ -0,0 +1,260 @@ +from io import BytesIO + +from . import Image, ImageFile + +try: + from . import _avif + + SUPPORTED = True +except ImportError: + SUPPORTED = False + +# Decoder options as module globals, until there is a way to pass parameters +# to Image.open (see https://github.com/python-pillow/Pillow/issues/569) +DECODE_CODEC_CHOICE = "auto" +CHROMA_UPSAMPLING = "auto" + +_VALID_AVIF_MODES = {"RGB", "RGBA"} + + +def _accept(prefix): + if prefix[4:8] != b"ftyp": + return + coding_brands = (b"avif", b"avis") + container_brands = (b"mif1", b"msf1") + major_brand = prefix[8:12] + if major_brand in coding_brands: + if not SUPPORTED: + return ( + "image file could not be identified because AVIF " + "support not installed" + ) + return True + if major_brand in container_brands: + # We accept files with AVIF container brands; we can't yet know if + # the ftyp box has the correct compatible brands, but if it doesn't + # then the plugin will raise a SyntaxError which Pillow will catch + # before moving on to the next plugin that accepts the file. + # + # Also, because this file might not actually be an AVIF file, we + # don't raise an error if AVIF support isn't properly compiled. + return True + + +class AvifImageFile(ImageFile.ImageFile): + format = "AVIF" + format_description = "AVIF image" + __loaded = -1 + __frame = 0 + + def _open(self): + self._decoder = _avif.AvifDecoder( + self.fp.read(), DECODE_CODEC_CHOICE, CHROMA_UPSAMPLING + ) + + # Get info from decoder + width, height, n_frames, mode, icc, exif, xmp = self._decoder.get_info() + self._size = width, height + self.n_frames = n_frames + self.is_animated = self.n_frames > 1 + self._mode = self.rawmode = mode + self.tile = [] + + if icc: + self.info["icc_profile"] = icc + if exif: + self.info["exif"] = exif + if xmp: + self.info["xmp"] = xmp + + def seek(self, frame): + if not self._seek_check(frame): + return + + self.__frame = frame + + def load(self): + if self.__loaded != self.__frame: + # We need to load the image data for this frame + data, timescale, tsp_in_ts, dur_in_ts = self._decoder.get_frame( + self.__frame + ) + timestamp = round(1000 * (tsp_in_ts / timescale)) + duration = round(1000 * (dur_in_ts / timescale)) + self.info["timestamp"] = timestamp + self.info["duration"] = duration + self.__loaded = self.__frame + + # Set tile + if self.fp and self._exclusive_fp: + self.fp.close() + self.fp = BytesIO(data) + self.tile = [("raw", (0, 0) + self.size, 0, self.rawmode)] + + return super().load() + + def tell(self): + return self.__frame + + +def _save_all(im, fp, filename): + _save(im, fp, filename, save_all=True) + + +def _save(im, fp, filename, save_all=False): + info = im.encoderinfo.copy() + if save_all: + append_images = list(info.get("append_images", [])) + else: + append_images = [] + + total = 0 + for ims in [im] + append_images: + total += getattr(ims, "n_frames", 1) + + is_single_frame = total == 1 + + qmin = info.get("qmin") + qmax = info.get("qmax") + + if qmin is None and qmax is None: + # The min and max quantizer settings in libavif range from 0 (best quality) + # to 63 (worst quality). If neither are explicitly specified, we use a 0-100 + # quality scale (default 75) and calculate the qmin and qmax from that. + # + # - qmin is 0 for quality >= 64. Below that, qmin has an inverse linear + # relation to quality (i.e., quality 63 = qmin 1, quality 0 => qmin 63) + # - qmax is 0 for quality=100, then qmax increases linearly relative to + # quality decreasing, until it flattens out at quality=37. + quality = info.get("quality", 75) + if not isinstance(quality, int) or quality < 0 or quality > 100: + msg = "Invalid quality setting" + raise ValueError(msg) + qmin = max(0, min(64 - quality, 63)) + qmax = max(0, min(100 - quality, 63)) + + duration = info.get("duration", 0) + subsampling = info.get("subsampling", "4:2:0") + speed = info.get("speed", 6) + codec = info.get("codec", "auto") + range_ = info.get("range", "full") + tile_rows_log2 = info.get("tile_rows", 0) + tile_cols_log2 = info.get("tile_cols", 0) + alpha_premultiplied = bool(info.get("alpha_premultiplied", False)) + 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", im.info.get("exif")) + if isinstance(exif, Image.Exif): + exif = exif.tobytes() + xmp = info.get("xmp", im.info.get("xmp") or im.info.get("XML:com.adobe.xmp")) + + if isinstance(xmp, str): + xmp = xmp.encode("utf-8") + + advanced = info.get("advanced") + if isinstance(advanced, dict): + advanced = tuple([k, v] for (k, v) in advanced.items()) + if advanced is not None: + try: + advanced = tuple(advanced) + except TypeError: + invalid = True + else: + invalid = all(isinstance(v, tuple) and len(v) == 2 for v in advanced) + if invalid: + msg = ( + "advanced codec options must be a dict of key-value string " + "pairs or a series of key-value two-tuples" + ) + raise ValueError(msg) + advanced = tuple( + [(str(k).encode("utf-8"), str(v).encode("utf-8")) for k, v in advanced] + ) + + # Setup the AVIF encoder + enc = _avif.AvifEncoder( + im.size[0], + im.size[1], + subsampling, + qmin, + qmax, + quality, + speed, + codec, + range_, + tile_rows_log2, + tile_cols_log2, + alpha_premultiplied, + autotiling, + icc_profile or b"", + exif or b"", + xmp or b"", + advanced, + ) + + # Add each frame + frame_idx = 0 + frame_dur = 0 + cur_idx = im.tell() + try: + for ims in [im] + append_images: + # Get # of frames in this image + nfr = getattr(ims, "n_frames", 1) + + for idx in range(nfr): + ims.seek(idx) + ims.load() + + # Make sure image mode is supported + frame = ims + rawmode = ims.mode + if ims.mode not in _VALID_AVIF_MODES: + alpha = ( + "A" in ims.mode + or "a" in ims.mode + or (ims.mode == "P" and "A" in ims.im.getpalettemode()) + ) + rawmode = "RGBA" if alpha else "RGB" + frame = ims.convert(rawmode) + + # Update frame duration + if isinstance(duration, (list, tuple)): + frame_dur = duration[frame_idx] + else: + frame_dur = duration + + # Append the frame to the animation encoder + enc.add( + frame.tobytes("raw", rawmode), + frame_dur, + frame.size[0], + frame.size[1], + rawmode, + is_single_frame, + ) + + # Update frame index + frame_idx += 1 + + if not save_all: + break + + finally: + im.seek(cur_idx) + + # Get the final output from the encoder + data = enc.finish() + if data is None: + msg = "cannot write file as AVIF (encoder returned None)" + raise OSError(msg) + + fp.write(data) + + +Image.register_open(AvifImageFile.format, AvifImageFile, _accept) +if SUPPORTED: + Image.register_save(AvifImageFile.format, _save) + Image.register_save_all(AvifImageFile.format, _save_all) + Image.register_extensions(AvifImageFile.format, [".avif", ".avifs"]) + Image.register_mime(AvifImageFile.format, "image/avif") diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 2a6b4646bbd..40ef526dc0d 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1442,7 +1442,9 @@ def getexif(self): # XMP tags if ExifTags.Base.Orientation not in self._exif: - xmp_tags = self.info.get("XML:com.adobe.xmp") + xmp_tags = self.info.get("XML:com.adobe.xmp") or self.info.get("xmp") + if isinstance(xmp_tags, bytes): + xmp_tags = xmp_tags.decode("utf-8") if xmp_tags: match = re.search(r'tiff:Orientation(="|>)([0-9])', xmp_tags) if match: diff --git a/src/PIL/__init__.py b/src/PIL/__init__.py index 2bb8f6d7f10..a13ec83f967 100644 --- a/src/PIL/__init__.py +++ b/src/PIL/__init__.py @@ -23,6 +23,7 @@ _plugins = [ + "AvifImagePlugin", "BlpImagePlugin", "BmpImagePlugin", "BufrStubImagePlugin", diff --git a/src/PIL/features.py b/src/PIL/features.py index f14e60cf5d4..af5a09dc58f 100644 --- a/src/PIL/features.py +++ b/src/PIL/features.py @@ -13,6 +13,7 @@ "freetype2": ("PIL._imagingft", "freetype2_version"), "littlecms2": ("PIL._imagingcms", "littlecms_version"), "webp": ("PIL._webp", "webpdecoder_version"), + "avif": ("PIL._avif", "libavif_version"), } @@ -263,6 +264,7 @@ def pilinfo(out=None, supported_formats=True): ("freetype2", "FREETYPE2"), ("littlecms2", "LITTLECMS2"), ("webp", "WEBP"), + ("avif", "AVIF"), ("transp_webp", "WEBP Transparency"), ("webp_mux", "WEBPMUX"), ("webp_anim", "WEBP Animation"), diff --git a/src/_avif.c b/src/_avif.c new file mode 100644 index 00000000000..55a592c1a14 --- /dev/null +++ b/src/_avif.c @@ -0,0 +1,908 @@ +#define PY_SSIZE_T_CLEAN + +#include +#include "avif/avif.h" + +#if AVIF_VERSION < 80300 +#define AVIF_CHROMA_UPSAMPLING_AUTOMATIC AVIF_CHROMA_UPSAMPLING_BILINEAR +#define AVIF_CHROMA_UPSAMPLING_BEST_QUALITY AVIF_CHROMA_UPSAMPLING_BILINEAR +#define AVIF_CHROMA_UPSAMPLING_FASTEST AVIF_CHROMA_UPSAMPLING_NEAREST +#endif + +typedef struct { + avifPixelFormat subsampling; + int qmin; + int qmax; + int quality; + int speed; + avifCodecChoice codec; + avifRange range; + avifBool alpha_premultiplied; + int tile_rows_log2; + int tile_cols_log2; + avifBool autotiling; +} avifEncOptions; + +// Encoder type +typedef struct { + PyObject_HEAD + avifEncoder *encoder; + avifImage *image; + PyObject *icc_bytes; + PyObject *exif_bytes; + PyObject *xmp_bytes; + int frame_index; +} AvifEncoderObject; + +static PyTypeObject AvifEncoder_Type; + +// Decoder type +typedef struct { + PyObject_HEAD + avifDecoder *decoder; + PyObject *data; + char *mode; +} AvifDecoderObject; + +static PyTypeObject AvifDecoder_Type; + +static int max_threads = 0; + +static void +init_max_threads(void) { + PyObject *os = NULL; + PyObject *n = NULL; + long num_cpus; + + os = PyImport_ImportModule("os"); + if (os == NULL) { + goto error; + } + + if (PyObject_HasAttrString(os, "sched_getaffinity")) { + n = PyObject_CallMethod(os, "sched_getaffinity", "i", 0); + if (n == NULL) { + goto error; + } + num_cpus = PySet_Size(n); + } else { + n = PyObject_CallMethod(os, "cpu_count", NULL); + if (n == NULL) { + goto error; + } + num_cpus = PyLong_AsLong(n); + } + + if (num_cpus < 1) { + goto error; + } + + max_threads = (int)num_cpus; + +done: + Py_XDECREF(os); + Py_XDECREF(n); + return; + +error: + if (PyErr_Occurred()) { + PyErr_Clear(); + } + PyErr_WarnEx( + PyExc_RuntimeWarning, "could not get cpu count: using max_threads=1", 1); + goto done; +} + +static int +normalize_quantize_value(int qvalue) { + if (qvalue < AVIF_QUANTIZER_BEST_QUALITY) { + return AVIF_QUANTIZER_BEST_QUALITY; + } else if (qvalue > AVIF_QUANTIZER_WORST_QUALITY) { + return AVIF_QUANTIZER_WORST_QUALITY; + } else { + return qvalue; + } +} + +static int +normalize_tiles_log2(int value) { + if (value < 0) { + return 0; + } else if (value > 6) { + return 6; + } else { + return value; + } +} + +static PyObject * +exc_type_for_avif_result(avifResult result) { + switch (result) { + case AVIF_RESULT_INVALID_EXIF_PAYLOAD: + case AVIF_RESULT_INVALID_CODEC_SPECIFIC_OPTION: + return PyExc_ValueError; + case AVIF_RESULT_INVALID_FTYP: + case AVIF_RESULT_BMFF_PARSE_FAILED: + case AVIF_RESULT_TRUNCATED_DATA: + case AVIF_RESULT_NO_CONTENT: + return PyExc_SyntaxError; + default: + return PyExc_RuntimeError; + } +} + +static int +_codec_available(const char *name, uint32_t flags) { + avifCodecChoice codec = avifCodecChoiceFromName(name); + if (codec == AVIF_CODEC_CHOICE_AUTO) { + return 0; + } + const char *codec_name = avifCodecName(codec, flags); + return (codec_name == NULL) ? 0 : 1; +} + +PyObject * +_decoder_codec_available(PyObject *self, PyObject *args) { + char *codec_name; + if (!PyArg_ParseTuple(args, "s", &codec_name)) { + return NULL; + } + int is_available = _codec_available(codec_name, AVIF_CODEC_FLAG_CAN_DECODE); + return PyBool_FromLong(is_available); +} + +PyObject * +_encoder_codec_available(PyObject *self, PyObject *args) { + char *codec_name; + if (!PyArg_ParseTuple(args, "s", &codec_name)) { + return NULL; + } + int is_available = _codec_available(codec_name, AVIF_CODEC_FLAG_CAN_ENCODE); + return PyBool_FromLong(is_available); +} + +static void +_add_codec_specific_options(avifEncoder *encoder, PyObject *opts) { + Py_ssize_t i, size; + PyObject *keyval, *py_key, *py_val; + char *key, *val; + if (!PyTuple_Check(opts)) { + return; + } + 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) { + return; + } + py_key = PyTuple_GetItem(keyval, 0); + py_val = PyTuple_GetItem(keyval, 1); + if (!PyBytes_Check(py_key) || !PyBytes_Check(py_val)) { + return; + } + key = PyBytes_AsString(py_key); + val = PyBytes_AsString(py_val); + avifEncoderSetCodecSpecificOption(encoder, key, val); + } +} + +// Encoder functions +PyObject * +AvifEncoderNew(PyObject *self_, PyObject *args) { + unsigned int width, height; + avifEncOptions enc_options; + AvifEncoderObject *self = NULL; + avifEncoder *encoder = NULL; + + char *subsampling = "4:2:0"; + int qmin = AVIF_QUANTIZER_BEST_QUALITY; // =0 + int qmax = 10; // "High Quality", but not lossless + int quality = 75; + int speed = 8; + PyObject *icc_bytes; + PyObject *exif_bytes; + PyObject *xmp_bytes; + PyObject *alpha_premultiplied = NULL; + PyObject *autotiling = NULL; + int tile_rows_log2 = 0; + int tile_cols_log2 = 0; + + char *codec = "auto"; + char *range = "full"; + + PyObject *advanced; + + if (!PyArg_ParseTuple( + args, + "IIsiiiissiiOOSSSO", + &width, + &height, + &subsampling, + &qmin, + &qmax, + &quality, + &speed, + &codec, + &range, + &tile_rows_log2, + &tile_cols_log2, + &alpha_premultiplied, + &autotiling, + &icc_bytes, + &exif_bytes, + &xmp_bytes, + &advanced)) { + return NULL; + } + + if (strcmp(subsampling, "4:0:0") == 0) { + enc_options.subsampling = AVIF_PIXEL_FORMAT_YUV400; + } else if (strcmp(subsampling, "4:2:0") == 0) { + enc_options.subsampling = AVIF_PIXEL_FORMAT_YUV420; + } else if (strcmp(subsampling, "4:2:2") == 0) { + enc_options.subsampling = AVIF_PIXEL_FORMAT_YUV422; + } else if (strcmp(subsampling, "4:4:4") == 0) { + enc_options.subsampling = AVIF_PIXEL_FORMAT_YUV444; + } else { + PyErr_Format(PyExc_ValueError, "Invalid subsampling: %s", subsampling); + return NULL; + } + + enc_options.qmin = normalize_quantize_value(qmin); + enc_options.qmax = normalize_quantize_value(qmax); + enc_options.quality = quality; + + if (speed < AVIF_SPEED_SLOWEST) { + speed = AVIF_SPEED_SLOWEST; + } else if (speed > AVIF_SPEED_FASTEST) { + speed = AVIF_SPEED_FASTEST; + } + enc_options.speed = speed; + + if (strcmp(codec, "auto") == 0) { + enc_options.codec = AVIF_CODEC_CHOICE_AUTO; + } else { + enc_options.codec = avifCodecChoiceFromName(codec); + if (enc_options.codec == AVIF_CODEC_CHOICE_AUTO) { + PyErr_Format(PyExc_ValueError, "Invalid codec: %s", codec); + return NULL; + } else { + const char *codec_name = + avifCodecName(enc_options.codec, AVIF_CODEC_FLAG_CAN_ENCODE); + if (codec_name == NULL) { + PyErr_Format(PyExc_ValueError, "AV1 Codec cannot encode: %s", codec); + return NULL; + } + } + } + + if (strcmp(range, "full") == 0) { + enc_options.range = AVIF_RANGE_FULL; + } else if (strcmp(range, "limited") == 0) { + enc_options.range = AVIF_RANGE_LIMITED; + } else { + PyErr_SetString(PyExc_ValueError, "Invalid range"); + return NULL; + } + + // Validate canvas dimensions + if (width <= 0 || height <= 0) { + PyErr_SetString(PyExc_ValueError, "invalid canvas dimensions"); + return NULL; + } + + enc_options.tile_rows_log2 = normalize_tiles_log2(tile_rows_log2); + enc_options.tile_cols_log2 = normalize_tiles_log2(tile_cols_log2); + + if (alpha_premultiplied == Py_True) { + enc_options.alpha_premultiplied = AVIF_TRUE; + } else { + enc_options.alpha_premultiplied = AVIF_FALSE; + } + + enc_options.autotiling = (autotiling == Py_True) ? AVIF_TRUE : AVIF_FALSE; + + // Create a new animation encoder and picture frame + self = PyObject_New(AvifEncoderObject, &AvifEncoder_Type); + if (self) { + self->icc_bytes = NULL; + self->exif_bytes = NULL; + self->xmp_bytes = NULL; + + encoder = avifEncoderCreate(); + + if (max_threads == 0) { + init_max_threads(); + } + + encoder->maxThreads = max_threads; +#if AVIF_VERSION >= 1000000 + encoder->quality = enc_options.quality; +#else + encoder->minQuantizer = enc_options.qmin; + encoder->maxQuantizer = enc_options.qmax; +#endif + encoder->codecChoice = enc_options.codec; + encoder->speed = enc_options.speed; + encoder->timescale = (uint64_t)1000; + encoder->tileRowsLog2 = enc_options.tile_rows_log2; + encoder->tileColsLog2 = enc_options.tile_cols_log2; + +#if AVIF_VERSION >= 110000 + encoder->autoTiling = enc_options.autotiling; +#endif + +#if AVIF_VERSION >= 80200 + _add_codec_specific_options(encoder, advanced); +#endif + + self->encoder = encoder; + + avifImage *image = avifImageCreateEmpty(); + // Set these in advance so any upcoming RGB -> YUV use the proper coefficients + image->yuvRange = enc_options.range; + image->yuvFormat = enc_options.subsampling; + image->colorPrimaries = AVIF_COLOR_PRIMARIES_UNSPECIFIED; + image->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED; + image->matrixCoefficients = AVIF_MATRIX_COEFFICIENTS_BT601; + image->width = width; + image->height = height; + image->depth = 8; +#if AVIF_VERSION >= 90000 + image->alphaPremultiplied = enc_options.alpha_premultiplied; +#endif + + if (PyBytes_GET_SIZE(icc_bytes)) { + self->icc_bytes = icc_bytes; + Py_INCREF(icc_bytes); + avifImageSetProfileICC( + image, + (uint8_t *)PyBytes_AS_STRING(icc_bytes), + PyBytes_GET_SIZE(icc_bytes)); + } else { + image->colorPrimaries = AVIF_COLOR_PRIMARIES_BT709; + image->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_SRGB; + } + + if (PyBytes_GET_SIZE(exif_bytes)) { + self->exif_bytes = exif_bytes; + Py_INCREF(exif_bytes); + avifImageSetMetadataExif( + image, + (uint8_t *)PyBytes_AS_STRING(exif_bytes), + PyBytes_GET_SIZE(exif_bytes)); + } + if (PyBytes_GET_SIZE(xmp_bytes)) { + self->xmp_bytes = xmp_bytes; + Py_INCREF(xmp_bytes); + avifImageSetMetadataXMP( + image, + (uint8_t *)PyBytes_AS_STRING(xmp_bytes), + PyBytes_GET_SIZE(xmp_bytes)); + } + + self->image = image; + self->frame_index = -1; + + return (PyObject *)self; + } + PyErr_SetString(PyExc_RuntimeError, "could not create encoder object"); + return NULL; +} + +PyObject * +_encoder_dealloc(AvifEncoderObject *self) { + if (self->encoder) { + avifEncoderDestroy(self->encoder); + } + if (self->image) { + avifImageDestroy(self->image); + } + Py_XDECREF(self->icc_bytes); + Py_XDECREF(self->exif_bytes); + Py_XDECREF(self->xmp_bytes); + Py_RETURN_NONE; +} + +PyObject * +_encoder_add(AvifEncoderObject *self, PyObject *args) { + uint8_t *rgb_bytes; + Py_ssize_t size; + unsigned int duration; + unsigned int width; + unsigned int height; + char *mode; + PyObject *is_single_frame = NULL; + PyObject *ret = Py_None; + + int is_first_frame; + int channels; + avifRGBImage rgb; + avifResult result; + + avifEncoder *encoder = self->encoder; + avifImage *image = self->image; + avifImage *frame = NULL; + + if (!PyArg_ParseTuple( + args, + "z#IIIsO", + (char **)&rgb_bytes, + &size, + &duration, + &width, + &height, + &mode, + &is_single_frame)) { + return NULL; + } + + is_first_frame = (self->frame_index == -1); + + if ((image->width != width) || (image->height != height)) { + PyErr_Format( + PyExc_ValueError, + "Image sequence dimensions mismatch, %ux%u != %ux%u", + image->width, + image->height, + width, + height); + return NULL; + } + + if (is_first_frame) { + // If we don't have an image populated with yuv planes, this is the first frame + frame = image; + } else { + frame = avifImageCreateEmpty(); + + frame->colorPrimaries = image->colorPrimaries; + frame->transferCharacteristics = image->transferCharacteristics; + frame->matrixCoefficients = image->matrixCoefficients; + frame->yuvRange = image->yuvRange; + frame->yuvFormat = image->yuvFormat; + frame->depth = image->depth; +#if AVIF_VERSION >= 90000 + frame->alphaPremultiplied = image->alphaPremultiplied; +#endif + } + + frame->width = width; + frame->height = height; + + memset(&rgb, 0, sizeof(avifRGBImage)); + + avifRGBImageSetDefaults(&rgb, frame); + rgb.depth = 8; + + if (strcmp(mode, "RGBA") == 0) { + rgb.format = AVIF_RGB_FORMAT_RGBA; + channels = 4; + } else { + rgb.format = AVIF_RGB_FORMAT_RGB; + channels = 3; + } + + avifRGBImageAllocatePixels(&rgb); + + if (rgb.rowBytes * rgb.height != size) { + PyErr_Format( + PyExc_RuntimeError, + "rgb data is incorrect size: %u * %u (%u) != %u", + rgb.rowBytes, + rgb.height, + rgb.rowBytes * rgb.height, + size); + ret = NULL; + goto end; + } + + // rgb.pixels is safe for writes + memcpy(rgb.pixels, rgb_bytes, size); + + Py_BEGIN_ALLOW_THREADS + result = avifImageRGBToYUV(frame, &rgb); + Py_END_ALLOW_THREADS + + if (result != AVIF_RESULT_OK) { + PyErr_Format( + exc_type_for_avif_result(result), + "Conversion to YUV failed: %s", + avifResultToString(result)); + ret = NULL; + goto end; + } + + uint32_t addImageFlags = AVIF_ADD_IMAGE_FLAG_NONE; + if (PyObject_IsTrue(is_single_frame)) { + addImageFlags |= AVIF_ADD_IMAGE_FLAG_SINGLE; + } + + Py_BEGIN_ALLOW_THREADS + result = avifEncoderAddImage(encoder, frame, duration, addImageFlags); + Py_END_ALLOW_THREADS + + if (result != AVIF_RESULT_OK) { + PyErr_Format( + exc_type_for_avif_result(result), + "Failed to encode image: %s", + avifResultToString(result)); + ret = NULL; + goto end; + } + +end: + avifRGBImageFreePixels(&rgb); + if (!is_first_frame) { + avifImageDestroy(frame); + } + + if (ret == Py_None) { + self->frame_index++; + Py_RETURN_NONE; + } else { + return ret; + } +} + +PyObject * +_encoder_finish(AvifEncoderObject *self) { + avifEncoder *encoder = self->encoder; + + avifRWData raw = AVIF_DATA_EMPTY; + avifResult result; + PyObject *ret = NULL; + + Py_BEGIN_ALLOW_THREADS + result = avifEncoderFinish(encoder, &raw); + Py_END_ALLOW_THREADS + + if (result != AVIF_RESULT_OK) { + PyErr_Format( + exc_type_for_avif_result(result), + "Failed to finish encoding: %s", + avifResultToString(result)); + avifRWDataFree(&raw); + return NULL; + } + + ret = PyBytes_FromStringAndSize((char *)raw.data, raw.size); + + avifRWDataFree(&raw); + + return ret; +} + +// Decoder functions +PyObject * +AvifDecoderNew(PyObject *self_, PyObject *args) { + PyObject *avif_bytes; + AvifDecoderObject *self = NULL; + + char *upsampling_str; + char *codec_str; + avifCodecChoice codec; + avifChromaUpsampling upsampling; + + avifResult result; + + if (!PyArg_ParseTuple(args, "Sss", &avif_bytes, &codec_str, &upsampling_str)) { + return NULL; + } + + if (!strcmp(upsampling_str, "auto")) { + upsampling = AVIF_CHROMA_UPSAMPLING_AUTOMATIC; + } else if (!strcmp(upsampling_str, "fastest")) { + upsampling = AVIF_CHROMA_UPSAMPLING_FASTEST; + } else if (!strcmp(upsampling_str, "best")) { + upsampling = AVIF_CHROMA_UPSAMPLING_BEST_QUALITY; + } else if (!strcmp(upsampling_str, "nearest")) { + upsampling = AVIF_CHROMA_UPSAMPLING_NEAREST; + } else if (!strcmp(upsampling_str, "bilinear")) { + upsampling = AVIF_CHROMA_UPSAMPLING_BILINEAR; + } else { + PyErr_Format(PyExc_ValueError, "Invalid upsampling option: %s", upsampling_str); + return NULL; + } + + if (strcmp(codec_str, "auto") == 0) { + codec = AVIF_CODEC_CHOICE_AUTO; + } else { + codec = avifCodecChoiceFromName(codec_str); + if (codec == AVIF_CODEC_CHOICE_AUTO) { + PyErr_Format(PyExc_ValueError, "Invalid codec: %s", codec_str); + return NULL; + } else { + const char *codec_name = avifCodecName(codec, AVIF_CODEC_FLAG_CAN_DECODE); + if (codec_name == NULL) { + PyErr_Format( + PyExc_ValueError, "AV1 Codec cannot decode: %s", codec_str); + return NULL; + } + } + } + + self = PyObject_New(AvifDecoderObject, &AvifDecoder_Type); + if (!self) { + PyErr_SetString(PyExc_RuntimeError, "could not create decoder object"); + return NULL; + } + self->decoder = NULL; + + Py_INCREF(avif_bytes); + self->data = avif_bytes; + + self->decoder = avifDecoderCreate(); +#if AVIF_VERSION >= 80400 + if (max_threads == 0) { + init_max_threads(); + } + self->decoder->maxThreads = max_threads; +#endif + self->decoder->codecChoice = codec; + + avifDecoderSetIOMemory( + self->decoder, + (uint8_t *)PyBytes_AS_STRING(self->data), + PyBytes_GET_SIZE(self->data)); + + result = avifDecoderParse(self->decoder); + + if (result != AVIF_RESULT_OK) { + PyErr_Format( + exc_type_for_avif_result(result), + "Failed to decode image: %s", + avifResultToString(result)); + avifDecoderDestroy(self->decoder); + self->decoder = NULL; + Py_DECREF(self); + return NULL; + } + + if (self->decoder->alphaPresent) { + self->mode = "RGBA"; + } else { + self->mode = "RGB"; + } + + return (PyObject *)self; +} + +PyObject * +_decoder_dealloc(AvifDecoderObject *self) { + if (self->decoder) { + avifDecoderDestroy(self->decoder); + } + Py_XDECREF(self->data); + Py_RETURN_NONE; +} + +PyObject * +_decoder_get_info(AvifDecoderObject *self) { + avifDecoder *decoder = self->decoder; + avifImage *image = decoder->image; + + PyObject *icc = NULL; + PyObject *exif = NULL; + PyObject *xmp = NULL; + PyObject *ret = NULL; + + if (image->xmp.size) { + xmp = PyBytes_FromStringAndSize((const char *)image->xmp.data, image->xmp.size); + } + + if (image->exif.size) { + exif = + PyBytes_FromStringAndSize((const char *)image->exif.data, image->exif.size); + } + + if (image->icc.size) { + icc = PyBytes_FromStringAndSize((const char *)image->icc.data, image->icc.size); + } + + ret = Py_BuildValue( + "IIIsSSS", + image->width, + image->height, + decoder->imageCount, + self->mode, + NULL == icc ? Py_None : icc, + NULL == exif ? Py_None : exif, + NULL == xmp ? Py_None : xmp); + + Py_XDECREF(xmp); + Py_XDECREF(exif); + Py_XDECREF(icc); + + return ret; +} + +PyObject * +_decoder_get_frame(AvifDecoderObject *self, PyObject *args) { + PyObject *bytes; + PyObject *ret; + Py_ssize_t size; + avifResult result; + avifRGBImage rgb; + avifDecoder *decoder; + avifImage *image; + uint32_t frame_index; + uint32_t row_bytes; + + decoder = self->decoder; + + if (!PyArg_ParseTuple(args, "I", &frame_index)) { + return NULL; + } + + result = avifDecoderNthImage(decoder, frame_index); + + if (result != AVIF_RESULT_OK) { + PyErr_Format( + exc_type_for_avif_result(result), + "Failed to decode frame %u: %s", + decoder->imageIndex + 1, + avifResultToString(result)); + return NULL; + } + + image = decoder->image; + + memset(&rgb, 0, sizeof(rgb)); + avifRGBImageSetDefaults(&rgb, image); + + rgb.depth = 8; + + if (decoder->alphaPresent) { + rgb.format = AVIF_RGB_FORMAT_RGBA; + } else { + rgb.format = AVIF_RGB_FORMAT_RGB; + rgb.ignoreAlpha = AVIF_TRUE; + } + + 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; + } + + avifRGBImageAllocatePixels(&rgb); + + 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), + "Conversion from YUV failed: %s", + avifResultToString(result)); + avifRGBImageFreePixels(&rgb); + return NULL; + } + + size = rgb.rowBytes * rgb.height; + + bytes = PyBytes_FromStringAndSize((char *)rgb.pixels, size); + avifRGBImageFreePixels(&rgb); + + ret = Py_BuildValue( + "SKKK", + bytes, + decoder->timescale, + decoder->imageTiming.ptsInTimescales, + decoder->imageTiming.durationInTimescales); + + Py_DECREF(bytes); + + return ret; +} + +/* -------------------------------------------------------------------- */ +/* Type Definitions */ +/* -------------------------------------------------------------------- */ + +// AvifEncoder methods +static struct PyMethodDef _encoder_methods[] = { + {"add", (PyCFunction)_encoder_add, METH_VARARGS}, + {"finish", (PyCFunction)_encoder_finish, METH_NOARGS}, + {NULL, NULL} /* sentinel */ +}; + +// AvifDecoder type definition +static PyTypeObject AvifEncoder_Type = { + // clang-format off + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name = "AvifEncoder", + // clang-format on + .tp_basicsize = sizeof(AvifEncoderObject), + .tp_dealloc = (destructor)_encoder_dealloc, + .tp_flags = Py_TPFLAGS_DEFAULT, + .tp_methods = _encoder_methods, +}; + +// AvifDecoder methods +static struct PyMethodDef _decoder_methods[] = { + {"get_info", (PyCFunction)_decoder_get_info, METH_NOARGS}, + {"get_frame", (PyCFunction)_decoder_get_frame, METH_VARARGS}, + {NULL, NULL} /* sentinel */ +}; + +// AvifDecoder type definition +static PyTypeObject AvifDecoder_Type = { + // clang-format off + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name = "AvifDecoder", + // clang-format on + .tp_basicsize = sizeof(AvifDecoderObject), + .tp_itemsize = 0, + .tp_dealloc = (destructor)_decoder_dealloc, + .tp_flags = Py_TPFLAGS_DEFAULT, + .tp_methods = _decoder_methods, +}; + +PyObject * +AvifCodecVersions() { + char codecVersions[256]; + avifCodecVersions(codecVersions); + return PyUnicode_FromString(codecVersions); +} + +/* -------------------------------------------------------------------- */ +/* Module Setup */ +/* -------------------------------------------------------------------- */ + +static PyMethodDef avifMethods[] = { + {"AvifDecoder", AvifDecoderNew, METH_VARARGS}, + {"AvifEncoder", AvifEncoderNew, METH_VARARGS}, + {"AvifCodecVersions", AvifCodecVersions, METH_NOARGS}, + {"decoder_codec_available", _decoder_codec_available, METH_VARARGS}, + {"encoder_codec_available", _encoder_codec_available, METH_VARARGS}, + {NULL, NULL}}; + +static int +setup_module(PyObject *m) { + PyObject *d = PyModule_GetDict(m); + + PyObject *v = PyUnicode_FromString(avifVersion()); + if (PyDict_SetItemString(d, "libavif_version", v) < 0) { + Py_DECREF(v); + return -1; + } + Py_DECREF(v); + + v = Py_BuildValue( + "(iii)", AVIF_VERSION_MAJOR, AVIF_VERSION_MINOR, AVIF_VERSION_PATCH); + + if (PyDict_SetItemString(d, "VERSION", v) < 0) { + Py_DECREF(v); + return -1; + } + Py_DECREF(v); + + if (PyType_Ready(&AvifDecoder_Type) < 0 || PyType_Ready(&AvifEncoder_Type) < 0) { + return -1; + } + return 0; +} + +PyMODINIT_FUNC +PyInit__avif(void) { + PyObject *m; + + static PyModuleDef module_def = { + PyModuleDef_HEAD_INIT, + .m_name = "_avif", + .m_size = -1, + .m_methods = avifMethods, + }; + + m = PyModule_Create(&module_def); + if (setup_module(m) < 0) { + return NULL; + } + + return m; +} diff --git a/winbuild/build.rst b/winbuild/build.rst index a8e4ebaa6cc..ae746fbc3eb 100644 --- a/winbuild/build.rst +++ b/winbuild/build.rst @@ -59,6 +59,7 @@ Run ``build_prepare.py`` to configure the build:: build architecture (default: same as host Python) --nmake build dependencies using NMake instead of Ninja --no-imagequant skip GPL-licensed optional dependency libimagequant + --no-avif skip optional dependency libavif --no-fribidi, --no-raqm skip LGPL-licensed optional dependency FriBiDi diff --git a/winbuild/build_prepare.py b/winbuild/build_prepare.py index 776173e1caf..1dcf4962e30 100644 --- a/winbuild/build_prepare.py +++ b/winbuild/build_prepare.py @@ -38,6 +38,11 @@ def cmd_rmdir(path: str) -> str: return f'rmdir /S /Q "{path}"' +def cmd_lib_combine(outfile: str, *libfiles) -> str: + params = " ".join(['"%s"' % f for f in libfiles]) + return "LIB.EXE /OUT:{outfile} {params}".format(outfile=outfile, params=params) + + def cmd_nmake( makefile: str | None = None, target: str = "", @@ -365,6 +370,39 @@ def cmd_msbuild( ], "bins": [r"*.dll"], }, + "libavif": { + "url": "https://github.com/AOMediaCodec/libavif/archive/v1.0.1.zip", + "filename": "libavif-1.0.1.zip", + "dir": "libavif-1.0.1", + "license": "LICENSE", + "build": [ + cmd_cd("ext"), + cmd_rmdir("aom"), + 'cmd.exe /c "aom.cmd"', + cmd_rmdir("dav1d"), + 'cmd.exe /c "dav1d.cmd"', + cmd_cd(".."), + *cmds_cmake( + "avif", + "-DBUILD_SHARED_LIBS=OFF", + "-DAVIF_CODEC_AOM=ON", + "-DAVIF_LOCAL_AOM=ON", + "-DAVIF_CODEC_DAV1D=ON", + "-DAVIF_LOCAL_DAV1D=ON", + ), + cmd_nmake(), + cmd_lib_combine( + r"avif_combined.lib", + r"avif.lib", + r"ext\aom\build.libavif\aom.lib", + r"ext\dav1d\build\src\libdav1d.a", + ), + cmd_copy(r"avif_combined.lib", r"avif.lib"), + cmd_mkdir(r"{inc_dir}\avif"), + cmd_copy(r"include\avif\avif.h", r"{inc_dir}\avif"), + ], + "libs": [r"avif.lib"], + }, } @@ -651,6 +689,11 @@ def build_dep_all() -> None: action="store_true", help="skip LGPL-licensed optional dependency FriBiDi", ) + parser.add_argument( + "--no-avif", + action="store_true", + help="skip optional dependency libavif", + ) args = parser.parse_args() arch_prefs = ARCHITECTURES[args.architecture] @@ -691,6 +734,8 @@ def build_dep_all() -> None: disabled += ["libimagequant"] if args.no_fribidi: disabled += ["fribidi"] + if args.no_avif: + disabled += ["libavif"] prefs = { "architecture": args.architecture,