range_decoder.h 4.7 KB

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