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

11
#include "./vpx_config.h"
12
#include "./vp9_rtcd.h"
13 14

#include "vpx_mem/vpx_mem.h"
15
#include "vpx_ports/mem.h"
16
#include "vpx_ports/vpx_once.h"
17

18
#include "vp9/common/vp9_reconintra.h"
19
#include "vp9/common/vp9_onyxc_int.h"
John Koleszar's avatar
John Koleszar committed
20

21
const TX_TYPE intra_mode_to_tx_type_lookup[INTRA_MODES] = {
22 23 24 25 26 27 28 29 30 31
  DCT_DCT,    // DC
  ADST_DCT,   // V
  DCT_ADST,   // H
  DCT_DCT,    // D45
  ADST_ADST,  // D135
  ADST_DCT,   // D117
  DCT_ADST,   // D153
  DCT_ADST,   // D207
  ADST_DCT,   // D63
  ADST_ADST,  // TM
32 33
};

34 35 36 37 38 39 40 41 42 43 44 45 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] = {
  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
};

53 54 55
// This serves as a wrapper function, so that all the prediction functions
// can be unified and accessed as a pointer array. Note that the boundary
// above and left are not necessarily used all the time.
56
#define intra_pred_sized(type, size) \
57 58 59 60 61 62 63
  void vp9_##type##_predictor_##size##x##size##_c(uint8_t *dst, \
                                                  ptrdiff_t stride, \
                                                  const uint8_t *above, \
                                                  const uint8_t *left) { \
    type##_predictor(dst, stride, size, above, left); \
  }

64
#if CONFIG_VP9_HIGHBITDEPTH
65 66
#define intra_pred_highbd_sized(type, size) \
  void vp9_highbd_##type##_predictor_##size##x##size##_c( \
67 68
      uint16_t *dst, ptrdiff_t stride, const uint16_t *above, \
      const uint16_t *left, int bd) { \
69
    highbd_##type##_predictor(dst, stride, size, above, left, bd); \
70 71 72 73 74 75 76
  }

#define intra_pred_allsizes(type) \
  intra_pred_sized(type, 4) \
  intra_pred_sized(type, 8) \
  intra_pred_sized(type, 16) \
  intra_pred_sized(type, 32) \
77 78 79 80
  intra_pred_highbd_sized(type, 4) \
  intra_pred_highbd_sized(type, 8) \
  intra_pred_highbd_sized(type, 16) \
  intra_pred_highbd_sized(type, 32)
81

82 83 84 85 86 87 88 89 90
#define intra_pred_no_4x4(type) \
  intra_pred_sized(type, 8) \
  intra_pred_sized(type, 16) \
  intra_pred_sized(type, 32) \
  intra_pred_highbd_sized(type, 4) \
  intra_pred_highbd_sized(type, 8) \
  intra_pred_highbd_sized(type, 16) \
  intra_pred_highbd_sized(type, 32)

91 92
#else

93 94 95 96 97
#define intra_pred_allsizes(type) \
  intra_pred_sized(type, 4) \
  intra_pred_sized(type, 8) \
  intra_pred_sized(type, 16) \
  intra_pred_sized(type, 32)
98 99 100 101 102

#define intra_pred_no_4x4(type) \
  intra_pred_sized(type, 8) \
  intra_pred_sized(type, 16) \
  intra_pred_sized(type, 32)
103 104
#endif  // CONFIG_VP9_HIGHBITDEPTH

105 106 107 108
#define DST(x, y) dst[(x) + (y) * stride]
#define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)
#define AVG2(a, b) (((a) + (b) + 1) >> 1)

