2
0

encodemv.c 8.3 KB

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