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