Debian Codegraph @

terps/alan3/decode.c

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