vp9_dct.c 22.6 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9 10
 */

11
#include <assert.h>
John Koleszar's avatar
John Koleszar committed
12
#include <math.h>
13 14

#include "./vp9_rtcd.h"
Jingning Han's avatar
Jingning Han committed
15
#include "./vpx_config.h"
16
#include "./vpx_dsp_rtcd.h"
17

18
#include "vp9/common/vp9_blockd.h"
19
#include "vp9/common/vp9_idct.h"
20
#include "vpx_dsp/fwd_txfm.h"
Jingning Han's avatar
Jingning Han committed
21
#include "vpx_ports/mem.h"
22

23
static void fdct4(const tran_low_t *input, tran_low_t *output) {
24 25
  tran_high_t step[4];
  tran_high_t temp1, temp2;
Yaowu Xu's avatar
Yaowu Xu committed
26 27 28 29 30 31 32 33

  step[0] = input[0] + input[3];
  step[1] = input[1] + input[2];
  step[2] = input[1] - input[2];
  step[3] = input[0] - input[3];

  temp1 = (step[0] + step[1]) * cospi_16_64;
  temp2 = (step[0] - step[1]) * cospi_16_64;
34 35
  output[0] = (tran_low_t)fdct_round_shift(temp1);
  output[2] = (tran_low_t)fdct_round_shift(temp2);
Yaowu Xu's avatar
Yaowu Xu committed
36 37
  temp1 = step[2] * cospi_24_64 + step[3] * cospi_8_64;
  temp2 = -step[2] * cospi_8_64 + step[3] * cospi_24_64;
38 39
  output[1] = (tran_low_t)fdct_round_shift(temp1);
  output[3] = (tran_low_t)fdct_round_shift(temp2);
Yaowu Xu's avatar
Yaowu Xu committed
40
}
Jingning Han's avatar
Jingning Han committed
41

42
static void fdct8(const tran_low_t *input, tran_low_t *output) {
43 44 45
  tran_high_t s0, s1, s2, s3, s4, s5, s6, s7;  // canbe16
  tran_high_t t0, t1, t2, t3;                  // needs32
  tran_high_t x0, x1, x2, x3;                  // canbe16
Yunqing Wang's avatar
Yunqing Wang committed
46 47

  // stage 1
Christian Duvivier's avatar
Christian Duvivier committed
48 49 50 51 52 53 54 55 56
  s0 = input[0] + input[7];
  s1 = input[1] + input[6];
  s2 = input[2] + input[5];
  s3 = input[3] + input[4];
  s4 = input[3] - input[4];
  s5 = input[2] - input[5];
  s6 = input[1] - input[6];
  s7 = input[0] - input[7];

57
  // fdct4(step, step);
Christian Duvivier's avatar
Christian Duvivier committed
58 59 60 61 62 63
  x0 = s0 + s3;
  x1 = s1 + s2;
  x2 = s1 - s2;
  x3 = s0 - s3;
  t0 = (x0 + x1) * cospi_16_64;
  t1 = (x0 - x1) * cospi_16_64;
clang-format's avatar
clang-format committed
64 65
  t2 = x2 * cospi_24_64 + x3 * cospi_8_64;
  t3 = -x2 * cospi_8_64 + x3 * cospi_24_64;
66 67 68 69
  output[0] = (tran_low_t)fdct_round_shift(t0);
  output[2] = (tran_low_t)fdct_round_shift(t2);
  output[4] = (tran_low_t)fdct_round_shift(t1);
  output[6] = (tran_low_t)fdct_round_shift(t3);
70

Yunqing Wang's avatar
Yunqing Wang committed
71
  // Stage 2
Christian Duvivier's avatar
Christian Duvivier committed
72 73
  t0 = (s6 - s5) * cospi_16_64;
  t1 = (s6 + s5) * cospi_16_64;
74 75
  t2 = (tran_low_t)fdct_round_shift(t0);
  t3 = (tran_low_t)fdct_round_shift(t1);
76

Yunqing Wang's avatar
Yunqing Wang committed
77
  // Stage 3
Christian Duvivier's avatar
Christian Duvivier committed
78 79 80 81
  x0 = s4 + t2;
  x1 = s4 - t2;
  x2 = s7 - t3;
  x3 = s7 + t3;
82

Yunqing Wang's avatar
Yunqing Wang committed
83
  // Stage 4
clang-format's avatar
clang-format committed
84 85
  t0 = x0 * cospi_28_64 + x3 * cospi_4_64;
  t1 = x1 * cospi_12_64 + x2 * cospi_20_64;
Christian Duvivier's avatar
Christian Duvivier committed
86
  t2 = x2 * cospi_12_64 + x1 * -cospi_20_64;
clang-format's avatar
clang-format committed
87
  t3 = x3 * cospi_28_64 + x0 * -cospi_4_64;
88 89 90 91
  output[1] = (tran_low_t)fdct_round_shift(t0);
  output[3] = (tran_low_t)fdct_round_shift(t2);
  output[5] = (tran_low_t)fdct_round_shift(t1);
  output[7] = (tran_low_t)fdct_round_shift(t3);
Yunqing Wang's avatar
Yunqing Wang committed
92
}
93

