reconintra.c 25.5 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *  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.
 */

#include "./vpx_config.h"
#include "./vpx_dsp_rtcd.h"

14
#if CONFIG_VPX_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
15
#include "aom_dsp/vpx_dsp_common.h"
16
#endif  // CONFIG_VPX_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
17 18 19
#include "aom_mem/vpx_mem.h"
#include "aom_ports/mem.h"
#include "aom_ports/vpx_once.h"
Jingning Han's avatar
Jingning Han committed
20

Yaowu Xu's avatar
Yaowu Xu committed
21 22
#include "av1/common/reconintra.h"
#include "av1/common/onyxc_int.h"
Jingning Han's avatar
Jingning Han committed
23

24
#if CONFIG_MISC_FIXES
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
};
#else
Jingning Han's avatar
Jingning Han committed
46 47 48 49 50 51 52
enum {
  NEED_LEFT = 1 << 1,
  NEED_ABOVE = 1 << 2,
  NEED_ABOVERIGHT = 1 << 3,
};

static const uint8_t extend_modes[INTRA_MODES] = {
clang-format's avatar
clang-format committed
53 54 55 56 57 58 59 60 61 62
  NEED_ABOVE | NEED_LEFT,  // DC
  NEED_ABOVE,              // V
  NEED_LEFT,               // H
  NEED_ABOVERIGHT,         // D45
  NEED_LEFT | NEED_ABOVE,  // D135
  NEED_LEFT | NEED_ABOVE,  // D117
  NEED_LEFT | NEED_ABOVE,  // D153
  NEED_LEFT,               // D207
  NEED_ABOVERIGHT,         // D63
  NEED_LEFT | NEED_ABOVE,  // TM
Jingning Han's avatar
Jingning Han committed
63
};
64 65
#endif

66
#if CONFIG_MISC_FIXES
67 68 69 70
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] = {
clang-format's avatar
clang-format committed
71
  0, 1, 2, 3,
72 73
};
static const uint8_t orders_32x16[8] = {
clang-format's avatar
clang-format committed
74
  0, 2, 1, 3, 4, 6, 5, 7,
75 76
};
static const uint8_t orders_16x32[8] = {
clang-format's avatar
clang-format committed
77
  0, 1, 2, 3, 4, 5, 6, 7,
78 79
};
static const uint8_t orders_16x16[16] = {
clang-format's avatar
clang-format committed
80
  0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15,
81 82
};
static const uint8_t orders_16x8[32] = {
clang-format's avatar
clang-format committed
83 84
  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,
85 86
};
static const uint8_t orders_8x16[32] = {
clang-format's avatar
clang-format committed
87 88
  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,
89 90
};
static const uint8_t orders_8x8[64] = {
clang-format's avatar
clang-format committed
91 92 93 94
  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,
95 96
};
static const uint8_t *const orders[BLOCK_SIZES] = {
clang-format's avatar
clang-format committed
97 98
  orders_8x8,   orders_8x8,   orders_8x8,   orders_8x8,   orders_8x16,
  orders_16x8,  orders_16x16, orders_16x32, orders_32x16, orders_32x32,
99 100 101 102
  orders_32x64, orders_64x32, orders_64x64,
};

