rangedec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*++
  2. Copyright (c) Alex Ionescu. All rights reserved.
  3. Module Name:
  4. rangedec.c
  5. Abstract:
  6. This module implements the Range Decoder, which is how LZMA describes the
  7. arithmetic coder that it uses to represent the binary representation of the
  8. LZ77 match length-distance pairs after the initial compression pass. At the
  9. implementation level, this coder works with an alphabet of only 2 symbols:
  10. the bit "0", and the bit "1", so there are only ever two probability ranges
  11. that need to be checked each pass. In LZMA, a probability of 100% encodes a
  12. "0", while 0% encodes a "1". Initially, all probabilities are assumed to be
  13. 50%. Probabilities are stored using 11-bits (2048 \=\= 100%), and thus use 16
  14. bits of storage. Finally, the range decoder is adaptive, meaning that each
  15. time a bit is decoded, the probabilities are updated: each 0 increases the
  16. probability of another 0, and each 1 decrases it. The algorithm adapts the
  17. probabilities using an exponential moving average with a shift ratio of 5.
  18. Author:
  19. Alex Ionescu (@aionescu) 15-Apr-2020 - Initial version
  20. Environment:
  21. Windows & Linux, user mode and kernel mode.
  22. --*/
  23. #include "minlzlib.h"
  24. //
  25. // The range decoder uses 11 probability bits, where 2048 is 100% chance of a 0
  26. //
  27. #define LZMA_RC_PROBABILITY_BITS 11
  28. #define LZMA_RC_MAX_PROBABILITY (1 << LZMA_RC_PROBABILITY_BITS)
  29. const uint16_t k_LzmaRcHalfProbability = LZMA_RC_MAX_PROBABILITY / 2;
  30. //
  31. // The range decoder uses an exponential moving average of the last probability
  32. // hit (match or miss) with an adaptation rate of 5 bits (which falls in the
  33. // middle of its 11 bits used to encode a probability.
  34. //
  35. #define LZMA_RC_ADAPTATION_RATE_SHIFT 5
  36. //
  37. // The range decoder has enough precision for the range only as long as the top
  38. // 8 bits are still set. Once it falls below, it needs a renormalization step.
  39. //
  40. #define LZMA_RC_MIN_RANGE (1 << 24)
  41. //
  42. // The range decoder must be initialized with 5 bytes, the first of which is
  43. // ignored
  44. //
  45. #define LZMA_RC_INIT_BYTES 5
  46. //
  47. // State used for the binary adaptive arithmetic coder (LZMA Range Decoder)
  48. //
  49. typedef struct _RANGE_DECODER_STATE
  50. {
  51. //
  52. // Start and end location of the current stream's range encoder buffer
  53. //
  54. const uint8_t* Start;
  55. const uint8_t* Limit;
  56. //
  57. // Current probability range and 32-bit arithmetic encoded sequence code
  58. //
  59. uint32_t Range;
  60. uint32_t Code;
  61. } RANGE_DECODER_STATE, *PRANGE_DECODER_STATE;
  62. RANGE_DECODER_STATE RcState;
  63. bool
  64. RcInitialize (
  65. uint16_t* ChunkSize
  66. )
  67. {
  68. uint8_t i, rcByte;
  69. const uint8_t* chunkEnd;
  70. //
  71. // Make sure that the input buffer has enough space for the requirements of
  72. // the range encoder. We (temporarily) seek forward to validate this.
  73. //
  74. if (!BfSeek(*ChunkSize, &chunkEnd))
  75. {
  76. return false;
  77. }
  78. BfSeek(-*ChunkSize, &chunkEnd);
  79. //
  80. // The initial probability range is set to its highest value, after which
  81. // the next 5 bytes are used to initialize the initial code. Note that the
  82. // first byte outputted by the encoder is always going to be zero, so it is
  83. // ignored here.
  84. //
  85. RcState.Range = (uint32_t)-1;
  86. RcState.Code = 0;
  87. for (i = 0; i < LZMA_RC_INIT_BYTES; i++)
  88. {
  89. BfRead(&rcByte);
  90. RcState.Code = (RcState.Code << 8) | rcByte;
  91. }
  92. //
  93. // Store our current location in the buffer now, and how far we can go on
  94. // reading. Then decrease the total chunk size by the count of init bytes,
  95. // so that the caller can check, once done (RcIsComplete), if the code has
  96. // become 0 exactly when the compressed chunk size has been fully consumed
  97. // by the decoder.
  98. //
  99. BfSeek(0, &RcState.Start);
  100. RcState.Limit = RcState.Start + *ChunkSize;
  101. *ChunkSize -= LZMA_RC_INIT_BYTES;
  102. return true;
  103. }
  104. bool
  105. RcCanRead (
  106. void
  107. )
  108. {
  109. const uint8_t* pos;
  110. //
  111. // We can keep reading symbols as long as we haven't reached the end of the
  112. // input buffer yet.
  113. //
  114. BfSeek(0, &pos);
  115. return pos <= RcState.Limit;
  116. }
  117. bool
  118. RcIsComplete (
  119. uint32_t* BytesProcessed
  120. )
  121. {
  122. const uint8_t* pos;
  123. //
  124. // When the last symbol has been decoded, the last code should be zero as
  125. // there is nothing left to describe. Return the offset in the buffer where
  126. // this occurred (which should be equal to the compressed size).
  127. //
  128. BfSeek(0, &pos);
  129. *BytesProcessed = (uint32_t)(pos - RcState.Start);
  130. return (RcState.Code == 0);
  131. }
  132. void
  133. RcNormalize (
  134. void
  135. )
  136. {
  137. uint8_t rcByte;
  138. //
  139. // Whenever we drop below 24 bits, there is no longer enough precision in
  140. // the probability range not to avoid a "stuck" state where we cannot tell
  141. // apart the two branches (above/below the probability range) because the
  142. // two options appear identical with the number of precision bits that we
  143. // have. In this case, shift the state by a byte (8 bits) and read another.
  144. //
  145. if (RcState.Range < LZMA_RC_MIN_RANGE)
  146. {
  147. RcState.Range <<= 8;
  148. RcState.Code <<= 8;
  149. BfRead(&rcByte);
  150. RcState.Code |= rcByte;
  151. }
  152. }
  153. void
  154. RcAdapt (
  155. bool Miss,
  156. uint16_t* Probability
  157. )
  158. {
  159. //
  160. // In the canonical range encoders out there (including this one used by
  161. // LZMA, we want the probability to adapt (change) as we read more or less
  162. // bits that match our expectation. In order to quickly adapt to change,
  163. // use an exponential moving average. The standard way of doing this is to
  164. // use an integer based adaptation with a shift that's somewhere between
  165. // {1, bits-1}. Since LZMA uses 11 bits for its model, 5 is a nice number
  166. // that lands exactly between 1 and 10.
  167. //
  168. if (Miss)
  169. {
  170. *Probability -= *Probability >> LZMA_RC_ADAPTATION_RATE_SHIFT;
  171. }
  172. else
  173. {
  174. *Probability += (LZMA_RC_MAX_PROBABILITY - *Probability) >>
  175. LZMA_RC_ADAPTATION_RATE_SHIFT;
  176. }
  177. }
  178. uint8_t
  179. RcIsBitSet (
  180. uint16_t* Probability
  181. )
  182. {
  183. uint32_t bound;
  184. uint8_t bit;
  185. //
  186. // Always begin by making sure the range has been normalized for precision
  187. //
  188. RcNormalize();
  189. //
  190. // Check if the current arithmetic code is descried by the next calculated
  191. // proportionally-divided probability range. Recall that the probabilities
  192. // encode the chance of the symbol (bit) being a 0 -- not a 1!
  193. //
  194. // Therefore, if the next chunk of the code lies outside of this new range,
  195. // we are still on the path to our 0. Otherwise, if the code is now part of
  196. // the newly defined range (inclusive), then we produce a 1 and limit the
  197. // range to produce a new range and code for the next decoding pass.
  198. //
  199. bound = (RcState.Range >> LZMA_RC_PROBABILITY_BITS) * *Probability;
  200. if (RcState.Code < bound)
  201. {
  202. RcState.Range = bound;
  203. bit = 0;
  204. }
  205. else
  206. {
  207. RcState.Range -= bound;
  208. RcState.Code -= bound;
  209. bit = 1;
  210. }
  211. //
  212. // Always finish by adapt the probabilities based on the bit value
  213. //
  214. RcAdapt(bit, Probability);
  215. return bit;
  216. }
  217. uint8_t
  218. RcIsFixedBitSet(
  219. void
  220. )
  221. {
  222. uint8_t bit;
  223. //
  224. // This is a specialized version of RcIsBitSet with two differences:
  225. //
  226. // First, there is no adaptive probability -- it is hardcoded to 50%.
  227. //
  228. // Second, because there are 11 bits per probability, and 50% is 1<<10,
  229. // "(LZMA_RC_PROBABILITY_BITS) * Probability" is essentially 1. As such,
  230. // we can just shift by 1 (in other words, halving the range).
  231. //
  232. RcNormalize();
  233. RcState.Range >>= 1;
  234. if (RcState.Code < RcState.Range)
  235. {
  236. bit = 0;
  237. }
  238. else
  239. {
  240. RcState.Code -= RcState.Range;
  241. bit = 1;
  242. }
  243. return bit;
  244. }
  245. uint8_t
  246. RcGetBitTree (
  247. uint16_t* BitModel,
  248. uint16_t Limit
  249. )
  250. {
  251. uint16_t symbol;
  252. //
  253. // Context probability bit trees always begin at index 1. Iterate over each
  254. // decoded bit and just keep shifting it in place, until we reach the total
  255. // expected number of bits, which should never be over 8 (limit is 0x100).
  256. //
  257. // Once decoded, always subtract the limit back from the symbol since we go
  258. // one bit "past" the limit in the loop, as a side effect of the tree being
  259. // off-by-one.
  260. //
  261. for (symbol = 1; symbol < Limit; )
  262. {
  263. symbol = (uint16_t)(symbol << 1) | RcIsBitSet(&BitModel[symbol]);
  264. }
  265. return (symbol - Limit) & 0xFF;
  266. }
  267. uint8_t
  268. RcGetReverseBitTree (
  269. uint16_t* BitModel,
  270. uint8_t HighestBit
  271. )
  272. {
  273. uint16_t symbol;
  274. uint8_t i, bit, result;
  275. //
  276. // This is the same logic as in RcGetBitTree, but with the bits actually
  277. // encoded in reverse order. We keep track of the probability index as the
  278. // "symbol" just like RcGetBitTree, but actually decode the result in the
  279. // opposite order.
  280. //
  281. for (i = 0, symbol = 1, result = 0; i < HighestBit; i++)
  282. {
  283. bit = RcIsBitSet(&BitModel[symbol]);
  284. symbol = (uint16_t)(symbol << 1) | bit;
  285. result |= bit << i;
  286. }
  287. return result;
  288. }
  289. uint8_t
  290. RcDecodeMatchedBitTree (
  291. uint16_t* BitModel,
  292. uint8_t MatchByte
  293. )
  294. {
  295. uint16_t symbol, bytePos, matchBit;
  296. uint8_t bit;
  297. //
  298. // Parse each bit in the "match byte" (see LzDecodeLiteral), which we call
  299. // a "match bit".
  300. //
  301. // Then, treat this as a special bit tree decoding where two possible trees
  302. // are used: one for when the "match bit" is set, and a separate one for
  303. // when the "match bit" is not set. Since each tree can encode up to 256
  304. // symbols, each one has 0x100 slots.
  305. //
  306. // Finally, we have the original bit tree which we'll revert back to once
  307. // the match bits are no longer in play, which we parse for the remainder
  308. // of the symbol.
  309. //
  310. for (bytePos = MatchByte, symbol = 1; symbol < 0x100; bytePos <<= 1)
  311. {
  312. matchBit = (bytePos >> 7) & 1;
  313. bit = RcIsBitSet(&BitModel[symbol + (0x100 * (matchBit + 1))]);
  314. symbol = (uint16_t)(symbol << 1) | bit;
  315. if (matchBit != bit)
  316. {
  317. while (symbol < 0x100)
  318. {
  319. symbol = (uint16_t)(symbol << 1) | RcIsBitSet(&BitModel[symbol]);
  320. }
  321. break;
  322. }
  323. }
  324. return symbol & 0xFF;
  325. }
  326. uint32_t
  327. RcGetFixed (
  328. uint8_t HighestBit
  329. )
  330. {
  331. uint32_t symbol;
  332. //
  333. // Fixed probability bit trees always begin at index 0. Iterate over each
  334. // decoded bit and just keep shifting it in place, until we reach the total
  335. // expected number of bits (typically never higher than 26 -- the maximum
  336. // number of "direct bits" that the distance of a "match" can encode).
  337. //
  338. symbol = 0;
  339. do
  340. {
  341. symbol = (symbol << 1) | RcIsFixedBitSet();
  342. } while (--HighestBit > 0);
  343. return symbol;
  344. }
  345. void
  346. RcSetDefaultProbability (
  347. uint16_t* Probability
  348. )
  349. {
  350. //
  351. // By default, we initialize the probabilities to 0.5 (50% chance).
  352. //
  353. *Probability = k_LzmaRcHalfProbability;
  354. }