vp9_entropy.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #ifndef VP9_COMMON_VP9_ENTROPY_H_
  11. #define VP9_COMMON_VP9_ENTROPY_H_
  12. #include "vpx/vpx_integer.h"
  13. #include "vpx_dsp/prob.h"
  14. #include "vp9/common/vp9_common.h"
  15. #include "vp9/common/vp9_enums.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #define DIFF_UPDATE_PROB 252
  20. // Coefficient token alphabet
  21. #define ZERO_TOKEN 0 // 0 Extra Bits 0+0
  22. #define ONE_TOKEN 1 // 1 Extra Bits 0+1
  23. #define TWO_TOKEN 2 // 2 Extra Bits 0+1
  24. #define THREE_TOKEN 3 // 3 Extra Bits 0+1
  25. #define FOUR_TOKEN 4 // 4 Extra Bits 0+1
  26. #define CATEGORY1_TOKEN 5 // 5-6 Extra Bits 1+1
  27. #define CATEGORY2_TOKEN 6 // 7-10 Extra Bits 2+1
  28. #define CATEGORY3_TOKEN 7 // 11-18 Extra Bits 3+1
  29. #define CATEGORY4_TOKEN 8 // 19-34 Extra Bits 4+1
  30. #define CATEGORY5_TOKEN 9 // 35-66 Extra Bits 5+1
  31. #define CATEGORY6_TOKEN 10 // 67+ Extra Bits 14+1
  32. #define EOB_TOKEN 11 // EOB Extra Bits 0+0
  33. #define ENTROPY_TOKENS 12
  34. #define ENTROPY_NODES 11
  35. DECLARE_ALIGNED(16, extern const uint8_t, vp9_pt_energy_class[ENTROPY_TOKENS]);
  36. #define CAT1_MIN_VAL 5
  37. #define CAT2_MIN_VAL 7
  38. #define CAT3_MIN_VAL 11
  39. #define CAT4_MIN_VAL 19
  40. #define CAT5_MIN_VAL 35
  41. #define CAT6_MIN_VAL 67
  42. // Extra bit probabilities.
  43. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat1_prob[1]);
  44. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat2_prob[2]);
  45. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat3_prob[3]);
  46. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat4_prob[4]);
  47. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat5_prob[5]);
  48. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat6_prob[14]);
  49. #if CONFIG_VP9_HIGHBITDEPTH
  50. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat1_prob_high10[1]);
  51. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat2_prob_high10[2]);
  52. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat3_prob_high10[3]);
  53. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat4_prob_high10[4]);
  54. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat5_prob_high10[5]);
  55. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat6_prob_high10[16]);
  56. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat1_prob_high12[1]);
  57. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat2_prob_high12[2]);
  58. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat3_prob_high12[3]);
  59. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat4_prob_high12[4]);
  60. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat5_prob_high12[5]);
  61. DECLARE_ALIGNED(16, extern const uint8_t, vp9_cat6_prob_high12[18]);
  62. #endif // CONFIG_VP9_HIGHBITDEPTH
  63. #define EOB_MODEL_TOKEN 3
  64. #define DCT_MAX_VALUE 16384
  65. #if CONFIG_VP9_HIGHBITDEPTH
  66. #define DCT_MAX_VALUE_HIGH10 65536
  67. #define DCT_MAX_VALUE_HIGH12 262144
  68. #endif // CONFIG_VP9_HIGHBITDEPTH
  69. /* Coefficients are predicted via a 3-dimensional probability table. */
  70. #define REF_TYPES 2 // intra=0, inter=1
  71. /* Middle dimension reflects the coefficient position within the transform. */
  72. #define COEF_BANDS 6
  73. /* Inside dimension is measure of nearby complexity, that reflects the energy
  74. of nearby coefficients are nonzero. For the first coefficient (DC, unless
  75. block type is 0), we look at the (already encoded) blocks above and to the
  76. left of the current block. The context index is then the number (0,1,or 2)
  77. of these blocks having nonzero coefficients.
  78. After decoding a coefficient, the measure is determined by the size of the
  79. most recently decoded coefficient.
  80. Note that the intuitive meaning of this measure changes as coefficients
  81. are decoded, e.g., prior to the first token, a zero means that my neighbors
  82. are empty while, after the first token, because of the use of end-of-block,
  83. a zero means we just decoded a zero and hence guarantees that a non-zero
  84. coefficient will appear later in this block. However, this shift
  85. in meaning is perfectly OK because our context depends also on the
  86. coefficient band (and since zigzag positions 0, 1, and 2 are in
  87. distinct bands). */
  88. #define COEFF_CONTEXTS 6
  89. #define BAND_COEFF_CONTEXTS(band) ((band) == 0 ? 3 : COEFF_CONTEXTS)
  90. // #define ENTROPY_STATS
  91. typedef unsigned int vp9_coeff_count[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
  92. [ENTROPY_TOKENS];
  93. typedef unsigned int vp9_coeff_stats[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
  94. [ENTROPY_NODES][2];
  95. #define SUBEXP_PARAM 4 /* Subexponential code parameter */
  96. #define MODULUS_PARAM 13 /* Modulus parameter */
  97. struct VP9Common;
  98. void vp9_default_coef_probs(struct VP9Common *cm);
  99. void vp9_adapt_coef_probs(struct VP9Common *cm);
  100. // This is the index in the scan order beyond which all coefficients for
  101. // 8x8 transform and above are in the top band.
  102. // This macro is currently unused but may be used by certain implementations
  103. #define MAXBAND_INDEX 21
  104. DECLARE_ALIGNED(16, extern const uint8_t, vp9_coefband_trans_8x8plus[1024]);
  105. DECLARE_ALIGNED(16, extern const uint8_t, vp9_coefband_trans_4x4[16]);
  106. static INLINE const uint8_t *get_band_translate(TX_SIZE tx_size) {
  107. return tx_size == TX_4X4 ? vp9_coefband_trans_4x4
  108. : vp9_coefband_trans_8x8plus;
  109. }
  110. // 128 lists of probabilities are stored for the following ONE node probs:
  111. // 1, 3, 5, 7, ..., 253, 255
  112. // In between probabilities are interpolated linearly
  113. #define COEFF_PROB_MODELS 255
  114. #define UNCONSTRAINED_NODES 3
  115. #define PIVOT_NODE 2 // which node is pivot
  116. #define MODEL_NODES (ENTROPY_NODES - UNCONSTRAINED_NODES)
  117. extern const vpx_tree_index vp9_coef_con_tree[TREE_SIZE(ENTROPY_TOKENS)];
  118. extern const vpx_prob vp9_pareto8_full[COEFF_PROB_MODELS][MODEL_NODES];
  119. typedef vpx_prob vp9_coeff_probs_model[REF_TYPES][COEF_BANDS]
  120. [COEFF_CONTEXTS][UNCONSTRAINED_NODES];
  121. typedef unsigned int vp9_coeff_count_model[REF_TYPES][COEF_BANDS]
  122. [COEFF_CONTEXTS]
  123. [UNCONSTRAINED_NODES + 1];
  124. void vp9_model_to_full_probs(const vpx_prob *model, vpx_prob *full);
  125. typedef char ENTROPY_CONTEXT;
  126. static INLINE int combine_entropy_contexts(ENTROPY_CONTEXT a,
  127. ENTROPY_CONTEXT b) {
  128. return (a != 0) + (b != 0);
  129. }
  130. static INLINE int get_entropy_context(TX_SIZE tx_size, const ENTROPY_CONTEXT *a,
  131. const ENTROPY_CONTEXT *l) {
  132. ENTROPY_CONTEXT above_ec = 0, left_ec = 0;
  133. switch (tx_size) {
  134. case TX_4X4:
  135. above_ec = a[0] != 0;
  136. left_ec = l[0] != 0;
  137. break;
  138. case TX_8X8:
  139. above_ec = !!*(const uint16_t *)a;
  140. left_ec = !!*(const uint16_t *)l;
  141. break;
  142. case TX_16X16:
  143. above_ec = !!*(const uint32_t *)a;
  144. left_ec = !!*(const uint32_t *)l;
  145. break;
  146. case TX_32X32:
  147. above_ec = !!*(const uint64_t *)a;
  148. left_ec = !!*(const uint64_t *)l;
  149. break;
  150. default:
  151. assert(0 && "Invalid transform size.");
  152. break;
  153. }
  154. return combine_entropy_contexts(above_ec, left_ec);
  155. }
  156. #ifdef __cplusplus
  157. } // extern "C"
  158. #endif
  159. #endif // VP9_COMMON_VP9_ENTROPY_H_