static int vp10_has_right(BLOCK_SIZE bsize, int mi_row, int mi_col,
clang-format's avatar
clang-format committed
103 104
                          int right_available, TX_SIZE txsz, int y, int x,
                          int ss_x) {
105 106 107 108 109 110 111 112
  if (y == 0) {
    int wl = mi_width_log2_lookup[bsize];
    int hl = mi_height_log2_lookup[bsize];
    int w = 1 << (wl + 1 - ss_x);
    int step = 1 << txsz;
    const uint8_t *order = orders[bsize];
    int my_order, tr_order;

clang-format's avatar
clang-format committed
113
    if (x + step < w) return 1;
114 115 116 117

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

clang-format's avatar
clang-format committed
118
    if (mi_row == 0) return right_available;
119

clang-format's avatar
clang-format committed
120
    if (((mi_col + 1) << wl) >= 8) return 0;
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

    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 {
    int wl = mi_width_log2_lookup[bsize];
    int w = 1 << (wl + 1 - ss_x);
    int step = 1 << txsz;

    return x + step < w;
  }
}

static int vp10_has_bottom(BLOCK_SIZE bsize, int mi_row, int mi_col,
clang-format's avatar
clang-format committed
136 137
                           int bottom_available, TX_SIZE txsz, int y, int x,
                           int ss_y) {
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  if (x == 0) {
    int wl = mi_width_log2_lookup[bsize];
    int hl = mi_height_log2_lookup[bsize];
    int h = 1 << (hl + 1 - ss_y);
    int step = 1 << txsz;
    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);

clang-format's avatar
clang-format committed
153
    if (((mi_row + 1) << hl) >= 8) return 0;
154

clang-format's avatar
clang-format committed
155
    if (y + step < h) return 1;
156 157 158 159 160 161 162 163 164 165

    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;
  }
}
#endif
Jingning Han's avatar
Jingning Han committed
166 167 168 169 170 171 172

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

173
#if CONFIG_VPX_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
174 175 176 177 178
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];
179
#endif  // CONFIG_VPX_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
180 181

static void vp10_init_intra_predictors_internal(void) {
clang-format's avatar
clang-format committed
182 183
#define INIT_NO_4X4(p, type)                  \
  p[TX_8X8] = vpx_##type##_predictor_8x8;     \
Jingning Han's avatar
Jingning Han committed
184 185 186
  p[TX_16X16] = vpx_##type##_predictor_16x16; \
  p[TX_32X32] = vpx_##type##_predictor_32x32

clang-format's avatar
clang-format committed
187
#define INIT_ALL_SIZES(p, type)           \
188 189 190
  p[TX_4X4] = vpx_##type##_predictor_4x4; \
  INIT_NO_4X4(p, type)

Jingning Han's avatar
Jingning Han committed
191 192
  INIT_ALL_SIZES(pred[V_PRED], v);
  INIT_ALL_SIZES(pred[H_PRED], h);
193
#if CONFIG_MISC_FIXES
194 195 196 197
  INIT_ALL_SIZES(pred[D207_PRED], d207e);
  INIT_ALL_SIZES(pred[D45_PRED], d45e);
  INIT_ALL_SIZES(pred[D63_PRED], d63e);
#else
Jingning Han's avatar
Jingning Han committed
198 199 200
  INIT_ALL_SIZES(pred[D207_PRED], d207);
  INIT_ALL_SIZES(pred[D45_PRED], d45);
  INIT_ALL_SIZES(pred[D63_PRED], d63);
201
#endif
Jingning Han's avatar
Jingning Han committed
202 203 204 205 206 207 208 209 210 211
  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);

212
#if CONFIG_VPX_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
213 214
  INIT_ALL_SIZES(pred_high[V_PRED], highbd_v);
  INIT_ALL_SIZES(pred_high[H_PRED], highbd_h);
215
#if CONFIG_MISC_FIXES
216 217 218 219
  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_d63);
#else
Jingning Han's avatar
Jingning Han committed
220 221 222
  INIT_ALL_SIZES(pred_high[D207_PRED], highbd_d207);
  INIT_ALL_SIZES(pred_high[D45_PRED], highbd_d45);
  INIT_ALL_SIZES(pred_high[D63_PRED], highbd_d63);
223
#endif
Jingning Han's avatar
Jingning Han committed
224 225 226 227 228 229 230 231 232
  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);
233
#endif  // CONFIG_VPX_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
234 235 236 237

#undef intra_pred_allsizes
}

238
#if CONFIG_MISC_FIXES
Yaowu Xu's avatar
Yaowu Xu committed
239
static INLINE void memset16(uint16_t *dst, int val, int n) {
clang-format's avatar
clang-format committed
240
  while (n--) *dst++ = val;
241 242 243
}
#endif

