Grok  9.5.0
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  { \
29  d = (*curctx)->mps ^ 1; \
30  *curctx = (*curctx)->nlps; \
31  } \
32  else \
33  { \
34  d = (*curctx)->mps; \
35  *curctx = (*curctx)->nmps; \
36  } \
37  }
38 
39 /* For internal use of decompress_macro() */
40 #define lpsexchange_dec_macro(d, curctx, a) \
41  { \
42  if(a < (*curctx)->qeval) \
43  { \
44  a = (*curctx)->qeval; \
45  d = (*curctx)->mps; \
46  *curctx = (*curctx)->nmps; \
47  } \
48  else \
49  { \
50  a = (*curctx)->qeval; \
51  d = (*curctx)->mps ^ 1; \
52  *curctx = (*curctx)->nlps; \
53  } \
54  }
55 
61 static INLINE uint32_t mqc_raw_decode(mqcoder* mqc)
62 {
63  if(mqc->ct == 0)
64  {
65  /* Given mqc_raw_init_dec() we know that at some point we will */
66  /* have a 0xFF 0xFF artificial marker */
67  if(mqc->c == 0xff)
68  {
69  if(*mqc->bp > 0x8f)
70  {
71  mqc->c = 0xff;
72  mqc->ct = 8;
73  }
74  else
75  {
76  mqc->c = *mqc->bp;
77  mqc->bp++;
78  mqc->ct = 7;
79  }
80  }
81  else
82  {
83  mqc->c = *mqc->bp;
84  mqc->bp++;
85  mqc->ct = 8;
86  }
87  }
88  mqc->ct--;
89 
90  return ((uint32_t)mqc->c >> mqc->ct) & 0x01U;
91 }
92 
93 #define bytein_dec_macro(mqc, c, ct) \
94  { \
95  /* Given mqc_init_dec() we know that at some point we will */ \
96  /* have a 0xFF 0xFF artificial marker */ \
97  uint32_t l_c = *(mqc->bp + 1); \
98  if(*mqc->bp == 0xff) \
99  { \
100  if(l_c > 0x8f) \
101  { \
102  c += 0xff00; \
103  ct = 8; \
104  mqc->end_of_byte_stream_counter++; \
105  } \
106  else \
107  { \
108  mqc->bp++; \
109  c += l_c << 9; \
110  ct = 7; \
111  } \
112  } \
113  else \
114  { \
115  mqc->bp++; \
116  c += l_c << 8; \
117  ct = 8; \
118  } \
119  }
120 
121 /* For internal use of decompress_macro() */
122 #define renorm_dec_macro(mqc, a, c, ct) \
123  { \
124  do \
125  { \
126  if(ct == 0) \
127  bytein_dec_macro(mqc, c, ct); \
128  a <<= 1; \
129  c <<= 1; \
130  ct--; \
131  } while(a < A_MIN); \
132  }
133 
134 #define decompress_macro(d, mqc, curctx, a, c, ct) \
135  { \
136  /* Implements ISO 15444-1 C.3.2 Decompressing a decision (DECODE) */ \
137  a -= (*curctx)->qeval; \
138  uint32_t qeval_shift = (*curctx)->qeval << 16; \
139  if(c < qeval_shift) \
140  { \
141  lpsexchange_dec_macro(d, curctx, a); \
142  renorm_dec_macro(mqc, a, c, ct); \
143  } \
144  else \
145  { \
146  c -= qeval_shift; \
147  if(a < A_MIN) \
148  { \
149  mpsexchange_dec_macro(d, curctx, a); \
150  renorm_dec_macro(mqc, a, c, ct); \
151  } \
152  else \
153  { \
154  d = (*curctx)->mps; \
155  } \
156  } \
157  }
158 
163 static INLINE void mqc_bytein(mqcoder* const mqc)
164 {
165  bytein_dec_macro(mqc, mqc->c, mqc->ct);
166 }
167 
172 #define mqc_renormd(mqc) renorm_dec_macro(mqc, mqc->a, mqc->c, mqc->ct)
173 
180 #define mqc_decode(d, mqc) decompress_macro(d, mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct)
static INLINE uint32_t mqc_raw_decode(mqcoder *mqc)
Decompress a symbol using raw-decoder.
Definition: mqc_dec_inl.h:61
static INLINE void mqc_bytein(mqcoder *const mqc)
Input a byte.
Definition: mqc_dec_inl.h:163
#define bytein_dec_macro(mqc, c, ct)
Definition: mqc_dec_inl.h:93
#define INLINE
Definition: t1_common.h:28