/
arithmetic_decoding.c
192 lines (170 loc) · 4.45 KB
/
arithmetic_decoding.c
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
/*
* Arithmetic decoding
*
* Copyright (C) 2012 Vincenzo Maffione
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bitbuffer.h"
#include "cumulative_sums_tree.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define min(a,b) ((a) < (b)) ? (a) : (b)
#define NUM_CODEPOINTS 257 // compatible with ASCII encoding
uint64_t total_count; // current total symbols counter
uint64_t LOW, HIGH;
static int bs(struct CST* CST_p, uint64_t x)
{
int l, m, h;
uint64_t ncs_0, ncs_1, ratio;
l = 0;
h = NUM_CODEPOINTS;
ratio = (HIGH - LOW) / total_count;
for (;;)
{
m = (l + h) / 2;
ncs_0 = cumulative_sums_lookup(CST_p, m) * ratio + LOW;
ncs_1 = cumulative_sums_lookup(CST_p, m + 1) * ratio + LOW;
if (m == NUM_CODEPOINTS-1)
{
if (ncs_0 <= x)
break;
}
else if (ncs_0 <= x && x < ncs_1)
break;
if (x < ncs_0)
h = m;
else
l = m + 1;
if (l >= h)
break;
}
if (l >= h)
return -1;
return m;
}
int arithmetic_decoding(const char* encoded_file, const char* decoded_file)
{
FILE* fout;
uint8_t byte;
int i;
int i_ss;
int i_d;
int n;
int bitc = 0;
uint64_t tmp, mask, stream_slice;
int bits_to_read;
struct CST* cumulative_sums_tree_pointer;
struct BitBuffer* bbp = bit_open(encoded_file, BIT_BUFFER_READ);
if (bbp == NULL)
{
printf("%s: No such file\n", encoded_file);
return -1;
}
cumulative_sums_tree_pointer = CST_create(NUM_CODEPOINTS);
if (cumulative_sums_tree_pointer == NULL)
{
printf("Error: CST creation failed\n");
bit_close(bbp);
return -1;
}
fout = fopen(decoded_file, "wb");
if (fout == NULL)
{
printf("Error: cannot create output file %s\n", decoded_file);
return -1;
}
// we deal with the zero-frequency problem initializing all counters to 1 and consequently NUM_CODEPOINTS to total_count
total_count = NUM_CODEPOINTS;
for (i=1; i<NUM_CODEPOINTS; i++)
counter_increment(cumulative_sums_tree_pointer, i);
LOW = 0;
HIGH = 0xFFFFFFFFFFFFFFFF;
bits_to_read = sizeof(uint64_t) * 8;
stream_slice = 0x0000000000000000;
for (;;)
{
n = bit_read(bbp, &tmp, bits_to_read);
if (n == -1)
{
printf("Error occurred!!\n");
return -1;
}
// here n could be zero, but this is not a concern
bitc+=n;
if (bits_to_read == sizeof(uint64_t) * 8)
mask = 0xFFFFFFFFFFFFFFFF;
else
mask = ((uint64_t)1 << bits_to_read) - 1;
stream_slice = ((stream_slice << bits_to_read) | (tmp & mask));
/*
i = 0;
mask = 0x0000000000000001;
while (i < bits_to_read)
{
stream_slice <<= 1;
if (tmp & mask)
stream_slice |= 0x0000000000000001;
mask <<= 1;
i++;
} */
n = bs(cumulative_sums_tree_pointer, stream_slice);
if (n == -1)
return -1;
if (n == NUM_CODEPOINTS - 1) // EOF symbol received
break;
byte = (uint8_t)n;
fwrite(&byte, 1, 1, fout);
tmp = ((HIGH - LOW) / total_count);
HIGH = LOW + tmp * (cumulative_sums_lookup(cumulative_sums_tree_pointer, byte + 1));
LOW = LOW + tmp * cumulative_sums_lookup(cumulative_sums_tree_pointer, byte);
// deadlock resolution
if ((HIGH & 0xC000000000000000) == 0x8000000000000000 && (LOW & 0xC000000000000000) == 0x4000000000000000)
{
i_d = 2;
mask = 0x2000000000000000;
while (mask && (LOW & mask) && (HIGH & mask) == 0)
{
i_d++;
mask >>= 1;
}
LOW &= ~mask;
HIGH |= mask;
HIGH <<= i_d;
LOW <<= i_d;
}
else
i_d = 0;
// stream scaling
tmp = LOW ^ HIGH;
i_ss = 0;
mask = 0x8000000000000000;
while (mask && (tmp & mask) == 0)
{
i_ss++;
mask >>= 1;
}
bits_to_read = i_ss + i_d; // for the next cycle
LOW <<= i_ss;
HIGH <<= i_ss;
// adaptive update
counter_increment(cumulative_sums_tree_pointer, byte + 1);
total_count++;
}
bit_close(bbp);
CST_destroy(cumulative_sums_tree_pointer);
fclose(fout);
return 0;
}