reconintra.c 112 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
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.
Jingning Han's avatar
Jingning Han committed
10
11
 */

hui su's avatar
hui su committed
12
13
#include <math.h>

Yaowu Xu's avatar
Yaowu Xu committed
14
15
16
#include "./av1_rtcd.h"
#include "./aom_config.h"
#include "./aom_dsp_rtcd.h"
17
#include "aom_ports/system_state.h"
Jingning Han's avatar
Jingning Han committed
18

19
#if CONFIG_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
20
#include "aom_dsp/aom_dsp_common.h"
21
#endif  // CONFIG_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
22
#include "aom_mem/aom_mem.h"
23
#include "aom_ports/mem.h"
Yaowu Xu's avatar
Yaowu Xu committed
24
#include "aom_ports/aom_once.h"
25
26
#include "av1/common/reconintra.h"
#include "av1/common/onyxc_int.h"
Luc Trudeau's avatar
Luc Trudeau committed
27
28
29
#if CONFIG_CFL
#include "av1/common/cfl.h"
#endif
Jingning Han's avatar
Jingning Han committed
30

31
32
33
34
35
36
37
38
enum {
  NEED_LEFT = 1 << 1,
  NEED_ABOVE = 1 << 2,
  NEED_ABOVERIGHT = 1 << 3,
  NEED_ABOVELEFT = 1 << 4,
  NEED_BOTTOMLEFT = 1 << 5,
};

39
40
41
42
#if CONFIG_INTRA_EDGE
#define INTRA_EDGE_FILT 3
#define INTRA_EDGE_TAPS 5
#if CONFIG_INTRA_EDGE_UPSAMPLE
Joe Young's avatar
Joe Young committed
43
#define MAX_UPSAMPLE_SZ 16
Joe Young's avatar
Joe Young committed
44
45
#endif  // CONFIG_INTRA_EDGE_UPSAMPLE
#endif  // CONFIG_INTRA_EDGE
46

47
48
49
50
51
52
53
54
55
56
static const uint8_t extend_modes[INTRA_MODES] = {
  NEED_ABOVE | NEED_LEFT,                   // DC
  NEED_ABOVE,                               // V
  NEED_LEFT,                                // H
  NEED_ABOVE | NEED_ABOVERIGHT,             // D45
  NEED_LEFT | NEED_ABOVE | NEED_ABOVELEFT,  // D135
  NEED_LEFT | NEED_ABOVE | NEED_ABOVELEFT,  // D117
  NEED_LEFT | NEED_ABOVE | NEED_ABOVELEFT,  // D153
  NEED_LEFT | NEED_BOTTOMLEFT,              // D207
  NEED_ABOVE | NEED_ABOVERIGHT,             // D63
Urvang Joshi's avatar
Urvang Joshi committed
57
  NEED_LEFT | NEED_ABOVE,                   // SMOOTH
Urvang Joshi's avatar
Urvang Joshi committed
58
59
  NEED_LEFT | NEED_ABOVE,                   // SMOOTH_V
  NEED_LEFT | NEED_ABOVE,                   // SMOOTH_H
Urvang Joshi's avatar
Urvang Joshi committed
60
  NEED_LEFT | NEED_ABOVE | NEED_ABOVELEFT,  // PAETH
61
};
Jingning Han's avatar
Jingning Han committed
62

