-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoder.rs
196 lines (188 loc) · 6.52 KB
/
coder.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
pub mod qmcoder {
#[allow(non_snake_case)]
#[derive(Debug)]
struct QMstatus {
State:u8,
qcHex:u32,
qcDec:f32,
In:u8, // increase
De:u8, // decrease
}
impl QMstatus {
pub fn new(State:u8, qcHex:u32, qcDec:f32, In:u8, De:u8)->QMstatus {
QMstatus {
State,
qcHex,
qcDec,
In,
De,
}
}
}
#[allow(non_snake_case)]
#[derive(Debug, Clone)]
pub struct Encoder {
qm_table: Vec<QMstatus>,
state:u8,
Qc:u32,
A:u32 ,
c:u32,
CT:u8, // C&A left shift count
SC:u8, // stack count
BP:u32,
MPS:bool,
LPS:bool,
}
impl Encoder {
pub fn new()-> Encoder {
Encoder {
qm_table: Vec::new(),
state:0,
Qc: 0x59EB,
A: 0x10000,
c: 0x8000,
CT: 11,
SC: 0,
BP: 0,
MPS: true,
LPS: false,
}
}
// read qt table to Encoder structure
pub fn read_QT_table(mut self) {
use std::io::{BufRead, BufReader};
use std::fs::File;
let reader = BufReader::new(File::open("src/qmtable.txt").expect("Cannot open qmtable.txt"));
let mut cnt = 0;
let mut state:u8 = 0;
let mut qcHex:u32 = 0;
let mut qcDec:f32 = 0.0;
let mut In:u8 = 0;
let mut De:u8 = 0;
for line in reader.lines() {
for word in line.unwrap().split_whitespace() {
// println!("word '{}' cnt:{}", word, cnt);
match cnt {
0 => { state = word.parse::<u8>().unwrap(); }
1 => { qcHex = u32::from_str_radix(word, 16).unwrap(); }
2 => { qcDec = word.parse::<f32>().unwrap(); }
3 => { In = word.parse::<u8>().unwrap(); }
4 => {
match word.parse::<u8>() {
Ok(n) => De = n,
Err(e) => {
println!("ErrorMsg: {}",e);
De = 0; // change 's' value to 0
},
};
let tmp = QMstatus::new(state, qcHex, qcDec, In, De);
// println!("{:?}",&tmp);
self.qm_table.push(tmp);
}
_ => {panic!("Unexpected error at match cnt!");}
}
cnt = (cnt + 1)%5;
}
}
}
pub fn change_state(mut self, isInc:bool) {
for i in self.qm_table {
let n:u8;
if i.qcHex == self.Qc {
if isInc { n = i.In; }
else { n = i.De; }
// we haved change 's' value to 0
if n == 0 {
let tmp = self.LPS;
self.LPS = self.MPS;
self.MPS = tmp;
}
else {
if isInc { self.state = i.State + n; }
else { self.state = i.State - n; }
for j in self.qm_table.Clone() {
if self.state == j.State {
self.Qc = i.qcHex;
}
}
}
}
}
}
pub fn renormalize(mut self, result:&Vec<u8>){
while self.A < 0x8000 {
self.A <<= 1;
self.c <<= 1;
self.CT -= 1;
if self.CT == 0 {
// byte out
let t = self.c >> 19;
if t > 0xff {
self.BP += 1;
// stuff 0
if self.BP == 0xff {
// result += '{0:b}'.format(self.BP)
self.BP = 0;
}
// output stacked zeros
while self.SC > 0 {
//result += '{0:b}'.format(self.BP)
self.BP = 0;
self.SC -= 1;
}
//result += '{0:b}'.format(self.BP)
self.BP = t;
}
else {
if t == 0xff {
self.SC += 1;
} else {
while self.SC > 0 {
// result += '{0:b}'.format(self.BP)
self.BP = 0xff;
// result += '{0:b}'.format(self.BP)
self.BP = 0;
self.SC -= 1;
}
// result += '{0:b}'.format(self.BP)
self.BP = t;
}
}
self.c &= 0x7ffff;
self.CT = 8;
}
}
//result
}
pub fn encode(mut self, bit_string: Vec<bool>)->Vec<u8> {
let mut result = Vec::new();
for bit in bit_string {
if bit == self.MPS {
self.A = self.A - self.Qc;
if self.A < 0x8000 {
if self.A < self.Qc {
self.c += self.A;
self.A = self.Qc;
}
// change Qn stage
// self.change_state();
// renormalize
// result = self.renormalize(result);
}
}
if bit == self.LPS {
self.A = self.A - self.Qc;
if self.A >= self.Qc {
self.c += self.A;
self.A = self.Qc;
}
// change Qn stage
// self.change_state();
// renormalize
// self.renormalize();
}
}
result
}
}
}