reconintra.c 76.4 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 17 18 19
#include "./av1_rtcd.h"
#include "./aom_config.h"
#include "./aom_dsp_rtcd.h"
#include "aom_dsp/aom_dsp_common.h"
#include "aom_mem/aom_mem.h"
#include "aom_ports/aom_once.h"
20 21
#include "aom_ports/mem.h"
#include "aom_ports/system_state.h"
22 23
#include "av1/common/reconintra.h"
#include "av1/common/onyxc_int.h"
Luc Trudeau's avatar
Luc Trudeau committed
24 25 26
#if CONFIG_CFL
#include "av1/common/cfl.h"
#endif
Jingning Han's avatar
Jingning Han committed
27

28 29 30 31 32 33 34 35
enum {
  NEED_LEFT = 1 << 1,
  NEED_ABOVE = 1 << 2,
  NEED_ABOVERIGHT = 1 << 3,
  NEED_ABOVELEFT = 1 << 4,
  NEED_BOTTOMLEFT = 1 << 5,
};

36 37 38
#if CONFIG_INTRA_EDGE
#define INTRA_EDGE_FILT 3
#define INTRA_EDGE_TAPS 5
Joe Young's avatar
Joe Young committed
39
#define MAX_UPSAMPLE_SZ 16
Joe Young's avatar
Joe Young committed
40
#endif  // CONFIG_INTRA_EDGE
41

42 43 44 45 46 47
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
Hui Su's avatar
Hui Su committed
48 49 50 51
  NEED_LEFT | NEED_ABOVE | NEED_ABOVELEFT,  // D113
  NEED_LEFT | NEED_ABOVE | NEED_ABOVELEFT,  // D157
  NEED_LEFT | NEED_BOTTOMLEFT,              // D203
  NEED_ABOVE | NEED_ABOVERIGHT,             // D67
Urvang Joshi's avatar
Urvang Joshi committed
52
  NEED_LEFT | NEED_ABOVE,                   // SMOOTH
Urvang Joshi's avatar
Urvang Joshi committed
53 54
  NEED_LEFT | NEED_ABOVE,                   // SMOOTH_V
  NEED_LEFT | NEED_ABOVE,                   // SMOOTH_H
Urvang Joshi's avatar
Urvang Joshi committed
55
  NEED_LEFT | NEED_ABOVE | NEED_ABOVELEFT,  // PAETH
56
};
Jingning Han's avatar
Jingning Han committed
57

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
// Tables to store if the top-right reference pixels are available. The flags
// are represented with bits, packed into 8-bit integers. E.g., for the 32x32
// blocks in a 128x128 superblock, the index of the "o" block is 10 (in raster
// order), so its flag is stored at the 3rd bit of the 2nd entry in the table,
// i.e. (table[10 / 8] >> (10 % 8)) & 1.
//       . . . .
//       . . . .
//       . . o .
//       . . . .
uint8_t has_tr_4x4[128] = {
  255, 255, 255, 255, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
  127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
  255, 127, 255, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
  127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
  255, 255, 255, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
  127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
  255, 127, 255, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
  127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
76
};
77 78 79 80 81 82
uint8_t has_tr_4x8[64] = {
  255, 255, 255, 255, 119, 119, 119, 119, 127, 127, 127, 127, 119,
  119, 119, 119, 255, 127, 255, 127, 119, 119, 119, 119, 127, 127,
  127, 127, 119, 119, 119, 119, 255, 255, 255, 127, 119, 119, 119,
  119, 127, 127, 127, 127, 119, 119, 119, 119, 255, 127, 255, 127,
  119, 119, 119, 119, 127, 127, 127, 127, 119, 119, 119, 119,
83
};
84 85 86 87 88
uint8_t has_tr_8x4[64] = {
  255, 255, 0, 0, 85, 85, 0, 0, 119, 119, 0, 0, 85, 85, 0, 0,
  127, 127, 0, 0, 85, 85, 0, 0, 119, 119, 0, 0, 85, 85, 0, 0,
  255, 127, 0, 0, 85, 85, 0, 0, 119, 119, 0, 0, 85, 85, 0, 0,
  127, 127, 0, 0, 85, 85, 0, 0, 119, 119, 0, 0, 85, 85, 0, 0,
89
};
90 91 92
uint8_t has_tr_8x8[32] = {
  255, 255, 85, 85, 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85,
  255, 127, 85, 85, 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85,
93
};
94 95 96
uint8_t has_tr_8x16[16] = {
  255, 255, 119, 119, 127, 127, 119, 119,
  255, 127, 119, 119, 127, 127, 119, 119,
97
};
98 99
uint8_t has_tr_16x8[16] = {
  255, 0, 85, 0, 119, 0, 85, 0, 127, 0, 85, 0, 119, 0, 85, 0,
100
};
101 102
uint8_t has_tr_16x16[8] = {
  255, 85, 119, 85, 127, 85, 119, 85,
103
};
104 105
uint8_t has_tr_16x32[4] = { 255, 119, 127, 119 };
uint8_t has_tr_32x16[4] = { 15, 5, 7, 5 };
106 107 108 109 110 111 112 113 114 115 116
uint8_t has_tr_32x32[2] = { 95, 87 };
uint8_t has_tr_32x64[1] = { 127 };
uint8_t has_tr_64x32[1] = { 19 };
uint8_t has_tr_64x64[1] = { 7 };
uint8_t has_tr_64x128[1] = { 3 };
uint8_t has_tr_128x64[1] = { 1 };
uint8_t has_tr_128x128[1] = { 1 };
uint8_t has_tr_4x16[32] = {
  255, 255, 255, 255, 127, 127, 127, 127, 255, 127, 255,
  127, 127, 127, 127, 127, 255, 255, 255, 127, 127, 127,
  127, 127, 255, 127, 255, 127, 127, 127, 127, 127,
117
};
118 119 120
uint8_t has_tr_16x4[32] = {
  255, 0, 0, 0, 85, 0, 0, 0, 119, 0, 0, 0, 85, 0, 0, 0,
  127, 0, 0, 0, 85, 0, 0, 0, 119, 0, 0, 0, 85, 0, 0, 0,
121
};
122 123
uint8_t has_tr_8x32[8] = {
  255, 255, 127, 127, 255, 127, 127, 127,
124
};
125 126 127
uint8_t has_tr_32x8[8] = {
  15, 0, 5, 0, 7, 0, 5, 0,
};
128 129 130 131
uint8_t has_tr_16x64[2] = { 255, 127 };
uint8_t has_tr_64x16[2] = { 3, 1 };
uint8_t has_tr_32x128[1] = { 15 };
uint8_t has_tr_128x32[1] = { 1 };
132