63
64
65
static const uint16_t orders_128x128[1] = { 0 };
static const uint16_t orders_128x64[2] = { 0, 1 };
static const uint16_t orders_64x128[2] = { 0, 1 };
66
67
68
69
70
71
72
73
74
75
76
77
78
static const uint16_t orders_64x64[4] = { 0, 1, 2, 3 };
static const uint16_t orders_64x32[8] = { 0, 2, 1, 3, 4, 6, 5, 7 };
static const uint16_t orders_32x64[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
static const uint16_t orders_32x32[16] = { 0, 1, 4,  5,  2,  3,  6,  7,
                                           8, 9, 12, 13, 10, 11, 14, 15 };
static const uint16_t orders_32x16[32] = { 0,  2,  8,  10, 1,  3,  9,  11,
                                           4,  6,  12, 14, 5,  7,  13, 15,
                                           16, 18, 24, 26, 17, 19, 25, 27,
                                           20, 22, 28, 30, 21, 23, 29, 31 };
static const uint16_t orders_16x32[32] = { 0,  1,  2,  3,  8,  9,  10, 11,
                                           4,  5,  6,  7,  12, 13, 14, 15,
                                           16, 17, 18, 19, 24, 25, 26, 27,
                                           20, 21, 22, 23, 28, 29, 30, 31 };
79
static const uint16_t orders_16x16[64] = {
clang-format's avatar
clang-format committed
80
81
82
  0,  1,  4,  5,  16, 17, 20, 21, 2,  3,  6,  7,  18, 19, 22, 23,
  8,  9,  12, 13, 24, 25, 28, 29, 10, 11, 14, 15, 26, 27, 30, 31,
  32, 33, 36, 37, 48, 49, 52, 53, 34, 35, 38, 39, 50, 51, 54, 55,
83
  40, 41, 44, 45, 56, 57, 60, 61, 42, 43, 46, 47, 58, 59, 62, 63
84
};
85
static const uint16_t orders_16x8[128] = {
clang-format's avatar
clang-format committed
86
87
88
89
90
91
92
  0,  2,  8,  10, 32,  34,  40,  42,  1,  3,  9,  11, 33,  35,  41,  43,
  4,  6,  12, 14, 36,  38,  44,  46,  5,  7,  13, 15, 37,  39,  45,  47,
  16, 18, 24, 26, 48,  50,  56,  58,  17, 19, 25, 27, 49,  51,  57,  59,
  20, 22, 28, 30, 52,  54,  60,  62,  21, 23, 29, 31, 53,  55,  61,  63,
  64, 66, 72, 74, 96,  98,  104, 106, 65, 67, 73, 75, 97,  99,  105, 107,
  68, 70, 76, 78, 100, 102, 108, 110, 69, 71, 77, 79, 101, 103, 109, 111,
  80, 82, 88, 90, 112, 114, 120, 122, 81, 83, 89, 91, 113, 115, 121, 123,
93
  84, 86, 92, 94, 116, 118, 124, 126, 85, 87, 93, 95, 117, 119, 125, 127
94
};
95
static const uint16_t orders_8x16[128] = {
clang-format's avatar
clang-format committed
96
97
98
99
100
  0,  1,  2,  3,  8,  9,  10, 11, 32,  33,  34,  35,  40,  41,  42,  43,
  4,  5,  6,  7,  12, 13, 14, 15, 36,  37,  38,  39,  44,  45,  46,  47,
  16, 17, 18, 19, 24, 25, 26, 27, 48,  49,  50,  51,  56,  57,  58,  59,
  20, 21, 22, 23, 28, 29, 30, 31, 52,  53,  54,  55,  60,  61,  62,  63,
  64, 65, 66, 67, 72, 73, 74, 75, 96,  97,  98,  99,  104, 105, 106, 107,
101
102
  68, 69, 70, 71, 76, 77, 78, 79, 100, 101, 102, 103, 108, 109, 110, 111,
  80, 81, 82, 83, 88, 89, 90, 91, 112, 113, 114, 115, 120, 121, 122, 123,
103
  84, 85, 86, 87, 92, 93, 94, 95, 116, 117, 118, 119, 124, 125, 126, 127
104
};
105
static const uint16_t orders_8x8[256] = {
clang-format's avatar
clang-format committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  0,   1,   4,   5,   16,  17,  20,  21,  64,  65,  68,  69,  80,  81,  84,
  85,  2,   3,   6,   7,   18,  19,  22,  23,  66,  67,  70,  71,  82,  83,
  86,  87,  8,   9,   12,  13,  24,  25,  28,  29,  72,  73,  76,  77,  88,
  89,  92,  93,  10,  11,  14,  15,  26,  27,  30,  31,  74,  75,  78,  79,
  90,  91,  94,  95,  32,  33,  36,  37,  48,  49,  52,  53,  96,  97,  100,
  101, 112, 113, 116, 117, 34,  35,  38,  39,  50,  51,  54,  55,  98,  99,
  102, 103, 114, 115, 118, 119, 40,  41,  44,  45,  56,  57,  60,  61,  104,
  105, 108, 109, 120, 121, 124, 125, 42,  43,  46,  47,  58,  59,  62,  63,
  106, 107, 110, 111, 122, 123, 126, 127, 128, 129, 132, 133, 144, 145, 148,
  149, 192, 193, 196, 197, 208, 209, 212, 213, 130, 131, 134, 135, 146, 147,
  150, 151, 194, 195, 198, 199, 210, 211, 214, 215, 136, 137, 140, 141, 152,
  153, 156, 157, 200, 201, 204, 205, 216, 217, 220, 221, 138, 139, 142, 143,
  154, 155, 158, 159, 202, 203, 206, 207, 218, 219, 222, 223, 160, 161, 164,
  165, 176, 177, 180, 181, 224, 225, 228, 229, 240, 241, 244, 245, 162, 163,
  166, 167, 178, 179, 182, 183, 226, 227, 230, 231, 242, 243, 246, 247, 168,
  169, 172, 173, 184, 185, 188, 189, 232, 233, 236, 237, 248, 249, 252, 253,
  170, 171, 174, 175, 186, 187, 190, 191, 234, 235, 238, 239, 250, 251, 254,
123
  255
124
};
125
#if CONFIG_EXT_PARTITION
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
static const uint16_t orders_4x8[512] = {
  0,   1,   2,   3,   8,   9,   10,  11,  32,  33,  34,  35,  40,  41,  42,
  43,  128, 129, 130, 131, 136, 137, 138, 139, 160, 161, 162, 163, 168, 169,
  170, 171, 4,   5,   6,   7,   12,  13,  14,  15,  36,  37,  38,  39,  44,
  45,  46,  47,  132, 133, 134, 135, 140, 141, 142, 143, 164, 165, 166, 167,
  172, 173, 174, 175, 16,  17,  18,  19,  24,  25,  26,  27,  48,  49,  50,
  51,  56,  57,  58,  59,  144, 145, 146, 147, 152, 153, 154, 155, 176, 177,
  178, 179, 184, 185, 186, 187, 20,  21,  22,  23,  28,  29,  30,  31,  52,
  53,  54,  55,  60,  61,  62,  63,  148, 149, 150, 151, 156, 157, 158, 159,
  180, 181, 182, 183, 188, 189, 190, 191, 64,  65,  66,  67,  72,  73,  74,
  75,  96,  97,  98,  99,  104, 105, 106, 107, 192, 193, 194, 195, 200, 201,
  202, 203, 224, 225, 226, 227, 232, 233, 234, 235, 68,  69,  70,  71,  76,
  77,  78,  79,  100, 101, 102, 103, 108, 109, 110, 111, 196, 197, 198, 199,
  204, 205, 206, 207, 228, 229, 230, 231, 236, 237, 238, 239, 80,  81,  82,
  83,  88,  89,  90,  91,  112, 113, 114, 115, 120, 121, 122, 123, 208, 209,
  210, 211, 216, 217, 218, 219, 240, 241, 242, 243, 248, 249, 250, 251, 84,
  85,  86,  87,  92,  93,  94,  95,  116, 117, 118, 119, 124, 125, 126, 127,
  212, 213, 214, 215, 220, 221, 222, 223, 244, 245, 246, 247, 252, 253, 254,
  255, 256, 257, 258, 259, 264, 265, 266, 267, 288, 289, 290, 291, 296, 297,
  298, 299, 384, 385, 386, 387, 392, 393, 394, 395, 416, 417, 418, 419, 424,
  425, 426, 427, 260, 261, 262, 263, 268, 269, 270, 271, 292, 293, 294, 295,
  300, 301, 302, 303, 388, 389, 390, 391, 396, 397, 398, 399, 420, 421, 422,
  423, 428, 429, 430, 431, 272, 273, 274, 275, 280, 281, 282, 283, 304, 305,
  306, 307, 312, 313, 314, 315, 400, 401, 402, 403, 408, 409, 410, 411, 432,
  433, 434, 435, 440, 441, 442, 443, 276, 277, 278, 279, 284, 285, 286, 287,
  308, 309, 310, 311, 316, 317, 318, 319, 404, 405, 406, 407, 412, 413, 414,
  415, 436, 437, 438, 439, 444, 445, 446, 447, 320, 321, 322, 323, 328, 329,
  330, 331, 352, 353, 354, 355, 360, 361, 362, 363, 448, 449, 450, 451, 456,
  457, 458, 459, 480, 481, 482, 483, 488, 489, 490, 491, 324, 325, 326, 327,
  332, 333, 334, 335, 356, 357, 358, 359, 364, 365, 366, 367, 452, 453, 454,
  455, 460, 461, 462, 463, 484, 485, 486, 487, 492, 493, 494, 495, 336, 337,
  338, 339, 344, 345, 346, 347, 368, 369, 370, 371, 376, 377, 378, 379, 464,
  465, 466, 467, 472, 473, 474, 475, 496, 497, 498, 499, 504, 505, 506, 507,
  340, 341, 342, 343, 348, 349, 350, 351, 372, 373, 374, 375, 380, 381, 382,
  383, 468, 469, 470, 471, 476, 477, 478, 479, 500, 501, 502, 503, 508, 509,
161
  510, 511
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
};
static const uint16_t orders_8x4[512] = {
  0,   2,   8,   10,  32,  34,  40,  42,  128, 130, 136, 138, 160, 162, 168,
  170, 1,   3,   9,   11,  33,  35,  41,  43,  129, 131, 137, 139, 161, 163,
  169, 171, 4,   6,   12,  14,  36,  38,  44,  46,  132, 134, 140, 142, 164,
  166, 172, 174, 5,   7,   13,  15,  37,  39,  45,  47,  133, 135, 141, 143,
  165, 167, 173, 175, 16,  18,  24,  26,  48,  50,  56,  58,  144, 146, 152,
  154, 176, 178, 184, 186, 17,  19,  25,  27,  49,  51,  57,  59,  145, 147,
  153, 155, 177, 179, 185, 187, 20,  22,  28,  30,  52,  54,  60,  62,  148,
  150, 156, 158, 180, 182, 188, 190, 21,  23,  29,  31,  53,  55,  61,  63,
  149, 151, 157, 159, 181, 183, 189, 191, 64,  66,  72,  74,  96,  98,  104,
  106, 192, 194, 200, 202, 224, 226, 232, 234, 65,  67,  73,  75,  97,  99,
  105, 107, 193, 195, 201, 203, 225, 227, 233, 235, 68,  70,  76,  78,  100,
  102, 108, 110, 196, 198, 204, 206, 228, 230, 236, 238, 69,  71,  77,  79,
  101, 103, 109, 111, 197, 199, 205, 207, 229, 231, 237, 239, 80,  82,  88,
  90,  112, 114, 120, 122, 208, 210, 216, 218, 240, 242, 248, 250, 81,  83,
  89,  91,  113, 115, 121, 123, 209, 211, 217, 219, 241, 243, 249, 251, 84,
  86,  92,  94,  116, 118, 124, 126, 212, 214, 220, 222, 244, 246, 252, 254,
  85,  87,  93,  95,  117, 119, 125, 127, 213, 215, 221, 223, 245, 247, 253,
  255, 256, 258, 264, 266, 288, 290, 296, 298, 384, 386, 392, 394, 416, 418,
  424, 426, 257, 259, 265, 267, 289, 291, 297, 299, 385, 387, 393, 395, 417,
  419, 425, 427, 260, 262, 268, 270, 292, 294, 300, 302, 388, 390, 396, 398,
  420, 422, 428, 430, 261, 263, 269, 271, 293, 295, 301, 303, 389, 391, 397,
  399, 421, 423, 429, 431, 272, 274, 280, 282, 304, 306, 312, 314, 400, 402,
  408, 410, 432, 434, 440, 442, 273, 275, 281, 283, 305, 307, 313, 315, 401,
  403, 409, 411, 433, 435, 441, 443, 276, 278, 284, 286, 308, 310, 316, 318,
  404, 406, 412, 414, 436, 438, 444, 446, 277, 279, 285, 287, 309, 311, 317,
  319, 405, 407, 413, 415, 437, 439, 445, 447, 320, 322, 328, 330, 352, 354,
  360, 362, 448, 450, 456, 458, 480, 482, 488, 490, 321, 323, 329, 331, 353,
  355, 361, 363, 449, 451, 457, 459, 481, 483, 489, 491, 324, 326, 332, 334,
  356, 358, 364, 366, 452, 454, 460, 462, 484, 486, 492, 494, 325, 327, 333,
  335, 357, 359, 365, 367, 453, 455, 461, 463, 485, 487, 493, 495, 336, 338,
  344, 346, 368, 370, 376, 378, 464, 466, 472, 474, 496, 498, 504, 506, 337,
  339, 345, 347, 369, 371, 377, 379, 465, 467, 473, 475, 497, 499, 505, 507,
  340, 342, 348, 350, 372, 374, 380, 382, 468, 470, 476, 478, 500, 502, 508,
  510, 341, 343, 349, 351, 373, 375, 381, 383, 469, 471, 477, 479, 501, 503,
198
  509, 511
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
};
static const uint16_t orders_4x4[1024] = {
  0,    1,    4,    5,    16,   17,   20,   21,   64,   65,   68,   69,   80,
  81,   84,   85,   256,  257,  260,  261,  272,  273,  276,  277,  320,  321,
  324,  325,  336,  337,  340,  341,  2,    3,    6,    7,    18,   19,   22,
  23,   66,   67,   70,   71,   82,   83,   86,   87,   258,  259,  262,  263,
  274,  275,  278,  279,  322,  323,  326,  327,  338,  339,  342,  343,  8,
  9,    12,   13,   24,   25,   28,   29,   72,   73,   76,   77,   88,   89,
  92,   93,   264,  265,  268,  269,  280,  281,  284,  285,  328,  329,  332,
  333,  344,  345,  348,  349,  10,   11,   14,   15,   26,   27,   30,   31,
  74,   75,   78,   79,   90,   91,   94,   95,   266,  267,  270,  271,  282,
  283,  286,  287,  330,  331,  334,  335,  346,  347,  350,  351,  32,   33,
  36,   37,   48,   49,   52,   53,   96,   97,   100,  101,  112,  113,  116,
  117,  288,  289,  292,  293,  304,  305,  308,  309,  352,  353,  356,  357,
  368,  369,  372,  373,  34,   35,   38,   39,   50,   51,   54,   55,   98,
  99,   102,  103,  114,  115,  118,  119,  290,  291,  294,  295,  306,  307,
  310,  311,  354,  355,  358,  359,  370,  371,  374,  375,  40,   41,   44,
  45,   56,   57,   60,   61,   104,  105,  108,  109,  120,  121,  124,  125,
  296,  297,  300,  301,  312,  313,  316,  317,  360,  361,  364,  365,  376,
  377,  380,  381,  42,   43,   46,   47,   58,   59,   62,   63,   106,  107,
  110,  111,  122,  123,  126,  127,  298,  299,  302,  303,  314,  315,  318,
  319,  362,  363,  366,  367,  378,  379,  382,  383,  128,  129,  132,  133,
  144,  145,  148,  149,  192,  193,  196,  197,  208,  209,  212,  213,  384,
  385,  388,  389,  400,  401,  404,  405,  448,  449,  452,  453,  464,  465,
  468,  469,  130,  131,  134,  135,  146,  147,  150,  151,  194,  195,  198,
  199,  210,  211,  214,  215,  386,  387,  390,  391,  402,  403,  406,  407,
  450,  451,  454,  455,  466,  467,  470,  471,  136,  137,  140,  141,  152,
  153,  156,  157,  200,  201,  204,  205,  216,  217,  220,  221,  392,  393,
  396,  397,  408,  409,  412,  413,  456,  457,  460,  461,  472,  473,  476,
  477,  138,  139,  142,  143,  154,  155,  158,  159,  202,  203,  206,  207,
  218,  219,  222,  223,  394,  395,  398,  399,  410,  411,  414,  415,  458,
  459,  462,  463,  474,  475,  478,  479,  160,  161,  164,  165,  176,  177,
  180,  181,  224,  225,  228,  229,  240,  241,  244,  245,  416,  417,  420,
  421,  432,  433,  436,  437,  480,  481,  484,  485,  496,  497,  500,  501,
  162,  163,  166,  167,  178,  179,  182,  183,  226,  227,  230,  231,  242,
  243,  246,  247,  418,  419,  422,  423,  434,  435,  438,  439,  482,  483,
  486,  487,  498,  499,  502,  503,  168,  169,  172,  173,  184,  185,  188,
  189,  232,  233,  236,  237,  248,  249,  252,  253,  424,  425,  428,  429,
  440,  441,  444,  445,  488,  489,  492,  493,  504,  505,  508,  509,  170,
  171,  174,  175,  186,  187,  190,  191,  234,  235,  238,  239,  250,  251,
  254,  255,  426,  427,  430,  431,  442,  443,  446,  447,  490,  491,  494,
  495,  506,  507,  510,  511,  512,  513,  516,  517,  528,  529,  532,  533,
  576,  577,  580,  581,  592,  593,  596,  597,  768,  769,  772,  773,  784,
  785,  788,  789,  832,  833,  836,  837,  848,  849,  852,  853,  514,  515,
  518,  519,  530,  531,  534,  535,  578,  579,  582,  583,  594,  595,  598,
  599,  770,  771,  774,  775,  786,  787,  790,  791,  834,  835,  838,  839,
  850,  851,  854,  855,  520,  521,  524,  525,  536,  537,  540,  541,  584,
  585,  588,  589,  600,  601,  604,  605,  776,  777,  780,  781,  792,  793,
  796,  797,  840,  841,  844,  845,  856,  857,  860,  861,  522,  523,  526,
  527,  538,  539,  542,  543,  586,  587,  590,  591,  602,  603,  606,  607,
  778,  779,  782,  783,  794,  795,  798,  799,  842,  843,  846,  847,  858,
  859,  862,  863,  544,  545,  548,  549,  560,  561,  564,  565,  608,  609,
  612,  613,  624,  625,  628,  629,  800,  801,  804,  805,  816,  817,  820,
  821,  864,  865,  868,  869,  880,  881,  884,  885,  546,  547,  550,  551,
  562,  563,  566,  567,  610,  611,  614,  615,  626,  627,  630,  631,  802,
  803,  806,  807,  818,  819,  822,  823,  866,  867,  870,  871,  882,  883,
  886,  887,  552,  553,  556,  557,  568,  569,  572,  573,  616,  617,  620,
  621,  632,  633,  636,  637,  808,  809,  812,  813,  824,  825,  828,  829,
  872,  873,  876,  877,  888,  889,  892,  893,  554,  555,  558,  559,  570,
  571,  574,  575,  618,  619,  622,  623,  634,  635,  638,  639,  810,  811,
  814,  815,  826,  827,  830,  831,  874,  875,  878,  879,  890,  891,  894,
  895,  640,  641,  644,  645,  656,  657,  660,  661,  704,  705,  708,  709,
  720,  721,  724,  725,  896,  897,  900,  901,  912,  913,  916,  917,  960,
  961,  964,  965,  976,  977,  980,  981,  642,  643,  646,  647,  658,  659,
  662,  663,  706,  707,  710,  711,  722,  723,  726,  727,  898,  899,  902,
  903,  914,  915,  918,  919,  962,  963,  966,  967,  978,  979,  982,  983,
  648,  649,  652,  653,  664,  665,  668,  669,  712,  713,  716,  717,  728,
  729,  732,  733,  904,  905,  908,  909,  920,  921,  924,  925,  968,  969,
  972,  973,  984,  985,  988,  989,  650,  651,  654,  655,  666,  667,  670,
  671,  714,  715,  718,  719,  730,  731,  734,  735,  906,  907,  910,  911,
  922,  923,  926,  927,  970,  971,  974,  975,  986,  987,  990,  991,  672,
  673,  676,  677,  688,  689,  692,  693,  736,  737,  740,  741,  752,  753,
  756,  757,  928,  929,  932,  933,  944,  945,  948,  949,  992,  993,  996,
  997,  1008, 1009, 1012, 1013, 674,  675,  678,  679,  690,  691,  694,  695,
  738,  739,  742,  743,  754,  755,  758,  759,  930,  931,  934,  935,  946,
  947,  950,  951,  994,  995,  998,  999,  1010, 1011, 1014, 1015, 680,  681,
  684,  685,  696,  697,  700,  701,  744,  745,  748,  749,  760,  761,  764,
  765,  936,  937,  940,  941,  952,  953,  956,  957,  1000, 1001, 1004, 1005,
  1016, 1017, 1020, 1021, 682,  683,  686,  687,  698,  699,  702,  703,  746,
  747,  750,  751,  762,  763,  766,  767,  938,  939,  942,  943,  954,  955,
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
  958,  959,  1002, 1003, 1006, 1007, 1018, 1019, 1022, 1023
};
#endif

#if CONFIG_EXT_PARTITION_TYPES
static const uint16_t orders_128x32[4] = { 0, 1, 2, 3 };
static const uint16_t orders_64x16[16] = { 0, 4,  1, 5,  2,  6,  3,  7,
                                           8, 12, 9, 13, 10, 14, 11, 15 };
static const uint16_t orders_32x128[4] = { 0, 1, 2, 3 };
static const uint16_t orders_32x8[64] = {
  0,  4,  16, 20, 1,  5,  17, 21, 2,  6,  18, 22, 3,  7,  19, 23,
  8,  12, 24, 28, 9,  13, 25, 29, 10, 14, 26, 30, 11, 15, 27, 31,
  32, 36, 48, 52, 33, 37, 49, 53, 34, 38, 50, 54, 35, 39, 51, 55,
  40, 44, 56, 60, 41, 45, 57, 61, 42, 46, 58, 62, 43, 47, 59, 63
};
static const uint16_t orders_8x32[64] = {
  0,  1,  2,  3,  4,  5,  6,  7,  16, 17, 18, 19, 20, 21, 22, 23,
  8,  9,  10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31,
  32, 33, 34, 35, 36, 37, 38, 39, 48, 49, 50, 51, 52, 53, 54, 55,
  40, 41, 42, 43, 44, 45, 46, 47, 56, 57, 58, 59, 60, 61, 62, 63
};
static const uint16_t orders_16x64[16] = { 0, 1, 2,  3,  4,  5,  6,  7,
                                           8, 9, 10, 11, 12, 13, 14, 15 };
#if CONFIG_EXT_PARTITION
static const uint16_t orders_16x4[256] = {
  0,   4,   16,  20,  64,  68,  80,  84,  1,   5,   17,  21,  65,  69,  81,
  85,  2,   6,   18,  22,  66,  70,  82,  86,  3,   7,   19,  23,  67,  71,
  83,  87,  8,   12,  24,  28,  72,  76,  88,  92,  9,   13,  25,  29,  73,
  77,  89,  93,  10,  14,  26,  30,  74,  78,  90,  94,  11,  15,  27,  31,
  75,  79,  91,  95,  32,  36,  48,  52,  96,  100, 112, 116, 33,  37,  49,
  53,  97,  101, 113, 117, 34,  38,  50,  54,  98,  102, 114, 118, 35,  39,
  51,  55,  99,  103, 115, 119, 40,  44,  56,  60,  104, 108, 120, 124, 41,
  45,  57,  61,  105, 109, 121, 125, 42,  46,  58,  62,  106, 110, 122, 126,
  43,  47,  59,  63,  107, 111, 123, 127, 128, 132, 144, 148, 192, 196, 208,
  212, 129, 133, 145, 149, 193, 197, 209, 213, 130, 134, 146, 150, 194, 198,
  210, 214, 131, 135, 147, 151, 195, 199, 211, 215, 136, 140, 152, 156, 200,
  204, 216, 220, 137, 141, 153, 157, 201, 205, 217, 221, 138, 142, 154, 158,
  202, 206, 218, 222, 139, 143, 155, 159, 203, 207, 219, 223, 160, 164, 176,
  180, 224, 228, 240, 244, 161, 165, 177, 181, 225, 229, 241, 245, 162, 166,
  178, 182, 226, 230, 242, 246, 163, 167, 179, 183, 227, 231, 243, 247, 168,
  172, 184, 188, 232, 236, 248, 252, 169, 173, 185, 189, 233, 237, 249, 253,
  170, 174, 186, 190, 234, 238, 250, 254, 171, 175, 187, 191, 235, 239, 251,
  255
};
static const uint16_t orders_4x16[256] = {
  0,   1,   2,   3,   4,   5,   6,   7,   16,  17,  18,  19,  20,  21,  22,
  23,  64,  65,  66,  67,  68,  69,  70,  71,  80,  81,  82,  83,  84,  85,
  86,  87,  8,   9,   10,  11,  12,  13,  14,  15,  24,  25,  26,  27,  28,
  29,  30,  31,  72,  73,  74,  75,  76,  77,  78,  79,  88,  89,  90,  91,
  92,  93,  94,  95,  32,  33,  34,  35,  36,  37,  38,  39,  48,  49,  50,
  51,  52,  53,  54,  55,  96,  97,  98,  99,  100, 101, 102, 103, 112, 113,
  114, 115, 116, 117, 118, 119, 40,  41,  42,  43,  44,  45,  46,  47,  56,
  57,  58,  59,  60,  61,  62,  63,  104, 105, 106, 107, 108, 109, 110, 111,
  120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
  135, 144, 145, 146, 147, 148, 149, 150, 151, 192, 193, 194, 195, 196, 197,
  198, 199, 208, 209, 210, 211, 212, 213, 214, 215, 136, 137, 138, 139, 140,
  141, 142, 143, 152, 153, 154, 155, 156, 157, 158, 159, 200, 201, 202, 203,
  204, 205, 206, 207, 216, 217, 218, 219, 220, 221, 222, 223, 160, 161, 162,
  163, 164, 165, 166, 167, 176, 177, 178, 179, 180, 181, 182, 183, 224, 225,
  226, 227, 228, 229, 230, 231, 240, 241, 242, 243, 244, 245, 246, 247, 168,
  169, 170, 171, 172, 173, 174, 175, 184, 185, 186, 187, 188, 189, 190, 191,
  232, 233, 234, 235, 236, 237, 238, 239, 248, 249, 250, 251, 252, 253, 254,
  255
};
#endif  // CONFIG_EXT_PARTITION
#endif  // CONFIG_EXT_PARTITION_TYPES

#if CONFIG_EXT_PARTITION_TYPES && !CONFIG_EXT_PARTITION_TYPES_AB
static const uint16_t orders_vert_128x128[1] = { 0 };
static const uint16_t orders_vert_64x64[4] = { 0, 2, 1, 3 };
static const uint16_t orders_vert_32x32[16] = { 0, 2,  4,  6,  1, 3,  5,  7,
                                                8, 10, 12, 14, 9, 11, 13, 15 };
static const uint16_t orders_vert_16x16[64] = {
  0,  2,  4,  6,  16, 18, 20, 22, 1,  3,  5,  7,  17, 19, 21, 23,
  8,  10, 12, 14, 24, 26, 28, 30, 9,  11, 13, 15, 25, 27, 29, 31,
  32, 34, 36, 38, 48, 50, 52, 54, 33, 35, 37, 39, 49, 51, 53, 55,
  40, 42, 44, 46, 56, 58, 60, 62, 41, 43, 45, 47, 57, 59, 61, 63
};
#if CONFIG_EXT_PARTITION
static const uint16_t orders_vert_8x8[256] = {
  0,   2,   4,   6,   16,  18,  20,  22,  64,  66,  68,  70,  80,  82,  84,
  86,  1,   3,   5,   7,   17,  19,  21,  23,  65,  67,  69,  71,  81,  83,
  85,  87,  8,   10,  12,  14,  24,  26,  28,  30,  72,  74,  76,  78,  88,
  90,  92,  94,  9,   11,  13,  15,  25,  27,  29,  31,  73,  75,  77,  79,
  89,  91,  93,  95,  32,  34,  36,  38,  48,  50,  52,  54,  96,  98,  100,
  102, 112, 114, 116, 118, 33,  35,  37,  39,  49,  51,  53,  55,  97,  99,
  101, 103, 113, 115, 117, 119, 40,  42,  44,  46,  56,  58,  60,  62,  104,
  106, 108, 110, 120, 122, 124, 126, 41,  43,  45,  47,  57,  59,  61,  63,
  105, 107, 109, 111, 121, 123, 125, 127, 128, 130, 132, 134, 144, 146, 148,
  150, 192, 194, 196, 198, 208, 210, 212, 214, 129, 131, 133, 135, 145, 147,
  149, 151, 193, 195, 197, 199, 209, 211, 213, 215, 136, 138, 140, 142, 152,
  154, 156, 158, 200, 202, 204, 206, 216, 218, 220, 222, 137, 139, 141, 143,
  153, 155, 157, 159, 201, 203, 205, 207, 217, 219, 221, 223, 160, 162, 164,
  166, 176, 178, 180, 182, 224, 226, 228, 230, 240, 242, 244, 246, 161, 163,
  165, 167, 177, 179, 181, 183, 225, 227, 229, 231, 241, 243, 245, 247, 168,
  170, 172, 174, 184, 186, 188, 190, 232, 234, 236, 238, 248, 250, 252, 254,
  169, 171, 173, 175, 185, 187, 189, 191, 233, 235, 237, 239, 249, 251, 253,
  255
377
};
378
#endif  // CONFIG_EXT_PARTITION
379
#endif  // CONFIG_EXT_PARTITION_TYPES && !CONFIG_EXT_PARTITION_TYPES_AB
380

381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// The orders_* tables encode the order in which we visit blocks of the given
// size. For example, orders_32x32 has (128/32)^2 = 4^2 = 16 entries that
// correspond to the 32x32 blocks in a 128x128 superblock (in raster order). If
// an entry at position i is less than the entry at position j, that means that
// the block at position i is visited before that at position j if the
// superblock is divided by repeated PARTITION_SPLITs down to 32x32 blocks.
//
// The other tables are similar. Those for non-square block sizes assume that
// the superblock was partition with PARTITION_SPLIT until the square block size
// with the larger of their dimensions, then there was a horizontal or vertical
// partition to generate their size. For example, the orders_16x32 table assumes
// that the superblock was divided by PARTITION_SPLIT down to 32x32 blocks, then
// each was partitioned by a PARTITION_VERT into two 16x32 blocks.
//
// These tables can be used to calculate whether one block has been decoded
// before another. Since PARTITION_SPLIT is the only recursive partitioning
// operation, it doesn't actually matter whether the other block is the same
// size as the current one: comparing entries in the table will still correctly
// answer the question "has the other one been decode already?".
//
// Also note that this works correctly for the mixed horizontal partitions
// (PARTITION_HORZ_A and PARTITION_HORZ_B): if you look up the smaller square
// block size, the rectangle will be treated as two blocks that are decoded
// consecutively (before or after this block). If you look up the rectangle, the
// two squares will be treated as a single rectangle and, again, they were
// decoded together before or after this block.
407
#if CONFIG_EXT_PARTITION
clang-format's avatar
clang-format committed
408
/* clang-format off */
409
static const uint16_t *const orders[BLOCK_SIZES_ALL] = {
Jingning Han's avatar
Jingning Han committed
410
  // 2X2,         2X4,            4X2
411
  NULL,           NULL,           NULL,
412
413
414
415
  //                              4X4
                                  orders_4x4,
  // 4X8,         8X4,            8X8
  orders_4x8,     orders_8x4,     orders_8x8,
416
417
418
419
420
421
422
  // 8X16,        16X8,           16X16
  orders_8x16,    orders_16x8,    orders_16x16,
  // 16X32,       32X16,          32X32
  orders_16x32,   orders_32x16,   orders_32x32,
  // 32X64,       64X32,          64X64
  orders_32x64,   orders_64x32,   orders_64x64,
  // 64x128,      128x64,         128x128
423
  orders_64x128,  orders_128x64,  orders_128x128,
424
#if CONFIG_EXT_PARTITION_TYPES
425
426
  // 4x16,        16x4,           8x32
  orders_4x16,    orders_16x4,    orders_8x32,
427
  // 32x8,        16x64,          64x16
428
429
430
  orders_32x8,    orders_16x64,   orders_64x16,
  // 32x128,      128x32
  orders_32x128,  orders_128x32
431
432
433
434
435
436
437
438
#else
  // 4x16,        16x4,           8x32
  NULL,           NULL,           NULL,
  // 32x8,        16x64,          64x16
  NULL,           NULL,           NULL,
  // 32x128,      128x32
  NULL,           NULL
#endif
439
};
clang-format's avatar
clang-format committed
440
/* clang-format on */
441
#else
clang-format's avatar
clang-format committed
442
/* clang-format off */
443
static const uint16_t *const orders[BLOCK_SIZES_ALL] = {
Jingning Han's avatar
Jingning Han committed
444
  // 2X2,         2X4,            4X2
445
  NULL,           NULL,           NULL,
446
447
448
449
  //                              4X4
                                  orders_8x8,
  // 4X8,         8X4,            8X8
  orders_8x16,    orders_16x8,    orders_16x16,
450
451
452
453
454
  // 8X16,        16X8,           16X16
  orders_16x32,   orders_32x16,   orders_32x32,
  // 16X32,       32X16,          32X32
  orders_32x64,   orders_64x32,   orders_64x64,
  // 32X64,       64X32,          64X64
455
  orders_64x128,  orders_128x64,  orders_128x128,
456
#if CONFIG_EXT_PARTITION_TYPES
457
458
  // 4x16,        16x4,           8x32
  orders_8x32,    orders_32x8,    orders_16x64,
459
460
  // 32x8,        16x64,          64x16
  orders_64x16,   orders_32x128,  orders_128x32
461
462
463
464
465
466
#else
  // 4x16,        16x4,           8x32
  NULL,           NULL,           NULL,
  // 32x8,        16x64,          64x16
  NULL,           NULL,           NULL
#endif
467
};
clang-format's avatar
clang-format committed
468
/* clang-format on */
469
470
#endif  // CONFIG_EXT_PARTITION

471
#if CONFIG_EXT_PARTITION_TYPES && !CONFIG_EXT_PARTITION_TYPES_AB
472
473
474
475
476
477
478
479
480
// The orders_vert_* tables are like the orders_* tables above, but describe the
// order we visit square blocks when doing a PARTITION_VERT_A or
// PARTITION_VERT_B. This is the same order as normal except for on the last
// split where we go vertically (TL, BL, TR, BR). We treat the rectangular block
// as a pair of squares, which means that these tables work correctly for both
// mixed vertical partition types.
//
// There are tables for each of the square sizes. Vertical rectangles (like
// BLOCK_16X32) use their respective "non-vert" table
481
#if CONFIG_EXT_PARTITION
clang-format's avatar
clang-format committed
482
/* clang-format off */
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
static const uint16_t *const orders_vert[BLOCK_SIZES] = {
  // 2X2,        2X4,    4X2
  NULL,          NULL,   NULL,
  //                     4X4
                         NULL,
  // 4X8,        8X4,    8X8
  NULL,          NULL,   orders_vert_8x8,
  // 8X16,       16X8,   16X16
  orders_8x16,   NULL,   orders_vert_16x16,
  // 16X32,      32X16,  32X32
  orders_16x32,  NULL,   orders_vert_32x32,
  // 32X64,      64X32,  64X64
  orders_32x64,  NULL,   orders_vert_64x64,
  // 64x128,     128x64, 128x128
  orders_64x128, NULL,   orders_vert_128x128,
498
};
clang-format's avatar
clang-format committed
499
/* clang-format on */
500
#else
clang-format's avatar
clang-format committed
501
/* clang-format off */
502
503
504
505
506
507
508
509
510
511
512
513
514
static const uint16_t *const orders_vert[BLOCK_SIZES] = {
  // 2X2,        2X4,    4X2
  NULL,          NULL,   NULL,
  //                     4X4
                         NULL,
  // 4X8,        8X4,    8X8
  NULL,          NULL,   orders_vert_16x16,
  // 8X16,       16X8,   16X16
  orders_16x32,  NULL,   orders_vert_32x32,
  // 16X32,      32X16,  32X32
  orders_32x64,  NULL,   orders_vert_64x64,
  // 32X64,      64X32,  64X64
  orders_64x128, NULL,   orders_vert_128x128,
515
};
clang-format's avatar
clang-format committed
516
/* clang-format on */
517
#endif  // CONFIG_EXT_PARTITION
518
#endif  // CONFIG_EXT_PARTITION_TYPES && !CONFIG_EXT_PARTITION_TYPES_AB
519

520
521
522
static const uint16_t *get_order_table(PARTITION_TYPE partition,
                                       BLOCK_SIZE bsize) {
  const uint16_t *ret = NULL;
523
#if CONFIG_EXT_PARTITION_TYPES && !CONFIG_EXT_PARTITION_TYPES_AB
524
525
526
527
528
529
530
531
532
533
  // If this is a mixed vertical partition, look up bsize in orders_vert.
  if (partition == PARTITION_VERT_A || partition == PARTITION_VERT_B) {
    assert(bsize < BLOCK_SIZES);
    ret = orders_vert[bsize];
  } else {
    ret = orders[bsize];
  }
#else
  (void)partition;
  ret = orders[bsize];
534
#endif  // CONFIG_EXT_PARTITION_TYPES && !CONFIG_EXT_PARTITION_TYPES_AB
535
536
537
538
539
540
541
542
543

  assert(ret);
  return ret;
}

static int has_top_right(const AV1_COMMON *cm, BLOCK_SIZE bsize, int mi_row,
                         int mi_col, int top_available, int right_available,
                         PARTITION_TYPE partition, TX_SIZE txsz, int row_off,
                         int col_off, int ss_x) {
544
545
546
547
548
  if (!top_available || !right_available) return 0;

  const int bw_unit = block_size_wide[bsize] >> tx_size_wide_log2[0];
  const int plane_bw_unit = AOMMAX(bw_unit >> ss_x, 1);
  const int top_right_count_unit = tx_size_wide_unit[txsz];
549

550
  if (row_off > 0) {  // Just need to check if enough pixels on the right.
551
552
553
554
555
#if CONFIG_EXT_PARTITION
    if (col_off + top_right_count_unit >=
        (block_size_wide[BLOCK_64X64] >> (tx_size_wide_log2[0] + ss_x)))
      return 0;
#endif
556
557
558
559
    return col_off + top_right_count_unit < plane_bw_unit;
  } else {
    // All top-right pixels are in the block above, which is already available.
    if (col_off + top_right_count_unit < plane_bw_unit) return 1;
560

561
562
    const int bw_in_mi_log2 = mi_width_log2_lookup[bsize];
    const int bh_in_mi_log2 = mi_height_log2_lookup[bsize];
563
564
565
    const int sb_mi_size = mi_size_high[cm->sb_size];
    const int blk_row_in_sb = (mi_row & (sb_mi_size - 1)) >> bh_in_mi_log2;
    const int blk_col_in_sb = (mi_col & (sb_mi_size - 1)) >> bw_in_mi_log2;
566

567
568
569
    // Top row of superblock: so top-right pixels are in the top and/or
    // top-right superblocks, both of which are already available.
    if (blk_row_in_sb == 0) return 1;
570

571
572
    // Rightmost column of superblock (and not the top row): so top-right pixels
    // fall in the right superblock, which is not available yet.
573
    if (((blk_col_in_sb + 1) << bw_in_mi_log2) >= sb_mi_size) return 0;
574

575
576
    // General case (neither top row nor rightmost column): check if the
    // top-right block is coded before the current block.
577
    const uint16_t *const order = get_order_table(partition, bsize);
578
579
580
581
582
583
584
585
586
    const int this_blk_index =
        ((blk_row_in_sb + 0) << (MAX_MIB_SIZE_LOG2 - bw_in_mi_log2)) +
        blk_col_in_sb + 0;
    const uint16_t this_blk_order = order[this_blk_index];
    const int tr_blk_index =
        ((blk_row_in_sb - 1) << (MAX_MIB_SIZE_LOG2 - bw_in_mi_log2)) +
        blk_col_in_sb + 1;
    const uint16_t tr_blk_order = order[tr_blk_index];
    return tr_blk_order < this_blk_order;
587
588
589
  }
}

590
591
static int has_bottom_left(const AV1_COMMON *cm, BLOCK_SIZE bsize, int mi_row,
                           int mi_col, int bottom_available, int left_available,
592
593
                           PARTITION_TYPE partition, TX_SIZE txsz, int row_off,
                           int col_off, int ss_y) {
594
595
596
597
  if (!bottom_available || !left_available) return 0;

  if (col_off > 0) {
    // Bottom-left pixels are in the bottom-left block, which is not available.
598
599
    return 0;
  } else {
600
601
602
    const int bh_unit = block_size_high[bsize] >> tx_size_high_log2[0];
    const int plane_bh_unit = AOMMAX(bh_unit >> ss_y, 1);
    const int bottom_left_count_unit = tx_size_high_unit[txsz];
603

604
605
606
607
608
    // All bottom-left pixels are in the left block, which is already available.
    if (row_off + bottom_left_count_unit < plane_bh_unit) return 1;

    const int bw_in_mi_log2 = mi_width_log2_lookup[bsize];
    const int bh_in_mi_log2 = mi_height_log2_lookup[bsize];
609
610
611
    const int sb_mi_size = mi_size_high[cm->sb_size];
    const int blk_row_in_sb = (mi_row & (sb_mi_size - 1)) >> bh_in_mi_log2;
    const int blk_col_in_sb = (mi_col & (sb_mi_size - 1)) >> bw_in_mi_log2;
612
613
614
615
616

    // Leftmost column of superblock: so bottom-left pixels maybe in the left
    // and/or bottom-left superblocks. But only the left superblock is
    // available, so check if all required pixels fall in that superblock.
    if (blk_col_in_sb == 0) {
617
618
619
620
      const int blk_start_row_off = blk_row_in_sb
                                        << (bh_in_mi_log2 + MI_SIZE_LOG2 -
                                            tx_size_wide_log2[0]) >>
                                    ss_y;
621
      const int row_off_in_sb = blk_start_row_off + row_off;
622
      const int sb_height_unit =
623
          sb_mi_size << (MI_SIZE_LOG2 - tx_size_wide_log2[0]) >> ss_y;
624
625
      return row_off_in_sb + bottom_left_count_unit < sb_height_unit;
    }
626

627
628
    // Bottom row of superblock (and not the leftmost column): so bottom-left
    // pixels fall in the bottom superblock, which is not available yet.
629
    if (((blk_row_in_sb + 1) << bh_in_mi_log2) >= sb_mi_size) return 0;
630
631
632

    // General case (neither leftmost column nor bottom row): check if the
    // bottom-left block is coded before the current block.
633
    const uint16_t *const order = get_order_table(partition, bsize);
634
635
636
637
638
639
640
641
642
    const int this_blk_index =
        ((blk_row_in_sb + 0) << (MAX_MIB_SIZE_LOG2 - bw_in_mi_log2)) +
        blk_col_in_sb + 0;
    const uint16_t this_blk_order = order[this_blk_index];
    const int bl_blk_index =
        ((blk_row_in_sb + 1) << (MAX_MIB_SIZE_LOG2 - bw_in_mi_log2)) +
        blk_col_in_sb - 1;
    const uint16_t bl_blk_order = order[bl_blk_index];
    return bl_blk_order < this_blk_order;
643
644
  }
}
Jingning Han's avatar
Jingning Han committed
645
646
647
648

typedef void (*intra_pred_fn)(uint8_t *dst, ptrdiff_t stride,
                              const uint8_t *above, const uint8_t *left);

649
650
static intra_pred_fn pred[INTRA_MODES][TX_SIZES_ALL];
static intra_pred_fn dc_pred[2][2][TX_SIZES_ALL];
Jingning Han's avatar
Jingning Han committed
651

652
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
653
654
655
typedef void (*intra_high_pred_fn)(uint16_t *dst, ptrdiff_t stride,
                                   const uint16_t *above, const uint16_t *left,
                                   int bd);
656
657
static intra_high_pred_fn pred_high[INTRA_MODES][TX_SIZES_ALL];
static intra_high_pred_fn dc_pred_high[2][2][TX_SIZES_ALL];
658
#endif  // CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
659

Yaowu Xu's avatar
Yaowu Xu committed
660
static void av1_init_intra_predictors_internal(void) {
661
662
663
#if CONFIG_EXT_INTRA
  assert(NELEMENTS(mode_to_angle_map) == INTRA_MODES);
#endif  // CONFIG_EXT_INTRA
664

665
666
667
668
669
670
671
672
673
674
675
#if CONFIG_TX64X64
#define INIT_RECTANGULAR(p, type)             \
  p[TX_4X8] = aom_##type##_predictor_4x8;     \
  p[TX_8X4] = aom_##type##_predictor_8x4;     \
  p[TX_8X16] = aom_##type##_predictor_8x16;   \
  p[TX_16X8] = aom_##type##_predictor_16x8;   \
  p[TX_16X32] = aom_##type##_predictor_16x32; \
  p[TX_32X16] = aom_##type##_predictor_32x16; \
  p[TX_32X64] = aom_##type##_predictor_32x64; \
  p[TX_64X32] = aom_##type##_predictor_64x32;
#else
676
677
678
679
680
681
682
#define INIT_RECTANGULAR(p, type)             \
  p[TX_4X8] = aom_##type##_predictor_4x8;     \
  p[TX_8X4] = aom_##type##_predictor_8x4;     \
  p[TX_8X16] = aom_##type##_predictor_8x16;   \
  p[TX_16X8] = aom_##type##_predictor_16x8;   \
  p[TX_16X32] = aom_##type##_predictor_16x32; \
  p[TX_32X16] = aom_##type##_predictor_32x16;
683
#endif  // CONFIG_TX64X64
684

685
686
687
688
689
#if CONFIG_TX64X64
#define INIT_NO_4X4(p, type)                  \
  p[TX_8X8] = aom_##type##_predictor_8x8;     \
  p[TX_16X16] = aom_##type##_predictor_16x16; \
  p[TX_32X32] = aom_##type##_predictor_32x32; \
690
691
  p[TX_64X64] = aom_##type##_predictor_64x64; \
  INIT_RECTANGULAR(p, type)
692
#else
clang-format's avatar
clang-format committed
693
#define INIT_NO_4X4(p, type)                  \
Yaowu Xu's avatar
Yaowu Xu committed
694
695
  p[TX_8X8] = aom_##type##_predictor_8x8;     \
  p[TX_16X16] = aom_##type##_predictor_16x16; \
696
697
  p[TX_32X32] = aom_##type##_predictor_32x32; \
  INIT_RECTANGULAR(p, type)
698
#endif  // CONFIG_TX64X64
Jingning Han's avatar
Jingning Han committed
699

Jingning Han's avatar
Jingning Han committed
700
701
702
#define INIT_ALL_SIZES(p, type)           \
  p[TX_4X4] = aom_##type##_predictor_4x4; \
  INIT_NO_4X4(p, type)
703

Jingning Han's avatar
Jingning Han committed
704
705
  INIT_ALL_SIZES(pred[V_PRED], v);
  INIT_ALL_SIZES(pred[H_PRED], h);
706
707
708
  INIT_ALL_SIZES(pred[D207_PRED], d207e);
  INIT_ALL_SIZES(pred[D45_PRED], d45e);
  INIT_ALL_SIZES(pred[D63_PRED], d63e);
Jingning Han's avatar
Jingning Han committed
709
710
711
  INIT_ALL_SIZES(pred[D117_PRED], d117);
  INIT_ALL_SIZES(pred[D135_PRED], d135);
  INIT_ALL_SIZES(pred[D153_PRED], d153);
Urvang Joshi's avatar
Urvang Joshi committed
712

Urvang Joshi's avatar
Urvang Joshi committed
713
  INIT_ALL_SIZES(pred[PAETH_PRED], paeth);
714
  INIT_ALL_SIZES(pred[SMOOTH_PRED], smooth);
Urvang Joshi's avatar
Urvang Joshi committed
715
716
  INIT_ALL_SIZES(pred[SMOOTH_V_PRED], smooth_v);
  INIT_ALL_SIZES(pred[SMOOTH_H_PRED], smooth_h);
Jingning Han's avatar
Jingning Han committed
717
718
719
720
721
722

  INIT_ALL_SIZES(dc_pred[0][0], dc_128);
  INIT_ALL_SIZES(dc_pred[0][1], dc_top);
  INIT_ALL_SIZES(dc_pred[1][0], dc_left);
  INIT_ALL_SIZES(dc_pred[1][1], dc);

723
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
724
725
  INIT_ALL_SIZES(pred_high[V_PRED], highbd_v);
  INIT_ALL_SIZES(pred_high[H_PRED], highbd_h);
726
727
728
  INIT_ALL_SIZES(pred_high[D207_PRED], highbd_d207e);
  INIT_ALL_SIZES(pred_high[D45_PRED], highbd_d45e);
  INIT_ALL_SIZES(pred_high[D63_PRED], highbd_d63e);
Jingning Han's avatar
Jingning Han committed
729
730
731
  INIT_ALL_SIZES(pred_high[D117_PRED], highbd_d117);
  INIT_ALL_SIZES(pred_high[D135_PRED], highbd_d135);
  INIT_ALL_SIZES(pred_high[D153_PRED], highbd_d153);
Urvang Joshi's avatar
Urvang Joshi committed
732

Urvang Joshi's avatar
Urvang Joshi committed
733
  INIT_ALL_SIZES(pred_high[PAETH_PRED], highbd_paeth);
734
  INIT_ALL_SIZES(pred_high[SMOOTH_PRED], highbd_smooth);
Urvang Joshi's avatar
Urvang Joshi committed
735
736
  INIT_ALL_SIZES(pred_high[SMOOTH_V_PRED], highbd_smooth_v);
  INIT_ALL_SIZES(pred_high[SMOOTH_H_PRED], highbd_smooth_h);
Jingning Han's avatar
Jingning Han committed
737
738
739
740
741

  INIT_ALL_SIZES(dc_pred_high[0][0], highbd_dc_128);
  INIT_ALL_SIZES(dc_pred_high[0][1], highbd_dc_top);
  INIT_ALL_SIZES(dc_pred_high[1][0], highbd_dc_left);
  INIT_ALL_SIZES(dc_pred_high[1][1], highbd_dc);
742
#endif  // CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
743
744
745
746

#undef intra_pred_allsizes
}

hui su's avatar
hui su committed
747
748
#if CONFIG_EXT_INTRA
// Directional prediction, zone 1: 0 < angle < 90
749
static void dr_prediction_z1(uint8_t *dst, ptrdiff_t stride, int bw, int bh,
hui su's avatar
hui su committed
750
                             const uint8_t *above, const uint8_t *left,
751
752
#if CONFIG_INTRA_EDGE_UPSAMPLE
                             int upsample_above,
Joe Young's avatar
Joe Young committed
753
#endif  // CONFIG_INTRA_EDGE_UPSAMPLE
hui su's avatar
hui su committed
754
                             int dx, int dy) {
hui su's avatar
hui su committed
755
  int r, c, x, base, shift, val;
hui su's avatar
hui su committed
756
757
758
759

  (void)left;
  (void)dy;
  assert(dy == 1);
hui su's avatar
hui su committed
760
  assert(dx > 0);
hui su's avatar
hui su committed
761

762
763
#if !CONFIG_INTRA_EDGE_UPSAMPLE
  const int upsample_above = 0;
Joe Young's avatar
Joe Young committed
764
#endif  // !CONFIG_INTRA_EDGE_UPSAMPLE
765
766
767
  const int max_base_x = ((bw + bh) - 1) << upsample_above;
  const int frac_bits = 8 - upsample_above;
  const int base_inc = 1 << upsample_above;
hui su's avatar
hui su committed
768
  x = dx;
769
  for (r = 0; r < bh; ++r, dst += stride, x += dx) {
770
771
    base = x >> frac_bits;
    shift = (x << upsample_above) & 0xFF;
hui su's avatar
hui su committed
772

773
774
775
    if (base >= max_base_x) {
      for (int i = r; i < bh; ++i) {
        memset(dst, above[max_base_x], bw * sizeof(dst[0]));
hui su's avatar
hui su committed
776
777
778
779
780
        dst += stride;
      }
      return;
    }

781
782
    for (c = 0; c < bw; ++c, base += base_inc) {
      if (base < max_base_x) {
hui su's avatar
hui su committed
783
784
        val = above[base] * (256 - shift) + above[base + 1] * shift;
        val = ROUND_POWER_OF_TWO(val, 8);
hui su's avatar
hui su committed
785
786
        dst[c] = clip_pixel(val);
      } else {
787
        dst[c] = above[max_base_x];
hui su's avatar
hui su committed
788
789
790
791
792
793
      }
    }
  }
}

// Directional prediction, zone 2: 90 < angle < 180
794
static void dr_prediction_z2(uint8_t *dst, ptrdiff_t stride, int bw, int bh,
hui su's avatar
hui su committed
795
                             const uint8_t *above, const uint8_t *left,
796
797
#if CONFIG_INTRA_EDGE_UPSAMPLE
                             int upsample_above, int upsample_left,
Joe Young's avatar
Joe Young committed
798
#endif  // CONFIG_INTRA_EDGE_UPSAMPLE
hui su's avatar
hui su committed
799
                             int dx, int dy) {
Debargha Mukherjee's avatar
Debargha Mukherjee committed
800
  int r, c, x, y, shift1, shift2, val, base1, base2;
hui su's avatar
hui su committed
801
802
803
804

  assert(dx > 0);
  assert(dy > 0);

805
806
807
#if !CONFIG_INTRA_EDGE_UPSAMPLE
  const int upsample_above = 0;
  const int upsample_left = 0;
Joe Young's avatar
Joe Young committed
808
#endif  // !CONFIG_INTRA_EDGE_UPSAMPLE
809
810
811
812
  const int min_base_x = -(1 << upsample_above);
  const int frac_bits_x = 8 - upsample_above;
  const int frac_bits_y = 8 - upsample_left;
  const int base_inc_x = 1 << upsample_above;
hui su's avatar
hui su committed
813
  x = -dx;
814
  for (r = 0; r < bh; ++r, x -= dx, dst += stride) {
815
    base1 = x >> frac_bits_x;
hui su's avatar
hui su committed
816
    y = (r << 8) - dy;
817
818
    for (c = 0; c < bw; ++c, base1 += base_inc_x, y -= dy) {
      if (base1 >= min_base_x) {
Yaowu Xu's avatar
Yaowu Xu committed
819
        shift1 = (x * (1 << upsample_above)) & 0xFF;
hui su's avatar
hui su committed
820
821
        val = above[base1] * (256 - shift1) + above[base1 + 1] * shift1;
        val = ROUND_POWER_OF_TWO(val, 8);
hui su's avatar
hui su committed
822
      } else {
823
824
        base2 = y >> frac_bits_y;
        assert(base2 >= -(1 << upsample_left));
Yaowu Xu's avatar
Yaowu Xu committed
825
        shift2 = (y * (1 << upsample_left)) & 0xFF;
826
827
        val = left[base2] * (256 - shift2) + left[base2 + 1] * shift2;
        val = ROUND_POWER_OF_TWO(val, 8);
hui su's avatar
hui su committed
828
829
830
831
832
833
834
      }
      dst[c] = clip_pixel(val);
    }
  }
}

// Directional prediction, zone 3: 180 < angle < 270
835
static void dr_prediction_z3(uint8_t *dst, ptrdiff_t stride, int bw, int bh,
hui su's avatar
hui su committed
836
                             const uint8_t *above, const uint8_t *left,
837
838
#if CONFIG_INTRA_EDGE_UPSAMPLE
                             int upsample_left,
Joe Young's avatar
Joe Young committed
839
#endif  // CONFIG_INTRA_EDGE_UPSAMPLE
hui su's avatar
hui su committed
840
                             int dx, int dy) {
hui su's avatar
hui su committed
841
  int r, c, y, base, shift, val;
hui su's avatar
hui su committed
842
843
844

  (void)above;
  (void)dx;
845

hui su's avatar
hui su committed
846
  assert(dx == 1);
hui su's avatar
hui su committed
847
  assert(dy > 0);
hui su's avatar
hui su committed
848

849
850
#if !CONFIG_INTRA_EDGE_UPSAMPLE
  const int upsample_left = 0;
Joe Young's avatar
Joe Young committed
851
#endif  // !CONFIG_INTRA_EDGE_UPSAMPLE
852
853
854
  const int max_base_y = (bw + bh - 1) << upsample_left;
  const int frac_bits = 8 - upsample_left;
  const int base_inc = 1 << upsample_left;
hui su's avatar
hui su committed
855
  y = dy;
856
  for (c = 0; c < bw; ++c, y += dy) {
857
858
    base = y >> frac_bits;
    shift = (y << upsample_left) & 0xFF;
hui su's avatar
hui su committed
859

860
861
    for (r = 0; r < bh; ++r, base += base_inc) {
      if (base < max_base_y) {
hui su's avatar
hui su committed
862
863
864
        val = left[base] * (256 - shift) + left[base + 1] * shift;
        val = ROUND_POWER_OF_TWO(val, 8);
        dst[r * stride + c] = clip_pixel(val);
hui su's avatar
hui su committed
865
      } else {
866
        for (; r < bh; ++r) dst[r * stride + c] = left[max_base_y];
hui su's avatar
hui su committed
867
        break;
hui su's avatar
hui su committed
868
869
870
871
872
      }
    }
  }
}

873
874
875
876
// Get the shift (up-scaled by 256) in X w.r.t a unit change in Y.
// If angle > 0 && angle < 90, dx = -((int)(256 / t));
// If angle > 90 && angle < 180, dx = (int)(256 / t);
// If angle > 180 && angle < 270, dx = 1;
Yaowu Xu's avatar
Yaowu Xu committed
877
static INLINE int get_dx(int angle) {
878
  if (angle > 0 && angle < 90) {
hui su's avatar
hui su committed
879
    return dr_intra_derivative[angle];
880
881
882
883
884
885
886
887
888
889
890
891
  } else if (angle > 90 && angle < 180) {
    return dr_intra_derivative[180 - angle];
  } else {
    // In this case, we are not really going to use dx. We may return any value.
    return 1;
  }
}

// Get the shift (up-scaled by 256) in Y w.r.t a unit change in X.
// If angle > 0 && angle < 90, dy = 1;
// If angle > 90 && angle < 180, dy = (int)(256 * t);
// If angle > 180 && angle < 270, dy = -((int)(256 * t));
Yaowu Xu's avatar
Yaowu Xu committed
892
static INLINE int get_dy(int angle) {
893
  if (angle > 90 && angle < 180) {
clang-format's avatar
clang-format committed
894
895
    return dr_intra_derivative[angle - 90];
  } else if (angle > 180 && angle < 270) {
hui su's avatar
hui su committed
896
    return dr_intra_derivative[270 - angle];
clang-format's avatar
clang-format committed
897
  } else {
898
899
900
901
902
    // In this case, we are not really going to use dy. We may return any value.
    return 1;
  }
}

hui su's avatar
hui su committed
903
static void dr_predictor(uint8_t *dst, ptrdiff_t stride, TX_SIZE tx_size,
hui su's avatar
hui su committed
904
                         const uint8_t *above, const uint8_t *left,
905
906
#if CONFIG_INTRA_EDGE_UPSAMPLE
                         int upsample_above, int upsample_left,
Joe Young's avatar
Joe Young committed
907
#endif  // CONFIG_INTRA_EDGE_UPSAMPLE
hui su's avatar
hui su committed
908
                         int angle) {
909
910
  const int dx = get_dx(angle);
  const int dy = get_dy(angle);
911
912
  const int bw = tx_size_wide[tx_size];
  const int bh = tx_size_high[tx_size];
913
914
  assert(angle > 0 && angle < 270);

hui su's avatar
hui su committed
915
  if (angle > 0 && angle < 90) {
916
    dr_prediction_z1(dst, stride, bw, bh, above, left,
917
918
#if CONFIG_INTRA_EDGE_UPSAMPLE
                     upsample_above,
Joe Young's avatar
Joe Young committed
919
#endif  // CONFIG_INTRA_EDGE_UPSAMPLE
hui su's avatar
hui su committed
920
                     dx, dy);
hui su's avatar
hui su committed
921
  } else if (angle > 90 && angle < 180) {
922
    dr_prediction_z2(dst, stride, bw, bh, above, left,
923
924
#if CONFIG_INTRA_EDGE_UPSAMPLE
                     upsample_above, upsample_left,
Joe Young's avatar
Joe Young committed
925
#endif  // CONFIG_INTRA_EDGE_UPSAMPLE
hui su's avatar
hui su committed
926
                     dx, dy);
hui su's avatar
hui su committed
927
  } else if (angle > 180 && angle < 270) {
928
    dr_prediction_z3(dst, stride, bw, bh, above, left,
929
930
#if CONFIG_INTRA_EDGE_UPSAMPLE
                     upsample_left,
Joe Young's avatar
Joe Young committed
931
#endif  // CONFIG_INTRA_EDGE_UPSAMPLE
hui su's avatar
hui su committed
932
                     dx, dy);
hui su's avatar
hui su committed
933
  } else if (angle == 90) {
hui su's avatar
hui su committed
934
    pred[V_PRED][tx_size](dst, stride, above, left);
hui su's avatar
hui su committed
935
  } else if (angle == 180) {
hui su's avatar
hui su committed
936
    pred[H_PRED][tx_size](dst, stride, above, left);
hui su's avatar
hui su committed
937
938
939
  }
}

940
#if CONFIG_HIGHBITDEPTH
hui su's avatar
hui su committed
941
// Directional prediction, zone 1: 0 < angle < 90
942
943
944
static void highbd_dr_prediction_z1(uint16_t *dst, ptrdiff_t stride, int bw,
                                    int bh, const uint16_t *above,
                                    const uint16_t *left,
945
946
#if CONFIG_INTRA_EDGE_UPSAMPLE
                                    int upsample_above,
Joe Young's avatar
Joe Young committed
947
#endif  // CONFIG_INTRA_EDGE_UPSAMPLE
hui su's avatar
hui su committed
948
                                    int dx, int dy, int bd) {
949
  int r, c, x, base, shift, val;
hui su's avatar
hui su committed
950
951
952
953

  (void)left;
  (void)dy;
  assert(dy == 1);
954
  assert(dx > 0);
hui su's avatar
hui su committed
955

956
957
#if !CONFIG_INTRA_EDGE_UPSAMPLE
  const int upsample_above = 0;
Joe Young's avatar
Joe Young committed
958
#endif  // !CONFIG_INTRA_EDGE_UPSAMPLE
959
960
961
  const int max_base_x = ((bw + bh) - 1) << upsample_above;
  const int frac_bits = 8 - upsample_above;
  const int base_inc = 1 << upsample_above;
962
  x = dx;
963
  for (r = 0; r < bh; ++r, dst += stride, x += dx) {
964
965
    base = x >> frac_bits;
    shift = (x << upsample_above) & 0xFF;
966

967
968
969
    if (base >= max_base_x) {
      for (int i = r; i < bh; ++i) {
        aom_memset16(dst, above[max_base_x], bw);
970
971
972
973
974
        dst += stride;
      }
      return;
    }

975
976
    for (c = 0; c < bw; ++c, base += base_inc) {
      if (base < max_base_x) {
hui su's avatar
hui su committed
977
978
        val = above[base] * (256 - shift) + above[base + 1] * shift;
        val = ROUND_POWER_OF_TWO(val, 8);
hui su's avatar
hui su committed
979
980
        dst[c] = clip_pixel_highbd(val, bd);
      } else {
981
        dst[c] = above[max_base_x];
hui su's avatar
hui su committed
982
983
984
985
986
987
      }
    }
  }
}

// Directional prediction, zone 2: 90 < angle < 180
988
989
990
static void highbd_dr_prediction_z2(uint16_t *dst, ptrdiff_t stride, int bw,
                                    int bh, const uint16_t *above,
                                    const uint16_t *left,
991
992
#if CONFIG_INTRA_EDGE_UPSAMPLE
                                    int upsample_above, int upsample_left,
Joe Young's avatar
Joe Young committed
993
#endif  // CONFIG_INTRA_EDGE_UPSAMPLE
hui su's avatar
hui su committed
994
                                    int dx, int dy, int bd) {
995
  int r, c, x, y, shift, val, base;