Debian Codegraph @

terps/alan2/decode.c

raw symbols SCIP
1 /*----------------------------------------------------------------------*\
2
3 decode.c
4
5 Arithmetic decoding module in Arun
6
7 \*----------------------------------------------------------------------*/
8
9 #include <stdio.h>
10
11 #include "main.h"
12 #include "decode.h"
13
14
15 /* Bit output */
16 static int decodeBuffer; /* Bits to be input */
17 static int bitsToGo; /* Bits still in buffer */
18 static int garbageBits; /* Bits past EOF */
19
20
21 #ifdef _PROTOTYPES_
22 static int inputBit(void)
23 #else
24 static int inputBit()
25 #endif
26 {
27 int bit;
28
29 if (!bitsToGo) { /* More bits available ? */
30 decodeBuffer = getc(txtfil); /* No, so get more */
31 if (decodeBuffer == EOF) {
32 garbageBits++;
33 if (garbageBits > VALUEBITS-2)
34 syserr("Error in encoded data file.");
35 } else
36 bitsToGo = 8; /* Another Char, 8 new bits */
37 }
38 bit = decodeBuffer&1; /* Get next bit */
39 decodeBuffer = decodeBuffer>>1; /* and remove it */
40 bitsToGo--;
41 return bit;
42 }
43
44
45 /* Current state of decoding */
46
47 static CodeValue value; /* Currently seen code value */
48 static CodeValue low, high; /* Current code region */
49
50
51 #ifdef _PROTOTYPES_
52 void startDecoding(void)
53 #else
54 void startDecoding()
55 #endif
56 {
57 int i;
58
59 bitsToGo = 0;
60 garbageBits = 0;
61
62 value = 0;
63 for (i = 0; i < VALUEBITS; i++)
64 value = 2*value + inputBit();
65 low = 0;
66 high = TOPVALUE;
67 }
68
69
70 #ifdef _PROTOTYPES_
71 int decodeChar(void)
72 #else
73 int decodeChar()
74 #endif
75 {
76 long range;
77 int f;
78 int symbol;
79
80 range = (long)(high-low) + 1;
81 f = (((long)(value-low)+1)*freq[0]-1)/range;
82
83 /* Find the symbol */
84 for (symbol = 1; freq[symbol] > f; symbol++);
85
86 high = low + range*freq[symbol-1]/freq[0]-1;
87 low = low + range*freq[symbol]/freq[0];
88
89 for (;;) {
90 if (high < HALF)
91 ;
92 else if (low >= HALF) {
93 value = value - HALF;
94 low = low - HALF;
95 high = high - HALF;
96 } else if (low >= ONEQUARTER && high < THREEQUARTER) {
97 value = value - ONEQUARTER;
98 low = low - ONEQUARTER;
99 high = high - ONEQUARTER;
100 } else
101 break;
102
103 /* Scale up the range */
104 low = 2*low;
105 high = 2*high+1;
106 value = 2*value + inputBit();
107 }
108 return symbol-1;
109 }
110
111
112
113 /* Structure for saved decode info */
114 typedef struct DecodeInfo {
115 long fpos;
116 int buffer;
117 int bits;
118 CodeValue value;
119 CodeValue high;
120 CodeValue low;
121 } DecodeInfo;
122
123
124 /*======================================================================
125
126 pushDecode()
127
128 Save so much info about the decoding process so it is possible to
129 restore and continue later.
130
131 */
132 #ifdef _PROTOTYPES_
133 void *pushDecode(void)
134 #else
135 void *pushDecode()
136 #endif
137 {
138 DecodeInfo *info;
139
140 info = (DecodeInfo *) allocate(sizeof(DecodeInfo));
141 info->fpos = ftell(txtfil);
142 info->buffer = decodeBuffer;
143 info->bits = bitsToGo;
144 info->value = value;
145 info->high = high;
146 info->low = low;
147 return(info);
148 }
149
150
151 /*======================================================================
152
153 popDecode()
154
155 Restore enough info about the decoding process so it is possible to
156 continue after having decoded something else.
157
158 */
159 #ifdef _PROTOTYPES_
160 void popDecode(void *i)
161 #else
162 void popDecode(i)
163 void *i;
164 #endif
165 {
166 DecodeInfo *info = (DecodeInfo *) i;
167
168 fseek(txtfil, info->fpos, 0);
169 decodeBuffer = info->buffer;
170 bitsToGo = info->bits;
171 value = info->value;
172 high = info->high;
173 low = info->low;
174
175 free(info);
176 }