94 95 96 97 98 99
static void fdct16(const tran_low_t in[16], tran_low_t out[16]) {
  tran_high_t step1[8];      // canbe16
  tran_high_t step2[8];      // canbe16
  tran_high_t step3[8];      // canbe16
  tran_high_t input[8];      // canbe16
  tran_high_t temp1, temp2;  // needs32
100

101 102 103 104 105 106 107
  // step 1
  input[0] = in[0] + in[15];
  input[1] = in[1] + in[14];
  input[2] = in[2] + in[13];
  input[3] = in[3] + in[12];
  input[4] = in[4] + in[11];
  input[5] = in[5] + in[10];
clang-format's avatar
clang-format committed
108 109
  input[6] = in[6] + in[9];
  input[7] = in[7] + in[8];
110

clang-format's avatar
clang-format committed
111 112
  step1[0] = in[7] - in[8];
  step1[1] = in[6] - in[9];
113 114 115 116 117 118
  step1[2] = in[5] - in[10];
  step1[3] = in[4] - in[11];
  step1[4] = in[3] - in[12];
  step1[5] = in[2] - in[13];
  step1[6] = in[1] - in[14];
  step1[7] = in[0] - in[15];
119

120
  // fdct8(step, step);
Christian Duvivier's avatar
Christian Duvivier committed
121
  {
122 123 124
    tran_high_t s0, s1, s2, s3, s4, s5, s6, s7;  // canbe16
    tran_high_t t0, t1, t2, t3;                  // needs32
    tran_high_t x0, x1, x2, x3;                  // canbe16
Christian Duvivier's avatar
Christian Duvivier committed
125

126 127 128 129 130 131 132 133 134
    // stage 1
    s0 = input[0] + input[7];
    s1 = input[1] + input[6];
    s2 = input[2] + input[5];
    s3 = input[3] + input[4];
    s4 = input[3] - input[4];
    s5 = input[2] - input[5];
    s6 = input[1] - input[6];
    s7 = input[0] - input[7];
Christian Duvivier's avatar
Christian Duvivier committed
135

136 137 138 139 140 141 142
    // fdct4(step, step);
    x0 = s0 + s3;
    x1 = s1 + s2;
    x2 = s1 - s2;
    x3 = s0 - s3;
    t0 = (x0 + x1) * cospi_16_64;
    t1 = (x0 - x1) * cospi_16_64;
clang-format's avatar
clang-format committed
143
    t2 = x3 * cospi_8_64 + x2 * cospi_24_64;
144 145 146 147 148
    t3 = x3 * cospi_24_64 - x2 * cospi_8_64;
    out[0] = (tran_low_t)fdct_round_shift(t0);
    out[4] = (tran_low_t)fdct_round_shift(t2);
    out[8] = (tran_low_t)fdct_round_shift(t1);
    out[12] = (tran_low_t)fdct_round_shift(t3);
Christian Duvivier's avatar
Christian Duvivier committed
149

150 151 152 153 154
    // Stage 2
    t0 = (s6 - s5) * cospi_16_64;
    t1 = (s6 + s5) * cospi_16_64;
    t2 = fdct_round_shift(t0);
    t3 = fdct_round_shift(t1);
Christian Duvivier's avatar
Christian Duvivier committed
155

156 157 158 159 160
    // Stage 3
    x0 = s4 + t2;
    x1 = s4 - t2;
    x2 = s7 - t3;
    x3 = s7 + t3;
161

162
    // Stage 4
clang-format's avatar
clang-format committed
163 164
    t0 = x0 * cospi_28_64 + x3 * cospi_4_64;
    t1 = x1 * cospi_12_64 + x2 * cospi_20_64;
165
    t2 = x2 * cospi_12_64 + x1 * -cospi_20_64;
clang-format's avatar
clang-format committed
166
    t3 = x3 * cospi_28_64 + x0 * -cospi_4_64;
167 168 169 170
    out[2] = (tran_low_t)fdct_round_shift(t0);
    out[6] = (tran_low_t)fdct_round_shift(t2);
    out[10] = (tran_low_t)fdct_round_shift(t1);
    out[14] = (tran_low_t)fdct_round_shift(t3);
171 172
  }

173 174 175 176 177 178 179 180 181
  // step 2
  temp1 = (step1[5] - step1[2]) * cospi_16_64;
  temp2 = (step1[4] - step1[3]) * cospi_16_64;
  step2[2] = fdct_round_shift(temp1);
  step2[3] = fdct_round_shift(temp2);
  temp1 = (step1[4] + step1[3]) * cospi_16_64;
  temp2 = (step1[5] + step1[2]) * cospi_16_64;
  step2[4] = fdct_round_shift(temp1);
  step2[5] = fdct_round_shift(temp2);
182

183 184 185 186 187 188 189 190 191
  // step 3
  step3[0] = step1[0] + step2[3];
  step3[1] = step1[1] + step2[2];
  step3[2] = step1[1] - step2[2];
  step3[3] = step1[0] - step2[3];
  step3[4] = step1[7] - step2[4];
  step3[5] = step1[6] - step2[5];
  step3[6] = step1[6] + step2[5];
  step3[7] = step1[7] + step2[4];
192

193
  // step 4
clang-format's avatar
clang-format committed
194 195
  temp1 = step3[1] * -cospi_8_64 + step3[6] * cospi_24_64;
  temp2 = step3[2] * cospi_24_64 + step3[5] * cospi_8_64;
196 197 198
  step2[1] = fdct_round_shift(temp1);
  step2[2] = fdct_round_shift(temp2);
  temp1 = step3[2] * cospi_8_64 - step3[5] * cospi_24_64;
clang-format's avatar
clang-format committed
199
  temp2 = step3[1] * cospi_24_64 + step3[6] * cospi_8_64;
200 201
  step2[5] = fdct_round_shift(temp1);
  step2[6] = fdct_round_shift(temp2);
202

203 204 205 206 207 208 209 210 211
  // step 5
  step1[0] = step3[0] + step2[1];
  step1[1] = step3[0] - step2[1];
  step1[2] = step3[3] + step2[2];
  step1[3] = step3[3] - step2[2];
  step1[4] = step3[4] - step2[5];
  step1[5] = step3[4] + step2[5];
  step1[6] = step3[7] - step2[6];
  step1[7] = step3[7] + step2[6];
212

213
  // step 6
clang-format's avatar
clang-format committed
214
  temp1 = step1[0] * cospi_30_64 + step1[7] * cospi_2_64;
215 216 217
  temp2 = step1[1] * cospi_14_64 + step1[6] * cospi_18_64;
  out[1] = (tran_low_t)fdct_round_shift(temp1);
  out[9] = (tran_low_t)fdct_round_shift(temp2);
218

219
  temp1 = step1[2] * cospi_22_64 + step1[5] * cospi_10_64;
clang-format's avatar
clang-format committed
220
  temp2 = step1[3] * cospi_6_64 + step1[4] * cospi_26_64;
221 222
  out[5] = (tran_low_t)fdct_round_shift(temp1);
  out[13] = (tran_low_t)fdct_round_shift(temp2);
223

clang-format's avatar
clang-format committed
224
  temp1 = step1[3] * -cospi_26_64 + step1[4] * cospi_6_64;
225 226 227
  temp2 = step1[2] * -cospi_10_64 + step1[5] * cospi_22_64;
  out[3] = (tran_low_t)fdct_round_shift(temp1);
  out[11] = (tran_low_t)fdct_round_shift(temp2);
228

229
  temp1 = step1[1] * -cospi_18_64 + step1[6] * cospi_14_64;
clang-format's avatar
clang-format committed
230
  temp2 = step1[0] * -cospi_2_64 + step1[7] * cospi_30_64;
231 232 233
  out[7] = (tran_low_t)fdct_round_shift(temp1);
  out[15] = (tran_low_t)fdct_round_shift(temp2);
}
234

