/src/aom/av1/decoder/decodetxb.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/decoder/decodetxb.h" |
13 | | |
14 | | #include "aom_ports/mem.h" |
15 | | #include "av1/common/idct.h" |
16 | | #include "av1/common/scan.h" |
17 | | #include "av1/common/txb_common.h" |
18 | | #include "av1/decoder/decodemv.h" |
19 | | |
20 | | #define ACCT_STR __func__ |
21 | | |
22 | 669k | static int read_golomb(MACROBLOCKD *xd, aom_reader *r) { |
23 | 669k | int x = 1; |
24 | 669k | int length = 0; |
25 | 669k | int i = 0; |
26 | | |
27 | 2.06M | while (!i) { |
28 | 1.39M | i = aom_read_bit(r, ACCT_STR); |
29 | 1.39M | ++length; |
30 | 1.39M | if (length > 20) { |
31 | 299 | aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME, |
32 | 299 | "Invalid length in read_golomb"); |
33 | 299 | } |
34 | 1.39M | } |
35 | | |
36 | 1.39M | for (i = 0; i < length - 1; ++i) { |
37 | 728k | x <<= 1; |
38 | 728k | x += aom_read_bit(r, ACCT_STR); |
39 | 728k | } |
40 | | |
41 | 669k | return x - 1; |
42 | 669k | } |
43 | | |
44 | 16.9M | static inline int rec_eob_pos(const int eob_token, const int extra) { |
45 | 16.9M | int eob = av1_eob_group_start[eob_token]; |
46 | 16.9M | if (eob > 2) { |
47 | 8.74M | eob += extra; |
48 | 8.74M | } |
49 | 16.9M | return eob; |
50 | 16.9M | } |
51 | | |
52 | | static inline int get_dqv(const int16_t *dequant, int coeff_idx, |
53 | 83.7M | const qm_val_t *iqmatrix) { |
54 | 83.7M | int dqv = dequant[!!coeff_idx]; |
55 | 83.7M | if (iqmatrix != NULL) |
56 | 2.19M | dqv = |
57 | 2.19M | ((iqmatrix[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS; |
58 | 83.7M | return dqv; |
59 | 83.7M | } |
60 | | |
61 | | static inline void read_coeffs_reverse_2d(aom_reader *r, TX_SIZE tx_size, |
62 | | int start_si, int end_si, |
63 | | const int16_t *scan, int bhl, |
64 | | uint8_t *levels, |
65 | | base_cdf_arr base_cdf, |
66 | 8.29M | br_cdf_arr br_cdf) { |
67 | 192M | for (int c = end_si; c >= start_si; --c) { |
68 | 184M | const int pos = scan[c]; |
69 | 184M | const int coeff_ctx = get_lower_levels_ctx_2d(levels, pos, bhl, tx_size); |
70 | 184M | const int nsymbs = 4; |
71 | 184M | int level = aom_read_symbol(r, base_cdf[coeff_ctx], nsymbs, ACCT_STR); |
72 | 184M | if (level > NUM_BASE_LEVELS) { |
73 | 12.1M | const int br_ctx = get_br_ctx_2d(levels, pos, bhl); |
74 | 12.1M | aom_cdf_prob *cdf = br_cdf[br_ctx]; |
75 | 16.9M | for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
76 | 16.5M | const int k = aom_read_symbol(r, cdf, BR_CDF_SIZE, ACCT_STR); |
77 | 16.5M | level += k; |
78 | 16.5M | if (k < BR_CDF_SIZE - 1) break; |
79 | 16.5M | } |
80 | 12.1M | } |
81 | 184M | levels[get_padded_idx(pos, bhl)] = level; |
82 | 184M | } |
83 | 8.29M | } |
84 | | |
85 | | static inline void read_coeffs_reverse(aom_reader *r, TX_SIZE tx_size, |
86 | | TX_CLASS tx_class, int start_si, |
87 | | int end_si, const int16_t *scan, int bhl, |
88 | | uint8_t *levels, base_cdf_arr base_cdf, |
89 | 9.32M | br_cdf_arr br_cdf) { |
90 | 36.5M | for (int c = end_si; c >= start_si; --c) { |
91 | 27.2M | const int pos = scan[c]; |
92 | 27.2M | const int coeff_ctx = |
93 | 27.2M | get_lower_levels_ctx(levels, pos, bhl, tx_size, tx_class); |
94 | 27.2M | const int nsymbs = 4; |
95 | 27.2M | int level = aom_read_symbol(r, base_cdf[coeff_ctx], nsymbs, ACCT_STR); |
96 | 27.2M | if (level > NUM_BASE_LEVELS) { |
97 | 3.90M | const int br_ctx = get_br_ctx(levels, pos, bhl, tx_class); |
98 | 3.90M | aom_cdf_prob *cdf = br_cdf[br_ctx]; |
99 | 6.33M | for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
100 | 6.10M | const int k = aom_read_symbol(r, cdf, BR_CDF_SIZE, ACCT_STR); |
101 | 6.10M | level += k; |
102 | 6.10M | if (k < BR_CDF_SIZE - 1) break; |
103 | 6.10M | } |
104 | 3.90M | } |
105 | 27.2M | levels[get_padded_idx(pos, bhl)] = level; |
106 | 27.2M | } |
107 | 9.32M | } |
108 | | |
109 | | static uint8_t read_coeffs_txb(const AV1_COMMON *const cm, |
110 | | DecoderCodingBlock *dcb, aom_reader *const r, |
111 | | const int blk_row, const int blk_col, |
112 | | const int plane, const TXB_CTX *const txb_ctx, |
113 | 29.2M | const TX_SIZE tx_size) { |
114 | 29.2M | MACROBLOCKD *const xd = &dcb->xd; |
115 | 29.2M | FRAME_CONTEXT *const ec_ctx = xd->tile_ctx; |
116 | 29.2M | const int32_t max_value = (1 << (7 + xd->bd)) - 1; |
117 | 29.2M | const int32_t min_value = -(1 << (7 + xd->bd)); |
118 | 29.2M | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
119 | 29.2M | const PLANE_TYPE plane_type = get_plane_type(plane); |
120 | 29.2M | MB_MODE_INFO *const mbmi = xd->mi[0]; |
121 | 29.2M | struct macroblockd_plane *const pd = &xd->plane[plane]; |
122 | 29.2M | const int16_t *const dequant = pd->seg_dequant_QTX[mbmi->segment_id]; |
123 | 29.2M | tran_low_t *const tcoeffs = dcb->dqcoeff_block[plane] + dcb->cb_offset[plane]; |
124 | 29.2M | const int shift = av1_get_tx_scale(tx_size); |
125 | 29.2M | const int bhl = get_txb_bhl(tx_size); |
126 | 29.2M | const int width = get_txb_wide(tx_size); |
127 | 29.2M | const int height = get_txb_high(tx_size); |
128 | 29.2M | int cul_level = 0; |
129 | 29.2M | int dc_val = 0; |
130 | 29.2M | uint8_t levels_buf[TX_PAD_2D]; |
131 | 29.2M | uint8_t *const levels = set_levels(levels_buf, height); |
132 | 29.2M | const int all_zero = aom_read_symbol( |
133 | 29.2M | r, ec_ctx->txb_skip_cdf[txs_ctx][txb_ctx->txb_skip_ctx], 2, ACCT_STR); |
134 | 29.2M | eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane]; |
135 | 29.2M | uint16_t *const eob = &(eob_data->eob); |
136 | 29.2M | uint16_t *const max_scan_line = &(eob_data->max_scan_line); |
137 | 29.2M | *max_scan_line = 0; |
138 | 29.2M | *eob = 0; |
139 | | |
140 | | #if CONFIG_INSPECTION |
141 | | if (plane == 0) { |
142 | | const int txk_type_idx = |
143 | | av1_get_txk_type_index(mbmi->bsize, blk_row, blk_col); |
144 | | mbmi->tx_skip[txk_type_idx] = all_zero; |
145 | | } |
146 | | #endif |
147 | | |
148 | 29.2M | if (all_zero) { |
149 | 11.3M | *max_scan_line = 0; |
150 | 11.3M | if (plane == 0) { |
151 | 2.42M | xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col] = DCT_DCT; |
152 | 2.42M | } |
153 | 11.3M | return 0; |
154 | 11.3M | } |
155 | | |
156 | 17.9M | if (plane == AOM_PLANE_Y) { |
157 | | // only y plane's tx_type is transmitted |
158 | 9.99M | av1_read_tx_type(cm, xd, blk_row, blk_col, tx_size, r); |
159 | 9.99M | } |
160 | 17.9M | const TX_TYPE tx_type = |
161 | 17.9M | av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size, |
162 | 17.9M | cm->features.reduced_tx_set_used); |
163 | 17.9M | const TX_CLASS tx_class = tx_type_to_class[tx_type]; |
164 | 17.9M | const qm_val_t *iqmatrix = |
165 | 17.9M | av1_get_iqmatrix(&cm->quant_params, xd, plane, tx_size, tx_type); |
166 | 17.9M | const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
167 | 17.9M | const int16_t *const scan = scan_order->scan; |
168 | 17.9M | int eob_extra = 0; |
169 | 17.9M | int eob_pt = 1; |
170 | | |
171 | 17.9M | const int eob_multi_size = txsize_log2_minus4[tx_size]; |
172 | 17.9M | const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1; |
173 | 17.9M | switch (eob_multi_size) { |
174 | 5.18M | case 0: |
175 | 5.18M | eob_pt = |
176 | 5.18M | aom_read_symbol(r, ec_ctx->eob_flag_cdf16[plane_type][eob_multi_ctx], |
177 | 5.18M | 5, ACCT_STR) + |
178 | 5.18M | 1; |
179 | 5.18M | break; |
180 | 2.30M | case 1: |
181 | 2.30M | eob_pt = |
182 | 2.30M | aom_read_symbol(r, ec_ctx->eob_flag_cdf32[plane_type][eob_multi_ctx], |
183 | 2.30M | 6, ACCT_STR) + |
184 | 2.30M | 1; |
185 | 2.30M | break; |
186 | 4.02M | case 2: |
187 | 4.02M | eob_pt = |
188 | 4.02M | aom_read_symbol(r, ec_ctx->eob_flag_cdf64[plane_type][eob_multi_ctx], |
189 | 4.02M | 7, ACCT_STR) + |
190 | 4.02M | 1; |
191 | 4.02M | break; |
192 | 1.51M | case 3: |
193 | 1.51M | eob_pt = |
194 | 1.51M | aom_read_symbol(r, ec_ctx->eob_flag_cdf128[plane_type][eob_multi_ctx], |
195 | 1.51M | 8, ACCT_STR) + |
196 | 1.51M | 1; |
197 | 1.51M | break; |
198 | 2.59M | case 4: |
199 | 2.59M | eob_pt = |
200 | 2.59M | aom_read_symbol(r, ec_ctx->eob_flag_cdf256[plane_type][eob_multi_ctx], |
201 | 2.59M | 9, ACCT_STR) + |
202 | 2.59M | 1; |
203 | 2.59M | break; |
204 | 729k | case 5: |
205 | 729k | eob_pt = |
206 | 729k | aom_read_symbol(r, ec_ctx->eob_flag_cdf512[plane_type][eob_multi_ctx], |
207 | 729k | 10, ACCT_STR) + |
208 | 729k | 1; |
209 | 729k | break; |
210 | 958k | case 6: |
211 | 958k | default: |
212 | 958k | eob_pt = aom_read_symbol( |
213 | 958k | r, ec_ctx->eob_flag_cdf1024[plane_type][eob_multi_ctx], 11, |
214 | 958k | ACCT_STR) + |
215 | 958k | 1; |
216 | 958k | break; |
217 | 17.9M | } |
218 | | |
219 | 16.9M | const int eob_offset_bits = av1_eob_offset_bits[eob_pt]; |
220 | 16.9M | if (eob_offset_bits > 0) { |
221 | 8.75M | const int eob_ctx = eob_pt - 3; |
222 | 8.75M | int bit = aom_read_symbol( |
223 | 8.75M | r, ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_ctx], 2, ACCT_STR); |
224 | 8.75M | if (bit) { |
225 | 3.36M | eob_extra += (1 << (eob_offset_bits - 1)); |
226 | 3.36M | } |
227 | | |
228 | 27.3M | for (int i = 1; i < eob_offset_bits; i++) { |
229 | 18.6M | bit = aom_read_bit(r, ACCT_STR); |
230 | 18.6M | if (bit) { |
231 | 9.19M | eob_extra += (1 << (eob_offset_bits - 1 - i)); |
232 | 9.19M | } |
233 | 18.6M | } |
234 | 8.75M | } |
235 | 16.9M | *eob = rec_eob_pos(eob_pt, eob_extra); |
236 | | |
237 | 16.9M | if (*eob > 1) { |
238 | 9.33M | memset(levels_buf, 0, |
239 | 9.33M | sizeof(*levels_buf) * |
240 | 9.33M | ((height + TX_PAD_HOR) * (width + TX_PAD_VER) + TX_PAD_END)); |
241 | 9.33M | } |
242 | | |
243 | 16.9M | { |
244 | | // Read the non-zero coefficient with scan index eob-1 |
245 | | // TODO(angiebird): Put this into a function |
246 | 16.9M | const int c = *eob - 1; |
247 | 16.9M | const int pos = scan[c]; |
248 | 16.9M | const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, c); |
249 | 16.9M | const int nsymbs = 3; |
250 | 16.9M | aom_cdf_prob *cdf = |
251 | 16.9M | ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx]; |
252 | 16.9M | int level = aom_read_symbol(r, cdf, nsymbs, ACCT_STR) + 1; |
253 | 16.9M | if (level > NUM_BASE_LEVELS) { |
254 | 503k | const int br_ctx = get_br_ctx_eob(pos, bhl, tx_class); |
255 | 503k | cdf = ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx]; |
256 | 709k | for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
257 | 694k | const int k = aom_read_symbol(r, cdf, BR_CDF_SIZE, ACCT_STR); |
258 | 694k | level += k; |
259 | 694k | if (k < BR_CDF_SIZE - 1) break; |
260 | 694k | } |
261 | 503k | } |
262 | 16.9M | levels[get_padded_idx(pos, bhl)] = level; |
263 | 16.9M | } |
264 | 16.9M | if (*eob > 1) { |
265 | 9.34M | base_cdf_arr base_cdf = ec_ctx->coeff_base_cdf[txs_ctx][plane_type]; |
266 | 9.34M | br_cdf_arr br_cdf = |
267 | 9.34M | ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type]; |
268 | 9.34M | if (tx_class == TX_CLASS_2D) { |
269 | 8.29M | read_coeffs_reverse_2d(r, tx_size, 1, *eob - 1 - 1, scan, bhl, levels, |
270 | 8.29M | base_cdf, br_cdf); |
271 | 8.29M | read_coeffs_reverse(r, tx_size, tx_class, 0, 0, scan, bhl, levels, |
272 | 8.29M | base_cdf, br_cdf); |
273 | 8.29M | } else { |
274 | 1.05M | read_coeffs_reverse(r, tx_size, tx_class, 0, *eob - 1 - 1, scan, bhl, |
275 | 1.05M | levels, base_cdf, br_cdf); |
276 | 1.05M | } |
277 | 9.34M | } |
278 | | |
279 | 245M | for (int c = 0; c < *eob; ++c) { |
280 | 228M | const int pos = scan[c]; |
281 | 228M | uint8_t sign; |
282 | 228M | tran_low_t level = levels[get_padded_idx(pos, bhl)]; |
283 | 228M | if (level) { |
284 | 83.5M | *max_scan_line = AOMMAX(*max_scan_line, pos); |
285 | 83.5M | if (c == 0) { |
286 | 14.1M | const int dc_sign_ctx = txb_ctx->dc_sign_ctx; |
287 | 14.1M | sign = aom_read_symbol(r, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], |
288 | 14.1M | 2, ACCT_STR); |
289 | 69.3M | } else { |
290 | 69.3M | sign = aom_read_bit(r, ACCT_STR); |
291 | 69.3M | } |
292 | 83.5M | if (level >= MAX_BASE_BR_RANGE) { |
293 | 669k | level += read_golomb(xd, r); |
294 | 669k | } |
295 | | |
296 | 83.5M | if (c == 0) dc_val = sign ? -level : level; |
297 | | |
298 | | // Bitmasking to clamp level to valid range: |
299 | | // The valid range for 8/10/12 bit vdieo is at most 14/16/18 bit |
300 | 83.5M | level &= 0xfffff; |
301 | 83.5M | cul_level += level; |
302 | 83.5M | tran_low_t dq_coeff; |
303 | | // Bitmasking to clamp dq_coeff to valid range: |
304 | | // The valid range for 8/10/12 bit video is at most 17/19/21 bit |
305 | 83.5M | dq_coeff = |
306 | 83.5M | (tran_low_t)((int64_t)level * get_dqv(dequant, scan[c], iqmatrix) & |
307 | 83.5M | 0xffffff); |
308 | 83.5M | dq_coeff = dq_coeff >> shift; |
309 | 83.5M | if (sign) { |
310 | 39.5M | dq_coeff = -dq_coeff; |
311 | 39.5M | } |
312 | 83.5M | tcoeffs[pos] = clamp(dq_coeff, min_value, max_value); |
313 | 83.5M | } |
314 | 228M | } |
315 | | |
316 | 16.9M | cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level); |
317 | | |
318 | | // DC value |
319 | 16.9M | set_dc_sign(&cul_level, dc_val); |
320 | | |
321 | 16.9M | return cul_level; |
322 | 17.9M | } |
323 | | |
324 | | void av1_read_coeffs_txb(const AV1_COMMON *const cm, DecoderCodingBlock *dcb, |
325 | | aom_reader *const r, const int plane, const int row, |
326 | 28.8M | const int col, const TX_SIZE tx_size) { |
327 | | #if TXCOEFF_TIMER |
328 | | struct aom_usec_timer timer; |
329 | | aom_usec_timer_start(&timer); |
330 | | #endif |
331 | 28.8M | MACROBLOCKD *const xd = &dcb->xd; |
332 | 28.8M | MB_MODE_INFO *const mbmi = xd->mi[0]; |
333 | 28.8M | struct macroblockd_plane *const pd = &xd->plane[plane]; |
334 | | |
335 | 28.8M | const BLOCK_SIZE bsize = mbmi->bsize; |
336 | 28.8M | assert(bsize < BLOCK_SIZES_ALL); |
337 | 29.2M | const BLOCK_SIZE plane_bsize = |
338 | 29.2M | get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y); |
339 | | |
340 | 29.2M | TXB_CTX txb_ctx; |
341 | 29.2M | get_txb_ctx(plane_bsize, tx_size, plane, pd->above_entropy_context + col, |
342 | 29.2M | pd->left_entropy_context + row, &txb_ctx); |
343 | | |
344 | 29.2M | assert(dcb->xd.error_info->setjmp); |
345 | 29.2M | const uint8_t cul_level = |
346 | 29.2M | read_coeffs_txb(cm, dcb, r, row, col, plane, &txb_ctx, tx_size); |
347 | 29.2M | av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level, col, |
348 | 29.2M | row); |
349 | | |
350 | 29.2M | if (is_inter_block(mbmi)) { |
351 | 8.73M | const PLANE_TYPE plane_type = get_plane_type(plane); |
352 | | // tx_type will be read out in av1_read_coeffs_txb_facade |
353 | 8.73M | const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, row, col, tx_size, |
354 | 8.73M | cm->features.reduced_tx_set_used); |
355 | | |
356 | 8.73M | if (plane == 0) { |
357 | 3.68M | const int txw = tx_size_wide_unit[tx_size]; |
358 | 3.68M | const int txh = tx_size_high_unit[tx_size]; |
359 | | // The 16x16 unit is due to the constraint from tx_64x64 which sets the |
360 | | // maximum tx size for chroma as 32x32. Coupled with 4x1 transform block |
361 | | // size, the constraint takes effect in 32x16 / 16x32 size too. To solve |
362 | | // the intricacy, cover all the 16x16 units inside a 64 level transform. |
363 | 3.68M | if (txw == tx_size_wide_unit[TX_64X64] || |
364 | 3.54M | txh == tx_size_high_unit[TX_64X64]) { |
365 | 170k | const int tx_unit = tx_size_wide_unit[TX_16X16]; |
366 | 170k | const int stride = xd->tx_type_map_stride; |
367 | 759k | for (int idy = 0; idy < txh; idy += tx_unit) { |
368 | 2.68M | for (int idx = 0; idx < txw; idx += tx_unit) { |
369 | 2.09M | xd->tx_type_map[(row + idy) * stride + col + idx] = tx_type; |
370 | 2.09M | } |
371 | 588k | } |
372 | 170k | } |
373 | 3.68M | } |
374 | 8.73M | } |
375 | | |
376 | | #if TXCOEFF_TIMER |
377 | | aom_usec_timer_mark(&timer); |
378 | | const int64_t elapsed_time = aom_usec_timer_elapsed(&timer); |
379 | | cm->txcoeff_timer += elapsed_time; |
380 | | ++cm->txb_count; |
381 | | #endif |
382 | 29.2M | } |