Grok  7.6.3
mqc_dec_inl.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2021 Grok Image Compression Inc.
3  *
4  * This source code is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License, version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This source code is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Affero General Public License for more details.
12  *
13  * You should have received a copy of the GNU Affero General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  *
17  * This source code incorporates work covered by the BSD 2-clause license.
18  * Please see the LICENSE file in the root directory for details.
19  *
20  */
21 
22 #pragma once
23 
24 /* For internal use of decompress_macro() */
25 #define mpsexchange_dec_macro(d, curctx, a) \
26 { \
27  if (a < (*curctx)->qeval) { \
28  d = (*curctx)->mps ^ 1; \
29  *curctx = (*curctx)->nlps; \
30  } else { \
31  d = (*curctx)->mps; \
32  *curctx = (*curctx)->nmps; \
33  } \
34 }
35 
36 /* For internal use of decompress_macro() */
37 #define lpsexchange_dec_macro(d, curctx, a) \
38 { \
39  if (a < (*curctx)->qeval) { \
40  a = (*curctx)->qeval; \
41  d = (*curctx)->mps; \
42  *curctx = (*curctx)->nmps; \
43  } else { \
44  a = (*curctx)->qeval; \
45  d = (*curctx)->mps ^ 1; \
46  *curctx = (*curctx)->nlps; \
47  } \
48 }
49 
55 static INLINE uint32_t mqc_raw_decode(mqcoder *mqc){
56  if (mqc->ct == 0) {
57  /* Given mqc_raw_init_dec() we know that at some point we will */
58  /* have a 0xFF 0xFF artificial marker */
59  if (mqc->c == 0xff) {
60  if (*mqc->bp > 0x8f) {
61  mqc->c = 0xff;
62  mqc->ct = 8;
63  } else {
64  mqc->c = *mqc->bp;
65  mqc->bp ++;
66  mqc->ct = 7;
67  }
68  } else {
69  mqc->c = *mqc->bp;
70  mqc->bp ++;
71  mqc->ct = 8;
72  }
73  }
74  mqc->ct--;
75 
76  return ((uint32_t)mqc->c >> mqc->ct) & 0x01U;
77 }
78 
79 #define bytein_dec_macro(mqc, c, ct) \
80 { \
81  /* Given mqc_init_dec() we know that at some point we will */ \
82  /* have a 0xFF 0xFF artificial marker */ \
83  uint32_t l_c = *(mqc->bp + 1); \
84  if (*mqc->bp == 0xff) { \
85  if (l_c > 0x8f) { \
86  c += 0xff00; \
87  ct = 8; \
88  mqc->end_of_byte_stream_counter ++; \
89  } else { \
90  mqc->bp++; \
91  c += l_c << 9; \
92  ct = 7; \
93  } \
94  } else { \
95  mqc->bp++; \
96  c += l_c << 8; \
97  ct = 8; \
98  } \
99 }
100 
101 /* For internal use of decompress_macro() */
102 #define renorm_dec_macro(mqc, a, c, ct) \
103 { \
104  do { \
105  if (ct == 0) \
106  bytein_dec_macro(mqc, c, ct); \
107  a <<= 1; \
108  c <<= 1; \
109  ct--; \
110  } while (a < A_MIN); \
111 }
112 
113 #define decompress_macro(d, mqc, curctx, a, c, ct) \
114 { \
115  /* Implements ISO 15444-1 C.3.2 Decompressing a decision (DECODE) */ \
116  a -= (*curctx)->qeval; \
117  uint32_t qeval_shift = (*curctx)->qeval << 16; \
118  if (c < qeval_shift) { \
119  lpsexchange_dec_macro(d, curctx, a); \
120  renorm_dec_macro(mqc, a, c, ct); \
121  } else { \
122  c -= qeval_shift; \
123  if (a < A_MIN) { \
124  mpsexchange_dec_macro(d, curctx, a); \
125  renorm_dec_macro(mqc, a, c, ct); \
126  } else { \
127  d = (*curctx)->mps; \
128  } \
129  } \
130 }
131 
136 static INLINE void mqc_bytein(mqcoder *const mqc){
137  bytein_dec_macro(mqc, mqc->c, mqc->ct);
138 }
139 
144 #define mqc_renormd(mqc) \
145  renorm_dec_macro(mqc, mqc->a, mqc->c, mqc->ct)
146 
153 #define mqc_decode(d, mqc) \
154  decompress_macro(d, mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct)
T1_CTXNO_ZC
#define T1_CTXNO_ZC
Definition: t1_common.h:59
grk::mqcoder::ctxs
const mqc_state * ctxs[MQC_NUMCTXS]
Array of contexts.
Definition: mqc.h:58
grk::mqc_init_dec
void mqc_init_dec(mqcoder *mqc, uint8_t *bp, uint32_t len)
Initialize the decoder for MQ decoding.
Definition: mqc_dec.cpp:140
T1_CTXNO_UNI
#define T1_CTXNO_UNI
Definition: t1_common.h:63
mqc_bytein
static INLINE void mqc_bytein(mqcoder *const mqc)
Input a byte.
Definition: mqc_dec_inl.h:136
grk::mqcoder::end
uint8_t * end
pointer to the end of the buffer
Definition: mqc.h:56
T1_CTXNO_AGG
#define T1_CTXNO_AGG
Definition: t1_common.h:62
t1_common.h
grk::mqc_raw_init_dec
void mqc_raw_init_dec(mqcoder *mqc, uint8_t *bp, uint32_t len)
Initialize the decoder for RAW decoding.
Definition: mqc_dec.cpp:156
grk::mqcoder::start
uint8_t * start
pointer to the start of the buffer
Definition: mqc.h:54
grk::mqc_resetstates
void mqc_resetstates(mqcoder *mqc)
Definition: mqc_dec.cpp:167
grk::A_MIN
const uint32_t A_MIN
Definition: mqc.h:67
bytein_dec_macro
#define bytein_dec_macro(mqc, c, ct)
Definition: mqc_dec_inl.h:79
grk::mqcoder::bp
uint8_t * bp
pointer to the current position in the buffer
Definition: mqc.h:52
mqc_setcurctx
#define mqc_setcurctx(mqc, ctxno)
Definition: mqc_inl.h:38
grk::mqcoder::ct
uint32_t ct
number of bits already read or free to write
Definition: mqc.h:48
grk_cblk_dec_compressed_data_pad_right
const uint8_t grk_cblk_dec_compressed_data_pad_right
< Space for a fake FFFF marker
Definition: t1_common.h:37
grk
Copyright (C) 2016-2021 Grok Image Compression Inc.
Definition: BitIO.cpp:23
grk::mqcoder::c
uint32_t c
temporary buffer where bits are coded or decoded
Definition: mqc.h:44
grk::mqc_state
Definition: mqc.h:30
mqc_raw_decode
static INLINE uint32_t mqc_raw_decode(mqcoder *mqc)
Decompress a symbol using raw-decoder.
Definition: mqc_dec_inl.h:55
INLINE
#define INLINE
Definition: t1_common.h:28
grk::mqc_finish_dec
void mqc_finish_dec(mqcoder *mqc)
Terminate RAW/MQC decoding.
Definition: mqc_dec.cpp:162
grk::mqcoder::backup
uint8_t backup[grk_cblk_dec_compressed_data_pad_right]
Original value of the 2 bytes at end[0] and end[1].
Definition: mqc.h:64
grk::mqc_init_dec_common
static void mqc_init_dec_common(mqcoder *mqc, uint8_t *bp, uint32_t len)
Definition: mqc_dec.cpp:126
grk::mqcoder::end_of_byte_stream_counter
uint32_t end_of_byte_stream_counter
Definition: mqc.h:50
grk::mqc_states
static const mqc_state mqc_states[47 *2]
Definition: mqc_dec.cpp:29
grk::mqcoder::a
uint32_t a
only used by MQ decoder
Definition: mqc.h:46
grk::mqcoder
Definition: mqc.h:42
MQC_NUMCTXS
#define MQC_NUMCTXS
Definition: mqc.h:41