Grok 20.3.2
PacketIter.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 <limits>
21
22namespace grk
23{
24
26{
29};
30
64{
66
83 bool init(uint8_t resno, uint8_t decompLevel, Rect32 tileBounds, uint32_t dx, uint32_t dy,
84 bool windowed, Rect32 tileWindow);
85
89 void print(void);
90
92 uint8_t precWidthExp;
99
105
107 uint64_t precWidthPRJ;
114
117
119 uint64_t dxPRJ;
121 uint64_t dyPRJ;
122
127
129 uint8_t resno_;
132
137
142
153
155 bool valid;
156};
157
182
204
205class PacketManager;
206
240{
244 PacketIter();
248 ~PacketIter();
249
261 void init(PacketManager* packetMan, uint32_t pocIndex, TileCodingParams* tcp, Rect32 tileBounds,
262 bool compression, uint8_t max_res, uint64_t max_precincts,
263 uint32_t** componentPrecinctInfo);
264
268 void printStaticState(void);
269
273 void printDynamicState(void);
274
282 void enable_tile_part_generation(uint32_t prog_iter_num, bool first_poc_tile_part,
283 uint8_t newTilePartProgressionPosition);
284
289 bool genPrecinctInfoOPT();
290
294 void genPrecinctInfo();
295
303 uint8_t resNumber);
304
311 uint8_t* get_include(uint16_t layerIndex);
312
317 bool update_include(void);
318
322 void destroy_include(void);
323
330 bool next(SparseBuffer* compressedPackets);
331
336 GRK_PROG_ORDER getProgression(void) const;
337
342 uint16_t getCompno(void) const;
343
348 uint8_t getResno(void) const;
349
354 uint64_t getPrecinctIndex(void) const;
355
360 uint16_t getLayno(void) const;
361
362private:
364 uint16_t compno = 0;
366 uint8_t resno = 0;
368 uint64_t precinctIndex = 0;
370 uint16_t layno = 0;
374 uint16_t numcomps = 0;
377
379 uint64_t x = 0;
381 uint64_t y = 0;
387 uint32_t dx = 0;
392 uint32_t dy = 0;
394 uint32_t dxActive = 0;
396 uint32_t dyActive = 0;
397
401 void update_dxy(void);
402
406 bool checkForRemainingValidProgression(int32_t prog, uint32_t prog_iter_num,
407 const char* progString);
408 // This packet iterator is designed so that the innermost progression
409 // is only incremented before the **next** packet is processed.
410 // i.e. it is not incremented before the very first packet is processed,
411 // but rather before all subsequent packets are processed.
412 // This flag keeps track of this state.
413 bool incrementInner = false;
414
420 bool singleProgression_ = false;
422 bool compression_ = false;
428
430 uint32_t px0grid_ = 0;
432 uint32_t py0grid_ = 0;
433
435 bool skippedLeft_ = false;
436
465 bool precInfoCheck(ResPrecinctInfo* rpInfo);
467 void generatePrecinctIndex(void);
472 bool validatePrecinct(void);
478 void update_dxy_for_comp(PacketIterInfoComponent* comp, bool updateActive);
482 bool isWholeTile(void);
483
488 bool next_cprl(SparseBuffer*);
489 bool next_cprlOPT(SparseBuffer* compressedPackets);
490
495 bool next_pcrl();
496 bool next_pcrlOPT();
497
502 bool next_lrcp();
503 bool next_lrcpOPT();
508 bool next_rlcp();
509 bool next_rlcpOPT();
514 bool next_rpcl(SparseBuffer*);
515 bool next_rpclOPT(SparseBuffer* compressedPackets);
516
517 bool skipPackets(SparseBuffer* compressedPackets, uint64_t numPackets);
518};
519
520} // namespace grk
Definition PacketManager.h:188
Manages a list of buffers (named chunks) which can be treated as one single contiguous buffer.
Definition SparseBuffer.h:41
enum _GRK_PROG_ORDER GRK_PROG_ORDER
Environment variables.
ResWindow.
Definition CompressedChunkCache.h:36
T2_MODE
Definition PacketIter.h:26
@ FINAL_PASS
Function called in rate allocation process.
Definition PacketIter.h:28
@ THRESH_CALC
Definition PacketIter.h:27
Rect< uint32_t > Rect32
Definition geometry.h:64
void enable_tile_part_generation(uint32_t prog_iter_num, bool first_poc_tile_part, uint8_t newTilePartProgressionPosition)
Modifies the packet iterator to enable tile part generation.
Definition PacketIter.cpp:444
bool isWholeTile(void)
Returns true if the full tile is being decoded (no windowed sub-region).
Definition PacketIter.cpp:988
bool next_cprl(SparseBuffer *)
Get next packet in component-precinct-resolution-layer order.
Definition PacketIter.cpp:1043
void destroy_include(void)
Clears all include states.
Definition PacketIter.cpp:774
bool genPrecinctY0GridPCRL_OPT(ResPrecinctInfo *rpInfo)
Computes py0grid_ for PCRL/CPRL OPT path.
Definition PacketIter.cpp:812
bool next_rpcl(SparseBuffer *)
Get next packet in resolution-precinct-component-layer order.
Definition PacketIter.cpp:1188
uint8_t * get_include(uint16_t layerIndex)
Gets point to packet include information i.e.
uint16_t layno
current layer number in iteration
Definition PacketIter.h:370
uint16_t getCompno(void) const
Gets component number for iterator's current packet.
Definition PacketIter.cpp:752
ResPrecinctInfo * precinctInfoOPT_
Cached per-resolution precinct info for the optimized iteration path.
Definition PacketIter.h:427
bool next_lrcp()
Get next packet in layer-resolution-component-precinct order.
Definition PacketIter.cpp:1126
uint32_t dy
Minimum vertical step size across all components/resolutions for spatial progressions.
Definition PacketIter.h:392
uint16_t compno
current component number in iteration
Definition PacketIter.h:364
bool next_rpclOPT(SparseBuffer *compressedPackets)
Definition PacketIter.cpp:1451
bool update_include(void)
Updates include state for current packet.
Definition PacketIter.cpp:768
bool next_pcrlOPT()
Definition PacketIter.cpp:1392
void init(PacketManager *packetMan, uint32_t pocIndex, TileCodingParams *tcp, Rect32 tileBounds, bool compression, uint8_t max_res, uint64_t max_precincts, uint32_t **componentPrecinctInfo)
Initializes PacketItr.
Definition PacketIter.cpp:883
PacketIterInfoComponent * comps
per-component precinct grid info, length = numcomps
Definition PacketIter.h:376
uint32_t dxActive
active horizontal step (accounts for alignment to current x position)
Definition PacketIter.h:394
bool skippedLeft_
RPCL OPT: tracks whether left-of-window precincts have been skipped for current row.
Definition PacketIter.h:435
bool next_pcrl()
Get next packet in precinct-component-resolution-layer order.
Definition PacketIter.cpp:1083
grk_progression prog
progression order and iteration bounds
Definition PacketIter.h:372
PacketManager * packetManager
owning packet manager
Definition PacketIter.h:416
bool next_lrcpOPT()
Definition PacketIter.cpp:1257
bool genPrecinctX0Grid(ResPrecinctInfo *rpInfo)
Computes px0grid_ for the current (x,y) position (non-OPT path).
Definition PacketIter.cpp:800
void generatePrecinctIndex(void)
Computes precinctIndex from (px0grid_, py0grid_) and precinct grid width.
Definition PacketIter.cpp:376
uint32_t dx
Minimum horizontal step size across all components/resolutions for spatial progressions (PCRL,...
Definition PacketIter.h:387
bool genPrecinctX0GridPCRL_OPT(ResPrecinctInfo *rpInfo)
Computes px0grid_ for PCRL/CPRL OPT path.
Definition PacketIter.cpp:821
uint8_t maxNumDecompositionResolutions
max number of decomposition resolutions to decompress
Definition PacketIter.h:418
bool checkForRemainingValidProgression(int32_t prog, uint32_t prog_iter_num, const char *progString)
Checks if there is a remaining valid progression for tile part generation.
Definition PacketIter.cpp:386
bool skipPackets(SparseBuffer *compressedPackets, uint64_t numPackets)
Definition PacketIter.cpp:1243
uint8_t getResno(void) const
Gets resolution number for iterator's current packet.
Definition PacketIter.cpp:756
uint64_t y
current y position in spatial progression (projected coordinates)
Definition PacketIter.h:381
uint32_t px0grid_
precinct grid x-coordinate of current precinct's top-left corner
Definition PacketIter.h:430
void printStaticState(void)
Prints static debug state of iterator.
Definition PacketIter.cpp:172
uint16_t getLayno(void) const
Gets layer number for iterator's current packet.
Definition PacketIter.cpp:764
uint64_t precinctIndex
current precinct index in iteration
Definition PacketIter.h:368
uint32_t dyActive
active vertical step (accounts for alignment to current y position)
Definition PacketIter.h:396
bool next_rlcp()
Get next packet in resolution-layer-component-precinct order.
Definition PacketIter.cpp:1157
uint8_t resno
current resolution number in iteration
Definition PacketIter.h:366
void update_dxy_for_comp(PacketIterInfoComponent *comp, bool updateActive)
Computes dx/dy for a single component and merges into the global minimum.
Definition PacketIter.cpp:861
void printDynamicState(void)
Prints dynamic debug state of the iterator.
Definition PacketIter.cpp:186
void genPrecinctInfo()
Generates non-optimized precinct information.
Definition PacketIter.cpp:200
uint64_t getPrecinctIndex(void) const
Gets precinct index for iterator's current packet.
Definition PacketIter.cpp:760
bool singleProgression_
true if there is exactly one progression (no POC markers)
Definition PacketIter.h:420
bool genPrecinctY0Grid(ResPrecinctInfo *rpInfo)
Computes py0grid_ for the current (x,y) position (non-OPT path).
Definition PacketIter.cpp:789
void genPrecinctY0GridRPCL_OPT(ResPrecinctInfo *rpInfo)
Computes py0grid_ for RPCL OPT path (tile origin at (0,0), no subsampling).
Definition PacketIter.cpp:830
bool next_rlcpOPT()
Definition PacketIter.cpp:1287
bool genPrecinctInfoOPT()
Generates optimized precinct information.
Definition PacketIter.cpp:262
bool next(SparseBuffer *compressedPackets)
Moves to next packet.
Definition PacketIter.cpp:993
GRK_PROG_ORDER getProgression(void) const
Gets current progression order.
Definition PacketIter.cpp:748
bool compression_
true when used for compression, false for decompression
Definition PacketIter.h:422
void genPrecinctX0GridRPCL_OPT(ResPrecinctInfo *rpInfo)
Computes px0grid_ for RPCL OPT path.
Definition PacketIter.cpp:835
PacketIter()
Constructs PacketIter.
Definition PacketIter.cpp:146
bool next_cprlOPT(SparseBuffer *compressedPackets)
Definition PacketIter.cpp:1318
uint32_t py0grid_
precinct grid y-coordinate of current precinct's top-left corner
Definition PacketIter.h:432
void update_dxy(void)
Computes dx/dy step sizes for all components.
Definition PacketIter.cpp:852
uint16_t numcomps
number of image components
Definition PacketIter.h:374
~PacketIter()
Destroys PacketIter.
Definition PacketIter.cpp:167
bool incrementInner
Definition PacketIter.h:413
bool precInfoCheck(ResPrecinctInfo *rpInfo)
Validates that the current (compno, resno, x, y) maps to a valid precinct.
Definition PacketIter.cpp:778
uint64_t x
current x position in spatial progression (projected coordinates)
Definition PacketIter.h:379
bool validatePrecinct(void)
Validates the current precinct and computes grid coordinates.
Definition PacketIter.cpp:326
Component-level information for packet iteration.
Definition PacketIter.h:191
PacketIterInfoComponent()
Definition PacketIter.cpp:159
uint32_t dy
component vertical subsampling factor (from SIZ marker)
Definition PacketIter.h:198
uint8_t numresolutions
number of resolution levels for this component (1 to GRK_MAXRLVLS)
Definition PacketIter.h:200
PacketIterInfoResolution * resolutions
array of resolution-level precinct grid info, length = numresolutions
Definition PacketIter.h:202
~PacketIterInfoComponent()
Definition PacketIter.cpp:162
uint32_t dx
component horizontal subsampling factor (from SIZ marker)
Definition PacketIter.h:196
Resolution-level precinct grid geometry for packet iteration.
Definition PacketIter.h:167
uint8_t precWidthExp
log2 precinct width (from codestream SPcoc marker, 0-15)
Definition PacketIter.h:172
uint32_t precinctGridWidth
number of precincts horizontally in this resolution's precinct grid
Definition PacketIter.h:176
~PacketIterInfoResolution()
Definition PacketIter.cpp:155
ResPrecinctInfo * precinctInfo
fully computed precinct info (non-OPT path only; null when precinctInfoOPT_ is used)
Definition PacketIter.h:180
PacketIterInfoResolution()
Definition PacketIter.cpp:151
uint32_t precinctGridHeight
number of precincts vertically in this resolution's precinct grid
Definition PacketIter.h:178
uint8_t precHeightExp
log2 precinct height (from codestream SPcoc marker, 0-15)
Definition PacketIter.h:174
Definition geometry.h:174
Cached geometric state of a tile component's resolution, relative to its precinct grid.
Definition PacketIter.h:64
uint32_t resOffsetY0PRJ
vertical offset of resolution origin relative to projected precinct grid
Definition PacketIter.h:104
Rect< uint64_t > winPrecPRJ
decode window bounds snapped to precinct boundaries, in projected space
Definition PacketIter.h:139
uint64_t dxPRJ
component subsampling projected to resolution: compDx << decompLevel
Definition PacketIter.h:119
uint8_t resno_
resolution number (0 = lowest)
Definition PacketIter.h:129
uint32_t resInPrecGridX0
floor(res.x0 / 2^precWidthExp): resolution origin in precinct grid coords
Definition PacketIter.h:124
uint64_t precHeightPRJ
precinct height in projected space: compDy << precHeightExpPRJ
Definition PacketIter.h:111
bool init(uint8_t resno, uint8_t decompLevel, Rect32 tileBounds, uint32_t dx, uint32_t dy, bool windowed, Rect32 tileWindow)
Initializes resolution precinct info for a specific resolution level.
Definition PacketIter.cpp:70
uint64_t dyPRJ
component subsampling projected to resolution: compDy << decompLevel
Definition PacketIter.h:121
uint64_t precWidthPRJMinusOne
precWidthPRJ - 1 (cached for bitwise alignment checks)
Definition PacketIter.h:109
uint64_t winPrecinctsTop_
packets in precinct rows above the decode window
Definition PacketIter.h:150
uint64_t precHeightPRJMinusOne
precHeightPRJ - 1 (cached for bitwise alignment checks)
Definition PacketIter.h:113
uint32_t resInPrecGridY0
floor(res.y0 / 2^precHeightExp): resolution origin in precinct grid coords
Definition PacketIter.h:126
uint8_t precHeightExpPRJ
log2 precinct height in projected space: precHeightExp + decompLevel
Definition PacketIter.h:98
uint8_t precWidthExpPRJ
log2 precinct width in projected space: precWidthExp + decompLevel
Definition PacketIter.h:96
uint64_t innerPrecincts_
cached packet count: comp_e * lay_e (for RPCL skip optimization)
Definition PacketIter.h:144
uint64_t winPrecinctsRight_
packets in precinct columns to the right of the decode window
Definition PacketIter.h:148
bool valid
true if init() completed successfully
Definition PacketIter.h:155
uint64_t numPrecincts_
total number of precincts in this resolution (= tileBoundsPrecGrid.area())
Definition PacketIter.h:116
uint64_t winPrecinctsLeft_
packets in precinct columns to the left of the decode window
Definition PacketIter.h:146
uint8_t precWidthExp
log2 precinct width in resolution space (0-15, from codestream SPcoc)
Definition PacketIter.h:92
Rect32 winPrecGrid
decode window bounds mapped to precinct grid (resolution space)
Definition PacketIter.h:141
ResPrecinctInfo()
Definition PacketIter.cpp:63
Rect< uint64_t > tileBoundsPrecPRJ
tile bounds snapped to precinct boundaries, in projected space
Definition PacketIter.h:134
uint32_t resOffsetX0PRJ
horizontal offset of resolution origin relative to projected precinct grid (zero when tile origin is ...
Definition PacketIter.h:102
uint8_t precHeightExp
log2 precinct height in resolution space
Definition PacketIter.h:94
uint64_t winPrecinctsBottom_
packets in precinct rows below the decode window
Definition PacketIter.h:152
uint64_t precWidthPRJ
precinct width in projected space: compDx << precWidthExpPRJ
Definition PacketIter.h:107
Rect32 tileBoundsPrecGrid
tile bounds mapped to precinct grid (resolution space)
Definition PacketIter.h:136
void print(void)
Prints resolution precinct info for debugging.
Definition PacketIter.cpp:127
uint8_t decompLevel_
decomposition level (numresolutions - 1 - resno)
Definition PacketIter.h:131
Tile coding parameters : this structure is used to store coding/decoding parameters common to all til...
Definition CodingParams.h:124
Progression order change (POC).