109
#if CONFIG_VP9_HIGHBITDEPTH
110 111 112
static INLINE void highbd_d207_predictor(uint16_t *dst, ptrdiff_t stride,
                                         int bs, const uint16_t *above,
                                         const uint16_t *left, int bd) {
Deb Mukherjee's avatar
Deb Mukherjee committed
113
  int r, c;
114 115 116 117 118
  (void) above;
  (void) bd;

  // First column.
  for (r = 0; r < bs - 1; ++r) {
119
    dst[r * stride] = AVG2(left[r], left[r + 1]);
120 121 122 123 124 125
  }
  dst[(bs - 1) * stride] = left[bs - 1];
  dst++;

  // Second column.
  for (r = 0; r < bs - 2; ++r) {
126
    dst[r * stride] = AVG3(left[r], left[r + 1], left[r + 2]);
127
  }
128
  dst[(bs - 2) * stride] = AVG3(left[bs - 2], left[bs - 1], left[bs - 1]);
129 130 131 132 133 134 135 136 137 138 139 140 141
  dst[(bs - 1) * stride] = left[bs - 1];
  dst++;

  // Rest of last row.
  for (c = 0; c < bs - 2; ++c)
    dst[(bs - 1) * stride + c] = left[bs - 1];

  for (r = bs - 2; r >= 0; --r) {
    for (c = 0; c < bs - 2; ++c)
      dst[r * stride + c] = dst[(r + 1) * stride + c - 2];
  }
}

142 143 144
static INLINE void highbd_d63_predictor(uint16_t *dst, ptrdiff_t stride,
                                        int bs, const uint16_t *above,
                                        const uint16_t *left, int bd) {
Deb Mukherjee's avatar
Deb Mukherjee committed
145
  int r, c;
146 147 148 149
  (void) left;
  (void) bd;
  for (r = 0; r < bs; ++r) {
    for (c = 0; c < bs; ++c) {
150 151 152
      dst[c] = r & 1 ? AVG3(above[(r >> 1) + c], above[(r >> 1) + c + 1],
                            above[(r >> 1) + c + 2])
                     : AVG2(above[(r >> 1) + c], above[(r >> 1) + c + 1]);
153 154 155 156 157
    }
    dst += stride;
  }
}

158 159 160
static INLINE void highbd_d45_predictor(uint16_t *dst, ptrdiff_t stride, int bs,
                                        const uint16_t *above,
                                        const uint16_t *left, int bd) {
Deb Mukherjee's avatar
Deb Mukherjee committed
161
  int r, c;
162 163 164 165
  (void) left;
  (void) bd;
  for (r = 0; r < bs; ++r) {
    for (c = 0; c < bs; ++c) {
166 167
      dst[c] = r + c + 2 < bs * 2 ? AVG3(above[r + c], above[r + c + 1],
                                         above[r + c + 2])
168 169 170 171 172 173
                                  : above[bs * 2 - 1];
    }
    dst += stride;
  }
}

174 175 176
static INLINE void highbd_d117_predictor(uint16_t *dst, ptrdiff_t stride,
                                         int bs, const uint16_t *above,
                                         const uint16_t *left, int bd) {
177
  int r, c;
Deb Mukherjee's avatar
Deb Mukherjee committed
178
  (void) bd;
179 180 181

  // first row
  for (c = 0; c < bs; c++)
182
    dst[c] = AVG2(above[c - 1], above[c]);
183 184 185
  dst += stride;

  // second row
186
  dst[0] = AVG3(left[0], above[-1], above[0]);
187
  for (c = 1; c < bs; c++)
188
    dst[c] = AVG3(above[c - 2], above[c - 1], above[c]);
189 190 191
  dst += stride;

  // the rest of first col
192
  dst[0] = AVG3(above[-1], left[0], left[1]);
193
  for (r = 3; r < bs; ++r)
194
    dst[(r - 2) * stride] = AVG3(left[r - 3], left[r - 2], left[r - 1]);
195 196 197 198 199 200 201 202 203

  // the rest of the block
  for (r = 2; r < bs; ++r) {
    for (c = 1; c < bs; c++)
      dst[c] = dst[-2 * stride + c - 1];
    dst += stride;
  }
}

204 205 206
static INLINE void highbd_d135_predictor(uint16_t *dst, ptrdiff_t stride,
                                         int bs, const uint16_t *above,
                                         const uint16_t *left, int bd) {
207
  int r, c;
Deb Mukherjee's avatar
Deb Mukherjee committed
208
  (void) bd;
209
  dst[0] = AVG3(left[0], above[-1], above[0]);
210
  for (c = 1; c < bs; c++)
211
    dst[c] = AVG3(above[c - 2], above[c - 1], above[c]);
212

213
  dst[stride] = AVG3(above[-1], left[0], left[1]);
214
  for (r = 2; r < bs; ++r)
215
    dst[r * stride] = AVG3(left[r - 2], left[r - 1], left[r]);
216 217 218 219 220 221 222 223 224

  dst += stride;
  for (r = 1; r < bs; ++r) {
    for (c = 1; c < bs; c++)
      dst[c] = dst[-stride + c - 1];
    dst += stride;
  }
}

