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