Grok  9.5.0
codestream.hpp
Go to the documentation of this file.
1 // Copyright (c) 2019 - 2021, Osamu Watanabe
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include <cstdint>
32 #include <cassert>
33 #include <cstring>
34 #include <string>
35 #include <vector>
36 #include <memory>
37 #include "utils.hpp"
38 
40  public:
41  virtual ~j2c_source_base() = default;
42  virtual uint8_t get_byte() = 0;
43  virtual int get_N_byte(uint8_t *buf, uint32_t length) = 0;
44  virtual uint16_t get_word() = 0;
45  // virtual uint32_t get_dword() = 0;
46 };
47 
49  private:
50  std::unique_ptr<uint8_t[]> buf;
51  uint32_t pos;
52  uint32_t len;
53 
54  public:
56  buf = nullptr;
57  pos = 0;
58  len = 0;
59  }
60  void alloc_memory(uint32_t length);
61  uint8_t get_byte() override;
62  int get_N_byte(uint8_t *buf, uint32_t length) override;
63  uint16_t get_word() override;
64  uint8_t *get_buf_pos() { return (buf.get() + pos); }
65  int rewind_2bytes();
66  int forward_Nbytes(uint32_t N);
67 };
68 
70  public:
71  virtual ~j2c_destination_base() = default;
72  virtual int32_t put_byte(uint8_t byte) = 0;
73  virtual int32_t put_word(uint16_t word) = 0;
74  virtual int32_t put_dword(uint32_t put_dword) = 0;
75  virtual int32_t put_N_bytes(uint8_t *src, uint32_t length) = 0;
76  virtual int32_t flush(std::ofstream &dst) = 0;
77 };
78 
80  private:
81  std::vector<uint8_t> buf;
82  uint32_t pos;
83 
84  public:
85  j2c_dst_memory() { pos = 0; }
86  ~j2c_dst_memory() override = default;
87  int32_t put_byte(uint8_t byte) override;
88  int32_t put_word(uint16_t word) override;
89  int32_t put_dword(uint32_t dword) override;
90  int32_t put_N_bytes(uint8_t *src, uint32_t length) override;
91  int32_t flush(std::ofstream &dst) override;
92  size_t get_length() const;
93  void print_bytes();
94 };
95 
96 class buf_chain {
97  private:
98  uint32_t total_length;
99  uint32_t num_nodes;
100  std::vector<uint8_t *> node_buf;
101  std::vector<uint32_t> node_length;
102  uint32_t node_pos;
103  uint32_t pos;
104  uint8_t *current_buf;
105  uint32_t current_length;
106  uint8_t tmp_byte;
107  uint8_t last_byte;
108  uint8_t bits;
109 
110  public:
112  total_length = 0;
113  num_nodes = 0;
114  node_buf = {};
115  node_length = {};
116  node_pos = 0;
117  pos = 0;
118  current_buf = nullptr;
119  current_length = 0;
120  tmp_byte = 0;
121  last_byte = 0;
122  bits = 0;
123  }
124  explicit buf_chain(uint32_t num) {
125  total_length = 0;
126  num_nodes = num;
127  for (uint32_t i = 0; i < num; ++i) {
128  node_buf.push_back(nullptr);
129  node_length.push_back(0);
130  }
131  node_pos = 0;
132  pos = 0;
133  current_buf = nullptr;
134  current_length = 0;
135  tmp_byte = 0;
136  last_byte = 0;
137  bits = 0;
138  }
140  if (this != &bc) {
141  this->total_length = bc.total_length;
142  this->num_nodes = bc.num_nodes;
143  this->node_pos = bc.node_pos;
144  this->pos = bc.pos;
145  this->current_length = bc.current_length;
146  this->tmp_byte = bc.tmp_byte;
147  this->last_byte = bc.last_byte;
148  this->bits = bc.bits;
149  this->node_buf.reserve(bc.node_buf.size());
150  for (size_t i = 0; i < bc.node_buf.size(); ++i) {
151  this->node_buf.push_back(bc.node_buf[i]);
152  this->node_length.push_back(bc.node_length[i]);
153  }
154  }
155  return *this;
156  }
157  void add_buf_node(uint8_t *buf, uint32_t len) {
158  total_length += len;
159  node_buf.push_back(buf);
160  node_length.push_back(len);
161  num_nodes++;
162  }
163  void set_buf_node(uint32_t index, uint8_t *buf, uint32_t len) {
164  total_length += len;
165  node_buf[index] = buf;
166  node_length[index] = len;
167  }
168  void activate() {
169  pos = 0;
170  node_pos = 0;
171  current_buf = node_buf[0];
173  }
174  void activate(int n) {
175  assert(n < this->node_buf.size());
176  pos = 0;
177  node_pos = n;
180  }
181  void flush_bits() { bits = 0; }
182  void check_last_FF() {
183  if (tmp_byte == 0xFF) {
184  this->get_bit();
185  }
186  }
187  uint32_t get_total_length() const { return total_length; }
188  uint8_t get_specific_byte(uint32_t bufpos) { return *(current_buf + bufpos); }
189  uint8_t get_byte() {
190  if (pos > current_length - 1) {
191  node_pos++;
192  assert(node_pos <= num_nodes);
195  pos = 0;
196  }
197  return *(current_buf + pos++);
198  }
199  uint8_t *get_current_address() {
200  if (pos > current_length - 1) {
201  node_pos++;
202  assert(node_pos <= num_nodes);
205  pos = 0;
206  }
207  return (current_buf + pos++);
208  }
209  void copy_N_bytes(uint8_t *&buf, uint32_t N) {
210  // The first input argument is a reference of pointer.
211  // The reason of this is the address of 'buf' shall be increased by N.
212  assert((pos + N) <= current_length);
213  memcpy(buf, current_buf + pos, N);
214  pos += N;
215  buf += N;
216  }
217  uint16_t get_word() {
218  uint16_t word = get_byte();
219  word <<= 8;
220  word += get_byte();
221  return word;
222  }
223  uint8_t get_bit() {
224  if (bits == 0) {
225  tmp_byte = get_byte();
226  if (last_byte == 255) {
227  bits = 7;
228  } else {
229  bits = 8;
230  }
232  }
233  bits--;
234  return (tmp_byte >> bits) & 1;
235  }
236  uint32_t get_N_bits(uint8_t N) {
237  uint32_t cwd = 0;
238  uint8_t bit;
239  for (int i = 0; i < N; i++) {
240  bit = get_bit();
241  cwd <<= 1;
242  cwd += static_cast<uint32_t>(bit);
243  }
244  return cwd;
245  }
246 };
247 
249  private:
250  std::vector<uint8_t> buf;
251  uint8_t tmp;
252  uint8_t last;
253  uint8_t bits;
254  int32_t pos;
255 
256  public:
257  packet_header_writer() : tmp(0), last(0), bits(8), pos(0) {
258  buf.reserve(512);
259  // we don't use empty packet
260  put_bit(1);
261  };
262 
263  int32_t get_length() const { return pos; }
264 
265  size_t copy_buf(uint8_t *p) {
266  for (size_t i = 0; i < buf.size(); ++i) {
267  p[i] = buf[i];
268  }
269  return buf.size();
270  }
271 
272  void put_bit(uint8_t b) {
273  if (bits == 0) {
274  last = tmp;
275  // if the last byte was 0xFF, next 1 bit shall be skipped (bit-stuffing)
276  bits = (last == 0xFF) ? 7 : 8;
277  buf.push_back(tmp);
278  pos++;
279  tmp = 0;
280  }
281  bits--;
282  tmp += b << bits;
283  }
284 
285  void put_Nbits(uint32_t cwd, uint8_t n) {
286  for (int i = n - 1; n >= 0; --n) {
287  put_bit((cwd >> i) & 1);
288  }
289  }
290 
291  void flush(bool use_EPH = false) {
292  for (int i = 0; i < bits; ++i) {
293  put_bit(0);
294  }
295  buf.push_back(tmp);
296  pos++;
297  // if the last byte was 0xFF, next 1 bit shall be skipped (bit-stuffing)
298  if (tmp == 0xFF) {
299  buf.push_back(0);
300  pos++;
301  }
302  if (use_EPH) {
303  // write EPH marker
304  buf.push_back(0xFF);
305  buf.push_back(0x92);
306  pos += 2;
307  }
308  }
309 };
310 
312  private:
313  uint8_t level;
314  int32_t index;
315  int32_t parent_index;
316  std::vector<int32_t> child_index;
317  uint8_t state;
318  uint16_t current_value;
319  uint16_t value;
320  bool set_flag; // only for encoder
321 
322  public:
324  level = 0;
325  index = -1;
326  parent_index = 0;
327  child_index = {};
328  state = 0;
329  current_value = 0;
330  value = 0;
331  set_flag = false;
332  }
333  void set_node(uint32_t l, uint32_t i, uint32_t pi) {
334  level = l;
335  index = i;
336  parent_index = pi;
337  }
338  void add_child(int32_t val = 0) { child_index.push_back(val); }
339  uint8_t get_level() const { return level; }
340  int32_t get_index() const { return index; }
341  int32_t get_parent_index() const { return parent_index; }
342  std::vector<int32_t> get_child_index() { return child_index; }
343  uint8_t get_state() const { return state; }
344  void set_state(uint8_t s) { state = s; }
345  uint16_t get_current_value() const { return current_value; }
346  void set_current_value(uint16_t cv) { current_value = cv; }
347  uint16_t get_value() const { return value; }
348  void set_value(uint16_t v) {
349  value = v;
350  set_flag = true;
351  }
352  bool is_set() const { return set_flag; }
353 };
354 
355 class tagtree {
356  public:
357  uint8_t level;
358  std::unique_ptr<tagtree_node[]> node;
359  uint32_t num_nodes;
360  const uint32_t num_cblk_x;
361  const uint32_t num_cblk_y;
362 
363  public:
364  // tagtree() {
365  // level = 0;
366  // node = nullptr;
367  // num_nodes = 0;
368  // num_cblk_x = 0;
369  // num_cblk_y = 0;
370  // }
371  tagtree(const uint32_t nx, const uint32_t ny) : level(1), num_nodes(0), num_cblk_x(nx), num_cblk_y(ny) {
372  uint32_t num_nodes_current_level, width_current_level, height_current_level;
373  width_current_level = num_cblk_x;
374  height_current_level = num_cblk_y;
375 
376  // calculate number of nodes
377  while (true) {
378  num_nodes_current_level = width_current_level * height_current_level;
379  num_nodes += num_nodes_current_level;
380  width_current_level = ceil_int(width_current_level, 2);
381  height_current_level = ceil_int(height_current_level, 2);
382  if (num_nodes_current_level <= 1) {
383  break;
384  } else {
385  level++;
386  }
387  }
388  // create tagtree nodes
389  node = std::make_unique<tagtree_node[]>(num_nodes);
390 
391  // build tagtree structure
392  uint32_t node_index = 0, parent_num = 0, depth = level - 1;
393  uint32_t parent_index, row_parent_index;
394  width_current_level = num_cblk_x;
395  height_current_level = num_cblk_y;
396  tagtree_node *current_node, *parent_node;
397  current_node = &node[node_index];
398  while (true) {
399  num_nodes_current_level = width_current_level * height_current_level;
400  if (num_nodes_current_level <= 1) {
401  break;
402  }
403  parent_num += num_nodes_current_level;
404  row_parent_index = parent_num;
405  for (uint32_t i = 0; i < height_current_level; i++) {
406  parent_index = row_parent_index;
407  for (uint32_t j = 0; j < width_current_level; j++) {
408  current_node->set_node(depth, node_index, parent_index);
409  parent_node = &node[parent_index];
410  parent_node->add_child(node_index);
411  node_index++;
412  current_node = &node[node_index];
413 
414  if (j % 2 == 1 && j != width_current_level - 1) {
415  parent_index++; // move to next parent in horizontal
416  }
417  }
418  if (i % 2 == 1) {
419  row_parent_index += ceil_int(width_current_level, 2); // move to next parent in vertical
420  }
421  }
422  width_current_level = ceil_int(width_current_level, 2); // number of horizontal nodes for next level
423  height_current_level = ceil_int(height_current_level, 2); // number of vertical nodes for next level
424  depth--;
425  }
426  // when we get here, root node should be set.
427  current_node = &node[num_nodes - 1];
428  current_node->set_node(depth, node_index,
429  -1); // parent index = - 1 means I am the ROOT
430  }
431  void build() const {
432  for (uint32_t i = 0; i < this->num_nodes; ++i) {
433  tagtree_node *current = &this->node[i];
434  // need to reset current value because packet header generation will be done twice
435  current->set_current_value(0);
436  current->set_state(0);
437  if (!current->is_set()) {
438  std::vector<int32_t> children = current->get_child_index();
439  uint16_t val = this->node[children[0]].get_value();
440  for (int &j : children) {
441  uint16_t tmp = this->node[j].get_value();
442  val = (val > tmp) ? tmp : val;
443  }
444  current->set_value(val);
445  }
446  }
447  }
448  // tagtree &operator=(const tagtree &bc) {
449  // level = bc.level;
450  // num_nodes = bc.num_nodes;
451  // num_cblk_x = bc.num_cblk_x;
452  // num_cblk_y = bc.num_cblk_y;
453  // node = std::make_unique<tagtree_node[]>(num_nodes);
454  // for (unsigned long i = 0; i < num_nodes; ++i) {
455  // node[i] = bc.node[i];
456  // }
457  // return *this;
458  // }
459 };
Definition: codestream.hpp:96
uint8_t tmp_byte
Definition: codestream.hpp:106
void add_buf_node(uint8_t *buf, uint32_t len)
Definition: codestream.hpp:157
std::vector< uint32_t > node_length
Definition: codestream.hpp:101
uint8_t get_specific_byte(uint32_t bufpos)
Definition: codestream.hpp:188
buf_chain & operator=(const buf_chain &bc)
Definition: codestream.hpp:139
uint32_t node_pos
Definition: codestream.hpp:102
void flush_bits()
Definition: codestream.hpp:181
uint8_t last_byte
Definition: codestream.hpp:107
void check_last_FF()
Definition: codestream.hpp:182
uint32_t total_length
Definition: codestream.hpp:98
uint32_t num_nodes
Definition: codestream.hpp:99
uint8_t get_bit()
Definition: codestream.hpp:223
uint32_t pos
Definition: codestream.hpp:103
buf_chain(uint32_t num)
Definition: codestream.hpp:124
uint32_t current_length
Definition: codestream.hpp:105
void activate(int n)
Definition: codestream.hpp:174
buf_chain()
Definition: codestream.hpp:111
void set_buf_node(uint32_t index, uint8_t *buf, uint32_t len)
Definition: codestream.hpp:163
void copy_N_bytes(uint8_t *&buf, uint32_t N)
Definition: codestream.hpp:209
uint32_t get_total_length() const
Definition: codestream.hpp:187
uint8_t * get_current_address()
Definition: codestream.hpp:199
uint16_t get_word()
Definition: codestream.hpp:217
uint32_t get_N_bits(uint8_t N)
Definition: codestream.hpp:236
void activate()
Definition: codestream.hpp:168
std::vector< uint8_t * > node_buf
Definition: codestream.hpp:100
uint8_t get_byte()
Definition: codestream.hpp:189
uint8_t bits
Definition: codestream.hpp:108
uint8_t * current_buf
Definition: codestream.hpp:104
Definition: codestream.hpp:69
virtual int32_t put_N_bytes(uint8_t *src, uint32_t length)=0
virtual int32_t put_byte(uint8_t byte)=0
virtual ~j2c_destination_base()=default
virtual int32_t flush(std::ofstream &dst)=0
virtual int32_t put_word(uint16_t word)=0
virtual int32_t put_dword(uint32_t put_dword)=0
Definition: codestream.hpp:79
size_t get_length() const
~j2c_dst_memory() override=default
int32_t put_dword(uint32_t dword) override
uint32_t pos
Definition: codestream.hpp:82
void print_bytes()
int32_t put_N_bytes(uint8_t *src, uint32_t length) override
j2c_dst_memory()
Definition: codestream.hpp:85
int32_t put_byte(uint8_t byte) override
int32_t put_word(uint16_t word) override
int32_t flush(std::ofstream &dst) override
std::vector< uint8_t > buf
Definition: codestream.hpp:81
Definition: codestream.hpp:39
virtual uint8_t get_byte()=0
virtual ~j2c_source_base()=default
virtual int get_N_byte(uint8_t *buf, uint32_t length)=0
virtual uint16_t get_word()=0
Definition: codestream.hpp:48
int get_N_byte(uint8_t *buf, uint32_t length) override
Definition: codestream_source.cpp:48
uint32_t len
Definition: codestream.hpp:52
uint16_t get_word() override
Definition: codestream_source.cpp:55
uint8_t get_byte() override
Definition: codestream_source.cpp:38
int rewind_2bytes()
Definition: codestream_source.cpp:64
void alloc_memory(uint32_t length)
Definition: codestream_source.cpp:32
uint8_t * get_buf_pos()
Definition: codestream.hpp:64
std::unique_ptr< uint8_t[]> buf
Definition: codestream.hpp:50
j2c_src_memory()
Definition: codestream.hpp:55
uint32_t pos
Definition: codestream.hpp:51
int forward_Nbytes(uint32_t N)
Definition: codestream_source.cpp:73
Definition: codestream.hpp:248
std::vector< uint8_t > buf
Definition: codestream.hpp:250
uint8_t tmp
Definition: codestream.hpp:251
uint8_t bits
Definition: codestream.hpp:253
packet_header_writer()
Definition: codestream.hpp:257
size_t copy_buf(uint8_t *p)
Definition: codestream.hpp:265
void put_bit(uint8_t b)
Definition: codestream.hpp:272
void put_Nbits(uint32_t cwd, uint8_t n)
Definition: codestream.hpp:285
uint8_t last
Definition: codestream.hpp:252
void flush(bool use_EPH=false)
Definition: codestream.hpp:291
int32_t get_length() const
Definition: codestream.hpp:263
int32_t pos
Definition: codestream.hpp:254
Definition: codestream.hpp:311
std::vector< int32_t > child_index
Definition: codestream.hpp:316
int32_t index
Definition: codestream.hpp:314
uint16_t current_value
Definition: codestream.hpp:318
uint8_t level
Definition: codestream.hpp:313
bool is_set() const
Definition: codestream.hpp:352
uint8_t get_state() const
Definition: codestream.hpp:343
int32_t get_index() const
Definition: codestream.hpp:340
void add_child(int32_t val=0)
Definition: codestream.hpp:338
tagtree_node()
Definition: codestream.hpp:323
int32_t get_parent_index() const
Definition: codestream.hpp:341
std::vector< int32_t > get_child_index()
Definition: codestream.hpp:342
bool set_flag
Definition: codestream.hpp:320
uint16_t get_value() const
Definition: codestream.hpp:347
uint8_t state
Definition: codestream.hpp:317
uint8_t get_level() const
Definition: codestream.hpp:339
uint16_t get_current_value() const
Definition: codestream.hpp:345
void set_current_value(uint16_t cv)
Definition: codestream.hpp:346
void set_node(uint32_t l, uint32_t i, uint32_t pi)
Definition: codestream.hpp:333
void set_state(uint8_t s)
Definition: codestream.hpp:344
void set_value(uint16_t v)
Definition: codestream.hpp:348
uint16_t value
Definition: codestream.hpp:319
int32_t parent_index
Definition: codestream.hpp:315
Definition: codestream.hpp:355
uint32_t num_nodes
Definition: codestream.hpp:359
const uint32_t num_cblk_y
Definition: codestream.hpp:361
uint8_t level
Definition: codestream.hpp:357
tagtree(const uint32_t nx, const uint32_t ny)
Definition: codestream.hpp:371
void build() const
Definition: codestream.hpp:431
std::unique_ptr< tagtree_node[]> node
Definition: codestream.hpp:358
const uint32_t num_cblk_x
Definition: codestream.hpp:360
#define ceil_int(a, b)
Definition: utils.hpp:36