Coverage Report

Created: 2026-09-14 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
      int32_t c = (int32_t)((uint32_t)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
      int32_t c = (int32_t)((uint32_t)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
      int32_t c = (int32_t)((uint32_t)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
      int32_t c = (int32_t)((uint32_t)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
      // no clipping to -32768;32767 required
328
0
      int out = (sum+rndH)>>postShift;
329
330
0
      dst[y*stride+i] = Clip1_8bit(dst[y*stride+i] + out);
331
332
0
      logtrace(LogTransform,"*%d ",out);
333
0
    }
334
335
0
    logtrace(LogTransform,"*\n");
336
0
  }
337
0
}
338
339
340
void transform_4x4_luma_add_16_fallback(uint16_t *dst, const int16_t *coeffs, ptrdiff_t stride,
341
                                        int bit_depth)
342
0
{
343
0
  int16_t g[4][4];
344
345
0
  int postShift = 20-bit_depth;
346
0
  int rndV = 1<<(7-1);
347
0
  int rndH = 1<<(postShift-1);
348
349
350
  // --- V ---
351
352
0
  for (int c=0;c<4;c++) {
353
    /*
354
    logtrace(LogTransform,"DST-V: ");
355
    for (int r=0;r<4;r++) {
356
      logtrace(LogTransform,"%d ",coeffs[c+r*4]);
357
    }
358
    logtrace(LogTransform,"* -> ");
359
    */
360
361
0
    for (int i=0;i<4;i++) {
362
0
      int sum=0;
363
364
0
      for (int j=0;j<4;j++) {
365
0
        sum += mat_8_357[j][i] * coeffs[c+j*4];
366
0
      }
367
368
0
      g[i][c] = Clip3(-32768,32767, (sum+rndV)>>7);
369
0
    }
370
371
    /*
372
    for (int y=0;y<4;y++) {
373
      logtrace(LogTransform,"*%d ",g[y][c]);
374
    }
375
    logtrace(LogTransform,"*\n");
376
    */
377
0
  }
378
379
380
  // --- H ---
381
382
0
  for (int y=0;y<4;y++) {
383
384
    /*
385
    logtrace(LogTransform,"DST-H: ");
386
    for (int c=0;c<4;c++) {
387
      logtrace(LogTransform,"%d ",g[y][c]);
388
    }
389
    logtrace(LogTransform,"* -> ");
390
    */
391
392
0
    for (int i=0;i<4;i++) {
393
0
      int sum=0;
394
395
0
      for (int j=0;j<4;j++) {
396
0
        sum += mat_8_357[j][i] * g[y][j];
397
0
      }
398
399
      // no clipping to -32768;32767 required
400
      // (at bit depth 16 the residual needs 17 bits and clipping breaks it)
401
0
      int out = (sum+rndH)>>postShift;
402
403
0
      dst[y*stride+i] = Clip_BitDepth(dst[y*stride+i] + out, bit_depth);
404
405
0
      logtrace(LogTransform,"*%d ",out);
406
0
    }
407
408
0
    logtrace(LogTransform,"*\n");
409
0
  }
410
0
}
411
412
413
void fdst_4x4_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride)
414
0
{
415
0
  int16_t g[4*4];
416
417
0
  int BD = 8;
418
0
  int shift1 = Log2(4) + BD -9;
419
0
  int shift2 = Log2(4) + 6;
420
421
0
  int rnd1 = 1<<(shift1-1);
422
0
  int rnd2 = 1<<(shift2-1);
423
424
425
  // --- V ---
426
427
0
  for (int c=0;c<4;c++) {
428
429
    /*
430
    logtrace(LogTransform,"DST-V: ");
431
    for (int r=0;r<4;r++) {
432
      logtrace(LogTransform,"%d ",coeffs[c+r*4]);
433
    }
434
    logtrace(LogTransform,"* -> ");
435
    */
436
437
0
    for (int i=0;i<4;i++) {
438
0
      int sum=0;
439
440
0
      for (int j=0;j<4;j++) {
441
0
        sum += mat_8_357[i][j] * input[c+j*stride];
442
0
      }
443
444
0
      g[c+4*i] = Clip3(-32768,32767, (sum+rnd1)>>shift1);
445
0
    }
446
0
  }
447
448
449
  // --- H ---
450
451
0
  for (int y=0;y<4;y++) {
452
0
    for (int i=0;i<4;i++) {
453
0
      int sum=0;
454
455
0
      for (int j=0;j<4;j++) {
456
0
        sum += mat_8_357[i][j] * g[y*4+j];
457
0
      }
458
459
      // TODO: do we need clipping ?
460
0
      int out = (sum+rnd2)>>shift2; // Clip3(-32768,32767, (sum+rndH)>>postShift);
461
462
0
      coeffs[y*4+i] = out;
463
464
0
      logtrace(LogTransform,"*%d ",out);
465
0
    }
466
467
0
    logtrace(LogTransform,"*\n");
468
0
  }
469
0
}
470
471
472
void transform_idst_4x4_fallback(int32_t *dst, const int16_t *coeffs, int bdShift, int max_coeff_bits)
473
0
{
474
0
  int16_t g[4][4];
475
476
0
  int rndV = 1<<(7-1);
477
0
  int rndH = 1<<(bdShift-1);
478
479
0
  int CoeffMax = (1<<max_coeff_bits)-1;
480
0
  int CoeffMin = -(1<<max_coeff_bits);
481
482
483
  // --- V ---
484
485
0
  for (int c=0;c<4;c++) {
486
0
    for (int i=0;i<4;i++) {
487
0
      int sum=0;
488
489
0
      for (int j=0;j<4;j++) {
490
0
        sum += mat_8_357[j][i] * coeffs[c+j*4];
491
0
      }
492
493
0
      g[i][c] = Clip3(CoeffMin,CoeffMax, (sum+rndV)>>7);
494
0
    }
495
0
  }
496
497
498
  // --- H ---
499
500
0
  for (int y=0;y<4;y++) {
501
0
    for (int i=0;i<4;i++) {
502
0
      int sum=0;
503
504
0
      for (int j=0;j<4;j++) {
505
0
        sum += mat_8_357[j][i] * g[y][j];
506
0
      }
507
508
0
      dst[y*4+i] = (sum + rndH)>>bdShift;
509
0
    }
510
0
  }
511
0
}
512
513
514
515
static int8_t mat_dct[32][32] = {
516
  { 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},
517
  { 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},
518
  { 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},
519
  { 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},
520
  { 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},
521
  { 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},
522
  { 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},
523
  { 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},
524
  { 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},
525
  { 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},
526
  { 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},
527
  { 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},
528
  { 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},
529
  { 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},
530
  { 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},
531
  { 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},
532
  { 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},
533
  { 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},
534
  { 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},
535
  { 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},
536
  { 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},
537
  { 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},
538
  { 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},
539
  { 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},
540
  { 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},
541
  { 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},
542
  { 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},
543
  { 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},
544
  { 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},
545
  { 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},
546
  {  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},
547
  {  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}
548
};
549
550
551
552
553
template <class pixel_t>
554
void transform_idct_add(pixel_t *dst, ptrdiff_t stride,
555
                        int nT, const int16_t *coeffs, int bit_depth)
