intrapred.c 35.5 KB
Newer Older
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 11
 */

12
#include <assert.h>
13 14
#include <math.h>

Yaowu Xu's avatar
Yaowu Xu committed
15 16
#include "./aom_config.h"
#include "./aom_dsp_rtcd.h"
17

Yaowu Xu's avatar
Yaowu Xu committed
18 19
#include "aom_dsp/aom_dsp_common.h"
#include "aom_mem/aom_mem.h"
20
#include "aom_ports/bitops.h"
21

clang-format's avatar
clang-format committed
22
#define DST(x, y) dst[(x) + (y)*stride]
23 24 25
#define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)
#define AVG2(a, b) (((a) + (b) + 1) >> 1)

26 27 28
static INLINE void d207e_predictor(uint8_t *dst, ptrdiff_t stride, int bw,
                                   int bh, const uint8_t *above,
                                   const uint8_t *left) {
29
  int r, c;
clang-format's avatar
clang-format committed
30
  (void)above;
31

32 33
  for (r = 0; r < bh; ++r) {
    for (c = 0; c < bw; ++c) {
34 35
      dst[c] = c & 1 ? AVG3(left[(c >> 1) + r], left[(c >> 1) + r + 1],
                            left[(c >> 1) + r + 2])
clang-format's avatar
clang-format committed
36
                     : AVG2(left[(c >> 1) + r], left[(c >> 1) + r + 1]);
37 38 39 40 41
    }
    dst += stride;
  }
}

42 43 44
static INLINE void d63e_predictor(uint8_t *dst, ptrdiff_t stride, int bw,
                                  int bh, const uint8_t *above,
                                  const uint8_t *left) {
45
  int r, c;
clang-format's avatar
clang-format committed
46
  (void)left;
47 48
  for (r = 0; r < bh; ++r) {
    for (c = 0; c < bw; ++c) {
49 50
      dst[c] = r & 1 ? AVG3(above[(r >> 1) + c], above[(r >> 1) + c + 1],
                            above[(r >> 1) + c + 2])
clang-format's avatar
clang-format committed
51
                     : AVG2(above[(r >> 1) + c], above[(r >> 1) + c + 1]);
52 53 54 55 56
    }
    dst += stride;
  }
}

57 58 59
static INLINE void d45e_predictor(uint8_t *dst, ptrdiff_t stride, int bw,
                                  int bh, const uint8_t *above,
                                  const uint8_t *left) {
60
  int r, c;
clang-format's avatar
clang-format committed
61
  (void)left;
62 63
  for (r = 0; r < bh; ++r) {
    for (c = 0; c < bw; ++c) {
64
      dst[c] = AVG3(above[r + c], above[r + c + 1],
65
                    above[r + c + 1 + (r + c + 2 < bw + bh)]);
66 67 68 69 70
    }
    dst += stride;
  }
}

71 72 73
static INLINE void d117_predictor(uint8_t *dst, ptrdiff_t stride, int bw,
                                  int bh, const uint8_t *above,
                                  const uint8_t *left) {
74 75 76
  int r, c;

  // first row
77
  for (c = 0; c < bw; c++) dst[c] = AVG2(above[c - 1], above[c]);
78 79 80 81
  dst += stride;

  // second row
  dst[0] = AVG3(left[0], above[-1], above[0]);
82
  for (c = 1; c < bw; c++) dst[c] = AVG3(above[c - 2], above[c - 1], above[c]);
83 84 85 86
  dst += stride;

  // the rest of first col
  dst[0] = AVG3(above[-1], left[0], left[1]);
87
  for (r = 3; r < bh; ++r)
88 89 90
    dst[(r - 2) * stride] = AVG3(left[r - 3], left[r - 2], left[r - 1]);

  // the rest of the block
91 92
  for (r = 2; r < bh; ++r) {
    for (c = 1; c < bw; c++) dst[c] = dst[-2 * stride + c - 1];
93 94 95 96
    dst += stride;
  }
}

97 98 99
static INLINE void d135_predictor(uint8_t *dst, ptrdiff_t stride, int bw,
                                  int bh, const uint8_t *above,
                                  const uint8_t *left) {
100
  int i;
101 102 103 104 105 106 107 108 109
#if CONFIG_TX64X64
#if defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ > 7
  // silence a spurious -Warray-bounds warning, possibly related to:
  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56273
  uint8_t border[133];
#else
  uint8_t border[64 + 64 - 1];  // outer border from bottom-left to top-right
#endif
#else
110 111 112 113 114 115 116
#if defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ > 7
  // silence a spurious -Warray-bounds warning, possibly related to:
  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56273
  uint8_t border[69];
#else
  uint8_t border[32 + 32 - 1];  // outer border from bottom-left to top-right
#endif
117
#endif  // CONFIG_TX64X64
118

119 120 121
  // dst(bh, bh - 2)[0], i.e., border starting at bottom-left
  for (i = 0; i < bh - 2; ++i) {
    border[i] = AVG3(left[bh - 3 - i], left[bh - 2 - i], left[bh - 1 - i]);
122
  }
123 124 125
  border[bh - 2] = AVG3(above[-1], left[0], left[1]);
  border[bh - 1] = AVG3(left[0], above[-1], above[0]);
  border[bh - 0] = AVG3(above[-1], above[0], above[1]);
126
  // dst[0][2, size), i.e., remaining top border ascending
127 128
  for (i = 0; i < bw - 2; ++i) {
    border[bh + 1 + i] = AVG3(above[i], above[i + 1], above[i + 2]);
129
  }
130

131 132
  for (i = 0; i < bh; ++i) {
    memcpy(dst + i * stride, border + bh - 1 - i, bw);
133 134 135
  }
}

