vp9_reconintra.c 28 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 <stdio.h>
John Koleszar's avatar
John Koleszar committed
12
#include "vpx_ports/config.h"
13
#include "vp9_rtcd.h"
14
#include "vp9/common/vp9_reconintra.h"
John Koleszar's avatar
John Koleszar committed
15 16
#include "vpx_mem/vpx_mem.h"

17 18
/* For skip_recon_mb(), add vp9_build_intra_predictors_mby_s(MACROBLOCKD *xd)
 * and vp9_build_intra_predictors_mbuv_s(MACROBLOCKD *xd).
19
 */
20

21 22
static void d27_predictor(uint8_t *ypred_ptr, int y_stride, int n,
                          uint8_t *yabove_row, uint8_t *yleft_col) {
John Koleszar's avatar
John Koleszar committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  int r, c, h, w, v;
  int a, b;
  r = 0;
  for (c = 0; c < n - 2; c++) {
    if (c & 1)
      a = yleft_col[r + 1];
    else
      a = (yleft_col[r] + yleft_col[r + 1] + 1) >> 1;
    b = yabove_row[c + 2];
    ypred_ptr[c] = (2 * a + (c + 1) * b + (c + 3) / 2) / (c + 3);
  }
  for (r = 1; r < n / 2 - 1; r++) {
    for (c = 0; c < n - 2 - 2 * r; c++) {
      if (c & 1)
        a = yleft_col[r + 1];
      else
        a = (yleft_col[r] + yleft_col[r + 1] + 1) >> 1;
      b = ypred_ptr[(r - 1) * y_stride + c + 2];
      ypred_ptr[r * y_stride + c] = (2 * a + (c + 1) * b + (c + 3) / 2) / (c + 3);
    }
  }
  for (; r < n - 1; ++r) {
    for (c = 0; c < n; c++) {
      v = (c & 1 ? yleft_col[r + 1] : (yleft_col[r] + yleft_col[r + 1] + 1) >> 1);
      h = r - c / 2;
      ypred_ptr[h * y_stride + c] = v;
    }
  }
  c = 0;
  r = n - 1;
  ypred_ptr[r * y_stride] = (ypred_ptr[(r - 1) * y_stride] +
                             yleft_col[r] + 1) >> 1;
  for (r = n - 2; r >= n / 2; --r) {
    w = c + (n - 1 - r) * 2;
    ypred_ptr[r * y_stride + w] = (ypred_ptr[(r - 1) * y_stride + w] +
                                   ypred_ptr[r * y_stride + w - 1] + 1) >> 1;
  }
  for (c = 1; c < n; c++) {
    for (r = n - 1; r >= n / 2 + c / 2; --r) {
      w = c + (n - 1 - r) * 2;
      ypred_ptr[r * y_stride + w] = (ypred_ptr[(r - 1) * y_stride + w] +
                                     ypred_ptr[r * y_stride + w - 1] + 1) >> 1;
    }
  }
67 68
}

69 70
static void d63_predictor(uint8_t *ypred_ptr, int y_stride, int n,
                          uint8_t *yabove_row, uint8_t *yleft_col) {
John Koleszar's avatar
John Koleszar committed
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 106 107 108 109 110 111 112 113
  int r, c, h, w, v;
  int a, b;
  c = 0;
  for (r = 0; r < n - 2; r++) {
    if (r & 1)
      a = yabove_row[c + 1];
    else
      a = (yabove_row[c] + yabove_row[c + 1] + 1) >> 1;
    b = yleft_col[r + 2];
    ypred_ptr[r * y_stride] = (2 * a + (r + 1) * b + (r + 3) / 2) / (r + 3);
  }
  for (c = 1; c < n / 2 - 1; c++) {
    for (r = 0; r < n - 2 - 2 * c; r++) {
      if (r & 1)
        a = yabove_row[c + 1];
      else
        a = (yabove_row[c] + yabove_row[c + 1] + 1) >> 1;
      b = ypred_ptr[(r + 2) * y_stride + c - 1];
      ypred_ptr[r * y_stride + c] = (2 * a + (c + 1) * b + (c + 3) / 2) / (c + 3);
    }
  }
  for (; c < n - 1; ++c) {
    for (r = 0; r < n; r++) {
      v = (r & 1 ? yabove_row[c + 1] : (yabove_row[c] + yabove_row[c + 1] + 1) >> 1);
      w = c - r / 2;
      ypred_ptr[r * y_stride + w] = v;
    }
  }
  r = 0;
  c = n - 1;
  ypred_ptr[c] = (ypred_ptr[(c - 1)] + yabove_row[c] + 1) >> 1;
  for (c = n - 2; c >= n / 2; --c) {
    h = r + (n - 1 - c) * 2;
    ypred_ptr[h * y_stride + c] = (ypred_ptr[h * y_stride + c - 1] +
                                   ypred_ptr[(h - 1) * y_stride + c] + 1) >> 1;
  }
  for (r = 1; r < n; r++) {
    for (c = n - 1; c >= n / 2 + r / 2; --c) {
      h = r + (n - 1 - c) * 2;
      ypred_ptr[h * y_stride + c] = (ypred_ptr[h * y_stride + c - 1] +
                                     ypred_ptr[(h - 1) * y_stride + c] + 1) >> 1;
    }
  }
114 115
}