133
#if CONFIG_EXT_PARTITION
134 135 136 137 138 139 140 141 142 143 144 145 146
static const uint8_t *const has_tr_tables[BLOCK_SIZES_ALL] = {
  // 4X4
  has_tr_4x4,
  // 4X8,       8X4,            8X8
  has_tr_4x8, has_tr_8x4, has_tr_8x8,
  // 8X16,      16X8,           16X16
  has_tr_8x16, has_tr_16x8, has_tr_16x16,
  // 16X32,     32X16,          32X32
  has_tr_16x32, has_tr_32x16, has_tr_32x32,
  // 32X64,     64X32,          64X64
  has_tr_32x64, has_tr_64x32, has_tr_64x64,
  // 64x128,    128x64,         128x128
  has_tr_64x128, has_tr_128x64, has_tr_128x128,
147
#if CONFIG_EXT_PARTITION_TYPES
148 149 150 151 152 153
  // 4x16,      16x4,            8x32
  has_tr_4x16, has_tr_16x4, has_tr_8x32,
  // 32x8,      16x64,           64x16
  has_tr_32x8, has_tr_16x64, has_tr_64x16,
  // 32x128,    128x32
  has_tr_32x128, has_tr_128x32
154
#else
155
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
156
#endif
157
};
158
#else
159 160 161
static const uint8_t *const has_tr_tables[BLOCK_SIZES_ALL] = {
  // 4X4
  has_tr_8x8,
162
  // 4X8,         8X4,            8X8
163
  has_tr_8x16, has_tr_16x8, has_tr_16x16,
164
  // 8X16,        16X8,           16X16
165
  has_tr_16x32, has_tr_32x16, has_tr_32x32,
166
  // 16X32,       32X16,          32X32
167
  has_tr_32x64, has_tr_64x32, has_tr_64x64,
168
  // 32X64,       64X32,          64X64
169 170
  has_tr_64x128, has_tr_128x64, has_tr_128x128,

171
#if CONFIG_EXT_PARTITION_TYPES
172
  // 4x16,        16x4,           8x32
173
  has_tr_8x32, has_tr_32x8, has_tr_16x64,
174
  // 32x8,        16x64,          64x16
175
  has_tr_64x16, has_tr_32x128, has_tr_128x32
176
#else
177
  NULL, NULL, NULL, NULL, NULL, NULL
178
#endif
179 180 181
};
#endif  // CONFIG_EXT_PARTITION

182
#if CONFIG_EXT_PARTITION_TYPES
183 184 185 186 187 188 189 190 191 192 193
uint8_t has_tr_vert_8x8[32] = {
  255, 255, 0, 0, 119, 119, 0, 0, 127, 127, 0, 0, 119, 119, 0, 0,
  255, 127, 0, 0, 119, 119, 0, 0, 127, 127, 0, 0, 119, 119, 0, 0,
};
uint8_t has_tr_vert_16x16[8] = {
  255, 0, 119, 0, 127, 0, 119, 0,
};
uint8_t has_tr_vert_32x32[2] = { 15, 7 };
uint8_t has_tr_vert_64x64[1] = { 3 };

// The _vert_* tables are like the ordinary tables above, but describe the
194 195 196 197 198 199 200 201
// 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
202
#if CONFIG_EXT_PARTITION
203 204 205 206 207 208 209 210 211 212 213 214
static const uint8_t *const has_tr_vert_tables[BLOCK_SIZES] = {
  // 4X4
  NULL,
  // 4X8,      8X4,         8X8
  has_tr_4x8, NULL, has_tr_vert_8x8,
  // 8X16,     16X8,        16X16
  has_tr_8x16, NULL, has_tr_vert_16x16,
  // 16X32,    32X16,       32X32
  has_tr_16x32, NULL, has_tr_vert_32x32,
  // 32X64,    64X32,       64X64
  has_tr_32x64, NULL, has_tr_vert_64x64,
  // 64x128,   128x64,      128x128
215
  has_tr_64x128, NULL, has_tr_128x128
216 217
};
#else
218 219 220 221 222 223 224 225 226 227
static const uint8_t *const has_tr_vert_tables[BLOCK_SIZES] = {
  // 4X4
  NULL,
  // 4X8,       8X4,         8X8
  has_tr_8x16, NULL, has_tr_vert_16x16,
  // 8X16,      16X8,        16X16
  has_tr_16x32, NULL, has_tr_vert_32x32,
  // 16X32,     32X16,       32X32
  has_tr_32x64, NULL, has_tr_vert_64x64,
  // 32X64,     64X32,       64X64
228
  has_tr_64x128, NULL, has_tr_128x128
229
};
230
#endif
231
#endif  // CONFIG_EXT_PARTITION_TYPES
232