225 226 227
static INLINE void highbd_d153_predictor(uint16_t *dst, ptrdiff_t stride,
                                         int bs, const uint16_t *above,
                                         const uint16_t *left, int bd) {
228
  int r, c;
Deb Mukherjee's avatar
Deb Mukherjee committed
229
  (void) bd;
230
  dst[0] = AVG2(above[-1], left[0]);
231
  for (r = 1; r < bs; r++)
232
    dst[r * stride] = AVG2(left[r - 1], left[r]);
233 234
  dst++;

235 236
  dst[0] = AVG3(left[0], above[-1], above[0]);
  dst[stride] = AVG3(above[-1], left[0], left[1]);
237
  for (r = 2; r < bs; r++)
238
    dst[r * stride] = AVG3(left[r - 2], left[r - 1], left[r]);
239 240 241
  dst++;

  for (c = 0; c < bs - 2; c++)
242
    dst[c] = AVG3(above[c - 1], above[c], above[c + 1]);
243 244 245 246 247 248 249 250 251
  dst += stride;

  for (r = 1; r < bs; ++r) {
    for (c = 0; c < bs - 2; c++)
      dst[c] = dst[-stride + c - 2];
    dst += stride;
  }
}

252 253 254
static INLINE void highbd_v_predictor(uint16_t *dst, ptrdiff_t stride,
                                      int bs, const uint16_t *above,
                                      const uint16_t *left, int bd) {
Deb Mukherjee's avatar
Deb Mukherjee committed
255
  int r;
256 257 258
  (void) left;
  (void) bd;
  for (r = 0; r < bs; r++) {
James Zern's avatar
James Zern committed
259
    memcpy(dst, above, bs * sizeof(uint16_t));
260 261 262 263
    dst += stride;
  }
}

264 265 266
static INLINE void highbd_h_predictor(uint16_t *dst, ptrdiff_t stride,
                                      int bs, const uint16_t *above,
                                      const uint16_t *left, int bd) {
Deb Mukherjee's avatar
Deb Mukherjee committed
267
  int r;
268 269 270 271 272 273 274 275
  (void) above;
  (void) bd;
  for (r = 0; r < bs; r++) {
    vpx_memset16(dst, left[r], bs);
    dst += stride;
  }
}

276 277 278
static INLINE void highbd_tm_predictor(uint16_t *dst, ptrdiff_t stride,
                                       int bs, const uint16_t *above,
                                       const uint16_t *left, int bd) {
279 280
  int r, c;
  int ytop_left = above[-1];
Deb Mukherjee's avatar
Deb Mukherjee committed
281
  (void) bd;
282 283 284

  for (r = 0; r < bs; r++) {
    for (c = 0; c < bs; c++)
285
      dst[c] = clip_pixel_highbd(left[r] + above[c] - ytop_left, bd);
286 287 288 289
    dst += stride;
  }
}

290 291 292
static INLINE void highbd_dc_128_predictor(uint16_t *dst, ptrdiff_t stride,
                                           int bs, const uint16_t *above,
                                           const uint16_t *left, int bd) {
Deb Mukherjee's avatar
Deb Mukherjee committed
293
  int r;
294 295 296 297 298 299 300 301 302
  (void) above;
  (void) left;

  for (r = 0; r < bs; r++) {
    vpx_memset16(dst, 128 << (bd - 8), bs);
    dst += stride;
  }
}

303 304 305
static INLINE void highbd_dc_left_predictor(uint16_t *dst, ptrdiff_t stride,
                                            int bs, const uint16_t *above,
                                            const uint16_t *left, int bd) {
Deb Mukherjee's avatar
Deb Mukherjee committed
306
  int i, r, expected_dc, sum = 0;
307 308 309 310 311 312 313 314 315 316 317 318 319
  (void) above;
  (void) bd;

  for (i = 0; i < bs; i++)
    sum += left[i];
  expected_dc = (sum + (bs >> 1)) / bs;

  for (r = 0; r < bs; r++) {
    vpx_memset16(dst, expected_dc, bs);
    dst += stride;
  }
}

