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