vp9_mvref_common.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #include "vp9/common/vp9_mvref_common.h"
  11. // This function searches the neighborhood of a given MB/SB
  12. // to try and find candidate reference vectors.
  13. static void find_mv_refs_idx(const VP9_COMMON *cm, const MACROBLOCKD *xd,
  14. MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
  15. int_mv *mv_ref_list, int block, int mi_row,
  16. int mi_col, uint8_t *mode_context) {
  17. const int *ref_sign_bias = cm->ref_frame_sign_bias;
  18. int i, refmv_count = 0;
  19. const POSITION *const mv_ref_search = mv_ref_blocks[mi->sb_type];
  20. int different_ref_found = 0;
  21. int context_counter = 0;
  22. const MV_REF *const prev_frame_mvs =
  23. cm->use_prev_frame_mvs
  24. ? cm->prev_frame->mvs + mi_row * cm->mi_cols + mi_col
  25. : NULL;
  26. const TileInfo *const tile = &xd->tile;
  27. // Blank the reference vector list
  28. memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
  29. // The nearest 2 blocks are treated differently
  30. // if the size < 8x8 we get the mv from the bmi substructure,
  31. // and we also need to keep a mode count.
  32. for (i = 0; i < 2; ++i) {
  33. const POSITION *const mv_ref = &mv_ref_search[i];
  34. if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
  35. const MODE_INFO *const candidate_mi =
  36. xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
  37. // Keep counts for entropy encoding.
  38. context_counter += mode_2_counter[candidate_mi->mode];
  39. different_ref_found = 1;
  40. if (candidate_mi->ref_frame[0] == ref_frame)
  41. ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, block),
  42. refmv_count, mv_ref_list, Done);
  43. else if (candidate_mi->ref_frame[1] == ref_frame)
  44. ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 1, mv_ref->col, block),
  45. refmv_count, mv_ref_list, Done);
  46. }
  47. }
  48. // Check the rest of the neighbors in much the same way
  49. // as before except we don't need to keep track of sub blocks or
  50. // mode counts.
  51. for (; i < MVREF_NEIGHBOURS; ++i) {
  52. const POSITION *const mv_ref = &mv_ref_search[i];
  53. if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
  54. const MODE_INFO *const candidate_mi =
  55. xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
  56. different_ref_found = 1;
  57. if (candidate_mi->ref_frame[0] == ref_frame)
  58. ADD_MV_REF_LIST(candidate_mi->mv[0], refmv_count, mv_ref_list, Done);
  59. else if (candidate_mi->ref_frame[1] == ref_frame)
  60. ADD_MV_REF_LIST(candidate_mi->mv[1], refmv_count, mv_ref_list, Done);
  61. }
  62. }
  63. // Check the last frame's mode and mv info.
  64. if (cm->use_prev_frame_mvs) {
  65. if (prev_frame_mvs->ref_frame[0] == ref_frame) {
  66. ADD_MV_REF_LIST(prev_frame_mvs->mv[0], refmv_count, mv_ref_list, Done);
  67. } else if (prev_frame_mvs->ref_frame[1] == ref_frame) {
  68. ADD_MV_REF_LIST(prev_frame_mvs->mv[1], refmv_count, mv_ref_list, Done);
  69. }
  70. }
  71. // Since we couldn't find 2 mvs from the same reference frame
  72. // go back through the neighbors and find motion vectors from
  73. // different reference frames.
  74. if (different_ref_found) {
  75. for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
  76. const POSITION *mv_ref = &mv_ref_search[i];
  77. if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
  78. const MODE_INFO *const candidate_mi =
  79. xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
  80. // If the candidate is INTRA we don't want to consider its mv.
  81. IF_DIFF_REF_FRAME_ADD_MV(candidate_mi, ref_frame, ref_sign_bias,
  82. refmv_count, mv_ref_list, Done);
  83. }
  84. }
  85. }
  86. // Since we still don't have a candidate we'll try the last frame.
  87. if (cm->use_prev_frame_mvs) {
  88. if (prev_frame_mvs->ref_frame[0] != ref_frame &&
  89. prev_frame_mvs->ref_frame[0] > INTRA_FRAME) {
  90. int_mv mv = prev_frame_mvs->mv[0];
  91. if (ref_sign_bias[prev_frame_mvs->ref_frame[0]] !=
  92. ref_sign_bias[ref_frame]) {
  93. mv.as_mv.row *= -1;
  94. mv.as_mv.col *= -1;
  95. }
  96. ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done);
  97. }
  98. if (prev_frame_mvs->ref_frame[1] > INTRA_FRAME &&
  99. prev_frame_mvs->ref_frame[1] != ref_frame &&
  100. prev_frame_mvs->mv[1].as_int != prev_frame_mvs->mv[0].as_int) {
  101. int_mv mv = prev_frame_mvs->mv[1];
  102. if (ref_sign_bias[prev_frame_mvs->ref_frame[1]] !=
  103. ref_sign_bias[ref_frame]) {
  104. mv.as_mv.row *= -1;
  105. mv.as_mv.col *= -1;
  106. }
  107. ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done);
  108. }
  109. }
  110. Done:
  111. mode_context[ref_frame] = counter_to_context[context_counter];
  112. // Clamp vectors
  113. for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
  114. clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
  115. }
  116. void vp9_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd,
  117. MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
  118. int_mv *mv_ref_list, int mi_row, int mi_col,
  119. uint8_t *mode_context) {
  120. find_mv_refs_idx(cm, xd, mi, ref_frame, mv_ref_list, -1, mi_row, mi_col,
  121. mode_context);
  122. }
  123. void vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp, int_mv *mvlist,
  124. int_mv *nearest_mv, int_mv *near_mv) {
  125. int i;
  126. // Make sure all the candidates are properly clamped etc
  127. for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
  128. lower_mv_precision(&mvlist[i].as_mv, allow_hp);
  129. clamp_mv2(&mvlist[i].as_mv, xd);
  130. }
  131. *nearest_mv = mvlist[0];
  132. *near_mv = mvlist[1];
  133. }
  134. void vp9_append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd, int block,
  135. int ref, int mi_row, int mi_col,
  136. int_mv *nearest_mv, int_mv *near_mv,
  137. uint8_t *mode_context) {
  138. int_mv mv_list[MAX_MV_REF_CANDIDATES];
  139. MODE_INFO *const mi = xd->mi[0];
  140. b_mode_info *bmi = mi->bmi;
  141. int n;
  142. assert(MAX_MV_REF_CANDIDATES == 2);
  143. find_mv_refs_idx(cm, xd, mi, mi->ref_frame[ref], mv_list, block, mi_row,
  144. mi_col, mode_context);
  145. near_mv->as_int = 0;
  146. switch (block) {
  147. case 0:
  148. nearest_mv->as_int = mv_list[0].as_int;
  149. near_mv->as_int = mv_list[1].as_int;
  150. break;
  151. case 1:
  152. case 2:
  153. nearest_mv->as_int = bmi[0].as_mv[ref].as_int;
  154. for (n = 0; n < MAX_MV_REF_CANDIDATES; ++n)
  155. if (nearest_mv->as_int != mv_list[n].as_int) {
  156. near_mv->as_int = mv_list[n].as_int;
  157. break;
  158. }
  159. break;
  160. case 3: {
  161. int_mv candidates[2 + MAX_MV_REF_CANDIDATES];
  162. candidates[0] = bmi[1].as_mv[ref];
  163. candidates[1] = bmi[0].as_mv[ref];
  164. candidates[2] = mv_list[0];
  165. candidates[3] = mv_list[1];
  166. nearest_mv->as_int = bmi[2].as_mv[ref].as_int;
  167. for (n = 0; n < 2 + MAX_MV_REF_CANDIDATES; ++n)
  168. if (nearest_mv->as_int != candidates[n].as_int) {
  169. near_mv->as_int = candidates[n].as_int;
  170. break;
  171. }
  172. break;
  173. }
  174. default: assert(0 && "Invalid block index.");
  175. }
  176. }