Revert "Set Unspecified CICP in Seq Header OBU (#2687)" (#2859)

This partially reverts commit fa089a0c3f8ed6e202034589cf98394e0226d66c.
The added test avifcicptest.cc is not reverted.
AVM is also left as is, as it's a new codec and there are no backward
compatibility concerns.
Set the CICP in codec_svt.c which was not done previously.

Fixes #2850
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e4387c7..ced68fe 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,6 +28,9 @@
 * Fix grayscale conversion when changing the bit depth.
 * Bump cmake_minimum_required from 3.13 to 3.22
 * Associate transformative properties with alpha auxiliary image items.
+* Always forward the CICP color primaries, transfer characteristics,
+  and matrix coefficients to the AV1 encoder, which writes them in the Sequence
+  Header OBU, for compatibility with libraries that wrongly ignore the colr box.
 
 ### Removed since 1.3.0
 
diff --git a/src/codec_aom.c b/src/codec_aom.c
index e472736..f17e8cc 100644
--- a/src/codec_aom.c
+++ b/src/codec_aom.c
@@ -848,12 +848,24 @@
             // Keep default Unspecified (2) colour primaries, transfer characteristics,
             // and matrix coefficients.
         } else {
-            // libaom's defaults are AOM_CSP_UNKNOWN and 0 (studio/limited range).
-            // Call aom_codec_control() only if the values are not the defaults.
-
             // AVIF specification, Section 2.2.1. "AV1 Item Configuration Property":
             //   The values of the fields in the AV1CodecConfigurationBox shall match those
             //   of the Sequence Header OBU in the AV1 Image Item Data.
+            // CICP values could be set to 2/2/2 (Unspecified) in the Sequence Header OBU for
+            // simplicity and to save 3 bytes, but some decoders ignore the colr box and rely
+            // on the OBU contents instead. See #2850.
+            // libaom's defaults are AOM_CICP_CP_UNSPECIFIED, AOM_CICP_TC_UNSPECIFIED,
+            // AOM_CICP_MC_UNSPECIFIED, AOM_CSP_UNKNOWN, and 0 (studio/limited range). Call
+            // aom_codec_control() only if the values are not the defaults.
+            if (image->colorPrimaries != AVIF_COLOR_PRIMARIES_UNSPECIFIED) {
+                aom_codec_control(&codec->internal->encoder, AV1E_SET_COLOR_PRIMARIES, (int)image->colorPrimaries);
+            }
+            if (image->transferCharacteristics != AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED) {
+                aom_codec_control(&codec->internal->encoder, AV1E_SET_TRANSFER_CHARACTERISTICS, (int)image->transferCharacteristics);
+            }
+            if (image->matrixCoefficients != AVIF_MATRIX_COEFFICIENTS_UNSPECIFIED) {
+                aom_codec_control(&codec->internal->encoder, AV1E_SET_MATRIX_COEFFICIENTS, (int)image->matrixCoefficients);
+            }
             if (image->yuvChromaSamplePosition != AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN) {
                 aom_codec_control(&codec->internal->encoder, AV1E_SET_CHROMA_SAMPLE_POSITION, (int)image->yuvChromaSamplePosition);
             }
@@ -1094,6 +1106,9 @@
             }
         }
 
+        aomImage.cp = (aom_color_primaries_t)image->colorPrimaries;
+        aomImage.tc = (aom_transfer_characteristics_t)image->transferCharacteristics;
+        aomImage.mc = (aom_matrix_coefficients_t)image->matrixCoefficients;
         // AVIF specification, Section 2.2.1. "AV1 Item Configuration Property":
         //   The values of the fields in the AV1CodecConfigurationBox shall match those
         //   of the Sequence Header OBU in the AV1 Image Item Data.
diff --git a/src/codec_rav1e.c b/src/codec_rav1e.c
index 293d100..8f8c9ee 100644
--- a/src/codec_rav1e.c
+++ b/src/codec_rav1e.c
@@ -202,15 +202,16 @@
             }
         }
 
