about summary refs log tree commit diff
path: root/converter/other/fiasco/codec/control.c
blob: 0588623f85b92249b551db752841d852c8fb4b3f (plain) (blame)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 *  control.c:          Control unit of WFA structure
 *
 *  Written by:         Ullrich Hafner
 *
 *  This file is part of FIASCO (Fractal Image And Sequence COdec)
 *  Copyright (C) 1994-2000 Ullrich Hafner
 */

/*
 *  $Date: 2000/06/14 20:50:51 $
 *  $Author: hafner $
 *  $Revision: 5.1 $
 *  $State: Exp $
 */

#include "config.h"

#include <string.h>

#include "types.h"
#include "macros.h"
#include "error.h"

#include "cwfa.h"
#include "ip.h"
#include "misc.h"
#include "wfalib.h"
#include "control.h"

/*****************************************************************************

                                prototypes

*****************************************************************************/

static void
clear_or_alloc (real_t **ptr, size_t size);
static void
compute_images (unsigned from, unsigned to, const wfa_t *wfa, coding_t *c);

/*****************************************************************************

                                public code

*****************************************************************************/

void
append_state (bool_t auxiliary_state, real_t final, unsigned level_of_state,
              wfa_t *wfa, coding_t *c)
/*
 *  Append a 'wfa' state. If 'auxiliary_state' == YES then
 *  allocate memory for inner products and state images.  'final' is
 *  the final distribution of the new state. 'level_of_state' is the
 *  level of the subimage which is represented by this state
 *
 *  No return value.
 *
 *  Side effects:
 *      The WFA information are updated in structure 'wfa'
 *      State images are computed and inner products are cleared (in 'c')
 */
{
   wfa->final_distribution [wfa->states] = final;
   wfa->level_of_state [wfa->states]     = level_of_state;

   if (!auxiliary_state)
   {
      unsigned level;

      wfa->domain_type [wfa->states] = USE_DOMAIN_MASK;

      /*
       *  Allocate memory for inner products and for state images
       */
      clear_or_alloc (&c->images_of_state [wfa->states],
                      size_of_tree (c->options.images_level));

      for (level = c->options.images_level + 1;
           level <= c->options.lc_max_level; level++)
         clear_or_alloc (&c->ip_states_state [wfa->states][level],
                         wfa->states + 1);

      clear_or_alloc (&c->ip_images_state [wfa->states],
                      size_of_tree (c->products_level));

      /*
       *  Compute the images of the current state at level 0,..,'imageslevel'
       */

      c->images_of_state [wfa->states][0] = final;
      compute_images (wfa->states, wfa->states, wfa, c);

      /*
       *  Compute the inner products between the current state and the
       *  old states 0,...,'states'-1
       */

      compute_ip_states_state (wfa->states, wfa->states, wfa, c);
   }
   else
   {
      unsigned level;

      wfa->domain_type [wfa->states] = 0;

      /*
       *  Free the allocated memory
       */
      if (c->images_of_state [wfa->states] != NULL)
      {
         Free (c->images_of_state [wfa->states]);
         c->images_of_state [wfa->states] = NULL;
      }
      for (level = 0; level <= c->options.lc_max_level; level++)
         if (c->ip_states_state [wfa->states][level])
         {
            Free (c->ip_states_state [wfa->states][level]);
            c->ip_states_state [wfa->states][level] = NULL;
         }
      if (c->ip_images_state [wfa->states])
      {
         Free (c->ip_images_state [wfa->states]);
         c->ip_images_state [wfa->states] = NULL;
      }
   }

   wfa->states++;
   if (wfa->states >= MAXSTATES)
      error ("Maximum number of states reached!");
}

void
append_basis_states (unsigned basis_states, wfa_t *wfa, coding_t *c)
/*
 *  Append the WFA basis states 0, ... , ('basis_states' - 1).
 *
 *  No return value.
 *
 *  Side effects:
 *      The WFA information are updated in structure 'wfa'
 *      State images and inner products are computed (in 'c')
 */
{
   unsigned level, state;

   /*
    *  Allocate memory
    */

   for (state = 0; state < basis_states; state++)
   {
      clear_or_alloc (&c->images_of_state [state],
                      size_of_tree (c->options.images_level));

      for (level = c->options.images_level + 1;
           level <= c->options.lc_max_level; level++)
         clear_or_alloc (&c->ip_states_state [state][level], state + 1);

      clear_or_alloc (&c->ip_images_state [state],
                      size_of_tree (c->products_level));

      c->images_of_state [state][0] = wfa->final_distribution [state];
      wfa->level_of_state [state]   = -1;
   }

   compute_images (0, basis_states - 1, wfa, c);
   compute_ip_states_state (0, basis_states - 1, wfa, c);
   wfa->states = basis_states;

   if (wfa->states >= MAXSTATES)
      error ("Maximum number of states reached!");
}

void
append_transitions (unsigned state, unsigned label, const real_t *weight,
                    const word_t *into, wfa_t *wfa)
/*
 *  Append the 'wfa' transitions (given by the arrays 'weight' and 'into')
 *  of the range ('state','label').
 *
 *  No return value.
 *
 *  Side effects:
 *      new 'wfa' edges are appended
 */
{
   unsigned edge;

   wfa->y_column [state][label] = 0;
   for (edge = 0; isedge (into [edge]); edge++)
   {
      append_edge (state, into [edge], weight [edge], label, wfa);
      if (into [edge] == wfa->y_state [state][label])
         wfa->y_column [state][label] = 1;
   }
}

/*****************************************************************************

                                private code

*****************************************************************************/

static void
compute_images (unsigned from, unsigned to, const wfa_t *wfa, coding_t *c)
/*
 *  Computes the images of the given states 'from', ... , 'to'
 *  at level 0,...,'c->imagelevel'.
 *  Uses the fact that each state image is a linear combination of state
 *  images, i.e. s_i := c_0 s_0 + ... + c_i s_i.
 */
{
   unsigned label, level, state;

   /*
    *  Compute the images Phi(state)
    *  #                level = 0
    *  ##               level = 1
    *  ####             level = 2
    *  ########         level = 3
    *  ...
    *  ########...##    level = imageslevel
    */

   for (level = 1; level <= c->options.images_level; level++)
      for (state = from; state <= to; state++)
         for (label = 0; label < MAXLABELS; label++)
         {
            real_t   *dst, *src;
            unsigned  edge;
            int       domain;           /* current domain */

            if (ischild (domain = wfa->tree[state][label]))
            {
               dst = c->images_of_state [state] + address_of_level (level) +
                     label * size_of_level (level - 1);
               src = c->images_of_state [domain]
                     + address_of_level (level - 1);
               memcpy (dst, src, size_of_level (level - 1) * sizeof (real_t));
            }
            for (edge = 0; isedge (domain = wfa->into[state][label][edge]);
                 edge++)
            {
               unsigned n;
               real_t   weight = wfa->weight [state][label][edge];

               dst = c->images_of_state [state] + address_of_level (level) +
                     label * size_of_level (level - 1);
               src = c->images_of_state [domain]
                     + address_of_level (level - 1);

               for (n = size_of_level (level - 1); n; n--)
                  *dst++ += *src++ * weight;
            }
         }

}

static void
clear_or_alloc (real_t **ptr, size_t size)
/*
 *  if *ptr == NULL     allocate memory with Calloc
 *  otherwise           fill the real_t-array ptr[] with 0
 */
{
   if (*ptr == NULL)
      *ptr = Calloc (size, sizeof (real_t));
   else
      memset (*ptr, 0, size * sizeof (real_t));

}