reconintra.c 40.2 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 15 16 17 18 19 20 21 22
#include "./vpx_config.h"
#include "./vpx_dsp_rtcd.h"

#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"

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

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
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
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
static const uint8_t orders_64x64[1] = { 0 };
static const uint8_t orders_64x32[2] = { 0, 1 };
static const uint8_t orders_32x64[2] = { 0, 1 };
static const uint8_t orders_32x32[4] = {
  0, 1,
  2, 3,
};
static const uint8_t orders_32x16[8] = {
  0, 2,
  1, 3,
  4, 6,
  5, 7,
};
static const uint8_t orders_16x32[8] = {
  0, 1, 2, 3,
  4, 5, 6, 7,
};
static const uint8_t orders_16x16[16] = {
  0,   1,  4,  5,
  2,   3,  6,  7,
  8,   9, 12, 13,
  10, 11, 14, 15,
};
static const uint8_t orders_16x8[32] = {
  0,   2,  8, 10,
  1,   3,  9, 11,
  4,   6, 12, 14,
  5,   7, 13, 15,
  16, 18, 24, 26,
  17, 19, 25, 27,
  20, 22, 28, 30,
  21, 23, 29, 31,
};
static const uint8_t orders_8x16[32] = {
  0,   1,  2,  3,  8,  9, 10, 11,
  4,   5,  6,  7, 12, 13, 14, 15,
  16, 17, 18, 19, 24, 25, 26, 27,
  20, 21, 22, 23, 28, 29, 30, 31,
};
static const uint8_t orders_8x8[64] = {
  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,
};
static const uint8_t *const orders[BLOCK_SIZES] = {
  orders_8x8, orders_8x8, orders_8x8, orders_8x8,
  orders_8x16, orders_16x8, orders_16x16,
  orders_16x32, orders_32x16, orders_32x32,
  orders_32x64, orders_64x32, orders_64x64,
};

static int vp10_has_right(BLOCK_SIZE bsize, int mi_row, int mi_col,
                          int right_available,
                          TX_SIZE txsz, int y, int x, int ss_x) {
hui su's avatar
hui su committed
106 107 108 109
  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;

110
  if (y == 0) {
hui su's avatar
hui su committed
111
    const int hl = mi_height_log2_lookup[bsize];
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
    const uint8_t *order = orders[bsize];
    int my_order, tr_order;

    if (x + step < w)
      return 1;

    mi_row = (mi_row & 7) >> hl;
    mi_col = (mi_col & 7) >> wl;

    if (mi_row == 0)
      return right_available;

    if (((mi_col + 1) << wl) >= 8)
      return 0;

    my_order = order[((mi_row + 0) << (3 - wl)) + mi_col + 0];
    tr_order = order[((mi_row - 1) << (3 - wl)) + mi_col + 1];

    return my_order > tr_order && right_available;
  } else {
    return x + step < w;
  }
}

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) {
  if (x == 0) {
hui su's avatar
hui su committed
140 141 142 143
    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;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    const uint8_t *order = orders[bsize];
    int my_order, bl_order;

    mi_row = (mi_row & 7) >> hl;
    mi_col = (mi_col & 7) >> wl;

    if (mi_col == 0)
      return bottom_available &&
             (mi_row << (hl + !ss_y)) + y + step < (8 << !ss_y);

    if (((mi_row + 1) << hl) >= 8)
      return 0;

    if (y + step < h)
      return 1;

    my_order = order[((mi_row + 0) << (3 - wl)) + mi_col + 0];
    bl_order = order[((mi_row + 1) << (3 - wl)) + mi_col - 1];

    return bl_order < my_order && bottom_available;
  } else {
    return 0;
  }
}
Jingning Han's avatar
Jingning Han committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

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) {
184
#define INIT_NO_4X4(p, type) \
Jingning Han's avatar
Jingning Han committed
185 186 187 188
  p[TX_8X8] = vpx_##type##_predictor_8x8; \
  p[TX_16X16] = vpx_##type##_predictor_16x16; \
  p[TX_32X32] = vpx_##type##_predictor_32x32

189 190 191 192
#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
193 194
  INIT_ALL_SIZES(pred[V_PRED], v);
  INIT_ALL_SIZES(pred[H_PRED], h);
195 196 197
  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
198 199 200 201 202 203 204 205 206 207 208 209 210
  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);
211 212 213
  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
