Grok  10.0.3
Codeblock.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2022 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 #pragma once
17 
18 #include "grk_includes.h"
19 #include <vector>
20 namespace grk
21 {
22 // code segment (code block can be encoded into multiple segments)
23 struct Segment
24 {
26  {
27  clear();
28  }
29  void clear()
30  {
31  numpasses = 0;
32  len = 0;
33  maxpasses = 0;
35  numBytesInPacket = 0;
36  }
37  uint32_t numpasses; // number of passes in segment
38  uint32_t len; // total length of segment
39  uint32_t maxpasses; // maximum number of passes in segment
40  uint32_t numPassesInPacket; // number of passes contributed by current packet
41  uint32_t numBytesInPacket; // number of bytes contributed by current packet
42 };
43 
44 // compressing/decoding pass
45 struct CodePass
46 {
47  CodePass() : rate(0), distortiondec(0), len(0), term(0), slope(0) {}
48  uint32_t rate;
49  double distortiondec;
50  uint32_t len;
51  uint8_t term;
52  uint16_t slope; // ln(slope) in 8.8 fixed point
53 };
54 // quality layer
55 struct Layer
56 {
57  Layer() : numpasses(0), len(0), distortion(0), data(nullptr) {}
58  uint32_t numpasses; // Number of passes in the layer
59  uint32_t len; // number of bytes in layer
60  double distortion; // layer distortion decrease
61  uint8_t* data; // compressed layer data
62 };
63 
64 // note: block lives in canvas coordinates
65 struct Codeblock : public grk_buf2d<int32_t, AllocatorAligned>, public ICacheable
66 {
67  Codeblock(uint16_t numLayers)
68  : numbps(0), numlenbits(0)
69 #ifdef DEBUG_LOSSLESS_T2
70  ,
71  included(false)
72 #endif
73  {
74  numPassesInPacket = std::vector<uint8_t>(numLayers);
75  std::fill(numPassesInPacket.begin(), numPassesInPacket.end(), 0);
76  }
77  virtual ~Codeblock()
78  {
80  }
81  Codeblock(const Codeblock& rhs)
82  : grk_buf2d(rhs), numbps(rhs.numbps), numlenbits(rhs.numlenbits),
84 #ifdef DEBUG_LOSSLESS_T2
85  ,
86  included(0)
87 #endif
88  {
90  }
92  {
93  if(this != &rhs)
94  { // self-assignment check expected
95  x0 = rhs.x0;
96  y0 = rhs.y0;
97  x1 = rhs.x1;
98  y1 = rhs.y1;
100  numbps = rhs.numbps;
101  numlenbits = rhs.numlenbits;
103 #ifdef DEBUG_LOSSLESS_T2
104  included = rhs.included;
105  packet_length_info = rhs.packet_length_info;
106 #endif
107  }
108  return *this;
109  }
111  {
112  (*(grk_rect32*)this) = r;
113  }
115  uint8_t numbps;
116  uint8_t numlenbits;
117  uint8_t getNumPassesInPacket(uint16_t layno)
118  {
119  assert(layno < numPassesInPacket.size());
120  return numPassesInPacket[layno];
121  }
122  void setNumPassesInPacket(uint16_t layno, uint8_t passes)
123  {
124  assert(layno < numPassesInPacket.size());
125  numPassesInPacket[layno] = passes;
126  }
127  void incNumPassesInPacket(uint16_t layno, uint8_t delta)
128  {
129  assert(layno < numPassesInPacket.size());
130  numPassesInPacket[layno] += delta;
131  }
132 
133  protected:
134  std::vector<uint8_t> numPassesInPacket;
135 #ifdef DEBUG_LOSSLESS_T2
136  uint32_t included;
137  std::vector<PacketLengthInfo> packet_length_info;
138 #endif
139 };
140 
142 {
143  CompressCodeblock(uint16_t numLayers)
144  : Codeblock(numLayers), paddedCompressedStream(nullptr), layers(nullptr), passes(nullptr),
146  {}
148  {
150  grk_free(layers);
151  grk_free(passes);
152  }
153  bool init()
154  {
155  if(!layers)
156  {
158  if(!layers)
159  return false;
160  }
161  if(!passes)
162  {
163  passes = (CodePass*)grk_calloc(100, sizeof(CodePass));
164  if(!passes)
165  return false;
166  }
167  return true;
168  }
175  bool allocData(size_t nominalBlockSize)
176  {
177  uint32_t desired_data_size = (uint32_t)(nominalBlockSize * sizeof(uint32_t));
178  // we add two fake zero bytes at beginning of buffer, so that mq coder
179  // can be initialized to data[-1] == actualData[1], and still point
180  // to a valid memory location
181  auto buf = new uint8_t[desired_data_size + grk_cblk_enc_compressed_data_pad_left];
182  buf[0] = 0;
183  buf[1] = 0;
184 
187  compressedStream.len = desired_data_size;
189 
190  return true;
191  }
195  uint32_t numPassesInPreviousPackets; /* number of passes in previous packets */
196  uint32_t numPassesTotal; /* total number of passes in all layers */
197  uint32_t* contextStream;
198 };
199 
201 {
202  DecompressCodeblock(uint16_t numLayers)
203  : Codeblock(numLayers), segs(nullptr), numSegments(0),
204 #ifdef DEBUG_LOSSLESS_T2
205  included(0),
206 #endif
208  {}
210  {
211  release();
212  }
213  Segment* getSegment(uint32_t segmentIndex)
214  {
215  if(!segs)
216  {
220  }
221  else if(segmentIndex >= numSegmentsAllocated)
222  {
223  auto new_segs = new Segment[2 * numSegmentsAllocated];
224  for(uint32_t i = 0; i < numSegmentsAllocated; ++i)
225  new_segs[i] = segs[i];
227  delete[] segs;
228  segs = new_segs;
229  }
230 
231  return segs + segmentIndex;
232  }
233  bool init()
234  {
235  return true;
236  }
237  uint32_t getNumSegments(void)
238  {
239  return numSegments;
240  }
242  {
243  return numSegments ? getSegment(numSegments - 1) : nullptr;
244  }
246  {
247  numSegments++;
248  return getCurrentSegment();
249  }
251  {
252  for(auto& b : seg_buffers)
253  delete b;
254  seg_buffers.clear();
255  numSegments = 0;
256  }
258  {
259  return std::accumulate(seg_buffers.begin(), seg_buffers.end(), (size_t)0,
260  [](const size_t s, grk_buf8* a) { return (s + a->len); });
261  }
262  bool copyToContiguousBuffer(uint8_t* buffer)
263  {
264  if(!buffer)
265  return false;
266  size_t offset = 0;
267  for(auto& buf : seg_buffers)
268  {
269  if(buf->len)
270  {
271  memcpy(buffer + offset, buf->buf, buf->len);
272  offset += buf->len;
273  }
274  }
275  return true;
276  }
277  void release(void)
278  {
280  delete[] segs;
281  segs = nullptr;
283  }
284  std::vector<grk_buf8*> seg_buffers;
285 
286  private:
287  Segment* segs; /* information on segments */
288  uint32_t numSegments; /* number of segment in block*/
289  uint32_t numSegmentsAllocated; // number of segments allocated for segs array
290 };
291 
292 } // namespace grk
Definition: ICacheable.h:29
Copyright (C) 2016-2022 Grok Image Compression Inc.
Definition: ICacheable.h:20
const uint16_t maxCompressLayersGRK
Definition: CodeStreamLimits.h:43
void grk_free(void *ptr)
Deallocates or frees a memory block.
Definition: MemManager.cpp:96
static const float delta
Definition: WaveletFwd.cpp:42
void * grk_calloc(size_t num, size_t size)
Allocate a memory block with elements initialized to 0.
Definition: MemManager.cpp:68
Definition: Codeblock.h:46
uint16_t slope
Definition: Codeblock.h:52
uint8_t term
Definition: Codeblock.h:51
uint32_t len
Definition: Codeblock.h:50
uint32_t rate
Definition: Codeblock.h:48
double distortiondec
Definition: Codeblock.h:49
CodePass()
Definition: Codeblock.h:47
Definition: Codeblock.h:66
uint8_t numlenbits
Definition: Codeblock.h:116
Codeblock(const Codeblock &rhs)
Definition: Codeblock.h:81
uint8_t getNumPassesInPacket(uint16_t layno)
Definition: Codeblock.h:117
uint8_t numbps
Definition: Codeblock.h:115
void setRect(grk_rect32 r)
Definition: Codeblock.h:110
void setNumPassesInPacket(uint16_t layno, uint8_t passes)
Definition: Codeblock.h:122
virtual ~Codeblock()
Definition: Codeblock.h:77
std::vector< uint8_t > numPassesInPacket
Definition: Codeblock.h:134
Codeblock & operator=(const Codeblock &rhs)
Definition: Codeblock.h:91
grk_buf8 compressedStream
Definition: Codeblock.h:114
void incNumPassesInPacket(uint16_t layno, uint8_t delta)
Definition: Codeblock.h:127
Codeblock(uint16_t numLayers)
Definition: Codeblock.h:67
Definition: Codeblock.h:142
bool init()
Definition: Codeblock.h:153
uint32_t numPassesInPreviousPackets
Definition: Codeblock.h:195
uint32_t numPassesTotal
Definition: Codeblock.h:196
CodePass * passes
Definition: Codeblock.h:194
bool allocData(size_t nominalBlockSize)
Allocates data memory for an compressing code block.
Definition: Codeblock.h:175
CompressCodeblock(uint16_t numLayers)
Definition: Codeblock.h:143
uint32_t * contextStream
Definition: Codeblock.h:197
Layer * layers
Definition: Codeblock.h:193
uint8_t * paddedCompressedStream
Definition: Codeblock.h:192
virtual ~CompressCodeblock()
Definition: Codeblock.h:147
Definition: Codeblock.h:201
size_t getSegBuffersLen()
Definition: Codeblock.h:257
Segment * nextSegment(void)
Definition: Codeblock.h:245
bool init()
Definition: Codeblock.h:233
bool copyToContiguousBuffer(uint8_t *buffer)
Definition: Codeblock.h:262
virtual ~DecompressCodeblock()
Definition: Codeblock.h:209
uint32_t numSegmentsAllocated
Definition: Codeblock.h:289
DecompressCodeblock(uint16_t numLayers)
Definition: Codeblock.h:202
Segment * getCurrentSegment(void)
Definition: Codeblock.h:241
void release(void)
Definition: Codeblock.h:277
void cleanUpSegBuffers()
Definition: Codeblock.h:250
std::vector< grk_buf8 * > seg_buffers
Definition: Codeblock.h:284
Segment * segs
Definition: Codeblock.h:287
uint32_t numSegments
Definition: Codeblock.h:288
uint32_t getNumSegments(void)
Definition: Codeblock.h:237
Segment * getSegment(uint32_t segmentIndex)
Definition: Codeblock.h:213
Definition: Codeblock.h:56
uint32_t len
Definition: Codeblock.h:59
double distortion
Definition: Codeblock.h:60
uint8_t * data
Definition: Codeblock.h:61
Layer()
Definition: Codeblock.h:57
uint32_t numpasses
Definition: Codeblock.h:58
Definition: Codeblock.h:24
void clear()
Definition: Codeblock.h:29
uint32_t maxpasses
Definition: Codeblock.h:39
uint32_t len
Definition: Codeblock.h:38
uint32_t numBytesInPacket
Definition: Codeblock.h:41
uint32_t numPassesInPacket
Definition: Codeblock.h:40
Segment()
Definition: Codeblock.h:25
uint32_t numpasses
Definition: Codeblock.h:37
Definition: buffer.h:230
bool owns_data
Definition: buffer.h:198
size_t len
Definition: buffer.h:197
virtual void dealloc()
Definition: buffer.h:117
int32_t * buf
Definition: buffer.h:195
size_t offset
Definition: buffer.h:196
uint32_t y1
Definition: geometry.h:124
uint32_t x0
Definition: geometry.h:124
uint32_t x1
Definition: geometry.h:124
uint32_t y0
Definition: geometry.h:124
const uint8_t grk_cblk_enc_compressed_data_pad_left
Definition: t1_common.h:40