findnearmv.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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(MACROBLOCKD *xd, const MODE_INFO *here, int_mv *nearest,
  21. int_mv *nearby, int_mv *best_mv, int cnt[4],
  22. int refframe, int *ref_frame_sign_bias) {
  23. const MODE_INFO *above = here - xd->mode_info_stride;
  24. const MODE_INFO *left = here - 1;
  25. const MODE_INFO *aboveleft = above - 1;
  26. int_mv near_mvs[4];
  27. int_mv *mv = near_mvs;
  28. int *cntx = cnt;
  29. enum { CNT_INTRA, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
  30. /* Zero accumulators */
  31. mv[0].as_int = mv[1].as_int = mv[2].as_int = 0;
  32. cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0;
  33. /* Process above */
  34. if (above->mbmi.ref_frame != INTRA_FRAME) {
  35. if (above->mbmi.mv.as_int) {
  36. (++mv)->as_int = above->mbmi.mv.as_int;
  37. mv_bias(ref_frame_sign_bias[above->mbmi.ref_frame], refframe, mv,
  38. ref_frame_sign_bias);
  39. ++cntx;
  40. }
  41. *cntx += 2;
  42. }
  43. /* Process left */
  44. if (left->mbmi.ref_frame != INTRA_FRAME) {
  45. if (left->mbmi.mv.as_int) {
  46. int_mv this_mv;
  47. this_mv.as_int = left->mbmi.mv.as_int;
  48. mv_bias(ref_frame_sign_bias[left->mbmi.ref_frame], refframe, &this_mv,
  49. ref_frame_sign_bias);
  50. if (this_mv.as_int != mv->as_int) {
  51. (++mv)->as_int = this_mv.as_int;
  52. ++cntx;
  53. }
  54. *cntx += 2;
  55. } else {
  56. cnt[CNT_INTRA] += 2;
  57. }
  58. }
  59. /* Process above left */
  60. if (aboveleft->mbmi.ref_frame != INTRA_FRAME) {
  61. if (aboveleft->mbmi.mv.as_int) {
  62. int_mv this_mv;
  63. this_mv.as_int = aboveleft->mbmi.mv.as_int;
  64. mv_bias(ref_frame_sign_bias[aboveleft->mbmi.ref_frame], refframe,
  65. &this_mv, ref_frame_sign_bias);
  66. if (this_mv.as_int != mv->as_int) {
  67. (++mv)->as_int = this_mv.as_int;
  68. ++cntx;
  69. }
  70. *cntx += 1;
  71. } else {
  72. cnt[CNT_INTRA] += 1;
  73. }
  74. }
  75. /* If we have three distinct MV's ... */
  76. if (cnt[CNT_SPLITMV]) {
  77. /* See if above-left MV can be merged with NEAREST */
  78. if (mv->as_int == near_mvs[CNT_NEAREST].as_int) cnt[CNT_NEAREST] += 1;
  79. }
  80. cnt[CNT_SPLITMV] =
  81. ((above->mbmi.mode == SPLITMV) + (left->mbmi.mode == SPLITMV)) * 2 +
  82. (aboveleft->mbmi.mode == SPLITMV);
  83. /* Swap near and nearest if necessary */
  84. if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
  85. int tmp;
  86. tmp = cnt[CNT_NEAREST];
  87. cnt[CNT_NEAREST] = cnt[CNT_NEAR];
  88. cnt[CNT_NEAR] = tmp;
  89. tmp = near_mvs[CNT_NEAREST].as_int;
  90. near_mvs[CNT_NEAREST].as_int = near_mvs[CNT_NEAR].as_int;
  91. near_mvs[CNT_NEAR].as_int = tmp;
  92. }
  93. /* Use near_mvs[0] to store the "best" MV */
  94. if (cnt[CNT_NEAREST] >= cnt[CNT_INTRA]) {
  95. near_mvs[CNT_INTRA] = near_mvs[CNT_NEAREST];
  96. }
  97. /* Set up return values */
  98. best_mv->as_int = near_mvs[0].as_int;
  99. nearest->as_int = near_mvs[CNT_NEAREST].as_int;
  100. nearby->as_int = near_mvs[CNT_NEAR].as_int;
  101. }
  102. static void invert_and_clamp_mvs(int_mv *inv, int_mv *src, MACROBLOCKD *xd) {
  103. inv->as_mv.row = src->as_mv.row * -1;
  104. inv->as_mv.col = src->as_mv.col * -1;
  105. vp8_clamp_mv2(inv, xd);
  106. vp8_clamp_mv2(src, xd);
  107. }
  108. int vp8_find_near_mvs_bias(MACROBLOCKD *xd, const MODE_INFO *here,
  109. int_mv mode_mv_sb[2][MB_MODE_COUNT],
  110. int_mv best_mv_sb[2], int cnt[4], int refframe,
  111. int *ref_frame_sign_bias) {
  112. int sign_bias = ref_frame_sign_bias[refframe];
  113. vp8_find_near_mvs(xd, here, &mode_mv_sb[sign_bias][NEARESTMV],
  114. &mode_mv_sb[sign_bias][NEARMV], &best_mv_sb[sign_bias], cnt,
  115. refframe, ref_frame_sign_bias);
  116. invert_and_clamp_mvs(&mode_mv_sb[!sign_bias][NEARESTMV],
  117. &mode_mv_sb[sign_bias][NEARESTMV], xd);
  118. invert_and_clamp_mvs(&mode_mv_sb[!sign_bias][NEARMV],
  119. &mode_mv_sb[sign_bias][NEARMV], xd);
  120. invert_and_clamp_mvs(&best_mv_sb[!sign_bias], &best_mv_sb[sign_bias], xd);
  121. return sign_bias;
  122. }
  123. vp8_prob *vp8_mv_ref_probs(vp8_prob p[VP8_MVREFS - 1],
  124. const int near_mv_ref_ct[4]) {
  125. p[0] = vp8_mode_contexts[near_mv_ref_ct[0]][0];
  126. p[1] = vp8_mode_contexts[near_mv_ref_ct[1]][1];
  127. p[2] = vp8_mode_contexts[near_mv_ref_ct[2]][2];
  128. p[3] = vp8_mode_contexts[near_mv_ref_ct[3]][3];
  129. /* p[3] = vp8_mode_contexts[near_mv_ref_ct[1] + near_mv_ref_ct[2] +
  130. near_mv_ref_ct[3]][3]; */
  131. return p;
  132. }