vp9_encodemv.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 <math.h>
  11. #include "vp9/common/vp9_common.h"
  12. #include "vp9/common/vp9_entropymode.h"
  13. #include "vp9/common/vp9_systemdependent.h"
  14. #include "vp9/encoder/vp9_cost.h"
  15. #include "vp9/encoder/vp9_encodemv.h"
  16. static struct vp9_token mv_joint_encodings[MV_JOINTS];
  17. static struct vp9_token mv_class_encodings[MV_CLASSES];
  18. static struct vp9_token mv_fp_encodings[MV_FP_SIZE];
  19. static struct vp9_token mv_class0_encodings[CLASS0_SIZE];
  20. void vp9_entropy_mv_init(void) {
  21. vp9_tokens_from_tree(mv_joint_encodings, vp9_mv_joint_tree);
  22. vp9_tokens_from_tree(mv_class_encodings, vp9_mv_class_tree);
  23. vp9_tokens_from_tree(mv_class0_encodings, vp9_mv_class0_tree);
  24. vp9_tokens_from_tree(mv_fp_encodings, vp9_mv_fp_tree);
  25. }
  26. static void encode_mv_component(vp9_writer* w, int comp,
  27. const nmv_component* mvcomp, int usehp) {
  28. int offset;
  29. const int sign = comp < 0;
  30. const int mag = sign ? -comp : comp;
  31. const int mv_class = vp9_get_mv_class(mag - 1, &offset);
  32. const int d = offset >> 3; // int mv data
  33. const int fr = (offset >> 1) & 3; // fractional mv data
  34. const int hp = offset & 1; // high precision mv data
  35. assert(comp != 0);
  36. // Sign
  37. vp9_write(w, sign, mvcomp->sign);
  38. // Class
  39. vp9_write_token(w, vp9_mv_class_tree, mvcomp->classes,
  40. &mv_class_encodings[mv_class]);
  41. // Integer bits
  42. if (mv_class == MV_CLASS_0) {
  43. vp9_write_token(w, vp9_mv_class0_tree, mvcomp->class0,
  44. &mv_class0_encodings[d]);
  45. } else {
  46. int i;
  47. const int n = mv_class + CLASS0_BITS - 1; // number of bits
  48. for (i = 0; i < n; ++i)
  49. vp9_write(w, (d >> i) & 1, mvcomp->bits[i]);
  50. }
  51. // Fractional bits
  52. vp9_write_token(w, vp9_mv_fp_tree,
  53. mv_class == MV_CLASS_0 ? mvcomp->class0_fp[d] : mvcomp->fp,
  54. &mv_fp_encodings[fr]);
  55. // High precision bit
  56. if (usehp)
  57. vp9_write(w, hp,
  58. mv_class == MV_CLASS_0 ? mvcomp->class0_hp : mvcomp->hp);
  59. }
  60. static void build_nmv_component_cost_table(int *mvcost,
  61. const nmv_component* const mvcomp,
  62. int usehp) {
  63. int i, v;
  64. int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE];
  65. int bits_cost[MV_OFFSET_BITS][2];
  66. int class0_fp_cost[CLASS0_SIZE][MV_FP_SIZE], fp_cost[MV_FP_SIZE];
  67. int class0_hp_cost[2], hp_cost[2];
  68. sign_cost[0] = vp9_cost_zero(mvcomp->sign);
  69. sign_cost[1] = vp9_cost_one(mvcomp->sign);
  70. vp9_cost_tokens(class_cost, mvcomp->classes, vp9_mv_class_tree);
  71. vp9_cost_tokens(class0_cost, mvcomp->class0, vp9_mv_class0_tree);
  72. for (i = 0; i < MV_OFFSET_BITS; ++i) {
  73. bits_cost[i][0] = vp9_cost_zero(mvcomp->bits[i]);
  74. bits_cost[i][1] = vp9_cost_one(mvcomp->bits[i]);
  75. }
  76. for (i = 0; i < CLASS0_SIZE; ++i)
  77. vp9_cost_tokens(class0_fp_cost[i], mvcomp->class0_fp[i], vp9_mv_fp_tree);
  78. vp9_cost_tokens(fp_cost, mvcomp->fp, vp9_mv_fp_tree);
  79. if (usehp) {
  80. class0_hp_cost[0] = vp9_cost_zero(mvcomp->class0_hp);
  81. class0_hp_cost[1] = vp9_cost_one(mvcomp->class0_hp);
  82. hp_cost[0] = vp9_cost_zero(mvcomp->hp);
  83. hp_cost[1] = vp9_cost_one(mvcomp->hp);
  84. }
  85. mvcost[0] = 0;
  86. for (v = 1; v <= MV_MAX; ++v) {
  87. int z, c, o, d, e, f, cost = 0;
  88. z = v - 1;
  89. c = vp9_get_mv_class(z, &o);
  90. cost += class_cost[c];
  91. d = (o >> 3); /* int mv data */
  92. f = (o >> 1) & 3; /* fractional pel mv data */
  93. e = (o & 1); /* high precision mv data */
  94. if (c == MV_CLASS_0) {
  95. cost += class0_cost[d];
  96. } else {
  97. int i, b;
  98. b = c + CLASS0_BITS - 1; /* number of bits */
  99. for (i = 0; i < b; ++i)
  100. cost += bits_cost[i][((d >> i) & 1)];
  101. }
  102. if (c == MV_CLASS_0) {
  103. cost += class0_fp_cost[d][f];
  104. } else {
  105. cost += fp_cost[f];
  106. }
  107. if (usehp) {
  108. if (c == MV_CLASS_0) {
  109. cost += class0_hp_cost[e];
  110. } else {
  111. cost += hp_cost[e];
  112. }
  113. }
  114. mvcost[v] = cost + sign_cost[0];
  115. mvcost[-v] = cost + sign_cost[1];
  116. }
  117. }
  118. static int update_mv(vp9_writer *w, const unsigned int ct[2], vp9_prob *cur_p,
  119. vp9_prob upd_p) {
  120. const vp9_prob new_p = get_binary_prob(ct[0], ct[1]) | 1;
  121. const int update = cost_branch256(ct, *cur_p) + vp9_cost_zero(upd_p) >
  122. cost_branch256(ct, new_p) + vp9_cost_one(upd_p) + 7 * 256;
  123. vp9_write(w, update, upd_p);
  124. if (update) {
  125. *cur_p = new_p;
  126. vp9_write_literal(w, new_p >> 1, 7);
  127. }
  128. return update;
  129. }
  130. static void write_mv_update(const vp9_tree_index *tree,
  131. vp9_prob probs[/*n - 1*/],
  132. const unsigned int counts[/*n - 1*/],
  133. int n, vp9_writer *w) {
  134. int i;
  135. unsigned int branch_ct[32][2];
  136. // Assuming max number of probabilities <= 32
  137. assert(n <= 32);
  138. vp9_tree_probs_from_distribution(tree, branch_ct, counts);
  139. for (i = 0; i < n - 1; ++i)
  140. update_mv(w, branch_ct[i], &probs[i], MV_UPDATE_PROB);
  141. }
  142. void vp9_write_nmv_probs(VP9_COMMON *cm, int usehp, vp9_writer *w,
  143. nmv_context_counts *const counts) {
  144. int i, j;
  145. nmv_context *const mvc = &cm->fc->nmvc;
  146. write_mv_update(vp9_mv_joint_tree, mvc->joints, counts->joints, MV_JOINTS, w);
  147. for (i = 0; i < 2; ++i) {
  148. nmv_component *comp = &mvc->comps[i];
  149. nmv_component_counts *comp_counts = &counts->comps[i];
  150. update_mv(w, comp_counts->sign, &comp->sign, MV_UPDATE_PROB);
  151. write_mv_update(vp9_mv_class_tree, comp->classes, comp_counts->classes,
  152. MV_CLASSES, w);
  153. write_mv_update(vp9_mv_class0_tree, comp->class0, comp_counts->class0,
  154. CLASS0_SIZE, w);
  155. for (j = 0; j < MV_OFFSET_BITS; ++j)
  156. update_mv(w, comp_counts->bits[j], &comp->bits[j], MV_UPDATE_PROB);
  157. }
  158. for (i = 0; i < 2; ++i) {
  159. for (j = 0; j < CLASS0_SIZE; ++j)
  160. write_mv_update(vp9_mv_fp_tree, mvc->comps[i].class0_fp[j],
  161. counts->comps[i].class0_fp[j], MV_FP_SIZE, w);
  162. write_mv_update(vp9_mv_fp_tree, mvc->comps[i].fp, counts->comps[i].fp,
  163. MV_FP_SIZE, w);
  164. }
  165. if (usehp) {
  166. for (i = 0; i < 2; ++i) {
  167. update_mv(w, counts->comps[i].class0_hp, &mvc->comps[i].class0_hp,
  168. MV_UPDATE_PROB);
  169. update_mv(w, counts->comps[i].hp, &mvc->comps[i].hp, MV_UPDATE_PROB);
  170. }
  171. }
  172. }
  173. void vp9_encode_mv(VP9_COMP* cpi, vp9_writer* w,
  174. const MV* mv, const MV* ref,
  175. const nmv_context* mvctx, int usehp) {
  176. const MV diff = {mv->row - ref->row,
  177. mv->col - ref->col};
  178. const MV_JOINT_TYPE j = vp9_get_mv_joint(&diff);
  179. usehp = usehp && vp9_use_mv_hp(ref);
  180. vp9_write_token(w, vp9_mv_joint_tree, mvctx->joints, &mv_joint_encodings[j]);
  181. if (mv_joint_vertical(j))
  182. encode_mv_component(w, diff.row, &mvctx->comps[0], usehp);
  183. if (mv_joint_horizontal(j))
  184. encode_mv_component(w, diff.col, &mvctx->comps[1], usehp);
  185. // If auto_mv_step_size is enabled then keep track of the largest
  186. // motion vector component used.
  187. if (cpi->sf.mv.auto_mv_step_size) {
  188. unsigned int maxv = MAX(abs(mv->row), abs(mv->col)) >> 3;
  189. cpi->max_mv_magnitude = MAX(maxv, cpi->max_mv_magnitude);
  190. }
  191. }
  192. void vp9_build_nmv_cost_table(int *mvjoint, int *mvcost[2],
  193. const nmv_context* ctx, int usehp) {
  194. vp9_cost_tokens(mvjoint, ctx->joints, vp9_mv_joint_tree);
  195. build_nmv_component_cost_table(mvcost[0], &ctx->comps[0], usehp);
  196. build_nmv_component_cost_table(mvcost[1], &ctx->comps[1], usehp);
  197. }
  198. static void inc_mvs(const MB_MODE_INFO *mbmi, const int_mv mvs[2],
  199. nmv_context_counts *counts) {
  200. int i;
  201. for (i = 0; i < 1 + has_second_ref(mbmi); ++i) {
  202. const MV *ref = &mbmi->ref_mvs[mbmi->ref_frame[i]][0].as_mv;
  203. const MV diff = {mvs[i].as_mv.row - ref->row,
  204. mvs[i].as_mv.col - ref->col};
  205. vp9_inc_mv(&diff, counts);
  206. }
  207. }
  208. void vp9_update_mv_count(ThreadData *td) {
  209. const MACROBLOCKD *xd = &td->mb.e_mbd;
  210. const MODE_INFO *mi = xd->mi[0];
  211. const MB_MODE_INFO *const mbmi = &mi->mbmi;
  212. if (mbmi->sb_type < BLOCK_8X8) {
  213. const int num_4x4_w = num_4x4_blocks_wide_lookup[mbmi->sb_type];
  214. const int num_4x4_h = num_4x4_blocks_high_lookup[mbmi->sb_type];
  215. int idx, idy;
  216. for (idy = 0; idy < 2; idy += num_4x4_h) {
  217. for (idx = 0; idx < 2; idx += num_4x4_w) {
  218. const int i = idy * 2 + idx;
  219. if (mi->bmi[i].as_mode == NEWMV)
  220. inc_mvs(mbmi, mi->bmi[i].as_mv, &td->counts->mv);
  221. }
  222. }
  223. } else {
  224. if (mbmi->mode == NEWMV)
  225. inc_mvs(mbmi, mbmi->mv, &td->counts->mv);
  226. }
  227. }