Debian Codegraph @

converter/other/fiasco/input/nd.c

raw
1 /*
2 * nd.c: Input of prediction tree
3 *
4 * Written by: Ullrich Hafner
5 *
6 * This file is part of FIASCO (Fractal Image And Sequence COdec)
7 * Copyright (C) 1994-2000 Ullrich Hafner
8 */
9
10 /*
11 * $Date: 2000/06/14 20:50:13 $
12 * $Author: hafner $
13 * $Revision: 5.1 $
14 * $State: Exp $
15 */
16
17 #include "config.h"
18
19 #include "types.h"
20 #include "macros.h"
21 #include "error.h"
22
23 #include "bit-io.h"
24 #include "arith.h"
25 #include "misc.h"
26 #include "list.h"
27 #include "wfalib.h"
28
29 #include "nd.h"
30
31 /*****************************************************************************
32
33 prototypes
34
35 *****************************************************************************/
36
37 static void
38 decode_nd_coefficients (unsigned total, wfa_t *wfa, bitfile_t *input);
39 static unsigned
40 decode_nd_tree (wfa_t *wfa, bitfile_t *input);
41
42 /*****************************************************************************
43
44 public code
45
46 *****************************************************************************/
47
48 void
49 read_nd (wfa_t *wfa, bitfile_t *input)
50 /*
51 * Read transitions of the nondetermistic 'wfa' part from 'input' stream.
52 * ND is used only at levels {'wfa->p_min_level', ... , 'wfa->p_max_level'}.
53 *
54 * Side effects:
55 * 'wfa->into' and 'wfa->weights' are filled with the decoded values
56 */
57 {
58 unsigned total = decode_nd_tree (wfa, input);
59
60 if (total > 0)
61 decode_nd_coefficients (total, wfa, input);
62 }
63
64 /*****************************************************************************
65
66 private code
67
68 *****************************************************************************/
69
70 static unsigned
71 decode_nd_tree (wfa_t *wfa, bitfile_t *input)
72 /*
73 * Read 'wfa' prediction tree of given 'input' stream.
74 *
75 * No return value.
76 *
77 * Side effects:
78 * 'wfa->into' is filled with the decoded values
79 */
80 {
81 lqueue_t *queue; /* queue of states */
82 int next, state; /* state and its current child */
83 unsigned total = 0; /* total number of predicted states */
84 u_word_t sum0, sum1; /* Probability model */
85 u_word_t code; /* The present input code value */
86 u_word_t low; /* Start of the current code range */
87 u_word_t high; /* End of the current code range */
88
89 /*
90 * Initialize arithmetic decoder
91 */
92 code = get_bits (input, 16);
93 low = 0;
94 high = 0xffff;
95 sum0 = 1;
96 sum1 = 11;
97
98 queue = alloc_queue (sizeof (int));
99 state = wfa->root_state;
100 queue_append (queue, &state);
101
102 /*
103 * Traverse the WFA tree in breadth first order (using a queue).
104 */
105 while (queue_remove (queue, &next))
106 {
107 unsigned label;
108
109 if (wfa->level_of_state [next] > wfa->wfainfo->p_max_level + 1)
110 {
111 /*
112 * Nondetermismn is not allowed at levels larger than
113 * 'wfa->wfainfo->p_max_level'.
114 */
115 for (label = 0; label < MAXLABELS; label++)
116 if (ischild (state = wfa->tree [next][label]))
117 queue_append (queue, &state); /* continue with children */
118 }
119 else if (wfa->level_of_state [next] > wfa->wfainfo->p_min_level)
120 {
121 for (label = 0; label < MAXLABELS; label++)
122 if (ischild (state = wfa->tree [next][label]))
123 {
124 unsigned count; /* Current interval count */
125 unsigned range; /* Current interval range */
126
127 count = (((code - low) + 1) * sum1 - 1) / ((high - low) + 1);
128 if (count < sum0)
129 {
130 /*
131 * Decode a '0' symbol
132 * First, the range is expanded to account for the
133 * symbol removal.
134 */
135 range = (high - low) + 1;
136 high = low + (u_word_t) ((range * sum0) / sum1 - 1 );
137 RESCALE_INPUT_INTERVAL;
138 /*
139 * Update the frequency counts
140 */
141 sum0++;
142 sum1++;
143 if (sum1 > 50) /* scale the symbol frequencies */
144 {
145 sum0 >>= 1;
146 sum1 >>= 1;
147 if (!sum0)
148 sum0 = 1;
149 if (sum0 >= sum1)
150 sum1 = sum0 + 1;
151 }
152 if (wfa->level_of_state [state] > wfa->wfainfo->p_min_level)
153 queue_append (queue, &state);
154 }
155 else
156 {
157 /*
158 * Decode a '1' symbol
159 * First, the range is expanded to account for the
160 * symbol removal.
161 */
162 range = (high - low) + 1;
163 high = low + (u_word_t) ((range * sum1) / sum1 - 1);
164 low = low + (u_word_t) ((range * sum0) / sum1);
165 RESCALE_INPUT_INTERVAL;
166 /*
167 * Update the frequency counts
168 */
169 sum1++;
170 if (sum1 > 50) /* scale the symbol frequencies */
171 {
172 sum0 >>= 1;
173 sum1 >>= 1;
174 if (!sum0)
175 sum0 = 1;
176 if (sum0 >= sum1)
177 sum1 = sum0 + 1;
178 }
179 append_edge (next, 0, -1, label, wfa);
180 total++;
181 }
182 }
183 }
184 }
185 free_queue (queue);
186
187 INPUT_BYTE_ALIGN (input);
188
189 return total;
190 }
191
192 static void
193 decode_nd_coefficients (unsigned total, wfa_t *wfa, bitfile_t *input)
194 /*
195 * Read #'total' weights of nondeterministic part of 'wfa'
196 * of given 'input' stream.
197 * 'frame' gives the current frame number.
198 *
199 * No return value.
200 *
201 * Side effects:
202 * 'wfa->weights' is filled with the decoded values.
203 */
204 {
205 unsigned *coefficients; /* array of factors to encode */
206 unsigned *ptr; /* pointer to current factor */
207
208 /*
209 * Decode array of coefficients stored with arithmetic coding
210 */
211 {
212 const int scaling = 50; /* scaling factor of prob. model */
213 unsigned c_symbols = 1 << (wfa->wfainfo->dc_rpf->mantissa_bits + 1);
214
215 ptr = coefficients = decode_array (input, NULL, &c_symbols, 1,
216 total, scaling);
217 }
218
219 /*
220 * Fill 'wfa->weights' with decoded coefficients
221 */
222 {
223 unsigned state, label;
224
225 for (state = wfa->basis_states; state < wfa->states; state++)
226 for (label = 0; label < MAXLABELS; label++)
227 if (ischild (wfa->tree [state][label])
228 && isedge (wfa->into [state][label][0]))
229 {
230 wfa->weight [state][label][0] = btor (*ptr++,
231 wfa->wfainfo->dc_rpf);
232 wfa->int_weight [state][label][0]
233 = wfa->weight [state][label][0] * 512 + 0.5;
234 }
235 }
236 Free (coefficients);
237 }
238
239
240