Cleanup for #1069

Clean up and add comments for functions added in PR #1069 for
tests/gtest/avif_fuzztest_enc_dec_layered.cc.

In GetAvifLayeredRandDims(), change the `seeds` parameter to a
std::vector so that vector bounds checks can be performed. Change the
for loop's condition to be i < kMaxNumLayers - 1 because the
subexpression kMaxNumLayers - 1 is also used elsewhere. Change
sizes.back() to sizes[kMaxNumLayers - 1], which uses an explicit array
index equal to the upper bound of the preceding for loop.

In CreateAvifLayered8b() and CreateAvifLayered16b(), skip the local
variable `layers`.

Add comments for constants and functions.

Change `display_width/height` to `last_layer_width/height`.

Document the newly added test image kodim03_yuv420_8bpc_384x256.y4m.

Fixes https://github.com/AOMediaCodec/libavif/issues/3386.
Fixes https://github.com/AOMediaCodec/libavif/issues/3392.

Co-authored-by: Wan-Teh Chang <wtc@google.com>
diff --git a/tests/data/README.md b/tests/data/README.md
index 86ff1e0..31499f5 100644
--- a/tests/data/README.md
+++ b/tests/data/README.md
@@ -155,12 +155,21 @@
 
 ### Files `kodim*`
 
-#### File [kodim03_yuv420_8bpc.avif](io/kodim03_yuv420_8bpc.avif)
+#### File [kodim03_yuv420_8bpc.avif](io/kodim03_yuv420_8bpc.avif), [kodim03_yuv420_8bpc.y4m](kodim03_yuv420_8bpc.y4m)
 
 ![](io/kodim03_yuv420_8bpc.avif)
 
 License: released by the Eastman Kodak Company for unrestricted usage
 
+#### File [kodim03_yuv420_8bpc_384x256.y4m](kodim03_yuv420_8bpc_384x256.y4m)
+
+![](kodim03_yuv420_8bpc_384x256.y4m)
+
+License: released by the Eastman Kodak Company for unrestricted usage
+
+It is [kodim03_yuv420_8bpc.y4m](kodim03_yuv420_8bpc.y4m) resized to half the
+width and height.
+
 #### File [kodim03_grayscale_gamma1.6.png](kodim03_grayscale_gamma1.6.png)
 
 ![](kodim03_grayscale_gamma1.6.png)
diff --git a/tests/gtest/avif_fuzztest_helpers.cc b/tests/gtest/avif_fuzztest_helpers.cc
index e73ab73..28ab5ba 100644
--- a/tests/gtest/avif_fuzztest_helpers.cc
+++ b/tests/gtest/avif_fuzztest_helpers.cc
@@ -23,24 +23,30 @@
 
 //------------------------------------------------------------------------------
 
+// The number of seeds for kMaxNumLayers - 1 pairs of random width and height.
 constexpr size_t kNumLayeredRandDimSeeds = 2 * (kMaxNumLayers - 1);
 
+// Generates kMaxNumLayers pairs of random width and height. The first
+// kMaxNumLayers - 1 pairs of width and height are less than last_layer_width
+// and last_layer_height, respectively. The last pair is the given
+// last_layer_width and last_layer_height.
 template <typename Sample>
 std::array<std::pair<size_t, size_t>, kMaxNumLayers> GetAvifLayeredRandDims(
-    size_t display_width, size_t display_height, const Sample* seeds) {
-  assert(display_width > 1);
-  assert(display_height > 1);
+    size_t last_layer_width, size_t last_layer_height,
+    const std::vector<Sample>& seeds) {
+  assert(last_layer_width > 1);
+  assert(last_layer_height > 1);
   std::array<std::pair<size_t, size_t>, kMaxNumLayers> sizes = {};
-  for (size_t i = 0; i + 1 < kMaxNumLayers; ++i) {
+  for (size_t i = 0; i < kMaxNumLayers - 1; ++i) {
     const size_t width =
-        1 + (static_cast<size_t>(seeds[2 * i]) % (display_width - 1));
+        1 + (static_cast<size_t>(seeds[2 * i]) % (last_layer_width - 1));
     const size_t height =
-        1 + (static_cast<size_t>(seeds[2 * i + 1]) % (display_height - 1));
-    assert(width < display_width);
-    assert(height < display_height);
+        1 + (static_cast<size_t>(seeds[2 * i + 1]) % (last_layer_height - 1));
+    assert(width < last_layer_width);
+    assert(height < last_layer_height);
     sizes[i] = {width, height};
   }
-  sizes.back() = {display_width, display_height};
+  sizes[kMaxNumLayers - 1] = {last_layer_width, last_layer_height};
   return sizes;
 }
 
@@ -130,33 +136,33 @@
                                           avifPixelFormat pixel_format,
                                           bool has_alpha,
                                           const std::vector<uint8_t>& samples) {
-  std::vector<ImagePtr> layers = CreateAvifAnim8b(
-      kMaxNumLayers, width, height, pixel_format, has_alpha, samples);
-  return layers;
+  return CreateAvifAnim8b(kMaxNumLayers, width, height, pixel_format, has_alpha,
+                          samples);
 }
 
 std::vector<ImagePtr> CreateAvifLayered16b(
     size_t width, size_t height, int depth, avifPixelFormat pixel_format,
     bool has_alpha, const std::vector<uint16_t>& samples) {
-  std::vector<ImagePtr> layers = CreateAvifAnim16b(
-      kMaxNumLayers, width, height, depth, pixel_format, has_alpha, samples);
-  return layers;
+  return CreateAvifAnim16b(kMaxNumLayers, width, height, depth, pixel_format,
+                           has_alpha, samples);
 }
 
