14d1abfb2Sjoerg ///////////////////////////////////////////////////////////////////////////////
24d1abfb2Sjoerg //
34d1abfb2Sjoerg /// \file range_decoder.h
44d1abfb2Sjoerg /// \brief Range Decoder
54d1abfb2Sjoerg ///
64d1abfb2Sjoerg // Authors: Igor Pavlov
74d1abfb2Sjoerg // Lasse Collin
84d1abfb2Sjoerg //
94d1abfb2Sjoerg // This file has been put into the public domain.
104d1abfb2Sjoerg // You can do whatever you want with this file.
114d1abfb2Sjoerg //
124d1abfb2Sjoerg ///////////////////////////////////////////////////////////////////////////////
134d1abfb2Sjoerg
144d1abfb2Sjoerg #ifndef LZMA_RANGE_DECODER_H
154d1abfb2Sjoerg #define LZMA_RANGE_DECODER_H
164d1abfb2Sjoerg
174d1abfb2Sjoerg #include "range_common.h"
184d1abfb2Sjoerg
194d1abfb2Sjoerg
204d1abfb2Sjoerg typedef struct {
214d1abfb2Sjoerg uint32_t range;
224d1abfb2Sjoerg uint32_t code;
234d1abfb2Sjoerg uint32_t init_bytes_left;
244d1abfb2Sjoerg } lzma_range_decoder;
254d1abfb2Sjoerg
264d1abfb2Sjoerg
274d1abfb2Sjoerg /// Reads the first five bytes to initialize the range decoder.
28*7653b22fSchristos static inline lzma_ret
rc_read_init(lzma_range_decoder * rc,const uint8_t * restrict in,size_t * restrict in_pos,size_t in_size)294d1abfb2Sjoerg rc_read_init(lzma_range_decoder *rc, const uint8_t *restrict in,
304d1abfb2Sjoerg size_t *restrict in_pos, size_t in_size)
314d1abfb2Sjoerg {
324d1abfb2Sjoerg while (rc->init_bytes_left > 0) {
334d1abfb2Sjoerg if (*in_pos == in_size)
34*7653b22fSchristos return LZMA_OK;
35*7653b22fSchristos
36*7653b22fSchristos // The first byte is always 0x00. It could have been omitted
37*7653b22fSchristos // in LZMA2 but it wasn't, so one byte is wasted in every
38*7653b22fSchristos // LZMA2 chunk.
39*7653b22fSchristos if (rc->init_bytes_left == 5 && in[*in_pos] != 0x00)
40*7653b22fSchristos return LZMA_DATA_ERROR;
414d1abfb2Sjoerg
424d1abfb2Sjoerg rc->code = (rc->code << 8) | in[*in_pos];
434d1abfb2Sjoerg ++*in_pos;
444d1abfb2Sjoerg --rc->init_bytes_left;
454d1abfb2Sjoerg }
464d1abfb2Sjoerg
47*7653b22fSchristos return LZMA_STREAM_END;
484d1abfb2Sjoerg }
494d1abfb2Sjoerg
504d1abfb2Sjoerg
514d1abfb2Sjoerg /// Makes local copies of range decoder and *in_pos variables. Doing this
524d1abfb2Sjoerg /// improves speed significantly. The range decoder macros expect also
534d1abfb2Sjoerg /// variables `in' and `in_size' to be defined.
544d1abfb2Sjoerg #define rc_to_local(range_decoder, in_pos) \
554d1abfb2Sjoerg lzma_range_decoder rc = range_decoder; \
564d1abfb2Sjoerg size_t rc_in_pos = (in_pos); \
574d1abfb2Sjoerg uint32_t rc_bound
584d1abfb2Sjoerg
594d1abfb2Sjoerg
604d1abfb2Sjoerg /// Stores the local copes back to the range decoder structure.
614d1abfb2Sjoerg #define rc_from_local(range_decoder, in_pos) \
624d1abfb2Sjoerg do { \
634d1abfb2Sjoerg range_decoder = rc; \
644d1abfb2Sjoerg in_pos = rc_in_pos; \
654d1abfb2Sjoerg } while (0)
664d1abfb2Sjoerg
674d1abfb2Sjoerg
684d1abfb2Sjoerg /// Resets the range decoder structure.
694d1abfb2Sjoerg #define rc_reset(range_decoder) \
704d1abfb2Sjoerg do { \
714d1abfb2Sjoerg (range_decoder).range = UINT32_MAX; \
724d1abfb2Sjoerg (range_decoder).code = 0; \
734d1abfb2Sjoerg (range_decoder).init_bytes_left = 5; \
744d1abfb2Sjoerg } while (0)
754d1abfb2Sjoerg
764d1abfb2Sjoerg
774d1abfb2Sjoerg /// When decoding has been properly finished, rc.code is always zero unless
784d1abfb2Sjoerg /// the input stream is corrupt. So checking this can catch some corrupt
794d1abfb2Sjoerg /// files especially if they don't have any other integrity check.
804d1abfb2Sjoerg #define rc_is_finished(range_decoder) \
814d1abfb2Sjoerg ((range_decoder).code == 0)
824d1abfb2Sjoerg
834d1abfb2Sjoerg
844d1abfb2Sjoerg /// Read the next input byte if needed. If more input is needed but there is
854d1abfb2Sjoerg /// no more input available, "goto out" is used to jump out of the main
864d1abfb2Sjoerg /// decoder loop.
874d1abfb2Sjoerg #define rc_normalize(seq) \
884d1abfb2Sjoerg do { \
894d1abfb2Sjoerg if (rc.range < RC_TOP_VALUE) { \
904d1abfb2Sjoerg if (unlikely(rc_in_pos == in_size)) { \
914d1abfb2Sjoerg coder->sequence = seq; \
924d1abfb2Sjoerg goto out; \
934d1abfb2Sjoerg } \
944d1abfb2Sjoerg rc.range <<= RC_SHIFT_BITS; \
954d1abfb2Sjoerg rc.code = (rc.code << RC_SHIFT_BITS) | in[rc_in_pos++]; \
964d1abfb2Sjoerg } \
974d1abfb2Sjoerg } while (0)
984d1abfb2Sjoerg
994d1abfb2Sjoerg
1004d1abfb2Sjoerg /// Start decoding a bit. This must be used together with rc_update_0()
1014d1abfb2Sjoerg /// and rc_update_1():
1024d1abfb2Sjoerg ///
1034d1abfb2Sjoerg /// rc_if_0(prob, seq) {
1044d1abfb2Sjoerg /// rc_update_0(prob);
1054d1abfb2Sjoerg /// // Do something
1064d1abfb2Sjoerg /// } else {
1074d1abfb2Sjoerg /// rc_update_1(prob);
1084d1abfb2Sjoerg /// // Do something else
1094d1abfb2Sjoerg /// }
1104d1abfb2Sjoerg ///
1114d1abfb2Sjoerg #define rc_if_0(prob, seq) \
1124d1abfb2Sjoerg rc_normalize(seq); \
1134d1abfb2Sjoerg rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) * (prob); \
1144d1abfb2Sjoerg if (rc.code < rc_bound)
1154d1abfb2Sjoerg
1164d1abfb2Sjoerg
1174d1abfb2Sjoerg /// Update the range decoder state and the used probability variable to
1184d1abfb2Sjoerg /// match a decoded bit of 0.
1194d1abfb2Sjoerg #define rc_update_0(prob) \
1204d1abfb2Sjoerg do { \
1214d1abfb2Sjoerg rc.range = rc_bound; \
1224d1abfb2Sjoerg prob += (RC_BIT_MODEL_TOTAL - (prob)) >> RC_MOVE_BITS; \
1234d1abfb2Sjoerg } while (0)
1244d1abfb2Sjoerg
1254d1abfb2Sjoerg
1264d1abfb2Sjoerg /// Update the range decoder state and the used probability variable to
1274d1abfb2Sjoerg /// match a decoded bit of 1.
1284d1abfb2Sjoerg #define rc_update_1(prob) \
1294d1abfb2Sjoerg do { \
1304d1abfb2Sjoerg rc.range -= rc_bound; \
1314d1abfb2Sjoerg rc.code -= rc_bound; \
1324d1abfb2Sjoerg prob -= (prob) >> RC_MOVE_BITS; \
1334d1abfb2Sjoerg } while (0)
1344d1abfb2Sjoerg
1354d1abfb2Sjoerg
1364d1abfb2Sjoerg /// Decodes one bit and runs action0 or action1 depending on the decoded bit.
1374d1abfb2Sjoerg /// This macro is used as the last step in bittree reverse decoders since
1384d1abfb2Sjoerg /// those don't use "symbol" for anything else than indexing the probability
1394d1abfb2Sjoerg /// arrays.
1404d1abfb2Sjoerg #define rc_bit_last(prob, action0, action1, seq) \
1414d1abfb2Sjoerg do { \
1424d1abfb2Sjoerg rc_if_0(prob, seq) { \
1434d1abfb2Sjoerg rc_update_0(prob); \
1444d1abfb2Sjoerg action0; \
1454d1abfb2Sjoerg } else { \
1464d1abfb2Sjoerg rc_update_1(prob); \
1474d1abfb2Sjoerg action1; \
1484d1abfb2Sjoerg } \
1494d1abfb2Sjoerg } while (0)
1504d1abfb2Sjoerg
1514d1abfb2Sjoerg
1524d1abfb2Sjoerg /// Decodes one bit, updates "symbol", and runs action0 or action1 depending
1534d1abfb2Sjoerg /// on the decoded bit.
1544d1abfb2Sjoerg #define rc_bit(prob, action0, action1, seq) \
1554d1abfb2Sjoerg rc_bit_last(prob, \
1564d1abfb2Sjoerg symbol <<= 1; action0, \
1574d1abfb2Sjoerg symbol = (symbol << 1) + 1; action1, \
1584d1abfb2Sjoerg seq);
1594d1abfb2Sjoerg
1604d1abfb2Sjoerg
1614d1abfb2Sjoerg /// Like rc_bit() but add "case seq:" as a prefix. This makes the unrolled
1624d1abfb2Sjoerg /// loops more readable because the code isn't littered with "case"
1634d1abfb2Sjoerg /// statements. On the other hand this also makes it less readable, since
1644d1abfb2Sjoerg /// spotting the places where the decoder loop may be restarted is less
1654d1abfb2Sjoerg /// obvious.
1664d1abfb2Sjoerg #define rc_bit_case(prob, action0, action1, seq) \
1674d1abfb2Sjoerg case seq: rc_bit(prob, action0, action1, seq)
1684d1abfb2Sjoerg
1694d1abfb2Sjoerg
1704d1abfb2Sjoerg /// Decode a bit without using a probability.
1714d1abfb2Sjoerg #define rc_direct(dest, seq) \
1724d1abfb2Sjoerg do { \
1734d1abfb2Sjoerg rc_normalize(seq); \
1744d1abfb2Sjoerg rc.range >>= 1; \
1754d1abfb2Sjoerg rc.code -= rc.range; \
1764d1abfb2Sjoerg rc_bound = UINT32_C(0) - (rc.code >> 31); \
1774d1abfb2Sjoerg rc.code += rc.range & rc_bound; \
1784d1abfb2Sjoerg dest = (dest << 1) + (rc_bound + 1); \
1794d1abfb2Sjoerg } while (0)
1804d1abfb2Sjoerg
1814d1abfb2Sjoerg
1824d1abfb2Sjoerg // NOTE: No macros are provided for bittree decoding. It seems to be simpler
1834d1abfb2Sjoerg // to just write them open in the code.
1844d1abfb2Sjoerg
1854d1abfb2Sjoerg #endif
186