Grok 20.3.2
PacketTracker.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 <cstring>
22#include <cstdint>
23
24namespace grk
25{
26
28{
29public:
30 PacketTracker(uint16_t numComps, uint8_t numRes, uint64_t numPrec, uint16_t numLayers)
31 : numComps_(numComps), numRes_(numRes), numPrec_(numPrec), numLayers_(numLayers)
32 {
34 bits_.resize(len, 0);
35 }
36
37 void clear()
38 {
39 std::fill(bits_.begin(), bits_.end(), 0);
40 }
41
42 void packet_encoded(uint16_t comp, uint8_t res, uint64_t prec, uint16_t layer)
43 {
44 if(!isValidIndex(comp, res, prec, layer))
45 return;
46
47 auto ind = index(comp, res, prec, layer);
48 // Using right shift for division by 8 and bitwise AND for modulus 8
49 bits_[ind >> 3] |= static_cast<uint8_t>(1 << (ind & 7));
50 }
51
52 bool is_packet_encoded(uint16_t comp, uint8_t res, uint64_t prec, uint16_t layer) const
53 {
54 if(!isValidIndex(comp, res, prec, layer))
55 return true;
56
57 auto ind = index(comp, res, prec, layer);
58 return bits_[ind >> 3] &
59 (1 << (ind & 7)); // Using right shift for division by 8 and bitwise AND for modulus 8
60 }
61
62private:
63 std::vector<uint8_t> bits_;
64 uint16_t numComps_;
65 uint8_t numRes_;
66 uint64_t numPrec_;
67 uint16_t numLayers_;
68
69 uint64_t get_buffer_len(uint16_t numComps, uint8_t numRes, uint64_t numPrec,
70 uint16_t numLayers) const
71 {
72 uint64_t totalBits = static_cast<uint64_t>(numComps) * numRes * numPrec * numLayers;
73 return (totalBits + 7) >> 3; // Using right shift for division by 8
74 }
75
76 bool isValidIndex(uint16_t comp, uint8_t res, uint64_t prec, uint16_t layer) const
77 {
78 return comp < numComps_ && res < numRes_ && prec < numPrec_ && layer < numLayers_;
79 }
80
81 uint64_t index(uint16_t comp, uint8_t res, uint64_t prec, uint16_t layer) const
82 {
83 return static_cast<uint64_t>(layer) * numComps_ * numRes_ * numPrec_ +
84 static_cast<uint64_t>(comp) * numRes_ * numPrec_ +
85 static_cast<uint64_t>(res) * numPrec_ + prec;
86 }
87};
88
89} // namespace grk
PacketTracker(uint16_t numComps, uint8_t numRes, uint64_t numPrec, uint16_t numLayers)
Definition PacketTracker.h:30
void clear()
Definition PacketTracker.h:37
uint16_t numComps_
Definition PacketTracker.h:64
bool isValidIndex(uint16_t comp, uint8_t res, uint64_t prec, uint16_t layer) const
Definition PacketTracker.h:76
uint64_t get_buffer_len(uint16_t numComps, uint8_t numRes, uint64_t numPrec, uint16_t numLayers) const
Definition PacketTracker.h:69
bool is_packet_encoded(uint16_t comp, uint8_t res, uint64_t prec, uint16_t layer) const
Definition PacketTracker.h:52
void packet_encoded(uint16_t comp, uint8_t res, uint64_t prec, uint16_t layer)
Definition PacketTracker.h:42
uint64_t index(uint16_t comp, uint8_t res, uint64_t prec, uint16_t layer) const
Definition PacketTracker.h:81
uint64_t numPrec_
Definition PacketTracker.h:66
uint8_t numRes_
Definition PacketTracker.h:65
uint16_t numLayers_
Definition PacketTracker.h:67
std::vector< uint8_t > bits_
Definition PacketTracker.h:63
ResWindow.
Definition CompressedChunkCache.h:36