Grok  9.5.0
Codeblock.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2021 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 grkBuffer2d<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  : grkBuffer2d(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  (*(grkRectU32*)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  {
193  delete[] segs;
194  }
195  Segment* getSegment(uint32_t segmentIndex)
196  {
197  if(!segs)
198  {
202  }
203  else if(segmentIndex >= numSegmentsAllocated)
204  {
205  auto new_segs = new Segment[2 * numSegmentsAllocated];
206  for(uint32_t i = 0; i < numSegmentsAllocated; ++i)
207  new_segs[i] = segs[i];
209  delete[] segs;
210  segs = new_segs;
211  }
212 
213  return segs + segmentIndex;
214  }
215  bool init()
216  {
217  return true;
218  }
219  uint32_t getNumSegments(void)
220  {
221  return numSegments;
222  }
224  {
225  return numSegments ? getSegment(numSegments - 1) : nullptr;
226  }
228  {
229  numSegments++;
230  return getCurrentSegment();
231  }
233  {
234  for(auto& b : seg_buffers)
235  delete b;
236  seg_buffers.clear();
237  numSegments = 0;
238  }
240  {
241  return std::accumulate(seg_buffers.begin(), seg_buffers.end(), (size_t)0,
242  [](const size_t s, grkBufferU8* a) { return (s + a->len); });
243  }
244  bool copyToContiguousBuffer(uint8_t* buffer)
245  {
246  if(!buffer)
247  return false;
248  size_t offset = 0;
249  for(auto& buf : seg_buffers)
250  {
251  if(buf->len)
252  {
253  memcpy(buffer + offset, buf->buf, buf->len);
254  offset += buf->len;
255  }
256  }
257  return true;
258  }
259  std::vector<grkBufferU8*> seg_buffers;
260 
261  private:
262  Segment* segs; /* information on segments */
263  uint32_t numSegments; /* number of segment in block*/
264  uint32_t numSegmentsAllocated; // number of segments allocated for segs array
265 };
266 
267 } // namespace grk
Definition: ICacheable.h:29
Copyright (C) 2016-2021 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:145
void * grkCalloc(size_t num, size_t size)
Allocate a memory block with elements initialized to 0.
Definition: MemManager.cpp:113
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
grkBufferU8 compressedStream
Definition: Codeblock.h:111
uint32_t numPassesInPacket
Definition: Codeblock.h:114
uint8_t numbps
Definition: Codeblock.h:112
virtual ~Codeblock()
Definition: Codeblock.h:74
Codeblock & operator=(const Codeblock &rhs)
Definition: Codeblock.h:88
void setRect(grkRectU32 r)
Definition: Codeblock.h:107
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:239
Segment * nextSegment(void)
Definition: Codeblock.h:227
bool init()
Definition: Codeblock.h:215
bool copyToContiguousBuffer(uint8_t *buffer)
Definition: Codeblock.h:244
virtual ~DecompressCodeblock()
Definition: Codeblock.h:190
uint32_t numSegmentsAllocated
Definition: Codeblock.h:264
Segment * getCurrentSegment(void)
Definition: Codeblock.h:223
void cleanUpSegBuffers()
Definition: Codeblock.h:232
std::vector< grkBufferU8 * > seg_buffers
Definition: Codeblock.h:259
Segment * segs
Definition: Codeblock.h:262
uint32_t numSegments
Definition: Codeblock.h:263
uint32_t getNumSegments(void)
Definition: Codeblock.h:219
DecompressCodeblock()
Definition: Codeblock.h:183
Segment * getSegment(uint32_t segmentIndex)
Definition: Codeblock.h:195
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:223
bool owns_data
Definition: MemManager.h:215
size_t offset
Definition: MemManager.h:213
virtual void dealloc()
Definition: MemManager.h:135
T * buf
Definition: MemManager.h:212
size_t len
Definition: MemManager.h:214
uint32_t x1
Definition: util.h:96
uint32_t y0
Definition: util.h:96
uint32_t y1
Definition: util.h:96
uint32_t x0
Definition: util.h:96
const uint8_t grk_cblk_enc_compressed_data_pad_left
Definition: t1_common.h:40