320 321 322
static INLINE void highbd_dc_top_predictor(uint16_t *dst, ptrdiff_t stride,
                                           int bs, const uint16_t *above,
                                           const uint16_t *left, int bd) {
Deb Mukherjee's avatar
Deb Mukherjee committed
323
  int i, r, expected_dc, sum = 0;
324 325 326 327 328 329 330 331 332 333 334 335 336
  (void) left;
  (void) bd;

  for (i = 0; i < bs; i++)
    sum += above[i];
  expected_dc = (sum + (bs >> 1)) / bs;

  for (r = 0; r < bs; r++) {
    vpx_memset16(dst, expected_dc, bs);
    dst += stride;
  }
}

337 338 339
static INLINE void highbd_dc_predictor(uint16_t *dst, ptrdiff_t stride,
                                       int bs, const uint16_t *above,
                                       const uint16_t *left, int bd) {
340 341
  int i, r, expected_dc, sum = 0;
  const int count = 2 * bs;
Deb Mukherjee's avatar
Deb Mukherjee committed
342
  (void) bd;
343 344 345 346 347 348 349 350 351 352 353 354 355 356

  for (i = 0; i < bs; i++) {
    sum += above[i];
    sum += left[i];
  }

  expected_dc = (sum + (count >> 1)) / count;

  for (r = 0; r < bs; r++) {
    vpx_memset16(dst, expected_dc, bs);
    dst += stride;
  }
}
#endif  // CONFIG_VP9_HIGHBITDEPTH
357

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
void vp9_d207_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
                              const uint8_t *above, const uint8_t *left) {
  const int I = left[0];
  const int J = left[1];
  const int K = left[2];
  const int L = left[3];
  (void)above;
  DST(0, 0) =             AVG2(I, J);
  DST(2, 0) = DST(0, 1) = AVG2(J, K);
  DST(2, 1) = DST(0, 2) = AVG2(K, L);
  DST(1, 0) =             AVG3(I, J, K);
  DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
  DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
  DST(3, 2) = DST(2, 2) =
      DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
}

375 376
static INLINE void d207_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                  const uint8_t *above, const uint8_t *left) {
377
  int r, c;
378
  (void) above;
379
  // first column
Dmitry Kovalev's avatar
Dmitry Kovalev committed
380
  for (r = 0; r < bs - 1; ++r)
381
    dst[r * stride] = AVG2(left[r], left[r + 1]);
382 383
  dst[(bs - 1) * stride] = left[bs - 1];
  dst++;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
384

385
  // second column
Dmitry Kovalev's avatar
Dmitry Kovalev committed
386
  for (r = 0; r < bs - 2; ++r)
387 388
    dst[r * stride] = AVG3(left[r], left[r + 1], left[r + 2]);
  dst[(bs - 2) * stride] = AVG3(left[bs - 2], left[bs - 1], left[bs - 1]);
389 390
  dst[(bs - 1) * stride] = left[bs - 1];
  dst++;
391

392
  // rest of last row
Dmitry Kovalev's avatar
Dmitry Kovalev committed
393
  for (c = 0; c < bs - 2; ++c)
394
    dst[(bs - 1) * stride + c] = left[bs - 1];
395

Dmitry Kovalev's avatar
Dmitry Kovalev committed
396 397
  for (r = bs - 2; r >= 0; --r)
    for (c = 0; c < bs - 2; ++c)
398
      dst[r * stride + c] = dst[(r + 1) * stride + c - 2];
399
}
400
intra_pred_no_4x4(d207)
401

