Grok 15.1.0
mqc.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2025 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#include <t1_common.h>
26namespace grk
28
29// the next line must be uncommented in order to support debugging
30// for plugin encode
31// #define PLUGIN_DEBUG_ENCODE
32
33struct mqc_state;
34struct mqcoder;
35
37{
39 uint32_t qeval;
41 uint32_t mps;
46};
47
48#define MQC_NUMCTXS 19
49struct mqcoder
50{
52 uint32_t c;
54 uint32_t a;
56 uint32_t ct;
57 /* only used by decoder, to count the number of times a terminating 0xFF >0x8F marker is read */
60 uint8_t* bp;
62 uint8_t* start;
64 uint8_t* end;
69 /* lut_ctxno_zc shifted by (1 << 9) * bandIndex */
70 const uint8_t* lut_ctxno_zc_orient;
73#ifdef PLUGIN_DEBUG_ENCODE
74 grk_plugin_debug_mqc debug_mqc;
75#endif
76};
77
78const uint32_t A_MIN = 0x8000;
79
80#include "mqc_inl.h"
81#include "mqc_dec_inl.h"
82#include "mqc_enc_inl.h"
83
84uint32_t mqc_numbytes_enc(mqcoder* mqc);
85void mqc_resetstates(mqcoder* mqc);
86
87/* ENCODE */
88
89void mqc_init_enc(mqcoder* mqc, uint8_t* bp);
90void mqc_encode(mqcoder* mqc, uint32_t d);
91void mqc_flush_enc(mqcoder* mqc);
93uint32_t mqc_bypass_get_extra_bytes_enc(mqcoder* mqc, bool erterm);
94void mqc_bypass_enc(mqcoder* mqc, uint32_t d);
95void mqc_bypass_flush_enc(mqcoder* mqc, bool erterm);
96void mqc_restart_init_enc(mqcoder* mqc);
97void mqc_erterm_enc(mqcoder* mqc);
98void mqc_segmark_enc(mqcoder* mqc);
99
100/* DECODE */
101
117void mqc_init_dec(mqcoder* mqc, uint8_t* bp, uint32_t len);
118
121
123passes, so as to restore the bytes temporarily overwritten.
124
125@param mqc MQC handle
126@param bp Pointer to the start of the buffer from which the bytes will be read
127 Note that OPJ_COMMON_CBLK_DATA_EXTRA bytes at the end of the buffer
128 will be temporarily overwritten with an artificial 0xFF 0xFF marker.
129 (they will be backuped in the mqc structure to be restored later)
130 So bp must be at least len + OPJ_COMMON_CBLK_DATA_EXTRA large, and
131 writable.
132@param len Length of the input buffer
133*/
134void mqc_raw_init_dec(mqcoder* mqc, uint8_t* bp, uint32_t len);
144void mqc_finish_dec(mqcoder* mqc);
145
146} // namespace grk
#define MQC_NUMCTXS
Definition mqc.h:48
Copyright (C) 2016-2025 Grok Image Compression Inc.
Definition ICacheable.h:20
void mqc_flush_enc(mqcoder *mqc)
Definition mqc_enc.cpp:213
uint32_t mqc_numbytes_enc(mqcoder *mqc)
Definition mqc_enc.cpp:177
void mqc_segmark_enc(mqcoder *mqc)
Definition mqc_enc.cpp:327
void mqc_raw_init_dec(mqcoder *mqc, uint8_t *bp, uint32_t len)
Initialize the decoder for RAW decoding.
Definition mqc_dec.cpp:107
void mqc_bypass_flush_enc(mqcoder *mqc, bool erterm)
Definition mqc_enc.cpp:252
const uint32_t A_MIN
Definition mqc.h:78
void mqc_erterm_enc(mqcoder *mqc)
Definition mqc_enc.cpp:312
void mqc_bypass_enc(mqcoder *mqc, uint32_t d)
void mqc_finish_dec(mqcoder *mqc)
Terminate RAW/MQC decoding.
Definition mqc_dec.cpp:114
void mqc_restart_init_enc(mqcoder *mqc)
Definition mqc_enc.cpp:294
void mqc_bypass_init_enc(mqcoder *mqc)
Definition mqc_enc.cpp:229
void mqc_encode(mqcoder *mqc, uint32_t d)
Definition mqc_enc.cpp:205
void mqc_init_enc(mqcoder *mqc, uint8_t *bp)
Definition mqc_enc.cpp:182
uint32_t mqc_bypass_get_extra_bytes_enc(mqcoder *mqc, bool erterm)
Definition mqc_enc.cpp:247
void mqc_init_dec(mqcoder *mqc, uint8_t *bp, uint32_t len)
Initialize the decoder for MQ decoding.
Definition mqc_dec.cpp:91
void mqc_resetstates(mqcoder *mqc)
Definition mqc_dec.cpp:120
Definition mqc.h:37
const mqc_state * nmps
next state if the next encoded symbol is the MPS
Definition mqc.h:43
const mqc_state * nlps
next state if the next encoded symbol is the LPS
Definition mqc.h:45
uint32_t mps
the Most Probable Symbol (0 or 1)
Definition mqc.h:41
uint32_t qeval
the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff)
Definition mqc.h:39
Definition mqc.h:50
uint32_t end_of_byte_stream_counter
Definition mqc.h:58
uint8_t * end
pointer to the end of the buffer
Definition mqc.h:64
const mqc_state * ctxs[MQC_NUMCTXS]
Array of contexts.
Definition mqc.h:66
uint32_t a
only used by MQ decoder
Definition mqc.h:54
uint8_t * start
pointer to the start of the buffer
Definition mqc.h:62
const uint8_t * lut_ctxno_zc_orient
Definition mqc.h:70
const mqc_state ** curctx
Active context.
Definition mqc.h:68
uint8_t * bp
pointer to the current position in the buffer
Definition mqc.h:60
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:72
uint32_t ct
number of bits already read or free to write
Definition mqc.h:56
uint32_t c
temporary buffer where bits are coded or decoded
Definition mqc.h:52
const uint8_t grk_cblk_dec_compressed_data_pad_right
< Space for a fake FFFF marker
Definition t1_common.h:37