vp9_idctllm.c 39.1 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
12
13
14
15
16
17
18
19
20
21
22
23
24
 */


/****************************************************************************
 * Notes:
 *
 * This implementation makes use of 16 bit fixed point verio of two multiply
 * constants:
 *         1.   sqrt(2) * cos (pi/8)
 *         2.   sqrt(2) * sin (pi/8)
 * Becuase the first constant is bigger than 1, to maintain the same 16 bit
 * fixed point precision as the second one, we use a trick of
 *         x * a = x + x*(a-1)
 * so
 *         x * sqrt(2) * cos (pi/8) = x + x * (sqrt(2) *cos(pi/8)-1).
 **************************************************************************/
Yunqing Wang's avatar
Yunqing Wang committed
25
26
#include <assert.h>
#include <math.h>
Dmitry Kovalev's avatar
Dmitry Kovalev committed
27

28
#include "./vpx_config.h"
29
30
#include "vp9/common/vp9_systemdependent.h"
#include "vp9/common/vp9_blockd.h"
31
#include "vp9/common/vp9_common.h"
32
#include "vp9/common/vp9_idct.h"
33

Dmitry Kovalev's avatar
Dmitry Kovalev committed
34
35
36
37
38
39
40
#define ROUND_POWER_OF_TWO(value, n) (((value) + (1 << ((n) - 1))) >> (n))

/* If we don't want to use ROUND_POWER_OF_TWO macro
static INLINE int16_t round_power_of_two(int16_t value, int n) {
  return (value + (1 << (n - 1))) >> n;
}*/

Dmitry Kovalev's avatar
Dmitry Kovalev committed
41
42
43
44
45
46
typedef void (*transform_1d)(int16_t*, int16_t*);

typedef struct {
  transform_1d cols, rows;  // vertical and horizontal
} transform_2d;

47
void vp9_short_inv_walsh4x4_x8_c(int16_t *input, int16_t *output, int pitch) {
John Koleszar's avatar
John Koleszar committed
48
49
  int i;
  int a1, b1, c1, d1;
50
51
  int16_t *ip = input;
  int16_t *op = output;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
52
  const int half_pitch = pitch >> 1;
John Koleszar's avatar
John Koleszar committed
53
54

  for (i = 0; i < 4; i++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
55
56
57
58
    a1 = (ip[0] + ip[3]) >> WHT_UPSCALE_FACTOR;
    b1 = (ip[1] + ip[2]) >> WHT_UPSCALE_FACTOR;
    c1 = (ip[1] - ip[2]) >> WHT_UPSCALE_FACTOR;
    d1 = (ip[0] - ip[3]) >> WHT_UPSCALE_FACTOR;
John Koleszar's avatar
John Koleszar committed
59
60
61
62
63
64
65

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

    ip += 4;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
66
    op += half_pitch;
John Koleszar's avatar
John Koleszar committed
67
68
69
70
71
  }

  ip = output;
  op = output;
  for (i = 0; i < 4; i++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
72
73
74
75
    a1 = ip[half_pitch * 0] + ip[half_pitch * 3];
    b1 = ip[half_pitch * 1] + ip[half_pitch * 2];
    c1 = ip[half_pitch * 1] - ip[half_pitch * 2];
    d1 = ip[half_pitch * 0] - ip[half_pitch * 3];
John Koleszar's avatar
John Koleszar committed
76
77


Dmitry Kovalev's avatar
Dmitry Kovalev committed
78
79
80
81
    op[half_pitch * 0] = (a1 + b1 + 1) >> 1;
    op[half_pitch * 1] = (c1 + d1) >> 1;
    op[half_pitch * 2] = (a1 - b1) >> 1;
    op[half_pitch * 3] = (d1 - c1) >> 1;
John Koleszar's avatar
John Koleszar committed
82
83
84
85
86

    ip++;
    op++;
  }
}
Hui Su's avatar
Hui Su committed
87

88
void vp9_short_inv_walsh4x4_1_x8_c(int16_t *in, int16_t *out, int pitch) {
John Koleszar's avatar
John Koleszar committed
89
  int i;
90
91
92
  int16_t tmp[4];
  int16_t *ip = in;
  int16_t *op = tmp;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
93
  const int half_pitch = pitch >> 1;
John Koleszar's avatar
John Koleszar committed
94
95

  op[0] = ((ip[0] >> WHT_UPSCALE_FACTOR) + 1) >> 1;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
96
  op[1] = op[2] = op[3] = (ip[0] >> WHT_UPSCALE_FACTOR) >> 1;
John Koleszar's avatar
John Koleszar committed
97
98
99
100

  ip = tmp;
  op = out;
  for (i = 0; i < 4; i++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
101
102
    op[half_pitch * 0] = (ip[0] + 1) >> 1;
    op[half_pitch * 1] = op[half_pitch * 2] = op[half_pitch * 3] = ip[0] >> 1;
John Koleszar's avatar
John Koleszar committed
103
104
105
106
    ip++;
    op++;
  }
}
Hui Su's avatar
Hui Su committed
107

Yaowu Xu's avatar
Yaowu Xu committed
108
void vp9_dc_only_inv_walsh_add_c(int input_dc, uint8_t *pred_ptr,
109
                                 uint8_t *dst_ptr,
110
                                 int pitch, int stride) {
John Koleszar's avatar
John Koleszar committed
111
  int r, c;
Yaowu Xu's avatar
Yaowu Xu committed
112
  int16_t dc = input_dc;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
113
  int16_t tmp[4 * 4];
Yaowu Xu's avatar
Yaowu Xu committed
114
  vp9_short_inv_walsh4x4_1_x8_c(&dc, tmp, 4 << 1);
Hui Su's avatar
Hui Su committed
115

John Koleszar's avatar
John Koleszar committed
116
  for (r = 0; r < 4; r++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
117
    for (c = 0; c < 4; c++)
118
      dst_ptr[c] = clip_pixel(tmp[r * 4 + c] + pred_ptr[c]);
Hui Su's avatar
Hui Su committed
119

John Koleszar's avatar
John Koleszar committed
120
121
122
    dst_ptr += stride;
    pred_ptr += pitch;
  }
Hui Su's avatar
Hui Su committed
123
}
Yaowu Xu's avatar
Yaowu Xu committed
124

Yaowu Xu's avatar
Yaowu Xu committed
125
126
127
128
129
130
131
132
133
134
135
136
void idct4_1d(int16_t *input, int16_t *output) {
  int16_t step[4];
  int temp1, temp2;
  // stage 1
  temp1 = (input[0] + input[2]) * cospi_16_64;
  temp2 = (input[0] - input[2]) * cospi_16_64;
  step[0] = dct_const_round_shift(temp1);
  step[1] = dct_const_round_shift(temp2);
  temp1 = input[1] * cospi_24_64 - input[3] * cospi_8_64;
  temp2 = input[1] * cospi_8_64 + input[3] * cospi_24_64;
  step[2] = dct_const_round_shift(temp1);
  step[3] = dct_const_round_shift(temp2);
John Koleszar's avatar
John Koleszar committed
137

Yaowu Xu's avatar
Yaowu Xu committed
138
139
140
141
142
  // stage 2
  output[0] = step[0] + step[3];
  output[1] = step[1] + step[2];
  output[2] = step[1] - step[2];
  output[3] = step[0] - step[3];
143
144
}