116 117
static void d45_predictor(uint8_t *ypred_ptr, int y_stride, int n,
                          uint8_t *yabove_row, uint8_t *yleft_col) {
John Koleszar's avatar
John Koleszar committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  int r, c;
  for (r = 0; r < n - 1; ++r) {
    for (c = 0; c <= r; ++c) {
      ypred_ptr[(r - c) * y_stride + c] =
        (yabove_row[r + 1] * (c + 1) +
         yleft_col[r + 1] * (r - c + 1) + r / 2 + 1) / (r + 2);
    }
  }
  for (c = 0; c <= r; ++c) {
    int yabove_ext = yabove_row[r]; // 2*yabove_row[r] - yabove_row[r-1];
    int yleft_ext = yleft_col[r]; // 2*yleft_col[r] - yleft_col[r-1];
    yabove_ext = (yabove_ext > 255 ? 255 : (yabove_ext < 0 ? 0 : yabove_ext));
    yleft_ext = (yleft_ext > 255 ? 255 : (yleft_ext < 0 ? 0 : yleft_ext));
    ypred_ptr[(r - c) * y_stride + c] =
      (yabove_ext * (c + 1) +
       yleft_ext * (r - c + 1) + r / 2 + 1) / (r + 2);
  }
  for (r = 1; r < n; ++r) {
    for (c = n - r; c < n; ++c)
      ypred_ptr[r * y_stride + c] = (ypred_ptr[(r - 1) * y_stride + c] +
                                     ypred_ptr[r * y_stride + c - 1] + 1) >> 1;
  }
140 141
}

142 143
static void d117_predictor(uint8_t *ypred_ptr, int y_stride, int n,
                           uint8_t *yabove_row, uint8_t *yleft_col) {
John Koleszar's avatar
John Koleszar committed
144 145 146 147 148 149 150 151 152 153 154
  int r, c;
  for (c = 0; c < n; c++)
    ypred_ptr[c] = (yabove_row[c - 1] + yabove_row[c] + 1) >> 1;
  ypred_ptr += y_stride;
  for (c = 0; c < n; c++)
    ypred_ptr[c] = yabove_row[c - 1];
  ypred_ptr += y_stride;
  for (r = 2; r < n; ++r) {
    ypred_ptr[0] = yleft_col[r - 2];
    for (c = 1; c < n; c++)
      ypred_ptr[c] = ypred_ptr[-2 * y_stride + c - 1];
155
    ypred_ptr += y_stride;
John Koleszar's avatar
John Koleszar committed
156
  }
157 158
}

159 160
static void d135_predictor(uint8_t *ypred_ptr, int y_stride, int n,
                           uint8_t *yabove_row, uint8_t *yleft_col) {
John Koleszar's avatar
John Koleszar committed
161 162 163 164 165 166 167 168 169 170 171
  int r, c;
  ypred_ptr[0] = yabove_row[-1];
  for (c = 1; c < n; c++)
    ypred_ptr[c] = yabove_row[c - 1];
  for (r = 1; r < n; ++r)
    ypred_ptr[r * y_stride] = yleft_col[r - 1];

  ypred_ptr += y_stride;
  for (r = 1; r < n; ++r) {
    for (c = 1; c < n; c++) {
      ypred_ptr[c] = ypred_ptr[-y_stride + c - 1];
172
    }
John Koleszar's avatar
John Koleszar committed
173 174
    ypred_ptr += y_stride;
  }
175 176
}

177 178
static void d153_predictor(uint8_t *ypred_ptr, int y_stride, int n,
                           uint8_t *yabove_row, uint8_t *yleft_col) {
John Koleszar's avatar
John Koleszar committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192
  int r, c;
  ypred_ptr[0] = (yabove_row[-1] + yleft_col[0] + 1) >> 1;
  for (r = 1; r < n; r++)
    ypred_ptr[r * y_stride] = (yleft_col[r - 1] + yleft_col[r] + 1) >> 1;
  ypred_ptr++;
  ypred_ptr[0] = yabove_row[-1];
  for (r = 1; r < n; r++)
    ypred_ptr[r * y_stride] = yleft_col[r - 1];
  ypred_ptr++;

  for (c = 0; c < n - 2; c++)
    ypred_ptr[c] = yabove_row[c];
  ypred_ptr += y_stride;
  for (r = 1; r < n; ++r) {
193
    for (c = 0; c < n - 2; c++)
John Koleszar's avatar
John Koleszar committed
194
      ypred_ptr[c] = ypred_ptr[-y_stride + c - 2];
195
    ypred_ptr += y_stride;
John Koleszar's avatar
John Koleszar committed
196
  }
197 198
}

