From 8696eab7c6a7069fa87b5a3c7fd57b22d1c6e3c9 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 29 Nov 2024 20:43:24 +1100 Subject: [PATCH] Moved codec availability errors to Python --- src/PIL/AvifImagePlugin.py | 8 ++++++++ src/_avif.c | 23 ----------------------- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/src/PIL/AvifImagePlugin.py b/src/PIL/AvifImagePlugin.py index 137a2642557..c92e2534ef9 100644 --- a/src/PIL/AvifImagePlugin.py +++ b/src/PIL/AvifImagePlugin.py @@ -74,6 +74,11 @@ def _open(self) -> None: ) raise SyntaxError(msg) + if DECODE_CODEC_CHOICE != "auto" and not _avif.decoder_codec_available( + DECODE_CODEC_CHOICE + ): + msg = "Invalid opening codec" + raise ValueError(msg) self._decoder = _avif.AvifDecoder( self.fp.read(), DECODE_CODEC_CHOICE, @@ -153,6 +158,9 @@ def _save( speed = info.get("speed", 6) max_threads = info.get("max_threads", _get_default_max_threads()) codec = info.get("codec", "auto") + if codec != "auto" and not _avif.encoder_codec_available(codec): + msg = "Invalid saving codec" + raise ValueError(msg) range_ = info.get("range", "full") tile_rows_log2 = info.get("tile_rows", 0) tile_cols_log2 = info.get("tile_cols", 0) diff --git a/src/_avif.c b/src/_avif.c index 21b3ee72266..1d4bb74010e 100644 --- a/src/_avif.c +++ b/src/_avif.c @@ -349,17 +349,6 @@ AvifEncoderNew(PyObject *self_, PyObject *args) { 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) { @@ -734,18 +723,6 @@ AvifDecoderNew(PyObject *self_, PyObject *args) { 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);