/src/aom/av1/encoder/tokenize.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
30 | | static inline int av1_fast_palette_color_index_context_on_edge( |
31 | 0 | const uint8_t *color_map, int stride, int r, int c, int *color_idx) { |
32 | 0 | const bool has_left = (c - 1 >= 0); |
33 | 0 | const bool has_above = (r - 1 >= 0); |
34 | 0 | assert(r > 0 || c > 0); |
35 | 0 | assert(has_above ^ has_left); |
36 | 0 | assert(color_idx); |
37 | 0 | (void)has_left; |
38 | |
|
39 | 0 | const uint8_t color_neighbor = has_above |
40 | 0 | ? color_map[(r - 1) * stride + (c - 0)] |
41 | 0 | : 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 | 0 | const uint8_t current_color = *color_idx = color_map[r * stride + c]; |
45 | 0 | if (color_neighbor > current_color) { |
46 | 0 | (*color_idx)++; |
47 | 0 | } else if (color_neighbor == current_color) { |
48 | 0 | *color_idx = 0; |
49 | 0 | } |
50 | | |
51 | | // Get hash value of context. |
52 | | // The non-diagonal neighbors get a weight of 2. |
53 | 0 | const uint8_t color_score = 2; |
54 | 0 | const uint8_t hash_multiplier = 1; |
55 | 0 | const uint8_t color_index_ctx_hash = color_score * hash_multiplier; |
56 | | |
57 | | // Lookup context from hash. |
58 | 0 | const int color_index_ctx = |
59 | 0 | av1_palette_color_index_context_lookup[color_index_ctx_hash]; |
60 | 0 | assert(color_index_ctx == 0); |
61 | 0 | (void)color_index_ctx; |
62 | 0 | return 0; |
63 | 0 | } |
64 | | |
65 | | #define SWAP(i, j) \ |
66 | 0 | do { \ |
67 | 0 | const uint8_t tmp_score = score_rank[i]; \ |
68 | 0 | const uint8_t tmp_color = color_rank[i]; \ |
69 | 0 | score_rank[i] = score_rank[j]; \ |
70 | 0 | color_rank[i] = color_rank[j]; \ |
71 | 0 | score_rank[j] = tmp_score; \ |
72 | 0 | color_rank[j] = tmp_color; \ |
73 | 0 | } while (0) |
74 | 0 | #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. |
78 | | static inline int av1_fast_palette_color_index_context(const uint8_t *color_map, |
79 | | int stride, int r, int c, |
80 | 0 | int *color_idx) { |
81 | 0 | assert(r > 0 || c > 0); |
82 | |
|
83 | 0 | const bool has_above = (r - 1 >= 0); |
84 | 0 | const bool has_left = (c - 1 >= 0); |
85 | 0 | assert(has_above || has_left); |
86 | 0 | if (has_above ^ has_left) { |
87 | 0 | return av1_fast_palette_color_index_context_on_edge(color_map, stride, r, c, |
88 | 0 | color_idx); |
89 | 0 | } |
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 | 0 | uint8_t color_neighbors[NUM_PALETTE_NEIGHBORS]; |
96 | 0 | color_neighbors[0] = color_map[(r - 0) * stride + (c - 1)]; |
97 | 0 | color_neighbors[1] = color_map[(r - 1) * stride + (c - 0)]; |
98 | 0 | 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 | 0 | uint8_t scores[NUM_PALETTE_NEIGHBORS] = { 2, 2, 1 }; |
103 | 0 | uint8_t num_invalid_colors = 0; |
104 | 0 | if (color_neighbors[0] == color_neighbors[1]) { |
105 | 0 | scores[0] += scores[1]; |
106 | 0 | color_neighbors[1] = INVALID_COLOR_IDX; |
107 | 0 | num_invalid_colors += 1; |
108 | |
|
109 | 0 | if (color_neighbors[0] == color_neighbors[2]) { |
110 | 0 | scores[0] += scores[2]; |
111 | 0 | num_invalid_colors += 1; |
112 | 0 | } |
113 | 0 | } else if (color_neighbors[0] == color_neighbors[2]) { |
114 | 0 | scores[0] += scores[2]; |
115 | 0 | num_invalid_colors += 1; |
116 | 0 | } else if (color_neighbors[1] == color_neighbors[2]) { |
117 | 0 | scores[1] += scores[2]; |
118 | 0 | num_invalid_colors += 1; |
119 | 0 | } |
120 | |
|
121 | 0 | const uint8_t num_valid_colors = NUM_PALETTE_NEIGHBORS - num_invalid_colors; |
122 | |
|
123 | 0 | uint8_t *color_rank = color_neighbors; |
124 | 0 | uint8_t *score_rank = scores; |
125 | | |
126 | | // Sort everything |
127 | 0 | if (num_valid_colors > 1) { |
128 | 0 | if (color_neighbors[1] == INVALID_COLOR_IDX) { |
129 | 0 | scores[1] = scores[2]; |
130 | 0 | color_neighbors[1] = color_neighbors[2]; |
131 | 0 | } |
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 | 0 | if (score_rank[0] < score_rank[1] || |
136 | 0 | (score_rank[0] == score_rank[1] && color_rank[0] > color_rank[1])) { |
137 | 0 | SWAP(0, 1); |
138 | 0 | } |
139 | 0 | if (num_valid_colors > 2) { |
140 | 0 | if (score_rank[0] < score_rank[2]) { |
141 | 0 | SWAP(0, 2); |
142 | 0 | } |
143 | 0 | if (score_rank[1] < score_rank[2]) { |
144 | 0 | SWAP(1, 2); |
145 | 0 | } |
146 | 0 | } |
147 | 0 | } |
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 | 0 | const uint8_t current_color = *color_idx = color_map[r * stride + c]; |
153 | 0 | for (int idx = 0; idx < num_valid_colors; idx++) { |
154 | 0 | if (color_rank[idx] > current_color) { |
155 | 0 | (*color_idx)++; |
156 | 0 | } else if (color_rank[idx] == current_color) { |
157 | 0 | *color_idx = idx; |
158 | 0 | break; |
159 | 0 | } |
160 | 0 | } |
161 | | |
162 | | // Get hash value of context. |
163 | 0 | uint8_t color_index_ctx_hash = 0; |
164 | 0 | static const uint8_t hash_multipliers[NUM_PALETTE_NEIGHBORS] = { 1, 2, 2 }; |
165 | 0 | for (int idx = 0; idx < num_valid_colors; ++idx) { |
166 | 0 | color_index_ctx_hash += score_rank[idx] * hash_multipliers[idx]; |
167 | 0 | } |
168 | 0 | assert(color_index_ctx_hash > 0); |
169 | 0 | assert(color_index_ctx_hash <= MAX_COLOR_CONTEXT_HASH); |
170 | | |
171 | | // Lookup context from hash. |
172 | 0 | const int color_index_ctx = 9 - color_index_ctx_hash; |
173 | 0 | assert(color_index_ctx == |
174 | 0 | av1_palette_color_index_context_lookup[color_index_ctx_hash]); |
175 | 0 | assert(color_index_ctx >= 0); |
176 | 0 | assert(color_index_ctx < PALETTE_COLOR_INDEX_CONTEXTS); |
177 | 0 | return color_index_ctx; |
178 | 0 | } |
179 | | #undef INVALID_COLOR_IDX |
180 | | #undef SWAP |
181 | | |
182 | | static int cost_and_tokenize_map(Av1ColorMapParam *param, TokenExtra **t, |
183 | | int plane, int calc_rate, int allow_update_cdf, |
184 | 0 | FRAME_COUNTS *counts) { |
185 | 0 | const uint8_t *const color_map = param->color_map; |
186 | 0 | MapCdf map_cdf = param->map_cdf; |
187 | 0 | ColorCost color_cost = param->color_cost; |
188 | 0 | const int plane_block_width = param->plane_width; |
189 | 0 | const int rows = param->rows; |
190 | 0 | const int cols = param->cols; |
191 | 0 | const int n = param->n_colors; |
192 | 0 | const int palette_size_idx = n - PALETTE_MIN_SIZE; |
193 | 0 | int this_rate = 0; |
194 | |
|
195 | 0 | (void)plane; |
196 | 0 | (void)counts; |
197 | |
|
198 | 0 | for (int k = 1; k < rows + cols - 1; ++k) { |
199 | 0 | for (int j = AOMMIN(k, cols - 1); j >= AOMMAX(0, k - rows + 1); --j) { |
200 | 0 | int i = k - j; |
201 | 0 | int color_new_idx; |
202 | 0 | const int color_ctx = av1_fast_palette_color_index_context( |
203 | 0 | color_map, plane_block_width, i, j, &color_new_idx); |
204 | 0 | assert(color_new_idx >= 0 && color_new_idx < n); |
205 | 0 | if (calc_rate) { |
206 | 0 | this_rate += color_cost[palette_size_idx][color_ctx][color_new_idx]; |
207 | 0 | } else { |
208 | 0 | (*t)->token = color_new_idx; |
209 | 0 | (*t)->color_ctx = color_ctx; |
210 | 0 | ++(*t); |
211 | 0 | if (allow_update_cdf) |
212 | 0 | 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 | 0 | } |
223 | 0 | } |
224 | 0 | } |
225 | 0 | if (calc_rate) return this_rate; |
226 | 0 | return 0; |
227 | 0 | } |
228 | | |
229 | | static void get_palette_params(const MACROBLOCK *const x, int plane, |
230 | 0 | BLOCK_SIZE bsize, Av1ColorMapParam *params) { |
231 | 0 | const MACROBLOCKD *const xd = &x->e_mbd; |
232 | 0 | const MB_MODE_INFO *const mbmi = xd->mi[0]; |
233 | 0 | const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
234 | 0 | params->color_map = xd->plane[plane].color_index_map; |
235 | 0 | params->map_cdf = plane ? xd->tile_ctx->palette_uv_color_index_cdf |
236 | 0 | : xd->tile_ctx->palette_y_color_index_cdf; |
237 | 0 | params->color_cost = plane ? x->mode_costs.palette_uv_color_cost |
238 | 0 | : x->mode_costs.palette_y_color_cost; |
239 | 0 | params->n_colors = pmi->palette_size[plane]; |
240 | 0 | av1_get_block_dimensions(bsize, plane, xd, ¶ms->plane_width, NULL, |
241 | 0 | ¶ms->rows, ¶ms->cols); |
242 | 0 | } |
243 | | |
244 | | // TODO(any): Remove this function |
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 | 0 | Av1ColorMapParam *params) { |
249 | 0 | (void)tx_size; |
250 | 0 | memset(params, 0, sizeof(*params)); |
251 | 0 | switch (type) { |
252 | 0 | case PALETTE_MAP: get_palette_params(x, plane, bsize, params); break; |
253 | 0 | default: assert(0 && "Invalid color map type"); return; |
254 | 0 | } |
255 | 0 | } |
256 | | |
257 | | int av1_cost_color_map(const MACROBLOCK *const x, int plane, BLOCK_SIZE bsize, |
258 | 0 | TX_SIZE tx_size, COLOR_MAP_TYPE type) { |
259 | 0 | assert(plane == 0 || plane == 1); |
260 | 0 | Av1ColorMapParam color_map_params; |
261 | 0 | get_color_map_params(x, plane, bsize, tx_size, type, &color_map_params); |
262 | 0 | return cost_and_tokenize_map(&color_map_params, NULL, plane, 1, 0, NULL); |
263 | 0 | } |
264 | | |
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 | 0 | FRAME_COUNTS *counts) { |
269 | 0 | assert(plane == 0 || plane == 1); |
270 | 0 | Av1ColorMapParam color_map_params; |
271 | 0 | 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 | 0 | (*t)->token = color_map_params.color_map[0]; |
274 | 0 | (*t)->color_ctx = -1; |
275 | 0 | ++(*t); |
276 | 0 | cost_and_tokenize_map(&color_map_params, t, plane, 0, allow_update_cdf, |
277 | 0 | counts); |
278 | 0 | } |
279 | | |
280 | | static void tokenize_vartx(ThreadData *td, TX_SIZE tx_size, |
281 | | BLOCK_SIZE plane_bsize, int blk_row, int blk_col, |
282 | 0 | int block, int plane, void *arg) { |
283 | 0 | MACROBLOCK *const x = &td->mb; |
284 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
285 | 0 | MB_MODE_INFO *const mbmi = xd->mi[0]; |
286 | 0 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
287 | 0 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
288 | 0 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
289 | |
|
290 | 0 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
291 | | |
292 | 0 | const TX_SIZE plane_tx_size = |
293 | 0 | plane ? av1_get_max_uv_txsize(mbmi->bsize, pd->subsampling_x, |
294 | 0 | pd->subsampling_y) |
295 | 0 | : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row, |
296 | 0 | blk_col)]; |
297 | |
|
298 | 0 | if (tx_size == plane_tx_size || plane) { |
299 | 0 | plane_bsize = |
300 | 0 | get_plane_block_size(mbmi->bsize, pd->subsampling_x, pd->subsampling_y); |
301 | |
|
302 | 0 | struct tokenize_b_args *args = arg; |
303 | 0 | if (args->allow_update_cdf) |
304 | 0 | av1_update_and_record_txb_context(plane, block, blk_row, blk_col, |
305 | 0 | plane_bsize, tx_size, arg); |
306 | 0 | else |
307 | 0 | av1_record_txb_context(plane, block, blk_row, blk_col, plane_bsize, |
308 | 0 | tx_size, arg); |
309 | |
|
310 | 0 | } else { |
311 | | // Half the block size in transform block unit. |
312 | 0 | const TX_SIZE sub_txs = sub_tx_size_map[tx_size]; |
313 | 0 | const int bsw = tx_size_wide_unit[sub_txs]; |
314 | 0 | const int bsh = tx_size_high_unit[sub_txs]; |
315 | 0 | const int step = bsw * bsh; |
316 | 0 | const int row_end = |
317 | 0 | AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row); |
318 | 0 | const int col_end = |
319 | 0 | AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col); |
320 | |
|
321 | 0 | assert(bsw > 0 && bsh > 0); |
322 | |
|
323 | 0 | for (int row = 0; row < row_end; row += bsh) { |
324 | 0 | const int offsetr = blk_row + row; |
325 | 0 | for (int col = 0; col < col_end; col += bsw) { |
326 | 0 | const int offsetc = blk_col + col; |
327 | |
|
328 | 0 | tokenize_vartx(td, sub_txs, plane_bsize, offsetr, offsetc, block, plane, |
329 | 0 | arg); |
330 | 0 | block += step; |
331 | 0 | } |
332 | 0 | } |
333 | 0 | } |
334 | 0 | } |
335 | | |
336 | | void av1_tokenize_sb_vartx(const AV1_COMP *cpi, ThreadData *td, |
337 | | RUN_TYPE dry_run, BLOCK_SIZE bsize, int *rate, |
338 | 0 | uint8_t allow_update_cdf) { |
339 | 0 | assert(bsize < BLOCK_SIZES_ALL); |
340 | 0 | const AV1_COMMON *const cm = &cpi->common; |
341 | 0 | MACROBLOCK *const x = &td->mb; |
342 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
343 | 0 | const int mi_row = xd->mi_row; |
344 | 0 | const int mi_col = xd->mi_col; |
345 | 0 | if (mi_row >= cm->mi_params.mi_rows || mi_col >= cm->mi_params.mi_cols) |
346 | 0 | return; |
347 | | |
348 | 0 | const int num_planes = av1_num_planes(cm); |
349 | 0 | MB_MODE_INFO *const mbmi = xd->mi[0]; |
350 | 0 | struct tokenize_b_args arg = { cpi, td, 0, allow_update_cdf, dry_run }; |
351 | |
|
352 | 0 | if (mbmi->skip_txfm) { |
353 | 0 | av1_reset_entropy_context(xd, bsize, num_planes); |
354 | 0 | return; |
355 | 0 | } |
356 | | |
357 | 0 | for (int plane = 0; plane < num_planes; ++plane) { |
358 | 0 | if (plane && !xd->is_chroma_ref) break; |
359 | 0 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
360 | 0 | const int ss_x = pd->subsampling_x; |
361 | 0 | const int ss_y = pd->subsampling_y; |
362 | 0 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y); |
363 | 0 | assert(plane_bsize < BLOCK_SIZES_ALL); |
364 | 0 | const int mi_width = mi_size_wide[plane_bsize]; |
365 | 0 | const int mi_height = mi_size_high[plane_bsize]; |
366 | 0 | const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, plane_bsize, plane); |
367 | 0 | const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size]; |
368 | 0 | const int bw = mi_size_wide[txb_size]; |
369 | 0 | const int bh = mi_size_high[txb_size]; |
370 | 0 | int block = 0; |
371 | 0 | const int step = |
372 | 0 | tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size]; |
373 | |
|
374 | 0 | const BLOCK_SIZE max_unit_bsize = |
375 | 0 | get_plane_block_size(BLOCK_64X64, ss_x, ss_y); |
376 | 0 | int mu_blocks_wide = mi_size_wide[max_unit_bsize]; |
377 | 0 | int mu_blocks_high = mi_size_high[max_unit_bsize]; |
378 | |
|
379 | 0 | mu_blocks_wide = AOMMIN(mi_width, mu_blocks_wide); |
380 | 0 | mu_blocks_high = AOMMIN(mi_height, mu_blocks_high); |
381 | |
|
382 | 0 | for (int idy = 0; idy < mi_height; idy += mu_blocks_high) { |
383 | 0 | for (int idx = 0; idx < mi_width; idx += mu_blocks_wide) { |
384 | 0 | const int unit_height = AOMMIN(mu_blocks_high + idy, mi_height); |
385 | 0 | const int unit_width = AOMMIN(mu_blocks_wide + idx, mi_width); |
386 | 0 | for (int blk_row = idy; blk_row < unit_height; blk_row += bh) { |
387 | 0 | for (int blk_col = idx; blk_col < unit_width; blk_col += bw) { |
388 | 0 | tokenize_vartx(td, max_tx_size, plane_bsize, blk_row, blk_col, |
389 | 0 | block, plane, &arg); |
390 | 0 | block += step; |
391 | 0 | } |
392 | 0 | } |
393 | 0 | } |
394 | 0 | } |
395 | 0 | } |
396 | 0 | if (rate) *rate += arg.this_rate; |
397 | 0 | } |