199 200 201
static void corner_predictor(unsigned char *ypred_ptr, int y_stride, int n,
                             unsigned char *yabove_row,
                             unsigned char *yleft_col) {
Paul Wilkins's avatar
Paul Wilkins committed
202
  int mh, mv, maxgradh, maxgradv, x, y, nx, ny;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  int i, j;
  int top_left = yabove_row[-1];
  mh = mv = 0;
  maxgradh = yabove_row[1] - top_left;
  maxgradv = yleft_col[1] - top_left;
  for (i = 2; i < n; ++i) {
    int gh = yabove_row[i] - yabove_row[i - 2];
    int gv = yleft_col[i] - yleft_col[i - 2];
    if (gh > maxgradh) {
      maxgradh = gh;
      mh = i - 1;
    }
    if (gv > maxgradv) {
      maxgradv = gv;
      mv = i - 1;
    }
  }
  nx = mh + mv + 3;
  ny = 2 * n + 1 - nx;

  x = top_left;
  for (i = 0; i <= mh; ++i) x += yabove_row[i];
  for (i = 0; i <= mv; ++i) x += yleft_col[i];
  x += (nx >> 1);
  x /= nx;
  y = 0;
  for (i = mh + 1; i < n; ++i) y += yabove_row[i];
  for (i = mv + 1; i < n; ++i) y += yleft_col[i];
  y += (ny >> 1);
  y /= ny;

  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j)
      ypred_ptr[j] = (i <= mh && j <= mv ? x : y);
    ypred_ptr += y_stride;
  }
}

241
void vp9_recon_intra_mbuv(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
242 243
  int i;
  for (i = 16; i < 24; i += 2) {
Paul Wilkins's avatar
Paul Wilkins committed
244
    BLOCKD *b = &xd->block[i];
245
    vp9_recon2b(b->predictor, b->diff, *(b->base_dst) + b->dst, b->dst_stride);
John Koleszar's avatar
John Koleszar committed
246
  }
John Koleszar's avatar
John Koleszar committed
247 248
}

249
void vp9_build_intra_predictors_internal(unsigned char *src, int src_stride,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
250
                                         unsigned char *ypred_ptr,
251 252
                                         int y_stride, int mode, int bsize,
                                         int up_available, int left_available) {
John Koleszar's avatar
John Koleszar committed
253

Ronald S. Bultje's avatar
Ronald S. Bultje committed
254 255
  unsigned char *yabove_row = src - src_stride;
  unsigned char yleft_col[32];
John Koleszar's avatar
John Koleszar committed
256 257 258
  unsigned char ytop_left = yabove_row[-1];
  int r, c, i;

Ronald S. Bultje's avatar
Ronald S. Bultje committed
259
  for (i = 0; i < bsize; i++) {
260
    yleft_col[i] = src[i * src_stride - 1];
John Koleszar's avatar
John Koleszar committed
261 262 263 264 265 266 267 268 269
  }

  /* for Y */
  switch (mode) {
    case DC_PRED: {
      int expected_dc;
      int i;
      int shift;
      int average = 0;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
270 271
      int log2_bsize_minus_1;

272 273 274 275
      assert(bsize == 4 || bsize == 8 || bsize == 16 || bsize == 32);
      if (bsize == 4) {
        log2_bsize_minus_1 = 1;
      } else if (bsize == 8) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
276 277 278 279 280 281
        log2_bsize_minus_1 = 2;
      } else if (bsize == 16) {
        log2_bsize_minus_1 = 3;
      } else /* bsize == 32 */ {
        log2_bsize_minus_1 = 4;
      }
John Koleszar's avatar
John Koleszar committed
282

283 284
      if (up_available || left_available) {
        if (up_available) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
285
          for (i = 0; i < bsize; i++) {
John Koleszar's avatar
John Koleszar committed
286 287
            average += yabove_row[i];
          }
John Koleszar's avatar
John Koleszar committed
288 289
        }

290
        if (left_available) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
291
          for (i = 0; i < bsize; i++) {
John Koleszar's avatar
John Koleszar committed
292 293
            average += yleft_col[i];
          }
John Koleszar's avatar
John Koleszar committed
294
        }
295
        shift = log2_bsize_minus_1 + up_available + left_available;
John Koleszar's avatar
John Koleszar committed
296 297 298 299 300
        expected_dc = (average + (1 << (shift - 1))) >> shift;
      } else {
        expected_dc = 128;
      }

Ronald S. Bultje's avatar
Ronald S. Bultje committed
301 302 303
      for (r = 0; r < bsize; r++) {
        vpx_memset(ypred_ptr, expected_dc, bsize);
        ypred_ptr += y_stride;
John Koleszar's avatar
John Koleszar committed
304
      }
John Koleszar's avatar
John Koleszar committed
305 306
    }
    break;
John Koleszar's avatar
John Koleszar committed
307
    case V_PRED: {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
308 309
      for (r = 0; r < bsize; r++) {
        memcpy(ypred_ptr, yabove_row, bsize);
John Koleszar's avatar
John Koleszar committed
310 311
        ypred_ptr += y_stride;
      }
John Koleszar's avatar
John Koleszar committed
312 313
    }
    break;
