reconintra.c 55.9 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
2
3
4
5
6
7
8
9
10
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

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

Jingning Han's avatar
Jingning Han committed
13
14
#include "./vpx_config.h"
#include "./vpx_dsp_rtcd.h"
15
#include "vpx_ports/system_state.h"
Jingning Han's avatar
Jingning Han committed
16
17
18
19
20
21
22
23

#if CONFIG_VP9_HIGHBITDEPTH
#include "vpx_dsp/vpx_dsp_common.h"
#endif  // CONFIG_VP9_HIGHBITDEPTH
#include "vpx_mem/vpx_mem.h"
#include "vpx_ports/mem.h"
#include "vpx_ports/vpx_once.h"

24
25
#include "vp10/common/reconintra.h"
#include "vp10/common/onyxc_int.h"
Jingning Han's avatar
Jingning Han committed
26

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
enum {
  NEED_LEFT = 1 << 1,
  NEED_ABOVE = 1 << 2,
  NEED_ABOVERIGHT = 1 << 3,
  NEED_ABOVELEFT = 1 << 4,
  NEED_BOTTOMLEFT = 1 << 5,
};

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
  NEED_LEFT | NEED_ABOVE | NEED_ABOVELEFT,  // TM
};
Jingning Han's avatar
Jingning Han committed
47

48
49
50
51
static const uint8_t orders_128x128[1] = { 0 };
static const uint8_t orders_128x64[2] = { 0, 1 };
static const uint8_t orders_64x128[2] = { 0, 1 };
static const uint8_t orders_64x64[4] = {
52
53
54
  0, 1,
  2, 3,
};
55
static const uint8_t orders_64x32[8] = {
56
57
58
59
60
  0, 2,
  1, 3,
  4, 6,
  5, 7,
};
61
static const uint8_t orders_32x64[8] = {
62
63
64
  0, 1, 2, 3,
  4, 5, 6, 7,
};
65
static const uint8_t orders_32x32[16] = {
66
67
68
69
70
  0,   1,  4,  5,
  2,   3,  6,  7,
  8,   9, 12, 13,
  10, 11, 14, 15,
};
71
static const uint8_t orders_32x16[32] = {
72
73
74
75
76
77
78
79
80
  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,
};
81
static const uint8_t orders_16x32[32] = {
82
83
84
85
86
  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,
};
87
static const uint8_t orders_16x16[64] = {
88
89
90
91
92
93
94
95
96
  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,
  40, 41, 44, 45, 56, 57, 60, 61,
  42, 43, 46, 47, 58, 59, 62, 63,
};
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

#if CONFIG_EXT_PARTITION
static const uint8_t orders_16x8[128] = {
  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,
  84, 86, 92, 94, 116, 118, 124, 126,
  85, 87, 93, 95, 117, 119, 125, 127,
};
static const uint8_t orders_8x16[128] = {
  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,
  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,
  84, 85, 86, 87, 92, 93, 94, 95, 116, 117, 118, 119, 124, 125, 126, 127,
};
static const uint8_t orders_8x8[256] = {
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, 255,
};

146
static const uint8_t *const orders[BLOCK_SIZES] = {
147
148
149
150
151
152
153
154
155
156
157
158
  //                              4X4
                                  orders_8x8,
  // 4X8,         8X4,            8X8
  orders_8x8,     orders_8x8,     orders_8x8,
  // 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
  orders_64x128,  orders_128x64,  orders_128x128
159
};
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#else
static const uint8_t *const orders[BLOCK_SIZES] = {
  //                              4X4
                                  orders_16x16,
  // 4X8,         8X4,            8X8
  orders_16x16,   orders_16x16,   orders_16x16,
  // 8X16,        16X8,           16X16
  orders_16x32,   orders_32x16,   orders_32x32,
  // 16X32,       32X16,          32X32
  orders_32x64,   orders_64x32,   orders_64x64,
  // 32X64,       64X32,          64X64
  orders_64x128,  orders_128x64,  orders_128x128
};
#endif  // CONFIG_EXT_PARTITION

175
#if CONFIG_EXT_PARTITION_TYPES
176
static const uint8_t orders_verta_64x64[4] = {
177
178
179
  0, 2,
  1, 2,
};
180
static const uint8_t orders_verta_32x32[16] = {
181
182
183
184
185
  0,   2,  4,  6,
  1,   2,  5,  6,
  8,  10, 12, 14,
  9,  10, 13, 14,
};
186
static const uint8_t orders_verta_16x16[64] = {
187
188
189
190
191
192
193
194
195
  0,   2,  4,  6, 16, 18, 20, 22,
  1,   2,  5,  6, 17, 18, 21, 22,
  8,  10, 12, 14, 24, 26, 28, 30,
  9,  10, 13, 14, 25, 26, 29, 30,
  32, 34, 36, 38, 48, 50, 52, 54,
  33, 34, 37, 38, 49, 50, 53, 54,
  40, 42, 44, 46, 56, 58, 60, 62,
  41, 42, 45, 46, 57, 58, 61, 62,
};
196
197
198
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
#if CONFIG_EXT_PARTITION
static const uint8_t orders_verta_8x8[256] = {
0,     2,   4,   6,  16,  18,  20,  22,  64,  66,  68,  70,  80,  82,  84,  86,
1,     2,   5,   6,  17,  18,  21,  22,  65,  66,  69,  70,  81,  82,  85,  86,
8,    10,  12,  14,  24,  26,  28,  30,  72,  74,  76,  78,  88,  90,  92,  94,
9,    10,  13,  14,  25,  26,  29,  30,  73,  74,  77,  78,  89,  90,  93,  94,
32,   34,  36,  38,  48,  50,  52,  54,  96,  98, 100, 102, 112, 114, 116, 118,
33,   34,  37,  38,  49,  50,  53,  54,  97,  98, 101, 102, 113, 114, 117, 118,
40,   42,  44,  46,  56,  58,  60,  62, 104, 106, 108, 110, 120, 122, 124, 126,
41,   42,  45,  46,  57,  58,  61,  62, 105, 106, 109, 110, 121, 122, 125, 126,
128, 130, 132, 134, 144, 146, 148, 150, 192, 194, 196, 198, 208, 210, 212, 214,
129, 130, 133, 134, 145, 146, 149, 150, 193, 194, 197, 198, 209, 210, 213, 214,
136, 138, 140, 142, 152, 154, 156, 158, 200, 202, 204, 206, 216, 218, 220, 222,
137, 138, 141, 142, 153, 154, 157, 158, 201, 202, 205, 206, 217, 218, 221, 222,
160, 162, 164, 166, 176, 178, 180, 182, 224, 226, 228, 230, 240, 242, 244, 246,
161, 162, 165, 166, 177, 178, 181, 182, 225, 226, 229, 230, 241, 242, 245, 246,
168, 170, 172, 174, 184, 186, 188, 190, 232, 234, 236, 238, 248, 250, 252, 254,
169, 170, 173, 174, 185, 186, 189, 190, 233, 234, 237, 238, 249, 250, 253, 254,
};
static const uint8_t *const orders_verta[BLOCK_SIZES] = {
  //                                  4X4
                                      orders_verta_8x8,
  // 4X8,           8X4,              8X8
  orders_verta_8x8, orders_verta_8x8, orders_verta_8x8,
  // 8X16,          16X8,             16X16
  orders_8x16,      orders_16x8,      orders_verta_16x16,
  // 16X32,         32X16,            32X32
  orders_16x32,     orders_32x16,     orders_verta_32x32,
  // 32X64,         64X32,            64X64
  orders_32x64,     orders_64x32,     orders_verta_64x64,
  // 64x128,        128x64,           128x128
  orders_64x128,    orders_128x64,    orders_128x128
};
#else
230
static const uint8_t *const orders_verta[BLOCK_SIZES] = {
231
232
233
234
235
236
237
238
239
240
  //                                      4X4
                                          orders_verta_16x16,
  // 4X8,             8X4,                8X8
  orders_verta_16x16, orders_verta_16x16, orders_verta_16x16,
  // 8X16,            16X8,               16X16
  orders_16x32,       orders_32x16,       orders_verta_32x32,
  // 16X32,           32X16,              32X32
  orders_32x64,       orders_64x32,       orders_verta_64x64,
  // 32X64,           64X32,              64X64
  orders_64x128,      orders_128x64,      orders_128x128
241
};
242
#endif  // CONFIG_EXT_PARTITION
243
#endif  // CONFIG_EXT_PARTITION_TYPES
244
245
246

