summaryrefslogtreecommitdiffstats
path: root/hostTools/lzma/decompress/BitTreeCoder.h
blob: 8c663e6d0460612a26ea59fc9b68e3d46393eba9 (plain)
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
#ifndef __BITTREECODER_H
#define __BITTREECODER_H

#include "AriBitCoder.h"
#include "RCDefs.h"

//BRCM modification start
#ifdef _HOST_TOOL
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h" 
#endif

#ifdef _CFE_
#include "lib_malloc.h"
#include "lib_printf.h"
#define malloc(x) KMALLOC(x, 0)
#endif

#ifdef __KERNEL__
#include <linux/kernel.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
#define printf printk
//#define malloc(x) kmalloc(x,GFP_KERNEL)
#define malloc(x) vmalloc(x)
#define free(x) vfree(x)
#endif
//BRCM modification end

//////////////////////////
// CBitTreeDecoder

typedef struct CBitTreeDecoder
{
  UINT32       m_NumBitLevels;
  CBitDecoder *m_Models;
} CBitTreeDecoder;

//  ~CBitTreeDecoder() { free(m_Models); }
INLINE void BitTreeDecoderInit(CBitTreeDecoder *bitTreeDecoder, UINT32 aNumBitLevels)
  {
    int i;
    bitTreeDecoder->m_NumBitLevels = aNumBitLevels;
    bitTreeDecoder->m_Models       = (CBitDecoder *)malloc( sizeof(CBitDecoder) * (1 << bitTreeDecoder->m_NumBitLevels));
    //BRCM modification
    //printf("malloc in BitTreeDecoderInit=%d\n",sizeof(CBitDecoder) * (1 << bitTreeDecoder->m_NumBitLevels));
    if (!bitTreeDecoder->m_Models) {
	    printf("Error in allocating memory for bitTreeDecoder!\n");
	    return;
    }	    
    for(i = 1; i < (1 << aNumBitLevels); i++)
      BitDecoderInit(&bitTreeDecoder->m_Models[i]);
  }
INLINE UINT32 BitTreeDecode(ISequentialInStream *in_stream, CBitTreeDecoder *bitTreeDecoder, CRangeDecoder *aRangeDecoder)
  {
    UINT32 aModelIndex = 1;
    UINT32 aRange = aRangeDecoder->m_Range;
    UINT32 aCode = aRangeDecoder->m_Code;
    UINT32 aBitIndex;
    for(aBitIndex = bitTreeDecoder->m_NumBitLevels; aBitIndex > 0; aBitIndex--)
    {
      RC_GETBIT(kNumMoveBits, bitTreeDecoder->m_Models[aModelIndex], aModelIndex)
    }
    aRangeDecoder->m_Range = aRange;
    aRangeDecoder->m_Code = aCode;
    return aModelIndex - (1 << bitTreeDecoder->m_NumBitLevels);
  }


////////////////////////////////
// CReverseBitTreeDecoder2

typedef struct CReverseBitTreeDecoder2
{
  UINT32       m_NumBitLevels;
  CBitDecoder *m_Models;
} CReverseBitTreeDecoder2;

//  CReverseBitTreeDecoder2(): m_Models(0) { }
//  ~CReverseBitTreeDecoder2() { free(m_Models); }
INLINE BOOL ReverseBitTreeDecoder2Create(CReverseBitTreeDecoder2 *reverseBitTreeDecoder2, UINT32 aNumBitLevels)
  {
    reverseBitTreeDecoder2->m_NumBitLevels = aNumBitLevels;
    reverseBitTreeDecoder2->m_Models       = (CBitDecoder *)malloc( sizeof(CBitDecoder) * (1 << reverseBitTreeDecoder2->m_NumBitLevels));
    //printf("malloc in ReverseBitTreeDecoder2Create=%d\n",sizeof(CBitDecoder) * (1 << reverseBitTreeDecoder2->m_NumBitLevels));
    if (!reverseBitTreeDecoder2->m_Models) {
	    printf("Error in allocating memory for reverseBitTreeDecoder2!\n");
	    return 0;
    }	    
    return (reverseBitTreeDecoder2->m_Models != 0);
  }