235 236 237
static void fadst4(const tran_low_t *input, tran_low_t *output) {
  tran_high_t x0, x1, x2, x3;
  tran_high_t s0, s1, s2, s3, s4, s5, s6, s7;
238

239 240 241 242
  x0 = input[0];
  x1 = input[1];
  x2 = input[2];
  x3 = input[3];
243

244 245 246 247
  if (!(x0 | x1 | x2 | x3)) {
    output[0] = output[1] = output[2] = output[3] = 0;
    return;
  }
248

249 250 251 252 253 254 255
  s0 = sinpi_1_9 * x0;
  s1 = sinpi_4_9 * x0;
  s2 = sinpi_2_9 * x1;
  s3 = sinpi_1_9 * x1;
  s4 = sinpi_3_9 * x2;
  s5 = sinpi_4_9 * x3;
  s6 = sinpi_2_9 * x3;
256
  s7 = x0 + x1 - x3;
257

258 259 260 261
  x0 = s0 + s2 + s5;
  x1 = sinpi_3_9 * s7;
  x2 = s1 - s3 + s6;
  x3 = s4;
262

263 264 265 266
  s0 = x0 + x3;
  s1 = x1;
  s2 = x2 - x3;
  s3 = x2 - x0 + x3;
267

268 269 270 271 272
  // 1-D transform scaling factor is sqrt(2).
  output[0] = (tran_low_t)fdct_round_shift(s0);
  output[1] = (tran_low_t)fdct_round_shift(s1);
  output[2] = (tran_low_t)fdct_round_shift(s2);
  output[3] = (tran_low_t)fdct_round_shift(s3);
273 274
}

