1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker /*!\file
13*77c1e3ccSAndroid Build Coastguard Worker * \brief Declares functions used in palette search.
14*77c1e3ccSAndroid Build Coastguard Worker */
15*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AV1_ENCODER_PALETTE_H_
16*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_ENCODER_PALETTE_H_
17*77c1e3ccSAndroid Build Coastguard Worker
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/blockd.h"
19*77c1e3ccSAndroid Build Coastguard Worker
20*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
21*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
22*77c1e3ccSAndroid Build Coastguard Worker #endif
23*77c1e3ccSAndroid Build Coastguard Worker
24*77c1e3ccSAndroid Build Coastguard Worker struct AV1_COMP;
25*77c1e3ccSAndroid Build Coastguard Worker struct PICK_MODE_CONTEXT;
26*77c1e3ccSAndroid Build Coastguard Worker struct macroblock;
27*77c1e3ccSAndroid Build Coastguard Worker
28*77c1e3ccSAndroid Build Coastguard Worker /*!\cond */
29*77c1e3ccSAndroid Build Coastguard Worker #define AV1_K_MEANS_RENAME(func, dim) func##_dim##dim
30*77c1e3ccSAndroid Build Coastguard Worker
31*77c1e3ccSAndroid Build Coastguard Worker void AV1_K_MEANS_RENAME(av1_k_means, 1)(const int16_t *data, int16_t *centroids,
32*77c1e3ccSAndroid Build Coastguard Worker uint8_t *indices, int n, int k,
33*77c1e3ccSAndroid Build Coastguard Worker int max_itr);
34*77c1e3ccSAndroid Build Coastguard Worker void AV1_K_MEANS_RENAME(av1_k_means, 2)(const int16_t *data, int16_t *centroids,
35*77c1e3ccSAndroid Build Coastguard Worker uint8_t *indices, int n, int k,
36*77c1e3ccSAndroid Build Coastguard Worker int max_itr);
37*77c1e3ccSAndroid Build Coastguard Worker /*!\endcond */
38*77c1e3ccSAndroid Build Coastguard Worker
39*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Calculates the cluster to which each data point belong.
40*77c1e3ccSAndroid Build Coastguard Worker *
41*77c1e3ccSAndroid Build Coastguard Worker * \ingroup palette_mode_search
42*77c1e3ccSAndroid Build Coastguard Worker * \param[in] data The data points whose cluster indices are
43*77c1e3ccSAndroid Build Coastguard Worker * to be computed. The data layout is
44*77c1e3ccSAndroid Build Coastguard Worker * NUM_DATA_POINTS X DATA_DIM.
45*77c1e3ccSAndroid Build Coastguard Worker * \param[in] centroids Pointer to the centroids. The data layout
46*77c1e3ccSAndroid Build Coastguard Worker * is NUM_CENTROIDS X DATA_DIM.
47*77c1e3ccSAndroid Build Coastguard Worker * \param[in] indices Pointer to store the computed indices.
48*77c1e3ccSAndroid Build Coastguard Worker * \param[in] n Number of data points.
49*77c1e3ccSAndroid Build Coastguard Worker * \param[in] k Number of clusters.
50*77c1e3ccSAndroid Build Coastguard Worker * \param[in] dim Data dimension.
51*77c1e3ccSAndroid Build Coastguard Worker *
52*77c1e3ccSAndroid Build Coastguard Worker * \remark Returns nothing, but saves each data's cluster index in \a indices.
53*77c1e3ccSAndroid Build Coastguard Worker */
av1_calc_indices(const int16_t * data,const int16_t * centroids,uint8_t * indices,int n,int k,int dim)54*77c1e3ccSAndroid Build Coastguard Worker static inline void av1_calc_indices(const int16_t *data,
55*77c1e3ccSAndroid Build Coastguard Worker const int16_t *centroids, uint8_t *indices,
56*77c1e3ccSAndroid Build Coastguard Worker int n, int k, int dim) {
57*77c1e3ccSAndroid Build Coastguard Worker assert(n > 0);
58*77c1e3ccSAndroid Build Coastguard Worker assert(k > 0);
59*77c1e3ccSAndroid Build Coastguard Worker if (dim == 1) {
60*77c1e3ccSAndroid Build Coastguard Worker av1_calc_indices_dim1(data, centroids, indices, /*total_dist=*/NULL, n, k);
61*77c1e3ccSAndroid Build Coastguard Worker } else if (dim == 2) {
62*77c1e3ccSAndroid Build Coastguard Worker av1_calc_indices_dim2(data, centroids, indices, /*total_dist=*/NULL, n, k);
63*77c1e3ccSAndroid Build Coastguard Worker } else {
64*77c1e3ccSAndroid Build Coastguard Worker assert(0 && "Untemplated k means dimension");
65*77c1e3ccSAndroid Build Coastguard Worker }
66*77c1e3ccSAndroid Build Coastguard Worker }
67*77c1e3ccSAndroid Build Coastguard Worker
68*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Performs k-means cluster on the data.
69*77c1e3ccSAndroid Build Coastguard Worker *
70*77c1e3ccSAndroid Build Coastguard Worker * \ingroup palette_mode_search
71*77c1e3ccSAndroid Build Coastguard Worker * \param[in] data The data points to be clustered. The data
72*77c1e3ccSAndroid Build Coastguard Worker * layout is NUM_DATA_POINTS X DATA_DIM.
73*77c1e3ccSAndroid Build Coastguard Worker * \param[in] centroids Pointer to store the computed centroids.
74*77c1e3ccSAndroid Build Coastguard Worker * The data layout is
75*77c1e3ccSAndroid Build Coastguard Worker * NUM_CENTROIDS X DATA_DIM.
76*77c1e3ccSAndroid Build Coastguard Worker * \param[in] indices Pointer to store the computed indices. For
77*77c1e3ccSAndroid Build Coastguard Worker * each training data.
78*77c1e3ccSAndroid Build Coastguard Worker * \param[in] n Number of data points.
79*77c1e3ccSAndroid Build Coastguard Worker * \param[in] k Number of clusters.
80*77c1e3ccSAndroid Build Coastguard Worker * \param[in] dim Data dimension.
81*77c1e3ccSAndroid Build Coastguard Worker * \param[in] max_itr Maximum number of iterations to run.
82*77c1e3ccSAndroid Build Coastguard Worker *
83*77c1e3ccSAndroid Build Coastguard Worker * \remark Returns nothing, but saves each cluster's centroid in centroids and
84*77c1e3ccSAndroid Build Coastguard Worker * each data's cluster index in \a indices.
85*77c1e3ccSAndroid Build Coastguard Worker *
86*77c1e3ccSAndroid Build Coastguard Worker * \attention The output centroids are rounded off to nearest integers.
87*77c1e3ccSAndroid Build Coastguard Worker */
av1_k_means(const int16_t * data,int16_t * centroids,uint8_t * indices,int n,int k,int dim,int max_itr)88*77c1e3ccSAndroid Build Coastguard Worker static inline void av1_k_means(const int16_t *data, int16_t *centroids,
89*77c1e3ccSAndroid Build Coastguard Worker uint8_t *indices, int n, int k, int dim,
90*77c1e3ccSAndroid Build Coastguard Worker int max_itr) {
91*77c1e3ccSAndroid Build Coastguard Worker assert(n > 0);
92*77c1e3ccSAndroid Build Coastguard Worker assert(k > 0);
93*77c1e3ccSAndroid Build Coastguard Worker if (dim == 1) {
94*77c1e3ccSAndroid Build Coastguard Worker AV1_K_MEANS_RENAME(av1_k_means, 1)(data, centroids, indices, n, k, max_itr);
95*77c1e3ccSAndroid Build Coastguard Worker } else if (dim == 2) {
96*77c1e3ccSAndroid Build Coastguard Worker AV1_K_MEANS_RENAME(av1_k_means, 2)(data, centroids, indices, n, k, max_itr);
97*77c1e3ccSAndroid Build Coastguard Worker } else {
98*77c1e3ccSAndroid Build Coastguard Worker assert(0 && "Untemplated k means dimension");
99*77c1e3ccSAndroid Build Coastguard Worker }
100*77c1e3ccSAndroid Build Coastguard Worker }
101*77c1e3ccSAndroid Build Coastguard Worker
102*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Checks what colors are in the color cache.
103*77c1e3ccSAndroid Build Coastguard Worker *
104*77c1e3ccSAndroid Build Coastguard Worker * \ingroup palette_mode_search
105*77c1e3ccSAndroid Build Coastguard Worker * \param[in] color_cache A cache of colors.
106*77c1e3ccSAndroid Build Coastguard Worker * \param[in] n_cache Number of colors in the cache.
107*77c1e3ccSAndroid Build Coastguard Worker * \param[in] colors New base colors.
108*77c1e3ccSAndroid Build Coastguard Worker * \param[in] n_colors Number of new colors.
109*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cache_color_found Stores what cached colors are presented in
110*77c1e3ccSAndroid Build Coastguard Worker * colors.
111*77c1e3ccSAndroid Build Coastguard Worker * \param[in] out_cache_colors Stores what colors are not in the cache.
112*77c1e3ccSAndroid Build Coastguard Worker *
113*77c1e3ccSAndroid Build Coastguard Worker * \return Returns the number of colors that are not in cache. In addition,
114*77c1e3ccSAndroid Build Coastguard Worker * records whether each cache color is presented in colors in cache_color_found,
115*77c1e3ccSAndroid Build Coastguard Worker * and stores and stores the out of cache colors in out_cache_colors.
116*77c1e3ccSAndroid Build Coastguard Worker */
117*77c1e3ccSAndroid Build Coastguard Worker int av1_index_color_cache(const uint16_t *color_cache, int n_cache,
118*77c1e3ccSAndroid Build Coastguard Worker const uint16_t *colors, int n_colors,
119*77c1e3ccSAndroid Build Coastguard Worker uint8_t *cache_color_found, int *out_cache_colors);
120*77c1e3ccSAndroid Build Coastguard Worker
121*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Gets the rate cost for each delta-encoding v palette.
122*77c1e3ccSAndroid Build Coastguard Worker *
123*77c1e3ccSAndroid Build Coastguard Worker * \ingroup palette_mode_search
124*77c1e3ccSAndroid Build Coastguard Worker * \param[in] pmi Struct that stores the palette mode info.
125*77c1e3ccSAndroid Build Coastguard Worker * \param[in] bit_depth Pixel bitdepth of the sequence.
126*77c1e3ccSAndroid Build Coastguard Worker * \param[in] zero_count Stores the number of zero deltas.
127*77c1e3ccSAndroid Build Coastguard Worker * \param[in] min_bits Minimum bits for the deltas. Sets to
128*77c1e3ccSAndroid Build Coastguard Worker * bit_depth - 4.
129*77c1e3ccSAndroid Build Coastguard Worker *
130*77c1e3ccSAndroid Build Coastguard Worker * \return Returns the number of bits used to transmit each v palette color
131*77c1e3ccSAndroid Build Coastguard Worker * delta and assigns zero_count with the number of deltas being 0.
132*77c1e3ccSAndroid Build Coastguard Worker */
133*77c1e3ccSAndroid Build Coastguard Worker int av1_get_palette_delta_bits_v(const PALETTE_MODE_INFO *const pmi,
134*77c1e3ccSAndroid Build Coastguard Worker int bit_depth, int *zero_count, int *min_bits);
135*77c1e3ccSAndroid Build Coastguard Worker
136*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Gets the rate cost for transmitting luma palette color values.
137*77c1e3ccSAndroid Build Coastguard Worker *
138*77c1e3ccSAndroid Build Coastguard Worker * \ingroup palette_mode_search
139*77c1e3ccSAndroid Build Coastguard Worker * \param[in] pmi Struct that stores the palette mode info.
140*77c1e3ccSAndroid Build Coastguard Worker * \param[in] color_cache Color cache presented at the decoder.
141*77c1e3ccSAndroid Build Coastguard Worker * \param[in] n_cache Number of colors in the cache.
142*77c1e3ccSAndroid Build Coastguard Worker * \param[in] bit_depth Pixel bitdepth of the sequence.
143*77c1e3ccSAndroid Build Coastguard Worker *
144*77c1e3ccSAndroid Build Coastguard Worker * \return Returns the rate needed to transmit the palette. Note that this does
145*77c1e3ccSAndroid Build Coastguard Worker * not include the cost of transmitted the color map.
146*77c1e3ccSAndroid Build Coastguard Worker */
147*77c1e3ccSAndroid Build Coastguard Worker int av1_palette_color_cost_y(const PALETTE_MODE_INFO *const pmi,
148*77c1e3ccSAndroid Build Coastguard Worker const uint16_t *color_cache, int n_cache,
149*77c1e3ccSAndroid Build Coastguard Worker int bit_depth);
150*77c1e3ccSAndroid Build Coastguard Worker
151*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Gets the rate cost for transmitting luma palette chroma values.
152*77c1e3ccSAndroid Build Coastguard Worker *
153*77c1e3ccSAndroid Build Coastguard Worker * \ingroup palette_mode_search
154*77c1e3ccSAndroid Build Coastguard Worker * \param[in] pmi Struct that stores the palette mode info.
155*77c1e3ccSAndroid Build Coastguard Worker * \param[in] color_cache Color cache presented at the decoder.
156*77c1e3ccSAndroid Build Coastguard Worker * \param[in] n_cache Number of colors in the cache.
157*77c1e3ccSAndroid Build Coastguard Worker * \param[in] bit_depth Pixel bitdepth of the sequence.
158*77c1e3ccSAndroid Build Coastguard Worker *
159*77c1e3ccSAndroid Build Coastguard Worker * \return Returns the rate needed to transmit the palette. Note that this does
160*77c1e3ccSAndroid Build Coastguard Worker * not include the cost of transmitted the color map.
161*77c1e3ccSAndroid Build Coastguard Worker */
162*77c1e3ccSAndroid Build Coastguard Worker int av1_palette_color_cost_uv(const PALETTE_MODE_INFO *const pmi,
163*77c1e3ccSAndroid Build Coastguard Worker const uint16_t *color_cache, int n_cache,
164*77c1e3ccSAndroid Build Coastguard Worker int bit_depth);
165*77c1e3ccSAndroid Build Coastguard Worker
166*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Search for the best palette in the luma plane.
167*77c1e3ccSAndroid Build Coastguard Worker *
168*77c1e3ccSAndroid Build Coastguard Worker * \ingroup palette_mode_search
169*77c1e3ccSAndroid Build Coastguard Worker * \callergraph
170*77c1e3ccSAndroid Build Coastguard Worker * This function is used in both inter and intra frame coding.
171*77c1e3ccSAndroid Build Coastguard Worker */
172*77c1e3ccSAndroid Build Coastguard Worker void av1_rd_pick_palette_intra_sby(
173*77c1e3ccSAndroid Build Coastguard Worker const struct AV1_COMP *cpi, struct macroblock *x, BLOCK_SIZE bsize,
174*77c1e3ccSAndroid Build Coastguard Worker int dc_mode_cost, MB_MODE_INFO *best_mbmi, uint8_t *best_palette_color_map,
175*77c1e3ccSAndroid Build Coastguard Worker int64_t *best_rd, int *rate, int *rate_tokenonly, int64_t *distortion,
176*77c1e3ccSAndroid Build Coastguard Worker uint8_t *skippable, int *beat_best_rd, struct PICK_MODE_CONTEXT *ctx,
177*77c1e3ccSAndroid Build Coastguard Worker uint8_t *best_blk_skip, uint8_t *tx_type_map);
178*77c1e3ccSAndroid Build Coastguard Worker
179*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Search for the best palette in the chroma plane.
180*77c1e3ccSAndroid Build Coastguard Worker *
181*77c1e3ccSAndroid Build Coastguard Worker * \ingroup palette_mode_search
182*77c1e3ccSAndroid Build Coastguard Worker * \callergraph
183*77c1e3ccSAndroid Build Coastguard Worker * This function is used in both inter and intra frame coding.
184*77c1e3ccSAndroid Build Coastguard Worker */
185*77c1e3ccSAndroid Build Coastguard Worker void av1_rd_pick_palette_intra_sbuv(const struct AV1_COMP *cpi,
186*77c1e3ccSAndroid Build Coastguard Worker struct macroblock *x, int dc_mode_cost,
187*77c1e3ccSAndroid Build Coastguard Worker uint8_t *best_palette_color_map,
188*77c1e3ccSAndroid Build Coastguard Worker MB_MODE_INFO *const best_mbmi,
189*77c1e3ccSAndroid Build Coastguard Worker int64_t *best_rd, int *rate,
190*77c1e3ccSAndroid Build Coastguard Worker int *rate_tokenonly, int64_t *distortion,
191*77c1e3ccSAndroid Build Coastguard Worker uint8_t *skippable);
192*77c1e3ccSAndroid Build Coastguard Worker
193*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Resets palette color map for chroma channels.
194*77c1e3ccSAndroid Build Coastguard Worker */
195*77c1e3ccSAndroid Build Coastguard Worker void av1_restore_uv_color_map(const struct AV1_COMP *cpi, struct macroblock *x);
196*77c1e3ccSAndroid Build Coastguard Worker
197*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
198*77c1e3ccSAndroid Build Coastguard Worker } // extern "C"
199*77c1e3ccSAndroid Build Coastguard Worker #endif
200*77c1e3ccSAndroid Build Coastguard Worker
201*77c1e3ccSAndroid Build Coastguard Worker #endif // AOM_AV1_ENCODER_PALETTE_H_
202