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(
114 const MACROBLOCKD *xd, const uint8_t *ref8, int ref_stride, uint8_t *dst8,
115 int dst_stride, PREDICTION_MODE mode, TX_SIZE tx_size, int up_available,
116 int left_available, int right_available, int x, int y, int plane, int bd) {
117 int i;
118 uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
119 uint16_t *ref = CONVERT_TO_SHORTPTR(ref8);
120 DECLARE_ALIGNED(16, uint16_t, left_col[32]);
121 DECLARE_ALIGNED(16, uint16_t, above_data[64 + 16]);
122 uint16_t *above_row = above_data + 16;
123 const uint16_t *const_above_row = above_row;
124 const int bs = 4 << tx_size;
125 int frame_width, frame_height;
126 int x0, y0;
127 const struct macroblockd_plane *const pd = &xd->plane[plane];
128 const int need_left = extend_modes[mode] & NEED_LEFT;
129 const int need_above = extend_modes[mode] & NEED_ABOVE;
130 const int need_aboveright = extend_modes[mode] & NEED_ABOVERIGHT;
131 int base = 128 << (bd - 8);
132 // 127 127 127 .. 127 127 127 127 127 127
133 // 129 A B .. Y Z
134 // 129 C D .. W X
135 // 129 E F .. U V
136 // 129 G H .. S T T T T T
137 // For 10 bit and 12 bit, 127 and 129 are replaced by base -1 and base + 1.
138
139 // Get current frame pointer, width and height.
140 if (plane == 0) {
141 frame_width = xd->cur_buf->y_width;
142 frame_height = xd->cur_buf->y_height;
143 } else {
144 frame_width = xd->cur_buf->uv_width;
145 frame_height = xd->cur_buf->uv_height;
146 }
147
148 // Get block position in current frame.
149 x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
150 y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
151
152 // NEED_LEFT
153 if (need_left) {
154 if (left_available) {
155 if (xd->mb_to_bottom_edge < 0) {
156 /* slower path if the block needs border extension */
157 if (y0 + bs <= frame_height) {
158 for (i = 0; i < bs; ++i) left_col[i] = ref[i * ref_stride - 1];
159 } else {
160 const int extend_bottom = frame_height - y0;
161 for (i = 0; i < extend_bottom; ++i)
162 left_col[i] = ref[i * ref_stride - 1];
163 for (; i < bs; ++i)
164 left_col[i] = ref[(extend_bottom - 1) * ref_stride - 1];
165 }
166 } else {
167 /* faster path if the block does not need extension */
168 for (i = 0; i < bs; ++i) left_col[i] = ref[i * ref_stride - 1];
169 }
170 } else {
171 vpx_memset16(left_col, base + 1, bs);
172 }
173 }
174
175 // NEED_ABOVE
176 if (need_above) {
177 if (up_available) {
178 const uint16_t *above_ref = ref - ref_stride;
179 if (xd->mb_to_right_edge < 0) {
180 /* slower path if the block needs border extension */
181 if (x0 + bs <= frame_width) {
182 memcpy(above_row, above_ref, bs * sizeof(above_row[0]));
183 } else if (x0 <= frame_width) {
184 const int r = frame_width - x0;
185 memcpy(above_row, above_ref, r * sizeof(above_row[0]));
186 vpx_memset16(above_row + r, above_row[r - 1], x0 + bs - frame_width);
187 }
188 } else {
189 /* faster path if the block does not need extension */
190 if (bs == 4 && right_available && left_available) {
191 const_above_row = above_ref;
192 } else {
193 memcpy(above_row, above_ref, bs * sizeof(above_row[0]));
194 }
195 }
196 above_row[-1] = left_available ? above_ref[-1] : (base + 1);
197 } else {
198 vpx_memset16(above_row, base - 1, bs);
199 above_row[-1] = base - 1;
200 }
201 }
202
203 // NEED_ABOVERIGHT
204 if (need_aboveright) {
205 if (up_available) {
206 const uint16_t *above_ref = ref - ref_stride;
207 if (xd->mb_to_right_edge < 0) {
208 /* slower path if the block needs border extension */
209 if (x0 + 2 * bs <= frame_width) {
210 if (right_available && bs == 4) {
211 memcpy(above_row, above_ref, 2 * bs * sizeof(above_row[0]));
212 } else {
213 memcpy(above_row, above_ref, bs * sizeof(above_row[0]));
214 vpx_memset16(above_row + bs, above_row[bs - 1], bs);
215 }
216 } else if (x0 + bs <= frame_width) {
217 const int r = frame_width - x0;
218 if (right_available && bs == 4) {
219 memcpy(above_row, above_ref, r * sizeof(above_row[0]));
220 vpx_memset16(above_row + r, above_row[r - 1],
221 x0 + 2 * bs - frame_width);
222 } else {
223 memcpy(above_row, above_ref, bs * sizeof(above_row[0]));
224 vpx_memset16(above_row + bs, above_row[bs - 1], bs);
225 }
226 } else if (x0 <= frame_width) {
227 const int r = frame_width - x0;
228 memcpy(above_row, above_ref, r * sizeof(above_row[0]));
229 vpx_memset16(above_row + r, above_row[r - 1],
230 x0 + 2 * bs - frame_width);
231 }
232 above_row[-1] = left_available ? above_ref[-1] : (base + 1);
233 } else {
234 /* faster path if the block does not need extension */
235 if (bs == 4 && right_available && left_available) {
236 const_above_row = above_ref;
237 } else {
238 memcpy(above_row, above_ref, bs * sizeof(above_row[0]));
239 if (bs == 4 && right_available)
240 memcpy(above_row + bs, above_ref + bs, bs * sizeof(above_row[0]));
241 else
242 vpx_memset16(above_row + bs, above_row[bs - 1], bs);
243 above_row[-1] = left_available ? above_ref[-1] : (base + 1);
244 }
245 }
246 } else {
247 vpx_memset16(above_row, base - 1, bs * 2);
248 above_row[-1] = base - 1;
249 }
250 }
251
252 // predict
253 if (mode == DC_PRED) {
254 dc_pred_high[left_available][up_available][tx_size](
255 dst, dst_stride, const_above_row, left_col, xd->bd);
256 } else {
257 pred_high[mode][tx_size](dst, dst_stride, const_above_row, left_col,
258 xd->bd);
259 }
260 }
261 #endif // CONFIG_VP9_HIGHBITDEPTH
262
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)263 static void build_intra_predictors(const MACROBLOCKD *xd, const uint8_t *ref,
264 int ref_stride, uint8_t *dst, int dst_stride,
265 PREDICTION_MODE mode, TX_SIZE tx_size,
266 int up_available, int left_available,
267 int right_available, int x, int y,
268 int plane) {
269 int i;
270 DECLARE_ALIGNED(16, uint8_t, left_col[32]);
271 DECLARE_ALIGNED(16, uint8_t, above_data[64 + 16]);
272 uint8_t *above_row = above_data + 16;
273 const uint8_t *const_above_row = above_row;
274 const int bs = 4 << tx_size;
275 int frame_width, frame_height;
276 int x0, y0;
277 const struct macroblockd_plane *const pd = &xd->plane[plane];
278
279 // 127 127 127 .. 127 127 127 127 127 127
280 // 129 A B .. Y Z
281 // 129 C D .. W X
282 // 129 E F .. U V
283 // 129 G H .. S T T T T T
284 // ..
285
286 // Get current frame pointer, width and height.
287 if (plane == 0) {
288 frame_width = xd->cur_buf->y_width;
289 frame_height = xd->cur_buf->y_height;
290 } else {
291 frame_width = xd->cur_buf->uv_width;
292 frame_height = xd->cur_buf->uv_height;
293 }
294
295 // Get block position in current frame.
296 x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
297 y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
298
299 // NEED_LEFT
300 if (extend_modes[mode] & NEED_LEFT) {
301 if (left_available) {
302 if (xd->mb_to_bottom_edge < 0) {
303 /* slower path if the block needs border extension */
304 if (y0 + bs <= frame_height) {
305 for (i = 0; i < bs; ++i) left_col[i] = ref[i * ref_stride - 1];
306 } else {
307 const int extend_bottom = frame_height - y0;
308 for (i = 0; i < extend_bottom; ++i)
309 left_col[i] = ref[i * ref_stride - 1];
310 for (; i < bs; ++i)
311 left_col[i] = ref[(extend_bottom - 1) * ref_stride - 1];
312 }
313 } else {
314 /* faster path if the block does not need extension */
315 for (i = 0; i < bs; ++i) left_col[i] = ref[i * ref_stride - 1];
316 }
317 } else {
318 memset(left_col, 129, bs);
319 }
320 }
321
322 // NEED_ABOVE
323 if (extend_modes[mode] & NEED_ABOVE) {
324 if (up_available) {
325 const uint8_t *above_ref = ref - ref_stride;
326 if (xd->mb_to_right_edge < 0) {
327 /* slower path if the block needs border extension */
328 if (x0 + bs <= frame_width) {
329 memcpy(above_row, above_ref, bs);
330 } else if (x0 <= frame_width) {
331 const int r = frame_width - x0;
332 memcpy(above_row, above_ref, r);
333 memset(above_row + r, above_row[r - 1], x0 + bs - frame_width);
334 }
335 } else {
336 /* faster path if the block does not need extension */
337 if (bs == 4 && right_available && left_available) {
338 const_above_row = above_ref;
339 } else {
340 memcpy(above_row, above_ref, bs);
341 }
342 }
343 above_row[-1] = left_available ? above_ref[-1] : 129;
344 } else {
345 memset(above_row, 127, bs);
346 above_row[-1] = 127;
347 }
348 }
349
350 // NEED_ABOVERIGHT
351 if (extend_modes[mode] & NEED_ABOVERIGHT) {
352 if (up_available) {
353 const uint8_t *above_ref = ref - ref_stride;
354 if (xd->mb_to_right_edge < 0) {
355 /* slower path if the block needs border extension */
356 if (x0 + 2 * bs <= frame_width) {
357 if (right_available && bs == 4) {
358 memcpy(above_row, above_ref, 2 * bs);
359 } else {
360 memcpy(above_row, above_ref, bs);
361 memset(above_row + bs, above_row[bs - 1], bs);
362 }
363 } else if (x0 + bs <= frame_width) {
364 const int r = frame_width - x0;
365 if (right_available && bs == 4) {
366 memcpy(above_row, above_ref, r);
367 memset(above_row + r, above_row[r - 1], x0 + 2 * bs - frame_width);
368 } else {
369 memcpy(above_row, above_ref, bs);
370 memset(above_row + bs, above_row[bs - 1], bs);
371 }
372 } else if (x0 <= frame_width) {
373 const int r = frame_width - x0;
374 memcpy(above_row, above_ref, r);
375 memset(above_row + r, above_row[r - 1], x0 + 2 * bs - frame_width);
376 }
377 } else {
378 /* faster path if the block does not need extension */
379 if (bs == 4 && right_available && left_available) {
380 const_above_row = above_ref;
381 } else {
382 memcpy(above_row, above_ref, bs);
383 if (bs == 4 && right_available)
384 memcpy(above_row + bs, above_ref + bs, bs);
385 else
386 memset(above_row + bs, above_row[bs - 1], bs);
387 }
388 }
389 above_row[-1] = left_available ? above_ref[-1] : 129;
390 } else {
391 memset(above_row, 127, bs * 2);
392 above_row[-1] = 127;
393 }
394 }
395
396 // predict
397 if (mode == DC_PRED) {
398 dc_pred[left_available][up_available][tx_size](dst, dst_stride,
399 const_above_row, left_col);
400 } else {
401 pred[mode][tx_size](dst, dst_stride, const_above_row, left_col);
402 }
403 }
404
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)405 void vp9_predict_intra_block(const MACROBLOCKD *xd, int bwl_in, TX_SIZE tx_size,
406 PREDICTION_MODE mode, const uint8_t *ref,
407 int ref_stride, uint8_t *dst, int dst_stride,
408 int aoff, int loff, int plane) {
409 const int bw = (1 << bwl_in);
410 const int txw = (1 << tx_size);
411 const int have_top = loff || (xd->above_mi != NULL);
412 const int have_left = aoff || (xd->left_mi != NULL);
413 const int have_right = (aoff + txw) < bw;
414 const int x = aoff * 4;
415 const int y = loff * 4;
416
417 #if CONFIG_VP9_HIGHBITDEPTH
418 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
419 build_intra_predictors_high(xd, ref, ref_stride, dst, dst_stride, mode,
420 tx_size, have_top, have_left, have_right, x, y,
421 plane, xd->bd);
422 return;
423 }
424 #endif
425 build_intra_predictors(xd, ref, ref_stride, dst, dst_stride, mode, tx_size,
426 have_top, have_left, have_right, x, y, plane);
427 }
428
vp9_init_intra_predictors(void)429 void vp9_init_intra_predictors(void) {
430 once(vp9_init_intra_predictors_internal);
431 }
432