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,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, TX_SIZE tx_size,
134 BLOCK_SIZE plane_bsize, int blk_row, int blk_col,
135 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 av1_update_and_record_txb_context(plane, block, blk_row, blk_col,
155 plane_bsize, tx_size, arg);
156
157 } else {
158 // Half the block size in transform block unit.
159 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
160 const int bsw = tx_size_wide_unit[sub_txs];
161 const int bsh = tx_size_high_unit[sub_txs];
162 const int step = bsw * bsh;
163
164 assert(bsw > 0 && bsh > 0);
165
166 for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) {
167 for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) {
168 const int offsetr = blk_row + row;
169 const int offsetc = blk_col + col;
170
171 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
172
173 tokenize_vartx(td, sub_txs, plane_bsize, offsetr, offsetc, block, plane,
174 arg);
175 block += step;
176 }
177 }
178 }
179 }
180
av1_tokenize_sb_vartx(const AV1_COMP * cpi,ThreadData * td,RUN_TYPE dry_run,BLOCK_SIZE bsize,int * rate,uint8_t allow_update_cdf)181 void av1_tokenize_sb_vartx(const AV1_COMP *cpi, ThreadData *td,
182 RUN_TYPE dry_run, BLOCK_SIZE bsize, int *rate,
183 uint8_t allow_update_cdf) {
184 assert(bsize < BLOCK_SIZES_ALL);
185 const AV1_COMMON *const cm = &cpi->common;
186 MACROBLOCK *const x = &td->mb;
187 MACROBLOCKD *const xd = &x->e_mbd;
188 const int mi_row = xd->mi_row;
189 const int mi_col = xd->mi_col;
190 if (mi_row >= cm->mi_params.mi_rows || mi_col >= cm->mi_params.mi_cols)
191 return;
192
193 const int num_planes = av1_num_planes(cm);
194 MB_MODE_INFO *const mbmi = xd->mi[0];
195 struct tokenize_b_args arg = { cpi, td, 0, allow_update_cdf, dry_run };
196
197 if (mbmi->skip) {
198 av1_reset_entropy_context(xd, bsize, num_planes);
199 return;
200 }
201
202 for (int plane = 0; plane < num_planes; ++plane) {
203 if (plane && !xd->is_chroma_ref) break;
204 const struct macroblockd_plane *const pd = &xd->plane[plane];
205 const int ss_x = pd->subsampling_x;
206 const int ss_y = pd->subsampling_y;
207 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y);
208 assert(plane_bsize < BLOCK_SIZES_ALL);
209 const int mi_width = mi_size_wide[plane_bsize];
210 const int mi_height = mi_size_high[plane_bsize];
211 const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, plane_bsize, plane);
212 const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
213 const int bw = mi_size_wide[txb_size];
214 const int bh = mi_size_high[txb_size];
215 int block = 0;
216 const int step =
217 tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
218
219 const BLOCK_SIZE max_unit_bsize =
220 get_plane_block_size(BLOCK_64X64, ss_x, ss_y);
221 int mu_blocks_wide = mi_size_wide[max_unit_bsize];
222 int mu_blocks_high = mi_size_high[max_unit_bsize];
223
224 mu_blocks_wide = AOMMIN(mi_width, mu_blocks_wide);
225 mu_blocks_high = AOMMIN(mi_height, mu_blocks_high);
226
227 for (int idy = 0; idy < mi_height; idy += mu_blocks_high) {
228 for (int idx = 0; idx < mi_width; idx += mu_blocks_wide) {
229 const int unit_height = AOMMIN(mu_blocks_high + idy, mi_height);
230 const int unit_width = AOMMIN(mu_blocks_wide + idx, mi_width);
231 for (int blk_row = idy; blk_row < unit_height; blk_row += bh) {
232 for (int blk_col = idx; blk_col < unit_width; blk_col += bw) {
233 tokenize_vartx(td, max_tx_size, plane_bsize, blk_row, blk_col,
234 block, plane, &arg);
235 block += step;
236 }
237 }
238 }
239 }
240 }
241 if (rate) *rate += arg.this_rate;
242 }
243