556
0
{
557
  /*
558
    The effective shift is
559
    7 bits right for bit-depth 8,
560
    6 bits right for bit-depth 9,
561
    5 bits right for bit-depth 10.
562
563
    Computation is independent of the block size.
564
    Each multiplication with the table includes a left shift of 6 bits.
565
    Hence, we have 2* 6 bits = 12 bits left shift.
566
    V-pass has fixed 7 bit right shift.
567
    H-pass has 20-BitDepth bit right shift;
568
569
    Effective shift 's' means: residual value 1 gives DC-coeff (1<<s).
570
   */
571
572
573
0
  int postShift = 20-bit_depth;
574
0
  int rnd1 = 1<<(7-1);
575
0
  int rnd2 = 1<<(postShift-1);
576
0
  int fact = (1<<(5-Log2(nT)));
577
578
0
  int16_t g[32*32];  // actually, only [nT*nT] used
579
580
  // TODO: valgrind reports that dst[] contains uninitialized data.
581
  // Probably from intra-prediction.
582
583
  /*
584
  for (int i=0;i<nT*nT;i++) {
585
    printf("%d\n",coeffs[i]);
586
  }
587
588
  for (int y=0;y<nT;y++) {
589
    for (int i=0;i<nT;i++) {
590
      printf("%d ",dst[y*stride+i]);
591
    }
592
  }
593
  printf("\n");
594
  */
595
596
  /*
597
  printf("--- input\n");
598
  for (int r=0;r<nT;r++, printf("\n"))
599
    for (int c=0;c<nT;c++) {
600
      printf("%3d ",coeffs[c+r*nT]);
601
    }
602
  */
603
604
0
  for (int c=0;c<nT;c++) {
605
606
    /*
607
    logtrace(LogTransform,"DCT-V: ");
608
    for (int i=0;i<nT;i++) {
609
      logtrace(LogTransform,"*%d ",coeffs[c+i*nT]);
610
    }
611
    logtrace(LogTransform,"* -> ");
612
    */
613
614
615
    // find last non-zero coefficient to reduce computations carried out in DCT
616
617
0
    int lastCol = nT-1;
618
0
    for (;lastCol>=0;lastCol--) {
619
0
      if (coeffs[c+lastCol*nT]) { break; }
620
0
    }
621
622
0
    for (int i=0;i<nT;i++) {
623
0
      int sum=0;
624
625
      /*
626
      printf("input: ");
627
      for (int j=0;j<nT;j++) {
628
        printf("%3d ",coeffs[c+j*nT]);
629
      }
630
      printf("\n");
631
632
      printf("mat: ");
633
      for (int j=0;j<nT;j++) {
634
        printf("%3d ",mat_dct[fact*j][i]);
635
      }
636
      printf("\n");
637
      */
638
639
0
      for (int j=0;j<=lastCol /*nT*/;j++) {
640
0
        sum += mat_dct[fact*j][i] * coeffs[c+j*nT];
641
0
      }
642
643
0
      g[c+i*nT] = Clip3(-32768,32767, (sum+rnd1)>>7);
644
645
0
      logtrace(LogTransform,"*%d ",g[c+i*nT]);
646
0
    }
647
0
    logtrace(LogTransform,"*\n");
648
0
  }
649
650
  /*
651
  printf("--- temp\n");
652
  for (int r=0;r<nT;r++, printf("\n"))
653
    for (int c=0;c<nT;c++) {
654
      printf("%3d ",g[c+r*nT]);
655
    }
656
  */
657
658
0
  for (int y=0;y<nT;y++) {
659
    /*
660
    logtrace(LogTransform,"DCT-H: ");
661
    for (int i=0;i<nT;i++) {
662
      logtrace(LogTransform,"*%d ",g[i+y*nT]);
663
    }
664
    logtrace(LogTransform,"* -> ");
665
    */
666
667
668
    // find last non-zero coefficient to reduce computations carried out in DCT
669
670
0
    int lastCol = nT-1;
671
0
    for (;lastCol>=0;lastCol--) {
672
0
      if (g[y*nT+lastCol]) { break; }
673
0
    }
674
675
676
0
    for (int i=0;i<nT;i++) {
677
0
      int sum=0;
678
679
0
      for (int j=0;j<=lastCol /*nT*/;j++) {
680
0
        sum += mat_dct[fact*j][i] * g[y*nT+j];
681
0
      }
682
683
      //int out = Clip3(-32768,32767, (sum+rnd2)>>postShift);
684
0
      int out = (sum+rnd2)>>postShift;
685
686
      //fprintf(stderr,"%d*%d+%d = %d\n",y,stride,i,y*stride+i);
687
      //fprintf(stderr,"[%p]=%d\n",&dst[y*stride+i], Clip1_8bit(dst[y*stride+i]));
688
0
      dst[y*stride+i] = Clip_BitDepth(dst[y*stride+i] + out, bit_depth);
689
690
0
      logtrace(LogTransform,"*%d ",out);
691
0
    }
692
0
    logtrace(LogTransform,"*\n");
693
0
  }
694
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)
695
696
697
698
void transform_idct_fallback(int32_t *dst, int nT, const int16_t *coeffs, int bdShift, int max_coeff_bits)
699
0
{
700
  /*
701
    The effective shift is
702
    7 bits right for bit-depth 8,
703
    6 bits right for bit-depth 9,
704
    5 bits right for bit-depth 10.
705
706
    One transformation with raw transform filter values increases range be 2048 (=32*64).
707
    This equals 11 bits.
708
709
    Computation is independent of the block size.
710
    Each multiplication with the table includes a left shift of 6 bits.
711
    Hence, we have 2* 6 bits = 12 bits left shift.
712
    V-pass has fixed 7 bit right shift.
713
    H-pass has 20-BitDepth bit right shift;
714
715
    Effective shift 's' means: residual value 1 gives DC-coeff (1<<s).
716
   */
717
718
719
0
  int rnd1 = 1<<(7-1);
720
0
  int fact = (1<<(5-Log2(nT)));
721
722
  //int bdShift = 20-bit_depth;
723
0
  int rnd2 = 1<<(bdShift-1);
724
725
0
  int16_t g[32*32];  // actually, only [nT*nT] used
726
727
0
  int CoeffMax = (1<<max_coeff_bits)-1;
728
0
  int CoeffMin = -(1<<max_coeff_bits);
729
730
  // TODO: valgrind reports that dst[] contains uninitialized data.
731
  // Probably from intra-prediction.
732
733
  /*
734
  for (int i=0;i<nT*nT;i++) {
735
    printf("%d\n",coeffs[i]);
736
  }
737
738
  for (int y=0;y<nT;y++) {
739
    for (int i=0;i<nT;i++) {
740
      printf("%d ",dst[y*stride+i]);
741
    }
742
  }
743
  printf("\n");
744
  */
745
746
  /*
747
  printf("--- input\n");
748
  for (int r=0;r<nT;r++, printf("\n"))
749
    for (int c=0;c<nT;c++) {
750
      printf("%3d ",coeffs[c+r*nT]);
751
    }
752
  */
753
754
0
  for (int c=0;c<nT;c++) {
755
756
    /*
757
    logtrace(LogTransform,"DCT-V: ");
758
    for (int i=0;i<nT;i++) {
759
      logtrace(LogTransform,"*%d ",coeffs[c+i*nT]);
760
    }
761
    logtrace(LogTransform,"* -> ");
762
    */
763
764
765
    // find last non-zero coefficient to reduce computations carried out in DCT
766
767
0
    int lastCol = nT-1;
768
0
    for (;lastCol>=0;lastCol--) {
769
0
      if (coeffs[c+lastCol*nT]) { break; }
770
0
    }
771
772
0
    for (int i=0;i<nT;i++) {
773
0
      int sum=0;
774
775
      /*
776
      printf("input: ");
777
      for (int j=0;j<nT;j++) {
778
        printf("%3d ",coeffs[c+j*nT]);
779
      }
780
      printf("\n");
781
782
      printf("mat: ");
783
      for (int j=0;j<nT;j++) {
784
        printf("%3d ",mat_dct[fact*j][i]);
785
      }
786
      printf("\n");
787
      */
788
789
0
      for (int j=0;j<=lastCol /*nT*/;j++) {
790
0
        sum += mat_dct[fact*j][i] * coeffs[c+j*nT];
791
0
      }
792
793
0
      g[c+i*nT] = Clip3(CoeffMin,CoeffMax, (sum+rnd1)>>7);
794
795
0
      logtrace(LogTransform,"*%d ",g[c+i*nT]);
796
0
    }
797
0
    logtrace(LogTransform,"*\n");
798
0
  }
799
800
  /*
801
  printf("--- temp\n");
802
  for (int r=0;r<nT;r++, printf("\n"))
803
    for (int c=0;c<nT;c++) {
804
      printf("%3d ",g[c+r*nT]);
805
    }
806
  */
807
808
0
  for (int y=0;y<nT;y++) {
809
    /*
810
    logtrace(LogTransform,"DCT-H: ");
811
    for (int i=0;i<nT;i++) {
812
      logtrace(LogTransform,"*%d ",g[i+y*nT]);
813
    }
814
    logtrace(LogTransform,"* -> ");
815
    */
816
817
818
    // find last non-zero coefficient to reduce computations carried out in DCT
819
820
0
    int lastCol = nT-1;
821
0
    for (;lastCol>=0;lastCol--) {
822
0
      if (g[y*nT+lastCol]) { break; }
823
0
    }
824
825
826
0
    for (int i=0;i<nT;i++) {
827
0
      int sum=0;
828
829
0
      for (int j=0;j<=lastCol /*nT*/;j++) {
830
0
        sum += mat_dct[fact*j][i] * g[y*nT+j];
831
0
      }
832
833
0
      dst[y*nT+i] = (sum + rnd2)>>bdShift;
834
835
0
      logtrace(LogTransform,"*%d ",sum);
836
0
    }
837
0
    logtrace(LogTransform,"*\n");
838
0
  }
839
0
}
840
841
842
void transform_idct_4x4_fallback(int32_t *dst, const int16_t *coeffs, int bdShift, int max_coeff_bits)
843
0
{
844
0
  transform_idct_fallback(dst,4,coeffs,bdShift,max_coeff_bits);
845
0
}
846
847
void transform_idct_8x8_fallback(int32_t *dst, const int16_t *coeffs, int bdShift, int max_coeff_bits)
848
0
{
849
0
  transform_idct_fallback(dst,8,coeffs,bdShift,max_coeff_bits);
850
0
}
851
852
void transform_idct_16x16_fallback(int32_t *dst, const int16_t *coeffs,
853
                                   int bdShift, int max_coeff_bits)