static int vp10_has_right(BLOCK_SIZE bsize, int mi_row, int mi_col,
                          int right_available,
247
248
249
#if CONFIG_EXT_PARTITION_TYPES
                          PARTITION_TYPE partition,
#endif
250
                          TX_SIZE txsz, int y, int x, int ss_x) {
hui su's avatar
hui su committed
251
252
253
254
  const int wl = mi_width_log2_lookup[bsize];
  const int w = VPXMAX(num_4x4_blocks_wide_lookup[bsize] >> ss_x, 1);
  const int step = 1 << txsz;

255
256
257
258
259
260
  if (!right_available) {
    return 0;
  } else {
    // Handle block size 4x8 and 4x4
    if (ss_x == 0 && num_4x4_blocks_wide_lookup[bsize] < 2 && x == 0)
      return 1;
261

262
263
264
265
    if (y == 0) {
      const int hl = mi_height_log2_lookup[bsize];
      const uint8_t *order;
      int my_order, tr_order;
266
#if CONFIG_EXT_PARTITION_TYPES
267
268
269
      if (partition == PARTITION_VERT_A)
        order = orders_verta[bsize];
      else
270
#endif  // CONFIG_EXT_PARTITION_TYPES
271
      order = orders[bsize];
272

273
274
      if (x + step < w)
        return 1;
275

276
277
      mi_row = (mi_row & MAX_MIB_MASK) >> hl;
      mi_col = (mi_col & MAX_MIB_MASK) >> wl;
278

279
280
281
      // If top row of coding unit
      if (mi_row == 0)
        return 1;
282

283
      // If rightmost column of coding unit
284
      if (((mi_col + 1) << wl) >= MAX_MIB_SIZE)
285
        return 0;
286

287
288
      my_order = order[((mi_row + 0) << (MAX_MIB_SIZE_LOG2 - wl)) + mi_col + 0];
      tr_order = order[((mi_row - 1) << (MAX_MIB_SIZE_LOG2 - wl)) + mi_col + 1];
289

290
291
292
293
      return my_order > tr_order;
    } else {
      return x + step < w;
    }
294
295
296
297
298
299
  }
}

static int vp10_has_bottom(BLOCK_SIZE bsize, int mi_row, int mi_col,
                           int bottom_available, TX_SIZE txsz,
                           int y, int x, int ss_y) {
300
301
302
  if (!bottom_available || x != 0) {
    return 0;
  } else {
hui su's avatar
hui su committed
303
304
305
306
    const int wl = mi_width_log2_lookup[bsize];
    const int hl = mi_height_log2_lookup[bsize];
    const int h = 1 << (hl + 1 - ss_y);
    const int step = 1 << txsz;
307
308
309
    const uint8_t *order = orders[bsize];
    int my_order, bl_order;

310
311
312
313
314
315
316
    // Handle block size 8x4 and 4x4
    if (ss_y == 0 && num_4x4_blocks_high_lookup[bsize] < 2 && y == 0)
      return 1;

    if (y + step < h)
      return 1;

317
318
    mi_row = (mi_row & MAX_MIB_MASK) >> hl;
    mi_col = (mi_col & MAX_MIB_MASK) >> wl;
319
320

    if (mi_col == 0)
321
      return (mi_row << (hl + !ss_y)) + y + step < (MAX_MIB_SIZE << !ss_y);
322

323
    if (((mi_row + 1) << hl) >= MAX_MIB_SIZE)
324
325
      return 0;

326
327
    my_order = order[((mi_row + 0) << (MAX_MIB_SIZE_LOG2 - wl)) + mi_col + 0];
    bl_order = order[((mi_row + 1) << (MAX_MIB_SIZE_LOG2 - wl)) + mi_col - 1];
328

329
    return bl_order < my_order;
330
331
  }
}
Jingning Han's avatar
Jingning Han committed
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

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

static intra_pred_fn pred[INTRA_MODES][TX_SIZES];
static intra_pred_fn dc_pred[2][2][TX_SIZES];

#if CONFIG_VP9_HIGHBITDEPTH
typedef void (*intra_high_pred_fn)(uint16_t *dst, ptrdiff_t stride,
                                   const uint16_t *above, const uint16_t *left,
                                   int bd);
static intra_high_pred_fn pred_high[INTRA_MODES][4];
static intra_high_pred_fn dc_pred_high[2][2][4];
#endif  // CONFIG_VP9_HIGHBITDEPTH

static void vp10_init_intra_predictors_internal(void) {
348
#define INIT_NO_4X4(p, type) \
Jingning Han's avatar
Jingning Han committed
349
350
351
352
  p[TX_8X8] = vpx_##type##_predictor_8x8; \
  p[TX_16X16] = vpx_##type##_predictor_16x16; \
  p[TX_32X32] = vpx_##type##_predictor_32x32

353
354
355
356
#define INIT_ALL_SIZES(p, type) \
  p[TX_4X4] = vpx_##type##_predictor_4x4; \
  INIT_NO_4X4(p, type)

Jingning Han's avatar
Jingning Han committed
357
358
  INIT_ALL_SIZES(pred[V_PRED], v);
  INIT_ALL_SIZES(pred[H_PRED], h);
359
360
361
  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
362
363
364
365
366
367
368
369
370
371
372
373
374
  INIT_ALL_SIZES(pred[D117_PRED], d117);
  INIT_ALL_SIZES(pred[D135_PRED], d135);
  INIT_ALL_SIZES(pred[D153_PRED], d153);
  INIT_ALL_SIZES(pred[TM_PRED], tm);

  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);

#if CONFIG_VP9_HIGHBITDEPTH
  INIT_ALL_SIZES(pred_high[V_PRED], highbd_v);
  INIT_ALL_SIZES(pred_high[H_PRED], highbd_h);
375
376
377
  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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
  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);
  INIT_ALL_SIZES(pred_high[TM_PRED], highbd_tm);

  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);
#endif  // CONFIG_VP9_HIGHBITDEPTH

#undef intra_pred_allsizes
}

hui su's avatar
hui su committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#if CONFIG_EXT_INTRA
#define FILTER_INTRA_PREC_BITS 10

static const uint8_t ext_intra_extend_modes[FILTER_INTRA_MODES] = {
  NEED_LEFT | NEED_ABOVE,      // FILTER_DC
  NEED_LEFT | NEED_ABOVE,      // FILTER_V
  NEED_LEFT | NEED_ABOVE,      // FILTER_H
  NEED_LEFT | NEED_ABOVE,      // FILTER_D45
  NEED_LEFT | NEED_ABOVE,      // FILTER_D135
  NEED_LEFT | NEED_ABOVE,      // FILTER_D117
  NEED_LEFT | NEED_ABOVE,      // FILTER_D153
  NEED_LEFT | NEED_ABOVE,      // FILTER_D207
  NEED_LEFT | NEED_ABOVE,      // FILTER_D63
  NEED_LEFT | NEED_ABOVE,      // FILTER_TM
};

