vp9_detokenize.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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) ++coef_counts[band][ctx][token]; \
  25. } while (0)
  26. static INLINE int read_bool(vpx_reader *r, int prob, BD_VALUE *value,
  27. int *count, unsigned int *range) {
  28. const unsigned int split = (*range * prob + (256 - prob)) >> CHAR_BIT;
  29. const BD_VALUE bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
  30. #if CONFIG_BITSTREAM_DEBUG
  31. const int queue_r = bitstream_queue_get_read();
  32. const int frame_idx = bitstream_queue_get_frame_read();
  33. int ref_result, ref_prob;
  34. bitstream_queue_pop(&ref_result, &ref_prob);
  35. if (prob != ref_prob) {
  36. fprintf(stderr,
  37. "\n *** [bit] prob error, frame_idx_r %d prob %d ref_prob %d "
  38. "queue_r %d\n",
  39. frame_idx, prob, ref_prob, queue_r);
  40. assert(0);
  41. }
  42. #endif
  43. if (*count < 0) {
  44. r->value = *value;
  45. r->count = *count;
  46. vpx_reader_fill(r);
  47. *value = r->value;
  48. *count = r->count;
  49. }
  50. if (*value >= bigsplit) {
  51. *range = *range - split;
  52. *value = *value - bigsplit;
  53. {
  54. const int shift = vpx_norm[*range];
  55. *range <<= shift;
  56. *value <<= shift;
  57. *count -= shift;
  58. }
  59. #if CONFIG_BITSTREAM_DEBUG
  60. {
  61. const int bit = 1;
  62. if (bit != ref_result) {
  63. fprintf(
  64. stderr,
  65. "\n *** [bit] result error, frame_idx_r %d bit %d ref_result %d "
  66. "queue_r %d\n",
  67. frame_idx, bit, ref_result, queue_r);
  68. assert(0);
  69. }
  70. }
  71. #endif
  72. return 1;
  73. }
  74. *range = split;
  75. {
  76. const int shift = vpx_norm[*range];
  77. *range <<= shift;
  78. *value <<= shift;
  79. *count -= shift;
  80. }
  81. #if CONFIG_BITSTREAM_DEBUG
  82. {
  83. const int bit = 0;
  84. if (bit != ref_result) {
  85. fprintf(stderr,
  86. "\n *** [bit] result error, frame_idx_r %d bit %d ref_result %d "
  87. "queue_r %d\n",
  88. frame_idx, bit, ref_result, queue_r);
  89. assert(0);
  90. }
  91. }
  92. #endif
  93. return 0;
  94. }
  95. static INLINE int read_coeff(vpx_reader *r, const vpx_prob *probs, int n,
  96. BD_VALUE *value, int *count, unsigned int *range) {
  97. int i, val = 0;
  98. for (i = 0; i < n; ++i)
  99. val = (val << 1) | read_bool(r, probs[i], value, count, range);
  100. return val;
  101. }
  102. static int decode_coefs(const MACROBLOCKD *xd, PLANE_TYPE type,
  103. tran_low_t *dqcoeff, TX_SIZE tx_size, const int16_t *dq,
  104. int ctx, const int16_t *scan, const int16_t *nb,
  105. vpx_reader *r) {
  106. FRAME_COUNTS *counts = xd->counts;
  107. const int max_eob = 16 << (tx_size << 1);
  108. const FRAME_CONTEXT *const fc = xd->fc;
  109. const int ref = is_inter_block(xd->mi[0]);
  110. int band, c = 0;
  111. const vpx_prob(*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
  112. fc->coef_probs[tx_size][type][ref];
  113. const vpx_prob *prob;
  114. unsigned int(*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1];
  115. unsigned int(*eob_branch_count)[COEFF_CONTEXTS];
  116. uint8_t token_cache[32 * 32];
  117. const uint8_t *band_translate = get_band_translate(tx_size);
  118. const int dq_shift = (tx_size == TX_32X32);
  119. int v;
  120. int16_t dqv = dq[0];
  121. const uint8_t *const cat6_prob =
  122. #if CONFIG_VP9_HIGHBITDEPTH
  123. (xd->bd == VPX_BITS_12)
  124. ? vp9_cat6_prob_high12
  125. : (xd->bd == VPX_BITS_10) ? vp9_cat6_prob_high12 + 2 :
  126. #endif // CONFIG_VP9_HIGHBITDEPTH
  127. vp9_cat6_prob;
  128. const int cat6_bits =
  129. #if CONFIG_VP9_HIGHBITDEPTH
  130. (xd->bd == VPX_BITS_12) ? 18
  131. : (xd->bd == VPX_BITS_10) ? 16 :
  132. #endif // CONFIG_VP9_HIGHBITDEPTH
  133. 14;
  134. // Keep value, range, and count as locals. The compiler produces better
  135. // results with the locals than using r directly.
  136. BD_VALUE value = r->value;
  137. unsigned int range = r->range;
  138. int count = r->count;
  139. if (counts) {
  140. coef_counts = counts->coef[tx_size][type][ref];
  141. eob_branch_count = counts->eob_branch[tx_size][type][ref];
  142. }
  143. while (c < max_eob) {
  144. int val = -1;
  145. band = *band_translate++;
  146. prob = coef_probs[band][ctx];
  147. if (counts) ++eob_branch_count[band][ctx];
  148. if (!read_bool(r, prob[EOB_CONTEXT_NODE], &value, &count, &range)) {
  149. INCREMENT_COUNT(EOB_MODEL_TOKEN);
  150. break;
  151. }
  152. while (!read_bool(r, prob[ZERO_CONTEXT_NODE], &value, &count, &range)) {
  153. INCREMENT_COUNT(ZERO_TOKEN);
  154. dqv = dq[1];
  155. token_cache[scan[c]] = 0;
  156. ++c;
  157. if (c >= max_eob) {
  158. r->value = value;
  159. r->range = range;
  160. r->count = count;
  161. return c; // zero tokens at the end (no eob token)
  162. }
  163. ctx = get_coef_context(nb, token_cache, c);
  164. band = *band_translate++;
  165. prob = coef_probs[band][ctx];
  166. }
  167. if (read_bool(r, prob[ONE_CONTEXT_NODE], &value, &count, &range)) {
  168. const vpx_prob *p = vp9_pareto8_full[prob[PIVOT_NODE] - 1];
  169. INCREMENT_COUNT(TWO_TOKEN);
  170. if (read_bool(r, p[0], &value, &count, &range)) {
  171. if (read_bool(r, p[3], &value, &count, &range)) {
  172. token_cache[scan[c]] = 5;
  173. if (read_bool(r, p[5], &value, &count, &range)) {
  174. if (read_bool(r, p[7], &value, &count, &range)) {
  175. val = CAT6_MIN_VAL +
  176. read_coeff(r, cat6_prob, cat6_bits, &value, &count, &range);
  177. } else {
  178. val = CAT5_MIN_VAL +
  179. read_coeff(r, vp9_cat5_prob, 5, &value, &count, &range);
  180. }
  181. } else if (read_bool(r, p[6], &value, &count, &range)) {
  182. val = CAT4_MIN_VAL +
  183. read_coeff(r, vp9_cat4_prob, 4, &value, &count, &range);
  184. } else {
  185. val = CAT3_MIN_VAL +
  186. read_coeff(r, vp9_cat3_prob, 3, &value, &count, &range);
  187. }
  188. } else {
  189. token_cache[scan[c]] = 4;
  190. if (read_bool(r, p[4], &value, &count, &range)) {
  191. val = CAT2_MIN_VAL +
  192. read_coeff(r, vp9_cat2_prob, 2, &value, &count, &range);
  193. } else {
  194. val = CAT1_MIN_VAL +
  195. read_coeff(r, vp9_cat1_prob, 1, &value, &count, &range);
  196. }
  197. }
  198. #if CONFIG_VP9_HIGHBITDEPTH
  199. // val may use 18-bits
  200. v = (int)(((int64_t)val * dqv) >> dq_shift);
  201. #else
  202. v = (val * dqv) >> dq_shift;
  203. #endif
  204. } else {
  205. if (read_bool(r, p[1], &value, &count, &range)) {
  206. token_cache[scan[c]] = 3;
  207. v = ((3 + read_bool(r, p[2], &value, &count, &range)) * dqv) >>
  208. dq_shift;
  209. } else {
  210. token_cache[scan[c]] = 2;
  211. v = (2 * dqv) >> dq_shift;
  212. }
  213. }
  214. } else {
  215. INCREMENT_COUNT(ONE_TOKEN);
  216. token_cache[scan[c]] = 1;
  217. v = dqv >> dq_shift;
  218. }
  219. #if CONFIG_COEFFICIENT_RANGE_CHECKING
  220. #if CONFIG_VP9_HIGHBITDEPTH
  221. dqcoeff[scan[c]] = highbd_check_range(
  222. read_bool(r, 128, &value, &count, &range) ? -v : v, xd->bd);
  223. #else
  224. dqcoeff[scan[c]] =
  225. check_range(read_bool(r, 128, &value, &count, &range) ? -v : v);
  226. #endif // CONFIG_VP9_HIGHBITDEPTH
  227. #else
  228. if (read_bool(r, 128, &value, &count, &range)) {
  229. dqcoeff[scan[c]] = -v;
  230. } else {
  231. dqcoeff[scan[c]] = v;
  232. }
  233. #endif // CONFIG_COEFFICIENT_RANGE_CHECKING
  234. ++c;
  235. ctx = get_coef_context(nb, token_cache, c);
  236. dqv = dq[1];
  237. }
  238. r->value = value;
  239. r->range = range;
  240. r->count = count;
  241. return c;
  242. }
  243. static void get_ctx_shift(MACROBLOCKD *xd, int *ctx_shift_a, int *ctx_shift_l,
  244. int x, int y, unsigned int tx_size_in_blocks) {
  245. if (xd->max_blocks_wide) {
  246. if (tx_size_in_blocks + x > xd->max_blocks_wide)
  247. *ctx_shift_a = (tx_size_in_blocks - (xd->max_blocks_wide - x)) * 8;
  248. }
  249. if (xd->max_blocks_high) {
  250. if (tx_size_in_blocks + y > xd->max_blocks_high)
  251. *ctx_shift_l = (tx_size_in_blocks - (xd->max_blocks_high - y)) * 8;
  252. }
  253. }
  254. int vp9_decode_block_tokens(TileWorkerData *twd, int plane,
  255. const scan_order *sc, int x, int y, TX_SIZE tx_size,
  256. int seg_id) {
  257. vpx_reader *r = &twd->bit_reader;
  258. MACROBLOCKD *xd = &twd->xd;
  259. struct macroblockd_plane *const pd = &xd->plane[plane];
  260. const int16_t *const dequant = pd->seg_dequant[seg_id];
  261. int eob;
  262. ENTROPY_CONTEXT *a = pd->above_context + x;
  263. ENTROPY_CONTEXT *l = pd->left_context + y;
  264. int ctx;
  265. int ctx_shift_a = 0;
  266. int ctx_shift_l = 0;
  267. switch (tx_size) {
  268. case TX_4X4:
  269. ctx = a[0] != 0;
  270. ctx += l[0] != 0;
  271. eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
  272. dequant, ctx, sc->scan, sc->neighbors, r);
  273. a[0] = l[0] = (eob > 0);
  274. break;
  275. case TX_8X8:
  276. get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_8X8);
  277. ctx = !!*(const uint16_t *)a;
  278. ctx += !!*(const uint16_t *)l;
  279. eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
  280. dequant, ctx, sc->scan, sc->neighbors, r);
  281. *(uint16_t *)a = ((eob > 0) * 0x0101) >> ctx_shift_a;
  282. *(uint16_t *)l = ((eob > 0) * 0x0101) >> ctx_shift_l;
  283. break;
  284. case TX_16X16:
  285. get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_16X16);
  286. ctx = !!*(const uint32_t *)a;
  287. ctx += !!*(const uint32_t *)l;
  288. eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
  289. dequant, ctx, sc->scan, sc->neighbors, r);
  290. *(uint32_t *)a = ((eob > 0) * 0x01010101) >> ctx_shift_a;
  291. *(uint32_t *)l = ((eob > 0) * 0x01010101) >> ctx_shift_l;
  292. break;
  293. case TX_32X32:
  294. get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_32X32);
  295. // NOTE: casting to uint64_t here is safe because the default memory
  296. // alignment is at least 8 bytes and the TX_32X32 is aligned on 8 byte
  297. // boundaries.
  298. ctx = !!*(const uint64_t *)a;
  299. ctx += !!*(const uint64_t *)l;
  300. eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
  301. dequant, ctx, sc->scan, sc->neighbors, r);
  302. *(uint64_t *)a = ((eob > 0) * 0x0101010101010101ULL) >> ctx_shift_a;
  303. *(uint64_t *)l = ((eob > 0) * 0x0101010101010101ULL) >> ctx_shift_l;
  304. break;
  305. default:
  306. assert(0 && "Invalid transform size.");
  307. eob = 0;
  308. break;
  309. }
  310. return eob;
  311. }