136 137 138
static INLINE void d153_predictor(uint8_t *dst, ptrdiff_t stride, int bw,
                                  int bh, const uint8_t *above,
                                  const uint8_t *left) {
139 140
  int r, c;
  dst[0] = AVG2(above[-1], left[0]);
141
  for (r = 1; r < bh; r++) dst[r * stride] = AVG2(left[r - 1], left[r]);
142 143 144 145
  dst++;

  dst[0] = AVG3(left[0], above[-1], above[0]);
  dst[stride] = AVG3(above[-1], left[0], left[1]);
146
  for (r = 2; r < bh; r++)
147 148 149
    dst[r * stride] = AVG3(left[r - 2], left[r - 1], left[r]);
  dst++;

150
  for (c = 0; c < bw - 2; c++)
151 152 153
    dst[c] = AVG3(above[c - 1], above[c], above[c + 1]);
  dst += stride;

154 155
  for (r = 1; r < bh; ++r) {
    for (c = 0; c < bw - 2; c++) dst[c] = dst[-stride + c - 2];
156 157 158 159
    dst += stride;
  }
}

160
static INLINE void v_predictor(uint8_t *dst, ptrdiff_t stride, int bw, int bh,
161 162
                               const uint8_t *above, const uint8_t *left) {
  int r;
clang-format's avatar
clang-format committed
163
  (void)left;
164

165 166
  for (r = 0; r < bh; r++) {
    memcpy(dst, above, bw);
167 168 169 170
    dst += stride;
  }
}

171
static INLINE void h_predictor(uint8_t *dst, ptrdiff_t stride, int bw, int bh,
172 173
                               const uint8_t *above, const uint8_t *left) {
  int r;
clang-format's avatar
clang-format committed
174
  (void)above;
175

176 177
  for (r = 0; r < bh; r++) {
    memset(dst, left[r], bw);
178 179 180 181
    dst += stride;
  }
}

Urvang Joshi's avatar
Urvang Joshi committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
static INLINE int abs_diff(int a, int b) { return (a > b) ? a - b : b - a; }

static INLINE uint16_t paeth_predictor_single(uint16_t left, uint16_t top,
                                              uint16_t top_left) {
  const int base = top + left - top_left;
  const int p_left = abs_diff(base, left);
  const int p_top = abs_diff(base, top);
  const int p_top_left = abs_diff(base, top_left);

  // Return nearest to base of left, top and top_left.
  return (p_left <= p_top && p_left <= p_top_left)
             ? left
             : (p_top <= p_top_left) ? top : top_left;
}

197 198 199
static INLINE void paeth_predictor(uint8_t *dst, ptrdiff_t stride, int bw,
                                   int bh, const uint8_t *above,
                                   const uint8_t *left) {
Urvang Joshi's avatar
Urvang Joshi committed
200 201 202
  int r, c;
  const uint8_t ytop_left = above[-1];

203 204
  for (r = 0; r < bh; r++) {
    for (c = 0; c < bw; c++)
Urvang Joshi's avatar
Urvang Joshi committed
205 206 207 208 209
      dst[c] = (uint8_t)paeth_predictor_single(left[r], above[c], ytop_left);
    dst += stride;
  }
}

Urvang Joshi's avatar
Urvang Joshi committed
210 211 212
// Weights are quadratic from '1' to '1 / block_size', scaled by
// 2^sm_weight_log2_scale.
static const int sm_weight_log2_scale = 8;
213

214
#if CONFIG_TX64X64
215 216
// max(block_size_wide[BLOCK_LARGEST], block_size_high[BLOCK_LARGEST])
#define MAX_BLOCK_DIM 64
217
#else
218
#define MAX_BLOCK_DIM 32
219
#endif  // CONFIG_TX64X64
220

