1 /*
2  * Copyright (c) 2016, 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 AOM_AV1_COMMON_COMMON_DATA_H_
13 #define AOM_AV1_COMMON_COMMON_DATA_H_
14 
15 #include "av1/common/enums.h"
16 #include "aom/aom_integer.h"
17 #include "aom_dsp/aom_dsp_common.h"
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 // Log 2 conversion lookup tables in units of mode info (4x4).
24 // The Mi_Width_Log2 table in the spec (Section 9.3. Conversion tables).
25 static const uint8_t mi_size_wide_log2[BLOCK_SIZES_ALL] = {
26   0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 0, 2, 1, 3, 2, 4
27 };
28 // The Mi_Height_Log2 table in the spec (Section 9.3. Conversion tables).
29 static const uint8_t mi_size_high_log2[BLOCK_SIZES_ALL] = {
30   0, 1, 0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 2, 0, 3, 1, 4, 2
31 };
32 
33 // Width/height lookup tables in units of mode info (4x4).
34 // The Num_4x4_Blocks_Wide table in the spec (Section 9.3. Conversion tables).
35 static const uint8_t mi_size_wide[BLOCK_SIZES_ALL] = {
36   1, 1, 2, 2, 2, 4, 4, 4, 8, 8, 8, 16, 16, 16, 32, 32, 1, 4, 2, 8, 4, 16
37 };
38 
39 // The Num_4x4_Blocks_High table in the spec (Section 9.3. Conversion tables).
40 static const uint8_t mi_size_high[BLOCK_SIZES_ALL] = {
41   1, 2, 1, 2, 4, 2, 4, 8, 4, 8, 16, 8, 16, 32, 16, 32, 4, 1, 8, 2, 16, 4
42 };
43 
44 // Width/height lookup tables in units of samples.
45 // The Block_Width table in the spec (Section 9.3. Conversion tables).
46 static const uint8_t block_size_wide[BLOCK_SIZES_ALL] = {
47   4,  4,  8,  8,   8,   16, 16, 16, 32, 32, 32,
48   64, 64, 64, 128, 128, 4,  16, 8,  32, 16, 64
49 };
50 
51 // The Block_Height table in the spec (Section 9.3. Conversion tables).
52 static const uint8_t block_size_high[BLOCK_SIZES_ALL] = {
53   4,  8,  4,   8,  16,  8,  16, 32, 16, 32, 64,
54   32, 64, 128, 64, 128, 16, 4,  32, 8,  64, 16
55 };
56 
57 // Maps a block size to a context.
58 // The Size_Group table in the spec (Section 9.3. Conversion tables).
59 // AOMMIN(3, AOMMIN(mi_size_wide_log2(bsize), mi_size_high_log2(bsize)))
60 static const uint8_t size_group_lookup[BLOCK_SIZES_ALL] = {
61   0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 0, 0, 1, 1, 2, 2
62 };
63 
64 static const uint8_t num_pels_log2_lookup[BLOCK_SIZES_ALL] = {
65   4, 5, 5, 6, 7, 7, 8, 9, 9, 10, 11, 11, 12, 13, 13, 14, 6, 6, 8, 8, 10, 10
66 };
67 
68 // A compressed version of the Partition_Subsize table in the spec (9.3.
69 // Conversion tables), for square block sizes only.
70 /* clang-format off */
71 static const BLOCK_SIZE subsize_lookup[EXT_PARTITION_TYPES][SQR_BLOCK_SIZES] = {
72   {     // PARTITION_NONE
73     BLOCK_4X4, BLOCK_8X8, BLOCK_16X16,
74     BLOCK_32X32, BLOCK_64X64, BLOCK_128X128
75   }, {  // PARTITION_HORZ
76     BLOCK_INVALID, BLOCK_8X4, BLOCK_16X8,
77     BLOCK_32X16, BLOCK_64X32, BLOCK_128X64
78   }, {  // PARTITION_VERT
79     BLOCK_INVALID, BLOCK_4X8, BLOCK_8X16,
80     BLOCK_16X32, BLOCK_32X64, BLOCK_64X128
81   }, {  // PARTITION_SPLIT
82     BLOCK_INVALID, BLOCK_4X4, BLOCK_8X8,
83     BLOCK_16X16, BLOCK_32X32, BLOCK_64X64
84   }, {  // PARTITION_HORZ_A
85     BLOCK_INVALID, BLOCK_INVALID, BLOCK_16X8,
86     BLOCK_32X16, BLOCK_64X32, BLOCK_128X64
87   }, {  // PARTITION_HORZ_B
88     BLOCK_INVALID, BLOCK_INVALID, BLOCK_16X8,
89     BLOCK_32X16, BLOCK_64X32, BLOCK_128X64
90   }, {  // PARTITION_VERT_A
91     BLOCK_INVALID, BLOCK_INVALID, BLOCK_8X16,
92     BLOCK_16X32, BLOCK_32X64, BLOCK_64X128
93   }, {  // PARTITION_VERT_B
94     BLOCK_INVALID, BLOCK_INVALID, BLOCK_8X16,
95     BLOCK_16X32, BLOCK_32X64, BLOCK_64X128
96   }, {  // PARTITION_HORZ_4
97     BLOCK_INVALID, BLOCK_INVALID, BLOCK_16X4,
98     BLOCK_32X8, BLOCK_64X16, BLOCK_INVALID
99   }, {  // PARTITION_VERT_4
100     BLOCK_INVALID, BLOCK_INVALID, BLOCK_4X16,
101     BLOCK_8X32, BLOCK_16X64, BLOCK_INVALID
102   }
103 };
104 
105 static const TX_SIZE max_txsize_lookup[BLOCK_SIZES_ALL] = {
106   //                   4X4
107                        TX_4X4,
108   // 4X8,    8X4,      8X8
109   TX_4X4,    TX_4X4,   TX_8X8,
110   // 8X16,   16X8,     16X16
111   TX_8X8,    TX_8X8,   TX_16X16,
112   // 16X32,  32X16,    32X32
113   TX_16X16,  TX_16X16, TX_32X32,
114   // 32X64,  64X32,
115   TX_32X32,  TX_32X32,
116   // 64X64
117   TX_64X64,
118   // 64x128, 128x64,   128x128
119   TX_64X64,  TX_64X64, TX_64X64,
120   // 4x16,   16x4,     8x32
121   TX_4X4,    TX_4X4,   TX_8X8,
122   // 32x8,   16x64     64x16
123   TX_8X8,    TX_16X16, TX_16X16
124 };
125 
126 static const TX_SIZE max_txsize_rect_lookup[BLOCK_SIZES_ALL] = {
127       // 4X4
128       TX_4X4,
129       // 4X8,    8X4,      8X8
130       TX_4X8,    TX_8X4,   TX_8X8,
131       // 8X16,   16X8,     16X16
132       TX_8X16,   TX_16X8,  TX_16X16,
133       // 16X32,  32X16,    32X32
134       TX_16X32,  TX_32X16, TX_32X32,
135       // 32X64,  64X32,
136       TX_32X64,  TX_64X32,
137       // 64X64
138       TX_64X64,
139       // 64x128, 128x64,   128x128
140       TX_64X64,  TX_64X64, TX_64X64,
141       // 4x16,   16x4,
142       TX_4X16,   TX_16X4,
143       // 8x32,   32x8
144       TX_8X32,   TX_32X8,
145       // 16x64,  64x16
146       TX_16X64,  TX_64X16
147 };
148 
149 static const TX_TYPE_1D vtx_tab[TX_TYPES] = {
150   DCT_1D,      ADST_1D, DCT_1D,      ADST_1D,
151   FLIPADST_1D, DCT_1D,  FLIPADST_1D, ADST_1D, FLIPADST_1D, IDTX_1D,
152   DCT_1D,      IDTX_1D, ADST_1D,     IDTX_1D, FLIPADST_1D, IDTX_1D,
153 };
154 
155 static const TX_TYPE_1D htx_tab[TX_TYPES] = {
156   DCT_1D,  DCT_1D,      ADST_1D,     ADST_1D,
157   DCT_1D,  FLIPADST_1D, FLIPADST_1D, FLIPADST_1D, ADST_1D, IDTX_1D,
158   IDTX_1D, DCT_1D,      IDTX_1D,     ADST_1D,     IDTX_1D, FLIPADST_1D,
159 };
160 
161 #define TXSIZE_CAT_INVALID (-1)
162 
163 /* clang-format on */
164 
165 static const TX_SIZE sub_tx_size_map[TX_SIZES_ALL] = {
166   TX_4X4,    // TX_4X4
167   TX_4X4,    // TX_8X8
168   TX_8X8,    // TX_16X16
169   TX_16X16,  // TX_32X32
170   TX_32X32,  // TX_64X64
171   TX_4X4,    // TX_4X8
172   TX_4X4,    // TX_8X4
173   TX_8X8,    // TX_8X16
174   TX_8X8,    // TX_16X8
175   TX_16X16,  // TX_16X32
176   TX_16X16,  // TX_32X16
177   TX_32X32,  // TX_32X64
178   TX_32X32,  // TX_64X32
179   TX_4X8,    // TX_4X16
180   TX_8X4,    // TX_16X4
181   TX_8X16,   // TX_8X32
182   TX_16X8,   // TX_32X8
183   TX_16X32,  // TX_16X64
184   TX_32X16,  // TX_64X16
185 };
186 
187 static const TX_SIZE txsize_horz_map[TX_SIZES_ALL] = {
188   TX_4X4,    // TX_4X4
189   TX_8X8,    // TX_8X8
190   TX_16X16,  // TX_16X16
191   TX_32X32,  // TX_32X32
192   TX_64X64,  // TX_64X64
193   TX_4X4,    // TX_4X8
194   TX_8X8,    // TX_8X4
195   TX_8X8,    // TX_8X16
196   TX_16X16,  // TX_16X8
197   TX_16X16,  // TX_16X32
198   TX_32X32,  // TX_32X16
199   TX_32X32,  // TX_32X64
200   TX_64X64,  // TX_64X32
201   TX_4X4,    // TX_4X16
202   TX_16X16,  // TX_16X4
203   TX_8X8,    // TX_8X32
204   TX_32X32,  // TX_32X8
205   TX_16X16,  // TX_16X64
206   TX_64X64,  // TX_64X16
207 };
208 
209 static const TX_SIZE txsize_vert_map[TX_SIZES_ALL] = {
210   TX_4X4,    // TX_4X4
211   TX_8X8,    // TX_8X8
212   TX_16X16,  // TX_16X16
213   TX_32X32,  // TX_32X32
214   TX_64X64,  // TX_64X64
215   TX_8X8,    // TX_4X8
216   TX_4X4,    // TX_8X4
217   TX_16X16,  // TX_8X16
218   TX_8X8,    // TX_16X8
219   TX_32X32,  // TX_16X32
220   TX_16X16,  // TX_32X16
221   TX_64X64,  // TX_32X64
222   TX_32X32,  // TX_64X32
223   TX_16X16,  // TX_4X16
224   TX_4X4,    // TX_16X4
225   TX_32X32,  // TX_8X32
226   TX_8X8,    // TX_32X8
227   TX_64X64,  // TX_16X64
228   TX_16X16,  // TX_64X16
229 };
230 
231 #define TX_SIZE_W_MIN 4
232 
233 // Transform block width in pixels
234 static const int tx_size_wide[TX_SIZES_ALL] = {
235   4, 8, 16, 32, 64, 4, 8, 8, 16, 16, 32, 32, 64, 4, 16, 8, 32, 16, 64,
236 };
237 
238 #define TX_SIZE_H_MIN 4
239 
240 // Transform block height in pixels
241 static const int tx_size_high[TX_SIZES_ALL] = {
242   4, 8, 16, 32, 64, 8, 4, 16, 8, 32, 16, 64, 32, 16, 4, 32, 8, 64, 16,
243 };
244 
245 // Transform block width in unit
246 static const int tx_size_wide_unit[TX_SIZES_ALL] = {
247   1, 2, 4, 8, 16, 1, 2, 2, 4, 4, 8, 8, 16, 1, 4, 2, 8, 4, 16,
248 };
249 
250 // Transform block height in unit
251 static const int tx_size_high_unit[TX_SIZES_ALL] = {
252   1, 2, 4, 8, 16, 2, 1, 4, 2, 8, 4, 16, 8, 4, 1, 8, 2, 16, 4,
253 };
254 
255 // Transform block width in log2
256 static const int tx_size_wide_log2[TX_SIZES_ALL] = {
257   2, 3, 4, 5, 6, 2, 3, 3, 4, 4, 5, 5, 6, 2, 4, 3, 5, 4, 6,
258 };
259 
260 // Transform block height in log2
261 static const int tx_size_high_log2[TX_SIZES_ALL] = {
262   2, 3, 4, 5, 6, 3, 2, 4, 3, 5, 4, 6, 5, 4, 2, 5, 3, 6, 4,
263 };
264 
265 static const int tx_size_2d[TX_SIZES_ALL + 1] = {
266   16,  64,   256,  1024, 4096, 32,  32,  128,  128,  512,
267   512, 2048, 2048, 64,   64,   256, 256, 1024, 1024,
268 };
269 
270 static const BLOCK_SIZE txsize_to_bsize[TX_SIZES_ALL] = {
271   BLOCK_4X4,    // TX_4X4
272   BLOCK_8X8,    // TX_8X8
273   BLOCK_16X16,  // TX_16X16
274   BLOCK_32X32,  // TX_32X32
275   BLOCK_64X64,  // TX_64X64
276   BLOCK_4X8,    // TX_4X8
277   BLOCK_8X4,    // TX_8X4
278   BLOCK_8X16,   // TX_8X16
279   BLOCK_16X8,   // TX_16X8
280   BLOCK_16X32,  // TX_16X32
281   BLOCK_32X16,  // TX_32X16
282   BLOCK_32X64,  // TX_32X64
283   BLOCK_64X32,  // TX_64X32
284   BLOCK_4X16,   // TX_4X16
285   BLOCK_16X4,   // TX_16X4
286   BLOCK_8X32,   // TX_8X32
287   BLOCK_32X8,   // TX_32X8
288   BLOCK_16X64,  // TX_16X64
289   BLOCK_64X16,  // TX_64X16
290 };
291 
292 static const TX_SIZE txsize_sqr_map[TX_SIZES_ALL] = {
293   TX_4X4,    // TX_4X4
294   TX_8X8,    // TX_8X8
295   TX_16X16,  // TX_16X16
296   TX_32X32,  // TX_32X32
297   TX_64X64,  // TX_64X64
298   TX_4X4,    // TX_4X8
299   TX_4X4,    // TX_8X4
300   TX_8X8,    // TX_8X16
301   TX_8X8,    // TX_16X8
302   TX_16X16,  // TX_16X32
303   TX_16X16,  // TX_32X16
304   TX_32X32,  // TX_32X64
305   TX_32X32,  // TX_64X32
306   TX_4X4,    // TX_4X16
307   TX_4X4,    // TX_16X4
308   TX_8X8,    // TX_8X32
309   TX_8X8,    // TX_32X8
310   TX_16X16,  // TX_16X64
311   TX_16X16,  // TX_64X16
312 };
313 
314 static const TX_SIZE txsize_sqr_up_map[TX_SIZES_ALL] = {
315   TX_4X4,    // TX_4X4
316   TX_8X8,    // TX_8X8
317   TX_16X16,  // TX_16X16
318   TX_32X32,  // TX_32X32
319   TX_64X64,  // TX_64X64
320   TX_8X8,    // TX_4X8
321   TX_8X8,    // TX_8X4
322   TX_16X16,  // TX_8X16
323   TX_16X16,  // TX_16X8
324   TX_32X32,  // TX_16X32
325   TX_32X32,  // TX_32X16
326   TX_64X64,  // TX_32X64
327   TX_64X64,  // TX_64X32
328   TX_16X16,  // TX_4X16
329   TX_16X16,  // TX_16X4
330   TX_32X32,  // TX_8X32
331   TX_32X32,  // TX_32X8
332   TX_64X64,  // TX_16X64
333   TX_64X64,  // TX_64X16
334 };
335 
336 static const int8_t txsize_log2_minus4[TX_SIZES_ALL] = {
337   0,  // TX_4X4
338   2,  // TX_8X8
339   4,  // TX_16X16
340   6,  // TX_32X32
341   6,  // TX_64X64
342   1,  // TX_4X8
343   1,  // TX_8X4
344   3,  // TX_8X16
345   3,  // TX_16X8
346   5,  // TX_16X32
347   5,  // TX_32X16
348   6,  // TX_32X64
349   6,  // TX_64X32
350   2,  // TX_4X16
351   2,  // TX_16X4
352   4,  // TX_8X32
353   4,  // TX_32X8
354   5,  // TX_16X64
355   5,  // TX_64X16
356 };
357 
358 /* clang-format off */
359 static const TX_SIZE tx_mode_to_biggest_tx_size[TX_MODES] = {
360   TX_4X4,    // ONLY_4X4
361   TX_64X64,  // TX_MODE_LARGEST
362   TX_64X64,  // TX_MODE_SELECT
363 };
364 
365 // The Subsampled_Size table in the spec (Section 5.11.38. Get plane residual
366 // size function).
367 static const BLOCK_SIZE ss_size_lookup[BLOCK_SIZES_ALL][2][2] = {
368   //  ss_x == 0      ss_x == 0          ss_x == 1      ss_x == 1
369   //  ss_y == 0      ss_y == 1          ss_y == 0      ss_y == 1
370   { { BLOCK_4X4,     BLOCK_4X4 },     { BLOCK_4X4,     BLOCK_4X4 } },
371   { { BLOCK_4X8,     BLOCK_4X4 },     { BLOCK_INVALID, BLOCK_4X4 } },
372   { { BLOCK_8X4,     BLOCK_INVALID }, { BLOCK_4X4,     BLOCK_4X4 } },
373   { { BLOCK_8X8,     BLOCK_8X4 },     { BLOCK_4X8,     BLOCK_4X4 } },
374   { { BLOCK_8X16,    BLOCK_8X8 },     { BLOCK_INVALID, BLOCK_4X8 } },
375   { { BLOCK_16X8,    BLOCK_INVALID }, { BLOCK_8X8,     BLOCK_8X4 } },
376   { { BLOCK_16X16,   BLOCK_16X8 },    { BLOCK_8X16,    BLOCK_8X8 } },
377   { { BLOCK_16X32,   BLOCK_16X16 },   { BLOCK_INVALID, BLOCK_8X16 } },
378   { { BLOCK_32X16,   BLOCK_INVALID }, { BLOCK_16X16,   BLOCK_16X8 } },
379   { { BLOCK_32X32,   BLOCK_32X16 },   { BLOCK_16X32,   BLOCK_16X16 } },
380   { { BLOCK_32X64,   BLOCK_32X32 },   { BLOCK_INVALID, BLOCK_16X32 } },
381   { { BLOCK_64X32,   BLOCK_INVALID }, { BLOCK_32X32,   BLOCK_32X16 } },
382   { { BLOCK_64X64,   BLOCK_64X32 },   { BLOCK_32X64,   BLOCK_32X32 } },
383   { { BLOCK_64X128,  BLOCK_64X64 },   { BLOCK_INVALID, BLOCK_32X64 } },
384   { { BLOCK_128X64,  BLOCK_INVALID }, { BLOCK_64X64,   BLOCK_64X32 } },
385   { { BLOCK_128X128, BLOCK_128X64 },  { BLOCK_64X128,  BLOCK_64X64 } },
386   { { BLOCK_4X16,    BLOCK_4X8 },     { BLOCK_INVALID, BLOCK_4X8 } },
387   { { BLOCK_16X4,    BLOCK_INVALID }, { BLOCK_8X4,     BLOCK_8X4 } },
388   { { BLOCK_8X32,    BLOCK_8X16 },    { BLOCK_INVALID, BLOCK_4X16 } },
389   { { BLOCK_32X8,    BLOCK_INVALID }, { BLOCK_16X8,    BLOCK_16X4 } },
390   { { BLOCK_16X64,   BLOCK_16X32 },   { BLOCK_INVALID, BLOCK_8X32 } },
391   { { BLOCK_64X16,   BLOCK_INVALID }, { BLOCK_32X16,   BLOCK_32X8 } }
392 };
393 /* clang-format on */
394 
395 // Generates 5 bit field in which each bit set to 1 represents
396 // a blocksize partition  11111 means we split 128x128, 64x64, 32x32, 16x16
397 // and 8x8.  10000 means we just split the 128x128 to 64x64
398 /* clang-format off */
399 static const struct {
400   PARTITION_CONTEXT above;
401   PARTITION_CONTEXT left;
402 } partition_context_lookup[BLOCK_SIZES_ALL] = {
403   { 31, 31 },  // 4X4   - {0b11111, 0b11111}
404   { 31, 30 },  // 4X8   - {0b11111, 0b11110}
405   { 30, 31 },  // 8X4   - {0b11110, 0b11111}
406   { 30, 30 },  // 8X8   - {0b11110, 0b11110}
407   { 30, 28 },  // 8X16  - {0b11110, 0b11100}
408   { 28, 30 },  // 16X8  - {0b11100, 0b11110}
409   { 28, 28 },  // 16X16 - {0b11100, 0b11100}
410   { 28, 24 },  // 16X32 - {0b11100, 0b11000}
411   { 24, 28 },  // 32X16 - {0b11000, 0b11100}
412   { 24, 24 },  // 32X32 - {0b11000, 0b11000}
413   { 24, 16 },  // 32X64 - {0b11000, 0b10000}
414   { 16, 24 },  // 64X32 - {0b10000, 0b11000}
415   { 16, 16 },  // 64X64 - {0b10000, 0b10000}
416   { 16, 0 },   // 64X128- {0b10000, 0b00000}
417   { 0, 16 },   // 128X64- {0b00000, 0b10000}
418   { 0, 0 },    // 128X128-{0b00000, 0b00000}
419   { 31, 28 },  // 4X16  - {0b11111, 0b11100}
420   { 28, 31 },  // 16X4  - {0b11100, 0b11111}
421   { 30, 24 },  // 8X32  - {0b11110, 0b11000}
422   { 24, 30 },  // 32X8  - {0b11000, 0b11110}
423   { 28, 16 },  // 16X64 - {0b11100, 0b10000}
424   { 16, 28 },  // 64X16 - {0b10000, 0b11100}
425 };
426 /* clang-format on */
427 
428 static const int intra_mode_context[INTRA_MODES] = {
429   0, 1, 2, 3, 4, 4, 4, 4, 3, 0, 1, 2, 0,
430 };
431 
432 // Note: this is also used in unit tests. So whenever one changes the table,
433 // the unit tests need to be changed accordingly.
434 static const int quant_dist_weight[4][2] = {
435   { 2, 3 }, { 2, 5 }, { 2, 7 }, { 1, MAX_FRAME_DISTANCE }
436 };
437 static const int quant_dist_lookup_table[2][4][2] = {
438   { { 9, 7 }, { 11, 5 }, { 12, 4 }, { 13, 3 } },
439   { { 7, 9 }, { 5, 11 }, { 4, 12 }, { 3, 13 } },
440 };
441 
442 #ifdef __cplusplus
443 }  // extern "C"
444 #endif
445 
446 #endif  // AOM_AV1_COMMON_COMMON_DATA_H_
447