408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
static int intra_subpel_interp(int base, int shift, const uint8_t *ref,
                               int ref_start_idx, int ref_end_idx,
                               INTRA_FILTER filter_type) {
  int val, k, idx, filter_idx = 0;
  const int16_t *filter = NULL;

  if (filter_type == INTRA_FILTER_LINEAR) {
    val = ref[base] * (256 - shift) + ref[base + 1] * shift;
    val = ROUND_POWER_OF_TWO(val, 8);
  } else {
    filter_idx = ROUND_POWER_OF_TWO(shift, 8 - SUBPEL_BITS);
    filter = vp10_intra_filter_kernels[filter_type][filter_idx];

    if (filter_idx < (1 << SUBPEL_BITS)) {
      val = 0;
      for (k = 0; k < SUBPEL_TAPS; ++k) {
        idx = base + 1 - (SUBPEL_TAPS / 2) + k;
        idx = VPXMAX(VPXMIN(idx, ref_end_idx), ref_start_idx);
        val += ref[idx] * filter[k];
      }
      val = ROUND_POWER_OF_TWO(val, FILTER_BITS);
    } else {
      val = ref[base + 1];
    }
  }

  return val;
}

hui su's avatar
hui su committed
437
438
439
// Directional prediction, zone 1: 0 < angle < 90
static void dr_prediction_z1(uint8_t *dst, ptrdiff_t stride, int bs,
                             const uint8_t *above, const uint8_t *left,
440
                             int dx, int dy, INTRA_FILTER filter_type) {
hui su's avatar
hui su committed
441
  int r, c, x, base, shift, val;
hui su's avatar
hui su committed
442
443
444
445
446
447

  (void)left;
  (void)dy;
  assert(dy == 1);
  assert(dx < 0);

hui su's avatar
hui su committed
448
449
450
  if (filter_type != INTRA_FILTER_LINEAR) {
    const int pad_size = SUBPEL_TAPS >> 1;
    int len;
451
452
    DECLARE_ALIGNED(16, uint8_t, buf[SUBPEL_SHIFTS][MAX_SB_SIZE]);
    DECLARE_ALIGNED(16, uint8_t, src[MAX_SB_SIZE + SUBPEL_TAPS]);
hui su's avatar
hui su committed
453
454
455
456
457
458
459
460
461
462
    uint8_t flags[SUBPEL_SHIFTS];

    memset(flags, 0, SUBPEL_SHIFTS * sizeof(flags[0]));
    memset(src, above[0], pad_size * sizeof(above[0]));
    memcpy(src + pad_size, above, 2 * bs * sizeof(above[0]));
    memset(src + pad_size + 2 * bs, above[2 * bs - 1],
           pad_size * sizeof(above[0]));
    flags[0] = 1;
    x = -dx;
    for (r = 0; r < bs; ++r, dst += stride, x -= dx) {
hui su's avatar
hui su committed
463
      base = x >> 8;
hui su's avatar
hui su committed
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
      shift = x & 0xFF;
      shift = ROUND_POWER_OF_TWO(shift, 8 - SUBPEL_BITS);
      if (shift == SUBPEL_SHIFTS) {
        base += 1;
        shift = 0;
      }
      len = VPXMIN(bs, 2 * bs - 1 - base);
      if (len <= 0) {
        int i;
        for (i = r; i < bs; ++i) {
          memset(dst, above[2 * bs - 1], bs * sizeof(dst[0]));
          dst += stride;
        }
        return;
      }

      if (len <= (bs >> 1) && !flags[shift]) {
        base = x >> 8;
        shift = x & 0xFF;
        for (c = 0; c < len; ++c) {
          val = intra_subpel_interp(base, shift, above, 0, 2 * bs - 1,
                                    filter_type);
          dst[c] = clip_pixel(val);
          ++base;
        }
      } else {
        if (!flags[shift]) {
491
          const int16_t *filter = vp10_intra_filter_kernels[filter_type][shift];
hui su's avatar
hui su committed
492
          vpx_convolve8_horiz(src + pad_size, 2 * bs, buf[shift], 2 * bs,
493
                              filter, 16,
hui su's avatar
hui su committed
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
                              NULL, 16, 2 * bs, 2 * bs < 16 ? 2 : 1);
          flags[shift] = 1;
        }
        memcpy(dst, shift == 0 ? src + pad_size + base : &buf[shift][base],
            len * sizeof(dst[0]));
      }

      if (len < bs)
        memset(dst + len, above[2 * bs - 1], (bs - len) * sizeof(dst[0]));
    }
    return;
  }

  // For linear filter, C code is faster.
  x = -dx;
  for (r = 0; r < bs; ++r, dst += stride, x -= dx) {
    base = x >> 8;
    shift = x & 0xFF;

    if (base >= 2 * bs - 1) {
      int i;
      for (i = r; i < bs; ++i) {
        memset(dst, above[2 * bs - 1], bs * sizeof(dst[0]));
        dst += stride;
      }
      return;
    }

    for (c = 0; c < bs; ++c, ++base) {
hui su's avatar
hui su committed
523
      if (base < 2 * bs - 1) {
hui su's avatar
hui su committed
524
525
        val = above[base] * (256 - shift) + above[base + 1] * shift;
        val = ROUND_POWER_OF_TWO(val, 8);
hui su's avatar
hui su committed
526
527
528
529
530
531
532
533
534
535
536
        dst[c] = clip_pixel(val);
      } else {
        dst[c] = above[2 * bs - 1];
      }
    }
  }
}

// Directional prediction, zone 2: 90 < angle < 180
static void dr_prediction_z2(uint8_t *dst, ptrdiff_t stride, int bs,
                             const uint8_t *above, const uint8_t *left,
537
                             int dx, int dy, INTRA_FILTER filter_type) {
Debargha Mukherjee's avatar
Debargha Mukherjee committed
538
  int r, c, x, y, shift1, shift2, val, base1, base2;
hui su's avatar
hui su committed
539
540
541
542

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

hui su's avatar
hui su committed
543
544
545
546
547
548
549
550
551
  x = -dx;
  for (r = 0; r < bs; ++r, x -= dx, dst += stride) {
    base1 = x >> 8;
    y = (r << 8) - dy;
    for (c = 0; c < bs; ++c, ++base1, y -= dy) {
      if (base1 >= -1) {
        shift1 = x & 0xFF;
        val = intra_subpel_interp(base1, shift1, above, -1, bs - 1,
                                  filter_type);
hui su's avatar
hui su committed
552
      } else {
hui su's avatar
hui su committed
553
554
555
556
557
        base2 = y >> 8;
        if (base2 >= 0) {
          shift2 = y & 0xFF;
          val = intra_subpel_interp(base2, shift2, left, 0, bs - 1,
                                    filter_type);
hui su's avatar
hui su committed
558
        } else {
559
          val = left[0];
hui su's avatar
hui su committed
560
561
562
563
564
565
566
567
568
569
        }
      }
      dst[c] = clip_pixel(val);
    }
  }
}