233
static const uint8_t *get_has_tr_table(PARTITION_TYPE partition,
234
                                       BLOCK_SIZE bsize) {
235
  const uint8_t *ret = NULL;
236
#if CONFIG_EXT_PARTITION_TYPES
237 238 239
  // 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);
240
    ret = has_tr_vert_tables[bsize];
241
  } else {
242
    ret = has_tr_tables[bsize];
243 244 245
  }
#else
  (void)partition;
246
  ret = has_tr_tables[bsize];
247
#endif  // CONFIG_EXT_PARTITION_TYPES
248 249 250 251 252 253 254
  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,
255
                         int col_off, int ss_x, int ss_y) {
256 257 258 259 260
  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];
261

262
  if (row_off > 0) {  // Just need to check if enough pixels on the right.
263
#if CONFIG_EXT_PARTITION
264
    if (block_size_wide[bsize] > block_size_wide[BLOCK_64X64]) {
265 266 267 268 269 270 271
      // Special case: For 128x128 blocks, the transform unit whose
      // top-right corner is at the center of the block does in fact have
      // pixels available at its top-right corner.
      if (row_off == mi_size_high[BLOCK_64X64] >> ss_y &&
          col_off + top_right_count_unit == mi_size_wide[BLOCK_64X64] >> ss_x) {
        return 1;
      }
272 273 274 275
      const int plane_bw_unit_64 = mi_size_wide[BLOCK_64X64] >> ss_x;
      const int col_off_64 = col_off % plane_bw_unit_64;
      return col_off_64 + top_right_count_unit < plane_bw_unit_64;
    }
276 277
#else
    (void)ss_y;
278
#endif
279 280 281 282
    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;
283

284 285
    const int bw_in_mi_log2 = mi_width_log2_lookup[bsize];
    const int bh_in_mi_log2 = mi_height_log2_lookup[bsize];
286
    const int sb_mi_size = mi_size_high[cm->seq_params.sb_size];
287 288
    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;
289

290 291 292
    // 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;
293

294 295
    // Rightmost column of superblock (and not the top row): so top-right pixels
    // fall in the right superblock, which is not available yet.
296 297 298
    if (((blk_col_in_sb + 1) << bw_in_mi_log2) >= sb_mi_size) {
      return 0;
    }
299

300 301 302 303 304
    // General case (neither top row nor rightmost column): check if the
    // top-right block is coded before the current block.
    const int this_blk_index =
        ((blk_row_in_sb + 0) << (MAX_MIB_SIZE_LOG2 - bw_in_mi_log2)) +
        blk_col_in_sb + 0;
305 306 307 308
    const int idx1 = this_blk_index / 8;
    const int idx2 = this_blk_index % 8;
    const uint8_t *has_tr_table = get_has_tr_table(partition, bsize);
    return (has_tr_table[idx1] >> idx2) & 1;
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
// Similar to the has_tr_* tables, but store if the bottom-left reference
// pixels are available.
uint8_t has_bl_4x4[128] = {
  84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0,  1,  1,  1,  84, 85, 85,
  85, 16, 17, 17, 17, 84, 85, 85, 85, 0,  0,  1,  0,  84, 85, 85, 85, 16, 17,
  17, 17, 84, 85, 85, 85, 0,  1,  1,  1,  84, 85, 85, 85, 16, 17, 17, 17, 84,
  85, 85, 85, 0,  0,  0,  0,  84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85,
  0,  1,  1,  1,  84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0,  0,  1,
  0,  84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0,  1,  1,  1,  84, 85,
  85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0,  0,  0,  0,
};
uint8_t has_bl_4x8[64] = {
  16, 17, 17, 17, 0, 1, 1, 1, 16, 17, 17, 17, 0, 0, 1, 0,
  16, 17, 17, 17, 0, 1, 1, 1, 16, 17, 17, 17, 0, 0, 0, 0,
  16, 17, 17, 17, 0, 1, 1, 1, 16, 17, 17, 17, 0, 0, 1, 0,
  16, 17, 17, 17, 0, 1, 1, 1, 16, 17, 17, 17, 0, 0, 0, 0,
};
uint8_t has_bl_8x4[64] = {
  254, 255, 84, 85, 254, 255, 16, 17, 254, 255, 84, 85, 254, 255, 0, 1,
  254, 255, 84, 85, 254, 255, 16, 17, 254, 255, 84, 85, 254, 255, 0, 0,
  254, 255, 84, 85, 254, 255, 16, 17, 254, 255, 84, 85, 254, 255, 0, 1,
  254, 255, 84, 85, 254, 255, 16, 17, 254, 255, 84, 85, 254, 255, 0, 0,
};
uint8_t has_bl_8x8[32] = {
  84, 85, 16, 17, 84, 85, 0, 1, 84, 85, 16, 17, 84, 85, 0, 0,
  84, 85, 16, 17, 84, 85, 0, 1, 84, 85, 16, 17, 84, 85, 0, 0,
};
uint8_t has_bl_8x16[16] = {
  16, 17, 0, 1, 16, 17, 0, 0, 16, 17, 0, 1, 16, 17, 0, 0,
};
uint8_t has_bl_16x8[16] = {
  254, 84, 254, 16, 254, 84, 254, 0, 254, 84, 254, 16, 254, 84, 254, 0,
};
uint8_t has_bl_16x16[8] = {
  84, 16, 84, 0, 84, 16, 84, 0,
};
348 349 350
uint8_t has_bl_16x32[4] = { 16, 0, 16, 0 };
uint8_t has_bl_32x16[4] = { 78, 14, 78, 14 };
uint8_t has_bl_32x32[2] = { 4, 4 };
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
uint8_t has_bl_32x64[1] = { 0 };
uint8_t has_bl_64x32[1] = { 34 };
uint8_t has_bl_64x64[1] = { 0 };
uint8_t has_bl_64x128[1] = { 0 };
uint8_t has_bl_128x64[1] = { 0 };
uint8_t has_bl_128x128[1] = { 0 };
uint8_t has_bl_4x16[32] = {
  0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0,
  0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0,
};
uint8_t has_bl_16x4[32] = {
  254, 254, 254, 84, 254, 254, 254, 16, 254, 254, 254, 84, 254, 254, 254, 0,
  254, 254, 254, 84, 254, 254, 254, 16, 254, 254, 254, 84, 254, 254, 254, 0,
};
uint8_t has_bl_8x32[8] = {
  0, 1, 0, 0, 0, 1, 0, 0,
};
uint8_t has_bl_32x8[8] = {
  238, 78, 238, 14, 238, 78, 238, 14,
};
371 372 373 374
uint8_t has_bl_16x64[2] = { 0, 0 };
uint8_t has_bl_64x16[2] = { 42, 42 };
uint8_t has_bl_32x128[1] = { 0 };
uint8_t has_bl_128x32[1] = { 0 };
375 376 377 378 379 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 407 408 409 410 411 412 413 414 415 416 417

#if CONFIG_EXT_PARTITION
static const uint8_t *const has_bl_tables[BLOCK_SIZES_ALL] = {
  // 4X4
  has_bl_4x4,
  // 4X8,         8X4,         8X8
  has_bl_4x8, has_bl_8x4, has_bl_8x8,
  // 8X16,        16X8,        16X16
  has_bl_8x16, has_bl_16x8, has_bl_16x16,
  // 16X32,       32X16,       32X32
  has_bl_16x32, has_bl_32x16, has_bl_32x32,
  // 32X64,       64X32,       64X64
  has_bl_32x64, has_bl_64x32, has_bl_64x64,
  // 64x128,      128x64,      128x128
  has_bl_64x128, has_bl_128x64, has_bl_128x128,
#if CONFIG_EXT_PARTITION_TYPES
  // 4x16,        16x4,        8x32
  has_bl_4x16, has_bl_16x4, has_bl_8x32,
  // 32x8,        16x64,       64x16
  has_bl_32x8, has_bl_16x64, has_bl_64x16,
  // 32x128,      128x32
  has_bl_32x128, has_bl_128x32
#else
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
#endif
};
#else
static const uint8_t *const has_bl_tables[BLOCK_SIZES_ALL] = {
  // 4X4
  has_bl_8x8,
  // 4X8,         8X4,            8X8
  has_bl_8x16, has_bl_16x8, has_bl_16x16,
  // 8X16,        16X8,           16X16
  has_bl_16x32, has_bl_32x16, has_bl_32x32,
  // 16X32,       32X16,          32X32
  has_bl_32x64, has_bl_64x32, has_bl_64x64,
  // 32X64,       64X32,          64X64
  has_bl_64x128, has_bl_128x64, has_bl_128x128,

#if CONFIG_EXT_PARTITION_TYPES
  // 4x16,        16x4,           8x32
  has_bl_8x32, has_bl_32x8, has_bl_16x64,
  // 32x8,        16x64,          64x16
418
  has_bl_64x16, has_bl_32x128, has_bl_128x32
419
#else
420
  NULL, NULL, NULL, NULL, NULL, NULL
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
#endif
};
#endif  // CONFIG_EXT_PARTITION

#if CONFIG_EXT_PARTITION_TYPES
uint8_t has_bl_vert_8x8[32] = {
  254, 255, 16, 17, 254, 255, 0, 1, 254, 255, 16, 17, 254, 255, 0, 0,
  254, 255, 16, 17, 254, 255, 0, 1, 254, 255, 16, 17, 254, 255, 0, 0,
};
uint8_t has_bl_vert_16x16[8] = {
  254, 16, 254, 0, 254, 16, 254, 0,
};
uint8_t has_bl_vert_32x32[2] = { 14, 14 };
uint8_t has_bl_vert_64x64[1] = { 2 };

// The _vert_* tables are like the ordinary 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
#if CONFIG_EXT_PARTITION
static const uint8_t *const has_bl_vert_tables[BLOCK_SIZES] = {
  // 4X4
  NULL,
  // 4X8,     8X4,         8X8
  has_bl_4x8, NULL, has_bl_vert_8x8,
  // 8X16,    16X8,        16X16
  has_bl_8x16, NULL, has_bl_vert_16x16,
  // 16X32,   32X16,       32X32
  has_bl_16x32, NULL, has_bl_vert_32x32,
  // 32X64,   64X32,       64X64
  has_bl_32x64, NULL, has_bl_vert_64x64,
  // 64x128,  128x64,      128x128
458
  has_bl_64x128, NULL, has_bl_128x128
459 460 461 462 463 464 465 466 467 468 469 470
};
#else
static const uint8_t *const has_bl_vert_tables[BLOCK_SIZES] = {
  // 4X4
  NULL,
  // 4X8,      8X4,         8X8
  has_bl_8x16, NULL, has_bl_vert_16x16,
  // 8X16,     16X8,        16X16
  has_bl_16x32, NULL, has_bl_vert_32x32,
  // 16X32,    32X16,       32X32
  has_bl_32x64, NULL, has_bl_vert_64x64,
  // 32X64,    64X32,       64X64
471
  has_bl_64x128, NULL, has_bl_128x128
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
};
#endif
#endif  // CONFIG_EXT_PARTITION_TYPES

static const uint8_t *get_has_bl_table(PARTITION_TYPE partition,
                                       BLOCK_SIZE bsize) {
  const uint8_t *ret = NULL;
#if CONFIG_EXT_PARTITION_TYPES
  // 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 = has_bl_vert_tables[bsize];
  } else {
    ret = has_bl_tables[bsize];
  }
#else
  (void)partition;
  ret = has_bl_tables[bsize];
#endif  // CONFIG_EXT_PARTITION_TYPES
  assert(ret);
  return ret;
}