275 276
static void fadst8(const tran_low_t *input, tran_low_t *output) {
  tran_high_t s0, s1, s2, s3, s4, s5, s6, s7;
277

278 279 280 281 282 283 284 285
  tran_high_t x0 = input[7];
  tran_high_t x1 = input[0];
  tran_high_t x2 = input[5];
  tran_high_t x3 = input[2];
  tran_high_t x4 = input[3];
  tran_high_t x5 = input[4];
  tran_high_t x6 = input[1];
  tran_high_t x7 = input[6];
286 287

  // stage 1
clang-format's avatar
clang-format committed
288 289
  s0 = cospi_2_64 * x0 + cospi_30_64 * x1;
  s1 = cospi_30_64 * x0 - cospi_2_64 * x1;
290 291 292 293
  s2 = cospi_10_64 * x2 + cospi_22_64 * x3;
  s3 = cospi_22_64 * x2 - cospi_10_64 * x3;
  s4 = cospi_18_64 * x4 + cospi_14_64 * x5;
  s5 = cospi_14_64 * x4 - cospi_18_64 * x5;
clang-format's avatar
clang-format committed
294 295
  s6 = cospi_26_64 * x6 + cospi_6_64 * x7;
  s7 = cospi_6_64 * x6 - cospi_26_64 * x7;
296

297 298 299 300 301 302 303 304
  x0 = fdct_round_shift(s0 + s4);
  x1 = fdct_round_shift(s1 + s5);
  x2 = fdct_round_shift(s2 + s6);
  x3 = fdct_round_shift(s3 + s7);
  x4 = fdct_round_shift(s0 - s4);
  x5 = fdct_round_shift(s1 - s5);
  x6 = fdct_round_shift(s2 - s6);
  x7 = fdct_round_shift(s3 - s7);
305 306 307 308 309 310

  // stage 2
  s0 = x0;
  s1 = x1;
  s2 = x2;
  s3 = x3;
clang-format's avatar
clang-format committed
311 312 313 314
  s4 = cospi_8_64 * x4 + cospi_24_64 * x5;
  s5 = cospi_24_64 * x4 - cospi_8_64 * x5;
  s6 = -cospi_24_64 * x6 + cospi_8_64 * x7;
  s7 = cospi_8_64 * x6 + cospi_24_64 * x7;
315 316 317 318 319

  x0 = s0 + s2;
  x1 = s1 + s3;
  x2 = s0 - s2;
  x3 = s1 - s3;
320 321 322 323
  x4 = fdct_round_shift(s4 + s6);
  x5 = fdct_round_shift(s5 + s7);
  x6 = fdct_round_shift(s4 - s6);
  x7 = fdct_round_shift(s5 - s7);
324 325 326 327 328 329 330

  // stage 3
  s2 = cospi_16_64 * (x2 + x3);
  s3 = cospi_16_64 * (x2 - x3);
  s6 = cospi_16_64 * (x6 + x7);
  s7 = cospi_16_64 * (x6 - x7);

331 332 333 334
  x2 = fdct_round_shift(s2);
  x3 = fdct_round_shift(s3);
  x6 = fdct_round_shift(s6);
  x7 = fdct_round_shift(s7);
335

336 337 338 339 340 341 342 343
  output[0] = (tran_low_t)x0;
  output[1] = (tran_low_t)-x4;
  output[2] = (tran_low_t)x6;
  output[3] = (tran_low_t)-x2;
  output[4] = (tran_low_t)x3;
  output[5] = (tran_low_t)-x7;
  output[6] = (tran_low_t)x5;
  output[7] = (tran_low_t)-x1;
John Koleszar's avatar
John Koleszar committed
344 345
}