// Directional prediction, zone 3: 180 < angle < 270
static void dr_prediction_z3(uint8_t *dst, ptrdiff_t stride, int bs,
                             const uint8_t *above, const uint8_t *left,
570
                             int dx, int dy, INTRA_FILTER filter_type) {
hui su's avatar
hui su committed
571
  int r, c, y, base, shift, val;
hui su's avatar
hui su committed
572
573
574

  (void)above;
  (void)dx;
575

hui su's avatar
hui su committed
576
577
578
  assert(dx == 1);
  assert(dy < 0);

hui su's avatar
hui su committed
579
580
581
  if (filter_type != INTRA_FILTER_LINEAR) {
    const int pad_size = SUBPEL_TAPS >> 1;
    int len, i;
582
583
    DECLARE_ALIGNED(16, uint8_t, buf[MAX_SB_SIZE][4 * SUBPEL_SHIFTS]);
    DECLARE_ALIGNED(16, uint8_t, src[(MAX_SB_SIZE + SUBPEL_TAPS) * 4]);
hui su's avatar
hui su committed
584
585
586
587
588
589
590
591
592
593
594
595
    uint8_t flags[SUBPEL_SHIFTS];

    memset(flags, 0, SUBPEL_SHIFTS * sizeof(flags[0]));
    for (i = 0; i < pad_size; ++i)
      src[4 * i] = left[0];
    for (i = 0; i < 2 * bs; ++i)
      src[4 * (i + pad_size)] = left[i];
    for (i = 0; i < pad_size; ++i)
      src[4 * (i + 2 * bs + pad_size)] = left[2 * bs - 1];
    flags[0] = 1;
    y = -dy;
    for (c = 0; c < bs; ++c, y -= dy) {
hui su's avatar
hui su committed
596
      base = y >> 8;
hui su's avatar
hui su committed
597
598
599
600
601
602
603
604
605
      shift = y & 0xFF;
      shift = ROUND_POWER_OF_TWO(shift, 8 - SUBPEL_BITS);
      if (shift == SUBPEL_SHIFTS) {
        base += 1;
        shift = 0;
      }
      len = VPXMIN(bs, 2 * bs - 1 - base);

      if (len <= 0) {
Debargha Mukherjee's avatar
Debargha Mukherjee committed
606
607
        for (r = 0; r < bs; ++r) {
          dst[r * stride + c] = left[ 2 * bs - 1];
hui su's avatar
hui su committed
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
        }
        continue;
      }

      if (len <= (bs >> 1) && !flags[shift]) {
        base = y >> 8;
        shift = y & 0xFF;
        for (r = 0; r < len; ++r) {
          val = intra_subpel_interp(base, shift, left, 0, 2 * bs - 1,
                                    filter_type);
          dst[r * stride + c] = clip_pixel(val);
          ++base;
        }
      } else {
        if (!flags[shift]) {
623
          const int16_t *filter = vp10_intra_filter_kernels[filter_type][shift];
hui su's avatar
hui su committed
624
625
          vpx_convolve8_vert(src + 4 * pad_size, 4,
                             buf[0] + 4 * shift, 4 * SUBPEL_SHIFTS, NULL, 16,
626
                             filter, 16,
hui su's avatar
hui su committed
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
                             2 * bs < 16 ? 4 : 4, 2 * bs);
          flags[shift] = 1;
        }

        if (shift == 0) {
          for (r = 0; r < len; ++r) {
            dst[r * stride + c] = left[r + base];
          }
        } else {
          for (r = 0; r < len; ++r) {
            dst[r * stride + c] = buf[r + base][4 * shift];
          }
        }
      }

      if (len < bs) {
        for (r = len; r < bs; ++r) {
          dst[r * stride + c] = left[ 2 * bs - 1];
        }
      }
    }
    return;
  }

  // For linear filter, C code is faster.
  y = -dy;
  for (c = 0; c < bs; ++c, y -= dy) {
    base = y >> 8;
    shift = y & 0xFF;

    for (r = 0; r < bs; ++r, ++base) {
658
      if (base < 2 * bs - 1) {
hui su's avatar
hui su committed
659
660
661
        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
662
      } else {
hui su's avatar
hui su committed
663
664
665
        for (; r < bs; ++r)
          dst[r * stride + c] = left[2 * bs - 1];
        break;
hui su's avatar
hui su committed
666
667
668
669
670
      }
    }
  }
}

671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
// 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;
static inline int get_dx(int angle) {
  if (angle > 0 && angle < 90) {
    return -dr_intra_derivative[angle];
  } 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));
static inline int get_dy(int angle) {
  if (angle > 90 && angle < 180) {
      return dr_intra_derivative[angle - 90];
    } else if (angle > 180 && angle < 270) {
hui su's avatar
hui su committed
694
      return -dr_intra_derivative[270 - angle];
695
696
697
698
699
700
    } else {
    // 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
701
static void dr_predictor(uint8_t *dst, ptrdiff_t stride, TX_SIZE tx_size,
702
703
                         const uint8_t *above, const uint8_t *left, int angle,
                         INTRA_FILTER filter_type) {
704
705
  const int dx = get_dx(angle);
  const int dy = get_dy(angle);
706
  const int bs = 4 * num_4x4_blocks_wide_txsize_lookup[tx_size];
707
708
  assert(angle > 0 && angle < 270);

hui su's avatar
hui su committed
709
  if (angle > 0 && angle < 90) {
710
    dr_prediction_z1(dst, stride, bs, above, left, dx, dy, filter_type);
hui su's avatar
hui su committed
711
  } else if (angle > 90 && angle < 180) {
712
    dr_prediction_z2(dst, stride, bs, above, left, dx, dy, filter_type);
hui su's avatar
hui su committed
713
  } else if (angle > 180 && angle < 270) {
714
    dr_prediction_z3(dst, stride, bs, above, left, dx, dy, filter_type);
hui su's avatar
hui su committed
715
  } else if (angle == 90) {
hui su's avatar
hui su committed
716
    pred[V_PRED][tx_size](dst, stride, above, left);
hui su's avatar
hui su committed
717
  } else if (angle == 180) {
hui su's avatar
hui su committed
718
    pred[H_PRED][tx_size](dst, stride, above, left);
hui su's avatar
hui su committed
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
  }
}

static int filter_intra_taps_4[TX_SIZES][INTRA_MODES][4] = {
    {
        {735, 881, -537, -54},
        {1005, 519, -488, -11},
        {383, 990, -343, -6},
        {442, 805, -542, 319},
        {658, 616, -133, -116},
        {875, 442, -141, -151},
        {386, 741, -23, -80},
        {390, 1027, -446, 51},
        {679, 606, -523, 262},
        {903, 922, -778, -23},
    },
    {
        {648, 803, -444, 16},
        {972, 620, -576, 7},
        {561, 967, -499, -5},
        {585, 762, -468, 144},
        {596, 619, -182, -9},
        {895, 459, -176, -153},
        {557, 722, -126, -129},
        {601, 839, -523, 105},
        {562, 709, -499, 251},
        {803, 872, -695, 43},
    },
    {
        {423, 728, -347, 111},
        {963, 685, -665, 23},
        {281, 1024, -480, 216},
        {640, 596, -437, 78},
        {429, 669, -259, 99},
        {740, 646, -415, 23},
        {568, 771, -346, 40},
        {404, 833, -486, 209},
        {398, 712, -423, 307},
        {939, 935, -887, 17},
    },
    {
        {477, 737, -393, 150},
        {881, 630, -546, 67},
        {506, 984, -443, -20},
        {114, 459, -270, 528},
        {433, 528, 14, 3},
        {837, 470, -301, -30},
        {181, 777, 89, -107},
        {-29, 716, -232, 259},
        {589, 646, -495, 255},
        {740, 884, -728, 77},
    },
};

static void filter_intra_predictors_4tap(uint8_t *dst, ptrdiff_t stride, int bs,
                                         const uint8_t *above,
                                         const uint8_t *left,
                                         int mode) {
  int k, r, c;
  int pred[33][65];
  int mean, ipred;
  const TX_SIZE tx_size = (bs == 32) ? TX_32X32 :
      ((bs == 16) ? TX_16X16 : ((bs == 8) ? TX_8X8 : (TX_4X4)));
  const int c0 = filter_intra_taps_4[tx_size][mode][0];
  const int c1 = filter_intra_taps_4[tx_size][mode][1];
  const int c2 = filter_intra_taps_4[tx_size][mode][2];
  const int c3 = filter_intra_taps_4[tx_size][mode][3];

  k = 0;
  mean = 0;
  while (k < bs) {
    mean = mean + (int)left[k];
    mean = mean + (int)above[k];
    k++;
  }
  mean = (mean + bs) / (2 * bs);

  for (r = 0; r < bs; ++r)
    pred[r + 1][0] = (int)left[r] - mean;

  for (c = 0; c < 2 * bs + 1; ++c)
    pred[0][c] = (int)above[c - 1] - mean;

  for (r = 1; r < bs + 1; ++r)
    for (c = 1; c < 2 * bs + 1 - r; ++c) {
      ipred = c0 * pred[r - 1][c] + c1 * pred[r][c - 1] +
          c2 * pred[r - 1][c - 1] + c3 * pred[r - 1][c + 1];
806
      pred[r][c] = ROUND_POWER_OF_TWO_SIGNED(ipred, FILTER_INTRA_PREC_BITS);
hui su's avatar
hui su committed
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
    }

  for (r = 0; r < bs; ++r) {
    for (c = 0; c < bs; ++c) {
      ipred = pred[r + 1][c + 1] + mean;
      dst[c] = clip_pixel(ipred);
    }
    dst += stride;
  }
}

static void dc_filter_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                               const uint8_t *above, const uint8_t *left) {
  filter_intra_predictors_4tap(dst, stride, bs, above, left, DC_PRED);
}

static void v_filter_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                               const uint8_t *above, const uint8_t *left) {
  filter_intra_predictors_4tap(dst, stride, bs, above, left, V_PRED);
}

