vp9_subexp.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2013 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 "vp9/common/vp9_common.h"
  11. #include "vp9/common/vp9_entropy.h"
  12. #include "vp9/encoder/vp9_cost.h"
  13. #include "vp9/encoder/vp9_subexp.h"
  14. #include "vp9/encoder/vp9_writer.h"
  15. #define vp9_cost_upd256 ((int)(vp9_cost_one(upd) - vp9_cost_zero(upd)))
  16. static const int update_bits[255] = {
  17. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  18. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  19. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  20. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  21. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  22. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  23. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  24. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  25. 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  26. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  27. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  28. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  29. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  30. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  31. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  32. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 0,
  33. };
  34. static int recenter_nonneg(int v, int m) {
  35. if (v > (m << 1))
  36. return v;
  37. else if (v >= m)
  38. return ((v - m) << 1);
  39. else
  40. return ((m - v) << 1) - 1;
  41. }
  42. static int remap_prob(int v, int m) {
  43. int i;
  44. static const int map_table[MAX_PROB - 1] = {
  45. // generated by:
  46. // map_table[j] = split_index(j, MAX_PROB - 1, MODULUS_PARAM);
  47. 20, 21, 22, 23, 24, 25, 0, 26, 27, 28, 29, 30, 31, 32, 33,
  48. 34, 35, 36, 37, 1, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  49. 48, 49, 2, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
  50. 3, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 4, 74,
  51. 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 5, 86, 87, 88,
  52. 89, 90, 91, 92, 93, 94, 95, 96, 97, 6, 98, 99, 100, 101, 102,
  53. 103, 104, 105, 106, 107, 108, 109, 7, 110, 111, 112, 113, 114, 115, 116,
  54. 117, 118, 119, 120, 121, 8, 122, 123, 124, 125, 126, 127, 128, 129, 130,
  55. 131, 132, 133, 9, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144,
  56. 145, 10, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 11,
  57. 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 12, 170, 171,
  58. 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 13, 182, 183, 184, 185,
  59. 186, 187, 188, 189, 190, 191, 192, 193, 14, 194, 195, 196, 197, 198, 199,
  60. 200, 201, 202, 203, 204, 205, 15, 206, 207, 208, 209, 210, 211, 212, 213,
  61. 214, 215, 216, 217, 16, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
  62. 228, 229, 17, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241,
  63. 18, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 19,
  64. };
  65. v--;
  66. m--;
  67. if ((m << 1) <= MAX_PROB)
  68. i = recenter_nonneg(v, m) - 1;
  69. else
  70. i = recenter_nonneg(MAX_PROB - 1 - v, MAX_PROB - 1 - m) - 1;
  71. i = map_table[i];
  72. return i;
  73. }
  74. static int prob_diff_update_cost(vp9_prob newp, vp9_prob oldp) {
  75. int delp = remap_prob(newp, oldp);
  76. return update_bits[delp] * 256;
  77. }
  78. static void encode_uniform(vp9_writer *w, int v) {
  79. const int l = 8;
  80. const int m = (1 << l) - 191;
  81. if (v < m) {
  82. vp9_write_literal(w, v, l - 1);
  83. } else {
  84. vp9_write_literal(w, m + ((v - m) >> 1), l - 1);
  85. vp9_write_literal(w, (v - m) & 1, 1);
  86. }
  87. }
  88. static INLINE int write_bit_gte(vp9_writer *w, int word, int test) {
  89. vp9_write_literal(w, word >= test, 1);
  90. return word >= test;
  91. }
  92. static void encode_term_subexp(vp9_writer *w, int word) {
  93. if (!write_bit_gte(w, word, 16)) {
  94. vp9_write_literal(w, word, 4);
  95. } else if (!write_bit_gte(w, word, 32)) {
  96. vp9_write_literal(w, word - 16, 4);
  97. } else if (!write_bit_gte(w, word, 64)) {
  98. vp9_write_literal(w, word - 32, 5);
  99. } else {
  100. encode_uniform(w, word - 64);
  101. }
  102. }
  103. void vp9_write_prob_diff_update(vp9_writer *w, vp9_prob newp, vp9_prob oldp) {
  104. const int delp = remap_prob(newp, oldp);
  105. encode_term_subexp(w, delp);
  106. }
  107. int vp9_prob_diff_update_savings_search(const unsigned int *ct,
  108. vp9_prob oldp, vp9_prob *bestp,
  109. vp9_prob upd) {
  110. const int old_b = cost_branch256(ct, oldp);
  111. int bestsavings = 0;
  112. vp9_prob newp, bestnewp = oldp;
  113. const int step = *bestp > oldp ? -1 : 1;
  114. for (newp = *bestp; newp != oldp; newp += step) {
  115. const int new_b = cost_branch256(ct, newp);
  116. const int update_b = prob_diff_update_cost(newp, oldp) + vp9_cost_upd256;
  117. const int savings = old_b - new_b - update_b;
  118. if (savings > bestsavings) {
  119. bestsavings = savings;
  120. bestnewp = newp;
  121. }
  122. }
  123. *bestp = bestnewp;
  124. return bestsavings;
  125. }
  126. int vp9_prob_diff_update_savings_search_model(const unsigned int *ct,
  127. const vp9_prob *oldp,
  128. vp9_prob *bestp,
  129. vp9_prob upd,
  130. int stepsize) {
  131. int i, old_b, new_b, update_b, savings, bestsavings, step;
  132. int newp;
  133. vp9_prob bestnewp, newplist[ENTROPY_NODES], oldplist[ENTROPY_NODES];
  134. vp9_model_to_full_probs(oldp, oldplist);
  135. memcpy(newplist, oldp, sizeof(vp9_prob) * UNCONSTRAINED_NODES);
  136. for (i = UNCONSTRAINED_NODES, old_b = 0; i < ENTROPY_NODES; ++i)
  137. old_b += cost_branch256(ct + 2 * i, oldplist[i]);
  138. old_b += cost_branch256(ct + 2 * PIVOT_NODE, oldplist[PIVOT_NODE]);
  139. bestsavings = 0;
  140. bestnewp = oldp[PIVOT_NODE];
  141. if (*bestp > oldp[PIVOT_NODE]) {
  142. step = -stepsize;
  143. for (newp = *bestp; newp > oldp[PIVOT_NODE]; newp += step) {
  144. if (newp < 1 || newp > 255)
  145. continue;
  146. newplist[PIVOT_NODE] = newp;
  147. vp9_model_to_full_probs(newplist, newplist);
  148. for (i = UNCONSTRAINED_NODES, new_b = 0; i < ENTROPY_NODES; ++i)
  149. new_b += cost_branch256(ct + 2 * i, newplist[i]);
  150. new_b += cost_branch256(ct + 2 * PIVOT_NODE, newplist[PIVOT_NODE]);
  151. update_b = prob_diff_update_cost(newp, oldp[PIVOT_NODE]) +
  152. vp9_cost_upd256;
  153. savings = old_b - new_b - update_b;
  154. if (savings > bestsavings) {
  155. bestsavings = savings;
  156. bestnewp = newp;
  157. }
  158. }
  159. } else {
  160. step = stepsize;
  161. for (newp = *bestp; newp < oldp[PIVOT_NODE]; newp += step) {
  162. if (newp < 1 || newp > 255)
  163. continue;
  164. newplist[PIVOT_NODE] = newp;
  165. vp9_model_to_full_probs(newplist, newplist);
  166. for (i = UNCONSTRAINED_NODES, new_b = 0; i < ENTROPY_NODES; ++i)
  167. new_b += cost_branch256(ct + 2 * i, newplist[i]);
  168. new_b += cost_branch256(ct + 2 * PIVOT_NODE, newplist[PIVOT_NODE]);
  169. update_b = prob_diff_update_cost(newp, oldp[PIVOT_NODE]) +
  170. vp9_cost_upd256;
  171. savings = old_b - new_b - update_b;
  172. if (savings > bestsavings) {
  173. bestsavings = savings;
  174. bestnewp = newp;
  175. }
  176. }
  177. }
  178. *bestp = bestnewp;
  179. return bestsavings;
  180. }
  181. void vp9_cond_prob_diff_update(vp9_writer *w, vp9_prob *oldp,
  182. const unsigned int ct[2]) {
  183. const vp9_prob upd = DIFF_UPDATE_PROB;
  184. vp9_prob newp = get_binary_prob(ct[0], ct[1]);
  185. const int savings = vp9_prob_diff_update_savings_search(ct, *oldp, &newp,
  186. upd);
  187. assert(newp >= 1);
  188. if (savings > 0) {
  189. vp9_write(w, 1, upd);
  190. vp9_write_prob_diff_update(w, newp, *oldp);
  191. *oldp = newp;
  192. } else {
  193. vp9_write(w, 0, upd);
  194. }
  195. }