John Koleszar's avatar
John Koleszar committed
314
    case H_PRED: {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
315 316
      for (r = 0; r < bsize; r++) {
        vpx_memset(ypred_ptr, yleft_col[r], bsize);
John Koleszar's avatar
John Koleszar committed
317 318
        ypred_ptr += y_stride;
      }
John Koleszar's avatar
John Koleszar committed
319 320
    }
    break;
John Koleszar's avatar
John Koleszar committed
321
    case TM_PRED: {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
322 323
      for (r = 0; r < bsize; r++) {
        for (c = 0; c < bsize; c++) {
John Koleszar's avatar
John Koleszar committed
324
          int pred =  yleft_col[r] + yabove_row[ c] - ytop_left;
John Koleszar's avatar
John Koleszar committed
325

John Koleszar's avatar
John Koleszar committed
326 327
          if (pred < 0)
            pred = 0;
John Koleszar's avatar
John Koleszar committed
328

John Koleszar's avatar
John Koleszar committed
329 330
          if (pred > 255)
            pred = 255;
John Koleszar's avatar
John Koleszar committed
331

John Koleszar's avatar
John Koleszar committed
332
          ypred_ptr[c] = pred;
John Koleszar's avatar
John Koleszar committed
333 334
        }

John Koleszar's avatar
John Koleszar committed
335 336
        ypred_ptr += y_stride;
      }
John Koleszar's avatar
John Koleszar committed
337 338
    }
    break;
John Koleszar's avatar
John Koleszar committed
339
    case D45_PRED: {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
340
      d45_predictor(ypred_ptr, y_stride, bsize,  yabove_row, yleft_col);
341 342
    }
    break;
John Koleszar's avatar
John Koleszar committed
343
    case D135_PRED: {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
344
      d135_predictor(ypred_ptr, y_stride, bsize,  yabove_row, yleft_col);
345 346
    }
    break;
John Koleszar's avatar
John Koleszar committed
347
    case D117_PRED: {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
348
      d117_predictor(ypred_ptr, y_stride, bsize,  yabove_row, yleft_col);
349 350
    }
    break;
John Koleszar's avatar
John Koleszar committed
351
    case D153_PRED: {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
352
      d153_predictor(ypred_ptr, y_stride, bsize,  yabove_row, yleft_col);
353 354
    }
    break;
John Koleszar's avatar
John Koleszar committed
355
    case D27_PRED: {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
356
      d27_predictor(ypred_ptr, y_stride, bsize,  yabove_row, yleft_col);
357 358
    }
    break;
John Koleszar's avatar
John Koleszar committed
359
    case D63_PRED: {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
360
      d63_predictor(ypred_ptr, y_stride, bsize,  yabove_row, yleft_col);
361 362
    }
    break;
363
    case I8X8_PRED:
John Koleszar's avatar
John Koleszar committed
364 365 366 367 368 369 370
    case B_PRED:
    case NEARESTMV:
    case NEARMV:
    case ZEROMV:
    case NEWMV:
    case SPLITMV:
    case MB_MODE_COUNT:
John Koleszar's avatar
John Koleszar committed
371 372
      break;
  }
