Grok 20.3.2
TileProcessor.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 <mutex>
21#include <memory>
22
23#include "PacketTracker.h"
24#include "ITileProcessor.h"
25
26namespace grk
27{
28
30
35struct TileProcessor : virtual public ITileProcessor
36{
46 TileProcessor(uint16_t index, TileCodingParams* tcp, CodeStream* codeStream, IStream* stream,
47 bool isCompressor, uint32_t tileCacheStrategy);
48
52 virtual ~TileProcessor() override;
53
54 void setProcessors(MarkerParser* parser) override;
55
59 virtual bool init(void) override;
60
61 void setStream(IStream* stream, bool ownsStream) override;
62
63 bool decompressWithTLM(const std::shared_ptr<TPFetchSeq>& tilePartFetchSeq, CoderPool* streamPool,
64 Rect32 unreducedImageBounds, std::function<void()> post,
65 TileFutureManager& futures) override;
66
67 bool decompressPrepareWithTLM(const std::shared_ptr<TPFetchSeq>& tilePartFetchSeq) override;
68
73 bool doPostT1(void) override;
74
81 void prepareForDecompression(void) override;
82
88 bool parseTilePart(std::vector<std::unique_ptr<MarkerParser>>* parsers, IStream* bifurcatedStream,
89 uint16_t mainMarkerId, TilePartInfo tilePartInfo) override;
90
98 bool decompressFromCachedTileParts() override;
99
109 bool readSOT(IStream* stream, uint8_t* headerData, uint16_t headerSize,
110 TilePartInfo& tilePartInfo, bool needToReadIndexAndLength) override;
111
120 void scheduleAndRunDecompress(CoderPool* coderPool, Rect32 unreducedImageBounds,
121 std::function<void()> post, TileFutureManager& futures) override;
127 void post_decompressT2T1(GrkImage* scratch) override;
128
136 bool differentialUpdate(Rect32 unreducedImageBounds) override;
137
143 GrkImage* getImage(void) override;
144
150 void setImage(GrkImage* img) override;
151
157 Rect32 getUnreducedTileWindow(void) override;
158
164 TileCodingParams* getTCP(void) override;
165
171 uint8_t getMaxNumDecompressResolutions(void) override;
172
178 IStream* getStream(void) override;
179
185 uint16_t getIndex(void) const override;
186
191 void incrementIndex(void) override;
192
198 Tile* getTile(void) override;
199
201
207 CodecScheduler* getScheduler(void) override;
208
215 bool isCompressor(void) override;
216
222 uint64_t getNumProcessedPackets(void) override;
223
228 void incNumProcessedPackets(void) override;
229
235 void incNumProcessedPackets(uint64_t numPackets) override;
236
241 void incNumReadDataPackets(void) override;
242
248 uint32_t getTileCacheStrategy(void) override;
249
255 grk_plugin_tile* getCurrentPluginTile(void) const override;
256
262 void setCurrentPluginTile(grk_plugin_tile* tile) override;
263
269 CodingParams* getCodingParams(void) override;
270
276 GrkImage* getHeaderImage(void) override;
277
283 std::shared_ptr<PacketLengthCache<uint32_t>> getPacketLengthCache(void) override;
284
292 bool needsMctDecompress(uint16_t compno) override;
293
300 bool needsMctDecompress(void) override;
301
302 bool shouldDecodeComponent(uint16_t compno) override;
303
309 Mct* getMCT(void) override;
310
315 void release(void) override;
316
322 void release(uint32_t strategy) override;
323 void releaseForSwath() override;
324
331 bool readPLT(uint8_t* headerData, uint16_t headerSize) override;
332
340 bool allSOTMarkersParsed() override;
341 bool hasUnparsedTileParts() override;
342
348 void setTruncated(void) override;
349
350 bool hasError(void) override;
351
352 bool isInitialized(void) override;
353
354 bool isBestEffortDecompressed(void) override;
355 void setBestEffortDecompressed(void) override;
356 bool scheduledForDecompression(void) override;
357 void resetSOTParsing() override;
358 bool reinitForReDecompress(void) override;
359 bool isStripOutputWritten() const override
360 {
361 return stripOutputWritten_;
362 }
363
364protected:
370
376
381 CodingParams* cp_ = nullptr;
382
387 std::shared_ptr<PacketLengthCache<uint32_t>> packetLengthCache_;
388
393 Tile* tile_ = nullptr;
394
396 uint16_t tileIndex_ = 0;
397
403
408 IStream* stream_ = nullptr;
409
414 Mct* mct_ = nullptr;
415
421
427
428private:
429 std::vector<tf::Task> blockTasks_;
430
431 bool initialized_ = false;
432
433 void prepareConcurrentParsing(void);
434
435 std::atomic<bool> success_ = true;
437
438 // Flow components for task graph scheduling.
439 // rootFlow_ owns the composed DAG submitted to the executor.
440 // tileHeaderParseFlow_/prepareFlow_ are populated during parsing and may
441 // outlive a single submission cycle, so stale copies are kept alive until
442 // the previous executor run completes (via staleParsing_).
443 std::unique_ptr<FlowComponent> rootFlow_;
444 std::unique_ptr<FlowComponent> tileHeaderParseFlow_;
445 std::unique_ptr<FlowComponent> prepareFlow_;
446 std::vector<std::unique_ptr<FlowComponent>> staleParsing_;
447 std::unique_ptr<FlowComponent> t2ParseFlow_;
448 std::unique_ptr<FlowComponent> allocAndScheduleFlow_;
449 std::unique_ptr<FlowComponent> postDecompressFlow_;
450
455 void deallocBuffers();
456
463
469
475
480 std::atomic<uint64_t> numReadDataPackets_ = 0;
481
483 uint64_t startPos_ = 0;
484
486 uint8_t numSOTsParsed_ = 0;
487
492 bool truncated_ = false;
493
500
505 GrkImage* image_ = nullptr;
506
512
518
523 uint32_t tileCacheStrategy_ = 0;
524
526
527 std::shared_ptr<TPFetchSeq> tilePartFetchSeq_;
529
530 std::vector<uint8_t> threadTilePart_;
531 std::mutex pltMutex_;
532
533 // band callback for strip-mode ImageFormat output
535 void* ioBandUserData_ = nullptr;
537};
538
539} // namespace grk
Definition TileFutureManager.h:29
Definition CodeStream.h:108
An abstract class that can execute T1 phase of codec by running a tf::Executor.
Definition CodecScheduler.h:72
Stores header and data for an image.
Definition GrkImage.h:54
Definition mct.h:48
Stub — freebyrd thread pool has been removed.
Definition SchedulerFreebyrd.h:48
bool(* grk_io_band_callback)(uint32_t yBegin, uint32_t yEnd, grk_image *image, void *user_data)
Callback invoked when a horizontal band of the decompressed image is ready for writing.
Definition grok.h:647
ResWindow.
Definition CompressedChunkCache.h:36
Rect< uint32_t > Rect32
Definition geometry.h:64
Definition CoderPool.h:53
Coding parameters.
Definition CodingParams.h:402
Definition IStream.h:60
Interface for managing tile compression/decompression.
Definition ITileProcessor.h:37
Definition MarkerParser.h:61
Sequence of shared_ptr<DataSlice> tile parts parsed from either TLM or SOT marker.
Definition TPFetchSeq.h:114
Tile coding parameters : this structure is used to store coding/decoding parameters common to all til...
Definition CodingParams.h:124
Definition Tile.h:39
Definition ITileProcessor.h:24
bool readPLT(uint8_t *headerData, uint16_t headerSize) override
Reads a PLT marker (Packet length, tile-part header).
Definition TileProcessor.cpp:967
GrkImage * getImage(void) override
Gets the tile GrkImage.
Definition TileProcessor.cpp:1086
bool shouldDecodeComponent(uint16_t compno) override
Check if a component should be decoded based on user selection.
Definition TileProcessor.cpp:1640
grk_io_band_callback ioBandCallback_
Definition TileProcessor.h:534
std::unique_ptr< FlowComponent > rootFlow_
Definition TileProcessor.h:443
grk_plugin_tile * getCurrentPluginTile(void) const override
Gets the Current Plugin Tile object.
Definition TileProcessor.cpp:1016
bool decompressFromCachedTileParts() override
Parse tile-part packets using cached SOT offsets from tilePartSeq_, without re-reading SOT markers.
Definition TileProcessor.cpp:514
uint16_t getIndex(void) const override
Get the Index object.
Definition TileProcessor.cpp:1066
GrkImage * image_
GrkImage for this tile
Definition TileProcessor.h:505
uint64_t getNumProcessedPackets(void) override
Get the Num Processed Packets object.
Definition TileProcessor.cpp:1024
Tile * getTile(void) override
Get the Tile object.
Definition TileProcessor.cpp:1074
std::vector< uint8_t > threadTilePart_
Definition TileProcessor.h:530
uint8_t getMaxNumDecompressResolutions(void) override
Get the Max Num Decompress Resolutions object.
Definition TileProcessor.cpp:1590
void * ioBandUserData_
Definition TileProcessor.h:535
bool hasUnparsedTileParts() override
True when we know for certain that tile parts are missing.
Definition TileProcessor.cpp:833
uint8_t numSOTsParsed_
number of SOT markers parsed
Definition TileProcessor.h:486
std::mutex pltMutex_
Definition TileProcessor.h:531
bool doPostT1(void) override
Performs post T1 processing.
Definition TileProcessor.cpp:1099
void setImage(GrkImage *img) override
Gets the tile GrkImage.
Definition TileProcessor.cpp:1090
bool truncated_
true if one of this tile's tile parts is truncated
Definition TileProcessor.h:492
bool stripOutputWritten_
Definition TileProcessor.h:536
void incNumProcessedPackets(void) override
Increments the number of processed packets by 1.
Definition TileProcessor.cpp:1028
CodecScheduler * getScheduler(void) override
Get the Scheduler object.
Definition TileProcessor.cpp:1078
bool isInitialized(void) override
Checks if the processor is initialized.
Definition TileProcessor.cpp:253
TileProcessor(uint16_t index, TileCodingParams *tcp, CodeStream *codeStream, IStream *stream, bool isCompressor, uint32_t tileCacheStrategy)
Constructs a TileProcessor.
Definition TileProcessor.cpp:79
uint64_t numProcessedPackets_
number of packets processed
Definition TileProcessor.h:474
Tile * tile_
Tile
Definition TileProcessor.h:393
bool isCompressor(void) override
Definition TileProcessor.cpp:1082
void setBestEffortDecompressed(void) override
Definition TileProcessor.cpp:263
grk_plugin_tile * current_plugin_tile_
grk_plugin_tile
Definition TileProcessor.h:375
TileCodingParams * getTCP(void) override
Definition TileProcessor.cpp:1047
Rect32 unreducedImageWindow_
unreduced image window
Definition TileProcessor.h:517
std::unique_ptr< FlowComponent > allocAndScheduleFlow_
Definition TileProcessor.h:448
bool scheduledForDecompression(void) override
Definition TileProcessor.cpp:268
bool needsMctDecompress(void) override
Definition TileProcessor.cpp:1617
bool scheduledForDecompression_
Definition TileProcessor.h:499
Rect32 getUnreducedTileWindow(void) override
Get the Unreduced Tile Window object.
Definition TileProcessor.cpp:1607
bool hasError(void) override
Checks if an error has occurred.
Definition TileProcessor.cpp:1011
void decompress_synch_plugin_with_host(void)
Definition TileProcessor.cpp:168
void deallocBuffers()
deallocate buffers
Definition TileProcessor.cpp:945
std::shared_ptr< PacketLengthCache< uint32_t > > packetLengthCache_
PacketLengthCache
Definition TileProcessor.h:387
void incNumReadDataPackets(void) override
Increments the number of read data packets by 1.
Definition TileProcessor.cpp:1612
grk_progression_state getProgressionState() override
Gets the progression state.
Definition TileProcessor.cpp:238
bool readSOT(IStream *stream, uint8_t *headerData, uint16_t headerSize, TilePartInfo &tilePartInfo, bool needToReadIndexAndLength) override
Reads SOT marker.
Definition TileProcessor.cpp:551
TPSeq tilePartSeq_
Definition TileProcessor.h:528
GrkImage * headerImage_
header GrkImage
Definition TileProcessor.h:369
bool differentialUpdate(Rect32 unreducedImageBounds) override
Updates differential decompress state.
Definition TileProcessor.cpp:954
std::vector< std::unique_ptr< FlowComponent > > staleParsing_
Definition TileProcessor.h:446
void prepareConcurrentParsing(void)
Definition TileProcessor.cpp:839
void setCurrentPluginTile(grk_plugin_tile *tile) override
Set the Current Plugin Tile object.
Definition TileProcessor.cpp:1020
void prepareForDecompression(void) override
Prepares for decompression.
Definition TileProcessor.cpp:863
MarkerParser * markerParser_
MarkerParser
Definition TileProcessor.h:468
std::unique_ptr< FlowComponent > t2ParseFlow_
Definition TileProcessor.h:447
bool isCompressor_
true if tile will be compressed
Definition TileProcessor.h:511
CodecScheduler * scheduler_
CodecScheduler
Definition TileProcessor.h:420
bool initialized_
Definition TileProcessor.h:431
std::atomic< bool > success_
Definition TileProcessor.h:435
CodingParams * getCodingParams(void) override
Get the Coding Params object.
Definition TileProcessor.cpp:1037
std::shared_ptr< PacketLengthCache< uint32_t > > getPacketLengthCache(void) override
Get the Packet Length Cache object.
Definition TileProcessor.cpp:1052
void setStream(IStream *stream, bool ownsStream) override
Sets the stream for input/output operations.
Definition TileProcessor.cpp:415
uint32_t tileCacheStrategy_
tile cache strategy
Definition TileProcessor.h:523
IStream * getStream(void) override
Get the Stream object.
Definition TileProcessor.cpp:1062
void release(void) override
Releases resources - image and tile.
Definition TileProcessor.cpp:941
bool bestEffortDecompressed_
true if tile was decompressed on a best-effort basis (may have been truncated or errored).
Definition TileProcessor.h:498
void setTruncated(void) override
Sets processor to truncated if not all tile parts have been parsed.
Definition TileProcessor.cpp:822
void setProcessors(MarkerParser *parser) override
Sets the marker parser for processing.
Definition TileProcessor.cpp:113
void resetSOTParsing() override
Reset SOT parsing state so the tile can be re-parsed from the codestream.
Definition TileProcessor.cpp:273
bool concurrentFlowsStale_
Definition TileProcessor.h:436
bool decompressWithTLM(const std::shared_ptr< TPFetchSeq > &tilePartFetchSeq, CoderPool *streamPool, Rect32 unreducedImageBounds, std::function< void()> post, TileFutureManager &futures) override
Decompresses the tile using Tile Length Markers (TLM).
Definition TileProcessor.cpp:502
bool createDecompressTileComponentWindows(void)
Create a Tile Window Buffers object.
Definition TileProcessor.cpp:982
bool parseTilePart(std::vector< std::unique_ptr< MarkerParser > > *parsers, IStream *bifurcatedStream, uint16_t mainMarkerId, TilePartInfo tilePartInfo) override
Parses tile part.
Definition TileProcessor.cpp:673
bool decompressPrepareWithTLM(const std::shared_ptr< TPFetchSeq > &tilePartFetchSeq) override
Prepares for decompression using Tile Length Markers (TLM).
Definition TileProcessor.cpp:423
void scheduleAndRunDecompress(CoderPool *coderPool, Rect32 unreducedImageBounds, std::function< void()> post, TileFutureManager &futures) override
Schedule T2/T1 decompression.
Definition TileProcessor.cpp:1131
bool allSOTMarkersParsed() override
Checks if tile is completely parsed.
Definition TileProcessor.cpp:828
uint64_t startPos_
Definition TileProcessor.h:483
std::unique_ptr< FlowComponent > prepareFlow_
Definition TileProcessor.h:445
GrkImage * getHeaderImage(void) override
Get the Header Image object.
Definition TileProcessor.cpp:1042
virtual ~TileProcessor() override
Destroys a TileProcessor.
Definition TileProcessor.cpp:102
std::shared_ptr< TPFetchSeq > tilePartFetchSeq_
Definition TileProcessor.h:527
std::unique_ptr< FlowComponent > tileHeaderParseFlow_
Definition TileProcessor.h:444
bool reinitForReDecompress(void) override
Reinitialize for re-decompression after LRU eviction.
Definition TileProcessor.cpp:281
std::atomic< uint64_t > numReadDataPackets_
number of data packets read
Definition TileProcessor.h:480
IStream * stream_
IStream
Definition TileProcessor.h:408
uint32_t getTileCacheStrategy(void) override
Gets the Tile Cache Strategy object.
Definition TileProcessor.cpp:1057
virtual bool init(void) override
Initializes a TileProcessor.
Definition TileProcessor.cpp:305
bool isStripOutputWritten() const override
Check if strip-mode output was written directly (bypass multi-tile band callback).
Definition TileProcessor.h:359
void incrementIndex(void) override
Increments the tile index.
Definition TileProcessor.cpp:1070
TilePartInfo tilePartInfo_
Definition TileProcessor.h:482
TileCodingParams * tcp_
TileCodingParams
Definition TileProcessor.h:402
SchedulerFreebyrd * schedulerFreebyrd_
SchedulerFreebyrd — strip-based decompression via freebyrd pool
Definition TileProcessor.h:426
std::vector< tf::Task > blockTasks_
Definition TileProcessor.h:429
std::unique_ptr< FlowComponent > postDecompressFlow_
Definition TileProcessor.h:449
uint16_t tileIndex_
index of tile being currently compressed/decompressed
Definition TileProcessor.h:396
Mct * mct_
MCT
Definition TileProcessor.h:414
void releaseForSwath() override
Unconditionally releases both image and tile data (swath consumer path).
Definition TileProcessor.cpp:928
CodingParams * cp_
CodingParams
Definition TileProcessor.h:381
Mct * getMCT(void) override
gets Mct
Definition TileProcessor.cpp:898
void post_decompressT2T1(GrkImage *scratch) override
Performs post T2+T1 processing.
Definition TileProcessor.cpp:1104
bool isBestEffortDecompressed(void) override
Check if tile was decompressed on a best-effort basis (may have been truncated or errored).
Definition TileProcessor.cpp:258
Plugin tile.
Stores progression state information Note: limited to 256 components.