Skip to content

Commit

Permalink
Allow users to pass max_threads to the avif encoder via Image.save (#54)
Browse files Browse the repository at this point in the history
* Allow users to pass max_threads to the avif encoder via Image.save
* Distinguish global default max threads from encoder param
* Allow overriding DEFAULT_MAX_THREADS on AvifImagePlugin module

---------

Co-authored-by: Wyatt Borsos <[email protected]>
  • Loading branch information
fdintino and yit-b authored Jul 3, 2024
1 parent 48e041e commit a6d6dd1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/pillow_avif/AvifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# to Image.open (see https://github.com/python-pillow/Pillow/issues/569)
DECODE_CODEC_CHOICE = "auto"
CHROMA_UPSAMPLING = "auto"
DEFAULT_MAX_THREADS = 0

_VALID_AVIF_MODES = {"RGB", "RGBA"}

Expand Down Expand Up @@ -138,6 +139,7 @@ def _save(im, fp, filename, save_all=False):
duration = info.get("duration", 0)
subsampling = info.get("subsampling", "4:2:0")
speed = info.get("speed", 6)
max_threads = info.get("max_threads", DEFAULT_MAX_THREADS)
codec = info.get("codec", "auto")
range_ = info.get("range", "full")
tile_rows_log2 = info.get("tile_rows", 0)
Expand Down Expand Up @@ -196,6 +198,7 @@ def _save(im, fp, filename, save_all=False):
qmax,
quality,
speed,
max_threads,
codec,
range_,
tile_rows_log2,
Expand Down
17 changes: 11 additions & 6 deletions src/pillow_avif/_avif.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ typedef struct {

static PyTypeObject AvifDecoder_Type;

static int max_threads = 0;
static int default_max_threads = 0;

static void
init_max_threads(void) {
Expand Down Expand Up @@ -85,7 +85,7 @@ init_max_threads(void) {
goto error;
}

max_threads = (int)num_cpus;
default_max_threads = (int)num_cpus;

done:
Py_XDECREF(os);
Expand Down Expand Up @@ -321,6 +321,7 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
int quality = 75;
int speed = 8;
int exif_orientation = 0;
int max_threads = default_max_threads;
PyObject *icc_bytes;
PyObject *exif_bytes;
PyObject *xmp_bytes;
Expand All @@ -336,14 +337,15 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {

if (!PyArg_ParseTuple(
args,
"IIsiiiissiiOOSSiSO",
"IIsiiiiissiiOOSSiSO",
&width,
&height,
&subsampling,
&qmin,
&qmax,
&quality,
&speed,
&max_threads,
&codec,
&range,
&tile_rows_log2,
Expand Down Expand Up @@ -445,7 +447,10 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
encoder = avifEncoderCreate();

if (max_threads == 0) {
init_max_threads();
if (default_max_threads == 0) {
init_max_threads();
}
max_threads = default_max_threads;
}

int is_aom_encode = strcmp(codec, "aom") == 0 ||
Expand Down Expand Up @@ -777,10 +782,10 @@ AvifDecoderNew(PyObject *self_, PyObject *args) {

self->decoder = avifDecoderCreate();
#if AVIF_VERSION >= 80400
if (max_threads == 0) {
if (default_max_threads == 0) {
init_max_threads();
}
self->decoder->maxThreads = max_threads;
self->decoder->maxThreads = default_max_threads;
#endif
self->decoder->codecChoice = codec;

Expand Down

0 comments on commit a6d6dd1

Please sign in to comment.