Limit the number of encoder maxThreads to 64 when using libaom. (#2008)

Limit the number of encoder maxThreads to 64 when using libaom.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f6d17b2..968b0bf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,9 @@
 
 ## [Unreleased]
 
+### Changed
+* limit the number of encoder maxThreads to 64 when using libaom.
+
 ## [1.0.4] - 2024-02-08
 
 ### Changed
diff --git a/src/codec_aom.c b/src/codec_aom.c
index 6bf2ea7..205251f 100644
--- a/src/codec_aom.c
+++ b/src/codec_aom.c
@@ -748,7 +748,9 @@
             cfg->g_lag_in_frames = 0;
         }
         if (encoder->maxThreads > 1) {
-            cfg->g_threads = encoder->maxThreads;
+            // libaom fails if cfg->g_threads is greater than 64 threads. See MAX_NUM_THREADS in
+            // aom/aom_util/aom_thread.h.
+            cfg->g_threads = AVIF_MIN(encoder->maxThreads, 64);
         }
 
         codec->internal->monochromeEnabled = AVIF_FALSE;