244
#if CONFIG_VPX_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
245
static void build_intra_predictors_high(const MACROBLOCKD *xd,
clang-format's avatar
clang-format committed
246 247 248
                                        const uint8_t *ref8, int ref_stride,
                                        uint8_t *dst8, int dst_stride,
                                        PREDICTION_MODE mode, TX_SIZE tx_size,
249
#if CONFIG_MISC_FIXES
250 251 252
                                        int n_top_px, int n_topright_px,
                                        int n_left_px, int n_bottomleft_px,
#else
clang-format's avatar
clang-format committed
253
                                        int up_available, int left_available,
Jingning Han's avatar
Jingning Han committed
254
                                        int right_available,
255
#endif
clang-format's avatar
clang-format committed
256
                                        int x, int y, int plane, int bd) {
Jingning Han's avatar
Jingning Han committed
257 258 259
  int i;
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
  uint16_t *ref = CONVERT_TO_SHORTPTR(ref8);
260
#if CONFIG_MISC_FIXES
Jingning Han's avatar
Jingning Han committed
261
  DECLARE_ALIGNED(16, uint16_t, left_col[32]);
262 263 264
#else
  DECLARE_ALIGNED(16, uint16_t, left_col[64]);
#endif
Jingning Han's avatar
Jingning Han committed
265 266 267 268
  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;
269
#if CONFIG_MISC_FIXES
270 271
  const uint16_t *above_ref = ref - ref_stride;
#else
Jingning Han's avatar
Jingning Han committed
272 273 274
  int frame_width, frame_height;
  int x0, y0;
  const struct macroblockd_plane *const pd = &xd->plane[plane];
275
#endif
276 277 278
  const int need_left = extend_modes[mode] & NEED_LEFT;
  const int need_above = extend_modes[mode] & NEED_ABOVE;
  const int need_aboveright = extend_modes[mode] & NEED_ABOVERIGHT;
Jingning Han's avatar
Jingning Han committed
279
  int base = 128 << (bd - 8);
clang-format's avatar
clang-format committed
280 281 282 283 284
// 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
Jingning Han's avatar
Jingning Han committed
285

286
#if CONFIG_MISC_FIXES
clang-format's avatar
clang-format committed
287 288 289 290 291 292
  (void)x;
  (void)y;
  (void)plane;
  (void)need_left;
  (void)need_above;
  (void)need_aboveright;
293 294 295 296 297 298

  // NEED_LEFT
  if (extend_modes[mode] & NEED_LEFT) {
    const int need_bottom = !!(extend_modes[mode] & NEED_BOTTOMLEFT);
    i = 0;
    if (n_left_px > 0) {
clang-format's avatar
clang-format committed
299
      for (; i < n_left_px; i++) left_col[i] = ref[i * ref_stride - 1];
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
      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
  if (extend_modes[mode] & NEED_ABOVE) {
    const int need_right = !!(extend_modes[mode] & NEED_ABOVERIGHT);
    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);
    }
  }

  if (extend_modes[mode] & NEED_ABOVELEFT) {
clang-format's avatar
clang-format committed
331 332
    above_row[-1] =
        n_top_px > 0 ? (n_left_px > 0 ? above_ref[-1] : base + 1) : base - 1;
333 334
  }
#else
Jingning Han's avatar
Jingning Han committed
335 336 337 338 339 340 341 342 343 344 345 346 347
  // Get current frame pointer, width and height.
  if (plane == 0) {
    frame_width = xd->cur_buf->y_width;
    frame_height = xd->cur_buf->y_height;
  } else {
    frame_width = xd->cur_buf->uv_width;
    frame_height = xd->cur_buf->uv_height;
  }

  // Get block position in current frame.
  x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
  y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;

348 349 350 351 352 353
  // NEED_LEFT
  if (need_left) {
    if (left_available) {
      if (xd->mb_to_bottom_edge < 0) {
        /* slower path if the block needs border extension */
        if (y0 + bs <= frame_height) {
clang-format's avatar
clang-format committed
354
          for (i = 0; i < bs; ++i) left_col[i] = ref[i * ref_stride - 1];
355 356 357 358 359 360 361
        } else {
          const int extend_bottom = frame_height - y0;
          for (i = 0; i < extend_bottom; ++i)
            left_col[i] = ref[i * ref_stride - 1];
          for (; i < bs; ++i)
            left_col[i] = ref[(extend_bottom - 1) * ref_stride - 1];
        }
Jingning Han's avatar
Jingning Han committed
362
      } else {
363
        /* faster path if the block does not need extension */
clang-format's avatar
clang-format committed
364
        for (i = 0; i < bs; ++i) left_col[i] = ref[i * ref_stride - 1];
Jingning Han's avatar
Jingning Han committed
365 366
      }
    } else {
367 368
      // TODO(Peter): this value should probably change for high bitdepth
      vpx_memset16(left_col, base + 1, bs);
Jingning Han's avatar
Jingning Han committed
369 370 371
    }
  }

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
  // NEED_ABOVE
  if (need_above) {
    if (up_available) {
      const uint16_t *above_ref = ref - ref_stride;
      if (xd->mb_to_right_edge < 0) {
        /* slower path if the block needs border extension */
        if (x0 + bs <= frame_width) {
          memcpy(above_row, above_ref, bs * sizeof(above_row[0]));
        } else if (x0 <= frame_width) {
          const int r = frame_width - x0;
          memcpy(above_row, above_ref, r * sizeof(above_row[0]));
          vpx_memset16(above_row + r, above_row[r - 1], x0 + bs - frame_width);
        }
      } else {
        /* faster path if the block does not need extension */
        if (bs == 4 && right_available && left_available) {
          const_above_row = above_ref;
Jingning Han's avatar
Jingning Han committed
389
        } else {
390
          memcpy(above_row, above_ref, bs * sizeof(above_row[0]));
Jingning Han's avatar
Jingning Han committed
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
      }
      above_row[-1] = left_available ? above_ref[-1] : (base + 1);
    } else {
      vpx_memset16(above_row, base - 1, bs);
      above_row[-1] = base - 1;
    }
  }

  // NEED_ABOVERIGHT
  if (need_aboveright) {
    if (up_available) {
      const uint16_t *above_ref = ref - ref_stride;
      if (xd->mb_to_right_edge < 0) {
        /* slower path if the block needs border extension */
        if (x0 + 2 * bs <= frame_width) {
          if (right_available && bs == 4) {
            memcpy(above_row, above_ref, 2 * bs * sizeof(above_row[0]));
          } else {
            memcpy(above_row, above_ref, bs * sizeof(above_row[0]));
            vpx_memset16(above_row + bs, above_row[bs - 1], bs);
          }
        } else if (x0 + bs <= frame_width) {
          const int r = frame_width - x0;
          if (right_available && bs == 4) {
            memcpy(above_row, above_ref, r * sizeof(above_row[0]));
            vpx_memset16(above_row + r, above_row[r - 1],
                         x0 + 2 * bs - frame_width);
          } else {
            memcpy(above_row, above_ref, bs * sizeof(above_row[0]));
            vpx_memset16(above_row + bs, above_row[bs - 1], bs);
          }
        } else if (x0 <= frame_width) {
          const int r = frame_width - x0;
425
          memcpy(above_row, above_ref, r * sizeof(above_row[0]));
Jingning Han's avatar
Jingning Han committed
426 427
          vpx_memset16(above_row + r, above_row[r - 1],
                       x0 + 2 * bs - frame_width);
428 429 430 431 432 433 434
        }
        // TODO(Peter) this value should probably change for high bitdepth
        above_row[-1] = left_available ? above_ref[-1] : (base + 1);
      } else {
        /* faster path if the block does not need extension */
        if (bs == 4 && right_available && left_available) {
          const_above_row = above_ref;
Jingning Han's avatar
Jingning Han committed
435
        } else {
436
          memcpy(above_row, above_ref, bs * sizeof(above_row[0]));
437 438 439 440 441 442
          if (bs == 4 && right_available)
            memcpy(above_row + bs, above_ref + bs, bs * sizeof(above_row[0]));
          else
            vpx_memset16(above_row + bs, above_row[bs - 1], bs);
          // TODO(Peter): this value should probably change for high bitdepth
          above_row[-1] = left_available ? above_ref[-1] : (base + 1);
Jingning Han's avatar
Jingning Han committed
443 444 445
        }
      }
    } else {
446 447 448
      vpx_memset16(above_row, base - 1, bs * 2);
      // TODO(Peter): this value should probably change for high bitdepth
      above_row[-1] = base - 1;
Jingning Han's avatar
Jingning Han committed
449 450
    }
  }
451
#endif
Jingning Han's avatar
Jingning Han committed
452 453 454

  // predict
  if (mode == DC_PRED) {
455
#if CONFIG_MISC_FIXES
clang-format's avatar
clang-format committed
456 457
    dc_pred_high[n_left_px > 0][n_top_px > 0][tx_size](
        dst, dst_stride, const_above_row, left_col, xd->bd);
458
#else
clang-format's avatar
clang-format committed
459 460
    dc_pred_high[left_available][up_available][tx_size](
        dst, dst_stride, const_above_row, left_col, xd->bd);
461
#endif
Jingning Han's avatar
Jingning Han committed
462 463 464 465 466
  } else {
    pred_high[mode][tx_size](dst, dst_stride, const_above_row, left_col,
                             xd->bd);
  }
}
467
#endif  // CONFIG_VPX_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
468 469 470 471

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,
472
#if CONFIG_MISC_FIXES
473 474 475
                                   int n_top_px, int n_topright_px,
                                   int n_left_px, int n_bottomleft_px,
#else
Jingning Han's avatar
Jingning Han committed
476
                                   int up_available, int left_available,
477 478 479
                                   int right_available,
#endif
                                   int x, int y, int plane) {
Jingning Han's avatar
Jingning Han committed
480
  int i;
481
#if CONFIG_MISC_FIXES
482 483 484
  DECLARE_ALIGNED(16, uint8_t, left_col[64]);
  const uint8_t *above_ref = ref - ref_stride;
#else
Jingning Han's avatar
Jingning Han committed
485
  DECLARE_ALIGNED(16, uint8_t, left_col[32]);
486 487 488 489
  int frame_width, frame_height;
  int x0, y0;
  const struct macroblockd_plane *const pd = &xd->plane[plane];
#endif
Jingning Han's avatar
Jingning Han committed
490 491 492 493 494
  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;

clang-format's avatar
clang-format committed
495 496 497 498 499 500
// 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
// ..
Jingning Han's avatar
Jingning Han committed
501

502
#if CONFIG_MISC_FIXES
clang-format's avatar
clang-format committed
503 504 505 506
  (void)xd;
  (void)x;
  (void)y;
  (void)plane;
507 508 509 510 511
  assert(n_top_px >= 0);
  assert(n_topright_px >= 0);
  assert(n_left_px >= 0);
  assert(n_bottomleft_px >= 0);
#else
Jingning Han's avatar
Jingning Han committed
512 513 514 515 516 517 518 519 520 521 522 523
  // Get current frame pointer, width and height.
  if (plane == 0) {
    frame_width = xd->cur_buf->y_width;
    frame_height = xd->cur_buf->y_height;
  } else {
    frame_width = xd->cur_buf->uv_width;
    frame_height = xd->cur_buf->uv_height;
  }

  // Get block position in current frame.
  x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
  y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
524
#endif
Jingning Han's avatar
Jingning Han committed
525 526 527

  // NEED_LEFT
  if (extend_modes[mode] & NEED_LEFT) {
528
#if CONFIG_MISC_FIXES
529 530 531
    const int need_bottom = !!(extend_modes[mode] & NEED_BOTTOMLEFT);
    i = 0;
    if (n_left_px > 0) {
clang-format's avatar
clang-format committed
532
      for (; i < n_left_px; i++) left_col[i] = ref[i * ref_stride - 1];
533 534 535 536 537 538 539 540 541 542 543
      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);
    }
#else
Jingning Han's avatar
Jingning Han committed
544 545 546 547
    if (left_available) {
      if (xd->mb_to_bottom_edge < 0) {
        /* slower path if the block needs border extension */
        if (y0 + bs <= frame_height) {
clang-format's avatar
clang-format committed
548
          for (i = 0; i < bs; ++i) left_col[i] = ref[i * ref_stride - 1];
Jingning Han's avatar
Jingning Han committed
549 550 551 552 553 554 555 556 557
        } else {
          const int extend_bottom = frame_height - y0;
          for (i = 0; i < extend_bottom; ++i)
            left_col[i] = ref[i * ref_stride - 1];
          for (; i < bs; ++i)
            left_col[i] = ref[(extend_bottom - 1) * ref_stride - 1];
        }
      } else {
        /* faster path if the block does not need extension */
clang-format's avatar
clang-format committed
558
        for (i = 0; i < bs; ++i) left_col[i] = ref[i * ref_stride - 1];
Jingning Han's avatar
Jingning Han committed
559 560 561 562
      }
    } else {
      memset(left_col, 129, bs);
    }
563
#endif
Jingning Han's avatar
Jingning Han committed
564 565 566 567
  }

  // NEED_ABOVE
  if (extend_modes[mode] & NEED_ABOVE) {
568
#if CONFIG_MISC_FIXES
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    const int need_right = !!(extend_modes[mode] & NEED_ABOVERIGHT);
    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);
    }
