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