/src/avm/av2/decoder/decodetxb.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Alliance for Open Media. All rights reserved |
3 | | * |
4 | | * This source code is subject to the terms of the BSD 3-Clause Clear License |
5 | | * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear |
6 | | * License was not distributed with this source code in the LICENSE file, you |
7 | | * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the |
8 | | * Alliance for Open Media Patent License 1.0 was not distributed with this |
9 | | * source code in the PATENTS file, you can obtain it at |
10 | | * aomedia.org/license/patent-license/. |
11 | | */ |
12 | | |
13 | | #include "av2/decoder/decodetxb.h" |
14 | | |
15 | | #include "avm_ports/mem.h" |
16 | | #include "av2/common/hr_coding.h" |
17 | | #include "av2/common/idct.h" |
18 | | #include "av2/common/pred_common.h" |
19 | | #include "av2/common/reconintra.h" |
20 | | #include "av2/common/scan.h" |
21 | | #include "av2/common/txb_common.h" |
22 | | #include "av2/decoder/decodemv.h" |
23 | | |
24 | | #if CONFIG_PARAKIT_COLLECT_DATA |
25 | | #include "av2/common/cost.h" |
26 | | #endif |
27 | | |
28 | | #if CONFIG_PARAKIT_COLLECT_DATA |
29 | | static int get_q_ctx(int q) { |
30 | | if (q <= 90) return 0; |
31 | | if (q <= 140) return 1; |
32 | | if (q <= 190) return 2; |
33 | | return 3; |
34 | | } |
35 | | #endif |
36 | | |
37 | | /*!\brief Read and decode from bitstream an integer value following Exp-Golomb |
38 | | * coding with order k |
39 | | * |
40 | | * \ingroup coefficient_coding |
41 | | * |
42 | | * This function reads and decodes from the bitstream r an integer value which |
43 | | * was coded in Exp-Golomb with order k |
44 | | * |
45 | | * \param[in] xd Pointer to structure holding the data for the |
46 | | * current macroblockd |
47 | | * \param[in] r Pointer to the bitstream reader |
48 | | * \param[in] k Order of Exp-Golomb coding |
49 | | * |
50 | | */ |
51 | 13.3k | static int read_exp_golomb(MACROBLOCKD *xd, avm_reader *r, int k) { |
52 | 13.3k | int length = avm_read_unary(r, 21, ACCT_INFO("hr")); |
53 | 13.3k | if (length > 20) { |
54 | 1.19k | avm_internal_error(xd->error_info, AVM_CODEC_CORRUPT_FRAME, |
55 | 1.19k | "Invalid length in read_exp_golomb"); |
56 | 1.19k | } |
57 | 13.3k | length += k; |
58 | 13.3k | int x = 1 << length; |
59 | 13.3k | x += avm_read_literal(r, length, ACCT_INFO("hr")); |
60 | | |
61 | 13.3k | return x - (1 << k); |
62 | 13.3k | } |
63 | | |
64 | | /*!\brief Read and decode from the bitstream a Truncated-Rice coded integer |
65 | | * value. |
66 | | * |
67 | | * \ingroup coefficient_coding |
68 | | * |
69 | | * This function reads and decodes from the bitstream a Truncated-Rice coded |
70 | | * integer value, with Rice parameter m. It first reads and decodes the unary |
71 | | * prefix q from the input bitstream r. Given a shorter prefix (q < cmax), it |
72 | | * decodes the remaining offset of the integer using Golomb-Rice coding; |
73 | | * otherwise (i.e., when q == cmax) it shifts to use Exp-Golomb to decode the |
74 | | * remainder value. |
75 | | * |
76 | | * \param[in] xd Pointer to structure holding the data for the |
77 | | * current macroblockd |
78 | | * \param[in] r Pointer to the reader of the bitstream |
79 | | * \param[in] m Parameter of the Rice distribution |
80 | | * \param[in] k Order of the Exp-Golomb code |
81 | | * \param[in] cmax Maximum unary prefix length above which Exp-Golomb |
82 | | * is used instead of Golomb-Rice coding |
83 | | * |
84 | | */ |
85 | | static int read_truncated_rice(MACROBLOCKD *xd, avm_reader *r, int m, int k, |
86 | 411k | int cmax) { |
87 | 411k | int q = avm_read_unary(r, cmax, ACCT_INFO("hr")); |
88 | | |
89 | 411k | int rem = (q == cmax) ? read_exp_golomb(xd, r, k) |
90 | 411k | : avm_read_literal(r, m, ACCT_INFO("hr")); |
91 | 411k | return rem + (q << m); |
92 | 411k | } |
93 | | |
94 | | /*!\brief Read and decode from the bitstream the high range (HR) value of |
95 | | * the coefficient level using adaptive Truncated-Rice coding. |
96 | | * |
97 | | * \ingroup coefficient_coding |
98 | | * |
99 | | * This function reads and decodes from the bitstream the high range (HR) |
100 | | * value of the coefficient level following adaptive Truncated-Rice coding. |
101 | | * It first derives the Rice parameter m from the input context value ctx. |
102 | | * It then invokes the read_truncated_rice function to decode the HR |
103 | | * coefficient value using Rice parameter m, Exp-Golomb order k = m + 1, |
104 | | * and maximum unary prefix lenght cmax = AVMMIN(m + 4, 6). |
105 | | * |
106 | | * \param[in] xd Pointer to structure holding the data for the |
107 | | * current macroblockd |
108 | | * \param[in] r Pointer to the reader of the bitstream |
109 | | * \param[in] ctx Context value |
110 | | * |
111 | | */ |
112 | 411k | static int read_adaptive_hr(MACROBLOCKD *xd, avm_reader *r, int ctx) { |
113 | 411k | int m = get_adaptive_param(ctx); |
114 | 411k | return read_truncated_rice(xd, r, m, m + 1, AVMMIN(m + 4, 6)); |
115 | 411k | } |
116 | | |
117 | | // Read high range part of coeff |
118 | | static INLINE int read_high_range(MACROBLOCKD *xd, avm_reader *r, int tcq_mode, |
119 | 12.2M | int level, int lf, int *hr_avg, int plane) { |
120 | 12.2M | int max_br = lf ? (plane == 0 ? LF_MAX_BASE_BR_RANGE // 8 |
121 | 6.64M | : LF_NUM_BASE_LEVELS + 1) |
122 | 12.2M | : MAX_BASE_BR_RANGE; |
123 | 12.2M | int use_tcq_hr = tcq_mode && (level >= max_br - 1); |
124 | 12.2M | int hr_level_avg = *hr_avg; |
125 | 12.2M | int use_hr = use_tcq_hr || level >= max_br; |
126 | 12.2M | if (use_hr) { |
127 | 396k | int hr = read_adaptive_hr(xd, r, hr_level_avg); |
128 | 396k | level += hr << (tcq_mode ? 1 : 0); |
129 | 396k | *hr_avg = (hr_level_avg + hr) >> 1; |
130 | 396k | } |
131 | 12.2M | return level; |
132 | 12.2M | } |
133 | | |
134 | 3.06M | static INLINE int rec_eob_pos(const int eob_token, const int extra) { |
135 | 3.06M | int eob = av2_eob_group_start[eob_token]; |
136 | 3.06M | if (eob > 2) { |
137 | 1.01M | eob += extra; |
138 | 1.01M | } |
139 | 3.06M | return eob; |
140 | 3.06M | } |
141 | | |
142 | | static INLINE int get_dqv(const int32_t *dequant, int coeff_idx, |
143 | 12.3M | const qm_val_t *iqmatrix) { |
144 | 12.3M | int dqv = dequant[!!coeff_idx]; |
145 | 12.3M | if (iqmatrix != NULL) |
146 | 1.64M | dqv = |
147 | 1.64M | ((iqmatrix[coeff_idx] * dqv) + (1 << (AVM_QM_BITS - 1))) >> AVM_QM_BITS; |
148 | 12.3M | return dqv; |
149 | 12.3M | } |
150 | | |
151 | 1.04M | static int read_low_range(avm_reader *r, avm_cdf_prob *cdf) { |
152 | 1.04M | int br_level = 0; |
153 | 1.35M | for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
154 | 1.04M | const int k = avm_read_symbol(r, cdf, BR_CDF_SIZE, |
155 | 1.04M | ACCT_INFO("k", "read_low_range cdf")); |
156 | 1.04M | br_level += k; |
157 | 1.04M | if (k < BR_CDF_SIZE - 1) break; |
158 | 1.04M | } |
159 | 1.04M | return br_level; |
160 | 1.04M | } |
161 | | |
162 | | static INLINE void read_coeffs_reverse_2d( |
163 | | avm_reader *r, int start_si, int end_si, const int16_t *scan, int bwl, |
164 | | uint8_t *levels, base_lf_cdf_arr base_lf_cdf, br_cdf_arr br_lf_cdf, |
165 | | int plane, base_cdf_arr base_cdf, br_cdf_arr br_cdf, |
166 | | base_lf_uv_cdf_arr base_lf_uv_cdf, base_uv_cdf_arr base_uv_cdf, |
167 | 1.09M | br_cdf_arr br_uv_cdf, int *state) { |
168 | 16.5M | for (int c = end_si; c >= start_si; --c) { |
169 | 15.4M | const int pos = scan[c]; |
170 | 15.4M | int level = 0; |
171 | 15.4M | const int row = pos >> bwl; |
172 | 15.4M | const int col = pos - (row << bwl); |
173 | 15.4M | int limits = get_lf_limits(row, col, 0, plane); |
174 | 15.4M | int q_i = tcq_quant(*state); |
175 | 15.4M | if (plane > 0) { |
176 | 2.16M | if (limits) { |
177 | 0 | const int coeff_ctx = |
178 | 0 | get_lower_levels_ctx_lf_2d_chroma(levels, pos, bwl, plane); |
179 | 0 | level += avm_read_symbol(r, base_lf_uv_cdf[coeff_ctx], LF_BASE_SYMBOLS, |
180 | 0 | ACCT_INFO("level", "base_lf_uv_cdf")); |
181 | 2.16M | } else { |
182 | 2.16M | const int coeff_ctx = |
183 | 2.16M | get_lower_levels_ctx_2d_chroma(levels, pos, bwl, plane); |
184 | 2.16M | level += avm_read_symbol(r, base_uv_cdf[coeff_ctx], 4, |
185 | 2.16M | ACCT_INFO("level", "base_uv_cdf")); |
186 | 2.16M | if (level > NUM_BASE_LEVELS) { |
187 | 143k | const int br_ctx = get_br_ctx_2d_chroma(levels, pos, bwl); |
188 | 143k | avm_cdf_prob *cdf = br_uv_cdf[br_ctx]; |
189 | 143k | level += read_low_range(r, cdf); |
190 | 143k | } |
191 | 2.16M | } |
192 | 13.2M | } else { |
193 | 13.2M | if (limits) { |
194 | 4.74M | const int coeff_ctx = get_lower_levels_ctx_lf_2d(levels, pos, bwl); |
195 | 4.74M | level += |
196 | 4.74M | avm_read_symbol(r, base_lf_cdf[coeff_ctx][q_i], LF_BASE_SYMBOLS, |
197 | 4.74M | ACCT_INFO("level", "base_lf_cdf")); |
198 | 4.74M | if (level > LF_NUM_BASE_LEVELS) { |
199 | 261k | const int br_ctx = get_br_lf_ctx_2d(levels, pos, bwl); |
200 | 261k | avm_cdf_prob *cdf = br_lf_cdf[br_ctx]; |
201 | 261k | level += read_low_range(r, cdf); |
202 | 261k | } |
203 | 8.52M | } else { |
204 | 8.52M | const int coeff_ctx = get_lower_levels_ctx_2d(levels, pos, bwl, plane); |
205 | 8.52M | level += avm_read_symbol(r, base_cdf[coeff_ctx][q_i], 4, |
206 | 8.52M | ACCT_INFO("level", "base_cdf")); |
207 | 8.52M | if (level > NUM_BASE_LEVELS) { |
208 | 418k | const int br_ctx = get_br_ctx_2d(levels, pos, bwl); |
209 | 418k | avm_cdf_prob *cdf = br_cdf[br_ctx]; |
210 | 418k | level += read_low_range(r, cdf); |
211 | 418k | } |
212 | 8.52M | } |
213 | 13.2M | } |
214 | 15.4M | levels[get_padded_idx(pos, bwl)] = level; |
215 | 15.4M | *state = tcq_next_state(*state, level); |
216 | 15.4M | } |
217 | 1.09M | } |
218 | | |
219 | | static INLINE void read_coeffs_reverse( |
220 | | avm_reader *r, TX_CLASS tx_class, int start_si, int end_si, |
221 | | const int16_t *scan, int bwl, uint8_t *levels, base_lf_cdf_arr base_lf_cdf, |
222 | | br_cdf_arr br_lf_cdf, int plane, base_cdf_arr base_cdf, br_cdf_arr br_cdf, |
223 | | base_lf_uv_cdf_arr base_lf_uv_cdf, base_uv_cdf_arr base_uv_cdf, |
224 | 1.06M | br_cdf_arr br_uv_cdf, int *state) { |
225 | 2.30M | for (int c = end_si; c >= start_si; --c) { |
226 | 1.24M | const int pos = scan[c]; |
227 | 1.24M | int level = 0; |
228 | 1.24M | int q_i = tcq_quant(*state); |
229 | 1.24M | const int row = pos >> bwl; |
230 | 1.24M | const int col = pos - (row << bwl); |
231 | 1.24M | int limits = get_lf_limits(row, col, tx_class, plane); |
232 | 1.24M | if (plane > 0) { |
233 | 246k | if (limits) { |
234 | 243k | const int coeff_ctx = |
235 | 243k | get_lower_levels_lf_ctx_chroma(levels, pos, bwl, tx_class, plane); |
236 | 243k | level += avm_read_symbol(r, base_lf_uv_cdf[coeff_ctx], LF_BASE_SYMBOLS, |
237 | 243k | ACCT_INFO("level", "base_lf_uv_cdf")); |
238 | 243k | } else { |
239 | 2.69k | const int coeff_ctx = |
240 | 2.69k | get_lower_levels_ctx_chroma(levels, pos, bwl, tx_class, plane); |
241 | 2.69k | level += avm_read_symbol(r, base_uv_cdf[coeff_ctx], 4, |
242 | 2.69k | ACCT_INFO("level", "base_uv_cdf")); |
243 | 2.69k | if (level > NUM_BASE_LEVELS) { |
244 | 241 | const int br_ctx = get_br_ctx_chroma(levels, pos, bwl, tx_class); |
245 | 241 | avm_cdf_prob *cdf = br_uv_cdf[br_ctx]; |
246 | 241 | level += read_low_range(r, cdf); |
247 | 241 | } |
248 | 2.69k | } |
249 | 996k | } else { |
250 | 996k | if (limits) { |
251 | 907k | const int coeff_ctx = |
252 | 907k | get_lower_levels_lf_ctx(levels, pos, bwl, tx_class); |
253 | 907k | level += |
254 | 907k | avm_read_symbol(r, base_lf_cdf[coeff_ctx][q_i], LF_BASE_SYMBOLS, |
255 | 907k | ACCT_INFO("level", "base_lf_cdf")); |
256 | | |
257 | 907k | if (level > LF_NUM_BASE_LEVELS) { |
258 | 147k | const int br_ctx = get_br_lf_ctx(levels, pos, bwl, tx_class); |
259 | 147k | avm_cdf_prob *cdf = br_lf_cdf[br_ctx]; |
260 | 147k | level += read_low_range(r, cdf); |
261 | 147k | } |
262 | 907k | } else { |
263 | 89.6k | const int coeff_ctx = |
264 | 89.6k | get_lower_levels_ctx(levels, pos, bwl, tx_class, plane); |
265 | 89.6k | level += avm_read_symbol(r, base_cdf[coeff_ctx][q_i], 4, |
266 | 89.6k | ACCT_INFO("level", "base_cdf")); |
267 | 89.6k | if (level > NUM_BASE_LEVELS) { |
268 | 8.77k | const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class); |
269 | 8.77k | avm_cdf_prob *cdf = br_cdf[br_ctx]; |
270 | 8.77k | level += read_low_range(r, cdf); |
271 | 8.77k | } |
272 | 89.6k | } |
273 | 996k | } |
274 | 1.24M | levels[get_padded_idx(pos, bwl)] = level; |
275 | 1.24M | *state = tcq_next_state(*state, level); |
276 | 1.24M | } |
277 | 1.06M | } |
278 | | |
279 | | static INLINE void read_coeffs_forward_2d(avm_reader *r, int start_si, |
280 | | int end_si, const int16_t *scan, |
281 | | int bwl, uint8_t *levels, |
282 | | base_fsc_cdf_arr base_cdf, |
283 | 30.4k | br_cdf_arr br_cdf) { |
284 | 508k | for (int c = start_si; c <= end_si; c++) { |
285 | 477k | const int pos = scan[c]; |
286 | 477k | const int coeff_ctx = get_upper_levels_ctx_2d(levels, pos, bwl); |
287 | 477k | const int nsymbs = 4; |
288 | 477k | int level = avm_read_symbol(r, base_cdf[coeff_ctx], nsymbs, |
289 | 477k | ACCT_INFO("level", "base_cdf")); |
290 | 477k | if (level > NUM_BASE_LEVELS) { |
291 | 42.5k | const int br_ctx = get_br_ctx_skip(levels, pos, bwl); |
292 | 42.5k | avm_cdf_prob *cdf = br_cdf[br_ctx]; |
293 | 42.5k | level += read_low_range(r, cdf); |
294 | 42.5k | } |
295 | 477k | levels[get_padded_idx_left(pos, bwl)] = level; |
296 | 477k | } |
297 | 30.4k | } |
298 | | |
299 | | // Decode the end-of-block syntax. |
300 | | static INLINE void decode_eob(DecoderCodingBlock *dcb, avm_reader *const r, |
301 | | const int plane, const TX_SIZE tx_size |
302 | | #if CONFIG_PARAKIT_COLLECT_DATA |
303 | | , |
304 | | const AV2_COMMON *const cm |
305 | | #endif |
306 | 3.05M | ) { |
307 | 3.05M | MACROBLOCKD *const xd = &dcb->xd; |
308 | 3.05M | FRAME_CONTEXT *const ec_ctx = xd->tile_ctx; |
309 | 3.05M | const int is_inter = is_inter_block(xd->mi[0], xd->tree_type); |
310 | 3.05M | const int pl_ctx = get_eob_plane_ctx(plane, is_inter); |
311 | 3.05M | eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane]; |
312 | 3.05M | uint16_t *const eob = &(eob_data->eob); |
313 | 3.05M | eob_info *bob_data = dcb->bob_data[plane] + dcb->txb_offset[plane]; |
314 | 3.05M | uint16_t *const bob = &(bob_data->eob); |
315 | | |
316 | 3.05M | int eob_extra = 0; |
317 | 3.05M | int eob_pt = 1; |
318 | 3.05M | const int eob_multi_size = txsize_log2_minus4[tx_size]; |
319 | | #if CONFIG_PARAKIT_COLLECT_DATA |
320 | | const int qp_index = get_q_ctx(cm->quant_params.base_qindex); |
321 | | int idxlist[MAX_CTX_DIM]; |
322 | | idxlist[0] = qp_index; |
323 | | idxlist[1] = pl_ctx; |
324 | | idxlist[2] = -1; |
325 | | idxlist[3] = -1; |
326 | | #endif |
327 | 3.05M | switch (eob_multi_size) { |
328 | 1.84M | case 0: |
329 | | #if CONFIG_PARAKIT_COLLECT_DATA |
330 | | { |
331 | | eob_pt = |
332 | | avm_read_symbol_probdata(r, ec_ctx->eob_flag_cdf16[pl_ctx], idxlist, |
333 | | cm->prob_models[EOB_FLAG_CDF16]) + |
334 | | 1; |
335 | | break; |
336 | | } |
337 | | #else |
338 | 1.84M | eob_pt = |
339 | 1.84M | avm_read_symbol(r, ec_ctx->eob_flag_cdf16[pl_ctx], EOB_MAX_SYMS - 6, |
340 | 1.84M | ACCT_INFO("eob_pt", "eob_multi_size:0")) + |
341 | 1.84M | 1; |
342 | 1.84M | break; |
343 | 0 | #endif |
344 | 140k | case 1: |
345 | | #if CONFIG_PARAKIT_COLLECT_DATA |
346 | | { |
347 | | eob_pt = |
348 | | avm_read_symbol_probdata(r, ec_ctx->eob_flag_cdf32[pl_ctx], idxlist, |
349 | | cm->prob_models[EOB_FLAG_CDF32]) + |
350 | | 1; |
351 | | break; |
352 | | } |
353 | | #else |
354 | 140k | eob_pt = |
355 | 140k | avm_read_symbol(r, ec_ctx->eob_flag_cdf32[pl_ctx], EOB_MAX_SYMS - 5, |
356 | 140k | ACCT_INFO("eob_pt", "eob_multi_size:1")) + |
357 | 140k | 1; |
358 | 140k | break; |
359 | 0 | #endif |
360 | 192k | case 2: |
361 | 192k | eob_pt = |
362 | 192k | avm_read_symbol(r, ec_ctx->eob_flag_cdf64[pl_ctx], EOB_MAX_SYMS - 4, |
363 | 192k | ACCT_INFO("eob_pt", "eob_multi_size:2")) + |
364 | 192k | 1; |
365 | 192k | break; |
366 | 220k | case 3: |
367 | 220k | eob_pt = |
368 | 220k | avm_read_symbol(r, ec_ctx->eob_flag_cdf128[pl_ctx], EOB_MAX_SYMS - 3, |
369 | 220k | ACCT_INFO("eob_pt", "eob_multi_size:3")) + |
370 | 220k | 1; |
371 | 220k | break; |
372 | 203k | case 4: |
373 | 203k | eob_pt = |
374 | 203k | avm_read_symbol(r, ec_ctx->eob_flag_cdf256[pl_ctx], EOB_MAX_SYMS - 3, |
375 | 203k | ACCT_INFO("eob_pt", "eob_multi_size:4")); |
376 | 203k | if (eob_pt == EOB_PT_INDEX_COUNT - 1) |
377 | 21.4k | eob_pt += |
378 | 21.4k | avm_read_literal(r, 1, ACCT_INFO("eob_pt", "eob_multi_size:4")); |
379 | 203k | eob_pt += 1; |
380 | 203k | break; |
381 | 163k | case 5: |
382 | 163k | eob_pt = |
383 | 163k | avm_read_symbol(r, ec_ctx->eob_flag_cdf512[pl_ctx], EOB_MAX_SYMS - 3, |
384 | 163k | ACCT_INFO("eob_pt", "eob_multi_size:5")); |
385 | 163k | if (eob_pt == EOB_PT_INDEX_COUNT - 1) |
386 | 4.69k | eob_pt += |
387 | 4.69k | avm_read_literal(r, 2, ACCT_INFO("eob_pt", "eob_multi_size:5")); |
388 | 163k | if (eob_pt >= EOB_MAX_SYMS - 1) { |
389 | 788 | avm_internal_error(xd->error_info, AVM_CODEC_CORRUPT_FRAME, |
390 | 788 | "Invalid value for EOB position token"); |
391 | 788 | } |
392 | 163k | eob_pt += 1; |
393 | 163k | break; |
394 | 275k | case 6: |
395 | 275k | default: |
396 | 275k | eob_pt = |
397 | 275k | avm_read_symbol(r, ec_ctx->eob_flag_cdf1024[pl_ctx], EOB_MAX_SYMS - 3, |
398 | 275k | ACCT_INFO("eob_pt", "eob_multi_size:6")); |
399 | 275k | if (eob_pt == EOB_PT_INDEX_COUNT - 1) |
400 | 6.51k | eob_pt += |
401 | 6.51k | avm_read_literal(r, 2, ACCT_INFO("eob_pt", "eob_multi_size:6")); |
402 | 275k | eob_pt += 1; |
403 | 275k | break; |
404 | 3.05M | } |
405 | | |
406 | 3.05M | const int eob_offset_bits = av2_eob_offset_bits[eob_pt]; |
407 | 3.05M | if (eob_offset_bits > 0) { |
408 | 1.01M | int bit = avm_read_symbol(r, ec_ctx->eob_extra_cdf, 2, |
409 | 1.01M | ACCT_INFO("eob_extra_cdf")); |
410 | 1.01M | if (bit) { |
411 | 450k | eob_extra += (1 << (eob_offset_bits - 1)); |
412 | 450k | } |
413 | 1.01M | eob_extra += |
414 | 1.01M | avm_read_literal(r, eob_offset_bits - 1, ACCT_INFO("eob_extra")); |
415 | 1.01M | } |
416 | 3.05M | *eob = rec_eob_pos(eob_pt, eob_extra); |
417 | 3.05M | *bob = *eob; // escape character |
418 | 3.05M | } |
419 | | |
420 | | uint8_t av2_read_sig_txtype(const AV2_COMMON *const cm, DecoderCodingBlock *dcb, |
421 | | avm_reader *const r, const int blk_row, |
422 | | const int blk_col, const int plane, |
423 | | const TXB_CTX *const txb_ctx, |
424 | 7.83M | const TX_SIZE tx_size) { |
425 | 7.83M | MACROBLOCKD *const xd = &dcb->xd; |
426 | 7.83M | FRAME_CONTEXT *const ec_ctx = xd->tile_ctx; |
427 | 7.83M | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
428 | | |
429 | 7.83M | const int is_inter = is_inter_block(xd->mi[0], xd->tree_type); |
430 | | |
431 | 7.83M | eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane]; |
432 | 7.83M | uint16_t *const eob = &(eob_data->eob); |
433 | 7.83M | uint16_t *const max_scan_line = &(eob_data->max_scan_line); |
434 | 7.83M | *max_scan_line = 0; |
435 | 7.83M | *eob = 0; |
436 | | |
437 | 7.83M | int txb_skip_ctx = txb_ctx->txb_skip_ctx; |
438 | 7.83M | int all_zero; |
439 | 7.83M | if (plane == AVM_PLANE_Y || plane == AVM_PLANE_U) { |
440 | 6.11M | MB_MODE_INFO *const mbmi = xd->mi[0]; |
441 | 6.11M | const int pred_mode_ctx = |
442 | 6.11M | (is_inter || mbmi->fsc_mode[xd->tree_type == CHROMA_PART]) ? 1 : 0; |
443 | 6.11M | all_zero = avm_read_symbol( |
444 | 6.11M | r, ec_ctx->txb_skip_cdf[pred_mode_ctx][txs_ctx][txb_skip_ctx], 2, |
445 | 6.11M | ACCT_INFO("all_zero", "plane_y_or_u")); |
446 | 6.11M | } else { |
447 | 1.72M | txb_skip_ctx += (xd->eob_u_flag ? V_TXB_SKIP_CONTEXT_OFFSET : 0); |
448 | 1.72M | all_zero = avm_read_symbol(r, ec_ctx->v_txb_skip_cdf[txb_skip_ctx], 2, |
449 | 1.72M | ACCT_INFO("all_zero", "plane_v")); |
450 | 1.72M | } |
451 | | |
452 | | #if CONFIG_INSPECTION |
453 | | MB_MODE_INFO *const mbmi = xd->mi[0]; |
454 | | if (plane == 0) { |
455 | | const int txk_type_idx = |
456 | | av2_get_txk_type_index(mbmi->sb_type[0], blk_row, blk_col); |
457 | | mbmi->tx_skip[txk_type_idx] = all_zero; |
458 | | } |
459 | | #endif // CONFIG_INSPECTION |
460 | | |
461 | 7.83M | if (plane == AVM_PLANE_U) { |
462 | 1.64M | xd->eob_u_flag = all_zero ? 0 : 1; |
463 | 1.64M | } |
464 | | |
465 | 7.83M | #if !CONFIG_INSPECTION |
466 | 7.83M | MB_MODE_INFO *mbmi = xd->mi[0]; |
467 | 7.83M | #endif // !CONFIG_INSPECTION |
468 | | |
469 | 7.83M | if (is_inter_block(mbmi, xd->tree_type)) { |
470 | 68.7k | const int txb_idx = get_tx_partition_idx(mbmi, plane); |
471 | 68.7k | mbmi->is_wide_angle[plane > 0][txb_idx] = 0; |
472 | 68.7k | mbmi->mapped_intra_mode[plane > 0][txb_idx] = DC_PRED; |
473 | 7.76M | } else { |
474 | 7.76M | PREDICTION_MODE mode = (plane == PLANE_TYPE_Y ? mbmi->mode : mbmi->uv_mode); |
475 | 7.76M | const int angle_delta = |
476 | 7.76M | mbmi->angle_delta[plane != AVM_PLANE_Y] * ANGLE_STEP; |
477 | 7.76M | wide_angle_mapping(mbmi, angle_delta, tx_size, mode, plane); |
478 | 7.76M | } |
479 | | |
480 | 7.83M | if (all_zero) { |
481 | 4.59M | *max_scan_line = 0; |
482 | 4.59M | if (plane == 0) { |
483 | 2.27M | xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col] = DCT_DCT; |
484 | 2.27M | } |
485 | | |
486 | 4.59M | return 0; |
487 | 4.59M | } |
488 | 3.23M | decode_eob(dcb, r, plane, tx_size |
489 | | #if CONFIG_PARAKIT_COLLECT_DATA |
490 | | , |
491 | | cm |
492 | | #endif |
493 | 3.23M | ); |
494 | 3.23M | av2_read_tx_type(cm, xd, blk_row, blk_col, tx_size, r, plane, *eob, |
495 | 3.23M | is_inter ? 0 : *eob); |
496 | | |
497 | 3.23M | if (plane == AVM_PLANE_U && is_cctx_allowed(cm, xd)) { |
498 | 155k | const int skip_cctx = is_inter ? 0 : (*eob == 1); |
499 | 155k | if (!all_zero && !skip_cctx) { |
500 | 74.3k | av2_read_cctx_type(cm, xd, blk_row, blk_col, tx_size, r); |
501 | 81.5k | } else { |
502 | 81.5k | int row_offset, col_offset; |
503 | 81.5k | get_chroma_mi_offsets(xd, &row_offset, &col_offset); |
504 | 81.5k | update_cctx_array(xd, blk_row, blk_col, row_offset, col_offset, tx_size, |
505 | 81.5k | CCTX_NONE); |
506 | 81.5k | } |
507 | 155k | } |
508 | 3.23M | return 1; |
509 | 7.83M | } |
510 | | |
511 | | uint8_t av2_read_coeffs_txb_skip(const AV2_COMMON *const cm, |
512 | | DecoderCodingBlock *dcb, avm_reader *const r, |
513 | | const int blk_row, const int blk_col, |
514 | 30.4k | const int plane, const TX_SIZE tx_size) { |
515 | 30.4k | MACROBLOCKD *const xd = &dcb->xd; |
516 | 30.4k | MB_MODE_INFO *const mbmi = xd->mi[0]; |
517 | 30.4k | FRAME_CONTEXT *const ec_ctx = xd->tile_ctx; |
518 | 30.4k | struct macroblockd_plane *const pd = &xd->plane[plane]; |
519 | 30.4k | const PLANE_TYPE plane_type = get_plane_type(plane); |
520 | | |
521 | 30.4k | const int32_t max_value = (1 << (7 + xd->bd)) - 1; |
522 | 30.4k | const int32_t min_value = -(1 << (7 + xd->bd)); |
523 | | |
524 | 30.4k | const int32_t *const dequant = pd->seg_dequant_QTX[mbmi->segment_id]; |
525 | 30.4k | tran_low_t *const tcoeffs = dcb->dqcoeff_block[plane] + dcb->cb_offset[plane]; |
526 | 30.4k | const int shift = av2_get_tx_scale(tx_size); |
527 | 30.4k | const int bwl = get_txb_bwl(tx_size); |
528 | 30.4k | const int width = get_txb_wide(tx_size); |
529 | 30.4k | const int height = get_txb_high(tx_size); |
530 | | #if CONFIG_INSPECTION |
531 | | tran_low_t *const tcoeffs_copy = |
532 | | dcb->dqcoeff_block_copy[plane] + dcb->cb_offset[plane]; |
533 | | tran_low_t *const quant_coeffs = |
534 | | dcb->qcoeff_block[plane] + dcb->cb_offset[plane]; |
535 | | tran_low_t *const dequant_values = |
536 | | dcb->dequant_values[plane] + dcb->cb_offset[plane]; |
537 | | // For TX sizes > 32x32, all coeffs are zero except for top-left 32x32. |
538 | | const int coeff_width = AVMMIN(width, 32); |
539 | | const int coeff_height = AVMMIN(height, 32); |
540 | | memset(tcoeffs_copy, 0, sizeof(tran_low_t) * coeff_width * coeff_height); |
541 | | memset(quant_coeffs, 0, sizeof(tran_low_t) * coeff_width * coeff_height); |
542 | | memset(dequant_values, 0, sizeof(tran_low_t) * coeff_width * coeff_height); |
543 | | #endif // CONFIG_INSPECTION |
544 | 30.4k | int cul_level = 0; |
545 | 30.4k | int dc_val = 0; |
546 | 30.4k | uint8_t levels_buf[TX_PAD_2D]; |
547 | 30.4k | uint8_t *const levels = set_levels(levels_buf, width); |
548 | 30.4k | int8_t signs_buf[TX_PAD_2D]; |
549 | 30.4k | int8_t *const signs = set_signs(signs_buf, width); |
550 | 30.4k | eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane]; |
551 | 30.4k | eob_data->max_scan_line = 0; |
552 | 30.4k | eob_data->eob = av2_get_max_eob(tx_size); |
553 | 30.4k | eob_info *bob_data = dcb->bob_data[plane] + dcb->txb_offset[plane]; |
554 | 30.4k | bob_data->max_scan_line = 0; |
555 | | |
556 | 30.4k | const TX_TYPE tx_type = |
557 | 30.4k | av2_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size, |
558 | 30.4k | is_reduced_tx_set_used(cm, plane_type)); |
559 | 30.4k | const qm_val_t *iqmatrix = |
560 | 30.4k | av2_get_iqmatrix(&cm->quant_params, xd, plane, tx_size, tx_type); |
561 | | #if CONFIG_INSPECTION |
562 | | for (int c = 0; c < width * height; c++) { |
563 | | dequant_values[c] = get_dqv(dequant, c, iqmatrix); |
564 | | } |
565 | | #endif // CONFIG_INSPECTION |
566 | 30.4k | const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
567 | 30.4k | const int16_t *const scan = scan_order->scan; |
568 | 30.4k | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
569 | 30.4k | const int size_ctx = AVMMIN(txs_ctx, TX_16X16); |
570 | 30.4k | if (eob_data->eob > 1) { |
571 | 30.4k | memset(levels_buf, 0, sizeof(*levels_buf) * TX_PAD_2D); |
572 | 30.4k | memset(signs_buf, 0, sizeof(*signs_buf) * TX_PAD_2D); |
573 | 30.4k | base_fsc_cdf_arr base_cdf = ec_ctx->coeff_base_cdf_idtx[size_ctx]; |
574 | 30.4k | br_cdf_arr br_cdf = ec_ctx->coeff_br_cdf_idtx[size_ctx]; |
575 | 30.4k | const int bob = av2_get_max_eob(tx_size) - bob_data->eob; |
576 | 30.4k | { |
577 | 30.4k | const int pos = scan[bob]; |
578 | 30.4k | const int coeff_ctx_bob = get_lower_levels_ctx_bob(bwl, height, bob); |
579 | 30.4k | const int nsymbs_bob = 3; |
580 | 30.4k | avm_cdf_prob *cdf_bob = |
581 | 30.4k | ec_ctx->coeff_base_bob_cdf[size_ctx][coeff_ctx_bob]; |
582 | 30.4k | int level = avm_read_symbol(r, cdf_bob, nsymbs_bob, |
583 | 30.4k | ACCT_INFO("level", "cdf_bob")) + |
584 | 30.4k | 1; |
585 | 30.4k | if (level > NUM_BASE_LEVELS) { |
586 | 7.68k | const int br_ctx = get_br_ctx_skip(levels, pos, bwl); |
587 | 7.68k | avm_cdf_prob *cdf = br_cdf[br_ctx]; |
588 | 7.68k | level += read_low_range(r, cdf); |
589 | 7.68k | } |
590 | 30.4k | levels[get_padded_idx_left(pos, bwl)] = level; |
591 | 30.4k | } |
592 | 30.4k | read_coeffs_forward_2d(r, bob + 1, eob_data->eob - 1, scan, bwl, levels, |
593 | 30.4k | base_cdf, br_cdf); |
594 | 30.4k | } |
595 | | |
596 | 30.4k | const int bob = av2_get_max_eob(tx_size) - bob_data->eob; |
597 | 30.4k | int hr_level_avg = 0; |
598 | 537k | for (int c = bob; c < eob_data->eob; c++) { |
599 | 506k | const int pos = scan[c]; |
600 | 506k | const int sign_idx = get_padded_idx_left(pos, bwl); |
601 | 506k | uint8_t sign; |
602 | 506k | tran_low_t level = levels[get_padded_idx_left(pos, bwl)]; |
603 | 506k | if (level) { |
604 | 115k | eob_data->max_scan_line = AVMMAX(eob_data->max_scan_line, pos); |
605 | 115k | int idtx_sign_ctx = get_sign_ctx_skip(signs, levels, pos, bwl); |
606 | 115k | sign = avm_read_symbol(r, ec_ctx->idtx_sign_cdf[size_ctx][idtx_sign_ctx], |
607 | 115k | 2, ACCT_INFO("sign")); |
608 | 115k | signs[sign_idx] = sign > 0 ? -1 : 1; |
609 | 115k | level = read_high_range(xd, r, 0, level, 0, &hr_level_avg, plane); |
610 | 115k | if (c == 0) dc_val = sign ? -level : level; |
611 | | // Bitmasking to clamp level to valid range: |
612 | | // The valid range for 8/10/12 bit video is at most 14/16/18 bit |
613 | 115k | if ((level >> 20) != 0) { |
614 | 4 | avm_internal_error(xd->error_info, AVM_CODEC_ERROR, |
615 | 4 | "Invalid FSC coeff level beyond 20 bits"); |
616 | 4 | } |
617 | 115k | cul_level += level; |
618 | 115k | tran_low_t dq_coeff; |
619 | | // Bitmasking to clamp dq_coeff to valid range: |
620 | | // The valid range for 8/10/12 bit video is at most 17/19/21 bits |
621 | 115k | const int64_t dq_coeff_hp = |
622 | 115k | (int64_t)level * get_dqv(dequant, scan[c], iqmatrix) & 0xffffff; |
623 | 115k | dq_coeff = |
624 | 115k | (tran_low_t)(ROUND_POWER_OF_TWO_64(dq_coeff_hp, QUANT_TABLE_BITS)); |
625 | 115k | dq_coeff = dq_coeff >> shift; |
626 | 115k | if (sign) { |
627 | 57.1k | dq_coeff = -dq_coeff; |
628 | 57.1k | } |
629 | 115k | tcoeffs[pos] = clamp(dq_coeff, min_value, max_value); |
630 | | #if CONFIG_INSPECTION |
631 | | tcoeffs_copy[pos] = tcoeffs[pos]; |
632 | | quant_coeffs[pos] = sign ? -level : level; |
633 | | #endif // CONFIG_INSPECTION |
634 | 115k | } |
635 | 506k | } |
636 | 30.4k | cul_level = AVMMIN(COEFF_CONTEXT_MASK, cul_level); |
637 | 30.4k | set_dc_sign(&cul_level, dc_val); |
638 | 30.4k | return cul_level; |
639 | 30.4k | } |
640 | | |
641 | | // This function returns the partial absolute level of the coefficient |
642 | | // with hidden parity. |
643 | | static INLINE tran_low_t read_coeff_hidden(avm_reader *r, TX_CLASS tx_class, |
644 | | const int16_t *scan, int bwl, |
645 | | uint8_t *levels, int parity, |
646 | 61.1k | base_ph_cdf_arr base_cdf_ph) { |
647 | 61.1k | int q_index; |
648 | 61.1k | const int pos = scan[0]; |
649 | 61.1k | int ctx_idx = get_base_ctx_ph(levels, pos, bwl, tx_class); |
650 | 61.1k | q_index = avm_read_symbol(r, base_cdf_ph[ctx_idx], 4, ACCT_INFO("q_index")); |
651 | | |
652 | 61.1k | assert(q_index <= MAX_BASE_BR_RANGE); |
653 | 61.1k | uint8_t level = (q_index << 1) + parity; |
654 | 61.1k | levels[get_padded_idx(pos, bwl)] = level; |
655 | 61.1k | return level; |
656 | 61.1k | } |
657 | | |
658 | | uint8_t av2_read_coeffs_txb(const AV2_COMMON *const cm, DecoderCodingBlock *dcb, |
659 | | avm_reader *const r, const int blk_row, |
660 | | const int blk_col, const int plane, |
661 | | const TXB_CTX *const txb_ctx, |
662 | 3.02M | const TX_SIZE tx_size) { |
663 | 3.02M | MACROBLOCKD *const xd = &dcb->xd; |
664 | 3.02M | FRAME_CONTEXT *const ec_ctx = xd->tile_ctx; |
665 | 3.02M | const int32_t max_value = (1 << (7 + xd->bd)) - 1; |
666 | 3.02M | const int32_t min_value = -(1 << (7 + xd->bd)); |
667 | 3.02M | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
668 | 3.02M | const PLANE_TYPE plane_type = get_plane_type(plane); |
669 | 3.02M | MB_MODE_INFO *const mbmi = xd->mi[0]; |
670 | 3.02M | struct macroblockd_plane *const pd = &xd->plane[plane]; |
671 | 3.02M | const int32_t *const dequant = pd->seg_dequant_QTX[mbmi->segment_id]; |
672 | 3.02M | tran_low_t *const tcoeffs = dcb->dqcoeff_block[plane] + dcb->cb_offset[plane]; |
673 | 3.02M | const int shift = av2_get_tx_scale(tx_size); |
674 | 3.02M | const int bwl = get_txb_bwl(tx_size); |
675 | 3.02M | const int width = get_txb_wide(tx_size); |
676 | 3.02M | const int height = get_txb_high(tx_size); |
677 | | #if CONFIG_INSPECTION |
678 | | tran_low_t *const tcoeffs_copy = |
679 | | dcb->dqcoeff_block_copy[plane] + dcb->cb_offset[plane]; |
680 | | tran_low_t *const quant_coeffs = |
681 | | dcb->qcoeff_block[plane] + dcb->cb_offset[plane]; |
682 | | tran_low_t *const dequant_values = |
683 | | dcb->dequant_values[plane] + dcb->cb_offset[plane]; |
684 | | // For TX sizes > 32x32, all coeffs are zero except for top-left 32x32. |
685 | | const int coeff_width = AVMMIN(width, 32); |
686 | | const int coeff_height = AVMMIN(height, 32); |
687 | | memset(tcoeffs_copy, 0, sizeof(tran_low_t) * coeff_width * coeff_height); |
688 | | memset(quant_coeffs, 0, sizeof(tran_low_t) * coeff_width * coeff_height); |
689 | | memset(dequant_values, 0, sizeof(tran_low_t) * coeff_width * coeff_height); |
690 | | #endif // CONFIG_INSPECTION |
691 | 3.02M | int cul_level = 0; |
692 | 3.02M | int dc_val = 0; |
693 | 3.02M | uint8_t levels_buf[TX_PAD_2D]; |
694 | 3.02M | uint8_t *const levels = set_levels(levels_buf, width); |
695 | 3.02M | eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane]; |
696 | 3.02M | uint16_t *const eob = &(eob_data->eob); |
697 | 3.02M | uint16_t *const max_scan_line = &(eob_data->max_scan_line); |
698 | | |
699 | 3.02M | const TX_TYPE tx_type = |
700 | 3.02M | av2_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size, |
701 | 3.02M | is_reduced_tx_set_used(cm, plane_type)); |
702 | 3.02M | const TX_CLASS tx_class = tx_type_to_class[get_primary_tx_type(tx_type)]; |
703 | 3.02M | const qm_val_t *iqmatrix = |
704 | 3.02M | av2_get_iqmatrix(&cm->quant_params, xd, plane, tx_size, tx_type); |
705 | | #if CONFIG_INSPECTION |
706 | | for (int c = 0; c < width * height; c++) { |
707 | | dequant_values[c] = get_dqv(dequant, c, iqmatrix); |
708 | | } |
709 | | #endif // CONFIG_INSPECTION |
710 | 3.02M | const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
711 | 3.02M | const int16_t *const scan = scan_order->scan; |
712 | | |
713 | | // read sec_tx_type here |
714 | | // Only y plane's sec_tx_type is transmitted |
715 | 3.02M | if ((plane == AVM_PLANE_Y) && |
716 | 2.18M | (is_inter_block(mbmi, xd->tree_type) |
717 | 2.18M | ? (*eob > 3 && cm->seq_params.enable_inter_ist) |
718 | 2.18M | : (*eob != 1 && cm->seq_params.enable_ist && |
719 | 585k | !xd->mi[0]->fsc_mode[xd->tree_type == CHROMA_PART]))) { |
720 | 585k | av2_read_sec_tx_type(cm, xd, blk_row, blk_col, tx_size, eob, r); |
721 | 585k | } |
722 | | |
723 | 3.02M | if (*eob > 1) { |
724 | 1.10M | memset(levels_buf, 0, sizeof(*levels_buf) * TX_PAD_2D); |
725 | 1.10M | } |
726 | | |
727 | 3.02M | int tcq_mode = tcq_enable(cm->features.tcq_mode, |
728 | 3.02M | xd->lossless[mbmi->segment_id], plane, tx_class); |
729 | 3.02M | int state = tcq_init_state(tcq_mode); |
730 | | |
731 | 3.02M | { |
732 | | // Read the non-zero coefficient with scan index eob-1 |
733 | | // TODO(angiebird): Put this into a function |
734 | 3.02M | const int c = *eob - 1; |
735 | 3.02M | const int pos = scan[c]; |
736 | 3.02M | const int coeff_ctx = get_lower_levels_ctx_eob(bwl, height, c); |
737 | | |
738 | 3.02M | int level = 0; |
739 | 3.02M | const int row = pos >> bwl; |
740 | 3.02M | const int col = pos - (row << bwl); |
741 | 3.02M | int limits = get_lf_limits(row, col, tx_class, plane); |
742 | 3.02M | if (plane > 0) { |
743 | 900k | if (limits) { |
744 | 660k | avm_cdf_prob *cdf = ec_ctx->coeff_base_lf_eob_uv_cdf[coeff_ctx]; |
745 | 660k | level += |
746 | 660k | avm_read_symbol(r, cdf, LF_BASE_SYMBOLS - 1, |
747 | 660k | ACCT_INFO("level", "coeff_base_lf_eob_uv_cdf")) + |
748 | 660k | 1; |
749 | 660k | } else { |
750 | 240k | avm_cdf_prob *cdf = ec_ctx->coeff_base_eob_uv_cdf[coeff_ctx]; |
751 | 240k | level += avm_read_symbol(r, cdf, 3, |
752 | 240k | ACCT_INFO("level", "coeff_base_eob_uv_cdf")) + |
753 | 240k | 1; |
754 | | |
755 | 240k | if (level > NUM_BASE_LEVELS) { |
756 | 3.61k | const int br_ctx = 0; /* get_lf_ctx_eob */ |
757 | 3.61k | cdf = ec_ctx->coeff_br_uv_cdf[br_ctx]; |
758 | 3.61k | level += read_low_range(r, cdf); |
759 | 3.61k | } |
760 | 240k | } |
761 | 2.12M | } else { |
762 | 2.12M | if (limits) { |
763 | 1.80M | avm_cdf_prob *cdf = ec_ctx->coeff_base_lf_eob_cdf[txs_ctx][coeff_ctx]; |
764 | 1.80M | level += avm_read_symbol(r, cdf, LF_BASE_SYMBOLS - 1, |
765 | 1.80M | ACCT_INFO("level", "coeff_base_lf_eob_cdf")) + |
766 | 1.80M | 1; |
767 | | |
768 | 1.80M | if (level > LF_NUM_BASE_LEVELS) { |
769 | 13.8k | const int br_ctx = get_br_ctx_lf_eob(pos, tx_class); |
770 | 13.8k | cdf = ec_ctx->coeff_br_lf_cdf[br_ctx]; |
771 | 13.8k | level += read_low_range(r, cdf); |
772 | 13.8k | } |
773 | 1.80M | } else { |
774 | 320k | avm_cdf_prob *cdf = ec_ctx->coeff_base_eob_cdf[txs_ctx][coeff_ctx]; |
775 | 320k | level += avm_read_symbol(r, cdf, 3, |
776 | 320k | ACCT_INFO("level", "coeff_base_eob_cdf")) + |
777 | 320k | 1; |
778 | | |
779 | 320k | if (level > NUM_BASE_LEVELS) { |
780 | 6.34k | const int br_ctx = 0; /* get_lf_ctx_eob */ |
781 | 6.34k | cdf = ec_ctx->coeff_br_cdf[br_ctx]; |
782 | 6.34k | level += read_low_range(r, cdf); |
783 | 6.34k | } |
784 | 320k | } |
785 | 2.12M | } |
786 | 3.02M | levels[get_padded_idx(pos, bwl)] = level; |
787 | 3.02M | state = tcq_next_state(state, level); |
788 | 3.02M | } |
789 | | |
790 | 3.02M | bool enable_parity_hiding = |
791 | 3.02M | cm->features.allow_parity_hiding && !xd->lossless[mbmi->segment_id] && |
792 | 214k | plane == PLANE_TYPE_Y && |
793 | 105k | ph_allowed_tx_types[get_primary_tx_type(tx_type)] && (*eob > PHTHRESH); |
794 | 3.02M | int num_nz = 0, sum_abs1 = 0; |
795 | 3.02M | bool is_hidden = false; |
796 | 3.02M | if (*eob > 1) { |
797 | 1.11M | base_lf_cdf_arr base_lf_cdf = ec_ctx->coeff_base_lf_cdf[txs_ctx]; |
798 | 1.11M | br_cdf_arr br_lf_cdf = ec_ctx->coeff_br_lf_cdf; |
799 | 1.11M | base_cdf_arr base_cdf = ec_ctx->coeff_base_cdf[txs_ctx]; |
800 | 1.11M | br_cdf_arr br_cdf = ec_ctx->coeff_br_cdf; |
801 | 1.11M | base_lf_uv_cdf_arr base_lf_uv_cdf = ec_ctx->coeff_base_lf_uv_cdf; |
802 | 1.11M | base_uv_cdf_arr base_uv_cdf = ec_ctx->coeff_base_uv_cdf; |
803 | 1.11M | br_cdf_arr br_uv_cdf = ec_ctx->coeff_br_uv_cdf; |
804 | | |
805 | 1.11M | if (tx_class == TX_CLASS_2D) { |
806 | 1.09M | read_coeffs_reverse_2d(r, 1, *eob - 2, scan, bwl, levels, base_lf_cdf, |
807 | 1.09M | br_lf_cdf, plane, base_cdf, br_cdf, base_lf_uv_cdf, |
808 | 1.09M | base_uv_cdf, br_uv_cdf, &state); |
809 | 1.09M | if (enable_parity_hiding) { |
810 | 3.02M | for (int si = *eob - 1; si > 0; --si) { |
811 | 2.94M | int pos = scan[si]; |
812 | 2.94M | int level = |
813 | 2.94M | AVMMIN(levels[get_padded_idx(pos, bwl)], MAX_BASE_BR_RANGE); |
814 | 2.94M | if (level) { |
815 | 831k | ++num_nz; |
816 | 831k | sum_abs1 += level; |
817 | 831k | } |
818 | 2.94M | } |
819 | 74.8k | is_hidden = num_nz >= PHTHRESH; |
820 | 74.8k | } |
821 | 1.09M | if (is_hidden) { |
822 | 59.9k | read_coeff_hidden(r, tx_class, scan, bwl, levels, (sum_abs1 & 1), |
823 | 59.9k | ec_ctx->coeff_base_ph_cdf); |
824 | 1.03M | } else { |
825 | 1.03M | read_coeffs_reverse(r, tx_class, 0, 0, scan, bwl, levels, base_lf_cdf, |
826 | 1.03M | br_lf_cdf, plane, base_cdf, br_cdf, base_lf_uv_cdf, |
827 | 1.03M | base_uv_cdf, br_uv_cdf, &state); |
828 | 1.03M | } |
829 | 1.09M | } else { |
830 | 12.4k | read_coeffs_reverse(r, tx_class, 1, *eob - 2, scan, bwl, levels, |
831 | 12.4k | base_lf_cdf, br_lf_cdf, plane, base_cdf, br_cdf, |
832 | 12.4k | base_lf_uv_cdf, base_uv_cdf, br_uv_cdf, &state); |
833 | 12.4k | if (enable_parity_hiding) { |
834 | 46.1k | for (int si = *eob - 1; si > 0; --si) { |
835 | 44.4k | int pos = scan[si]; |
836 | 44.4k | int level = |
837 | 44.4k | AVMMIN(levels[get_padded_idx(pos, bwl)], MAX_BASE_BR_RANGE); |
838 | 44.4k | if (level) { |
839 | 14.8k | ++num_nz; |
840 | 14.8k | sum_abs1 += level; |
841 | 14.8k | } |
842 | 44.4k | } |
843 | 1.69k | is_hidden = num_nz >= PHTHRESH; |
844 | 1.69k | } |
845 | 12.4k | if (is_hidden) { |
846 | 1.14k | read_coeff_hidden(r, tx_class, scan, bwl, levels, (sum_abs1 & 1), |
847 | 1.14k | ec_ctx->coeff_base_ph_cdf); |
848 | 11.2k | } else { |
849 | 11.2k | read_coeffs_reverse(r, tx_class, 0, 0, scan, bwl, levels, base_lf_cdf, |
850 | 11.2k | br_lf_cdf, plane, base_cdf, br_cdf, base_lf_uv_cdf, |
851 | 11.2k | base_uv_cdf, br_uv_cdf, &state); |
852 | 11.2k | } |
853 | 12.4k | } |
854 | 1.11M | } |
855 | | |
856 | 3.02M | int hr_level_avg = 0; |
857 | 23.8M | for (int c = *eob - 1; c >= 0; --c) { |
858 | 20.8M | const int pos = scan[c]; |
859 | 20.8M | uint8_t sign; |
860 | 20.8M | tran_low_t level = levels[get_padded_idx(pos, bwl)]; |
861 | 20.8M | const int tmp_sign_idx = pos; |
862 | 20.8M | if (plane == AVM_PLANE_U) { |
863 | 1.87M | xd->tmp_sign[pos] = 0; |
864 | 1.87M | } |
865 | 20.8M | if (level) { |
866 | 12.2M | *max_scan_line = AVMMAX(*max_scan_line, pos); |
867 | 12.2M | const int row = pos >> bwl; |
868 | 12.2M | const int col = pos - (row << bwl); |
869 | 12.2M | const bool dc_2dtx = (c == 0); |
870 | 12.2M | const bool dc_hor = (col == 0) && tx_class == TX_CLASS_HORIZ; |
871 | 12.2M | const bool dc_ver = (row == 0) && tx_class == TX_CLASS_VERT; |
872 | 12.2M | if (dc_2dtx || dc_hor || dc_ver) { |
873 | 2.98M | const int dc_sign_ctx = dc_2dtx ? txb_ctx->dc_sign_ctx : 0; |
874 | 2.98M | if (plane == AVM_PLANE_Y || plane == AVM_PLANE_U) { |
875 | 2.59M | if (plane == AVM_PLANE_Y) { |
876 | 2.15M | sign = avm_read_symbol( |
877 | 2.15M | r, |
878 | 2.15M | ec_ctx->dc_sign_cdf[plane_type][is_hidden ? 1 : 0][dc_sign_ctx], |
879 | 2.15M | 2, ACCT_INFO("sign", "dc_sign_cdf", "plane_y_or_u")); |
880 | 2.15M | } else { |
881 | 447k | sign = avm_read_literal( |
882 | 447k | r, 1, ACCT_INFO("sign", "dc_sign_cdf", "plane_y_or_u")); |
883 | 447k | } |
884 | 2.59M | } else { |
885 | 381k | sign = avm_read_literal( |
886 | 381k | r, 1, ACCT_INFO("sign", "v_dc_sign_cdf", "plane_v")); |
887 | 381k | } |
888 | 2.98M | if (plane == AVM_PLANE_U) xd->tmp_sign[tmp_sign_idx] = (sign ? 2 : 1); |
889 | 9.23M | } else { |
890 | 9.23M | sign = avm_read_bit(r, ACCT_INFO("sign")); |
891 | 9.23M | } |
892 | 12.2M | if (is_hidden && c == 0) { |
893 | 53.0k | if (level >= ((NUM_BASE_LEVELS + 1) << 1)) { |
894 | 14.8k | int hr_level = (read_adaptive_hr(xd, r, hr_level_avg >> 1) << 1); |
895 | 14.8k | level += hr_level; |
896 | 14.8k | hr_level_avg = (hr_level_avg + hr_level) >> 1; |
897 | 14.8k | } |
898 | 12.1M | } else { |
899 | 12.1M | int limits = get_lf_limits(row, col, tx_class, plane); |
900 | 12.1M | level = read_high_range(xd, r, tcq_mode, level, limits, &hr_level_avg, |
901 | 12.1M | plane); |
902 | 12.1M | } |
903 | 12.2M | if (c == 0) dc_val = sign ? -level : level; |
904 | | |
905 | | // Bitmasking to clamp level to valid range: |
906 | | // The valid range for 8/10/12 bit vdieo is at most 14/16/18 bit |
907 | 12.2M | if ((level >> 20) != 0) { |
908 | 15 | avm_internal_error(xd->error_info, AVM_CODEC_ERROR, |
909 | 15 | "Invalid coeff level beyond 20 bits"); |
910 | 15 | } |
911 | 12.2M | cul_level += level; |
912 | 12.2M | if (!xd->lossless[mbmi->segment_id] && tcq_mode) { |
913 | 6.66M | tcoeffs[pos] = sign ? -level : level; |
914 | 6.66M | } else { |
915 | 5.55M | tran_low_t dq_coeff; |
916 | | // Bitmasking to clamp dq_coeff to valid range: |
917 | | // The valid range for 8/10/12 bit video is at most 17/19/21 bit |
918 | 5.55M | const int64_t dq_coeff_hp = |
919 | 5.55M | (int64_t)level * get_dqv(dequant, scan[c], iqmatrix) & 0xffffff; |
920 | 5.55M | dq_coeff = |
921 | 5.55M | (tran_low_t)(ROUND_POWER_OF_TWO_64(dq_coeff_hp, QUANT_TABLE_BITS)); |
922 | 5.55M | dq_coeff = dq_coeff >> shift; |
923 | 5.55M | if (sign) { |
924 | 2.13M | dq_coeff = -dq_coeff; |
925 | 2.13M | } |
926 | 5.55M | tcoeffs[pos] = clamp(dq_coeff, min_value, max_value); |
927 | | #if CONFIG_INSPECTION |
928 | | tcoeffs_copy[pos] = tcoeffs[pos]; |
929 | | quant_coeffs[pos] = sign ? -level : level; |
930 | | #endif // CONFIG_INSPECTION |
931 | 5.55M | } |
932 | 12.2M | } |
933 | 20.8M | } |
934 | | |
935 | 3.02M | if (!xd->lossless[mbmi->segment_id] && tcq_mode) { |
936 | 681k | state = tcq_init_state(tcq_mode); |
937 | 11.1M | for (int c = *eob - 1; c >= 0; --c) { |
938 | 10.4M | const int pos = scan[c]; |
939 | 10.4M | int Qx = tcq_quant(state); |
940 | 10.4M | int level = abs(tcoeffs[pos]); |
941 | | |
942 | 10.4M | if (abs(tcoeffs[pos])) { |
943 | 6.63M | tran_low_t dq_coeff; |
944 | 6.63M | tran_low_t qIdx = AVMMAX(0, (abs(tcoeffs[pos]) << 1) - Qx); |
945 | 6.63M | if ((qIdx >> 20) != 0) { |
946 | 8 | avm_internal_error(xd->error_info, AVM_CODEC_ERROR, |
947 | 8 | "Invalid TCQ coeff level beyond 20 bits"); |
948 | 8 | } |
949 | | // Bitmasking to clamp dq_coeff to valid range: |
950 | | // The valid range for 8/10/12 bit video is at most 17/19/21 bit |
951 | 6.63M | int tempdqv = get_dqv(dequant, scan[c], iqmatrix); |
952 | 6.63M | const int64_t dq_coeff_hp = (int64_t)qIdx * tempdqv & 0xffffff; |
953 | 6.63M | dq_coeff = |
954 | 6.63M | (tran_low_t)(ROUND_POWER_OF_TWO_64(dq_coeff_hp, QUANT_TABLE_BITS)); |
955 | 6.63M | dq_coeff = dq_coeff >> (shift + 1); |
956 | | // dq_coeff = dq_coeff >> (shift); |
957 | | // dq_coeff = (tran_low_t)ROUND_POWER_OF_TWO_64((tran_high_t)qIdx * |
958 | | // get_dqv(dequant, scan[c], iqmatrix), QUANT_TABLE_BITS) >> (shift + |
959 | | // 1); |
960 | 6.63M | if (tcoeffs[pos] < 0) { |
961 | 3.26M | dq_coeff = -dq_coeff; |
962 | 3.26M | } |
963 | | |
964 | 6.63M | tcoeffs[pos] = clamp(dq_coeff, min_value, max_value); |
965 | 6.63M | } |
966 | | // state transition |
967 | 10.4M | state = tcq_next_state(state, level); |
968 | 10.4M | } |
969 | 681k | } |
970 | | |
971 | 3.02M | cul_level = AVMMIN(COEFF_CONTEXT_MASK, cul_level); |
972 | | |
973 | | // DC value |
974 | 3.02M | set_dc_sign(&cul_level, dc_val); |
975 | | |
976 | 3.02M | return cul_level; |
977 | 3.02M | } |
978 | | |
979 | | void av2_read_coeffs_txb_facade(const AV2_COMMON *const cm, |
980 | | DecoderCodingBlock *dcb, avm_reader *const r, |
981 | | const int plane, const int row, const int col, |
982 | 7.78M | const TX_SIZE tx_size) { |
983 | | #if TXCOEFF_TIMER |
984 | | struct avm_usec_timer timer; |
985 | | avm_usec_timer_start(&timer); |
986 | | #endif |
987 | 7.78M | MACROBLOCKD *const xd = &dcb->xd; |
988 | 7.78M | MB_MODE_INFO *const mbmi = xd->mi[0]; |
989 | 7.78M | struct macroblockd_plane *const pd = &xd->plane[plane]; |
990 | 7.78M | const BLOCK_SIZE plane_bsize = get_mb_plane_block_size( |
991 | 7.78M | xd, mbmi, plane, pd->subsampling_x, pd->subsampling_y); |
992 | | |
993 | 7.78M | TXB_CTX txb_ctx; |
994 | 7.78M | get_txb_ctx(plane_bsize, tx_size, plane, pd->above_entropy_context + col, |
995 | 7.78M | pd->left_entropy_context + row, &txb_ctx, |
996 | 7.78M | mbmi->fsc_mode[xd->tree_type == CHROMA_PART] && |
997 | 49.9k | cm->seq_params.enable_fsc); |
998 | | |
999 | 7.78M | const uint8_t decode_rest = |
1000 | 7.78M | av2_read_sig_txtype(cm, dcb, r, row, col, plane, &txb_ctx, tx_size); |
1001 | | |
1002 | 7.78M | const PLANE_TYPE plane_type = get_plane_type(plane); |
1003 | 7.78M | const TX_TYPE tx_type = |
1004 | 7.78M | av2_get_tx_type(xd, plane_type, row, col, tx_size, |
1005 | 7.78M | is_reduced_tx_set_used(cm, plane_type)); |
1006 | 7.78M | const int is_inter = is_inter_block(mbmi, xd->tree_type); |
1007 | 7.78M | uint8_t cul_level = 0; |
1008 | 7.78M | if (decode_rest) { |
1009 | 3.05M | if (((cm->seq_params.enable_fsc && |
1010 | 1.68M | mbmi->fsc_mode[xd->tree_type == CHROMA_PART] && |
1011 | 26.3k | get_primary_tx_type(tx_type) == IDTX && plane == PLANE_TYPE_Y) || |
1012 | 3.03M | use_inter_fsc(cm, plane, tx_type, is_inter))) { |
1013 | 30.4k | cul_level = |
1014 | 30.4k | av2_read_coeffs_txb_skip(cm, dcb, r, row, col, plane, tx_size); |
1015 | 3.02M | } else { |
1016 | 3.02M | cul_level = |
1017 | 3.02M | av2_read_coeffs_txb(cm, dcb, r, row, col, plane, &txb_ctx, tx_size); |
1018 | 3.02M | } |
1019 | 4.72M | } else { |
1020 | 4.72M | av2_update_txk_skip_array(cm, xd->mi_row, xd->mi_col, xd->tree_type, |
1021 | 4.72M | &mbmi->chroma_ref_info, plane, row, col, tx_size); |
1022 | 4.72M | } |
1023 | 7.78M | av2_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level, col, |
1024 | 7.78M | row); |
1025 | 7.78M | if (is_inter_block(mbmi, xd->tree_type) && (plane == 0)) { |
1026 | 31.0k | const TX_TYPE tx_type_inter = |
1027 | 31.0k | av2_get_tx_type(xd, plane_type, row, col, tx_size, |
1028 | 31.0k | is_reduced_tx_set_used(cm, plane_type)); |
1029 | 31.0k | update_txk_array(xd, row, col, tx_size, tx_type_inter); |
1030 | 31.0k | } |
1031 | | |
1032 | | #if TXCOEFF_TIMER |
1033 | | avm_usec_timer_mark(&timer); |
1034 | | const int64_t elapsed_time = avm_usec_timer_elapsed(&timer); |
1035 | | cm->txcoeff_timer += elapsed_time; |
1036 | | ++cm->txb_count; |
1037 | | #endif |
1038 | 7.78M | } |