/src/libavif/ext/aom/av1/encoder/txb_rdopt.c
Line | Count | Source (jump to first uncovered line) |
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 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/txb_rdopt.h" |
13 | | #include "av1/encoder/txb_rdopt_utils.h" |
14 | | |
15 | | #include "aom_ports/mem.h" |
16 | | #include "av1/common/idct.h" |
17 | | |
18 | | static inline void update_coeff_general( |
19 | | int *accu_rate, int64_t *accu_dist, int si, int eob, TX_SIZE tx_size, |
20 | | TX_CLASS tx_class, int bhl, int width, int64_t rdmult, int shift, |
21 | | int dc_sign_ctx, const int16_t *dequant, const int16_t *scan, |
22 | | const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff, |
23 | | tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels, |
24 | 208M | const qm_val_t *iqmatrix, const qm_val_t *qmatrix) { |
25 | 208M | const int dqv = get_dqv(dequant, scan[si], iqmatrix); |
26 | 208M | const int ci = scan[si]; |
27 | 208M | const tran_low_t qc = qcoeff[ci]; |
28 | 208M | const int is_last = si == (eob - 1); |
29 | 208M | const int coeff_ctx = get_lower_levels_ctx_general( |
30 | 208M | is_last, si, bhl, width, levels, ci, tx_size, tx_class); |
31 | 208M | if (qc == 0) { |
32 | 8.52M | *accu_rate += txb_costs->base_cost[coeff_ctx][0]; |
33 | 199M | } else { |
34 | 199M | const int sign = (qc < 0) ? 1 : 0; |
35 | 199M | const tran_low_t abs_qc = abs(qc); |
36 | 199M | const tran_low_t tqc = tcoeff[ci]; |
37 | 199M | const tran_low_t dqc = dqcoeff[ci]; |
38 | 199M | const int64_t dist = get_coeff_dist(tqc, dqc, shift, qmatrix, ci); |
39 | 199M | const int64_t dist0 = get_coeff_dist(tqc, 0, shift, qmatrix, ci); |
40 | 199M | const int rate = |
41 | 199M | get_coeff_cost_general(is_last, ci, abs_qc, sign, coeff_ctx, |
42 | 199M | dc_sign_ctx, txb_costs, bhl, tx_class, levels); |
43 | 199M | const int64_t rd = RDCOST(rdmult, rate, dist); |
44 | | |
45 | 199M | tran_low_t qc_low, dqc_low; |
46 | 199M | tran_low_t abs_qc_low; |
47 | 199M | int64_t dist_low, rd_low; |
48 | 199M | int rate_low; |
49 | 199M | if (abs_qc == 1) { |
50 | 13.8M | abs_qc_low = qc_low = dqc_low = 0; |
51 | 13.8M | dist_low = dist0; |
52 | 13.8M | rate_low = txb_costs->base_cost[coeff_ctx][0]; |
53 | 185M | } else { |
54 | 185M | get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low); |
55 | 185M | abs_qc_low = abs_qc - 1; |
56 | 185M | dist_low = get_coeff_dist(tqc, dqc_low, shift, qmatrix, ci); |
57 | 185M | rate_low = |
58 | 185M | get_coeff_cost_general(is_last, ci, abs_qc_low, sign, coeff_ctx, |
59 | 185M | dc_sign_ctx, txb_costs, bhl, tx_class, levels); |
60 | 185M | } |
61 | | |
62 | 199M | rd_low = RDCOST(rdmult, rate_low, dist_low); |
63 | 199M | if (rd_low < rd) { |
64 | 7.17M | qcoeff[ci] = qc_low; |
65 | 7.17M | dqcoeff[ci] = dqc_low; |
66 | 7.17M | levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX); |
67 | 7.17M | *accu_rate += rate_low; |
68 | 7.17M | *accu_dist += dist_low - dist0; |
69 | 192M | } else { |
70 | 192M | *accu_rate += rate; |
71 | 192M | *accu_dist += dist - dist0; |
72 | 192M | } |
73 | 199M | } |
74 | 208M | } |
75 | | |
76 | | static AOM_FORCE_INLINE void update_coeff_simple( |
77 | | int *accu_rate, int si, int eob, TX_SIZE tx_size, TX_CLASS tx_class, |
78 | | int bhl, int64_t rdmult, int shift, const int16_t *dequant, |
79 | | const int16_t *scan, const LV_MAP_COEFF_COST *txb_costs, |
80 | | const tran_low_t *tcoeff, tran_low_t *qcoeff, tran_low_t *dqcoeff, |
81 | 3.72G | uint8_t *levels, const qm_val_t *iqmatrix, const qm_val_t *qmatrix) { |
82 | 3.72G | const int dqv = get_dqv(dequant, scan[si], iqmatrix); |
83 | 3.72G | (void)eob; |
84 | | // this simple version assumes the coeff's scan_idx is not DC (scan_idx != 0) |
85 | | // and not the last (scan_idx != eob - 1) |
86 | 3.72G | assert(si != eob - 1); |
87 | 3.72G | assert(si > 0); |
88 | 3.72G | const int ci = scan[si]; |
89 | 3.72G | const tran_low_t qc = qcoeff[ci]; |
90 | 3.72G | const int coeff_ctx = |
91 | 3.72G | get_lower_levels_ctx(levels, ci, bhl, tx_size, tx_class); |
92 | 3.72G | if (qc == 0) { |
93 | 1.04G | *accu_rate += txb_costs->base_cost[coeff_ctx][0]; |
94 | 2.67G | } else { |
95 | 2.67G | const tran_low_t abs_qc = abs(qc); |
96 | 2.67G | const tran_low_t abs_tqc = abs(tcoeff[ci]); |
97 | 2.67G | const tran_low_t abs_dqc = abs(dqcoeff[ci]); |
98 | 2.67G | int rate_low = 0; |
99 | 2.67G | const int rate = get_two_coeff_cost_simple( |
100 | 2.67G | ci, abs_qc, coeff_ctx, txb_costs, bhl, tx_class, levels, &rate_low); |
101 | 2.67G | if (abs_dqc < abs_tqc) { |
102 | 1.43G | *accu_rate += rate; |
103 | 1.43G | return; |
104 | 1.43G | } |
105 | | |
106 | 1.24G | const int64_t dist = get_coeff_dist(abs_tqc, abs_dqc, shift, qmatrix, ci); |
107 | 1.24G | const int64_t rd = RDCOST(rdmult, rate, dist); |
108 | | |
109 | 1.24G | const tran_low_t abs_qc_low = abs_qc - 1; |
110 | 1.24G | const tran_low_t abs_dqc_low = (abs_qc_low * dqv) >> shift; |
111 | 1.24G | const int64_t dist_low = |
112 | 1.24G | get_coeff_dist(abs_tqc, abs_dqc_low, shift, qmatrix, ci); |
113 | 1.24G | const int64_t rd_low = RDCOST(rdmult, rate_low, dist_low); |
114 | | |
115 | 1.24G | if (rd_low < rd) { |
116 | 101M | const int sign = (qc < 0) ? 1 : 0; |
117 | 101M | qcoeff[ci] = (-sign ^ abs_qc_low) + sign; |
118 | 101M | dqcoeff[ci] = (-sign ^ abs_dqc_low) + sign; |
119 | 101M | levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX); |
120 | 101M | *accu_rate += rate_low; |
121 | 1.14G | } else { |
122 | 1.14G | *accu_rate += rate; |
123 | 1.14G | } |
124 | 1.24G | } |
125 | 3.72G | } |
126 | | |
127 | | static AOM_FORCE_INLINE void update_coeff_eob( |
128 | | int *accu_rate, int64_t *accu_dist, int *eob, int *nz_num, int *nz_ci, |
129 | | int si, TX_SIZE tx_size, TX_CLASS tx_class, int bhl, int width, |
130 | | int dc_sign_ctx, int64_t rdmult, int shift, const int16_t *dequant, |
131 | | const int16_t *scan, const LV_MAP_EOB_COST *txb_eob_costs, |
132 | | const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff, |
133 | | tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels, int sharpness, |
134 | 865M | const qm_val_t *iqmatrix, const qm_val_t *qmatrix) { |
135 | 865M | const int dqv = get_dqv(dequant, scan[si], iqmatrix); |
136 | 865M | assert(si != *eob - 1); |
137 | 865M | const int ci = scan[si]; |
138 | 865M | const tran_low_t qc = qcoeff[ci]; |
139 | 865M | const int coeff_ctx = |
140 | 865M | get_lower_levels_ctx(levels, ci, bhl, tx_size, tx_class); |
141 | 865M | if (qc == 0) { |
142 | 563M | *accu_rate += txb_costs->base_cost[coeff_ctx][0]; |
143 | 563M | } else { |
144 | 301M | int lower_level = 0; |
145 | 301M | const tran_low_t abs_qc = abs(qc); |
146 | 301M | const tran_low_t tqc = tcoeff[ci]; |
147 | 301M | const tran_low_t dqc = dqcoeff[ci]; |
148 | 301M | const int sign = (qc < 0) ? 1 : 0; |
149 | 301M | const int64_t dist0 = get_coeff_dist(tqc, 0, shift, qmatrix, ci); |
150 | 301M | int64_t dist = get_coeff_dist(tqc, dqc, shift, qmatrix, ci) - dist0; |
151 | 301M | int rate = |
152 | 301M | get_coeff_cost_general(0, ci, abs_qc, sign, coeff_ctx, dc_sign_ctx, |
153 | 301M | txb_costs, bhl, tx_class, levels); |
154 | 301M | int64_t rd = RDCOST(rdmult, *accu_rate + rate, *accu_dist + dist); |
155 | | |
156 | 301M | tran_low_t qc_low, dqc_low; |
157 | 301M | tran_low_t abs_qc_low; |
158 | 301M | int64_t dist_low, rd_low; |
159 | 301M | int rate_low; |
160 | | |
161 | 301M | if (abs_qc == 1) { |
162 | 145M | abs_qc_low = 0; |
163 | 145M | dqc_low = qc_low = 0; |
164 | 145M | dist_low = 0; |
165 | 145M | rate_low = txb_costs->base_cost[coeff_ctx][0]; |
166 | 145M | rd_low = RDCOST(rdmult, *accu_rate + rate_low, *accu_dist); |
167 | 156M | } else { |
168 | 156M | get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low); |
169 | 156M | abs_qc_low = abs_qc - 1; |
170 | 156M | dist_low = get_coeff_dist(tqc, dqc_low, shift, qmatrix, ci) - dist0; |
171 | 156M | rate_low = |
172 | 156M | get_coeff_cost_general(0, ci, abs_qc_low, sign, coeff_ctx, |
173 | 156M | dc_sign_ctx, txb_costs, bhl, tx_class, levels); |
174 | 156M | rd_low = RDCOST(rdmult, *accu_rate + rate_low, *accu_dist + dist_low); |
175 | 156M | } |
176 | | |
177 | 301M | int lower_level_new_eob = 0; |
178 | 301M | const int new_eob = si + 1; |
179 | 301M | const int coeff_ctx_new_eob = get_lower_levels_ctx_eob(bhl, width, si); |
180 | 301M | const int new_eob_cost = |
181 | 301M | get_eob_cost(new_eob, txb_eob_costs, txb_costs, tx_class); |
182 | 301M | int rate_coeff_eob = |
183 | 301M | new_eob_cost + get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx_new_eob, |
184 | 301M | dc_sign_ctx, txb_costs, bhl, |
185 | 301M | tx_class); |
186 | 301M | int64_t dist_new_eob = dist; |
187 | 301M | int64_t rd_new_eob = RDCOST(rdmult, rate_coeff_eob, dist_new_eob); |
188 | | |
189 | 301M | if (abs_qc_low > 0) { |
190 | 173M | const int rate_coeff_eob_low = |
191 | 173M | new_eob_cost + get_coeff_cost_eob(ci, abs_qc_low, sign, |
192 | 173M | coeff_ctx_new_eob, dc_sign_ctx, |
193 | 173M | txb_costs, bhl, tx_class); |
194 | 173M | const int64_t dist_new_eob_low = dist_low; |
195 | 173M | const int64_t rd_new_eob_low = |
196 | 173M | RDCOST(rdmult, rate_coeff_eob_low, dist_new_eob_low); |
197 | 173M | if (rd_new_eob_low < rd_new_eob) { |
198 | 10.0M | lower_level_new_eob = 1; |
199 | 10.0M | rd_new_eob = rd_new_eob_low; |
200 | 10.0M | rate_coeff_eob = rate_coeff_eob_low; |
201 | 10.0M | dist_new_eob = dist_new_eob_low; |
202 | 10.0M | } |
203 | 173M | } |
204 | | |
205 | 315M | if (sharpness == 0 || abs_qc > 1) { |
206 | 315M | if (rd_low < rd) { |
207 | 48.7M | lower_level = 1; |
208 | 48.7M | rd = rd_low; |
209 | 48.7M | rate = rate_low; |
210 | 48.7M | dist = dist_low; |
211 | 48.7M | } |
212 | 315M | } |
213 | | |
214 | 315M | if (sharpness == 0 && rd_new_eob < rd) { |
215 | 77.4M | for (int ni = 0; ni < *nz_num; ++ni) { |
216 | 39.7M | int last_ci = nz_ci[ni]; |
217 | 39.7M | levels[get_padded_idx(last_ci, bhl)] = 0; |
218 | 39.7M | qcoeff[last_ci] = 0; |
219 | 39.7M | dqcoeff[last_ci] = 0; |
220 | 39.7M | } |
221 | 37.6M | *eob = new_eob; |
222 | 37.6M | *nz_num = 0; |
223 | 37.6M | *accu_rate = rate_coeff_eob; |
224 | 37.6M | *accu_dist = dist_new_eob; |
225 | 37.6M | lower_level = lower_level_new_eob; |
226 | 264M | } else { |
227 | 264M | *accu_rate += rate; |
228 | 264M | *accu_dist += dist; |
229 | 264M | } |
230 | | |
231 | 301M | if (lower_level) { |
232 | 32.7M | qcoeff[ci] = qc_low; |
233 | 32.7M | dqcoeff[ci] = dqc_low; |
234 | 32.7M | levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX); |
235 | 32.7M | } |
236 | 301M | if (qcoeff[ci]) { |
237 | 291M | nz_ci[*nz_num] = ci; |
238 | 291M | ++*nz_num; |
239 | 291M | } |
240 | 301M | } |
241 | 865M | } |
242 | | |
243 | | static inline void update_skip(int *accu_rate, int64_t accu_dist, int *eob, |
244 | | int nz_num, int *nz_ci, int64_t rdmult, |
245 | | int skip_cost, int non_skip_cost, |
246 | 10.3M | tran_low_t *qcoeff, tran_low_t *dqcoeff) { |
247 | 10.3M | const int64_t rd = RDCOST(rdmult, *accu_rate + non_skip_cost, accu_dist); |
248 | 10.3M | const int64_t rd_new_eob = RDCOST(rdmult, skip_cost, 0); |
249 | 10.3M | if (rd_new_eob < rd) { |
250 | 6.80M | for (int i = 0; i < nz_num; ++i) { |
251 | 3.57M | const int ci = nz_ci[i]; |
252 | 3.57M | qcoeff[ci] = 0; |
253 | 3.57M | dqcoeff[ci] = 0; |
254 | | // no need to set up levels because this is the last step |
255 | | // levels[get_padded_idx(ci, bhl)] = 0; |
256 | 3.57M | } |
257 | 3.22M | *accu_rate = 0; |
258 | 3.22M | *eob = 0; |
259 | 3.22M | } |
260 | 10.3M | } |
261 | | |
262 | | // TODO(angiebird): use this function whenever it's possible |
263 | | static int get_tx_type_cost(const MACROBLOCK *x, const MACROBLOCKD *xd, |
264 | | int plane, TX_SIZE tx_size, TX_TYPE tx_type, |
265 | 231M | int reduced_tx_set_used) { |
266 | 231M | if (plane > 0) return 0; |
267 | | |
268 | 178M | const TX_SIZE square_tx_size = txsize_sqr_map[tx_size]; |
269 | | |
270 | 178M | const MB_MODE_INFO *mbmi = xd->mi[0]; |
271 | 178M | const int is_inter = is_inter_block(mbmi); |
272 | 178M | if (get_ext_tx_types(tx_size, is_inter, reduced_tx_set_used) > 1 && |
273 | 178M | !xd->lossless[xd->mi[0]->segment_id]) { |
274 | 163M | const int ext_tx_set = |
275 | 163M | get_ext_tx_set(tx_size, is_inter, reduced_tx_set_used); |
276 | 163M | if (is_inter) { |
277 | 7.12M | if (ext_tx_set > 0) |
278 | 7.12M | return x->mode_costs |
279 | 7.12M | .inter_tx_type_costs[ext_tx_set][square_tx_size][tx_type]; |
280 | 155M | } else { |
281 | 155M | if (ext_tx_set > 0) { |
282 | 155M | PREDICTION_MODE intra_dir; |
283 | 155M | if (mbmi->filter_intra_mode_info.use_filter_intra) |
284 | 23.9M | intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info |
285 | 23.9M | .filter_intra_mode]; |
286 | 131M | else |
287 | 131M | intra_dir = mbmi->mode; |
288 | 155M | return x->mode_costs.intra_tx_type_costs[ext_tx_set][square_tx_size] |
289 | 155M | [intra_dir][tx_type]; |
290 | 155M | } |
291 | 155M | } |
292 | 163M | } |
293 | 15.4M | return 0; |
294 | 178M | } |
295 | | |
296 | | int av1_optimize_txb(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane, |
297 | | int block, TX_SIZE tx_size, TX_TYPE tx_type, |
298 | | const TXB_CTX *const txb_ctx, int *rate_cost, |
299 | 141M | int sharpness) { |
300 | 141M | MACROBLOCKD *xd = &x->e_mbd; |
301 | 141M | const struct macroblock_plane *p = &x->plane[plane]; |
302 | 141M | const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type); |
303 | 141M | const int16_t *scan = scan_order->scan; |
304 | 141M | const int shift = av1_get_tx_scale(tx_size); |
305 | 141M | int eob = p->eobs[block]; |
306 | 141M | const int16_t *dequant = p->dequant_QTX; |
307 | 141M | const qm_val_t *iqmatrix = |
308 | 141M | av1_get_iqmatrix(&cpi->common.quant_params, xd, plane, tx_size, tx_type); |
309 | 141M | const qm_val_t *qmatrix = |
310 | 141M | cpi->oxcf.tune_cfg.dist_metric == AOM_DIST_METRIC_QM_PSNR |
311 | 141M | ? av1_get_qmatrix(&cpi->common.quant_params, xd, plane, tx_size, |
312 | 0 | tx_type) |
313 | 141M | : NULL; |
314 | 141M | const int block_offset = BLOCK_OFFSET(block); |
315 | 141M | tran_low_t *qcoeff = p->qcoeff + block_offset; |
316 | 141M | tran_low_t *dqcoeff = p->dqcoeff + block_offset; |
317 | 141M | const tran_low_t *tcoeff = p->coeff + block_offset; |
318 | 141M | const CoeffCosts *coeff_costs = &x->coeff_costs; |
319 | | |
320 | | // This function is not called if eob = 0. |
321 | 141M | assert(eob > 0); |
322 | | |
323 | 141M | const AV1_COMMON *cm = &cpi->common; |
324 | 141M | const PLANE_TYPE plane_type = get_plane_type(plane); |
325 | 141M | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
326 | 141M | const TX_CLASS tx_class = tx_type_to_class[tx_type]; |
327 | 141M | const MB_MODE_INFO *mbmi = xd->mi[0]; |
328 | 141M | const int bhl = get_txb_bhl(tx_size); |
329 | 141M | const int width = get_txb_wide(tx_size); |
330 | 141M | const int height = get_txb_high(tx_size); |
331 | 141M | assert(height == (1 << bhl)); |
332 | 141M | const int is_inter = is_inter_block(mbmi); |
333 | 141M | const LV_MAP_COEFF_COST *txb_costs = |
334 | 141M | &coeff_costs->coeff_costs[txs_ctx][plane_type]; |
335 | 141M | const int eob_multi_size = txsize_log2_minus4[tx_size]; |
336 | 141M | const LV_MAP_EOB_COST *txb_eob_costs = |
337 | 141M | &coeff_costs->eob_costs[eob_multi_size][plane_type]; |
338 | | |
339 | | // For the IQ tune, increase rshift from 2 to 4. |
340 | | // This biases trellis quantization towards keeping more coefficients, and |
341 | | // together with the IQ rdmult adjustment in |
342 | | // av1_compute_rd_mult_based_on_qindex(), this helps preserve image |
343 | | // features (like repeating patterns and camera noise/film grain), which |
344 | | // improves SSIMULACRA 2 scores. |
345 | 141M | const int rshift = cpi->oxcf.tune_cfg.tuning == AOM_TUNE_IQ ? 4 : 2; |
346 | | |
347 | 141M | const int64_t rdmult = ROUND_POWER_OF_TWO( |
348 | 141M | (int64_t)x->rdmult * |
349 | 141M | (plane_rd_mult[is_inter][plane_type] << (2 * (xd->bd - 8))), |
350 | 141M | rshift); |
351 | | |
352 | 141M | uint8_t levels_buf[TX_PAD_2D]; |
353 | 141M | uint8_t *const levels = set_levels(levels_buf, height); |
354 | | |
355 | 141M | if (eob > 1) av1_txb_init_levels(qcoeff, width, height, levels); |
356 | | |
357 | | // TODO(angirbird): check iqmatrix |
358 | | |
359 | 141M | const int non_skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][0]; |
360 | 141M | const int skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1]; |
361 | 141M | const int eob_cost = get_eob_cost(eob, txb_eob_costs, txb_costs, tx_class); |
362 | 141M | int accu_rate = eob_cost; |
363 | 141M | int64_t accu_dist = 0; |
364 | 141M | int si = eob - 1; |
365 | 141M | const int ci = scan[si]; |
366 | 141M | const tran_low_t qc = qcoeff[ci]; |
367 | 141M | const tran_low_t abs_qc = abs(qc); |
368 | 141M | const int sign = qc < 0; |
369 | 141M | const int max_nz_num = 2; |
370 | 141M | int nz_num = 1; |
371 | 141M | int nz_ci[3] = { ci, 0, 0 }; |
372 | 141M | if (abs_qc >= 2) { |
373 | 84.8M | update_coeff_general(&accu_rate, &accu_dist, si, eob, tx_size, tx_class, |
374 | 84.8M | bhl, width, rdmult, shift, txb_ctx->dc_sign_ctx, |
375 | 84.8M | dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff, |
376 | 84.8M | levels, iqmatrix, qmatrix); |
377 | 84.8M | --si; |
378 | 84.8M | } else { |
379 | 56.3M | assert(abs_qc == 1); |
380 | 56.3M | const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, si); |
381 | 56.3M | accu_rate += |
382 | 56.3M | get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx, txb_ctx->dc_sign_ctx, |
383 | 56.3M | txb_costs, bhl, tx_class); |
384 | 56.3M | const tran_low_t tqc = tcoeff[ci]; |
385 | 56.3M | const tran_low_t dqc = dqcoeff[ci]; |
386 | 56.3M | const int64_t dist = get_coeff_dist(tqc, dqc, shift, qmatrix, ci); |
387 | 56.3M | const int64_t dist0 = get_coeff_dist(tqc, 0, shift, qmatrix, ci); |
388 | 56.3M | accu_dist += dist - dist0; |
389 | 56.3M | --si; |
390 | 56.3M | } |
391 | | |
392 | 141M | #define UPDATE_COEFF_EOB_CASE(tx_class_literal) \ |
393 | 141M | case tx_class_literal: \ |
394 | 1.01G | for (; si >= 0 && nz_num <= max_nz_num; --si) { \ |
395 | 876M | update_coeff_eob(&accu_rate, &accu_dist, &eob, &nz_num, nz_ci, si, \ |
396 | 876M | tx_size, tx_class_literal, bhl, width, \ |
397 | 876M | txb_ctx->dc_sign_ctx, rdmult, shift, dequant, scan, \ |
398 | 876M | txb_eob_costs, txb_costs, tcoeff, qcoeff, dqcoeff, \ |
399 | 876M | levels, sharpness, iqmatrix, qmatrix); \ |
400 | 876M | } \ |
401 | 140M | break |
402 | 141M | switch (tx_class) { |
403 | 117M | UPDATE_COEFF_EOB_CASE(TX_CLASS_2D); |
404 | 21.6M | UPDATE_COEFF_EOB_CASE(TX_CLASS_HORIZ); |
405 | 1.61M | UPDATE_COEFF_EOB_CASE(TX_CLASS_VERT); |
406 | 0 | #undef UPDATE_COEFF_EOB_CASE |
407 | 0 | default: assert(false); |
408 | 141M | } |
409 | | |
410 | 138M | if (si == -1 && nz_num <= max_nz_num && sharpness == 0) { |
411 | 10.3M | update_skip(&accu_rate, accu_dist, &eob, nz_num, nz_ci, rdmult, skip_cost, |
412 | 10.3M | non_skip_cost, qcoeff, dqcoeff); |
413 | 10.3M | } |
414 | | |
415 | 138M | #define UPDATE_COEFF_SIMPLE_CASE(tx_class_literal) \ |
416 | 139M | case tx_class_literal: \ |
417 | 4.04G | for (; si >= 1; --si) { \ |
418 | 3.90G | update_coeff_simple(&accu_rate, si, eob, tx_size, tx_class_literal, bhl, \ |
419 | 3.90G | rdmult, shift, dequant, scan, txb_costs, tcoeff, \ |
420 | 3.90G | qcoeff, dqcoeff, levels, iqmatrix, qmatrix); \ |
421 | 3.90G | } \ |
422 | 139M | break |
423 | 138M | switch (tx_class) { |
424 | 116M | UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_2D); |
425 | 21.6M | UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_HORIZ); |
426 | 1.61M | UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_VERT); |
427 | 0 | #undef UPDATE_COEFF_SIMPLE_CASE |
428 | 0 | default: assert(false); |
429 | 138M | } |
430 | | |
431 | | // DC position |
432 | 135M | if (si == 0) { |
433 | | // no need to update accu_dist because it's not used after this point |
434 | 122M | int64_t dummy_dist = 0; |
435 | 122M | update_coeff_general(&accu_rate, &dummy_dist, si, eob, tx_size, tx_class, |
436 | 122M | bhl, width, rdmult, shift, txb_ctx->dc_sign_ctx, |
437 | 122M | dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff, |
438 | 122M | levels, iqmatrix, qmatrix); |
439 | 122M | } |
440 | | |
441 | 135M | const int tx_type_cost = get_tx_type_cost(x, xd, plane, tx_size, tx_type, |
442 | 135M | cm->features.reduced_tx_set_used); |
443 | 135M | if (eob == 0) |
444 | 3.23M | accu_rate += skip_cost; |
445 | 132M | else |
446 | 132M | accu_rate += non_skip_cost + tx_type_cost; |
447 | | |
448 | 135M | p->eobs[block] = eob; |
449 | 135M | p->txb_entropy_ctx[block] = |
450 | 135M | av1_get_txb_entropy_context(qcoeff, scan_order, p->eobs[block]); |
451 | | |
452 | 135M | *rate_cost = accu_rate; |
453 | 135M | return eob; |
454 | 138M | } |
455 | | |
456 | | static AOM_FORCE_INLINE int warehouse_efficients_txb( |
457 | | const MACROBLOCK *x, const int plane, const int block, |
458 | | const TX_SIZE tx_size, const TXB_CTX *const txb_ctx, |
459 | | const struct macroblock_plane *p, const int eob, |
460 | | const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs, |
461 | | const MACROBLOCKD *const xd, const TX_TYPE tx_type, const TX_CLASS tx_class, |
462 | 93.7M | int reduced_tx_set_used) { |
463 | 93.7M | const tran_low_t *const qcoeff = p->qcoeff + BLOCK_OFFSET(block); |
464 | 93.7M | const int txb_skip_ctx = txb_ctx->txb_skip_ctx; |
465 | 93.7M | const int bhl = get_txb_bhl(tx_size); |
466 | 93.7M | const int width = get_txb_wide(tx_size); |
467 | 93.7M | const int height = get_txb_high(tx_size); |
468 | 93.7M | const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
469 | 93.7M | const int16_t *const scan = scan_order->scan; |
470 | 93.7M | uint8_t levels_buf[TX_PAD_2D]; |
471 | 93.7M | uint8_t *const levels = set_levels(levels_buf, height); |
472 | 93.7M | DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]); |
473 | 93.7M | const int eob_multi_size = txsize_log2_minus4[tx_size]; |
474 | 93.7M | const LV_MAP_EOB_COST *const eob_costs = |
475 | 93.7M | &x->coeff_costs.eob_costs[eob_multi_size][plane_type]; |
476 | 93.7M | int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0]; |
477 | | |
478 | 93.7M | av1_txb_init_levels(qcoeff, width, height, levels); |
479 | | |
480 | 93.7M | cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used); |
481 | | |
482 | 93.7M | cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class); |
483 | | |
484 | 93.7M | av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts); |
485 | | |
486 | 93.7M | const int(*lps_cost)[COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1] = |
487 | 93.7M | coeff_costs->lps_cost; |
488 | 93.7M | int c = eob - 1; |
489 | 93.7M | { |
490 | 93.7M | const int pos = scan[c]; |
491 | 93.7M | const tran_low_t v = qcoeff[pos]; |
492 | 93.7M | const int sign = AOMSIGN(v); |
493 | 93.7M | const int level = (v ^ sign) - sign; |
494 | 93.7M | const int coeff_ctx = coeff_contexts[pos]; |
495 | 93.7M | cost += coeff_costs->base_eob_cost[coeff_ctx][AOMMIN(level, 3) - 1]; |
496 | | |
497 | 94.2M | if (v) { |
498 | | // sign bit cost |
499 | 94.2M | if (level > NUM_BASE_LEVELS) { |
500 | 59.4M | const int ctx = get_br_ctx_eob(pos, bhl, tx_class); |
501 | 59.4M | cost += get_br_cost(level, lps_cost[ctx]); |
502 | 59.4M | } |
503 | 94.2M | if (c) { |
504 | 93.8M | cost += av1_cost_literal(1); |
505 | 93.8M | } else { |
506 | 428k | const int sign01 = (sign ^ sign) - sign; |
507 | 428k | const int dc_sign_ctx = txb_ctx->dc_sign_ctx; |
508 | 428k | cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01]; |
509 | 428k | return cost; |
510 | 428k | } |
511 | 94.2M | } |
512 | 93.7M | } |
513 | 93.3M | const int(*base_cost)[8] = coeff_costs->base_cost; |
514 | 4.76G | for (c = eob - 2; c >= 1; --c) { |
515 | 4.66G | const int pos = scan[c]; |
516 | 4.66G | const int coeff_ctx = coeff_contexts[pos]; |
517 | 4.66G | const tran_low_t v = qcoeff[pos]; |
518 | 4.66G | const int level = abs(v); |
519 | 4.66G | cost += base_cost[coeff_ctx][AOMMIN(level, 3)]; |
520 | 4.66G | if (v) { |
521 | | // sign bit cost |
522 | 3.20G | cost += av1_cost_literal(1); |
523 | 3.20G | if (level > NUM_BASE_LEVELS) { |
524 | 2.02G | const int ctx = get_br_ctx(levels, pos, bhl, tx_class); |
525 | 2.02G | cost += get_br_cost(level, lps_cost[ctx]); |
526 | 2.02G | } |
527 | 3.20G | } |
528 | 4.66G | } |
529 | | // c == 0 after previous loop |
530 | 93.3M | { |
531 | 93.3M | const int pos = scan[c]; |
532 | 93.3M | const tran_low_t v = qcoeff[pos]; |
533 | 93.3M | const int coeff_ctx = coeff_contexts[pos]; |
534 | 93.3M | const int sign = AOMSIGN(v); |
535 | 93.3M | const int level = (v ^ sign) - sign; |
536 | 93.3M | cost += base_cost[coeff_ctx][AOMMIN(level, 3)]; |
537 | | |
538 | 93.3M | if (v) { |
539 | | // sign bit cost |
540 | 89.5M | const int sign01 = (sign ^ sign) - sign; |
541 | 89.5M | const int dc_sign_ctx = txb_ctx->dc_sign_ctx; |
542 | 89.5M | cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01]; |
543 | 89.5M | if (level > NUM_BASE_LEVELS) { |
544 | 79.8M | const int ctx = get_br_ctx(levels, pos, bhl, tx_class); |
545 | 79.8M | cost += get_br_cost(level, lps_cost[ctx]); |
546 | 79.8M | } |
547 | 89.5M | } |
548 | 93.3M | } |
549 | 93.3M | return cost; |
550 | 93.7M | } |
551 | | |
552 | | /*!\brief Estimate the entropy cost of transform coefficients using Laplacian |
553 | | * distribution. |
554 | | * |
555 | | * \ingroup coefficient_coding |
556 | | * |
557 | | * This function assumes each transform coefficient is of its own Laplacian |
558 | | * distribution and the coefficient is the only observation of the Laplacian |
559 | | * distribution. |
560 | | * |
561 | | * Based on that, each coefficient's coding cost can be estimated by computing |
562 | | * the entropy of the corresponding Laplacian distribution. |
563 | | * |
564 | | * This function then return the sum of the estimated entropy cost for all |
565 | | * coefficients in the transform block. |
566 | | * |
567 | | * Note that the entropy cost of end of block (eob) and transform type (tx_type) |
568 | | * are not included. |
569 | | * |
570 | | * \param[in] x Pointer to structure holding the data for the |
571 | | current encoding macroblock |
572 | | * \param[in] plane The index of the current plane |
573 | | * \param[in] block The index of the current transform block in the |
574 | | * macroblock. It's defined by number of 4x4 units that have been coded before |
575 | | * the currernt transform block |
576 | | * \param[in] tx_size The transform size |
577 | | * \param[in] tx_type The transform type |
578 | | * \return int Estimated entropy cost of coefficients in the |
579 | | * transform block. |
580 | | */ |
581 | | static int av1_cost_coeffs_txb_estimate(const MACROBLOCK *x, const int plane, |
582 | | const int block, const TX_SIZE tx_size, |
583 | 0 | const TX_TYPE tx_type) { |
584 | 0 | assert(plane == 0); |
585 | |
|
586 | 0 | int cost = 0; |
587 | 0 | const struct macroblock_plane *p = &x->plane[plane]; |
588 | 0 | const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type); |
589 | 0 | const int16_t *scan = scan_order->scan; |
590 | 0 | tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block); |
591 | |
|
592 | 0 | int eob = p->eobs[block]; |
593 | | |
594 | | // coeffs |
595 | 0 | int c = eob - 1; |
596 | | // eob |
597 | 0 | { |
598 | 0 | const int pos = scan[c]; |
599 | 0 | const tran_low_t v = abs(qcoeff[pos]) - 1; |
600 | 0 | cost += (v << (AV1_PROB_COST_SHIFT + 2)); |
601 | 0 | } |
602 | | // other coeffs |
603 | 0 | for (c = eob - 2; c >= 0; c--) { |
604 | 0 | const int pos = scan[c]; |
605 | 0 | const tran_low_t v = abs(qcoeff[pos]); |
606 | 0 | const int idx = AOMMIN(v, 14); |
607 | |
|
608 | 0 | cost += costLUT[idx]; |
609 | 0 | } |
610 | | |
611 | | // const_term does not contain DC, and log(e) does not contain eob, so both |
612 | | // (eob-1) |
613 | 0 | cost += (const_term + loge_par) * (eob - 1); |
614 | |
|
615 | 0 | return cost; |
616 | 0 | } |
617 | | |
618 | | static AOM_FORCE_INLINE int warehouse_efficients_txb_laplacian( |
619 | | const MACROBLOCK *x, const int plane, const int block, |
620 | | const TX_SIZE tx_size, const TXB_CTX *const txb_ctx, const int eob, |
621 | | const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs, |
622 | | const MACROBLOCKD *const xd, const TX_TYPE tx_type, const TX_CLASS tx_class, |
623 | 0 | int reduced_tx_set_used) { |
624 | 0 | const int txb_skip_ctx = txb_ctx->txb_skip_ctx; |
625 | |
|
626 | 0 | const int eob_multi_size = txsize_log2_minus4[tx_size]; |
627 | 0 | const LV_MAP_EOB_COST *const eob_costs = |
628 | 0 | &x->coeff_costs.eob_costs[eob_multi_size][plane_type]; |
629 | 0 | int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0]; |
630 | |
|
631 | 0 | cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used); |
632 | |
|
633 | 0 | cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class); |
634 | |
|
635 | 0 | cost += av1_cost_coeffs_txb_estimate(x, plane, block, tx_size, tx_type); |
636 | 0 | return cost; |
637 | 0 | } |
638 | | |
639 | | int av1_cost_coeffs_txb(const MACROBLOCK *x, const int plane, const int block, |
640 | | const TX_SIZE tx_size, const TX_TYPE tx_type, |
641 | 94.3M | const TXB_CTX *const txb_ctx, int reduced_tx_set_used) { |
642 | 94.3M | const struct macroblock_plane *p = &x->plane[plane]; |
643 | 94.3M | const int eob = p->eobs[block]; |
644 | 94.3M | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
645 | 94.3M | const PLANE_TYPE plane_type = get_plane_type(plane); |
646 | 94.3M | const LV_MAP_COEFF_COST *const coeff_costs = |
647 | 94.3M | &x->coeff_costs.coeff_costs[txs_ctx][plane_type]; |
648 | 94.3M | if (eob == 0) { |
649 | 600k | return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1]; |
650 | 600k | } |
651 | | |
652 | 93.7M | const MACROBLOCKD *const xd = &x->e_mbd; |
653 | 93.7M | const TX_CLASS tx_class = tx_type_to_class[tx_type]; |
654 | | |
655 | 93.7M | return warehouse_efficients_txb(x, plane, block, tx_size, txb_ctx, p, eob, |
656 | 93.7M | plane_type, coeff_costs, xd, tx_type, |
657 | 93.7M | tx_class, reduced_tx_set_used); |
658 | 94.3M | } |
659 | | |
660 | | int av1_cost_coeffs_txb_laplacian(const MACROBLOCK *x, const int plane, |
661 | | const int block, const TX_SIZE tx_size, |
662 | | const TX_TYPE tx_type, |
663 | | const TXB_CTX *const txb_ctx, |
664 | | const int reduced_tx_set_used, |
665 | 0 | const int adjust_eob) { |
666 | 0 | const struct macroblock_plane *p = &x->plane[plane]; |
667 | 0 | int eob = p->eobs[block]; |
668 | |
|
669 | 0 | if (adjust_eob) { |
670 | 0 | const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type); |
671 | 0 | const int16_t *scan = scan_order->scan; |
672 | 0 | tran_low_t *tcoeff = p->coeff + BLOCK_OFFSET(block); |
673 | 0 | tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block); |
674 | 0 | tran_low_t *dqcoeff = p->dqcoeff + BLOCK_OFFSET(block); |
675 | 0 | update_coeff_eob_fast(&eob, av1_get_tx_scale(tx_size), p->dequant_QTX, scan, |
676 | 0 | tcoeff, qcoeff, dqcoeff); |
677 | 0 | p->eobs[block] = eob; |
678 | 0 | } |
679 | |
|
680 | 0 | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
681 | 0 | const PLANE_TYPE plane_type = get_plane_type(plane); |
682 | 0 | const LV_MAP_COEFF_COST *const coeff_costs = |
683 | 0 | &x->coeff_costs.coeff_costs[txs_ctx][plane_type]; |
684 | 0 | if (eob == 0) { |
685 | 0 | return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1]; |
686 | 0 | } |
687 | | |
688 | 0 | const MACROBLOCKD *const xd = &x->e_mbd; |
689 | 0 | const TX_CLASS tx_class = tx_type_to_class[tx_type]; |
690 | |
|
691 | 0 | return warehouse_efficients_txb_laplacian( |
692 | 0 | x, plane, block, tx_size, txb_ctx, eob, plane_type, coeff_costs, xd, |
693 | 0 | tx_type, tx_class, reduced_tx_set_used); |
694 | 0 | } |