static void h_filter_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                               const uint8_t *above, const uint8_t *left) {
  filter_intra_predictors_4tap(dst, stride, bs, above, left, H_PRED);
}

static void d45_filter_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                 const uint8_t *above, const uint8_t *left) {
  filter_intra_predictors_4tap(dst, stride, bs, above, left, D45_PRED);
}

static void d135_filter_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                  const uint8_t *above, const uint8_t *left) {
  filter_intra_predictors_4tap(dst, stride, bs, above, left, D135_PRED);
}

static void d117_filter_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                  const uint8_t *above, const uint8_t *left) {
  filter_intra_predictors_4tap(dst, stride, bs, above, left, D117_PRED);
}

static void d153_filter_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                  const uint8_t *above, const uint8_t *left) {
  filter_intra_predictors_4tap(dst, stride, bs, above, left, D153_PRED);
}

static void d207_filter_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                  const uint8_t *above, const uint8_t *left) {
  filter_intra_predictors_4tap(dst, stride, bs, above, left, D207_PRED);
}

static void d63_filter_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                 const uint8_t *above, const uint8_t *left) {
  filter_intra_predictors_4tap(dst, stride, bs, above, left, D63_PRED);
}

static void tm_filter_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                const uint8_t *above, const uint8_t *left) {
  filter_intra_predictors_4tap(dst, stride, bs, above, left, TM_PRED);
}

static void (*filter_intra_predictors[EXT_INTRA_MODES])(uint8_t *dst,
    ptrdiff_t stride, int bs, const uint8_t *above, const uint8_t *left) = {
        dc_filter_predictor, v_filter_predictor, h_filter_predictor,
        d45_filter_predictor, d135_filter_predictor, d117_filter_predictor,
        d153_filter_predictor, d207_filter_predictor, d63_filter_predictor,
        tm_filter_predictor,
};

#if CONFIG_VP9_HIGHBITDEPTH
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
static int highbd_intra_subpel_interp(int base, int shift, const uint16_t *ref,
                                      int ref_start_idx, int ref_end_idx,
                                      INTRA_FILTER filter_type) {
  int val, k, idx, filter_idx = 0;
  const int16_t *filter = NULL;

  if (filter_type == INTRA_FILTER_LINEAR) {
    val = ref[base] * (256 - shift) + ref[base + 1] * shift;
    val = ROUND_POWER_OF_TWO(val, 8);
  } else {
    filter_idx = ROUND_POWER_OF_TWO(shift, 8 - SUBPEL_BITS);
    filter = vp10_intra_filter_kernels[filter_type][filter_idx];

    if (filter_idx < (1 << SUBPEL_BITS)) {
      val = 0;
      for (k = 0; k < SUBPEL_TAPS; ++k) {
        idx = base + 1 - (SUBPEL_TAPS / 2) + k;
        idx = VPXMAX(VPXMIN(idx, ref_end_idx), ref_start_idx);
        val += ref[idx] * filter[k];
      }
      val = ROUND_POWER_OF_TWO(val, FILTER_BITS);
    } else {
      val = ref[base + 1];
    }
  }

  return val;
}

hui su's avatar
hui su committed
906
907
908
// Directional prediction, zone 1: 0 < angle < 90
static void highbd_dr_prediction_z1(uint16_t *dst, ptrdiff_t stride, int bs,
                                    const uint16_t *above, const uint16_t *left,
909
910
                                    int dx, int dy, int bd,
                                    INTRA_FILTER filter_type) {
hui su's avatar
hui su committed
911
912
913
914
915
916
917
918
919
920
  int r, c, x, y, base, shift, val;

  (void)left;
  (void)dy;
  assert(dy == 1);
  assert(dx < 0);

  for (r = 0; r < bs; ++r) {
    y = r + 1;
    for (c = 0; c < bs; ++c) {
921
      x = (c << 8) - y * dx;
hui su's avatar
hui su committed
922
      base = x >> 8;
923
      shift = x - (base << 8);
hui su's avatar
hui su committed
924
      if (base < 2 * bs - 1) {
925
926
        val = highbd_intra_subpel_interp(base, shift, above, 0, 2 * bs - 1,
                                         filter_type);
hui su's avatar
hui su committed
927
928
929
930
931
932
933
934
935
936
937
938
        dst[c] = clip_pixel_highbd(val, bd);
      } else {
        dst[c] = above[2 * bs - 1];
      }
    }
    dst += stride;
  }
}

// Directional prediction, zone 2: 90 < angle < 180
static void highbd_dr_prediction_z2(uint16_t *dst, ptrdiff_t stride, int bs,
                                    const uint16_t *above, const uint16_t *left,
939
940
941
                                    int dx, int dy, int bd,
                                    INTRA_FILTER filter_type) {
  int r, c, x, y, shift, val, base;
hui su's avatar
hui su committed
942
943
944
945
946
947
948

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

  for (r = 0; r < bs; ++r) {
    for (c = 0; c < bs; ++c) {
      y = r + 1;
949
950
951
952
953
954
      x = (c << 8) - y * dx;
      base = x >> 8;
      if (base >= -1) {
        shift = x - (base << 8);
        val = highbd_intra_subpel_interp(base, shift, above, -1, bs - 1,
                                         filter_type);
hui su's avatar
hui su committed
955
956
      } else {
        x = c + 1;
957
        y = (r << 8) - x * dy;
hui su's avatar
hui su committed
958
959
        base = y >> 8;
        if (base >= 0) {
960
961
962
          shift = y - (base  << 8);
          val = highbd_intra_subpel_interp(base, shift, left, 0, bs - 1,
                                           filter_type);
hui su's avatar
hui su committed
963
        } else {
964
          val = left[0];
hui su's avatar
hui su committed
965
966
967
968
969
970
971
972
973
974
975
        }
      }
      dst[c] = clip_pixel_highbd(val, bd);
    }
    dst += stride;
  }
}

