/src/mozilla-central/third_party/aom/av1/decoder/decodetxb.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/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 | 0 | static int read_golomb(MACROBLOCKD *xd, aom_reader *r) { |
23 | 0 | int x = 1; |
24 | 0 | int length = 0; |
25 | 0 | int i = 0; |
26 | 0 |
|
27 | 0 | while (!i) { |
28 | 0 | i = aom_read_bit(r, ACCT_STR); |
29 | 0 | ++length; |
30 | 0 | if (length > 20) { |
31 | 0 | aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME, |
32 | 0 | "Invalid length in read_golomb"); |
33 | 0 | break; |
34 | 0 | } |
35 | 0 | } |
36 | 0 |
|
37 | 0 | for (i = 0; i < length - 1; ++i) { |
38 | 0 | x <<= 1; |
39 | 0 | x += aom_read_bit(r, ACCT_STR); |
40 | 0 | } |
41 | 0 |
|
42 | 0 | return x - 1; |
43 | 0 | } |
44 | | |
45 | 0 | static INLINE int rec_eob_pos(const int eob_token, const int extra) { |
46 | 0 | int eob = k_eob_group_start[eob_token]; |
47 | 0 | if (eob > 2) { |
48 | 0 | eob += extra; |
49 | 0 | } |
50 | 0 | return eob; |
51 | 0 | } |
52 | | |
53 | | static INLINE int get_dqv(const int16_t *dequant, int coeff_idx, |
54 | 0 | const qm_val_t *iqmatrix) { |
55 | 0 | int dqv = dequant[!!coeff_idx]; |
56 | 0 | if (iqmatrix != NULL) |
57 | 0 | dqv = |
58 | 0 | ((iqmatrix[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS; |
59 | 0 | return dqv; |
60 | 0 | } |
61 | | |
62 | | static INLINE void read_coeffs_reverse_2d(aom_reader *r, TX_SIZE tx_size, |
63 | | int start_si, int end_si, |
64 | | const int16_t *scan, int bwl, |
65 | | uint8_t *levels, |
66 | | base_cdf_arr base_cdf, |
67 | 0 | br_cdf_arr br_cdf) { |
68 | 0 | for (int c = end_si; c >= start_si; --c) { |
69 | 0 | const int pos = scan[c]; |
70 | 0 | const int coeff_ctx = get_lower_levels_ctx_2d(levels, pos, bwl, tx_size); |
71 | 0 | const int nsymbs = 4; |
72 | 0 | int level = aom_read_symbol(r, base_cdf[coeff_ctx], nsymbs, ACCT_STR); |
73 | 0 | if (level > NUM_BASE_LEVELS) { |
74 | 0 | const int br_ctx = get_br_ctx_2d(levels, pos, bwl); |
75 | 0 | aom_cdf_prob *cdf = br_cdf[br_ctx]; |
76 | 0 | for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
77 | 0 | const int k = aom_read_symbol(r, cdf, BR_CDF_SIZE, ACCT_STR); |
78 | 0 | level += k; |
79 | 0 | if (k < BR_CDF_SIZE - 1) break; |
80 | 0 | } |
81 | 0 | } |
82 | 0 | levels[get_padded_idx(pos, bwl)] = level; |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | | static INLINE void read_coeffs_reverse(aom_reader *r, TX_SIZE tx_size, |
87 | | TX_CLASS tx_class, int start_si, |
88 | | int end_si, const int16_t *scan, int bwl, |
89 | | uint8_t *levels, base_cdf_arr base_cdf, |
90 | 0 | br_cdf_arr br_cdf) { |
91 | 0 | for (int c = end_si; c >= start_si; --c) { |
92 | 0 | const int pos = scan[c]; |
93 | 0 | const int coeff_ctx = |
94 | 0 | get_lower_levels_ctx(levels, pos, bwl, tx_size, tx_class); |
95 | 0 | const int nsymbs = 4; |
96 | 0 | int level = aom_read_symbol(r, base_cdf[coeff_ctx], nsymbs, ACCT_STR); |
97 | 0 | if (level > NUM_BASE_LEVELS) { |
98 | 0 | const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class); |
99 | 0 | aom_cdf_prob *cdf = br_cdf[br_ctx]; |
100 | 0 | for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
101 | 0 | const int k = aom_read_symbol(r, cdf, BR_CDF_SIZE, ACCT_STR); |
102 | 0 | level += k; |
103 | 0 | if (k < BR_CDF_SIZE - 1) break; |
104 | 0 | } |
105 | 0 | } |
106 | 0 | levels[get_padded_idx(pos, bwl)] = level; |
107 | 0 | } |
108 | 0 | } |
109 | | |
110 | | uint8_t av1_read_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCKD *const xd, |
111 | | aom_reader *const r, const int blk_row, |
112 | | const int blk_col, const int plane, |
113 | | const TXB_CTX *const txb_ctx, |
114 | 0 | const TX_SIZE tx_size) { |
115 | 0 | FRAME_CONTEXT *const ec_ctx = xd->tile_ctx; |
116 | 0 | const int32_t max_value = (1 << (7 + xd->bd)) - 1; |
117 | 0 | const int32_t min_value = -(1 << (7 + xd->bd)); |
118 | 0 | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
119 | 0 | const PLANE_TYPE plane_type = get_plane_type(plane); |
120 | 0 | MB_MODE_INFO *const mbmi = xd->mi[0]; |
121 | 0 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
122 | 0 | const int16_t *const dequant = pd->seg_dequant_QTX[mbmi->segment_id]; |
123 | 0 | tran_low_t *const tcoeffs = pd->dqcoeff_block + xd->cb_offset[plane]; |
124 | 0 | const int shift = av1_get_tx_scale(tx_size); |
125 | 0 | const int bwl = get_txb_bwl(tx_size); |
126 | 0 | const int width = get_txb_wide(tx_size); |
127 | 0 | const int height = get_txb_high(tx_size); |
128 | 0 | int cul_level = 0; |
129 | 0 | int dc_val = 0; |
130 | 0 | uint8_t levels_buf[TX_PAD_2D]; |
131 | 0 | uint8_t *const levels = set_levels(levels_buf, width); |
132 | 0 | const int all_zero = aom_read_symbol( |
133 | 0 | r, ec_ctx->txb_skip_cdf[txs_ctx][txb_ctx->txb_skip_ctx], 2, ACCT_STR); |
134 | 0 | eob_info *eob_data = pd->eob_data + xd->txb_offset[plane]; |
135 | 0 | uint16_t *const eob = &(eob_data->eob); |
136 | 0 | uint16_t *const max_scan_line = &(eob_data->max_scan_line); |
137 | 0 | *max_scan_line = 0; |
138 | 0 | *eob = 0; |
139 | 0 | if (all_zero) { |
140 | 0 | *max_scan_line = 0; |
141 | 0 | if (plane == 0) { |
142 | 0 | const int txk_type_idx = |
143 | 0 | av1_get_txk_type_index(mbmi->sb_type, blk_row, blk_col); |
144 | 0 | mbmi->txk_type[txk_type_idx] = DCT_DCT; |
145 | 0 | } |
146 | 0 | return 0; |
147 | 0 | } |
148 | 0 |
|
149 | 0 | memset(levels_buf, 0, |
150 | 0 | sizeof(*levels_buf) * |
151 | 0 | ((width + TX_PAD_HOR) * (height + TX_PAD_VER) + TX_PAD_END)); |
152 | 0 | if (plane == AOM_PLANE_Y) { |
153 | 0 | // only y plane's tx_type is transmitted |
154 | 0 | av1_read_tx_type(cm, xd, blk_row, blk_col, tx_size, r); |
155 | 0 | } |
156 | 0 | const TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, blk_row, blk_col, |
157 | 0 | tx_size, cm->reduced_tx_set_used); |
158 | 0 | const TX_CLASS tx_class = tx_type_to_class[tx_type]; |
159 | 0 | const TX_SIZE qm_tx_size = av1_get_adjusted_tx_size(tx_size); |
160 | 0 | const qm_val_t *iqmatrix = |
161 | 0 | IS_2D_TRANSFORM(tx_type) |
162 | 0 | ? pd->seg_iqmatrix[mbmi->segment_id][qm_tx_size] |
163 | 0 | : cm->giqmatrix[NUM_QM_LEVELS - 1][0][qm_tx_size]; |
164 | 0 | const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
165 | 0 | const int16_t *const scan = scan_order->scan; |
166 | 0 | int eob_extra = 0; |
167 | 0 | int eob_pt = 1; |
168 | 0 |
|
169 | 0 | const int eob_multi_size = txsize_log2_minus4[tx_size]; |
170 | 0 | const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1; |
171 | 0 | switch (eob_multi_size) { |
172 | 0 | case 0: |
173 | 0 | eob_pt = |
174 | 0 | aom_read_symbol(r, ec_ctx->eob_flag_cdf16[plane_type][eob_multi_ctx], |
175 | 0 | 5, ACCT_STR) + |
176 | 0 | 1; |
177 | 0 | break; |
178 | 0 | case 1: |
179 | 0 | eob_pt = |
180 | 0 | aom_read_symbol(r, ec_ctx->eob_flag_cdf32[plane_type][eob_multi_ctx], |
181 | 0 | 6, ACCT_STR) + |
182 | 0 | 1; |
183 | 0 | break; |
184 | 0 | case 2: |
185 | 0 | eob_pt = |
186 | 0 | aom_read_symbol(r, ec_ctx->eob_flag_cdf64[plane_type][eob_multi_ctx], |
187 | 0 | 7, ACCT_STR) + |
188 | 0 | 1; |
189 | 0 | break; |
190 | 0 | case 3: |
191 | 0 | eob_pt = |
192 | 0 | aom_read_symbol(r, ec_ctx->eob_flag_cdf128[plane_type][eob_multi_ctx], |
193 | 0 | 8, ACCT_STR) + |
194 | 0 | 1; |
195 | 0 | break; |
196 | 0 | case 4: |
197 | 0 | eob_pt = |
198 | 0 | aom_read_symbol(r, ec_ctx->eob_flag_cdf256[plane_type][eob_multi_ctx], |
199 | 0 | 9, ACCT_STR) + |
200 | 0 | 1; |
201 | 0 | break; |
202 | 0 | case 5: |
203 | 0 | eob_pt = |
204 | 0 | aom_read_symbol(r, ec_ctx->eob_flag_cdf512[plane_type][eob_multi_ctx], |
205 | 0 | 10, ACCT_STR) + |
206 | 0 | 1; |
207 | 0 | break; |
208 | 0 | case 6: |
209 | 0 | default: |
210 | 0 | eob_pt = aom_read_symbol( |
211 | 0 | r, ec_ctx->eob_flag_cdf1024[plane_type][eob_multi_ctx], 11, |
212 | 0 | ACCT_STR) + |
213 | 0 | 1; |
214 | 0 | break; |
215 | 0 | } |
216 | 0 |
|
217 | 0 | if (k_eob_offset_bits[eob_pt] > 0) { |
218 | 0 | const int eob_ctx = eob_pt - 3; |
219 | 0 | int bit = aom_read_symbol( |
220 | 0 | r, ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_ctx], 2, ACCT_STR); |
221 | 0 | if (bit) { |
222 | 0 | eob_extra += (1 << (k_eob_offset_bits[eob_pt] - 1)); |
223 | 0 | } |
224 | 0 |
|
225 | 0 | for (int i = 1; i < k_eob_offset_bits[eob_pt]; i++) { |
226 | 0 | bit = aom_read_bit(r, ACCT_STR); |
227 | 0 | if (bit) { |
228 | 0 | eob_extra += (1 << (k_eob_offset_bits[eob_pt] - 1 - i)); |
229 | 0 | } |
230 | 0 | } |
231 | 0 | } |
232 | 0 | *eob = rec_eob_pos(eob_pt, eob_extra); |
233 | 0 |
|
234 | 0 | { |
235 | 0 | // Read the non-zero coefficient with scan index eob-1 |
236 | 0 | // TODO(angiebird): Put this into a function |
237 | 0 | const int c = *eob - 1; |
238 | 0 | const int pos = scan[c]; |
239 | 0 | const int coeff_ctx = get_lower_levels_ctx_eob(bwl, height, c); |
240 | 0 | const int nsymbs = 3; |
241 | 0 | aom_cdf_prob *cdf = |
242 | 0 | ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx]; |
243 | 0 | int level = aom_read_symbol(r, cdf, nsymbs, ACCT_STR) + 1; |
244 | 0 | if (level > NUM_BASE_LEVELS) { |
245 | 0 | const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class); |
246 | 0 | for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
247 | 0 | const int k = aom_read_symbol( |
248 | 0 | r, |
249 | 0 | ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx], |
250 | 0 | BR_CDF_SIZE, ACCT_STR); |
251 | 0 | level += k; |
252 | 0 | if (k < BR_CDF_SIZE - 1) break; |
253 | 0 | } |
254 | 0 | } |
255 | 0 | levels[get_padded_idx(pos, bwl)] = level; |
256 | 0 | } |
257 | 0 | if (*eob > 1) { |
258 | 0 | base_cdf_arr base_cdf = ec_ctx->coeff_base_cdf[txs_ctx][plane_type]; |
259 | 0 | br_cdf_arr br_cdf = |
260 | 0 | ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type]; |
261 | 0 | if (tx_class == TX_CLASS_2D) { |
262 | 0 | read_coeffs_reverse_2d(r, tx_size, 1, *eob - 1 - 1, scan, bwl, levels, |
263 | 0 | base_cdf, br_cdf); |
264 | 0 | read_coeffs_reverse(r, tx_size, tx_class, 0, 0, scan, bwl, levels, |
265 | 0 | base_cdf, br_cdf); |
266 | 0 | } else { |
267 | 0 | read_coeffs_reverse(r, tx_size, tx_class, 0, *eob - 1 - 1, scan, bwl, |
268 | 0 | levels, base_cdf, br_cdf); |
269 | 0 | } |
270 | 0 | } |
271 | 0 |
|
272 | 0 | int16_t num_zero_coeffs = 0; |
273 | 0 | for (int c = 0; c < *eob; ++c) { |
274 | 0 | const int pos = scan[c]; |
275 | 0 | num_zero_coeffs = AOMMAX(num_zero_coeffs, pos); |
276 | 0 | } |
277 | 0 | memset(tcoeffs, 0, (num_zero_coeffs + 1) * sizeof(tcoeffs[0])); |
278 | 0 |
|
279 | 0 | for (int c = 0; c < *eob; ++c) { |
280 | 0 | const int pos = scan[c]; |
281 | 0 | uint8_t sign; |
282 | 0 | tran_low_t level = levels[get_padded_idx(pos, bwl)]; |
283 | 0 | if (level) { |
284 | 0 | *max_scan_line = AOMMAX(*max_scan_line, pos); |
285 | 0 | if (c == 0) { |
286 | 0 | const int dc_sign_ctx = txb_ctx->dc_sign_ctx; |
287 | 0 | sign = aom_read_symbol(r, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], |
288 | 0 | 2, ACCT_STR); |
289 | 0 | } else { |
290 | 0 | sign = aom_read_bit(r, ACCT_STR); |
291 | 0 | } |
292 | 0 | if (level >= MAX_BASE_BR_RANGE) { |
293 | 0 | level += read_golomb(xd, r); |
294 | 0 | } |
295 | 0 |
|
296 | 0 | if (c == 0) dc_val = sign ? -level : level; |
297 | 0 |
|
298 | 0 | // Bitmasking to clamp level to valid range: |
299 | 0 | // The valid range for 8/10/12 bit vdieo is at most 14/16/18 bit |
300 | 0 | level &= 0xfffff; |
301 | 0 | cul_level += level; |
302 | 0 | tran_low_t dq_coeff; |
303 | 0 | // Bitmasking to clamp dq_coeff to valid range: |
304 | 0 | // The valid range for 8/10/12 bit video is at most 17/19/21 bit |
305 | 0 | dq_coeff = (tran_low_t)( |
306 | 0 | (int64_t)level * get_dqv(dequant, scan[c], iqmatrix) & 0xffffff); |
307 | 0 | dq_coeff = dq_coeff >> shift; |
308 | 0 | if (sign) { |
309 | 0 | dq_coeff = -dq_coeff; |
310 | 0 | } |
311 | 0 | tcoeffs[pos] = clamp(dq_coeff, min_value, max_value); |
312 | 0 | } |
313 | 0 | } |
314 | 0 |
|
315 | 0 | cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level); |
316 | 0 |
|
317 | 0 | // DC value |
318 | 0 | set_dc_sign(&cul_level, dc_val); |
319 | 0 |
|
320 | 0 | return cul_level; |
321 | 0 | } |
322 | | |
323 | | void av1_read_coeffs_txb_facade(const AV1_COMMON *const cm, |
324 | | MACROBLOCKD *const xd, aom_reader *const r, |
325 | | const int plane, const int row, const int col, |
326 | 0 | const TX_SIZE tx_size) { |
327 | | #if TXCOEFF_TIMER |
328 | | struct aom_usec_timer timer; |
329 | | aom_usec_timer_start(&timer); |
330 | | #endif |
331 | | MB_MODE_INFO *const mbmi = xd->mi[0]; |
332 | 0 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
333 | 0 |
|
334 | 0 | const BLOCK_SIZE bsize = mbmi->sb_type; |
335 | 0 | const BLOCK_SIZE plane_bsize = |
336 | 0 | get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y); |
337 | 0 |
|
338 | 0 | TXB_CTX txb_ctx; |
339 | 0 | get_txb_ctx(plane_bsize, tx_size, plane, pd->above_context + col, |
340 | 0 | pd->left_context + row, &txb_ctx); |
341 | 0 | const uint8_t cul_level = |
342 | 0 | av1_read_coeffs_txb(cm, xd, r, row, col, plane, &txb_ctx, tx_size); |
343 | 0 | av1_set_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level, col, row); |
344 | 0 |
|
345 | 0 | if (is_inter_block(mbmi)) { |
346 | 0 | PLANE_TYPE plane_type = get_plane_type(plane); |
347 | 0 | // tx_type will be read out in av1_read_coeffs_txb_facade |
348 | 0 | const TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, row, col, tx_size, |
349 | 0 | cm->reduced_tx_set_used); |
350 | 0 |
|
351 | 0 | if (plane == 0) |
352 | 0 | update_txk_array(mbmi->txk_type, mbmi->sb_type, row, col, tx_size, |
353 | 0 | tx_type); |
354 | 0 | } |
355 | 0 |
|
356 | | #if TXCOEFF_TIMER |
357 | | aom_usec_timer_mark(&timer); |
358 | | const int64_t elapsed_time = aom_usec_timer_elapsed(&timer); |
359 | | cm->txcoeff_timer += elapsed_time; |
360 | | ++cm->txb_count; |
361 | | #endif |
362 | | } |