2
0

vp9_encodemv.c 9.3 KB

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