346 347 348
static void fadst16(const tran_low_t *input, tran_low_t *output) {
  tran_high_t s0, s1, s2, s3, s4, s5, s6, s7, s8;
  tran_high_t s9, s10, s11, s12, s13, s14, s15;
349

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
  tran_high_t x0 = input[15];
  tran_high_t x1 = input[0];
  tran_high_t x2 = input[13];
  tran_high_t x3 = input[2];
  tran_high_t x4 = input[11];
  tran_high_t x5 = input[4];
  tran_high_t x6 = input[9];
  tran_high_t x7 = input[6];
  tran_high_t x8 = input[7];
  tran_high_t x9 = input[8];
  tran_high_t x10 = input[5];
  tran_high_t x11 = input[10];
  tran_high_t x12 = input[3];
  tran_high_t x13 = input[12];
  tran_high_t x14 = input[1];
  tran_high_t x15 = input[14];
John Koleszar's avatar
John Koleszar committed
366

367
  // stage 1
clang-format's avatar
clang-format committed
368
  s0 = x0 * cospi_1_64 + x1 * cospi_31_64;
369
  s1 = x0 * cospi_31_64 - x1 * cospi_1_64;
clang-format's avatar
clang-format committed
370
  s2 = x2 * cospi_5_64 + x3 * cospi_27_64;
371
  s3 = x2 * cospi_27_64 - x3 * cospi_5_64;
clang-format's avatar
clang-format committed
372
  s4 = x4 * cospi_9_64 + x5 * cospi_23_64;
373 374 375 376 377 378 379 380
  s5 = x4 * cospi_23_64 - x5 * cospi_9_64;
  s6 = x6 * cospi_13_64 + x7 * cospi_19_64;
  s7 = x6 * cospi_19_64 - x7 * cospi_13_64;
  s8 = x8 * cospi_17_64 + x9 * cospi_15_64;
  s9 = x8 * cospi_15_64 - x9 * cospi_17_64;
  s10 = x10 * cospi_21_64 + x11 * cospi_11_64;
  s11 = x10 * cospi_11_64 - x11 * cospi_21_64;
  s12 = x12 * cospi_25_64 + x13 * cospi_7_64;
clang-format's avatar
clang-format committed
381
  s13 = x12 * cospi_7_64 - x13 * cospi_25_64;
382
  s14 = x14 * cospi_29_64 + x15 * cospi_3_64;
clang-format's avatar
clang-format committed
383
  s15 = x14 * cospi_3_64 - x15 * cospi_29_64;
384

385 386 387 388 389 390 391 392
  x0 = fdct_round_shift(s0 + s8);
  x1 = fdct_round_shift(s1 + s9);
  x2 = fdct_round_shift(s2 + s10);
  x3 = fdct_round_shift(s3 + s11);
  x4 = fdct_round_shift(s4 + s12);
  x5 = fdct_round_shift(s5 + s13);
  x6 = fdct_round_shift(s6 + s14);
  x7 = fdct_round_shift(s7 + s15);
clang-format's avatar
clang-format committed
393 394
  x8 = fdct_round_shift(s0 - s8);
  x9 = fdct_round_shift(s1 - s9);
395 396 397 398 399 400
  x10 = fdct_round_shift(s2 - s10);
  x11 = fdct_round_shift(s3 - s11);
  x12 = fdct_round_shift(s4 - s12);
  x13 = fdct_round_shift(s5 - s13);
  x14 = fdct_round_shift(s6 - s14);
  x15 = fdct_round_shift(s7 - s15);
401 402 403 404 405 406 407 408 409 410

  // stage 2
  s0 = x0;
  s1 = x1;
  s2 = x2;
  s3 = x3;
  s4 = x4;
  s5 = x5;
  s6 = x6;
  s7 = x7;
clang-format's avatar
clang-format committed
411 412 413 414 415 416 417 418
  s8 = x8 * cospi_4_64 + x9 * cospi_28_64;
  s9 = x8 * cospi_28_64 - x9 * cospi_4_64;
  s10 = x10 * cospi_20_64 + x11 * cospi_12_64;
  s11 = x10 * cospi_12_64 - x11 * cospi_20_64;
  s12 = -x12 * cospi_28_64 + x13 * cospi_4_64;
  s13 = x12 * cospi_4_64 + x13 * cospi_28_64;
  s14 = -x14 * cospi_12_64 + x15 * cospi_20_64;
  s15 = x14 * cospi_20_64 + x15 * cospi_12_64;
419 420 421 422 423 424 425 426 427

  x0 = s0 + s4;
  x1 = s1 + s5;
  x2 = s2 + s6;
  x3 = s3 + s7;
  x4 = s0 - s4;
  x5 = s1 - s5;
  x6 = s2 - s6;
  x7 = s3 - s7;
428 429 430 431 432 433 434 435
  x8 = fdct_round_shift(s8 + s12);
  x9 = fdct_round_shift(s9 + s13);
  x10 = fdct_round_shift(s10 + s14);
  x11 = fdct_round_shift(s11 + s15);
  x12 = fdct_round_shift(s8 - s12);
  x13 = fdct_round_shift(s9 - s13);
  x14 = fdct_round_shift(s10 - s14);
  x15 = fdct_round_shift(s11 - s15);
436 437 438 439 440 441

  // stage 3
  s0 = x0;
  s1 = x1;
  s2 = x2;
  s3 = x3;
clang-format's avatar
clang-format committed
442
  s4 = x4 * cospi_8_64 + x5 * cospi_24_64;
443
  s5 = x4 * cospi_24_64 - x5 * cospi_8_64;
clang-format's avatar
clang-format committed
444 445
  s6 = -x6 * cospi_24_64 + x7 * cospi_8_64;
  s7 = x6 * cospi_8_64 + x7 * cospi_24_64;
446 447 448 449
  s8 = x8;
  s9 = x9;
  s10 = x10;
  s11 = x11;
clang-format's avatar
clang-format committed
450
  s12 = x12 * cospi_8_64 + x13 * cospi_24_64;
451
  s13 = x12 * cospi_24_64 - x13 * cospi_8_64;
clang-format's avatar
clang-format committed
452 453
  s14 = -x14 * cospi_24_64 + x15 * cospi_8_64;
  s15 = x14 * cospi_8_64 + x15 * cospi_24_64;
454 455 456 457 458

  x0 = s0 + s2;
  x1 = s1 + s3;
  x2 = s0 - s2;
  x3 = s1 - s3;
459 460 461 462
  x4 = fdct_round_shift(s4 + s6);
  x5 = fdct_round_shift(s5 + s7);
  x6 = fdct_round_shift(s4 - s6);
  x7 = fdct_round_shift(s5 - s7);
463 464 465 466
  x8 = s8 + s10;
  x9 = s9 + s11;
  x10 = s8 - s10;
  x11 = s9 - s11;
467 468 469 470
  x12 = fdct_round_shift(s12 + s14);
  x13 = fdct_round_shift(s13 + s15);
  x14 = fdct_round_shift(s12 - s14);
  x15 = fdct_round_shift(s13 - s15);
471 472

  // stage 4
clang-format's avatar
clang-format committed
473
  s2 = (-cospi_16_64) * (x2 + x3);
474 475
  s3 = cospi_16_64 * (x2 - x3);
  s6 = cospi_16_64 * (x6 + x7);
clang-format's avatar
clang-format committed
476
  s7 = cospi_16_64 * (-x6 + x7);
477
  s10 = cospi_16_64 * (x10 + x11);
clang-format's avatar
clang-format committed
478 479
  s11 = cospi_16_64 * (-x10 + x11);
  s14 = (-cospi_16_64) * (x14 + x15);
480 481
  s15 = cospi_16_64 * (x14 - x15);

482 483 484 485 486 487 488 489
  x2 = fdct_round_shift(s2);
  x3 = fdct_round_shift(s3);
  x6 = fdct_round_shift(s6);
  x7 = fdct_round_shift(s7);
  x10 = fdct_round_shift(s10);
  x11 = fdct_round_shift(s11);
  x14 = fdct_round_shift(s14);
  x15 = fdct_round_shift(s15);
490

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  output[0] = (tran_low_t)x0;
  output[1] = (tran_low_t)-x8;
  output[2] = (tran_low_t)x12;
  output[3] = (tran_low_t)-x4;
  output[4] = (tran_low_t)x6;
  output[5] = (tran_low_t)x14;
  output[6] = (tran_low_t)x10;
  output[7] = (tran_low_t)x2;
  output[8] = (tran_low_t)x3;
  output[9] = (tran_low_t)x11;
  output[10] = (tran_low_t)x15;
  output[11] = (tran_low_t)x7;
  output[12] = (tran_low_t)x5;
  output[13] = (tran_low_t)-x13;
  output[14] = (tran_low_t)x9;
  output[15] = (tran_low_t)-x1;
507 508
}