214 215 216 217 218 219 220 221 222 223 224 225 226 227
  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
}

Yaowu Xu's avatar
Yaowu Xu committed
228
static INLINE void memset16(uint16_t *dst, int val, int n) {
229 230 231 232
  while (n--)
    *dst++ = val;
}

hui su's avatar
hui su committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
#if CONFIG_EXT_INTRA
#define PI 3.14159265
#define FILTER_INTRA_PREC_BITS 10
#define FILTER_INTRA_ROUND_VAL 511

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

// 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,
                             int dx, int dy) {
  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) {
      x = c * 256 - y * dx;
      base = x >> 8;
      shift = x - base * 256;
      if (base < 2 * bs - 1) {
        val =
            (above[base] * (256 - shift) + above[base + 1] * shift + 128) >> 8;
        dst[c] = clip_pixel(val);
      } else {
        dst[c] = above[2 * bs - 1];
      }
    }
    dst += stride;
  }
}

// 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,
                             int dx, int dy) {
  int r, c, x, y, val1, val2, shift, val, base;

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

  for (r = 0; r < bs; ++r) {
    for (c = 0; c < bs; ++c) {
      y = r + 1;
      x = c * 256 - y * dx;
      if (x >= -256) {
        if (x <= 0) {
          val1 = above[-1];
          val2 = above[0];
          shift = x + 256;
        } else {
          base = x >> 8;
          val1 = above[base];
          val2 = above[base + 1];
          shift = x - base * 256;
        }
      } else {
        x = c + 1;
        y = r * 256 - x * dy;
        base = y >> 8;
        if (base >= 0) {
          val1 = left[base];
          val2 = left[base + 1];
          shift = y - base * 256;
        } else {
          val1 = val2 = left[0];
          shift = 0;
        }
      }
      val = (val1 * (256 - shift) + val2 * shift + 128) >> 8;
      dst[c] = clip_pixel(val);
    }
    dst += stride;
  }
}

// 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,
                             int dx, int dy) {
  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;
      y = r * 256 - x * dy;
      base = y >> 8;
      shift = y - base * 256;
341
      if (base < 2 * bs - 1) {
hui su's avatar
hui su committed
342 343 344 345 346 347 348 349 350 351 352
        val =
            (left[base] * (256 - shift) + left[base + 1] * shift + 128) >> 8;
        dst[c] = clip_pixel(val);
      } else {
        dst[c] = left[bs - 1];
      }
    }
    dst += stride;
  }
}

hui su's avatar
hui su committed
353
static void dr_predictor(uint8_t *dst, ptrdiff_t stride, TX_SIZE tx_size,
hui su's avatar
hui su committed
354 355 356
                         const uint8_t *above, const uint8_t *left, int angle) {
  double t = 0;
  int dx, dy;
hui su's avatar
hui su committed
357
  int bs = 4 << tx_size;
hui su's avatar
hui su committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

  if (angle != 90 && angle != 180)
    t = tan(angle * PI / 180.0);
  if (angle > 0 && angle < 90) {
    dx = -((int)(256 / t));
    dy = 1;
    dr_prediction_z1(dst, stride, bs, above, left, dx, dy);
  } else if (angle > 90 && angle < 180) {
    t = -t;
    dx = (int)(256 / t);
    dy = (int)(256 * t);
    dr_prediction_z2(dst, stride, bs, above, left, dx, dy);
  } else if (angle > 180 && angle < 270) {
    dx = 1;
    dy = -((int)(256 * t));
    dr_prediction_z3(dst, stride, bs, above, left, dx, dy);
  } else if (angle == 90) {
hui su's avatar
hui su committed
375
    pred[V_PRED][tx_size](dst, stride, above, left);
hui su's avatar
hui su committed
376
  } else if (angle == 180) {
hui su's avatar
hui su committed
377
    pred[H_PRED][tx_size](dst, stride, above, left);
hui su's avatar
hui su committed
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 418 419 420 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 458 459 460 461 462 463 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 491 492 493 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 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
  }
}

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];
      pred[r][c] = ipred < 0 ?
          -((-ipred + FILTER_INTRA_ROUND_VAL) >> FILTER_INTRA_PREC_BITS) :
          ((ipred + FILTER_INTRA_ROUND_VAL) >> FILTER_INTRA_PREC_BITS);
    }

  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