#else
Jingning Han's avatar
Jingning Han committed
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
    if (up_available) {
      const uint8_t *above_ref = ref - ref_stride;
      if (xd->mb_to_right_edge < 0) {
        /* slower path if the block needs border extension */
        if (x0 + bs <= frame_width) {
          memcpy(above_row, above_ref, bs);
        } else if (x0 <= frame_width) {
          const int r = frame_width - x0;
          memcpy(above_row, above_ref, r);
          memset(above_row + r, above_row[r - 1], x0 + bs - frame_width);
        }
      } else {
        /* faster path if the block does not need extension */
        if (bs == 4 && right_available && left_available) {
          const_above_row = above_ref;
        } else {
          memcpy(above_row, above_ref, bs);
        }
      }
      above_row[-1] = left_available ? above_ref[-1] : 129;
    } else {
      memset(above_row, 127, bs);
      above_row[-1] = 127;
    }
608
#endif
Jingning Han's avatar
Jingning Han committed
609 610
  }

611
#if CONFIG_MISC_FIXES
612 613 614 615
  if (extend_modes[mode] & NEED_ABOVELEFT) {
    above_row[-1] = n_top_px > 0 ? (n_left_px > 0 ? above_ref[-1] : 129) : 127;
  }
#else
Jingning Han's avatar
Jingning Han committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
  // NEED_ABOVERIGHT
  if (extend_modes[mode] & NEED_ABOVERIGHT) {
    if (up_available) {
      const uint8_t *above_ref = ref - ref_stride;
      if (xd->mb_to_right_edge < 0) {
        /* slower path if the block needs border extension */
        if (x0 + 2 * bs <= frame_width) {
          if (right_available && bs == 4) {
            memcpy(above_row, above_ref, 2 * bs);
          } else {
            memcpy(above_row, above_ref, bs);
            memset(above_row + bs, above_row[bs - 1], bs);
          }
        } else if (x0 + bs <= frame_width) {
          const int r = frame_width - x0;
          if (right_available && bs == 4) {
            memcpy(above_row, above_ref, r);
            memset(above_row + r, above_row[r - 1], x0 + 2 * bs - frame_width);
          } else {
            memcpy(above_row, above_ref, bs);
            memset(above_row + bs, above_row[bs - 1], bs);
          }
        } else if (x0 <= frame_width) {
          const int r = frame_width - x0;
          memcpy(above_row, above_ref, r);
          memset(above_row + r, above_row[r - 1], x0 + 2 * bs - frame_width);
        }
      } else {
        /* faster path if the block does not need extension */
        if (bs == 4 && right_available && left_available) {
          const_above_row = above_ref;
        } else {
          memcpy(above_row, above_ref, bs);
          if (bs == 4 && right_available)
            memcpy(above_row + bs, above_ref + bs, bs);
          else
            memset(above_row + bs, above_row[bs - 1], bs);
        }
      }
      above_row[-1] = left_available ? above_ref[-1] : 129;
    } else {
      memset(above_row, 127, bs * 2);
      above_row[-1] = 127;
    }
  }
