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