Change the eval order of luma palette_size

The order of luma palette_size evaluation in
av1_rd_pick_palette_intra_sby() for sf
prune_palette_search_level=2 is changed from descending
followed by ascending order to ascending followed by
descending order in order to facilitate early exit based
on palette header rdcost.

This change is verified to be bit-exact for speed < 3 in good
and allintra encoding modes.

For allintra video encode (on screen content set),

          Instruction Count       BD-Rate Loss(%)
cpu-used     Reduction(%)   avg.psnr  ovr.psnr  ssim
   3          -0.040       -0.1802   -0.1811   -0.7997
   4          -0.055       -0.2912   -0.2835   -1.0394
   5           0.023       -0.3423   -0.3413   -1.0311
   6          -0.157       -0.3567   -0.3550   -0.7304

For good video encode (on screen content set),

          Instruction Count       BD-Rate Loss(%)
cpu-used     Reduction(%)   avg.psnr  ovr.psnr  ssim
   3          -1.034       -0.5324   -0.5500   -0.8091
   4           0.051       -0.1340   -0.1007   -0.1401
   5          -0.046        0.0997    0.0828    0.1359
   6          -0.252        0.0895    0.0500   -0.1844

For AVIF still image encode,

          Instruction Count    BD-Rate Loss(%)
cpu-used     Reduction(%)      psnr       ssim
   3           -0.004         -0.0030    -0.0116
   4            0.004         -0.0030    -0.0055
   5           -0.002         -0.0049    -0.0085
   6           -0.007         -0.0061    -0.0173

BUG=aomedia:3096

STATS_CHANGED

Change-Id: Ia26c826bc27d7ebf907f4e3c7512526e88b744af
diff --git a/av1/encoder/palette.c b/av1/encoder/palette.c
index 1b0bf62..af8a255 100644
--- a/av1/encoder/palette.c
+++ b/av1/encoder/palette.c
@@ -632,20 +632,19 @@
     } else {
       const int max_n = AOMMIN(colors, PALETTE_MAX_SIZE),
                 min_n = PALETTE_MIN_SIZE;
-      // Perform top color palette search in descending order
-      int last_n_searched = max_n;
+      // Perform top color palette search in ascending order
+      int last_n_searched = min_n;
       perform_top_color_palette_search(
-          cpi, x, mbmi, bsize, dc_mode_cost, data, top_colors, max_n, min_n - 1,
-          -1, /*do_header_rd_based_gating=*/false, &last_n_searched,
-          color_cache, n_cache, best_mbmi, best_palette_color_map, best_rd,
-          rate, rate_tokenonly, distortion, skippable, beat_best_rd, ctx,
+          cpi, x, mbmi, bsize, dc_mode_cost, data, top_colors, min_n, max_n + 1,
+          1, /*do_header_rd_based_gating=*/false, &last_n_searched, color_cache,
+          n_cache, best_mbmi, best_palette_color_map, best_rd, rate,
+          rate_tokenonly, distortion, skippable, beat_best_rd, ctx,
           best_blk_skip, tx_type_map);
-
-      if (last_n_searched > min_n) {
-        // Search in ascending order until we get to the previous best
+      if (last_n_searched < max_n) {
+        // Search in descending order until we get to the previous best
         perform_top_color_palette_search(
-            cpi, x, mbmi, bsize, dc_mode_cost, data, top_colors, min_n,
-            last_n_searched, 1, /*do_header_rd_based_gating=*/false, &unused,
+            cpi, x, mbmi, bsize, dc_mode_cost, data, top_colors, max_n,
+            last_n_searched, -1, /*do_header_rd_based_gating=*/false, &unused,
             color_cache, n_cache, best_mbmi, best_palette_color_map, best_rd,
             rate, rate_tokenonly, distortion, skippable, beat_best_rd, ctx,
             best_blk_skip, tx_type_map);
@@ -662,20 +661,20 @@
                      rate_tokenonly, distortion, skippable, beat_best_rd, ctx,
                      best_blk_skip, tx_type_map, NULL, NULL);
       } else {
-        // Perform k-means palette search in descending order
-        last_n_searched = max_n;
+        // Perform k-means palette search in ascending order
+        last_n_searched = min_n;
         perform_k_means_palette_search(
             cpi, x, mbmi, bsize, dc_mode_cost, data, lower_bound, upper_bound,
-            max_n, min_n - 1, -1, /*do_header_rd_based_gating=*/false,
+            min_n, max_n + 1, 1, /*do_header_rd_based_gating=*/false,
             &last_n_searched, color_cache, n_cache, best_mbmi,
             best_palette_color_map, best_rd, rate, rate_tokenonly, distortion,
             skippable, beat_best_rd, ctx, best_blk_skip, tx_type_map, color_map,
             rows * cols);
-        if (last_n_searched > min_n) {
-          // Search in ascending order until we get to the previous best
+        if (last_n_searched < max_n) {
+          // Search in descending order until we get to the previous best
           perform_k_means_palette_search(
               cpi, x, mbmi, bsize, dc_mode_cost, data, lower_bound, upper_bound,
-              min_n, last_n_searched, 1, /*do_header_rd_based_gating=*/false,
+              max_n, last_n_searched, -1, /*do_header_rd_based_gating=*/false,
               &unused, color_cache, n_cache, best_mbmi, best_palette_color_map,
               best_rd, rate, rate_tokenonly, distortion, skippable,
               beat_best_rd, ctx, best_blk_skip, tx_type_map, color_map,