vp9_mvref_common.c 7.4 KB

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