495 496
static int has_bottom_left(const AV1_COMMON *cm, BLOCK_SIZE bsize, int mi_row,
                           int mi_col, int bottom_available, int left_available,
497
                           PARTITION_TYPE partition, TX_SIZE txsz, int row_off,
498
                           int col_off, int ss_x, int ss_y) {
499 500
  if (!bottom_available || !left_available) return 0;

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
#if CONFIG_EXT_PARTITION
  // Special case for 128x* blocks, when col_off is half the block width.
  // This is needed because 128x* superblocks are divided into 64x* blocks in
  // raster order
  if (block_size_wide[bsize] > block_size_wide[BLOCK_64X64] && col_off > 0) {
    const int plane_bw_unit_64 = mi_size_wide[BLOCK_64X64] >> ss_x;
    const int col_off_64 = col_off % plane_bw_unit_64;
    if (col_off_64 == 0) {
      // We are at the left edge of top-right or bottom-right 64x* block.
      const int plane_bh_unit_64 = mi_size_high[BLOCK_64X64] >> ss_y;
      const int row_off_64 = row_off % plane_bh_unit_64;
      const int plane_bh_unit =
          AOMMIN(mi_size_high[bsize] >> ss_y, plane_bh_unit_64);
      // Check if all bottom-left pixels are in the left 64x* block (which is
      // already coded).
      return row_off_64 + tx_size_high_unit[txsz] < plane_bh_unit;
    }
  }
#else
  (void)ss_x;
#endif  // CONFIG_EXT_PARTITION

523 524
  if (col_off > 0) {
    // Bottom-left pixels are in the bottom-left block, which is not available.
525 526
    return 0;
  } else {
527 528 529
    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];
530

531 532 533 534 535
    // 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];
536
    const int sb_mi_size = mi_size_high[cm->seq_params.sb_size];
537 538
    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;
539 540 541 542 543

    // 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) {
544 545 546 547
      const int blk_start_row_off = blk_row_in_sb
                                        << (bh_in_mi_log2 + MI_SIZE_LOG2 -
                                            tx_size_wide_log2[0]) >>
                                    ss_y;
548
      const int row_off_in_sb = blk_start_row_off + row_off;
549
      const int sb_height_unit =
550
          sb_mi_size << (MI_SIZE_LOG2 - tx_size_wide_log2[0]) >> ss_y;
551 552
      return row_off_in_sb + bottom_left_count_unit < sb_height_unit;
    }
