-
Notifications
You must be signed in to change notification settings - Fork 33
/
arithmetic_coding_in_fixed_bits.pl
204 lines (151 loc) · 4.58 KB
/
arithmetic_coding_in_fixed_bits.pl
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
#!/usr/bin/perl
# Author: Trizen
# Date: 12 July 2023
# Edit: 05 February 2024
# https://github.com/trizen
# The Arithmetic Coding algorithm, implemented using native integers.
# References:
# Data Compression (Summer 2023) - Lecture 15 - Infinite Precision in Finite Bits
# https://youtube.com/watch?v=EqKbT3QdtOI
#
# Basic arithmetic coder in C++
# https://github.com/billbird/arith32
use 5.036;
use List::Util qw(max);
use constant BITS => 32;
use constant MAX => oct('0b' . ('1' x BITS));
sub create_cfreq ($freq) {
my @cf;
my $T = 0;
foreach my $i (sort { $a <=> $b } keys %$freq) {
$freq->{$i} // next;
$cf[$i] = $T;
$T += $freq->{$i};
$cf[$i + 1] = $T;
}
return (\@cf, $T);
}
sub encode ($string) {
my $enc = '';
my @bytes = unpack('C*', $string);
my $EOF_SYMBOL = (max(@bytes) // 0) + 1;
push @bytes, $EOF_SYMBOL;
my %freq;
++$freq{$_} for @bytes;
my ($cf, $T) = create_cfreq(\%freq);
if ($T > MAX) {
die "Too few bits: $T > ${\MAX}";
}
my $low = 0;
my $high = MAX;
my $uf_count = 0;
foreach my $c (@bytes) {
my $w = $high - $low + 1;
$high = ($low + int(($w * $cf->[$c + 1]) / $T) - 1) & MAX;
$low = ($low + int(($w * $cf->[$c]) / $T)) & MAX;
if ($high > MAX) {
die "high > MAX: $high > ${\MAX}";
}
if ($low >= $high) { die "$low >= $high" }
while (1) {
if (($high >> (BITS - 1)) == ($low >> (BITS - 1))) {
my $bit = $high >> (BITS - 1);
$enc .= $bit;
if ($uf_count > 0) {
$enc .= join('', 1 - $bit) x $uf_count;
$uf_count = 0;
}
$low <<= 1;
($high <<= 1) |= 1;
}
elsif (((($low >> (BITS - 2)) & 0x1) == 1) && ((($high >> (BITS - 2)) & 0x1) == 0)) {
($high <<= 1) |= (1 << (BITS - 1));
$high |= 1;
($low <<= 1) &= ((1 << (BITS - 1)) - 1);
++$uf_count;
}
else {
last;
}
$low &= MAX;
$high &= MAX;
}
}
$enc .= '0';
$enc .= '1';
while (length($enc) % 8 != 0) {
$enc .= '1';
}
return ($enc, \%freq);
}
sub decode ($bits, $freq) {
open my $fh, '<:raw', \$bits;
my ($cf, $T) = create_cfreq($freq);
my $dec = '';
my $low = 0;
my $high = MAX;
my $enc = oct('0b' . join '', map { getc($fh) // 1 } 1 .. BITS);
my @table;
foreach my $i (sort { $a <=> $b } keys %$freq) {
foreach my $j ($cf->[$i] .. $cf->[$i + 1] - 1) {
$table[$j] = $i;
}
}
my $EOF_SYMBOL = max(keys %$freq) // 0;
while (1) {
my $w = $high - $low + 1;
my $ss = int((($T * ($enc - $low + 1)) - 1) / $w);
my $i = $table[$ss] // last;
last if ($i == $EOF_SYMBOL);
$dec .= chr($i);
$high = ($low + int(($w * $cf->[$i + 1]) / $T) - 1) & MAX;
$low = ($low + int(($w * $cf->[$i]) / $T)) & MAX;
if ($high > MAX) {
die "error";
}
if ($low >= $high) { die "$low >= $high" }
while (1) {
if (($high >> (BITS - 1)) == ($low >> (BITS - 1))) {
($high <<= 1) |= 1;
$low <<= 1;
($enc <<= 1) |= (getc($fh) // 1);
}
elsif (((($low >> (BITS - 2)) & 0x1) == 1) && ((($high >> (BITS - 2)) & 0x1) == 0)) {
($high <<= 1) |= (1 << (BITS - 1));
$high |= 1;
($low <<= 1) &= ((1 << (BITS - 1)) - 1);
$enc = (($enc >> (BITS - 1)) << (BITS - 1)) | (($enc & ((1 << (BITS - 2)) - 1)) << 1) | (getc($fh) // 1);
}
else {
last;
}
$low &= MAX;
$high &= MAX;
$enc &= MAX;
}
}
return $dec;
}
my $str = "ABRACADABRA AND A VERY SAD SALAD";
if (@ARGV) {
if (-f $ARGV[0]) {
$str = do {
open my $fh, '<:raw', $ARGV[0];
local $/;
<$fh>;
};
}
else {
$str = $ARGV[0];
}
}
my ($enc, $freq) = encode($str);
say $enc;
say "Encoded bytes length: ", length($enc) / 8;
my $dec = decode($enc, $freq);
say $dec;
$str eq $dec or die "Decoding error: ", length($str), ' <=> ', length($dec);
__END__
0100110110111110100000000100000111110000110110011111000010110011011001000101100011011101001110000000010001111111
Encoded bytes length: 14
ABRACADABRA AND A VERY SAD SALAD