221 222 223
static const uint8_t sm_weight_arrays[2 * MAX_BLOCK_DIM] = {
  // Unused, because we always offset by bs, which is at least 2.
  0, 0,
224
  // bs = 2
225
  255, 128,
226
  // bs = 4
227
  255, 149, 85, 64,
228
  // bs = 8
229
  255, 197, 146, 105, 73, 50, 37, 32,
230
  // bs = 16
231
  255, 225, 196, 170, 145, 123, 102, 84, 68, 54, 43, 33, 26, 20, 17, 16,
232
  // bs = 32
233 234
  255, 240, 225, 210, 196, 182, 169, 157, 145, 133, 122, 111, 101, 92, 83, 74,
  66, 59, 52, 45, 39, 34, 29, 25, 21, 17, 14, 12, 10, 9, 8, 8,
235 236
#if CONFIG_TX64X64
  // bs = 64
237 238 239 240
  255, 248, 240, 233, 225, 218, 210, 203, 196, 189, 182, 176, 169, 163, 156,
  150, 144, 138, 133, 127, 121, 116, 111, 106, 101, 96, 91, 86, 82, 77, 73, 69,
  65, 61, 57, 54, 50, 47, 44, 41, 38, 35, 32, 29, 27, 25, 22, 20, 18, 16, 15,
  13, 12, 10, 9, 8, 7, 6, 6, 5, 5, 4, 4, 4,
241 242 243
#endif  // CONFIG_TX64X64
};

244
// Some basic checks on weights for smooth predictor.
245 246 247 248 249 250
#define sm_weights_sanity_checks(weights_w, weights_h, weights_scale, \
                                 pred_scale)                          \
  assert(weights_w[0] < weights_scale);                               \
  assert(weights_h[0] < weights_scale);                               \
  assert(weights_scale - weights_w[bw - 1] < weights_scale);          \
  assert(weights_scale - weights_h[bh - 1] < weights_scale);          \
251 252
  assert(pred_scale < 31)  // ensures no overflow when calculating predictor.

253 254
#define divide_round(value, bits) (((value) + (1 << ((bits)-1))) >> (bits))

255 256 257 258 259 260 261
static INLINE void smooth_predictor(uint8_t *dst, ptrdiff_t stride, int bw,
                                    int bh, const uint8_t *above,
                                    const uint8_t *left) {
  const uint8_t below_pred = left[bh - 1];   // estimated by bottom-left pixel
  const uint8_t right_pred = above[bw - 1];  // estimated by top-right pixel
  const uint8_t *const sm_weights_w = sm_weight_arrays + bw;
  const uint8_t *const sm_weights_h = sm_weight_arrays + bh;
262 263
  // scale = 2 * 2^sm_weight_log2_scale
  const int log2_scale = 1 + sm_weight_log2_scale;
264
  const uint16_t scale = (1 << sm_weight_log2_scale);
265 266
  sm_weights_sanity_checks(sm_weights_w, sm_weights_h, scale,
                           log2_scale + sizeof(*dst));
267
  int r;
268
  for (r = 0; r < bh; ++r) {
269
    int c;
270
    for (c = 0; c < bw; ++c) {
271
      const uint8_t pixels[] = { above[c], below_pred, left[r], right_pred };
272 273
      const uint8_t weights[] = { sm_weights_h[r], scale - sm_weights_h[r],
                                  sm_weights_w[c], scale - sm_weights_w[c] };
274
      uint32_t this_pred = 0;
275
      int i;
276
      assert(scale >= sm_weights_h[r] && scale >= sm_weights_w[c]);
277 278 279
      for (i = 0; i < 4; ++i) {
        this_pred += weights[i] * pixels[i];
      }
280
      dst[c] = clip_pixel(divide_round(this_pred, log2_scale));
281 282 283 284 285
    }
    dst += stride;
  }
}

Urvang Joshi's avatar
Urvang Joshi committed
286
#if CONFIG_SMOOTH_HV
287 288
static INLINE void smooth_v_predictor(uint8_t *dst, ptrdiff_t stride, int bw,
                                      int bh, const uint8_t *above,
Urvang Joshi's avatar
Urvang Joshi committed
289
                                      const uint8_t *left) {
290 291
  const uint8_t below_pred = left[bh - 1];  // estimated by bottom-left pixel
  const uint8_t *const sm_weights = sm_weight_arrays + bh;
Urvang Joshi's avatar
Urvang Joshi committed
292 293 294
  // scale = 2^sm_weight_log2_scale
  const int log2_scale = sm_weight_log2_scale;
  const uint16_t scale = (1 << sm_weight_log2_scale);
295 296
  sm_weights_sanity_checks(sm_weights, sm_weights, scale,
                           log2_scale + sizeof(*dst));
Urvang Joshi's avatar
Urvang Joshi committed
297 298

  int r;
299
  for (r = 0; r < bh; r++) {
Urvang Joshi's avatar
Urvang Joshi committed
300
    int c;
301
    for (c = 0; c < bw; ++c) {
Urvang Joshi's avatar
Urvang Joshi committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315
      const uint8_t pixels[] = { above[c], below_pred };
      const uint8_t weights[] = { sm_weights[r], scale - sm_weights[r] };
      uint32_t this_pred = 0;
      assert(scale >= sm_weights[r]);
      int i;
      for (i = 0; i < 2; ++i) {
        this_pred += weights[i] * pixels[i];
      }
      dst[c] = clip_pixel(divide_round(this_pred, log2_scale));
    }
    dst += stride;
  }
}

