-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmitic_coding.cpp
More file actions
376 lines (317 loc) · 12.2 KB
/
arithmitic_coding.cpp
File metadata and controls
376 lines (317 loc) · 12.2 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include "arithmitic_coding.h"
#include "my_utils.h"
#include <list>
#include <iostream>
#include <vector>
#include <cmath>
#include <ctime>
#define TWO_POWER_16 65536
#define TWO_POWER_MINUS_16 1.52587890625e-05
#define TWO_POWER_24 1.67772e+07
#define TWO_POWER_MINUS_24 5.96046e-08
#define TWO_POWER_32 4.29497e+09
#define TWO_POWER_MINUS_32 2.32831e-10
#define TWO_POWER_60 1.15292e+18
#define TWO_POWER_MINUS_60 8.67362e-19
#define TWO_POWER_53 9.0072e+15
#define TWO_POWER_MINUS_53 1.11022e-16
#define BINARY_32_ONES 4294967295
#define BINARY_1_SHIFTED_LEFT_32_TIMES 4294967296l /*(1<<32)*/
#define BINARY_31_ONES 2147483647
void getFreq(std::list<Node *> nodes, int *dictFreq, int &maxFreq)
{
for (Node *child : nodes)
{
int currentFreq = child->freq;
dictFreq[child->index] = currentFreq;
if (currentFreq > maxFreq)
maxFreq = currentFreq;
if (!child->getChildren().empty())
getFreq(child->getChildren(), dictFreq, maxFreq);
}
}
int *dictionaryFreq(DictionaryTree &dictionary, int &maxFreq)
{
int *dictFreq = new int[dictionary.size];
for (int i = 0; i < dictionary.size; i++)
{
dictFreq[i] = 0;
}
maxFreq = 0;
getFreq(dictionary.getChildren(), dictFreq, maxFreq);
/*int totFreq = 0;
for (int i = 0; i < dictionary.size; i++) {
totFreq += dictFreq[i];
}
std::cout << totFreq << '\n';*/
return dictFreq;
}
long double *ranges(int* freq, int freqSize, int encodedStreamSize)
{
long double *dictRanges = new long double[freqSize];
dictRanges[0] = (long double)freq[0] / encodedStreamSize;
for (int i = 1; i < freqSize; i++)
{
dictRanges[i] = dictRanges[i - 1] + (long double)freq[i] / encodedStreamSize;
}
return dictRanges;
}
std::vector<int> generateDiffEncodedStream(std::list<int> &encodedStream, int &min, int &max)
{
std::vector<int> diffEncodedStream;
int j = 0;
int iBefore = encodedStream.front();
min = encodedStream.front();
max = min;
for (int &i : encodedStream)
{
if (j == 0)
{
diffEncodedStream.push_back(i);
j++;
continue;
}
int diff = i - iBefore;
diffEncodedStream.push_back(diff);
if (diff < min)
min = diff;
else if (diff > max)
max = diff;
iBefore = i;
j++;
}
return diffEncodedStream;
}
int *generateDiffFreq(std::vector<int> &diffEncodedStream, int min, int max, int& arraySize)
{
arraySize = max - min;
int *diffFreq = new int[arraySize];
for (int i = 0; i < arraySize; i++)
{
diffFreq[i] = 0;
}
//int actualSize = 0;
for (int& i : diffEncodedStream)
{
diffFreq[i - min] += 1;
}
return diffFreq;
}
void add_to_buffer(bool bit,
int num,
long long &buffer,
int &buffer_size,
std::list<long long> &encoded_message)
{
if (num <= 0)
return;
if (buffer_size + num >= 64)
{
int current_num = 64 - buffer_size;
if (bit)
// print('adding', current_num, 'ones to buffer')
buffer = (buffer << current_num) ^ ((1 << current_num) - 1);
else
// print('adding', current_num, 'zeros to buffer')
buffer = (buffer << current_num);
encoded_message.push_back(buffer);
buffer_size = 0;
buffer = 0;
add_to_buffer(bit, num - current_num, buffer, buffer_size, encoded_message);
}
else
{
// print('bit', bit)
if (bit)
// print('adding', num, 'ones to buffer')
buffer = (buffer << num) ^ ((1 << num) - 1);
else
// print('adding', num, 'zeros to buffer')
buffer = (buffer << num);
// print('buffer_size before', buffer_size)
buffer_size = buffer_size + num;
// print('buffer_size after', buffer_size)
}
}
bool encode_bit(long long ¤t_low_bin,
long long ¤t_high_bin,
long long &buffer,
int &buffer_size,
std::list<long long> &encoded_message)
{
// shift Low to the left once
// print('current_low_bin before shift\t\t', format(current_low_bin, '033b'))
current_low_bin = current_low_bin << 1;
// print('current_low_bin after shift\t\t', format(current_low_bin, '033b'))
// shift High to the left once but the LSB = 1
// print('current_high_bin before shift\t\t', format(current_high_bin, '033b'))
current_high_bin = (current_high_bin << 1) ^ 1;
// print('current_high_bin after shift\t\t', format(current_high_bin, '033b'))
// select the 33th bit
bool encoded_bit = (bool)(BINARY_1_SHIFTED_LEFT_32_TIMES & current_low_bin);
// buffer_size = buffer_size+1
if (encoded_bit)
{
// print('1 to encoded stream')
// shift buffer to the left once but the LSB = 1
add_to_buffer(encoded_bit, 1, buffer, buffer_size, encoded_message);
// remove the 33th bit from Low & High
current_low_bin = current_low_bin & (BINARY_32_ONES);
// print('current_low_bin after removing 33th bit\t', format(current_low_bin, '032b'))
current_high_bin = current_high_bin & (BINARY_32_ONES);
// print('current_high_bin after removing 33th bit', format(current_high_bin, '032b'))
}
else
{
// print('0 to encoded stream')
add_to_buffer(encoded_bit, 1, buffer, buffer_size, encoded_message);
}
/*if buffer_size >= 64:
encoded_message = encoded_message + buffer*/
return encoded_bit;
}
void get_low_high_bin(long double current_low,
long double current_high,
long long ¤t_low_bin,
long long ¤t_high_bin)
{
// *TWO_POWER_32 to shift left 32 times in binary
current_low_bin = current_low * TWO_POWER_32;
if (current_high >= 1)
{
current_high_bin = current_high * TWO_POWER_32 - 1;
}
else
{
current_high_bin = current_high * TWO_POWER_32;
}
}
void squeeze_out_2nd_MSB(long long ¤t_low_bin, long long ¤t_high_bin)
{
// std::cout << ('current_low_bin before squeezing out the 31th bit\t', format(current_low_bin, '032b'))
// std::cout << ('current_high_bin before squeezing out the 31th bit\t', format(current_high_bin, '032b'))
// shift Low left then make the 32th bit 0 again
current_low_bin = (current_low_bin << 1) & BINARY_31_ONES;
// shift High left (LSB = 1) then make the 32th bit 1 again
current_high_bin = (((current_high_bin << 1) & (BINARY_32_ONES)) | (1 << 31)) ^ 1;
// std::cout << ('current_low_bin after squeezing out the 31th bit\t', format(current_low_bin, '032b'))
// std::cout << ('current_high_bin before squeezing out the 31th bit\t', format(current_high_bin, '032b'))
// std::cout << ()
}
std::list<long long /*64bit*/> arithmitic_encoding(std::list<int> &message, /*DictionaryTree& dictionary, int *dictFreq,*/ int &lastBufferSize)
{
std::cout << "Arithmitic encoding....." << '\n';
std::list<long long> encoded_message;
long long buffer = 0;
int buffer_size = 0;
int underflow_counter = 0;
std::cout << "Calculating diff....." << '\n';
int min, max;
std::vector<int> diffEncodedStream = generateDiffEncodedStream(message, min, max);
std::cout << "Generating diff freq....." << '\n';
int diffFreqSize;
int* diffFreq = generateDiffFreq(diffEncodedStream, min, max, diffFreqSize);
/*std::cout << "Generating frequencies....." << '\n';
int maxFreq;
dictFreq = dictionaryFreq(dictionary, maxFreq);*/
std::cout << "Generating diff ranges....." << '\n';
long double* diffRanges = ranges(diffFreq, diffFreqSize, diffEncodedStream.size());
std::cout << "Encoding....." << '\n';
long double current_low = 0;
long double current_high = 1.0;
int j = 0;
// std::cout << (message);
for (int &i : message)
{
// std::cout << (i)
long double cl = current_low;
long double d = current_high - cl;
if (i > 0)
current_low = cl + d * diffRanges[i - 1];
else
current_low = 0;
current_high = cl + d * diffRanges[i];
// std::cout << ('current low\t', current_low)
// std::cout << ('current high\t', current_high)
long long current_low_bin, current_high_bin;
get_low_high_bin(current_low, current_high, current_low_bin, current_high_bin);
// std::cout << ('current low\t', format(int(current_low_bin), '032b'))
// std::cout << ('current high\t', format(int(current_high_bin), '032b'))
// MSBs equality
// std::cout << ('eq cond\t\t', current_low_bin >> 31, current_high_bin >> 31)
if (current_low_bin >> 31 == current_high_bin >> 31)
{
bool encoded_bit = encode_bit(current_low_bin, current_high_bin, buffer, buffer_size, encoded_message);
if (underflow_counter > 0)
{
// std::cout << (encoded_bit)
if (encoded_bit)
{
add_to_buffer(0, underflow_counter, buffer, buffer_size, encoded_message);
// std::cout << ('0'*underflow_counter, 'to encoded stream (Underflow)')
}
else
{
add_to_buffer(1, underflow_counter, buffer, buffer_size, encoded_message);
// std::cout << ('1'*underflow_counter, 'to encoded stream (Underflow)')
}
// std::cout << ()
// buffer_size = buffer_size+underflow_counter
underflow_counter = 0;
}
// MSBs equality
while (current_low_bin >> 31 == current_high_bin >> 31)
{
// std::cout << ('eq cond\t\t', current_low_bin >> 31, current_high_bin >> 31)
encoded_bit = encode_bit(current_low_bin, current_high_bin, buffer, buffer_size, encoded_message);
}
}
else if ((current_low_bin >> 30 ^ current_high_bin >> 30) == 0b11 and current_low_bin >> 30 == 1)
{
// Underflow
// 31th bit equality
// std::cout << ('Underflow')
underflow_counter = underflow_counter + 1;
squeeze_out_2nd_MSB(current_low_bin, current_high_bin);
while (((current_low_bin >> 30 ^ current_high_bin >> 30) == 0b11) & (current_low_bin >> 30 == 1))
{
underflow_counter = underflow_counter + 1;
squeeze_out_2nd_MSB(current_low_bin, current_high_bin);
}
}
current_low = current_low_bin * TWO_POWER_MINUS_32;
if (current_high_bin >= 0b11111111111111111111111111111111)
{
current_high = 1.0;
}
else
{
current_high = (current_high_bin)*TWO_POWER_MINUS_32;
}
/*std::cout << ('next low\t', current_low)
std::cout << ('next high\t', current_high)
std::cout << ('next low\t', format(current_low_bin, '032b'))
std::cout << ('next high\t', format(current_high_bin, '032b'))
std::cout << ('buffer', format(buffer, '0' + str(buffer_size) + 'b'), 'size', buffer_size)
std::cout << ('============================================================================================')
std::cout << ()*/
j++;
if (j % 100000 == 0)
PrintProgressBar((float)j / message.size());
}
// final bits
// current_low_bin, current_high_bin = get_low_high_bin(current_low, current_high)
add_to_buffer(1, 1, buffer, buffer_size, encoded_message);
// std::cout << ('last_buffer_size', buffer_size)
/*current_low_bin = current_low_bin << 1
current_high_bin = current_high_bin << 1
while ((current_low_bin >> 31) ^ (current_high_bin >> 31)) & 1:
buffer, buffer_size, encoded_message = add_to_buffer(1, 1, buffer, buffer_size, encoded_message)
current_low_bin = current_low_bin << 1
current_high_bin = current_high_bin << 1*/
if (buffer_size > 0)
encoded_message.push_back(buffer);
PrintProgressBar(1);
lastBufferSize = buffer_size;
return encoded_message;
}