Merge "Dynamic resize for real-time: reference scaling."
[aom.git] / vp9 / common / vp9_reconintra.c
blobf969ff1a8fd136bc0f10aa2269ffcb6f14ee12d3
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * 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.
9 */
11 #include "./vpx_config.h"
12 #include "./vp9_rtcd.h"
14 #include "vpx_mem/vpx_mem.h"
15 #include "vpx_ports/mem.h"
16 #include "vpx_ports/vpx_once.h"
18 #include "vp9/common/vp9_reconintra.h"
19 #include "vp9/common/vp9_onyxc_int.h"
21 const TX_TYPE intra_mode_to_tx_type_lookup[INTRA_MODES] = {
22 DCT_DCT, // DC
23 ADST_DCT, // V
24 DCT_ADST, // H
25 DCT_DCT, // D45
26 ADST_ADST, // D135
27 ADST_DCT, // D117
28 DCT_ADST, // D153
29 DCT_ADST, // D207
30 ADST_DCT, // D63
31 ADST_ADST, // TM
34 enum {
35 NEED_LEFT = 1 << 1,
36 NEED_ABOVE = 1 << 2,
37 NEED_ABOVERIGHT = 1 << 3,
40 static const uint8_t extend_modes[INTRA_MODES] = {
41 NEED_ABOVE | NEED_LEFT, // DC
42 NEED_ABOVE, // V
43 NEED_LEFT, // H
44 NEED_ABOVERIGHT, // D45
45 NEED_LEFT | NEED_ABOVE, // D135
46 NEED_LEFT | NEED_ABOVE, // D117
47 NEED_LEFT | NEED_ABOVE, // D153
48 NEED_LEFT, // D207
49 NEED_ABOVERIGHT, // D63
50 NEED_LEFT | NEED_ABOVE, // TM
53 // This serves as a wrapper function, so that all the prediction functions
54 // can be unified and accessed as a pointer array. Note that the boundary
55 // above and left are not necessarily used all the time.
56 #define intra_pred_sized(type, size) \
57 void vp9_##type##_predictor_##size##x##size##_c(uint8_t *dst, \
58 ptrdiff_t stride, \
59 const uint8_t *above, \
60 const uint8_t *left) { \
61 type##_predictor(dst, stride, size, above, left); \
64 #if CONFIG_VP9_HIGHBITDEPTH
65 #define intra_pred_highbd_sized(type, size) \
66 void vp9_highbd_##type##_predictor_##size##x##size##_c( \
67 uint16_t *dst, ptrdiff_t stride, const uint16_t *above, \
68 const uint16_t *left, int bd) { \
69 highbd_##type##_predictor(dst, stride, size, above, left, bd); \
72 #define intra_pred_allsizes(type) \
73 intra_pred_sized(type, 4) \
74 intra_pred_sized(type, 8) \
75 intra_pred_sized(type, 16) \
76 intra_pred_sized(type, 32) \
77 intra_pred_highbd_sized(type, 4) \
78 intra_pred_highbd_sized(type, 8) \
79 intra_pred_highbd_sized(type, 16) \
80 intra_pred_highbd_sized(type, 32)
82 #define intra_pred_no_4x4(type) \
83 intra_pred_sized(type, 8) \
84 intra_pred_sized(type, 16) \
85 intra_pred_sized(type, 32) \
86 intra_pred_highbd_sized(type, 4) \
87 intra_pred_highbd_sized(type, 8) \
88 intra_pred_highbd_sized(type, 16) \
89 intra_pred_highbd_sized(type, 32)
91 #else
93 #define intra_pred_allsizes(type) \
94 intra_pred_sized(type, 4) \
95 intra_pred_sized(type, 8) \
96 intra_pred_sized(type, 16) \
97 intra_pred_sized(type, 32)
99 #define intra_pred_no_4x4(type) \
100 intra_pred_sized(type, 8) \
101 intra_pred_sized(type, 16) \
102 intra_pred_sized(type, 32)
103 #endif // CONFIG_VP9_HIGHBITDEPTH
105 #define DST(x, y) dst[(x) + (y) * stride]
106 #define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)
107 #define AVG2(a, b) (((a) + (b) + 1) >> 1)
109 #if CONFIG_VP9_HIGHBITDEPTH
110 static INLINE void highbd_d207_predictor(uint16_t *dst, ptrdiff_t stride,
111 int bs, const uint16_t *above,
112 const uint16_t *left, int bd) {
113 int r, c;
114 (void) above;
115 (void) bd;
117 // First column.
118 for (r = 0; r < bs - 1; ++r) {
119 dst[r * stride] = AVG2(left[r], left[r + 1]);
121 dst[(bs - 1) * stride] = left[bs - 1];
122 dst++;
124 // Second column.
125 for (r = 0; r < bs - 2; ++r) {
126 dst[r * stride] = AVG3(left[r], left[r + 1], left[r + 2]);
128 dst[(bs - 2) * stride] = AVG3(left[bs - 2], left[bs - 1], left[bs - 1]);
129 dst[(bs - 1) * stride] = left[bs - 1];
130 dst++;
132 // Rest of last row.
133 for (c = 0; c < bs - 2; ++c)
134 dst[(bs - 1) * stride + c] = left[bs - 1];
136 for (r = bs - 2; r >= 0; --r) {
137 for (c = 0; c < bs - 2; ++c)
138 dst[r * stride + c] = dst[(r + 1) * stride + c - 2];
142 static INLINE void highbd_d63_predictor(uint16_t *dst, ptrdiff_t stride,
143 int bs, const uint16_t *above,
144 const uint16_t *left, int bd) {
145 int r, c;
146 (void) left;
147 (void) bd;
148 for (r = 0; r < bs; ++r) {
149 for (c = 0; c < bs; ++c) {
150 dst[c] = r & 1 ? AVG3(above[(r >> 1) + c], above[(r >> 1) + c + 1],
151 above[(r >> 1) + c + 2])
152 : AVG2(above[(r >> 1) + c], above[(r >> 1) + c + 1]);
154 dst += stride;
158 static INLINE void highbd_d45_predictor(uint16_t *dst, ptrdiff_t stride, int bs,
159 const uint16_t *above,
160 const uint16_t *left, int bd) {
161 int r, c;
162 (void) left;
163 (void) bd;
164 for (r = 0; r < bs; ++r) {
165 for (c = 0; c < bs; ++c) {
166 dst[c] = r + c + 2 < bs * 2 ? AVG3(above[r + c], above[r + c + 1],
167 above[r + c + 2])
168 : above[bs * 2 - 1];
170 dst += stride;
174 static INLINE void highbd_d117_predictor(uint16_t *dst, ptrdiff_t stride,
175 int bs, const uint16_t *above,
176 const uint16_t *left, int bd) {
177 int r, c;
178 (void) bd;
180 // first row
181 for (c = 0; c < bs; c++)
182 dst[c] = AVG2(above[c - 1], above[c]);
183 dst += stride;
185 // 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 dst += stride;
191 // 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]);
196 // the rest of the block
197 for (r = 2; r < bs; ++r) {
198 for (c = 1; c < bs; c++)
199 dst[c] = dst[-2 * stride + c - 1];
200 dst += stride;
204 static INLINE void highbd_d135_predictor(uint16_t *dst, ptrdiff_t stride,
205 int bs, const uint16_t *above,
206 const uint16_t *left, int bd) {
207 int r, c;
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]);
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]);
217 dst += stride;
218 for (r = 1; r < bs; ++r) {
219 for (c = 1; c < bs; c++)
220 dst[c] = dst[-stride + c - 1];
221 dst += stride;
225 static INLINE void highbd_d153_predictor(uint16_t *dst, ptrdiff_t stride,
226 int bs, const uint16_t *above,
227 const uint16_t *left, int bd) {
228 int r, c;
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 dst++;
235 dst[0] = AVG3(left[0], above[-1], above[0]);
236 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 dst++;
241 for (c = 0; c < bs - 2; c++)
242 dst[c] = AVG3(above[c - 1], above[c], above[c + 1]);
243 dst += stride;
245 for (r = 1; r < bs; ++r) {
246 for (c = 0; c < bs - 2; c++)
247 dst[c] = dst[-stride + c - 2];
248 dst += stride;
252 static INLINE void highbd_v_predictor(uint16_t *dst, ptrdiff_t stride,
253 int bs, const uint16_t *above,
254 const uint16_t *left, int bd) {
255 int r;
256 (void) left;
257 (void) bd;
258 for (r = 0; r < bs; r++) {
259 memcpy(dst, above, bs * sizeof(uint16_t));
260 dst += stride;
264 static INLINE void highbd_h_predictor(uint16_t *dst, ptrdiff_t stride,
265 int bs, const uint16_t *above,
266 const uint16_t *left, int bd) {
267 int r;
268 (void) above;
269 (void) bd;
270 for (r = 0; r < bs; r++) {
271 vpx_memset16(dst, left[r], bs);
272 dst += stride;
276 static INLINE void highbd_tm_predictor(uint16_t *dst, ptrdiff_t stride,
277 int bs, const uint16_t *above,
278 const uint16_t *left, int bd) {
279 int r, c;
280 int ytop_left = above[-1];
281 (void) bd;
283 for (r = 0; r < bs; r++) {
284 for (c = 0; c < bs; c++)
285 dst[c] = clip_pixel_highbd(left[r] + above[c] - ytop_left, bd);
286 dst += stride;
290 static INLINE void highbd_dc_128_predictor(uint16_t *dst, ptrdiff_t stride,
291 int bs, const uint16_t *above,
292 const uint16_t *left, int bd) {
293 int r;
294 (void) above;
295 (void) left;
297 for (r = 0; r < bs; r++) {
298 vpx_memset16(dst, 128 << (bd - 8), bs);
299 dst += stride;
303 static INLINE void highbd_dc_left_predictor(uint16_t *dst, ptrdiff_t stride,
304 int bs, const uint16_t *above,
305 const uint16_t *left, int bd) {
306 int i, r, expected_dc, sum = 0;
307 (void) above;
308 (void) bd;
310 for (i = 0; i < bs; i++)
311 sum += left[i];
312 expected_dc = (sum + (bs >> 1)) / bs;
314 for (r = 0; r < bs; r++) {
315 vpx_memset16(dst, expected_dc, bs);
316 dst += stride;
320 static INLINE void highbd_dc_top_predictor(uint16_t *dst, ptrdiff_t stride,
321 int bs, const uint16_t *above,
322 const uint16_t *left, int bd) {
323 int i, r, expected_dc, sum = 0;
324 (void) left;
325 (void) bd;
327 for (i = 0; i < bs; i++)
328 sum += above[i];
329 expected_dc = (sum + (bs >> 1)) / bs;
331 for (r = 0; r < bs; r++) {
332 vpx_memset16(dst, expected_dc, bs);
333 dst += stride;
337 static INLINE void highbd_dc_predictor(uint16_t *dst, ptrdiff_t stride,
338 int bs, const uint16_t *above,
339 const uint16_t *left, int bd) {
340 int i, r, expected_dc, sum = 0;
341 const int count = 2 * bs;
342 (void) bd;
344 for (i = 0; i < bs; i++) {
345 sum += above[i];
346 sum += left[i];
349 expected_dc = (sum + (count >> 1)) / count;
351 for (r = 0; r < bs; r++) {
352 vpx_memset16(dst, expected_dc, bs);
353 dst += stride;
356 #endif // CONFIG_VP9_HIGHBITDEPTH
358 void vp9_d207_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
359 const uint8_t *above, const uint8_t *left) {
360 const int I = left[0];
361 const int J = left[1];
362 const int K = left[2];
363 const int L = left[3];
364 (void)above;
365 DST(0, 0) = AVG2(I, J);
366 DST(2, 0) = DST(0, 1) = AVG2(J, K);
367 DST(2, 1) = DST(0, 2) = AVG2(K, L);
368 DST(1, 0) = AVG3(I, J, K);
369 DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
370 DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
371 DST(3, 2) = DST(2, 2) =
372 DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
375 static INLINE void d207_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
376 const uint8_t *above, const uint8_t *left) {
377 int r, c;
378 (void) above;
379 // first column
380 for (r = 0; r < bs - 1; ++r)
381 dst[r * stride] = AVG2(left[r], left[r + 1]);
382 dst[(bs - 1) * stride] = left[bs - 1];
383 dst++;
385 // second column
386 for (r = 0; r < bs - 2; ++r)
387 dst[r * stride] = AVG3(left[r], left[r + 1], left[r + 2]);
388 dst[(bs - 2) * stride] = AVG3(left[bs - 2], left[bs - 1], left[bs - 1]);
389 dst[(bs - 1) * stride] = left[bs - 1];
390 dst++;
392 // rest of last row
393 for (c = 0; c < bs - 2; ++c)
394 dst[(bs - 1) * stride + c] = left[bs - 1];
396 for (r = bs - 2; r >= 0; --r)
397 for (c = 0; c < bs - 2; ++c)
398 dst[r * stride + c] = dst[(r + 1) * stride + c - 2];
400 intra_pred_no_4x4(d207)
402 void vp9_d63_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
403 const uint8_t *above, const uint8_t *left) {
404 const int A = above[0];
405 const int B = above[1];
406 const int C = above[2];
407 const int D = above[3];
408 const int E = above[4];
409 const int F = above[5];
410 const int G = above[6];
411 (void)left;
412 DST(0, 0) = AVG2(A, B);
413 DST(1, 0) = DST(0, 2) = AVG2(B, C);
414 DST(2, 0) = DST(1, 2) = AVG2(C, D);
415 DST(3, 0) = DST(2, 2) = AVG2(D, E);
416 DST(3, 2) = AVG2(E, F); // differs from vp8
418 DST(0, 1) = AVG3(A, B, C);
419 DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
420 DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
421 DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
422 DST(3, 3) = AVG3(E, F, G); // differs from vp8
425 static INLINE void d63_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
426 const uint8_t *above, const uint8_t *left) {
427 int r, c;
428 int size;
429 (void)left;
430 for (c = 0; c < bs; ++c) {
431 dst[c] = AVG2(above[c], above[c + 1]);
432 dst[stride + c] = AVG3(above[c], above[c + 1], above[c + 2]);
434 for (r = 2, size = bs - 2; r < bs; r += 2, --size) {
435 memcpy(dst + (r + 0) * stride, dst + (r >> 1), size);
436 memset(dst + (r + 0) * stride + size, above[bs - 1], bs - size);
437 memcpy(dst + (r + 1) * stride, dst + stride + (r >> 1), size);
438 memset(dst + (r + 1) * stride + size, above[bs - 1], bs - size);
441 intra_pred_no_4x4(d63)
443 void vp9_d45_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
444 const uint8_t *above, const uint8_t *left) {
445 const int A = above[0];
446 const int B = above[1];
447 const int C = above[2];
448 const int D = above[3];
449 const int E = above[4];
450 const int F = above[5];
451 const int G = above[6];
452 const int H = above[7];
453 (void)stride;
454 (void)left;
455 DST(0, 0) = AVG3(A, B, C);
456 DST(1, 0) = DST(0, 1) = AVG3(B, C, D);
457 DST(2, 0) = DST(1, 1) = DST(0, 2) = AVG3(C, D, E);
458 DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
459 DST(3, 1) = DST(2, 2) = DST(1, 3) = AVG3(E, F, G);
460 DST(3, 2) = DST(2, 3) = AVG3(F, G, H);
461 DST(3, 3) = H; // differs from vp8
464 static INLINE void d45_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
465 const uint8_t *above, const uint8_t *left) {
466 const uint8_t above_right = above[bs - 1];
467 const uint8_t *const dst_row0 = dst;
468 int x, size;
469 (void)left;
471 for (x = 0; x < bs - 1; ++x) {
472 dst[x] = AVG3(above[x], above[x + 1], above[x + 2]);
474 dst[bs - 1] = above_right;
475 dst += stride;
476 for (x = 1, size = bs - 2; x < bs; ++x, --size) {
477 memcpy(dst, dst_row0 + x, size);
478 memset(dst + size, above_right, x + 1);
479 dst += stride;
482 intra_pred_no_4x4(d45)
484 void vp9_d117_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
485 const uint8_t *above, const uint8_t *left) {
486 const int I = left[0];
487 const int J = left[1];
488 const int K = left[2];
489 const int X = above[-1];
490 const int A = above[0];
491 const int B = above[1];
492 const int C = above[2];
493 const int D = above[3];
494 DST(0, 0) = DST(1, 2) = AVG2(X, A);
495 DST(1, 0) = DST(2, 2) = AVG2(A, B);
496 DST(2, 0) = DST(3, 2) = AVG2(B, C);
497 DST(3, 0) = AVG2(C, D);
499 DST(0, 3) = AVG3(K, J, I);
500 DST(0, 2) = AVG3(J, I, X);
501 DST(0, 1) = DST(1, 3) = AVG3(I, X, A);
502 DST(1, 1) = DST(2, 3) = AVG3(X, A, B);
503 DST(2, 1) = DST(3, 3) = AVG3(A, B, C);
504 DST(3, 1) = AVG3(B, C, D);
507 static INLINE void d117_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
508 const uint8_t *above, const uint8_t *left) {
509 int r, c;
511 // first row
512 for (c = 0; c < bs; c++)
513 dst[c] = AVG2(above[c - 1], above[c]);
514 dst += stride;
516 // second row
517 dst[0] = AVG3(left[0], above[-1], above[0]);
518 for (c = 1; c < bs; c++)
519 dst[c] = AVG3(above[c - 2], above[c - 1], above[c]);
520 dst += stride;
522 // the rest of first col
523 dst[0] = AVG3(above[-1], left[0], left[1]);
524 for (r = 3; r < bs; ++r)
525 dst[(r - 2) * stride] = AVG3(left[r - 3], left[r - 2], left[r - 1]);
527 // the rest of the block
528 for (r = 2; r < bs; ++r) {
529 for (c = 1; c < bs; c++)
530 dst[c] = dst[-2 * stride + c - 1];
531 dst += stride;
534 intra_pred_no_4x4(d117)
536 void vp9_d135_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
537 const uint8_t *above, const uint8_t *left) {
538 const int I = left[0];
539 const int J = left[1];
540 const int K = left[2];
541 const int L = left[3];
542 const int X = above[-1];
543 const int A = above[0];
544 const int B = above[1];
545 const int C = above[2];
546 const int D = above[3];
547 (void)stride;
548 DST(0, 3) = AVG3(J, K, L);
549 DST(1, 3) = DST(0, 2) = AVG3(I, J, K);
550 DST(2, 3) = DST(1, 2) = DST(0, 1) = AVG3(X, I, J);
551 DST(3, 3) = DST(2, 2) = DST(1, 1) = DST(0, 0) = AVG3(A, X, I);
552 DST(3, 2) = DST(2, 1) = DST(1, 0) = AVG3(B, A, X);
553 DST(3, 1) = DST(2, 0) = AVG3(C, B, A);
554 DST(3, 0) = AVG3(D, C, B);
557 static INLINE void d135_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
558 const uint8_t *above, const uint8_t *left) {
559 int r, c;
560 dst[0] = AVG3(left[0], above[-1], above[0]);
561 for (c = 1; c < bs; c++)
562 dst[c] = AVG3(above[c - 2], above[c - 1], above[c]);
564 dst[stride] = AVG3(above[-1], left[0], left[1]);
565 for (r = 2; r < bs; ++r)
566 dst[r * stride] = AVG3(left[r - 2], left[r - 1], left[r]);
568 dst += stride;
569 for (r = 1; r < bs; ++r) {
570 for (c = 1; c < bs; c++)
571 dst[c] = dst[-stride + c - 1];
572 dst += stride;
575 intra_pred_no_4x4(d135)
577 void vp9_d153_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
578 const uint8_t *above, const uint8_t *left) {
579 const int I = left[0];
580 const int J = left[1];
581 const int K = left[2];
582 const int L = left[3];
583 const int X = above[-1];
584 const int A = above[0];
585 const int B = above[1];
586 const int C = above[2];
588 DST(0, 0) = DST(2, 1) = AVG2(I, X);
589 DST(0, 1) = DST(2, 2) = AVG2(J, I);
590 DST(0, 2) = DST(2, 3) = AVG2(K, J);
591 DST(0, 3) = AVG2(L, K);
593 DST(3, 0) = AVG3(A, B, C);
594 DST(2, 0) = AVG3(X, A, B);
595 DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
596 DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
597 DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
598 DST(1, 3) = AVG3(L, K, J);
601 static INLINE void d153_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
602 const uint8_t *above, const uint8_t *left) {
603 int r, c;
604 dst[0] = AVG2(above[-1], left[0]);
605 for (r = 1; r < bs; r++)
606 dst[r * stride] = AVG2(left[r - 1], left[r]);
607 dst++;
609 dst[0] = AVG3(left[0], above[-1], above[0]);
610 dst[stride] = AVG3(above[-1], left[0], left[1]);
611 for (r = 2; r < bs; r++)
612 dst[r * stride] = AVG3(left[r - 2], left[r - 1], left[r]);
613 dst++;
615 for (c = 0; c < bs - 2; c++)
616 dst[c] = AVG3(above[c - 1], above[c], above[c + 1]);
617 dst += stride;
619 for (r = 1; r < bs; ++r) {
620 for (c = 0; c < bs - 2; c++)
621 dst[c] = dst[-stride + c - 2];
622 dst += stride;
625 intra_pred_no_4x4(d153)
627 static INLINE void v_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
628 const uint8_t *above, const uint8_t *left) {
629 int r;
630 (void) left;
632 for (r = 0; r < bs; r++) {
633 memcpy(dst, above, bs);
634 dst += stride;
637 intra_pred_allsizes(v)
639 static INLINE void h_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
640 const uint8_t *above, const uint8_t *left) {
641 int r;
642 (void) above;
644 for (r = 0; r < bs; r++) {
645 memset(dst, left[r], bs);
646 dst += stride;
649 intra_pred_allsizes(h)
651 static INLINE void tm_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
652 const uint8_t *above, const uint8_t *left) {
653 int r, c;
654 int ytop_left = above[-1];
656 for (r = 0; r < bs; r++) {
657 for (c = 0; c < bs; c++)
658 dst[c] = clip_pixel(left[r] + above[c] - ytop_left);
659 dst += stride;
662 intra_pred_allsizes(tm)
664 static INLINE void dc_128_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
665 const uint8_t *above, const uint8_t *left) {
666 int r;
667 (void) above;
668 (void) left;
670 for (r = 0; r < bs; r++) {
671 memset(dst, 128, bs);
672 dst += stride;
675 intra_pred_allsizes(dc_128)
677 static INLINE void dc_left_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
678 const uint8_t *above,
679 const uint8_t *left) {
680 int i, r, expected_dc, sum = 0;
681 (void) above;
683 for (i = 0; i < bs; i++)
684 sum += left[i];
685 expected_dc = (sum + (bs >> 1)) / bs;
687 for (r = 0; r < bs; r++) {
688 memset(dst, expected_dc, bs);
689 dst += stride;
692 intra_pred_allsizes(dc_left)
694 static INLINE void dc_top_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
695 const uint8_t *above, const uint8_t *left) {
696 int i, r, expected_dc, sum = 0;
697 (void) left;
699 for (i = 0; i < bs; i++)
700 sum += above[i];
701 expected_dc = (sum + (bs >> 1)) / bs;
703 for (r = 0; r < bs; r++) {
704 memset(dst, expected_dc, bs);
705 dst += stride;
708 intra_pred_allsizes(dc_top)
710 static INLINE void dc_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
711 const uint8_t *above, const uint8_t *left) {
712 int i, r, expected_dc, sum = 0;
713 const int count = 2 * bs;
715 for (i = 0; i < bs; i++) {
716 sum += above[i];
717 sum += left[i];
720 expected_dc = (sum + (count >> 1)) / count;
722 for (r = 0; r < bs; r++) {
723 memset(dst, expected_dc, bs);
724 dst += stride;
727 intra_pred_allsizes(dc)
728 #undef intra_pred_allsizes
730 typedef void (*intra_pred_fn)(uint8_t *dst, ptrdiff_t stride,
731 const uint8_t *above, const uint8_t *left);
733 static intra_pred_fn pred[INTRA_MODES][TX_SIZES];
734 static intra_pred_fn dc_pred[2][2][TX_SIZES];
736 #if CONFIG_VP9_HIGHBITDEPTH
737 typedef void (*intra_high_pred_fn)(uint16_t *dst, ptrdiff_t stride,
738 const uint16_t *above, const uint16_t *left,
739 int bd);
740 static intra_high_pred_fn pred_high[INTRA_MODES][4];
741 static intra_high_pred_fn dc_pred_high[2][2][4];
742 #endif // CONFIG_VP9_HIGHBITDEPTH
744 static void vp9_init_intra_predictors_internal(void) {
745 #define INIT_ALL_SIZES(p, type) \
746 p[TX_4X4] = vp9_##type##_predictor_4x4; \
747 p[TX_8X8] = vp9_##type##_predictor_8x8; \
748 p[TX_16X16] = vp9_##type##_predictor_16x16; \
749 p[TX_32X32] = vp9_##type##_predictor_32x32
751 INIT_ALL_SIZES(pred[V_PRED], v);
752 INIT_ALL_SIZES(pred[H_PRED], h);
753 INIT_ALL_SIZES(pred[D207_PRED], d207);
754 INIT_ALL_SIZES(pred[D45_PRED], d45);
755 INIT_ALL_SIZES(pred[D63_PRED], d63);
756 INIT_ALL_SIZES(pred[D117_PRED], d117);
757 INIT_ALL_SIZES(pred[D135_PRED], d135);
758 INIT_ALL_SIZES(pred[D153_PRED], d153);
759 INIT_ALL_SIZES(pred[TM_PRED], tm);
761 INIT_ALL_SIZES(dc_pred[0][0], dc_128);
762 INIT_ALL_SIZES(dc_pred[0][1], dc_top);
763 INIT_ALL_SIZES(dc_pred[1][0], dc_left);
764 INIT_ALL_SIZES(dc_pred[1][1], dc);
766 #if CONFIG_VP9_HIGHBITDEPTH
767 INIT_ALL_SIZES(pred_high[V_PRED], highbd_v);
768 INIT_ALL_SIZES(pred_high[H_PRED], highbd_h);
769 INIT_ALL_SIZES(pred_high[D207_PRED], highbd_d207);
770 INIT_ALL_SIZES(pred_high[D45_PRED], highbd_d45);
771 INIT_ALL_SIZES(pred_high[D63_PRED], highbd_d63);
772 INIT_ALL_SIZES(pred_high[D117_PRED], highbd_d117);
773 INIT_ALL_SIZES(pred_high[D135_PRED], highbd_d135);
774 INIT_ALL_SIZES(pred_high[D153_PRED], highbd_d153);
775 INIT_ALL_SIZES(pred_high[TM_PRED], highbd_tm);
777 INIT_ALL_SIZES(dc_pred_high[0][0], highbd_dc_128);
778 INIT_ALL_SIZES(dc_pred_high[0][1], highbd_dc_top);
779 INIT_ALL_SIZES(dc_pred_high[1][0], highbd_dc_left);
780 INIT_ALL_SIZES(dc_pred_high[1][1], highbd_dc);
781 #endif // CONFIG_VP9_HIGHBITDEPTH
783 #undef intra_pred_allsizes
786 #if CONFIG_VP9_HIGHBITDEPTH
787 static void build_intra_predictors_high(const MACROBLOCKD *xd,
788 const uint8_t *ref8,
789 int ref_stride,
790 uint8_t *dst8,
791 int dst_stride,
792 PREDICTION_MODE mode,
793 TX_SIZE tx_size,
794 int up_available,
795 int left_available,
796 int right_available,
797 int x, int y,
798 int plane, int bd) {
799 int i;
800 uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
801 uint16_t *ref = CONVERT_TO_SHORTPTR(ref8);
802 DECLARE_ALIGNED(16, uint16_t, left_col[32]);
803 DECLARE_ALIGNED(16, uint16_t, above_data[64 + 16]);
804 uint16_t *above_row = above_data + 16;
805 const uint16_t *const_above_row = above_row;
806 const int bs = 4 << tx_size;
807 int frame_width, frame_height;
808 int x0, y0;
809 const struct macroblockd_plane *const pd = &xd->plane[plane];
810 // int base=128;
811 int base = 128 << (bd - 8);
812 // 127 127 127 .. 127 127 127 127 127 127
813 // 129 A B .. Y Z
814 // 129 C D .. W X
815 // 129 E F .. U V
816 // 129 G H .. S T T T T T
818 // Get current frame pointer, width and height.
819 if (plane == 0) {
820 frame_width = xd->cur_buf->y_width;
821 frame_height = xd->cur_buf->y_height;
822 } else {
823 frame_width = xd->cur_buf->uv_width;
824 frame_height = xd->cur_buf->uv_height;
827 // Get block position in current frame.
828 x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
829 y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
831 // left
832 if (left_available) {
833 if (xd->mb_to_bottom_edge < 0) {
834 /* slower path if the block needs border extension */
835 if (y0 + bs <= frame_height) {
836 for (i = 0; i < bs; ++i)
837 left_col[i] = ref[i * ref_stride - 1];
838 } else {
839 const int extend_bottom = frame_height - y0;
840 for (i = 0; i < extend_bottom; ++i)
841 left_col[i] = ref[i * ref_stride - 1];
842 for (; i < bs; ++i)
843 left_col[i] = ref[(extend_bottom - 1) * ref_stride - 1];
845 } else {
846 /* faster path if the block does not need extension */
847 for (i = 0; i < bs; ++i)
848 left_col[i] = ref[i * ref_stride - 1];
850 } else {
851 // TODO(Peter): this value should probably change for high bitdepth
852 vpx_memset16(left_col, base + 1, bs);
855 // TODO(hkuang) do not extend 2*bs pixels for all modes.
856 // above
857 if (up_available) {
858 const uint16_t *above_ref = ref - ref_stride;
859 if (xd->mb_to_right_edge < 0) {
860 /* slower path if the block needs border extension */
861 if (x0 + 2 * bs <= frame_width) {
862 if (right_available && bs == 4) {
863 memcpy(above_row, above_ref, 2 * bs * sizeof(uint16_t));
864 } else {
865 memcpy(above_row, above_ref, bs * sizeof(uint16_t));
866 vpx_memset16(above_row + bs, above_row[bs - 1], bs);
868 } else if (x0 + bs <= frame_width) {
869 const int r = frame_width - x0;
870 if (right_available && bs == 4) {
871 memcpy(above_row, above_ref, r * sizeof(uint16_t));
872 vpx_memset16(above_row + r, above_row[r - 1],
873 x0 + 2 * bs - frame_width);
874 } else {
875 memcpy(above_row, above_ref, bs * sizeof(uint16_t));
876 vpx_memset16(above_row + bs, above_row[bs - 1], bs);
878 } else if (x0 <= frame_width) {
879 const int r = frame_width - x0;
880 memcpy(above_row, above_ref, r * sizeof(uint16_t));
881 vpx_memset16(above_row + r, above_row[r - 1],
882 x0 + 2 * bs - frame_width);
884 // TODO(Peter) this value should probably change for high bitdepth
885 above_row[-1] = left_available ? above_ref[-1] : (base+1);
886 } else {
887 /* faster path if the block does not need extension */
888 if (bs == 4 && right_available && left_available) {
889 const_above_row = above_ref;
890 } else {
891 memcpy(above_row, above_ref, bs * sizeof(uint16_t));
892 if (bs == 4 && right_available)
893 memcpy(above_row + bs, above_ref + bs, bs * sizeof(uint16_t));
894 else
895 vpx_memset16(above_row + bs, above_row[bs - 1], bs);
896 // TODO(Peter): this value should probably change for high bitdepth
897 above_row[-1] = left_available ? above_ref[-1] : (base+1);
900 } else {
901 vpx_memset16(above_row, base - 1, bs * 2);
902 // TODO(Peter): this value should probably change for high bitdepth
903 above_row[-1] = base - 1;
906 // predict
907 if (mode == DC_PRED) {
908 dc_pred_high[left_available][up_available][tx_size](dst, dst_stride,
909 const_above_row,
910 left_col, xd->bd);
911 } else {
912 pred_high[mode][tx_size](dst, dst_stride, const_above_row, left_col,
913 xd->bd);
916 #endif // CONFIG_VP9_HIGHBITDEPTH
918 static void build_intra_predictors(const MACROBLOCKD *xd, const uint8_t *ref,
919 int ref_stride, uint8_t *dst, int dst_stride,
920 PREDICTION_MODE mode, TX_SIZE tx_size,
921 int up_available, int left_available,
922 int right_available, int x, int y,
923 int plane) {
924 int i;
925 DECLARE_ALIGNED(16, uint8_t, left_col[32]);
926 DECLARE_ALIGNED(16, uint8_t, above_data[64 + 16]);
927 uint8_t *above_row = above_data + 16;
928 const uint8_t *const_above_row = above_row;
929 const int bs = 4 << tx_size;
930 int frame_width, frame_height;
931 int x0, y0;
932 const struct macroblockd_plane *const pd = &xd->plane[plane];
934 // 127 127 127 .. 127 127 127 127 127 127
935 // 129 A B .. Y Z
936 // 129 C D .. W X
937 // 129 E F .. U V
938 // 129 G H .. S T T T T T
939 // ..
941 // Get current frame pointer, width and height.
942 if (plane == 0) {
943 frame_width = xd->cur_buf->y_width;
944 frame_height = xd->cur_buf->y_height;
945 } else {
946 frame_width = xd->cur_buf->uv_width;
947 frame_height = xd->cur_buf->uv_height;
950 // Get block position in current frame.
951 x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
952 y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
954 // NEED_LEFT
955 if (extend_modes[mode] & NEED_LEFT) {
956 if (left_available) {
957 if (xd->mb_to_bottom_edge < 0) {
958 /* slower path if the block needs border extension */
959 if (y0 + bs <= frame_height) {
960 for (i = 0; i < bs; ++i)
961 left_col[i] = ref[i * ref_stride - 1];
962 } else {
963 const int extend_bottom = frame_height - y0;
964 for (i = 0; i < extend_bottom; ++i)
965 left_col[i] = ref[i * ref_stride - 1];
966 for (; i < bs; ++i)
967 left_col[i] = ref[(extend_bottom - 1) * ref_stride - 1];
969 } else {
970 /* faster path if the block does not need extension */
971 for (i = 0; i < bs; ++i)
972 left_col[i] = ref[i * ref_stride - 1];
974 } else {
975 memset(left_col, 129, bs);
979 // NEED_ABOVE
980 if (extend_modes[mode] & NEED_ABOVE) {
981 if (up_available) {
982 const uint8_t *above_ref = ref - ref_stride;
983 if (xd->mb_to_right_edge < 0) {
984 /* slower path if the block needs border extension */
985 if (x0 + bs <= frame_width) {
986 memcpy(above_row, above_ref, bs);
987 } else if (x0 <= frame_width) {
988 const int r = frame_width - x0;
989 memcpy(above_row, above_ref, r);
990 memset(above_row + r, above_row[r - 1], x0 + bs - frame_width);
992 } else {
993 /* faster path if the block does not need extension */
994 if (bs == 4 && right_available && left_available) {
995 const_above_row = above_ref;
996 } else {
997 memcpy(above_row, above_ref, bs);
1000 above_row[-1] = left_available ? above_ref[-1] : 129;
1001 } else {
1002 memset(above_row, 127, bs);
1003 above_row[-1] = 127;
1007 // NEED_ABOVERIGHT
1008 if (extend_modes[mode] & NEED_ABOVERIGHT) {
1009 if (up_available) {
1010 const uint8_t *above_ref = ref - ref_stride;
1011 if (xd->mb_to_right_edge < 0) {
1012 /* slower path if the block needs border extension */
1013 if (x0 + 2 * bs <= frame_width) {
1014 if (right_available && bs == 4) {
1015 memcpy(above_row, above_ref, 2 * bs);
1016 } else {
1017 memcpy(above_row, above_ref, bs);
1018 memset(above_row + bs, above_row[bs - 1], bs);
1020 } else if (x0 + bs <= frame_width) {
1021 const int r = frame_width - x0;
1022 if (right_available && bs == 4) {
1023 memcpy(above_row, above_ref, r);
1024 memset(above_row + r, above_row[r - 1], x0 + 2 * bs - frame_width);
1025 } else {
1026 memcpy(above_row, above_ref, bs);
1027 memset(above_row + bs, above_row[bs - 1], bs);
1029 } else if (x0 <= frame_width) {
1030 const int r = frame_width - x0;
1031 memcpy(above_row, above_ref, r);
1032 memset(above_row + r, above_row[r - 1], x0 + 2 * bs - frame_width);
1034 } else {
1035 /* faster path if the block does not need extension */
1036 if (bs == 4 && right_available && left_available) {
1037 const_above_row = above_ref;
1038 } else {
1039 memcpy(above_row, above_ref, bs);
1040 if (bs == 4 && right_available)
1041 memcpy(above_row + bs, above_ref + bs, bs);
1042 else
1043 memset(above_row + bs, above_row[bs - 1], bs);
1046 above_row[-1] = left_available ? above_ref[-1] : 129;
1047 } else {
1048 memset(above_row, 127, bs * 2);
1049 above_row[-1] = 127;
1053 // predict
1054 if (mode == DC_PRED) {
1055 dc_pred[left_available][up_available][tx_size](dst, dst_stride,
1056 const_above_row, left_col);
1057 } else {
1058 pred[mode][tx_size](dst, dst_stride, const_above_row, left_col);
1062 void vp9_predict_intra_block(const MACROBLOCKD *xd, int bwl_in,
1063 TX_SIZE tx_size, PREDICTION_MODE mode,
1064 const uint8_t *ref, int ref_stride,
1065 uint8_t *dst, int dst_stride,
1066 int aoff, int loff, int plane) {
1067 const int bw = (1 << bwl_in);
1068 const int txw = (1 << tx_size);
1069 const int have_top = loff || xd->up_available;
1070 const int have_left = aoff || xd->left_available;
1071 const int have_right = (aoff + txw) < bw;
1072 const int x = aoff * 4;
1073 const int y = loff * 4;
1075 #if CONFIG_VP9_HIGHBITDEPTH
1076 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1077 build_intra_predictors_high(xd, ref, ref_stride, dst, dst_stride, mode,
1078 tx_size, have_top, have_left, have_right,
1079 x, y, plane, xd->bd);
1080 return;
1082 #endif
1083 build_intra_predictors(xd, ref, ref_stride, dst, dst_stride, mode, tx_size,
1084 have_top, have_left, have_right, x, y, plane);
1087 void vp9_init_intra_predictors(void) {
1088 once(vp9_init_intra_predictors_internal);