Yaowu Xu's avatar
Yaowu Xu committed
145
146
void vp9_short_idct4x4llm_c(int16_t *input, int16_t *output, int pitch) {
  int16_t out[4 * 4];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
147
148
  int16_t *outptr = out;
  const int half_pitch = pitch >> 1;
Yaowu Xu's avatar
Yaowu Xu committed
149
150
  int i, j;
  int16_t temp_in[4], temp_out[4];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
151
152

  // Rows
Yaowu Xu's avatar
Yaowu Xu committed
153
154
155
156
157
158
159
  for (i = 0; i < 4; ++i) {
    for (j = 0; j < 4; ++j)
      temp_in[j] = input[j];
    idct4_1d(temp_in, outptr);
    input += 4;
    outptr += 4;
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
160
161

  // Columns
Yaowu Xu's avatar
Yaowu Xu committed
162
163
164
165
166
  for (i = 0; i < 4; ++i) {
    for (j = 0; j < 4; ++j)
      temp_in[j] = out[j * 4 + i];
    idct4_1d(temp_in, temp_out);
    for (j = 0; j < 4; ++j)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
167
      output[j * half_pitch + i] = ROUND_POWER_OF_TWO(temp_out[j], 4);
Yaowu Xu's avatar
Yaowu Xu committed
168
169
170
171
172
173
174
  }
}

void vp9_short_idct4x4llm_1_c(int16_t *input, int16_t *output, int pitch) {
  int i;
  int a1;
  int16_t *op = output;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
175
  const int half_pitch = pitch >> 1;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
176
177
178
  int16_t out = dct_const_round_shift(input[0] * cospi_16_64);
  out = dct_const_round_shift(out * cospi_16_64);
  a1 = ROUND_POWER_OF_TWO(out, 4);
Yaowu Xu's avatar
Yaowu Xu committed
179
180

  for (i = 0; i < 4; i++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
181
    op[0] = op[1] = op[2] = op[3] = a1;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
182
    op += half_pitch;
Yaowu Xu's avatar
Yaowu Xu committed
183
184
185
186
187
188
189
  }
}

void vp9_dc_only_idct_add_c(int input_dc, uint8_t *pred_ptr,
                            uint8_t *dst_ptr, int pitch, int stride) {
  int a1;
  int r, c;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
190
191
192
  int16_t out = dct_const_round_shift(input_dc * cospi_16_64);
  out = dct_const_round_shift(out * cospi_16_64);
  a1 = ROUND_POWER_OF_TWO(out, 4);
Yaowu Xu's avatar
Yaowu Xu committed
193
194

  for (r = 0; r < 4; r++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
195
    for (c = 0; c < 4; c++)
Yaowu Xu's avatar
Yaowu Xu committed
196
      dst_ptr[c] = clip_pixel(a1 + pred_ptr[c]);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
197

Yaowu Xu's avatar
Yaowu Xu committed
198
199
200
201
202
    dst_ptr += stride;
    pred_ptr += pitch;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
void idct8_1d(int16_t *input, int16_t *output) {
  int16_t step1[8], step2[8];
  int temp1, temp2;
  // stage 1
  step1[0] = input[0];
  step1[2] = input[4];
  step1[1] = input[2];
  step1[3] = input[6];
  temp1 = input[1] * cospi_28_64 - input[7] * cospi_4_64;
  temp2 = input[1] * cospi_4_64 + input[7] * cospi_28_64;
  step1[4] = dct_const_round_shift(temp1);
  step1[7] = dct_const_round_shift(temp2);
  temp1 = input[5] * cospi_12_64 - input[3] * cospi_20_64;
  temp2 = input[5] * cospi_20_64 + input[3] * cospi_12_64;
  step1[5] = dct_const_round_shift(temp1);
  step1[6] = dct_const_round_shift(temp2);
John Koleszar's avatar
John Koleszar committed
219

Yaowu Xu's avatar
Yaowu Xu committed
220
221
  // stage 2 & stage 3 - even half
  idct4_1d(step1, step1);
222

Yaowu Xu's avatar
Yaowu Xu committed
223
224
225
226
227
  // stage 2 - odd half
  step2[4] = step1[4] + step1[5];
  step2[5] = step1[4] - step1[5];
  step2[6] = -step1[6] + step1[7];
  step2[7] = step1[6] + step1[7];
John Koleszar's avatar
John Koleszar committed
228

Yaowu Xu's avatar
Yaowu Xu committed
229
230
231
232
233
234
235
  // stage 3 -odd half
  step1[4] = step2[4];
  temp1 = (step2[6] - step2[5]) * cospi_16_64;
  temp2 = (step2[5] + step2[6]) * cospi_16_64;
  step1[5] = dct_const_round_shift(temp1);
  step1[6] = dct_const_round_shift(temp2);
  step1[7] = step2[7];
John Koleszar's avatar
John Koleszar committed
236

Yaowu Xu's avatar
Yaowu Xu committed
237
238
239
240
241
242
243
244
245
  // stage 4
  output[0] = step1[0] + step1[7];
  output[1] = step1[1] + step1[6];
  output[2] = step1[2] + step1[5];
  output[3] = step1[3] + step1[4];
  output[4] = step1[3] - step1[4];
  output[5] = step1[2] - step1[5];
  output[6] = step1[1] - step1[6];
  output[7] = step1[0] - step1[7];
246
247
}

Yaowu Xu's avatar
Yaowu Xu committed
248
249
void vp9_short_idct8x8_c(int16_t *input, int16_t *output, int pitch) {
  int16_t out[8 * 8];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
250
251
  int16_t *outptr = out;
  const int half_pitch = pitch >> 1;
Yaowu Xu's avatar
Yaowu Xu committed
252
253
  int i, j;
  int16_t temp_in[8], temp_out[8];
Yunqing Wang's avatar
Yunqing Wang committed
254

Dmitry Kovalev's avatar
Dmitry Kovalev committed
255
  // Rows
Yaowu Xu's avatar
Yaowu Xu committed
256
257
258
259
  for (i = 0; i < 8; ++i) {
    idct8_1d(input, outptr);
    input += 8;
    outptr += 8;
Yunqing Wang's avatar
Yunqing Wang committed
260
261
  }

Dmitry Kovalev's avatar
Dmitry Kovalev committed
262
  // Columns
Yaowu Xu's avatar
Yaowu Xu committed
263
264
265
266
267
  for (i = 0; i < 8; ++i) {
    for (j = 0; j < 8; ++j)
      temp_in[j] = out[j * 8 + i];
    idct8_1d(temp_in, temp_out);
    for (j = 0; j < 8; ++j)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
268
      output[j * half_pitch + i] = ROUND_POWER_OF_TWO(temp_out[j], 5);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
269
  }
Yunqing Wang's avatar
Yunqing Wang committed
270
271
}

Jingning Han's avatar
Jingning Han committed
272
273
274
static void iadst4_1d(int16_t *input, int16_t *output) {
  int s0, s1, s2, s3, s4, s5, s6, s7;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
275
276
277
278
  int x0 = input[0];
  int x1 = input[1];
  int x2 = input[2];
  int x3 = input[3];
Jingning Han's avatar
Jingning Han committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313

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

  s0 = sinpi_1_9 * x0;
  s1 = sinpi_2_9 * x0;
  s2 = sinpi_3_9 * x1;
  s3 = sinpi_4_9 * x2;
  s4 = sinpi_1_9 * x2;
  s5 = sinpi_2_9 * x3;
  s6 = sinpi_4_9 * x3;
  s7 = x0 - x2 + x3;

  x0 = s0 + s3 + s5;
  x1 = s1 - s4 - s6;
  x2 = sinpi_3_9 * s7;
  x3 = s2;

  s0 = x0 + x3;
  s1 = x1 + x3;
  s2 = x2;
  s3 = x0 + x1 - x3;

  // 1-D transform scaling factor is sqrt(2).
  // The overall dynamic range is 14b (input) + 14b (multiplication scaling)
  // + 1b (addition) = 29b.
  // Hence the output bit depth is 15b.
  output[0] = dct_const_round_shift(s0);
  output[1] = dct_const_round_shift(s1);
  output[2] = dct_const_round_shift(s2);
  output[3] = dct_const_round_shift(s3);
}

Dmitry Kovalev's avatar
Dmitry Kovalev committed
314
315
316
317
318
319
320
static const transform_2d IHT_4[] = {
  { idct4_1d,  idct4_1d  },  // DCT_DCT  = 0
  { iadst4_1d, idct4_1d  },  // ADST_DCT = 1
  { idct4_1d,  iadst4_1d },  // DCT_ADST = 2
  { iadst4_1d, iadst4_1d }   // ADST_ADST = 3
};

Jingning Han's avatar
Jingning Han committed
321
322
void vp9_short_iht4x4_c(int16_t *input, int16_t *output,
                        int pitch, TX_TYPE tx_type) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
323
324
325
  int16_t out[4 * 4];
  int16_t *outptr = out;
  const int half_pitch = pitch >> 1;
Jingning Han's avatar
Jingning Han committed
326
327
  int i, j;
  int16_t temp_in[4], temp_out[4];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
328
  const transform_2d ht = IHT_4[tx_type];
Jingning Han's avatar
Jingning Han committed
329
330
331

  // inverse transform row vectors
  for (i = 0; i < 4; ++i) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
332
    ht.rows(input, outptr);
Jingning Han's avatar
Jingning Han committed
333
334
335
336
337
338
339
340
    input  += 4;
    outptr += 4;
  }

  // inverse transform column vectors
  for (i = 0; i < 4; ++i) {
    for (j = 0; j < 4; ++j)
      temp_in[j] = out[j * 4 + i];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
341
    ht.cols(temp_in, temp_out);
Jingning Han's avatar
Jingning Han committed
342
    for (j = 0; j < 4; ++j)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
343
      output[j * half_pitch + i] = ROUND_POWER_OF_TWO(temp_out[j], 4);
Jingning Han's avatar
Jingning Han committed
344
345
346
  }
}

347
348
349
static void iadst8_1d(int16_t *input, int16_t *output) {
  int s0, s1, s2, s3, s4, s5, s6, s7;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
350
351
352
353
354
355
356
357
  int x0 = input[7];
  int x1 = input[0];
  int x2 = input[5];
  int x3 = input[2];
  int x4 = input[3];
  int x5 = input[4];
  int x6 = input[1];
  int x7 = input[6];
358
359
360

  if (!(x0 | x1 | x2 | x3 | x4 | x5 | x6 | x7)) {
    output[0] = output[1] = output[2] = output[3] = output[4]
Dmitry Kovalev's avatar
Dmitry Kovalev committed
361
              = output[5] = output[6] = output[7] = 0;
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
    return;
  }

  // stage 1
  s0 = cospi_2_64  * x0 + cospi_30_64 * x1;
  s1 = cospi_30_64 * x0 - cospi_2_64  * x1;
  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;
  s6 = cospi_26_64 * x6 + cospi_6_64  * x7;
  s7 = cospi_6_64  * x6 - cospi_26_64 * x7;

  x0 = dct_const_round_shift(s0 + s4);
  x1 = dct_const_round_shift(s1 + s5);
  x2 = dct_const_round_shift(s2 + s6);
  x3 = dct_const_round_shift(s3 + s7);
  x4 = dct_const_round_shift(s0 - s4);
  x5 = dct_const_round_shift(s1 - s5);
  x6 = dct_const_round_shift(s2 - s6);
  x7 = dct_const_round_shift(s3 - s7);

  // stage 2
  s0 = x0;
  s1 = x1;
  s2 = x2;
  s3 = x3;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
389
390
391
392
  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;
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413

  x0 = s0 + s2;
  x1 = s1 + s3;
  x2 = s0 - s2;
  x3 = s1 - s3;
  x4 = dct_const_round_shift(s4 + s6);
  x5 = dct_const_round_shift(s5 + s7);
  x6 = dct_const_round_shift(s4 - s6);
  x7 = dct_const_round_shift(s5 - s7);

  // 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);

  x2 = dct_const_round_shift(s2);
  x3 = dct_const_round_shift(s3);
  x6 = dct_const_round_shift(s6);
  x7 = dct_const_round_shift(s7);

Dmitry Kovalev's avatar
Dmitry Kovalev committed
414
415
416
417
418
419
420
421
  output[0] =  x0;
  output[1] = -x4;
  output[2] =  x6;
  output[3] = -x2;
  output[4] =  x3;
  output[5] = -x7;
  output[6] =  x5;
  output[7] = -x1;
422
423
}

Dmitry Kovalev's avatar
Dmitry Kovalev committed
424
425
426
427
428
429
430
static const transform_2d IHT_8[] = {
  { idct8_1d,  idct8_1d  },  // DCT_DCT  = 0
  { iadst8_1d, idct8_1d  },  // ADST_DCT = 1
  { idct8_1d,  iadst8_1d },  // DCT_ADST = 2
  { iadst8_1d, iadst8_1d }   // ADST_ADST = 3
};

431
void vp9_short_iht8x8_c(int16_t *input, int16_t *output,
Jingning Han's avatar
Jingning Han committed
432
                        int pitch, TX_TYPE tx_type) {
433
  int16_t out[8 * 8];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
434
435
  int16_t *outptr = out;
  const int half_pitch = pitch >> 1;
436
437
  int i, j;
  int16_t temp_in[8], temp_out[8];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
438
  const transform_2d ht = IHT_8[tx_type];
439
440
441

  // inverse transform row vectors
  for (i = 0; i < 8; ++i) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
442
    ht.rows(input, outptr);
443
444
445
446
447
448
449
450
    input += 8;
    outptr += 8;
  }

  // inverse transform column vectors
  for (i = 0; i < 8; ++i) {
    for (j = 0; j < 8; ++j)
      temp_in[j] = out[j * 8 + i];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
451
    ht.cols(temp_in, temp_out);
452
    for (j = 0; j < 8; ++j)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
453
      output[j * half_pitch + i] = ROUND_POWER_OF_TWO(temp_out[j], 5);
454
455
456
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
457
458
void vp9_short_idct10_8x8_c(int16_t *input, int16_t *output, int pitch) {
  int16_t out[8 * 8];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
459
460
  int16_t *outptr = out;
  const int half_pitch = pitch >> 1;
Yunqing Wang's avatar
Yunqing Wang committed
461
  int i, j;
Yaowu Xu's avatar
Yaowu Xu committed
462
  int16_t temp_in[8], temp_out[8];
Yunqing Wang's avatar
Yunqing Wang committed
463

Yaowu Xu's avatar
Yaowu Xu committed
464
465
466
467
468
469
470
  vpx_memset(out, 0, sizeof(out));
  // First transform rows
  // only first 4 row has non-zero coefs
  for (i = 0; i < 4; ++i) {
    idct8_1d(input, outptr);
    input += 8;
    outptr += 8;
Yunqing Wang's avatar
Yunqing Wang committed
471
472
  }

Yaowu Xu's avatar
Yaowu Xu committed
473
474
475
476
477
478
  // Then transform columns
  for (i = 0; i < 8; ++i) {
    for (j = 0; j < 8; ++j)
      temp_in[j] = out[j * 8 + i];
    idct8_1d(temp_in, temp_out);
    for (j = 0; j < 8; ++j)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
479
      output[j * half_pitch + i] = ROUND_POWER_OF_TWO(temp_out[j], 5);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
480
  }
Yaowu Xu's avatar
Yaowu Xu committed
481
482
483
}

void vp9_short_idct1_8x8_c(int16_t *input, int16_t *output) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
484
485
  int16_t out = dct_const_round_shift(input[0] * cospi_16_64);
  out = dct_const_round_shift(out * cospi_16_64);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
486
  output[0] = ROUND_POWER_OF_TWO(out, 5);
Yunqing Wang's avatar
Yunqing Wang committed
487
}
488

Yaowu Xu's avatar
Yaowu Xu committed
489
490
void idct16_1d(int16_t *input, int16_t *output) {
  int16_t step1[16], step2[16];
491
  int temp1, temp2;
492

Yaowu Xu's avatar
Yaowu Xu committed
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
  // stage 1
  step1[0] = input[0/2];
  step1[1] = input[16/2];
  step1[2] = input[8/2];
  step1[3] = input[24/2];
  step1[4] = input[4/2];
  step1[5] = input[20/2];
  step1[6] = input[12/2];
  step1[7] = input[28/2];
  step1[8] = input[2/2];
  step1[9] = input[18/2];
  step1[10] = input[10/2];
  step1[11] = input[26/2];
  step1[12] = input[6/2];
  step1[13] = input[22/2];
  step1[14] = input[14/2];
  step1[15] = input[30/2];

  // stage 2
  step2[0] = step1[0];
  step2[1] = step1[1];
  step2[2] = step1[2];
  step2[3] = step1[3];
  step2[4] = step1[4];
  step2[5] = step1[5];
  step2[6] = step1[6];
  step2[7] = step1[7];

  temp1 = step1[8] * cospi_30_64 - step1[15] * cospi_2_64;
  temp2 = step1[8] * cospi_2_64 + step1[15] * cospi_30_64;
  step2[8] = dct_const_round_shift(temp1);
  step2[15] = dct_const_round_shift(temp2);

  temp1 = step1[9] * cospi_14_64 - step1[14] * cospi_18_64;
  temp2 = step1[9] * cospi_18_64 + step1[14] * cospi_14_64;
  step2[9] = dct_const_round_shift(temp1);
  step2[14] = dct_const_round_shift(temp2);

  temp1 = step1[10] * cospi_22_64 - step1[13] * cospi_10_64;
  temp2 = step1[10] * cospi_10_64 + step1[13] * cospi_22_64;
  step2[10] = dct_const_round_shift(temp1);
  step2[13] = dct_const_round_shift(temp2);

  temp1 = step1[11] * cospi_6_64 - step1[12] * cospi_26_64;
  temp2 = step1[11] * cospi_26_64 + step1[12] * cospi_6_64;
  step2[11] = dct_const_round_shift(temp1);
  step2[12] = dct_const_round_shift(temp2);

  // stage 3
  step1[0] = step2[0];
  step1[1] = step2[1];
  step1[2] = step2[2];
  step1[3] = step2[3];

  temp1 = step2[4] * cospi_28_64 - step2[7] * cospi_4_64;
  temp2 = step2[4] * cospi_4_64 + step2[7] * cospi_28_64;
  step1[4] = dct_const_round_shift(temp1);
  step1[7] = dct_const_round_shift(temp2);
  temp1 = step2[5] * cospi_12_64 - step2[6] * cospi_20_64;
  temp2 = step2[5] * cospi_20_64 + step2[6] * cospi_12_64;
  step1[5] = dct_const_round_shift(temp1);
  step1[6] = dct_const_round_shift(temp2);

  step1[8] = step2[8] + step2[9];
  step1[9] = step2[8] - step2[9];
  step1[10] = -step2[10] + step2[11];
  step1[11] = step2[10] + step2[11];
  step1[12] = step2[12] + step2[13];
  step1[13] = step2[12] - step2[13];
  step1[14] = -step2[14] + step2[15];
  step1[15] = step2[14] + step2[15];

  temp1 = (step1[0] + step1[1]) * cospi_16_64;
  temp2 = (step1[0] - step1[1]) * cospi_16_64;
  step2[0] = dct_const_round_shift(temp1);
  step2[1] = dct_const_round_shift(temp2);
  temp1 = step1[2] * cospi_24_64 - step1[3] * cospi_8_64;
  temp2 = step1[2] * cospi_8_64 + step1[3] * cospi_24_64;
  step2[2] = dct_const_round_shift(temp1);
  step2[3] = dct_const_round_shift(temp2);
  step2[4] = step1[4] + step1[5];
  step2[5] = step1[4] - step1[5];
  step2[6] = -step1[6] + step1[7];
  step2[7] = step1[6] + step1[7];

  step2[8] = step1[8];
  step2[15] = step1[15];
  temp1 = -step1[9] * cospi_8_64 + step1[14] * cospi_24_64;
  temp2 = step1[9] * cospi_24_64 + step1[14] * cospi_8_64;
  step2[9] = dct_const_round_shift(temp1);
  step2[14] = dct_const_round_shift(temp2);
  temp1 = -step1[10] * cospi_24_64 - step1[13] * cospi_8_64;
  temp2 = -step1[10] * cospi_8_64 + step1[13] * cospi_24_64;
  step2[10] = dct_const_round_shift(temp1);
  step2[13] = dct_const_round_shift(temp2);
  step2[11] = step1[11];
  step2[12] = step1[12];

  // stage 5
  step1[0] = step2[0] + step2[3];
  step1[1] = step2[1] + step2[2];
  step1[2] = step2[1] - step2[2];
  step1[3] = step2[0] - step2[3];
  step1[4] = step2[4];
  temp1 = (step2[6] - step2[5]) * cospi_16_64;
  temp2 = (step2[5] + step2[6]) * cospi_16_64;
  step1[5] = dct_const_round_shift(temp1);
  step1[6] = dct_const_round_shift(temp2);
  step1[7] = step2[7];

  step1[8] = step2[8] + step2[11];
  step1[9] = step2[9] + step2[10];
  step1[10] = step2[9] - step2[10];
  step1[11] = step2[8] - step2[11];
  step1[12] = -step2[12] + step2[15];
  step1[13] = -step2[13] + step2[14];
  step1[14] = step2[13] + step2[14];
  step1[15] = step2[12] + step2[15];

  // stage 6
  step2[0] = step1[0] + step1[7];
  step2[1] = step1[1] + step1[6];
  step2[2] = step1[2] + step1[5];
  step2[3] = step1[3] + step1[4];
  step2[4] = step1[3] - step1[4];
  step2[5] = step1[2] - step1[5];
  step2[6] = step1[1] - step1[6];
  step2[7] = step1[0] - step1[7];
  step2[8] = step1[8];
  step2[9] = step1[9];
  temp1 = (-step1[10] + step1[13]) * cospi_16_64;
  temp2 = (step1[10] + step1[13]) * cospi_16_64;
  step2[10] = dct_const_round_shift(temp1);
  step2[13] = dct_const_round_shift(temp2);
  temp1 = (-step1[11] + step1[12]) * cospi_16_64;
  temp2 = (step1[11] + step1[12]) * cospi_16_64;
  step2[11] = dct_const_round_shift(temp1);
  step2[12] = dct_const_round_shift(temp2);
  step2[14] = step1[14];
  step2[15] = step1[15];

  // stage 7
  output[0] = step2[0] + step2[15];
  output[1] = step2[1] + step2[14];
  output[2] = step2[2] + step2[13];
  output[3] = step2[3] + step2[12];
  output[4] = step2[4] + step2[11];
  output[5] = step2[5] + step2[10];
  output[6] = step2[6] + step2[9];
  output[7] = step2[7] + step2[8];
  output[8] = step2[7] - step2[8];
  output[9] = step2[6] - step2[9];
  output[10] = step2[5] - step2[10];
  output[11] = step2[4] - step2[11];
  output[12] = step2[3] - step2[12];
  output[13] = step2[2] - step2[13];
  output[14] = step2[1] - step2[14];
  output[15] = step2[0] - step2[15];
651
652
653
}

void vp9_short_idct16x16_c(int16_t *input, int16_t *output, int pitch) {
654
  int16_t out[16 * 16];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
655
656
  int16_t *outptr = out;
  const int half_pitch = pitch >> 1;
657
658
  int i, j;
  int16_t temp_in[16], temp_out[16];
659

660
661
  // First transform rows
  for (i = 0; i < 16; ++i) {
Yaowu Xu's avatar
Yaowu Xu committed
662
    idct16_1d(input, outptr);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
663
    input += half_pitch;
664
665
    outptr += 16;
  }
666

667
668
669
670
  // Then transform columns
  for (i = 0; i < 16; ++i) {
    for (j = 0; j < 16; ++j)
      temp_in[j] = out[j * 16 + i];
Yaowu Xu's avatar
Yaowu Xu committed
671
    idct16_1d(temp_in, temp_out);
672
    for (j = 0; j < 16; ++j)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
673
      output[j * 16 + i] = ROUND_POWER_OF_TWO(temp_out[j], 6);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
674
  }
675
}
Yunqing Wang's avatar
Yunqing Wang committed
676

677
678
679
void iadst16_1d(int16_t *input, int16_t *output) {
  int s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
  int x0 = input[15];
  int x1 = input[0];
  int x2 = input[13];
  int x3 = input[2];
  int x4 = input[11];
  int x5 = input[4];
  int x6 = input[9];
  int x7 = input[6];
  int x8 = input[7];
  int x9 = input[8];
  int x10 = input[5];
  int x11 = input[10];
  int x12 = input[3];
  int x13 = input[12];
  int x14 = input[1];
  int x15 = input[14];
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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829

  if (!(x0 | x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8
           | x9 | x10 | x11 | x12 | x13 | x14 | x15)) {
    output[0] = output[1] = output[2] = output[3] = output[4]
              = output[5] = output[6] = output[7] = output[8]
              = output[9] = output[10] = output[11] = output[12]
              = output[13] = output[14] = output[15] = 0;
    return;
  }

  // stage 1
  s0 = x0 * cospi_1_64  + x1 * cospi_31_64;
  s1 = x0 * cospi_31_64 - x1 * cospi_1_64;
  s2 = x2 * cospi_5_64  + x3 * cospi_27_64;
  s3 = x2 * cospi_27_64 - x3 * cospi_5_64;
  s4 = x4 * cospi_9_64  + x5 * cospi_23_64;
  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;
  s13 = x12 * cospi_7_64  - x13 * cospi_25_64;
  s14 = x14 * cospi_29_64 + x15 * cospi_3_64;
  s15 = x14 * cospi_3_64  - x15 * cospi_29_64;

  x0 = dct_const_round_shift(s0 + s8);
  x1 = dct_const_round_shift(s1 + s9);
  x2 = dct_const_round_shift(s2 + s10);
  x3 = dct_const_round_shift(s3 + s11);
  x4 = dct_const_round_shift(s4 + s12);
  x5 = dct_const_round_shift(s5 + s13);
  x6 = dct_const_round_shift(s6 + s14);
  x7 = dct_const_round_shift(s7 + s15);
  x8  = dct_const_round_shift(s0 - s8);
  x9  = dct_const_round_shift(s1 - s9);
  x10 = dct_const_round_shift(s2 - s10);
  x11 = dct_const_round_shift(s3 - s11);
  x12 = dct_const_round_shift(s4 - s12);
  x13 = dct_const_round_shift(s5 - s13);
  x14 = dct_const_round_shift(s6 - s14);
  x15 = dct_const_round_shift(s7 - s15);

  // stage 2
  s0 = x0;
  s1 = x1;
  s2 = x2;
  s3 = x3;
  s4 = x4;
  s5 = x5;
  s6 = x6;
  s7 = x7;
  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;

  x0 = s0 + s4;
  x1 = s1 + s5;
  x2 = s2 + s6;
  x3 = s3 + s7;
  x4 = s0 - s4;
  x5 = s1 - s5;
  x6 = s2 - s6;
  x7 = s3 - s7;
  x8 = dct_const_round_shift(s8 + s12);
  x9 = dct_const_round_shift(s9 + s13);
  x10 = dct_const_round_shift(s10 + s14);
  x11 = dct_const_round_shift(s11 + s15);
  x12 = dct_const_round_shift(s8 - s12);
  x13 = dct_const_round_shift(s9 - s13);
  x14 = dct_const_round_shift(s10 - s14);
  x15 = dct_const_round_shift(s11 - s15);

  // stage 3
  s0 = x0;
  s1 = x1;
  s2 = x2;
  s3 = x3;
  s4 = x4 * cospi_8_64  + x5 * cospi_24_64;
  s5 = x4 * cospi_24_64 - x5 * cospi_8_64;
  s6 = - x6 * cospi_24_64 + x7 * cospi_8_64;
  s7 =   x6 * cospi_8_64  + x7 * cospi_24_64;
  s8 = x8;
  s9 = x9;
  s10 = x10;
  s11 = x11;
  s12 = x12 * cospi_8_64  + x13 * cospi_24_64;
  s13 = x12 * cospi_24_64 - x13 * cospi_8_64;
  s14 = - x14 * cospi_24_64 + x15 * cospi_8_64;
  s15 =   x14 * cospi_8_64  + x15 * cospi_24_64;

  x0 = s0 + s2;
  x1 = s1 + s3;
  x2 = s0 - s2;
  x3 = s1 - s3;
  x4 = dct_const_round_shift(s4 + s6);
  x5 = dct_const_round_shift(s5 + s7);
  x6 = dct_const_round_shift(s4 - s6);
  x7 = dct_const_round_shift(s5 - s7);
  x8 = s8 + s10;
  x9 = s9 + s11;
  x10 = s8 - s10;
  x11 = s9 - s11;
  x12 = dct_const_round_shift(s12 + s14);
  x13 = dct_const_round_shift(s13 + s15);
  x14 = dct_const_round_shift(s12 - s14);
  x15 = dct_const_round_shift(s13 - s15);

  // stage 4
  s2 = (- cospi_16_64) * (x2 + x3);
  s3 = cospi_16_64 * (x2 - x3);
  s6 = cospi_16_64 * (x6 + x7);
  s7 = cospi_16_64 * (- x6 + x7);
  s10 = cospi_16_64 * (x10 + x11);
  s11 = cospi_16_64 * (- x10 + x11);
  s14 = (- cospi_16_64) * (x14 + x15);
  s15 = cospi_16_64 * (x14 - x15);

  x2 = dct_const_round_shift(s2);
  x3 = dct_const_round_shift(s3);
  x6 = dct_const_round_shift(s6);
  x7 = dct_const_round_shift(s7);
  x10 = dct_const_round_shift(s10);
  x11 = dct_const_round_shift(s11);
  x14 = dct_const_round_shift(s14);
  x15 = dct_const_round_shift(s15);

Dmitry Kovalev's avatar
Dmitry Kovalev committed
830
831
832
833
834
835
836
837
838
  output[0] =  x0;
  output[1] = -x8;
  output[2] =  x12;
  output[3] = -x4;
  output[4] =  x6;
  output[5] =  x14;
  output[6] =  x10;
  output[7] =  x2;
  output[8] =  x3;
839
  output[9] =  x11;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
840
841
842
843
844
845
  output[10] =  x15;
  output[11] =  x7;
  output[12] =  x5;
  output[13] = -x13;
  output[14] =  x9;
  output[15] = -x1;
846
847
}

Dmitry Kovalev's avatar
Dmitry Kovalev committed
848
849
850
851
852
853
854
static const transform_2d IHT_16[] = {
  { idct16_1d,  idct16_1d  },  // DCT_DCT  = 0
  { iadst16_1d, idct16_1d  },  // ADST_DCT = 1
  { idct16_1d,  iadst16_1d },  // DCT_ADST = 2
  { iadst16_1d, iadst16_1d }   // ADST_ADST = 3
};

855
void vp9_short_iht16x16_c(int16_t *input, int16_t *output,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
856
                          int pitch, TX_TYPE tx_type) {
857
  int16_t out[16 * 16];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
858
859
  int16_t *outptr = out;
  const int half_pitch = pitch >> 1;
860
861
  int i, j;
  int16_t temp_in[16], temp_out[16];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
862
  const transform_2d ht = IHT_16[tx_type];
863

Dmitry Kovalev's avatar
Dmitry Kovalev committed
864
  // Rows
865
  for (i = 0; i < 16; ++i) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
866
867
    ht.rows(input, outptr);
    input += half_pitch;
868
869
870
    outptr += 16;
  }

Dmitry Kovalev's avatar
Dmitry Kovalev committed
871
  // Columns
872
873
874
  for (i = 0; i < 16; ++i) {
    for (j = 0; j < 16; ++j)
      temp_in[j] = out[j * 16 + i];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
875
    ht.cols(temp_in, temp_out);
876
    for (j = 0; j < 16; ++j)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
877
      output[j * 16 + i] = ROUND_POWER_OF_TWO(temp_out[j], 6);
878
879
880
  }
}

