Palette: Special case for 2 unique colors. When a block only contains 2 unique colors, there's no need to run K-means, as 'lb' and 'ub' can directly be used as the centroids. Two benefits: 1. Encoding will be faster for this special case, and 2. Can potentially avoid sub-optimal/duplicate centroids generation (due to integer rounding) when 'ub' and 'lb' values are very close. Change-Id: Ice04fc8577e3cc8c74f87d623a16ac8de3257cdd
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c index 33512aa..2953a5e 100644 --- a/av1/encoder/rdopt.c +++ b/av1/encoder/rdopt.c
@@ -2497,14 +2497,24 @@ for (n = colors > PALETTE_MAX_SIZE ? PALETTE_MAX_SIZE : colors; n >= 2; --n) { - for (i = 0; i < n; ++i) - centroids[i] = lb + (2 * i + 1) * (ub - lb) / n / 2; - av1_k_means(data, centroids, color_map, rows * cols, n, 1, max_itr); - k = av1_remove_duplicates(centroids, n); - if (k < PALETTE_MIN_SIZE) { - // Too few unique colors to create a palette. And DC_PRED will work well - // for that case anyway. So skip. - continue; + if (colors == PALETTE_MIN_SIZE) { + // Special case: These colors automatically become the centroids. + assert(colors == n); + assert(colors == 2); + centroids[0] = lb; + centroids[1] = ub; + k = 2; + } else { + for (i = 0; i < n; ++i) { + centroids[i] = lb + (2 * i + 1) * (ub - lb) / n / 2; + } + av1_k_means(data, centroids, color_map, rows * cols, n, 1, max_itr); + k = av1_remove_duplicates(centroids, n); + if (k < PALETTE_MIN_SIZE) { + // Too few unique colors to create a palette. And DC_PRED will work + // well for that case anyway. So skip. + continue; + } } #if CONFIG_HIGHBITDEPTH