Small refactoring

Also do not use the default CALIC parameters

Change-Id: I9f4c68ea00e53ce06c39ffafd3c20d43a3408809
Reviewed-on: https://chromium-review.googlesource.com/c/codecs/libwebp2/+/6718035
Reviewed-by: Maryla Ustarroz-Calonge <maryla@google.com>
Tested-by: WebM Builds <builds@webmproject.org>
diff --git a/src/common/lossless/calic.cc b/src/common/lossless/calic.cc
index 4eb924a..7f2b2a8 100644
--- a/src/common/lossless/calic.cc
+++ b/src/common/lossless/calic.cc
@@ -48,7 +48,8 @@
   return WP2_STATUS_OK;
 }
 
-WP2Status CalicState::SetQuantization(Quantization quantization) {
+WP2Status CalicState::SetParameters(Quantization quantization, int16_t min_tpv,
+                                    int16_t max_tpv) {
   // Initialize the quantized delta table.
   quantization_ = quantization;
   [[maybe_unused]] const uint8_t max_context =
@@ -93,15 +94,14 @@
   WP2_CHECK_ALLOC_OK(mean_errors_.resize(size));
   WP2_CHECK_ALLOC_OK(s_errors_.resize(size));
   WP2_CHECK_ALLOC_OK(n_errors_.resize(size));
-  WP2_CHECK_STATUS(Reset());
 
-  return WP2_STATUS_OK;
-}
-
-void CalicState::SetMinMax(int16_t min_tpv, int16_t max_tpv) {
   min_tpv_ = min_tpv;
   max_tpv_ = max_tpv;
   default_value_ = (min_tpv + max_tpv + 1) / 2;
+
+  WP2_CHECK_STATUS(Reset());
+
+  return WP2_STATUS_OK;
 }
 
 uint8_t CalicState::GetNumContextsFromQuantization(Quantization quantization) {
diff --git a/src/common/lossless/calic.h b/src/common/lossless/calic.h
index 638ea05..2f4ab9e 100644
--- a/src/common/lossless/calic.h
+++ b/src/common/lossless/calic.h
@@ -28,7 +28,7 @@
 
 // Define ORIGINAL_CALIC to get the original version of CALIC, with no
 // enhancement on the coding context.
-#define ORIGINAL_CALIC
+// #define ORIGINAL_CALIC
 
 namespace WP2L {
 namespace calic {
@@ -47,10 +47,10 @@
   WP2Status Reset() override;
   void StartProcessingLine(uint32_t y, const int16_t* row) override;
 
-  // Set the quantization type for the plane to compress.
-  WP2Status SetQuantization(Quantization quantization);
-  // Sets the minimum and maximum true pixel inclusive values in the image.
-  void SetMinMax(int16_t min_tpv, int16_t max_tpv);
+  // Set the quantization type for the plane to compress and the minimum and
+  // maximum true pixel inclusive values in the image.
+  WP2Status SetParameters(Quantization quantization, int16_t min_tpv,
+                          int16_t max_tpv);
   // Get the number of contexts for the given quantization method.
   static uint8_t GetNumContextsFromQuantization(Quantization quantization);
   // Predicts the pixel value at position x, as well as the context `ctxt` and
diff --git a/src/dec/lossless/plane_dec.cc b/src/dec/lossless/plane_dec.cc
index 4cfaa31..94889ec 100644
--- a/src/dec/lossless/plane_dec.cc
+++ b/src/dec/lossless/plane_dec.cc
@@ -73,9 +73,8 @@
                             WP2::SymbolReader& sr, ImagePlanes& img) {
     const int16_t min_tpv = global.min_tpv[plane_to_decompress];
     const int16_t max_tpv = global.max_tpv[plane_to_decompress];
-    SetMinMax(min_tpv, max_tpv);
-    WP2_CHECK_STATUS(
-        SetQuantization(global.calic_quantizations[plane_to_decompress]));
+    WP2_CHECK_STATUS(SetParameters(
+        global.calic_quantizations[plane_to_decompress], min_tpv, max_tpv));
 
     for (size_t y = 0; y < img.height(); ++y) {
       int16_t* row = img.PlaneRow(plane_to_decompress, y);
@@ -194,6 +193,8 @@
   }
 };
 
+////////////////////////////////////////////////////////////////////////////////
+
 // Main function to decompress a buffer using an SCP state.
 template <typename STATE>
 WP2Status Decompress(const ScpGlobal& global, uint32_t width, uint32_t height,
diff --git a/src/enc/lossless/losslessi_enc.cc b/src/enc/lossless/losslessi_enc.cc
index b473c04..50b4d41 100644
--- a/src/enc/lossless/losslessi_enc.cc
+++ b/src/enc/lossless/losslessi_enc.cc
@@ -1140,10 +1140,8 @@
                   "capacity");
     return WP2_STATUS_OK;
   }
-  if (config->algorithm == EncodingAlgorithm::kSCP) {
-    return WP2_STATUS_OK;
-  }
-  if (config->algorithm == EncodingAlgorithm::kCALIC) {
+  if (config->algorithm == EncodingAlgorithm::kSCP ||
+      config->algorithm == EncodingAlgorithm::kCALIC) {
     return WP2_STATUS_OK;
   }
 
diff --git a/src/enc/lossless/plane_enc.cc b/src/enc/lossless/plane_enc.cc
index 6ab8465..f061b4d 100644
--- a/src/enc/lossless/plane_enc.cc
+++ b/src/enc/lossless/plane_enc.cc
@@ -105,8 +105,8 @@
                           int16_t min_tpv, int16_t max_tpv,
                           Encoding* encoding) {
     encoding->tokens.clear();
-    WP2_CHECK_STATUS(SetQuantization(config_.quantizations[plane_to_compress]));
-    SetMinMax(min_tpv, max_tpv);
+    WP2_CHECK_STATUS(SetParameters(config_.quantizations[plane_to_compress],
+                                   min_tpv, max_tpv));
 
     for (size_t y = 0; y < img.height(); ++y) {
       const int16_t* row = img.PlaneRow(plane_to_compress, y);
@@ -345,10 +345,17 @@
   const int num_tokens_ori = encode_info != nullptr ? enc.NumTokens() : 0;
 
   enc.AddDebugPrefix("GlobalHeader");
-  enc.PutRValue(std::is_same<STATE, ClassicalEncState>::value ? 0
-                : std::is_same<STATE, JXLEncState>::value     ? 1
-                                                              : 2,
-                static_cast<int>(PlaneCodec::Method::kNum), "method");
+  int method;
+  if constexpr (std::is_same<STATE, ClassicalEncState>::value) {
+    method = 0;
+  } else if constexpr (std::is_same<STATE, JXLEncState>::value) {
+    method = 1;
+  } else if constexpr (std::is_same<STATE, CalicEncState>::value) {
+    method = 2;
+  } else {
+    assert(false);
+  }
+  enc.PutRValue(method, static_cast<int>(PlaneCodec::Method::kNum), "method");
 
   // Process the different planes independently.
   std::array<Encoding, 4> encodings;
@@ -475,8 +482,7 @@
     jxl_state.SetConfig(jxl_config);
     WP2::ANSEncCounter enc_counter;
     WP2_CHECK_STATUS(Compress(img_planes, min_tpv, max_tpv, effort, enc_counter,
-                              jxl_state, dicts,
-                              /*encode_info=*/nullptr));
+                              jxl_state, dicts, /*encode_info=*/nullptr));
 
     const float cost = enc_counter.GetCost(dicts);
     if (cost < cost_best) {
@@ -624,11 +630,16 @@
     }
   }
 #endif
+
+#ifdef ORIGINAL_CALIC
+  config_best.quantizations.fill(calic::CalicState::Quantization::kOriginal);
+#else
   // These defaults work well if the last two channels are like chroma.
   config_best.quantizations = {calic::CalicState::Quantization::kOriginal,
-                               calic::CalicState::Quantization::kOriginal,
-                               calic::CalicState::Quantization::kOriginal,
-                               calic::CalicState::Quantization::kOriginal};
+                               calic::CalicState::Quantization::k20,
+                               calic::CalicState::Quantization::k18,
+                               calic::CalicState::Quantization::k18};
+#endif
   state.SetConfig(config_best);
   WP2_CHECK_STATUS(Compress(img_planes, min_tpv, max_tpv, effort, enc, state,
                             dicts, encode_info));