509
static const transform_2d FHT_4[] = {
clang-format's avatar
clang-format committed
510 511 512 513
  { fdct4, fdct4 },   // DCT_DCT  = 0
  { fadst4, fdct4 },  // ADST_DCT = 1
  { fdct4, fadst4 },  // DCT_ADST = 2
  { fadst4, fadst4 }  // ADST_ADST = 3
514 515 516
};

static const transform_2d FHT_8[] = {
clang-format's avatar
clang-format committed
517 518 519 520
  { fdct8, fdct8 },   // DCT_DCT  = 0
  { fadst8, fdct8 },  // ADST_DCT = 1
  { fdct8, fadst8 },  // DCT_ADST = 2
  { fadst8, fadst8 }  // ADST_ADST = 3
521 522 523
};

static const transform_2d FHT_16[] = {
clang-format's avatar
clang-format committed
524 525 526 527
  { fdct16, fdct16 },   // DCT_DCT  = 0
  { fadst16, fdct16 },  // ADST_DCT = 1
  { fdct16, fadst16 },  // DCT_ADST = 2
  { fadst16, fadst16 }  // ADST_ADST = 3
528 529
};

clang-format's avatar
clang-format committed
530 531
void vp9_fht4x4_c(const int16_t *input, tran_low_t *output, int stride,
                  int tx_type) {
532
  if (tx_type == DCT_DCT) {
533
    vpx_fdct4x4_c(input, output, stride);
534 535 536 537 538 539 540 541
  } else {
    tran_low_t out[4 * 4];
    int i, j;
    tran_low_t temp_in[4], temp_out[4];
    const transform_2d ht = FHT_4[tx_type];

    // Columns
    for (i = 0; i < 4; ++i) {
clang-format's avatar
clang-format committed
542 543
      for (j = 0; j < 4; ++j) temp_in[j] = input[j * stride + i] * 16;
      if (i == 0 && temp_in[0]) temp_in[0] += 1;
544
      ht.cols(temp_in, temp_out);
clang-format's avatar
clang-format committed
545
      for (j = 0; j < 4; ++j) out[j * 4 + i] = temp_out[j];
546 547 548 549
    }

    // Rows
    for (i = 0; i < 4; ++i) {
clang-format's avatar
clang-format committed
550
      for (j = 0; j < 4; ++j) temp_in[j] = out[j + i * 4];
551
      ht.rows(temp_in, temp_out);
clang-format's avatar
clang-format committed
552
      for (j = 0; j < 4; ++j) output[j + i * 4] = (temp_out[j] + 1) >> 2;
553 554 555 556 557 558
    }
  }
}

