range_decoder.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file range_decoder.h
  4. /// \brief Range Decoder
  5. ///
  6. // Authors: Igor Pavlov
  7. // Lasse Collin
  8. //
  9. // This file has been put into the public domain.
  10. // You can do whatever you want with this file.
  11. //
  12. ///////////////////////////////////////////////////////////////////////////////
  13. #ifndef LZMA_RANGE_DECODER_H
  14. #define LZMA_RANGE_DECODER_H
  15. #include "range_common.h"
  16. typedef struct {
  17. uint32_t range;
  18. uint32_t code;
  19. uint32_t init_bytes_left;
  20. } lzma_range_decoder;
  21. /// Reads the first five bytes to initialize the range decoder.
  22. static inline lzma_ret
  23. rc_read_init(lzma_range_decoder *rc, const uint8_t *restrict in,
  24. size_t *restrict in_pos, size_t in_size)
  25. {
  26. while (rc->init_bytes_left > 0) {
  27. if (*in_pos == in_size)
  28. return LZMA_OK;
  29. // The first byte is always 0x00. It could have been omitted
  30. // in LZMA2 but it wasn't, so one byte is wasted in every
  31. // LZMA2 chunk.
  32. if (rc->init_bytes_left == 5 && in[*in_pos] != 0x00)
  33. return LZMA_DATA_ERROR;
  34. rc->code = (rc->code << 8) | in[*in_pos];
  35. ++*in_pos;
  36. --rc->init_bytes_left;
  37. }
  38. return LZMA_STREAM_END;
  39. }
  40. /// Makes local copies of range decoder and *in_pos variables. Doing this
  41. /// improves speed significantly. The range decoder macros expect also
  42. /// variables `in' and `in_size' to be defined.
  43. #define rc_to_local(range_decoder, in_pos) \
  44. lzma_range_decoder rc = range_decoder; \
  45. size_t rc_in_pos = (in_pos); \
  46. uint32_t rc_bound
  47. /// Stores the local copes back to the range decoder structure.
  48. #define rc_from_local(range_decoder, in_pos) \
  49. do { \
  50. range_decoder = rc; \
  51. in_pos = rc_in_pos; \
  52. } while (0)
  53. /// Resets the range decoder structure.
  54. #define rc_reset(range_decoder) \
  55. do { \
  56. (range_decoder).range = UINT32_MAX; \
  57. (range_decoder).code = 0; \
  58. (range_decoder).init_bytes_left = 5; \
  59. } while (0)
  60. /// When decoding has been properly finished, rc.code is always zero unless
  61. /// the input stream is corrupt. So checking this can catch some corrupt
  62. /// files especially if they don't have any other integrity check.
  63. #define rc_is_finished(range_decoder) \
  64. ((range_decoder).code == 0)
  65. /// Read the next input byte if needed. If more input is needed but there is
  66. /// no more input available, "goto out" is used to jump out of the main
  67. /// decoder loop.
  68. #define rc_normalize(seq) \
  69. do { \
  70. if (rc.range < RC_TOP_VALUE) { \
  71. if (unlikely(rc_in_pos == in_size)) { \
  72. coder->sequence = seq; \
  73. goto out; \
  74. } \
  75. rc.range <<= RC_SHIFT_BITS; \
  76. rc.code = (rc.code << RC_SHIFT_BITS) | in[rc_in_pos++]; \
  77. } \
  78. } while (0)
  79. /// Start decoding a bit. This must be used together with rc_update_0()
  80. /// and rc_update_1():
  81. ///
  82. /// rc_if_0(prob, seq) {
  83. /// rc_update_0(prob);
  84. /// // Do something
  85. /// } else {
  86. /// rc_update_1(prob);
  87. /// // Do something else
  88. /// }
  89. ///
  90. #define rc_if_0(prob, seq) \
  91. rc_normalize(seq); \
  92. rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) * (prob); \
  93. if (rc.code < rc_bound)
  94. /// Update the range decoder state and the used probability variable to
  95. /// match a decoded bit of 0.
  96. #define rc_update_0(prob) \
  97. do { \
  98. rc.range = rc_bound; \
  99. prob += (RC_BIT_MODEL_TOTAL - (prob)) >> RC_MOVE_BITS; \
  100. } while (0)
  101. /// Update the range decoder state and the used probability variable to
  102. /// match a decoded bit of 1.
  103. #define rc_update_1(prob) \
  104. do { \
  105. rc.range -= rc_bound; \
  106. rc.code -= rc_bound; \
  107. prob -= (prob) >> RC_MOVE_BITS; \
  108. } while (0)
  109. /// Decodes one bit and runs action0 or action1 depending on the decoded bit.
  110. /// This macro is used as the last step in bittree reverse decoders since
  111. /// those don't use "symbol" for anything else than indexing the probability
  112. /// arrays.
  113. #define rc_bit_last(prob, action0, action1, seq) \
  114. do { \
  115. rc_if_0(prob, seq) { \
  116. rc_update_0(prob); \
  117. action0; \
  118. } else { \
  119. rc_update_1(prob); \
  120. action1; \
  121. } \
  122. } while (0)
  123. /// Decodes one bit, updates "symbol", and runs action0 or action1 depending
  124. /// on the decoded bit.
  125. #define rc_bit(prob, action0, action1, seq) \
  126. rc_bit_last(prob, \
  127. symbol <<= 1; action0, \
  128. symbol = (symbol << 1) + 1; action1, \
  129. seq);
  130. /// Like rc_bit() but add "case seq:" as a prefix. This makes the unrolled
  131. /// loops more readable because the code isn't littered with "case"
  132. /// statements. On the other hand this also makes it less readable, since
  133. /// spotting the places where the decoder loop may be restarted is less
  134. /// obvious.
  135. #define rc_bit_case(prob, action0, action1, seq) \
  136. case seq: rc_bit(prob, action0, action1, seq)
  137. /// Decode a bit without using a probability.
  138. #define rc_direct(dest, seq) \
  139. do { \
  140. rc_normalize(seq); \
  141. rc.range >>= 1; \
  142. rc.code -= rc.range; \
  143. rc_bound = UINT32_C(0) - (rc.code >> 31); \
  144. rc.code += rc.range & rc_bound; \
  145. dest = (dest << 1) + (rc_bound + 1); \
  146. } while (0)
  147. // NOTE: No macros are provided for bittree decoding. It seems to be simpler
  148. // to just write them open in the code.
  149. #endif