vp9_entropymv.c 8.4 KB

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