findnearmv.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "findnearmv.h"
  11. const unsigned char vp8_mbsplit_offset[4][16] = {
  12. { 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  13. { 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  14. { 0, 2, 8, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  15. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
  16. };
  17. /* Predict motion vectors using those from already-decoded nearby blocks.
  18. Note that we only consider one 4x4 subblock from each candidate 16x16
  19. macroblock. */
  20. void vp8_find_near_mvs
  21. (
  22. MACROBLOCKD *xd,
  23. const MODE_INFO *here,
  24. int_mv *nearest,
  25. int_mv *nearby,
  26. int_mv *best_mv,
  27. int cnt[4],
  28. int refframe,
  29. int *ref_frame_sign_bias
  30. )
  31. {
  32. const MODE_INFO *above = here - xd->mode_info_stride;
  33. const MODE_INFO *left = here - 1;
  34. const MODE_INFO *aboveleft = above - 1;
  35. int_mv near_mvs[4];
  36. int_mv *mv = near_mvs;
  37. int *cntx = cnt;
  38. enum {CNT_INTRA, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV};
  39. /* Zero accumulators */
  40. mv[0].as_int = mv[1].as_int = mv[2].as_int = 0;
  41. cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0;
  42. /* Process above */
  43. if (above->mbmi.ref_frame != INTRA_FRAME)
  44. {
  45. if (above->mbmi.mv.as_int)
  46. {
  47. (++mv)->as_int = above->mbmi.mv.as_int;
  48. mv_bias(ref_frame_sign_bias[above->mbmi.ref_frame], refframe, mv, ref_frame_sign_bias);
  49. ++cntx;
  50. }
  51. *cntx += 2;
  52. }
  53. /* Process left */
  54. if (left->mbmi.ref_frame != INTRA_FRAME)
  55. {
  56. if (left->mbmi.mv.as_int)
  57. {
  58. int_mv this_mv;
  59. this_mv.as_int = left->mbmi.mv.as_int;
  60. mv_bias(ref_frame_sign_bias[left->mbmi.ref_frame], refframe, &this_mv, ref_frame_sign_bias);
  61. if (this_mv.as_int != mv->as_int)
  62. {
  63. (++mv)->as_int = this_mv.as_int;
  64. ++cntx;
  65. }
  66. *cntx += 2;
  67. }
  68. else
  69. cnt[CNT_INTRA] += 2;
  70. }
  71. /* Process above left */
  72. if (aboveleft->mbmi.ref_frame != INTRA_FRAME)
  73. {
  74. if (aboveleft->mbmi.mv.as_int)
  75. {
  76. int_mv this_mv;
  77. this_mv.as_int = aboveleft->mbmi.mv.as_int;
  78. mv_bias(ref_frame_sign_bias[aboveleft->mbmi.ref_frame], refframe, &this_mv, ref_frame_sign_bias);
  79. if (this_mv.as_int != mv->as_int)
  80. {
  81. (++mv)->as_int = this_mv.as_int;
  82. ++cntx;
  83. }
  84. *cntx += 1;
  85. }
  86. else
  87. cnt[CNT_INTRA] += 1;
  88. }
  89. /* If we have three distinct MV's ... */
  90. if (cnt[CNT_SPLITMV])
  91. {
  92. /* See if above-left MV can be merged with NEAREST */
  93. if (mv->as_int == near_mvs[CNT_NEAREST].as_int)
  94. cnt[CNT_NEAREST] += 1;
  95. }
  96. cnt[CNT_SPLITMV] = ((above->mbmi.mode == SPLITMV)
  97. + (left->mbmi.mode == SPLITMV)) * 2
  98. + (aboveleft->mbmi.mode == SPLITMV);
  99. /* Swap near and nearest if necessary */
  100. if (cnt[CNT_NEAR] > cnt[CNT_NEAREST])
  101. {
  102. int tmp;
  103. tmp = cnt[CNT_NEAREST];
  104. cnt[CNT_NEAREST] = cnt[CNT_NEAR];
  105. cnt[CNT_NEAR] = tmp;
  106. tmp = near_mvs[CNT_NEAREST].as_int;
  107. near_mvs[CNT_NEAREST].as_int = near_mvs[CNT_NEAR].as_int;
  108. near_mvs[CNT_NEAR].as_int = tmp;
  109. }
  110. /* Use near_mvs[0] to store the "best" MV */
  111. if (cnt[CNT_NEAREST] >= cnt[CNT_INTRA])
  112. near_mvs[CNT_INTRA] = near_mvs[CNT_NEAREST];
  113. /* Set up return values */
  114. best_mv->as_int = near_mvs[0].as_int;
  115. nearest->as_int = near_mvs[CNT_NEAREST].as_int;
  116. nearby->as_int = near_mvs[CNT_NEAR].as_int;
  117. }
  118. static void invert_and_clamp_mvs(int_mv *inv, int_mv *src, MACROBLOCKD *xd)
  119. {
  120. inv->as_mv.row = src->as_mv.row * -1;
  121. inv->as_mv.col = src->as_mv.col * -1;
  122. vp8_clamp_mv2(inv, xd);
  123. vp8_clamp_mv2(src, xd);
  124. }
  125. int vp8_find_near_mvs_bias
  126. (
  127. MACROBLOCKD *xd,
  128. const MODE_INFO *here,
  129. int_mv mode_mv_sb[2][MB_MODE_COUNT],
  130. int_mv best_mv_sb[2],
  131. int cnt[4],
  132. int refframe,
  133. int *ref_frame_sign_bias
  134. )
  135. {
  136. int sign_bias = ref_frame_sign_bias[refframe];
  137. vp8_find_near_mvs(xd,
  138. here,
  139. &mode_mv_sb[sign_bias][NEARESTMV],
  140. &mode_mv_sb[sign_bias][NEARMV],
  141. &best_mv_sb[sign_bias],
  142. cnt,
  143. refframe,
  144. ref_frame_sign_bias);
  145. invert_and_clamp_mvs(&mode_mv_sb[!sign_bias][NEARESTMV],
  146. &mode_mv_sb[sign_bias][NEARESTMV], xd);
  147. invert_and_clamp_mvs(&mode_mv_sb[!sign_bias][NEARMV],
  148. &mode_mv_sb[sign_bias][NEARMV], xd);
  149. invert_and_clamp_mvs(&best_mv_sb[!sign_bias],
  150. &best_mv_sb[sign_bias], xd);
  151. return sign_bias;
  152. }
  153. vp8_prob *vp8_mv_ref_probs(
  154. vp8_prob p[VP8_MVREFS-1], const int near_mv_ref_ct[4]
  155. )
  156. {
  157. p[0] = vp8_mode_contexts [near_mv_ref_ct[0]] [0];
  158. p[1] = vp8_mode_contexts [near_mv_ref_ct[1]] [1];
  159. p[2] = vp8_mode_contexts [near_mv_ref_ct[2]] [2];
  160. p[3] = vp8_mode_contexts [near_mv_ref_ct[3]] [3];
  161. /*p[3] = vp8_mode_contexts [near_mv_ref_ct[1] + near_mv_ref_ct[2] + near_mv_ref_ct[3]] [3];*/
  162. return p;
  163. }