316 317
static INLINE void smooth_h_predictor(uint8_t *dst, ptrdiff_t stride, int bw,
                                      int bh, const uint8_t *above,
Urvang Joshi's avatar
Urvang Joshi committed
318
                                      const uint8_t *left) {
319 320
  const uint8_t right_pred = above[bw - 1];  // estimated by top-right pixel
  const uint8_t *const sm_weights = sm_weight_arrays + bw;
Urvang Joshi's avatar
Urvang Joshi committed
321 322 323
  // scale = 2^sm_weight_log2_scale
  const int log2_scale = sm_weight_log2_scale;
  const uint16_t scale = (1 << sm_weight_log2_scale);
324 325
  sm_weights_sanity_checks(sm_weights, sm_weights, scale,
                           log2_scale + sizeof(*dst));
Urvang Joshi's avatar
Urvang Joshi committed
326 327

  int r;
328
  for (r = 0; r < bh; r++) {
Urvang Joshi's avatar
Urvang Joshi committed
329
    int c;
330
    for (c = 0; c < bw; ++c) {
Urvang Joshi's avatar
Urvang Joshi committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
      const uint8_t pixels[] = { left[r], right_pred };
      const uint8_t weights[] = { sm_weights[c], scale - sm_weights[c] };
      uint32_t this_pred = 0;
      assert(scale >= sm_weights[c]);
      int i;
      for (i = 0; i < 2; ++i) {
        this_pred += weights[i] * pixels[i];
      }
      dst[c] = clip_pixel(divide_round(this_pred, log2_scale));
    }
    dst += stride;
  }
}
#endif  // CONFIG_SMOOTH_HV

346 347 348
static INLINE void dc_128_predictor(uint8_t *dst, ptrdiff_t stride, int bw,
                                    int bh, const uint8_t *above,
                                    const uint8_t *left) {
349
  int r;
clang-format's avatar
clang-format committed
350 351
  (void)above;
  (void)left;
352

353 354
  for (r = 0; r < bh; r++) {
    memset(dst, 128, bw);
355 356 357 358
    dst += stride;
  }
}

359 360
static INLINE void dc_left_predictor(uint8_t *dst, ptrdiff_t stride, int bw,
                                     int bh, const uint8_t *above,
361 362
                                     const uint8_t *left) {
  int i, r, expected_dc, sum = 0;
clang-format's avatar
clang-format committed
363
  (void)above;
364

365 366
  for (i = 0; i < bh; i++) sum += left[i];
  expected_dc = (sum + (bh >> 1)) / bh;
367

368 369
  for (r = 0; r < bh; r++) {
    memset(dst, expected_dc, bw);
370 371 372 373
    dst += stride;
  }
}

374 375 376
static INLINE void dc_top_predictor(uint8_t *dst, ptrdiff_t stride, int bw,
                                    int bh, const uint8_t *above,
                                    const uint8_t *left) {
377
  int i, r, expected_dc, sum = 0;
clang-format's avatar
clang-format committed
378
  (void)left;
379

380 381
  for (i = 0; i < bw; i++) sum += above[i];
  expected_dc = (sum + (bw >> 1)) / bw;
382

383 384
  for (r = 0; r < bh; r++) {
    memset(dst, expected_dc, bw);
385 386 387 388
    dst += stride;
  }
}

389
static INLINE void dc_predictor(uint8_t *dst, ptrdiff_t stride, int bw, int bh,
390 391
                                const uint8_t *above, const uint8_t *left) {
  int i, r, expected_dc, sum = 0;
392
  const int count = bw + bh;
393

394
  for (i = 0; i < bw; i++) {
395
    sum += above[i];
396 397
  }
  for (i = 0; i < bh; i++) {
398 399 400 401 402
    sum += left[i];
  }

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

403 404
  for (r = 0; r < bh; r++) {
    memset(dst, expected_dc, bw);
405 406 407 408
    dst += stride;
  }
}

409 410 411 412 413 414 415 416
void aom_d45e_predictor_2x2_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];
  (void)stride;
  (void)left;
Jingning Han's avatar
Jingning Han committed
417

418 419
  DST(0, 0) = AVG3(A, B, C);
  DST(1, 0) = DST(0, 1) = AVG3(B, C, D);
Jingning Han's avatar
Jingning Han committed
420
  DST(1, 1) = AVG3(C, D, D);
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
}

void aom_d117_predictor_2x2_c(uint8_t *dst, ptrdiff_t stride,
                              const uint8_t *above, const uint8_t *left) {
  const int I = left[0];
  const int X = above[-1];
  const int A = above[0];
  const int B = above[1];
  DST(0, 0) = AVG2(X, A);
  DST(1, 0) = AVG2(A, B);
  DST(0, 1) = AVG3(I, X, A);
  DST(1, 1) = AVG3(X, A, B);
}

void aom_d135_predictor_2x2_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 X = above[-1];
  const int A = above[0];
  const int B = above[1];
  (void)stride;
  DST(0, 1) = AVG3(X, I, J);
  DST(1, 1) = DST(0, 0) = AVG3(A, X, I);
  DST(1, 0) = AVG3(B, A, X);
}