Yunqing Wang's avatar
Yunqing Wang committed
881
882
void vp9_short_idct10_16x16_c(int16_t *input, int16_t *output, int pitch) {
    int16_t out[16 * 16];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
883
884
    int16_t *outptr = out;
    const int half_pitch = pitch >> 1;
Yunqing Wang's avatar
Yunqing Wang committed
885
886
887
888
889
890
891
892
    int i, j;
    int16_t temp_in[16], temp_out[16];

    /* First transform rows. Since all non-zero dct coefficients are in
     * upper-left 4x4 area, we only need to calculate first 4 rows here.
     */
    vpx_memset(out, 0, sizeof(out));
    for (i = 0; i < 4; ++i) {
Yaowu Xu's avatar
Yaowu Xu committed
893
      idct16_1d(input, outptr);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
894
      input += half_pitch;
Yunqing Wang's avatar
Yunqing Wang committed
895
896
897
898
899
900
901
      outptr += 16;
    }

    // Then transform columns
    for (i = 0; i < 16; ++i) {
      for (j = 0; j < 16; ++j)
        temp_in[j] = out[j*16 + i];
Yaowu Xu's avatar
Yaowu Xu committed
902
      idct16_1d(temp_in, temp_out);
Yunqing Wang's avatar
Yunqing Wang committed
903
      for (j = 0; j < 16; ++j)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
904
        output[j*16 + i] = ROUND_POWER_OF_TWO(temp_out[j], 6);
Yunqing Wang's avatar
Yunqing Wang committed
905
906
    }
}
907

