vp9_reconinter.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #include <assert.h>
  11. #include "./vpx_scale_rtcd.h"
  12. #include "./vpx_config.h"
  13. #include "vpx/vpx_integer.h"
  14. #include "vp9/common/vp9_blockd.h"
  15. #include "vp9/common/vp9_reconinter.h"
  16. #include "vp9/common/vp9_reconintra.h"
  17. #if CONFIG_VP9_HIGHBITDEPTH
  18. void vp9_highbd_build_inter_predictor(const uint8_t *src, int src_stride,
  19. uint8_t *dst, int dst_stride,
  20. const MV *src_mv,
  21. const struct scale_factors *sf,
  22. int w, int h, int ref,
  23. const InterpKernel *kernel,
  24. enum mv_precision precision,
  25. int x, int y, int bd) {
  26. const int is_q4 = precision == MV_PRECISION_Q4;
  27. const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2,
  28. is_q4 ? src_mv->col : src_mv->col * 2 };
  29. MV32 mv = vp9_scale_mv(&mv_q4, x, y, sf);
  30. const int subpel_x = mv.col & SUBPEL_MASK;
  31. const int subpel_y = mv.row & SUBPEL_MASK;
  32. src += (mv.row >> SUBPEL_BITS) * src_stride + (mv.col >> SUBPEL_BITS);
  33. highbd_inter_predictor(src, src_stride, dst, dst_stride, subpel_x, subpel_y,
  34. sf, w, h, ref, kernel, sf->x_step_q4, sf->y_step_q4,
  35. bd);
  36. }
  37. #endif // CONFIG_VP9_HIGHBITDEPTH
  38. void vp9_build_inter_predictor(const uint8_t *src, int src_stride,
  39. uint8_t *dst, int dst_stride,
  40. const MV *src_mv,
  41. const struct scale_factors *sf,
  42. int w, int h, int ref,
  43. const InterpKernel *kernel,
  44. enum mv_precision precision,
  45. int x, int y) {
  46. const int is_q4 = precision == MV_PRECISION_Q4;
  47. const MV mv_q4 = { is_q4 ? src_mv->row : src_mv->row * 2,
  48. is_q4 ? src_mv->col : src_mv->col * 2 };
  49. MV32 mv = vp9_scale_mv(&mv_q4, x, y, sf);
  50. const int subpel_x = mv.col & SUBPEL_MASK;
  51. const int subpel_y = mv.row & SUBPEL_MASK;
  52. src += (mv.row >> SUBPEL_BITS) * src_stride + (mv.col >> SUBPEL_BITS);
  53. inter_predictor(src, src_stride, dst, dst_stride, subpel_x, subpel_y,
  54. sf, w, h, ref, kernel, sf->x_step_q4, sf->y_step_q4);
  55. }
  56. static INLINE int round_mv_comp_q4(int value) {
  57. return (value < 0 ? value - 2 : value + 2) / 4;
  58. }
  59. static MV mi_mv_pred_q4(const MODE_INFO *mi, int idx) {
  60. MV res = { round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.row +
  61. mi->bmi[1].as_mv[idx].as_mv.row +
  62. mi->bmi[2].as_mv[idx].as_mv.row +
  63. mi->bmi[3].as_mv[idx].as_mv.row),
  64. round_mv_comp_q4(mi->bmi[0].as_mv[idx].as_mv.col +
  65. mi->bmi[1].as_mv[idx].as_mv.col +
  66. mi->bmi[2].as_mv[idx].as_mv.col +
  67. mi->bmi[3].as_mv[idx].as_mv.col) };
  68. return res;
  69. }
  70. static INLINE int round_mv_comp_q2(int value) {
  71. return (value < 0 ? value - 1 : value + 1) / 2;
  72. }
  73. static MV mi_mv_pred_q2(const MODE_INFO *mi, int idx, int block0, int block1) {
  74. MV res = { round_mv_comp_q2(mi->bmi[block0].as_mv[idx].as_mv.row +
  75. mi->bmi[block1].as_mv[idx].as_mv.row),
  76. round_mv_comp_q2(mi->bmi[block0].as_mv[idx].as_mv.col +
  77. mi->bmi[block1].as_mv[idx].as_mv.col) };
  78. return res;
  79. }
  80. // TODO(jkoleszar): yet another mv clamping function :-(
  81. MV clamp_mv_to_umv_border_sb(const MACROBLOCKD *xd, const MV *src_mv,
  82. int bw, int bh, int ss_x, int ss_y) {
  83. // If the MV points so far into the UMV border that no visible pixels
  84. // are used for reconstruction, the subpel part of the MV can be
  85. // discarded and the MV limited to 16 pixels with equivalent results.
  86. const int spel_left = (VP9_INTERP_EXTEND + bw) << SUBPEL_BITS;
  87. const int spel_right = spel_left - SUBPEL_SHIFTS;
  88. const int spel_top = (VP9_INTERP_EXTEND + bh) << SUBPEL_BITS;
  89. const int spel_bottom = spel_top - SUBPEL_SHIFTS;
  90. MV clamped_mv = {
  91. src_mv->row * (1 << (1 - ss_y)),
  92. src_mv->col * (1 << (1 - ss_x))
  93. };
  94. assert(ss_x <= 1);
  95. assert(ss_y <= 1);
  96. clamp_mv(&clamped_mv,
  97. xd->mb_to_left_edge * (1 << (1 - ss_x)) - spel_left,
  98. xd->mb_to_right_edge * (1 << (1 - ss_x)) + spel_right,
  99. xd->mb_to_top_edge * (1 << (1 - ss_y)) - spel_top,
  100. xd->mb_to_bottom_edge * (1 << (1 - ss_y)) + spel_bottom);
  101. return clamped_mv;
  102. }
  103. MV average_split_mvs(const struct macroblockd_plane *pd,
  104. const MODE_INFO *mi, int ref, int block) {
  105. const int ss_idx = ((pd->subsampling_x > 0) << 1) | (pd->subsampling_y > 0);
  106. MV res = {0, 0};
  107. switch (ss_idx) {
  108. case 0:
  109. res = mi->bmi[block].as_mv[ref].as_mv;
  110. break;
  111. case 1:
  112. res = mi_mv_pred_q2(mi, ref, block, block + 2);
  113. break;
  114. case 2:
  115. res = mi_mv_pred_q2(mi, ref, block, block + 1);
  116. break;
  117. case 3:
  118. res = mi_mv_pred_q4(mi, ref);
  119. break;
  120. default:
  121. assert(ss_idx <= 3 && ss_idx >= 0);
  122. }
  123. return res;
  124. }
  125. static void build_inter_predictors(MACROBLOCKD *xd, int plane, int block,
  126. int bw, int bh,
  127. int x, int y, int w, int h,
  128. int mi_x, int mi_y) {
  129. struct macroblockd_plane *const pd = &xd->plane[plane];
  130. const MODE_INFO *mi = xd->mi[0];
  131. const int is_compound = has_second_ref(mi);
  132. const InterpKernel *kernel = vp9_filter_kernels[mi->interp_filter];
  133. int ref;
  134. for (ref = 0; ref < 1 + is_compound; ++ref) {
  135. const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
  136. struct buf_2d *const pre_buf = &pd->pre[ref];
  137. struct buf_2d *const dst_buf = &pd->dst;
  138. uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
  139. const MV mv = mi->sb_type < BLOCK_8X8
  140. ? average_split_mvs(pd, mi, ref, block)
  141. : mi->mv[ref].as_mv;
  142. // TODO(jkoleszar): This clamping is done in the incorrect place for the
  143. // scaling case. It needs to be done on the scaled MV, not the pre-scaling
  144. // MV. Note however that it performs the subsampling aware scaling so
  145. // that the result is always q4.
  146. // mv_precision precision is MV_PRECISION_Q4.
  147. const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, &mv, bw, bh,
  148. pd->subsampling_x,
  149. pd->subsampling_y);
  150. uint8_t *pre;
  151. MV32 scaled_mv;
  152. int xs, ys, subpel_x, subpel_y;
  153. const int is_scaled = vp9_is_scaled(sf);
  154. if (is_scaled) {
  155. // Co-ordinate of containing block to pixel precision.
  156. const int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
  157. const int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
  158. #if CONFIG_BETTER_HW_COMPATIBILITY
  159. assert(xd->mi[0]->sb_type != BLOCK_4X8 &&
  160. xd->mi[0]->sb_type != BLOCK_8X4);
  161. assert(mv_q4.row == mv.row * (1 << (1 - pd->subsampling_y)) &&
  162. mv_q4.col == mv.col * (1 << (1 - pd->subsampling_x)));
  163. #endif
  164. if (plane == 0)
  165. pre_buf->buf = xd->block_refs[ref]->buf->y_buffer;
  166. else if (plane == 1)
  167. pre_buf->buf = xd->block_refs[ref]->buf->u_buffer;
  168. else
  169. pre_buf->buf = xd->block_refs[ref]->buf->v_buffer;
  170. pre_buf->buf += scaled_buffer_offset(x_start + x, y_start + y,
  171. pre_buf->stride, sf);
  172. pre = pre_buf->buf;
  173. scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
  174. xs = sf->x_step_q4;
  175. ys = sf->y_step_q4;
  176. } else {
  177. pre = pre_buf->buf + (y * pre_buf->stride + x);
  178. scaled_mv.row = mv_q4.row;
  179. scaled_mv.col = mv_q4.col;
  180. xs = ys = 16;
  181. }
  182. subpel_x = scaled_mv.col & SUBPEL_MASK;
  183. subpel_y = scaled_mv.row & SUBPEL_MASK;
  184. pre += (scaled_mv.row >> SUBPEL_BITS) * pre_buf->stride
  185. + (scaled_mv.col >> SUBPEL_BITS);
  186. #if CONFIG_VP9_HIGHBITDEPTH
  187. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  188. highbd_inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
  189. subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys,
  190. xd->bd);
  191. } else {
  192. inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
  193. subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys);
  194. }
  195. #else
  196. inter_predictor(pre, pre_buf->stride, dst, dst_buf->stride,
  197. subpel_x, subpel_y, sf, w, h, ref, kernel, xs, ys);
  198. #endif // CONFIG_VP9_HIGHBITDEPTH
  199. }
  200. }
  201. static void build_inter_predictors_for_planes(MACROBLOCKD *xd, BLOCK_SIZE bsize,
  202. int mi_row, int mi_col,
  203. int plane_from, int plane_to) {
  204. int plane;
  205. const int mi_x = mi_col * MI_SIZE;
  206. const int mi_y = mi_row * MI_SIZE;
  207. for (plane = plane_from; plane <= plane_to; ++plane) {
  208. const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize,
  209. &xd->plane[plane]);
  210. const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
  211. const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
  212. const int bw = 4 * num_4x4_w;
  213. const int bh = 4 * num_4x4_h;
  214. if (xd->mi[0]->sb_type < BLOCK_8X8) {
  215. int i = 0, x, y;
  216. assert(bsize == BLOCK_8X8);
  217. for (y = 0; y < num_4x4_h; ++y)
  218. for (x = 0; x < num_4x4_w; ++x)
  219. build_inter_predictors(xd, plane, i++, bw, bh,
  220. 4 * x, 4 * y, 4, 4, mi_x, mi_y);
  221. } else {
  222. build_inter_predictors(xd, plane, 0, bw, bh,
  223. 0, 0, bw, bh, mi_x, mi_y);
  224. }
  225. }
  226. }
  227. void vp9_build_inter_predictors_sby(MACROBLOCKD *xd, int mi_row, int mi_col,
  228. BLOCK_SIZE bsize) {
  229. build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0, 0);
  230. }
  231. void vp9_build_inter_predictors_sbp(MACROBLOCKD *xd, int mi_row, int mi_col,
  232. BLOCK_SIZE bsize, int plane) {
  233. build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, plane, plane);
  234. }
  235. void vp9_build_inter_predictors_sbuv(MACROBLOCKD *xd, int mi_row, int mi_col,
  236. BLOCK_SIZE bsize) {
  237. build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 1,
  238. MAX_MB_PLANE - 1);
  239. }
  240. void vp9_build_inter_predictors_sb(MACROBLOCKD *xd, int mi_row, int mi_col,
  241. BLOCK_SIZE bsize) {
  242. build_inter_predictors_for_planes(xd, bsize, mi_row, mi_col, 0,
  243. MAX_MB_PLANE - 1);
  244. }
  245. void vp9_setup_dst_planes(struct macroblockd_plane planes[MAX_MB_PLANE],
  246. const YV12_BUFFER_CONFIG *src,
  247. int mi_row, int mi_col) {
  248. uint8_t *const buffers[MAX_MB_PLANE] = { src->y_buffer, src->u_buffer,
  249. src->v_buffer};
  250. const int strides[MAX_MB_PLANE] = { src->y_stride, src->uv_stride,
  251. src->uv_stride};
  252. int i;
  253. for (i = 0; i < MAX_MB_PLANE; ++i) {
  254. struct macroblockd_plane *const pd = &planes[i];
  255. setup_pred_plane(&pd->dst, buffers[i], strides[i], mi_row, mi_col, NULL,
  256. pd->subsampling_x, pd->subsampling_y);
  257. }
  258. }
  259. void vp9_setup_pre_planes(MACROBLOCKD *xd, int idx,
  260. const YV12_BUFFER_CONFIG *src,
  261. int mi_row, int mi_col,
  262. const struct scale_factors *sf) {
  263. if (src != NULL) {
  264. int i;
  265. uint8_t *const buffers[MAX_MB_PLANE] = { src->y_buffer, src->u_buffer,
  266. src->v_buffer};
  267. const int strides[MAX_MB_PLANE] = { src->y_stride, src->uv_stride,
  268. src->uv_stride};
  269. for (i = 0; i < MAX_MB_PLANE; ++i) {
  270. struct macroblockd_plane *const pd = &xd->plane[i];
  271. setup_pred_plane(&pd->pre[idx], buffers[i], strides[i], mi_row, mi_col,
  272. sf, pd->subsampling_x, pd->subsampling_y);
  273. }
  274. }
  275. }