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