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