Grok 20.3.2
TPFetchSeq.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 <vector>
21#include <memory>
22#include <set>
23#include <unordered_map>
24#include <atomic>
25
26#include "FetchCommon.h"
27
28namespace grk
29{
30
36template<typename T>
38{
39 using iterator = typename std::vector<std::shared_ptr<T>>::iterator;
40 using const_iterator = typename std::vector<std::shared_ptr<T>>::const_iterator;
41 std::shared_ptr<T>& operator[](size_t index)
42 {
43 return objStore_[index];
44 }
45 const std::shared_ptr<T>& operator[](size_t index) const
46 {
47 return objStore_[index];
48 }
49
55 {
56 return objStore_.begin();
57 }
58
64 {
65 return objStore_.end();
66 }
67
72 bool empty() const
73 {
74 return objStore_.empty();
75 }
76
81 size_t size() const
82 {
83 return objStore_.size();
84 }
85
90 void push_back(std::shared_ptr<T> ptr)
91 {
92 objStore_.push_back(std::move(ptr));
93 }
94
95 void resize(size_t N)
96 {
97 objStore_.resize(N);
98 }
99 void clear()
100 {
101 objStore_.clear();
102 }
103
104private:
105 std::vector<std::shared_ptr<T>> objStore_;
106};
107
113struct TPSeq : public SharedPtrSeq<DataSlice>
114{
118 ~TPSeq() = default;
119
128 bool push_back(uint8_t tilePart, uint8_t numTileParts, uint64_t offset, uint32_t length)
129 {
130 // Once one tile part has non-zero number of tile parts, then every
131 // subsequent tile part must have matching number of tile parts
133 {
134 grklog.error("Number of tile parts %d does not match previous value %d", numTileParts,
136 return false;
137 }
138 signalledNumTileParts_ = numTileParts;
139
140 if(tilePart != size())
141 {
142 grklog.error("Tile part %d is out of sequence with current number of tile parts %d", tilePart,
143 size());
144 return false;
145 }
146 SharedPtrSeq<DataSlice>::push_back(std::make_shared<DataSlice>(offset, length));
147 return true;
148 }
149
150 void reset()
151 {
152 clear();
154 }
155
162 void complete(uint64_t tileStreamOffset)
163 {
164 for(auto& part : *this)
165 {
166 part->offset_ += tileStreamOffset;
167 }
169 signalledNumTileParts_ = static_cast<uint8_t>(size());
170 }
171
172private:
179};
180
181using TPSEQ_VEC = std::vector<std::unique_ptr<TPSeq>>;
182
188struct TPFetch : public DataSlice
189{
197 TPFetch(uint64_t offset, uint64_t length, uint16_t tileIndex)
198 : DataSlice(offset, length), tileIndex_(tileIndex)
199 {}
200
208 void copy(uint8_t* chunk, size_t chunkLen)
209 {
210 if(!data_)
211 {
212 data_ = std::make_unique<uint8_t[]>(length_);
213 }
214 std::memcpy(data_.get() + fetchOffset_, chunk, chunkLen);
215 fetchOffset_ += chunkLen;
216 }
217
218 uint16_t tileIndex_ = 0;
219 std::unique_ptr<uint8_t[]> data_;
220 size_t fetchOffset_ = 0;
221 std::unique_ptr<IStream> stream_;
222};
223
224struct TPFetchSeq : SharedPtrSeq<TPFetch>
225{
226 void push_back(uint16_t tileIndex, const std::unique_ptr<TPSeq>& tileParts)
227 {
228 if(!tileParts)
229 return;
230
231 for(auto& part : *tileParts)
232 {
234 std::make_shared<TPFetch>(part->offset_, part->length_, tileIndex));
235 }
236 }
237 void push_back(uint16_t tileIndex, const std::unique_ptr<TPSeq>& tileParts,
238 std::vector<std::shared_ptr<TPFetch>>& outParts)
239 {
240 if(!tileParts)
241 return;
242
243 for(auto& part : *tileParts)
244 {
245 auto fetchPtr = std::make_shared<TPFetch>(part->offset_, part->length_, tileIndex);
247 outParts.push_back(fetchPtr);
248 }
249 }
250
251 static void
252 genCollections(const TPSEQ_VEC* allTileParts, std::set<uint16_t>& slated,
253 std::shared_ptr<TPFetchSeq>& tilePartFetchFlat,
254 std::shared_ptr<std::unordered_map<uint16_t, std::shared_ptr<TPFetchSeq>>>&
255 tilePartFetchByTile)
256 {
257 for(auto& tileIndex : slated)
258 {
259 const auto& tileParts = (*allTileParts)[tileIndex];
260 std::vector<std::shared_ptr<TPFetch>> tileFetchParts;
261
262 tilePartFetchFlat->push_back(tileIndex, tileParts, tileFetchParts);
263
264 auto [it, inserted] = tilePartFetchByTile->emplace(tileIndex, std::make_unique<TPFetchSeq>());
265 if(inserted)
266 {
267 for(auto& fetchPtr : tileFetchParts)
268 {
269 it->second->SharedPtrSeq<TPFetch>::push_back(fetchPtr);
270 }
271 }
272 }
273 }
274
276 {
277 std::atomic_ref<uint8_t> atomicCount(fetchCount_);
278 return atomicCount.fetch_add(1, std::memory_order_seq_cst) + 1;
279 }
280
281private:
282 uint8_t fetchCount_ = 0;
283};
284
285// IRequestBatch adapter for tile part fetch requests
287{
288 TileRequestBatch(std::shared_ptr<TPFetchSeq> requests) : requests_(std::move(requests))
289 {
290 iter_ = requests_->begin();
291 }
292 bool hasMore() const override
293 {
294 return iter_ != requests_->end();
295 }
296 size_t remaining() const override
297 {
298 return static_cast<size_t>(requests_->end() - iter_);
299 }
300 std::pair<uint64_t, uint64_t> next() override
301 {
302 auto& req = *iter_++;
303 return {req->offset_, req->offset_ + req->length_ - 1};
304 }
305
306private:
307 std::shared_ptr<TPFetchSeq> requests_;
309};
310
311} // namespace grk
ResWindow.
Definition CompressedChunkCache.h:36
ILogger & grklog
Definition Logger.cpp:24
std::vector< std::unique_ptr< TPSeq > > TPSEQ_VEC
Definition TPFetchSeq.h:181
DataSlice(uint64_t offset, uint64_t length)
Definition FetchCommon.h:115
uint64_t length_
Definition FetchCommon.h:119
Definition FetchCommon.h:158
Sequence of shared_ptr<T>.
Definition TPFetchSeq.h:38
void push_back(std::shared_ptr< T > ptr)
Adds a pre-existing shared_ptr to the end of the sequence.
Definition TPFetchSeq.h:90
void resize(size_t N)
Definition TPFetchSeq.h:95
iterator begin()
Returns iterator to beginning of sequence.
Definition TPFetchSeq.h:54
typename std::vector< std::shared_ptr< T > >::const_iterator const_iterator
Definition TPFetchSeq.h:40
const_iterator end() const
Returns const iterator to end of sequence.
Definition TPFetchSeq.h:63
const std::shared_ptr< T > & operator[](size_t index) const
Definition TPFetchSeq.h:45
bool empty() const
Checks if there are no objects in store.
Definition TPFetchSeq.h:72
size_t size() const
Returns number of objects in store.
Definition TPFetchSeq.h:81
void clear()
Definition TPFetchSeq.h:99
std::shared_ptr< T > & operator[](size_t index)
Definition TPFetchSeq.h:41
std::vector< std::shared_ptr< T > > objStore_
Definition TPFetchSeq.h:105
typename std::vector< std::shared_ptr< T > >::iterator iterator
Definition TPFetchSeq.h:39
std::unique_ptr< IStream > stream_
Definition TPFetchSeq.h:221
uint16_t tileIndex_
Definition TPFetchSeq.h:218
size_t fetchOffset_
Definition TPFetchSeq.h:220
std::unique_ptr< uint8_t[]> data_
Definition TPFetchSeq.h:219
void copy(uint8_t *chunk, size_t chunkLen)
Copies next fetched data chunk into data buffer Chunks are guaranteed to be received in order for a s...
Definition TPFetchSeq.h:208
TPFetch(uint64_t offset, uint64_t length, uint16_t tileIndex)
Construct a new TPFetch object.
Definition TPFetchSeq.h:197
Definition TPFetchSeq.h:225
uint8_t incrementFetchCount()
Definition TPFetchSeq.h:275
static void genCollections(const TPSEQ_VEC *allTileParts, std::set< uint16_t > &slated, std::shared_ptr< TPFetchSeq > &tilePartFetchFlat, std::shared_ptr< std::unordered_map< uint16_t, std::shared_ptr< TPFetchSeq > > > &tilePartFetchByTile)
Definition TPFetchSeq.h:252
uint8_t fetchCount_
Definition TPFetchSeq.h:282
void push_back(uint16_t tileIndex, const std::unique_ptr< TPSeq > &tileParts)
Definition TPFetchSeq.h:226
void push_back(uint16_t tileIndex, const std::unique_ptr< TPSeq > &tileParts, std::vector< std::shared_ptr< TPFetch > > &outParts)
Definition TPFetchSeq.h:237
Sequence of shared_ptr<DataSlice> tile parts parsed from either TLM or SOT marker.
Definition TPFetchSeq.h:114
void reset()
Definition TPFetchSeq.h:150
bool push_back(uint8_t tilePart, uint8_t numTileParts, uint64_t offset, uint32_t length)
Pushes a new tile part to the back of the sequence.
Definition TPFetchSeq.h:128
uint8_t signalledNumTileParts_
Number of tile parts signalled in code stream This number is either explicitly stored in SOT,...
Definition TPFetchSeq.h:178
void complete(uint64_t tileStreamOffset)
Completes calculations such as absolute tile part offsets, which are not available when TLM markers a...
Definition TPFetchSeq.h:162
~TPSeq()=default
Destroys a TPSeq.
size_t remaining() const override
Definition TPFetchSeq.h:296
std::shared_ptr< TPFetchSeq > requests_
Definition TPFetchSeq.h:307
TPFetchSeq::iterator iter_
Definition TPFetchSeq.h:308
bool hasMore() const override
Definition TPFetchSeq.h:292
TileRequestBatch(std::shared_ptr< TPFetchSeq > requests)
Definition TPFetchSeq.h:288
std::pair< uint64_t, uint64_t > next() override
Definition TPFetchSeq.h:300