2
0

vp9_subexp.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. i = map_table[i];
  70. return i;
  71. }
  72. static int prob_diff_update_cost(vpx_prob newp, vpx_prob oldp) {
  73. int delp = remap_prob(newp, oldp);
  74. return update_bits[delp] << VP9_PROB_COST_SHIFT;
  75. }
  76. static void encode_uniform(vpx_writer *w, int v) {
  77. const int l = 8;
  78. const int m = (1 << l) - 191;
  79. if (v < m) {
  80. vpx_write_literal(w, v, l - 1);
  81. } else {
  82. vpx_write_literal(w, m + ((v - m) >> 1), l - 1);
  83. vpx_write_literal(w, (v - m) & 1, 1);
  84. }
  85. }
  86. static INLINE int write_bit_gte(vpx_writer *w, int word, int test) {
  87. vpx_write_literal(w, word >= test, 1);
  88. return word >= test;
  89. }
  90. static void encode_term_subexp(vpx_writer *w, int word) {
  91. if (!write_bit_gte(w, word, 16)) {
  92. vpx_write_literal(w, word, 4);
  93. } else if (!write_bit_gte(w, word, 32)) {
  94. vpx_write_literal(w, word - 16, 4);
  95. } else if (!write_bit_gte(w, word, 64)) {
  96. vpx_write_literal(w, word - 32, 5);
  97. } else {
  98. encode_uniform(w, word - 64);
  99. }
  100. }
  101. void vp9_write_prob_diff_update(vpx_writer *w, vpx_prob newp, vpx_prob oldp) {
  102. const int delp = remap_prob(newp, oldp);
  103. encode_term_subexp(w, delp);
  104. }
  105. int vp9_prob_diff_update_savings_search(const unsigned int *ct, vpx_prob oldp,
  106. vpx_prob *bestp, vpx_prob upd) {
  107. const int old_b = cost_branch256(ct, oldp);
  108. int bestsavings = 0;
  109. vpx_prob newp, bestnewp = oldp;
  110. const int step = *bestp > oldp ? -1 : 1;
  111. const int upd_cost = vp9_cost_one(upd) - vp9_cost_zero(upd);
  112. if (old_b > upd_cost + (MIN_DELP_BITS << VP9_PROB_COST_SHIFT)) {
  113. for (newp = *bestp; newp != oldp; newp += step) {
  114. const int new_b = cost_branch256(ct, newp);
  115. const int update_b = prob_diff_update_cost(newp, oldp) + upd_cost;
  116. const int savings = old_b - new_b - update_b;
  117. if (savings > bestsavings) {
  118. bestsavings = savings;
  119. bestnewp = newp;
  120. }
  121. }
  122. }
  123. *bestp = bestnewp;
  124. return bestsavings;
  125. }
  126. int vp9_prob_diff_update_savings_search_model(const unsigned int *ct,
  127. const vpx_prob oldp,
  128. vpx_prob *bestp, vpx_prob upd,
  129. int stepsize) {
  130. int i, old_b, new_b, update_b, savings, bestsavings;
  131. int newp;
  132. const int step_sign = *bestp > oldp ? -1 : 1;
  133. const int step = stepsize * step_sign;
  134. const int upd_cost = vp9_cost_one(upd) - vp9_cost_zero(upd);
  135. const vpx_prob *newplist, *oldplist;
  136. vpx_prob bestnewp;
  137. oldplist = vp9_pareto8_full[oldp - 1];
  138. old_b = cost_branch256(ct + 2 * PIVOT_NODE, oldp);
  139. for (i = UNCONSTRAINED_NODES; i < ENTROPY_NODES; ++i)
  140. old_b += cost_branch256(ct + 2 * i, oldplist[i - UNCONSTRAINED_NODES]);
  141. bestsavings = 0;
  142. bestnewp = oldp;
  143. assert(stepsize > 0);
  144. if (old_b > upd_cost + (MIN_DELP_BITS << VP9_PROB_COST_SHIFT)) {
  145. for (newp = *bestp; (newp - oldp) * step_sign < 0; newp += step) {
  146. if (newp < 1 || newp > 255) continue;
  147. newplist = vp9_pareto8_full[newp - 1];
  148. new_b = cost_branch256(ct + 2 * PIVOT_NODE, newp);
  149. for (i = UNCONSTRAINED_NODES; i < ENTROPY_NODES; ++i)
  150. new_b += cost_branch256(ct + 2 * i, newplist[i - UNCONSTRAINED_NODES]);
  151. update_b = prob_diff_update_cost(newp, oldp) + upd_cost;
  152. savings = old_b - new_b - update_b;
  153. if (savings > bestsavings) {
  154. bestsavings = savings;
  155. bestnewp = newp;
  156. }
  157. }
  158. }
  159. *bestp = bestnewp;
  160. return bestsavings;
  161. }
  162. void vp9_cond_prob_diff_update(vpx_writer *w, vpx_prob *oldp,
  163. const unsigned int ct[2]) {
  164. const vpx_prob upd = DIFF_UPDATE_PROB;
  165. vpx_prob newp = get_binary_prob(ct[0], ct[1]);
  166. const int savings =
  167. vp9_prob_diff_update_savings_search(ct, *oldp, &newp, upd);
  168. assert(newp >= 1);
  169. if (savings > 0) {
  170. vpx_write(w, 1, upd);
  171. vp9_write_prob_diff_update(w, newp, *oldp);
  172. *oldp = newp;
  173. } else {
  174. vpx_write(w, 0, upd);
  175. }
  176. }