854
0
{
855
0
  transform_idct_fallback(dst,16,coeffs,bdShift,max_coeff_bits);
856
0
}
857
858
void transform_idct_32x32_fallback(int32_t *dst, const int16_t *coeffs,
859
                                   int bdShift, int max_coeff_bits)
860
0
{
861
0
  transform_idct_fallback(dst,32,coeffs,bdShift,max_coeff_bits);
862
0
}
863
864
865
866
867
void transform_4x4_add_8_fallback(uint8_t *dst, const int16_t *coeffs, ptrdiff_t stride)
868
0
{
869
0
  transform_idct_add<uint8_t>(dst,stride,  4, coeffs, 8);
870
0
}
871
872
void transform_8x8_add_8_fallback(uint8_t *dst, const int16_t *coeffs, ptrdiff_t stride)
873
0
{
874
0
  transform_idct_add<uint8_t>(dst,stride,  8, coeffs, 8);
875
0
}
876
877
void transform_16x16_add_8_fallback(uint8_t *dst, const int16_t *coeffs, ptrdiff_t stride)
878
0
{
879
0
  transform_idct_add<uint8_t>(dst,stride,  16, coeffs, 8);
880
0
}
881
882
void transform_32x32_add_8_fallback(uint8_t *dst, const int16_t *coeffs, ptrdiff_t stride)
883
0
{
884
0
  transform_idct_add<uint8_t>(dst,stride,  32, coeffs, 8);
885
0
}
886
887
888
void transform_4x4_add_16_fallback(uint16_t *dst, const int16_t *coeffs, ptrdiff_t stride, int bit_depth)
889
0
{
890
0
  transform_idct_add<uint16_t>(dst,stride,  4, coeffs, bit_depth);
891
0
}
892
893
void transform_8x8_add_16_fallback(uint16_t *dst, const int16_t *coeffs, ptrdiff_t stride, int bit_depth)
894
0
{
895
0
  transform_idct_add<uint16_t>(dst,stride,  8, coeffs, bit_depth);
896
0
}
897
898
void transform_16x16_add_16_fallback(uint16_t *dst, const int16_t *coeffs, ptrdiff_t stride, int bit_depth)
899
0
{
900
0
  transform_idct_add<uint16_t>(dst,stride,  16, coeffs, bit_depth);
901
0
}
902
903
void transform_32x32_add_16_fallback(uint16_t *dst, const int16_t *coeffs, ptrdiff_t stride, int bit_depth)
904
0
{
905
0
  transform_idct_add<uint16_t>(dst,stride,  32, coeffs, bit_depth);
906
0
}
907
908
909
static void transform_fdct_8(int16_t* coeffs, int nT,
910
                             const int16_t *input, ptrdiff_t stride)
