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,MapCdf map_pb_cdf)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, MapCdf map_pb_cdf) {
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_pb_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 MapCdf map_pb_cdf = plane ? x->tile_pb_ctx->palette_uv_color_index_cdf
111 : x->tile_pb_ctx->palette_y_color_index_cdf;
112 return cost_and_tokenize_map(&color_map_params, NULL, plane, 1, 0, NULL,
113 map_pb_cdf);
114 }
115
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)116 void av1_tokenize_color_map(const MACROBLOCK *const x, int plane,
117 TOKENEXTRA **t, BLOCK_SIZE bsize, TX_SIZE tx_size,
118 COLOR_MAP_TYPE type, int allow_update_cdf,
119 FRAME_COUNTS *counts) {
120 assert(plane == 0 || plane == 1);
121 Av1ColorMapParam color_map_params;
122 get_color_map_params(x, plane, bsize, tx_size, type, &color_map_params);
123 // The first color index does not use context or entropy.
124 (*t)->token = color_map_params.color_map[0];
125 (*t)->color_map_cdf = NULL;
126 ++(*t);
127 MapCdf map_pb_cdf = plane ? x->tile_pb_ctx->palette_uv_color_index_cdf
128 : x->tile_pb_ctx->palette_y_color_index_cdf;
129 cost_and_tokenize_map(&color_map_params, t, plane, 0, allow_update_cdf,
130 counts, map_pb_cdf);
131 }
132
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)133 static void tokenize_vartx(ThreadData *td, TOKENEXTRA **t, RUN_TYPE dry_run,
134 TX_SIZE tx_size, BLOCK_SIZE plane_bsize, int blk_row,
135 int blk_col, int block, int plane, void *arg) {
136 MACROBLOCK *const x = &td->mb;
137 MACROBLOCKD *const xd = &x->e_mbd;
138 MB_MODE_INFO *const mbmi = xd->mi[0];
139 const struct macroblockd_plane *const pd = &xd->plane[plane];
140 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
141 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
142
143 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
144
145 const TX_SIZE plane_tx_size =
146 plane ? av1_get_max_uv_txsize(mbmi->sb_type, pd->subsampling_x,
147 pd->subsampling_y)
148 : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row,
149 blk_col)];
150
151 if (tx_size == plane_tx_size || plane) {
152 plane_bsize = get_plane_block_size(mbmi->sb_type, pd->subsampling_x,
153 pd->subsampling_y);
154 if (!dry_run) {
155 av1_update_and_record_txb_context(plane, block, blk_row, blk_col,
156 plane_bsize, tx_size, arg);
157 } else if (dry_run == DRY_RUN_NORMAL) {
158 av1_update_txb_context_b(plane, block, blk_row, blk_col, plane_bsize,
159 tx_size, arg);
160 } else {
161 printf("DRY_RUN_COSTCOEFFS is not supported yet\n");
162 assert(0);
163 }
164 } else {
165 // Half the block size in transform block unit.
166 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
167 const int bsw = tx_size_wide_unit[sub_txs];
168 const int bsh = tx_size_high_unit[sub_txs];
169 const int step = bsw * bsh;
170
171 assert(bsw > 0 && bsh > 0);
172
173 for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) {
174 for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) {
175 const int offsetr = blk_row + row;
176 const int offsetc = blk_col + col;
177
178 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
179
180 tokenize_vartx(td, t, dry_run, sub_txs, plane_bsize, offsetr, offsetc,
181 block, plane, arg);
182 block += step;
183 }
184 }
185 }
186 }
187
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)188 void av1_tokenize_sb_vartx(const AV1_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
189 RUN_TYPE dry_run, int mi_row, int mi_col,
190 BLOCK_SIZE bsize, int *rate,
191 uint8_t allow_update_cdf) {
192 const AV1_COMMON *const cm = &cpi->common;
193 const int num_planes = av1_num_planes(cm);
194 MACROBLOCK *const x = &td->mb;
195 MACROBLOCKD *const xd = &x->e_mbd;
196 MB_MODE_INFO *const mbmi = xd->mi[0];
197 (void)t;
198 struct tokenize_b_args arg = { cpi, td, t, 0, allow_update_cdf };
199 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
200
201 if (mbmi->skip) {
202 av1_reset_skip_context(xd, mi_row, mi_col, bsize, num_planes);
203 return;
204 }
205
206 for (int plane = 0; plane < num_planes; ++plane) {
207 if (!is_chroma_reference(mi_row, mi_col, bsize,
208 xd->plane[plane].subsampling_x,
209 xd->plane[plane].subsampling_y)) {
210 continue;
211 }
212 const struct macroblockd_plane *const pd = &xd->plane[plane];
213 const BLOCK_SIZE bsizec =
214 scale_chroma_bsize(bsize, pd->subsampling_x, pd->subsampling_y);
215 const BLOCK_SIZE plane_bsize =
216 get_plane_block_size(bsizec, pd->subsampling_x, pd->subsampling_y);
217 const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
218 const int mi_height = block_size_high[plane_bsize] >> tx_size_high_log2[0];
219 const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, plane_bsize, plane);
220 const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
221 int bw = block_size_wide[txb_size] >> tx_size_wide_log2[0];
222 int bh = block_size_high[txb_size] >> tx_size_high_log2[0];
223 int idx, idy;
224 int block = 0;
225 int step = tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
226
227 const BLOCK_SIZE max_unit_bsize =
228 get_plane_block_size(BLOCK_64X64, pd->subsampling_x, pd->subsampling_y);
229 int mu_blocks_wide =
230 block_size_wide[max_unit_bsize] >> tx_size_wide_log2[0];
231 int mu_blocks_high =
232 block_size_high[max_unit_bsize] >> tx_size_high_log2[0];
233
234 mu_blocks_wide = AOMMIN(mi_width, mu_blocks_wide);
235 mu_blocks_high = AOMMIN(mi_height, mu_blocks_high);
236
237 for (idy = 0; idy < mi_height; idy += mu_blocks_high) {
238 for (idx = 0; idx < mi_width; idx += mu_blocks_wide) {
239 int blk_row, blk_col;
240 const int unit_height = AOMMIN(mu_blocks_high + idy, mi_height);
241 const int unit_width = AOMMIN(mu_blocks_wide + idx, mi_width);
242 for (blk_row = idy; blk_row < unit_height; blk_row += bh) {
243 for (blk_col = idx; blk_col < unit_width; blk_col += bw) {
244 tokenize_vartx(td, t, dry_run, max_tx_size, plane_bsize, blk_row,
245 blk_col, block, plane, &arg);
246 block += step;
247 }
248 }
249 }
250 }
251 }
252 if (rate) *rate += arg.this_rate;
253 }
254