402 403 404 405 406 407 408 409 410 411 412 413 414 415
void vp9_d63_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
                             const uint8_t *above, const uint8_t *left) {
  const int A = above[0];
  const int B = above[1];
  const int C = above[2];
  const int D = above[3];
  const int E = above[4];
  const int F = above[5];
  const int G = above[6];
  (void)left;
  DST(0, 0) =             AVG2(A, B);
  DST(1, 0) = DST(0, 2) = AVG2(B, C);
  DST(2, 0) = DST(1, 2) = AVG2(C, D);
  DST(3, 0) = DST(2, 2) = AVG2(D, E);
416
              DST(3, 2) = AVG2(E, F);  // differs from vp8
417 418 419 420 421

  DST(0, 1) =             AVG3(A, B, C);
  DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
  DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
  DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
422
              DST(3, 3) = AVG3(E, F, G);  // differs from vp8
423 424
}

425 426
static INLINE void d63_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                 const uint8_t *above, const uint8_t *left) {
427
  int r, c;
428
  (void) left;
429
  for (r = 0; r < bs; ++r) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
430
    for (c = 0; c < bs; ++c)
431 432 433
      dst[c] = r & 1 ? AVG3(above[(r >> 1) + c], above[(r >> 1) + c + 1],
                            above[(r >> 1) + c + 2])
                     : AVG2(above[(r >> 1) + c], above[(r >> 1) + c + 1]);
434
    dst += stride;
John Koleszar's avatar
John Koleszar committed
435
  }
436
}
437
intra_pred_no_4x4(d63)
438

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
void vp9_d45_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
                             const uint8_t *above, const uint8_t *left) {
  const int A = above[0];
  const int B = above[1];
  const int C = above[2];
  const int D = above[3];
  const int E = above[4];
  const int F = above[5];
  const int G = above[6];
  const int H = above[7];
  (void)stride;
  (void)left;
  DST(0, 0)                                     = AVG3(A, B, C);
  DST(1, 0) = DST(0, 1)                         = AVG3(B, C, D);
  DST(2, 0) = DST(1, 1) = DST(0, 2)             = AVG3(C, D, E);
  DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
              DST(3, 1) = DST(2, 2) = DST(1, 3) = AVG3(E, F, G);
                          DST(3, 2) = DST(2, 3) = AVG3(F, G, H);
457
                                      DST(3, 3) = H;  // differs from vp8
458 459
}

460 461
static INLINE void d45_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                 const uint8_t *above, const uint8_t *left) {
462 463 464 465 466 467 468 469 470 471 472
  const uint8_t above_right = above[bs - 1];
  int x, size;
  uint8_t avg[31];  // TODO(jzern): this could be block size specific
  (void)left;

  for (x = 0; x < bs - 1; ++x) {
    avg[x] = AVG3(above[x], above[x + 1], above[x + 2]);
  }
  for (x = 0, size = bs - 1; x < bs; ++x, --size) {
    memcpy(dst, avg + x, size);
    memset(dst + size, above_right, x + 1);
473
    dst += stride;
John Koleszar's avatar
John Koleszar committed
474
  }
475
}
476
intra_pred_no_4x4(d45)
477

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
void vp9_d117_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
                              const uint8_t *above, const uint8_t *left) {
  const int I = left[0];
  const int J = left[1];
  const int K = left[2];
  const int X = above[-1];
  const int A = above[0];
  const int B = above[1];
  const int C = above[2];
  const int D = above[3];
  DST(0, 0) = DST(1, 2) = AVG2(X, A);
  DST(1, 0) = DST(2, 2) = AVG2(A, B);
  DST(2, 0) = DST(3, 2) = AVG2(B, C);
  DST(3, 0)             = AVG2(C, D);

  DST(0, 3) =             AVG3(K, J, I);
  DST(0, 2) =             AVG3(J, I, X);
  DST(0, 1) = DST(1, 3) = AVG3(I, X, A);
  DST(1, 1) = DST(2, 3) = AVG3(X, A, B);
  DST(2, 1) = DST(3, 3) = AVG3(A, B, C);
  DST(3, 1) =             AVG3(B, C, D);
}