-size_t GetNumSamplesLayeredRandDim(size_t display_width, size_t display_height,
+size_t GetNumSamplesLayeredRandDim(size_t last_layer_width,
+                                   size_t last_layer_height,
                                    avifPixelFormat pixel_format,
                                    bool has_alpha) {
-  return kNumLayeredRandDimSeeds + GetNumSamples(kMaxNumLayers, display_width,
-                                                 display_height, pixel_format,
-                                                 has_alpha);
+  return kNumLayeredRandDimSeeds +
+         GetNumSamples(kMaxNumLayers, last_layer_width, last_layer_height,
+                       pixel_format, has_alpha);
 }
 
 std::vector<ImagePtr> CreateAvifLayeredRandDim8b(
-    size_t display_width, size_t display_height, avifPixelFormat pixel_format,
-    bool has_alpha, const std::vector<uint8_t>& samples) {
+    size_t last_layer_width, size_t last_layer_height,
+    avifPixelFormat pixel_format, bool has_alpha,
+    const std::vector<uint8_t>& samples) {
   assert(samples.size() >= kNumLayeredRandDimSeeds);
   const auto dims =
-      GetAvifLayeredRandDims(display_width, display_height, samples.data());
+      GetAvifLayeredRandDims(last_layer_width, last_layer_height, samples);
   std::vector<ImagePtr> layers;
   layers.reserve(kMaxNumLayers);
 
@@ -174,12 +180,12 @@
 }
 
 std::vector<ImagePtr> CreateAvifLayeredRandDim16b(
-    size_t display_width, size_t display_height, int depth,
+    size_t last_layer_width, size_t last_layer_height, int depth,
     avifPixelFormat pixel_format, bool has_alpha,
     const std::vector<uint16_t>& samples) {
   assert(samples.size() >= kNumLayeredRandDimSeeds);
   const auto dims =
-      GetAvifLayeredRandDims(display_width, display_height, samples.data());
+      GetAvifLayeredRandDims(last_layer_width, last_layer_height, samples);
   std::vector<ImagePtr> layers;
   layers.reserve(kMaxNumLayers);
 
diff --git a/tests/gtest/avif_fuzztest_helpers.h b/tests/gtest/avif_fuzztest_helpers.h
index 849f368..e210827 100644
--- a/tests/gtest/avif_fuzztest_helpers.h
+++ b/tests/gtest/avif_fuzztest_helpers.h
@@ -58,6 +58,7 @@
                                         avifPixelFormat pixel_format,
                                         bool has_alpha,
                                         const std::vector<uint16_t>& samples);
+// CreateAvifLayered* all return exactly kMaxNumLayers images.
 std::vector<ImagePtr> CreateAvifLayered8b(size_t width, size_t height,
                                           avifPixelFormat pixel_format,
                                           bool has_alpha,
@@ -66,10 +67,11 @@
     size_t width, size_t height, int depth, avifPixelFormat pixel_format,
     bool has_alpha, const std::vector<uint16_t>& samples);
 std::vector<ImagePtr> CreateAvifLayeredRandDim8b(
-    size_t display_width, size_t display_height, avifPixelFormat pixel_format,
-    bool has_alpha, const std::vector<uint8_t>& samples);
+    size_t last_layer_width, size_t last_layer_height,
+    avifPixelFormat pixel_format, bool has_alpha,
+    const std::vector<uint8_t>& samples);
 std::vector<ImagePtr> CreateAvifLayeredRandDim16b(
-    size_t display_width, size_t display_height, int depth,
+    size_t last_layer_width, size_t last_layer_height, int depth,
     avifPixelFormat pixel_format, bool has_alpha,
     const std::vector<uint16_t>& samples);
 EncoderPtr CreateAvifEncoder(avifCodecChoice codec_choice, int max_threads,
@@ -103,7 +105,10 @@
 
 size_t GetNumSamples(size_t num_frames, size_t width, size_t height,
                      avifPixelFormat pixel_format, bool has_alpha);
-size_t GetNumSamplesLayeredRandDim(size_t display_width, size_t display_height,
+// Returns the number of samples needed for CreateAvifLayeredRandDim8b() and
+// CreateAvifLayeredRandDim16b().
+size_t GetNumSamplesLayeredRandDim(size_t last_layer_width,
+                                   size_t last_layer_height,
                                    avifPixelFormat pixel_format,
                                    bool has_alpha);
 
@@ -254,21 +259,21 @@
   return fuzztest::OneOf(ArbitraryAvifLayered8b(), ArbitraryAvifLayered16b());
 }
 
-// Layered avifImage generator type: fixed number of layers, display size,
+// Layered avifImage generator type: fixed number of layers, last layer size,
 // pixel format and 8-bit samples.
 inline auto ArbitraryAvifLayeredRandDim8b() {
   constexpr uint16_t kMinLayerDimension = 8;
   constexpr uint16_t kMaxLayerDimension =
       kMaxDimension / kMaxNumFramesSquareRoot;
   return fuzztest::FlatMap(
-      [](size_t display_width, size_t display_height,
+      [](size_t last_layer_width, size_t last_layer_height,
          avifPixelFormat pixel_format, bool has_alpha) {
         return fuzztest::Map(
-            CreateAvifLayeredRandDim8b, fuzztest::Just(display_width),
-            fuzztest::Just(display_height), fuzztest::Just(pixel_format),
+            CreateAvifLayeredRandDim8b, fuzztest::Just(last_layer_width),
+            fuzztest::Just(last_layer_height), fuzztest::Just(pixel_format),
             fuzztest::Just(has_alpha),
             fuzztest::Arbitrary<std::vector<uint8_t>>().WithSize(
-                GetNumSamplesLayeredRandDim(display_width, display_height,
+                GetNumSamplesLayeredRandDim(last_layer_width, last_layer_height,
                                             pixel_format, has_alpha)));
       },
       fuzztest::InRange<uint16_t>(kMinLayerDimension, kMaxLayerDimension),
@@ -276,23 +281,24 @@
       ArbitraryPixelFormat(), fuzztest::Arbitrary<bool>());
 }
 
-// Layered avifImage generator type: fixed number of layers, display size,
+// Layered avifImage generator type: fixed number of layers, last layer size,
 // depth, pixel format and 16-bit samples.
 inline auto ArbitraryAvifLayeredRandDim16b() {
   constexpr uint16_t kMinLayerDimension = 8;
   constexpr uint16_t kMaxLayerDimension =
       kMaxDimension / kMaxNumFramesSquareRoot;
   return fuzztest::FlatMap(
-      [](size_t display_width, size_t display_height, int depth,
+      [](size_t last_layer_width, size_t last_layer_height, int depth,
          avifPixelFormat pixel_format, bool has_alpha) {
         return fuzztest::Map(
-            CreateAvifLayeredRandDim16b, fuzztest::Just(display_width),
-            fuzztest::Just(display_height), fuzztest::Just(depth),
+            CreateAvifLayeredRandDim16b, fuzztest::Just(last_layer_width),
+            fuzztest::Just(last_layer_height), fuzztest::Just(depth),
             fuzztest::Just(pixel_format), fuzztest::Just(has_alpha),
             fuzztest::ContainerOf<std::vector<uint16_t>>(
                 fuzztest::InRange<uint16_t>(0, (1 << depth) - 1))
                 .WithSize(GetNumSamplesLayeredRandDim(
-                    display_width, display_height, pixel_format, has_alpha)));
+                    last_layer_width, last_layer_height, pixel_format,
+                    has_alpha)));
       },
       fuzztest::InRange<uint16_t>(kMinLayerDimension, kMaxLayerDimension),
       fuzztest::InRange<uint16_t>(kMinLayerDimension, kMaxLayerDimension),
@@ -300,7 +306,7 @@
       fuzztest::Arbitrary<bool>());
 }
 
-// Generator for an arbitrary layered still image with display size override.
+// Generator for an arbitrary layered still image with varying layer sizes.
 inline auto ArbitraryAvifLayeredRandDim() {
   return fuzztest::OneOf(ArbitraryAvifLayeredRandDim8b(),
                          ArbitraryAvifLayeredRandDim16b());