// 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,
                                    int dx, int dy, int bd) {
  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) {
      x = c * 256 - y * dx;
      base = x >> 8;
      shift = x - base * 256;
      if (base < 2 * bs - 1) {
        val =
            (above[base] * (256 - shift) + above[base + 1] * shift + 128) >> 8;
        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,
                                    int dx, int dy, int bd) {
  int r, c, x, y, val1, val2, shift, val, base;

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

  for (r = 0; r < bs; ++r) {
    for (c = 0; c < bs; ++c) {
      y = r + 1;
      x = c * 256 - y * dx;
      if (x >= -256) {
        if (x <= 0) {
          val1 = above[-1];
          val2 = above[0];
          shift = x + 256;
        } else {
          base = x >> 8;
          val1 = above[base];
          val2 = above[base + 1];
          shift = x - base * 256;
        }
      } else {
        x = c + 1;
        y = r * 256 - x * dy;
        base = y >> 8;
        if (base >= 0) {
          val1 = left[base];
          val2 = left[base + 1];
          shift = y - base * 256;
        } else {
          val1 = val2 = left[0];
          shift = 0;
        }
      }
      val = (val1 * (256 - shift) + val2 * shift + 128) >> 8;
      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,
                                    int dx, int dy, int bd) {
  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;
      y = r * 256 - x * dy;
      base = y >> 8;
      shift = y - base * 256;
628
      if (base < 2 * bs - 1) {
hui su's avatar
hui su committed
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 658 659 660 661 662 663 664 665 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 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 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 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
        val =
            (left[base] * (256 - shift) + left[base + 1] * shift + 128) >> 8;
        dst[c] = clip_pixel_highbd(val, bd);
      } else {
        dst[c] = left[bs - 1];
      }
    }
    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,
                                int angle, int bd) {
  double t = 0;
  int dx, dy;

  if (angle != 90 && angle != 180)
    t = tan(angle * PI / 180.0);
  if (angle > 0 && angle < 90) {
    dx = -((int)(256 / t));
    dy = 1;
    highbd_dr_prediction_z1(dst, stride, bs, above, left, dx, dy, bd);
  } else if (angle > 90 && angle < 180) {
    t = -t;
    dx = (int)(256 / t);
    dy = (int)(256 * t);
    highbd_dr_prediction_z2(dst, stride, bs, above, left, dx, dy, bd);
  } else if (angle > 180 && angle < 270) {
    dx = 1;
    dy = -((int)(256 * t));
    highbd_dr_prediction_z3(dst, stride, bs, above, left, dx, dy, bd);
  } 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];
      pred[r][c] = ipred < 0 ?
          -((-ipred + FILTER_INTRA_ROUND_VAL) >> FILTER_INTRA_PREC_BITS) :
          ((ipred + FILTER_INTRA_ROUND_VAL) >> FILTER_INTRA_PREC_BITS);
    }

  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
821 822 823 824 825 826 827 828
#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,
829 830
                                        int n_top_px, int n_topright_px,
                                        int n_left_px, int n_bottomleft_px,
hui su's avatar
hui su committed
831
                                        int plane) {
Jingning Han's avatar
Jingning Han committed
832 833 834
  int i;
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
  uint16_t *ref = CONVERT_TO_SHORTPTR(ref8);
835
  DECLARE_ALIGNED(16, uint16_t, left_col[64]);
Jingning Han's avatar
Jingning Han committed
836 837 838 839
  DECLARE_ALIGNED(16, uint16_t, above_data[64 + 16]);
  uint16_t *above_row = above_data + 16;
  const uint16_t *const_above_row = above_row;
  const int bs = 4 << tx_size;
hui su's avatar
hui su committed
840 841
  int need_left = extend_modes[mode] & NEED_LEFT;
  int need_above = extend_modes[mode] & NEED_ABOVE;
842
  const uint16_t *above_ref = ref - ref_stride;
hui su's avatar
hui su committed
843
  int base = 128 << (xd->bd - 8);
Jingning Han's avatar
Jingning Han committed
844 845 846 847 848 849
  // 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
850 851 852 853 854
#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
855
  int p_angle = 0;
hui su's avatar
hui su committed
856

hui su's avatar
hui su committed
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
  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
874 875 876
  }
#endif  // CONFIG_EXT_INTRA

877 878 879
  (void) plane;

  // NEED_LEFT
hui su's avatar
hui su committed
880 881 882 883 884
  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
885 886 887
    } 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
888 889 890 891
    } else {
      need_bottom = !!(extend_modes[mode] & NEED_BOTTOMLEFT);
    }
