context.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Copyright 2013 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Functions to map previous bytes into a context id. */
  6. #ifndef BROTLI_ENC_CONTEXT_H_
  7. #define BROTLI_ENC_CONTEXT_H_
  8. #include "../common/types.h"
  9. #include "../common/port.h"
  10. #if defined(__cplusplus) || defined(c_plusplus)
  11. extern "C" {
  12. #endif
  13. /* Second-order context lookup table for UTF8 byte streams.
  14. If p1 and p2 are the previous two bytes, we calculate the context as
  15. context = kUTF8ContextLookup[p1] | kUTF8ContextLookup[p2 + 256].
  16. If the previous two bytes are ASCII characters (i.e. < 128), this will be
  17. equivalent to
  18. context = 4 * context1(p1) + context2(p2),
  19. where context1 is based on the previous byte in the following way:
  20. 0 : non-ASCII control
  21. 1 : \t, \n, \r
  22. 2 : space
  23. 3 : other punctuation
  24. 4 : " '
  25. 5 : %
  26. 6 : ( < [ {
  27. 7 : ) > ] }
  28. 8 : , ; :
  29. 9 : .
  30. 10 : =
  31. 11 : number
  32. 12 : upper-case vowel
  33. 13 : upper-case consonant
  34. 14 : lower-case vowel
  35. 15 : lower-case consonant
  36. and context2 is based on the second last byte:
  37. 0 : control, space
  38. 1 : punctuation
  39. 2 : upper-case letter, number
  40. 3 : lower-case letter
  41. If the last byte is ASCII, and the second last byte is not (in a valid UTF8
  42. stream it will be a continuation byte, value between 128 and 191), the
  43. context is the same as if the second last byte was an ASCII control or space.
  44. If the last byte is a UTF8 lead byte (value >= 192), then the next byte will
  45. be a continuation byte and the context id is 2 or 3 depending on the LSB of
  46. the last byte and to a lesser extent on the second last byte if it is ASCII.
  47. If the last byte is a UTF8 continuation byte, the second last byte can be:
  48. - continuation byte: the next byte is probably ASCII or lead byte (assuming
  49. 4-byte UTF8 characters are rare) and the context id is 0 or 1.
  50. - lead byte (192 - 207): next byte is ASCII or lead byte, context is 0 or 1
  51. - lead byte (208 - 255): next byte is continuation byte, context is 2 or 3
  52. The possible value combinations of the previous two bytes, the range of
  53. context ids and the type of the next byte is summarized in the table below:
  54. |--------\-----------------------------------------------------------------|
  55. | \ Last byte |
  56. | Second \---------------------------------------------------------------|
  57. | last byte \ ASCII | cont. byte | lead byte |
  58. | \ (0-127) | (128-191) | (192-) |
  59. |=============|===================|=====================|==================|
  60. | ASCII | next: ASCII/lead | not valid | next: cont. |
  61. | (0-127) | context: 4 - 63 | | context: 2 - 3 |
  62. |-------------|-------------------|---------------------|------------------|
  63. | cont. byte | next: ASCII/lead | next: ASCII/lead | next: cont. |
  64. | (128-191) | context: 4 - 63 | context: 0 - 1 | context: 2 - 3 |
  65. |-------------|-------------------|---------------------|------------------|
  66. | lead byte | not valid | next: ASCII/lead | not valid |
  67. | (192-207) | | context: 0 - 1 | |
  68. |-------------|-------------------|---------------------|------------------|
  69. | lead byte | not valid | next: cont. | not valid |
  70. | (208-) | | context: 2 - 3 | |
  71. |-------------|-------------------|---------------------|------------------|
  72. */
  73. static const uint8_t kUTF8ContextLookup[512] = {
  74. /* Last byte. */
  75. /* */
  76. /* ASCII range. */
  77. 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 4, 0, 0,
  78. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  79. 8, 12, 16, 12, 12, 20, 12, 16, 24, 28, 12, 12, 32, 12, 36, 12,
  80. 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 32, 32, 24, 40, 28, 12,
  81. 12, 48, 52, 52, 52, 48, 52, 52, 52, 48, 52, 52, 52, 52, 52, 48,
  82. 52, 52, 52, 52, 52, 48, 52, 52, 52, 52, 52, 24, 12, 28, 12, 12,
  83. 12, 56, 60, 60, 60, 56, 60, 60, 60, 56, 60, 60, 60, 60, 60, 56,
  84. 60, 60, 60, 60, 60, 56, 60, 60, 60, 60, 60, 24, 12, 28, 12, 0,
  85. /* UTF8 continuation byte range. */
  86. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  87. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  88. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  89. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  90. /* UTF8 lead byte range. */
  91. 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  92. 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  93. 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  94. 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  95. /* Second last byte. */
  96. /* */
  97. /* ASCII range. */
  98. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  99. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  100. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  101. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1,
  102. 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  103. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
  104. 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  105. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 0,
  106. /* UTF8 continuation byte range. */
  107. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  108. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  109. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  110. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  111. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  112. /* UTF8 lead byte range. */
  113. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  114. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  115. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  116. };
  117. /* Context lookup table for small signed integers. */
  118. static const uint8_t kSigned3BitContextLookup[] = {
  119. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  120. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  121. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  122. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  123. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  124. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  125. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  126. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  127. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  128. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  129. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  130. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  131. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  132. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  133. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  134. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7,
  135. };
  136. typedef enum ContextType {
  137. CONTEXT_LSB6 = 0,
  138. CONTEXT_MSB6 = 1,
  139. CONTEXT_UTF8 = 2,
  140. CONTEXT_SIGNED = 3
  141. } ContextType;
  142. static BROTLI_INLINE uint8_t Context(uint8_t p1, uint8_t p2, ContextType mode) {
  143. switch (mode) {
  144. case CONTEXT_LSB6:
  145. return p1 & 0x3f;
  146. case CONTEXT_MSB6:
  147. return (uint8_t)(p1 >> 2);
  148. case CONTEXT_UTF8:
  149. return kUTF8ContextLookup[p1] | kUTF8ContextLookup[p2 + 256];
  150. case CONTEXT_SIGNED:
  151. return (uint8_t)((kSigned3BitContextLookup[p1] << 3) +
  152. kSigned3BitContextLookup[p2]);
  153. default:
  154. return 0;
  155. }
  156. }
  157. #if defined(__cplusplus) || defined(c_plusplus)
  158. } /* extern "C" */
  159. #endif
  160. #endif /* BROTLI_ENC_CONTEXT_H_ */