1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libvpx/vp9/common/vp9_reconinter.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,245 @@ 1.4 +/* 1.5 + * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license 1.8 + * that can be found in the LICENSE file in the root of the source 1.9 + * tree. An additional intellectual property rights grant can be found 1.10 + * in the file PATENTS. All contributing project authors may 1.11 + * be found in the AUTHORS file in the root of the source tree. 1.12 + */ 1.13 + 1.14 +#include <assert.h> 1.15 + 1.16 +#include "./vpx_scale_rtcd.h" 1.17 +#include "./vpx_config.h" 1.18 + 1.19 +#include "vpx/vpx_integer.h" 1.20 + 1.21 +#include "vp9/common/vp9_blockd.h" 1.22 +#include "vp9/common/vp9_filter.h" 1.23 +#include "vp9/common/vp9_reconinter.h" 1.24 +#include "vp9/common/vp9_reconintra.h" 1.25 + 1.26 +void vp9_setup_interp_filters(MACROBLOCKD *xd, 1.27 + INTERPOLATION_TYPE mcomp_filter_type, 1.28 + VP9_COMMON *cm) { 1.29 + if (xd->mi_8x8 && xd->mi_8x8[0]) { 1.30 + MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi; 1.31 + 1.32 + set_scale_factors(xd, mbmi->ref_frame[0] - LAST_FRAME, 1.33 + mbmi->ref_frame[1] - LAST_FRAME, 1.34 + cm->active_ref_scale); 1.35 + } else { 1.36 + set_scale_factors(xd, -1, -1, cm->active_ref_scale); 1.37 + } 1.38 + 1.39 + xd->subpix.filter_x = xd->subpix.filter_y = 1.40 + vp9_get_filter_kernel(mcomp_filter_type == SWITCHABLE ? 1.41 + EIGHTTAP : mcomp_filter_type); 1.42 + 1.43 + assert(((intptr_t)xd->subpix.filter_x & 0xff) == 0); 1.44 +} 1.45 + 1.46 +static void inter_predictor(const uint8_t *src, int src_stride, 1.47 + uint8_t *dst, int dst_stride, 1.48 + const MV32 *mv, 1.49 + const struct scale_factors *scale, 1.50 + int w, int h, int ref, 1.51 + const struct subpix_fn_table *subpix, 1.52 + int xs, int ys) { 1.53 + const int subpel_x = mv->col & SUBPEL_MASK; 1.54 + const int subpel_y = mv->row & SUBPEL_MASK; 1.55 + 1.56 + src += (mv->row >> SUBPEL_BITS) * src_stride + (mv->col >> SUBPEL_BITS); 1.57 + scale->sfc->predict[subpel_x != 0][subpel_y != 0][ref]( 1.58 + src, src_stride, dst, dst_stride, 1.59 + subpix->filter_x[subpel_x], xs, 1.60 + subpix->filter_y[subpel_y], ys, 1.61 + w, h); 1.62 +} 1.63 + 1.64 +void vp9_build_inter_predictor(const uint8_t *src, int src_stride, 1.65 + uint8_t *dst, int dst_stride, 1.66 + const MV *src_mv, 1.67 + const struct scale_factors *scale, 1.68 + int w, int h, int ref, 1.69 + const struct subpix_fn_table *subpix, 1.70 + enum mv_precision precision) { 1.71 + const int is_q4 = precision == MV_PRECISION_Q4; 1.72 + const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2, 1.73 + is_q4 ? src_mv->col : src_mv->col * 2 }; 1.74 + const struct scale_factors_common *sfc = scale->sfc; 1.75 + const MV32 mv = sfc->scale_mv(&mv_q4, scale); 1.76 + 1.77 + inter_predictor(src, src_stride, dst, dst_stride, &mv, scale, 1.78 + w, h, ref, subpix, sfc->x_step_q4, sfc->y_step_q4); 1.79 +} 1.80 + 1.81 +static INLINE int round_mv_comp_q4(int value) { 1.82 + return (value < 0 ? value - 2 : value + 2) / 4; 1.83 +} 1.84 + 1.85 +static MV mi_mv_pred_q4(const MODE_INFO *mi, int idx) { 1.86 + MV res = { round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.row + 1.87 + mi->bmi[1].as_mv[idx].as_mv.row + 1.88 + mi->bmi[2].as_mv[idx].as_mv.row + 1.89 + mi->bmi[3].as_mv[idx].as_mv.row), 1.90 + round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.col + 1.91 + mi->bmi[1].as_mv[idx].as_mv.col + 1.92 + mi->bmi[2].as_mv[idx].as_mv.col + 1.93 + mi->bmi[3].as_mv[idx].as_mv.col) }; 1.94 + return res; 1.95 +} 1.96 + 1.97 +// TODO(jkoleszar): yet another mv clamping function :-( 1.98 +MV clamp_mv_to_umv_border_sb(const MACROBLOCKD *xd, const MV *src_mv, 1.99 + int bw, int bh, int ss_x, int ss_y) { 1.100 + // If the MV points so far into the UMV border that no visible pixels 1.101 + // are used for reconstruction, the subpel part of the MV can be 1.102 + // discarded and the MV limited to 16 pixels with equivalent results. 1.103 + const int spel_left = (VP9_INTERP_EXTEND + bw) << SUBPEL_BITS; 1.104 + const int spel_right = spel_left - SUBPEL_SHIFTS; 1.105 + const int spel_top = (VP9_INTERP_EXTEND + bh) << SUBPEL_BITS; 1.106 + const int spel_bottom = spel_top - SUBPEL_SHIFTS; 1.107 + MV clamped_mv = { 1.108 + src_mv->row * (1 << (1 - ss_y)), 1.109 + src_mv->col * (1 << (1 - ss_x)) 1.110 + }; 1.111 + assert(ss_x <= 1); 1.112 + assert(ss_y <= 1); 1.113 + 1.114 + clamp_mv(&clamped_mv, 1.115 + xd->mb_to_left_edge * (1 << (1 - ss_x)) - spel_left, 1.116 + xd->mb_to_right_edge * (1 << (1 - ss_x)) + spel_right, 1.117 + xd->mb_to_top_edge * (1 << (1 - ss_y)) - spel_top, 1.118 + xd->mb_to_bottom_edge * (1 << (1 - ss_y)) + spel_bottom); 1.119 + 1.120 + return clamped_mv; 1.121 +} 1.122 + 1.123 + 1.124 +// TODO(jkoleszar): In principle, pred_w, pred_h are unnecessary, as we could 1.125 +// calculate the subsampled BLOCK_SIZE, but that type isn't defined for 1.126 +// sizes smaller than 16x16 yet. 1.127 +static void build_inter_predictors(MACROBLOCKD *xd, int plane, int block, 1.128 + BLOCK_SIZE bsize, int pred_w, int pred_h, 1.129 + int mi_x, int mi_y) { 1.130 + struct macroblockd_plane *const pd = &xd->plane[plane]; 1.131 + const int bwl = b_width_log2(bsize) - pd->subsampling_x; 1.132 + const int bw = 4 << bwl; 1.133 + const int bh = plane_block_height(bsize, pd); 1.134 + const int x = 4 * (block & ((1 << bwl) - 1)); 1.135 + const int y = 4 * (block >> bwl); 1.136 + const MODE_INFO *mi = xd->mi_8x8[0]; 1.137 + const int is_compound = has_second_ref(&mi->mbmi); 1.138 + int ref; 1.139 + 1.140 + assert(x < bw); 1.141 + assert(y < bh); 1.142 + assert(mi->mbmi.sb_type < BLOCK_8X8 || 4 << pred_w == bw); 1.143 + assert(mi->mbmi.sb_type < BLOCK_8X8 || 4 << pred_h == bh); 1.144 + 1.145 + for (ref = 0; ref < 1 + is_compound; ++ref) { 1.146 + struct scale_factors *const scale = &xd->scale_factor[ref]; 1.147 + struct buf_2d *const pre_buf = &pd->pre[ref]; 1.148 + struct buf_2d *const dst_buf = &pd->dst; 1.149 + uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x; 1.150 + 1.151 + // TODO(jkoleszar): All chroma MVs in SPLITMV mode are taken as the 1.152 + // same MV (the average of the 4 luma MVs) but we could do something 1.153 + // smarter for non-4:2:0. Just punt for now, pending the changes to get 1.154 + // rid of SPLITMV mode entirely. 1.155 + const MV mv = mi->mbmi.sb_type < BLOCK_8X8 1.156 + ? (plane == 0 ? mi->bmi[block].as_mv[ref].as_mv 1.157 + : mi_mv_pred_q4(mi, ref)) 1.158 + : mi->mbmi.mv[ref].as_mv; 1.159 + 1.160 + // TODO(jkoleszar): This clamping is done in the incorrect place for the 1.161 + // scaling case. It needs to be done on the scaled MV, not the pre-scaling 1.162 + // MV. Note however that it performs the subsampling aware scaling so 1.163 + // that the result is always q4. 1.164 + // mv_precision precision is MV_PRECISION_Q4. 1.165 + const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, &mv, bw, bh, 1.166 + pd->subsampling_x, 1.167 + pd->subsampling_y); 1.168 + 1.169 + uint8_t *pre; 1.170 + MV32 scaled_mv; 1.171 + int xs, ys; 1.172 + 1.173 + if (vp9_is_scaled(scale->sfc)) { 1.174 + pre = pre_buf->buf + scaled_buffer_offset(x, y, pre_buf->stride, scale); 1.175 + scale->sfc->set_scaled_offsets(scale, mi_y + y, mi_x + x); 1.176 + scaled_mv = scale->sfc->scale_mv(&mv_q4, scale); 1.177 + xs = scale->sfc->x_step_q4; 1.178 + ys = scale->sfc->y_step_q4; 1.179 + } else { 1.180 + pre = pre_buf->buf + (y * pre_buf->stride + x); 1.181 + scaled_mv.row = mv_q4.row; 1.182 + scaled_mv.col = mv_q4.col; 1.183 + xs = ys = 16; 1.184 + } 1.185 + 1.186 + inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride, 1.187 + &scaled_mv, scale, 1.188 + 4 << pred_w, 4 << pred_h, ref, 1.189 + &xd->subpix, xs, ys); 1.190 + } 1.191 +} 1.192 + 1.193 +static void build_inter_predictors_for_planes(MACROBLOCKD *xd, BLOCK_SIZE bsize, 1.194 + int mi_row, int mi_col, 1.195 + int plane_from, int plane_to) { 1.196 + int plane; 1.197 + for (plane = plane_from; plane <= plane_to; ++plane) { 1.198 + const int mi_x = mi_col * MI_SIZE; 1.199 + const int mi_y = mi_row * MI_SIZE; 1.200 + const int bwl = b_width_log2(bsize) - xd->plane[plane].subsampling_x; 1.201 + const int bhl = b_height_log2(bsize) - xd->plane[plane].subsampling_y; 1.202 + 1.203 + if (xd->mi_8x8[0]->mbmi.sb_type < BLOCK_8X8) { 1.204 + int i = 0, x, y; 1.205 + assert(bsize == BLOCK_8X8); 1.206 + for (y = 0; y < 1 << bhl; ++y) 1.207 + for (x = 0; x < 1 << bwl; ++x) 1.208 + build_inter_predictors(xd, plane, i++, bsize, 0, 0, mi_x, mi_y); 1.209 + } else { 1.210 + build_inter_predictors(xd, plane, 0, bsize, bwl, bhl, mi_x, mi_y); 1.211 + } 1.212 + } 1.213 +} 1.214 + 1.215 +void vp9_build_inter_predictors_sby(MACROBLOCKD *xd, int mi_row, int mi_col, 1.216 + BLOCK_SIZE bsize) { 1.217 + build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0, 0); 1.218 +} 1.219 +void vp9_build_inter_predictors_sbuv(MACROBLOCKD *xd, int mi_row, int mi_col, 1.220 + BLOCK_SIZE bsize) { 1.221 + build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 1, 1.222 + MAX_MB_PLANE - 1); 1.223 +} 1.224 +void vp9_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col, 1.225 + BLOCK_SIZE bsize) { 1.226 + build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0, 1.227 + MAX_MB_PLANE - 1); 1.228 +} 1.229 + 1.230 +// TODO(dkovalev: find better place for this function) 1.231 +void vp9_setup_scale_factors(VP9_COMMON *cm, int i) { 1.232 + const int ref = cm->active_ref_idx[i]; 1.233 + struct scale_factors *const sf = &cm->active_ref_scale[i]; 1.234 + struct scale_factors_common *const sfc = &cm->active_ref_scale_comm[i]; 1.235 + if (ref >= NUM_YV12_BUFFERS) { 1.236 + vp9_zero(*sf); 1.237 + vp9_zero(*sfc); 1.238 + } else { 1.239 + YV12_BUFFER_CONFIG *const fb = &cm->yv12_fb[ref]; 1.240 + vp9_setup_scale_factors_for_frame(sf, sfc, 1.241 + fb->y_crop_width, fb->y_crop_height, 1.242 + cm->width, cm->height); 1.243 + 1.244 + if (vp9_is_scaled(sfc)) 1.245 + vp9_extend_frame_borders(fb, cm->subsampling_x, cm->subsampling_y); 1.246 + } 1.247 +} 1.248 +