-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy patharithmeticmodel.cpp
More file actions
220 lines (189 loc) · 7.24 KB
/
arithmeticmodel.cpp
File metadata and controls
220 lines (189 loc) · 7.24 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
/*
===============================================================================
FILE: arithmeticmodel.cpp
CONTENTS:
A modular C++ wrapper for an adapted version of Amir Said's FastAC Code.
see: http://www.cipr.rpi.edu/~said/FastAC.html
PROGRAMMERS:
info@rapidlasso.de - https://rapidlasso.de
COPYRIGHT:
(c) 2007-2022, rapidlasso GmbH - fast tools to catch reality
This is free software; you can redistribute and/or modify it under the
terms of the Apache Public License 2.0 published by the Apache Software
Foundation. See the COPYING file for more information.
This software is distributed WITHOUT ANY WARRANTY and without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
CHANGE HISTORY:
see header file
===============================================================================
*/
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// -
// Fast arithmetic coding implementation -
// -> 32-bit variables, 32-bit product, periodic updates, table decoding -
// -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// -
// Version 1.00 - April 25, 2004 -
// -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// -
// WARNING -
// ========= -
// -
// The only purpose of this program is to demonstrate the basic principles -
// of arithmetic coding. It is provided as is, without any express or -
// implied warranty, without even the warranty of fitness for any particular -
// purpose, or that the implementations are correct. -
// -
// Permission to copy and redistribute this code is hereby granted, provided -
// that this warning and copyright notices are not removed or altered. -
// -
// Copyright (c) 2004 by Amir Said (said@ieee.org) & -
// William A. Pearlman (pearlw@ecse.rpi.edu) -
// -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// -
// A description of the arithmetic coding method used here is available in -
// -
// Lossless Compression Handbook, ed. K. Sayood -
// Chapter 5: Arithmetic Coding (A. Said), pp. 101-152, Academic Press, 2003 -
// -
// A. Said, Introduction to Arithetic Coding Theory and Practice -
// HP Labs report HPL-2004-76 - http://www.hpl.hp.com/techreports/ -
// -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#include "arithmeticmodel.hpp"
#include <stdio.h>
#include <stdlib.h>
ArithmeticModel::ArithmeticModel(U32 symbols, BOOL compress)
{
this->symbols = symbols;
this->compress = compress;
distribution = 0;
decoder_table = 0;
last_symbol = 0;
symbols_until_update = 0;
symbol_count = 0;
table_shift = 0;
table_size = 0;
total_count = 0;
update_cycle = 0;
}
ArithmeticModel::~ArithmeticModel()
{
if (distribution) delete [] distribution;
}
I32 ArithmeticModel::init(U32* table)
{
if (distribution == 0)
{
if ( (symbols < 2) || (symbols > (1 << 11)) )
{
return -1; // invalid number of symbols
}
last_symbol = symbols - 1;
if ((!compress) && (symbols > 16))
{
U32 table_bits = 3;
while (symbols > (1U << (table_bits + 2))) ++table_bits;
table_size = 1 << table_bits;
table_shift = DM__LengthShift - table_bits;
distribution = new U32[2*symbols+table_size+2];
decoder_table = distribution + 2 * symbols;
}
else // small alphabet: no table needed
{
decoder_table = 0;
table_size = table_shift = 0;
distribution = new U32[2*symbols];
}
if (distribution == 0)
{
return -1; // "cannot allocate model memory");
}
symbol_count = distribution + symbols;
}
total_count = 0;
update_cycle = symbols;
if (table)
for (U32 k = 0; k < symbols; k++) symbol_count[k] = table[k];
else
for (U32 k = 0; k < symbols; k++) symbol_count[k] = 1;
update();
symbols_until_update = update_cycle = (symbols + 6) >> 1;
return 0;
}
void ArithmeticModel::update()
{
// halve counts when a threshold is reached
if ((total_count += update_cycle) > DM__MaxCount)
{
total_count = 0;
for (U32 n = 0; n < symbols; n++)
{
total_count += (symbol_count[n] = (symbol_count[n] + 1) >> 1);
}
}
// compute cumulative distribution, decoder table
U32 k, sum = 0, s = 0;
U32 scale = 0x80000000U / total_count;
if (compress || (table_size == 0))
{
for (k = 0; k < symbols; k++)
{
distribution[k] = (scale * sum) >> (31 - DM__LengthShift);
sum += symbol_count[k];
}
}
else
{
for (k = 0; k < symbols; k++)
{
distribution[k] = (scale * sum) >> (31 - DM__LengthShift);
sum += symbol_count[k];
U32 w = distribution[k] >> table_shift;
while (s < w) decoder_table[++s] = k - 1;
}
decoder_table[0] = 0;
while (s <= table_size) decoder_table[++s] = symbols - 1;
}
// set frequency of model updates
update_cycle = (5 * update_cycle) >> 2;
U32 max_cycle = (symbols + 6) << 3;
if (update_cycle > max_cycle) update_cycle = max_cycle;
symbols_until_update = update_cycle;
}
ArithmeticBitModel::ArithmeticBitModel()
{
init();
}
ArithmeticBitModel::~ArithmeticBitModel()
{
}
void ArithmeticBitModel::init()
{
// initialization to equiprobable model
bit_0_count = 1;
bit_count = 2;
bit_0_prob = 1U << (BM__LengthShift - 1);
// start with frequent updates
update_cycle = bits_until_update = 4;
}
void ArithmeticBitModel::update()
{
// halve counts when a threshold is reached
if ((bit_count += update_cycle) > BM__MaxCount)
{
bit_count = (bit_count + 1) >> 1;
bit_0_count = (bit_0_count + 1) >> 1;
if (bit_0_count == bit_count) ++bit_count;
}
// compute scaled bit 0 probability
U32 scale = 0x80000000U / bit_count;
bit_0_prob = (bit_0_count * scale) >> (31 - BM__LengthShift);
// set frequency of model updates
update_cycle = (5 * update_cycle) >> 2;
if (update_cycle > 64) update_cycle = 64;
bits_until_update = update_cycle;
}