// Directional prediction, zone 3: 180 < angle < 270
static void highbd_dr_prediction_z3(uint16_t *dst, ptrdiff_t stride, int bs,
                                    const uint16_t *above, const uint16_t *left,
976
977
                                    int dx, int dy, int bd,
                                    INTRA_FILTER filter_type) {
hui su's avatar
hui su committed
978
979
980
981
982
983
984
985
986
987
  int r, c, x, y, base, shift, val;

  (void)above;
  (void)dx;
  assert(dx == 1);
  assert(dy < 0);

  for (r = 0; r < bs; ++r) {
    for (c = 0; c < bs; ++c) {
      x = c + 1;
988
      y = (r << 8) - x * dy;
hui su's avatar
hui su committed
989
      base = y >> 8;
990
      shift = y - (base << 8);
991
      if (base < 2 * bs - 1) {
992
993
        val = highbd_intra_subpel_interp(base, shift, left, 0, 2 * bs - 1,
                                         filter_type);
hui su's avatar
hui su committed
994
995
        dst[c] = clip_pixel_highbd(val, bd);
      } else {
996
        dst[c] = left[2 * bs - 1];
hui su's avatar
hui su committed
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
      }
    }
    dst += stride;
  }
}

static INLINE void highbd_v_predictor(uint16_t *dst, ptrdiff_t stride,
                                      int bs, const uint16_t *above,
                                      const uint16_t *left, int bd) {
  int r;
  (void) left;
  (void) bd;
  for (r = 0; r < bs; r++) {
    memcpy(dst, above, bs * sizeof(uint16_t));
    dst += stride;
  }
}

static INLINE void highbd_h_predictor(uint16_t *dst, ptrdiff_t stride,
                                      int bs, const uint16_t *above,
                                      const uint16_t *left, int bd) {
  int r;
  (void) above;
  (void) bd;
  for (r = 0; r < bs; r++) {
    vpx_memset16(dst, left[r], bs);
    dst += stride;
  }
}

static void highbd_dr_predictor(uint16_t *dst, ptrdiff_t stride, int bs,
                                const uint16_t *above, const uint16_t *left,
1029
                                int angle, int bd, INTRA_FILTER filter) {
1030
1031
  const int dx = get_dx(angle);
  const int dy = get_dy(angle);
1032
  assert(angle > 0 && angle < 270);
hui su's avatar
hui su committed
1033
1034

  if (angle > 0 && angle < 90) {
1035
    highbd_dr_prediction_z1(dst, stride, bs, above, left, dx, dy, bd, filter);
hui su's avatar
hui su committed
1036
  } else if (angle > 90 && angle < 180) {
1037
    highbd_dr_prediction_z2(dst, stride, bs, above, left, dx, dy, bd, filter);
hui su's avatar
hui su committed
1038
  } else if (angle > 180 && angle < 270) {
1039
    highbd_dr_prediction_z3(dst, stride, bs, above, left, dx, dy, bd, filter);
hui su's avatar
hui su committed
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
  } else if (angle == 90) {
    highbd_v_predictor(dst, stride, bs, above, left, bd);
  } else if (angle == 180) {
    highbd_h_predictor(dst, stride, bs, above, left, bd);
  }
}

static void highbd_filter_intra_predictors_4tap(uint16_t *dst, ptrdiff_t stride,
                                                int bs, const uint16_t *above,
                                                const uint16_t *left, int mode,
                                                int bd) {
  int k, r, c;
  int pred[33][65];
  int mean, ipred;
  const TX_SIZE tx_size = (bs == 32) ? TX_32X32 :
      ((bs == 16) ? TX_16X16 : ((bs == 8) ? TX_8X8 : (TX_4X4)));
  const int c0 = filter_intra_taps_4[tx_size][mode][0];
  const int c1 = filter_intra_taps_4[tx_size][mode][1];
  const int c2 = filter_intra_taps_4[tx_size][mode][2];
  const int c3 = filter_intra_taps_4[tx_size][mode][3];

  k = 0;
  mean = 0;
  while (k < bs) {
    mean = mean + (int)left[k];
    mean = mean + (int)above[k];
    k++;
  }
  mean = (mean + bs) / (2 * bs);

  for (r = 0; r < bs; ++r)
    pred[r + 1][0] = (int)left[r] - mean;

  for (c = 0; c < 2 * bs + 1; ++c)
    pred[0][c] = (int)above[c - 1] - mean;

  for (r = 1; r < bs + 1; ++r)
    for (c = 1; c < 2 * bs + 1 - r; ++c) {
      ipred = c0 * pred[r - 1][c] + c1 * pred[r][c - 1] +
          c2 * pred[r - 1][c - 1] + c3 * pred[r - 1][c + 1];
1080
      pred[r][c] = ROUND_POWER_OF_TWO_SIGNED(ipred, FILTER_INTRA_PREC_BITS);
hui su's avatar
hui su committed
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
    }

  for (r = 0; r < bs; ++r) {
    for (c = 0; c < bs; ++c) {
      ipred = pred[r + 1][c + 1] + mean;
      dst[c] = clip_pixel_highbd(ipred, bd);
    }
    dst += stride;
  }
}

static void highbd_dc_filter_predictor(uint16_t *dst, ptrdiff_t stride,
                                       int bs, const uint16_t *above,
                                       const uint16_t *left, int bd) {
  highbd_filter_intra_predictors_4tap(dst, stride, bs, above, left, DC_PRED,
                                      bd);
}

static void highbd_v_filter_predictor(uint16_t *dst, ptrdiff_t stride,
                                      int bs, const uint16_t *above,
                                      const uint16_t *left, int bd) {
  highbd_filter_intra_predictors_4tap(dst, stride, bs, above, left, V_PRED,
                                      bd);
}

static void highbd_h_filter_predictor(uint16_t *dst, ptrdiff_t stride,
                                      int bs, const uint16_t *above,
                                      const uint16_t *left, int bd) {
  highbd_filter_intra_predictors_4tap(dst, stride, bs, above, left, H_PRED,
                                      bd);
}

static void highbd_d45_filter_predictor(uint16_t *dst, ptrdiff_t stride,
                                        int bs, const uint16_t *above,
                                        const uint16_t *left, int bd) {
  highbd_filter_intra_predictors_4tap(dst, stride, bs, above, left, D45_PRED,
                                      bd);
}

static void highbd_d135_filter_predictor(uint16_t *dst, ptrdiff_t stride,
                                         int bs, const uint16_t *above,
                                         const uint16_t *left, int bd) {
  highbd_filter_intra_predictors_4tap(dst, stride, bs, above, left, D135_PRED,
                                      bd);
}

static void highbd_d117_filter_predictor(uint16_t *dst, ptrdiff_t stride,
                                         int bs, const uint16_t *above,
                                         const uint16_t *left, int bd) {
  highbd_filter_intra_predictors_4tap(dst, stride, bs, above, left, D117_PRED,
                                      bd);
}

static void highbd_d153_filter_predictor(uint16_t *dst, ptrdiff_t stride,
                                         int bs, const uint16_t *above,
                                         const uint16_t *left, int bd) {
  highbd_filter_intra_predictors_4tap(dst, stride, bs, above, left, D153_PRED,
                                      bd);
}