553

554 555
    // Bottom row of superblock (and not the leftmost column): so bottom-left
    // pixels fall in the bottom superblock, which is not available yet.
556
    if (((blk_row_in_sb + 1) << bh_in_mi_log2) >= sb_mi_size) return 0;
557 558 559 560 561 562

    // General case (neither leftmost column nor bottom row): check if the
    // bottom-left block is coded before the current block.
    const int this_blk_index =
        ((blk_row_in_sb + 0) << (MAX_MIB_SIZE_LOG2 - bw_in_mi_log2)) +
        blk_col_in_sb + 0;
563 564 565 566
    const int idx1 = this_blk_index / 8;
    const int idx2 = this_blk_index % 8;
    const uint8_t *has_bl_table = get_has_bl_table(partition, bsize);
    return (has_bl_table[idx1] >> idx2) & 1;
567 568
  }
}
Jingning Han's avatar
Jingning Han committed
569 570 571 572

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

573 574
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
575 576 577 578

typedef void (*intra_high_pred_fn)(uint16_t *dst, ptrdiff_t stride,
                                   const uint16_t *above, const uint16_t *left,
                                   int bd);
579 580
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];
Jingning Han's avatar
Jingning Han committed
581

Yaowu Xu's avatar
Yaowu Xu committed
582
static void av1_init_intra_predictors_internal(void) {
583
  assert(NELEMENTS(mode_to_angle_map) == INTRA_MODES);
584

585 586 587 588 589 590 591 592
#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; \
593 594
  p[TX_64X32] = aom_##type##_predictor_64x32; \
  p[TX_4X16] = aom_##type##_predictor_4x16;   \
595 596
  p[TX_16X4] = aom_##type##_predictor_16x4;   \
  p[TX_8X32] = aom_##type##_predictor_8x32;   \
597 598 599
  p[TX_32X8] = aom_##type##_predictor_32x8;   \
  p[TX_16X64] = aom_##type##_predictor_16x64; \
  p[TX_64X16] = aom_##type##_predictor_64x16;
600

601 602 603 604
#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; \
605 606
  p[TX_64X64] = aom_##type##_predictor_64x64; \
  INIT_RECTANGULAR(p, type)
Jingning Han's avatar
Jingning Han committed
607

Jingning Han's avatar
Jingning Han committed
608 609 610
#define INIT_ALL_SIZES(p, type)           \
  p[TX_4X4] = aom_##type##_predictor_4x4; \
  INIT_NO_4X4(p, type)
611

Jingning Han's avatar
Jingning Han committed
612 613
  INIT_ALL_SIZES(pred[V_PRED], v);
  INIT_ALL_SIZES(pred[H_PRED], h);
Hui Su's avatar
Hui Su committed
614
  INIT_ALL_SIZES(pred[D203_PRED], d207e);
615
  INIT_ALL_SIZES(pred[D45_PRED], d45e);
Hui Su's avatar
Hui Su committed
616 617
  INIT_ALL_SIZES(pred[D67_PRED], d63e);
  INIT_ALL_SIZES(pred[D113_PRED], d117);
Jingning Han's avatar
Jingning Han committed
618
  INIT_ALL_SIZES(pred[D135_PRED], d135);
Hui Su's avatar
Hui Su committed
619
  INIT_ALL_SIZES(pred[D157_PRED], d153);
Urvang Joshi's avatar
Urvang Joshi committed
620

Urvang Joshi's avatar
Urvang Joshi committed
621
  INIT_ALL_SIZES(pred[PAETH_PRED], paeth);
622
  INIT_ALL_SIZES(pred[SMOOTH_PRED], smooth);
Urvang Joshi's avatar
Urvang Joshi committed
623 624
  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
625 626 627 628 629 630 631 632

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

  INIT_ALL_SIZES(pred_high[V_PRED], highbd_v);
  INIT_ALL_SIZES(pred_high[H_PRED], highbd_h);
Hui Su's avatar
Hui Su committed
633
  INIT_ALL_SIZES(pred_high[D203_PRED], highbd_d207e);
634
  INIT_ALL_SIZES(pred_high[D45_PRED], highbd_d45e);
Hui Su's avatar
Hui Su committed
635 636
  INIT_ALL_SIZES(pred_high[D67_PRED], highbd_d63e);
  INIT_ALL_SIZES(pred_high[D113_PRED], highbd_d117);
Jingning Han's avatar
Jingning Han committed
637
  INIT_ALL_SIZES(pred_high[D135_PRED], highbd_d135);
Hui Su's avatar
Hui Su committed
638
  INIT_ALL_SIZES(pred_high[D157_PRED], highbd_d153);
Urvang Joshi's avatar
Urvang Joshi committed
639

Urvang Joshi's avatar
Urvang Joshi committed
640
  INIT_ALL_SIZES(pred_high[PAETH_PRED], highbd_paeth);
641
  INIT_ALL_SIZES(pred_high[SMOOTH_PRED], highbd_smooth);
Urvang Joshi's avatar
Urvang Joshi committed
642 643
  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
644 645 646 647 648 649 650 651

  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);
