| 1 |
/* |
| 2 |
* weights.c: Input of weights |
| 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 "pm_c_util.h" |
| 20 |
|
| 21 |
#include "types.h" |
| 22 |
#include "macros.h" |
| 23 |
#include "error.h" |
| 24 |
|
| 25 |
#include "bit-io.h" |
| 26 |
#include "arith.h" |
| 27 |
#include "rpf.h" |
| 28 |
#include "misc.h" |
| 29 |
|
| 30 |
#include "weights.h" |
| 31 |
|
| 32 |
/***************************************************************************** |
| 33 |
|
| 34 |
public code |
| 35 |
|
| 36 |
*****************************************************************************/ |
| 37 |
|
| 38 |
void |
| 39 |
read_weights (unsigned total, wfa_t *wfa, bitfile_t *input) |
| 40 |
/* |
| 41 |
* Read #'total' weights from input stream 'input' and |
| 42 |
* update transitions of the WFA states with corresponding weights. |
| 43 |
* |
| 44 |
* No return value. |
| 45 |
* |
| 46 |
* Side effects: |
| 47 |
* 'wfa->weights' are filled with the decoded values |
| 48 |
*/ |
| 49 |
{ |
| 50 |
unsigned state; |
| 51 |
unsigned label; |
| 52 |
unsigned edge; /* current edge */ |
| 53 |
unsigned *weights_array; /* array of weights to encode */ |
| 54 |
unsigned *level_array; /* array of corresponding levels */ |
| 55 |
unsigned offset1, offset2; /* prob. model offsets. */ |
| 56 |
unsigned offset3, offset4; /* prob. model offsets. */ |
| 57 |
bool_t delta_approx = NO; /* true if delta has been used */ |
| 58 |
|
| 59 |
/* |
| 60 |
* Check whether delta approximation has been used |
| 61 |
*/ |
| 62 |
for (state = wfa->basis_states; state < wfa->states; state++) |
| 63 |
if (wfa->delta_state [state]) |
| 64 |
{ |
| 65 |
delta_approx = YES; |
| 66 |
break; |
| 67 |
} |
| 68 |
|
| 69 |
/* |
| 70 |
* Generate array of corresponding levels (context of probability model) |
| 71 |
*/ |
| 72 |
{ |
| 73 |
int min_level, max_level; /* min and max range level */ |
| 74 |
int d_min_level, d_max_level; /* min and max range level (delta) */ |
| 75 |
unsigned *lptr; /* pointer to current corresp. level */ |
| 76 |
int domain; /* current domain */ |
| 77 |
bool_t dc, d_dc; /* indicates whether DC is used */ |
| 78 |
|
| 79 |
/* |
| 80 |
* Compute minimum and maximum level of delta and normal approximations |
| 81 |
*/ |
| 82 |
min_level = d_min_level = MAXLEVEL; |
| 83 |
max_level = d_max_level = 0; |
| 84 |
dc = d_dc = NO; |
| 85 |
|
| 86 |
for (state = wfa->basis_states; state < wfa->states; state++) |
| 87 |
for (label = 0; label < MAXLABELS; label++) |
| 88 |
if (isrange (wfa->tree [state][label])) |
| 89 |
{ |
| 90 |
if (delta_approx && wfa->delta_state [state]) |
| 91 |
{ |
| 92 |
d_min_level = |
| 93 |
MIN(d_min_level, wfa->level_of_state [state] - 1); |
| 94 |
d_max_level = |
| 95 |
MAX(d_max_level, wfa->level_of_state [state] - 1); |
| 96 |
if (wfa->into [state][label][0] == 0) |
| 97 |
d_dc = YES; |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
min_level = |
| 102 |
MIN(min_level, wfa->level_of_state [state] - 1); |
| 103 |
max_level = |
| 104 |
MAX(max_level, wfa->level_of_state [state] - 1); |
| 105 |
if (wfa->into [state][label][0] == 0) |
| 106 |
dc = YES; |
| 107 |
} |
| 108 |
} |
| 109 |
if (min_level > max_level) /* no lc found */ |
| 110 |
max_level = min_level - 1; |
| 111 |
if (d_min_level > d_max_level) |
| 112 |
d_max_level = d_min_level - 1; |
| 113 |
|
| 114 |
offset1 = dc ? 1 : 0; |
| 115 |
offset2 = offset1 + (d_dc ? 1 : 0); |
| 116 |
offset3 = offset2 + (max_level - min_level + 1); |
| 117 |
offset4 = offset3 + (d_max_level - d_min_level + 1); |
| 118 |
|
| 119 |
lptr = level_array = Calloc (total, sizeof (int)); |
| 120 |
for (state = wfa->basis_states; state < wfa->states; state++) |
| 121 |
for (label = 0; label < MAXLABELS; label++) |
| 122 |
if (isrange (wfa->tree[state][label])) |
| 123 |
for (edge = 0; isedge (domain = wfa->into[state][label][edge]); |
| 124 |
edge++) |
| 125 |
{ |
| 126 |
if ((unsigned) (lptr - level_array) >= total) |
| 127 |
error ("Can't read more than %d weights.", total); |
| 128 |
if (domain) |
| 129 |
{ |
| 130 |
if (delta_approx && wfa->delta_state [state]) |
| 131 |
*lptr++ = offset3 + wfa->level_of_state [state] |
| 132 |
- 1 - d_min_level; |
| 133 |
else |
| 134 |
*lptr++ = offset2 + wfa->level_of_state [state] |
| 135 |
- 1 - min_level; |
| 136 |
} |
| 137 |
else |
| 138 |
*lptr++ = delta_approx && wfa->delta_state [state] |
| 139 |
? offset1 : 0; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/* |
| 144 |
* Decode the list of weights with an arithmetic decoder |
| 145 |
*/ |
| 146 |
{ |
| 147 |
unsigned i; |
| 148 |
unsigned *c_symbols = Calloc (offset4, sizeof (unsigned)); |
| 149 |
const unsigned scale = 500; /* scaling of probability model */ |
| 150 |
|
| 151 |
c_symbols [0] = 1 << (wfa->wfainfo->dc_rpf->mantissa_bits + 1); |
| 152 |
if (offset1 != offset2) |
| 153 |
c_symbols [offset1] = 1 << (wfa->wfainfo->d_dc_rpf->mantissa_bits |
| 154 |
+ 1); |
| 155 |
for (i = offset2; i < offset3; i++) |
| 156 |
c_symbols [i] = 1 << (wfa->wfainfo->rpf->mantissa_bits + 1); |
| 157 |
for (; i < offset4; i++) |
| 158 |
c_symbols [i] = 1 << (wfa->wfainfo->d_rpf->mantissa_bits + 1); |
| 159 |
|
| 160 |
weights_array = decode_array (input, level_array, c_symbols, |
| 161 |
offset4, total, scale); |
| 162 |
Free (c_symbols); |
| 163 |
} |
| 164 |
Free (level_array); |
| 165 |
|
| 166 |
/* |
| 167 |
* Update transitions with decoded weights |
| 168 |
*/ |
| 169 |
{ |
| 170 |
unsigned *wptr = weights_array; /* pointer to current weight */ |
| 171 |
int domain; /* current domain */ |
| 172 |
|
| 173 |
for (state = wfa->basis_states; state < wfa->states; state++) |
| 174 |
for (label = 0; label < MAXLABELS; label++) |
| 175 |
if (isrange (wfa->tree[state][label])) |
| 176 |
for (edge = 0; isedge (domain = wfa->into[state][label][edge]); |
| 177 |
edge++) |
| 178 |
{ |
| 179 |
if (domain) /* not DC component */ |
| 180 |
{ |
| 181 |
if (delta_approx && wfa->delta_state [state]) |
| 182 |
wfa->weight [state][label][edge] |
| 183 |
= btor (*wptr++, wfa->wfainfo->d_rpf); |
| 184 |
else |
| 185 |
wfa->weight [state][label][edge] |
| 186 |
= btor (*wptr++, wfa->wfainfo->rpf); |
| 187 |
} |
| 188 |
else |
| 189 |
{ |
| 190 |
if (delta_approx && wfa->delta_state [state]) |
| 191 |
wfa->weight [state][label][edge] |
| 192 |
= btor (*wptr++, wfa->wfainfo->d_dc_rpf); |
| 193 |
else |
| 194 |
wfa->weight [state][label][edge] |
| 195 |
= btor (*wptr++, wfa->wfainfo->dc_rpf); |
| 196 |
} |
| 197 |
wfa->int_weight [state][label][edge] |
| 198 |
= wfa->weight [state][label][edge] * 512 + 0.5; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
Free (weights_array); |
| 203 |
} |
| 204 |
|