void aom_d153_predictor_2x2_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 X = above[-1];
  const int A = above[0];

  DST(0, 0) = AVG2(I, X);
  DST(0, 1) = AVG2(J, I);
  DST(1, 0) = AVG3(I, X, A);
  DST(1, 1) = AVG3(J, I, X);
}

Yaowu Xu's avatar
Yaowu Xu committed
461
void aom_d45e_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
462 463 464 465 466 467 468 469 470 471 472
                              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;
clang-format's avatar
clang-format committed
473 474 475
  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);
476
  DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
clang-format's avatar
clang-format committed
477 478 479
  DST(3, 1) = DST(2, 2) = DST(1, 3) = AVG3(E, F, G);
  DST(3, 2) = DST(2, 3) = AVG3(F, G, H);
  DST(3, 3) = AVG3(G, H, H);
480 481
}

Yaowu Xu's avatar
Yaowu Xu committed
482
void aom_d117_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
483 484 485 486 487 488 489 490 491 492 493 494
                              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);
clang-format's avatar
clang-format committed
495
  DST(3, 0) = AVG2(C, D);
496

clang-format's avatar
clang-format committed
497 498
  DST(0, 3) = AVG3(K, J, I);
  DST(0, 2) = AVG3(J, I, X);
499 500 501
  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);
clang-format's avatar
clang-format committed
502
  DST(3, 1) = AVG3(B, C, D);
503 504
}

Yaowu Xu's avatar
Yaowu Xu committed
505
void aom_d135_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
506 507 508 509 510 511 512 513 514 515 516
                              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;
clang-format's avatar
clang-format committed
517 518 519
  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);
520
  DST(3, 3) = DST(2, 2) = DST(1, 1) = DST(0, 0) = AVG3(A, X, I);
clang-format's avatar
clang-format committed
521 522 523
  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);
524 525
}

Yaowu Xu's avatar
Yaowu Xu committed
526
void aom_d153_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
527 528 529 530 531 532 533 534 535 536 537 538 539
                              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);
clang-format's avatar
clang-format committed
540
  DST(0, 3) = AVG2(L, K);
541

clang-format's avatar
clang-format committed
542 543
  DST(3, 0) = AVG3(A, B, C);
  DST(2, 0) = AVG3(X, A, B);
544 545 546
  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);
clang-format's avatar
clang-format committed
547
  DST(1, 3) = AVG3(L, K, J);
548 549
}

550
#if CONFIG_HIGHBITDEPTH
551
static INLINE void highbd_d207e_predictor(uint16_t *dst, ptrdiff_t stride,
552
                                          int bw, int bh, const uint16_t *above,
553 554
                                          const uint16_t *left, int bd) {
  int r, c;
clang-format's avatar
clang-format committed
555 556
  (void)above;
  (void)bd;
557

558 559
  for (r = 0; r < bh; ++r) {
    for (c = 0; c < bw; ++c) {
560 561
      dst[c] = c & 1 ? AVG3(left[(c >> 1) + r], left[(c >> 1) + r + 1],
                            left[(c >> 1) + r + 2])
clang-format's avatar
clang-format committed
562
                     : AVG2(left[(c >> 1) + r], left[(c >> 1) + r + 1]);
563 564 565 566 567
    }
    dst += stride;
  }
}

568
static INLINE void highbd_d63e_predictor(uint16_t *dst, ptrdiff_t stride,
569
                                         int bw, int bh, const uint16_t *above,
570
                                         const uint16_t *left, int bd) {
571
  int r, c;
clang-format's avatar
clang-format committed
572 573
  (void)left;
  (void)bd;
574 575
  for (r = 0; r < bh; ++r) {
    for (c = 0; c < bw; ++c) {
576 577
      dst[c] = r & 1 ? AVG3(above[(r >> 1) + c], above[(r >> 1) + c + 1],
                            above[(r >> 1) + c + 2])
clang-format's avatar
clang-format committed
578
                     : AVG2(above[(r >> 1) + c], above[(r >> 1) + c + 1]);
579 580 581 582 583
    }
    dst += stride;
  }
}

584
static INLINE void highbd_d45e_predictor(uint16_t *dst, ptrdiff_t stride,
585
                                         int bw, int bh, const uint16_t *above,
586 587
                                         const uint16_t *left, int bd) {
  int r, c;
clang-format's avatar
clang-format committed
588 589
  (void)left;
  (void)bd;
590 591
  for (r = 0; r < bh; ++r) {
    for (c = 0; c < bw; ++c) {
592
      dst[c] = AVG3(above[r + c], above[r + c + 1],
593
                    above[r + c + 1 + (r + c + 2 < bw + bh)]);
594 595 596 597 598
    }
    dst += stride;
  }
}