void vp9_fdct8x8_quant_c(const int16_t *input, int stride,
                         tran_low_t *coeff_ptr, intptr_t n_coeffs,
Johann's avatar
Johann committed
559 560
                         int skip_block, const int16_t *round_ptr,
                         const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
clang-format's avatar
clang-format committed
561 562 563
                         tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
                         uint16_t *eob_ptr, const int16_t *scan,
                         const int16_t *iscan) {
564 565 566 567 568
  int eob = -1;

  int i, j;
  tran_low_t intermediate[64];

Johann's avatar
Johann committed
569 570
  (void)iscan;

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
  // Transform columns
  {
    tran_low_t *output = intermediate;
    tran_high_t s0, s1, s2, s3, s4, s5, s6, s7;  // canbe16
    tran_high_t t0, t1, t2, t3;                  // needs32
    tran_high_t x0, x1, x2, x3;                  // canbe16

    int i;
    for (i = 0; i < 8; i++) {
      // stage 1
      s0 = (input[0 * stride] + input[7 * stride]) * 4;
      s1 = (input[1 * stride] + input[6 * stride]) * 4;
      s2 = (input[2 * stride] + input[5 * stride]) * 4;
      s3 = (input[3 * stride] + input[4 * stride]) * 4;
      s4 = (input[3 * stride] - input[4 * stride]) * 4;
      s5 = (input[2 * stride] - input[5 * stride]) * 4;
      s6 = (input[1 * stride] - input[6 * stride]) * 4;
      s7 = (input[0 * stride] - input[7 * stride]) * 4;

      // fdct4(step, step);
      x0 = s0 + s3;
      x1 = s1 + s2;
      x2 = s1 - s2;
      x3 = s0 - s3;
      t0 = (x0 + x1) * cospi_16_64;
      t1 = (x0 - x1) * cospi_16_64;
clang-format's avatar
clang-format committed
597 598
      t2 = x2 * cospi_24_64 + x3 * cospi_8_64;
      t3 = -x2 * cospi_8_64 + x3 * cospi_24_64;
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
      output[0 * 8] = (tran_low_t)fdct_round_shift(t0);
      output[2 * 8] = (tran_low_t)fdct_round_shift(t2);
      output[4 * 8] = (tran_low_t)fdct_round_shift(t1);
      output[6 * 8] = (tran_low_t)fdct_round_shift(t3);

      // Stage 2
      t0 = (s6 - s5) * cospi_16_64;
      t1 = (s6 + s5) * cospi_16_64;
      t2 = fdct_round_shift(t0);
      t3 = fdct_round_shift(t1);

      // Stage 3
      x0 = s4 + t2;
      x1 = s4 - t2;
      x2 = s7 - t3;
      x3 = s7 + t3;

      // Stage 4
clang-format's avatar
clang-format committed
617 618
      t0 = x0 * cospi_28_64 + x3 * cospi_4_64;
      t1 = x1 * cospi_12_64 + x2 * cospi_20_64;
619
      t2 = x2 * cospi_12_64 + x1 * -cospi_20_64;
clang-format's avatar
clang-format committed
620
      t3 = x3 * cospi_28_64 + x0 * -cospi_4_64;
621 622 623 624 625 626 627 628 629 630 631 632
      output[1 * 8] = (tran_low_t)fdct_round_shift(t0);
      output[3 * 8] = (tran_low_t)fdct_round_shift(t2);
      output[5 * 8] = (tran_low_t)fdct_round_shift(t1);
      output[7 * 8] = (tran_low_t)fdct_round_shift(t3);
      input++;
      output++;
    }
  }

  // Rows
  for (i = 0; i < 8; ++i) {
    fdct8(&intermediate[i * 8], &coeff_ptr[i * 8]);
clang-format's avatar
clang-format committed
633
    for (j = 0; j < 8; ++j) coeff_ptr[j + i * 8] /= 2;
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
  }

  memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
  memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));

  if (!skip_block) {
    // Quantization pass: All coefficients with index >= zero_flag are
    // skippable. Note: zero_flag can be zero.
    for (i = 0; i < n_coeffs; i++) {
      const int rc = scan[i];
      const int coeff = coeff_ptr[rc];
      const int coeff_sign = (coeff >> 31);
      const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;

      int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX);
      tmp = (tmp * quant_ptr[rc != 0]) >> 16;

      qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign;
      dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0];

clang-format's avatar
clang-format committed
654
      if (tmp) eob = i;
655 656 657 658 659
    }
  }
  *eob_ptr = eob + 1;
}

