/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 int cost_and_tokenize_map(Av1ColorMapParam *param, TokenExtra **t, |
31 | | int plane, int calc_rate, int allow_update_cdf, |
32 | 0 | FRAME_COUNTS *counts) { |
33 | 0 | const uint8_t *const color_map = param->color_map; |
34 | 0 | MapCdf map_cdf = param->map_cdf; |
35 | 0 | ColorCost color_cost = param->color_cost; |
36 | 0 | const int plane_block_width = param->plane_width; |
37 | 0 | const int rows = param->rows; |
38 | 0 | const int cols = param->cols; |
39 | 0 | const int n = param->n_colors; |
40 | 0 | const int palette_size_idx = n - PALETTE_MIN_SIZE; |
41 | 0 | int this_rate = 0; |
42 | |
|
43 | 0 | (void)plane; |
44 | 0 | (void)counts; |
45 | |
|
46 | 0 | for (int k = 1; k < rows + cols - 1; ++k) { |
47 | 0 | for (int j = AOMMIN(k, cols - 1); j >= AOMMAX(0, k - rows + 1); --j) { |
48 | 0 | int i = k - j; |
49 | 0 | int color_new_idx; |
50 | 0 | const int color_ctx = av1_fast_palette_color_index_context( |
51 | 0 | color_map, plane_block_width, i, j, &color_new_idx); |
52 | 0 | assert(color_new_idx >= 0 && color_new_idx < n); |
53 | 0 | if (calc_rate) { |
54 | 0 | this_rate += color_cost[palette_size_idx][color_ctx][color_new_idx]; |
55 | 0 | } else { |
56 | 0 | (*t)->token = color_new_idx; |
57 | 0 | (*t)->color_ctx = color_ctx; |
58 | 0 | ++(*t); |
59 | 0 | if (allow_update_cdf) |
60 | 0 | update_cdf(map_cdf[palette_size_idx][color_ctx], color_new_idx, n); |
61 | | #if CONFIG_ENTROPY_STATS |
62 | | if (plane) { |
63 | | ++counts->palette_uv_color_index[palette_size_idx][color_ctx] |
64 | | [color_new_idx]; |
65 | | } else { |
66 | | ++counts->palette_y_color_index[palette_size_idx][color_ctx] |
67 | | [color_new_idx]; |
68 | | } |
69 | | #endif |
70 | 0 | } |
71 | 0 | } |
72 | 0 | } |
73 | 0 | if (calc_rate) return this_rate; |
74 | 0 | return 0; |
75 | 0 | } |
76 | | |
77 | | static void get_palette_params(const MACROBLOCK *const x, int plane, |
78 | 0 | BLOCK_SIZE bsize, Av1ColorMapParam *params) { |
79 | 0 | const MACROBLOCKD *const xd = &x->e_mbd; |
80 | 0 | const MB_MODE_INFO *const mbmi = xd->mi[0]; |
81 | 0 | const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
82 | 0 | params->color_map = xd->plane[plane].color_index_map; |
83 | 0 | params->map_cdf = plane ? xd->tile_ctx->palette_uv_color_index_cdf |
84 | 0 | : xd->tile_ctx->palette_y_color_index_cdf; |
85 | 0 | params->color_cost = plane ? x->mode_costs.palette_uv_color_cost |
86 | 0 | : x->mode_costs.palette_y_color_cost; |
87 | 0 | params->n_colors = pmi->palette_size[plane]; |
88 | 0 | av1_get_block_dimensions(bsize, plane, xd, ¶ms->plane_width, NULL, |
89 | 0 | ¶ms->rows, ¶ms->cols); |
90 | 0 | } |
91 | | |
92 | | // TODO(any): Remove this function |
93 | | static void get_color_map_params(const MACROBLOCK *const x, int plane, |
94 | | BLOCK_SIZE bsize, TX_SIZE tx_size, |
95 | | COLOR_MAP_TYPE type, |
96 | 0 | Av1ColorMapParam *params) { |
97 | 0 | (void)tx_size; |
98 | 0 | memset(params, 0, sizeof(*params)); |
99 | 0 | switch (type) { |
100 | 0 | case PALETTE_MAP: get_palette_params(x, plane, bsize, params); break; |
101 | 0 | default: assert(0 && "Invalid color map type"); return; |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | | int av1_cost_color_map(const MACROBLOCK *const x, int plane, BLOCK_SIZE bsize, |
106 | 0 | TX_SIZE tx_size, COLOR_MAP_TYPE type) { |
107 | 0 | assert(plane == 0 || plane == 1); |
108 | 0 | Av1ColorMapParam color_map_params; |
109 | 0 | get_color_map_params(x, plane, bsize, tx_size, type, &color_map_params); |
110 | 0 | return cost_and_tokenize_map(&color_map_params, NULL, plane, 1, 0, NULL); |
111 | 0 | } |
112 | | |
113 | | void av1_tokenize_color_map(const MACROBLOCK *const x, int plane, |
114 | | TokenExtra **t, BLOCK_SIZE bsize, TX_SIZE tx_size, |
115 | | COLOR_MAP_TYPE type, int allow_update_cdf, |
116 | 0 | FRAME_COUNTS *counts) { |
117 | 0 | assert(plane == 0 || plane == 1); |
118 | 0 | Av1ColorMapParam color_map_params; |
119 | 0 | get_color_map_params(x, plane, bsize, tx_size, type, &color_map_params); |
120 | | // The first color index does not use context or entropy. |
121 | 0 | (*t)->token = color_map_params.color_map[0]; |
122 | 0 | (*t)->color_ctx = -1; |
123 | 0 | ++(*t); |
124 | 0 | cost_and_tokenize_map(&color_map_params, t, plane, 0, allow_update_cdf, |
125 | 0 | counts); |
126 | 0 | } |
127 | | |
128 | | static void tokenize_vartx(ThreadData *td, TX_SIZE tx_size, |
129 | | BLOCK_SIZE plane_bsize, int blk_row, int blk_col, |
130 | 0 | int block, int plane, void *arg) { |
131 | 0 | MACROBLOCK *const x = &td->mb; |
132 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
133 | 0 | MB_MODE_INFO *const mbmi = xd->mi[0]; |
134 | 0 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
135 | 0 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
136 | 0 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
137 | |
|
138 | 0 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
139 | | |
140 | 0 | const TX_SIZE plane_tx_size = |
141 | 0 | plane ? av1_get_max_uv_txsize(mbmi->bsize, pd->subsampling_x, |
142 | 0 | pd->subsampling_y) |
143 | 0 | : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row, |
144 | 0 | blk_col)]; |
145 | |
|
146 | 0 | if (tx_size == plane_tx_size || plane) { |
147 | 0 | plane_bsize = |
148 | 0 | get_plane_block_size(mbmi->bsize, pd->subsampling_x, pd->subsampling_y); |
149 | 0 | av1_update_and_record_txb_context(plane, block, blk_row, blk_col, |
150 | 0 | plane_bsize, tx_size, arg); |
151 | |
|
152 | 0 | } else { |
153 | | // Half the block size in transform block unit. |
154 | 0 | const TX_SIZE sub_txs = sub_tx_size_map[tx_size]; |
155 | 0 | const int bsw = tx_size_wide_unit[sub_txs]; |
156 | 0 | const int bsh = tx_size_high_unit[sub_txs]; |
157 | 0 | const int step = bsw * bsh; |
158 | 0 | const int row_end = |
159 | 0 | AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row); |
160 | 0 | const int col_end = |
161 | 0 | AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col); |
162 | |
|
163 | 0 | assert(bsw > 0 && bsh > 0); |
164 | |
|
165 | 0 | for (int row = 0; row < row_end; row += bsh) { |
166 | 0 | const int offsetr = blk_row + row; |
167 | 0 | for (int col = 0; col < col_end; col += bsw) { |
168 | 0 | const int offsetc = blk_col + col; |
169 | |
|
170 | 0 | tokenize_vartx(td, sub_txs, plane_bsize, offsetr, offsetc, block, plane, |
171 | 0 | arg); |
172 | 0 | block += step; |
173 | 0 | } |
174 | 0 | } |
175 | 0 | } |
176 | 0 | } |
177 | | |
178 | | void av1_tokenize_sb_vartx(const AV1_COMP *cpi, ThreadData *td, |
179 | | RUN_TYPE dry_run, BLOCK_SIZE bsize, int *rate, |
180 | 0 | uint8_t allow_update_cdf) { |
181 | 0 | assert(bsize < BLOCK_SIZES_ALL); |
182 | 0 | const AV1_COMMON *const cm = &cpi->common; |
183 | 0 | MACROBLOCK *const x = &td->mb; |
184 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
185 | 0 | const int mi_row = xd->mi_row; |
186 | 0 | const int mi_col = xd->mi_col; |
187 | 0 | if (mi_row >= cm->mi_params.mi_rows || mi_col >= cm->mi_params.mi_cols) |
188 | 0 | return; |
189 | | |
190 | 0 | const int num_planes = av1_num_planes(cm); |
191 | 0 | MB_MODE_INFO *const mbmi = xd->mi[0]; |
192 | 0 | struct tokenize_b_args arg = { cpi, td, 0, allow_update_cdf, dry_run }; |
193 | |
|
194 | 0 | if (mbmi->skip_txfm) { |
195 | 0 | av1_reset_entropy_context(xd, bsize, num_planes); |
196 | 0 | return; |
197 | 0 | } |
198 | | |
199 | 0 | for (int plane = 0; plane < num_planes; ++plane) { |
200 | 0 | if (plane && !xd->is_chroma_ref) break; |
201 | 0 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
202 | 0 | const int ss_x = pd->subsampling_x; |
203 | 0 | const int ss_y = pd->subsampling_y; |
204 | 0 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y); |
205 | 0 | assert(plane_bsize < BLOCK_SIZES_ALL); |
206 | 0 | const int mi_width = mi_size_wide[plane_bsize]; |
207 | 0 | const int mi_height = mi_size_high[plane_bsize]; |
208 | 0 | const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, plane_bsize, plane); |
209 | 0 | const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size]; |
210 | 0 | const int bw = mi_size_wide[txb_size]; |
211 | 0 | const int bh = mi_size_high[txb_size]; |
212 | 0 | int block = 0; |
213 | 0 | const int step = |
214 | 0 | tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size]; |
215 | |
|
216 | 0 | const BLOCK_SIZE max_unit_bsize = |
217 | 0 | get_plane_block_size(BLOCK_64X64, ss_x, ss_y); |
218 | 0 | int mu_blocks_wide = mi_size_wide[max_unit_bsize]; |
219 | 0 | int mu_blocks_high = mi_size_high[max_unit_bsize]; |
220 | |
|
221 | 0 | mu_blocks_wide = AOMMIN(mi_width, mu_blocks_wide); |
222 | 0 | mu_blocks_high = AOMMIN(mi_height, mu_blocks_high); |
223 | |
|
224 | 0 | for (int idy = 0; idy < mi_height; idy += mu_blocks_high) { |
225 | 0 | for (int idx = 0; idx < mi_width; idx += mu_blocks_wide) { |
226 | 0 | const int unit_height = AOMMIN(mu_blocks_high + idy, mi_height); |
227 | 0 | const int unit_width = AOMMIN(mu_blocks_wide + idx, mi_width); |
228 | 0 | for (int blk_row = idy; blk_row < unit_height; blk_row += bh) { |
229 | 0 | for (int blk_col = idx; blk_col < unit_width; blk_col += bw) { |
230 | 0 | tokenize_vartx(td, max_tx_size, plane_bsize, blk_row, blk_col, |
231 | 0 | block, plane, &arg); |
232 | 0 | block += step; |
233 | 0 | } |
234 | 0 | } |
235 | 0 | } |
236 | 0 | } |
237 | 0 | } |
238 | 0 | if (rate) *rate += arg.this_rate; |
239 | 0 | } |