vp9_mvref_common.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 \
  19. ((VP9_ENC_BORDER_IN_PIXELS - 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 },
  83. { 0, -1 },
  84. { -1, -1 },
  85. { -2, 0 },
  86. { 0, -2 },
  87. { -2, -1 },
  88. { -1, -2 },
  89. { -2, -2 } },
  90. // 4X8
  91. { { -1, 0 },
  92. { 0, -1 },
  93. { -1, -1 },
  94. { -2, 0 },
  95. { 0, -2 },
  96. { -2, -1 },
  97. { -1, -2 },
  98. { -2, -2 } },
  99. // 8X4
  100. { { -1, 0 },
  101. { 0, -1 },
  102. { -1, -1 },
  103. { -2, 0 },
  104. { 0, -2 },
  105. { -2, -1 },
  106. { -1, -2 },
  107. { -2, -2 } },
  108. // 8X8
  109. { { -1, 0 },
  110. { 0, -1 },
  111. { -1, -1 },
  112. { -2, 0 },
  113. { 0, -2 },
  114. { -2, -1 },
  115. { -1, -2 },
  116. { -2, -2 } },
  117. // 8X16
  118. { { 0, -1 },
  119. { -1, 0 },
  120. { 1, -1 },
  121. { -1, -1 },
  122. { 0, -2 },
  123. { -2, 0 },
  124. { -2, -1 },
  125. { -1, -2 } },
  126. // 16X8
  127. { { -1, 0 },
  128. { 0, -1 },
  129. { -1, 1 },
  130. { -1, -1 },
  131. { -2, 0 },
  132. { 0, -2 },
  133. { -1, -2 },
  134. { -2, -1 } },
  135. // 16X16
  136. { { -1, 0 },
  137. { 0, -1 },
  138. { -1, 1 },
  139. { 1, -1 },
  140. { -1, -1 },
  141. { -3, 0 },
  142. { 0, -3 },
  143. { -3, -3 } },
  144. // 16X32
  145. { { 0, -1 },
  146. { -1, 0 },
  147. { 2, -1 },
  148. { -1, -1 },
  149. { -1, 1 },
  150. { 0, -3 },
  151. { -3, 0 },
  152. { -3, -3 } },
  153. // 32X16
  154. { { -1, 0 },
  155. { 0, -1 },
  156. { -1, 2 },
  157. { -1, -1 },
  158. { 1, -1 },
  159. { -3, 0 },
  160. { 0, -3 },
  161. { -3, -3 } },
  162. // 32X32
  163. { { -1, 1 },
  164. { 1, -1 },
  165. { -1, 2 },
  166. { 2, -1 },
  167. { -1, -1 },
  168. { -3, 0 },
  169. { 0, -3 },
  170. { -3, -3 } },
  171. // 32X64
  172. { { 0, -1 },
  173. { -1, 0 },
  174. { 4, -1 },
  175. { -1, 2 },
  176. { -1, -1 },
  177. { 0, -3 },
  178. { -3, 0 },
  179. { 2, -1 } },
  180. // 64X32
  181. { { -1, 0 },
  182. { 0, -1 },
  183. { -1, 4 },
  184. { 2, -1 },
  185. { -1, -1 },
  186. { -3, 0 },
  187. { 0, -3 },
  188. { -1, 2 } },
  189. // 64X64
  190. { { -1, 3 },
  191. { 3, -1 },
  192. { -1, 4 },
  193. { 4, -1 },
  194. { -1, -1 },
  195. { -1, 0 },
  196. { 0, -1 },
  197. { -1, 6 } }
  198. };
  199. static const int idx_n_column_to_subblock[4][2] = {
  200. { 1, 2 }, { 1, 3 }, { 3, 2 }, { 3, 3 }
  201. };
  202. // clamp_mv_ref
  203. #define MV_BORDER (16 << 3) // Allow 16 pels in 1/8th pel units
  204. static INLINE void clamp_mv_ref(MV *mv, const MACROBLOCKD *xd) {
  205. clamp_mv(mv, xd->mb_to_left_edge - MV_BORDER,
  206. xd->mb_to_right_edge + MV_BORDER, xd->mb_to_top_edge - MV_BORDER,
  207. xd->mb_to_bottom_edge + MV_BORDER);
  208. }
  209. // This function returns either the appropriate sub block or block's mv
  210. // on whether the block_size < 8x8 and we have check_sub_blocks set.
  211. static INLINE int_mv get_sub_block_mv(const MODE_INFO *candidate, int which_mv,
  212. int search_col, int block_idx) {
  213. return block_idx >= 0 && candidate->sb_type < BLOCK_8X8
  214. ? candidate
  215. ->bmi[idx_n_column_to_subblock[block_idx][search_col == 0]]
  216. .as_mv[which_mv]
  217. : candidate->mv[which_mv];
  218. }
  219. // Performs mv sign inversion if indicated by the reference frame combination.
  220. static INLINE int_mv scale_mv(const MODE_INFO *mi, int ref,
  221. const MV_REFERENCE_FRAME this_ref_frame,
  222. const int *ref_sign_bias) {
  223. int_mv mv = mi->mv[ref];
  224. if (ref_sign_bias[mi->ref_frame[ref]] != ref_sign_bias[this_ref_frame]) {
  225. mv.as_mv.row *= -1;
  226. mv.as_mv.col *= -1;
  227. }
  228. return mv;
  229. }
  230. // This macro is used to add a motion vector mv_ref list if it isn't
  231. // already in the list. If it's the second motion vector it will also
  232. // skip all additional processing and jump to Done!
  233. #define ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done) \
  234. do { \
  235. if (refmv_count) { \
  236. if ((mv).as_int != (mv_ref_list)[0].as_int) { \
  237. (mv_ref_list)[(refmv_count)] = (mv); \
  238. goto Done; \
  239. } \
  240. } else { \
  241. (mv_ref_list)[(refmv_count)++] = (mv); \
  242. } \
  243. } while (0)
  244. // If either reference frame is different, not INTRA, and they
  245. // are different from each other scale and add the mv to our list.
  246. #define IF_DIFF_REF_FRAME_ADD_MV(mbmi, ref_frame, ref_sign_bias, refmv_count, \
  247. mv_ref_list, Done) \
  248. do { \
  249. if (is_inter_block(mbmi)) { \
  250. if ((mbmi)->ref_frame[0] != ref_frame) \
  251. ADD_MV_REF_LIST(scale_mv((mbmi), 0, ref_frame, ref_sign_bias), \
  252. refmv_count, mv_ref_list, Done); \
  253. if (has_second_ref(mbmi) && (mbmi)->ref_frame[1] != ref_frame && \
  254. (mbmi)->mv[1].as_int != (mbmi)->mv[0].as_int) \
  255. ADD_MV_REF_LIST(scale_mv((mbmi), 1, ref_frame, ref_sign_bias), \
  256. refmv_count, mv_ref_list, Done); \
  257. } \
  258. } while (0)
  259. // Checks that the given mi_row, mi_col and search point
  260. // are inside the borders of the tile.
  261. static INLINE int is_inside(const TileInfo *const tile, int mi_col, int mi_row,
  262. int mi_rows, const POSITION *mi_pos) {
  263. return !(mi_row + mi_pos->row < 0 ||
  264. mi_col + mi_pos->col < tile->mi_col_start ||
  265. mi_row + mi_pos->row >= mi_rows ||
  266. mi_col + mi_pos->col >= tile->mi_col_end);
  267. }
  268. // TODO(jingning): this mv clamping function should be block size dependent.
  269. static INLINE void clamp_mv2(MV *mv, const MACROBLOCKD *xd) {
  270. clamp_mv(mv, xd->mb_to_left_edge - LEFT_TOP_MARGIN,
  271. xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN,
  272. xd->mb_to_top_edge - LEFT_TOP_MARGIN,
  273. xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN);
  274. }
  275. static INLINE void lower_mv_precision(MV *mv, int allow_hp) {
  276. const int use_hp = allow_hp && use_mv_hp(mv);
  277. if (!use_hp) {
  278. if (mv->row & 1) mv->row += (mv->row > 0 ? -1 : 1);
  279. if (mv->col & 1) mv->col += (mv->col > 0 ? -1 : 1);
  280. }
  281. }
  282. typedef void (*find_mv_refs_sync)(void *const data, int mi_row);
  283. void vp9_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd,
  284. MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
  285. int_mv *mv_ref_list, int mi_row, int mi_col,
  286. uint8_t *mode_context);
  287. // check a list of motion vectors by sad score using a number rows of pixels
  288. // above and a number cols of pixels in the left to select the one with best
  289. // score to use as ref motion vector
  290. void vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp, int_mv *mvlist,
  291. int_mv *nearest_mv, int_mv *near_mv);
  292. void vp9_append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd, int block,
  293. int ref, int mi_row, int mi_col,
  294. int_mv *nearest_mv, int_mv *near_mv,
  295. uint8_t *mode_context);
  296. #ifdef __cplusplus
  297. } // extern "C"
  298. #endif
  299. #endif // VP9_COMMON_VP9_MVREF_COMMON_H_