/src/libavif/ext/aom/av1/encoder/encodetxb.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2017, 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 "av1/encoder/encodetxb.h" |
13 | | |
14 | | #include <stdint.h> |
15 | | |
16 | | #include "aom_ports/mem.h" |
17 | | #include "av1/common/blockd.h" |
18 | | #include "av1/common/idct.h" |
19 | | #include "av1/common/pred_common.h" |
20 | | #include "av1/common/scan.h" |
21 | | #include "av1/encoder/bitstream.h" |
22 | | #include "av1/encoder/cost.h" |
23 | | #include "av1/encoder/encodeframe.h" |
24 | | #include "av1/encoder/hash.h" |
25 | | #include "av1/encoder/rdopt.h" |
26 | | #include "av1/encoder/tokenize.h" |
27 | | |
28 | 127k | void av1_alloc_txb_buf(AV1_COMP *cpi) { |
29 | 127k | AV1_COMMON *cm = &cpi->common; |
30 | 127k | CoeffBufferPool *coeff_buf_pool = &cpi->coeff_buffer_pool; |
31 | 127k | const int num_sb_rows = |
32 | 127k | CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, cm->seq_params->mib_size_log2); |
33 | 127k | const int num_sb_cols = |
34 | 127k | CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, cm->seq_params->mib_size_log2); |
35 | 127k | const int size = num_sb_rows * num_sb_cols; |
36 | 127k | const int num_planes = av1_num_planes(cm); |
37 | 127k | const int subsampling_x = cm->seq_params->subsampling_x; |
38 | 127k | const int subsampling_y = cm->seq_params->subsampling_y; |
39 | 127k | const int luma_max_sb_square = |
40 | 127k | 1 << num_pels_log2_lookup[cm->seq_params->sb_size]; |
41 | 127k | const int chroma_max_sb_square = |
42 | 127k | luma_max_sb_square >> (subsampling_x + subsampling_y); |
43 | 127k | const int total_max_sb_square = |
44 | 127k | (luma_max_sb_square + (num_planes - 1) * chroma_max_sb_square); |
45 | 127k | if ((size_t)size > SIZE_MAX / (size_t)total_max_sb_square) { |
46 | 0 | aom_internal_error(cm->error, AOM_CODEC_ERROR, |
47 | 0 | "A multiplication would overflow size_t"); |
48 | 0 | } |
49 | 127k | const size_t num_tcoeffs = (size_t)size * (size_t)total_max_sb_square; |
50 | 127k | const int txb_unit_size = TX_SIZE_W_MIN * TX_SIZE_H_MIN; |
51 | | |
52 | 127k | av1_free_txb_buf(cpi); |
53 | | // TODO(jingning): This should be further reduced. |
54 | 127k | CHECK_MEM_ERROR(cm, cpi->coeff_buffer_base, |
55 | 127k | aom_malloc(sizeof(*cpi->coeff_buffer_base) * size)); |
56 | 127k | if (sizeof(*coeff_buf_pool->tcoeff) > SIZE_MAX / num_tcoeffs) { |
57 | 0 | aom_internal_error(cm->error, AOM_CODEC_ERROR, |
58 | 0 | "A multiplication would overflow size_t"); |
59 | 0 | } |
60 | 127k | CHECK_MEM_ERROR( |
61 | 127k | cm, coeff_buf_pool->tcoeff, |
62 | 127k | aom_memalign(32, sizeof(*coeff_buf_pool->tcoeff) * num_tcoeffs)); |
63 | 127k | if (sizeof(*coeff_buf_pool->eobs) > SIZE_MAX / num_tcoeffs) { |
64 | 0 | aom_internal_error(cm->error, AOM_CODEC_ERROR, |
65 | 0 | "A multiplication would overflow size_t"); |
66 | 0 | } |
67 | 127k | CHECK_MEM_ERROR( |
68 | 127k | cm, coeff_buf_pool->eobs, |
69 | 127k | aom_malloc(sizeof(*coeff_buf_pool->eobs) * num_tcoeffs / txb_unit_size)); |
70 | 127k | if (sizeof(*coeff_buf_pool->entropy_ctx) > SIZE_MAX / num_tcoeffs) { |
71 | 0 | aom_internal_error(cm->error, AOM_CODEC_ERROR, |
72 | 0 | "A multiplication would overflow size_t"); |
73 | 0 | } |
74 | 127k | CHECK_MEM_ERROR(cm, coeff_buf_pool->entropy_ctx, |
75 | 127k | aom_malloc(sizeof(*coeff_buf_pool->entropy_ctx) * |
76 | 127k | num_tcoeffs / txb_unit_size)); |
77 | | |
78 | 127k | tran_low_t *tcoeff_ptr = coeff_buf_pool->tcoeff; |
79 | 127k | uint16_t *eob_ptr = coeff_buf_pool->eobs; |
80 | 127k | uint8_t *entropy_ctx_ptr = coeff_buf_pool->entropy_ctx; |
81 | 471k | for (int i = 0; i < size; i++) { |
82 | 944k | for (int plane = 0; plane < num_planes; plane++) { |
83 | 600k | const int max_sb_square = |
84 | 600k | (plane == AOM_PLANE_Y) ? luma_max_sb_square : chroma_max_sb_square; |
85 | 600k | cpi->coeff_buffer_base[i].tcoeff[plane] = tcoeff_ptr; |
86 | 600k | cpi->coeff_buffer_base[i].eobs[plane] = eob_ptr; |
87 | 600k | cpi->coeff_buffer_base[i].entropy_ctx[plane] = entropy_ctx_ptr; |
88 | 600k | tcoeff_ptr += max_sb_square; |
89 | 600k | eob_ptr += max_sb_square / txb_unit_size; |
90 | 600k | entropy_ctx_ptr += max_sb_square / txb_unit_size; |
91 | 600k | } |
92 | 343k | } |
93 | 127k | } |
94 | | |
95 | 201k | void av1_free_txb_buf(AV1_COMP *cpi) { |
96 | 201k | CoeffBufferPool *coeff_buf_pool = &cpi->coeff_buffer_pool; |
97 | 201k | aom_free(cpi->coeff_buffer_base); |
98 | 201k | cpi->coeff_buffer_base = NULL; |
99 | 201k | aom_free(coeff_buf_pool->tcoeff); |
100 | 201k | coeff_buf_pool->tcoeff = NULL; |
101 | 201k | aom_free(coeff_buf_pool->eobs); |
102 | 201k | coeff_buf_pool->eobs = NULL; |
103 | 201k | aom_free(coeff_buf_pool->entropy_ctx); |
104 | 201k | coeff_buf_pool->entropy_ctx = NULL; |
105 | 201k | } |
106 | | |
107 | 103M | static void write_golomb(aom_writer *w, int level) { |
108 | 103M | int x = level + 1; |
109 | 103M | int i = x; |
110 | 103M | int length = 0; |
111 | | |
112 | 609M | while (i) { |
113 | 506M | i >>= 1; |
114 | 506M | ++length; |
115 | 506M | } |
116 | 103M | assert(length > 0); |
117 | | |
118 | 493M | for (i = 0; i < length - 1; ++i) aom_write_bit(w, 0); |
119 | | |
120 | 585M | for (i = length - 1; i >= 0; --i) aom_write_bit(w, (x >> i) & 0x01); |
121 | 103M | } |
122 | | |
123 | | static const int8_t eob_to_pos_small[33] = { |
124 | | 0, 1, 2, // 0-2 |
125 | | 3, 3, // 3-4 |
126 | | 4, 4, 4, 4, // 5-8 |
127 | | 5, 5, 5, 5, 5, 5, 5, 5, // 9-16 |
128 | | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 // 17-32 |
129 | | }; |
130 | | |
131 | | static const int8_t eob_to_pos_large[17] = { |
132 | | 6, // place holder |
133 | | 7, // 33-64 |
134 | | 8, 8, // 65-128 |
135 | | 9, 9, 9, 9, // 129-256 |
136 | | 10, 10, 10, 10, 10, 10, 10, 10, // 257-512 |
137 | | 11 // 513- |
138 | | }; |
139 | | |
140 | 557M | int av1_get_eob_pos_token(const int eob, int *const extra) { |
141 | 557M | int t; |
142 | | |
143 | 557M | if (eob < 33) { |
144 | 337M | t = eob_to_pos_small[eob]; |
145 | 337M | } else { |
146 | 220M | const int e = AOMMIN((eob - 1) >> 5, 16); |
147 | 220M | t = eob_to_pos_large[e]; |
148 | 220M | } |
149 | | |
150 | 557M | *extra = eob - av1_eob_group_start[t]; |
151 | | |
152 | 557M | return t; |
153 | 557M | } |
154 | | |
155 | | #if CONFIG_ENTROPY_STATS |
156 | | static void update_eob_context(int cdf_idx, int eob, TX_SIZE tx_size, |
157 | | TX_CLASS tx_class, PLANE_TYPE plane, |
158 | | FRAME_CONTEXT *ec_ctx, FRAME_COUNTS *counts, |
159 | | uint8_t allow_update_cdf) { |
160 | | #else |
161 | | static void update_eob_context(int eob, TX_SIZE tx_size, TX_CLASS tx_class, |
162 | | PLANE_TYPE plane, FRAME_CONTEXT *ec_ctx, |
163 | 12.4M | uint8_t allow_update_cdf) { |
164 | 12.4M | #endif |
165 | 12.4M | int eob_extra; |
166 | 12.4M | const int eob_pt = av1_get_eob_pos_token(eob, &eob_extra); |
167 | 12.4M | TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
168 | | |
169 | 12.4M | const int eob_multi_size = txsize_log2_minus4[tx_size]; |
170 | 12.4M | const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1; |
171 | | |
172 | 12.4M | switch (eob_multi_size) { |
173 | 6.90M | case 0: |
174 | | #if CONFIG_ENTROPY_STATS |
175 | | ++counts->eob_multi16[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
176 | | #endif |
177 | 6.90M | if (allow_update_cdf) |
178 | 6.90M | update_cdf(ec_ctx->eob_flag_cdf16[plane][eob_multi_ctx], eob_pt - 1, 5); |
179 | 6.90M | break; |
180 | 891k | case 1: |
181 | | #if CONFIG_ENTROPY_STATS |
182 | | ++counts->eob_multi32[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
183 | | #endif |
184 | 891k | if (allow_update_cdf) |
185 | 891k | update_cdf(ec_ctx->eob_flag_cdf32[plane][eob_multi_ctx], eob_pt - 1, 6); |
186 | 891k | break; |
187 | 3.86M | case 2: |
188 | | #if CONFIG_ENTROPY_STATS |
189 | | ++counts->eob_multi64[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
190 | | #endif |
191 | 3.86M | if (allow_update_cdf) |
192 | 3.85M | update_cdf(ec_ctx->eob_flag_cdf64[plane][eob_multi_ctx], eob_pt - 1, 7); |
193 | 3.86M | break; |
194 | 211k | case 3: |
195 | | #if CONFIG_ENTROPY_STATS |
196 | | ++counts->eob_multi128[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
197 | | #endif |
198 | 211k | if (allow_update_cdf) { |
199 | 211k | update_cdf(ec_ctx->eob_flag_cdf128[plane][eob_multi_ctx], eob_pt - 1, |
200 | 211k | 8); |
201 | 211k | } |
202 | 211k | break; |
203 | 378k | case 4: |
204 | | #if CONFIG_ENTROPY_STATS |
205 | | ++counts->eob_multi256[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
206 | | #endif |
207 | 378k | if (allow_update_cdf) { |
208 | 378k | update_cdf(ec_ctx->eob_flag_cdf256[plane][eob_multi_ctx], eob_pt - 1, |
209 | 378k | 9); |
210 | 378k | } |
211 | 378k | break; |
212 | 64.9k | case 5: |
213 | | #if CONFIG_ENTROPY_STATS |
214 | | ++counts->eob_multi512[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
215 | | #endif |
216 | 64.9k | if (allow_update_cdf) { |
217 | 64.9k | update_cdf(ec_ctx->eob_flag_cdf512[plane][eob_multi_ctx], eob_pt - 1, |
218 | 64.9k | 10); |
219 | 64.9k | } |
220 | 64.9k | break; |
221 | 131k | case 6: |
222 | 131k | default: |
223 | | #if CONFIG_ENTROPY_STATS |
224 | | ++counts->eob_multi1024[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
225 | | #endif |
226 | 131k | if (allow_update_cdf) { |
227 | 131k | update_cdf(ec_ctx->eob_flag_cdf1024[plane][eob_multi_ctx], eob_pt - 1, |
228 | 131k | 11); |
229 | 131k | } |
230 | 131k | break; |
231 | 12.4M | } |
232 | | |
233 | 12.4M | if (av1_eob_offset_bits[eob_pt] > 0) { |
234 | 12.2M | int eob_ctx = eob_pt - 3; |
235 | 12.2M | int eob_shift = av1_eob_offset_bits[eob_pt] - 1; |
236 | 12.2M | int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0; |
237 | | #if CONFIG_ENTROPY_STATS |
238 | | counts->eob_extra[cdf_idx][txs_ctx][plane][eob_pt][bit]++; |
239 | | #endif // CONFIG_ENTROPY_STATS |
240 | 12.2M | if (allow_update_cdf) |
241 | 12.2M | update_cdf(ec_ctx->eob_extra_cdf[txs_ctx][plane][eob_ctx], bit, 2); |
242 | 12.2M | } |
243 | 12.4M | } |
244 | | |
245 | | static inline int get_nz_map_ctx(const uint8_t *const levels, |
246 | | const int coeff_idx, const int bhl, |
247 | | const int width, const int scan_idx, |
248 | | const int is_eob, const TX_SIZE tx_size, |
249 | 0 | const TX_CLASS tx_class) { |
250 | 0 | if (is_eob) { |
251 | 0 | if (scan_idx == 0) return 0; |
252 | 0 | if (scan_idx <= (width << bhl) / 8) return 1; |
253 | 0 | if (scan_idx <= (width << bhl) / 4) return 2; |
254 | 0 | return 3; |
255 | 0 | } |
256 | 0 | const int stats = |
257 | 0 | get_nz_mag(levels + get_padded_idx(coeff_idx, bhl), bhl, tx_class); |
258 | 0 | return get_nz_map_ctx_from_stats(stats, coeff_idx, bhl, tx_size, tx_class); |
259 | 0 | } |
260 | | |
261 | | void av1_txb_init_levels_c(const tran_low_t *const coeff, const int width, |
262 | 0 | const int height, uint8_t *const levels) { |
263 | 0 | const int stride = height + TX_PAD_HOR; |
264 | 0 | uint8_t *ls = levels; |
265 | |
|
266 | 0 | memset(levels + stride * width, 0, |
267 | 0 | sizeof(*levels) * (TX_PAD_BOTTOM * stride + TX_PAD_END)); |
268 | |
|
269 | 0 | for (int i = 0; i < width; i++) { |
270 | 0 | for (int j = 0; j < height; j++) { |
271 | 0 | *ls++ = (uint8_t)clamp(abs(coeff[i * height + j]), 0, INT8_MAX); |
272 | 0 | } |
273 | 0 | for (int j = 0; j < TX_PAD_HOR; j++) { |
274 | 0 | *ls++ = 0; |
275 | 0 | } |
276 | 0 | } |
277 | 0 | } |
278 | | |
279 | | void av1_get_nz_map_contexts_c(const uint8_t *const levels, |
280 | | const int16_t *const scan, const uint16_t eob, |
281 | | const TX_SIZE tx_size, const TX_CLASS tx_class, |
282 | 0 | int8_t *const coeff_contexts) { |
283 | 0 | const int bhl = get_txb_bhl(tx_size); |
284 | 0 | const int width = get_txb_wide(tx_size); |
285 | 0 | for (int i = 0; i < eob; ++i) { |
286 | 0 | const int pos = scan[i]; |
287 | 0 | coeff_contexts[pos] = get_nz_map_ctx(levels, pos, bhl, width, i, |
288 | 0 | i == eob - 1, tx_size, tx_class); |
289 | 0 | } |
290 | 0 | } |
291 | | |
292 | | void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCK *const x, |
293 | | aom_writer *w, int blk_row, int blk_col, int plane, |
294 | 14.6M | int block, TX_SIZE tx_size) { |
295 | 14.6M | MACROBLOCKD *xd = &x->e_mbd; |
296 | 14.6M | const CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff; |
297 | 14.6M | const PLANE_TYPE plane_type = get_plane_type(plane); |
298 | 14.6M | const int txb_offset = x->mbmi_ext_frame->cb_offset[plane_type] / |
299 | 14.6M | (TX_SIZE_W_MIN * TX_SIZE_H_MIN); |
300 | 14.6M | const uint16_t *eob_txb = cb_coef_buff->eobs[plane] + txb_offset; |
301 | 14.6M | const uint16_t eob = eob_txb[block]; |
302 | 14.6M | const uint8_t *entropy_ctx = cb_coef_buff->entropy_ctx[plane] + txb_offset; |
303 | 14.6M | const int txb_skip_ctx = entropy_ctx[block] & TXB_SKIP_CTX_MASK; |
304 | 14.6M | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
305 | 14.6M | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
306 | 14.6M | aom_write_symbol(w, eob == 0, ec_ctx->txb_skip_cdf[txs_ctx][txb_skip_ctx], 2); |
307 | 14.6M | if (eob == 0) return; |
308 | | |
309 | 14.1M | const TX_TYPE tx_type = |
310 | 14.1M | av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size, |
311 | 14.1M | cm->features.reduced_tx_set_used); |
312 | | // Only y plane's tx_type is transmitted |
313 | 14.1M | if (plane == 0) { |
314 | 9.43M | av1_write_tx_type(cm, xd, tx_type, tx_size, w); |
315 | 9.43M | } |
316 | | |
317 | 14.1M | int eob_extra; |
318 | 14.1M | const int eob_pt = av1_get_eob_pos_token(eob, &eob_extra); |
319 | 14.1M | const int eob_multi_size = txsize_log2_minus4[tx_size]; |
320 | 14.1M | const TX_CLASS tx_class = tx_type_to_class[tx_type]; |
321 | 14.1M | const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1; |
322 | 14.1M | switch (eob_multi_size) { |
323 | 7.56M | case 0: |
324 | 7.56M | aom_write_symbol(w, eob_pt - 1, |
325 | 7.56M | ec_ctx->eob_flag_cdf16[plane_type][eob_multi_ctx], 5); |
326 | 7.56M | break; |
327 | 970k | case 1: |
328 | 970k | aom_write_symbol(w, eob_pt - 1, |
329 | 970k | ec_ctx->eob_flag_cdf32[plane_type][eob_multi_ctx], 6); |
330 | 970k | break; |
331 | 4.64M | case 2: |
332 | 4.64M | aom_write_symbol(w, eob_pt - 1, |
333 | 4.64M | ec_ctx->eob_flag_cdf64[plane_type][eob_multi_ctx], 7); |
334 | 4.64M | break; |
335 | 235k | case 3: |
336 | 235k | aom_write_symbol(w, eob_pt - 1, |
337 | 235k | ec_ctx->eob_flag_cdf128[plane_type][eob_multi_ctx], 8); |
338 | 235k | break; |
339 | 481k | case 4: |
340 | 481k | aom_write_symbol(w, eob_pt - 1, |
341 | 481k | ec_ctx->eob_flag_cdf256[plane_type][eob_multi_ctx], 9); |
342 | 481k | break; |
343 | 65.8k | case 5: |
344 | 65.8k | aom_write_symbol(w, eob_pt - 1, |
345 | 65.8k | ec_ctx->eob_flag_cdf512[plane_type][eob_multi_ctx], 10); |
346 | 65.8k | break; |
347 | 132k | default: |
348 | 132k | aom_write_symbol(w, eob_pt - 1, |
349 | 132k | ec_ctx->eob_flag_cdf1024[plane_type][eob_multi_ctx], 11); |
350 | 132k | break; |
351 | 14.1M | } |
352 | | |
353 | 14.1M | const int eob_offset_bits = av1_eob_offset_bits[eob_pt]; |
354 | 14.1M | if (eob_offset_bits > 0) { |
355 | 13.8M | const int eob_ctx = eob_pt - 3; |
356 | 13.8M | int eob_shift = eob_offset_bits - 1; |
357 | 13.8M | int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0; |
358 | 13.8M | aom_write_symbol(w, bit, |
359 | 13.8M | ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_ctx], 2); |
360 | 52.7M | for (int i = 1; i < eob_offset_bits; i++) { |
361 | 38.9M | eob_shift = eob_offset_bits - 1 - i; |
362 | 38.9M | bit = (eob_extra & (1 << eob_shift)) ? 1 : 0; |
363 | 38.9M | aom_write_bit(w, bit); |
364 | 38.9M | } |
365 | 13.8M | } |
366 | | |
367 | 14.1M | const int width = get_txb_wide(tx_size); |
368 | 14.1M | const int height = get_txb_high(tx_size); |
369 | 14.1M | uint8_t levels_buf[TX_PAD_2D]; |
370 | 14.1M | uint8_t *const levels = set_levels(levels_buf, height); |
371 | 14.1M | const tran_low_t *tcoeff_txb = |
372 | 14.1M | cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane_type]; |
373 | 14.1M | const tran_low_t *tcoeff = tcoeff_txb + BLOCK_OFFSET(block); |
374 | 14.1M | av1_txb_init_levels(tcoeff, width, height, levels); |
375 | 14.1M | const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
376 | 14.1M | const int16_t *const scan = scan_order->scan; |
377 | 14.1M | DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]); |
378 | 14.1M | av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts); |
379 | | |
380 | 14.1M | const int bhl = get_txb_bhl(tx_size); |
381 | 659M | for (int c = eob - 1; c >= 0; --c) { |
382 | 645M | const int pos = scan[c]; |
383 | 645M | const int coeff_ctx = coeff_contexts[pos]; |
384 | 645M | const tran_low_t v = tcoeff[pos]; |
385 | 645M | const tran_low_t level = abs(v); |
386 | | |
387 | 645M | if (c == eob - 1) { |
388 | 14.0M | aom_write_symbol( |
389 | 14.0M | w, AOMMIN(level, 3) - 1, |
390 | 14.0M | ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx], 3); |
391 | 631M | } else { |
392 | 631M | aom_write_symbol(w, AOMMIN(level, 3), |
393 | 631M | ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx], |
394 | 631M | 4); |
395 | 631M | } |
396 | 645M | if (level > NUM_BASE_LEVELS) { |
397 | | // level is above 1. |
398 | 300M | const int base_range = level - 1 - NUM_BASE_LEVELS; |
399 | 300M | const int br_ctx = get_br_ctx(levels, pos, bhl, tx_class); |
400 | 300M | aom_cdf_prob *cdf = |
401 | 300M | ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx]; |
402 | 845M | for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
403 | 742M | const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1); |
404 | 742M | aom_write_symbol(w, k, cdf, BR_CDF_SIZE); |
405 | 742M | if (k < BR_CDF_SIZE - 1) break; |
406 | 742M | } |
407 | 300M | } |
408 | 645M | } |
409 | | |
410 | | // Loop to code all signs in the transform block, |
411 | | // starting with the sign of DC (if applicable) |
412 | 663M | for (int c = 0; c < eob; ++c) { |
413 | 648M | const tran_low_t v = tcoeff[scan[c]]; |
414 | 648M | const tran_low_t level = abs(v); |
415 | 648M | const int sign = (v < 0) ? 1 : 0; |
416 | 648M | if (level) { |
417 | 471M | if (c == 0) { |
418 | 13.2M | const int dc_sign_ctx = |
419 | 13.2M | (entropy_ctx[block] >> DC_SIGN_CTX_SHIFT) & DC_SIGN_CTX_MASK; |
420 | 13.2M | aom_write_symbol(w, sign, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], |
421 | 13.2M | 2); |
422 | 458M | } else { |
423 | 458M | aom_write_bit(w, sign); |
424 | 458M | } |
425 | 471M | if (level > COEFF_BASE_RANGE + NUM_BASE_LEVELS) |
426 | 103M | write_golomb(w, level - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS); |
427 | 471M | } |
428 | 648M | } |
429 | 14.1M | } |
430 | | |
431 | | void av1_write_intra_coeffs_mb(const AV1_COMMON *const cm, MACROBLOCK *x, |
432 | 5.85M | aom_writer *w, BLOCK_SIZE bsize) { |
433 | 5.85M | MACROBLOCKD *xd = &x->e_mbd; |
434 | 5.85M | const int num_planes = av1_num_planes(cm); |
435 | 5.85M | int block[MAX_MB_PLANE] = { 0 }; |
436 | 5.85M | int row, col; |
437 | 5.85M | assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x, |
438 | 5.85M | xd->plane[0].subsampling_y)); |
439 | 5.85M | const int max_blocks_wide = max_block_wide(xd, bsize, 0); |
440 | 5.85M | const int max_blocks_high = max_block_high(xd, bsize, 0); |
441 | 5.85M | const BLOCK_SIZE max_unit_bsize = BLOCK_64X64; |
442 | 5.85M | int mu_blocks_wide = mi_size_wide[max_unit_bsize]; |
443 | 5.85M | int mu_blocks_high = mi_size_high[max_unit_bsize]; |
444 | 5.85M | mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide); |
445 | 5.85M | mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high); |
446 | | |
447 | 11.6M | for (row = 0; row < max_blocks_high; row += mu_blocks_high) { |
448 | 11.6M | for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) { |
449 | 15.7M | for (int plane = 0; plane < num_planes; ++plane) { |
450 | 10.0M | if (plane && !xd->is_chroma_ref) break; |
451 | 9.88M | const TX_SIZE tx_size = av1_get_tx_size(plane, xd); |
452 | 9.88M | const int stepr = tx_size_high_unit[tx_size]; |
453 | 9.88M | const int stepc = tx_size_wide_unit[tx_size]; |
454 | 9.88M | const int step = stepr * stepc; |
455 | 9.88M | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
456 | 9.88M | const int unit_height = ROUND_POWER_OF_TWO( |
457 | 9.88M | AOMMIN(mu_blocks_high + row, max_blocks_high), pd->subsampling_y); |
458 | 9.88M | const int unit_width = ROUND_POWER_OF_TWO( |
459 | 9.88M | AOMMIN(mu_blocks_wide + col, max_blocks_wide), pd->subsampling_x); |
460 | 21.0M | for (int blk_row = row >> pd->subsampling_y; blk_row < unit_height; |
461 | 11.1M | blk_row += stepr) { |
462 | 24.9M | for (int blk_col = col >> pd->subsampling_x; blk_col < unit_width; |
463 | 13.7M | blk_col += stepc) { |
464 | 13.7M | av1_write_coeffs_txb(cm, x, w, blk_row, blk_col, plane, |
465 | 13.7M | block[plane], tx_size); |
466 | 13.7M | block[plane] += step; |
467 | 13.7M | } |
468 | 11.1M | } |
469 | 9.88M | } |
470 | 5.84M | } |
471 | 5.84M | } |
472 | 5.85M | } |
473 | | |
474 | | uint8_t av1_get_txb_entropy_context(const tran_low_t *qcoeff, |
475 | 278M | const SCAN_ORDER *scan_order, int eob) { |
476 | 278M | const int16_t *const scan = scan_order->scan; |
477 | 278M | int cul_level = 0; |
478 | 278M | int c; |
479 | | |
480 | 278M | if (eob == 0) return 0; |
481 | 1.18G | for (c = 0; c < eob; ++c) { |
482 | 1.16G | cul_level += abs(qcoeff[scan[c]]); |
483 | 1.16G | if (cul_level > COEFF_CONTEXT_MASK) break; |
484 | 1.16G | } |
485 | | |
486 | 272M | cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level); |
487 | 272M | set_dc_sign(&cul_level, qcoeff[0]); |
488 | | |
489 | 272M | return (uint8_t)cul_level; |
490 | 278M | } |
491 | | |
492 | | static void update_tx_type_count(const AV1_COMP *cpi, const AV1_COMMON *cm, |
493 | | MACROBLOCKD *xd, int blk_row, int blk_col, |
494 | | int plane, TX_SIZE tx_size, |
495 | | FRAME_COUNTS *counts, |
496 | 12.4M | uint8_t allow_update_cdf) { |
497 | 12.4M | MB_MODE_INFO *mbmi = xd->mi[0]; |
498 | 12.4M | int is_inter = is_inter_block(mbmi); |
499 | 12.4M | const int reduced_tx_set_used = cm->features.reduced_tx_set_used; |
500 | 12.4M | FRAME_CONTEXT *fc = xd->tile_ctx; |
501 | 12.4M | #if !CONFIG_ENTROPY_STATS |
502 | 12.4M | (void)counts; |
503 | 12.4M | #endif // !CONFIG_ENTROPY_STATS |
504 | | |
505 | | // Only y plane's tx_type is updated |
506 | 12.4M | if (plane > 0) return; |
507 | 8.31M | const TX_TYPE tx_type = av1_get_tx_type(xd, PLANE_TYPE_Y, blk_row, blk_col, |
508 | 8.31M | tx_size, reduced_tx_set_used); |
509 | 8.31M | if (is_inter) { |
510 | 602k | if (cpi->oxcf.txfm_cfg.use_inter_dct_only) { |
511 | 0 | assert(tx_type == DCT_DCT); |
512 | 0 | } |
513 | 7.71M | } else { |
514 | 7.71M | if (cpi->oxcf.txfm_cfg.use_intra_dct_only) { |
515 | 0 | assert(tx_type == DCT_DCT); |
516 | 7.71M | } else if (cpi->oxcf.txfm_cfg.use_intra_default_tx_only) { |
517 | 0 | const TX_TYPE default_type = get_default_tx_type( |
518 | 0 | PLANE_TYPE_Y, xd, tx_size, cpi->use_screen_content_tools); |
519 | 0 | (void)default_type; |
520 | | // TODO(kyslov): We don't always respect use_intra_default_tx_only flag in |
521 | | // NonRD and REALTIME case. Specifically we ignore it in hybrid inta mode |
522 | | // search, when picking up intra mode in nonRD inter mode search and in RD |
523 | | // REALTIME mode when we limit TX type usage. |
524 | | // We need to fix txfm cfg for these cases. Meanwhile relieving the |
525 | | // assert. |
526 | 0 | assert(tx_type == default_type || cpi->sf.rt_sf.use_nonrd_pick_mode || |
527 | 0 | cpi->oxcf.mode == REALTIME); |
528 | 0 | } |
529 | 7.71M | } |
530 | | |
531 | 8.31M | if (get_ext_tx_types(tx_size, is_inter, reduced_tx_set_used) > 1 && |
532 | 8.20M | cm->quant_params.base_qindex > 0 && !mbmi->skip_txfm && |
533 | 6.39M | !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) { |
534 | 6.39M | const int eset = get_ext_tx_set(tx_size, is_inter, reduced_tx_set_used); |
535 | 6.39M | if (eset > 0) { |
536 | 6.39M | const TxSetType tx_set_type = |
537 | 6.39M | av1_get_ext_tx_set_type(tx_size, is_inter, reduced_tx_set_used); |
538 | 6.39M | if (is_inter) { |
539 | 527k | if (allow_update_cdf) { |
540 | 527k | update_cdf(fc->inter_ext_tx_cdf[eset][txsize_sqr_map[tx_size]], |
541 | 527k | av1_ext_tx_ind[tx_set_type][tx_type], |
542 | 527k | av1_num_ext_tx_set[tx_set_type]); |
543 | 527k | } |
544 | | #if CONFIG_ENTROPY_STATS |
545 | | ++counts->inter_ext_tx[eset][txsize_sqr_map[tx_size]] |
546 | | [av1_ext_tx_ind[tx_set_type][tx_type]]; |
547 | | #endif // CONFIG_ENTROPY_STATS |
548 | 5.86M | } else { |
549 | 5.86M | PREDICTION_MODE intra_dir; |
550 | 5.86M | if (mbmi->filter_intra_mode_info.use_filter_intra) |
551 | 579k | intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info |
552 | 579k | .filter_intra_mode]; |
553 | 5.28M | else |
554 | 5.28M | intra_dir = mbmi->mode; |
555 | | #if CONFIG_ENTROPY_STATS |
556 | | ++counts->intra_ext_tx[eset][txsize_sqr_map[tx_size]][intra_dir] |
557 | | [av1_ext_tx_ind[tx_set_type][tx_type]]; |
558 | | #endif // CONFIG_ENTROPY_STATS |
559 | 5.86M | if (allow_update_cdf) { |
560 | 5.86M | update_cdf( |
561 | 5.86M | fc->intra_ext_tx_cdf[eset][txsize_sqr_map[tx_size]][intra_dir], |
562 | 5.86M | av1_ext_tx_ind[tx_set_type][tx_type], |
563 | 5.86M | av1_num_ext_tx_set[tx_set_type]); |
564 | 5.86M | } |
565 | 5.86M | } |
566 | 6.39M | } |
567 | 6.39M | } |
568 | 8.31M | } |
569 | | |
570 | | void av1_update_and_record_txb_context(int plane, int block, int blk_row, |
571 | | int blk_col, BLOCK_SIZE plane_bsize, |
572 | 42.2M | TX_SIZE tx_size, void *arg) { |
573 | 42.2M | struct tokenize_b_args *const args = arg; |
574 | 42.2M | const AV1_COMP *cpi = args->cpi; |
575 | 42.2M | const AV1_COMMON *cm = &cpi->common; |
576 | 42.2M | ThreadData *const td = args->td; |
577 | 42.2M | MACROBLOCK *const x = &td->mb; |
578 | 42.2M | MACROBLOCKD *const xd = &x->e_mbd; |
579 | 42.2M | struct macroblock_plane *p = &x->plane[plane]; |
580 | 42.2M | struct macroblockd_plane *pd = &xd->plane[plane]; |
581 | 42.2M | const int eob = p->eobs[block]; |
582 | 42.2M | const int block_offset = BLOCK_OFFSET(block); |
583 | 42.2M | tran_low_t *qcoeff = p->qcoeff + block_offset; |
584 | 42.2M | const PLANE_TYPE plane_type = pd->plane_type; |
585 | 42.2M | const TX_TYPE tx_type = |
586 | 42.2M | av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size, |
587 | 42.2M | cm->features.reduced_tx_set_used); |
588 | 42.2M | const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
589 | 42.2M | tran_low_t *tcoeff; |
590 | 42.2M | assert(args->dry_run != DRY_RUN_COSTCOEFFS); |
591 | 42.2M | if (args->dry_run == OUTPUT_ENABLED) { |
592 | 12.9M | MB_MODE_INFO *mbmi = xd->mi[0]; |
593 | 12.9M | TXB_CTX txb_ctx; |
594 | 12.9M | get_txb_ctx(plane_bsize, tx_size, plane, |
595 | 12.9M | pd->above_entropy_context + blk_col, |
596 | 12.9M | pd->left_entropy_context + blk_row, &txb_ctx); |
597 | 12.9M | const int bhl = get_txb_bhl(tx_size); |
598 | 12.9M | const int width = get_txb_wide(tx_size); |
599 | 12.9M | const int height = get_txb_high(tx_size); |
600 | 12.9M | const uint8_t allow_update_cdf = args->allow_update_cdf; |
601 | 12.9M | const TX_SIZE txsize_ctx = get_txsize_entropy_ctx(tx_size); |
602 | 12.9M | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
603 | | #if CONFIG_ENTROPY_STATS |
604 | | int cdf_idx = cm->coef_cdf_category; |
605 | | ++td->counts->txb_skip[cdf_idx][txsize_ctx][txb_ctx.txb_skip_ctx][eob == 0]; |
606 | | #endif // CONFIG_ENTROPY_STATS |
607 | 12.9M | if (allow_update_cdf) { |
608 | 12.9M | update_cdf(ec_ctx->txb_skip_cdf[txsize_ctx][txb_ctx.txb_skip_ctx], |
609 | 12.9M | eob == 0, 2); |
610 | 12.9M | } |
611 | | |
612 | 12.9M | CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff; |
613 | 12.9M | const int txb_offset = x->mbmi_ext_frame->cb_offset[plane_type] / |
614 | 12.9M | (TX_SIZE_W_MIN * TX_SIZE_H_MIN); |
615 | 12.9M | uint16_t *eob_txb = cb_coef_buff->eobs[plane] + txb_offset; |
616 | 12.9M | uint8_t *const entropy_ctx = cb_coef_buff->entropy_ctx[plane] + txb_offset; |
617 | 12.9M | entropy_ctx[block] = txb_ctx.txb_skip_ctx; |
618 | 12.9M | eob_txb[block] = eob; |
619 | | |
620 | 12.9M | if (eob == 0) { |
621 | 541k | av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, 0, blk_col, |
622 | 541k | blk_row); |
623 | 541k | return; |
624 | 541k | } |
625 | 12.4M | const int segment_id = mbmi->segment_id; |
626 | 12.4M | const int seg_eob = av1_get_tx_eob(&cpi->common.seg, segment_id, tx_size); |
627 | 12.4M | tran_low_t *tcoeff_txb = |
628 | 12.4M | cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane_type]; |
629 | 12.4M | tcoeff = tcoeff_txb + block_offset; |
630 | 12.4M | memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob); |
631 | | |
632 | 12.4M | uint8_t levels_buf[TX_PAD_2D]; |
633 | 12.4M | uint8_t *const levels = set_levels(levels_buf, height); |
634 | 12.4M | av1_txb_init_levels(tcoeff, width, height, levels); |
635 | 12.4M | update_tx_type_count(cpi, cm, xd, blk_row, blk_col, plane, tx_size, |
636 | 12.4M | td->counts, allow_update_cdf); |
637 | | |
638 | 12.4M | const TX_CLASS tx_class = tx_type_to_class[tx_type]; |
639 | 12.4M | const int16_t *const scan = scan_order->scan; |
640 | | |
641 | | // record tx type usage |
642 | 12.4M | td->rd_counts.tx_type_used[tx_size][tx_type]++; |
643 | | |
644 | | #if CONFIG_ENTROPY_STATS |
645 | | update_eob_context(cdf_idx, eob, tx_size, tx_class, plane_type, ec_ctx, |
646 | | td->counts, allow_update_cdf); |
647 | | #else |
648 | 12.4M | update_eob_context(eob, tx_size, tx_class, plane_type, ec_ctx, |
649 | 12.4M | allow_update_cdf); |
650 | 12.4M | #endif |
651 | | |
652 | 12.4M | DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]); |
653 | 12.4M | av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, |
654 | 12.4M | coeff_contexts); |
655 | | |
656 | 610M | for (int c = eob - 1; c >= 0; --c) { |
657 | 598M | const int pos = scan[c]; |
658 | 598M | const int coeff_ctx = coeff_contexts[pos]; |
659 | 598M | const tran_low_t v = qcoeff[pos]; |
660 | 598M | const tran_low_t level = abs(v); |
661 | | /* abs_sum_level is needed to decide the job scheduling order of |
662 | | * pack bitstream multi-threading. This data is not needed if |
663 | | * multi-threading is disabled. */ |
664 | 598M | if (cpi->mt_info.pack_bs_mt_enabled) td->abs_sum_level += level; |
665 | | |
666 | 598M | if (allow_update_cdf) { |
667 | 598M | if (c == eob - 1) { |
668 | 12.4M | assert(coeff_ctx < 4); |
669 | 12.4M | update_cdf( |
670 | 12.4M | ec_ctx->coeff_base_eob_cdf[txsize_ctx][plane_type][coeff_ctx], |
671 | 12.4M | AOMMIN(level, 3) - 1, 3); |
672 | 586M | } else { |
673 | 586M | update_cdf(ec_ctx->coeff_base_cdf[txsize_ctx][plane_type][coeff_ctx], |
674 | 586M | AOMMIN(level, 3), 4); |
675 | 586M | } |
676 | 598M | } |
677 | 598M | if (c == eob - 1) { |
678 | 12.4M | assert(coeff_ctx < 4); |
679 | | #if CONFIG_ENTROPY_STATS |
680 | | ++td->counts->coeff_base_eob_multi[cdf_idx][txsize_ctx][plane_type] |
681 | | [coeff_ctx][AOMMIN(level, 3) - 1]; |
682 | | } else { |
683 | | ++td->counts->coeff_base_multi[cdf_idx][txsize_ctx][plane_type] |
684 | | [coeff_ctx][AOMMIN(level, 3)]; |
685 | | #endif |
686 | 12.4M | } |
687 | 598M | if (level > NUM_BASE_LEVELS) { |
688 | 269M | const int base_range = level - 1 - NUM_BASE_LEVELS; |
689 | 269M | const int br_ctx = get_br_ctx(levels, pos, bhl, tx_class); |
690 | 761M | for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
691 | 670M | const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1); |
692 | 673M | if (allow_update_cdf) { |
693 | 673M | update_cdf(ec_ctx->coeff_br_cdf[AOMMIN(txsize_ctx, TX_32X32)] |
694 | 673M | [plane_type][br_ctx], |
695 | 673M | k, BR_CDF_SIZE); |
696 | 673M | } |
697 | 2.30G | for (int lps = 0; lps < BR_CDF_SIZE - 1; lps++) { |
698 | | #if CONFIG_ENTROPY_STATS |
699 | | ++td->counts->coeff_lps[AOMMIN(txsize_ctx, TX_32X32)][plane_type] |
700 | | [lps][br_ctx][lps == k]; |
701 | | #endif // CONFIG_ENTROPY_STATS |
702 | 1.81G | if (lps == k) break; |
703 | 1.81G | } |
704 | | #if CONFIG_ENTROPY_STATS |
705 | | ++td->counts->coeff_lps_multi[cdf_idx][AOMMIN(txsize_ctx, TX_32X32)] |
706 | | [plane_type][br_ctx][k]; |
707 | | #endif |
708 | 670M | if (k < BR_CDF_SIZE - 1) break; |
709 | 670M | } |
710 | 269M | } |
711 | 598M | } |
712 | | // Update the context needed to code the DC sign (if applicable) |
713 | 12.4M | if (tcoeff[0] != 0) { |
714 | 11.6M | const int dc_sign = (tcoeff[0] < 0) ? 1 : 0; |
715 | 11.6M | const int dc_sign_ctx = txb_ctx.dc_sign_ctx; |
716 | | #if CONFIG_ENTROPY_STATS |
717 | | ++td->counts->dc_sign[plane_type][dc_sign_ctx][dc_sign]; |
718 | | #endif // CONFIG_ENTROPY_STATS |
719 | 11.6M | if (allow_update_cdf) |
720 | 11.6M | update_cdf(ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], dc_sign, 2); |
721 | 11.6M | entropy_ctx[block] |= dc_sign_ctx << DC_SIGN_CTX_SHIFT; |
722 | 11.6M | } |
723 | 29.2M | } else { |
724 | 29.2M | tcoeff = qcoeff; |
725 | 29.2M | } |
726 | 41.6M | const uint8_t cul_level = |
727 | 41.6M | av1_get_txb_entropy_context(tcoeff, scan_order, eob); |
728 | 41.6M | av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level, |
729 | 41.6M | blk_col, blk_row); |
730 | 41.6M | } |
731 | | |
732 | | void av1_record_txb_context(int plane, int block, int blk_row, int blk_col, |
733 | | BLOCK_SIZE plane_bsize, TX_SIZE tx_size, |
734 | 1.67M | void *arg) { |
735 | 1.67M | struct tokenize_b_args *const args = arg; |
736 | 1.67M | const AV1_COMP *cpi = args->cpi; |
737 | 1.67M | const AV1_COMMON *cm = &cpi->common; |
738 | 1.67M | ThreadData *const td = args->td; |
739 | 1.67M | MACROBLOCK *const x = &td->mb; |
740 | 1.67M | MACROBLOCKD *const xd = &x->e_mbd; |
741 | 1.67M | struct macroblock_plane *p = &x->plane[plane]; |
742 | 1.67M | struct macroblockd_plane *pd = &xd->plane[plane]; |
743 | 1.67M | const int eob = p->eobs[block]; |
744 | 1.67M | const int block_offset = BLOCK_OFFSET(block); |
745 | 1.67M | tran_low_t *qcoeff = p->qcoeff + block_offset; |
746 | 1.67M | const PLANE_TYPE plane_type = pd->plane_type; |
747 | 1.67M | const TX_TYPE tx_type = |
748 | 1.67M | av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size, |
749 | 1.67M | cm->features.reduced_tx_set_used); |
750 | 1.67M | const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
751 | 1.67M | tran_low_t *tcoeff; |
752 | 1.67M | assert(args->dry_run != DRY_RUN_COSTCOEFFS); |
753 | 1.67M | if (args->dry_run == OUTPUT_ENABLED) { |
754 | 1.67M | MB_MODE_INFO *mbmi = xd->mi[0]; |
755 | 1.67M | TXB_CTX txb_ctx; |
756 | 1.67M | get_txb_ctx(plane_bsize, tx_size, plane, |
757 | 1.67M | pd->above_entropy_context + blk_col, |
758 | 1.67M | pd->left_entropy_context + blk_row, &txb_ctx); |
759 | | #if CONFIG_ENTROPY_STATS |
760 | | const TX_SIZE txsize_ctx = get_txsize_entropy_ctx(tx_size); |
761 | | const int bhl = get_txb_bhl(tx_size); |
762 | | const int width = get_txb_wide(tx_size); |
763 | | const int height = get_txb_high(tx_size); |
764 | | int cdf_idx = cm->coef_cdf_category; |
765 | | ++td->counts->txb_skip[cdf_idx][txsize_ctx][txb_ctx.txb_skip_ctx][eob == 0]; |
766 | | #endif // CONFIG_ENTROPY_STATS |
767 | | |
768 | 1.67M | CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff; |
769 | 1.67M | const int txb_offset = x->mbmi_ext_frame->cb_offset[plane_type] / |
770 | 1.67M | (TX_SIZE_W_MIN * TX_SIZE_H_MIN); |
771 | 1.67M | uint16_t *eob_txb = cb_coef_buff->eobs[plane] + txb_offset; |
772 | 1.67M | uint8_t *const entropy_ctx = cb_coef_buff->entropy_ctx[plane] + txb_offset; |
773 | 1.67M | entropy_ctx[block] = txb_ctx.txb_skip_ctx; |
774 | 1.67M | eob_txb[block] = eob; |
775 | | |
776 | 1.67M | if (eob == 0) { |
777 | 17.8k | av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, 0, blk_col, |
778 | 17.8k | blk_row); |
779 | 17.8k | return; |
780 | 17.8k | } |
781 | 1.65M | const int segment_id = mbmi->segment_id; |
782 | 1.65M | const int seg_eob = av1_get_tx_eob(&cpi->common.seg, segment_id, tx_size); |
783 | 1.65M | tran_low_t *tcoeff_txb = |
784 | 1.65M | cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane_type]; |
785 | 1.65M | tcoeff = tcoeff_txb + block_offset; |
786 | 1.65M | memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob); |
787 | | |
788 | | #if CONFIG_ENTROPY_STATS |
789 | | uint8_t levels_buf[TX_PAD_2D]; |
790 | | uint8_t *const levels = set_levels(levels_buf, height); |
791 | | av1_txb_init_levels(tcoeff, width, height, levels); |
792 | | update_tx_type_count(cpi, cm, xd, blk_row, blk_col, plane, tx_size, |
793 | | td->counts, 0 /*allow_update_cdf*/); |
794 | | |
795 | | const TX_CLASS tx_class = tx_type_to_class[tx_type]; |
796 | | const bool do_coeff_scan = true; |
797 | | #else |
798 | 1.65M | const bool do_coeff_scan = cpi->mt_info.pack_bs_mt_enabled; |
799 | 1.65M | #endif |
800 | 1.65M | const int16_t *const scan = scan_order->scan; |
801 | | |
802 | | // record tx type usage |
803 | 1.65M | td->rd_counts.tx_type_used[tx_size][tx_type]++; |
804 | | |
805 | | #if CONFIG_ENTROPY_STATS |
806 | | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
807 | | update_eob_context(cdf_idx, eob, tx_size, tx_class, plane_type, ec_ctx, |
808 | | td->counts, 0 /*allow_update_cdf*/); |
809 | | |
810 | | DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]); |
811 | | av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, |
812 | | coeff_contexts); |
813 | | #endif |
814 | | |
815 | 64.5M | for (int c = eob - 1; (c >= 0) && do_coeff_scan; --c) { |
816 | 62.8M | const int pos = scan[c]; |
817 | 62.8M | const tran_low_t v = qcoeff[pos]; |
818 | 62.8M | const tran_low_t level = abs(v); |
819 | | /* abs_sum_level is needed to decide the job scheduling order of |
820 | | * pack bitstream multi-threading. This data is not needed if |
821 | | * multi-threading is disabled. */ |
822 | 62.8M | if (cpi->mt_info.pack_bs_mt_enabled) td->abs_sum_level += level; |
823 | | |
824 | | #if CONFIG_ENTROPY_STATS |
825 | | const int coeff_ctx = coeff_contexts[pos]; |
826 | | if (c == eob - 1) { |
827 | | assert(coeff_ctx < 4); |
828 | | ++td->counts->coeff_base_eob_multi[cdf_idx][txsize_ctx][plane_type] |
829 | | [coeff_ctx][AOMMIN(level, 3) - 1]; |
830 | | } else { |
831 | | ++td->counts->coeff_base_multi[cdf_idx][txsize_ctx][plane_type] |
832 | | [coeff_ctx][AOMMIN(level, 3)]; |
833 | | } |
834 | | if (level > NUM_BASE_LEVELS) { |
835 | | const int base_range = level - 1 - NUM_BASE_LEVELS; |
836 | | const int br_ctx = get_br_ctx(levels, pos, bhl, tx_class); |
837 | | for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
838 | | const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1); |
839 | | for (int lps = 0; lps < BR_CDF_SIZE - 1; lps++) { |
840 | | ++td->counts->coeff_lps[AOMMIN(txsize_ctx, TX_32X32)][plane_type] |
841 | | [lps][br_ctx][lps == k]; |
842 | | if (lps == k) break; |
843 | | } |
844 | | ++td->counts->coeff_lps_multi[cdf_idx][AOMMIN(txsize_ctx, TX_32X32)] |
845 | | [plane_type][br_ctx][k]; |
846 | | if (k < BR_CDF_SIZE - 1) break; |
847 | | } |
848 | | } |
849 | | #endif |
850 | 62.8M | } |
851 | | // Update the context needed to code the DC sign (if applicable) |
852 | 1.65M | if (tcoeff[0] != 0) { |
853 | 1.57M | const int dc_sign_ctx = txb_ctx.dc_sign_ctx; |
854 | | #if CONFIG_ENTROPY_STATS |
855 | | const int dc_sign = (tcoeff[0] < 0) ? 1 : 0; |
856 | | ++td->counts->dc_sign[plane_type][dc_sign_ctx][dc_sign]; |
857 | | #endif // CONFIG_ENTROPY_STATS |
858 | 1.57M | entropy_ctx[block] |= dc_sign_ctx << DC_SIGN_CTX_SHIFT; |
859 | 1.57M | } |
860 | 18.4E | } else { |
861 | 18.4E | tcoeff = qcoeff; |
862 | 18.4E | } |
863 | 1.65M | const uint8_t cul_level = |
864 | 1.65M | av1_get_txb_entropy_context(tcoeff, scan_order, eob); |
865 | 1.65M | av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level, |
866 | 1.65M | blk_col, blk_row); |
867 | 1.65M | } |
868 | | |
869 | | void av1_update_intra_mb_txb_context(const AV1_COMP *cpi, ThreadData *td, |
870 | | RUN_TYPE dry_run, BLOCK_SIZE bsize, |
871 | 18.4M | uint8_t allow_update_cdf) { |
872 | 18.4M | const AV1_COMMON *const cm = &cpi->common; |
873 | 18.4M | const int num_planes = av1_num_planes(cm); |
874 | 18.4M | MACROBLOCK *const x = &td->mb; |
875 | 18.4M | MACROBLOCKD *const xd = &x->e_mbd; |
876 | 18.4M | MB_MODE_INFO *const mbmi = xd->mi[0]; |
877 | 18.4M | struct tokenize_b_args arg = { cpi, td, 0, allow_update_cdf, dry_run }; |
878 | 18.4M | if (mbmi->skip_txfm) { |
879 | 0 | av1_reset_entropy_context(xd, bsize, num_planes); |
880 | 0 | return; |
881 | 0 | } |
882 | 18.4M | const foreach_transformed_block_visitor visit = |
883 | 18.4M | allow_update_cdf ? av1_update_and_record_txb_context |
884 | 18.4M | : av1_record_txb_context; |
885 | | |
886 | 49.2M | for (int plane = 0; plane < num_planes; ++plane) { |
887 | 32.0M | if (plane && !xd->is_chroma_ref) break; |
888 | 30.7M | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
889 | 30.7M | const int ss_x = pd->subsampling_x; |
890 | 30.7M | const int ss_y = pd->subsampling_y; |
891 | 30.7M | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y); |
892 | 30.7M | av1_foreach_transformed_block_in_plane(xd, plane_bsize, plane, visit, &arg); |
893 | 30.7M | } |
894 | 18.4M | } |
895 | | |
896 | | CB_COEFF_BUFFER *av1_get_cb_coeff_buffer(const struct AV1_COMP *cpi, int mi_row, |
897 | 689k | int mi_col) { |
898 | 689k | const AV1_COMMON *const cm = &cpi->common; |
899 | 689k | const int mib_size_log2 = cm->seq_params->mib_size_log2; |
900 | 689k | const int stride = |
901 | 689k | CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, cm->seq_params->mib_size_log2); |
902 | 689k | const int offset = |
903 | 689k | (mi_row >> mib_size_log2) * stride + (mi_col >> mib_size_log2); |
904 | 689k | return cpi->coeff_buffer_base + offset; |
905 | 689k | } |