/work/libde265/libde265/fallback-dct.cc
Line | Count | Source |
1 | | /* |
2 | | * H.265 video codec. |
3 | | * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de> |
4 | | * |
5 | | * This file is part of libde265. |
6 | | * |
7 | | * libde265 is free software: you can redistribute it and/or modify |
8 | | * it under the terms of the GNU Lesser General Public License as |
9 | | * published by the Free Software Foundation, either version 3 of |
10 | | * the License, or (at your option) any later version. |
11 | | * |
12 | | * libde265 is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public License |
18 | | * along with libde265. If not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include "fallback-dct.h" |
22 | | |
23 | | #if defined(_MSC_VER) || defined(__MINGW32__) |
24 | | # include <malloc.h> |
25 | | #elif defined(HAVE_ALLOCA_H) |
26 | | # include <alloca.h> |
27 | | #endif |
28 | | |
29 | | #include <assert.h> |
30 | | #include <algorithm> |
31 | | |
32 | | |
33 | | #if 0 |
34 | | static void printMatrix(const char* name, const int16_t* v, int n) |
35 | | { |
36 | | printf("--- %s ---\n",name); |
37 | | for (int r=0;r<n;r++) { |
38 | | for (int c=0;c<n;c++) { |
39 | | printf("%4d ",v[c+r*n]); |
40 | | } |
41 | | printf("\n"); |
42 | | } |
43 | | } |
44 | | #endif |
45 | | |
46 | | |
47 | | void transform_skip_8_fallback(uint8_t *dst, const int16_t *coeffs, ptrdiff_t stride) |
48 | 0 | { |
49 | 0 | int nT = 4; |
50 | 0 | int bdShift2 = 20-8; |
51 | |
|
52 | 0 | assert(0); // DEPRECATED, should not be used anymore because of fixed 4x4 size |
53 | | |
54 | 0 | for (int y=0;y<nT;y++) |
55 | 0 | for (int x=0;x<nT;x++) { |
56 | 0 | int32_t c = coeffs[x+y*nT] << 7; |
57 | 0 | c = (c+(1<<(bdShift2-1)))>>bdShift2; |
58 | |
|
59 | 0 | dst[y*stride+x] = Clip1_8bit(dst[y*stride+x] + c); |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | | |
64 | | void transform_skip_16_fallback(uint16_t *dst, const int16_t *coeffs, ptrdiff_t stride, int bit_depth) |
65 | 0 | { |
66 | 0 | int nT = 4; |
67 | 0 | int bdShift2 = 20-bit_depth; |
68 | |
|
69 | 0 | assert(0); // DEPRECATED, should not be used anymore because of fixed 4x4 size |
70 | | |
71 | 0 | for (int y=0;y<nT;y++) |
72 | 0 | for (int x=0;x<nT;x++) { |
73 | 0 | int32_t c = coeffs[x+y*nT] << 7; |
74 | 0 | c = (c+(1<<(bdShift2-1)))>>bdShift2; |
75 | |
|
76 | 0 | dst[y*stride+x] = Clip_BitDepth(dst[y*stride+x] + c, bit_depth); |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | | |
81 | | void transform_skip_residual_fallback(int32_t *residual, const int16_t *coeffs, int nT, |
82 | | int tsShift,int bdShift) |
83 | 0 | { |
84 | 0 | const int rnd = 1<<(bdShift-1); |
85 | |
|
86 | 0 | for (int y=0;y<nT;y++) |
87 | 0 | for (int x=0;x<nT;x++) { |
88 | 0 | int32_t c = (int32_t)((uint32_t)coeffs[x+y*nT] << tsShift); // C++ up to C++17 treats left-shift of signed values as UB |
89 | 0 | residual[x+y*nT] = (c + rnd) >> bdShift; |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | | |
94 | | void transform_skip_rdpcm_v_8_fallback(uint8_t *dst, const int16_t *coeffs, int log2nT, ptrdiff_t stride) |
95 | 0 | { |
96 | 0 | int bitDepth = 8; |
97 | 0 | int bdShift2 = 20-bitDepth; |
98 | 0 | int offset = (1<<(bdShift2-1)); |
99 | 0 | int tsShift = 5 + log2nT; // TODO: extended_precision |
100 | 0 | int nT = 1<<log2nT; |
101 | |
|
102 | 0 | for (int x=0;x<nT;x++) { |
103 | 0 | int32_t sum = 0; |
104 | |
|
105 | 0 | for (int y=0;y<nT;y++) { |
106 | 0 | int c = coeffs[x+y*nT] << tsShift; |
107 | 0 | sum += (c+offset)>>bdShift2; |
108 | |
|
109 | 0 | dst[y*stride+x] = Clip1_8bit(dst[y*stride+x] + sum); |
110 | 0 | } |
111 | 0 | } |
112 | 0 | } |
113 | | |
114 | | void transform_skip_rdpcm_h_8_fallback(uint8_t *dst, const int16_t *coeffs, int log2nT, ptrdiff_t stride) |
115 | 0 | { |
116 | 0 | int bitDepth = 8; |
117 | 0 | int bdShift2 = 20-bitDepth; |
118 | 0 | int offset = (1<<(bdShift2-1)); |
119 | 0 | int tsShift = 5 + log2nT; // TODO: extended_precision |
120 | 0 | int nT = 1<<log2nT; |
121 | |
|
122 | 0 | for (int y=0;y<nT;y++) { |
123 | 0 | int32_t sum = 0; |
124 | |
|
125 | 0 | for (int x=0;x<nT;x++) { |
126 | 0 | int c = coeffs[x+y*nT] << tsShift; |
127 | 0 | sum += (c+offset)>>bdShift2; |
128 | |
|
129 | 0 | dst[y*stride+x] = Clip1_8bit(dst[y*stride+x] + sum); |
130 | 0 | } |
131 | 0 | } |
132 | 0 | } |
133 | | |
134 | | |
135 | | void transform_bypass_rdpcm_v_8_fallback(uint8_t *dst, const int16_t *coeffs,int nT,ptrdiff_t stride) |
136 | 0 | { |
137 | 0 | for (int x=0;x<nT;x++) { |
138 | 0 | int32_t sum=0; |
139 | 0 | for (int y=0;y<nT;y++) { |
140 | 0 | sum += coeffs[x+y*nT]; |
141 | |
|
142 | 0 | dst[y*stride+x] = Clip1_8bit(dst[y*stride+x] + sum); |
143 | 0 | } |
144 | 0 | } |
145 | 0 | } |
146 | | |
147 | | |
148 | | void transform_bypass_rdpcm_h_8_fallback(uint8_t *dst, const int16_t *coeffs,int nT,ptrdiff_t stride) |
149 | 0 | { |
150 | 0 | for (int y=0;y<nT;y++) { |
151 | 0 | int32_t sum=0; |
152 | 0 | for (int x=0;x<nT;x++) { |
153 | 0 | sum += coeffs[x+y*nT]; |
154 | |
|
155 | 0 | dst[y*stride+x] = Clip1_8bit(dst[y*stride+x] + sum); |
156 | 0 | } |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | | |
161 | | void transform_bypass_rdpcm_v_fallback(int32_t *dst, const int16_t *coeffs,int nT) |
162 | 0 | { |
163 | 0 | for (int x=0;x<nT;x++) { |
164 | 0 | int32_t sum=0; |
165 | 0 | for (int y=0;y<nT;y++) { |
166 | 0 | sum += coeffs[x+y*nT]; |
167 | |
|
168 | 0 | dst[y*nT+x] = sum; |
169 | 0 | } |
170 | 0 | } |
171 | 0 | } |
172 | | |
173 | | |
174 | | void transform_bypass_rdpcm_h_fallback(int32_t *dst, const int16_t *coeffs,int nT) |
175 | 0 | { |
176 | 0 | for (int y=0;y<nT;y++) { |
177 | 0 | int32_t sum=0; |
178 | 0 | for (int x=0;x<nT;x++) { |
179 | 0 | sum += coeffs[x+y*nT]; |
180 | |
|
181 | 0 | dst[y*nT+x] = sum; |
182 | 0 | } |
183 | 0 | } |
184 | 0 | } |
185 | | |
186 | | |
187 | | void rdpcm_v_fallback(int32_t* residual, const int16_t* coeffs, int nT,int tsShift,int bdShift) |
188 | 0 | { |
189 | 0 | int rnd = (1<<(bdShift-1)); |
190 | |
|
191 | 0 | for (int x=0;x<nT;x++) { |
192 | 0 | int sum=0; |
193 | 0 | for (int y=0;y<nT;y++) { |
194 | 0 | int c = coeffs[x+y*nT] << tsShift; |
195 | 0 | sum += (c+rnd)>>bdShift; |
196 | 0 | residual[y*nT+x] = sum; |
197 | 0 | } |
198 | 0 | } |
199 | 0 | } |
200 | | |
201 | | |
202 | | void rdpcm_h_fallback(int32_t* residual, const int16_t* coeffs, int nT,int tsShift,int bdShift) |
203 | 0 | { |
204 | 0 | int rnd = (1<<(bdShift-1)); |
205 | |
|
206 | 0 | for (int y=0;y<nT;y++) { |
207 | 0 | int sum=0; |
208 | 0 | for (int x=0;x<nT;x++) { |
209 | 0 | int c = coeffs[x+y*nT] << tsShift; |
210 | 0 | sum += (c+rnd)>>bdShift; |
211 | 0 | residual[y*nT+x] = sum; |
212 | 0 | } |
213 | 0 | } |
214 | 0 | } |
215 | | |
216 | | |
217 | | void transform_bypass_fallback(int32_t *dst, const int16_t *coeffs, int nT) |
218 | 0 | { |
219 | 0 | for (int y=0;y<nT;y++) |
220 | 0 | for (int x=0;x<nT;x++) { |
221 | 0 | int32_t c = coeffs[x+y*nT]; |
222 | |
|
223 | 0 | dst[y*nT+x] = c; |
224 | 0 | } |
225 | 0 | } |
226 | | |
227 | | |
228 | | void transform_bypass_8_fallback(uint8_t *dst, const int16_t *coeffs, int nT, ptrdiff_t stride) |
229 | 0 | { |
230 | 0 | for (int y=0;y<nT;y++) |
231 | 0 | for (int x=0;x<nT;x++) { |
232 | 0 | int32_t c = coeffs[x+y*nT]; |
233 | |
|
234 | 0 | dst[y*stride+x] = Clip1_8bit(dst[y*stride+x] + c); |
235 | 0 | } |
236 | 0 | } |
237 | | |
238 | | |
239 | | void transform_bypass_16_fallback(uint16_t *dst, const int16_t *coeffs, int nT, ptrdiff_t stride, int bit_depth) |
240 | 0 | { |
241 | 0 | for (int y=0;y<nT;y++) |
242 | 0 | for (int x=0;x<nT;x++) { |
243 | 0 | int32_t c = coeffs[x+y*nT]; |
244 | |
|
245 | 0 | dst[y*stride+x] = Clip_BitDepth(dst[y*stride+x] + c, bit_depth); |
246 | 0 | } |
247 | 0 | } |
248 | | |
249 | | |
250 | | void rotate_coefficients_fallback(int16_t *coeff, int nT) |
251 | 0 | { |
252 | 0 | for (int y=0;y<nT/2;y++) |
253 | 0 | for (int x=0;x<nT;x++) { |
254 | 0 | std::swap(coeff[y*nT+x], coeff[(nT-1-y)*nT + nT-1-x]); |
255 | 0 | } |
256 | 0 | } |
257 | | |
258 | | |
259 | | |
260 | | static int8_t mat_8_357[4][4] = { |
261 | | { 29, 55, 74, 84 }, |
262 | | { 74, 74, 0,-74 }, |
263 | | { 84,-29,-74, 55 }, |
264 | | { 55,-84, 74,-29 } |
265 | | }; |
266 | | |
267 | | |
268 | | |
269 | | void transform_4x4_luma_add_8_fallback(uint8_t *dst, const int16_t *coeffs, ptrdiff_t stride) |
270 | 0 | { |
271 | 0 | int16_t g[4][4]; |
272 | |
|
273 | 0 | int postShift = 20-8; // 8 bit |
274 | 0 | int rndV = 1<<(7-1); |
275 | 0 | int rndH = 1<<(postShift-1); |
276 | | |
277 | | |
278 | | // --- V --- |
279 | |
|
280 | 0 | for (int c=0;c<4;c++) { |
281 | | /* |
282 | | logtrace(LogTransform,"DST-V: "); |
283 | | for (int r=0;r<4;r++) { |
284 | | logtrace(LogTransform,"%d ",coeffs[c+r*4]); |
285 | | } |
286 | | logtrace(LogTransform,"* -> "); |
287 | | */ |
288 | |
|
289 | 0 | for (int i=0;i<4;i++) { |
290 | 0 | int sum=0; |
291 | |
|
292 | 0 | for (int j=0;j<4;j++) { |
293 | 0 | sum += mat_8_357[j][i] * coeffs[c+j*4]; |
294 | 0 | } |
295 | |
|
296 | 0 | g[i][c] = Clip3(-32768,32767, (sum+rndV)>>7); |
297 | 0 | } |
298 | | |
299 | | /* |
300 | | for (int y=0;y<4;y++) { |
301 | | logtrace(LogTransform,"*%d ",g[y][c]); |
302 | | } |
303 | | logtrace(LogTransform,"*\n"); |
304 | | */ |
305 | 0 | } |
306 | | |
307 | | |
308 | | // --- H --- |
309 | |
|
310 | 0 | for (int y=0;y<4;y++) { |
311 | | |
312 | | /* |
313 | | logtrace(LogTransform,"DST-H: "); |
314 | | for (int c=0;c<4;c++) { |
315 | | logtrace(LogTransform,"%d ",g[y][c]); |
316 | | } |
317 | | logtrace(LogTransform,"* -> "); |
318 | | */ |
319 | |
|
320 | 0 | for (int i=0;i<4;i++) { |
321 | 0 | int sum=0; |
322 | |
|
323 | 0 | for (int j=0;j<4;j++) { |
324 | 0 | sum += mat_8_357[j][i] * g[y][j]; |
325 | 0 | } |
326 | |
|
327 | 0 | int out = Clip3(-32768,32767, (sum+rndH)>>postShift); |
328 | |
|
329 | 0 | dst[y*stride+i] = Clip1_8bit(dst[y*stride+i] + out); |
330 | |
|
331 | 0 | logtrace(LogTransform,"*%d ",out); |
332 | 0 | } |
333 | |
|
334 | 0 | logtrace(LogTransform,"*\n"); |
335 | 0 | } |
336 | 0 | } |
337 | | |
338 | | |
339 | | void transform_4x4_luma_add_16_fallback(uint16_t *dst, const int16_t *coeffs, ptrdiff_t stride, |
340 | | int bit_depth) |
341 | 0 | { |
342 | 0 | int16_t g[4][4]; |
343 | |
|
344 | 0 | int postShift = 20-bit_depth; |
345 | 0 | int rndV = 1<<(7-1); |
346 | 0 | int rndH = 1<<(postShift-1); |
347 | | |
348 | | |
349 | | // --- V --- |
350 | |
|
351 | 0 | for (int c=0;c<4;c++) { |
352 | | /* |
353 | | logtrace(LogTransform,"DST-V: "); |
354 | | for (int r=0;r<4;r++) { |
355 | | logtrace(LogTransform,"%d ",coeffs[c+r*4]); |
356 | | } |
357 | | logtrace(LogTransform,"* -> "); |
358 | | */ |
359 | |
|
360 | 0 | for (int i=0;i<4;i++) { |
361 | 0 | int sum=0; |
362 | |
|
363 | 0 | for (int j=0;j<4;j++) { |
364 | 0 | sum += mat_8_357[j][i] * coeffs[c+j*4]; |
365 | 0 | } |
366 | |
|
367 | 0 | g[i][c] = Clip3(-32768,32767, (sum+rndV)>>7); |
368 | 0 | } |
369 | | |
370 | | /* |
371 | | for (int y=0;y<4;y++) { |
372 | | logtrace(LogTransform,"*%d ",g[y][c]); |
373 | | } |
374 | | logtrace(LogTransform,"*\n"); |
375 | | */ |
376 | 0 | } |
377 | | |
378 | | |
379 | | // --- H --- |
380 | |
|
381 | 0 | for (int y=0;y<4;y++) { |
382 | | |
383 | | /* |
384 | | logtrace(LogTransform,"DST-H: "); |
385 | | for (int c=0;c<4;c++) { |
386 | | logtrace(LogTransform,"%d ",g[y][c]); |
387 | | } |
388 | | logtrace(LogTransform,"* -> "); |
389 | | */ |
390 | |
|
391 | 0 | for (int i=0;i<4;i++) { |
392 | 0 | int sum=0; |
393 | |
|
394 | 0 | for (int j=0;j<4;j++) { |
395 | 0 | sum += mat_8_357[j][i] * g[y][j]; |
396 | 0 | } |
397 | |
|
398 | 0 | int out = Clip3(-32768,32767, (sum+rndH)>>postShift); |
399 | |
|
400 | 0 | dst[y*stride+i] = Clip_BitDepth(dst[y*stride+i] + out, bit_depth); |
401 | |
|
402 | 0 | logtrace(LogTransform,"*%d ",out); |
403 | 0 | } |
404 | |
|
405 | 0 | logtrace(LogTransform,"*\n"); |
406 | 0 | } |
407 | 0 | } |
408 | | |
409 | | |
410 | | void fdst_4x4_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride) |
411 | 0 | { |
412 | 0 | int16_t g[4*4]; |
413 | |
|
414 | 0 | int BD = 8; |
415 | 0 | int shift1 = Log2(4) + BD -9; |
416 | 0 | int shift2 = Log2(4) + 6; |
417 | |
|
418 | 0 | int rnd1 = 1<<(shift1-1); |
419 | 0 | int rnd2 = 1<<(shift2-1); |
420 | | |
421 | | |
422 | | // --- V --- |
423 | |
|
424 | 0 | for (int c=0;c<4;c++) { |
425 | | |
426 | | /* |
427 | | logtrace(LogTransform,"DST-V: "); |
428 | | for (int r=0;r<4;r++) { |
429 | | logtrace(LogTransform,"%d ",coeffs[c+r*4]); |
430 | | } |
431 | | logtrace(LogTransform,"* -> "); |
432 | | */ |
433 | |
|
434 | 0 | for (int i=0;i<4;i++) { |
435 | 0 | int sum=0; |
436 | |
|
437 | 0 | for (int j=0;j<4;j++) { |
438 | 0 | sum += mat_8_357[i][j] * input[c+j*stride]; |
439 | 0 | } |
440 | |
|
441 | 0 | g[c+4*i] = Clip3(-32768,32767, (sum+rnd1)>>shift1); |
442 | 0 | } |
443 | 0 | } |
444 | | |
445 | | |
446 | | // --- H --- |
447 | |
|
448 | 0 | for (int y=0;y<4;y++) { |
449 | 0 | for (int i=0;i<4;i++) { |
450 | 0 | int sum=0; |
451 | |
|
452 | 0 | for (int j=0;j<4;j++) { |
453 | 0 | sum += mat_8_357[i][j] * g[y*4+j]; |
454 | 0 | } |
455 | | |
456 | | // TODO: do we need clipping ? |
457 | 0 | int out = (sum+rnd2)>>shift2; // Clip3(-32768,32767, (sum+rndH)>>postShift); |
458 | |
|
459 | 0 | coeffs[y*4+i] = out; |
460 | |
|
461 | 0 | logtrace(LogTransform,"*%d ",out); |
462 | 0 | } |
463 | |
|
464 | 0 | logtrace(LogTransform,"*\n"); |
465 | 0 | } |
466 | 0 | } |
467 | | |
468 | | |
469 | | void transform_idst_4x4_fallback(int32_t *dst, const int16_t *coeffs, int bdShift, int max_coeff_bits) |
470 | 0 | { |
471 | 0 | int16_t g[4][4]; |
472 | |
|
473 | 0 | int rndV = 1<<(7-1); |
474 | 0 | int rndH = 1<<(bdShift-1); |
475 | |
|
476 | 0 | int CoeffMax = (1<<max_coeff_bits)-1; |
477 | 0 | int CoeffMin = -(1<<max_coeff_bits); |
478 | | |
479 | | |
480 | | // --- V --- |
481 | |
|
482 | 0 | for (int c=0;c<4;c++) { |
483 | 0 | for (int i=0;i<4;i++) { |
484 | 0 | int sum=0; |
485 | |
|
486 | 0 | for (int j=0;j<4;j++) { |
487 | 0 | sum += mat_8_357[j][i] * coeffs[c+j*4]; |
488 | 0 | } |
489 | |
|
490 | 0 | g[i][c] = Clip3(CoeffMin,CoeffMax, (sum+rndV)>>7); |
491 | 0 | } |
492 | 0 | } |
493 | | |
494 | | |
495 | | // --- H --- |
496 | |
|
497 | 0 | for (int y=0;y<4;y++) { |
498 | 0 | for (int i=0;i<4;i++) { |
499 | 0 | int sum=0; |
500 | |
|
501 | 0 | for (int j=0;j<4;j++) { |
502 | 0 | sum += mat_8_357[j][i] * g[y][j]; |
503 | 0 | } |
504 | |
|
505 | 0 | dst[y*4+i] = (sum + rndH)>>bdShift; |
506 | 0 | } |
507 | 0 | } |
508 | 0 | } |
509 | | |
510 | | |
511 | | |
512 | | static int8_t mat_dct[32][32] = { |
513 | | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64}, |
514 | | { 90, 90, 88, 85, 82, 78, 73, 67, 61, 54, 46, 38, 31, 22, 13, 4, -4,-13,-22,-31,-38,-46,-54,-61,-67,-73,-78,-82,-85,-88,-90,-90}, |
515 | | { 90, 87, 80, 70, 57, 43, 25, 9, -9,-25,-43,-57,-70,-80,-87,-90, -90,-87,-80,-70,-57,-43,-25, -9, 9, 25, 43, 57, 70, 80, 87, 90}, |
516 | | { 90, 82, 67, 46, 22, -4,-31,-54,-73,-85,-90,-88,-78,-61,-38,-13, 13, 38, 61, 78, 88, 90, 85, 73, 54, 31, 4,-22,-46,-67,-82,-90}, |
517 | | { 89, 75, 50, 18,-18,-50,-75,-89,-89,-75,-50,-18, 18, 50, 75, 89, 89, 75, 50, 18,-18,-50,-75,-89,-89,-75,-50,-18, 18, 50, 75, 89}, |
518 | | { 88, 67, 31,-13,-54,-82,-90,-78,-46, -4, 38, 73, 90, 85, 61, 22, -22,-61,-85,-90,-73,-38, 4, 46, 78, 90, 82, 54, 13,-31,-67,-88}, |
519 | | { 87, 57, 9,-43,-80,-90,-70,-25, 25, 70, 90, 80, 43, -9,-57,-87, -87,-57, -9, 43, 80, 90, 70, 25,-25,-70,-90,-80,-43, 9, 57, 87}, |
520 | | { 85, 46,-13,-67,-90,-73,-22, 38, 82, 88, 54, -4,-61,-90,-78,-31, 31, 78, 90, 61, 4,-54,-88,-82,-38, 22, 73, 90, 67, 13,-46,-85}, |
521 | | { 83, 36,-36,-83,-83,-36, 36, 83, 83, 36,-36,-83,-83,-36, 36, 83, 83, 36,-36,-83,-83,-36, 36, 83, 83, 36,-36,-83,-83,-36, 36, 83}, |
522 | | { 82, 22,-54,-90,-61, 13, 78, 85, 31,-46,-90,-67, 4, 73, 88, 38, -38,-88,-73, -4, 67, 90, 46,-31,-85,-78,-13, 61, 90, 54,-22,-82}, |
523 | | { 80, 9,-70,-87,-25, 57, 90, 43,-43,-90,-57, 25, 87, 70, -9,-80, -80, -9, 70, 87, 25,-57,-90,-43, 43, 90, 57,-25,-87,-70, 9, 80}, |
524 | | { 78, -4,-82,-73, 13, 85, 67,-22,-88,-61, 31, 90, 54,-38,-90,-46, 46, 90, 38,-54,-90,-31, 61, 88, 22,-67,-85,-13, 73, 82, 4,-78}, |
525 | | { 75,-18,-89,-50, 50, 89, 18,-75,-75, 18, 89, 50,-50,-89,-18, 75, 75,-18,-89,-50, 50, 89, 18,-75,-75, 18, 89, 50,-50,-89,-18, 75}, |
526 | | { 73,-31,-90,-22, 78, 67,-38,-90,-13, 82, 61,-46,-88, -4, 85, 54, -54,-85, 4, 88, 46,-61,-82, 13, 90, 38,-67,-78, 22, 90, 31,-73}, |
527 | | { 70,-43,-87, 9, 90, 25,-80,-57, 57, 80,-25,-90, -9, 87, 43,-70, -70, 43, 87, -9,-90,-25, 80, 57,-57,-80, 25, 90, 9,-87,-43, 70}, |
528 | | { 67,-54,-78, 38, 85,-22,-90, 4, 90, 13,-88,-31, 82, 46,-73,-61, 61, 73,-46,-82, 31, 88,-13,-90, -4, 90, 22,-85,-38, 78, 54,-67}, |
529 | | { 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64}, |
530 | | { 61,-73,-46, 82, 31,-88,-13, 90, -4,-90, 22, 85,-38,-78, 54, 67, -67,-54, 78, 38,-85,-22, 90, 4,-90, 13, 88,-31,-82, 46, 73,-61}, |
531 | | { 57,-80,-25, 90, -9,-87, 43, 70,-70,-43, 87, 9,-90, 25, 80,-57, -57, 80, 25,-90, 9, 87,-43,-70, 70, 43,-87, -9, 90,-25,-80, 57}, |
532 | | { 54,-85, -4, 88,-46,-61, 82, 13,-90, 38, 67,-78,-22, 90,-31,-73, 73, 31,-90, 22, 78,-67,-38, 90,-13,-82, 61, 46,-88, 4, 85,-54}, |
533 | | { 50,-89, 18, 75,-75,-18, 89,-50,-50, 89,-18,-75, 75, 18,-89, 50, 50,-89, 18, 75,-75,-18, 89,-50,-50, 89,-18,-75, 75, 18,-89, 50}, |
534 | | { 46,-90, 38, 54,-90, 31, 61,-88, 22, 67,-85, 13, 73,-82, 4, 78, -78, -4, 82,-73,-13, 85,-67,-22, 88,-61,-31, 90,-54,-38, 90,-46}, |
535 | | { 43,-90, 57, 25,-87, 70, 9,-80, 80, -9,-70, 87,-25,-57, 90,-43, -43, 90,-57,-25, 87,-70, -9, 80,-80, 9, 70,-87, 25, 57,-90, 43}, |
536 | | { 38,-88, 73, -4,-67, 90,-46,-31, 85,-78, 13, 61,-90, 54, 22,-82, 82,-22,-54, 90,-61,-13, 78,-85, 31, 46,-90, 67, 4,-73, 88,-38}, |
537 | | { 36,-83, 83,-36,-36, 83,-83, 36, 36,-83, 83,-36,-36, 83,-83, 36, 36,-83, 83,-36,-36, 83,-83, 36, 36,-83, 83,-36,-36, 83,-83, 36}, |
538 | | { 31,-78, 90,-61, 4, 54,-88, 82,-38,-22, 73,-90, 67,-13,-46, 85, -85, 46, 13,-67, 90,-73, 22, 38,-82, 88,-54, -4, 61,-90, 78,-31}, |
539 | | { 25,-70, 90,-80, 43, 9,-57, 87,-87, 57, -9,-43, 80,-90, 70,-25, -25, 70,-90, 80,-43, -9, 57,-87, 87,-57, 9, 43,-80, 90,-70, 25}, |
540 | | { 22,-61, 85,-90, 73,-38, -4, 46,-78, 90,-82, 54,-13,-31, 67,-88, 88,-67, 31, 13,-54, 82,-90, 78,-46, 4, 38,-73, 90,-85, 61,-22}, |
541 | | { 18,-50, 75,-89, 89,-75, 50,-18,-18, 50,-75, 89,-89, 75,-50, 18, 18,-50, 75,-89, 89,-75, 50,-18,-18, 50,-75, 89,-89, 75,-50, 18}, |
542 | | { 13,-38, 61,-78, 88,-90, 85,-73, 54,-31, 4, 22,-46, 67,-82, 90, -90, 82,-67, 46,-22, -4, 31,-54, 73,-85, 90,-88, 78,-61, 38,-13}, |
543 | | { 9,-25, 43,-57, 70,-80, 87,-90, 90,-87, 80,-70, 57,-43, 25, -9, -9, 25,-43, 57,-70, 80,-87, 90,-90, 87,-80, 70,-57, 43,-25, 9}, |
544 | | { 4,-13, 22,-31, 38,-46, 54,-61, 67,-73, 78,-82, 85,-88, 90,-90, 90,-90, 88,-85, 82,-78, 73,-67, 61,-54, 46,-38, 31,-22, 13, -4} |
545 | | }; |
546 | | |
547 | | |
548 | | |
549 | | |
550 | | template <class pixel_t> |
551 | | void transform_idct_add(pixel_t *dst, ptrdiff_t stride, |
552 | | int nT, const int16_t *coeffs, int bit_depth) |
553 | 0 | { |
554 | | /* |
555 | | The effective shift is |
556 | | 7 bits right for bit-depth 8, |
557 | | 6 bits right for bit-depth 9, |
558 | | 5 bits right for bit-depth 10. |
559 | | |
560 | | Computation is independent of the block size. |
561 | | Each multiplication with the table includes a left shift of 6 bits. |
562 | | Hence, we have 2* 6 bits = 12 bits left shift. |
563 | | V-pass has fixed 7 bit right shift. |
564 | | H-pass has 20-BitDepth bit right shift; |
565 | | |
566 | | Effective shift 's' means: residual value 1 gives DC-coeff (1<<s). |
567 | | */ |
568 | | |
569 | |
|
570 | 0 | int postShift = 20-bit_depth; |
571 | 0 | int rnd1 = 1<<(7-1); |
572 | 0 | int rnd2 = 1<<(postShift-1); |
573 | 0 | int fact = (1<<(5-Log2(nT))); |
574 | |
|
575 | 0 | int16_t g[32*32]; // actually, only [nT*nT] used |
576 | | |
577 | | // TODO: valgrind reports that dst[] contains uninitialized data. |
578 | | // Probably from intra-prediction. |
579 | | |
580 | | /* |
581 | | for (int i=0;i<nT*nT;i++) { |
582 | | printf("%d\n",coeffs[i]); |
583 | | } |
584 | | |
585 | | for (int y=0;y<nT;y++) { |
586 | | for (int i=0;i<nT;i++) { |
587 | | printf("%d ",dst[y*stride+i]); |
588 | | } |
589 | | } |
590 | | printf("\n"); |
591 | | */ |
592 | | |
593 | | /* |
594 | | printf("--- input\n"); |
595 | | for (int r=0;r<nT;r++, printf("\n")) |
596 | | for (int c=0;c<nT;c++) { |
597 | | printf("%3d ",coeffs[c+r*nT]); |
598 | | } |
599 | | */ |
600 | |
|
601 | 0 | for (int c=0;c<nT;c++) { |
602 | | |
603 | | /* |
604 | | logtrace(LogTransform,"DCT-V: "); |
605 | | for (int i=0;i<nT;i++) { |
606 | | logtrace(LogTransform,"*%d ",coeffs[c+i*nT]); |
607 | | } |
608 | | logtrace(LogTransform,"* -> "); |
609 | | */ |
610 | | |
611 | | |
612 | | // find last non-zero coefficient to reduce computations carried out in DCT |
613 | |
|
614 | 0 | int lastCol = nT-1; |
615 | 0 | for (;lastCol>=0;lastCol--) { |
616 | 0 | if (coeffs[c+lastCol*nT]) { break; } |
617 | 0 | } |
618 | |
|
619 | 0 | for (int i=0;i<nT;i++) { |
620 | 0 | int sum=0; |
621 | | |
622 | | /* |
623 | | printf("input: "); |
624 | | for (int j=0;j<nT;j++) { |
625 | | printf("%3d ",coeffs[c+j*nT]); |
626 | | } |
627 | | printf("\n"); |
628 | | |
629 | | printf("mat: "); |
630 | | for (int j=0;j<nT;j++) { |
631 | | printf("%3d ",mat_dct[fact*j][i]); |
632 | | } |
633 | | printf("\n"); |
634 | | */ |
635 | |
|
636 | 0 | for (int j=0;j<=lastCol /*nT*/;j++) { |
637 | 0 | sum += mat_dct[fact*j][i] * coeffs[c+j*nT]; |
638 | 0 | } |
639 | |
|
640 | 0 | g[c+i*nT] = Clip3(-32768,32767, (sum+rnd1)>>7); |
641 | |
|
642 | 0 | logtrace(LogTransform,"*%d ",g[c+i*nT]); |
643 | 0 | } |
644 | 0 | logtrace(LogTransform,"*\n"); |
645 | 0 | } |
646 | | |
647 | | /* |
648 | | printf("--- temp\n"); |
649 | | for (int r=0;r<nT;r++, printf("\n")) |
650 | | for (int c=0;c<nT;c++) { |
651 | | printf("%3d ",g[c+r*nT]); |
652 | | } |
653 | | */ |
654 | |
|
655 | 0 | for (int y=0;y<nT;y++) { |
656 | | /* |
657 | | logtrace(LogTransform,"DCT-H: "); |
658 | | for (int i=0;i<nT;i++) { |
659 | | logtrace(LogTransform,"*%d ",g[i+y*nT]); |
660 | | } |
661 | | logtrace(LogTransform,"* -> "); |
662 | | */ |
663 | | |
664 | | |
665 | | // find last non-zero coefficient to reduce computations carried out in DCT |
666 | |
|
667 | 0 | int lastCol = nT-1; |
668 | 0 | for (;lastCol>=0;lastCol--) { |
669 | 0 | if (g[y*nT+lastCol]) { break; } |
670 | 0 | } |
671 | | |
672 | |
|
673 | 0 | for (int i=0;i<nT;i++) { |
674 | 0 | int sum=0; |
675 | |
|
676 | 0 | for (int j=0;j<=lastCol /*nT*/;j++) { |
677 | 0 | sum += mat_dct[fact*j][i] * g[y*nT+j]; |
678 | 0 | } |
679 | | |
680 | | //int out = Clip3(-32768,32767, (sum+rnd2)>>postShift); |
681 | 0 | int out = (sum+rnd2)>>postShift; |
682 | | |
683 | | //fprintf(stderr,"%d*%d+%d = %d\n",y,stride,i,y*stride+i); |
684 | | //fprintf(stderr,"[%p]=%d\n",&dst[y*stride+i], Clip1_8bit(dst[y*stride+i])); |
685 | 0 | dst[y*stride+i] = Clip_BitDepth(dst[y*stride+i] + out, bit_depth); |
686 | |
|
687 | 0 | logtrace(LogTransform,"*%d ",out); |
688 | 0 | } |
689 | 0 | logtrace(LogTransform,"*\n"); |
690 | 0 | } |
691 | 0 | } Unexecuted instantiation: void transform_idct_add<unsigned char>(unsigned char*, long, int, short const*, int) Unexecuted instantiation: void transform_idct_add<unsigned short>(unsigned short*, long, int, short const*, int) |
692 | | |
693 | | |
694 | | |
695 | | void transform_idct_fallback(int32_t *dst, int nT, const int16_t *coeffs, int bdShift, int max_coeff_bits) |
696 | 0 | { |
697 | | /* |
698 | | The effective shift is |
699 | | 7 bits right for bit-depth 8, |
700 | | 6 bits right for bit-depth 9, |
701 | | 5 bits right for bit-depth 10. |
702 | | |
703 | | One transformation with raw transform filter values increases range be 2048 (=32*64). |
704 | | This equals 11 bits. |
705 | | |
706 | | Computation is independent of the block size. |
707 | | Each multiplication with the table includes a left shift of 6 bits. |
708 | | Hence, we have 2* 6 bits = 12 bits left shift. |
709 | | V-pass has fixed 7 bit right shift. |
710 | | H-pass has 20-BitDepth bit right shift; |
711 | | |
712 | | Effective shift 's' means: residual value 1 gives DC-coeff (1<<s). |
713 | | */ |
714 | | |
715 | |
|
716 | 0 | int rnd1 = 1<<(7-1); |
717 | 0 | int fact = (1<<(5-Log2(nT))); |
718 | | |
719 | | //int bdShift = 20-bit_depth; |
720 | 0 | int rnd2 = 1<<(bdShift-1); |
721 | |
|
722 | 0 | int16_t g[32*32]; // actually, only [nT*nT] used |
723 | |
|
724 | 0 | int CoeffMax = (1<<max_coeff_bits)-1; |
725 | 0 | int CoeffMin = -(1<<max_coeff_bits); |
726 | | |
727 | | // TODO: valgrind reports that dst[] contains uninitialized data. |
728 | | // Probably from intra-prediction. |
729 | | |
730 | | /* |
731 | | for (int i=0;i<nT*nT;i++) { |
732 | | printf("%d\n",coeffs[i]); |
733 | | } |
734 | | |
735 | | for (int y=0;y<nT;y++) { |
736 | | for (int i=0;i<nT;i++) { |
737 | | printf("%d ",dst[y*stride+i]); |
738 | | } |
739 | | } |
740 | | printf("\n"); |
741 | | */ |
742 | | |
743 | | /* |
744 | | printf("--- input\n"); |
745 | | for (int r=0;r<nT;r++, printf("\n")) |
746 | | for (int c=0;c<nT;c++) { |
747 | | printf("%3d ",coeffs[c+r*nT]); |
748 | | } |
749 | | */ |
750 | |
|
751 | 0 | for (int c=0;c<nT;c++) { |
752 | | |
753 | | /* |
754 | | logtrace(LogTransform,"DCT-V: "); |
755 | | for (int i=0;i<nT;i++) { |
756 | | logtrace(LogTransform,"*%d ",coeffs[c+i*nT]); |
757 | | } |
758 | | logtrace(LogTransform,"* -> "); |
759 | | */ |
760 | | |
761 | | |
762 | | // find last non-zero coefficient to reduce computations carried out in DCT |
763 | |
|
764 | 0 | int lastCol = nT-1; |
765 | 0 | for (;lastCol>=0;lastCol--) { |
766 | 0 | if (coeffs[c+lastCol*nT]) { break; } |
767 | 0 | } |
768 | |
|
769 | 0 | for (int i=0;i<nT;i++) { |
770 | 0 | int sum=0; |
771 | | |
772 | | /* |
773 | | printf("input: "); |
774 | | for (int j=0;j<nT;j++) { |
775 | | printf("%3d ",coeffs[c+j*nT]); |
776 | | } |
777 | | printf("\n"); |
778 | | |
779 | | printf("mat: "); |
780 | | for (int j=0;j<nT;j++) { |
781 | | printf("%3d ",mat_dct[fact*j][i]); |
782 | | } |
783 | | printf("\n"); |
784 | | */ |
785 | |
|
786 | 0 | for (int j=0;j<=lastCol /*nT*/;j++) { |
787 | 0 | sum += mat_dct[fact*j][i] * coeffs[c+j*nT]; |
788 | 0 | } |
789 | |
|
790 | 0 | g[c+i*nT] = Clip3(CoeffMin,CoeffMax, (sum+rnd1)>>7); |
791 | |
|
792 | 0 | logtrace(LogTransform,"*%d ",g[c+i*nT]); |
793 | 0 | } |
794 | 0 | logtrace(LogTransform,"*\n"); |
795 | 0 | } |
796 | | |
797 | | /* |
798 | | printf("--- temp\n"); |
799 | | for (int r=0;r<nT;r++, printf("\n")) |
800 | | for (int c=0;c<nT;c++) { |
801 | | printf("%3d ",g[c+r*nT]); |
802 | | } |
803 | | */ |
804 | |
|
805 | 0 | for (int y=0;y<nT;y++) { |
806 | | /* |
807 | | logtrace(LogTransform,"DCT-H: "); |
808 | | for (int i=0;i<nT;i++) { |
809 | | logtrace(LogTransform,"*%d ",g[i+y*nT]); |
810 | | } |
811 | | logtrace(LogTransform,"* -> "); |
812 | | */ |
813 | | |
814 | | |
815 | | // find last non-zero coefficient to reduce computations carried out in DCT |
816 | |
|
817 | 0 | int lastCol = nT-1; |
818 | 0 | for (;lastCol>=0;lastCol--) { |
819 | 0 | if (g[y*nT+lastCol]) { break; } |
820 | 0 | } |
821 | | |
822 | |
|
823 | 0 | for (int i=0;i<nT;i++) { |
824 | 0 | int sum=0; |
825 | |
|
826 | 0 | for (int j=0;j<=lastCol /*nT*/;j++) { |
827 | 0 | sum += mat_dct[fact*j][i] * g[y*nT+j]; |
828 | 0 | } |
829 | |
|
830 | 0 | dst[y*nT+i] = (sum + rnd2)>>bdShift; |
831 | |
|
832 | 0 | logtrace(LogTransform,"*%d ",sum); |
833 | 0 | } |
834 | 0 | logtrace(LogTransform,"*\n"); |
835 | 0 | } |
836 | 0 | } |
837 | | |
838 | | |
839 | | void transform_idct_4x4_fallback(int32_t *dst, const int16_t *coeffs, int bdShift, int max_coeff_bits) |
840 | 0 | { |
841 | 0 | transform_idct_fallback(dst,4,coeffs,bdShift,max_coeff_bits); |
842 | 0 | } |
843 | | |
844 | | void transform_idct_8x8_fallback(int32_t *dst, const int16_t *coeffs, int bdShift, int max_coeff_bits) |
845 | 0 | { |
846 | 0 | transform_idct_fallback(dst,8,coeffs,bdShift,max_coeff_bits); |
847 | 0 | } |
848 | | |
849 | | void transform_idct_16x16_fallback(int32_t *dst, const int16_t *coeffs, |
850 | | int bdShift, int max_coeff_bits) |
851 | 0 | { |
852 | 0 | transform_idct_fallback(dst,16,coeffs,bdShift,max_coeff_bits); |
853 | 0 | } |
854 | | |
855 | | void transform_idct_32x32_fallback(int32_t *dst, const int16_t *coeffs, |
856 | | int bdShift, int max_coeff_bits) |
857 | 0 | { |
858 | 0 | transform_idct_fallback(dst,32,coeffs,bdShift,max_coeff_bits); |
859 | 0 | } |
860 | | |
861 | | |
862 | | |
863 | | |
864 | | void transform_4x4_add_8_fallback(uint8_t *dst, const int16_t *coeffs, ptrdiff_t stride) |
865 | 0 | { |
866 | 0 | transform_idct_add<uint8_t>(dst,stride, 4, coeffs, 8); |
867 | 0 | } |
868 | | |
869 | | void transform_8x8_add_8_fallback(uint8_t *dst, const int16_t *coeffs, ptrdiff_t stride) |
870 | 0 | { |
871 | 0 | transform_idct_add<uint8_t>(dst,stride, 8, coeffs, 8); |
872 | 0 | } |
873 | | |
874 | | void transform_16x16_add_8_fallback(uint8_t *dst, const int16_t *coeffs, ptrdiff_t stride) |
875 | 0 | { |
876 | 0 | transform_idct_add<uint8_t>(dst,stride, 16, coeffs, 8); |
877 | 0 | } |
878 | | |
879 | | void transform_32x32_add_8_fallback(uint8_t *dst, const int16_t *coeffs, ptrdiff_t stride) |
880 | 0 | { |
881 | 0 | transform_idct_add<uint8_t>(dst,stride, 32, coeffs, 8); |
882 | 0 | } |
883 | | |
884 | | |
885 | | void transform_4x4_add_16_fallback(uint16_t *dst, const int16_t *coeffs, ptrdiff_t stride, int bit_depth) |
886 | 0 | { |
887 | 0 | transform_idct_add<uint16_t>(dst,stride, 4, coeffs, bit_depth); |
888 | 0 | } |
889 | | |
890 | | void transform_8x8_add_16_fallback(uint16_t *dst, const int16_t *coeffs, ptrdiff_t stride, int bit_depth) |
891 | 0 | { |
892 | 0 | transform_idct_add<uint16_t>(dst,stride, 8, coeffs, bit_depth); |
893 | 0 | } |
894 | | |
895 | | void transform_16x16_add_16_fallback(uint16_t *dst, const int16_t *coeffs, ptrdiff_t stride, int bit_depth) |
896 | 0 | { |
897 | 0 | transform_idct_add<uint16_t>(dst,stride, 16, coeffs, bit_depth); |
898 | 0 | } |
899 | | |
900 | | void transform_32x32_add_16_fallback(uint16_t *dst, const int16_t *coeffs, ptrdiff_t stride, int bit_depth) |
901 | 0 | { |
902 | 0 | transform_idct_add<uint16_t>(dst,stride, 32, coeffs, bit_depth); |
903 | 0 | } |
904 | | |
905 | | |
906 | | static void transform_fdct_8(int16_t* coeffs, int nT, |
907 | | const int16_t *input, ptrdiff_t stride) |
908 | 0 | { |
909 | | /* |
910 | | Each sum over a basis vector sums nT elements, which is compensated by |
911 | | shifting right by Log2(nT), effectively dividing by 2^Log2(nT) = nT. |
912 | | Do this in each of the H/V passes. |
913 | | |
914 | | Each multiplication with the table includes a left shift of 6 bits. |
915 | | Hence, we have in total 2* 6 bits = 12 bits left shift because of the |
916 | | multiplications. |
917 | | |
918 | | We carry out shifts after each pass: |
919 | | First (V) pass has BitDepth-9 bit right shift, |
920 | | Second (H) pass has fixed 6 bit right shift. |
921 | | |
922 | | For bit-depth 8, the total shift is 7 bits left. |
923 | | For bit-depth 9, the total shift is 6 bits left. |
924 | | For bit-depth 10, the total shift is 5 bits left. |
925 | | |
926 | | I.e.: a constant residual value 1 gives DC-coeff (1<<s). |
927 | | |
928 | | For 8-bit images in a 32x32 block, the input are 8 bits + 1 sign bit. |
929 | | After the first pass, we need 9+5+6=20 bits for the intermediate sum |
930 | | (9 bit input, 5 bit because we sum 2^5 elements, 6 bit because of multiplication with 64). |
931 | | The first pass shift is Log2(32) - 1 -> 4 bits and we are down to 16 bits again. |
932 | | After the second pass, we need 16+5+6=27 bits for the intermediate sum |
933 | | (16 bit input, 5 bit because we sum 2^5 elements, 6 bit because of coefficient multiplication). |
934 | | The second pass shift is Log2(32)+6 = 11 and we are down again to 16 bits. |
935 | | |
936 | | For larger input bit-depths, the intermediate result after the first pass |
937 | | will be wider accordingly, but the widths after the shifts are the same. |
938 | | */ |
939 | |
|
940 | 0 | int BitDepth = 8; |
941 | | |
942 | | // / compensate everything | / effective word length | |
943 | 0 | int shift1 = Log2(nT) + 6 + BitDepth - 15; |
944 | 0 | int shift2 = Log2(nT) + 6; |
945 | |
|
946 | 0 | int rnd1 = 1<<(shift1-1); |
947 | 0 | int rnd2 = 1<<(shift2-1); |
948 | 0 | int fact = (1<<(5-Log2(nT))); |
949 | |
|
950 | 0 | int16_t g[32*32]; // actually, only [nT*nT] used |
951 | |
|
952 | 0 | for (int c=0;c<nT;c++) { |
953 | |
|
954 | 0 | for (int i=0;i<nT;i++) { |
955 | 0 | int sum=0; |
956 | |
|
957 | 0 | for (int j=0;j<nT;j++) { |
958 | 0 | sum += mat_dct[fact*i][j] * input[c+j*stride]; |
959 | 0 | } |
960 | |
|
961 | 0 | g[c+i*nT] = (sum+rnd1)>>shift1; // clipping to -32768;32767 unnecessary |
962 | 0 | } |
963 | 0 | } |
964 | | |
965 | |
|
966 | 0 | for (int y=0;y<nT;y++) { |
967 | 0 | for (int i=0;i<nT;i++) { |
968 | 0 | int sum=0; |
969 | |
|
970 | 0 | for (int j=0;j<nT;j++) { |
971 | 0 | sum += mat_dct[fact*i][j] * g[y*nT+j]; |
972 | 0 | } |
973 | | |
974 | | // no clipping to -32768;32767 required |
975 | 0 | int out = (sum+rnd2)>>shift2; |
976 | |
|
977 | 0 | coeffs[y*nT+i] = out; |
978 | 0 | } |
979 | 0 | } |
980 | 0 | } |
981 | | |
982 | | |
983 | | void fdct_4x4_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride) |
984 | 0 | { |
985 | 0 | transform_fdct_8(coeffs, 4, input,stride); |
986 | 0 | } |
987 | | |
988 | | void fdct_8x8_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride) |
989 | 0 | { |
990 | 0 | transform_fdct_8(coeffs, 8, input,stride); |
991 | 0 | } |
992 | | |
993 | | void fdct_16x16_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride) |
994 | 0 | { |
995 | 0 | transform_fdct_8(coeffs, 16, input,stride); |
996 | 0 | } |
997 | | |
998 | | void fdct_32x32_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride) |
999 | 0 | { |
1000 | 0 | transform_fdct_8(coeffs, 32, input,stride); |
1001 | 0 | } |
1002 | | |
1003 | | |
1004 | | |
1005 | | |
1006 | | void hadamard_transform_8(int16_t *coeffs, int n, const int16_t *input, ptrdiff_t stride) |
1007 | 0 | { |
1008 | 0 | int16_t tmp[32*32]; |
1009 | | |
1010 | | // row transforms |
1011 | | |
1012 | | //printMatrix("input",input,n); |
1013 | |
|
1014 | 0 | int16_t am[32],bm[32]; |
1015 | 0 | int16_t *a = am, *b = bm; |
1016 | 0 | for (int row=0;row<n;row++) { |
1017 | 0 | int rs = row*stride; |
1018 | 0 | for (int i=0;i<(n>>1);i++) { |
1019 | 0 | a[ i] = input[i+rs] + input[i+(n>>1)+rs]; |
1020 | 0 | a[(n>>1)+i] = input[i+rs] - input[i+(n>>1)+rs]; |
1021 | 0 | } |
1022 | |
|
1023 | 0 | int iOuter=(n>>1); |
1024 | 0 | int nInner=(n>>2); |
1025 | |
|
1026 | 0 | while (nInner>=2) { |
1027 | 0 | std::swap(a,b); |
1028 | |
|
1029 | 0 | for (int k=0;k<n;k+=iOuter) { |
1030 | 0 | for (int i=0;i<nInner;i++) { |
1031 | 0 | a[k+i ] = b[k+i] + b[k+i+nInner]; |
1032 | 0 | a[k+i+nInner] = b[k+i] - b[k+i+nInner]; |
1033 | 0 | } |
1034 | 0 | } |
1035 | |
|
1036 | 0 | iOuter>>=1; |
1037 | 0 | nInner>>=1; |
1038 | 0 | } |
1039 | |
|
1040 | 0 | for (int k=0;k<n;k+=2) { |
1041 | 0 | tmp[k +n*row] = a[k] + a[k+1]; |
1042 | 0 | tmp[k+1+n*row] = a[k] - a[k+1]; |
1043 | 0 | } |
1044 | 0 | } |
1045 | | |
1046 | | //printMatrix("tmp",tmp,n); |
1047 | | |
1048 | | // column transforms |
1049 | |
|
1050 | 0 | for (int col=0;col<n;col++) { |
1051 | 0 | for (int i=0;i<(n>>1);i++) { |
1052 | 0 | a[ i] = tmp[i*n+col] + tmp[(i+(n>>1))*n+col]; |
1053 | 0 | a[(n>>1)+i] = tmp[i*n+col] - tmp[(i+(n>>1))*n+col]; |
1054 | 0 | } |
1055 | |
|
1056 | 0 | int iOuter=(n>>1); |
1057 | 0 | int nInner=(n>>2); |
1058 | |
|
1059 | 0 | while (nInner>=2) { |
1060 | 0 | std::swap(a,b); |
1061 | |
|
1062 | 0 | for (int k=0;k<n;k+=iOuter) { |
1063 | 0 | for (int i=0;i<nInner;i++) { |
1064 | 0 | a[k+i ] = b[k+i] + b[k+i+nInner]; |
1065 | 0 | a[k+i+nInner] = b[k+i] - b[k+i+nInner]; |
1066 | 0 | } |
1067 | 0 | } |
1068 | |
|
1069 | 0 | iOuter>>=1; |
1070 | 0 | nInner>>=1; |
1071 | 0 | } |
1072 | |
|
1073 | 0 | for (int k=0;k<n;k+=2) { |
1074 | 0 | coeffs[col+(k )*n] = a[k] + a[k+1]; |
1075 | 0 | coeffs[col+(k+1)*n] = a[k] - a[k+1]; |
1076 | 0 | } |
1077 | 0 | } |
1078 | | |
1079 | | //printMatrix("coeffs",coeffs,n); |
1080 | 0 | } |
1081 | | |
1082 | | |
1083 | | void hadamard_4x4_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride) |
1084 | 0 | { |
1085 | 0 | int16_t tmp[4*4]; |
1086 | | |
1087 | | // row transforms |
1088 | | |
1089 | | //printMatrix("input",input,4); |
1090 | |
|
1091 | 0 | int16_t a[4]; |
1092 | 0 | for (int row=0;row<4;row++) { |
1093 | 0 | int rs = row*stride; |
1094 | 0 | a[0] = input[0+rs] + input[2+rs]; |
1095 | 0 | a[1] = input[1+rs] + input[3+rs]; |
1096 | 0 | a[2] = input[0+rs] - input[2+rs]; |
1097 | 0 | a[3] = input[1+rs] - input[3+rs]; |
1098 | |
|
1099 | 0 | tmp[0+4*row] = a[0]+a[1]; |
1100 | 0 | tmp[1+4*row] = a[0]-a[1]; |
1101 | 0 | tmp[2+4*row] = a[2]+a[3]; |
1102 | 0 | tmp[3+4*row] = a[2]-a[3]; |
1103 | 0 | } |
1104 | | |
1105 | | //printMatrix("tmp",tmp,4); |
1106 | | |
1107 | | // column transforms |
1108 | |
|
1109 | 0 | for (int col=0;col<4;col++) { |
1110 | 0 | a[0] = tmp[col+0*4] + tmp[col+2*4]; |
1111 | 0 | a[1] = tmp[col+1*4] + tmp[col+3*4]; |
1112 | 0 | a[2] = tmp[col+0*4] - tmp[col+2*4]; |
1113 | 0 | a[3] = tmp[col+1*4] - tmp[col+3*4]; |
1114 | |
|
1115 | 0 | coeffs[col+0*4] = a[0]+a[1]; |
1116 | 0 | coeffs[col+1*4] = a[0]-a[1]; |
1117 | 0 | coeffs[col+2*4] = a[2]+a[3]; |
1118 | 0 | coeffs[col+3*4] = a[2]-a[3]; |
1119 | 0 | } |
1120 | | |
1121 | | //printMatrix("coeffs",coeffs,4); |
1122 | 0 | } |
1123 | | |
1124 | | |
1125 | | void hadamard_8x8_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride) |
1126 | 0 | { |
1127 | 0 | int16_t tmp[8*8]; |
1128 | | |
1129 | | // row transforms |
1130 | | |
1131 | | //printMatrix("input",input,8); |
1132 | |
|
1133 | 0 | int16_t a[8],b[8]; |
1134 | 0 | for (int row=0;row<8;row++) { |
1135 | 0 | int rs = row*stride; |
1136 | 0 | a[0] = input[0+rs] + input[4+rs]; |
1137 | 0 | a[1] = input[1+rs] + input[5+rs]; |
1138 | 0 | a[2] = input[2+rs] + input[6+rs]; |
1139 | 0 | a[3] = input[3+rs] + input[7+rs]; |
1140 | 0 | a[4] = input[0+rs] - input[4+rs]; |
1141 | 0 | a[5] = input[1+rs] - input[5+rs]; |
1142 | 0 | a[6] = input[2+rs] - input[6+rs]; |
1143 | 0 | a[7] = input[3+rs] - input[7+rs]; |
1144 | |
|
1145 | 0 | b[0] = a[0]+a[2]; |
1146 | 0 | b[1] = a[1]+a[3]; |
1147 | 0 | b[2] = a[0]-a[2]; |
1148 | 0 | b[3] = a[1]-a[3]; |
1149 | 0 | b[4] = a[4]+a[6]; |
1150 | 0 | b[5] = a[5]+a[7]; |
1151 | 0 | b[6] = a[4]-a[6]; |
1152 | 0 | b[7] = a[5]-a[7]; |
1153 | |
|
1154 | 0 | tmp[0+8*row] = b[0]+b[1]; |
1155 | 0 | tmp[1+8*row] = b[0]-b[1]; |
1156 | 0 | tmp[2+8*row] = b[2]+b[3]; |
1157 | 0 | tmp[3+8*row] = b[2]-b[3]; |
1158 | 0 | tmp[4+8*row] = b[4]+b[5]; |
1159 | 0 | tmp[5+8*row] = b[4]-b[5]; |
1160 | 0 | tmp[6+8*row] = b[6]+b[7]; |
1161 | 0 | tmp[7+8*row] = b[6]-b[7]; |
1162 | 0 | } |
1163 | | |
1164 | | //printMatrix("tmp",tmp,8); |
1165 | | |
1166 | | // column transforms |
1167 | |
|
1168 | 0 | for (int col=0;col<8;col++) { |
1169 | 0 | a[0] = tmp[col+0*8] + tmp[col+4*8]; |
1170 | 0 | a[1] = tmp[col+1*8] + tmp[col+5*8]; |
1171 | 0 | a[2] = tmp[col+2*8] + tmp[col+6*8]; |
1172 | 0 | a[3] = tmp[col+3*8] + tmp[col+7*8]; |
1173 | 0 | a[4] = tmp[col+0*8] - tmp[col+4*8]; |
1174 | 0 | a[5] = tmp[col+1*8] - tmp[col+5*8]; |
1175 | 0 | a[6] = tmp[col+2*8] - tmp[col+6*8]; |
1176 | 0 | a[7] = tmp[col+3*8] - tmp[col+7*8]; |
1177 | |
|
1178 | 0 | b[0] = a[0]+a[2]; |
1179 | 0 | b[1] = a[1]+a[3]; |
1180 | 0 | b[2] = a[0]-a[2]; |
1181 | 0 | b[3] = a[1]-a[3]; |
1182 | 0 | b[4] = a[4]+a[6]; |
1183 | 0 | b[5] = a[5]+a[7]; |
1184 | 0 | b[6] = a[4]-a[6]; |
1185 | 0 | b[7] = a[5]-a[7]; |
1186 | |
|
1187 | 0 | coeffs[col+0*8] = b[0]+b[1]; |
1188 | 0 | coeffs[col+1*8] = b[0]-b[1]; |
1189 | 0 | coeffs[col+2*8] = b[2]+b[3]; |
1190 | 0 | coeffs[col+3*8] = b[2]-b[3]; |
1191 | 0 | coeffs[col+4*8] = b[4]+b[5]; |
1192 | 0 | coeffs[col+5*8] = b[4]-b[5]; |
1193 | 0 | coeffs[col+6*8] = b[6]+b[7]; |
1194 | 0 | coeffs[col+7*8] = b[6]-b[7]; |
1195 | 0 | } |
1196 | | |
1197 | | //printMatrix("coeffs",coeffs,8); |
1198 | 0 | } |
1199 | | |
1200 | | |
1201 | | void hadamard_16x16_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride) |
1202 | 0 | { |
1203 | 0 | hadamard_transform_8(coeffs,16, input,stride); |
1204 | 0 | } |
1205 | | |
1206 | | void hadamard_32x32_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride) |
1207 | 0 | { |
1208 | 0 | hadamard_transform_8(coeffs,32, input,stride); |
1209 | 0 | } |