-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharithmetic_decoder.h
More file actions
100 lines (78 loc) · 2.13 KB
/
arithmetic_decoder.h
File metadata and controls
100 lines (78 loc) · 2.13 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
#ifndef ARITHMETIC_DECODER_H
#define ARITHMETIC_DECODER_H
#include <math.h>
#include "common.h"
#ifdef WINDOWS
#include "file_map.h"
#else
#include "u_file_map.h"
#endif
#include "model.h"
//#pragma warning(push, 4)
////////////////////////////////////////////////////////
class arithmetic_decoder
{
private:
file_map<bit>* m_input;
const DWORD top_value;
DWORD l, u, tag;
arithmetic_decoder& operator=(const arithmetic_decoder&);
arithmetic_decoder(const arithmetic_decoder& );
public:
arithmetic_decoder(file_map<bit>* fm): top_value(0xFFFFFFFF),
l(0),
u(top_value),
tag(0)
{
m_input = fm;
for (size_t i = 0; i < (sizeof(tag) * 8); ++i)
{
tag <<= 1;
tag += m_input->read_next();
}
}
inline void decode(DWORD lower_bound, DWORD upper_bound, DWORD total)
{
bool eql_cond, e3_cond;
QWORD range = (QWORD)u - l + 1;
ASSERT(l < u);
ASSERT((lower_bound <= upper_bound) && (upper_bound <= total));
u = l + (DWORD)(((range * upper_bound) / total) - 1);
l = l + (DWORD)((range * lower_bound) / total);
while( (eql_cond = (MSB(u) == MSB(l))) ||
(e3_cond = ((M2SB(l) == 1) && (M2SB(u) == 0))) )
{
if(eql_cond)
{
l <<= 1;
u <<= 1;
tag <<= 1;
u += 1;
bit b = m_input->read_next();
ASSERT (((tag + b) == tag + 1) || ((tag + b) == tag));
tag += b;
}
else if (e3_cond)
{
l <<= 1;
u <<= 1;
tag <<= 1;
u += 1;
bit b = m_input->read_next();
ASSERT (((tag + b) == tag + 1) || ((tag + b) == tag));
tag += b;
l += MSB_MASK;
u += MSB_MASK;
tag += MSB_MASK;
}
}
ASSERT(l < u);
}
inline DWORD decode_target(DWORD total)
{
DWORD ret = (DWORD)((((QWORD)(tag - l) + 1) * total - 1) / ((QWORD)u - l + 1));
return (ret);
}
};
#endif //ARITHMETIC_DECODER_H
////////////////////////////////////////////////////////