av1_inv_txfm1d.c 78.6 KB
Newer Older
Angie Chiang's avatar
Angie Chiang committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Angie Chiang's avatar
Angie Chiang committed
3
 *
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Angie Chiang's avatar
Angie Chiang committed
10
11
 */

12
#include <stdlib.h>
13
#include "aom_dsp/inv_txfm.h"
Yaowu Xu's avatar
Yaowu Xu committed
14
#include "av1/common/av1_inv_txfm1d.h"
Angie Chiang's avatar
Angie Chiang committed
15
16

int32_t range_check_value(int32_t value, int8_t bit) {
Angie Chiang's avatar
Angie Chiang committed
17
#if CONFIG_COEFFICIENT_RANGE_CHECKING
Angie Chiang's avatar
Angie Chiang committed
18
19
20
21
22
23
24
25
26
27
28
  const int64_t maxValue = (1LL << (bit - 1)) - 1;
  const int64_t minValue = -(1LL << (bit - 1));
  if (value < minValue || value > maxValue) {
    fprintf(stderr, "coeff out of bit range, value: %d bit %d\n", value, bit);
    assert(0);
  }
#else
  (void)bit;
#endif
  return value;
}
29

Angie Chiang's avatar
Angie Chiang committed
30
#if CONFIG_COEFFICIENT_RANGE_CHECKING
31
32
void range_check_func(int32_t stage, const int32_t *input, const int32_t *buf,
                      int32_t size, int8_t bit) {
33
34
  const int64_t maxValue = (1LL << (bit - 1)) - 1;
  const int64_t minValue = -(1LL << (bit - 1));
35

36
37
  int in_range = 1;

38
39
  for (int i = 0; i < size; ++i) {
    if (buf[i] < minValue || buf[i] > maxValue) {
40
41
42
43
44
45
      in_range = 0;
    }
  }

  if (!in_range) {
    fprintf(stderr, "Error: coeffs contain out-of-range values\n");
46
    fprintf(stderr, "size: %d\n", size);
47
48
49
50
51
    fprintf(stderr, "stage: %d\n", stage);
    fprintf(stderr, "allowed range: [%" PRId64 ";%" PRId64 "]\n", minValue,
            maxValue);

    fprintf(stderr, "coeffs: ");
52

53
54
55
56
    fprintf(stderr, "[");
    for (int j = 0; j < size; j++) {
      if (j > 0) fprintf(stderr, ", ");
      fprintf(stderr, "%d", input[j]);
57
    }
58
59
60
61
62
63
64
65
66
67
    fprintf(stderr, "]\n");

    fprintf(stderr, "   buf: ");

    fprintf(stderr, "[");
    for (int j = 0; j < size; j++) {
      if (j > 0) fprintf(stderr, ", ");
      fprintf(stderr, "%d", buf[j]);
    }
    fprintf(stderr, "]\n\n");
Angie Chiang's avatar
Angie Chiang committed
68
  }
69
70

  assert(in_range);
71
72
}

73
#define apply_range(stage, input, buf, size, bit) \
74
  range_check_func(stage, input, buf, size, bit)
Angie Chiang's avatar
Angie Chiang committed
75
#else
76
77
78
#define apply_range(stage, input, buf, size, bit) \
  clamp_buf((int32_t *)buf, size, bit)

79
static INLINE void clamp_buf(int32_t *buf, int32_t size, int8_t bit) {
80
  if (bit <= 16) {
81
82
    const int32_t max_value = (1 << 15) - 1;
    const int32_t min_value = -(1 << 15);
83

84
    for (int i = 0; i < size; ++i) buf[i] = clamp(buf[i], min_value, max_value);
85
  }
86
}
Angie Chiang's avatar
Angie Chiang committed
87
88
#endif

Angie Chiang's avatar
Angie Chiang committed
89
90
91
92
93
94
95
96
97
int32_t clamp_value(int32_t value, int8_t bit) {
  if (bit <= 16) {
    const int32_t maxValue = (1 << 15) - 1;
    const int32_t minValue = -(1 << 15);
    return clamp(value, minValue, maxValue);
  }
  return value;
}