INLINE void ReverseBitTreeDecoder2Init(CReverseBitTreeDecoder2 *reverseBitTreeDecoder2)
  {
    UINT32 aNumModels = 1 << reverseBitTreeDecoder2->m_NumBitLevels;
    UINT32 i;
    for(i = 1; i < aNumModels; i++)
      BitDecoderInit(&reverseBitTreeDecoder2->m_Models[i]);
  }
INLINE UINT32 ReverseBitTreeDecoder2Decode(ISequentialInStream *in_stream, CReverseBitTreeDecoder2 *reverseBitTreeDecoder2, CRangeDecoder *aRangeDecoder)
  {
    UINT32 aModelIndex = 1;
    UINT32 aSymbol = 0;
    UINT32 aRange = aRangeDecoder->m_Range;
    UINT32 aCode = aRangeDecoder->m_Code;
    UINT32 aBitIndex;
    for(aBitIndex = 0; aBitIndex < reverseBitTreeDecoder2->m_NumBitLevels; aBitIndex++)
    {
      RC_GETBIT2(kNumMoveBits, reverseBitTreeDecoder2->m_Models[aModelIndex], aModelIndex, ; , aSymbol |= (1 << aBitIndex))
    }
    aRangeDecoder->m_Range = aRange;
    aRangeDecoder->m_Code = aCode;
    return aSymbol;
  }


////////////////////////////
// CReverseBitTreeDecoder

typedef struct CReverseBitTreeDecoder
{
  UINT32        m_NumBitLevels;
  CBitDecoder  *m_Models;
} CReverseBitTreeDecoder;

//    CReverseBitTreeDecoder(): m_Models(0) { }
//    ~CReverseBitTreeDecoder() { free(m_Models); }
INLINE void ReverseBitTreeDecoderInit(CReverseBitTreeDecoder *reverseBitTreeDecoder, UINT32 aNumBitLevels)
  {
    int i;
    reverseBitTreeDecoder->m_NumBitLevels = aNumBitLevels;
    reverseBitTreeDecoder->m_Models       = (CBitDecoder *)malloc( sizeof(CBitDecoder) * (1 << reverseBitTreeDecoder->m_NumBitLevels));
    //printf("malloc in ReverseBitTreeDecoderInit=%d\n",sizeof(CBitDecoder) * (1 << reverseBitTreeDecoder->m_NumBitLevels));
    if (!reverseBitTreeDecoder->m_Models) {
	    printf("Error in allocating memory for reverseBitTreeDecoder!\n");
	    return;
    }	    
    for(i = 1; i < (1 << reverseBitTreeDecoder->m_NumBitLevels); i++)
      BitDecoderInit(&reverseBitTreeDecoder->m_Models[i]);
  }

INLINE UINT32 ReverseBitTreeDecoderDecode(ISequentialInStream *in_stream, CReverseBitTreeDecoder *reverseBitTreeDecoder, CRangeDecoder *aRangeDecoder)
  {
    UINT32 aModelIndex = 1;
    UINT32 aSymbol = 0;
    UINT32 aRange = aRangeDecoder->m_Range;
    UINT32 aCode = aRangeDecoder->m_Code;
    UINT32 aBitIndex;
    for(aBitIndex = 0; aBitIndex < reverseBitTreeDecoder->m_NumBitLevels; aBitIndex++)
    {
      RC_GETBIT2(kNumMoveBits, reverseBitTreeDecoder->m_Models[aModelIndex], aModelIndex, ; , aSymbol |= (1 << aBitIndex))
    }
    aRangeDecoder->m_Range = aRange;
    aRangeDecoder->m_Code = aCode;
    return aSymbol;
  }



#endif