/src/libvpx/vp9/common/vp9_reconintra.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
70 | 1 | static void vp9_init_intra_predictors_internal(void) { |
71 | 1 | #define INIT_ALL_SIZES(p, type) \ |
72 | 26 | p[TX_4X4] = vpx_##type##_predictor_4x4; \ |
73 | 26 | p[TX_8X8] = vpx_##type##_predictor_8x8; \ |
74 | 26 | p[TX_16X16] = vpx_##type##_predictor_16x16; \ |
75 | 26 | p[TX_32X32] = vpx_##type##_predictor_32x32 |
76 | | |
77 | 1 | INIT_ALL_SIZES(pred[V_PRED], v); |
78 | 1 | INIT_ALL_SIZES(pred[H_PRED], h); |
79 | 1 | INIT_ALL_SIZES(pred[D207_PRED], d207); |
80 | 1 | INIT_ALL_SIZES(pred[D45_PRED], d45); |
81 | 1 | INIT_ALL_SIZES(pred[D63_PRED], d63); |
82 | 1 | INIT_ALL_SIZES(pred[D117_PRED], d117); |
83 | 1 | INIT_ALL_SIZES(pred[D135_PRED], d135); |
84 | 1 | INIT_ALL_SIZES(pred[D153_PRED], d153); |
85 | 1 | INIT_ALL_SIZES(pred[TM_PRED], tm); |
86 | | |
87 | 1 | INIT_ALL_SIZES(dc_pred[0][0], dc_128); |
88 | 1 | INIT_ALL_SIZES(dc_pred[0][1], dc_top); |
89 | 1 | INIT_ALL_SIZES(dc_pred[1][0], dc_left); |
90 | 1 | INIT_ALL_SIZES(dc_pred[1][1], dc); |
91 | | |
92 | 1 | #if CONFIG_VP9_HIGHBITDEPTH |
93 | 1 | INIT_ALL_SIZES(pred_high[V_PRED], highbd_v); |
94 | 1 | INIT_ALL_SIZES(pred_high[H_PRED], highbd_h); |
95 | 1 | INIT_ALL_SIZES(pred_high[D207_PRED], highbd_d207); |
96 | 1 | INIT_ALL_SIZES(pred_high[D45_PRED], highbd_d45); |
97 | 1 | INIT_ALL_SIZES(pred_high[D63_PRED], highbd_d63); |
98 | 1 | INIT_ALL_SIZES(pred_high[D117_PRED], highbd_d117); |
99 | 1 | INIT_ALL_SIZES(pred_high[D135_PRED], highbd_d135); |
100 | 1 | INIT_ALL_SIZES(pred_high[D153_PRED], highbd_d153); |
101 | 1 | INIT_ALL_SIZES(pred_high[TM_PRED], highbd_tm); |
102 | | |
103 | 1 | INIT_ALL_SIZES(dc_pred_high[0][0], highbd_dc_128); |
104 | 1 | INIT_ALL_SIZES(dc_pred_high[0][1], highbd_dc_top); |
105 | 1 | INIT_ALL_SIZES(dc_pred_high[1][0], highbd_dc_left); |
106 | 1 | INIT_ALL_SIZES(dc_pred_high[1][1], highbd_dc); |
107 | 1 | #endif // CONFIG_VP9_HIGHBITDEPTH |
108 | | |
109 | 1 | #undef intra_pred_allsizes |
110 | 1 | } |
111 | | |
112 | | #if CONFIG_VP9_HIGHBITDEPTH |
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 | 0 | int left_available, int right_available, int x, int y, int plane, int bd) { |
117 | 0 | int i; |
118 | 0 | uint16_t *dst = CONVERT_TO_SHORTPTR(dst8); |
119 | 0 | uint16_t *ref = CONVERT_TO_SHORTPTR(ref8); |
120 | 0 | DECLARE_ALIGNED(16, uint16_t, left_col[32]); |
121 | 0 | DECLARE_ALIGNED(16, uint16_t, above_data[64 + 16]); |
122 | 0 | uint16_t *above_row = above_data + 16; |
123 | 0 | const uint16_t *const_above_row = above_row; |
124 | 0 | const int bs = 4 << tx_size; |
125 | 0 | int frame_width, frame_height; |
126 | 0 | int x0, y0; |
127 | 0 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
128 | 0 | const int need_left = extend_modes[mode] & NEED_LEFT; |
129 | 0 | const int need_above = extend_modes[mode] & NEED_ABOVE; |
130 | 0 | const int need_aboveright = extend_modes[mode] & NEED_ABOVERIGHT; |
131 | 0 | 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 | 0 | if (plane == 0) { |
141 | 0 | frame_width = xd->cur_buf->y_width; |
142 | 0 | frame_height = xd->cur_buf->y_height; |
143 | 0 | } else { |
144 | 0 | frame_width = xd->cur_buf->uv_width; |
145 | 0 | frame_height = xd->cur_buf->uv_height; |
146 | 0 | } |
147 | | |
148 | | // Get block position in current frame. |
149 | 0 | x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x; |
150 | 0 | y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y; |
151 | | |
152 | | // NEED_LEFT |
153 | 0 | if (need_left) { |
154 | 0 | if (left_available) { |
155 | 0 | if (xd->mb_to_bottom_edge < 0) { |
156 | | /* slower path if the block needs border extension */ |
157 | 0 | if (y0 + bs <= frame_height) { |
158 | 0 | for (i = 0; i < bs; ++i) left_col[i] = ref[i * ref_stride - 1]; |
159 | 0 | } else { |
160 | 0 | const int extend_bottom = frame_height - y0; |
161 | 0 | for (i = 0; i < extend_bottom; ++i) |
162 | 0 | left_col[i] = ref[i * ref_stride - 1]; |
163 | 0 | for (; i < bs; ++i) |
164 | 0 | left_col[i] = ref[(extend_bottom - 1) * ref_stride - 1]; |
165 | 0 | } |
166 | 0 | } else { |
167 | | /* faster path if the block does not need extension */ |
168 | 0 | for (i = 0; i < bs; ++i) left_col[i] = ref[i * ref_stride - 1]; |
169 | 0 | } |
170 | 0 | } else { |
171 | 0 | vpx_memset16(left_col, base + 1, bs); |
172 | 0 | } |
173 | 0 | } |
174 | | |
175 | | // NEED_ABOVE |
176 | 0 | if (need_above) { |
177 | 0 | if (up_available) { |
178 | 0 | const uint16_t *above_ref = ref - ref_stride; |
179 | 0 | if (xd->mb_to_right_edge < 0) { |
180 | | /* slower path if the block needs border extension */ |
181 | 0 | if (x0 + bs <= frame_width) { |
182 | 0 | memcpy(above_row, above_ref, bs * sizeof(above_row[0])); |
183 | 0 | } else if (x0 <= frame_width) { |
184 | 0 | const int r = frame_width - x0; |
185 | 0 | memcpy(above_row, above_ref, r * sizeof(above_row[0])); |
186 | 0 | vpx_memset16(above_row + r, above_row[r - 1], x0 + bs - frame_width); |
187 | 0 | } |
188 | 0 | } else { |
189 | | /* faster path if the block does not need extension */ |
190 | 0 | if (bs == 4 && right_available && left_available) { |
191 | 0 | const_above_row = above_ref; |
192 | 0 | } else { |
193 | 0 | memcpy(above_row, above_ref, bs * sizeof(above_row[0])); |
194 | 0 | } |
195 | 0 | } |
196 | 0 | above_row[-1] = left_available ? above_ref[-1] : (base + 1); |
197 | 0 | } else { |
198 | 0 | vpx_memset16(above_row, base - 1, bs); |
199 | 0 | above_row[-1] = base - 1; |
200 | 0 | } |
201 | 0 | } |
202 | | |
203 | | // NEED_ABOVERIGHT |
204 | 0 | if (need_aboveright) { |
205 | 0 | if (up_available) { |
206 | 0 | const uint16_t *above_ref = ref - ref_stride; |
207 | 0 | if (xd->mb_to_right_edge < 0) { |
208 | | /* slower path if the block needs border extension */ |
209 | 0 | if (x0 + 2 * bs <= frame_width) { |
210 | 0 | if (right_available && bs == 4) { |
211 | 0 | memcpy(above_row, above_ref, 2 * bs * sizeof(above_row[0])); |
212 | 0 | } else { |
213 | 0 | memcpy(above_row, above_ref, bs * sizeof(above_row[0])); |
214 | 0 | vpx_memset16(above_row + bs, above_row[bs - 1], bs); |
215 | 0 | } |
216 | 0 | } else if (x0 + bs <= frame_width) { |
217 | 0 | const int r = frame_width - x0; |
218 | 0 | if (right_available && bs == 4) { |
219 | 0 | memcpy(above_row, above_ref, r * sizeof(above_row[0])); |
220 | 0 | vpx_memset16(above_row + r, above_row[r - 1], |
221 | 0 | x0 + 2 * bs - frame_width); |
222 | 0 | } else { |
223 | 0 | memcpy(above_row, above_ref, bs * sizeof(above_row[0])); |
224 | 0 | vpx_memset16(above_row + bs, above_row[bs - 1], bs); |
225 | 0 | } |
226 | 0 | } else if (x0 <= frame_width) { |
227 | 0 | const int r = frame_width - x0; |
228 | 0 | memcpy(above_row, above_ref, r * sizeof(above_row[0])); |
229 | 0 | vpx_memset16(above_row + r, above_row[r - 1], |
230 | 0 | x0 + 2 * bs - frame_width); |
231 | 0 | } |
232 | 0 | above_row[-1] = left_available ? above_ref[-1] : (base + 1); |
233 | 0 | } else { |
234 | | /* faster path if the block does not need extension */ |
235 | 0 | if (bs == 4 && right_available && left_available) { |
236 | 0 | const_above_row = above_ref; |
237 | 0 | } else { |
238 | 0 | memcpy(above_row, above_ref, bs * sizeof(above_row[0])); |
239 | 0 | if (bs == 4 && right_available) |
240 | 0 | memcpy(above_row + bs, above_ref + bs, bs * sizeof(above_row[0])); |
241 | 0 | else |
242 | 0 | vpx_memset16(above_row + bs, above_row[bs - 1], bs); |
243 | 0 | above_row[-1] = left_available ? above_ref[-1] : (base + 1); |
244 | 0 | } |
245 | 0 | } |
246 | 0 | } else { |
247 | 0 | vpx_memset16(above_row, base - 1, bs * 2); |
248 | 0 | above_row[-1] = base - 1; |
249 | 0 | } |
250 | 0 | } |
251 | | |
252 | | // predict |
253 | 0 | if (mode == DC_PRED) { |
254 | 0 | dc_pred_high[left_available][up_available][tx_size]( |
255 | 0 | dst, dst_stride, const_above_row, left_col, xd->bd); |
256 | 0 | } else { |
257 | 0 | pred_high[mode][tx_size](dst, dst_stride, const_above_row, left_col, |
258 | 0 | xd->bd); |
259 | 0 | } |
260 | 0 | } |
261 | | #endif // CONFIG_VP9_HIGHBITDEPTH |
262 | | |
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 | 440M | int plane) { |
269 | 440M | int i; |
270 | 440M | DECLARE_ALIGNED(16, uint8_t, left_col[32]); |
271 | 440M | DECLARE_ALIGNED(16, uint8_t, above_data[64 + 16]); |
272 | 440M | uint8_t *above_row = above_data + 16; |
273 | 440M | const uint8_t *const_above_row = above_row; |
274 | 440M | const int bs = 4 << tx_size; |
275 | 440M | int frame_width, frame_height; |
276 | 440M | int x0, y0; |
277 | 440M | 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 | 440M | if (plane == 0) { |
288 | 261M | frame_width = xd->cur_buf->y_width; |
289 | 261M | frame_height = xd->cur_buf->y_height; |
290 | 261M | } else { |
291 | 179M | frame_width = xd->cur_buf->uv_width; |
292 | 179M | frame_height = xd->cur_buf->uv_height; |
293 | 179M | } |
294 | | |
295 | | // Get block position in current frame. |
296 | 440M | x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x; |
297 | 440M | y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y; |
298 | | |
299 | | // NEED_LEFT |
300 | 440M | if (extend_modes[mode] & NEED_LEFT) { |
301 | 318M | if (left_available) { |
302 | 283M | if (xd->mb_to_bottom_edge < 0) { |
303 | | /* slower path if the block needs border extension */ |
304 | 1.23M | if (y0 + bs <= frame_height) { |
305 | 4.92M | for (i = 0; i < bs; ++i) left_col[i] = ref[i * ref_stride - 1]; |
306 | 764k | } else { |
307 | 469k | const int extend_bottom = frame_height - y0; |
308 | 4.39M | for (i = 0; i < extend_bottom; ++i) |
309 | 3.92M | left_col[i] = ref[i * ref_stride - 1]; |
310 | 5.13M | for (; i < bs; ++i) |
311 | 4.66M | left_col[i] = ref[(extend_bottom - 1) * ref_stride - 1]; |
312 | 469k | } |
313 | 282M | } else { |
314 | | /* faster path if the block does not need extension */ |
315 | 1.63G | for (i = 0; i < bs; ++i) left_col[i] = ref[i * ref_stride - 1]; |
316 | 282M | } |
317 | 283M | } else { |
318 | 34.9M | memset(left_col, 129, bs); |
319 | 34.9M | } |
320 | 318M | } |
321 | | |
322 | | // NEED_ABOVE |
323 | 440M | if (extend_modes[mode] & NEED_ABOVE) { |
324 | 272M | if (up_available) { |
325 | 237M | const uint8_t *above_ref = ref - ref_stride; |
326 | 237M | if (xd->mb_to_right_edge < 0) { |
327 | | /* slower path if the block needs border extension */ |
328 | 789k | if (x0 + bs <= frame_width) { |
329 | 451k | memcpy(above_row, above_ref, bs); |
330 | 451k | } else if (x0 <= frame_width) { |
331 | 338k | const int r = frame_width - x0; |
332 | 338k | memcpy(above_row, above_ref, r); |
333 | 338k | memset(above_row + r, above_row[r - 1], x0 + bs - frame_width); |
334 | 338k | } |
335 | 236M | } else { |
336 | | /* faster path if the block does not need extension */ |
337 | 236M | if (bs == 4 && right_available && left_available) { |
338 | 69.4M | const_above_row = above_ref; |
339 | 166M | } else { |
340 | 166M | memcpy(above_row, above_ref, bs); |
341 | 166M | } |
342 | 236M | } |
343 | 237M | above_row[-1] = left_available ? above_ref[-1] : 129; |
344 | 237M | } else { |
345 | 35.6M | memset(above_row, 127, bs); |
346 | 35.6M | above_row[-1] = 127; |
347 | 35.6M | } |
348 | 272M | } |
349 | | |
350 | | // NEED_ABOVERIGHT |
351 | 440M | if (extend_modes[mode] & NEED_ABOVERIGHT) { |
352 | 78.1M | if (up_available) { |
353 | 68.4M | const uint8_t *above_ref = ref - ref_stride; |
354 | 68.4M | if (xd->mb_to_right_edge < 0) { |
355 | | /* slower path if the block needs border extension */ |
356 | 183k | if (x0 + 2 * bs <= frame_width) { |
357 | 51.7k | if (right_available && bs == 4) { |
358 | 51.4k | memcpy(above_row, above_ref, 2 * bs); |
359 | 51.4k | } else { |
360 | 304 | memcpy(above_row, above_ref, bs); |
361 | 304 | memset(above_row + bs, above_row[bs - 1], bs); |
362 | 304 | } |
363 | 132k | } else if (x0 + bs <= frame_width) { |
364 | 45.2k | const int r = frame_width - x0; |
365 | 45.2k | if (right_available && bs == 4) { |
366 | 18.9k | memcpy(above_row, above_ref, r); |
367 | 18.9k | memset(above_row + r, above_row[r - 1], x0 + 2 * bs - frame_width); |
368 | 26.2k | } else { |
369 | 26.2k | memcpy(above_row, above_ref, bs); |
370 | 26.2k | memset(above_row + bs, above_row[bs - 1], bs); |
371 | 26.2k | } |
372 | 86.8k | } else if (x0 <= frame_width) { |
373 | 86.8k | const int r = frame_width - x0; |
374 | 86.8k | memcpy(above_row, above_ref, r); |
375 | 86.8k | memset(above_row + r, above_row[r - 1], x0 + 2 * bs - frame_width); |
376 | 86.8k | } |
377 | 68.3M | } else { |
378 | | /* faster path if the block does not need extension */ |
379 | 68.3M | if (bs == 4 && right_available && left_available) { |
380 | 18.8M | const_above_row = above_ref; |
381 | 49.4M | } else { |
382 | 49.4M | memcpy(above_row, above_ref, bs); |
383 | 49.4M | if (bs == 4 && right_available) |
384 | 3.11M | memcpy(above_row + bs, above_ref + bs, bs); |
385 | 46.3M | else |
386 | 46.3M | memset(above_row + bs, above_row[bs - 1], bs); |
387 | 49.4M | } |
388 | 68.3M | } |
389 | 68.4M | above_row[-1] = left_available ? above_ref[-1] : 129; |
390 | 68.4M | } else { |
391 | 9.69M | memset(above_row, 127, bs * 2); |
392 | 9.69M | above_row[-1] = 127; |
393 | 9.69M | } |
394 | 78.1M | } |
395 | | |
396 | | // predict |
397 | 440M | if (mode == DC_PRED) { |
398 | 71.3M | dc_pred[left_available][up_available][tx_size](dst, dst_stride, |
399 | 71.3M | const_above_row, left_col); |
400 | 369M | } else { |
401 | 369M | pred[mode][tx_size](dst, dst_stride, const_above_row, left_col); |
402 | 369M | } |
403 | 440M | } |
404 | | |
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 | 440M | int aoff, int loff, int plane) { |
409 | 440M | const int bw = (1 << bwl_in); |
410 | 440M | const int txw = (1 << tx_size); |
411 | 440M | const int have_top = loff || (xd->above_mi != NULL); |
412 | 440M | const int have_left = aoff || (xd->left_mi != NULL); |
413 | 440M | const int have_right = (aoff + txw) < bw; |
414 | 440M | const int x = aoff * 4; |
415 | 440M | const int y = loff * 4; |
416 | | |
417 | 440M | #if CONFIG_VP9_HIGHBITDEPTH |
418 | 440M | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
419 | 0 | build_intra_predictors_high(xd, ref, ref_stride, dst, dst_stride, mode, |
420 | 0 | tx_size, have_top, have_left, have_right, x, y, |
421 | 0 | plane, xd->bd); |
422 | 0 | return; |
423 | 0 | } |
424 | 440M | #endif |
425 | 440M | build_intra_predictors(xd, ref, ref_stride, dst, dst_stride, mode, tx_size, |
426 | 440M | have_top, have_left, have_right, x, y, plane); |
427 | 440M | } |
428 | | |
429 | 1 | void vp9_init_intra_predictors(void) { |
430 | 1 | once(vp9_init_intra_predictors_internal); |
431 | 1 | } |