#else
892
    const int need_bottom = !!(extend_modes[mode] & NEED_BOTTOMLEFT);
hui su's avatar
hui su committed
893
#endif  // CONFIG_EXT_INTRA
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
    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))
        memset16(&left_col[i], left_col[i - 1], (bs << need_bottom) - i);
    } else {
      memset16(left_col, base + 1, bs << need_bottom);
    }
  }

  // NEED_ABOVE
hui su's avatar
hui su committed
911 912 913 914
  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
915 916 917 918
      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
919 920 921 922
    } else {
      need_right = !!(extend_modes[mode] & NEED_ABOVERIGHT);
    }
#else
923
    const int need_right = !!(extend_modes[mode] & NEED_ABOVERIGHT);
hui su's avatar
hui su committed
924
#endif  // CONFIG_EXT_INTRA
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
    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))
        memset16(&above_row[i], above_row[i - 1], (bs << need_right) - i);
    } else {
      memset16(above_row, base - 1, bs << need_right);
    }
  }

hui su's avatar
hui su committed
940 941
#if CONFIG_EXT_INTRA
  if (ext_intra_mode_info->use_ext_intra_mode[plane != 0] ||
hui su's avatar
hui su committed
942 943 944
      (extend_modes[mode] & NEED_ABOVELEFT) ||
      (mode != DC_PRED && mode != TM_PRED &&
        xd->mi[0]->mbmi.sb_type >= BLOCK_8X8)) {
945 946 947
    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
948 949
#else
  if ((extend_modes[mode] & NEED_ABOVELEFT)) {
950 951 952
    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
953
#endif  // CONFIG_EXT_INTRA
Jingning Han's avatar
Jingning Han committed
954

hui su's avatar
hui su committed
955 956
#if CONFIG_EXT_INTRA
  if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
hui su's avatar
hui su committed
957
    highbd_filter_intra_predictors[ext_intra_mode](dst, dst_stride, bs,
hui su's avatar
hui su committed
958
        const_above_row, left_col, xd->bd);
hui su's avatar
hui su committed
959 960 961 962 963 964
    return;
  }

  if (mode != DC_PRED && mode != TM_PRED &&
      xd->mi[0]->mbmi.sb_type >= BLOCK_8X8) {
    highbd_dr_predictor(dst, dst_stride, bs, const_above_row, left_col,
hui su's avatar
hui su committed
965
                        p_angle, xd->bd);
hui su's avatar
hui su committed
966 967 968 969
    return;
  }
#endif  // CONFIG_EXT_INTRA

Jingning Han's avatar
Jingning Han committed
970 971
  // predict
  if (mode == DC_PRED) {
972 973 974
    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
975 976 977 978 979 980 981 982 983 984
  } 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,
985 986
                                   int n_top_px, int n_topright_px,
                                   int n_left_px, int n_bottomleft_px,
hui su's avatar
hui su committed
987
                                   int plane) {
Jingning Han's avatar
Jingning Han committed
988
  int i;
989 990
  DECLARE_ALIGNED(16, uint8_t, left_col[64]);
  const uint8_t *above_ref = ref - ref_stride;
Jingning Han's avatar
Jingning Han committed
991 992 993 994
  DECLARE_ALIGNED(16, uint8_t, above_data[64 + 16]);
  uint8_t *above_row = above_data + 16;
  const uint8_t *const_above_row = above_row;
  const int bs = 4 << tx_size;
hui su's avatar
hui su committed
995 996 997 998 999 1000 1001
  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
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
  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
1021 1022
  }
#endif  // CONFIG_EXT_INTRA
Jingning Han's avatar
Jingning Han committed
1023 1024 1025 1026 1027 1028 1029 1030

  // 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
  // ..

1031 1032 1033 1034 1035 1036
  (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
1037 1038

  // NEED_LEFT
hui su's avatar
hui su committed
1039 1040 1041 1042
  if (need_left) {
#if CONFIG_EXT_INTRA
    int need_bottom;
    if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
hui su's avatar
hui su committed
1043 1044 1045 1046
      need_bottom = 0;
    } 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
1047 1048 1049 1050
    } else {
      need_bottom = !!(extend_modes[mode] & NEED_BOTTOMLEFT);
    }
#else
1051
    const int need_bottom = !!(extend_modes[mode] & NEED_BOTTOMLEFT);
