1 /*
2  *  Copyright (c) 2010 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 
13 #include "vpx_mem/vpx_mem.h"
14 #include "vpx_ports/vpx_once.h"
15 
16 #include "./vp9_rtcd.h"
17 
18 #include "vp9/common/vp9_reconintra.h"
19 #include "vp9/common/vp9_onyxc_int.h"
20 
21 const TX_TYPE intra_mode_to_tx_type_lookup[INTRA_MODES] = {
22   DCT_DCT,    // DC
23   ADST_DCT,   // V
24   DCT_ADST,   // H
25   DCT_DCT,    // D45
26   ADST_ADST,  // D135
27   ADST_DCT,   // D117
28   DCT_ADST,   // D153
29   DCT_ADST,   // D207
30   ADST_DCT,   // D63
31   ADST_ADST,  // TM
32 };
33 
34 #define intra_pred_sized(type, size) \
35   void vp9_##type##_predictor_##size##x##size##_c(uint8_t *dst, \
36                                                   ptrdiff_t stride, \
37                                                   const uint8_t *above, \
38                                                   const uint8_t *left) { \
39     type##_predictor(dst, stride, size, above, left); \
40   }
41 
42 #define intra_pred_allsizes(type) \
43   intra_pred_sized(type, 4) \
44   intra_pred_sized(type, 8) \
45   intra_pred_sized(type, 16) \
46   intra_pred_sized(type, 32)
47 
d207_predictor(uint8_t * dst,ptrdiff_t stride,int bs,const uint8_t * above,const uint8_t * left)48 static INLINE void d207_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
49                                   const uint8_t *above, const uint8_t *left) {
50   int r, c;
51   (void)above;
52   // first column
53   for (r = 0; r < bs - 1; ++r)
54     dst[r * stride] = ROUND_POWER_OF_TWO(left[r] + left[r + 1], 1);
55   dst[(bs - 1) * stride] = left[bs - 1];
56   dst++;
57 
58   // second column
59   for (r = 0; r < bs - 2; ++r)
60     dst[r * stride] = ROUND_POWER_OF_TWO(left[r] + left[r + 1] * 2 +
61                                          left[r + 2], 2);
62   dst[(bs - 2) * stride] = ROUND_POWER_OF_TWO(left[bs - 2] +
63                                               left[bs - 1] * 3, 2);
64   dst[(bs - 1) * stride] = left[bs - 1];
65   dst++;
66 
67   // rest of last row
68   for (c = 0; c < bs - 2; ++c)
69     dst[(bs - 1) * stride + c] = left[bs - 1];
70 
71   for (r = bs - 2; r >= 0; --r)
72     for (c = 0; c < bs - 2; ++c)
73       dst[r * stride + c] = dst[(r + 1) * stride + c - 2];
74 }
intra_pred_allsizes(d207)75 intra_pred_allsizes(d207)
76 
77 static INLINE void d63_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
78                                  const uint8_t *above, const uint8_t *left) {
79   int r, c;
80   (void)left;
81   for (r = 0; r < bs; ++r) {
82     for (c = 0; c < bs; ++c)
83       dst[c] = r & 1 ? ROUND_POWER_OF_TWO(above[r/2 + c] +
84                                           above[r/2 + c + 1] * 2 +
85                                           above[r/2 + c + 2], 2)
86                      : ROUND_POWER_OF_TWO(above[r/2 + c] +
87                                           above[r/2 + c + 1], 1);
88     dst += stride;
89   }
90 }
intra_pred_allsizes(d63)91 intra_pred_allsizes(d63)
92 
93 static INLINE void d45_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
94                                  const uint8_t *above, const uint8_t *left) {
95   int r, c;
96   (void)left;
97   for (r = 0; r < bs; ++r) {
98     for (c = 0; c < bs; ++c)
99       dst[c] = r + c + 2 < bs * 2 ?  ROUND_POWER_OF_TWO(above[r + c] +
100                                                         above[r + c + 1] * 2 +
101                                                         above[r + c + 2], 2)
102                                   : above[bs * 2 - 1];
103     dst += stride;
104   }
105 }
intra_pred_allsizes(d45)106 intra_pred_allsizes(d45)
107 
108 static INLINE void d117_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
109                                   const uint8_t *above, const uint8_t *left) {
110   int r, c;
111   // first row
112   for (c = 0; c < bs; c++)
113     dst[c] = ROUND_POWER_OF_TWO(above[c - 1] + above[c], 1);
114   dst += stride;
115 
116   // second row
117   dst[0] = ROUND_POWER_OF_TWO(left[0] + above[-1] * 2 + above[0], 2);
118   for (c = 1; c < bs; c++)
119     dst[c] = ROUND_POWER_OF_TWO(above[c - 2] + above[c - 1] * 2 + above[c], 2);
120   dst += stride;
121 
122   // the rest of first col
123   dst[0] = ROUND_POWER_OF_TWO(above[-1] + left[0] * 2 + left[1], 2);
124   for (r = 3; r < bs; ++r)
125     dst[(r - 2) * stride] = ROUND_POWER_OF_TWO(left[r - 3] + left[r - 2] * 2 +
126                                                left[r - 1], 2);
127 
128   // the rest of the block
129   for (r = 2; r < bs; ++r) {
130     for (c = 1; c < bs; c++)
131       dst[c] = dst[-2 * stride + c - 1];
132     dst += stride;
133   }
134 }
intra_pred_allsizes(d117)135 intra_pred_allsizes(d117)
136 
137 static INLINE void d135_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
138                                   const uint8_t *above, const uint8_t *left) {
139   int r, c;
140   dst[0] = ROUND_POWER_OF_TWO(left[0] + above[-1] * 2 + above[0], 2);
141   for (c = 1; c < bs; c++)
142     dst[c] = ROUND_POWER_OF_TWO(above[c - 2] + above[c - 1] * 2 + above[c], 2);
143 
144   dst[stride] = ROUND_POWER_OF_TWO(above[-1] + left[0] * 2 + left[1], 2);
145   for (r = 2; r < bs; ++r)
146     dst[r * stride] = ROUND_POWER_OF_TWO(left[r - 2] + left[r - 1] * 2 +
147                                          left[r], 2);
148 
149   dst += stride;
150   for (r = 1; r < bs; ++r) {
151     for (c = 1; c < bs; c++)
152       dst[c] = dst[-stride + c - 1];
153     dst += stride;
154   }
155 }
intra_pred_allsizes(d135)156 intra_pred_allsizes(d135)
157 
158 static INLINE void d153_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
159                                   const uint8_t *above, const uint8_t *left) {
160   int r, c;
161   dst[0] = ROUND_POWER_OF_TWO(above[-1] + left[0], 1);
162   for (r = 1; r < bs; r++)
163     dst[r * stride] = ROUND_POWER_OF_TWO(left[r - 1] + left[r], 1);
164   dst++;
165 
166   dst[0] = ROUND_POWER_OF_TWO(left[0] + above[-1] * 2 + above[0], 2);
167   dst[stride] = ROUND_POWER_OF_TWO(above[-1] + left[0] * 2 + left[1], 2);
168   for (r = 2; r < bs; r++)
169     dst[r * stride] = ROUND_POWER_OF_TWO(left[r - 2] + left[r - 1] * 2 +
170                                          left[r], 2);
171   dst++;
172 
173   for (c = 0; c < bs - 2; c++)
174     dst[c] = ROUND_POWER_OF_TWO(above[c - 1] + above[c] * 2 + above[c + 1], 2);
175   dst += stride;
176 
177   for (r = 1; r < bs; ++r) {
178     for (c = 0; c < bs - 2; c++)
179       dst[c] = dst[-stride + c - 2];
180     dst += stride;
181   }
182 }
intra_pred_allsizes(d153)183 intra_pred_allsizes(d153)
184 
185 static INLINE void v_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
186                                const uint8_t *above, const uint8_t *left) {
187   int r;
188   (void)left;
189   for (r = 0; r < bs; r++) {
190     vpx_memcpy(dst, above, bs);
191     dst += stride;
192   }
193 }
intra_pred_allsizes(v)194 intra_pred_allsizes(v)
195 
196 static INLINE void h_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
197                                const uint8_t *above, const uint8_t *left) {
198   int r;
199   (void)above;
200   for (r = 0; r < bs; r++) {
201     vpx_memset(dst, left[r], bs);
202     dst += stride;
203   }
204 }
intra_pred_allsizes(h)205 intra_pred_allsizes(h)
206 
207 static INLINE void tm_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
208                                 const uint8_t *above, const uint8_t *left) {
209   int r, c;
210   int ytop_left = above[-1];
211 
212   for (r = 0; r < bs; r++) {
213     for (c = 0; c < bs; c++)
214       dst[c] = clip_pixel(left[r] + above[c] - ytop_left);
215     dst += stride;
216   }
217 }
intra_pred_allsizes(tm)218 intra_pred_allsizes(tm)
219 
220 static INLINE void dc_128_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
221                                     const uint8_t *above, const uint8_t *left) {
222   int r;
223   (void)above;
224   (void)left;
225   for (r = 0; r < bs; r++) {
226     vpx_memset(dst, 128, bs);
227     dst += stride;
228   }
229 }
intra_pred_allsizes(dc_128)230 intra_pred_allsizes(dc_128)
231 
232 static INLINE void dc_left_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
233                                      const uint8_t *above,
234                                      const uint8_t *left) {
235   int i, r, expected_dc, sum = 0;
236   (void)above;
237   for (i = 0; i < bs; i++)
238     sum += left[i];
239   expected_dc = (sum + (bs >> 1)) / bs;
240 
241   for (r = 0; r < bs; r++) {
242     vpx_memset(dst, expected_dc, bs);
243     dst += stride;
244   }
245 }
intra_pred_allsizes(dc_left)246 intra_pred_allsizes(dc_left)
247 
248 static INLINE void dc_top_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
249                                     const uint8_t *above, const uint8_t *left) {
250   int i, r, expected_dc, sum = 0;
251   (void)left;
252   for (i = 0; i < bs; i++)
253     sum += above[i];
254   expected_dc = (sum + (bs >> 1)) / bs;
255 
256   for (r = 0; r < bs; r++) {
257     vpx_memset(dst, expected_dc, bs);
258     dst += stride;
259   }
260 }
intra_pred_allsizes(dc_top)261 intra_pred_allsizes(dc_top)
262 
263 static INLINE void dc_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
264                                 const uint8_t *above, const uint8_t *left) {
265   int i, r, expected_dc, sum = 0;
266   const int count = 2 * bs;
267 
268   for (i = 0; i < bs; i++) {
269     sum += above[i];
270     sum += left[i];
271   }
272 
273   expected_dc = (sum + (count >> 1)) / count;
274 
275   for (r = 0; r < bs; r++) {
276     vpx_memset(dst, expected_dc, bs);
277     dst += stride;
278   }
279 }
280 intra_pred_allsizes(dc)
281 #undef intra_pred_allsizes
282 
283 typedef void (*intra_pred_fn)(uint8_t *dst, ptrdiff_t stride,
284                               const uint8_t *above, const uint8_t *left);
285 
286 static intra_pred_fn pred[INTRA_MODES][4];
287 static intra_pred_fn dc_pred[2][2][4];
288 
init_intra_pred_fn_ptrs(void)289 static void init_intra_pred_fn_ptrs(void) {
290 #define intra_pred_allsizes(l, type) \
291   l[0] = vp9_##type##_predictor_4x4; \
292   l[1] = vp9_##type##_predictor_8x8; \
293   l[2] = vp9_##type##_predictor_16x16; \
294   l[3] = vp9_##type##_predictor_32x32
295 
296   intra_pred_allsizes(pred[V_PRED], v);
297   intra_pred_allsizes(pred[H_PRED], h);
298   intra_pred_allsizes(pred[D207_PRED], d207);
299   intra_pred_allsizes(pred[D45_PRED], d45);
300   intra_pred_allsizes(pred[D63_PRED], d63);
301   intra_pred_allsizes(pred[D117_PRED], d117);
302   intra_pred_allsizes(pred[D135_PRED], d135);
303   intra_pred_allsizes(pred[D153_PRED], d153);
304   intra_pred_allsizes(pred[TM_PRED], tm);
305 
306   intra_pred_allsizes(dc_pred[0][0], dc_128);
307   intra_pred_allsizes(dc_pred[0][1], dc_top);
308   intra_pred_allsizes(dc_pred[1][0], dc_left);
309   intra_pred_allsizes(dc_pred[1][1], dc);
310 
311 #undef intra_pred_allsizes
312 }
313 
build_intra_predictors(const MACROBLOCKD * xd,const uint8_t * ref,int ref_stride,uint8_t * dst,int dst_stride,MB_PREDICTION_MODE mode,TX_SIZE tx_size,int up_available,int left_available,int right_available,int x,int y,int plane)314 static void build_intra_predictors(const MACROBLOCKD *xd, const uint8_t *ref,
315                                    int ref_stride, uint8_t *dst, int dst_stride,
316                                    MB_PREDICTION_MODE mode, TX_SIZE tx_size,
317                                    int up_available, int left_available,
318                                    int right_available, int x, int y,
319                                    int plane) {
320   int i;
321   DECLARE_ALIGNED_ARRAY(16, uint8_t, left_col, 64);
322   DECLARE_ALIGNED_ARRAY(16, uint8_t, above_data, 128 + 16);
323   uint8_t *above_row = above_data + 16;
324   const uint8_t *const_above_row = above_row;
325   const int bs = 4 << tx_size;
326   int frame_width, frame_height;
327   int x0, y0;
328   const struct macroblockd_plane *const pd = &xd->plane[plane];
329 
330   // 127 127 127 .. 127 127 127 127 127 127
331   // 129  A   B  ..  Y   Z
332   // 129  C   D  ..  W   X
333   // 129  E   F  ..  U   V
334   // 129  G   H  ..  S   T   T   T   T   T
335   // ..
336 
337   once(init_intra_pred_fn_ptrs);
338 
339   // Get current frame pointer, width and height.
340   if (plane == 0) {
341     frame_width = xd->cur_buf->y_width;
342     frame_height = xd->cur_buf->y_height;
343   } else {
344     frame_width = xd->cur_buf->uv_width;
345     frame_height = xd->cur_buf->uv_height;
346   }
347 
348   // Get block position in current frame.
349   x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
350   y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
351 
352   vpx_memset(left_col, 129, 64);
353 
354   // left
355   if (left_available) {
356     if (xd->mb_to_bottom_edge < 0) {
357       /* slower path if the block needs border extension */
358       if (y0 + bs <= frame_height) {
359         for (i = 0; i < bs; ++i)
360           left_col[i] = ref[i * ref_stride - 1];
361       } else {
362         const int extend_bottom = frame_height - y0;
363         for (i = 0; i < extend_bottom; ++i)
364           left_col[i] = ref[i * ref_stride - 1];
365         for (; i < bs; ++i)
366           left_col[i] = ref[(extend_bottom - 1) * ref_stride - 1];
367       }
368     } else {
369       /* faster path if the block does not need extension */
370       for (i = 0; i < bs; ++i)
371         left_col[i] = ref[i * ref_stride - 1];
372     }
373   }
374 
375   // TODO(hkuang) do not extend 2*bs pixels for all modes.
376   // above
377   if (up_available) {
378     const uint8_t *above_ref = ref - ref_stride;
379     if (xd->mb_to_right_edge < 0) {
380       /* slower path if the block needs border extension */
381       if (x0 + 2 * bs <= frame_width) {
382         if (right_available && bs == 4) {
383           vpx_memcpy(above_row, above_ref, 2 * bs);
384         } else {
385           vpx_memcpy(above_row, above_ref, bs);
386           vpx_memset(above_row + bs, above_row[bs - 1], bs);
387         }
388       } else if (x0 + bs <= frame_width) {
389         const int r = frame_width - x0;
390         if (right_available && bs == 4) {
391           vpx_memcpy(above_row, above_ref, r);
392           vpx_memset(above_row + r, above_row[r - 1],
393                      x0 + 2 * bs - frame_width);
394         } else {
395           vpx_memcpy(above_row, above_ref, bs);
396           vpx_memset(above_row + bs, above_row[bs - 1], bs);
397         }
398       } else if (x0 <= frame_width) {
399         const int r = frame_width - x0;
400         if (right_available && bs == 4) {
401           vpx_memcpy(above_row, above_ref, r);
402           vpx_memset(above_row + r, above_row[r - 1],
403                      x0 + 2 * bs - frame_width);
404         } else {
405           vpx_memcpy(above_row, above_ref, r);
406           vpx_memset(above_row + r, above_row[r - 1],
407                      x0 + 2 * bs - frame_width);
408         }
409       }
410       above_row[-1] = left_available ? above_ref[-1] : 129;
411     } else {
412       /* faster path if the block does not need extension */
413       if (bs == 4 && right_available && left_available) {
414         const_above_row = above_ref;
415       } else {
416         vpx_memcpy(above_row, above_ref, bs);
417         if (bs == 4 && right_available)
418           vpx_memcpy(above_row + bs, above_ref + bs, bs);
419         else
420           vpx_memset(above_row + bs, above_row[bs - 1], bs);
421         above_row[-1] = left_available ? above_ref[-1] : 129;
422       }
423     }
424   } else {
425     vpx_memset(above_row, 127, bs * 2);
426     above_row[-1] = 127;
427   }
428 
429   // predict
430   if (mode == DC_PRED) {
431     dc_pred[left_available][up_available][tx_size](dst, dst_stride,
432                                                    const_above_row, left_col);
433   } else {
434     pred[mode][tx_size](dst, dst_stride, const_above_row, left_col);
435   }
436 }
437 
vp9_predict_intra_block(const MACROBLOCKD * xd,int block_idx,int bwl_in,TX_SIZE tx_size,MB_PREDICTION_MODE mode,const uint8_t * ref,int ref_stride,uint8_t * dst,int dst_stride,int aoff,int loff,int plane)438 void vp9_predict_intra_block(const MACROBLOCKD *xd, int block_idx, int bwl_in,
439                              TX_SIZE tx_size, MB_PREDICTION_MODE mode,
440                              const uint8_t *ref, int ref_stride,
441                              uint8_t *dst, int dst_stride,
442                              int aoff, int loff, int plane) {
443   const int bwl = bwl_in - tx_size;
444   const int wmask = (1 << bwl) - 1;
445   const int have_top = (block_idx >> bwl) || xd->up_available;
446   const int have_left = (block_idx & wmask) || xd->left_available;
447   const int have_right = ((block_idx & wmask) != wmask);
448   const int x = aoff * 4;
449   const int y = loff * 4;
450 
451   assert(bwl >= 0);
452   build_intra_predictors(xd, ref, ref_stride, dst, dst_stride, mode, tx_size,
453                          have_top, have_left, have_right, x, y, plane);
454 }
455