/media/libvpx/vp8/encoder/encodemv.c

http://github.com/zpao/v8monkey · C · 417 lines · 274 code · 90 blank · 53 comment · 30 complexity · c01c71c128a618707c33a5d2568ce2df MD5 · raw file

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