908

Yaowu Xu's avatar
Yaowu Xu committed
909
void vp9_short_idct1_16x16_c(int16_t *input, int16_t *output) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
910
911
912
  int16_t out = dct_const_round_shift(input[0] * cospi_16_64);
  out = dct_const_round_shift(out * cospi_16_64);
  output[0] = ROUND_POWER_OF_TWO(out, 6);
913
}
914

915
916
917
void idct32_1d(int16_t *input, int16_t *output) {
  int16_t step1[32], step2[32];
  int temp1, temp2;
918

919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
  // stage 1
  step1[0] = input[0];
  step1[1] = input[16];
  step1[2] = input[8];
  step1[3] = input[24];
  step1[4] = input[4];
  step1[5] = input[20];
  step1[6] = input[12];
  step1[7] = input[28];
  step1[8] = input[2];
  step1[9] = input[18];
  step1[10] = input[10];
  step1[11] = input[26];
  step1[12] = input[6];
  step1[13] = input[22];
  step1[14] = input[14];
  step1[15] = input[30];

  temp1 = input[1] * cospi_31_64 - input[31] * cospi_1_64;
  temp2 = input[1] * cospi_1_64 + input[31] * cospi_31_64;
  step1[16] = dct_const_round_shift(temp1);
  step1[31] = dct_const_round_shift(temp2);

  temp1 = input[17] * cospi_15_64 - input[15] * cospi_17_64;
  temp2 = input[17] * cospi_17_64 + input[15] * cospi_15_64;
  step1[17] = dct_const_round_shift(temp1);
  step1[30] = dct_const_round_shift(temp2);

  temp1 = input[9] * cospi_23_64 - input[23] * cospi_9_64;
  temp2 = input[9] * cospi_9_64 + input[23] * cospi_23_64;
  step1[18] = dct_const_round_shift(temp1);
  step1[29] = dct_const_round_shift(temp2);

  temp1 = input[25] * cospi_7_64 - input[7] * cospi_25_64;
  temp2 = input[25] * cospi_25_64 + input[7] * cospi_7_64;
  step1[19] = dct_const_round_shift(temp1);
  step1[28] = dct_const_round_shift(temp2);

  temp1 = input[5] * cospi_27_64 - input[27] * cospi_5_64;
  temp2 = input[5] * cospi_5_64 + input[27] * cospi_27_64;
  step1[20] = dct_const_round_shift(temp1);
  step1[27] = dct_const_round_shift(temp2);

  temp1 = input[21] * cospi_11_64 - input[11] * cospi_21_64;
  temp2 = input[21] * cospi_21_64 + input[11] * cospi_11_64;
  step1[21] = dct_const_round_shift(temp1);
  step1[26] = dct_const_round_shift(temp2);

  temp1 = input[13] * cospi_19_64 - input[19] * cospi_13_64;
  temp2 = input[13] * cospi_13_64 + input[19] * cospi_19_64;
  step1[22] = dct_const_round_shift(temp1);
  step1[25] = dct_const_round_shift(temp2);

  temp1 = input[29] * cospi_3_64 - input[3] * cospi_29_64;
  temp2 = input[29] * cospi_29_64 + input[3] * cospi_3_64;
  step1[23] = dct_const_round_shift(temp1);
  step1[24] = dct_const_round_shift(temp2);

  // stage 2
  step2[0] = step1[0];
  step2[1] = step1[1];
  step2[2] = step1[2];
  step2[3] = step1[3];
  step2[4] = step1[4];
  step2[5] = step1[5];
  step2[6] = step1[6];
  step2[7] = step1[7];

  temp1 = step1[8] * cospi_30_64 - step1[15] * cospi_2_64;
  temp2 = step1[8] * cospi_2_64 + step1[15] * cospi_30_64;
  step2[8] = dct_const_round_shift(temp1);
  step2[15] = dct_const_round_shift(temp2);

  temp1 = step1[9] * cospi_14_64 - step1[14] * cospi_18_64;
  temp2 = step1[9] * cospi_18_64 + step1[14] * cospi_14_64;
  step2[9] = dct_const_round_shift(temp1);
  step2[14] = dct_const_round_shift(temp2);

  temp1 = step1[10] * cospi_22_64 - step1[13] * cospi_10_64;
  temp2 = step1[10] * cospi_10_64 + step1[13] * cospi_22_64;
  step2[10] = dct_const_round_shift(temp1);
  step2[13] = dct_const_round_shift(temp2);

  temp1 = step1[11] * cospi_6_64 - step1[12] * cospi_26_64;
  temp2 = step1[11] * cospi_26_64 + step1[12] * cospi_6_64;
  step2[11] = dct_const_round_shift(temp1);
  step2[12] = dct_const_round_shift(temp2);

  step2[16] = step1[16] + step1[17];
  step2[17] = step1[16] - step1[17];
  step2[18] = -step1[18] + step1[19];
  step2[19] = step1[18] + step1[19];
  step2[20] = step1[20] + step1[21];
  step2[21] = step1[20] - step1[21];
  step2[22] = -step1[22] + step1[23];
  step2[23] = step1[22] + step1[23];
  step2[24] = step1[24] + step1[25];
  step2[25] = step1[24] - step1[25];
  step2[26] = -step1[26] + step1[27];
  step2[27] = step1[26] + step1[27];
  step2[28] = step1[28] + step1[29];
  step2[29] = step1[28] - step1[29];
  step2[30] = -step1[30] + step1[31];
  step2[31] = step1[30] + step1[31];

  // stage 3
  step1[0] = step2[0];
  step1[1] = step2[1];
  step1[2] = step2[2];
  step1[3] = step2[3];

  temp1 = step2[4] * cospi_28_64 - step2[7] * cospi_4_64;
  temp2 = step2[4] * cospi_4_64 + step2[7] * cospi_28_64;
  step1[4] = dct_const_round_shift(temp1);
  step1[7] = dct_const_round_shift(temp2);
  temp1 = step2[5] * cospi_12_64 - step2[6] * cospi_20_64;
  temp2 = step2[5] * cospi_20_64 + step2[6] * cospi_12_64;
  step1[5] = dct_const_round_shift(temp1);
  step1[6] = dct_const_round_shift(temp2);

  step1[8] = step2[8] + step2[9];
  step1[9] = step2[8] - step2[9];
  step1[10] = -step2[10] + step2[11];
  step1[11] = step2[10] + step2[11];
  step1[12] = step2[12] + step2[13];
  step1[13] = step2[12] - step2[13];
  step1[14] = -step2[14] + step2[15];
  step1[15] = step2[14] + step2[15];

  step1[16] = step2[16];
  step1[31] = step2[31];
  temp1 = -step2[17] * cospi_4_64 + step2[30] * cospi_28_64;
  temp2 = step2[17] * cospi_28_64 + step2[30] * cospi_4_64;
  step1[17] = dct_const_round_shift(temp1);
  step1[30] = dct_const_round_shift(temp2);
  temp1 = -step2[18] * cospi_28_64 - step2[29] * cospi_4_64;
  temp2 = -step2[18] * cospi_4_64 + step2[29] * cospi_28_64;
  step1[18] = dct_const_round_shift(temp1);
  step1[29] = dct_const_round_shift(temp2);
  step1[19] = step2[19];
  step1[20] = step2[20];
  temp1 = -step2[21] * cospi_20_64 + step2[26] * cospi_12_64;
  temp2 = step2[21] * cospi_12_64 + step2[26] * cospi_20_64;
  step1[21] = dct_const_round_shift(temp1);
  step1[26] = dct_const_round_shift(temp2);
  temp1 = -step2[22] * cospi_12_64 - step2[25] * cospi_20_64;
  temp2 = -step2[22] * cospi_20_64 + step2[25] * cospi_12_64;
  step1[22] = dct_const_round_shift(temp1);
  step1[25] = dct_const_round_shift(temp2);
  step1[23] = step2[23];
  step1[24] = step2[24];
  step1[27] = step2[27];
  step1[28] = step2[28];

  // stage 4
  temp1 = (step1[0] + step1[1]) * cospi_16_64;
  temp2 = (step1[0] - step1[1]) * cospi_16_64;
  step2[0] = dct_const_round_shift(temp1);
  step2[1] = dct_const_round_shift(temp2);
  temp1 = step1[2] * cospi_24_64 - step1[3] * cospi_8_64;
  temp2 = step1[2] * cospi_8_64 + step1[3] * cospi_24_64;
  step2[2] = dct_const_round_shift(temp1);
  step2[3] = dct_const_round_shift(temp2);
  step2[4] = step1[4] + step1[5];
  step2[5] = step1[4] - step1[5];
  step2[6] = -step1[6] + step1[7];
  step2[7] = step1[6] + step1[7];

  step2[8] = step1[8];
  step2[15] = step1[15];
  temp1 = -step1[9] * cospi_8_64 + step1[14] * cospi_24_64;
  temp2 = step1[9] * cospi_24_64 + step1[14] * cospi_8_64;
  step2[9] = dct_const_round_shift(temp1);
  step2[14] = dct_const_round_shift(temp2);
  temp1 = -step1[10] * cospi_24_64 - step1[13] * cospi_8_64;
  temp2 = -step1[10] * cospi_8_64 + step1[13] * cospi_24_64;
  step2[10] = dct_const_round_shift(temp1);
  step2[13] = dct_const_round_shift(temp2);
  step2[11] = step1[11];
  step2[12] = step1[12];

  step2[16] = step1[16] + step1[19];
  step2[17] = step1[17] + step1[18];
  step2[18] = step1[17] - step1[18];
  step2[19] = step1[16] - step1[19];
  step2[20] = -step1[20] + step1[23];
  step2[21] = -step1[21] + step1[22];
  step2[22] = step1[21] + step1[22];
  step2[23] = step1[20] + step1[23];

  step2[24] = step1[24] + step1[27];
  step2[25] = step1[25] + step1[26];
  step2[26] = step1[25] - step1[26];
  step2[27] = step1[24] - step1[27];
  step2[28] = -step1[28] + step1[31];
  step2[29] = -step1[29] + step1[30];
  step2[30] = step1[29] + step1[30];
  step2[31] = step1[28] + step1[31];

  // stage 5
  step1[0] = step2[0] + step2[3];
  step1[1] = step2[1] + step2[2];
  step1[2] = step2[1] - step2[2];
  step1[3] = step2[0] - step2[3];
  step1[4] = step2[4];
  temp1 = (step2[6] - step2[5]) * cospi_16_64;
  temp2 = (step2[5] + step2[6]) * cospi_16_64;
  step1[5] = dct_const_round_shift(temp1);
  step1[6] = dct_const_round_shift(temp2);
  step1[7] = step2[7];

  step1[8] = step2[8] + step2[11];
  step1[9] = step2[9] + step2[10];
  step1[10] = step2[9] - step2[10];
  step1[11] = step2[8] - step2[11];
  step1[12] = -step2[12] + step2[15];
  step1[13] = -step2[13] + step2[14];
  step1[14] = step2[13] + step2[14];
  step1[15] = step2[12] + step2[15];

  step1[16] = step2[16];
  step1[17] = step2[17];
  temp1 = -step2[18] * cospi_8_64 + step2[29] * cospi_24_64;
  temp2 = step2[18] * cospi_24_64 + step2[29] * cospi_8_64;
  step1[18] = dct_const_round_shift(temp1);
  step1[29] = dct_const_round_shift(temp2);
  temp1 = -step2[19] * cospi_8_64 + step2[28] * cospi_24_64;
  temp2 = step2[19] * cospi_24_64 + step2[28] * cospi_8_64;
  step1[19] = dct_const_round_shift(temp1);
  step1[28] = dct_const_round_shift(temp2);
  temp1 = -step2[20] * cospi_24_64 - step2[27] * cospi_8_64;
  temp2 = -step2[20] * cospi_8_64 + step2[27] * cospi_24_64;
  step1[20] = dct_const_round_shift(temp1);
  step1[27] = dct_const_round_shift(temp2);
  temp1 = -step2[21] * cospi_24_64 - step2[26] * cospi_8_64;
  temp2 = -step2[21] * cospi_8_64 + step2[26] * cospi_24_64;
  step1[21] = dct_const_round_shift(temp1);
  step1[26] = dct_const_round_shift(temp2);
  step1[22] = step2[22];
  step1[23] = step2[23];
  step1[24] = step2[24];
  step1[25] = step2[25];
  step1[30] = step2[30];
  step1[31] = step2[31];

  // stage 6
  step2[0] = step1[0] + step1[7];
  step2[1] = step1[1] + step1[6];
  step2[2] = step1[2] + step1[5];
  step2[3] = step1[3] + step1[4];
  step2[4] = step1[3] - step1[4];
  step2[5] = step1[2] - step1[5];
  step2[6] = step1[1] - step1[6];
  step2[7] = step1[0] - step1[7];
  step2[8] = step1[8];
  step2[9] = step1[9];
  temp1 = (-step1[10] + step1[13]) * cospi_16_64;
  temp2 = (step1[10] + step1[13]) * cospi_16_64;
  step2[10] = dct_const_round_shift(temp1);
  step2[13] = dct_const_round_shift(temp2);
  temp1 = (-step1[11] + step1[12]) * cospi_16_64;
  temp2 = (step1[11] + step1[12]) * cospi_16_64;
  step2[11] = dct_const_round_shift(temp1);
  step2[12] = dct_const_round_shift(temp2);
  step2[14] = step1[14];
  step2[15] = step1[15];

  step2[16] = step1[16] + step1[23];
  step2[17] = step1[17] + step1[22];
  step2[18] = step1[18] + step1[21];
  step2[19] = step1[19] + step1[20];
  step2[20] = step1[19] - step1[20];
  step2[21] = step1[18] - step1[21];
  step2[22] = step1[17] - step1[22];
  step2[23] = step1[16] - step1[23];

  step2[24] = -step1[24] + step1[31];
  step2[25] = -step1[25] + step1[30];
  step2[26] = -step1[26] + step1[29];
  step2[27] = -step1[27] + step1[28];
  step2[28] = step1[27] + step1[28];
  step2[29] = step1[26] + step1[29];
  step2[30] = step1[25] + step1[30];
  step2[31] = step1[24] + step1[31];

  // stage 7
  step1[0] = step2[0] + step2[15];
  step1[1] = step2[1] + step2[14];
  step1[2] = step2[2] + step2[13];
  step1[3] = step2[3] + step2[12];
  step1[4] = step2[4] + step2[11];
  step1[5] = step2[5] + step2[10];
  step1[6] = step2[6] + step2[9];
  step1[7] = step2[7] + step2[8];
  step1[8] = step2[7] - step2[8];
  step1[9] = step2[6] - step2[9];
  step1[10] = step2[5] - step2[10];
  step1[11] = step2[4] - step2[11];
  step1[12] = step2[3] - step2[12];
  step1[13] = step2[2] - step2[13];
  step1[14] = step2[1] - step2[14];
  step1[15] = step2[0] - step2[15];

  step1[16] = step2[16];
  step1[17] = step2[17];
  step1[18] = step2[18];
  step1[19] = step2[19];
  temp1 = (-step2[20] + step2[27]) * cospi_16_64;
  temp2 = (step2[20] + step2[27]) * cospi_16_64;
  step1[20] = dct_const_round_shift(temp1);
  step1[27] = dct_const_round_shift(temp2);
  temp1 = (-step2[21] + step2[26]) * cospi_16_64;
  temp2 = (step2[21] + step2[26]) * cospi_16_64;
  step1[21] = dct_const_round_shift(temp1);
  step1[26] = dct_const_round_shift(temp2);
  temp1 = (-step2[22] + step2[25]) * cospi_16_64;
  temp2 = (step2[22] + step2[25]) * cospi_16_64;
  step1[22] = dct_const_round_shift(temp1);
  step1[25] = dct_const_round_shift(temp2);
  temp1 = (-step2[23] + step2[24]) * cospi_16_64;
  temp2 = (step2[23] + step2[24]) * cospi_16_64;
  step1[23] = dct_const_round_shift(temp1);
  step1[24] = dct_const_round_shift(temp2);
  step1[28] = step2[28];
  step1[29] = step2[29];
  step1[30] = step2[30];
  step1[31] = step2[31];

  // final stage
  output[0] = step1[0] + step1[31];
  output[1] = step1[1] + step1[30];
  output[2] = step1[2] + step1[29];
  output[3] = step1[3] + step1[28];
  output[4] = step1[4] + step1[27];
  output[5] = step1[5] + step1[26];
  output[6] = step1[6] + step1[25];
  output[7] = step1[7] + step1[24];
  output[8] = step1[8] + step1[23];
  output[9] = step1[9] + step1[22];
  output[10] = step1[10] + step1[21];
  output[11] = step1[11] + step1[20];
  output[12] = step1[12] + step1[19];
  output[13] = step1[13] + step1[18];
  output[14] = step1[14] + step1[17];
  output[15] = step1[15] + step1[16];
  output[16] = step1[15] - step1[16];
  output[17] = step1[14] - step1[17];
  output[18] = step1[13] - step1[18];
  output[19] = step1[12] - step1[19];
  output[20] = step1[11] - step1[20];
  output[21] = step1[10] - step1[21];
  output[22] = step1[9] - step1[22];
  output[23] = step1[8] - step1[23];
  output[24] = step1[7] - step1[24];
  output[25] = step1[6] - step1[25];
  output[26] = step1[5] - step1[26];
  output[27] = step1[4] - step1[27];
  output[28] = step1[3] - step1[28];
  output[29] = step1[2] - step1[29];
  output[30] = step1[1] - step1[30];
  output[31] = step1[0] - step1[31];
1280
}
1281