599
static INLINE void highbd_d117_predictor(uint16_t *dst, ptrdiff_t stride,
600
                                         int bw, int bh, const uint16_t *above,
601 602
                                         const uint16_t *left, int bd) {
  int r, c;
clang-format's avatar
clang-format committed
603
  (void)bd;
604 605

  // first row
606
  for (c = 0; c < bw; c++) dst[c] = AVG2(above[c - 1], above[c]);
607 608 609 610
  dst += stride;

  // second row
  dst[0] = AVG3(left[0], above[-1], above[0]);
611
  for (c = 1; c < bw; c++) dst[c] = AVG3(above[c - 2], above[c - 1], above[c]);
612 613 614 615
  dst += stride;

  // the rest of first col
  dst[0] = AVG3(above[-1], left[0], left[1]);
616
  for (r = 3; r < bh; ++r)
617 618 619
    dst[(r - 2) * stride] = AVG3(left[r - 3], left[r - 2], left[r - 1]);

  // the rest of the block
620 621
  for (r = 2; r < bh; ++r) {
    for (c = 1; c < bw; c++) dst[c] = dst[-2 * stride + c - 1];
622 623 624 625 626
    dst += stride;
  }
}

static INLINE void highbd_d135_predictor(uint16_t *dst, ptrdiff_t stride,
627
                                         int bw, int bh, const uint16_t *above,
628 629
                                         const uint16_t *left, int bd) {
  int r, c;
clang-format's avatar
clang-format committed
630
  (void)bd;
631
  dst[0] = AVG3(left[0], above[-1], above[0]);
632
  for (c = 1; c < bw; c++) dst[c] = AVG3(above[c - 2], above[c - 1], above[c]);
633 634

  dst[stride] = AVG3(above[-1], left[0], left[1]);
635
  for (r = 2; r < bh; ++r)
636 637 638
    dst[r * stride] = AVG3(left[r - 2], left[r - 1], left[r]);

  dst += stride;
639 640
  for (r = 1; r < bh; ++r) {
    for (c = 1; c < bw; c++) dst[c] = dst[-stride + c - 1];
641 642 643 644 645
    dst += stride;
  }
}

static INLINE void highbd_d153_predictor(uint16_t *dst, ptrdiff_t stride,
646
                                         int bw, int bh, const uint16_t *above,
647 648
                                         const uint16_t *left, int bd) {
  int r, c;
clang-format's avatar
clang-format committed
649
  (void)bd;
650
  dst[0] = AVG2(above[-1], left[0]);
651
  for (r = 1; r < bh; r++) dst[r * stride] = AVG2(left[r - 1], left[r]);
652 653 654 655
  dst++;

  dst[0] = AVG3(left[0], above[-1], above[0]);
  dst[stride] = AVG3(above[-1], left[0], left[1]);
656
  for (r = 2; r < bh; r++)
657 658 659
    dst[r * stride] = AVG3(left[r - 2], left[r - 1], left[r]);
  dst++;

660
  for (c = 0; c < bw - 2; c++)
661 662 663
    dst[c] = AVG3(above[c - 1], above[c], above[c + 1]);
  dst += stride;

664 665
  for (r = 1; r < bh; ++r) {
    for (c = 0; c < bw - 2; c++) dst[c] = dst[-stride + c - 2];
666 667 668 669
    dst += stride;
  }
}

670 671
static INLINE void highbd_v_predictor(uint16_t *dst, ptrdiff_t stride, int bw,
                                      int bh, const uint16_t *above,
672 673
                                      const uint16_t *left, int bd) {
  int r;
clang-format's avatar
clang-format committed
674 675
  (void)left;
  (void)bd;
676 677
  for (r = 0; r < bh; r++) {
    memcpy(dst, above, bw * sizeof(uint16_t));
678 679 680 681
    dst += stride;
  }
}

682 683
static INLINE void highbd_h_predictor(uint16_t *dst, ptrdiff_t stride, int bw,
                                      int bh, const uint16_t *above,
684 685
                                      const uint16_t *left, int bd) {
  int r;
clang-format's avatar
clang-format committed
686 687
  (void)above;
  (void)bd;
688 689
  for (r = 0; r < bh; r++) {
    aom_memset16(dst, left[r], bw);
690 691 692 693
    dst += stride;
  }
}

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
void aom_highbd_d207_predictor_2x2_c(uint16_t *dst, ptrdiff_t stride,
                                     const uint16_t *above,
                                     const uint16_t *left, int bd) {
  const int I = left[0];
  const int J = left[1];
  const int K = left[2];
  const int L = left[3];
  (void)above;
  (void)bd;
  DST(0, 0) = AVG2(I, J);
  DST(0, 1) = AVG2(J, K);
  DST(1, 0) = AVG3(I, J, K);
  DST(1, 1) = AVG3(J, K, L);
}

void aom_highbd_d63_predictor_2x2_c(uint16_t *dst, ptrdiff_t stride,
                                    const uint16_t *above, const uint16_t *left,
                                    int bd) {
  const int A = above[0];
  const int B = above[1];
  const int C = above[2];
  const int D = above[3];
  (void)left;
  (void)bd;
  DST(0, 0) = AVG2(A, B);
  DST(1, 0) = AVG2(B, C);
  DST(0, 1) = AVG3(A, B, C);
  DST(1, 1) = AVG3(B, C, D);
}