98
// TODO(angiebird): Make 1-d txfm functions static
99
void av1_idct4_new(const int32_t *input, int32_t *output, int8_t cos_bit,
Yaowu Xu's avatar
Yaowu Xu committed
100
                   const int8_t *stage_range) {
Angie Chiang's avatar
Angie Chiang committed
101
102
103
104
105
106
107
108
  const int32_t size = 4;
  const int32_t *cospi;

  int32_t stage = 0;
  int32_t *bf0, *bf1;
  int32_t step[4];

  // stage 0;
109
  apply_range(stage, input, input, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
110
111
112

  // stage 1;
  stage++;
113
  assert(output != input);
Angie Chiang's avatar
Angie Chiang committed
114
115
116
117
118
  bf1 = output;
  bf1[0] = input[0];
  bf1[1] = input[2];
  bf1[2] = input[1];
  bf1[3] = input[3];
119
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
120
121
122

  // stage 2
  stage++;
123
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
124
125
  bf0 = output;
  bf1 = step;
126
127
128
129
  bf1[0] = half_btf(cospi[32], bf0[0], cospi[32], bf0[1], cos_bit);
  bf1[1] = half_btf(cospi[32], bf0[0], -cospi[32], bf0[1], cos_bit);
  bf1[2] = half_btf(cospi[48], bf0[2], -cospi[16], bf0[3], cos_bit);
  bf1[3] = half_btf(cospi[16], bf0[2], cospi[48], bf0[3], cos_bit);
130
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
131
132
133
134
135
136
137
138
139

  // stage 3
  stage++;
  bf0 = step;
  bf1 = output;
  bf1[0] = bf0[0] + bf0[3];
  bf1[1] = bf0[1] + bf0[2];
  bf1[2] = bf0[1] - bf0[2];
  bf1[3] = bf0[0] - bf0[3];
140
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
141
}
Angie Chiang's avatar
Angie Chiang committed
142

143
void av1_idct8_new(const int32_t *input, int32_t *output, int8_t cos_bit,
Yaowu Xu's avatar
Yaowu Xu committed
144
                   const int8_t *stage_range) {
Angie Chiang's avatar
Angie Chiang committed
145
146
147
148
149
150
151
152
  const int32_t size = 8;
  const int32_t *cospi;

  int32_t stage = 0;
  int32_t *bf0, *bf1;
  int32_t step[8];

  // stage 0;
153
  apply_range(stage, input, input, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
154
155
156

  // stage 1;
  stage++;
157
  assert(output != input);
Angie Chiang's avatar
Angie Chiang committed
158
159
160
161
162
163
164
165
166
  bf1 = output;
  bf1[0] = input[0];
  bf1[1] = input[4];
  bf1[2] = input[2];
  bf1[3] = input[6];
  bf1[4] = input[1];
  bf1[5] = input[5];
  bf1[6] = input[3];
  bf1[7] = input[7];
167
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
168
169
170

  // stage 2
  stage++;
171
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
172
173
174
175
176
177
  bf0 = output;
  bf1 = step;
  bf1[0] = bf0[0];
  bf1[1] = bf0[1];
  bf1[2] = bf0[2];
  bf1[3] = bf0[3];
178
179
180
181
  bf1[4] = half_btf(cospi[56], bf0[4], -cospi[8], bf0[7], cos_bit);
  bf1[5] = half_btf(cospi[24], bf0[5], -cospi[40], bf0[6], cos_bit);
  bf1[6] = half_btf(cospi[40], bf0[5], cospi[24], bf0[6], cos_bit);
  bf1[7] = half_btf(cospi[8], bf0[4], cospi[56], bf0[7], cos_bit);
182
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
183
184
185

  // stage 3
  stage++;
186
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
187
188
  bf0 = step;
  bf1 = output;
189
190
191
192
  bf1[0] = half_btf(cospi[32], bf0[0], cospi[32], bf0[1], cos_bit);
  bf1[1] = half_btf(cospi[32], bf0[0], -cospi[32], bf0[1], cos_bit);
  bf1[2] = half_btf(cospi[48], bf0[2], -cospi[16], bf0[3], cos_bit);
  bf1[3] = half_btf(cospi[16], bf0[2], cospi[48], bf0[3], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
193
194
195
196
  bf1[4] = bf0[4] + bf0[5];
  bf1[5] = bf0[4] - bf0[5];
  bf1[6] = -bf0[6] + bf0[7];
  bf1[7] = bf0[6] + bf0[7];
197
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
198
199
200

  // stage 4
  stage++;
201
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
202
203
204
205
206
207
208
  bf0 = output;
  bf1 = step;
  bf1[0] = bf0[0] + bf0[3];
  bf1[1] = bf0[1] + bf0[2];
  bf1[2] = bf0[1] - bf0[2];
  bf1[3] = bf0[0] - bf0[3];
  bf1[4] = bf0[4];
209
210
  bf1[5] = half_btf(-cospi[32], bf0[5], cospi[32], bf0[6], cos_bit);
  bf1[6] = half_btf(cospi[32], bf0[5], cospi[32], bf0[6], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
211
  bf1[7] = bf0[7];
212
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
213
214
215
216
217
218
219
220
221
222
223
224
225

  // stage 5
  stage++;
  bf0 = step;
  bf1 = output;
  bf1[0] = bf0[0] + bf0[7];
  bf1[1] = bf0[1] + bf0[6];
  bf1[2] = bf0[2] + bf0[5];
  bf1[3] = bf0[3] + bf0[4];
  bf1[4] = bf0[3] - bf0[4];
  bf1[5] = bf0[2] - bf0[5];
  bf1[6] = bf0[1] - bf0[6];
  bf1[7] = bf0[0] - bf0[7];
226
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
227
}
Angie Chiang's avatar
Angie Chiang committed
228

229
230
void av1_idct16_new(const int32_t *input, int32_t *output, int8_t cos_bit,
                    const int8_t *stage_range) {
Angie Chiang's avatar
Angie Chiang committed
231
232
233
234
235
236
237
238
  const int32_t size = 16;
  const int32_t *cospi;

  int32_t stage = 0;
  int32_t *bf0, *bf1;
  int32_t step[16];

  // stage 0;
239
  apply_range(stage, input, input, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
240
241
242

  // stage 1;
  stage++;
243
  assert(output != input);
Angie Chiang's avatar
Angie Chiang committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  bf1 = output;
  bf1[0] = input[0];
  bf1[1] = input[8];
  bf1[2] = input[4];
  bf1[3] = input[12];
  bf1[4] = input[2];
  bf1[5] = input[10];
  bf1[6] = input[6];
  bf1[7] = input[14];
  bf1[8] = input[1];
  bf1[9] = input[9];
  bf1[10] = input[5];
  bf1[11] = input[13];
  bf1[12] = input[3];
  bf1[13] = input[11];
  bf1[14] = input[7];
  bf1[15] = input[15];
261
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
262
263
264

  // stage 2
  stage++;
265
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
266
267
268
269
270
271
272
273
274
275
  bf0 = output;
  bf1 = step;
  bf1[0] = bf0[0];
  bf1[1] = bf0[1];
  bf1[2] = bf0[2];
  bf1[3] = bf0[3];
  bf1[4] = bf0[4];
  bf1[5] = bf0[5];
  bf1[6] = bf0[6];
  bf1[7] = bf0[7];
276
277
278
279
280
281
282
283
  bf1[8] = half_btf(cospi[60], bf0[8], -cospi[4], bf0[15], cos_bit);
  bf1[9] = half_btf(cospi[28], bf0[9], -cospi[36], bf0[14], cos_bit);
  bf1[10] = half_btf(cospi[44], bf0[10], -cospi[20], bf0[13], cos_bit);
  bf1[11] = half_btf(cospi[12], bf0[11], -cospi[52], bf0[12], cos_bit);
  bf1[12] = half_btf(cospi[52], bf0[11], cospi[12], bf0[12], cos_bit);
  bf1[13] = half_btf(cospi[20], bf0[10], cospi[44], bf0[13], cos_bit);
  bf1[14] = half_btf(cospi[36], bf0[9], cospi[28], bf0[14], cos_bit);
  bf1[15] = half_btf(cospi[4], bf0[8], cospi[60], bf0[15], cos_bit);
284
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
285
286
287

  // stage 3
  stage++;
288
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
289
290
291
292
293
294
  bf0 = step;
  bf1 = output;
  bf1[0] = bf0[0];
  bf1[1] = bf0[1];
  bf1[2] = bf0[2];
  bf1[3] = bf0[3];
295
296
297
298
  bf1[4] = half_btf(cospi[56], bf0[4], -cospi[8], bf0[7], cos_bit);
  bf1[5] = half_btf(cospi[24], bf0[5], -cospi[40], bf0[6], cos_bit);
  bf1[6] = half_btf(cospi[40], bf0[5], cospi[24], bf0[6], cos_bit);
  bf1[7] = half_btf(cospi[8], bf0[4], cospi[56], bf0[7], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
299
300
301
302
303
304
305
306
  bf1[8] = bf0[8] + bf0[9];
  bf1[9] = bf0[8] - bf0[9];
  bf1[10] = -bf0[10] + bf0[11];
  bf1[11] = bf0[10] + bf0[11];
  bf1[12] = bf0[12] + bf0[13];
  bf1[13] = bf0[12] - bf0[13];
  bf1[14] = -bf0[14] + bf0[15];
  bf1[15] = bf0[14] + bf0[15];
307
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
308
309
310

  // stage 4
  stage++;
311
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
312
313
  bf0 = output;
  bf1 = step;
314
315
316
317
  bf1[0] = half_btf(cospi[32], bf0[0], cospi[32], bf0[1], cos_bit);
  bf1[1] = half_btf(cospi[32], bf0[0], -cospi[32], bf0[1], cos_bit);
  bf1[2] = half_btf(cospi[48], bf0[2], -cospi[16], bf0[3], cos_bit);
  bf1[3] = half_btf(cospi[16], bf0[2], cospi[48], bf0[3], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
318
319
320
321
322
  bf1[4] = bf0[4] + bf0[5];
  bf1[5] = bf0[4] - bf0[5];
  bf1[6] = -bf0[6] + bf0[7];
  bf1[7] = bf0[6] + bf0[7];
  bf1[8] = bf0[8];
323
324
  bf1[9] = half_btf(-cospi[16], bf0[9], cospi[48], bf0[14], cos_bit);
  bf1[10] = half_btf(-cospi[48], bf0[10], -cospi[16], bf0[13], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
325
326
  bf1[11] = bf0[11];
  bf1[12] = bf0[12];
327
328
  bf1[13] = half_btf(-cospi[16], bf0[10], cospi[48], bf0[13], cos_bit);
  bf1[14] = half_btf(cospi[48], bf0[9], cospi[16], bf0[14], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
329
  bf1[15] = bf0[15];
330
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
331
332
333

  // stage 5
  stage++;
334
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
335
336
337
338
339
340
341
  bf0 = step;
  bf1 = output;
  bf1[0] = bf0[0] + bf0[3];
  bf1[1] = bf0[1] + bf0[2];
  bf1[2] = bf0[1] - bf0[2];
  bf1[3] = bf0[0] - bf0[3];
  bf1[4] = bf0[4];
342
343
  bf1[5] = half_btf(-cospi[32], bf0[5], cospi[32], bf0[6], cos_bit);
  bf1[6] = half_btf(cospi[32], bf0[5], cospi[32], bf0[6], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
344
345
346
347
348
349
350
351
352
  bf1[7] = bf0[7];
  bf1[8] = bf0[8] + bf0[11];
  bf1[9] = bf0[9] + bf0[10];
  bf1[10] = bf0[9] - bf0[10];
  bf1[11] = bf0[8] - bf0[11];
  bf1[12] = -bf0[12] + bf0[15];
  bf1[13] = -bf0[13] + bf0[14];
  bf1[14] = bf0[13] + bf0[14];
  bf1[15] = bf0[12] + bf0[15];
353
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
354
355
356

  // stage 6
  stage++;
357
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
358
359
360
361
362
363
364
365
366
367
368
369
  bf0 = output;
  bf1 = step;
  bf1[0] = bf0[0] + bf0[7];
  bf1[1] = bf0[1] + bf0[6];
  bf1[2] = bf0[2] + bf0[5];
  bf1[3] = bf0[3] + bf0[4];
  bf1[4] = bf0[3] - bf0[4];
  bf1[5] = bf0[2] - bf0[5];
  bf1[6] = bf0[1] - bf0[6];
  bf1[7] = bf0[0] - bf0[7];
  bf1[8] = bf0[8];
  bf1[9] = bf0[9];
370
371
372
373
  bf1[10] = half_btf(-cospi[32], bf0[10], cospi[32], bf0[13], cos_bit);
  bf1[11] = half_btf(-cospi[32], bf0[11], cospi[32], bf0[12], cos_bit);
  bf1[12] = half_btf(cospi[32], bf0[11], cospi[32], bf0[12], cos_bit);
  bf1[13] = half_btf(cospi[32], bf0[10], cospi[32], bf0[13], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
374
375
  bf1[14] = bf0[14];
  bf1[15] = bf0[15];
376
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397

  // stage 7
  stage++;
  bf0 = step;
  bf1 = output;
  bf1[0] = bf0[0] + bf0[15];
  bf1[1] = bf0[1] + bf0[14];
  bf1[2] = bf0[2] + bf0[13];
  bf1[3] = bf0[3] + bf0[12];
  bf1[4] = bf0[4] + bf0[11];
  bf1[5] = bf0[5] + bf0[10];
  bf1[6] = bf0[6] + bf0[9];
  bf1[7] = bf0[7] + bf0[8];
  bf1[8] = bf0[7] - bf0[8];
  bf1[9] = bf0[6] - bf0[9];
  bf1[10] = bf0[5] - bf0[10];
  bf1[11] = bf0[4] - bf0[11];
  bf1[12] = bf0[3] - bf0[12];
  bf1[13] = bf0[2] - bf0[13];
  bf1[14] = bf0[1] - bf0[14];
  bf1[15] = bf0[0] - bf0[15];
398
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
399
}
Angie Chiang's avatar
Angie Chiang committed
400

401
402
void av1_idct32_new(const int32_t *input, int32_t *output, int8_t cos_bit,
                    const int8_t *stage_range) {
Angie Chiang's avatar
Angie Chiang committed
403
404
405
406
407
408
409
410
  const int32_t size = 32;
  const int32_t *cospi;

  int32_t stage = 0;
  int32_t *bf0, *bf1;
  int32_t step[32];

  // stage 0;
411
  apply_range(stage, input, input, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
412
413
414

  // stage 1;
  stage++;
415
  assert(output != input);
Angie Chiang's avatar
Angie Chiang committed
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
  bf1 = output;
  bf1[0] = input[0];
  bf1[1] = input[16];
  bf1[2] = input[8];
  bf1[3] = input[24];
  bf1[4] = input[4];
  bf1[5] = input[20];
  bf1[6] = input[12];
  bf1[7] = input[28];
  bf1[8] = input[2];
  bf1[9] = input[18];
  bf1[10] = input[10];
  bf1[11] = input[26];
  bf1[12] = input[6];
  bf1[13] = input[22];
  bf1[14] = input[14];
  bf1[15] = input[30];
  bf1[16] = input[1];
  bf1[17] = input[17];
  bf1[18] = input[9];
  bf1[19] = input[25];
  bf1[20] = input[5];
  bf1[21] = input[21];
  bf1[22] = input[13];
  bf1[23] = input[29];
  bf1[24] = input[3];
  bf1[25] = input[19];
  bf1[26] = input[11];
  bf1[27] = input[27];
  bf1[28] = input[7];
  bf1[29] = input[23];
  bf1[30] = input[15];
  bf1[31] = input[31];
449
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
450
451
452

  // stage 2
  stage++;
453
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
  bf0 = output;
  bf1 = step;
  bf1[0] = bf0[0];
  bf1[1] = bf0[1];
  bf1[2] = bf0[2];
  bf1[3] = bf0[3];
  bf1[4] = bf0[4];
  bf1[5] = bf0[5];
  bf1[6] = bf0[6];
  bf1[7] = bf0[7];
  bf1[8] = bf0[8];
  bf1[9] = bf0[9];
  bf1[10] = bf0[10];
  bf1[11] = bf0[11];
  bf1[12] = bf0[12];
  bf1[13] = bf0[13];
  bf1[14] = bf0[14];
  bf1[15] = bf0[15];
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
  bf1[16] = half_btf(cospi[62], bf0[16], -cospi[2], bf0[31], cos_bit);
  bf1[17] = half_btf(cospi[30], bf0[17], -cospi[34], bf0[30], cos_bit);
  bf1[18] = half_btf(cospi[46], bf0[18], -cospi[18], bf0[29], cos_bit);
  bf1[19] = half_btf(cospi[14], bf0[19], -cospi[50], bf0[28], cos_bit);
  bf1[20] = half_btf(cospi[54], bf0[20], -cospi[10], bf0[27], cos_bit);
  bf1[21] = half_btf(cospi[22], bf0[21], -cospi[42], bf0[26], cos_bit);
  bf1[22] = half_btf(cospi[38], bf0[22], -cospi[26], bf0[25], cos_bit);
  bf1[23] = half_btf(cospi[6], bf0[23], -cospi[58], bf0[24], cos_bit);
  bf1[24] = half_btf(cospi[58], bf0[23], cospi[6], bf0[24], cos_bit);
  bf1[25] = half_btf(cospi[26], bf0[22], cospi[38], bf0[25], cos_bit);
  bf1[26] = half_btf(cospi[42], bf0[21], cospi[22], bf0[26], cos_bit);
  bf1[27] = half_btf(cospi[10], bf0[20], cospi[54], bf0[27], cos_bit);
  bf1[28] = half_btf(cospi[50], bf0[19], cospi[14], bf0[28], cos_bit);
  bf1[29] = half_btf(cospi[18], bf0[18], cospi[46], bf0[29], cos_bit);
  bf1[30] = half_btf(cospi[34], bf0[17], cospi[30], bf0[30], cos_bit);
  bf1[31] = half_btf(cospi[2], bf0[16], cospi[62], bf0[31], cos_bit);
488
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
489
490
491

  // stage 3
  stage++;
492
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
493
494
495
496
497
498
499
500
501
502
  bf0 = step;
  bf1 = output;
  bf1[0] = bf0[0];
  bf1[1] = bf0[1];
  bf1[2] = bf0[2];
  bf1[3] = bf0[3];
  bf1[4] = bf0[4];
  bf1[5] = bf0[5];
  bf1[6] = bf0[6];
  bf1[7] = bf0[7];
503
504
505
506
507
508
509
510
  bf1[8] = half_btf(cospi[60], bf0[8], -cospi[4], bf0[15], cos_bit);
  bf1[9] = half_btf(cospi[28], bf0[9], -cospi[36], bf0[14], cos_bit);
  bf1[10] = half_btf(cospi[44], bf0[10], -cospi[20], bf0[13], cos_bit);
  bf1[11] = half_btf(cospi[12], bf0[11], -cospi[52], bf0[12], cos_bit);
  bf1[12] = half_btf(cospi[52], bf0[11], cospi[12], bf0[12], cos_bit);
  bf1[13] = half_btf(cospi[20], bf0[10], cospi[44], bf0[13], cos_bit);
  bf1[14] = half_btf(cospi[36], bf0[9], cospi[28], bf0[14], cos_bit);
  bf1[15] = half_btf(cospi[4], bf0[8], cospi[60], bf0[15], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
  bf1[16] = bf0[16] + bf0[17];
  bf1[17] = bf0[16] - bf0[17];
  bf1[18] = -bf0[18] + bf0[19];
  bf1[19] = bf0[18] + bf0[19];
  bf1[20] = bf0[20] + bf0[21];
  bf1[21] = bf0[20] - bf0[21];
  bf1[22] = -bf0[22] + bf0[23];
  bf1[23] = bf0[22] + bf0[23];
  bf1[24] = bf0[24] + bf0[25];
  bf1[25] = bf0[24] - bf0[25];
  bf1[26] = -bf0[26] + bf0[27];
  bf1[27] = bf0[26] + bf0[27];
  bf1[28] = bf0[28] + bf0[29];
  bf1[29] = bf0[28] - bf0[29];
  bf1[30] = -bf0[30] + bf0[31];
  bf1[31] = bf0[30] + bf0[31];
527
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
528
529
530

  // stage 4
  stage++;
531
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
532
533
534
535
536
537
  bf0 = output;
  bf1 = step;
  bf1[0] = bf0[0];
  bf1[1] = bf0[1];
  bf1[2] = bf0[2];
  bf1[3] = bf0[3];
538
539
540
541
  bf1[4] = half_btf(cospi[56], bf0[4], -cospi[8], bf0[7], cos_bit);
  bf1[5] = half_btf(cospi[24], bf0[5], -cospi[40], bf0[6], cos_bit);
  bf1[6] = half_btf(cospi[40], bf0[5], cospi[24], bf0[6], cos_bit);
  bf1[7] = half_btf(cospi[8], bf0[4], cospi[56], bf0[7], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
542
543
544
545
546
547
548
549
550
  bf1[8] = bf0[8] + bf0[9];
  bf1[9] = bf0[8] - bf0[9];
  bf1[10] = -bf0[10] + bf0[11];
  bf1[11] = bf0[10] + bf0[11];
  bf1[12] = bf0[12] + bf0[13];
  bf1[13] = bf0[12] - bf0[13];
  bf1[14] = -bf0[14] + bf0[15];
  bf1[15] = bf0[14] + bf0[15];
  bf1[16] = bf0[16];
551
552
  bf1[17] = half_btf(-cospi[8], bf0[17], cospi[56], bf0[30], cos_bit);
  bf1[18] = half_btf(-cospi[56], bf0[18], -cospi[8], bf0[29], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
553
554
  bf1[19] = bf0[19];
  bf1[20] = bf0[20];
555
556
  bf1[21] = half_btf(-cospi[40], bf0[21], cospi[24], bf0[26], cos_bit);
  bf1[22] = half_btf(-cospi[24], bf0[22], -cospi[40], bf0[25], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
557
558
  bf1[23] = bf0[23];
  bf1[24] = bf0[24];
559
560
  bf1[25] = half_btf(-cospi[40], bf0[22], cospi[24], bf0[25], cos_bit);
  bf1[26] = half_btf(cospi[24], bf0[21], cospi[40], bf0[26], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
561
562
  bf1[27] = bf0[27];
  bf1[28] = bf0[28];
563
564
  bf1[29] = half_btf(-cospi[8], bf0[18], cospi[56], bf0[29], cos_bit);
  bf1[30] = half_btf(cospi[56], bf0[17], cospi[8], bf0[30], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
565
  bf1[31] = bf0[31];
566
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
567
568
569

  // stage 5
  stage++;
570
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
571
572
  bf0 = step;
  bf1 = output;
573
574
575
576
  bf1[0] = half_btf(cospi[32], bf0[0], cospi[32], bf0[1], cos_bit);
  bf1[1] = half_btf(cospi[32], bf0[0], -cospi[32], bf0[1], cos_bit);
  bf1[2] = half_btf(cospi[48], bf0[2], -cospi[16], bf0[3], cos_bit);
  bf1[3] = half_btf(cospi[16], bf0[2], cospi[48], bf0[3], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
577
578
579
580
581
  bf1[4] = bf0[4] + bf0[5];
  bf1[5] = bf0[4] - bf0[5];
  bf1[6] = -bf0[6] + bf0[7];
  bf1[7] = bf0[6] + bf0[7];
  bf1[8] = bf0[8];
582
583
  bf1[9] = half_btf(-cospi[16], bf0[9], cospi[48], bf0[14], cos_bit);
  bf1[10] = half_btf(-cospi[48], bf0[10], -cospi[16], bf0[13], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
584
585
  bf1[11] = bf0[11];
  bf1[12] = bf0[12];
586
587
  bf1[13] = half_btf(-cospi[16], bf0[10], cospi[48], bf0[13], cos_bit);
  bf1[14] = half_btf(cospi[48], bf0[9], cospi[16], bf0[14], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
  bf1[15] = bf0[15];
  bf1[16] = bf0[16] + bf0[19];
  bf1[17] = bf0[17] + bf0[18];
  bf1[18] = bf0[17] - bf0[18];
  bf1[19] = bf0[16] - bf0[19];
  bf1[20] = -bf0[20] + bf0[23];
  bf1[21] = -bf0[21] + bf0[22];
  bf1[22] = bf0[21] + bf0[22];
  bf1[23] = bf0[20] + bf0[23];
  bf1[24] = bf0[24] + bf0[27];
  bf1[25] = bf0[25] + bf0[26];
  bf1[26] = bf0[25] - bf0[26];
  bf1[27] = bf0[24] - bf0[27];
  bf1[28] = -bf0[28] + bf0[31];
  bf1[29] = -bf0[29] + bf0[30];
  bf1[30] = bf0[29] + bf0[30];
  bf1[31] = bf0[28] + bf0[31];
605
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
606
607
608

  // stage 6
  stage++;
609
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
610
611
612
613
614
615
616
  bf0 = output;
  bf1 = step;
  bf1[0] = bf0[0] + bf0[3];
  bf1[1] = bf0[1] + bf0[2];
  bf1[2] = bf0[1] - bf0[2];
  bf1[3] = bf0[0] - bf0[3];
  bf1[4] = bf0[4];
617
618
  bf1[5] = half_btf(-cospi[32], bf0[5], cospi[32], bf0[6], cos_bit);
  bf1[6] = half_btf(cospi[32], bf0[5], cospi[32], bf0[6], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
619
620
621
622
623
624
625
626
627
628
629
  bf1[7] = bf0[7];
  bf1[8] = bf0[8] + bf0[11];
  bf1[9] = bf0[9] + bf0[10];
  bf1[10] = bf0[9] - bf0[10];
  bf1[11] = bf0[8] - bf0[11];
  bf1[12] = -bf0[12] + bf0[15];
  bf1[13] = -bf0[13] + bf0[14];
  bf1[14] = bf0[13] + bf0[14];
  bf1[15] = bf0[12] + bf0[15];
  bf1[16] = bf0[16];
  bf1[17] = bf0[17];
630
631
632
633
  bf1[18] = half_btf(-cospi[16], bf0[18], cospi[48], bf0[29], cos_bit);
  bf1[19] = half_btf(-cospi[16], bf0[19], cospi[48], bf0[28], cos_bit);
  bf1[20] = half_btf(-cospi[48], bf0[20], -cospi[16], bf0[27], cos_bit);
  bf1[21] = half_btf(-cospi[48], bf0[21], -cospi[16], bf0[26], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
634
635
636
637
  bf1[22] = bf0[22];
  bf1[23] = bf0[23];
  bf1[24] = bf0[24];
  bf1[25] = bf0[25];
638
639
640
641
  bf1[26] = half_btf(-cospi[16], bf0[21], cospi[48], bf0[26], cos_bit);
  bf1[27] = half_btf(-cospi[16], bf0[20], cospi[48], bf0[27], cos_bit);
  bf1[28] = half_btf(cospi[48], bf0[19], cospi[16], bf0[28], cos_bit);
  bf1[29] = half_btf(cospi[48], bf0[18], cospi[16], bf0[29], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
642
643
  bf1[30] = bf0[30];
  bf1[31] = bf0[31];
644
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
645
646
647

  // stage 7
  stage++;
648
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
649
650
651
652
653
654
655
656
657
658
659
660
  bf0 = step;
  bf1 = output;
  bf1[0] = bf0[0] + bf0[7];
  bf1[1] = bf0[1] + bf0[6];
  bf1[2] = bf0[2] + bf0[5];
  bf1[3] = bf0[3] + bf0[4];
  bf1[4] = bf0[3] - bf0[4];
  bf1[5] = bf0[2] - bf0[5];
  bf1[6] = bf0[1] - bf0[6];
  bf1[7] = bf0[0] - bf0[7];
  bf1[8] = bf0[8];
  bf1[9] = bf0[9];
661
662
663
664
  bf1[10] = half_btf(-cospi[32], bf0[10], cospi[32], bf0[13], cos_bit);
  bf1[11] = half_btf(-cospi[32], bf0[11], cospi[32], bf0[12], cos_bit);
  bf1[12] = half_btf(cospi[32], bf0[11], cospi[32], bf0[12], cos_bit);
  bf1[13] = half_btf(cospi[32], bf0[10], cospi[32], bf0[13], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
  bf1[14] = bf0[14];
  bf1[15] = bf0[15];
  bf1[16] = bf0[16] + bf0[23];
  bf1[17] = bf0[17] + bf0[22];
  bf1[18] = bf0[18] + bf0[21];
  bf1[19] = bf0[19] + bf0[20];
  bf1[20] = bf0[19] - bf0[20];
  bf1[21] = bf0[18] - bf0[21];
  bf1[22] = bf0[17] - bf0[22];
  bf1[23] = bf0[16] - bf0[23];
  bf1[24] = -bf0[24] + bf0[31];
  bf1[25] = -bf0[25] + bf0[30];
  bf1[26] = -bf0[26] + bf0[29];
  bf1[27] = -bf0[27] + bf0[28];
  bf1[28] = bf0[27] + bf0[28];
  bf1[29] = bf0[26] + bf0[29];
  bf1[30] = bf0[25] + bf0[30];
  bf1[31] = bf0[24] + bf0[31];
683
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
684
685
686

  // stage 8
  stage++;
687
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
  bf0 = output;
  bf1 = step;
  bf1[0] = bf0[0] + bf0[15];
  bf1[1] = bf0[1] + bf0[14];
  bf1[2] = bf0[2] + bf0[13];
  bf1[3] = bf0[3] + bf0[12];
  bf1[4] = bf0[4] + bf0[11];
  bf1[5] = bf0[5] + bf0[10];
  bf1[6] = bf0[6] + bf0[9];
  bf1[7] = bf0[7] + bf0[8];
  bf1[8] = bf0[7] - bf0[8];
  bf1[9] = bf0[6] - bf0[9];
  bf1[10] = bf0[5] - bf0[10];
  bf1[11] = bf0[4] - bf0[11];
  bf1[12] = bf0[3] - bf0[12];
  bf1[13] = bf0[2] - bf0[13];
  bf1[14] = bf0[1] - bf0[14];
  bf1[15] = bf0[0] - bf0[15];
  bf1[16] = bf0[16];
  bf1[17] = bf0[17];
  bf1[18] = bf0[18];
  bf1[19] = bf0[19];
710
711
712
713
714
715
716
717
  bf1[20] = half_btf(-cospi[32], bf0[20], cospi[32], bf0[27], cos_bit);
  bf1[21] = half_btf(-cospi[32], bf0[21], cospi[32], bf0[26], cos_bit);
  bf1[22] = half_btf(-cospi[32], bf0[22], cospi[32], bf0[25], cos_bit);
  bf1[23] = half_btf(-cospi[32], bf0[23], cospi[32], bf0[24], cos_bit);
  bf1[24] = half_btf(cospi[32], bf0[23], cospi[32], bf0[24], cos_bit);
  bf1[25] = half_btf(cospi[32], bf0[22], cospi[32], bf0[25], cos_bit);
  bf1[26] = half_btf(cospi[32], bf0[21], cospi[32], bf0[26], cos_bit);
  bf1[27] = half_btf(cospi[32], bf0[20], cospi[32], bf0[27], cos_bit);
Angie Chiang's avatar
Angie Chiang committed
718
719
720
721
  bf1[28] = bf0[28];
  bf1[29] = bf0[29];
  bf1[30] = bf0[30];
  bf1[31] = bf0[31];
722
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
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

  // stage 9
  stage++;
  bf0 = step;
  bf1 = output;
  bf1[0] = bf0[0] + bf0[31];
  bf1[1] = bf0[1] + bf0[30];
  bf1[2] = bf0[2] + bf0[29];
  bf1[3] = bf0[3] + bf0[28];
  bf1[4] = bf0[4] + bf0[27];
  bf1[5] = bf0[5] + bf0[26];
  bf1[6] = bf0[6] + bf0[25];
  bf1[7] = bf0[7] + bf0[24];
  bf1[8] = bf0[8] + bf0[23];
  bf1[9] = bf0[9] + bf0[22];
  bf1[10] = bf0[10] + bf0[21];
  bf1[11] = bf0[11] + bf0[20];
  bf1[12] = bf0[12] + bf0[19];
  bf1[13] = bf0[13] + bf0[18];
  bf1[14] = bf0[14] + bf0[17];
  bf1[15] = bf0[15] + bf0[16];
  bf1[16] = bf0[15] - bf0[16];
  bf1[17] = bf0[14] - bf0[17];
  bf1[18] = bf0[13] - bf0[18];
  bf1[19] = bf0[12] - bf0[19];
  bf1[20] = bf0[11] - bf0[20];
  bf1[21] = bf0[10] - bf0[21];
  bf1[22] = bf0[9] - bf0[22];
  bf1[23] = bf0[8] - bf0[23];
  bf1[24] = bf0[7] - bf0[24];
  bf1[25] = bf0[6] - bf0[25];
  bf1[26] = bf0[5] - bf0[26];
  bf1[27] = bf0[4] - bf0[27];
  bf1[28] = bf0[3] - bf0[28];
  bf1[29] = bf0[2] - bf0[29];
  bf1[30] = bf0[1] - bf0[30];
  bf1[31] = bf0[0] - bf0[31];
760
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
761
}
Angie Chiang's avatar
Angie Chiang committed
762

763
764
void av1_iadst4_new(const int32_t *input, int32_t *output, int8_t cos_bit,
                    const int8_t *stage_range) {
Yaowu Xu's avatar
Yaowu Xu committed
765
766
  const int32_t size = 4;
  int32_t stage = 0;
767
  int bit = cos_bit;
768
769
  const int32_t *sinpi = sinpi_arr(bit);
  int32_t s0, s1, s2, s3, s4, s5, s6, s7;
Yaowu Xu's avatar
Yaowu Xu committed
770
  // stage 0;
771
  apply_range(stage, input, input, size, stage_range[stage]);
772
773
774
775
776
777
778
779
780
781

  int32_t x0 = input[0];
  int32_t x1 = input[1];
  int32_t x2 = input[2];
  int32_t x3 = input[3];

  if (!(x0 | x1 | x2 | x3)) {
    output[0] = output[1] = output[2] = output[3] = 0;
    return;
  }
Angie Chiang's avatar
Angie Chiang committed
782

Angie Chiang's avatar
Angie Chiang committed
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
  // stage 1
  s0 = range_check_value(sinpi[1] * x0, stage_range[1]);
  s1 = range_check_value(sinpi[2] * x0, stage_range[1]);
  s2 = range_check_value(sinpi[3] * x1, stage_range[1]);
  s3 = range_check_value(sinpi[4] * x2, stage_range[1]);
  s4 = range_check_value(sinpi[1] * x2, stage_range[1]);
  s5 = range_check_value(sinpi[2] * x3, stage_range[1]);
  s6 = range_check_value(sinpi[4] * x3, stage_range[1]);
  s7 = clamp_value(x0 - x2, stage_range[1]);

  // stage 2
  s7 = clamp_value(s7 + x3, stage_range[2]);

  // stage 3
  s0 = range_check_value(s0 + s3, stage_range[3] + bit);
  s1 = range_check_value(s1 - s4, stage_range[3] + bit);
  s3 = range_check_value(s2, stage_range[3] + bit);
  s2 = range_check_value(sinpi[3] * s7, stage_range[3] + bit);
801

Angie Chiang's avatar
Angie Chiang committed
802
803
804
805
806
807
808
809
810
811
812
813
  // stage 4
  s0 = range_check_value(s0 + s5, stage_range[4] + bit);
  s1 = range_check_value(s1 - s6, stage_range[4] + bit);

  // stage 5
  x0 = range_check_value(s0 + s3, stage_range[5] + bit);
  x1 = range_check_value(s1 + s3, stage_range[5] + bit);
  x2 = range_check_value(s2, stage_range[5] + bit);
  x3 = range_check_value(s0 + s1, stage_range[5] + bit);

  // stage 6
  x3 = range_check_value(x3 - s3, stage_range[6] + bit);
814
815
816
817
818

  // 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.
Angie Chiang's avatar
Angie Chiang committed
819
820
821
822
823
  output[0] = round_shift(x0, bit);
  output[1] = round_shift(x1, bit);
  output[2] = round_shift(x2, bit);
  output[3] = round_shift(x3, bit);
  apply_range(6, input, output, size, stage_range[6]);
Angie Chiang's avatar
Angie Chiang committed
824
}
Angie Chiang's avatar
Angie Chiang committed
825

826
827
void av1_iadst8_new(const int32_t *input, int32_t *output, int8_t cos_bit,
                    const int8_t *stage_range) {
Angie Chiang's avatar
Angie Chiang committed
828
829
830
831
832
833
834
835
  const int32_t size = 8;
  const int32_t *cospi;

  int32_t stage = 0;
  int32_t *bf0, *bf1;
  int32_t step[8];

  // stage 0;
836
  apply_range(stage, input, input, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
837
838
839
840

  // stage 1;
  stage++;
  bf1 = output;
841
842
843
844
845
846
847
848
  bf1[0] = input[7];
  bf1[1] = input[0];
  bf1[2] = input[5];
  bf1[3] = input[2];
  bf1[4] = input[3];
  bf1[5] = input[4];
  bf1[6] = input[1];
  bf1[7] = input[6];
849
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
850
851
852

  // stage 2
  stage++;
853
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
854
855
  bf0 = output;
  bf1 = step;
856
857
858
859
860
861
862
863
  bf1[0] = half_btf(cospi[4], bf0[0], cospi[60], bf0[1], cos_bit);
  bf1[1] = half_btf(-cospi[4], bf0[1], cospi[60], bf0[0], cos_bit);
  bf1[2] = half_btf(cospi[20], bf0[2], cospi[44], bf0[3], cos_bit);
  bf1[3] = half_btf(-cospi[20], bf0[3], cospi[44], bf0[2], cos_bit);
  bf1[4] = half_btf(cospi[36], bf0[4], cospi[28], bf0[5], cos_bit);
  bf1[5] = half_btf(-cospi[36], bf0[5], cospi[28], bf0[4], cos_bit);
  bf1[6] = half_btf(cospi[52], bf0[6], cospi[12], bf0[7], cos_bit);
  bf1[7] = half_btf(-cospi[52], bf0[7], cospi[12], bf0[6], cos_bit);
864
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
865
866
867
868
869

  // stage 3
  stage++;
  bf0 = step;
  bf1 = output;
870
871
872
873
874
875
876
877
  bf1[0] = bf0[0] + bf0[4];
  bf1[1] = bf0[1] + bf0[5];
  bf1[2] = bf0[2] + bf0[6];
  bf1[3] = bf0[3] + bf0[7];
  bf1[4] = -bf0[4] + bf0[0];
  bf1[5] = -bf0[5] + bf0[1];
  bf1[6] = -bf0[6] + bf0[2];
  bf1[7] = -bf0[7] + bf0[3];
878
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
879
880
881

  // stage 4
  stage++;
882
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
883
884
885
886
887
888
  bf0 = output;
  bf1 = step;
  bf1[0] = bf0[0];
  bf1[1] = bf0[1];
  bf1[2] = bf0[2];
  bf1[3] = bf0[3];
889
890
891
892
  bf1[4] = half_btf(cospi[16], bf0[4], cospi[48], bf0[5], cos_bit);
  bf1[5] = half_btf(-cospi[16], bf0[5], cospi[48], bf0[4], cos_bit);
  bf1[6] = half_btf(-cospi[48], bf0[6], cospi[16], bf0[7], cos_bit);
  bf1[7] = half_btf(cospi[48], bf0[7], cospi[16], bf0[6], cos_bit);
893
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
894
895
896
897
898

  // stage 5
  stage++;
  bf0 = step;
  bf1 = output;
899
900
901
902
903
904
905
906
  bf1[0] = bf0[0] + bf0[2];
  bf1[1] = bf0[1] + bf0[3];
  bf1[2] = -bf0[2] + bf0[0];
  bf1[3] = -bf0[3] + bf0[1];
  bf1[4] = bf0[4] + bf0[6];
  bf1[5] = bf0[5] + bf0[7];
  bf1[6] = -bf0[6] + bf0[4];
  bf1[7] = -bf0[7] + bf0[5];
907
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
908
909
910

  // stage 6
  stage++;
911
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
912
913
  bf0 = output;
  bf1 = step;
914
915
  bf1[0] = bf0[0];
  bf1[1] = bf0[1];
916
917
  bf1[2] = half_btf(cospi[32], bf0[2], cospi[32], bf0[3], cos_bit);
  bf1[3] = half_btf(-cospi[32], bf0[3], cospi[32], bf0[2], cos_bit);
918
919
  bf1[4] = bf0[4];
  bf1[5] = bf0[5];
920
921
  bf1[6] = half_btf(cospi[32], bf0[6], cospi[32], bf0[7], cos_bit);
  bf1[7] = half_btf(-cospi[32], bf0[7], cospi[32], bf0[6], cos_bit);
922
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
923
924
925
926
927

  // stage 7
  stage++;
  bf0 = step;
  bf1 = output;
928
929
930
931
932
933
934
935
  bf1[0] = bf0[0];
  bf1[1] = -bf0[4];
  bf1[2] = bf0[6];
  bf1[3] = -bf0[2];
  bf1[4] = bf0[3];
  bf1[5] = -bf0[7];
  bf1[6] = bf0[5];
  bf1[7] = -bf0[1];
936
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
937
}
Angie Chiang's avatar
Angie Chiang committed
938

939
940
void av1_iadst16_new(const int32_t *input, int32_t *output, int8_t cos_bit,
                     const int8_t *stage_range) {
Angie Chiang's avatar
Angie Chiang committed
941
942
943
944
945
946
947
948
  const int32_t size = 16;
  const int32_t *cospi;

  int32_t stage = 0;
  int32_t *bf0, *bf1;
  int32_t step[16];

  // stage 0;
949
  apply_range(stage, input, input, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
950
951
952
953

  // stage 1;
  stage++;
  bf1 = output;
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
  bf1[0] = input[15];
  bf1[1] = input[0];
  bf1[2] = input[13];
  bf1[3] = input[2];
  bf1[4] = input[11];
  bf1[5] = input[4];
  bf1[6] = input[9];
  bf1[7] = input[6];
  bf1[8] = input[7];
  bf1[9] = input[8];
  bf1[10] = input[5];
  bf1[11] = input[10];
  bf1[12] = input[3];
  bf1[13] = input[12];
  bf1[14] = input[1];
  bf1[15] = input[14];
970
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
971
972
973

  // stage 2
  stage++;
974
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
975
976
  bf0 = output;
  bf1 = step;
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
  bf1[0] = half_btf(cospi[2], bf0[0], cospi[62], bf0[1], cos_bit);
  bf1[1] = half_btf(-cospi[2], bf0[1], cospi[62], bf0[0], cos_bit);
  bf1[2] = half_btf(cospi[10], bf0[2], cospi[54], bf0[3], cos_bit);
  bf1[3] = half_btf(-cospi[10], bf0[3], cospi[54], bf0[2], cos_bit);
  bf1[4] = half_btf(cospi[18], bf0[4], cospi[46], bf0[5], cos_bit);
  bf1[5] = half_btf(-cospi[18], bf0[5], cospi[46], bf0[4], cos_bit);
  bf1[6] = half_btf(cospi[26], bf0[6], cospi[38], bf0[7], cos_bit);
  bf1[7] = half_btf(-cospi[26], bf0[7], cospi[38], bf0[6], cos_bit);
  bf1[8] = half_btf(cospi[34], bf0[8], cospi[30], bf0[9], cos_bit);
  bf1[9] = half_btf(-cospi[34], bf0[9], cospi[30], bf0[8], cos_bit);
  bf1[10] = half_btf(cospi[42], bf0[10], cospi[22], bf0[11], cos_bit);
  bf1[11] = half_btf(-cospi[42], bf0[11], cospi[22], bf0[10], cos_bit);
  bf1[12] = half_btf(cospi[50], bf0[12], cospi[14], bf0[13], cos_bit);
  bf1[13] = half_btf(-cospi[50], bf0[13], cospi[14], bf0[12], cos_bit);
  bf1[14] = half_btf(cospi[58], bf0[14], cospi[6], bf0[15], cos_bit);
  bf1[15] = half_btf(-cospi[58], bf0[15], cospi[6], bf0[14], cos_bit);
993
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
994
995
996
997
998

  // stage 3
  stage++;
  bf0 = step;
  bf1 = output;
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
  bf1[0] = bf0[0] + bf0[8];
  bf1[1] = bf0[1] + bf0[9];
  bf1[2] = bf0[2] + bf0[10];
  bf1[3] = bf0[3] + bf0[11];
  bf1[4] = bf0[4] + bf0[12];
  bf1[5] = bf0[5] + bf0[13];
  bf1[6] = bf0[6] + bf0[14];
  bf1[7] = bf0[7] + bf0[15];
  bf1[8] = -bf0[8] + bf0[0];
  bf1[9] = -bf0[9] + bf0[1];
  bf1[10] = -bf0[10] + bf0[2];
  bf1[11] = -bf0[11] + bf0[3];
  bf1[12] = -bf0[12] + bf0[4];
  bf1[13] = -bf0[13] + bf0[5];
  bf1[14] = -bf0[14] + bf0[6];
  bf1[15] = -bf0[15] + bf0[7];
1015
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
1016
1017
1018

  // stage 4
  stage++;
1019
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
1020
1021
1022
1023
1024
1025
  bf0 = output;
  bf1 = step;
  bf1[0] = bf0[0];
  bf1[1] = bf0[1];
  bf1[2] = bf0[2];
  bf1[3] = bf0[3];
1026
1027
1028
1029
  bf1[4] = bf0[4];
  bf1[5] = bf0[5];
  bf1[6] = bf0[6];
  bf1[7] = bf0[7];
1030
1031
1032
1033
1034
1035
1036
1037
  bf1[8] = half_btf(cospi[8], bf0[8], cospi[56], bf0[9], cos_bit);
  bf1[9] = half_btf(-cospi[8], bf0[9], cospi[56], bf0[8], cos_bit);
  bf1[10] = half_btf(cospi[40], bf0[10], cospi[24], bf0[11], cos_bit);
  bf1[11] = half_btf(-cospi[40], bf0[11], cospi[24], bf0[10], cos_bit);
  bf1[12] = half_btf(-cospi[56], bf0[12], cospi[8], bf0[13], cos_bit);
  bf1[13] = half_btf(cospi[56], bf0[13], cospi[8], bf0[12], cos_bit);
  bf1[14] = half_btf(-cospi[24], bf0[14], cospi[40], bf0[15], cos_bit);
  bf1[15] = half_btf(cospi[24], bf0[15], cospi[40], bf0[14], cos_bit);
1038
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
1039
1040
1041
1042
1043
1044
1045
1046
1047

  // stage 5
  stage++;
  bf0 = step;
  bf1 = output;
  bf1[0] = bf0[0] + bf0[4];
  bf1[1] = bf0[1] + bf0[5];
  bf1[2] = bf0[2] + bf0[6];
  bf1[3] = bf0[3] + bf0[7];
1048
1049
1050
1051
  bf1[4] = -bf0[4] + bf0[0];
  bf1[5] = -bf0[5] + bf0[1];
  bf1[6] = -bf0[6] + bf0[2];
  bf1[7] = -bf0[7] + bf0[3];
Angie Chiang's avatar
Angie Chiang committed
1052
1053
1054
1055
  bf1[8] = bf0[8] + bf0[12];
  bf1[9] = bf0[9] + bf0[13];
  bf1[10] = bf0[10] + bf0[14];
  bf1[11] = bf0[11] + bf0[15];
1056
1057
1058
1059
  bf1[12] = -bf0[12] + bf0[8];
  bf1[13] = -bf0[13] + bf0[9];
  bf1[14] = -bf0[14] + bf0[10];
  bf1[15] = -bf0[15] + bf0[11];
1060
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
1061
1062
1063

  // stage 6
  stage++;
1064
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
1065
1066
1067
1068
1069
1070
  bf0 = output;
  bf1 = step;
  bf1[0] = bf0[0];
  bf1[1] = bf0[1];
  bf1[2] = bf0[2];
  bf1[3] = bf0[3];
1071
1072
1073
1074
  bf1[4] = half_btf(cospi[16], bf0[4], cospi[48], bf0[5], cos_bit);
  bf1[5] = half_btf(-cospi[16], bf0[5], cospi[48], bf0[4], cos_bit);
  bf1[6] = half_btf(-cospi[48], bf0[6], cospi[16], bf0[7], cos_bit);
  bf1[7] = half_btf(cospi[48], bf0[7], cospi[16], bf0[6], cos_bit);
1075
1076
1077
1078
  bf1[8] = bf0[8];
  bf1[9] = bf0[9];
  bf1[10] = bf0[10];
  bf1[11] = bf0[11];
1079
1080
1081
1082
  bf1[12] = half_btf(cospi[16], bf0[12], cospi[48], bf0[13], cos_bit);
  bf1[13] = half_btf(-cospi[16], bf0[13], cospi[48], bf0[12], cos_bit);
  bf1[14] = half_btf(-cospi[48], bf0[14], cospi[16], bf0[15], cos_bit);
  bf1[15] = half_btf(cospi[48], bf0[15], cospi[16], bf0[14], cos_bit);
1083
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
1084
1085
1086
1087
1088

  // stage 7
  stage++;
  bf0 = step;
  bf1 = output;
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
  bf1[0] = bf0[0] + bf0[2];
  bf1[1] = bf0[1] + bf0[3];
  bf1[2] = -bf0[2] + bf0[0];
  bf1[3] = -bf0[3] + bf0[1];
  bf1[4] = bf0[4] + bf0[6];
  bf1[5] = bf0[5] + bf0[7];
  bf1[6] = -bf0[6] + bf0[4];
  bf1[7] = -bf0[7] + bf0[5];
  bf1[8] = bf0[8] + bf0[10];
  bf1[9] = bf0[9] + bf0[11];
  bf1[10] = -bf0[10] + bf0[8];
  bf1[11] = -bf0[11] + bf0[9];
  bf1[12] = bf0[12] + bf0[14];
  bf1[13] = bf0[13] + bf0[15];
  bf1[14] = -bf0[14] + bf0[12];
  bf1[15] = -bf0[15] + bf0[13];
1105
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
1106
1107
1108

  // stage 8
  stage++;
1109
  cospi = cospi_arr(cos_bit);
Angie Chiang's avatar
Angie Chiang committed
1110
1111
  bf0 = output;
  bf1 = step;
1112
1113
  bf1[0] = bf0[0];
  bf1[1] = bf0[1];
1114
1115
  bf1[2] = half_btf(cospi[32], bf0[2], cospi[32], bf0[3], cos_bit);
  bf1[3] = half_btf(-cospi[32], bf0[3], cospi[32], bf0[2], cos_bit);
1116
1117
  bf1[4] = bf0[4];
  bf1[5] = bf0[5];
1118
1119
  bf1[6] = half_btf(cospi[32], bf0[6], cospi[32], bf0[7], cos_bit);
  bf1[7] = half_btf(-cospi[32], bf0[7], cospi[32], bf0[6], cos_bit);
1120
1121
  bf1[8] = bf0[8];
  bf1[9] = bf0[9];
1122
1123
  bf1[10] = half_btf(cospi[32], bf0[10], cospi[32], bf0[11], cos_bit);
  bf1[11] = half_btf(-cospi[32], bf0[11], cospi[32], bf0[10], cos_bit);
1124
1125
  bf1[12] = bf0[12];
  bf1[13] = bf0[13];
1126
1127
  bf1[14] = half_btf(cospi[32], bf0[14], cospi[32], bf0[15], cos_bit);
  bf1[15] = half_btf(-cospi[32], bf0[15], cospi[32], bf0[14], cos_bit);
1128
  apply_range(stage, input, bf1, size, stage_range[stage]);
Angie Chiang's avatar
Angie Chiang committed
1129
1130
1131
1132
1133

  // stage 9
  stage++;
  bf0 = step;
  bf1 = output;
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
  bf1[0] = bf0[0];
  bf1[1] = -bf0[8];
  bf1[2] = bf0[12];
  bf1[3] = -bf0[4];
  bf1[4] = bf0[6];
  bf1[