#undef intra_pred_allsizes
}

hui su's avatar
hui su committed
652
// Directional prediction, zone 1: 0 < angle < 90
653
static void dr_prediction_z1(uint8_t *dst, ptrdiff_t stride, int bw, int bh,
hui su's avatar
hui su committed
654
                             const uint8_t *above, const uint8_t *left,
Yaowu Xu's avatar
Yaowu Xu committed
655
#if CONFIG_INTRA_EDGE
656
                             int upsample_above,
Yaowu Xu's avatar
Yaowu Xu committed
657
#endif  // CONFIG_INTRA_EDGE
hui su's avatar
hui su committed
658
                             int dx, int dy) {
hui su's avatar
hui su committed
659
  int r, c, x, base, shift, val;
hui su's avatar
hui su committed
660 661 662 663

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

Yaowu Xu's avatar
Yaowu Xu committed
666
#if !CONFIG_INTRA_EDGE
667
  const int upsample_above = 0;
Yaowu Xu's avatar
Yaowu Xu committed
668
#endif  // !CONFIG_INTRA_EDGE
669
  const int max_base_x = ((bw + bh) - 1) << upsample_above;
670 671 672
#if CONFIG_EXT_INTRA_MOD2
  const int frac_bits = 6 - upsample_above;
#else
673
  const int frac_bits = 8 - upsample_above;
674
#endif
675
  const int base_inc = 1 << upsample_above;
hui su's avatar
hui su committed
676
  x = dx;
677
  for (r = 0; r < bh; ++r, dst += stride, x += dx) {
678
    base = x >> frac_bits;
679 680 681
#if CONFIG_EXT_INTRA_MOD2
    shift = ((x << upsample_above) & 0x3F) >> 1;
#else
682
    shift = (x << upsample_above) & 0xFF;
683
#endif
hui su's avatar
hui su committed
684

685 686 687
    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
688 689 690 691 692
        dst += stride;
      }
      return;
    }

693 694
    for (c = 0; c < bw; ++c, base += base_inc) {
      if (base < max_base_x) {
695 696 697 698
#if CONFIG_EXT_INTRA_MOD2
        val = above[base] * (32 - shift) + above[base + 1] * shift;
        val = ROUND_POWER_OF_TWO(val, 5);
#else
hui su's avatar
hui su committed
699 700
        val = above[base] * (256 - shift) + above[base + 1] * shift;
        val = ROUND_POWER_OF_TWO(val, 8);
701
#endif
hui su's avatar
hui su committed
702 703
        dst[c] = clip_pixel(val);
      } else {
704
        dst[c] = above[max_base_x];
hui su's avatar
hui su committed
705 706 707 708 709 710
      }
    }
  }
}

