vp9_detokenize.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "vpx_mem/vpx_mem.h"
  11. #include "vpx_ports/mem.h"
  12. #include "vp9/common/vp9_blockd.h"
  13. #include "vp9/common/vp9_common.h"
  14. #include "vp9/common/vp9_entropy.h"
  15. #if CONFIG_COEFFICIENT_RANGE_CHECKING
  16. #include "vp9/common/vp9_idct.h"
  17. #endif
  18. #include "vp9/decoder/vp9_detokenize.h"
  19. #define EOB_CONTEXT_NODE 0
  20. #define ZERO_CONTEXT_NODE 1
  21. #define ONE_CONTEXT_NODE 2
  22. #define INCREMENT_COUNT(token) \
  23. do { \
  24. if (counts) \
  25. ++coef_counts[band][ctx][token]; \
  26. } while (0)
  27. static INLINE int read_coeff(const vpx_prob *probs, int n, vpx_reader *r) {
  28. int i, val = 0;
  29. for (i = 0; i < n; ++i)
  30. val = (val << 1) | vpx_read(r, probs[i]);
  31. return val;
  32. }
  33. static int decode_coefs(const MACROBLOCKD *xd,
  34. PLANE_TYPE type,
  35. tran_low_t *dqcoeff, TX_SIZE tx_size, const int16_t *dq,
  36. int ctx, const int16_t *scan, const int16_t *nb,
  37. vpx_reader *r) {
  38. FRAME_COUNTS *counts = xd->counts;
  39. const int max_eob = 16 << (tx_size << 1);
  40. const FRAME_CONTEXT *const fc = xd->fc;
  41. const int ref = is_inter_block(xd->mi[0]);
  42. int band, c = 0;
  43. const vpx_prob (*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
  44. fc->coef_probs[tx_size][type][ref];
  45. const vpx_prob *prob;
  46. unsigned int (*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1];
  47. unsigned int (*eob_branch_count)[COEFF_CONTEXTS];
  48. uint8_t token_cache[32 * 32];
  49. const uint8_t *band_translate = get_band_translate(tx_size);
  50. const int dq_shift = (tx_size == TX_32X32);
  51. int v, token;
  52. int16_t dqv = dq[0];
  53. const uint8_t *const cat6_prob =
  54. #if CONFIG_VP9_HIGHBITDEPTH
  55. (xd->bd == VPX_BITS_12) ? vp9_cat6_prob_high12 :
  56. (xd->bd == VPX_BITS_10) ? vp9_cat6_prob_high12 + 2 :
  57. #endif // CONFIG_VP9_HIGHBITDEPTH
  58. vp9_cat6_prob;
  59. const int cat6_bits =
  60. #if CONFIG_VP9_HIGHBITDEPTH
  61. (xd->bd == VPX_BITS_12) ? 18 :
  62. (xd->bd == VPX_BITS_10) ? 16 :
  63. #endif // CONFIG_VP9_HIGHBITDEPTH
  64. 14;
  65. if (counts) {
  66. coef_counts = counts->coef[tx_size][type][ref];
  67. eob_branch_count = counts->eob_branch[tx_size][type][ref];
  68. }
  69. while (c < max_eob) {
  70. int val = -1;
  71. band = *band_translate++;
  72. prob = coef_probs[band][ctx];
  73. if (counts)
  74. ++eob_branch_count[band][ctx];
  75. if (!vpx_read(r, prob[EOB_CONTEXT_NODE])) {
  76. INCREMENT_COUNT(EOB_MODEL_TOKEN);
  77. break;
  78. }
  79. while (!vpx_read(r, prob[ZERO_CONTEXT_NODE])) {
  80. INCREMENT_COUNT(ZERO_TOKEN);
  81. dqv = dq[1];
  82. token_cache[scan[c]] = 0;
  83. ++c;
  84. if (c >= max_eob)
  85. return c; // zero tokens at the end (no eob token)
  86. ctx = get_coef_context(nb, token_cache, c);
  87. band = *band_translate++;
  88. prob = coef_probs[band][ctx];
  89. }
  90. if (!vpx_read(r, prob[ONE_CONTEXT_NODE])) {
  91. INCREMENT_COUNT(ONE_TOKEN);
  92. token = ONE_TOKEN;
  93. val = 1;
  94. } else {
  95. INCREMENT_COUNT(TWO_TOKEN);
  96. token = vpx_read_tree(r, vp9_coef_con_tree,
  97. vp9_pareto8_full[prob[PIVOT_NODE] - 1]);
  98. switch (token) {
  99. case TWO_TOKEN:
  100. case THREE_TOKEN:
  101. case FOUR_TOKEN:
  102. val = token;
  103. break;
  104. case CATEGORY1_TOKEN:
  105. val = CAT1_MIN_VAL + read_coeff(vp9_cat1_prob, 1, r);
  106. break;
  107. case CATEGORY2_TOKEN:
  108. val = CAT2_MIN_VAL + read_coeff(vp9_cat2_prob, 2, r);
  109. break;
  110. case CATEGORY3_TOKEN:
  111. val = CAT3_MIN_VAL + read_coeff(vp9_cat3_prob, 3, r);
  112. break;
  113. case CATEGORY4_TOKEN:
  114. val = CAT4_MIN_VAL + read_coeff(vp9_cat4_prob, 4, r);
  115. break;
  116. case CATEGORY5_TOKEN:
  117. val = CAT5_MIN_VAL + read_coeff(vp9_cat5_prob, 5, r);
  118. break;
  119. case CATEGORY6_TOKEN:
  120. val = CAT6_MIN_VAL + read_coeff(cat6_prob, cat6_bits, r);
  121. break;
  122. }
  123. }
  124. v = (val * dqv) >> dq_shift;
  125. #if CONFIG_COEFFICIENT_RANGE_CHECKING
  126. #if CONFIG_VP9_HIGHBITDEPTH
  127. dqcoeff[scan[c]] = highbd_check_range((vpx_read_bit(r) ? -v : v),
  128. xd->bd);
  129. #else
  130. dqcoeff[scan[c]] = check_range(vpx_read_bit(r) ? -v : v);
  131. #endif // CONFIG_VP9_HIGHBITDEPTH
  132. #else
  133. dqcoeff[scan[c]] = vpx_read_bit(r) ? -v : v;
  134. #endif // CONFIG_COEFFICIENT_RANGE_CHECKING
  135. token_cache[scan[c]] = vp9_pt_energy_class[token];
  136. ++c;
  137. ctx = get_coef_context(nb, token_cache, c);
  138. dqv = dq[1];
  139. }
  140. return c;
  141. }
  142. static void get_ctx_shift(MACROBLOCKD *xd, int *ctx_shift_a, int *ctx_shift_l,
  143. int x, int y, unsigned int tx_size_in_blocks) {
  144. if (xd->max_blocks_wide) {
  145. if (tx_size_in_blocks + x > xd->max_blocks_wide)
  146. *ctx_shift_a = (tx_size_in_blocks - (xd->max_blocks_wide - x)) * 8;
  147. }
  148. if (xd->max_blocks_high) {
  149. if (tx_size_in_blocks + y > xd->max_blocks_high)
  150. *ctx_shift_l = (tx_size_in_blocks - (xd->max_blocks_high - y)) * 8;
  151. }
  152. }
  153. int vp9_decode_block_tokens(MACROBLOCKD *xd, int plane, const scan_order *sc,
  154. int x, int y, TX_SIZE tx_size, vpx_reader *r,
  155. int seg_id) {
  156. struct macroblockd_plane *const pd = &xd->plane[plane];
  157. const int16_t *const dequant = pd->seg_dequant[seg_id];
  158. int eob;
  159. ENTROPY_CONTEXT *a = pd->above_context + x;
  160. ENTROPY_CONTEXT *l = pd->left_context + y;
  161. int ctx;
  162. int ctx_shift_a = 0;
  163. int ctx_shift_l = 0;
  164. switch (tx_size) {
  165. case TX_4X4:
  166. ctx = a[0] != 0;
  167. ctx += l[0] != 0;
  168. eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
  169. dequant, ctx, sc->scan, sc->neighbors, r);
  170. a[0] = l[0] = (eob > 0);
  171. break;
  172. case TX_8X8:
  173. get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_8X8);
  174. ctx = !!*(const uint16_t *)a;
  175. ctx += !!*(const uint16_t *)l;
  176. eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
  177. dequant, ctx, sc->scan, sc->neighbors, r);
  178. *(uint16_t *)a = ((eob > 0) * 0x0101) >> ctx_shift_a;
  179. *(uint16_t *)l = ((eob > 0) * 0x0101) >> ctx_shift_l;
  180. break;
  181. case TX_16X16:
  182. get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_16X16);
  183. ctx = !!*(const uint32_t *)a;
  184. ctx += !!*(const uint32_t *)l;
  185. eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
  186. dequant, ctx, sc->scan, sc->neighbors, r);
  187. *(uint32_t *)a = ((eob > 0) * 0x01010101) >> ctx_shift_a;
  188. *(uint32_t *)l = ((eob > 0) * 0x01010101) >> ctx_shift_l;
  189. break;
  190. case TX_32X32:
  191. get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_32X32);
  192. // NOTE: casting to uint64_t here is safe because the default memory
  193. // alignment is at least 8 bytes and the TX_32X32 is aligned on 8 byte
  194. // boundaries.
  195. ctx = !!*(const uint64_t *)a;
  196. ctx += !!*(const uint64_t *)l;
  197. eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
  198. dequant, ctx, sc->scan, sc->neighbors, r);
  199. *(uint64_t *)a = ((eob > 0) * 0x0101010101010101ULL) >> ctx_shift_a;
  200. *(uint64_t *)l = ((eob > 0) * 0x0101010101010101ULL) >> ctx_shift_l;
  201. break;
  202. default:
  203. assert(0 && "Invalid transform size.");
  204. eob = 0;
  205. break;
  206. }
  207. return eob;
  208. }