2
0

findnearmv.c 5.3 KB

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