Grok 20.3.2
mqc_base.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2026 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
18#include "mqc_state.h"
19#include <string>
20#include <cstddef>
21
22#pragma once
23
24namespace grk::t1
25{
26
27typedef uint32_t grk_flag;
28
29#define MQC_NUMCTXS 19
30
31#define PUSH_MQC() \
32 auto curctx = coder.curctx; \
33 uint32_t c = coder.c; \
34 uint32_t a = coder.a; \
35 uint8_t ct = coder.ct
36
37#define POP_MQC() \
38 coder.curctx = curctx; \
39 coder.c = c; \
40 coder.a = a; \
41 coder.ct = ct;
42
48
49/* SIGMA: significance state (3 cols x 6 rows)
50 * CHI: state for negative sample value (1 col x 6 rows)
51 * MU: state for visited in refinement pass (1 col x 4 rows)
52 * PI: state for visited in significance pass (1 col * 4 rows)
53 */
54
55#define T1_SIGMA_0 (1U << 0)
56#define T1_SIGMA_1 (1U << 1)
57#define T1_SIGMA_2 (1U << 2)
58#define T1_SIGMA_3 (1U << 3)
59#define T1_SIGMA_4 (1U << 4)
60#define T1_SIGMA_5 (1U << 5)
61#define T1_SIGMA_6 (1U << 6)
62#define T1_SIGMA_7 (1U << 7)
63#define T1_SIGMA_8 (1U << 8)
64#define T1_SIGMA_9 (1U << 9)
65#define T1_SIGMA_10 (1U << 10)
66#define T1_SIGMA_11 (1U << 11)
67#define T1_SIGMA_12 (1U << 12)
68#define T1_SIGMA_13 (1U << 13)
69#define T1_SIGMA_14 (1U << 14)
70#define T1_SIGMA_15 (1U << 15)
71#define T1_SIGMA_16 (1U << 16)
72#define T1_SIGMA_17 (1U << 17)
73#define T1_CHI_0 (1U << 18)
74#define T1_CHI_0_I 18
75#define T1_CHI_1 (1U << 19)
76#define T1_CHI_1_I 19
77#define T1_MU_0 (1U << 20)
78#define T1_PI_0 (1U << 21)
79#define T1_CHI_2 (1U << 22)
80#define T1_CHI_2_I 22
81#define T1_MU_1 (1U << 23)
82#define T1_PI_1_I 24
83#define T1_PI_1 (1U << T1_PI_1_I)
84#define T1_CHI_3 (1U << 25)
85#define T1_MU_2 (1U << 26)
86#define T1_PI_2_I 27
87#define T1_PI_2 (1U << T1_PI_2_I)
88#define T1_CHI_4 (1U << 28)
89#define T1_MU_3 (1U << 29)
90#define T1_PI_3 (1U << 30)
91#define T1_CHI_5 (1U << 31)
92#define T1_CHI_5_I 31
93
113
114#define T1_SIGMA_NW T1_SIGMA_0
115#define T1_SIGMA_N T1_SIGMA_1
116#define T1_SIGMA_NE T1_SIGMA_2
117#define T1_SIGMA_W T1_SIGMA_3
118#define T1_SIGMA_THIS T1_SIGMA_4
119#define T1_SIGMA_E T1_SIGMA_5
120#define T1_SIGMA_SW T1_SIGMA_6
121#define T1_SIGMA_S T1_SIGMA_7
122#define T1_SIGMA_SE T1_SIGMA_8
123#define T1_SIGMA_NEIGHBOURS \
124 (T1_SIGMA_NW | T1_SIGMA_N | T1_SIGMA_NE | T1_SIGMA_W | T1_SIGMA_E | T1_SIGMA_SW | T1_SIGMA_S | \
125 T1_SIGMA_SE)
126
127#define T1_CHI_THIS T1_CHI_1
128#define T1_CHI_THIS_I T1_CHI_1_I
129#define T1_MU_THIS T1_MU_0
130#define T1_PI_THIS T1_PI_0
131#define T1_CHI_S T1_CHI_2
132
133#define T1_LUT_SGN_W (1U << 0)
134#define T1_LUT_SIG_N (1U << 1)
135#define T1_LUT_SGN_E (1U << 2)
136#define T1_LUT_SIG_W (1U << 3)
137#define T1_LUT_SGN_N (1U << 4)
138#define T1_LUT_SIG_E (1U << 5)
139#define T1_LUT_SGN_S (1U << 6)
140#define T1_LUT_SIG_S (1U << 7)
141
142#define T1_TYPE_MQ 0
143#define T1_TYPE_RAW 1
144
145#define SETCURCTX(curctx, ctxno) curctx = &(mqc)->ctxs[(uint32_t)(ctxno)]
146
147#define GETCTXNO_MAG(f) \
148 (uint8_t)(T1_CTXNO_MAG + (((f) & T1_MU_0) ? 2 : !!((f) & T1_SIGMA_NEIGHBOURS)))
149
150#define UPDATE_FLAGS(flags, flagsPtr, ci, s, stride, vsc) \
151 { \
152 /* east */ \
153 flagsPtr[-1] |= T1_SIGMA_5 << (ci); \
154 /* mark target as significant */ \
155 flags |= ((s << T1_CHI_1_I) | T1_SIGMA_4) << (ci); \
156 /* west */ \
157 flagsPtr[1] |= T1_SIGMA_3 << (ci); \
158 /* north-west, north, north-east */ \
159 if(ci == 0U && !(vsc)) \
160 { \
161 auto north = flagsPtr - (stride); \
162 *north |= (s << T1_CHI_5_I) | T1_SIGMA_16; \
163 north[-1] |= T1_SIGMA_17; \
164 north[1] |= T1_SIGMA_15; \
165 } \
166 /* south-west, south, south-east */ \
167 if(ci == 9U) \
168 { \
169 auto south = flagsPtr + (stride); \
170 *south |= (s << T1_CHI_0_I) | T1_SIGMA_1; \
171 south[-1] |= T1_SIGMA_2; \
172 south[1] |= T1_SIGMA_0; \
173 } \
174 }
175
176#define GETCTXNO_ZC(mqc, f) (mqc)->lut_ctxno_zc_orient[((f) & T1_SIGMA_NEIGHBOURS)]
177
178/*
1790 pfX T1_CHI_THIS T1_LUT_SGN_W
1801 tfX T1_SIGMA_1 T1_LUT_SIG_N
1812 nfX T1_CHI_THIS T1_LUT_SGN_E
1823 tfX T1_SIGMA_3 T1_LUT_SIG_W
1834 fX T1_CHI_(THIS - 1) T1_LUT_SGN_N
1845 tfX T1_SIGMA_5 T1_LUT_SIG_E
1856 fX T1_CHI_(THIS + 1) T1_LUT_SGN_S
1867 tfX T1_SIGMA_7 T1_LUT_SIG_S
187*/
188inline uint8_t getctxtno_sc_or_spb_index(uint32_t fX, uint32_t pfX, uint32_t nfX, uint32_t ci)
189{
190 uint32_t lu = (fX >> (ci)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 | T1_SIGMA_7);
191 lu |= (pfX >> (T1_CHI_THIS_I + (ci))) & (1U << 0);
192 lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci))) & (1U << 2);
193 if(ci == 0U)
194 lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4);
195 else
196 lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 3U)))) & (1U << 4);
197 lu |= (fX >> (T1_CHI_2_I - 6U + (ci))) & (1U << 6);
198 return (uint8_t)lu;
199}
200
206{
210 explicit mqcoder_base(bool cached);
211
215 ~mqcoder_base() = default;
216
217 // Copy constructor
218 mqcoder_base(const mqcoder_base& other);
219
220 // Assignment operator for mqcoder_base
221 mqcoder_base& operator=(const mqcoder_base& other);
222
223 bool operator==(const mqcoder_base& other) const;
227 void print(const std::string& msg);
228
229 void reinit(void);
230
234 uint32_t c;
235
239 uint32_t a;
243 uint8_t ct;
244
254 uint8_t* bp;
255
264
268 ptrdiff_t curctx_index_;
269
274
279};
280
281} // namespace grk::t1
#define T1_CHI_2_I
Definition mqc_base.h:80
#define MQC_NUMCTXS
Definition mqc_base.h:29
#define T1_CHI_0_I
Definition mqc_base.h:74
#define T1_CHI_1_I
Definition mqc_base.h:76
#define T1_SIGMA_3
Definition mqc_base.h:58
#define T1_SIGMA_5
Definition mqc_base.h:60
#define T1_SIGMA_7
Definition mqc_base.h:62
#define T1_CHI_THIS_I
Definition mqc_base.h:128
#define T1_SIGMA_1
Definition mqc_base.h:56
Definition SchedulerFreebyrd.h:36
uint8_t getctxtno_sc_or_spb_index(uint32_t fX, uint32_t pfX, uint32_t nfX, uint32_t ci)
Definition mqc_base.h:188
uint32_t grk_flag
Definition mqc_base.h:27
Definition mqc_state.h:30
const mqc_state ** curctx
Pointer to current context in ctxs array.
Definition mqc_base.h:263
uint8_t ct
Number of bits already read / available to write.
Definition mqc_base.h:243
uint32_t c
Temporary buffer where bits are coded or decoded.
Definition mqc_base.h:234
mqcoder_base & operator=(const mqcoder_base &other)
Definition mqc_base.cpp:50
~mqcoder_base()=default
Destroys an mqcoder_base.
bool operator==(const mqcoder_base &other) const
Definition mqc_base.cpp:73
uint32_t a
Definition mqc_base.h:239
void print(const std::string &msg)
Prints internal state.
Definition mqc_base.cpp:100
const mqc_state * ctxs[MQC_NUMCTXS]
Array of contexts.
Definition mqc_base.h:259
bool finalLayer_
true if final layer is being decompressed
Definition mqc_base.h:278
uint8_t * bp
Pointer to current position in buffer.
Definition mqc_base.h:254
mqcoder_base(bool cached)
Creates an mqcoder_base.
Definition mqc_base.cpp:26
void reinit(void)
Definition mqc_base.cpp:37
ptrdiff_t curctx_index_
Index of curctx in ctxs array.
Definition mqc_base.h:268
uint32_t end_of_byte_stream_counter
Count the number of times a terminating {0xFF, >0x8F} marker is read.
Definition mqc_base.h:250
bool cached_
true if in differential decompress mode
Definition mqc_base.h:273