| 1 |
/* |
| 2 |
* weights.c: Output 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:31 $ |
| 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 "wfa.h" |
| 26 |
#include "misc.h" |
| 27 |
#include "bit-io.h" |
| 28 |
#include "arith.h" |
| 29 |
#include "wfalib.h" |
| 30 |
|
| 31 |
#include "weights.h" |
| 32 |
|
| 33 |
/***************************************************************************** |
| 34 |
|
| 35 |
public code |
| 36 |
|
| 37 |
*****************************************************************************/ |
| 38 |
|
| 39 |
void |
| 40 |
write_weights (unsigned total, const wfa_t *wfa, bitfile_t *output) |
| 41 |
/* |
| 42 |
* Traverse the transition matrices of the 'wfa' and write #'total' |
| 43 |
* weights != 0 to stream 'output'. |
| 44 |
* |
| 45 |
* No return value. |
| 46 |
*/ |
| 47 |
{ |
| 48 |
unsigned state, label; /* current label */ |
| 49 |
unsigned offset1, offset2; /* model offsets. */ |
| 50 |
unsigned offset3, offset4; /* model offsets. */ |
| 51 |
unsigned *weights_array; /* array of weights to encode */ |
| 52 |
unsigned *wptr; /* pointer to current weight */ |
| 53 |
unsigned *level_array; /* array of corresponding levels */ |
| 54 |
unsigned *lptr; /* pointer to current corr. level */ |
| 55 |
int min_level, max_level; /* min and max range level */ |
| 56 |
int d_min_level, d_max_level; /* min and max delta range level */ |
| 57 |
bool_t dc, d_dc; /* true if dc or delta dc are used */ |
| 58 |
bool_t delta_approx = NO; /* true if delta has been used */ |
| 59 |
unsigned delta_count = 0; /* number of delta ranges */ |
| 60 |
unsigned bits = bits_processed (output); |
| 61 |
|
| 62 |
/* |
| 63 |
* Check whether delta approximation has been used |
| 64 |
*/ |
| 65 |
for (state = wfa->basis_states; state < wfa->states; state++) |
| 66 |
if (wfa->delta_state [state]) |
| 67 |
{ |
| 68 |
delta_approx = YES; |
| 69 |
break; |
| 70 |
} |
| 71 |
|
| 72 |
/* |
| 73 |
* Generate array of corresponding levels (context of probability model) |
| 74 |
*/ |
| 75 |
min_level = d_min_level = MAXLEVEL; |
| 76 |
max_level = d_max_level = 0; |
| 77 |
dc = d_dc = NO; |
| 78 |
|
| 79 |
for (state = wfa->basis_states; state < wfa->states; state++) |
| 80 |
for (label = 0; label < MAXLABELS; label++) |
| 81 |
if (isrange (wfa->tree [state][label])) |
| 82 |
{ |
| 83 |
if (delta_approx && wfa->delta_state [state]) /* delta approx. */ |
| 84 |
{ |
| 85 |
d_min_level = MIN(d_min_level, wfa->level_of_state [state] - 1); |
| 86 |
d_max_level = MAX(d_max_level, wfa->level_of_state [state] - 1); |
| 87 |
if (wfa->into [state][label][0] == 0) |
| 88 |
d_dc = YES; |
| 89 |
} |
| 90 |
else |
| 91 |
{ |
| 92 |
min_level = MIN(min_level, wfa->level_of_state [state] - 1); |
| 93 |
max_level = MAX(max_level, wfa->level_of_state [state] - 1); |
| 94 |
if (wfa->into [state][label][0] == 0) |
| 95 |
dc = YES; |
| 96 |
} |
| 97 |
} |
| 98 |
if (min_level > max_level) /* no lc found */ |
| 99 |
max_level = min_level - 1; |
| 100 |
if (d_min_level > d_max_level) |
| 101 |
d_max_level = d_min_level - 1; |
| 102 |
|
| 103 |
/* |
| 104 |
* Context model: |
| 105 |
* 0 DC weight |
| 106 |
* 1 Delta DC weight |
| 107 |
* 2-k normal weights per level |
| 108 |
* k+1 - m Delta weights per level |
| 109 |
*/ |
| 110 |
|
| 111 |
offset1 = dc ? 1 : 0; |
| 112 |
offset2 = offset1 + (d_dc ? 1 : 0); |
| 113 |
offset3 = offset2 + (max_level - min_level + 1); |
| 114 |
offset4 = offset3 + (d_max_level - d_min_level + 1); |
| 115 |
|
| 116 |
/* |
| 117 |
* Weights are encoded as follows: |
| 118 |
* all weights of state n |
| 119 |
* sorted by label |
| 120 |
* sorted by domain number |
| 121 |
*/ |
| 122 |
|
| 123 |
wptr = weights_array = Calloc (total, sizeof (unsigned)); |
| 124 |
lptr = level_array = Calloc (total, sizeof (unsigned)); |
| 125 |
|
| 126 |
for (state = wfa->basis_states; state < wfa->states; state++) |
| 127 |
for (label = 0; label < MAXLABELS; label++) |
| 128 |
if (isrange (wfa->tree [state][label])) |
| 129 |
{ |
| 130 |
int edge; /* current edge */ |
| 131 |
int domain; /* current domain (context of model) */ |
| 132 |
|
| 133 |
for (edge = 0; isedge (domain = wfa->into [state][label][edge]); |
| 134 |
edge++) |
| 135 |
{ |
| 136 |
if (wptr - weights_array >= (int) total) |
| 137 |
error ("Can't write more than %d weights.", total); |
| 138 |
if (domain) /* not DC component */ |
| 139 |
{ |
| 140 |
if (delta_approx && wfa->delta_state [state]) /* delta */ |
| 141 |
{ |
| 142 |
*wptr++ = rtob (wfa->weight [state][label][edge], |
| 143 |
wfa->wfainfo->d_rpf); |
| 144 |
*lptr++ = offset3 |
| 145 |
+ wfa->level_of_state [state] - 1 - d_min_level; |
| 146 |
delta_count++; |
| 147 |
} |
| 148 |
else |
| 149 |
{ |
| 150 |
*wptr++ = rtob (wfa->weight [state][label][edge], |
| 151 |
wfa->wfainfo->rpf); |
| 152 |
*lptr++ = offset2 |
| 153 |
+ wfa->level_of_state [state] - 1 - min_level; |
| 154 |
} |
| 155 |
} |
| 156 |
else /* DC component */ |
| 157 |
{ |
| 158 |
if (delta_approx && wfa->delta_state [state]) /* delta */ |
| 159 |
{ |
| 160 |
*wptr++ = rtob (wfa->weight [state][label][edge], |
| 161 |
wfa->wfainfo->d_dc_rpf); |
| 162 |
*lptr++ = offset1; |
| 163 |
} |
| 164 |
else |
| 165 |
{ |
| 166 |
*wptr++ = rtob (wfa->weight [state][label][edge], |
| 167 |
wfa->wfainfo->dc_rpf); |
| 168 |
*lptr++ = 0; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
{ |
| 175 |
unsigned i; |
| 176 |
unsigned *c_symbols = Calloc (offset4, sizeof (int)); |
| 177 |
const int scale = 500; /* scaling of probability model */ |
| 178 |
|
| 179 |
c_symbols [0] = 1 << (wfa->wfainfo->dc_rpf->mantissa_bits + 1); |
| 180 |
if (offset1 != offset2) |
| 181 |
c_symbols [offset1] = 1 << (wfa->wfainfo->d_dc_rpf->mantissa_bits |
| 182 |
+ 1); |
| 183 |
for (i = offset2; i < offset3; i++) |
| 184 |
c_symbols [i] = 1 << (wfa->wfainfo->rpf->mantissa_bits + 1); |
| 185 |
for (; i < offset4; i++) |
| 186 |
c_symbols [i] = 1 << (wfa->wfainfo->d_rpf->mantissa_bits + 1); |
| 187 |
|
| 188 |
encode_array (output, weights_array, level_array, c_symbols, offset4, |
| 189 |
total, scale); |
| 190 |
Free (c_symbols); |
| 191 |
} |
| 192 |
|
| 193 |
debug_message ("%d delta weights out of %d.", delta_count, total); |
| 194 |
debug_message ("weights: %5d bits. (%5d symbols => %5.2f bps)", |
| 195 |
bits_processed (output) - bits, total, |
| 196 |
(bits_processed (output) - bits) / (double) total); |
| 197 |
|
| 198 |
Free (weights_array); |
| 199 |
Free (level_array); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|