// Directional prediction, zone 2: 90 < angle < 180
711
static void dr_prediction_z2(uint8_t *dst, ptrdiff_t stride, int bw, int bh,
hui su's avatar
hui su committed
712
                             const uint8_t *above, const uint8_t *left,
Yaowu Xu's avatar
Yaowu Xu committed
713
#if CONFIG_INTRA_EDGE
714
                             int upsample_above, int upsample_left,
Yaowu Xu's avatar
Yaowu Xu committed
715
#endif  // CONFIG_INTRA_EDGE
hui su's avatar
hui su committed
716
                             int dx, int dy) {
Debargha Mukherjee's avatar
Debargha Mukherjee committed
717
  int r, c, x, y, shift1, shift2, val, base1, base2;
hui su's avatar
hui su committed
718 719 720 721

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

Yaowu Xu's avatar
Yaowu Xu committed
722
#if !CONFIG_INTRA_EDGE
723 724
  const int upsample_above = 0;
  const int upsample_left = 0;
Yaowu Xu's avatar
Yaowu Xu committed
725
#endif  // !CONFIG_INTRA_EDGE
726
  const int min_base_x = -(1 << upsample_above);
727 728 729 730
#if CONFIG_EXT_INTRA_MOD2
  const int frac_bits_x = 6 - upsample_above;
  const int frac_bits_y = 6 - upsample_left;
#else
731 732
  const int frac_bits_x = 8 - upsample_above;
  const int frac_bits_y = 8 - upsample_left;
733
#endif
734
  const int base_inc_x = 1 << upsample_above;
hui su's avatar
hui su committed
735
  x = -dx;
736
  for (r = 0; r < bh; ++r, x -= dx, dst += stride) {
737
    base1 = x >> frac_bits_x;
738 739 740
#if CONFIG_EXT_INTRA_MOD2
    y = (r << 6) - dy;
#else
hui su's avatar
hui su committed
741
    y = (r << 8) - dy;
742
#endif
743 744
    for (c = 0; c < bw; ++c, base1 += base_inc_x, y -= dy) {
      if (base1 >= min_base_x) {
745 746 747 748 749
#if CONFIG_EXT_INTRA_MOD2
        shift1 = ((x * (1 << upsample_above)) & 0x3F) >> 1;
        val = above[base1] * (32 - shift1) + above[base1 + 1] * shift1;
        val = ROUND_POWER_OF_TWO(val, 5);
#else
Yaowu Xu's avatar
Yaowu Xu committed
750
        shift1 = (x * (1 << upsample_above)) & 0xFF;
hui su's avatar
hui su committed
751 752
        val = above[base1] * (256 - shift1) + above[base1 + 1] * shift1;
        val = ROUND_POWER_OF_TWO(val, 8);
753
#endif
hui su's avatar
hui su committed
754
      } else {
755 756
        base2 = y >> frac_bits_y;
        assert(base2 >= -(1 << upsample_left));
757 758 759 760 761
#if CONFIG_EXT_INTRA_MOD2
        shift2 = ((y * (1 << upsample_left)) & 0x3F) >> 1;
        val = left[base2] * (32 - shift2) + left[base2 + 1] * shift2;
        val = ROUND_POWER_OF_TWO(val, 5);
#else
Yaowu Xu's avatar
Yaowu Xu committed
762
        shift2 = (y * (1 << upsample_left)) & 0xFF;
763 764
        val = left[base2] * (256 - shift2) + left[base2 + 1] * shift2;
        val = ROUND_POWER_OF_TWO(val, 8);
765
#endif
hui su's avatar
hui su committed
766 767 768 769 770 771 772
      }
      dst[c] = clip_pixel(val);
    }
  }
}

// Directional prediction, zone 3: 180 < angle < 270
773
static void dr_prediction_z3(uint8_t *dst, ptrdiff_t stride, int bw, int bh,
hui su's avatar
hui su committed
774
                             const uint8_t *above, const uint8_t *left,
Yaowu Xu's avatar
Yaowu Xu committed
775
#if CONFIG_INTRA_EDGE
776
                             int upsample_left,
Yaowu Xu's avatar
Yaowu Xu committed
777
#endif  // CONFIG_INTRA_EDGE
hui su's avatar
hui su committed
778
                             int dx, int dy) {
hui su's avatar
hui su committed
779
  int r, c, y, base, shift, val;
hui su's avatar
hui su committed
780 781 782

  (void)above;
  (void)dx;
783

hui su's avatar
hui su committed
784
  assert(dx == 1);
hui su's avatar
hui su committed
785
  assert(dy > 0);
hui su's avatar
hui su committed
786

Yaowu Xu's avatar
Yaowu Xu committed
787
#if !CONFIG_INTRA_EDGE
788
  const int upsample_left = 0;
Yaowu Xu's avatar
Yaowu Xu committed
789
#endif  // !CONFIG_INTRA_EDGE
790
  const int max_base_y = (bw + bh - 1) << upsample_left;
791 792 793
#if CONFIG_EXT_INTRA_MOD2
  const int frac_bits = 6 - upsample_left;
#else
794
  const int frac_bits = 8 - upsample_left;
795
#endif
796
  const int base_inc = 1 << upsample_left;
hui su's avatar
hui su committed
797
  y = dy;
798
  for (c = 0; c < bw; ++c, y += dy) {
799
    base = y >> frac_bits;
800 801 802
#if CONFIG_EXT_INTRA_MOD2
    shift = ((y << upsample_left) & 0x3F) >> 1;
#else
803
    shift = (y << upsample_left) & 0xFF;
804
#endif
hui su's avatar
hui su committed
805

806 807
    for (r = 0; r < bh; ++r, base += base_inc) {
      if (base < max_base_y) {
808 809 810 811
#if CONFIG_EXT_INTRA_MOD2
        val = left[base] * (32 - shift) + left[base + 1] * shift;
        val = ROUND_POWER_OF_TWO(val, 5);
#else
hui su's avatar
hui su committed
812 813
        val = left[base] * (256 - shift) + left[base + 1] * shift;
        val = ROUND_POWER_OF_TWO(val, 8);
814
#endif
hui su's avatar
hui su committed
815
        dst[r * stride + c] = clip_pixel(val);
hui su's avatar
hui su committed
816
      } else {
817
        for (; r < bh; ++r) dst[r * stride + c] = left[max_base_y];
hui su's avatar
hui su committed
818
        break;
hui su's avatar
hui su committed
819 820 821 822 823
      }
    }
  }
}

