Skip to content

Commit 3914241

Browse files
committed
Moved codec availability errors to Python
1 parent 0835ce7 commit 3914241

File tree

2 files changed

+8
-23
lines changed

2 files changed

+8
-23
lines changed

src/PIL/AvifImagePlugin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ def _open(self) -> None:
7474
)
7575
raise SyntaxError(msg)
7676

77+
if DECODE_CODEC_CHOICE != "auto" and not _avif.decoder_codec_available(
78+
DECODE_CODEC_CHOICE
79+
):
80+
msg = "Invalid opening codec"
81+
raise ValueError(msg)
7782
self._decoder = _avif.AvifDecoder(
7883
self.fp.read(),
7984
DECODE_CODEC_CHOICE,
@@ -153,6 +158,9 @@ def _save(
153158
speed = info.get("speed", 6)
154159
max_threads = info.get("max_threads", _get_default_max_threads())
155160
codec = info.get("codec", "auto")
161+
if codec != "auto" and not _avif.encoder_codec_available(codec):
162+
msg = "Invalid saving codec"
163+
raise ValueError(msg)
156164
range_ = info.get("range", "full")
157165
tile_rows_log2 = info.get("tile_rows", 0)
158166
tile_cols_log2 = info.get("tile_cols", 0)

src/_avif.c

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,6 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
235235
enc_options.codec = AVIF_CODEC_CHOICE_AUTO;
236236
} else {
237237
enc_options.codec = avifCodecChoiceFromName(codec);
238-
if (enc_options.codec == AVIF_CODEC_CHOICE_AUTO) {
239-
PyErr_Format(PyExc_ValueError, "Invalid codec: %s", codec);
240-
return NULL;
241-
} else {
242-
const char *codec_name =
243-
avifCodecName(enc_options.codec, AVIF_CODEC_FLAG_CAN_ENCODE);
244-
if (codec_name == NULL) {
245-
PyErr_Format(PyExc_ValueError, "AV1 Codec cannot encode: %s", codec);
246-
return NULL;
247-
}
248-
}
249238
}
250239

251240
if (strcmp(range, "full") == 0) {
@@ -619,18 +608,6 @@ AvifDecoderNew(PyObject *self_, PyObject *args) {
619608
codec = AVIF_CODEC_CHOICE_AUTO;
620609
} else {
621610
codec = avifCodecChoiceFromName(codec_str);
622-
if (codec == AVIF_CODEC_CHOICE_AUTO) {
623-
PyErr_Format(PyExc_ValueError, "Invalid codec: %s", codec_str);
624-
return NULL;
625-
} else {
626-
const char *codec_name = avifCodecName(codec, AVIF_CODEC_FLAG_CAN_DECODE);
627-
if (codec_name == NULL) {
628-
PyErr_Format(
629-
PyExc_ValueError, "AV1 Codec cannot decode: %s", codec_str
630-
);
631-
return NULL;
632-
}
633-
}
634611
}
635612

636613
self = PyObject_New(AvifDecoderObject, &AvifDecoder_Type);

0 commit comments

Comments
 (0)