1/*
2Copyright (C) 2002 Andrea Mazzoleni ( http://advancemame.sf.net )
3Copyright (C) 2001-4 Igor Pavlov ( http://www.7-zip.org )
4
5This library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Lesser General Public
7License version 2.1 as published by the Free Software Foundation.
8
9This library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Lesser General Public License for more details.
13
14You should have received a copy of the GNU Lesser General Public
15License along with this library; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19#ifndef __BITTREECODER_H
20#define __BITTREECODER_H
21
22#include "aribitcd.h"
23#include "rcdefs.h"
24
25
26//////////////////////////
27// CBitTreeDecoder
28
29template <int aNumMoveBits, UINT32 m_NumBitLevels>
30class CBitTreeDecoder
31{
32 CMyBitDecoder<aNumMoveBits> m_Models[1 << m_NumBitLevels];
33public:
34 void Init()
35 {
36 for(UINT32 i = 1; i < (1 << m_NumBitLevels); i++)
37 m_Models[i].Init();
38 }
39 UINT32 Decode(CMyRangeDecoder *aRangeDecoder)
40 {
41 UINT32 aModelIndex = 1;
42 RC_INIT_VAR
43 for(UINT32 aBitIndex = m_NumBitLevels; aBitIndex > 0; aBitIndex--)
44 {
45 // aModelIndex = (aModelIndex << 1) + m_Models[aModelIndex].Decode(aRangeDecoder);
46 RC_GETBIT(aNumMoveBits, m_Models[aModelIndex].m_Probability, aModelIndex)
47 }
48 RC_FLUSH_VAR
49 return aModelIndex - (1 << m_NumBitLevels);
50 };
51};
52
53////////////////////////////////
54// CReverseBitTreeDecoder
55
56template <int aNumMoveBits>
57class CReverseBitTreeDecoder2
58{
59 CMyBitDecoder<aNumMoveBits> *m_Models;
60 UINT32 m_NumBitLevels;
61public:
62 CReverseBitTreeDecoder2(): m_Models(0) { }
63 ~CReverseBitTreeDecoder2() { delete []m_Models; }
64 bool Create(UINT32 aNumBitLevels)
65 {
66 m_NumBitLevels = aNumBitLevels;
67 m_Models = new CMyBitDecoder<aNumMoveBits>[1 << aNumBitLevels];
68 return (m_Models != 0);
69 }
70 void Init()
71 {
72 UINT32 aNumModels = 1 << m_NumBitLevels;
73 for(UINT32 i = 1; i < aNumModels; i++)
74 m_Models[i].Init();
75 }
76 UINT32 Decode(CMyRangeDecoder *aRangeDecoder)
77 {
78 UINT32 aModelIndex = 1;
79 UINT32 aSymbol = 0;
80 RC_INIT_VAR
81 for(UINT32 aBitIndex = 0; aBitIndex < m_NumBitLevels; aBitIndex++)
82 {
83 // UINT32 aBit = m_Models[aModelIndex].Decode(aRangeDecoder);
84 // aModelIndex <<= 1;
85 // aModelIndex += aBit;
86 // aSymbol |= (aBit << aBitIndex);
87 RC_GETBIT2(aNumMoveBits, m_Models[aModelIndex].m_Probability, aModelIndex, ; , aSymbol |= (1 << aBitIndex))
88 }
89 RC_FLUSH_VAR
90 return aSymbol;
91 };
92};
93////////////////////////////
94// CReverseBitTreeDecoder2
95
96template <int aNumMoveBits, UINT32 m_NumBitLevels>
97class CReverseBitTreeDecoder
98{
99 CMyBitDecoder<aNumMoveBits> m_Models[1 << m_NumBitLevels];
100public:
101 void Init()
102 {
103 for(UINT32 i = 1; i < (1 << m_NumBitLevels); i++)
104 m_Models[i].Init();
105 }
106 UINT32 Decode(CMyRangeDecoder *aRangeDecoder)
107 {
108 UINT32 aModelIndex = 1;
109 UINT32 aSymbol = 0;
110 RC_INIT_VAR
111 for(UINT32 aBitIndex = 0; aBitIndex < m_NumBitLevels; aBitIndex++)
112 {
113 // UINT32 aBit = m_Models[aModelIndex].Decode(aRangeDecoder);
114 // aModelIndex <<= 1;
115 // aModelIndex += aBit;
116 // aSymbol |= (aBit << aBitIndex);
117 RC_GETBIT2(aNumMoveBits, m_Models[aModelIndex].m_Probability, aModelIndex, ; , aSymbol |= (1 << aBitIndex))
118 }
119 RC_FLUSH_VAR
120 return aSymbol;
121 }
122};
123
124
125
126#endif
127