Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license |
michael@0 | 5 | * that can be found in the LICENSE file in the root of the source |
michael@0 | 6 | * tree. An additional intellectual property rights grant can be found |
michael@0 | 7 | * in the file PATENTS. All contributing project authors may |
michael@0 | 8 | * be found in the AUTHORS file in the root of the source tree. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include <assert.h> |
michael@0 | 12 | |
michael@0 | 13 | #include "./vpx_scale_rtcd.h" |
michael@0 | 14 | #include "./vpx_config.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "vpx/vpx_integer.h" |
michael@0 | 17 | |
michael@0 | 18 | #include "vp9/common/vp9_blockd.h" |
michael@0 | 19 | #include "vp9/common/vp9_filter.h" |
michael@0 | 20 | #include "vp9/common/vp9_reconinter.h" |
michael@0 | 21 | #include "vp9/common/vp9_reconintra.h" |
michael@0 | 22 | |
michael@0 | 23 | void vp9_setup_interp_filters(MACROBLOCKD *xd, |
michael@0 | 24 | INTERPOLATION_TYPE mcomp_filter_type, |
michael@0 | 25 | VP9_COMMON *cm) { |
michael@0 | 26 | if (xd->mi_8x8 && xd->mi_8x8[0]) { |
michael@0 | 27 | MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi; |
michael@0 | 28 | |
michael@0 | 29 | set_scale_factors(xd, mbmi->ref_frame[0] - LAST_FRAME, |
michael@0 | 30 | mbmi->ref_frame[1] - LAST_FRAME, |
michael@0 | 31 | cm->active_ref_scale); |
michael@0 | 32 | } else { |
michael@0 | 33 | set_scale_factors(xd, -1, -1, cm->active_ref_scale); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | xd->subpix.filter_x = xd->subpix.filter_y = |
michael@0 | 37 | vp9_get_filter_kernel(mcomp_filter_type == SWITCHABLE ? |
michael@0 | 38 | EIGHTTAP : mcomp_filter_type); |
michael@0 | 39 | |
michael@0 | 40 | assert(((intptr_t)xd->subpix.filter_x & 0xff) == 0); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | static void inter_predictor(const uint8_t *src, int src_stride, |
michael@0 | 44 | uint8_t *dst, int dst_stride, |
michael@0 | 45 | const MV32 *mv, |
michael@0 | 46 | const struct scale_factors *scale, |
michael@0 | 47 | int w, int h, int ref, |
michael@0 | 48 | const struct subpix_fn_table *subpix, |
michael@0 | 49 | int xs, int ys) { |
michael@0 | 50 | const int subpel_x = mv->col & SUBPEL_MASK; |
michael@0 | 51 | const int subpel_y = mv->row & SUBPEL_MASK; |
michael@0 | 52 | |
michael@0 | 53 | src += (mv->row >> SUBPEL_BITS) * src_stride + (mv->col >> SUBPEL_BITS); |
michael@0 | 54 | scale->sfc->predict[subpel_x != 0][subpel_y != 0][ref]( |
michael@0 | 55 | src, src_stride, dst, dst_stride, |
michael@0 | 56 | subpix->filter_x[subpel_x], xs, |
michael@0 | 57 | subpix->filter_y[subpel_y], ys, |
michael@0 | 58 | w, h); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | void vp9_build_inter_predictor(const uint8_t *src, int src_stride, |
michael@0 | 62 | uint8_t *dst, int dst_stride, |
michael@0 | 63 | const MV *src_mv, |
michael@0 | 64 | const struct scale_factors *scale, |
michael@0 | 65 | int w, int h, int ref, |
michael@0 | 66 | const struct subpix_fn_table *subpix, |
michael@0 | 67 | enum mv_precision precision) { |
michael@0 | 68 | const int is_q4 = precision == MV_PRECISION_Q4; |
michael@0 | 69 | const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2, |
michael@0 | 70 | is_q4 ? src_mv->col : src_mv->col * 2 }; |
michael@0 | 71 | const struct scale_factors_common *sfc = scale->sfc; |
michael@0 | 72 | const MV32 mv = sfc->scale_mv(&mv_q4, scale); |
michael@0 | 73 | |
michael@0 | 74 | inter_predictor(src, src_stride, dst, dst_stride, &mv, scale, |
michael@0 | 75 | w, h, ref, subpix, sfc->x_step_q4, sfc->y_step_q4); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | static INLINE int round_mv_comp_q4(int value) { |
michael@0 | 79 | return (value < 0 ? value - 2 : value + 2) / 4; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | static MV mi_mv_pred_q4(const MODE_INFO *mi, int idx) { |
michael@0 | 83 | MV res = { round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.row + |
michael@0 | 84 | mi->bmi[1].as_mv[idx].as_mv.row + |
michael@0 | 85 | mi->bmi[2].as_mv[idx].as_mv.row + |
michael@0 | 86 | mi->bmi[3].as_mv[idx].as_mv.row), |
michael@0 | 87 | round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.col + |
michael@0 | 88 | mi->bmi[1].as_mv[idx].as_mv.col + |
michael@0 | 89 | mi->bmi[2].as_mv[idx].as_mv.col + |
michael@0 | 90 | mi->bmi[3].as_mv[idx].as_mv.col) }; |
michael@0 | 91 | return res; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | // TODO(jkoleszar): yet another mv clamping function :-( |
michael@0 | 95 | MV clamp_mv_to_umv_border_sb(const MACROBLOCKD *xd, const MV *src_mv, |
michael@0 | 96 | int bw, int bh, int ss_x, int ss_y) { |
michael@0 | 97 | // If the MV points so far into the UMV border that no visible pixels |
michael@0 | 98 | // are used for reconstruction, the subpel part of the MV can be |
michael@0 | 99 | // discarded and the MV limited to 16 pixels with equivalent results. |
michael@0 | 100 | const int spel_left = (VP9_INTERP_EXTEND + bw) << SUBPEL_BITS; |
michael@0 | 101 | const int spel_right = spel_left - SUBPEL_SHIFTS; |
michael@0 | 102 | const int spel_top = (VP9_INTERP_EXTEND + bh) << SUBPEL_BITS; |
michael@0 | 103 | const int spel_bottom = spel_top - SUBPEL_SHIFTS; |
michael@0 | 104 | MV clamped_mv = { |
michael@0 | 105 | src_mv->row * (1 << (1 - ss_y)), |
michael@0 | 106 | src_mv->col * (1 << (1 - ss_x)) |
michael@0 | 107 | }; |
michael@0 | 108 | assert(ss_x <= 1); |
michael@0 | 109 | assert(ss_y <= 1); |
michael@0 | 110 | |
michael@0 | 111 | clamp_mv(&clamped_mv, |
michael@0 | 112 | xd->mb_to_left_edge * (1 << (1 - ss_x)) - spel_left, |
michael@0 | 113 | xd->mb_to_right_edge * (1 << (1 - ss_x)) + spel_right, |
michael@0 | 114 | xd->mb_to_top_edge * (1 << (1 - ss_y)) - spel_top, |
michael@0 | 115 | xd->mb_to_bottom_edge * (1 << (1 - ss_y)) + spel_bottom); |
michael@0 | 116 | |
michael@0 | 117 | return clamped_mv; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | |
michael@0 | 121 | // TODO(jkoleszar): In principle, pred_w, pred_h are unnecessary, as we could |
michael@0 | 122 | // calculate the subsampled BLOCK_SIZE, but that type isn't defined for |
michael@0 | 123 | // sizes smaller than 16x16 yet. |
michael@0 | 124 | static void build_inter_predictors(MACROBLOCKD *xd, int plane, int block, |
michael@0 | 125 | BLOCK_SIZE bsize, int pred_w, int pred_h, |
michael@0 | 126 | int mi_x, int mi_y) { |
michael@0 | 127 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
michael@0 | 128 | const int bwl = b_width_log2(bsize) - pd->subsampling_x; |
michael@0 | 129 | const int bw = 4 << bwl; |
michael@0 | 130 | const int bh = plane_block_height(bsize, pd); |
michael@0 | 131 | const int x = 4 * (block & ((1 << bwl) - 1)); |
michael@0 | 132 | const int y = 4 * (block >> bwl); |
michael@0 | 133 | const MODE_INFO *mi = xd->mi_8x8[0]; |
michael@0 | 134 | const int is_compound = has_second_ref(&mi->mbmi); |
michael@0 | 135 | int ref; |
michael@0 | 136 | |
michael@0 | 137 | assert(x < bw); |
michael@0 | 138 | assert(y < bh); |
michael@0 | 139 | assert(mi->mbmi.sb_type < BLOCK_8X8 || 4 << pred_w == bw); |
michael@0 | 140 | assert(mi->mbmi.sb_type < BLOCK_8X8 || 4 << pred_h == bh); |
michael@0 | 141 | |
michael@0 | 142 | for (ref = 0; ref < 1 + is_compound; ++ref) { |
michael@0 | 143 | struct scale_factors *const scale = &xd->scale_factor[ref]; |
michael@0 | 144 | struct buf_2d *const pre_buf = &pd->pre[ref]; |
michael@0 | 145 | struct buf_2d *const dst_buf = &pd->dst; |
michael@0 | 146 | uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x; |
michael@0 | 147 | |
michael@0 | 148 | // TODO(jkoleszar): All chroma MVs in SPLITMV mode are taken as the |
michael@0 | 149 | // same MV (the average of the 4 luma MVs) but we could do something |
michael@0 | 150 | // smarter for non-4:2:0. Just punt for now, pending the changes to get |
michael@0 | 151 | // rid of SPLITMV mode entirely. |
michael@0 | 152 | const MV mv = mi->mbmi.sb_type < BLOCK_8X8 |
michael@0 | 153 | ? (plane == 0 ? mi->bmi[block].as_mv[ref].as_mv |
michael@0 | 154 | : mi_mv_pred_q4(mi, ref)) |
michael@0 | 155 | : mi->mbmi.mv[ref].as_mv; |
michael@0 | 156 | |
michael@0 | 157 | // TODO(jkoleszar): This clamping is done in the incorrect place for the |
michael@0 | 158 | // scaling case. It needs to be done on the scaled MV, not the pre-scaling |
michael@0 | 159 | // MV. Note however that it performs the subsampling aware scaling so |
michael@0 | 160 | // that the result is always q4. |
michael@0 | 161 | // mv_precision precision is MV_PRECISION_Q4. |
michael@0 | 162 | const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, &mv, bw, bh, |
michael@0 | 163 | pd->subsampling_x, |
michael@0 | 164 | pd->subsampling_y); |
michael@0 | 165 | |
michael@0 | 166 | uint8_t *pre; |
michael@0 | 167 | MV32 scaled_mv; |
michael@0 | 168 | int xs, ys; |
michael@0 | 169 | |
michael@0 | 170 | if (vp9_is_scaled(scale->sfc)) { |
michael@0 | 171 | pre = pre_buf->buf + scaled_buffer_offset(x, y, pre_buf->stride, scale); |
michael@0 | 172 | scale->sfc->set_scaled_offsets(scale, mi_y + y, mi_x + x); |
michael@0 | 173 | scaled_mv = scale->sfc->scale_mv(&mv_q4, scale); |
michael@0 | 174 | xs = scale->sfc->x_step_q4; |
michael@0 | 175 | ys = scale->sfc->y_step_q4; |
michael@0 | 176 | } else { |
michael@0 | 177 | pre = pre_buf->buf + (y * pre_buf->stride + x); |
michael@0 | 178 | scaled_mv.row = mv_q4.row; |
michael@0 | 179 | scaled_mv.col = mv_q4.col; |
michael@0 | 180 | xs = ys = 16; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride, |
michael@0 | 184 | &scaled_mv, scale, |
michael@0 | 185 | 4 << pred_w, 4 << pred_h, ref, |
michael@0 | 186 | &xd->subpix, xs, ys); |
michael@0 | 187 | } |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | static void build_inter_predictors_for_planes(MACROBLOCKD *xd, BLOCK_SIZE bsize, |
michael@0 | 191 | int mi_row, int mi_col, |
michael@0 | 192 | int plane_from, int plane_to) { |
michael@0 | 193 | int plane; |
michael@0 | 194 | for (plane = plane_from; plane <= plane_to; ++plane) { |
michael@0 | 195 | const int mi_x = mi_col * MI_SIZE; |
michael@0 | 196 | const int mi_y = mi_row * MI_SIZE; |
michael@0 | 197 | const int bwl = b_width_log2(bsize) - xd->plane[plane].subsampling_x; |
michael@0 | 198 | const int bhl = b_height_log2(bsize) - xd->plane[plane].subsampling_y; |
michael@0 | 199 | |
michael@0 | 200 | if (xd->mi_8x8[0]->mbmi.sb_type < BLOCK_8X8) { |
michael@0 | 201 | int i = 0, x, y; |
michael@0 | 202 | assert(bsize == BLOCK_8X8); |
michael@0 | 203 | for (y = 0; y < 1 << bhl; ++y) |
michael@0 | 204 | for (x = 0; x < 1 << bwl; ++x) |
michael@0 | 205 | build_inter_predictors(xd, plane, i++, bsize, 0, 0, mi_x, mi_y); |
michael@0 | 206 | } else { |
michael@0 | 207 | build_inter_predictors(xd, plane, 0, bsize, bwl, bhl, mi_x, mi_y); |
michael@0 | 208 | } |
michael@0 | 209 | } |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | void vp9_build_inter_predictors_sby(MACROBLOCKD *xd, int mi_row, int mi_col, |
michael@0 | 213 | BLOCK_SIZE bsize) { |
michael@0 | 214 | build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0, 0); |
michael@0 | 215 | } |
michael@0 | 216 | void vp9_build_inter_predictors_sbuv(MACROBLOCKD *xd, int mi_row, int mi_col, |
michael@0 | 217 | BLOCK_SIZE bsize) { |
michael@0 | 218 | build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 1, |
michael@0 | 219 | MAX_MB_PLANE - 1); |
michael@0 | 220 | } |
michael@0 | 221 | void vp9_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col, |
michael@0 | 222 | BLOCK_SIZE bsize) { |
michael@0 | 223 | build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0, |
michael@0 | 224 | MAX_MB_PLANE - 1); |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | // TODO(dkovalev: find better place for this function) |
michael@0 | 228 | void vp9_setup_scale_factors(VP9_COMMON *cm, int i) { |
michael@0 | 229 | const int ref = cm->active_ref_idx[i]; |
michael@0 | 230 | struct scale_factors *const sf = &cm->active_ref_scale[i]; |
michael@0 | 231 | struct scale_factors_common *const sfc = &cm->active_ref_scale_comm[i]; |
michael@0 | 232 | if (ref >= NUM_YV12_BUFFERS) { |
michael@0 | 233 | vp9_zero(*sf); |
michael@0 | 234 | vp9_zero(*sfc); |
michael@0 | 235 | } else { |
michael@0 | 236 | YV12_BUFFER_CONFIG *const fb = &cm->yv12_fb[ref]; |
michael@0 | 237 | vp9_setup_scale_factors_for_frame(sf, sfc, |
michael@0 | 238 | fb->y_crop_width, fb->y_crop_height, |
michael@0 | 239 | cm->width, cm->height); |
michael@0 | 240 | |
michael@0 | 241 | if (vp9_is_scaled(sfc)) |
michael@0 | 242 | vp9_extend_frame_borders(fb, cm->subsampling_x, cm->subsampling_y); |
michael@0 | 243 | } |
michael@0 | 244 | } |
michael@0 | 245 |