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->high_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_high_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;
292 if (vp9_is_scaled(sf)) {
293 pre = pre_buf->buf + scaled_buffer_offset(x, y, pre_buf->stride, sf);
294 scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
298 pre = pre_buf->buf + (y * pre_buf->stride + x);
299 scaled_mv.row = mv_q4.row;
300 scaled_mv.col = mv_q4.col;
303 subpel_x = scaled_mv.col & SUBPEL_MASK;
304 subpel_y = scaled_mv.row & SUBPEL_MASK;
305 pre += (scaled_mv.row >> SUBPEL_BITS) * pre_buf->stride
306 + (scaled_mv.col >> SUBPEL_BITS);
308 #if CONFIG_VP9_HIGHBITDEPTH
309 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
310 high_inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
311 subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys,
314 inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
315 subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys);
318 inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
319 subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys);
320 #endif // CONFIG_VP9_HIGHBITDEPTH
324 static void build_inter_predictors_for_planes(MACROBLOCKD *xd, BLOCK_SIZE bsize,
325 int mi_row, int mi_col,
326 int plane_from, int plane_to) {
328 const int mi_x = mi_col * MI_SIZE;
329 const int mi_y = mi_row * MI_SIZE;
330 for (plane = plane_from; plane <= plane_to; ++plane) {
331 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize,
333 const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
334 const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
335 const int bw = 4 * num_4x4_w;
336 const int bh = 4 * num_4x4_h;
338 if (xd->mi[0].src_mi->mbmi.sb_type < BLOCK_8X8) {
340 assert(bsize == BLOCK_8X8);
341 for (y = 0; y < num_4x4_h; ++y)
342 for (x = 0; x < num_4x4_w; ++x)
343 build_inter_predictors(xd, plane, i++, bw, bh,
344 4 * x, 4 * y, 4, 4, mi_x, mi_y);
346 build_inter_predictors(xd, plane, 0, bw, bh,
347 0, 0, bw, bh, mi_x, mi_y);
352 void vp9_build_inter_predictors_sby(MACROBLOCKD *xd, int mi_row, int mi_col,
354 build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0, 0);
356 void vp9_build_inter_predictors_sbuv(MACROBLOCKD *xd, int mi_row, int mi_col,
358 build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 1,
361 void vp9_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
363 build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0,
367 // TODO(jingning): This function serves as a placeholder for decoder prediction
368 // using on demand border extension. It should be moved to /decoder/ directory.
369 static void dec_build_inter_predictors(MACROBLOCKD *xd, int plane, int block,
371 int x, int y, int w, int h,
372 int mi_x, int mi_y) {
373 struct macroblockd_plane *const pd = &xd->plane[plane];
374 const MODE_INFO *mi = xd->mi[0].src_mi;
375 const int is_compound = has_second_ref(&mi->mbmi);
376 const InterpKernel *kernel = vp9_get_interp_kernel(mi->mbmi.interp_filter);
379 for (ref = 0; ref < 1 + is_compound; ++ref) {
380 const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
381 struct buf_2d *const pre_buf = &pd->pre[ref];
382 struct buf_2d *const dst_buf = &pd->dst;
383 uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
384 const MV mv = mi->mbmi.sb_type < BLOCK_8X8
385 ? average_split_mvs(pd, mi, ref, block)
386 : mi->mbmi.mv[ref].as_mv;
389 // TODO(jkoleszar): This clamping is done in the incorrect place for the
390 // scaling case. It needs to be done on the scaled MV, not the pre-scaling
391 // MV. Note however that it performs the subsampling aware scaling so
392 // that the result is always q4.
393 // mv_precision precision is MV_PRECISION_Q4.
394 const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, &mv, bw, bh,
399 int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height, buf_stride,
401 uint8_t *ref_frame, *buf_ptr;
402 const YV12_BUFFER_CONFIG *ref_buf = xd->block_refs[ref]->buf;
404 // Get reference frame pointer, width and height.
406 frame_width = ref_buf->y_crop_width;
407 frame_height = ref_buf->y_crop_height;
408 ref_frame = ref_buf->y_buffer;
410 frame_width = ref_buf->uv_crop_width;
411 frame_height = ref_buf->uv_crop_height;
412 ref_frame = plane == 1 ? ref_buf->u_buffer : ref_buf->v_buffer;
415 if (vp9_is_scaled(sf)) {
416 // Co-ordinate of containing block to pixel precision.
417 int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
418 int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
420 // Co-ordinate of the block to 1/16th pixel precision.
421 x0_16 = (x_start + x) << SUBPEL_BITS;
422 y0_16 = (y_start + y) << SUBPEL_BITS;
424 // Co-ordinate of current block in reference frame
425 // to 1/16th pixel precision.
426 x0_16 = sf->scale_value_x(x0_16, sf);
427 y0_16 = sf->scale_value_y(y0_16, sf);
429 // Map the top left corner of the block into the reference frame.
430 x0 = sf->scale_value_x(x_start + x, sf);
431 y0 = sf->scale_value_y(y_start + y, sf);
433 // Scale the MV and incorporate the sub-pixel offset of the block
434 // in the reference frame.
435 scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
439 // Co-ordinate of containing block to pixel precision.
440 x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
441 y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
443 // Co-ordinate of the block to 1/16th pixel precision.
444 x0_16 = x0 << SUBPEL_BITS;
445 y0_16 = y0 << SUBPEL_BITS;
447 scaled_mv.row = mv_q4.row;
448 scaled_mv.col = mv_q4.col;
451 subpel_x = scaled_mv.col & SUBPEL_MASK;
452 subpel_y = scaled_mv.row & SUBPEL_MASK;
454 // Calculate the top left corner of the best matching block in the reference frame.
455 x0 += scaled_mv.col >> SUBPEL_BITS;
456 y0 += scaled_mv.row >> SUBPEL_BITS;
457 x0_16 += scaled_mv.col;
458 y0_16 += scaled_mv.row;
460 // Get reference block pointer.
461 buf_ptr = ref_frame + y0 * pre_buf->stride + x0;
462 buf_stride = pre_buf->stride;
464 // Do border extension if there is motion or the
465 // width/height is not a multiple of 8 pixels.
466 if (scaled_mv.col || scaled_mv.row ||
467 (frame_width & 0x7) || (frame_height & 0x7)) {
468 // Get reference block bottom right coordinate.
469 int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1;
470 int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1;
471 int x_pad = 0, y_pad = 0;
473 if (subpel_x || (sf->x_step_q4 & SUBPEL_MASK)) {
474 x0 -= VP9_INTERP_EXTEND - 1;
475 x1 += VP9_INTERP_EXTEND;
479 if (subpel_y || (sf->y_step_q4 & SUBPEL_MASK)) {
480 y0 -= VP9_INTERP_EXTEND - 1;
481 y1 += VP9_INTERP_EXTEND;
485 // Skip border extension if block is inside the frame.
486 if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width - 1 ||
487 y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) {
488 uint8_t *buf_ptr1 = ref_frame + y0 * pre_buf->stride + x0;
489 // Extend the border.
490 #if CONFIG_VP9_HIGHBITDEPTH
491 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
492 high_build_mc_border(buf_ptr1,
502 buf_stride = x1 - x0 + 1;
503 buf_ptr = CONVERT_TO_BYTEPTR(xd->mc_buf_high) +
504 y_pad * 3 * buf_stride + x_pad * 3;
506 build_mc_border(buf_ptr1,
516 buf_stride = x1 - x0 + 1;
517 buf_ptr = xd->mc_buf + y_pad * 3 * buf_stride + x_pad * 3;
520 build_mc_border(buf_ptr1,
530 buf_stride = x1 - x0 + 1;
531 buf_ptr = xd->mc_buf + y_pad * 3 * buf_stride + x_pad * 3;
532 #endif // CONFIG_VP9_HIGHBITDEPTH
536 #if CONFIG_VP9_HIGHBITDEPTH
537 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
538 high_inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
539 subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
541 inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
542 subpel_y, sf, w, h, ref, kernel, xs, ys);
545 inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
546 subpel_y, sf, w, h, ref, kernel, xs, ys);
547 #endif // CONFIG_VP9_HIGHBITDEPTH
551 void vp9_dec_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
554 const int mi_x = mi_col * MI_SIZE;
555 const int mi_y = mi_row * MI_SIZE;
556 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
557 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize,
559 const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
560 const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
561 const int bw = 4 * num_4x4_w;
562 const int bh = 4 * num_4x4_h;
564 if (xd->mi[0].src_mi->mbmi.sb_type < BLOCK_8X8) {
566 assert(bsize == BLOCK_8X8);
567 for (y = 0; y < num_4x4_h; ++y)
568 for (x = 0; x < num_4x4_w; ++x)
569 dec_build_inter_predictors(xd, plane, i++, bw, bh,
570 4 * x, 4 * y, 4, 4, mi_x, mi_y);
572 dec_build_inter_predictors(xd, plane, 0, bw, bh,
573 0, 0, bw, bh, mi_x, mi_y);
578 void vp9_setup_dst_planes(struct macroblockd_plane planes[MAX_MB_PLANE],
579 const YV12_BUFFER_CONFIG *src,
580 int mi_row, int mi_col) {
581 uint8_t *const buffers[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
583 const int strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
587 for (i = 0; i < MAX_MB_PLANE; ++i) {
588 struct macroblockd_plane *const pd = &planes[i];
589 setup_pred_plane(&pd->dst, buffers[i], strides[i], mi_row, mi_col, NULL,
590 pd->subsampling_x, pd->subsampling_y);
594 void vp9_setup_pre_planes(MACROBLOCKD *xd, int idx,
595 const YV12_BUFFER_CONFIG *src,
596 int mi_row, int mi_col,
597 const struct scale_factors *sf) {
600 uint8_t *const buffers[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
602 const int strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
605 for (i = 0; i < MAX_MB_PLANE; ++i) {
606 struct macroblockd_plane *const pd = &xd->plane[i];
607 setup_pred_plane(&pd->pre[idx], buffers[i], strides[i], mi_row, mi_col,
608 sf, pd->subsampling_x, pd->subsampling_y);