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