summaryrefslogtreecommitdiffstats
path: root/hostTools/lzma/decompress/LiteralCoder.h
blob: 2b1670d9925b5292a29f7a7bd29542faa2967d7e (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
#ifndef __LITERALCODER_H
#define __LITERALCODER_H

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

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

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

#ifdef __KERNEL__
#include <linux/kernel.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

//#define kNumMoveBits 5

typedef struct LitDecoder2
{
  CBitDecoder m_Decoders[3][1 << 8];
} LitDecoder2;


INLINE void LitDecoder2Init(LitDecoder2 *litDecoder2)
  {
    int i, j;
    for (i = 0; i < 3; i++)
      for (j = 1; j < (1 << 8); j++)
        BitDecoderInit(&litDecoder2->m_Decoders[i][j]);
  }

INLINE BYTE LitDecoder2DecodeNormal(ISequentialInStream *in_stream, LitDecoder2 *litDecoder2, CRangeDecoder *aRangeDecoder)
  {
    UINT32 aSymbol = 1;
    UINT32 aRange = aRangeDecoder->m_Range;
    UINT32 aCode = aRangeDecoder->m_Code;        
    do
    {
      RC_GETBIT(kNumMoveBits, litDecoder2->m_Decoders[0][aSymbol], aSymbol)
    }
    while (aSymbol < 0x100);
    aRangeDecoder->m_Range = aRange;
    aRangeDecoder->m_Code = aCode;
    return aSymbol;
  }

INLINE BYTE LitDecoder2DecodeWithMatchByte(ISequentialInStream *in_stream, LitDecoder2 *litDecoder2, CRangeDecoder *aRangeDecoder, BYTE aMatchByte)
  {
    UINT32 aSymbol = 1;
    UINT32 aRange = aRangeDecoder->m_Range;
    UINT32 aCode = aRangeDecoder->m_Code;        
    do
    {
      UINT32 aBit;
      UINT32 aMatchBit = (aMatchByte >> 7) & 1;
      aMatchByte <<= 1;
      RC_GETBIT2(kNumMoveBits, litDecoder2->m_Decoders[1 + aMatchBit][aSymbol], aSymbol, 
          aBit = 0, aBit = 1)
      if (aMatchBit != aBit)
      {
        while (aSymbol < 0x100)
        {
          RC_GETBIT(kNumMoveBits, litDecoder2->m_Decoders[0][aSymbol], aSymbol)
        }
        break;
      }
    }
    while (aSymbol < 0x100);
    aRangeDecoder->m_Range = aRange;
    aRangeDecoder->m_Code = aCode;
    return aSymbol;
  }


typedef struct LitDecoder
{
  LitDecoder2 *m_Coders;
  UINT32 m_NumPrevBits;
  UINT32 m_NumPosBits;
  UINT32 m_PosMask;
} LitDecoder;


//  LitDecoder(): m_Coders(0) {}
//  ~LitDecoder()  { Free(); }

/*
INLINE void LitDecoderFree(LitDecoder *litDecoder)
  { 
    free( (char *) litDecoder->m_Coders );
    litDecoder->m_Coders = 0;
  }
*/

INLINE void LitDecoderCreate(LitDecoder *litDecoder, UINT32 aNumPosBits, UINT32 aNumPrevBits)
  {
//    LitDecoderFree(litDecoder);
    UINT32 aNumStates;
    litDecoder->m_NumPosBits = aNumPosBits;
    litDecoder->m_PosMask = (1 << aNumPosBits) - 1;
    litDecoder->m_NumPrevBits = aNumPrevBits;
    aNumStates = 1 << (aNumPrevBits + aNumPosBits);
    litDecoder->m_Coders = (LitDecoder2*) malloc( sizeof( LitDecoder2 ) * aNumStates );
    //printf("malloc in LitDecoderCreate=%d\n",sizeof( LitDecoder2 ) * aNumStates);
    if (litDecoder->m_Coders == 0)
        printf( "Error allocating memory for LitDecoder m_Coders!\n" );
  }

INLINE void LitDecoderInit(LitDecoder *litDecoder)
  {
    UINT32 i;
    UINT32 aNumStates = 1 << (litDecoder->m_NumPrevBits + litDecoder->m_NumPosBits);
    for (i = 0; i < aNumStates; i++)
      LitDecoder2Init(&litDecoder->m_Coders[i]);
  }

INLINE UINT32 LitDecoderGetState(LitDecoder *litDecoder, UINT32 aPos, BYTE aPrevByte)
  { 
    return ((aPos & litDecoder->m_PosMask) << litDecoder->m_NumPrevBits) + (aPrevByte >> (8 - litDecoder->m_NumPrevBits)); 
  }

INLINE BYTE LitDecodeNormal(ISequentialInStream *in_stream, LitDecoder *litDecoder, CRangeDecoder *aRangeDecoder, UINT32 aPos, BYTE aPrevByte)
  { 
    return LitDecoder2DecodeNormal(in_stream, &litDecoder->m_Coders[LitDecoderGetState(litDecoder, aPos, aPrevByte)], aRangeDecoder); 
  }

INLINE BYTE LitDecodeWithMatchByte(ISequentialInStream *in_stream, LitDecoder *litDecoder, CRangeDecoder *aRangeDecoder, UINT32 aPos, BYTE aPrevByte, BYTE aMatchByte)
  { 
      return LitDecoder2DecodeWithMatchByte(in_stream, &litDecoder->m_Coders[LitDecoderGetState(litDecoder, aPos, aPrevByte)], aRangeDecoder, aMatchByte); 
  }

#endif