av1_inv_txfm2d.c 8.21 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
 */

Yaowu Xu's avatar
Yaowu Xu committed
12
#include "./av1_rtcd.h"
13
#include "av1/common/enums.h"
Yaowu Xu's avatar
Yaowu Xu committed
14
15
16
#include "av1/common/av1_txfm.h"
#include "av1/common/av1_inv_txfm1d.h"
#include "av1/common/av1_inv_txfm2d_cfg.h"
Angie Chiang's avatar
Angie Chiang committed
17

Yaowu Xu's avatar
Yaowu Xu committed
18
static INLINE TxfmFunc inv_txfm_type_to_func(TXFM_TYPE txfm_type) {
19
  switch (txfm_type) {
Yaowu Xu's avatar
Yaowu Xu committed
20
21
22
23
24
25
26
27
    case TXFM_TYPE_DCT4: return av1_idct4_new;
    case TXFM_TYPE_DCT8: return av1_idct8_new;
    case TXFM_TYPE_DCT16: return av1_idct16_new;
    case TXFM_TYPE_DCT32: return av1_idct32_new;
    case TXFM_TYPE_ADST4: return av1_iadst4_new;
    case TXFM_TYPE_ADST8: return av1_iadst8_new;
    case TXFM_TYPE_ADST16: return av1_iadst16_new;
    case TXFM_TYPE_ADST32: return av1_iadst32_new;
clang-format's avatar
clang-format committed
28
    default: assert(0); return NULL;
29
30
31
  }
}

32
#if CONFIG_EXT_TX
clang-format's avatar
clang-format committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
static const TXFM_2D_CFG *inv_txfm_cfg_ls[FLIPADST_ADST + 1][TX_SIZES] = {
  { &inv_txfm_2d_cfg_dct_dct_4, &inv_txfm_2d_cfg_dct_dct_8,
    &inv_txfm_2d_cfg_dct_dct_16, &inv_txfm_2d_cfg_dct_dct_32 },
  { &inv_txfm_2d_cfg_adst_dct_4, &inv_txfm_2d_cfg_adst_dct_8,
    &inv_txfm_2d_cfg_adst_dct_16, &inv_txfm_2d_cfg_adst_dct_32 },
  { &inv_txfm_2d_cfg_dct_adst_4, &inv_txfm_2d_cfg_dct_adst_8,
    &inv_txfm_2d_cfg_dct_adst_16, &inv_txfm_2d_cfg_dct_adst_32 },
  { &inv_txfm_2d_cfg_adst_adst_4, &inv_txfm_2d_cfg_adst_adst_8,
    &inv_txfm_2d_cfg_adst_adst_16, &inv_txfm_2d_cfg_adst_adst_32 },
  { &inv_txfm_2d_cfg_adst_dct_4, &inv_txfm_2d_cfg_adst_dct_8,
    &inv_txfm_2d_cfg_adst_dct_16, &inv_txfm_2d_cfg_adst_dct_32 },
  { &inv_txfm_2d_cfg_dct_adst_4, &inv_txfm_2d_cfg_dct_adst_8,
    &inv_txfm_2d_cfg_dct_adst_16, &inv_txfm_2d_cfg_dct_adst_32 },
  { &inv_txfm_2d_cfg_adst_adst_4, &inv_txfm_2d_cfg_adst_adst_8,
    &inv_txfm_2d_cfg_adst_adst_16, &inv_txfm_2d_cfg_adst_adst_32 },
  { &inv_txfm_2d_cfg_adst_adst_4, &inv_txfm_2d_cfg_adst_adst_8,
    &inv_txfm_2d_cfg_adst_adst_16, &inv_txfm_2d_cfg_adst_adst_32 },
  { &inv_txfm_2d_cfg_adst_adst_4, &inv_txfm_2d_cfg_adst_adst_8,
    &inv_txfm_2d_cfg_adst_adst_16, &inv_txfm_2d_cfg_adst_adst_32 },
52
53
};
#else
clang-format's avatar
clang-format committed
54
static const TXFM_2D_CFG *inv_txfm_cfg_ls[TX_TYPES][TX_SIZES] = {
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  {
#if CONFIG_CB4X4
      NULL,
#endif
      &inv_txfm_2d_cfg_dct_dct_4, &inv_txfm_2d_cfg_dct_dct_8,
      &inv_txfm_2d_cfg_dct_dct_16, &inv_txfm_2d_cfg_dct_dct_32 },
  {
#if CONFIG_CB4X4
      NULL,
#endif
      &inv_txfm_2d_cfg_adst_dct_4, &inv_txfm_2d_cfg_adst_dct_8,
      &inv_txfm_2d_cfg_adst_dct_16, &inv_txfm_2d_cfg_adst_dct_32 },
  {
#if CONFIG_CB4X4
      NULL,
#endif
      &inv_txfm_2d_cfg_dct_adst_4, &inv_txfm_2d_cfg_dct_adst_8,
      &inv_txfm_2d_cfg_dct_adst_16, &inv_txfm_2d_cfg_dct_adst_32 },
  {
#if CONFIG_CB4X4
      NULL,
#endif
      &inv_txfm_2d_cfg_adst_adst_4, &inv_txfm_2d_cfg_adst_adst_8,
      &inv_txfm_2d_cfg_adst_adst_16, &inv_txfm_2d_cfg_adst_adst_32 },
79
80
81
};
#endif

