-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_rc.h
154 lines (125 loc) · 3.44 KB
/
sub_rc.h
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
#pragma once
// *******************************************************************************************
// This file is a part of FaStore software distributed under GNU GPL 2 licence.
// The code in this file is based on ORCOM software.
// Range coder algorithm based on 'Carryless rangecoder' by Dmitry Subbotin
// Github : https://github.com/refresh-bio/FaStore
// Authors: Lukasz Roguski, Idoia Ochoa, Mikel Hernaez & Sebastian Deorowicz
// *******************************************************************************************
#ifndef H_RANGECODER
#define H_RANGECODER
#include <cstdint>
#include "defs.h"
#include <assert.h>
// *******************************************************************************************
//
// *******************************************************************************************
template<typename T_IO_STREAM> class CBasicRangeCoder
{
};
// *******************************************************************************************
//
// *******************************************************************************************
template<typename T_IO_STREAM> class CRangeEncoder : public CBasicRangeCoder<T_IO_STREAM>
{
public:
typedef uint64_t Code;
typedef uint32_t Freq;
static const uint32_t CodeBits = 32;
static const Freq TopValue = 0x00ffffffULL;
static const Code Mask64 = 0xff00000000000000ULL;
static const Freq Mask32 = 0xffffffffULL;
T_IO_STREAM &io_stream;
Code low;
Freq range;
CRangeEncoder(T_IO_STREAM& _io_stream) : io_stream(_io_stream), low(0), range(0)
{}
void Start()
{
low = 0;
range = Mask32;
}
void EncodeFrequency(Freq symFreq_, Freq cumFreq_, Freq totalFreqSum_)
{
assert(range > totalFreqSum_);
range /= totalFreqSum_;
low += range * cumFreq_;
range *= symFreq_;
while (range <= TopValue)
{
assert(range != 0);
if ((low ^ (low + range)) & Mask64)
{
Freq r = (Freq)low;
range = (r | TopValue) - r;
}
io_stream.PutByte(low >> 56);
low <<= 8, range <<= 8;
}
}
void End()
{
for (int i = 0; i < 8; i++)
{
io_stream.PutByte(low >> 56);
low <<= 8;
}
}
};
// *******************************************************************************************
//
// *******************************************************************************************
template<typename T_IO_STREAM> class CRangeDecoder : public CBasicRangeCoder<T_IO_STREAM>
{
public:
typedef uint64_t Code;
typedef uint32_t Freq;
static const uint32_t CodeBits = 32;
static const Freq TopValue = 0x00ffffffULL;
static const Code Mask64 = 0xff00000000000000ULL;
static const Freq Mask32 = 0xffffffffULL;
T_IO_STREAM &io_stream;
Code low;
Freq range;
CRangeDecoder(T_IO_STREAM &_io_stream) : io_stream(_io_stream), low(0), range(0)
{
buffer = 0;
}
void Start()
{
buffer = 0;
for (uint32_t i = 1; i <= 8; ++i)
{
buffer |= (Code)io_stream.GetByte() << (64 - i * 8);
}
low = 0;
range = Mask32;
}
Freq GetCumulativeFreq(Freq totalFreq_)
{
assert(totalFreq_ != 0);
return (Freq) (buffer / (range /= totalFreq_));
}
void UpdateFrequency(Freq symFreq_, Freq lowEnd_, Freq /*totalFreq_*/)
{
Freq r = lowEnd_*range;
buffer -= r;
low += r;
range *= symFreq_;
while (range <= TopValue)
{
if ((low ^ (low + range)) & Mask64)
{
Freq r = (Freq)low;
range = (r | TopValue) - r;
}
buffer = (buffer << 8) + io_stream.GetByte();
low <<= 8, range <<= 8;
}
}
void End()
{}
private:
Code buffer;
};
#endif // H_RANGECODER