vp9_mvref_common.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2012 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. #ifndef VP9_COMMON_VP9_MVREF_COMMON_H_
  11. #define VP9_COMMON_VP9_MVREF_COMMON_H_
  12. #include "vp9/common/vp9_onyxc_int.h"
  13. #include "vp9/common/vp9_blockd.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #define LEFT_TOP_MARGIN ((VP9_ENC_BORDER_IN_PIXELS - VP9_INTERP_EXTEND) << 3)
  18. #define RIGHT_BOTTOM_MARGIN ((VP9_ENC_BORDER_IN_PIXELS -\
  19. VP9_INTERP_EXTEND) << 3)
  20. #define MVREF_NEIGHBOURS 8
  21. typedef struct position {
  22. int row;
  23. int col;
  24. } POSITION;
  25. typedef enum {
  26. BOTH_ZERO = 0,
  27. ZERO_PLUS_PREDICTED = 1,
  28. BOTH_PREDICTED = 2,
  29. NEW_PLUS_NON_INTRA = 3,
  30. BOTH_NEW = 4,
  31. INTRA_PLUS_NON_INTRA = 5,
  32. BOTH_INTRA = 6,
  33. INVALID_CASE = 9
  34. } motion_vector_context;
  35. // This is used to figure out a context for the ref blocks. The code flattens
  36. // an array that would have 3 possible counts (0, 1 & 2) for 3 choices by
  37. // adding 9 for each intra block, 3 for each zero mv and 1 for each new
  38. // motion vector. This single number is then converted into a context
  39. // with a single lookup ( counter_to_context ).
  40. static const int mode_2_counter[MB_MODE_COUNT] = {
  41. 9, // DC_PRED
  42. 9, // V_PRED
  43. 9, // H_PRED
  44. 9, // D45_PRED
  45. 9, // D135_PRED
  46. 9, // D117_PRED
  47. 9, // D153_PRED
  48. 9, // D207_PRED
  49. 9, // D63_PRED
  50. 9, // TM_PRED
  51. 0, // NEARESTMV
  52. 0, // NEARMV
  53. 3, // ZEROMV
  54. 1, // NEWMV
  55. };
  56. // There are 3^3 different combinations of 3 counts that can be either 0,1 or
  57. // 2. However the actual count can never be greater than 2 so the highest
  58. // counter we need is 18. 9 is an invalid counter that's never used.
  59. static const int counter_to_context[19] = {
  60. BOTH_PREDICTED, // 0
  61. NEW_PLUS_NON_INTRA, // 1
  62. BOTH_NEW, // 2
  63. ZERO_PLUS_PREDICTED, // 3
  64. NEW_PLUS_NON_INTRA, // 4
  65. INVALID_CASE, // 5
  66. BOTH_ZERO, // 6
  67. INVALID_CASE, // 7
  68. INVALID_CASE, // 8
  69. INTRA_PLUS_NON_INTRA, // 9
  70. INTRA_PLUS_NON_INTRA, // 10
  71. INVALID_CASE, // 11
  72. INTRA_PLUS_NON_INTRA, // 12
  73. INVALID_CASE, // 13
  74. INVALID_CASE, // 14
  75. INVALID_CASE, // 15
  76. INVALID_CASE, // 16
  77. INVALID_CASE, // 17
  78. BOTH_INTRA // 18
  79. };
  80. static const POSITION mv_ref_blocks[BLOCK_SIZES][MVREF_NEIGHBOURS] = {
  81. // 4X4
  82. {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
  83. // 4X8
  84. {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
  85. // 8X4
  86. {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
  87. // 8X8
  88. {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
  89. // 8X16
  90. {{0, -1}, {-1, 0}, {1, -1}, {-1, -1}, {0, -2}, {-2, 0}, {-2, -1}, {-1, -2}},
  91. // 16X8
  92. {{-1, 0}, {0, -1}, {-1, 1}, {-1, -1}, {-2, 0}, {0, -2}, {-1, -2}, {-2, -1}},
  93. // 16X16
  94. {{-1, 0}, {0, -1}, {-1, 1}, {1, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
  95. // 16X32
  96. {{0, -1}, {-1, 0}, {2, -1}, {-1, -1}, {-1, 1}, {0, -3}, {-3, 0}, {-3, -3}},
  97. // 32X16
  98. {{-1, 0}, {0, -1}, {-1, 2}, {-1, -1}, {1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
  99. // 32X32
  100. {{-1, 1}, {1, -1}, {-1, 2}, {2, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
  101. // 32X64
  102. {{0, -1}, {-1, 0}, {4, -1}, {-1, 2}, {-1, -1}, {0, -3}, {-3, 0}, {2, -1}},
  103. // 64X32
  104. {{-1, 0}, {0, -1}, {-1, 4}, {2, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-1, 2}},
  105. // 64X64
  106. {{-1, 3}, {3, -1}, {-1, 4}, {4, -1}, {-1, -1}, {-1, 0}, {0, -1}, {-1, 6}}
  107. };
  108. static const int idx_n_column_to_subblock[4][2] = {
  109. {1, 2},
  110. {1, 3},
  111. {3, 2},
  112. {3, 3}
  113. };
  114. // clamp_mv_ref
  115. #define MV_BORDER (16 << 3) // Allow 16 pels in 1/8th pel units
  116. static INLINE void clamp_mv_ref(MV *mv, const MACROBLOCKD *xd) {
  117. clamp_mv(mv, xd->mb_to_left_edge - MV_BORDER,
  118. xd->mb_to_right_edge + MV_BORDER,
  119. xd->mb_to_top_edge - MV_BORDER,
  120. xd->mb_to_bottom_edge + MV_BORDER);
  121. }
  122. // This function returns either the appropriate sub block or block's mv
  123. // on whether the block_size < 8x8 and we have check_sub_blocks set.
  124. static INLINE int_mv get_sub_block_mv(const MODE_INFO *candidate, int which_mv,
  125. int search_col, int block_idx) {
  126. return block_idx >= 0 && candidate->sb_type < BLOCK_8X8
  127. ? candidate->bmi[idx_n_column_to_subblock[block_idx][search_col == 0]]
  128. .as_mv[which_mv]
  129. : candidate->mv[which_mv];
  130. }
  131. // Performs mv sign inversion if indicated by the reference frame combination.
  132. static INLINE int_mv scale_mv(const MODE_INFO *mi, int ref,
  133. const MV_REFERENCE_FRAME this_ref_frame,
  134. const int *ref_sign_bias) {
  135. int_mv mv = mi->mv[ref];
  136. if (ref_sign_bias[mi->ref_frame[ref]] != ref_sign_bias[this_ref_frame]) {
  137. mv.as_mv.row *= -1;
  138. mv.as_mv.col *= -1;
  139. }
  140. return mv;
  141. }
  142. // This macro is used to add a motion vector mv_ref list if it isn't
  143. // already in the list. If it's the second motion vector it will also
  144. // skip all additional processing and jump to Done!
  145. #define ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done) \
  146. do { \
  147. if (refmv_count) { \
  148. if ((mv).as_int != (mv_ref_list)[0].as_int) { \
  149. (mv_ref_list)[(refmv_count)] = (mv); \
  150. goto Done; \
  151. } \
  152. } else { \
  153. (mv_ref_list)[(refmv_count)++] = (mv); \
  154. } \
  155. } while (0)
  156. // If either reference frame is different, not INTRA, and they
  157. // are different from each other scale and add the mv to our list.
  158. #define IF_DIFF_REF_FRAME_ADD_MV(mbmi, ref_frame, ref_sign_bias, refmv_count, \
  159. mv_ref_list, Done) \
  160. do { \
  161. if (is_inter_block(mbmi)) { \
  162. if ((mbmi)->ref_frame[0] != ref_frame) \
  163. ADD_MV_REF_LIST(scale_mv((mbmi), 0, ref_frame, ref_sign_bias), \
  164. refmv_count, mv_ref_list, Done); \
  165. if (has_second_ref(mbmi) && \
  166. (mbmi)->ref_frame[1] != ref_frame && \
  167. (mbmi)->mv[1].as_int != (mbmi)->mv[0].as_int) \
  168. ADD_MV_REF_LIST(scale_mv((mbmi), 1, ref_frame, ref_sign_bias), \
  169. refmv_count, mv_ref_list, Done); \
  170. } \
  171. } while (0)
  172. // Checks that the given mi_row, mi_col and search point
  173. // are inside the borders of the tile.
  174. static INLINE int is_inside(const TileInfo *const tile,
  175. int mi_col, int mi_row, int mi_rows,
  176. const POSITION *mi_pos) {
  177. return !(mi_row + mi_pos->row < 0 ||
  178. mi_col + mi_pos->col < tile->mi_col_start ||
  179. mi_row + mi_pos->row >= mi_rows ||
  180. mi_col + mi_pos->col >= tile->mi_col_end);
  181. }
  182. // TODO(jingning): this mv clamping function should be block size dependent.
  183. static INLINE void clamp_mv2(MV *mv, const MACROBLOCKD *xd) {
  184. clamp_mv(mv, xd->mb_to_left_edge - LEFT_TOP_MARGIN,
  185. xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN,
  186. xd->mb_to_top_edge - LEFT_TOP_MARGIN,
  187. xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN);
  188. }
  189. static INLINE void lower_mv_precision(MV *mv, int allow_hp) {
  190. const int use_hp = allow_hp && use_mv_hp(mv);
  191. if (!use_hp) {
  192. if (mv->row & 1)
  193. mv->row += (mv->row > 0 ? -1 : 1);
  194. if (mv->col & 1)
  195. mv->col += (mv->col > 0 ? -1 : 1);
  196. }
  197. }
  198. typedef void (*find_mv_refs_sync)(void *const data, int mi_row);
  199. void vp9_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd,
  200. MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
  201. int_mv *mv_ref_list, int mi_row, int mi_col,
  202. uint8_t *mode_context);
  203. // check a list of motion vectors by sad score using a number rows of pixels
  204. // above and a number cols of pixels in the left to select the one with best
  205. // score to use as ref motion vector
  206. void vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp,
  207. int_mv *mvlist, int_mv *nearest_mv, int_mv *near_mv);
  208. void vp9_append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd,
  209. int block, int ref, int mi_row, int mi_col,
  210. int_mv *nearest_mv, int_mv *near_mv,
  211. uint8_t *mode_context);
  212. #ifdef __cplusplus
  213. } // extern "C"
  214. #endif
  215. #endif // VP9_COMMON_VP9_MVREF_COMMON_H_