2
0

detokenize.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "vp8/common/blockd.h"
  11. #include "onyxd_int.h"
  12. #include "vpx_mem/vpx_mem.h"
  13. #include "vpx_ports/mem.h"
  14. #include "detokenize.h"
  15. void vp8_reset_mb_tokens_context(MACROBLOCKD *x) {
  16. ENTROPY_CONTEXT *a_ctx = ((ENTROPY_CONTEXT *)x->above_context);
  17. ENTROPY_CONTEXT *l_ctx = ((ENTROPY_CONTEXT *)x->left_context);
  18. memset(a_ctx, 0, sizeof(ENTROPY_CONTEXT_PLANES) - 1);
  19. memset(l_ctx, 0, sizeof(ENTROPY_CONTEXT_PLANES) - 1);
  20. /* Clear entropy contexts for Y2 blocks */
  21. if (!x->mode_info_context->mbmi.is_4x4) {
  22. a_ctx[8] = l_ctx[8] = 0;
  23. }
  24. }
  25. /*
  26. ------------------------------------------------------------------------------
  27. Residual decoding (Paragraph 13.2 / 13.3)
  28. */
  29. static const uint8_t kBands[16 + 1] = {
  30. 0, 1, 2, 3, 6, 4, 5, 6, 6,
  31. 6, 6, 6, 6, 6, 6, 7, 0 /* extra entry as sentinel */
  32. };
  33. static const uint8_t kCat3[] = { 173, 148, 140, 0 };
  34. static const uint8_t kCat4[] = { 176, 155, 140, 135, 0 };
  35. static const uint8_t kCat5[] = { 180, 157, 141, 134, 130, 0 };
  36. static const uint8_t kCat6[] = { 254, 254, 243, 230, 196, 177,
  37. 153, 140, 133, 130, 129, 0 };
  38. static const uint8_t *const kCat3456[] = { kCat3, kCat4, kCat5, kCat6 };
  39. static const uint8_t kZigzag[16] = { 0, 1, 4, 8, 5, 2, 3, 6,
  40. 9, 12, 13, 10, 7, 11, 14, 15 };
  41. #define VP8GetBit vp8dx_decode_bool
  42. #define NUM_PROBAS 11
  43. #define NUM_CTX 3
  44. /* for const-casting */
  45. typedef const uint8_t (*ProbaArray)[NUM_CTX][NUM_PROBAS];
  46. static int GetSigned(BOOL_DECODER *br, int value_to_sign) {
  47. int split = (br->range + 1) >> 1;
  48. VP8_BD_VALUE bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8);
  49. int v;
  50. if (br->count < 0) vp8dx_bool_decoder_fill(br);
  51. if (br->value < bigsplit) {
  52. br->range = split;
  53. v = value_to_sign;
  54. } else {
  55. br->range = br->range - split;
  56. br->value = br->value - bigsplit;
  57. v = -value_to_sign;
  58. }
  59. br->range += br->range;
  60. br->value += br->value;
  61. br->count--;
  62. return v;
  63. }
  64. /*
  65. Returns the position of the last non-zero coeff plus one
  66. (and 0 if there's no coeff at all)
  67. */
  68. static int GetCoeffs(BOOL_DECODER *br, ProbaArray prob, int ctx, int n,
  69. int16_t *out) {
  70. const uint8_t *p = prob[n][ctx];
  71. if (!VP8GetBit(br, p[0])) { /* first EOB is more a 'CBP' bit. */
  72. return 0;
  73. }
  74. while (1) {
  75. ++n;
  76. if (!VP8GetBit(br, p[1])) {
  77. p = prob[kBands[n]][0];
  78. } else { /* non zero coeff */
  79. int v, j;
  80. if (!VP8GetBit(br, p[2])) {
  81. p = prob[kBands[n]][1];
  82. v = 1;
  83. } else {
  84. if (!VP8GetBit(br, p[3])) {
  85. if (!VP8GetBit(br, p[4])) {
  86. v = 2;
  87. } else {
  88. v = 3 + VP8GetBit(br, p[5]);
  89. }
  90. } else {
  91. if (!VP8GetBit(br, p[6])) {
  92. if (!VP8GetBit(br, p[7])) {
  93. v = 5 + VP8GetBit(br, 159);
  94. } else {
  95. v = 7 + 2 * VP8GetBit(br, 165);
  96. v += VP8GetBit(br, 145);
  97. }
  98. } else {
  99. const uint8_t *tab;
  100. const int bit1 = VP8GetBit(br, p[8]);
  101. const int bit0 = VP8GetBit(br, p[9 + bit1]);
  102. const int cat = 2 * bit1 + bit0;
  103. v = 0;
  104. for (tab = kCat3456[cat]; *tab; ++tab) {
  105. v += v + VP8GetBit(br, *tab);
  106. }
  107. v += 3 + (8 << cat);
  108. }
  109. }
  110. p = prob[kBands[n]][2];
  111. }
  112. j = kZigzag[n - 1];
  113. out[j] = GetSigned(br, v);
  114. if (n == 16 || !VP8GetBit(br, p[0])) { /* EOB */
  115. return n;
  116. }
  117. }
  118. if (n == 16) {
  119. return 16;
  120. }
  121. }
  122. }
  123. int vp8_decode_mb_tokens(VP8D_COMP *dx, MACROBLOCKD *x) {
  124. BOOL_DECODER *bc = x->current_bc;
  125. const FRAME_CONTEXT *const fc = &dx->common.fc;
  126. char *eobs = x->eobs;
  127. int i;
  128. int nonzeros;
  129. int eobtotal = 0;
  130. short *qcoeff_ptr;
  131. ProbaArray coef_probs;
  132. ENTROPY_CONTEXT *a_ctx = ((ENTROPY_CONTEXT *)x->above_context);
  133. ENTROPY_CONTEXT *l_ctx = ((ENTROPY_CONTEXT *)x->left_context);
  134. ENTROPY_CONTEXT *a;
  135. ENTROPY_CONTEXT *l;
  136. int skip_dc = 0;
  137. qcoeff_ptr = &x->qcoeff[0];
  138. if (!x->mode_info_context->mbmi.is_4x4) {
  139. a = a_ctx + 8;
  140. l = l_ctx + 8;
  141. coef_probs = fc->coef_probs[1];
  142. nonzeros = GetCoeffs(bc, coef_probs, (*a + *l), 0, qcoeff_ptr + 24 * 16);
  143. *a = *l = (nonzeros > 0);
  144. eobs[24] = nonzeros;
  145. eobtotal += nonzeros - 16;
  146. coef_probs = fc->coef_probs[0];
  147. skip_dc = 1;
  148. } else {
  149. coef_probs = fc->coef_probs[3];
  150. skip_dc = 0;
  151. }
  152. for (i = 0; i < 16; ++i) {
  153. a = a_ctx + (i & 3);
  154. l = l_ctx + ((i & 0xc) >> 2);
  155. nonzeros = GetCoeffs(bc, coef_probs, (*a + *l), skip_dc, qcoeff_ptr);
  156. *a = *l = (nonzeros > 0);
  157. nonzeros += skip_dc;
  158. eobs[i] = nonzeros;
  159. eobtotal += nonzeros;
  160. qcoeff_ptr += 16;
  161. }
  162. coef_probs = fc->coef_probs[2];
  163. a_ctx += 4;
  164. l_ctx += 4;
  165. for (i = 16; i < 24; ++i) {
  166. a = a_ctx + ((i > 19) << 1) + (i & 1);
  167. l = l_ctx + ((i > 19) << 1) + ((i & 3) > 1);
  168. nonzeros = GetCoeffs(bc, coef_probs, (*a + *l), 0, qcoeff_ptr);
  169. *a = *l = (nonzeros > 0);
  170. eobs[i] = nonzeros;
  171. eobtotal += nonzeros;
  172. qcoeff_ptr += 16;
  173. }
  174. return eobtotal;
  175. }