Grok  9.7.5
ht_block_encoding.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 #include <cstdint>
31 
32 #define MAX_Lcup 16834
33 #define MAX_Scup 4079
34 
35 /********************************************************************************
36  * state_MS_enc: state class for MagSgn encoding
37  *******************************************************************************/
38 class state_MS_enc {
39  private:
40  int32_t pos; // current position in the buffer
41  uint8_t *const buf; // buffer for MagSgn
42 #ifdef MSNAIVE
43  uint8_t bits;
44  uint8_t max;
45  uint8_t tmp;
46 #else
47  uint64_t Creg; // temporal buffer to store up to 4 codewords
48  uint32_t ctreg; // number of used bits in Creg
49  uint8_t last; // last byte in the buffer
50  void emit_dword(); // internal function to emit 4 code words
51 #endif
52 
53  public:
54  explicit state_MS_enc(uint8_t *p)
55  : pos(0),
56  buf(p),
57 #ifdef MSNAIVE
58  bits(0),
59  max(8),
60  tmp(0)
61 #else
62  Creg(0),
63  ctreg(0),
64  last(0)
65 #endif
66  {
67  }
68 #ifdef MSNAIVE
69  void emitMagSgnBits(uint32_t cwd, uint8_t m_n);
70 #else
71  void emitMagSgnBits(uint32_t cwd, uint8_t m_n, uint8_t emb_1);
72 #endif
73  int32_t termMS();
74 };
75 
76 class state_MEL_enc; // forward declaration for friend function "termMELandVLC()"
77 /********************************************************************************
78  * state_VLC_enc: state classe for VLC encoding
79  *******************************************************************************/
81  private:
82  int32_t pos;
83  uint8_t bits;
84  uint8_t tmp;
85  uint8_t last;
86  uint8_t *const buf;
87 
88  friend int32_t termMELandVLC(state_VLC_enc &, state_MEL_enc &);
89 
90  public:
91  explicit state_VLC_enc(uint8_t *p) : pos(MAX_Scup - 2), bits(4), tmp(0xF), last(0xFF), buf(p) {
92  buf[pos + 1] = 0xFF;
93  }
94  void emitVLCBits(uint16_t cwd, uint8_t len);
95 };
96 
97 /********************************************************************************
98  * state_MEL_enc: state class for MEL encoding
99  *******************************************************************************/
101  private:
102  int8_t MEL_k;
103  uint8_t MEL_run;
104  const uint8_t MEL_E[13];
105  uint8_t MEL_t;
106  int32_t pos;
107  uint8_t rem;
108  uint8_t tmp;
109  uint8_t *const buf;
110  void emitMELbit(uint8_t bit);
111 
112  friend int32_t termMELandVLC(state_VLC_enc &, state_MEL_enc &);
113 
114  public:
115  explicit state_MEL_enc(uint8_t *p)
116  : MEL_k(0),
117  MEL_run(0),
118  MEL_E{0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5},
119  MEL_t(1 << MEL_E[MEL_k]),
120  pos(0),
121  rem(8),
122  tmp(0),
123  buf(p) {}
124  void encodeMEL(uint8_t smel);
125  void termMEL();
126 };
Definition: ht_block_encoding.hpp:100
int8_t MEL_k
Definition: ht_block_encoding.hpp:102
const uint8_t MEL_E[13]
Definition: ht_block_encoding.hpp:104
uint8_t *const buf
Definition: ht_block_encoding.hpp:109
int32_t pos
Definition: ht_block_encoding.hpp:106
uint8_t MEL_run
Definition: ht_block_encoding.hpp:103
uint8_t MEL_t
Definition: ht_block_encoding.hpp:105
friend int32_t termMELandVLC(state_VLC_enc &, state_MEL_enc &)
Definition: ht_block_encoding.cpp:385
void encodeMEL(uint8_t smel)
Definition: ht_block_encoding.cpp:229
uint8_t rem
Definition: ht_block_encoding.hpp:107
state_MEL_enc(uint8_t *p)
Definition: ht_block_encoding.hpp:115
void termMEL()
Definition: ht_block_encoding.cpp:259
void emitMELbit(uint8_t bit)
Definition: ht_block_encoding.cpp:218
uint8_t tmp
Definition: ht_block_encoding.hpp:108
Definition: ht_block_encoding.hpp:38
uint64_t Creg
Definition: ht_block_encoding.hpp:47
state_MS_enc(uint8_t *p)
Definition: ht_block_encoding.hpp:54
int32_t termMS()
Definition: ht_block_encoding.cpp:167
void emitMagSgnBits(uint32_t cwd, uint8_t m_n, uint8_t emb_1)
Definition: ht_block_encoding.cpp:142
uint8_t *const buf
Definition: ht_block_encoding.hpp:41
void emit_dword()
Definition: ht_block_encoding.cpp:151
uint8_t last
Definition: ht_block_encoding.hpp:49
uint32_t ctreg
Definition: ht_block_encoding.hpp:48
int32_t pos
Definition: ht_block_encoding.hpp:40
Definition: ht_block_encoding.hpp:80
int32_t pos
Definition: ht_block_encoding.hpp:82
uint8_t last
Definition: ht_block_encoding.hpp:85
uint8_t tmp
Definition: ht_block_encoding.hpp:84
friend int32_t termMELandVLC(state_VLC_enc &, state_MEL_enc &)
Definition: ht_block_encoding.cpp:385
state_VLC_enc(uint8_t *p)
Definition: ht_block_encoding.hpp:91
uint8_t bits
Definition: ht_block_encoding.hpp:83
uint8_t *const buf
Definition: ht_block_encoding.hpp:86
void emitVLCBits(uint16_t cwd, uint8_t len)
Definition: ht_block_encoding.cpp:268
#define MAX_Scup
Definition: ht_block_encoding.hpp:33