Grok  9.5.0
ht_block_decoding.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 <algorithm> // for max{a,b,c,d}
33 
34 #if defined(_MSC_VER) || defined(__MINGW64__)
35  #include <intrin.h>
36 #elif defined(__x86_64__)
37  #include <x86intrin.h>
38 #endif
39 
40 const int32_t bitmask32[32] = {
41  0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
42  0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
43  0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
44  0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF};
45 
46 /********************************************************************************
47  * state_MS: state class for MagSgn decoding
48  *******************************************************************************/
49 class state_MS_dec {
50  private:
51  uint32_t pos;
52  uint8_t bits;
53  uint8_t tmp;
54  uint8_t last;
55  const uint8_t *buf;
56  const uint32_t length;
57  uint64_t Creg;
58  uint8_t ctreg;
59 
60  public:
61  state_MS_dec(const uint8_t *Dcup, uint32_t Pcup)
62  : pos(0), bits(0), tmp(0), last(0), buf(Dcup), length(Pcup), Creg(0), ctreg(0) {
63  while (ctreg < 32) {
64  loadByte();
65  }
66  }
67  void loadByte();
68  void close(int32_t num_bits);
69  uint8_t importMagSgnBit();
70  int32_t decodeMagSgnValue(int32_t m_n, int32_t i_n);
71 };
72 
73 /********************************************************************************
74  * state_MEL_unPacker and state_MEL: state classes for MEL decoding
75  *******************************************************************************/
77  private:
78  uint32_t pos;
79  int8_t bits;
80  uint8_t tmp;
81  const uint8_t *buf;
82  uint32_t length;
83 
84  public:
85  state_MEL_unPacker(const uint8_t *Dcup, uint32_t Lcup, uint32_t Pcup)
86  : pos(Pcup), bits(0), tmp(0), buf(Dcup), length(Lcup) {}
87  uint8_t impoertMELbit();
88 };
89 
91  private:
92  uint8_t MEL_k;
93  uint8_t MEL_run;
94  uint8_t MEL_one;
95  const uint8_t MEL_E[13];
97 
98  public:
100  : MEL_k(0),
101  MEL_run(0),
102  MEL_one(0),
103  MEL_E{0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5},
104  MEL_unPacker(&unpacker) {}
105  uint8_t decodeMELSym();
106 };
107 
108 #define ADVANCED
109 #ifdef ADVANCED
110  #define getbitfunc getVLCbit()
111 #else
112  #define getbitfunc importVLCBit()
113 #endif
114 /********************************************************************************
115  * state_VLC: state classe for VLC decoding
116  *******************************************************************************/
118  private:
119  int32_t pos;
120  uint8_t last;
121 #ifndef ADVANCED
122  uint8_t tmp;
123  uint32_t rev_length;
124 #else
125  int32_t ctreg;
126  uint64_t Creg;
127 #endif
128  uint8_t bits;
129  uint8_t *buf;
130 
131  public:
132  state_VLC_dec(uint8_t *Dcup, uint32_t Lcup, uint32_t Pcup)
133 #ifndef ADVANCED
134  : pos((Lcup > 2) ? Lcup - 3 : 0),
135  last(*(Dcup + Lcup - 2)),
136  tmp(last >> 4),
137  rev_length(Pcup),
138  bits(((tmp & 0x07) < 7) ? 4 : 3),
139  buf(Dcup) {
140  }
141  uint8_t importVLCBit();
142 #else
143  : pos(Lcup - 2 - Pcup), ctreg(0), Creg(0), bits(0), buf(Dcup + Pcup) {
144  load_bytes();
145  ctreg -= 4;
146  Creg >>= 4;
147  while (ctreg < 32) {
148  load_bytes();
149  }
150  }
151  void load_bytes();
152  uint8_t getVLCbit();
153  void close32(int32_t num_bits);
154 #endif
155  void decodeCxtVLC(const uint16_t &context, uint8_t (&u_off)[2], uint8_t (&rho)[2], uint8_t (&emb_k)[2],
156  uint8_t (&emb_1)[2], const uint8_t &first_or_second, const uint16_t *dec_CxtVLC_table);
157  uint8_t decodeUPrefix();
158  uint8_t decodeUSuffix(const uint8_t &u_pfx);
159  uint8_t decodeUExtension(const uint8_t &u_sfx);
160 };
161 /********************************************************************************
162  * SP_dec: state classe for HT SigProp decoding
163  *******************************************************************************/
164 class SP_dec {
165  private:
166  const uint32_t Lref;
167  uint8_t bits;
168  uint8_t tmp;
169  uint8_t last;
170  uint32_t pos;
171  const uint8_t *Dref;
172 
173  public:
174  SP_dec(const uint8_t *HT_magref_segment, uint32_t magref_length)
175  : Lref(magref_length),
176  bits(0),
177  tmp(0),
178  last(0),
179  pos(0),
180  Dref((Lref == 0) ? nullptr : HT_magref_segment) {}
181  uint8_t importSigPropBit();
182 };
183 
184 /********************************************************************************
185  * MR_dec: state classe for HT MagRef decoding
186  *******************************************************************************/
187 class MR_dec {
188  private:
189  const uint32_t Lref;
190  uint8_t bits;
191  uint8_t last;
192  uint8_t tmp;
193  int32_t pos;
194  const uint8_t *Dref;
195 
196  public:
197  MR_dec(const uint8_t *HT_magref_segment, uint32_t magref_length)
198  : Lref(magref_length),
199  bits(0),
200  last(0xFF),
201  tmp(0),
202  pos((Lref == 0) ? -1 : magref_length - 1),
203  Dref((Lref == 0) ? nullptr : HT_magref_segment) {}
204  uint8_t importMagRefBit();
205 };
206 
207 void htj2k_decode(j2k_codeblock *block, const uint8_t ROIshift);
Definition: ht_block_decoding.hpp:187
uint8_t bits
Definition: ht_block_decoding.hpp:190
uint8_t last
Definition: ht_block_decoding.hpp:191
int32_t pos
Definition: ht_block_decoding.hpp:193
uint8_t tmp
Definition: ht_block_decoding.hpp:192
uint8_t importMagRefBit()
Definition: ht_block_decoding.cpp:376
const uint32_t Lref
Definition: ht_block_decoding.hpp:189
MR_dec(const uint8_t *HT_magref_segment, uint32_t magref_length)
Definition: ht_block_decoding.hpp:197
const uint8_t * Dref
Definition: ht_block_decoding.hpp:194
Definition: ht_block_decoding.hpp:164
uint8_t last
Definition: ht_block_decoding.hpp:169
const uint8_t * Dref
Definition: ht_block_decoding.hpp:171
const uint32_t Lref
Definition: ht_block_decoding.hpp:166
uint8_t importSigPropBit()
Definition: ht_block_decoding.cpp:351
uint8_t bits
Definition: ht_block_decoding.hpp:167
uint32_t pos
Definition: ht_block_decoding.hpp:170
SP_dec(const uint8_t *HT_magref_segment, uint32_t magref_length)
Definition: ht_block_decoding.hpp:174
uint8_t tmp
Definition: ht_block_decoding.hpp:168
Definition: coding_units.hpp:64
Definition: ht_block_decoding.hpp:90
uint8_t decodeMELSym()
Definition: ht_block_decoding.cpp:151
state_MEL_unPacker * MEL_unPacker
Definition: ht_block_decoding.hpp:96
state_MEL_decoder(state_MEL_unPacker &unpacker)
Definition: ht_block_decoding.hpp:99
uint8_t MEL_one
Definition: ht_block_decoding.hpp:94
uint8_t MEL_k
Definition: ht_block_decoding.hpp:92
uint8_t MEL_run
Definition: ht_block_decoding.hpp:93
const uint8_t MEL_E[13]
Definition: ht_block_decoding.hpp:95
Definition: ht_block_decoding.hpp:76
const uint8_t * buf
Definition: ht_block_decoding.hpp:81
int8_t bits
Definition: ht_block_decoding.hpp:79
uint8_t impoertMELbit()
Definition: ht_block_decoding.cpp:136
uint32_t length
Definition: ht_block_decoding.hpp:82
uint8_t tmp
Definition: ht_block_decoding.hpp:80
uint32_t pos
Definition: ht_block_decoding.hpp:78
state_MEL_unPacker(const uint8_t *Dcup, uint32_t Lcup, uint32_t Pcup)
Definition: ht_block_decoding.hpp:85
Definition: ht_block_decoding.hpp:49
void close(int32_t num_bits)
Definition: ht_block_decoding.cpp:83
uint8_t last
Definition: ht_block_decoding.hpp:54
uint8_t importMagSgnBit()
Definition: ht_block_decoding.cpp:91
uint8_t ctreg
Definition: ht_block_decoding.hpp:58
uint8_t tmp
Definition: ht_block_decoding.hpp:53
const uint8_t * buf
Definition: ht_block_decoding.hpp:55
uint8_t bits
Definition: ht_block_decoding.hpp:52
const uint32_t length
Definition: ht_block_decoding.hpp:56
uint32_t pos
Definition: ht_block_decoding.hpp:51
uint64_t Creg
Definition: ht_block_decoding.hpp:57
void loadByte()
Definition: ht_block_decoding.cpp:71
int32_t decodeMagSgnValue(int32_t m_n, int32_t i_n)
Definition: ht_block_decoding.cpp:116
state_MS_dec(const uint8_t *Dcup, uint32_t Pcup)
Definition: ht_block_decoding.hpp:61
Definition: ht_block_decoding.hpp:117
int32_t ctreg
Definition: ht_block_decoding.hpp:125
uint8_t decodeUExtension(const uint8_t &u_sfx)
Definition: ht_block_decoding.cpp:336
void load_bytes()
Definition: ht_block_decoding.cpp:209
uint8_t decodeUSuffix(const uint8_t &u_pfx)
Definition: ht_block_decoding.cpp:321
uint8_t getVLCbit()
Definition: ht_block_decoding.cpp:255
void decodeCxtVLC(const uint16_t &context, uint8_t(&u_off)[2], uint8_t(&rho)[2], uint8_t(&emb_k)[2], uint8_t(&emb_1)[2], const uint8_t &first_or_second, const uint16_t *dec_CxtVLC_table)
Definition: ht_block_decoding.cpp:271
int32_t pos
Definition: ht_block_decoding.hpp:119
uint8_t bits
Definition: ht_block_decoding.hpp:128
void close32(int32_t num_bits)
Definition: ht_block_decoding.cpp:262
uint64_t Creg
Definition: ht_block_decoding.hpp:126
state_VLC_dec(uint8_t *Dcup, uint32_t Lcup, uint32_t Pcup)
Definition: ht_block_decoding.hpp:132
uint8_t decodeUPrefix()
Definition: ht_block_decoding.cpp:307
uint8_t last
Definition: ht_block_decoding.hpp:120
uint8_t * buf
Definition: ht_block_decoding.hpp:129
const int32_t bitmask32[32]
Definition: ht_block_decoding.hpp:40
void htj2k_decode(j2k_codeblock *block, const uint8_t ROIshift)
Definition: ht_block_decoding.cpp:1076