static void highbd_d207_filter_predictor(uint16_t *dst, ptrdiff_t stride,
                                         int bs, const uint16_t *above,
                                         const uint16_t *left, int bd) {
  highbd_filter_intra_predictors_4tap(dst, stride, bs, above, left, D207_PRED,
                                      bd);
}

static void highbd_d63_filter_predictor(uint16_t *dst, ptrdiff_t stride,
                                        int bs, const uint16_t *above,
                                        const uint16_t *left, int bd) {
  highbd_filter_intra_predictors_4tap(dst, stride, bs, above, left, D63_PRED,
                                      bd);
}

static void highbd_tm_filter_predictor(uint16_t *dst, ptrdiff_t stride,
                                       int bs, const uint16_t *above,
                                       const uint16_t *left, int bd) {
  highbd_filter_intra_predictors_4tap(dst, stride, bs, above, left, TM_PRED,
                                      bd);
}

static void (*highbd_filter_intra_predictors[EXT_INTRA_MODES])(uint16_t *dst,
    ptrdiff_t stride, int bs, const uint16_t *above, const uint16_t *left,
    int bd) = {
        highbd_dc_filter_predictor, highbd_v_filter_predictor,
        highbd_h_filter_predictor, highbd_d45_filter_predictor,
        highbd_d135_filter_predictor, highbd_d117_filter_predictor,
        highbd_d153_filter_predictor, highbd_d207_filter_predictor,
        highbd_d63_filter_predictor, highbd_tm_filter_predictor,
};
#endif  // CONFIG_VP9_HIGHBITDEPTH
#endif  // CONFIG_EXT_INTRA

Jingning Han's avatar
Jingning Han committed
1174
1175
1176
1177
1178
1179
1180
1181
#if CONFIG_VP9_HIGHBITDEPTH
static void build_intra_predictors_high(const MACROBLOCKD *xd,
                                        const uint8_t *ref8,
                                        int ref_stride,
                                        uint8_t *dst8,
                                        int dst_stride,
                                        PREDICTION_MODE mode,
                                        TX_SIZE tx_size,
1182
1183
                                        int n_top_px, int n_topright_px,
                                        int n_left_px, int n_bottomleft_px,
hui su's avatar
hui su committed
1184
                                        int plane) {
Jingning Han's avatar
Jingning Han committed
1185
1186
1187
  int i;
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
  uint16_t *ref = CONVERT_TO_SHORTPTR(ref8);
1188
1189
  DECLARE_ALIGNED(16, uint16_t, left_col[MAX_SB_SIZE]);
  DECLARE_ALIGNED(16, uint16_t, above_data[MAX_SB_SIZE + 16]);
Jingning Han's avatar
Jingning Han committed
1190
1191
  uint16_t *above_row = above_data + 16;
  const uint16_t *const_above_row = above_row;
1192
  const int bs = 4 * num_4x4_blocks_wide_txsize_lookup[tx_size];
hui su's avatar
hui su committed
1193
1194
  int need_left = extend_modes[mode] & NEED_LEFT;
  int need_above = extend_modes[mode] & NEED_ABOVE;
1195
  const uint16_t *above_ref = ref - ref_stride;
hui su's avatar
hui su committed
1196
  int base = 128 << (xd->bd - 8);
Jingning Han's avatar
Jingning Han committed
1197
1198
1199
1200
1201
1202
  // 127 127 127 .. 127 127 127 127 127 127
  // 129  A   B  ..  Y   Z
  // 129  C   D  ..  W   X
  // 129  E   F  ..  U   V
  // 129  G   H  ..  S   T   T   T   T   T

hui su's avatar
hui su committed
1203
1204
1205
1206
1207
#if CONFIG_EXT_INTRA
  const EXT_INTRA_MODE_INFO *ext_intra_mode_info =
      &xd->mi[0]->mbmi.ext_intra_mode_info;
  const EXT_INTRA_MODE ext_intra_mode =
      ext_intra_mode_info->ext_intra_mode[plane != 0];
hui su's avatar
hui su committed
1208
  int p_angle = 0;
hui su's avatar
hui su committed
1209

hui su's avatar
hui su committed
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
  if (mode != DC_PRED && mode != TM_PRED &&
      xd->mi[0]->mbmi.sb_type >= BLOCK_8X8) {
    p_angle = mode_to_angle_map[mode] +
        xd->mi[0]->mbmi.angle_delta[plane != 0] * ANGLE_STEP;
    if (p_angle <= 90)
      need_above = 1, need_left = 0;
    else if (p_angle < 180)
      need_above = 1, need_left = 1;
    else
      need_above = 0, need_left = 1;
  }

  if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
    EXT_INTRA_MODE ext_intra_mode =
        ext_intra_mode_info->ext_intra_mode[plane != 0];
    need_left = ext_intra_extend_modes[ext_intra_mode] & NEED_LEFT;
    need_above = ext_intra_extend_modes[ext_intra_mode] & NEED_ABOVE;
hui su's avatar
hui su committed
1227
1228
1229
  }
#endif  // CONFIG_EXT_INTRA

1230
  (void) plane;
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
  assert(n_top_px >= 0);
  assert(n_topright_px >= 0);
  assert(n_left_px >= 0);
  assert(n_bottomleft_px >= 0);

  if ((!need_above && n_left_px == 0) || (!need_left && n_top_px == 0)) {
    int i;
    const int val = (n_left_px == 0) ? base + 1 : base - 1;
    for (i = 0; i < bs; ++i) {
      vpx_memset16(dst, val, bs);
      dst += dst_stride;
    }
    return;
  }
1245
1246

  // NEED_LEFT
hui su's avatar
hui su committed
1247
1248
1249
1250
1251
  if (need_left) {
#if CONFIG_EXT_INTRA
    int need_bottom;
    if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
        need_bottom = 0;
hui su's avatar
hui su committed
1252
1253
1254
    } else if (mode != DC_PRED && mode != TM_PRED &&
        xd->mi[0]->mbmi.sb_type >= BLOCK_8X8) {
        need_bottom = p_angle > 180;
hui su's avatar
hui su committed
1255
1256
1257
1258
    } else {
      need_bottom = !!(extend_modes[mode] & NEED_BOTTOMLEFT);
    }
#else
1259
    const int need_bottom = !!(extend_modes[mode] & NEED_BOTTOMLEFT);
hui su's avatar
hui su committed
1260
#endif  // CONFIG_EXT_INTRA
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
    i = 0;
    if (n_left_px > 0) {
      for (; i < n_left_px; i++)
        left_col[i] = ref[i * ref_stride - 1];
      if (need_bottom && n_bottomleft_px > 0) {
        assert(i == bs);
        for (; i < bs + n_bottomleft_px; i++)
          left_col[i] = ref[i * ref_stride - 1];
      }
      if (i < (bs << need_bottom))
Yaowu Xu's avatar
Yaowu Xu committed
1271
        vpx_memset16(&left_col[i], left_col[i - 1], (bs << need_bottom) - i);
1272
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
1273
      vpx_memset16(left_col, base + 1, bs << need_bottom);
1274
1275
1276
1277
    }
  }

  // NEED_ABOVE
hui su's avatar
hui su committed
1278
1279
1280
1281
  if (need_above) {
#if CONFIG_EXT_INTRA
    int need_right;
    if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
hui su's avatar
hui su committed
1282
1283
1284
1285
      need_right = 1;
    } else if (mode != DC_PRED && mode != TM_PRED &&
        xd->mi[0]->mbmi.sb_type >= BLOCK_8X8) {
      need_right = p_angle < 90;
hui su's avatar
hui su committed
1286
1287
1288
1289
    } else {
      need_right = !!(extend_modes[mode] & NEED_ABOVERIGHT);
    }
