xref: /aosp_15_r20/external/libaom/av1/encoder/tokenize.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 #include <assert.h>
13 #include <math.h>
14 #include <stdio.h>
15 #include <string.h>
16 
17 #include "aom_mem/aom_mem.h"
18 
19 #include "av1/common/entropy.h"
20 #include "av1/common/pred_common.h"
21 #include "av1/common/scan.h"
22 #include "av1/common/seg_common.h"
23 
24 #include "av1/encoder/cost.h"
25 #include "av1/encoder/encoder.h"
26 #include "av1/encoder/encodetxb.h"
27 #include "av1/encoder/rdopt.h"
28 #include "av1/encoder/tokenize.h"
29 
av1_fast_palette_color_index_context_on_edge(const uint8_t * color_map,int stride,int r,int c,int * color_idx)30 static inline int av1_fast_palette_color_index_context_on_edge(
31     const uint8_t *color_map, int stride, int r, int c, int *color_idx) {
32   const bool has_left = (c - 1 >= 0);
33   const bool has_above = (r - 1 >= 0);
34   assert(r > 0 || c > 0);
35   assert(has_above ^ has_left);
36   assert(color_idx);
37   (void)has_left;
38 
39   const uint8_t color_neighbor = has_above
40                                      ? color_map[(r - 1) * stride + (c - 0)]
41                                      : color_map[(r - 0) * stride + (c - 1)];
42   // If the neighbor color has higher index than current color index, then we
43   // move up by 1.
44   const uint8_t current_color = *color_idx = color_map[r * stride + c];
45   if (color_neighbor > current_color) {
46     (*color_idx)++;
47   } else if (color_neighbor == current_color) {
48     *color_idx = 0;
49   }
50 
51   // Get hash value of context.
52   // The non-diagonal neighbors get a weight of 2.
53   const uint8_t color_score = 2;
54   const uint8_t hash_multiplier = 1;
55   const uint8_t color_index_ctx_hash = color_score * hash_multiplier;
56 
57   // Lookup context from hash.
58   const int color_index_ctx =
59       av1_palette_color_index_context_lookup[color_index_ctx_hash];
60   assert(color_index_ctx == 0);
61   (void)color_index_ctx;
62   return 0;
63 }
64 
65 #define SWAP(i, j)                           \
66   do {                                       \
67     const uint8_t tmp_score = score_rank[i]; \
68     const uint8_t tmp_color = color_rank[i]; \
69     score_rank[i] = score_rank[j];           \
70     color_rank[i] = color_rank[j];           \
71     score_rank[j] = tmp_score;               \
72     color_rank[j] = tmp_color;               \
73   } while (0)
74 #define INVALID_COLOR_IDX (UINT8_MAX)
75 
76 // A faster version of av1_get_palette_color_index_context used by the encoder
77 // exploiting the fact that the encoder does not need to maintain a color order.
av1_fast_palette_color_index_context(const uint8_t * color_map,int stride,int r,int c,int * color_idx)78 static inline int av1_fast_palette_color_index_context(const uint8_t *color_map,
79                                                        int stride, int r, int c,
80                                                        int *color_idx) {
81   assert(r > 0 || c > 0);
82 
83   const bool has_above = (r - 1 >= 0);
84   const bool has_left = (c - 1 >= 0);
85   assert(has_above || has_left);
86   if (has_above ^ has_left) {
87     return av1_fast_palette_color_index_context_on_edge(color_map, stride, r, c,
88                                                         color_idx);
89   }
90 
91   // This goes in the order of left, top, and top-left. This has the advantage
92   // that unless anything here are not distinct or invalid, this will already
93   // be in sorted order. Furthermore, if either of the first two is
94   // invalid, we know the last one is also invalid.
95   uint8_t color_neighbors[NUM_PALETTE_NEIGHBORS];
96   color_neighbors[0] = color_map[(r - 0) * stride + (c - 1)];
97   color_neighbors[1] = color_map[(r - 1) * stride + (c - 0)];
98   color_neighbors[2] = color_map[(r - 1) * stride + (c - 1)];
99 
100   // Aggregate duplicated values.
101   // Since our array is so small, using a couple if statements is faster
102   uint8_t scores[NUM_PALETTE_NEIGHBORS] = { 2, 2, 1 };
103   uint8_t num_invalid_colors = 0;
104   if (color_neighbors[0] == color_neighbors[1]) {
105     scores[0] += scores[1];
106     color_neighbors[1] = INVALID_COLOR_IDX;
107     num_invalid_colors += 1;
108 
109     if (color_neighbors[0] == color_neighbors[2]) {
110       scores[0] += scores[2];
111       num_invalid_colors += 1;
112     }
113   } else if (color_neighbors[0] == color_neighbors[2]) {
114     scores[0] += scores[2];
115     num_invalid_colors += 1;
116   } else if (color_neighbors[1] == color_neighbors[2]) {
117     scores[1] += scores[2];
118     num_invalid_colors += 1;
119   }
120 
121   const uint8_t num_valid_colors = NUM_PALETTE_NEIGHBORS - num_invalid_colors;
122 
123   uint8_t *color_rank = color_neighbors;
124   uint8_t *score_rank = scores;
125 
126   // Sort everything
127   if (num_valid_colors > 1) {
128     if (color_neighbors[1] == INVALID_COLOR_IDX) {
129       scores[1] = scores[2];
130       color_neighbors[1] = color_neighbors[2];
131     }
132 
133     // We need to swap the first two elements if they have the same score but
134     // the color indices are not in the right order
135     if (score_rank[0] < score_rank[1] ||
136         (score_rank[0] == score_rank[1] && color_rank[0] > color_rank[1])) {
137       SWAP(0, 1);
138     }
139     if (num_valid_colors > 2) {
140       if (score_rank[0] < score_rank[2]) {
141         SWAP(0, 2);
142       }
143       if (score_rank[1] < score_rank[2]) {
144         SWAP(1, 2);
145       }
146     }
147   }
148 
149   // If any of the neighbor colors has higher index than current color index,
150   // then we move up by 1 unless the current color is the same as one of the
151   // neighbors.
152   const uint8_t current_color = *color_idx = color_map[r * stride + c];
153   for (int idx = 0; idx < num_valid_colors; idx++) {
154     if (color_rank[idx] > current_color) {
155       (*color_idx)++;
156     } else if (color_rank[idx] == current_color) {
157       *color_idx = idx;
158       break;
159     }
160   }
161 
162   // Get hash value of context.
163   uint8_t color_index_ctx_hash = 0;
164   static const uint8_t hash_multipliers[NUM_PALETTE_NEIGHBORS] = { 1, 2, 2 };
165   for (int idx = 0; idx < num_valid_colors; ++idx) {
166     color_index_ctx_hash += score_rank[idx] * hash_multipliers[idx];
167   }
168   assert(color_index_ctx_hash > 0);
169   assert(color_index_ctx_hash <= MAX_COLOR_CONTEXT_HASH);
170 
171   // Lookup context from hash.
172   const int color_index_ctx = 9 - color_index_ctx_hash;
173   assert(color_index_ctx ==
174          av1_palette_color_index_context_lookup[color_index_ctx_hash]);
175   assert(color_index_ctx >= 0);
176   assert(color_index_ctx < PALETTE_COLOR_INDEX_CONTEXTS);
177   return color_index_ctx;
178 }
179 #undef INVALID_COLOR_IDX
180 #undef SWAP
181 
cost_and_tokenize_map(Av1ColorMapParam * param,TokenExtra ** t,int plane,int calc_rate,int allow_update_cdf,FRAME_COUNTS * counts)182 static int cost_and_tokenize_map(Av1ColorMapParam *param, TokenExtra **t,
183                                  int plane, int calc_rate, int allow_update_cdf,
184                                  FRAME_COUNTS *counts) {
185   const uint8_t *const color_map = param->color_map;
186   MapCdf map_cdf = param->map_cdf;
187   ColorCost color_cost = param->color_cost;
188   const int plane_block_width = param->plane_width;
189   const int rows = param->rows;
190   const int cols = param->cols;
191   const int n = param->n_colors;
192   const int palette_size_idx = n - PALETTE_MIN_SIZE;
193   int this_rate = 0;
194 
195   (void)plane;
196   (void)counts;
197 
198   for (int k = 1; k < rows + cols - 1; ++k) {
199     for (int j = AOMMIN(k, cols - 1); j >= AOMMAX(0, k - rows + 1); --j) {
200       int i = k - j;
201       int color_new_idx;
202       const int color_ctx = av1_fast_palette_color_index_context(
203           color_map, plane_block_width, i, j, &color_new_idx);
204       assert(color_new_idx >= 0 && color_new_idx < n);
205       if (calc_rate) {
206         this_rate += color_cost[palette_size_idx][color_ctx][color_new_idx];
207       } else {
208         (*t)->token = color_new_idx;
209         (*t)->color_ctx = color_ctx;
210         ++(*t);
211         if (allow_update_cdf)
212           update_cdf(map_cdf[palette_size_idx][color_ctx], color_new_idx, n);
213 #if CONFIG_ENTROPY_STATS
214         if (plane) {
215           ++counts->palette_uv_color_index[palette_size_idx][color_ctx]
216                                           [color_new_idx];
217         } else {
218           ++counts->palette_y_color_index[palette_size_idx][color_ctx]
219                                          [color_new_idx];
220         }
221 #endif
222       }
223     }
224   }
225   if (calc_rate) return this_rate;
226   return 0;
227 }
228 
get_palette_params(const MACROBLOCK * const x,int plane,BLOCK_SIZE bsize,Av1ColorMapParam * params)229 static void get_palette_params(const MACROBLOCK *const x, int plane,
230                                BLOCK_SIZE bsize, Av1ColorMapParam *params) {
231   const MACROBLOCKD *const xd = &x->e_mbd;
232   const MB_MODE_INFO *const mbmi = xd->mi[0];
233   const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
234   params->color_map = xd->plane[plane].color_index_map;
235   params->map_cdf = plane ? xd->tile_ctx->palette_uv_color_index_cdf
236                           : xd->tile_ctx->palette_y_color_index_cdf;
237   params->color_cost = plane ? x->mode_costs.palette_uv_color_cost
238                              : x->mode_costs.palette_y_color_cost;
239   params->n_colors = pmi->palette_size[plane];
240   av1_get_block_dimensions(bsize, plane, xd, &params->plane_width, NULL,
241                            &params->rows, &params->cols);
242 }
243 
244 // TODO(any): Remove this function
get_color_map_params(const MACROBLOCK * const x,int plane,BLOCK_SIZE bsize,TX_SIZE tx_size,COLOR_MAP_TYPE type,Av1ColorMapParam * params)245 static void get_color_map_params(const MACROBLOCK *const x, int plane,
246                                  BLOCK_SIZE bsize, TX_SIZE tx_size,
247                                  COLOR_MAP_TYPE type,
248                                  Av1ColorMapParam *params) {
249   (void)tx_size;
250   memset(params, 0, sizeof(*params));
251   switch (type) {
252     case PALETTE_MAP: get_palette_params(x, plane, bsize, params); break;
253     default: assert(0 && "Invalid color map type"); return;
254   }
255 }
256 
av1_cost_color_map(const MACROBLOCK * const x,int plane,BLOCK_SIZE bsize,TX_SIZE tx_size,COLOR_MAP_TYPE type)257 int av1_cost_color_map(const MACROBLOCK *const x, int plane, BLOCK_SIZE bsize,
258                        TX_SIZE tx_size, COLOR_MAP_TYPE type) {
259   assert(plane == 0 || plane == 1);
260   Av1ColorMapParam color_map_params;
261   get_color_map_params(x, plane, bsize, tx_size, type, &color_map_params);
262   return cost_and_tokenize_map(&color_map_params, NULL, plane, 1, 0, NULL);
263 }
264 
av1_tokenize_color_map(const MACROBLOCK * const x,int plane,TokenExtra ** t,BLOCK_SIZE bsize,TX_SIZE tx_size,COLOR_MAP_TYPE type,int allow_update_cdf,FRAME_COUNTS * counts)265 void av1_tokenize_color_map(const MACROBLOCK *const x, int plane,
266                             TokenExtra **t, BLOCK_SIZE bsize, TX_SIZE tx_size,
267                             COLOR_MAP_TYPE type, int allow_update_cdf,
268                             FRAME_COUNTS *counts) {
269   assert(plane == 0 || plane == 1);
270   Av1ColorMapParam color_map_params;
271   get_color_map_params(x, plane, bsize, tx_size, type, &color_map_params);
272   // The first color index does not use context or entropy.
273   (*t)->token = color_map_params.color_map[0];
274   (*t)->color_ctx = -1;
275   ++(*t);
276   cost_and_tokenize_map(&color_map_params, t, plane, 0, allow_update_cdf,
277                         counts);
278 }
279 
tokenize_vartx(ThreadData * td,TX_SIZE tx_size,BLOCK_SIZE plane_bsize,int blk_row,int blk_col,int block,int plane,void * arg)280 static void tokenize_vartx(ThreadData *td, TX_SIZE tx_size,
281                            BLOCK_SIZE plane_bsize, int blk_row, int blk_col,
282                            int block, int plane, void *arg) {
283   MACROBLOCK *const x = &td->mb;
284   MACROBLOCKD *const xd = &x->e_mbd;
285   MB_MODE_INFO *const mbmi = xd->mi[0];
286   const struct macroblockd_plane *const pd = &xd->plane[plane];
287   const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
288   const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
289 
290   if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
291 
292   const TX_SIZE plane_tx_size =
293       plane ? av1_get_max_uv_txsize(mbmi->bsize, pd->subsampling_x,
294                                     pd->subsampling_y)
295             : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row,
296                                                          blk_col)];
297 
298   if (tx_size == plane_tx_size || plane) {
299     plane_bsize =
300         get_plane_block_size(mbmi->bsize, pd->subsampling_x, pd->subsampling_y);
301 
302     struct tokenize_b_args *args = arg;
303     if (args->allow_update_cdf)
304       av1_update_and_record_txb_context(plane, block, blk_row, blk_col,
305                                         plane_bsize, tx_size, arg);
306     else
307       av1_record_txb_context(plane, block, blk_row, blk_col, plane_bsize,
308                              tx_size, arg);
309 
310   } else {
311     // Half the block size in transform block unit.
312     const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
313     const int bsw = tx_size_wide_unit[sub_txs];
314     const int bsh = tx_size_high_unit[sub_txs];
315     const int step = bsw * bsh;
316     const int row_end =
317         AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row);
318     const int col_end =
319         AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col);
320 
321     assert(bsw > 0 && bsh > 0);
322 
323     for (int row = 0; row < row_end; row += bsh) {
324       const int offsetr = blk_row + row;
325       for (int col = 0; col < col_end; col += bsw) {
326         const int offsetc = blk_col + col;
327 
328         tokenize_vartx(td, sub_txs, plane_bsize, offsetr, offsetc, block, plane,
329                        arg);
330         block += step;
331       }
332     }
333   }
334 }
335 
av1_tokenize_sb_vartx(const AV1_COMP * cpi,ThreadData * td,RUN_TYPE dry_run,BLOCK_SIZE bsize,int * rate,uint8_t allow_update_cdf)336 void av1_tokenize_sb_vartx(const AV1_COMP *cpi, ThreadData *td,
337                            RUN_TYPE dry_run, BLOCK_SIZE bsize, int *rate,
338                            uint8_t allow_update_cdf) {
339   assert(bsize < BLOCK_SIZES_ALL);
340   const AV1_COMMON *const cm = &cpi->common;
341   MACROBLOCK *const x = &td->mb;
342   MACROBLOCKD *const xd = &x->e_mbd;
343   const int mi_row = xd->mi_row;
344   const int mi_col = xd->mi_col;
345   if (mi_row >= cm->mi_params.mi_rows || mi_col >= cm->mi_params.mi_cols)
346     return;
347 
348   const int num_planes = av1_num_planes(cm);
349   MB_MODE_INFO *const mbmi = xd->mi[0];
350   struct tokenize_b_args arg = { cpi, td, 0, allow_update_cdf, dry_run };
351 
352   if (mbmi->skip_txfm) {
353     av1_reset_entropy_context(xd, bsize, num_planes);
354     return;
355   }
356 
357   for (int plane = 0; plane < num_planes; ++plane) {
358     if (plane && !xd->is_chroma_ref) break;
359     const struct macroblockd_plane *const pd = &xd->plane[plane];
360     const int ss_x = pd->subsampling_x;
361     const int ss_y = pd->subsampling_y;
362     const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y);
363     assert(plane_bsize < BLOCK_SIZES_ALL);
364     const int mi_width = mi_size_wide[plane_bsize];
365     const int mi_height = mi_size_high[plane_bsize];
366     const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, plane_bsize, plane);
367     const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
368     const int bw = mi_size_wide[txb_size];
369     const int bh = mi_size_high[txb_size];
370     int block = 0;
371     const int step =
372         tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
373 
374     const BLOCK_SIZE max_unit_bsize =
375         get_plane_block_size(BLOCK_64X64, ss_x, ss_y);
376     int mu_blocks_wide = mi_size_wide[max_unit_bsize];
377     int mu_blocks_high = mi_size_high[max_unit_bsize];
378 
379     mu_blocks_wide = AOMMIN(mi_width, mu_blocks_wide);
380     mu_blocks_high = AOMMIN(mi_height, mu_blocks_high);
381 
382     for (int idy = 0; idy < mi_height; idy += mu_blocks_high) {
383       for (int idx = 0; idx < mi_width; idx += mu_blocks_wide) {
384         const int unit_height = AOMMIN(mu_blocks_high + idy, mi_height);
385         const int unit_width = AOMMIN(mu_blocks_wide + idx, mi_width);
386         for (int blk_row = idy; blk_row < unit_height; blk_row += bh) {
387           for (int blk_col = idx; blk_col < unit_width; blk_col += bw) {
388             tokenize_vartx(td, max_tx_size, plane_bsize, blk_row, blk_col,
389                            block, plane, &arg);
390             block += step;
391           }
392         }
393       }
394     }
395   }
396   if (rate) *rate += arg.this_rate;
397 }
398