911
0
{
912
  /*
913
    Each sum over a basis vector sums nT elements, which is compensated by
914
    shifting right by Log2(nT), effectively dividing by 2^Log2(nT) = nT.
915
    Do this in each of the H/V passes.
916
917
    Each multiplication with the table includes a left shift of 6 bits.
918
    Hence, we have in total 2* 6 bits = 12 bits left shift because of the
919
    multiplications.
920
921
    We carry out shifts after each pass:
922
    First (V) pass has BitDepth-9 bit right shift,
923
    Second (H) pass has fixed 6 bit right shift.
924
925
    For bit-depth 8, the total shift is 7 bits left.
926
    For bit-depth 9, the total shift is 6 bits left.
927
    For bit-depth 10, the total shift is 5 bits left.
928
929
    I.e.: a constant residual value 1 gives DC-coeff (1<<s).
930
931
    For 8-bit images in a 32x32 block, the input are 8 bits + 1 sign bit.
932
    After the first pass, we need 9+5+6=20 bits for the intermediate sum
933
    (9 bit input, 5 bit because we sum 2^5 elements, 6 bit because of multiplication with 64).
934
    The first pass shift is Log2(32) - 1 -> 4 bits and we are down to 16 bits again.
935
    After the second pass, we need 16+5+6=27 bits for the intermediate sum
936
    (16 bit input, 5 bit because we sum 2^5 elements, 6 bit because of coefficient multiplication).
937
    The second pass shift is Log2(32)+6 = 11 and we are down again to 16 bits.
938
939
    For larger input bit-depths, the intermediate result after the first pass
940
    will be wider accordingly, but the widths after the shifts are the same.
941
  */
942
943
0
  int BitDepth = 8;
944
945
  //          / compensate everything | / effective word length |
946
0
  int shift1 = Log2(nT) + 6 + BitDepth  - 15;
947
0
  int shift2 = Log2(nT) + 6;
948
949
0
  int rnd1 = 1<<(shift1-1);
950
0
  int rnd2 = 1<<(shift2-1);
951
0
  int fact = (1<<(5-Log2(nT)));
952
953
0
  int16_t g[32*32];  // actually, only [nT*nT] used
954
955
0
  for (int c=0;c<nT;c++) {
956
957
0
    for (int i=0;i<nT;i++) {
958
0
      int sum=0;
959
960
0
      for (int j=0;j<nT;j++) {
961
0
        sum += mat_dct[fact*i][j] * input[c+j*stride];
962
0
      }
963
964
0
      g[c+i*nT] = (sum+rnd1)>>shift1; // clipping to -32768;32767 unnecessary
965
0
    }
966
0
  }
967
968
969
0
  for (int y=0;y<nT;y++) {
970
0
    for (int i=0;i<nT;i++) {
971
0
      int sum=0;
972
973
0
      for (int j=0;j<nT;j++) {
974
0
        sum += mat_dct[fact*i][j] * g[y*nT+j];
975
0
      }
976
977
      // no clipping to -32768;32767 required
978
0
      int out = (sum+rnd2)>>shift2;
979
980
0
      coeffs[y*nT+i] = out;
981
0
    }
982
0
  }
983
0
}
984
985
986
void fdct_4x4_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride)
987
0
{
988
0
  transform_fdct_8(coeffs, 4, input,stride);
989
0
}
990
991
void fdct_8x8_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride)
992
0
{
993
0
  transform_fdct_8(coeffs, 8, input,stride);
994
0
}
995
996
void fdct_16x16_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride)
997
0
{
998
0
  transform_fdct_8(coeffs, 16, input,stride);
999
0
}
1000
1001
void fdct_32x32_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride)
1002
0
{
1003
0
  transform_fdct_8(coeffs, 32, input,stride);
1004
0
}
1005
1006
1007
1008
1009
void hadamard_transform_8(int16_t *coeffs, int n, const int16_t *input, ptrdiff_t stride)
1010
0
{
1011
0
  int16_t tmp[32*32];
1012
1013
  // row transforms
1014
1015
  //printMatrix("input",input,n);
1016
1017
0
  int16_t am[32],bm[32];
1018
0
  int16_t *a = am, *b = bm;
1019
0
  for (int row=0;row<n;row++) {
1020
0
    int rs = row*stride;
1021
0
    for (int i=0;i<(n>>1);i++) {
1022
0
      a[       i] = input[i+rs] + input[i+(n>>1)+rs];
1023
0
      a[(n>>1)+i] = input[i+rs] - input[i+(n>>1)+rs];
1024
0
    }
1025
1026
0
    int iOuter=(n>>1);
1027
0
    int nInner=(n>>2);
1028
1029
0
    while (nInner>=2) {
1030
0
      std::swap(a,b);
1031
1032
0
      for (int k=0;k<n;k+=iOuter) {
1033
0
        for (int i=0;i<nInner;i++) {
1034
0
          a[k+i       ] = b[k+i] + b[k+i+nInner];
1035
0
          a[k+i+nInner] = b[k+i] - b[k+i+nInner];
1036
0
        }
1037
0
      }
1038
1039
0
      iOuter>>=1;
1040
0
      nInner>>=1;
1041
0
    }
1042
1043
0
    for (int k=0;k<n;k+=2) {
1044
0
      tmp[k  +n*row] = a[k] + a[k+1];
1045
0
      tmp[k+1+n*row] = a[k] - a[k+1];
1046
0
    }
1047
0
  }
1048
1049
  //printMatrix("tmp",tmp,n);
1050
1051
  // column transforms
1052
1053
0
  for (int col=0;col<n;col++) {
1054
0
    for (int i=0;i<(n>>1);i++) {
1055
0
      a[       i] = tmp[i*n+col] + tmp[(i+(n>>1))*n+col];
1056
0
      a[(n>>1)+i] = tmp[i*n+col] - tmp[(i+(n>>1))*n+col];
1057
0
    }
1058
1059
0
    int iOuter=(n>>1);
1060
0
    int nInner=(n>>2);
1061
1062
0
    while (nInner>=2) {
1063
0
      std::swap(a,b);
1064
1065
0
      for (int k=0;k<n;k+=iOuter) {
1066
0
        for (int i=0;i<nInner;i++) {
1067
0
          a[k+i       ] = b[k+i] + b[k+i+nInner];
1068
0
          a[k+i+nInner] = b[k+i] - b[k+i+nInner];
1069
0
        }
1070
0
      }
1071
1072
0
      iOuter>>=1;
1073
0
      nInner>>=1;
1074
0
    }
1075
1076
0
    for (int k=0;k<n;k+=2) {
1077
0
      coeffs[col+(k  )*n] = a[k] + a[k+1];
1078
0
      coeffs[col+(k+1)*n] = a[k] - a[k+1];
1079
0
    }
1080
0
  }
1081
1082
  //printMatrix("coeffs",coeffs,n);
1083
0
}
1084
1085
1086
void hadamard_4x4_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride)
1087
0
{
1088
0
  int16_t tmp[4*4];
1089
1090
  // row transforms
1091
1092
  //printMatrix("input",input,4);
1093
1094
0
  int16_t a[4];
1095
0
  for (int row=0;row<4;row++) {
1096
0
    int rs = row*stride;
1097
0
    a[0] = input[0+rs] + input[2+rs];
1098
0
    a[1] = input[1+rs] + input[3+rs];
1099
0
    a[2] = input[0+rs] - input[2+rs];
1100
0
    a[3] = input[1+rs] - input[3+rs];
1101
1102
0
    tmp[0+4*row] = a[0]+a[1];
1103
0
    tmp[1+4*row] = a[0]-a[1];
1104
0
    tmp[2+4*row] = a[2]+a[3];
1105
0
    tmp[3+4*row] = a[2]-a[3];
1106
0
  }
1107
1108
  //printMatrix("tmp",tmp,4);
1109
1110
  // column transforms
1111
1112
0
  for (int col=0;col<4;col++) {
1113
0
    a[0] = tmp[col+0*4] + tmp[col+2*4];
1114
0
    a[1] = tmp[col+1*4] + tmp[col+3*4];
1115
0
    a[2] = tmp[col+0*4] - tmp[col+2*4];
1116
0
    a[3] = tmp[col+1*4] - tmp[col+3*4];
1117
1118
0
    coeffs[col+0*4] = a[0]+a[1];
1119
0
    coeffs[col+1*4] = a[0]-a[1];
1120
0
    coeffs[col+2*4] = a[2]+a[3];
1121
0
    coeffs[col+3*4] = a[2]-a[3];
1122
0
  }
1123
1124
  //printMatrix("coeffs",coeffs,4);
1125
0
}
1126
1127
1128
void hadamard_8x8_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride)
1129
0
{
1130
0
  int16_t tmp[8*8];
1131
1132
  // row transforms
1133
1134
  //printMatrix("input",input,8);
1135
1136
0
  int16_t a[8],b[8];
1137
0
  for (int row=0;row<8;row++) {
1138
0
    int rs = row*stride;
1139
0
    a[0] = input[0+rs] + input[4+rs];
1140
0
    a[1] = input[1+rs] + input[5+rs];
1141
0
    a[2] = input[2+rs] + input[6+rs];
1142
0
    a[3] = input[3+rs] + input[7+rs];
1143
0
    a[4] = input[0+rs] - input[4+rs];
1144
0
    a[5] = input[1+rs] - input[5+rs];
1145
0
    a[6] = input[2+rs] - input[6+rs];
1146
0
    a[7] = input[3+rs] - input[7+rs];
1147
1148
0
    b[0] = a[0]+a[2];
1149
0
    b[1] = a[1]+a[3];
1150
0
    b[2] = a[0]-a[2];
1151
0
    b[3] = a[1]-a[3];
1152
0
    b[4] = a[4]+a[6];
1153
0
    b[5] = a[5]+a[7];
1154
0
    b[6] = a[4]-a[6];
1155
0
    b[7] = a[5]-a[7];
1156
1157
0
    tmp[0+8*row] = b[0]+b[1];
1158
0
    tmp[1+8*row] = b[0]-b[1];
1159
0
    tmp[2+8*row] = b[2]+b[3];
1160
0
    tmp[3+8*row] = b[2]-b[3];
1161
0
    tmp[4+8*row] = b[4]+b[5];
1162
0
    tmp[5+8*row] = b[4]-b[5];
1163
0
    tmp[6+8*row] = b[6]+b[7];
1164
0
    tmp[7+8*row] = b[6]-b[7];
1165
0
  }
1166
1167
  //printMatrix("tmp",tmp,8);
1168
1169
  // column transforms
1170
1171
0
  for (int col=0;col<8;col++) {
1172
0
    a[0] = tmp[col+0*8] + tmp[col+4*8];
1173
0
    a[1] = tmp[col+1*8] + tmp[col+5*8];
1174
0
    a[2] = tmp[col+2*8] + tmp[col+6*8];
1175
0
    a[3] = tmp[col+3*8] + tmp[col+7*8];
1176
0
    a[4] = tmp[col+0*8] - tmp[col+4*8];
1177
0
    a[5] = tmp[col+1*8] - tmp[col+5*8];
1178
0
    a[6] = tmp[col+2*8] - tmp[col+6*8];
1179
0
    a[7] = tmp[col+3*8] - tmp[col+7*8];
1180
1181
0
    b[0] = a[0]+a[2];
1182
0
    b[1] = a[1]+a[3];
1183
0
    b[2] = a[0]-a[2];
1184
0
    b[3] = a[1]-a[3];
1185
0
    b[4] = a[4]+a[6];
1186
0
    b[5] = a[5]+a[7];
1187
0
    b[6] = a[4]-a[6];
1188
0
    b[7] = a[5]-a[7];
1189
1190
0
    coeffs[col+0*8] = b[0]+b[1];
1191
0
    coeffs[col+1*8] = b[0]-b[1];
1192
0
    coeffs[col+2*8] = b[2]+b[3];
1193
0
    coeffs[col+3*8] = b[2]-b[3];
1194
0
    coeffs[col+4*8] = b[4]+b[5];
1195
0
    coeffs[col+5*8] = b[4]-b[5];
1196
0
    coeffs[col+6*8] = b[6]+b[7];
1197
0
    coeffs[col+7*8] = b[6]-b[7];
1198
0
  }
1199
1200
  //printMatrix("coeffs",coeffs,8);
1201
0
}
1202
1203
1204
void hadamard_16x16_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride)
1205
0
{
1206
0
  hadamard_transform_8(coeffs,16, input,stride);
1207
0
}
1208
1209
void hadamard_32x32_8_fallback(int16_t *coeffs, const int16_t *input, ptrdiff_t stride)
1210
0
{
1211
0
  hadamard_transform_8(coeffs,32, input,stride);
1212
0
}
1213
1214
1215
void dequant_coeff_block_fallback(int16_t* coeffBuf, const int16_t* coeffList,
1216
                                  const int16_t* coeffPos, int nCoeff,
1217
                                  int32_t fact, int32_t offset, int32_t bdShift)
1218
0
{
1219
0
  for (int i=0;i<nCoeff;i++) {
1220
0
    int32_t v = Clip3(-32768, 32767, (coeffList[i]*fact + offset) >> bdShift);
1221
0
    coeffBuf[ coeffPos[i] ] = (int16_t)v;
1222
0
  }
1223
0
}