clang-format's avatar
clang-format committed
660 661
void vp9_fht8x8_c(const int16_t *input, tran_low_t *output, int stride,
                  int tx_type) {
662
  if (tx_type == DCT_DCT) {
663
    vpx_fdct8x8_c(input, output, stride);
664 665 666 667 668 669 670 671
  } else {
    tran_low_t out[64];
    int i, j;
    tran_low_t temp_in[8], temp_out[8];
    const transform_2d ht = FHT_8[tx_type];

    // Columns
    for (i = 0; i < 8; ++i) {
clang-format's avatar
clang-format committed
672
      for (j = 0; j < 8; ++j) temp_in[j] = input[j * stride + i] * 4;
673
      ht.cols(temp_in, temp_out);
clang-format's avatar
clang-format committed
674
      for (j = 0; j < 8; ++j) out[j * 8 + i] = temp_out[j];
675 676 677 678
    }

    // Rows
    for (i = 0; i < 8; ++i) {
clang-format's avatar
clang-format committed
679
      for (j = 0; j < 8; ++j) temp_in[j] = out[j + i * 8];
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
      ht.rows(temp_in, temp_out);
      for (j = 0; j < 8; ++j)
        output[j + i * 8] = (temp_out[j] + (temp_out[j] < 0)) >> 1;
    }
  }
}

/* 4-point reversible, orthonormal Walsh-Hadamard in 3.5 adds, 0.5 shifts per
   pixel. */
void vp9_fwht4x4_c(const int16_t *input, tran_low_t *output, int stride) {
  int i;
  tran_high_t a1, b1, c1, d1, e1;
  const int16_t *ip_pass0 = input;
  const tran_low_t *ip = NULL;
  tran_low_t *op = output;

  for (i = 0; i < 4; i++) {
    a1 = ip_pass0[0 * stride];
    b1 = ip_pass0[1 * stride];
    c1 = ip_pass0[2 * stride];
    d1 = ip_pass0[3 * stride];

    a1 += b1;
    d1 = d1 - c1;
    e1 = (a1 - d1) >> 1;
    b1 = e1 - b1;
    c1 = e1 - c1;
    a1 -= c1;
    d1 += b1;
    op[0] = (tran_low_t)a1;
    op[4] = (tran_low_t)c1;
    op[8] = (tran_low_t)d1;
    op[12] = (tran_low_t)b1;

    ip_pass0++;
    op++;
  }
  ip = output;
  op = output;

  for (i = 0; i < 4; i++) {
    a1 = ip[0];
    b1 = ip[1];
    c1 = ip[2];
    d1 = ip[3];

    a1 += b1;
    d1 -= c1;
    e1 = (a1 - d1) >> 1;
    b1 = e1 - b1;
    c1 = e1 - c1;
    a1 -= c1;
    d1 += b1;
    op[0] = (tran_low_t)(a1 * UNIT_QUANT_FACTOR);
    op[1] = (tran_low_t)(c1 * UNIT_QUANT_FACTOR);
    op[2] = (tran_low_t)(d1 * UNIT_QUANT_FACTOR);
    op[3] = (tran_low_t)(b1 * UNIT_QUANT_FACTOR);

    ip += 4;
    op += 4;
  }
}

clang-format's avatar
clang-format committed
743 744
void vp9_fht16x16_c(const int16_t *input, tran_low_t *output, int stride,
                    int tx_type) {
745
  if (tx_type == DCT_DCT) {
746
    vpx_fdct16x16_c(input, output, stride);
747
  } else {
748
    tran_low_t out[256];
749
    int i, j;
750
    tran_low_t temp_in[16], temp_out[16];
751 752 753 754
    const transform_2d ht = FHT_16[tx_type];

    // Columns
    for (i = 0; i < 16; ++i) {
clang-format's avatar
clang-format committed
755
      for (j = 0; j < 16; ++j) temp_in[j] = input[j * stride + i] * 4;
756 757
      ht.cols(temp_in, temp_out);
      for (j = 0; j < 16; ++j)
758
        out[j * 16 + i] = (temp_out[j] + 1 + (temp_out[j] < 0)) >> 2;
759
    }
760

761 762
    // Rows
    for (i = 0; i < 16; ++i) {
clang-format's avatar
clang-format committed
763
      for (j = 0; j < 16; ++j) temp_in[j] = out[j + i * 16];
764
      ht.rows(temp_in, temp_out);
clang-format's avatar
clang-format committed
765
      for (j = 0; j < 16; ++j) output[j + i * 16] = temp_out[j];
766
    }
767
  }
768
}
769

770
#if CONFIG_VP9_HIGHBITDEPTH
clang-format's avatar
clang-format committed
771 772
void vp9_highbd_fht4x4_c(const int16_t *input, tran_low_t *output, int stride,
                         int tx_type) {
773 774 775
  vp9_fht4x4_c(input, output, stride, tx_type);
}

clang-format's avatar
clang-format committed
776 777
void vp9_highbd_fht8x8_c(const int16_t *input, tran_low_t *output, int stride,
                         int tx_type) {
778 779 780
  vp9_fht8x8_c(input, output, stride, tx_type);
}

781 782
void vp9_highbd_fwht4x4_c(const int16_t *input, tran_low_t *output,
                          int stride) {
783 784 785
  vp9_fwht4x4_c(input, output, stride);
}

clang-format's avatar
clang-format committed
786 787
void vp9_highbd_fht16x16_c(const int16_t *input, tran_low_t *output, int stride,
                           int tx_type) {
788 789 790
  vp9_fht16x16_c(input, output, stride, tx_type);
}
#endif  // CONFIG_VP9_HIGHBITDEPTH