John Koleszar's avatar
John Koleszar committed
373 374
}

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 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 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 661 662
#if CONFIG_COMP_INTERINTRA_PRED
static void combine_interintra(MB_PREDICTION_MODE mode,
                               unsigned char *interpred,
                               int interstride,
                               unsigned char *intrapred,
                               int intrastride,
                               int size) {
  // TODO(debargha): Explore different ways of combining predictors
  //                 or designing the tables below
  static const int scale_bits = 8;
  static const int scale_max = 1 << scale_bits;
  static const int scale_round = (1 << scale_bits) - 1;
  // This table is a function A + B*exp(-kx), where x is hor. index
  static const int weights1d[32] = {
    128, 122, 116, 111, 107, 103,  99,  96,
    93, 90, 88, 85, 83, 81, 80, 78,
    77, 76, 75, 74, 73, 72, 71, 70,
    70, 69, 69, 68, 68, 68, 67, 67,
  };
  // This table is a function A + B*exp(-k.sqrt(xy)), where x, y are
  // hor. and vert. indices
  static const int weights2d[1024] = {
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 128, 128, 128, 128, 128, 128, 128,
    128, 122, 120, 118, 116, 115, 114, 113,
    112, 111, 111, 110, 109, 109, 108, 107,
    107, 106, 106, 105, 105, 104, 104, 104,
    103, 103, 102, 102, 102, 101, 101, 101,
    128, 120, 116, 114, 112, 111, 109, 108,
    107, 106, 105, 104, 103, 102, 102, 101,
    100, 100,  99,  99,  98,  97,  97,  96,
    96,  96,  95,  95,  94,  94,  93,  93,
    128, 118, 114, 111, 109, 107, 106, 104,
    103, 102, 101, 100,  99,  98,  97,  97,
    96,  95,  95,  94,  93,  93,  92,  92,
    91,  91,  90,  90,  90,  89,  89,  88,
    128, 116, 112, 109, 107, 105, 103, 102,
    100,  99,  98,  97,  96,  95,  94,  93,
    93,  92,  91,  91,  90,  90,  89,  89,
    88,  88,  87,  87,  86,  86,  85,  85,
    128, 115, 111, 107, 105, 103, 101,  99,
    98,  97,  96,  94,  93,  93,  92,  91,
    90,  89,  89,  88,  88,  87,  86,  86,
    85,  85,  84,  84,  84,  83,  83,  82,
    128, 114, 109, 106, 103, 101,  99,  97,
    96,  95,  93,  92,  91,  90,  90,  89,
    88,  87,  87,  86,  85,  85,  84,  84,
    83,  83,  82,  82,  82,  81,  81,  80,
    128, 113, 108, 104, 102,  99,  97,  96,
    94,  93,  92,  91,  90,  89,  88,  87,
    86,  85,  85,  84,  84,  83,  83,  82,
    82,  81,  81,  80,  80,  79,  79,  79,
    128, 112, 107, 103, 100,  98,  96,  94,
    93,  91,  90,  89,  88,  87,  86,  85,
    85,  84,  83,  83,  82,  82,  81,  80,
    80,  80,  79,  79,  78,  78,  78,  77,
    128, 111, 106, 102,  99,  97,  95,  93,
    91,  90,  89,  88,  87,  86,  85,  84,
    83,  83,  82,  81,  81,  80,  80,  79,
    79,  78,  78,  77,  77,  77,  76,  76,
    128, 111, 105, 101,  98,  96,  93,  92,
    90,  89,  88,  86,  85,  84,  84,  83,
    82,  81,  81,  80,  80,  79,  79,  78,
    78,  77,  77,  76,  76,  76,  75,  75,
    128, 110, 104, 100,  97,  94,  92,  91,
    89,  88,  86,  85,  84,  83,  83,  82,
    81,  80,  80,  79,  79,  78,  78,  77,
    77,  76,  76,  75,  75,  75,  74,  74,
    128, 109, 103,  99,  96,  93,  91,  90,
    88,  87,  85,  84,  83,  82,  82,  81,
    80,  79,  79,  78,  78,  77,  77,  76,
    76,  75,  75,  75,  74,  74,  74,  73,
    128, 109, 102,  98,  95,  93,  90,  89,
    87,  86,  84,  83,  82,  81,  81,  80,
    79,  78,  78,  77,  77,  76,  76,  75,
    75,  75,  74,  74,  73,  73,  73,  73,
    128, 108, 102,  97,  94,  92,  90,  88,
    86,  85,  84,  83,  82,  81,  80,  79,
    78,  78,  77,  77,  76,  76,  75,  75,
    74,  74,  73,  73,  73,  73,  72,  72,
    128, 107, 101,  97,  93,  91,  89,  87,
    85,  84,  83,  82,  81,  80,  79,  78,
    78,  77,  76,  76,  75,  75,  74,  74,
    74,  73,  73,  73,  72,  72,  72,  71,
    128, 107, 100,  96,  93,  90,  88,  86,
    85,  83,  82,  81,  80,  79,  78,  78,
    77,  76,  76,  75,  75,  74,  74,  73,
    73,  73,  72,  72,  72,  71,  71,  71,
    128, 106, 100,  95,  92,  89,  87,  85,
    84,  83,  81,  80,  79,  78,  78,  77,
    76,  76,  75,  75,  74,  74,  73,  73,
    72,  72,  72,  72,  71,  71,  71,  70,
    128, 106,  99,  95,  91,  89,  87,  85,
    83,  82,  81,  80,  79,  78,  77,  76,
    76,  75,  75,  74,  74,  73,  73,  72,
    72,  72,  71,  71,  71,  71,  70,  70,
    128, 105,  99,  94,  91,  88,  86,  84,
    83,  81,  80,  79,  78,  77,  77,  76,
    75,  75,  74,  74,  73,  73,  72,  72,
    72,  71,  71,  71,  70,  70,  70,  70,
    128, 105,  98,  93,  90,  88,  85,  84,
    82,  81,  80,  79,  78,  77,  76,  75,
    75,  74,  74,  73,  73,  72,  72,  71,
    71,  71,  71,  70,  70,  70,  70,  69,
    128, 104,  97,  93,  90,  87,  85,  83,
    82,  80,  79,  78,  77,  76,  76,  75,
    74,  74,  73,  73,  72,  72,  71,  71,
    71,  70,  70,  70,  70,  69,  69,  69,
    128, 104,  97,  92,  89,  86,  84,  83,
    81,  80,  79,  78,  77,  76,  75,  74,
    74,  73,  73,  72,  72,  71,  71,  71,
    70,  70,  70,  70,  69,  69,  69,  69,
    128, 104,  96,  92,  89,  86,  84,  82,
    80,  79,  78,  77,  76,  75,  75,  74,
    73,  73,  72,  72,  71,  71,  71,  70,
    70,  70,  70,  69,  69,  69,  69,  68,
    128, 103,  96,  91,  88,  85,  83,  82,
    80,  79,  78,  77,  76,  75,  74,  74,
    73,  72,  72,  72,  71,  71,  70,  70,
    70,  70,  69,  69,  69,  69,  68,  68,
    128, 103,  96,  91,  88,  85,  83,  81,
    80,  78,  77,  76,  75,  75,  74,  73,
    73,  72,  72,  71,  71,  70,  70,  70,
    70,  69,  69,  69,  69,  68,  68,  68,
    128, 102,  95,  90,  87,  84,  82,  81,
    79,  78,  77,  76,  75,  74,  73,  73,
    72,  72,  71,  71,  71,  70,  70,  70,
    69,  69,  69,  69,  68,  68,  68,  68,
    128, 102,  95,  90,  87,  84,  82,  80,
    79,  77,  76,  75,  75,  74,  73,  73,
    72,  72,  71,  71,  70,  70,  70,  69,
    69,  69,  69,  68,  68,  68,  68,  68,
    128, 102,  94,  90,  86,  84,  82,  80,
    78,  77,  76,  75,  74,  73,  73,  72,
    72,  71,  71,  70,  70,  70,  69,  69,
    69,  69,  68,  68,  68,  68,  68,  67,
    128, 101,  94,  89,  86,  83,  81,  79,
    78,  77,  76,  75,  74,  73,  73,  72,
    71,  71,  71,  70,  70,  69,  69,  69,
    69,  68,  68,  68,  68,  68,  67,  67,
    128, 101,  93,  89,  85,  83,  81,  79,
    78,  76,  75,  74,  74,  73,  72,  72,
    71,  71,  70,  70,  70,  69,  69,  69,
    68,  68,  68,  68,  68,  67,  67,  67,
    128, 101,  93,  88,  85,  82,  80,  79,
    77,  76,  75,  74,  73,  73,  72,  71,
    71,  70,  70,  70,  69,  69,  69,  68,
    68,  68,  68,  68,  67,  67,  67,  67,
  };
  int size_scale = (size == 32 ? 1 :
                    size == 16 ? 2 :
                    size == 8  ? 4 : 8);
  int i, j;
  switch (mode) {
    case V_PRED:
      for (i = 0; i < size; ++i) {
        for (j = 0; j < size; ++j) {
          int k = i * interstride + j;
          int scale = weights1d[i * size_scale];
          interpred[k] =
              ((scale_max - scale) * interpred[k] +
               scale * intrapred[i * intrastride + j] + scale_round)
              >> scale_bits;
        }
      }
      break;

    case H_PRED:
      for (i = 0; i < size; ++i) {
        for (j = 0; j < size; ++j) {
          int k = i * interstride + j;
          int scale = weights1d[j * size_scale];
          interpred[k] =
              ((scale_max - scale) * interpred[k] +
               scale * intrapred[i * intrastride + j] + scale_round)
              >> scale_bits;
        }
      }
      break;

    case D63_PRED:
    case D117_PRED:
      for (i = 0; i < size; ++i) {
        for (j = 0; j < size; ++j) {
          int k = i * interstride + j;
          int scale = (weights2d[i * size_scale * 32 + j * size_scale] +
                       weights1d[i * size_scale]) >> 1;
          interpred[k] =
              ((scale_max - scale) * interpred[k] +
               scale * intrapred[i * intrastride + j] + scale_round)
              >> scale_bits;
        }
      }
      break;

    case D27_PRED:
    case D153_PRED:
      for (i = 0; i < size; ++i) {
        for (j = 0; j < size; ++j) {
          int k = i * interstride + j;
          int scale = (weights2d[i * size_scale * 32 + j * size_scale] +
                       weights1d[j * size_scale]) >> 1;
          interpred[k] =
              ((scale_max - scale) * interpred[k] +
               scale * intrapred[i * intrastride + j] + scale_round)
              >> scale_bits;
        }
      }
      break;

    case D135_PRED:
      for (i = 0; i < size; ++i) {
        for (j = 0; j < size; ++j) {
          int k = i * interstride + j;
          int scale = weights2d[i * size_scale * 32 + j * size_scale];
          interpred[k] =
              ((scale_max - scale) * interpred[k] +
               scale * intrapred[i * intrastride + j] + scale_round)
              >> scale_bits;
        }
      }
      break;

    case D45_PRED:
    case DC_PRED:
    case TM_PRED:
    default:
      // simple average
      for (i = 0; i < size; ++i) {
        for (j = 0; j < size; ++j) {
          int k = i * interstride + j;
          interpred[k] = (interpred[k] + intrapred[i * intrastride + j]) >> 1;
        }
      }
      break;
  }
}

