Changeset View
Changeset View
Standalone View
Standalone View
extern/draco/draco/src/draco/compression/entropy/ans.h
- This file was moved from extern/draco/dracoenc/src/draco/compression/entropy/ans.h.
| Show First 20 Lines • Show All 249 Lines • ▼ Show 20 Lines | |||||
| static inline void uabs_write(struct AnsCoder *ans, int val, AnsP8 p0) { | static inline void uabs_write(struct AnsCoder *ans, int val, AnsP8 p0) { | ||||
| AnsP8 p = DRACO_ANS_P8_PRECISION - p0; | AnsP8 p = DRACO_ANS_P8_PRECISION - p0; | ||||
| const unsigned l_s = val ? p : p0; | const unsigned l_s = val ? p : p0; | ||||
| while (ans->state >= | while (ans->state >= | ||||
| DRACO_ANS_L_BASE / DRACO_ANS_P8_PRECISION * DRACO_ANS_IO_BASE * l_s) { | DRACO_ANS_L_BASE / DRACO_ANS_P8_PRECISION * DRACO_ANS_IO_BASE * l_s) { | ||||
| ans->buf[ans->buf_offset++] = ans->state % DRACO_ANS_IO_BASE; | ans->buf[ans->buf_offset++] = ans->state % DRACO_ANS_IO_BASE; | ||||
| ans->state /= DRACO_ANS_IO_BASE; | ans->state /= DRACO_ANS_IO_BASE; | ||||
| } | } | ||||
| if (!val) | if (!val) { | ||||
| ans->state = DRACO_ANS_DIV(ans->state * DRACO_ANS_P8_PRECISION, p0); | ans->state = DRACO_ANS_DIV(ans->state * DRACO_ANS_P8_PRECISION, p0); | ||||
| else | } else { | ||||
| ans->state = | ans->state = | ||||
| DRACO_ANS_DIV((ans->state + 1) * DRACO_ANS_P8_PRECISION + p - 1, p) - 1; | DRACO_ANS_DIV((ans->state + 1) * DRACO_ANS_P8_PRECISION + p - 1, p) - 1; | ||||
| } | } | ||||
| } | |||||
| static inline int uabs_read(struct AnsDecoder *ans, AnsP8 p0) { | static inline int uabs_read(struct AnsDecoder *ans, AnsP8 p0) { | ||||
| AnsP8 p = DRACO_ANS_P8_PRECISION - p0; | AnsP8 p = DRACO_ANS_P8_PRECISION - p0; | ||||
| int s; | int s; | ||||
| // unsigned int xp1; | // unsigned int xp1; | ||||
| unsigned xp, sp; | unsigned xp, sp; | ||||
| unsigned state = ans->state; | unsigned state = ans->state; | ||||
| while (state < DRACO_ANS_L_BASE && ans->buf_offset > 0) { | while (state < DRACO_ANS_L_BASE && ans->buf_offset > 0) { | ||||
| state = state * DRACO_ANS_IO_BASE + ans->buf[--ans->buf_offset]; | state = state * DRACO_ANS_IO_BASE + ans->buf[--ans->buf_offset]; | ||||
| } | } | ||||
| sp = state * p; | sp = state * p; | ||||
| // xp1 = (sp + p) / DRACO_ANS_P8_PRECISION; | // xp1 = (sp + p) / DRACO_ANS_P8_PRECISION; | ||||
| xp = sp / DRACO_ANS_P8_PRECISION; | xp = sp / DRACO_ANS_P8_PRECISION; | ||||
| // s = xp1 - xp; | // s = xp1 - xp; | ||||
| s = (sp & 0xFF) >= p0; | s = (sp & 0xFF) >= p0; | ||||
| if (UNPREDICTABLE(s)) | if (UNPREDICTABLE(s)) { | ||||
| ans->state = xp; | ans->state = xp; | ||||
| else | } else { | ||||
| ans->state = state - xp; | ans->state = state - xp; | ||||
| } | |||||
| return s; | return s; | ||||
| } | } | ||||
| static inline int uabs_read_bit(struct AnsDecoder *ans) { | static inline int uabs_read_bit(struct AnsDecoder *ans) { | ||||
| int s; | int s; | ||||
| unsigned state = ans->state; | unsigned state = ans->state; | ||||
| while (state < DRACO_ANS_L_BASE && ans->buf_offset > 0) { | while (state < DRACO_ANS_L_BASE && ans->buf_offset > 0) { | ||||
| state = state * DRACO_ANS_IO_BASE + ans->buf[--ans->buf_offset]; | state = state * DRACO_ANS_IO_BASE + ans->buf[--ans->buf_offset]; | ||||
| } | } | ||||
| s = static_cast<int>(state & 1); | s = static_cast<int>(state & 1); | ||||
| ans->state = state >> 1; | ans->state = state >> 1; | ||||
| return s; | return s; | ||||
| } | } | ||||
| static inline int ans_read_init(struct AnsDecoder *const ans, | static inline int ans_read_init(struct AnsDecoder *const ans, | ||||
| const uint8_t *const buf, int offset) { | const uint8_t *const buf, int offset) { | ||||
| unsigned x; | unsigned x; | ||||
| if (offset < 1) | if (offset < 1) { | ||||
| return 1; | return 1; | ||||
| } | |||||
| ans->buf = buf; | ans->buf = buf; | ||||
| x = buf[offset - 1] >> 6; | x = buf[offset - 1] >> 6; | ||||
| if (x == 0) { | if (x == 0) { | ||||
| ans->buf_offset = offset - 1; | ans->buf_offset = offset - 1; | ||||
| ans->state = buf[offset - 1] & 0x3F; | ans->state = buf[offset - 1] & 0x3F; | ||||
| } else if (x == 1) { | } else if (x == 1) { | ||||
| if (offset < 2) | if (offset < 2) { | ||||
| return 1; | return 1; | ||||
| } | |||||
| ans->buf_offset = offset - 2; | ans->buf_offset = offset - 2; | ||||
| ans->state = mem_get_le16(buf + offset - 2) & 0x3FFF; | ans->state = mem_get_le16(buf + offset - 2) & 0x3FFF; | ||||
| } else if (x == 2) { | } else if (x == 2) { | ||||
| if (offset < 3) | if (offset < 3) { | ||||
| return 1; | return 1; | ||||
| } | |||||
| ans->buf_offset = offset - 3; | ans->buf_offset = offset - 3; | ||||
| ans->state = mem_get_le24(buf + offset - 3) & 0x3FFFFF; | ans->state = mem_get_le24(buf + offset - 3) & 0x3FFFFF; | ||||
| } else { | } else { | ||||
| return 1; | return 1; | ||||
| } | } | ||||
| ans->state += DRACO_ANS_L_BASE; | ans->state += DRACO_ANS_L_BASE; | ||||
| if (ans->state >= DRACO_ANS_L_BASE * DRACO_ANS_IO_BASE) | if (ans->state >= DRACO_ANS_L_BASE * DRACO_ANS_IO_BASE) { | ||||
| return 1; | return 1; | ||||
| } | |||||
| return 0; | return 0; | ||||
| } | } | ||||
| static inline int ans_read_end(struct AnsDecoder *const ans) { | static inline int ans_read_end(struct AnsDecoder *const ans) { | ||||
| return ans->state == DRACO_ANS_L_BASE; | return ans->state == DRACO_ANS_L_BASE; | ||||
| } | } | ||||
| static inline int ans_reader_has_error(const struct AnsDecoder *const ans) { | static inline int ans_reader_has_error(const struct AnsDecoder *const ans) { | ||||
| ▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | |||||
| public: | public: | ||||
| RAnsDecoder() {} | RAnsDecoder() {} | ||||
| // Initializes the decoder from the input buffer. The |offset| specifies the | // Initializes the decoder from the input buffer. The |offset| specifies the | ||||
| // number of bytes encoded by the encoder. A non zero return value is an | // number of bytes encoded by the encoder. A non zero return value is an | ||||
| // error. | // error. | ||||
| inline int read_init(const uint8_t *const buf, int offset) { | inline int read_init(const uint8_t *const buf, int offset) { | ||||
| unsigned x; | unsigned x; | ||||
| if (offset < 1) | if (offset < 1) { | ||||
| return 1; | return 1; | ||||
| } | |||||
| ans_.buf = buf; | ans_.buf = buf; | ||||
| x = buf[offset - 1] >> 6; | x = buf[offset - 1] >> 6; | ||||
| if (x == 0) { | if (x == 0) { | ||||
| ans_.buf_offset = offset - 1; | ans_.buf_offset = offset - 1; | ||||
| ans_.state = buf[offset - 1] & 0x3F; | ans_.state = buf[offset - 1] & 0x3F; | ||||
| } else if (x == 1) { | } else if (x == 1) { | ||||
| if (offset < 2) | if (offset < 2) { | ||||
| return 1; | return 1; | ||||
| } | |||||
| ans_.buf_offset = offset - 2; | ans_.buf_offset = offset - 2; | ||||
| ans_.state = mem_get_le16(buf + offset - 2) & 0x3FFF; | ans_.state = mem_get_le16(buf + offset - 2) & 0x3FFF; | ||||
| } else if (x == 2) { | } else if (x == 2) { | ||||
| if (offset < 3) | if (offset < 3) { | ||||
| return 1; | return 1; | ||||
| } | |||||
| ans_.buf_offset = offset - 3; | ans_.buf_offset = offset - 3; | ||||
| ans_.state = mem_get_le24(buf + offset - 3) & 0x3FFFFF; | ans_.state = mem_get_le24(buf + offset - 3) & 0x3FFFFF; | ||||
| } else if (x == 3) { | } else if (x == 3) { | ||||
| ans_.buf_offset = offset - 4; | ans_.buf_offset = offset - 4; | ||||
| ans_.state = mem_get_le32(buf + offset - 4) & 0x3FFFFFFF; | ans_.state = mem_get_le32(buf + offset - 4) & 0x3FFFFFFF; | ||||
| } else { | } else { | ||||
| return 1; | return 1; | ||||
| } | } | ||||
| ans_.state += l_rans_base; | ans_.state += l_rans_base; | ||||
| if (ans_.state >= l_rans_base * DRACO_ANS_IO_BASE) | if (ans_.state >= l_rans_base * DRACO_ANS_IO_BASE) { | ||||
| return 1; | return 1; | ||||
| } | |||||
| return 0; | return 0; | ||||
| } | } | ||||
| inline int read_end() { return ans_.state == l_rans_base; } | inline int read_end() { return ans_.state == l_rans_base; } | ||||
| inline int reader_has_error() { | inline int reader_has_error() { | ||||
| return ans_.state < l_rans_base && ans_.buf_offset == 0; | return ans_.state < l_rans_base && ans_.buf_offset == 0; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 66 Lines • Show Last 20 Lines | |||||