vp9_entropymv.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "vp9/common/vp9_onyxc_int.h"
  11. #include "vp9/common/vp9_entropymv.h"
  12. const vpx_tree_index vp9_mv_joint_tree[TREE_SIZE(MV_JOINTS)] = {
  13. -MV_JOINT_ZERO, 2, -MV_JOINT_HNZVZ, 4, -MV_JOINT_HZVNZ, -MV_JOINT_HNZVNZ
  14. };
  15. const vpx_tree_index vp9_mv_class_tree[TREE_SIZE(MV_CLASSES)] = {
  16. -MV_CLASS_0, 2, -MV_CLASS_1, 4, 6,
  17. 8, -MV_CLASS_2, -MV_CLASS_3, 10, 12,
  18. -MV_CLASS_4, -MV_CLASS_5, -MV_CLASS_6, 14, 16,
  19. 18, -MV_CLASS_7, -MV_CLASS_8, -MV_CLASS_9, -MV_CLASS_10,
  20. };
  21. const vpx_tree_index vp9_mv_class0_tree[TREE_SIZE(CLASS0_SIZE)] = {
  22. -0, -1,
  23. };
  24. const vpx_tree_index vp9_mv_fp_tree[TREE_SIZE(MV_FP_SIZE)] = { -0, 2, -1,
  25. 4, -2, -3 };
  26. static const nmv_context default_nmv_context = {
  27. { 32, 64, 96 },
  28. { {
  29. // Vertical component
  30. 128, // sign
  31. { 224, 144, 192, 168, 192, 176, 192, 198, 198, 245 }, // class
  32. { 216 }, // class0
  33. { 136, 140, 148, 160, 176, 192, 224, 234, 234, 240 }, // bits
  34. { { 128, 128, 64 }, { 96, 112, 64 } }, // class0_fp
  35. { 64, 96, 64 }, // fp
  36. 160, // class0_hp bit
  37. 128, // hp
  38. },
  39. {
  40. // Horizontal component
  41. 128, // sign
  42. { 216, 128, 176, 160, 176, 176, 192, 198, 198, 208 }, // class
  43. { 208 }, // class0
  44. { 136, 140, 148, 160, 176, 192, 224, 234, 234, 240 }, // bits
  45. { { 128, 128, 64 }, { 96, 112, 64 } }, // class0_fp
  46. { 64, 96, 64 }, // fp
  47. 160, // class0_hp bit
  48. 128, // hp
  49. } },
  50. };
  51. static const uint8_t log_in_base_2[] = {
  52. 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  53. 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  54. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  55. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  56. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7,
  57. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  58. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  59. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  60. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 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, 9, 9, 9, 9, 9, 9, 9, 9,
  86. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  87. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  88. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  89. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  90. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  91. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10
  92. };
  93. static INLINE int mv_class_base(MV_CLASS_TYPE c) {
  94. return c ? CLASS0_SIZE << (c + 2) : 0;
  95. }
  96. MV_CLASS_TYPE vp9_get_mv_class(int z, int *offset) {
  97. const MV_CLASS_TYPE c = (z >= CLASS0_SIZE * 4096)
  98. ? MV_CLASS_10
  99. : (MV_CLASS_TYPE)log_in_base_2[z >> 3];
  100. if (offset) *offset = z - mv_class_base(c);
  101. return c;
  102. }
  103. static void inc_mv_component(int v, nmv_component_counts *comp_counts, int incr,
  104. int usehp) {
  105. int s, z, c, o, d, e, f;
  106. assert(v != 0); /* should not be zero */
  107. s = v < 0;
  108. comp_counts->sign[s] += incr;
  109. z = (s ? -v : v) - 1; /* magnitude - 1 */
  110. c = vp9_get_mv_class(z, &o);
  111. comp_counts->classes[c] += incr;
  112. d = (o >> 3); /* int mv data */
  113. f = (o >> 1) & 3; /* fractional pel mv data */
  114. e = (o & 1); /* high precision mv data */
  115. if (c == MV_CLASS_0) {
  116. comp_counts->class0[d] += incr;
  117. comp_counts->class0_fp[d][f] += incr;
  118. comp_counts->class0_hp[e] += usehp * incr;
  119. } else {
  120. int i;
  121. int b = c + CLASS0_BITS - 1; // number of bits
  122. for (i = 0; i < b; ++i) comp_counts->bits[i][((d >> i) & 1)] += incr;
  123. comp_counts->fp[f] += incr;
  124. comp_counts->hp[e] += usehp * incr;
  125. }
  126. }
  127. void vp9_inc_mv(const MV *mv, nmv_context_counts *counts) {
  128. if (counts != NULL) {
  129. const MV_JOINT_TYPE j = vp9_get_mv_joint(mv);
  130. ++counts->joints[j];
  131. if (mv_joint_vertical(j)) {
  132. inc_mv_component(mv->row, &counts->comps[0], 1, 1);
  133. }
  134. if (mv_joint_horizontal(j)) {
  135. inc_mv_component(mv->col, &counts->comps[1], 1, 1);
  136. }
  137. }
  138. }
  139. void vp9_adapt_mv_probs(VP9_COMMON *cm, int allow_hp) {
  140. int i, j;
  141. nmv_context *fc = &cm->fc->nmvc;
  142. const nmv_context *pre_fc = &cm->frame_contexts[cm->frame_context_idx].nmvc;
  143. const nmv_context_counts *counts = &cm->counts.mv;
  144. vpx_tree_merge_probs(vp9_mv_joint_tree, pre_fc->joints, counts->joints,
  145. fc->joints);
  146. for (i = 0; i < 2; ++i) {
  147. nmv_component *comp = &fc->comps[i];
  148. const nmv_component *pre_comp = &pre_fc->comps[i];
  149. const nmv_component_counts *c = &counts->comps[i];
  150. comp->sign = mode_mv_merge_probs(pre_comp->sign, c->sign);
  151. vpx_tree_merge_probs(vp9_mv_class_tree, pre_comp->classes, c->classes,
  152. comp->classes);
  153. vpx_tree_merge_probs(vp9_mv_class0_tree, pre_comp->class0, c->class0,
  154. comp->class0);
  155. for (j = 0; j < MV_OFFSET_BITS; ++j)
  156. comp->bits[j] = mode_mv_merge_probs(pre_comp->bits[j], c->bits[j]);
  157. for (j = 0; j < CLASS0_SIZE; ++j)
  158. vpx_tree_merge_probs(vp9_mv_fp_tree, pre_comp->class0_fp[j],
  159. c->class0_fp[j], comp->class0_fp[j]);
  160. vpx_tree_merge_probs(vp9_mv_fp_tree, pre_comp->fp, c->fp, comp->fp);
  161. if (allow_hp) {
  162. comp->class0_hp = mode_mv_merge_probs(pre_comp->class0_hp, c->class0_hp);
  163. comp->hp = mode_mv_merge_probs(pre_comp->hp, c->hp);
  164. }
  165. }
  166. }
  167. void vp9_init_mv_probs(VP9_COMMON *cm) { cm->fc->nmvc = default_nmv_context; }