1282
void vp9_short_idct32x32_c(int16_t *input, int16_t *output, int pitch) {
1283
  int16_t out[32 * 32];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
1284
1285
  int16_t *outptr = out;
  const int half_pitch = pitch >> 1;
1286
1287
1288
  int i, j;
  int16_t temp_in[32], temp_out[32];

Dmitry Kovalev's avatar
Dmitry Kovalev committed
1289
  // Rows
1290
1291
  for (i = 0; i < 32; ++i) {
    idct32_1d(input, outptr);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
1292
    input += half_pitch;
1293
1294
    outptr += 32;
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
1295
1296

  // Columns
1297
1298
1299
1300
1301
  for (i = 0; i < 32; ++i) {
    for (j = 0; j < 32; ++j)
      temp_in[j] = out[j * 32 + i];
    idct32_1d(temp_in, temp_out);
    for (j = 0; j < 32; ++j)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
1302
      output[j * 32 + i] = ROUND_POWER_OF_TWO(temp_out[j], 6);
1303
1304
  }
}
1305

1306
void vp9_short_idct1_32x32_c(int16_t *input, int16_t *output) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
1307
1308
  int16_t out = dct_const_round_shift(input[0] * cospi_16_64);
  out = dct_const_round_shift(out * cospi_16_64);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
1309
  output[0] = ROUND_POWER_OF_TWO(out, 6);
1310
}