void vp9_build_interintra_16x16_predictors_mb(MACROBLOCKD *xd,
                                              unsigned char *ypred,
                                              unsigned char *upred,
                                              unsigned char *vpred,
                                              int ystride, int uvstride) {
  vp9_build_interintra_16x16_predictors_mby(xd, ypred, ystride);
  vp9_build_interintra_16x16_predictors_mbuv(xd, upred, vpred, uvstride);
}

void vp9_build_interintra_16x16_predictors_mby(MACROBLOCKD *xd,
                                               unsigned char *ypred,
                                               int ystride) {
  static const int scale_bits = 6;
  unsigned char intrapredictor[256];
  int i, j;
  vp9_build_intra_predictors_internal(
      xd->dst.y_buffer, xd->dst.y_stride,
      intrapredictor, 16,
      xd->mode_info_context->mbmi.interintra_mode, 16,
      xd->up_available, xd->left_available);
  combine_interintra(xd->mode_info_context->mbmi.interintra_mode,
                     ypred, ystride, intrapredictor, 16, 16);
}

void vp9_build_interintra_16x16_predictors_mbuv(MACROBLOCKD *xd,
                                                unsigned char *upred,
                                                unsigned char *vpred,
                                                int uvstride) {
  int i, j;
  unsigned char uintrapredictor[64];
  unsigned char vintrapredictor[64];
  vp9_build_intra_predictors_internal(
      xd->dst.u_buffer, xd->dst.uv_stride,
      uintrapredictor, 8,
      xd->mode_info_context->mbmi.interintra_uv_mode, 8,
      xd->up_available, xd->left_available);
  vp9_build_intra_predictors_internal(
      xd->dst.v_buffer, xd->dst.uv_stride,
      vintrapredictor, 8,
      xd->mode_info_context->mbmi.interintra_uv_mode, 8,
      xd->up_available, xd->left_available);
  combine_interintra(xd->mode_info_context->mbmi.interintra_uv_mode,
                     upred, uvstride, uintrapredictor, 8, 8);
  combine_interintra(xd->mode_info_context->mbmi.interintra_uv_mode,
                     vpred, uvstride, vintrapredictor, 8, 8);
}
#endif

