Grok 20.3.2
CodeblockCompressImpl.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#pragma once
19
20#include <algorithm>
21#include "CodeblockImpl.h"
23
24namespace grk::t1
25{
26
32{
36 CodePass() : rate_(0), distortiondec_(0), len_(0), term_(false), slope_(0) {}
40 ~CodePass() = default;
44 uint16_t rate_;
52 uint16_t len_;
53
57 bool term_;
58
62 uint16_t slope_;
63};
64
69struct Layer
70{
74 Layer() : totalPasses_(0), len(0), distortion(0), data(nullptr) {}
78 ~Layer() = default;
82 uint8_t totalPasses_;
86 uint16_t len;
90 double distortion;
94 uint8_t* data;
95};
96
98{
99 explicit CodeblockCompressImpl(uint16_t numLayers)
100 : CodeblockImpl(numLayers), paddedCompressedStream(nullptr), layers(nullptr), passes(nullptr),
102#ifdef PLUGIN_DEBUG_ENCODE
103 ,
104 context_stream(nullptr)
105#endif
106 {}
108 {
109 delete[] layers;
110 delete[] passes;
111 }
112 void init()
113 {
115 if(!layers)
116 layers = new Layer[numLayers_];
117 if(!passes)
118 passes = new CodePass[3 * 32 - 2];
119 }
120
126 bool allocData(size_t nominalBlockSize)
127 {
128 // The MQ coder output can exceed nominalBlockSize * 4 for small code blocks
129 // with many bit-planes (up to ~30 planes × 3 passes each, with flush/termination
130 // overhead per pass). Use a minimum of 4096 bytes to prevent overflow.
131 uint32_t desired_data_size =
132 std::max((uint32_t)(nominalBlockSize * sizeof(uint32_t)), (uint32_t)4096);
133 // we add two fake zero bytes at beginning of buffer, so that mq coder
134 // can be initialized to data[-1] == actualData[1], and still point
135 // to a valid memory location
136 auto buf = new uint8_t[desired_data_size + grk_cblk_enc_compressed_data_pad_left];
137 buf[0] = 0;
138 buf[1] = 0;
139
141 compressedStream.set_buf(buf, desired_data_size);
142 compressedStream.set_owns_data(true);
143
144 return true;
145 }
146 CodePass* getPass(uint8_t passno)
147 {
148 return passes + passno;
149 }
150 uint8_t getNumPasses(void) const
151 {
152 return totalPasses_;
153 }
154 void setNumPasses(uint8_t numPasses)
155 {
156 totalPasses_ = numPasses;
157 }
159 {
160 return passes + totalPasses_ - 1;
161 }
162 Layer* getLayer(uint16_t layno)
163 {
164 return layers + layno;
165 }
167 {
169 }
170 void setPaddedCompressedStream(uint8_t* stream)
171 {
172 paddedCompressedStream = stream;
173 }
175 {
177 }
178 void setNumPassesInPreviousLayers(uint8_t numPasses)
179 {
180 numPassesInPreviousPackets = numPasses;
181 }
182
183private:
188 uint8_t totalPasses_; /* total number of passes in all layers */
189#ifdef PLUGIN_DEBUG_ENCODE
190 uint32_t* context_stream;
191#endif
192};
193
194} // namespace grk::t1
const uint8_t grk_cblk_enc_compressed_data_pad_left
Definition CodeblockCompressImpl.h:22
Definition SchedulerFreebyrd.h:36
Information about compression/decompression coding pass.
Definition CodeblockCompressImpl.h:32
double distortiondec_
distortion decrease of pass
Definition CodeblockCompressImpl.h:48
CodePass()
Constructs a CodePass.
Definition CodeblockCompressImpl.h:36
~CodePass()=default
Destroys a CodePass.
uint16_t len_
length of pass in bytes
Definition CodeblockCompressImpl.h:52
uint16_t rate_
total rate of block up to and including this pass
Definition CodeblockCompressImpl.h:44
uint16_t slope_
ln(slope) in 8.8 fixed point
Definition CodeblockCompressImpl.h:62
bool term_
True if this pass terminates a segment, otherwise false.
Definition CodeblockCompressImpl.h:57
uint8_t * paddedCompressedStream
Definition CodeblockCompressImpl.h:184
bool allocData(size_t nominalBlockSize)
Allocates data memory for a compression code block.
Definition CodeblockCompressImpl.h:126
CodePass * passes
Definition CodeblockCompressImpl.h:186
uint8_t numPassesInPreviousPackets
Definition CodeblockCompressImpl.h:187
uint8_t * getPaddedCompressedStream(void)
Definition CodeblockCompressImpl.h:166
void setNumPassesInPreviousLayers(uint8_t numPasses)
Definition CodeblockCompressImpl.h:178
~CodeblockCompressImpl()
Definition CodeblockCompressImpl.h:107
uint8_t getNumPassesInPreviousLayers(void)
Definition CodeblockCompressImpl.h:174
uint8_t getNumPasses(void) const
Definition CodeblockCompressImpl.h:150
CodePass * getPass(uint8_t passno)
Definition CodeblockCompressImpl.h:146
CodeblockCompressImpl(uint16_t numLayers)
Definition CodeblockCompressImpl.h:99
CodePass * getLastPass(void)
Definition CodeblockCompressImpl.h:158
Layer * layers
Definition CodeblockCompressImpl.h:185
void setNumPasses(uint8_t numPasses)
Definition CodeblockCompressImpl.h:154
void init()
Definition CodeblockCompressImpl.h:112
void setPaddedCompressedStream(uint8_t *stream)
Definition CodeblockCompressImpl.h:170
uint8_t totalPasses_
Definition CodeblockCompressImpl.h:188
Layer * getLayer(uint16_t layno)
Definition CodeblockCompressImpl.h:162
uint16_t numLayers_
Definition CodeblockImpl.h:80
CodeblockImpl(uint16_t numLayers)
Definition CodeblockImpl.h:25
void init()
Definition CodeblockImpl.h:70
Buffer8 compressedStream
Definition CodeblockImpl.h:76
Store information on quality layer.
Definition CodeblockCompressImpl.h:70
double distortion
Layer distortion decrease.
Definition CodeblockCompressImpl.h:90
uint8_t totalPasses_
Number of passes in the layer.
Definition CodeblockCompressImpl.h:82
uint8_t * data
Compressed layer data.
Definition CodeblockCompressImpl.h:94
Layer()
Constructs a Layer.
Definition CodeblockCompressImpl.h:74
~Layer()=default
Destroys a Layer.
uint16_t len
Number of bytes in layer.
Definition CodeblockCompressImpl.h:86