1#include <stdlib.h> 2#include "bitstream.h" 3 4/* C O N S T A N T S */ 5const unsigned int mask [33] = { 6 0x00000000, 0x00000001, 0x00000003, 0x00000007, 7 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F, 8 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF, 9 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF, 10 0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 11 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 12 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 13 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 14 0xFFFFFFFF 15}; 16 17/* F U N C T I O N S */ 18 19// resets bitstream decoding 20void 21MPC_decoder::Reset_BitstreamDecode ( void ) 22{ 23 dword = 0; 24 pos = 0; 25 Zaehler = 0; 26 WordsRead = 0; 27} 28 29// reports the number of read bits 30unsigned int 31MPC_decoder::BitsRead ( void ) 32{ 33 return 32*WordsRead + pos; 34} 35 36// read desired number of bits out of the bitstream 37unsigned int 38MPC_decoder::Bitstream_read ( const unsigned int bits ) 39{ 40 unsigned int out = dword; 41 42 pos += bits; 43 44 if (pos<32) 45 { 46 out >>= (32-pos); 47 } 48 else 49 { 50 dword = Speicher[Zaehler=(Zaehler+1)&MEMMASK]; 51 pos -= 32; 52 if (pos) 53 { 54 out <<= pos; 55 out |= dword >> (32-pos); 56 } 57 ++WordsRead; 58 } 59 60 return out & mask[bits]; 61} 62 63// decode huffman 64int 65MPC_decoder::Huffman_Decode ( const HuffmanTyp* Table ) 66{ 67 // load preview and decode 68 unsigned int code = dword << pos; 69 if (pos>18) code |= Speicher[(Zaehler+1)&MEMMASK] >> (32-pos); 70 while (code < Table->Code) Table++; 71 72 // set the new position within bitstream without performing a dummy-read 73 if ((pos += Table->Length)>=32) 74 { 75 pos -= 32; 76 dword = Speicher[Zaehler=(Zaehler+1)&MEMMASK]; 77 ++WordsRead; 78 } 79 80 return Table->Value; 81} 82 83// faster huffman through previewing less bits 84int 85MPC_decoder::Huffman_Decode_fast ( const HuffmanTyp* Table ) 86{ 87 // load preview and decode 88 unsigned int code = dword << pos; 89 if (pos>22) code |= Speicher[(Zaehler+1)&MEMMASK] >> (32-pos); 90 while (code < Table->Code) Table++; 91 92 // set the new position within bitstream without performing a dummy-read 93 if ((pos += Table->Length)>=32) 94 { 95 pos -= 32; 96 dword = Speicher[Zaehler=(Zaehler+1)&MEMMASK]; 97 ++WordsRead; 98 } 99 100 return Table->Value; 101} 102 103// even faster huffman through previewing even less bits 104int 105MPC_decoder::Huffman_Decode_faster ( const HuffmanTyp* Table ) 106{ 107 // load preview and decode 108 unsigned int code = dword << pos; 109 if (pos>27) code |= Speicher[(Zaehler+1)&MEMMASK] >> (32-pos); 110 while (code < Table->Code) Table++; 111 112 // set the new position within bitstream without performing a dummy-read 113 if ((pos += Table->Length)>=32) 114 { 115 pos -= 32; 116 dword = Speicher[Zaehler=(Zaehler+1)&MEMMASK]; 117 ++WordsRead; 118 } 119 120 return Table->Value; 121} 122 123// decode SCFI-bundle (sv4,5,6) 124void 125MPC_decoder::SCFI_Bundle_read ( const HuffmanTyp* Table, int* SCFI, int* DSCF ) 126{ 127 // load preview and decode 128 unsigned int code = dword << pos; 129 if (pos>26) code |= Speicher[(Zaehler+1)&MEMMASK] >> (32-pos); 130 while (code < Table->Code) Table++; 131 132 // set the new position within bitstream without performing a dummy-read 133 if ((pos += Table->Length)>=32) 134 { 135 pos -= 32; 136 dword = Speicher[Zaehler=(Zaehler+1)&MEMMASK]; 137 ++WordsRead; 138 } 139 140 *SCFI = Table->Value >> 1; 141 *DSCF = Table->Value & 1; 142} 143 144static int 145cmpfn ( const void* p1, const void* p2 ) 146{ 147 if ( ((const MPC_decoder::HuffmanTyp*) p1)->Code < ((const MPC_decoder::HuffmanTyp*) p2)->Code ) return +1; 148 if ( ((const MPC_decoder::HuffmanTyp*) p1)->Code > ((const MPC_decoder::HuffmanTyp*) p2)->Code ) return -1; 149 return 0; 150} 151 152// sort huffman-tables by codeword 153// offset resulting value 154void 155MPC_decoder::Resort_HuffTables ( const unsigned int elements, HuffmanTyp* Table, const int offset ) 156{ 157 unsigned int i; 158 159 for ( i = 0; i < elements; i++ ) { 160 Table[i].Code <<= 32 - Table[i].Length; 161 Table[i].Value = i - offset; 162 } 163 qsort ( Table, elements, sizeof(*Table), cmpfn ); 164} 165 166/* end of bitstream.c */ 167