824 825 826 827
// 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
828
static INLINE int get_dx(int angle) {
829
  if (angle > 0 && angle < 90) {
hui su's avatar
hui su committed
830
    return dr_intra_derivative[angle];
831 832 833 834 835 836 837 838 839 840 841 842
  } 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
843
static INLINE int get_dy(int angle) {
844
  if (angle > 90 && angle < 180) {
clang-format's avatar
clang-format committed
845 846
    return dr_intra_derivative[angle - 90];
  } else if (angle > 180 && angle < 270) {
hui su's avatar
hui su committed
847
    return dr_intra_derivative[270 - angle];
clang-format's avatar
clang-format committed
848
  } else {
849 850 851 852 853
    // 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
854
static void dr_predictor(uint8_t *dst, ptrdiff_t stride, TX_SIZE tx_size,
hui su's avatar
hui su committed
855
                         const uint8_t *above, const uint8_t *left,
Yaowu Xu's avatar
Yaowu Xu committed
856
#if CONFIG_INTRA_EDGE
857
                         int upsample_above, int upsample_left,
Yaowu Xu's avatar
Yaowu Xu committed
858
#endif  // CONFIG_INTRA_EDGE
hui su's avatar
hui su committed
859
                         int angle) {
860 861
  const int dx = get_dx(angle);
  const int dy = get_dy(angle);
862 863
  const int bw = tx_size_wide[tx_size];
  const int bh = tx_size_high[tx_size];
864 865
  assert(angle > 0 && angle < 270);

hui su's avatar
hui su committed
866
  if (angle > 0 && angle < 90) {
867
    dr_prediction_z1(dst, stride, bw, bh, above, left,
Yaowu Xu's avatar
Yaowu Xu committed
868
#if CONFIG_INTRA_EDGE
869
                     upsample_above,
Yaowu Xu's avatar
Yaowu Xu committed
870
#endif  // CONFIG_INTRA_EDGE
hui su's avatar
hui su committed
871
                     dx, dy);
hui su's avatar
hui su committed
872
  } else if (angle > 90 && angle < 180) {
873
    dr_prediction_z2(dst, stride, bw, bh, above, left,
Yaowu Xu's avatar
Yaowu Xu committed
874
#if CONFIG_INTRA_EDGE
875
                     upsample_above, upsample_left,
Yaowu Xu's avatar
Yaowu Xu committed
876
#endif  // CONFIG_INTRA_EDGE
hui su's avatar
hui su committed
877
                     dx, dy);
hui su's avatar
hui su committed
878
  } else if (angle > 180 && angle < 270) {
879
    dr_prediction_z3(dst, stride, bw, bh, above, left,
Yaowu Xu's avatar
Yaowu Xu committed
880
#if CONFIG_INTRA_EDGE
881
                     upsample_left,
Yaowu Xu's avatar
Yaowu Xu committed
882
#endif  // CONFIG_INTRA_EDGE
hui su's avatar
hui su committed
883
                     dx, dy);
hui su's avatar
hui su committed
884
  } else if (angle == 90) {
hui su's avatar
hui su committed
885
    pred[V_PRED][tx_size](dst, stride, above, left);
hui su's avatar
hui su committed
886
  } else if (angle == 180) {
hui su's avatar
hui su committed
887
    pred[H_PRED][tx_size](dst, stride, above, left);
hui su's avatar
hui su committed
888 889 890 891
  }
}

// Directional prediction, zone 1: 0 < angle < 90
892 893 894
static void highbd_dr_prediction_z1(uint16_t *dst, ptrdiff_t stride, int bw,
                                    int bh, const uint16_t *above,
                                    const uint16_t *left,
Yaowu Xu's avatar
Yaowu Xu committed
895
#if CONFIG_INTRA_EDGE
896
                                    int upsample_above,
Yaowu Xu's avatar
Yaowu Xu committed
897
#endif  // CONFIG_INTRA_EDGE
hui su's avatar
hui su committed
898
                                    int dx, int dy, int bd) {
899
  int r, c, x, base, shift, val;
hui su's avatar
hui su committed
900 901 902 903

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

Yaowu Xu's avatar
Yaowu Xu committed
906
#if !CONFIG_INTRA_EDGE
907
  const int upsample_above = 0;
Yaowu Xu's avatar
Yaowu Xu committed
908
#endif  // !CONFIG_INTRA_EDGE
909
  const int max_base_x = ((bw + bh) - 1) << upsample_above;
910 911 912
#if CONFIG_EXT_INTRA_MOD2
  const int frac_bits = 6 - upsample_above;
#else
913
  const int frac_bits = 8 - upsample_above;
914
#endif
915
  const int base_inc = 1 << upsample_above;
916
  x = dx;
917
  for (r = 0; r < bh; ++r, dst += stride, x += dx) {
918
    base = x >> frac_bits;
919 920 921
#if CONFIG_EXT_INTRA_MOD2
    shift = ((x << upsample_above) & 0x3F) >> 1;
#else
922
    shift = (x << upsample_above) & 0xFF;
923
#endif
924

925 926 927
    if (base >= max_base_x) {
      for (int i = r; i < bh; ++i) {
        aom_memset16(dst, above[max_base_x], bw);
928 929 930 931 932
        dst += stride;
      }
      return;
    }

933 934
    for (c = 0; c < bw; ++c, base += base_inc) {
      if (base < max_base_x) {
935 936 937 938
#if CONFIG_EXT_INTRA_MOD2
        val = above[base] * (32 - shift) + above[base + 1] * shift;
        val = ROUND_POWER_OF_TWO(val, 5);
#else
hui su's avatar
hui su committed
939 940
        val = above[base] * (256 - shift) + above[base + 1] * shift;
        val = ROUND_POWER_OF_TWO(val, 8);
941
#endif
hui su's avatar
hui su committed
942 943
        dst[c] = clip_pixel_highbd(val, bd);
      } else {
944
        dst[c] = above[max_base_x];
hui su's avatar
hui su committed
945 946 947 948 949 950
      }
    }
  }
}

// Directional prediction, zone 2: 90 < angle < 180