vp9_reconinter.c 12 KB

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