663 664
void vp9_build_intra_predictors_mby(MACROBLOCKD *xd) {
  vp9_build_intra_predictors_internal(xd->dst.y_buffer, xd->dst.y_stride,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
665
                                      xd->predictor, 16,
666 667
                                      xd->mode_info_context->mbmi.mode, 16,
                                      xd->up_available, xd->left_available);
668
}
John Koleszar's avatar
John Koleszar committed
669

670 671
void vp9_build_intra_predictors_mby_s(MACROBLOCKD *xd) {
  vp9_build_intra_predictors_internal(xd->dst.y_buffer, xd->dst.y_stride,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
672
                                      xd->dst.y_buffer, xd->dst.y_stride,
673 674
                                      xd->mode_info_context->mbmi.mode, 16,
                                      xd->up_available, xd->left_available);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
675 676 677
}

#if CONFIG_SUPERBLOCKS
678 679
void vp9_build_intra_predictors_sby_s(MACROBLOCKD *xd) {
  vp9_build_intra_predictors_internal(xd->dst.y_buffer, xd->dst.y_stride,
680 681 682
                                      xd->dst.y_buffer, xd->dst.y_stride,
                                      xd->mode_info_context->mbmi.mode, 32,
                                      xd->up_available, xd->left_available);
683
}
Ronald S. Bultje's avatar
Ronald S. Bultje committed
684
#endif
John Koleszar's avatar
John Koleszar committed
685

686
#if CONFIG_COMP_INTRA_PRED
687
void vp9_build_comp_intra_predictors_mby(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
688 689 690
  unsigned char predictor[2][256];
  int i;

691
  vp9_build_intra_predictors_internal(xd->dst.y_buffer, xd->dst.y_stride,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
692
                                      predictor[0], 16,
693
                                      xd->mode_info_context->mbmi.mode,
694 695
                                      16, xd->up_available,
                                      xd->left_available);
696
  vp9_build_intra_predictors_internal(xd->dst.y_buffer, xd->dst.y_stride,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
697
                                      predictor[1], 16,
698
                                      xd->mode_info_context->mbmi.second_mode,
699 700
                                      16, xd->up_available,
                                      xd->left_available);
John Koleszar's avatar
John Koleszar committed
701 702

  for (i = 0; i < 256; i++) {
Paul Wilkins's avatar
Paul Wilkins committed
703
    xd->predictor[i] = (predictor[0][i] + predictor[1][i] + 1) >> 1;
John Koleszar's avatar
John Koleszar committed
704
  }
John Koleszar's avatar
John Koleszar committed
705
}
706
#endif
John Koleszar's avatar
John Koleszar committed
707

708
void vp9_build_intra_predictors_mbuv_internal(MACROBLOCKD *xd,
709 710 711
                                              unsigned char *upred_ptr,
                                              unsigned char *vpred_ptr,
                                              int uv_stride,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
712
                                              int mode, int bsize) {
713
  vp9_build_intra_predictors_internal(xd->dst.u_buffer, xd->dst.uv_stride,
714 715
                                      upred_ptr, uv_stride, mode, bsize,
                                      xd->up_available, xd->left_available);
716
  vp9_build_intra_predictors_internal(xd->dst.v_buffer, xd->dst.uv_stride,
717 718
                                      vpred_ptr, uv_stride, mode, bsize,
                                      xd->up_available, xd->left_available);
John Koleszar's avatar
John Koleszar committed
719
}
720

721 722
void vp9_build_intra_predictors_mbuv(MACROBLOCKD *xd) {
  vp9_build_intra_predictors_mbuv_internal(xd, &xd->predictor[256],
Ronald S. Bultje's avatar
Ronald S. Bultje committed
723 724 725
                                           &xd->predictor[320], 8,
                                           xd->mode_info_context->mbmi.uv_mode,
                                           8);
726 727
}

