/src/avm/av2/common/idct.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 <math.h> |
14 | | |
15 | | #include "config/avm_dsp_rtcd.h" |
16 | | #include "config/av2_rtcd.h" |
17 | | |
18 | | #include "avm_ports/mem.h" |
19 | | #include "av2/common/av2_txfm.h" |
20 | | #include "av2/common/blockd.h" |
21 | | #include "av2/common/enums.h" |
22 | | #include "av2/common/idct.h" |
23 | | #include "av2/common/scan.h" |
24 | | #include "av2/common/txb_common.h" |
25 | | |
26 | 0 | static INLINE void clamp_buf(int32_t *buf, int32_t size, int8_t bit) { |
27 | 0 | for (int i = 0; i < size; ++i) buf[i] = clamp_value(buf[i], bit); |
28 | 0 | } |
29 | | |
30 | | void inv_txfm_dct2_size4_c(const int *src, int *dst, int shift, int line, |
31 | | int skip_line, int zero_line, const int coef_min, |
32 | 0 | const int coef_max) { |
33 | 0 | (void)zero_line; |
34 | 0 | int j; |
35 | 0 | int a[2], b[2]; |
36 | 0 | int add = 1 << (shift - 1); |
37 | |
|
38 | 0 | const int *tx_mat = tx_kernel_dct2_size4[INV_TXFM][0]; |
39 | |
|
40 | 0 | const int nz_line = line - skip_line; |
41 | 0 | for (j = 0; j < nz_line; j++) { |
42 | 0 | b[0] = tx_mat[1 * 4 + 0] * src[1] + tx_mat[3 * 4 + 0] * src[3]; |
43 | 0 | b[1] = tx_mat[1 * 4 + 1] * src[1] + tx_mat[3 * 4 + 1] * src[3]; |
44 | 0 | a[0] = tx_mat[0 * 4 + 0] * src[0] + tx_mat[2 * 4 + 0] * src[2]; |
45 | 0 | a[1] = tx_mat[0 * 4 + 1] * src[0] + tx_mat[2 * 4 + 1] * src[2]; |
46 | |
|
47 | 0 | dst[0 * line] = clamp((a[0] + b[0] + add) >> shift, coef_min, coef_max); |
48 | 0 | dst[1 * line] = clamp((a[1] + b[1] + add) >> shift, coef_min, coef_max); |
49 | 0 | dst[2 * line] = clamp((a[1] - b[1] + add) >> shift, coef_min, coef_max); |
50 | 0 | dst[3 * line] = clamp((a[0] - b[0] + add) >> shift, coef_min, coef_max); |
51 | |
|
52 | 0 | src += 4; |
53 | 0 | dst++; |
54 | 0 | } |
55 | 0 | } |
56 | | |
57 | | void inv_txfm_dct2_size8_c(const int *src, int *dst, int shift, int line, |
58 | | int skip_line, int zero_line, const int coef_min, |
59 | 0 | const int coef_max) { |
60 | 0 | (void)zero_line; |
61 | 0 | int j, k; |
62 | 0 | int a[4], b[4]; |
63 | 0 | int c[2], d[2]; |
64 | 0 | int add = 1 << (shift - 1); |
65 | |
|
66 | 0 | const int *tx_mat = tx_kernel_dct2_size8[INV_TXFM][0]; |
67 | |
|
68 | 0 | const int nz_line = line - skip_line; |
69 | 0 | for (j = 0; j < nz_line; j++) { |
70 | 0 | for (k = 0; k < 4; k++) { |
71 | 0 | b[k] = tx_mat[1 * 8 + k] * src[1] + tx_mat[3 * 8 + k] * src[3] + |
72 | 0 | tx_mat[5 * 8 + k] * src[5] + tx_mat[7 * 8 + k] * src[7]; |
73 | 0 | } |
74 | |
|
75 | 0 | d[0] = tx_mat[2 * 8 + 0] * src[2] + tx_mat[6 * 8 + 0] * src[6]; |
76 | 0 | d[1] = tx_mat[2 * 8 + 1] * src[2] + tx_mat[6 * 8 + 1] * src[6]; |
77 | 0 | c[0] = tx_mat[0 * 8 + 0] * src[0] + tx_mat[4 * 8 + 0] * src[4]; |
78 | 0 | c[1] = tx_mat[0 * 8 + 1] * src[0] + tx_mat[4 * 8 + 1] * src[4]; |
79 | |
|
80 | 0 | a[0] = c[0] + d[0]; |
81 | 0 | a[3] = c[0] - d[0]; |
82 | 0 | a[1] = c[1] + d[1]; |
83 | 0 | a[2] = c[1] - d[1]; |
84 | |
|
85 | 0 | for (k = 0; k < 4; k++) { |
86 | 0 | dst[(k)*line] = clamp((a[k] + b[k] + add) >> shift, coef_min, coef_max); |
87 | 0 | dst[(k + 4) * line] = |
88 | 0 | clamp((a[3 - k] - b[3 - k] + add) >> shift, coef_min, coef_max); |
89 | 0 | } |
90 | 0 | src += 8; |
91 | 0 | dst++; |
92 | 0 | } |
93 | 0 | } |
94 | | |
95 | | void inv_txfm_dct2_size16_c(const int *src, int *dst, int shift, int line, |
96 | | int skip_line, int zero_line, const int coef_min, |
97 | 0 | const int coef_max) { |
98 | 0 | (void)zero_line; |
99 | 0 | int j, k; |
100 | 0 | int a[8], b[8]; |
101 | 0 | int c[4], d[4]; |
102 | 0 | int e[2], f[2]; |
103 | 0 | int add = 1 << (shift - 1); |
104 | |
|
105 | 0 | const int *tx_mat = tx_kernel_dct2_size16[INV_TXFM][0]; |
106 | |
|
107 | 0 | const int nz_line = line - skip_line; |
108 | |
|
109 | 0 | for (j = 0; j < nz_line; j++) { |
110 | 0 | for (k = 0; k < 8; k++) { |
111 | 0 | b[k] = tx_mat[1 * 16 + k] * src[1] + tx_mat[3 * 16 + k] * src[3] + |
112 | 0 | tx_mat[5 * 16 + k] * src[5] + tx_mat[7 * 16 + k] * src[7] + |
113 | 0 | tx_mat[9 * 16 + k] * src[9] + tx_mat[11 * 16 + k] * src[11] + |
114 | 0 | tx_mat[13 * 16 + k] * src[13] + tx_mat[15 * 16 + k] * src[15]; |
115 | 0 | } |
116 | 0 | for (k = 0; k < 4; k++) { |
117 | 0 | d[k] = tx_mat[2 * 16 + k] * src[2] + tx_mat[6 * 16 + k] * src[6] + |
118 | 0 | tx_mat[10 * 16 + k] * src[10] + tx_mat[14 * 16 + k] * src[14]; |
119 | 0 | } |
120 | 0 | f[0] = tx_mat[4 * 16] * src[4] + tx_mat[12 * 16] * src[12]; |
121 | 0 | e[0] = tx_mat[0] * src[0] + tx_mat[8 * 16] * src[8]; |
122 | 0 | f[1] = tx_mat[4 * 16 + 1] * src[4] + tx_mat[12 * 16 + 1] * src[12]; |
123 | 0 | e[1] = tx_mat[0 * 16 + 1] * src[0] + tx_mat[8 * 16 + 1] * src[8]; |
124 | 0 | for (k = 0; k < 2; k++) { |
125 | 0 | c[k] = e[k] + f[k]; |
126 | 0 | c[k + 2] = e[1 - k] - f[1 - k]; |
127 | 0 | } |
128 | 0 | for (k = 0; k < 4; k++) { |
129 | 0 | a[k] = c[k] + d[k]; |
130 | 0 | a[k + 4] = c[3 - k] - d[3 - k]; |
131 | 0 | } |
132 | 0 | for (k = 0; k < 8; k++) { |
133 | 0 | dst[(k)*line] = clamp((a[k] + b[k] + add) >> shift, coef_min, coef_max); |
134 | 0 | dst[(k + 8) * line] = |
135 | 0 | clamp((a[7 - k] - b[7 - k] + add) >> shift, coef_min, coef_max); |
136 | 0 | } |
137 | 0 | src += 16; |
138 | 0 | dst++; |
139 | 0 | } |
140 | 0 | } |
141 | | |
142 | | void inv_txfm_dct2_size32_c(const int *src, int *dst, int shift, int line, |
143 | | int skip_line, int zero_line, const int coef_min, |
144 | 0 | const int coef_max) { |
145 | 0 | (void)zero_line; |
146 | 0 | int j, k; |
147 | 0 | int a[16], b[16]; |
148 | 0 | int c[8], d[8]; |
149 | 0 | int e[4], f[4]; |
150 | 0 | int g[2], h[2]; |
151 | 0 | int add = 1 << (shift - 1); |
152 | |
|
153 | 0 | const int *tx_mat = tx_kernel_dct2_size32[INV_TXFM][0]; |
154 | |
|
155 | 0 | const int nz_line = line - skip_line; |
156 | |
|
157 | 0 | for (j = 0; j < nz_line; j++) { |
158 | 0 | for (k = 0; k < 16; k++) { |
159 | 0 | b[k] = tx_mat[1 * 32 + k] * src[1] + tx_mat[3 * 32 + k] * src[3] + |
160 | 0 | tx_mat[5 * 32 + k] * src[5] + tx_mat[7 * 32 + k] * src[7] + |
161 | 0 | tx_mat[9 * 32 + k] * src[9] + tx_mat[11 * 32 + k] * src[11] + |
162 | 0 | tx_mat[13 * 32 + k] * src[13] + tx_mat[15 * 32 + k] * src[15] + |
163 | 0 | tx_mat[17 * 32 + k] * src[17] + tx_mat[19 * 32 + k] * src[19] + |
164 | 0 | tx_mat[21 * 32 + k] * src[21] + tx_mat[23 * 32 + k] * src[23] + |
165 | 0 | tx_mat[25 * 32 + k] * src[25] + tx_mat[27 * 32 + k] * src[27] + |
166 | 0 | tx_mat[29 * 32 + k] * src[29] + tx_mat[31 * 32 + k] * src[31]; |
167 | 0 | } |
168 | |
|
169 | 0 | for (k = 0; k < 8; k++) { |
170 | 0 | d[k] = tx_mat[2 * 32 + k] * src[2] + tx_mat[6 * 32 + k] * src[6] + |
171 | 0 | tx_mat[10 * 32 + k] * src[10] + tx_mat[14 * 32 + k] * src[14] + |
172 | 0 | tx_mat[18 * 32 + k] * src[18] + tx_mat[22 * 32 + k] * src[22] + |
173 | 0 | tx_mat[26 * 32 + k] * src[26] + tx_mat[30 * 32 + k] * src[30]; |
174 | 0 | } |
175 | 0 | for (k = 0; k < 4; k++) { |
176 | 0 | f[k] = tx_mat[4 * 32 + k] * src[4] + tx_mat[12 * 32 + k] * src[12] + |
177 | 0 | tx_mat[20 * 32 + k] * src[20] + tx_mat[28 * 32 + k] * src[28]; |
178 | 0 | } |
179 | 0 | h[0] = tx_mat[8 * 32 + 0] * src[8] + tx_mat[24 * 32 + 0] * src[24]; |
180 | 0 | h[1] = tx_mat[8 * 32 + 1] * src[8] + tx_mat[24 * 32 + 1] * src[24]; |
181 | 0 | g[0] = tx_mat[0 * 32 + 0] * src[0] + tx_mat[16 * 32 + 0] * src[16]; |
182 | 0 | g[1] = tx_mat[0 * 32 + 1] * src[0] + tx_mat[16 * 32 + 1] * src[16]; |
183 | |
|
184 | 0 | e[0] = g[0] + h[0]; |
185 | 0 | e[3] = g[0] - h[0]; |
186 | 0 | e[1] = g[1] + h[1]; |
187 | 0 | e[2] = g[1] - h[1]; |
188 | 0 | for (k = 0; k < 4; k++) { |
189 | 0 | c[k] = e[k] + f[k]; |
190 | 0 | c[k + 4] = e[3 - k] - f[3 - k]; |
191 | 0 | } |
192 | 0 | for (k = 0; k < 8; k++) { |
193 | 0 | a[k] = c[k] + d[k]; |
194 | 0 | a[k + 8] = c[7 - k] - d[7 - k]; |
195 | 0 | } |
196 | 0 | for (k = 0; k < 16; k++) { |
197 | 0 | dst[(k)*line] = clamp((a[k] + b[k] + add) >> shift, coef_min, coef_max); |
198 | 0 | dst[(k + 16) * line] = |
199 | 0 | clamp((a[15 - k] - b[15 - k] + add) >> shift, coef_min, coef_max); |
200 | 0 | } |
201 | 0 | src += 32; |
202 | 0 | dst++; |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | | // ********************************** IDTX ********************************** |
207 | | void inv_txfm_idtx_size4_c(const int *src, int *dst, int shift, int line, |
208 | | int skip_line, int zero_line, const int coef_min, |
209 | 0 | const int coef_max) { |
210 | 0 | (void)zero_line; |
211 | 0 | const int offset = 1 << (shift - 1); |
212 | 0 | const int nz_line = line - skip_line; |
213 | 0 | const int tx1d_size = 4; |
214 | 0 | const int scale = 128; |
215 | |
|
216 | 0 | for (int i = 0; i < nz_line; i++) { |
217 | 0 | for (int j = 0; j < tx1d_size; j++) { |
218 | 0 | dst[j * line + i] = |
219 | 0 | clamp((int)(src[i * tx1d_size + j] * scale + offset) >> shift, |
220 | 0 | coef_min, coef_max); |
221 | 0 | } |
222 | 0 | } |
223 | 0 | } |
224 | | |
225 | | void inv_txfm_idtx_size8_c(const int *src, int *dst, int shift, int line, |
226 | | int skip_line, int zero_line, const int coef_min, |
227 | 0 | const int coef_max) { |
228 | 0 | (void)zero_line; |
229 | 0 | const int offset = 1 << (shift - 1); |
230 | 0 | const int nz_line = line - skip_line; |
231 | 0 | const int tx1d_size = 8; |
232 | 0 | const int scale = 181; |
233 | |
|
234 | 0 | for (int i = 0; i < nz_line; i++) { |
235 | 0 | for (int j = 0; j < tx1d_size; j++) { |
236 | 0 | dst[j * line + i] = |
237 | 0 | clamp((int)(src[i * tx1d_size + j] * scale + offset) >> shift, |
238 | 0 | coef_min, coef_max); |
239 | 0 | } |
240 | 0 | } |
241 | 0 | } |
242 | | |
243 | | void inv_txfm_idtx_size16_c(const int *src, int *dst, int shift, int line, |
244 | | int skip_line, int zero_line, const int coef_min, |
245 | 0 | const int coef_max) { |
246 | 0 | (void)zero_line; |
247 | 0 | const int offset = 1 << (shift - 1); |
248 | 0 | const int nz_line = line - skip_line; |
249 | 0 | const int tx1d_size = 16; |
250 | 0 | const int scale = 256; |
251 | |
|
252 | 0 | for (int i = 0; i < nz_line; i++) { |
253 | 0 | for (int j = 0; j < tx1d_size; j++) { |
254 | 0 | dst[j * line + i] = |
255 | 0 | clamp((int)(src[i * tx1d_size + j] * scale + offset) >> shift, |
256 | 0 | coef_min, coef_max); |
257 | 0 | } |
258 | 0 | } |
259 | 0 | } |
260 | | |
261 | | void inv_txfm_idtx_size32_c(const int *src, int *dst, int shift, int line, |
262 | | int skip_line, int zero_line, const int coef_min, |
263 | 0 | const int coef_max) { |
264 | 0 | (void)zero_line; |
265 | 0 | const int offset = 1 << (shift - 1); |
266 | 0 | const int nz_line = line - skip_line; |
267 | 0 | const int tx1d_size = 32; |
268 | 0 | const int scale = 362; |
269 | |
|
270 | 0 | for (int i = 0; i < nz_line; i++) { |
271 | 0 | for (int j = 0; j < tx1d_size; j++) { |
272 | 0 | dst[j * line + i] = |
273 | 0 | clamp((int)(src[i * tx1d_size + j] * scale + offset) >> shift, |
274 | 0 | coef_min, coef_max); |
275 | 0 | } |
276 | 0 | } |
277 | 0 | } |
278 | | |
279 | | void inv_txfm_adst_size4_c(const int *src, int *dst, int shift, int line, |
280 | | int skip_line, int zero_line, const int coef_min, |
281 | 0 | const int coef_max) { |
282 | 0 | (void)zero_line; |
283 | 0 | const int offset = 1 << (shift - 1); |
284 | 0 | const int nz_line = line - skip_line; |
285 | 0 | const int tx1d_size = 4; |
286 | 0 | const int *tx_mat = tx_kernel_adst_size4[INV_TXFM][0]; |
287 | |
|
288 | 0 | for (int i = 0; i < nz_line; i++) { |
289 | 0 | for (int j = 0; j < tx1d_size; j++) { |
290 | 0 | int sum = 0; |
291 | 0 | for (int k = 0; k < tx1d_size; k++) { |
292 | 0 | sum += src[i * tx1d_size + k] * tx_mat[k * tx1d_size + j]; |
293 | 0 | } |
294 | 0 | dst[j * line + i] = |
295 | 0 | clamp((int)(sum + offset) >> shift, coef_min, coef_max); |
296 | 0 | } |
297 | 0 | } |
298 | 0 | } |
299 | | |
300 | | void inv_txfm_fdst_size4_c(const int *src, int *dst, int shift, int line, |
301 | | int skip_line, int zero_line, const int coef_min, |
302 | 0 | const int coef_max) { |
303 | 0 | (void)zero_line; |
304 | 0 | const int offset = 1 << (shift - 1); |
305 | 0 | const int nz_line = line - skip_line; |
306 | 0 | const int tx1d_size = 4; |
307 | 0 | const int *tx_mat = tx_kernel_fdst_size4[INV_TXFM][0]; |
308 | |
|
309 | 0 | for (int i = 0; i < nz_line; i++) { |
310 | 0 | for (int j = 0; j < tx1d_size; j++) { |
311 | 0 | int sum = 0; |
312 | 0 | for (int k = 0; k < tx1d_size; k++) { |
313 | 0 | sum += src[i * tx1d_size + k] * tx_mat[k * tx1d_size + j]; |
314 | 0 | } |
315 | 0 | dst[j * line + i] = |
316 | 0 | clamp((int)(sum + offset) >> shift, coef_min, coef_max); |
317 | 0 | } |
318 | 0 | } |
319 | 0 | } |
320 | | |
321 | | void inv_txfm_adst_size8_c(const int *src, int *dst, int shift, int line, |
322 | | int skip_line, int zero_line, const int coef_min, |
323 | 0 | const int coef_max) { |
324 | 0 | (void)zero_line; |
325 | 0 | const int offset = 1 << (shift - 1); |
326 | 0 | const int nz_line = line - skip_line; |
327 | 0 | const int tx1d_size = 8; |
328 | 0 | const int *tx_mat = tx_kernel_adst_size8[INV_TXFM][0]; |
329 | |
|
330 | 0 | for (int i = 0; i < nz_line; i++) { |
331 | 0 | for (int j = 0; j < tx1d_size; j++) { |
332 | 0 | int sum = 0; |
333 | 0 | for (int k = 0; k < tx1d_size; k++) { |
334 | 0 | sum += src[i * tx1d_size + k] * tx_mat[k * tx1d_size + j]; |
335 | 0 | } |
336 | 0 | dst[j * line + i] = |
337 | 0 | clamp((int)(sum + offset) >> shift, coef_min, coef_max); |
338 | 0 | } |
339 | 0 | } |
340 | 0 | } |
341 | | |
342 | | void inv_txfm_fdst_size8_c(const int *src, int *dst, int shift, int line, |
343 | | int skip_line, int zero_line, const int coef_min, |
344 | 0 | const int coef_max) { |
345 | 0 | (void)zero_line; |
346 | 0 | const int offset = 1 << (shift - 1); |
347 | 0 | const int nz_line = line - skip_line; |
348 | 0 | const int tx1d_size = 8; |
349 | 0 | const int *tx_mat = tx_kernel_fdst_size8[INV_TXFM][0]; |
350 | |
|
351 | 0 | for (int i = 0; i < nz_line; i++) { |
352 | 0 | for (int j = 0; j < tx1d_size; j++) { |
353 | 0 | int sum = 0; |
354 | 0 | for (int k = 0; k < tx1d_size; k++) { |
355 | 0 | sum += src[i * tx1d_size + k] * tx_mat[k * tx1d_size + j]; |
356 | 0 | } |
357 | 0 | dst[j * line + i] = |
358 | 0 | clamp((int)(sum + offset) >> shift, coef_min, coef_max); |
359 | 0 | } |
360 | 0 | } |
361 | 0 | } |
362 | | |
363 | | void inv_txfm_adst_size16_c(const int *src, int *dst, int shift, int line, |
364 | | int skip_line, int zero_line, const int coef_min, |
365 | 0 | const int coef_max) { |
366 | 0 | (void)zero_line; |
367 | 0 | const int offset = 1 << (shift - 1); |
368 | 0 | const int nz_line = line - skip_line; |
369 | 0 | const int tx1d_size = 16; |
370 | 0 | const int *tx_mat = tx_kernel_adst_size16[INV_TXFM][0]; |
371 | |
|
372 | 0 | for (int i = 0; i < nz_line; i++) { |
373 | 0 | for (int j = 0; j < tx1d_size; j++) { |
374 | 0 | int sum = 0; |
375 | 0 | for (int k = 0; k < tx1d_size; k++) { |
376 | 0 | sum += src[i * tx1d_size + k] * tx_mat[k * tx1d_size + j]; |
377 | 0 | } |
378 | 0 | dst[j * line + i] = |
379 | 0 | clamp((int)(sum + offset) >> shift, coef_min, coef_max); |
380 | 0 | } |
381 | 0 | } |
382 | 0 | } |
383 | | |
384 | | void inv_txfm_fdst_size16_c(const int *src, int *dst, int shift, int line, |
385 | | int skip_line, int zero_line, const int coef_min, |
386 | 0 | const int coef_max) { |
387 | 0 | (void)zero_line; |
388 | 0 | const int offset = 1 << (shift - 1); |
389 | 0 | const int nz_line = line - skip_line; |
390 | 0 | const int tx1d_size = 16; |
391 | 0 | const int *tx_mat = tx_kernel_fdst_size16[INV_TXFM][0]; |
392 | |
|
393 | 0 | for (int i = 0; i < nz_line; i++) { |
394 | 0 | for (int j = 0; j < tx1d_size; j++) { |
395 | 0 | int sum = 0; |
396 | 0 | for (int k = 0; k < tx1d_size; k++) { |
397 | 0 | sum += src[i * tx1d_size + k] * tx_mat[k * tx1d_size + j]; |
398 | 0 | } |
399 | 0 | dst[j * line + i] = |
400 | 0 | clamp((int)(sum + offset) >> shift, coef_min, coef_max); |
401 | 0 | } |
402 | 0 | } |
403 | 0 | } |
404 | | |
405 | | void inv_txfm_ddtx_size4_c(const int *src, int *dst, int shift, int line, |
406 | | int skip_line, int zero_line, const int coef_min, |
407 | 0 | const int coef_max) { |
408 | 0 | (void)zero_line; |
409 | 0 | const int offset = 1 << (shift - 1); |
410 | 0 | const int nz_line = line - skip_line; |
411 | 0 | const int tx1d_size = 4; |
412 | 0 | const int *tx_mat = tx_kernel_ddtx_size4[INV_TXFM][0]; |
413 | |
|
414 | 0 | for (int i = 0; i < nz_line; i++) { |
415 | 0 | for (int j = 0; j < tx1d_size; j++) { |
416 | 0 | int sum = 0; |
417 | 0 | for (int k = 0; k < tx1d_size; k++) { |
418 | 0 | sum += src[i * tx1d_size + k] * tx_mat[k * tx1d_size + j]; |
419 | 0 | } |
420 | 0 | dst[j * line + i] = |
421 | 0 | clamp((int)(sum + offset) >> shift, coef_min, coef_max); |
422 | 0 | } |
423 | 0 | } |
424 | 0 | } |
425 | | |
426 | | void inv_txfm_ddtx_size8_c(const int *src, int *dst, int shift, int line, |
427 | | int skip_line, int zero_line, const int coef_min, |
428 | 0 | const int coef_max) { |
429 | 0 | (void)zero_line; |
430 | 0 | const int offset = 1 << (shift - 1); |
431 | 0 | const int nz_line = line - skip_line; |
432 | 0 | const int tx1d_size = 8; |
433 | 0 | const int *tx_mat = tx_kernel_ddtx_size8[INV_TXFM][0]; |
434 | |
|
435 | 0 | for (int i = 0; i < nz_line; i++) { |
436 | 0 | for (int j = 0; j < tx1d_size; j++) { |
437 | 0 | int sum = 0; |
438 | 0 | for (int k = 0; k < tx1d_size; k++) { |
439 | 0 | sum += src[i * tx1d_size + k] * tx_mat[k * tx1d_size + j]; |
440 | 0 | } |
441 | 0 | dst[j * line + i] = |
442 | 0 | clamp((int)(sum + offset) >> shift, coef_min, coef_max); |
443 | 0 | } |
444 | 0 | } |
445 | 0 | } |
446 | | |
447 | | void inv_txfm_ddtx_size16_c(const int *src, int *dst, int shift, int line, |
448 | | int skip_line, int zero_line, const int coef_min, |
449 | 0 | const int coef_max) { |
450 | 0 | (void)zero_line; |
451 | 0 | const int offset = 1 << (shift - 1); |
452 | 0 | const int nz_line = line - skip_line; |
453 | 0 | const int tx1d_size = 16; |
454 | 0 | const int *tx_mat = tx_kernel_ddtx_size16[INV_TXFM][0]; |
455 | |
|
456 | 0 | for (int i = 0; i < nz_line; i++) { |
457 | 0 | for (int j = 0; j < tx1d_size; j++) { |
458 | 0 | int sum = 0; |
459 | 0 | for (int k = 0; k < tx1d_size; k++) { |
460 | 0 | sum += src[i * tx1d_size + k] * tx_mat[k * tx1d_size + j]; |
461 | 0 | } |
462 | 0 | dst[j * line + i] = |
463 | 0 | clamp((int)(sum + offset) >> shift, coef_min, coef_max); |
464 | 0 | } |
465 | 0 | } |
466 | 0 | } |
467 | | |
468 | | void inv_txfm_fddt_size4_c(const int *src, int *dst, int shift, int line, |
469 | | int skip_line, int zero_line, const int coef_min, |
470 | 0 | const int coef_max) { |
471 | 0 | (void)zero_line; |
472 | 0 | const int offset = 1 << (shift - 1); |
473 | 0 | const int nz_line = line - skip_line; |
474 | 0 | const int tx1d_size = 4; |
475 | 0 | const int *tx_mat = tx_kernel_ddtx_size4[INV_TXFM][0]; |
476 | |
|
477 | 0 | for (int i = 0; i < nz_line; i++) { |
478 | 0 | for (int j = 0; j < tx1d_size; j++) { |
479 | 0 | int sum = 0; |
480 | 0 | for (int k = 0; k < tx1d_size; k++) { |
481 | 0 | sum += src[i * tx1d_size + k] * |
482 | 0 | tx_mat[k * tx1d_size + (tx1d_size - 1 - j)]; |
483 | 0 | } |
484 | 0 | dst[j * line + i] = |
485 | 0 | clamp((int)(sum + offset) >> shift, coef_min, coef_max); |
486 | 0 | } |
487 | 0 | } |
488 | 0 | } |
489 | | |
490 | | void inv_txfm_fddt_size8_c(const int *src, int *dst, int shift, int line, |
491 | | int skip_line, int zero_line, const int coef_min, |
492 | 0 | const int coef_max) { |
493 | 0 | (void)zero_line; |
494 | 0 | const int offset = 1 << (shift - 1); |
495 | 0 | const int nz_line = line - skip_line; |
496 | 0 | const int tx1d_size = 8; |
497 | 0 | const int *tx_mat = tx_kernel_ddtx_size8[INV_TXFM][0]; |
498 | |
|
499 | 0 | for (int i = 0; i < nz_line; i++) { |
500 | 0 | for (int j = 0; j < tx1d_size; j++) { |
501 | 0 | int sum = 0; |
502 | 0 | for (int k = 0; k < tx1d_size; k++) { |
503 | 0 | sum += src[i * tx1d_size + k] * |
504 | 0 | tx_mat[k * tx1d_size + (tx1d_size - 1 - j)]; |
505 | 0 | } |
506 | 0 | dst[j * line + i] = |
507 | 0 | clamp((int)(sum + offset) >> shift, coef_min, coef_max); |
508 | 0 | } |
509 | 0 | } |
510 | 0 | } |
511 | | |
512 | | void inv_txfm_fddt_size16_c(const int *src, int *dst, int shift, int line, |
513 | | int skip_line, int zero_line, const int coef_min, |
514 | 0 | const int coef_max) { |
515 | 0 | (void)zero_line; |
516 | 0 | const int offset = 1 << (shift - 1); |
517 | 0 | const int nz_line = line - skip_line; |
518 | 0 | const int tx1d_size = 16; |
519 | 0 | const int *tx_mat = tx_kernel_ddtx_size16[INV_TXFM][0]; |
520 | |
|
521 | 0 | for (int i = 0; i < nz_line; i++) { |
522 | 0 | for (int j = 0; j < tx1d_size; j++) { |
523 | 0 | int sum = 0; |
524 | 0 | for (int k = 0; k < tx1d_size; k++) { |
525 | 0 | sum += src[i * tx1d_size + k] * |
526 | 0 | tx_mat[k * tx1d_size + (tx1d_size - 1 - j)]; |
527 | 0 | } |
528 | 0 | dst[j * line + i] = |
529 | 0 | clamp((int)(sum + offset) >> shift, coef_min, coef_max); |
530 | 0 | } |
531 | 0 | } |
532 | 0 | } |
533 | | |
534 | | void inv_transform_1d_c(const int *src, int *dst, int shift, int line, |
535 | | int skip_line, int zero_line, const int coef_min, |
536 | | const int coef_max, const int tx_type_index, |
537 | 0 | const int size_index) { |
538 | 0 | switch (size_index) { |
539 | 0 | case 0: |
540 | 0 | switch (tx_type_index) { |
541 | 0 | case 0: |
542 | 0 | inv_txfm_dct2_size4_c(src, dst, shift, line, skip_line, zero_line, |
543 | 0 | coef_min, coef_max); |
544 | 0 | break; |
545 | 0 | case 1: |
546 | 0 | inv_txfm_idtx_size4_c(src, dst, shift, line, skip_line, zero_line, |
547 | 0 | coef_min, coef_max); |
548 | 0 | break; |
549 | 0 | case 2: |
550 | 0 | inv_txfm_adst_size4_c(src, dst, shift, line, skip_line, zero_line, |
551 | 0 | coef_min, coef_max); |
552 | 0 | break; |
553 | 0 | case 3: |
554 | 0 | inv_txfm_fdst_size4_c(src, dst, shift, line, skip_line, zero_line, |
555 | 0 | coef_min, coef_max); |
556 | 0 | break; |
557 | 0 | case 4: |
558 | 0 | inv_txfm_ddtx_size4_c(src, dst, shift, line, skip_line, zero_line, |
559 | 0 | coef_min, coef_max); |
560 | 0 | break; |
561 | 0 | case 5: |
562 | 0 | inv_txfm_fddt_size4_c(src, dst, shift, line, skip_line, zero_line, |
563 | 0 | coef_min, coef_max); |
564 | 0 | break; |
565 | 0 | default: assert(0); break; |
566 | 0 | } |
567 | 0 | break; |
568 | 0 | case 1: |
569 | 0 | switch (tx_type_index) { |
570 | 0 | case 0: |
571 | 0 | inv_txfm_dct2_size8_c(src, dst, shift, line, skip_line, zero_line, |
572 | 0 | coef_min, coef_max); |
573 | 0 | break; |
574 | 0 | case 1: |
575 | 0 | inv_txfm_idtx_size8_c(src, dst, shift, line, skip_line, zero_line, |
576 | 0 | coef_min, coef_max); |
577 | 0 | break; |
578 | 0 | case 2: |
579 | 0 | inv_txfm_adst_size8_c(src, dst, shift, line, skip_line, zero_line, |
580 | 0 | coef_min, coef_max); |
581 | 0 | break; |
582 | 0 | case 3: |
583 | 0 | inv_txfm_fdst_size8_c(src, dst, shift, line, skip_line, zero_line, |
584 | 0 | coef_min, coef_max); |
585 | 0 | break; |
586 | 0 | case 4: |
587 | 0 | inv_txfm_ddtx_size8_c(src, dst, shift, line, skip_line, zero_line, |
588 | 0 | coef_min, coef_max); |
589 | 0 | break; |
590 | 0 | case 5: |
591 | 0 | inv_txfm_fddt_size8_c(src, dst, shift, line, skip_line, zero_line, |
592 | 0 | coef_min, coef_max); |
593 | 0 | break; |
594 | 0 | default: assert(0); break; |
595 | 0 | } |
596 | 0 | break; |
597 | 0 | case 2: |
598 | 0 | switch (tx_type_index) { |
599 | 0 | case 0: |
600 | 0 | inv_txfm_dct2_size16_c(src, dst, shift, line, skip_line, zero_line, |
601 | 0 | coef_min, coef_max); |
602 | 0 | break; |
603 | 0 | case 1: |
604 | 0 | inv_txfm_idtx_size16_c(src, dst, shift, line, skip_line, zero_line, |
605 | 0 | coef_min, coef_max); |
606 | 0 | break; |
607 | 0 | case 2: |
608 | 0 | inv_txfm_adst_size16_c(src, dst, shift, line, skip_line, zero_line, |
609 | 0 | coef_min, coef_max); |
610 | 0 | break; |
611 | 0 | case 3: |
612 | 0 | inv_txfm_fdst_size16_c(src, dst, shift, line, skip_line, zero_line, |
613 | 0 | coef_min, coef_max); |
614 | 0 | break; |
615 | 0 | case 4: |
616 | 0 | inv_txfm_ddtx_size16_c(src, dst, shift, line, skip_line, zero_line, |
617 | 0 | coef_min, coef_max); |
618 | 0 | break; |
619 | 0 | case 5: |
620 | 0 | inv_txfm_fddt_size16_c(src, dst, shift, line, skip_line, zero_line, |
621 | 0 | coef_min, coef_max); |
622 | 0 | break; |
623 | 0 | default: assert(0); break; |
624 | 0 | } |
625 | 0 | break; |
626 | 0 | case 3: |
627 | 0 | switch (tx_type_index) { |
628 | 0 | case 0: |
629 | 0 | inv_txfm_dct2_size32_c(src, dst, shift, line, skip_line, zero_line, |
630 | 0 | coef_min, coef_max); |
631 | 0 | break; |
632 | 0 | case 1: |
633 | 0 | inv_txfm_idtx_size32_c(src, dst, shift, line, skip_line, zero_line, |
634 | 0 | coef_min, coef_max); |
635 | 0 | break; |
636 | 0 | default: assert(0); break; |
637 | 0 | } |
638 | 0 | break; |
639 | 0 | default: assert(0); break; |
640 | 0 | } |
641 | 0 | } |
642 | | |
643 | | void inv_txfm_c(const tran_low_t *input, uint16_t *dest, int stride, |
644 | 0 | const TxfmParam *txfm_param) { |
645 | 0 | const TX_SIZE tx_size = txfm_param->tx_size; |
646 | 0 | TX_TYPE tx_type = txfm_param->tx_type; |
647 | |
|
648 | 0 | int width = AVMMIN(MAX_TX_SIZE >> 1, tx_size_wide[tx_size]); |
649 | 0 | int height = AVMMIN(MAX_TX_SIZE >> 1, tx_size_high[tx_size]); |
650 | 0 | const uint32_t tx_wide_index = |
651 | 0 | AVMMIN(MAX_TX_SIZE_LOG2 - 1, tx_size_wide_log2[tx_size]) - 2; |
652 | 0 | const uint32_t tx_high_index = |
653 | 0 | AVMMIN(MAX_TX_SIZE_LOG2 - 1, tx_size_high_log2[tx_size]) - 2; |
654 | |
|
655 | 0 | const int intermediate_bitdepth = txfm_param->bd + 8; |
656 | 0 | const int rng_min = -(1 << (intermediate_bitdepth - 1)); |
657 | 0 | const int rng_max = (1 << (intermediate_bitdepth - 1)) - 1; |
658 | |
|
659 | 0 | const int col_rng_min = -(1 << txfm_param->bd); |
660 | 0 | const int col_rng_max = (1 << txfm_param->bd) - 1; |
661 | |
|
662 | 0 | if (txfm_param->lossless) { |
663 | 0 | assert(tx_type == DCT_DCT); |
664 | 0 | av2_highbd_iwht4x4_add(input, dest, stride, txfm_param->eob, |
665 | 0 | txfm_param->bd); |
666 | 0 | return; |
667 | 0 | } |
668 | | |
669 | 0 | int tx_type_row = g_hor_tx_type[tx_type]; |
670 | 0 | int tx_type_col = g_ver_tx_type[tx_type]; |
671 | |
|
672 | 0 | if (txfm_param->use_ddt) { |
673 | 0 | const int use_ddt_row = (width == 4 && REPLACE_ADST4) || |
674 | 0 | (width == 8 && REPLACE_ADST8) || |
675 | 0 | (width == 16 && REPLACE_ADST16); |
676 | 0 | if (use_ddt_row && (tx_type_row == DST7 || tx_type_row == DCT8)) { |
677 | 0 | tx_type_row = (tx_type_row == DST7) ? DDTX : FDDT; |
678 | 0 | } |
679 | 0 | const int use_ddt_col = (height == 4 && REPLACE_ADST4) || |
680 | 0 | (height == 8 && REPLACE_ADST8) || |
681 | 0 | (height == 16 && REPLACE_ADST16); |
682 | 0 | if (use_ddt_col && (tx_type_col == DST7 || tx_type_col == DCT8)) { |
683 | 0 | tx_type_col = (tx_type_col == DST7) ? DDTX : FDDT; |
684 | 0 | } |
685 | 0 | } |
686 | |
|
687 | 0 | int skipWidth = width > 32 ? width - 32 : 0; |
688 | 0 | int skipHeight = height > 32 ? height - 32 : 0; |
689 | |
|
690 | 0 | int block[MAX_TX_SQUARE]; |
691 | 0 | int tmp[MAX_TX_SQUARE]; |
692 | |
|
693 | 0 | const int log2width = tx_size_wide_log2[tx_size]; |
694 | 0 | const int log2height = tx_size_high_log2[tx_size]; |
695 | 0 | const int sqrt2 = ((log2width + log2height) & 1) ? 1 : 0; |
696 | | |
697 | | // This assert is required to silence the static analyzer warnings. |
698 | 0 | assert(width * height > 0); |
699 | |
|
700 | 0 | if (sqrt2) { |
701 | 0 | for (int i = 0; i < AVMMIN(1024, width * height); i++) { |
702 | 0 | tmp[i] = round_shift((int64_t)input[i] * NewInvSqrt2, NewSqrt2Bits); |
703 | 0 | } |
704 | 0 | } else { |
705 | 0 | memcpy(tmp, input, AVMMIN(1024, width * height) * sizeof(tran_low_t)); |
706 | 0 | } |
707 | |
|
708 | 0 | memcpy(block, tmp, AVMMIN(1024, width * height) * sizeof(*tmp)); |
709 | |
|
710 | 0 | clamp_buf(block, AVMMIN(1024, width * height), txfm_param->bd + 8); |
711 | |
|
712 | 0 | if (skipWidth) { |
713 | 0 | for (int y = 0; y < height; y++) { |
714 | 0 | memcpy(block + y * width, tmp + y * 32, 32 * sizeof(*tmp)); |
715 | 0 | } |
716 | 0 | } |
717 | |
|
718 | 0 | const int shift_1st = inv_tx_shift[tx_size][0]; |
719 | 0 | const int shift_2nd = inv_tx_shift[tx_size][1]; |
720 | |
|
721 | 0 | assert(shift_1st >= 0); |
722 | 0 | assert(shift_2nd >= 0); |
723 | |
|
724 | 0 | inv_transform_1d_c(block, tmp, shift_1st, height, skipHeight, skipWidth, |
725 | 0 | rng_min, rng_max, tx_type_row, tx_wide_index); |
726 | |
|
727 | 0 | inv_transform_1d_c(tmp, block, shift_2nd, width, 0, skipHeight, col_rng_min, |
728 | 0 | col_rng_max, tx_type_col, tx_high_index); |
729 | |
|
730 | 0 | if (width < tx_size_wide[tx_size]) { |
731 | 0 | assert(width == 32); |
732 | 0 | memcpy(tmp, block, width * height * sizeof(*block)); |
733 | 0 | for (int y = 0; y < height; y++) { |
734 | 0 | for (int x = 0; x < width; x++) { |
735 | 0 | block[y * 2 * width + 2 * x] = tmp[y * width + x]; |
736 | 0 | block[y * 2 * width + 2 * x + 1] = tmp[y * width + x]; |
737 | 0 | } |
738 | 0 | } |
739 | 0 | width = tx_size_wide[tx_size]; |
740 | 0 | } |
741 | 0 | if (height < tx_size_high[tx_size]) { |
742 | 0 | assert(height == 32); |
743 | 0 | memcpy(tmp, block, width * height * sizeof(*block)); |
744 | 0 | for (int y = 0; y < height; y++) { |
745 | 0 | for (int x = 0; x < width; x++) { |
746 | 0 | block[2 * y * width + x] = tmp[y * width + x]; |
747 | 0 | block[(2 * y + 1) * width + x] = tmp[y * width + x]; |
748 | 0 | } |
749 | 0 | } |
750 | 0 | height = tx_size_high[tx_size]; |
751 | 0 | } |
752 | |
|
753 | 0 | for (int y = 0; y < height; y++) { |
754 | 0 | for (int x = 0; x < width; x++) { |
755 | 0 | dest[y * stride + x] = highbd_clip_pixel_add( |
756 | 0 | dest[y * stride + x], block[(y * width) + x], txfm_param->bd); |
757 | 0 | } |
758 | 0 | } |
759 | 0 | } |
760 | | |
761 | 1.92M | int av2_get_tx_scale(const TX_SIZE tx_size) { |
762 | 1.92M | const int pels = tx_size_2d[tx_size]; |
763 | | // Largest possible pels is 4096 (64x64). |
764 | 1.92M | return (pels > 256) + (pels > 1024); |
765 | 1.92M | } |
766 | | |
767 | | // NOTE: The implementation of all inverses need to be aware of the fact |
768 | | // that input and output could be the same buffer. |
769 | | |
770 | | void av2_lossless_inv_idtx_add_c(const tran_low_t *input, uint16_t *dest, |
771 | 0 | int stride, const TxfmParam *txfm_param) { |
772 | 0 | const int txw = tx_size_wide[txfm_param->tx_size]; |
773 | 0 | const int txh = tx_size_high[txfm_param->tx_size]; |
774 | 0 | int scale_bits = 3 - av2_get_tx_scale(txfm_param->tx_size); |
775 | 0 | for (int i = 0; i < txh; i++) { |
776 | 0 | for (int j = 0; j < txw; j++) { |
777 | 0 | dest[i * stride + j] = highbd_clip_pixel_add( |
778 | 0 | dest[i * stride + j], (input[i * txw + j] >> scale_bits), |
779 | 0 | txfm_param->bd); |
780 | 0 | } |
781 | 0 | } |
782 | 0 | } |
783 | | |
784 | | void av2_lossless_inv_idtx_add_vert_c(const tran_low_t *input, uint16_t *dest, |
785 | 1.42k | int stride, const TxfmParam *txfm_param) { |
786 | 1.42k | const int txw = tx_size_wide[txfm_param->tx_size]; |
787 | 1.42k | const int txh = tx_size_high[txfm_param->tx_size]; |
788 | 1.42k | int scale_bits = 3 - av2_get_tx_scale(txfm_param->tx_size); |
789 | 7.44k | for (int j = 0; j < txw; j++) { |
790 | 6.02k | int delay = 0; |
791 | 38.1k | for (int i = 0; i < txh; i++) { |
792 | 32.0k | tran_low_t current_txcoeff = (input[i * txw + j] >> scale_bits); |
793 | 32.0k | dest[i * stride + j] = highbd_clip_pixel_add( |
794 | 32.0k | dest[i * stride + j], current_txcoeff + delay, txfm_param->bd); |
795 | 32.0k | delay += current_txcoeff; |
796 | 32.0k | } |
797 | 6.02k | } |
798 | 1.42k | } |
799 | | |
800 | | void av2_lossless_inv_idtx_add_horz_c(const tran_low_t *input, uint16_t *dest, |
801 | 1.13k | int stride, const TxfmParam *txfm_param) { |
802 | 1.13k | const int txw = tx_size_wide[txfm_param->tx_size]; |
803 | 1.13k | const int txh = tx_size_high[txfm_param->tx_size]; |
804 | 1.13k | int scale_bits = 3 - av2_get_tx_scale(txfm_param->tx_size); |
805 | 6.15k | for (int i = 0; i < txh; i++) { |
806 | 5.01k | int delay = 0; |
807 | 34.7k | for (int j = 0; j < txw; j++) { |
808 | 29.7k | tran_low_t current_txcoeff = (input[i * txw + j] >> scale_bits); |
809 | 29.7k | dest[i * stride + j] = highbd_clip_pixel_add( |
810 | 29.7k | dest[i * stride + j], current_txcoeff + delay, txfm_param->bd); |
811 | 29.7k | delay += current_txcoeff; |
812 | 29.7k | } |
813 | 5.01k | } |
814 | 1.13k | } |
815 | | |
816 | | // idct |
817 | | void av2_highbd_iwht4x4_add(const tran_low_t *input, uint16_t *dest, int stride, |
818 | 876k | int eob, int bd) { |
819 | 876k | if (eob > 1) |
820 | 40.2k | av2_highbd_iwht4x4_16_add(input, dest, stride, bd); |
821 | 836k | else |
822 | 836k | av2_highbd_iwht4x4_1_add(input, dest, stride, bd); |
823 | 876k | } |
824 | | |
825 | | // inverse hadamard transform for DPCM lossless vertical mode |
826 | | void av2_highbd_iwht4x4_vert_add(const tran_low_t *input, uint16_t *dest, |
827 | 190k | int stride, int eob, int bd) { |
828 | 190k | if (eob > 1) |
829 | 36.5k | av2_highbd_iwht4x4_16_vert_add(input, dest, stride, bd); |
830 | 154k | else |
831 | 154k | av2_highbd_iwht4x4_1_vert_add(input, dest, stride, bd); |
832 | 190k | } |
833 | | |
834 | | // inverse hadamard transform for DPCM lossless horizontal mode |
835 | | void av2_highbd_iwht4x4_horz_add(const tran_low_t *input, uint16_t *dest, |
836 | 154k | int stride, int eob, int bd) { |
837 | 154k | if (eob > 1) |
838 | 25.2k | av2_highbd_iwht4x4_16_horz_add(input, dest, stride, bd); |
839 | 129k | else |
840 | 129k | av2_highbd_iwht4x4_1_horz_add(input, dest, stride, bd); |
841 | 154k | } |
842 | | |
843 | | // inverse transform for 4x4 dpcm lossless vertical mode |
844 | | void av2_highbd_inv_txfm_add_4x4_vert_c(const tran_low_t *input, uint16_t *dest, |
845 | | int stride, |
846 | 190k | const TxfmParam *txfm_param) { |
847 | 190k | assert(av2_ext_tx_used[txfm_param->tx_set_type][txfm_param->tx_type]); |
848 | 190k | int eob = txfm_param->eob; |
849 | 190k | int bd = txfm_param->bd; |
850 | 190k | int lossless = txfm_param->lossless; |
851 | 190k | const TX_TYPE tx_type = txfm_param->tx_type; |
852 | 190k | if (lossless) { |
853 | 190k | assert(tx_type == DCT_DCT); |
854 | 190k | (void)tx_type; |
855 | 190k | av2_highbd_iwht4x4_vert_add(input, dest, stride, eob, bd); |
856 | 190k | return; |
857 | 190k | } |
858 | 190k | } |
859 | | |
860 | | // inverse transform for 4x4 dpcm lossless horizontal mode |
861 | | void av2_highbd_inv_txfm_add_4x4_horz_c(const tran_low_t *input, uint16_t *dest, |
862 | | int stride, |
863 | 154k | const TxfmParam *txfm_param) { |
864 | 154k | assert(av2_ext_tx_used[txfm_param->tx_set_type][txfm_param->tx_type]); |
865 | 154k | int eob = txfm_param->eob; |
866 | 154k | int bd = txfm_param->bd; |
867 | 154k | int lossless = txfm_param->lossless; |
868 | 154k | const TX_TYPE tx_type = txfm_param->tx_type; |
869 | 154k | if (lossless) { |
870 | 154k | assert(tx_type == DCT_DCT); |
871 | 154k | (void)tx_type; |
872 | 154k | av2_highbd_iwht4x4_horz_add(input, dest, stride, eob, bd); |
873 | 154k | return; |
874 | 154k | } |
875 | 154k | } |
876 | | |
877 | | static void init_txfm_param(const MACROBLOCKD *xd, int plane, TX_SIZE tx_size, |
878 | | TX_TYPE tx_type, int eob, int reduced_tx_set, |
879 | 1.96M | int use_ddt, TxfmParam *txfm_param) { |
880 | 1.96M | (void)plane; |
881 | 1.96M | MB_MODE_INFO *const mbmi = xd->mi[0]; |
882 | 1.96M | txfm_param->tx_type = get_primary_tx_type(tx_type); |
883 | 1.96M | txfm_param->sec_tx_set = 0; |
884 | 1.96M | txfm_param->sec_tx_type = 0; |
885 | 1.96M | txfm_param->intra_mode = get_intra_mode(mbmi, plane); |
886 | 1.96M | txfm_param->is_inter = is_inter_block(xd->mi[0], xd->tree_type); |
887 | 1.96M | const int width = tx_size_wide[tx_size]; |
888 | 1.96M | const int height = tx_size_high[tx_size]; |
889 | 1.96M | bool mode_dependent_condition = |
890 | 1.96M | (txfm_param->is_inter |
891 | 1.96M | ? (txfm_param->tx_type == DCT_DCT && width >= 16 && height >= 16) |
892 | 1.96M | : txfm_param->intra_mode < PAETH_PRED); |
893 | 1.96M | if (mode_dependent_condition && !xd->lossless[mbmi->segment_id]) { |
894 | | // updated EOB condition |
895 | 685k | txfm_param->sec_tx_type = get_secondary_tx_type(tx_type); |
896 | 685k | txfm_param->sec_tx_set = get_secondary_tx_set(tx_type); |
897 | 685k | } |
898 | 1.96M | txfm_param->tx_size = tx_size; |
899 | | // EOB needs to adjusted after inverse IST |
900 | 1.96M | if (txfm_param->sec_tx_type) { |
901 | 176k | const int st_size_class = |
902 | 176k | (width == 8 && height == 8 && txfm_param->tx_type == DCT_DCT) ? 1 |
903 | 176k | : (width >= 8 && height >= 8) ? (txfm_param->tx_type == DCT_DCT ? 2 : 3) |
904 | 166k | : 0; |
905 | 176k | txfm_param->eob = |
906 | 176k | (st_size_class == 0) ? IST_4x4_HEIGHT |
907 | 176k | : (st_size_class == 1) |
908 | 156k | ? IST_8x8_HEIGHT_RED |
909 | 156k | : ((st_size_class == 3) ? IST_ADST_NZ_CNT : IST_8x8_HEIGHT); |
910 | 1.78M | } else { |
911 | 1.78M | txfm_param->eob = eob; |
912 | 1.78M | } |
913 | 1.96M | txfm_param->use_ddt = use_ddt; |
914 | 1.96M | txfm_param->lossless = xd->lossless[xd->mi[0]->segment_id]; |
915 | 1.96M | txfm_param->bd = xd->bd; |
916 | 1.96M | txfm_param->tx_set_type = av2_get_ext_tx_set_type( |
917 | 1.96M | txfm_param->tx_size, is_inter_block(xd->mi[0], xd->tree_type), |
918 | 1.96M | reduced_tx_set); |
919 | 1.96M | } |
920 | | |
921 | | void av2_highbd_inv_txfm_add_c(const tran_low_t *input, uint16_t *dest, |
922 | 0 | int stride, const TxfmParam *txfm_param) { |
923 | 0 | assert(av2_ext_tx_used[txfm_param->tx_set_type][txfm_param->tx_type]); |
924 | 0 | inv_txfm(input, dest, stride, txfm_param); |
925 | 0 | } |
926 | | |
927 | | // inverse transform for dpcm lossless horizontal mode |
928 | | void av2_highbd_inv_txfm_add_horz_c(const tran_low_t *input, uint16_t *dest, |
929 | 155k | int stride, const TxfmParam *txfm_param) { |
930 | 155k | assert(av2_ext_tx_used[txfm_param->tx_set_type][txfm_param->tx_type]); |
931 | 155k | const TX_SIZE tx_size = txfm_param->tx_size; |
932 | 155k | assert(txfm_param->lossless); |
933 | 155k | if (txfm_param->tx_type == IDTX) { |
934 | 1.13k | av2_lossless_inv_idtx_add_horz(input, dest, stride, txfm_param); |
935 | 1.13k | return; |
936 | 1.13k | } |
937 | 154k | switch (tx_size) { |
938 | 154k | case TX_4X4: |
939 | 154k | av2_highbd_inv_txfm_add_4x4_horz_c(input, dest, stride, txfm_param); |
940 | 154k | break; |
941 | 0 | default: assert(0 && "Invalid transform size for lossless coding"); break; |
942 | 154k | } |
943 | 154k | } |
944 | | |
945 | | // inverse transform for dpcm lossless vertical mode |
946 | | void av2_highbd_inv_txfm_add_vert_c(const tran_low_t *input, uint16_t *dest, |
947 | 192k | int stride, const TxfmParam *txfm_param) { |
948 | 192k | assert(av2_ext_tx_used[txfm_param->tx_set_type][txfm_param->tx_type]); |
949 | 192k | const TX_SIZE tx_size = txfm_param->tx_size; |
950 | 192k | assert(txfm_param->lossless); |
951 | 192k | if (txfm_param->tx_type == IDTX) { |
952 | 1.42k | av2_lossless_inv_idtx_add_vert(input, dest, stride, txfm_param); |
953 | 1.42k | return; |
954 | 1.42k | } |
955 | 190k | switch (tx_size) { |
956 | 191k | case TX_4X4: |
957 | 191k | av2_highbd_inv_txfm_add_4x4_vert_c(input, dest, stride, txfm_param); |
958 | 191k | break; |
959 | 0 | default: assert(0 && "Invalid transform size for lossless coding"); break; |
960 | 190k | } |
961 | 190k | } |
962 | | |
963 | | // Apply inverse cross chroma component transform |
964 | | void av2_inv_cross_chroma_tx_block(tran_low_t *dqcoeff_c1, |
965 | | tran_low_t *dqcoeff_c2, TX_SIZE tx_size, |
966 | 601k | CctxType cctx_type, const int bd) { |
967 | 601k | if (cctx_type == CCTX_NONE) return; |
968 | 29.0k | const int ncoeffs = av2_get_max_eob(tx_size); |
969 | 29.0k | int32_t *src_c1 = (int32_t *)dqcoeff_c1; |
970 | 29.0k | int32_t *src_c2 = (int32_t *)dqcoeff_c2; |
971 | 29.0k | int64_t tmp[2] = { 0, 0 }; |
972 | | |
973 | 29.0k | const int angle_idx = cctx_type - CCTX_START; |
974 | 6.58M | for (int i = 0; i < ncoeffs; i++) { |
975 | 6.55M | tmp[0] = (int64_t)cctx_mtx[angle_idx][0] * (int64_t)src_c1[i] - |
976 | 6.55M | (int64_t)cctx_mtx[angle_idx][1] * (int64_t)src_c2[i]; |
977 | 6.55M | tmp[1] = (int64_t)cctx_mtx[angle_idx][1] * (int64_t)src_c1[i] + |
978 | 6.55M | (int64_t)cctx_mtx[angle_idx][0] * (int64_t)src_c2[i]; |
979 | 6.55M | src_c1[i] = (int32_t)ROUND_POWER_OF_TWO_SIGNED_64(tmp[0], CCTX_PREC_BITS); |
980 | 6.55M | src_c2[i] = (int32_t)ROUND_POWER_OF_TWO_SIGNED_64(tmp[1], CCTX_PREC_BITS); |
981 | 6.55M | src_c1[i] = clamp_value(src_c1[i], 8 + bd); |
982 | 6.55M | src_c2[i] = clamp_value(src_c2[i], 8 + bd); |
983 | 6.55M | } |
984 | 29.0k | } |
985 | | |
986 | | static void av2_highbd_inv_txfm_add_master(const tran_low_t *input, |
987 | | uint16_t *dest, int stride, |
988 | 1.61M | const TxfmParam *txfm_param) { |
989 | 1.61M | if (txfm_param->lossless) { |
990 | 878k | if (txfm_param->tx_type == IDTX) { |
991 | 6.15k | av2_lossless_inv_idtx_add(input, dest, stride, txfm_param); |
992 | 6.15k | return; |
993 | 6.15k | } |
994 | 878k | } |
995 | 1.61M | av2_highbd_inv_txfm_add(input, dest, stride, txfm_param); |
996 | 1.61M | } |
997 | | |
998 | | void av2_inverse_transform_block(const MACROBLOCKD *xd, |
999 | | const tran_low_t *dqcoeff, int plane, |
1000 | | TX_TYPE tx_type, TX_SIZE tx_size, |
1001 | | uint16_t *dst, int stride, int eob, |
1002 | 1.98M | int use_ddt, int reduced_tx_set) { |
1003 | 1.98M | if (!eob) return; |
1004 | | |
1005 | 1.98M | assert(eob <= av2_get_max_eob(tx_size)); |
1006 | | |
1007 | 1.96M | TxfmParam txfm_param; |
1008 | 1.96M | init_txfm_param(xd, plane, tx_size, tx_type, eob, reduced_tx_set, use_ddt, |
1009 | 1.96M | &txfm_param); |
1010 | 1.96M | assert(av2_ext_tx_used[txfm_param.tx_set_type][txfm_param.tx_type]); |
1011 | 1.96M | assert(IMPLIES(txfm_param.sec_tx_type, |
1012 | 1.96M | block_signals_sec_tx_type(xd, tx_size, txfm_param.tx_type, |
1013 | 1.96M | txfm_param.eob))); |
1014 | | |
1015 | | // Work buffer for secondary transform |
1016 | 1.96M | DECLARE_ALIGNED(32, tran_low_t, temp_dqcoeff[MAX_TX_SQUARE]); |
1017 | 1.96M | memcpy(temp_dqcoeff, dqcoeff, sizeof(tran_low_t) * tx_size_2d[tx_size]); |
1018 | | |
1019 | 1.96M | av2_inv_stxfm(temp_dqcoeff, &txfm_param); |
1020 | | |
1021 | 1.96M | MB_MODE_INFO *const mbmi = xd->mi[0]; |
1022 | 1.96M | if (xd->lossless[mbmi->segment_id]) { |
1023 | 1.22M | PREDICTION_MODE cur_pred_mode = |
1024 | 1.22M | (plane == AVM_PLANE_Y) ? mbmi->mode : get_uv_mode(mbmi->uv_mode); |
1025 | 1.22M | int cur_dpcm_flag = |
1026 | 1.22M | (plane == AVM_PLANE_Y) ? mbmi->use_dpcm_y : mbmi->use_dpcm_uv; |
1027 | 1.22M | int cur_angle_delta = (plane == AVM_PLANE_Y) ? mbmi->angle_delta[0] : 0; |
1028 | 1.22M | if (cur_pred_mode == V_PRED && cur_angle_delta == 0 && cur_dpcm_flag > 0) { |
1029 | 192k | av2_highbd_inv_txfm_add_vert(temp_dqcoeff, dst, stride, &txfm_param); |
1030 | 1.03M | } else if (cur_pred_mode == H_PRED && cur_angle_delta == 0 && |
1031 | 196k | cur_dpcm_flag > 0) { |
1032 | 155k | av2_highbd_inv_txfm_add_horz(temp_dqcoeff, dst, stride, &txfm_param); |
1033 | 874k | } else { |
1034 | 874k | av2_highbd_inv_txfm_add_master(temp_dqcoeff, dst, stride, &txfm_param); |
1035 | 874k | } |
1036 | 1.22M | } else { |
1037 | 743k | av2_highbd_inv_txfm_add_master(temp_dqcoeff, dst, stride, &txfm_param); |
1038 | 743k | } |
1039 | 1.96M | } |
1040 | | |
1041 | | // Inverse secondary transform |
1042 | | void inv_stxfm_c(tran_low_t *src, tran_low_t *dst, const PREDICTION_MODE mode, |
1043 | 0 | const uint8_t stx_idx, const int size, const int bd) { |
1044 | 0 | assert(stx_idx < 4); |
1045 | 0 | const int16_t *kernel = (size == 0) ? ist_4x4_kernel[mode][stx_idx][0] |
1046 | 0 | : ist_8x8_kernel[mode][stx_idx][0]; |
1047 | 0 | int *out = dst; |
1048 | 0 | const int shift = 7; |
1049 | |
|
1050 | 0 | int reduced_width, reduced_height; |
1051 | 0 | if (size == 0) { |
1052 | 0 | reduced_height = IST_4x4_HEIGHT; |
1053 | 0 | reduced_width = IST_4x4_WIDTH; |
1054 | 0 | } else { |
1055 | 0 | reduced_height = (size == 1) |
1056 | 0 | ? IST_8x8_HEIGHT_RED |
1057 | 0 | : ((size == 3) ? IST_ADST_NZ_CNT : IST_8x8_HEIGHT); |
1058 | 0 | reduced_width = IST_8x8_WIDTH; |
1059 | 0 | } |
1060 | 0 | for (int j = 0; j < reduced_width; j++) { |
1061 | 0 | int32_t resi = 0; |
1062 | 0 | const int16_t *kernel_tmp = kernel; |
1063 | 0 | int *srcPtr = src; |
1064 | 0 | for (int i = 0; i < reduced_height; i++) { |
1065 | 0 | resi += *srcPtr++ * *kernel_tmp; |
1066 | 0 | kernel_tmp += reduced_width; |
1067 | 0 | } |
1068 | 0 | *out++ = clamp_value(ROUND_POWER_OF_TWO_SIGNED(resi, shift), 8 + bd); |
1069 | 0 | kernel++; |
1070 | 0 | } |
1071 | 0 | } |
1072 | | |
1073 | 1.95M | void av2_inv_stxfm(tran_low_t *coeff, TxfmParam *txfm_param) { |
1074 | 1.95M | const TX_TYPE stx_type = txfm_param->sec_tx_type; |
1075 | | |
1076 | 1.95M | const int width = tx_size_wide[txfm_param->tx_size] <= 32 |
1077 | 1.95M | ? tx_size_wide[txfm_param->tx_size] |
1078 | 1.95M | : 32; |
1079 | 1.95M | const int height = tx_size_high[txfm_param->tx_size] <= 32 |
1080 | 1.95M | ? tx_size_high[txfm_param->tx_size] |
1081 | 1.95M | : 32; |
1082 | | |
1083 | 1.95M | if ((width >= 4 && height >= 4) && stx_type) { |
1084 | 176k | const PREDICTION_MODE intra_mode = |
1085 | 176k | (txfm_param->is_inter ? DC_PRED : txfm_param->intra_mode); |
1086 | 176k | PREDICTION_MODE mode = 0, mode_t = 0; |
1087 | 176k | const int log2width = tx_size_wide_log2[txfm_param->tx_size]; |
1088 | | |
1089 | 176k | const int sb_size = (width >= 8 && height >= 8) ? 8 : 4; |
1090 | 176k | const int16_t *scan_order_out; |
1091 | | // Align scan order of IST with primary transform scan order |
1092 | 176k | const SCAN_ORDER *scan_order_in = |
1093 | 176k | get_scan(txfm_param->tx_size, txfm_param->tx_type); |
1094 | 176k | const int16_t *const scan = scan_order_in->scan; |
1095 | 176k | tran_low_t buf0[64] = { 0 }, buf1[64] = { 0 }; |
1096 | 176k | tran_low_t *tmp = buf0; |
1097 | 176k | tran_low_t *src = coeff; |
1098 | | |
1099 | 176k | int reduced_width = sb_size == 8 ? IST_8x8_WIDTH : IST_4x4_WIDTH; |
1100 | 7.99M | for (int r = 0; r < reduced_width; r++) { |
1101 | | // Align scan order of IST with primary transform scan order |
1102 | 7.82M | *tmp = src[scan[r]]; |
1103 | 7.82M | tmp++; |
1104 | 7.82M | } |
1105 | 176k | int8_t transpose = 0; |
1106 | 176k | mode = AVMMIN(intra_mode, SMOOTH_H_PRED); |
1107 | 176k | if ((mode == H_PRED) || (mode == D157_PRED) || (mode == D67_PRED) || |
1108 | 146k | (mode == SMOOTH_H_PRED)) |
1109 | 39.6k | transpose = 1; |
1110 | | #if STX_COEFF_DEBUG |
1111 | | fprintf(stderr, |
1112 | | "[inv stx] inter %d ptx %d txs %dx%d tp %d stx_set %d stx_type %d\n" |
1113 | | "(stx coeff)\n", |
1114 | | txfm_param->is_inter, get_primary_tx_type(txfm_param->tx_type), |
1115 | | width, height, transpose, txfm_param->sec_tx_set, stx_type); |
1116 | | for (int i = 0; i < height; i++) { |
1117 | | for (int j = 0; j < width; j++) { |
1118 | | fprintf(stderr, "%d,", coeff[i * width + j]); |
1119 | | } |
1120 | | fprintf(stderr, "\n"); |
1121 | | } |
1122 | | #endif // STX_COEFF_DEBUG |
1123 | 176k | mode_t = txfm_param->sec_tx_set; |
1124 | 176k | if (sb_size == 8) |
1125 | 176k | assert(mode_t < IST_8x8_SET_SIZE); |
1126 | 19.4k | else |
1127 | 176k | assert(mode_t < IST_4x4_SET_SIZE); |
1128 | 176k | if (transpose) { |
1129 | 39.6k | scan_order_out = (sb_size == 4) |
1130 | 39.6k | ? stx_scan_orders_transpose_4x4[log2width - 2] |
1131 | 39.6k | : stx_scan_orders_transpose_8x8[log2width - 2]; |
1132 | 136k | } else { |
1133 | 136k | scan_order_out = (sb_size == 4) ? stx_scan_orders_4x4[log2width - 2] |
1134 | 136k | : stx_scan_orders_8x8[log2width - 2]; |
1135 | 136k | } |
1136 | 176k | const int st_size_class = |
1137 | 176k | (width == 8 && height == 8 && txfm_param->tx_type == DCT_DCT) ? 1 |
1138 | 176k | : (width >= 8 && height >= 8) ? (txfm_param->tx_type == DCT_DCT ? 2 : 3) |
1139 | 166k | : 0; |
1140 | 176k | inv_stxfm(buf0, buf1, mode_t, stx_type - 1, st_size_class, txfm_param->bd); |
1141 | 176k | tmp = buf1; |
1142 | 176k | src = coeff; |
1143 | 176k | memset(src, 0, width * height * sizeof(tran_low_t)); |
1144 | 176k | const int16_t *sup_reg_mapping = |
1145 | 176k | sb_size == 8 |
1146 | 176k | ? &coeff8x8_mapping[txfm_param->sec_tx_set * 3 + stx_type - 1][0] |
1147 | 176k | : NULL; |
1148 | 7.99M | for (int r = 0; r < reduced_width; r++) { |
1149 | 7.82M | if (sb_size == 8) |
1150 | 7.51M | src[scan_order_out[sup_reg_mapping[r]]] = *tmp; |
1151 | 311k | else |
1152 | 311k | src[scan_order_out[r]] = *tmp; |
1153 | 7.82M | tmp++; |
1154 | 7.82M | } |
1155 | | #if STX_COEFF_DEBUG |
1156 | | fprintf(stderr, "(ptx coeff)\n"); |
1157 | | for (int i = 0; i < height; i++) { |
1158 | | for (int j = 0; j < width; j++) { |
1159 | | fprintf(stderr, "%d,", coeff[i * width + j]); |
1160 | | } |
1161 | | fprintf(stderr, "\n"); |
1162 | | } |
1163 | | #endif // STX_COEFF_DEBUG |
1164 | 176k | } |
1165 | 1.95M | } |