/src/aom/av1/encoder/txb_rdopt_utils.h
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 | | #ifndef AOM_AV1_ENCODER_TXB_RDOPT_UTILS_H_ |
13 | | #define AOM_AV1_ENCODER_TXB_RDOPT_UTILS_H_ |
14 | | |
15 | | #include "av1/encoder/encodetxb.h" |
16 | | |
17 | | static const int golomb_bits_cost[32] = { |
18 | | 0, 512, 512 * 3, 512 * 3, 512 * 5, 512 * 5, 512 * 5, 512 * 5, |
19 | | 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, |
20 | | 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, |
21 | | 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9 |
22 | | }; |
23 | | |
24 | | static const int golomb_cost_diff[32] = { |
25 | | 0, 512, 512 * 2, 0, 512 * 2, 0, 0, 0, 512 * 2, 0, 0, 0, 0, 0, 0, 0, |
26 | | 512 * 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
27 | | }; |
28 | | |
29 | | // Look up table of individual cost of coefficient by its quantization level. |
30 | | // determined based on Laplacian distribution conditioned on estimated context |
31 | | static const int costLUT[15] = { -1143, 53, 545, 825, 1031, |
32 | | 1209, 1393, 1577, 1763, 1947, |
33 | | 2132, 2317, 2501, 2686, 2871 }; |
34 | | |
35 | | static const int const_term = (1 << AV1_PROB_COST_SHIFT); |
36 | | |
37 | | static const int loge_par = ((14427 << AV1_PROB_COST_SHIFT) + 5000) / 10000; |
38 | | |
39 | | static INLINE int get_dqv(const int16_t *dequant, int coeff_idx, |
40 | 1.68M | const qm_val_t *iqmatrix) { |
41 | 1.68M | int dqv = dequant[!!coeff_idx]; |
42 | 1.68M | if (iqmatrix != NULL) |
43 | 0 | dqv = |
44 | 0 | ((iqmatrix[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS; |
45 | 1.68M | return dqv; |
46 | 1.68M | } |
47 | | |
48 | | static INLINE int64_t get_coeff_dist(tran_low_t tcoeff, tran_low_t dqcoeff, |
49 | 895k | int shift) { |
50 | 895k | const int64_t diff = (tcoeff - dqcoeff) * (1 << shift); |
51 | 895k | const int64_t error = diff * diff; |
52 | 895k | return error; |
53 | 895k | } |
54 | | |
55 | | static int get_eob_cost(int eob, const LV_MAP_EOB_COST *txb_eob_costs, |
56 | 510k | const LV_MAP_COEFF_COST *txb_costs, TX_CLASS tx_class) { |
57 | 510k | int eob_extra; |
58 | 510k | const int eob_pt = av1_get_eob_pos_token(eob, &eob_extra); |
59 | 510k | int eob_cost = 0; |
60 | 510k | const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1; |
61 | 510k | eob_cost = txb_eob_costs->eob_cost[eob_multi_ctx][eob_pt - 1]; |
62 | | |
63 | 510k | if (av1_eob_offset_bits[eob_pt] > 0) { |
64 | 167k | const int eob_ctx = eob_pt - 3; |
65 | 167k | const int eob_shift = av1_eob_offset_bits[eob_pt] - 1; |
66 | 167k | const int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0; |
67 | 167k | eob_cost += txb_costs->eob_extra_cost[eob_ctx][bit]; |
68 | 167k | const int offset_bits = av1_eob_offset_bits[eob_pt]; |
69 | 167k | if (offset_bits > 1) eob_cost += av1_cost_literal(offset_bits - 1); |
70 | 167k | } |
71 | 510k | return eob_cost; |
72 | 510k | } |
73 | | |
74 | 668k | static INLINE int get_golomb_cost(int abs_qc) { |
75 | 668k | if (abs_qc >= 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) { |
76 | 376k | const int r = abs_qc - COEFF_BASE_RANGE - NUM_BASE_LEVELS; |
77 | 376k | const int length = get_msb(r) + 1; |
78 | 376k | return av1_cost_literal(2 * length - 1); |
79 | 376k | } |
80 | 291k | return 0; |
81 | 668k | } |
82 | | |
83 | 648k | static INLINE int get_br_cost(tran_low_t level, const int *coeff_lps) { |
84 | 648k | const int base_range = AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE); |
85 | 648k | return coeff_lps[base_range] + get_golomb_cost(level); |
86 | 648k | } |
87 | | |
88 | | static INLINE int get_br_cost_with_diff(tran_low_t level, const int *coeff_lps, |
89 | 106k | int *diff) { |
90 | 106k | const int base_range = AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE); |
91 | 106k | int golomb_bits = 0; |
92 | 106k | if (level <= COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS) |
93 | 67.0k | *diff += coeff_lps[base_range + COEFF_BASE_RANGE + 1]; |
94 | | |
95 | 106k | if (level >= COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS) { |
96 | 41.0k | int r = level - COEFF_BASE_RANGE - NUM_BASE_LEVELS; |
97 | 41.0k | if (r < 32) { |
98 | 21.6k | golomb_bits = golomb_bits_cost[r]; |
99 | 21.6k | *diff += golomb_cost_diff[r]; |
100 | 21.6k | } else { |
101 | 19.4k | golomb_bits = get_golomb_cost(level); |
102 | 19.4k | *diff += (r & (r - 1)) == 0 ? 1024 : 0; |
103 | 19.4k | } |
104 | 41.0k | } |
105 | | |
106 | 106k | return coeff_lps[base_range] + golomb_bits; |
107 | 106k | } |
108 | | |
109 | | static AOM_FORCE_INLINE int get_two_coeff_cost_simple( |
110 | | int ci, tran_low_t abs_qc, int coeff_ctx, |
111 | | const LV_MAP_COEFF_COST *txb_costs, int bwl, TX_CLASS tx_class, |
112 | 170k | const uint8_t *levels, int *cost_low) { |
113 | | // this simple version assumes the coeff's scan_idx is not DC (scan_idx != 0) |
114 | | // and not the last (scan_idx != eob - 1) |
115 | 170k | assert(ci > 0); |
116 | 170k | int cost = txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)]; |
117 | 170k | int diff = 0; |
118 | 170k | if (abs_qc <= 3) diff = txb_costs->base_cost[coeff_ctx][abs_qc + 4]; |
119 | 170k | if (abs_qc) { |
120 | 170k | cost += av1_cost_literal(1); |
121 | 170k | if (abs_qc > NUM_BASE_LEVELS) { |
122 | 106k | const int br_ctx = get_br_ctx(levels, ci, bwl, tx_class); |
123 | 106k | int brcost_diff = 0; |
124 | 106k | cost += get_br_cost_with_diff(abs_qc, txb_costs->lps_cost[br_ctx], |
125 | 106k | &brcost_diff); |
126 | 106k | diff += brcost_diff; |
127 | 106k | } |
128 | 170k | } |
129 | 170k | *cost_low = cost - diff; |
130 | | |
131 | 170k | return cost; |
132 | 170k | } |
133 | | |
134 | | static INLINE int get_coeff_cost_eob(int ci, tran_low_t abs_qc, int sign, |
135 | | int coeff_ctx, int dc_sign_ctx, |
136 | | const LV_MAP_COEFF_COST *txb_costs, |
137 | 229k | int bwl, TX_CLASS tx_class) { |
138 | 229k | int cost = 0; |
139 | 229k | cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1]; |
140 | 229k | if (abs_qc != 0) { |
141 | 229k | if (ci == 0) { |
142 | 118k | cost += txb_costs->dc_sign_cost[dc_sign_ctx][sign]; |
143 | 118k | } else { |
144 | 111k | cost += av1_cost_literal(1); |
145 | 111k | } |
146 | 229k | if (abs_qc > NUM_BASE_LEVELS) { |
147 | 56.6k | int br_ctx; |
148 | 56.6k | br_ctx = get_br_ctx_eob(ci, bwl, tx_class); |
149 | 56.6k | cost += get_br_cost(abs_qc, txb_costs->lps_cost[br_ctx]); |
150 | 56.6k | } |
151 | 229k | } |
152 | 229k | return cost; |
153 | 229k | } |
154 | | |
155 | | static INLINE int get_coeff_cost_general(int is_last, int ci, tran_low_t abs_qc, |
156 | | int sign, int coeff_ctx, |
157 | | int dc_sign_ctx, |
158 | | const LV_MAP_COEFF_COST *txb_costs, |
159 | | int bwl, TX_CLASS tx_class, |
160 | 309k | const uint8_t *levels) { |
161 | 309k | int cost = 0; |
162 | 309k | if (is_last) { |
163 | 153k | cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1]; |
164 | 156k | } else { |
165 | 156k | cost += txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)]; |
166 | 156k | } |
167 | 309k | if (abs_qc != 0) { |
168 | 309k | if (ci == 0) { |
169 | 187k | cost += txb_costs->dc_sign_cost[dc_sign_ctx][sign]; |
170 | 187k | } else { |
171 | 122k | cost += av1_cost_literal(1); |
172 | 122k | } |
173 | 309k | if (abs_qc > NUM_BASE_LEVELS) { |
174 | 248k | int br_ctx; |
175 | 248k | if (is_last) |
176 | 141k | br_ctx = get_br_ctx_eob(ci, bwl, tx_class); |
177 | 107k | else |
178 | 107k | br_ctx = get_br_ctx(levels, ci, bwl, tx_class); |
179 | 248k | cost += get_br_cost(abs_qc, txb_costs->lps_cost[br_ctx]); |
180 | 248k | } |
181 | 309k | } |
182 | 309k | return cost; |
183 | 309k | } |
184 | | |
185 | | static INLINE void get_qc_dqc_low(tran_low_t abs_qc, int sign, int dqv, |
186 | | int shift, tran_low_t *qc_low, |
187 | 141k | tran_low_t *dqc_low) { |
188 | 141k | tran_low_t abs_qc_low = abs_qc - 1; |
189 | 141k | *qc_low = (-sign ^ abs_qc_low) + sign; |
190 | 141k | assert((sign ? -abs_qc_low : abs_qc_low) == *qc_low); |
191 | 141k | tran_low_t abs_dqc_low = (abs_qc_low * dqv) >> shift; |
192 | 141k | *dqc_low = (-sign ^ abs_dqc_low) + sign; |
193 | 141k | assert((sign ? -abs_dqc_low : abs_dqc_low) == *dqc_low); |
194 | 141k | } |
195 | | |
196 | | static INLINE void update_coeff_eob_fast(int *eob, int shift, |
197 | | const int16_t *dequant_ptr, |
198 | | const int16_t *scan, |
199 | | const tran_low_t *coeff_ptr, |
200 | | tran_low_t *qcoeff_ptr, |
201 | 0 | tran_low_t *dqcoeff_ptr) { |
202 | | // TODO(sarahparker) make this work for aomqm |
203 | 0 | int eob_out = *eob; |
204 | 0 | int zbin[2] = { dequant_ptr[0] + ROUND_POWER_OF_TWO(dequant_ptr[0] * 70, 7), |
205 | 0 | dequant_ptr[1] + ROUND_POWER_OF_TWO(dequant_ptr[1] * 70, 7) }; |
206 | |
|
207 | 0 | for (int i = *eob - 1; i >= 0; i--) { |
208 | 0 | const int rc = scan[i]; |
209 | 0 | const int qcoeff = qcoeff_ptr[rc]; |
210 | 0 | const int coeff = coeff_ptr[rc]; |
211 | 0 | const int coeff_sign = AOMSIGN(coeff); |
212 | 0 | int64_t abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
213 | |
|
214 | 0 | if (((abs_coeff << (1 + shift)) < zbin[rc != 0]) || (qcoeff == 0)) { |
215 | 0 | eob_out--; |
216 | 0 | qcoeff_ptr[rc] = 0; |
217 | 0 | dqcoeff_ptr[rc] = 0; |
218 | 0 | } else { |
219 | 0 | break; |
220 | 0 | } |
221 | 0 | } |
222 | |
|
223 | 0 | *eob = eob_out; |
224 | 0 | } |
225 | | #endif // AOM_AV1_ENCODER_TXB_RDOPT_UTILS_H_ |