#else
1290
    const int need_right = !!(extend_modes[mode] & NEED_ABOVERIGHT);
hui su's avatar
hui su committed
1291
#endif  // CONFIG_EXT_INTRA
1292
1293
1294
1295
1296
1297
1298
1299
1300
    if (n_top_px > 0) {
      memcpy(above_row, above_ref, n_top_px * 2);
      i = n_top_px;
      if (need_right && n_topright_px > 0) {
        assert(n_top_px == bs);
        memcpy(above_row + bs, above_ref + bs, n_topright_px * 2);
        i += n_topright_px;
      }
      if (i < (bs << need_right))
Yaowu Xu's avatar
Yaowu Xu committed
1301
        vpx_memset16(&above_row[i], above_row[i - 1], (bs << need_right) - i);
1302
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
1303
      vpx_memset16(above_row, base - 1, bs << need_right);
1304
1305
1306
    }
  }

hui su's avatar
hui su committed
1307
1308
#if CONFIG_EXT_INTRA
  if (ext_intra_mode_info->use_ext_intra_mode[plane != 0] ||
hui su's avatar
hui su committed
1309
1310
1311
      (extend_modes[mode] & NEED_ABOVELEFT) ||
      (mode != DC_PRED && mode != TM_PRED &&
        xd->mi[0]->mbmi.sb_type >= BLOCK_8X8)) {
1312
1313
1314
    above_row[-1] = n_top_px > 0 ?
        (n_left_px > 0 ? above_ref[-1] : base + 1) : base - 1;
  }
hui su's avatar
hui su committed
1315
1316
#else
  if ((extend_modes[mode] & NEED_ABOVELEFT)) {
1317
1318
1319
    above_row[-1] = n_top_px > 0 ?
        (n_left_px > 0 ? above_ref[-1] : base + 1) : base - 1;
  }
hui su's avatar
hui su committed
1320
#endif  // CONFIG_EXT_INTRA
Jingning Han's avatar
Jingning Han committed
1321

hui su's avatar
hui su committed
1322
1323
#if CONFIG_EXT_INTRA
  if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
hui su's avatar
hui su committed
1324
    highbd_filter_intra_predictors[ext_intra_mode](dst, dst_stride, bs,
hui su's avatar
hui su committed
1325
        const_above_row, left_col, xd->bd);
hui su's avatar
hui su committed
1326
1327
1328
1329
1330
    return;
  }

  if (mode != DC_PRED && mode != TM_PRED &&
      xd->mi[0]->mbmi.sb_type >= BLOCK_8X8) {
1331
    INTRA_FILTER filter = INTRA_FILTER_LINEAR;
Alex Converse's avatar
Alex Converse committed
1332
    if (plane == 0 && vp10_is_intra_filter_switchable(p_angle))
1333
      filter = xd->mi[0]->mbmi.intra_filter;
hui su's avatar
hui su committed
1334
    highbd_dr_predictor(dst, dst_stride, bs, const_above_row, left_col,
1335
                        p_angle, xd->bd, filter);
hui su's avatar
hui su committed
1336
1337
1338
1339
    return;
  }
#endif  // CONFIG_EXT_INTRA

Jingning Han's avatar
Jingning Han committed
1340
1341
  // predict
  if (mode == DC_PRED) {
1342
1343
1344
    dc_pred_high[n_left_px > 0][n_top_px > 0][tx_size](dst, dst_stride,
                                                       const_above_row,
                                                       left_col, xd->bd);
Jingning Han's avatar
Jingning Han committed
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
  } else {
    pred_high[mode][tx_size](dst, dst_stride, const_above_row, left_col,
                             xd->bd);
  }
}
#endif  // CONFIG_VP9_HIGHBITDEPTH

static void build_intra_predictors(const MACROBLOCKD *xd, const uint8_t *ref,
                                   int ref_stride, uint8_t *dst, int dst_stride,
                                   PREDICTION_MODE mode, TX_SIZE tx_size,
1355
1356
                                   int n_top_px, int n_topright_px,
                                   int n_left_px, int n_bottomleft_px,
hui su's avatar
hui su committed
1357
                                   int plane) {
Jingning Han's avatar
Jingning Han committed
1358
  int i;
1359
  DECLARE_ALIGNED(16, uint8_t, left_col[MAX_SB_SIZE]);
1360
  const uint8_t *above_ref = ref - ref_stride;
1361
  DECLARE_ALIGNED(16, uint8_t, above_data[MAX_SB_SIZE + 16]);
Jingning Han's avatar
Jingning Han committed
1362
1363
  uint8_t *above_row = above_data + 16;
  const uint8_t *const_above_row = above_row;
1364
  const int bs = 4 * num_4x4_blocks_wide_txsize_lookup[tx_size];
hui su's avatar
hui su committed
1365
1366
1367
1368
1369
1370
1371
  int need_left = extend_modes[mode] & NEED_LEFT;
  int need_above = extend_modes[mode] & NEED_ABOVE;
#if CONFIG_EXT_INTRA
  const EXT_INTRA_MODE_INFO *ext_intra_mode_info =
      &xd->mi[0]->mbmi.ext_intra_mode_info;
  const EXT_INTRA_MODE ext_intra_mode =
      ext_intra_mode_info->ext_intra_mode[plane != 0];
hui su's avatar
hui su committed
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
  int p_angle = 0;

  if (mode != DC_PRED && mode != TM_PRED &&
      xd->mi[0]->mbmi.sb_type >= BLOCK_8X8) {
    p_angle = mode_to_angle_map[mode] +
        xd->mi[0]->mbmi.angle_delta[plane != 0] * ANGLE_STEP;
    if (p_angle <= 90)
      need_above = 1, need_left = 0;
    else if (p_angle < 180)
      need_above = 1, need_left = 1;
    else
      need_above = 0, need_left = 1;
  }

  if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
    EXT_INTRA_MODE ext_intra_mode =
        ext_intra_mode_info->ext_intra_mode[plane != 0];
    need_left = ext_intra_extend_modes[ext_intra_mode] & NEED_LEFT;
    need_above = ext_intra_extend_modes[ext_intra_mode] & NEED_ABOVE;
hui su's avatar
hui su committed
1391
1392
  }
#endif  // CONFIG_EXT_INTRA
Jingning Han's avatar
Jingning Han committed
1393
1394
1395
1396
1397
1398
1399
1400

  // 127 127 127 .. 127 127 127 127 127 127
  // 129  A   B  ..  Y   Z
  // 129  C   D  ..  W   X
  // 129  E   F  ..  U   V
  // 129  G   H  ..  S   T   T   T   T   T
  // ..

1401
1402
1403
1404
1405
1406
  (void) xd;
  (void) plane;
  assert(n_top_px >= 0);
  assert(n_topright_px >= 0);
  assert(n_left_px >= 0);
  assert(n_bottomleft_px >= 0);
Jingning Han's avatar
Jingning Han committed
1407

1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
  if ((!need_above && n_left_px == 0) || (!need_left && n_top_px == 0)) {
    int i;
    const int val = (n_left_px == 0) ? 129 : 127;
    for (i = 0; i < bs; ++i) {
      memset(dst, val, bs);
      dst += dst_stride;
    }
    return;
  }

Jingning Han's avatar
Jingning Han committed
1418
  // NEED_LEFT
hui su's avatar
hui su committed
1419
1420
1421
1422
  if (need_left) {
#if CONFIG_EXT_INTRA
    int need_bottom;
    if