vp9_subexp.c 7.4 KB

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