501 502
static INLINE void d117_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                  const uint8_t *above, const uint8_t *left) {
John Koleszar's avatar
John Koleszar committed
503
  int r, c;
504

505
  // first row
506
  for (c = 0; c < bs; c++)
507
    dst[c] = AVG2(above[c - 1], above[c]);
508
  dst += stride;
509 510

  // second row
511
  dst[0] = AVG3(left[0], above[-1], above[0]);
512
  for (c = 1; c < bs; c++)
513
    dst[c] = AVG3(above[c - 2], above[c - 1], above[c]);
514
  dst += stride;
515 516

  // the rest of first col
517
  dst[0] = AVG3(above[-1], left[0], left[1]);
518
  for (r = 3; r < bs; ++r)
519
    dst[(r - 2) * stride] = AVG3(left[r - 3], left[r - 2], left[r - 1]);
520

521
  // the rest of the block
522 523
  for (r = 2; r < bs; ++r) {
    for (c = 1; c < bs; c++)
524 525
      dst[c] = dst[-2 * stride + c - 1];
    dst += stride;
John Koleszar's avatar
John Koleszar committed
526
  }
527
}
528
intra_pred_no_4x4(d117)
529

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
void vp9_d135_predictor_4x4(uint8_t *dst, ptrdiff_t stride,
                            const uint8_t *above, const uint8_t *left) {
  const int I = left[0];
  const int J = left[1];
  const int K = left[2];
  const int L = left[3];
  const int X = above[-1];
  const int A = above[0];
  const int B = above[1];
  const int C = above[2];
  const int D = above[3];
  (void)stride;
  DST(0, 3)                                     = AVG3(J, K, L);
  DST(1, 3) = DST(0, 2)                         = AVG3(I, J, K);
  DST(2, 3) = DST(1, 2) = DST(0, 1)             = AVG3(X, I, J);
  DST(3, 3) = DST(2, 2) = DST(1, 1) = DST(0, 0) = AVG3(A, X, I);
              DST(3, 2) = DST(2, 1) = DST(1, 0) = AVG3(B, A, X);
                          DST(3, 1) = DST(2, 0) = AVG3(C, B, A);
                                      DST(3, 0) = AVG3(D, C, B);
}

551 552
static INLINE void d135_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                  const uint8_t *above, const uint8_t *left) {
John Koleszar's avatar
John Koleszar committed
553
  int r, c;
554
  dst[0] = AVG3(left[0], above[-1], above[0]);
555
  for (c = 1; c < bs; c++)
556
    dst[c] = AVG3(above[c - 2], above[c - 1], above[c]);
557

558
  dst[stride] = AVG3(above[-1], left[0], left[1]);
559
  for (r = 2; r < bs; ++r)
560
    dst[r * stride] = AVG3(left[r - 2], left[r - 1], left[r]);
John Koleszar's avatar
John Koleszar committed
561

562
  dst += stride;
563 564
  for (r = 1; r < bs; ++r) {
    for (c = 1; c < bs; c++)
565 566
      dst[c] = dst[-stride + c - 1];
    dst += stride;
John Koleszar's avatar
John Koleszar committed
567
  }
568
}
569
intra_pred_no_4x4(d135)
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
void vp9_d153_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
                              const uint8_t *above, const uint8_t *left) {
  const int I = left[0];
  const int J = left[1];
  const int K = left[2];
  const int L = left[3];
  const int X = above[-1];
  const int A = above[0];
  const int B = above[1];
  const int C = above[2];

  DST(0, 0) = DST(2, 1) = AVG2(I, X);
  DST(0, 1) = DST(2, 2) = AVG2(J, I);
  DST(0, 2) = DST(2, 3) = AVG2(K, J);
  DST(0, 3)             = AVG2(L, K);

  DST(3, 0)             = AVG3(A, B, C);
  DST(2, 0)             = AVG3(X, A, B);
  DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
  DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
  DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
  DST(1, 3)             = AVG3(L, K, J);
}

595 596
static INLINE void d153_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                  const uint8_t *above, const uint8_t *left) {
John Koleszar's avatar
John Koleszar committed
597
  int r, c;
598
  dst[0] = AVG2(above[-1], left[0]);
599
  for (r = 1; r < bs; r++)
600
    dst[r * stride] = AVG2(left[r - 1], left[r]);
601 602
  dst++;

603 604
  dst[0] = AVG3(left[0], above[-1], above[0]);
  dst[stride] = AVG3(above[-1], left[0], left[1]);
605
  for (r = 2; r < bs; r++)
606
    dst[r * stride] = AVG3(left[r - 2], left[r - 1], left[r]);
607
  dst++;
John Koleszar's avatar
John Koleszar committed
608

609
  for (c = 0; c < bs - 2; c++)
610
    dst[c] = AVG3(above[c - 1], above[c], above[c + 1]);
611 612
  dst += stride;

613 614
  for (r = 1; r < bs; ++r) {
    for (c = 0; c < bs - 2; c++)
615 616
      dst[c] = dst[-stride + c - 2];
    dst += stride;
John Koleszar's avatar
John Koleszar committed
617
  }
618
}
619
intra_pred_no_4x4(d153)
620

