encodemv.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 "vp8/common/common.h"
  11. #include "encodemv.h"
  12. #include "vp8/common/entropymode.h"
  13. #include "vp8/common/systemdependent.h"
  14. #include "vpx_ports/system_state.h"
  15. #include <math.h>
  16. #ifdef VP8_ENTROPY_STATS
  17. extern unsigned int active_section;
  18. #endif
  19. static void encode_mvcomponent(vp8_writer *const w, const int v,
  20. const struct mv_context *mvc) {
  21. const vp8_prob *p = mvc->prob;
  22. const int x = v < 0 ? -v : v;
  23. if (x < mvnum_short) /* Small */
  24. {
  25. vp8_write(w, 0, p[mvpis_short]);
  26. vp8_treed_write(w, vp8_small_mvtree, p + MVPshort, x, 3);
  27. if (!x) return; /* no sign bit */
  28. } else /* Large */
  29. {
  30. int i = 0;
  31. vp8_write(w, 1, p[mvpis_short]);
  32. do
  33. vp8_write(w, (x >> i) & 1, p[MVPbits + i]);
  34. while (++i < 3);
  35. i = mvlong_width - 1; /* Skip bit 3, which is sometimes implicit */
  36. do
  37. vp8_write(w, (x >> i) & 1, p[MVPbits + i]);
  38. while (--i > 3);
  39. if (x & 0xFFF0) vp8_write(w, (x >> 3) & 1, p[MVPbits + 3]);
  40. }
  41. vp8_write(w, v < 0, p[MVPsign]);
  42. }
  43. #if 0
  44. static int max_mv_r = 0;
  45. static int max_mv_c = 0;
  46. #endif
  47. void vp8_encode_motion_vector(vp8_writer *w, const MV *mv,
  48. const MV_CONTEXT *mvc) {
  49. #if 0
  50. {
  51. if (abs(mv->row >> 1) > max_mv_r)
  52. {
  53. FILE *f = fopen("maxmv.stt", "a");
  54. max_mv_r = abs(mv->row >> 1);
  55. fprintf(f, "New Mv Row Max %6d\n", (mv->row >> 1));
  56. if ((abs(mv->row) / 2) != max_mv_r)
  57. fprintf(f, "MV Row conversion error %6d\n", abs(mv->row) / 2);
  58. fclose(f);
  59. }
  60. if (abs(mv->col >> 1) > max_mv_c)
  61. {
  62. FILE *f = fopen("maxmv.stt", "a");
  63. fprintf(f, "New Mv Col Max %6d\n", (mv->col >> 1));
  64. max_mv_c = abs(mv->col >> 1);
  65. fclose(f);
  66. }
  67. }
  68. #endif
  69. encode_mvcomponent(w, mv->row >> 1, &mvc[0]);
  70. encode_mvcomponent(w, mv->col >> 1, &mvc[1]);
  71. }
  72. static unsigned int cost_mvcomponent(const int v,
  73. const struct mv_context *mvc) {
  74. const vp8_prob *p = mvc->prob;
  75. const int x = v;
  76. unsigned int cost;
  77. if (x < mvnum_short) {
  78. cost = vp8_cost_zero(p[mvpis_short]) +
  79. vp8_treed_cost(vp8_small_mvtree, p + MVPshort, x, 3);
  80. if (!x) return cost;
  81. } else {
  82. int i = 0;
  83. cost = vp8_cost_one(p[mvpis_short]);
  84. do {
  85. cost += vp8_cost_bit(p[MVPbits + i], (x >> i) & 1);
  86. } while (++i < 3);
  87. i = mvlong_width - 1; /* Skip bit 3, which is sometimes implicit */
  88. do {
  89. cost += vp8_cost_bit(p[MVPbits + i], (x >> i) & 1);
  90. } while (--i > 3);
  91. if (x & 0xFFF0) cost += vp8_cost_bit(p[MVPbits + 3], (x >> 3) & 1);
  92. }
  93. return cost; /* + vp8_cost_bit( p [MVPsign], v < 0); */
  94. }
  95. void vp8_build_component_cost_table(int *mvcost[2], const MV_CONTEXT *mvc,
  96. int mvc_flag[2]) {
  97. int i = 1;
  98. unsigned int cost0 = 0;
  99. unsigned int cost1 = 0;
  100. vpx_clear_system_state();
  101. i = 1;
  102. if (mvc_flag[0]) {
  103. mvcost[0][0] = cost_mvcomponent(0, &mvc[0]);
  104. do {
  105. cost0 = cost_mvcomponent(i, &mvc[0]);
  106. mvcost[0][i] = cost0 + vp8_cost_zero(mvc[0].prob[MVPsign]);
  107. mvcost[0][-i] = cost0 + vp8_cost_one(mvc[0].prob[MVPsign]);
  108. } while (++i <= mv_max);
  109. }
  110. i = 1;
  111. if (mvc_flag[1]) {
  112. mvcost[1][0] = cost_mvcomponent(0, &mvc[1]);
  113. do {
  114. cost1 = cost_mvcomponent(i, &mvc[1]);
  115. mvcost[1][i] = cost1 + vp8_cost_zero(mvc[1].prob[MVPsign]);
  116. mvcost[1][-i] = cost1 + vp8_cost_one(mvc[1].prob[MVPsign]);
  117. } while (++i <= mv_max);
  118. }
  119. }
  120. /* Motion vector probability table update depends on benefit.
  121. * Small correction allows for the fact that an update to an MV probability
  122. * may have benefit in subsequent frames as well as the current one.
  123. */
  124. #define MV_PROB_UPDATE_CORRECTION -1
  125. static void calc_prob(vp8_prob *p, const unsigned int ct[2]) {
  126. const unsigned int tot = ct[0] + ct[1];
  127. if (tot) {
  128. const vp8_prob x = ((ct[0] * 255) / tot) & -2;
  129. *p = x ? x : 1;
  130. }
  131. }
  132. static void update(vp8_writer *const w, const unsigned int ct[2],
  133. vp8_prob *const cur_p, const vp8_prob new_p,
  134. const vp8_prob update_p, int *updated) {
  135. const int cur_b = vp8_cost_branch(ct, *cur_p);
  136. const int new_b = vp8_cost_branch(ct, new_p);
  137. const int cost =
  138. 7 + MV_PROB_UPDATE_CORRECTION +
  139. ((vp8_cost_one(update_p) - vp8_cost_zero(update_p) + 128) >> 8);
  140. if (cur_b - new_b > cost) {
  141. *cur_p = new_p;
  142. vp8_write(w, 1, update_p);
  143. vp8_write_literal(w, new_p >> 1, 7);
  144. *updated = 1;
  145. } else
  146. vp8_write(w, 0, update_p);
  147. }
  148. static void write_component_probs(vp8_writer *const w,
  149. struct mv_context *cur_mvc,
  150. const struct mv_context *default_mvc_,
  151. const struct mv_context *update_mvc,
  152. const unsigned int events[MVvals],
  153. unsigned int rc, int *updated) {
  154. vp8_prob *Pcur = cur_mvc->prob;
  155. const vp8_prob *default_mvc = default_mvc_->prob;
  156. const vp8_prob *Pupdate = update_mvc->prob;
  157. unsigned int is_short_ct[2], sign_ct[2];
  158. unsigned int bit_ct[mvlong_width][2];
  159. unsigned int short_ct[mvnum_short];
  160. unsigned int short_bct[mvnum_short - 1][2];
  161. vp8_prob Pnew[MVPcount];
  162. (void)rc;
  163. vp8_copy_array(Pnew, default_mvc, MVPcount);
  164. vp8_zero(is_short_ct) vp8_zero(sign_ct) vp8_zero(bit_ct) vp8_zero(short_ct)
  165. vp8_zero(short_bct)
  166. /* j=0 */
  167. {
  168. const int c = events[mv_max];
  169. is_short_ct[0] += c; /* Short vector */
  170. short_ct[0] += c; /* Magnitude distribution */
  171. }
  172. /* j: 1 ~ mv_max (1023) */
  173. {
  174. int j = 1;
  175. do {
  176. const int c1 = events[mv_max + j]; /* positive */
  177. const int c2 = events[mv_max - j]; /* negative */
  178. const int c = c1 + c2;
  179. int a = j;
  180. sign_ct[0] += c1;
  181. sign_ct[1] += c2;
  182. if (a < mvnum_short) {
  183. is_short_ct[0] += c; /* Short vector */
  184. short_ct[a] += c; /* Magnitude distribution */
  185. } else {
  186. int k = mvlong_width - 1;
  187. is_short_ct[1] += c; /* Long vector */
  188. /* bit 3 not always encoded. */
  189. do {
  190. bit_ct[k][(a >> k) & 1] += c;
  191. } while (--k >= 0);
  192. }
  193. } while (++j <= mv_max);
  194. }
  195. calc_prob(Pnew + mvpis_short, is_short_ct);
  196. calc_prob(Pnew + MVPsign, sign_ct);
  197. {
  198. vp8_prob p[mvnum_short - 1]; /* actually only need branch ct */
  199. int j = 0;
  200. vp8_tree_probs_from_distribution(8, vp8_small_mvencodings, vp8_small_mvtree,
  201. p, short_bct, short_ct, 256, 1);
  202. do {
  203. calc_prob(Pnew + MVPshort + j, short_bct[j]);
  204. } while (++j < mvnum_short - 1);
  205. }
  206. {
  207. int j = 0;
  208. do {
  209. calc_prob(Pnew + MVPbits + j, bit_ct[j]);
  210. } while (++j < mvlong_width);
  211. }
  212. update(w, is_short_ct, Pcur + mvpis_short, Pnew[mvpis_short], *Pupdate++,
  213. updated);
  214. update(w, sign_ct, Pcur + MVPsign, Pnew[MVPsign], *Pupdate++, updated);
  215. {
  216. const vp8_prob *const new_p = Pnew + MVPshort;
  217. vp8_prob *const cur_p = Pcur + MVPshort;
  218. int j = 0;
  219. do {
  220. update(w, short_bct[j], cur_p + j, new_p[j], *Pupdate++, updated);
  221. } while (++j < mvnum_short - 1);
  222. }
  223. {
  224. const vp8_prob *const new_p = Pnew + MVPbits;
  225. vp8_prob *const cur_p = Pcur + MVPbits;
  226. int j = 0;
  227. do {
  228. update(w, bit_ct[j], cur_p + j, new_p[j], *Pupdate++, updated);
  229. } while (++j < mvlong_width);
  230. }
  231. }
  232. void vp8_write_mvprobs(VP8_COMP *cpi) {
  233. vp8_writer *const w = cpi->bc;
  234. MV_CONTEXT *mvc = cpi->common.fc.mvc;
  235. int flags[2] = { 0, 0 };
  236. #ifdef VP8_ENTROPY_STATS
  237. active_section = 4;
  238. #endif
  239. write_component_probs(w, &mvc[0], &vp8_default_mv_context[0],
  240. &vp8_mv_update_probs[0], cpi->mb.MVcount[0], 0,
  241. &flags[0]);
  242. write_component_probs(w, &mvc[1], &vp8_default_mv_context[1],
  243. &vp8_mv_update_probs[1], cpi->mb.MVcount[1], 1,
  244. &flags[1]);
  245. if (flags[0] || flags[1]) {
  246. vp8_build_component_cost_table(
  247. cpi->mb.mvcost, (const MV_CONTEXT *)cpi->common.fc.mvc, flags);
  248. }
  249. #ifdef VP8_ENTROPY_STATS
  250. active_section = 5;
  251. #endif
  252. }