Debian Codegraph @

converter/other/fiasco/codec/approx.c

raw
1 /*
2 * approx.c: Approximation of range images with matching pursuit
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:51 $
12 * $Author: hafner $
13 * $Revision: 5.1 $
14 * $State: Exp $
15 */
16
17 #include "config.h"
18
19 #include <math.h>
20
21 #include "types.h"
22 #include "macros.h"
23 #include "error.h"
24
25 #include "cwfa.h"
26 #include "ip.h"
27 #include "rpf.h"
28 #include "domain-pool.h"
29 #include "misc.h"
30 #include "list.h"
31 #include "approx.h"
32 #include "coeff.h"
33 #include "wfalib.h"
34
35 /*****************************************************************************
36
37 local variables
38
39 *****************************************************************************/
40
41 typedef struct mp
42 {
43 word_t exclude [MAXEDGES];
44 word_t indices [MAXEDGES + 1];
45 word_t into [MAXEDGES + 1];
46 real_t weight [MAXEDGES];
47 real_t matrix_bits;
48 real_t weights_bits;
49 real_t err;
50 real_t costs;
51 } mp_t;
52
53 /*****************************************************************************
54
55 prototypes
56
57 *****************************************************************************/
58
59 static void
60 orthogonalize (unsigned index, unsigned n, unsigned level, real_t min_norm,
61 const word_t *domain_blocks, const coding_t *c);
62 static void
63 matching_pursuit (mp_t *mp, bool_t full_search, real_t price,
64 unsigned max_edges, int y_state, const range_t *range,
65 const domain_pool_t *domain_pool, const coeff_t *coeff,
66 const wfa_t *wfa, const coding_t *c);
67
68 /*****************************************************************************
69
70 public code
71
72 *****************************************************************************/
73
74 real_t
75 approximate_range (real_t max_costs, real_t price, int max_edges,
76 int y_state, range_t *range, domain_pool_t *domain_pool,
77 coeff_t *coeff, const wfa_t *wfa, const coding_t *c) {
78 /*----------------------------------------------------------------------------
79 Approximate image block 'range' by matching pursuit. This functions
80 calls the matching pursuit algorithm several times (with different
81 parameters) in order to find the best approximation. Refer to function
82 'matching_pursuit()' for more details about parameters.
83
84 Return value: approximation costs
85 -----------------------------------------------------------------------------*/
86 mp_t mp;
87
88 /*
89 * First approximation attempt: default matching pursuit algorithm.
90 */
91 mp.exclude [0] = NO_EDGE;
92 matching_pursuit(&mp, c->options.full_search, price, max_edges,
93 y_state, range, domain_pool, coeff, wfa, c);
94
95 /*
96 * Next approximation attempt: remove domain block mp->indices [0]
97 * from domain pool (vector with smallest costs) and run the
98 * matching pursuit again.
99 */
100 if (c->options.second_domain_block) {
101 mp_t tmp_mp;
102
103 tmp_mp = mp; /* initial value */
104
105 tmp_mp.exclude[0] = tmp_mp.indices [0];
106 tmp_mp.exclude[1] = NO_EDGE;
107
108 matching_pursuit(&tmp_mp, c->options.full_search, price, max_edges,
109 y_state, range, domain_pool, coeff, wfa, c);
110 if (tmp_mp.costs < mp.costs) /* success */
111 mp = tmp_mp;
112 }
113
114 /*
115 * Next approximation attempt: check whether some coefficients have
116 * been quantized to zero. Vectors causing the underflow are
117 * removed from the domain pool and then the matching pursuit
118 * algorithm is run again (until underflow doesn't occur anymore).
119 */
120 if (c->options.check_for_underflow) {
121 mp_t tmp_mp;
122 int iteration;
123
124 tmp_mp = mp; /* initial value */
125 iteration = -1; /* initial value */
126
127 do {
128 int i;
129
130 ++iteration;
131 tmp_mp.exclude[iteration] = NO_EDGE;
132
133 for (i = 0; isdomain(tmp_mp.indices[i]); ++i) {
134 if (tmp_mp.weight [i] == 0) {
135 tmp_mp.exclude[iteration] = tmp_mp.indices [i];
136 break;
137 }
138 }
139 if (isdomain (tmp_mp.exclude [iteration])) {
140 /* try again */
141 tmp_mp.exclude [iteration + 1] = NO_EDGE;
142
143 matching_pursuit(&tmp_mp, c->options.full_search, price,
144 max_edges, y_state, range, domain_pool,
145 coeff, wfa, c);
146 if (tmp_mp.costs < mp.costs) /* success */
147 mp = tmp_mp;
148 }
149 } while (isdomain (tmp_mp.exclude [iteration])
150 && iteration < MAXEDGES - 1);
151 }
152
153 /*
154 * Next approximation attempt: check whether some coefficients have
155 * been quantized to +/- max-value. Vectors causing the overflow are
156 * removed from the domain pool and then the matching pursuit
157 * algorithm is run again (until overflow doesn't occur anymore).
158 */
159 if (c->options.check_for_overflow) {
160 mp_t tmp_mp;
161 int iteration;
162
163 tmp_mp = mp; /* initial value */
164 iteration = -1; /* initial value */
165
166 do {
167 int i;
168
169 ++iteration;
170 tmp_mp.exclude[iteration] = NO_EDGE;
171
172 for (i = 0; isdomain (tmp_mp.indices [i]); ++i) {
173 rpf_t * const rpf =
174 tmp_mp.indices [i] ? coeff->rpf : coeff->dc_rpf;
175
176 if (tmp_mp.weight [i] == btor (rtob (200, rpf), rpf)
177 || tmp_mp.weight [i] == btor (rtob (-200, rpf), rpf)) {
178 tmp_mp.exclude [iteration] = tmp_mp.indices [i];
179 break;
180 }
181 }
182
183 if (isdomain(tmp_mp.exclude[iteration])) {
184 /* try again */
185 tmp_mp.exclude[iteration + 1] = NO_EDGE;
186
187 matching_pursuit(&tmp_mp, c->options.full_search, price,
188 max_edges, y_state, range, domain_pool,
189 coeff, wfa, c);
190 if (tmp_mp.costs < mp.costs) /* success */
191 mp = tmp_mp;
192 }
193 } while (isdomain (tmp_mp.exclude [iteration])
194 && iteration < MAXEDGES - 1);
195 }
196
197 /*
198 * Finally, check whether the best approximation has costs
199 * smaller than 'max_costs'.
200 */
201 if (mp.costs < max_costs) {
202 int edge;
203 int new_index, old_index;
204
205 new_index = 0;
206 for (old_index = 0; isdomain (mp.indices[old_index]); ++old_index) {
207 if (mp.weight [old_index] != 0) {
208 mp.indices [new_index] = mp.indices [old_index];
209 mp.into [new_index] = mp.into [old_index];
210 mp.weight [new_index] = mp.weight [old_index];
211 ++new_index;
212 }
213 }
214 mp.indices [new_index] = NO_EDGE;
215 mp.into [new_index] = NO_EDGE;
216
217 /*
218 * Update of probability models
219 */
220 {
221 word_t * const domain_blocks =
222 domain_pool->generate(range->level, y_state,
223 wfa,
224 domain_pool->model);
225 domain_pool->update(domain_blocks, mp.indices,
226 range->level, y_state, wfa,
227 domain_pool->model);
228 coeff->update (mp.weight, mp.into, range->level, coeff);
229
230 Free(domain_blocks);
231 }
232
233 for (edge = 0; isedge (mp.indices [edge]); ++edge) {
234 range->into [edge] = mp.into [edge];
235 range->weight [edge] = mp.weight [edge];
236 }
237 range->into [edge] = NO_EDGE;
238 range->matrix_bits = mp.matrix_bits;
239 range->weights_bits = mp.weights_bits;
240 range->err = mp.err;
241 } else {
242 range->into [0] = NO_EDGE;
243 mp.costs = MAXCOSTS;
244 }
245
246 return mp.costs;
247 }
248
249
250
251 /*****************************************************************************
252
253 local variables
254
255 *****************************************************************************/
256
257 static real_t norm_ortho_vector [MAXSTATES];
258 /*
259 * Square-norm of the i-th vector of the orthogonal basis (OB)
260 * ||o_i||^2; i = 0, ... ,n
261 */
262 static real_t ip_image_ortho_vector [MAXEDGES];
263 /*
264 * Inner product between the i-th vector of the OB and the given range:
265 * <b, o_i>; i = 0, ... ,n
266 */
267 static real_t ip_domain_ortho_vector [MAXSTATES][MAXEDGES];
268 /*
269 * Inner product between the i-th vector of the OB and the image of domain j:
270 * <s_j, o_i>; j = 0, ... , wfa->states; i = 0, ... ,n,
271 */
272 static real_t rem_denominator [MAXSTATES];
273 static real_t rem_numerator [MAXSTATES];
274 /*
275 * At step n of the orthogonalization the comparative value
276 * (numerator_i / denominator_i):= <b, o_n>^2 / ||o_n|| ,
277 * is computed for every domain i,
278 * where o_n := s_i - \sum(k = 0, ... , n-1) {(<s_i, o_k> / ||o_k||^2) o_k}
279 * To avoid computing the same values over and over again,
280 * the constant (remaining) parts of every domain are
281 * stored in 'rem_numerator' and 'rem_denominator' separately
282 */
283 static bool_t used [MAXSTATES];
284 /*
285 * Shows whether a domain image was already used in a
286 * linear combination (YES) or not (NO)
287 */
288
289 /*****************************************************************************
290
291 private code
292
293 *****************************************************************************/
294
295 static void
296 matching_pursuit (mp_t *mp, bool_t full_search, real_t price,
297 unsigned max_edges, int y_state, const range_t *range,
298 const domain_pool_t *domain_pool, const coeff_t *coeff,
299 const wfa_t *wfa, const coding_t *c)
300 /*
301 * Find an approximation of the current 'range' with a linear
302 * combination of vectors of the 'domain_pool'. The linear
303 * combination is generated step by step with the matching pursuit
304 * algorithm. If flag 'full_search' is set then compute complete set
305 * of linear combinations with n = {0, ..., 'max_edges'} vectors and
306 * return the best one. Otherwise abort the computation as soon as
307 * costs (LC (n + 1)) exceed costs ( LC (n)) and return the
308 * sub-optimal solution. 'price' is the langrange multiplier
309 * weighting rate and distortion. 'band' is the current color band
310 * and 'y_state' the corresponding state in the Y component at same
311 * pixel position. 'domain_pool' gives the set of available vectors,
312 * and 'coeff' the model for the linear factors. The number of
313 * elements in the linear combination is limited by 'max_edges'. In
314 * 'mp', vectors may be specified which should be excluded during the
315 * approximation.
316 *
317 * No return value.
318 *
319 * Side effects:
320 * vectors, factors, rate, distortion and costs are stored in 'mp'
321 */
322 {
323 unsigned n; /* current vector of the OB */
324 int index; /* best fitting domain image */
325 unsigned domain; /* counter */
326 real_t norm; /* norm of range image */
327 real_t additional_bits; /* bits for mc, nd, and tree */
328 word_t *domain_blocks; /* current set of domain images */
329 const real_t min_norm = 2e-3; /* lower bound of norm */
330 unsigned best_n = 0;
331 unsigned size = size_of_level (range->level);
332
333 /*
334 * Initialize domain pool and inner product arrays
335 */
336 domain_blocks = domain_pool->generate (range->level, y_state, wfa,
337 domain_pool->model);
338 for (domain = 0; domain_blocks [domain] >= 0; domain++)
339 {
340 used [domain] = NO;
341 rem_denominator [domain] = /* norm of domain */
342 get_ip_state_state (domain_blocks [domain], domain_blocks [domain],
343 range->level, c);
344 if (rem_denominator [domain] / size < min_norm)
345 used [domain] = YES; /* don't use domains with small norm */
346 else
347 rem_numerator [domain] /* inner product <s_domain, b> */
348 = get_ip_image_state (range->image, range->address,
349 range->level, domain_blocks [domain], c);
350 if (!used [domain] && fabs (rem_numerator [domain]) < min_norm)
351 used [domain] = YES;
352 }
353
354 /*
355 * Exclude all domain blocks given in array 'mp->exclude'
356 */
357 for (n = 0; isdomain (mp->exclude [n]); n++)
358 used [mp->exclude [n]] = YES;
359
360 /*
361 * Compute the approximation costs if 'range' is approximated with
362 * no linear combination, i.e. the error is equal to the square
363 * of the image norm and the size of the automaton is determined by
364 * storing only zero elements in the current matrix row
365 */
366 for (norm = 0, n = 0; n < size; n++)
367 norm += square (c->pixels [range->address * size + n]);
368
369 additional_bits = range->tree_bits + range->mv_tree_bits
370 + range->mv_coord_bits + range->nd_tree_bits
371 + range->nd_weights_bits;
372
373 mp->err = norm;
374 mp->weights_bits = 0;
375 mp->matrix_bits = domain_pool->bits (domain_blocks, NULL, range->level,
376 y_state, wfa, domain_pool->model);
377 mp->costs = (mp->matrix_bits + mp->weights_bits
378 + additional_bits) * price + mp->err;
379
380 n = 0;
381 do
382 {
383 /*
384 * Current approximation is: b = d_0 o_0 + ... + d_(n-1) o_(n-1)
385 * with corresponding costs 'range->err + range->bits * p'.
386 * For all remaining state images s_i (used[s_i] == NO) set
387 * o_n : = s_i - \sum(k = 0, ... , n-1) {(<s_i, o_k> / ||o_k||^2) o_k}
388 * and try to beat current costs.
389 * Choose that vector for the next orthogonalization step,
390 * which has minimal costs: s_index.
391 * (No progress is indicated by index == -1)
392 */
393
394 real_t min_matrix_bits = 0;
395 real_t min_weights_bits = 0;
396 real_t min_error = 0;
397 real_t min_weight [MAXEDGES];
398 real_t min_costs = full_search ? MAXCOSTS : mp->costs;
399
400 for (index = -1, domain = 0; domain_blocks [domain] >= 0; domain++)
401 if (!used [domain])
402 {
403 real_t matrix_bits, weights_bits;
404 /*
405 * To speed up the search through the domain images,
406 * the costs of using domain image 'domain' as next vector
407 * can be approximated in a first step:
408 * improvement of image quality
409 * <= square (rem_numerator[domain]) / rem_denominator[domain]
410 */
411 {
412 word_t vectors [MAXEDGES + 1];
413 word_t states [MAXEDGES + 1];
414 real_t weights [MAXEDGES + 1];
415 unsigned i, k;
416
417 for (i = 0, k = 0; k < n; k++)
418 if (mp->weight [k] != 0)
419 {
420 vectors [i] = mp->indices [k];
421 states [i] = domain_blocks [vectors [i]];
422 weights [i] = mp->weight [k];
423 i++;
424 }
425 vectors [i] = domain;
426 states [i] = domain_blocks [domain];
427 weights [i] = 0.5;
428 vectors [i + 1] = -1;
429 states [i + 1] = -1;
430
431 weights_bits = coeff->bits (weights, states, range->level,
432 coeff);
433 matrix_bits = domain_pool->bits (domain_blocks, vectors,
434 range->level, y_state,
435 wfa, domain_pool->model);
436 }
437 if (((matrix_bits + weights_bits + additional_bits) * price +
438 mp->err -
439 square (rem_numerator[domain]) / rem_denominator[domain])
440 < min_costs)
441 {
442 /*
443 * 1.) Compute the weights (linear factors) c_i of the
444 * linear combination
445 * b = c_0 v_0 + ... + c_(n-1) v_(n-1) + c_n v_'domain'
446 * Use backward substitution to obtain c_i from the linear
447 * factors of the lin. comb. b = d_0 o_0 + ... + d_n o_n
448 * of the corresponding orthogonal vectors {o_0, ..., o_n}.
449 * Vector o_n of the orthogonal basis is obtained by using
450 * vector 'v_domain' in step n of the Gram Schmidt
451 * orthogonalization (see above for definition of o_n).
452 * Recursive formula for the coefficients c_i:
453 * c_n := <b, o_n> / ||o_n||^2
454 * for i = n - 1, ... , 0:
455 * c_i := <b, o_i> / ||o_i||^2 +
456 * \sum (k = i + 1, ... , n){ c_k <v_k, o_i>
457 * / ||o_i||^2 }
458 * 2.) Because linear factors are stored with reduced
459 * precision factor c_i is rounded with the given
460 * precision in step i of the recursive formula.
461 */
462
463 unsigned k; /* counter */
464 int l; /* counter */
465 real_t m_bits; /* number of matrix bits to store */
466 real_t w_bits; /* number of weights bits to store */
467 real_t r [MAXEDGES]; /* rounded linear factors */
468 real_t f [MAXEDGES]; /* linear factors */
469 int v [MAXEDGES]; /* mapping of domains to vectors*/
470 real_t costs; /* current approximation costs */
471 real_t m_err; /* current approximation error */
472
473 f [n] = rem_numerator [domain] / rem_denominator [domain];
474 v [n] = domain; /* corresponding mapping */
475 for (k = 0; k < n; k++)
476 {
477 f[k] = ip_image_ortho_vector[k] / norm_ortho_vector[k];
478 v [k] = mp->indices [k];
479 }
480
481 for (l = n; l >= 0; --l) {
482 rpf_t * const rpf = domain_blocks[v[l]]
483 ? coeff->rpf : coeff->dc_rpf;
484
485 unsigned int k;
486
487 r[l] = f[l] = btor(rtob(f[l], rpf), rpf);
488
489 {
490 real_t const fl = f[l];
491
492 for (k = 0; k < l; ++k) {
493 f[k] -= fl * ip_domain_ortho_vector[v[l]][k]
494 / norm_ortho_vector[k];
495 }
496 }
497 }
498
499 /*
500 * Compute the number of output bits of the linear
501 * combination and store the weights with reduced
502 * precision. The resulting linear combination is
503 * b = r_0 v_0 + ... + r_(n-1) v_(n-1) + r_n v_'domain'
504 */
505 {
506 word_t vectors [MAXEDGES + 1];
507 word_t states [MAXEDGES + 1];
508 real_t weights [MAXEDGES + 1];
509 int i;
510
511 for (i = 0, k = 0; k <= n; k++)
512 if (f [k] != 0)
513 {
514 vectors [i] = v [k];
515 states [i] = domain_blocks [v [k]];
516 weights [i] = f [k];
517 i++;
518 }
519 vectors [i] = -1;
520 states [i] = -1;
521
522 w_bits =
523 coeff->bits(weights, states, range->level, coeff);
524 m_bits = domain_pool->bits (domain_blocks, vectors,
525 range->level, y_state,
526 wfa, domain_pool->model);
527 }
528
529 /*
530 * To compute the approximation error, the corresponding
531 * linear factors of the linear combination
532 * b = r_0 o_0 + ... + r_(n-1) o_(n-1) + r_n o_'domain'
533 * with orthogonal vectors must be computed with following
534 * formula:
535 * r_i := r_i +
536 * \sum (k = i + 1, ... , n) { r_k <v_k, o_i>
537 * / ||o_i||^2 }
538 */
539 for (l = 0; (unsigned) l <= n; l++)
540 {
541 /*
542 * compute <v_n, o_n>
543 */
544 real_t a;
545
546 a = get_ip_state_state (domain_blocks [v [l]],
547 domain_blocks [domain],
548 range->level, c);
549 for (k = 0; k < n; k++)
550 a -= ip_domain_ortho_vector [v [l]][k]
551 / norm_ortho_vector [k]
552 * ip_domain_ortho_vector [domain][k];
553 ip_domain_ortho_vector [v [l]][n] = a;
554 }
555 norm_ortho_vector [n] = rem_denominator [domain];
556 ip_image_ortho_vector [n] = rem_numerator [domain];
557
558 for (k = 0; k <= n; k++)
559 for (l = k + 1; (unsigned) l <= n; l++)
560 r [k] += ip_domain_ortho_vector [v [l]][k] * r [l]
561 / norm_ortho_vector [k];
562 /*
563 * Compute approximation error:
564 * error := ||b||^2 +
565 * \sum (k = 0, ... , n){r_k^2 ||o_k||^2 - 2 r_k <b, o_k>}
566 */
567 m_err = norm;
568 for (k = 0; k <= n; k++)
569 m_err += square (r [k]) * norm_ortho_vector [k]
570 - 2 * r [k] * ip_image_ortho_vector [k];
571 if (m_err < 0) /* TODO: return MAXCOSTS */
572 warning ("Negative image norm: %f"
573 " (current domain: %d, level = %d)",
574 (double) m_err, domain, range->level);
575
576 costs = (m_bits + w_bits + additional_bits) * price + m_err;
577 if (costs < min_costs) /* found a better approximation */
578 {
579 index = domain;
580 min_costs = costs;
581 min_matrix_bits = m_bits;
582 min_weights_bits = w_bits;
583 min_error = m_err;
584 for (k = 0; k <= n; k++)
585 min_weight [k] = f [k];
586 }
587 }
588 }
589
590 if (index >= 0) /* found a better approximation */
591 {
592 if (min_costs < mp->costs)
593 {
594 unsigned k;
595
596 mp->costs = min_costs;
597 mp->err = min_error;
598 mp->matrix_bits = min_matrix_bits;
599 mp->weights_bits = min_weights_bits;
600
601 for (k = 0; k <= n; k++)
602 mp->weight [k] = min_weight [k];
603
604 best_n = n + 1;
605 }
606
607 mp->indices [n] = index;
608 mp->into [n] = domain_blocks [index];
609
610 used [index] = YES;
611
612 /*
613 * Gram-Schmidt orthogonalization step n
614 */
615 orthogonalize (index, n, range->level, min_norm, domain_blocks, c);
616 n++;
617 }
618 }
619 while (n < max_edges && index >= 0);
620
621 mp->indices [best_n] = NO_EDGE;
622
623 mp->costs = (mp->matrix_bits + mp->weights_bits + additional_bits) * price
624 + mp->err;
625
626 Free (domain_blocks);
627 }
628
629 static void
630 orthogonalize (unsigned index, unsigned n, unsigned level, real_t min_norm,
631 const word_t *domain_blocks, const coding_t *c)
632 /*
633 * Step 'n' of the Gram-Schmidt orthogonalization procedure:
634 * vector 'index' is orthogonalized with respect to the set
635 * {u_[0], ... , u_['n' - 1]}. The size of the image blocks is given by
636 * 'level'. If the denominator gets smaller than 'min_norm' then
637 * the corresponding domain is excluded from the list of available
638 * domain blocks.
639 *
640 * No return value.
641 *
642 * Side effects:
643 * The remainder values (numerator and denominator) of
644 * all 'domain_blocks' are updated.
645 */
646 {
647 unsigned domain;
648
649 ip_image_ortho_vector [n] = rem_numerator [index];
650 norm_ortho_vector [n] = rem_denominator [index];
651
652 /*
653 * Compute inner products between all domain images and
654 * vector n of the orthogonal basis:
655 * for (i = 0, ... , wfa->states)
656 * <s_i, o_n> := <s_i, v_n> -
657 * \sum (k = 0, ... , n - 1){ <v_n, o_k> <s_i, o_k> / ||o_k||^2}
658 * Moreover the denominator and numerator parts of the comparative
659 * value are updated.
660 */
661 for (domain = 0; domain_blocks [domain] >= 0; domain++)
662 if (!used [domain])
663 {
664 unsigned k;
665 real_t tmp = get_ip_state_state (domain_blocks [index],
666 domain_blocks [domain], level, c);
667
668 for (k = 0; k < n; k++)
669 tmp -= ip_domain_ortho_vector [domain][k] / norm_ortho_vector [k]
670 * ip_domain_ortho_vector [index][k];
671 ip_domain_ortho_vector [domain][n] = tmp;
672 rem_denominator [domain] -= square (tmp) / norm_ortho_vector [n];
673 rem_numerator [domain] -= ip_image_ortho_vector [n]
674 / norm_ortho_vector [n]
675 * ip_domain_ortho_vector [domain][n] ;
676
677 /*
678 * Exclude vectors with small denominator
679 */
680 if (!used [domain])
681 if (rem_denominator [domain] / size_of_level (level) < min_norm)
682 used [domain] = YES;
683 }
684 }
685
686
687