621 622
static INLINE void v_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                               const uint8_t *above, const uint8_t *left) {
623
  int r;
624
  (void) left;
625 626

  for (r = 0; r < bs; r++) {
James Zern's avatar
James Zern committed
627
    memcpy(dst, above, bs);
628
    dst += stride;
629 630
  }
}
631
intra_pred_allsizes(v)
632

633 634
static INLINE void h_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                               const uint8_t *above, const uint8_t *left) {
635
  int r;
636
  (void) above;
637 638

  for (r = 0; r < bs; r++) {
James Zern's avatar
James Zern committed
639
    memset(dst, left[r], bs);
640
    dst += stride;
641 642
  }
}
643
intra_pred_allsizes(h)
644

645 646
static INLINE void tm_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                const uint8_t *above, const uint8_t *left) {
647
  int r, c;
648
  int ytop_left = above[-1];
649 650 651

  for (r = 0; r < bs; r++) {
    for (c = 0; c < bs; c++)
652 653
      dst[c] = clip_pixel(left[r] + above[c] - ytop_left);
    dst += stride;
654 655
  }
}
656
intra_pred_allsizes(tm)
657

658 659
static INLINE void dc_128_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                    const uint8_t *above, const uint8_t *left) {
660
  int r;
661 662
  (void) above;
  (void) left;
663 664

  for (r = 0; r < bs; r++) {
James Zern's avatar
James Zern committed
665
    memset(dst, 128, bs);
666
    dst += stride;
667 668
  }
}
669
intra_pred_allsizes(dc_128)
670

671 672 673 674
static INLINE void dc_left_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                     const uint8_t *above,
                                     const uint8_t *left) {
  int i, r, expected_dc, sum = 0;
675
  (void) above;
676 677

  for (i = 0; i < bs; i++)
678 679
    sum += left[i];
  expected_dc = (sum + (bs >> 1)) / bs;
680 681

  for (r = 0; r < bs; r++) {
James Zern's avatar
James Zern committed
682
    memset(dst, expected_dc, bs);
683
    dst += stride;
684 685
  }
}
686
intra_pred_allsizes(dc_left)
687

688 689 690
static INLINE void dc_top_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                    const uint8_t *above, const uint8_t *left) {
  int i, r, expected_dc, sum = 0;
691
  (void) left;
692 693

  for (i = 0; i < bs; i++)
694 695
    sum += above[i];
  expected_dc = (sum + (bs >> 1)) / bs;
696 697

  for (r = 0; r < bs; r++) {
James Zern's avatar
James Zern committed
698
    memset(dst, expected_dc, bs);
699
    dst += stride;
700 701
  }
}
702
intra_pred_allsizes(dc_top)
703

704 705 706
static INLINE void dc_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                const uint8_t *above, const uint8_t *left) {
  int i, r, expected_dc, sum = 0;
707 708
  const int count = 2 * bs;

709 710 711 712 713 714
  for (i = 0; i < bs; i++) {
    sum += above[i];
    sum += left[i];
  }

  expected_dc = (sum + (count >> 1)) / count;
715 716

  for (r = 0; r < bs; r++) {
James Zern's avatar
James Zern committed
717
    memset(dst, expected_dc, bs);
718
    dst += stride;
719 720
  }
}
721 722 723
intra_pred_allsizes(dc)
#undef intra_pred_allsizes

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

727 728 729
static intra_pred_fn pred[INTRA_MODES][TX_SIZES];
static intra_pred_fn dc_pred[2][2][TX_SIZES];

730 731 732 733 734 735 736 737
#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

