2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
13 #include "./vpx_scale_rtcd.h"
14 #include "./vpx_config.h"
16 #include "vpx/vpx_integer.h"
18 #include "vp9/common/vp9_blockd.h"
19 #include "vp9/common/vp9_filter.h"
20 #include "vp9/common/vp9_reconinter.h"
21 #include "vp9/common/vp9_reconintra.h"
23 static void build_mc_border(const uint8_t *src, int src_stride,
24 uint8_t *dst, int dst_stride,
25 int x, int y, int b_w, int b_h, int w, int h) {
26 // Get a pointer to the start of the real data for this row.
27 const uint8_t *ref_row = src - x - y * src_stride;
30 ref_row += (h - 1) * src_stride;
32 ref_row += y * src_stride;
36 int left = x < 0 ? -x : 0;
47 copy = b_w - left - right;
50 memset(dst, ref_row[0], left);
53 memcpy(dst + left, ref_row + x + left, copy);
56 memset(dst + left + copy, ref_row[w - 1], right);
62 ref_row += src_stride;
66 #if CONFIG_VP9_HIGHBITDEPTH
67 static void high_build_mc_border(const uint8_t *src8, int src_stride,
68 uint16_t *dst, int dst_stride,
69 int x, int y, int b_w, int b_h,
71 // Get a pointer to the start of the real data for this row.
72 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
73 const uint16_t *ref_row = src - x - y * src_stride;
76 ref_row += (h - 1) * src_stride;
78 ref_row += y * src_stride;
82 int left = x < 0 ? -x : 0;
93 copy = b_w - left - right;
96 vpx_memset16(dst, ref_row[0], left);
99 memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
102 vpx_memset16(dst + left + copy, ref_row[w - 1], right);
108 ref_row += src_stride;
111 #endif // CONFIG_VP9_HIGHBITDEPTH
113 static void inter_predictor(const uint8_t *src, int src_stride,
114 uint8_t *dst, int dst_stride,
117 const struct scale_factors *sf,
118 int w, int h, int ref,
119 const InterpKernel *kernel,
121 sf->predict[subpel_x != 0][subpel_y != 0][ref](
122 src, src_stride, dst, dst_stride,
123 kernel[subpel_x], xs, kernel[subpel_y], ys, w, h);
126 void vp9_build_inter_predictor(const uint8_t *src, int src_stride,
127 uint8_t *dst, int dst_stride,
129 const struct scale_factors *sf,
130 int w, int h, int ref,
131 const InterpKernel *kernel,
132 enum mv_precision precision,
134 const int is_q4 = precision == MV_PRECISION_Q4;
135 const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2,
136 is_q4 ? src_mv->col : src_mv->col * 2 };
137 MV32 mv = vp9_scale_mv(&mv_q4, x, y, sf);
138 const int subpel_x = mv.col & SUBPEL_MASK;
139 const int subpel_y = mv.row & SUBPEL_MASK;
141 src += (mv.row >> SUBPEL_BITS) * src_stride + (mv.col >> SUBPEL_BITS);
143 inter_predictor(src, src_stride, dst, dst_stride, subpel_x, subpel_y,
144 sf, w, h, ref, kernel, sf->x_step_q4, sf->y_step_q4);
147 #if CONFIG_VP9_HIGHBITDEPTH
148 static void high_inter_predictor(const uint8_t *src, int src_stride,
149 uint8_t *dst, int dst_stride,
152 const struct scale_factors *sf,
153 int w, int h, int ref,
154 const InterpKernel *kernel,
155 int xs, int ys, int bd) {
156 sf->highbd_predict[subpel_x != 0][subpel_y != 0][ref](
157 src, src_stride, dst, dst_stride,
158 kernel[subpel_x], xs, kernel[subpel_y], ys, w, h, bd);
161 void vp9_highbd_build_inter_predictor(const uint8_t *src, int src_stride,
162 uint8_t *dst, int dst_stride,
164 const struct scale_factors *sf,
165 int w, int h, int ref,
166 const InterpKernel *kernel,
167 enum mv_precision precision,
168 int x, int y, int bd) {
169 const int is_q4 = precision == MV_PRECISION_Q4;
170 const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2,
171 is_q4 ? src_mv->col : src_mv->col * 2 };
172 MV32 mv = vp9_scale_mv(&mv_q4, x, y, sf);
173 const int subpel_x = mv.col & SUBPEL_MASK;
174 const int subpel_y = mv.row & SUBPEL_MASK;
176 src += (mv.row >> SUBPEL_BITS) * src_stride + (mv.col >> SUBPEL_BITS);
178 high_inter_predictor(src, src_stride, dst, dst_stride, subpel_x, subpel_y,
179 sf, w, h, ref, kernel, sf->x_step_q4, sf->y_step_q4, bd);
181 #endif // CONFIG_VP9_HIGHBITDEPTH
183 static INLINE int round_mv_comp_q4(int value) {
184 return (value < 0 ? value - 2 : value + 2) / 4;
187 static MV mi_mv_pred_q4(const MODE_INFO *mi, int idx) {
188 MV res = { round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.row +
189 mi->bmi[1].as_mv[idx].as_mv.row +
190 mi->bmi[2].as_mv[idx].as_mv.row +
191 mi->bmi[3].as_mv[idx].as_mv.row),
192 round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.col +
193 mi->bmi[1].as_mv[idx].as_mv.col +
194 mi->bmi[2].as_mv[idx].as_mv.col +
195 mi->bmi[3].as_mv[idx].as_mv.col) };
199 static INLINE int round_mv_comp_q2(int value) {
200 return (value < 0 ? value - 1 : value + 1) / 2;
203 static MV mi_mv_pred_q2(const MODE_INFO *mi, int idx, int block0, int block1) {
204 MV res = { round_mv_comp_q2(mi->bmi[block0].as_mv[idx].as_mv.row +
205 mi->bmi[block1].as_mv[idx].as_mv.row),
206 round_mv_comp_q2(mi->bmi[block0].as_mv[idx].as_mv.col +
207 mi->bmi[block1].as_mv[idx].as_mv.col) };
211 // TODO(jkoleszar): yet another mv clamping function :-(
212 MV clamp_mv_to_umv_border_sb(const MACROBLOCKD *xd, const MV *src_mv,
213 int bw, int bh, int ss_x, int ss_y) {
214 // If the MV points so far into the UMV border that no visible pixels
215 // are used for reconstruction, the subpel part of the MV can be
216 // discarded and the MV limited to 16 pixels with equivalent results.
217 const int spel_left = (VP9_INTERP_EXTEND + bw) << SUBPEL_BITS;
218 const int spel_right = spel_left - SUBPEL_SHIFTS;
219 const int spel_top = (VP9_INTERP_EXTEND + bh) << SUBPEL_BITS;
220 const int spel_bottom = spel_top - SUBPEL_SHIFTS;
222 src_mv->row * (1 << (1 - ss_y)),
223 src_mv->col * (1 << (1 - ss_x))
228 clamp_mv(&clamped_mv,
229 xd->mb_to_left_edge * (1 << (1 - ss_x)) - spel_left,
230 xd->mb_to_right_edge * (1 << (1 - ss_x)) + spel_right,
231 xd->mb_to_top_edge * (1 << (1 - ss_y)) - spel_top,
232 xd->mb_to_bottom_edge * (1 << (1 - ss_y)) + spel_bottom);
237 static MV average_split_mvs(const struct macroblockd_plane *pd,
238 const MODE_INFO *mi, int ref, int block) {
239 const int ss_idx = ((pd->subsampling_x > 0) << 1) | (pd->subsampling_y > 0);
243 res = mi->bmi[block].as_mv[ref].as_mv;
246 res = mi_mv_pred_q2(mi, ref, block, block + 2);
249 res = mi_mv_pred_q2(mi, ref, block, block + 1);
252 res = mi_mv_pred_q4(mi, ref);
255 assert(ss_idx <= 3 || ss_idx >= 0);
260 static void build_inter_predictors(MACROBLOCKD *xd, int plane, int block,
262 int x, int y, int w, int h,
263 int mi_x, int mi_y) {
264 struct macroblockd_plane *const pd = &xd->plane[plane];
265 const MODE_INFO *mi = xd->mi[0].src_mi;
266 const int is_compound = has_second_ref(&mi->mbmi);
267 const InterpKernel *kernel = vp9_get_interp_kernel(mi->mbmi.interp_filter);
270 for (ref = 0; ref < 1 + is_compound; ++ref) {
271 const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
272 struct buf_2d *const pre_buf = &pd->pre[ref];
273 struct buf_2d *const dst_buf = &pd->dst;
274 uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
275 const MV mv = mi->mbmi.sb_type < BLOCK_8X8
276 ? average_split_mvs(pd, mi, ref, block)
277 : mi->mbmi.mv[ref].as_mv;
279 // TODO(jkoleszar): This clamping is done in the incorrect place for the
280 // scaling case. It needs to be done on the scaled MV, not the pre-scaling
281 // MV. Note however that it performs the subsampling aware scaling so
282 // that the result is always q4.
283 // mv_precision precision is MV_PRECISION_Q4.
284 const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, &mv, bw, bh,
290 int xs, ys, subpel_x, subpel_y;
291 const int is_scaled = vp9_is_scaled(sf);
294 pre = pre_buf->buf + scaled_buffer_offset(x, y, pre_buf->stride, sf);
295 scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
299 pre = pre_buf->buf + (y * pre_buf->stride + x);
300 scaled_mv.row = mv_q4.row;
301 scaled_mv.col = mv_q4.col;
304 subpel_x = scaled_mv.col & SUBPEL_MASK;
305 subpel_y = scaled_mv.row & SUBPEL_MASK;
306 pre += (scaled_mv.row >> SUBPEL_BITS) * pre_buf->stride
307 + (scaled_mv.col >> SUBPEL_BITS);
309 #if CONFIG_VP9_HIGHBITDEPTH
310 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
311 high_inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
312 subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys,
315 inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
316 subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys);
319 inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
320 subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys);
321 #endif // CONFIG_VP9_HIGHBITDEPTH
325 static void build_inter_predictors_for_planes(MACROBLOCKD *xd, BLOCK_SIZE bsize,
326 int mi_row, int mi_col,
327 int plane_from, int plane_to) {
329 const int mi_x = mi_col * MI_SIZE;
330 const int mi_y = mi_row * MI_SIZE;
331 for (plane = plane_from; plane <= plane_to; ++plane) {
332 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize,
334 const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
335 const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
336 const int bw = 4 * num_4x4_w;
337 const int bh = 4 * num_4x4_h;
339 if (xd->mi[0].src_mi->mbmi.sb_type < BLOCK_8X8) {
341 assert(bsize == BLOCK_8X8);
342 for (y = 0; y < num_4x4_h; ++y)
343 for (x = 0; x < num_4x4_w; ++x)
344 build_inter_predictors(xd, plane, i++, bw, bh,
345 4 * x, 4 * y, 4, 4, mi_x, mi_y);
347 build_inter_predictors(xd, plane, 0, bw, bh,
348 0, 0, bw, bh, mi_x, mi_y);
353 void vp9_build_inter_predictors_sby(MACROBLOCKD *xd, int mi_row, int mi_col,
355 build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0, 0);
357 void vp9_build_inter_predictors_sbuv(MACROBLOCKD *xd, int mi_row, int mi_col,
359 build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 1,
362 void vp9_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
364 build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0,
368 // TODO(jingning): This function serves as a placeholder for decoder prediction
369 // using on demand border extension. It should be moved to /decoder/ directory.
370 static void dec_build_inter_predictors(MACROBLOCKD *xd, int plane, int block,
372 int x, int y, int w, int h,
373 int mi_x, int mi_y) {
374 struct macroblockd_plane *const pd = &xd->plane[plane];
375 const MODE_INFO *mi = xd->mi[0].src_mi;
376 const int is_compound = has_second_ref(&mi->mbmi);
377 const InterpKernel *kernel = vp9_get_interp_kernel(mi->mbmi.interp_filter);
380 for (ref = 0; ref < 1 + is_compound; ++ref) {
381 const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
382 struct buf_2d *const pre_buf = &pd->pre[ref];
383 struct buf_2d *const dst_buf = &pd->dst;
384 uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
385 const MV mv = mi->mbmi.sb_type < BLOCK_8X8
386 ? average_split_mvs(pd, mi, ref, block)
387 : mi->mbmi.mv[ref].as_mv;
389 const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, &mv, bw, bh,
394 int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height, buf_stride,
396 uint8_t *ref_frame, *buf_ptr;
397 const YV12_BUFFER_CONFIG *ref_buf = xd->block_refs[ref]->buf;
398 const int is_scaled = vp9_is_scaled(sf);
400 // Get reference frame pointer, width and height.
402 frame_width = ref_buf->y_crop_width;
403 frame_height = ref_buf->y_crop_height;
404 ref_frame = ref_buf->y_buffer;
406 frame_width = ref_buf->uv_crop_width;
407 frame_height = ref_buf->uv_crop_height;
408 ref_frame = plane == 1 ? ref_buf->u_buffer : ref_buf->v_buffer;
412 // Co-ordinate of containing block to pixel precision.
413 int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
414 int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
416 // Co-ordinate of the block to 1/16th pixel precision.
417 x0_16 = (x_start + x) << SUBPEL_BITS;
418 y0_16 = (y_start + y) << SUBPEL_BITS;
420 // Co-ordinate of current block in reference frame
421 // to 1/16th pixel precision.
422 x0_16 = sf->scale_value_x(x0_16, sf);
423 y0_16 = sf->scale_value_y(y0_16, sf);
425 // Map the top left corner of the block into the reference frame.
426 x0 = sf->scale_value_x(x_start + x, sf);
427 y0 = sf->scale_value_y(y_start + y, sf);
429 // Scale the MV and incorporate the sub-pixel offset of the block
430 // in the reference frame.
431 scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
435 // Co-ordinate of containing block to pixel precision.
436 x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
437 y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
439 // Co-ordinate of the block to 1/16th pixel precision.
440 x0_16 = x0 << SUBPEL_BITS;
441 y0_16 = y0 << SUBPEL_BITS;
443 scaled_mv.row = mv_q4.row;
444 scaled_mv.col = mv_q4.col;
447 subpel_x = scaled_mv.col & SUBPEL_MASK;
448 subpel_y = scaled_mv.row & SUBPEL_MASK;
450 // Calculate the top left corner of the best matching block in the
452 x0 += scaled_mv.col >> SUBPEL_BITS;
453 y0 += scaled_mv.row >> SUBPEL_BITS;
454 x0_16 += scaled_mv.col;
455 y0_16 += scaled_mv.row;
457 // Get reference block pointer.
458 buf_ptr = ref_frame + y0 * pre_buf->stride + x0;
459 buf_stride = pre_buf->stride;
461 // Do border extension if there is motion or the
462 // width/height is not a multiple of 8 pixels.
463 if (is_scaled || scaled_mv.col || scaled_mv.row ||
464 (frame_width & 0x7) || (frame_height & 0x7)) {
465 // Get reference block bottom right coordinate.
466 int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1;
467 int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1;
468 int x_pad = 0, y_pad = 0;
470 if (subpel_x || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
471 x0 -= VP9_INTERP_EXTEND - 1;
472 x1 += VP9_INTERP_EXTEND;
476 if (subpel_y || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
477 y0 -= VP9_INTERP_EXTEND - 1;
478 y1 += VP9_INTERP_EXTEND;
482 // Skip border extension if block is inside the frame.
483 if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width - 1 ||
484 y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) {
485 uint8_t *buf_ptr1 = ref_frame + y0 * pre_buf->stride + x0;
486 // Extend the border.
487 #if CONFIG_VP9_HIGHBITDEPTH
488 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
489 high_build_mc_border(buf_ptr1,
499 buf_stride = x1 - x0 + 1;
500 buf_ptr = CONVERT_TO_BYTEPTR(xd->mc_buf_high) +
501 y_pad * 3 * buf_stride + x_pad * 3;
503 build_mc_border(buf_ptr1,
513 buf_stride = x1 - x0 + 1;
514 buf_ptr = xd->mc_buf + y_pad * 3 * buf_stride + x_pad * 3;
517 build_mc_border(buf_ptr1,
527 buf_stride = x1 - x0 + 1;
528 buf_ptr = xd->mc_buf + y_pad * 3 * buf_stride + x_pad * 3;
529 #endif // CONFIG_VP9_HIGHBITDEPTH
533 #if CONFIG_VP9_HIGHBITDEPTH
534 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
535 high_inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
536 subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
538 inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
539 subpel_y, sf, w, h, ref, kernel, xs, ys);
542 inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
543 subpel_y, sf, w, h, ref, kernel, xs, ys);
544 #endif // CONFIG_VP9_HIGHBITDEPTH
548 void vp9_dec_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
551 const int mi_x = mi_col * MI_SIZE;
552 const int mi_y = mi_row * MI_SIZE;
553 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
554 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize,
556 const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
557 const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
558 const int bw = 4 * num_4x4_w;
559 const int bh = 4 * num_4x4_h;
561 if (xd->mi[0].src_mi->mbmi.sb_type < BLOCK_8X8) {
563 assert(bsize == BLOCK_8X8);
564 for (y = 0; y < num_4x4_h; ++y)
565 for (x = 0; x < num_4x4_w; ++x)
566 dec_build_inter_predictors(xd, plane, i++, bw, bh,
567 4 * x, 4 * y, 4, 4, mi_x, mi_y);
569 dec_build_inter_predictors(xd, plane, 0, bw, bh,
570 0, 0, bw, bh, mi_x, mi_y);
575 void vp9_setup_dst_planes(struct macroblockd_plane planes[MAX_MB_PLANE],
576 const YV12_BUFFER_CONFIG *src,
577 int mi_row, int mi_col) {
578 uint8_t *const buffers[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
580 const int strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
584 for (i = 0; i < MAX_MB_PLANE; ++i) {
585 struct macroblockd_plane *const pd = &planes[i];
586 setup_pred_plane(&pd->dst, buffers[i], strides[i], mi_row, mi_col, NULL,
587 pd->subsampling_x, pd->subsampling_y);
591 void vp9_setup_pre_planes(MACROBLOCKD *xd, int idx,
592 const YV12_BUFFER_CONFIG *src,
593 int mi_row, int mi_col,
594 const struct scale_factors *sf) {
597 uint8_t *const buffers[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
599 const int strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
602 for (i = 0; i < MAX_MB_PLANE; ++i) {
603 struct macroblockd_plane *const pd = &xd->plane[i];
604 setup_pred_plane(&pd->pre[idx], buffers[i], strides[i], mi_row, mi_col,
605 sf, pd->subsampling_x, pd->subsampling_y);