Yaowu Xu's avatar
Yaowu Xu committed
82
TXFM_2D_FLIP_CFG av1_get_inv_txfm_cfg(int tx_type, int tx_size) {
83
84
85
  TXFM_2D_FLIP_CFG cfg;
  set_flip_cfg(tx_type, &cfg);
  cfg.cfg = inv_txfm_cfg_ls[tx_type][tx_size];
86
87
88
  return cfg;
}

Yaowu Xu's avatar
Yaowu Xu committed
89
TXFM_2D_FLIP_CFG av1_get_inv_txfm_64x64_cfg(int tx_type) {
clang-format's avatar
clang-format committed
90
  TXFM_2D_FLIP_CFG cfg = { 0, 0, NULL };
91
92
  switch (tx_type) {
    case DCT_DCT:
93
94
      cfg.cfg = &inv_txfm_2d_cfg_dct_dct_64;
      set_flip_cfg(tx_type, &cfg);
95
      break;
clang-format's avatar
clang-format committed
96
    default: assert(0);
97
98
99
100
  }
  return cfg;
}

Yaowu Xu's avatar
Yaowu Xu committed
101
static INLINE void inv_txfm2d_add_c(const int32_t *input, int16_t *output,
102
                                    int stride, TXFM_2D_FLIP_CFG *cfg,
Angie Chiang's avatar
Angie Chiang committed
103
                                    int32_t *txfm_buf) {
104
105
106
107
108
109
110
111
  const int txfm_size = cfg->cfg->txfm_size;
  const int8_t *shift = cfg->cfg->shift;
  const int8_t *stage_range_col = cfg->cfg->stage_range_col;
  const int8_t *stage_range_row = cfg->cfg->stage_range_row;
  const int8_t *cos_bit_col = cfg->cfg->cos_bit_col;
  const int8_t *cos_bit_row = cfg->cfg->cos_bit_row;
  const TxfmFunc txfm_func_col = inv_txfm_type_to_func(cfg->cfg->txfm_type_col);
  const TxfmFunc txfm_func_row = inv_txfm_type_to_func(cfg->cfg->txfm_type_row);
Angie Chiang's avatar
Angie Chiang committed
112
113
114
115
116
117
118

  // txfm_buf's length is  txfm_size * txfm_size + 2 * txfm_size
  // it is used for intermediate data buffering
  int32_t *temp_in = txfm_buf;
  int32_t *temp_out = temp_in + txfm_size;
  int32_t *buf = temp_out + txfm_size;
  int32_t *buf_ptr = buf;
119
  int c, r;
Angie Chiang's avatar
Angie Chiang committed
120
121

  // Rows
122
  for (r = 0; r < txfm_size; ++r) {
Angie Chiang's avatar
Angie Chiang committed
123
124
125
126
127
128
129
    txfm_func_row(input, buf_ptr, cos_bit_row, stage_range_row);
    round_shift_array(buf_ptr, txfm_size, -shift[0]);
    input += txfm_size;
    buf_ptr += txfm_size;
  }

  // Columns
130
131
  for (c = 0; c < txfm_size; ++c) {
    if (cfg->lr_flip == 0) {
clang-format's avatar
clang-format committed
132
      for (r = 0; r < txfm_size; ++r) temp_in[r] = buf[r * txfm_size + c];
133
134
135
136
137
    } else {
      // flip left right
      for (r = 0; r < txfm_size; ++r)
        temp_in[r] = buf[r * txfm_size + (txfm_size - c - 1)];
    }
Angie Chiang's avatar
Angie Chiang committed
138
139
    txfm_func_col(temp_in, temp_out, cos_bit_col, stage_range_col);
    round_shift_array(temp_out, txfm_size, -shift[1]);
140
    if (cfg->ud_flip == 0) {
clang-format's avatar
clang-format committed
141
      for (r = 0; r < txfm_size; ++r) output[r * stride + c] += temp_out[r];
142
143
144
145
146
    } else {
      // flip upside down
      for (r = 0; r < txfm_size; ++r)
        output[r * stride + c] += temp_out[txfm_size - r - 1];
    }
Angie Chiang's avatar
Angie Chiang committed
147
148
149
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
150
151
void av1_inv_txfm2d_add_4x4_c(const int32_t *input, uint16_t *output,
                              int stride, int tx_type, int bd) {
Angie Chiang's avatar
Angie Chiang committed
152
153
154
155
156
  int txfm_buf[4 * 4 + 4 + 4];
  // output contains the prediction signal which is always positive and smaller
  // than (1 << bd) - 1
  // since bd < 16-1, therefore we can treat the uint16_t* output buffer as an
  // int16_t*
Yaowu Xu's avatar
Yaowu Xu committed
157
  TXFM_2D_FLIP_CFG cfg = av1_get_inv_txfm_cfg(tx_type, TX_4X4);
158
  inv_txfm2d_add_c(input, (int16_t *)output, stride, &cfg, txfm_buf);
Angie Chiang's avatar
Angie Chiang committed
159
160
161
  clamp_block((int16_t *)output, 4, stride, 0, (1 << bd) - 1);
}

Yaowu Xu's avatar
Yaowu Xu committed
162
163
void av1_inv_txfm2d_add_8x8_c(const int32_t *input, uint16_t *output,
                              int stride, int tx_type, int bd) {
Angie Chiang's avatar
Angie Chiang committed
164
165
166
167
168
  int txfm_buf[8 * 8 + 8 + 8];
  // output contains the prediction signal which is always positive and smaller
  // than (1 << bd) - 1
  // since bd < 16-1, therefore we can treat the uint16_t* output buffer as an
  // int16_t*
Yaowu Xu's avatar
Yaowu Xu committed
169
  TXFM_2D_FLIP_CFG cfg = av1_get_inv_txfm_cfg(tx_type, TX_8X8);
170
  inv_txfm2d_add_c(input, (int16_t *)output, stride, &cfg, txfm_buf);
Angie Chiang's avatar
Angie Chiang committed
171
172
173
  clamp_block((int16_t *)output, 8, stride, 0, (1 << bd) - 1);
}

Yaowu Xu's avatar
Yaowu Xu committed
174
175
void av1_inv_txfm2d_add_16x16_c(const int32_t *input, uint16_t *output,
                                int stride, int tx_type, int bd) {
Angie Chiang's avatar
Angie Chiang committed
176
177
178
179
180
  int txfm_buf[16 * 16 + 16 + 16];
  // output contains the prediction signal which is always positive and smaller
  // than (1 << bd) - 1
  // since bd < 16-1, therefore we can treat the uint16_t* output buffer as an
  // int16_t*
Yaowu Xu's avatar
Yaowu Xu committed
181
  TXFM_2D_FLIP_CFG cfg = av1_get_inv_txfm_cfg(tx_type, TX_16X16);
182
  inv_txfm2d_add_c(input, (int16_t *)output, stride, &cfg, txfm_buf);
Angie Chiang's avatar
Angie Chiang committed
183
184
185
  clamp_block((int16_t *)output, 16, stride, 0, (1 << bd) - 1);
}

Yaowu Xu's avatar
Yaowu Xu committed
186
187
void av1_inv_txfm2d_add_32x32_c(const int32_t *input, uint16_t *output,
                                int stride, int tx_type, int bd) {
Angie Chiang's avatar
Angie Chiang committed
188
189
190
191
192
  int txfm_buf[32 * 32 + 32 + 32];
  // output contains the prediction signal which is always positive and smaller
  // than (1 << bd) - 1
  // since bd < 16-1, therefore we can treat the uint16_t* output buffer as an
  // int16_t*
Yaowu Xu's avatar
Yaowu Xu committed
193
  TXFM_2D_FLIP_CFG cfg = av1_get_inv_txfm_cfg(tx_type, TX_32X32);
194
  inv_txfm2d_add_c(input, (int16_t *)output, stride, &cfg, txfm_buf);
Angie Chiang's avatar
Angie Chiang committed
195
196
  clamp_block((int16_t *)output, 32, stride, 0, (1 << bd) - 1);
}
Angie Chiang's avatar
Angie Chiang committed
197

Yaowu Xu's avatar
Yaowu Xu committed
198
199
void av1_inv_txfm2d_add_64x64_c(const int32_t *input, uint16_t *output,
                                int stride, int tx_type, int bd) {
Angie Chiang's avatar
Angie Chiang committed
200
201
202
203
204
  int txfm_buf[64 * 64 + 64 + 64];
  // output contains the prediction signal which is always positive and smaller
  // than (1 << bd) - 1
  // since bd < 16-1, therefore we can treat the uint16_t* output buffer as an
  // int16_t*
Yaowu Xu's avatar
Yaowu Xu committed
205
  TXFM_2D_FLIP_CFG cfg = av1_get_inv_txfm_64x64_cfg(tx_type);
206
  inv_txfm2d_add_c(input, (int16_t *)output, stride, &cfg, txfm_buf);
Angie Chiang's avatar
Angie Chiang committed
207
208
  clamp_block((int16_t *)output, 64, stride, 0, (1 << bd) - 1);
}