661
#endif
Jingning Han's avatar
Jingning Han committed
662 663 664

  // predict
  if (mode == DC_PRED) {
665
#if CONFIG_MISC_FIXES
666 667 668
    dc_pred[n_left_px > 0][n_top_px > 0][tx_size](dst, dst_stride,
                                                  const_above_row, left_col);
#else
Jingning Han's avatar
Jingning Han committed
669 670
    dc_pred[left_available][up_available][tx_size](dst, dst_stride,
                                                   const_above_row, left_col);
671
#endif
Jingning Han's avatar
Jingning Han committed
672 673 674 675 676
  } else {
    pred[mode][tx_size](dst, dst_stride, const_above_row, left_col);
  }
}

677
void vp10_predict_intra_block(const MACROBLOCKD *xd, int bwl_in, int bhl_in,
clang-format's avatar
clang-format committed
678 679 680
                              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
681 682 683 684 685
  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;
686
#if CONFIG_MISC_FIXES
687 688 689 690 691 692
  const int bw = VPXMAX(2, 1 << bwl_in);
  const int bh = VPXMAX(2, 1 << bhl_in);
  const int mi_row = -xd->mb_to_top_edge >> 6;
  const int mi_col = -xd->mb_to_left_edge >> 6;
  const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
  const struct macroblockd_plane *const pd = &xd->plane[plane];
693 694
  const int right_available =
      mi_col + (bw >> !pd->subsampling_x) < xd->tile.mi_col_end;
clang-format's avatar
clang-format committed
695 696 697 698 699
  const int have_right = vp10_has_right(bsize, mi_row, mi_col, right_available,
                                        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);
700 701 702 703 704 705 706
  const int wpx = 4 * bw;
  const int hpx = 4 * bh;
  const int txpx = 4 * txw;

  int xr = (xd->mb_to_right_edge >> (3 + pd->subsampling_x)) + (wpx - x - txpx);
  int yd =
      (xd->mb_to_bottom_edge >> (3 + pd->subsampling_y)) + (hpx - y - txpx);
707 708 709 710
#else
  const int bw = (1 << bwl_in);
  const int have_right = (aoff + txw) < bw;
#endif  // CONFIG_MISC_FIXES
Jingning Han's avatar
Jingning Han committed
711

712
#if CONFIG_MISC_FIXES
713
#if CONFIG_VPX_HIGHBITDEPTH
714 715
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    build_intra_predictors_high(xd, ref, ref_stride, dst, dst_stride, mode,
clang-format's avatar
clang-format committed
716
                                tx_size, have_top ? VPXMIN(txpx, xr + txpx) : 0,
717 718 719 720 721 722 723
                                have_top && have_right ? VPXMIN(txpx, xr) : 0,
                                have_left ? VPXMIN(txpx, yd + txpx) : 0,
                                have_bottom && have_left ? VPXMIN(txpx, yd) : 0,
                                x, y, plane, xd->bd);
    return;
  }