void aom_highbd_d45e_predictor_2x2_c(uint16_t *dst, ptrdiff_t stride,
                                     const uint16_t *above,
                                     const uint16_t *left, int bd) {
  const int A = above[0];
  const int B = above[1];
  const int C = above[2];
  const int D = above[3];
  (void)stride;
  (void)left;
  (void)bd;
  DST(0, 0) = AVG3(A, B, C);
  DST(1, 0) = DST(0, 1) = AVG3(B, C, D);
  DST(1, 1) = AVG3(C, D, D);
}

void aom_highbd_d117_predictor_2x2_c(uint16_t *dst, ptrdiff_t stride,
                                     const uint16_t *above,
                                     const uint16_t *left, int bd) {
  const int I = left[0];
  const int X = above[-1];
  const int A = above[0];
  const int B = above[1];
  (void)bd;
  DST(0, 0) = AVG2(X, A);
  DST(1, 0) = AVG2(A, B);
  DST(0, 1) = AVG3(I, X, A);
  DST(1, 1) = AVG3(X, A, B);
}

void aom_highbd_d135_predictor_2x2_c(uint16_t *dst, ptrdiff_t stride,
                                     const uint16_t *above,
                                     const uint16_t *left, int bd) {
  const int I = left[0];
  const int J = left[1];
  const int X = above[-1];
  const int A = above[0];
  const int B = above[1];
  (void)bd;
  DST(0, 1) = AVG3(X, I, J);
  DST(1, 1) = DST(0, 0) = AVG3(A, X, I);
  DST(1, 0) = AVG3(B, A, X);
}

void aom_highbd_d153_predictor_2x2_c(uint16_t *dst, ptrdiff_t stride,
                                     const uint16_t *above,
                                     const uint16_t *left, int bd) {
  const int I = left[0];
  const int J = left[1];
  const int X = above[-1];
  const int A = above[0];
  (void)bd;
  DST(0, 0) = AVG2(I, X);
  DST(0, 1) = AVG2(J, I);
  DST(1, 0) = AVG3(I, X, A);
  DST(1, 1) = AVG3(J, I, X);
}

Urvang Joshi's avatar
Urvang Joshi committed
781
static INLINE void highbd_paeth_predictor(uint16_t *dst, ptrdiff_t stride,
782
                                          int bw, int bh, const uint16_t *above,
Urvang Joshi's avatar
Urvang Joshi committed
783 784 785 786 787
                                          const uint16_t *left, int bd) {
  int r, c;
  const uint16_t ytop_left = above[-1];
  (void)bd;

788 789
  for (r = 0; r < bh; r++) {
    for (c = 0; c < bw; c++)
Urvang Joshi's avatar
Urvang Joshi committed
790 791 792 793 794
      dst[c] = paeth_predictor_single(left[r], above[c], ytop_left);
    dst += stride;
  }
}

795
static INLINE void highbd_smooth_predictor(uint16_t *dst, ptrdiff_t stride,
796 797
                                           int bw, int bh,
                                           const uint16_t *above,
798
                                           const uint16_t *left, int bd) {
799 800 801 802
  const uint16_t below_pred = left[bh - 1];   // estimated by bottom-left pixel
  const uint16_t right_pred = above[bw - 1];  // estimated by top-right pixel
  const uint8_t *const sm_weights_w = sm_weight_arrays + bw;
  const uint8_t *const sm_weights_h = sm_weight_arrays + bh;
803 804
  // scale = 2 * 2^sm_weight_log2_scale
  const int log2_scale = 1 + sm_weight_log2_scale;
805
  const uint16_t scale = (1 << sm_weight_log2_scale);
806 807
  sm_weights_sanity_checks(sm_weights_w, sm_weights_h, scale,
                           log2_scale + sizeof(*dst));
808
  int r;
809
  for (r = 0; r < bh; ++r) {
810
    int c;
811
    for (c = 0; c < bw; ++c) {
812
      const uint16_t pixels[] = { above[c], below_pred, left[r], right_pred };
813 814
      const uint8_t weights[] = { sm_weights_h[r], scale - sm_weights_h[r],
                                  sm_weights_w[c], scale - sm_weights_w[c] };
815
      uint32_t this_pred = 0;
816
      int i;
817
      assert(scale >= sm_weights_h[r] && scale >= sm_weights_w[c]);
818 819 820
      for (i = 0; i < 4; ++i) {
        this_pred += weights[i] * pixels[i];
      }
821
      dst[c] = clip_pixel_highbd(divide_round(this_pred, log2_scale), bd);
822 823 824 825 826
    }
    dst += stride;
  }
}

