Grok 20.3.2
PacketManager.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
20namespace grk
21{
26constexpr size_t GRK_INCLUDE_TRACKER_CHUNK_SIZE = 1024;
27
35{
41 {
42 for(uint8_t i = 0; i < GRK_MAXRLVLS; ++i)
43 chunkMap[i] = nullptr; // Buffers start as nullptr
44 }
45
51 {
52 clear();
53 }
54
64 bool update(uint8_t resno, uint64_t bitIndex)
65 {
66 if(resno >= GRK_MAXRLVLS)
67 throw std::out_of_range("Resolution index out of range");
68
69 if(!chunkMap[resno])
70 chunkMap[resno] = new std::unordered_map<size_t, uint8_t*>(); // Lazily allocate the map
71
72 auto& chunks = *chunkMap[resno];
73 uint64_t byteIndex = bitIndex >> 3; // Byte index within the resolution's buffer
74 uint64_t chunkIndex =
75 byteIndex / GRK_INCLUDE_TRACKER_CHUNK_SIZE; // Determine which chunk to access
76 uint64_t chunkOffset = byteIndex % GRK_INCLUDE_TRACKER_CHUNK_SIZE; // Offset within the chunk
77
78 // Lazily allocate the chunk
79 if(!chunks.contains(chunkIndex))
80 chunks[chunkIndex] = new uint8_t[GRK_INCLUDE_TRACKER_CHUNK_SIZE](); // Allocate chunk lazily
81
82 auto include = chunks[chunkIndex] + chunkOffset;
83 uint8_t bit = (bitIndex & 7);
84 uint8_t val = *include;
85 if(((val >> bit) & 1) == 0)
86 {
87 *include = (uint8_t)(val | (1 << bit));
88 return true;
89 }
90
91 return false;
92 }
93
98 void clear()
99 {
100 for(uint8_t i = 0; i < GRK_MAXRLVLS; ++i)
101 {
102 if(chunkMap[i])
103 {
104 for(auto& chunk : *chunkMap[i])
105 delete[] chunk.second; // Delete each chunk
106 delete chunkMap[i]; // Delete the map
107 chunkMap[i] = nullptr;
108 }
109 }
110 }
111
112private:
117 std::unordered_map<size_t, uint8_t*>* chunkMap[GRK_MAXRLVLS];
118};
119
121{
124 include(new std::unordered_map<uint16_t, LayerIncludeBuffers*>())
125 {
127 }
128
130 {
131 clear();
132 delete include;
133 }
134
135 bool update(uint16_t layno, uint8_t resno, uint16_t compno, uint64_t precno)
136 {
137 LayerIncludeBuffers* layerBuf = nullptr;
138
139 // Retrieve or create the ResIncludeBuffers for the current layer
141 {
142 layerBuf = currentLayerIncludeBuf;
143 }
144 else
145 {
146 if(include->find(layno) == include->end())
147 include->operator[](layno) = layerBuf = new LayerIncludeBuffers;
148 else
149 layerBuf = include->operator[](layno);
150 currentLayerIncludeBuf = layerBuf;
151 currentLayer = layno;
152 }
153
154 // Calculate the index in bits
155 auto numprecs = numPrecinctsPerRes[resno];
156 uint64_t bitIndex = compno * numprecs + precno;
157
158 return layerBuf->update(resno, bitIndex);
159 }
160
161 void clear()
162 {
163 for(auto it = include->begin(); it != include->end(); ++it)
164 delete it->second;
165 include->clear();
166 }
167
169 {
170 for(uint8_t i = 0; i < GRK_MAXRLVLS; ++i)
171 numPrecinctsPerRes[i] = 0;
172 }
173
174 void updateNumPrecinctsPerRes(uint8_t resno, uint64_t numPrecincts)
175 {
176 if(numPrecincts > numPrecinctsPerRes[resno])
177 numPrecinctsPerRes[resno] = numPrecincts;
178 }
179
180private:
182 uint16_t currentLayer;
184 std::unordered_map<uint16_t, LayerIncludeBuffers*>* include;
185};
186
188{
189public:
190 PacketManager(bool compression, GrkImage* img, CodingParams* cparams, uint16_t tilenumber,
191 T2_MODE t2_mode, ITileProcessor* tileProc);
192 virtual ~PacketManager();
193 PacketIter* getPacketIter(uint32_t poc) const;
201 void enable_tile_part_generation(uint32_t prog_iter_num, bool first_poc_tile_part,
202 uint8_t newTilePartProgressionPosition);
210 static void updateCompressParams(const GrkImage* image, CodingParams* p_cp, TileCodingParams* tcp,
211 uint16_t tile_no);
212
214 uint32_t getNumProgressions(void);
217 Rect32 getTileBounds(void);
219 T2_MODE getT2Mode(void);
220
221private:
236 static void updateCompressTcpProgressions(TileCodingParams* tcp, uint16_t num_comps,
237 Rect32 tileBounds, uint64_t max_precincts,
238 uint8_t max_res, uint32_t dx_min, uint32_t dy_min,
239 bool poc);
260 static void getParams(const GrkImage* image, CodingParams* p_cp, TileCodingParams* tcp,
261 uint16_t tileno, Rect32* tileBounds, uint32_t* dx_min, uint32_t* dy_min,
262 IncludeTracker* includeTracker, uint64_t* max_precincts, uint8_t* max_res,
263 uint32_t** componentPrecinctInfo);
266 uint16_t tileIndex_;
272};
273
274} // namespace grk
Stores header and data for an image.
Definition GrkImage.h:54
Rect32 getTileBounds(void)
Definition PacketManager.cpp:103
static void updateCompressTcpProgressions(TileCodingParams *tcp, uint16_t num_comps, Rect32 tileBounds, uint64_t max_precincts, uint8_t max_res, uint32_t dx_min, uint32_t dy_min, bool poc)
Updates the coding parameters.
Definition PacketManager.cpp:216
static void updateCompressParams(const GrkImage *image, CodingParams *p_cp, TileCodingParams *tcp, uint16_t tile_no)
Updates the compressing parameters of the codec.
Definition PacketManager.cpp:239
IncludeTracker * includeTracker_
Definition PacketManager.h:267
ITileProcessor * getTileProcessor(void)
Definition PacketManager.cpp:123
CodingParams * getCodingParams(void)
Definition PacketManager.cpp:107
T2_MODE getT2Mode(void)
Definition PacketManager.cpp:111
uint16_t tileIndex_
Definition PacketManager.h:266
GrkImage * getImage()
Definition PacketManager.cpp:99
IncludeTracker * getIncludeTracker(void)
Definition PacketManager.cpp:254
static void getParams(const GrkImage *image, CodingParams *p_cp, TileCodingParams *tcp, uint16_t tileno, Rect32 *tileBounds, uint32_t *dx_min, uint32_t *dy_min, IncludeTracker *includeTracker, uint64_t *max_precincts, uint8_t *max_res, uint32_t **componentPrecinctInfo)
Get the compression parameters needed to update the coding parameters and all the pocs.
Definition PacketManager.cpp:134
virtual ~PacketManager()
Definition PacketManager.cpp:90
void enable_tile_part_generation(uint32_t prog_iter_num, bool first_poc_tile_part, uint8_t newTilePartProgressionPosition)
Modify the packet iterator for enabling tile part generation.
Definition PacketManager.cpp:127
uint32_t getNumProgressions(void)
Definition PacketManager.cpp:115
CodingParams * cp_
Definition PacketManager.h:265
PacketIter * getPacketIter(uint32_t poc) const
Definition PacketManager.cpp:119
GrkImage * image_
Definition PacketManager.h:264
PacketManager(bool compression, GrkImage *img, CodingParams *cparams, uint16_t tilenumber, T2_MODE t2_mode, ITileProcessor *tileProc)
Definition PacketManager.cpp:50
Rect32 tileBounds_
Definition PacketManager.h:271
PacketIter * pi_
Definition PacketManager.h:268
T2_MODE t2Mode_
Definition PacketManager.h:269
ITileProcessor * tileProcessor_
Definition PacketManager.h:270
#define GRK_MAXRLVLS
Definition grok.h:336
ResWindow.
Definition CompressedChunkCache.h:36
T2_MODE
Definition PacketIter.h:26
Rect< uint32_t > Rect32
Definition geometry.h:64
constexpr size_t GRK_INCLUDE_TRACKER_CHUNK_SIZE
Chunk size for chunked resolution include buffer.
Definition PacketManager.h:26
Coding parameters.
Definition CodingParams.h:402
Interface for managing tile compression/decompression.
Definition ITileProcessor.h:37
Definition PacketManager.h:121
~IncludeTracker()
Definition PacketManager.h:129
IncludeTracker(void)
Definition PacketManager.h:122
void updateNumPrecinctsPerRes(uint8_t resno, uint64_t numPrecincts)
Definition PacketManager.h:174
std::unordered_map< uint16_t, LayerIncludeBuffers * > * include
Definition PacketManager.h:184
uint64_t numPrecinctsPerRes[GRK_MAXRLVLS]
Definition PacketManager.h:181
bool update(uint16_t layno, uint8_t resno, uint16_t compno, uint64_t precno)
Definition PacketManager.h:135
uint16_t currentLayer
Definition PacketManager.h:182
void clear()
Definition PacketManager.h:161
void resetNumPrecinctsPerRes(void)
Definition PacketManager.h:168
LayerIncludeBuffers * currentLayerIncludeBuf
Definition PacketManager.h:183
Include buffers for all resolutions in a given layer.
Definition PacketManager.h:35
LayerIncludeBuffers()
Construct a new LayerIncludeBuffers object.
Definition PacketManager.h:40
~LayerIncludeBuffers()
Destroy the LayerIncludeBuffers object.
Definition PacketManager.h:50
bool update(uint8_t resno, uint64_t bitIndex)
Get the byte object Lazily get or allocate a resolution's specific byte within its include buffer's m...
Definition PacketManager.h:64
std::unordered_map< size_t, uint8_t * > * chunkMap[GRK_MAXRLVLS]
Lazily allocated maps of chunks for each resolution.
Definition PacketManager.h:117
void clear()
Clears all chunks and chunkMaps.
Definition PacketManager.h:98
Iterates through JPEG 2000 packets following a progression order.
Definition PacketIter.h:240
Tile coding parameters : this structure is used to store coding/decoding parameters common to all til...
Definition CodingParams.h:124