-        // Section 2.3.4 of AV1-ISOBMFF says 'colr' with 'nclx' should be present and shall match CICP
-        // values in the Sequence Header OBU, unless the latter has 2/2/2 (Unspecified).
-        // So set CICP values to 2/2/2 (Unspecified) in the Sequence Header OBU for simplicity.
-        // It may also save 3 bytes since the AV1 encoder may set color_description_present_flag to 0
-        // (see Section 5.5.2 "Color config syntax" of the AV1 specification).
+        // AVIF specification, Section 2.2.1. "AV1 Item Configuration Property":
+        //   The values of the fields in the AV1CodecConfigurationBox shall match those
+        //   of the Sequence Header OBU in the AV1 Image Item Data.
+        // CICP values could be set to 2/2/2 (Unspecified) in the Sequence Header OBU for
+        // simplicity and to save 3 bytes, but some decoders ignore the colr box and rely
+        // on the OBU contents instead. See #2850.
         rav1e_config_set_color_description(rav1eConfig,
-                                           RA_MATRIX_COEFFICIENTS_UNSPECIFIED,
-                                           RA_COLOR_PRIMARIES_UNSPECIFIED,
-                                           RA_TRANSFER_CHARACTERISTICS_UNSPECIFIED);
+                                           (RaMatrixCoefficients)image->matrixCoefficients,
+                                           (RaColorPrimaries)image->colorPrimaries,
+                                           (RaTransferCharacteristics)image->transferCharacteristics);
 
         codec->internal->rav1eContext = rav1e_context_new(rav1eConfig);
         if (!codec->internal->rav1eContext) {
diff --git a/src/codec_svt.c b/src/codec_svt.c
index 1d00d1c..84e4c46 100644
--- a/src/codec_svt.c
+++ b/src/codec_svt.c
@@ -142,14 +142,15 @@
         svt_config->encoder_color_format = color_format;
         svt_config->encoder_bit_depth = (uint8_t)image->depth;
 
-        // Section 2.3.4 of AV1-ISOBMFF says 'colr' with 'nclx' should be present and shall match CICP
-        // values in the Sequence Header OBU, unless the latter has 2/2/2 (Unspecified).
-        // So set CICP values to 2/2/2 (Unspecified) in the Sequence Header OBU for simplicity.
-        // It may also save 3 bytes since the AV1 encoder may set color_description_present_flag to 0
-        // (see Section 5.5.2 "Color config syntax" of the AV1 specification).
-        svt_config->color_primaries = EB_CICP_CP_UNSPECIFIED;
-        svt_config->transfer_characteristics = EB_CICP_TC_UNSPECIFIED;
-        svt_config->matrix_coefficients = EB_CICP_MC_UNSPECIFIED;
+        // AVIF specification, Section 2.2.1. "AV1 Item Configuration Property":
+        //   The values of the fields in the AV1CodecConfigurationBox shall match those
+        //   of the Sequence Header OBU in the AV1 Image Item Data.
+        // CICP values could be set to 2/2/2 (Unspecified) in the Sequence Header OBU for
+        // simplicity and to save 3 bytes, but some decoders ignore the colr box and rely
+        // on the OBU contents instead. See #2850.
+        svt_config->color_primaries = (EbColorPrimaries)image->colorPrimaries;
+        svt_config->transfer_characteristics = (EbTransferCharacteristics)image->transferCharacteristics;
+        svt_config->matrix_coefficients = (EbMatrixCoefficients)image->matrixCoefficients;
 
         svt_config->color_range = svt_range;
 #if !SVT_AV1_CHECK_VERSION(0, 9, 0)
diff --git a/tests/data/goldens/kodim03_23_animation.avif.xml b/tests/data/goldens/kodim03_23_animation.avif.xml
index 0723c6f..1537368 100644
--- a/tests/data/goldens/kodim03_23_animation.avif.xml
+++ b/tests/data/goldens/kodim03_23_animation.avif.xml
@@ -124,7 +124,7 @@
  </OBUConfig>
  <OBUSamples>
   <Sample number="1" DTS="0" CTS="0" size="REDACTED" RAP="1" >
-   <OBU size="REDACTED" type="seq_header" header_size="2" has_size_field="1" has_ext="0" temporalID="0" spatialID="0" sequence_width="768" sequence_height="512" still_picture="0" OperatingPointIdc="0" profile="0" level="4" bit_depth="8" monochrome="0" color_description_present_flag="0" color_primaries="2" transfer_characteristics="2" matrix_coefficients="2" color_range="1" chroma_subsampling_x="1" chroma_subsampling_y="1" chroma_sample_position="0" film_grain_params_present="0" />
+   <OBU size="REDACTED" type="seq_header" header_size="2" has_size_field="1" has_ext="0" temporalID="0" spatialID="0" sequence_width="768" sequence_height="512" still_picture="0" OperatingPointIdc="0" profile="0" level="4" bit_depth="8" monochrome="0" color_description_present_flag="1" color_primaries="1" transfer_characteristics="13" matrix_coefficients="6" color_range="1" chroma_subsampling_x="1" chroma_subsampling_y="1" chroma_sample_position="0" film_grain_params_present="0" />
    <OBU size="REDACTED" type="frame" header_size="4" has_size_field="1" has_ext="0" temporalID="0" spatialID="0" uncompressed_header_bytes="12" frame_type="key" refresh_frame_flags="255" show_frame="1" width="768" height="512" nb_tiles="2" >
      <Tile number="0" start="REDACTED" size="REDACTED"/>
      <Tile number="1" start="REDACTED" size="REDACTED"/>
diff --git a/tests/data/goldens/kodim03_23_animation_keyframes.avif.xml b/tests/data/goldens/kodim03_23_animation_keyframes.avif.xml
index 50a91fa..0e80768 100644
--- a/tests/data/goldens/kodim03_23_animation_keyframes.avif.xml
+++ b/tests/data/goldens/kodim03_23_animation_keyframes.avif.xml
@@ -122,7 +122,7 @@
  </OBUConfig>
  <OBUSamples>
   <Sample number="1" DTS="0" CTS="0" size="REDACTED" RAP="1" >
-   <OBU size="REDACTED" type="seq_header" header_size="2" has_size_field="1" has_ext="0" temporalID="0" spatialID="0" sequence_width="768" sequence_height="512" still_picture="0" OperatingPointIdc="0" profile="0" level="4" bit_depth="8" monochrome="0" color_description_present_flag="0" color_primaries="2" transfer_characteristics="2" matrix_coefficients="2" color_range="1" chroma_subsampling_x="1" chroma_subsampling_y="1" chroma_sample_position="0" film_grain_params_present="0" />
+   <OBU size="REDACTED" type="seq_header" header_size="2" has_size_field="1" has_ext="0" temporalID="0" spatialID="0" sequence_width="768" sequence_height="512" still_picture="0" OperatingPointIdc="0" profile="0" level="4" bit_depth="8" monochrome="0" color_description_present_flag="1" color_primaries="1" transfer_characteristics="13" matrix_coefficients="6" color_range="1" chroma_subsampling_x="1" chroma_subsampling_y="1" chroma_sample_position="0" film_grain_params_present="0" />
    <OBU size="REDACTED" type="frame" header_size="4" has_size_field="1" has_ext="0" temporalID="0" spatialID="0" uncompressed_header_bytes="12" frame_type="key" refresh_frame_flags="255" show_frame="1" width="768" height="512" nb_tiles="2" >
      <Tile number="0" start="REDACTED" size="REDACTED"/>
      <Tile number="1" start="REDACTED" size="REDACTED"/>
@@ -130,7 +130,7 @@
   </Sample>
 
   <Sample number="2" DTS="1" CTS="1" size="REDACTED" RAP="1" >
-   <OBU size="REDACTED" type="seq_header" header_size="2" has_size_field="1" has_ext="0" temporalID="0" spatialID="0" sequence_width="768" sequence_height="512" still_picture="0" OperatingPointIdc="0" profile="0" level="4" bit_depth="8" monochrome="0" color_description_present_flag="0" color_primaries="2" transfer_characteristics="2" matrix_coefficients="2" color_range="1" chroma_subsampling_x="1" chroma_subsampling_y="1" chroma_sample_position="0" film_grain_params_present="0" />
+   <OBU size="REDACTED" type="seq_header" header_size="2" has_size_field="1" has_ext="0" temporalID="0" spatialID="0" sequence_width="768" sequence_height="512" still_picture="0" OperatingPointIdc="0" profile="0" level="4" bit_depth="8" monochrome="0" color_description_present_flag="1" color_primaries="1" transfer_characteristics="13" matrix_coefficients="6" color_range="1" chroma_subsampling_x="1" chroma_subsampling_y="1" chroma_sample_position="0" film_grain_params_present="0" />
    <OBU size="REDACTED" type="frame" header_size="4" has_size_field="1" has_ext="0" temporalID="0" spatialID="0" uncompressed_header_bytes="12" frame_type="key" refresh_frame_flags="255" show_frame="1" width="768" height="512" nb_tiles="2" >
      <Tile number="0" start="REDACTED" size="REDACTED"/>
      <Tile number="1" start="REDACTED" size="REDACTED"/>