Grok 20.3.2
TLMMarker.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#include <memory>
20#include <vector>
21#include <span> // C++20
22
23namespace grk
24{
25
30template<typename T>
32{
38 TilePartLength(uint16_t tileIndex, T len) : tileIndex_(tileIndex), length_(len) {}
39
40 uint16_t tileIndex_;
41
43};
44
51{
56 using TL_VEC = std::vector<TilePartLength<uint32_t>>;
57
58public:
60 : tilePartLengthsIter_(), // Default-initialized iterator (invalid until tilePartLengths_
61 // exists)
62 lastMarkerId_(-1), valid_(true)
63 {}
64
70 {
71 if(!valid_)
72 return;
73
74 // push tile part length
76 {
77 tilePartLengths_ = std::make_unique<TL_VEC>();
79 tilePartLengths_->begin(); // Set to begin (empty vector’s begin == end)
80 }
81 tilePartLengths_->push_back(tilePartLength);
82 }
83
87 void reset() noexcept
88 {
91 }
92
98 TilePartLength<uint32_t>* next(bool peek = false) noexcept
99 {
101 return nullptr;
102
103 auto result = &(*tilePartLengthsIter_);
104 if(!peek)
106
107 return result;
108 }
109
114 [[nodiscard]] bool empty() const noexcept
115 {
116 return !tilePartLengths_ || tilePartLengths_->empty();
117 }
118
124 [[nodiscard]] bool validateMarkerId(uint8_t markerId) noexcept
125 {
126 if(!valid_)
127 return false;
128 if(markerId <= lastMarkerId_)
129 {
130 grklog.warn("TLM: marker id %d is greater than last marker id %d. Disabling TLM.", markerId,
132 valid_ = false;
133 return false;
134 }
135 lastMarkerId_ = (int32_t)markerId;
136 return true;
137 }
138
139private:
143 std::unique_ptr<TL_VEC> tilePartLengths_;
144
148 TL_VEC::iterator tilePartLengthsIter_;
149
155
159 bool valid_;
160};
161
167{
172 explicit TLMMarker(uint16_t numSignalledTiles);
173
178 TLMMarker(const std::string& filePath, uint16_t numSignalledTiles, uint64_t tileStreamStart);
179
184 explicit TLMMarker(IStream* stream);
185
189 ~TLMMarker() = default;
190
197 bool read(std::span<uint8_t> headerData, uint16_t headerSize);
198
202 void rewind() noexcept;
203
209 uint8_t getNumTileParts(uint16_t tileIndex) const noexcept;
210
216 TilePartLength<uint32_t>* next(bool peek);
217
221 void invalidate() noexcept;
222
227 [[nodiscard]] bool valid() const noexcept;
228
234 void seekNextSlated(TileWindow* tilesToDecompress, TileCache* tileCache, IStream* stream);
235
241 bool writeBegin(uint32_t numTilePartsTotal);
242
248 void add(TilePartLength<uint32_t> info);
249
257 void add(uint16_t tile_index, uint32_t tile_part_size) noexcept;
258
263 bool writeEnd();
264
271 void readComplete(uint64_t tileStreamStart) noexcept;
272
273 const TPSEQ_VEC& getTileParts(void) const;
274
275private:
277
283
287 IStream* stream_ = nullptr;
288
292 uint64_t streamStart = 0;
293
297 bool valid_ = true;
298
305 bool hasTileIndices_ = false;
306
311 uint16_t tileCount_ = 0;
312
316 uint16_t numSignalledTiles_ = 0;
317
322 uint64_t tilePartStart_ = 0;
323
324 std::string filePath_;
325};
326
327} // namespace grk
Manages a collection of TLM markers, assuming strictly increasing marker ids.
Definition TLMMarker.h:51
void push_back(TilePartLength< uint32_t > tilePartLength)
Pushes a TilePartLength.
Definition TLMMarker.h:69
bool valid_
Flag indicating if the manager is valid (i.e., marker ids are strictly increasing).
Definition TLMMarker.h:159
void reset() noexcept
Resets the iterator state for traversing markers.
Definition TLMMarker.h:87
int32_t lastMarkerId_
stores last valid marker id.
Definition TLMMarker.h:154
TLMMarkerManager()
Definition TLMMarker.h:59
TL_VEC::iterator tilePartLengthsIter_
Iterator into the single TLM tile lengths vector.
Definition TLMMarker.h:148
std::vector< TilePartLength< uint32_t > > TL_VEC
vector of TilePartLength stored in sequence as they appear in the code stream
Definition TLMMarker.h:56
TilePartLength< uint32_t > * next(bool peek=false) noexcept
Gets the next tile part length.
Definition TLMMarker.h:98
std::unique_ptr< TL_VEC > tilePartLengths_
Single TL_VEC storing all tile part lengths in order.
Definition TLMMarker.h:143
bool empty() const noexcept
Checks if the manager contains no markers.
Definition TLMMarker.h:114
bool validateMarkerId(uint8_t markerId) noexcept
Validates marker id.
Definition TLMMarker.h:124
Caches tile processors so that repeated decompress calls on the same codec can reuse SOT metadata,...
Definition TileCache.h:108
Stores state of rectangular window of tiles slated for decompression Each tile's slated or completely...
Definition TileWindow.h:34
ResWindow.
Definition CompressedChunkCache.h:36
ILogger & grklog
Definition Logger.cpp:24
std::vector< std::unique_ptr< TPSeq > > TPSEQ_VEC
Definition TPFetchSeq.h:181
Definition IStream.h:60
std::unique_ptr< TLMMarkerManager > markerManager_
Definition TLMMarker.h:276
bool writeEnd()
Finishes writing TLM marker.
Definition TLMMarker.cpp:424
bool valid_
true if TLM markers are valid
Definition TLMMarker.h:297
void invalidate() noexcept
Marks the object as invalid, in case of corrupt TLM marker.
Definition TLMMarker.cpp:104
void rewind() noexcept
Rewinds state so that tile part lengths can be read.
Definition TLMMarker.cpp:257
bool valid() const noexcept
Checks if the object is valid.
Definition TLMMarker.cpp:99
IStream * stream_
IStream
Definition TLMMarker.h:287
void add(TilePartLength< uint32_t > info)
Adds a new tile part(decompression).
Definition TLMMarker.cpp:109
bool writeBegin(uint32_t numTilePartsTotal)
Prepares to write TLM marker to code stream.
Definition TLMMarker.cpp:372
bool read(std::span< uint8_t > headerData, uint16_t headerSize)
Reads a TLM marker.
Definition TLMMarker.cpp:132
void seekNextSlated(TileWindow *tilesToDecompress, TileCache *tileCache, IStream *stream)
Seeks to next scheduled tile part.
Definition TLMMarker.cpp:322
std::string filePath_
Definition TLMMarker.h:324
TLMMarker(uint16_t numSignalledTiles)
Constructs a TLMMarker.
Definition TLMMarker.cpp:66
bool hasTileIndices_
true if TLM markers store tile indices.
Definition TLMMarker.h:305
uint8_t getNumTileParts(uint16_t tileIndex) const noexcept
Gets the number of tile parts for a given tile index.
Definition TLMMarker.cpp:279
const TPSEQ_VEC & getTileParts(void) const
Definition TLMMarker.cpp:274
~TLMMarker()=default
Destroys a TLMMarker.
TPSEQ_VEC tilePartsPerTile_
stores tile part info sequence for each tile Useful if SOT markers don't store number of tile parts p...
Definition TLMMarker.h:282
TilePartLength< uint32_t > * next(bool peek)
Gets next TilePartLength.
Definition TLMMarker.cpp:295
uint16_t tileCount_
used to track tile index when there are no tile indices stored in markers
Definition TLMMarker.h:311
uint64_t streamStart
cached start of stream before writing TLM markers
Definition TLMMarker.h:292
void readComplete(uint64_t tileStreamStart) noexcept
Completes calculations such as absolute tile part start position for all tile parts in tilePartsPerTi...
Definition TLMMarker.cpp:248
uint64_t tilePartStart_
calculated start position for current tile part parsed from markers
Definition TLMMarker.h:322
uint16_t numSignalledTiles_
number of tiles signalled in main header
Definition TLMMarker.h:316
Stores tile part's length and tile index.
Definition TLMMarker.h:32
uint16_t tileIndex_
Definition TLMMarker.h:40
T length_
Definition TLMMarker.h:42
TilePartLength(uint16_t tileIndex, T len)
Constructs a TilePartLength.
Definition TLMMarker.h:38