738
static void vp9_init_intra_predictors_internal(void) {
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
#define INIT_ALL_SIZES(p, type) \
  p[TX_4X4] = vp9_##type##_predictor_4x4; \
  p[TX_8X8] = vp9_##type##_predictor_8x8; \
  p[TX_16X16] = vp9_##type##_predictor_16x16; \
  p[TX_32X32] = vp9_##type##_predictor_32x32

  INIT_ALL_SIZES(pred[V_PRED], v);
  INIT_ALL_SIZES(pred[H_PRED], h);
  INIT_ALL_SIZES(pred[D207_PRED], d207);
  INIT_ALL_SIZES(pred[D45_PRED], d45);
  INIT_ALL_SIZES(pred[D63_PRED], d63);
  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);

760
#if CONFIG_VP9_HIGHBITDEPTH
761 762 763 764 765 766 767 768 769 770 771 772 773 774
  INIT_ALL_SIZES(pred_high[V_PRED], highbd_v);
  INIT_ALL_SIZES(pred_high[H_PRED], highbd_h);
  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);
  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);
775 776 777
#endif  // CONFIG_VP9_HIGHBITDEPTH

#undef intra_pred_allsizes
778
}
779

780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
#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,
                                        int up_available,
                                        int left_available,
                                        int right_available,
                                        int x, int y,
                                        int plane, int bd) {
  int i;
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
  uint16_t *ref = CONVERT_TO_SHORTPTR(ref8);
796
  DECLARE_ALIGNED(16, uint16_t, left_col[32]);
797
  DECLARE_ALIGNED(16, uint16_t, above_data[64 + 16]);
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
  uint16_t *above_row = above_data + 16;
  const uint16_t *const_above_row = above_row;
  const int bs = 4 << tx_size;
  int frame_width, frame_height;
  int x0, y0;
  const struct macroblockd_plane *const pd = &xd->plane[plane];
  //  int base=128;
  int base = 128 << (bd - 8);
  // 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

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

  // left
  if (left_available) {
    if (xd->mb_to_bottom_edge < 0) {
      /* slower path if the block needs border extension */
      if (y0 + bs <= frame_height) {
        for (i = 0; i < bs; ++i)
          left_col[i] = ref[i * ref_stride - 1];
      } 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 */
      for (i = 0; i < bs; ++i)
        left_col[i] = ref[i * ref_stride - 1];
    }
  } else {
    // TODO(Peter): this value should probably change for high bitdepth
    vpx_memset16(left_col, base + 1, bs);
  }

  // TODO(hkuang) do not extend 2*bs pixels for all modes.
  // 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 + 2 * bs <= frame_width) {
        if (right_available && bs == 4) {
James Zern's avatar
James Zern committed
857
          memcpy(above_row, above_ref, 2 * bs * sizeof(uint16_t));
858
        } else {
James Zern's avatar
James Zern committed
859
          memcpy(above_row, above_ref, bs * sizeof(uint16_t));
860 861 862 863 864
          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) {
James Zern's avatar
James Zern committed
865
          memcpy(above_row, above_ref, r * sizeof(uint16_t));
866 867 868
          vpx_memset16(above_row + r, above_row[r - 1],
                       x0 + 2 * bs - frame_width);
        } else {
James Zern's avatar
James Zern committed
869
          memcpy(above_row, above_ref, bs * sizeof(uint16_t));
870 871 872 873
          vpx_memset16(above_row + bs, above_row[bs - 1], bs);
        }
      } else if (x0 <= frame_width) {
        const int r = frame_width - x0;
James Zern's avatar
James Zern committed
874
        memcpy(above_row, above_ref, r * sizeof(uint16_t));
hkuang's avatar
hkuang committed
875
        vpx_memset16(above_row + r, above_row[r - 1],
876 877 878 879 880 881 882 883 884
                       x0 + 2 * bs - frame_width);
      }
      // 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;
      } else {
James Zern's avatar
James Zern committed
885
        memcpy(above_row, above_ref, bs * sizeof(uint16_t));
886
        if (bs == 4 && right_available)
James Zern's avatar
James Zern committed
887
          memcpy(above_row + bs, above_ref + bs, bs * sizeof(uint16_t));
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
        else
          vpx_memset16(above_row + bs, above_row[bs -