hui su's avatar
hui su committed
1052
#endif  // CONFIG_EXT_INTRA
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
    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))
        memset(&left_col[i], left_col[i - 1], (bs << need_bottom) - i);
    } else {
      memset(left_col, 129, bs << need_bottom);
    }
Jingning Han's avatar
Jingning Han committed
1067 1068 1069
  }

  // NEED_ABOVE
hui su's avatar
hui su committed
1070 1071 1072 1073
  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
1074 1075 1076 1077
      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
1078 1079 1080 1081
    } else {
      need_right = !!(extend_modes[mode] & NEED_ABOVERIGHT);
    }
#else
1082
    const int need_right = !!(extend_modes[mode] & NEED_ABOVERIGHT);
hui su's avatar
hui su committed
1083
#endif  // CONFIG_EXT_INTRA
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
    if (n_top_px > 0) {
      memcpy(above_row, above_ref, n_top_px);
      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);
        i += n_topright_px;
      }
      if (i < (bs << need_right))
        memset(&above_row[i], above_row[i - 1], (bs << need_right) - i);
    } else {
      memset(above_row, 127, bs << need_right);
    }
Jingning Han's avatar
Jingning Han committed
1097 1098
  }

hui su's avatar
hui su committed
1099 1100
#if CONFIG_EXT_INTRA
  if (ext_intra_mode_info->use_ext_intra_mode[plane != 0] ||
hui su's avatar
hui su committed
1101 1102 1103
      (extend_modes[mode] & NEED_ABOVELEFT) ||
      (mode != DC_PRED && mode != TM_PRED &&
          xd->mi[0]->mbmi.sb_type >= BLOCK_8X8)) {
1104 1105
    above_row[-1] = n_top_px > 0 ? (n_left_px > 0 ? above_ref[-1] : 129) : 127;
  }
hui su's avatar
hui su committed
1106 1107
#else
  if ((extend_modes[mode] & NEED_ABOVELEFT)) {
1108 1109
    above_row[-1] = n_top_px > 0 ? (n_left_px > 0 ? above_ref[-1] : 129) : 127;
  }
hui su's avatar
hui su committed
1110
#endif  // CONFIG_EXT_INTRA
Jingning Han's avatar
Jingning Han committed
1111

hui su's avatar
hui su committed
1112 1113
#if CONFIG_EXT_INTRA
  if (ext_intra_mode_info->use_ext_intra_mode[plane != 0]) {
hui su's avatar
hui su committed
1114 1115 1116 1117 1118 1119 1120 1121
    filter_intra_predictors[ext_intra_mode](dst, dst_stride, bs,
        const_above_row, left_col);
    return;
  }

  if (mode != DC_PRED && mode != TM_PRED &&
      xd->mi[0]->mbmi.sb_type >= BLOCK_8X8) {
    dr_predictor(dst, dst_stride, tx_size, const_above_row, left_col, p_angle);
hui su's avatar
hui su committed
1122 1123 1124 1125
    return;
  }
#endif  // CONFIG_EXT_INTRA

Jingning Han's avatar
Jingning Han committed
1126 1127
  // predict
  if (mode == DC_PRED) {
1128 1129
    dc_pred[n_left_px > 0][n_top_px > 0][tx_size](dst, dst_stride,
                                                  const_above_row, left_col);
Jingning Han's avatar
Jingning Han committed
1130 1131 1132 1133 1134
  } else {
    pred[mode][tx_size](dst, dst_stride, const_above_row, left_col);
  }
}

