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 #include "./vpx_dsp_rtcd.h"
13 
14 #if CONFIG_VP9_HIGHBITDEPTH
15 #include "vpx_dsp/vpx_dsp_common.h"
16 #endif  // CONFIG_VP9_HIGHBITDEPTH
17 #include "vpx_mem/vpx_mem.h"
18 #include "vpx_ports/mem.h"
19 #include "vpx_ports/vpx_once.h"
20 
21 #include "vp9/common/vp9_reconintra.h"
22 #include "vp9/common/vp9_onyxc_int.h"
23 
24 const TX_TYPE intra_mode_to_tx_type_lookup[INTRA_MODES] = {
25   DCT_DCT,    // DC
26   ADST_DCT,   // V
27   DCT_ADST,   // H
28   DCT_DCT,    // D45
29   ADST_ADST,  // D135
30   ADST_DCT,   // D117
31   DCT_ADST,   // D153
32   DCT_ADST,   // D207
33   ADST_DCT,   // D63
34   ADST_ADST,  // TM
35 };
36 
37 enum {
38   NEED_LEFT = 1 << 1,
39   NEED_ABOVE = 1 << 2,
40   NEED_ABOVERIGHT = 1 << 3,
41 };
42 
43 static const uint8_t extend_modes[INTRA_MODES] = {
44   NEED_ABOVE | NEED_LEFT,       // DC
45   NEED_ABOVE,                   // V
46   NEED_LEFT,                    // H
47   NEED_ABOVERIGHT,              // D45
48   NEED_LEFT | NEED_ABOVE,       // D135
49   NEED_LEFT | NEED_ABOVE,       // D117
50   NEED_LEFT | NEED_ABOVE,       // D153
51   NEED_LEFT,                    // D207
52   NEED_ABOVERIGHT,              // D63
53   NEED_LEFT | NEED_ABOVE,       // TM
54 };
55 
56 typedef void (*intra_pred_fn)(uint8_t *dst, ptrdiff_t stride,
57                               const uint8_t *above, const uint8_t *left);
58 
59 static intra_pred_fn pred[INTRA_MODES][TX_SIZES];
60 static intra_pred_fn dc_pred[2][2][TX_SIZES];
61 
62 #if CONFIG_VP9_HIGHBITDEPTH
63 typedef void (*intra_high_pred_fn)(uint16_t *dst, ptrdiff_t stride,
64                                    const uint16_t *above, const uint16_t *left,
65                                    int bd);
66 static intra_high_pred_fn pred_high[INTRA_MODES][4];
67 static intra_high_pred_fn dc_pred_high[2][2][4];
68 #endif  // CONFIG_VP9_HIGHBITDEPTH
69 
vp9_init_intra_predictors_internal(void)70 static void vp9_init_intra_predictors_internal(void) {
71 #define INIT_ALL_SIZES(p, type) \
72   p[TX_4X4] = vpx_##type##_predictor_4x4; \
73   p[TX_8X8] = vpx_##type##_predictor_8x8; \
74   p[TX_16X16] = vpx_##type##_predictor_16x16; \
75   p[TX_32X32] = vpx_##type##_predictor_32x32
76 
77   INIT_ALL_SIZES(pred[V_PRED], v);
78   INIT_ALL_SIZES(pred[H_PRED], h);
79   INIT_ALL_SIZES(pred[D207_PRED], d207);
80   INIT_ALL_SIZES(pred[D45_PRED], d45);
81   INIT_ALL_SIZES(pred[D63_PRED], d63);
82   INIT_ALL_SIZES(pred[D117_PRED], d117);
83   INIT_ALL_SIZES(pred[D135_PRED], d135);
84   INIT_ALL_SIZES(pred[D153_PRED], d153);
85   INIT_ALL_SIZES(pred[TM_PRED], tm);
86 
87   INIT_ALL_SIZES(dc_pred[0][0], dc_128);
88   INIT_ALL_SIZES(dc_pred[0][1], dc_top);
89   INIT_ALL_SIZES(dc_pred[1][0], dc_left);
90   INIT_ALL_SIZES(dc_pred[1][1], dc);
91 
92 #if CONFIG_VP9_HIGHBITDEPTH
93   INIT_ALL_SIZES(pred_high[V_PRED], highbd_v);
94   INIT_ALL_SIZES(pred_high[H_PRED], highbd_h);
95   INIT_ALL_SIZES(pred_high[D207_PRED], highbd_d207);
96   INIT_ALL_SIZES(pred_high[D45_PRED], highbd_d45);
97   INIT_ALL_SIZES(pred_high[D63_PRED], highbd_d63);
98   INIT_ALL_SIZES(pred_high[D117_PRED], highbd_d117);
99   INIT_ALL_SIZES(pred_high[D135_PRED], highbd_d135);
100   INIT_ALL_SIZES(pred_high[D153_PRED], highbd_d153);
101   INIT_ALL_SIZES(pred_high[TM_PRED], highbd_tm);
102 
103   INIT_ALL_SIZES(dc_pred_high[0][0], highbd_dc_128);
104   INIT_ALL_SIZES(dc_pred_high[0][1], highbd_dc_top);
105   INIT_ALL_SIZES(dc_pred_high[1][0], highbd_dc_left);
106   INIT_ALL_SIZES(dc_pred_high[1][1], highbd_dc);
107 #endif  // CONFIG_VP9_HIGHBITDEPTH
108 
109 #undef intra_pred_allsizes
110 }
111 
112 #if CONFIG_VP9_HIGHBITDEPTH
build_intra_predictors_high(const MACROBLOCKD * xd,const uint8_t * ref8,int ref_stride,uint8_t * dst8,int dst_stride,PREDICTION_MODE mode,TX_SIZE tx_size,int up_available,int left_available,int right_available,int x,int y,int plane,int bd)113 static void build_intra_predictors_high(const MACROBLOCKD *xd,
114                                         const uint8_t *ref8,
115                                         int ref_stride,
116                                         uint8_t *dst8,
117                                         int dst_stride,
118                                         PREDICTION_MODE mode,
119                                         TX_SIZE tx_size,
120                                         int up_available,
121                                         int left_available,
122                                         int right_available,
123                                         int x, int y,
124                                         int plane, int bd) {
125   int i;
126   uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
127   uint16_t *ref = CONVERT_TO_SHORTPTR(ref8);
128   DECLARE_ALIGNED(16, uint16_t, left_col[32]);
129   DECLARE_ALIGNED(16, uint16_t, above_data[64 + 16]);
130   uint16_t *above_row = above_data + 16;
131   const uint16_t *const_above_row = above_row;
132   const int bs = 4 << tx_size;
133   int frame_width, frame_height;
134   int x0, y0;
135   const struct macroblockd_plane *const pd = &xd->plane[plane];
136   int base = 128 << (bd - 8);
137   // 127 127 127 .. 127 127 127 127 127 127
138   // 129  A   B  ..  Y   Z
139   // 129  C   D  ..  W   X
140   // 129  E   F  ..  U   V
141   // 129  G   H  ..  S   T   T   T   T   T
142 
143   // Get current frame pointer, width and height.
144   if (plane == 0) {
145     frame_width = xd->cur_buf->y_width;
146     frame_height = xd->cur_buf->y_height;
147   } else {
148     frame_width = xd->cur_buf->uv_width;
149     frame_height = xd->cur_buf->uv_height;
150   }
151 
152   // Get block position in current frame.
153   x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
154   y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
155 
156   // left
157   if (left_available) {
158     if (xd->mb_to_bottom_edge < 0) {
159       /* slower path if the block needs border extension */
160       if (y0 + bs <= frame_height) {
161         for (i = 0; i < bs; ++i)
162           left_col[i] = ref[i * ref_stride - 1];
163       } else {
164         const int extend_bottom = frame_height - y0;
165         for (i = 0; i < extend_bottom; ++i)
166           left_col[i] = ref[i * ref_stride - 1];
167         for (; i < bs; ++i)
168           left_col[i] = ref[(extend_bottom - 1) * ref_stride - 1];
169       }
170     } else {
171       /* faster path if the block does not need extension */
172       for (i = 0; i < bs; ++i)
173         left_col[i] = ref[i * ref_stride - 1];
174     }
175   } else {
176     // TODO(Peter): this value should probably change for high bitdepth
177     vpx_memset16(left_col, base + 1, bs);
178   }
179 
180   // TODO(hkuang) do not extend 2*bs pixels for all modes.
181   // above
182   if (up_available) {
183     const uint16_t *above_ref = ref - ref_stride;
184     if (xd->mb_to_right_edge < 0) {
185       /* slower path if the block needs border extension */
186       if (x0 + 2 * bs <= frame_width) {
187         if (right_available && bs == 4) {
188           memcpy(above_row, above_ref, 2 * bs * sizeof(above_row[0]));
189         } else {
190           memcpy(above_row, above_ref, bs * sizeof(above_row[0]));
191           vpx_memset16(above_row + bs, above_row[bs - 1], bs);
192         }
193       } else if (x0 + bs <= frame_width) {
194         const int r = frame_width - x0;
195         if (right_available && bs == 4) {
196           memcpy(above_row, above_ref, r * sizeof(above_row[0]));
197           vpx_memset16(above_row + r, above_row[r - 1],
198                        x0 + 2 * bs - frame_width);
199         } else {
200           memcpy(above_row, above_ref, bs * sizeof(above_row[0]));
201           vpx_memset16(above_row + bs, above_row[bs - 1], bs);
202         }
203       } else if (x0 <= frame_width) {
204         const int r = frame_width - x0;
205         memcpy(above_row, above_ref, r * sizeof(above_row[0]));
206         vpx_memset16(above_row + r, above_row[r - 1],
207                        x0 + 2 * bs - frame_width);
208       }
209       // TODO(Peter) this value should probably change for high bitdepth
210       above_row[-1] = left_available ? above_ref[-1] : (base+1);
211     } else {
212       /* faster path if the block does not need extension */
213       if (bs == 4 && right_available && left_available) {
214         const_above_row = above_ref;
215       } else {
216         memcpy(above_row, above_ref, bs * sizeof(above_row[0]));
217         if (bs == 4 && right_available)
218           memcpy(above_row + bs, above_ref + bs, bs * sizeof(above_row[0]));
219         else
220           vpx_memset16(above_row + bs, above_row[bs - 1], bs);
221         // TODO(Peter): this value should probably change for high bitdepth
222         above_row[-1] = left_available ? above_ref[-1] : (base+1);
223       }
224     }
225   } else {
226     vpx_memset16(above_row, base - 1, bs * 2);
227     // TODO(Peter): this value should probably change for high bitdepth
228     above_row[-1] = base - 1;
229   }
230 
231   // predict
232   if (mode == DC_PRED) {
233     dc_pred_high[left_available][up_available][tx_size](dst, dst_stride,
234                                                         const_above_row,
235                                                         left_col, xd->bd);
236   } else {
237     pred_high[mode][tx_size](dst, dst_stride, const_above_row, left_col,
238                              xd->bd);
239   }
240 }
241 #endif  // CONFIG_VP9_HIGHBITDEPTH
242 
build_intra_predictors(const MACROBLOCKD * xd,const uint8_t * ref,int ref_stride,uint8_t * dst,int dst_stride,PREDICTION_MODE mode,TX_SIZE tx_size,int up_available,int left_available,int right_available,int x,int y,int plane)243 static void build_intra_predictors(const MACROBLOCKD *xd, const uint8_t *ref,
244                                    int ref_stride, uint8_t *dst, int dst_stride,
245                                    PREDICTION_MODE mode, TX_SIZE tx_size,
246                                    int up_available, int left_available,
247                                    int right_available, int x, int y,
248                                    int plane) {
249   int i;
250   DECLARE_ALIGNED(16, uint8_t, left_col[32]);
251   DECLARE_ALIGNED(16, uint8_t, above_data[64 + 16]);
252   uint8_t *above_row = above_data + 16;
253   const uint8_t *const_above_row = above_row;
254   const int bs = 4 << tx_size;
255   int frame_width, frame_height;
256   int x0, y0;
257   const struct macroblockd_plane *const pd = &xd->plane[plane];
258 
259   // 127 127 127 .. 127 127 127 127 127 127
260   // 129  A   B  ..  Y   Z
261   // 129  C   D  ..  W   X
262   // 129  E   F  ..  U   V
263   // 129  G   H  ..  S   T   T   T   T   T
264   // ..
265 
266   // Get current frame pointer, width and height.
267   if (plane == 0) {
268     frame_width = xd->cur_buf->y_width;
269     frame_height = xd->cur_buf->y_height;
270   } else {
271     frame_width = xd->cur_buf->uv_width;
272     frame_height = xd->cur_buf->uv_height;
273   }
274 
275   // Get block position in current frame.
276   x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
277   y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
278 
279   // NEED_LEFT
280   if (extend_modes[mode] & NEED_LEFT) {
281     if (left_available) {
282       if (xd->mb_to_bottom_edge < 0) {
283         /* slower path if the block needs border extension */
284         if (y0 + bs <= frame_height) {
285           for (i = 0; i < bs; ++i)
286             left_col[i] = ref[i * ref_stride - 1];
287         } else {
288           const int extend_bottom = frame_height - y0;
289           for (i = 0; i < extend_bottom; ++i)
290             left_col[i] = ref[i * ref_stride - 1];
291           for (; i < bs; ++i)
292             left_col[i] = ref[(extend_bottom - 1) * ref_stride - 1];
293         }
294       } else {
295         /* faster path if the block does not need extension */
296         for (i = 0; i < bs; ++i)
297           left_col[i] = ref[i * ref_stride - 1];
298       }
299     } else {
300       memset(left_col, 129, bs);
301     }
302   }
303 
304   // NEED_ABOVE
305   if (extend_modes[mode] & NEED_ABOVE) {
306     if (up_available) {
307       const uint8_t *above_ref = ref - ref_stride;
308       if (xd->mb_to_right_edge < 0) {
309         /* slower path if the block needs border extension */
310         if (x0 + bs <= frame_width) {
311           memcpy(above_row, above_ref, bs);
312         } else if (x0 <= frame_width) {
313           const int r = frame_width - x0;
314           memcpy(above_row, above_ref, r);
315           memset(above_row + r, above_row[r - 1], x0 + bs - frame_width);
316         }
317       } else {
318         /* faster path if the block does not need extension */
319         if (bs == 4 && right_available && left_available) {
320           const_above_row = above_ref;
321         } else {
322           memcpy(above_row, above_ref, bs);
323         }
324       }
325       above_row[-1] = left_available ? above_ref[-1] : 129;
326     } else {
327       memset(above_row, 127, bs);
328       above_row[-1] = 127;
329     }
330   }
331 
332   // NEED_ABOVERIGHT
333   if (extend_modes[mode] & NEED_ABOVERIGHT) {
334     if (up_available) {
335       const uint8_t *above_ref = ref - ref_stride;
336       if (xd->mb_to_right_edge < 0) {
337         /* slower path if the block needs border extension */
338         if (x0 + 2 * bs <= frame_width) {
339           if (right_available && bs == 4) {
340             memcpy(above_row, above_ref, 2 * bs);
341           } else {
342             memcpy(above_row, above_ref, bs);
343             memset(above_row + bs, above_row[bs - 1], bs);
344           }
345         } else if (x0 + bs <= frame_width) {
346           const int r = frame_width - x0;
347           if (right_available && bs == 4) {
348             memcpy(above_row, above_ref, r);
349             memset(above_row + r, above_row[r - 1], x0 + 2 * bs - frame_width);
350           } else {
351             memcpy(above_row, above_ref, bs);
352             memset(above_row + bs, above_row[bs - 1], bs);
353           }
354         } else if (x0 <= frame_width) {
355           const int r = frame_width - x0;
356           memcpy(above_row, above_ref, r);
357           memset(above_row + r, above_row[r - 1], x0 + 2 * bs - frame_width);
358         }
359       } else {
360         /* faster path if the block does not need extension */
361         if (bs == 4 && right_available && left_available) {
362           const_above_row = above_ref;
363         } else {
364           memcpy(above_row, above_ref, bs);
365           if (bs == 4 && right_available)
366             memcpy(above_row + bs, above_ref + bs, bs);
367           else
368             memset(above_row + bs, above_row[bs - 1], bs);
369         }
370       }
371       above_row[-1] = left_available ? above_ref[-1] : 129;
372     } else {
373       memset(above_row, 127, bs * 2);
374       above_row[-1] = 127;
375     }
376   }
377 
378   // predict
379   if (mode == DC_PRED) {
380     dc_pred[left_available][up_available][tx_size](dst, dst_stride,
381                                                    const_above_row, left_col);
382   } else {
383     pred[mode][tx_size](dst, dst_stride, const_above_row, left_col);
384   }
385 }
386 
vp9_predict_intra_block(const MACROBLOCKD * xd,int bwl_in,TX_SIZE tx_size,PREDICTION_MODE mode,const uint8_t * ref,int ref_stride,uint8_t * dst,int dst_stride,int aoff,int loff,int plane)387 void vp9_predict_intra_block(const MACROBLOCKD *xd, int bwl_in,
388                              TX_SIZE tx_size, PREDICTION_MODE mode,
389                              const uint8_t *ref, int ref_stride,
390                              uint8_t *dst, int dst_stride,
391                              int aoff, int loff, int plane) {
392   const int bw = (1 << bwl_in);
393   const int txw = (1 << tx_size);
394   const int have_top = loff || xd->up_available;
395   const int have_left = aoff || xd->left_available;
396   const int have_right = (aoff + txw) < bw;
397   const int x = aoff * 4;
398   const int y = loff * 4;
399 
400 #if CONFIG_VP9_HIGHBITDEPTH
401   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
402     build_intra_predictors_high(xd, ref, ref_stride, dst, dst_stride, mode,
403                                 tx_size, have_top, have_left, have_right,
404                                 x, y, plane, xd->bd);
405     return;
406   }
407 #endif
408   build_intra_predictors(xd, ref, ref_stride, dst, dst_stride, mode, tx_size,
409                          have_top, have_left, have_right, x, y, plane);
410 }
411 
vp9_init_intra_predictors(void)412 void vp9_init_intra_predictors(void) {
413   once(vp9_init_intra_predictors_internal);
414 }
415