827
#if CONFIG_SMOOTH_HV
Urvang Joshi's avatar
Urvang Joshi committed
828
static INLINE void highbd_smooth_v_predictor(uint16_t *dst, ptrdiff_t stride,
829 830
                                             int bw, int bh,
                                             const uint16_t *above,
Urvang Joshi's avatar
Urvang Joshi committed
831
                                             const uint16_t *left, int bd) {
832 833
  const uint16_t below_pred = left[bh - 1];  // estimated by bottom-left pixel
  const uint8_t *const sm_weights = sm_weight_arrays + bh;
Urvang Joshi's avatar
Urvang Joshi committed
834 835 836
  // scale = 2^sm_weight_log2_scale
  const int log2_scale = sm_weight_log2_scale;
  const uint16_t scale = (1 << sm_weight_log2_scale);
837 838
  sm_weights_sanity_checks(sm_weights, sm_weights, scale,
                           log2_scale + sizeof(*dst));
Urvang Joshi's avatar
Urvang Joshi committed
839 840

  int r;
841
  for (r = 0; r < bh; r++) {
Urvang Joshi's avatar
Urvang Joshi committed
842
    int c;
843
    for (c = 0; c < bw; ++c) {
Urvang Joshi's avatar
Urvang Joshi committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
      const uint16_t pixels[] = { above[c], below_pred };
      const uint8_t weights[] = { sm_weights[r], scale - sm_weights[r] };
      uint32_t this_pred = 0;
      assert(scale >= sm_weights[r]);
      int i;
      for (i = 0; i < 2; ++i) {
        this_pred += weights[i] * pixels[i];
      }
      dst[c] = clip_pixel_highbd(divide_round(this_pred, log2_scale), bd);
    }
    dst += stride;
  }
}

static INLINE void highbd_smooth_h_predictor(uint16_t *dst, ptrdiff_t stride,
859 860
                                             int bw, int bh,
                                             const uint16_t *above,
Urvang Joshi's avatar
Urvang Joshi committed
861
                                             const uint16_t *left, int bd) {
862 863
  const uint16_t right_pred = above[bw - 1];  // estimated by top-right pixel
  const uint8_t *const sm_weights = sm_weight_arrays + bw;
Urvang Joshi's avatar
Urvang Joshi committed
864 865 866
  // scale = 2^sm_weight_log2_scale
  const int log2_scale = sm_weight_log2_scale;
  const uint16_t scale = (1 << sm_weight_log2_scale);
867 868
  sm_weights_sanity_checks(sm_weights, sm_weights, scale,
                           log2_scale + sizeof(*dst));
Urvang Joshi's avatar
Urvang Joshi committed
869 870

  int r;
871
  for (r = 0; r < bh; r++) {
Urvang Joshi's avatar
Urvang Joshi committed
872
    int c;
873
    for (c = 0; c < bw; ++c) {
Urvang Joshi's avatar
Urvang Joshi committed
874 875 876 877 878 879 880 881 882 883 884 885 886
      const uint16_t pixels[] = { left[r], right_pred };
      const uint8_t weights[] = { sm_weights[c], scale - sm_weights[c] };
      uint32_t this_pred = 0;
      assert(scale >= sm_weights[c]);
      int i;
      for (i = 0; i < 2; ++i) {
        this_pred += weights[i] * pixels[i];
      }
      dst[c] = clip_pixel_highbd(divide_round(this_pred, log2_scale), bd);
    }
    dst += stride;
  }
}
Urvang Joshi's avatar
Urvang Joshi committed
887
#endif  // CONFIG_SMOOTH_HV
888 889

static INLINE void highbd_dc_128_predictor(uint16_t *dst, ptrdiff_t stride,
890 891
                                           int bw, int bh,
                                           const uint16_t *above,
892 893
                                           const uint16_t *left, int bd) {
  int r;
clang-format's avatar
clang-format committed
894 895
  (void)above;
  (void)left;
896

897 898
  for (r = 0; r < bh; r++) {
    aom_memset16(dst, 128 << (bd - 8), bw);
899 900 901 902 903
    dst += stride;
  }
}

static INLINE void highbd_dc_left_predictor(uint16_t *dst, ptrdiff_t stride,
904 905
                                            int bw, int bh,
                                            const uint16_t *above,
906 907
                                            const uint16_t *left, int bd) {
  int i, r, expected_dc, sum = 0;
clang-format's avatar
clang-format committed
908 909
  (void)above;
  (void)bd;
910

911 912
  for (i = 0; i < bh; i++) sum += left[i];
  expected_dc = (sum + (bh >> 1)) / bh;
913

914 915
  for (r = 0; r < bh; r++) {
    aom_memset16(dst, expected_dc, bw);
916 917 918 919 920
    dst += stride;
  }
}

static INLINE void highbd_dc_top_predictor(uint16_t *dst, ptrdiff_t stride,
921 922
                                           int bw, int bh,
                                           const uint16_t *above,
923 924
                                           const uint16_t *left, int bd) {
  int i, r, expected_dc, sum = 0;
clang-format's avatar
clang-format committed
925 926
  (void)left;
  (void)bd;
927

928 929
  for (i = 0; i < bw; i++) sum += above[i];
  expected_dc = (sum + (bw >> 1)) / bw;
930

931 932
  for (r = 0; r < bh; r++) {
    aom_memset16(dst, expected_dc, bw);
933 934 935 936
    dst += stride;
  }
}

937 938
static INLINE void highbd_dc_predictor(uint16_t *dst, ptrdiff_t stride, int bw,
                                       int bh, const uint16_t *above,
hui su's avatar