Grok  9.7.5
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 {
69 #ifdef DEBUG_LOSSLESS_T2
70  ,
71  included(false)
72 #endif
73  {}
74  virtual ~Codeblock()
75  {
77  }
78  Codeblock(const Codeblock& rhs)
79  : grk_buf2d(rhs), numbps(rhs.numbps), numlenbits(rhs.numlenbits),
81 #ifdef DEBUG_LOSSLESS_T2
82  ,
83  included(0)
84 #endif
85  {
87  }
89  {
90  if(this != &rhs)
91  { // self-assignment check expected
92  x0 = rhs.x0;
93  y0 = rhs.y0;
94  x1 = rhs.x1;
95  y1 = rhs.y1;
97  numbps = rhs.numbps;
98  numlenbits = rhs.numlenbits;
100 #ifdef DEBUG_LOSSLESS_T2
101  included = rhs.included;
102  packet_length_info = rhs.packet_length_info;
103 #endif
104  }
105  return *this;
106  }
108  {
109  (*(grk_rect32*)this) = r;
110  }
112  uint8_t numbps;
113  uint8_t numlenbits;
114  uint32_t numPassesInPacket; /* number of passes encoded in current packet */
115  protected:
116 #ifdef DEBUG_LOSSLESS_T2
117  uint32_t included;
118  std::vector<PacketLengthInfo> packet_length_info;
119 #endif
120 };
121 
123 {
125  : paddedCompressedStream(nullptr), layers(nullptr), passes(nullptr),
127  {}
129  {
131  grkFree(layers);
132  grkFree(passes);
133  }
134  bool init()
135  {
136  if(!layers)
137  {
139  if(!layers)
140  return false;
141  }
142  if(!passes)
143  {
144  passes = (CodePass*)grkCalloc(100, sizeof(CodePass));
145  if(!passes)
146  return false;
147  }
148  return true;
149  }
156  bool allocData(size_t nominalBlockSize)
157  {
158  uint32_t desired_data_size = (uint32_t)(nominalBlockSize * sizeof(uint32_t));
159  // we add two fake zero bytes at beginning of buffer, so that mq coder
160  // can be initialized to data[-1] == actualData[1], and still point
161  // to a valid memory location
162  auto buf = new uint8_t[desired_data_size + grk_cblk_enc_compressed_data_pad_left];
163  buf[0] = 0;
164  buf[1] = 0;
165 
168  compressedStream.len = desired_data_size;
170 
171  return true;
172  }
176  uint32_t numPassesInPreviousPackets; /* number of passes in previous packets */
177  uint32_t numPassesTotal; /* total number of passes in all layers */
178  uint32_t* contextStream;
179 };
180 
182 {
184  : segs(nullptr), numSegments(0),
185 #ifdef DEBUG_LOSSLESS_T2
186  included(0),
187 #endif
189  {}
191  {
192  release();
193  }
194  Segment* getSegment(uint32_t segmentIndex)
195  {
196  if(!segs)
197  {
201  }
202  else if(segmentIndex >= numSegmentsAllocated)
203  {
204  auto new_segs = new Segment[2 * numSegmentsAllocated];
205  for(uint32_t i = 0; i < numSegmentsAllocated; ++i)
206  new_segs[i] = segs[i];
208  delete[] segs;
209  segs = new_segs;
210  }
211 
212  return segs + segmentIndex;
213  }
214  bool init()
215  {
216  return true;
217  }
218  uint32_t getNumSegments(void)
219  {
220  return numSegments;
221  }
223  {
224  return numSegments ? getSegment(numSegments - 1) : nullptr;
225  }
227  {
228  numSegments++;
229  return getCurrentSegment();
230  }
232  {
233  for(auto& b : seg_buffers)
234  delete b;
235  seg_buffers.clear();
236  numSegments = 0;
237  }
239  {
240  return std::accumulate(seg_buffers.begin(), seg_buffers.end(), (size_t)0,
241  [](const size_t s, grk_buf8* a) { return (s + a->len); });
242  }
243  bool copyToContiguousBuffer(uint8_t* buffer)
244  {
245  if(!buffer)
246  return false;
247  size_t offset = 0;
248  for(auto& buf : seg_buffers)
249  {
250  if(buf->len)
251  {
252  memcpy(buffer + offset, buf->buf, buf->len);
253  offset += buf->len;
254  }
255  }
256  return true;
257  }
258  void release(void)
259  {
261  delete[] segs;
262  segs = nullptr;
264  }
265  std::vector<grk_buf8*> seg_buffers;
266 
267  private:
268  Segment* segs; /* information on segments */
269  uint32_t numSegments; /* number of segment in block*/
270  uint32_t numSegmentsAllocated; // number of segments allocated for segs array
271 };
272 
273 } // 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 grkFree(void *ptr)
Deallocates or frees a memory block.
Definition: MemManager.cpp:149
void * grkCalloc(size_t num, size_t size)
Allocate a memory block with elements initialized to 0.
Definition: MemManager.cpp:115
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
Codeblock()
Definition: Codeblock.h:67
uint8_t numlenbits
Definition: Codeblock.h:113
Codeblock(const Codeblock &rhs)
Definition: Codeblock.h:78
uint32_t numPassesInPacket
Definition: Codeblock.h:114
uint8_t numbps
Definition: Codeblock.h:112
void setRect(grk_rect32 r)
Definition: Codeblock.h:107
virtual ~Codeblock()
Definition: Codeblock.h:74
Codeblock & operator=(const Codeblock &rhs)
Definition: Codeblock.h:88
grk_buf8 compressedStream
Definition: Codeblock.h:111
Definition: Codeblock.h:123
bool init()
Definition: Codeblock.h:134
CompressCodeblock()
Definition: Codeblock.h:124
uint32_t numPassesInPreviousPackets
Definition: Codeblock.h:176
uint32_t numPassesTotal
Definition: Codeblock.h:177
CodePass * passes
Definition: Codeblock.h:175
bool allocData(size_t nominalBlockSize)
Allocates data memory for an compressing code block.
Definition: Codeblock.h:156
uint32_t * contextStream
Definition: Codeblock.h:178
Layer * layers
Definition: Codeblock.h:174
uint8_t * paddedCompressedStream
Definition: Codeblock.h:173
virtual ~CompressCodeblock()
Definition: Codeblock.h:128
Definition: Codeblock.h:182
size_t getSegBuffersLen()
Definition: Codeblock.h:238
Segment * nextSegment(void)
Definition: Codeblock.h:226
bool init()
Definition: Codeblock.h:214
bool copyToContiguousBuffer(uint8_t *buffer)
Definition: Codeblock.h:243
virtual ~DecompressCodeblock()
Definition: Codeblock.h:190
uint32_t numSegmentsAllocated
Definition: Codeblock.h:270
Segment * getCurrentSegment(void)
Definition: Codeblock.h:222
void release(void)
Definition: Codeblock.h:258
void cleanUpSegBuffers()
Definition: Codeblock.h:231
std::vector< grk_buf8 * > seg_buffers
Definition: Codeblock.h:265
Segment * segs
Definition: Codeblock.h:268
uint32_t numSegments
Definition: Codeblock.h:269
uint32_t getNumSegments(void)
Definition: Codeblock.h:218
DecompressCodeblock()
Definition: Codeblock.h:183
Segment * getSegment(uint32_t segmentIndex)
Definition: Codeblock.h:194
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: MemManager.h:278
bool owns_data
Definition: MemManager.h:241
size_t len
Definition: MemManager.h:240
virtual void dealloc()
Definition: MemManager.h:160
T * buf
Definition: MemManager.h:238
size_t offset
Definition: MemManager.h:239
uint32_t y1
Definition: util.h:109
uint32_t x0
Definition: util.h:109
uint32_t x1
Definition: util.h:109
uint32_t y0
Definition: util.h:109
const uint8_t grk_cblk_enc_compressed_data_pad_left
Definition: t1_common.h:40