]> granicus.if.org Git - libvpx/blob - vpx_dsp/intrapred.c
Merge "block error avx2: sum in 32 bits when possible"
[libvpx] / vpx_dsp / intrapred.c
1 /*
2  *  Copyright (c) 2015 The WebM project authors. All Rights Reserved.
3  *
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  */
10
11 #include "./vpx_config.h"
12 #include "./vpx_dsp_rtcd.h"
13
14 #include "vpx_dsp/vpx_dsp_common.h"
15 #include "vpx_mem/vpx_mem.h"
16
17 #define DST(x, y) dst[(x) + (y)*stride]
18 #define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)
19 #define AVG2(a, b) (((a) + (b) + 1) >> 1)
20
21 static INLINE void d207_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
22                                   const uint8_t *above, const uint8_t *left) {
23   int r, c;
24   (void)above;
25   // first column
26   for (r = 0; r < bs - 1; ++r) dst[r * stride] = AVG2(left[r], left[r + 1]);
27   dst[(bs - 1) * stride] = left[bs - 1];
28   dst++;
29
30   // second column
31   for (r = 0; r < bs - 2; ++r)
32     dst[r * stride] = AVG3(left[r], left[r + 1], left[r + 2]);
33   dst[(bs - 2) * stride] = AVG3(left[bs - 2], left[bs - 1], left[bs - 1]);
34   dst[(bs - 1) * stride] = left[bs - 1];
35   dst++;
36
37   // rest of last row
38   for (c = 0; c < bs - 2; ++c) dst[(bs - 1) * stride + c] = left[bs - 1];
39
40   for (r = bs - 2; r >= 0; --r)
41     for (c = 0; c < bs - 2; ++c)
42       dst[r * stride + c] = dst[(r + 1) * stride + c - 2];
43 }
44
45 static INLINE void d63_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
46                                  const uint8_t *above, const uint8_t *left) {
47   int r, c;
48   int size;
49   (void)left;
50   for (c = 0; c < bs; ++c) {
51     dst[c] = AVG2(above[c], above[c + 1]);
52     dst[stride + c] = AVG3(above[c], above[c + 1], above[c + 2]);
53   }
54   for (r = 2, size = bs - 2; r < bs; r += 2, --size) {
55     memcpy(dst + (r + 0) * stride, dst + (r >> 1), size);
56     memset(dst + (r + 0) * stride + size, above[bs - 1], bs - size);
57     memcpy(dst + (r + 1) * stride, dst + stride + (r >> 1), size);
58     memset(dst + (r + 1) * stride + size, above[bs - 1], bs - size);
59   }
60 }
61
62 static INLINE void d45_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
63                                  const uint8_t *above, const uint8_t *left) {
64   const uint8_t above_right = above[bs - 1];
65   const uint8_t *const dst_row0 = dst;
66   int x, size;
67   (void)left;
68
69   for (x = 0; x < bs - 1; ++x) {
70     dst[x] = AVG3(above[x], above[x + 1], above[x + 2]);
71   }
72   dst[bs - 1] = above_right;
73   dst += stride;
74   for (x = 1, size = bs - 2; x < bs; ++x, --size) {
75     memcpy(dst, dst_row0 + x, size);
76     memset(dst + size, above_right, x + 1);
77     dst += stride;
78   }
79 }
80
81 static INLINE void d117_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
82                                   const uint8_t *above, const uint8_t *left) {
83   int r, c;
84
85   // first row
86   for (c = 0; c < bs; c++) dst[c] = AVG2(above[c - 1], above[c]);
87   dst += stride;
88
89   // second row
90   dst[0] = AVG3(left[0], above[-1], above[0]);
91   for (c = 1; c < bs; c++) dst[c] = AVG3(above[c - 2], above[c - 1], above[c]);
92   dst += stride;
93
94   // the rest of first col
95   dst[0] = AVG3(above[-1], left[0], left[1]);
96   for (r = 3; r < bs; ++r)
97     dst[(r - 2) * stride] = AVG3(left[r - 3], left[r - 2], left[r - 1]);
98
99   // the rest of the block
100   for (r = 2; r < bs; ++r) {
101     for (c = 1; c < bs; c++) dst[c] = dst[-2 * stride + c - 1];
102     dst += stride;
103   }
104 }
105
106 static INLINE void d135_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
107                                   const uint8_t *above, const uint8_t *left) {
108   int i;
109 #if defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ > 7
110   // silence a spurious -Warray-bounds warning, possibly related to:
111   // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56273
112   uint8_t border[69];
113 #else
114   uint8_t border[32 + 32 - 1];  // outer border from bottom-left to top-right
115 #endif
116
117   // dst(bs, bs - 2)[0], i.e., border starting at bottom-left
118   for (i = 0; i < bs - 2; ++i) {
119     border[i] = AVG3(left[bs - 3 - i], left[bs - 2 - i], left[bs - 1 - i]);
120   }
121   border[bs - 2] = AVG3(above[-1], left[0], left[1]);
122   border[bs - 1] = AVG3(left[0], above[-1], above[0]);
123   border[bs - 0] = AVG3(above[-1], above[0], above[1]);
124   // dst[0][2, size), i.e., remaining top border ascending
125   for (i = 0; i < bs - 2; ++i) {
126     border[bs + 1 + i] = AVG3(above[i], above[i + 1], above[i + 2]);
127   }
128
129   for (i = 0; i < bs; ++i) {
130     memcpy(dst + i * stride, border + bs - 1 - i, bs);
131   }
132 }
133
134 static INLINE void d153_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
135                                   const uint8_t *above, const uint8_t *left) {
136   int r, c;
137   dst[0] = AVG2(above[-1], left[0]);
138   for (r = 1; r < bs; r++) dst[r * stride] = AVG2(left[r - 1], left[r]);
139   dst++;
140
141   dst[0] = AVG3(left[0], above[-1], above[0]);
142   dst[stride] = AVG3(above[-1], left[0], left[1]);
143   for (r = 2; r < bs; r++)
144     dst[r * stride] = AVG3(left[r - 2], left[r - 1], left[r]);
145   dst++;
146
147   for (c = 0; c < bs - 2; c++)
148     dst[c] = AVG3(above[c - 1], above[c], above[c + 1]);
149   dst += stride;
150
151   for (r = 1; r < bs; ++r) {
152     for (c = 0; c < bs - 2; c++) dst[c] = dst[-stride + c - 2];
153     dst += stride;
154   }
155 }
156
157 static INLINE void v_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
158                                const uint8_t *above, const uint8_t *left) {
159   int r;
160   (void)left;
161
162   for (r = 0; r < bs; r++) {
163     memcpy(dst, above, bs);
164     dst += stride;
165   }
166 }
167
168 static INLINE void h_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
169                                const uint8_t *above, const uint8_t *left) {
170   int r;
171   (void)above;
172
173   for (r = 0; r < bs; r++) {
174     memset(dst, left[r], bs);
175     dst += stride;
176   }
177 }
178
179 static INLINE void tm_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
180                                 const uint8_t *above, const uint8_t *left) {
181   int r, c;
182   int ytop_left = above[-1];
183
184   for (r = 0; r < bs; r++) {
185     for (c = 0; c < bs; c++)
186       dst[c] = clip_pixel(left[r] + above[c] - ytop_left);
187     dst += stride;
188   }
189 }
190
191 static INLINE void dc_128_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
192                                     const uint8_t *above, const uint8_t *left) {
193   int r;
194   (void)above;
195   (void)left;
196
197   for (r = 0; r < bs; r++) {
198     memset(dst, 128, bs);
199     dst += stride;
200   }
201 }
202
203 static INLINE void dc_left_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
204                                      const uint8_t *above,
205                                      const uint8_t *left) {
206   int i, r, expected_dc, sum = 0;
207   (void)above;
208
209   for (i = 0; i < bs; i++) sum += left[i];
210   expected_dc = (sum + (bs >> 1)) / bs;
211
212   for (r = 0; r < bs; r++) {
213     memset(dst, expected_dc, bs);
214     dst += stride;
215   }
216 }
217
218 static INLINE void dc_top_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
219                                     const uint8_t *above, const uint8_t *left) {
220   int i, r, expected_dc, sum = 0;
221   (void)left;
222
223   for (i = 0; i < bs; i++) sum += above[i];
224   expected_dc = (sum + (bs >> 1)) / bs;
225
226   for (r = 0; r < bs; r++) {
227     memset(dst, expected_dc, bs);
228     dst += stride;
229   }
230 }
231
232 static INLINE void dc_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
233                                 const uint8_t *above, const uint8_t *left) {
234   int i, r, expected_dc, sum = 0;
235   const int count = 2 * bs;
236
237   for (i = 0; i < bs; i++) {
238     sum += above[i];
239     sum += left[i];
240   }
241
242   expected_dc = (sum + (count >> 1)) / count;
243
244   for (r = 0; r < bs; r++) {
245     memset(dst, expected_dc, bs);
246     dst += stride;
247   }
248 }
249
250 void vpx_he_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
251                             const uint8_t *above, const uint8_t *left) {
252   const int H = above[-1];
253   const int I = left[0];
254   const int J = left[1];
255   const int K = left[2];
256   const int L = left[3];
257
258   memset(dst + stride * 0, AVG3(H, I, J), 4);
259   memset(dst + stride * 1, AVG3(I, J, K), 4);
260   memset(dst + stride * 2, AVG3(J, K, L), 4);
261   memset(dst + stride * 3, AVG3(K, L, L), 4);
262 }
263
264 void vpx_ve_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
265                             const uint8_t *above, const uint8_t *left) {
266   const int H = above[-1];
267   const int I = above[0];
268   const int J = above[1];
269   const int K = above[2];
270   const int L = above[3];
271   const int M = above[4];
272   (void)left;
273
274   dst[0] = AVG3(H, I, J);
275   dst[1] = AVG3(I, J, K);
276   dst[2] = AVG3(J, K, L);
277   dst[3] = AVG3(K, L, M);
278   memcpy(dst + stride * 1, dst, 4);
279   memcpy(dst + stride * 2, dst, 4);
280   memcpy(dst + stride * 3, dst, 4);
281 }
282
283 void vpx_d207_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
284                               const uint8_t *above, const uint8_t *left) {
285   const int I = left[0];
286   const int J = left[1];
287   const int K = left[2];
288   const int L = left[3];
289   (void)above;
290   DST(0, 0) = AVG2(I, J);
291   DST(2, 0) = DST(0, 1) = AVG2(J, K);
292   DST(2, 1) = DST(0, 2) = AVG2(K, L);
293   DST(1, 0) = AVG3(I, J, K);
294   DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
295   DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
296   DST(3, 2) = DST(2, 2) = DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
297 }
298
299 void vpx_d63_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
300                              const uint8_t *above, const uint8_t *left) {
301   const int A = above[0];
302   const int B = above[1];
303   const int C = above[2];
304   const int D = above[3];
305   const int E = above[4];
306   const int F = above[5];
307   const int G = above[6];
308   (void)left;
309   DST(0, 0) = AVG2(A, B);
310   DST(1, 0) = DST(0, 2) = AVG2(B, C);
311   DST(2, 0) = DST(1, 2) = AVG2(C, D);
312   DST(3, 0) = DST(2, 2) = AVG2(D, E);
313   DST(3, 2) = AVG2(E, F);  // differs from vp8
314
315   DST(0, 1) = AVG3(A, B, C);
316   DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
317   DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
318   DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
319   DST(3, 3) = AVG3(E, F, G);  // differs from vp8
320 }
321
322 void vpx_d63e_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
323                               const uint8_t *above, const uint8_t *left) {
324   const int A = above[0];
325   const int B = above[1];
326   const int C = above[2];
327   const int D = above[3];
328   const int E = above[4];
329   const int F = above[5];
330   const int G = above[6];
331   const int H = above[7];
332   (void)left;
333   DST(0, 0) = AVG2(A, B);
334   DST(1, 0) = DST(0, 2) = AVG2(B, C);
335   DST(2, 0) = DST(1, 2) = AVG2(C, D);
336   DST(3, 0) = DST(2, 2) = AVG2(D, E);
337   DST(3, 2) = AVG3(E, F, G);
338
339   DST(0, 1) = AVG3(A, B, C);
340   DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
341   DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
342   DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
343   DST(3, 3) = AVG3(F, G, H);
344 }
345
346 void vpx_d45_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
347                              const uint8_t *above, const uint8_t *left) {
348   const int A = above[0];
349   const int B = above[1];
350   const int C = above[2];
351   const int D = above[3];
352   const int E = above[4];
353   const int F = above[5];
354   const int G = above[6];
355   const int H = above[7];
356   (void)stride;
357   (void)left;
358   DST(0, 0) = AVG3(A, B, C);
359   DST(1, 0) = DST(0, 1) = AVG3(B, C, D);
360   DST(2, 0) = DST(1, 1) = DST(0, 2) = AVG3(C, D, E);
361   DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
362   DST(3, 1) = DST(2, 2) = DST(1, 3) = AVG3(E, F, G);
363   DST(3, 2) = DST(2, 3) = AVG3(F, G, H);
364   DST(3, 3) = H;  // differs from vp8
365 }
366
367 void vpx_d45e_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
368                               const uint8_t *above, const uint8_t *left) {
369   const int A = above[0];
370   const int B = above[1];
371   const int C = above[2];
372   const int D = above[3];
373   const int E = above[4];
374   const int F = above[5];
375   const int G = above[6];
376   const int H = above[7];
377   (void)stride;
378   (void)left;
379   DST(0, 0) = AVG3(A, B, C);
380   DST(1, 0) = DST(0, 1) = AVG3(B, C, D);
381   DST(2, 0) = DST(1, 1) = DST(0, 2) = AVG3(C, D, E);
382   DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
383   DST(3, 1) = DST(2, 2) = DST(1, 3) = AVG3(E, F, G);
384   DST(3, 2) = DST(2, 3) = AVG3(F, G, H);
385   DST(3, 3) = AVG3(G, H, H);
386 }
387
388 void vpx_d117_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
389                               const uint8_t *above, const uint8_t *left) {
390   const int I = left[0];
391   const int J = left[1];
392   const int K = left[2];
393   const int X = above[-1];
394   const int A = above[0];
395   const int B = above[1];
396   const int C = above[2];
397   const int D = above[3];
398   DST(0, 0) = DST(1, 2) = AVG2(X, A);
399   DST(1, 0) = DST(2, 2) = AVG2(A, B);
400   DST(2, 0) = DST(3, 2) = AVG2(B, C);
401   DST(3, 0) = AVG2(C, D);
402
403   DST(0, 3) = AVG3(K, J, I);
404   DST(0, 2) = AVG3(J, I, X);
405   DST(0, 1) = DST(1, 3) = AVG3(I, X, A);
406   DST(1, 1) = DST(2, 3) = AVG3(X, A, B);
407   DST(2, 1) = DST(3, 3) = AVG3(A, B, C);
408   DST(3, 1) = AVG3(B, C, D);
409 }
410
411 void vpx_d135_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
412                               const uint8_t *above, const uint8_t *left) {
413   const int I = left[0];
414   const int J = left[1];
415   const int K = left[2];
416   const int L = left[3];
417   const int X = above[-1];
418   const int A = above[0];
419   const int B = above[1];
420   const int C = above[2];
421   const int D = above[3];
422   (void)stride;
423   DST(0, 3) = AVG3(J, K, L);
424   DST(1, 3) = DST(0, 2) = AVG3(I, J, K);
425   DST(2, 3) = DST(1, 2) = DST(0, 1) = AVG3(X, I, J);
426   DST(3, 3) = DST(2, 2) = DST(1, 1) = DST(0, 0) = AVG3(A, X, I);
427   DST(3, 2) = DST(2, 1) = DST(1, 0) = AVG3(B, A, X);
428   DST(3, 1) = DST(2, 0) = AVG3(C, B, A);
429   DST(3, 0) = AVG3(D, C, B);
430 }
431
432 void vpx_d153_predictor_4x4_c(uint8_t *dst, ptrdiff_t stride,
433                               const uint8_t *above, const uint8_t *left) {
434   const int I = left[0];
435   const int J = left[1];
436   const int K = left[2];
437   const int L = left[3];
438   const int X = above[-1];
439   const int A = above[0];
440   const int B = above[1];
441   const int C = above[2];
442
443   DST(0, 0) = DST(2, 1) = AVG2(I, X);
444   DST(0, 1) = DST(2, 2) = AVG2(J, I);
445   DST(0, 2) = DST(2, 3) = AVG2(K, J);
446   DST(0, 3) = AVG2(L, K);
447
448   DST(3, 0) = AVG3(A, B, C);
449   DST(2, 0) = AVG3(X, A, B);
450   DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
451   DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
452   DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
453   DST(1, 3) = AVG3(L, K, J);
454 }
455
456 #if CONFIG_VP9_HIGHBITDEPTH
457 static INLINE void highbd_d207_predictor(uint16_t *dst, ptrdiff_t stride,
458                                          int bs, const uint16_t *above,
459                                          const uint16_t *left, int bd) {
460   int r, c;
461   (void)above;
462   (void)bd;
463
464   // First column.
465   for (r = 0; r < bs - 1; ++r) {
466     dst[r * stride] = AVG2(left[r], left[r + 1]);
467   }
468   dst[(bs - 1) * stride] = left[bs - 1];
469   dst++;
470
471   // Second column.
472   for (r = 0; r < bs - 2; ++r) {
473     dst[r * stride] = AVG3(left[r], left[r + 1], left[r + 2]);
474   }
475   dst[(bs - 2) * stride] = AVG3(left[bs - 2], left[bs - 1], left[bs - 1]);
476   dst[(bs - 1) * stride] = left[bs - 1];
477   dst++;
478
479   // Rest of last row.
480   for (c = 0; c < bs - 2; ++c) dst[(bs - 1) * stride + c] = left[bs - 1];
481
482   for (r = bs - 2; r >= 0; --r) {
483     for (c = 0; c < bs - 2; ++c)
484       dst[r * stride + c] = dst[(r + 1) * stride + c - 2];
485   }
486 }
487
488 static INLINE void highbd_d63_predictor(uint16_t *dst, ptrdiff_t stride, int bs,
489                                         const uint16_t *above,
490                                         const uint16_t *left, int bd) {
491   int r, c;
492   (void)left;
493   (void)bd;
494   for (r = 0; r < bs; ++r) {
495     for (c = 0; c < bs; ++c) {
496       dst[c] = r & 1 ? AVG3(above[(r >> 1) + c], above[(r >> 1) + c + 1],
497                             above[(r >> 1) + c + 2])
498                      : AVG2(above[(r >> 1) + c], above[(r >> 1) + c + 1]);
499     }
500     dst += stride;
501   }
502 }
503
504 static INLINE void highbd_d45_predictor(uint16_t *dst, ptrdiff_t stride, int bs,
505                                         const uint16_t *above,
506                                         const uint16_t *left, int bd) {
507   int r, c;
508   (void)left;
509   (void)bd;
510   for (r = 0; r < bs; ++r) {
511     for (c = 0; c < bs; ++c) {
512       dst[c] = r + c + 2 < bs * 2
513                    ? AVG3(above[r + c], above[r + c + 1], above[r + c + 2])
514                    : above[bs * 2 - 1];
515     }
516     dst += stride;
517   }
518 }
519
520 static INLINE void highbd_d117_predictor(uint16_t *dst, ptrdiff_t stride,
521                                          int bs, const uint16_t *above,
522                                          const uint16_t *left, int bd) {
523   int r, c;
524   (void)bd;
525
526   // first row
527   for (c = 0; c < bs; c++) dst[c] = AVG2(above[c - 1], above[c]);
528   dst += stride;
529
530   // second row
531   dst[0] = AVG3(left[0], above[-1], above[0]);
532   for (c = 1; c < bs; c++) dst[c] = AVG3(above[c - 2], above[c - 1], above[c]);
533   dst += stride;
534
535   // the rest of first col
536   dst[0] = AVG3(above[-1], left[0], left[1]);
537   for (r = 3; r < bs; ++r)
538     dst[(r - 2) * stride] = AVG3(left[r - 3], left[r - 2], left[r - 1]);
539
540   // the rest of the block
541   for (r = 2; r < bs; ++r) {
542     for (c = 1; c < bs; c++) dst[c] = dst[-2 * stride + c - 1];
543     dst += stride;
544   }
545 }
546
547 static INLINE void highbd_d135_predictor(uint16_t *dst, ptrdiff_t stride,
548                                          int bs, const uint16_t *above,
549                                          const uint16_t *left, int bd) {
550   int i;
551 #if defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ > 7
552   // silence a spurious -Warray-bounds warning, possibly related to:
553   // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56273
554   uint16_t border[69];
555 #else
556   uint16_t border[32 + 32 - 1];  // outer border from bottom-left to top-right
557 #endif
558   (void)bd;
559
560   // dst(bs, bs - 2)[0], i.e., border starting at bottom-left
561   for (i = 0; i < bs - 2; ++i) {
562     border[i] = AVG3(left[bs - 3 - i], left[bs - 2 - i], left[bs - 1 - i]);
563   }
564   border[bs - 2] = AVG3(above[-1], left[0], left[1]);
565   border[bs - 1] = AVG3(left[0], above[-1], above[0]);
566   border[bs - 0] = AVG3(above[-1], above[0], above[1]);
567   // dst[0][2, size), i.e., remaining top border ascending
568   for (i = 0; i < bs - 2; ++i) {
569     border[bs + 1 + i] = AVG3(above[i], above[i + 1], above[i + 2]);
570   }
571
572   for (i = 0; i < bs; ++i) {
573     memcpy(dst + i * stride, border + bs - 1 - i, bs * sizeof(dst[0]));
574   }
575 }
576
577 static INLINE void highbd_d153_predictor(uint16_t *dst, ptrdiff_t stride,
578                                          int bs, const uint16_t *above,
579                                          const uint16_t *left, int bd) {
580   int r, c;
581   (void)bd;
582   dst[0] = AVG2(above[-1], left[0]);
583   for (r = 1; r < bs; r++) dst[r * stride] = AVG2(left[r - 1], left[r]);
584   dst++;
585
586   dst[0] = AVG3(left[0], above[-1], above[0]);
587   dst[stride] = AVG3(above[-1], left[0], left[1]);
588   for (r = 2; r < bs; r++)
589     dst[r * stride] = AVG3(left[r - 2], left[r - 1], left[r]);
590   dst++;
591
592   for (c = 0; c < bs - 2; c++)
593     dst[c] = AVG3(above[c - 1], above[c], above[c + 1]);
594   dst += stride;
595
596   for (r = 1; r < bs; ++r) {
597     for (c = 0; c < bs - 2; c++) dst[c] = dst[-stride + c - 2];
598     dst += stride;
599   }
600 }
601
602 static INLINE void highbd_v_predictor(uint16_t *dst, ptrdiff_t stride, int bs,
603                                       const uint16_t *above,
604                                       const uint16_t *left, int bd) {
605   int r;
606   (void)left;
607   (void)bd;
608   for (r = 0; r < bs; r++) {
609     memcpy(dst, above, bs * sizeof(uint16_t));
610     dst += stride;
611   }
612 }
613
614 static INLINE void highbd_h_predictor(uint16_t *dst, ptrdiff_t stride, int bs,
615                                       const uint16_t *above,
616                                       const uint16_t *left, int bd) {
617   int r;
618   (void)above;
619   (void)bd;
620   for (r = 0; r < bs; r++) {
621     vpx_memset16(dst, left[r], bs);
622     dst += stride;
623   }
624 }
625
626 static INLINE void highbd_tm_predictor(uint16_t *dst, ptrdiff_t stride, int bs,
627                                        const uint16_t *above,
628                                        const uint16_t *left, int bd) {
629   int r, c;
630   int ytop_left = above[-1];
631   (void)bd;
632
633   for (r = 0; r < bs; r++) {
634     for (c = 0; c < bs; c++)
635       dst[c] = clip_pixel_highbd(left[r] + above[c] - ytop_left, bd);
636     dst += stride;
637   }
638 }
639
640 static INLINE void highbd_dc_128_predictor(uint16_t *dst, ptrdiff_t stride,
641                                            int bs, const uint16_t *above,
642                                            const uint16_t *left, int bd) {
643   int r;
644   (void)above;
645   (void)left;
646
647   for (r = 0; r < bs; r++) {
648     vpx_memset16(dst, 128 << (bd - 8), bs);
649     dst += stride;
650   }
651 }
652
653 static INLINE void highbd_dc_left_predictor(uint16_t *dst, ptrdiff_t stride,
654                                             int bs, const uint16_t *above,
655                                             const uint16_t *left, int bd) {
656   int i, r, expected_dc, sum = 0;
657   (void)above;
658   (void)bd;
659
660   for (i = 0; i < bs; i++) sum += left[i];
661   expected_dc = (sum + (bs >> 1)) / bs;
662
663   for (r = 0; r < bs; r++) {
664     vpx_memset16(dst, expected_dc, bs);
665     dst += stride;
666   }
667 }
668
669 static INLINE void highbd_dc_top_predictor(uint16_t *dst, ptrdiff_t stride,
670                                            int bs, const uint16_t *above,
671                                            const uint16_t *left, int bd) {
672   int i, r, expected_dc, sum = 0;
673   (void)left;
674   (void)bd;
675
676   for (i = 0; i < bs; i++) sum += above[i];
677   expected_dc = (sum + (bs >> 1)) / bs;
678
679   for (r = 0; r < bs; r++) {
680     vpx_memset16(dst, expected_dc, bs);
681     dst += stride;
682   }
683 }
684
685 static INLINE void highbd_dc_predictor(uint16_t *dst, ptrdiff_t stride, int bs,
686                                        const uint16_t *above,
687                                        const uint16_t *left, int bd) {
688   int i, r, expected_dc, sum = 0;
689   const int count = 2 * bs;
690   (void)bd;
691
692   for (i = 0; i < bs; i++) {
693     sum += above[i];
694     sum += left[i];
695   }
696
697   expected_dc = (sum + (count >> 1)) / count;
698
699   for (r = 0; r < bs; r++) {
700     vpx_memset16(dst, expected_dc, bs);
701     dst += stride;
702   }
703 }
704
705 void vpx_highbd_d207_predictor_4x4_c(uint16_t *dst, ptrdiff_t stride,
706                                      const uint16_t *above,
707                                      const uint16_t *left, int bd) {
708   const int I = left[0];
709   const int J = left[1];
710   const int K = left[2];
711   const int L = left[3];
712   (void)above;
713   (void)bd;
714   DST(0, 0) = AVG2(I, J);
715   DST(2, 0) = DST(0, 1) = AVG2(J, K);
716   DST(2, 1) = DST(0, 2) = AVG2(K, L);
717   DST(1, 0) = AVG3(I, J, K);
718   DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
719   DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
720   DST(3, 2) = DST(2, 2) = DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
721 }
722
723 void vpx_highbd_d63_predictor_4x4_c(uint16_t *dst, ptrdiff_t stride,
724                                     const uint16_t *above, const uint16_t *left,
725                                     int bd) {
726   const int A = above[0];
727   const int B = above[1];
728   const int C = above[2];
729   const int D = above[3];
730   const int E = above[4];
731   const int F = above[5];
732   const int G = above[6];
733   (void)left;
734   (void)bd;
735   DST(0, 0) = AVG2(A, B);
736   DST(1, 0) = DST(0, 2) = AVG2(B, C);
737   DST(2, 0) = DST(1, 2) = AVG2(C, D);
738   DST(3, 0) = DST(2, 2) = AVG2(D, E);
739   DST(3, 2) = AVG2(E, F);  // differs from vp8
740
741   DST(0, 1) = AVG3(A, B, C);
742   DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
743   DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
744   DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
745   DST(3, 3) = AVG3(E, F, G);  // differs from vp8
746 }
747
748 void vpx_highbd_d45_predictor_4x4_c(uint16_t *dst, ptrdiff_t stride,
749                                     const uint16_t *above, const uint16_t *left,
750                                     int bd) {
751   const int A = above[0];
752   const int B = above[1];
753   const int C = above[2];
754   const int D = above[3];
755   const int E = above[4];
756   const int F = above[5];
757   const int G = above[6];
758   const int H = above[7];
759   (void)left;
760   (void)bd;
761   DST(0, 0) = AVG3(A, B, C);
762   DST(1, 0) = DST(0, 1) = AVG3(B, C, D);
763   DST(2, 0) = DST(1, 1) = DST(0, 2) = AVG3(C, D, E);
764   DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
765   DST(3, 1) = DST(2, 2) = DST(1, 3) = AVG3(E, F, G);
766   DST(3, 2) = DST(2, 3) = AVG3(F, G, H);
767   DST(3, 3) = H;  // differs from vp8
768 }
769
770 void vpx_highbd_d117_predictor_4x4_c(uint16_t *dst, ptrdiff_t stride,
771                                      const uint16_t *above,
772                                      const uint16_t *left, int bd) {
773   const int I = left[0];
774   const int J = left[1];
775   const int K = left[2];
776   const int X = above[-1];
777   const int A = above[0];
778   const int B = above[1];
779   const int C = above[2];
780   const int D = above[3];
781   (void)bd;
782   DST(0, 0) = DST(1, 2) = AVG2(X, A);
783   DST(1, 0) = DST(2, 2) = AVG2(A, B);
784   DST(2, 0) = DST(3, 2) = AVG2(B, C);
785   DST(3, 0) = AVG2(C, D);
786
787   DST(0, 3) = AVG3(K, J, I);
788   DST(0, 2) = AVG3(J, I, X);
789   DST(0, 1) = DST(1, 3) = AVG3(I, X, A);
790   DST(1, 1) = DST(2, 3) = AVG3(X, A, B);
791   DST(2, 1) = DST(3, 3) = AVG3(A, B, C);
792   DST(3, 1) = AVG3(B, C, D);
793 }
794
795 void vpx_highbd_d135_predictor_4x4_c(uint16_t *dst, ptrdiff_t stride,
796                                      const uint16_t *above,
797                                      const uint16_t *left, int bd) {
798   const int I = left[0];
799   const int J = left[1];
800   const int K = left[2];
801   const int L = left[3];
802   const int X = above[-1];
803   const int A = above[0];
804   const int B = above[1];
805   const int C = above[2];
806   const int D = above[3];
807   (void)bd;
808   DST(0, 3) = AVG3(J, K, L);
809   DST(1, 3) = DST(0, 2) = AVG3(I, J, K);
810   DST(2, 3) = DST(1, 2) = DST(0, 1) = AVG3(X, I, J);
811   DST(3, 3) = DST(2, 2) = DST(1, 1) = DST(0, 0) = AVG3(A, X, I);
812   DST(3, 2) = DST(2, 1) = DST(1, 0) = AVG3(B, A, X);
813   DST(3, 1) = DST(2, 0) = AVG3(C, B, A);
814   DST(3, 0) = AVG3(D, C, B);
815 }
816
817 void vpx_highbd_d153_predictor_4x4_c(uint16_t *dst, ptrdiff_t stride,
818                                      const uint16_t *above,
819                                      const uint16_t *left, int bd) {
820   const int I = left[0];
821   const int J = left[1];
822   const int K = left[2];
823   const int L = left[3];
824   const int X = above[-1];
825   const int A = above[0];
826   const int B = above[1];
827   const int C = above[2];
828   (void)bd;
829
830   DST(0, 0) = DST(2, 1) = AVG2(I, X);
831   DST(0, 1) = DST(2, 2) = AVG2(J, I);
832   DST(0, 2) = DST(2, 3) = AVG2(K, J);
833   DST(0, 3) = AVG2(L, K);
834
835   DST(3, 0) = AVG3(A, B, C);
836   DST(2, 0) = AVG3(X, A, B);
837   DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
838   DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
839   DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
840   DST(1, 3) = AVG3(L, K, J);
841 }
842 #endif  // CONFIG_VP9_HIGHBITDEPTH
843
844 // This serves as a wrapper function, so that all the prediction functions
845 // can be unified and accessed as a pointer array. Note that the boundary
846 // above and left are not necessarily used all the time.
847 #define intra_pred_sized(type, size)                        \
848   void vpx_##type##_predictor_##size##x##size##_c(          \
849       uint8_t *dst, ptrdiff_t stride, const uint8_t *above, \
850       const uint8_t *left) {                                \
851     type##_predictor(dst, stride, size, above, left);       \
852   }
853
854 #if CONFIG_VP9_HIGHBITDEPTH
855 #define intra_pred_highbd_sized(type, size)                        \
856   void vpx_highbd_##type##_predictor_##size##x##size##_c(          \
857       uint16_t *dst, ptrdiff_t stride, const uint16_t *above,      \
858       const uint16_t *left, int bd) {                              \
859     highbd_##type##_predictor(dst, stride, size, above, left, bd); \
860   }
861
862 /* clang-format off */
863 #define intra_pred_allsizes(type) \
864   intra_pred_sized(type, 4) \
865   intra_pred_sized(type, 8) \
866   intra_pred_sized(type, 16) \
867   intra_pred_sized(type, 32) \
868   intra_pred_highbd_sized(type, 4) \
869   intra_pred_highbd_sized(type, 8) \
870   intra_pred_highbd_sized(type, 16) \
871   intra_pred_highbd_sized(type, 32)
872
873 #define intra_pred_no_4x4(type) \
874   intra_pred_sized(type, 8) \
875   intra_pred_sized(type, 16) \
876   intra_pred_sized(type, 32) \
877   intra_pred_highbd_sized(type, 8) \
878   intra_pred_highbd_sized(type, 16) \
879   intra_pred_highbd_sized(type, 32)
880
881 #else
882 #define intra_pred_allsizes(type) \
883   intra_pred_sized(type, 4) \
884   intra_pred_sized(type, 8) \
885   intra_pred_sized(type, 16) \
886   intra_pred_sized(type, 32)
887
888 #define intra_pred_no_4x4(type) \
889   intra_pred_sized(type, 8) \
890   intra_pred_sized(type, 16) \
891   intra_pred_sized(type, 32)
892 #endif  // CONFIG_VP9_HIGHBITDEPTH
893
894 intra_pred_no_4x4(d207)
895 intra_pred_no_4x4(d63)
896 intra_pred_no_4x4(d45)
897 intra_pred_no_4x4(d117)
898 intra_pred_no_4x4(d135)
899 intra_pred_no_4x4(d153)
900 intra_pred_allsizes(v)
901 intra_pred_allsizes(h)
902 intra_pred_allsizes(tm)
903 intra_pred_allsizes(dc_128)
904 intra_pred_allsizes(dc_left)
905 intra_pred_allsizes(dc_top)
906 intra_pred_allsizes(dc)
907 /* clang-format on */
908 #undef intra_pred_allsizes