#endif
clang-format's avatar
clang-format committed
724
  build_intra_predictors(xd, ref, ref_stride, dst, dst_stride, mode, tx_size,
725 726 727
                         have_top ? VPXMIN(txpx, xr + txpx) : 0,
                         have_top && have_right ? VPXMIN(txpx, xr) : 0,
                         have_left ? VPXMIN(txpx, yd + txpx) : 0,
clang-format's avatar
clang-format committed
728 729
                         have_bottom && have_left ? VPXMIN(txpx, yd) : 0, x, y,
                         plane);
730
#else  // CONFIG_MISC_FIXES
clang-format's avatar
clang-format committed
731
  (void)bhl_in;
732
#if CONFIG_VPX_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
733 734
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    build_intra_predictors_high(xd, ref, ref_stride, dst, dst_stride, mode,
clang-format's avatar
clang-format committed
735 736
                                tx_size, have_top, have_left, have_right, x, y,
                                plane, xd->bd);
Jingning Han's avatar
Jingning Han committed
737 738 739 740 741
    return;
  }
#endif
  build_intra_predictors(xd, ref, ref_stride, dst, dst_stride, mode, tx_size,
                         have_top, have_left, have_right, x, y, plane);
742
#endif  // CONFIG_MISC_FIXES
Jingning Han's avatar
Jingning Han committed
743 744 745 746 747
}

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