1135
void vp10_predict_intra_block(const MACROBLOCKD *xd, int bwl_in, int bhl_in,
hui su's avatar
hui su committed
1136 1137 1138 1139
                              TX_SIZE tx_size, PREDICTION_MODE mode,
                              const uint8_t *ref, int ref_stride,
                              uint8_t *dst, int dst_stride,
                              int aoff, int loff, int plane) {
Jingning Han's avatar
Jingning Han committed
1140 1141 1142 1143 1144
  const int txw = (1 << tx_size);
  const int have_top = loff || xd->up_available;
  const int have_left = aoff || xd->left_available;
  const int x = aoff * 4;
  const int y = loff * 4;
1145 1146
  const int bw = VPXMAX(2, 1 << bwl_in);
  const int bh = VPXMAX(2, 1 << bhl_in);
hui su's avatar
hui su committed
1147 1148
  const int mi_row = -xd->mb_to_top_edge >> (3 + MI_SIZE_LOG2);
  const int mi_col = -xd->mb_to_left_edge >> (3 + MI_SIZE_LOG2);
1149 1150
  const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
  const struct macroblockd_plane *const pd = &xd->plane[plane];
1151 1152
  const int right_available =
      mi_col + (bw >> !pd->subsampling_x) < xd->tile.mi_col_end;
1153
  const int have_right = vp10_has_right(bsize, mi_row, mi_col,
1154
                                        right_available,
1155 1156 1157 1158 1159 1160 1161 1162 1163
                                        tx_size, loff, aoff,
                                        pd->subsampling_x);
  const int have_bottom = vp10_has_bottom(bsize, mi_row, mi_col,
                                          xd->mb_to_bottom_edge > 0,
                                          tx_size, loff, aoff,
                                          pd->subsampling_y);
  const int wpx = 4 * bw;
  const int hpx = 4 * bh;
  const int txpx = 4 * txw;
hui su's avatar
hui su committed
1164 1165 1166 1167 1168 1169 1170 1171
  // Distance between the right edge of this prediction block to
  // the frame right edge
  const int xr = (xd->mb_to_right_edge >> (3 + pd->subsampling_x)) +
      (wpx - x - txpx);
  // Distance between the bottom edge of this prediction block to
  // the frame bottom edge
  const int yd = (xd->mb_to_bottom_edge >> (3 + pd->subsampling_y)) +
      (hpx - y - txpx);
Jingning Han's avatar
Jingning Han committed
1172

hui su's avatar
hui su committed
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
  if (xd->mi[0]->mbmi.palette_mode_info.palette_size[plane != 0] > 0) {
    const int bs = 4 * (1 << tx_size);
    const int stride = 4 * (1 << bwl_in);
    int r, c;
    uint8_t *map = NULL;
#if CONFIG_VP9_HIGHBITDEPTH
    uint16_t *palette = xd->mi[0]->mbmi.palette_mode_info.palette_colors +
        plane * PALETTE_MAX_SIZE;
#else
    uint8_t *palette = xd->mi[0]->mbmi.palette_mode_info.palette_colors +
        plane * PALETTE_MAX_SIZE;
#endif  // CONFIG_VP9_HIGHBITDEPTH

    map = xd->plane[plane != 0].color_index_map;

#if CONFIG_VP9_HIGHBITDEPTH
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
      uint16_t *dst16 = CONVERT_TO_SHORTPTR(dst);
      for (r = 0; r < bs; ++r)
        for (c = 0; c < bs; ++c)
          dst16[r * dst_stride + c] =
              palette[map[(r + y) * stride + c + x]];
    } else {
#endif  // CONFIG_VP9_HIGHBITDEPTH
      for (r = 0; r < bs; ++r)
        for (c = 0; c < bs; ++c)
          dst[r * dst_stride + c] = palette[map[(r + y) * stride + c + x]];
#if CONFIG_VP9_HIGHBITDEPTH
    }
#endif  // CONFIG_VP9_HIGHBITDEPTH

    return;
  }

1207 1208 1209 1210 1211 1212 1213 1214
#if CONFIG_VP9_HIGHBITDEPTH
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    build_intra_predictors_high(xd, ref, ref_stride, dst, dst_stride, mode,
                                tx_size,
                                have_top ? VPXMIN(txpx, xr + txpx) : 0,
                                have_top && have_right ? VPXMIN(txpx, xr) : 0,
                                have_left ? VPXMIN(txpx, yd + txpx) : 0,
                                have_bottom && have_left ? VPXMIN(txpx, yd) : 0,
hui su's avatar
hui su committed
1215
                                plane);
1216 1217 1218 1219 1220 1221 1222 1223 1224
    return;
  }
#endif
  build_intra_predictors(xd, ref, ref_stride, dst, dst_stride, mode,
                         tx_size,
                         have_top ? VPXMIN(txpx, xr + txpx) : 0,
                         have_top && have_right ? VPXMIN(txpx, xr) : 0,
                         have_left ? VPXMIN(txpx, yd + txpx) : 0,
                         have_bottom && have_left ? VPXMIN(txpx, yd) : 0,
hui su's avatar
hui su committed
1225
                         plane);
Jingning Han's avatar
Jingning Han committed
1226 1227 1228 1229 1230
}

void vp10_init_intra_predictors(void) {
  once(vp10_init_intra_predictors_internal);
}