Fix group id conflicting with item id. #2391 (#2396)

diff --git a/src/write.c b/src/write.c
index 723302e..003914f 100644
--- a/src/write.c
+++ b/src/write.c
@@ -2272,7 +2272,7 @@
     return AVIF_RESULT_OK;
 }
 
-static avifResult avifWriteAltrGroup(avifRWStream * s, const avifEncoderItemIdArray * itemIDs)
+static avifResult avifWriteAltrGroup(avifRWStream * s, uint32_t groupID, const avifEncoderItemIdArray * itemIDs)
 {
     avifBoxMarker grpl;
     AVIF_CHECKRES(avifRWStreamWriteBox(s, "grpl", AVIF_BOX_SIZE_TBD, &grpl));
@@ -2280,7 +2280,7 @@
     avifBoxMarker altr;
     AVIF_CHECKRES(avifRWStreamWriteFullBox(s, "altr", AVIF_BOX_SIZE_TBD, 0, 0, &altr));
 
-    AVIF_CHECKRES(avifRWStreamWriteU32(s, 1));                        // unsigned int(32) group_id;
+    AVIF_CHECKRES(avifRWStreamWriteU32(s, groupID));                  // unsigned int(32) group_id;
     AVIF_CHECKRES(avifRWStreamWriteU32(s, (uint32_t)itemIDs->count)); // unsigned int(32) num_entities_in_group;
     for (uint32_t i = 0; i < itemIDs->count; ++i) {
         AVIF_CHECKRES(avifRWStreamWriteU32(s, (uint32_t)itemIDs->item_id[i])); // unsigned int(32) entity_id;
@@ -3294,7 +3294,15 @@
     // Write grpl/altr box
 
     if (encoder->data->alternativeItemIDs.count) {
-        AVIF_CHECKRES(avifWriteAltrGroup(&s, &encoder->data->alternativeItemIDs));
+        // Section 8.18.3.3 of ISO 14496-12 (ISOBMFF) says:
+        //   group_id is a non-negative integer assigned to the particular grouping that shall not be equal to any
+        //   group_id value of any other EntityToGroupBox, any item_ID value of the hierarchy level
+        //   (file, movie. or track) that contains the GroupsListBox, or any track_ID value (when the
+        //   GroupsListBox is contained in the file level).
+        AVIF_ASSERT_OR_RETURN(encoder->data->lastItemID < UINT16_MAX);
+        ++encoder->data->lastItemID;
+        const uint32_t groupID = encoder->data->lastItemID;
+        AVIF_CHECKRES(avifWriteAltrGroup(&s, groupID, &encoder->data->alternativeItemIDs));
     }
 
     // -----------------------------------------------------------------------
diff --git a/tests/data/goldens/paris_exif_xmp_gainmap_bigendian.jpg.avif.xml b/tests/data/goldens/paris_exif_xmp_gainmap_bigendian.jpg.avif.xml
index 9fd29ce..157342c 100644
--- a/tests/data/goldens/paris_exif_xmp_gainmap_bigendian.jpg.avif.xml
+++ b/tests/data/goldens/paris_exif_xmp_gainmap_bigendian.jpg.avif.xml
@@ -105,7 +105,7 @@
 </ItemPropertyAssociationBox>
 </ItemPropertiesBox>
 <GroupListBox Size="36" Type="grpl" Specification="iff" Container="meta" >
-<EntityToGroupTypeBox Size="28" Type="altr" Version="0" Flags="0" Specification="iff" Container="grpl" group_id="1">
+<EntityToGroupTypeBox Size="28" Type="altr" Version="0" Flags="0" Specification="iff" Container="grpl" group_id="6">
 <EntityToGroupTypeBoxEntry EntityID="2"/>
 <EntityToGroupTypeBoxEntry EntityID="1"/>
 </EntityToGroupTypeBox>
diff --git a/tests/data/goldens/paris_exif_xmp_gainmap_littleendian.jpg.avif.xml b/tests/data/goldens/paris_exif_xmp_gainmap_littleendian.jpg.avif.xml
index 811f1df..64cd712 100644
--- a/tests/data/goldens/paris_exif_xmp_gainmap_littleendian.jpg.avif.xml
+++ b/tests/data/goldens/paris_exif_xmp_gainmap_littleendian.jpg.avif.xml
@@ -105,7 +105,7 @@
 </ItemPropertyAssociationBox>
 </ItemPropertiesBox>
 <GroupListBox Size="36" Type="grpl" Specification="iff" Container="meta" >
-<EntityToGroupTypeBox Size="28" Type="altr" Version="0" Flags="0" Specification="iff" Container="grpl" group_id="1">
+<EntityToGroupTypeBox Size="28" Type="altr" Version="0" Flags="0" Specification="iff" Container="grpl" group_id="6">
 <EntityToGroupTypeBoxEntry EntityID="2"/>
 <EntityToGroupTypeBoxEntry EntityID="1"/>
 </EntityToGroupTypeBox>
diff --git a/tests/data/goldens/paris_exif_xmp_icc_gainmap_bigendian.jpg.avif.xml b/tests/data/goldens/paris_exif_xmp_icc_gainmap_bigendian.jpg.avif.xml
index 98a7cfe..1b3acab 100644
--- a/tests/data/goldens/paris_exif_xmp_icc_gainmap_bigendian.jpg.avif.xml
+++ b/tests/data/goldens/paris_exif_xmp_icc_gainmap_bigendian.jpg.avif.xml
@@ -107,7 +107,7 @@
 </ItemPropertyAssociationBox>
 </ItemPropertiesBox>
 <GroupListBox Size="36" Type="grpl" Specification="iff" Container="meta" >
-<EntityToGroupTypeBox Size="28" Type="altr" Version="0" Flags="0" Specification="iff" Container="grpl" group_id="1">
+<EntityToGroupTypeBox Size="28" Type="altr" Version="0" Flags="0" Specification="iff" Container="grpl" group_id="6">
 <EntityToGroupTypeBoxEntry EntityID="2"/>
 <EntityToGroupTypeBoxEntry EntityID="1"/>
 </EntityToGroupTypeBox>
diff --git a/tests/data/seine_hdr_gainmap_small_srgb.avif b/tests/data/seine_hdr_gainmap_small_srgb.avif
index 681edac..da4983d 100644
--- a/tests/data/seine_hdr_gainmap_small_srgb.avif
+++ b/tests/data/seine_hdr_gainmap_small_srgb.avif
Binary files differ
diff --git a/tests/data/seine_hdr_gainmap_srgb.avif b/tests/data/seine_hdr_gainmap_srgb.avif
index 6a79b9d..caaba4f 100644
--- a/tests/data/seine_hdr_gainmap_srgb.avif
+++ b/tests/data/seine_hdr_gainmap_srgb.avif
Binary files differ
diff --git a/tests/data/seine_sdr_gainmap_big_srgb.avif b/tests/data/seine_sdr_gainmap_big_srgb.avif
index fad5b61..a51244b 100644
--- a/tests/data/seine_sdr_gainmap_big_srgb.avif
+++ b/tests/data/seine_sdr_gainmap_big_srgb.avif
Binary files differ
diff --git a/tests/data/seine_sdr_gainmap_srgb.avif b/tests/data/seine_sdr_gainmap_srgb.avif
index b716742..09c4ba2 100644
--- a/tests/data/seine_sdr_gainmap_srgb.avif
+++ b/tests/data/seine_sdr_gainmap_srgb.avif
Binary files differ