728 729
void vp9_build_intra_predictors_mbuv_s(MACROBLOCKD *xd) {
  vp9_build_intra_predictors_mbuv_internal(xd, xd->dst.u_buffer,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
730 731 732 733
                                           xd->dst.v_buffer,
                                           xd->dst.uv_stride,
                                           xd->mode_info_context->mbmi.uv_mode,
                                           8);
734 735
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
736
#if CONFIG_SUPERBLOCKS
737 738
void vp9_build_intra_predictors_sbuv_s(MACROBLOCKD *xd) {
  vp9_build_intra_predictors_mbuv_internal(xd, xd->dst.u_buffer,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
739 740 741 742 743 744
                                           xd->dst.v_buffer, xd->dst.uv_stride,
                                           xd->mode_info_context->mbmi.uv_mode,
                                           16);
}
#endif

745
#if CONFIG_COMP_INTRA_PRED
746
void vp9_build_comp_intra_predictors_mbuv(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
747 748 749
  unsigned char predictor[2][2][64];
  int i;

750
  vp9_build_intra_predictors_mbuv_internal(
751 752
      xd, predictor[0][0], predictor[1][0], 8,
      xd->mode_info_context->mbmi.uv_mode, 8);
753
  vp9_build_intra_predictors_mbuv_internal(
754 755
      xd, predictor[0][1], predictor[1][1], 8,
      xd->mode_info_context->mbmi.second_uv_mode, 8);
John Koleszar's avatar
John Koleszar committed
756
  for (i = 0; i < 64; i++) {
Paul Wilkins's avatar
Paul Wilkins committed
757
    xd->predictor[256 + i] = (predictor[0][0][i] + predictor[0][1][i] + 1) >> 1;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
758 759
    xd->predictor[256 + 64 + i] = (predictor[1][0][i] +
                                   predictor[1][1][i] + 1) >> 1;
John Koleszar's avatar
John Koleszar committed
760
  }
761 762 763
}
#endif

764
void vp9_intra8x8_predict(BLOCKD *xd,
Yaowu Xu's avatar
Yaowu Xu committed
765
                          int mode,
John Koleszar's avatar
John Koleszar committed
766
                          unsigned char *predictor) {
767
  vp9_build_intra_predictors_internal(*(xd->base_dst) + xd->dst,
768 769
                                      xd->dst_stride, predictor, 16,
                                      mode, 8, 1, 1);
Yaowu Xu's avatar
Yaowu Xu committed
770 771
}

772
#if CONFIG_COMP_INTRA_PRED
773
void vp9_comp_intra8x8_predict(BLOCKD *xd,
774
                               int mode, int second_mode,
John Koleszar's avatar
John Koleszar committed
775 776 777 778
                               unsigned char *out_predictor) {
  unsigned char predictor[2][8 * 16];
  int i, j;

779 780
  vp9_intra8x8_predict(xd, mode, predictor[0]);
  vp9_intra8x8_predict(xd, second_mode, predictor[1]);
John Koleszar's avatar
John Koleszar committed
781 782 783 784

  for (i = 0; i < 8 * 16; i += 16) {
    for (j = i; j < i + 8; j++) {
      out_predictor[j] = (predictor[0][j] + predictor[1][j] + 1) >> 1;
785
    }
John Koleszar's avatar
John Koleszar committed
786
  }
787 788 789
}
#endif

790
void vp9_intra_uv4x4_predict(BLOCKD *xd,
Yaowu Xu's avatar
Yaowu Xu committed
791
                             int mode,
John Koleszar's avatar
John Koleszar committed
792
                             unsigned char *predictor) {
793
  vp9_build_intra_predictors_internal(*(xd->base_dst) + xd->dst,
794 795
                                      xd->dst_stride, predictor, 8,
                                      mode, 4, 1, 1);
Yaowu Xu's avatar
Yaowu Xu committed
796 797
}

798
#if CONFIG_COMP_INTRA_PRED
799
void vp9_comp_intra_uv4x4_predict(BLOCKD *xd,
800
                                  int mode, int mode2,
John Koleszar's avatar
John Koleszar committed
801 802 803 804
                                  unsigned char *out_predictor) {
  unsigned char predictor[2][8 * 4];
  int i, j;

805 806
  vp9_intra_uv4x4_predict(xd, mode, predictor[0]);
  vp9_intra_uv4x4_predict(xd, mode2, predictor[1]);
John Koleszar's avatar
John Koleszar committed
807 808 809 810

  for (i = 0; i < 4 * 8; i += 8) {
    for (j = i; j < i + 4; j++) {
      out_predictor[j] = (predictor[0][j] + predictor[1][j] + 1) >> 1;
811
    }
John Koleszar's avatar
John Koleszar committed
812
  }
813 814
}
#endif
Yaowu Xu's avatar
Yaowu Xu committed
815 816

/* TODO: try different ways of use Y-UV mode correlation
817 818 819
   Current code assumes that a uv 4x4 block use same mode
   as corresponding Y 8x8 area
   */