blob: 57560a54ced91281464ce5fc846e0a36eaef46bf [file] [log] [blame]
Angie Chiang971a5962017-02-24 11:31:35 -08001/*
2 * Copyright (c) 2017, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12#ifndef AV1_COMMON_TXB_COMMON_H_
13#define AV1_COMMON_TXB_COMMON_H_
14static int16_t coeff_band_4x4[16] = { 0, 1, 2, 3, 4, 5, 6, 7,
15 8, 9, 10, 11, 12, 13, 14, 15 };
16
17static int16_t coeff_band_8x8[64] = {
18 0, 1, 2, 2, 3, 3, 4, 4, 5, 6, 2, 2, 3, 3, 4, 4,
19 7, 7, 8, 8, 9, 9, 10, 10, 7, 7, 8, 8, 9, 9, 10, 10,
20 11, 11, 12, 12, 13, 13, 14, 14, 11, 11, 12, 12, 13, 13, 14, 14,
21 15, 15, 16, 16, 17, 17, 18, 18, 15, 15, 16, 16, 17, 17, 18, 18,
22};
23
24static int16_t coeff_band_16x16[256] = {
25 0, 1, 4, 4, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 2, 3, 4,
26 4, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 5, 5, 6, 6, 7, 7,
27 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 5, 5, 6, 6, 7, 7, 7, 7, 8,
28 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12,
29 13, 13, 13, 13, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13,
30 13, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 10, 10,
31 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15,
32 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 14, 14, 14, 14, 15, 15, 15, 15,
33 16, 16, 16, 16, 17, 17, 17, 17, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16,
34 16, 17, 17, 17, 17, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17,
35 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 18,
36 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 18, 18, 18, 18,
37 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 18, 18, 18, 18, 19, 19, 19,
38 19, 20, 20, 20, 20, 21, 21, 21, 21,
39};
40
41static int16_t coeff_band_32x32[1024] = {
42 0, 1, 4, 4, 7, 7, 7, 7, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11,
43 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 2, 3, 4, 4, 7, 7,
44 7, 7, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12,
45 12, 12, 12, 12, 12, 12, 12, 5, 5, 6, 6, 7, 7, 7, 7, 10, 10, 10, 10,
46 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12,
47 12, 5, 5, 6, 6, 7, 7, 7, 7, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11,
48 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 8, 8, 8, 8, 9,
49 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11,
50 12, 12, 12, 12, 12, 12, 12, 12, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10,
51 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12,
52 12, 12, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11,
53 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 8, 8, 8, 8,
54 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11,
55 11, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14,
56 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16,
57 16, 16, 16, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14,
58 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 13, 13, 13,
59 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15,
60 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 13, 13, 13, 13, 13, 13, 13, 13, 14,
61 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16,
62 16, 16, 16, 16, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14,
63 14, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 13, 13,
64 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15,
65 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 13, 13, 13, 13, 13, 13, 13, 13,
66 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16,
67 16, 16, 16, 16, 16, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14,
68 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 17,
69 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
70 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 17, 17, 17, 17, 17, 17, 17,
71 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20,
72 20, 20, 20, 20, 20, 20, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18,
73 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20,
74 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19,
75 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 17, 17, 17, 17, 17, 17,
76 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 20,
77 20, 20, 20, 20, 20, 20, 20, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18,
78 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20,
79 20, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19,
80 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 17, 17, 17, 17, 17,
81 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19,
82 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22,
83 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24,
84 24, 24, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 23,
85 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 21, 21, 21, 21,
86 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23,
87 23, 24, 24, 24, 24, 24, 24, 24, 24, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22,
88 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24,
89 24, 24, 24, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
90 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 21, 21, 21,
91 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23,
92 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 21, 21, 21, 21, 21, 21, 21, 21, 22,
93 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24,
94 24, 24, 24, 24, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22,
95 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24,
96};
97
Angie Chiangcea11f22017-02-24 12:30:40 -080098typedef struct txb_ctx {
99 int txb_skip_ctx;
100 int dc_sign_ctx;
101} TXB_CTX;
102
Angie Chiang971a5962017-02-24 11:31:35 -0800103#define BASE_CONTEXT_POSITION_NUM 12
104static int base_ref_offset[BASE_CONTEXT_POSITION_NUM][2] = {
105 /* clang-format off*/
106 { -2, 0 }, { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -2 }, { 0, -1 }, { 0, 1 },
107 { 0, 2 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { 2, 0 }
108 /* clang-format on*/
109};
110
111static INLINE int get_base_ctx(const tran_low_t *tcoeffs,
112 int c, // raster order
113 const int bwl, const int level) {
114 const int row = c >> bwl;
115 const int col = c - (row << bwl);
116 const int stride = 1 << bwl;
117 const int level_minus_1 = level - 1;
118 int ctx = 0;
119 int mag = 0;
120 int idx;
121 int ctx_idx = -1;
122 tran_low_t abs_coeff;
123
124 ctx = 0;
125 for (idx = 0; idx < BASE_CONTEXT_POSITION_NUM; ++idx) {
126 int ref_row = row + base_ref_offset[idx][0];
127 int ref_col = col + base_ref_offset[idx][1];
128 int pos = (ref_row << bwl) + ref_col;
129
130 if (ref_row < 0 || ref_col < 0 || ref_row >= stride || ref_col >= stride)
131 continue;
132
133 abs_coeff = abs(tcoeffs[pos]);
134 ctx += abs_coeff > level_minus_1;
135
136 if (base_ref_offset[idx][0] >= 0 && base_ref_offset[idx][1] >= 0)
137 mag |= abs_coeff > level;
138 }
139 ctx = (ctx + 1) >> 1;
140 if (row == 0 && col == 0) {
141 ctx_idx = (ctx << 1) + mag;
142 assert(ctx_idx < 8);
143 } else if (row == 0) {
144 ctx_idx = 8 + (ctx << 1) + mag;
145 assert(ctx_idx < 18);
146 } else if (col == 0) {
147 ctx_idx = 8 + 10 + (ctx << 1) + mag;
148 assert(ctx_idx < 28);
149 } else {
150 ctx_idx = 8 + 10 + 10 + (ctx << 1) + mag;
151 assert(ctx_idx < COEFF_BASE_CONTEXTS);
152 }
153 return ctx_idx;
154}
155
156#define BR_CONTEXT_POSITION_NUM 8 // Base range coefficient context
157static int br_ref_offset[BR_CONTEXT_POSITION_NUM][2] = {
158 /* clang-format off*/
159 { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 },
160 { 0, 1 }, { 1, -1 }, { 1, 0 }, { 1, 1 },
161 /* clang-format on*/
162};
163
164static int br_level_map[9] = {
165 0, 0, 1, 1, 2, 2, 3, 3, 3,
166};
167
168static INLINE int get_level_ctx(const tran_low_t *tcoeffs,
169 const int c, // raster order
170 const int bwl) {
171 const int row = c >> bwl;
172 const int col = c - (row << bwl);
173 const int stride = 1 << bwl;
174 const int level_minus_1 = NUM_BASE_LEVELS;
175 int ctx = 0;
176 int idx;
177 tran_low_t abs_coeff;
178 int mag = 0, offset = 0;
179
180 for (idx = 0; idx < BR_CONTEXT_POSITION_NUM; ++idx) {
181 int ref_row = row + br_ref_offset[idx][0];
182 int ref_col = col + br_ref_offset[idx][1];
183 int pos = (ref_row << bwl) + ref_col;
184
185 if (ref_row < 0 || ref_col < 0 || ref_row >= stride || ref_col >= stride)
186 continue;
187
188 abs_coeff = abs(tcoeffs[pos]);
189 ctx += abs_coeff > level_minus_1;
190
191 if (br_ref_offset[idx][0] >= 0 && br_ref_offset[idx][1] >= 0)
192 mag = AOMMAX(mag, abs_coeff);
193 }
194
195 if (mag <= 1)
196 offset = 0;
197 else if (mag <= 3)
198 offset = 1;
199 else if (mag <= 6)
200 offset = 2;
201 else
202 offset = 3;
203
204 ctx = br_level_map[ctx];
205
206 ctx += offset * BR_TMP_OFFSET;
207
208 // DC: 0 - 1
209 if (row == 0 && col == 0) return ctx;
210
211 // Top row: 2 - 4
212 if (row == 0) return 2 + ctx;
213
214 // Left column: 5 - 7
215 if (col == 0) return 5 + ctx;
216
217 // others: 8 - 11
218 return 8 + ctx;
219}
220
221static int sig_ref_offset[11][2] = {
222 { -2, -1 }, { -2, 0 }, { -2, 1 }, { -1, -2 }, { -1, -1 }, { -1, 0 },
223 { -1, 1 }, { 0, -2 }, { 0, -1 }, { 1, -2 }, { 1, -1 },
224};
225
226static INLINE int get_nz_map_ctx(const tran_low_t *tcoeffs,
227 const uint8_t *txb_mask,
228 const int c, // raster order
229 const int bwl) {
230 const int row = c >> bwl;
231 const int col = c - (row << bwl);
232 int ctx = 0;
233 int idx;
234 int stride = 1 << bwl;
235
236 if (row == 0 && col == 0) return 0;
237
238 if (row == 0 && col == 1) return 1 + (tcoeffs[0] != 0);
239
240 if (row == 1 && col == 0) return 3 + (tcoeffs[0] != 0);
241
242 if (row == 1 && col == 1) {
243 int pos;
244 ctx = (tcoeffs[0] != 0);
245
246 if (txb_mask[1]) ctx += (tcoeffs[1] != 0);
247 pos = 1 << bwl;
248 if (txb_mask[pos]) ctx += (tcoeffs[pos] != 0);
249
250 ctx = (ctx + 1) >> 1;
251
252 // unit test purpose
253 if (5 + ctx > 7) exit(0);
254
255 return 5 + ctx;
256 }
257
258 for (idx = 0; idx < 11; ++idx) {
259 int ref_row = row + sig_ref_offset[idx][0];
260 int ref_col = col + sig_ref_offset[idx][1];
261 int pos;
262
263 if (ref_row < 0 || ref_col < 0 || ref_row >= stride || ref_col >= stride)
264 continue;
265
266 pos = (ref_row << bwl) + ref_col;
267
268 if (txb_mask[pos]) ctx += (tcoeffs[pos] != 0);
269 }
270
271 if (row == 0) {
272 ctx = (ctx + 1) >> 1;
273
274 if (ctx >= 3) exit(0);
275 return 8 + ctx;
276 }
277
278 if (col == 0) {
279 ctx = (ctx + 1) >> 1;
280
281 if (ctx >= 3) exit(0);
282 return 11 + ctx;
283 }
284
285 ctx >>= 1;
286
287 if (14 + ctx >= 20) exit(0);
288
289 return 14 + ctx;
290}
291
292static INLINE int get_eob_ctx(const tran_low_t *tcoeffs,
293 const int c, // raster order
294 const int bwl) {
295 (void)tcoeffs;
296 if (bwl == 2) return coeff_band_4x4[c];
297 if (bwl == 3) return coeff_band_8x8[c];
298 if (bwl == 4) return coeff_band_16x16[c];
299 if (bwl == 5) return coeff_band_32x32[c];
300
301 exit(0);
302}
303
Angie Chiangcea11f22017-02-24 12:30:40 -0800304static INLINE void set_dc_sign(int *cul_level, tran_low_t v) {
305 if (v < 0)
306 *cul_level |= 1 << COEFF_CONTEXT_BITS;
307 else if (v > 0)
308 *cul_level += 2 << COEFF_CONTEXT_BITS;
309}
Angie Chiang971a5962017-02-24 11:31:35 -0800310#endif // AV1_COMMON_TXB_COMMON_H_