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